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
167 * This routine makes sure that the metadata tree is tall enough to hold
168 * "size" bytes of data.
173 static int build_height(struct gfs2_inode
*ip
, int height
)
175 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
176 struct buffer_head
*bh
, *dibh
;
177 uint64_t block
= 0, *bp
;
182 while (ip
->i_di
.di_height
< height
) {
183 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
188 bp
= (uint64_t *)(dibh
->b_data
+ sizeof(struct gfs2_dinode
));
189 for (x
= 0; x
< sdp
->sd_diptrs
; x
++, bp
++)
196 /* Get a new block, fill it with the old direct
197 pointers, and write it out */
199 block
= gfs2_alloc_meta(ip
);
201 bh
= gfs2_meta_new(ip
->i_gl
, block
);
202 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
203 gfs2_metatype_set(bh
,
206 gfs2_buffer_copy_tail(bh
,
207 sizeof(struct gfs2_meta_header
),
208 dibh
, sizeof(struct gfs2_dinode
));
213 /* Set up the new direct pointer and write it out to disk */
215 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
217 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
220 *(uint64_t *)(dibh
->b_data
+
221 sizeof(struct gfs2_dinode
)) =
223 ip
->i_di
.di_blocks
++;
226 ip
->i_di
.di_height
++;
228 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
236 * find_metapath - Find path through the metadata tree
237 * @ip: The inode pointer
238 * @mp: The metapath to return the result in
239 * @block: The disk block to look up
241 * This routine returns a struct metapath structure that defines a path
242 * through the metadata of inode "ip" to get to block "block".
245 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
246 * filesystem with a blocksize of 4096.
248 * find_metapath() would return a struct metapath structure set to:
249 * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
250 * and mp_list[2] = 165.
252 * That means that in order to get to the block containing the byte at
253 * offset 101342453, we would load the indirect block pointed to by pointer
254 * 0 in the dinode. We would then load the indirect block pointed to by
255 * pointer 48 in that indirect block. We would then load the data block
256 * pointed to by pointer 165 in that indirect block.
258 * ----------------------------------------
263 * ----------------------------------------
267 * ----------------------------------------
271 * |0 5 6 7 8 9 0 1 2|
272 * ----------------------------------------
276 * ----------------------------------------
281 * ----------------------------------------
285 * ----------------------------------------
286 * | Data block containing offset |
290 * ----------------------------------------
294 static void find_metapath(struct gfs2_inode
*ip
, uint64_t block
,
297 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
301 for (i
= ip
->i_di
.di_height
; i
--;)
302 mp
->mp_list
[i
] = (__u16
)do_div(b
, sdp
->sd_inptrs
);
307 * metapointer - Return pointer to start of metadata in a buffer
309 * @height: The metadata height (0 = dinode)
312 * Return a pointer to the block number of the next height of the metadata
313 * tree given a buffer containing the pointer to the current height of the
317 static inline u64
*metapointer(struct buffer_head
*bh
, int *boundary
,
318 unsigned int height
, const struct metapath
*mp
)
320 unsigned int head_size
= (height
> 0) ?
321 sizeof(struct gfs2_meta_header
) : sizeof(struct gfs2_dinode
);
324 ptr
= ((u64
*)(bh
->b_data
+ head_size
)) + mp
->mp_list
[height
];
325 if (ptr
+ 1 == (u64
*)(bh
->b_data
+ bh
->b_size
))
331 * lookup_block - Get the next metadata block in metadata tree
332 * @ip: The GFS2 inode
333 * @bh: Buffer containing the pointers to metadata blocks
334 * @height: The height of the tree (0 = dinode)
336 * @create: Non-zero if we may create a new meatdata block
337 * @new: Used to indicate if we did create a new metadata block
338 * @block: the returned disk block number
340 * Given a metatree, complete to a particular height, checks to see if the next
341 * height of the tree exists. If not the next height of the tree is created.
342 * The block number of the next height of the metadata tree is returned.
346 static int lookup_block(struct gfs2_inode
*ip
, struct buffer_head
*bh
,
347 unsigned int height
, struct metapath
*mp
, int create
,
348 int *new, uint64_t *block
)
351 uint64_t *ptr
= metapointer(bh
, &boundary
, height
, mp
);
354 *block
= be64_to_cpu(*ptr
);
363 if (height
== ip
->i_di
.di_height
- 1 && !gfs2_is_dir(ip
))
364 *block
= gfs2_alloc_data(ip
);
366 *block
= gfs2_alloc_meta(ip
);
368 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
370 *ptr
= cpu_to_be64(*block
);
371 ip
->i_di
.di_blocks
++;
378 * gfs2_block_pointers - Map a block from an inode to a disk block
380 * @lblock: The logical block number
381 * @new: Value/Result argument (1 = may create/did create new blocks)
382 * @boundary: gets set if we've hit a block boundary
383 * @mp: metapath to use
385 * Find the block number on the current device which corresponds to an
386 * inode's block. If the block had to be created, "new" will be set.
391 static struct buffer_head
*gfs2_block_pointers(struct inode
*inode
, u64 lblock
,
392 int *new, u64
*dblock
,
396 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
397 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
398 struct buffer_head
*bh
;
402 unsigned int end_of_metadata
;
409 if (gfs2_assert_warn(sdp
, !gfs2_is_stuffed(ip
)))
412 bsize
= (gfs2_is_dir(ip
)) ? sdp
->sd_jbsize
: sdp
->sd_sb
.sb_bsize
;
414 height
= calc_tree_height(ip
, (lblock
+ 1) * bsize
);
415 if (ip
->i_di
.di_height
< height
) {
419 error
= build_height(ip
, height
);
424 find_metapath(ip
, lblock
, mp
);
425 end_of_metadata
= ip
->i_di
.di_height
- 1;
427 error
= gfs2_meta_inode_buffer(ip
, &bh
);
431 for (x
= 0; x
< end_of_metadata
; x
++) {
432 lookup_block(ip
, bh
, x
, mp
, create
, new, dblock
);
437 error
= gfs2_meta_indirect_buffer(ip
, x
+1, *dblock
, *new, &bh
);
442 *boundary
= lookup_block(ip
, bh
, end_of_metadata
, mp
, create
, new, dblock
);
444 struct buffer_head
*dibh
;
445 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
447 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
448 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
454 return ERR_PTR(error
);
458 static inline void bmap_lock(struct inode
*inode
, int create
)
460 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
462 down_write(&ip
->i_rw_mutex
);
464 down_read(&ip
->i_rw_mutex
);
467 static inline void bmap_unlock(struct inode
*inode
, int create
)
469 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
471 up_write(&ip
->i_rw_mutex
);
473 up_read(&ip
->i_rw_mutex
);
476 int gfs2_block_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, int *boundary
)
479 struct buffer_head
*bh
;
482 bmap_lock(inode
, create
);
483 bh
= gfs2_block_pointers(inode
, lblock
, new, dblock
, boundary
, &mp
);
484 bmap_unlock(inode
, create
);
493 int gfs2_extent_map(struct inode
*inode
, u64 lblock
, int *new, u64
*dblock
, unsigned *extlen
)
495 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
496 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
498 struct buffer_head
*bh
;
506 bmap_lock(inode
, create
);
507 bh
= gfs2_block_pointers(inode
, lblock
, new, dblock
, &boundary
, &mp
);
510 if (bh
&& !IS_ERR(bh
) && *dblock
&& !*new) {
514 unsigned end_of_metadata
= ip
->i_di
.di_height
- 1;
516 nptrs
= (end_of_metadata
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
517 while (++mp
.mp_list
[end_of_metadata
] < nptrs
) {
518 lookup_block(ip
, bh
, end_of_metadata
, &mp
, 0, &tmp_new
, &tmp_dblock
);
519 if (*dblock
+ *extlen
!= tmp_dblock
)
524 bmap_unlock(inode
, create
);
534 * recursive_scan - recursively scan through the end of a file
536 * @dibh: the dinode buffer
537 * @mp: the path through the metadata to the point to start
538 * @height: the height the recursion is at
539 * @block: the indirect block to look at
540 * @first: 1 if this is the first block
541 * @bc: the call to make for each piece of metadata
542 * @data: data opaque to this function to pass to @bc
544 * When this is first called @height and @block should be zero and
545 * @first should be 1.
550 static int recursive_scan(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
551 struct metapath
*mp
, unsigned int height
,
552 uint64_t block
, int first
, block_call_t bc
,
555 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
556 struct buffer_head
*bh
= NULL
;
557 uint64_t *top
, *bottom
;
560 int mh_size
= sizeof(struct gfs2_meta_header
);
563 error
= gfs2_meta_inode_buffer(ip
, &bh
);
568 top
= (uint64_t *)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) +
570 bottom
= (uint64_t *)(bh
->b_data
+ sizeof(struct gfs2_dinode
)) +
573 error
= gfs2_meta_indirect_buffer(ip
, height
, block
, 0, &bh
);
577 top
= (uint64_t *)(bh
->b_data
+ mh_size
) +
578 ((first
) ? mp
->mp_list
[height
] : 0);
580 bottom
= (uint64_t *)(bh
->b_data
+ mh_size
) + sdp
->sd_inptrs
;
583 error
= bc(ip
, dibh
, bh
, top
, bottom
, height
, data
);
587 if (height
< ip
->i_di
.di_height
- 1)
588 for (; top
< bottom
; top
++, first
= 0) {
592 bn
= be64_to_cpu(*top
);
594 error
= recursive_scan(ip
, dibh
, mp
, height
+ 1, bn
,
607 * do_strip - Look for a layer a particular layer of the file and strip it off
609 * @dibh: the dinode buffer
610 * @bh: A buffer of pointers
611 * @top: The first pointer in the buffer
612 * @bottom: One more than the last pointer
613 * @height: the height this buffer is at
614 * @data: a pointer to a struct strip_mine
619 static int do_strip(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
620 struct buffer_head
*bh
, uint64_t *top
, uint64_t *bottom
,
621 unsigned int height
, void *data
)
623 struct strip_mine
*sm
= (struct strip_mine
*)data
;
624 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
625 struct gfs2_rgrp_list rlist
;
629 unsigned int rg_blocks
= 0;
631 unsigned int revokes
= 0;
638 if (height
!= sm
->sm_height
)
646 metadata
= (height
!= ip
->i_di
.di_height
- 1);
648 revokes
= (height
) ? sdp
->sd_inptrs
: sdp
->sd_diptrs
;
650 error
= gfs2_rindex_hold(sdp
, &ip
->i_alloc
.al_ri_gh
);
654 memset(&rlist
, 0, sizeof(struct gfs2_rgrp_list
));
658 for (p
= top
; p
< bottom
; p
++) {
662 bn
= be64_to_cpu(*p
);
664 if (bstart
+ blen
== bn
)
668 gfs2_rlist_add(sdp
, &rlist
, bstart
);
676 gfs2_rlist_add(sdp
, &rlist
, bstart
);
678 goto out
; /* Nothing to do */
680 gfs2_rlist_alloc(&rlist
, LM_ST_EXCLUSIVE
, 0);
682 for (x
= 0; x
< rlist
.rl_rgrps
; x
++) {
683 struct gfs2_rgrpd
*rgd
;
684 rgd
= rlist
.rl_ghs
[x
].gh_gl
->gl_object
;
685 rg_blocks
+= rgd
->rd_ri
.ri_length
;
688 error
= gfs2_glock_nq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
692 error
= gfs2_trans_begin(sdp
, rg_blocks
+ RES_DINODE
+
693 RES_INDIRECT
+ RES_STATFS
+ RES_QUOTA
,
698 down_write(&ip
->i_rw_mutex
);
700 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
701 gfs2_trans_add_bh(ip
->i_gl
, bh
, 1);
706 for (p
= top
; p
< bottom
; p
++) {
710 bn
= be64_to_cpu(*p
);
712 if (bstart
+ blen
== bn
)
717 gfs2_free_meta(ip
, bstart
, blen
);
719 gfs2_free_data(ip
, bstart
, blen
);
727 if (!ip
->i_di
.di_blocks
)
728 gfs2_consist_inode(ip
);
729 ip
->i_di
.di_blocks
--;
733 gfs2_free_meta(ip
, bstart
, blen
);
735 gfs2_free_data(ip
, bstart
, blen
);
738 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
740 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
742 up_write(&ip
->i_rw_mutex
);
747 gfs2_glock_dq_m(rlist
.rl_rgrps
, rlist
.rl_ghs
);
750 gfs2_rlist_free(&rlist
);
753 gfs2_glock_dq_uninit(&ip
->i_alloc
.al_ri_gh
);
759 * do_grow - Make a file look bigger than it is
761 * @size: the size to set the file to
763 * Called with an exclusive lock on @ip.
768 static int do_grow(struct gfs2_inode
*ip
, uint64_t size
)
770 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
771 struct gfs2_alloc
*al
;
772 struct buffer_head
*dibh
;
776 al
= gfs2_alloc_get(ip
);
778 error
= gfs2_quota_lock(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
782 error
= gfs2_quota_check(ip
, ip
->i_di
.di_uid
, ip
->i_di
.di_gid
);
786 al
->al_requested
= sdp
->sd_max_height
+ RES_DATA
;
788 error
= gfs2_inplace_reserve(ip
);
792 error
= gfs2_trans_begin(sdp
,
793 sdp
->sd_max_height
+ al
->al_rgd
->rd_ri
.ri_length
+
794 RES_JDATA
+ RES_DINODE
+ RES_STATFS
+ RES_QUOTA
, 0);
798 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
)) {
799 if (gfs2_is_stuffed(ip
)) {
800 error
= gfs2_unstuff_dinode(ip
, gfs2_unstuffer_page
,
806 h
= calc_tree_height(ip
, size
);
807 if (ip
->i_di
.di_height
< h
) {
808 down_write(&ip
->i_rw_mutex
);
809 error
= build_height(ip
, h
);
810 up_write(&ip
->i_rw_mutex
);
816 ip
->i_di
.di_size
= size
;
817 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
819 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
823 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
824 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
831 gfs2_inplace_release(ip
);
834 gfs2_quota_unlock(ip
);
842 static int trunc_start(struct gfs2_inode
*ip
, uint64_t size
)
844 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
845 struct buffer_head
*dibh
;
846 int journaled
= gfs2_is_jdata(ip
);
849 error
= gfs2_trans_begin(sdp
,
850 RES_DINODE
+ ((journaled
) ? RES_JDATA
: 0), 0);
854 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
858 if (gfs2_is_stuffed(ip
)) {
859 ip
->i_di
.di_size
= size
;
860 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
861 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
862 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
863 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
) + size
);
867 if (size
& (uint64_t)(sdp
->sd_sb
.sb_bsize
- 1))
868 error
= gfs2_block_truncate_page(ip
->i_vnode
->i_mapping
);
871 ip
->i_di
.di_size
= size
;
872 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
873 ip
->i_di
.di_flags
|= GFS2_DIF_TRUNC_IN_PROG
;
874 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
875 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
887 static int trunc_dealloc(struct gfs2_inode
*ip
, uint64_t size
)
889 unsigned int height
= ip
->i_di
.di_height
;
897 lblock
= (size
- 1) >> ip
->i_sbd
->sd_sb
.sb_bsize_shift
;
899 find_metapath(ip
, lblock
, &mp
);
902 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
907 struct strip_mine sm
;
908 sm
.sm_first
= !!size
;
909 sm
.sm_height
= height
;
911 error
= recursive_scan(ip
, NULL
, &mp
, 0, 0, 1, do_strip
, &sm
);
916 gfs2_quota_unhold(ip
);
923 static int trunc_end(struct gfs2_inode
*ip
)
925 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
926 struct buffer_head
*dibh
;
929 error
= gfs2_trans_begin(sdp
, RES_DINODE
, 0);
933 down_write(&ip
->i_rw_mutex
);
935 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
939 if (!ip
->i_di
.di_size
) {
940 ip
->i_di
.di_height
= 0;
941 ip
->i_di
.di_goal_meta
=
942 ip
->i_di
.di_goal_data
=
944 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
946 ip
->i_di
.di_mtime
= ip
->i_di
.di_ctime
= get_seconds();
947 ip
->i_di
.di_flags
&= ~GFS2_DIF_TRUNC_IN_PROG
;
949 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
950 gfs2_dinode_out(&ip
->i_di
, dibh
->b_data
);
954 up_write(&ip
->i_rw_mutex
);
962 * do_shrink - make a file smaller
964 * @size: the size to make the file
965 * @truncator: function to truncate the last partial block
967 * Called with an exclusive lock on @ip.
972 static int do_shrink(struct gfs2_inode
*ip
, uint64_t size
)
976 error
= trunc_start(ip
, size
);
982 error
= trunc_dealloc(ip
, size
);
984 error
= trunc_end(ip
);
990 * gfs2_truncatei - make a file a given size
992 * @size: the size to make the file
993 * @truncator: function to truncate the last partial block
995 * The file size can grow, shrink, or stay the same size.
1000 int gfs2_truncatei(struct gfs2_inode
*ip
, uint64_t size
)
1004 if (gfs2_assert_warn(ip
->i_sbd
, S_ISREG(ip
->i_di
.di_mode
)))
1007 if (size
> ip
->i_di
.di_size
)
1008 error
= do_grow(ip
, size
);
1010 error
= do_shrink(ip
, size
);
1015 int gfs2_truncatei_resume(struct gfs2_inode
*ip
)
1018 error
= trunc_dealloc(ip
, ip
->i_di
.di_size
);
1020 error
= trunc_end(ip
);
1024 int gfs2_file_dealloc(struct gfs2_inode
*ip
)
1026 return trunc_dealloc(ip
, 0);
1030 * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
1032 * @len: the number of bytes to be written to the file
1033 * @data_blocks: returns the number of data blocks required
1034 * @ind_blocks: returns the number of indirect blocks required
1038 void gfs2_write_calc_reserv(struct gfs2_inode
*ip
, unsigned int len
,
1039 unsigned int *data_blocks
, unsigned int *ind_blocks
)
1041 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1044 if (gfs2_is_dir(ip
)) {
1045 *data_blocks
= DIV_ROUND_UP(len
, sdp
->sd_jbsize
) + 2;
1046 *ind_blocks
= 3 * (sdp
->sd_max_jheight
- 1);
1048 *data_blocks
= (len
>> sdp
->sd_sb
.sb_bsize_shift
) + 3;
1049 *ind_blocks
= 3 * (sdp
->sd_max_height
- 1);
1052 for (tmp
= *data_blocks
; tmp
> sdp
->sd_diptrs
;) {
1053 tmp
= DIV_ROUND_UP(tmp
, sdp
->sd_inptrs
);
1059 * gfs2_write_alloc_required - figure out if a write will require an allocation
1060 * @ip: the file being written to
1061 * @offset: the offset to write to
1062 * @len: the number of bytes being written
1063 * @alloc_required: set to 1 if an alloc is required, 0 otherwise
1068 int gfs2_write_alloc_required(struct gfs2_inode
*ip
, uint64_t offset
,
1069 unsigned int len
, int *alloc_required
)
1071 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
1072 uint64_t lblock
, lblock_stop
, dblock
;
1077 *alloc_required
= 0;
1082 if (gfs2_is_stuffed(ip
)) {
1084 sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
))
1085 *alloc_required
= 1;
1089 if (gfs2_is_dir(ip
)) {
1090 unsigned int bsize
= sdp
->sd_jbsize
;
1092 do_div(lblock
, bsize
);
1093 lblock_stop
= offset
+ len
+ bsize
- 1;
1094 do_div(lblock_stop
, bsize
);
1096 unsigned int shift
= sdp
->sd_sb
.sb_bsize_shift
;
1097 lblock
= offset
>> shift
;
1098 lblock_stop
= (offset
+ len
+ sdp
->sd_sb
.sb_bsize
- 1) >> shift
;
1101 for (; lblock
< lblock_stop
; lblock
+= extlen
) {
1102 error
= gfs2_extent_map(ip
->i_vnode
, lblock
, &new, &dblock
, &extlen
);
1107 *alloc_required
= 1;