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 "ops_address.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);
76 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
78 memset(kaddr + ip->i_disksize, 0,
79 PAGE_CACHE_SIZE - ip->i_disksize);
82 SetPageUptodate(page);
85 if (!page_has_buffers(page))
86 create_empty_buffers(page, 1 << inode->i_blkbits,
89 bh = page_buffers(page);
91 if (!buffer_mapped(bh))
92 map_bh(bh, inode->i_sb, block);
94 set_buffer_uptodate(bh);
95 if (!gfs2_is_jdata(ip))
96 mark_buffer_dirty(bh);
97 if (!gfs2_is_writeback(ip))
98 gfs2_trans_add_bh(ip->i_gl, bh, 0);
102 page_cache_release(page);
109 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
110 * @ip: The GFS2 inode to unstuff
111 * @unstuffer: the routine that handles unstuffing a non-zero length file
112 * @private: private data for the unstuffer
114 * This routine unstuffs a dinode and returns it to a "normal" state such
115 * that the height can be grown in the traditional way.
120 int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
122 struct buffer_head *bh, *dibh;
123 struct gfs2_dinode *di;
125 int isdir = gfs2_is_dir(ip);
128 down_write(&ip->i_rw_mutex);
130 error = gfs2_meta_inode_buffer(ip, &dibh);
134 if (ip->i_disksize) {
135 /* Get a free block, fill it with the stuffed data,
136 and write it out to disk */
139 block = gfs2_alloc_block(ip, &n);
141 gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
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 error = gfs2_unstuffer_page(ip, dibh, block, page);
155 /* Set up the pointer to the new block */
157 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
158 di = (struct gfs2_dinode *)dibh->b_data;
159 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
161 if (ip->i_disksize) {
162 *(__be64 *)(di + 1) = cpu_to_be64(block);
163 gfs2_add_inode_blocks(&ip->i_inode, 1);
164 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
168 di->di_height = cpu_to_be16(1);
173 up_write(&ip->i_rw_mutex);
179 * find_metapath - Find path through the metadata tree
180 * @sdp: The superblock
181 * @mp: The metapath to return the result in
182 * @block: The disk block to look up
183 * @height: The pre-calculated height of the metadata tree
185 * This routine returns a struct metapath structure that defines a path
186 * through the metadata of inode "ip" to get to block "block".
189 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
190 * filesystem with a blocksize of 4096.
192 * find_metapath() would return a struct metapath structure set to:
193 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
194 * and mp_list[2] = 165.
196 * That means that in order to get to the block containing the byte at
197 * offset 101342453, we would load the indirect block pointed to by pointer
198 * 0 in the dinode. We would then load the indirect block pointed to by
199 * pointer 48 in that indirect block. We would then load the data block
200 * pointed to by pointer 165 in that indirect block.
202 * ----------------------------------------
207 * ----------------------------------------
211 * ----------------------------------------
215 * |0 5 6 7 8 9 0 1 2|
216 * ----------------------------------------
220 * ----------------------------------------
225 * ----------------------------------------
229 * ----------------------------------------
230 * | Data block containing offset |
234 * ----------------------------------------
238 static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
239 struct metapath *mp, unsigned int height)
243 for (i = height; i--;)
244 mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
248 static inline unsigned int metapath_branch_start(const struct metapath *mp)
250 if (mp->mp_list[0] == 0)
256 * metapointer - Return pointer to start of metadata in a buffer
257 * @height: The metadata height (0 = dinode)
260 * Return a pointer to the block number of the next height of the metadata
261 * tree given a buffer containing the pointer to the current height of the
265 static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
267 struct buffer_head *bh = mp->mp_bh[height];
268 unsigned int head_size = (height > 0) ?
269 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
270 return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
274 * lookup_metapath - Walk the metadata tree to a specific point
278 * Assumes that the inode's buffer has already been looked up and
279 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
280 * by find_metapath().
282 * If this function encounters part of the tree which has not been
283 * allocated, it returns the current height of the tree at the point
284 * at which it found the unallocated block. Blocks which are found are
285 * added to the mp->mp_bh[] list.
287 * Returns: error or height of metadata tree
290 static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
292 unsigned int end_of_metadata = ip->i_height - 1;
298 for (x = 0; x < end_of_metadata; x++) {
299 ptr = metapointer(x, mp);
300 dblock = be64_to_cpu(*ptr);
304 ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
312 static inline void release_metapath(struct metapath *mp)
316 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
317 if (mp->mp_bh[i] == NULL)
319 brelse(mp->mp_bh[i]);
324 * gfs2_extent_length - Returns length of an extent of blocks
325 * @start: Start of the buffer
326 * @len: Length of the buffer in bytes
327 * @ptr: Current position in the buffer
328 * @limit: Max extent length to return (0 = unlimited)
329 * @eob: Set to 1 if we hit "end of block"
331 * If the first block is zero (unallocated) it will return the number of
332 * unallocated blocks in the extent, otherwise it will return the number
333 * of contiguous blocks in the extent.
335 * Returns: The length of the extent (minimum of one block)
338 static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
340 const __be64 *end = (start + len);
341 const __be64 *first = ptr;
342 u64 d = be64_to_cpu(*ptr);
349 if (limit && --limit == 0)
353 } while(be64_to_cpu(*ptr) == d);
356 return (ptr - first);
359 static inline void bmap_lock(struct gfs2_inode *ip, int create)
362 down_write(&ip->i_rw_mutex);
364 down_read(&ip->i_rw_mutex);
367 static inline void bmap_unlock(struct gfs2_inode *ip, int create)
370 up_write(&ip->i_rw_mutex);
372 up_read(&ip->i_rw_mutex);
375 static inline __be64 *gfs2_indirect_init(struct metapath *mp,
376 struct gfs2_glock *gl, unsigned int i,
377 unsigned offset, u64 bn)
379 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
380 ((i > 1) ? sizeof(struct gfs2_meta_header) :
381 sizeof(struct gfs2_dinode)));
383 BUG_ON(mp->mp_bh[i] != NULL);
384 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
385 gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
386 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
387 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
389 *ptr = cpu_to_be64(bn);
395 ALLOC_GROW_DEPTH = 1,
396 ALLOC_GROW_HEIGHT = 2,
397 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
401 * gfs2_bmap_alloc - Build a metadata tree of the requested height
402 * @inode: The GFS2 inode
403 * @lblock: The logical starting block of the extent
404 * @bh_map: This is used to return the mapping details
406 * @sheight: The starting height (i.e. whats already mapped)
407 * @height: The height to build to
408 * @maxlen: The max number of data blocks to alloc
410 * In this routine we may have to alloc:
411 * i) Indirect blocks to grow the metadata tree height
412 * ii) Indirect blocks to fill in lower part of the metadata tree
415 * The function is in two parts. The first part works out the total
416 * number of blocks which we need. The second part does the actual
417 * allocation asking for an extent at a time (if enough contiguous free
418 * blocks are available, there will only be one request per bmap call)
419 * and uses the state machine to initialise the blocks in order.
421 * Returns: errno on error
424 static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
425 struct buffer_head *bh_map, struct metapath *mp,
426 const unsigned int sheight,
427 const unsigned int height,
428 const unsigned int maxlen)
430 struct gfs2_inode *ip = GFS2_I(inode);
431 struct gfs2_sbd *sdp = GFS2_SB(inode);
432 struct buffer_head *dibh = mp->mp_bh[0];
434 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
436 unsigned ptrs_per_blk;
437 const unsigned end_of_metadata = height - 1;
439 enum alloc_state state;
444 BUG_ON(dibh == NULL);
446 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
448 if (height == sheight) {
449 struct buffer_head *bh;
450 /* Bottom indirect block exists, find unalloced extent size */
451 ptr = metapointer(end_of_metadata, mp);
452 bh = mp->mp_bh[end_of_metadata];
453 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
458 /* Need to allocate indirect blocks */
459 ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
460 dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
461 if (height == ip->i_height) {
462 /* Writing into existing tree, extend tree down */
463 iblks = height - sheight;
464 state = ALLOC_GROW_DEPTH;
466 /* Building up tree height */
467 state = ALLOC_GROW_HEIGHT;
468 iblks = height - ip->i_height;
469 branch_start = metapath_branch_start(mp);
470 iblks += (height - branch_start);
474 /* start of the second part of the function (state machine) */
476 blks = dblks + iblks;
480 bn = gfs2_alloc_block(ip, &n);
482 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
483 gfs2_trans_add_unrevoke(sdp, bn, n);
485 /* Growing height of tree */
486 case ALLOC_GROW_HEIGHT:
488 ptr = (__be64 *)(dibh->b_data +
489 sizeof(struct gfs2_dinode));
492 for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
493 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
494 if (i - 1 == height - ip->i_height) {
496 gfs2_buffer_copy_tail(mp->mp_bh[i],
497 sizeof(struct gfs2_meta_header),
498 dibh, sizeof(struct gfs2_dinode));
499 gfs2_buffer_clear_tail(dibh,
500 sizeof(struct gfs2_dinode) +
502 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
503 sizeof(struct gfs2_meta_header));
505 state = ALLOC_GROW_DEPTH;
506 for(i = branch_start; i < height; i++) {
507 if (mp->mp_bh[i] == NULL)
509 brelse(mp->mp_bh[i]);
516 /* Branching from existing tree */
517 case ALLOC_GROW_DEPTH:
518 if (i > 1 && i < height)
519 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
520 for (; i < height && n > 0; i++, n--)
521 gfs2_indirect_init(mp, ip->i_gl, i,
522 mp->mp_list[i-1], bn++);
527 /* Tree complete, adding data blocks */
530 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
531 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
533 ptr = metapointer(end_of_metadata, mp);
536 *ptr++ = cpu_to_be64(bn++);
539 } while (state != ALLOC_DATA);
541 ip->i_height = height;
542 gfs2_add_inode_blocks(&ip->i_inode, alloced);
543 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
544 map_bh(bh_map, inode->i_sb, dblock);
545 bh_map->b_size = dblks << inode->i_blkbits;
546 set_buffer_new(bh_map);
551 * gfs2_block_map - Map a block from an inode to a disk block
553 * @lblock: The logical block number
554 * @bh_map: The bh to be mapped
555 * @create: True if its ok to alloc blocks to satify the request
557 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
558 * read of metadata will be required before the next block can be
559 * mapped. Sets buffer_new() if new blocks were allocated.
564 int gfs2_block_map(struct inode *inode, sector_t lblock,
565 struct buffer_head *bh_map, int create)
567 struct gfs2_inode *ip = GFS2_I(inode);
568 struct gfs2_sbd *sdp = GFS2_SB(inode);
569 unsigned int bsize = sdp->sd_sb.sb_bsize;
570 const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
571 const u64 *arr = sdp->sd_heightsize;
578 struct buffer_head *bh;
583 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
584 bmap_lock(ip, create);
585 clear_buffer_mapped(bh_map);
586 clear_buffer_new(bh_map);
587 clear_buffer_boundary(bh_map);
588 if (gfs2_is_dir(ip)) {
589 bsize = sdp->sd_jbsize;
590 arr = sdp->sd_jheightsize;
593 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
597 height = ip->i_height;
598 size = (lblock + 1) * bsize;
599 while (size > arr[height])
601 find_metapath(sdp, lblock, &mp, height);
603 if (height > ip->i_height || gfs2_is_stuffed(ip))
605 ret = lookup_metapath(ip, &mp);
608 if (ret != ip->i_height)
610 ptr = metapointer(ip->i_height - 1, &mp);
613 map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
614 bh = mp.mp_bh[ip->i_height - 1];
615 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
616 bh_map->b_size = (len << inode->i_blkbits);
618 set_buffer_boundary(bh_map);
621 release_metapath(&mp);
622 bmap_unlock(ip, create);
626 /* All allocations are done here, firstly check create flag */
628 BUG_ON(gfs2_is_stuffed(ip));
633 /* At this point ret is the tree depth of already allocated blocks */
634 ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
639 * Deprecated: do not use in new code
641 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
643 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
651 bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
652 ret = gfs2_block_map(inode, lblock, &bh, create);
653 *extlen = bh.b_size >> inode->i_blkbits;
654 *dblock = bh.b_blocknr;
663 * recursive_scan - recursively scan through the end of a file
665 * @dibh: the dinode buffer
666 * @mp: the path through the metadata to the point to start
667 * @height: the height the recursion is at
668 * @block: the indirect block to look at
669 * @first: 1 if this is the first block
670 * @bc: the call to make for each piece of metadata
671 * @data: data opaque to this function to pass to @bc
673 * When this is first called @height and @block should be zero and
674 * @first should be 1.
679 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
680 struct metapath *mp, unsigned int height,
681 u64 block, int first, block_call_t bc,
684 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
685 struct buffer_head *bh = NULL;
686 __be64 *top, *bottom;
689 int mh_size = sizeof(struct gfs2_meta_header);
692 error = gfs2_meta_inode_buffer(ip, &bh);
697 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
698 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
700 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
704 top = (__be64 *)(bh->b_data + mh_size) +
705 (first ? mp->mp_list[height] : 0);
707 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
710 error = bc(ip, dibh, bh, top, bottom, height, data);
714 if (height < ip->i_height - 1)
715 for (; top < bottom; top++, first = 0) {
719 bn = be64_to_cpu(*top);
721 error = recursive_scan(ip, dibh, mp, height + 1, bn,
733 * do_strip - Look for a layer a particular layer of the file and strip it off
735 * @dibh: the dinode buffer
736 * @bh: A buffer of pointers
737 * @top: The first pointer in the buffer
738 * @bottom: One more than the last pointer
739 * @height: the height this buffer is at
740 * @data: a pointer to a struct strip_mine
745 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
746 struct buffer_head *bh, __be64 *top, __be64 *bottom,
747 unsigned int height, void *data)
749 struct strip_mine *sm = data;
750 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
751 struct gfs2_rgrp_list rlist;
755 unsigned int rg_blocks = 0;
757 unsigned int revokes = 0;
764 if (height != sm->sm_height)
772 metadata = (height != ip->i_height - 1);
774 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
776 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
780 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
784 for (p = top; p < bottom; p++) {
788 bn = be64_to_cpu(*p);
790 if (bstart + blen == bn)
794 gfs2_rlist_add(sdp, &rlist, bstart);
802 gfs2_rlist_add(sdp, &rlist, bstart);
804 goto out; /* Nothing to do */
806 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
808 for (x = 0; x < rlist.rl_rgrps; x++) {
809 struct gfs2_rgrpd *rgd;
810 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
811 rg_blocks += rgd->rd_length;
814 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
818 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
819 RES_INDIRECT + RES_STATFS + RES_QUOTA,
824 down_write(&ip->i_rw_mutex);
826 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
827 gfs2_trans_add_bh(ip->i_gl, bh, 1);
832 for (p = top; p < bottom; p++) {
836 bn = be64_to_cpu(*p);
838 if (bstart + blen == bn)
843 gfs2_free_meta(ip, bstart, blen);
845 gfs2_free_data(ip, bstart, blen);
853 gfs2_add_inode_blocks(&ip->i_inode, -1);
857 gfs2_free_meta(ip, bstart, blen);
859 gfs2_free_data(ip, bstart, blen);
862 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
864 gfs2_dinode_out(ip, dibh->b_data);
866 up_write(&ip->i_rw_mutex);
871 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
873 gfs2_rlist_free(&rlist);
875 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
880 * do_grow - Make a file look bigger than it is
882 * @size: the size to set the file to
884 * Called with an exclusive lock on @ip.
889 static int do_grow(struct gfs2_inode *ip, u64 size)
891 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
892 struct gfs2_alloc *al;
893 struct buffer_head *dibh;
896 al = gfs2_alloc_get(ip);
900 error = gfs2_quota_lock_check(ip);
904 al->al_requested = sdp->sd_max_height + RES_DATA;
906 error = gfs2_inplace_reserve(ip);
910 error = gfs2_trans_begin(sdp,
911 sdp->sd_max_height + al->al_rgd->rd_length +
912 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
916 error = gfs2_meta_inode_buffer(ip, &dibh);
920 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
921 if (gfs2_is_stuffed(ip)) {
922 error = gfs2_unstuff_dinode(ip, NULL);
928 ip->i_disksize = size;
929 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
930 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
931 gfs2_dinode_out(ip, dibh->b_data);
938 gfs2_inplace_release(ip);
940 gfs2_quota_unlock(ip);
948 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
950 * This is partly borrowed from ext3.
952 static int gfs2_block_truncate_page(struct address_space *mapping)
954 struct inode *inode = mapping->host;
955 struct gfs2_inode *ip = GFS2_I(inode);
956 loff_t from = inode->i_size;
957 unsigned long index = from >> PAGE_CACHE_SHIFT;
958 unsigned offset = from & (PAGE_CACHE_SIZE-1);
959 unsigned blocksize, iblock, length, pos;
960 struct buffer_head *bh;
964 page = grab_cache_page(mapping, index);
968 blocksize = inode->i_sb->s_blocksize;
969 length = blocksize - (offset & (blocksize - 1));
970 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
972 if (!page_has_buffers(page))
973 create_empty_buffers(page, blocksize, 0);
975 /* Find the buffer that contains "offset" */
976 bh = page_buffers(page);
978 while (offset >= pos) {
979 bh = bh->b_this_page;
986 if (!buffer_mapped(bh)) {
987 gfs2_block_map(inode, iblock, bh, 0);
988 /* unmapped? It's a hole - nothing to do */
989 if (!buffer_mapped(bh))
993 /* Ok, it's mapped. Make sure it's up-to-date */
994 if (PageUptodate(page))
995 set_buffer_uptodate(bh);
997 if (!buffer_uptodate(bh)) {
999 ll_rw_block(READ, 1, &bh);
1001 /* Uhhuh. Read error. Complain and punt. */
1002 if (!buffer_uptodate(bh))
1007 if (!gfs2_is_writeback(ip))
1008 gfs2_trans_add_bh(ip->i_gl, bh, 0);
1010 zero_user(page, offset, length);
1014 page_cache_release(page);
1018 static int trunc_start(struct gfs2_inode *ip, u64 size)
1020 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1021 struct buffer_head *dibh;
1022 int journaled = gfs2_is_jdata(ip);
1025 error = gfs2_trans_begin(sdp,
1026 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
1030 error = gfs2_meta_inode_buffer(ip, &dibh);
1034 if (gfs2_is_stuffed(ip)) {
1035 ip->i_disksize = size;
1036 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1037 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1038 gfs2_dinode_out(ip, dibh->b_data);
1039 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
1043 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
1044 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
1047 ip->i_disksize = size;
1048 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1049 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
1050 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1051 gfs2_dinode_out(ip, dibh->b_data);
1058 gfs2_trans_end(sdp);
1062 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
1064 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1065 unsigned int height = ip->i_height;
1073 lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
1075 find_metapath(sdp, lblock, &mp, ip->i_height);
1076 if (!gfs2_alloc_get(ip))
1079 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1084 struct strip_mine sm;
1085 sm.sm_first = !!size;
1086 sm.sm_height = height;
1088 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1093 gfs2_quota_unhold(ip);
1100 static int trunc_end(struct gfs2_inode *ip)
1102 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1103 struct buffer_head *dibh;
1106 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1110 down_write(&ip->i_rw_mutex);
1112 error = gfs2_meta_inode_buffer(ip, &dibh);
1116 if (!ip->i_disksize) {
1118 ip->i_goal = ip->i_no_addr;
1119 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1121 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1122 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
1124 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1125 gfs2_dinode_out(ip, dibh->b_data);
1129 up_write(&ip->i_rw_mutex);
1130 gfs2_trans_end(sdp);
1135 * do_shrink - make a file smaller
1137 * @size: the size to make the file
1138 * @truncator: function to truncate the last partial block
1140 * Called with an exclusive lock on @ip.
1145 static int do_shrink(struct gfs2_inode *ip, u64 size)
1149 error = trunc_start(ip, size);
1155 error = trunc_dealloc(ip, size);
1157 error = trunc_end(ip);
1162 static int do_touch(struct gfs2_inode *ip, u64 size)
1164 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1165 struct buffer_head *dibh;
1168 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1172 down_write(&ip->i_rw_mutex);
1174 error = gfs2_meta_inode_buffer(ip, &dibh);
1178 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1179 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1180 gfs2_dinode_out(ip, dibh->b_data);
1184 up_write(&ip->i_rw_mutex);
1185 gfs2_trans_end(sdp);
1190 * gfs2_truncatei - make a file a given size
1192 * @size: the size to make the file
1193 * @truncator: function to truncate the last partial block
1195 * The file size can grow, shrink, or stay the same size.
1200 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1204 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1207 if (size > ip->i_disksize)
1208 error = do_grow(ip, size);
1209 else if (size < ip->i_disksize)
1210 error = do_shrink(ip, size);
1212 /* update time stamps */
1213 error = do_touch(ip, size);
1218 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1221 error = trunc_dealloc(ip, ip->i_disksize);
1223 error = trunc_end(ip);
1227 int gfs2_file_dealloc(struct gfs2_inode *ip)
1229 return trunc_dealloc(ip, 0);
1233 * gfs2_write_alloc_required - figure out if a write will require an allocation
1234 * @ip: the file being written to
1235 * @offset: the offset to write to
1236 * @len: the number of bytes being written
1237 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1242 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1243 unsigned int len, int *alloc_required)
1245 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1246 struct buffer_head bh;
1248 u64 lblock, lblock_stop, size;
1251 *alloc_required = 0;
1256 if (gfs2_is_stuffed(ip)) {
1258 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1259 *alloc_required = 1;
1263 *alloc_required = 1;
1264 shift = sdp->sd_sb.sb_bsize_shift;
1265 BUG_ON(gfs2_is_dir(ip));
1266 end_of_file = (ip->i_disksize + sdp->sd_sb.sb_bsize - 1) >> shift;
1267 lblock = offset >> shift;
1268 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1269 if (lblock_stop > end_of_file)
1272 size = (lblock_stop - lblock) << shift;
1276 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1277 if (!buffer_mapped(&bh))
1280 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1283 *alloc_required = 0;