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 <linux/gfs2_ondisk.h>
16 #include <linux/crc32.h>
17 #include <asm/semaphore.h>
20 #include "lm_interface.h"
33 /* This doesn't need to be that large as max 64 bit pointers in a 4k
34 * block is 512, so __u16 is fine for that. It saves stack space to
38 __u16 mp_list[GFS2_MAX_META_HEIGHT];
41 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
42 struct buffer_head *bh, uint64_t *top,
43 uint64_t *bottom, unsigned int height,
48 unsigned int sm_height;
52 * @gfs2_unstuffer_sync - Synchronously unstuff a dinode
58 * Cheat and use a metadata buffer instead of a data page.
63 int gfs2_unstuffer_sync(struct gfs2_inode *ip, struct buffer_head *dibh,
64 uint64_t block, void *private)
66 struct buffer_head *bh;
69 bh = gfs2_meta_new(ip->i_gl, block);
71 gfs2_buffer_copy_tail(bh, 0, dibh, sizeof(struct gfs2_dinode));
74 error = sync_dirty_buffer(bh);
83 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
84 * @ip: The GFS2 inode to unstuff
85 * @unstuffer: the routine that handles unstuffing a non-zero length file
86 * @private: private data for the unstuffer
88 * This routine unstuffs a dinode and returns it to a "normal" state such
89 * that the height can be grown in the traditional way.
94 int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
97 struct buffer_head *bh, *dibh;
99 int isdir = gfs2_is_dir(ip);
102 down_write(&ip->i_rw_mutex);
104 error = gfs2_meta_inode_buffer(ip, &dibh);
108 if (ip->i_di.di_size) {
109 /* Get a free block, fill it with the stuffed data,
110 and write it out to disk */
113 block = gfs2_alloc_meta(ip);
115 error = gfs2_dir_get_new_buffer(ip, block, &bh);
118 gfs2_buffer_copy_tail(bh,
119 sizeof(struct gfs2_meta_header),
120 dibh, sizeof(struct gfs2_dinode));
123 block = gfs2_alloc_data(ip);
125 error = unstuffer(ip, dibh, block, private);
131 /* Set up the pointer to the new block */
133 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
135 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
137 if (ip->i_di.di_size) {
138 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
140 ip->i_di.di_blocks++;
143 ip->i_di.di_height = 1;
145 gfs2_dinode_out(&ip->i_di, dibh->b_data);
151 up_write(&ip->i_rw_mutex);
157 * calc_tree_height - Calculate the height of a metadata tree
158 * @ip: The GFS2 inode
159 * @size: The proposed size of the file
161 * Work out how tall a metadata tree needs to be in order to accommodate a
162 * file of a particular size. If size is less than the current size of
163 * the inode, then the current size of the inode is used instead of the
166 * Returns: the height the tree should be
169 static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
171 struct gfs2_sbd *sdp = ip->i_sbd;
173 unsigned int max, height;
175 if (ip->i_di.di_size > size)
176 size = ip->i_di.di_size;
178 if (gfs2_is_dir(ip)) {
179 arr = sdp->sd_jheightsize;
180 max = sdp->sd_max_jheight;
182 arr = sdp->sd_heightsize;
183 max = sdp->sd_max_height;
186 for (height = 0; height < max; height++)
187 if (arr[height] >= size)
194 * build_height - Build a metadata tree of the requested height
195 * @ip: The GFS2 inode
196 * @height: The height to build to
198 * This routine makes sure that the metadata tree is tall enough to hold
199 * "size" bytes of data.
204 static int build_height(struct gfs2_inode *ip, int height)
206 struct gfs2_sbd *sdp = ip->i_sbd;
207 struct buffer_head *bh, *dibh;
208 uint64_t block = 0, *bp;
213 while (ip->i_di.di_height < height) {
214 error = gfs2_meta_inode_buffer(ip, &dibh);
219 bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
220 for (x = 0; x < sdp->sd_diptrs; x++, bp++)
227 /* Get a new block, fill it with the old direct
228 pointers, and write it out */
230 block = gfs2_alloc_meta(ip);
232 bh = gfs2_meta_new(ip->i_gl, block);
233 gfs2_trans_add_bh(ip->i_gl, bh, 1);
234 gfs2_metatype_set(bh,
237 gfs2_buffer_copy_tail(bh,
238 sizeof(struct gfs2_meta_header),
239 dibh, sizeof(struct gfs2_dinode));
244 /* Set up the new direct pointer and write it out to disk */
246 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
248 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
251 *(uint64_t *)(dibh->b_data +
252 sizeof(struct gfs2_dinode)) =
254 ip->i_di.di_blocks++;
257 ip->i_di.di_height++;
259 gfs2_dinode_out(&ip->i_di, dibh->b_data);
267 * find_metapath - Find path through the metadata tree
268 * @ip: The inode pointer
269 * @mp: The metapath to return the result in
270 * @block: The disk block to look up
272 * This routine returns a struct metapath structure that defines a path
273 * through the metadata of inode "ip" to get to block "block".
276 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
277 * filesystem with a blocksize of 4096.
279 * find_metapath() would return a struct metapath structure set to:
280 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
281 * and mp_list[2] = 165.
283 * That means that in order to get to the block containing the byte at
284 * offset 101342453, we would load the indirect block pointed to by pointer
285 * 0 in the dinode. We would then load the indirect block pointed to by
286 * pointer 48 in that indirect block. We would then load the data block
287 * pointed to by pointer 165 in that indirect block.
289 * ----------------------------------------
294 * ----------------------------------------
298 * ----------------------------------------
302 * |0 5 6 7 8 9 0 1 2|
303 * ----------------------------------------
307 * ----------------------------------------
312 * ----------------------------------------
316 * ----------------------------------------
317 * | Data block containing offset |
321 * ----------------------------------------
325 static void find_metapath(struct gfs2_inode *ip, uint64_t block,
328 struct gfs2_sbd *sdp = ip->i_sbd;
332 for (i = ip->i_di.di_height; i--;)
333 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
338 * metapointer - Return pointer to start of metadata in a buffer
340 * @height: The metadata height (0 = dinode)
343 * Return a pointer to the block number of the next height of the metadata
344 * tree given a buffer containing the pointer to the current height of the
348 static inline uint64_t *metapointer(struct buffer_head *bh,
349 unsigned int height, struct metapath *mp)
351 unsigned int head_size = (height > 0) ?
352 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
354 return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
358 * lookup_block - Get the next metadata block in metadata tree
359 * @ip: The GFS2 inode
360 * @bh: Buffer containing the pointers to metadata blocks
361 * @height: The height of the tree (0 = dinode)
363 * @create: Non-zero if we may create a new meatdata block
364 * @new: Used to indicate if we did create a new metadata block
365 * @block: the returned disk block number
367 * Given a metatree, complete to a particular height, checks to see if the next
368 * height of the tree exists. If not the next height of the tree is created.
369 * The block number of the next height of the metadata tree is returned.
373 static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
374 unsigned int height, struct metapath *mp, int create,
375 int *new, uint64_t *block)
377 uint64_t *ptr = metapointer(bh, height, mp);
380 *block = be64_to_cpu(*ptr);
389 if (height == ip->i_di.di_height - 1 &&
391 *block = gfs2_alloc_data(ip);
393 *block = gfs2_alloc_meta(ip);
395 gfs2_trans_add_bh(ip->i_gl, bh, 1);
397 *ptr = cpu_to_be64(*block);
398 ip->i_di.di_blocks++;
404 * gfs2_block_map - Map a block from an inode to a disk block
405 * @ip: The GFS2 inode
406 * @lblock: The logical block number
407 * @new: Value/Result argument (1 = may create/did create new blocks)
408 * @dblock: the disk block number of the start of an extent
409 * @extlen: the size of the extent
411 * Find the block number on the current device which corresponds to an
412 * inode's block. If the block had to be created, "new" will be set.
417 int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
418 uint64_t *dblock, uint32_t *extlen)
420 struct gfs2_sbd *sdp = ip->i_sbd;
421 struct buffer_head *bh;
426 unsigned int end_of_metadata;
436 down_write(&ip->i_rw_mutex);
438 down_read(&ip->i_rw_mutex);
440 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
443 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
445 height = calc_tree_height(ip, (lblock + 1) * bsize);
446 if (ip->i_di.di_height < height) {
450 error = build_height(ip, height);
455 find_metapath(ip, lblock, &mp);
456 end_of_metadata = ip->i_di.di_height - 1;
458 error = gfs2_meta_inode_buffer(ip, &bh);
462 for (x = 0; x < end_of_metadata; x++) {
463 lookup_block(ip, bh, x, &mp, create, new, dblock);
468 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
473 lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
475 if (extlen && *dblock) {
483 nptrs = (end_of_metadata) ? sdp->sd_inptrs :
486 while (++mp.mp_list[end_of_metadata] < nptrs) {
487 lookup_block(ip, bh, end_of_metadata, &mp,
488 0, &tmp_new, &tmp_dblock);
490 if (*dblock + *extlen != tmp_dblock)
501 error = gfs2_meta_inode_buffer(ip, &bh);
503 gfs2_trans_add_bh(ip->i_gl, bh, 1);
504 gfs2_dinode_out(&ip->i_di, bh->b_data);
511 up_write(&ip->i_rw_mutex);
513 up_read(&ip->i_rw_mutex);
519 * recursive_scan - recursively scan through the end of a file
521 * @dibh: the dinode buffer
522 * @mp: the path through the metadata to the point to start
523 * @height: the height the recursion is at
524 * @block: the indirect block to look at
525 * @first: 1 if this is the first block
526 * @bc: the call to make for each piece of metadata
527 * @data: data opaque to this function to pass to @bc
529 * When this is first called @height and @block should be zero and
530 * @first should be 1.
535 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
536 struct metapath *mp, unsigned int height,
537 uint64_t block, int first, block_call_t bc,
540 struct gfs2_sbd *sdp = ip->i_sbd;
541 struct buffer_head *bh = NULL;
542 uint64_t *top, *bottom;
545 int mh_size = sizeof(struct gfs2_meta_header);
548 error = gfs2_meta_inode_buffer(ip, &bh);
553 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
555 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
558 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
562 top = (uint64_t *)(bh->b_data + mh_size) +
563 ((first) ? mp->mp_list[height] : 0);
565 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
568 error = bc(ip, dibh, bh, top, bottom, height, data);
572 if (height < ip->i_di.di_height - 1)
573 for (; top < bottom; top++, first = 0) {
577 bn = be64_to_cpu(*top);
579 error = recursive_scan(ip, dibh, mp, height + 1, bn,
592 * do_strip - Look for a layer a particular layer of the file and strip it off
594 * @dibh: the dinode buffer
595 * @bh: A buffer of pointers
596 * @top: The first pointer in the buffer
597 * @bottom: One more than the last pointer
598 * @height: the height this buffer is at
599 * @data: a pointer to a struct strip_mine
604 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
605 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
606 unsigned int height, void *data)
608 struct strip_mine *sm = (struct strip_mine *)data;
609 struct gfs2_sbd *sdp = ip->i_sbd;
610 struct gfs2_rgrp_list rlist;
614 unsigned int rg_blocks = 0;
616 unsigned int revokes = 0;
623 if (height != sm->sm_height)
631 metadata = (height != ip->i_di.di_height - 1);
633 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
635 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
639 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
643 for (p = top; p < bottom; p++) {
647 bn = be64_to_cpu(*p);
649 if (bstart + blen == bn)
653 gfs2_rlist_add(sdp, &rlist, bstart);
661 gfs2_rlist_add(sdp, &rlist, bstart);
663 goto out; /* Nothing to do */
665 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
667 for (x = 0; x < rlist.rl_rgrps; x++) {
668 struct gfs2_rgrpd *rgd;
669 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
670 rg_blocks += rgd->rd_ri.ri_length;
673 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
677 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
678 RES_INDIRECT + RES_STATFS + RES_QUOTA,
683 down_write(&ip->i_rw_mutex);
685 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
686 gfs2_trans_add_bh(ip->i_gl, bh, 1);
691 for (p = top; p < bottom; p++) {
695 bn = be64_to_cpu(*p);
697 if (bstart + blen == bn)
702 gfs2_free_meta(ip, bstart, blen);
704 gfs2_free_data(ip, bstart, blen);
712 if (!ip->i_di.di_blocks)
713 gfs2_consist_inode(ip);
714 ip->i_di.di_blocks--;
718 gfs2_free_meta(ip, bstart, blen);
720 gfs2_free_data(ip, bstart, blen);
723 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
725 gfs2_dinode_out(&ip->i_di, dibh->b_data);
727 up_write(&ip->i_rw_mutex);
732 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
735 gfs2_rlist_free(&rlist);
738 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
744 * do_grow - Make a file look bigger than it is
746 * @size: the size to set the file to
748 * Called with an exclusive lock on @ip.
753 static int do_grow(struct gfs2_inode *ip, uint64_t size)
755 struct gfs2_sbd *sdp = ip->i_sbd;
756 struct gfs2_alloc *al;
757 struct buffer_head *dibh;
761 al = gfs2_alloc_get(ip);
763 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
767 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
771 al->al_requested = sdp->sd_max_height + RES_DATA;
773 error = gfs2_inplace_reserve(ip);
777 error = gfs2_trans_begin(sdp,
778 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
779 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
783 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
784 if (gfs2_is_stuffed(ip)) {
785 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
791 h = calc_tree_height(ip, size);
792 if (ip->i_di.di_height < h) {
793 down_write(&ip->i_rw_mutex);
794 error = build_height(ip, h);
795 up_write(&ip->i_rw_mutex);
801 ip->i_di.di_size = size;
802 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
804 error = gfs2_meta_inode_buffer(ip, &dibh);
808 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
809 gfs2_dinode_out(&ip->i_di, dibh->b_data);
816 gfs2_inplace_release(ip);
819 gfs2_quota_unlock(ip);
827 static int trunc_start(struct gfs2_inode *ip, uint64_t size)
829 struct gfs2_sbd *sdp = ip->i_sbd;
830 struct buffer_head *dibh;
831 int journaled = gfs2_is_jdata(ip);
834 error = gfs2_trans_begin(sdp,
835 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
839 error = gfs2_meta_inode_buffer(ip, &dibh);
843 if (gfs2_is_stuffed(ip)) {
844 ip->i_di.di_size = size;
845 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
846 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
847 gfs2_dinode_out(&ip->i_di, dibh->b_data);
848 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
852 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
853 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
856 ip->i_di.di_size = size;
857 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
858 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
859 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
860 gfs2_dinode_out(&ip->i_di, dibh->b_data);
872 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
874 unsigned int height = ip->i_di.di_height;
882 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
884 find_metapath(ip, lblock, &mp);
887 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
892 struct strip_mine sm;
893 sm.sm_first = !!size;
894 sm.sm_height = height;
896 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
901 gfs2_quota_unhold(ip);
908 static int trunc_end(struct gfs2_inode *ip)
910 struct gfs2_sbd *sdp = ip->i_sbd;
911 struct buffer_head *dibh;
914 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
918 down_write(&ip->i_rw_mutex);
920 error = gfs2_meta_inode_buffer(ip, &dibh);
924 if (!ip->i_di.di_size) {
925 ip->i_di.di_height = 0;
926 ip->i_di.di_goal_meta =
927 ip->i_di.di_goal_data =
929 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
931 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
932 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
934 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
935 gfs2_dinode_out(&ip->i_di, dibh->b_data);
939 up_write(&ip->i_rw_mutex);
947 * do_shrink - make a file smaller
949 * @size: the size to make the file
950 * @truncator: function to truncate the last partial block
952 * Called with an exclusive lock on @ip.
957 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
961 error = trunc_start(ip, size);
967 error = trunc_dealloc(ip, size);
969 error = trunc_end(ip);
975 * gfs2_truncatei - make a file a given size
977 * @size: the size to make the file
978 * @truncator: function to truncate the last partial block
980 * The file size can grow, shrink, or stay the same size.
985 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
989 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
992 if (size > ip->i_di.di_size)
993 error = do_grow(ip, size);
995 error = do_shrink(ip, size);
1000 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1003 error = trunc_dealloc(ip, ip->i_di.di_size);
1005 error = trunc_end(ip);
1009 int gfs2_file_dealloc(struct gfs2_inode *ip)
1011 return trunc_dealloc(ip, 0);
1015 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1017 * @len: the number of bytes to be written to the file
1018 * @data_blocks: returns the number of data blocks required
1019 * @ind_blocks: returns the number of indirect blocks required
1023 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1024 unsigned int *data_blocks, unsigned int *ind_blocks)
1026 struct gfs2_sbd *sdp = ip->i_sbd;
1029 if (gfs2_is_dir(ip)) {
1030 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1031 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1033 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1034 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1037 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1038 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1044 * gfs2_write_alloc_required - figure out if a write will require an allocation
1045 * @ip: the file being written to
1046 * @offset: the offset to write to
1047 * @len: the number of bytes being written
1048 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1053 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1054 unsigned int len, int *alloc_required)
1056 struct gfs2_sbd *sdp = ip->i_sbd;
1057 uint64_t lblock, lblock_stop, dblock;
1062 *alloc_required = 0;
1067 if (gfs2_is_stuffed(ip)) {
1069 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1070 *alloc_required = 1;
1074 if (gfs2_is_dir(ip)) {
1075 unsigned int bsize = sdp->sd_jbsize;
1077 do_div(lblock, bsize);
1078 lblock_stop = offset + len + bsize - 1;
1079 do_div(lblock_stop, bsize);
1081 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1082 lblock = offset >> shift;
1083 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1086 for (; lblock < lblock_stop; lblock += extlen) {
1087 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1092 *alloc_required = 1;