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 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>
19 #include "lm_interface.h"
32 /* This doesn't need to be that large as max 64 bit pointers in a 4k
33 * block is 512, so __u16 is fine for that. It saves stack space to
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
, uint64_t *top
,
42 uint64_t *bottom
, unsigned int height
,
47 unsigned int sm_height
;
51 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
52 * @ip: The GFS2 inode to unstuff
53 * @unstuffer: the routine that handles unstuffing a non-zero length file
54 * @private: private data for the unstuffer
56 * This routine unstuffs a dinode and returns it to a "normal" state such
57 * that the height can be grown in the traditional way.
62 int gfs2_unstuff_dinode(struct gfs2_inode
*ip
, gfs2_unstuffer_t unstuffer
,
65 struct buffer_head
*bh
, *dibh
;
67 int isdir
= gfs2_is_dir(ip
);
70 down_write(&ip
->i_rw_mutex
);
72 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
76 if (ip
->i_di
.di_size
) {
77 /* Get a free block, fill it with the stuffed data,
78 and write it out to disk */
81 block
= gfs2_alloc_meta(ip
);
83 error
= gfs2_dir_get_new_buffer(ip
, block
, &bh
);
86 gfs2_buffer_copy_tail(bh
,
87 sizeof(struct gfs2_meta_header
),
88 dibh
, sizeof(struct gfs2_dinode
));
91 block
= gfs2_alloc_data(ip
);
93 error
= unstuffer(ip
, dibh
, block
, private);
99 /* Set up the pointer to the new block */
101 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
103 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
105 if (ip
->i_di
.di_size
) {
106 *(uint64_t *)(dibh
->b_data
+ sizeof(struct gfs2_dinode
)) =
108 ip
->i_di
.di_blocks
++;
111 ip
->i_di
.di_height
= 1;
113 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
119 up_write(&ip
->i_rw_mutex
);
125 * calc_tree_height - Calculate the height of a metadata tree
126 * @ip: The GFS2 inode
127 * @size: The proposed size of the file
129 * Work out how tall a metadata tree needs to be in order to accommodate a
130 * file of a particular size. If size is less than the current size of
131 * the inode, then the current size of the inode is used instead of the
134 * Returns: the height the tree should be
137 static unsigned int calc_tree_height(struct gfs2_inode
*ip
, uint64_t size
)
139 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
141 unsigned int max
, height
;
143 if (ip
->i_di
.di_size
> size
)
144 size
= ip
->i_di
.di_size
;
146 if (gfs2_is_dir(ip
)) {
147 arr
= sdp
->sd_jheightsize
;
148 max
= sdp
->sd_max_jheight
;
150 arr
= sdp
->sd_heightsize
;
151 max
= sdp
->sd_max_height
;
154 for (height
= 0; height
< max
; height
++)
155 if (arr
[height
] >= size
)
162 * build_height - Build a metadata tree of the requested height
163 * @ip: The GFS2 inode
164 * @height: The height to build to
170 static int build_height(struct inode
*inode
, unsigned height
)
172 struct gfs2_inode
*ip
= GFS2_I(inode
);
173 unsigned new_height
= height
- ip
->i_di
.di_height
;
174 struct buffer_head
*dibh
;
175 struct buffer_head
*blocks
[GFS2_MAX_META_HEIGHT
];
181 if (height
<= ip
->i_di
.di_height
)
184 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
188 for(n
= 0; n
< new_height
; n
++) {
189 bn
= gfs2_alloc_meta(ip
);
190 blocks
[n
] = gfs2_meta_new(ip
->i_gl
, bn
);
191 gfs2_trans_add_bh(ip
->i_gl
, blocks
[n
], 1);
195 bn
= blocks
[0]->b_blocknr
;
196 if (new_height
> 1) {
197 for(; n
< new_height
-1; n
++) {
198 gfs2_metatype_set(blocks
[n
], GFS2_METATYPE_IN
,
200 gfs2_buffer_clear_tail(blocks
[n
],
201 sizeof(struct gfs2_meta_header
));
202 bp
= (u64
*)(blocks
[n
]->b_data
+
203 sizeof(struct gfs2_meta_header
));
204 *bp
= cpu_to_be64(blocks
[n
+1]->b_blocknr
);
209 gfs2_metatype_set(blocks
[n
], GFS2_METATYPE_IN
, GFS2_FORMAT_IN
);
210 gfs2_buffer_copy_tail(blocks
[n
], sizeof(struct gfs2_meta_header
),
211 dibh
, sizeof(struct gfs2_dinode
));
213 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
214 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
215 bp
= (u64
*)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
216 *bp
= cpu_to_be64(bn
);
217 ip
->i_di
.di_height
+= new_height
;
218 ip
->i_di
.di_blocks
+= new_height
;
219 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
225 * find_metapath - Find path through the metadata tree
226 * @ip: The inode pointer
227 * @mp: The metapath to return the result in
228 * @block: The disk block to look up
230 * This routine returns a struct metapath structure that defines a path
231 * through the metadata of inode "ip" to get to block "block".
234 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
235 * filesystem with a blocksize of 4096.
237 * find_metapath() would return a struct metapath structure set to:
238 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
239 * and mp_list[2] = 165.
241 * That means that in order to get to the block containing the byte at
242 * offset 101342453, we would load the indirect block pointed to by pointer
243 * 0 in the dinode. We would then load the indirect block pointed to by
244 * pointer 48 in that indirect block. We would then load the data block
245 * pointed to by pointer 165 in that indirect block.
247 * ----------------------------------------
252 * ----------------------------------------
256 * ----------------------------------------
260 * |0 5 6 7 8 9 0 1 2|
261 * ----------------------------------------
265 * ----------------------------------------
270 * ----------------------------------------
274 * ----------------------------------------
275 * | Data block containing offset |
279 * ----------------------------------------
283 static void find_metapath(struct gfs2_inode
*ip
, uint64_t block
,
286 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
290 for (i
= ip
->i_di
.di_height
; i
--;)
291 mp
->mp_list
[i
] = (__u16
)do_div(b
, sdp
->sd_inptrs
);
296 * metapointer - Return pointer to start of metadata in a buffer
298 * @height: The metadata height (0 = dinode)
301 * Return a pointer to the block number of the next height of the metadata
302 * tree given a buffer containing the pointer to the current height of the
306 static inline u64
*metapointer(struct buffer_head
*bh
, int *boundary
,
307 unsigned int height
, const struct metapath
*mp
)
309 unsigned int head_size
= (height
> 0) ?
310 sizeof(struct gfs2_meta_header
) : sizeof(struct gfs2_dinode
);
313 ptr
= ((u64
*)(bh
->b_data
+ head_size
)) + mp
->mp_list
[height
];
314 if (ptr
+ 1 == (u64
*)(bh
->b_data
+ bh
->b_size
))
320 * lookup_block - Get the next metadata block in metadata tree
321 * @ip: The GFS2 inode
322 * @bh: Buffer containing the pointers to metadata blocks
323 * @height: The height of the tree (0 = dinode)
325 * @create: Non-zero if we may create a new meatdata block
326 * @new: Used to indicate if we did create a new metadata block
327 * @block: the returned disk block number
329 * Given a metatree, complete to a particular height, checks to see if the next
330 * height of the tree exists. If not the next height of the tree is created.
331 * The block number of the next height of the metadata tree is returned.
335 static int lookup_block(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
336 unsigned int height
, struct metapath
*mp
, int create
,
337 int *new, uint64_t *block
)
340 uint64_t *ptr
= metapointer(bh
, &boundary
, height
, mp
);
343 *block
= be64_to_cpu(*ptr
);
352 if (height
== ip
->i_di
.di_height
- 1 && !gfs2_is_dir(ip
))
353 *block
= gfs2_alloc_data(ip
);
355 *block
= gfs2_alloc_meta(ip
);
357 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
359 *ptr
= cpu_to_be64(*block
);
360 ip
->i_di
.di_blocks
++;
367 * gfs2_block_pointers - Map a block from an inode to a disk block
369 * @lblock: The logical block number
370 * @new: Value/Result argument (1 = may create/did create new blocks)
371 * @boundary: gets set if we've hit a block boundary
372 * @mp: metapath to use
374 * Find the block number on the current device which corresponds to an
375 * inode's block. If the block had to be created, "new" will be set.
380 static struct buffer_head
*gfs2_block_pointers(struct inode
*inode
, u64 lblock
,
381 int *new, u64
*dblock
,
385 struct gfs2_inode
*ip
= GFS2_I(inode
);
386 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
387 struct buffer_head
*bh
;
391 unsigned int end_of_metadata
;
398 if (gfs2_assert_warn(sdp
, !gfs2_is_stuffed(ip
)))
401 bsize
= (gfs2_is_dir(ip
)) ? sdp
->sd_jbsize
: sdp
->sd_sb
.sb_bsize
;
403 height
= calc_tree_height(ip
, (lblock
+ 1) * bsize
);
404 if (ip
->i_di
.di_height
< height
) {
408 error
= build_height(inode
, height
);
413 find_metapath(ip
, lblock
, mp
);
414 end_of_metadata
= ip
->i_di
.di_height
- 1;
416 error
= gfs2_meta_inode_buffer(ip
, &bh
);
420 for (x
= 0; x
< end_of_metadata
; x
++) {
421 lookup_block(ip
, bh
, x
, mp
, create
, new, dblock
);
426 error
= gfs2_meta_indirect_buffer(ip
, x
+1, *dblock
, *new, &bh
);
431 *boundary
= lookup_block(ip
, bh
, end_of_metadata
, mp
, create
, new, dblock
);
433 struct buffer_head
*dibh
;
434 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
436 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
437 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
443 return ERR_PTR(error
);
447 static inline void bmap_lock(struct inode
*inode
, int create
)
449 struct gfs2_inode
*ip
= GFS2_I(inode
);
451 down_write(&ip
->i_rw_mutex
);
453 down_read(&ip
->i_rw_mutex
);
456 static inline void bmap_unlock(struct inode
*inode
, int create
)
458 struct gfs2_inode
*ip
= GFS2_I(inode
);
460 up_write(&ip
->i_rw_mutex
);
462 up_read(&ip
->i_rw_mutex
);
465 int gfs2_block_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, int *boundary
)
468 struct buffer_head
*bh
;
471 bmap_lock(inode
, create
);
472 bh
= gfs2_block_pointers(inode
, lblock
, new, dblock
, boundary
, &mp
);
473 bmap_unlock(inode
, create
);
482 int gfs2_extent_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, unsigned *extlen
)
484 struct gfs2_inode
*ip
= GFS2_I(inode
);
485 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
487 struct buffer_head
*bh
;
495 bmap_lock(inode
, create
);
496 bh
= gfs2_block_pointers(inode
, lblock
, new, dblock
, &boundary
, &mp
);
499 if (bh
&& !IS_ERR(bh
) && *dblock
&& !*new) {
503 unsigned end_of_metadata
= ip
->i_di
.di_height
- 1;
505 nptrs
= (end_of_metadata
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
506 while (++mp
.mp_list
[end_of_metadata
] < nptrs
) {
507 lookup_block(ip
, bh
, end_of_metadata
, &mp
, 0, &tmp_new
, &tmp_dblock
);
508 if (*dblock
+ *extlen
!= tmp_dblock
)
513 bmap_unlock(inode
, create
);
523 * recursive_scan - recursively scan through the end of a file
525 * @dibh: the dinode buffer
526 * @mp: the path through the metadata to the point to start
527 * @height: the height the recursion is at
528 * @block: the indirect block to look at
529 * @first: 1 if this is the first block
530 * @bc: the call to make for each piece of metadata
531 * @data: data opaque to this function to pass to @bc
533 * When this is first called @height and @block should be zero and
534 * @first should be 1.
539 static int recursive_scan(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
540 struct metapath
*mp
, unsigned int height
,
541 uint64_t block
, int first
, block_call_t bc
,
544 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
545 struct buffer_head
*bh
= NULL
;
546 uint64_t *top
, *bottom
;
549 int mh_size
= sizeof(struct gfs2_meta_header
);
552 error
= gfs2_meta_inode_buffer(ip
, &bh
);
557 top
= (uint64_t *)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) +
559 bottom
= (uint64_t *)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) +
562 error
= gfs2_meta_indirect_buffer(ip
, height
, block
, 0, &bh
);
566 top
= (uint64_t *)(bh
->b_data
+ mh_size
) +
567 ((first
) ? mp
->mp_list
[height
] : 0);
569 bottom
= (uint64_t *)(bh
->b_data
+ mh_size
) + sdp
->sd_inptrs
;
572 error
= bc(ip
, dibh
, bh
, top
, bottom
, height
, data
);
576 if (height
< ip
->i_di
.di_height
- 1)
577 for (; top
< bottom
; top
++, first
= 0) {
581 bn
= be64_to_cpu(*top
);
583 error
= recursive_scan(ip
, dibh
, mp
, height
+ 1, bn
,
596 * do_strip - Look for a layer a particular layer of the file and strip it off
598 * @dibh: the dinode buffer
599 * @bh: A buffer of pointers
600 * @top: The first pointer in the buffer
601 * @bottom: One more than the last pointer
602 * @height: the height this buffer is at
603 * @data: a pointer to a struct strip_mine
608 static int do_strip(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
609 struct buffer_head
*bh
, uint64_t *top
, uint64_t *bottom
,
610 unsigned int height
, void *data
)
612 struct strip_mine
*sm
= data
;
613 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
614 struct gfs2_rgrp_list rlist
;
618 unsigned int rg_blocks
= 0;
620 unsigned int revokes
= 0;
627 if (height
!= sm
->sm_height
)
635 metadata
= (height
!= ip
->i_di
.di_height
- 1);
637 revokes
= (height
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
639 error
= gfs2_rindex_hold(sdp
, &ip
->i_alloc
.al_ri_gh
);
643 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
647 for (p
= top
; p
< bottom
; p
++) {
651 bn
= be64_to_cpu(*p
);
653 if (bstart
+ blen
== bn
)
657 gfs2_rlist_add(sdp
, &rlist
, bstart
);
665 gfs2_rlist_add(sdp
, &rlist
, bstart
);
667 goto out
; /* Nothing to do */
669 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
, 0);
671 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
672 struct gfs2_rgrpd
*rgd
;
673 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
674 rg_blocks
+= rgd
->rd_ri
.ri_length
;
677 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
681 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+
682 RES_INDIRECT
+ RES_STATFS
+ RES_QUOTA
,
687 down_write(&ip
->i_rw_mutex
);
689 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
690 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
695 for (p
= top
; p
< bottom
; p
++) {
699 bn
= be64_to_cpu(*p
);
701 if (bstart
+ blen
== bn
)
706 gfs2_free_meta(ip
, bstart
, blen
);
708 gfs2_free_data(ip
, bstart
, blen
);
716 if (!ip
->i_di
.di_blocks
)
717 gfs2_consist_inode(ip
);
718 ip
->i_di
.di_blocks
--;
722 gfs2_free_meta(ip
, bstart
, blen
);
724 gfs2_free_data(ip
, bstart
, blen
);
727 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
729 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
731 up_write(&ip
->i_rw_mutex
);
736 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
739 gfs2_rlist_free(&rlist
);
742 gfs2_glock_dq_uninit(&ip
->i_alloc
.al_ri_gh
);
748 * do_grow - Make a file look bigger than it is
750 * @size: the size to set the file to
752 * Called with an exclusive lock on @ip.
757 static int do_grow(struct gfs2_inode
*ip
, uint64_t size
)
759 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
760 struct gfs2_alloc
*al
;
761 struct buffer_head
*dibh
;
765 al
= gfs2_alloc_get(ip
);
767 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
771 error
= gfs2_quota_check(ip
, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
775 al
->al_requested
= sdp
->sd_max_height
+ RES_DATA
;
777 error
= gfs2_inplace_reserve(ip
);
781 error
= gfs2_trans_begin(sdp
,
782 sdp
->sd_max_height
+ al
->al_rgd
->rd_ri
.ri_length
+
783 RES_JDATA
+ RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
787 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
)) {
788 if (gfs2_is_stuffed(ip
)) {
789 error
= gfs2_unstuff_dinode(ip
, gfs2_unstuffer_page
,
795 h
= calc_tree_height(ip
, size
);
796 if (ip
->i_di
.di_height
< h
) {
797 down_write(&ip
->i_rw_mutex
);
798 error
= build_height(&ip
->i_inode
, h
);
799 up_write(&ip
->i_rw_mutex
);
805 ip
->i_di
.di_size
= size
;
806 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
808 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
812 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
813 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
820 gfs2_inplace_release(ip
);
823 gfs2_quota_unlock(ip
);
831 static int trunc_start(struct gfs2_inode
*ip
, uint64_t size
)
833 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
834 struct buffer_head
*dibh
;
835 int journaled
= gfs2_is_jdata(ip
);
838 error
= gfs2_trans_begin(sdp
,
839 RES_DINODE
+ ((journaled
) ? RES_JDATA
: 0), 0);
843 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
847 if (gfs2_is_stuffed(ip
)) {
848 ip
->i_di
.di_size
= size
;
849 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
850 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
851 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
852 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
) + size
);
856 if (size
& (uint64_t)(sdp
->sd_sb
.sb_bsize
- 1))
857 error
= gfs2_block_truncate_page(ip
->i_inode
.i_mapping
);
860 ip
->i_di
.di_size
= size
;
861 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
862 ip
->i_di
.di_flags
|= GFS2_DIF_TRUNC_IN_PROG
;
863 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
864 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
876 static int trunc_dealloc(struct gfs2_inode
*ip
, uint64_t size
)
878 unsigned int height
= ip
->i_di
.di_height
;
886 lblock
= (size
- 1) >> GFS2_SB(&ip
->i_inode
)->sd_sb
.sb_bsize_shift
;
888 find_metapath(ip
, lblock
, &mp
);
891 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
896 struct strip_mine sm
;
897 sm
.sm_first
= !!size
;
898 sm
.sm_height
= height
;
900 error
= recursive_scan(ip
, NULL
, &mp
, 0, 0, 1, do_strip
, &sm
);
905 gfs2_quota_unhold(ip
);
912 static int trunc_end(struct gfs2_inode
*ip
)
914 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
915 struct buffer_head
*dibh
;
918 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
922 down_write(&ip
->i_rw_mutex
);
924 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
928 if (!ip
->i_di
.di_size
) {
929 ip
->i_di
.di_height
= 0;
930 ip
->i_di
.di_goal_meta
=
931 ip
->i_di
.di_goal_data
=
933 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
935 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
936 ip
->i_di
.di_flags
&= ~GFS2_DIF_TRUNC_IN_PROG
;
938 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
939 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
943 up_write(&ip
->i_rw_mutex
);
951 * do_shrink - make a file smaller
953 * @size: the size to make the file
954 * @truncator: function to truncate the last partial block
956 * Called with an exclusive lock on @ip.
961 static int do_shrink(struct gfs2_inode
*ip
, uint64_t size
)
965 error
= trunc_start(ip
, size
);
971 error
= trunc_dealloc(ip
, size
);
973 error
= trunc_end(ip
);
979 * gfs2_truncatei - make a file a given size
981 * @size: the size to make the file
982 * @truncator: function to truncate the last partial block
984 * The file size can grow, shrink, or stay the same size.
989 int gfs2_truncatei(struct gfs2_inode
*ip
, uint64_t size
)
993 if (gfs2_assert_warn(GFS2_SB(&ip
->i_inode
), S_ISREG(ip
->i_di
.di_mode
)))
996 if (size
> ip
->i_di
.di_size
)
997 error
= do_grow(ip
, size
);
999 error
= do_shrink(ip
, size
);
1004 int gfs2_truncatei_resume(struct gfs2_inode
*ip
)
1007 error
= trunc_dealloc(ip
, ip
->i_di
.di_size
);
1009 error
= trunc_end(ip
);
1013 int gfs2_file_dealloc(struct gfs2_inode
*ip
)
1015 return trunc_dealloc(ip
, 0);
1019 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1021 * @len: the number of bytes to be written to the file
1022 * @data_blocks: returns the number of data blocks required
1023 * @ind_blocks: returns the number of indirect blocks required
1027 void gfs2_write_calc_reserv(struct gfs2_inode
*ip
, unsigned int len
,
1028 unsigned int *data_blocks
, unsigned int *ind_blocks
)
1030 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1033 if (gfs2_is_dir(ip
)) {
1034 *data_blocks
= DIV_ROUND_UP(len
, sdp
->sd_jbsize
) + 2;
1035 *ind_blocks
= 3 * (sdp
->sd_max_jheight
- 1);
1037 *data_blocks
= (len
>> sdp
->sd_sb
.sb_bsize_shift
) + 3;
1038 *ind_blocks
= 3 * (sdp
->sd_max_height
- 1);
1041 for (tmp
= *data_blocks
; tmp
> sdp
->sd_diptrs
;) {
1042 tmp
= DIV_ROUND_UP(tmp
, sdp
->sd_inptrs
);
1048 * gfs2_write_alloc_required - figure out if a write will require an allocation
1049 * @ip: the file being written to
1050 * @offset: the offset to write to
1051 * @len: the number of bytes being written
1052 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1057 int gfs2_write_alloc_required(struct gfs2_inode
*ip
, uint64_t offset
,
1058 unsigned int len
, int *alloc_required
)
1060 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1061 uint64_t lblock
, lblock_stop
, dblock
;
1066 *alloc_required
= 0;
1071 if (gfs2_is_stuffed(ip
)) {
1073 sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
1074 *alloc_required
= 1;
1078 if (gfs2_is_dir(ip
)) {
1079 unsigned int bsize
= sdp
->sd_jbsize
;
1081 do_div(lblock
, bsize
);
1082 lblock_stop
= offset
+ len
+ bsize
- 1;
1083 do_div(lblock_stop
, bsize
);
1085 unsigned int shift
= sdp
->sd_sb
.sb_bsize_shift
;
1086 lblock
= offset
>> shift
;
1087 lblock_stop
= (offset
+ len
+ sdp
->sd_sb
.sb_bsize
- 1) >> shift
;
1090 for (; lblock
< lblock_stop
; lblock
+= extlen
) {
1091 error
= gfs2_extent_map(&ip
->i_inode
, lblock
, &new, &dblock
, &extlen
);
1096 *alloc_required
= 1;