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_meta(ip
);
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 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
);
171 di
->di_height
= cpu_to_be16(1);
176 up_write(&ip
->i_rw_mutex
);
181 * build_height - Build a metadata tree of the requested height
182 * @ip: The GFS2 inode
183 * @height: The height to build to
189 static int build_height(struct inode
*inode
, struct metapath
*mp
, unsigned height
)
191 struct gfs2_inode
*ip
= GFS2_I(inode
);
192 unsigned new_height
= height
- ip
->i_height
;
193 struct buffer_head
*dibh
;
194 struct gfs2_dinode
*di
;
200 if (height
<= ip
->i_height
)
203 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
207 for(n
= 0; n
< new_height
; n
++) {
208 bn
= gfs2_alloc_meta(ip
);
209 gfs2_trans_add_unrevoke(GFS2_SB(inode
), bn
, 1);
210 mp
->mp_bh
[n
] = gfs2_meta_new(ip
->i_gl
, bn
);
211 gfs2_trans_add_bh(ip
->i_gl
, mp
->mp_bh
[n
], 1);
215 bn
= mp
->mp_bh
[0]->b_blocknr
;
216 if (new_height
> 1) {
217 for(; n
< new_height
-1; n
++) {
218 gfs2_metatype_set(mp
->mp_bh
[n
], GFS2_METATYPE_IN
,
220 gfs2_buffer_clear_tail(mp
->mp_bh
[n
],
221 sizeof(struct gfs2_meta_header
));
222 bp
= (__be64
*)(mp
->mp_bh
[n
]->b_data
+
223 sizeof(struct gfs2_meta_header
));
224 *bp
= cpu_to_be64(mp
->mp_bh
[n
+1]->b_blocknr
);
225 brelse(mp
->mp_bh
[n
]);
229 gfs2_metatype_set(mp
->mp_bh
[n
], GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
230 gfs2_buffer_copy_tail(mp
->mp_bh
[n
], sizeof(struct gfs2_meta_header
),
231 dibh
, sizeof(struct gfs2_dinode
));
232 brelse(mp
->mp_bh
[n
]);
233 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
234 di
= (struct gfs2_dinode
*)dibh
->b_data
;
235 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
236 *(__be64
*)(di
+ 1) = cpu_to_be64(bn
);
237 ip
->i_height
+= new_height
;
238 ip
->i_di
.di_blocks
+= new_height
;
239 gfs2_set_inode_blocks(&ip
->i_inode
);
240 di
->di_height
= cpu_to_be16(ip
->i_height
);
241 di
->di_blocks
= cpu_to_be64(ip
->i_di
.di_blocks
);
247 * find_metapath - Find path through the metadata tree
248 * @ip: The inode pointer
249 * @mp: The metapath to return the result in
250 * @block: The disk block to look up
252 * This routine returns a struct metapath structure that defines a path
253 * through the metadata of inode "ip" to get to block "block".
256 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
257 * filesystem with a blocksize of 4096.
259 * find_metapath() would return a struct metapath structure set to:
260 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
261 * and mp_list[2] = 165.
263 * That means that in order to get to the block containing the byte at
264 * offset 101342453, we would load the indirect block pointed to by pointer
265 * 0 in the dinode. We would then load the indirect block pointed to by
266 * pointer 48 in that indirect block. We would then load the data block
267 * pointed to by pointer 165 in that indirect block.
269 * ----------------------------------------
274 * ----------------------------------------
278 * ----------------------------------------
282 * |0 5 6 7 8 9 0 1 2|
283 * ----------------------------------------
287 * ----------------------------------------
292 * ----------------------------------------
296 * ----------------------------------------
297 * | Data block containing offset |
301 * ----------------------------------------
305 static void find_metapath(struct gfs2_inode
*ip
, u64 block
,
308 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
311 for (i
= ip
->i_height
; i
--;)
312 mp
->mp_list
[i
] = do_div(block
, sdp
->sd_inptrs
);
317 * metapointer - Return pointer to start of metadata in a buffer
318 * @height: The metadata height (0 = dinode)
321 * Return a pointer to the block number of the next height of the metadata
322 * tree given a buffer containing the pointer to the current height of the
326 static inline __be64
*metapointer(int *boundary
, unsigned int height
,
327 const struct metapath
*mp
)
329 struct buffer_head
*bh
= mp
->mp_bh
[height
];
330 unsigned int head_size
= (height
> 0) ?
331 sizeof(struct gfs2_meta_header
) : sizeof(struct gfs2_dinode
);
334 ptr
= ((__be64
*)(bh
->b_data
+ head_size
)) + mp
->mp_list
[height
];
335 if (ptr
+ 1 == (__be64
*)(bh
->b_data
+ bh
->b_size
))
341 * lookup_block - Get the next metadata block in metadata tree
342 * @ip: The GFS2 inode
343 * @height: The height of the tree (0 = dinode)
345 * @create: Non-zero if we may create a new meatdata block
346 * @new: Used to indicate if we did create a new metadata block
347 * @block: the returned disk block number
349 * Given a metatree, complete to a particular height, checks to see if the next
350 * height of the tree exists. If not the next height of the tree is created.
351 * The block number of the next height of the metadata tree is returned.
355 static int lookup_block(struct gfs2_inode
*ip
, unsigned int height
,
356 struct metapath
*mp
, int create
,
357 int *new, u64
*block
)
360 __be64
*ptr
= metapointer(&boundary
, height
, mp
);
363 *block
= be64_to_cpu(*ptr
);
372 if (height
== ip
->i_height
- 1 && !gfs2_is_dir(ip
))
373 *block
= gfs2_alloc_data(ip
);
375 *block
= gfs2_alloc_meta(ip
);
376 gfs2_trans_add_unrevoke(GFS2_SB(&ip
->i_inode
), *block
, 1);
379 gfs2_trans_add_bh(ip
->i_gl
, mp
->mp_bh
[height
], 1);
381 *ptr
= cpu_to_be64(*block
);
382 ip
->i_di
.di_blocks
++;
383 gfs2_set_inode_blocks(&ip
->i_inode
);
389 static int lookup_metapath(struct inode
*inode
, struct metapath
*mp
,
390 int create
, int *new, u64
*dblock
)
392 struct buffer_head
*bh
;
393 struct gfs2_inode
*ip
= GFS2_I(inode
);
394 unsigned int end_of_metadata
= ip
->i_height
- 1;
396 int ret
= gfs2_meta_inode_buffer(ip
, &bh
);
402 for (x
= 0; x
< end_of_metadata
; x
++) {
403 lookup_block(ip
, x
, mp
, create
, new, dblock
);
407 ret
= gfs2_meta_indirect_buffer(ip
, x
+1, *dblock
, *new, &mp
->mp_bh
[x
+1]);
412 return lookup_block(ip
, end_of_metadata
, mp
, create
, new, dblock
);
415 static void release_metapath(struct metapath
*mp
)
419 for (i
= 0; i
< GFS2_MAX_META_HEIGHT
; i
++)
421 brelse(mp
->mp_bh
[i
]);
424 static inline void bmap_lock(struct inode
*inode
, int create
)
426 struct gfs2_inode
*ip
= GFS2_I(inode
);
428 down_write(&ip
->i_rw_mutex
);
430 down_read(&ip
->i_rw_mutex
);
433 static inline void bmap_unlock(struct inode
*inode
, int create
)
435 struct gfs2_inode
*ip
= GFS2_I(inode
);
437 up_write(&ip
->i_rw_mutex
);
439 up_read(&ip
->i_rw_mutex
);
443 * gfs2_block_map - Map a block from an inode to a disk block
445 * @lblock: The logical block number
446 * @bh_map: The bh to be mapped
448 * Find the block number on the current device which corresponds to an
449 * inode's block. If the block had to be created, "new" will be set.
454 int gfs2_block_map(struct inode
*inode
, sector_t lblock
,
455 struct buffer_head
*bh_map
, int create
)
457 struct gfs2_inode
*ip
= GFS2_I(inode
);
458 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
459 unsigned int bsize
= sdp
->sd_sb
.sb_bsize
;
464 unsigned int maxlen
= bh_map
->b_size
>> inode
->i_blkbits
;
467 const u64
*arr
= sdp
->sd_heightsize
;
470 if (gfs2_assert_warn(sdp
, !gfs2_is_stuffed(ip
)))
473 memset(mp
.mp_bh
, 0, sizeof(mp
.mp_bh
));
474 bmap_lock(inode
, create
);
475 clear_buffer_mapped(bh_map
);
476 clear_buffer_new(bh_map
);
477 clear_buffer_boundary(bh_map
);
478 if (gfs2_is_dir(ip
)) {
479 bsize
= sdp
->sd_jbsize
;
480 arr
= sdp
->sd_jheightsize
;
482 size
= (lblock
+ 1) * bsize
;
484 if (size
> arr
[ip
->i_height
]) {
485 u8 height
= ip
->i_height
;
488 while (size
> arr
[height
])
490 error
= build_height(inode
, &mp
, height
);
495 find_metapath(ip
, lblock
, &mp
);
496 error
= lookup_metapath(inode
, &mp
, create
, &new, &dblock
);
502 map_bh(bh_map
, inode
->i_sb
, dblock
);
504 set_buffer_boundary(bh_map
);
506 gfs2_trans_add_bh(ip
->i_gl
, mp
.mp_bh
[0], 1);
507 gfs2_dinode_out(ip
, mp
.mp_bh
[0]->b_data
);
508 set_buffer_new(bh_map
);
511 while(--maxlen
&& !buffer_boundary(bh_map
)) {
513 mp
.mp_list
[ip
->i_height
- 1]++;
514 boundary
= lookup_block(ip
, ip
->i_height
- 1, &mp
, 0, &new, &eblock
);
515 if (eblock
!= ++dblock
)
517 bh_map
->b_size
+= (1 << inode
->i_blkbits
);
519 set_buffer_boundary(bh_map
);
525 release_metapath(&mp
);
526 bmap_unlock(inode
, create
);
531 * Deprecated: do not use in new code
533 int gfs2_extent_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, unsigned *extlen
)
535 struct buffer_head bh
= { .b_state
= 0, .b_blocknr
= 0 };
543 bh
.b_size
= 1 << (inode
->i_blkbits
+ 5);
544 ret
= gfs2_block_map(inode
, lblock
, &bh
, create
);
545 *extlen
= bh
.b_size
>> inode
->i_blkbits
;
546 *dblock
= bh
.b_blocknr
;
555 * recursive_scan - recursively scan through the end of a file
557 * @dibh: the dinode buffer
558 * @mp: the path through the metadata to the point to start
559 * @height: the height the recursion is at
560 * @block: the indirect block to look at
561 * @first: 1 if this is the first block
562 * @bc: the call to make for each piece of metadata
563 * @data: data opaque to this function to pass to @bc
565 * When this is first called @height and @block should be zero and
566 * @first should be 1.
571 static int recursive_scan(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
572 struct metapath
*mp
, unsigned int height
,
573 u64 block
, int first
, block_call_t bc
,
576 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
577 struct buffer_head
*bh
= NULL
;
578 __be64
*top
, *bottom
;
581 int mh_size
= sizeof(struct gfs2_meta_header
);
584 error
= gfs2_meta_inode_buffer(ip
, &bh
);
589 top
= (__be64
*)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) + mp
->mp_list
[0];
590 bottom
= (__be64
*)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) + sdp
->sd_diptrs
;
592 error
= gfs2_meta_indirect_buffer(ip
, height
, block
, 0, &bh
);
596 top
= (__be64
*)(bh
->b_data
+ mh_size
) +
597 (first
? mp
->mp_list
[height
] : 0);
599 bottom
= (__be64
*)(bh
->b_data
+ mh_size
) + sdp
->sd_inptrs
;
602 error
= bc(ip
, dibh
, bh
, top
, bottom
, height
, data
);
606 if (height
< ip
->i_height
- 1)
607 for (; top
< bottom
; top
++, first
= 0) {
611 bn
= be64_to_cpu(*top
);
613 error
= recursive_scan(ip
, dibh
, mp
, height
+ 1, bn
,
625 * do_strip - Look for a layer a particular layer of the file and strip it off
627 * @dibh: the dinode buffer
628 * @bh: A buffer of pointers
629 * @top: The first pointer in the buffer
630 * @bottom: One more than the last pointer
631 * @height: the height this buffer is at
632 * @data: a pointer to a struct strip_mine
637 static int do_strip(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
638 struct buffer_head
*bh
, __be64
*top
, __be64
*bottom
,
639 unsigned int height
, void *data
)
641 struct strip_mine
*sm
= data
;
642 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
643 struct gfs2_rgrp_list rlist
;
647 unsigned int rg_blocks
= 0;
649 unsigned int revokes
= 0;
656 if (height
!= sm
->sm_height
)
664 metadata
= (height
!= ip
->i_height
- 1);
666 revokes
= (height
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
668 error
= gfs2_rindex_hold(sdp
, &ip
->i_alloc
->al_ri_gh
);
672 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
676 for (p
= top
; p
< bottom
; p
++) {
680 bn
= be64_to_cpu(*p
);
682 if (bstart
+ blen
== bn
)
686 gfs2_rlist_add(sdp
, &rlist
, bstart
);
694 gfs2_rlist_add(sdp
, &rlist
, bstart
);
696 goto out
; /* Nothing to do */
698 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
);
700 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
701 struct gfs2_rgrpd
*rgd
;
702 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
703 rg_blocks
+= rgd
->rd_length
;
706 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
710 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+
711 RES_INDIRECT
+ RES_STATFS
+ RES_QUOTA
,
716 down_write(&ip
->i_rw_mutex
);
718 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
719 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
724 for (p
= top
; p
< bottom
; p
++) {
728 bn
= be64_to_cpu(*p
);
730 if (bstart
+ blen
== bn
)
735 gfs2_free_meta(ip
, bstart
, blen
);
737 gfs2_free_data(ip
, bstart
, blen
);
745 if (!ip
->i_di
.di_blocks
)
746 gfs2_consist_inode(ip
);
747 ip
->i_di
.di_blocks
--;
748 gfs2_set_inode_blocks(&ip
->i_inode
);
752 gfs2_free_meta(ip
, bstart
, blen
);
754 gfs2_free_data(ip
, bstart
, blen
);
757 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
759 gfs2_dinode_out(ip
, dibh
->b_data
);
761 up_write(&ip
->i_rw_mutex
);
766 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
768 gfs2_rlist_free(&rlist
);
770 gfs2_glock_dq_uninit(&ip
->i_alloc
->al_ri_gh
);
775 * do_grow - Make a file look bigger than it is
777 * @size: the size to set the file to
779 * Called with an exclusive lock on @ip.
784 static int do_grow(struct gfs2_inode
*ip
, u64 size
)
786 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
787 struct gfs2_alloc
*al
;
788 struct buffer_head
*dibh
;
791 al
= gfs2_alloc_get(ip
);
793 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
797 error
= gfs2_quota_check(ip
, ip
->i_inode
.i_uid
, ip
->i_inode
.i_gid
);
801 al
->al_requested
= sdp
->sd_max_height
+ RES_DATA
;
803 error
= gfs2_inplace_reserve(ip
);
807 error
= gfs2_trans_begin(sdp
,
808 sdp
->sd_max_height
+ al
->al_rgd
->rd_length
+
809 RES_JDATA
+ RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
813 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
)) {
814 const u64
*arr
= sdp
->sd_heightsize
;
815 if (gfs2_is_stuffed(ip
)) {
816 error
= gfs2_unstuff_dinode(ip
, NULL
);
821 down_write(&ip
->i_rw_mutex
);
822 if (size
> arr
[ip
->i_height
]) {
824 u8 height
= ip
->i_height
;
825 while(size
> arr
[height
])
827 error
= build_height(&ip
->i_inode
, &mp
, height
);
829 up_write(&ip
->i_rw_mutex
);
834 ip
->i_di
.di_size
= size
;
835 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
837 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
841 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
842 gfs2_dinode_out(ip
, dibh
->b_data
);
848 gfs2_inplace_release(ip
);
850 gfs2_quota_unlock(ip
);
858 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
860 * This is partly borrowed from ext3.
862 static int gfs2_block_truncate_page(struct address_space
*mapping
)
864 struct inode
*inode
= mapping
->host
;
865 struct gfs2_inode
*ip
= GFS2_I(inode
);
866 loff_t from
= inode
->i_size
;
867 unsigned long index
= from
>> PAGE_CACHE_SHIFT
;
868 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
869 unsigned blocksize
, iblock
, length
, pos
;
870 struct buffer_head
*bh
;
874 page
= grab_cache_page(mapping
, index
);
878 blocksize
= inode
->i_sb
->s_blocksize
;
879 length
= blocksize
- (offset
& (blocksize
- 1));
880 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
882 if (!page_has_buffers(page
))
883 create_empty_buffers(page
, blocksize
, 0);
885 /* Find the buffer that contains "offset" */
886 bh
= page_buffers(page
);
888 while (offset
>= pos
) {
889 bh
= bh
->b_this_page
;
896 if (!buffer_mapped(bh
)) {
897 gfs2_block_map(inode
, iblock
, bh
, 0);
898 /* unmapped? It's a hole - nothing to do */
899 if (!buffer_mapped(bh
))
903 /* Ok, it's mapped. Make sure it's up-to-date */
904 if (PageUptodate(page
))
905 set_buffer_uptodate(bh
);
907 if (!buffer_uptodate(bh
)) {
909 ll_rw_block(READ
, 1, &bh
);
911 /* Uhhuh. Read error. Complain and punt. */
912 if (!buffer_uptodate(bh
))
917 if (!gfs2_is_writeback(ip
))
918 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
920 zero_user(page
, offset
, length
);
924 page_cache_release(page
);
928 static int trunc_start(struct gfs2_inode
*ip
, u64 size
)
930 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
931 struct buffer_head
*dibh
;
932 int journaled
= gfs2_is_jdata(ip
);
935 error
= gfs2_trans_begin(sdp
,
936 RES_DINODE
+ (journaled
? RES_JDATA
: 0), 0);
940 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
944 if (gfs2_is_stuffed(ip
)) {
945 ip
->i_di
.di_size
= size
;
946 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
947 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
948 gfs2_dinode_out(ip
, dibh
->b_data
);
949 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
) + size
);
953 if (size
& (u64
)(sdp
->sd_sb
.sb_bsize
- 1))
954 error
= gfs2_block_truncate_page(ip
->i_inode
.i_mapping
);
957 ip
->i_di
.di_size
= size
;
958 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
959 ip
->i_di
.di_flags
|= GFS2_DIF_TRUNC_IN_PROG
;
960 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
961 gfs2_dinode_out(ip
, dibh
->b_data
);
972 static int trunc_dealloc(struct gfs2_inode
*ip
, u64 size
)
974 unsigned int height
= ip
->i_height
;
982 lblock
= (size
- 1) >> GFS2_SB(&ip
->i_inode
)->sd_sb
.sb_bsize_shift
;
984 find_metapath(ip
, lblock
, &mp
);
987 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
992 struct strip_mine sm
;
993 sm
.sm_first
= !!size
;
994 sm
.sm_height
= height
;
996 error
= recursive_scan(ip
, NULL
, &mp
, 0, 0, 1, do_strip
, &sm
);
1001 gfs2_quota_unhold(ip
);
1008 static int trunc_end(struct gfs2_inode
*ip
)
1010 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1011 struct buffer_head
*dibh
;
1014 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
1018 down_write(&ip
->i_rw_mutex
);
1020 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1024 if (!ip
->i_di
.di_size
) {
1026 ip
->i_goal
= ip
->i_no_addr
;
1027 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
1029 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1030 ip
->i_di
.di_flags
&= ~GFS2_DIF_TRUNC_IN_PROG
;
1032 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1033 gfs2_dinode_out(ip
, dibh
->b_data
);
1037 up_write(&ip
->i_rw_mutex
);
1038 gfs2_trans_end(sdp
);
1043 * do_shrink - make a file smaller
1045 * @size: the size to make the file
1046 * @truncator: function to truncate the last partial block
1048 * Called with an exclusive lock on @ip.
1053 static int do_shrink(struct gfs2_inode
*ip
, u64 size
)
1057 error
= trunc_start(ip
, size
);
1063 error
= trunc_dealloc(ip
, size
);
1065 error
= trunc_end(ip
);
1070 static int do_touch(struct gfs2_inode
*ip
, u64 size
)
1072 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1073 struct buffer_head
*dibh
;
1076 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
1080 down_write(&ip
->i_rw_mutex
);
1082 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1086 ip
->i_inode
.i_mtime
= ip
->i_inode
.i_ctime
= CURRENT_TIME
;
1087 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1088 gfs2_dinode_out(ip
, dibh
->b_data
);
1092 up_write(&ip
->i_rw_mutex
);
1093 gfs2_trans_end(sdp
);
1098 * gfs2_truncatei - make a file a given size
1100 * @size: the size to make the file
1101 * @truncator: function to truncate the last partial block
1103 * The file size can grow, shrink, or stay the same size.
1108 int gfs2_truncatei(struct gfs2_inode
*ip
, u64 size
)
1112 if (gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), S_ISREG(ip
->i_inode
.i_mode
)))
1115 if (size
> ip
->i_di
.di_size
)
1116 error
= do_grow(ip
, size
);
1117 else if (size
< ip
->i_di
.di_size
)
1118 error
= do_shrink(ip
, size
);
1120 /* update time stamps */
1121 error
= do_touch(ip
, size
);
1126 int gfs2_truncatei_resume(struct gfs2_inode
*ip
)
1129 error
= trunc_dealloc(ip
, ip
->i_di
.di_size
);
1131 error
= trunc_end(ip
);
1135 int gfs2_file_dealloc(struct gfs2_inode
*ip
)
1137 return trunc_dealloc(ip
, 0);
1141 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1143 * @len: the number of bytes to be written to the file
1144 * @data_blocks: returns the number of data blocks required
1145 * @ind_blocks: returns the number of indirect blocks required
1149 void gfs2_write_calc_reserv(struct gfs2_inode
*ip
, unsigned int len
,
1150 unsigned int *data_blocks
, unsigned int *ind_blocks
)
1152 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1155 if (gfs2_is_dir(ip
)) {
1156 *data_blocks
= DIV_ROUND_UP(len
, sdp
->sd_jbsize
) + 2;
1157 *ind_blocks
= 3 * (sdp
->sd_max_jheight
- 1);
1159 *data_blocks
= (len
>> sdp
->sd_sb
.sb_bsize_shift
) + 3;
1160 *ind_blocks
= 3 * (sdp
->sd_max_height
- 1);
1163 for (tmp
= *data_blocks
; tmp
> sdp
->sd_diptrs
;) {
1164 tmp
= DIV_ROUND_UP(tmp
, sdp
->sd_inptrs
);
1170 * gfs2_write_alloc_required - figure out if a write will require an allocation
1171 * @ip: the file being written to
1172 * @offset: the offset to write to
1173 * @len: the number of bytes being written
1174 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1179 int gfs2_write_alloc_required(struct gfs2_inode
*ip
, u64 offset
,
1180 unsigned int len
, int *alloc_required
)
1182 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1183 struct buffer_head bh
;
1185 u64 lblock
, lblock_stop
, size
;
1187 *alloc_required
= 0;
1192 if (gfs2_is_stuffed(ip
)) {
1194 sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
1195 *alloc_required
= 1;
1199 *alloc_required
= 1;
1200 shift
= sdp
->sd_sb
.sb_bsize_shift
;
1201 if (gfs2_is_dir(ip
)) {
1202 unsigned int bsize
= sdp
->sd_jbsize
;
1204 do_div(lblock
, bsize
);
1205 lblock_stop
= offset
+ len
+ bsize
- 1;
1206 do_div(lblock_stop
, bsize
);
1208 u64 end_of_file
= (ip
->i_di
.di_size
+ sdp
->sd_sb
.sb_bsize
- 1) >> shift
;
1209 lblock
= offset
>> shift
;
1210 lblock_stop
= (offset
+ len
+ sdp
->sd_sb
.sb_bsize
- 1) >> shift
;
1211 if (lblock_stop
> end_of_file
)
1215 size
= (lblock_stop
- lblock
) << shift
;
1219 gfs2_block_map(&ip
->i_inode
, lblock
, &bh
, 0);
1220 if (!buffer_mapped(&bh
))
1223 lblock
+= (bh
.b_size
>> ip
->i_inode
.i_blkbits
);
1226 *alloc_required
= 0;