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/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/crc32.h>
19 #include "lm_interface.h"
30 #include "ops_address.h"
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
37 __u16 mp_list[GFS2_MAX_META_HEIGHT];
40 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
41 struct buffer_head *bh, u64 *top,
42 u64 *bottom, unsigned int height,
47 unsigned int sm_height;
51 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
53 * @dibh: the dinode buffer
54 * @block: the block number that was allocated
55 * @private: any locked page held by the caller process
60 static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
61 u64 block, struct page *page)
63 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
64 struct inode *inode = &ip->i_inode;
65 struct buffer_head *bh;
68 if (!page || page->index) {
69 page = grab_cache_page(inode->i_mapping, 0);
75 if (!PageUptodate(page)) {
76 void *kaddr = kmap(page);
78 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
80 memset(kaddr + ip->i_di.di_size, 0,
81 PAGE_CACHE_SIZE - ip->i_di.di_size);
84 SetPageUptodate(page);
87 if (!page_has_buffers(page))
88 create_empty_buffers(page, 1 << inode->i_blkbits,
91 bh = page_buffers(page);
93 if (!buffer_mapped(bh))
94 map_bh(bh, inode->i_sb, block);
96 set_buffer_uptodate(bh);
97 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
98 gfs2_trans_add_bh(ip->i_gl, bh, 0);
99 mark_buffer_dirty(bh);
103 page_cache_release(page);
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
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.
121 int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
123 struct buffer_head *bh, *dibh;
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,
145 sizeof(struct gfs2_meta_header),
146 dibh, sizeof(struct gfs2_dinode));
149 block = gfs2_alloc_data(ip);
151 error = gfs2_unstuffer_page(ip, dibh, block, page);
157 /* Set up the pointer to the new block */
159 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
161 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
163 if (ip->i_di.di_size) {
164 *(u64 *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
166 ip->i_di.di_blocks++;
169 ip->i_di.di_height = 1;
171 gfs2_dinode_out(&ip->i_di, dibh->b_data);
176 up_write(&ip->i_rw_mutex);
181 * calc_tree_height - Calculate the height of a metadata tree
182 * @ip: The GFS2 inode
183 * @size: The proposed size of the file
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
190 * Returns: the height the tree should be
193 static unsigned int calc_tree_height(struct gfs2_inode *ip, u64 size)
195 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
197 unsigned int max, height;
199 if (ip->i_di.di_size > size)
200 size = ip->i_di.di_size;
202 if (gfs2_is_dir(ip)) {
203 arr = sdp->sd_jheightsize;
204 max = sdp->sd_max_jheight;
206 arr = sdp->sd_heightsize;
207 max = sdp->sd_max_height;
210 for (height = 0; height < max; height++)
211 if (arr[height] >= size)
218 * build_height - Build a metadata tree of the requested height
219 * @ip: The GFS2 inode
220 * @height: The height to build to
226 static int build_height(struct inode *inode, unsigned height)
228 struct gfs2_inode *ip = GFS2_I(inode);
229 unsigned new_height = height - ip->i_di.di_height;
230 struct buffer_head *dibh;
231 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
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 = (u64 *)(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 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
271 bp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
272 *bp = cpu_to_be64(bn);
273 ip->i_di.di_height += new_height;
274 ip->i_di.di_blocks += new_height;
275 gfs2_dinode_out(&ip->i_di, dibh->b_data);
281 * find_metapath - Find path through the metadata tree
282 * @ip: The inode pointer
283 * @mp: The metapath to return the result in
284 * @block: The disk block to look up
286 * This routine returns a struct metapath structure that defines a path
287 * through the metadata of inode "ip" to get to block "block".
290 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
291 * filesystem with a blocksize of 4096.
293 * find_metapath() would return a struct metapath structure set to:
294 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
295 * and mp_list[2] = 165.
297 * That means that in order to get to the block containing the byte at
298 * offset 101342453, we would load the indirect block pointed to by pointer
299 * 0 in the dinode. We would then load the indirect block pointed to by
300 * pointer 48 in that indirect block. We would then load the data block
301 * pointed to by pointer 165 in that indirect block.
303 * ----------------------------------------
308 * ----------------------------------------
312 * ----------------------------------------
316 * |0 5 6 7 8 9 0 1 2|
317 * ----------------------------------------
321 * ----------------------------------------
326 * ----------------------------------------
330 * ----------------------------------------
331 * | Data block containing offset |
335 * ----------------------------------------
339 static void find_metapath(struct gfs2_inode *ip, u64 block,
342 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
346 for (i = ip->i_di.di_height; i--;)
347 mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
352 * metapointer - Return pointer to start of metadata in a buffer
354 * @height: The metadata height (0 = dinode)
357 * Return a pointer to the block number of the next height of the metadata
358 * tree given a buffer containing the pointer to the current height of the
362 static inline u64 *metapointer(struct buffer_head *bh, int *boundary,
363 unsigned int height, const struct metapath *mp)
365 unsigned int head_size = (height > 0) ?
366 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
369 ptr = ((u64 *)(bh->b_data + head_size)) + mp->mp_list[height];
370 if (ptr + 1 == (u64 *)(bh->b_data + bh->b_size))
376 * lookup_block - Get the next metadata block in metadata tree
377 * @ip: The GFS2 inode
378 * @bh: Buffer containing the pointers to metadata blocks
379 * @height: The height of the tree (0 = dinode)
381 * @create: Non-zero if we may create a new meatdata block
382 * @new: Used to indicate if we did create a new metadata block
383 * @block: the returned disk block number
385 * Given a metatree, complete to a particular height, checks to see if the next
386 * height of the tree exists. If not the next height of the tree is created.
387 * The block number of the next height of the metadata tree is returned.
391 static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
392 unsigned int height, struct metapath *mp, int create,
393 int *new, u64 *block)
396 u64 *ptr = metapointer(bh, &boundary, height, mp);
399 *block = be64_to_cpu(*ptr);
408 if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
409 *block = gfs2_alloc_data(ip);
411 *block = gfs2_alloc_meta(ip);
413 gfs2_trans_add_bh(ip->i_gl, bh, 1);
415 *ptr = cpu_to_be64(*block);
416 ip->i_di.di_blocks++;
423 * gfs2_block_pointers - Map a block from an inode to a disk block
425 * @lblock: The logical block number
426 * @new: Value/Result argument (1 = may create/did create new blocks)
427 * @boundary: gets set if we've hit a block boundary
428 * @mp: metapath to use
430 * Find the block number on the current device which corresponds to an
431 * inode's block. If the block had to be created, "new" will be set.
436 static struct buffer_head *gfs2_block_pointers(struct inode *inode, u64 lblock,
437 int *new, u64 *dblock,
441 struct gfs2_inode *ip = GFS2_I(inode);
442 struct gfs2_sbd *sdp = GFS2_SB(inode);
443 struct buffer_head *bh;
447 unsigned int end_of_metadata;
454 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
457 bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
459 height = calc_tree_height(ip, (lblock + 1) * bsize);
460 if (ip->i_di.di_height < height) {
464 error = build_height(inode, height);
469 find_metapath(ip, lblock, mp);
470 end_of_metadata = ip->i_di.di_height - 1;
472 error = gfs2_meta_inode_buffer(ip, &bh);
476 for (x = 0; x < end_of_metadata; x++) {
477 lookup_block(ip, bh, x, mp, create, new, dblock);
482 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
487 *boundary = lookup_block(ip, bh, end_of_metadata, mp, create, new, dblock);
489 struct buffer_head *dibh;
490 error = gfs2_meta_inode_buffer(ip, &dibh);
492 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
493 gfs2_dinode_out(&ip->i_di, dibh->b_data);
499 return ERR_PTR(error);
503 static inline void bmap_lock(struct inode *inode, int create)
505 struct gfs2_inode *ip = GFS2_I(inode);
507 down_write(&ip->i_rw_mutex);
509 down_read(&ip->i_rw_mutex);
512 static inline void bmap_unlock(struct inode *inode, int create)
514 struct gfs2_inode *ip = GFS2_I(inode);
516 up_write(&ip->i_rw_mutex);
518 up_read(&ip->i_rw_mutex);
521 int gfs2_block_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, int *boundary)
524 struct buffer_head *bh;
527 bmap_lock(inode, create);
528 bh = gfs2_block_pointers(inode, lblock, new, dblock, boundary, &mp);
529 bmap_unlock(inode, create);
538 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
540 struct gfs2_inode *ip = GFS2_I(inode);
541 struct gfs2_sbd *sdp = GFS2_SB(inode);
543 struct buffer_head *bh;
551 bmap_lock(inode, create);
552 bh = gfs2_block_pointers(inode, lblock, new, dblock, &boundary, &mp);
555 if (bh != NULL && !IS_ERR(bh) && *dblock != 0 && *new == 0) {
559 unsigned end_of_metadata = ip->i_di.di_height - 1;
561 nptrs = (end_of_metadata) ? sdp->sd_inptrs : sdp->sd_diptrs;
562 while (++mp.mp_list[end_of_metadata] < nptrs) {
563 lookup_block(ip, bh, end_of_metadata, &mp, 0, &tmp_new, &tmp_dblock);
564 if (*dblock + *extlen != tmp_dblock)
569 bmap_unlock(inode, create);
579 * recursive_scan - recursively scan through the end of a file
581 * @dibh: the dinode buffer
582 * @mp: the path through the metadata to the point to start
583 * @height: the height the recursion is at
584 * @block: the indirect block to look at
585 * @first: 1 if this is the first block
586 * @bc: the call to make for each piece of metadata
587 * @data: data opaque to this function to pass to @bc
589 * When this is first called @height and @block should be zero and
590 * @first should be 1.
595 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
596 struct metapath *mp, unsigned int height,
597 u64 block, int first, block_call_t bc,
600 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
601 struct buffer_head *bh = NULL;
605 int mh_size = sizeof(struct gfs2_meta_header);
608 error = gfs2_meta_inode_buffer(ip, &bh);
613 top = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
614 bottom = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
616 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
620 top = (u64 *)(bh->b_data + mh_size) +
621 ((first) ? mp->mp_list[height] : 0);
623 bottom = (u64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
626 error = bc(ip, dibh, bh, top, bottom, height, data);
630 if (height < ip->i_di.di_height - 1)
631 for (; top < bottom; top++, first = 0) {
635 bn = be64_to_cpu(*top);
637 error = recursive_scan(ip, dibh, mp, height + 1, bn,
649 * do_strip - Look for a layer a particular layer of the file and strip it off
651 * @dibh: the dinode buffer
652 * @bh: A buffer of pointers
653 * @top: The first pointer in the buffer
654 * @bottom: One more than the last pointer
655 * @height: the height this buffer is at
656 * @data: a pointer to a struct strip_mine
661 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
662 struct buffer_head *bh, u64 *top, u64 *bottom,
663 unsigned int height, void *data)
665 struct strip_mine *sm = data;
666 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
667 struct gfs2_rgrp_list rlist;
671 unsigned int rg_blocks = 0;
673 unsigned int revokes = 0;
680 if (height != sm->sm_height)
688 metadata = (height != ip->i_di.di_height - 1);
690 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
692 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
696 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
700 for (p = top; p < bottom; p++) {
704 bn = be64_to_cpu(*p);
706 if (bstart + blen == bn)
710 gfs2_rlist_add(sdp, &rlist, bstart);
718 gfs2_rlist_add(sdp, &rlist, bstart);
720 goto out; /* Nothing to do */
722 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
724 for (x = 0; x < rlist.rl_rgrps; x++) {
725 struct gfs2_rgrpd *rgd;
726 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
727 rg_blocks += rgd->rd_ri.ri_length;
730 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
734 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
735 RES_INDIRECT + RES_STATFS + RES_QUOTA,
740 down_write(&ip->i_rw_mutex);
742 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
743 gfs2_trans_add_bh(ip->i_gl, bh, 1);
748 for (p = top; p < bottom; p++) {
752 bn = be64_to_cpu(*p);
754 if (bstart + blen == bn)
759 gfs2_free_meta(ip, bstart, blen);
761 gfs2_free_data(ip, bstart, blen);
769 if (!ip->i_di.di_blocks)
770 gfs2_consist_inode(ip);
771 ip->i_di.di_blocks--;
775 gfs2_free_meta(ip, bstart, blen);
777 gfs2_free_data(ip, bstart, blen);
780 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
782 gfs2_dinode_out(&ip->i_di, dibh->b_data);
784 up_write(&ip->i_rw_mutex);
789 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
791 gfs2_rlist_free(&rlist);
793 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
798 * do_grow - Make a file look bigger than it is
800 * @size: the size to set the file to
802 * Called with an exclusive lock on @ip.
807 static int do_grow(struct gfs2_inode *ip, u64 size)
809 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
810 struct gfs2_alloc *al;
811 struct buffer_head *dibh;
815 al = gfs2_alloc_get(ip);
817 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
821 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
825 al->al_requested = sdp->sd_max_height + RES_DATA;
827 error = gfs2_inplace_reserve(ip);
831 error = gfs2_trans_begin(sdp,
832 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
833 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
837 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
838 if (gfs2_is_stuffed(ip)) {
839 error = gfs2_unstuff_dinode(ip, NULL);
844 h = calc_tree_height(ip, size);
845 if (ip->i_di.di_height < h) {
846 down_write(&ip->i_rw_mutex);
847 error = build_height(&ip->i_inode, h);
848 up_write(&ip->i_rw_mutex);
854 ip->i_di.di_size = size;
855 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
857 error = gfs2_meta_inode_buffer(ip, &dibh);
861 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
862 gfs2_dinode_out(&ip->i_di, dibh->b_data);
868 gfs2_inplace_release(ip);
870 gfs2_quota_unlock(ip);
878 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
880 * This is partly borrowed from ext3.
882 static int gfs2_block_truncate_page(struct address_space *mapping)
884 struct inode *inode = mapping->host;
885 struct gfs2_inode *ip = GFS2_I(inode);
886 struct gfs2_sbd *sdp = GFS2_SB(inode);
887 loff_t from = inode->i_size;
888 unsigned long index = from >> PAGE_CACHE_SHIFT;
889 unsigned offset = from & (PAGE_CACHE_SIZE-1);
890 unsigned blocksize, iblock, length, pos;
891 struct buffer_head *bh;
896 page = grab_cache_page(mapping, index);
900 blocksize = inode->i_sb->s_blocksize;
901 length = blocksize - (offset & (blocksize - 1));
902 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
904 if (!page_has_buffers(page))
905 create_empty_buffers(page, blocksize, 0);
907 /* Find the buffer that contains "offset" */
908 bh = page_buffers(page);
910 while (offset >= pos) {
911 bh = bh->b_this_page;
918 if (!buffer_mapped(bh)) {
919 gfs2_get_block(inode, iblock, bh, 0);
920 /* unmapped? It's a hole - nothing to do */
921 if (!buffer_mapped(bh))
925 /* Ok, it's mapped. Make sure it's up-to-date */
926 if (PageUptodate(page))
927 set_buffer_uptodate(bh);
929 if (!buffer_uptodate(bh)) {
931 ll_rw_block(READ, 1, &bh);
933 /* Uhhuh. Read error. Complain and punt. */
934 if (!buffer_uptodate(bh))
938 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
939 gfs2_trans_add_bh(ip->i_gl, bh, 0);
941 kaddr = kmap_atomic(page, KM_USER0);
942 memset(kaddr + offset, 0, length);
943 flush_dcache_page(page);
944 kunmap_atomic(kaddr, KM_USER0);
948 page_cache_release(page);
952 static int trunc_start(struct gfs2_inode *ip, u64 size)
954 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
955 struct buffer_head *dibh;
956 int journaled = gfs2_is_jdata(ip);
959 error = gfs2_trans_begin(sdp,
960 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
964 error = gfs2_meta_inode_buffer(ip, &dibh);
968 if (gfs2_is_stuffed(ip)) {
969 ip->i_di.di_size = size;
970 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
971 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
972 gfs2_dinode_out(&ip->i_di, dibh->b_data);
973 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
977 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
978 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
981 ip->i_di.di_size = size;
982 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
983 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
984 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
985 gfs2_dinode_out(&ip->i_di, dibh->b_data);
996 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
998 unsigned int height = ip->i_di.di_height;
1006 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
1008 find_metapath(ip, lblock, &mp);
1011 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1016 struct strip_mine sm;
1017 sm.sm_first = !!size;
1018 sm.sm_height = height;
1020 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1025 gfs2_quota_unhold(ip);
1032 static int trunc_end(struct gfs2_inode *ip)
1034 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1035 struct buffer_head *dibh;
1038 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1042 down_write(&ip->i_rw_mutex);
1044 error = gfs2_meta_inode_buffer(ip, &dibh);
1048 if (!ip->i_di.di_size) {
1049 ip->i_di.di_height = 0;
1050 ip->i_di.di_goal_meta =
1051 ip->i_di.di_goal_data =
1053 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1055 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1056 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1058 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1059 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1063 up_write(&ip->i_rw_mutex);
1064 gfs2_trans_end(sdp);
1069 * do_shrink - make a file smaller
1071 * @size: the size to make the file
1072 * @truncator: function to truncate the last partial block
1074 * Called with an exclusive lock on @ip.
1079 static int do_shrink(struct gfs2_inode *ip, u64 size)
1083 error = trunc_start(ip, size);
1089 error = trunc_dealloc(ip, size);
1091 error = trunc_end(ip);
1097 * gfs2_truncatei - make a file a given size
1099 * @size: the size to make the file
1100 * @truncator: function to truncate the last partial block
1102 * The file size can grow, shrink, or stay the same size.
1107 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1111 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_di.di_mode)))
1114 if (size > ip->i_di.di_size)
1115 error = do_grow(ip, size);
1117 error = do_shrink(ip, size);
1122 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1125 error = trunc_dealloc(ip, ip->i_di.di_size);
1127 error = trunc_end(ip);
1131 int gfs2_file_dealloc(struct gfs2_inode *ip)
1133 return trunc_dealloc(ip, 0);
1137 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1139 * @len: the number of bytes to be written to the file
1140 * @data_blocks: returns the number of data blocks required
1141 * @ind_blocks: returns the number of indirect blocks required
1145 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1146 unsigned int *data_blocks, unsigned int *ind_blocks)
1148 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1151 if (gfs2_is_dir(ip)) {
1152 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1153 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1155 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1156 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1159 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1160 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1166 * gfs2_write_alloc_required - figure out if a write will require an allocation
1167 * @ip: the file being written to
1168 * @offset: the offset to write to
1169 * @len: the number of bytes being written
1170 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1175 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1176 unsigned int len, int *alloc_required)
1178 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1179 u64 lblock, lblock_stop, dblock;
1184 *alloc_required = 0;
1189 if (gfs2_is_stuffed(ip)) {
1191 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1192 *alloc_required = 1;
1196 if (gfs2_is_dir(ip)) {
1197 unsigned int bsize = sdp->sd_jbsize;
1199 do_div(lblock, bsize);
1200 lblock_stop = offset + len + bsize - 1;
1201 do_div(lblock_stop, bsize);
1203 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1204 lblock = offset >> shift;
1205 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1208 for (; lblock < lblock_stop; lblock += extlen) {
1209 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
1214 *alloc_required = 1;