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 gfs2_add_inode_blocks(&ip->i_inode, 1);
165 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
169 di->di_height = cpu_to_be16(1);
174 up_write(&ip->i_rw_mutex);
179 * build_height - Build a metadata tree of the requested height
180 * @ip: The GFS2 inode
181 * @height: The height to build to
187 static int build_height(struct inode *inode, struct metapath *mp, unsigned height)
189 struct gfs2_inode *ip = GFS2_I(inode);
190 unsigned new_height = height - ip->i_height;
191 struct buffer_head *dibh = mp->mp_bh[0];
192 struct gfs2_dinode *di;
197 BUG_ON(height <= ip->i_height);
201 bn = gfs2_alloc_block(ip, &n);
202 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, n);
204 mp->mp_bh[i + 1] = gfs2_meta_new(ip->i_gl, bn++);
205 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i + 1], 1);
208 } while(i < new_height);
211 bn = mp->mp_bh[1]->b_blocknr;
212 if (new_height > 1) {
213 for(; n < new_height-1; n++) {
214 gfs2_metatype_set(mp->mp_bh[n + 1], GFS2_METATYPE_IN,
216 gfs2_buffer_clear_tail(mp->mp_bh[n + 1],
217 sizeof(struct gfs2_meta_header));
218 bp = (__be64 *)(mp->mp_bh[n + 1]->b_data +
219 sizeof(struct gfs2_meta_header));
220 *bp = cpu_to_be64(mp->mp_bh[n+2]->b_blocknr);
221 brelse(mp->mp_bh[n+1]);
222 mp->mp_bh[n+1] = NULL;
225 gfs2_metatype_set(mp->mp_bh[n+1], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
226 gfs2_buffer_copy_tail(mp->mp_bh[n+1], sizeof(struct gfs2_meta_header),
227 dibh, sizeof(struct gfs2_dinode));
228 brelse(mp->mp_bh[n+1]);
229 mp->mp_bh[n+1] = NULL;
230 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
231 di = (struct gfs2_dinode *)dibh->b_data;
232 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
233 *(__be64 *)(di + 1) = cpu_to_be64(bn);
234 ip->i_height += new_height;
235 gfs2_add_inode_blocks(&ip->i_inode, new_height);
236 di->di_height = cpu_to_be16(ip->i_height);
237 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
242 * find_metapath - Find path through the metadata tree
243 * @ip: The inode pointer
244 * @mp: The metapath to return the result in
245 * @block: The disk block to look up
247 * This routine returns a struct metapath structure that defines a path
248 * through the metadata of inode "ip" to get to block "block".
251 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
252 * filesystem with a blocksize of 4096.
254 * find_metapath() would return a struct metapath structure set to:
255 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
256 * and mp_list[2] = 165.
258 * That means that in order to get to the block containing the byte at
259 * offset 101342453, we would load the indirect block pointed to by pointer
260 * 0 in the dinode. We would then load the indirect block pointed to by
261 * pointer 48 in that indirect block. We would then load the data block
262 * pointed to by pointer 165 in that indirect block.
264 * ----------------------------------------
269 * ----------------------------------------
273 * ----------------------------------------
277 * |0 5 6 7 8 9 0 1 2|
278 * ----------------------------------------
282 * ----------------------------------------
287 * ----------------------------------------
291 * ----------------------------------------
292 * | Data block containing offset |
296 * ----------------------------------------
300 static void find_metapath(struct gfs2_inode *ip, u64 block,
303 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
306 for (i = ip->i_height; i--;)
307 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
312 * metapointer - Return pointer to start of metadata in a buffer
313 * @height: The metadata height (0 = dinode)
316 * Return a pointer to the block number of the next height of the metadata
317 * tree given a buffer containing the pointer to the current height of the
321 static inline __be64 *metapointer(int *boundary, unsigned int height,
322 const struct metapath *mp)
324 struct buffer_head *bh = mp->mp_bh[height];
325 unsigned int head_size = (height > 0) ?
326 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
329 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
330 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
336 * lookup_block - Get the next metadata block in metadata tree
337 * @ip: The GFS2 inode
338 * @height: The height of the tree (0 = dinode)
340 * @create: Non-zero if we may create a new meatdata block
341 * @new: Used to indicate if we did create a new metadata block
342 * @block: the returned disk block number
344 * Given a metatree, complete to a particular height, checks to see if the next
345 * height of the tree exists. If not the next height of the tree is created.
346 * The block number of the next height of the metadata tree is returned.
350 static int lookup_block(struct gfs2_inode *ip, unsigned int height,
351 struct metapath *mp, int create,
352 int *new, u64 *block)
355 __be64 *ptr = metapointer(&boundary, height, mp);
359 *block = be64_to_cpu(*ptr);
368 *block = gfs2_alloc_block(ip, &n);
369 if (height != ip->i_height - 1 || gfs2_is_dir(ip))
370 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), *block, 1);
372 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[height], 1);
374 *ptr = cpu_to_be64(*block);
375 gfs2_add_inode_blocks(&ip->i_inode, 1);
381 static int lookup_metapath(struct inode *inode, struct metapath *mp,
382 int create, int *new, u64 *dblock)
384 struct buffer_head *bh;
385 struct gfs2_inode *ip = GFS2_I(inode);
386 unsigned int end_of_metadata = ip->i_height - 1;
390 for (x = 0; x < end_of_metadata; x++) {
391 lookup_block(ip, x, mp, create, new, dblock);
395 ret = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &mp->mp_bh[x+1]);
400 return lookup_block(ip, end_of_metadata, mp, create, new, dblock);
403 static void release_metapath(struct metapath *mp)
407 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++)
409 brelse(mp->mp_bh[i]);
413 * gfs2_extent_length - Returns length of an extent of blocks
414 * @start: Start of the buffer
415 * @len: Length of the buffer in bytes
416 * @ptr: Current position in the buffer
417 * @limit: Max extent length to return (0 = unlimited)
418 * @eob: Set to 1 if we hit "end of block"
420 * If the first block is zero (unallocated) it will return the number of
421 * unallocated blocks in the extent, otherwise it will return the number
422 * of contiguous blocks in the extent.
424 * Returns: The length of the extent (minimum of one block)
427 static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
429 const __be64 *end = (start + len);
430 const __be64 *first = ptr;
431 u64 d = be64_to_cpu(*ptr);
438 if (limit && --limit == 0)
442 } while(be64_to_cpu(*ptr) == d);
445 return (ptr - first);
448 static inline void bmap_lock(struct inode *inode, int create)
450 struct gfs2_inode *ip = GFS2_I(inode);
452 down_write(&ip->i_rw_mutex);
454 down_read(&ip->i_rw_mutex);
457 static inline void bmap_unlock(struct inode *inode, int create)
459 struct gfs2_inode *ip = GFS2_I(inode);
461 up_write(&ip->i_rw_mutex);
463 up_read(&ip->i_rw_mutex);
467 * gfs2_block_map - Map a block from an inode to a disk block
469 * @lblock: The logical block number
470 * @bh_map: The bh to be mapped
472 * Find the block number on the current device which corresponds to an
473 * inode's block. If the block had to be created, "new" will be set.
478 int gfs2_block_map(struct inode *inode, sector_t lblock,
479 struct buffer_head *bh_map, int create)
481 struct gfs2_inode *ip = GFS2_I(inode);
482 struct gfs2_sbd *sdp = GFS2_SB(inode);
483 unsigned int bsize = sdp->sd_sb.sb_bsize;
488 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
491 const u64 *arr = sdp->sd_heightsize;
494 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
497 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
498 bmap_lock(inode, create);
499 clear_buffer_mapped(bh_map);
500 clear_buffer_new(bh_map);
501 clear_buffer_boundary(bh_map);
502 if (gfs2_is_dir(ip)) {
503 bsize = sdp->sd_jbsize;
504 arr = sdp->sd_jheightsize;
506 size = (lblock + 1) * bsize;
508 error = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
512 if (size > arr[ip->i_height]) {
513 u8 height = ip->i_height;
516 while (size > arr[height])
518 error = build_height(inode, &mp, height);
523 find_metapath(ip, lblock, &mp);
524 error = lookup_metapath(inode, &mp, create, &new, &dblock);
530 map_bh(bh_map, inode->i_sb, dblock);
532 set_buffer_boundary(bh_map);
533 gfs2_trans_add_bh(ip->i_gl, mp.mp_bh[0], 1);
534 gfs2_dinode_out(ip, mp.mp_bh[0]->b_data);
535 set_buffer_new(bh_map);
541 struct buffer_head *bh = mp.mp_bh[ip->i_height - 1];
542 __be64 *ptr = metapointer(&boundary, ip->i_height - 1, &mp);
543 map_bh(bh_map, inode->i_sb, dblock);
544 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
546 bh_map->b_size = (len << inode->i_blkbits);
548 set_buffer_boundary(bh_map);
553 release_metapath(&mp);
554 bmap_unlock(inode, create);
559 * Deprecated: do not use in new code
561 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
563 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
571 bh.b_size = 1 << (inode->i_blkbits + 5);
572 ret = gfs2_block_map(inode, lblock, &bh, create);
573 *extlen = bh.b_size >> inode->i_blkbits;
574 *dblock = bh.b_blocknr;
583 * recursive_scan - recursively scan through the end of a file
585 * @dibh: the dinode buffer
586 * @mp: the path through the metadata to the point to start
587 * @height: the height the recursion is at
588 * @block: the indirect block to look at
589 * @first: 1 if this is the first block
590 * @bc: the call to make for each piece of metadata
591 * @data: data opaque to this function to pass to @bc
593 * When this is first called @height and @block should be zero and
594 * @first should be 1.
599 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
600 struct metapath *mp, unsigned int height,
601 u64 block, int first, block_call_t bc,
604 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
605 struct buffer_head *bh = NULL;
606 __be64 *top, *bottom;
609 int mh_size = sizeof(struct gfs2_meta_header);
612 error = gfs2_meta_inode_buffer(ip, &bh);
617 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
618 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
620 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
624 top = (__be64 *)(bh->b_data + mh_size) +
625 (first ? mp->mp_list[height] : 0);
627 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
630 error = bc(ip, dibh, bh, top, bottom, height, data);
634 if (height < ip->i_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,
653 * do_strip - Look for a layer a particular layer of the file and strip it off
655 * @dibh: the dinode buffer
656 * @bh: A buffer of pointers
657 * @top: The first pointer in the buffer
658 * @bottom: One more than the last pointer
659 * @height: the height this buffer is at
660 * @data: a pointer to a struct strip_mine
665 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
666 struct buffer_head *bh, __be64 *top, __be64 *bottom,
667 unsigned int height, void *data)
669 struct strip_mine *sm = data;
670 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
671 struct gfs2_rgrp_list rlist;
675 unsigned int rg_blocks = 0;
677 unsigned int revokes = 0;
684 if (height != sm->sm_height)
692 metadata = (height != ip->i_height - 1);
694 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
696 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
700 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
704 for (p = top; p < bottom; p++) {
708 bn = be64_to_cpu(*p);
710 if (bstart + blen == bn)
714 gfs2_rlist_add(sdp, &rlist, bstart);
722 gfs2_rlist_add(sdp, &rlist, bstart);
724 goto out; /* Nothing to do */
726 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
728 for (x = 0; x < rlist.rl_rgrps; x++) {
729 struct gfs2_rgrpd *rgd;
730 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
731 rg_blocks += rgd->rd_length;
734 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
738 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
739 RES_INDIRECT + RES_STATFS + RES_QUOTA,
744 down_write(&ip->i_rw_mutex);
746 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
747 gfs2_trans_add_bh(ip->i_gl, bh, 1);
752 for (p = top; p < bottom; p++) {
756 bn = be64_to_cpu(*p);
758 if (bstart + blen == bn)
763 gfs2_free_meta(ip, bstart, blen);
765 gfs2_free_data(ip, bstart, blen);
773 gfs2_add_inode_blocks(&ip->i_inode, -1);
777 gfs2_free_meta(ip, bstart, blen);
779 gfs2_free_data(ip, bstart, blen);
782 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
784 gfs2_dinode_out(ip, dibh->b_data);
786 up_write(&ip->i_rw_mutex);
791 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
793 gfs2_rlist_free(&rlist);
795 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
800 * do_grow - Make a file look bigger than it is
802 * @size: the size to set the file to
804 * Called with an exclusive lock on @ip.
809 static int do_grow(struct gfs2_inode *ip, u64 size)
811 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
812 struct gfs2_alloc *al;
813 struct buffer_head *dibh;
816 al = gfs2_alloc_get(ip);
818 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
822 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
826 al->al_requested = sdp->sd_max_height + RES_DATA;
828 error = gfs2_inplace_reserve(ip);
832 error = gfs2_trans_begin(sdp,
833 sdp->sd_max_height + al->al_rgd->rd_length +
834 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
838 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
839 const u64 *arr = sdp->sd_heightsize;
840 if (gfs2_is_stuffed(ip)) {
841 error = gfs2_unstuff_dinode(ip, NULL);
846 down_write(&ip->i_rw_mutex);
847 if (size > arr[ip->i_height]) {
849 u8 height = ip->i_height;
850 while(size > arr[height])
852 error = build_height(&ip->i_inode, &mp, height);
854 up_write(&ip->i_rw_mutex);
859 ip->i_di.di_size = size;
860 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
862 error = gfs2_meta_inode_buffer(ip, &dibh);
866 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
867 gfs2_dinode_out(ip, dibh->b_data);
873 gfs2_inplace_release(ip);
875 gfs2_quota_unlock(ip);
883 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
885 * This is partly borrowed from ext3.
887 static int gfs2_block_truncate_page(struct address_space *mapping)
889 struct inode *inode = mapping->host;
890 struct gfs2_inode *ip = GFS2_I(inode);
891 loff_t from = inode->i_size;
892 unsigned long index = from >> PAGE_CACHE_SHIFT;
893 unsigned offset = from & (PAGE_CACHE_SIZE-1);
894 unsigned blocksize, iblock, length, pos;
895 struct buffer_head *bh;
899 page = grab_cache_page(mapping, index);
903 blocksize = inode->i_sb->s_blocksize;
904 length = blocksize - (offset & (blocksize - 1));
905 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
907 if (!page_has_buffers(page))
908 create_empty_buffers(page, blocksize, 0);
910 /* Find the buffer that contains "offset" */
911 bh = page_buffers(page);
913 while (offset >= pos) {
914 bh = bh->b_this_page;
921 if (!buffer_mapped(bh)) {
922 gfs2_block_map(inode, iblock, bh, 0);
923 /* unmapped? It's a hole - nothing to do */
924 if (!buffer_mapped(bh))
928 /* Ok, it's mapped. Make sure it's up-to-date */
929 if (PageUptodate(page))
930 set_buffer_uptodate(bh);
932 if (!buffer_uptodate(bh)) {
934 ll_rw_block(READ, 1, &bh);
936 /* Uhhuh. Read error. Complain and punt. */
937 if (!buffer_uptodate(bh))
942 if (!gfs2_is_writeback(ip))
943 gfs2_trans_add_bh(ip->i_gl, bh, 0);
945 zero_user(page, offset, length);
949 page_cache_release(page);
953 static int trunc_start(struct gfs2_inode *ip, u64 size)
955 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
956 struct buffer_head *dibh;
957 int journaled = gfs2_is_jdata(ip);
960 error = gfs2_trans_begin(sdp,
961 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
965 error = gfs2_meta_inode_buffer(ip, &dibh);
969 if (gfs2_is_stuffed(ip)) {
970 ip->i_di.di_size = size;
971 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
972 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
973 gfs2_dinode_out(ip, dibh->b_data);
974 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
978 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
979 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
982 ip->i_di.di_size = size;
983 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
984 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
985 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
986 gfs2_dinode_out(ip, dibh->b_data);
997 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
999 unsigned int height = ip->i_height;
1007 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
1009 find_metapath(ip, lblock, &mp);
1012 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1017 struct strip_mine sm;
1018 sm.sm_first = !!size;
1019 sm.sm_height = height;
1021 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1026 gfs2_quota_unhold(ip);
1033 static int trunc_end(struct gfs2_inode *ip)
1035 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1036 struct buffer_head *dibh;
1039 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1043 down_write(&ip->i_rw_mutex);
1045 error = gfs2_meta_inode_buffer(ip, &dibh);
1049 if (!ip->i_di.di_size) {
1051 ip->i_goal = ip->i_no_addr;
1052 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1054 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1055 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1057 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1058 gfs2_dinode_out(ip, dibh->b_data);
1062 up_write(&ip->i_rw_mutex);
1063 gfs2_trans_end(sdp);
1068 * do_shrink - make a file smaller
1070 * @size: the size to make the file
1071 * @truncator: function to truncate the last partial block
1073 * Called with an exclusive lock on @ip.
1078 static int do_shrink(struct gfs2_inode *ip, u64 size)
1082 error = trunc_start(ip, size);
1088 error = trunc_dealloc(ip, size);
1090 error = trunc_end(ip);
1095 static int do_touch(struct gfs2_inode *ip, u64 size)
1097 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1098 struct buffer_head *dibh;
1101 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1105 down_write(&ip->i_rw_mutex);
1107 error = gfs2_meta_inode_buffer(ip, &dibh);
1111 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1112 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1113 gfs2_dinode_out(ip, dibh->b_data);
1117 up_write(&ip->i_rw_mutex);
1118 gfs2_trans_end(sdp);
1123 * gfs2_truncatei - make a file a given size
1125 * @size: the size to make the file
1126 * @truncator: function to truncate the last partial block
1128 * The file size can grow, shrink, or stay the same size.
1133 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1137 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1140 if (size > ip->i_di.di_size)
1141 error = do_grow(ip, size);
1142 else if (size < ip->i_di.di_size)
1143 error = do_shrink(ip, size);
1145 /* update time stamps */
1146 error = do_touch(ip, size);
1151 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1154 error = trunc_dealloc(ip, ip->i_di.di_size);
1156 error = trunc_end(ip);
1160 int gfs2_file_dealloc(struct gfs2_inode *ip)
1162 return trunc_dealloc(ip, 0);
1166 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1168 * @len: the number of bytes to be written to the file
1169 * @data_blocks: returns the number of data blocks required
1170 * @ind_blocks: returns the number of indirect blocks required
1174 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1175 unsigned int *data_blocks, unsigned int *ind_blocks)
1177 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1180 if (gfs2_is_dir(ip)) {
1181 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1182 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1184 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1185 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1188 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1189 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1195 * gfs2_write_alloc_required - figure out if a write will require an allocation
1196 * @ip: the file being written to
1197 * @offset: the offset to write to
1198 * @len: the number of bytes being written
1199 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1204 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1205 unsigned int len, int *alloc_required)
1207 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1208 struct buffer_head bh;
1210 u64 lblock, lblock_stop, size;
1212 *alloc_required = 0;
1217 if (gfs2_is_stuffed(ip)) {
1219 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1220 *alloc_required = 1;
1224 *alloc_required = 1;
1225 shift = sdp->sd_sb.sb_bsize_shift;
1226 if (gfs2_is_dir(ip)) {
1227 unsigned int bsize = sdp->sd_jbsize;
1229 do_div(lblock, bsize);
1230 lblock_stop = offset + len + bsize - 1;
1231 do_div(lblock_stop, bsize);
1233 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1234 lblock = offset >> shift;
1235 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1236 if (lblock_stop > end_of_file)
1240 size = (lblock_stop - lblock) << shift;
1244 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1245 if (!buffer_mapped(&bh))
1248 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1251 *alloc_required = 0;