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)) =
134 ip->i_di.di_blocks++;
137 ip->i_di.di_height = 1;
139 gfs2_dinode_out(&ip->i_di, dibh->b_data);
145 up_write(&ip->i_rw_mutex);
151 * calc_tree_height - Calculate the height of a metadata tree
152 * @ip: The GFS2 inode
153 * @size: The proposed size of the file
155 * Work out how tall a metadata tree needs to be in order to accommodate a
156 * file of a particular size. If size is less than the current size of
157 * the inode, then the current size of the inode is used instead of the
160 * Returns: the height the tree should be
163 static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
165 struct gfs2_sbd *sdp = ip->i_sbd;
167 unsigned int max, height;
169 if (ip->i_di.di_size > size)
170 size = ip->i_di.di_size;
172 if (gfs2_is_dir(ip)) {
173 arr = sdp->sd_jheightsize;
174 max = sdp->sd_max_jheight;
176 arr = sdp->sd_heightsize;
177 max = sdp->sd_max_height;
180 for (height = 0; height < max; height++)
181 if (arr[height] >= size)
188 * build_height - Build a metadata tree of the requested height
189 * @ip: The GFS2 inode
190 * @height: The height to build to
192 * This routine makes sure that the metadata tree is tall enough to hold
193 * "size" bytes of data.
198 static int build_height(struct gfs2_inode *ip, int height)
200 struct gfs2_sbd *sdp = ip->i_sbd;
201 struct buffer_head *bh, *dibh;
202 uint64_t block = 0, *bp;
207 while (ip->i_di.di_height < height) {
208 error = gfs2_meta_inode_buffer(ip, &dibh);
213 bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
214 for (x = 0; x < sdp->sd_diptrs; x++, bp++)
221 /* Get a new block, fill it with the old direct
222 pointers, and write it out */
224 block = gfs2_alloc_meta(ip);
226 bh = gfs2_meta_new(ip->i_gl, block);
227 gfs2_trans_add_bh(ip->i_gl, bh, 1);
228 gfs2_metatype_set(bh,
231 gfs2_buffer_copy_tail(bh,
232 sizeof(struct gfs2_meta_header),
233 dibh, sizeof(struct gfs2_dinode));
238 /* Set up the new direct pointer and write it out to disk */
240 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
242 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
245 *(uint64_t *)(dibh->b_data +
246 sizeof(struct gfs2_dinode)) =
248 ip->i_di.di_blocks++;
251 ip->i_di.di_height++;
253 gfs2_dinode_out(&ip->i_di, dibh->b_data);
261 * find_metapath - Find path through the metadata tree
262 * @ip: The inode pointer
263 * @mp: The metapath to return the result in
264 * @block: The disk block to look up
266 * This routine returns a struct metapath structure that defines a path
267 * through the metadata of inode "ip" to get to block "block".
270 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
271 * filesystem with a blocksize of 4096.
273 * find_metapath() would return a struct metapath structure set to:
274 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
275 * and mp_list[2] = 165.
277 * That means that in order to get to the block containing the byte at
278 * offset 101342453, we would load the indirect block pointed to by pointer
279 * 0 in the dinode. We would then load the indirect block pointed to by
280 * pointer 48 in that indirect block. We would then load the data block
281 * pointed to by pointer 165 in that indirect block.
283 * ----------------------------------------
288 * ----------------------------------------
292 * ----------------------------------------
296 * |0 5 6 7 8 9 0 1 2|
297 * ----------------------------------------
301 * ----------------------------------------
306 * ----------------------------------------
310 * ----------------------------------------
311 * | Data block containing offset |
315 * ----------------------------------------
319 static void find_metapath(struct gfs2_inode *ip, uint64_t block,
322 struct gfs2_sbd *sdp = ip->i_sbd;
326 for (i = ip->i_di.di_height; i--;)
327 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
332 * metapointer - Return pointer to start of metadata in a buffer
334 * @height: The metadata height (0 = dinode)
337 * Return a pointer to the block number of the next height of the metadata
338 * tree given a buffer containing the pointer to the current height of the
342 static inline uint64_t *metapointer(struct buffer_head *bh,
343 unsigned int height, struct metapath *mp)
345 unsigned int head_size = (height > 0) ?
346 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
348 return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
352 * lookup_block - Get the next metadata block in metadata tree
353 * @ip: The GFS2 inode
354 * @bh: Buffer containing the pointers to metadata blocks
355 * @height: The height of the tree (0 = dinode)
357 * @create: Non-zero if we may create a new meatdata block
358 * @new: Used to indicate if we did create a new metadata block
359 * @block: the returned disk block number
361 * Given a metatree, complete to a particular height, checks to see if the next
362 * height of the tree exists. If not the next height of the tree is created.
363 * The block number of the next height of the metadata tree is returned.
367 static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
368 unsigned int height, struct metapath *mp, int create,
369 int *new, uint64_t *block)
371 uint64_t *ptr = metapointer(bh, height, mp);
374 *block = be64_to_cpu(*ptr);
383 if (height == ip->i_di.di_height - 1 &&
385 *block = gfs2_alloc_data(ip);
387 *block = gfs2_alloc_meta(ip);
389 gfs2_trans_add_bh(ip->i_gl, bh, 1);
391 *ptr = cpu_to_be64(*block);
392 ip->i_di.di_blocks++;
398 * gfs2_block_map - Map a block from an inode to a disk block
399 * @ip: The GFS2 inode
400 * @lblock: The logical block number
401 * @new: Value/Result argument (1 = may create/did create new blocks)
402 * @dblock: the disk block number of the start of an extent
403 * @extlen: the size of the extent
405 * Find the block number on the current device which corresponds to an
406 * inode's block. If the block had to be created, "new" will be set.
411 int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
412 uint64_t *dblock, uint32_t *extlen)
414 struct gfs2_sbd *sdp = ip->i_sbd;
415 struct buffer_head *bh;
420 unsigned int end_of_metadata;
430 down_write(&ip->i_rw_mutex);
432 down_read(&ip->i_rw_mutex);
434 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
437 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
439 height = calc_tree_height(ip, (lblock + 1) * bsize);
440 if (ip->i_di.di_height < height) {
444 error = build_height(ip, height);
449 find_metapath(ip, lblock, &mp);
450 end_of_metadata = ip->i_di.di_height - 1;
452 error = gfs2_meta_inode_buffer(ip, &bh);
456 for (x = 0; x < end_of_metadata; x++) {
457 lookup_block(ip, bh, x, &mp, create, new, dblock);
462 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
467 lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
469 if (extlen && *dblock) {
477 nptrs = (end_of_metadata) ? sdp->sd_inptrs :
480 while (++mp.mp_list[end_of_metadata] < nptrs) {
481 lookup_block(ip, bh, end_of_metadata, &mp,
482 0, &tmp_new, &tmp_dblock);
484 if (*dblock + *extlen != tmp_dblock)
495 error = gfs2_meta_inode_buffer(ip, &bh);
497 gfs2_trans_add_bh(ip->i_gl, bh, 1);
498 gfs2_dinode_out(&ip->i_di, bh->b_data);
505 up_write(&ip->i_rw_mutex);
507 up_read(&ip->i_rw_mutex);
513 * recursive_scan - recursively scan through the end of a file
515 * @dibh: the dinode buffer
516 * @mp: the path through the metadata to the point to start
517 * @height: the height the recursion is at
518 * @block: the indirect block to look at
519 * @first: 1 if this is the first block
520 * @bc: the call to make for each piece of metadata
521 * @data: data opaque to this function to pass to @bc
523 * When this is first called @height and @block should be zero and
524 * @first should be 1.
529 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
530 struct metapath *mp, unsigned int height,
531 uint64_t block, int first, block_call_t bc,
534 struct gfs2_sbd *sdp = ip->i_sbd;
535 struct buffer_head *bh = NULL;
536 uint64_t *top, *bottom;
539 int mh_size = sizeof(struct gfs2_meta_header);
542 error = gfs2_meta_inode_buffer(ip, &bh);
547 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
549 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
552 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
556 top = (uint64_t *)(bh->b_data + mh_size) +
557 ((first) ? mp->mp_list[height] : 0);
559 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
562 error = bc(ip, dibh, bh, top, bottom, height, data);
566 if (height < ip->i_di.di_height - 1)
567 for (; top < bottom; top++, first = 0) {
571 bn = be64_to_cpu(*top);
573 error = recursive_scan(ip, dibh, mp, height + 1, bn,
586 * do_strip - Look for a layer a particular layer of the file and strip it off
588 * @dibh: the dinode buffer
589 * @bh: A buffer of pointers
590 * @top: The first pointer in the buffer
591 * @bottom: One more than the last pointer
592 * @height: the height this buffer is at
593 * @data: a pointer to a struct strip_mine
598 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
599 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
600 unsigned int height, void *data)
602 struct strip_mine *sm = (struct strip_mine *)data;
603 struct gfs2_sbd *sdp = ip->i_sbd;
604 struct gfs2_rgrp_list rlist;
608 unsigned int rg_blocks = 0;
610 unsigned int revokes = 0;
617 if (height != sm->sm_height)
625 metadata = (height != ip->i_di.di_height - 1);
627 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
629 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
633 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
637 for (p = top; p < bottom; p++) {
641 bn = be64_to_cpu(*p);
643 if (bstart + blen == bn)
647 gfs2_rlist_add(sdp, &rlist, bstart);
655 gfs2_rlist_add(sdp, &rlist, bstart);
657 goto out; /* Nothing to do */
659 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
661 for (x = 0; x < rlist.rl_rgrps; x++) {
662 struct gfs2_rgrpd *rgd;
663 rgd = get_gl2rgd(rlist.rl_ghs[x].gh_gl);
664 rg_blocks += rgd->rd_ri.ri_length;
667 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
671 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
672 RES_INDIRECT + RES_STATFS + RES_QUOTA,
677 down_write(&ip->i_rw_mutex);
679 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
680 gfs2_trans_add_bh(ip->i_gl, bh, 1);
685 for (p = top; p < bottom; p++) {
689 bn = be64_to_cpu(*p);
691 if (bstart + blen == bn)
696 gfs2_free_meta(ip, bstart, blen);
698 gfs2_free_data(ip, bstart, blen);
706 if (!ip->i_di.di_blocks)
707 gfs2_consist_inode(ip);
708 ip->i_di.di_blocks--;
712 gfs2_free_meta(ip, bstart, blen);
714 gfs2_free_data(ip, bstart, blen);
717 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
719 gfs2_dinode_out(&ip->i_di, dibh->b_data);
721 up_write(&ip->i_rw_mutex);
726 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
729 gfs2_rlist_free(&rlist);
732 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
738 * do_grow - Make a file look bigger than it is
740 * @size: the size to set the file to
742 * Called with an exclusive lock on @ip.
747 static int do_grow(struct gfs2_inode *ip, uint64_t size)
749 struct gfs2_sbd *sdp = ip->i_sbd;
750 struct gfs2_alloc *al;
751 struct buffer_head *dibh;
755 al = gfs2_alloc_get(ip);
757 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
761 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
765 al->al_requested = sdp->sd_max_height + RES_DATA;
767 error = gfs2_inplace_reserve(ip);
771 error = gfs2_trans_begin(sdp,
772 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
773 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
777 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
778 if (gfs2_is_stuffed(ip)) {
779 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
785 h = calc_tree_height(ip, size);
786 if (ip->i_di.di_height < h) {
787 down_write(&ip->i_rw_mutex);
788 error = build_height(ip, h);
789 up_write(&ip->i_rw_mutex);
795 ip->i_di.di_size = size;
796 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
798 error = gfs2_meta_inode_buffer(ip, &dibh);
802 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
803 gfs2_dinode_out(&ip->i_di, dibh->b_data);
810 gfs2_inplace_release(ip);
813 gfs2_quota_unlock(ip);
821 static int trunc_start(struct gfs2_inode *ip, uint64_t size)
823 struct gfs2_sbd *sdp = ip->i_sbd;
824 struct buffer_head *dibh;
825 int journaled = gfs2_is_jdata(ip);
828 error = gfs2_trans_begin(sdp,
829 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
833 error = gfs2_meta_inode_buffer(ip, &dibh);
837 if (gfs2_is_stuffed(ip)) {
838 ip->i_di.di_size = size;
839 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
840 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
841 gfs2_dinode_out(&ip->i_di, dibh->b_data);
842 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
846 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
847 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
850 ip->i_di.di_size = size;
851 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
852 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
853 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
854 gfs2_dinode_out(&ip->i_di, dibh->b_data);
866 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
868 unsigned int height = ip->i_di.di_height;
876 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
878 find_metapath(ip, lblock, &mp);
881 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
886 struct strip_mine sm;
887 sm.sm_first = !!size;
888 sm.sm_height = height;
890 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
895 gfs2_quota_unhold(ip);
902 static int trunc_end(struct gfs2_inode *ip)
904 struct gfs2_sbd *sdp = ip->i_sbd;
905 struct buffer_head *dibh;
908 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
912 down_write(&ip->i_rw_mutex);
914 error = gfs2_meta_inode_buffer(ip, &dibh);
918 if (!ip->i_di.di_size) {
919 ip->i_di.di_height = 0;
920 ip->i_di.di_goal_meta =
921 ip->i_di.di_goal_data =
923 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
925 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
926 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
928 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
929 gfs2_dinode_out(&ip->i_di, dibh->b_data);
933 up_write(&ip->i_rw_mutex);
941 * do_shrink - make a file smaller
943 * @size: the size to make the file
944 * @truncator: function to truncate the last partial block
946 * Called with an exclusive lock on @ip.
951 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
955 error = trunc_start(ip, size);
961 error = trunc_dealloc(ip, size);
963 error = trunc_end(ip);
969 * gfs2_truncatei - make a file a given size
971 * @size: the size to make the file
972 * @truncator: function to truncate the last partial block
974 * The file size can grow, shrink, or stay the same size.
979 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
983 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
986 if (size > ip->i_di.di_size)
987 error = do_grow(ip, size);
989 error = do_shrink(ip, size);
994 int gfs2_truncatei_resume(struct gfs2_inode *ip)
997 error = trunc_dealloc(ip, ip->i_di.di_size);
999 error = trunc_end(ip);
1003 int gfs2_file_dealloc(struct gfs2_inode *ip)
1005 return trunc_dealloc(ip, 0);
1009 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1011 * @len: the number of bytes to be written to the file
1012 * @data_blocks: returns the number of data blocks required
1013 * @ind_blocks: returns the number of indirect blocks required
1017 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1018 unsigned int *data_blocks, unsigned int *ind_blocks)
1020 struct gfs2_sbd *sdp = ip->i_sbd;
1023 if (gfs2_is_dir(ip)) {
1024 *data_blocks = DIV_RU(len, sdp->sd_jbsize) + 2;
1025 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1027 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1028 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1031 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1032 tmp = DIV_RU(tmp, sdp->sd_inptrs);
1038 * gfs2_write_alloc_required - figure out if a write will require an allocation
1039 * @ip: the file being written to
1040 * @offset: the offset to write to
1041 * @len: the number of bytes being written
1042 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1047 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1048 unsigned int len, int *alloc_required)
1050 struct gfs2_sbd *sdp = ip->i_sbd;
1051 uint64_t lblock, lblock_stop, dblock;
1056 *alloc_required = 0;
1061 if (gfs2_is_stuffed(ip)) {
1063 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1064 *alloc_required = 1;
1068 if (gfs2_is_dir(ip)) {
1069 unsigned int bsize = sdp->sd_jbsize;
1071 do_div(lblock, bsize);
1072 lblock_stop = offset + len + bsize - 1;
1073 do_div(lblock_stop, bsize);
1075 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1076 lblock = offset >> shift;
1077 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1080 for (; lblock < lblock_stop; lblock += extlen) {
1081 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1086 *alloc_required = 1;