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 v.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, uint64_t *top,
42 uint64_t *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 uint64_t 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 *(uint64_t *)(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);
177 up_write(&ip->i_rw_mutex);
183 * calc_tree_height - Calculate the height of a metadata tree
184 * @ip: The GFS2 inode
185 * @size: The proposed size of the file
187 * Work out how tall a metadata tree needs to be in order to accommodate a
188 * file of a particular size. If size is less than the current size of
189 * the inode, then the current size of the inode is used instead of the
192 * Returns: the height the tree should be
195 static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
197 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
199 unsigned int max, height;
201 if (ip->i_di.di_size > size)
202 size = ip->i_di.di_size;
204 if (gfs2_is_dir(ip)) {
205 arr = sdp->sd_jheightsize;
206 max = sdp->sd_max_jheight;
208 arr = sdp->sd_heightsize;
209 max = sdp->sd_max_height;
212 for (height = 0; height < max; height++)
213 if (arr[height] >= size)
220 * build_height - Build a metadata tree of the requested height
221 * @ip: The GFS2 inode
222 * @height: The height to build to
228 static int build_height(struct inode *inode, unsigned height)
230 struct gfs2_inode *ip = GFS2_I(inode);
231 unsigned new_height = height - ip->i_di.di_height;
232 struct buffer_head *dibh;
233 struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
239 if (height <= ip->i_di.di_height)
242 error = gfs2_meta_inode_buffer(ip, &dibh);
246 for(n = 0; n < new_height; n++) {
247 bn = gfs2_alloc_meta(ip);
248 blocks[n] = gfs2_meta_new(ip->i_gl, bn);
249 gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
253 bn = blocks[0]->b_blocknr;
254 if (new_height > 1) {
255 for(; n < new_height-1; n++) {
256 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
258 gfs2_buffer_clear_tail(blocks[n],
259 sizeof(struct gfs2_meta_header));
260 bp = (u64 *)(blocks[n]->b_data +
261 sizeof(struct gfs2_meta_header));
262 *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
267 gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
268 gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
269 dibh, sizeof(struct gfs2_dinode));
271 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
272 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
273 bp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
274 *bp = cpu_to_be64(bn);
275 ip->i_di.di_height += new_height;
276 ip->i_di.di_blocks += new_height;
277 gfs2_dinode_out(&ip->i_di, dibh->b_data);
283 * find_metapath - Find path through the metadata tree
284 * @ip: The inode pointer
285 * @mp: The metapath to return the result in
286 * @block: The disk block to look up
288 * This routine returns a struct metapath structure that defines a path
289 * through the metadata of inode "ip" to get to block "block".
292 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
293 * filesystem with a blocksize of 4096.
295 * find_metapath() would return a struct metapath structure set to:
296 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
297 * and mp_list[2] = 165.
299 * That means that in order to get to the block containing the byte at
300 * offset 101342453, we would load the indirect block pointed to by pointer
301 * 0 in the dinode. We would then load the indirect block pointed to by
302 * pointer 48 in that indirect block. We would then load the data block
303 * pointed to by pointer 165 in that indirect block.
305 * ----------------------------------------
310 * ----------------------------------------
314 * ----------------------------------------
318 * |0 5 6 7 8 9 0 1 2|
319 * ----------------------------------------
323 * ----------------------------------------
328 * ----------------------------------------
332 * ----------------------------------------
333 * | Data block containing offset |
337 * ----------------------------------------
341 static void find_metapath(struct gfs2_inode *ip, uint64_t block,
344 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
348 for (i = ip->i_di.di_height; i--;)
349 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
354 * metapointer - Return pointer to start of metadata in a buffer
356 * @height: The metadata height (0 = dinode)
359 * Return a pointer to the block number of the next height of the metadata
360 * tree given a buffer containing the pointer to the current height of the
364 static inline u64 *metapointer(struct buffer_head *bh, int *boundary,
365 unsigned int height, const struct metapath *mp)
367 unsigned int head_size = (height > 0) ?
368 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
371 ptr = ((u64 *)(bh->b_data + head_size)) + mp->mp_list[height];
372 if (ptr + 1 == (u64*)(bh->b_data + bh->b_size))
378 * lookup_block - Get the next metadata block in metadata tree
379 * @ip: The GFS2 inode
380 * @bh: Buffer containing the pointers to metadata blocks
381 * @height: The height of the tree (0 = dinode)
383 * @create: Non-zero if we may create a new meatdata block
384 * @new: Used to indicate if we did create a new metadata block
385 * @block: the returned disk block number
387 * Given a metatree, complete to a particular height, checks to see if the next
388 * height of the tree exists. If not the next height of the tree is created.
389 * The block number of the next height of the metadata tree is returned.
393 static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
394 unsigned int height, struct metapath *mp, int create,
395 int *new, uint64_t *block)
398 uint64_t *ptr = metapointer(bh, &boundary, height, mp);
401 *block = be64_to_cpu(*ptr);
410 if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
411 *block = gfs2_alloc_data(ip);
413 *block = gfs2_alloc_meta(ip);
415 gfs2_trans_add_bh(ip->i_gl, bh, 1);
417 *ptr = cpu_to_be64(*block);
418 ip->i_di.di_blocks++;
425 * gfs2_block_pointers - Map a block from an inode to a disk block
427 * @lblock: The logical block number
428 * @new: Value/Result argument (1 = may create/did create new blocks)
429 * @boundary: gets set if we've hit a block boundary
430 * @mp: metapath to use
432 * Find the block number on the current device which corresponds to an
433 * inode's block. If the block had to be created, "new" will be set.
438 static struct buffer_head *gfs2_block_pointers(struct inode *inode, u64 lblock,
439 int *new, u64 *dblock,
443 struct gfs2_inode *ip = GFS2_I(inode);
444 struct gfs2_sbd *sdp = GFS2_SB(inode);
445 struct buffer_head *bh;
449 unsigned int end_of_metadata;
456 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
459 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
461 height = calc_tree_height(ip, (lblock + 1) * bsize);
462 if (ip->i_di.di_height < height) {
466 error = build_height(inode, height);
471 find_metapath(ip, lblock, mp);
472 end_of_metadata = ip->i_di.di_height - 1;
474 error = gfs2_meta_inode_buffer(ip, &bh);
478 for (x = 0; x < end_of_metadata; x++) {
479 lookup_block(ip, bh, x, mp, create, new, dblock);
484 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
489 *boundary = lookup_block(ip, bh, end_of_metadata, mp, create, new, dblock);
491 struct buffer_head *dibh;
492 error = gfs2_meta_inode_buffer(ip, &dibh);
494 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
495 gfs2_dinode_out(&ip->i_di, dibh->b_data);
501 return ERR_PTR(error);
505 static inline void bmap_lock(struct inode *inode, int create)
507 struct gfs2_inode *ip = GFS2_I(inode);
509 down_write(&ip->i_rw_mutex);
511 down_read(&ip->i_rw_mutex);
514 static inline void bmap_unlock(struct inode *inode, int create)
516 struct gfs2_inode *ip = GFS2_I(inode);
518 up_write(&ip->i_rw_mutex);
520 up_read(&ip->i_rw_mutex);
523 int gfs2_block_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, int *boundary)
526 struct buffer_head *bh;
529 bmap_lock(inode, create);
530 bh = gfs2_block_pointers(inode, lblock, new, dblock, boundary, &mp);
531 bmap_unlock(inode, create);
540 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
542 struct gfs2_inode *ip = GFS2_I(inode);
543 struct gfs2_sbd *sdp = GFS2_SB(inode);
545 struct buffer_head *bh;
553 bmap_lock(inode, create);
554 bh = gfs2_block_pointers(inode, lblock, new, dblock, &boundary, &mp);
557 if (bh && !IS_ERR(bh) && *dblock && !*new) {
561 unsigned end_of_metadata = ip->i_di.di_height - 1;
563 nptrs = (end_of_metadata) ? sdp->sd_inptrs : sdp->sd_diptrs;
564 while (++mp.mp_list[end_of_metadata] < nptrs) {
565 lookup_block(ip, bh, end_of_metadata, &mp, 0, &tmp_new, &tmp_dblock);
566 if (*dblock + *extlen != tmp_dblock)
571 bmap_unlock(inode, create);
581 * recursive_scan - recursively scan through the end of a file
583 * @dibh: the dinode buffer
584 * @mp: the path through the metadata to the point to start
585 * @height: the height the recursion is at
586 * @block: the indirect block to look at
587 * @first: 1 if this is the first block
588 * @bc: the call to make for each piece of metadata
589 * @data: data opaque to this function to pass to @bc
591 * When this is first called @height and @block should be zero and
592 * @first should be 1.
597 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
598 struct metapath *mp, unsigned int height,
599 uint64_t block, int first, block_call_t bc,
602 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
603 struct buffer_head *bh = NULL;
604 uint64_t *top, *bottom;
607 int mh_size = sizeof(struct gfs2_meta_header);
610 error = gfs2_meta_inode_buffer(ip, &bh);
615 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
617 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
620 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
624 top = (uint64_t *)(bh->b_data + mh_size) +
625 ((first) ? mp->mp_list[height] : 0);
627 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
630 error = bc(ip, dibh, bh, top, bottom, height, data);
634 if (height < ip->i_di.di_height - 1)
635 for (; top < bottom; top++, first = 0) {
639 bn = be64_to_cpu(*top);
641 error = recursive_scan(ip, dibh, mp, height + 1, bn,
654 * do_strip - Look for a layer a particular layer of the file and strip it off
656 * @dibh: the dinode buffer
657 * @bh: A buffer of pointers
658 * @top: The first pointer in the buffer
659 * @bottom: One more than the last pointer
660 * @height: the height this buffer is at
661 * @data: a pointer to a struct strip_mine
666 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
667 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
668 unsigned int height, void *data)
670 struct strip_mine *sm = data;
671 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
672 struct gfs2_rgrp_list rlist;
676 unsigned int rg_blocks = 0;
678 unsigned int revokes = 0;
685 if (height != sm->sm_height)
693 metadata = (height != ip->i_di.di_height - 1);
695 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
697 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
701 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
705 for (p = top; p < bottom; p++) {
709 bn = be64_to_cpu(*p);
711 if (bstart + blen == bn)
715 gfs2_rlist_add(sdp, &rlist, bstart);
723 gfs2_rlist_add(sdp, &rlist, bstart);
725 goto out; /* Nothing to do */
727 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
729 for (x = 0; x < rlist.rl_rgrps; x++) {
730 struct gfs2_rgrpd *rgd;
731 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
732 rg_blocks += rgd->rd_ri.ri_length;
735 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
739 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
740 RES_INDIRECT + RES_STATFS + RES_QUOTA,
745 down_write(&ip->i_rw_mutex);
747 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
748 gfs2_trans_add_bh(ip->i_gl, bh, 1);
753 for (p = top; p < bottom; p++) {
757 bn = be64_to_cpu(*p);
759 if (bstart + blen == bn)
764 gfs2_free_meta(ip, bstart, blen);
766 gfs2_free_data(ip, bstart, blen);
774 if (!ip->i_di.di_blocks)
775 gfs2_consist_inode(ip);
776 ip->i_di.di_blocks--;
780 gfs2_free_meta(ip, bstart, blen);
782 gfs2_free_data(ip, bstart, blen);
785 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
787 gfs2_dinode_out(&ip->i_di, dibh->b_data);
789 up_write(&ip->i_rw_mutex);
794 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
797 gfs2_rlist_free(&rlist);
800 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
806 * do_grow - Make a file look bigger than it is
808 * @size: the size to set the file to
810 * Called with an exclusive lock on @ip.
815 static int do_grow(struct gfs2_inode *ip, uint64_t size)
817 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
818 struct gfs2_alloc *al;
819 struct buffer_head *dibh;
823 al = gfs2_alloc_get(ip);
825 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
829 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
833 al->al_requested = sdp->sd_max_height + RES_DATA;
835 error = gfs2_inplace_reserve(ip);
839 error = gfs2_trans_begin(sdp,
840 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
841 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
845 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
846 if (gfs2_is_stuffed(ip)) {
847 error = gfs2_unstuff_dinode(ip, NULL);
852 h = calc_tree_height(ip, size);
853 if (ip->i_di.di_height < h) {
854 down_write(&ip->i_rw_mutex);
855 error = build_height(&ip->i_inode, h);
856 up_write(&ip->i_rw_mutex);
862 ip->i_di.di_size = size;
863 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
865 error = gfs2_meta_inode_buffer(ip, &dibh);
869 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
870 gfs2_dinode_out(&ip->i_di, dibh->b_data);
877 gfs2_inplace_release(ip);
880 gfs2_quota_unlock(ip);
890 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
892 * This is partly borrowed from ext3.
894 static int gfs2_block_truncate_page(struct address_space *mapping)
896 struct inode *inode = mapping->host;
897 struct gfs2_inode *ip = GFS2_I(inode);
898 struct gfs2_sbd *sdp = GFS2_SB(inode);
899 loff_t from = inode->i_size;
900 unsigned long index = from >> PAGE_CACHE_SHIFT;
901 unsigned offset = from & (PAGE_CACHE_SIZE-1);
902 unsigned blocksize, iblock, length, pos;
903 struct buffer_head *bh;
908 page = grab_cache_page(mapping, index);
912 blocksize = inode->i_sb->s_blocksize;
913 length = blocksize - (offset & (blocksize - 1));
914 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
916 if (!page_has_buffers(page))
917 create_empty_buffers(page, blocksize, 0);
919 /* Find the buffer that contains "offset" */
920 bh = page_buffers(page);
922 while (offset >= pos) {
923 bh = bh->b_this_page;
930 if (!buffer_mapped(bh)) {
931 gfs2_get_block(inode, iblock, bh, 0);
932 /* unmapped? It's a hole - nothing to do */
933 if (!buffer_mapped(bh))
937 /* Ok, it's mapped. Make sure it's up-to-date */
938 if (PageUptodate(page))
939 set_buffer_uptodate(bh);
941 if (!buffer_uptodate(bh)) {
943 ll_rw_block(READ, 1, &bh);
945 /* Uhhuh. Read error. Complain and punt. */
946 if (!buffer_uptodate(bh))
950 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
951 gfs2_trans_add_bh(ip->i_gl, bh, 0);
953 kaddr = kmap_atomic(page, KM_USER0);
954 memset(kaddr + offset, 0, length);
955 flush_dcache_page(page);
956 kunmap_atomic(kaddr, KM_USER0);
960 page_cache_release(page);
964 static int trunc_start(struct gfs2_inode *ip, uint64_t size)
966 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
967 struct buffer_head *dibh;
968 int journaled = gfs2_is_jdata(ip);
971 error = gfs2_trans_begin(sdp,
972 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
976 error = gfs2_meta_inode_buffer(ip, &dibh);
980 if (gfs2_is_stuffed(ip)) {
981 ip->i_di.di_size = size;
982 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
983 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
984 gfs2_dinode_out(&ip->i_di, dibh->b_data);
985 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
989 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
990 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
993 ip->i_di.di_size = size;
994 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
995 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
996 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
997 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1004 gfs2_trans_end(sdp);
1009 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
1011 unsigned int height = ip->i_di.di_height;
1019 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
1021 find_metapath(ip, lblock, &mp);
1024 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1029 struct strip_mine sm;
1030 sm.sm_first = !!size;
1031 sm.sm_height = height;
1033 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1038 gfs2_quota_unhold(ip);
1045 static int trunc_end(struct gfs2_inode *ip)
1047 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1048 struct buffer_head *dibh;
1051 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1055 down_write(&ip->i_rw_mutex);
1057 error = gfs2_meta_inode_buffer(ip, &dibh);
1061 if (!ip->i_di.di_size) {
1062 ip->i_di.di_height = 0;
1063 ip->i_di.di_goal_meta =
1064 ip->i_di.di_goal_data =
1066 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1068 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1069 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1071 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1072 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1076 up_write(&ip->i_rw_mutex);
1078 gfs2_trans_end(sdp);
1084 * do_shrink - make a file smaller
1086 * @size: the size to make the file
1087 * @truncator: function to truncate the last partial block
1089 * Called with an exclusive lock on @ip.
1094 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
1098 error = trunc_start(ip, size);
1104 error = trunc_dealloc(ip, size);
1106 error = trunc_end(ip);
1112 * gfs2_truncatei - make a file a given size
1114 * @size: the size to make the file
1115 * @truncator: function to truncate the last partial block
1117 * The file size can grow, shrink, or stay the same size.
1122 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
1126 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_di.di_mode)))
1129 if (size > ip->i_di.di_size)
1130 error = do_grow(ip, size);
1132 error = do_shrink(ip, size);
1137 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1140 error = trunc_dealloc(ip, ip->i_di.di_size);
1142 error = trunc_end(ip);
1146 int gfs2_file_dealloc(struct gfs2_inode *ip)
1148 return trunc_dealloc(ip, 0);
1152 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1154 * @len: the number of bytes to be written to the file
1155 * @data_blocks: returns the number of data blocks required
1156 * @ind_blocks: returns the number of indirect blocks required
1160 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1161 unsigned int *data_blocks, unsigned int *ind_blocks)
1163 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1166 if (gfs2_is_dir(ip)) {
1167 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1168 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1170 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1171 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1174 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1175 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1181 * gfs2_write_alloc_required - figure out if a write will require an allocation
1182 * @ip: the file being written to
1183 * @offset: the offset to write to
1184 * @len: the number of bytes being written
1185 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1190 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1191 unsigned int len, int *alloc_required)
1193 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1194 uint64_t lblock, lblock_stop, dblock;
1199 *alloc_required = 0;
1204 if (gfs2_is_stuffed(ip)) {
1206 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1207 *alloc_required = 1;
1211 if (gfs2_is_dir(ip)) {
1212 unsigned int bsize = sdp->sd_jbsize;
1214 do_div(lblock, bsize);
1215 lblock_stop = offset + len + bsize - 1;
1216 do_div(lblock_stop, bsize);
1218 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1219 lblock = offset >> shift;
1220 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1223 for (; lblock < lblock_stop; lblock += extlen) {
1224 error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
1229 *alloc_required = 1;