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 <asm/semaphore.h>
19 #include "lm_interface.h"
32 /* This doesn't need to be that large as max 64 bit pointers in a 4k
33 * block is 512, so __u16 is fine for that. It saves stack space to
37 __u16 mp_list[GFS2_MAX_META_HEIGHT];
40 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
41 struct buffer_head *bh, uint64_t *top,
42 uint64_t *bottom, unsigned int height,
47 unsigned int sm_height;
51 * @gfs2_unstuffer_sync - Synchronously unstuff a dinode
57 * Cheat and use a metadata buffer instead of a data page.
62 int gfs2_unstuffer_sync(struct gfs2_inode *ip, struct buffer_head *dibh,
63 uint64_t block, void *private)
65 struct buffer_head *bh;
68 bh = gfs2_meta_new(ip->i_gl, block);
70 gfs2_buffer_copy_tail(bh, 0, dibh, sizeof(struct gfs2_dinode));
73 error = sync_dirty_buffer(bh);
81 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
82 * @ip: The GFS2 inode to unstuff
83 * @unstuffer: the routine that handles unstuffing a non-zero length file
84 * @private: private data for the unstuffer
86 * This routine unstuffs a dinode and returns it to a "normal" state such
87 * that the height can be grown in the traditional way.
92 int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
95 struct buffer_head *bh, *dibh;
97 int isdir = gfs2_is_dir(ip);
100 down_write(&ip->i_rw_mutex);
102 error = gfs2_meta_inode_buffer(ip, &dibh);
106 if (ip->i_di.di_size) {
107 /* Get a free block, fill it with the stuffed data,
108 and write it out to disk */
111 block = gfs2_alloc_meta(ip);
113 error = gfs2_dir_get_buffer(ip, block, 1, &bh);
116 gfs2_buffer_copy_tail(bh,
117 sizeof(struct gfs2_meta_header),
118 dibh, sizeof(struct gfs2_dinode));
121 block = gfs2_alloc_data(ip);
123 error = unstuffer(ip, dibh, block, private);
129 /* Set up the pointer to the new block */
131 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
133 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
135 if (ip->i_di.di_size) {
136 *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
138 ip->i_di.di_blocks++;
141 ip->i_di.di_height = 1;
143 gfs2_dinode_out(&ip->i_di, dibh->b_data);
149 up_write(&ip->i_rw_mutex);
155 * calc_tree_height - Calculate the height of a metadata tree
156 * @ip: The GFS2 inode
157 * @size: The proposed size of the file
159 * Work out how tall a metadata tree needs to be in order to accommodate a
160 * file of a particular size. If size is less than the current size of
161 * the inode, then the current size of the inode is used instead of the
164 * Returns: the height the tree should be
167 static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
169 struct gfs2_sbd *sdp = ip->i_sbd;
171 unsigned int max, height;
173 if (ip->i_di.di_size > size)
174 size = ip->i_di.di_size;
176 if (gfs2_is_dir(ip)) {
177 arr = sdp->sd_jheightsize;
178 max = sdp->sd_max_jheight;
180 arr = sdp->sd_heightsize;
181 max = sdp->sd_max_height;
184 for (height = 0; height < max; height++)
185 if (arr[height] >= size)
192 * build_height - Build a metadata tree of the requested height
193 * @ip: The GFS2 inode
194 * @height: The height to build to
196 * This routine makes sure that the metadata tree is tall enough to hold
197 * "size" bytes of data.
202 static int build_height(struct gfs2_inode *ip, int height)
204 struct gfs2_sbd *sdp = ip->i_sbd;
205 struct buffer_head *bh, *dibh;
206 uint64_t block = 0, *bp;
211 while (ip->i_di.di_height < height) {
212 error = gfs2_meta_inode_buffer(ip, &dibh);
217 bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
218 for (x = 0; x < sdp->sd_diptrs; x++, bp++)
225 /* Get a new block, fill it with the old direct
226 pointers, and write it out */
228 block = gfs2_alloc_meta(ip);
230 bh = gfs2_meta_new(ip->i_gl, block);
231 gfs2_trans_add_bh(ip->i_gl, bh, 1);
232 gfs2_metatype_set(bh,
235 gfs2_buffer_copy_tail(bh,
236 sizeof(struct gfs2_meta_header),
237 dibh, sizeof(struct gfs2_dinode));
242 /* Set up the new direct pointer and write it out to disk */
244 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
246 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
249 *(uint64_t *)(dibh->b_data +
250 sizeof(struct gfs2_dinode)) =
252 ip->i_di.di_blocks++;
255 ip->i_di.di_height++;
257 gfs2_dinode_out(&ip->i_di, dibh->b_data);
265 * find_metapath - Find path through the metadata tree
266 * @ip: The inode pointer
267 * @mp: The metapath to return the result in
268 * @block: The disk block to look up
270 * This routine returns a struct metapath structure that defines a path
271 * through the metadata of inode "ip" to get to block "block".
274 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
275 * filesystem with a blocksize of 4096.
277 * find_metapath() would return a struct metapath structure set to:
278 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
279 * and mp_list[2] = 165.
281 * That means that in order to get to the block containing the byte at
282 * offset 101342453, we would load the indirect block pointed to by pointer
283 * 0 in the dinode. We would then load the indirect block pointed to by
284 * pointer 48 in that indirect block. We would then load the data block
285 * pointed to by pointer 165 in that indirect block.
287 * ----------------------------------------
292 * ----------------------------------------
296 * ----------------------------------------
300 * |0 5 6 7 8 9 0 1 2|
301 * ----------------------------------------
305 * ----------------------------------------
310 * ----------------------------------------
314 * ----------------------------------------
315 * | Data block containing offset |
319 * ----------------------------------------
323 static void find_metapath(struct gfs2_inode *ip, uint64_t block,
326 struct gfs2_sbd *sdp = ip->i_sbd;
330 for (i = ip->i_di.di_height; i--;)
331 mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
336 * metapointer - Return pointer to start of metadata in a buffer
338 * @height: The metadata height (0 = dinode)
341 * Return a pointer to the block number of the next height of the metadata
342 * tree given a buffer containing the pointer to the current height of the
346 static inline uint64_t *metapointer(struct buffer_head *bh,
347 unsigned int height, struct metapath *mp)
349 unsigned int head_size = (height > 0) ?
350 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
352 return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
356 * lookup_block - Get the next metadata block in metadata tree
357 * @ip: The GFS2 inode
358 * @bh: Buffer containing the pointers to metadata blocks
359 * @height: The height of the tree (0 = dinode)
361 * @create: Non-zero if we may create a new meatdata block
362 * @new: Used to indicate if we did create a new metadata block
363 * @block: the returned disk block number
365 * Given a metatree, complete to a particular height, checks to see if the next
366 * height of the tree exists. If not the next height of the tree is created.
367 * The block number of the next height of the metadata tree is returned.
371 static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
372 unsigned int height, struct metapath *mp, int create,
373 int *new, uint64_t *block)
375 uint64_t *ptr = metapointer(bh, height, mp);
378 *block = be64_to_cpu(*ptr);
387 if (height == ip->i_di.di_height - 1 &&
389 *block = gfs2_alloc_data(ip);
391 *block = gfs2_alloc_meta(ip);
393 gfs2_trans_add_bh(ip->i_gl, bh, 1);
395 *ptr = cpu_to_be64(*block);
396 ip->i_di.di_blocks++;
402 * gfs2_block_map - Map a block from an inode to a disk block
403 * @ip: The GFS2 inode
404 * @lblock: The logical block number
405 * @new: Value/Result argument (1 = may create/did create new blocks)
406 * @dblock: the disk block number of the start of an extent
407 * @extlen: the size of the extent
409 * Find the block number on the current device which corresponds to an
410 * inode's block. If the block had to be created, "new" will be set.
415 int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
416 uint64_t *dblock, uint32_t *extlen)
418 struct gfs2_sbd *sdp = ip->i_sbd;
419 struct buffer_head *bh;
424 unsigned int end_of_metadata;
434 down_write(&ip->i_rw_mutex);
436 down_read(&ip->i_rw_mutex);
438 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
441 bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
443 height = calc_tree_height(ip, (lblock + 1) * bsize);
444 if (ip->i_di.di_height < height) {
448 error = build_height(ip, height);
453 find_metapath(ip, lblock, &mp);
454 end_of_metadata = ip->i_di.di_height - 1;
456 error = gfs2_meta_inode_buffer(ip, &bh);
460 for (x = 0; x < end_of_metadata; x++) {
461 lookup_block(ip, bh, x, &mp, create, new, dblock);
466 error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
471 lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
473 if (extlen && *dblock) {
481 nptrs = (end_of_metadata) ? sdp->sd_inptrs :
484 while (++mp.mp_list[end_of_metadata] < nptrs) {
485 lookup_block(ip, bh, end_of_metadata, &mp,
486 0, &tmp_new, &tmp_dblock);
488 if (*dblock + *extlen != tmp_dblock)
499 error = gfs2_meta_inode_buffer(ip, &bh);
501 gfs2_trans_add_bh(ip->i_gl, bh, 1);
502 gfs2_dinode_out(&ip->i_di, bh->b_data);
509 up_write(&ip->i_rw_mutex);
511 up_read(&ip->i_rw_mutex);
517 * recursive_scan - recursively scan through the end of a file
519 * @dibh: the dinode buffer
520 * @mp: the path through the metadata to the point to start
521 * @height: the height the recursion is at
522 * @block: the indirect block to look at
523 * @first: 1 if this is the first block
524 * @bc: the call to make for each piece of metadata
525 * @data: data opaque to this function to pass to @bc
527 * When this is first called @height and @block should be zero and
528 * @first should be 1.
533 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
534 struct metapath *mp, unsigned int height,
535 uint64_t block, int first, block_call_t bc,
538 struct gfs2_sbd *sdp = ip->i_sbd;
539 struct buffer_head *bh = NULL;
540 uint64_t *top, *bottom;
543 int mh_size = sizeof(struct gfs2_meta_header);
546 error = gfs2_meta_inode_buffer(ip, &bh);
551 top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
553 bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
556 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
560 top = (uint64_t *)(bh->b_data + mh_size) +
561 ((first) ? mp->mp_list[height] : 0);
563 bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
566 error = bc(ip, dibh, bh, top, bottom, height, data);
570 if (height < ip->i_di.di_height - 1)
571 for (; top < bottom; top++, first = 0) {
575 bn = be64_to_cpu(*top);
577 error = recursive_scan(ip, dibh, mp, height + 1, bn,
590 * do_strip - Look for a layer a particular layer of the file and strip it off
592 * @dibh: the dinode buffer
593 * @bh: A buffer of pointers
594 * @top: The first pointer in the buffer
595 * @bottom: One more than the last pointer
596 * @height: the height this buffer is at
597 * @data: a pointer to a struct strip_mine
602 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
603 struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
604 unsigned int height, void *data)
606 struct strip_mine *sm = (struct strip_mine *)data;
607 struct gfs2_sbd *sdp = ip->i_sbd;
608 struct gfs2_rgrp_list rlist;
612 unsigned int rg_blocks = 0;
614 unsigned int revokes = 0;
621 if (height != sm->sm_height)
629 metadata = (height != ip->i_di.di_height - 1);
631 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
633 error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
637 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
641 for (p = top; p < bottom; p++) {
645 bn = be64_to_cpu(*p);
647 if (bstart + blen == bn)
651 gfs2_rlist_add(sdp, &rlist, bstart);
659 gfs2_rlist_add(sdp, &rlist, bstart);
661 goto out; /* Nothing to do */
663 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
665 for (x = 0; x < rlist.rl_rgrps; x++) {
666 struct gfs2_rgrpd *rgd;
667 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
668 rg_blocks += rgd->rd_ri.ri_length;
671 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
675 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
676 RES_INDIRECT + RES_STATFS + RES_QUOTA,
681 down_write(&ip->i_rw_mutex);
683 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
684 gfs2_trans_add_bh(ip->i_gl, bh, 1);
689 for (p = top; p < bottom; p++) {
693 bn = be64_to_cpu(*p);
695 if (bstart + blen == bn)
700 gfs2_free_meta(ip, bstart, blen);
702 gfs2_free_data(ip, bstart, blen);
710 if (!ip->i_di.di_blocks)
711 gfs2_consist_inode(ip);
712 ip->i_di.di_blocks--;
716 gfs2_free_meta(ip, bstart, blen);
718 gfs2_free_data(ip, bstart, blen);
721 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
723 gfs2_dinode_out(&ip->i_di, dibh->b_data);
725 up_write(&ip->i_rw_mutex);
730 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
733 gfs2_rlist_free(&rlist);
736 gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
742 * do_grow - Make a file look bigger than it is
744 * @size: the size to set the file to
746 * Called with an exclusive lock on @ip.
751 static int do_grow(struct gfs2_inode *ip, uint64_t size)
753 struct gfs2_sbd *sdp = ip->i_sbd;
754 struct gfs2_alloc *al;
755 struct buffer_head *dibh;
759 al = gfs2_alloc_get(ip);
761 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
765 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
769 al->al_requested = sdp->sd_max_height + RES_DATA;
771 error = gfs2_inplace_reserve(ip);
775 error = gfs2_trans_begin(sdp,
776 sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
777 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
781 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
782 if (gfs2_is_stuffed(ip)) {
783 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
789 h = calc_tree_height(ip, size);
790 if (ip->i_di.di_height < h) {
791 down_write(&ip->i_rw_mutex);
792 error = build_height(ip, h);
793 up_write(&ip->i_rw_mutex);
799 ip->i_di.di_size = size;
800 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
802 error = gfs2_meta_inode_buffer(ip, &dibh);
806 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
807 gfs2_dinode_out(&ip->i_di, dibh->b_data);
814 gfs2_inplace_release(ip);
817 gfs2_quota_unlock(ip);
825 static int trunc_start(struct gfs2_inode *ip, uint64_t size)
827 struct gfs2_sbd *sdp = ip->i_sbd;
828 struct buffer_head *dibh;
829 int journaled = gfs2_is_jdata(ip);
832 error = gfs2_trans_begin(sdp,
833 RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
837 error = gfs2_meta_inode_buffer(ip, &dibh);
841 if (gfs2_is_stuffed(ip)) {
842 ip->i_di.di_size = size;
843 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
844 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
845 gfs2_dinode_out(&ip->i_di, dibh->b_data);
846 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
850 if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
851 error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
854 ip->i_di.di_size = size;
855 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
856 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
857 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
858 gfs2_dinode_out(&ip->i_di, dibh->b_data);
870 static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
872 unsigned int height = ip->i_di.di_height;
880 lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
882 find_metapath(ip, lblock, &mp);
885 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
890 struct strip_mine sm;
891 sm.sm_first = !!size;
892 sm.sm_height = height;
894 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
899 gfs2_quota_unhold(ip);
906 static int trunc_end(struct gfs2_inode *ip)
908 struct gfs2_sbd *sdp = ip->i_sbd;
909 struct buffer_head *dibh;
912 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
916 down_write(&ip->i_rw_mutex);
918 error = gfs2_meta_inode_buffer(ip, &dibh);
922 if (!ip->i_di.di_size) {
923 ip->i_di.di_height = 0;
924 ip->i_di.di_goal_meta =
925 ip->i_di.di_goal_data =
927 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
929 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
930 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
932 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
933 gfs2_dinode_out(&ip->i_di, dibh->b_data);
937 up_write(&ip->i_rw_mutex);
945 * do_shrink - make a file smaller
947 * @size: the size to make the file
948 * @truncator: function to truncate the last partial block
950 * Called with an exclusive lock on @ip.
955 static int do_shrink(struct gfs2_inode *ip, uint64_t size)
959 error = trunc_start(ip, size);
965 error = trunc_dealloc(ip, size);
967 error = trunc_end(ip);
973 * gfs2_truncatei - make a file a given size
975 * @size: the size to make the file
976 * @truncator: function to truncate the last partial block
978 * The file size can grow, shrink, or stay the same size.
983 int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
987 if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
990 if (size > ip->i_di.di_size)
991 error = do_grow(ip, size);
993 error = do_shrink(ip, size);
998 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1001 error = trunc_dealloc(ip, ip->i_di.di_size);
1003 error = trunc_end(ip);
1007 int gfs2_file_dealloc(struct gfs2_inode *ip)
1009 return trunc_dealloc(ip, 0);
1013 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1015 * @len: the number of bytes to be written to the file
1016 * @data_blocks: returns the number of data blocks required
1017 * @ind_blocks: returns the number of indirect blocks required
1021 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1022 unsigned int *data_blocks, unsigned int *ind_blocks)
1024 struct gfs2_sbd *sdp = ip->i_sbd;
1027 if (gfs2_is_dir(ip)) {
1028 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1029 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1031 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1032 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1035 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1036 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1042 * gfs2_write_alloc_required - figure out if a write will require an allocation
1043 * @ip: the file being written to
1044 * @offset: the offset to write to
1045 * @len: the number of bytes being written
1046 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1051 int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
1052 unsigned int len, int *alloc_required)
1054 struct gfs2_sbd *sdp = ip->i_sbd;
1055 uint64_t lblock, lblock_stop, dblock;
1060 *alloc_required = 0;
1065 if (gfs2_is_stuffed(ip)) {
1067 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1068 *alloc_required = 1;
1072 if (gfs2_is_dir(ip)) {
1073 unsigned int bsize = sdp->sd_jbsize;
1075 do_div(lblock, bsize);
1076 lblock_stop = offset + len + bsize - 1;
1077 do_div(lblock_stop, bsize);
1079 unsigned int shift = sdp->sd_sb.sb_bsize_shift;
1080 lblock = offset >> shift;
1081 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1084 for (; lblock < lblock_stop; lblock += extlen) {
1085 error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
1090 *alloc_required = 1;