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