2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/crc32.h>
17 #include <asm/semaphore.h>
20 #include "lm_interface.h"
33 /* This doesn't need to be that large as max 64 bit pointers in a 4k
34 * block is 512, so __u16 is fine for that. It saves stack space to
38 __u16 mp_list
[GFS2_MAX_META_HEIGHT
];
41 typedef int (*block_call_t
) (struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
42 struct buffer_head
*bh
, uint64_t *top
,
43 uint64_t *bottom
, unsigned int height
,
48 unsigned int sm_height
;
52 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
53 * @ip: The GFS2 inode to unstuff
54 * @unstuffer: the routine that handles unstuffing a non-zero length file
55 * @private: private data for the unstuffer
57 * This routine unstuffs a dinode and returns it to a "normal" state such
58 * that the height can be grown in the traditional way.
63 int gfs2_unstuff_dinode(struct gfs2_inode
*ip
, gfs2_unstuffer_t unstuffer
,
66 struct buffer_head
*bh
, *dibh
;
68 int isdir
= gfs2_is_dir(ip
);
71 down_write(&ip
->i_rw_mutex
);
73 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
77 if (ip
->i_di
.di_size
) {
78 /* Get a free block, fill it with the stuffed data,
79 and write it out to disk */
82 block
= gfs2_alloc_meta(ip
);
84 error
= gfs2_dir_get_new_buffer(ip
, block
, &bh
);
87 gfs2_buffer_copy_tail(bh
,
88 sizeof(struct gfs2_meta_header
),
89 dibh
, sizeof(struct gfs2_dinode
));
92 block
= gfs2_alloc_data(ip
);
94 error
= unstuffer(ip
, dibh
, block
, private);
100 /* Set up the pointer to the new block */
102 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
104 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
106 if (ip
->i_di
.di_size
) {
107 *(uint64_t *)(dibh
->b_data
+ sizeof(struct gfs2_dinode
)) =
109 ip
->i_di
.di_blocks
++;
112 ip
->i_di
.di_height
= 1;
114 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
120 up_write(&ip
->i_rw_mutex
);
126 * calc_tree_height - Calculate the height of a metadata tree
127 * @ip: The GFS2 inode
128 * @size: The proposed size of the file
130 * Work out how tall a metadata tree needs to be in order to accommodate a
131 * file of a particular size. If size is less than the current size of
132 * the inode, then the current size of the inode is used instead of the
135 * Returns: the height the tree should be
138 static unsigned int calc_tree_height(struct gfs2_inode
*ip
, uint64_t size
)
140 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
142 unsigned int max
, height
;
144 if (ip
->i_di
.di_size
> size
)
145 size
= ip
->i_di
.di_size
;
147 if (gfs2_is_dir(ip
)) {
148 arr
= sdp
->sd_jheightsize
;
149 max
= sdp
->sd_max_jheight
;
151 arr
= sdp
->sd_heightsize
;
152 max
= sdp
->sd_max_height
;
155 for (height
= 0; height
< max
; height
++)
156 if (arr
[height
] >= size
)
163 * build_height - Build a metadata tree of the requested height
164 * @ip: The GFS2 inode
165 * @height: The height to build to
171 static int build_height(struct inode
*inode
, unsigned height
)
173 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
174 unsigned new_height
= height
- ip
->i_di
.di_height
;
175 struct buffer_head
*dibh
;
176 struct buffer_head
*blocks
[GFS2_MAX_META_HEIGHT
];
182 if (height
<= ip
->i_di
.di_height
)
185 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
189 for(n
= 0; n
< new_height
; n
++) {
190 bn
= gfs2_alloc_meta(ip
);
191 blocks
[n
] = gfs2_meta_new(ip
->i_gl
, bn
);
192 gfs2_trans_add_bh(ip
->i_gl
, blocks
[n
], 1);
196 bn
= blocks
[0]->b_blocknr
;
197 if (new_height
> 1) {
198 for(; n
< new_height
-1; n
++) {
199 gfs2_metatype_set(blocks
[n
], GFS2_METATYPE_IN
,
201 gfs2_buffer_clear_tail(blocks
[n
],
202 sizeof(struct gfs2_meta_header
));
203 bp
= (u64
*)(blocks
[n
]->b_data
+
204 sizeof(struct gfs2_meta_header
));
205 *bp
= cpu_to_be64(blocks
[n
+1]->b_blocknr
);
210 gfs2_metatype_set(blocks
[n
], GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
211 gfs2_buffer_copy_tail(blocks
[n
], sizeof(struct gfs2_meta_header
),
212 dibh
, sizeof(struct gfs2_dinode
));
214 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
215 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
216 bp
= (u64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
217 *bp
= cpu_to_be64(bn
);
218 ip
->i_di
.di_height
+= new_height
;
219 ip
->i_di
.di_blocks
+= new_height
;
220 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
226 * find_metapath - Find path through the metadata tree
227 * @ip: The inode pointer
228 * @mp: The metapath to return the result in
229 * @block: The disk block to look up
231 * This routine returns a struct metapath structure that defines a path
232 * through the metadata of inode "ip" to get to block "block".
235 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
236 * filesystem with a blocksize of 4096.
238 * find_metapath() would return a struct metapath structure set to:
239 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
240 * and mp_list[2] = 165.
242 * That means that in order to get to the block containing the byte at
243 * offset 101342453, we would load the indirect block pointed to by pointer
244 * 0 in the dinode. We would then load the indirect block pointed to by
245 * pointer 48 in that indirect block. We would then load the data block
246 * pointed to by pointer 165 in that indirect block.
248 * ----------------------------------------
253 * ----------------------------------------
257 * ----------------------------------------
261 * |0 5 6 7 8 9 0 1 2|
262 * ----------------------------------------
266 * ----------------------------------------
271 * ----------------------------------------
275 * ----------------------------------------
276 * | Data block containing offset |
280 * ----------------------------------------
284 static void find_metapath(struct gfs2_inode
*ip
, uint64_t block
,
287 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
291 for (i
= ip
->i_di
.di_height
; i
--;)
292 mp
->mp_list
[i
] = (__u16
)do_div(b
, sdp
->sd_inptrs
);
297 * metapointer - Return pointer to start of metadata in a buffer
299 * @height: The metadata height (0 = dinode)
302 * Return a pointer to the block number of the next height of the metadata
303 * tree given a buffer containing the pointer to the current height of the
307 static inline u64
*metapointer(struct buffer_head
*bh
, int *boundary
,
308 unsigned int height
, const struct metapath
*mp
)
310 unsigned int head_size
= (height
> 0) ?
311 sizeof(struct gfs2_meta_header
) : sizeof(struct gfs2_dinode
);
314 ptr
= ((u64
*)(bh
->b_data
+ head_size
)) + mp
->mp_list
[height
];
315 if (ptr
+ 1 == (u64
*)(bh
->b_data
+ bh
->b_size
))
321 * lookup_block - Get the next metadata block in metadata tree
322 * @ip: The GFS2 inode
323 * @bh: Buffer containing the pointers to metadata blocks
324 * @height: The height of the tree (0 = dinode)
326 * @create: Non-zero if we may create a new meatdata block
327 * @new: Used to indicate if we did create a new metadata block
328 * @block: the returned disk block number
330 * Given a metatree, complete to a particular height, checks to see if the next
331 * height of the tree exists. If not the next height of the tree is created.
332 * The block number of the next height of the metadata tree is returned.
336 static int lookup_block(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
337 unsigned int height
, struct metapath
*mp
, int create
,
338 int *new, uint64_t *block
)
341 uint64_t *ptr
= metapointer(bh
, &boundary
, height
, mp
);
344 *block
= be64_to_cpu(*ptr
);
353 if (height
== ip
->i_di
.di_height
- 1 && !gfs2_is_dir(ip
))
354 *block
= gfs2_alloc_data(ip
);
356 *block
= gfs2_alloc_meta(ip
);
358 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
360 *ptr
= cpu_to_be64(*block
);
361 ip
->i_di
.di_blocks
++;
368 * gfs2_block_pointers - Map a block from an inode to a disk block
370 * @lblock: The logical block number
371 * @new: Value/Result argument (1 = may create/did create new blocks)
372 * @boundary: gets set if we've hit a block boundary
373 * @mp: metapath to use
375 * Find the block number on the current device which corresponds to an
376 * inode's block. If the block had to be created, "new" will be set.
381 static struct buffer_head
*gfs2_block_pointers(struct inode
*inode
, u64 lblock
,
382 int *new, u64
*dblock
,
386 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
387 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
388 struct buffer_head
*bh
;
392 unsigned int end_of_metadata
;
399 if (gfs2_assert_warn(sdp
, !gfs2_is_stuffed(ip
)))
402 bsize
= (gfs2_is_dir(ip
)) ? sdp
->sd_jbsize
: sdp
->sd_sb
.sb_bsize
;
404 height
= calc_tree_height(ip
, (lblock
+ 1) * bsize
);
405 if (ip
->i_di
.di_height
< height
) {
409 error
= build_height(inode
, height
);
414 find_metapath(ip
, lblock
, mp
);
415 end_of_metadata
= ip
->i_di
.di_height
- 1;
417 error
= gfs2_meta_inode_buffer(ip
, &bh
);
421 for (x
= 0; x
< end_of_metadata
; x
++) {
422 lookup_block(ip
, bh
, x
, mp
, create
, new, dblock
);
427 error
= gfs2_meta_indirect_buffer(ip
, x
+1, *dblock
, *new, &bh
);
432 *boundary
= lookup_block(ip
, bh
, end_of_metadata
, mp
, create
, new, dblock
);
434 struct buffer_head
*dibh
;
435 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
437 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
438 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
444 return ERR_PTR(error
);
448 static inline void bmap_lock(struct inode
*inode
, int create
)
450 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
452 down_write(&ip
->i_rw_mutex
);
454 down_read(&ip
->i_rw_mutex
);
457 static inline void bmap_unlock(struct inode
*inode
, int create
)
459 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
461 up_write(&ip
->i_rw_mutex
);
463 up_read(&ip
->i_rw_mutex
);
466 int gfs2_block_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, int *boundary
)
469 struct buffer_head
*bh
;
472 bmap_lock(inode
, create
);
473 bh
= gfs2_block_pointers(inode
, lblock
, new, dblock
, boundary
, &mp
);
474 bmap_unlock(inode
, create
);
483 int gfs2_extent_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, unsigned *extlen
)
485 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
486 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
488 struct buffer_head
*bh
;
496 bmap_lock(inode
, create
);
497 bh
= gfs2_block_pointers(inode
, lblock
, new, dblock
, &boundary
, &mp
);
500 if (bh
&& !IS_ERR(bh
) && *dblock
&& !*new) {
504 unsigned end_of_metadata
= ip
->i_di
.di_height
- 1;
506 nptrs
= (end_of_metadata
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
507 while (++mp
.mp_list
[end_of_metadata
] < nptrs
) {
508 lookup_block(ip
, bh
, end_of_metadata
, &mp
, 0, &tmp_new
, &tmp_dblock
);
509 if (*dblock
+ *extlen
!= tmp_dblock
)
514 bmap_unlock(inode
, create
);
524 * recursive_scan - recursively scan through the end of a file
526 * @dibh: the dinode buffer
527 * @mp: the path through the metadata to the point to start
528 * @height: the height the recursion is at
529 * @block: the indirect block to look at
530 * @first: 1 if this is the first block
531 * @bc: the call to make for each piece of metadata
532 * @data: data opaque to this function to pass to @bc
534 * When this is first called @height and @block should be zero and
535 * @first should be 1.
540 static int recursive_scan(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
541 struct metapath
*mp
, unsigned int height
,
542 uint64_t block
, int first
, block_call_t bc
,
545 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
546 struct buffer_head
*bh
= NULL
;
547 uint64_t *top
, *bottom
;
550 int mh_size
= sizeof(struct gfs2_meta_header
);
553 error
= gfs2_meta_inode_buffer(ip
, &bh
);
558 top
= (uint64_t *)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) +
560 bottom
= (uint64_t *)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) +
563 error
= gfs2_meta_indirect_buffer(ip
, height
, block
, 0, &bh
);
567 top
= (uint64_t *)(bh
->b_data
+ mh_size
) +
568 ((first
) ? mp
->mp_list
[height
] : 0);
570 bottom
= (uint64_t *)(bh
->b_data
+ mh_size
) + sdp
->sd_inptrs
;
573 error
= bc(ip
, dibh
, bh
, top
, bottom
, height
, data
);
577 if (height
< ip
->i_di
.di_height
- 1)
578 for (; top
< bottom
; top
++, first
= 0) {
582 bn
= be64_to_cpu(*top
);
584 error
= recursive_scan(ip
, dibh
, mp
, height
+ 1, bn
,
597 * do_strip - Look for a layer a particular layer of the file and strip it off
599 * @dibh: the dinode buffer
600 * @bh: A buffer of pointers
601 * @top: The first pointer in the buffer
602 * @bottom: One more than the last pointer
603 * @height: the height this buffer is at
604 * @data: a pointer to a struct strip_mine
609 static int do_strip(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
610 struct buffer_head
*bh
, uint64_t *top
, uint64_t *bottom
,
611 unsigned int height
, void *data
)
613 struct strip_mine
*sm
= (struct strip_mine
*)data
;
614 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
615 struct gfs2_rgrp_list rlist
;
619 unsigned int rg_blocks
= 0;
621 unsigned int revokes
= 0;
628 if (height
!= sm
->sm_height
)
636 metadata
= (height
!= ip
->i_di
.di_height
- 1);
638 revokes
= (height
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
640 error
= gfs2_rindex_hold(sdp
, &ip
->i_alloc
.al_ri_gh
);
644 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
648 for (p
= top
; p
< bottom
; p
++) {
652 bn
= be64_to_cpu(*p
);
654 if (bstart
+ blen
== bn
)
658 gfs2_rlist_add(sdp
, &rlist
, bstart
);
666 gfs2_rlist_add(sdp
, &rlist
, bstart
);
668 goto out
; /* Nothing to do */
670 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
, 0);
672 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
673 struct gfs2_rgrpd
*rgd
;
674 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
675 rg_blocks
+= rgd
->rd_ri
.ri_length
;
678 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
682 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+
683 RES_INDIRECT
+ RES_STATFS
+ RES_QUOTA
,
688 down_write(&ip
->i_rw_mutex
);
690 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
691 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
696 for (p
= top
; p
< bottom
; p
++) {
700 bn
= be64_to_cpu(*p
);
702 if (bstart
+ blen
== bn
)
707 gfs2_free_meta(ip
, bstart
, blen
);
709 gfs2_free_data(ip
, bstart
, blen
);
717 if (!ip
->i_di
.di_blocks
)
718 gfs2_consist_inode(ip
);
719 ip
->i_di
.di_blocks
--;
723 gfs2_free_meta(ip
, bstart
, blen
);
725 gfs2_free_data(ip
, bstart
, blen
);
728 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
730 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
732 up_write(&ip
->i_rw_mutex
);
737 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
740 gfs2_rlist_free(&rlist
);
743 gfs2_glock_dq_uninit(&ip
->i_alloc
.al_ri_gh
);
749 * do_grow - Make a file look bigger than it is
751 * @size: the size to set the file to
753 * Called with an exclusive lock on @ip.
758 static int do_grow(struct gfs2_inode
*ip
, uint64_t size
)
760 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
761 struct gfs2_alloc
*al
;
762 struct buffer_head
*dibh
;
766 al
= gfs2_alloc_get(ip
);
768 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
772 error
= gfs2_quota_check(ip
, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
776 al
->al_requested
= sdp
->sd_max_height
+ RES_DATA
;
778 error
= gfs2_inplace_reserve(ip
);
782 error
= gfs2_trans_begin(sdp
,
783 sdp
->sd_max_height
+ al
->al_rgd
->rd_ri
.ri_length
+
784 RES_JDATA
+ RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
788 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
)) {
789 if (gfs2_is_stuffed(ip
)) {
790 error
= gfs2_unstuff_dinode(ip
, gfs2_unstuffer_page
,
796 h
= calc_tree_height(ip
, size
);
797 if (ip
->i_di
.di_height
< h
) {
798 down_write(&ip
->i_rw_mutex
);
799 error
= build_height(ip
->i_vnode
, h
);
800 up_write(&ip
->i_rw_mutex
);
806 ip
->i_di
.di_size
= size
;
807 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
809 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
813 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
814 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
821 gfs2_inplace_release(ip
);
824 gfs2_quota_unlock(ip
);
832 static int trunc_start(struct gfs2_inode
*ip
, uint64_t size
)
834 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
835 struct buffer_head
*dibh
;
836 int journaled
= gfs2_is_jdata(ip
);
839 error
= gfs2_trans_begin(sdp
,
840 RES_DINODE
+ ((journaled
) ? RES_JDATA
: 0), 0);
844 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
848 if (gfs2_is_stuffed(ip
)) {
849 ip
->i_di
.di_size
= size
;
850 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
851 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
852 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
853 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
) + size
);
857 if (size
& (uint64_t)(sdp
->sd_sb
.sb_bsize
- 1))
858 error
= gfs2_block_truncate_page(ip
->i_vnode
->i_mapping
);
861 ip
->i_di
.di_size
= size
;
862 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
863 ip
->i_di
.di_flags
|= GFS2_DIF_TRUNC_IN_PROG
;
864 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
865 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
877 static int trunc_dealloc(struct gfs2_inode
*ip
, uint64_t size
)
879 unsigned int height
= ip
->i_di
.di_height
;
887 lblock
= (size
- 1) >> ip
->i_sbd
->sd_sb
.sb_bsize_shift
;
889 find_metapath(ip
, lblock
, &mp
);
892 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
897 struct strip_mine sm
;
898 sm
.sm_first
= !!size
;
899 sm
.sm_height
= height
;
901 error
= recursive_scan(ip
, NULL
, &mp
, 0, 0, 1, do_strip
, &sm
);
906 gfs2_quota_unhold(ip
);
913 static int trunc_end(struct gfs2_inode
*ip
)
915 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
916 struct buffer_head
*dibh
;
919 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
923 down_write(&ip
->i_rw_mutex
);
925 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
929 if (!ip
->i_di
.di_size
) {
930 ip
->i_di
.di_height
= 0;
931 ip
->i_di
.di_goal_meta
=
932 ip
->i_di
.di_goal_data
=
934 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
936 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
937 ip
->i_di
.di_flags
&= ~GFS2_DIF_TRUNC_IN_PROG
;
939 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
940 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
944 up_write(&ip
->i_rw_mutex
);
952 * do_shrink - make a file smaller
954 * @size: the size to make the file
955 * @truncator: function to truncate the last partial block
957 * Called with an exclusive lock on @ip.
962 static int do_shrink(struct gfs2_inode
*ip
, uint64_t size
)
966 error
= trunc_start(ip
, size
);
972 error
= trunc_dealloc(ip
, size
);
974 error
= trunc_end(ip
);
980 * gfs2_truncatei - make a file a given size
982 * @size: the size to make the file
983 * @truncator: function to truncate the last partial block
985 * The file size can grow, shrink, or stay the same size.
990 int gfs2_truncatei(struct gfs2_inode
*ip
, uint64_t size
)
994 if (gfs2_assert_warn(ip
->i_sbd
, S_ISREG(ip
->i_di
.di_mode
)))
997 if (size
> ip
->i_di
.di_size
)
998 error
= do_grow(ip
, size
);
1000 error
= do_shrink(ip
, size
);
1005 int gfs2_truncatei_resume(struct gfs2_inode
*ip
)
1008 error
= trunc_dealloc(ip
, ip
->i_di
.di_size
);
1010 error
= trunc_end(ip
);
1014 int gfs2_file_dealloc(struct gfs2_inode
*ip
)
1016 return trunc_dealloc(ip
, 0);
1020 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1022 * @len: the number of bytes to be written to the file
1023 * @data_blocks: returns the number of data blocks required
1024 * @ind_blocks: returns the number of indirect blocks required
1028 void gfs2_write_calc_reserv(struct gfs2_inode
*ip
, unsigned int len
,
1029 unsigned int *data_blocks
, unsigned int *ind_blocks
)
1031 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1034 if (gfs2_is_dir(ip
)) {
1035 *data_blocks
= DIV_ROUND_UP(len
, sdp
->sd_jbsize
) + 2;
1036 *ind_blocks
= 3 * (sdp
->sd_max_jheight
- 1);
1038 *data_blocks
= (len
>> sdp
->sd_sb
.sb_bsize_shift
) + 3;
1039 *ind_blocks
= 3 * (sdp
->sd_max_height
- 1);
1042 for (tmp
= *data_blocks
; tmp
> sdp
->sd_diptrs
;) {
1043 tmp
= DIV_ROUND_UP(tmp
, sdp
->sd_inptrs
);
1049 * gfs2_write_alloc_required - figure out if a write will require an allocation
1050 * @ip: the file being written to
1051 * @offset: the offset to write to
1052 * @len: the number of bytes being written
1053 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1058 int gfs2_write_alloc_required(struct gfs2_inode
*ip
, uint64_t offset
,
1059 unsigned int len
, int *alloc_required
)
1061 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1062 uint64_t lblock
, lblock_stop
, dblock
;
1067 *alloc_required
= 0;
1072 if (gfs2_is_stuffed(ip
)) {
1074 sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
1075 *alloc_required
= 1;
1079 if (gfs2_is_dir(ip
)) {
1080 unsigned int bsize
= sdp
->sd_jbsize
;
1082 do_div(lblock
, bsize
);
1083 lblock_stop
= offset
+ len
+ bsize
- 1;
1084 do_div(lblock_stop
, bsize
);
1086 unsigned int shift
= sdp
->sd_sb
.sb_bsize_shift
;
1087 lblock
= offset
>> shift
;
1088 lblock_stop
= (offset
+ len
+ sdp
->sd_sb
.sb_bsize
- 1) >> shift
;
1091 for (; lblock
< lblock_stop
; lblock
+= extlen
) {
1092 error
= gfs2_extent_map(ip
->i_vnode
, lblock
, &new, &dblock
, &extlen
);
1097 *alloc_required
= 1;