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;
192 struct gfs2_dinode *di;
198 if (height <= ip->i_height)
201 error = gfs2_meta_inode_buffer(ip, &dibh);
207 bn = gfs2_alloc_block(ip, &n);
208 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, n);
210 mp->mp_bh[i] = gfs2_meta_new(ip->i_gl, bn++);
211 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i], 1);
214 } while(i < new_height);
217 bn = mp->mp_bh[0]->b_blocknr;
218 if (new_height > 1) {
219 for(; n < new_height-1; n++) {
220 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN,
222 gfs2_buffer_clear_tail(mp->mp_bh[n],
223 sizeof(struct gfs2_meta_header));
224 bp = (__be64 *)(mp->mp_bh[n]->b_data +
225 sizeof(struct gfs2_meta_header));
226 *bp = cpu_to_be64(mp->mp_bh[n+1]->b_blocknr);
227 brelse(mp->mp_bh[n]);
231 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
232 gfs2_buffer_copy_tail(mp->mp_bh[n], sizeof(struct gfs2_meta_header),
233 dibh, sizeof(struct gfs2_dinode));
234 brelse(mp->mp_bh[n]);
235 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
236 di = (struct gfs2_dinode *)dibh->b_data;
237 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
238 *(__be64 *)(di + 1) = cpu_to_be64(bn);
239 ip->i_height += new_height;
240 gfs2_add_inode_blocks(&ip->i_inode, new_height);
241 di->di_height = cpu_to_be16(ip->i_height);
242 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
248 * find_metapath - Find path through the metadata tree
249 * @ip: The inode pointer
250 * @mp: The metapath to return the result in
251 * @block: The disk block to look up
253 * This routine returns a struct metapath structure that defines a path
254 * through the metadata of inode "ip" to get to block "block".
257 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
258 * filesystem with a blocksize of 4096.
260 * find_metapath() would return a struct metapath structure set to:
261 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
262 * and mp_list[2] = 165.
264 * That means that in order to get to the block containing the byte at
265 * offset 101342453, we would load the indirect block pointed to by pointer
266 * 0 in the dinode. We would then load the indirect block pointed to by
267 * pointer 48 in that indirect block. We would then load the data block
268 * pointed to by pointer 165 in that indirect block.
270 * ----------------------------------------
275 * ----------------------------------------
279 * ----------------------------------------
283 * |0 5 6 7 8 9 0 1 2|
284 * ----------------------------------------
288 * ----------------------------------------
293 * ----------------------------------------
297 * ----------------------------------------
298 * | Data block containing offset |
302 * ----------------------------------------
306 static void find_metapath(struct gfs2_inode *ip, u64 block,
309 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
312 for (i = ip->i_height; i--;)
313 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
318 * 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(int *boundary, unsigned int height,
328 const struct metapath *mp)
330 struct buffer_head *bh = mp->mp_bh[height];
331 unsigned int head_size = (height > 0) ?
332 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
335 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
336 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
342 * lookup_block - Get the next metadata block in metadata tree
343 * @ip: The GFS2 inode
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, unsigned int height,
357 struct metapath *mp, int create,
358 int *new, u64 *block)
361 __be64 *ptr = metapointer(&boundary, height, mp);
365 *block = be64_to_cpu(*ptr);
374 *block = gfs2_alloc_block(ip, &n);
375 if (height != ip->i_height - 1 || gfs2_is_dir(ip))
376 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), *block, 1);
378 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[height], 1);
380 *ptr = cpu_to_be64(*block);
381 gfs2_add_inode_blocks(&ip->i_inode, 1);
387 static int lookup_metapath(struct inode *inode, struct metapath *mp,
388 int create, int *new, u64 *dblock)
390 struct buffer_head *bh;
391 struct gfs2_inode *ip = GFS2_I(inode);
392 unsigned int end_of_metadata = ip->i_height - 1;
394 int ret = gfs2_meta_inode_buffer(ip, &bh);
400 for (x = 0; x < end_of_metadata; x++) {
401 lookup_block(ip, x, mp, create, new, dblock);
405 ret = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &mp->mp_bh[x+1]);
410 return lookup_block(ip, end_of_metadata, mp, create, new, dblock);
413 static void release_metapath(struct metapath *mp)
417 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++)
419 brelse(mp->mp_bh[i]);
423 * gfs2_extent_length - Returns length of an extent of blocks
424 * @start: Start of the buffer
425 * @len: Length of the buffer in bytes
426 * @ptr: Current position in the buffer
427 * @limit: Max extent length to return (0 = unlimited)
428 * @eob: Set to 1 if we hit "end of block"
430 * If the first block is zero (unallocated) it will return the number of
431 * unallocated blocks in the extent, otherwise it will return the number
432 * of contiguous blocks in the extent.
434 * Returns: The length of the extent (minimum of one block)
437 static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
439 const __be64 *end = (start + len);
440 const __be64 *first = ptr;
441 u64 d = be64_to_cpu(*ptr);
448 if (limit && --limit == 0)
452 } while(be64_to_cpu(*ptr) == d);
455 return (ptr - first);
458 static inline void bmap_lock(struct inode *inode, int create)
460 struct gfs2_inode *ip = GFS2_I(inode);
462 down_write(&ip->i_rw_mutex);
464 down_read(&ip->i_rw_mutex);
467 static inline void bmap_unlock(struct inode *inode, int create)
469 struct gfs2_inode *ip = GFS2_I(inode);
471 up_write(&ip->i_rw_mutex);
473 up_read(&ip->i_rw_mutex);
477 * gfs2_block_map - Map a block from an inode to a disk block
479 * @lblock: The logical block number
480 * @bh_map: The bh to be mapped
482 * Find the block number on the current device which corresponds to an
483 * inode's block. If the block had to be created, "new" will be set.
488 int gfs2_block_map(struct inode *inode, sector_t lblock,
489 struct buffer_head *bh_map, int create)
491 struct gfs2_inode *ip = GFS2_I(inode);
492 struct gfs2_sbd *sdp = GFS2_SB(inode);
493 unsigned int bsize = sdp->sd_sb.sb_bsize;
498 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
501 const u64 *arr = sdp->sd_heightsize;
504 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
507 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
508 bmap_lock(inode, create);
509 clear_buffer_mapped(bh_map);
510 clear_buffer_new(bh_map);
511 clear_buffer_boundary(bh_map);
512 if (gfs2_is_dir(ip)) {
513 bsize = sdp->sd_jbsize;
514 arr = sdp->sd_jheightsize;
516 size = (lblock + 1) * bsize;
518 if (size > arr[ip->i_height]) {
519 u8 height = ip->i_height;
522 while (size > arr[height])
524 error = build_height(inode, &mp, height);
529 find_metapath(ip, lblock, &mp);
530 error = lookup_metapath(inode, &mp, create, &new, &dblock);
536 map_bh(bh_map, inode->i_sb, dblock);
538 set_buffer_boundary(bh_map);
539 gfs2_trans_add_bh(ip->i_gl, mp.mp_bh[0], 1);
540 gfs2_dinode_out(ip, mp.mp_bh[0]->b_data);
541 set_buffer_new(bh_map);
547 struct buffer_head *bh = mp.mp_bh[ip->i_height - 1];
548 __be64 *ptr = metapointer(&boundary, ip->i_height - 1, &mp);
549 map_bh(bh_map, inode->i_sb, dblock);
550 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
552 bh_map->b_size = (len << inode->i_blkbits);
554 set_buffer_boundary(bh_map);
559 release_metapath(&mp);
560 bmap_unlock(inode, create);
565 * Deprecated: do not use in new code
567 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
569 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
577 bh.b_size = 1 << (inode->i_blkbits + 5);
578 ret = gfs2_block_map(inode, lblock, &bh, create);
579 *extlen = bh.b_size >> inode->i_blkbits;
580 *dblock = bh.b_blocknr;
589 * recursive_scan - recursively scan through the end of a file
591 * @dibh: the dinode buffer
592 * @mp: the path through the metadata to the point to start
593 * @height: the height the recursion is at
594 * @block: the indirect block to look at
595 * @first: 1 if this is the first block
596 * @bc: the call to make for each piece of metadata
597 * @data: data opaque to this function to pass to @bc
599 * When this is first called @height and @block should be zero and
600 * @first should be 1.
605 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
606 struct metapath *mp, unsigned int height,
607 u64 block, int first, block_call_t bc,
610 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
611 struct buffer_head *bh = NULL;
612 __be64 *top, *bottom;
615 int mh_size = sizeof(struct gfs2_meta_header);
618 error = gfs2_meta_inode_buffer(ip, &bh);
623 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
624 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
626 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
630 top = (__be64 *)(bh->b_data + mh_size) +
631 (first ? mp->mp_list[height] : 0);
633 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
636 error = bc(ip, dibh, bh, top, bottom, height, data);
640 if (height < ip->i_height - 1)
641 for (; top < bottom; top++, first = 0) {
645 bn = be64_to_cpu(*top);
647 error = recursive_scan(ip, dibh, mp, height + 1, bn,
659 * do_strip - Look for a layer a particular layer of the file and strip it off
661 * @dibh: the dinode buffer
662 * @bh: A buffer of pointers
663 * @top: The first pointer in the buffer
664 * @bottom: One more than the last pointer
665 * @height: the height this buffer is at
666 * @data: a pointer to a struct strip_mine
671 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
672 struct buffer_head *bh, __be64 *top, __be64 *bottom,
673 unsigned int height, void *data)
675 struct strip_mine *sm = data;
676 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
677 struct gfs2_rgrp_list rlist;
681 unsigned int rg_blocks = 0;
683 unsigned int revokes = 0;
690 if (height != sm->sm_height)
698 metadata = (height != ip->i_height - 1);
700 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
702 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
706 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
710 for (p = top; p < bottom; p++) {
714 bn = be64_to_cpu(*p);
716 if (bstart + blen == bn)
720 gfs2_rlist_add(sdp, &rlist, bstart);
728 gfs2_rlist_add(sdp, &rlist, bstart);
730 goto out; /* Nothing to do */
732 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
734 for (x = 0; x < rlist.rl_rgrps; x++) {
735 struct gfs2_rgrpd *rgd;
736 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
737 rg_blocks += rgd->rd_length;
740 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
744 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
745 RES_INDIRECT + RES_STATFS + RES_QUOTA,
750 down_write(&ip->i_rw_mutex);
752 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
753 gfs2_trans_add_bh(ip->i_gl, bh, 1);
758 for (p = top; p < bottom; p++) {
762 bn = be64_to_cpu(*p);
764 if (bstart + blen == bn)
769 gfs2_free_meta(ip, bstart, blen);
771 gfs2_free_data(ip, bstart, blen);
779 gfs2_add_inode_blocks(&ip->i_inode, -1);
783 gfs2_free_meta(ip, bstart, blen);
785 gfs2_free_data(ip, bstart, blen);
788 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
790 gfs2_dinode_out(ip, dibh->b_data);
792 up_write(&ip->i_rw_mutex);
797 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
799 gfs2_rlist_free(&rlist);
801 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, u64 size)
817 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
818 struct gfs2_alloc *al;
819 struct buffer_head *dibh;
822 al = gfs2_alloc_get(ip);
824 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
828 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
832 al->al_requested = sdp->sd_max_height + RES_DATA;
834 error = gfs2_inplace_reserve(ip);
838 error = gfs2_trans_begin(sdp,
839 sdp->sd_max_height + al->al_rgd->rd_length +
840 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
844 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
845 const u64 *arr = sdp->sd_heightsize;
846 if (gfs2_is_stuffed(ip)) {
847 error = gfs2_unstuff_dinode(ip, NULL);
852 down_write(&ip->i_rw_mutex);
853 if (size > arr[ip->i_height]) {
855 u8 height = ip->i_height;
856 while(size > arr[height])
858 error = build_height(&ip->i_inode, &mp, height);
860 up_write(&ip->i_rw_mutex);
865 ip->i_di.di_size = size;
866 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
868 error = gfs2_meta_inode_buffer(ip, &dibh);
872 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
873 gfs2_dinode_out(ip, dibh->b_data);
879 gfs2_inplace_release(ip);
881 gfs2_quota_unlock(ip);
889 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
891 * This is partly borrowed from ext3.
893 static int gfs2_block_truncate_page(struct address_space *mapping)
895 struct inode *inode = mapping->host;
896 struct gfs2_inode *ip = GFS2_I(inode);
897 loff_t from = inode->i_size;
898 unsigned long index = from >> PAGE_CACHE_SHIFT;
899 unsigned offset = from & (PAGE_CACHE_SIZE-1);
900 unsigned blocksize, iblock, length, pos;
901 struct buffer_head *bh;
905 page = grab_cache_page(mapping, index);
909 blocksize = inode->i_sb->s_blocksize;
910 length = blocksize - (offset & (blocksize - 1));
911 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
913 if (!page_has_buffers(page))
914 create_empty_buffers(page, blocksize, 0);
916 /* Find the buffer that contains "offset" */
917 bh = page_buffers(page);
919 while (offset >= pos) {
920 bh = bh->b_this_page;
927 if (!buffer_mapped(bh)) {
928 gfs2_block_map(inode, iblock, bh, 0);
929 /* unmapped? It's a hole - nothing to do */
930 if (!buffer_mapped(bh))
934 /* Ok, it's mapped. Make sure it's up-to-date */
935 if (PageUptodate(page))
936 set_buffer_uptodate(bh);
938 if (!buffer_uptodate(bh)) {
940 ll_rw_block(READ, 1, &bh);
942 /* Uhhuh. Read error. Complain and punt. */
943 if (!buffer_uptodate(bh))
948 if (!gfs2_is_writeback(ip))
949 gfs2_trans_add_bh(ip->i_gl, bh, 0);
951 zero_user(page, offset, length);
955 page_cache_release(page);
959 static int trunc_start(struct gfs2_inode *ip, u64 size)
961 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
962 struct buffer_head *dibh;
963 int journaled = gfs2_is_jdata(ip);
966 error = gfs2_trans_begin(sdp,
967 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
971 error = gfs2_meta_inode_buffer(ip, &dibh);
975 if (gfs2_is_stuffed(ip)) {
976 ip->i_di.di_size = size;
977 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
978 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
979 gfs2_dinode_out(ip, dibh->b_data);
980 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
984 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
985 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
988 ip->i_di.di_size = size;
989 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
990 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
991 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
992 gfs2_dinode_out(ip, dibh->b_data);
1003 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
1005 unsigned int height = ip->i_height;
1013 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
1015 find_metapath(ip, lblock, &mp);
1018 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1023 struct strip_mine sm;
1024 sm.sm_first = !!size;
1025 sm.sm_height = height;
1027 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1032 gfs2_quota_unhold(ip);
1039 static int trunc_end(struct gfs2_inode *ip)
1041 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1042 struct buffer_head *dibh;
1045 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1049 down_write(&ip->i_rw_mutex);
1051 error = gfs2_meta_inode_buffer(ip, &dibh);
1055 if (!ip->i_di.di_size) {
1057 ip->i_goal = ip->i_no_addr;
1058 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1060 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1061 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1063 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1064 gfs2_dinode_out(ip, dibh->b_data);
1068 up_write(&ip->i_rw_mutex);
1069 gfs2_trans_end(sdp);
1074 * do_shrink - make a file smaller
1076 * @size: the size to make the file
1077 * @truncator: function to truncate the last partial block
1079 * Called with an exclusive lock on @ip.
1084 static int do_shrink(struct gfs2_inode *ip, u64 size)
1088 error = trunc_start(ip, size);
1094 error = trunc_dealloc(ip, size);
1096 error = trunc_end(ip);
1101 static int do_touch(struct gfs2_inode *ip, u64 size)
1103 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1104 struct buffer_head *dibh;
1107 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1111 down_write(&ip->i_rw_mutex);
1113 error = gfs2_meta_inode_buffer(ip, &dibh);
1117 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1118 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1119 gfs2_dinode_out(ip, dibh->b_data);
1123 up_write(&ip->i_rw_mutex);
1124 gfs2_trans_end(sdp);
1129 * gfs2_truncatei - make a file a given size
1131 * @size: the size to make the file
1132 * @truncator: function to truncate the last partial block
1134 * The file size can grow, shrink, or stay the same size.
1139 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1143 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1146 if (size > ip->i_di.di_size)
1147 error = do_grow(ip, size);
1148 else if (size < ip->i_di.di_size)
1149 error = do_shrink(ip, size);
1151 /* update time stamps */
1152 error = do_touch(ip, size);
1157 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1160 error = trunc_dealloc(ip, ip->i_di.di_size);
1162 error = trunc_end(ip);
1166 int gfs2_file_dealloc(struct gfs2_inode *ip)
1168 return trunc_dealloc(ip, 0);
1172 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1174 * @len: the number of bytes to be written to the file
1175 * @data_blocks: returns the number of data blocks required
1176 * @ind_blocks: returns the number of indirect blocks required
1180 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1181 unsigned int *data_blocks, unsigned int *ind_blocks)
1183 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1186 if (gfs2_is_dir(ip)) {
1187 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1188 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1190 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1191 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1194 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1195 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1201 * gfs2_write_alloc_required - figure out if a write will require an allocation
1202 * @ip: the file being written to
1203 * @offset: the offset to write to
1204 * @len: the number of bytes being written
1205 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1210 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1211 unsigned int len, int *alloc_required)
1213 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1214 struct buffer_head bh;
1216 u64 lblock, lblock_stop, size;
1218 *alloc_required = 0;
1223 if (gfs2_is_stuffed(ip)) {
1225 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1226 *alloc_required = 1;
1230 *alloc_required = 1;
1231 shift = sdp->sd_sb.sb_bsize_shift;
1232 if (gfs2_is_dir(ip)) {
1233 unsigned int bsize = sdp->sd_jbsize;
1235 do_div(lblock, bsize);
1236 lblock_stop = offset + len + bsize - 1;
1237 do_div(lblock_stop, bsize);
1239 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1240 lblock = offset >> shift;
1241 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1242 if (lblock_stop > end_of_file)
1246 size = (lblock_stop - lblock) << shift;
1250 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1251 if (!buffer_mapped(&bh))
1254 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1257 *alloc_required = 0;