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>
28 #include "trace_gfs2.h"
30 /* This doesn't need to be that large as max 64 bit pointers in a 4k
31 * block is 512, so __u16 is fine for that. It saves stack space to
35 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
36 __u16 mp_list[GFS2_MAX_META_HEIGHT];
39 typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
40 struct buffer_head *bh, __be64 *top,
41 __be64 *bottom, unsigned int height,
46 unsigned int sm_height;
50 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
52 * @dibh: the dinode buffer
53 * @block: the block number that was allocated
54 * @private: any locked page held by the caller process
59 static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
60 u64 block, struct page *page)
62 struct inode *inode = &ip->i_inode;
63 struct buffer_head *bh;
66 if (!page || page->index) {
67 page = grab_cache_page(inode->i_mapping, 0);
73 if (!PageUptodate(page)) {
74 void *kaddr = kmap(page);
75 u64 dsize = i_size_read(inode);
77 if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
78 dsize = dibh->b_size - sizeof(struct gfs2_dinode);
80 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
81 memset(kaddr + dsize, 0, PAGE_CACHE_SIZE - dsize);
84 SetPageUptodate(page);
87 if (!page_has_buffers(page))
88 create_empty_buffers(page, 1 << inode->i_blkbits,
91 bh = page_buffers(page);
93 if (!buffer_mapped(bh))
94 map_bh(bh, inode->i_sb, block);
96 set_buffer_uptodate(bh);
97 if (!gfs2_is_jdata(ip))
98 mark_buffer_dirty(bh);
99 if (!gfs2_is_writeback(ip))
100 gfs2_trans_add_bh(ip->i_gl, bh, 0);
104 page_cache_release(page);
111 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
112 * @ip: The GFS2 inode to unstuff
113 * @unstuffer: the routine that handles unstuffing a non-zero length file
114 * @private: private data for the unstuffer
116 * This routine unstuffs a dinode and returns it to a "normal" state such
117 * that the height can be grown in the traditional way.
122 int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
124 struct buffer_head *bh, *dibh;
125 struct gfs2_dinode *di;
127 int isdir = gfs2_is_dir(ip);
130 down_write(&ip->i_rw_mutex);
132 error = gfs2_meta_inode_buffer(ip, &dibh);
136 if (ip->i_disksize) {
137 /* Get a free block, fill it with the stuffed data,
138 and write it out to disk */
141 error = gfs2_alloc_block(ip, &block, &n);
145 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
146 error = gfs2_dir_get_new_buffer(ip, block, &bh);
149 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
150 dibh, sizeof(struct gfs2_dinode));
153 error = gfs2_unstuffer_page(ip, dibh, block, page);
159 /* Set up the pointer to the new block */
161 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
162 di = (struct gfs2_dinode *)dibh->b_data;
163 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
165 if (ip->i_disksize) {
166 *(__be64 *)(di + 1) = cpu_to_be64(block);
167 gfs2_add_inode_blocks(&ip->i_inode, 1);
168 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
172 di->di_height = cpu_to_be16(1);
177 up_write(&ip->i_rw_mutex);
183 * find_metapath - Find path through the metadata tree
184 * @sdp: The superblock
185 * @mp: The metapath to return the result in
186 * @block: The disk block to look up
187 * @height: The pre-calculated height of the metadata tree
189 * This routine returns a struct metapath structure that defines a path
190 * through the metadata of inode "ip" to get to block "block".
193 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
194 * filesystem with a blocksize of 4096.
196 * find_metapath() would return a struct metapath structure set to:
197 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
198 * and mp_list[2] = 165.
200 * That means that in order to get to the block containing the byte at
201 * offset 101342453, we would load the indirect block pointed to by pointer
202 * 0 in the dinode. We would then load the indirect block pointed to by
203 * pointer 48 in that indirect block. We would then load the data block
204 * pointed to by pointer 165 in that indirect block.
206 * ----------------------------------------
211 * ----------------------------------------
215 * ----------------------------------------
219 * |0 5 6 7 8 9 0 1 2|
220 * ----------------------------------------
224 * ----------------------------------------
229 * ----------------------------------------
233 * ----------------------------------------
234 * | Data block containing offset |
238 * ----------------------------------------
242 static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
243 struct metapath *mp, unsigned int height)
247 for (i = height; i--;)
248 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
252 static inline unsigned int metapath_branch_start(const struct metapath *mp)
254 if (mp->mp_list[0] == 0)
260 * metapointer - Return pointer to start of metadata in a buffer
261 * @height: The metadata height (0 = dinode)
264 * Return a pointer to the block number of the next height of the metadata
265 * tree given a buffer containing the pointer to the current height of the
269 static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
271 struct buffer_head *bh = mp->mp_bh[height];
272 unsigned int head_size = (height > 0) ?
273 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
274 return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
278 * lookup_metapath - Walk the metadata tree to a specific point
282 * Assumes that the inode's buffer has already been looked up and
283 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
284 * by find_metapath().
286 * If this function encounters part of the tree which has not been
287 * allocated, it returns the current height of the tree at the point
288 * at which it found the unallocated block. Blocks which are found are
289 * added to the mp->mp_bh[] list.
291 * Returns: error or height of metadata tree
294 static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
296 unsigned int end_of_metadata = ip->i_height - 1;
302 for (x = 0; x < end_of_metadata; x++) {
303 ptr = metapointer(x, mp);
304 dblock = be64_to_cpu(*ptr);
308 ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
316 static inline void release_metapath(struct metapath *mp)
320 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
321 if (mp->mp_bh[i] == NULL)
323 brelse(mp->mp_bh[i]);
328 * gfs2_extent_length - Returns length of an extent of blocks
329 * @start: Start of the buffer
330 * @len: Length of the buffer in bytes
331 * @ptr: Current position in the buffer
332 * @limit: Max extent length to return (0 = unlimited)
333 * @eob: Set to 1 if we hit "end of block"
335 * If the first block is zero (unallocated) it will return the number of
336 * unallocated blocks in the extent, otherwise it will return the number
337 * of contiguous blocks in the extent.
339 * Returns: The length of the extent (minimum of one block)
342 static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
344 const __be64 *end = (start + len);
345 const __be64 *first = ptr;
346 u64 d = be64_to_cpu(*ptr);
353 if (limit && --limit == 0)
357 } while(be64_to_cpu(*ptr) == d);
360 return (ptr - first);
363 static inline void bmap_lock(struct gfs2_inode *ip, int create)
366 down_write(&ip->i_rw_mutex);
368 down_read(&ip->i_rw_mutex);
371 static inline void bmap_unlock(struct gfs2_inode *ip, int create)
374 up_write(&ip->i_rw_mutex);
376 up_read(&ip->i_rw_mutex);
379 static inline __be64 *gfs2_indirect_init(struct metapath *mp,
380 struct gfs2_glock *gl, unsigned int i,
381 unsigned offset, u64 bn)
383 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
384 ((i > 1) ? sizeof(struct gfs2_meta_header) :
385 sizeof(struct gfs2_dinode)));
387 BUG_ON(mp->mp_bh[i] != NULL);
388 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
389 gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
390 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
391 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
393 *ptr = cpu_to_be64(bn);
399 ALLOC_GROW_DEPTH = 1,
400 ALLOC_GROW_HEIGHT = 2,
401 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
405 * gfs2_bmap_alloc - Build a metadata tree of the requested height
406 * @inode: The GFS2 inode
407 * @lblock: The logical starting block of the extent
408 * @bh_map: This is used to return the mapping details
410 * @sheight: The starting height (i.e. whats already mapped)
411 * @height: The height to build to
412 * @maxlen: The max number of data blocks to alloc
414 * In this routine we may have to alloc:
415 * i) Indirect blocks to grow the metadata tree height
416 * ii) Indirect blocks to fill in lower part of the metadata tree
419 * The function is in two parts. The first part works out the total
420 * number of blocks which we need. The second part does the actual
421 * allocation asking for an extent at a time (if enough contiguous free
422 * blocks are available, there will only be one request per bmap call)
423 * and uses the state machine to initialise the blocks in order.
425 * Returns: errno on error
428 static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
429 struct buffer_head *bh_map, struct metapath *mp,
430 const unsigned int sheight,
431 const unsigned int height,
432 const unsigned int maxlen)
434 struct gfs2_inode *ip = GFS2_I(inode);
435 struct gfs2_sbd *sdp = GFS2_SB(inode);
436 struct buffer_head *dibh = mp->mp_bh[0];
438 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
440 unsigned ptrs_per_blk;
441 const unsigned end_of_metadata = height - 1;
443 enum alloc_state state;
448 BUG_ON(dibh == NULL);
450 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
452 if (height == sheight) {
453 struct buffer_head *bh;
454 /* Bottom indirect block exists, find unalloced extent size */
455 ptr = metapointer(end_of_metadata, mp);
456 bh = mp->mp_bh[end_of_metadata];
457 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
462 /* Need to allocate indirect blocks */
463 ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
464 dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
465 if (height == ip->i_height) {
466 /* Writing into existing tree, extend tree down */
467 iblks = height - sheight;
468 state = ALLOC_GROW_DEPTH;
470 /* Building up tree height */
471 state = ALLOC_GROW_HEIGHT;
472 iblks = height - ip->i_height;
473 branch_start = metapath_branch_start(mp);
474 iblks += (height - branch_start);
478 /* start of the second part of the function (state machine) */
480 blks = dblks + iblks;
485 error = gfs2_alloc_block(ip, &bn, &n);
489 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
490 gfs2_trans_add_unrevoke(sdp, bn, n);
492 /* Growing height of tree */
493 case ALLOC_GROW_HEIGHT:
495 ptr = (__be64 *)(dibh->b_data +
496 sizeof(struct gfs2_dinode));
499 for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
500 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
501 if (i - 1 == height - ip->i_height) {
503 gfs2_buffer_copy_tail(mp->mp_bh[i],
504 sizeof(struct gfs2_meta_header),
505 dibh, sizeof(struct gfs2_dinode));
506 gfs2_buffer_clear_tail(dibh,
507 sizeof(struct gfs2_dinode) +
509 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
510 sizeof(struct gfs2_meta_header));
512 state = ALLOC_GROW_DEPTH;
513 for(i = branch_start; i < height; i++) {
514 if (mp->mp_bh[i] == NULL)
516 brelse(mp->mp_bh[i]);
523 /* Branching from existing tree */
524 case ALLOC_GROW_DEPTH:
525 if (i > 1 && i < height)
526 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
527 for (; i < height && n > 0; i++, n--)
528 gfs2_indirect_init(mp, ip->i_gl, i,
529 mp->mp_list[i-1], bn++);
534 /* Tree complete, adding data blocks */
537 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
538 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
540 ptr = metapointer(end_of_metadata, mp);
543 *ptr++ = cpu_to_be64(bn++);
546 } while ((state != ALLOC_DATA) || !dblock);
548 ip->i_height = height;
549 gfs2_add_inode_blocks(&ip->i_inode, alloced);
550 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
551 map_bh(bh_map, inode->i_sb, dblock);
552 bh_map->b_size = dblks << inode->i_blkbits;
553 set_buffer_new(bh_map);
558 * gfs2_block_map - Map a block from an inode to a disk block
560 * @lblock: The logical block number
561 * @bh_map: The bh to be mapped
562 * @create: True if its ok to alloc blocks to satify the request
564 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
565 * read of metadata will be required before the next block can be
566 * mapped. Sets buffer_new() if new blocks were allocated.
571 int gfs2_block_map(struct inode *inode, sector_t lblock,
572 struct buffer_head *bh_map, int create)
574 struct gfs2_inode *ip = GFS2_I(inode);
575 struct gfs2_sbd *sdp = GFS2_SB(inode);
576 unsigned int bsize = sdp->sd_sb.sb_bsize;
577 const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
578 const u64 *arr = sdp->sd_heightsize;
585 struct buffer_head *bh;
590 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
591 bmap_lock(ip, create);
592 clear_buffer_mapped(bh_map);
593 clear_buffer_new(bh_map);
594 clear_buffer_boundary(bh_map);
595 trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
596 if (gfs2_is_dir(ip)) {
597 bsize = sdp->sd_jbsize;
598 arr = sdp->sd_jheightsize;
601 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
605 height = ip->i_height;
606 size = (lblock + 1) * bsize;
607 while (size > arr[height])
609 find_metapath(sdp, lblock, &mp, height);
611 if (height > ip->i_height || gfs2_is_stuffed(ip))
613 ret = lookup_metapath(ip, &mp);
616 if (ret != ip->i_height)
618 ptr = metapointer(ip->i_height - 1, &mp);
621 map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
622 bh = mp.mp_bh[ip->i_height - 1];
623 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
624 bh_map->b_size = (len << inode->i_blkbits);
626 set_buffer_boundary(bh_map);
629 release_metapath(&mp);
630 trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
631 bmap_unlock(ip, create);
635 /* All allocations are done here, firstly check create flag */
637 BUG_ON(gfs2_is_stuffed(ip));
642 /* At this point ret is the tree depth of already allocated blocks */
643 ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
648 * Deprecated: do not use in new code
650 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
652 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
660 bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
661 ret = gfs2_block_map(inode, lblock, &bh, create);
662 *extlen = bh.b_size >> inode->i_blkbits;
663 *dblock = bh.b_blocknr;
672 * recursive_scan - recursively scan through the end of a file
674 * @dibh: the dinode buffer
675 * @mp: the path through the metadata to the point to start
676 * @height: the height the recursion is at
677 * @block: the indirect block to look at
678 * @first: 1 if this is the first block
679 * @bc: the call to make for each piece of metadata
680 * @data: data opaque to this function to pass to @bc
682 * When this is first called @height and @block should be zero and
683 * @first should be 1.
688 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
689 struct metapath *mp, unsigned int height,
690 u64 block, int first, block_call_t bc,
693 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
694 struct buffer_head *bh = NULL;
695 __be64 *top, *bottom;
698 int mh_size = sizeof(struct gfs2_meta_header);
701 error = gfs2_meta_inode_buffer(ip, &bh);
706 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
707 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
709 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
713 top = (__be64 *)(bh->b_data + mh_size) +
714 (first ? mp->mp_list[height] : 0);
716 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
719 error = bc(ip, dibh, bh, top, bottom, height, data);
723 if (height < ip->i_height - 1)
724 for (; top < bottom; top++, first = 0) {
728 bn = be64_to_cpu(*top);
730 error = recursive_scan(ip, dibh, mp, height + 1, bn,
742 * do_strip - Look for a layer a particular layer of the file and strip it off
744 * @dibh: the dinode buffer
745 * @bh: A buffer of pointers
746 * @top: The first pointer in the buffer
747 * @bottom: One more than the last pointer
748 * @height: the height this buffer is at
749 * @data: a pointer to a struct strip_mine
754 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
755 struct buffer_head *bh, __be64 *top, __be64 *bottom,
756 unsigned int height, void *data)
758 struct strip_mine *sm = data;
759 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
760 struct gfs2_rgrp_list rlist;
764 unsigned int rg_blocks = 0;
766 unsigned int revokes = 0;
773 if (height != sm->sm_height)
781 metadata = (height != ip->i_height - 1);
783 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
785 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
789 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
793 for (p = top; p < bottom; p++) {
797 bn = be64_to_cpu(*p);
799 if (bstart + blen == bn)
803 gfs2_rlist_add(sdp, &rlist, bstart);
811 gfs2_rlist_add(sdp, &rlist, bstart);
813 goto out; /* Nothing to do */
815 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
817 for (x = 0; x < rlist.rl_rgrps; x++) {
818 struct gfs2_rgrpd *rgd;
819 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
820 rg_blocks += rgd->rd_length;
823 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
827 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
828 RES_INDIRECT + RES_STATFS + RES_QUOTA,
833 down_write(&ip->i_rw_mutex);
835 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
836 gfs2_trans_add_bh(ip->i_gl, bh, 1);
841 for (p = top; p < bottom; p++) {
845 bn = be64_to_cpu(*p);
847 if (bstart + blen == bn)
852 gfs2_free_meta(ip, bstart, blen);
854 gfs2_free_data(ip, bstart, blen);
862 gfs2_add_inode_blocks(&ip->i_inode, -1);
866 gfs2_free_meta(ip, bstart, blen);
868 gfs2_free_data(ip, bstart, blen);
871 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
873 gfs2_dinode_out(ip, dibh->b_data);
875 up_write(&ip->i_rw_mutex);
880 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
882 gfs2_rlist_free(&rlist);
884 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
889 * do_grow - Make a file look bigger than it is
891 * @size: the size to set the file to
893 * Called with an exclusive lock on @ip.
898 static int do_grow(struct gfs2_inode *ip, u64 size)
900 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
901 struct gfs2_alloc *al;
902 struct buffer_head *dibh;
905 al = gfs2_alloc_get(ip);
909 error = gfs2_quota_lock_check(ip);
913 al->al_requested = sdp->sd_max_height + RES_DATA;
915 error = gfs2_inplace_reserve(ip);
919 error = gfs2_trans_begin(sdp,
920 sdp->sd_max_height + al->al_rgd->rd_length +
921 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
925 error = gfs2_meta_inode_buffer(ip, &dibh);
929 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
930 if (gfs2_is_stuffed(ip)) {
931 error = gfs2_unstuff_dinode(ip, NULL);
937 ip->i_disksize = size;
938 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
939 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
940 gfs2_dinode_out(ip, dibh->b_data);
947 gfs2_inplace_release(ip);
949 gfs2_quota_unlock(ip);
957 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
959 * This is partly borrowed from ext3.
961 static int gfs2_block_truncate_page(struct address_space *mapping)
963 struct inode *inode = mapping->host;
964 struct gfs2_inode *ip = GFS2_I(inode);
965 loff_t from = inode->i_size;
966 unsigned long index = from >> PAGE_CACHE_SHIFT;
967 unsigned offset = from & (PAGE_CACHE_SIZE-1);
968 unsigned blocksize, iblock, length, pos;
969 struct buffer_head *bh;
973 page = grab_cache_page(mapping, index);
977 blocksize = inode->i_sb->s_blocksize;
978 length = blocksize - (offset & (blocksize - 1));
979 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
981 if (!page_has_buffers(page))
982 create_empty_buffers(page, blocksize, 0);
984 /* Find the buffer that contains "offset" */
985 bh = page_buffers(page);
987 while (offset >= pos) {
988 bh = bh->b_this_page;
995 if (!buffer_mapped(bh)) {
996 gfs2_block_map(inode, iblock, bh, 0);
997 /* unmapped? It's a hole - nothing to do */
998 if (!buffer_mapped(bh))
1002 /* Ok, it's mapped. Make sure it's up-to-date */
1003 if (PageUptodate(page))
1004 set_buffer_uptodate(bh);
1006 if (!buffer_uptodate(bh)) {
1008 ll_rw_block(READ, 1, &bh);
1010 /* Uhhuh. Read error. Complain and punt. */
1011 if (!buffer_uptodate(bh))
1016 if (!gfs2_is_writeback(ip))
1017 gfs2_trans_add_bh(ip->i_gl, bh, 0);
1019 zero_user(page, offset, length);
1020 mark_buffer_dirty(bh);
1023 page_cache_release(page);
1027 static int trunc_start(struct gfs2_inode *ip, u64 size)
1029 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1030 struct buffer_head *dibh;
1031 int journaled = gfs2_is_jdata(ip);
1034 error = gfs2_trans_begin(sdp,
1035 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
1039 error = gfs2_meta_inode_buffer(ip, &dibh);
1043 if (gfs2_is_stuffed(ip)) {
1044 u64 dsize = size + sizeof(struct gfs2_inode);
1045 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1046 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1047 gfs2_dinode_out(ip, dibh->b_data);
1048 if (dsize > dibh->b_size)
1049 dsize = dibh->b_size;
1050 gfs2_buffer_clear_tail(dibh, dsize);
1053 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
1054 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
1057 ip->i_disksize = size;
1058 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1059 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
1060 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1061 gfs2_dinode_out(ip, dibh->b_data);
1068 gfs2_trans_end(sdp);
1072 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
1074 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1075 unsigned int height = ip->i_height;
1083 lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
1085 find_metapath(sdp, lblock, &mp, ip->i_height);
1086 if (!gfs2_alloc_get(ip))
1089 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1094 struct strip_mine sm;
1095 sm.sm_first = !!size;
1096 sm.sm_height = height;
1098 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1103 gfs2_quota_unhold(ip);
1110 static int trunc_end(struct gfs2_inode *ip)
1112 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1113 struct buffer_head *dibh;
1116 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1120 down_write(&ip->i_rw_mutex);
1122 error = gfs2_meta_inode_buffer(ip, &dibh);
1126 if (!ip->i_disksize) {
1128 ip->i_goal = ip->i_no_addr;
1129 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1131 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1132 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
1134 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1135 gfs2_dinode_out(ip, dibh->b_data);
1139 up_write(&ip->i_rw_mutex);
1140 gfs2_trans_end(sdp);
1145 * do_shrink - make a file smaller
1147 * @size: the size to make the file
1148 * @truncator: function to truncate the last partial block
1150 * Called with an exclusive lock on @ip.
1155 static int do_shrink(struct gfs2_inode *ip, u64 size)
1159 error = trunc_start(ip, size);
1165 error = trunc_dealloc(ip, size);
1167 error = trunc_end(ip);
1172 static int do_touch(struct gfs2_inode *ip, u64 size)
1174 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1175 struct buffer_head *dibh;
1178 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1182 down_write(&ip->i_rw_mutex);
1184 error = gfs2_meta_inode_buffer(ip, &dibh);
1188 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1189 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1190 gfs2_dinode_out(ip, dibh->b_data);
1194 up_write(&ip->i_rw_mutex);
1195 gfs2_trans_end(sdp);
1200 * gfs2_truncatei - make a file a given size
1202 * @size: the size to make the file
1203 * @truncator: function to truncate the last partial block
1205 * The file size can grow, shrink, or stay the same size.
1210 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1214 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1217 if (size > ip->i_disksize)
1218 error = do_grow(ip, size);
1219 else if (size < ip->i_disksize)
1220 error = do_shrink(ip, size);
1222 /* update time stamps */
1223 error = do_touch(ip, size);
1228 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1231 error = trunc_dealloc(ip, ip->i_disksize);
1233 error = trunc_end(ip);
1237 int gfs2_file_dealloc(struct gfs2_inode *ip)
1239 return trunc_dealloc(ip, 0);
1243 * gfs2_write_alloc_required - figure out if a write will require an allocation
1244 * @ip: the file being written to
1245 * @offset: the offset to write to
1246 * @len: the number of bytes being written
1247 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1252 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1253 unsigned int len, int *alloc_required)
1255 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1256 struct buffer_head bh;
1258 u64 lblock, lblock_stop, size;
1261 *alloc_required = 0;
1266 if (gfs2_is_stuffed(ip)) {
1268 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1269 *alloc_required = 1;
1273 *alloc_required = 1;
1274 shift = sdp->sd_sb.sb_bsize_shift;
1275 BUG_ON(gfs2_is_dir(ip));
1276 end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift;
1277 lblock = offset >> shift;
1278 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1279 if (lblock_stop > end_of_file)
1282 size = (lblock_stop - lblock) << shift;
1286 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1287 if (!buffer_mapped(&bh))
1290 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1293 *alloc_required = 0;