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);
82 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
83 * @ip: The GFS2 inode to unstuff
84 * @unstuffer: the routine that handles unstuffing a non-zero length file
85 * @private: private data for the unstuffer
87 * This routine unstuffs a dinode and returns it to a "normal" state such
88 * that the height can be grown in the traditional way.
93 int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
96 struct buffer_head *bh, *dibh;
98 int isdir = gfs2_is_dir(ip);
101 down_write(&ip->i_rw_mutex);
103 error = gfs2_meta_inode_buffer(ip, &dibh);
107 if (ip->i_di.di_size) {
108 /* Get a free block, fill it with the stuffed data,
109 and write it out to disk */
112 block = gfs2_alloc_meta(ip);
114 error = gfs2_dir_get_new_buffer(ip, block, &bh);
117 gfs2_buffer_copy_tail(bh,
118 sizeof(struct gfs2_meta_header),
119 dibh, sizeof(struct gfs2_dinode));
122 block = gfs2_alloc_data(ip);
124 error = unstuffer(ip, dibh, block, private);
130 /* Set up the pointer to the new block */
132 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
134 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
136 if (ip->i_di.di_size) {
137 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
139 ip->i_di.di_blocks++;
142 ip->i_di.di_height = 1;
144 gfs2_dinode_out(&ip->i_di, dibh->b_data);
150 up_write(&ip->i_rw_mutex);
156 * calc_tree_height - Calculate the height of a metadata tree
157 * @ip: The GFS2 inode
158 * @size: The proposed size of the file
160 * Work out how tall a metadata tree needs to be in order to accommodate a
161 * file of a particular size. If size is less than the current size of
162 * the inode, then the current size of the inode is used instead of the
165 * Returns: the height the tree should be
168 static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
170 struct gfs2_sbd *sdp = ip->i_sbd;
172 unsigned int max, height;
174 if (ip->i_di.di_size > size)
175 size = ip->i_di.di_size;
177 if (gfs2_is_dir(ip)) {
178 arr = sdp->sd_jheightsize;
179 max = sdp->sd_max_jheight;
181 arr = sdp->sd_heightsize;
182 max = sdp->sd_max_height;
185 for (height = 0; height < max; height++)
186 if (arr[height] >= size)
193 * build_height - Build a metadata tree of the requested height
194 * @ip: The GFS2 inode
195 * @height: The height to build to
197 * This routine makes sure that the metadata tree is tall enough to hold
198 * "size" bytes of data.
203 static int build_height(struct gfs2_inode *ip, int height)
205 struct gfs2_sbd *sdp = ip->i_sbd;
206 struct buffer_head *bh, *dibh;
207 uint64_t block = 0, *bp;
212 while (ip->i_di.di_height < height) {
213 error = gfs2_meta_inode_buffer(ip, &dibh);
218 bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
219 for (x = 0; x < sdp->sd_diptrs; x++, bp++)
226 /* Get a new block, fill it with the old direct
227 pointers, and write it out */
229 block = gfs2_alloc_meta(ip);
231 bh = gfs2_meta_new(ip->i_gl, block);
232 gfs2_trans_add_bh(ip->i_gl, bh, 1);
233 gfs2_metatype_set(bh,
236 gfs2_buffer_copy_tail(bh,
237 sizeof(struct gfs2_meta_header),
238 dibh, sizeof(struct gfs2_dinode));
243 /* Set up the new direct pointer and write it out to disk */
245 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
247 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
250 *(uint64_t *)(dibh->b_data +
251 sizeof(struct gfs2_dinode)) =
253 ip->i_di.di_blocks++;
256 ip->i_di.di_height++;
258 gfs2_dinode_out(&ip->i_di, dibh->b_data);
266 * find_metapath - Find path through the metadata tree
267 * @ip: The inode pointer
268 * @mp: The metapath to return the result in
269 * @block: The disk block to look up
271 * This routine returns a struct metapath structure that defines a path
272 * through the metadata of inode "ip" to get to block "block".
275 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
276 * filesystem with a blocksize of 4096.
278 * find_metapath() would return a struct metapath structure set to:
279 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
280 * and mp_list[2] = 165.
282 * That means that in order to get to the block containing the byte at
283 * offset 101342453, we would load the indirect block pointed to by pointer
284 * 0 in the dinode. We would then load the indirect block pointed to by
285 * pointer 48 in that indirect block. We would then load the data block
286 * pointed to by pointer 165 in that indirect block.
288 * ----------------------------------------
293 * ----------------------------------------
297 * ----------------------------------------
301 * |0 5 6 7 8 9 0 1 2|
302 * ----------------------------------------
306 * ----------------------------------------
311 * ----------------------------------------
315 * ----------------------------------------
316 * | Data block containing offset |
320 * ----------------------------------------
324 static void find_metapath(struct gfs2_inode *ip, uint64_t block,
327 struct gfs2_sbd *sdp = ip->i_sbd;
331 for (i = ip->i_di.di_height; i--;)
332 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
337 * metapointer - Return pointer to start of metadata in a buffer
339 * @height: The metadata height (0 = dinode)
342 * Return a pointer to the block number of the next height of the metadata
343 * tree given a buffer containing the pointer to the current height of the
347 static inline uint64_t *metapointer(struct buffer_head *bh,
348 unsigned int height, struct metapath *mp)
350 unsigned int head_size = (height > 0) ?
351 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
353 return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
357 * lookup_block - Get the next metadata block in metadata tree
358 * @ip: The GFS2 inode
359 * @bh: Buffer containing the pointers to metadata blocks
360 * @height: The height of the tree (0 = dinode)
362 * @create: Non-zero if we may create a new meatdata block
363 * @new: Used to indicate if we did create a new metadata block
364 * @block: the returned disk block number
366 * Given a metatree, complete to a particular height, checks to see if the next
367 * height of the tree exists. If not the next height of the tree is created.
368 * The block number of the next height of the metadata tree is returned.
372 static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
373 unsigned int height, struct metapath *mp, int create,
374 int *new, uint64_t *block)
376 uint64_t *ptr = metapointer(bh, height, mp);
379 *block = be64_to_cpu(*ptr);
388 if (height == ip->i_di.di_height - 1 &&
390 *block = gfs2_alloc_data(ip);
392 *block = gfs2_alloc_meta(ip);
394 gfs2_trans_add_bh(ip->i_gl, bh, 1);
396 *ptr = cpu_to_be64(*block);
397 ip->i_di.di_blocks++;
403 * gfs2_block_map - Map a block from an inode to a disk block
404 * @ip: The GFS2 inode
405 * @lblock: The logical block number
406 * @new: Value/Result argument (1 = may create/did create new blocks)
407 * @dblock: the disk block number of the start of an extent
408 * @extlen: the size of the extent
410 * Find the block number on the current device which corresponds to an
411 * inode's block. If the block had to be created, "new" will be set.
416 int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
417 uint64_t *dblock, uint32_t *extlen)
419 struct gfs2_sbd *sdp = ip->i_sbd;
420 struct buffer_head *bh;
425 unsigned int end_of_metadata;
435 down_write(&ip->i_rw_mutex);
437 down_read(&ip->i_rw_mutex);
439 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
442 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
444 height = calc_tree_height(ip, (lblock + 1) * bsize);
445 if (ip->i_di.di_height < height) {
449 error = build_height(ip, height);
454 find_metapath(ip, lblock, &mp);
455 end_of_metadata = ip->i_di.di_height - 1;
457 error = gfs2_meta_inode_buffer(ip, &bh);
461 for (x = 0; x < end_of_metadata; x++) {
462 lookup_block(ip, bh, x, &mp, create, new, dblock);
467 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
472 lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
474 if (extlen && *dblock) {
482 nptrs = (end_of_metadata) ? sdp->sd_inptrs :
485 while (++mp.mp_list[end_of_metadata] < nptrs) {
486 lookup_block(ip, bh, end_of_metadata, &mp,
487 0, &tmp_new, &tmp_dblock);
489 if (*dblock + *extlen != tmp_dblock)
500 error = gfs2_meta_inode_buffer(ip, &bh);
502 gfs2_trans_add_bh(ip->i_gl, bh, 1);
503 gfs2_dinode_out(&ip->i_di, bh->b_data);
510 up_write(&ip->i_rw_mutex);
512 up_read(&ip->i_rw_mutex);
518 * recursive_scan - recursively scan through the end of a file
520 * @dibh: the dinode buffer
521 * @mp: the path through the metadata to the point to start
522 * @height: the height the recursion is at
523 * @block: the indirect block to look at
524 * @first: 1 if this is the first block
525 * @bc: the call to make for each piece of metadata
526 * @data: data opaque to this function to pass to @bc
528 * When this is first called @height and @block should be zero and
529 * @first should be 1.
534 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
535 struct metapath *mp, unsigned int height,
536 uint64_t block, int first, block_call_t bc,
539 struct gfs2_sbd *sdp = ip->i_sbd;
540 struct buffer_head *bh = NULL;
541 uint64_t *top, *bottom;
544 int mh_size = sizeof(struct gfs2_meta_header);
547 error = gfs2_meta_inode_buffer(ip, &bh);
552 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
554 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
557 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
561 top = (uint64_t *)(bh->b_data + mh_size) +
562 ((first) ? mp->mp_list[height] : 0);
564 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
567 error = bc(ip, dibh, bh, top, bottom, height, data);
571 if (height < ip->i_di.di_height - 1)
572 for (; top < bottom; top++, first = 0) {
576 bn = be64_to_cpu(*top);
578 error = recursive_scan(ip, dibh, mp, height + 1, bn,
591 * do_strip - Look for a layer a particular layer of the file and strip it off
593 * @dibh: the dinode buffer
594 * @bh: A buffer of pointers
595 * @top: The first pointer in the buffer
596 * @bottom: One more than the last pointer
597 * @height: the height this buffer is at
598 * @data: a pointer to a struct strip_mine
603 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
604 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
605 unsigned int height, void *data)
607 struct strip_mine *sm = (struct strip_mine *)data;
608 struct gfs2_sbd *sdp = ip->i_sbd;
609 struct gfs2_rgrp_list rlist;
613 unsigned int rg_blocks = 0;
615 unsigned int revokes = 0;
622 if (height != sm->sm_height)
630 metadata = (height != ip->i_di.di_height - 1);
632 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
634 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
638 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
642 for (p = top; p < bottom; p++) {
646 bn = be64_to_cpu(*p);
648 if (bstart + blen == bn)
652 gfs2_rlist_add(sdp, &rlist, bstart);
660 gfs2_rlist_add(sdp, &rlist, bstart);
662 goto out; /* Nothing to do */
664 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
666 for (x = 0; x < rlist.rl_rgrps; x++) {
667 struct gfs2_rgrpd *rgd;
668 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
669 rg_blocks += rgd->rd_ri.ri_length;
672 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
676 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
677 RES_INDIRECT + RES_STATFS + RES_QUOTA,
682 down_write(&ip->i_rw_mutex);
684 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
685 gfs2_trans_add_bh(ip->i_gl, bh, 1);
690 for (p = top; p < bottom; p++) {
694 bn = be64_to_cpu(*p);
696 if (bstart + blen == bn)
701 gfs2_free_meta(ip, bstart, blen);
703 gfs2_free_data(ip, bstart, blen);
711 if (!ip->i_di.di_blocks)
712 gfs2_consist_inode(ip);
713 ip->i_di.di_blocks--;
717 gfs2_free_meta(ip, bstart, blen);
719 gfs2_free_data(ip, bstart, blen);
722 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
724 gfs2_dinode_out(&ip->i_di, dibh->b_data);
726 up_write(&ip->i_rw_mutex);
731 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
734 gfs2_rlist_free(&rlist);
737 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
743 * do_grow - Make a file look bigger than it is
745 * @size: the size to set the file to
747 * Called with an exclusive lock on @ip.
752 static int do_grow(struct gfs2_inode *ip, uint64_t size)
754 struct gfs2_sbd *sdp = ip->i_sbd;
755 struct gfs2_alloc *al;
756 struct buffer_head *dibh;
760 al = gfs2_alloc_get(ip);
762 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
766 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
770 al->al_requested = sdp->sd_max_height + RES_DATA;
772 error = gfs2_inplace_reserve(ip);
776 error = gfs2_trans_begin(sdp,
777 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
778 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
782 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
783 if (gfs2_is_stuffed(ip)) {
784 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
790 h = calc_tree_height(ip, size);
791 if (ip->i_di.di_height < h) {
792 down_write(&ip->i_rw_mutex);
793 error = build_height(ip, h);
794 up_write(&ip->i_rw_mutex);
800 ip->i_di.di_size = size;
801 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
803 error = gfs2_meta_inode_buffer(ip, &dibh);
807 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
808 gfs2_dinode_out(&ip->i_di, dibh->b_data);
815 gfs2_inplace_release(ip);
818 gfs2_quota_unlock(ip);
826 static int trunc_start(struct gfs2_inode *ip, uint64_t size)
828 struct gfs2_sbd *sdp = ip->i_sbd;
829 struct buffer_head *dibh;
830 int journaled = gfs2_is_jdata(ip);
833 error = gfs2_trans_begin(sdp,
834 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
838 error = gfs2_meta_inode_buffer(ip, &dibh);
842 if (gfs2_is_stuffed(ip)) {
843 ip->i_di.di_size = size;
844 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
845 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
846 gfs2_dinode_out(&ip->i_di, dibh->b_data);
847 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
851 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
852 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
855 ip->i_di.di_size = size;
856 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
857 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
858 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
859 gfs2_dinode_out(&ip->i_di, dibh->b_data);
871 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
873 unsigned int height = ip->i_di.di_height;
881 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
883 find_metapath(ip, lblock, &mp);
886 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
891 struct strip_mine sm;
892 sm.sm_first = !!size;
893 sm.sm_height = height;
895 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
900 gfs2_quota_unhold(ip);
907 static int trunc_end(struct gfs2_inode *ip)
909 struct gfs2_sbd *sdp = ip->i_sbd;
910 struct buffer_head *dibh;
913 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
917 down_write(&ip->i_rw_mutex);
919 error = gfs2_meta_inode_buffer(ip, &dibh);
923 if (!ip->i_di.di_size) {
924 ip->i_di.di_height = 0;
925 ip->i_di.di_goal_meta =
926 ip->i_di.di_goal_data =
928 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
930 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
931 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
933 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
934 gfs2_dinode_out(&ip->i_di, dibh->b_data);
938 up_write(&ip->i_rw_mutex);
946 * do_shrink - make a file smaller
948 * @size: the size to make the file
949 * @truncator: function to truncate the last partial block
951 * Called with an exclusive lock on @ip.
956 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
960 error = trunc_start(ip, size);
966 error = trunc_dealloc(ip, size);
968 error = trunc_end(ip);
974 * gfs2_truncatei - make a file a given size
976 * @size: the size to make the file
977 * @truncator: function to truncate the last partial block
979 * The file size can grow, shrink, or stay the same size.
984 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
988 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
991 if (size > ip->i_di.di_size)
992 error = do_grow(ip, size);
994 error = do_shrink(ip, size);
999 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1002 error = trunc_dealloc(ip, ip->i_di.di_size);
1004 error = trunc_end(ip);
1008 int gfs2_file_dealloc(struct gfs2_inode *ip)
1010 return trunc_dealloc(ip, 0);
1014 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1016 * @len: the number of bytes to be written to the file
1017 * @data_blocks: returns the number of data blocks required
1018 * @ind_blocks: returns the number of indirect blocks required
1022 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1023 unsigned int *data_blocks, unsigned int *ind_blocks)
1025 struct gfs2_sbd *sdp = ip->i_sbd;
1028 if (gfs2_is_dir(ip)) {
1029 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1030 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1032 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1033 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1036 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1037 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1043 * gfs2_write_alloc_required - figure out if a write will require an allocation
1044 * @ip: the file being written to
1045 * @offset: the offset to write to
1046 * @len: the number of bytes being written
1047 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1052 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1053 unsigned int len, int *alloc_required)
1055 struct gfs2_sbd *sdp = ip->i_sbd;
1056 uint64_t lblock, lblock_stop, dblock;
1061 *alloc_required = 0;
1066 if (gfs2_is_stuffed(ip)) {
1068 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1069 *alloc_required = 1;
1073 if (gfs2_is_dir(ip)) {
1074 unsigned int bsize = sdp->sd_jbsize;
1076 do_div(lblock, bsize);
1077 lblock_stop = offset + len + bsize - 1;
1078 do_div(lblock_stop, bsize);
1080 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1081 lblock = offset >> shift;
1082 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1085 for (; lblock < lblock_stop; lblock += extlen) {
1086 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1091 *alloc_required = 1;