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