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 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
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, __be64 *top,
42 __be64 *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 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 (!gfs2_is_jdata(ip))
97 mark_buffer_dirty(bh);
98 if (!gfs2_is_writeback(ip))
99 gfs2_trans_add_bh(ip->i_gl, bh, 0);
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;
124 struct gfs2_dinode *di;
126 int isdir = gfs2_is_dir(ip);
129 down_write(&ip->i_rw_mutex);
131 error = gfs2_meta_inode_buffer(ip, &dibh);
135 if (ip->i_di.di_size) {
136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */
140 block = gfs2_alloc_block(ip, &n);
142 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
143 error = gfs2_dir_get_new_buffer(ip, block, &bh);
146 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
147 dibh, sizeof(struct gfs2_dinode));
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, struct metapath *mp, 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 gfs2_dinode *di;
199 if (height <= ip->i_height)
202 error = gfs2_meta_inode_buffer(ip, &dibh);
208 bn = gfs2_alloc_block(ip, &n);
209 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, n);
211 mp->mp_bh[i] = gfs2_meta_new(ip->i_gl, bn++);
212 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i], 1);
215 } while(i < new_height);
218 bn = mp->mp_bh[0]->b_blocknr;
219 if (new_height > 1) {
220 for(; n < new_height-1; n++) {
221 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN,
223 gfs2_buffer_clear_tail(mp->mp_bh[n],
224 sizeof(struct gfs2_meta_header));
225 bp = (__be64 *)(mp->mp_bh[n]->b_data +
226 sizeof(struct gfs2_meta_header));
227 *bp = cpu_to_be64(mp->mp_bh[n+1]->b_blocknr);
228 brelse(mp->mp_bh[n]);
232 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
233 gfs2_buffer_copy_tail(mp->mp_bh[n], sizeof(struct gfs2_meta_header),
234 dibh, sizeof(struct gfs2_dinode));
235 brelse(mp->mp_bh[n]);
236 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
237 di = (struct gfs2_dinode *)dibh->b_data;
238 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
239 *(__be64 *)(di + 1) = cpu_to_be64(bn);
240 ip->i_height += new_height;
241 ip->i_di.di_blocks += new_height;
242 gfs2_set_inode_blocks(&ip->i_inode);
243 di->di_height = cpu_to_be16(ip->i_height);
244 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
250 * find_metapath - Find path through the metadata tree
251 * @ip: The inode pointer
252 * @mp: The metapath to return the result in
253 * @block: The disk block to look up
255 * This routine returns a struct metapath structure that defines a path
256 * through the metadata of inode "ip" to get to block "block".
259 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
260 * filesystem with a blocksize of 4096.
262 * find_metapath() would return a struct metapath structure set to:
263 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
264 * and mp_list[2] = 165.
266 * That means that in order to get to the block containing the byte at
267 * offset 101342453, we would load the indirect block pointed to by pointer
268 * 0 in the dinode. We would then load the indirect block pointed to by
269 * pointer 48 in that indirect block. We would then load the data block
270 * pointed to by pointer 165 in that indirect block.
272 * ----------------------------------------
277 * ----------------------------------------
281 * ----------------------------------------
285 * |0 5 6 7 8 9 0 1 2|
286 * ----------------------------------------
290 * ----------------------------------------
295 * ----------------------------------------
299 * ----------------------------------------
300 * | Data block containing offset |
304 * ----------------------------------------
308 static void find_metapath(struct gfs2_inode *ip, u64 block,
311 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
314 for (i = ip->i_height; i--;)
315 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
320 * metapointer - Return pointer to start of metadata in a buffer
321 * @height: The metadata height (0 = dinode)
324 * Return a pointer to the block number of the next height of the metadata
325 * tree given a buffer containing the pointer to the current height of the
329 static inline __be64 *metapointer(int *boundary, unsigned int height,
330 const struct metapath *mp)
332 struct buffer_head *bh = mp->mp_bh[height];
333 unsigned int head_size = (height > 0) ?
334 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
337 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
338 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
344 * lookup_block - Get the next metadata block in metadata tree
345 * @ip: The GFS2 inode
346 * @height: The height of the tree (0 = dinode)
348 * @create: Non-zero if we may create a new meatdata block
349 * @new: Used to indicate if we did create a new metadata block
350 * @block: the returned disk block number
352 * Given a metatree, complete to a particular height, checks to see if the next
353 * height of the tree exists. If not the next height of the tree is created.
354 * The block number of the next height of the metadata tree is returned.
358 static int lookup_block(struct gfs2_inode *ip, unsigned int height,
359 struct metapath *mp, int create,
360 int *new, u64 *block)
363 __be64 *ptr = metapointer(&boundary, height, mp);
367 *block = be64_to_cpu(*ptr);
376 *block = gfs2_alloc_block(ip, &n);
377 if (height != ip->i_height - 1 || gfs2_is_dir(ip))
378 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), *block, 1);
380 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[height], 1);
382 *ptr = cpu_to_be64(*block);
383 ip->i_di.di_blocks++;
384 gfs2_set_inode_blocks(&ip->i_inode);
390 static int lookup_metapath(struct inode *inode, struct metapath *mp,
391 int create, int *new, u64 *dblock)
393 struct buffer_head *bh;
394 struct gfs2_inode *ip = GFS2_I(inode);
395 unsigned int end_of_metadata = ip->i_height - 1;
397 int ret = gfs2_meta_inode_buffer(ip, &bh);
403 for (x = 0; x < end_of_metadata; x++) {
404 lookup_block(ip, x, mp, create, new, dblock);
408 ret = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &mp->mp_bh[x+1]);
413 return lookup_block(ip, end_of_metadata, mp, create, new, dblock);
416 static void release_metapath(struct metapath *mp)
420 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++)
422 brelse(mp->mp_bh[i]);
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, sector_t lblock,
456 struct buffer_head *bh_map, int create)
458 struct gfs2_inode *ip = GFS2_I(inode);
459 struct gfs2_sbd *sdp = GFS2_SB(inode);
460 unsigned int bsize = sdp->sd_sb.sb_bsize;
465 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
468 const u64 *arr = sdp->sd_heightsize;
471 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
474 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
475 bmap_lock(inode, create);
476 clear_buffer_mapped(bh_map);
477 clear_buffer_new(bh_map);
478 clear_buffer_boundary(bh_map);
479 if (gfs2_is_dir(ip)) {
480 bsize = sdp->sd_jbsize;
481 arr = sdp->sd_jheightsize;
483 size = (lblock + 1) * bsize;
485 if (size > arr[ip->i_height]) {
486 u8 height = ip->i_height;
489 while (size > arr[height])
491 error = build_height(inode, &mp, height);
496 find_metapath(ip, lblock, &mp);
497 error = lookup_metapath(inode, &mp, create, &new, &dblock);
503 map_bh(bh_map, inode->i_sb, dblock);
505 set_buffer_boundary(bh_map);
507 gfs2_trans_add_bh(ip->i_gl, mp.mp_bh[0], 1);
508 gfs2_dinode_out(ip, mp.mp_bh[0]->b_data);
509 set_buffer_new(bh_map);
512 while(--maxlen && !buffer_boundary(bh_map)) {
514 mp.mp_list[ip->i_height - 1]++;
515 boundary = lookup_block(ip, ip->i_height - 1, &mp, 0, &new, &eblock);
516 if (eblock != ++dblock)
518 bh_map->b_size += (1 << inode->i_blkbits);
520 set_buffer_boundary(bh_map);
526 release_metapath(&mp);
527 bmap_unlock(inode, create);
532 * Deprecated: do not use in new code
534 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
536 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
544 bh.b_size = 1 << (inode->i_blkbits + 5);
545 ret = gfs2_block_map(inode, lblock, &bh, create);
546 *extlen = bh.b_size >> inode->i_blkbits;
547 *dblock = bh.b_blocknr;
556 * recursive_scan - recursively scan through the end of a file
558 * @dibh: the dinode buffer
559 * @mp: the path through the metadata to the point to start
560 * @height: the height the recursion is at
561 * @block: the indirect block to look at
562 * @first: 1 if this is the first block
563 * @bc: the call to make for each piece of metadata
564 * @data: data opaque to this function to pass to @bc
566 * When this is first called @height and @block should be zero and
567 * @first should be 1.
572 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
573 struct metapath *mp, unsigned int height,
574 u64 block, int first, block_call_t bc,
577 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
578 struct buffer_head *bh = NULL;
579 __be64 *top, *bottom;
582 int mh_size = sizeof(struct gfs2_meta_header);
585 error = gfs2_meta_inode_buffer(ip, &bh);
590 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
591 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
593 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
597 top = (__be64 *)(bh->b_data + mh_size) +
598 (first ? mp->mp_list[height] : 0);
600 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
603 error = bc(ip, dibh, bh, top, bottom, height, data);
607 if (height < ip->i_height - 1)
608 for (; top < bottom; top++, first = 0) {
612 bn = be64_to_cpu(*top);
614 error = recursive_scan(ip, dibh, mp, height + 1, bn,
626 * do_strip - Look for a layer a particular layer of the file and strip it off
628 * @dibh: the dinode buffer
629 * @bh: A buffer of pointers
630 * @top: The first pointer in the buffer
631 * @bottom: One more than the last pointer
632 * @height: the height this buffer is at
633 * @data: a pointer to a struct strip_mine
638 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
639 struct buffer_head *bh, __be64 *top, __be64 *bottom,
640 unsigned int height, void *data)
642 struct strip_mine *sm = data;
643 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
644 struct gfs2_rgrp_list rlist;
648 unsigned int rg_blocks = 0;
650 unsigned int revokes = 0;
657 if (height != sm->sm_height)
665 metadata = (height != ip->i_height - 1);
667 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
669 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
673 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
677 for (p = top; p < bottom; p++) {
681 bn = be64_to_cpu(*p);
683 if (bstart + blen == bn)
687 gfs2_rlist_add(sdp, &rlist, bstart);
695 gfs2_rlist_add(sdp, &rlist, bstart);
697 goto out; /* Nothing to do */
699 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
701 for (x = 0; x < rlist.rl_rgrps; x++) {
702 struct gfs2_rgrpd *rgd;
703 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
704 rg_blocks += rgd->rd_length;
707 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
711 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
712 RES_INDIRECT + RES_STATFS + RES_QUOTA,
717 down_write(&ip->i_rw_mutex);
719 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
720 gfs2_trans_add_bh(ip->i_gl, bh, 1);
725 for (p = top; p < bottom; p++) {
729 bn = be64_to_cpu(*p);
731 if (bstart + blen == bn)
736 gfs2_free_meta(ip, bstart, blen);
738 gfs2_free_data(ip, bstart, blen);
746 if (!ip->i_di.di_blocks)
747 gfs2_consist_inode(ip);
748 ip->i_di.di_blocks--;
749 gfs2_set_inode_blocks(&ip->i_inode);
753 gfs2_free_meta(ip, bstart, blen);
755 gfs2_free_data(ip, bstart, blen);
758 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
760 gfs2_dinode_out(ip, dibh->b_data);
762 up_write(&ip->i_rw_mutex);
767 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
769 gfs2_rlist_free(&rlist);
771 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
776 * do_grow - Make a file look bigger than it is
778 * @size: the size to set the file to
780 * Called with an exclusive lock on @ip.
785 static int do_grow(struct gfs2_inode *ip, u64 size)
787 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
788 struct gfs2_alloc *al;
789 struct buffer_head *dibh;
792 al = gfs2_alloc_get(ip);
794 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
798 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
802 al->al_requested = sdp->sd_max_height + RES_DATA;
804 error = gfs2_inplace_reserve(ip);
808 error = gfs2_trans_begin(sdp,
809 sdp->sd_max_height + al->al_rgd->rd_length +
810 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
814 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
815 const u64 *arr = sdp->sd_heightsize;
816 if (gfs2_is_stuffed(ip)) {
817 error = gfs2_unstuff_dinode(ip, NULL);
822 down_write(&ip->i_rw_mutex);
823 if (size > arr[ip->i_height]) {
825 u8 height = ip->i_height;
826 while(size > arr[height])
828 error = build_height(&ip->i_inode, &mp, height);
830 up_write(&ip->i_rw_mutex);
835 ip->i_di.di_size = size;
836 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
838 error = gfs2_meta_inode_buffer(ip, &dibh);
842 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
843 gfs2_dinode_out(ip, dibh->b_data);
849 gfs2_inplace_release(ip);
851 gfs2_quota_unlock(ip);
859 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
861 * This is partly borrowed from ext3.
863 static int gfs2_block_truncate_page(struct address_space *mapping)
865 struct inode *inode = mapping->host;
866 struct gfs2_inode *ip = GFS2_I(inode);
867 loff_t from = inode->i_size;
868 unsigned long index = from >> PAGE_CACHE_SHIFT;
869 unsigned offset = from & (PAGE_CACHE_SIZE-1);
870 unsigned blocksize, iblock, length, pos;
871 struct buffer_head *bh;
875 page = grab_cache_page(mapping, index);
879 blocksize = inode->i_sb->s_blocksize;
880 length = blocksize - (offset & (blocksize - 1));
881 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
883 if (!page_has_buffers(page))
884 create_empty_buffers(page, blocksize, 0);
886 /* Find the buffer that contains "offset" */
887 bh = page_buffers(page);
889 while (offset >= pos) {
890 bh = bh->b_this_page;
897 if (!buffer_mapped(bh)) {
898 gfs2_block_map(inode, iblock, bh, 0);
899 /* unmapped? It's a hole - nothing to do */
900 if (!buffer_mapped(bh))
904 /* Ok, it's mapped. Make sure it's up-to-date */
905 if (PageUptodate(page))
906 set_buffer_uptodate(bh);
908 if (!buffer_uptodate(bh)) {
910 ll_rw_block(READ, 1, &bh);
912 /* Uhhuh. Read error. Complain and punt. */
913 if (!buffer_uptodate(bh))
918 if (!gfs2_is_writeback(ip))
919 gfs2_trans_add_bh(ip->i_gl, bh, 0);
921 zero_user(page, offset, length);
925 page_cache_release(page);
929 static int trunc_start(struct gfs2_inode *ip, u64 size)
931 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
932 struct buffer_head *dibh;
933 int journaled = gfs2_is_jdata(ip);
936 error = gfs2_trans_begin(sdp,
937 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
941 error = gfs2_meta_inode_buffer(ip, &dibh);
945 if (gfs2_is_stuffed(ip)) {
946 ip->i_di.di_size = size;
947 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
948 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
949 gfs2_dinode_out(ip, dibh->b_data);
950 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
954 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
955 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
958 ip->i_di.di_size = size;
959 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
960 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
961 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
962 gfs2_dinode_out(ip, dibh->b_data);
973 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
975 unsigned int height = ip->i_height;
983 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
985 find_metapath(ip, lblock, &mp);
988 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
993 struct strip_mine sm;
994 sm.sm_first = !!size;
995 sm.sm_height = height;
997 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1002 gfs2_quota_unhold(ip);
1009 static int trunc_end(struct gfs2_inode *ip)
1011 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1012 struct buffer_head *dibh;
1015 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1019 down_write(&ip->i_rw_mutex);
1021 error = gfs2_meta_inode_buffer(ip, &dibh);
1025 if (!ip->i_di.di_size) {
1027 ip->i_goal = ip->i_no_addr;
1028 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1030 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1031 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1033 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1034 gfs2_dinode_out(ip, dibh->b_data);
1038 up_write(&ip->i_rw_mutex);
1039 gfs2_trans_end(sdp);
1044 * do_shrink - make a file smaller
1046 * @size: the size to make the file
1047 * @truncator: function to truncate the last partial block
1049 * Called with an exclusive lock on @ip.
1054 static int do_shrink(struct gfs2_inode *ip, u64 size)
1058 error = trunc_start(ip, size);
1064 error = trunc_dealloc(ip, size);
1066 error = trunc_end(ip);
1071 static int do_touch(struct gfs2_inode *ip, u64 size)
1073 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1074 struct buffer_head *dibh;
1077 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1081 down_write(&ip->i_rw_mutex);
1083 error = gfs2_meta_inode_buffer(ip, &dibh);
1087 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1088 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1089 gfs2_dinode_out(ip, dibh->b_data);
1093 up_write(&ip->i_rw_mutex);
1094 gfs2_trans_end(sdp);
1099 * gfs2_truncatei - make a file a given size
1101 * @size: the size to make the file
1102 * @truncator: function to truncate the last partial block
1104 * The file size can grow, shrink, or stay the same size.
1109 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1113 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1116 if (size > ip->i_di.di_size)
1117 error = do_grow(ip, size);
1118 else if (size < ip->i_di.di_size)
1119 error = do_shrink(ip, size);
1121 /* update time stamps */
1122 error = do_touch(ip, size);
1127 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1130 error = trunc_dealloc(ip, ip->i_di.di_size);
1132 error = trunc_end(ip);
1136 int gfs2_file_dealloc(struct gfs2_inode *ip)
1138 return trunc_dealloc(ip, 0);
1142 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1144 * @len: the number of bytes to be written to the file
1145 * @data_blocks: returns the number of data blocks required
1146 * @ind_blocks: returns the number of indirect blocks required
1150 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1151 unsigned int *data_blocks, unsigned int *ind_blocks)
1153 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1156 if (gfs2_is_dir(ip)) {
1157 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1158 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1160 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1161 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1164 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1165 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1171 * gfs2_write_alloc_required - figure out if a write will require an allocation
1172 * @ip: the file being written to
1173 * @offset: the offset to write to
1174 * @len: the number of bytes being written
1175 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1180 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1181 unsigned int len, int *alloc_required)
1183 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1184 struct buffer_head bh;
1186 u64 lblock, lblock_stop, size;
1188 *alloc_required = 0;
1193 if (gfs2_is_stuffed(ip)) {
1195 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1196 *alloc_required = 1;
1200 *alloc_required = 1;
1201 shift = sdp->sd_sb.sb_bsize_shift;
1202 if (gfs2_is_dir(ip)) {
1203 unsigned int bsize = sdp->sd_jbsize;
1205 do_div(lblock, bsize);
1206 lblock_stop = offset + len + bsize - 1;
1207 do_div(lblock_stop, bsize);
1209 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1210 lblock = offset >> shift;
1211 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1212 if (lblock_stop > end_of_file)
1216 size = (lblock_stop - lblock) << shift;
1220 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1221 if (!buffer_mapped(&bh))
1224 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1227 *alloc_required = 0;