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_unstuff_dinode - Unstuff a dinode when the data has grown too big
53 * @ip: The GFS2 inode to unstuff
54 * @unstuffer: the routine that handles unstuffing a non-zero length file
55 * @private: private data for the unstuffer
57 * This routine unstuffs a dinode and returns it to a "normal" state such
58 * that the height can be grown in the traditional way.
63 int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
66 struct buffer_head *bh, *dibh;
68 int isdir = gfs2_is_dir(ip);
71 down_write(&ip->i_rw_mutex);
73 error = gfs2_meta_inode_buffer(ip, &dibh);
77 if (ip->i_di.di_size) {
78 /* Get a free block, fill it with the stuffed data,
79 and write it out to disk */
82 block = gfs2_alloc_meta(ip);
84 error = gfs2_dir_get_new_buffer(ip, block, &bh);
87 gfs2_buffer_copy_tail(bh,
88 sizeof(struct gfs2_meta_header),
89 dibh, sizeof(struct gfs2_dinode));
92 block = gfs2_alloc_data(ip);
94 error = unstuffer(ip, dibh, block, private);
100 /* Set up the pointer to the new block */
102 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
104 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
106 if (ip->i_di.di_size) {
107 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
109 ip->i_di.di_blocks++;
112 ip->i_di.di_height = 1;
114 gfs2_dinode_out(&ip->i_di, dibh->b_data);
120 up_write(&ip->i_rw_mutex);
126 * calc_tree_height - Calculate the height of a metadata tree
127 * @ip: The GFS2 inode
128 * @size: The proposed size of the file
130 * Work out how tall a metadata tree needs to be in order to accommodate a
131 * file of a particular size. If size is less than the current size of
132 * the inode, then the current size of the inode is used instead of the
135 * Returns: the height the tree should be
138 static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
140 struct gfs2_sbd *sdp = ip->i_sbd;
142 unsigned int max, height;
144 if (ip->i_di.di_size > size)
145 size = ip->i_di.di_size;
147 if (gfs2_is_dir(ip)) {
148 arr = sdp->sd_jheightsize;
149 max = sdp->sd_max_jheight;
151 arr = sdp->sd_heightsize;
152 max = sdp->sd_max_height;
155 for (height = 0; height < max; height++)
156 if (arr[height] >= size)
163 * build_height - Build a metadata tree of the requested height
164 * @ip: The GFS2 inode
165 * @height: The height to build to
167 * This routine makes sure that the metadata tree is tall enough to hold
168 * "size" bytes of data.
173 static int build_height(struct gfs2_inode *ip, int height)
175 struct gfs2_sbd *sdp = ip->i_sbd;
176 struct buffer_head *bh, *dibh;
177 uint64_t block = 0, *bp;
182 while (ip->i_di.di_height < height) {
183 error = gfs2_meta_inode_buffer(ip, &dibh);
188 bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
189 for (x = 0; x < sdp->sd_diptrs; x++, bp++)
196 /* Get a new block, fill it with the old direct
197 pointers, and write it out */
199 block = gfs2_alloc_meta(ip);
201 bh = gfs2_meta_new(ip->i_gl, block);
202 gfs2_trans_add_bh(ip->i_gl, bh, 1);
203 gfs2_metatype_set(bh,
206 gfs2_buffer_copy_tail(bh,
207 sizeof(struct gfs2_meta_header),
208 dibh, sizeof(struct gfs2_dinode));
213 /* Set up the new direct pointer and write it out to disk */
215 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
217 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
220 *(uint64_t *)(dibh->b_data +
221 sizeof(struct gfs2_dinode)) =
223 ip->i_di.di_blocks++;
226 ip->i_di.di_height++;
228 gfs2_dinode_out(&ip->i_di, dibh->b_data);
236 * find_metapath - Find path through the metadata tree
237 * @ip: The inode pointer
238 * @mp: The metapath to return the result in
239 * @block: The disk block to look up
241 * This routine returns a struct metapath structure that defines a path
242 * through the metadata of inode "ip" to get to block "block".
245 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
246 * filesystem with a blocksize of 4096.
248 * find_metapath() would return a struct metapath structure set to:
249 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
250 * and mp_list[2] = 165.
252 * That means that in order to get to the block containing the byte at
253 * offset 101342453, we would load the indirect block pointed to by pointer
254 * 0 in the dinode. We would then load the indirect block pointed to by
255 * pointer 48 in that indirect block. We would then load the data block
256 * pointed to by pointer 165 in that indirect block.
258 * ----------------------------------------
263 * ----------------------------------------
267 * ----------------------------------------
271 * |0 5 6 7 8 9 0 1 2|
272 * ----------------------------------------
276 * ----------------------------------------
281 * ----------------------------------------
285 * ----------------------------------------
286 * | Data block containing offset |
290 * ----------------------------------------
294 static void find_metapath(struct gfs2_inode *ip, uint64_t block,
297 struct gfs2_sbd *sdp = ip->i_sbd;
301 for (i = ip->i_di.di_height; i--;)
302 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
307 * metapointer - Return pointer to start of metadata in a buffer
309 * @height: The metadata height (0 = dinode)
312 * Return a pointer to the block number of the next height of the metadata
313 * tree given a buffer containing the pointer to the current height of the
317 static inline uint64_t *metapointer(struct buffer_head *bh,
318 unsigned int height, struct metapath *mp)
320 unsigned int head_size = (height > 0) ?
321 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
323 return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
327 * lookup_block - Get the next metadata block in metadata tree
328 * @ip: The GFS2 inode
329 * @bh: Buffer containing the pointers to metadata blocks
330 * @height: The height of the tree (0 = dinode)
332 * @create: Non-zero if we may create a new meatdata block
333 * @new: Used to indicate if we did create a new metadata block
334 * @block: the returned disk block number
336 * Given a metatree, complete to a particular height, checks to see if the next
337 * height of the tree exists. If not the next height of the tree is created.
338 * The block number of the next height of the metadata tree is returned.
342 static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
343 unsigned int height, struct metapath *mp, int create,
344 int *new, uint64_t *block)
346 uint64_t *ptr = metapointer(bh, height, mp);
349 *block = be64_to_cpu(*ptr);
358 if (height == ip->i_di.di_height - 1 &&
360 *block = gfs2_alloc_data(ip);
362 *block = gfs2_alloc_meta(ip);
364 gfs2_trans_add_bh(ip->i_gl, bh, 1);
366 *ptr = cpu_to_be64(*block);
367 ip->i_di.di_blocks++;
373 * gfs2_block_map - Map a block from an inode to a disk block
374 * @ip: The GFS2 inode
375 * @lblock: The logical block number
376 * @new: Value/Result argument (1 = may create/did create new blocks)
377 * @dblock: the disk block number of the start of an extent
378 * @extlen: the size of the extent
380 * Find the block number on the current device which corresponds to an
381 * inode's block. If the block had to be created, "new" will be set.
386 int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
387 uint64_t *dblock, uint32_t *extlen)
389 struct gfs2_sbd *sdp = ip->i_sbd;
390 struct buffer_head *bh;
395 unsigned int end_of_metadata;
405 down_write(&ip->i_rw_mutex);
407 down_read(&ip->i_rw_mutex);
409 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
412 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
414 height = calc_tree_height(ip, (lblock + 1) * bsize);
415 if (ip->i_di.di_height < height) {
419 error = build_height(ip, height);
424 find_metapath(ip, lblock, &mp);
425 end_of_metadata = ip->i_di.di_height - 1;
427 error = gfs2_meta_inode_buffer(ip, &bh);
431 for (x = 0; x < end_of_metadata; x++) {
432 lookup_block(ip, bh, x, &mp, create, new, dblock);
437 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
442 lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
444 if (extlen && *dblock) {
452 nptrs = (end_of_metadata) ? sdp->sd_inptrs :
455 while (++mp.mp_list[end_of_metadata] < nptrs) {
456 lookup_block(ip, bh, end_of_metadata, &mp,
457 0, &tmp_new, &tmp_dblock);
459 if (*dblock + *extlen != tmp_dblock)
470 error = gfs2_meta_inode_buffer(ip, &bh);
472 gfs2_trans_add_bh(ip->i_gl, bh, 1);
473 gfs2_dinode_out(&ip->i_di, bh->b_data);
480 up_write(&ip->i_rw_mutex);
482 up_read(&ip->i_rw_mutex);
488 * recursive_scan - recursively scan through the end of a file
490 * @dibh: the dinode buffer
491 * @mp: the path through the metadata to the point to start
492 * @height: the height the recursion is at
493 * @block: the indirect block to look at
494 * @first: 1 if this is the first block
495 * @bc: the call to make for each piece of metadata
496 * @data: data opaque to this function to pass to @bc
498 * When this is first called @height and @block should be zero and
499 * @first should be 1.
504 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
505 struct metapath *mp, unsigned int height,
506 uint64_t block, int first, block_call_t bc,
509 struct gfs2_sbd *sdp = ip->i_sbd;
510 struct buffer_head *bh = NULL;
511 uint64_t *top, *bottom;
514 int mh_size = sizeof(struct gfs2_meta_header);
517 error = gfs2_meta_inode_buffer(ip, &bh);
522 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
524 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
527 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
531 top = (uint64_t *)(bh->b_data + mh_size) +
532 ((first) ? mp->mp_list[height] : 0);
534 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
537 error = bc(ip, dibh, bh, top, bottom, height, data);
541 if (height < ip->i_di.di_height - 1)
542 for (; top < bottom; top++, first = 0) {
546 bn = be64_to_cpu(*top);
548 error = recursive_scan(ip, dibh, mp, height + 1, bn,
561 * do_strip - Look for a layer a particular layer of the file and strip it off
563 * @dibh: the dinode buffer
564 * @bh: A buffer of pointers
565 * @top: The first pointer in the buffer
566 * @bottom: One more than the last pointer
567 * @height: the height this buffer is at
568 * @data: a pointer to a struct strip_mine
573 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
574 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
575 unsigned int height, void *data)
577 struct strip_mine *sm = (struct strip_mine *)data;
578 struct gfs2_sbd *sdp = ip->i_sbd;
579 struct gfs2_rgrp_list rlist;
583 unsigned int rg_blocks = 0;
585 unsigned int revokes = 0;
592 if (height != sm->sm_height)
600 metadata = (height != ip->i_di.di_height - 1);
602 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
604 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
608 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
612 for (p = top; p < bottom; p++) {
616 bn = be64_to_cpu(*p);
618 if (bstart + blen == bn)
622 gfs2_rlist_add(sdp, &rlist, bstart);
630 gfs2_rlist_add(sdp, &rlist, bstart);
632 goto out; /* Nothing to do */
634 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
636 for (x = 0; x < rlist.rl_rgrps; x++) {
637 struct gfs2_rgrpd *rgd;
638 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
639 rg_blocks += rgd->rd_ri.ri_length;
642 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
646 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
647 RES_INDIRECT + RES_STATFS + RES_QUOTA,
652 down_write(&ip->i_rw_mutex);
654 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
655 gfs2_trans_add_bh(ip->i_gl, bh, 1);
660 for (p = top; p < bottom; p++) {
664 bn = be64_to_cpu(*p);
666 if (bstart + blen == bn)
671 gfs2_free_meta(ip, bstart, blen);
673 gfs2_free_data(ip, bstart, blen);
681 if (!ip->i_di.di_blocks)
682 gfs2_consist_inode(ip);
683 ip->i_di.di_blocks--;
687 gfs2_free_meta(ip, bstart, blen);
689 gfs2_free_data(ip, bstart, blen);
692 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
694 gfs2_dinode_out(&ip->i_di, dibh->b_data);
696 up_write(&ip->i_rw_mutex);
701 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
704 gfs2_rlist_free(&rlist);
707 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
713 * do_grow - Make a file look bigger than it is
715 * @size: the size to set the file to
717 * Called with an exclusive lock on @ip.
722 static int do_grow(struct gfs2_inode *ip, uint64_t size)
724 struct gfs2_sbd *sdp = ip->i_sbd;
725 struct gfs2_alloc *al;
726 struct buffer_head *dibh;
730 al = gfs2_alloc_get(ip);
732 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
736 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
740 al->al_requested = sdp->sd_max_height + RES_DATA;
742 error = gfs2_inplace_reserve(ip);
746 error = gfs2_trans_begin(sdp,
747 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
748 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
752 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
753 if (gfs2_is_stuffed(ip)) {
754 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
760 h = calc_tree_height(ip, size);
761 if (ip->i_di.di_height < h) {
762 down_write(&ip->i_rw_mutex);
763 error = build_height(ip, h);
764 up_write(&ip->i_rw_mutex);
770 ip->i_di.di_size = size;
771 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
773 error = gfs2_meta_inode_buffer(ip, &dibh);
777 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
778 gfs2_dinode_out(&ip->i_di, dibh->b_data);
785 gfs2_inplace_release(ip);
788 gfs2_quota_unlock(ip);
796 static int trunc_start(struct gfs2_inode *ip, uint64_t size)
798 struct gfs2_sbd *sdp = ip->i_sbd;
799 struct buffer_head *dibh;
800 int journaled = gfs2_is_jdata(ip);
803 error = gfs2_trans_begin(sdp,
804 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
808 error = gfs2_meta_inode_buffer(ip, &dibh);
812 if (gfs2_is_stuffed(ip)) {
813 ip->i_di.di_size = size;
814 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
815 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
816 gfs2_dinode_out(&ip->i_di, dibh->b_data);
817 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
821 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
822 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
825 ip->i_di.di_size = size;
826 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
827 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
828 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
829 gfs2_dinode_out(&ip->i_di, dibh->b_data);
841 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
843 unsigned int height = ip->i_di.di_height;
851 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
853 find_metapath(ip, lblock, &mp);
856 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
861 struct strip_mine sm;
862 sm.sm_first = !!size;
863 sm.sm_height = height;
865 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
870 gfs2_quota_unhold(ip);
877 static int trunc_end(struct gfs2_inode *ip)
879 struct gfs2_sbd *sdp = ip->i_sbd;
880 struct buffer_head *dibh;
883 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
887 down_write(&ip->i_rw_mutex);
889 error = gfs2_meta_inode_buffer(ip, &dibh);
893 if (!ip->i_di.di_size) {
894 ip->i_di.di_height = 0;
895 ip->i_di.di_goal_meta =
896 ip->i_di.di_goal_data =
898 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
900 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
901 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
903 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
904 gfs2_dinode_out(&ip->i_di, dibh->b_data);
908 up_write(&ip->i_rw_mutex);
916 * do_shrink - make a file smaller
918 * @size: the size to make the file
919 * @truncator: function to truncate the last partial block
921 * Called with an exclusive lock on @ip.
926 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
930 error = trunc_start(ip, size);
936 error = trunc_dealloc(ip, size);
938 error = trunc_end(ip);
944 * gfs2_truncatei - make a file a given size
946 * @size: the size to make the file
947 * @truncator: function to truncate the last partial block
949 * The file size can grow, shrink, or stay the same size.
954 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
958 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
961 if (size > ip->i_di.di_size)
962 error = do_grow(ip, size);
964 error = do_shrink(ip, size);
969 int gfs2_truncatei_resume(struct gfs2_inode *ip)
972 error = trunc_dealloc(ip, ip->i_di.di_size);
974 error = trunc_end(ip);
978 int gfs2_file_dealloc(struct gfs2_inode *ip)
980 return trunc_dealloc(ip, 0);
984 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
986 * @len: the number of bytes to be written to the file
987 * @data_blocks: returns the number of data blocks required
988 * @ind_blocks: returns the number of indirect blocks required
992 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
993 unsigned int *data_blocks, unsigned int *ind_blocks)
995 struct gfs2_sbd *sdp = ip->i_sbd;
998 if (gfs2_is_dir(ip)) {
999 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1000 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1002 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1003 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1006 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1007 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1013 * gfs2_write_alloc_required - figure out if a write will require an allocation
1014 * @ip: the file being written to
1015 * @offset: the offset to write to
1016 * @len: the number of bytes being written
1017 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1022 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1023 unsigned int len, int *alloc_required)
1025 struct gfs2_sbd *sdp = ip->i_sbd;
1026 uint64_t lblock, lblock_stop, dblock;
1031 *alloc_required = 0;
1036 if (gfs2_is_stuffed(ip)) {
1038 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1039 *alloc_required = 1;
1043 if (gfs2_is_dir(ip)) {
1044 unsigned int bsize = sdp->sd_jbsize;
1046 do_div(lblock, bsize);
1047 lblock_stop = offset + len + bsize - 1;
1048 do_div(lblock_stop, bsize);
1050 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1051 lblock = offset >> shift;
1052 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1055 for (; lblock < lblock_stop; lblock += extlen) {
1056 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1061 *alloc_required = 1;