1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
4 * Written by Alex Tomas <alex@clusterfs.com>
6 * Architecture independence:
7 * Copyright (c) 2005, Bull S.A.
8 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
12 * Extents support for EXT4
15 * - ext4*_error() should be used in some situations
16 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
17 * - smart tree reduction
21 #include <linux/time.h>
22 #include <linux/jbd2.h>
23 #include <linux/highuid.h>
24 #include <linux/pagemap.h>
25 #include <linux/quotaops.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <linux/uaccess.h>
29 #include <linux/fiemap.h>
30 #include <linux/backing-dev.h>
31 #include "ext4_jbd2.h"
32 #include "ext4_extents.h"
35 #include <trace/events/ext4.h>
38 * used by extent splitting.
40 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
42 #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
43 #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
45 #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
46 #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
48 static __le32
ext4_extent_block_csum(struct inode
*inode
,
49 struct ext4_extent_header
*eh
)
51 struct ext4_inode_info
*ei
= EXT4_I(inode
);
52 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
55 csum
= ext4_chksum(sbi
, ei
->i_csum_seed
, (__u8
*)eh
,
56 EXT4_EXTENT_TAIL_OFFSET(eh
));
57 return cpu_to_le32(csum
);
60 static int ext4_extent_block_csum_verify(struct inode
*inode
,
61 struct ext4_extent_header
*eh
)
63 struct ext4_extent_tail
*et
;
65 if (!ext4_has_metadata_csum(inode
->i_sb
))
68 et
= find_ext4_extent_tail(eh
);
69 if (et
->et_checksum
!= ext4_extent_block_csum(inode
, eh
))
74 static void ext4_extent_block_csum_set(struct inode
*inode
,
75 struct ext4_extent_header
*eh
)
77 struct ext4_extent_tail
*et
;
79 if (!ext4_has_metadata_csum(inode
->i_sb
))
82 et
= find_ext4_extent_tail(eh
);
83 et
->et_checksum
= ext4_extent_block_csum(inode
, eh
);
86 static int ext4_split_extent(handle_t
*handle
,
88 struct ext4_ext_path
**ppath
,
89 struct ext4_map_blocks
*map
,
93 static int ext4_split_extent_at(handle_t
*handle
,
95 struct ext4_ext_path
**ppath
,
100 static int ext4_find_delayed_extent(struct inode
*inode
,
101 struct extent_status
*newes
);
103 static int ext4_ext_truncate_extend_restart(handle_t
*handle
,
109 if (!ext4_handle_valid(handle
))
111 if (handle
->h_buffer_credits
>= needed
)
114 * If we need to extend the journal get a few extra blocks
115 * while we're at it for efficiency's sake.
118 err
= ext4_journal_extend(handle
, needed
- handle
->h_buffer_credits
);
121 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
133 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
134 struct ext4_ext_path
*path
)
137 /* path points to block */
138 BUFFER_TRACE(path
->p_bh
, "get_write_access");
139 return ext4_journal_get_write_access(handle
, path
->p_bh
);
141 /* path points to leaf/index in inode body */
142 /* we use in-core data, no need to protect them */
152 int __ext4_ext_dirty(const char *where
, unsigned int line
, handle_t
*handle
,
153 struct inode
*inode
, struct ext4_ext_path
*path
)
157 WARN_ON(!rwsem_is_locked(&EXT4_I(inode
)->i_data_sem
));
159 ext4_extent_block_csum_set(inode
, ext_block_hdr(path
->p_bh
));
160 /* path points to block */
161 err
= __ext4_handle_dirty_metadata(where
, line
, handle
,
164 /* path points to leaf/index in inode body */
165 err
= ext4_mark_inode_dirty(handle
, inode
);
170 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
171 struct ext4_ext_path
*path
,
175 int depth
= path
->p_depth
;
176 struct ext4_extent
*ex
;
179 * Try to predict block placement assuming that we are
180 * filling in a file which will eventually be
181 * non-sparse --- i.e., in the case of libbfd writing
182 * an ELF object sections out-of-order but in a way
183 * the eventually results in a contiguous object or
184 * executable file, or some database extending a table
185 * space file. However, this is actually somewhat
186 * non-ideal if we are writing a sparse file such as
187 * qemu or KVM writing a raw image file that is going
188 * to stay fairly sparse, since it will end up
189 * fragmenting the file system's free space. Maybe we
190 * should have some hueristics or some way to allow
191 * userspace to pass a hint to file system,
192 * especially if the latter case turns out to be
195 ex
= path
[depth
].p_ext
;
197 ext4_fsblk_t ext_pblk
= ext4_ext_pblock(ex
);
198 ext4_lblk_t ext_block
= le32_to_cpu(ex
->ee_block
);
200 if (block
> ext_block
)
201 return ext_pblk
+ (block
- ext_block
);
203 return ext_pblk
- (ext_block
- block
);
206 /* it looks like index is empty;
207 * try to find starting block from index itself */
208 if (path
[depth
].p_bh
)
209 return path
[depth
].p_bh
->b_blocknr
;
212 /* OK. use inode's group */
213 return ext4_inode_to_goal_block(inode
);
217 * Allocation for a meta data block
220 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
221 struct ext4_ext_path
*path
,
222 struct ext4_extent
*ex
, int *err
, unsigned int flags
)
224 ext4_fsblk_t goal
, newblock
;
226 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
227 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, flags
,
232 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
236 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
237 / sizeof(struct ext4_extent
);
238 #ifdef AGGRESSIVE_TEST
239 if (!check
&& size
> 6)
245 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
249 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
250 / sizeof(struct ext4_extent_idx
);
251 #ifdef AGGRESSIVE_TEST
252 if (!check
&& size
> 5)
258 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
262 size
= sizeof(EXT4_I(inode
)->i_data
);
263 size
-= sizeof(struct ext4_extent_header
);
264 size
/= sizeof(struct ext4_extent
);
265 #ifdef AGGRESSIVE_TEST
266 if (!check
&& size
> 3)
272 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
276 size
= sizeof(EXT4_I(inode
)->i_data
);
277 size
-= sizeof(struct ext4_extent_header
);
278 size
/= sizeof(struct ext4_extent_idx
);
279 #ifdef AGGRESSIVE_TEST
280 if (!check
&& size
> 4)
287 ext4_force_split_extent_at(handle_t
*handle
, struct inode
*inode
,
288 struct ext4_ext_path
**ppath
, ext4_lblk_t lblk
,
291 struct ext4_ext_path
*path
= *ppath
;
292 int unwritten
= ext4_ext_is_unwritten(path
[path
->p_depth
].p_ext
);
294 return ext4_split_extent_at(handle
, inode
, ppath
, lblk
, unwritten
?
295 EXT4_EXT_MARK_UNWRIT1
|EXT4_EXT_MARK_UNWRIT2
: 0,
296 EXT4_EX_NOCACHE
| EXT4_GET_BLOCKS_PRE_IO
|
297 (nofail
? EXT4_GET_BLOCKS_METADATA_NOFAIL
:0));
301 * Calculate the number of metadata blocks needed
302 * to allocate @blocks
303 * Worse case is one block per extent
305 int ext4_ext_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
307 struct ext4_inode_info
*ei
= EXT4_I(inode
);
310 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
311 / sizeof(struct ext4_extent_idx
));
314 * If the new delayed allocation block is contiguous with the
315 * previous da block, it can share index blocks with the
316 * previous block, so we only need to allocate a new index
317 * block every idxs leaf blocks. At ldxs**2 blocks, we need
318 * an additional index block, and at ldxs**3 blocks, yet
319 * another index blocks.
321 if (ei
->i_da_metadata_calc_len
&&
322 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
325 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
327 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
329 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
331 ei
->i_da_metadata_calc_len
= 0;
333 ei
->i_da_metadata_calc_len
++;
334 ei
->i_da_metadata_calc_last_lblock
++;
339 * In the worst case we need a new set of index blocks at
340 * every level of the inode's extent tree.
342 ei
->i_da_metadata_calc_len
= 1;
343 ei
->i_da_metadata_calc_last_lblock
= lblock
;
344 return ext_depth(inode
) + 1;
348 ext4_ext_max_entries(struct inode
*inode
, int depth
)
352 if (depth
== ext_depth(inode
)) {
354 max
= ext4_ext_space_root(inode
, 1);
356 max
= ext4_ext_space_root_idx(inode
, 1);
359 max
= ext4_ext_space_block(inode
, 1);
361 max
= ext4_ext_space_block_idx(inode
, 1);
367 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
369 ext4_fsblk_t block
= ext4_ext_pblock(ext
);
370 int len
= ext4_ext_get_actual_len(ext
);
371 ext4_lblk_t lblock
= le32_to_cpu(ext
->ee_block
);
376 * - overflow/wrap-around
378 if (lblock
+ len
<= lblock
)
380 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
383 static int ext4_valid_extent_idx(struct inode
*inode
,
384 struct ext4_extent_idx
*ext_idx
)
386 ext4_fsblk_t block
= ext4_idx_pblock(ext_idx
);
388 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
391 static int ext4_valid_extent_entries(struct inode
*inode
,
392 struct ext4_extent_header
*eh
,
395 unsigned short entries
;
396 if (eh
->eh_entries
== 0)
399 entries
= le16_to_cpu(eh
->eh_entries
);
403 struct ext4_extent
*ext
= EXT_FIRST_EXTENT(eh
);
404 struct ext4_super_block
*es
= EXT4_SB(inode
->i_sb
)->s_es
;
405 ext4_fsblk_t pblock
= 0;
406 ext4_lblk_t lblock
= 0;
407 ext4_lblk_t prev
= 0;
410 if (!ext4_valid_extent(inode
, ext
))
413 /* Check for overlapping extents */
414 lblock
= le32_to_cpu(ext
->ee_block
);
415 len
= ext4_ext_get_actual_len(ext
);
416 if ((lblock
<= prev
) && prev
) {
417 pblock
= ext4_ext_pblock(ext
);
418 es
->s_last_error_block
= cpu_to_le64(pblock
);
423 prev
= lblock
+ len
- 1;
426 struct ext4_extent_idx
*ext_idx
= EXT_FIRST_INDEX(eh
);
428 if (!ext4_valid_extent_idx(inode
, ext_idx
))
437 static int __ext4_ext_check(const char *function
, unsigned int line
,
438 struct inode
*inode
, struct ext4_extent_header
*eh
,
439 int depth
, ext4_fsblk_t pblk
)
441 const char *error_msg
;
442 int max
= 0, err
= -EFSCORRUPTED
;
444 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
445 error_msg
= "invalid magic";
448 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
449 error_msg
= "unexpected eh_depth";
452 if (unlikely(eh
->eh_max
== 0)) {
453 error_msg
= "invalid eh_max";
456 max
= ext4_ext_max_entries(inode
, depth
);
457 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
458 error_msg
= "too large eh_max";
461 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
462 error_msg
= "invalid eh_entries";
465 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
466 error_msg
= "invalid extent entries";
469 if (unlikely(depth
> 32)) {
470 error_msg
= "too large eh_depth";
473 /* Verify checksum on non-root extent tree nodes */
474 if (ext_depth(inode
) != depth
&&
475 !ext4_extent_block_csum_verify(inode
, eh
)) {
476 error_msg
= "extent tree corrupted";
483 ext4_error_inode(inode
, function
, line
, 0,
484 "pblk %llu bad header/extent: %s - magic %x, "
485 "entries %u, max %u(%u), depth %u(%u)",
486 (unsigned long long) pblk
, error_msg
,
487 le16_to_cpu(eh
->eh_magic
),
488 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
489 max
, le16_to_cpu(eh
->eh_depth
), depth
);
493 #define ext4_ext_check(inode, eh, depth, pblk) \
494 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
496 int ext4_ext_check_inode(struct inode
*inode
)
498 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
), 0);
501 static void ext4_cache_extents(struct inode
*inode
,
502 struct ext4_extent_header
*eh
)
504 struct ext4_extent
*ex
= EXT_FIRST_EXTENT(eh
);
505 ext4_lblk_t prev
= 0;
508 for (i
= le16_to_cpu(eh
->eh_entries
); i
> 0; i
--, ex
++) {
509 unsigned int status
= EXTENT_STATUS_WRITTEN
;
510 ext4_lblk_t lblk
= le32_to_cpu(ex
->ee_block
);
511 int len
= ext4_ext_get_actual_len(ex
);
513 if (prev
&& (prev
!= lblk
))
514 ext4_es_cache_extent(inode
, prev
, lblk
- prev
, ~0,
517 if (ext4_ext_is_unwritten(ex
))
518 status
= EXTENT_STATUS_UNWRITTEN
;
519 ext4_es_cache_extent(inode
, lblk
, len
,
520 ext4_ext_pblock(ex
), status
);
525 static struct buffer_head
*
526 __read_extent_tree_block(const char *function
, unsigned int line
,
527 struct inode
*inode
, ext4_fsblk_t pblk
, int depth
,
530 struct buffer_head
*bh
;
533 bh
= sb_getblk_gfp(inode
->i_sb
, pblk
, __GFP_MOVABLE
| GFP_NOFS
);
535 return ERR_PTR(-ENOMEM
);
537 if (!bh_uptodate_or_lock(bh
)) {
538 trace_ext4_ext_load_extent(inode
, pblk
, _RET_IP_
);
539 err
= bh_submit_read(bh
);
543 if (buffer_verified(bh
) && !(flags
& EXT4_EX_FORCE_CACHE
))
545 if (!ext4_has_feature_journal(inode
->i_sb
) ||
547 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_journal_inum
))) {
548 err
= __ext4_ext_check(function
, line
, inode
,
549 ext_block_hdr(bh
), depth
, pblk
);
553 set_buffer_verified(bh
);
555 * If this is a leaf block, cache all of its entries
557 if (!(flags
& EXT4_EX_NOCACHE
) && depth
== 0) {
558 struct ext4_extent_header
*eh
= ext_block_hdr(bh
);
559 ext4_cache_extents(inode
, eh
);
568 #define read_extent_tree_block(inode, pblk, depth, flags) \
569 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
573 * This function is called to cache a file's extent information in the
576 int ext4_ext_precache(struct inode
*inode
)
578 struct ext4_inode_info
*ei
= EXT4_I(inode
);
579 struct ext4_ext_path
*path
= NULL
;
580 struct buffer_head
*bh
;
581 int i
= 0, depth
, ret
= 0;
583 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
584 return 0; /* not an extent-mapped inode */
586 down_read(&ei
->i_data_sem
);
587 depth
= ext_depth(inode
);
589 path
= kcalloc(depth
+ 1, sizeof(struct ext4_ext_path
),
592 up_read(&ei
->i_data_sem
);
596 /* Don't cache anything if there are no external extent blocks */
599 path
[0].p_hdr
= ext_inode_hdr(inode
);
600 ret
= ext4_ext_check(inode
, path
[0].p_hdr
, depth
, 0);
603 path
[0].p_idx
= EXT_FIRST_INDEX(path
[0].p_hdr
);
606 * If this is a leaf block or we've reached the end of
607 * the index block, go up
610 path
[i
].p_idx
> EXT_LAST_INDEX(path
[i
].p_hdr
)) {
611 brelse(path
[i
].p_bh
);
616 bh
= read_extent_tree_block(inode
,
617 ext4_idx_pblock(path
[i
].p_idx
++),
619 EXT4_EX_FORCE_CACHE
);
626 path
[i
].p_hdr
= ext_block_hdr(bh
);
627 path
[i
].p_idx
= EXT_FIRST_INDEX(path
[i
].p_hdr
);
629 ext4_set_inode_state(inode
, EXT4_STATE_EXT_PRECACHED
);
631 up_read(&ei
->i_data_sem
);
632 ext4_ext_drop_refs(path
);
638 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
640 int k
, l
= path
->p_depth
;
643 for (k
= 0; k
<= l
; k
++, path
++) {
645 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
646 ext4_idx_pblock(path
->p_idx
));
647 } else if (path
->p_ext
) {
648 ext_debug(" %d:[%d]%d:%llu ",
649 le32_to_cpu(path
->p_ext
->ee_block
),
650 ext4_ext_is_unwritten(path
->p_ext
),
651 ext4_ext_get_actual_len(path
->p_ext
),
652 ext4_ext_pblock(path
->p_ext
));
659 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
661 int depth
= ext_depth(inode
);
662 struct ext4_extent_header
*eh
;
663 struct ext4_extent
*ex
;
669 eh
= path
[depth
].p_hdr
;
670 ex
= EXT_FIRST_EXTENT(eh
);
672 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
674 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
675 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
676 ext4_ext_is_unwritten(ex
),
677 ext4_ext_get_actual_len(ex
), ext4_ext_pblock(ex
));
682 static void ext4_ext_show_move(struct inode
*inode
, struct ext4_ext_path
*path
,
683 ext4_fsblk_t newblock
, int level
)
685 int depth
= ext_depth(inode
);
686 struct ext4_extent
*ex
;
688 if (depth
!= level
) {
689 struct ext4_extent_idx
*idx
;
690 idx
= path
[level
].p_idx
;
691 while (idx
<= EXT_MAX_INDEX(path
[level
].p_hdr
)) {
692 ext_debug("%d: move %d:%llu in new index %llu\n", level
,
693 le32_to_cpu(idx
->ei_block
),
694 ext4_idx_pblock(idx
),
702 ex
= path
[depth
].p_ext
;
703 while (ex
<= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
704 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
705 le32_to_cpu(ex
->ee_block
),
707 ext4_ext_is_unwritten(ex
),
708 ext4_ext_get_actual_len(ex
),
715 #define ext4_ext_show_path(inode, path)
716 #define ext4_ext_show_leaf(inode, path)
717 #define ext4_ext_show_move(inode, path, newblock, level)
720 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
726 depth
= path
->p_depth
;
727 for (i
= 0; i
<= depth
; i
++, path
++)
735 * ext4_ext_binsearch_idx:
736 * binary search for the closest index of the given block
737 * the header must be checked before calling this
740 ext4_ext_binsearch_idx(struct inode
*inode
,
741 struct ext4_ext_path
*path
, ext4_lblk_t block
)
743 struct ext4_extent_header
*eh
= path
->p_hdr
;
744 struct ext4_extent_idx
*r
, *l
, *m
;
747 ext_debug("binsearch for %u(idx): ", block
);
749 l
= EXT_FIRST_INDEX(eh
) + 1;
750 r
= EXT_LAST_INDEX(eh
);
753 if (block
< le32_to_cpu(m
->ei_block
))
757 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
758 m
, le32_to_cpu(m
->ei_block
),
759 r
, le32_to_cpu(r
->ei_block
));
763 ext_debug(" -> %u->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
764 ext4_idx_pblock(path
->p_idx
));
766 #ifdef CHECK_BINSEARCH
768 struct ext4_extent_idx
*chix
, *ix
;
771 chix
= ix
= EXT_FIRST_INDEX(eh
);
772 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
774 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
775 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
777 ix
, EXT_FIRST_INDEX(eh
));
778 printk(KERN_DEBUG
"%u <= %u\n",
779 le32_to_cpu(ix
->ei_block
),
780 le32_to_cpu(ix
[-1].ei_block
));
782 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
783 <= le32_to_cpu(ix
[-1].ei_block
));
784 if (block
< le32_to_cpu(ix
->ei_block
))
788 BUG_ON(chix
!= path
->p_idx
);
795 * ext4_ext_binsearch:
796 * binary search for closest extent of the given block
797 * the header must be checked before calling this
800 ext4_ext_binsearch(struct inode
*inode
,
801 struct ext4_ext_path
*path
, ext4_lblk_t block
)
803 struct ext4_extent_header
*eh
= path
->p_hdr
;
804 struct ext4_extent
*r
, *l
, *m
;
806 if (eh
->eh_entries
== 0) {
808 * this leaf is empty:
809 * we get such a leaf in split/add case
814 ext_debug("binsearch for %u: ", block
);
816 l
= EXT_FIRST_EXTENT(eh
) + 1;
817 r
= EXT_LAST_EXTENT(eh
);
821 if (block
< le32_to_cpu(m
->ee_block
))
825 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
826 m
, le32_to_cpu(m
->ee_block
),
827 r
, le32_to_cpu(r
->ee_block
));
831 ext_debug(" -> %d:%llu:[%d]%d ",
832 le32_to_cpu(path
->p_ext
->ee_block
),
833 ext4_ext_pblock(path
->p_ext
),
834 ext4_ext_is_unwritten(path
->p_ext
),
835 ext4_ext_get_actual_len(path
->p_ext
));
837 #ifdef CHECK_BINSEARCH
839 struct ext4_extent
*chex
, *ex
;
842 chex
= ex
= EXT_FIRST_EXTENT(eh
);
843 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
844 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
845 <= le32_to_cpu(ex
[-1].ee_block
));
846 if (block
< le32_to_cpu(ex
->ee_block
))
850 BUG_ON(chex
!= path
->p_ext
);
856 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
858 struct ext4_extent_header
*eh
;
860 eh
= ext_inode_hdr(inode
);
863 eh
->eh_magic
= EXT4_EXT_MAGIC
;
864 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
865 ext4_mark_inode_dirty(handle
, inode
);
869 struct ext4_ext_path
*
870 ext4_find_extent(struct inode
*inode
, ext4_lblk_t block
,
871 struct ext4_ext_path
**orig_path
, int flags
)
873 struct ext4_extent_header
*eh
;
874 struct buffer_head
*bh
;
875 struct ext4_ext_path
*path
= orig_path
? *orig_path
: NULL
;
876 short int depth
, i
, ppos
= 0;
879 eh
= ext_inode_hdr(inode
);
880 depth
= ext_depth(inode
);
881 if (depth
< 0 || depth
> EXT4_MAX_EXTENT_DEPTH
) {
882 EXT4_ERROR_INODE(inode
, "inode has invalid extent depth: %d",
889 ext4_ext_drop_refs(path
);
890 if (depth
> path
[0].p_maxdepth
) {
892 *orig_path
= path
= NULL
;
896 /* account possible depth increase */
897 path
= kcalloc(depth
+ 2, sizeof(struct ext4_ext_path
),
900 return ERR_PTR(-ENOMEM
);
901 path
[0].p_maxdepth
= depth
+ 1;
907 if (!(flags
& EXT4_EX_NOCACHE
) && depth
== 0)
908 ext4_cache_extents(inode
, eh
);
909 /* walk through the tree */
911 ext_debug("depth %d: num %d, max %d\n",
912 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
914 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
915 path
[ppos
].p_block
= ext4_idx_pblock(path
[ppos
].p_idx
);
916 path
[ppos
].p_depth
= i
;
917 path
[ppos
].p_ext
= NULL
;
919 bh
= read_extent_tree_block(inode
, path
[ppos
].p_block
, --i
,
926 eh
= ext_block_hdr(bh
);
928 path
[ppos
].p_bh
= bh
;
929 path
[ppos
].p_hdr
= eh
;
932 path
[ppos
].p_depth
= i
;
933 path
[ppos
].p_ext
= NULL
;
934 path
[ppos
].p_idx
= NULL
;
937 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
938 /* if not an empty leaf */
939 if (path
[ppos
].p_ext
)
940 path
[ppos
].p_block
= ext4_ext_pblock(path
[ppos
].p_ext
);
942 ext4_ext_show_path(inode
, path
);
947 ext4_ext_drop_refs(path
);
955 * ext4_ext_insert_index:
956 * insert new index [@logical;@ptr] into the block at @curp;
957 * check where to insert: before @curp or after @curp
959 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
960 struct ext4_ext_path
*curp
,
961 int logical
, ext4_fsblk_t ptr
)
963 struct ext4_extent_idx
*ix
;
966 err
= ext4_ext_get_access(handle
, inode
, curp
);
970 if (unlikely(logical
== le32_to_cpu(curp
->p_idx
->ei_block
))) {
971 EXT4_ERROR_INODE(inode
,
972 "logical %d == ei_block %d!",
973 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
974 return -EFSCORRUPTED
;
977 if (unlikely(le16_to_cpu(curp
->p_hdr
->eh_entries
)
978 >= le16_to_cpu(curp
->p_hdr
->eh_max
))) {
979 EXT4_ERROR_INODE(inode
,
980 "eh_entries %d >= eh_max %d!",
981 le16_to_cpu(curp
->p_hdr
->eh_entries
),
982 le16_to_cpu(curp
->p_hdr
->eh_max
));
983 return -EFSCORRUPTED
;
986 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
988 ext_debug("insert new index %d after: %llu\n", logical
, ptr
);
989 ix
= curp
->p_idx
+ 1;
992 ext_debug("insert new index %d before: %llu\n", logical
, ptr
);
996 len
= EXT_LAST_INDEX(curp
->p_hdr
) - ix
+ 1;
999 ext_debug("insert new index %d: "
1000 "move %d indices from 0x%p to 0x%p\n",
1001 logical
, len
, ix
, ix
+ 1);
1002 memmove(ix
+ 1, ix
, len
* sizeof(struct ext4_extent_idx
));
1005 if (unlikely(ix
> EXT_MAX_INDEX(curp
->p_hdr
))) {
1006 EXT4_ERROR_INODE(inode
, "ix > EXT_MAX_INDEX!");
1007 return -EFSCORRUPTED
;
1010 ix
->ei_block
= cpu_to_le32(logical
);
1011 ext4_idx_store_pblock(ix
, ptr
);
1012 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
1014 if (unlikely(ix
> EXT_LAST_INDEX(curp
->p_hdr
))) {
1015 EXT4_ERROR_INODE(inode
, "ix > EXT_LAST_INDEX!");
1016 return -EFSCORRUPTED
;
1019 err
= ext4_ext_dirty(handle
, inode
, curp
);
1020 ext4_std_error(inode
->i_sb
, err
);
1027 * inserts new subtree into the path, using free index entry
1029 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1030 * - makes decision where to split
1031 * - moves remaining extents and index entries (right to the split point)
1032 * into the newly allocated blocks
1033 * - initializes subtree
1035 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
1037 struct ext4_ext_path
*path
,
1038 struct ext4_extent
*newext
, int at
)
1040 struct buffer_head
*bh
= NULL
;
1041 int depth
= ext_depth(inode
);
1042 struct ext4_extent_header
*neh
;
1043 struct ext4_extent_idx
*fidx
;
1044 int i
= at
, k
, m
, a
;
1045 ext4_fsblk_t newblock
, oldblock
;
1047 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
1049 size_t ext_size
= 0;
1051 /* make decision: where to split? */
1052 /* FIXME: now decision is simplest: at current extent */
1054 /* if current leaf will be split, then we should use
1055 * border from split point */
1056 if (unlikely(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
))) {
1057 EXT4_ERROR_INODE(inode
, "p_ext > EXT_MAX_EXTENT!");
1058 return -EFSCORRUPTED
;
1060 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
1061 border
= path
[depth
].p_ext
[1].ee_block
;
1062 ext_debug("leaf will be split."
1063 " next leaf starts at %d\n",
1064 le32_to_cpu(border
));
1066 border
= newext
->ee_block
;
1067 ext_debug("leaf will be added."
1068 " next leaf starts at %d\n",
1069 le32_to_cpu(border
));
1073 * If error occurs, then we break processing
1074 * and mark filesystem read-only. index won't
1075 * be inserted and tree will be in consistent
1076 * state. Next mount will repair buffers too.
1080 * Get array to track all allocated blocks.
1081 * We need this to handle errors and free blocks
1084 ablocks
= kcalloc(depth
, sizeof(ext4_fsblk_t
), GFP_NOFS
);
1088 /* allocate all needed blocks */
1089 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
1090 for (a
= 0; a
< depth
- at
; a
++) {
1091 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
1092 newext
, &err
, flags
);
1095 ablocks
[a
] = newblock
;
1098 /* initialize new leaf */
1099 newblock
= ablocks
[--a
];
1100 if (unlikely(newblock
== 0)) {
1101 EXT4_ERROR_INODE(inode
, "newblock == 0!");
1102 err
= -EFSCORRUPTED
;
1105 bh
= sb_getblk_gfp(inode
->i_sb
, newblock
, __GFP_MOVABLE
| GFP_NOFS
);
1106 if (unlikely(!bh
)) {
1112 err
= ext4_journal_get_create_access(handle
, bh
);
1116 neh
= ext_block_hdr(bh
);
1117 neh
->eh_entries
= 0;
1118 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1119 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1122 /* move remainder of path[depth] to the new leaf */
1123 if (unlikely(path
[depth
].p_hdr
->eh_entries
!=
1124 path
[depth
].p_hdr
->eh_max
)) {
1125 EXT4_ERROR_INODE(inode
, "eh_entries %d != eh_max %d!",
1126 path
[depth
].p_hdr
->eh_entries
,
1127 path
[depth
].p_hdr
->eh_max
);
1128 err
= -EFSCORRUPTED
;
1131 /* start copy from next extent */
1132 m
= EXT_MAX_EXTENT(path
[depth
].p_hdr
) - path
[depth
].p_ext
++;
1133 ext4_ext_show_move(inode
, path
, newblock
, depth
);
1135 struct ext4_extent
*ex
;
1136 ex
= EXT_FIRST_EXTENT(neh
);
1137 memmove(ex
, path
[depth
].p_ext
, sizeof(struct ext4_extent
) * m
);
1138 le16_add_cpu(&neh
->eh_entries
, m
);
1141 /* zero out unused area in the extent block */
1142 ext_size
= sizeof(struct ext4_extent_header
) +
1143 sizeof(struct ext4_extent
) * le16_to_cpu(neh
->eh_entries
);
1144 memset(bh
->b_data
+ ext_size
, 0, inode
->i_sb
->s_blocksize
- ext_size
);
1145 ext4_extent_block_csum_set(inode
, neh
);
1146 set_buffer_uptodate(bh
);
1149 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1155 /* correct old leaf */
1157 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1160 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
1161 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1167 /* create intermediate indexes */
1169 if (unlikely(k
< 0)) {
1170 EXT4_ERROR_INODE(inode
, "k %d < 0!", k
);
1171 err
= -EFSCORRUPTED
;
1175 ext_debug("create %d intermediate indices\n", k
);
1176 /* insert new index into current index block */
1177 /* current depth stored in i var */
1180 oldblock
= newblock
;
1181 newblock
= ablocks
[--a
];
1182 bh
= sb_getblk(inode
->i_sb
, newblock
);
1183 if (unlikely(!bh
)) {
1189 err
= ext4_journal_get_create_access(handle
, bh
);
1193 neh
= ext_block_hdr(bh
);
1194 neh
->eh_entries
= cpu_to_le16(1);
1195 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1196 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1197 neh
->eh_depth
= cpu_to_le16(depth
- i
);
1198 fidx
= EXT_FIRST_INDEX(neh
);
1199 fidx
->ei_block
= border
;
1200 ext4_idx_store_pblock(fidx
, oldblock
);
1202 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1203 i
, newblock
, le32_to_cpu(border
), oldblock
);
1205 /* move remainder of path[i] to the new index block */
1206 if (unlikely(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
1207 EXT_LAST_INDEX(path
[i
].p_hdr
))) {
1208 EXT4_ERROR_INODE(inode
,
1209 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1210 le32_to_cpu(path
[i
].p_ext
->ee_block
));
1211 err
= -EFSCORRUPTED
;
1214 /* start copy indexes */
1215 m
= EXT_MAX_INDEX(path
[i
].p_hdr
) - path
[i
].p_idx
++;
1216 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
1217 EXT_MAX_INDEX(path
[i
].p_hdr
));
1218 ext4_ext_show_move(inode
, path
, newblock
, i
);
1220 memmove(++fidx
, path
[i
].p_idx
,
1221 sizeof(struct ext4_extent_idx
) * m
);
1222 le16_add_cpu(&neh
->eh_entries
, m
);
1224 /* zero out unused area in the extent block */
1225 ext_size
= sizeof(struct ext4_extent_header
) +
1226 (sizeof(struct ext4_extent
) * le16_to_cpu(neh
->eh_entries
));
1227 memset(bh
->b_data
+ ext_size
, 0,
1228 inode
->i_sb
->s_blocksize
- ext_size
);
1229 ext4_extent_block_csum_set(inode
, neh
);
1230 set_buffer_uptodate(bh
);
1233 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1239 /* correct old index */
1241 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
1244 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1245 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1253 /* insert new index */
1254 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1255 le32_to_cpu(border
), newblock
);
1259 if (buffer_locked(bh
))
1265 /* free all allocated blocks in error case */
1266 for (i
= 0; i
< depth
; i
++) {
1269 ext4_free_blocks(handle
, inode
, NULL
, ablocks
[i
], 1,
1270 EXT4_FREE_BLOCKS_METADATA
);
1279 * ext4_ext_grow_indepth:
1280 * implements tree growing procedure:
1281 * - allocates new block
1282 * - moves top-level data (index block or leaf) into the new block
1283 * - initializes new top-level, creating index that points to the
1284 * just created block
1286 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1289 struct ext4_extent_header
*neh
;
1290 struct buffer_head
*bh
;
1291 ext4_fsblk_t newblock
, goal
= 0;
1292 struct ext4_super_block
*es
= EXT4_SB(inode
->i_sb
)->s_es
;
1294 size_t ext_size
= 0;
1296 /* Try to prepend new index to old one */
1297 if (ext_depth(inode
))
1298 goal
= ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode
)));
1299 if (goal
> le32_to_cpu(es
->s_first_data_block
)) {
1300 flags
|= EXT4_MB_HINT_TRY_GOAL
;
1303 goal
= ext4_inode_to_goal_block(inode
);
1304 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, flags
,
1309 bh
= sb_getblk_gfp(inode
->i_sb
, newblock
, __GFP_MOVABLE
| GFP_NOFS
);
1314 err
= ext4_journal_get_create_access(handle
, bh
);
1320 ext_size
= sizeof(EXT4_I(inode
)->i_data
);
1321 /* move top-level index/leaf into new block */
1322 memmove(bh
->b_data
, EXT4_I(inode
)->i_data
, ext_size
);
1323 /* zero out unused area in the extent block */
1324 memset(bh
->b_data
+ ext_size
, 0, inode
->i_sb
->s_blocksize
- ext_size
);
1326 /* set size of new block */
1327 neh
= ext_block_hdr(bh
);
1328 /* old root could have indexes or leaves
1329 * so calculate e_max right way */
1330 if (ext_depth(inode
))
1331 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1333 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1334 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1335 ext4_extent_block_csum_set(inode
, neh
);
1336 set_buffer_uptodate(bh
);
1339 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1343 /* Update top-level index: num,max,pointer */
1344 neh
= ext_inode_hdr(inode
);
1345 neh
->eh_entries
= cpu_to_le16(1);
1346 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh
), newblock
);
1347 if (neh
->eh_depth
== 0) {
1348 /* Root extent block becomes index block */
1349 neh
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1350 EXT_FIRST_INDEX(neh
)->ei_block
=
1351 EXT_FIRST_EXTENT(neh
)->ee_block
;
1353 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1354 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1355 le32_to_cpu(EXT_FIRST_INDEX(neh
)->ei_block
),
1356 ext4_idx_pblock(EXT_FIRST_INDEX(neh
)));
1358 le16_add_cpu(&neh
->eh_depth
, 1);
1359 ext4_mark_inode_dirty(handle
, inode
);
1367 * ext4_ext_create_new_leaf:
1368 * finds empty index and adds new leaf.
1369 * if no free index is found, then it requests in-depth growing.
1371 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1372 unsigned int mb_flags
,
1373 unsigned int gb_flags
,
1374 struct ext4_ext_path
**ppath
,
1375 struct ext4_extent
*newext
)
1377 struct ext4_ext_path
*path
= *ppath
;
1378 struct ext4_ext_path
*curp
;
1379 int depth
, i
, err
= 0;
1382 i
= depth
= ext_depth(inode
);
1384 /* walk up to the tree and look for free index entry */
1385 curp
= path
+ depth
;
1386 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1391 /* we use already allocated block for index block,
1392 * so subsequent data blocks should be contiguous */
1393 if (EXT_HAS_FREE_INDEX(curp
)) {
1394 /* if we found index with free entry, then use that
1395 * entry: create all needed subtree and add new leaf */
1396 err
= ext4_ext_split(handle
, inode
, mb_flags
, path
, newext
, i
);
1401 path
= ext4_find_extent(inode
,
1402 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1405 err
= PTR_ERR(path
);
1407 /* tree is full, time to grow in depth */
1408 err
= ext4_ext_grow_indepth(handle
, inode
, mb_flags
);
1413 path
= ext4_find_extent(inode
,
1414 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1417 err
= PTR_ERR(path
);
1422 * only first (depth 0 -> 1) produces free space;
1423 * in all other cases we have to split the grown tree
1425 depth
= ext_depth(inode
);
1426 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1427 /* now we need to split */
1437 * search the closest allocated block to the left for *logical
1438 * and returns it at @logical + it's physical address at @phys
1439 * if *logical is the smallest allocated block, the function
1440 * returns 0 at @phys
1441 * return value contains 0 (success) or error code
1443 static int ext4_ext_search_left(struct inode
*inode
,
1444 struct ext4_ext_path
*path
,
1445 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1447 struct ext4_extent_idx
*ix
;
1448 struct ext4_extent
*ex
;
1451 if (unlikely(path
== NULL
)) {
1452 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1453 return -EFSCORRUPTED
;
1455 depth
= path
->p_depth
;
1458 if (depth
== 0 && path
->p_ext
== NULL
)
1461 /* usually extent in the path covers blocks smaller
1462 * then *logical, but it can be that extent is the
1463 * first one in the file */
1465 ex
= path
[depth
].p_ext
;
1466 ee_len
= ext4_ext_get_actual_len(ex
);
1467 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1468 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1469 EXT4_ERROR_INODE(inode
,
1470 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1471 *logical
, le32_to_cpu(ex
->ee_block
));
1472 return -EFSCORRUPTED
;
1474 while (--depth
>= 0) {
1475 ix
= path
[depth
].p_idx
;
1476 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1477 EXT4_ERROR_INODE(inode
,
1478 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1479 ix
!= NULL
? le32_to_cpu(ix
->ei_block
) : 0,
1480 EXT_FIRST_INDEX(path
[depth
].p_hdr
) != NULL
?
1481 le32_to_cpu(EXT_FIRST_INDEX(path
[depth
].p_hdr
)->ei_block
) : 0,
1483 return -EFSCORRUPTED
;
1489 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1490 EXT4_ERROR_INODE(inode
,
1491 "logical %d < ee_block %d + ee_len %d!",
1492 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1493 return -EFSCORRUPTED
;
1496 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1497 *phys
= ext4_ext_pblock(ex
) + ee_len
- 1;
1502 * search the closest allocated block to the right for *logical
1503 * and returns it at @logical + it's physical address at @phys
1504 * if *logical is the largest allocated block, the function
1505 * returns 0 at @phys
1506 * return value contains 0 (success) or error code
1508 static int ext4_ext_search_right(struct inode
*inode
,
1509 struct ext4_ext_path
*path
,
1510 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
,
1511 struct ext4_extent
**ret_ex
)
1513 struct buffer_head
*bh
= NULL
;
1514 struct ext4_extent_header
*eh
;
1515 struct ext4_extent_idx
*ix
;
1516 struct ext4_extent
*ex
;
1518 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1521 if (unlikely(path
== NULL
)) {
1522 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1523 return -EFSCORRUPTED
;
1525 depth
= path
->p_depth
;
1528 if (depth
== 0 && path
->p_ext
== NULL
)
1531 /* usually extent in the path covers blocks smaller
1532 * then *logical, but it can be that extent is the
1533 * first one in the file */
1535 ex
= path
[depth
].p_ext
;
1536 ee_len
= ext4_ext_get_actual_len(ex
);
1537 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1538 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1539 EXT4_ERROR_INODE(inode
,
1540 "first_extent(path[%d].p_hdr) != ex",
1542 return -EFSCORRUPTED
;
1544 while (--depth
>= 0) {
1545 ix
= path
[depth
].p_idx
;
1546 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1547 EXT4_ERROR_INODE(inode
,
1548 "ix != EXT_FIRST_INDEX *logical %d!",
1550 return -EFSCORRUPTED
;
1556 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1557 EXT4_ERROR_INODE(inode
,
1558 "logical %d < ee_block %d + ee_len %d!",
1559 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1560 return -EFSCORRUPTED
;
1563 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1564 /* next allocated block in this leaf */
1569 /* go up and search for index to the right */
1570 while (--depth
>= 0) {
1571 ix
= path
[depth
].p_idx
;
1572 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1576 /* we've gone up to the root and found no index to the right */
1580 /* we've found index to the right, let's
1581 * follow it and find the closest allocated
1582 * block to the right */
1584 block
= ext4_idx_pblock(ix
);
1585 while (++depth
< path
->p_depth
) {
1586 /* subtract from p_depth to get proper eh_depth */
1587 bh
= read_extent_tree_block(inode
, block
,
1588 path
->p_depth
- depth
, 0);
1591 eh
= ext_block_hdr(bh
);
1592 ix
= EXT_FIRST_INDEX(eh
);
1593 block
= ext4_idx_pblock(ix
);
1597 bh
= read_extent_tree_block(inode
, block
, path
->p_depth
- depth
, 0);
1600 eh
= ext_block_hdr(bh
);
1601 ex
= EXT_FIRST_EXTENT(eh
);
1603 *logical
= le32_to_cpu(ex
->ee_block
);
1604 *phys
= ext4_ext_pblock(ex
);
1612 * ext4_ext_next_allocated_block:
1613 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1614 * NOTE: it considers block number from index entry as
1615 * allocated block. Thus, index entries have to be consistent
1619 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1623 BUG_ON(path
== NULL
);
1624 depth
= path
->p_depth
;
1626 if (depth
== 0 && path
->p_ext
== NULL
)
1627 return EXT_MAX_BLOCKS
;
1629 while (depth
>= 0) {
1630 if (depth
== path
->p_depth
) {
1632 if (path
[depth
].p_ext
&&
1633 path
[depth
].p_ext
!=
1634 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1635 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1638 if (path
[depth
].p_idx
!=
1639 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1640 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1645 return EXT_MAX_BLOCKS
;
1649 * ext4_ext_next_leaf_block:
1650 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1652 static ext4_lblk_t
ext4_ext_next_leaf_block(struct ext4_ext_path
*path
)
1656 BUG_ON(path
== NULL
);
1657 depth
= path
->p_depth
;
1659 /* zero-tree has no leaf blocks at all */
1661 return EXT_MAX_BLOCKS
;
1663 /* go to index block */
1666 while (depth
>= 0) {
1667 if (path
[depth
].p_idx
!=
1668 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1669 return (ext4_lblk_t
)
1670 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1674 return EXT_MAX_BLOCKS
;
1678 * ext4_ext_correct_indexes:
1679 * if leaf gets modified and modified extent is first in the leaf,
1680 * then we have to correct all indexes above.
1681 * TODO: do we need to correct tree in all cases?
1683 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1684 struct ext4_ext_path
*path
)
1686 struct ext4_extent_header
*eh
;
1687 int depth
= ext_depth(inode
);
1688 struct ext4_extent
*ex
;
1692 eh
= path
[depth
].p_hdr
;
1693 ex
= path
[depth
].p_ext
;
1695 if (unlikely(ex
== NULL
|| eh
== NULL
)) {
1696 EXT4_ERROR_INODE(inode
,
1697 "ex %p == NULL or eh %p == NULL", ex
, eh
);
1698 return -EFSCORRUPTED
;
1702 /* there is no tree at all */
1706 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1707 /* we correct tree if first leaf got modified only */
1712 * TODO: we need correction if border is smaller than current one
1715 border
= path
[depth
].p_ext
->ee_block
;
1716 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1719 path
[k
].p_idx
->ei_block
= border
;
1720 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1725 /* change all left-side indexes */
1726 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1728 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1731 path
[k
].p_idx
->ei_block
= border
;
1732 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1741 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1742 struct ext4_extent
*ex2
)
1744 unsigned short ext1_ee_len
, ext2_ee_len
;
1746 if (ext4_ext_is_unwritten(ex1
) != ext4_ext_is_unwritten(ex2
))
1749 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1750 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1752 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1753 le32_to_cpu(ex2
->ee_block
))
1757 * To allow future support for preallocated extents to be added
1758 * as an RO_COMPAT feature, refuse to merge to extents if
1759 * this can result in the top bit of ee_len being set.
1761 if (ext1_ee_len
+ ext2_ee_len
> EXT_INIT_MAX_LEN
)
1764 * The check for IO to unwritten extent is somewhat racy as we
1765 * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after
1766 * dropping i_data_sem. But reserved blocks should save us in that
1769 if (ext4_ext_is_unwritten(ex1
) &&
1770 (ext4_test_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
) ||
1771 atomic_read(&EXT4_I(inode
)->i_unwritten
) ||
1772 (ext1_ee_len
+ ext2_ee_len
> EXT_UNWRITTEN_MAX_LEN
)))
1774 #ifdef AGGRESSIVE_TEST
1775 if (ext1_ee_len
>= 4)
1779 if (ext4_ext_pblock(ex1
) + ext1_ee_len
== ext4_ext_pblock(ex2
))
1785 * This function tries to merge the "ex" extent to the next extent in the tree.
1786 * It always tries to merge towards right. If you want to merge towards
1787 * left, pass "ex - 1" as argument instead of "ex".
1788 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1789 * 1 if they got merged.
1791 static int ext4_ext_try_to_merge_right(struct inode
*inode
,
1792 struct ext4_ext_path
*path
,
1793 struct ext4_extent
*ex
)
1795 struct ext4_extent_header
*eh
;
1796 unsigned int depth
, len
;
1797 int merge_done
= 0, unwritten
;
1799 depth
= ext_depth(inode
);
1800 BUG_ON(path
[depth
].p_hdr
== NULL
);
1801 eh
= path
[depth
].p_hdr
;
1803 while (ex
< EXT_LAST_EXTENT(eh
)) {
1804 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1806 /* merge with next extent! */
1807 unwritten
= ext4_ext_is_unwritten(ex
);
1808 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1809 + ext4_ext_get_actual_len(ex
+ 1));
1811 ext4_ext_mark_unwritten(ex
);
1813 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1814 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1815 * sizeof(struct ext4_extent
);
1816 memmove(ex
+ 1, ex
+ 2, len
);
1818 le16_add_cpu(&eh
->eh_entries
, -1);
1820 WARN_ON(eh
->eh_entries
== 0);
1821 if (!eh
->eh_entries
)
1822 EXT4_ERROR_INODE(inode
, "eh->eh_entries = 0!");
1829 * This function does a very simple check to see if we can collapse
1830 * an extent tree with a single extent tree leaf block into the inode.
1832 static void ext4_ext_try_to_merge_up(handle_t
*handle
,
1833 struct inode
*inode
,
1834 struct ext4_ext_path
*path
)
1837 unsigned max_root
= ext4_ext_space_root(inode
, 0);
1840 if ((path
[0].p_depth
!= 1) ||
1841 (le16_to_cpu(path
[0].p_hdr
->eh_entries
) != 1) ||
1842 (le16_to_cpu(path
[1].p_hdr
->eh_entries
) > max_root
))
1846 * We need to modify the block allocation bitmap and the block
1847 * group descriptor to release the extent tree block. If we
1848 * can't get the journal credits, give up.
1850 if (ext4_journal_extend(handle
, 2))
1854 * Copy the extent data up to the inode
1856 blk
= ext4_idx_pblock(path
[0].p_idx
);
1857 s
= le16_to_cpu(path
[1].p_hdr
->eh_entries
) *
1858 sizeof(struct ext4_extent_idx
);
1859 s
+= sizeof(struct ext4_extent_header
);
1861 path
[1].p_maxdepth
= path
[0].p_maxdepth
;
1862 memcpy(path
[0].p_hdr
, path
[1].p_hdr
, s
);
1863 path
[0].p_depth
= 0;
1864 path
[0].p_ext
= EXT_FIRST_EXTENT(path
[0].p_hdr
) +
1865 (path
[1].p_ext
- EXT_FIRST_EXTENT(path
[1].p_hdr
));
1866 path
[0].p_hdr
->eh_max
= cpu_to_le16(max_root
);
1868 brelse(path
[1].p_bh
);
1869 ext4_free_blocks(handle
, inode
, NULL
, blk
, 1,
1870 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
1874 * This function tries to merge the @ex extent to neighbours in the tree.
1875 * return 1 if merge left else 0.
1877 static void ext4_ext_try_to_merge(handle_t
*handle
,
1878 struct inode
*inode
,
1879 struct ext4_ext_path
*path
,
1880 struct ext4_extent
*ex
) {
1881 struct ext4_extent_header
*eh
;
1885 depth
= ext_depth(inode
);
1886 BUG_ON(path
[depth
].p_hdr
== NULL
);
1887 eh
= path
[depth
].p_hdr
;
1889 if (ex
> EXT_FIRST_EXTENT(eh
))
1890 merge_done
= ext4_ext_try_to_merge_right(inode
, path
, ex
- 1);
1893 (void) ext4_ext_try_to_merge_right(inode
, path
, ex
);
1895 ext4_ext_try_to_merge_up(handle
, inode
, path
);
1899 * check if a portion of the "newext" extent overlaps with an
1902 * If there is an overlap discovered, it updates the length of the newext
1903 * such that there will be no overlap, and then returns 1.
1904 * If there is no overlap found, it returns 0.
1906 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info
*sbi
,
1907 struct inode
*inode
,
1908 struct ext4_extent
*newext
,
1909 struct ext4_ext_path
*path
)
1912 unsigned int depth
, len1
;
1913 unsigned int ret
= 0;
1915 b1
= le32_to_cpu(newext
->ee_block
);
1916 len1
= ext4_ext_get_actual_len(newext
);
1917 depth
= ext_depth(inode
);
1918 if (!path
[depth
].p_ext
)
1920 b2
= EXT4_LBLK_CMASK(sbi
, le32_to_cpu(path
[depth
].p_ext
->ee_block
));
1923 * get the next allocated block if the extent in the path
1924 * is before the requested block(s)
1927 b2
= ext4_ext_next_allocated_block(path
);
1928 if (b2
== EXT_MAX_BLOCKS
)
1930 b2
= EXT4_LBLK_CMASK(sbi
, b2
);
1933 /* check for wrap through zero on extent logical start block*/
1934 if (b1
+ len1
< b1
) {
1935 len1
= EXT_MAX_BLOCKS
- b1
;
1936 newext
->ee_len
= cpu_to_le16(len1
);
1940 /* check for overlap */
1941 if (b1
+ len1
> b2
) {
1942 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1950 * ext4_ext_insert_extent:
1951 * tries to merge requsted extent into the existing extent or
1952 * inserts requested extent as new one into the tree,
1953 * creating new leaf in the no-space case.
1955 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1956 struct ext4_ext_path
**ppath
,
1957 struct ext4_extent
*newext
, int gb_flags
)
1959 struct ext4_ext_path
*path
= *ppath
;
1960 struct ext4_extent_header
*eh
;
1961 struct ext4_extent
*ex
, *fex
;
1962 struct ext4_extent
*nearex
; /* nearest extent */
1963 struct ext4_ext_path
*npath
= NULL
;
1964 int depth
, len
, err
;
1966 int mb_flags
= 0, unwritten
;
1968 if (gb_flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
1969 mb_flags
|= EXT4_MB_DELALLOC_RESERVED
;
1970 if (unlikely(ext4_ext_get_actual_len(newext
) == 0)) {
1971 EXT4_ERROR_INODE(inode
, "ext4_ext_get_actual_len(newext) == 0");
1972 return -EFSCORRUPTED
;
1974 depth
= ext_depth(inode
);
1975 ex
= path
[depth
].p_ext
;
1976 eh
= path
[depth
].p_hdr
;
1977 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1978 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1979 return -EFSCORRUPTED
;
1982 /* try to insert block into found extent and return */
1983 if (ex
&& !(gb_flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
1986 * Try to see whether we should rather test the extent on
1987 * right from ex, or from the left of ex. This is because
1988 * ext4_find_extent() can return either extent on the
1989 * left, or on the right from the searched position. This
1990 * will make merging more effective.
1992 if (ex
< EXT_LAST_EXTENT(eh
) &&
1993 (le32_to_cpu(ex
->ee_block
) +
1994 ext4_ext_get_actual_len(ex
) <
1995 le32_to_cpu(newext
->ee_block
))) {
1998 } else if ((ex
> EXT_FIRST_EXTENT(eh
)) &&
1999 (le32_to_cpu(newext
->ee_block
) +
2000 ext4_ext_get_actual_len(newext
) <
2001 le32_to_cpu(ex
->ee_block
)))
2004 /* Try to append newex to the ex */
2005 if (ext4_can_extents_be_merged(inode
, ex
, newext
)) {
2006 ext_debug("append [%d]%d block to %u:[%d]%d"
2008 ext4_ext_is_unwritten(newext
),
2009 ext4_ext_get_actual_len(newext
),
2010 le32_to_cpu(ex
->ee_block
),
2011 ext4_ext_is_unwritten(ex
),
2012 ext4_ext_get_actual_len(ex
),
2013 ext4_ext_pblock(ex
));
2014 err
= ext4_ext_get_access(handle
, inode
,
2018 unwritten
= ext4_ext_is_unwritten(ex
);
2019 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
2020 + ext4_ext_get_actual_len(newext
));
2022 ext4_ext_mark_unwritten(ex
);
2023 eh
= path
[depth
].p_hdr
;
2029 /* Try to prepend newex to the ex */
2030 if (ext4_can_extents_be_merged(inode
, newext
, ex
)) {
2031 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2033 le32_to_cpu(newext
->ee_block
),
2034 ext4_ext_is_unwritten(newext
),
2035 ext4_ext_get_actual_len(newext
),
2036 le32_to_cpu(ex
->ee_block
),
2037 ext4_ext_is_unwritten(ex
),
2038 ext4_ext_get_actual_len(ex
),
2039 ext4_ext_pblock(ex
));
2040 err
= ext4_ext_get_access(handle
, inode
,
2045 unwritten
= ext4_ext_is_unwritten(ex
);
2046 ex
->ee_block
= newext
->ee_block
;
2047 ext4_ext_store_pblock(ex
, ext4_ext_pblock(newext
));
2048 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
2049 + ext4_ext_get_actual_len(newext
));
2051 ext4_ext_mark_unwritten(ex
);
2052 eh
= path
[depth
].p_hdr
;
2058 depth
= ext_depth(inode
);
2059 eh
= path
[depth
].p_hdr
;
2060 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
2063 /* probably next leaf has space for us? */
2064 fex
= EXT_LAST_EXTENT(eh
);
2065 next
= EXT_MAX_BLOCKS
;
2066 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
))
2067 next
= ext4_ext_next_leaf_block(path
);
2068 if (next
!= EXT_MAX_BLOCKS
) {
2069 ext_debug("next leaf block - %u\n", next
);
2070 BUG_ON(npath
!= NULL
);
2071 npath
= ext4_find_extent(inode
, next
, NULL
, 0);
2073 return PTR_ERR(npath
);
2074 BUG_ON(npath
->p_depth
!= path
->p_depth
);
2075 eh
= npath
[depth
].p_hdr
;
2076 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
2077 ext_debug("next leaf isn't full(%d)\n",
2078 le16_to_cpu(eh
->eh_entries
));
2082 ext_debug("next leaf has no free space(%d,%d)\n",
2083 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
2087 * There is no free space in the found leaf.
2088 * We're gonna add a new leaf in the tree.
2090 if (gb_flags
& EXT4_GET_BLOCKS_METADATA_NOFAIL
)
2091 mb_flags
|= EXT4_MB_USE_RESERVED
;
2092 err
= ext4_ext_create_new_leaf(handle
, inode
, mb_flags
, gb_flags
,
2096 depth
= ext_depth(inode
);
2097 eh
= path
[depth
].p_hdr
;
2100 nearex
= path
[depth
].p_ext
;
2102 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2107 /* there is no extent in this leaf, create first one */
2108 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
2109 le32_to_cpu(newext
->ee_block
),
2110 ext4_ext_pblock(newext
),
2111 ext4_ext_is_unwritten(newext
),
2112 ext4_ext_get_actual_len(newext
));
2113 nearex
= EXT_FIRST_EXTENT(eh
);
2115 if (le32_to_cpu(newext
->ee_block
)
2116 > le32_to_cpu(nearex
->ee_block
)) {
2118 ext_debug("insert %u:%llu:[%d]%d before: "
2120 le32_to_cpu(newext
->ee_block
),
2121 ext4_ext_pblock(newext
),
2122 ext4_ext_is_unwritten(newext
),
2123 ext4_ext_get_actual_len(newext
),
2128 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
2129 ext_debug("insert %u:%llu:[%d]%d after: "
2131 le32_to_cpu(newext
->ee_block
),
2132 ext4_ext_pblock(newext
),
2133 ext4_ext_is_unwritten(newext
),
2134 ext4_ext_get_actual_len(newext
),
2137 len
= EXT_LAST_EXTENT(eh
) - nearex
+ 1;
2139 ext_debug("insert %u:%llu:[%d]%d: "
2140 "move %d extents from 0x%p to 0x%p\n",
2141 le32_to_cpu(newext
->ee_block
),
2142 ext4_ext_pblock(newext
),
2143 ext4_ext_is_unwritten(newext
),
2144 ext4_ext_get_actual_len(newext
),
2145 len
, nearex
, nearex
+ 1);
2146 memmove(nearex
+ 1, nearex
,
2147 len
* sizeof(struct ext4_extent
));
2151 le16_add_cpu(&eh
->eh_entries
, 1);
2152 path
[depth
].p_ext
= nearex
;
2153 nearex
->ee_block
= newext
->ee_block
;
2154 ext4_ext_store_pblock(nearex
, ext4_ext_pblock(newext
));
2155 nearex
->ee_len
= newext
->ee_len
;
2158 /* try to merge extents */
2159 if (!(gb_flags
& EXT4_GET_BLOCKS_PRE_IO
))
2160 ext4_ext_try_to_merge(handle
, inode
, path
, nearex
);
2163 /* time to correct all indexes above */
2164 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2168 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
2171 ext4_ext_drop_refs(npath
);
2176 static int ext4_fill_fiemap_extents(struct inode
*inode
,
2177 ext4_lblk_t block
, ext4_lblk_t num
,
2178 struct fiemap_extent_info
*fieinfo
)
2180 struct ext4_ext_path
*path
= NULL
;
2181 struct ext4_extent
*ex
;
2182 struct extent_status es
;
2183 ext4_lblk_t next
, next_del
, start
= 0, end
= 0;
2184 ext4_lblk_t last
= block
+ num
;
2185 int exists
, depth
= 0, err
= 0;
2186 unsigned int flags
= 0;
2187 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
2189 while (block
< last
&& block
!= EXT_MAX_BLOCKS
) {
2191 /* find extent for this block */
2192 down_read(&EXT4_I(inode
)->i_data_sem
);
2194 path
= ext4_find_extent(inode
, block
, &path
, 0);
2196 up_read(&EXT4_I(inode
)->i_data_sem
);
2197 err
= PTR_ERR(path
);
2202 depth
= ext_depth(inode
);
2203 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2204 up_read(&EXT4_I(inode
)->i_data_sem
);
2205 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2206 err
= -EFSCORRUPTED
;
2209 ex
= path
[depth
].p_ext
;
2210 next
= ext4_ext_next_allocated_block(path
);
2215 /* there is no extent yet, so try to allocate
2216 * all requested space */
2219 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
2220 /* need to allocate space before found extent */
2222 end
= le32_to_cpu(ex
->ee_block
);
2223 if (block
+ num
< end
)
2225 } else if (block
>= le32_to_cpu(ex
->ee_block
)
2226 + ext4_ext_get_actual_len(ex
)) {
2227 /* need to allocate space after found extent */
2232 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
2234 * some part of requested space is covered
2238 end
= le32_to_cpu(ex
->ee_block
)
2239 + ext4_ext_get_actual_len(ex
);
2240 if (block
+ num
< end
)
2246 BUG_ON(end
<= start
);
2250 es
.es_len
= end
- start
;
2253 es
.es_lblk
= le32_to_cpu(ex
->ee_block
);
2254 es
.es_len
= ext4_ext_get_actual_len(ex
);
2255 es
.es_pblk
= ext4_ext_pblock(ex
);
2256 if (ext4_ext_is_unwritten(ex
))
2257 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2261 * Find delayed extent and update es accordingly. We call
2262 * it even in !exists case to find out whether es is the
2263 * last existing extent or not.
2265 next_del
= ext4_find_delayed_extent(inode
, &es
);
2266 if (!exists
&& next_del
) {
2268 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2269 FIEMAP_EXTENT_UNKNOWN
);
2271 up_read(&EXT4_I(inode
)->i_data_sem
);
2273 if (unlikely(es
.es_len
== 0)) {
2274 EXT4_ERROR_INODE(inode
, "es.es_len == 0");
2275 err
= -EFSCORRUPTED
;
2280 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2281 * we need to check next == EXT_MAX_BLOCKS because it is
2282 * possible that an extent is with unwritten and delayed
2283 * status due to when an extent is delayed allocated and
2284 * is allocated by fallocate status tree will track both of
2287 * So we could return a unwritten and delayed extent, and
2288 * its block is equal to 'next'.
2290 if (next
== next_del
&& next
== EXT_MAX_BLOCKS
) {
2291 flags
|= FIEMAP_EXTENT_LAST
;
2292 if (unlikely(next_del
!= EXT_MAX_BLOCKS
||
2293 next
!= EXT_MAX_BLOCKS
)) {
2294 EXT4_ERROR_INODE(inode
,
2295 "next extent == %u, next "
2296 "delalloc extent = %u",
2298 err
= -EFSCORRUPTED
;
2304 err
= fiemap_fill_next_extent(fieinfo
,
2305 (__u64
)es
.es_lblk
<< blksize_bits
,
2306 (__u64
)es
.es_pblk
<< blksize_bits
,
2307 (__u64
)es
.es_len
<< blksize_bits
,
2317 block
= es
.es_lblk
+ es
.es_len
;
2320 ext4_ext_drop_refs(path
);
2326 * ext4_ext_determine_hole - determine hole around given block
2327 * @inode: inode we lookup in
2328 * @path: path in extent tree to @lblk
2329 * @lblk: pointer to logical block around which we want to determine hole
2331 * Determine hole length (and start if easily possible) around given logical
2332 * block. We don't try too hard to find the beginning of the hole but @path
2333 * actually points to extent before @lblk, we provide it.
2335 * The function returns the length of a hole starting at @lblk. We update @lblk
2336 * to the beginning of the hole if we managed to find it.
2338 static ext4_lblk_t
ext4_ext_determine_hole(struct inode
*inode
,
2339 struct ext4_ext_path
*path
,
2342 int depth
= ext_depth(inode
);
2343 struct ext4_extent
*ex
;
2346 ex
= path
[depth
].p_ext
;
2348 /* there is no extent yet, so gap is [0;-] */
2350 len
= EXT_MAX_BLOCKS
;
2351 } else if (*lblk
< le32_to_cpu(ex
->ee_block
)) {
2352 len
= le32_to_cpu(ex
->ee_block
) - *lblk
;
2353 } else if (*lblk
>= le32_to_cpu(ex
->ee_block
)
2354 + ext4_ext_get_actual_len(ex
)) {
2357 *lblk
= le32_to_cpu(ex
->ee_block
) + ext4_ext_get_actual_len(ex
);
2358 next
= ext4_ext_next_allocated_block(path
);
2359 BUG_ON(next
== *lblk
);
2368 * ext4_ext_put_gap_in_cache:
2369 * calculate boundaries of the gap that the requested block fits into
2370 * and cache this gap
2373 ext4_ext_put_gap_in_cache(struct inode
*inode
, ext4_lblk_t hole_start
,
2374 ext4_lblk_t hole_len
)
2376 struct extent_status es
;
2378 ext4_es_find_delayed_extent_range(inode
, hole_start
,
2379 hole_start
+ hole_len
- 1, &es
);
2381 /* There's delayed extent containing lblock? */
2382 if (es
.es_lblk
<= hole_start
)
2384 hole_len
= min(es
.es_lblk
- hole_start
, hole_len
);
2386 ext_debug(" -> %u:%u\n", hole_start
, hole_len
);
2387 ext4_es_insert_extent(inode
, hole_start
, hole_len
, ~0,
2388 EXTENT_STATUS_HOLE
);
2393 * removes index from the index block.
2395 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
2396 struct ext4_ext_path
*path
, int depth
)
2401 /* free index block */
2403 path
= path
+ depth
;
2404 leaf
= ext4_idx_pblock(path
->p_idx
);
2405 if (unlikely(path
->p_hdr
->eh_entries
== 0)) {
2406 EXT4_ERROR_INODE(inode
, "path->p_hdr->eh_entries == 0");
2407 return -EFSCORRUPTED
;
2409 err
= ext4_ext_get_access(handle
, inode
, path
);
2413 if (path
->p_idx
!= EXT_LAST_INDEX(path
->p_hdr
)) {
2414 int len
= EXT_LAST_INDEX(path
->p_hdr
) - path
->p_idx
;
2415 len
*= sizeof(struct ext4_extent_idx
);
2416 memmove(path
->p_idx
, path
->p_idx
+ 1, len
);
2419 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
2420 err
= ext4_ext_dirty(handle
, inode
, path
);
2423 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
2424 trace_ext4_ext_rm_idx(inode
, leaf
);
2426 ext4_free_blocks(handle
, inode
, NULL
, leaf
, 1,
2427 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
2429 while (--depth
>= 0) {
2430 if (path
->p_idx
!= EXT_FIRST_INDEX(path
->p_hdr
))
2433 err
= ext4_ext_get_access(handle
, inode
, path
);
2436 path
->p_idx
->ei_block
= (path
+1)->p_idx
->ei_block
;
2437 err
= ext4_ext_dirty(handle
, inode
, path
);
2445 * ext4_ext_calc_credits_for_single_extent:
2446 * This routine returns max. credits that needed to insert an extent
2447 * to the extent tree.
2448 * When pass the actual path, the caller should calculate credits
2451 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2452 struct ext4_ext_path
*path
)
2455 int depth
= ext_depth(inode
);
2458 /* probably there is space in leaf? */
2459 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2460 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2463 * There are some space in the leaf tree, no
2464 * need to account for leaf block credit
2466 * bitmaps and block group descriptor blocks
2467 * and other metadata blocks still need to be
2470 /* 1 bitmap, 1 block group descriptor */
2471 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2476 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2480 * How many index/leaf blocks need to change/allocate to add @extents extents?
2482 * If we add a single extent, then in the worse case, each tree level
2483 * index/leaf need to be changed in case of the tree split.
2485 * If more extents are inserted, they could cause the whole tree split more
2486 * than once, but this is really rare.
2488 int ext4_ext_index_trans_blocks(struct inode
*inode
, int extents
)
2493 /* If we are converting the inline data, only one is needed here. */
2494 if (ext4_has_inline_data(inode
))
2497 depth
= ext_depth(inode
);
2507 static inline int get_default_free_blocks_flags(struct inode
*inode
)
2509 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
) ||
2510 ext4_test_inode_flag(inode
, EXT4_INODE_EA_INODE
))
2511 return EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
;
2512 else if (ext4_should_journal_data(inode
))
2513 return EXT4_FREE_BLOCKS_FORGET
;
2517 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2518 struct ext4_extent
*ex
,
2519 long long *partial_cluster
,
2520 ext4_lblk_t from
, ext4_lblk_t to
)
2522 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2523 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2525 int flags
= get_default_free_blocks_flags(inode
);
2528 * For bigalloc file systems, we never free a partial cluster
2529 * at the beginning of the extent. Instead, we make a note
2530 * that we tried freeing the cluster, and check to see if we
2531 * need to free it on a subsequent call to ext4_remove_blocks,
2532 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
2534 flags
|= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
;
2536 trace_ext4_remove_blocks(inode
, ex
, from
, to
, *partial_cluster
);
2538 * If we have a partial cluster, and it's different from the
2539 * cluster of the last block, we need to explicitly free the
2540 * partial cluster here.
2542 pblk
= ext4_ext_pblock(ex
) + ee_len
- 1;
2543 if (*partial_cluster
> 0 &&
2544 *partial_cluster
!= (long long) EXT4_B2C(sbi
, pblk
)) {
2545 ext4_free_blocks(handle
, inode
, NULL
,
2546 EXT4_C2B(sbi
, *partial_cluster
),
2547 sbi
->s_cluster_ratio
, flags
);
2548 *partial_cluster
= 0;
2551 #ifdef EXTENTS_STATS
2553 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2554 spin_lock(&sbi
->s_ext_stats_lock
);
2555 sbi
->s_ext_blocks
+= ee_len
;
2556 sbi
->s_ext_extents
++;
2557 if (ee_len
< sbi
->s_ext_min
)
2558 sbi
->s_ext_min
= ee_len
;
2559 if (ee_len
> sbi
->s_ext_max
)
2560 sbi
->s_ext_max
= ee_len
;
2561 if (ext_depth(inode
) > sbi
->s_depth_max
)
2562 sbi
->s_depth_max
= ext_depth(inode
);
2563 spin_unlock(&sbi
->s_ext_stats_lock
);
2566 if (from
>= le32_to_cpu(ex
->ee_block
)
2567 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2570 long long first_cluster
;
2572 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2573 pblk
= ext4_ext_pblock(ex
) + ee_len
- num
;
2575 * Usually we want to free partial cluster at the end of the
2576 * extent, except for the situation when the cluster is still
2577 * used by any other extent (partial_cluster is negative).
2579 if (*partial_cluster
< 0 &&
2580 *partial_cluster
== -(long long) EXT4_B2C(sbi
, pblk
+num
-1))
2581 flags
|= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER
;
2583 ext_debug("free last %u blocks starting %llu partial %lld\n",
2584 num
, pblk
, *partial_cluster
);
2585 ext4_free_blocks(handle
, inode
, NULL
, pblk
, num
, flags
);
2587 * If the block range to be freed didn't start at the
2588 * beginning of a cluster, and we removed the entire
2589 * extent and the cluster is not used by any other extent,
2590 * save the partial cluster here, since we might need to
2591 * delete if we determine that the truncate or punch hole
2592 * operation has removed all of the blocks in the cluster.
2593 * If that cluster is used by another extent, preserve its
2594 * negative value so it isn't freed later on.
2596 * If the whole extent wasn't freed, we've reached the
2597 * start of the truncated/punched region and have finished
2598 * removing blocks. If there's a partial cluster here it's
2599 * shared with the remainder of the extent and is no longer
2600 * a candidate for removal.
2602 if (EXT4_PBLK_COFF(sbi
, pblk
) && ee_len
== num
) {
2603 first_cluster
= (long long) EXT4_B2C(sbi
, pblk
);
2604 if (first_cluster
!= -*partial_cluster
)
2605 *partial_cluster
= first_cluster
;
2607 *partial_cluster
= 0;
2610 ext4_error(sbi
->s_sb
, "strange request: removal(2) "
2612 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2618 * ext4_ext_rm_leaf() Removes the extents associated with the
2619 * blocks appearing between "start" and "end". Both "start"
2620 * and "end" must appear in the same extent or EIO is returned.
2622 * @handle: The journal handle
2623 * @inode: The files inode
2624 * @path: The path to the leaf
2625 * @partial_cluster: The cluster which we'll have to free if all extents
2626 * has been released from it. However, if this value is
2627 * negative, it's a cluster just to the right of the
2628 * punched region and it must not be freed.
2629 * @start: The first block to remove
2630 * @end: The last block to remove
2633 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2634 struct ext4_ext_path
*path
,
2635 long long *partial_cluster
,
2636 ext4_lblk_t start
, ext4_lblk_t end
)
2638 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2639 int err
= 0, correct_index
= 0;
2640 int depth
= ext_depth(inode
), credits
;
2641 struct ext4_extent_header
*eh
;
2644 ext4_lblk_t ex_ee_block
;
2645 unsigned short ex_ee_len
;
2646 unsigned unwritten
= 0;
2647 struct ext4_extent
*ex
;
2650 /* the header must be checked already in ext4_ext_remove_space() */
2651 ext_debug("truncate since %u in leaf to %u\n", start
, end
);
2652 if (!path
[depth
].p_hdr
)
2653 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2654 eh
= path
[depth
].p_hdr
;
2655 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2656 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2657 return -EFSCORRUPTED
;
2659 /* find where to start removing */
2660 ex
= path
[depth
].p_ext
;
2662 ex
= EXT_LAST_EXTENT(eh
);
2664 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2665 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2667 trace_ext4_ext_rm_leaf(inode
, start
, ex
, *partial_cluster
);
2669 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2670 ex_ee_block
+ ex_ee_len
> start
) {
2672 if (ext4_ext_is_unwritten(ex
))
2677 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2678 unwritten
, ex_ee_len
);
2679 path
[depth
].p_ext
= ex
;
2681 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2682 b
= ex_ee_block
+ex_ee_len
- 1 < end
?
2683 ex_ee_block
+ex_ee_len
- 1 : end
;
2685 ext_debug(" border %u:%u\n", a
, b
);
2687 /* If this extent is beyond the end of the hole, skip it */
2688 if (end
< ex_ee_block
) {
2690 * We're going to skip this extent and move to another,
2691 * so note that its first cluster is in use to avoid
2692 * freeing it when removing blocks. Eventually, the
2693 * right edge of the truncated/punched region will
2694 * be just to the left.
2696 if (sbi
->s_cluster_ratio
> 1) {
2697 pblk
= ext4_ext_pblock(ex
);
2699 -(long long) EXT4_B2C(sbi
, pblk
);
2702 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2703 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2705 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2706 EXT4_ERROR_INODE(inode
,
2707 "can not handle truncate %u:%u "
2709 start
, end
, ex_ee_block
,
2710 ex_ee_block
+ ex_ee_len
- 1);
2711 err
= -EFSCORRUPTED
;
2713 } else if (a
!= ex_ee_block
) {
2714 /* remove tail of the extent */
2715 num
= a
- ex_ee_block
;
2717 /* remove whole extent: excellent! */
2721 * 3 for leaf, sb, and inode plus 2 (bmap and group
2722 * descriptor) for each block group; assume two block
2723 * groups plus ex_ee_len/blocks_per_block_group for
2726 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2727 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2729 credits
+= (ext_depth(inode
)) + 1;
2731 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2733 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2737 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2741 err
= ext4_remove_blocks(handle
, inode
, ex
, partial_cluster
,
2747 /* this extent is removed; mark slot entirely unused */
2748 ext4_ext_store_pblock(ex
, 0);
2750 ex
->ee_len
= cpu_to_le16(num
);
2752 * Do not mark unwritten if all the blocks in the
2753 * extent have been removed.
2755 if (unwritten
&& num
)
2756 ext4_ext_mark_unwritten(ex
);
2758 * If the extent was completely released,
2759 * we need to remove it from the leaf
2762 if (end
!= EXT_MAX_BLOCKS
- 1) {
2764 * For hole punching, we need to scoot all the
2765 * extents up when an extent is removed so that
2766 * we dont have blank extents in the middle
2768 memmove(ex
, ex
+1, (EXT_LAST_EXTENT(eh
) - ex
) *
2769 sizeof(struct ext4_extent
));
2771 /* Now get rid of the one at the end */
2772 memset(EXT_LAST_EXTENT(eh
), 0,
2773 sizeof(struct ext4_extent
));
2775 le16_add_cpu(&eh
->eh_entries
, -1);
2778 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2782 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block
, num
,
2783 ext4_ext_pblock(ex
));
2785 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2786 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2789 if (correct_index
&& eh
->eh_entries
)
2790 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2793 * If there's a partial cluster and at least one extent remains in
2794 * the leaf, free the partial cluster if it isn't shared with the
2795 * current extent. If it is shared with the current extent
2796 * we zero partial_cluster because we've reached the start of the
2797 * truncated/punched region and we're done removing blocks.
2799 if (*partial_cluster
> 0 && ex
>= EXT_FIRST_EXTENT(eh
)) {
2800 pblk
= ext4_ext_pblock(ex
) + ex_ee_len
- 1;
2801 if (*partial_cluster
!= (long long) EXT4_B2C(sbi
, pblk
)) {
2802 ext4_free_blocks(handle
, inode
, NULL
,
2803 EXT4_C2B(sbi
, *partial_cluster
),
2804 sbi
->s_cluster_ratio
,
2805 get_default_free_blocks_flags(inode
));
2807 *partial_cluster
= 0;
2810 /* if this leaf is free, then we should
2811 * remove it from index block above */
2812 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2813 err
= ext4_ext_rm_idx(handle
, inode
, path
, depth
);
2820 * ext4_ext_more_to_rm:
2821 * returns 1 if current index has to be freed (even partial)
2824 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2826 BUG_ON(path
->p_idx
== NULL
);
2828 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2832 * if truncate on deeper level happened, it wasn't partial,
2833 * so we have to consider current index for truncation
2835 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2840 int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
,
2843 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2844 int depth
= ext_depth(inode
);
2845 struct ext4_ext_path
*path
= NULL
;
2846 long long partial_cluster
= 0;
2850 ext_debug("truncate since %u to %u\n", start
, end
);
2852 /* probably first extent we're gonna free will be last in block */
2853 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, depth
+ 1);
2855 return PTR_ERR(handle
);
2858 trace_ext4_ext_remove_space(inode
, start
, end
, depth
);
2861 * Check if we are removing extents inside the extent tree. If that
2862 * is the case, we are going to punch a hole inside the extent tree
2863 * so we have to check whether we need to split the extent covering
2864 * the last block to remove so we can easily remove the part of it
2865 * in ext4_ext_rm_leaf().
2867 if (end
< EXT_MAX_BLOCKS
- 1) {
2868 struct ext4_extent
*ex
;
2869 ext4_lblk_t ee_block
, ex_end
, lblk
;
2872 /* find extent for or closest extent to this block */
2873 path
= ext4_find_extent(inode
, end
, NULL
, EXT4_EX_NOCACHE
);
2875 ext4_journal_stop(handle
);
2876 return PTR_ERR(path
);
2878 depth
= ext_depth(inode
);
2879 /* Leaf not may not exist only if inode has no blocks at all */
2880 ex
= path
[depth
].p_ext
;
2883 EXT4_ERROR_INODE(inode
,
2884 "path[%d].p_hdr == NULL",
2886 err
= -EFSCORRUPTED
;
2891 ee_block
= le32_to_cpu(ex
->ee_block
);
2892 ex_end
= ee_block
+ ext4_ext_get_actual_len(ex
) - 1;
2895 * See if the last block is inside the extent, if so split
2896 * the extent at 'end' block so we can easily remove the
2897 * tail of the first part of the split extent in
2898 * ext4_ext_rm_leaf().
2900 if (end
>= ee_block
&& end
< ex_end
) {
2903 * If we're going to split the extent, note that
2904 * the cluster containing the block after 'end' is
2905 * in use to avoid freeing it when removing blocks.
2907 if (sbi
->s_cluster_ratio
> 1) {
2908 pblk
= ext4_ext_pblock(ex
) + end
- ee_block
+ 1;
2910 -(long long) EXT4_B2C(sbi
, pblk
);
2914 * Split the extent in two so that 'end' is the last
2915 * block in the first new extent. Also we should not
2916 * fail removing space due to ENOSPC so try to use
2917 * reserved block if that happens.
2919 err
= ext4_force_split_extent_at(handle
, inode
, &path
,
2924 } else if (sbi
->s_cluster_ratio
> 1 && end
>= ex_end
) {
2926 * If there's an extent to the right its first cluster
2927 * contains the immediate right boundary of the
2928 * truncated/punched region. Set partial_cluster to
2929 * its negative value so it won't be freed if shared
2930 * with the current extent. The end < ee_block case
2931 * is handled in ext4_ext_rm_leaf().
2934 err
= ext4_ext_search_right(inode
, path
, &lblk
, &pblk
,
2940 -(long long) EXT4_B2C(sbi
, pblk
);
2944 * We start scanning from right side, freeing all the blocks
2945 * after i_size and walking into the tree depth-wise.
2947 depth
= ext_depth(inode
);
2952 le16_to_cpu(path
[k
].p_hdr
->eh_entries
)+1;
2954 path
= kcalloc(depth
+ 1, sizeof(struct ext4_ext_path
),
2957 ext4_journal_stop(handle
);
2960 path
[0].p_maxdepth
= path
[0].p_depth
= depth
;
2961 path
[0].p_hdr
= ext_inode_hdr(inode
);
2964 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
, 0)) {
2965 err
= -EFSCORRUPTED
;
2971 while (i
>= 0 && err
== 0) {
2973 /* this is leaf block */
2974 err
= ext4_ext_rm_leaf(handle
, inode
, path
,
2975 &partial_cluster
, start
,
2977 /* root level has p_bh == NULL, brelse() eats this */
2978 brelse(path
[i
].p_bh
);
2979 path
[i
].p_bh
= NULL
;
2984 /* this is index block */
2985 if (!path
[i
].p_hdr
) {
2986 ext_debug("initialize header\n");
2987 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2990 if (!path
[i
].p_idx
) {
2991 /* this level hasn't been touched yet */
2992 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2993 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2994 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2996 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2998 /* we were already here, see at next index */
3002 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3003 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
3005 if (ext4_ext_more_to_rm(path
+ i
)) {
3006 struct buffer_head
*bh
;
3007 /* go to the next level */
3008 ext_debug("move to level %d (block %llu)\n",
3009 i
+ 1, ext4_idx_pblock(path
[i
].p_idx
));
3010 memset(path
+ i
+ 1, 0, sizeof(*path
));
3011 bh
= read_extent_tree_block(inode
,
3012 ext4_idx_pblock(path
[i
].p_idx
), depth
- i
- 1,
3015 /* should we reset i_size? */
3019 /* Yield here to deal with large extent trees.
3020 * Should be a no-op if we did IO above. */
3022 if (WARN_ON(i
+ 1 > depth
)) {
3023 err
= -EFSCORRUPTED
;
3026 path
[i
+ 1].p_bh
= bh
;
3028 /* save actual number of indexes since this
3029 * number is changed at the next iteration */
3030 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
3033 /* we finished processing this index, go up */
3034 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
3035 /* index is empty, remove it;
3036 * handle must be already prepared by the
3037 * truncatei_leaf() */
3038 err
= ext4_ext_rm_idx(handle
, inode
, path
, i
);
3040 /* root level has p_bh == NULL, brelse() eats this */
3041 brelse(path
[i
].p_bh
);
3042 path
[i
].p_bh
= NULL
;
3044 ext_debug("return to level %d\n", i
);
3048 trace_ext4_ext_remove_space_done(inode
, start
, end
, depth
,
3049 partial_cluster
, path
->p_hdr
->eh_entries
);
3052 * If we still have something in the partial cluster and we have removed
3053 * even the first extent, then we should free the blocks in the partial
3054 * cluster as well. (This code will only run when there are no leaves
3055 * to the immediate left of the truncated/punched region.)
3057 if (partial_cluster
> 0 && err
== 0) {
3058 /* don't zero partial_cluster since it's not used afterwards */
3059 ext4_free_blocks(handle
, inode
, NULL
,
3060 EXT4_C2B(sbi
, partial_cluster
),
3061 sbi
->s_cluster_ratio
,
3062 get_default_free_blocks_flags(inode
));
3065 /* TODO: flexible tree reduction should be here */
3066 if (path
->p_hdr
->eh_entries
== 0) {
3068 * truncate to zero freed all the tree,
3069 * so we need to correct eh_depth
3071 err
= ext4_ext_get_access(handle
, inode
, path
);
3073 ext_inode_hdr(inode
)->eh_depth
= 0;
3074 ext_inode_hdr(inode
)->eh_max
=
3075 cpu_to_le16(ext4_ext_space_root(inode
, 0));
3076 err
= ext4_ext_dirty(handle
, inode
, path
);
3080 ext4_ext_drop_refs(path
);
3085 ext4_journal_stop(handle
);
3091 * called at mount time
3093 void ext4_ext_init(struct super_block
*sb
)
3096 * possible initialization would be here
3099 if (ext4_has_feature_extents(sb
)) {
3100 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
3101 printk(KERN_INFO
"EXT4-fs: file extents enabled"
3102 #ifdef AGGRESSIVE_TEST
3103 ", aggressive tests"
3105 #ifdef CHECK_BINSEARCH
3108 #ifdef EXTENTS_STATS
3113 #ifdef EXTENTS_STATS
3114 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
3115 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
3116 EXT4_SB(sb
)->s_ext_max
= 0;
3122 * called at umount time
3124 void ext4_ext_release(struct super_block
*sb
)
3126 if (!ext4_has_feature_extents(sb
))
3129 #ifdef EXTENTS_STATS
3130 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
3131 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3132 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3133 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
3134 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
3135 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3136 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
3141 static int ext4_zeroout_es(struct inode
*inode
, struct ext4_extent
*ex
)
3143 ext4_lblk_t ee_block
;
3144 ext4_fsblk_t ee_pblock
;
3145 unsigned int ee_len
;
3147 ee_block
= le32_to_cpu(ex
->ee_block
);
3148 ee_len
= ext4_ext_get_actual_len(ex
);
3149 ee_pblock
= ext4_ext_pblock(ex
);
3154 return ext4_es_insert_extent(inode
, ee_block
, ee_len
, ee_pblock
,
3155 EXTENT_STATUS_WRITTEN
);
3158 /* FIXME!! we need to try to merge to left or right after zero-out */
3159 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
3161 ext4_fsblk_t ee_pblock
;
3162 unsigned int ee_len
;
3164 ee_len
= ext4_ext_get_actual_len(ex
);
3165 ee_pblock
= ext4_ext_pblock(ex
);
3166 return ext4_issue_zeroout(inode
, le32_to_cpu(ex
->ee_block
), ee_pblock
,
3171 * ext4_split_extent_at() splits an extent at given block.
3173 * @handle: the journal handle
3174 * @inode: the file inode
3175 * @path: the path to the extent
3176 * @split: the logical block where the extent is splitted.
3177 * @split_flags: indicates if the extent could be zeroout if split fails, and
3178 * the states(init or unwritten) of new extents.
3179 * @flags: flags used to insert new extent to extent tree.
3182 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3183 * of which are deterimined by split_flag.
3185 * There are two cases:
3186 * a> the extent are splitted into two extent.
3187 * b> split is not needed, and just mark the extent.
3189 * return 0 on success.
3191 static int ext4_split_extent_at(handle_t
*handle
,
3192 struct inode
*inode
,
3193 struct ext4_ext_path
**ppath
,
3198 struct ext4_ext_path
*path
= *ppath
;
3199 ext4_fsblk_t newblock
;
3200 ext4_lblk_t ee_block
;
3201 struct ext4_extent
*ex
, newex
, orig_ex
, zero_ex
;
3202 struct ext4_extent
*ex2
= NULL
;
3203 unsigned int ee_len
, depth
;
3206 BUG_ON((split_flag
& (EXT4_EXT_DATA_VALID1
| EXT4_EXT_DATA_VALID2
)) ==
3207 (EXT4_EXT_DATA_VALID1
| EXT4_EXT_DATA_VALID2
));
3209 ext_debug("ext4_split_extents_at: inode %lu, logical"
3210 "block %llu\n", inode
->i_ino
, (unsigned long long)split
);
3212 ext4_ext_show_leaf(inode
, path
);
3214 depth
= ext_depth(inode
);
3215 ex
= path
[depth
].p_ext
;
3216 ee_block
= le32_to_cpu(ex
->ee_block
);
3217 ee_len
= ext4_ext_get_actual_len(ex
);
3218 newblock
= split
- ee_block
+ ext4_ext_pblock(ex
);
3220 BUG_ON(split
< ee_block
|| split
>= (ee_block
+ ee_len
));
3221 BUG_ON(!ext4_ext_is_unwritten(ex
) &&
3222 split_flag
& (EXT4_EXT_MAY_ZEROOUT
|
3223 EXT4_EXT_MARK_UNWRIT1
|
3224 EXT4_EXT_MARK_UNWRIT2
));
3226 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3230 if (split
== ee_block
) {
3232 * case b: block @split is the block that the extent begins with
3233 * then we just change the state of the extent, and splitting
3236 if (split_flag
& EXT4_EXT_MARK_UNWRIT2
)
3237 ext4_ext_mark_unwritten(ex
);
3239 ext4_ext_mark_initialized(ex
);
3241 if (!(flags
& EXT4_GET_BLOCKS_PRE_IO
))
3242 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3244 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3249 memcpy(&orig_ex
, ex
, sizeof(orig_ex
));
3250 ex
->ee_len
= cpu_to_le16(split
- ee_block
);
3251 if (split_flag
& EXT4_EXT_MARK_UNWRIT1
)
3252 ext4_ext_mark_unwritten(ex
);
3255 * path may lead to new leaf, not to original leaf any more
3256 * after ext4_ext_insert_extent() returns,
3258 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3260 goto fix_extent_len
;
3263 ex2
->ee_block
= cpu_to_le32(split
);
3264 ex2
->ee_len
= cpu_to_le16(ee_len
- (split
- ee_block
));
3265 ext4_ext_store_pblock(ex2
, newblock
);
3266 if (split_flag
& EXT4_EXT_MARK_UNWRIT2
)
3267 ext4_ext_mark_unwritten(ex2
);
3269 err
= ext4_ext_insert_extent(handle
, inode
, ppath
, &newex
, flags
);
3270 if (err
== -ENOSPC
&& (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
3271 if (split_flag
& (EXT4_EXT_DATA_VALID1
|EXT4_EXT_DATA_VALID2
)) {
3272 if (split_flag
& EXT4_EXT_DATA_VALID1
) {
3273 err
= ext4_ext_zeroout(inode
, ex2
);
3274 zero_ex
.ee_block
= ex2
->ee_block
;
3275 zero_ex
.ee_len
= cpu_to_le16(
3276 ext4_ext_get_actual_len(ex2
));
3277 ext4_ext_store_pblock(&zero_ex
,
3278 ext4_ext_pblock(ex2
));
3280 err
= ext4_ext_zeroout(inode
, ex
);
3281 zero_ex
.ee_block
= ex
->ee_block
;
3282 zero_ex
.ee_len
= cpu_to_le16(
3283 ext4_ext_get_actual_len(ex
));
3284 ext4_ext_store_pblock(&zero_ex
,
3285 ext4_ext_pblock(ex
));
3288 err
= ext4_ext_zeroout(inode
, &orig_ex
);
3289 zero_ex
.ee_block
= orig_ex
.ee_block
;
3290 zero_ex
.ee_len
= cpu_to_le16(
3291 ext4_ext_get_actual_len(&orig_ex
));
3292 ext4_ext_store_pblock(&zero_ex
,
3293 ext4_ext_pblock(&orig_ex
));
3297 goto fix_extent_len
;
3298 /* update the extent length and mark as initialized */
3299 ex
->ee_len
= cpu_to_le16(ee_len
);
3300 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3301 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3303 goto fix_extent_len
;
3305 /* update extent status tree */
3306 err
= ext4_zeroout_es(inode
, &zero_ex
);
3310 goto fix_extent_len
;
3313 ext4_ext_show_leaf(inode
, path
);
3317 ex
->ee_len
= orig_ex
.ee_len
;
3318 ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3323 * ext4_split_extents() splits an extent and mark extent which is covered
3324 * by @map as split_flags indicates
3326 * It may result in splitting the extent into multiple extents (up to three)
3327 * There are three possibilities:
3328 * a> There is no split required
3329 * b> Splits in two extents: Split is happening at either end of the extent
3330 * c> Splits in three extents: Somone is splitting in middle of the extent
3333 static int ext4_split_extent(handle_t
*handle
,
3334 struct inode
*inode
,
3335 struct ext4_ext_path
**ppath
,
3336 struct ext4_map_blocks
*map
,
3340 struct ext4_ext_path
*path
= *ppath
;
3341 ext4_lblk_t ee_block
;
3342 struct ext4_extent
*ex
;
3343 unsigned int ee_len
, depth
;
3346 int split_flag1
, flags1
;
3347 int allocated
= map
->m_len
;
3349 depth
= ext_depth(inode
);
3350 ex
= path
[depth
].p_ext
;
3351 ee_block
= le32_to_cpu(ex
->ee_block
);
3352 ee_len
= ext4_ext_get_actual_len(ex
);
3353 unwritten
= ext4_ext_is_unwritten(ex
);
3355 if (map
->m_lblk
+ map
->m_len
< ee_block
+ ee_len
) {
3356 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
;
3357 flags1
= flags
| EXT4_GET_BLOCKS_PRE_IO
;
3359 split_flag1
|= EXT4_EXT_MARK_UNWRIT1
|
3360 EXT4_EXT_MARK_UNWRIT2
;
3361 if (split_flag
& EXT4_EXT_DATA_VALID2
)
3362 split_flag1
|= EXT4_EXT_DATA_VALID1
;
3363 err
= ext4_split_extent_at(handle
, inode
, ppath
,
3364 map
->m_lblk
+ map
->m_len
, split_flag1
, flags1
);
3368 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3371 * Update path is required because previous ext4_split_extent_at() may
3372 * result in split of original leaf or extent zeroout.
3374 path
= ext4_find_extent(inode
, map
->m_lblk
, ppath
, 0);
3376 return PTR_ERR(path
);
3377 depth
= ext_depth(inode
);
3378 ex
= path
[depth
].p_ext
;
3380 EXT4_ERROR_INODE(inode
, "unexpected hole at %lu",
3381 (unsigned long) map
->m_lblk
);
3382 return -EFSCORRUPTED
;
3384 unwritten
= ext4_ext_is_unwritten(ex
);
3387 if (map
->m_lblk
>= ee_block
) {
3388 split_flag1
= split_flag
& EXT4_EXT_DATA_VALID2
;
3390 split_flag1
|= EXT4_EXT_MARK_UNWRIT1
;
3391 split_flag1
|= split_flag
& (EXT4_EXT_MAY_ZEROOUT
|
3392 EXT4_EXT_MARK_UNWRIT2
);
3394 err
= ext4_split_extent_at(handle
, inode
, ppath
,
3395 map
->m_lblk
, split_flag1
, flags
);
3400 ext4_ext_show_leaf(inode
, path
);
3402 return err
? err
: allocated
;
3406 * This function is called by ext4_ext_map_blocks() if someone tries to write
3407 * to an unwritten extent. It may result in splitting the unwritten
3408 * extent into multiple extents (up to three - one initialized and two
3410 * There are three possibilities:
3411 * a> There is no split required: Entire extent should be initialized
3412 * b> Splits in two extents: Write is happening at either end of the extent
3413 * c> Splits in three extents: Somone is writing in middle of the extent
3416 * - The extent pointed to by 'path' is unwritten.
3417 * - The extent pointed to by 'path' contains a superset
3418 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3420 * Post-conditions on success:
3421 * - the returned value is the number of blocks beyond map->l_lblk
3422 * that are allocated and initialized.
3423 * It is guaranteed to be >= map->m_len.
3425 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
3426 struct inode
*inode
,
3427 struct ext4_map_blocks
*map
,
3428 struct ext4_ext_path
**ppath
,
3431 struct ext4_ext_path
*path
= *ppath
;
3432 struct ext4_sb_info
*sbi
;
3433 struct ext4_extent_header
*eh
;
3434 struct ext4_map_blocks split_map
;
3435 struct ext4_extent zero_ex1
, zero_ex2
;
3436 struct ext4_extent
*ex
, *abut_ex
;
3437 ext4_lblk_t ee_block
, eof_block
;
3438 unsigned int ee_len
, depth
, map_len
= map
->m_len
;
3439 int allocated
= 0, max_zeroout
= 0;
3441 int split_flag
= EXT4_EXT_DATA_VALID2
;
3443 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3444 "block %llu, max_blocks %u\n", inode
->i_ino
,
3445 (unsigned long long)map
->m_lblk
, map_len
);
3447 sbi
= EXT4_SB(inode
->i_sb
);
3448 eof_block
= (EXT4_I(inode
)->i_disksize
+ inode
->i_sb
->s_blocksize
- 1)
3449 >> inode
->i_sb
->s_blocksize_bits
;
3450 if (eof_block
< map
->m_lblk
+ map_len
)
3451 eof_block
= map
->m_lblk
+ map_len
;
3453 depth
= ext_depth(inode
);
3454 eh
= path
[depth
].p_hdr
;
3455 ex
= path
[depth
].p_ext
;
3456 ee_block
= le32_to_cpu(ex
->ee_block
);
3457 ee_len
= ext4_ext_get_actual_len(ex
);
3458 zero_ex1
.ee_len
= 0;
3459 zero_ex2
.ee_len
= 0;
3461 trace_ext4_ext_convert_to_initialized_enter(inode
, map
, ex
);
3463 /* Pre-conditions */
3464 BUG_ON(!ext4_ext_is_unwritten(ex
));
3465 BUG_ON(!in_range(map
->m_lblk
, ee_block
, ee_len
));
3468 * Attempt to transfer newly initialized blocks from the currently
3469 * unwritten extent to its neighbor. This is much cheaper
3470 * than an insertion followed by a merge as those involve costly
3471 * memmove() calls. Transferring to the left is the common case in
3472 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3473 * followed by append writes.
3475 * Limitations of the current logic:
3476 * - L1: we do not deal with writes covering the whole extent.
3477 * This would require removing the extent if the transfer
3479 * - L2: we only attempt to merge with an extent stored in the
3480 * same extent tree node.
3482 if ((map
->m_lblk
== ee_block
) &&
3483 /* See if we can merge left */
3484 (map_len
< ee_len
) && /*L1*/
3485 (ex
> EXT_FIRST_EXTENT(eh
))) { /*L2*/
3486 ext4_lblk_t prev_lblk
;
3487 ext4_fsblk_t prev_pblk
, ee_pblk
;
3488 unsigned int prev_len
;
3491 prev_lblk
= le32_to_cpu(abut_ex
->ee_block
);
3492 prev_len
= ext4_ext_get_actual_len(abut_ex
);
3493 prev_pblk
= ext4_ext_pblock(abut_ex
);
3494 ee_pblk
= ext4_ext_pblock(ex
);
3497 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3498 * upon those conditions:
3499 * - C1: abut_ex is initialized,
3500 * - C2: abut_ex is logically abutting ex,
3501 * - C3: abut_ex is physically abutting ex,
3502 * - C4: abut_ex can receive the additional blocks without
3503 * overflowing the (initialized) length limit.
3505 if ((!ext4_ext_is_unwritten(abut_ex
)) && /*C1*/
3506 ((prev_lblk
+ prev_len
) == ee_block
) && /*C2*/
3507 ((prev_pblk
+ prev_len
) == ee_pblk
) && /*C3*/
3508 (prev_len
< (EXT_INIT_MAX_LEN
- map_len
))) { /*C4*/
3509 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3513 trace_ext4_ext_convert_to_initialized_fastpath(inode
,
3516 /* Shift the start of ex by 'map_len' blocks */
3517 ex
->ee_block
= cpu_to_le32(ee_block
+ map_len
);
3518 ext4_ext_store_pblock(ex
, ee_pblk
+ map_len
);
3519 ex
->ee_len
= cpu_to_le16(ee_len
- map_len
);
3520 ext4_ext_mark_unwritten(ex
); /* Restore the flag */
3522 /* Extend abut_ex by 'map_len' blocks */
3523 abut_ex
->ee_len
= cpu_to_le16(prev_len
+ map_len
);
3525 /* Result: number of initialized blocks past m_lblk */
3526 allocated
= map_len
;
3528 } else if (((map
->m_lblk
+ map_len
) == (ee_block
+ ee_len
)) &&
3529 (map_len
< ee_len
) && /*L1*/
3530 ex
< EXT_LAST_EXTENT(eh
)) { /*L2*/
3531 /* See if we can merge right */
3532 ext4_lblk_t next_lblk
;
3533 ext4_fsblk_t next_pblk
, ee_pblk
;
3534 unsigned int next_len
;
3537 next_lblk
= le32_to_cpu(abut_ex
->ee_block
);
3538 next_len
= ext4_ext_get_actual_len(abut_ex
);
3539 next_pblk
= ext4_ext_pblock(abut_ex
);
3540 ee_pblk
= ext4_ext_pblock(ex
);
3543 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3544 * upon those conditions:
3545 * - C1: abut_ex is initialized,
3546 * - C2: abut_ex is logically abutting ex,
3547 * - C3: abut_ex is physically abutting ex,
3548 * - C4: abut_ex can receive the additional blocks without
3549 * overflowing the (initialized) length limit.
3551 if ((!ext4_ext_is_unwritten(abut_ex
)) && /*C1*/
3552 ((map
->m_lblk
+ map_len
) == next_lblk
) && /*C2*/
3553 ((ee_pblk
+ ee_len
) == next_pblk
) && /*C3*/
3554 (next_len
< (EXT_INIT_MAX_LEN
- map_len
))) { /*C4*/
3555 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3559 trace_ext4_ext_convert_to_initialized_fastpath(inode
,
3562 /* Shift the start of abut_ex by 'map_len' blocks */
3563 abut_ex
->ee_block
= cpu_to_le32(next_lblk
- map_len
);
3564 ext4_ext_store_pblock(abut_ex
, next_pblk
- map_len
);
3565 ex
->ee_len
= cpu_to_le16(ee_len
- map_len
);
3566 ext4_ext_mark_unwritten(ex
); /* Restore the flag */
3568 /* Extend abut_ex by 'map_len' blocks */
3569 abut_ex
->ee_len
= cpu_to_le16(next_len
+ map_len
);
3571 /* Result: number of initialized blocks past m_lblk */
3572 allocated
= map_len
;
3576 /* Mark the block containing both extents as dirty */
3577 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3579 /* Update path to point to the right extent */
3580 path
[depth
].p_ext
= abut_ex
;
3583 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3585 WARN_ON(map
->m_lblk
< ee_block
);
3587 * It is safe to convert extent to initialized via explicit
3588 * zeroout only if extent is fully inside i_size or new_size.
3590 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
3592 if (EXT4_EXT_MAY_ZEROOUT
& split_flag
)
3593 max_zeroout
= sbi
->s_extent_max_zeroout_kb
>>
3594 (inode
->i_sb
->s_blocksize_bits
- 10);
3596 if (ext4_encrypted_inode(inode
))
3601 * 1. split the extent into three extents.
3602 * 2. split the extent into two extents, zeroout the head of the first
3604 * 3. split the extent into two extents, zeroout the tail of the second
3606 * 4. split the extent into two extents with out zeroout.
3607 * 5. no splitting needed, just possibly zeroout the head and / or the
3608 * tail of the extent.
3610 split_map
.m_lblk
= map
->m_lblk
;
3611 split_map
.m_len
= map
->m_len
;
3613 if (max_zeroout
&& (allocated
> split_map
.m_len
)) {
3614 if (allocated
<= max_zeroout
) {
3617 cpu_to_le32(split_map
.m_lblk
+
3620 cpu_to_le16(allocated
- split_map
.m_len
);
3621 ext4_ext_store_pblock(&zero_ex1
,
3622 ext4_ext_pblock(ex
) + split_map
.m_lblk
+
3623 split_map
.m_len
- ee_block
);
3624 err
= ext4_ext_zeroout(inode
, &zero_ex1
);
3627 split_map
.m_len
= allocated
;
3629 if (split_map
.m_lblk
- ee_block
+ split_map
.m_len
<
3632 if (split_map
.m_lblk
!= ee_block
) {
3633 zero_ex2
.ee_block
= ex
->ee_block
;
3634 zero_ex2
.ee_len
= cpu_to_le16(split_map
.m_lblk
-
3636 ext4_ext_store_pblock(&zero_ex2
,
3637 ext4_ext_pblock(ex
));
3638 err
= ext4_ext_zeroout(inode
, &zero_ex2
);
3643 split_map
.m_len
+= split_map
.m_lblk
- ee_block
;
3644 split_map
.m_lblk
= ee_block
;
3645 allocated
= map
->m_len
;
3649 err
= ext4_split_extent(handle
, inode
, ppath
, &split_map
, split_flag
,
3654 /* If we have gotten a failure, don't zero out status tree */
3656 err
= ext4_zeroout_es(inode
, &zero_ex1
);
3658 err
= ext4_zeroout_es(inode
, &zero_ex2
);
3660 return err
? err
: allocated
;
3664 * This function is called by ext4_ext_map_blocks() from
3665 * ext4_get_blocks_dio_write() when DIO to write
3666 * to an unwritten extent.
3668 * Writing to an unwritten extent may result in splitting the unwritten
3669 * extent into multiple initialized/unwritten extents (up to three)
3670 * There are three possibilities:
3671 * a> There is no split required: Entire extent should be unwritten
3672 * b> Splits in two extents: Write is happening at either end of the extent
3673 * c> Splits in three extents: Somone is writing in middle of the extent
3675 * This works the same way in the case of initialized -> unwritten conversion.
3677 * One of more index blocks maybe needed if the extent tree grow after
3678 * the unwritten extent split. To prevent ENOSPC occur at the IO
3679 * complete, we need to split the unwritten extent before DIO submit
3680 * the IO. The unwritten extent called at this time will be split
3681 * into three unwritten extent(at most). After IO complete, the part
3682 * being filled will be convert to initialized by the end_io callback function
3683 * via ext4_convert_unwritten_extents().
3685 * Returns the size of unwritten extent to be written on success.
3687 static int ext4_split_convert_extents(handle_t
*handle
,
3688 struct inode
*inode
,
3689 struct ext4_map_blocks
*map
,
3690 struct ext4_ext_path
**ppath
,
3693 struct ext4_ext_path
*path
= *ppath
;
3694 ext4_lblk_t eof_block
;
3695 ext4_lblk_t ee_block
;
3696 struct ext4_extent
*ex
;
3697 unsigned int ee_len
;
3698 int split_flag
= 0, depth
;
3700 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3701 __func__
, inode
->i_ino
,
3702 (unsigned long long)map
->m_lblk
, map
->m_len
);
3704 eof_block
= (EXT4_I(inode
)->i_disksize
+ inode
->i_sb
->s_blocksize
- 1)
3705 >> inode
->i_sb
->s_blocksize_bits
;
3706 if (eof_block
< map
->m_lblk
+ map
->m_len
)
3707 eof_block
= map
->m_lblk
+ map
->m_len
;
3709 * It is safe to convert extent to initialized via explicit
3710 * zeroout only if extent is fully insde i_size or new_size.
3712 depth
= ext_depth(inode
);
3713 ex
= path
[depth
].p_ext
;
3714 ee_block
= le32_to_cpu(ex
->ee_block
);
3715 ee_len
= ext4_ext_get_actual_len(ex
);
3717 /* Convert to unwritten */
3718 if (flags
& EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
) {
3719 split_flag
|= EXT4_EXT_DATA_VALID1
;
3720 /* Convert to initialized */
3721 } else if (flags
& EXT4_GET_BLOCKS_CONVERT
) {
3722 split_flag
|= ee_block
+ ee_len
<= eof_block
?
3723 EXT4_EXT_MAY_ZEROOUT
: 0;
3724 split_flag
|= (EXT4_EXT_MARK_UNWRIT2
| EXT4_EXT_DATA_VALID2
);
3726 flags
|= EXT4_GET_BLOCKS_PRE_IO
;
3727 return ext4_split_extent(handle
, inode
, ppath
, map
, split_flag
, flags
);
3730 static int ext4_convert_unwritten_extents_endio(handle_t
*handle
,
3731 struct inode
*inode
,
3732 struct ext4_map_blocks
*map
,
3733 struct ext4_ext_path
**ppath
)
3735 struct ext4_ext_path
*path
= *ppath
;
3736 struct ext4_extent
*ex
;
3737 ext4_lblk_t ee_block
;
3738 unsigned int ee_len
;
3742 depth
= ext_depth(inode
);
3743 ex
= path
[depth
].p_ext
;
3744 ee_block
= le32_to_cpu(ex
->ee_block
);
3745 ee_len
= ext4_ext_get_actual_len(ex
);
3747 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3748 "block %llu, max_blocks %u\n", inode
->i_ino
,
3749 (unsigned long long)ee_block
, ee_len
);
3751 /* If extent is larger than requested it is a clear sign that we still
3752 * have some extent state machine issues left. So extent_split is still
3754 * TODO: Once all related issues will be fixed this situation should be
3757 if (ee_block
!= map
->m_lblk
|| ee_len
> map
->m_len
) {
3758 #ifdef CONFIG_EXT4_DEBUG
3759 ext4_warning(inode
->i_sb
, "Inode (%ld) finished: extent logical block %llu,"
3760 " len %u; IO logical block %llu, len %u",
3761 inode
->i_ino
, (unsigned long long)ee_block
, ee_len
,
3762 (unsigned long long)map
->m_lblk
, map
->m_len
);
3764 err
= ext4_split_convert_extents(handle
, inode
, map
, ppath
,
3765 EXT4_GET_BLOCKS_CONVERT
);
3768 path
= ext4_find_extent(inode
, map
->m_lblk
, ppath
, 0);
3770 return PTR_ERR(path
);
3771 depth
= ext_depth(inode
);
3772 ex
= path
[depth
].p_ext
;
3775 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3778 /* first mark the extent as initialized */
3779 ext4_ext_mark_initialized(ex
);
3781 /* note: ext4_ext_correct_indexes() isn't needed here because
3782 * borders are not changed
3784 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3786 /* Mark modified extent as dirty */
3787 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3789 ext4_ext_show_leaf(inode
, path
);
3794 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3796 static int check_eofblocks_fl(handle_t
*handle
, struct inode
*inode
,
3798 struct ext4_ext_path
*path
,
3802 struct ext4_extent_header
*eh
;
3803 struct ext4_extent
*last_ex
;
3805 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))
3808 depth
= ext_depth(inode
);
3809 eh
= path
[depth
].p_hdr
;
3812 * We're going to remove EOFBLOCKS_FL entirely in future so we
3813 * do not care for this case anymore. Simply remove the flag
3814 * if there are no extents.
3816 if (unlikely(!eh
->eh_entries
))
3818 last_ex
= EXT_LAST_EXTENT(eh
);
3820 * We should clear the EOFBLOCKS_FL flag if we are writing the
3821 * last block in the last extent in the file. We test this by
3822 * first checking to see if the caller to
3823 * ext4_ext_get_blocks() was interested in the last block (or
3824 * a block beyond the last block) in the current extent. If
3825 * this turns out to be false, we can bail out from this
3826 * function immediately.
3828 if (lblk
+ len
< le32_to_cpu(last_ex
->ee_block
) +
3829 ext4_ext_get_actual_len(last_ex
))
3832 * If the caller does appear to be planning to write at or
3833 * beyond the end of the current extent, we then test to see
3834 * if the current extent is the last extent in the file, by
3835 * checking to make sure it was reached via the rightmost node
3836 * at each level of the tree.
3838 for (i
= depth
-1; i
>= 0; i
--)
3839 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3842 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3843 return ext4_mark_inode_dirty(handle
, inode
);
3847 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3849 * Return 1 if there is a delalloc block in the range, otherwise 0.
3851 int ext4_find_delalloc_range(struct inode
*inode
,
3852 ext4_lblk_t lblk_start
,
3853 ext4_lblk_t lblk_end
)
3855 struct extent_status es
;
3857 ext4_es_find_delayed_extent_range(inode
, lblk_start
, lblk_end
, &es
);
3859 return 0; /* there is no delay extent in this tree */
3860 else if (es
.es_lblk
<= lblk_start
&&
3861 lblk_start
< es
.es_lblk
+ es
.es_len
)
3863 else if (lblk_start
<= es
.es_lblk
&& es
.es_lblk
<= lblk_end
)
3869 int ext4_find_delalloc_cluster(struct inode
*inode
, ext4_lblk_t lblk
)
3871 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3872 ext4_lblk_t lblk_start
, lblk_end
;
3873 lblk_start
= EXT4_LBLK_CMASK(sbi
, lblk
);
3874 lblk_end
= lblk_start
+ sbi
->s_cluster_ratio
- 1;
3876 return ext4_find_delalloc_range(inode
, lblk_start
, lblk_end
);
3880 * Determines how many complete clusters (out of those specified by the 'map')
3881 * are under delalloc and were reserved quota for.
3882 * This function is called when we are writing out the blocks that were
3883 * originally written with their allocation delayed, but then the space was
3884 * allocated using fallocate() before the delayed allocation could be resolved.
3885 * The cases to look for are:
3886 * ('=' indicated delayed allocated blocks
3887 * '-' indicates non-delayed allocated blocks)
3888 * (a) partial clusters towards beginning and/or end outside of allocated range
3889 * are not delalloc'ed.
3891 * |----c---=|====c====|====c====|===-c----|
3892 * |++++++ allocated ++++++|
3893 * ==> 4 complete clusters in above example
3895 * (b) partial cluster (outside of allocated range) towards either end is
3896 * marked for delayed allocation. In this case, we will exclude that
3899 * |----====c========|========c========|
3900 * |++++++ allocated ++++++|
3901 * ==> 1 complete clusters in above example
3904 * |================c================|
3905 * |++++++ allocated ++++++|
3906 * ==> 0 complete clusters in above example
3908 * The ext4_da_update_reserve_space will be called only if we
3909 * determine here that there were some "entire" clusters that span
3910 * this 'allocated' range.
3911 * In the non-bigalloc case, this function will just end up returning num_blks
3912 * without ever calling ext4_find_delalloc_range.
3915 get_reserved_cluster_alloc(struct inode
*inode
, ext4_lblk_t lblk_start
,
3916 unsigned int num_blks
)
3918 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3919 ext4_lblk_t alloc_cluster_start
, alloc_cluster_end
;
3920 ext4_lblk_t lblk_from
, lblk_to
, c_offset
;
3921 unsigned int allocated_clusters
= 0;
3923 alloc_cluster_start
= EXT4_B2C(sbi
, lblk_start
);
3924 alloc_cluster_end
= EXT4_B2C(sbi
, lblk_start
+ num_blks
- 1);
3926 /* max possible clusters for this allocation */
3927 allocated_clusters
= alloc_cluster_end
- alloc_cluster_start
+ 1;
3929 trace_ext4_get_reserved_cluster_alloc(inode
, lblk_start
, num_blks
);
3931 /* Check towards left side */
3932 c_offset
= EXT4_LBLK_COFF(sbi
, lblk_start
);
3934 lblk_from
= EXT4_LBLK_CMASK(sbi
, lblk_start
);
3935 lblk_to
= lblk_from
+ c_offset
- 1;
3937 if (ext4_find_delalloc_range(inode
, lblk_from
, lblk_to
))
3938 allocated_clusters
--;
3941 /* Now check towards right. */
3942 c_offset
= EXT4_LBLK_COFF(sbi
, lblk_start
+ num_blks
);
3943 if (allocated_clusters
&& c_offset
) {
3944 lblk_from
= lblk_start
+ num_blks
;
3945 lblk_to
= lblk_from
+ (sbi
->s_cluster_ratio
- c_offset
) - 1;
3947 if (ext4_find_delalloc_range(inode
, lblk_from
, lblk_to
))
3948 allocated_clusters
--;
3951 return allocated_clusters
;
3955 convert_initialized_extent(handle_t
*handle
, struct inode
*inode
,
3956 struct ext4_map_blocks
*map
,
3957 struct ext4_ext_path
**ppath
,
3958 unsigned int allocated
)
3960 struct ext4_ext_path
*path
= *ppath
;
3961 struct ext4_extent
*ex
;
3962 ext4_lblk_t ee_block
;
3963 unsigned int ee_len
;
3968 * Make sure that the extent is no bigger than we support with
3971 if (map
->m_len
> EXT_UNWRITTEN_MAX_LEN
)
3972 map
->m_len
= EXT_UNWRITTEN_MAX_LEN
/ 2;
3974 depth
= ext_depth(inode
);
3975 ex
= path
[depth
].p_ext
;
3976 ee_block
= le32_to_cpu(ex
->ee_block
);
3977 ee_len
= ext4_ext_get_actual_len(ex
);
3979 ext_debug("%s: inode %lu, logical"
3980 "block %llu, max_blocks %u\n", __func__
, inode
->i_ino
,
3981 (unsigned long long)ee_block
, ee_len
);
3983 if (ee_block
!= map
->m_lblk
|| ee_len
> map
->m_len
) {
3984 err
= ext4_split_convert_extents(handle
, inode
, map
, ppath
,
3985 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
);
3988 path
= ext4_find_extent(inode
, map
->m_lblk
, ppath
, 0);
3990 return PTR_ERR(path
);
3991 depth
= ext_depth(inode
);
3992 ex
= path
[depth
].p_ext
;
3994 EXT4_ERROR_INODE(inode
, "unexpected hole at %lu",
3995 (unsigned long) map
->m_lblk
);
3996 return -EFSCORRUPTED
;
4000 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
4003 /* first mark the extent as unwritten */
4004 ext4_ext_mark_unwritten(ex
);
4006 /* note: ext4_ext_correct_indexes() isn't needed here because
4007 * borders are not changed
4009 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
4011 /* Mark modified extent as dirty */
4012 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
4015 ext4_ext_show_leaf(inode
, path
);
4017 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4018 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
, map
->m_len
);
4021 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4022 if (allocated
> map
->m_len
)
4023 allocated
= map
->m_len
;
4024 map
->m_len
= allocated
;
4029 ext4_ext_handle_unwritten_extents(handle_t
*handle
, struct inode
*inode
,
4030 struct ext4_map_blocks
*map
,
4031 struct ext4_ext_path
**ppath
, int flags
,
4032 unsigned int allocated
, ext4_fsblk_t newblock
)
4034 struct ext4_ext_path
*path
= *ppath
;
4038 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
4039 "block %llu, max_blocks %u, flags %x, allocated %u\n",
4040 inode
->i_ino
, (unsigned long long)map
->m_lblk
, map
->m_len
,
4042 ext4_ext_show_leaf(inode
, path
);
4045 * When writing into unwritten space, we should not fail to
4046 * allocate metadata blocks for the new extent block if needed.
4048 flags
|= EXT4_GET_BLOCKS_METADATA_NOFAIL
;
4050 trace_ext4_ext_handle_unwritten_extents(inode
, map
, flags
,
4051 allocated
, newblock
);
4053 /* get_block() before submit the IO, split the extent */
4054 if (flags
& EXT4_GET_BLOCKS_PRE_IO
) {
4055 ret
= ext4_split_convert_extents(handle
, inode
, map
, ppath
,
4056 flags
| EXT4_GET_BLOCKS_CONVERT
);
4059 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4062 /* IO end_io complete, convert the filled extent to written */
4063 if (flags
& EXT4_GET_BLOCKS_CONVERT
) {
4064 if (flags
& EXT4_GET_BLOCKS_ZERO
) {
4065 if (allocated
> map
->m_len
)
4066 allocated
= map
->m_len
;
4067 err
= ext4_issue_zeroout(inode
, map
->m_lblk
, newblock
,
4072 ret
= ext4_convert_unwritten_extents_endio(handle
, inode
, map
,
4075 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4076 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
4080 map
->m_flags
|= EXT4_MAP_MAPPED
;
4081 map
->m_pblk
= newblock
;
4082 if (allocated
> map
->m_len
)
4083 allocated
= map
->m_len
;
4084 map
->m_len
= allocated
;
4087 /* buffered IO case */
4089 * repeat fallocate creation request
4090 * we already have an unwritten extent
4092 if (flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
) {
4093 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4097 /* buffered READ or buffered write_begin() lookup */
4098 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
4100 * We have blocks reserved already. We
4101 * return allocated blocks so that delalloc
4102 * won't do block reservation for us. But
4103 * the buffer head will be unmapped so that
4104 * a read from the block returns 0s.
4106 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4110 /* buffered write, writepage time, convert*/
4111 ret
= ext4_ext_convert_to_initialized(handle
, inode
, map
, ppath
, flags
);
4113 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4120 map
->m_flags
|= EXT4_MAP_NEW
;
4122 * if we allocated more blocks than requested
4123 * we need to make sure we unmap the extra block
4124 * allocated. The actual needed block will get
4125 * unmapped later when we find the buffer_head marked
4128 if (allocated
> map
->m_len
) {
4129 clean_bdev_aliases(inode
->i_sb
->s_bdev
, newblock
+ map
->m_len
,
4130 allocated
- map
->m_len
);
4131 allocated
= map
->m_len
;
4133 map
->m_len
= allocated
;
4136 * If we have done fallocate with the offset that is already
4137 * delayed allocated, we would have block reservation
4138 * and quota reservation done in the delayed write path.
4139 * But fallocate would have already updated quota and block
4140 * count for this offset. So cancel these reservation
4142 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) {
4143 unsigned int reserved_clusters
;
4144 reserved_clusters
= get_reserved_cluster_alloc(inode
,
4145 map
->m_lblk
, map
->m_len
);
4146 if (reserved_clusters
)
4147 ext4_da_update_reserve_space(inode
,
4153 map
->m_flags
|= EXT4_MAP_MAPPED
;
4154 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0) {
4155 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
,
4161 if (allocated
> map
->m_len
)
4162 allocated
= map
->m_len
;
4163 ext4_ext_show_leaf(inode
, path
);
4164 map
->m_pblk
= newblock
;
4165 map
->m_len
= allocated
;
4167 return err
? err
: allocated
;
4171 * get_implied_cluster_alloc - check to see if the requested
4172 * allocation (in the map structure) overlaps with a cluster already
4173 * allocated in an extent.
4174 * @sb The filesystem superblock structure
4175 * @map The requested lblk->pblk mapping
4176 * @ex The extent structure which might contain an implied
4177 * cluster allocation
4179 * This function is called by ext4_ext_map_blocks() after we failed to
4180 * find blocks that were already in the inode's extent tree. Hence,
4181 * we know that the beginning of the requested region cannot overlap
4182 * the extent from the inode's extent tree. There are three cases we
4183 * want to catch. The first is this case:
4185 * |--- cluster # N--|
4186 * |--- extent ---| |---- requested region ---|
4189 * The second case that we need to test for is this one:
4191 * |--------- cluster # N ----------------|
4192 * |--- requested region --| |------- extent ----|
4193 * |=======================|
4195 * The third case is when the requested region lies between two extents
4196 * within the same cluster:
4197 * |------------- cluster # N-------------|
4198 * |----- ex -----| |---- ex_right ----|
4199 * |------ requested region ------|
4200 * |================|
4202 * In each of the above cases, we need to set the map->m_pblk and
4203 * map->m_len so it corresponds to the return the extent labelled as
4204 * "|====|" from cluster #N, since it is already in use for data in
4205 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4206 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4207 * as a new "allocated" block region. Otherwise, we will return 0 and
4208 * ext4_ext_map_blocks() will then allocate one or more new clusters
4209 * by calling ext4_mb_new_blocks().
4211 static int get_implied_cluster_alloc(struct super_block
*sb
,
4212 struct ext4_map_blocks
*map
,
4213 struct ext4_extent
*ex
,
4214 struct ext4_ext_path
*path
)
4216 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4217 ext4_lblk_t c_offset
= EXT4_LBLK_COFF(sbi
, map
->m_lblk
);
4218 ext4_lblk_t ex_cluster_start
, ex_cluster_end
;
4219 ext4_lblk_t rr_cluster_start
;
4220 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
4221 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
4222 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
4224 /* The extent passed in that we are trying to match */
4225 ex_cluster_start
= EXT4_B2C(sbi
, ee_block
);
4226 ex_cluster_end
= EXT4_B2C(sbi
, ee_block
+ ee_len
- 1);
4228 /* The requested region passed into ext4_map_blocks() */
4229 rr_cluster_start
= EXT4_B2C(sbi
, map
->m_lblk
);
4231 if ((rr_cluster_start
== ex_cluster_end
) ||
4232 (rr_cluster_start
== ex_cluster_start
)) {
4233 if (rr_cluster_start
== ex_cluster_end
)
4234 ee_start
+= ee_len
- 1;
4235 map
->m_pblk
= EXT4_PBLK_CMASK(sbi
, ee_start
) + c_offset
;
4236 map
->m_len
= min(map
->m_len
,
4237 (unsigned) sbi
->s_cluster_ratio
- c_offset
);
4239 * Check for and handle this case:
4241 * |--------- cluster # N-------------|
4242 * |------- extent ----|
4243 * |--- requested region ---|
4247 if (map
->m_lblk
< ee_block
)
4248 map
->m_len
= min(map
->m_len
, ee_block
- map
->m_lblk
);
4251 * Check for the case where there is already another allocated
4252 * block to the right of 'ex' but before the end of the cluster.
4254 * |------------- cluster # N-------------|
4255 * |----- ex -----| |---- ex_right ----|
4256 * |------ requested region ------|
4257 * |================|
4259 if (map
->m_lblk
> ee_block
) {
4260 ext4_lblk_t next
= ext4_ext_next_allocated_block(path
);
4261 map
->m_len
= min(map
->m_len
, next
- map
->m_lblk
);
4264 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 1);
4268 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 0);
4274 * Block allocation/map/preallocation routine for extents based files
4277 * Need to be called with
4278 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4279 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
4281 * return > 0, number of of blocks already mapped/allocated
4282 * if create == 0 and these are pre-allocated blocks
4283 * buffer head is unmapped
4284 * otherwise blocks are mapped
4286 * return = 0, if plain look up failed (blocks have not been allocated)
4287 * buffer head is unmapped
4289 * return < 0, error case.
4291 int ext4_ext_map_blocks(handle_t
*handle
, struct inode
*inode
,
4292 struct ext4_map_blocks
*map
, int flags
)
4294 struct ext4_ext_path
*path
= NULL
;
4295 struct ext4_extent newex
, *ex
, *ex2
;
4296 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4297 ext4_fsblk_t newblock
= 0;
4298 int free_on_err
= 0, err
= 0, depth
, ret
;
4299 unsigned int allocated
= 0, offset
= 0;
4300 unsigned int allocated_clusters
= 0;
4301 struct ext4_allocation_request ar
;
4302 ext4_lblk_t cluster_offset
;
4303 bool map_from_cluster
= false;
4305 ext_debug("blocks %u/%u requested for inode %lu\n",
4306 map
->m_lblk
, map
->m_len
, inode
->i_ino
);
4307 trace_ext4_ext_map_blocks_enter(inode
, map
->m_lblk
, map
->m_len
, flags
);
4309 /* find extent for this block */
4310 path
= ext4_find_extent(inode
, map
->m_lblk
, NULL
, 0);
4312 err
= PTR_ERR(path
);
4317 depth
= ext_depth(inode
);
4320 * consistent leaf must not be empty;
4321 * this situation is possible, though, _during_ tree modification;
4322 * this is why assert can't be put in ext4_find_extent()
4324 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
4325 EXT4_ERROR_INODE(inode
, "bad extent address "
4326 "lblock: %lu, depth: %d pblock %lld",
4327 (unsigned long) map
->m_lblk
, depth
,
4328 path
[depth
].p_block
);
4329 err
= -EFSCORRUPTED
;
4333 ex
= path
[depth
].p_ext
;
4335 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
4336 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
4337 unsigned short ee_len
;
4341 * unwritten extents are treated as holes, except that
4342 * we split out initialized portions during a write.
4344 ee_len
= ext4_ext_get_actual_len(ex
);
4346 trace_ext4_ext_show_extent(inode
, ee_block
, ee_start
, ee_len
);
4348 /* if found extent covers block, simply return it */
4349 if (in_range(map
->m_lblk
, ee_block
, ee_len
)) {
4350 newblock
= map
->m_lblk
- ee_block
+ ee_start
;
4351 /* number of remaining blocks in the extent */
4352 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
4353 ext_debug("%u fit into %u:%d -> %llu\n", map
->m_lblk
,
4354 ee_block
, ee_len
, newblock
);
4357 * If the extent is initialized check whether the
4358 * caller wants to convert it to unwritten.
4360 if ((!ext4_ext_is_unwritten(ex
)) &&
4361 (flags
& EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
)) {
4362 allocated
= convert_initialized_extent(
4363 handle
, inode
, map
, &path
,
4366 } else if (!ext4_ext_is_unwritten(ex
))
4369 ret
= ext4_ext_handle_unwritten_extents(
4370 handle
, inode
, map
, &path
, flags
,
4371 allocated
, newblock
);
4381 * requested block isn't allocated yet;
4382 * we couldn't try to create block if create flag is zero
4384 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
4385 ext4_lblk_t hole_start
, hole_len
;
4387 hole_start
= map
->m_lblk
;
4388 hole_len
= ext4_ext_determine_hole(inode
, path
, &hole_start
);
4390 * put just found gap into cache to speed up
4391 * subsequent requests
4393 ext4_ext_put_gap_in_cache(inode
, hole_start
, hole_len
);
4395 /* Update hole_len to reflect hole size after map->m_lblk */
4396 if (hole_start
!= map
->m_lblk
)
4397 hole_len
-= map
->m_lblk
- hole_start
;
4399 map
->m_len
= min_t(unsigned int, map
->m_len
, hole_len
);
4405 * Okay, we need to do block allocation.
4407 newex
.ee_block
= cpu_to_le32(map
->m_lblk
);
4408 cluster_offset
= EXT4_LBLK_COFF(sbi
, map
->m_lblk
);
4411 * If we are doing bigalloc, check to see if the extent returned
4412 * by ext4_find_extent() implies a cluster we can use.
4414 if (cluster_offset
&& ex
&&
4415 get_implied_cluster_alloc(inode
->i_sb
, map
, ex
, path
)) {
4416 ar
.len
= allocated
= map
->m_len
;
4417 newblock
= map
->m_pblk
;
4418 map_from_cluster
= true;
4419 goto got_allocated_blocks
;
4422 /* find neighbour allocated blocks */
4423 ar
.lleft
= map
->m_lblk
;
4424 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
4427 ar
.lright
= map
->m_lblk
;
4429 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
, &ex2
);
4433 /* Check if the extent after searching to the right implies a
4434 * cluster we can use. */
4435 if ((sbi
->s_cluster_ratio
> 1) && ex2
&&
4436 get_implied_cluster_alloc(inode
->i_sb
, map
, ex2
, path
)) {
4437 ar
.len
= allocated
= map
->m_len
;
4438 newblock
= map
->m_pblk
;
4439 map_from_cluster
= true;
4440 goto got_allocated_blocks
;
4444 * See if request is beyond maximum number of blocks we can have in
4445 * a single extent. For an initialized extent this limit is
4446 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4447 * EXT_UNWRITTEN_MAX_LEN.
4449 if (map
->m_len
> EXT_INIT_MAX_LEN
&&
4450 !(flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
))
4451 map
->m_len
= EXT_INIT_MAX_LEN
;
4452 else if (map
->m_len
> EXT_UNWRITTEN_MAX_LEN
&&
4453 (flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
))
4454 map
->m_len
= EXT_UNWRITTEN_MAX_LEN
;
4456 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
4457 newex
.ee_len
= cpu_to_le16(map
->m_len
);
4458 err
= ext4_ext_check_overlap(sbi
, inode
, &newex
, path
);
4460 allocated
= ext4_ext_get_actual_len(&newex
);
4462 allocated
= map
->m_len
;
4464 /* allocate new block */
4466 ar
.goal
= ext4_ext_find_goal(inode
, path
, map
->m_lblk
);
4467 ar
.logical
= map
->m_lblk
;
4469 * We calculate the offset from the beginning of the cluster
4470 * for the logical block number, since when we allocate a
4471 * physical cluster, the physical block should start at the
4472 * same offset from the beginning of the cluster. This is
4473 * needed so that future calls to get_implied_cluster_alloc()
4476 offset
= EXT4_LBLK_COFF(sbi
, map
->m_lblk
);
4477 ar
.len
= EXT4_NUM_B2C(sbi
, offset
+allocated
);
4479 ar
.logical
-= offset
;
4480 if (S_ISREG(inode
->i_mode
))
4481 ar
.flags
= EXT4_MB_HINT_DATA
;
4483 /* disable in-core preallocation for non-regular files */
4485 if (flags
& EXT4_GET_BLOCKS_NO_NORMALIZE
)
4486 ar
.flags
|= EXT4_MB_HINT_NOPREALLOC
;
4487 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
4488 ar
.flags
|= EXT4_MB_DELALLOC_RESERVED
;
4489 if (flags
& EXT4_GET_BLOCKS_METADATA_NOFAIL
)
4490 ar
.flags
|= EXT4_MB_USE_RESERVED
;
4491 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
4494 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
4495 ar
.goal
, newblock
, allocated
);
4497 allocated_clusters
= ar
.len
;
4498 ar
.len
= EXT4_C2B(sbi
, ar
.len
) - offset
;
4499 if (ar
.len
> allocated
)
4502 got_allocated_blocks
:
4503 /* try to insert new extent into found leaf and return */
4504 ext4_ext_store_pblock(&newex
, newblock
+ offset
);
4505 newex
.ee_len
= cpu_to_le16(ar
.len
);
4506 /* Mark unwritten */
4507 if (flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
){
4508 ext4_ext_mark_unwritten(&newex
);
4509 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4513 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0)
4514 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
4517 err
= ext4_ext_insert_extent(handle
, inode
, &path
,
4520 if (err
&& free_on_err
) {
4521 int fb_flags
= flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
?
4522 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
: 0;
4523 /* free data blocks we just allocated */
4524 /* not a good idea to call discard here directly,
4525 * but otherwise we'd need to call it every free() */
4526 ext4_discard_preallocations(inode
);
4527 ext4_free_blocks(handle
, inode
, NULL
, newblock
,
4528 EXT4_C2B(sbi
, allocated_clusters
), fb_flags
);
4532 /* previous routine could use block we allocated */
4533 newblock
= ext4_ext_pblock(&newex
);
4534 allocated
= ext4_ext_get_actual_len(&newex
);
4535 if (allocated
> map
->m_len
)
4536 allocated
= map
->m_len
;
4537 map
->m_flags
|= EXT4_MAP_NEW
;
4540 * Update reserved blocks/metadata blocks after successful
4541 * block allocation which had been deferred till now.
4543 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) {
4544 unsigned int reserved_clusters
;
4546 * Check how many clusters we had reserved this allocated range
4548 reserved_clusters
= get_reserved_cluster_alloc(inode
,
4549 map
->m_lblk
, allocated
);
4550 if (!map_from_cluster
) {
4551 BUG_ON(allocated_clusters
< reserved_clusters
);
4552 if (reserved_clusters
< allocated_clusters
) {
4553 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4554 int reservation
= allocated_clusters
-
4557 * It seems we claimed few clusters outside of
4558 * the range of this allocation. We should give
4559 * it back to the reservation pool. This can
4560 * happen in the following case:
4562 * * Suppose s_cluster_ratio is 4 (i.e., each
4563 * cluster has 4 blocks. Thus, the clusters
4564 * are [0-3],[4-7],[8-11]...
4565 * * First comes delayed allocation write for
4566 * logical blocks 10 & 11. Since there were no
4567 * previous delayed allocated blocks in the
4568 * range [8-11], we would reserve 1 cluster
4570 * * Next comes write for logical blocks 3 to 8.
4571 * In this case, we will reserve 2 clusters
4572 * (for [0-3] and [4-7]; and not for [8-11] as
4573 * that range has a delayed allocated blocks.
4574 * Thus total reserved clusters now becomes 3.
4575 * * Now, during the delayed allocation writeout
4576 * time, we will first write blocks [3-8] and
4577 * allocate 3 clusters for writing these
4578 * blocks. Also, we would claim all these
4579 * three clusters above.
4580 * * Now when we come here to writeout the
4581 * blocks [10-11], we would expect to claim
4582 * the reservation of 1 cluster we had made
4583 * (and we would claim it since there are no
4584 * more delayed allocated blocks in the range
4585 * [8-11]. But our reserved cluster count had
4586 * already gone to 0.
4588 * Thus, at the step 4 above when we determine
4589 * that there are still some unwritten delayed
4590 * allocated blocks outside of our current
4591 * block range, we should increment the
4592 * reserved clusters count so that when the
4593 * remaining blocks finally gets written, we
4596 dquot_reserve_block(inode
,
4597 EXT4_C2B(sbi
, reservation
));
4598 spin_lock(&ei
->i_block_reservation_lock
);
4599 ei
->i_reserved_data_blocks
+= reservation
;
4600 spin_unlock(&ei
->i_block_reservation_lock
);
4603 * We will claim quota for all newly allocated blocks.
4604 * We're updating the reserved space *after* the
4605 * correction above so we do not accidentally free
4606 * all the metadata reservation because we might
4607 * actually need it later on.
4609 ext4_da_update_reserve_space(inode
, allocated_clusters
,
4615 * Cache the extent and update transaction to commit on fdatasync only
4616 * when it is _not_ an unwritten extent.
4618 if ((flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
) == 0)
4619 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4621 ext4_update_inode_fsync_trans(handle
, inode
, 0);
4623 if (allocated
> map
->m_len
)
4624 allocated
= map
->m_len
;
4625 ext4_ext_show_leaf(inode
, path
);
4626 map
->m_flags
|= EXT4_MAP_MAPPED
;
4627 map
->m_pblk
= newblock
;
4628 map
->m_len
= allocated
;
4630 ext4_ext_drop_refs(path
);
4633 trace_ext4_ext_map_blocks_exit(inode
, flags
, map
,
4634 err
? err
: allocated
);
4635 return err
? err
: allocated
;
4638 int ext4_ext_truncate(handle_t
*handle
, struct inode
*inode
)
4640 struct super_block
*sb
= inode
->i_sb
;
4641 ext4_lblk_t last_block
;
4645 * TODO: optimization is possible here.
4646 * Probably we need not scan at all,
4647 * because page truncation is enough.
4650 /* we have to know where to truncate from in crash case */
4651 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
4652 err
= ext4_mark_inode_dirty(handle
, inode
);
4656 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
4657 >> EXT4_BLOCK_SIZE_BITS(sb
);
4659 err
= ext4_es_remove_extent(inode
, last_block
,
4660 EXT_MAX_BLOCKS
- last_block
);
4661 if (err
== -ENOMEM
) {
4663 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
4668 return ext4_ext_remove_space(inode
, last_block
, EXT_MAX_BLOCKS
- 1);
4671 static int ext4_alloc_file_blocks(struct file
*file
, ext4_lblk_t offset
,
4672 ext4_lblk_t len
, loff_t new_size
,
4675 struct inode
*inode
= file_inode(file
);
4681 struct ext4_map_blocks map
;
4682 unsigned int credits
;
4685 BUG_ON(!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
));
4686 map
.m_lblk
= offset
;
4689 * Don't normalize the request if it can fit in one extent so
4690 * that it doesn't get unnecessarily split into multiple
4693 if (len
<= EXT_UNWRITTEN_MAX_LEN
)
4694 flags
|= EXT4_GET_BLOCKS_NO_NORMALIZE
;
4697 * credits to insert 1 extent into extent tree
4699 credits
= ext4_chunk_trans_blocks(inode
, len
);
4700 depth
= ext_depth(inode
);
4703 while (ret
>= 0 && len
) {
4705 * Recalculate credits when extent tree depth changes.
4707 if (depth
!= ext_depth(inode
)) {
4708 credits
= ext4_chunk_trans_blocks(inode
, len
);
4709 depth
= ext_depth(inode
);
4712 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
,
4714 if (IS_ERR(handle
)) {
4715 ret
= PTR_ERR(handle
);
4718 ret
= ext4_map_blocks(handle
, inode
, &map
, flags
);
4720 ext4_debug("inode #%lu: block %u: len %u: "
4721 "ext4_ext_map_blocks returned %d",
4722 inode
->i_ino
, map
.m_lblk
,
4724 ext4_mark_inode_dirty(handle
, inode
);
4725 ret2
= ext4_journal_stop(handle
);
4729 map
.m_len
= len
= len
- ret
;
4730 epos
= (loff_t
)map
.m_lblk
<< inode
->i_blkbits
;
4731 inode
->i_ctime
= current_time(inode
);
4733 if (epos
> new_size
)
4735 if (ext4_update_inode_size(inode
, epos
) & 0x1)
4736 inode
->i_mtime
= inode
->i_ctime
;
4738 if (epos
> inode
->i_size
)
4739 ext4_set_inode_flag(inode
,
4740 EXT4_INODE_EOFBLOCKS
);
4742 ext4_mark_inode_dirty(handle
, inode
);
4743 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4744 ret2
= ext4_journal_stop(handle
);
4748 if (ret
== -ENOSPC
&&
4749 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
4754 return ret
> 0 ? ret2
: ret
;
4757 static long ext4_zero_range(struct file
*file
, loff_t offset
,
4758 loff_t len
, int mode
)
4760 struct inode
*inode
= file_inode(file
);
4761 handle_t
*handle
= NULL
;
4762 unsigned int max_blocks
;
4763 loff_t new_size
= 0;
4767 int partial_begin
, partial_end
;
4770 unsigned int blkbits
= inode
->i_blkbits
;
4772 trace_ext4_zero_range(inode
, offset
, len
, mode
);
4774 if (!S_ISREG(inode
->i_mode
))
4777 /* Call ext4_force_commit to flush all data in case of data=journal. */
4778 if (ext4_should_journal_data(inode
)) {
4779 ret
= ext4_force_commit(inode
->i_sb
);
4785 * Round up offset. This is not fallocate, we neet to zero out
4786 * blocks, so convert interior block aligned part of the range to
4787 * unwritten and possibly manually zero out unaligned parts of the
4790 start
= round_up(offset
, 1 << blkbits
);
4791 end
= round_down((offset
+ len
), 1 << blkbits
);
4793 if (start
< offset
|| end
> offset
+ len
)
4795 partial_begin
= offset
& ((1 << blkbits
) - 1);
4796 partial_end
= (offset
+ len
) & ((1 << blkbits
) - 1);
4798 lblk
= start
>> blkbits
;
4799 max_blocks
= (end
>> blkbits
);
4800 if (max_blocks
< lblk
)
4808 * Indirect files do not support unwritten extnets
4810 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
4815 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
4816 (offset
+ len
> i_size_read(inode
) ||
4817 offset
+ len
> EXT4_I(inode
)->i_disksize
)) {
4818 new_size
= offset
+ len
;
4819 ret
= inode_newsize_ok(inode
, new_size
);
4824 flags
= EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT
;
4825 if (mode
& FALLOC_FL_KEEP_SIZE
)
4826 flags
|= EXT4_GET_BLOCKS_KEEP_SIZE
;
4828 /* Wait all existing dio workers, newcomers will block on i_mutex */
4829 inode_dio_wait(inode
);
4831 /* Preallocate the range including the unaligned edges */
4832 if (partial_begin
|| partial_end
) {
4833 ret
= ext4_alloc_file_blocks(file
,
4834 round_down(offset
, 1 << blkbits
) >> blkbits
,
4835 (round_up((offset
+ len
), 1 << blkbits
) -
4836 round_down(offset
, 1 << blkbits
)) >> blkbits
,
4843 /* Zero range excluding the unaligned edges */
4844 if (max_blocks
> 0) {
4845 flags
|= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
|
4849 * Prevent page faults from reinstantiating pages we have
4850 * released from page cache.
4852 down_write(&EXT4_I(inode
)->i_mmap_sem
);
4854 ret
= ext4_break_layouts(inode
);
4856 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4860 ret
= ext4_update_disksize_before_punch(inode
, offset
, len
);
4862 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4865 /* Now release the pages and zero block aligned part of pages */
4866 truncate_pagecache_range(inode
, start
, end
- 1);
4867 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
4869 ret
= ext4_alloc_file_blocks(file
, lblk
, max_blocks
, new_size
,
4871 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4875 if (!partial_begin
&& !partial_end
)
4879 * In worst case we have to writeout two nonadjacent unwritten
4880 * blocks and update the inode
4882 credits
= (2 * ext4_ext_index_trans_blocks(inode
, 2)) + 1;
4883 if (ext4_should_journal_data(inode
))
4885 handle
= ext4_journal_start(inode
, EXT4_HT_MISC
, credits
);
4886 if (IS_ERR(handle
)) {
4887 ret
= PTR_ERR(handle
);
4888 ext4_std_error(inode
->i_sb
, ret
);
4892 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
4894 ext4_update_inode_size(inode
, new_size
);
4897 * Mark that we allocate beyond EOF so the subsequent truncate
4898 * can proceed even if the new size is the same as i_size.
4900 if ((offset
+ len
) > i_size_read(inode
))
4901 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
4903 ext4_mark_inode_dirty(handle
, inode
);
4905 /* Zero out partial block at the edges of the range */
4906 ret
= ext4_zero_partial_blocks(handle
, inode
, offset
, len
);
4908 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4910 if (file
->f_flags
& O_SYNC
)
4911 ext4_handle_sync(handle
);
4913 ext4_journal_stop(handle
);
4915 inode_unlock(inode
);
4920 * preallocate space for a file. This implements ext4's fallocate file
4921 * operation, which gets called from sys_fallocate system call.
4922 * For block-mapped files, posix_fallocate should fall back to the method
4923 * of writing zeroes to the required new blocks (the same behavior which is
4924 * expected for file systems which do not support fallocate() system call).
4926 long ext4_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
4928 struct inode
*inode
= file_inode(file
);
4929 loff_t new_size
= 0;
4930 unsigned int max_blocks
;
4934 unsigned int blkbits
= inode
->i_blkbits
;
4937 * Encrypted inodes can't handle collapse range or insert
4938 * range since we would need to re-encrypt blocks with a
4939 * different IV or XTS tweak (which are based on the logical
4942 * XXX It's not clear why zero range isn't working, but we'll
4943 * leave it disabled for encrypted inodes for now. This is a
4944 * bug we should fix....
4946 if (ext4_encrypted_inode(inode
) &&
4947 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
|
4948 FALLOC_FL_ZERO_RANGE
)))
4951 /* Return error if mode is not supported */
4952 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
4953 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
4954 FALLOC_FL_INSERT_RANGE
))
4957 if (mode
& FALLOC_FL_PUNCH_HOLE
)
4958 return ext4_punch_hole(inode
, offset
, len
);
4960 ret
= ext4_convert_inline_data(inode
);
4964 if (mode
& FALLOC_FL_COLLAPSE_RANGE
)
4965 return ext4_collapse_range(inode
, offset
, len
);
4967 if (mode
& FALLOC_FL_INSERT_RANGE
)
4968 return ext4_insert_range(inode
, offset
, len
);
4970 if (mode
& FALLOC_FL_ZERO_RANGE
)
4971 return ext4_zero_range(file
, offset
, len
, mode
);
4973 trace_ext4_fallocate_enter(inode
, offset
, len
, mode
);
4974 lblk
= offset
>> blkbits
;
4976 max_blocks
= EXT4_MAX_BLOCKS(len
, offset
, blkbits
);
4977 flags
= EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT
;
4978 if (mode
& FALLOC_FL_KEEP_SIZE
)
4979 flags
|= EXT4_GET_BLOCKS_KEEP_SIZE
;
4984 * We only support preallocation for extent-based files only
4986 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
4991 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
4992 (offset
+ len
> i_size_read(inode
) ||
4993 offset
+ len
> EXT4_I(inode
)->i_disksize
)) {
4994 new_size
= offset
+ len
;
4995 ret
= inode_newsize_ok(inode
, new_size
);
5000 /* Wait all existing dio workers, newcomers will block on i_mutex */
5001 inode_dio_wait(inode
);
5003 ret
= ext4_alloc_file_blocks(file
, lblk
, max_blocks
, new_size
, flags
);
5007 if (file
->f_flags
& O_SYNC
&& EXT4_SB(inode
->i_sb
)->s_journal
) {
5008 ret
= jbd2_complete_transaction(EXT4_SB(inode
->i_sb
)->s_journal
,
5009 EXT4_I(inode
)->i_sync_tid
);
5012 inode_unlock(inode
);
5013 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
, ret
);
5018 * This function convert a range of blocks to written extents
5019 * The caller of this function will pass the start offset and the size.
5020 * all unwritten extents within this range will be converted to
5023 * This function is called from the direct IO end io call back
5024 * function, to convert the fallocated extents after IO is completed.
5025 * Returns 0 on success.
5027 int ext4_convert_unwritten_extents(handle_t
*handle
, struct inode
*inode
,
5028 loff_t offset
, ssize_t len
)
5030 unsigned int max_blocks
;
5033 struct ext4_map_blocks map
;
5034 unsigned int credits
, blkbits
= inode
->i_blkbits
;
5036 map
.m_lblk
= offset
>> blkbits
;
5037 max_blocks
= EXT4_MAX_BLOCKS(len
, offset
, blkbits
);
5040 * This is somewhat ugly but the idea is clear: When transaction is
5041 * reserved, everything goes into it. Otherwise we rather start several
5042 * smaller transactions for conversion of each extent separately.
5045 handle
= ext4_journal_start_reserved(handle
,
5046 EXT4_HT_EXT_CONVERT
);
5048 return PTR_ERR(handle
);
5052 * credits to insert 1 extent into extent tree
5054 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
5056 while (ret
>= 0 && ret
< max_blocks
) {
5058 map
.m_len
= (max_blocks
-= ret
);
5060 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
,
5062 if (IS_ERR(handle
)) {
5063 ret
= PTR_ERR(handle
);
5067 ret
= ext4_map_blocks(handle
, inode
, &map
,
5068 EXT4_GET_BLOCKS_IO_CONVERT_EXT
);
5070 ext4_warning(inode
->i_sb
,
5071 "inode #%lu: block %u: len %u: "
5072 "ext4_ext_map_blocks returned %d",
5073 inode
->i_ino
, map
.m_lblk
,
5075 ext4_mark_inode_dirty(handle
, inode
);
5077 ret2
= ext4_journal_stop(handle
);
5078 if (ret
<= 0 || ret2
)
5082 ret2
= ext4_journal_stop(handle
);
5083 return ret
> 0 ? ret2
: ret
;
5087 * If newes is not existing extent (newes->ec_pblk equals zero) find
5088 * delayed extent at start of newes and update newes accordingly and
5089 * return start of the next delayed extent.
5091 * If newes is existing extent (newes->ec_pblk is not equal zero)
5092 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
5093 * extent found. Leave newes unmodified.
5095 static int ext4_find_delayed_extent(struct inode
*inode
,
5096 struct extent_status
*newes
)
5098 struct extent_status es
;
5099 ext4_lblk_t block
, next_del
;
5101 if (newes
->es_pblk
== 0) {
5102 ext4_es_find_delayed_extent_range(inode
, newes
->es_lblk
,
5103 newes
->es_lblk
+ newes
->es_len
- 1, &es
);
5106 * No extent in extent-tree contains block @newes->es_pblk,
5107 * then the block may stay in 1)a hole or 2)delayed-extent.
5113 if (es
.es_lblk
> newes
->es_lblk
) {
5115 newes
->es_len
= min(es
.es_lblk
- newes
->es_lblk
,
5120 newes
->es_len
= es
.es_lblk
+ es
.es_len
- newes
->es_lblk
;
5123 block
= newes
->es_lblk
+ newes
->es_len
;
5124 ext4_es_find_delayed_extent_range(inode
, block
, EXT_MAX_BLOCKS
, &es
);
5126 next_del
= EXT_MAX_BLOCKS
;
5128 next_del
= es
.es_lblk
;
5132 /* fiemap flags we can handle specified here */
5133 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
5135 static int ext4_xattr_fiemap(struct inode
*inode
,
5136 struct fiemap_extent_info
*fieinfo
)
5140 __u32 flags
= FIEMAP_EXTENT_LAST
;
5141 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
5145 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
5146 struct ext4_iloc iloc
;
5147 int offset
; /* offset of xattr in inode */
5149 error
= ext4_get_inode_loc(inode
, &iloc
);
5152 physical
= (__u64
)iloc
.bh
->b_blocknr
<< blockbits
;
5153 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
5154 EXT4_I(inode
)->i_extra_isize
;
5156 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
5157 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
5159 } else { /* external block */
5160 physical
= (__u64
)EXT4_I(inode
)->i_file_acl
<< blockbits
;
5161 length
= inode
->i_sb
->s_blocksize
;
5165 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
5167 return (error
< 0 ? error
: 0);
5170 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
5171 __u64 start
, __u64 len
)
5173 ext4_lblk_t start_blk
;
5176 if (ext4_has_inline_data(inode
)) {
5179 error
= ext4_inline_data_fiemap(inode
, fieinfo
, &has_inline
,
5186 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
5187 error
= ext4_ext_precache(inode
);
5192 /* fallback to generic here if not in extents fmt */
5193 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
5194 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
5197 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
5200 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
5201 error
= ext4_xattr_fiemap(inode
, fieinfo
);
5203 ext4_lblk_t len_blks
;
5206 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
5207 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
5208 if (last_blk
>= EXT_MAX_BLOCKS
)
5209 last_blk
= EXT_MAX_BLOCKS
-1;
5210 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
5213 * Walk the extent tree gathering extent information
5214 * and pushing extents back to the user.
5216 error
= ext4_fill_fiemap_extents(inode
, start_blk
,
5224 * Function to access the path buffer for marking it dirty.
5225 * It also checks if there are sufficient credits left in the journal handle
5229 ext4_access_path(handle_t
*handle
, struct inode
*inode
,
5230 struct ext4_ext_path
*path
)
5234 if (!ext4_handle_valid(handle
))
5238 * Check if need to extend journal credits
5239 * 3 for leaf, sb, and inode plus 2 (bmap and group
5240 * descriptor) for each block group; assume two block
5243 if (handle
->h_buffer_credits
< 7) {
5244 credits
= ext4_writepage_trans_blocks(inode
);
5245 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
5246 /* EAGAIN is success */
5247 if (err
&& err
!= -EAGAIN
)
5251 err
= ext4_ext_get_access(handle
, inode
, path
);
5256 * ext4_ext_shift_path_extents:
5257 * Shift the extents of a path structure lying between path[depth].p_ext
5258 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5259 * if it is right shift or left shift operation.
5262 ext4_ext_shift_path_extents(struct ext4_ext_path
*path
, ext4_lblk_t shift
,
5263 struct inode
*inode
, handle_t
*handle
,
5264 enum SHIFT_DIRECTION SHIFT
)
5267 struct ext4_extent
*ex_start
, *ex_last
;
5269 depth
= path
->p_depth
;
5271 while (depth
>= 0) {
5272 if (depth
== path
->p_depth
) {
5273 ex_start
= path
[depth
].p_ext
;
5275 return -EFSCORRUPTED
;
5277 ex_last
= EXT_LAST_EXTENT(path
[depth
].p_hdr
);
5279 err
= ext4_access_path(handle
, inode
, path
+ depth
);
5283 if (ex_start
== EXT_FIRST_EXTENT(path
[depth
].p_hdr
))
5286 while (ex_start
<= ex_last
) {
5287 if (SHIFT
== SHIFT_LEFT
) {
5288 le32_add_cpu(&ex_start
->ee_block
,
5290 /* Try to merge to the left. */
5292 EXT_FIRST_EXTENT(path
[depth
].p_hdr
))
5294 ext4_ext_try_to_merge_right(inode
,
5295 path
, ex_start
- 1))
5300 le32_add_cpu(&ex_last
->ee_block
, shift
);
5301 ext4_ext_try_to_merge_right(inode
, path
,
5306 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
5310 if (--depth
< 0 || !update
)
5314 /* Update index too */
5315 err
= ext4_access_path(handle
, inode
, path
+ depth
);
5319 if (SHIFT
== SHIFT_LEFT
)
5320 le32_add_cpu(&path
[depth
].p_idx
->ei_block
, -shift
);
5322 le32_add_cpu(&path
[depth
].p_idx
->ei_block
, shift
);
5323 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
5327 /* we are done if current index is not a starting index */
5328 if (path
[depth
].p_idx
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))
5339 * ext4_ext_shift_extents:
5340 * All the extents which lies in the range from @start to the last allocated
5341 * block for the @inode are shifted either towards left or right (depending
5342 * upon @SHIFT) by @shift blocks.
5343 * On success, 0 is returned, error otherwise.
5346 ext4_ext_shift_extents(struct inode
*inode
, handle_t
*handle
,
5347 ext4_lblk_t start
, ext4_lblk_t shift
,
5348 enum SHIFT_DIRECTION SHIFT
)
5350 struct ext4_ext_path
*path
;
5352 struct ext4_extent
*extent
;
5353 ext4_lblk_t stop
, *iterator
, ex_start
, ex_end
;
5355 /* Let path point to the last extent */
5356 path
= ext4_find_extent(inode
, EXT_MAX_BLOCKS
- 1, NULL
,
5359 return PTR_ERR(path
);
5361 depth
= path
->p_depth
;
5362 extent
= path
[depth
].p_ext
;
5366 stop
= le32_to_cpu(extent
->ee_block
);
5369 * For left shifts, make sure the hole on the left is big enough to
5370 * accommodate the shift. For right shifts, make sure the last extent
5371 * won't be shifted beyond EXT_MAX_BLOCKS.
5373 if (SHIFT
== SHIFT_LEFT
) {
5374 path
= ext4_find_extent(inode
, start
- 1, &path
,
5377 return PTR_ERR(path
);
5378 depth
= path
->p_depth
;
5379 extent
= path
[depth
].p_ext
;
5381 ex_start
= le32_to_cpu(extent
->ee_block
);
5382 ex_end
= le32_to_cpu(extent
->ee_block
) +
5383 ext4_ext_get_actual_len(extent
);
5389 if ((start
== ex_start
&& shift
> ex_start
) ||
5390 (shift
> start
- ex_end
)) {
5395 if (shift
> EXT_MAX_BLOCKS
-
5396 (stop
+ ext4_ext_get_actual_len(extent
))) {
5403 * In case of left shift, iterator points to start and it is increased
5404 * till we reach stop. In case of right shift, iterator points to stop
5405 * and it is decreased till we reach start.
5407 if (SHIFT
== SHIFT_LEFT
)
5413 * Its safe to start updating extents. Start and stop are unsigned, so
5414 * in case of right shift if extent with 0 block is reached, iterator
5415 * becomes NULL to indicate the end of the loop.
5417 while (iterator
&& start
<= stop
) {
5418 path
= ext4_find_extent(inode
, *iterator
, &path
,
5421 return PTR_ERR(path
);
5422 depth
= path
->p_depth
;
5423 extent
= path
[depth
].p_ext
;
5425 EXT4_ERROR_INODE(inode
, "unexpected hole at %lu",
5426 (unsigned long) *iterator
);
5427 return -EFSCORRUPTED
;
5429 if (SHIFT
== SHIFT_LEFT
&& *iterator
>
5430 le32_to_cpu(extent
->ee_block
)) {
5431 /* Hole, move to the next extent */
5432 if (extent
< EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
5433 path
[depth
].p_ext
++;
5435 *iterator
= ext4_ext_next_allocated_block(path
);
5440 if (SHIFT
== SHIFT_LEFT
) {
5441 extent
= EXT_LAST_EXTENT(path
[depth
].p_hdr
);
5442 *iterator
= le32_to_cpu(extent
->ee_block
) +
5443 ext4_ext_get_actual_len(extent
);
5445 extent
= EXT_FIRST_EXTENT(path
[depth
].p_hdr
);
5446 if (le32_to_cpu(extent
->ee_block
) > 0)
5447 *iterator
= le32_to_cpu(extent
->ee_block
) - 1;
5449 /* Beginning is reached, end of the loop */
5451 /* Update path extent in case we need to stop */
5452 while (le32_to_cpu(extent
->ee_block
) < start
)
5454 path
[depth
].p_ext
= extent
;
5456 ret
= ext4_ext_shift_path_extents(path
, shift
, inode
,
5462 ext4_ext_drop_refs(path
);
5468 * ext4_collapse_range:
5469 * This implements the fallocate's collapse range functionality for ext4
5470 * Returns: 0 and non-zero on error.
5472 int ext4_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
5474 struct super_block
*sb
= inode
->i_sb
;
5475 ext4_lblk_t punch_start
, punch_stop
;
5477 unsigned int credits
;
5478 loff_t new_size
, ioffset
;
5482 * We need to test this early because xfstests assumes that a
5483 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5484 * system does not support collapse range.
5486 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
5489 /* Collapse range works only on fs block size aligned offsets. */
5490 if (offset
& (EXT4_CLUSTER_SIZE(sb
) - 1) ||
5491 len
& (EXT4_CLUSTER_SIZE(sb
) - 1))
5494 if (!S_ISREG(inode
->i_mode
))
5497 trace_ext4_collapse_range(inode
, offset
, len
);
5499 punch_start
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
5500 punch_stop
= (offset
+ len
) >> EXT4_BLOCK_SIZE_BITS(sb
);
5502 /* Call ext4_force_commit to flush all data in case of data=journal. */
5503 if (ext4_should_journal_data(inode
)) {
5504 ret
= ext4_force_commit(inode
->i_sb
);
5511 * There is no need to overlap collapse range with EOF, in which case
5512 * it is effectively a truncate operation
5514 if (offset
+ len
>= i_size_read(inode
)) {
5519 /* Currently just for extent based files */
5520 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
5525 /* Wait for existing dio to complete */
5526 inode_dio_wait(inode
);
5529 * Prevent page faults from reinstantiating pages we have released from
5532 down_write(&EXT4_I(inode
)->i_mmap_sem
);
5534 ret
= ext4_break_layouts(inode
);
5539 * Need to round down offset to be aligned with page size boundary
5540 * for page size > block size.
5542 ioffset
= round_down(offset
, PAGE_SIZE
);
5544 * Write tail of the last page before removed range since it will get
5545 * removed from the page cache below.
5547 ret
= filemap_write_and_wait_range(inode
->i_mapping
, ioffset
, offset
);
5551 * Write data that will be shifted to preserve them when discarding
5552 * page cache below. We are also protected from pages becoming dirty
5555 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
+ len
,
5559 truncate_pagecache(inode
, ioffset
);
5561 credits
= ext4_writepage_trans_blocks(inode
);
5562 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
5563 if (IS_ERR(handle
)) {
5564 ret
= PTR_ERR(handle
);
5568 down_write(&EXT4_I(inode
)->i_data_sem
);
5569 ext4_discard_preallocations(inode
);
5571 ret
= ext4_es_remove_extent(inode
, punch_start
,
5572 EXT_MAX_BLOCKS
- punch_start
);
5574 up_write(&EXT4_I(inode
)->i_data_sem
);
5578 ret
= ext4_ext_remove_space(inode
, punch_start
, punch_stop
- 1);
5580 up_write(&EXT4_I(inode
)->i_data_sem
);
5583 ext4_discard_preallocations(inode
);
5585 ret
= ext4_ext_shift_extents(inode
, handle
, punch_stop
,
5586 punch_stop
- punch_start
, SHIFT_LEFT
);
5588 up_write(&EXT4_I(inode
)->i_data_sem
);
5592 new_size
= i_size_read(inode
) - len
;
5593 i_size_write(inode
, new_size
);
5594 EXT4_I(inode
)->i_disksize
= new_size
;
5596 up_write(&EXT4_I(inode
)->i_data_sem
);
5598 ext4_handle_sync(handle
);
5599 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
5600 ext4_mark_inode_dirty(handle
, inode
);
5601 ext4_update_inode_fsync_trans(handle
, inode
, 1);
5604 ext4_journal_stop(handle
);
5606 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5608 inode_unlock(inode
);
5613 * ext4_insert_range:
5614 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5615 * The data blocks starting from @offset to the EOF are shifted by @len
5616 * towards right to create a hole in the @inode. Inode size is increased
5618 * Returns 0 on success, error otherwise.
5620 int ext4_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
5622 struct super_block
*sb
= inode
->i_sb
;
5624 struct ext4_ext_path
*path
;
5625 struct ext4_extent
*extent
;
5626 ext4_lblk_t offset_lblk
, len_lblk
, ee_start_lblk
= 0;
5627 unsigned int credits
, ee_len
;
5628 int ret
= 0, depth
, split_flag
= 0;
5632 * We need to test this early because xfstests assumes that an
5633 * insert range of (0, 1) will return EOPNOTSUPP if the file
5634 * system does not support insert range.
5636 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
5639 /* Insert range works only on fs block size aligned offsets. */
5640 if (offset
& (EXT4_CLUSTER_SIZE(sb
) - 1) ||
5641 len
& (EXT4_CLUSTER_SIZE(sb
) - 1))
5644 if (!S_ISREG(inode
->i_mode
))
5647 trace_ext4_insert_range(inode
, offset
, len
);
5649 offset_lblk
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
5650 len_lblk
= len
>> EXT4_BLOCK_SIZE_BITS(sb
);
5652 /* Call ext4_force_commit to flush all data in case of data=journal */
5653 if (ext4_should_journal_data(inode
)) {
5654 ret
= ext4_force_commit(inode
->i_sb
);
5660 /* Currently just for extent based files */
5661 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
5666 /* Check for wrap through zero */
5667 if (inode
->i_size
+ len
> inode
->i_sb
->s_maxbytes
) {
5672 /* Offset should be less than i_size */
5673 if (offset
>= i_size_read(inode
)) {
5678 /* Wait for existing dio to complete */
5679 inode_dio_wait(inode
);
5682 * Prevent page faults from reinstantiating pages we have released from
5685 down_write(&EXT4_I(inode
)->i_mmap_sem
);
5687 ret
= ext4_break_layouts(inode
);
5692 * Need to round down to align start offset to page size boundary
5693 * for page size > block size.
5695 ioffset
= round_down(offset
, PAGE_SIZE
);
5696 /* Write out all dirty pages */
5697 ret
= filemap_write_and_wait_range(inode
->i_mapping
, ioffset
,
5701 truncate_pagecache(inode
, ioffset
);
5703 credits
= ext4_writepage_trans_blocks(inode
);
5704 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
5705 if (IS_ERR(handle
)) {
5706 ret
= PTR_ERR(handle
);
5710 /* Expand file to avoid data loss if there is error while shifting */
5711 inode
->i_size
+= len
;
5712 EXT4_I(inode
)->i_disksize
+= len
;
5713 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
5714 ret
= ext4_mark_inode_dirty(handle
, inode
);
5718 down_write(&EXT4_I(inode
)->i_data_sem
);
5719 ext4_discard_preallocations(inode
);
5721 path
= ext4_find_extent(inode
, offset_lblk
, NULL
, 0);
5723 up_write(&EXT4_I(inode
)->i_data_sem
);
5727 depth
= ext_depth(inode
);
5728 extent
= path
[depth
].p_ext
;
5730 ee_start_lblk
= le32_to_cpu(extent
->ee_block
);
5731 ee_len
= ext4_ext_get_actual_len(extent
);
5734 * If offset_lblk is not the starting block of extent, split
5735 * the extent @offset_lblk
5737 if ((offset_lblk
> ee_start_lblk
) &&
5738 (offset_lblk
< (ee_start_lblk
+ ee_len
))) {
5739 if (ext4_ext_is_unwritten(extent
))
5740 split_flag
= EXT4_EXT_MARK_UNWRIT1
|
5741 EXT4_EXT_MARK_UNWRIT2
;
5742 ret
= ext4_split_extent_at(handle
, inode
, &path
,
5743 offset_lblk
, split_flag
,
5745 EXT4_GET_BLOCKS_PRE_IO
|
5746 EXT4_GET_BLOCKS_METADATA_NOFAIL
);
5749 ext4_ext_drop_refs(path
);
5752 up_write(&EXT4_I(inode
)->i_data_sem
);
5756 ext4_ext_drop_refs(path
);
5760 ret
= ext4_es_remove_extent(inode
, offset_lblk
,
5761 EXT_MAX_BLOCKS
- offset_lblk
);
5763 up_write(&EXT4_I(inode
)->i_data_sem
);
5768 * if offset_lblk lies in a hole which is at start of file, use
5769 * ee_start_lblk to shift extents
5771 ret
= ext4_ext_shift_extents(inode
, handle
,
5772 ee_start_lblk
> offset_lblk
? ee_start_lblk
: offset_lblk
,
5773 len_lblk
, SHIFT_RIGHT
);
5775 up_write(&EXT4_I(inode
)->i_data_sem
);
5777 ext4_handle_sync(handle
);
5779 ext4_update_inode_fsync_trans(handle
, inode
, 1);
5782 ext4_journal_stop(handle
);
5784 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5786 inode_unlock(inode
);
5791 * ext4_swap_extents - Swap extents between two inodes
5793 * @inode1: First inode
5794 * @inode2: Second inode
5795 * @lblk1: Start block for first inode
5796 * @lblk2: Start block for second inode
5797 * @count: Number of blocks to swap
5798 * @unwritten: Mark second inode's extents as unwritten after swap
5799 * @erp: Pointer to save error value
5801 * This helper routine does exactly what is promise "swap extents". All other
5802 * stuff such as page-cache locking consistency, bh mapping consistency or
5803 * extent's data copying must be performed by caller.
5805 * i_mutex is held for both inodes
5806 * i_data_sem is locked for write for both inodes
5808 * All pages from requested range are locked for both inodes
5811 ext4_swap_extents(handle_t
*handle
, struct inode
*inode1
,
5812 struct inode
*inode2
, ext4_lblk_t lblk1
, ext4_lblk_t lblk2
,
5813 ext4_lblk_t count
, int unwritten
, int *erp
)
5815 struct ext4_ext_path
*path1
= NULL
;
5816 struct ext4_ext_path
*path2
= NULL
;
5817 int replaced_count
= 0;
5819 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1
)->i_data_sem
));
5820 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2
)->i_data_sem
));
5821 BUG_ON(!inode_is_locked(inode1
));
5822 BUG_ON(!inode_is_locked(inode2
));
5824 *erp
= ext4_es_remove_extent(inode1
, lblk1
, count
);
5827 *erp
= ext4_es_remove_extent(inode2
, lblk2
, count
);
5832 struct ext4_extent
*ex1
, *ex2
, tmp_ex
;
5833 ext4_lblk_t e1_blk
, e2_blk
;
5834 int e1_len
, e2_len
, len
;
5837 path1
= ext4_find_extent(inode1
, lblk1
, NULL
, EXT4_EX_NOCACHE
);
5838 if (IS_ERR(path1
)) {
5839 *erp
= PTR_ERR(path1
);
5845 path2
= ext4_find_extent(inode2
, lblk2
, NULL
, EXT4_EX_NOCACHE
);
5846 if (IS_ERR(path2
)) {
5847 *erp
= PTR_ERR(path2
);
5851 ex1
= path1
[path1
->p_depth
].p_ext
;
5852 ex2
= path2
[path2
->p_depth
].p_ext
;
5853 /* Do we have somthing to swap ? */
5854 if (unlikely(!ex2
|| !ex1
))
5857 e1_blk
= le32_to_cpu(ex1
->ee_block
);
5858 e2_blk
= le32_to_cpu(ex2
->ee_block
);
5859 e1_len
= ext4_ext_get_actual_len(ex1
);
5860 e2_len
= ext4_ext_get_actual_len(ex2
);
5863 if (!in_range(lblk1
, e1_blk
, e1_len
) ||
5864 !in_range(lblk2
, e2_blk
, e2_len
)) {
5865 ext4_lblk_t next1
, next2
;
5867 /* if hole after extent, then go to next extent */
5868 next1
= ext4_ext_next_allocated_block(path1
);
5869 next2
= ext4_ext_next_allocated_block(path2
);
5870 /* If hole before extent, then shift to that extent */
5875 /* Do we have something to swap */
5876 if (next1
== EXT_MAX_BLOCKS
|| next2
== EXT_MAX_BLOCKS
)
5878 /* Move to the rightest boundary */
5879 len
= next1
- lblk1
;
5880 if (len
< next2
- lblk2
)
5881 len
= next2
- lblk2
;
5890 /* Prepare left boundary */
5891 if (e1_blk
< lblk1
) {
5893 *erp
= ext4_force_split_extent_at(handle
, inode1
,
5898 if (e2_blk
< lblk2
) {
5900 *erp
= ext4_force_split_extent_at(handle
, inode2
,
5905 /* ext4_split_extent_at() may result in leaf extent split,
5906 * path must to be revalidated. */
5910 /* Prepare right boundary */
5912 if (len
> e1_blk
+ e1_len
- lblk1
)
5913 len
= e1_blk
+ e1_len
- lblk1
;
5914 if (len
> e2_blk
+ e2_len
- lblk2
)
5915 len
= e2_blk
+ e2_len
- lblk2
;
5917 if (len
!= e1_len
) {
5919 *erp
= ext4_force_split_extent_at(handle
, inode1
,
5920 &path1
, lblk1
+ len
, 0);
5924 if (len
!= e2_len
) {
5926 *erp
= ext4_force_split_extent_at(handle
, inode2
,
5927 &path2
, lblk2
+ len
, 0);
5931 /* ext4_split_extent_at() may result in leaf extent split,
5932 * path must to be revalidated. */
5936 BUG_ON(e2_len
!= e1_len
);
5937 *erp
= ext4_ext_get_access(handle
, inode1
, path1
+ path1
->p_depth
);
5940 *erp
= ext4_ext_get_access(handle
, inode2
, path2
+ path2
->p_depth
);
5944 /* Both extents are fully inside boundaries. Swap it now */
5946 ext4_ext_store_pblock(ex1
, ext4_ext_pblock(ex2
));
5947 ext4_ext_store_pblock(ex2
, ext4_ext_pblock(&tmp_ex
));
5948 ex1
->ee_len
= cpu_to_le16(e2_len
);
5949 ex2
->ee_len
= cpu_to_le16(e1_len
);
5951 ext4_ext_mark_unwritten(ex2
);
5952 if (ext4_ext_is_unwritten(&tmp_ex
))
5953 ext4_ext_mark_unwritten(ex1
);
5955 ext4_ext_try_to_merge(handle
, inode2
, path2
, ex2
);
5956 ext4_ext_try_to_merge(handle
, inode1
, path1
, ex1
);
5957 *erp
= ext4_ext_dirty(handle
, inode2
, path2
+
5961 *erp
= ext4_ext_dirty(handle
, inode1
, path1
+
5964 * Looks scarry ah..? second inode already points to new blocks,
5965 * and it was successfully dirtied. But luckily error may happen
5966 * only due to journal error, so full transaction will be
5973 replaced_count
+= len
;
5977 ext4_ext_drop_refs(path1
);
5979 ext4_ext_drop_refs(path2
);
5981 path1
= path2
= NULL
;
5983 return replaced_count
;