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 __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 gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
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 (sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
|| gfs2_is_jdata(ip
))
99 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
103 page_cache_release(page
);
110 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
111 * @ip: The GFS2 inode to unstuff
112 * @unstuffer: the routine that handles unstuffing a non-zero length file
113 * @private: private data for the unstuffer
115 * This routine unstuffs a dinode and returns it to a "normal" state such
116 * that the height can be grown in the traditional way.
121 int gfs2_unstuff_dinode(struct gfs2_inode
*ip
, struct page
*page
)
123 struct buffer_head
*bh
, *dibh
;
124 struct gfs2_dinode
*di
;
126 int isdir
= gfs2_is_dir(ip
);
129 down_write(&ip
->i_rw_mutex
);
131 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
135 if (ip
->i_di
.di_size
) {
136 /* Get a free block, fill it with the stuffed data,
137 and write it out to disk */
140 block
= gfs2_alloc_meta(ip
);
142 error
= gfs2_dir_get_new_buffer(ip
, block
, &bh
);
145 gfs2_buffer_copy_tail(bh
, sizeof(struct gfs2_meta_header
),
146 dibh
, sizeof(struct gfs2_dinode
));
149 block
= gfs2_alloc_data(ip
);
151 error
= gfs2_unstuffer_page(ip
, dibh
, block
, page
);
157 /* Set up the pointer to the new block */
159 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
160 di
= (struct gfs2_dinode
*)dibh
->b_data
;
161 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
163 if (ip
->i_di
.di_size
) {
164 *(__be64
*)(di
+ 1) = cpu_to_be64(block
);
165 ip
->i_di
.di_blocks
++;
166 gfs2_set_inode_blocks(&ip
->i_inode
);
167 di
->di_blocks
= cpu_to_be64(ip
->i_di
.di_blocks
);
170 ip
->i_di
.di_height
= 1;
171 di
->di_height
= cpu_to_be16(1);
176 up_write(&ip
->i_rw_mutex
);
181 * calc_tree_height - Calculate the height of a metadata tree
182 * @ip: The GFS2 inode
183 * @size: The proposed size of the file
185 * Work out how tall a metadata tree needs to be in order to accommodate a
186 * file of a particular size. If size is less than the current size of
187 * the inode, then the current size of the inode is used instead of the
190 * Returns: the height the tree should be
193 static unsigned int calc_tree_height(struct gfs2_inode
*ip
, u64 size
)
195 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
197 unsigned int max
, height
;
199 if (ip
->i_di
.di_size
> size
)
200 size
= ip
->i_di
.di_size
;
202 if (gfs2_is_dir(ip
)) {
203 arr
= sdp
->sd_jheightsize
;
204 max
= sdp
->sd_max_jheight
;
206 arr
= sdp
->sd_heightsize
;
207 max
= sdp
->sd_max_height
;
210 for (height
= 0; height
< max
; height
++)
211 if (arr
[height
] >= size
)
218 * build_height - Build a metadata tree of the requested height
219 * @ip: The GFS2 inode
220 * @height: The height to build to
226 static int build_height(struct inode
*inode
, unsigned height
)
228 struct gfs2_inode
*ip
= GFS2_I(inode
);
229 unsigned new_height
= height
- ip
->i_di
.di_height
;
230 struct buffer_head
*dibh
;
231 struct buffer_head
*blocks
[GFS2_MAX_META_HEIGHT
];
232 struct gfs2_dinode
*di
;
238 if (height
<= ip
->i_di
.di_height
)
241 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
245 for(n
= 0; n
< new_height
; n
++) {
246 bn
= gfs2_alloc_meta(ip
);
247 blocks
[n
] = gfs2_meta_new(ip
->i_gl
, bn
);
248 gfs2_trans_add_bh(ip
->i_gl
, blocks
[n
], 1);
252 bn
= blocks
[0]->b_blocknr
;
253 if (new_height
> 1) {
254 for(; n
< new_height
-1; n
++) {
255 gfs2_metatype_set(blocks
[n
], GFS2_METATYPE_IN
,
257 gfs2_buffer_clear_tail(blocks
[n
],
258 sizeof(struct gfs2_meta_header
));
259 bp
= (__be64
*)(blocks
[n
]->b_data
+
260 sizeof(struct gfs2_meta_header
));
261 *bp
= cpu_to_be64(blocks
[n
+1]->b_blocknr
);
266 gfs2_metatype_set(blocks
[n
], GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
267 gfs2_buffer_copy_tail(blocks
[n
], sizeof(struct gfs2_meta_header
),
268 dibh
, sizeof(struct gfs2_dinode
));
270 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
271 di
= (struct gfs2_dinode
*)dibh
->b_data
;
272 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
273 *(__be64
*)(di
+ 1) = cpu_to_be64(bn
);
274 ip
->i_di
.di_height
+= new_height
;
275 ip
->i_di
.di_blocks
+= new_height
;
276 gfs2_set_inode_blocks(&ip
->i_inode
);
277 di
->di_height
= cpu_to_be16(ip
->i_di
.di_height
);
278 di
->di_blocks
= cpu_to_be64(ip
->i_di
.di_blocks
);
284 * find_metapath - Find path through the metadata tree
285 * @ip: The inode pointer
286 * @mp: The metapath to return the result in
287 * @block: The disk block to look up
289 * This routine returns a struct metapath structure that defines a path
290 * through the metadata of inode "ip" to get to block "block".
293 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
294 * filesystem with a blocksize of 4096.
296 * find_metapath() would return a struct metapath structure set to:
297 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
298 * and mp_list[2] = 165.
300 * That means that in order to get to the block containing the byte at
301 * offset 101342453, we would load the indirect block pointed to by pointer
302 * 0 in the dinode. We would then load the indirect block pointed to by
303 * pointer 48 in that indirect block. We would then load the data block
304 * pointed to by pointer 165 in that indirect block.
306 * ----------------------------------------
311 * ----------------------------------------
315 * ----------------------------------------
319 * |0 5 6 7 8 9 0 1 2|
320 * ----------------------------------------
324 * ----------------------------------------
329 * ----------------------------------------
333 * ----------------------------------------
334 * | Data block containing offset |
338 * ----------------------------------------
342 static void find_metapath(struct gfs2_inode
*ip
, u64 block
,
345 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
349 for (i
= ip
->i_di
.di_height
; i
--;)
350 mp
->mp_list
[i
] = do_div(b
, sdp
->sd_inptrs
);
355 * metapointer - Return pointer to start of metadata in a buffer
357 * @height: The metadata height (0 = dinode)
360 * Return a pointer to the block number of the next height of the metadata
361 * tree given a buffer containing the pointer to the current height of the
365 static inline __be64
*metapointer(struct buffer_head
*bh
, int *boundary
,
366 unsigned int height
, const struct metapath
*mp
)
368 unsigned int head_size
= (height
> 0) ?
369 sizeof(struct gfs2_meta_header
) : sizeof(struct gfs2_dinode
);
372 ptr
= ((__be64
*)(bh
->b_data
+ head_size
)) + mp
->mp_list
[height
];
373 if (ptr
+ 1 == (__be64
*)(bh
->b_data
+ bh
->b_size
))
379 * lookup_block - Get the next metadata block in metadata tree
380 * @ip: The GFS2 inode
381 * @bh: Buffer containing the pointers to metadata blocks
382 * @height: The height of the tree (0 = dinode)
384 * @create: Non-zero if we may create a new meatdata block
385 * @new: Used to indicate if we did create a new metadata block
386 * @block: the returned disk block number
388 * Given a metatree, complete to a particular height, checks to see if the next
389 * height of the tree exists. If not the next height of the tree is created.
390 * The block number of the next height of the metadata tree is returned.
394 static int lookup_block(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
395 unsigned int height
, struct metapath
*mp
, int create
,
396 int *new, u64
*block
)
399 __be64
*ptr
= metapointer(bh
, &boundary
, height
, mp
);
402 *block
= be64_to_cpu(*ptr
);
411 if (height
== ip
->i_di
.di_height
- 1 && !gfs2_is_dir(ip
))
412 *block
= gfs2_alloc_data(ip
);
414 *block
= gfs2_alloc_meta(ip
);
416 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
418 *ptr
= cpu_to_be64(*block
);
419 ip
->i_di
.di_blocks
++;
420 gfs2_set_inode_blocks(&ip
->i_inode
);
426 static inline void bmap_lock(struct inode
*inode
, int create
)
428 struct gfs2_inode
*ip
= GFS2_I(inode
);
430 down_write(&ip
->i_rw_mutex
);
432 down_read(&ip
->i_rw_mutex
);
435 static inline void bmap_unlock(struct inode
*inode
, int create
)
437 struct gfs2_inode
*ip
= GFS2_I(inode
);
439 up_write(&ip
->i_rw_mutex
);
441 up_read(&ip
->i_rw_mutex
);
445 * gfs2_block_map - Map a block from an inode to a disk block
447 * @lblock: The logical block number
448 * @bh_map: The bh to be mapped
450 * Find the block number on the current device which corresponds to an
451 * inode's block. If the block had to be created, "new" will be set.
456 int gfs2_block_map(struct inode
*inode
, u64 lblock
, int create
,
457 struct buffer_head
*bh_map
)
459 struct gfs2_inode
*ip
= GFS2_I(inode
);
460 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
461 struct buffer_head
*bh
;
464 unsigned int end_of_metadata
;
470 unsigned int maxlen
= bh_map
->b_size
>> inode
->i_blkbits
;
476 if (gfs2_assert_warn(sdp
, !gfs2_is_stuffed(ip
)))
479 bmap_lock(inode
, create
);
480 clear_buffer_mapped(bh_map
);
481 clear_buffer_new(bh_map
);
482 clear_buffer_boundary(bh_map
);
483 bsize
= gfs2_is_dir(ip
) ? sdp
->sd_jbsize
: sdp
->sd_sb
.sb_bsize
;
484 size
= (lblock
+ 1) * bsize
;
486 if (size
> ip
->i_di
.di_size
) {
487 height
= calc_tree_height(ip
, size
);
488 if (ip
->i_di
.di_height
< height
) {
492 error
= build_height(inode
, height
);
498 find_metapath(ip
, lblock
, &mp
);
499 end_of_metadata
= ip
->i_di
.di_height
- 1;
500 error
= gfs2_meta_inode_buffer(ip
, &bh
);
504 for (x
= 0; x
< end_of_metadata
; x
++) {
505 lookup_block(ip
, bh
, x
, &mp
, create
, &new, &dblock
);
510 error
= gfs2_meta_indirect_buffer(ip
, x
+1, dblock
, new, &bh
);
515 boundary
= lookup_block(ip
, bh
, end_of_metadata
, &mp
, create
, &new, &dblock
);
517 map_bh(bh_map
, inode
->i_sb
, dblock
);
519 set_buffer_boundary(bh_map
);
521 struct buffer_head
*dibh
;
522 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
524 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
525 gfs2_dinode_out(ip
, dibh
->b_data
);
528 set_buffer_new(bh_map
);
531 while(--maxlen
&& !buffer_boundary(bh_map
)) {
534 mp
.mp_list
[end_of_metadata
]++;
535 boundary
= lookup_block(ip
, bh
, end_of_metadata
, &mp
, 0, &new, &eblock
);
536 if (eblock
!= ++dblock
)
538 bh_map
->b_size
+= (1 << inode
->i_blkbits
);
540 set_buffer_boundary(bh_map
);
548 bmap_unlock(inode
, create
);
552 int gfs2_extent_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, unsigned *extlen
)
554 struct buffer_head bh
= { .b_state
= 0, .b_blocknr
= 0 };
562 bh
.b_size
= 1 << (inode
->i_blkbits
+ 5);
563 ret
= gfs2_block_map(inode
, lblock
, create
, &bh
);
564 *extlen
= bh
.b_size
>> inode
->i_blkbits
;
565 *dblock
= bh
.b_blocknr
;
574 * recursive_scan - recursively scan through the end of a file
576 * @dibh: the dinode buffer
577 * @mp: the path through the metadata to the point to start
578 * @height: the height the recursion is at
579 * @block: the indirect block to look at
580 * @first: 1 if this is the first block
581 * @bc: the call to make for each piece of metadata
582 * @data: data opaque to this function to pass to @bc
584 * When this is first called @height and @block should be zero and
585 * @first should be 1.
590 static int recursive_scan(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
591 struct metapath
*mp
, unsigned int height
,
592 u64 block
, int first
, block_call_t bc
,
595 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
596 struct buffer_head
*bh
= NULL
;
597 __be64
*top
, *bottom
;
600 int mh_size
= sizeof(struct gfs2_meta_header
);
603 error
= gfs2_meta_inode_buffer(ip
, &bh
);
608 top
= (__be64
*)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) + mp
->mp_list
[0];
609 bottom
= (__be64
*)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) + sdp
->sd_diptrs
;
611 error
= gfs2_meta_indirect_buffer(ip
, height
, block
, 0, &bh
);
615 top
= (__be64
*)(bh
->b_data
+ mh_size
) +
616 (first
? mp
->mp_list
[height
] : 0);
618 bottom
= (__be64
*)(bh
->b_data
+ mh_size
) + sdp
->sd_inptrs
;
621 error
= bc(ip
, dibh
, bh
, top
, bottom
, height
, data
);
625 if (height
< ip
->i_di
.di_height
- 1)
626 for (; top
< bottom
; top
++, first
= 0) {
630 bn
= be64_to_cpu(*top
);
632 error
= recursive_scan(ip
, dibh
, mp
, height
+ 1, bn
,
644 * do_strip - Look for a layer a particular layer of the file and strip it off
646 * @dibh: the dinode buffer
647 * @bh: A buffer of pointers
648 * @top: The first pointer in the buffer
649 * @bottom: One more than the last pointer
650 * @height: the height this buffer is at
651 * @data: a pointer to a struct strip_mine
656 static int do_strip(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
657 struct buffer_head
*bh
, __be64
*top
, __be64
*bottom
,
658 unsigned int height
, void *data
)
660 struct strip_mine
*sm
= data
;
661 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
662 struct gfs2_rgrp_list rlist
;
666 unsigned int rg_blocks
= 0;
668 unsigned int revokes
= 0;
675 if (height
!= sm
->sm_height
)
683 metadata
= (height
!= ip
->i_di
.di_height
- 1);
685 revokes
= (height
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
687 error
= gfs2_rindex_hold(sdp
, &ip
->i_alloc
.al_ri_gh
);
691 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
695 for (p
= top
; p
< bottom
; p
++) {
699 bn
= be64_to_cpu(*p
);
701 if (bstart
+ blen
== bn
)
705 gfs2_rlist_add(sdp
, &rlist
, bstart
);
713 gfs2_rlist_add(sdp
, &rlist
, bstart
);
715 goto out
; /* Nothing to do */
717 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
, 0);
719 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
720 struct gfs2_rgrpd
*rgd
;
721 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
722 rg_blocks
+= rgd
->rd_length
;
725 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
729 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+
730 RES_INDIRECT
+ RES_STATFS
+ RES_QUOTA
,
735 down_write(&ip
->i_rw_mutex
);
737 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
738 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
743 for (p
= top
; p
< bottom
; p
++) {
747 bn
= be64_to_cpu(*p
);
749 if (bstart
+ blen
== bn
)
754 gfs2_free_meta(ip
, bstart
, blen
);
756 gfs2_free_data(ip
, bstart
, blen
);
764 if (!ip
->i_di
.di_blocks
)
765 gfs2_consist_inode(ip
);
766 ip
->i_di
.di_blocks
--;
767 gfs2_set_inode_blocks(&ip
->i_inode
);
771 gfs2_free_meta(ip
, bstart
, blen
);
773 gfs2_free_data(ip
, bstart
, blen
);
776 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
778 gfs2_dinode_out(ip
, dibh
->b_data
);
780 up_write(&ip
->i_rw_mutex
);
785 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
787 gfs2_rlist_free(&rlist
);
789 gfs2_glock_dq_uninit(&ip
->i_alloc
.al_ri_gh
);
794 * do_grow - Make a file look bigger than it is
796 * @size: the size to set the file to
798 * Called with an exclusive lock on @ip.
803 static int do_grow(struct gfs2_inode
*ip
, u64 size
)
805 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
806 struct gfs2_alloc
*al
;
807 struct buffer_head
*dibh
;
811 al
= gfs2_alloc_get(ip
);
813 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
817 error
= gfs2_quota_check(ip
, ip
->i_inode
.i_uid
, ip
->i_inode
.i_gid
);
821 al
->al_requested
= sdp
->sd_max_height
+ RES_DATA
;
823 error
= gfs2_inplace_reserve(ip
);
827 error
= gfs2_trans_begin(sdp
,
828 sdp
->sd_max_height
+ al
->al_rgd
->rd_length
+
829 RES_JDATA
+ RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
833 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
)) {
834 if (gfs2_is_stuffed(ip
)) {
835 error
= gfs2_unstuff_dinode(ip
, NULL
);
840 h
= calc_tree_height(ip
, size
);
841 if (ip
->i_di
.di_height
< h
) {
842 down_write(&ip
->i_rw_mutex
);
843 error
= build_height(&ip
->i_inode
, h
);
844 up_write(&ip
->i_rw_mutex
);
850 ip
->i_di
.di_size
= size
;
851 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
853 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
857 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
858 gfs2_dinode_out(ip
, dibh
->b_data
);
864 gfs2_inplace_release(ip
);
866 gfs2_quota_unlock(ip
);
874 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
876 * This is partly borrowed from ext3.
878 static int gfs2_block_truncate_page(struct address_space
*mapping
)
880 struct inode
*inode
= mapping
->host
;
881 struct gfs2_inode
*ip
= GFS2_I(inode
);
882 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
883 loff_t from
= inode
->i_size
;
884 unsigned long index
= from
>> PAGE_CACHE_SHIFT
;
885 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
886 unsigned blocksize
, iblock
, length
, pos
;
887 struct buffer_head
*bh
;
891 page
= grab_cache_page(mapping
, index
);
895 blocksize
= inode
->i_sb
->s_blocksize
;
896 length
= blocksize
- (offset
& (blocksize
- 1));
897 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
899 if (!page_has_buffers(page
))
900 create_empty_buffers(page
, blocksize
, 0);
902 /* Find the buffer that contains "offset" */
903 bh
= page_buffers(page
);
905 while (offset
>= pos
) {
906 bh
= bh
->b_this_page
;
913 if (!buffer_mapped(bh
)) {
914 gfs2_get_block(inode
, iblock
, bh
, 0);
915 /* unmapped? It's a hole - nothing to do */
916 if (!buffer_mapped(bh
))
920 /* Ok, it's mapped. Make sure it's up-to-date */
921 if (PageUptodate(page
))
922 set_buffer_uptodate(bh
);
924 if (!buffer_uptodate(bh
)) {
926 ll_rw_block(READ
, 1, &bh
);
928 /* Uhhuh. Read error. Complain and punt. */
929 if (!buffer_uptodate(bh
))
934 if (sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
|| gfs2_is_jdata(ip
))
935 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
937 zero_user_page(page
, offset
, length
, KM_USER0
);
941 page_cache_release(page
);
945 static int trunc_start(struct gfs2_inode
*ip
, u64 size
)
947 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
948 struct buffer_head
*dibh
;
949 int journaled
= gfs2_is_jdata(ip
);
952 error
= gfs2_trans_begin(sdp
,
953 RES_DINODE
+ (journaled
? RES_JDATA
: 0), 0);
957 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
961 if (gfs2_is_stuffed(ip
)) {
962 ip
->i_di
.di_size
= size
;
963 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
964 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
965 gfs2_dinode_out(ip
, dibh
->b_data
);
966 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
) + size
);
970 if (size
& (u64
)(sdp
->sd_sb
.sb_bsize
- 1))
971 error
= gfs2_block_truncate_page(ip
->i_inode
.i_mapping
);
974 ip
->i_di
.di_size
= size
;
975 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
976 ip
->i_di
.di_flags
|= GFS2_DIF_TRUNC_IN_PROG
;
977 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
978 gfs2_dinode_out(ip
, dibh
->b_data
);
989 static int trunc_dealloc(struct gfs2_inode
*ip
, u64 size
)
991 unsigned int height
= ip
->i_di
.di_height
;
999 lblock
= (size
- 1) >> GFS2_SB(&ip
->i_inode
)->sd_sb
.sb_bsize_shift
;
1001 find_metapath(ip
, lblock
, &mp
);
1004 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
1009 struct strip_mine sm
;
1010 sm
.sm_first
= !!size
;
1011 sm
.sm_height
= height
;
1013 error
= recursive_scan(ip
, NULL
, &mp
, 0, 0, 1, do_strip
, &sm
);
1018 gfs2_quota_unhold(ip
);
1025 static int trunc_end(struct gfs2_inode
*ip
)
1027 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1028 struct buffer_head
*dibh
;
1031 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
1035 down_write(&ip
->i_rw_mutex
);
1037 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1041 if (!ip
->i_di
.di_size
) {
1042 ip
->i_di
.di_height
= 0;
1043 ip
->i_di
.di_goal_meta
=
1044 ip
->i_di
.di_goal_data
=
1046 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
1048 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1049 ip
->i_di
.di_flags
&= ~GFS2_DIF_TRUNC_IN_PROG
;
1051 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1052 gfs2_dinode_out(ip
, dibh
->b_data
);
1056 up_write(&ip
->i_rw_mutex
);
1057 gfs2_trans_end(sdp
);
1062 * do_shrink - make a file smaller
1064 * @size: the size to make the file
1065 * @truncator: function to truncate the last partial block
1067 * Called with an exclusive lock on @ip.
1072 static int do_shrink(struct gfs2_inode
*ip
, u64 size
)
1076 error
= trunc_start(ip
, size
);
1082 error
= trunc_dealloc(ip
, size
);
1084 error
= trunc_end(ip
);
1089 static int do_touch(struct gfs2_inode
*ip
, u64 size
)
1091 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1092 struct buffer_head
*dibh
;
1095 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
1099 down_write(&ip
->i_rw_mutex
);
1101 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1105 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1106 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1107 gfs2_dinode_out(ip
, dibh
->b_data
);
1111 up_write(&ip
->i_rw_mutex
);
1112 gfs2_trans_end(sdp
);
1117 * gfs2_truncatei - make a file a given size
1119 * @size: the size to make the file
1120 * @truncator: function to truncate the last partial block
1122 * The file size can grow, shrink, or stay the same size.
1127 int gfs2_truncatei(struct gfs2_inode
*ip
, u64 size
)
1131 if (gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), S_ISREG(ip
->i_inode
.i_mode
)))
1134 if (size
> ip
->i_di
.di_size
)
1135 error
= do_grow(ip
, size
);
1136 else if (size
< ip
->i_di
.di_size
)
1137 error
= do_shrink(ip
, size
);
1139 /* update time stamps */
1140 error
= do_touch(ip
, size
);
1145 int gfs2_truncatei_resume(struct gfs2_inode
*ip
)
1148 error
= trunc_dealloc(ip
, ip
->i_di
.di_size
);
1150 error
= trunc_end(ip
);
1154 int gfs2_file_dealloc(struct gfs2_inode
*ip
)
1156 return trunc_dealloc(ip
, 0);
1160 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1162 * @len: the number of bytes to be written to the file
1163 * @data_blocks: returns the number of data blocks required
1164 * @ind_blocks: returns the number of indirect blocks required
1168 void gfs2_write_calc_reserv(struct gfs2_inode
*ip
, unsigned int len
,
1169 unsigned int *data_blocks
, unsigned int *ind_blocks
)
1171 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1174 if (gfs2_is_dir(ip
)) {
1175 *data_blocks
= DIV_ROUND_UP(len
, sdp
->sd_jbsize
) + 2;
1176 *ind_blocks
= 3 * (sdp
->sd_max_jheight
- 1);
1178 *data_blocks
= (len
>> sdp
->sd_sb
.sb_bsize_shift
) + 3;
1179 *ind_blocks
= 3 * (sdp
->sd_max_height
- 1);
1182 for (tmp
= *data_blocks
; tmp
> sdp
->sd_diptrs
;) {
1183 tmp
= DIV_ROUND_UP(tmp
, sdp
->sd_inptrs
);
1189 * gfs2_write_alloc_required - figure out if a write will require an allocation
1190 * @ip: the file being written to
1191 * @offset: the offset to write to
1192 * @len: the number of bytes being written
1193 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1198 int gfs2_write_alloc_required(struct gfs2_inode
*ip
, u64 offset
,
1199 unsigned int len
, int *alloc_required
)
1201 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1202 u64 lblock
, lblock_stop
, dblock
;
1207 *alloc_required
= 0;
1212 if (gfs2_is_stuffed(ip
)) {
1214 sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
1215 *alloc_required
= 1;
1219 if (gfs2_is_dir(ip
)) {
1220 unsigned int bsize
= sdp
->sd_jbsize
;
1222 do_div(lblock
, bsize
);
1223 lblock_stop
= offset
+ len
+ bsize
- 1;
1224 do_div(lblock_stop
, bsize
);
1226 unsigned int shift
= sdp
->sd_sb
.sb_bsize_shift
;
1227 lblock
= offset
>> shift
;
1228 lblock_stop
= (offset
+ len
+ sdp
->sd_sb
.sb_bsize
- 1) >> shift
;
1231 for (; lblock
< lblock_stop
; lblock
+= extlen
) {
1232 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new, &dblock
, &extlen
);
1237 *alloc_required
= 1;