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