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_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_di.di_size) {
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 zero_metapath_length(const struct metapath *mp,
253 for (i = 0; i < height - 1; i++) {
254 if (mp->mp_list[i] != 0)
261 * metapointer - Return pointer to start of metadata in a buffer
262 * @height: The metadata height (0 = dinode)
265 * Return a pointer to the block number of the next height of the metadata
266 * tree given a buffer containing the pointer to the current height of the
270 static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
272 struct buffer_head *bh = mp->mp_bh[height];
273 unsigned int head_size = (height > 0) ?
274 sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
275 return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
279 * lookup_metapath - Walk the metadata tree to a specific point
283 * Assumes that the inode's buffer has already been looked up and
284 * hooked onto mp->mp_bh[0] and that the metapath has been initialised
285 * by find_metapath().
287 * If this function encounters part of the tree which has not been
288 * allocated, it returns the current height of the tree at the point
289 * at which it found the unallocated block. Blocks which are found are
290 * added to the mp->mp_bh[] list.
292 * Returns: error or height of metadata tree
295 static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
297 unsigned int end_of_metadata = ip->i_height - 1;
303 for (x = 0; x < end_of_metadata; x++) {
304 ptr = metapointer(x, mp);
305 dblock = be64_to_cpu(*ptr);
309 ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, 0, &mp->mp_bh[x+1]);
317 static inline void release_metapath(struct metapath *mp)
321 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
322 if (mp->mp_bh[i] == NULL)
324 brelse(mp->mp_bh[i]);
329 * gfs2_extent_length - Returns length of an extent of blocks
330 * @start: Start of the buffer
331 * @len: Length of the buffer in bytes
332 * @ptr: Current position in the buffer
333 * @limit: Max extent length to return (0 = unlimited)
334 * @eob: Set to 1 if we hit "end of block"
336 * If the first block is zero (unallocated) it will return the number of
337 * unallocated blocks in the extent, otherwise it will return the number
338 * of contiguous blocks in the extent.
340 * Returns: The length of the extent (minimum of one block)
343 static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
345 const __be64 *end = (start + len);
346 const __be64 *first = ptr;
347 u64 d = be64_to_cpu(*ptr);
354 if (limit && --limit == 0)
358 } while(be64_to_cpu(*ptr) == d);
361 return (ptr - first);
364 static inline void bmap_lock(struct gfs2_inode *ip, int create)
367 down_write(&ip->i_rw_mutex);
369 down_read(&ip->i_rw_mutex);
372 static inline void bmap_unlock(struct gfs2_inode *ip, int create)
375 up_write(&ip->i_rw_mutex);
377 up_read(&ip->i_rw_mutex);
380 static inline __be64 *gfs2_indirect_init(struct metapath *mp,
381 struct gfs2_glock *gl, unsigned int i,
382 unsigned offset, u64 bn)
384 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
385 ((i > 1) ? sizeof(struct gfs2_meta_header) :
386 sizeof(struct gfs2_dinode)));
388 BUG_ON(mp->mp_bh[i] != NULL);
389 mp->mp_bh[i] = gfs2_meta_new(gl, bn);
390 gfs2_trans_add_bh(gl, mp->mp_bh[i], 1);
391 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
392 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
394 *ptr = cpu_to_be64(bn);
400 ALLOC_GROW_DEPTH = 1,
401 ALLOC_GROW_HEIGHT = 2,
402 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
406 * gfs2_bmap_alloc - Build a metadata tree of the requested height
407 * @inode: The GFS2 inode
408 * @lblock: The logical starting block of the extent
409 * @bh_map: This is used to return the mapping details
411 * @sheight: The starting height (i.e. whats already mapped)
412 * @height: The height to build to
413 * @maxlen: The max number of data blocks to alloc
415 * In this routine we may have to alloc:
416 * i) Indirect blocks to grow the metadata tree height
417 * ii) Indirect blocks to fill in lower part of the metadata tree
420 * The function is in two parts. The first part works out the total
421 * number of blocks which we need. The second part does the actual
422 * allocation asking for an extent at a time (if enough contiguous free
423 * blocks are available, there will only be one request per bmap call)
424 * and uses the state machine to initialise the blocks in order.
426 * Returns: errno on error
429 static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
430 struct buffer_head *bh_map, struct metapath *mp,
431 const unsigned int sheight,
432 const unsigned int height,
433 const unsigned int maxlen)
435 struct gfs2_inode *ip = GFS2_I(inode);
436 struct gfs2_sbd *sdp = GFS2_SB(inode);
437 struct buffer_head *dibh = mp->mp_bh[0];
439 unsigned n, i, blks, alloced = 0, iblks = 0, zmpl = 0;
441 unsigned ptrs_per_blk;
442 const unsigned end_of_metadata = height - 1;
444 enum alloc_state state;
449 BUG_ON(dibh == NULL);
451 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
453 if (height == sheight) {
454 struct buffer_head *bh;
455 /* Bottom indirect block exists, find unalloced extent size */
456 ptr = metapointer(end_of_metadata, mp);
457 bh = mp->mp_bh[end_of_metadata];
458 dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
463 /* Need to allocate indirect blocks */
464 ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
465 dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
466 if (height == ip->i_height) {
467 /* Writing into existing tree, extend tree down */
468 iblks = height - sheight;
469 state = ALLOC_GROW_DEPTH;
471 /* Building up tree height */
472 state = ALLOC_GROW_HEIGHT;
473 iblks = height - ip->i_height;
474 zmpl = zero_metapath_length(mp, height);
480 /* start of the second part of the function (state machine) */
482 blks = dblks + iblks;
486 bn = gfs2_alloc_block(ip, &n);
488 if (state != ALLOC_DATA || gfs2_is_jdata(ip))
489 gfs2_trans_add_unrevoke(sdp, bn, n);
491 /* Growing height of tree */
492 case ALLOC_GROW_HEIGHT:
494 ptr = (__be64 *)(dibh->b_data +
495 sizeof(struct gfs2_dinode));
498 for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
499 gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
500 if (i - 1 == height - ip->i_height) {
502 gfs2_buffer_copy_tail(mp->mp_bh[i],
503 sizeof(struct gfs2_meta_header),
504 dibh, sizeof(struct gfs2_dinode));
505 gfs2_buffer_clear_tail(dibh,
506 sizeof(struct gfs2_dinode) +
508 ptr = (__be64 *)(mp->mp_bh[i]->b_data +
509 sizeof(struct gfs2_meta_header));
511 state = ALLOC_GROW_DEPTH;
512 for(i = zmpl; i < height; i++) {
513 if (mp->mp_bh[i] == NULL)
515 brelse(mp->mp_bh[i]);
522 /* Branching from existing tree */
523 case ALLOC_GROW_DEPTH:
524 if (i > 1 && i < height)
525 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[i-1], 1);
526 for (; i < height && n > 0; i++, n--)
527 gfs2_indirect_init(mp, ip->i_gl, i,
528 mp->mp_list[i-1], bn++);
533 /* Tree complete, adding data blocks */
536 BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
537 gfs2_trans_add_bh(ip->i_gl, mp->mp_bh[end_of_metadata], 1);
539 ptr = metapointer(end_of_metadata, mp);
542 *ptr++ = cpu_to_be64(bn++);
545 } while (state != ALLOC_DATA);
547 ip->i_height = height;
548 gfs2_add_inode_blocks(&ip->i_inode, alloced);
549 gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
550 map_bh(bh_map, inode->i_sb, dblock);
551 bh_map->b_size = dblks << inode->i_blkbits;
552 set_buffer_new(bh_map);
557 * gfs2_block_map - Map a block from an inode to a disk block
559 * @lblock: The logical block number
560 * @bh_map: The bh to be mapped
561 * @create: True if its ok to alloc blocks to satify the request
563 * Sets buffer_mapped() if successful, sets buffer_boundary() if a
564 * read of metadata will be required before the next block can be
565 * mapped. Sets buffer_new() if new blocks were allocated.
570 int gfs2_block_map(struct inode *inode, sector_t lblock,
571 struct buffer_head *bh_map, int create)
573 struct gfs2_inode *ip = GFS2_I(inode);
574 struct gfs2_sbd *sdp = GFS2_SB(inode);
575 unsigned int bsize = sdp->sd_sb.sb_bsize;
576 const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
577 const u64 *arr = sdp->sd_heightsize;
584 struct buffer_head *bh;
589 memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
590 bmap_lock(ip, create);
591 clear_buffer_mapped(bh_map);
592 clear_buffer_new(bh_map);
593 clear_buffer_boundary(bh_map);
594 if (gfs2_is_dir(ip)) {
595 bsize = sdp->sd_jbsize;
596 arr = sdp->sd_jheightsize;
599 ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
603 height = ip->i_height;
604 size = (lblock + 1) * bsize;
605 while (size > arr[height])
607 find_metapath(sdp, lblock, &mp, height);
609 if (height > ip->i_height || gfs2_is_stuffed(ip))
611 ret = lookup_metapath(ip, &mp);
614 if (ret != ip->i_height)
616 ptr = metapointer(ip->i_height - 1, &mp);
619 map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
620 bh = mp.mp_bh[ip->i_height - 1];
621 len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
622 bh_map->b_size = (len << inode->i_blkbits);
624 set_buffer_boundary(bh_map);
627 release_metapath(&mp);
628 bmap_unlock(ip, create);
632 /* All allocations are done here, firstly check create flag */
634 BUG_ON(gfs2_is_stuffed(ip));
639 /* At this point ret is the tree depth of already allocated blocks */
640 ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
645 * Deprecated: do not use in new code
647 int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
649 struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
657 bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
658 ret = gfs2_block_map(inode, lblock, &bh, create);
659 *extlen = bh.b_size >> inode->i_blkbits;
660 *dblock = bh.b_blocknr;
669 * recursive_scan - recursively scan through the end of a file
671 * @dibh: the dinode buffer
672 * @mp: the path through the metadata to the point to start
673 * @height: the height the recursion is at
674 * @block: the indirect block to look at
675 * @first: 1 if this is the first block
676 * @bc: the call to make for each piece of metadata
677 * @data: data opaque to this function to pass to @bc
679 * When this is first called @height and @block should be zero and
680 * @first should be 1.
685 static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
686 struct metapath *mp, unsigned int height,
687 u64 block, int first, block_call_t bc,
690 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
691 struct buffer_head *bh = NULL;
692 __be64 *top, *bottom;
695 int mh_size = sizeof(struct gfs2_meta_header);
698 error = gfs2_meta_inode_buffer(ip, &bh);
703 top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
704 bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
706 error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
710 top = (__be64 *)(bh->b_data + mh_size) +
711 (first ? mp->mp_list[height] : 0);
713 bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
716 error = bc(ip, dibh, bh, top, bottom, height, data);
720 if (height < ip->i_height - 1)
721 for (; top < bottom; top++, first = 0) {
725 bn = be64_to_cpu(*top);
727 error = recursive_scan(ip, dibh, mp, height + 1, bn,
739 * do_strip - Look for a layer a particular layer of the file and strip it off
741 * @dibh: the dinode buffer
742 * @bh: A buffer of pointers
743 * @top: The first pointer in the buffer
744 * @bottom: One more than the last pointer
745 * @height: the height this buffer is at
746 * @data: a pointer to a struct strip_mine
751 static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
752 struct buffer_head *bh, __be64 *top, __be64 *bottom,
753 unsigned int height, void *data)
755 struct strip_mine *sm = data;
756 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
757 struct gfs2_rgrp_list rlist;
761 unsigned int rg_blocks = 0;
763 unsigned int revokes = 0;
770 if (height != sm->sm_height)
778 metadata = (height != ip->i_height - 1);
780 revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
782 error = gfs2_rindex_hold(sdp, &ip->i_alloc->al_ri_gh);
786 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
790 for (p = top; p < bottom; p++) {
794 bn = be64_to_cpu(*p);
796 if (bstart + blen == bn)
800 gfs2_rlist_add(sdp, &rlist, bstart);
808 gfs2_rlist_add(sdp, &rlist, bstart);
810 goto out; /* Nothing to do */
812 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
814 for (x = 0; x < rlist.rl_rgrps; x++) {
815 struct gfs2_rgrpd *rgd;
816 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
817 rg_blocks += rgd->rd_length;
820 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
824 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
825 RES_INDIRECT + RES_STATFS + RES_QUOTA,
830 down_write(&ip->i_rw_mutex);
832 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
833 gfs2_trans_add_bh(ip->i_gl, bh, 1);
838 for (p = top; p < bottom; p++) {
842 bn = be64_to_cpu(*p);
844 if (bstart + blen == bn)
849 gfs2_free_meta(ip, bstart, blen);
851 gfs2_free_data(ip, bstart, blen);
859 gfs2_add_inode_blocks(&ip->i_inode, -1);
863 gfs2_free_meta(ip, bstart, blen);
865 gfs2_free_data(ip, bstart, blen);
868 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
870 gfs2_dinode_out(ip, dibh->b_data);
872 up_write(&ip->i_rw_mutex);
877 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
879 gfs2_rlist_free(&rlist);
881 gfs2_glock_dq_uninit(&ip->i_alloc->al_ri_gh);
886 * do_grow - Make a file look bigger than it is
888 * @size: the size to set the file to
890 * Called with an exclusive lock on @ip.
895 static int do_grow(struct gfs2_inode *ip, u64 size)
897 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
898 struct gfs2_alloc *al;
899 struct buffer_head *dibh;
902 al = gfs2_alloc_get(ip);
906 error = gfs2_quota_lock_check(ip);
910 al->al_requested = sdp->sd_max_height + RES_DATA;
912 error = gfs2_inplace_reserve(ip);
916 error = gfs2_trans_begin(sdp,
917 sdp->sd_max_height + al->al_rgd->rd_length +
918 RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
922 error = gfs2_meta_inode_buffer(ip, &dibh);
926 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
927 if (gfs2_is_stuffed(ip)) {
928 error = gfs2_unstuff_dinode(ip, NULL);
934 ip->i_di.di_size = size;
935 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
936 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
937 gfs2_dinode_out(ip, dibh->b_data);
944 gfs2_inplace_release(ip);
946 gfs2_quota_unlock(ip);
954 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
956 * This is partly borrowed from ext3.
958 static int gfs2_block_truncate_page(struct address_space *mapping)
960 struct inode *inode = mapping->host;
961 struct gfs2_inode *ip = GFS2_I(inode);
962 loff_t from = inode->i_size;
963 unsigned long index = from >> PAGE_CACHE_SHIFT;
964 unsigned offset = from & (PAGE_CACHE_SIZE-1);
965 unsigned blocksize, iblock, length, pos;
966 struct buffer_head *bh;
970 page = grab_cache_page(mapping, index);
974 blocksize = inode->i_sb->s_blocksize;
975 length = blocksize - (offset & (blocksize - 1));
976 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
978 if (!page_has_buffers(page))
979 create_empty_buffers(page, blocksize, 0);
981 /* Find the buffer that contains "offset" */
982 bh = page_buffers(page);
984 while (offset >= pos) {
985 bh = bh->b_this_page;
992 if (!buffer_mapped(bh)) {
993 gfs2_block_map(inode, iblock, bh, 0);
994 /* unmapped? It's a hole - nothing to do */
995 if (!buffer_mapped(bh))
999 /* Ok, it's mapped. Make sure it's up-to-date */
1000 if (PageUptodate(page))
1001 set_buffer_uptodate(bh);
1003 if (!buffer_uptodate(bh)) {
1005 ll_rw_block(READ, 1, &bh);
1007 /* Uhhuh. Read error. Complain and punt. */
1008 if (!buffer_uptodate(bh))
1013 if (!gfs2_is_writeback(ip))
1014 gfs2_trans_add_bh(ip->i_gl, bh, 0);
1016 zero_user(page, offset, length);
1020 page_cache_release(page);
1024 static int trunc_start(struct gfs2_inode *ip, u64 size)
1026 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1027 struct buffer_head *dibh;
1028 int journaled = gfs2_is_jdata(ip);
1031 error = gfs2_trans_begin(sdp,
1032 RES_DINODE + (journaled ? RES_JDATA : 0), 0);
1036 error = gfs2_meta_inode_buffer(ip, &dibh);
1040 if (gfs2_is_stuffed(ip)) {
1041 ip->i_di.di_size = size;
1042 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1043 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1044 gfs2_dinode_out(ip, dibh->b_data);
1045 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
1049 if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
1050 error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
1053 ip->i_di.di_size = size;
1054 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1055 ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
1056 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1057 gfs2_dinode_out(ip, dibh->b_data);
1064 gfs2_trans_end(sdp);
1068 static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
1070 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1071 unsigned int height = ip->i_height;
1079 lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
1081 find_metapath(sdp, lblock, &mp, ip->i_height);
1082 if (!gfs2_alloc_get(ip))
1085 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1090 struct strip_mine sm;
1091 sm.sm_first = !!size;
1092 sm.sm_height = height;
1094 error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
1099 gfs2_quota_unhold(ip);
1106 static int trunc_end(struct gfs2_inode *ip)
1108 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1109 struct buffer_head *dibh;
1112 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1116 down_write(&ip->i_rw_mutex);
1118 error = gfs2_meta_inode_buffer(ip, &dibh);
1122 if (!ip->i_di.di_size) {
1124 ip->i_goal = ip->i_no_addr;
1125 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1127 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1128 ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
1130 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1131 gfs2_dinode_out(ip, dibh->b_data);
1135 up_write(&ip->i_rw_mutex);
1136 gfs2_trans_end(sdp);
1141 * do_shrink - make a file smaller
1143 * @size: the size to make the file
1144 * @truncator: function to truncate the last partial block
1146 * Called with an exclusive lock on @ip.
1151 static int do_shrink(struct gfs2_inode *ip, u64 size)
1155 error = trunc_start(ip, size);
1161 error = trunc_dealloc(ip, size);
1163 error = trunc_end(ip);
1168 static int do_touch(struct gfs2_inode *ip, u64 size)
1170 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1171 struct buffer_head *dibh;
1174 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1178 down_write(&ip->i_rw_mutex);
1180 error = gfs2_meta_inode_buffer(ip, &dibh);
1184 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
1185 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1186 gfs2_dinode_out(ip, dibh->b_data);
1190 up_write(&ip->i_rw_mutex);
1191 gfs2_trans_end(sdp);
1196 * gfs2_truncatei - make a file a given size
1198 * @size: the size to make the file
1199 * @truncator: function to truncate the last partial block
1201 * The file size can grow, shrink, or stay the same size.
1206 int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
1210 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_inode.i_mode)))
1213 if (size > ip->i_di.di_size)
1214 error = do_grow(ip, size);
1215 else if (size < ip->i_di.di_size)
1216 error = do_shrink(ip, size);
1218 /* update time stamps */
1219 error = do_touch(ip, size);
1224 int gfs2_truncatei_resume(struct gfs2_inode *ip)
1227 error = trunc_dealloc(ip, ip->i_di.di_size);
1229 error = trunc_end(ip);
1233 int gfs2_file_dealloc(struct gfs2_inode *ip)
1235 return trunc_dealloc(ip, 0);
1239 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1241 * @len: the number of bytes to be written to the file
1242 * @data_blocks: returns the number of data blocks required
1243 * @ind_blocks: returns the number of indirect blocks required
1247 void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
1248 unsigned int *data_blocks, unsigned int *ind_blocks)
1250 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1253 if (gfs2_is_dir(ip)) {
1254 *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
1255 *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
1257 *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
1258 *ind_blocks = 3 * (sdp->sd_max_height - 1);
1261 for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
1262 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1268 * gfs2_write_alloc_required - figure out if a write will require an allocation
1269 * @ip: the file being written to
1270 * @offset: the offset to write to
1271 * @len: the number of bytes being written
1272 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1277 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
1278 unsigned int len, int *alloc_required)
1280 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1281 struct buffer_head bh;
1283 u64 lblock, lblock_stop, size;
1285 *alloc_required = 0;
1290 if (gfs2_is_stuffed(ip)) {
1292 sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
1293 *alloc_required = 1;
1297 *alloc_required = 1;
1298 shift = sdp->sd_sb.sb_bsize_shift;
1299 if (gfs2_is_dir(ip)) {
1300 unsigned int bsize = sdp->sd_jbsize;
1302 do_div(lblock, bsize);
1303 lblock_stop = offset + len + bsize - 1;
1304 do_div(lblock_stop, bsize);
1306 u64 end_of_file = (ip->i_di.di_size + sdp->sd_sb.sb_bsize - 1) >> shift;
1307 lblock = offset >> shift;
1308 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
1309 if (lblock_stop > end_of_file)
1313 size = (lblock_stop - lblock) << shift;
1317 gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
1318 if (!buffer_mapped(&bh))
1321 lblock += (bh.b_size >> ip->i_inode.i_blkbits);
1324 *alloc_required = 0;