2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 <asm/semaphore.h>
28 /* This doesn't need to be that large as max 64 bit pointers in a 4k
29 * block is 512, so __u16 is fine for that. It saves stack space to
33 __u16 mp_list[GFS2_MAX_META_HEIGHT];
36 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
37 struct buffer_head *bh, uint64_t *top,
38 uint64_t *bottom, unsigned int height,
43 unsigned int sm_height;
47 * @gfs2_unstuffer_sync - Synchronously unstuff a dinode
53 * Cheat and use a metadata buffer instead of a data page.
58 int gfs2_unstuffer_sync(struct gfs2_inode *ip, struct buffer_head *dibh,
59 uint64_t block, void *private)
61 struct buffer_head *bh;
64 bh = gfs2_meta_new(ip->i_gl, block);
66 gfs2_buffer_copy_tail(bh, 0, dibh, sizeof(struct gfs2_dinode));
69 error = sync_dirty_buffer(bh);
77 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
78 * @ip: The GFS2 inode to unstuff
79 * @unstuffer: the routine that handles unstuffing a non-zero length file
80 * @private: private data for the unstuffer
82 * This routine unstuffs a dinode and returns it to a "normal" state such
83 * that the height can be grown in the traditional way.
88 int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
91 struct buffer_head *bh, *dibh;
93 int isdir = gfs2_is_dir(ip);
96 down_write(&ip->i_rw_mutex);
98 error = gfs2_meta_inode_buffer(ip, &dibh);
102 if (ip->i_di.di_size) {
103 /* Get a free block, fill it with the stuffed data,
104 and write it out to disk */
107 block = gfs2_alloc_meta(ip);
109 error = gfs2_dir_get_buffer(ip, block, 1, &bh);
112 gfs2_buffer_copy_tail(bh,
113 sizeof(struct gfs2_meta_header),
114 dibh, sizeof(struct gfs2_dinode));
117 block = gfs2_alloc_data(ip);
119 error = unstuffer(ip, dibh, block, private);
125 /* Set up the pointer to the new block */
127 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
129 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
131 if (ip->i_di.di_size) {
132 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
133 ip->i_di.di_blocks++;
136 ip->i_di.di_height = 1;
138 gfs2_dinode_out(&ip->i_di, dibh->b_data);
144 up_write(&ip->i_rw_mutex);
150 * calc_tree_height - Calculate the height of a metadata tree
151 * @ip: The GFS2 inode
152 * @size: The proposed size of the file
154 * Work out how tall a metadata tree needs to be in order to accommodate a
155 * file of a particular size. If size is less than the current size of
156 * the inode, then the current size of the inode is used instead of the
159 * Returns: the height the tree should be
162 static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
164 struct gfs2_sbd *sdp = ip->i_sbd;
166 unsigned int max, height;
168 if (ip->i_di.di_size > size)
169 size = ip->i_di.di_size;
171 if (gfs2_is_dir(ip)) {
172 arr = sdp->sd_jheightsize;
173 max = sdp->sd_max_jheight;
175 arr = sdp->sd_heightsize;
176 max = sdp->sd_max_height;
179 for (height = 0; height < max; height++)
180 if (arr[height] >= size)
187 * build_height - Build a metadata tree of the requested height
188 * @ip: The GFS2 inode
189 * @height: The height to build to
191 * This routine makes sure that the metadata tree is tall enough to hold
192 * "size" bytes of data.
197 static int build_height(struct gfs2_inode *ip, int height)
199 struct gfs2_sbd *sdp = ip->i_sbd;
200 struct buffer_head *bh, *dibh;
201 uint64_t block = 0, *bp;
206 while (ip->i_di.di_height < height) {
207 error = gfs2_meta_inode_buffer(ip, &dibh);
212 bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
213 for (x = 0; x < sdp->sd_diptrs; x++, bp++)
220 /* Get a new block, fill it with the old direct
221 pointers, and write it out */
223 block = gfs2_alloc_meta(ip);
225 bh = gfs2_meta_new(ip->i_gl, block);
226 gfs2_trans_add_bh(ip->i_gl, bh, 1);
227 gfs2_metatype_set(bh,
230 gfs2_buffer_copy_tail(bh,
231 sizeof(struct gfs2_meta_header),
232 dibh, sizeof(struct gfs2_dinode));
237 /* Set up the new direct pointer and write it out to disk */
239 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
241 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
244 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) = cpu_to_be64(block);
245 ip->i_di.di_blocks++;
248 ip->i_di.di_height++;
250 gfs2_dinode_out(&ip->i_di, dibh->b_data);
258 * find_metapath - Find path through the metadata tree
259 * @ip: The inode pointer
260 * @mp: The metapath to return the result in
261 * @block: The disk block to look up
263 * This routine returns a struct metapath structure that defines a path
264 * through the metadata of inode "ip" to get to block "block".
267 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
268 * filesystem with a blocksize of 4096.
270 * find_metapath() would return a struct metapath structure set to:
271 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
272 * and mp_list[2] = 165.
274 * That means that in order to get to the block containing the byte at
275 * offset 101342453, we would load the indirect block pointed to by pointer
276 * 0 in the dinode. We would then load the indirect block pointed to by
277 * pointer 48 in that indirect block. We would then load the data block
278 * pointed to by pointer 165 in that indirect block.
280 * ----------------------------------------
285 * ----------------------------------------
289 * ----------------------------------------
293 * |0 5 6 7 8 9 0 1 2|
294 * ----------------------------------------
298 * ----------------------------------------
303 * ----------------------------------------
307 * ----------------------------------------
308 * | Data block containing offset |
312 * ----------------------------------------
316 static void find_metapath(struct gfs2_inode *ip, uint64_t block, struct metapath *mp)
318 struct gfs2_sbd *sdp = ip->i_sbd;
322 for (i = ip->i_di.di_height; i--;)
323 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
328 * metapointer - Return pointer to start of metadata in a buffer
330 * @height: The metadata height (0 = dinode)
333 * Return a pointer to the block number of the next height of the metadata
334 * tree given a buffer containing the pointer to the current height of the
338 static inline uint64_t *metapointer(struct buffer_head *bh,
339 unsigned int height, struct metapath *mp)
341 unsigned int head_size = (height > 0) ?
342 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
344 return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
348 * lookup_block - Get the next metadata block in metadata tree
349 * @ip: The GFS2 inode
350 * @bh: Buffer containing the pointers to metadata blocks
351 * @height: The height of the tree (0 = dinode)
353 * @create: Non-zero if we may create a new meatdata block
354 * @new: Used to indicate if we did create a new metadata block
355 * @block: the returned disk block number
357 * Given a metatree, complete to a particular height, checks to see if the next
358 * height of the tree exists. If not the next height of the tree is created.
359 * The block number of the next height of the metadata tree is returned.
363 static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
364 unsigned int height, struct metapath *mp, int create,
365 int *new, uint64_t *block)
367 uint64_t *ptr = metapointer(bh, height, mp);
370 *block = be64_to_cpu(*ptr);
379 if (height == ip->i_di.di_height - 1 &&
381 *block = gfs2_alloc_data(ip);
383 *block = gfs2_alloc_meta(ip);
385 gfs2_trans_add_bh(ip->i_gl, bh, 1);
387 *ptr = cpu_to_be64(*block);
388 ip->i_di.di_blocks++;
394 * gfs2_block_map - Map a block from an inode to a disk block
395 * @ip: The GFS2 inode
396 * @lblock: The logical block number
397 * @new: Value/Result argument (1 = may create/did create new blocks)
398 * @dblock: the disk block number of the start of an extent
399 * @extlen: the size of the extent
401 * Find the block number on the current device which corresponds to an
402 * inode's block. If the block had to be created, "new" will be set.
407 int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
408 uint64_t *dblock, uint32_t *extlen)
410 struct gfs2_sbd *sdp = ip->i_sbd;
411 struct buffer_head *bh;
416 unsigned int end_of_metadata;
426 down_write(&ip->i_rw_mutex);
428 down_read(&ip->i_rw_mutex);
430 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
433 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
435 height = calc_tree_height(ip, (lblock + 1) * bsize);
436 if (ip->i_di.di_height < height) {
440 error = build_height(ip, height);
445 find_metapath(ip, lblock, &mp);
446 end_of_metadata = ip->i_di.di_height - 1;
448 error = gfs2_meta_inode_buffer(ip, &bh);
452 for (x = 0; x < end_of_metadata; x++) {
453 lookup_block(ip, bh, x, &mp, create, new, dblock);
458 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
463 lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
465 if (extlen && *dblock) {
473 nptrs = (end_of_metadata) ? sdp->sd_inptrs :
476 while (++mp.mp_list[end_of_metadata] < nptrs) {
477 lookup_block(ip, bh, end_of_metadata, &mp,
478 0, &tmp_new, &tmp_dblock);
480 if (*dblock + *extlen != tmp_dblock)
491 error = gfs2_meta_inode_buffer(ip, &bh);
493 gfs2_trans_add_bh(ip->i_gl, bh, 1);
494 gfs2_dinode_out(&ip->i_di, bh->b_data);
501 up_write(&ip->i_rw_mutex);
503 up_read(&ip->i_rw_mutex);
509 * recursive_scan - recursively scan through the end of a file
511 * @dibh: the dinode buffer
512 * @mp: the path through the metadata to the point to start
513 * @height: the height the recursion is at
514 * @block: the indirect block to look at
515 * @first: 1 if this is the first block
516 * @bc: the call to make for each piece of metadata
517 * @data: data opaque to this function to pass to @bc
519 * When this is first called @height and @block should be zero and
520 * @first should be 1.
525 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
526 struct metapath *mp, unsigned int height,
527 uint64_t block, int first, block_call_t bc,
530 struct gfs2_sbd *sdp = ip->i_sbd;
531 struct buffer_head *bh = NULL;
532 uint64_t *top, *bottom;
535 int mh_size = sizeof(struct gfs2_meta_header);
538 error = gfs2_meta_inode_buffer(ip, &bh);
543 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
545 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
548 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
552 top = (uint64_t *)(bh->b_data + mh_size) +
553 ((first) ? mp->mp_list[height] : 0);
555 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
558 error = bc(ip, dibh, bh, top, bottom, height, data);
562 if (height < ip->i_di.di_height - 1)
563 for (; top < bottom; top++, first = 0) {
567 bn = be64_to_cpu(*top);
569 error = recursive_scan(ip, dibh, mp, height + 1, bn,
582 * do_strip - Look for a layer a particular layer of the file and strip it off
584 * @dibh: the dinode buffer
585 * @bh: A buffer of pointers
586 * @top: The first pointer in the buffer
587 * @bottom: One more than the last pointer
588 * @height: the height this buffer is at
589 * @data: a pointer to a struct strip_mine
594 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
595 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
596 unsigned int height, void *data)
598 struct strip_mine *sm = (struct strip_mine *)data;
599 struct gfs2_sbd *sdp = ip->i_sbd;
600 struct gfs2_rgrp_list rlist;
604 unsigned int rg_blocks = 0;
606 unsigned int revokes = 0;
613 if (height != sm->sm_height)
621 metadata = (height != ip->i_di.di_height - 1);
623 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
625 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
629 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
633 for (p = top; p < bottom; p++) {
637 bn = be64_to_cpu(*p);
639 if (bstart + blen == bn)
643 gfs2_rlist_add(sdp, &rlist, bstart);
651 gfs2_rlist_add(sdp, &rlist, bstart);
653 goto out; /* Nothing to do */
655 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
657 for (x = 0; x < rlist.rl_rgrps; x++) {
658 struct gfs2_rgrpd *rgd;
659 rgd = get_gl2rgd(rlist.rl_ghs[x].gh_gl);
660 rg_blocks += rgd->rd_ri.ri_length;
663 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
667 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
668 RES_INDIRECT + RES_STATFS + RES_QUOTA,
673 down_write(&ip->i_rw_mutex);
675 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
676 gfs2_trans_add_bh(ip->i_gl, bh, 1);
681 for (p = top; p < bottom; p++) {
685 bn = be64_to_cpu(*p);
687 if (bstart + blen == bn)
692 gfs2_free_meta(ip, bstart, blen);
694 gfs2_free_data(ip, bstart, blen);
702 if (!ip->i_di.di_blocks)
703 gfs2_consist_inode(ip);
704 ip->i_di.di_blocks--;
708 gfs2_free_meta(ip, bstart, blen);
710 gfs2_free_data(ip, bstart, blen);
713 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
715 gfs2_dinode_out(&ip->i_di, dibh->b_data);
717 up_write(&ip->i_rw_mutex);
722 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
725 gfs2_rlist_free(&rlist);
728 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
734 * do_grow - Make a file look bigger than it is
736 * @size: the size to set the file to
738 * Called with an exclusive lock on @ip.
743 static int do_grow(struct gfs2_inode *ip, uint64_t size)
745 struct gfs2_sbd *sdp = ip->i_sbd;
746 struct gfs2_alloc *al;
747 struct buffer_head *dibh;
751 al = gfs2_alloc_get(ip);
753 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
757 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
761 al->al_requested = sdp->sd_max_height + RES_DATA;
763 error = gfs2_inplace_reserve(ip);
767 error = gfs2_trans_begin(sdp,
768 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
769 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
773 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
774 if (gfs2_is_stuffed(ip)) {
775 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
781 h = calc_tree_height(ip, size);
782 if (ip->i_di.di_height < h) {
783 down_write(&ip->i_rw_mutex);
784 error = build_height(ip, h);
785 up_write(&ip->i_rw_mutex);
791 ip->i_di.di_size = size;
792 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
794 error = gfs2_meta_inode_buffer(ip, &dibh);
798 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
799 gfs2_dinode_out(&ip->i_di, dibh->b_data);
806 gfs2_inplace_release(ip);
809 gfs2_quota_unlock(ip);
817 static int trunc_start(struct gfs2_inode *ip, uint64_t size)
819 struct gfs2_sbd *sdp = ip->i_sbd;
820 struct buffer_head *dibh;
821 int journaled = gfs2_is_jdata(ip);
824 error = gfs2_trans_begin(sdp,
825 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
829 error = gfs2_meta_inode_buffer(ip, &dibh);
833 if (gfs2_is_stuffed(ip)) {
834 ip->i_di.di_size = size;
835 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
836 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
837 gfs2_dinode_out(&ip->i_di, dibh->b_data);
838 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
842 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
843 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
846 ip->i_di.di_size = size;
847 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
848 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
849 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
850 gfs2_dinode_out(&ip->i_di, dibh->b_data);
862 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
864 unsigned int height = ip->i_di.di_height;
872 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
874 find_metapath(ip, lblock, &mp);
877 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
882 struct strip_mine sm;
883 sm.sm_first = !!size;
884 sm.sm_height = height;
886 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
891 gfs2_quota_unhold(ip);
898 static int trunc_end(struct gfs2_inode *ip)
900 struct gfs2_sbd *sdp = ip->i_sbd;
901 struct buffer_head *dibh;
904 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
908 down_write(&ip->i_rw_mutex);
910 error = gfs2_meta_inode_buffer(ip, &dibh);
914 if (!ip->i_di.di_size) {
915 ip->i_di.di_height = 0;
916 ip->i_di.di_goal_meta =
917 ip->i_di.di_goal_data =
919 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
921 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
922 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
924 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
925 gfs2_dinode_out(&ip->i_di, dibh->b_data);
929 up_write(&ip->i_rw_mutex);
937 * do_shrink - make a file smaller
939 * @size: the size to make the file
940 * @truncator: function to truncate the last partial block
942 * Called with an exclusive lock on @ip.
947 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
951 error = trunc_start(ip, size);
957 error = trunc_dealloc(ip, size);
959 error = trunc_end(ip);
965 * gfs2_truncatei - make a file a given size
967 * @size: the size to make the file
968 * @truncator: function to truncate the last partial block
970 * The file size can grow, shrink, or stay the same size.
975 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
979 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
982 if (size > ip->i_di.di_size)
983 error = do_grow(ip, size);
985 error = do_shrink(ip, size);
990 int gfs2_truncatei_resume(struct gfs2_inode *ip)
993 error = trunc_dealloc(ip, ip->i_di.di_size);
995 error = trunc_end(ip);
999 int gfs2_file_dealloc(struct gfs2_inode *ip)
1001 return trunc_dealloc(ip, 0);
1005 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1007 * @len: the number of bytes to be written to the file
1008 * @data_blocks: returns the number of data blocks required
1009 * @ind_blocks: returns the number of indirect blocks required
1013 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1014 unsigned int *data_blocks, unsigned int *ind_blocks)
1016 struct gfs2_sbd *sdp = ip->i_sbd;
1019 if (gfs2_is_dir(ip)) {
1020 *data_blocks = DIV_RU(len, sdp->sd_jbsize) + 2;
1021 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1023 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1024 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1027 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1028 tmp = DIV_RU(tmp, sdp->sd_inptrs);
1034 * gfs2_write_alloc_required - figure out if a write will require an allocation
1035 * @ip: the file being written to
1036 * @offset: the offset to write to
1037 * @len: the number of bytes being written
1038 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1043 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1044 unsigned int len, int *alloc_required)
1046 struct gfs2_sbd *sdp = ip->i_sbd;
1047 uint64_t lblock, lblock_stop, dblock;
1052 *alloc_required = 0;
1057 if (gfs2_is_stuffed(ip)) {
1059 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1060 *alloc_required = 1;
1064 if (gfs2_is_dir(ip)) {
1065 unsigned int bsize = sdp->sd_jbsize;
1067 do_div(lblock, bsize);
1068 lblock_stop = offset + len + bsize - 1;
1069 do_div(lblock_stop, bsize);
1071 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1072 lblock = offset >> shift;
1073 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1076 for (; lblock < lblock_stop; lblock += extlen) {
1077 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1082 *alloc_required = 1;