2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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 version 2.
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/gfs2_ondisk.h>
15 #include <linux/crc32.h>
16 #include <linux/lm_interface.h>
29 #include "ops_address.h"
31 /* This doesn't need to be that large as max 64 bit pointers in a 4k
32 * block is 512, so __u16 is fine for that. It saves stack space to
36 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
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, __be64 *top,
42 __be64 *bottom, unsigned int height,
47 unsigned int sm_height;
51 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
53 * @dibh: the dinode buffer
54 * @block: the block number that was allocated
55 * @private: any locked page held by the caller process
60 static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
61 u64 block, struct page *page)
63 struct inode *inode = &ip->i_inode;
64 struct buffer_head *bh;
67 if (!page || page->index) {
68 page = grab_cache_page(inode->i_mapping, 0);
74 if (!PageUptodate(page)) {
75 void *kaddr = kmap(page);
77 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
79 memset(kaddr + ip->i_di.di_size, 0,
80 PAGE_CACHE_SIZE - ip->i_di.di_size);
83 SetPageUptodate(page);
86 if (!page_has_buffers(page))
87 create_empty_buffers(page, 1 << inode->i_blkbits,
90 bh = page_buffers(page);
92 if (!buffer_mapped(bh))
93 map_bh(bh, inode->i_sb, block);
95 set_buffer_uptodate(bh);
96 if (!gfs2_is_jdata(ip))
97 mark_buffer_dirty(bh);
98 if (!gfs2_is_writeback(ip))
99 gfs2_trans_add_bh(ip->i_gl, bh, 0);
103 page_cache_release(page);
110 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
111 * @ip: The GFS2 inode to unstuff
112 * @unstuffer: the routine that handles unstuffing a non-zero length file
113 * @private: private data for the unstuffer
115 * This routine unstuffs a dinode and returns it to a "normal" state such
116 * that the height can be grown in the traditional way.
121 int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
123 struct buffer_head *bh, *dibh;
124 struct gfs2_dinode *di;
126 int isdir = gfs2_is_dir(ip);
129 down_write(&ip->i_rw_mutex);
131 error = gfs2_meta_inode_buffer(ip, &dibh);
135 if (ip->i_di.di_size) {
136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */
140 block = gfs2_alloc_meta(ip);
142 error = gfs2_dir_get_new_buffer(ip, block, &bh);
145 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
146 dibh, sizeof(struct gfs2_dinode));
149 block = gfs2_alloc_data(ip);
151 error = gfs2_unstuffer_page(ip, dibh, block, page);
157 /* Set up the pointer to the new block */
159 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
160 di = (struct gfs2_dinode *)dibh->b_data;
161 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
163 if (ip->i_di.di_size) {
164 *(__be64 *)(di + 1) = cpu_to_be64(block);
165 ip->i_di.di_blocks++;
166 gfs2_set_inode_blocks(&ip->i_inode);
167 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
171 di->di_height = cpu_to_be16(1);
176 up_write(&ip->i_rw_mutex);
181 * build_height - Build a metadata tree of the requested height
182 * @ip: The GFS2 inode
183 * @height: The height to build to
189 static int build_height(struct inode *inode, struct metapath *mp, unsigned height)
191 struct gfs2_inode *ip = GFS2_I(inode);
192 unsigned new_height = height - ip->i_height;
193 struct buffer_head *dibh;
194 struct gfs2_dinode *di;
200 if (height <= ip->i_height)
203 error = gfs2_meta_inode_buffer(ip, &dibh);
207 for(n = 0; n < new_height; n++) {
208 bn = gfs2_alloc_meta(ip);
209 mp->mp_bh[n] = gfs2_meta_new(ip->i_gl, bn);
210 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[n], 1);
214 bn = mp->mp_bh[0]->b_blocknr;
215 if (new_height > 1) {
216 for(; n < new_height-1; n++) {
217 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN,
219 gfs2_buffer_clear_tail(mp->mp_bh[n],
220 sizeof(struct gfs2_meta_header));
221 bp = (__be64 *)(mp->mp_bh[n]->b_data +
222 sizeof(struct gfs2_meta_header));
223 *bp = cpu_to_be64(mp->mp_bh[n+1]->b_blocknr);
224 brelse(mp->mp_bh[n]);
228 gfs2_metatype_set(mp->mp_bh[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
229 gfs2_buffer_copy_tail(mp->mp_bh[n], sizeof(struct gfs2_meta_header),
230 dibh, sizeof(struct gfs2_dinode));
231 brelse(mp->mp_bh[n]);
232 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
233 di = (struct gfs2_dinode *)dibh->b_data;
234 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
235 *(__be64 *)(di + 1) = cpu_to_be64(bn);
236 ip->i_height += new_height;
237 ip->i_di.di_blocks += new_height;
238 gfs2_set_inode_blocks(&ip->i_inode);
239 di->di_height = cpu_to_be16(ip->i_height);
240 di->di_blocks = cpu_to_be64(ip->i_di.di_blocks);
246 * find_metapath - Find path through the metadata tree
247 * @ip: The inode pointer
248 * @mp: The metapath to return the result in
249 * @block: The disk block to look up
251 * This routine returns a struct metapath structure that defines a path
252 * through the metadata of inode "ip" to get to block "block".
255 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
256 * filesystem with a blocksize of 4096.
258 * find_metapath() would return a struct metapath structure set to:
259 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
260 * and mp_list[2] = 165.
262 * That means that in order to get to the block containing the byte at
263 * offset 101342453, we would load the indirect block pointed to by pointer
264 * 0 in the dinode. We would then load the indirect block pointed to by
265 * pointer 48 in that indirect block. We would then load the data block
266 * pointed to by pointer 165 in that indirect block.
268 * ----------------------------------------
273 * ----------------------------------------
277 * ----------------------------------------
281 * |0 5 6 7 8 9 0 1 2|
282 * ----------------------------------------
286 * ----------------------------------------
291 * ----------------------------------------
295 * ----------------------------------------
296 * | Data block containing offset |
300 * ----------------------------------------
304 static void find_metapath(struct gfs2_inode *ip, u64 block,
307 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
310 for (i = ip->i_height; i--;)
311 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
316 * metapointer - Return pointer to start of metadata in a buffer
317 * @height: The metadata height (0 = dinode)
320 * Return a pointer to the block number of the next height of the metadata
321 * tree given a buffer containing the pointer to the current height of the
325 static inline __be64 *metapointer(int *boundary, unsigned int height,
326 const struct metapath *mp)
328 struct buffer_head *bh = mp->mp_bh[height];
329 unsigned int head_size = (height > 0) ?
330 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
333 ptr = ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
334 if (ptr + 1 == (__be64 *)(bh->b_data + bh->b_size))
340 * lookup_block - Get the next metadata block in metadata tree
341 * @ip: The GFS2 inode
342 * @height: The height of the tree (0 = dinode)
344 * @create: Non-zero if we may create a new meatdata block
345 * @new: Used to indicate if we did create a new metadata block
346 * @block: the returned disk block number
348 * Given a metatree, complete to a particular height, checks to see if the next
349 * height of the tree exists. If not the next height of the tree is created.
350 * The block number of the next height of the metadata tree is returned.
354 static int lookup_block(struct gfs2_inode *ip, unsigned int height,
355 struct metapath *mp, int create,
356 int *new, u64 *block)
359 __be64 *ptr = metapointer(&boundary, height, mp);
362 *block = be64_to_cpu(*ptr);
371 if (height == ip->i_height - 1 && !gfs2_is_dir(ip))
372 *block = gfs2_alloc_data(ip);
374 *block = gfs2_alloc_meta(ip);
376 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[height], 1);
378 *ptr = cpu_to_be64(*block);
379 ip->i_di.di_blocks++;
380 gfs2_set_inode_blocks(&ip->i_inode);
386 static int lookup_metapath(struct inode *inode, struct metapath *mp,
387 int create, int *new, u64 *dblock)
389 struct buffer_head *bh;
390 struct gfs2_inode *ip = GFS2_I(inode);
391 unsigned int end_of_metadata = ip->i_height - 1;
393 int ret = gfs2_meta_inode_buffer(ip, &bh);
399 for (x = 0; x < end_of_metadata; x++) {
400 lookup_block(ip, x, mp, create, new, dblock);
404 ret = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &mp->mp_bh[x+1]);
409 return lookup_block(ip, end_of_metadata, mp, create, new, dblock);
412 static void release_metapath(struct metapath *mp)
416 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++)
418 brelse(mp->mp_bh[i]);
421 static inline void bmap_lock(struct inode *inode, int create)
423 struct gfs2_inode *ip = GFS2_I(inode);
425 down_write(&ip->i_rw_mutex);
427 down_read(&ip->i_rw_mutex);
430 static inline void bmap_unlock(struct inode *inode, int create)
432 struct gfs2_inode *ip = GFS2_I(inode);
434 up_write(&ip->i_rw_mutex);
436 up_read(&ip->i_rw_mutex);
440 * gfs2_block_map - Map a block from an inode to a disk block
442 * @lblock: The logical block number
443 * @bh_map: The bh to be mapped
445 * Find the block number on the current device which corresponds to an
446 * inode's block. If the block had to be created, "new" will be set.
451 int gfs2_block_map(struct inode *inode, sector_t lblock,
452 struct buffer_head *bh_map, int create)
454 struct gfs2_inode *ip = GFS2_I(inode);
455 struct gfs2_sbd *sdp = GFS2_SB(inode);
456 unsigned int bsize = sdp->sd_sb.sb_bsize;
461 unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
464 const u64 *arr = sdp->sd_heightsize;
467 if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
470 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
471 bmap_lock(inode, create);
472 clear_buffer_mapped(bh_map);
473 clear_buffer_new(bh_map);
474 clear_buffer_boundary(bh_map);
475 if (gfs2_is_dir(ip)) {
476 bsize = sdp->sd_jbsize;
477 arr = sdp->sd_jheightsize;
479 size = (lblock + 1) * bsize;
481 if (size > arr[ip->i_height]) {
482 u8 height = ip->i_height;
485 while (size > arr[height])
487 error = build_height(inode, &mp, height);
492 find_metapath(ip, lblock, &mp);
493 error = lookup_metapath(inode, &mp, create, &new, &dblock);
499 map_bh(bh_map, inode->i_sb, dblock);
501 set_buffer_boundary(bh_map);
503 gfs2_trans_add_bh(ip->i_gl, mp.mp_bh[0], 1);
504 gfs2_dinode_out(ip, mp.mp_bh[0]->b_data);
505 set_buffer_new(bh_map);
508 while(--maxlen && !buffer_boundary(bh_map)) {
510 mp.mp_list[ip->i_height - 1]++;
511 boundary = lookup_block(ip, ip->i_height - 1, &mp, 0, &new, &eblock);
512 if (eblock != ++dblock)
514 bh_map->b_size += (1 << inode->i_blkbits);
516 set_buffer_boundary(bh_map);
522 release_metapath(&mp);
523 bmap_unlock(inode, create);
528 * Deprecated: do not use in new code
530 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
532 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
540 bh.b_size = 1 << (inode->i_blkbits + 5);
541 ret = gfs2_block_map(inode, lblock, &bh, create);
542 *extlen = bh.b_size >> inode->i_blkbits;
543 *dblock = bh.b_blocknr;
552 * recursive_scan - recursively scan through the end of a file
554 * @dibh: the dinode buffer
555 * @mp: the path through the metadata to the point to start
556 * @height: the height the recursion is at
557 * @block: the indirect block to look at
558 * @first: 1 if this is the first block
559 * @bc: the call to make for each piece of metadata
560 * @data: data opaque to this function to pass to @bc
562 * When this is first called @height and @block should be zero and
563 * @first should be 1.
568 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
569 struct metapath *mp, unsigned int height,
570 u64 block, int first, block_call_t bc,
573 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
574 struct buffer_head *bh = NULL;
575 __be64 *top, *bottom;
578 int mh_size = sizeof(struct gfs2_meta_header);
581 error = gfs2_meta_inode_buffer(ip, &bh);
586 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
587 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
589 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
593 top = (__be64 *)(bh->b_data + mh_size) +
594 (first ? mp->mp_list[height] : 0);
596 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
599 error = bc(ip, dibh, bh, top, bottom, height, data);
603 if (height < ip->i_height - 1)
604 for (; top < bottom; top++, first = 0) {
608 bn = be64_to_cpu(*top);
610 error = recursive_scan(ip, dibh, mp, height + 1, bn,
622 * do_strip - Look for a layer a particular layer of the file and strip it off
624 * @dibh: the dinode buffer
625 * @bh: A buffer of pointers
626 * @top: The first pointer in the buffer
627 * @bottom: One more than the last pointer
628 * @height: the height this buffer is at
629 * @data: a pointer to a struct strip_mine
634 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
635 struct buffer_head *bh, __be64 *top, __be64 *bottom,
636 unsigned int height, void *data)
638 struct strip_mine *sm = data;
639 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
640 struct gfs2_rgrp_list rlist;
644 unsigned int rg_blocks = 0;
646 unsigned int revokes = 0;
653 if (height != sm->sm_height)
661 metadata = (height != ip->i_height - 1);
663 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
665 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
669 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
673 for (p = top; p < bottom; p++) {
677 bn = be64_to_cpu(*p);
679 if (bstart + blen == bn)
683 gfs2_rlist_add(sdp, &rlist, bstart);
691 gfs2_rlist_add(sdp, &rlist, bstart);
693 goto out; /* Nothing to do */
695 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
697 for (x = 0; x < rlist.rl_rgrps; x++) {
698 struct gfs2_rgrpd *rgd;
699 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
700 rg_blocks += rgd->rd_length;
703 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
707 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
708 RES_INDIRECT + RES_STATFS + RES_QUOTA,
713 down_write(&ip->i_rw_mutex);
715 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
716 gfs2_trans_add_bh(ip->i_gl, bh, 1);
721 for (p = top; p < bottom; p++) {
725 bn = be64_to_cpu(*p);
727 if (bstart + blen == bn)
732 gfs2_free_meta(ip, bstart, blen);
734 gfs2_free_data(ip, bstart, blen);
742 if (!ip->i_di.di_blocks)
743 gfs2_consist_inode(ip);
744 ip->i_di.di_blocks--;
745 gfs2_set_inode_blocks(&ip->i_inode);
749 gfs2_free_meta(ip, bstart, blen);
751 gfs2_free_data(ip, bstart, blen);
754 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
756 gfs2_dinode_out(ip, dibh->b_data);
758 up_write(&ip->i_rw_mutex);
763 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
765 gfs2_rlist_free(&rlist);
767 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
772 * do_grow - Make a file look bigger than it is
774 * @size: the size to set the file to
776 * Called with an exclusive lock on @ip.
781 static int do_grow(struct gfs2_inode *ip, u64 size)
783 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
784 struct gfs2_alloc *al;
785 struct buffer_head *dibh;
788 al = gfs2_alloc_get(ip);
790 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
794 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
798 al->al_requested = sdp->sd_max_height + RES_DATA;
800 error = gfs2_inplace_reserve(ip);
804 error = gfs2_trans_begin(sdp,
805 sdp->sd_max_height + al->al_rgd->rd_length +
806 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
810 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
811 const u64 *arr = sdp->sd_heightsize;
812 if (gfs2_is_stuffed(ip)) {
813 error = gfs2_unstuff_dinode(ip, NULL);
818 down_write(&ip->i_rw_mutex);
819 if (size > arr[ip->i_height]) {
821 u8 height = ip->i_height;
822 while(size > arr[height])
824 error = build_height(&ip->i_inode, &mp, height);
826 up_write(&ip->i_rw_mutex);
831 ip->i_di.di_size = size;
832 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
834 error = gfs2_meta_inode_buffer(ip, &dibh);
838 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
839 gfs2_dinode_out(ip, dibh->b_data);
845 gfs2_inplace_release(ip);
847 gfs2_quota_unlock(ip);
855 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
857 * This is partly borrowed from ext3.
859 static int gfs2_block_truncate_page(struct address_space *mapping)
861 struct inode *inode = mapping->host;
862 struct gfs2_inode *ip = GFS2_I(inode);
863 loff_t from = inode->i_size;
864 unsigned long index = from >> PAGE_CACHE_SHIFT;
865 unsigned offset = from & (PAGE_CACHE_SIZE-1);
866 unsigned blocksize, iblock, length, pos;
867 struct buffer_head *bh;
871 page = grab_cache_page(mapping, index);
875 blocksize = inode->i_sb->s_blocksize;
876 length = blocksize - (offset & (blocksize - 1));
877 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
879 if (!page_has_buffers(page))
880 create_empty_buffers(page, blocksize, 0);
882 /* Find the buffer that contains "offset" */
883 bh = page_buffers(page);
885 while (offset >= pos) {
886 bh = bh->b_this_page;
893 if (!buffer_mapped(bh)) {
894 gfs2_block_map(inode, iblock, bh, 0);
895 /* unmapped? It's a hole - nothing to do */
896 if (!buffer_mapped(bh))
900 /* Ok, it's mapped. Make sure it's up-to-date */
901 if (PageUptodate(page))
902 set_buffer_uptodate(bh);
904 if (!buffer_uptodate(bh)) {
906 ll_rw_block(READ, 1, &bh);
908 /* Uhhuh. Read error. Complain and punt. */
909 if (!buffer_uptodate(bh))
914 if (!gfs2_is_writeback(ip))
915 gfs2_trans_add_bh(ip->i_gl, bh, 0);
917 zero_user(page, offset, length);
921 page_cache_release(page);
925 static int trunc_start(struct gfs2_inode *ip, u64 size)
927 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
928 struct buffer_head *dibh;
929 int journaled = gfs2_is_jdata(ip);
932 error = gfs2_trans_begin(sdp,
933 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
937 error = gfs2_meta_inode_buffer(ip, &dibh);
941 if (gfs2_is_stuffed(ip)) {
942 ip->i_di.di_size = size;
943 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
944 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
945 gfs2_dinode_out(ip, dibh->b_data);
946 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
950 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
951 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
954 ip->i_di.di_size = size;
955 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
956 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
957 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
958 gfs2_dinode_out(ip, dibh->b_data);
969 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
971 unsigned int height = ip->i_height;
979 lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
981 find_metapath(ip, lblock, &mp);
984 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
989 struct strip_mine sm;
990 sm.sm_first = !!size;
991 sm.sm_height = height;
993 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
998 gfs2_quota_unhold(ip);
1005 static int trunc_end(struct gfs2_inode *ip)
1007 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1008 struct buffer_head *dibh;
1011 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1015 down_write(&ip->i_rw_mutex);
1017 error = gfs2_meta_inode_buffer(ip, &dibh);
1021 if (!ip->i_di.di_size) {
1023 ip->i_goal = ip->i_no_addr;
1024 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1026 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1027 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1029 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1030 gfs2_dinode_out(ip, dibh->b_data);
1034 up_write(&ip->i_rw_mutex);
1035 gfs2_trans_end(sdp);
1040 * do_shrink - make a file smaller
1042 * @size: the size to make the file
1043 * @truncator: function to truncate the last partial block
1045 * Called with an exclusive lock on @ip.
1050 static int do_shrink(struct gfs2_inode *ip, u64 size)
1054 error = trunc_start(ip, size);
1060 error = trunc_dealloc(ip, size);
1062 error = trunc_end(ip);
1067 static int do_touch(struct gfs2_inode *ip, u64 size)
1069 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1070 struct buffer_head *dibh;
1073 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1077 down_write(&ip->i_rw_mutex);
1079 error = gfs2_meta_inode_buffer(ip, &dibh);
1083 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1084 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1085 gfs2_dinode_out(ip, dibh->b_data);
1089 up_write(&ip->i_rw_mutex);
1090 gfs2_trans_end(sdp);
1095 * gfs2_truncatei - make a file a given size
1097 * @size: the size to make the file
1098 * @truncator: function to truncate the last partial block
1100 * The file size can grow, shrink, or stay the same size.
1105 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1109 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1112 if (size > ip->i_di.di_size)
1113 error = do_grow(ip, size);
1114 else if (size < ip->i_di.di_size)
1115 error = do_shrink(ip, size);
1117 /* update time stamps */
1118 error = do_touch(ip, size);
1123 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1126 error = trunc_dealloc(ip, ip->i_di.di_size);
1128 error = trunc_end(ip);
1132 int gfs2_file_dealloc(struct gfs2_inode *ip)
1134 return trunc_dealloc(ip, 0);
1138 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1140 * @len: the number of bytes to be written to the file
1141 * @data_blocks: returns the number of data blocks required
1142 * @ind_blocks: returns the number of indirect blocks required
1146 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1147 unsigned int *data_blocks, unsigned int *ind_blocks)
1149 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1152 if (gfs2_is_dir(ip)) {
1153 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1154 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1156 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1157 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1160 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1161 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1167 * gfs2_write_alloc_required - figure out if a write will require an allocation
1168 * @ip: the file being written to
1169 * @offset: the offset to write to
1170 * @len: the number of bytes being written
1171 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1176 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1177 unsigned int len, int *alloc_required)
1179 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1180 struct buffer_head bh;
1182 u64 lblock, lblock_stop, size;
1184 *alloc_required = 0;
1189 if (gfs2_is_stuffed(ip)) {
1191 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1192 *alloc_required = 1;
1196 *alloc_required = 1;
1197 shift = sdp->sd_sb.sb_bsize_shift;
1198 if (gfs2_is_dir(ip)) {
1199 unsigned int bsize = sdp->sd_jbsize;
1201 do_div(lblock, bsize);
1202 lblock_stop = offset + len + bsize - 1;
1203 do_div(lblock_stop, bsize);
1205 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1206 lblock = offset >> shift;
1207 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1208 if (lblock_stop > end_of_file)
1212 size = (lblock_stop - lblock) << shift;
1216 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1217 if (!buffer_mapped(&bh))
1220 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1223 *alloc_required = 0;