2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/gfs2_ondisk.h>
15 #include <linux/crc32.h>
16 #include <linux/lm_interface.h>
29 #include "ops_address.h"
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
36 __u16 mp_list[GFS2_MAX_META_HEIGHT];
39 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
40 struct buffer_head *bh, __be64 *top,
41 __be64 *bottom, unsigned int height,
46 unsigned int sm_height;
50 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
52 * @dibh: the dinode buffer
53 * @block: the block number that was allocated
54 * @private: any locked page held by the caller process
59 static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
60 u64 block, struct page *page)
62 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
63 struct inode *inode = &ip->i_inode;
64 struct buffer_head *bh;
67 if (!page || page->index) {
68 page = grab_cache_page(inode->i_mapping, 0);
74 if (!PageUptodate(page)) {
75 void *kaddr = kmap(page);
77 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
79 memset(kaddr + ip->i_di.di_size, 0,
80 PAGE_CACHE_SIZE - ip->i_di.di_size);
83 SetPageUptodate(page);
86 if (!page_has_buffers(page))
87 create_empty_buffers(page, 1 << inode->i_blkbits,
90 bh = page_buffers(page);
92 if (!buffer_mapped(bh))
93 map_bh(bh, inode->i_sb, block);
95 set_buffer_uptodate(bh);
96 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
97 gfs2_trans_add_bh(ip->i_gl, bh, 0);
98 mark_buffer_dirty(bh);
102 page_cache_release(page);
109 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
110 * @ip: The GFS2 inode to unstuff
111 * @unstuffer: the routine that handles unstuffing a non-zero length file
112 * @private: private data for the unstuffer
114 * This routine unstuffs a dinode and returns it to a "normal" state such
115 * that the height can be grown in the traditional way.
120 int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
122 struct buffer_head *bh, *dibh;
123 struct gfs2_dinode *di;
125 int isdir = gfs2_is_dir(ip);
128 down_write(&ip->i_rw_mutex);
130 error = gfs2_meta_inode_buffer(ip, &dibh);
134 if (ip->i_di.di_size) {
135 /* Get a free block, fill it with the stuffed data,
136 and write it out to disk */
139 block = gfs2_alloc_meta(ip);
141 error = gfs2_dir_get_new_buffer(ip, block, &bh);
144 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
145 dibh, sizeof(struct gfs2_dinode));
148 block = gfs2_alloc_data(ip);
150 error = gfs2_unstuffer_page(ip, dibh, block, page);
156 /* Set up the pointer to the new block */
158 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
159 di = (struct gfs2_dinode *)dibh->b_data;
160 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
162 if (ip->i_di.di_size) {
163 *(__be64 *)(di + 1) = cpu_to_be64(block);
164 ip->i_di.di_blocks++;
165 gfs2_set_inode_blocks(&ip->i_inode);
166 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
169 ip->i_di.di_height = 1;
170 di->di_height = cpu_to_be16(1);
175 up_write(&ip->i_rw_mutex);
180 * calc_tree_height - Calculate the height of a metadata tree
181 * @ip: The GFS2 inode
182 * @size: The proposed size of the file
184 * Work out how tall a metadata tree needs to be in order to accommodate a
185 * file of a particular size. If size is less than the current size of
186 * the inode, then the current size of the inode is used instead of the
189 * Returns: the height the tree should be
192 static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size)
194 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
196 unsigned int max, height;
198 if (ip->i_di.di_size > size)
199 size = ip->i_di.di_size;
201 if (gfs2_is_dir(ip)) {
202 arr = sdp->sd_jheightsize;
203 max = sdp->sd_max_jheight;
205 arr = sdp->sd_heightsize;
206 max = sdp->sd_max_height;
209 for (height = 0; height < max; height++)
210 if (arr[height] >= size)
217 * build_height - Build a metadata tree of the requested height
218 * @ip: The GFS2 inode
219 * @height: The height to build to
225 static int build_height(struct inode *inode, unsigned height)
227 struct gfs2_inode *ip = GFS2_I(inode);
228 unsigned new_height = height - ip->i_di.di_height;
229 struct buffer_head *dibh;
230 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
231 struct gfs2_dinode *di;
237 if (height <= ip->i_di.di_height)
240 error = gfs2_meta_inode_buffer(ip, &dibh);
244 for(n = 0; n < new_height; n++) {
245 bn = gfs2_alloc_meta(ip);
246 blocks[n] = gfs2_meta_new(ip->i_gl, bn);
247 gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
251 bn = blocks[0]->b_blocknr;
252 if (new_height > 1) {
253 for(; n < new_height-1; n++) {
254 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
256 gfs2_buffer_clear_tail(blocks[n],
257 sizeof(struct gfs2_meta_header));
258 bp = (__be64 *)(blocks[n]->b_data +
259 sizeof(struct gfs2_meta_header));
260 *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
265 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
266 gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
267 dibh, sizeof(struct gfs2_dinode));
269 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
270 di = (struct gfs2_dinode *)dibh->b_data;
271 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
272 *(__be64 *)(di + 1) = cpu_to_be64(bn);
273 ip->i_di.di_height += new_height;
274 ip->i_di.di_blocks += new_height;
275 gfs2_set_inode_blocks(&ip->i_inode);
276 di->di_height = cpu_to_be16(ip->i_di.di_height);
277 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
283 * find_metapath - Find path through the metadata tree
284 * @ip: The inode pointer
285 * @mp: The metapath to return the result in
286 * @block: The disk block to look up
288 * This routine returns a struct metapath structure that defines a path
289 * through the metadata of inode "ip" to get to block "block".
292 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
293 * filesystem with a blocksize of 4096.
295 * find_metapath() would return a struct metapath structure set to:
296 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
297 * and mp_list[2] = 165.
299 * That means that in order to get to the block containing the byte at
300 * offset 101342453, we would load the indirect block pointed to by pointer
301 * 0 in the dinode. We would then load the indirect block pointed to by
302 * pointer 48 in that indirect block. We would then load the data block
303 * pointed to by pointer 165 in that indirect block.
305 * ----------------------------------------
310 * ----------------------------------------
314 * ----------------------------------------
318 * |0 5 6 7 8 9 0 1 2|
319 * ----------------------------------------
323 * ----------------------------------------
328 * ----------------------------------------
332 * ----------------------------------------
333 * | Data block containing offset |
337 * ----------------------------------------
341 static void find_metapath(struct gfs2_inode *ip, u64 block,
344 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
348 for (i = ip->i_di.di_height; i--;)
349 mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
354 * metapointer - Return pointer to start of metadata in a buffer
356 * @height: The metadata height (0 = dinode)
359 * Return a pointer to the block number of the next height of the metadata
360 * tree given a buffer containing the pointer to the current height of the
364 static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
365 unsigned int height, const struct metapath *mp)
367 unsigned int head_size = (height > 0) ?
368 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
371 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
372 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
378 * lookup_block - Get the next metadata block in metadata tree
379 * @ip: The GFS2 inode
380 * @bh: Buffer containing the pointers to metadata blocks
381 * @height: The height of the tree (0 = dinode)
383 * @create: Non-zero if we may create a new meatdata block
384 * @new: Used to indicate if we did create a new metadata block
385 * @block: the returned disk block number
387 * Given a metatree, complete to a particular height, checks to see if the next
388 * height of the tree exists. If not the next height of the tree is created.
389 * The block number of the next height of the metadata tree is returned.
393 static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
394 unsigned int height, struct metapath *mp, int create,
395 int *new, u64 *block)
398 __be64 *ptr = metapointer(bh, &boundary, height, mp);
401 *block = be64_to_cpu(*ptr);
410 if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
411 *block = gfs2_alloc_data(ip);
413 *block = gfs2_alloc_meta(ip);
415 gfs2_trans_add_bh(ip->i_gl, bh, 1);
417 *ptr = cpu_to_be64(*block);
418 ip->i_di.di_blocks++;
419 gfs2_set_inode_blocks(&ip->i_inode);
425 static inline void bmap_lock(struct inode *inode, int create)
427 struct gfs2_inode *ip = GFS2_I(inode);
429 down_write(&ip->i_rw_mutex);
431 down_read(&ip->i_rw_mutex);
434 static inline void bmap_unlock(struct inode *inode, int create)
436 struct gfs2_inode *ip = GFS2_I(inode);
438 up_write(&ip->i_rw_mutex);
440 up_read(&ip->i_rw_mutex);
444 * gfs2_block_map - Map a block from an inode to a disk block
446 * @lblock: The logical block number
447 * @bh_map: The bh to be mapped
449 * Find the block number on the current device which corresponds to an
450 * inode's block. If the block had to be created, "new" will be set.
455 int gfs2_block_map(struct inode *inode, u64 lblock, int create,
456 struct buffer_head *bh_map)
458 struct gfs2_inode *ip = GFS2_I(inode);
459 struct gfs2_sbd *sdp = GFS2_SB(inode);
460 struct buffer_head *bh;
463 unsigned int end_of_metadata;
469 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
475 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
478 bmap_lock(inode, create);
479 clear_buffer_mapped(bh_map);
480 clear_buffer_new(bh_map);
481 clear_buffer_boundary(bh_map);
482 bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
483 size = (lblock + 1) * bsize;
485 if (size > ip->i_di.di_size) {
486 height = calc_tree_height(ip, size);
487 if (ip->i_di.di_height < height) {
491 error = build_height(inode, height);
497 find_metapath(ip, lblock, &mp);
498 end_of_metadata = ip->i_di.di_height - 1;
499 error = gfs2_meta_inode_buffer(ip, &bh);
503 for (x = 0; x < end_of_metadata; x++) {
504 lookup_block(ip, bh, x, &mp, create, &new, &dblock);
509 error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
514 boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
516 map_bh(bh_map, inode->i_sb, dblock);
518 set_buffer_boundary(bh_map);
520 struct buffer_head *dibh;
521 error = gfs2_meta_inode_buffer(ip, &dibh);
523 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
524 gfs2_dinode_out(ip, dibh->b_data);
527 set_buffer_new(bh_map);
530 while(--maxlen && !buffer_boundary(bh_map)) {
533 mp.mp_list[end_of_metadata]++;
534 boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
535 if (eblock != ++dblock)
537 bh_map->b_size += (1 << inode->i_blkbits);
539 set_buffer_boundary(bh_map);
547 bmap_unlock(inode, create);
551 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
553 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
561 bh.b_size = 1 << (inode->i_blkbits + 5);
562 ret = gfs2_block_map(inode, lblock, create, &bh);
563 *extlen = bh.b_size >> inode->i_blkbits;
564 *dblock = bh.b_blocknr;
573 * recursive_scan - recursively scan through the end of a file
575 * @dibh: the dinode buffer
576 * @mp: the path through the metadata to the point to start
577 * @height: the height the recursion is at
578 * @block: the indirect block to look at
579 * @first: 1 if this is the first block
580 * @bc: the call to make for each piece of metadata
581 * @data: data opaque to this function to pass to @bc
583 * When this is first called @height and @block should be zero and
584 * @first should be 1.
589 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
590 struct metapath *mp, unsigned int height,
591 u64 block, int first, block_call_t bc,
594 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
595 struct buffer_head *bh = NULL;
596 __be64 *top, *bottom;
599 int mh_size = sizeof(struct gfs2_meta_header);
602 error = gfs2_meta_inode_buffer(ip, &bh);
607 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
608 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
610 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
614 top = (__be64 *)(bh->b_data + mh_size) +
615 (first ? mp->mp_list[height] : 0);
617 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
620 error = bc(ip, dibh, bh, top, bottom, height, data);
624 if (height < ip->i_di.di_height - 1)
625 for (; top < bottom; top++, first = 0) {
629 bn = be64_to_cpu(*top);
631 error = recursive_scan(ip, dibh, mp, height + 1, bn,
643 * do_strip - Look for a layer a particular layer of the file and strip it off
645 * @dibh: the dinode buffer
646 * @bh: A buffer of pointers
647 * @top: The first pointer in the buffer
648 * @bottom: One more than the last pointer
649 * @height: the height this buffer is at
650 * @data: a pointer to a struct strip_mine
655 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
656 struct buffer_head *bh, __be64 *top, __be64 *bottom,
657 unsigned int height, void *data)
659 struct strip_mine *sm = data;
660 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
661 struct gfs2_rgrp_list rlist;
665 unsigned int rg_blocks = 0;
667 unsigned int revokes = 0;
674 if (height != sm->sm_height)
682 metadata = (height != ip->i_di.di_height - 1);
684 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
686 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
690 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
694 for (p = top; p < bottom; p++) {
698 bn = be64_to_cpu(*p);
700 if (bstart + blen == bn)
704 gfs2_rlist_add(sdp, &rlist, bstart);
712 gfs2_rlist_add(sdp, &rlist, bstart);
714 goto out; /* Nothing to do */
716 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
718 for (x = 0; x < rlist.rl_rgrps; x++) {
719 struct gfs2_rgrpd *rgd;
720 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
721 rg_blocks += rgd->rd_ri.ri_length;
724 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
728 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
729 RES_INDIRECT + RES_STATFS + RES_QUOTA,
734 down_write(&ip->i_rw_mutex);
736 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
737 gfs2_trans_add_bh(ip->i_gl, bh, 1);
742 for (p = top; p < bottom; p++) {
746 bn = be64_to_cpu(*p);
748 if (bstart + blen == bn)
753 gfs2_free_meta(ip, bstart, blen);
755 gfs2_free_data(ip, bstart, blen);
763 if (!ip->i_di.di_blocks)
764 gfs2_consist_inode(ip);
765 ip->i_di.di_blocks--;
766 gfs2_set_inode_blocks(&ip->i_inode);
770 gfs2_free_meta(ip, bstart, blen);
772 gfs2_free_data(ip, bstart, blen);
775 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
777 gfs2_dinode_out(ip, dibh->b_data);
779 up_write(&ip->i_rw_mutex);
784 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
786 gfs2_rlist_free(&rlist);
788 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
793 * do_grow - Make a file look bigger than it is
795 * @size: the size to set the file to
797 * Called with an exclusive lock on @ip.
802 static int do_grow(struct gfs2_inode *ip, u64 size)
804 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
805 struct gfs2_alloc *al;
806 struct buffer_head *dibh;
810 al = gfs2_alloc_get(ip);
812 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
816 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
820 al->al_requested = sdp->sd_max_height + RES_DATA;
822 error = gfs2_inplace_reserve(ip);
826 error = gfs2_trans_begin(sdp,
827 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
828 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
832 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
833 if (gfs2_is_stuffed(ip)) {
834 error = gfs2_unstuff_dinode(ip, NULL);
839 h = calc_tree_height(ip, size);
840 if (ip->i_di.di_height < h) {
841 down_write(&ip->i_rw_mutex);
842 error = build_height(&ip->i_inode, h);
843 up_write(&ip->i_rw_mutex);
849 ip->i_di.di_size = size;
850 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
852 error = gfs2_meta_inode_buffer(ip, &dibh);
856 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
857 gfs2_dinode_out(ip, dibh->b_data);
863 gfs2_inplace_release(ip);
865 gfs2_quota_unlock(ip);
873 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
875 * This is partly borrowed from ext3.
877 static int gfs2_block_truncate_page(struct address_space *mapping)
879 struct inode *inode = mapping->host;
880 struct gfs2_inode *ip = GFS2_I(inode);
881 struct gfs2_sbd *sdp = GFS2_SB(inode);
882 loff_t from = inode->i_size;
883 unsigned long index = from >> PAGE_CACHE_SHIFT;
884 unsigned offset = from & (PAGE_CACHE_SIZE-1);
885 unsigned blocksize, iblock, length, pos;
886 struct buffer_head *bh;
891 page = grab_cache_page(mapping, index);
895 blocksize = inode->i_sb->s_blocksize;
896 length = blocksize - (offset & (blocksize - 1));
897 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
899 if (!page_has_buffers(page))
900 create_empty_buffers(page, blocksize, 0);
902 /* Find the buffer that contains "offset" */
903 bh = page_buffers(page);
905 while (offset >= pos) {
906 bh = bh->b_this_page;
913 if (!buffer_mapped(bh)) {
914 gfs2_get_block(inode, iblock, bh, 0);
915 /* unmapped? It's a hole - nothing to do */
916 if (!buffer_mapped(bh))
920 /* Ok, it's mapped. Make sure it's up-to-date */
921 if (PageUptodate(page))
922 set_buffer_uptodate(bh);
924 if (!buffer_uptodate(bh)) {
926 ll_rw_block(READ, 1, &bh);
928 /* Uhhuh. Read error. Complain and punt. */
929 if (!buffer_uptodate(bh))
933 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
934 gfs2_trans_add_bh(ip->i_gl, bh, 0);
936 kaddr = kmap_atomic(page, KM_USER0);
937 memset(kaddr + offset, 0, length);
938 flush_dcache_page(page);
939 kunmap_atomic(kaddr, KM_USER0);
943 page_cache_release(page);
947 static int trunc_start(struct gfs2_inode *ip, u64 size)
949 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
950 struct buffer_head *dibh;
951 int journaled = gfs2_is_jdata(ip);
954 error = gfs2_trans_begin(sdp,
955 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
959 error = gfs2_meta_inode_buffer(ip, &dibh);
963 if (gfs2_is_stuffed(ip)) {
964 ip->i_di.di_size = size;
965 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
966 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
967 gfs2_dinode_out(ip, dibh->b_data);
968 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
972 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
973 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
976 ip->i_di.di_size = size;
977 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
978 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
979 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
980 gfs2_dinode_out(ip, dibh->b_data);
991 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
993 unsigned int height = ip->i_di.di_height;
1001 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
1003 find_metapath(ip, lblock, &mp);
1006 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1011 struct strip_mine sm;
1012 sm.sm_first = !!size;
1013 sm.sm_height = height;
1015 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1020 gfs2_quota_unhold(ip);
1027 static int trunc_end(struct gfs2_inode *ip)
1029 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1030 struct buffer_head *dibh;
1033 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1037 down_write(&ip->i_rw_mutex);
1039 error = gfs2_meta_inode_buffer(ip, &dibh);
1043 if (!ip->i_di.di_size) {
1044 ip->i_di.di_height = 0;
1045 ip->i_di.di_goal_meta =
1046 ip->i_di.di_goal_data =
1048 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1050 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
1051 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1053 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1054 gfs2_dinode_out(ip, dibh->b_data);
1058 up_write(&ip->i_rw_mutex);
1059 gfs2_trans_end(sdp);
1064 * do_shrink - make a file smaller
1066 * @size: the size to make the file
1067 * @truncator: function to truncate the last partial block
1069 * Called with an exclusive lock on @ip.
1074 static int do_shrink(struct gfs2_inode *ip, u64 size)
1078 error = trunc_start(ip, size);
1084 error = trunc_dealloc(ip, size);
1086 error = trunc_end(ip);
1092 * gfs2_truncatei - make a file a given size
1094 * @size: the size to make the file
1095 * @truncator: function to truncate the last partial block
1097 * The file size can grow, shrink, or stay the same size.
1102 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1106 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1109 if (size > ip->i_di.di_size)
1110 error = do_grow(ip, size);
1112 error = do_shrink(ip, size);
1117 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1120 error = trunc_dealloc(ip, ip->i_di.di_size);
1122 error = trunc_end(ip);
1126 int gfs2_file_dealloc(struct gfs2_inode *ip)
1128 return trunc_dealloc(ip, 0);
1132 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1134 * @len: the number of bytes to be written to the file
1135 * @data_blocks: returns the number of data blocks required
1136 * @ind_blocks: returns the number of indirect blocks required
1140 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1141 unsigned int *data_blocks, unsigned int *ind_blocks)
1143 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1146 if (gfs2_is_dir(ip)) {
1147 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1148 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1150 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1151 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1154 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1155 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1161 * gfs2_write_alloc_required - figure out if a write will require an allocation
1162 * @ip: the file being written to
1163 * @offset: the offset to write to
1164 * @len: the number of bytes being written
1165 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1170 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1171 unsigned int len, int *alloc_required)
1173 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1174 u64 lblock, lblock_stop, dblock;
1179 *alloc_required = 0;
1184 if (gfs2_is_stuffed(ip)) {
1186 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1187 *alloc_required = 1;
1191 if (gfs2_is_dir(ip)) {
1192 unsigned int bsize = sdp->sd_jbsize;
1194 do_div(lblock, bsize);
1195 lblock_stop = offset + len + bsize - 1;
1196 do_div(lblock_stop, bsize);
1198 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1199 lblock = offset >> shift;
1200 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1203 for (; lblock < lblock_stop; lblock += extlen) {
1204 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
1209 *alloc_required = 1;