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_disksize, 0,
80 PAGE_CACHE_SIZE - ip->i_disksize);
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_disksize) {
136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */
140 block = gfs2_alloc_block(ip, &n);
142 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
143 error = gfs2_dir_get_new_buffer(ip, block, &bh);
146 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
147 dibh, sizeof(struct gfs2_dinode));
150 error = gfs2_unstuffer_page(ip, dibh, block, page);
156 /* Set up the pointer to the new block */
158 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
159 di = (struct gfs2_dinode *)dibh->b_data;
160 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
162 if (ip->i_disksize) {
163 *(__be64 *)(di + 1) = cpu_to_be64(block);
164 gfs2_add_inode_blocks(&ip->i_inode, 1);
165 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
169 di->di_height = cpu_to_be16(1);
174 up_write(&ip->i_rw_mutex);
180 * find_metapath - Find path through the metadata tree
181 * @sdp: The superblock
182 * @mp: The metapath to return the result in
183 * @block: The disk block to look up
184 * @height: The pre-calculated height of the metadata tree
186 * This routine returns a struct metapath structure that defines a path
187 * through the metadata of inode "ip" to get to block "block".
190 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
191 * filesystem with a blocksize of 4096.
193 * find_metapath() would return a struct metapath structure set to:
194 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
195 * and mp_list[2] = 165.
197 * That means that in order to get to the block containing the byte at
198 * offset 101342453, we would load the indirect block pointed to by pointer
199 * 0 in the dinode. We would then load the indirect block pointed to by
200 * pointer 48 in that indirect block. We would then load the data block
201 * pointed to by pointer 165 in that indirect block.
203 * ----------------------------------------
208 * ----------------------------------------
212 * ----------------------------------------
216 * |0 5 6 7 8 9 0 1 2|
217 * ----------------------------------------
221 * ----------------------------------------
226 * ----------------------------------------
230 * ----------------------------------------
231 * | Data block containing offset |
235 * ----------------------------------------
239 static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
240 struct metapath *mp, unsigned int height)
244 for (i = height; i--;)
245 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
249 static inline unsigned int metapath_branch_start(const struct metapath *mp)
251 if (mp->mp_list[0] == 0)
257 * metapointer - Return pointer to start of metadata in a buffer
258 * @height: The metadata height (0 = dinode)
261 * Return a pointer to the block number of the next height of the metadata
262 * tree given a buffer containing the pointer to the current height of the
266 static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
268 struct buffer_head *bh = mp->mp_bh[height];
269 unsigned int head_size = (height > 0) ?
270 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
271 return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
275 * lookup_metapath - Walk the metadata tree to a specific point
279 * Assumes that the inode's buffer has already been looked up and
280 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
281 * by find_metapath().
283 * If this function encounters part of the tree which has not been
284 * allocated, it returns the current height of the tree at the point
285 * at which it found the unallocated block. Blocks which are found are
286 * added to the mp->mp_bh[] list.
288 * Returns: error or height of metadata tree
291 static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
293 unsigned int end_of_metadata = ip->i_height - 1;
299 for (x = 0; x < end_of_metadata; x++) {
300 ptr = metapointer(x, mp);
301 dblock = be64_to_cpu(*ptr);
305 ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
313 static inline void release_metapath(struct metapath *mp)
317 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
318 if (mp->mp_bh[i] == NULL)
320 brelse(mp->mp_bh[i]);
325 * gfs2_extent_length - Returns length of an extent of blocks
326 * @start: Start of the buffer
327 * @len: Length of the buffer in bytes
328 * @ptr: Current position in the buffer
329 * @limit: Max extent length to return (0 = unlimited)
330 * @eob: Set to 1 if we hit "end of block"
332 * If the first block is zero (unallocated) it will return the number of
333 * unallocated blocks in the extent, otherwise it will return the number
334 * of contiguous blocks in the extent.
336 * Returns: The length of the extent (minimum of one block)
339 static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
341 const __be64 *end = (start + len);
342 const __be64 *first = ptr;
343 u64 d = be64_to_cpu(*ptr);
350 if (limit && --limit == 0)
354 } while(be64_to_cpu(*ptr) == d);
357 return (ptr - first);
360 static inline void bmap_lock(struct gfs2_inode *ip, int create)
363 down_write(&ip->i_rw_mutex);
365 down_read(&ip->i_rw_mutex);
368 static inline void bmap_unlock(struct gfs2_inode *ip, int create)
371 up_write(&ip->i_rw_mutex);
373 up_read(&ip->i_rw_mutex);
376 static inline __be64 *gfs2_indirect_init(struct metapath *mp,
377 struct gfs2_glock *gl, unsigned int i,
378 unsigned offset, u64 bn)
380 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
381 ((i > 1) ? sizeof(struct gfs2_meta_header) :
382 sizeof(struct gfs2_dinode)));
384 BUG_ON(mp->mp_bh[i] != NULL);
385 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
386 gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
387 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
388 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
390 *ptr = cpu_to_be64(bn);
396 ALLOC_GROW_DEPTH = 1,
397 ALLOC_GROW_HEIGHT = 2,
398 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
402 * gfs2_bmap_alloc - Build a metadata tree of the requested height
403 * @inode: The GFS2 inode
404 * @lblock: The logical starting block of the extent
405 * @bh_map: This is used to return the mapping details
407 * @sheight: The starting height (i.e. whats already mapped)
408 * @height: The height to build to
409 * @maxlen: The max number of data blocks to alloc
411 * In this routine we may have to alloc:
412 * i) Indirect blocks to grow the metadata tree height
413 * ii) Indirect blocks to fill in lower part of the metadata tree
416 * The function is in two parts. The first part works out the total
417 * number of blocks which we need. The second part does the actual
418 * allocation asking for an extent at a time (if enough contiguous free
419 * blocks are available, there will only be one request per bmap call)
420 * and uses the state machine to initialise the blocks in order.
422 * Returns: errno on error
425 static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
426 struct buffer_head *bh_map, struct metapath *mp,
427 const unsigned int sheight,
428 const unsigned int height,
429 const unsigned int maxlen)
431 struct gfs2_inode *ip = GFS2_I(inode);
432 struct gfs2_sbd *sdp = GFS2_SB(inode);
433 struct buffer_head *dibh = mp->mp_bh[0];
435 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
437 unsigned ptrs_per_blk;
438 const unsigned end_of_metadata = height - 1;
440 enum alloc_state state;
445 BUG_ON(dibh == NULL);
447 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
449 if (height == sheight) {
450 struct buffer_head *bh;
451 /* Bottom indirect block exists, find unalloced extent size */
452 ptr = metapointer(end_of_metadata, mp);
453 bh = mp->mp_bh[end_of_metadata];
454 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
459 /* Need to allocate indirect blocks */
460 ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
461 dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
462 if (height == ip->i_height) {
463 /* Writing into existing tree, extend tree down */
464 iblks = height - sheight;
465 state = ALLOC_GROW_DEPTH;
467 /* Building up tree height */
468 state = ALLOC_GROW_HEIGHT;
469 iblks = height - ip->i_height;
470 branch_start = metapath_branch_start(mp);
471 iblks += (height - branch_start);
475 /* start of the second part of the function (state machine) */
477 blks = dblks + iblks;
481 bn = gfs2_alloc_block(ip, &n);
483 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
484 gfs2_trans_add_unrevoke(sdp, bn, n);
486 /* Growing height of tree */
487 case ALLOC_GROW_HEIGHT:
489 ptr = (__be64 *)(dibh->b_data +
490 sizeof(struct gfs2_dinode));
493 for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
494 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
495 if (i - 1 == height - ip->i_height) {
497 gfs2_buffer_copy_tail(mp->mp_bh[i],
498 sizeof(struct gfs2_meta_header),
499 dibh, sizeof(struct gfs2_dinode));
500 gfs2_buffer_clear_tail(dibh,
501 sizeof(struct gfs2_dinode) +
503 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
504 sizeof(struct gfs2_meta_header));
506 state = ALLOC_GROW_DEPTH;
507 for(i = branch_start; i < height; i++) {
508 if (mp->mp_bh[i] == NULL)
510 brelse(mp->mp_bh[i]);
517 /* Branching from existing tree */
518 case ALLOC_GROW_DEPTH:
519 if (i > 1 && i < height)
520 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
521 for (; i < height && n > 0; i++, n--)
522 gfs2_indirect_init(mp, ip->i_gl, i,
523 mp->mp_list[i-1], bn++);
528 /* Tree complete, adding data blocks */
531 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
532 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
534 ptr = metapointer(end_of_metadata, mp);
537 *ptr++ = cpu_to_be64(bn++);
540 } while (state != ALLOC_DATA);
542 ip->i_height = height;
543 gfs2_add_inode_blocks(&ip->i_inode, alloced);
544 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
545 map_bh(bh_map, inode->i_sb, dblock);
546 bh_map->b_size = dblks << inode->i_blkbits;
547 set_buffer_new(bh_map);
552 * gfs2_block_map - Map a block from an inode to a disk block
554 * @lblock: The logical block number
555 * @bh_map: The bh to be mapped
556 * @create: True if its ok to alloc blocks to satify the request
558 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
559 * read of metadata will be required before the next block can be
560 * mapped. Sets buffer_new() if new blocks were allocated.
565 int gfs2_block_map(struct inode *inode, sector_t lblock,
566 struct buffer_head *bh_map, int create)
568 struct gfs2_inode *ip = GFS2_I(inode);
569 struct gfs2_sbd *sdp = GFS2_SB(inode);
570 unsigned int bsize = sdp->sd_sb.sb_bsize;
571 const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
572 const u64 *arr = sdp->sd_heightsize;
579 struct buffer_head *bh;
584 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
585 bmap_lock(ip, create);
586 clear_buffer_mapped(bh_map);
587 clear_buffer_new(bh_map);
588 clear_buffer_boundary(bh_map);
589 if (gfs2_is_dir(ip)) {
590 bsize = sdp->sd_jbsize;
591 arr = sdp->sd_jheightsize;
594 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
598 height = ip->i_height;
599 size = (lblock + 1) * bsize;
600 while (size > arr[height])
602 find_metapath(sdp, lblock, &mp, height);
604 if (height > ip->i_height || gfs2_is_stuffed(ip))
606 ret = lookup_metapath(ip, &mp);
609 if (ret != ip->i_height)
611 ptr = metapointer(ip->i_height - 1, &mp);
614 map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
615 bh = mp.mp_bh[ip->i_height - 1];
616 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
617 bh_map->b_size = (len << inode->i_blkbits);
619 set_buffer_boundary(bh_map);
622 release_metapath(&mp);
623 bmap_unlock(ip, create);
627 /* All allocations are done here, firstly check create flag */
629 BUG_ON(gfs2_is_stuffed(ip));
634 /* At this point ret is the tree depth of already allocated blocks */
635 ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
640 * Deprecated: do not use in new code
642 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
644 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
652 bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
653 ret = gfs2_block_map(inode, lblock, &bh, create);
654 *extlen = bh.b_size >> inode->i_blkbits;
655 *dblock = bh.b_blocknr;
664 * recursive_scan - recursively scan through the end of a file
666 * @dibh: the dinode buffer
667 * @mp: the path through the metadata to the point to start
668 * @height: the height the recursion is at
669 * @block: the indirect block to look at
670 * @first: 1 if this is the first block
671 * @bc: the call to make for each piece of metadata
672 * @data: data opaque to this function to pass to @bc
674 * When this is first called @height and @block should be zero and
675 * @first should be 1.
680 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
681 struct metapath *mp, unsigned int height,
682 u64 block, int first, block_call_t bc,
685 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
686 struct buffer_head *bh = NULL;
687 __be64 *top, *bottom;
690 int mh_size = sizeof(struct gfs2_meta_header);
693 error = gfs2_meta_inode_buffer(ip, &bh);
698 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
699 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
701 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
705 top = (__be64 *)(bh->b_data + mh_size) +
706 (first ? mp->mp_list[height] : 0);
708 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
711 error = bc(ip, dibh, bh, top, bottom, height, data);
715 if (height < ip->i_height - 1)
716 for (; top < bottom; top++, first = 0) {
720 bn = be64_to_cpu(*top);
722 error = recursive_scan(ip, dibh, mp, height + 1, bn,
734 * do_strip - Look for a layer a particular layer of the file and strip it off
736 * @dibh: the dinode buffer
737 * @bh: A buffer of pointers
738 * @top: The first pointer in the buffer
739 * @bottom: One more than the last pointer
740 * @height: the height this buffer is at
741 * @data: a pointer to a struct strip_mine
746 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
747 struct buffer_head *bh, __be64 *top, __be64 *bottom,
748 unsigned int height, void *data)
750 struct strip_mine *sm = data;
751 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
752 struct gfs2_rgrp_list rlist;
756 unsigned int rg_blocks = 0;
758 unsigned int revokes = 0;
765 if (height != sm->sm_height)
773 metadata = (height != ip->i_height - 1);
775 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
777 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
781 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
785 for (p = top; p < bottom; p++) {
789 bn = be64_to_cpu(*p);
791 if (bstart + blen == bn)
795 gfs2_rlist_add(sdp, &rlist, bstart);
803 gfs2_rlist_add(sdp, &rlist, bstart);
805 goto out; /* Nothing to do */
807 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
809 for (x = 0; x < rlist.rl_rgrps; x++) {
810 struct gfs2_rgrpd *rgd;
811 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
812 rg_blocks += rgd->rd_length;
815 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
819 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
820 RES_INDIRECT + RES_STATFS + RES_QUOTA,
825 down_write(&ip->i_rw_mutex);
827 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
828 gfs2_trans_add_bh(ip->i_gl, bh, 1);
833 for (p = top; p < bottom; p++) {
837 bn = be64_to_cpu(*p);
839 if (bstart + blen == bn)
844 gfs2_free_meta(ip, bstart, blen);
846 gfs2_free_data(ip, bstart, blen);
854 gfs2_add_inode_blocks(&ip->i_inode, -1);
858 gfs2_free_meta(ip, bstart, blen);
860 gfs2_free_data(ip, bstart, blen);
863 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
865 gfs2_dinode_out(ip, dibh->b_data);
867 up_write(&ip->i_rw_mutex);
872 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
874 gfs2_rlist_free(&rlist);
876 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
881 * do_grow - Make a file look bigger than it is
883 * @size: the size to set the file to
885 * Called with an exclusive lock on @ip.
890 static int do_grow(struct gfs2_inode *ip, u64 size)
892 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
893 struct gfs2_alloc *al;
894 struct buffer_head *dibh;
897 al = gfs2_alloc_get(ip);
901 error = gfs2_quota_lock_check(ip);
905 al->al_requested = sdp->sd_max_height + RES_DATA;
907 error = gfs2_inplace_reserve(ip);
911 error = gfs2_trans_begin(sdp,
912 sdp->sd_max_height + al->al_rgd->rd_length +
913 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
917 error = gfs2_meta_inode_buffer(ip, &dibh);
921 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
922 if (gfs2_is_stuffed(ip)) {
923 error = gfs2_unstuff_dinode(ip, NULL);
929 ip->i_disksize = size;
930 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
931 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
932 gfs2_dinode_out(ip, dibh->b_data);
939 gfs2_inplace_release(ip);
941 gfs2_quota_unlock(ip);
949 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
951 * This is partly borrowed from ext3.
953 static int gfs2_block_truncate_page(struct address_space *mapping)
955 struct inode *inode = mapping->host;
956 struct gfs2_inode *ip = GFS2_I(inode);
957 loff_t from = inode->i_size;
958 unsigned long index = from >> PAGE_CACHE_SHIFT;
959 unsigned offset = from & (PAGE_CACHE_SIZE-1);
960 unsigned blocksize, iblock, length, pos;
961 struct buffer_head *bh;
965 page = grab_cache_page(mapping, index);
969 blocksize = inode->i_sb->s_blocksize;
970 length = blocksize - (offset & (blocksize - 1));
971 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
973 if (!page_has_buffers(page))
974 create_empty_buffers(page, blocksize, 0);
976 /* Find the buffer that contains "offset" */
977 bh = page_buffers(page);
979 while (offset >= pos) {
980 bh = bh->b_this_page;
987 if (!buffer_mapped(bh)) {
988 gfs2_block_map(inode, iblock, bh, 0);
989 /* unmapped? It's a hole - nothing to do */
990 if (!buffer_mapped(bh))
994 /* Ok, it's mapped. Make sure it's up-to-date */
995 if (PageUptodate(page))
996 set_buffer_uptodate(bh);
998 if (!buffer_uptodate(bh)) {
1000 ll_rw_block(READ, 1, &bh);
1002 /* Uhhuh. Read error. Complain and punt. */
1003 if (!buffer_uptodate(bh))
1008 if (!gfs2_is_writeback(ip))
1009 gfs2_trans_add_bh(ip->i_gl, bh, 0);
1011 zero_user(page, offset, length);
1015 page_cache_release(page);
1019 static int trunc_start(struct gfs2_inode *ip, u64 size)
1021 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1022 struct buffer_head *dibh;
1023 int journaled = gfs2_is_jdata(ip);
1026 error = gfs2_trans_begin(sdp,
1027 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
1031 error = gfs2_meta_inode_buffer(ip, &dibh);
1035 if (gfs2_is_stuffed(ip)) {
1036 ip->i_disksize = size;
1037 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1038 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1039 gfs2_dinode_out(ip, dibh->b_data);
1040 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
1044 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
1045 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
1048 ip->i_disksize = size;
1049 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1050 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
1051 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1052 gfs2_dinode_out(ip, dibh->b_data);
1059 gfs2_trans_end(sdp);
1063 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
1065 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1066 unsigned int height = ip->i_height;
1074 lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
1076 find_metapath(sdp, lblock, &mp, ip->i_height);
1077 if (!gfs2_alloc_get(ip))
1080 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1085 struct strip_mine sm;
1086 sm.sm_first = !!size;
1087 sm.sm_height = height;
1089 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1094 gfs2_quota_unhold(ip);
1101 static int trunc_end(struct gfs2_inode *ip)
1103 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1104 struct buffer_head *dibh;
1107 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1111 down_write(&ip->i_rw_mutex);
1113 error = gfs2_meta_inode_buffer(ip, &dibh);
1117 if (!ip->i_disksize) {
1119 ip->i_goal = ip->i_no_addr;
1120 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1122 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1123 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
1125 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1126 gfs2_dinode_out(ip, dibh->b_data);
1130 up_write(&ip->i_rw_mutex);
1131 gfs2_trans_end(sdp);
1136 * do_shrink - make a file smaller
1138 * @size: the size to make the file
1139 * @truncator: function to truncate the last partial block
1141 * Called with an exclusive lock on @ip.
1146 static int do_shrink(struct gfs2_inode *ip, u64 size)
1150 error = trunc_start(ip, size);
1156 error = trunc_dealloc(ip, size);
1158 error = trunc_end(ip);
1163 static int do_touch(struct gfs2_inode *ip, u64 size)
1165 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1166 struct buffer_head *dibh;
1169 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1173 down_write(&ip->i_rw_mutex);
1175 error = gfs2_meta_inode_buffer(ip, &dibh);
1179 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1180 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1181 gfs2_dinode_out(ip, dibh->b_data);
1185 up_write(&ip->i_rw_mutex);
1186 gfs2_trans_end(sdp);
1191 * gfs2_truncatei - make a file a given size
1193 * @size: the size to make the file
1194 * @truncator: function to truncate the last partial block
1196 * The file size can grow, shrink, or stay the same size.
1201 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1205 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1208 if (size > ip->i_disksize)
1209 error = do_grow(ip, size);
1210 else if (size < ip->i_disksize)
1211 error = do_shrink(ip, size);
1213 /* update time stamps */
1214 error = do_touch(ip, size);
1219 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1222 error = trunc_dealloc(ip, ip->i_disksize);
1224 error = trunc_end(ip);
1228 int gfs2_file_dealloc(struct gfs2_inode *ip)
1230 return trunc_dealloc(ip, 0);
1234 * gfs2_write_alloc_required - figure out if a write will require an allocation
1235 * @ip: the file being written to
1236 * @offset: the offset to write to
1237 * @len: the number of bytes being written
1238 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1243 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1244 unsigned int len, int *alloc_required)
1246 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1247 struct buffer_head bh;
1249 u64 lblock, lblock_stop, size;
1252 *alloc_required = 0;
1257 if (gfs2_is_stuffed(ip)) {
1259 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1260 *alloc_required = 1;
1264 *alloc_required = 1;
1265 shift = sdp->sd_sb.sb_bsize_shift;
1266 BUG_ON(gfs2_is_dir(ip));
1267 end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift;
1268 lblock = offset >> shift;
1269 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1270 if (lblock_stop > end_of_file)
1273 size = (lblock_stop - lblock) << shift;
1277 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1278 if (!buffer_mapped(&bh))
1281 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1284 *alloc_required = 0;