2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
33 #include <linux/time.h>
34 #include <linux/jbd2.h>
35 #include <linux/highuid.h>
36 #include <linux/pagemap.h>
37 #include <linux/quotaops.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include <linux/falloc.h>
41 #include <asm/uaccess.h>
42 #include <linux/fiemap.h>
43 #include "ext4_jbd2.h"
45 #include <trace/events/ext4.h>
48 * used by extent splitting.
50 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
52 #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
53 #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
55 #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
56 #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
58 static __le32
ext4_extent_block_csum(struct inode
*inode
,
59 struct ext4_extent_header
*eh
)
61 struct ext4_inode_info
*ei
= EXT4_I(inode
);
62 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
65 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)eh
,
66 EXT4_EXTENT_TAIL_OFFSET(eh
));
67 return cpu_to_le32(csum
);
70 static int ext4_extent_block_csum_verify(struct inode
*inode
,
71 struct ext4_extent_header
*eh
)
73 struct ext4_extent_tail
*et
;
75 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
76 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
79 et
= find_ext4_extent_tail(eh
);
80 if (et
->et_checksum
!= ext4_extent_block_csum(inode
, eh
))
85 static void ext4_extent_block_csum_set(struct inode
*inode
,
86 struct ext4_extent_header
*eh
)
88 struct ext4_extent_tail
*et
;
90 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
91 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
94 et
= find_ext4_extent_tail(eh
);
95 et
->et_checksum
= ext4_extent_block_csum(inode
, eh
);
98 static int ext4_split_extent(handle_t
*handle
,
100 struct ext4_ext_path
*path
,
101 struct ext4_map_blocks
*map
,
105 static int ext4_split_extent_at(handle_t
*handle
,
107 struct ext4_ext_path
*path
,
112 static int ext4_find_delayed_extent(struct inode
*inode
,
113 struct ext4_ext_cache
*newex
);
115 static int ext4_ext_truncate_extend_restart(handle_t
*handle
,
121 if (!ext4_handle_valid(handle
))
123 if (handle
->h_buffer_credits
> needed
)
125 err
= ext4_journal_extend(handle
, needed
);
128 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
140 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
141 struct ext4_ext_path
*path
)
144 /* path points to block */
145 return ext4_journal_get_write_access(handle
, path
->p_bh
);
147 /* path points to leaf/index in inode body */
148 /* we use in-core data, no need to protect them */
158 #define ext4_ext_dirty(handle, inode, path) \
159 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
160 static int __ext4_ext_dirty(const char *where
, unsigned int line
,
161 handle_t
*handle
, struct inode
*inode
,
162 struct ext4_ext_path
*path
)
166 ext4_extent_block_csum_set(inode
, ext_block_hdr(path
->p_bh
));
167 /* path points to block */
168 err
= __ext4_handle_dirty_metadata(where
, line
, handle
,
171 /* path points to leaf/index in inode body */
172 err
= ext4_mark_inode_dirty(handle
, inode
);
177 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
178 struct ext4_ext_path
*path
,
182 int depth
= path
->p_depth
;
183 struct ext4_extent
*ex
;
186 * Try to predict block placement assuming that we are
187 * filling in a file which will eventually be
188 * non-sparse --- i.e., in the case of libbfd writing
189 * an ELF object sections out-of-order but in a way
190 * the eventually results in a contiguous object or
191 * executable file, or some database extending a table
192 * space file. However, this is actually somewhat
193 * non-ideal if we are writing a sparse file such as
194 * qemu or KVM writing a raw image file that is going
195 * to stay fairly sparse, since it will end up
196 * fragmenting the file system's free space. Maybe we
197 * should have some hueristics or some way to allow
198 * userspace to pass a hint to file system,
199 * especially if the latter case turns out to be
202 ex
= path
[depth
].p_ext
;
204 ext4_fsblk_t ext_pblk
= ext4_ext_pblock(ex
);
205 ext4_lblk_t ext_block
= le32_to_cpu(ex
->ee_block
);
207 if (block
> ext_block
)
208 return ext_pblk
+ (block
- ext_block
);
210 return ext_pblk
- (ext_block
- block
);
213 /* it looks like index is empty;
214 * try to find starting block from index itself */
215 if (path
[depth
].p_bh
)
216 return path
[depth
].p_bh
->b_blocknr
;
219 /* OK. use inode's group */
220 return ext4_inode_to_goal_block(inode
);
224 * Allocation for a meta data block
227 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
228 struct ext4_ext_path
*path
,
229 struct ext4_extent
*ex
, int *err
, unsigned int flags
)
231 ext4_fsblk_t goal
, newblock
;
233 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
234 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, flags
,
239 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
243 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
244 / sizeof(struct ext4_extent
);
245 #ifdef AGGRESSIVE_TEST
246 if (!check
&& size
> 6)
252 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
256 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
257 / sizeof(struct ext4_extent_idx
);
258 #ifdef AGGRESSIVE_TEST
259 if (!check
&& size
> 5)
265 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
269 size
= sizeof(EXT4_I(inode
)->i_data
);
270 size
-= sizeof(struct ext4_extent_header
);
271 size
/= sizeof(struct ext4_extent
);
272 #ifdef AGGRESSIVE_TEST
273 if (!check
&& size
> 3)
279 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
283 size
= sizeof(EXT4_I(inode
)->i_data
);
284 size
-= sizeof(struct ext4_extent_header
);
285 size
/= sizeof(struct ext4_extent_idx
);
286 #ifdef AGGRESSIVE_TEST
287 if (!check
&& size
> 4)
294 * Calculate the number of metadata blocks needed
295 * to allocate @blocks
296 * Worse case is one block per extent
298 int ext4_ext_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
300 struct ext4_inode_info
*ei
= EXT4_I(inode
);
303 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
304 / sizeof(struct ext4_extent_idx
));
307 * If the new delayed allocation block is contiguous with the
308 * previous da block, it can share index blocks with the
309 * previous block, so we only need to allocate a new index
310 * block every idxs leaf blocks. At ldxs**2 blocks, we need
311 * an additional index block, and at ldxs**3 blocks, yet
312 * another index blocks.
314 if (ei
->i_da_metadata_calc_len
&&
315 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
318 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
320 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
322 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
324 ei
->i_da_metadata_calc_len
= 0;
326 ei
->i_da_metadata_calc_len
++;
327 ei
->i_da_metadata_calc_last_lblock
++;
332 * In the worst case we need a new set of index blocks at
333 * every level of the inode's extent tree.
335 ei
->i_da_metadata_calc_len
= 1;
336 ei
->i_da_metadata_calc_last_lblock
= lblock
;
337 return ext_depth(inode
) + 1;
341 ext4_ext_max_entries(struct inode
*inode
, int depth
)
345 if (depth
== ext_depth(inode
)) {
347 max
= ext4_ext_space_root(inode
, 1);
349 max
= ext4_ext_space_root_idx(inode
, 1);
352 max
= ext4_ext_space_block(inode
, 1);
354 max
= ext4_ext_space_block_idx(inode
, 1);
360 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
362 ext4_fsblk_t block
= ext4_ext_pblock(ext
);
363 int len
= ext4_ext_get_actual_len(ext
);
367 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
370 static int ext4_valid_extent_idx(struct inode
*inode
,
371 struct ext4_extent_idx
*ext_idx
)
373 ext4_fsblk_t block
= ext4_idx_pblock(ext_idx
);
375 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
378 static int ext4_valid_extent_entries(struct inode
*inode
,
379 struct ext4_extent_header
*eh
,
382 unsigned short entries
;
383 if (eh
->eh_entries
== 0)
386 entries
= le16_to_cpu(eh
->eh_entries
);
390 struct ext4_extent
*ext
= EXT_FIRST_EXTENT(eh
);
392 if (!ext4_valid_extent(inode
, ext
))
398 struct ext4_extent_idx
*ext_idx
= EXT_FIRST_INDEX(eh
);
400 if (!ext4_valid_extent_idx(inode
, ext_idx
))
409 static int __ext4_ext_check(const char *function
, unsigned int line
,
410 struct inode
*inode
, struct ext4_extent_header
*eh
,
413 const char *error_msg
;
416 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
417 error_msg
= "invalid magic";
420 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
421 error_msg
= "unexpected eh_depth";
424 if (unlikely(eh
->eh_max
== 0)) {
425 error_msg
= "invalid eh_max";
428 max
= ext4_ext_max_entries(inode
, depth
);
429 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
430 error_msg
= "too large eh_max";
433 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
434 error_msg
= "invalid eh_entries";
437 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
438 error_msg
= "invalid extent entries";
441 /* Verify checksum on non-root extent tree nodes */
442 if (ext_depth(inode
) != depth
&&
443 !ext4_extent_block_csum_verify(inode
, eh
)) {
444 error_msg
= "extent tree corrupted";
450 ext4_error_inode(inode
, function
, line
, 0,
451 "bad header/extent: %s - magic %x, "
452 "entries %u, max %u(%u), depth %u(%u)",
453 error_msg
, le16_to_cpu(eh
->eh_magic
),
454 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
455 max
, le16_to_cpu(eh
->eh_depth
), depth
);
460 #define ext4_ext_check(inode, eh, depth) \
461 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
463 int ext4_ext_check_inode(struct inode
*inode
)
465 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
468 static int __ext4_ext_check_block(const char *function
, unsigned int line
,
470 struct ext4_extent_header
*eh
,
472 struct buffer_head
*bh
)
476 if (buffer_verified(bh
))
478 ret
= ext4_ext_check(inode
, eh
, depth
);
481 set_buffer_verified(bh
);
485 #define ext4_ext_check_block(inode, eh, depth, bh) \
486 __ext4_ext_check_block(__func__, __LINE__, inode, eh, depth, bh)
489 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
491 int k
, l
= path
->p_depth
;
494 for (k
= 0; k
<= l
; k
++, path
++) {
496 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
497 ext4_idx_pblock(path
->p_idx
));
498 } else if (path
->p_ext
) {
499 ext_debug(" %d:[%d]%d:%llu ",
500 le32_to_cpu(path
->p_ext
->ee_block
),
501 ext4_ext_is_uninitialized(path
->p_ext
),
502 ext4_ext_get_actual_len(path
->p_ext
),
503 ext4_ext_pblock(path
->p_ext
));
510 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
512 int depth
= ext_depth(inode
);
513 struct ext4_extent_header
*eh
;
514 struct ext4_extent
*ex
;
520 eh
= path
[depth
].p_hdr
;
521 ex
= EXT_FIRST_EXTENT(eh
);
523 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
525 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
526 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
527 ext4_ext_is_uninitialized(ex
),
528 ext4_ext_get_actual_len(ex
), ext4_ext_pblock(ex
));
533 static void ext4_ext_show_move(struct inode
*inode
, struct ext4_ext_path
*path
,
534 ext4_fsblk_t newblock
, int level
)
536 int depth
= ext_depth(inode
);
537 struct ext4_extent
*ex
;
539 if (depth
!= level
) {
540 struct ext4_extent_idx
*idx
;
541 idx
= path
[level
].p_idx
;
542 while (idx
<= EXT_MAX_INDEX(path
[level
].p_hdr
)) {
543 ext_debug("%d: move %d:%llu in new index %llu\n", level
,
544 le32_to_cpu(idx
->ei_block
),
545 ext4_idx_pblock(idx
),
553 ex
= path
[depth
].p_ext
;
554 while (ex
<= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
555 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
556 le32_to_cpu(ex
->ee_block
),
558 ext4_ext_is_uninitialized(ex
),
559 ext4_ext_get_actual_len(ex
),
566 #define ext4_ext_show_path(inode, path)
567 #define ext4_ext_show_leaf(inode, path)
568 #define ext4_ext_show_move(inode, path, newblock, level)
571 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
573 int depth
= path
->p_depth
;
576 for (i
= 0; i
<= depth
; i
++, path
++)
584 * ext4_ext_binsearch_idx:
585 * binary search for the closest index of the given block
586 * the header must be checked before calling this
589 ext4_ext_binsearch_idx(struct inode
*inode
,
590 struct ext4_ext_path
*path
, ext4_lblk_t block
)
592 struct ext4_extent_header
*eh
= path
->p_hdr
;
593 struct ext4_extent_idx
*r
, *l
, *m
;
596 ext_debug("binsearch for %u(idx): ", block
);
598 l
= EXT_FIRST_INDEX(eh
) + 1;
599 r
= EXT_LAST_INDEX(eh
);
602 if (block
< le32_to_cpu(m
->ei_block
))
606 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
607 m
, le32_to_cpu(m
->ei_block
),
608 r
, le32_to_cpu(r
->ei_block
));
612 ext_debug(" -> %u->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
613 ext4_idx_pblock(path
->p_idx
));
615 #ifdef CHECK_BINSEARCH
617 struct ext4_extent_idx
*chix
, *ix
;
620 chix
= ix
= EXT_FIRST_INDEX(eh
);
621 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
623 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
624 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
626 ix
, EXT_FIRST_INDEX(eh
));
627 printk(KERN_DEBUG
"%u <= %u\n",
628 le32_to_cpu(ix
->ei_block
),
629 le32_to_cpu(ix
[-1].ei_block
));
631 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
632 <= le32_to_cpu(ix
[-1].ei_block
));
633 if (block
< le32_to_cpu(ix
->ei_block
))
637 BUG_ON(chix
!= path
->p_idx
);
644 * ext4_ext_binsearch:
645 * binary search for closest extent of the given block
646 * the header must be checked before calling this
649 ext4_ext_binsearch(struct inode
*inode
,
650 struct ext4_ext_path
*path
, ext4_lblk_t block
)
652 struct ext4_extent_header
*eh
= path
->p_hdr
;
653 struct ext4_extent
*r
, *l
, *m
;
655 if (eh
->eh_entries
== 0) {
657 * this leaf is empty:
658 * we get such a leaf in split/add case
663 ext_debug("binsearch for %u: ", block
);
665 l
= EXT_FIRST_EXTENT(eh
) + 1;
666 r
= EXT_LAST_EXTENT(eh
);
670 if (block
< le32_to_cpu(m
->ee_block
))
674 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
675 m
, le32_to_cpu(m
->ee_block
),
676 r
, le32_to_cpu(r
->ee_block
));
680 ext_debug(" -> %d:%llu:[%d]%d ",
681 le32_to_cpu(path
->p_ext
->ee_block
),
682 ext4_ext_pblock(path
->p_ext
),
683 ext4_ext_is_uninitialized(path
->p_ext
),
684 ext4_ext_get_actual_len(path
->p_ext
));
686 #ifdef CHECK_BINSEARCH
688 struct ext4_extent
*chex
, *ex
;
691 chex
= ex
= EXT_FIRST_EXTENT(eh
);
692 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
693 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
694 <= le32_to_cpu(ex
[-1].ee_block
));
695 if (block
< le32_to_cpu(ex
->ee_block
))
699 BUG_ON(chex
!= path
->p_ext
);
705 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
707 struct ext4_extent_header
*eh
;
709 eh
= ext_inode_hdr(inode
);
712 eh
->eh_magic
= EXT4_EXT_MAGIC
;
713 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
714 ext4_mark_inode_dirty(handle
, inode
);
715 ext4_ext_invalidate_cache(inode
);
719 struct ext4_ext_path
*
720 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
721 struct ext4_ext_path
*path
)
723 struct ext4_extent_header
*eh
;
724 struct buffer_head
*bh
;
725 short int depth
, i
, ppos
= 0, alloc
= 0;
727 eh
= ext_inode_hdr(inode
);
728 depth
= ext_depth(inode
);
730 /* account possible depth increase */
732 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
735 return ERR_PTR(-ENOMEM
);
742 /* walk through the tree */
744 ext_debug("depth %d: num %d, max %d\n",
745 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
747 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
748 path
[ppos
].p_block
= ext4_idx_pblock(path
[ppos
].p_idx
);
749 path
[ppos
].p_depth
= i
;
750 path
[ppos
].p_ext
= NULL
;
752 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
755 if (!bh_uptodate_or_lock(bh
)) {
756 trace_ext4_ext_load_extent(inode
, block
,
758 if (bh_submit_read(bh
) < 0) {
763 eh
= ext_block_hdr(bh
);
765 if (unlikely(ppos
> depth
)) {
767 EXT4_ERROR_INODE(inode
,
768 "ppos %d > depth %d", ppos
, depth
);
771 path
[ppos
].p_bh
= bh
;
772 path
[ppos
].p_hdr
= eh
;
775 if (ext4_ext_check_block(inode
, eh
, i
, bh
))
779 path
[ppos
].p_depth
= i
;
780 path
[ppos
].p_ext
= NULL
;
781 path
[ppos
].p_idx
= NULL
;
784 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
785 /* if not an empty leaf */
786 if (path
[ppos
].p_ext
)
787 path
[ppos
].p_block
= ext4_ext_pblock(path
[ppos
].p_ext
);
789 ext4_ext_show_path(inode
, path
);
794 ext4_ext_drop_refs(path
);
797 return ERR_PTR(-EIO
);
801 * ext4_ext_insert_index:
802 * insert new index [@logical;@ptr] into the block at @curp;
803 * check where to insert: before @curp or after @curp
805 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
806 struct ext4_ext_path
*curp
,
807 int logical
, ext4_fsblk_t ptr
)
809 struct ext4_extent_idx
*ix
;
812 err
= ext4_ext_get_access(handle
, inode
, curp
);
816 if (unlikely(logical
== le32_to_cpu(curp
->p_idx
->ei_block
))) {
817 EXT4_ERROR_INODE(inode
,
818 "logical %d == ei_block %d!",
819 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
823 if (unlikely(le16_to_cpu(curp
->p_hdr
->eh_entries
)
824 >= le16_to_cpu(curp
->p_hdr
->eh_max
))) {
825 EXT4_ERROR_INODE(inode
,
826 "eh_entries %d >= eh_max %d!",
827 le16_to_cpu(curp
->p_hdr
->eh_entries
),
828 le16_to_cpu(curp
->p_hdr
->eh_max
));
832 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
834 ext_debug("insert new index %d after: %llu\n", logical
, ptr
);
835 ix
= curp
->p_idx
+ 1;
838 ext_debug("insert new index %d before: %llu\n", logical
, ptr
);
842 len
= EXT_LAST_INDEX(curp
->p_hdr
) - ix
+ 1;
845 ext_debug("insert new index %d: "
846 "move %d indices from 0x%p to 0x%p\n",
847 logical
, len
, ix
, ix
+ 1);
848 memmove(ix
+ 1, ix
, len
* sizeof(struct ext4_extent_idx
));
851 if (unlikely(ix
> EXT_MAX_INDEX(curp
->p_hdr
))) {
852 EXT4_ERROR_INODE(inode
, "ix > EXT_MAX_INDEX!");
856 ix
->ei_block
= cpu_to_le32(logical
);
857 ext4_idx_store_pblock(ix
, ptr
);
858 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
860 if (unlikely(ix
> EXT_LAST_INDEX(curp
->p_hdr
))) {
861 EXT4_ERROR_INODE(inode
, "ix > EXT_LAST_INDEX!");
865 err
= ext4_ext_dirty(handle
, inode
, curp
);
866 ext4_std_error(inode
->i_sb
, err
);
873 * inserts new subtree into the path, using free index entry
875 * - allocates all needed blocks (new leaf and all intermediate index blocks)
876 * - makes decision where to split
877 * - moves remaining extents and index entries (right to the split point)
878 * into the newly allocated blocks
879 * - initializes subtree
881 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
883 struct ext4_ext_path
*path
,
884 struct ext4_extent
*newext
, int at
)
886 struct buffer_head
*bh
= NULL
;
887 int depth
= ext_depth(inode
);
888 struct ext4_extent_header
*neh
;
889 struct ext4_extent_idx
*fidx
;
891 ext4_fsblk_t newblock
, oldblock
;
893 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
896 /* make decision: where to split? */
897 /* FIXME: now decision is simplest: at current extent */
899 /* if current leaf will be split, then we should use
900 * border from split point */
901 if (unlikely(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
))) {
902 EXT4_ERROR_INODE(inode
, "p_ext > EXT_MAX_EXTENT!");
905 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
906 border
= path
[depth
].p_ext
[1].ee_block
;
907 ext_debug("leaf will be split."
908 " next leaf starts at %d\n",
909 le32_to_cpu(border
));
911 border
= newext
->ee_block
;
912 ext_debug("leaf will be added."
913 " next leaf starts at %d\n",
914 le32_to_cpu(border
));
918 * If error occurs, then we break processing
919 * and mark filesystem read-only. index won't
920 * be inserted and tree will be in consistent
921 * state. Next mount will repair buffers too.
925 * Get array to track all allocated blocks.
926 * We need this to handle errors and free blocks
929 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
933 /* allocate all needed blocks */
934 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
935 for (a
= 0; a
< depth
- at
; a
++) {
936 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
937 newext
, &err
, flags
);
940 ablocks
[a
] = newblock
;
943 /* initialize new leaf */
944 newblock
= ablocks
[--a
];
945 if (unlikely(newblock
== 0)) {
946 EXT4_ERROR_INODE(inode
, "newblock == 0!");
950 bh
= sb_getblk(inode
->i_sb
, newblock
);
957 err
= ext4_journal_get_create_access(handle
, bh
);
961 neh
= ext_block_hdr(bh
);
963 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
964 neh
->eh_magic
= EXT4_EXT_MAGIC
;
967 /* move remainder of path[depth] to the new leaf */
968 if (unlikely(path
[depth
].p_hdr
->eh_entries
!=
969 path
[depth
].p_hdr
->eh_max
)) {
970 EXT4_ERROR_INODE(inode
, "eh_entries %d != eh_max %d!",
971 path
[depth
].p_hdr
->eh_entries
,
972 path
[depth
].p_hdr
->eh_max
);
976 /* start copy from next extent */
977 m
= EXT_MAX_EXTENT(path
[depth
].p_hdr
) - path
[depth
].p_ext
++;
978 ext4_ext_show_move(inode
, path
, newblock
, depth
);
980 struct ext4_extent
*ex
;
981 ex
= EXT_FIRST_EXTENT(neh
);
982 memmove(ex
, path
[depth
].p_ext
, sizeof(struct ext4_extent
) * m
);
983 le16_add_cpu(&neh
->eh_entries
, m
);
986 ext4_extent_block_csum_set(inode
, neh
);
987 set_buffer_uptodate(bh
);
990 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
996 /* correct old leaf */
998 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1001 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
1002 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1008 /* create intermediate indexes */
1010 if (unlikely(k
< 0)) {
1011 EXT4_ERROR_INODE(inode
, "k %d < 0!", k
);
1016 ext_debug("create %d intermediate indices\n", k
);
1017 /* insert new index into current index block */
1018 /* current depth stored in i var */
1021 oldblock
= newblock
;
1022 newblock
= ablocks
[--a
];
1023 bh
= sb_getblk(inode
->i_sb
, newblock
);
1030 err
= ext4_journal_get_create_access(handle
, bh
);
1034 neh
= ext_block_hdr(bh
);
1035 neh
->eh_entries
= cpu_to_le16(1);
1036 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1037 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1038 neh
->eh_depth
= cpu_to_le16(depth
- i
);
1039 fidx
= EXT_FIRST_INDEX(neh
);
1040 fidx
->ei_block
= border
;
1041 ext4_idx_store_pblock(fidx
, oldblock
);
1043 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1044 i
, newblock
, le32_to_cpu(border
), oldblock
);
1046 /* move remainder of path[i] to the new index block */
1047 if (unlikely(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
1048 EXT_LAST_INDEX(path
[i
].p_hdr
))) {
1049 EXT4_ERROR_INODE(inode
,
1050 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1051 le32_to_cpu(path
[i
].p_ext
->ee_block
));
1055 /* start copy indexes */
1056 m
= EXT_MAX_INDEX(path
[i
].p_hdr
) - path
[i
].p_idx
++;
1057 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
1058 EXT_MAX_INDEX(path
[i
].p_hdr
));
1059 ext4_ext_show_move(inode
, path
, newblock
, i
);
1061 memmove(++fidx
, path
[i
].p_idx
,
1062 sizeof(struct ext4_extent_idx
) * m
);
1063 le16_add_cpu(&neh
->eh_entries
, m
);
1065 ext4_extent_block_csum_set(inode
, neh
);
1066 set_buffer_uptodate(bh
);
1069 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1075 /* correct old index */
1077 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
1080 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1081 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1089 /* insert new index */
1090 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1091 le32_to_cpu(border
), newblock
);
1095 if (buffer_locked(bh
))
1101 /* free all allocated blocks in error case */
1102 for (i
= 0; i
< depth
; i
++) {
1105 ext4_free_blocks(handle
, inode
, NULL
, ablocks
[i
], 1,
1106 EXT4_FREE_BLOCKS_METADATA
);
1115 * ext4_ext_grow_indepth:
1116 * implements tree growing procedure:
1117 * - allocates new block
1118 * - moves top-level data (index block or leaf) into the new block
1119 * - initializes new top-level, creating index that points to the
1120 * just created block
1122 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1124 struct ext4_extent
*newext
)
1126 struct ext4_extent_header
*neh
;
1127 struct buffer_head
*bh
;
1128 ext4_fsblk_t newblock
;
1131 newblock
= ext4_ext_new_meta_block(handle
, inode
, NULL
,
1132 newext
, &err
, flags
);
1136 bh
= sb_getblk(inode
->i_sb
, newblock
);
1139 ext4_std_error(inode
->i_sb
, err
);
1144 err
= ext4_journal_get_create_access(handle
, bh
);
1150 /* move top-level index/leaf into new block */
1151 memmove(bh
->b_data
, EXT4_I(inode
)->i_data
,
1152 sizeof(EXT4_I(inode
)->i_data
));
1154 /* set size of new block */
1155 neh
= ext_block_hdr(bh
);
1156 /* old root could have indexes or leaves
1157 * so calculate e_max right way */
1158 if (ext_depth(inode
))
1159 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1161 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1162 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1163 ext4_extent_block_csum_set(inode
, neh
);
1164 set_buffer_uptodate(bh
);
1167 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1171 /* Update top-level index: num,max,pointer */
1172 neh
= ext_inode_hdr(inode
);
1173 neh
->eh_entries
= cpu_to_le16(1);
1174 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh
), newblock
);
1175 if (neh
->eh_depth
== 0) {
1176 /* Root extent block becomes index block */
1177 neh
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1178 EXT_FIRST_INDEX(neh
)->ei_block
=
1179 EXT_FIRST_EXTENT(neh
)->ee_block
;
1181 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1182 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1183 le32_to_cpu(EXT_FIRST_INDEX(neh
)->ei_block
),
1184 ext4_idx_pblock(EXT_FIRST_INDEX(neh
)));
1186 le16_add_cpu(&neh
->eh_depth
, 1);
1187 ext4_mark_inode_dirty(handle
, inode
);
1195 * ext4_ext_create_new_leaf:
1196 * finds empty index and adds new leaf.
1197 * if no free index is found, then it requests in-depth growing.
1199 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1201 struct ext4_ext_path
*path
,
1202 struct ext4_extent
*newext
)
1204 struct ext4_ext_path
*curp
;
1205 int depth
, i
, err
= 0;
1208 i
= depth
= ext_depth(inode
);
1210 /* walk up to the tree and look for free index entry */
1211 curp
= path
+ depth
;
1212 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1217 /* we use already allocated block for index block,
1218 * so subsequent data blocks should be contiguous */
1219 if (EXT_HAS_FREE_INDEX(curp
)) {
1220 /* if we found index with free entry, then use that
1221 * entry: create all needed subtree and add new leaf */
1222 err
= ext4_ext_split(handle
, inode
, flags
, path
, newext
, i
);
1227 ext4_ext_drop_refs(path
);
1228 path
= ext4_ext_find_extent(inode
,
1229 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1232 err
= PTR_ERR(path
);
1234 /* tree is full, time to grow in depth */
1235 err
= ext4_ext_grow_indepth(handle
, inode
, flags
, newext
);
1240 ext4_ext_drop_refs(path
);
1241 path
= ext4_ext_find_extent(inode
,
1242 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1245 err
= PTR_ERR(path
);
1250 * only first (depth 0 -> 1) produces free space;
1251 * in all other cases we have to split the grown tree
1253 depth
= ext_depth(inode
);
1254 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1255 /* now we need to split */
1265 * search the closest allocated block to the left for *logical
1266 * and returns it at @logical + it's physical address at @phys
1267 * if *logical is the smallest allocated block, the function
1268 * returns 0 at @phys
1269 * return value contains 0 (success) or error code
1271 static int ext4_ext_search_left(struct inode
*inode
,
1272 struct ext4_ext_path
*path
,
1273 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1275 struct ext4_extent_idx
*ix
;
1276 struct ext4_extent
*ex
;
1279 if (unlikely(path
== NULL
)) {
1280 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1283 depth
= path
->p_depth
;
1286 if (depth
== 0 && path
->p_ext
== NULL
)
1289 /* usually extent in the path covers blocks smaller
1290 * then *logical, but it can be that extent is the
1291 * first one in the file */
1293 ex
= path
[depth
].p_ext
;
1294 ee_len
= ext4_ext_get_actual_len(ex
);
1295 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1296 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1297 EXT4_ERROR_INODE(inode
,
1298 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1299 *logical
, le32_to_cpu(ex
->ee_block
));
1302 while (--depth
>= 0) {
1303 ix
= path
[depth
].p_idx
;
1304 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1305 EXT4_ERROR_INODE(inode
,
1306 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1307 ix
!= NULL
? le32_to_cpu(ix
->ei_block
) : 0,
1308 EXT_FIRST_INDEX(path
[depth
].p_hdr
) != NULL
?
1309 le32_to_cpu(EXT_FIRST_INDEX(path
[depth
].p_hdr
)->ei_block
) : 0,
1317 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1318 EXT4_ERROR_INODE(inode
,
1319 "logical %d < ee_block %d + ee_len %d!",
1320 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1324 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1325 *phys
= ext4_ext_pblock(ex
) + ee_len
- 1;
1330 * search the closest allocated block to the right for *logical
1331 * and returns it at @logical + it's physical address at @phys
1332 * if *logical is the largest allocated block, the function
1333 * returns 0 at @phys
1334 * return value contains 0 (success) or error code
1336 static int ext4_ext_search_right(struct inode
*inode
,
1337 struct ext4_ext_path
*path
,
1338 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
,
1339 struct ext4_extent
**ret_ex
)
1341 struct buffer_head
*bh
= NULL
;
1342 struct ext4_extent_header
*eh
;
1343 struct ext4_extent_idx
*ix
;
1344 struct ext4_extent
*ex
;
1346 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1349 if (unlikely(path
== NULL
)) {
1350 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1353 depth
= path
->p_depth
;
1356 if (depth
== 0 && path
->p_ext
== NULL
)
1359 /* usually extent in the path covers blocks smaller
1360 * then *logical, but it can be that extent is the
1361 * first one in the file */
1363 ex
= path
[depth
].p_ext
;
1364 ee_len
= ext4_ext_get_actual_len(ex
);
1365 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1366 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1367 EXT4_ERROR_INODE(inode
,
1368 "first_extent(path[%d].p_hdr) != ex",
1372 while (--depth
>= 0) {
1373 ix
= path
[depth
].p_idx
;
1374 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1375 EXT4_ERROR_INODE(inode
,
1376 "ix != EXT_FIRST_INDEX *logical %d!",
1384 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1385 EXT4_ERROR_INODE(inode
,
1386 "logical %d < ee_block %d + ee_len %d!",
1387 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1391 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1392 /* next allocated block in this leaf */
1397 /* go up and search for index to the right */
1398 while (--depth
>= 0) {
1399 ix
= path
[depth
].p_idx
;
1400 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1404 /* we've gone up to the root and found no index to the right */
1408 /* we've found index to the right, let's
1409 * follow it and find the closest allocated
1410 * block to the right */
1412 block
= ext4_idx_pblock(ix
);
1413 while (++depth
< path
->p_depth
) {
1414 bh
= sb_bread(inode
->i_sb
, block
);
1417 eh
= ext_block_hdr(bh
);
1418 /* subtract from p_depth to get proper eh_depth */
1419 if (ext4_ext_check_block(inode
, eh
,
1420 path
->p_depth
- depth
, bh
)) {
1424 ix
= EXT_FIRST_INDEX(eh
);
1425 block
= ext4_idx_pblock(ix
);
1429 bh
= sb_bread(inode
->i_sb
, block
);
1432 eh
= ext_block_hdr(bh
);
1433 if (ext4_ext_check_block(inode
, eh
, path
->p_depth
- depth
, bh
)) {
1437 ex
= EXT_FIRST_EXTENT(eh
);
1439 *logical
= le32_to_cpu(ex
->ee_block
);
1440 *phys
= ext4_ext_pblock(ex
);
1448 * ext4_ext_next_allocated_block:
1449 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1450 * NOTE: it considers block number from index entry as
1451 * allocated block. Thus, index entries have to be consistent
1455 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1459 BUG_ON(path
== NULL
);
1460 depth
= path
->p_depth
;
1462 if (depth
== 0 && path
->p_ext
== NULL
)
1463 return EXT_MAX_BLOCKS
;
1465 while (depth
>= 0) {
1466 if (depth
== path
->p_depth
) {
1468 if (path
[depth
].p_ext
&&
1469 path
[depth
].p_ext
!=
1470 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1471 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1474 if (path
[depth
].p_idx
!=
1475 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1476 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1481 return EXT_MAX_BLOCKS
;
1485 * ext4_ext_next_leaf_block:
1486 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1488 static ext4_lblk_t
ext4_ext_next_leaf_block(struct ext4_ext_path
*path
)
1492 BUG_ON(path
== NULL
);
1493 depth
= path
->p_depth
;
1495 /* zero-tree has no leaf blocks at all */
1497 return EXT_MAX_BLOCKS
;
1499 /* go to index block */
1502 while (depth
>= 0) {
1503 if (path
[depth
].p_idx
!=
1504 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1505 return (ext4_lblk_t
)
1506 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1510 return EXT_MAX_BLOCKS
;
1514 * ext4_ext_correct_indexes:
1515 * if leaf gets modified and modified extent is first in the leaf,
1516 * then we have to correct all indexes above.
1517 * TODO: do we need to correct tree in all cases?
1519 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1520 struct ext4_ext_path
*path
)
1522 struct ext4_extent_header
*eh
;
1523 int depth
= ext_depth(inode
);
1524 struct ext4_extent
*ex
;
1528 eh
= path
[depth
].p_hdr
;
1529 ex
= path
[depth
].p_ext
;
1531 if (unlikely(ex
== NULL
|| eh
== NULL
)) {
1532 EXT4_ERROR_INODE(inode
,
1533 "ex %p == NULL or eh %p == NULL", ex
, eh
);
1538 /* there is no tree at all */
1542 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1543 /* we correct tree if first leaf got modified only */
1548 * TODO: we need correction if border is smaller than current one
1551 border
= path
[depth
].p_ext
->ee_block
;
1552 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1555 path
[k
].p_idx
->ei_block
= border
;
1556 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1561 /* change all left-side indexes */
1562 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1564 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1567 path
[k
].p_idx
->ei_block
= border
;
1568 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1577 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1578 struct ext4_extent
*ex2
)
1580 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1583 * Make sure that either both extents are uninitialized, or
1586 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1589 if (ext4_ext_is_uninitialized(ex1
))
1590 max_len
= EXT_UNINIT_MAX_LEN
;
1592 max_len
= EXT_INIT_MAX_LEN
;
1594 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1595 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1597 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1598 le32_to_cpu(ex2
->ee_block
))
1602 * To allow future support for preallocated extents to be added
1603 * as an RO_COMPAT feature, refuse to merge to extents if
1604 * this can result in the top bit of ee_len being set.
1606 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1608 #ifdef AGGRESSIVE_TEST
1609 if (ext1_ee_len
>= 4)
1613 if (ext4_ext_pblock(ex1
) + ext1_ee_len
== ext4_ext_pblock(ex2
))
1619 * This function tries to merge the "ex" extent to the next extent in the tree.
1620 * It always tries to merge towards right. If you want to merge towards
1621 * left, pass "ex - 1" as argument instead of "ex".
1622 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1623 * 1 if they got merged.
1625 static int ext4_ext_try_to_merge_right(struct inode
*inode
,
1626 struct ext4_ext_path
*path
,
1627 struct ext4_extent
*ex
)
1629 struct ext4_extent_header
*eh
;
1630 unsigned int depth
, len
;
1632 int uninitialized
= 0;
1634 depth
= ext_depth(inode
);
1635 BUG_ON(path
[depth
].p_hdr
== NULL
);
1636 eh
= path
[depth
].p_hdr
;
1638 while (ex
< EXT_LAST_EXTENT(eh
)) {
1639 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1641 /* merge with next extent! */
1642 if (ext4_ext_is_uninitialized(ex
))
1644 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1645 + ext4_ext_get_actual_len(ex
+ 1));
1647 ext4_ext_mark_uninitialized(ex
);
1649 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1650 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1651 * sizeof(struct ext4_extent
);
1652 memmove(ex
+ 1, ex
+ 2, len
);
1654 le16_add_cpu(&eh
->eh_entries
, -1);
1656 WARN_ON(eh
->eh_entries
== 0);
1657 if (!eh
->eh_entries
)
1658 EXT4_ERROR_INODE(inode
, "eh->eh_entries = 0!");
1665 * This function does a very simple check to see if we can collapse
1666 * an extent tree with a single extent tree leaf block into the inode.
1668 static void ext4_ext_try_to_merge_up(handle_t
*handle
,
1669 struct inode
*inode
,
1670 struct ext4_ext_path
*path
)
1673 unsigned max_root
= ext4_ext_space_root(inode
, 0);
1676 if ((path
[0].p_depth
!= 1) ||
1677 (le16_to_cpu(path
[0].p_hdr
->eh_entries
) != 1) ||
1678 (le16_to_cpu(path
[1].p_hdr
->eh_entries
) > max_root
))
1682 * We need to modify the block allocation bitmap and the block
1683 * group descriptor to release the extent tree block. If we
1684 * can't get the journal credits, give up.
1686 if (ext4_journal_extend(handle
, 2))
1690 * Copy the extent data up to the inode
1692 blk
= ext4_idx_pblock(path
[0].p_idx
);
1693 s
= le16_to_cpu(path
[1].p_hdr
->eh_entries
) *
1694 sizeof(struct ext4_extent_idx
);
1695 s
+= sizeof(struct ext4_extent_header
);
1697 memcpy(path
[0].p_hdr
, path
[1].p_hdr
, s
);
1698 path
[0].p_depth
= 0;
1699 path
[0].p_ext
= EXT_FIRST_EXTENT(path
[0].p_hdr
) +
1700 (path
[1].p_ext
- EXT_FIRST_EXTENT(path
[1].p_hdr
));
1701 path
[0].p_hdr
->eh_max
= cpu_to_le16(max_root
);
1703 brelse(path
[1].p_bh
);
1704 ext4_free_blocks(handle
, inode
, NULL
, blk
, 1,
1705 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
1709 * This function tries to merge the @ex extent to neighbours in the tree.
1710 * return 1 if merge left else 0.
1712 static void ext4_ext_try_to_merge(handle_t
*handle
,
1713 struct inode
*inode
,
1714 struct ext4_ext_path
*path
,
1715 struct ext4_extent
*ex
) {
1716 struct ext4_extent_header
*eh
;
1720 depth
= ext_depth(inode
);
1721 BUG_ON(path
[depth
].p_hdr
== NULL
);
1722 eh
= path
[depth
].p_hdr
;
1724 if (ex
> EXT_FIRST_EXTENT(eh
))
1725 merge_done
= ext4_ext_try_to_merge_right(inode
, path
, ex
- 1);
1728 (void) ext4_ext_try_to_merge_right(inode
, path
, ex
);
1730 ext4_ext_try_to_merge_up(handle
, inode
, path
);
1734 * check if a portion of the "newext" extent overlaps with an
1737 * If there is an overlap discovered, it updates the length of the newext
1738 * such that there will be no overlap, and then returns 1.
1739 * If there is no overlap found, it returns 0.
1741 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info
*sbi
,
1742 struct inode
*inode
,
1743 struct ext4_extent
*newext
,
1744 struct ext4_ext_path
*path
)
1747 unsigned int depth
, len1
;
1748 unsigned int ret
= 0;
1750 b1
= le32_to_cpu(newext
->ee_block
);
1751 len1
= ext4_ext_get_actual_len(newext
);
1752 depth
= ext_depth(inode
);
1753 if (!path
[depth
].p_ext
)
1755 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1756 b2
&= ~(sbi
->s_cluster_ratio
- 1);
1759 * get the next allocated block if the extent in the path
1760 * is before the requested block(s)
1763 b2
= ext4_ext_next_allocated_block(path
);
1764 if (b2
== EXT_MAX_BLOCKS
)
1766 b2
&= ~(sbi
->s_cluster_ratio
- 1);
1769 /* check for wrap through zero on extent logical start block*/
1770 if (b1
+ len1
< b1
) {
1771 len1
= EXT_MAX_BLOCKS
- b1
;
1772 newext
->ee_len
= cpu_to_le16(len1
);
1776 /* check for overlap */
1777 if (b1
+ len1
> b2
) {
1778 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1786 * ext4_ext_insert_extent:
1787 * tries to merge requsted extent into the existing extent or
1788 * inserts requested extent as new one into the tree,
1789 * creating new leaf in the no-space case.
1791 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1792 struct ext4_ext_path
*path
,
1793 struct ext4_extent
*newext
, int flag
)
1795 struct ext4_extent_header
*eh
;
1796 struct ext4_extent
*ex
, *fex
;
1797 struct ext4_extent
*nearex
; /* nearest extent */
1798 struct ext4_ext_path
*npath
= NULL
;
1799 int depth
, len
, err
;
1801 unsigned uninitialized
= 0;
1804 if (unlikely(ext4_ext_get_actual_len(newext
) == 0)) {
1805 EXT4_ERROR_INODE(inode
, "ext4_ext_get_actual_len(newext) == 0");
1808 depth
= ext_depth(inode
);
1809 ex
= path
[depth
].p_ext
;
1810 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1811 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1815 /* try to insert block into found extent and return */
1816 if (ex
&& !(flag
& EXT4_GET_BLOCKS_PRE_IO
)
1817 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1818 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
1819 ext4_ext_is_uninitialized(newext
),
1820 ext4_ext_get_actual_len(newext
),
1821 le32_to_cpu(ex
->ee_block
),
1822 ext4_ext_is_uninitialized(ex
),
1823 ext4_ext_get_actual_len(ex
),
1824 ext4_ext_pblock(ex
));
1825 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1830 * ext4_can_extents_be_merged should have checked that either
1831 * both extents are uninitialized, or both aren't. Thus we
1832 * need to check only one of them here.
1834 if (ext4_ext_is_uninitialized(ex
))
1836 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1837 + ext4_ext_get_actual_len(newext
));
1839 ext4_ext_mark_uninitialized(ex
);
1840 eh
= path
[depth
].p_hdr
;
1845 depth
= ext_depth(inode
);
1846 eh
= path
[depth
].p_hdr
;
1847 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1850 /* probably next leaf has space for us? */
1851 fex
= EXT_LAST_EXTENT(eh
);
1852 next
= EXT_MAX_BLOCKS
;
1853 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
))
1854 next
= ext4_ext_next_leaf_block(path
);
1855 if (next
!= EXT_MAX_BLOCKS
) {
1856 ext_debug("next leaf block - %u\n", next
);
1857 BUG_ON(npath
!= NULL
);
1858 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1860 return PTR_ERR(npath
);
1861 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1862 eh
= npath
[depth
].p_hdr
;
1863 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1864 ext_debug("next leaf isn't full(%d)\n",
1865 le16_to_cpu(eh
->eh_entries
));
1869 ext_debug("next leaf has no free space(%d,%d)\n",
1870 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1874 * There is no free space in the found leaf.
1875 * We're gonna add a new leaf in the tree.
1877 if (flag
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
)
1878 flags
= EXT4_MB_USE_ROOT_BLOCKS
;
1879 err
= ext4_ext_create_new_leaf(handle
, inode
, flags
, path
, newext
);
1882 depth
= ext_depth(inode
);
1883 eh
= path
[depth
].p_hdr
;
1886 nearex
= path
[depth
].p_ext
;
1888 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1893 /* there is no extent in this leaf, create first one */
1894 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
1895 le32_to_cpu(newext
->ee_block
),
1896 ext4_ext_pblock(newext
),
1897 ext4_ext_is_uninitialized(newext
),
1898 ext4_ext_get_actual_len(newext
));
1899 nearex
= EXT_FIRST_EXTENT(eh
);
1901 if (le32_to_cpu(newext
->ee_block
)
1902 > le32_to_cpu(nearex
->ee_block
)) {
1904 ext_debug("insert %u:%llu:[%d]%d before: "
1906 le32_to_cpu(newext
->ee_block
),
1907 ext4_ext_pblock(newext
),
1908 ext4_ext_is_uninitialized(newext
),
1909 ext4_ext_get_actual_len(newext
),
1914 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1915 ext_debug("insert %u:%llu:[%d]%d after: "
1917 le32_to_cpu(newext
->ee_block
),
1918 ext4_ext_pblock(newext
),
1919 ext4_ext_is_uninitialized(newext
),
1920 ext4_ext_get_actual_len(newext
),
1923 len
= EXT_LAST_EXTENT(eh
) - nearex
+ 1;
1925 ext_debug("insert %u:%llu:[%d]%d: "
1926 "move %d extents from 0x%p to 0x%p\n",
1927 le32_to_cpu(newext
->ee_block
),
1928 ext4_ext_pblock(newext
),
1929 ext4_ext_is_uninitialized(newext
),
1930 ext4_ext_get_actual_len(newext
),
1931 len
, nearex
, nearex
+ 1);
1932 memmove(nearex
+ 1, nearex
,
1933 len
* sizeof(struct ext4_extent
));
1937 le16_add_cpu(&eh
->eh_entries
, 1);
1938 path
[depth
].p_ext
= nearex
;
1939 nearex
->ee_block
= newext
->ee_block
;
1940 ext4_ext_store_pblock(nearex
, ext4_ext_pblock(newext
));
1941 nearex
->ee_len
= newext
->ee_len
;
1944 /* try to merge extents */
1945 if (!(flag
& EXT4_GET_BLOCKS_PRE_IO
))
1946 ext4_ext_try_to_merge(handle
, inode
, path
, nearex
);
1949 /* time to correct all indexes above */
1950 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1954 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
1958 ext4_ext_drop_refs(npath
);
1961 ext4_ext_invalidate_cache(inode
);
1965 static int ext4_fill_fiemap_extents(struct inode
*inode
,
1966 ext4_lblk_t block
, ext4_lblk_t num
,
1967 struct fiemap_extent_info
*fieinfo
)
1969 struct ext4_ext_path
*path
= NULL
;
1970 struct ext4_ext_cache newex
;
1971 struct ext4_extent
*ex
;
1972 ext4_lblk_t next
, next_del
, start
= 0, end
= 0;
1973 ext4_lblk_t last
= block
+ num
;
1974 int exists
, depth
= 0, err
= 0;
1975 unsigned int flags
= 0;
1976 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
1978 while (block
< last
&& block
!= EXT_MAX_BLOCKS
) {
1980 /* find extent for this block */
1981 down_read(&EXT4_I(inode
)->i_data_sem
);
1983 if (path
&& ext_depth(inode
) != depth
) {
1984 /* depth was changed. we have to realloc path */
1989 path
= ext4_ext_find_extent(inode
, block
, path
);
1991 up_read(&EXT4_I(inode
)->i_data_sem
);
1992 err
= PTR_ERR(path
);
1997 depth
= ext_depth(inode
);
1998 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1999 up_read(&EXT4_I(inode
)->i_data_sem
);
2000 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2004 ex
= path
[depth
].p_ext
;
2005 next
= ext4_ext_next_allocated_block(path
);
2006 ext4_ext_drop_refs(path
);
2011 /* there is no extent yet, so try to allocate
2012 * all requested space */
2015 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
2016 /* need to allocate space before found extent */
2018 end
= le32_to_cpu(ex
->ee_block
);
2019 if (block
+ num
< end
)
2021 } else if (block
>= le32_to_cpu(ex
->ee_block
)
2022 + ext4_ext_get_actual_len(ex
)) {
2023 /* need to allocate space after found extent */
2028 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
2030 * some part of requested space is covered
2034 end
= le32_to_cpu(ex
->ee_block
)
2035 + ext4_ext_get_actual_len(ex
);
2036 if (block
+ num
< end
)
2042 BUG_ON(end
<= start
);
2045 newex
.ec_block
= start
;
2046 newex
.ec_len
= end
- start
;
2049 newex
.ec_block
= le32_to_cpu(ex
->ee_block
);
2050 newex
.ec_len
= ext4_ext_get_actual_len(ex
);
2051 newex
.ec_start
= ext4_ext_pblock(ex
);
2052 if (ext4_ext_is_uninitialized(ex
))
2053 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2057 * Find delayed extent and update newex accordingly. We call
2058 * it even in !exists case to find out whether newex is the
2059 * last existing extent or not.
2061 next_del
= ext4_find_delayed_extent(inode
, &newex
);
2062 if (!exists
&& next_del
) {
2064 flags
|= FIEMAP_EXTENT_DELALLOC
;
2066 up_read(&EXT4_I(inode
)->i_data_sem
);
2068 if (unlikely(newex
.ec_len
== 0)) {
2069 EXT4_ERROR_INODE(inode
, "newex.ec_len == 0");
2074 /* This is possible iff next == next_del == EXT_MAX_BLOCKS */
2075 if (next
== next_del
) {
2076 flags
|= FIEMAP_EXTENT_LAST
;
2077 if (unlikely(next_del
!= EXT_MAX_BLOCKS
||
2078 next
!= EXT_MAX_BLOCKS
)) {
2079 EXT4_ERROR_INODE(inode
,
2080 "next extent == %u, next "
2081 "delalloc extent = %u",
2089 err
= fiemap_fill_next_extent(fieinfo
,
2090 (__u64
)newex
.ec_block
<< blksize_bits
,
2091 (__u64
)newex
.ec_start
<< blksize_bits
,
2092 (__u64
)newex
.ec_len
<< blksize_bits
,
2102 block
= newex
.ec_block
+ newex
.ec_len
;
2106 ext4_ext_drop_refs(path
);
2114 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
2115 __u32 len
, ext4_fsblk_t start
)
2117 struct ext4_ext_cache
*cex
;
2119 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
2120 trace_ext4_ext_put_in_cache(inode
, block
, len
, start
);
2121 cex
= &EXT4_I(inode
)->i_cached_extent
;
2122 cex
->ec_block
= block
;
2124 cex
->ec_start
= start
;
2125 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
2129 * ext4_ext_put_gap_in_cache:
2130 * calculate boundaries of the gap that the requested block fits into
2131 * and cache this gap
2134 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
2137 int depth
= ext_depth(inode
);
2140 struct ext4_extent
*ex
;
2142 ex
= path
[depth
].p_ext
;
2144 /* there is no extent yet, so gap is [0;-] */
2146 len
= EXT_MAX_BLOCKS
;
2147 ext_debug("cache gap(whole file):");
2148 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
2150 len
= le32_to_cpu(ex
->ee_block
) - block
;
2151 ext_debug("cache gap(before): %u [%u:%u]",
2153 le32_to_cpu(ex
->ee_block
),
2154 ext4_ext_get_actual_len(ex
));
2155 } else if (block
>= le32_to_cpu(ex
->ee_block
)
2156 + ext4_ext_get_actual_len(ex
)) {
2158 lblock
= le32_to_cpu(ex
->ee_block
)
2159 + ext4_ext_get_actual_len(ex
);
2161 next
= ext4_ext_next_allocated_block(path
);
2162 ext_debug("cache gap(after): [%u:%u] %u",
2163 le32_to_cpu(ex
->ee_block
),
2164 ext4_ext_get_actual_len(ex
),
2166 BUG_ON(next
== lblock
);
2167 len
= next
- lblock
;
2173 ext_debug(" -> %u:%lu\n", lblock
, len
);
2174 ext4_ext_put_in_cache(inode
, lblock
, len
, 0);
2178 * ext4_ext_in_cache()
2179 * Checks to see if the given block is in the cache.
2180 * If it is, the cached extent is stored in the given
2181 * cache extent pointer.
2183 * @inode: The files inode
2184 * @block: The block to look for in the cache
2185 * @ex: Pointer where the cached extent will be stored
2186 * if it contains block
2188 * Return 0 if cache is invalid; 1 if the cache is valid
2191 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
2192 struct ext4_extent
*ex
)
2194 struct ext4_ext_cache
*cex
;
2195 struct ext4_sb_info
*sbi
;
2199 * We borrow i_block_reservation_lock to protect i_cached_extent
2201 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
2202 cex
= &EXT4_I(inode
)->i_cached_extent
;
2203 sbi
= EXT4_SB(inode
->i_sb
);
2205 /* has cache valid data? */
2206 if (cex
->ec_len
== 0)
2209 if (in_range(block
, cex
->ec_block
, cex
->ec_len
)) {
2210 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
2211 ext4_ext_store_pblock(ex
, cex
->ec_start
);
2212 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
2213 ext_debug("%u cached by %u:%u:%llu\n",
2215 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
2219 trace_ext4_ext_in_cache(inode
, block
, ret
);
2220 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
2226 * removes index from the index block.
2228 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
2229 struct ext4_ext_path
*path
)
2234 /* free index block */
2236 leaf
= ext4_idx_pblock(path
->p_idx
);
2237 if (unlikely(path
->p_hdr
->eh_entries
== 0)) {
2238 EXT4_ERROR_INODE(inode
, "path->p_hdr->eh_entries == 0");
2241 err
= ext4_ext_get_access(handle
, inode
, path
);
2245 if (path
->p_idx
!= EXT_LAST_INDEX(path
->p_hdr
)) {
2246 int len
= EXT_LAST_INDEX(path
->p_hdr
) - path
->p_idx
;
2247 len
*= sizeof(struct ext4_extent_idx
);
2248 memmove(path
->p_idx
, path
->p_idx
+ 1, len
);
2251 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
2252 err
= ext4_ext_dirty(handle
, inode
, path
);
2255 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
2256 trace_ext4_ext_rm_idx(inode
, leaf
);
2258 ext4_free_blocks(handle
, inode
, NULL
, leaf
, 1,
2259 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
2264 * ext4_ext_calc_credits_for_single_extent:
2265 * This routine returns max. credits that needed to insert an extent
2266 * to the extent tree.
2267 * When pass the actual path, the caller should calculate credits
2270 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2271 struct ext4_ext_path
*path
)
2274 int depth
= ext_depth(inode
);
2277 /* probably there is space in leaf? */
2278 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2279 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2282 * There are some space in the leaf tree, no
2283 * need to account for leaf block credit
2285 * bitmaps and block group descriptor blocks
2286 * and other metadata blocks still need to be
2289 /* 1 bitmap, 1 block group descriptor */
2290 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2295 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2299 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2301 * if nrblocks are fit in a single extent (chunk flag is 1), then
2302 * in the worse case, each tree level index/leaf need to be changed
2303 * if the tree split due to insert a new extent, then the old tree
2304 * index/leaf need to be updated too
2306 * If the nrblocks are discontiguous, they could cause
2307 * the whole tree split more than once, but this is really rare.
2309 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2312 int depth
= ext_depth(inode
);
2322 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2323 struct ext4_extent
*ex
,
2324 ext4_fsblk_t
*partial_cluster
,
2325 ext4_lblk_t from
, ext4_lblk_t to
)
2327 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2328 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2332 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2333 flags
|= EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
;
2334 else if (ext4_should_journal_data(inode
))
2335 flags
|= EXT4_FREE_BLOCKS_FORGET
;
2338 * For bigalloc file systems, we never free a partial cluster
2339 * at the beginning of the extent. Instead, we make a note
2340 * that we tried freeing the cluster, and check to see if we
2341 * need to free it on a subsequent call to ext4_remove_blocks,
2342 * or at the end of the ext4_truncate() operation.
2344 flags
|= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
;
2346 trace_ext4_remove_blocks(inode
, ex
, from
, to
, *partial_cluster
);
2348 * If we have a partial cluster, and it's different from the
2349 * cluster of the last block, we need to explicitly free the
2350 * partial cluster here.
2352 pblk
= ext4_ext_pblock(ex
) + ee_len
- 1;
2353 if (*partial_cluster
&& (EXT4_B2C(sbi
, pblk
) != *partial_cluster
)) {
2354 ext4_free_blocks(handle
, inode
, NULL
,
2355 EXT4_C2B(sbi
, *partial_cluster
),
2356 sbi
->s_cluster_ratio
, flags
);
2357 *partial_cluster
= 0;
2360 #ifdef EXTENTS_STATS
2362 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2363 spin_lock(&sbi
->s_ext_stats_lock
);
2364 sbi
->s_ext_blocks
+= ee_len
;
2365 sbi
->s_ext_extents
++;
2366 if (ee_len
< sbi
->s_ext_min
)
2367 sbi
->s_ext_min
= ee_len
;
2368 if (ee_len
> sbi
->s_ext_max
)
2369 sbi
->s_ext_max
= ee_len
;
2370 if (ext_depth(inode
) > sbi
->s_depth_max
)
2371 sbi
->s_depth_max
= ext_depth(inode
);
2372 spin_unlock(&sbi
->s_ext_stats_lock
);
2375 if (from
>= le32_to_cpu(ex
->ee_block
)
2376 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2380 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2381 pblk
= ext4_ext_pblock(ex
) + ee_len
- num
;
2382 ext_debug("free last %u blocks starting %llu\n", num
, pblk
);
2383 ext4_free_blocks(handle
, inode
, NULL
, pblk
, num
, flags
);
2385 * If the block range to be freed didn't start at the
2386 * beginning of a cluster, and we removed the entire
2387 * extent, save the partial cluster here, since we
2388 * might need to delete if we determine that the
2389 * truncate operation has removed all of the blocks in
2392 if (pblk
& (sbi
->s_cluster_ratio
- 1) &&
2394 *partial_cluster
= EXT4_B2C(sbi
, pblk
);
2396 *partial_cluster
= 0;
2397 } else if (from
== le32_to_cpu(ex
->ee_block
)
2398 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2404 start
= ext4_ext_pblock(ex
);
2406 ext_debug("free first %u blocks starting %llu\n", num
, start
);
2407 ext4_free_blocks(handle
, inode
, NULL
, start
, num
, flags
);
2410 printk(KERN_INFO
"strange request: removal(2) "
2411 "%u-%u from %u:%u\n",
2412 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2419 * ext4_ext_rm_leaf() Removes the extents associated with the
2420 * blocks appearing between "start" and "end", and splits the extents
2421 * if "start" and "end" appear in the same extent
2423 * @handle: The journal handle
2424 * @inode: The files inode
2425 * @path: The path to the leaf
2426 * @start: The first block to remove
2427 * @end: The last block to remove
2430 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2431 struct ext4_ext_path
*path
, ext4_fsblk_t
*partial_cluster
,
2432 ext4_lblk_t start
, ext4_lblk_t end
)
2434 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2435 int err
= 0, correct_index
= 0;
2436 int depth
= ext_depth(inode
), credits
;
2437 struct ext4_extent_header
*eh
;
2440 ext4_lblk_t ex_ee_block
;
2441 unsigned short ex_ee_len
;
2442 unsigned uninitialized
= 0;
2443 struct ext4_extent
*ex
;
2445 /* the header must be checked already in ext4_ext_remove_space() */
2446 ext_debug("truncate since %u in leaf to %u\n", start
, end
);
2447 if (!path
[depth
].p_hdr
)
2448 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2449 eh
= path
[depth
].p_hdr
;
2450 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2451 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2454 /* find where to start removing */
2455 ex
= EXT_LAST_EXTENT(eh
);
2457 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2458 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2460 trace_ext4_ext_rm_leaf(inode
, start
, ex
, *partial_cluster
);
2462 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2463 ex_ee_block
+ ex_ee_len
> start
) {
2465 if (ext4_ext_is_uninitialized(ex
))
2470 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2471 uninitialized
, ex_ee_len
);
2472 path
[depth
].p_ext
= ex
;
2474 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2475 b
= ex_ee_block
+ex_ee_len
- 1 < end
?
2476 ex_ee_block
+ex_ee_len
- 1 : end
;
2478 ext_debug(" border %u:%u\n", a
, b
);
2480 /* If this extent is beyond the end of the hole, skip it */
2481 if (end
< ex_ee_block
) {
2483 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2484 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2486 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2487 EXT4_ERROR_INODE(inode
,
2488 "can not handle truncate %u:%u "
2490 start
, end
, ex_ee_block
,
2491 ex_ee_block
+ ex_ee_len
- 1);
2494 } else if (a
!= ex_ee_block
) {
2495 /* remove tail of the extent */
2496 num
= a
- ex_ee_block
;
2498 /* remove whole extent: excellent! */
2502 * 3 for leaf, sb, and inode plus 2 (bmap and group
2503 * descriptor) for each block group; assume two block
2504 * groups plus ex_ee_len/blocks_per_block_group for
2507 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2508 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2510 credits
+= (ext_depth(inode
)) + 1;
2512 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2514 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2518 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2522 err
= ext4_remove_blocks(handle
, inode
, ex
, partial_cluster
,
2528 /* this extent is removed; mark slot entirely unused */
2529 ext4_ext_store_pblock(ex
, 0);
2531 ex
->ee_len
= cpu_to_le16(num
);
2533 * Do not mark uninitialized if all the blocks in the
2534 * extent have been removed.
2536 if (uninitialized
&& num
)
2537 ext4_ext_mark_uninitialized(ex
);
2539 * If the extent was completely released,
2540 * we need to remove it from the leaf
2543 if (end
!= EXT_MAX_BLOCKS
- 1) {
2545 * For hole punching, we need to scoot all the
2546 * extents up when an extent is removed so that
2547 * we dont have blank extents in the middle
2549 memmove(ex
, ex
+1, (EXT_LAST_EXTENT(eh
) - ex
) *
2550 sizeof(struct ext4_extent
));
2552 /* Now get rid of the one at the end */
2553 memset(EXT_LAST_EXTENT(eh
), 0,
2554 sizeof(struct ext4_extent
));
2556 le16_add_cpu(&eh
->eh_entries
, -1);
2558 *partial_cluster
= 0;
2560 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2564 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block
, num
,
2565 ext4_ext_pblock(ex
));
2567 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2568 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2571 if (correct_index
&& eh
->eh_entries
)
2572 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2575 * If there is still a entry in the leaf node, check to see if
2576 * it references the partial cluster. This is the only place
2577 * where it could; if it doesn't, we can free the cluster.
2579 if (*partial_cluster
&& ex
>= EXT_FIRST_EXTENT(eh
) &&
2580 (EXT4_B2C(sbi
, ext4_ext_pblock(ex
) + ex_ee_len
- 1) !=
2581 *partial_cluster
)) {
2582 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2584 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2585 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2587 ext4_free_blocks(handle
, inode
, NULL
,
2588 EXT4_C2B(sbi
, *partial_cluster
),
2589 sbi
->s_cluster_ratio
, flags
);
2590 *partial_cluster
= 0;
2593 /* if this leaf is free, then we should
2594 * remove it from index block above */
2595 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2596 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2603 * ext4_ext_more_to_rm:
2604 * returns 1 if current index has to be freed (even partial)
2607 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2609 BUG_ON(path
->p_idx
== NULL
);
2611 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2615 * if truncate on deeper level happened, it wasn't partial,
2616 * so we have to consider current index for truncation
2618 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2623 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
,
2626 struct super_block
*sb
= inode
->i_sb
;
2627 int depth
= ext_depth(inode
);
2628 struct ext4_ext_path
*path
= NULL
;
2629 ext4_fsblk_t partial_cluster
= 0;
2633 ext_debug("truncate since %u to %u\n", start
, end
);
2635 /* probably first extent we're gonna free will be last in block */
2636 handle
= ext4_journal_start(inode
, depth
+ 1);
2638 return PTR_ERR(handle
);
2641 ext4_ext_invalidate_cache(inode
);
2643 trace_ext4_ext_remove_space(inode
, start
, depth
);
2646 * Check if we are removing extents inside the extent tree. If that
2647 * is the case, we are going to punch a hole inside the extent tree
2648 * so we have to check whether we need to split the extent covering
2649 * the last block to remove so we can easily remove the part of it
2650 * in ext4_ext_rm_leaf().
2652 if (end
< EXT_MAX_BLOCKS
- 1) {
2653 struct ext4_extent
*ex
;
2654 ext4_lblk_t ee_block
;
2656 /* find extent for this block */
2657 path
= ext4_ext_find_extent(inode
, end
, NULL
);
2659 ext4_journal_stop(handle
);
2660 return PTR_ERR(path
);
2662 depth
= ext_depth(inode
);
2663 /* Leaf not may not exist only if inode has no blocks at all */
2664 ex
= path
[depth
].p_ext
;
2667 EXT4_ERROR_INODE(inode
,
2668 "path[%d].p_hdr == NULL",
2675 ee_block
= le32_to_cpu(ex
->ee_block
);
2678 * See if the last block is inside the extent, if so split
2679 * the extent at 'end' block so we can easily remove the
2680 * tail of the first part of the split extent in
2681 * ext4_ext_rm_leaf().
2683 if (end
>= ee_block
&&
2684 end
< ee_block
+ ext4_ext_get_actual_len(ex
) - 1) {
2687 if (ext4_ext_is_uninitialized(ex
))
2688 split_flag
= EXT4_EXT_MARK_UNINIT1
|
2689 EXT4_EXT_MARK_UNINIT2
;
2692 * Split the extent in two so that 'end' is the last
2693 * block in the first new extent
2695 err
= ext4_split_extent_at(handle
, inode
, path
,
2696 end
+ 1, split_flag
,
2697 EXT4_GET_BLOCKS_PRE_IO
|
2698 EXT4_GET_BLOCKS_PUNCH_OUT_EXT
);
2705 * We start scanning from right side, freeing all the blocks
2706 * after i_size and walking into the tree depth-wise.
2708 depth
= ext_depth(inode
);
2713 le16_to_cpu(path
[k
].p_hdr
->eh_entries
)+1;
2715 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1),
2718 ext4_journal_stop(handle
);
2721 path
[0].p_depth
= depth
;
2722 path
[0].p_hdr
= ext_inode_hdr(inode
);
2725 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2732 while (i
>= 0 && err
== 0) {
2734 /* this is leaf block */
2735 err
= ext4_ext_rm_leaf(handle
, inode
, path
,
2736 &partial_cluster
, start
,
2738 /* root level has p_bh == NULL, brelse() eats this */
2739 brelse(path
[i
].p_bh
);
2740 path
[i
].p_bh
= NULL
;
2745 /* this is index block */
2746 if (!path
[i
].p_hdr
) {
2747 ext_debug("initialize header\n");
2748 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2751 if (!path
[i
].p_idx
) {
2752 /* this level hasn't been touched yet */
2753 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2754 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2755 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2757 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2759 /* we were already here, see at next index */
2763 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2764 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2766 if (ext4_ext_more_to_rm(path
+ i
)) {
2767 struct buffer_head
*bh
;
2768 /* go to the next level */
2769 ext_debug("move to level %d (block %llu)\n",
2770 i
+ 1, ext4_idx_pblock(path
[i
].p_idx
));
2771 memset(path
+ i
+ 1, 0, sizeof(*path
));
2772 bh
= sb_bread(sb
, ext4_idx_pblock(path
[i
].p_idx
));
2774 /* should we reset i_size? */
2778 if (WARN_ON(i
+ 1 > depth
)) {
2782 if (ext4_ext_check_block(inode
, ext_block_hdr(bh
),
2783 depth
- i
- 1, bh
)) {
2787 path
[i
+ 1].p_bh
= bh
;
2789 /* save actual number of indexes since this
2790 * number is changed at the next iteration */
2791 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2794 /* we finished processing this index, go up */
2795 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2796 /* index is empty, remove it;
2797 * handle must be already prepared by the
2798 * truncatei_leaf() */
2799 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2801 /* root level has p_bh == NULL, brelse() eats this */
2802 brelse(path
[i
].p_bh
);
2803 path
[i
].p_bh
= NULL
;
2805 ext_debug("return to level %d\n", i
);
2809 trace_ext4_ext_remove_space_done(inode
, start
, depth
, partial_cluster
,
2810 path
->p_hdr
->eh_entries
);
2812 /* If we still have something in the partial cluster and we have removed
2813 * even the first extent, then we should free the blocks in the partial
2814 * cluster as well. */
2815 if (partial_cluster
&& path
->p_hdr
->eh_entries
== 0) {
2816 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2818 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2819 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2821 ext4_free_blocks(handle
, inode
, NULL
,
2822 EXT4_C2B(EXT4_SB(sb
), partial_cluster
),
2823 EXT4_SB(sb
)->s_cluster_ratio
, flags
);
2824 partial_cluster
= 0;
2827 /* TODO: flexible tree reduction should be here */
2828 if (path
->p_hdr
->eh_entries
== 0) {
2830 * truncate to zero freed all the tree,
2831 * so we need to correct eh_depth
2833 err
= ext4_ext_get_access(handle
, inode
, path
);
2835 ext_inode_hdr(inode
)->eh_depth
= 0;
2836 ext_inode_hdr(inode
)->eh_max
=
2837 cpu_to_le16(ext4_ext_space_root(inode
, 0));
2838 err
= ext4_ext_dirty(handle
, inode
, path
);
2842 ext4_ext_drop_refs(path
);
2844 if (err
== -EAGAIN
) {
2848 ext4_journal_stop(handle
);
2854 * called at mount time
2856 void ext4_ext_init(struct super_block
*sb
)
2859 * possible initialization would be here
2862 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2863 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2864 printk(KERN_INFO
"EXT4-fs: file extents enabled"
2865 #ifdef AGGRESSIVE_TEST
2866 ", aggressive tests"
2868 #ifdef CHECK_BINSEARCH
2871 #ifdef EXTENTS_STATS
2876 #ifdef EXTENTS_STATS
2877 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2878 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2879 EXT4_SB(sb
)->s_ext_max
= 0;
2885 * called at umount time
2887 void ext4_ext_release(struct super_block
*sb
)
2889 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2892 #ifdef EXTENTS_STATS
2893 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2894 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2895 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2896 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2897 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2898 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2899 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2904 /* FIXME!! we need to try to merge to left or right after zero-out */
2905 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2907 ext4_fsblk_t ee_pblock
;
2908 unsigned int ee_len
;
2911 ee_len
= ext4_ext_get_actual_len(ex
);
2912 ee_pblock
= ext4_ext_pblock(ex
);
2914 ret
= sb_issue_zeroout(inode
->i_sb
, ee_pblock
, ee_len
, GFP_NOFS
);
2922 * ext4_split_extent_at() splits an extent at given block.
2924 * @handle: the journal handle
2925 * @inode: the file inode
2926 * @path: the path to the extent
2927 * @split: the logical block where the extent is splitted.
2928 * @split_flags: indicates if the extent could be zeroout if split fails, and
2929 * the states(init or uninit) of new extents.
2930 * @flags: flags used to insert new extent to extent tree.
2933 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2934 * of which are deterimined by split_flag.
2936 * There are two cases:
2937 * a> the extent are splitted into two extent.
2938 * b> split is not needed, and just mark the extent.
2940 * return 0 on success.
2942 static int ext4_split_extent_at(handle_t
*handle
,
2943 struct inode
*inode
,
2944 struct ext4_ext_path
*path
,
2949 ext4_fsblk_t newblock
;
2950 ext4_lblk_t ee_block
;
2951 struct ext4_extent
*ex
, newex
, orig_ex
;
2952 struct ext4_extent
*ex2
= NULL
;
2953 unsigned int ee_len
, depth
;
2956 BUG_ON((split_flag
& (EXT4_EXT_DATA_VALID1
| EXT4_EXT_DATA_VALID2
)) ==
2957 (EXT4_EXT_DATA_VALID1
| EXT4_EXT_DATA_VALID2
));
2959 ext_debug("ext4_split_extents_at: inode %lu, logical"
2960 "block %llu\n", inode
->i_ino
, (unsigned long long)split
);
2962 ext4_ext_show_leaf(inode
, path
);
2964 depth
= ext_depth(inode
);
2965 ex
= path
[depth
].p_ext
;
2966 ee_block
= le32_to_cpu(ex
->ee_block
);
2967 ee_len
= ext4_ext_get_actual_len(ex
);
2968 newblock
= split
- ee_block
+ ext4_ext_pblock(ex
);
2970 BUG_ON(split
< ee_block
|| split
>= (ee_block
+ ee_len
));
2972 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2976 if (split
== ee_block
) {
2978 * case b: block @split is the block that the extent begins with
2979 * then we just change the state of the extent, and splitting
2982 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2983 ext4_ext_mark_uninitialized(ex
);
2985 ext4_ext_mark_initialized(ex
);
2987 if (!(flags
& EXT4_GET_BLOCKS_PRE_IO
))
2988 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
2990 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
2995 memcpy(&orig_ex
, ex
, sizeof(orig_ex
));
2996 ex
->ee_len
= cpu_to_le16(split
- ee_block
);
2997 if (split_flag
& EXT4_EXT_MARK_UNINIT1
)
2998 ext4_ext_mark_uninitialized(ex
);
3001 * path may lead to new leaf, not to original leaf any more
3002 * after ext4_ext_insert_extent() returns,
3004 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3006 goto fix_extent_len
;
3009 ex2
->ee_block
= cpu_to_le32(split
);
3010 ex2
->ee_len
= cpu_to_le16(ee_len
- (split
- ee_block
));
3011 ext4_ext_store_pblock(ex2
, newblock
);
3012 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
3013 ext4_ext_mark_uninitialized(ex2
);
3015 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3016 if (err
== -ENOSPC
&& (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
3017 if (split_flag
& (EXT4_EXT_DATA_VALID1
|EXT4_EXT_DATA_VALID2
)) {
3018 if (split_flag
& EXT4_EXT_DATA_VALID1
)
3019 err
= ext4_ext_zeroout(inode
, ex2
);
3021 err
= ext4_ext_zeroout(inode
, ex
);
3023 err
= ext4_ext_zeroout(inode
, &orig_ex
);
3026 goto fix_extent_len
;
3027 /* update the extent length and mark as initialized */
3028 ex
->ee_len
= cpu_to_le16(ee_len
);
3029 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3030 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3033 goto fix_extent_len
;
3036 ext4_ext_show_leaf(inode
, path
);
3040 ex
->ee_len
= orig_ex
.ee_len
;
3041 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3046 * ext4_split_extents() splits an extent and mark extent which is covered
3047 * by @map as split_flags indicates
3049 * It may result in splitting the extent into multiple extents (upto three)
3050 * There are three possibilities:
3051 * a> There is no split required
3052 * b> Splits in two extents: Split is happening at either end of the extent
3053 * c> Splits in three extents: Somone is splitting in middle of the extent
3056 static int ext4_split_extent(handle_t
*handle
,
3057 struct inode
*inode
,
3058 struct ext4_ext_path
*path
,
3059 struct ext4_map_blocks
*map
,
3063 ext4_lblk_t ee_block
;
3064 struct ext4_extent
*ex
;
3065 unsigned int ee_len
, depth
;
3068 int split_flag1
, flags1
;
3070 depth
= ext_depth(inode
);
3071 ex
= path
[depth
].p_ext
;
3072 ee_block
= le32_to_cpu(ex
->ee_block
);
3073 ee_len
= ext4_ext_get_actual_len(ex
);
3074 uninitialized
= ext4_ext_is_uninitialized(ex
);
3076 if (map
->m_lblk
+ map
->m_len
< ee_block
+ ee_len
) {
3077 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
;
3078 flags1
= flags
| EXT4_GET_BLOCKS_PRE_IO
;
3080 split_flag1
|= EXT4_EXT_MARK_UNINIT1
|
3081 EXT4_EXT_MARK_UNINIT2
;
3082 if (split_flag
& EXT4_EXT_DATA_VALID2
)
3083 split_flag1
|= EXT4_EXT_DATA_VALID1
;
3084 err
= ext4_split_extent_at(handle
, inode
, path
,
3085 map
->m_lblk
+ map
->m_len
, split_flag1
, flags1
);
3090 ext4_ext_drop_refs(path
);
3091 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, path
);
3093 return PTR_ERR(path
);
3095 if (map
->m_lblk
>= ee_block
) {
3096 split_flag1
= split_flag
& (EXT4_EXT_MAY_ZEROOUT
|
3097 EXT4_EXT_DATA_VALID2
);
3099 split_flag1
|= EXT4_EXT_MARK_UNINIT1
;
3100 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
3101 split_flag1
|= EXT4_EXT_MARK_UNINIT2
;
3102 err
= ext4_split_extent_at(handle
, inode
, path
,
3103 map
->m_lblk
, split_flag1
, flags
);
3108 ext4_ext_show_leaf(inode
, path
);
3110 return err
? err
: map
->m_len
;
3114 * This function is called by ext4_ext_map_blocks() if someone tries to write
3115 * to an uninitialized extent. It may result in splitting the uninitialized
3116 * extent into multiple extents (up to three - one initialized and two
3118 * There are three possibilities:
3119 * a> There is no split required: Entire extent should be initialized
3120 * b> Splits in two extents: Write is happening at either end of the extent
3121 * c> Splits in three extents: Somone is writing in middle of the extent
3124 * - The extent pointed to by 'path' is uninitialized.
3125 * - The extent pointed to by 'path' contains a superset
3126 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3128 * Post-conditions on success:
3129 * - the returned value is the number of blocks beyond map->l_lblk
3130 * that are allocated and initialized.
3131 * It is guaranteed to be >= map->m_len.
3133 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
3134 struct inode
*inode
,
3135 struct ext4_map_blocks
*map
,
3136 struct ext4_ext_path
*path
)
3138 struct ext4_sb_info
*sbi
;
3139 struct ext4_extent_header
*eh
;
3140 struct ext4_map_blocks split_map
;
3141 struct ext4_extent zero_ex
;
3142 struct ext4_extent
*ex
;
3143 ext4_lblk_t ee_block
, eof_block
;
3144 unsigned int ee_len
, depth
;
3145 int allocated
, max_zeroout
= 0;
3149 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3150 "block %llu, max_blocks %u\n", inode
->i_ino
,
3151 (unsigned long long)map
->m_lblk
, map
->m_len
);
3153 sbi
= EXT4_SB(inode
->i_sb
);
3154 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
3155 inode
->i_sb
->s_blocksize_bits
;
3156 if (eof_block
< map
->m_lblk
+ map
->m_len
)
3157 eof_block
= map
->m_lblk
+ map
->m_len
;
3159 depth
= ext_depth(inode
);
3160 eh
= path
[depth
].p_hdr
;
3161 ex
= path
[depth
].p_ext
;
3162 ee_block
= le32_to_cpu(ex
->ee_block
);
3163 ee_len
= ext4_ext_get_actual_len(ex
);
3164 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3166 trace_ext4_ext_convert_to_initialized_enter(inode
, map
, ex
);
3168 /* Pre-conditions */
3169 BUG_ON(!ext4_ext_is_uninitialized(ex
));
3170 BUG_ON(!in_range(map
->m_lblk
, ee_block
, ee_len
));
3173 * Attempt to transfer newly initialized blocks from the currently
3174 * uninitialized extent to its left neighbor. This is much cheaper
3175 * than an insertion followed by a merge as those involve costly
3176 * memmove() calls. This is the common case in steady state for
3177 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3180 * Limitations of the current logic:
3181 * - L1: we only deal with writes at the start of the extent.
3182 * The approach could be extended to writes at the end
3183 * of the extent but this scenario was deemed less common.
3184 * - L2: we do not deal with writes covering the whole extent.
3185 * This would require removing the extent if the transfer
3187 * - L3: we only attempt to merge with an extent stored in the
3188 * same extent tree node.
3190 if ((map
->m_lblk
== ee_block
) && /*L1*/
3191 (map
->m_len
< ee_len
) && /*L2*/
3192 (ex
> EXT_FIRST_EXTENT(eh
))) { /*L3*/
3193 struct ext4_extent
*prev_ex
;
3194 ext4_lblk_t prev_lblk
;
3195 ext4_fsblk_t prev_pblk
, ee_pblk
;
3196 unsigned int prev_len
, write_len
;
3199 prev_lblk
= le32_to_cpu(prev_ex
->ee_block
);
3200 prev_len
= ext4_ext_get_actual_len(prev_ex
);
3201 prev_pblk
= ext4_ext_pblock(prev_ex
);
3202 ee_pblk
= ext4_ext_pblock(ex
);
3203 write_len
= map
->m_len
;
3206 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3207 * upon those conditions:
3208 * - C1: prev_ex is initialized,
3209 * - C2: prev_ex is logically abutting ex,
3210 * - C3: prev_ex is physically abutting ex,
3211 * - C4: prev_ex can receive the additional blocks without
3212 * overflowing the (initialized) length limit.
3214 if ((!ext4_ext_is_uninitialized(prev_ex
)) && /*C1*/
3215 ((prev_lblk
+ prev_len
) == ee_block
) && /*C2*/
3216 ((prev_pblk
+ prev_len
) == ee_pblk
) && /*C3*/
3217 (prev_len
< (EXT_INIT_MAX_LEN
- write_len
))) { /*C4*/
3218 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3222 trace_ext4_ext_convert_to_initialized_fastpath(inode
,
3225 /* Shift the start of ex by 'write_len' blocks */
3226 ex
->ee_block
= cpu_to_le32(ee_block
+ write_len
);
3227 ext4_ext_store_pblock(ex
, ee_pblk
+ write_len
);
3228 ex
->ee_len
= cpu_to_le16(ee_len
- write_len
);
3229 ext4_ext_mark_uninitialized(ex
); /* Restore the flag */
3231 /* Extend prev_ex by 'write_len' blocks */
3232 prev_ex
->ee_len
= cpu_to_le16(prev_len
+ write_len
);
3234 /* Mark the block containing both extents as dirty */
3235 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3237 /* Update path to point to the right extent */
3238 path
[depth
].p_ext
= prev_ex
;
3240 /* Result: number of initialized blocks past m_lblk */
3241 allocated
= write_len
;
3246 WARN_ON(map
->m_lblk
< ee_block
);
3248 * It is safe to convert extent to initialized via explicit
3249 * zeroout only if extent is fully insde i_size or new_size.
3251 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
3253 if (EXT4_EXT_MAY_ZEROOUT
& split_flag
)
3254 max_zeroout
= sbi
->s_extent_max_zeroout_kb
>>
3255 inode
->i_sb
->s_blocksize_bits
;
3257 /* If extent is less than s_max_zeroout_kb, zeroout directly */
3258 if (max_zeroout
&& (ee_len
<= max_zeroout
)) {
3259 err
= ext4_ext_zeroout(inode
, ex
);
3263 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3266 ext4_ext_mark_initialized(ex
);
3267 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3268 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3274 * 1. split the extent into three extents.
3275 * 2. split the extent into two extents, zeroout the first half.
3276 * 3. split the extent into two extents, zeroout the second half.
3277 * 4. split the extent into two extents with out zeroout.
3279 split_map
.m_lblk
= map
->m_lblk
;
3280 split_map
.m_len
= map
->m_len
;
3282 if (max_zeroout
&& (allocated
> map
->m_len
)) {
3283 if (allocated
<= max_zeroout
) {
3286 cpu_to_le32(map
->m_lblk
);
3287 zero_ex
.ee_len
= cpu_to_le16(allocated
);
3288 ext4_ext_store_pblock(&zero_ex
,
3289 ext4_ext_pblock(ex
) + map
->m_lblk
- ee_block
);
3290 err
= ext4_ext_zeroout(inode
, &zero_ex
);
3293 split_map
.m_lblk
= map
->m_lblk
;
3294 split_map
.m_len
= allocated
;
3295 } else if (map
->m_lblk
- ee_block
+ map
->m_len
< max_zeroout
) {
3297 if (map
->m_lblk
!= ee_block
) {
3298 zero_ex
.ee_block
= ex
->ee_block
;
3299 zero_ex
.ee_len
= cpu_to_le16(map
->m_lblk
-
3301 ext4_ext_store_pblock(&zero_ex
,
3302 ext4_ext_pblock(ex
));
3303 err
= ext4_ext_zeroout(inode
, &zero_ex
);
3308 split_map
.m_lblk
= ee_block
;
3309 split_map
.m_len
= map
->m_lblk
- ee_block
+ map
->m_len
;
3310 allocated
= map
->m_len
;
3314 allocated
= ext4_split_extent(handle
, inode
, path
,
3315 &split_map
, split_flag
, 0);
3320 return err
? err
: allocated
;
3324 * This function is called by ext4_ext_map_blocks() from
3325 * ext4_get_blocks_dio_write() when DIO to write
3326 * to an uninitialized extent.
3328 * Writing to an uninitialized extent may result in splitting the uninitialized
3329 * extent into multiple initialized/uninitialized extents (up to three)
3330 * There are three possibilities:
3331 * a> There is no split required: Entire extent should be uninitialized
3332 * b> Splits in two extents: Write is happening at either end of the extent
3333 * c> Splits in three extents: Somone is writing in middle of the extent
3335 * One of more index blocks maybe needed if the extent tree grow after
3336 * the uninitialized extent split. To prevent ENOSPC occur at the IO
3337 * complete, we need to split the uninitialized extent before DIO submit
3338 * the IO. The uninitialized extent called at this time will be split
3339 * into three uninitialized extent(at most). After IO complete, the part
3340 * being filled will be convert to initialized by the end_io callback function
3341 * via ext4_convert_unwritten_extents().
3343 * Returns the size of uninitialized extent to be written on success.
3345 static int ext4_split_unwritten_extents(handle_t
*handle
,
3346 struct inode
*inode
,
3347 struct ext4_map_blocks
*map
,
3348 struct ext4_ext_path
*path
,
3351 ext4_lblk_t eof_block
;
3352 ext4_lblk_t ee_block
;
3353 struct ext4_extent
*ex
;
3354 unsigned int ee_len
;
3355 int split_flag
= 0, depth
;
3357 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3358 "block %llu, max_blocks %u\n", inode
->i_ino
,
3359 (unsigned long long)map
->m_lblk
, map
->m_len
);
3361 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
3362 inode
->i_sb
->s_blocksize_bits
;
3363 if (eof_block
< map
->m_lblk
+ map
->m_len
)
3364 eof_block
= map
->m_lblk
+ map
->m_len
;
3366 * It is safe to convert extent to initialized via explicit
3367 * zeroout only if extent is fully insde i_size or new_size.
3369 depth
= ext_depth(inode
);
3370 ex
= path
[depth
].p_ext
;
3371 ee_block
= le32_to_cpu(ex
->ee_block
);
3372 ee_len
= ext4_ext_get_actual_len(ex
);
3374 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
3375 split_flag
|= EXT4_EXT_MARK_UNINIT2
;
3376 if (flags
& EXT4_GET_BLOCKS_CONVERT
)
3377 split_flag
|= EXT4_EXT_DATA_VALID2
;
3378 flags
|= EXT4_GET_BLOCKS_PRE_IO
;
3379 return ext4_split_extent(handle
, inode
, path
, map
, split_flag
, flags
);
3382 static int ext4_convert_unwritten_extents_endio(handle_t
*handle
,
3383 struct inode
*inode
,
3384 struct ext4_map_blocks
*map
,
3385 struct ext4_ext_path
*path
)
3387 struct ext4_extent
*ex
;
3388 ext4_lblk_t ee_block
;
3389 unsigned int ee_len
;
3393 depth
= ext_depth(inode
);
3394 ex
= path
[depth
].p_ext
;
3395 ee_block
= le32_to_cpu(ex
->ee_block
);
3396 ee_len
= ext4_ext_get_actual_len(ex
);
3398 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3399 "block %llu, max_blocks %u\n", inode
->i_ino
,
3400 (unsigned long long)ee_block
, ee_len
);
3402 /* If extent is larger than requested then split is required */
3403 if (ee_block
!= map
->m_lblk
|| ee_len
> map
->m_len
) {
3404 err
= ext4_split_unwritten_extents(handle
, inode
, map
, path
,
3405 EXT4_GET_BLOCKS_CONVERT
);
3408 ext4_ext_drop_refs(path
);
3409 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, path
);
3411 err
= PTR_ERR(path
);
3414 depth
= ext_depth(inode
);
3415 ex
= path
[depth
].p_ext
;
3418 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3421 /* first mark the extent as initialized */
3422 ext4_ext_mark_initialized(ex
);
3424 /* note: ext4_ext_correct_indexes() isn't needed here because
3425 * borders are not changed
3427 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3429 /* Mark modified extent as dirty */
3430 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3432 ext4_ext_show_leaf(inode
, path
);
3436 static void unmap_underlying_metadata_blocks(struct block_device
*bdev
,
3437 sector_t block
, int count
)
3440 for (i
= 0; i
< count
; i
++)
3441 unmap_underlying_metadata(bdev
, block
+ i
);
3445 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3447 static int check_eofblocks_fl(handle_t
*handle
, struct inode
*inode
,
3449 struct ext4_ext_path
*path
,
3453 struct ext4_extent_header
*eh
;
3454 struct ext4_extent
*last_ex
;
3456 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))
3459 depth
= ext_depth(inode
);
3460 eh
= path
[depth
].p_hdr
;
3463 * We're going to remove EOFBLOCKS_FL entirely in future so we
3464 * do not care for this case anymore. Simply remove the flag
3465 * if there are no extents.
3467 if (unlikely(!eh
->eh_entries
))
3469 last_ex
= EXT_LAST_EXTENT(eh
);
3471 * We should clear the EOFBLOCKS_FL flag if we are writing the
3472 * last block in the last extent in the file. We test this by
3473 * first checking to see if the caller to
3474 * ext4_ext_get_blocks() was interested in the last block (or
3475 * a block beyond the last block) in the current extent. If
3476 * this turns out to be false, we can bail out from this
3477 * function immediately.
3479 if (lblk
+ len
< le32_to_cpu(last_ex
->ee_block
) +
3480 ext4_ext_get_actual_len(last_ex
))
3483 * If the caller does appear to be planning to write at or
3484 * beyond the end of the current extent, we then test to see
3485 * if the current extent is the last extent in the file, by
3486 * checking to make sure it was reached via the rightmost node
3487 * at each level of the tree.
3489 for (i
= depth
-1; i
>= 0; i
--)
3490 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3493 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3494 return ext4_mark_inode_dirty(handle
, inode
);
3498 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3500 * Return 1 if there is a delalloc block in the range, otherwise 0.
3502 static int ext4_find_delalloc_range(struct inode
*inode
,
3503 ext4_lblk_t lblk_start
,
3504 ext4_lblk_t lblk_end
)
3506 struct extent_status es
;
3508 es
.start
= lblk_start
;
3509 ext4_es_find_extent(inode
, &es
);
3511 return 0; /* there is no delay extent in this tree */
3512 else if (es
.start
<= lblk_start
&& lblk_start
< es
.start
+ es
.len
)
3514 else if (lblk_start
<= es
.start
&& es
.start
<= lblk_end
)
3520 int ext4_find_delalloc_cluster(struct inode
*inode
, ext4_lblk_t lblk
)
3522 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3523 ext4_lblk_t lblk_start
, lblk_end
;
3524 lblk_start
= lblk
& (~(sbi
->s_cluster_ratio
- 1));
3525 lblk_end
= lblk_start
+ sbi
->s_cluster_ratio
- 1;
3527 return ext4_find_delalloc_range(inode
, lblk_start
, lblk_end
);
3531 * Determines how many complete clusters (out of those specified by the 'map')
3532 * are under delalloc and were reserved quota for.
3533 * This function is called when we are writing out the blocks that were
3534 * originally written with their allocation delayed, but then the space was
3535 * allocated using fallocate() before the delayed allocation could be resolved.
3536 * The cases to look for are:
3537 * ('=' indicated delayed allocated blocks
3538 * '-' indicates non-delayed allocated blocks)
3539 * (a) partial clusters towards beginning and/or end outside of allocated range
3540 * are not delalloc'ed.
3542 * |----c---=|====c====|====c====|===-c----|
3543 * |++++++ allocated ++++++|
3544 * ==> 4 complete clusters in above example
3546 * (b) partial cluster (outside of allocated range) towards either end is
3547 * marked for delayed allocation. In this case, we will exclude that
3550 * |----====c========|========c========|
3551 * |++++++ allocated ++++++|
3552 * ==> 1 complete clusters in above example
3555 * |================c================|
3556 * |++++++ allocated ++++++|
3557 * ==> 0 complete clusters in above example
3559 * The ext4_da_update_reserve_space will be called only if we
3560 * determine here that there were some "entire" clusters that span
3561 * this 'allocated' range.
3562 * In the non-bigalloc case, this function will just end up returning num_blks
3563 * without ever calling ext4_find_delalloc_range.
3566 get_reserved_cluster_alloc(struct inode
*inode
, ext4_lblk_t lblk_start
,
3567 unsigned int num_blks
)
3569 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3570 ext4_lblk_t alloc_cluster_start
, alloc_cluster_end
;
3571 ext4_lblk_t lblk_from
, lblk_to
, c_offset
;
3572 unsigned int allocated_clusters
= 0;
3574 alloc_cluster_start
= EXT4_B2C(sbi
, lblk_start
);
3575 alloc_cluster_end
= EXT4_B2C(sbi
, lblk_start
+ num_blks
- 1);
3577 /* max possible clusters for this allocation */
3578 allocated_clusters
= alloc_cluster_end
- alloc_cluster_start
+ 1;
3580 trace_ext4_get_reserved_cluster_alloc(inode
, lblk_start
, num_blks
);
3582 /* Check towards left side */
3583 c_offset
= lblk_start
& (sbi
->s_cluster_ratio
- 1);
3585 lblk_from
= lblk_start
& (~(sbi
->s_cluster_ratio
- 1));
3586 lblk_to
= lblk_from
+ c_offset
- 1;
3588 if (ext4_find_delalloc_range(inode
, lblk_from
, lblk_to
))
3589 allocated_clusters
--;
3592 /* Now check towards right. */
3593 c_offset
= (lblk_start
+ num_blks
) & (sbi
->s_cluster_ratio
- 1);
3594 if (allocated_clusters
&& c_offset
) {
3595 lblk_from
= lblk_start
+ num_blks
;
3596 lblk_to
= lblk_from
+ (sbi
->s_cluster_ratio
- c_offset
) - 1;
3598 if (ext4_find_delalloc_range(inode
, lblk_from
, lblk_to
))
3599 allocated_clusters
--;
3602 return allocated_clusters
;
3606 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3607 struct ext4_map_blocks
*map
,
3608 struct ext4_ext_path
*path
, int flags
,
3609 unsigned int allocated
, ext4_fsblk_t newblock
)
3613 ext4_io_end_t
*io
= ext4_inode_aio(inode
);
3615 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3616 "block %llu, max_blocks %u, flags %x, allocated %u\n",
3617 inode
->i_ino
, (unsigned long long)map
->m_lblk
, map
->m_len
,
3619 ext4_ext_show_leaf(inode
, path
);
3621 trace_ext4_ext_handle_uninitialized_extents(inode
, map
, flags
,
3622 allocated
, newblock
);
3624 /* get_block() before submit the IO, split the extent */
3625 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3626 ret
= ext4_split_unwritten_extents(handle
, inode
, map
,
3631 * Flag the inode(non aio case) or end_io struct (aio case)
3632 * that this IO needs to conversion to written when IO is
3636 ext4_set_io_unwritten_flag(inode
, io
);
3638 ext4_set_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3639 if (ext4_should_dioread_nolock(inode
))
3640 map
->m_flags
|= EXT4_MAP_UNINIT
;
3643 /* IO end_io complete, convert the filled extent to written */
3644 if ((flags
& EXT4_GET_BLOCKS_CONVERT
)) {
3645 ret
= ext4_convert_unwritten_extents_endio(handle
, inode
, map
,
3648 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3649 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
3655 /* buffered IO case */
3657 * repeat fallocate creation request
3658 * we already have an unwritten extent
3660 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3663 /* buffered READ or buffered write_begin() lookup */
3664 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3666 * We have blocks reserved already. We
3667 * return allocated blocks so that delalloc
3668 * won't do block reservation for us. But
3669 * the buffer head will be unmapped so that
3670 * a read from the block returns 0s.
3672 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
3676 /* buffered write, writepage time, convert*/
3677 ret
= ext4_ext_convert_to_initialized(handle
, inode
, map
, path
);
3679 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3686 map
->m_flags
|= EXT4_MAP_NEW
;
3688 * if we allocated more blocks than requested
3689 * we need to make sure we unmap the extra block
3690 * allocated. The actual needed block will get
3691 * unmapped later when we find the buffer_head marked
3694 if (allocated
> map
->m_len
) {
3695 unmap_underlying_metadata_blocks(inode
->i_sb
->s_bdev
,
3696 newblock
+ map
->m_len
,
3697 allocated
- map
->m_len
);
3698 allocated
= map
->m_len
;
3702 * If we have done fallocate with the offset that is already
3703 * delayed allocated, we would have block reservation
3704 * and quota reservation done in the delayed write path.
3705 * But fallocate would have already updated quota and block
3706 * count for this offset. So cancel these reservation
3708 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) {
3709 unsigned int reserved_clusters
;
3710 reserved_clusters
= get_reserved_cluster_alloc(inode
,
3711 map
->m_lblk
, map
->m_len
);
3712 if (reserved_clusters
)
3713 ext4_da_update_reserve_space(inode
,
3719 map
->m_flags
|= EXT4_MAP_MAPPED
;
3720 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0) {
3721 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
,
3727 if (allocated
> map
->m_len
)
3728 allocated
= map
->m_len
;
3729 ext4_ext_show_leaf(inode
, path
);
3730 map
->m_pblk
= newblock
;
3731 map
->m_len
= allocated
;
3734 ext4_ext_drop_refs(path
);
3737 return err
? err
: allocated
;
3741 * get_implied_cluster_alloc - check to see if the requested
3742 * allocation (in the map structure) overlaps with a cluster already
3743 * allocated in an extent.
3744 * @sb The filesystem superblock structure
3745 * @map The requested lblk->pblk mapping
3746 * @ex The extent structure which might contain an implied
3747 * cluster allocation
3749 * This function is called by ext4_ext_map_blocks() after we failed to
3750 * find blocks that were already in the inode's extent tree. Hence,
3751 * we know that the beginning of the requested region cannot overlap
3752 * the extent from the inode's extent tree. There are three cases we
3753 * want to catch. The first is this case:
3755 * |--- cluster # N--|
3756 * |--- extent ---| |---- requested region ---|
3759 * The second case that we need to test for is this one:
3761 * |--------- cluster # N ----------------|
3762 * |--- requested region --| |------- extent ----|
3763 * |=======================|
3765 * The third case is when the requested region lies between two extents
3766 * within the same cluster:
3767 * |------------- cluster # N-------------|
3768 * |----- ex -----| |---- ex_right ----|
3769 * |------ requested region ------|
3770 * |================|
3772 * In each of the above cases, we need to set the map->m_pblk and
3773 * map->m_len so it corresponds to the return the extent labelled as
3774 * "|====|" from cluster #N, since it is already in use for data in
3775 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3776 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3777 * as a new "allocated" block region. Otherwise, we will return 0 and
3778 * ext4_ext_map_blocks() will then allocate one or more new clusters
3779 * by calling ext4_mb_new_blocks().
3781 static int get_implied_cluster_alloc(struct super_block
*sb
,
3782 struct ext4_map_blocks
*map
,
3783 struct ext4_extent
*ex
,
3784 struct ext4_ext_path
*path
)
3786 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3787 ext4_lblk_t c_offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
-1);
3788 ext4_lblk_t ex_cluster_start
, ex_cluster_end
;
3789 ext4_lblk_t rr_cluster_start
;
3790 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3791 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3792 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
3794 /* The extent passed in that we are trying to match */
3795 ex_cluster_start
= EXT4_B2C(sbi
, ee_block
);
3796 ex_cluster_end
= EXT4_B2C(sbi
, ee_block
+ ee_len
- 1);
3798 /* The requested region passed into ext4_map_blocks() */
3799 rr_cluster_start
= EXT4_B2C(sbi
, map
->m_lblk
);
3801 if ((rr_cluster_start
== ex_cluster_end
) ||
3802 (rr_cluster_start
== ex_cluster_start
)) {
3803 if (rr_cluster_start
== ex_cluster_end
)
3804 ee_start
+= ee_len
- 1;
3805 map
->m_pblk
= (ee_start
& ~(sbi
->s_cluster_ratio
- 1)) +
3807 map
->m_len
= min(map
->m_len
,
3808 (unsigned) sbi
->s_cluster_ratio
- c_offset
);
3810 * Check for and handle this case:
3812 * |--------- cluster # N-------------|
3813 * |------- extent ----|
3814 * |--- requested region ---|
3818 if (map
->m_lblk
< ee_block
)
3819 map
->m_len
= min(map
->m_len
, ee_block
- map
->m_lblk
);
3822 * Check for the case where there is already another allocated
3823 * block to the right of 'ex' but before the end of the cluster.
3825 * |------------- cluster # N-------------|
3826 * |----- ex -----| |---- ex_right ----|
3827 * |------ requested region ------|
3828 * |================|
3830 if (map
->m_lblk
> ee_block
) {
3831 ext4_lblk_t next
= ext4_ext_next_allocated_block(path
);
3832 map
->m_len
= min(map
->m_len
, next
- map
->m_lblk
);
3835 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 1);
3839 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 0);
3845 * Block allocation/map/preallocation routine for extents based files
3848 * Need to be called with
3849 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3850 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3852 * return > 0, number of of blocks already mapped/allocated
3853 * if create == 0 and these are pre-allocated blocks
3854 * buffer head is unmapped
3855 * otherwise blocks are mapped
3857 * return = 0, if plain look up failed (blocks have not been allocated)
3858 * buffer head is unmapped
3860 * return < 0, error case.
3862 int ext4_ext_map_blocks(handle_t
*handle
, struct inode
*inode
,
3863 struct ext4_map_blocks
*map
, int flags
)
3865 struct ext4_ext_path
*path
= NULL
;
3866 struct ext4_extent newex
, *ex
, *ex2
;
3867 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3868 ext4_fsblk_t newblock
= 0;
3869 int free_on_err
= 0, err
= 0, depth
;
3870 unsigned int allocated
= 0, offset
= 0;
3871 unsigned int allocated_clusters
= 0;
3872 struct ext4_allocation_request ar
;
3873 ext4_io_end_t
*io
= ext4_inode_aio(inode
);
3874 ext4_lblk_t cluster_offset
;
3875 int set_unwritten
= 0;
3877 ext_debug("blocks %u/%u requested for inode %lu\n",
3878 map
->m_lblk
, map
->m_len
, inode
->i_ino
);
3879 trace_ext4_ext_map_blocks_enter(inode
, map
->m_lblk
, map
->m_len
, flags
);
3881 /* check in cache */
3882 if (ext4_ext_in_cache(inode
, map
->m_lblk
, &newex
)) {
3883 if (!newex
.ee_start_lo
&& !newex
.ee_start_hi
) {
3884 if ((sbi
->s_cluster_ratio
> 1) &&
3885 ext4_find_delalloc_cluster(inode
, map
->m_lblk
))
3886 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3888 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3890 * block isn't allocated yet and
3891 * user doesn't want to allocate it
3895 /* we should allocate requested block */
3897 /* block is already allocated */
3898 if (sbi
->s_cluster_ratio
> 1)
3899 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3900 newblock
= map
->m_lblk
3901 - le32_to_cpu(newex
.ee_block
)
3902 + ext4_ext_pblock(&newex
);
3903 /* number of remaining blocks in the extent */
3904 allocated
= ext4_ext_get_actual_len(&newex
) -
3905 (map
->m_lblk
- le32_to_cpu(newex
.ee_block
));
3910 /* find extent for this block */
3911 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, NULL
);
3913 err
= PTR_ERR(path
);
3918 depth
= ext_depth(inode
);
3921 * consistent leaf must not be empty;
3922 * this situation is possible, though, _during_ tree modification;
3923 * this is why assert can't be put in ext4_ext_find_extent()
3925 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
3926 EXT4_ERROR_INODE(inode
, "bad extent address "
3927 "lblock: %lu, depth: %d pblock %lld",
3928 (unsigned long) map
->m_lblk
, depth
,
3929 path
[depth
].p_block
);
3934 ex
= path
[depth
].p_ext
;
3936 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3937 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3938 unsigned short ee_len
;
3941 * Uninitialized extents are treated as holes, except that
3942 * we split out initialized portions during a write.
3944 ee_len
= ext4_ext_get_actual_len(ex
);
3946 trace_ext4_ext_show_extent(inode
, ee_block
, ee_start
, ee_len
);
3948 /* if found extent covers block, simply return it */
3949 if (in_range(map
->m_lblk
, ee_block
, ee_len
)) {
3950 newblock
= map
->m_lblk
- ee_block
+ ee_start
;
3951 /* number of remaining blocks in the extent */
3952 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3953 ext_debug("%u fit into %u:%d -> %llu\n", map
->m_lblk
,
3954 ee_block
, ee_len
, newblock
);
3957 * Do not put uninitialized extent
3960 if (!ext4_ext_is_uninitialized(ex
)) {
3961 ext4_ext_put_in_cache(inode
, ee_block
,
3965 allocated
= ext4_ext_handle_uninitialized_extents(
3966 handle
, inode
, map
, path
, flags
,
3967 allocated
, newblock
);
3972 if ((sbi
->s_cluster_ratio
> 1) &&
3973 ext4_find_delalloc_cluster(inode
, map
->m_lblk
))
3974 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3977 * requested block isn't allocated yet;
3978 * we couldn't try to create block if create flag is zero
3980 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3982 * put just found gap into cache to speed up
3983 * subsequent requests
3985 ext4_ext_put_gap_in_cache(inode
, path
, map
->m_lblk
);
3990 * Okay, we need to do block allocation.
3992 map
->m_flags
&= ~EXT4_MAP_FROM_CLUSTER
;
3993 newex
.ee_block
= cpu_to_le32(map
->m_lblk
);
3994 cluster_offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
-1);
3997 * If we are doing bigalloc, check to see if the extent returned
3998 * by ext4_ext_find_extent() implies a cluster we can use.
4000 if (cluster_offset
&& ex
&&
4001 get_implied_cluster_alloc(inode
->i_sb
, map
, ex
, path
)) {
4002 ar
.len
= allocated
= map
->m_len
;
4003 newblock
= map
->m_pblk
;
4004 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
4005 goto got_allocated_blocks
;
4008 /* find neighbour allocated blocks */
4009 ar
.lleft
= map
->m_lblk
;
4010 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
4013 ar
.lright
= map
->m_lblk
;
4015 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
, &ex2
);
4019 /* Check if the extent after searching to the right implies a
4020 * cluster we can use. */
4021 if ((sbi
->s_cluster_ratio
> 1) && ex2
&&
4022 get_implied_cluster_alloc(inode
->i_sb
, map
, ex2
, path
)) {
4023 ar
.len
= allocated
= map
->m_len
;
4024 newblock
= map
->m_pblk
;
4025 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
4026 goto got_allocated_blocks
;
4030 * See if request is beyond maximum number of blocks we can have in
4031 * a single extent. For an initialized extent this limit is
4032 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
4033 * EXT_UNINIT_MAX_LEN.
4035 if (map
->m_len
> EXT_INIT_MAX_LEN
&&
4036 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
4037 map
->m_len
= EXT_INIT_MAX_LEN
;
4038 else if (map
->m_len
> EXT_UNINIT_MAX_LEN
&&
4039 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
4040 map
->m_len
= EXT_UNINIT_MAX_LEN
;
4042 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
4043 newex
.ee_len
= cpu_to_le16(map
->m_len
);
4044 err
= ext4_ext_check_overlap(sbi
, inode
, &newex
, path
);
4046 allocated
= ext4_ext_get_actual_len(&newex
);
4048 allocated
= map
->m_len
;
4050 /* allocate new block */
4052 ar
.goal
= ext4_ext_find_goal(inode
, path
, map
->m_lblk
);
4053 ar
.logical
= map
->m_lblk
;
4055 * We calculate the offset from the beginning of the cluster
4056 * for the logical block number, since when we allocate a
4057 * physical cluster, the physical block should start at the
4058 * same offset from the beginning of the cluster. This is
4059 * needed so that future calls to get_implied_cluster_alloc()
4062 offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
- 1);
4063 ar
.len
= EXT4_NUM_B2C(sbi
, offset
+allocated
);
4065 ar
.logical
-= offset
;
4066 if (S_ISREG(inode
->i_mode
))
4067 ar
.flags
= EXT4_MB_HINT_DATA
;
4069 /* disable in-core preallocation for non-regular files */
4071 if (flags
& EXT4_GET_BLOCKS_NO_NORMALIZE
)
4072 ar
.flags
|= EXT4_MB_HINT_NOPREALLOC
;
4073 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
4076 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
4077 ar
.goal
, newblock
, allocated
);
4079 allocated_clusters
= ar
.len
;
4080 ar
.len
= EXT4_C2B(sbi
, ar
.len
) - offset
;
4081 if (ar
.len
> allocated
)
4084 got_allocated_blocks
:
4085 /* try to insert new extent into found leaf and return */
4086 ext4_ext_store_pblock(&newex
, newblock
+ offset
);
4087 newex
.ee_len
= cpu_to_le16(ar
.len
);
4088 /* Mark uninitialized */
4089 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
4090 ext4_ext_mark_uninitialized(&newex
);
4092 * io_end structure was created for every IO write to an
4093 * uninitialized extent. To avoid unnecessary conversion,
4094 * here we flag the IO that really needs the conversion.
4095 * For non asycn direct IO case, flag the inode state
4096 * that we need to perform conversion when IO is done.
4098 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
))
4100 if (ext4_should_dioread_nolock(inode
))
4101 map
->m_flags
|= EXT4_MAP_UNINIT
;
4105 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0)
4106 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
4109 err
= ext4_ext_insert_extent(handle
, inode
, path
,
4112 if (!err
&& set_unwritten
) {
4114 ext4_set_io_unwritten_flag(inode
, io
);
4116 ext4_set_inode_state(inode
,
4117 EXT4_STATE_DIO_UNWRITTEN
);
4120 if (err
&& free_on_err
) {
4121 int fb_flags
= flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
?
4122 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
: 0;
4123 /* free data blocks we just allocated */
4124 /* not a good idea to call discard here directly,
4125 * but otherwise we'd need to call it every free() */
4126 ext4_discard_preallocations(inode
);
4127 ext4_free_blocks(handle
, inode
, NULL
, ext4_ext_pblock(&newex
),
4128 ext4_ext_get_actual_len(&newex
), fb_flags
);
4132 /* previous routine could use block we allocated */
4133 newblock
= ext4_ext_pblock(&newex
);
4134 allocated
= ext4_ext_get_actual_len(&newex
);
4135 if (allocated
> map
->m_len
)
4136 allocated
= map
->m_len
;
4137 map
->m_flags
|= EXT4_MAP_NEW
;
4140 * Update reserved blocks/metadata blocks after successful
4141 * block allocation which had been deferred till now.
4143 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) {
4144 unsigned int reserved_clusters
;
4146 * Check how many clusters we had reserved this allocated range
4148 reserved_clusters
= get_reserved_cluster_alloc(inode
,
4149 map
->m_lblk
, allocated
);
4150 if (map
->m_flags
& EXT4_MAP_FROM_CLUSTER
) {
4151 if (reserved_clusters
) {
4153 * We have clusters reserved for this range.
4154 * But since we are not doing actual allocation
4155 * and are simply using blocks from previously
4156 * allocated cluster, we should release the
4157 * reservation and not claim quota.
4159 ext4_da_update_reserve_space(inode
,
4160 reserved_clusters
, 0);
4163 BUG_ON(allocated_clusters
< reserved_clusters
);
4164 /* We will claim quota for all newly allocated blocks.*/
4165 ext4_da_update_reserve_space(inode
, allocated_clusters
,
4167 if (reserved_clusters
< allocated_clusters
) {
4168 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4169 int reservation
= allocated_clusters
-
4172 * It seems we claimed few clusters outside of
4173 * the range of this allocation. We should give
4174 * it back to the reservation pool. This can
4175 * happen in the following case:
4177 * * Suppose s_cluster_ratio is 4 (i.e., each
4178 * cluster has 4 blocks. Thus, the clusters
4179 * are [0-3],[4-7],[8-11]...
4180 * * First comes delayed allocation write for
4181 * logical blocks 10 & 11. Since there were no
4182 * previous delayed allocated blocks in the
4183 * range [8-11], we would reserve 1 cluster
4185 * * Next comes write for logical blocks 3 to 8.
4186 * In this case, we will reserve 2 clusters
4187 * (for [0-3] and [4-7]; and not for [8-11] as
4188 * that range has a delayed allocated blocks.
4189 * Thus total reserved clusters now becomes 3.
4190 * * Now, during the delayed allocation writeout
4191 * time, we will first write blocks [3-8] and
4192 * allocate 3 clusters for writing these
4193 * blocks. Also, we would claim all these
4194 * three clusters above.
4195 * * Now when we come here to writeout the
4196 * blocks [10-11], we would expect to claim
4197 * the reservation of 1 cluster we had made
4198 * (and we would claim it since there are no
4199 * more delayed allocated blocks in the range
4200 * [8-11]. But our reserved cluster count had
4201 * already gone to 0.
4203 * Thus, at the step 4 above when we determine
4204 * that there are still some unwritten delayed
4205 * allocated blocks outside of our current
4206 * block range, we should increment the
4207 * reserved clusters count so that when the
4208 * remaining blocks finally gets written, we
4211 dquot_reserve_block(inode
,
4212 EXT4_C2B(sbi
, reservation
));
4213 spin_lock(&ei
->i_block_reservation_lock
);
4214 ei
->i_reserved_data_blocks
+= reservation
;
4215 spin_unlock(&ei
->i_block_reservation_lock
);
4221 * Cache the extent and update transaction to commit on fdatasync only
4222 * when it is _not_ an uninitialized extent.
4224 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
4225 ext4_ext_put_in_cache(inode
, map
->m_lblk
, allocated
, newblock
);
4226 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4228 ext4_update_inode_fsync_trans(handle
, inode
, 0);
4230 if (allocated
> map
->m_len
)
4231 allocated
= map
->m_len
;
4232 ext4_ext_show_leaf(inode
, path
);
4233 map
->m_flags
|= EXT4_MAP_MAPPED
;
4234 map
->m_pblk
= newblock
;
4235 map
->m_len
= allocated
;
4238 ext4_ext_drop_refs(path
);
4243 trace_ext4_ext_map_blocks_exit(inode
, map
, err
? err
: allocated
);
4245 return err
? err
: allocated
;
4248 void ext4_ext_truncate(struct inode
*inode
)
4250 struct address_space
*mapping
= inode
->i_mapping
;
4251 struct super_block
*sb
= inode
->i_sb
;
4252 ext4_lblk_t last_block
;
4258 * finish any pending end_io work so we won't run the risk of
4259 * converting any truncated blocks to initialized later
4261 ext4_flush_unwritten_io(inode
);
4264 * probably first extent we're gonna free will be last in block
4266 err
= ext4_writepage_trans_blocks(inode
);
4267 handle
= ext4_journal_start(inode
, err
);
4271 if (inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
4272 page_len
= PAGE_CACHE_SIZE
-
4273 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
4275 err
= ext4_discard_partial_page_buffers(handle
,
4276 mapping
, inode
->i_size
, page_len
, 0);
4282 if (ext4_orphan_add(handle
, inode
))
4285 down_write(&EXT4_I(inode
)->i_data_sem
);
4286 ext4_ext_invalidate_cache(inode
);
4288 ext4_discard_preallocations(inode
);
4291 * TODO: optimization is possible here.
4292 * Probably we need not scan at all,
4293 * because page truncation is enough.
4296 /* we have to know where to truncate from in crash case */
4297 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
4298 ext4_mark_inode_dirty(handle
, inode
);
4300 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
4301 >> EXT4_BLOCK_SIZE_BITS(sb
);
4302 err
= ext4_es_remove_extent(inode
, last_block
,
4303 EXT_MAX_BLOCKS
- last_block
);
4304 err
= ext4_ext_remove_space(inode
, last_block
, EXT_MAX_BLOCKS
- 1);
4306 /* In a multi-transaction truncate, we only make the final
4307 * transaction synchronous.
4310 ext4_handle_sync(handle
);
4312 up_write(&EXT4_I(inode
)->i_data_sem
);
4316 * If this was a simple ftruncate() and the file will remain alive,
4317 * then we need to clear up the orphan record which we created above.
4318 * However, if this was a real unlink then we were called by
4319 * ext4_delete_inode(), and we allow that function to clean up the
4320 * orphan info for us.
4323 ext4_orphan_del(handle
, inode
);
4325 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
4326 ext4_mark_inode_dirty(handle
, inode
);
4327 ext4_journal_stop(handle
);
4330 static void ext4_falloc_update_inode(struct inode
*inode
,
4331 int mode
, loff_t new_size
, int update_ctime
)
4333 struct timespec now
;
4336 now
= current_fs_time(inode
->i_sb
);
4337 if (!timespec_equal(&inode
->i_ctime
, &now
))
4338 inode
->i_ctime
= now
;
4341 * Update only when preallocation was requested beyond
4344 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
4345 if (new_size
> i_size_read(inode
))
4346 i_size_write(inode
, new_size
);
4347 if (new_size
> EXT4_I(inode
)->i_disksize
)
4348 ext4_update_i_disksize(inode
, new_size
);
4351 * Mark that we allocate beyond EOF so the subsequent truncate
4352 * can proceed even if the new size is the same as i_size.
4354 if (new_size
> i_size_read(inode
))
4355 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
4361 * preallocate space for a file. This implements ext4's fallocate file
4362 * operation, which gets called from sys_fallocate system call.
4363 * For block-mapped files, posix_fallocate should fall back to the method
4364 * of writing zeroes to the required new blocks (the same behavior which is
4365 * expected for file systems which do not support fallocate() system call).
4367 long ext4_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
4369 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
4372 unsigned int max_blocks
;
4377 struct ext4_map_blocks map
;
4378 unsigned int credits
, blkbits
= inode
->i_blkbits
;
4381 * currently supporting (pre)allocate mode for extent-based
4384 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
4387 /* Return error if mode is not supported */
4388 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
4391 if (mode
& FALLOC_FL_PUNCH_HOLE
)
4392 return ext4_punch_hole(file
, offset
, len
);
4394 trace_ext4_fallocate_enter(inode
, offset
, len
, mode
);
4395 map
.m_lblk
= offset
>> blkbits
;
4397 * We can't just convert len to max_blocks because
4398 * If blocksize = 4096 offset = 3072 and len = 2048
4400 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
4403 * credits to insert 1 extent into extent tree
4405 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
4406 mutex_lock(&inode
->i_mutex
);
4407 ret
= inode_newsize_ok(inode
, (len
+ offset
));
4409 mutex_unlock(&inode
->i_mutex
);
4410 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
, ret
);
4413 flags
= EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
;
4414 if (mode
& FALLOC_FL_KEEP_SIZE
)
4415 flags
|= EXT4_GET_BLOCKS_KEEP_SIZE
;
4417 * Don't normalize the request if it can fit in one extent so
4418 * that it doesn't get unnecessarily split into multiple
4421 if (len
<= EXT_UNINIT_MAX_LEN
<< blkbits
)
4422 flags
|= EXT4_GET_BLOCKS_NO_NORMALIZE
;
4424 /* Prevent race condition between unwritten */
4425 ext4_flush_unwritten_io(inode
);
4427 while (ret
>= 0 && ret
< max_blocks
) {
4428 map
.m_lblk
= map
.m_lblk
+ ret
;
4429 map
.m_len
= max_blocks
= max_blocks
- ret
;
4430 handle
= ext4_journal_start(inode
, credits
);
4431 if (IS_ERR(handle
)) {
4432 ret
= PTR_ERR(handle
);
4435 ret
= ext4_map_blocks(handle
, inode
, &map
, flags
);
4439 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
4440 "returned error inode#%lu, block=%u, "
4441 "max_blocks=%u", __func__
,
4442 inode
->i_ino
, map
.m_lblk
, max_blocks
);
4444 ext4_mark_inode_dirty(handle
, inode
);
4445 ret2
= ext4_journal_stop(handle
);
4448 if ((map
.m_lblk
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
4449 blkbits
) >> blkbits
))
4450 new_size
= offset
+ len
;
4452 new_size
= ((loff_t
) map
.m_lblk
+ ret
) << blkbits
;
4454 ext4_falloc_update_inode(inode
, mode
, new_size
,
4455 (map
.m_flags
& EXT4_MAP_NEW
));
4456 ext4_mark_inode_dirty(handle
, inode
);
4457 if ((file
->f_flags
& O_SYNC
) && ret
>= max_blocks
)
4458 ext4_handle_sync(handle
);
4459 ret2
= ext4_journal_stop(handle
);
4463 if (ret
== -ENOSPC
&&
4464 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
4468 mutex_unlock(&inode
->i_mutex
);
4469 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
,
4470 ret
> 0 ? ret2
: ret
);
4471 return ret
> 0 ? ret2
: ret
;
4475 * This function convert a range of blocks to written extents
4476 * The caller of this function will pass the start offset and the size.
4477 * all unwritten extents within this range will be converted to
4480 * This function is called from the direct IO end io call back
4481 * function, to convert the fallocated extents after IO is completed.
4482 * Returns 0 on success.
4484 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
4488 unsigned int max_blocks
;
4491 struct ext4_map_blocks map
;
4492 unsigned int credits
, blkbits
= inode
->i_blkbits
;
4494 map
.m_lblk
= offset
>> blkbits
;
4496 * We can't just convert len to max_blocks because
4497 * If blocksize = 4096 offset = 3072 and len = 2048
4499 max_blocks
= ((EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
) -
4502 * credits to insert 1 extent into extent tree
4504 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
4505 while (ret
>= 0 && ret
< max_blocks
) {
4507 map
.m_len
= (max_blocks
-= ret
);
4508 handle
= ext4_journal_start(inode
, credits
);
4509 if (IS_ERR(handle
)) {
4510 ret
= PTR_ERR(handle
);
4513 ret
= ext4_map_blocks(handle
, inode
, &map
,
4514 EXT4_GET_BLOCKS_IO_CONVERT_EXT
);
4517 ext4_msg(inode
->i_sb
, KERN_ERR
,
4518 "%s:%d: inode #%lu: block %u: len %u: "
4519 "ext4_ext_map_blocks returned %d",
4520 __func__
, __LINE__
, inode
->i_ino
, map
.m_lblk
,
4523 ext4_mark_inode_dirty(handle
, inode
);
4524 ret2
= ext4_journal_stop(handle
);
4525 if (ret
<= 0 || ret2
)
4528 return ret
> 0 ? ret2
: ret
;
4532 * If newex is not existing extent (newex->ec_start equals zero) find
4533 * delayed extent at start of newex and update newex accordingly and
4534 * return start of the next delayed extent.
4536 * If newex is existing extent (newex->ec_start is not equal zero)
4537 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
4538 * extent found. Leave newex unmodified.
4540 static int ext4_find_delayed_extent(struct inode
*inode
,
4541 struct ext4_ext_cache
*newex
)
4543 struct extent_status es
;
4544 ext4_lblk_t next_del
;
4546 es
.start
= newex
->ec_block
;
4547 next_del
= ext4_es_find_extent(inode
, &es
);
4549 if (newex
->ec_start
== 0) {
4551 * No extent in extent-tree contains block @newex->ec_start,
4552 * then the block may stay in 1)a hole or 2)delayed-extent.
4558 if (es
.start
> newex
->ec_block
) {
4560 newex
->ec_len
= min(es
.start
- newex
->ec_block
,
4565 newex
->ec_len
= es
.start
+ es
.len
- newex
->ec_block
;
4570 /* fiemap flags we can handle specified here */
4571 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4573 static int ext4_xattr_fiemap(struct inode
*inode
,
4574 struct fiemap_extent_info
*fieinfo
)
4578 __u32 flags
= FIEMAP_EXTENT_LAST
;
4579 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
4583 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
4584 struct ext4_iloc iloc
;
4585 int offset
; /* offset of xattr in inode */
4587 error
= ext4_get_inode_loc(inode
, &iloc
);
4590 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
4591 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
4592 EXT4_I(inode
)->i_extra_isize
;
4594 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
4595 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
4597 } else { /* external block */
4598 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
4599 length
= inode
->i_sb
->s_blocksize
;
4603 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
4605 return (error
< 0 ? error
: 0);
4609 * ext4_ext_punch_hole
4611 * Punches a hole of "length" bytes in a file starting
4614 * @inode: The inode of the file to punch a hole in
4615 * @offset: The starting byte offset of the hole
4616 * @length: The length of the hole
4618 * Returns the number of blocks removed or negative on err
4620 int ext4_ext_punch_hole(struct file
*file
, loff_t offset
, loff_t length
)
4622 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
4623 struct super_block
*sb
= inode
->i_sb
;
4624 ext4_lblk_t first_block
, stop_block
;
4625 struct address_space
*mapping
= inode
->i_mapping
;
4627 loff_t first_page
, last_page
, page_len
;
4628 loff_t first_page_offset
, last_page_offset
;
4629 int credits
, err
= 0;
4632 * Write out all dirty pages to avoid race conditions
4633 * Then release them.
4635 if (mapping
->nrpages
&& mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
4636 err
= filemap_write_and_wait_range(mapping
,
4637 offset
, offset
+ length
- 1);
4643 mutex_lock(&inode
->i_mutex
);
4644 /* It's not possible punch hole on append only file */
4645 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
)) {
4649 if (IS_SWAPFILE(inode
)) {
4654 /* No need to punch hole beyond i_size */
4655 if (offset
>= inode
->i_size
)
4659 * If the hole extends beyond i_size, set the hole
4660 * to end after the page that contains i_size
4662 if (offset
+ length
> inode
->i_size
) {
4663 length
= inode
->i_size
+
4664 PAGE_CACHE_SIZE
- (inode
->i_size
& (PAGE_CACHE_SIZE
- 1)) -
4668 first_page
= (offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
4669 last_page
= (offset
+ length
) >> PAGE_CACHE_SHIFT
;
4671 first_page_offset
= first_page
<< PAGE_CACHE_SHIFT
;
4672 last_page_offset
= last_page
<< PAGE_CACHE_SHIFT
;
4674 /* Now release the pages */
4675 if (last_page_offset
> first_page_offset
) {
4676 truncate_pagecache_range(inode
, first_page_offset
,
4677 last_page_offset
- 1);
4680 /* Wait all existing dio workers, newcomers will block on i_mutex */
4681 ext4_inode_block_unlocked_dio(inode
);
4682 err
= ext4_flush_unwritten_io(inode
);
4685 inode_dio_wait(inode
);
4687 credits
= ext4_writepage_trans_blocks(inode
);
4688 handle
= ext4_journal_start(inode
, credits
);
4689 if (IS_ERR(handle
)) {
4690 err
= PTR_ERR(handle
);
4696 * Now we need to zero out the non-page-aligned data in the
4697 * pages at the start and tail of the hole, and unmap the buffer
4698 * heads for the block aligned regions of the page that were
4699 * completely zeroed.
4701 if (first_page
> last_page
) {
4703 * If the file space being truncated is contained within a page
4704 * just zero out and unmap the middle of that page
4706 err
= ext4_discard_partial_page_buffers(handle
,
4707 mapping
, offset
, length
, 0);
4713 * zero out and unmap the partial page that contains
4714 * the start of the hole
4716 page_len
= first_page_offset
- offset
;
4718 err
= ext4_discard_partial_page_buffers(handle
, mapping
,
4719 offset
, page_len
, 0);
4725 * zero out and unmap the partial page that contains
4726 * the end of the hole
4728 page_len
= offset
+ length
- last_page_offset
;
4730 err
= ext4_discard_partial_page_buffers(handle
, mapping
,
4731 last_page_offset
, page_len
, 0);
4738 * If i_size is contained in the last page, we need to
4739 * unmap and zero the partial page after i_size
4741 if (inode
->i_size
>> PAGE_CACHE_SHIFT
== last_page
&&
4742 inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
4744 page_len
= PAGE_CACHE_SIZE
-
4745 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
4748 err
= ext4_discard_partial_page_buffers(handle
,
4749 mapping
, inode
->i_size
, page_len
, 0);
4756 first_block
= (offset
+ sb
->s_blocksize
- 1) >>
4757 EXT4_BLOCK_SIZE_BITS(sb
);
4758 stop_block
= (offset
+ length
) >> EXT4_BLOCK_SIZE_BITS(sb
);
4760 /* If there are no blocks to remove, return now */
4761 if (first_block
>= stop_block
)
4764 down_write(&EXT4_I(inode
)->i_data_sem
);
4765 ext4_ext_invalidate_cache(inode
);
4766 ext4_discard_preallocations(inode
);
4768 err
= ext4_es_remove_extent(inode
, first_block
,
4769 stop_block
- first_block
);
4770 err
= ext4_ext_remove_space(inode
, first_block
, stop_block
- 1);
4772 ext4_ext_invalidate_cache(inode
);
4773 ext4_discard_preallocations(inode
);
4776 ext4_handle_sync(handle
);
4778 up_write(&EXT4_I(inode
)->i_data_sem
);
4781 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
4782 ext4_mark_inode_dirty(handle
, inode
);
4783 ext4_journal_stop(handle
);
4785 ext4_inode_resume_unlocked_dio(inode
);
4787 mutex_unlock(&inode
->i_mutex
);
4791 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
4792 __u64 start
, __u64 len
)
4794 ext4_lblk_t start_blk
;
4797 /* fallback to generic here if not in extents fmt */
4798 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
4799 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
4802 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
4805 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
4806 error
= ext4_xattr_fiemap(inode
, fieinfo
);
4808 ext4_lblk_t len_blks
;
4811 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
4812 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
4813 if (last_blk
>= EXT_MAX_BLOCKS
)
4814 last_blk
= EXT_MAX_BLOCKS
-1;
4815 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
4818 * Walk the extent tree gathering extent information
4819 * and pushing extents back to the user.
4821 error
= ext4_fill_fiemap_extents(inode
, start_blk
,