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);
311 for (i = ip->i_height; i--;)
312 mp->mp_list[i] = do_div(b, sdp->sd_inptrs);
317 * metapointer - Return pointer to start of metadata in a buffer
319 * @height: The metadata height (0 = dinode)
322 * Return a pointer to the block number of the next height of the metadata
323 * tree given a buffer containing the pointer to the current height of the
327 static inline __be64 *metapointer(struct buffer_head *bh, int *boundary,
328 unsigned int height, const struct metapath *mp)
330 unsigned int head_size = (height > 0) ?
331 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
334 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
335 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
341 * lookup_block - Get the next metadata block in metadata tree
342 * @ip: The GFS2 inode
343 * @bh: Buffer containing the pointers to metadata blocks
344 * @height: The height of the tree (0 = dinode)
346 * @create: Non-zero if we may create a new meatdata block
347 * @new: Used to indicate if we did create a new metadata block
348 * @block: the returned disk block number
350 * Given a metatree, complete to a particular height, checks to see if the next
351 * height of the tree exists. If not the next height of the tree is created.
352 * The block number of the next height of the metadata tree is returned.
356 static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
357 unsigned int height, struct metapath *mp, int create,
358 int *new, u64 *block)
361 __be64 *ptr = metapointer(bh, &boundary, height, mp);
364 *block = be64_to_cpu(*ptr);
373 if (height == ip->i_height - 1 && !gfs2_is_dir(ip))
374 *block = gfs2_alloc_data(ip);
376 *block = gfs2_alloc_meta(ip);
378 gfs2_trans_add_bh(ip->i_gl, bh, 1);
380 *ptr = cpu_to_be64(*block);
381 ip->i_di.di_blocks++;
382 gfs2_set_inode_blocks(&ip->i_inode);
388 static inline void bmap_lock(struct inode *inode, int create)
390 struct gfs2_inode *ip = GFS2_I(inode);
392 down_write(&ip->i_rw_mutex);
394 down_read(&ip->i_rw_mutex);
397 static inline void bmap_unlock(struct inode *inode, int create)
399 struct gfs2_inode *ip = GFS2_I(inode);
401 up_write(&ip->i_rw_mutex);
403 up_read(&ip->i_rw_mutex);
407 * gfs2_block_map - Map a block from an inode to a disk block
409 * @lblock: The logical block number
410 * @bh_map: The bh to be mapped
412 * Find the block number on the current device which corresponds to an
413 * inode's block. If the block had to be created, "new" will be set.
418 int gfs2_block_map(struct inode *inode, sector_t lblock,
419 struct buffer_head *bh_map, int create)
421 struct gfs2_inode *ip = GFS2_I(inode);
422 struct gfs2_sbd *sdp = GFS2_SB(inode);
423 struct buffer_head *bh;
424 unsigned int bsize = sdp->sd_sb.sb_bsize;
425 unsigned int end_of_metadata;
431 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
434 struct buffer_head *dibh = NULL;
435 const u64 *arr = sdp->sd_heightsize;
438 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
441 bmap_lock(inode, create);
442 clear_buffer_mapped(bh_map);
443 clear_buffer_new(bh_map);
444 clear_buffer_boundary(bh_map);
445 if (gfs2_is_dir(ip)) {
446 bsize = sdp->sd_jbsize;
447 arr = sdp->sd_jheightsize;
449 size = (lblock + 1) * bsize;
451 if (size > arr[ip->i_height]) {
452 u8 height = ip->i_height;
455 while (size > arr[height])
457 error = build_height(inode, height);
462 find_metapath(ip, lblock, &mp);
463 end_of_metadata = ip->i_height - 1;
464 error = gfs2_meta_inode_buffer(ip, &bh);
470 for (x = 0; x < end_of_metadata; x++) {
471 lookup_block(ip, bh, x, &mp, create, &new, &dblock);
476 error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
481 boundary = lookup_block(ip, bh, end_of_metadata, &mp, create, &new, &dblock);
483 map_bh(bh_map, inode->i_sb, dblock);
485 set_buffer_boundary(bh_map);
487 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
488 gfs2_dinode_out(ip, dibh->b_data);
489 set_buffer_new(bh_map);
492 while(--maxlen && !buffer_boundary(bh_map)) {
495 mp.mp_list[end_of_metadata]++;
496 boundary = lookup_block(ip, bh, end_of_metadata, &mp, 0, &new, &eblock);
497 if (eblock != ++dblock)
499 bh_map->b_size += (1 << inode->i_blkbits);
501 set_buffer_boundary(bh_map);
511 bmap_unlock(inode, create);
516 * Deprecated: do not use in new code
518 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
520 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
528 bh.b_size = 1 << (inode->i_blkbits + 5);
529 ret = gfs2_block_map(inode, lblock, &bh, create);
530 *extlen = bh.b_size >> inode->i_blkbits;
531 *dblock = bh.b_blocknr;
540 * recursive_scan - recursively scan through the end of a file
542 * @dibh: the dinode buffer
543 * @mp: the path through the metadata to the point to start
544 * @height: the height the recursion is at
545 * @block: the indirect block to look at
546 * @first: 1 if this is the first block
547 * @bc: the call to make for each piece of metadata
548 * @data: data opaque to this function to pass to @bc
550 * When this is first called @height and @block should be zero and
551 * @first should be 1.
556 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
557 struct metapath *mp, unsigned int height,
558 u64 block, int first, block_call_t bc,
561 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
562 struct buffer_head *bh = NULL;
563 __be64 *top, *bottom;
566 int mh_size = sizeof(struct gfs2_meta_header);
569 error = gfs2_meta_inode_buffer(ip, &bh);
574 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
575 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
577 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
581 top = (__be64 *)(bh->b_data + mh_size) +
582 (first ? mp->mp_list[height] : 0);
584 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
587 error = bc(ip, dibh, bh, top, bottom, height, data);
591 if (height < ip->i_height - 1)
592 for (; top < bottom; top++, first = 0) {
596 bn = be64_to_cpu(*top);
598 error = recursive_scan(ip, dibh, mp, height + 1, bn,
610 * do_strip - Look for a layer a particular layer of the file and strip it off
612 * @dibh: the dinode buffer
613 * @bh: A buffer of pointers
614 * @top: The first pointer in the buffer
615 * @bottom: One more than the last pointer
616 * @height: the height this buffer is at
617 * @data: a pointer to a struct strip_mine
622 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
623 struct buffer_head *bh, __be64 *top, __be64 *bottom,
624 unsigned int height, void *data)
626 struct strip_mine *sm = data;
627 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
628 struct gfs2_rgrp_list rlist;
632 unsigned int rg_blocks = 0;
634 unsigned int revokes = 0;
641 if (height != sm->sm_height)
649 metadata = (height != ip->i_height - 1);
651 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
653 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
657 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
661 for (p = top; p < bottom; p++) {
665 bn = be64_to_cpu(*p);
667 if (bstart + blen == bn)
671 gfs2_rlist_add(sdp, &rlist, bstart);
679 gfs2_rlist_add(sdp, &rlist, bstart);
681 goto out; /* Nothing to do */
683 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
685 for (x = 0; x < rlist.rl_rgrps; x++) {
686 struct gfs2_rgrpd *rgd;
687 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
688 rg_blocks += rgd->rd_length;
691 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
695 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
696 RES_INDIRECT + RES_STATFS + RES_QUOTA,
701 down_write(&ip->i_rw_mutex);
703 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
704 gfs2_trans_add_bh(ip->i_gl, bh, 1);
709 for (p = top; p < bottom; p++) {
713 bn = be64_to_cpu(*p);
715 if (bstart + blen == bn)
720 gfs2_free_meta(ip, bstart, blen);
722 gfs2_free_data(ip, bstart, blen);
730 if (!ip->i_di.di_blocks)
731 gfs2_consist_inode(ip);
732 ip->i_di.di_blocks--;
733 gfs2_set_inode_blocks(&ip->i_inode);
737 gfs2_free_meta(ip, bstart, blen);
739 gfs2_free_data(ip, bstart, blen);
742 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
744 gfs2_dinode_out(ip, dibh->b_data);
746 up_write(&ip->i_rw_mutex);
751 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
753 gfs2_rlist_free(&rlist);
755 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
760 * do_grow - Make a file look bigger than it is
762 * @size: the size to set the file to
764 * Called with an exclusive lock on @ip.
769 static int do_grow(struct gfs2_inode *ip, u64 size)
771 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
772 struct gfs2_alloc *al;
773 struct buffer_head *dibh;
776 al = gfs2_alloc_get(ip);
778 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
782 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
786 al->al_requested = sdp->sd_max_height + RES_DATA;
788 error = gfs2_inplace_reserve(ip);
792 error = gfs2_trans_begin(sdp,
793 sdp->sd_max_height + al->al_rgd->rd_length +
794 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
798 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
799 const u64 *arr = sdp->sd_heightsize;
800 if (gfs2_is_stuffed(ip)) {
801 error = gfs2_unstuff_dinode(ip, NULL);
806 down_write(&ip->i_rw_mutex);
807 if (size > arr[ip->i_height]) {
808 u8 height = ip->i_height;
809 while(size > arr[height])
811 error = build_height(&ip->i_inode, height);
813 up_write(&ip->i_rw_mutex);
818 ip->i_di.di_size = size;
819 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
821 error = gfs2_meta_inode_buffer(ip, &dibh);
825 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
826 gfs2_dinode_out(ip, dibh->b_data);
832 gfs2_inplace_release(ip);
834 gfs2_quota_unlock(ip);
842 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
844 * This is partly borrowed from ext3.
846 static int gfs2_block_truncate_page(struct address_space *mapping)
848 struct inode *inode = mapping->host;
849 struct gfs2_inode *ip = GFS2_I(inode);
850 loff_t from = inode->i_size;
851 unsigned long index = from >> PAGE_CACHE_SHIFT;
852 unsigned offset = from & (PAGE_CACHE_SIZE-1);
853 unsigned blocksize, iblock, length, pos;
854 struct buffer_head *bh;
858 page = grab_cache_page(mapping, index);
862 blocksize = inode->i_sb->s_blocksize;
863 length = blocksize - (offset & (blocksize - 1));
864 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
866 if (!page_has_buffers(page))
867 create_empty_buffers(page, blocksize, 0);
869 /* Find the buffer that contains "offset" */
870 bh = page_buffers(page);
872 while (offset >= pos) {
873 bh = bh->b_this_page;
880 if (!buffer_mapped(bh)) {
881 gfs2_block_map(inode, iblock, bh, 0);
882 /* unmapped? It's a hole - nothing to do */
883 if (!buffer_mapped(bh))
887 /* Ok, it's mapped. Make sure it's up-to-date */
888 if (PageUptodate(page))
889 set_buffer_uptodate(bh);
891 if (!buffer_uptodate(bh)) {
893 ll_rw_block(READ, 1, &bh);
895 /* Uhhuh. Read error. Complain and punt. */
896 if (!buffer_uptodate(bh))
901 if (!gfs2_is_writeback(ip))
902 gfs2_trans_add_bh(ip->i_gl, bh, 0);
904 zero_user(page, offset, length);
908 page_cache_release(page);
912 static int trunc_start(struct gfs2_inode *ip, u64 size)
914 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
915 struct buffer_head *dibh;
916 int journaled = gfs2_is_jdata(ip);
919 error = gfs2_trans_begin(sdp,
920 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
924 error = gfs2_meta_inode_buffer(ip, &dibh);
928 if (gfs2_is_stuffed(ip)) {
929 ip->i_di.di_size = size;
930 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
931 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
932 gfs2_dinode_out(ip, dibh->b_data);
933 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
937 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
938 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
941 ip->i_di.di_size = size;
942 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
943 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
944 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
945 gfs2_dinode_out(ip, dibh->b_data);
956 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
958 unsigned int height = ip->i_height;
966 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
968 find_metapath(ip, lblock, &mp);
971 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
976 struct strip_mine sm;
977 sm.sm_first = !!size;
978 sm.sm_height = height;
980 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
985 gfs2_quota_unhold(ip);
992 static int trunc_end(struct gfs2_inode *ip)
994 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
995 struct buffer_head *dibh;
998 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1002 down_write(&ip->i_rw_mutex);
1004 error = gfs2_meta_inode_buffer(ip, &dibh);
1008 if (!ip->i_di.di_size) {
1010 ip->i_di.di_goal_meta =
1011 ip->i_di.di_goal_data =
1013 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1015 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1016 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1018 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1019 gfs2_dinode_out(ip, dibh->b_data);
1023 up_write(&ip->i_rw_mutex);
1024 gfs2_trans_end(sdp);
1029 * do_shrink - make a file smaller
1031 * @size: the size to make the file
1032 * @truncator: function to truncate the last partial block
1034 * Called with an exclusive lock on @ip.
1039 static int do_shrink(struct gfs2_inode *ip, u64 size)
1043 error = trunc_start(ip, size);
1049 error = trunc_dealloc(ip, size);
1051 error = trunc_end(ip);
1056 static int do_touch(struct gfs2_inode *ip, u64 size)
1058 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1059 struct buffer_head *dibh;
1062 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1066 down_write(&ip->i_rw_mutex);
1068 error = gfs2_meta_inode_buffer(ip, &dibh);
1072 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1073 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1074 gfs2_dinode_out(ip, dibh->b_data);
1078 up_write(&ip->i_rw_mutex);
1079 gfs2_trans_end(sdp);
1084 * gfs2_truncatei - make a file a given size
1086 * @size: the size to make the file
1087 * @truncator: function to truncate the last partial block
1089 * The file size can grow, shrink, or stay the same size.
1094 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1098 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1101 if (size > ip->i_di.di_size)
1102 error = do_grow(ip, size);
1103 else if (size < ip->i_di.di_size)
1104 error = do_shrink(ip, size);
1106 /* update time stamps */
1107 error = do_touch(ip, size);
1112 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1115 error = trunc_dealloc(ip, ip->i_di.di_size);
1117 error = trunc_end(ip);
1121 int gfs2_file_dealloc(struct gfs2_inode *ip)
1123 return trunc_dealloc(ip, 0);
1127 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1129 * @len: the number of bytes to be written to the file
1130 * @data_blocks: returns the number of data blocks required
1131 * @ind_blocks: returns the number of indirect blocks required
1135 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1136 unsigned int *data_blocks, unsigned int *ind_blocks)
1138 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1141 if (gfs2_is_dir(ip)) {
1142 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1143 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1145 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1146 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1149 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1150 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1156 * gfs2_write_alloc_required - figure out if a write will require an allocation
1157 * @ip: the file being written to
1158 * @offset: the offset to write to
1159 * @len: the number of bytes being written
1160 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1165 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1166 unsigned int len, int *alloc_required)
1168 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1169 struct buffer_head bh;
1171 u64 lblock, lblock_stop, size;
1173 *alloc_required = 0;
1178 if (gfs2_is_stuffed(ip)) {
1180 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1181 *alloc_required = 1;
1185 *alloc_required = 1;
1186 shift = sdp->sd_sb.sb_bsize_shift;
1187 if (gfs2_is_dir(ip)) {
1188 unsigned int bsize = sdp->sd_jbsize;
1190 do_div(lblock, bsize);
1191 lblock_stop = offset + len + bsize - 1;
1192 do_div(lblock_stop, bsize);
1194 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1195 lblock = offset >> shift;
1196 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1197 if (lblock_stop > end_of_file)
1201 size = (lblock_stop - lblock) << shift;
1205 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1206 if (!buffer_mapped(&bh))
1209 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1212 *alloc_required = 0;