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 inode *inode = &ip->i_inode;
63 struct buffer_head *bh;
66 if (!page || page->index) {
67 page = grab_cache_page(inode->i_mapping, 0);
73 if (!PageUptodate(page)) {
74 void *kaddr = kmap(page);
76 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
78 memset(kaddr + ip->i_di.di_size, 0,
79 PAGE_CACHE_SIZE - ip->i_di.di_size);
82 SetPageUptodate(page);
85 if (!page_has_buffers(page))
86 create_empty_buffers(page, 1 << inode->i_blkbits,
89 bh = page_buffers(page);
91 if (!buffer_mapped(bh))
92 map_bh(bh, inode->i_sb, block);
94 set_buffer_uptodate(bh);
95 if (!gfs2_is_jdata(ip))
96 mark_buffer_dirty(bh);
97 if (!gfs2_is_writeback(ip))
98 gfs2_trans_add_bh(ip->i_gl, bh, 0);
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);
170 di->di_height = cpu_to_be16(1);
175 up_write(&ip->i_rw_mutex);
180 * build_height - Build a metadata tree of the requested height
181 * @ip: The GFS2 inode
182 * @height: The height to build to
188 static int build_height(struct inode *inode, unsigned height)
190 struct gfs2_inode *ip = GFS2_I(inode);
191 unsigned new_height = height - ip->i_height;
192 struct buffer_head *dibh;
193 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
194 struct gfs2_dinode *di;
200 if (height <= ip->i_height)
203 error = gfs2_meta_inode_buffer(ip, &dibh);
207 for(n = 0; n < new_height; n++) {
208 bn = gfs2_alloc_meta(ip);
209 blocks[n] = gfs2_meta_new(ip->i_gl, bn);
210 gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
214 bn = blocks[0]->b_blocknr;
215 if (new_height > 1) {
216 for(; n < new_height-1; n++) {
217 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
219 gfs2_buffer_clear_tail(blocks[n],
220 sizeof(struct gfs2_meta_header));
221 bp = (__be64 *)(blocks[n]->b_data +
222 sizeof(struct gfs2_meta_header));
223 *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
228 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
229 gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
230 dibh, sizeof(struct gfs2_dinode));
232 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
233 di = (struct gfs2_dinode *)dibh->b_data;
234 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
235 *(__be64 *)(di + 1) = cpu_to_be64(bn);
236 ip->i_height += new_height;
237 ip->i_di.di_blocks += new_height;
238 gfs2_set_inode_blocks(&ip->i_inode);
239 di->di_height = cpu_to_be16(ip->i_height);
240 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
246 * find_metapath - Find path through the metadata tree
247 * @ip: The inode pointer
248 * @mp: The metapath to return the result in
249 * @block: The disk block to look up
251 * This routine returns a struct metapath structure that defines a path
252 * through the metadata of inode "ip" to get to block "block".
255 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
256 * filesystem with a blocksize of 4096.
258 * find_metapath() would return a struct metapath structure set to:
259 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
260 * and mp_list[2] = 165.
262 * That means that in order to get to the block containing the byte at
263 * offset 101342453, we would load the indirect block pointed to by pointer
264 * 0 in the dinode. We would then load the indirect block pointed to by
265 * pointer 48 in that indirect block. We would then load the data block
266 * pointed to by pointer 165 in that indirect block.
268 * ----------------------------------------
273 * ----------------------------------------
277 * ----------------------------------------
281 * |0 5 6 7 8 9 0 1 2|
282 * ----------------------------------------
286 * ----------------------------------------
291 * ----------------------------------------
295 * ----------------------------------------
296 * | Data block containing offset |
300 * ----------------------------------------
304 static void find_metapath(struct gfs2_inode *ip, u64 block,
307 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
310 for (i = ip->i_height; i--;)
311 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
316 * metapointer - Return pointer to start of metadata in a buffer
318 * @height: The metadata height (0 = dinode)
321 * Return a pointer to the block number of the next height of the metadata
322 * tree given a buffer containing the pointer to the current height of the
326 static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
327 unsigned int height, const struct metapath *mp)
329 unsigned int head_size = (height > 0) ?
330 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
333 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
334 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
340 * lookup_block - Get the next metadata block in metadata tree
341 * @ip: The GFS2 inode
342 * @bh: Buffer containing the pointers to metadata blocks
343 * @height: The height of the tree (0 = dinode)
345 * @create: Non-zero if we may create a new meatdata block
346 * @new: Used to indicate if we did create a new metadata block
347 * @block: the returned disk block number
349 * Given a metatree, complete to a particular height, checks to see if the next
350 * height of the tree exists. If not the next height of the tree is created.
351 * The block number of the next height of the metadata tree is returned.
355 static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
356 unsigned int height, struct metapath *mp, int create,
357 int *new, u64 *block)
360 __be64 *ptr = metapointer(bh, &boundary, height, mp);
363 *block = be64_to_cpu(*ptr);
372 if (height == ip->i_height - 1 && !gfs2_is_dir(ip))
373 *block = gfs2_alloc_data(ip);
375 *block = gfs2_alloc_meta(ip);
377 gfs2_trans_add_bh(ip->i_gl, bh, 1);
379 *ptr = cpu_to_be64(*block);
380 ip->i_di.di_blocks++;
381 gfs2_set_inode_blocks(&ip->i_inode);
387 static inline void bmap_lock(struct inode *inode, int create)
389 struct gfs2_inode *ip = GFS2_I(inode);
391 down_write(&ip->i_rw_mutex);
393 down_read(&ip->i_rw_mutex);
396 static inline void bmap_unlock(struct inode *inode, int create)
398 struct gfs2_inode *ip = GFS2_I(inode);
400 up_write(&ip->i_rw_mutex);
402 up_read(&ip->i_rw_mutex);
406 * gfs2_block_map - Map a block from an inode to a disk block
408 * @lblock: The logical block number
409 * @bh_map: The bh to be mapped
411 * Find the block number on the current device which corresponds to an
412 * inode's block. If the block had to be created, "new" will be set.
417 int gfs2_block_map(struct inode *inode, sector_t lblock,
418 struct buffer_head *bh_map, int create)
420 struct gfs2_inode *ip = GFS2_I(inode);
421 struct gfs2_sbd *sdp = GFS2_SB(inode);
422 struct buffer_head *bh;
423 unsigned int bsize = sdp->sd_sb.sb_bsize;
424 unsigned int end_of_metadata;
430 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
433 struct buffer_head *dibh = NULL;
434 const u64 *arr = sdp->sd_heightsize;
437 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
440 bmap_lock(inode, create);
441 clear_buffer_mapped(bh_map);
442 clear_buffer_new(bh_map);
443 clear_buffer_boundary(bh_map);
444 if (gfs2_is_dir(ip)) {
445 bsize = sdp->sd_jbsize;
446 arr = sdp->sd_jheightsize;
448 size = (lblock + 1) * bsize;
450 if (size > arr[ip->i_height]) {
451 u8 height = ip->i_height;
454 while (size > arr[height])
456 error = build_height(inode, height);
461 find_metapath(ip, lblock, &mp);
462 end_of_metadata = ip->i_height - 1;
463 error = gfs2_meta_inode_buffer(ip, &bh);
469 for (x = 0; x < end_of_metadata; x++) {
470 lookup_block(ip, bh, x, &mp, create, &new, &dblock);
475 error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
480 boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
482 map_bh(bh_map, inode->i_sb, dblock);
484 set_buffer_boundary(bh_map);
486 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
487 gfs2_dinode_out(ip, dibh->b_data);
488 set_buffer_new(bh_map);
491 while(--maxlen && !buffer_boundary(bh_map)) {
494 mp.mp_list[end_of_metadata]++;
495 boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
496 if (eblock != ++dblock)
498 bh_map->b_size += (1 << inode->i_blkbits);
500 set_buffer_boundary(bh_map);
510 bmap_unlock(inode, create);
515 * Deprecated: do not use in new code
517 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
519 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
527 bh.b_size = 1 << (inode->i_blkbits + 5);
528 ret = gfs2_block_map(inode, lblock, &bh, create);
529 *extlen = bh.b_size >> inode->i_blkbits;
530 *dblock = bh.b_blocknr;
539 * recursive_scan - recursively scan through the end of a file
541 * @dibh: the dinode buffer
542 * @mp: the path through the metadata to the point to start
543 * @height: the height the recursion is at
544 * @block: the indirect block to look at
545 * @first: 1 if this is the first block
546 * @bc: the call to make for each piece of metadata
547 * @data: data opaque to this function to pass to @bc
549 * When this is first called @height and @block should be zero and
550 * @first should be 1.
555 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
556 struct metapath *mp, unsigned int height,
557 u64 block, int first, block_call_t bc,
560 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
561 struct buffer_head *bh = NULL;
562 __be64 *top, *bottom;
565 int mh_size = sizeof(struct gfs2_meta_header);
568 error = gfs2_meta_inode_buffer(ip, &bh);
573 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
574 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
576 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
580 top = (__be64 *)(bh->b_data + mh_size) +
581 (first ? mp->mp_list[height] : 0);
583 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
586 error = bc(ip, dibh, bh, top, bottom, height, data);
590 if (height < ip->i_height - 1)
591 for (; top < bottom; top++, first = 0) {
595 bn = be64_to_cpu(*top);
597 error = recursive_scan(ip, dibh, mp, height + 1, bn,
609 * do_strip - Look for a layer a particular layer of the file and strip it off
611 * @dibh: the dinode buffer
612 * @bh: A buffer of pointers
613 * @top: The first pointer in the buffer
614 * @bottom: One more than the last pointer
615 * @height: the height this buffer is at
616 * @data: a pointer to a struct strip_mine
621 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
622 struct buffer_head *bh, __be64 *top, __be64 *bottom,
623 unsigned int height, void *data)
625 struct strip_mine *sm = data;
626 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
627 struct gfs2_rgrp_list rlist;
631 unsigned int rg_blocks = 0;
633 unsigned int revokes = 0;
640 if (height != sm->sm_height)
648 metadata = (height != ip->i_height - 1);
650 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
652 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
656 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
660 for (p = top; p < bottom; p++) {
664 bn = be64_to_cpu(*p);
666 if (bstart + blen == bn)
670 gfs2_rlist_add(sdp, &rlist, bstart);
678 gfs2_rlist_add(sdp, &rlist, bstart);
680 goto out; /* Nothing to do */
682 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
684 for (x = 0; x < rlist.rl_rgrps; x++) {
685 struct gfs2_rgrpd *rgd;
686 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
687 rg_blocks += rgd->rd_length;
690 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
694 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
695 RES_INDIRECT + RES_STATFS + RES_QUOTA,
700 down_write(&ip->i_rw_mutex);
702 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
703 gfs2_trans_add_bh(ip->i_gl, bh, 1);
708 for (p = top; p < bottom; p++) {
712 bn = be64_to_cpu(*p);
714 if (bstart + blen == bn)
719 gfs2_free_meta(ip, bstart, blen);
721 gfs2_free_data(ip, bstart, blen);
729 if (!ip->i_di.di_blocks)
730 gfs2_consist_inode(ip);
731 ip->i_di.di_blocks--;
732 gfs2_set_inode_blocks(&ip->i_inode);
736 gfs2_free_meta(ip, bstart, blen);
738 gfs2_free_data(ip, bstart, blen);
741 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
743 gfs2_dinode_out(ip, dibh->b_data);
745 up_write(&ip->i_rw_mutex);
750 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
752 gfs2_rlist_free(&rlist);
754 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
759 * do_grow - Make a file look bigger than it is
761 * @size: the size to set the file to
763 * Called with an exclusive lock on @ip.
768 static int do_grow(struct gfs2_inode *ip, u64 size)
770 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
771 struct gfs2_alloc *al;
772 struct buffer_head *dibh;
775 al = gfs2_alloc_get(ip);
777 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
781 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
785 al->al_requested = sdp->sd_max_height + RES_DATA;
787 error = gfs2_inplace_reserve(ip);
791 error = gfs2_trans_begin(sdp,
792 sdp->sd_max_height + al->al_rgd->rd_length +
793 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
797 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
798 const u64 *arr = sdp->sd_heightsize;
799 if (gfs2_is_stuffed(ip)) {
800 error = gfs2_unstuff_dinode(ip, NULL);
805 down_write(&ip->i_rw_mutex);
806 if (size > arr[ip->i_height]) {
807 u8 height = ip->i_height;
808 while(size > arr[height])
810 error = build_height(&ip->i_inode, height);
812 up_write(&ip->i_rw_mutex);
817 ip->i_di.di_size = size;
818 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
820 error = gfs2_meta_inode_buffer(ip, &dibh);
824 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
825 gfs2_dinode_out(ip, dibh->b_data);
831 gfs2_inplace_release(ip);
833 gfs2_quota_unlock(ip);
841 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
843 * This is partly borrowed from ext3.
845 static int gfs2_block_truncate_page(struct address_space *mapping)
847 struct inode *inode = mapping->host;
848 struct gfs2_inode *ip = GFS2_I(inode);
849 loff_t from = inode->i_size;
850 unsigned long index = from >> PAGE_CACHE_SHIFT;
851 unsigned offset = from & (PAGE_CACHE_SIZE-1);
852 unsigned blocksize, iblock, length, pos;
853 struct buffer_head *bh;
857 page = grab_cache_page(mapping, index);
861 blocksize = inode->i_sb->s_blocksize;
862 length = blocksize - (offset & (blocksize - 1));
863 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
865 if (!page_has_buffers(page))
866 create_empty_buffers(page, blocksize, 0);
868 /* Find the buffer that contains "offset" */
869 bh = page_buffers(page);
871 while (offset >= pos) {
872 bh = bh->b_this_page;
879 if (!buffer_mapped(bh)) {
880 gfs2_block_map(inode, iblock, bh, 0);
881 /* unmapped? It's a hole - nothing to do */
882 if (!buffer_mapped(bh))
886 /* Ok, it's mapped. Make sure it's up-to-date */
887 if (PageUptodate(page))
888 set_buffer_uptodate(bh);
890 if (!buffer_uptodate(bh)) {
892 ll_rw_block(READ, 1, &bh);
894 /* Uhhuh. Read error. Complain and punt. */
895 if (!buffer_uptodate(bh))
900 if (!gfs2_is_writeback(ip))
901 gfs2_trans_add_bh(ip->i_gl, bh, 0);
903 zero_user(page, offset, length);
907 page_cache_release(page);
911 static int trunc_start(struct gfs2_inode *ip, u64 size)
913 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
914 struct buffer_head *dibh;
915 int journaled = gfs2_is_jdata(ip);
918 error = gfs2_trans_begin(sdp,
919 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
923 error = gfs2_meta_inode_buffer(ip, &dibh);
927 if (gfs2_is_stuffed(ip)) {
928 ip->i_di.di_size = size;
929 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
930 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
931 gfs2_dinode_out(ip, dibh->b_data);
932 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
936 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
937 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
940 ip->i_di.di_size = size;
941 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
942 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
943 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
944 gfs2_dinode_out(ip, dibh->b_data);
955 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
957 unsigned int height = ip->i_height;
965 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
967 find_metapath(ip, lblock, &mp);
970 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
975 struct strip_mine sm;
976 sm.sm_first = !!size;
977 sm.sm_height = height;
979 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
984 gfs2_quota_unhold(ip);
991 static int trunc_end(struct gfs2_inode *ip)
993 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
994 struct buffer_head *dibh;
997 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1001 down_write(&ip->i_rw_mutex);
1003 error = gfs2_meta_inode_buffer(ip, &dibh);
1007 if (!ip->i_di.di_size) {
1009 ip->i_di.di_goal_meta =
1010 ip->i_di.di_goal_data =
1012 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1014 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1015 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1017 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1018 gfs2_dinode_out(ip, dibh->b_data);
1022 up_write(&ip->i_rw_mutex);
1023 gfs2_trans_end(sdp);
1028 * do_shrink - make a file smaller
1030 * @size: the size to make the file
1031 * @truncator: function to truncate the last partial block
1033 * Called with an exclusive lock on @ip.
1038 static int do_shrink(struct gfs2_inode *ip, u64 size)
1042 error = trunc_start(ip, size);
1048 error = trunc_dealloc(ip, size);
1050 error = trunc_end(ip);
1055 static int do_touch(struct gfs2_inode *ip, u64 size)
1057 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1058 struct buffer_head *dibh;
1061 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1065 down_write(&ip->i_rw_mutex);
1067 error = gfs2_meta_inode_buffer(ip, &dibh);
1071 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1072 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1073 gfs2_dinode_out(ip, dibh->b_data);
1077 up_write(&ip->i_rw_mutex);
1078 gfs2_trans_end(sdp);
1083 * gfs2_truncatei - make a file a given size
1085 * @size: the size to make the file
1086 * @truncator: function to truncate the last partial block
1088 * The file size can grow, shrink, or stay the same size.
1093 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1097 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1100 if (size > ip->i_di.di_size)
1101 error = do_grow(ip, size);
1102 else if (size < ip->i_di.di_size)
1103 error = do_shrink(ip, size);
1105 /* update time stamps */
1106 error = do_touch(ip, size);
1111 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1114 error = trunc_dealloc(ip, ip->i_di.di_size);
1116 error = trunc_end(ip);
1120 int gfs2_file_dealloc(struct gfs2_inode *ip)
1122 return trunc_dealloc(ip, 0);
1126 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1128 * @len: the number of bytes to be written to the file
1129 * @data_blocks: returns the number of data blocks required
1130 * @ind_blocks: returns the number of indirect blocks required
1134 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1135 unsigned int *data_blocks, unsigned int *ind_blocks)
1137 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1140 if (gfs2_is_dir(ip)) {
1141 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1142 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1144 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1145 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1148 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1149 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1155 * gfs2_write_alloc_required - figure out if a write will require an allocation
1156 * @ip: the file being written to
1157 * @offset: the offset to write to
1158 * @len: the number of bytes being written
1159 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1164 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1165 unsigned int len, int *alloc_required)
1167 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1168 struct buffer_head bh;
1170 u64 lblock, lblock_stop, size;
1172 *alloc_required = 0;
1177 if (gfs2_is_stuffed(ip)) {
1179 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1180 *alloc_required = 1;
1184 *alloc_required = 1;
1185 shift = sdp->sd_sb.sb_bsize_shift;
1186 if (gfs2_is_dir(ip)) {
1187 unsigned int bsize = sdp->sd_jbsize;
1189 do_div(lblock, bsize);
1190 lblock_stop = offset + len + bsize - 1;
1191 do_div(lblock_stop, bsize);
1193 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1194 lblock = offset >> shift;
1195 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1196 if (lblock_stop > end_of_file)
1200 size = (lblock_stop - lblock) << shift;
1204 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1205 if (!buffer_mapped(&bh))
1208 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1211 *alloc_required = 0;