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_trunc_restart_fn(struct inode
*inode
, int *dropped
)
106 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
107 * moment, get_block can be called only for blocks inside i_size since
108 * page cache has been already dropped and writes are blocked by
109 * i_mutex. So we can safely drop the i_data_sem here.
111 BUG_ON(EXT4_JOURNAL(inode
) == NULL
);
112 ext4_discard_preallocations(inode
);
113 up_write(&EXT4_I(inode
)->i_data_sem
);
119 * Make sure 'handle' has at least 'check_cred' credits. If not, restart
120 * transaction with 'restart_cred' credits. The function drops i_data_sem
121 * when restarting transaction and gets it after transaction is restarted.
123 * The function returns 0 on success, 1 if transaction had to be restarted,
124 * and < 0 in case of fatal error.
126 int ext4_datasem_ensure_credits(handle_t
*handle
, struct inode
*inode
,
127 int check_cred
, int restart_cred
,
133 ret
= ext4_journal_ensure_credits_fn(handle
, check_cred
, restart_cred
,
134 revoke_cred
, ext4_ext_trunc_restart_fn(inode
, &dropped
));
136 down_write(&EXT4_I(inode
)->i_data_sem
);
145 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
146 struct ext4_ext_path
*path
)
149 /* path points to block */
150 BUFFER_TRACE(path
->p_bh
, "get_write_access");
151 return ext4_journal_get_write_access(handle
, path
->p_bh
);
153 /* path points to leaf/index in inode body */
154 /* we use in-core data, no need to protect them */
164 int __ext4_ext_dirty(const char *where
, unsigned int line
, handle_t
*handle
,
165 struct inode
*inode
, struct ext4_ext_path
*path
)
169 WARN_ON(!rwsem_is_locked(&EXT4_I(inode
)->i_data_sem
));
171 ext4_extent_block_csum_set(inode
, ext_block_hdr(path
->p_bh
));
172 /* path points to block */
173 err
= __ext4_handle_dirty_metadata(where
, line
, handle
,
176 /* path points to leaf/index in inode body */
177 err
= ext4_mark_inode_dirty(handle
, inode
);
182 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
183 struct ext4_ext_path
*path
,
187 int depth
= path
->p_depth
;
188 struct ext4_extent
*ex
;
191 * Try to predict block placement assuming that we are
192 * filling in a file which will eventually be
193 * non-sparse --- i.e., in the case of libbfd writing
194 * an ELF object sections out-of-order but in a way
195 * the eventually results in a contiguous object or
196 * executable file, or some database extending a table
197 * space file. However, this is actually somewhat
198 * non-ideal if we are writing a sparse file such as
199 * qemu or KVM writing a raw image file that is going
200 * to stay fairly sparse, since it will end up
201 * fragmenting the file system's free space. Maybe we
202 * should have some hueristics or some way to allow
203 * userspace to pass a hint to file system,
204 * especially if the latter case turns out to be
207 ex
= path
[depth
].p_ext
;
209 ext4_fsblk_t ext_pblk
= ext4_ext_pblock(ex
);
210 ext4_lblk_t ext_block
= le32_to_cpu(ex
->ee_block
);
212 if (block
> ext_block
)
213 return ext_pblk
+ (block
- ext_block
);
215 return ext_pblk
- (ext_block
- block
);
218 /* it looks like index is empty;
219 * try to find starting block from index itself */
220 if (path
[depth
].p_bh
)
221 return path
[depth
].p_bh
->b_blocknr
;
224 /* OK. use inode's group */
225 return ext4_inode_to_goal_block(inode
);
229 * Allocation for a meta data block
232 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
233 struct ext4_ext_path
*path
,
234 struct ext4_extent
*ex
, int *err
, unsigned int flags
)
236 ext4_fsblk_t goal
, newblock
;
238 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
239 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, flags
,
244 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
248 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
249 / sizeof(struct ext4_extent
);
250 #ifdef AGGRESSIVE_TEST
251 if (!check
&& size
> 6)
257 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
261 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
262 / sizeof(struct ext4_extent_idx
);
263 #ifdef AGGRESSIVE_TEST
264 if (!check
&& size
> 5)
270 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
274 size
= sizeof(EXT4_I(inode
)->i_data
);
275 size
-= sizeof(struct ext4_extent_header
);
276 size
/= sizeof(struct ext4_extent
);
277 #ifdef AGGRESSIVE_TEST
278 if (!check
&& size
> 3)
284 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
288 size
= sizeof(EXT4_I(inode
)->i_data
);
289 size
-= sizeof(struct ext4_extent_header
);
290 size
/= sizeof(struct ext4_extent_idx
);
291 #ifdef AGGRESSIVE_TEST
292 if (!check
&& size
> 4)
299 ext4_force_split_extent_at(handle_t
*handle
, struct inode
*inode
,
300 struct ext4_ext_path
**ppath
, ext4_lblk_t lblk
,
303 struct ext4_ext_path
*path
= *ppath
;
304 int unwritten
= ext4_ext_is_unwritten(path
[path
->p_depth
].p_ext
);
306 return ext4_split_extent_at(handle
, inode
, ppath
, lblk
, unwritten
?
307 EXT4_EXT_MARK_UNWRIT1
|EXT4_EXT_MARK_UNWRIT2
: 0,
308 EXT4_EX_NOCACHE
| EXT4_GET_BLOCKS_PRE_IO
|
309 (nofail
? EXT4_GET_BLOCKS_METADATA_NOFAIL
:0));
313 * Calculate the number of metadata blocks needed
314 * to allocate @blocks
315 * Worse case is one block per extent
317 int ext4_ext_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
319 struct ext4_inode_info
*ei
= EXT4_I(inode
);
322 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
323 / sizeof(struct ext4_extent_idx
));
326 * If the new delayed allocation block is contiguous with the
327 * previous da block, it can share index blocks with the
328 * previous block, so we only need to allocate a new index
329 * block every idxs leaf blocks. At ldxs**2 blocks, we need
330 * an additional index block, and at ldxs**3 blocks, yet
331 * another index blocks.
333 if (ei
->i_da_metadata_calc_len
&&
334 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
337 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
339 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
341 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
343 ei
->i_da_metadata_calc_len
= 0;
345 ei
->i_da_metadata_calc_len
++;
346 ei
->i_da_metadata_calc_last_lblock
++;
351 * In the worst case we need a new set of index blocks at
352 * every level of the inode's extent tree.
354 ei
->i_da_metadata_calc_len
= 1;
355 ei
->i_da_metadata_calc_last_lblock
= lblock
;
356 return ext_depth(inode
) + 1;
360 ext4_ext_max_entries(struct inode
*inode
, int depth
)
364 if (depth
== ext_depth(inode
)) {
366 max
= ext4_ext_space_root(inode
, 1);
368 max
= ext4_ext_space_root_idx(inode
, 1);
371 max
= ext4_ext_space_block(inode
, 1);
373 max
= ext4_ext_space_block_idx(inode
, 1);
379 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
381 ext4_fsblk_t block
= ext4_ext_pblock(ext
);
382 int len
= ext4_ext_get_actual_len(ext
);
383 ext4_lblk_t lblock
= le32_to_cpu(ext
->ee_block
);
388 * - overflow/wrap-around
390 if (lblock
+ len
<= lblock
)
392 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
395 static int ext4_valid_extent_idx(struct inode
*inode
,
396 struct ext4_extent_idx
*ext_idx
)
398 ext4_fsblk_t block
= ext4_idx_pblock(ext_idx
);
400 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
403 static int ext4_valid_extent_entries(struct inode
*inode
,
404 struct ext4_extent_header
*eh
,
407 unsigned short entries
;
408 if (eh
->eh_entries
== 0)
411 entries
= le16_to_cpu(eh
->eh_entries
);
415 struct ext4_extent
*ext
= EXT_FIRST_EXTENT(eh
);
416 struct ext4_super_block
*es
= EXT4_SB(inode
->i_sb
)->s_es
;
417 ext4_fsblk_t pblock
= 0;
418 ext4_lblk_t lblock
= 0;
419 ext4_lblk_t prev
= 0;
422 if (!ext4_valid_extent(inode
, ext
))
425 /* Check for overlapping extents */
426 lblock
= le32_to_cpu(ext
->ee_block
);
427 len
= ext4_ext_get_actual_len(ext
);
428 if ((lblock
<= prev
) && prev
) {
429 pblock
= ext4_ext_pblock(ext
);
430 es
->s_last_error_block
= cpu_to_le64(pblock
);
435 prev
= lblock
+ len
- 1;
438 struct ext4_extent_idx
*ext_idx
= EXT_FIRST_INDEX(eh
);
440 if (!ext4_valid_extent_idx(inode
, ext_idx
))
449 static int __ext4_ext_check(const char *function
, unsigned int line
,
450 struct inode
*inode
, struct ext4_extent_header
*eh
,
451 int depth
, ext4_fsblk_t pblk
)
453 const char *error_msg
;
454 int max
= 0, err
= -EFSCORRUPTED
;
456 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
457 error_msg
= "invalid magic";
460 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
461 error_msg
= "unexpected eh_depth";
464 if (unlikely(eh
->eh_max
== 0)) {
465 error_msg
= "invalid eh_max";
468 max
= ext4_ext_max_entries(inode
, depth
);
469 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
470 error_msg
= "too large eh_max";
473 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
474 error_msg
= "invalid eh_entries";
477 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
478 error_msg
= "invalid extent entries";
481 if (unlikely(depth
> 32)) {
482 error_msg
= "too large eh_depth";
485 /* Verify checksum on non-root extent tree nodes */
486 if (ext_depth(inode
) != depth
&&
487 !ext4_extent_block_csum_verify(inode
, eh
)) {
488 error_msg
= "extent tree corrupted";
495 ext4_error_inode(inode
, function
, line
, 0,
496 "pblk %llu bad header/extent: %s - magic %x, "
497 "entries %u, max %u(%u), depth %u(%u)",
498 (unsigned long long) pblk
, error_msg
,
499 le16_to_cpu(eh
->eh_magic
),
500 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
501 max
, le16_to_cpu(eh
->eh_depth
), depth
);
505 #define ext4_ext_check(inode, eh, depth, pblk) \
506 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
508 int ext4_ext_check_inode(struct inode
*inode
)
510 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
), 0);
513 static struct buffer_head
*
514 __read_extent_tree_block(const char *function
, unsigned int line
,
515 struct inode
*inode
, ext4_fsblk_t pblk
, int depth
,
518 struct buffer_head
*bh
;
521 bh
= sb_getblk_gfp(inode
->i_sb
, pblk
, __GFP_MOVABLE
| GFP_NOFS
);
523 return ERR_PTR(-ENOMEM
);
525 if (!bh_uptodate_or_lock(bh
)) {
526 trace_ext4_ext_load_extent(inode
, pblk
, _RET_IP_
);
527 err
= bh_submit_read(bh
);
531 if (buffer_verified(bh
) && !(flags
& EXT4_EX_FORCE_CACHE
))
533 if (!ext4_has_feature_journal(inode
->i_sb
) ||
535 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_journal_inum
))) {
536 err
= __ext4_ext_check(function
, line
, inode
,
537 ext_block_hdr(bh
), depth
, pblk
);
541 set_buffer_verified(bh
);
543 * If this is a leaf block, cache all of its entries
545 if (!(flags
& EXT4_EX_NOCACHE
) && depth
== 0) {
546 struct ext4_extent_header
*eh
= ext_block_hdr(bh
);
547 struct ext4_extent
*ex
= EXT_FIRST_EXTENT(eh
);
548 ext4_lblk_t prev
= 0;
551 for (i
= le16_to_cpu(eh
->eh_entries
); i
> 0; i
--, ex
++) {
552 unsigned int status
= EXTENT_STATUS_WRITTEN
;
553 ext4_lblk_t lblk
= le32_to_cpu(ex
->ee_block
);
554 int len
= ext4_ext_get_actual_len(ex
);
556 if (prev
&& (prev
!= lblk
))
557 ext4_es_cache_extent(inode
, prev
,
561 if (ext4_ext_is_unwritten(ex
))
562 status
= EXTENT_STATUS_UNWRITTEN
;
563 ext4_es_cache_extent(inode
, lblk
, len
,
564 ext4_ext_pblock(ex
), status
);
575 #define read_extent_tree_block(inode, pblk, depth, flags) \
576 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
580 * This function is called to cache a file's extent information in the
583 int ext4_ext_precache(struct inode
*inode
)
585 struct ext4_inode_info
*ei
= EXT4_I(inode
);
586 struct ext4_ext_path
*path
= NULL
;
587 struct buffer_head
*bh
;
588 int i
= 0, depth
, ret
= 0;
590 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
591 return 0; /* not an extent-mapped inode */
593 down_read(&ei
->i_data_sem
);
594 depth
= ext_depth(inode
);
596 path
= kcalloc(depth
+ 1, sizeof(struct ext4_ext_path
),
599 up_read(&ei
->i_data_sem
);
603 /* Don't cache anything if there are no external extent blocks */
606 path
[0].p_hdr
= ext_inode_hdr(inode
);
607 ret
= ext4_ext_check(inode
, path
[0].p_hdr
, depth
, 0);
610 path
[0].p_idx
= EXT_FIRST_INDEX(path
[0].p_hdr
);
613 * If this is a leaf block or we've reached the end of
614 * the index block, go up
617 path
[i
].p_idx
> EXT_LAST_INDEX(path
[i
].p_hdr
)) {
618 brelse(path
[i
].p_bh
);
623 bh
= read_extent_tree_block(inode
,
624 ext4_idx_pblock(path
[i
].p_idx
++),
626 EXT4_EX_FORCE_CACHE
);
633 path
[i
].p_hdr
= ext_block_hdr(bh
);
634 path
[i
].p_idx
= EXT_FIRST_INDEX(path
[i
].p_hdr
);
636 ext4_set_inode_state(inode
, EXT4_STATE_EXT_PRECACHED
);
638 up_read(&ei
->i_data_sem
);
639 ext4_ext_drop_refs(path
);
645 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
647 int k
, l
= path
->p_depth
;
650 for (k
= 0; k
<= l
; k
++, path
++) {
652 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
653 ext4_idx_pblock(path
->p_idx
));
654 } else if (path
->p_ext
) {
655 ext_debug(" %d:[%d]%d:%llu ",
656 le32_to_cpu(path
->p_ext
->ee_block
),
657 ext4_ext_is_unwritten(path
->p_ext
),
658 ext4_ext_get_actual_len(path
->p_ext
),
659 ext4_ext_pblock(path
->p_ext
));
666 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
668 int depth
= ext_depth(inode
);
669 struct ext4_extent_header
*eh
;
670 struct ext4_extent
*ex
;
676 eh
= path
[depth
].p_hdr
;
677 ex
= EXT_FIRST_EXTENT(eh
);
679 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
681 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
682 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
683 ext4_ext_is_unwritten(ex
),
684 ext4_ext_get_actual_len(ex
), ext4_ext_pblock(ex
));
689 static void ext4_ext_show_move(struct inode
*inode
, struct ext4_ext_path
*path
,
690 ext4_fsblk_t newblock
, int level
)
692 int depth
= ext_depth(inode
);
693 struct ext4_extent
*ex
;
695 if (depth
!= level
) {
696 struct ext4_extent_idx
*idx
;
697 idx
= path
[level
].p_idx
;
698 while (idx
<= EXT_MAX_INDEX(path
[level
].p_hdr
)) {
699 ext_debug("%d: move %d:%llu in new index %llu\n", level
,
700 le32_to_cpu(idx
->ei_block
),
701 ext4_idx_pblock(idx
),
709 ex
= path
[depth
].p_ext
;
710 while (ex
<= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
711 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
712 le32_to_cpu(ex
->ee_block
),
714 ext4_ext_is_unwritten(ex
),
715 ext4_ext_get_actual_len(ex
),
722 #define ext4_ext_show_path(inode, path)
723 #define ext4_ext_show_leaf(inode, path)
724 #define ext4_ext_show_move(inode, path, newblock, level)
727 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
733 depth
= path
->p_depth
;
734 for (i
= 0; i
<= depth
; i
++, path
++)
742 * ext4_ext_binsearch_idx:
743 * binary search for the closest index of the given block
744 * the header must be checked before calling this
747 ext4_ext_binsearch_idx(struct inode
*inode
,
748 struct ext4_ext_path
*path
, ext4_lblk_t block
)
750 struct ext4_extent_header
*eh
= path
->p_hdr
;
751 struct ext4_extent_idx
*r
, *l
, *m
;
754 ext_debug("binsearch for %u(idx): ", block
);
756 l
= EXT_FIRST_INDEX(eh
) + 1;
757 r
= EXT_LAST_INDEX(eh
);
760 if (block
< le32_to_cpu(m
->ei_block
))
764 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
765 m
, le32_to_cpu(m
->ei_block
),
766 r
, le32_to_cpu(r
->ei_block
));
770 ext_debug(" -> %u->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
771 ext4_idx_pblock(path
->p_idx
));
773 #ifdef CHECK_BINSEARCH
775 struct ext4_extent_idx
*chix
, *ix
;
778 chix
= ix
= EXT_FIRST_INDEX(eh
);
779 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
781 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
782 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
784 ix
, EXT_FIRST_INDEX(eh
));
785 printk(KERN_DEBUG
"%u <= %u\n",
786 le32_to_cpu(ix
->ei_block
),
787 le32_to_cpu(ix
[-1].ei_block
));
789 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
790 <= le32_to_cpu(ix
[-1].ei_block
));
791 if (block
< le32_to_cpu(ix
->ei_block
))
795 BUG_ON(chix
!= path
->p_idx
);
802 * ext4_ext_binsearch:
803 * binary search for closest extent of the given block
804 * the header must be checked before calling this
807 ext4_ext_binsearch(struct inode
*inode
,
808 struct ext4_ext_path
*path
, ext4_lblk_t block
)
810 struct ext4_extent_header
*eh
= path
->p_hdr
;
811 struct ext4_extent
*r
, *l
, *m
;
813 if (eh
->eh_entries
== 0) {
815 * this leaf is empty:
816 * we get such a leaf in split/add case
821 ext_debug("binsearch for %u: ", block
);
823 l
= EXT_FIRST_EXTENT(eh
) + 1;
824 r
= EXT_LAST_EXTENT(eh
);
828 if (block
< le32_to_cpu(m
->ee_block
))
832 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
833 m
, le32_to_cpu(m
->ee_block
),
834 r
, le32_to_cpu(r
->ee_block
));
838 ext_debug(" -> %d:%llu:[%d]%d ",
839 le32_to_cpu(path
->p_ext
->ee_block
),
840 ext4_ext_pblock(path
->p_ext
),
841 ext4_ext_is_unwritten(path
->p_ext
),
842 ext4_ext_get_actual_len(path
->p_ext
));
844 #ifdef CHECK_BINSEARCH
846 struct ext4_extent
*chex
, *ex
;
849 chex
= ex
= EXT_FIRST_EXTENT(eh
);
850 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
851 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
852 <= le32_to_cpu(ex
[-1].ee_block
));
853 if (block
< le32_to_cpu(ex
->ee_block
))
857 BUG_ON(chex
!= path
->p_ext
);
863 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
865 struct ext4_extent_header
*eh
;
867 eh
= ext_inode_hdr(inode
);
870 eh
->eh_magic
= EXT4_EXT_MAGIC
;
871 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
872 ext4_mark_inode_dirty(handle
, inode
);
876 struct ext4_ext_path
*
877 ext4_find_extent(struct inode
*inode
, ext4_lblk_t block
,
878 struct ext4_ext_path
**orig_path
, int flags
)
880 struct ext4_extent_header
*eh
;
881 struct buffer_head
*bh
;
882 struct ext4_ext_path
*path
= orig_path
? *orig_path
: NULL
;
883 short int depth
, i
, ppos
= 0;
886 eh
= ext_inode_hdr(inode
);
887 depth
= ext_depth(inode
);
888 if (depth
< 0 || depth
> EXT4_MAX_EXTENT_DEPTH
) {
889 EXT4_ERROR_INODE(inode
, "inode has invalid extent depth: %d",
896 ext4_ext_drop_refs(path
);
897 if (depth
> path
[0].p_maxdepth
) {
899 *orig_path
= path
= NULL
;
903 /* account possible depth increase */
904 path
= kcalloc(depth
+ 2, sizeof(struct ext4_ext_path
),
907 return ERR_PTR(-ENOMEM
);
908 path
[0].p_maxdepth
= depth
+ 1;
914 /* walk through the tree */
916 ext_debug("depth %d: num %d, max %d\n",
917 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
919 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
920 path
[ppos
].p_block
= ext4_idx_pblock(path
[ppos
].p_idx
);
921 path
[ppos
].p_depth
= i
;
922 path
[ppos
].p_ext
= NULL
;
924 bh
= read_extent_tree_block(inode
, path
[ppos
].p_block
, --i
,
931 eh
= ext_block_hdr(bh
);
933 path
[ppos
].p_bh
= bh
;
934 path
[ppos
].p_hdr
= eh
;
937 path
[ppos
].p_depth
= i
;
938 path
[ppos
].p_ext
= NULL
;
939 path
[ppos
].p_idx
= NULL
;
942 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
943 /* if not an empty leaf */
944 if (path
[ppos
].p_ext
)
945 path
[ppos
].p_block
= ext4_ext_pblock(path
[ppos
].p_ext
);
947 ext4_ext_show_path(inode
, path
);
952 ext4_ext_drop_refs(path
);
960 * ext4_ext_insert_index:
961 * insert new index [@logical;@ptr] into the block at @curp;
962 * check where to insert: before @curp or after @curp
964 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
965 struct ext4_ext_path
*curp
,
966 int logical
, ext4_fsblk_t ptr
)
968 struct ext4_extent_idx
*ix
;
971 err
= ext4_ext_get_access(handle
, inode
, curp
);
975 if (unlikely(logical
== le32_to_cpu(curp
->p_idx
->ei_block
))) {
976 EXT4_ERROR_INODE(inode
,
977 "logical %d == ei_block %d!",
978 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
979 return -EFSCORRUPTED
;
982 if (unlikely(le16_to_cpu(curp
->p_hdr
->eh_entries
)
983 >= le16_to_cpu(curp
->p_hdr
->eh_max
))) {
984 EXT4_ERROR_INODE(inode
,
985 "eh_entries %d >= eh_max %d!",
986 le16_to_cpu(curp
->p_hdr
->eh_entries
),
987 le16_to_cpu(curp
->p_hdr
->eh_max
));
988 return -EFSCORRUPTED
;
991 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
993 ext_debug("insert new index %d after: %llu\n", logical
, ptr
);
994 ix
= curp
->p_idx
+ 1;
997 ext_debug("insert new index %d before: %llu\n", logical
, ptr
);
1001 len
= EXT_LAST_INDEX(curp
->p_hdr
) - ix
+ 1;
1004 ext_debug("insert new index %d: "
1005 "move %d indices from 0x%p to 0x%p\n",
1006 logical
, len
, ix
, ix
+ 1);
1007 memmove(ix
+ 1, ix
, len
* sizeof(struct ext4_extent_idx
));
1010 if (unlikely(ix
> EXT_MAX_INDEX(curp
->p_hdr
))) {
1011 EXT4_ERROR_INODE(inode
, "ix > EXT_MAX_INDEX!");
1012 return -EFSCORRUPTED
;
1015 ix
->ei_block
= cpu_to_le32(logical
);
1016 ext4_idx_store_pblock(ix
, ptr
);
1017 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
1019 if (unlikely(ix
> EXT_LAST_INDEX(curp
->p_hdr
))) {
1020 EXT4_ERROR_INODE(inode
, "ix > EXT_LAST_INDEX!");
1021 return -EFSCORRUPTED
;
1024 err
= ext4_ext_dirty(handle
, inode
, curp
);
1025 ext4_std_error(inode
->i_sb
, err
);
1032 * inserts new subtree into the path, using free index entry
1034 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1035 * - makes decision where to split
1036 * - moves remaining extents and index entries (right to the split point)
1037 * into the newly allocated blocks
1038 * - initializes subtree
1040 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
1042 struct ext4_ext_path
*path
,
1043 struct ext4_extent
*newext
, int at
)
1045 struct buffer_head
*bh
= NULL
;
1046 int depth
= ext_depth(inode
);
1047 struct ext4_extent_header
*neh
;
1048 struct ext4_extent_idx
*fidx
;
1049 int i
= at
, k
, m
, a
;
1050 ext4_fsblk_t newblock
, oldblock
;
1052 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
1054 size_t ext_size
= 0;
1056 /* make decision: where to split? */
1057 /* FIXME: now decision is simplest: at current extent */
1059 /* if current leaf will be split, then we should use
1060 * border from split point */
1061 if (unlikely(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
))) {
1062 EXT4_ERROR_INODE(inode
, "p_ext > EXT_MAX_EXTENT!");
1063 return -EFSCORRUPTED
;
1065 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
1066 border
= path
[depth
].p_ext
[1].ee_block
;
1067 ext_debug("leaf will be split."
1068 " next leaf starts at %d\n",
1069 le32_to_cpu(border
));
1071 border
= newext
->ee_block
;
1072 ext_debug("leaf will be added."
1073 " next leaf starts at %d\n",
1074 le32_to_cpu(border
));
1078 * If error occurs, then we break processing
1079 * and mark filesystem read-only. index won't
1080 * be inserted and tree will be in consistent
1081 * state. Next mount will repair buffers too.
1085 * Get array to track all allocated blocks.
1086 * We need this to handle errors and free blocks
1089 ablocks
= kcalloc(depth
, sizeof(ext4_fsblk_t
), GFP_NOFS
);
1093 /* allocate all needed blocks */
1094 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
1095 for (a
= 0; a
< depth
- at
; a
++) {
1096 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
1097 newext
, &err
, flags
);
1100 ablocks
[a
] = newblock
;
1103 /* initialize new leaf */
1104 newblock
= ablocks
[--a
];
1105 if (unlikely(newblock
== 0)) {
1106 EXT4_ERROR_INODE(inode
, "newblock == 0!");
1107 err
= -EFSCORRUPTED
;
1110 bh
= sb_getblk_gfp(inode
->i_sb
, newblock
, __GFP_MOVABLE
| GFP_NOFS
);
1111 if (unlikely(!bh
)) {
1117 err
= ext4_journal_get_create_access(handle
, bh
);
1121 neh
= ext_block_hdr(bh
);
1122 neh
->eh_entries
= 0;
1123 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1124 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1127 /* move remainder of path[depth] to the new leaf */
1128 if (unlikely(path
[depth
].p_hdr
->eh_entries
!=
1129 path
[depth
].p_hdr
->eh_max
)) {
1130 EXT4_ERROR_INODE(inode
, "eh_entries %d != eh_max %d!",
1131 path
[depth
].p_hdr
->eh_entries
,
1132 path
[depth
].p_hdr
->eh_max
);
1133 err
= -EFSCORRUPTED
;
1136 /* start copy from next extent */
1137 m
= EXT_MAX_EXTENT(path
[depth
].p_hdr
) - path
[depth
].p_ext
++;
1138 ext4_ext_show_move(inode
, path
, newblock
, depth
);
1140 struct ext4_extent
*ex
;
1141 ex
= EXT_FIRST_EXTENT(neh
);
1142 memmove(ex
, path
[depth
].p_ext
, sizeof(struct ext4_extent
) * m
);
1143 le16_add_cpu(&neh
->eh_entries
, m
);
1146 /* zero out unused area in the extent block */
1147 ext_size
= sizeof(struct ext4_extent_header
) +
1148 sizeof(struct ext4_extent
) * le16_to_cpu(neh
->eh_entries
);
1149 memset(bh
->b_data
+ ext_size
, 0, inode
->i_sb
->s_blocksize
- ext_size
);
1150 ext4_extent_block_csum_set(inode
, neh
);
1151 set_buffer_uptodate(bh
);
1154 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1160 /* correct old leaf */
1162 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1165 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
1166 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1172 /* create intermediate indexes */
1174 if (unlikely(k
< 0)) {
1175 EXT4_ERROR_INODE(inode
, "k %d < 0!", k
);
1176 err
= -EFSCORRUPTED
;
1180 ext_debug("create %d intermediate indices\n", k
);
1181 /* insert new index into current index block */
1182 /* current depth stored in i var */
1185 oldblock
= newblock
;
1186 newblock
= ablocks
[--a
];
1187 bh
= sb_getblk(inode
->i_sb
, newblock
);
1188 if (unlikely(!bh
)) {
1194 err
= ext4_journal_get_create_access(handle
, bh
);
1198 neh
= ext_block_hdr(bh
);
1199 neh
->eh_entries
= cpu_to_le16(1);
1200 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1201 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1202 neh
->eh_depth
= cpu_to_le16(depth
- i
);
1203 fidx
= EXT_FIRST_INDEX(neh
);
1204 fidx
->ei_block
= border
;
1205 ext4_idx_store_pblock(fidx
, oldblock
);
1207 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1208 i
, newblock
, le32_to_cpu(border
), oldblock
);
1210 /* move remainder of path[i] to the new index block */
1211 if (unlikely(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
1212 EXT_LAST_INDEX(path
[i
].p_hdr
))) {
1213 EXT4_ERROR_INODE(inode
,
1214 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1215 le32_to_cpu(path
[i
].p_ext
->ee_block
));
1216 err
= -EFSCORRUPTED
;
1219 /* start copy indexes */
1220 m
= EXT_MAX_INDEX(path
[i
].p_hdr
) - path
[i
].p_idx
++;
1221 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
1222 EXT_MAX_INDEX(path
[i
].p_hdr
));
1223 ext4_ext_show_move(inode
, path
, newblock
, i
);
1225 memmove(++fidx
, path
[i
].p_idx
,
1226 sizeof(struct ext4_extent_idx
) * m
);
1227 le16_add_cpu(&neh
->eh_entries
, m
);
1229 /* zero out unused area in the extent block */
1230 ext_size
= sizeof(struct ext4_extent_header
) +
1231 (sizeof(struct ext4_extent
) * le16_to_cpu(neh
->eh_entries
));
1232 memset(bh
->b_data
+ ext_size
, 0,
1233 inode
->i_sb
->s_blocksize
- ext_size
);
1234 ext4_extent_block_csum_set(inode
, neh
);
1235 set_buffer_uptodate(bh
);
1238 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1244 /* correct old index */
1246 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
1249 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1250 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1258 /* insert new index */
1259 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1260 le32_to_cpu(border
), newblock
);
1264 if (buffer_locked(bh
))
1270 /* free all allocated blocks in error case */
1271 for (i
= 0; i
< depth
; i
++) {
1274 ext4_free_blocks(handle
, inode
, NULL
, ablocks
[i
], 1,
1275 EXT4_FREE_BLOCKS_METADATA
);
1284 * ext4_ext_grow_indepth:
1285 * implements tree growing procedure:
1286 * - allocates new block
1287 * - moves top-level data (index block or leaf) into the new block
1288 * - initializes new top-level, creating index that points to the
1289 * just created block
1291 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1294 struct ext4_extent_header
*neh
;
1295 struct buffer_head
*bh
;
1296 ext4_fsblk_t newblock
, goal
= 0;
1297 struct ext4_super_block
*es
= EXT4_SB(inode
->i_sb
)->s_es
;
1299 size_t ext_size
= 0;
1301 /* Try to prepend new index to old one */
1302 if (ext_depth(inode
))
1303 goal
= ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode
)));
1304 if (goal
> le32_to_cpu(es
->s_first_data_block
)) {
1305 flags
|= EXT4_MB_HINT_TRY_GOAL
;
1308 goal
= ext4_inode_to_goal_block(inode
);
1309 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, flags
,
1314 bh
= sb_getblk_gfp(inode
->i_sb
, newblock
, __GFP_MOVABLE
| GFP_NOFS
);
1319 err
= ext4_journal_get_create_access(handle
, bh
);
1325 ext_size
= sizeof(EXT4_I(inode
)->i_data
);
1326 /* move top-level index/leaf into new block */
1327 memmove(bh
->b_data
, EXT4_I(inode
)->i_data
, ext_size
);
1328 /* zero out unused area in the extent block */
1329 memset(bh
->b_data
+ ext_size
, 0, inode
->i_sb
->s_blocksize
- ext_size
);
1331 /* set size of new block */
1332 neh
= ext_block_hdr(bh
);
1333 /* old root could have indexes or leaves
1334 * so calculate e_max right way */
1335 if (ext_depth(inode
))
1336 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1338 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1339 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1340 ext4_extent_block_csum_set(inode
, neh
);
1341 set_buffer_uptodate(bh
);
1344 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1348 /* Update top-level index: num,max,pointer */
1349 neh
= ext_inode_hdr(inode
);
1350 neh
->eh_entries
= cpu_to_le16(1);
1351 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh
), newblock
);
1352 if (neh
->eh_depth
== 0) {
1353 /* Root extent block becomes index block */
1354 neh
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1355 EXT_FIRST_INDEX(neh
)->ei_block
=
1356 EXT_FIRST_EXTENT(neh
)->ee_block
;
1358 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1359 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1360 le32_to_cpu(EXT_FIRST_INDEX(neh
)->ei_block
),
1361 ext4_idx_pblock(EXT_FIRST_INDEX(neh
)));
1363 le16_add_cpu(&neh
->eh_depth
, 1);
1364 ext4_mark_inode_dirty(handle
, inode
);
1372 * ext4_ext_create_new_leaf:
1373 * finds empty index and adds new leaf.
1374 * if no free index is found, then it requests in-depth growing.
1376 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1377 unsigned int mb_flags
,
1378 unsigned int gb_flags
,
1379 struct ext4_ext_path
**ppath
,
1380 struct ext4_extent
*newext
)
1382 struct ext4_ext_path
*path
= *ppath
;
1383 struct ext4_ext_path
*curp
;
1384 int depth
, i
, err
= 0;
1387 i
= depth
= ext_depth(inode
);
1389 /* walk up to the tree and look for free index entry */
1390 curp
= path
+ depth
;
1391 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1396 /* we use already allocated block for index block,
1397 * so subsequent data blocks should be contiguous */
1398 if (EXT_HAS_FREE_INDEX(curp
)) {
1399 /* if we found index with free entry, then use that
1400 * entry: create all needed subtree and add new leaf */
1401 err
= ext4_ext_split(handle
, inode
, mb_flags
, path
, newext
, i
);
1406 path
= ext4_find_extent(inode
,
1407 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1410 err
= PTR_ERR(path
);
1412 /* tree is full, time to grow in depth */
1413 err
= ext4_ext_grow_indepth(handle
, inode
, mb_flags
);
1418 path
= ext4_find_extent(inode
,
1419 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1422 err
= PTR_ERR(path
);
1427 * only first (depth 0 -> 1) produces free space;
1428 * in all other cases we have to split the grown tree
1430 depth
= ext_depth(inode
);
1431 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1432 /* now we need to split */
1442 * search the closest allocated block to the left for *logical
1443 * and returns it at @logical + it's physical address at @phys
1444 * if *logical is the smallest allocated block, the function
1445 * returns 0 at @phys
1446 * return value contains 0 (success) or error code
1448 static int ext4_ext_search_left(struct inode
*inode
,
1449 struct ext4_ext_path
*path
,
1450 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1452 struct ext4_extent_idx
*ix
;
1453 struct ext4_extent
*ex
;
1456 if (unlikely(path
== NULL
)) {
1457 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1458 return -EFSCORRUPTED
;
1460 depth
= path
->p_depth
;
1463 if (depth
== 0 && path
->p_ext
== NULL
)
1466 /* usually extent in the path covers blocks smaller
1467 * then *logical, but it can be that extent is the
1468 * first one in the file */
1470 ex
= path
[depth
].p_ext
;
1471 ee_len
= ext4_ext_get_actual_len(ex
);
1472 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1473 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1474 EXT4_ERROR_INODE(inode
,
1475 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1476 *logical
, le32_to_cpu(ex
->ee_block
));
1477 return -EFSCORRUPTED
;
1479 while (--depth
>= 0) {
1480 ix
= path
[depth
].p_idx
;
1481 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1482 EXT4_ERROR_INODE(inode
,
1483 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1484 ix
!= NULL
? le32_to_cpu(ix
->ei_block
) : 0,
1485 EXT_FIRST_INDEX(path
[depth
].p_hdr
) != NULL
?
1486 le32_to_cpu(EXT_FIRST_INDEX(path
[depth
].p_hdr
)->ei_block
) : 0,
1488 return -EFSCORRUPTED
;
1494 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1495 EXT4_ERROR_INODE(inode
,
1496 "logical %d < ee_block %d + ee_len %d!",
1497 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1498 return -EFSCORRUPTED
;
1501 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1502 *phys
= ext4_ext_pblock(ex
) + ee_len
- 1;
1507 * search the closest allocated block to the right for *logical
1508 * and returns it at @logical + it's physical address at @phys
1509 * if *logical is the largest allocated block, the function
1510 * returns 0 at @phys
1511 * return value contains 0 (success) or error code
1513 static int ext4_ext_search_right(struct inode
*inode
,
1514 struct ext4_ext_path
*path
,
1515 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
,
1516 struct ext4_extent
**ret_ex
)
1518 struct buffer_head
*bh
= NULL
;
1519 struct ext4_extent_header
*eh
;
1520 struct ext4_extent_idx
*ix
;
1521 struct ext4_extent
*ex
;
1523 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1526 if (unlikely(path
== NULL
)) {
1527 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1528 return -EFSCORRUPTED
;
1530 depth
= path
->p_depth
;
1533 if (depth
== 0 && path
->p_ext
== NULL
)
1536 /* usually extent in the path covers blocks smaller
1537 * then *logical, but it can be that extent is the
1538 * first one in the file */
1540 ex
= path
[depth
].p_ext
;
1541 ee_len
= ext4_ext_get_actual_len(ex
);
1542 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1543 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1544 EXT4_ERROR_INODE(inode
,
1545 "first_extent(path[%d].p_hdr) != ex",
1547 return -EFSCORRUPTED
;
1549 while (--depth
>= 0) {
1550 ix
= path
[depth
].p_idx
;
1551 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1552 EXT4_ERROR_INODE(inode
,
1553 "ix != EXT_FIRST_INDEX *logical %d!",
1555 return -EFSCORRUPTED
;
1561 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1562 EXT4_ERROR_INODE(inode
,
1563 "logical %d < ee_block %d + ee_len %d!",
1564 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1565 return -EFSCORRUPTED
;
1568 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1569 /* next allocated block in this leaf */
1574 /* go up and search for index to the right */
1575 while (--depth
>= 0) {
1576 ix
= path
[depth
].p_idx
;
1577 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1581 /* we've gone up to the root and found no index to the right */
1585 /* we've found index to the right, let's
1586 * follow it and find the closest allocated
1587 * block to the right */
1589 block
= ext4_idx_pblock(ix
);
1590 while (++depth
< path
->p_depth
) {
1591 /* subtract from p_depth to get proper eh_depth */
1592 bh
= read_extent_tree_block(inode
, block
,
1593 path
->p_depth
- depth
, 0);
1596 eh
= ext_block_hdr(bh
);
1597 ix
= EXT_FIRST_INDEX(eh
);
1598 block
= ext4_idx_pblock(ix
);
1602 bh
= read_extent_tree_block(inode
, block
, path
->p_depth
- depth
, 0);
1605 eh
= ext_block_hdr(bh
);
1606 ex
= EXT_FIRST_EXTENT(eh
);
1608 *logical
= le32_to_cpu(ex
->ee_block
);
1609 *phys
= ext4_ext_pblock(ex
);
1617 * ext4_ext_next_allocated_block:
1618 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1619 * NOTE: it considers block number from index entry as
1620 * allocated block. Thus, index entries have to be consistent
1624 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1628 BUG_ON(path
== NULL
);
1629 depth
= path
->p_depth
;
1631 if (depth
== 0 && path
->p_ext
== NULL
)
1632 return EXT_MAX_BLOCKS
;
1634 while (depth
>= 0) {
1635 if (depth
== path
->p_depth
) {
1637 if (path
[depth
].p_ext
&&
1638 path
[depth
].p_ext
!=
1639 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1640 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1643 if (path
[depth
].p_idx
!=
1644 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1645 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1650 return EXT_MAX_BLOCKS
;
1654 * ext4_ext_next_leaf_block:
1655 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1657 static ext4_lblk_t
ext4_ext_next_leaf_block(struct ext4_ext_path
*path
)
1661 BUG_ON(path
== NULL
);
1662 depth
= path
->p_depth
;
1664 /* zero-tree has no leaf blocks at all */
1666 return EXT_MAX_BLOCKS
;
1668 /* go to index block */
1671 while (depth
>= 0) {
1672 if (path
[depth
].p_idx
!=
1673 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1674 return (ext4_lblk_t
)
1675 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1679 return EXT_MAX_BLOCKS
;
1683 * ext4_ext_correct_indexes:
1684 * if leaf gets modified and modified extent is first in the leaf,
1685 * then we have to correct all indexes above.
1686 * TODO: do we need to correct tree in all cases?
1688 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1689 struct ext4_ext_path
*path
)
1691 struct ext4_extent_header
*eh
;
1692 int depth
= ext_depth(inode
);
1693 struct ext4_extent
*ex
;
1697 eh
= path
[depth
].p_hdr
;
1698 ex
= path
[depth
].p_ext
;
1700 if (unlikely(ex
== NULL
|| eh
== NULL
)) {
1701 EXT4_ERROR_INODE(inode
,
1702 "ex %p == NULL or eh %p == NULL", ex
, eh
);
1703 return -EFSCORRUPTED
;
1707 /* there is no tree at all */
1711 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1712 /* we correct tree if first leaf got modified only */
1717 * TODO: we need correction if border is smaller than current one
1720 border
= path
[depth
].p_ext
->ee_block
;
1721 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1724 path
[k
].p_idx
->ei_block
= border
;
1725 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1730 /* change all left-side indexes */
1731 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1733 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1736 path
[k
].p_idx
->ei_block
= border
;
1737 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1746 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1747 struct ext4_extent
*ex2
)
1749 unsigned short ext1_ee_len
, ext2_ee_len
;
1751 if (ext4_ext_is_unwritten(ex1
) != ext4_ext_is_unwritten(ex2
))
1754 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1755 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1757 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1758 le32_to_cpu(ex2
->ee_block
))
1762 * To allow future support for preallocated extents to be added
1763 * as an RO_COMPAT feature, refuse to merge to extents if
1764 * this can result in the top bit of ee_len being set.
1766 if (ext1_ee_len
+ ext2_ee_len
> EXT_INIT_MAX_LEN
)
1769 if (ext4_ext_is_unwritten(ex1
) &&
1770 ext1_ee_len
+ ext2_ee_len
> EXT_UNWRITTEN_MAX_LEN
)
1772 #ifdef AGGRESSIVE_TEST
1773 if (ext1_ee_len
>= 4)
1777 if (ext4_ext_pblock(ex1
) + ext1_ee_len
== ext4_ext_pblock(ex2
))
1783 * This function tries to merge the "ex" extent to the next extent in the tree.
1784 * It always tries to merge towards right. If you want to merge towards
1785 * left, pass "ex - 1" as argument instead of "ex".
1786 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1787 * 1 if they got merged.
1789 static int ext4_ext_try_to_merge_right(struct inode
*inode
,
1790 struct ext4_ext_path
*path
,
1791 struct ext4_extent
*ex
)
1793 struct ext4_extent_header
*eh
;
1794 unsigned int depth
, len
;
1795 int merge_done
= 0, unwritten
;
1797 depth
= ext_depth(inode
);
1798 BUG_ON(path
[depth
].p_hdr
== NULL
);
1799 eh
= path
[depth
].p_hdr
;
1801 while (ex
< EXT_LAST_EXTENT(eh
)) {
1802 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1804 /* merge with next extent! */
1805 unwritten
= ext4_ext_is_unwritten(ex
);
1806 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1807 + ext4_ext_get_actual_len(ex
+ 1));
1809 ext4_ext_mark_unwritten(ex
);
1811 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1812 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1813 * sizeof(struct ext4_extent
);
1814 memmove(ex
+ 1, ex
+ 2, len
);
1816 le16_add_cpu(&eh
->eh_entries
, -1);
1818 WARN_ON(eh
->eh_entries
== 0);
1819 if (!eh
->eh_entries
)
1820 EXT4_ERROR_INODE(inode
, "eh->eh_entries = 0!");
1827 * This function does a very simple check to see if we can collapse
1828 * an extent tree with a single extent tree leaf block into the inode.
1830 static void ext4_ext_try_to_merge_up(handle_t
*handle
,
1831 struct inode
*inode
,
1832 struct ext4_ext_path
*path
)
1835 unsigned max_root
= ext4_ext_space_root(inode
, 0);
1838 if ((path
[0].p_depth
!= 1) ||
1839 (le16_to_cpu(path
[0].p_hdr
->eh_entries
) != 1) ||
1840 (le16_to_cpu(path
[1].p_hdr
->eh_entries
) > max_root
))
1844 * We need to modify the block allocation bitmap and the block
1845 * group descriptor to release the extent tree block. If we
1846 * can't get the journal credits, give up.
1848 if (ext4_journal_extend(handle
, 2,
1849 ext4_free_metadata_revoke_credits(inode
->i_sb
, 1)))
1853 * Copy the extent data up to the inode
1855 blk
= ext4_idx_pblock(path
[0].p_idx
);
1856 s
= le16_to_cpu(path
[1].p_hdr
->eh_entries
) *
1857 sizeof(struct ext4_extent_idx
);
1858 s
+= sizeof(struct ext4_extent_header
);
1860 path
[1].p_maxdepth
= path
[0].p_maxdepth
;
1861 memcpy(path
[0].p_hdr
, path
[1].p_hdr
, s
);
1862 path
[0].p_depth
= 0;
1863 path
[0].p_ext
= EXT_FIRST_EXTENT(path
[0].p_hdr
) +
1864 (path
[1].p_ext
- EXT_FIRST_EXTENT(path
[1].p_hdr
));
1865 path
[0].p_hdr
->eh_max
= cpu_to_le16(max_root
);
1867 brelse(path
[1].p_bh
);
1868 ext4_free_blocks(handle
, inode
, NULL
, blk
, 1,
1869 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
1873 * This function tries to merge the @ex extent to neighbours in the tree.
1874 * return 1 if merge left else 0.
1876 static void ext4_ext_try_to_merge(handle_t
*handle
,
1877 struct inode
*inode
,
1878 struct ext4_ext_path
*path
,
1879 struct ext4_extent
*ex
) {
1880 struct ext4_extent_header
*eh
;
1884 depth
= ext_depth(inode
);
1885 BUG_ON(path
[depth
].p_hdr
== NULL
);
1886 eh
= path
[depth
].p_hdr
;
1888 if (ex
> EXT_FIRST_EXTENT(eh
))
1889 merge_done
= ext4_ext_try_to_merge_right(inode
, path
, ex
- 1);
1892 (void) ext4_ext_try_to_merge_right(inode
, path
, ex
);
1894 ext4_ext_try_to_merge_up(handle
, inode
, path
);
1898 * check if a portion of the "newext" extent overlaps with an
1901 * If there is an overlap discovered, it updates the length of the newext
1902 * such that there will be no overlap, and then returns 1.
1903 * If there is no overlap found, it returns 0.
1905 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info
*sbi
,
1906 struct inode
*inode
,
1907 struct ext4_extent
*newext
,
1908 struct ext4_ext_path
*path
)
1911 unsigned int depth
, len1
;
1912 unsigned int ret
= 0;
1914 b1
= le32_to_cpu(newext
->ee_block
);
1915 len1
= ext4_ext_get_actual_len(newext
);
1916 depth
= ext_depth(inode
);
1917 if (!path
[depth
].p_ext
)
1919 b2
= EXT4_LBLK_CMASK(sbi
, le32_to_cpu(path
[depth
].p_ext
->ee_block
));
1922 * get the next allocated block if the extent in the path
1923 * is before the requested block(s)
1926 b2
= ext4_ext_next_allocated_block(path
);
1927 if (b2
== EXT_MAX_BLOCKS
)
1929 b2
= EXT4_LBLK_CMASK(sbi
, b2
);
1932 /* check for wrap through zero on extent logical start block*/
1933 if (b1
+ len1
< b1
) {
1934 len1
= EXT_MAX_BLOCKS
- b1
;
1935 newext
->ee_len
= cpu_to_le16(len1
);
1939 /* check for overlap */
1940 if (b1
+ len1
> b2
) {
1941 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1949 * ext4_ext_insert_extent:
1950 * tries to merge requsted extent into the existing extent or
1951 * inserts requested extent as new one into the tree,
1952 * creating new leaf in the no-space case.
1954 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1955 struct ext4_ext_path
**ppath
,
1956 struct ext4_extent
*newext
, int gb_flags
)
1958 struct ext4_ext_path
*path
= *ppath
;
1959 struct ext4_extent_header
*eh
;
1960 struct ext4_extent
*ex
, *fex
;
1961 struct ext4_extent
*nearex
; /* nearest extent */
1962 struct ext4_ext_path
*npath
= NULL
;
1963 int depth
, len
, err
;
1965 int mb_flags
= 0, unwritten
;
1967 if (gb_flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
1968 mb_flags
|= EXT4_MB_DELALLOC_RESERVED
;
1969 if (unlikely(ext4_ext_get_actual_len(newext
) == 0)) {
1970 EXT4_ERROR_INODE(inode
, "ext4_ext_get_actual_len(newext) == 0");
1971 return -EFSCORRUPTED
;
1973 depth
= ext_depth(inode
);
1974 ex
= path
[depth
].p_ext
;
1975 eh
= path
[depth
].p_hdr
;
1976 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1977 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1978 return -EFSCORRUPTED
;
1981 /* try to insert block into found extent and return */
1982 if (ex
&& !(gb_flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
1985 * Try to see whether we should rather test the extent on
1986 * right from ex, or from the left of ex. This is because
1987 * ext4_find_extent() can return either extent on the
1988 * left, or on the right from the searched position. This
1989 * will make merging more effective.
1991 if (ex
< EXT_LAST_EXTENT(eh
) &&
1992 (le32_to_cpu(ex
->ee_block
) +
1993 ext4_ext_get_actual_len(ex
) <
1994 le32_to_cpu(newext
->ee_block
))) {
1997 } else if ((ex
> EXT_FIRST_EXTENT(eh
)) &&
1998 (le32_to_cpu(newext
->ee_block
) +
1999 ext4_ext_get_actual_len(newext
) <
2000 le32_to_cpu(ex
->ee_block
)))
2003 /* Try to append newex to the ex */
2004 if (ext4_can_extents_be_merged(inode
, ex
, newext
)) {
2005 ext_debug("append [%d]%d block to %u:[%d]%d"
2007 ext4_ext_is_unwritten(newext
),
2008 ext4_ext_get_actual_len(newext
),
2009 le32_to_cpu(ex
->ee_block
),
2010 ext4_ext_is_unwritten(ex
),
2011 ext4_ext_get_actual_len(ex
),
2012 ext4_ext_pblock(ex
));
2013 err
= ext4_ext_get_access(handle
, inode
,
2017 unwritten
= ext4_ext_is_unwritten(ex
);
2018 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
2019 + ext4_ext_get_actual_len(newext
));
2021 ext4_ext_mark_unwritten(ex
);
2022 eh
= path
[depth
].p_hdr
;
2028 /* Try to prepend newex to the ex */
2029 if (ext4_can_extents_be_merged(inode
, newext
, ex
)) {
2030 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2032 le32_to_cpu(newext
->ee_block
),
2033 ext4_ext_is_unwritten(newext
),
2034 ext4_ext_get_actual_len(newext
),
2035 le32_to_cpu(ex
->ee_block
),
2036 ext4_ext_is_unwritten(ex
),
2037 ext4_ext_get_actual_len(ex
),
2038 ext4_ext_pblock(ex
));
2039 err
= ext4_ext_get_access(handle
, inode
,
2044 unwritten
= ext4_ext_is_unwritten(ex
);
2045 ex
->ee_block
= newext
->ee_block
;
2046 ext4_ext_store_pblock(ex
, ext4_ext_pblock(newext
));
2047 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
2048 + ext4_ext_get_actual_len(newext
));
2050 ext4_ext_mark_unwritten(ex
);
2051 eh
= path
[depth
].p_hdr
;
2057 depth
= ext_depth(inode
);
2058 eh
= path
[depth
].p_hdr
;
2059 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
2062 /* probably next leaf has space for us? */
2063 fex
= EXT_LAST_EXTENT(eh
);
2064 next
= EXT_MAX_BLOCKS
;
2065 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
))
2066 next
= ext4_ext_next_leaf_block(path
);
2067 if (next
!= EXT_MAX_BLOCKS
) {
2068 ext_debug("next leaf block - %u\n", next
);
2069 BUG_ON(npath
!= NULL
);
2070 npath
= ext4_find_extent(inode
, next
, NULL
, 0);
2072 return PTR_ERR(npath
);
2073 BUG_ON(npath
->p_depth
!= path
->p_depth
);
2074 eh
= npath
[depth
].p_hdr
;
2075 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
2076 ext_debug("next leaf isn't full(%d)\n",
2077 le16_to_cpu(eh
->eh_entries
));
2081 ext_debug("next leaf has no free space(%d,%d)\n",
2082 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
2086 * There is no free space in the found leaf.
2087 * We're gonna add a new leaf in the tree.
2089 if (gb_flags
& EXT4_GET_BLOCKS_METADATA_NOFAIL
)
2090 mb_flags
|= EXT4_MB_USE_RESERVED
;
2091 err
= ext4_ext_create_new_leaf(handle
, inode
, mb_flags
, gb_flags
,
2095 depth
= ext_depth(inode
);
2096 eh
= path
[depth
].p_hdr
;
2099 nearex
= path
[depth
].p_ext
;
2101 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2106 /* there is no extent in this leaf, create first one */
2107 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
2108 le32_to_cpu(newext
->ee_block
),
2109 ext4_ext_pblock(newext
),
2110 ext4_ext_is_unwritten(newext
),
2111 ext4_ext_get_actual_len(newext
));
2112 nearex
= EXT_FIRST_EXTENT(eh
);
2114 if (le32_to_cpu(newext
->ee_block
)
2115 > le32_to_cpu(nearex
->ee_block
)) {
2117 ext_debug("insert %u:%llu:[%d]%d before: "
2119 le32_to_cpu(newext
->ee_block
),
2120 ext4_ext_pblock(newext
),
2121 ext4_ext_is_unwritten(newext
),
2122 ext4_ext_get_actual_len(newext
),
2127 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
2128 ext_debug("insert %u:%llu:[%d]%d after: "
2130 le32_to_cpu(newext
->ee_block
),
2131 ext4_ext_pblock(newext
),
2132 ext4_ext_is_unwritten(newext
),
2133 ext4_ext_get_actual_len(newext
),
2136 len
= EXT_LAST_EXTENT(eh
) - nearex
+ 1;
2138 ext_debug("insert %u:%llu:[%d]%d: "
2139 "move %d extents from 0x%p to 0x%p\n",
2140 le32_to_cpu(newext
->ee_block
),
2141 ext4_ext_pblock(newext
),
2142 ext4_ext_is_unwritten(newext
),
2143 ext4_ext_get_actual_len(newext
),
2144 len
, nearex
, nearex
+ 1);
2145 memmove(nearex
+ 1, nearex
,
2146 len
* sizeof(struct ext4_extent
));
2150 le16_add_cpu(&eh
->eh_entries
, 1);
2151 path
[depth
].p_ext
= nearex
;
2152 nearex
->ee_block
= newext
->ee_block
;
2153 ext4_ext_store_pblock(nearex
, ext4_ext_pblock(newext
));
2154 nearex
->ee_len
= newext
->ee_len
;
2157 /* try to merge extents */
2158 if (!(gb_flags
& EXT4_GET_BLOCKS_PRE_IO
))
2159 ext4_ext_try_to_merge(handle
, inode
, path
, nearex
);
2162 /* time to correct all indexes above */
2163 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2167 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
2170 ext4_ext_drop_refs(npath
);
2175 static int ext4_fill_fiemap_extents(struct inode
*inode
,
2176 ext4_lblk_t block
, ext4_lblk_t num
,
2177 struct fiemap_extent_info
*fieinfo
)
2179 struct ext4_ext_path
*path
= NULL
;
2180 struct ext4_extent
*ex
;
2181 struct extent_status es
;
2182 ext4_lblk_t next
, next_del
, start
= 0, end
= 0;
2183 ext4_lblk_t last
= block
+ num
;
2184 int exists
, depth
= 0, err
= 0;
2185 unsigned int flags
= 0;
2186 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
2188 while (block
< last
&& block
!= EXT_MAX_BLOCKS
) {
2190 /* find extent for this block */
2191 down_read(&EXT4_I(inode
)->i_data_sem
);
2193 path
= ext4_find_extent(inode
, block
, &path
, 0);
2195 up_read(&EXT4_I(inode
)->i_data_sem
);
2196 err
= PTR_ERR(path
);
2201 depth
= ext_depth(inode
);
2202 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2203 up_read(&EXT4_I(inode
)->i_data_sem
);
2204 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2205 err
= -EFSCORRUPTED
;
2208 ex
= path
[depth
].p_ext
;
2209 next
= ext4_ext_next_allocated_block(path
);
2214 /* there is no extent yet, so try to allocate
2215 * all requested space */
2218 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
2219 /* need to allocate space before found extent */
2221 end
= le32_to_cpu(ex
->ee_block
);
2222 if (block
+ num
< end
)
2224 } else if (block
>= le32_to_cpu(ex
->ee_block
)
2225 + ext4_ext_get_actual_len(ex
)) {
2226 /* need to allocate space after found extent */
2231 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
2233 * some part of requested space is covered
2237 end
= le32_to_cpu(ex
->ee_block
)
2238 + ext4_ext_get_actual_len(ex
);
2239 if (block
+ num
< end
)
2245 BUG_ON(end
<= start
);
2249 es
.es_len
= end
- start
;
2252 es
.es_lblk
= le32_to_cpu(ex
->ee_block
);
2253 es
.es_len
= ext4_ext_get_actual_len(ex
);
2254 es
.es_pblk
= ext4_ext_pblock(ex
);
2255 if (ext4_ext_is_unwritten(ex
))
2256 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2260 * Find delayed extent and update es accordingly. We call
2261 * it even in !exists case to find out whether es is the
2262 * last existing extent or not.
2264 next_del
= ext4_find_delayed_extent(inode
, &es
);
2265 if (!exists
&& next_del
) {
2267 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2268 FIEMAP_EXTENT_UNKNOWN
);
2270 up_read(&EXT4_I(inode
)->i_data_sem
);
2272 if (unlikely(es
.es_len
== 0)) {
2273 EXT4_ERROR_INODE(inode
, "es.es_len == 0");
2274 err
= -EFSCORRUPTED
;
2279 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2280 * we need to check next == EXT_MAX_BLOCKS because it is
2281 * possible that an extent is with unwritten and delayed
2282 * status due to when an extent is delayed allocated and
2283 * is allocated by fallocate status tree will track both of
2286 * So we could return a unwritten and delayed extent, and
2287 * its block is equal to 'next'.
2289 if (next
== next_del
&& next
== EXT_MAX_BLOCKS
) {
2290 flags
|= FIEMAP_EXTENT_LAST
;
2291 if (unlikely(next_del
!= EXT_MAX_BLOCKS
||
2292 next
!= EXT_MAX_BLOCKS
)) {
2293 EXT4_ERROR_INODE(inode
,
2294 "next extent == %u, next "
2295 "delalloc extent = %u",
2297 err
= -EFSCORRUPTED
;
2303 err
= fiemap_fill_next_extent(fieinfo
,
2304 (__u64
)es
.es_lblk
<< blksize_bits
,
2305 (__u64
)es
.es_pblk
<< blksize_bits
,
2306 (__u64
)es
.es_len
<< blksize_bits
,
2316 block
= es
.es_lblk
+ es
.es_len
;
2319 ext4_ext_drop_refs(path
);
2324 static int ext4_fill_es_cache_info(struct inode
*inode
,
2325 ext4_lblk_t block
, ext4_lblk_t num
,
2326 struct fiemap_extent_info
*fieinfo
)
2328 ext4_lblk_t next
, end
= block
+ num
- 1;
2329 struct extent_status es
;
2330 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
2334 while (block
<= end
) {
2337 if (!ext4_es_lookup_extent(inode
, block
, &next
, &es
))
2339 if (ext4_es_is_unwritten(&es
))
2340 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2341 if (ext4_es_is_delayed(&es
))
2342 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2343 FIEMAP_EXTENT_UNKNOWN
);
2344 if (ext4_es_is_hole(&es
))
2345 flags
|= EXT4_FIEMAP_EXTENT_HOLE
;
2347 flags
|= FIEMAP_EXTENT_LAST
;
2348 if (flags
& (FIEMAP_EXTENT_DELALLOC
|
2349 EXT4_FIEMAP_EXTENT_HOLE
))
2352 es
.es_pblk
= ext4_es_pblock(&es
);
2353 err
= fiemap_fill_next_extent(fieinfo
,
2354 (__u64
)es
.es_lblk
<< blksize_bits
,
2355 (__u64
)es
.es_pblk
<< blksize_bits
,
2356 (__u64
)es
.es_len
<< blksize_bits
,
2371 * ext4_ext_determine_hole - determine hole around given block
2372 * @inode: inode we lookup in
2373 * @path: path in extent tree to @lblk
2374 * @lblk: pointer to logical block around which we want to determine hole
2376 * Determine hole length (and start if easily possible) around given logical
2377 * block. We don't try too hard to find the beginning of the hole but @path
2378 * actually points to extent before @lblk, we provide it.
2380 * The function returns the length of a hole starting at @lblk. We update @lblk
2381 * to the beginning of the hole if we managed to find it.
2383 static ext4_lblk_t
ext4_ext_determine_hole(struct inode
*inode
,
2384 struct ext4_ext_path
*path
,
2387 int depth
= ext_depth(inode
);
2388 struct ext4_extent
*ex
;
2391 ex
= path
[depth
].p_ext
;
2393 /* there is no extent yet, so gap is [0;-] */
2395 len
= EXT_MAX_BLOCKS
;
2396 } else if (*lblk
< le32_to_cpu(ex
->ee_block
)) {
2397 len
= le32_to_cpu(ex
->ee_block
) - *lblk
;
2398 } else if (*lblk
>= le32_to_cpu(ex
->ee_block
)
2399 + ext4_ext_get_actual_len(ex
)) {
2402 *lblk
= le32_to_cpu(ex
->ee_block
) + ext4_ext_get_actual_len(ex
);
2403 next
= ext4_ext_next_allocated_block(path
);
2404 BUG_ON(next
== *lblk
);
2413 * ext4_ext_put_gap_in_cache:
2414 * calculate boundaries of the gap that the requested block fits into
2415 * and cache this gap
2418 ext4_ext_put_gap_in_cache(struct inode
*inode
, ext4_lblk_t hole_start
,
2419 ext4_lblk_t hole_len
)
2421 struct extent_status es
;
2423 ext4_es_find_extent_range(inode
, &ext4_es_is_delayed
, hole_start
,
2424 hole_start
+ hole_len
- 1, &es
);
2426 /* There's delayed extent containing lblock? */
2427 if (es
.es_lblk
<= hole_start
)
2429 hole_len
= min(es
.es_lblk
- hole_start
, hole_len
);
2431 ext_debug(" -> %u:%u\n", hole_start
, hole_len
);
2432 ext4_es_insert_extent(inode
, hole_start
, hole_len
, ~0,
2433 EXTENT_STATUS_HOLE
);
2438 * removes index from the index block.
2440 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
2441 struct ext4_ext_path
*path
, int depth
)
2446 /* free index block */
2448 path
= path
+ depth
;
2449 leaf
= ext4_idx_pblock(path
->p_idx
);
2450 if (unlikely(path
->p_hdr
->eh_entries
== 0)) {
2451 EXT4_ERROR_INODE(inode
, "path->p_hdr->eh_entries == 0");
2452 return -EFSCORRUPTED
;
2454 err
= ext4_ext_get_access(handle
, inode
, path
);
2458 if (path
->p_idx
!= EXT_LAST_INDEX(path
->p_hdr
)) {
2459 int len
= EXT_LAST_INDEX(path
->p_hdr
) - path
->p_idx
;
2460 len
*= sizeof(struct ext4_extent_idx
);
2461 memmove(path
->p_idx
, path
->p_idx
+ 1, len
);
2464 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
2465 err
= ext4_ext_dirty(handle
, inode
, path
);
2468 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
2469 trace_ext4_ext_rm_idx(inode
, leaf
);
2471 ext4_free_blocks(handle
, inode
, NULL
, leaf
, 1,
2472 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
2474 while (--depth
>= 0) {
2475 if (path
->p_idx
!= EXT_FIRST_INDEX(path
->p_hdr
))
2478 err
= ext4_ext_get_access(handle
, inode
, path
);
2481 path
->p_idx
->ei_block
= (path
+1)->p_idx
->ei_block
;
2482 err
= ext4_ext_dirty(handle
, inode
, path
);
2490 * ext4_ext_calc_credits_for_single_extent:
2491 * This routine returns max. credits that needed to insert an extent
2492 * to the extent tree.
2493 * When pass the actual path, the caller should calculate credits
2496 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2497 struct ext4_ext_path
*path
)
2500 int depth
= ext_depth(inode
);
2503 /* probably there is space in leaf? */
2504 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2505 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2508 * There are some space in the leaf tree, no
2509 * need to account for leaf block credit
2511 * bitmaps and block group descriptor blocks
2512 * and other metadata blocks still need to be
2515 /* 1 bitmap, 1 block group descriptor */
2516 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2521 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2525 * How many index/leaf blocks need to change/allocate to add @extents extents?
2527 * If we add a single extent, then in the worse case, each tree level
2528 * index/leaf need to be changed in case of the tree split.
2530 * If more extents are inserted, they could cause the whole tree split more
2531 * than once, but this is really rare.
2533 int ext4_ext_index_trans_blocks(struct inode
*inode
, int extents
)
2538 /* If we are converting the inline data, only one is needed here. */
2539 if (ext4_has_inline_data(inode
))
2542 depth
= ext_depth(inode
);
2552 static inline int get_default_free_blocks_flags(struct inode
*inode
)
2554 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
) ||
2555 ext4_test_inode_flag(inode
, EXT4_INODE_EA_INODE
))
2556 return EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
;
2557 else if (ext4_should_journal_data(inode
))
2558 return EXT4_FREE_BLOCKS_FORGET
;
2563 * ext4_rereserve_cluster - increment the reserved cluster count when
2564 * freeing a cluster with a pending reservation
2566 * @inode - file containing the cluster
2567 * @lblk - logical block in cluster to be reserved
2569 * Increments the reserved cluster count and adjusts quota in a bigalloc
2570 * file system when freeing a partial cluster containing at least one
2571 * delayed and unwritten block. A partial cluster meeting that
2572 * requirement will have a pending reservation. If so, the
2573 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to
2574 * defer reserved and allocated space accounting to a subsequent call
2577 static void ext4_rereserve_cluster(struct inode
*inode
, ext4_lblk_t lblk
)
2579 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2580 struct ext4_inode_info
*ei
= EXT4_I(inode
);
2582 dquot_reclaim_block(inode
, EXT4_C2B(sbi
, 1));
2584 spin_lock(&ei
->i_block_reservation_lock
);
2585 ei
->i_reserved_data_blocks
++;
2586 percpu_counter_add(&sbi
->s_dirtyclusters_counter
, 1);
2587 spin_unlock(&ei
->i_block_reservation_lock
);
2589 percpu_counter_add(&sbi
->s_freeclusters_counter
, 1);
2590 ext4_remove_pending(inode
, lblk
);
2593 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2594 struct ext4_extent
*ex
,
2595 struct partial_cluster
*partial
,
2596 ext4_lblk_t from
, ext4_lblk_t to
)
2598 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2599 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2600 ext4_fsblk_t last_pblk
, pblk
;
2604 /* only extent tail removal is allowed */
2605 if (from
< le32_to_cpu(ex
->ee_block
) ||
2606 to
!= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2607 ext4_error(sbi
->s_sb
,
2608 "strange request: removal(2) %u-%u from %u:%u",
2609 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2613 #ifdef EXTENTS_STATS
2614 spin_lock(&sbi
->s_ext_stats_lock
);
2615 sbi
->s_ext_blocks
+= ee_len
;
2616 sbi
->s_ext_extents
++;
2617 if (ee_len
< sbi
->s_ext_min
)
2618 sbi
->s_ext_min
= ee_len
;
2619 if (ee_len
> sbi
->s_ext_max
)
2620 sbi
->s_ext_max
= ee_len
;
2621 if (ext_depth(inode
) > sbi
->s_depth_max
)
2622 sbi
->s_depth_max
= ext_depth(inode
);
2623 spin_unlock(&sbi
->s_ext_stats_lock
);
2626 trace_ext4_remove_blocks(inode
, ex
, from
, to
, partial
);
2629 * if we have a partial cluster, and it's different from the
2630 * cluster of the last block in the extent, we free it
2632 last_pblk
= ext4_ext_pblock(ex
) + ee_len
- 1;
2634 if (partial
->state
!= initial
&&
2635 partial
->pclu
!= EXT4_B2C(sbi
, last_pblk
)) {
2636 if (partial
->state
== tofree
) {
2637 flags
= get_default_free_blocks_flags(inode
);
2638 if (ext4_is_pending(inode
, partial
->lblk
))
2639 flags
|= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
;
2640 ext4_free_blocks(handle
, inode
, NULL
,
2641 EXT4_C2B(sbi
, partial
->pclu
),
2642 sbi
->s_cluster_ratio
, flags
);
2643 if (flags
& EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
)
2644 ext4_rereserve_cluster(inode
, partial
->lblk
);
2646 partial
->state
= initial
;
2649 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2650 pblk
= ext4_ext_pblock(ex
) + ee_len
- num
;
2653 * We free the partial cluster at the end of the extent (if any),
2654 * unless the cluster is used by another extent (partial_cluster
2655 * state is nofree). If a partial cluster exists here, it must be
2656 * shared with the last block in the extent.
2658 flags
= get_default_free_blocks_flags(inode
);
2660 /* partial, left end cluster aligned, right end unaligned */
2661 if ((EXT4_LBLK_COFF(sbi
, to
) != sbi
->s_cluster_ratio
- 1) &&
2662 (EXT4_LBLK_CMASK(sbi
, to
) >= from
) &&
2663 (partial
->state
!= nofree
)) {
2664 if (ext4_is_pending(inode
, to
))
2665 flags
|= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
;
2666 ext4_free_blocks(handle
, inode
, NULL
,
2667 EXT4_PBLK_CMASK(sbi
, last_pblk
),
2668 sbi
->s_cluster_ratio
, flags
);
2669 if (flags
& EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
)
2670 ext4_rereserve_cluster(inode
, to
);
2671 partial
->state
= initial
;
2672 flags
= get_default_free_blocks_flags(inode
);
2675 flags
|= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER
;
2678 * For bigalloc file systems, we never free a partial cluster
2679 * at the beginning of the extent. Instead, we check to see if we
2680 * need to free it on a subsequent call to ext4_remove_blocks,
2681 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
2683 flags
|= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
;
2684 ext4_free_blocks(handle
, inode
, NULL
, pblk
, num
, flags
);
2686 /* reset the partial cluster if we've freed past it */
2687 if (partial
->state
!= initial
&& partial
->pclu
!= EXT4_B2C(sbi
, pblk
))
2688 partial
->state
= initial
;
2691 * If we've freed the entire extent but the beginning is not left
2692 * cluster aligned and is not marked as ineligible for freeing we
2693 * record the partial cluster at the beginning of the extent. It
2694 * wasn't freed by the preceding ext4_free_blocks() call, and we
2695 * need to look farther to the left to determine if it's to be freed
2696 * (not shared with another extent). Else, reset the partial
2697 * cluster - we're either done freeing or the beginning of the
2698 * extent is left cluster aligned.
2700 if (EXT4_LBLK_COFF(sbi
, from
) && num
== ee_len
) {
2701 if (partial
->state
== initial
) {
2702 partial
->pclu
= EXT4_B2C(sbi
, pblk
);
2703 partial
->lblk
= from
;
2704 partial
->state
= tofree
;
2707 partial
->state
= initial
;
2714 * ext4_ext_rm_leaf() Removes the extents associated with the
2715 * blocks appearing between "start" and "end". Both "start"
2716 * and "end" must appear in the same extent or EIO is returned.
2718 * @handle: The journal handle
2719 * @inode: The files inode
2720 * @path: The path to the leaf
2721 * @partial_cluster: The cluster which we'll have to free if all extents
2722 * has been released from it. However, if this value is
2723 * negative, it's a cluster just to the right of the
2724 * punched region and it must not be freed.
2725 * @start: The first block to remove
2726 * @end: The last block to remove
2729 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2730 struct ext4_ext_path
*path
,
2731 struct partial_cluster
*partial
,
2732 ext4_lblk_t start
, ext4_lblk_t end
)
2734 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2735 int err
= 0, correct_index
= 0;
2736 int depth
= ext_depth(inode
), credits
, revoke_credits
;
2737 struct ext4_extent_header
*eh
;
2740 ext4_lblk_t ex_ee_block
;
2741 unsigned short ex_ee_len
;
2742 unsigned unwritten
= 0;
2743 struct ext4_extent
*ex
;
2746 /* the header must be checked already in ext4_ext_remove_space() */
2747 ext_debug("truncate since %u in leaf to %u\n", start
, end
);
2748 if (!path
[depth
].p_hdr
)
2749 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2750 eh
= path
[depth
].p_hdr
;
2751 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2752 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2753 return -EFSCORRUPTED
;
2755 /* find where to start removing */
2756 ex
= path
[depth
].p_ext
;
2758 ex
= EXT_LAST_EXTENT(eh
);
2760 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2761 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2763 trace_ext4_ext_rm_leaf(inode
, start
, ex
, partial
);
2765 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2766 ex_ee_block
+ ex_ee_len
> start
) {
2768 if (ext4_ext_is_unwritten(ex
))
2773 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2774 unwritten
, ex_ee_len
);
2775 path
[depth
].p_ext
= ex
;
2777 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2778 b
= ex_ee_block
+ex_ee_len
- 1 < end
?
2779 ex_ee_block
+ex_ee_len
- 1 : end
;
2781 ext_debug(" border %u:%u\n", a
, b
);
2783 /* If this extent is beyond the end of the hole, skip it */
2784 if (end
< ex_ee_block
) {
2786 * We're going to skip this extent and move to another,
2787 * so note that its first cluster is in use to avoid
2788 * freeing it when removing blocks. Eventually, the
2789 * right edge of the truncated/punched region will
2790 * be just to the left.
2792 if (sbi
->s_cluster_ratio
> 1) {
2793 pblk
= ext4_ext_pblock(ex
);
2794 partial
->pclu
= EXT4_B2C(sbi
, pblk
);
2795 partial
->state
= nofree
;
2798 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2799 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2801 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2802 EXT4_ERROR_INODE(inode
,
2803 "can not handle truncate %u:%u "
2805 start
, end
, ex_ee_block
,
2806 ex_ee_block
+ ex_ee_len
- 1);
2807 err
= -EFSCORRUPTED
;
2809 } else if (a
!= ex_ee_block
) {
2810 /* remove tail of the extent */
2811 num
= a
- ex_ee_block
;
2813 /* remove whole extent: excellent! */
2817 * 3 for leaf, sb, and inode plus 2 (bmap and group
2818 * descriptor) for each block group; assume two block
2819 * groups plus ex_ee_len/blocks_per_block_group for
2822 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2823 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2825 credits
+= (ext_depth(inode
)) + 1;
2827 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2829 * We may end up freeing some index blocks and data from the
2830 * punched range. Note that partial clusters are accounted for
2831 * by ext4_free_data_revoke_credits().
2834 ext4_free_metadata_revoke_credits(inode
->i_sb
,
2836 ext4_free_data_revoke_credits(inode
, b
- a
+ 1);
2838 err
= ext4_datasem_ensure_credits(handle
, inode
, credits
,
2839 credits
, revoke_credits
);
2846 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2850 err
= ext4_remove_blocks(handle
, inode
, ex
, partial
, a
, b
);
2855 /* this extent is removed; mark slot entirely unused */
2856 ext4_ext_store_pblock(ex
, 0);
2858 ex
->ee_len
= cpu_to_le16(num
);
2860 * Do not mark unwritten if all the blocks in the
2861 * extent have been removed.
2863 if (unwritten
&& num
)
2864 ext4_ext_mark_unwritten(ex
);
2866 * If the extent was completely released,
2867 * we need to remove it from the leaf
2870 if (end
!= EXT_MAX_BLOCKS
- 1) {
2872 * For hole punching, we need to scoot all the
2873 * extents up when an extent is removed so that
2874 * we dont have blank extents in the middle
2876 memmove(ex
, ex
+1, (EXT_LAST_EXTENT(eh
) - ex
) *
2877 sizeof(struct ext4_extent
));
2879 /* Now get rid of the one at the end */
2880 memset(EXT_LAST_EXTENT(eh
), 0,
2881 sizeof(struct ext4_extent
));
2883 le16_add_cpu(&eh
->eh_entries
, -1);
2886 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2890 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block
, num
,
2891 ext4_ext_pblock(ex
));
2893 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2894 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2897 if (correct_index
&& eh
->eh_entries
)
2898 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2901 * If there's a partial cluster and at least one extent remains in
2902 * the leaf, free the partial cluster if it isn't shared with the
2903 * current extent. If it is shared with the current extent
2904 * we reset the partial cluster because we've reached the start of the
2905 * truncated/punched region and we're done removing blocks.
2907 if (partial
->state
== tofree
&& ex
>= EXT_FIRST_EXTENT(eh
)) {
2908 pblk
= ext4_ext_pblock(ex
) + ex_ee_len
- 1;
2909 if (partial
->pclu
!= EXT4_B2C(sbi
, pblk
)) {
2910 int flags
= get_default_free_blocks_flags(inode
);
2912 if (ext4_is_pending(inode
, partial
->lblk
))
2913 flags
|= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
;
2914 ext4_free_blocks(handle
, inode
, NULL
,
2915 EXT4_C2B(sbi
, partial
->pclu
),
2916 sbi
->s_cluster_ratio
, flags
);
2917 if (flags
& EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
)
2918 ext4_rereserve_cluster(inode
, partial
->lblk
);
2920 partial
->state
= initial
;
2923 /* if this leaf is free, then we should
2924 * remove it from index block above */
2925 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2926 err
= ext4_ext_rm_idx(handle
, inode
, path
, depth
);
2933 * ext4_ext_more_to_rm:
2934 * returns 1 if current index has to be freed (even partial)
2937 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2939 BUG_ON(path
->p_idx
== NULL
);
2941 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2945 * if truncate on deeper level happened, it wasn't partial,
2946 * so we have to consider current index for truncation
2948 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2953 int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
,
2956 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2957 int depth
= ext_depth(inode
);
2958 struct ext4_ext_path
*path
= NULL
;
2959 struct partial_cluster partial
;
2965 partial
.state
= initial
;
2967 ext_debug("truncate since %u to %u\n", start
, end
);
2969 /* probably first extent we're gonna free will be last in block */
2970 handle
= ext4_journal_start_with_revoke(inode
, EXT4_HT_TRUNCATE
,
2972 ext4_free_metadata_revoke_credits(inode
->i_sb
, depth
));
2974 return PTR_ERR(handle
);
2977 trace_ext4_ext_remove_space(inode
, start
, end
, depth
);
2980 * Check if we are removing extents inside the extent tree. If that
2981 * is the case, we are going to punch a hole inside the extent tree
2982 * so we have to check whether we need to split the extent covering
2983 * the last block to remove so we can easily remove the part of it
2984 * in ext4_ext_rm_leaf().
2986 if (end
< EXT_MAX_BLOCKS
- 1) {
2987 struct ext4_extent
*ex
;
2988 ext4_lblk_t ee_block
, ex_end
, lblk
;
2991 /* find extent for or closest extent to this block */
2992 path
= ext4_find_extent(inode
, end
, NULL
, EXT4_EX_NOCACHE
);
2994 ext4_journal_stop(handle
);
2995 return PTR_ERR(path
);
2997 depth
= ext_depth(inode
);
2998 /* Leaf not may not exist only if inode has no blocks at all */
2999 ex
= path
[depth
].p_ext
;
3002 EXT4_ERROR_INODE(inode
,
3003 "path[%d].p_hdr == NULL",
3005 err
= -EFSCORRUPTED
;
3010 ee_block
= le32_to_cpu(ex
->ee_block
);
3011 ex_end
= ee_block
+ ext4_ext_get_actual_len(ex
) - 1;
3014 * See if the last block is inside the extent, if so split
3015 * the extent at 'end' block so we can easily remove the
3016 * tail of the first part of the split extent in
3017 * ext4_ext_rm_leaf().
3019 if (end
>= ee_block
&& end
< ex_end
) {
3022 * If we're going to split the extent, note that
3023 * the cluster containing the block after 'end' is
3024 * in use to avoid freeing it when removing blocks.
3026 if (sbi
->s_cluster_ratio
> 1) {
3027 pblk
= ext4_ext_pblock(ex
) + end
- ee_block
+ 2;
3028 partial
.pclu
= EXT4_B2C(sbi
, pblk
);
3029 partial
.state
= nofree
;
3033 * Split the extent in two so that 'end' is the last
3034 * block in the first new extent. Also we should not
3035 * fail removing space due to ENOSPC so try to use
3036 * reserved block if that happens.
3038 err
= ext4_force_split_extent_at(handle
, inode
, &path
,
3043 } else if (sbi
->s_cluster_ratio
> 1 && end
>= ex_end
&&
3044 partial
.state
== initial
) {
3046 * If we're punching, there's an extent to the right.
3047 * If the partial cluster hasn't been set, set it to
3048 * that extent's first cluster and its state to nofree
3049 * so it won't be freed should it contain blocks to be
3050 * removed. If it's already set (tofree/nofree), we're
3051 * retrying and keep the original partial cluster info
3052 * so a cluster marked tofree as a result of earlier
3053 * extent removal is not lost.
3056 err
= ext4_ext_search_right(inode
, path
, &lblk
, &pblk
,
3061 partial
.pclu
= EXT4_B2C(sbi
, pblk
);
3062 partial
.state
= nofree
;
3067 * We start scanning from right side, freeing all the blocks
3068 * after i_size and walking into the tree depth-wise.
3070 depth
= ext_depth(inode
);
3075 le16_to_cpu(path
[k
].p_hdr
->eh_entries
)+1;
3077 path
= kcalloc(depth
+ 1, sizeof(struct ext4_ext_path
),
3080 ext4_journal_stop(handle
);
3083 path
[0].p_maxdepth
= path
[0].p_depth
= depth
;
3084 path
[0].p_hdr
= ext_inode_hdr(inode
);
3087 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
, 0)) {
3088 err
= -EFSCORRUPTED
;
3094 while (i
>= 0 && err
== 0) {
3096 /* this is leaf block */
3097 err
= ext4_ext_rm_leaf(handle
, inode
, path
,
3098 &partial
, start
, end
);
3099 /* root level has p_bh == NULL, brelse() eats this */
3100 brelse(path
[i
].p_bh
);
3101 path
[i
].p_bh
= NULL
;
3106 /* this is index block */
3107 if (!path
[i
].p_hdr
) {
3108 ext_debug("initialize header\n");
3109 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
3112 if (!path
[i
].p_idx
) {
3113 /* this level hasn't been touched yet */
3114 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
3115 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
3116 ext_debug("init index ptr: hdr 0x%p, num %d\n",
3118 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
3120 /* we were already here, see at next index */
3124 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3125 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
3127 if (ext4_ext_more_to_rm(path
+ i
)) {
3128 struct buffer_head
*bh
;
3129 /* go to the next level */
3130 ext_debug("move to level %d (block %llu)\n",
3131 i
+ 1, ext4_idx_pblock(path
[i
].p_idx
));
3132 memset(path
+ i
+ 1, 0, sizeof(*path
));
3133 bh
= read_extent_tree_block(inode
,
3134 ext4_idx_pblock(path
[i
].p_idx
), depth
- i
- 1,
3137 /* should we reset i_size? */
3141 /* Yield here to deal with large extent trees.
3142 * Should be a no-op if we did IO above. */
3144 if (WARN_ON(i
+ 1 > depth
)) {
3145 err
= -EFSCORRUPTED
;
3148 path
[i
+ 1].p_bh
= bh
;
3150 /* save actual number of indexes since this
3151 * number is changed at the next iteration */
3152 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
3155 /* we finished processing this index, go up */
3156 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
3157 /* index is empty, remove it;
3158 * handle must be already prepared by the
3159 * truncatei_leaf() */
3160 err
= ext4_ext_rm_idx(handle
, inode
, path
, i
);
3162 /* root level has p_bh == NULL, brelse() eats this */
3163 brelse(path
[i
].p_bh
);
3164 path
[i
].p_bh
= NULL
;
3166 ext_debug("return to level %d\n", i
);
3170 trace_ext4_ext_remove_space_done(inode
, start
, end
, depth
, &partial
,
3171 path
->p_hdr
->eh_entries
);
3174 * if there's a partial cluster and we have removed the first extent
3175 * in the file, then we also free the partial cluster, if any
3177 if (partial
.state
== tofree
&& err
== 0) {
3178 int flags
= get_default_free_blocks_flags(inode
);
3180 if (ext4_is_pending(inode
, partial
.lblk
))
3181 flags
|= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
;
3182 ext4_free_blocks(handle
, inode
, NULL
,
3183 EXT4_C2B(sbi
, partial
.pclu
),
3184 sbi
->s_cluster_ratio
, flags
);
3185 if (flags
& EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
)
3186 ext4_rereserve_cluster(inode
, partial
.lblk
);
3187 partial
.state
= initial
;
3190 /* TODO: flexible tree reduction should be here */
3191 if (path
->p_hdr
->eh_entries
== 0) {
3193 * truncate to zero freed all the tree,
3194 * so we need to correct eh_depth
3196 err
= ext4_ext_get_access(handle
, inode
, path
);
3198 ext_inode_hdr(inode
)->eh_depth
= 0;
3199 ext_inode_hdr(inode
)->eh_max
=
3200 cpu_to_le16(ext4_ext_space_root(inode
, 0));
3201 err
= ext4_ext_dirty(handle
, inode
, path
);
3205 ext4_ext_drop_refs(path
);
3210 ext4_journal_stop(handle
);
3216 * called at mount time
3218 void ext4_ext_init(struct super_block
*sb
)
3221 * possible initialization would be here
3224 if (ext4_has_feature_extents(sb
)) {
3225 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
3226 printk(KERN_INFO
"EXT4-fs: file extents enabled"
3227 #ifdef AGGRESSIVE_TEST
3228 ", aggressive tests"
3230 #ifdef CHECK_BINSEARCH
3233 #ifdef EXTENTS_STATS
3238 #ifdef EXTENTS_STATS
3239 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
3240 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
3241 EXT4_SB(sb
)->s_ext_max
= 0;
3247 * called at umount time
3249 void ext4_ext_release(struct super_block
*sb
)
3251 if (!ext4_has_feature_extents(sb
))
3254 #ifdef EXTENTS_STATS
3255 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
3256 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3257 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3258 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
3259 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
3260 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3261 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
3266 static int ext4_zeroout_es(struct inode
*inode
, struct ext4_extent
*ex
)
3268 ext4_lblk_t ee_block
;
3269 ext4_fsblk_t ee_pblock
;
3270 unsigned int ee_len
;
3272 ee_block
= le32_to_cpu(ex
->ee_block
);
3273 ee_len
= ext4_ext_get_actual_len(ex
);
3274 ee_pblock
= ext4_ext_pblock(ex
);
3279 return ext4_es_insert_extent(inode
, ee_block
, ee_len
, ee_pblock
,
3280 EXTENT_STATUS_WRITTEN
);
3283 /* FIXME!! we need to try to merge to left or right after zero-out */
3284 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
3286 ext4_fsblk_t ee_pblock
;
3287 unsigned int ee_len
;
3289 ee_len
= ext4_ext_get_actual_len(ex
);
3290 ee_pblock
= ext4_ext_pblock(ex
);
3291 return ext4_issue_zeroout(inode
, le32_to_cpu(ex
->ee_block
), ee_pblock
,
3296 * ext4_split_extent_at() splits an extent at given block.
3298 * @handle: the journal handle
3299 * @inode: the file inode
3300 * @path: the path to the extent
3301 * @split: the logical block where the extent is splitted.
3302 * @split_flags: indicates if the extent could be zeroout if split fails, and
3303 * the states(init or unwritten) of new extents.
3304 * @flags: flags used to insert new extent to extent tree.
3307 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3308 * of which are deterimined by split_flag.
3310 * There are two cases:
3311 * a> the extent are splitted into two extent.
3312 * b> split is not needed, and just mark the extent.
3314 * return 0 on success.
3316 static int ext4_split_extent_at(handle_t
*handle
,
3317 struct inode
*inode
,
3318 struct ext4_ext_path
**ppath
,
3323 struct ext4_ext_path
*path
= *ppath
;
3324 ext4_fsblk_t newblock
;
3325 ext4_lblk_t ee_block
;
3326 struct ext4_extent
*ex
, newex
, orig_ex
, zero_ex
;
3327 struct ext4_extent
*ex2
= NULL
;
3328 unsigned int ee_len
, depth
;
3331 BUG_ON((split_flag
& (EXT4_EXT_DATA_VALID1
| EXT4_EXT_DATA_VALID2
)) ==
3332 (EXT4_EXT_DATA_VALID1
| EXT4_EXT_DATA_VALID2
));
3334 ext_debug("ext4_split_extents_at: inode %lu, logical"
3335 "block %llu\n", inode
->i_ino
, (unsigned long long)split
);
3337 ext4_ext_show_leaf(inode
, path
);
3339 depth
= ext_depth(inode
);
3340 ex
= path
[depth
].p_ext
;
3341 ee_block
= le32_to_cpu(ex
->ee_block
);
3342 ee_len
= ext4_ext_get_actual_len(ex
);
3343 newblock
= split
- ee_block
+ ext4_ext_pblock(ex
);
3345 BUG_ON(split
< ee_block
|| split
>= (ee_block
+ ee_len
));
3346 BUG_ON(!ext4_ext_is_unwritten(ex
) &&
3347 split_flag
& (EXT4_EXT_MAY_ZEROOUT
|
3348 EXT4_EXT_MARK_UNWRIT1
|
3349 EXT4_EXT_MARK_UNWRIT2
));
3351 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3355 if (split
== ee_block
) {
3357 * case b: block @split is the block that the extent begins with
3358 * then we just change the state of the extent, and splitting
3361 if (split_flag
& EXT4_EXT_MARK_UNWRIT2
)
3362 ext4_ext_mark_unwritten(ex
);
3364 ext4_ext_mark_initialized(ex
);
3366 if (!(flags
& EXT4_GET_BLOCKS_PRE_IO
))
3367 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3369 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3374 memcpy(&orig_ex
, ex
, sizeof(orig_ex
));
3375 ex
->ee_len
= cpu_to_le16(split
- ee_block
);
3376 if (split_flag
& EXT4_EXT_MARK_UNWRIT1
)
3377 ext4_ext_mark_unwritten(ex
);
3380 * path may lead to new leaf, not to original leaf any more
3381 * after ext4_ext_insert_extent() returns,
3383 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3385 goto fix_extent_len
;
3388 ex2
->ee_block
= cpu_to_le32(split
);
3389 ex2
->ee_len
= cpu_to_le16(ee_len
- (split
- ee_block
));
3390 ext4_ext_store_pblock(ex2
, newblock
);
3391 if (split_flag
& EXT4_EXT_MARK_UNWRIT2
)
3392 ext4_ext_mark_unwritten(ex2
);
3394 err
= ext4_ext_insert_extent(handle
, inode
, ppath
, &newex
, flags
);
3395 if (err
== -ENOSPC
&& (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
3396 if (split_flag
& (EXT4_EXT_DATA_VALID1
|EXT4_EXT_DATA_VALID2
)) {
3397 if (split_flag
& EXT4_EXT_DATA_VALID1
) {
3398 err
= ext4_ext_zeroout(inode
, ex2
);
3399 zero_ex
.ee_block
= ex2
->ee_block
;
3400 zero_ex
.ee_len
= cpu_to_le16(
3401 ext4_ext_get_actual_len(ex2
));
3402 ext4_ext_store_pblock(&zero_ex
,
3403 ext4_ext_pblock(ex2
));
3405 err
= ext4_ext_zeroout(inode
, ex
);
3406 zero_ex
.ee_block
= ex
->ee_block
;
3407 zero_ex
.ee_len
= cpu_to_le16(
3408 ext4_ext_get_actual_len(ex
));
3409 ext4_ext_store_pblock(&zero_ex
,
3410 ext4_ext_pblock(ex
));
3413 err
= ext4_ext_zeroout(inode
, &orig_ex
);
3414 zero_ex
.ee_block
= orig_ex
.ee_block
;
3415 zero_ex
.ee_len
= cpu_to_le16(
3416 ext4_ext_get_actual_len(&orig_ex
));
3417 ext4_ext_store_pblock(&zero_ex
,
3418 ext4_ext_pblock(&orig_ex
));
3422 goto fix_extent_len
;
3423 /* update the extent length and mark as initialized */
3424 ex
->ee_len
= cpu_to_le16(ee_len
);
3425 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3426 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3428 goto fix_extent_len
;
3430 /* update extent status tree */
3431 err
= ext4_zeroout_es(inode
, &zero_ex
);
3435 goto fix_extent_len
;
3438 ext4_ext_show_leaf(inode
, path
);
3442 ex
->ee_len
= orig_ex
.ee_len
;
3443 ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3448 * ext4_split_extents() splits an extent and mark extent which is covered
3449 * by @map as split_flags indicates
3451 * It may result in splitting the extent into multiple extents (up to three)
3452 * There are three possibilities:
3453 * a> There is no split required
3454 * b> Splits in two extents: Split is happening at either end of the extent
3455 * c> Splits in three extents: Somone is splitting in middle of the extent
3458 static int ext4_split_extent(handle_t
*handle
,
3459 struct inode
*inode
,
3460 struct ext4_ext_path
**ppath
,
3461 struct ext4_map_blocks
*map
,
3465 struct ext4_ext_path
*path
= *ppath
;
3466 ext4_lblk_t ee_block
;
3467 struct ext4_extent
*ex
;
3468 unsigned int ee_len
, depth
;
3471 int split_flag1
, flags1
;
3472 int allocated
= map
->m_len
;
3474 depth
= ext_depth(inode
);
3475 ex
= path
[depth
].p_ext
;
3476 ee_block
= le32_to_cpu(ex
->ee_block
);
3477 ee_len
= ext4_ext_get_actual_len(ex
);
3478 unwritten
= ext4_ext_is_unwritten(ex
);
3480 if (map
->m_lblk
+ map
->m_len
< ee_block
+ ee_len
) {
3481 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
;
3482 flags1
= flags
| EXT4_GET_BLOCKS_PRE_IO
;
3484 split_flag1
|= EXT4_EXT_MARK_UNWRIT1
|
3485 EXT4_EXT_MARK_UNWRIT2
;
3486 if (split_flag
& EXT4_EXT_DATA_VALID2
)
3487 split_flag1
|= EXT4_EXT_DATA_VALID1
;
3488 err
= ext4_split_extent_at(handle
, inode
, ppath
,
3489 map
->m_lblk
+ map
->m_len
, split_flag1
, flags1
);
3493 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3496 * Update path is required because previous ext4_split_extent_at() may
3497 * result in split of original leaf or extent zeroout.
3499 path
= ext4_find_extent(inode
, map
->m_lblk
, ppath
, 0);
3501 return PTR_ERR(path
);
3502 depth
= ext_depth(inode
);
3503 ex
= path
[depth
].p_ext
;
3505 EXT4_ERROR_INODE(inode
, "unexpected hole at %lu",
3506 (unsigned long) map
->m_lblk
);
3507 return -EFSCORRUPTED
;
3509 unwritten
= ext4_ext_is_unwritten(ex
);
3512 if (map
->m_lblk
>= ee_block
) {
3513 split_flag1
= split_flag
& EXT4_EXT_DATA_VALID2
;
3515 split_flag1
|= EXT4_EXT_MARK_UNWRIT1
;
3516 split_flag1
|= split_flag
& (EXT4_EXT_MAY_ZEROOUT
|
3517 EXT4_EXT_MARK_UNWRIT2
);
3519 err
= ext4_split_extent_at(handle
, inode
, ppath
,
3520 map
->m_lblk
, split_flag1
, flags
);
3525 ext4_ext_show_leaf(inode
, path
);
3527 return err
? err
: allocated
;
3531 * This function is called by ext4_ext_map_blocks() if someone tries to write
3532 * to an unwritten extent. It may result in splitting the unwritten
3533 * extent into multiple extents (up to three - one initialized and two
3535 * There are three possibilities:
3536 * a> There is no split required: Entire extent should be initialized
3537 * b> Splits in two extents: Write is happening at either end of the extent
3538 * c> Splits in three extents: Somone is writing in middle of the extent
3541 * - The extent pointed to by 'path' is unwritten.
3542 * - The extent pointed to by 'path' contains a superset
3543 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3545 * Post-conditions on success:
3546 * - the returned value is the number of blocks beyond map->l_lblk
3547 * that are allocated and initialized.
3548 * It is guaranteed to be >= map->m_len.
3550 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
3551 struct inode
*inode
,
3552 struct ext4_map_blocks
*map
,
3553 struct ext4_ext_path
**ppath
,
3556 struct ext4_ext_path
*path
= *ppath
;
3557 struct ext4_sb_info
*sbi
;
3558 struct ext4_extent_header
*eh
;
3559 struct ext4_map_blocks split_map
;
3560 struct ext4_extent zero_ex1
, zero_ex2
;
3561 struct ext4_extent
*ex
, *abut_ex
;
3562 ext4_lblk_t ee_block
, eof_block
;
3563 unsigned int ee_len
, depth
, map_len
= map
->m_len
;
3564 int allocated
= 0, max_zeroout
= 0;
3566 int split_flag
= EXT4_EXT_DATA_VALID2
;
3568 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3569 "block %llu, max_blocks %u\n", inode
->i_ino
,
3570 (unsigned long long)map
->m_lblk
, map_len
);
3572 sbi
= EXT4_SB(inode
->i_sb
);
3573 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
3574 inode
->i_sb
->s_blocksize_bits
;
3575 if (eof_block
< map
->m_lblk
+ map_len
)
3576 eof_block
= map
->m_lblk
+ map_len
;
3578 depth
= ext_depth(inode
);
3579 eh
= path
[depth
].p_hdr
;
3580 ex
= path
[depth
].p_ext
;
3581 ee_block
= le32_to_cpu(ex
->ee_block
);
3582 ee_len
= ext4_ext_get_actual_len(ex
);
3583 zero_ex1
.ee_len
= 0;
3584 zero_ex2
.ee_len
= 0;
3586 trace_ext4_ext_convert_to_initialized_enter(inode
, map
, ex
);
3588 /* Pre-conditions */
3589 BUG_ON(!ext4_ext_is_unwritten(ex
));
3590 BUG_ON(!in_range(map
->m_lblk
, ee_block
, ee_len
));
3593 * Attempt to transfer newly initialized blocks from the currently
3594 * unwritten extent to its neighbor. This is much cheaper
3595 * than an insertion followed by a merge as those involve costly
3596 * memmove() calls. Transferring to the left is the common case in
3597 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3598 * followed by append writes.
3600 * Limitations of the current logic:
3601 * - L1: we do not deal with writes covering the whole extent.
3602 * This would require removing the extent if the transfer
3604 * - L2: we only attempt to merge with an extent stored in the
3605 * same extent tree node.
3607 if ((map
->m_lblk
== ee_block
) &&
3608 /* See if we can merge left */
3609 (map_len
< ee_len
) && /*L1*/
3610 (ex
> EXT_FIRST_EXTENT(eh
))) { /*L2*/
3611 ext4_lblk_t prev_lblk
;
3612 ext4_fsblk_t prev_pblk
, ee_pblk
;
3613 unsigned int prev_len
;
3616 prev_lblk
= le32_to_cpu(abut_ex
->ee_block
);
3617 prev_len
= ext4_ext_get_actual_len(abut_ex
);
3618 prev_pblk
= ext4_ext_pblock(abut_ex
);
3619 ee_pblk
= ext4_ext_pblock(ex
);
3622 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3623 * upon those conditions:
3624 * - C1: abut_ex is initialized,
3625 * - C2: abut_ex is logically abutting ex,
3626 * - C3: abut_ex is physically abutting ex,
3627 * - C4: abut_ex can receive the additional blocks without
3628 * overflowing the (initialized) length limit.
3630 if ((!ext4_ext_is_unwritten(abut_ex
)) && /*C1*/
3631 ((prev_lblk
+ prev_len
) == ee_block
) && /*C2*/
3632 ((prev_pblk
+ prev_len
) == ee_pblk
) && /*C3*/
3633 (prev_len
< (EXT_INIT_MAX_LEN
- map_len
))) { /*C4*/
3634 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3638 trace_ext4_ext_convert_to_initialized_fastpath(inode
,
3641 /* Shift the start of ex by 'map_len' blocks */
3642 ex
->ee_block
= cpu_to_le32(ee_block
+ map_len
);
3643 ext4_ext_store_pblock(ex
, ee_pblk
+ map_len
);
3644 ex
->ee_len
= cpu_to_le16(ee_len
- map_len
);
3645 ext4_ext_mark_unwritten(ex
); /* Restore the flag */
3647 /* Extend abut_ex by 'map_len' blocks */
3648 abut_ex
->ee_len
= cpu_to_le16(prev_len
+ map_len
);
3650 /* Result: number of initialized blocks past m_lblk */
3651 allocated
= map_len
;
3653 } else if (((map
->m_lblk
+ map_len
) == (ee_block
+ ee_len
)) &&
3654 (map_len
< ee_len
) && /*L1*/
3655 ex
< EXT_LAST_EXTENT(eh
)) { /*L2*/
3656 /* See if we can merge right */
3657 ext4_lblk_t next_lblk
;
3658 ext4_fsblk_t next_pblk
, ee_pblk
;
3659 unsigned int next_len
;
3662 next_lblk
= le32_to_cpu(abut_ex
->ee_block
);
3663 next_len
= ext4_ext_get_actual_len(abut_ex
);
3664 next_pblk
= ext4_ext_pblock(abut_ex
);
3665 ee_pblk
= ext4_ext_pblock(ex
);
3668 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3669 * upon those conditions:
3670 * - C1: abut_ex is initialized,
3671 * - C2: abut_ex is logically abutting ex,
3672 * - C3: abut_ex is physically abutting ex,
3673 * - C4: abut_ex can receive the additional blocks without
3674 * overflowing the (initialized) length limit.
3676 if ((!ext4_ext_is_unwritten(abut_ex
)) && /*C1*/
3677 ((map
->m_lblk
+ map_len
) == next_lblk
) && /*C2*/
3678 ((ee_pblk
+ ee_len
) == next_pblk
) && /*C3*/
3679 (next_len
< (EXT_INIT_MAX_LEN
- map_len
))) { /*C4*/
3680 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3684 trace_ext4_ext_convert_to_initialized_fastpath(inode
,
3687 /* Shift the start of abut_ex by 'map_len' blocks */
3688 abut_ex
->ee_block
= cpu_to_le32(next_lblk
- map_len
);
3689 ext4_ext_store_pblock(abut_ex
, next_pblk
- map_len
);
3690 ex
->ee_len
= cpu_to_le16(ee_len
- map_len
);
3691 ext4_ext_mark_unwritten(ex
); /* Restore the flag */
3693 /* Extend abut_ex by 'map_len' blocks */
3694 abut_ex
->ee_len
= cpu_to_le16(next_len
+ map_len
);
3696 /* Result: number of initialized blocks past m_lblk */
3697 allocated
= map_len
;
3701 /* Mark the block containing both extents as dirty */
3702 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3704 /* Update path to point to the right extent */
3705 path
[depth
].p_ext
= abut_ex
;
3708 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3710 WARN_ON(map
->m_lblk
< ee_block
);
3712 * It is safe to convert extent to initialized via explicit
3713 * zeroout only if extent is fully inside i_size or new_size.
3715 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
3717 if (EXT4_EXT_MAY_ZEROOUT
& split_flag
)
3718 max_zeroout
= sbi
->s_extent_max_zeroout_kb
>>
3719 (inode
->i_sb
->s_blocksize_bits
- 10);
3721 if (IS_ENCRYPTED(inode
))
3726 * 1. split the extent into three extents.
3727 * 2. split the extent into two extents, zeroout the head of the first
3729 * 3. split the extent into two extents, zeroout the tail of the second
3731 * 4. split the extent into two extents with out zeroout.
3732 * 5. no splitting needed, just possibly zeroout the head and / or the
3733 * tail of the extent.
3735 split_map
.m_lblk
= map
->m_lblk
;
3736 split_map
.m_len
= map
->m_len
;
3738 if (max_zeroout
&& (allocated
> split_map
.m_len
)) {
3739 if (allocated
<= max_zeroout
) {
3742 cpu_to_le32(split_map
.m_lblk
+
3745 cpu_to_le16(allocated
- split_map
.m_len
);
3746 ext4_ext_store_pblock(&zero_ex1
,
3747 ext4_ext_pblock(ex
) + split_map
.m_lblk
+
3748 split_map
.m_len
- ee_block
);
3749 err
= ext4_ext_zeroout(inode
, &zero_ex1
);
3752 split_map
.m_len
= allocated
;
3754 if (split_map
.m_lblk
- ee_block
+ split_map
.m_len
<
3757 if (split_map
.m_lblk
!= ee_block
) {
3758 zero_ex2
.ee_block
= ex
->ee_block
;
3759 zero_ex2
.ee_len
= cpu_to_le16(split_map
.m_lblk
-
3761 ext4_ext_store_pblock(&zero_ex2
,
3762 ext4_ext_pblock(ex
));
3763 err
= ext4_ext_zeroout(inode
, &zero_ex2
);
3768 split_map
.m_len
+= split_map
.m_lblk
- ee_block
;
3769 split_map
.m_lblk
= ee_block
;
3770 allocated
= map
->m_len
;
3774 err
= ext4_split_extent(handle
, inode
, ppath
, &split_map
, split_flag
,
3779 /* If we have gotten a failure, don't zero out status tree */
3781 err
= ext4_zeroout_es(inode
, &zero_ex1
);
3783 err
= ext4_zeroout_es(inode
, &zero_ex2
);
3785 return err
? err
: allocated
;
3789 * This function is called by ext4_ext_map_blocks() from
3790 * ext4_get_blocks_dio_write() when DIO to write
3791 * to an unwritten extent.
3793 * Writing to an unwritten extent may result in splitting the unwritten
3794 * extent into multiple initialized/unwritten extents (up to three)
3795 * There are three possibilities:
3796 * a> There is no split required: Entire extent should be unwritten
3797 * b> Splits in two extents: Write is happening at either end of the extent
3798 * c> Splits in three extents: Somone is writing in middle of the extent
3800 * This works the same way in the case of initialized -> unwritten conversion.
3802 * One of more index blocks maybe needed if the extent tree grow after
3803 * the unwritten extent split. To prevent ENOSPC occur at the IO
3804 * complete, we need to split the unwritten extent before DIO submit
3805 * the IO. The unwritten extent called at this time will be split
3806 * into three unwritten extent(at most). After IO complete, the part
3807 * being filled will be convert to initialized by the end_io callback function
3808 * via ext4_convert_unwritten_extents().
3810 * Returns the size of unwritten extent to be written on success.
3812 static int ext4_split_convert_extents(handle_t
*handle
,
3813 struct inode
*inode
,
3814 struct ext4_map_blocks
*map
,
3815 struct ext4_ext_path
**ppath
,
3818 struct ext4_ext_path
*path
= *ppath
;
3819 ext4_lblk_t eof_block
;
3820 ext4_lblk_t ee_block
;
3821 struct ext4_extent
*ex
;
3822 unsigned int ee_len
;
3823 int split_flag
= 0, depth
;
3825 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3826 __func__
, inode
->i_ino
,
3827 (unsigned long long)map
->m_lblk
, map
->m_len
);
3829 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
3830 inode
->i_sb
->s_blocksize_bits
;
3831 if (eof_block
< map
->m_lblk
+ map
->m_len
)
3832 eof_block
= map
->m_lblk
+ map
->m_len
;
3834 * It is safe to convert extent to initialized via explicit
3835 * zeroout only if extent is fully insde i_size or new_size.
3837 depth
= ext_depth(inode
);
3838 ex
= path
[depth
].p_ext
;
3839 ee_block
= le32_to_cpu(ex
->ee_block
);
3840 ee_len
= ext4_ext_get_actual_len(ex
);
3842 /* Convert to unwritten */
3843 if (flags
& EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
) {
3844 split_flag
|= EXT4_EXT_DATA_VALID1
;
3845 /* Convert to initialized */
3846 } else if (flags
& EXT4_GET_BLOCKS_CONVERT
) {
3847 split_flag
|= ee_block
+ ee_len
<= eof_block
?
3848 EXT4_EXT_MAY_ZEROOUT
: 0;
3849 split_flag
|= (EXT4_EXT_MARK_UNWRIT2
| EXT4_EXT_DATA_VALID2
);
3851 flags
|= EXT4_GET_BLOCKS_PRE_IO
;
3852 return ext4_split_extent(handle
, inode
, ppath
, map
, split_flag
, flags
);
3855 static int ext4_convert_unwritten_extents_endio(handle_t
*handle
,
3856 struct inode
*inode
,
3857 struct ext4_map_blocks
*map
,
3858 struct ext4_ext_path
**ppath
)
3860 struct ext4_ext_path
*path
= *ppath
;
3861 struct ext4_extent
*ex
;
3862 ext4_lblk_t ee_block
;
3863 unsigned int ee_len
;
3867 depth
= ext_depth(inode
);
3868 ex
= path
[depth
].p_ext
;
3869 ee_block
= le32_to_cpu(ex
->ee_block
);
3870 ee_len
= ext4_ext_get_actual_len(ex
);
3872 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3873 "block %llu, max_blocks %u\n", inode
->i_ino
,
3874 (unsigned long long)ee_block
, ee_len
);
3876 /* If extent is larger than requested it is a clear sign that we still
3877 * have some extent state machine issues left. So extent_split is still
3879 * TODO: Once all related issues will be fixed this situation should be
3882 if (ee_block
!= map
->m_lblk
|| ee_len
> map
->m_len
) {
3883 #ifdef CONFIG_EXT4_DEBUG
3884 ext4_warning(inode
->i_sb
, "Inode (%ld) finished: extent logical block %llu,"
3885 " len %u; IO logical block %llu, len %u",
3886 inode
->i_ino
, (unsigned long long)ee_block
, ee_len
,
3887 (unsigned long long)map
->m_lblk
, map
->m_len
);
3889 err
= ext4_split_convert_extents(handle
, inode
, map
, ppath
,
3890 EXT4_GET_BLOCKS_CONVERT
);
3893 path
= ext4_find_extent(inode
, map
->m_lblk
, ppath
, 0);
3895 return PTR_ERR(path
);
3896 depth
= ext_depth(inode
);
3897 ex
= path
[depth
].p_ext
;
3900 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3903 /* first mark the extent as initialized */
3904 ext4_ext_mark_initialized(ex
);
3906 /* note: ext4_ext_correct_indexes() isn't needed here because
3907 * borders are not changed
3909 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
3911 /* Mark modified extent as dirty */
3912 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
3914 ext4_ext_show_leaf(inode
, path
);
3919 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3921 static int check_eofblocks_fl(handle_t
*handle
, struct inode
*inode
,
3923 struct ext4_ext_path
*path
,
3927 struct ext4_extent_header
*eh
;
3928 struct ext4_extent
*last_ex
;
3930 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))
3933 depth
= ext_depth(inode
);
3934 eh
= path
[depth
].p_hdr
;
3937 * We're going to remove EOFBLOCKS_FL entirely in future so we
3938 * do not care for this case anymore. Simply remove the flag
3939 * if there are no extents.
3941 if (unlikely(!eh
->eh_entries
))
3943 last_ex
= EXT_LAST_EXTENT(eh
);
3945 * We should clear the EOFBLOCKS_FL flag if we are writing the
3946 * last block in the last extent in the file. We test this by
3947 * first checking to see if the caller to
3948 * ext4_ext_get_blocks() was interested in the last block (or
3949 * a block beyond the last block) in the current extent. If
3950 * this turns out to be false, we can bail out from this
3951 * function immediately.
3953 if (lblk
+ len
< le32_to_cpu(last_ex
->ee_block
) +
3954 ext4_ext_get_actual_len(last_ex
))
3957 * If the caller does appear to be planning to write at or
3958 * beyond the end of the current extent, we then test to see
3959 * if the current extent is the last extent in the file, by
3960 * checking to make sure it was reached via the rightmost node
3961 * at each level of the tree.
3963 for (i
= depth
-1; i
>= 0; i
--)
3964 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3967 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3968 return ext4_mark_inode_dirty(handle
, inode
);
3972 convert_initialized_extent(handle_t
*handle
, struct inode
*inode
,
3973 struct ext4_map_blocks
*map
,
3974 struct ext4_ext_path
**ppath
,
3975 unsigned int allocated
)
3977 struct ext4_ext_path
*path
= *ppath
;
3978 struct ext4_extent
*ex
;
3979 ext4_lblk_t ee_block
;
3980 unsigned int ee_len
;
3985 * Make sure that the extent is no bigger than we support with
3988 if (map
->m_len
> EXT_UNWRITTEN_MAX_LEN
)
3989 map
->m_len
= EXT_UNWRITTEN_MAX_LEN
/ 2;
3991 depth
= ext_depth(inode
);
3992 ex
= path
[depth
].p_ext
;
3993 ee_block
= le32_to_cpu(ex
->ee_block
);
3994 ee_len
= ext4_ext_get_actual_len(ex
);
3996 ext_debug("%s: inode %lu, logical"
3997 "block %llu, max_blocks %u\n", __func__
, inode
->i_ino
,
3998 (unsigned long long)ee_block
, ee_len
);
4000 if (ee_block
!= map
->m_lblk
|| ee_len
> map
->m_len
) {
4001 err
= ext4_split_convert_extents(handle
, inode
, map
, ppath
,
4002 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
);
4005 path
= ext4_find_extent(inode
, map
->m_lblk
, ppath
, 0);
4007 return PTR_ERR(path
);
4008 depth
= ext_depth(inode
);
4009 ex
= path
[depth
].p_ext
;
4011 EXT4_ERROR_INODE(inode
, "unexpected hole at %lu",
4012 (unsigned long) map
->m_lblk
);
4013 return -EFSCORRUPTED
;
4017 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
4020 /* first mark the extent as unwritten */
4021 ext4_ext_mark_unwritten(ex
);
4023 /* note: ext4_ext_correct_indexes() isn't needed here because
4024 * borders are not changed
4026 ext4_ext_try_to_merge(handle
, inode
, path
, ex
);
4028 /* Mark modified extent as dirty */
4029 err
= ext4_ext_dirty(handle
, inode
, path
+ path
->p_depth
);
4032 ext4_ext_show_leaf(inode
, path
);
4034 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4035 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
, map
->m_len
);
4038 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4039 if (allocated
> map
->m_len
)
4040 allocated
= map
->m_len
;
4041 map
->m_len
= allocated
;
4046 ext4_ext_handle_unwritten_extents(handle_t
*handle
, struct inode
*inode
,
4047 struct ext4_map_blocks
*map
,
4048 struct ext4_ext_path
**ppath
, int flags
,
4049 unsigned int allocated
, ext4_fsblk_t newblock
)
4051 struct ext4_ext_path
*path
= *ppath
;
4055 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
4056 "block %llu, max_blocks %u, flags %x, allocated %u\n",
4057 inode
->i_ino
, (unsigned long long)map
->m_lblk
, map
->m_len
,
4059 ext4_ext_show_leaf(inode
, path
);
4062 * When writing into unwritten space, we should not fail to
4063 * allocate metadata blocks for the new extent block if needed.
4065 flags
|= EXT4_GET_BLOCKS_METADATA_NOFAIL
;
4067 trace_ext4_ext_handle_unwritten_extents(inode
, map
, flags
,
4068 allocated
, newblock
);
4070 /* get_block() before submit the IO, split the extent */
4071 if (flags
& EXT4_GET_BLOCKS_PRE_IO
) {
4072 ret
= ext4_split_convert_extents(handle
, inode
, map
, ppath
,
4073 flags
| EXT4_GET_BLOCKS_CONVERT
);
4076 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4079 /* IO end_io complete, convert the filled extent to written */
4080 if (flags
& EXT4_GET_BLOCKS_CONVERT
) {
4081 if (flags
& EXT4_GET_BLOCKS_ZERO
) {
4082 if (allocated
> map
->m_len
)
4083 allocated
= map
->m_len
;
4084 err
= ext4_issue_zeroout(inode
, map
->m_lblk
, newblock
,
4089 ret
= ext4_convert_unwritten_extents_endio(handle
, inode
, map
,
4092 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4093 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
4097 map
->m_flags
|= EXT4_MAP_MAPPED
;
4098 map
->m_pblk
= newblock
;
4099 if (allocated
> map
->m_len
)
4100 allocated
= map
->m_len
;
4101 map
->m_len
= allocated
;
4104 /* buffered IO case */
4106 * repeat fallocate creation request
4107 * we already have an unwritten extent
4109 if (flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
) {
4110 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4114 /* buffered READ or buffered write_begin() lookup */
4115 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
4117 * We have blocks reserved already. We
4118 * return allocated blocks so that delalloc
4119 * won't do block reservation for us. But
4120 * the buffer head will be unmapped so that
4121 * a read from the block returns 0s.
4123 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4127 /* buffered write, writepage time, convert*/
4128 ret
= ext4_ext_convert_to_initialized(handle
, inode
, map
, ppath
, flags
);
4130 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4137 map
->m_flags
|= EXT4_MAP_NEW
;
4138 if (allocated
> map
->m_len
)
4139 allocated
= map
->m_len
;
4140 map
->m_len
= allocated
;
4143 map
->m_flags
|= EXT4_MAP_MAPPED
;
4144 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0) {
4145 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
,
4151 if (allocated
> map
->m_len
)
4152 allocated
= map
->m_len
;
4153 ext4_ext_show_leaf(inode
, path
);
4154 map
->m_pblk
= newblock
;
4155 map
->m_len
= allocated
;
4157 return err
? err
: allocated
;
4161 * get_implied_cluster_alloc - check to see if the requested
4162 * allocation (in the map structure) overlaps with a cluster already
4163 * allocated in an extent.
4164 * @sb The filesystem superblock structure
4165 * @map The requested lblk->pblk mapping
4166 * @ex The extent structure which might contain an implied
4167 * cluster allocation
4169 * This function is called by ext4_ext_map_blocks() after we failed to
4170 * find blocks that were already in the inode's extent tree. Hence,
4171 * we know that the beginning of the requested region cannot overlap
4172 * the extent from the inode's extent tree. There are three cases we
4173 * want to catch. The first is this case:
4175 * |--- cluster # N--|
4176 * |--- extent ---| |---- requested region ---|
4179 * The second case that we need to test for is this one:
4181 * |--------- cluster # N ----------------|
4182 * |--- requested region --| |------- extent ----|
4183 * |=======================|
4185 * The third case is when the requested region lies between two extents
4186 * within the same cluster:
4187 * |------------- cluster # N-------------|
4188 * |----- ex -----| |---- ex_right ----|
4189 * |------ requested region ------|
4190 * |================|
4192 * In each of the above cases, we need to set the map->m_pblk and
4193 * map->m_len so it corresponds to the return the extent labelled as
4194 * "|====|" from cluster #N, since it is already in use for data in
4195 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4196 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4197 * as a new "allocated" block region. Otherwise, we will return 0 and
4198 * ext4_ext_map_blocks() will then allocate one or more new clusters
4199 * by calling ext4_mb_new_blocks().
4201 static int get_implied_cluster_alloc(struct super_block
*sb
,
4202 struct ext4_map_blocks
*map
,
4203 struct ext4_extent
*ex
,
4204 struct ext4_ext_path
*path
)
4206 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4207 ext4_lblk_t c_offset
= EXT4_LBLK_COFF(sbi
, map
->m_lblk
);
4208 ext4_lblk_t ex_cluster_start
, ex_cluster_end
;
4209 ext4_lblk_t rr_cluster_start
;
4210 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
4211 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
4212 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
4214 /* The extent passed in that we are trying to match */
4215 ex_cluster_start
= EXT4_B2C(sbi
, ee_block
);
4216 ex_cluster_end
= EXT4_B2C(sbi
, ee_block
+ ee_len
- 1);
4218 /* The requested region passed into ext4_map_blocks() */
4219 rr_cluster_start
= EXT4_B2C(sbi
, map
->m_lblk
);
4221 if ((rr_cluster_start
== ex_cluster_end
) ||
4222 (rr_cluster_start
== ex_cluster_start
)) {
4223 if (rr_cluster_start
== ex_cluster_end
)
4224 ee_start
+= ee_len
- 1;
4225 map
->m_pblk
= EXT4_PBLK_CMASK(sbi
, ee_start
) + c_offset
;
4226 map
->m_len
= min(map
->m_len
,
4227 (unsigned) sbi
->s_cluster_ratio
- c_offset
);
4229 * Check for and handle this case:
4231 * |--------- cluster # N-------------|
4232 * |------- extent ----|
4233 * |--- requested region ---|
4237 if (map
->m_lblk
< ee_block
)
4238 map
->m_len
= min(map
->m_len
, ee_block
- map
->m_lblk
);
4241 * Check for the case where there is already another allocated
4242 * block to the right of 'ex' but before the end of the cluster.
4244 * |------------- cluster # N-------------|
4245 * |----- ex -----| |---- ex_right ----|
4246 * |------ requested region ------|
4247 * |================|
4249 if (map
->m_lblk
> ee_block
) {
4250 ext4_lblk_t next
= ext4_ext_next_allocated_block(path
);
4251 map
->m_len
= min(map
->m_len
, next
- map
->m_lblk
);
4254 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 1);
4258 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 0);
4264 * Block allocation/map/preallocation routine for extents based files
4267 * Need to be called with
4268 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4269 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
4271 * return > 0, number of of blocks already mapped/allocated
4272 * if create == 0 and these are pre-allocated blocks
4273 * buffer head is unmapped
4274 * otherwise blocks are mapped
4276 * return = 0, if plain look up failed (blocks have not been allocated)
4277 * buffer head is unmapped
4279 * return < 0, error case.
4281 int ext4_ext_map_blocks(handle_t
*handle
, struct inode
*inode
,
4282 struct ext4_map_blocks
*map
, int flags
)
4284 struct ext4_ext_path
*path
= NULL
;
4285 struct ext4_extent newex
, *ex
, *ex2
;
4286 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
4287 ext4_fsblk_t newblock
= 0;
4288 int free_on_err
= 0, err
= 0, depth
, ret
;
4289 unsigned int allocated
= 0, offset
= 0;
4290 unsigned int allocated_clusters
= 0;
4291 struct ext4_allocation_request ar
;
4292 ext4_lblk_t cluster_offset
;
4293 bool map_from_cluster
= false;
4295 ext_debug("blocks %u/%u requested for inode %lu\n",
4296 map
->m_lblk
, map
->m_len
, inode
->i_ino
);
4297 trace_ext4_ext_map_blocks_enter(inode
, map
->m_lblk
, map
->m_len
, flags
);
4299 /* find extent for this block */
4300 path
= ext4_find_extent(inode
, map
->m_lblk
, NULL
, 0);
4302 err
= PTR_ERR(path
);
4307 depth
= ext_depth(inode
);
4310 * consistent leaf must not be empty;
4311 * this situation is possible, though, _during_ tree modification;
4312 * this is why assert can't be put in ext4_find_extent()
4314 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
4315 EXT4_ERROR_INODE(inode
, "bad extent address "
4316 "lblock: %lu, depth: %d pblock %lld",
4317 (unsigned long) map
->m_lblk
, depth
,
4318 path
[depth
].p_block
);
4319 err
= -EFSCORRUPTED
;
4323 ex
= path
[depth
].p_ext
;
4325 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
4326 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
4327 unsigned short ee_len
;
4331 * unwritten extents are treated as holes, except that
4332 * we split out initialized portions during a write.
4334 ee_len
= ext4_ext_get_actual_len(ex
);
4336 trace_ext4_ext_show_extent(inode
, ee_block
, ee_start
, ee_len
);
4338 /* if found extent covers block, simply return it */
4339 if (in_range(map
->m_lblk
, ee_block
, ee_len
)) {
4340 newblock
= map
->m_lblk
- ee_block
+ ee_start
;
4341 /* number of remaining blocks in the extent */
4342 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
4343 ext_debug("%u fit into %u:%d -> %llu\n", map
->m_lblk
,
4344 ee_block
, ee_len
, newblock
);
4347 * If the extent is initialized check whether the
4348 * caller wants to convert it to unwritten.
4350 if ((!ext4_ext_is_unwritten(ex
)) &&
4351 (flags
& EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
)) {
4352 allocated
= convert_initialized_extent(
4353 handle
, inode
, map
, &path
,
4356 } else if (!ext4_ext_is_unwritten(ex
))
4359 ret
= ext4_ext_handle_unwritten_extents(
4360 handle
, inode
, map
, &path
, flags
,
4361 allocated
, newblock
);
4371 * requested block isn't allocated yet;
4372 * we couldn't try to create block if create flag is zero
4374 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
4375 ext4_lblk_t hole_start
, hole_len
;
4377 hole_start
= map
->m_lblk
;
4378 hole_len
= ext4_ext_determine_hole(inode
, path
, &hole_start
);
4380 * put just found gap into cache to speed up
4381 * subsequent requests
4383 ext4_ext_put_gap_in_cache(inode
, hole_start
, hole_len
);
4385 /* Update hole_len to reflect hole size after map->m_lblk */
4386 if (hole_start
!= map
->m_lblk
)
4387 hole_len
-= map
->m_lblk
- hole_start
;
4389 map
->m_len
= min_t(unsigned int, map
->m_len
, hole_len
);
4395 * Okay, we need to do block allocation.
4397 newex
.ee_block
= cpu_to_le32(map
->m_lblk
);
4398 cluster_offset
= EXT4_LBLK_COFF(sbi
, map
->m_lblk
);
4401 * If we are doing bigalloc, check to see if the extent returned
4402 * by ext4_find_extent() implies a cluster we can use.
4404 if (cluster_offset
&& ex
&&
4405 get_implied_cluster_alloc(inode
->i_sb
, map
, ex
, path
)) {
4406 ar
.len
= allocated
= map
->m_len
;
4407 newblock
= map
->m_pblk
;
4408 map_from_cluster
= true;
4409 goto got_allocated_blocks
;
4412 /* find neighbour allocated blocks */
4413 ar
.lleft
= map
->m_lblk
;
4414 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
4417 ar
.lright
= map
->m_lblk
;
4419 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
, &ex2
);
4423 /* Check if the extent after searching to the right implies a
4424 * cluster we can use. */
4425 if ((sbi
->s_cluster_ratio
> 1) && ex2
&&
4426 get_implied_cluster_alloc(inode
->i_sb
, map
, ex2
, path
)) {
4427 ar
.len
= allocated
= map
->m_len
;
4428 newblock
= map
->m_pblk
;
4429 map_from_cluster
= true;
4430 goto got_allocated_blocks
;
4434 * See if request is beyond maximum number of blocks we can have in
4435 * a single extent. For an initialized extent this limit is
4436 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4437 * EXT_UNWRITTEN_MAX_LEN.
4439 if (map
->m_len
> EXT_INIT_MAX_LEN
&&
4440 !(flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
))
4441 map
->m_len
= EXT_INIT_MAX_LEN
;
4442 else if (map
->m_len
> EXT_UNWRITTEN_MAX_LEN
&&
4443 (flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
))
4444 map
->m_len
= EXT_UNWRITTEN_MAX_LEN
;
4446 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
4447 newex
.ee_len
= cpu_to_le16(map
->m_len
);
4448 err
= ext4_ext_check_overlap(sbi
, inode
, &newex
, path
);
4450 allocated
= ext4_ext_get_actual_len(&newex
);
4452 allocated
= map
->m_len
;
4454 /* allocate new block */
4456 ar
.goal
= ext4_ext_find_goal(inode
, path
, map
->m_lblk
);
4457 ar
.logical
= map
->m_lblk
;
4459 * We calculate the offset from the beginning of the cluster
4460 * for the logical block number, since when we allocate a
4461 * physical cluster, the physical block should start at the
4462 * same offset from the beginning of the cluster. This is
4463 * needed so that future calls to get_implied_cluster_alloc()
4466 offset
= EXT4_LBLK_COFF(sbi
, map
->m_lblk
);
4467 ar
.len
= EXT4_NUM_B2C(sbi
, offset
+allocated
);
4469 ar
.logical
-= offset
;
4470 if (S_ISREG(inode
->i_mode
))
4471 ar
.flags
= EXT4_MB_HINT_DATA
;
4473 /* disable in-core preallocation for non-regular files */
4475 if (flags
& EXT4_GET_BLOCKS_NO_NORMALIZE
)
4476 ar
.flags
|= EXT4_MB_HINT_NOPREALLOC
;
4477 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
4478 ar
.flags
|= EXT4_MB_DELALLOC_RESERVED
;
4479 if (flags
& EXT4_GET_BLOCKS_METADATA_NOFAIL
)
4480 ar
.flags
|= EXT4_MB_USE_RESERVED
;
4481 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
4484 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
4485 ar
.goal
, newblock
, allocated
);
4487 allocated_clusters
= ar
.len
;
4488 ar
.len
= EXT4_C2B(sbi
, ar
.len
) - offset
;
4489 if (ar
.len
> allocated
)
4492 got_allocated_blocks
:
4493 /* try to insert new extent into found leaf and return */
4494 ext4_ext_store_pblock(&newex
, newblock
+ offset
);
4495 newex
.ee_len
= cpu_to_le16(ar
.len
);
4496 /* Mark unwritten */
4497 if (flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
){
4498 ext4_ext_mark_unwritten(&newex
);
4499 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
4503 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0)
4504 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
4507 err
= ext4_ext_insert_extent(handle
, inode
, &path
,
4510 if (err
&& free_on_err
) {
4511 int fb_flags
= flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
?
4512 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
: 0;
4513 /* free data blocks we just allocated */
4514 /* not a good idea to call discard here directly,
4515 * but otherwise we'd need to call it every free() */
4516 ext4_discard_preallocations(inode
);
4517 ext4_free_blocks(handle
, inode
, NULL
, newblock
,
4518 EXT4_C2B(sbi
, allocated_clusters
), fb_flags
);
4522 /* previous routine could use block we allocated */
4523 newblock
= ext4_ext_pblock(&newex
);
4524 allocated
= ext4_ext_get_actual_len(&newex
);
4525 if (allocated
> map
->m_len
)
4526 allocated
= map
->m_len
;
4527 map
->m_flags
|= EXT4_MAP_NEW
;
4530 * Reduce the reserved cluster count to reflect successful deferred
4531 * allocation of delayed allocated clusters or direct allocation of
4532 * clusters discovered to be delayed allocated. Once allocated, a
4533 * cluster is not included in the reserved count.
4535 if (test_opt(inode
->i_sb
, DELALLOC
) && !map_from_cluster
) {
4536 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) {
4538 * When allocating delayed allocated clusters, simply
4539 * reduce the reserved cluster count and claim quota
4541 ext4_da_update_reserve_space(inode
, allocated_clusters
,
4544 ext4_lblk_t lblk
, len
;
4548 * When allocating non-delayed allocated clusters
4549 * (from fallocate, filemap, DIO, or clusters
4550 * allocated when delalloc has been disabled by
4551 * ext4_nonda_switch), reduce the reserved cluster
4552 * count by the number of allocated clusters that
4553 * have previously been delayed allocated. Quota
4554 * has been claimed by ext4_mb_new_blocks() above,
4555 * so release the quota reservations made for any
4556 * previously delayed allocated clusters.
4558 lblk
= EXT4_LBLK_CMASK(sbi
, map
->m_lblk
);
4559 len
= allocated_clusters
<< sbi
->s_cluster_bits
;
4560 n
= ext4_es_delayed_clu(inode
, lblk
, len
);
4562 ext4_da_update_reserve_space(inode
, (int) n
, 0);
4567 * Cache the extent and update transaction to commit on fdatasync only
4568 * when it is _not_ an unwritten extent.
4570 if ((flags
& EXT4_GET_BLOCKS_UNWRIT_EXT
) == 0)
4571 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4573 ext4_update_inode_fsync_trans(handle
, inode
, 0);
4575 if (allocated
> map
->m_len
)
4576 allocated
= map
->m_len
;
4577 ext4_ext_show_leaf(inode
, path
);
4578 map
->m_flags
|= EXT4_MAP_MAPPED
;
4579 map
->m_pblk
= newblock
;
4580 map
->m_len
= allocated
;
4582 ext4_ext_drop_refs(path
);
4585 trace_ext4_ext_map_blocks_exit(inode
, flags
, map
,
4586 err
? err
: allocated
);
4587 return err
? err
: allocated
;
4590 int ext4_ext_truncate(handle_t
*handle
, struct inode
*inode
)
4592 struct super_block
*sb
= inode
->i_sb
;
4593 ext4_lblk_t last_block
;
4597 * TODO: optimization is possible here.
4598 * Probably we need not scan at all,
4599 * because page truncation is enough.
4602 /* we have to know where to truncate from in crash case */
4603 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
4604 err
= ext4_mark_inode_dirty(handle
, inode
);
4608 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
4609 >> EXT4_BLOCK_SIZE_BITS(sb
);
4611 err
= ext4_es_remove_extent(inode
, last_block
,
4612 EXT_MAX_BLOCKS
- last_block
);
4613 if (err
== -ENOMEM
) {
4615 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
4620 return ext4_ext_remove_space(inode
, last_block
, EXT_MAX_BLOCKS
- 1);
4623 static int ext4_alloc_file_blocks(struct file
*file
, ext4_lblk_t offset
,
4624 ext4_lblk_t len
, loff_t new_size
,
4627 struct inode
*inode
= file_inode(file
);
4633 struct ext4_map_blocks map
;
4634 unsigned int credits
;
4637 BUG_ON(!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
));
4638 map
.m_lblk
= offset
;
4641 * Don't normalize the request if it can fit in one extent so
4642 * that it doesn't get unnecessarily split into multiple
4645 if (len
<= EXT_UNWRITTEN_MAX_LEN
)
4646 flags
|= EXT4_GET_BLOCKS_NO_NORMALIZE
;
4649 * credits to insert 1 extent into extent tree
4651 credits
= ext4_chunk_trans_blocks(inode
, len
);
4652 depth
= ext_depth(inode
);
4655 while (ret
>= 0 && len
) {
4657 * Recalculate credits when extent tree depth changes.
4659 if (depth
!= ext_depth(inode
)) {
4660 credits
= ext4_chunk_trans_blocks(inode
, len
);
4661 depth
= ext_depth(inode
);
4664 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
,
4666 if (IS_ERR(handle
)) {
4667 ret
= PTR_ERR(handle
);
4670 ret
= ext4_map_blocks(handle
, inode
, &map
, flags
);
4672 ext4_debug("inode #%lu: block %u: len %u: "
4673 "ext4_ext_map_blocks returned %d",
4674 inode
->i_ino
, map
.m_lblk
,
4676 ext4_mark_inode_dirty(handle
, inode
);
4677 ret2
= ext4_journal_stop(handle
);
4681 map
.m_len
= len
= len
- ret
;
4682 epos
= (loff_t
)map
.m_lblk
<< inode
->i_blkbits
;
4683 inode
->i_ctime
= current_time(inode
);
4685 if (epos
> new_size
)
4687 if (ext4_update_inode_size(inode
, epos
) & 0x1)
4688 inode
->i_mtime
= inode
->i_ctime
;
4690 if (epos
> inode
->i_size
)
4691 ext4_set_inode_flag(inode
,
4692 EXT4_INODE_EOFBLOCKS
);
4694 ext4_mark_inode_dirty(handle
, inode
);
4695 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4696 ret2
= ext4_journal_stop(handle
);
4700 if (ret
== -ENOSPC
&&
4701 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
4706 return ret
> 0 ? ret2
: ret
;
4709 static long ext4_zero_range(struct file
*file
, loff_t offset
,
4710 loff_t len
, int mode
)
4712 struct inode
*inode
= file_inode(file
);
4713 handle_t
*handle
= NULL
;
4714 unsigned int max_blocks
;
4715 loff_t new_size
= 0;
4719 int partial_begin
, partial_end
;
4722 unsigned int blkbits
= inode
->i_blkbits
;
4724 trace_ext4_zero_range(inode
, offset
, len
, mode
);
4726 if (!S_ISREG(inode
->i_mode
))
4729 /* Call ext4_force_commit to flush all data in case of data=journal. */
4730 if (ext4_should_journal_data(inode
)) {
4731 ret
= ext4_force_commit(inode
->i_sb
);
4737 * Round up offset. This is not fallocate, we neet to zero out
4738 * blocks, so convert interior block aligned part of the range to
4739 * unwritten and possibly manually zero out unaligned parts of the
4742 start
= round_up(offset
, 1 << blkbits
);
4743 end
= round_down((offset
+ len
), 1 << blkbits
);
4745 if (start
< offset
|| end
> offset
+ len
)
4747 partial_begin
= offset
& ((1 << blkbits
) - 1);
4748 partial_end
= (offset
+ len
) & ((1 << blkbits
) - 1);
4750 lblk
= start
>> blkbits
;
4751 max_blocks
= (end
>> blkbits
);
4752 if (max_blocks
< lblk
)
4760 * Indirect files do not support unwritten extnets
4762 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
4767 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
4768 (offset
+ len
> i_size_read(inode
) ||
4769 offset
+ len
> EXT4_I(inode
)->i_disksize
)) {
4770 new_size
= offset
+ len
;
4771 ret
= inode_newsize_ok(inode
, new_size
);
4776 flags
= EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT
;
4777 if (mode
& FALLOC_FL_KEEP_SIZE
)
4778 flags
|= EXT4_GET_BLOCKS_KEEP_SIZE
;
4780 /* Wait all existing dio workers, newcomers will block on i_mutex */
4781 inode_dio_wait(inode
);
4783 /* Preallocate the range including the unaligned edges */
4784 if (partial_begin
|| partial_end
) {
4785 ret
= ext4_alloc_file_blocks(file
,
4786 round_down(offset
, 1 << blkbits
) >> blkbits
,
4787 (round_up((offset
+ len
), 1 << blkbits
) -
4788 round_down(offset
, 1 << blkbits
)) >> blkbits
,
4795 /* Zero range excluding the unaligned edges */
4796 if (max_blocks
> 0) {
4797 flags
|= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN
|
4801 * Prevent page faults from reinstantiating pages we have
4802 * released from page cache.
4804 down_write(&EXT4_I(inode
)->i_mmap_sem
);
4806 ret
= ext4_break_layouts(inode
);
4808 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4812 ret
= ext4_update_disksize_before_punch(inode
, offset
, len
);
4814 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4817 /* Now release the pages and zero block aligned part of pages */
4818 truncate_pagecache_range(inode
, start
, end
- 1);
4819 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
4821 ret
= ext4_alloc_file_blocks(file
, lblk
, max_blocks
, new_size
,
4823 up_write(&EXT4_I(inode
)->i_mmap_sem
);
4827 if (!partial_begin
&& !partial_end
)
4831 * In worst case we have to writeout two nonadjacent unwritten
4832 * blocks and update the inode
4834 credits
= (2 * ext4_ext_index_trans_blocks(inode
, 2)) + 1;
4835 if (ext4_should_journal_data(inode
))
4837 handle
= ext4_journal_start(inode
, EXT4_HT_MISC
, credits
);
4838 if (IS_ERR(handle
)) {
4839 ret
= PTR_ERR(handle
);
4840 ext4_std_error(inode
->i_sb
, ret
);
4844 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
4846 ext4_update_inode_size(inode
, new_size
);
4849 * Mark that we allocate beyond EOF so the subsequent truncate
4850 * can proceed even if the new size is the same as i_size.
4852 if ((offset
+ len
) > i_size_read(inode
))
4853 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
4855 ext4_mark_inode_dirty(handle
, inode
);
4857 /* Zero out partial block at the edges of the range */
4858 ret
= ext4_zero_partial_blocks(handle
, inode
, offset
, len
);
4860 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4862 if (file
->f_flags
& O_SYNC
)
4863 ext4_handle_sync(handle
);
4865 ext4_journal_stop(handle
);
4867 inode_unlock(inode
);
4872 * preallocate space for a file. This implements ext4's fallocate file
4873 * operation, which gets called from sys_fallocate system call.
4874 * For block-mapped files, posix_fallocate should fall back to the method
4875 * of writing zeroes to the required new blocks (the same behavior which is
4876 * expected for file systems which do not support fallocate() system call).
4878 long ext4_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
4880 struct inode
*inode
= file_inode(file
);
4881 loff_t new_size
= 0;
4882 unsigned int max_blocks
;
4886 unsigned int blkbits
= inode
->i_blkbits
;
4889 * Encrypted inodes can't handle collapse range or insert
4890 * range since we would need to re-encrypt blocks with a
4891 * different IV or XTS tweak (which are based on the logical
4894 * XXX It's not clear why zero range isn't working, but we'll
4895 * leave it disabled for encrypted inodes for now. This is a
4896 * bug we should fix....
4898 if (IS_ENCRYPTED(inode
) &&
4899 (mode
& (FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_INSERT_RANGE
|
4900 FALLOC_FL_ZERO_RANGE
)))
4903 /* Return error if mode is not supported */
4904 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
|
4905 FALLOC_FL_COLLAPSE_RANGE
| FALLOC_FL_ZERO_RANGE
|
4906 FALLOC_FL_INSERT_RANGE
))
4909 if (mode
& FALLOC_FL_PUNCH_HOLE
)
4910 return ext4_punch_hole(inode
, offset
, len
);
4912 ret
= ext4_convert_inline_data(inode
);
4916 if (mode
& FALLOC_FL_COLLAPSE_RANGE
)
4917 return ext4_collapse_range(inode
, offset
, len
);
4919 if (mode
& FALLOC_FL_INSERT_RANGE
)
4920 return ext4_insert_range(inode
, offset
, len
);
4922 if (mode
& FALLOC_FL_ZERO_RANGE
)
4923 return ext4_zero_range(file
, offset
, len
, mode
);
4925 trace_ext4_fallocate_enter(inode
, offset
, len
, mode
);
4926 lblk
= offset
>> blkbits
;
4928 max_blocks
= EXT4_MAX_BLOCKS(len
, offset
, blkbits
);
4929 flags
= EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT
;
4930 if (mode
& FALLOC_FL_KEEP_SIZE
)
4931 flags
|= EXT4_GET_BLOCKS_KEEP_SIZE
;
4936 * We only support preallocation for extent-based files only
4938 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
4943 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
4944 (offset
+ len
> i_size_read(inode
) ||
4945 offset
+ len
> EXT4_I(inode
)->i_disksize
)) {
4946 new_size
= offset
+ len
;
4947 ret
= inode_newsize_ok(inode
, new_size
);
4952 /* Wait all existing dio workers, newcomers will block on i_mutex */
4953 inode_dio_wait(inode
);
4955 ret
= ext4_alloc_file_blocks(file
, lblk
, max_blocks
, new_size
, flags
);
4959 if (file
->f_flags
& O_SYNC
&& EXT4_SB(inode
->i_sb
)->s_journal
) {
4960 ret
= jbd2_complete_transaction(EXT4_SB(inode
->i_sb
)->s_journal
,
4961 EXT4_I(inode
)->i_sync_tid
);
4964 inode_unlock(inode
);
4965 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
, ret
);
4970 * This function convert a range of blocks to written extents
4971 * The caller of this function will pass the start offset and the size.
4972 * all unwritten extents within this range will be converted to
4975 * This function is called from the direct IO end io call back
4976 * function, to convert the fallocated extents after IO is completed.
4977 * Returns 0 on success.
4979 int ext4_convert_unwritten_extents(handle_t
*handle
, struct inode
*inode
,
4980 loff_t offset
, ssize_t len
)
4982 unsigned int max_blocks
;
4985 struct ext4_map_blocks map
;
4986 unsigned int blkbits
= inode
->i_blkbits
;
4987 unsigned int credits
= 0;
4989 map
.m_lblk
= offset
>> blkbits
;
4990 max_blocks
= EXT4_MAX_BLOCKS(len
, offset
, blkbits
);
4994 * credits to insert 1 extent into extent tree
4996 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
4998 while (ret
>= 0 && ret
< max_blocks
) {
5000 map
.m_len
= (max_blocks
-= ret
);
5002 handle
= ext4_journal_start(inode
, EXT4_HT_MAP_BLOCKS
,
5004 if (IS_ERR(handle
)) {
5005 ret
= PTR_ERR(handle
);
5009 ret
= ext4_map_blocks(handle
, inode
, &map
,
5010 EXT4_GET_BLOCKS_IO_CONVERT_EXT
);
5012 ext4_warning(inode
->i_sb
,
5013 "inode #%lu: block %u: len %u: "
5014 "ext4_ext_map_blocks returned %d",
5015 inode
->i_ino
, map
.m_lblk
,
5017 ext4_mark_inode_dirty(handle
, inode
);
5019 ret2
= ext4_journal_stop(handle
);
5020 if (ret
<= 0 || ret2
)
5023 return ret
> 0 ? ret2
: ret
;
5026 int ext4_convert_unwritten_io_end_vec(handle_t
*handle
, ext4_io_end_t
*io_end
)
5029 struct ext4_io_end_vec
*io_end_vec
;
5032 * This is somewhat ugly but the idea is clear: When transaction is
5033 * reserved, everything goes into it. Otherwise we rather start several
5034 * smaller transactions for conversion of each extent separately.
5037 handle
= ext4_journal_start_reserved(handle
,
5038 EXT4_HT_EXT_CONVERT
);
5040 return PTR_ERR(handle
);
5043 list_for_each_entry(io_end_vec
, &io_end
->list_vec
, list
) {
5044 ret
= ext4_convert_unwritten_extents(handle
, io_end
->inode
,
5052 err
= ext4_journal_stop(handle
);
5054 return ret
< 0 ? ret
: err
;
5058 * If newes is not existing extent (newes->ec_pblk equals zero) find
5059 * delayed extent at start of newes and update newes accordingly and
5060 * return start of the next delayed extent.
5062 * If newes is existing extent (newes->ec_pblk is not equal zero)
5063 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
5064 * extent found. Leave newes unmodified.
5066 static int ext4_find_delayed_extent(struct inode
*inode
,
5067 struct extent_status
*newes
)
5069 struct extent_status es
;
5070 ext4_lblk_t block
, next_del
;
5072 if (newes
->es_pblk
== 0) {
5073 ext4_es_find_extent_range(inode
, &ext4_es_is_delayed
,
5075 newes
->es_lblk
+ newes
->es_len
- 1,
5079 * No extent in extent-tree contains block @newes->es_pblk,
5080 * then the block may stay in 1)a hole or 2)delayed-extent.
5086 if (es
.es_lblk
> newes
->es_lblk
) {
5088 newes
->es_len
= min(es
.es_lblk
- newes
->es_lblk
,
5093 newes
->es_len
= es
.es_lblk
+ es
.es_len
- newes
->es_lblk
;
5096 block
= newes
->es_lblk
+ newes
->es_len
;
5097 ext4_es_find_extent_range(inode
, &ext4_es_is_delayed
, block
,
5098 EXT_MAX_BLOCKS
, &es
);
5100 next_del
= EXT_MAX_BLOCKS
;
5102 next_del
= es
.es_lblk
;
5107 static int ext4_xattr_fiemap(struct inode
*inode
,
5108 struct fiemap_extent_info
*fieinfo
)
5112 __u32 flags
= FIEMAP_EXTENT_LAST
;
5113 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
5117 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
5118 struct ext4_iloc iloc
;
5119 int offset
; /* offset of xattr in inode */
5121 error
= ext4_get_inode_loc(inode
, &iloc
);
5124 physical
= (__u64
)iloc
.bh
->b_blocknr
<< blockbits
;
5125 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
5126 EXT4_I(inode
)->i_extra_isize
;
5128 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
5129 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
5131 } else { /* external block */
5132 physical
= (__u64
)EXT4_I(inode
)->i_file_acl
<< blockbits
;
5133 length
= inode
->i_sb
->s_blocksize
;
5137 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
5139 return (error
< 0 ? error
: 0);
5142 static int _ext4_fiemap(struct inode
*inode
,
5143 struct fiemap_extent_info
*fieinfo
,
5144 __u64 start
, __u64 len
,
5145 int (*fill
)(struct inode
*, ext4_lblk_t
,
5147 struct fiemap_extent_info
*))
5149 ext4_lblk_t start_blk
;
5150 u32 ext4_fiemap_flags
= FIEMAP_FLAG_SYNC
|FIEMAP_FLAG_XATTR
;
5154 if (ext4_has_inline_data(inode
)) {
5157 error
= ext4_inline_data_fiemap(inode
, fieinfo
, &has_inline
,
5164 if (fieinfo
->fi_flags
& FIEMAP_FLAG_CACHE
) {
5165 error
= ext4_ext_precache(inode
);
5168 fieinfo
->fi_flags
&= ~FIEMAP_FLAG_CACHE
;
5171 /* fallback to generic here if not in extents fmt */
5172 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) &&
5173 fill
== ext4_fill_fiemap_extents
)
5174 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
5177 if (fill
== ext4_fill_es_cache_info
)
5178 ext4_fiemap_flags
&= FIEMAP_FLAG_XATTR
;
5179 if (fiemap_check_flags(fieinfo
, ext4_fiemap_flags
))
5182 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
5183 error
= ext4_xattr_fiemap(inode
, fieinfo
);
5185 ext4_lblk_t len_blks
;
5188 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
5189 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
5190 if (last_blk
>= EXT_MAX_BLOCKS
)
5191 last_blk
= EXT_MAX_BLOCKS
-1;
5192 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
5195 * Walk the extent tree gathering extent information
5196 * and pushing extents back to the user.
5198 error
= fill(inode
, start_blk
, len_blks
, fieinfo
);
5203 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
5204 __u64 start
, __u64 len
)
5206 return _ext4_fiemap(inode
, fieinfo
, start
, len
,
5207 ext4_fill_fiemap_extents
);
5210 int ext4_get_es_cache(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
5211 __u64 start
, __u64 len
)
5213 if (ext4_has_inline_data(inode
)) {
5216 down_read(&EXT4_I(inode
)->xattr_sem
);
5217 has_inline
= ext4_has_inline_data(inode
);
5218 up_read(&EXT4_I(inode
)->xattr_sem
);
5223 return _ext4_fiemap(inode
, fieinfo
, start
, len
,
5224 ext4_fill_es_cache_info
);
5230 * Function to access the path buffer for marking it dirty.
5231 * It also checks if there are sufficient credits left in the journal handle
5235 ext4_access_path(handle_t
*handle
, struct inode
*inode
,
5236 struct ext4_ext_path
*path
)
5240 if (!ext4_handle_valid(handle
))
5244 * Check if need to extend journal credits
5245 * 3 for leaf, sb, and inode plus 2 (bmap and group
5246 * descriptor) for each block group; assume two block
5249 credits
= ext4_writepage_trans_blocks(inode
);
5250 err
= ext4_datasem_ensure_credits(handle
, inode
, 7, credits
, 0);
5254 err
= ext4_ext_get_access(handle
, inode
, path
);
5259 * ext4_ext_shift_path_extents:
5260 * Shift the extents of a path structure lying between path[depth].p_ext
5261 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5262 * if it is right shift or left shift operation.
5265 ext4_ext_shift_path_extents(struct ext4_ext_path
*path
, ext4_lblk_t shift
,
5266 struct inode
*inode
, handle_t
*handle
,
5267 enum SHIFT_DIRECTION SHIFT
)
5270 struct ext4_extent
*ex_start
, *ex_last
;
5272 depth
= path
->p_depth
;
5274 while (depth
>= 0) {
5275 if (depth
== path
->p_depth
) {
5276 ex_start
= path
[depth
].p_ext
;
5278 return -EFSCORRUPTED
;
5280 ex_last
= EXT_LAST_EXTENT(path
[depth
].p_hdr
);
5282 err
= ext4_access_path(handle
, inode
, path
+ depth
);
5286 if (ex_start
== EXT_FIRST_EXTENT(path
[depth
].p_hdr
))
5289 while (ex_start
<= ex_last
) {
5290 if (SHIFT
== SHIFT_LEFT
) {
5291 le32_add_cpu(&ex_start
->ee_block
,
5293 /* Try to merge to the left. */
5295 EXT_FIRST_EXTENT(path
[depth
].p_hdr
))
5297 ext4_ext_try_to_merge_right(inode
,
5298 path
, ex_start
- 1))
5303 le32_add_cpu(&ex_last
->ee_block
, shift
);
5304 ext4_ext_try_to_merge_right(inode
, path
,
5309 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
5313 if (--depth
< 0 || !update
)
5317 /* Update index too */
5318 err
= ext4_access_path(handle
, inode
, path
+ depth
);
5322 if (SHIFT
== SHIFT_LEFT
)
5323 le32_add_cpu(&path
[depth
].p_idx
->ei_block
, -shift
);
5325 le32_add_cpu(&path
[depth
].p_idx
->ei_block
, shift
);
5326 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
5330 /* we are done if current index is not a starting index */
5331 if (path
[depth
].p_idx
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))
5342 * ext4_ext_shift_extents:
5343 * All the extents which lies in the range from @start to the last allocated
5344 * block for the @inode are shifted either towards left or right (depending
5345 * upon @SHIFT) by @shift blocks.
5346 * On success, 0 is returned, error otherwise.
5349 ext4_ext_shift_extents(struct inode
*inode
, handle_t
*handle
,
5350 ext4_lblk_t start
, ext4_lblk_t shift
,
5351 enum SHIFT_DIRECTION SHIFT
)
5353 struct ext4_ext_path
*path
;
5355 struct ext4_extent
*extent
;
5356 ext4_lblk_t stop
, *iterator
, ex_start
, ex_end
;
5358 /* Let path point to the last extent */
5359 path
= ext4_find_extent(inode
, EXT_MAX_BLOCKS
- 1, NULL
,
5362 return PTR_ERR(path
);
5364 depth
= path
->p_depth
;
5365 extent
= path
[depth
].p_ext
;
5369 stop
= le32_to_cpu(extent
->ee_block
);
5372 * For left shifts, make sure the hole on the left is big enough to
5373 * accommodate the shift. For right shifts, make sure the last extent
5374 * won't be shifted beyond EXT_MAX_BLOCKS.
5376 if (SHIFT
== SHIFT_LEFT
) {
5377 path
= ext4_find_extent(inode
, start
- 1, &path
,
5380 return PTR_ERR(path
);
5381 depth
= path
->p_depth
;
5382 extent
= path
[depth
].p_ext
;
5384 ex_start
= le32_to_cpu(extent
->ee_block
);
5385 ex_end
= le32_to_cpu(extent
->ee_block
) +
5386 ext4_ext_get_actual_len(extent
);
5392 if ((start
== ex_start
&& shift
> ex_start
) ||
5393 (shift
> start
- ex_end
)) {
5398 if (shift
> EXT_MAX_BLOCKS
-
5399 (stop
+ ext4_ext_get_actual_len(extent
))) {
5406 * In case of left shift, iterator points to start and it is increased
5407 * till we reach stop. In case of right shift, iterator points to stop
5408 * and it is decreased till we reach start.
5410 if (SHIFT
== SHIFT_LEFT
)
5416 * Its safe to start updating extents. Start and stop are unsigned, so
5417 * in case of right shift if extent with 0 block is reached, iterator
5418 * becomes NULL to indicate the end of the loop.
5420 while (iterator
&& start
<= stop
) {
5421 path
= ext4_find_extent(inode
, *iterator
, &path
,
5424 return PTR_ERR(path
);
5425 depth
= path
->p_depth
;
5426 extent
= path
[depth
].p_ext
;
5428 EXT4_ERROR_INODE(inode
, "unexpected hole at %lu",
5429 (unsigned long) *iterator
);
5430 return -EFSCORRUPTED
;
5432 if (SHIFT
== SHIFT_LEFT
&& *iterator
>
5433 le32_to_cpu(extent
->ee_block
)) {
5434 /* Hole, move to the next extent */
5435 if (extent
< EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
5436 path
[depth
].p_ext
++;
5438 *iterator
= ext4_ext_next_allocated_block(path
);
5443 if (SHIFT
== SHIFT_LEFT
) {
5444 extent
= EXT_LAST_EXTENT(path
[depth
].p_hdr
);
5445 *iterator
= le32_to_cpu(extent
->ee_block
) +
5446 ext4_ext_get_actual_len(extent
);
5448 extent
= EXT_FIRST_EXTENT(path
[depth
].p_hdr
);
5449 if (le32_to_cpu(extent
->ee_block
) > 0)
5450 *iterator
= le32_to_cpu(extent
->ee_block
) - 1;
5452 /* Beginning is reached, end of the loop */
5454 /* Update path extent in case we need to stop */
5455 while (le32_to_cpu(extent
->ee_block
) < start
)
5457 path
[depth
].p_ext
= extent
;
5459 ret
= ext4_ext_shift_path_extents(path
, shift
, inode
,
5465 ext4_ext_drop_refs(path
);
5471 * ext4_collapse_range:
5472 * This implements the fallocate's collapse range functionality for ext4
5473 * Returns: 0 and non-zero on error.
5475 int ext4_collapse_range(struct inode
*inode
, loff_t offset
, loff_t len
)
5477 struct super_block
*sb
= inode
->i_sb
;
5478 ext4_lblk_t punch_start
, punch_stop
;
5480 unsigned int credits
;
5481 loff_t new_size
, ioffset
;
5485 * We need to test this early because xfstests assumes that a
5486 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5487 * system does not support collapse range.
5489 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
5492 /* Collapse range works only on fs block size aligned offsets. */
5493 if (offset
& (EXT4_CLUSTER_SIZE(sb
) - 1) ||
5494 len
& (EXT4_CLUSTER_SIZE(sb
) - 1))
5497 if (!S_ISREG(inode
->i_mode
))
5500 trace_ext4_collapse_range(inode
, offset
, len
);
5502 punch_start
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
5503 punch_stop
= (offset
+ len
) >> EXT4_BLOCK_SIZE_BITS(sb
);
5505 /* Call ext4_force_commit to flush all data in case of data=journal. */
5506 if (ext4_should_journal_data(inode
)) {
5507 ret
= ext4_force_commit(inode
->i_sb
);
5514 * There is no need to overlap collapse range with EOF, in which case
5515 * it is effectively a truncate operation
5517 if (offset
+ len
>= i_size_read(inode
)) {
5522 /* Currently just for extent based files */
5523 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
5528 /* Wait for existing dio to complete */
5529 inode_dio_wait(inode
);
5532 * Prevent page faults from reinstantiating pages we have released from
5535 down_write(&EXT4_I(inode
)->i_mmap_sem
);
5537 ret
= ext4_break_layouts(inode
);
5542 * Need to round down offset to be aligned with page size boundary
5543 * for page size > block size.
5545 ioffset
= round_down(offset
, PAGE_SIZE
);
5547 * Write tail of the last page before removed range since it will get
5548 * removed from the page cache below.
5550 ret
= filemap_write_and_wait_range(inode
->i_mapping
, ioffset
, offset
);
5554 * Write data that will be shifted to preserve them when discarding
5555 * page cache below. We are also protected from pages becoming dirty
5558 ret
= filemap_write_and_wait_range(inode
->i_mapping
, offset
+ len
,
5562 truncate_pagecache(inode
, ioffset
);
5564 credits
= ext4_writepage_trans_blocks(inode
);
5565 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
5566 if (IS_ERR(handle
)) {
5567 ret
= PTR_ERR(handle
);
5571 down_write(&EXT4_I(inode
)->i_data_sem
);
5572 ext4_discard_preallocations(inode
);
5574 ret
= ext4_es_remove_extent(inode
, punch_start
,
5575 EXT_MAX_BLOCKS
- punch_start
);
5577 up_write(&EXT4_I(inode
)->i_data_sem
);
5581 ret
= ext4_ext_remove_space(inode
, punch_start
, punch_stop
- 1);
5583 up_write(&EXT4_I(inode
)->i_data_sem
);
5586 ext4_discard_preallocations(inode
);
5588 ret
= ext4_ext_shift_extents(inode
, handle
, punch_stop
,
5589 punch_stop
- punch_start
, SHIFT_LEFT
);
5591 up_write(&EXT4_I(inode
)->i_data_sem
);
5595 new_size
= i_size_read(inode
) - len
;
5596 i_size_write(inode
, new_size
);
5597 EXT4_I(inode
)->i_disksize
= new_size
;
5599 up_write(&EXT4_I(inode
)->i_data_sem
);
5601 ext4_handle_sync(handle
);
5602 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
5603 ext4_mark_inode_dirty(handle
, inode
);
5604 ext4_update_inode_fsync_trans(handle
, inode
, 1);
5607 ext4_journal_stop(handle
);
5609 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5611 inode_unlock(inode
);
5616 * ext4_insert_range:
5617 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5618 * The data blocks starting from @offset to the EOF are shifted by @len
5619 * towards right to create a hole in the @inode. Inode size is increased
5621 * Returns 0 on success, error otherwise.
5623 int ext4_insert_range(struct inode
*inode
, loff_t offset
, loff_t len
)
5625 struct super_block
*sb
= inode
->i_sb
;
5627 struct ext4_ext_path
*path
;
5628 struct ext4_extent
*extent
;
5629 ext4_lblk_t offset_lblk
, len_lblk
, ee_start_lblk
= 0;
5630 unsigned int credits
, ee_len
;
5631 int ret
= 0, depth
, split_flag
= 0;
5635 * We need to test this early because xfstests assumes that an
5636 * insert range of (0, 1) will return EOPNOTSUPP if the file
5637 * system does not support insert range.
5639 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))
5642 /* Insert range works only on fs block size aligned offsets. */
5643 if (offset
& (EXT4_CLUSTER_SIZE(sb
) - 1) ||
5644 len
& (EXT4_CLUSTER_SIZE(sb
) - 1))
5647 if (!S_ISREG(inode
->i_mode
))
5650 trace_ext4_insert_range(inode
, offset
, len
);
5652 offset_lblk
= offset
>> EXT4_BLOCK_SIZE_BITS(sb
);
5653 len_lblk
= len
>> EXT4_BLOCK_SIZE_BITS(sb
);
5655 /* Call ext4_force_commit to flush all data in case of data=journal */
5656 if (ext4_should_journal_data(inode
)) {
5657 ret
= ext4_force_commit(inode
->i_sb
);
5663 /* Currently just for extent based files */
5664 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)) {
5669 /* Check for wrap through zero */
5670 if (inode
->i_size
+ len
> inode
->i_sb
->s_maxbytes
) {
5675 /* Offset should be less than i_size */
5676 if (offset
>= i_size_read(inode
)) {
5681 /* Wait for existing dio to complete */
5682 inode_dio_wait(inode
);
5685 * Prevent page faults from reinstantiating pages we have released from
5688 down_write(&EXT4_I(inode
)->i_mmap_sem
);
5690 ret
= ext4_break_layouts(inode
);
5695 * Need to round down to align start offset to page size boundary
5696 * for page size > block size.
5698 ioffset
= round_down(offset
, PAGE_SIZE
);
5699 /* Write out all dirty pages */
5700 ret
= filemap_write_and_wait_range(inode
->i_mapping
, ioffset
,
5704 truncate_pagecache(inode
, ioffset
);
5706 credits
= ext4_writepage_trans_blocks(inode
);
5707 handle
= ext4_journal_start(inode
, EXT4_HT_TRUNCATE
, credits
);
5708 if (IS_ERR(handle
)) {
5709 ret
= PTR_ERR(handle
);
5713 /* Expand file to avoid data loss if there is error while shifting */
5714 inode
->i_size
+= len
;
5715 EXT4_I(inode
)->i_disksize
+= len
;
5716 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
5717 ret
= ext4_mark_inode_dirty(handle
, inode
);
5721 down_write(&EXT4_I(inode
)->i_data_sem
);
5722 ext4_discard_preallocations(inode
);
5724 path
= ext4_find_extent(inode
, offset_lblk
, NULL
, 0);
5726 up_write(&EXT4_I(inode
)->i_data_sem
);
5730 depth
= ext_depth(inode
);
5731 extent
= path
[depth
].p_ext
;
5733 ee_start_lblk
= le32_to_cpu(extent
->ee_block
);
5734 ee_len
= ext4_ext_get_actual_len(extent
);
5737 * If offset_lblk is not the starting block of extent, split
5738 * the extent @offset_lblk
5740 if ((offset_lblk
> ee_start_lblk
) &&
5741 (offset_lblk
< (ee_start_lblk
+ ee_len
))) {
5742 if (ext4_ext_is_unwritten(extent
))
5743 split_flag
= EXT4_EXT_MARK_UNWRIT1
|
5744 EXT4_EXT_MARK_UNWRIT2
;
5745 ret
= ext4_split_extent_at(handle
, inode
, &path
,
5746 offset_lblk
, split_flag
,
5748 EXT4_GET_BLOCKS_PRE_IO
|
5749 EXT4_GET_BLOCKS_METADATA_NOFAIL
);
5752 ext4_ext_drop_refs(path
);
5755 up_write(&EXT4_I(inode
)->i_data_sem
);
5759 ext4_ext_drop_refs(path
);
5763 ret
= ext4_es_remove_extent(inode
, offset_lblk
,
5764 EXT_MAX_BLOCKS
- offset_lblk
);
5766 up_write(&EXT4_I(inode
)->i_data_sem
);
5771 * if offset_lblk lies in a hole which is at start of file, use
5772 * ee_start_lblk to shift extents
5774 ret
= ext4_ext_shift_extents(inode
, handle
,
5775 ee_start_lblk
> offset_lblk
? ee_start_lblk
: offset_lblk
,
5776 len_lblk
, SHIFT_RIGHT
);
5778 up_write(&EXT4_I(inode
)->i_data_sem
);
5780 ext4_handle_sync(handle
);
5782 ext4_update_inode_fsync_trans(handle
, inode
, 1);
5785 ext4_journal_stop(handle
);
5787 up_write(&EXT4_I(inode
)->i_mmap_sem
);
5789 inode_unlock(inode
);
5794 * ext4_swap_extents() - Swap extents between two inodes
5795 * @handle: handle for this transaction
5796 * @inode1: First inode
5797 * @inode2: Second inode
5798 * @lblk1: Start block for first inode
5799 * @lblk2: Start block for second inode
5800 * @count: Number of blocks to swap
5801 * @unwritten: Mark second inode's extents as unwritten after swap
5802 * @erp: Pointer to save error value
5804 * This helper routine does exactly what is promise "swap extents". All other
5805 * stuff such as page-cache locking consistency, bh mapping consistency or
5806 * extent's data copying must be performed by caller.
5808 * i_mutex is held for both inodes
5809 * i_data_sem is locked for write for both inodes
5811 * All pages from requested range are locked for both inodes
5814 ext4_swap_extents(handle_t
*handle
, struct inode
*inode1
,
5815 struct inode
*inode2
, ext4_lblk_t lblk1
, ext4_lblk_t lblk2
,
5816 ext4_lblk_t count
, int unwritten
, int *erp
)
5818 struct ext4_ext_path
*path1
= NULL
;
5819 struct ext4_ext_path
*path2
= NULL
;
5820 int replaced_count
= 0;
5822 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1
)->i_data_sem
));
5823 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2
)->i_data_sem
));
5824 BUG_ON(!inode_is_locked(inode1
));
5825 BUG_ON(!inode_is_locked(inode2
));
5827 *erp
= ext4_es_remove_extent(inode1
, lblk1
, count
);
5830 *erp
= ext4_es_remove_extent(inode2
, lblk2
, count
);
5835 struct ext4_extent
*ex1
, *ex2
, tmp_ex
;
5836 ext4_lblk_t e1_blk
, e2_blk
;
5837 int e1_len
, e2_len
, len
;
5840 path1
= ext4_find_extent(inode1
, lblk1
, NULL
, EXT4_EX_NOCACHE
);
5841 if (IS_ERR(path1
)) {
5842 *erp
= PTR_ERR(path1
);
5848 path2
= ext4_find_extent(inode2
, lblk2
, NULL
, EXT4_EX_NOCACHE
);
5849 if (IS_ERR(path2
)) {
5850 *erp
= PTR_ERR(path2
);
5854 ex1
= path1
[path1
->p_depth
].p_ext
;
5855 ex2
= path2
[path2
->p_depth
].p_ext
;
5856 /* Do we have somthing to swap ? */
5857 if (unlikely(!ex2
|| !ex1
))
5860 e1_blk
= le32_to_cpu(ex1
->ee_block
);
5861 e2_blk
= le32_to_cpu(ex2
->ee_block
);
5862 e1_len
= ext4_ext_get_actual_len(ex1
);
5863 e2_len
= ext4_ext_get_actual_len(ex2
);
5866 if (!in_range(lblk1
, e1_blk
, e1_len
) ||
5867 !in_range(lblk2
, e2_blk
, e2_len
)) {
5868 ext4_lblk_t next1
, next2
;
5870 /* if hole after extent, then go to next extent */
5871 next1
= ext4_ext_next_allocated_block(path1
);
5872 next2
= ext4_ext_next_allocated_block(path2
);
5873 /* If hole before extent, then shift to that extent */
5878 /* Do we have something to swap */
5879 if (next1
== EXT_MAX_BLOCKS
|| next2
== EXT_MAX_BLOCKS
)
5881 /* Move to the rightest boundary */
5882 len
= next1
- lblk1
;
5883 if (len
< next2
- lblk2
)
5884 len
= next2
- lblk2
;
5893 /* Prepare left boundary */
5894 if (e1_blk
< lblk1
) {
5896 *erp
= ext4_force_split_extent_at(handle
, inode1
,
5901 if (e2_blk
< lblk2
) {
5903 *erp
= ext4_force_split_extent_at(handle
, inode2
,
5908 /* ext4_split_extent_at() may result in leaf extent split,
5909 * path must to be revalidated. */
5913 /* Prepare right boundary */
5915 if (len
> e1_blk
+ e1_len
- lblk1
)
5916 len
= e1_blk
+ e1_len
- lblk1
;
5917 if (len
> e2_blk
+ e2_len
- lblk2
)
5918 len
= e2_blk
+ e2_len
- lblk2
;
5920 if (len
!= e1_len
) {
5922 *erp
= ext4_force_split_extent_at(handle
, inode1
,
5923 &path1
, lblk1
+ len
, 0);
5927 if (len
!= e2_len
) {
5929 *erp
= ext4_force_split_extent_at(handle
, inode2
,
5930 &path2
, lblk2
+ len
, 0);
5934 /* ext4_split_extent_at() may result in leaf extent split,
5935 * path must to be revalidated. */
5939 BUG_ON(e2_len
!= e1_len
);
5940 *erp
= ext4_ext_get_access(handle
, inode1
, path1
+ path1
->p_depth
);
5943 *erp
= ext4_ext_get_access(handle
, inode2
, path2
+ path2
->p_depth
);
5947 /* Both extents are fully inside boundaries. Swap it now */
5949 ext4_ext_store_pblock(ex1
, ext4_ext_pblock(ex2
));
5950 ext4_ext_store_pblock(ex2
, ext4_ext_pblock(&tmp_ex
));
5951 ex1
->ee_len
= cpu_to_le16(e2_len
);
5952 ex2
->ee_len
= cpu_to_le16(e1_len
);
5954 ext4_ext_mark_unwritten(ex2
);
5955 if (ext4_ext_is_unwritten(&tmp_ex
))
5956 ext4_ext_mark_unwritten(ex1
);
5958 ext4_ext_try_to_merge(handle
, inode2
, path2
, ex2
);
5959 ext4_ext_try_to_merge(handle
, inode1
, path1
, ex1
);
5960 *erp
= ext4_ext_dirty(handle
, inode2
, path2
+
5964 *erp
= ext4_ext_dirty(handle
, inode1
, path1
+
5967 * Looks scarry ah..? second inode already points to new blocks,
5968 * and it was successfully dirtied. But luckily error may happen
5969 * only due to journal error, so full transaction will be
5976 replaced_count
+= len
;
5980 ext4_ext_drop_refs(path1
);
5982 ext4_ext_drop_refs(path2
);
5984 path1
= path2
= NULL
;
5986 return replaced_count
;
5990 * ext4_clu_mapped - determine whether any block in a logical cluster has
5991 * been mapped to a physical cluster
5993 * @inode - file containing the logical cluster
5994 * @lclu - logical cluster of interest
5996 * Returns 1 if any block in the logical cluster is mapped, signifying
5997 * that a physical cluster has been allocated for it. Otherwise,
5998 * returns 0. Can also return negative error codes. Derived from
5999 * ext4_ext_map_blocks().
6001 int ext4_clu_mapped(struct inode
*inode
, ext4_lblk_t lclu
)
6003 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
6004 struct ext4_ext_path
*path
;
6005 int depth
, mapped
= 0, err
= 0;
6006 struct ext4_extent
*extent
;
6007 ext4_lblk_t first_lblk
, first_lclu
, last_lclu
;
6009 /* search for the extent closest to the first block in the cluster */
6010 path
= ext4_find_extent(inode
, EXT4_C2B(sbi
, lclu
), NULL
, 0);
6012 err
= PTR_ERR(path
);
6017 depth
= ext_depth(inode
);
6020 * A consistent leaf must not be empty. This situation is possible,
6021 * though, _during_ tree modification, and it's why an assert can't
6022 * be put in ext4_find_extent().
6024 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
6025 EXT4_ERROR_INODE(inode
,
6026 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
6027 (unsigned long) EXT4_C2B(sbi
, lclu
),
6028 depth
, path
[depth
].p_block
);
6029 err
= -EFSCORRUPTED
;
6033 extent
= path
[depth
].p_ext
;
6035 /* can't be mapped if the extent tree is empty */
6039 first_lblk
= le32_to_cpu(extent
->ee_block
);
6040 first_lclu
= EXT4_B2C(sbi
, first_lblk
);
6043 * Three possible outcomes at this point - found extent spanning
6044 * the target cluster, to the left of the target cluster, or to the
6045 * right of the target cluster. The first two cases are handled here.
6046 * The last case indicates the target cluster is not mapped.
6048 if (lclu
>= first_lclu
) {
6049 last_lclu
= EXT4_B2C(sbi
, first_lblk
+
6050 ext4_ext_get_actual_len(extent
) - 1);
6051 if (lclu
<= last_lclu
) {
6054 first_lblk
= ext4_ext_next_allocated_block(path
);
6055 first_lclu
= EXT4_B2C(sbi
, first_lblk
);
6056 if (lclu
== first_lclu
)
6062 ext4_ext_drop_refs(path
);
6065 return err
? err
: mapped
;