Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
9 | ||
b3b94faa DT |
10 | #include <linux/slab.h> |
11 | #include <linux/spinlock.h> | |
12 | #include <linux/completion.h> | |
13 | #include <linux/buffer_head.h> | |
5c676f6d | 14 | #include <linux/gfs2_ondisk.h> |
71b86f56 | 15 | #include <linux/crc32.h> |
7d308590 | 16 | #include <linux/lm_interface.h> |
b3b94faa DT |
17 | |
18 | #include "gfs2.h" | |
5c676f6d | 19 | #include "incore.h" |
b3b94faa DT |
20 | #include "bmap.h" |
21 | #include "glock.h" | |
22 | #include "inode.h" | |
b3b94faa | 23 | #include "meta_io.h" |
b3b94faa DT |
24 | #include "quota.h" |
25 | #include "rgrp.h" | |
26 | #include "trans.h" | |
18ec7d5c | 27 | #include "dir.h" |
5c676f6d | 28 | #include "util.h" |
ba7f7290 | 29 | #include "ops_address.h" |
b3b94faa DT |
30 | |
31 | /* This doesn't need to be that large as max 64 bit pointers in a 4k | |
32 | * block is 512, so __u16 is fine for that. It saves stack space to | |
33 | * keep it small. | |
34 | */ | |
35 | struct metapath { | |
dbac6710 | 36 | struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT]; |
b3b94faa DT |
37 | __u16 mp_list[GFS2_MAX_META_HEIGHT]; |
38 | }; | |
39 | ||
40 | typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh, | |
b44b84d7 AV |
41 | struct buffer_head *bh, __be64 *top, |
42 | __be64 *bottom, unsigned int height, | |
b3b94faa DT |
43 | void *data); |
44 | ||
45 | struct strip_mine { | |
46 | int sm_first; | |
47 | unsigned int sm_height; | |
48 | }; | |
49 | ||
f25ef0c1 SW |
50 | /** |
51 | * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page | |
52 | * @ip: the inode | |
53 | * @dibh: the dinode buffer | |
54 | * @block: the block number that was allocated | |
55 | * @private: any locked page held by the caller process | |
56 | * | |
57 | * Returns: errno | |
58 | */ | |
59 | ||
60 | static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh, | |
cd915493 | 61 | u64 block, struct page *page) |
f25ef0c1 | 62 | { |
f25ef0c1 SW |
63 | struct inode *inode = &ip->i_inode; |
64 | struct buffer_head *bh; | |
65 | int release = 0; | |
66 | ||
67 | if (!page || page->index) { | |
68 | page = grab_cache_page(inode->i_mapping, 0); | |
69 | if (!page) | |
70 | return -ENOMEM; | |
71 | release = 1; | |
72 | } | |
73 | ||
74 | if (!PageUptodate(page)) { | |
75 | void *kaddr = kmap(page); | |
76 | ||
77 | memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), | |
78 | ip->i_di.di_size); | |
79 | memset(kaddr + ip->i_di.di_size, 0, | |
80 | PAGE_CACHE_SIZE - ip->i_di.di_size); | |
81 | kunmap(page); | |
82 | ||
83 | SetPageUptodate(page); | |
84 | } | |
85 | ||
86 | if (!page_has_buffers(page)) | |
87 | create_empty_buffers(page, 1 << inode->i_blkbits, | |
88 | (1 << BH_Uptodate)); | |
89 | ||
90 | bh = page_buffers(page); | |
91 | ||
92 | if (!buffer_mapped(bh)) | |
93 | map_bh(bh, inode->i_sb, block); | |
94 | ||
95 | set_buffer_uptodate(bh); | |
eaf96527 SW |
96 | if (!gfs2_is_jdata(ip)) |
97 | mark_buffer_dirty(bh); | |
bf36a713 | 98 | if (!gfs2_is_writeback(ip)) |
8475487b | 99 | gfs2_trans_add_bh(ip->i_gl, bh, 0); |
f25ef0c1 SW |
100 | |
101 | if (release) { | |
102 | unlock_page(page); | |
103 | page_cache_release(page); | |
104 | } | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
b3b94faa DT |
109 | /** |
110 | * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big | |
111 | * @ip: The GFS2 inode to unstuff | |
112 | * @unstuffer: the routine that handles unstuffing a non-zero length file | |
113 | * @private: private data for the unstuffer | |
114 | * | |
115 | * This routine unstuffs a dinode and returns it to a "normal" state such | |
116 | * that the height can be grown in the traditional way. | |
117 | * | |
118 | * Returns: errno | |
119 | */ | |
120 | ||
f25ef0c1 | 121 | int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page) |
b3b94faa DT |
122 | { |
123 | struct buffer_head *bh, *dibh; | |
48516ced | 124 | struct gfs2_dinode *di; |
cd915493 | 125 | u64 block = 0; |
18ec7d5c | 126 | int isdir = gfs2_is_dir(ip); |
b3b94faa DT |
127 | int error; |
128 | ||
129 | down_write(&ip->i_rw_mutex); | |
130 | ||
131 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
132 | if (error) | |
133 | goto out; | |
907b9bce | 134 | |
b3b94faa DT |
135 | if (ip->i_di.di_size) { |
136 | /* Get a free block, fill it with the stuffed data, | |
137 | and write it out to disk */ | |
138 | ||
b45e41d7 SW |
139 | unsigned int n = 1; |
140 | block = gfs2_alloc_block(ip, &n); | |
18ec7d5c | 141 | if (isdir) { |
5731be53 | 142 | gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1); |
61e085a8 | 143 | error = gfs2_dir_get_new_buffer(ip, block, &bh); |
b3b94faa DT |
144 | if (error) |
145 | goto out_brelse; | |
48516ced | 146 | gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header), |
b3b94faa DT |
147 | dibh, sizeof(struct gfs2_dinode)); |
148 | brelse(bh); | |
149 | } else { | |
f25ef0c1 | 150 | error = gfs2_unstuffer_page(ip, dibh, block, page); |
b3b94faa DT |
151 | if (error) |
152 | goto out_brelse; | |
153 | } | |
154 | } | |
155 | ||
156 | /* Set up the pointer to the new block */ | |
157 | ||
d4e9c4c3 | 158 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
48516ced | 159 | di = (struct gfs2_dinode *)dibh->b_data; |
b3b94faa DT |
160 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
161 | ||
162 | if (ip->i_di.di_size) { | |
48516ced | 163 | *(__be64 *)(di + 1) = cpu_to_be64(block); |
b3b94faa | 164 | ip->i_di.di_blocks++; |
9e2dbdac | 165 | gfs2_set_inode_blocks(&ip->i_inode); |
48516ced | 166 | di->di_blocks = cpu_to_be64(ip->i_di.di_blocks); |
b3b94faa DT |
167 | } |
168 | ||
ecc30c79 | 169 | ip->i_height = 1; |
48516ced | 170 | di->di_height = cpu_to_be16(1); |
b3b94faa | 171 | |
a91ea69f | 172 | out_brelse: |
b3b94faa | 173 | brelse(dibh); |
a91ea69f | 174 | out: |
b3b94faa | 175 | up_write(&ip->i_rw_mutex); |
b3b94faa DT |
176 | return error; |
177 | } | |
178 | ||
b3b94faa DT |
179 | /** |
180 | * build_height - Build a metadata tree of the requested height | |
181 | * @ip: The GFS2 inode | |
182 | * @height: The height to build to | |
183 | * | |
b3b94faa DT |
184 | * |
185 | * Returns: errno | |
186 | */ | |
187 | ||
dbac6710 | 188 | static int build_height(struct inode *inode, struct metapath *mp, unsigned height) |
b3b94faa | 189 | { |
feaa7bba | 190 | struct gfs2_inode *ip = GFS2_I(inode); |
ecc30c79 | 191 | unsigned new_height = height - ip->i_height; |
e90c01e1 | 192 | struct buffer_head *dibh; |
48516ced | 193 | struct gfs2_dinode *di; |
b3b94faa | 194 | int error; |
b44b84d7 | 195 | __be64 *bp; |
e90c01e1 | 196 | u64 bn; |
b45e41d7 | 197 | unsigned n, i = 0; |
b3b94faa | 198 | |
ecc30c79 | 199 | if (height <= ip->i_height) |
e90c01e1 | 200 | return 0; |
b3b94faa | 201 | |
e90c01e1 SW |
202 | error = gfs2_meta_inode_buffer(ip, &dibh); |
203 | if (error) | |
204 | return error; | |
b3b94faa | 205 | |
b45e41d7 SW |
206 | do { |
207 | n = new_height - i; | |
208 | bn = gfs2_alloc_block(ip, &n); | |
209 | gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, n); | |
210 | do { | |
211 | mp->mp_bh[i] = gfs2_meta_new(ip->i_gl, bn++); | |
212 | gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i], 1); | |
213 | i++; | |
214 | } while(i < n); | |
215 | } while(i < new_height); | |
907b9bce | 216 | |
e90c01e1 | 217 | n = 0; |
dbac6710 | 218 | bn = mp->mp_bh[0]->b_blocknr; |
e90c01e1 SW |
219 | if (new_height > 1) { |
220 | for(; n < new_height-1; n++) { | |
dbac6710 | 221 | gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN, |
b3b94faa | 222 | GFS2_FORMAT_IN); |
dbac6710 | 223 | gfs2_buffer_clear_tail(mp->mp_bh[n], |
e90c01e1 | 224 | sizeof(struct gfs2_meta_header)); |
dbac6710 | 225 | bp = (__be64 *)(mp->mp_bh[n]->b_data + |
e90c01e1 | 226 | sizeof(struct gfs2_meta_header)); |
dbac6710 SW |
227 | *bp = cpu_to_be64(mp->mp_bh[n+1]->b_blocknr); |
228 | brelse(mp->mp_bh[n]); | |
229 | mp->mp_bh[n] = NULL; | |
b3b94faa | 230 | } |
b3b94faa | 231 | } |
dbac6710 SW |
232 | gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN); |
233 | gfs2_buffer_copy_tail(mp->mp_bh[n], sizeof(struct gfs2_meta_header), | |
e90c01e1 | 234 | dibh, sizeof(struct gfs2_dinode)); |
dbac6710 | 235 | brelse(mp->mp_bh[n]); |
e90c01e1 | 236 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
48516ced | 237 | di = (struct gfs2_dinode *)dibh->b_data; |
e90c01e1 | 238 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
48516ced | 239 | *(__be64 *)(di + 1) = cpu_to_be64(bn); |
ecc30c79 | 240 | ip->i_height += new_height; |
e90c01e1 | 241 | ip->i_di.di_blocks += new_height; |
9e2dbdac | 242 | gfs2_set_inode_blocks(&ip->i_inode); |
ecc30c79 | 243 | di->di_height = cpu_to_be16(ip->i_height); |
48516ced | 244 | di->di_blocks = cpu_to_be64(ip->i_di.di_blocks); |
e90c01e1 SW |
245 | brelse(dibh); |
246 | return error; | |
b3b94faa DT |
247 | } |
248 | ||
249 | /** | |
250 | * find_metapath - Find path through the metadata tree | |
251 | * @ip: The inode pointer | |
252 | * @mp: The metapath to return the result in | |
253 | * @block: The disk block to look up | |
254 | * | |
255 | * This routine returns a struct metapath structure that defines a path | |
256 | * through the metadata of inode "ip" to get to block "block". | |
257 | * | |
258 | * Example: | |
259 | * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a | |
260 | * filesystem with a blocksize of 4096. | |
261 | * | |
262 | * find_metapath() would return a struct metapath structure set to: | |
263 | * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48, | |
264 | * and mp_list[2] = 165. | |
265 | * | |
266 | * That means that in order to get to the block containing the byte at | |
267 | * offset 101342453, we would load the indirect block pointed to by pointer | |
268 | * 0 in the dinode. We would then load the indirect block pointed to by | |
269 | * pointer 48 in that indirect block. We would then load the data block | |
270 | * pointed to by pointer 165 in that indirect block. | |
271 | * | |
272 | * ---------------------------------------- | |
273 | * | Dinode | | | |
274 | * | | 4| | |
275 | * | |0 1 2 3 4 5 9| | |
276 | * | | 6| | |
277 | * ---------------------------------------- | |
278 | * | | |
279 | * | | |
280 | * V | |
281 | * ---------------------------------------- | |
282 | * | Indirect Block | | |
283 | * | 5| | |
284 | * | 4 4 4 4 4 5 5 1| | |
285 | * |0 5 6 7 8 9 0 1 2| | |
286 | * ---------------------------------------- | |
287 | * | | |
288 | * | | |
289 | * V | |
290 | * ---------------------------------------- | |
291 | * | Indirect Block | | |
292 | * | 1 1 1 1 1 5| | |
293 | * | 6 6 6 6 6 1| | |
294 | * |0 3 4 5 6 7 2| | |
295 | * ---------------------------------------- | |
296 | * | | |
297 | * | | |
298 | * V | |
299 | * ---------------------------------------- | |
300 | * | Data block containing offset | | |
301 | * | 101342453 | | |
302 | * | | | |
303 | * | | | |
304 | * ---------------------------------------- | |
305 | * | |
306 | */ | |
307 | ||
cd915493 | 308 | static void find_metapath(struct gfs2_inode *ip, u64 block, |
568f4c96 | 309 | struct metapath *mp) |
b3b94faa | 310 | { |
feaa7bba | 311 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
312 | unsigned int i; |
313 | ||
ecc30c79 | 314 | for (i = ip->i_height; i--;) |
7eabb77e | 315 | mp->mp_list[i] = do_div(block, sdp->sd_inptrs); |
b3b94faa DT |
316 | |
317 | } | |
318 | ||
319 | /** | |
320 | * metapointer - Return pointer to start of metadata in a buffer | |
b3b94faa DT |
321 | * @height: The metadata height (0 = dinode) |
322 | * @mp: The metapath | |
323 | * | |
324 | * Return a pointer to the block number of the next height of the metadata | |
325 | * tree given a buffer containing the pointer to the current height of the | |
326 | * metadata tree. | |
327 | */ | |
328 | ||
dbac6710 SW |
329 | static inline __be64 *metapointer(int *boundary, unsigned int height, |
330 | const struct metapath *mp) | |
b3b94faa | 331 | { |
dbac6710 | 332 | struct buffer_head *bh = mp->mp_bh[height]; |
b3b94faa DT |
333 | unsigned int head_size = (height > 0) ? |
334 | sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode); | |
b44b84d7 | 335 | __be64 *ptr; |
fd88de56 | 336 | *boundary = 0; |
b44b84d7 AV |
337 | ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height]; |
338 | if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size)) | |
fd88de56 SW |
339 | *boundary = 1; |
340 | return ptr; | |
b3b94faa DT |
341 | } |
342 | ||
343 | /** | |
344 | * lookup_block - Get the next metadata block in metadata tree | |
345 | * @ip: The GFS2 inode | |
b3b94faa DT |
346 | * @height: The height of the tree (0 = dinode) |
347 | * @mp: The metapath | |
348 | * @create: Non-zero if we may create a new meatdata block | |
349 | * @new: Used to indicate if we did create a new metadata block | |
350 | * @block: the returned disk block number | |
351 | * | |
352 | * Given a metatree, complete to a particular height, checks to see if the next | |
353 | * height of the tree exists. If not the next height of the tree is created. | |
354 | * The block number of the next height of the metadata tree is returned. | |
355 | * | |
356 | */ | |
357 | ||
dbac6710 SW |
358 | static int lookup_block(struct gfs2_inode *ip, unsigned int height, |
359 | struct metapath *mp, int create, | |
cd915493 | 360 | int *new, u64 *block) |
b3b94faa | 361 | { |
fd88de56 | 362 | int boundary; |
dbac6710 | 363 | __be64 *ptr = metapointer(&boundary, height, mp); |
b45e41d7 | 364 | unsigned int n = 1; |
b3b94faa DT |
365 | |
366 | if (*ptr) { | |
367 | *block = be64_to_cpu(*ptr); | |
fd88de56 | 368 | return boundary; |
b3b94faa DT |
369 | } |
370 | ||
371 | *block = 0; | |
372 | ||
373 | if (!create) | |
fd88de56 | 374 | return 0; |
b3b94faa | 375 | |
b45e41d7 | 376 | *block = gfs2_alloc_block(ip, &n); |
1639431a | 377 | if (height != ip->i_height - 1 || gfs2_is_dir(ip)) |
5731be53 | 378 | gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), *block, 1); |
b3b94faa | 379 | |
dbac6710 | 380 | gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[height], 1); |
b3b94faa DT |
381 | |
382 | *ptr = cpu_to_be64(*block); | |
383 | ip->i_di.di_blocks++; | |
9e2dbdac | 384 | gfs2_set_inode_blocks(&ip->i_inode); |
b3b94faa DT |
385 | |
386 | *new = 1; | |
fd88de56 | 387 | return 0; |
b3b94faa DT |
388 | } |
389 | ||
11707ea0 | 390 | static int lookup_metapath(struct inode *inode, struct metapath *mp, |
dbac6710 | 391 | int create, int *new, u64 *dblock) |
11707ea0 | 392 | { |
dbac6710 | 393 | struct buffer_head *bh; |
11707ea0 SW |
394 | struct gfs2_inode *ip = GFS2_I(inode); |
395 | unsigned int end_of_metadata = ip->i_height - 1; | |
396 | unsigned int x; | |
dbac6710 | 397 | int ret = gfs2_meta_inode_buffer(ip, &bh); |
11707ea0 SW |
398 | if (ret) |
399 | return ret; | |
400 | ||
dbac6710 | 401 | mp->mp_bh[0] = bh; |
11707ea0 SW |
402 | |
403 | for (x = 0; x < end_of_metadata; x++) { | |
dbac6710 | 404 | lookup_block(ip, x, mp, create, new, dblock); |
c85a665f | 405 | if (!*dblock) |
11707ea0 SW |
406 | return 0; |
407 | ||
dbac6710 | 408 | ret = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &mp->mp_bh[x+1]); |
11707ea0 SW |
409 | if (ret) |
410 | return ret; | |
411 | } | |
412 | ||
dbac6710 SW |
413 | return lookup_block(ip, end_of_metadata, mp, create, new, dblock); |
414 | } | |
415 | ||
416 | static void release_metapath(struct metapath *mp) | |
417 | { | |
418 | int i; | |
419 | ||
420 | for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) | |
421 | if (mp->mp_bh[i]) | |
422 | brelse(mp->mp_bh[i]); | |
11707ea0 SW |
423 | } |
424 | ||
4cf1ed81 SW |
425 | static inline void bmap_lock(struct inode *inode, int create) |
426 | { | |
427 | struct gfs2_inode *ip = GFS2_I(inode); | |
428 | if (create) | |
429 | down_write(&ip->i_rw_mutex); | |
430 | else | |
431 | down_read(&ip->i_rw_mutex); | |
432 | } | |
433 | ||
434 | static inline void bmap_unlock(struct inode *inode, int create) | |
435 | { | |
436 | struct gfs2_inode *ip = GFS2_I(inode); | |
437 | if (create) | |
438 | up_write(&ip->i_rw_mutex); | |
439 | else | |
440 | up_read(&ip->i_rw_mutex); | |
441 | } | |
442 | ||
b3b94faa | 443 | /** |
4cf1ed81 | 444 | * gfs2_block_map - Map a block from an inode to a disk block |
fd88de56 | 445 | * @inode: The inode |
b3b94faa | 446 | * @lblock: The logical block number |
4cf1ed81 | 447 | * @bh_map: The bh to be mapped |
b3b94faa DT |
448 | * |
449 | * Find the block number on the current device which corresponds to an | |
450 | * inode's block. If the block had to be created, "new" will be set. | |
451 | * | |
452 | * Returns: errno | |
453 | */ | |
454 | ||
e9e1ef2b BP |
455 | int gfs2_block_map(struct inode *inode, sector_t lblock, |
456 | struct buffer_head *bh_map, int create) | |
b3b94faa | 457 | { |
feaa7bba SW |
458 | struct gfs2_inode *ip = GFS2_I(inode); |
459 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
ecc30c79 | 460 | unsigned int bsize = sdp->sd_sb.sb_bsize; |
b3b94faa | 461 | int error = 0; |
7a6bbacb SW |
462 | int new = 0; |
463 | u64 dblock = 0; | |
464 | int boundary; | |
23591256 | 465 | unsigned int maxlen = bh_map->b_size >> inode->i_blkbits; |
4cf1ed81 SW |
466 | struct metapath mp; |
467 | u64 size; | |
ecc30c79 | 468 | const u64 *arr = sdp->sd_heightsize; |
7276b3b0 SW |
469 | BUG_ON(maxlen == 0); |
470 | ||
b3b94faa | 471 | if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip))) |
7a6bbacb | 472 | return 0; |
b3b94faa | 473 | |
dbac6710 | 474 | memset(mp.mp_bh, 0, sizeof(mp.mp_bh)); |
4cf1ed81 SW |
475 | bmap_lock(inode, create); |
476 | clear_buffer_mapped(bh_map); | |
477 | clear_buffer_new(bh_map); | |
478 | clear_buffer_boundary(bh_map); | |
ecc30c79 SW |
479 | if (gfs2_is_dir(ip)) { |
480 | bsize = sdp->sd_jbsize; | |
481 | arr = sdp->sd_jheightsize; | |
482 | } | |
4cf1ed81 SW |
483 | size = (lblock + 1) * bsize; |
484 | ||
ecc30c79 SW |
485 | if (size > arr[ip->i_height]) { |
486 | u8 height = ip->i_height; | |
487 | if (!create) | |
488 | goto out_ok; | |
489 | while (size > arr[height]) | |
490 | height++; | |
dbac6710 | 491 | error = build_height(inode, &mp, height); |
ecc30c79 SW |
492 | if (error) |
493 | goto out_fail; | |
b3b94faa DT |
494 | } |
495 | ||
4cf1ed81 | 496 | find_metapath(ip, lblock, &mp); |
dbac6710 | 497 | error = lookup_metapath(inode, &mp, create, &new, &dblock); |
11707ea0 | 498 | if (error < 0) |
4cf1ed81 | 499 | goto out_fail; |
11707ea0 | 500 | boundary = error; |
b3b94faa | 501 | |
7a6bbacb SW |
502 | if (dblock) { |
503 | map_bh(bh_map, inode->i_sb, dblock); | |
504 | if (boundary) | |
4cf1ed81 | 505 | set_buffer_boundary(bh_map); |
7a6bbacb | 506 | if (new) { |
dbac6710 SW |
507 | gfs2_trans_add_bh(ip->i_gl, mp.mp_bh[0], 1); |
508 | gfs2_dinode_out(ip, mp.mp_bh[0]->b_data); | |
7a6bbacb | 509 | set_buffer_new(bh_map); |
dbac6710 | 510 | goto out_ok; |
7a6bbacb SW |
511 | } |
512 | while(--maxlen && !buffer_boundary(bh_map)) { | |
513 | u64 eblock; | |
dbac6710 SW |
514 | mp.mp_list[ip->i_height - 1]++; |
515 | boundary = lookup_block(ip, ip->i_height - 1, &mp, 0, &new, &eblock); | |
7a6bbacb SW |
516 | if (eblock != ++dblock) |
517 | break; | |
f3b30912 | 518 | bh_map->b_size += (1 << inode->i_blkbits); |
7a6bbacb SW |
519 | if (boundary) |
520 | set_buffer_boundary(bh_map); | |
b3b94faa DT |
521 | } |
522 | } | |
4cf1ed81 SW |
523 | out_ok: |
524 | error = 0; | |
525 | out_fail: | |
dbac6710 | 526 | release_metapath(&mp); |
fd88de56 | 527 | bmap_unlock(inode, create); |
4cf1ed81 | 528 | return error; |
fd88de56 SW |
529 | } |
530 | ||
941e6d7d SW |
531 | /* |
532 | * Deprecated: do not use in new code | |
533 | */ | |
fd88de56 SW |
534 | int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen) |
535 | { | |
23591256 | 536 | struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 }; |
7a6bbacb | 537 | int ret; |
fd88de56 SW |
538 | int create = *new; |
539 | ||
540 | BUG_ON(!extlen); | |
541 | BUG_ON(!dblock); | |
542 | BUG_ON(!new); | |
543 | ||
23591256 | 544 | bh.b_size = 1 << (inode->i_blkbits + 5); |
e9e1ef2b | 545 | ret = gfs2_block_map(inode, lblock, &bh, create); |
7a6bbacb SW |
546 | *extlen = bh.b_size >> inode->i_blkbits; |
547 | *dblock = bh.b_blocknr; | |
548 | if (buffer_new(&bh)) | |
549 | *new = 1; | |
550 | else | |
551 | *new = 0; | |
552 | return ret; | |
b3b94faa DT |
553 | } |
554 | ||
555 | /** | |
556 | * recursive_scan - recursively scan through the end of a file | |
557 | * @ip: the inode | |
558 | * @dibh: the dinode buffer | |
559 | * @mp: the path through the metadata to the point to start | |
560 | * @height: the height the recursion is at | |
561 | * @block: the indirect block to look at | |
562 | * @first: 1 if this is the first block | |
563 | * @bc: the call to make for each piece of metadata | |
564 | * @data: data opaque to this function to pass to @bc | |
565 | * | |
566 | * When this is first called @height and @block should be zero and | |
567 | * @first should be 1. | |
568 | * | |
569 | * Returns: errno | |
570 | */ | |
571 | ||
572 | static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh, | |
573 | struct metapath *mp, unsigned int height, | |
cd915493 | 574 | u64 block, int first, block_call_t bc, |
b3b94faa DT |
575 | void *data) |
576 | { | |
feaa7bba | 577 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa | 578 | struct buffer_head *bh = NULL; |
b44b84d7 | 579 | __be64 *top, *bottom; |
cd915493 | 580 | u64 bn; |
b3b94faa DT |
581 | int error; |
582 | int mh_size = sizeof(struct gfs2_meta_header); | |
583 | ||
584 | if (!height) { | |
585 | error = gfs2_meta_inode_buffer(ip, &bh); | |
586 | if (error) | |
587 | return error; | |
588 | dibh = bh; | |
589 | ||
b44b84d7 AV |
590 | top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0]; |
591 | bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs; | |
b3b94faa DT |
592 | } else { |
593 | error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh); | |
594 | if (error) | |
595 | return error; | |
596 | ||
b44b84d7 | 597 | top = (__be64 *)(bh->b_data + mh_size) + |
c5392124 | 598 | (first ? mp->mp_list[height] : 0); |
b3b94faa | 599 | |
b44b84d7 | 600 | bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs; |
b3b94faa DT |
601 | } |
602 | ||
603 | error = bc(ip, dibh, bh, top, bottom, height, data); | |
604 | if (error) | |
605 | goto out; | |
606 | ||
ecc30c79 | 607 | if (height < ip->i_height - 1) |
b3b94faa DT |
608 | for (; top < bottom; top++, first = 0) { |
609 | if (!*top) | |
610 | continue; | |
611 | ||
612 | bn = be64_to_cpu(*top); | |
613 | ||
614 | error = recursive_scan(ip, dibh, mp, height + 1, bn, | |
615 | first, bc, data); | |
616 | if (error) | |
617 | break; | |
618 | } | |
619 | ||
a91ea69f | 620 | out: |
b3b94faa | 621 | brelse(bh); |
b3b94faa DT |
622 | return error; |
623 | } | |
624 | ||
625 | /** | |
626 | * do_strip - Look for a layer a particular layer of the file and strip it off | |
627 | * @ip: the inode | |
628 | * @dibh: the dinode buffer | |
629 | * @bh: A buffer of pointers | |
630 | * @top: The first pointer in the buffer | |
631 | * @bottom: One more than the last pointer | |
632 | * @height: the height this buffer is at | |
633 | * @data: a pointer to a struct strip_mine | |
634 | * | |
635 | * Returns: errno | |
636 | */ | |
637 | ||
638 | static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh, | |
b44b84d7 | 639 | struct buffer_head *bh, __be64 *top, __be64 *bottom, |
b3b94faa DT |
640 | unsigned int height, void *data) |
641 | { | |
feaa7bba SW |
642 | struct strip_mine *sm = data; |
643 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | |
b3b94faa | 644 | struct gfs2_rgrp_list rlist; |
cd915493 SW |
645 | u64 bn, bstart; |
646 | u32 blen; | |
b44b84d7 | 647 | __be64 *p; |
b3b94faa DT |
648 | unsigned int rg_blocks = 0; |
649 | int metadata; | |
650 | unsigned int revokes = 0; | |
651 | int x; | |
652 | int error; | |
653 | ||
654 | if (!*top) | |
655 | sm->sm_first = 0; | |
656 | ||
657 | if (height != sm->sm_height) | |
658 | return 0; | |
659 | ||
660 | if (sm->sm_first) { | |
661 | top++; | |
662 | sm->sm_first = 0; | |
663 | } | |
664 | ||
ecc30c79 | 665 | metadata = (height != ip->i_height - 1); |
b3b94faa DT |
666 | if (metadata) |
667 | revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs; | |
668 | ||
6dbd8224 | 669 | error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh); |
b3b94faa DT |
670 | if (error) |
671 | return error; | |
672 | ||
673 | memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); | |
674 | bstart = 0; | |
675 | blen = 0; | |
676 | ||
677 | for (p = top; p < bottom; p++) { | |
678 | if (!*p) | |
679 | continue; | |
680 | ||
681 | bn = be64_to_cpu(*p); | |
682 | ||
683 | if (bstart + blen == bn) | |
684 | blen++; | |
685 | else { | |
686 | if (bstart) | |
687 | gfs2_rlist_add(sdp, &rlist, bstart); | |
688 | ||
689 | bstart = bn; | |
690 | blen = 1; | |
691 | } | |
692 | } | |
693 | ||
694 | if (bstart) | |
695 | gfs2_rlist_add(sdp, &rlist, bstart); | |
696 | else | |
697 | goto out; /* Nothing to do */ | |
698 | ||
fe6c991c | 699 | gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE); |
b3b94faa DT |
700 | |
701 | for (x = 0; x < rlist.rl_rgrps; x++) { | |
702 | struct gfs2_rgrpd *rgd; | |
5c676f6d | 703 | rgd = rlist.rl_ghs[x].gh_gl->gl_object; |
bb8d8a6f | 704 | rg_blocks += rgd->rd_length; |
b3b94faa DT |
705 | } |
706 | ||
707 | error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); | |
708 | if (error) | |
709 | goto out_rlist; | |
710 | ||
711 | error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + | |
712 | RES_INDIRECT + RES_STATFS + RES_QUOTA, | |
713 | revokes); | |
714 | if (error) | |
715 | goto out_rg_gunlock; | |
716 | ||
717 | down_write(&ip->i_rw_mutex); | |
718 | ||
d4e9c4c3 SW |
719 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
720 | gfs2_trans_add_bh(ip->i_gl, bh, 1); | |
b3b94faa DT |
721 | |
722 | bstart = 0; | |
723 | blen = 0; | |
724 | ||
725 | for (p = top; p < bottom; p++) { | |
726 | if (!*p) | |
727 | continue; | |
728 | ||
729 | bn = be64_to_cpu(*p); | |
730 | ||
731 | if (bstart + blen == bn) | |
732 | blen++; | |
733 | else { | |
734 | if (bstart) { | |
735 | if (metadata) | |
736 | gfs2_free_meta(ip, bstart, blen); | |
737 | else | |
738 | gfs2_free_data(ip, bstart, blen); | |
739 | } | |
740 | ||
741 | bstart = bn; | |
742 | blen = 1; | |
743 | } | |
744 | ||
745 | *p = 0; | |
746 | if (!ip->i_di.di_blocks) | |
747 | gfs2_consist_inode(ip); | |
748 | ip->i_di.di_blocks--; | |
9e2dbdac | 749 | gfs2_set_inode_blocks(&ip->i_inode); |
b3b94faa DT |
750 | } |
751 | if (bstart) { | |
752 | if (metadata) | |
753 | gfs2_free_meta(ip, bstart, blen); | |
754 | else | |
755 | gfs2_free_data(ip, bstart, blen); | |
756 | } | |
757 | ||
4bd91ba1 | 758 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
b3b94faa | 759 | |
539e5d6b | 760 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
761 | |
762 | up_write(&ip->i_rw_mutex); | |
763 | ||
764 | gfs2_trans_end(sdp); | |
765 | ||
a91ea69f | 766 | out_rg_gunlock: |
b3b94faa | 767 | gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); |
a91ea69f | 768 | out_rlist: |
b3b94faa | 769 | gfs2_rlist_free(&rlist); |
a91ea69f | 770 | out: |
6dbd8224 | 771 | gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh); |
b3b94faa DT |
772 | return error; |
773 | } | |
774 | ||
775 | /** | |
776 | * do_grow - Make a file look bigger than it is | |
777 | * @ip: the inode | |
778 | * @size: the size to set the file to | |
779 | * | |
780 | * Called with an exclusive lock on @ip. | |
781 | * | |
782 | * Returns: errno | |
783 | */ | |
784 | ||
cd915493 | 785 | static int do_grow(struct gfs2_inode *ip, u64 size) |
b3b94faa | 786 | { |
feaa7bba | 787 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
788 | struct gfs2_alloc *al; |
789 | struct buffer_head *dibh; | |
b3b94faa DT |
790 | int error; |
791 | ||
792 | al = gfs2_alloc_get(ip); | |
793 | ||
794 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
795 | if (error) | |
796 | goto out; | |
797 | ||
2933f925 | 798 | error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid); |
b3b94faa DT |
799 | if (error) |
800 | goto out_gunlock_q; | |
801 | ||
802 | al->al_requested = sdp->sd_max_height + RES_DATA; | |
803 | ||
804 | error = gfs2_inplace_reserve(ip); | |
805 | if (error) | |
806 | goto out_gunlock_q; | |
807 | ||
808 | error = gfs2_trans_begin(sdp, | |
bb8d8a6f | 809 | sdp->sd_max_height + al->al_rgd->rd_length + |
b3b94faa DT |
810 | RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0); |
811 | if (error) | |
812 | goto out_ipres; | |
813 | ||
814 | if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { | |
ecc30c79 | 815 | const u64 *arr = sdp->sd_heightsize; |
b3b94faa | 816 | if (gfs2_is_stuffed(ip)) { |
f25ef0c1 | 817 | error = gfs2_unstuff_dinode(ip, NULL); |
b3b94faa DT |
818 | if (error) |
819 | goto out_end_trans; | |
820 | } | |
821 | ||
ecc30c79 SW |
822 | down_write(&ip->i_rw_mutex); |
823 | if (size > arr[ip->i_height]) { | |
dbac6710 | 824 | struct metapath mp; |
ecc30c79 SW |
825 | u8 height = ip->i_height; |
826 | while(size > arr[height]) | |
827 | height++; | |
dbac6710 | 828 | error = build_height(&ip->i_inode, &mp, height); |
b3b94faa | 829 | } |
ecc30c79 SW |
830 | up_write(&ip->i_rw_mutex); |
831 | if (error) | |
832 | goto out_end_trans; | |
b3b94faa DT |
833 | } |
834 | ||
835 | ip->i_di.di_size = size; | |
4bd91ba1 | 836 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
b3b94faa DT |
837 | |
838 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
839 | if (error) | |
840 | goto out_end_trans; | |
841 | ||
d4e9c4c3 | 842 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 843 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
844 | brelse(dibh); |
845 | ||
a91ea69f | 846 | out_end_trans: |
b3b94faa | 847 | gfs2_trans_end(sdp); |
a91ea69f | 848 | out_ipres: |
b3b94faa | 849 | gfs2_inplace_release(ip); |
a91ea69f | 850 | out_gunlock_q: |
b3b94faa | 851 | gfs2_quota_unlock(ip); |
a91ea69f | 852 | out: |
b3b94faa | 853 | gfs2_alloc_put(ip); |
b3b94faa DT |
854 | return error; |
855 | } | |
856 | ||
ba7f7290 SW |
857 | |
858 | /** | |
859 | * gfs2_block_truncate_page - Deal with zeroing out data for truncate | |
860 | * | |
861 | * This is partly borrowed from ext3. | |
862 | */ | |
863 | static int gfs2_block_truncate_page(struct address_space *mapping) | |
864 | { | |
865 | struct inode *inode = mapping->host; | |
866 | struct gfs2_inode *ip = GFS2_I(inode); | |
ba7f7290 SW |
867 | loff_t from = inode->i_size; |
868 | unsigned long index = from >> PAGE_CACHE_SHIFT; | |
869 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
870 | unsigned blocksize, iblock, length, pos; | |
871 | struct buffer_head *bh; | |
872 | struct page *page; | |
ba7f7290 SW |
873 | int err; |
874 | ||
875 | page = grab_cache_page(mapping, index); | |
876 | if (!page) | |
877 | return 0; | |
878 | ||
879 | blocksize = inode->i_sb->s_blocksize; | |
880 | length = blocksize - (offset & (blocksize - 1)); | |
881 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | |
882 | ||
883 | if (!page_has_buffers(page)) | |
884 | create_empty_buffers(page, blocksize, 0); | |
885 | ||
886 | /* Find the buffer that contains "offset" */ | |
887 | bh = page_buffers(page); | |
888 | pos = blocksize; | |
889 | while (offset >= pos) { | |
890 | bh = bh->b_this_page; | |
891 | iblock++; | |
892 | pos += blocksize; | |
893 | } | |
894 | ||
895 | err = 0; | |
896 | ||
897 | if (!buffer_mapped(bh)) { | |
e9e1ef2b | 898 | gfs2_block_map(inode, iblock, bh, 0); |
ba7f7290 SW |
899 | /* unmapped? It's a hole - nothing to do */ |
900 | if (!buffer_mapped(bh)) | |
901 | goto unlock; | |
902 | } | |
903 | ||
904 | /* Ok, it's mapped. Make sure it's up-to-date */ | |
905 | if (PageUptodate(page)) | |
906 | set_buffer_uptodate(bh); | |
907 | ||
908 | if (!buffer_uptodate(bh)) { | |
909 | err = -EIO; | |
910 | ll_rw_block(READ, 1, &bh); | |
911 | wait_on_buffer(bh); | |
912 | /* Uhhuh. Read error. Complain and punt. */ | |
913 | if (!buffer_uptodate(bh)) | |
914 | goto unlock; | |
1875f2f3 | 915 | err = 0; |
ba7f7290 SW |
916 | } |
917 | ||
bf36a713 | 918 | if (!gfs2_is_writeback(ip)) |
ba7f7290 SW |
919 | gfs2_trans_add_bh(ip->i_gl, bh, 0); |
920 | ||
eebd2aa3 | 921 | zero_user(page, offset, length); |
ba7f7290 SW |
922 | |
923 | unlock: | |
924 | unlock_page(page); | |
925 | page_cache_release(page); | |
926 | return err; | |
927 | } | |
928 | ||
cd915493 | 929 | static int trunc_start(struct gfs2_inode *ip, u64 size) |
b3b94faa | 930 | { |
feaa7bba | 931 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
932 | struct buffer_head *dibh; |
933 | int journaled = gfs2_is_jdata(ip); | |
934 | int error; | |
935 | ||
936 | error = gfs2_trans_begin(sdp, | |
c5392124 | 937 | RES_DINODE + (journaled ? RES_JDATA : 0), 0); |
b3b94faa DT |
938 | if (error) |
939 | return error; | |
940 | ||
941 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
942 | if (error) | |
943 | goto out; | |
944 | ||
945 | if (gfs2_is_stuffed(ip)) { | |
946 | ip->i_di.di_size = size; | |
4bd91ba1 | 947 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
d4e9c4c3 | 948 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 949 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
950 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size); |
951 | error = 1; | |
952 | ||
953 | } else { | |
cd915493 | 954 | if (size & (u64)(sdp->sd_sb.sb_bsize - 1)) |
feaa7bba | 955 | error = gfs2_block_truncate_page(ip->i_inode.i_mapping); |
b3b94faa DT |
956 | |
957 | if (!error) { | |
958 | ip->i_di.di_size = size; | |
4bd91ba1 | 959 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
b3b94faa | 960 | ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG; |
d4e9c4c3 | 961 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 962 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
963 | } |
964 | } | |
965 | ||
966 | brelse(dibh); | |
967 | ||
a91ea69f | 968 | out: |
b3b94faa | 969 | gfs2_trans_end(sdp); |
b3b94faa DT |
970 | return error; |
971 | } | |
972 | ||
cd915493 | 973 | static int trunc_dealloc(struct gfs2_inode *ip, u64 size) |
b3b94faa | 974 | { |
ecc30c79 | 975 | unsigned int height = ip->i_height; |
cd915493 | 976 | u64 lblock; |
b3b94faa DT |
977 | struct metapath mp; |
978 | int error; | |
979 | ||
980 | if (!size) | |
981 | lblock = 0; | |
18ec7d5c | 982 | else |
feaa7bba | 983 | lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift; |
b3b94faa DT |
984 | |
985 | find_metapath(ip, lblock, &mp); | |
986 | gfs2_alloc_get(ip); | |
987 | ||
988 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
989 | if (error) | |
990 | goto out; | |
991 | ||
992 | while (height--) { | |
993 | struct strip_mine sm; | |
994 | sm.sm_first = !!size; | |
995 | sm.sm_height = height; | |
996 | ||
997 | error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm); | |
998 | if (error) | |
999 | break; | |
1000 | } | |
1001 | ||
1002 | gfs2_quota_unhold(ip); | |
1003 | ||
a91ea69f | 1004 | out: |
b3b94faa DT |
1005 | gfs2_alloc_put(ip); |
1006 | return error; | |
1007 | } | |
1008 | ||
1009 | static int trunc_end(struct gfs2_inode *ip) | |
1010 | { | |
feaa7bba | 1011 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1012 | struct buffer_head *dibh; |
1013 | int error; | |
1014 | ||
1015 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
1016 | if (error) | |
1017 | return error; | |
1018 | ||
1019 | down_write(&ip->i_rw_mutex); | |
1020 | ||
1021 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1022 | if (error) | |
1023 | goto out; | |
1024 | ||
1025 | if (!ip->i_di.di_size) { | |
ecc30c79 | 1026 | ip->i_height = 0; |
ce276b06 | 1027 | ip->i_goal = ip->i_no_addr; |
b3b94faa DT |
1028 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
1029 | } | |
4bd91ba1 | 1030 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
b3b94faa DT |
1031 | ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG; |
1032 | ||
d4e9c4c3 | 1033 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 1034 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
1035 | brelse(dibh); |
1036 | ||
a91ea69f | 1037 | out: |
b3b94faa | 1038 | up_write(&ip->i_rw_mutex); |
b3b94faa | 1039 | gfs2_trans_end(sdp); |
b3b94faa DT |
1040 | return error; |
1041 | } | |
1042 | ||
1043 | /** | |
1044 | * do_shrink - make a file smaller | |
1045 | * @ip: the inode | |
1046 | * @size: the size to make the file | |
1047 | * @truncator: function to truncate the last partial block | |
1048 | * | |
1049 | * Called with an exclusive lock on @ip. | |
1050 | * | |
1051 | * Returns: errno | |
1052 | */ | |
1053 | ||
cd915493 | 1054 | static int do_shrink(struct gfs2_inode *ip, u64 size) |
b3b94faa DT |
1055 | { |
1056 | int error; | |
1057 | ||
aa6a85a9 | 1058 | error = trunc_start(ip, size); |
b3b94faa DT |
1059 | if (error < 0) |
1060 | return error; | |
1061 | if (error > 0) | |
1062 | return 0; | |
1063 | ||
1064 | error = trunc_dealloc(ip, size); | |
1065 | if (!error) | |
1066 | error = trunc_end(ip); | |
1067 | ||
1068 | return error; | |
1069 | } | |
1070 | ||
a13b8c5f WC |
1071 | static int do_touch(struct gfs2_inode *ip, u64 size) |
1072 | { | |
1073 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); | |
1074 | struct buffer_head *dibh; | |
1075 | int error; | |
1076 | ||
1077 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
1078 | if (error) | |
1079 | return error; | |
1080 | ||
1081 | down_write(&ip->i_rw_mutex); | |
1082 | ||
1083 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1084 | if (error) | |
1085 | goto do_touch_out; | |
1086 | ||
1087 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; | |
1088 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
1089 | gfs2_dinode_out(ip, dibh->b_data); | |
1090 | brelse(dibh); | |
1091 | ||
1092 | do_touch_out: | |
1093 | up_write(&ip->i_rw_mutex); | |
1094 | gfs2_trans_end(sdp); | |
1095 | return error; | |
1096 | } | |
1097 | ||
b3b94faa | 1098 | /** |
666a2c53 | 1099 | * gfs2_truncatei - make a file a given size |
b3b94faa DT |
1100 | * @ip: the inode |
1101 | * @size: the size to make the file | |
1102 | * @truncator: function to truncate the last partial block | |
1103 | * | |
1104 | * The file size can grow, shrink, or stay the same size. | |
1105 | * | |
1106 | * Returns: errno | |
1107 | */ | |
1108 | ||
cd915493 | 1109 | int gfs2_truncatei(struct gfs2_inode *ip, u64 size) |
b3b94faa DT |
1110 | { |
1111 | int error; | |
1112 | ||
b60623c2 | 1113 | if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode))) |
b3b94faa DT |
1114 | return -EINVAL; |
1115 | ||
1116 | if (size > ip->i_di.di_size) | |
1117 | error = do_grow(ip, size); | |
a13b8c5f | 1118 | else if (size < ip->i_di.di_size) |
aa6a85a9 | 1119 | error = do_shrink(ip, size); |
a13b8c5f WC |
1120 | else |
1121 | /* update time stamps */ | |
1122 | error = do_touch(ip, size); | |
b3b94faa DT |
1123 | |
1124 | return error; | |
1125 | } | |
1126 | ||
1127 | int gfs2_truncatei_resume(struct gfs2_inode *ip) | |
1128 | { | |
1129 | int error; | |
1130 | error = trunc_dealloc(ip, ip->i_di.di_size); | |
1131 | if (!error) | |
1132 | error = trunc_end(ip); | |
1133 | return error; | |
1134 | } | |
1135 | ||
1136 | int gfs2_file_dealloc(struct gfs2_inode *ip) | |
1137 | { | |
1138 | return trunc_dealloc(ip, 0); | |
1139 | } | |
1140 | ||
1141 | /** | |
1142 | * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file | |
1143 | * @ip: the file | |
1144 | * @len: the number of bytes to be written to the file | |
1145 | * @data_blocks: returns the number of data blocks required | |
1146 | * @ind_blocks: returns the number of indirect blocks required | |
1147 | * | |
1148 | */ | |
1149 | ||
1150 | void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len, | |
1151 | unsigned int *data_blocks, unsigned int *ind_blocks) | |
1152 | { | |
feaa7bba | 1153 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
1154 | unsigned int tmp; |
1155 | ||
18ec7d5c | 1156 | if (gfs2_is_dir(ip)) { |
5c676f6d | 1157 | *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2; |
b3b94faa DT |
1158 | *ind_blocks = 3 * (sdp->sd_max_jheight - 1); |
1159 | } else { | |
1160 | *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3; | |
1161 | *ind_blocks = 3 * (sdp->sd_max_height - 1); | |
1162 | } | |
1163 | ||
1164 | for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) { | |
5c676f6d | 1165 | tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs); |
b3b94faa DT |
1166 | *ind_blocks += tmp; |
1167 | } | |
1168 | } | |
1169 | ||
1170 | /** | |
1171 | * gfs2_write_alloc_required - figure out if a write will require an allocation | |
1172 | * @ip: the file being written to | |
1173 | * @offset: the offset to write to | |
1174 | * @len: the number of bytes being written | |
1175 | * @alloc_required: set to 1 if an alloc is required, 0 otherwise | |
1176 | * | |
1177 | * Returns: errno | |
1178 | */ | |
1179 | ||
cd915493 | 1180 | int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset, |
b3b94faa DT |
1181 | unsigned int len, int *alloc_required) |
1182 | { | |
feaa7bba | 1183 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
941e6d7d SW |
1184 | struct buffer_head bh; |
1185 | unsigned int shift; | |
1186 | u64 lblock, lblock_stop, size; | |
b3b94faa DT |
1187 | |
1188 | *alloc_required = 0; | |
1189 | ||
1190 | if (!len) | |
1191 | return 0; | |
1192 | ||
1193 | if (gfs2_is_stuffed(ip)) { | |
1194 | if (offset + len > | |
1195 | sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) | |
1196 | *alloc_required = 1; | |
1197 | return 0; | |
1198 | } | |
1199 | ||
941e6d7d SW |
1200 | *alloc_required = 1; |
1201 | shift = sdp->sd_sb.sb_bsize_shift; | |
18ec7d5c | 1202 | if (gfs2_is_dir(ip)) { |
b3b94faa DT |
1203 | unsigned int bsize = sdp->sd_jbsize; |
1204 | lblock = offset; | |
1205 | do_div(lblock, bsize); | |
1206 | lblock_stop = offset + len + bsize - 1; | |
1207 | do_div(lblock_stop, bsize); | |
1208 | } else { | |
1af53572 | 1209 | u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift; |
b3b94faa DT |
1210 | lblock = offset >> shift; |
1211 | lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift; | |
941e6d7d | 1212 | if (lblock_stop > end_of_file) |
05220535 | 1213 | return 0; |
b3b94faa DT |
1214 | } |
1215 | ||
941e6d7d SW |
1216 | size = (lblock_stop - lblock) << shift; |
1217 | do { | |
1218 | bh.b_state = 0; | |
1219 | bh.b_size = size; | |
1220 | gfs2_block_map(&ip->i_inode, lblock, &bh, 0); | |
1221 | if (!buffer_mapped(&bh)) | |
b3b94faa | 1222 | return 0; |
941e6d7d SW |
1223 | size -= bh.b_size; |
1224 | lblock += (bh.b_size >> ip->i_inode.i_blkbits); | |
1225 | } while(size > 0); | |
b3b94faa | 1226 | |
941e6d7d | 1227 | *alloc_required = 0; |
b3b94faa DT |
1228 | return 0; |
1229 | } | |
1230 |