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