2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include <linux/fiemap.h>
44 #include "ext4_jbd2.h"
45 #include "ext4_extents.h"
47 #include <trace/events/ext4.h>
49 static int ext4_split_extent(handle_t
*handle
,
51 struct ext4_ext_path
*path
,
52 struct ext4_map_blocks
*map
,
56 static int ext4_ext_truncate_extend_restart(handle_t
*handle
,
62 if (!ext4_handle_valid(handle
))
64 if (handle
->h_buffer_credits
> needed
)
66 err
= ext4_journal_extend(handle
, needed
);
69 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
81 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
82 struct ext4_ext_path
*path
)
85 /* path points to block */
86 return ext4_journal_get_write_access(handle
, path
->p_bh
);
88 /* path points to leaf/index in inode body */
89 /* we use in-core data, no need to protect them */
99 #define ext4_ext_dirty(handle, inode, path) \
100 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
101 static int __ext4_ext_dirty(const char *where
, unsigned int line
,
102 handle_t
*handle
, struct inode
*inode
,
103 struct ext4_ext_path
*path
)
107 /* path points to block */
108 err
= __ext4_handle_dirty_metadata(where
, line
, handle
,
111 /* path points to leaf/index in inode body */
112 err
= ext4_mark_inode_dirty(handle
, inode
);
117 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
118 struct ext4_ext_path
*path
,
124 struct ext4_extent
*ex
;
125 depth
= path
->p_depth
;
128 * Try to predict block placement assuming that we are
129 * filling in a file which will eventually be
130 * non-sparse --- i.e., in the case of libbfd writing
131 * an ELF object sections out-of-order but in a way
132 * the eventually results in a contiguous object or
133 * executable file, or some database extending a table
134 * space file. However, this is actually somewhat
135 * non-ideal if we are writing a sparse file such as
136 * qemu or KVM writing a raw image file that is going
137 * to stay fairly sparse, since it will end up
138 * fragmenting the file system's free space. Maybe we
139 * should have some hueristics or some way to allow
140 * userspace to pass a hint to file system,
141 * especially if the latter case turns out to be
144 ex
= path
[depth
].p_ext
;
146 ext4_fsblk_t ext_pblk
= ext4_ext_pblock(ex
);
147 ext4_lblk_t ext_block
= le32_to_cpu(ex
->ee_block
);
149 if (block
> ext_block
)
150 return ext_pblk
+ (block
- ext_block
);
152 return ext_pblk
- (ext_block
- block
);
155 /* it looks like index is empty;
156 * try to find starting block from index itself */
157 if (path
[depth
].p_bh
)
158 return path
[depth
].p_bh
->b_blocknr
;
161 /* OK. use inode's group */
162 return ext4_inode_to_goal_block(inode
);
166 * Allocation for a meta data block
169 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
170 struct ext4_ext_path
*path
,
171 struct ext4_extent
*ex
, int *err
, unsigned int flags
)
173 ext4_fsblk_t goal
, newblock
;
175 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
176 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, flags
,
181 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
185 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
186 / sizeof(struct ext4_extent
);
188 #ifdef AGGRESSIVE_TEST
196 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
200 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
201 / sizeof(struct ext4_extent_idx
);
203 #ifdef AGGRESSIVE_TEST
211 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
215 size
= sizeof(EXT4_I(inode
)->i_data
);
216 size
-= sizeof(struct ext4_extent_header
);
217 size
/= sizeof(struct ext4_extent
);
219 #ifdef AGGRESSIVE_TEST
227 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
231 size
= sizeof(EXT4_I(inode
)->i_data
);
232 size
-= sizeof(struct ext4_extent_header
);
233 size
/= sizeof(struct ext4_extent_idx
);
235 #ifdef AGGRESSIVE_TEST
244 * Calculate the number of metadata blocks needed
245 * to allocate @blocks
246 * Worse case is one block per extent
248 int ext4_ext_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
250 struct ext4_inode_info
*ei
= EXT4_I(inode
);
253 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
254 / sizeof(struct ext4_extent_idx
));
257 * If the new delayed allocation block is contiguous with the
258 * previous da block, it can share index blocks with the
259 * previous block, so we only need to allocate a new index
260 * block every idxs leaf blocks. At ldxs**2 blocks, we need
261 * an additional index block, and at ldxs**3 blocks, yet
262 * another index blocks.
264 if (ei
->i_da_metadata_calc_len
&&
265 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
266 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
268 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
270 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
272 ei
->i_da_metadata_calc_len
= 0;
274 ei
->i_da_metadata_calc_len
++;
275 ei
->i_da_metadata_calc_last_lblock
++;
280 * In the worst case we need a new set of index blocks at
281 * every level of the inode's extent tree.
283 ei
->i_da_metadata_calc_len
= 1;
284 ei
->i_da_metadata_calc_last_lblock
= lblock
;
285 return ext_depth(inode
) + 1;
289 ext4_ext_max_entries(struct inode
*inode
, int depth
)
293 if (depth
== ext_depth(inode
)) {
295 max
= ext4_ext_space_root(inode
, 1);
297 max
= ext4_ext_space_root_idx(inode
, 1);
300 max
= ext4_ext_space_block(inode
, 1);
302 max
= ext4_ext_space_block_idx(inode
, 1);
308 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
310 ext4_fsblk_t block
= ext4_ext_pblock(ext
);
311 int len
= ext4_ext_get_actual_len(ext
);
313 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
316 static int ext4_valid_extent_idx(struct inode
*inode
,
317 struct ext4_extent_idx
*ext_idx
)
319 ext4_fsblk_t block
= ext4_idx_pblock(ext_idx
);
321 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
324 static int ext4_valid_extent_entries(struct inode
*inode
,
325 struct ext4_extent_header
*eh
,
328 struct ext4_extent
*ext
;
329 struct ext4_extent_idx
*ext_idx
;
330 unsigned short entries
;
331 if (eh
->eh_entries
== 0)
334 entries
= le16_to_cpu(eh
->eh_entries
);
338 ext
= EXT_FIRST_EXTENT(eh
);
340 if (!ext4_valid_extent(inode
, ext
))
346 ext_idx
= EXT_FIRST_INDEX(eh
);
348 if (!ext4_valid_extent_idx(inode
, ext_idx
))
357 static int __ext4_ext_check(const char *function
, unsigned int line
,
358 struct inode
*inode
, struct ext4_extent_header
*eh
,
361 const char *error_msg
;
364 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
365 error_msg
= "invalid magic";
368 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
369 error_msg
= "unexpected eh_depth";
372 if (unlikely(eh
->eh_max
== 0)) {
373 error_msg
= "invalid eh_max";
376 max
= ext4_ext_max_entries(inode
, depth
);
377 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
378 error_msg
= "too large eh_max";
381 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
382 error_msg
= "invalid eh_entries";
385 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
386 error_msg
= "invalid extent entries";
392 ext4_error_inode(inode
, function
, line
, 0,
393 "bad header/extent: %s - magic %x, "
394 "entries %u, max %u(%u), depth %u(%u)",
395 error_msg
, le16_to_cpu(eh
->eh_magic
),
396 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
397 max
, le16_to_cpu(eh
->eh_depth
), depth
);
402 #define ext4_ext_check(inode, eh, depth) \
403 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
405 int ext4_ext_check_inode(struct inode
*inode
)
407 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
411 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
413 int k
, l
= path
->p_depth
;
416 for (k
= 0; k
<= l
; k
++, path
++) {
418 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
419 ext4_idx_pblock(path
->p_idx
));
420 } else if (path
->p_ext
) {
421 ext_debug(" %d:[%d]%d:%llu ",
422 le32_to_cpu(path
->p_ext
->ee_block
),
423 ext4_ext_is_uninitialized(path
->p_ext
),
424 ext4_ext_get_actual_len(path
->p_ext
),
425 ext4_ext_pblock(path
->p_ext
));
432 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
434 int depth
= ext_depth(inode
);
435 struct ext4_extent_header
*eh
;
436 struct ext4_extent
*ex
;
442 eh
= path
[depth
].p_hdr
;
443 ex
= EXT_FIRST_EXTENT(eh
);
445 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
447 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
448 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
449 ext4_ext_is_uninitialized(ex
),
450 ext4_ext_get_actual_len(ex
), ext4_ext_pblock(ex
));
455 static void ext4_ext_show_move(struct inode
*inode
, struct ext4_ext_path
*path
,
456 ext4_fsblk_t newblock
, int level
)
458 int depth
= ext_depth(inode
);
459 struct ext4_extent
*ex
;
461 if (depth
!= level
) {
462 struct ext4_extent_idx
*idx
;
463 idx
= path
[level
].p_idx
;
464 while (idx
<= EXT_MAX_INDEX(path
[level
].p_hdr
)) {
465 ext_debug("%d: move %d:%llu in new index %llu\n", level
,
466 le32_to_cpu(idx
->ei_block
),
467 ext4_idx_pblock(idx
),
475 ex
= path
[depth
].p_ext
;
476 while (ex
<= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
477 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
478 le32_to_cpu(ex
->ee_block
),
480 ext4_ext_is_uninitialized(ex
),
481 ext4_ext_get_actual_len(ex
),
488 #define ext4_ext_show_path(inode, path)
489 #define ext4_ext_show_leaf(inode, path)
490 #define ext4_ext_show_move(inode, path, newblock, level)
493 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
495 int depth
= path
->p_depth
;
498 for (i
= 0; i
<= depth
; i
++, path
++)
506 * ext4_ext_binsearch_idx:
507 * binary search for the closest index of the given block
508 * the header must be checked before calling this
511 ext4_ext_binsearch_idx(struct inode
*inode
,
512 struct ext4_ext_path
*path
, ext4_lblk_t block
)
514 struct ext4_extent_header
*eh
= path
->p_hdr
;
515 struct ext4_extent_idx
*r
, *l
, *m
;
518 ext_debug("binsearch for %u(idx): ", block
);
520 l
= EXT_FIRST_INDEX(eh
) + 1;
521 r
= EXT_LAST_INDEX(eh
);
524 if (block
< le32_to_cpu(m
->ei_block
))
528 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
529 m
, le32_to_cpu(m
->ei_block
),
530 r
, le32_to_cpu(r
->ei_block
));
534 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
535 ext4_idx_pblock(path
->p_idx
));
537 #ifdef CHECK_BINSEARCH
539 struct ext4_extent_idx
*chix
, *ix
;
542 chix
= ix
= EXT_FIRST_INDEX(eh
);
543 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
545 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
546 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
548 ix
, EXT_FIRST_INDEX(eh
));
549 printk(KERN_DEBUG
"%u <= %u\n",
550 le32_to_cpu(ix
->ei_block
),
551 le32_to_cpu(ix
[-1].ei_block
));
553 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
554 <= le32_to_cpu(ix
[-1].ei_block
));
555 if (block
< le32_to_cpu(ix
->ei_block
))
559 BUG_ON(chix
!= path
->p_idx
);
566 * ext4_ext_binsearch:
567 * binary search for closest extent of the given block
568 * the header must be checked before calling this
571 ext4_ext_binsearch(struct inode
*inode
,
572 struct ext4_ext_path
*path
, ext4_lblk_t block
)
574 struct ext4_extent_header
*eh
= path
->p_hdr
;
575 struct ext4_extent
*r
, *l
, *m
;
577 if (eh
->eh_entries
== 0) {
579 * this leaf is empty:
580 * we get such a leaf in split/add case
585 ext_debug("binsearch for %u: ", block
);
587 l
= EXT_FIRST_EXTENT(eh
) + 1;
588 r
= EXT_LAST_EXTENT(eh
);
592 if (block
< le32_to_cpu(m
->ee_block
))
596 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
597 m
, le32_to_cpu(m
->ee_block
),
598 r
, le32_to_cpu(r
->ee_block
));
602 ext_debug(" -> %d:%llu:[%d]%d ",
603 le32_to_cpu(path
->p_ext
->ee_block
),
604 ext4_ext_pblock(path
->p_ext
),
605 ext4_ext_is_uninitialized(path
->p_ext
),
606 ext4_ext_get_actual_len(path
->p_ext
));
608 #ifdef CHECK_BINSEARCH
610 struct ext4_extent
*chex
, *ex
;
613 chex
= ex
= EXT_FIRST_EXTENT(eh
);
614 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
615 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
616 <= le32_to_cpu(ex
[-1].ee_block
));
617 if (block
< le32_to_cpu(ex
->ee_block
))
621 BUG_ON(chex
!= path
->p_ext
);
627 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
629 struct ext4_extent_header
*eh
;
631 eh
= ext_inode_hdr(inode
);
634 eh
->eh_magic
= EXT4_EXT_MAGIC
;
635 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
636 ext4_mark_inode_dirty(handle
, inode
);
637 ext4_ext_invalidate_cache(inode
);
641 struct ext4_ext_path
*
642 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
643 struct ext4_ext_path
*path
)
645 struct ext4_extent_header
*eh
;
646 struct buffer_head
*bh
;
647 short int depth
, i
, ppos
= 0, alloc
= 0;
649 eh
= ext_inode_hdr(inode
);
650 depth
= ext_depth(inode
);
652 /* account possible depth increase */
654 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
657 return ERR_PTR(-ENOMEM
);
664 /* walk through the tree */
666 int need_to_validate
= 0;
668 ext_debug("depth %d: num %d, max %d\n",
669 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
671 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
672 path
[ppos
].p_block
= ext4_idx_pblock(path
[ppos
].p_idx
);
673 path
[ppos
].p_depth
= i
;
674 path
[ppos
].p_ext
= NULL
;
676 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
679 if (!bh_uptodate_or_lock(bh
)) {
680 trace_ext4_ext_load_extent(inode
, block
,
682 if (bh_submit_read(bh
) < 0) {
686 /* validate the extent entries */
687 need_to_validate
= 1;
689 eh
= ext_block_hdr(bh
);
691 if (unlikely(ppos
> depth
)) {
693 EXT4_ERROR_INODE(inode
,
694 "ppos %d > depth %d", ppos
, depth
);
697 path
[ppos
].p_bh
= bh
;
698 path
[ppos
].p_hdr
= eh
;
701 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
705 path
[ppos
].p_depth
= i
;
706 path
[ppos
].p_ext
= NULL
;
707 path
[ppos
].p_idx
= NULL
;
710 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
711 /* if not an empty leaf */
712 if (path
[ppos
].p_ext
)
713 path
[ppos
].p_block
= ext4_ext_pblock(path
[ppos
].p_ext
);
715 ext4_ext_show_path(inode
, path
);
720 ext4_ext_drop_refs(path
);
723 return ERR_PTR(-EIO
);
727 * ext4_ext_insert_index:
728 * insert new index [@logical;@ptr] into the block at @curp;
729 * check where to insert: before @curp or after @curp
731 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
732 struct ext4_ext_path
*curp
,
733 int logical
, ext4_fsblk_t ptr
)
735 struct ext4_extent_idx
*ix
;
738 err
= ext4_ext_get_access(handle
, inode
, curp
);
742 if (unlikely(logical
== le32_to_cpu(curp
->p_idx
->ei_block
))) {
743 EXT4_ERROR_INODE(inode
,
744 "logical %d == ei_block %d!",
745 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
749 if (unlikely(le16_to_cpu(curp
->p_hdr
->eh_entries
)
750 >= le16_to_cpu(curp
->p_hdr
->eh_max
))) {
751 EXT4_ERROR_INODE(inode
,
752 "eh_entries %d >= eh_max %d!",
753 le16_to_cpu(curp
->p_hdr
->eh_entries
),
754 le16_to_cpu(curp
->p_hdr
->eh_max
));
758 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
759 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
761 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
762 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
763 len
= len
< 0 ? 0 : len
;
764 ext_debug("insert new index %d after: %llu. "
765 "move %d from 0x%p to 0x%p\n",
767 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
768 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
770 ix
= curp
->p_idx
+ 1;
773 len
= len
* sizeof(struct ext4_extent_idx
);
774 len
= len
< 0 ? 0 : len
;
775 ext_debug("insert new index %d before: %llu. "
776 "move %d from 0x%p to 0x%p\n",
778 curp
->p_idx
, (curp
->p_idx
+ 1));
779 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
783 ix
->ei_block
= cpu_to_le32(logical
);
784 ext4_idx_store_pblock(ix
, ptr
);
785 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
787 if (unlikely(ix
> EXT_LAST_INDEX(curp
->p_hdr
))) {
788 EXT4_ERROR_INODE(inode
, "ix > EXT_LAST_INDEX!");
792 err
= ext4_ext_dirty(handle
, inode
, curp
);
793 ext4_std_error(inode
->i_sb
, err
);
800 * inserts new subtree into the path, using free index entry
802 * - allocates all needed blocks (new leaf and all intermediate index blocks)
803 * - makes decision where to split
804 * - moves remaining extents and index entries (right to the split point)
805 * into the newly allocated blocks
806 * - initializes subtree
808 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
810 struct ext4_ext_path
*path
,
811 struct ext4_extent
*newext
, int at
)
813 struct buffer_head
*bh
= NULL
;
814 int depth
= ext_depth(inode
);
815 struct ext4_extent_header
*neh
;
816 struct ext4_extent_idx
*fidx
;
818 ext4_fsblk_t newblock
, oldblock
;
820 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
823 /* make decision: where to split? */
824 /* FIXME: now decision is simplest: at current extent */
826 /* if current leaf will be split, then we should use
827 * border from split point */
828 if (unlikely(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
))) {
829 EXT4_ERROR_INODE(inode
, "p_ext > EXT_MAX_EXTENT!");
832 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
833 border
= path
[depth
].p_ext
[1].ee_block
;
834 ext_debug("leaf will be split."
835 " next leaf starts at %d\n",
836 le32_to_cpu(border
));
838 border
= newext
->ee_block
;
839 ext_debug("leaf will be added."
840 " next leaf starts at %d\n",
841 le32_to_cpu(border
));
845 * If error occurs, then we break processing
846 * and mark filesystem read-only. index won't
847 * be inserted and tree will be in consistent
848 * state. Next mount will repair buffers too.
852 * Get array to track all allocated blocks.
853 * We need this to handle errors and free blocks
856 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
860 /* allocate all needed blocks */
861 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
862 for (a
= 0; a
< depth
- at
; a
++) {
863 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
864 newext
, &err
, flags
);
867 ablocks
[a
] = newblock
;
870 /* initialize new leaf */
871 newblock
= ablocks
[--a
];
872 if (unlikely(newblock
== 0)) {
873 EXT4_ERROR_INODE(inode
, "newblock == 0!");
877 bh
= sb_getblk(inode
->i_sb
, newblock
);
884 err
= ext4_journal_get_create_access(handle
, bh
);
888 neh
= ext_block_hdr(bh
);
890 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
891 neh
->eh_magic
= EXT4_EXT_MAGIC
;
894 /* move remainder of path[depth] to the new leaf */
895 if (unlikely(path
[depth
].p_hdr
->eh_entries
!=
896 path
[depth
].p_hdr
->eh_max
)) {
897 EXT4_ERROR_INODE(inode
, "eh_entries %d != eh_max %d!",
898 path
[depth
].p_hdr
->eh_entries
,
899 path
[depth
].p_hdr
->eh_max
);
903 /* start copy from next extent */
904 m
= EXT_MAX_EXTENT(path
[depth
].p_hdr
) - path
[depth
].p_ext
++;
905 ext4_ext_show_move(inode
, path
, newblock
, depth
);
907 struct ext4_extent
*ex
;
908 ex
= EXT_FIRST_EXTENT(neh
);
909 memmove(ex
, path
[depth
].p_ext
, sizeof(struct ext4_extent
) * m
);
910 le16_add_cpu(&neh
->eh_entries
, m
);
913 set_buffer_uptodate(bh
);
916 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
922 /* correct old leaf */
924 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
927 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
928 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
934 /* create intermediate indexes */
936 if (unlikely(k
< 0)) {
937 EXT4_ERROR_INODE(inode
, "k %d < 0!", k
);
942 ext_debug("create %d intermediate indices\n", k
);
943 /* insert new index into current index block */
944 /* current depth stored in i var */
948 newblock
= ablocks
[--a
];
949 bh
= sb_getblk(inode
->i_sb
, newblock
);
956 err
= ext4_journal_get_create_access(handle
, bh
);
960 neh
= ext_block_hdr(bh
);
961 neh
->eh_entries
= cpu_to_le16(1);
962 neh
->eh_magic
= EXT4_EXT_MAGIC
;
963 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
964 neh
->eh_depth
= cpu_to_le16(depth
- i
);
965 fidx
= EXT_FIRST_INDEX(neh
);
966 fidx
->ei_block
= border
;
967 ext4_idx_store_pblock(fidx
, oldblock
);
969 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
970 i
, newblock
, le32_to_cpu(border
), oldblock
);
972 /* move remainder of path[i] to the new index block */
973 if (unlikely(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
974 EXT_LAST_INDEX(path
[i
].p_hdr
))) {
975 EXT4_ERROR_INODE(inode
,
976 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
977 le32_to_cpu(path
[i
].p_ext
->ee_block
));
981 /* start copy indexes */
982 m
= EXT_MAX_INDEX(path
[i
].p_hdr
) - path
[i
].p_idx
++;
983 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
984 EXT_MAX_INDEX(path
[i
].p_hdr
));
985 ext4_ext_show_move(inode
, path
, newblock
, i
);
987 memmove(++fidx
, path
[i
].p_idx
,
988 sizeof(struct ext4_extent_idx
) * m
);
989 le16_add_cpu(&neh
->eh_entries
, m
);
991 set_buffer_uptodate(bh
);
994 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1000 /* correct old index */
1002 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
1005 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1006 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1014 /* insert new index */
1015 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1016 le32_to_cpu(border
), newblock
);
1020 if (buffer_locked(bh
))
1026 /* free all allocated blocks in error case */
1027 for (i
= 0; i
< depth
; i
++) {
1030 ext4_free_blocks(handle
, inode
, NULL
, ablocks
[i
], 1,
1031 EXT4_FREE_BLOCKS_METADATA
);
1040 * ext4_ext_grow_indepth:
1041 * implements tree growing procedure:
1042 * - allocates new block
1043 * - moves top-level data (index block or leaf) into the new block
1044 * - initializes new top-level, creating index that points to the
1045 * just created block
1047 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1049 struct ext4_ext_path
*path
,
1050 struct ext4_extent
*newext
)
1052 struct ext4_ext_path
*curp
= path
;
1053 struct ext4_extent_header
*neh
;
1054 struct buffer_head
*bh
;
1055 ext4_fsblk_t newblock
;
1058 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
1059 newext
, &err
, flags
);
1063 bh
= sb_getblk(inode
->i_sb
, newblock
);
1066 ext4_std_error(inode
->i_sb
, err
);
1071 err
= ext4_journal_get_create_access(handle
, bh
);
1077 /* move top-level index/leaf into new block */
1078 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1080 /* set size of new block */
1081 neh
= ext_block_hdr(bh
);
1082 /* old root could have indexes or leaves
1083 * so calculate e_max right way */
1084 if (ext_depth(inode
))
1085 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1087 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1088 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1089 set_buffer_uptodate(bh
);
1092 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1096 /* create index in new top-level index: num,max,pointer */
1097 err
= ext4_ext_get_access(handle
, inode
, curp
);
1101 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1102 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1103 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1104 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1106 if (path
[0].p_hdr
->eh_depth
)
1107 curp
->p_idx
->ei_block
=
1108 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1110 curp
->p_idx
->ei_block
=
1111 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1112 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1114 neh
= ext_inode_hdr(inode
);
1115 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1116 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1117 le32_to_cpu(EXT_FIRST_INDEX(neh
)->ei_block
),
1118 ext4_idx_pblock(EXT_FIRST_INDEX(neh
)));
1120 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1121 err
= ext4_ext_dirty(handle
, inode
, curp
);
1129 * ext4_ext_create_new_leaf:
1130 * finds empty index and adds new leaf.
1131 * if no free index is found, then it requests in-depth growing.
1133 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1135 struct ext4_ext_path
*path
,
1136 struct ext4_extent
*newext
)
1138 struct ext4_ext_path
*curp
;
1139 int depth
, i
, err
= 0;
1142 i
= depth
= ext_depth(inode
);
1144 /* walk up to the tree and look for free index entry */
1145 curp
= path
+ depth
;
1146 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1151 /* we use already allocated block for index block,
1152 * so subsequent data blocks should be contiguous */
1153 if (EXT_HAS_FREE_INDEX(curp
)) {
1154 /* if we found index with free entry, then use that
1155 * entry: create all needed subtree and add new leaf */
1156 err
= ext4_ext_split(handle
, inode
, flags
, path
, newext
, i
);
1161 ext4_ext_drop_refs(path
);
1162 path
= ext4_ext_find_extent(inode
,
1163 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1166 err
= PTR_ERR(path
);
1168 /* tree is full, time to grow in depth */
1169 err
= ext4_ext_grow_indepth(handle
, inode
, flags
,
1175 ext4_ext_drop_refs(path
);
1176 path
= ext4_ext_find_extent(inode
,
1177 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1180 err
= PTR_ERR(path
);
1185 * only first (depth 0 -> 1) produces free space;
1186 * in all other cases we have to split the grown tree
1188 depth
= ext_depth(inode
);
1189 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1190 /* now we need to split */
1200 * search the closest allocated block to the left for *logical
1201 * and returns it at @logical + it's physical address at @phys
1202 * if *logical is the smallest allocated block, the function
1203 * returns 0 at @phys
1204 * return value contains 0 (success) or error code
1206 static int ext4_ext_search_left(struct inode
*inode
,
1207 struct ext4_ext_path
*path
,
1208 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1210 struct ext4_extent_idx
*ix
;
1211 struct ext4_extent
*ex
;
1214 if (unlikely(path
== NULL
)) {
1215 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1218 depth
= path
->p_depth
;
1221 if (depth
== 0 && path
->p_ext
== NULL
)
1224 /* usually extent in the path covers blocks smaller
1225 * then *logical, but it can be that extent is the
1226 * first one in the file */
1228 ex
= path
[depth
].p_ext
;
1229 ee_len
= ext4_ext_get_actual_len(ex
);
1230 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1231 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1232 EXT4_ERROR_INODE(inode
,
1233 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1234 *logical
, le32_to_cpu(ex
->ee_block
));
1237 while (--depth
>= 0) {
1238 ix
= path
[depth
].p_idx
;
1239 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1240 EXT4_ERROR_INODE(inode
,
1241 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1242 ix
!= NULL
? ix
->ei_block
: 0,
1243 EXT_FIRST_INDEX(path
[depth
].p_hdr
) != NULL
?
1244 EXT_FIRST_INDEX(path
[depth
].p_hdr
)->ei_block
: 0,
1252 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1253 EXT4_ERROR_INODE(inode
,
1254 "logical %d < ee_block %d + ee_len %d!",
1255 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1259 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1260 *phys
= ext4_ext_pblock(ex
) + ee_len
- 1;
1265 * search the closest allocated block to the right for *logical
1266 * and returns it at @logical + it's physical address at @phys
1267 * if *logical is the smallest allocated block, the function
1268 * returns 0 at @phys
1269 * return value contains 0 (success) or error code
1271 static int ext4_ext_search_right(struct inode
*inode
,
1272 struct ext4_ext_path
*path
,
1273 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
,
1274 struct ext4_extent
**ret_ex
)
1276 struct buffer_head
*bh
= NULL
;
1277 struct ext4_extent_header
*eh
;
1278 struct ext4_extent_idx
*ix
;
1279 struct ext4_extent
*ex
;
1281 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1284 if (unlikely(path
== NULL
)) {
1285 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1288 depth
= path
->p_depth
;
1291 if (depth
== 0 && path
->p_ext
== NULL
)
1294 /* usually extent in the path covers blocks smaller
1295 * then *logical, but it can be that extent is the
1296 * first one in the file */
1298 ex
= path
[depth
].p_ext
;
1299 ee_len
= ext4_ext_get_actual_len(ex
);
1300 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1301 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1302 EXT4_ERROR_INODE(inode
,
1303 "first_extent(path[%d].p_hdr) != ex",
1307 while (--depth
>= 0) {
1308 ix
= path
[depth
].p_idx
;
1309 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1310 EXT4_ERROR_INODE(inode
,
1311 "ix != EXT_FIRST_INDEX *logical %d!",
1319 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1320 EXT4_ERROR_INODE(inode
,
1321 "logical %d < ee_block %d + ee_len %d!",
1322 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1326 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1327 /* next allocated block in this leaf */
1332 /* go up and search for index to the right */
1333 while (--depth
>= 0) {
1334 ix
= path
[depth
].p_idx
;
1335 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1339 /* we've gone up to the root and found no index to the right */
1343 /* we've found index to the right, let's
1344 * follow it and find the closest allocated
1345 * block to the right */
1347 block
= ext4_idx_pblock(ix
);
1348 while (++depth
< path
->p_depth
) {
1349 bh
= sb_bread(inode
->i_sb
, block
);
1352 eh
= ext_block_hdr(bh
);
1353 /* subtract from p_depth to get proper eh_depth */
1354 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1358 ix
= EXT_FIRST_INDEX(eh
);
1359 block
= ext4_idx_pblock(ix
);
1363 bh
= sb_bread(inode
->i_sb
, block
);
1366 eh
= ext_block_hdr(bh
);
1367 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1371 ex
= EXT_FIRST_EXTENT(eh
);
1373 *logical
= le32_to_cpu(ex
->ee_block
);
1374 *phys
= ext4_ext_pblock(ex
);
1382 * ext4_ext_next_allocated_block:
1383 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1384 * NOTE: it considers block number from index entry as
1385 * allocated block. Thus, index entries have to be consistent
1389 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1393 BUG_ON(path
== NULL
);
1394 depth
= path
->p_depth
;
1396 if (depth
== 0 && path
->p_ext
== NULL
)
1397 return EXT_MAX_BLOCKS
;
1399 while (depth
>= 0) {
1400 if (depth
== path
->p_depth
) {
1402 if (path
[depth
].p_ext
!=
1403 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1404 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1407 if (path
[depth
].p_idx
!=
1408 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1409 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1414 return EXT_MAX_BLOCKS
;
1418 * ext4_ext_next_leaf_block:
1419 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1421 static ext4_lblk_t
ext4_ext_next_leaf_block(struct ext4_ext_path
*path
)
1425 BUG_ON(path
== NULL
);
1426 depth
= path
->p_depth
;
1428 /* zero-tree has no leaf blocks at all */
1430 return EXT_MAX_BLOCKS
;
1432 /* go to index block */
1435 while (depth
>= 0) {
1436 if (path
[depth
].p_idx
!=
1437 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1438 return (ext4_lblk_t
)
1439 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1443 return EXT_MAX_BLOCKS
;
1447 * ext4_ext_correct_indexes:
1448 * if leaf gets modified and modified extent is first in the leaf,
1449 * then we have to correct all indexes above.
1450 * TODO: do we need to correct tree in all cases?
1452 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1453 struct ext4_ext_path
*path
)
1455 struct ext4_extent_header
*eh
;
1456 int depth
= ext_depth(inode
);
1457 struct ext4_extent
*ex
;
1461 eh
= path
[depth
].p_hdr
;
1462 ex
= path
[depth
].p_ext
;
1464 if (unlikely(ex
== NULL
|| eh
== NULL
)) {
1465 EXT4_ERROR_INODE(inode
,
1466 "ex %p == NULL or eh %p == NULL", ex
, eh
);
1471 /* there is no tree at all */
1475 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1476 /* we correct tree if first leaf got modified only */
1481 * TODO: we need correction if border is smaller than current one
1484 border
= path
[depth
].p_ext
->ee_block
;
1485 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1488 path
[k
].p_idx
->ei_block
= border
;
1489 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1494 /* change all left-side indexes */
1495 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1497 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1500 path
[k
].p_idx
->ei_block
= border
;
1501 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1510 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1511 struct ext4_extent
*ex2
)
1513 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1516 * Make sure that either both extents are uninitialized, or
1519 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1522 if (ext4_ext_is_uninitialized(ex1
))
1523 max_len
= EXT_UNINIT_MAX_LEN
;
1525 max_len
= EXT_INIT_MAX_LEN
;
1527 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1528 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1530 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1531 le32_to_cpu(ex2
->ee_block
))
1535 * To allow future support for preallocated extents to be added
1536 * as an RO_COMPAT feature, refuse to merge to extents if
1537 * this can result in the top bit of ee_len being set.
1539 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1541 #ifdef AGGRESSIVE_TEST
1542 if (ext1_ee_len
>= 4)
1546 if (ext4_ext_pblock(ex1
) + ext1_ee_len
== ext4_ext_pblock(ex2
))
1552 * This function tries to merge the "ex" extent to the next extent in the tree.
1553 * It always tries to merge towards right. If you want to merge towards
1554 * left, pass "ex - 1" as argument instead of "ex".
1555 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1556 * 1 if they got merged.
1558 static int ext4_ext_try_to_merge_right(struct inode
*inode
,
1559 struct ext4_ext_path
*path
,
1560 struct ext4_extent
*ex
)
1562 struct ext4_extent_header
*eh
;
1563 unsigned int depth
, len
;
1565 int uninitialized
= 0;
1567 depth
= ext_depth(inode
);
1568 BUG_ON(path
[depth
].p_hdr
== NULL
);
1569 eh
= path
[depth
].p_hdr
;
1571 while (ex
< EXT_LAST_EXTENT(eh
)) {
1572 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1574 /* merge with next extent! */
1575 if (ext4_ext_is_uninitialized(ex
))
1577 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1578 + ext4_ext_get_actual_len(ex
+ 1));
1580 ext4_ext_mark_uninitialized(ex
);
1582 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1583 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1584 * sizeof(struct ext4_extent
);
1585 memmove(ex
+ 1, ex
+ 2, len
);
1587 le16_add_cpu(&eh
->eh_entries
, -1);
1589 WARN_ON(eh
->eh_entries
== 0);
1590 if (!eh
->eh_entries
)
1591 EXT4_ERROR_INODE(inode
, "eh->eh_entries = 0!");
1598 * This function tries to merge the @ex extent to neighbours in the tree.
1599 * return 1 if merge left else 0.
1601 static int ext4_ext_try_to_merge(struct inode
*inode
,
1602 struct ext4_ext_path
*path
,
1603 struct ext4_extent
*ex
) {
1604 struct ext4_extent_header
*eh
;
1609 depth
= ext_depth(inode
);
1610 BUG_ON(path
[depth
].p_hdr
== NULL
);
1611 eh
= path
[depth
].p_hdr
;
1613 if (ex
> EXT_FIRST_EXTENT(eh
))
1614 merge_done
= ext4_ext_try_to_merge_right(inode
, path
, ex
- 1);
1617 ret
= ext4_ext_try_to_merge_right(inode
, path
, ex
);
1623 * check if a portion of the "newext" extent overlaps with an
1626 * If there is an overlap discovered, it updates the length of the newext
1627 * such that there will be no overlap, and then returns 1.
1628 * If there is no overlap found, it returns 0.
1630 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info
*sbi
,
1631 struct inode
*inode
,
1632 struct ext4_extent
*newext
,
1633 struct ext4_ext_path
*path
)
1636 unsigned int depth
, len1
;
1637 unsigned int ret
= 0;
1639 b1
= le32_to_cpu(newext
->ee_block
);
1640 len1
= ext4_ext_get_actual_len(newext
);
1641 depth
= ext_depth(inode
);
1642 if (!path
[depth
].p_ext
)
1644 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1645 b2
&= ~(sbi
->s_cluster_ratio
- 1);
1648 * get the next allocated block if the extent in the path
1649 * is before the requested block(s)
1652 b2
= ext4_ext_next_allocated_block(path
);
1653 if (b2
== EXT_MAX_BLOCKS
)
1655 b2
&= ~(sbi
->s_cluster_ratio
- 1);
1658 /* check for wrap through zero on extent logical start block*/
1659 if (b1
+ len1
< b1
) {
1660 len1
= EXT_MAX_BLOCKS
- b1
;
1661 newext
->ee_len
= cpu_to_le16(len1
);
1665 /* check for overlap */
1666 if (b1
+ len1
> b2
) {
1667 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1675 * ext4_ext_insert_extent:
1676 * tries to merge requsted extent into the existing extent or
1677 * inserts requested extent as new one into the tree,
1678 * creating new leaf in the no-space case.
1680 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1681 struct ext4_ext_path
*path
,
1682 struct ext4_extent
*newext
, int flag
)
1684 struct ext4_extent_header
*eh
;
1685 struct ext4_extent
*ex
, *fex
;
1686 struct ext4_extent
*nearex
; /* nearest extent */
1687 struct ext4_ext_path
*npath
= NULL
;
1688 int depth
, len
, err
;
1690 unsigned uninitialized
= 0;
1693 if (unlikely(ext4_ext_get_actual_len(newext
) == 0)) {
1694 EXT4_ERROR_INODE(inode
, "ext4_ext_get_actual_len(newext) == 0");
1697 depth
= ext_depth(inode
);
1698 ex
= path
[depth
].p_ext
;
1699 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1700 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1704 /* try to insert block into found extent and return */
1705 if (ex
&& !(flag
& EXT4_GET_BLOCKS_PRE_IO
)
1706 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1707 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1708 ext4_ext_is_uninitialized(newext
),
1709 ext4_ext_get_actual_len(newext
),
1710 le32_to_cpu(ex
->ee_block
),
1711 ext4_ext_is_uninitialized(ex
),
1712 ext4_ext_get_actual_len(ex
),
1713 ext4_ext_pblock(ex
));
1714 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1719 * ext4_can_extents_be_merged should have checked that either
1720 * both extents are uninitialized, or both aren't. Thus we
1721 * need to check only one of them here.
1723 if (ext4_ext_is_uninitialized(ex
))
1725 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1726 + ext4_ext_get_actual_len(newext
));
1728 ext4_ext_mark_uninitialized(ex
);
1729 eh
= path
[depth
].p_hdr
;
1734 depth
= ext_depth(inode
);
1735 eh
= path
[depth
].p_hdr
;
1736 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1739 /* probably next leaf has space for us? */
1740 fex
= EXT_LAST_EXTENT(eh
);
1741 next
= EXT_MAX_BLOCKS
;
1742 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
))
1743 next
= ext4_ext_next_leaf_block(path
);
1744 if (next
!= EXT_MAX_BLOCKS
) {
1745 ext_debug("next leaf block - %d\n", next
);
1746 BUG_ON(npath
!= NULL
);
1747 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1749 return PTR_ERR(npath
);
1750 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1751 eh
= npath
[depth
].p_hdr
;
1752 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1753 ext_debug("next leaf isn't full(%d)\n",
1754 le16_to_cpu(eh
->eh_entries
));
1758 ext_debug("next leaf has no free space(%d,%d)\n",
1759 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1763 * There is no free space in the found leaf.
1764 * We're gonna add a new leaf in the tree.
1766 if (flag
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
)
1767 flags
= EXT4_MB_USE_ROOT_BLOCKS
;
1768 err
= ext4_ext_create_new_leaf(handle
, inode
, flags
, path
, newext
);
1771 depth
= ext_depth(inode
);
1772 eh
= path
[depth
].p_hdr
;
1775 nearex
= path
[depth
].p_ext
;
1777 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1782 /* there is no extent in this leaf, create first one */
1783 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1784 le32_to_cpu(newext
->ee_block
),
1785 ext4_ext_pblock(newext
),
1786 ext4_ext_is_uninitialized(newext
),
1787 ext4_ext_get_actual_len(newext
));
1788 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1789 } else if (le32_to_cpu(newext
->ee_block
)
1790 > le32_to_cpu(nearex
->ee_block
)) {
1791 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1792 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1793 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1794 len
= (len
- 1) * sizeof(struct ext4_extent
);
1795 len
= len
< 0 ? 0 : len
;
1796 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1797 "move %d from 0x%p to 0x%p\n",
1798 le32_to_cpu(newext
->ee_block
),
1799 ext4_ext_pblock(newext
),
1800 ext4_ext_is_uninitialized(newext
),
1801 ext4_ext_get_actual_len(newext
),
1802 nearex
, len
, nearex
+ 1, nearex
+ 2);
1803 memmove(nearex
+ 2, nearex
+ 1, len
);
1805 path
[depth
].p_ext
= nearex
+ 1;
1807 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1808 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1809 len
= len
< 0 ? 0 : len
;
1810 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1811 "move %d from 0x%p to 0x%p\n",
1812 le32_to_cpu(newext
->ee_block
),
1813 ext4_ext_pblock(newext
),
1814 ext4_ext_is_uninitialized(newext
),
1815 ext4_ext_get_actual_len(newext
),
1816 nearex
, len
, nearex
, nearex
+ 1);
1817 memmove(nearex
+ 1, nearex
, len
);
1818 path
[depth
].p_ext
= nearex
;
1821 le16_add_cpu(&eh
->eh_entries
, 1);
1822 nearex
= path
[depth
].p_ext
;
1823 nearex
->ee_block
= newext
->ee_block
;
1824 ext4_ext_store_pblock(nearex
, ext4_ext_pblock(newext
));
1825 nearex
->ee_len
= newext
->ee_len
;
1828 /* try to merge extents to the right */
1829 if (!(flag
& EXT4_GET_BLOCKS_PRE_IO
))
1830 ext4_ext_try_to_merge(inode
, path
, nearex
);
1832 /* try to merge extents to the left */
1834 /* time to correct all indexes above */
1835 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1839 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1843 ext4_ext_drop_refs(npath
);
1846 ext4_ext_invalidate_cache(inode
);
1850 static int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1851 ext4_lblk_t num
, ext_prepare_callback func
,
1854 struct ext4_ext_path
*path
= NULL
;
1855 struct ext4_ext_cache cbex
;
1856 struct ext4_extent
*ex
;
1857 ext4_lblk_t next
, start
= 0, end
= 0;
1858 ext4_lblk_t last
= block
+ num
;
1859 int depth
, exists
, err
= 0;
1861 BUG_ON(func
== NULL
);
1862 BUG_ON(inode
== NULL
);
1864 while (block
< last
&& block
!= EXT_MAX_BLOCKS
) {
1866 /* find extent for this block */
1867 down_read(&EXT4_I(inode
)->i_data_sem
);
1868 path
= ext4_ext_find_extent(inode
, block
, path
);
1869 up_read(&EXT4_I(inode
)->i_data_sem
);
1871 err
= PTR_ERR(path
);
1876 depth
= ext_depth(inode
);
1877 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1878 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1882 ex
= path
[depth
].p_ext
;
1883 next
= ext4_ext_next_allocated_block(path
);
1887 /* there is no extent yet, so try to allocate
1888 * all requested space */
1891 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1892 /* need to allocate space before found extent */
1894 end
= le32_to_cpu(ex
->ee_block
);
1895 if (block
+ num
< end
)
1897 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1898 + ext4_ext_get_actual_len(ex
)) {
1899 /* need to allocate space after found extent */
1904 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1906 * some part of requested space is covered
1910 end
= le32_to_cpu(ex
->ee_block
)
1911 + ext4_ext_get_actual_len(ex
);
1912 if (block
+ num
< end
)
1918 BUG_ON(end
<= start
);
1921 cbex
.ec_block
= start
;
1922 cbex
.ec_len
= end
- start
;
1925 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1926 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1927 cbex
.ec_start
= ext4_ext_pblock(ex
);
1930 if (unlikely(cbex
.ec_len
== 0)) {
1931 EXT4_ERROR_INODE(inode
, "cbex.ec_len == 0");
1935 err
= func(inode
, next
, &cbex
, ex
, cbdata
);
1936 ext4_ext_drop_refs(path
);
1941 if (err
== EXT_REPEAT
)
1943 else if (err
== EXT_BREAK
) {
1948 if (ext_depth(inode
) != depth
) {
1949 /* depth was changed. we have to realloc path */
1954 block
= cbex
.ec_block
+ cbex
.ec_len
;
1958 ext4_ext_drop_refs(path
);
1966 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1967 __u32 len
, ext4_fsblk_t start
)
1969 struct ext4_ext_cache
*cex
;
1971 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1972 cex
= &EXT4_I(inode
)->i_cached_extent
;
1973 cex
->ec_block
= block
;
1975 cex
->ec_start
= start
;
1976 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1980 * ext4_ext_put_gap_in_cache:
1981 * calculate boundaries of the gap that the requested block fits into
1982 * and cache this gap
1985 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1988 int depth
= ext_depth(inode
);
1991 struct ext4_extent
*ex
;
1993 ex
= path
[depth
].p_ext
;
1995 /* there is no extent yet, so gap is [0;-] */
1997 len
= EXT_MAX_BLOCKS
;
1998 ext_debug("cache gap(whole file):");
1999 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
2001 len
= le32_to_cpu(ex
->ee_block
) - block
;
2002 ext_debug("cache gap(before): %u [%u:%u]",
2004 le32_to_cpu(ex
->ee_block
),
2005 ext4_ext_get_actual_len(ex
));
2006 } else if (block
>= le32_to_cpu(ex
->ee_block
)
2007 + ext4_ext_get_actual_len(ex
)) {
2009 lblock
= le32_to_cpu(ex
->ee_block
)
2010 + ext4_ext_get_actual_len(ex
);
2012 next
= ext4_ext_next_allocated_block(path
);
2013 ext_debug("cache gap(after): [%u:%u] %u",
2014 le32_to_cpu(ex
->ee_block
),
2015 ext4_ext_get_actual_len(ex
),
2017 BUG_ON(next
== lblock
);
2018 len
= next
- lblock
;
2024 ext_debug(" -> %u:%lu\n", lblock
, len
);
2025 ext4_ext_put_in_cache(inode
, lblock
, len
, 0);
2029 * ext4_ext_check_cache()
2030 * Checks to see if the given block is in the cache.
2031 * If it is, the cached extent is stored in the given
2032 * cache extent pointer. If the cached extent is a hole,
2033 * this routine should be used instead of
2034 * ext4_ext_in_cache if the calling function needs to
2035 * know the size of the hole.
2037 * @inode: The files inode
2038 * @block: The block to look for in the cache
2039 * @ex: Pointer where the cached extent will be stored
2040 * if it contains block
2042 * Return 0 if cache is invalid; 1 if the cache is valid
2044 static int ext4_ext_check_cache(struct inode
*inode
, ext4_lblk_t block
,
2045 struct ext4_ext_cache
*ex
){
2046 struct ext4_ext_cache
*cex
;
2047 struct ext4_sb_info
*sbi
;
2051 * We borrow i_block_reservation_lock to protect i_cached_extent
2053 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
2054 cex
= &EXT4_I(inode
)->i_cached_extent
;
2055 sbi
= EXT4_SB(inode
->i_sb
);
2057 /* has cache valid data? */
2058 if (cex
->ec_len
== 0)
2061 if (in_range(block
, cex
->ec_block
, cex
->ec_len
)) {
2062 memcpy(ex
, cex
, sizeof(struct ext4_ext_cache
));
2063 ext_debug("%u cached by %u:%u:%llu\n",
2065 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
2070 sbi
->extent_cache_misses
++;
2072 sbi
->extent_cache_hits
++;
2073 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
2078 * ext4_ext_in_cache()
2079 * Checks to see if the given block is in the cache.
2080 * If it is, the cached extent is stored in the given
2083 * @inode: The files inode
2084 * @block: The block to look for in the cache
2085 * @ex: Pointer where the cached extent will be stored
2086 * if it contains block
2088 * Return 0 if cache is invalid; 1 if the cache is valid
2091 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
2092 struct ext4_extent
*ex
)
2094 struct ext4_ext_cache cex
;
2097 if (ext4_ext_check_cache(inode
, block
, &cex
)) {
2098 ex
->ee_block
= cpu_to_le32(cex
.ec_block
);
2099 ext4_ext_store_pblock(ex
, cex
.ec_start
);
2100 ex
->ee_len
= cpu_to_le16(cex
.ec_len
);
2110 * removes index from the index block.
2112 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
2113 struct ext4_ext_path
*path
)
2118 /* free index block */
2120 leaf
= ext4_idx_pblock(path
->p_idx
);
2121 if (unlikely(path
->p_hdr
->eh_entries
== 0)) {
2122 EXT4_ERROR_INODE(inode
, "path->p_hdr->eh_entries == 0");
2125 err
= ext4_ext_get_access(handle
, inode
, path
);
2129 if (path
->p_idx
!= EXT_LAST_INDEX(path
->p_hdr
)) {
2130 int len
= EXT_LAST_INDEX(path
->p_hdr
) - path
->p_idx
;
2131 len
*= sizeof(struct ext4_extent_idx
);
2132 memmove(path
->p_idx
, path
->p_idx
+ 1, len
);
2135 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
2136 err
= ext4_ext_dirty(handle
, inode
, path
);
2139 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
2140 ext4_free_blocks(handle
, inode
, NULL
, leaf
, 1,
2141 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
2146 * ext4_ext_calc_credits_for_single_extent:
2147 * This routine returns max. credits that needed to insert an extent
2148 * to the extent tree.
2149 * When pass the actual path, the caller should calculate credits
2152 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2153 struct ext4_ext_path
*path
)
2156 int depth
= ext_depth(inode
);
2159 /* probably there is space in leaf? */
2160 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2161 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2164 * There are some space in the leaf tree, no
2165 * need to account for leaf block credit
2167 * bitmaps and block group descriptor blocks
2168 * and other metadat blocks still need to be
2171 /* 1 bitmap, 1 block group descriptor */
2172 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2177 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2181 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2183 * if nrblocks are fit in a single extent (chunk flag is 1), then
2184 * in the worse case, each tree level index/leaf need to be changed
2185 * if the tree split due to insert a new extent, then the old tree
2186 * index/leaf need to be updated too
2188 * If the nrblocks are discontiguous, they could cause
2189 * the whole tree split more than once, but this is really rare.
2191 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2194 int depth
= ext_depth(inode
);
2204 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2205 struct ext4_extent
*ex
,
2206 ext4_lblk_t from
, ext4_lblk_t to
)
2208 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2209 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2211 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2212 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2213 #ifdef EXTENTS_STATS
2215 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2216 spin_lock(&sbi
->s_ext_stats_lock
);
2217 sbi
->s_ext_blocks
+= ee_len
;
2218 sbi
->s_ext_extents
++;
2219 if (ee_len
< sbi
->s_ext_min
)
2220 sbi
->s_ext_min
= ee_len
;
2221 if (ee_len
> sbi
->s_ext_max
)
2222 sbi
->s_ext_max
= ee_len
;
2223 if (ext_depth(inode
) > sbi
->s_depth_max
)
2224 sbi
->s_depth_max
= ext_depth(inode
);
2225 spin_unlock(&sbi
->s_ext_stats_lock
);
2228 if (from
>= le32_to_cpu(ex
->ee_block
)
2229 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2234 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2235 start
= ext4_ext_pblock(ex
) + ee_len
- num
;
2236 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2237 ext4_free_blocks(handle
, inode
, NULL
, start
, num
, flags
);
2238 } else if (from
== le32_to_cpu(ex
->ee_block
)
2239 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2245 start
= ext4_ext_pblock(ex
);
2247 ext_debug("free first %u blocks starting %llu\n", num
, start
);
2248 ext4_free_blocks(handle
, inode
, 0, start
, num
, flags
);
2251 printk(KERN_INFO
"strange request: removal(2) "
2252 "%u-%u from %u:%u\n",
2253 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2260 * ext4_ext_rm_leaf() Removes the extents associated with the
2261 * blocks appearing between "start" and "end", and splits the extents
2262 * if "start" and "end" appear in the same extent
2264 * @handle: The journal handle
2265 * @inode: The files inode
2266 * @path: The path to the leaf
2267 * @start: The first block to remove
2268 * @end: The last block to remove
2271 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2272 struct ext4_ext_path
*path
, ext4_lblk_t start
,
2275 int err
= 0, correct_index
= 0;
2276 int depth
= ext_depth(inode
), credits
;
2277 struct ext4_extent_header
*eh
;
2278 ext4_lblk_t a
, b
, block
;
2280 ext4_lblk_t ex_ee_block
;
2281 unsigned short ex_ee_len
;
2282 unsigned uninitialized
= 0;
2283 struct ext4_extent
*ex
;
2284 struct ext4_map_blocks map
;
2286 /* the header must be checked already in ext4_ext_remove_space() */
2287 ext_debug("truncate since %u in leaf\n", start
);
2288 if (!path
[depth
].p_hdr
)
2289 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2290 eh
= path
[depth
].p_hdr
;
2291 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2292 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2295 /* find where to start removing */
2296 ex
= EXT_LAST_EXTENT(eh
);
2298 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2299 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2301 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2302 ex_ee_block
+ ex_ee_len
> start
) {
2304 if (ext4_ext_is_uninitialized(ex
))
2309 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2310 uninitialized
, ex_ee_len
);
2311 path
[depth
].p_ext
= ex
;
2313 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2314 b
= ex_ee_block
+ex_ee_len
- 1 < end
?
2315 ex_ee_block
+ex_ee_len
- 1 : end
;
2317 ext_debug(" border %u:%u\n", a
, b
);
2319 /* If this extent is beyond the end of the hole, skip it */
2320 if (end
<= ex_ee_block
) {
2322 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2323 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2325 } else if (a
!= ex_ee_block
&&
2326 b
!= ex_ee_block
+ ex_ee_len
- 1) {
2328 * If this is a truncate, then this condition should
2329 * never happen because at least one of the end points
2330 * needs to be on the edge of the extent.
2332 if (end
== EXT_MAX_BLOCKS
- 1) {
2333 ext_debug(" bad truncate %u:%u\n",
2341 * else this is a hole punch, so the extent needs to
2342 * be split since neither edge of the hole is on the
2346 map
.m_pblk
= ext4_ext_pblock(ex
);
2347 map
.m_lblk
= ex_ee_block
;
2348 map
.m_len
= b
- ex_ee_block
;
2350 err
= ext4_split_extent(handle
,
2351 inode
, path
, &map
, 0,
2352 EXT4_GET_BLOCKS_PUNCH_OUT_EXT
|
2353 EXT4_GET_BLOCKS_PRE_IO
);
2358 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2360 b
= ex_ee_block
+ex_ee_len
- 1 < end
?
2361 ex_ee_block
+ex_ee_len
- 1 : end
;
2363 /* Then remove tail of this extent */
2364 block
= ex_ee_block
;
2367 } else if (a
!= ex_ee_block
) {
2368 /* remove tail of the extent */
2369 block
= ex_ee_block
;
2371 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2372 /* remove head of the extent */
2374 num
= ex_ee_block
+ ex_ee_len
- b
;
2377 * If this is a truncate, this condition
2378 * should never happen
2380 if (end
== EXT_MAX_BLOCKS
- 1) {
2381 ext_debug(" bad truncate %u:%u\n",
2387 /* remove whole extent: excellent! */
2388 block
= ex_ee_block
;
2390 if (a
!= ex_ee_block
) {
2391 ext_debug(" bad truncate %u:%u\n",
2397 if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2398 ext_debug(" bad truncate %u:%u\n",
2406 * 3 for leaf, sb, and inode plus 2 (bmap and group
2407 * descriptor) for each block group; assume two block
2408 * groups plus ex_ee_len/blocks_per_block_group for
2411 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2412 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2414 credits
+= (ext_depth(inode
)) + 1;
2416 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2418 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2422 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2426 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2431 /* this extent is removed; mark slot entirely unused */
2432 ext4_ext_store_pblock(ex
, 0);
2433 } else if (block
!= ex_ee_block
) {
2435 * If this was a head removal, then we need to update
2436 * the physical block since it is now at a different
2439 ext4_ext_store_pblock(ex
, ext4_ext_pblock(ex
) + (b
-a
));
2442 ex
->ee_block
= cpu_to_le32(block
);
2443 ex
->ee_len
= cpu_to_le16(num
);
2445 * Do not mark uninitialized if all the blocks in the
2446 * extent have been removed.
2448 if (uninitialized
&& num
)
2449 ext4_ext_mark_uninitialized(ex
);
2451 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2456 * If the extent was completely released,
2457 * we need to remove it from the leaf
2460 if (end
!= EXT_MAX_BLOCKS
- 1) {
2462 * For hole punching, we need to scoot all the
2463 * extents up when an extent is removed so that
2464 * we dont have blank extents in the middle
2466 memmove(ex
, ex
+1, (EXT_LAST_EXTENT(eh
) - ex
) *
2467 sizeof(struct ext4_extent
));
2469 /* Now get rid of the one at the end */
2470 memset(EXT_LAST_EXTENT(eh
), 0,
2471 sizeof(struct ext4_extent
));
2473 le16_add_cpu(&eh
->eh_entries
, -1);
2476 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2477 ext4_ext_pblock(ex
));
2479 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2480 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2483 if (correct_index
&& eh
->eh_entries
)
2484 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2486 /* if this leaf is free, then we should
2487 * remove it from index block above */
2488 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2489 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2496 * ext4_ext_more_to_rm:
2497 * returns 1 if current index has to be freed (even partial)
2500 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2502 BUG_ON(path
->p_idx
== NULL
);
2504 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2508 * if truncate on deeper level happened, it wasn't partial,
2509 * so we have to consider current index for truncation
2511 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2516 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2518 struct super_block
*sb
= inode
->i_sb
;
2519 int depth
= ext_depth(inode
);
2520 struct ext4_ext_path
*path
;
2524 ext_debug("truncate since %u\n", start
);
2526 /* probably first extent we're gonna free will be last in block */
2527 handle
= ext4_journal_start(inode
, depth
+ 1);
2529 return PTR_ERR(handle
);
2532 ext4_ext_invalidate_cache(inode
);
2535 * We start scanning from right side, freeing all the blocks
2536 * after i_size and walking into the tree depth-wise.
2538 depth
= ext_depth(inode
);
2539 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2541 ext4_journal_stop(handle
);
2544 path
[0].p_depth
= depth
;
2545 path
[0].p_hdr
= ext_inode_hdr(inode
);
2546 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2552 while (i
>= 0 && err
== 0) {
2554 /* this is leaf block */
2555 err
= ext4_ext_rm_leaf(handle
, inode
, path
,
2556 start
, EXT_MAX_BLOCKS
- 1);
2557 /* root level has p_bh == NULL, brelse() eats this */
2558 brelse(path
[i
].p_bh
);
2559 path
[i
].p_bh
= NULL
;
2564 /* this is index block */
2565 if (!path
[i
].p_hdr
) {
2566 ext_debug("initialize header\n");
2567 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2570 if (!path
[i
].p_idx
) {
2571 /* this level hasn't been touched yet */
2572 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2573 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2574 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2576 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2578 /* we were already here, see at next index */
2582 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2583 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2585 if (ext4_ext_more_to_rm(path
+ i
)) {
2586 struct buffer_head
*bh
;
2587 /* go to the next level */
2588 ext_debug("move to level %d (block %llu)\n",
2589 i
+ 1, ext4_idx_pblock(path
[i
].p_idx
));
2590 memset(path
+ i
+ 1, 0, sizeof(*path
));
2591 bh
= sb_bread(sb
, ext4_idx_pblock(path
[i
].p_idx
));
2593 /* should we reset i_size? */
2597 if (WARN_ON(i
+ 1 > depth
)) {
2601 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2606 path
[i
+ 1].p_bh
= bh
;
2608 /* save actual number of indexes since this
2609 * number is changed at the next iteration */
2610 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2613 /* we finished processing this index, go up */
2614 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2615 /* index is empty, remove it;
2616 * handle must be already prepared by the
2617 * truncatei_leaf() */
2618 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2620 /* root level has p_bh == NULL, brelse() eats this */
2621 brelse(path
[i
].p_bh
);
2622 path
[i
].p_bh
= NULL
;
2624 ext_debug("return to level %d\n", i
);
2628 /* TODO: flexible tree reduction should be here */
2629 if (path
->p_hdr
->eh_entries
== 0) {
2631 * truncate to zero freed all the tree,
2632 * so we need to correct eh_depth
2634 err
= ext4_ext_get_access(handle
, inode
, path
);
2636 ext_inode_hdr(inode
)->eh_depth
= 0;
2637 ext_inode_hdr(inode
)->eh_max
=
2638 cpu_to_le16(ext4_ext_space_root(inode
, 0));
2639 err
= ext4_ext_dirty(handle
, inode
, path
);
2643 ext4_ext_drop_refs(path
);
2647 ext4_journal_stop(handle
);
2653 * called at mount time
2655 void ext4_ext_init(struct super_block
*sb
)
2658 * possible initialization would be here
2661 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2662 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2663 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2664 #ifdef AGGRESSIVE_TEST
2665 printk(", aggressive tests");
2667 #ifdef CHECK_BINSEARCH
2668 printk(", check binsearch");
2670 #ifdef EXTENTS_STATS
2675 #ifdef EXTENTS_STATS
2676 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2677 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2678 EXT4_SB(sb
)->s_ext_max
= 0;
2684 * called at umount time
2686 void ext4_ext_release(struct super_block
*sb
)
2688 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2691 #ifdef EXTENTS_STATS
2692 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2693 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2694 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2695 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2696 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2697 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2698 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2703 /* FIXME!! we need to try to merge to left or right after zero-out */
2704 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2706 ext4_fsblk_t ee_pblock
;
2707 unsigned int ee_len
;
2710 ee_len
= ext4_ext_get_actual_len(ex
);
2711 ee_pblock
= ext4_ext_pblock(ex
);
2713 ret
= sb_issue_zeroout(inode
->i_sb
, ee_pblock
, ee_len
, GFP_NOFS
);
2721 * used by extent splitting.
2723 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2725 #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2726 #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2729 * ext4_split_extent_at() splits an extent at given block.
2731 * @handle: the journal handle
2732 * @inode: the file inode
2733 * @path: the path to the extent
2734 * @split: the logical block where the extent is splitted.
2735 * @split_flags: indicates if the extent could be zeroout if split fails, and
2736 * the states(init or uninit) of new extents.
2737 * @flags: flags used to insert new extent to extent tree.
2740 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2741 * of which are deterimined by split_flag.
2743 * There are two cases:
2744 * a> the extent are splitted into two extent.
2745 * b> split is not needed, and just mark the extent.
2747 * return 0 on success.
2749 static int ext4_split_extent_at(handle_t
*handle
,
2750 struct inode
*inode
,
2751 struct ext4_ext_path
*path
,
2756 ext4_fsblk_t newblock
;
2757 ext4_lblk_t ee_block
;
2758 struct ext4_extent
*ex
, newex
, orig_ex
;
2759 struct ext4_extent
*ex2
= NULL
;
2760 unsigned int ee_len
, depth
;
2763 ext_debug("ext4_split_extents_at: inode %lu, logical"
2764 "block %llu\n", inode
->i_ino
, (unsigned long long)split
);
2766 ext4_ext_show_leaf(inode
, path
);
2768 depth
= ext_depth(inode
);
2769 ex
= path
[depth
].p_ext
;
2770 ee_block
= le32_to_cpu(ex
->ee_block
);
2771 ee_len
= ext4_ext_get_actual_len(ex
);
2772 newblock
= split
- ee_block
+ ext4_ext_pblock(ex
);
2774 BUG_ON(split
< ee_block
|| split
>= (ee_block
+ ee_len
));
2776 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2780 if (split
== ee_block
) {
2782 * case b: block @split is the block that the extent begins with
2783 * then we just change the state of the extent, and splitting
2786 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2787 ext4_ext_mark_uninitialized(ex
);
2789 ext4_ext_mark_initialized(ex
);
2791 if (!(flags
& EXT4_GET_BLOCKS_PRE_IO
))
2792 ext4_ext_try_to_merge(inode
, path
, ex
);
2794 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2799 memcpy(&orig_ex
, ex
, sizeof(orig_ex
));
2800 ex
->ee_len
= cpu_to_le16(split
- ee_block
);
2801 if (split_flag
& EXT4_EXT_MARK_UNINIT1
)
2802 ext4_ext_mark_uninitialized(ex
);
2805 * path may lead to new leaf, not to original leaf any more
2806 * after ext4_ext_insert_extent() returns,
2808 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2810 goto fix_extent_len
;
2813 ex2
->ee_block
= cpu_to_le32(split
);
2814 ex2
->ee_len
= cpu_to_le16(ee_len
- (split
- ee_block
));
2815 ext4_ext_store_pblock(ex2
, newblock
);
2816 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2817 ext4_ext_mark_uninitialized(ex2
);
2819 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
2820 if (err
== -ENOSPC
&& (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2821 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2823 goto fix_extent_len
;
2824 /* update the extent length and mark as initialized */
2825 ex
->ee_len
= cpu_to_le32(ee_len
);
2826 ext4_ext_try_to_merge(inode
, path
, ex
);
2827 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2830 goto fix_extent_len
;
2833 ext4_ext_show_leaf(inode
, path
);
2837 ex
->ee_len
= orig_ex
.ee_len
;
2838 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2843 * ext4_split_extents() splits an extent and mark extent which is covered
2844 * by @map as split_flags indicates
2846 * It may result in splitting the extent into multiple extents (upto three)
2847 * There are three possibilities:
2848 * a> There is no split required
2849 * b> Splits in two extents: Split is happening at either end of the extent
2850 * c> Splits in three extents: Somone is splitting in middle of the extent
2853 static int ext4_split_extent(handle_t
*handle
,
2854 struct inode
*inode
,
2855 struct ext4_ext_path
*path
,
2856 struct ext4_map_blocks
*map
,
2860 ext4_lblk_t ee_block
;
2861 struct ext4_extent
*ex
;
2862 unsigned int ee_len
, depth
;
2865 int split_flag1
, flags1
;
2867 depth
= ext_depth(inode
);
2868 ex
= path
[depth
].p_ext
;
2869 ee_block
= le32_to_cpu(ex
->ee_block
);
2870 ee_len
= ext4_ext_get_actual_len(ex
);
2871 uninitialized
= ext4_ext_is_uninitialized(ex
);
2873 if (map
->m_lblk
+ map
->m_len
< ee_block
+ ee_len
) {
2874 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
?
2875 EXT4_EXT_MAY_ZEROOUT
: 0;
2876 flags1
= flags
| EXT4_GET_BLOCKS_PRE_IO
;
2878 split_flag1
|= EXT4_EXT_MARK_UNINIT1
|
2879 EXT4_EXT_MARK_UNINIT2
;
2880 err
= ext4_split_extent_at(handle
, inode
, path
,
2881 map
->m_lblk
+ map
->m_len
, split_flag1
, flags1
);
2886 ext4_ext_drop_refs(path
);
2887 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, path
);
2889 return PTR_ERR(path
);
2891 if (map
->m_lblk
>= ee_block
) {
2892 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
?
2893 EXT4_EXT_MAY_ZEROOUT
: 0;
2895 split_flag1
|= EXT4_EXT_MARK_UNINIT1
;
2896 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2897 split_flag1
|= EXT4_EXT_MARK_UNINIT2
;
2898 err
= ext4_split_extent_at(handle
, inode
, path
,
2899 map
->m_lblk
, split_flag1
, flags
);
2904 ext4_ext_show_leaf(inode
, path
);
2906 return err
? err
: map
->m_len
;
2909 #define EXT4_EXT_ZERO_LEN 7
2911 * This function is called by ext4_ext_map_blocks() if someone tries to write
2912 * to an uninitialized extent. It may result in splitting the uninitialized
2913 * extent into multiple extents (up to three - one initialized and two
2915 * There are three possibilities:
2916 * a> There is no split required: Entire extent should be initialized
2917 * b> Splits in two extents: Write is happening at either end of the extent
2918 * c> Splits in three extents: Somone is writing in middle of the extent
2920 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2921 struct inode
*inode
,
2922 struct ext4_map_blocks
*map
,
2923 struct ext4_ext_path
*path
)
2925 struct ext4_map_blocks split_map
;
2926 struct ext4_extent zero_ex
;
2927 struct ext4_extent
*ex
;
2928 ext4_lblk_t ee_block
, eof_block
;
2929 unsigned int allocated
, ee_len
, depth
;
2933 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2934 "block %llu, max_blocks %u\n", inode
->i_ino
,
2935 (unsigned long long)map
->m_lblk
, map
->m_len
);
2937 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2938 inode
->i_sb
->s_blocksize_bits
;
2939 if (eof_block
< map
->m_lblk
+ map
->m_len
)
2940 eof_block
= map
->m_lblk
+ map
->m_len
;
2942 depth
= ext_depth(inode
);
2943 ex
= path
[depth
].p_ext
;
2944 ee_block
= le32_to_cpu(ex
->ee_block
);
2945 ee_len
= ext4_ext_get_actual_len(ex
);
2946 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
2948 WARN_ON(map
->m_lblk
< ee_block
);
2950 * It is safe to convert extent to initialized via explicit
2951 * zeroout only if extent is fully insde i_size or new_size.
2953 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
2955 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2956 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
&&
2957 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2958 err
= ext4_ext_zeroout(inode
, ex
);
2962 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2965 ext4_ext_mark_initialized(ex
);
2966 ext4_ext_try_to_merge(inode
, path
, ex
);
2967 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2973 * 1. split the extent into three extents.
2974 * 2. split the extent into two extents, zeroout the first half.
2975 * 3. split the extent into two extents, zeroout the second half.
2976 * 4. split the extent into two extents with out zeroout.
2978 split_map
.m_lblk
= map
->m_lblk
;
2979 split_map
.m_len
= map
->m_len
;
2981 if (allocated
> map
->m_len
) {
2982 if (allocated
<= EXT4_EXT_ZERO_LEN
&&
2983 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2986 cpu_to_le32(map
->m_lblk
);
2987 zero_ex
.ee_len
= cpu_to_le16(allocated
);
2988 ext4_ext_store_pblock(&zero_ex
,
2989 ext4_ext_pblock(ex
) + map
->m_lblk
- ee_block
);
2990 err
= ext4_ext_zeroout(inode
, &zero_ex
);
2993 split_map
.m_lblk
= map
->m_lblk
;
2994 split_map
.m_len
= allocated
;
2995 } else if ((map
->m_lblk
- ee_block
+ map
->m_len
<
2996 EXT4_EXT_ZERO_LEN
) &&
2997 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2999 if (map
->m_lblk
!= ee_block
) {
3000 zero_ex
.ee_block
= ex
->ee_block
;
3001 zero_ex
.ee_len
= cpu_to_le16(map
->m_lblk
-
3003 ext4_ext_store_pblock(&zero_ex
,
3004 ext4_ext_pblock(ex
));
3005 err
= ext4_ext_zeroout(inode
, &zero_ex
);
3010 split_map
.m_lblk
= ee_block
;
3011 split_map
.m_len
= map
->m_lblk
- ee_block
+ map
->m_len
;
3012 allocated
= map
->m_len
;
3016 allocated
= ext4_split_extent(handle
, inode
, path
,
3017 &split_map
, split_flag
, 0);
3022 return err
? err
: allocated
;
3026 * This function is called by ext4_ext_map_blocks() from
3027 * ext4_get_blocks_dio_write() when DIO to write
3028 * to an uninitialized extent.
3030 * Writing to an uninitialized extent may result in splitting the uninitialized
3031 * extent into multiple /initialized uninitialized extents (up to three)
3032 * There are three possibilities:
3033 * a> There is no split required: Entire extent should be uninitialized
3034 * b> Splits in two extents: Write is happening at either end of the extent
3035 * c> Splits in three extents: Somone is writing in middle of the extent
3037 * One of more index blocks maybe needed if the extent tree grow after
3038 * the uninitialized extent split. To prevent ENOSPC occur at the IO
3039 * complete, we need to split the uninitialized extent before DIO submit
3040 * the IO. The uninitialized extent called at this time will be split
3041 * into three uninitialized extent(at most). After IO complete, the part
3042 * being filled will be convert to initialized by the end_io callback function
3043 * via ext4_convert_unwritten_extents().
3045 * Returns the size of uninitialized extent to be written on success.
3047 static int ext4_split_unwritten_extents(handle_t
*handle
,
3048 struct inode
*inode
,
3049 struct ext4_map_blocks
*map
,
3050 struct ext4_ext_path
*path
,
3053 ext4_lblk_t eof_block
;
3054 ext4_lblk_t ee_block
;
3055 struct ext4_extent
*ex
;
3056 unsigned int ee_len
;
3057 int split_flag
= 0, depth
;
3059 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3060 "block %llu, max_blocks %u\n", inode
->i_ino
,
3061 (unsigned long long)map
->m_lblk
, map
->m_len
);
3063 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
3064 inode
->i_sb
->s_blocksize_bits
;
3065 if (eof_block
< map
->m_lblk
+ map
->m_len
)
3066 eof_block
= map
->m_lblk
+ map
->m_len
;
3068 * It is safe to convert extent to initialized via explicit
3069 * zeroout only if extent is fully insde i_size or new_size.
3071 depth
= ext_depth(inode
);
3072 ex
= path
[depth
].p_ext
;
3073 ee_block
= le32_to_cpu(ex
->ee_block
);
3074 ee_len
= ext4_ext_get_actual_len(ex
);
3076 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
3077 split_flag
|= EXT4_EXT_MARK_UNINIT2
;
3079 flags
|= EXT4_GET_BLOCKS_PRE_IO
;
3080 return ext4_split_extent(handle
, inode
, path
, map
, split_flag
, flags
);
3083 static int ext4_convert_unwritten_extents_endio(handle_t
*handle
,
3084 struct inode
*inode
,
3085 struct ext4_ext_path
*path
)
3087 struct ext4_extent
*ex
;
3091 depth
= ext_depth(inode
);
3092 ex
= path
[depth
].p_ext
;
3094 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3095 "block %llu, max_blocks %u\n", inode
->i_ino
,
3096 (unsigned long long)le32_to_cpu(ex
->ee_block
),
3097 ext4_ext_get_actual_len(ex
));
3099 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3102 /* first mark the extent as initialized */
3103 ext4_ext_mark_initialized(ex
);
3105 /* note: ext4_ext_correct_indexes() isn't needed here because
3106 * borders are not changed
3108 ext4_ext_try_to_merge(inode
, path
, ex
);
3110 /* Mark modified extent as dirty */
3111 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3113 ext4_ext_show_leaf(inode
, path
);
3117 static void unmap_underlying_metadata_blocks(struct block_device
*bdev
,
3118 sector_t block
, int count
)
3121 for (i
= 0; i
< count
; i
++)
3122 unmap_underlying_metadata(bdev
, block
+ i
);
3126 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3128 static int check_eofblocks_fl(handle_t
*handle
, struct inode
*inode
,
3130 struct ext4_ext_path
*path
,
3134 struct ext4_extent_header
*eh
;
3135 struct ext4_extent
*last_ex
;
3137 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))
3140 depth
= ext_depth(inode
);
3141 eh
= path
[depth
].p_hdr
;
3143 if (unlikely(!eh
->eh_entries
)) {
3144 EXT4_ERROR_INODE(inode
, "eh->eh_entries == 0 and "
3145 "EOFBLOCKS_FL set");
3148 last_ex
= EXT_LAST_EXTENT(eh
);
3150 * We should clear the EOFBLOCKS_FL flag if we are writing the
3151 * last block in the last extent in the file. We test this by
3152 * first checking to see if the caller to
3153 * ext4_ext_get_blocks() was interested in the last block (or
3154 * a block beyond the last block) in the current extent. If
3155 * this turns out to be false, we can bail out from this
3156 * function immediately.
3158 if (lblk
+ len
< le32_to_cpu(last_ex
->ee_block
) +
3159 ext4_ext_get_actual_len(last_ex
))
3162 * If the caller does appear to be planning to write at or
3163 * beyond the end of the current extent, we then test to see
3164 * if the current extent is the last extent in the file, by
3165 * checking to make sure it was reached via the rightmost node
3166 * at each level of the tree.
3168 for (i
= depth
-1; i
>= 0; i
--)
3169 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3171 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3172 return ext4_mark_inode_dirty(handle
, inode
);
3176 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3177 struct ext4_map_blocks
*map
,
3178 struct ext4_ext_path
*path
, int flags
,
3179 unsigned int allocated
, ext4_fsblk_t newblock
)
3183 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3185 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3186 "block %llu, max_blocks %u, flags %d, allocated %u",
3187 inode
->i_ino
, (unsigned long long)map
->m_lblk
, map
->m_len
,
3189 ext4_ext_show_leaf(inode
, path
);
3191 /* get_block() before submit the IO, split the extent */
3192 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3193 ret
= ext4_split_unwritten_extents(handle
, inode
, map
,
3196 * Flag the inode(non aio case) or end_io struct (aio case)
3197 * that this IO needs to conversion to written when IO is
3200 if (io
&& !(io
->flag
& EXT4_IO_END_UNWRITTEN
)) {
3201 io
->flag
= EXT4_IO_END_UNWRITTEN
;
3202 atomic_inc(&EXT4_I(inode
)->i_aiodio_unwritten
);
3204 ext4_set_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3205 if (ext4_should_dioread_nolock(inode
))
3206 map
->m_flags
|= EXT4_MAP_UNINIT
;
3209 /* IO end_io complete, convert the filled extent to written */
3210 if ((flags
& EXT4_GET_BLOCKS_CONVERT
)) {
3211 ret
= ext4_convert_unwritten_extents_endio(handle
, inode
,
3214 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3215 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
3221 /* buffered IO case */
3223 * repeat fallocate creation request
3224 * we already have an unwritten extent
3226 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3229 /* buffered READ or buffered write_begin() lookup */
3230 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3232 * We have blocks reserved already. We
3233 * return allocated blocks so that delalloc
3234 * won't do block reservation for us. But
3235 * the buffer head will be unmapped so that
3236 * a read from the block returns 0s.
3238 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
3242 /* buffered write, writepage time, convert*/
3243 ret
= ext4_ext_convert_to_initialized(handle
, inode
, map
, path
);
3245 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3246 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
,
3258 map
->m_flags
|= EXT4_MAP_NEW
;
3260 * if we allocated more blocks than requested
3261 * we need to make sure we unmap the extra block
3262 * allocated. The actual needed block will get
3263 * unmapped later when we find the buffer_head marked
3266 if (allocated
> map
->m_len
) {
3267 unmap_underlying_metadata_blocks(inode
->i_sb
->s_bdev
,
3268 newblock
+ map
->m_len
,
3269 allocated
- map
->m_len
);
3270 allocated
= map
->m_len
;
3274 * If we have done fallocate with the offset that is already
3275 * delayed allocated, we would have block reservation
3276 * and quota reservation done in the delayed write path.
3277 * But fallocate would have already updated quota and block
3278 * count for this offset. So cancel these reservation
3280 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3281 ext4_da_update_reserve_space(inode
, allocated
, 0);
3284 map
->m_flags
|= EXT4_MAP_MAPPED
;
3286 if (allocated
> map
->m_len
)
3287 allocated
= map
->m_len
;
3288 ext4_ext_show_leaf(inode
, path
);
3289 map
->m_pblk
= newblock
;
3290 map
->m_len
= allocated
;
3293 ext4_ext_drop_refs(path
);
3296 return err
? err
: allocated
;
3300 * get_implied_cluster_alloc - check to see if the requested
3301 * allocation (in the map structure) overlaps with a cluster already
3302 * allocated in an extent.
3303 * @sbi The ext4-specific superblock structure
3304 * @map The requested lblk->pblk mapping
3305 * @ex The extent structure which might contain an implied
3306 * cluster allocation
3308 * This function is called by ext4_ext_map_blocks() after we failed to
3309 * find blocks that were already in the inode's extent tree. Hence,
3310 * we know that the beginning of the requested region cannot overlap
3311 * the extent from the inode's extent tree. There are three cases we
3312 * want to catch. The first is this case:
3314 * |--- cluster # N--|
3315 * |--- extent ---| |---- requested region ---|
3318 * The second case that we need to test for is this one:
3320 * |--------- cluster # N ----------------|
3321 * |--- requested region --| |------- extent ----|
3322 * |=======================|
3324 * The third case is when the requested region lies between two extents
3325 * within the same cluster:
3326 * |------------- cluster # N-------------|
3327 * |----- ex -----| |---- ex_right ----|
3328 * |------ requested region ------|
3329 * |================|
3331 * In each of the above cases, we need to set the map->m_pblk and
3332 * map->m_len so it corresponds to the return the extent labelled as
3333 * "|====|" from cluster #N, since it is already in use for data in
3334 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3335 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3336 * as a new "allocated" block region. Otherwise, we will return 0 and
3337 * ext4_ext_map_blocks() will then allocate one or more new clusters
3338 * by calling ext4_mb_new_blocks().
3340 static int get_implied_cluster_alloc(struct ext4_sb_info
*sbi
,
3341 struct ext4_map_blocks
*map
,
3342 struct ext4_extent
*ex
,
3343 struct ext4_ext_path
*path
)
3345 ext4_lblk_t c_offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
-1);
3346 ext4_lblk_t ex_cluster_start
, ex_cluster_end
;
3347 ext4_lblk_t rr_cluster_start
, rr_cluster_end
;
3348 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3349 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3350 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
3352 /* The extent passed in that we are trying to match */
3353 ex_cluster_start
= EXT4_B2C(sbi
, ee_block
);
3354 ex_cluster_end
= EXT4_B2C(sbi
, ee_block
+ ee_len
- 1);
3356 /* The requested region passed into ext4_map_blocks() */
3357 rr_cluster_start
= EXT4_B2C(sbi
, map
->m_lblk
);
3358 rr_cluster_end
= EXT4_B2C(sbi
, map
->m_lblk
+ map
->m_len
- 1);
3360 if ((rr_cluster_start
== ex_cluster_end
) ||
3361 (rr_cluster_start
== ex_cluster_start
)) {
3362 if (rr_cluster_start
== ex_cluster_end
)
3363 ee_start
+= ee_len
- 1;
3364 map
->m_pblk
= (ee_start
& ~(sbi
->s_cluster_ratio
- 1)) +
3366 map
->m_len
= min(map
->m_len
,
3367 (unsigned) sbi
->s_cluster_ratio
- c_offset
);
3369 * Check for and handle this case:
3371 * |--------- cluster # N-------------|
3372 * |------- extent ----|
3373 * |--- requested region ---|
3377 if (map
->m_lblk
< ee_block
)
3378 map
->m_len
= min(map
->m_len
, ee_block
- map
->m_lblk
);
3381 * Check for the case where there is already another allocated
3382 * block to the right of 'ex' but before the end of the cluster.
3384 * |------------- cluster # N-------------|
3385 * |----- ex -----| |---- ex_right ----|
3386 * |------ requested region ------|
3387 * |================|
3389 if (map
->m_lblk
> ee_block
) {
3390 ext4_lblk_t next
= ext4_ext_next_allocated_block(path
);
3391 map
->m_len
= min(map
->m_len
, next
- map
->m_lblk
);
3400 * Block allocation/map/preallocation routine for extents based files
3403 * Need to be called with
3404 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3405 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3407 * return > 0, number of of blocks already mapped/allocated
3408 * if create == 0 and these are pre-allocated blocks
3409 * buffer head is unmapped
3410 * otherwise blocks are mapped
3412 * return = 0, if plain look up failed (blocks have not been allocated)
3413 * buffer head is unmapped
3415 * return < 0, error case.
3417 int ext4_ext_map_blocks(handle_t
*handle
, struct inode
*inode
,
3418 struct ext4_map_blocks
*map
, int flags
)
3420 struct ext4_ext_path
*path
= NULL
;
3421 struct ext4_extent newex
, *ex
, *ex2
;
3422 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3423 ext4_fsblk_t newblock
= 0;
3424 int free_on_err
= 0, err
= 0, depth
, ret
;
3425 unsigned int allocated
= 0, offset
= 0;
3426 unsigned int punched_out
= 0;
3427 unsigned int result
= 0;
3428 struct ext4_allocation_request ar
;
3429 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3430 ext4_lblk_t cluster_offset
;
3431 struct ext4_map_blocks punch_map
;
3433 ext_debug("blocks %u/%u requested for inode %lu\n",
3434 map
->m_lblk
, map
->m_len
, inode
->i_ino
);
3435 trace_ext4_ext_map_blocks_enter(inode
, map
->m_lblk
, map
->m_len
, flags
);
3437 /* check in cache */
3438 if (!(flags
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
) &&
3439 ext4_ext_in_cache(inode
, map
->m_lblk
, &newex
)) {
3440 if (!newex
.ee_start_lo
&& !newex
.ee_start_hi
) {
3441 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3443 * block isn't allocated yet and
3444 * user doesn't want to allocate it
3448 /* we should allocate requested block */
3450 /* block is already allocated */
3451 newblock
= map
->m_lblk
3452 - le32_to_cpu(newex
.ee_block
)
3453 + ext4_ext_pblock(&newex
);
3454 /* number of remaining blocks in the extent */
3455 allocated
= ext4_ext_get_actual_len(&newex
) -
3456 (map
->m_lblk
- le32_to_cpu(newex
.ee_block
));
3461 /* find extent for this block */
3462 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, NULL
);
3464 err
= PTR_ERR(path
);
3469 depth
= ext_depth(inode
);
3472 * consistent leaf must not be empty;
3473 * this situation is possible, though, _during_ tree modification;
3474 * this is why assert can't be put in ext4_ext_find_extent()
3476 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
3477 EXT4_ERROR_INODE(inode
, "bad extent address "
3478 "lblock: %lu, depth: %d pblock %lld",
3479 (unsigned long) map
->m_lblk
, depth
,
3480 path
[depth
].p_block
);
3485 ex
= path
[depth
].p_ext
;
3487 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3488 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3489 unsigned short ee_len
;
3492 * Uninitialized extents are treated as holes, except that
3493 * we split out initialized portions during a write.
3495 ee_len
= ext4_ext_get_actual_len(ex
);
3496 /* if found extent covers block, simply return it */
3497 if (in_range(map
->m_lblk
, ee_block
, ee_len
)) {
3498 newblock
= map
->m_lblk
- ee_block
+ ee_start
;
3499 /* number of remaining blocks in the extent */
3500 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3501 ext_debug("%u fit into %u:%d -> %llu\n", map
->m_lblk
,
3502 ee_block
, ee_len
, newblock
);
3504 if ((flags
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
) == 0) {
3506 * Do not put uninitialized extent
3509 if (!ext4_ext_is_uninitialized(ex
)) {
3510 ext4_ext_put_in_cache(inode
, ee_block
,
3514 ret
= ext4_ext_handle_uninitialized_extents(
3515 handle
, inode
, map
, path
, flags
,
3516 allocated
, newblock
);
3521 * Punch out the map length, but only to the
3524 punched_out
= allocated
< map
->m_len
?
3525 allocated
: map
->m_len
;
3528 * Sense extents need to be converted to
3529 * uninitialized, they must fit in an
3530 * uninitialized extent
3532 if (punched_out
> EXT_UNINIT_MAX_LEN
)
3533 punched_out
= EXT_UNINIT_MAX_LEN
;
3535 punch_map
.m_lblk
= map
->m_lblk
;
3536 punch_map
.m_pblk
= newblock
;
3537 punch_map
.m_len
= punched_out
;
3538 punch_map
.m_flags
= 0;
3540 /* Check to see if the extent needs to be split */
3541 if (punch_map
.m_len
!= ee_len
||
3542 punch_map
.m_lblk
!= ee_block
) {
3544 ret
= ext4_split_extent(handle
, inode
,
3545 path
, &punch_map
, 0,
3546 EXT4_GET_BLOCKS_PUNCH_OUT_EXT
|
3547 EXT4_GET_BLOCKS_PRE_IO
);
3554 * find extent for the block at
3555 * the start of the hole
3557 ext4_ext_drop_refs(path
);
3560 path
= ext4_ext_find_extent(inode
,
3563 err
= PTR_ERR(path
);
3568 depth
= ext_depth(inode
);
3569 ex
= path
[depth
].p_ext
;
3570 ee_len
= ext4_ext_get_actual_len(ex
);
3571 ee_block
= le32_to_cpu(ex
->ee_block
);
3572 ee_start
= ext4_ext_pblock(ex
);
3576 ext4_ext_mark_uninitialized(ex
);
3578 ext4_ext_invalidate_cache(inode
);
3580 err
= ext4_ext_rm_leaf(handle
, inode
, path
,
3581 map
->m_lblk
, map
->m_lblk
+ punched_out
);
3583 if (!err
&& path
->p_hdr
->eh_entries
== 0) {
3585 * Punch hole freed all of this sub tree,
3586 * so we need to correct eh_depth
3588 err
= ext4_ext_get_access(handle
, inode
, path
);
3590 ext_inode_hdr(inode
)->eh_depth
= 0;
3591 ext_inode_hdr(inode
)->eh_max
=
3592 cpu_to_le16(ext4_ext_space_root(
3595 err
= ext4_ext_dirty(
3596 handle
, inode
, path
);
3605 * requested block isn't allocated yet;
3606 * we couldn't try to create block if create flag is zero
3608 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3610 * put just found gap into cache to speed up
3611 * subsequent requests
3613 ext4_ext_put_gap_in_cache(inode
, path
, map
->m_lblk
);
3618 * Okay, we need to do block allocation.
3620 newex
.ee_block
= cpu_to_le32(map
->m_lblk
);
3621 cluster_offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
-1);
3624 * If we are doing bigalloc, check to see if the extent returned
3625 * by ext4_ext_find_extent() implies a cluster we can use.
3627 if (cluster_offset
&& ex
&&
3628 get_implied_cluster_alloc(sbi
, map
, ex
, path
)) {
3629 ar
.len
= allocated
= map
->m_len
;
3630 newblock
= map
->m_pblk
;
3631 goto got_allocated_blocks
;
3634 /* find neighbour allocated blocks */
3635 ar
.lleft
= map
->m_lblk
;
3636 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
3639 ar
.lright
= map
->m_lblk
;
3641 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
, &ex2
);
3645 /* Check if the extent after searching to the right implies a
3646 * cluster we can use. */
3647 if ((sbi
->s_cluster_ratio
> 1) && ex2
&&
3648 get_implied_cluster_alloc(sbi
, map
, ex2
, path
)) {
3649 ar
.len
= allocated
= map
->m_len
;
3650 newblock
= map
->m_pblk
;
3651 goto got_allocated_blocks
;
3655 * See if request is beyond maximum number of blocks we can have in
3656 * a single extent. For an initialized extent this limit is
3657 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3658 * EXT_UNINIT_MAX_LEN.
3660 if (map
->m_len
> EXT_INIT_MAX_LEN
&&
3661 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3662 map
->m_len
= EXT_INIT_MAX_LEN
;
3663 else if (map
->m_len
> EXT_UNINIT_MAX_LEN
&&
3664 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3665 map
->m_len
= EXT_UNINIT_MAX_LEN
;
3667 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3668 newex
.ee_len
= cpu_to_le16(map
->m_len
);
3669 err
= ext4_ext_check_overlap(sbi
, inode
, &newex
, path
);
3671 allocated
= ext4_ext_get_actual_len(&newex
);
3673 allocated
= map
->m_len
;
3675 /* allocate new block */
3677 ar
.goal
= ext4_ext_find_goal(inode
, path
, map
->m_lblk
);
3678 ar
.logical
= map
->m_lblk
;
3680 * We calculate the offset from the beginning of the cluster
3681 * for the logical block number, since when we allocate a
3682 * physical cluster, the physical block should start at the
3683 * same offset from the beginning of the cluster. This is
3684 * needed so that future calls to get_implied_cluster_alloc()
3687 offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
- 1);
3688 ar
.len
= EXT4_NUM_B2C(sbi
, offset
+allocated
);
3690 ar
.logical
-= offset
;
3691 if (S_ISREG(inode
->i_mode
))
3692 ar
.flags
= EXT4_MB_HINT_DATA
;
3694 /* disable in-core preallocation for non-regular files */
3696 if (flags
& EXT4_GET_BLOCKS_NO_NORMALIZE
)
3697 ar
.flags
|= EXT4_MB_HINT_NOPREALLOC
;
3698 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
3701 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3702 ar
.goal
, newblock
, allocated
);
3704 ar
.len
= EXT4_C2B(sbi
, ar
.len
) - offset
;
3705 if (ar
.len
> allocated
)
3708 got_allocated_blocks
:
3709 /* try to insert new extent into found leaf and return */
3710 ext4_ext_store_pblock(&newex
, newblock
+ offset
);
3711 newex
.ee_len
= cpu_to_le16(ar
.len
);
3712 /* Mark uninitialized */
3713 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
3714 ext4_ext_mark_uninitialized(&newex
);
3716 * io_end structure was created for every IO write to an
3717 * uninitialized extent. To avoid unnecessary conversion,
3718 * here we flag the IO that really needs the conversion.
3719 * For non asycn direct IO case, flag the inode state
3720 * that we need to perform conversion when IO is done.
3722 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3723 if (io
&& !(io
->flag
& EXT4_IO_END_UNWRITTEN
)) {
3724 io
->flag
= EXT4_IO_END_UNWRITTEN
;
3725 atomic_inc(&EXT4_I(inode
)->i_aiodio_unwritten
);
3727 ext4_set_inode_state(inode
,
3728 EXT4_STATE_DIO_UNWRITTEN
);
3730 if (ext4_should_dioread_nolock(inode
))
3731 map
->m_flags
|= EXT4_MAP_UNINIT
;
3734 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
, ar
.len
);
3736 err
= ext4_ext_insert_extent(handle
, inode
, path
,
3738 if (err
&& free_on_err
) {
3739 int fb_flags
= flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
?
3740 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
: 0;
3741 /* free data blocks we just allocated */
3742 /* not a good idea to call discard here directly,
3743 * but otherwise we'd need to call it every free() */
3744 ext4_discard_preallocations(inode
);
3745 ext4_free_blocks(handle
, inode
, NULL
, ext4_ext_pblock(&newex
),
3746 ext4_ext_get_actual_len(&newex
), fb_flags
);
3750 /* previous routine could use block we allocated */
3751 newblock
= ext4_ext_pblock(&newex
);
3752 allocated
= ext4_ext_get_actual_len(&newex
);
3753 if (allocated
> map
->m_len
)
3754 allocated
= map
->m_len
;
3755 map
->m_flags
|= EXT4_MAP_NEW
;
3758 * Update reserved blocks/metadata blocks after successful
3759 * block allocation which had been deferred till now.
3761 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3762 ext4_da_update_reserve_space(inode
, allocated
, 1);
3765 * Cache the extent and update transaction to commit on fdatasync only
3766 * when it is _not_ an uninitialized extent.
3768 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
3769 ext4_ext_put_in_cache(inode
, map
->m_lblk
, allocated
, newblock
);
3770 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3772 ext4_update_inode_fsync_trans(handle
, inode
, 0);
3774 if (allocated
> map
->m_len
)
3775 allocated
= map
->m_len
;
3776 ext4_ext_show_leaf(inode
, path
);
3777 map
->m_flags
|= EXT4_MAP_MAPPED
;
3778 map
->m_pblk
= newblock
;
3779 map
->m_len
= allocated
;
3782 ext4_ext_drop_refs(path
);
3785 trace_ext4_ext_map_blocks_exit(inode
, map
->m_lblk
,
3786 newblock
, map
->m_len
, err
? err
: allocated
);
3788 result
= (flags
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
) ?
3789 punched_out
: allocated
;
3791 return err
? err
: result
;
3794 void ext4_ext_truncate(struct inode
*inode
)
3796 struct address_space
*mapping
= inode
->i_mapping
;
3797 struct super_block
*sb
= inode
->i_sb
;
3798 ext4_lblk_t last_block
;
3804 * finish any pending end_io work so we won't run the risk of
3805 * converting any truncated blocks to initialized later
3807 ext4_flush_completed_IO(inode
);
3810 * probably first extent we're gonna free will be last in block
3812 err
= ext4_writepage_trans_blocks(inode
);
3813 handle
= ext4_journal_start(inode
, err
);
3817 if (inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
3818 page_len
= PAGE_CACHE_SIZE
-
3819 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
3821 err
= ext4_discard_partial_page_buffers(handle
,
3822 mapping
, inode
->i_size
, page_len
, 0);
3828 if (ext4_orphan_add(handle
, inode
))
3831 down_write(&EXT4_I(inode
)->i_data_sem
);
3832 ext4_ext_invalidate_cache(inode
);
3834 ext4_discard_preallocations(inode
);
3837 * TODO: optimization is possible here.
3838 * Probably we need not scan at all,
3839 * because page truncation is enough.
3842 /* we have to know where to truncate from in crash case */
3843 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3844 ext4_mark_inode_dirty(handle
, inode
);
3846 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3847 >> EXT4_BLOCK_SIZE_BITS(sb
);
3848 err
= ext4_ext_remove_space(inode
, last_block
);
3850 /* In a multi-transaction truncate, we only make the final
3851 * transaction synchronous.
3854 ext4_handle_sync(handle
);
3856 up_write(&EXT4_I(inode
)->i_data_sem
);
3860 * If this was a simple ftruncate() and the file will remain alive,
3861 * then we need to clear up the orphan record which we created above.
3862 * However, if this was a real unlink then we were called by
3863 * ext4_delete_inode(), and we allow that function to clean up the
3864 * orphan info for us.
3867 ext4_orphan_del(handle
, inode
);
3869 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3870 ext4_mark_inode_dirty(handle
, inode
);
3871 ext4_journal_stop(handle
);
3874 static void ext4_falloc_update_inode(struct inode
*inode
,
3875 int mode
, loff_t new_size
, int update_ctime
)
3877 struct timespec now
;
3880 now
= current_fs_time(inode
->i_sb
);
3881 if (!timespec_equal(&inode
->i_ctime
, &now
))
3882 inode
->i_ctime
= now
;
3885 * Update only when preallocation was requested beyond
3888 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3889 if (new_size
> i_size_read(inode
))
3890 i_size_write(inode
, new_size
);
3891 if (new_size
> EXT4_I(inode
)->i_disksize
)
3892 ext4_update_i_disksize(inode
, new_size
);
3895 * Mark that we allocate beyond EOF so the subsequent truncate
3896 * can proceed even if the new size is the same as i_size.
3898 if (new_size
> i_size_read(inode
))
3899 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3905 * preallocate space for a file. This implements ext4's fallocate file
3906 * operation, which gets called from sys_fallocate system call.
3907 * For block-mapped files, posix_fallocate should fall back to the method
3908 * of writing zeroes to the required new blocks (the same behavior which is
3909 * expected for file systems which do not support fallocate() system call).
3911 long ext4_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
3913 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
3916 unsigned int max_blocks
;
3920 struct ext4_map_blocks map
;
3921 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3924 * currently supporting (pre)allocate mode for extent-based
3927 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
3930 /* Return error if mode is not supported */
3931 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
3934 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3935 return ext4_punch_hole(file
, offset
, len
);
3937 trace_ext4_fallocate_enter(inode
, offset
, len
, mode
);
3938 map
.m_lblk
= offset
>> blkbits
;
3940 * We can't just convert len to max_blocks because
3941 * If blocksize = 4096 offset = 3072 and len = 2048
3943 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3946 * credits to insert 1 extent into extent tree
3948 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3949 mutex_lock(&inode
->i_mutex
);
3950 ret
= inode_newsize_ok(inode
, (len
+ offset
));
3952 mutex_unlock(&inode
->i_mutex
);
3953 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
, ret
);
3957 while (ret
>= 0 && ret
< max_blocks
) {
3958 map
.m_lblk
= map
.m_lblk
+ ret
;
3959 map
.m_len
= max_blocks
= max_blocks
- ret
;
3960 handle
= ext4_journal_start(inode
, credits
);
3961 if (IS_ERR(handle
)) {
3962 ret
= PTR_ERR(handle
);
3965 ret
= ext4_map_blocks(handle
, inode
, &map
,
3966 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
|
3967 EXT4_GET_BLOCKS_NO_NORMALIZE
);
3971 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
3972 "returned error inode#%lu, block=%u, "
3973 "max_blocks=%u", __func__
,
3974 inode
->i_ino
, map
.m_lblk
, max_blocks
);
3976 ext4_mark_inode_dirty(handle
, inode
);
3977 ret2
= ext4_journal_stop(handle
);
3980 if ((map
.m_lblk
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3981 blkbits
) >> blkbits
))
3982 new_size
= offset
+ len
;
3984 new_size
= ((loff_t
) map
.m_lblk
+ ret
) << blkbits
;
3986 ext4_falloc_update_inode(inode
, mode
, new_size
,
3987 (map
.m_flags
& EXT4_MAP_NEW
));
3988 ext4_mark_inode_dirty(handle
, inode
);
3989 ret2
= ext4_journal_stop(handle
);
3993 if (ret
== -ENOSPC
&&
3994 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3998 mutex_unlock(&inode
->i_mutex
);
3999 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
,
4000 ret
> 0 ? ret2
: ret
);
4001 return ret
> 0 ? ret2
: ret
;
4005 * This function convert a range of blocks to written extents
4006 * The caller of this function will pass the start offset and the size.
4007 * all unwritten extents within this range will be converted to
4010 * This function is called from the direct IO end io call back
4011 * function, to convert the fallocated extents after IO is completed.
4012 * Returns 0 on success.
4014 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
4018 unsigned int max_blocks
;
4021 struct ext4_map_blocks map
;
4022 unsigned int credits
, blkbits
= inode
->i_blkbits
;
4024 map
.m_lblk
= offset
>> blkbits
;
4026 * We can't just convert len to max_blocks because
4027 * If blocksize = 4096 offset = 3072 and len = 2048
4029 max_blocks
= ((EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
) -
4032 * credits to insert 1 extent into extent tree
4034 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
4035 while (ret
>= 0 && ret
< max_blocks
) {
4037 map
.m_len
= (max_blocks
-= ret
);
4038 handle
= ext4_journal_start(inode
, credits
);
4039 if (IS_ERR(handle
)) {
4040 ret
= PTR_ERR(handle
);
4043 ret
= ext4_map_blocks(handle
, inode
, &map
,
4044 EXT4_GET_BLOCKS_IO_CONVERT_EXT
);
4047 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
4048 "returned error inode#%lu, block=%u, "
4049 "max_blocks=%u", __func__
,
4050 inode
->i_ino
, map
.m_lblk
, map
.m_len
);
4052 ext4_mark_inode_dirty(handle
, inode
);
4053 ret2
= ext4_journal_stop(handle
);
4054 if (ret
<= 0 || ret2
)
4057 return ret
> 0 ? ret2
: ret
;
4061 * Callback function called for each extent to gather FIEMAP information.
4063 static int ext4_ext_fiemap_cb(struct inode
*inode
, ext4_lblk_t next
,
4064 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
4072 struct fiemap_extent_info
*fieinfo
= data
;
4073 unsigned char blksize_bits
;
4075 blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
4076 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
4078 if (newex
->ec_start
== 0) {
4080 * No extent in extent-tree contains block @newex->ec_start,
4081 * then the block may stay in 1)a hole or 2)delayed-extent.
4083 * Holes or delayed-extents are processed as follows.
4084 * 1. lookup dirty pages with specified range in pagecache.
4085 * If no page is got, then there is no delayed-extent and
4086 * return with EXT_CONTINUE.
4087 * 2. find the 1st mapped buffer,
4088 * 3. check if the mapped buffer is both in the request range
4089 * and a delayed buffer. If not, there is no delayed-extent,
4091 * 4. a delayed-extent is found, the extent will be collected.
4093 ext4_lblk_t end
= 0;
4094 pgoff_t last_offset
;
4097 pgoff_t start_index
= 0;
4098 struct page
**pages
= NULL
;
4099 struct buffer_head
*bh
= NULL
;
4100 struct buffer_head
*head
= NULL
;
4101 unsigned int nr_pages
= PAGE_SIZE
/ sizeof(struct page
*);
4103 pages
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4107 offset
= logical
>> PAGE_SHIFT
;
4109 last_offset
= offset
;
4111 ret
= find_get_pages_tag(inode
->i_mapping
, &offset
,
4112 PAGECACHE_TAG_DIRTY
, nr_pages
, pages
);
4114 if (!(flags
& FIEMAP_EXTENT_DELALLOC
)) {
4115 /* First time, try to find a mapped buffer. */
4118 for (index
= 0; index
< ret
; index
++)
4119 page_cache_release(pages
[index
]);
4122 return EXT_CONTINUE
;
4127 /* Try to find the 1st mapped buffer. */
4128 end
= ((__u64
)pages
[index
]->index
<< PAGE_SHIFT
) >>
4130 if (!page_has_buffers(pages
[index
]))
4132 head
= page_buffers(pages
[index
]);
4139 if (end
>= newex
->ec_block
+
4141 /* The buffer is out of
4142 * the request range.
4146 if (buffer_mapped(bh
) &&
4147 end
>= newex
->ec_block
) {
4148 start_index
= index
- 1;
4149 /* get the 1st mapped buffer. */
4150 goto found_mapped_buffer
;
4153 bh
= bh
->b_this_page
;
4155 } while (bh
!= head
);
4157 /* No mapped buffer in the range found in this page,
4158 * We need to look up next page.
4161 /* There is no page left, but we need to limit
4164 newex
->ec_len
= end
- newex
->ec_block
;
4169 /*Find contiguous delayed buffers. */
4170 if (ret
> 0 && pages
[0]->index
== last_offset
)
4171 head
= page_buffers(pages
[0]);
4177 found_mapped_buffer
:
4178 if (bh
!= NULL
&& buffer_delay(bh
)) {
4179 /* 1st or contiguous delayed buffer found. */
4180 if (!(flags
& FIEMAP_EXTENT_DELALLOC
)) {
4182 * 1st delayed buffer found, record
4183 * the start of extent.
4185 flags
|= FIEMAP_EXTENT_DELALLOC
;
4186 newex
->ec_block
= end
;
4187 logical
= (__u64
)end
<< blksize_bits
;
4189 /* Find contiguous delayed buffers. */
4191 if (!buffer_delay(bh
))
4192 goto found_delayed_extent
;
4193 bh
= bh
->b_this_page
;
4195 } while (bh
!= head
);
4197 for (; index
< ret
; index
++) {
4198 if (!page_has_buffers(pages
[index
])) {
4202 head
= page_buffers(pages
[index
]);
4208 if (pages
[index
]->index
!=
4209 pages
[start_index
]->index
+ index
4211 /* Blocks are not contiguous. */
4217 if (!buffer_delay(bh
))
4218 /* Delayed-extent ends. */
4219 goto found_delayed_extent
;
4220 bh
= bh
->b_this_page
;
4222 } while (bh
!= head
);
4224 } else if (!(flags
& FIEMAP_EXTENT_DELALLOC
))
4228 found_delayed_extent
:
4229 newex
->ec_len
= min(end
- newex
->ec_block
,
4230 (ext4_lblk_t
)EXT_INIT_MAX_LEN
);
4231 if (ret
== nr_pages
&& bh
!= NULL
&&
4232 newex
->ec_len
< EXT_INIT_MAX_LEN
&&
4234 /* Have not collected an extent and continue. */
4235 for (index
= 0; index
< ret
; index
++)
4236 page_cache_release(pages
[index
]);
4240 for (index
= 0; index
< ret
; index
++)
4241 page_cache_release(pages
[index
]);
4245 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
4246 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
4248 if (ex
&& ext4_ext_is_uninitialized(ex
))
4249 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
4251 if (next
== EXT_MAX_BLOCKS
)
4252 flags
|= FIEMAP_EXTENT_LAST
;
4254 ret
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
4260 return EXT_CONTINUE
;
4262 /* fiemap flags we can handle specified here */
4263 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4265 static int ext4_xattr_fiemap(struct inode
*inode
,
4266 struct fiemap_extent_info
*fieinfo
)
4270 __u32 flags
= FIEMAP_EXTENT_LAST
;
4271 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
4275 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
4276 struct ext4_iloc iloc
;
4277 int offset
; /* offset of xattr in inode */
4279 error
= ext4_get_inode_loc(inode
, &iloc
);
4282 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
4283 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
4284 EXT4_I(inode
)->i_extra_isize
;
4286 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
4287 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
4289 } else { /* external block */
4290 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
4291 length
= inode
->i_sb
->s_blocksize
;
4295 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
4297 return (error
< 0 ? error
: 0);
4301 * ext4_ext_punch_hole
4303 * Punches a hole of "length" bytes in a file starting
4306 * @inode: The inode of the file to punch a hole in
4307 * @offset: The starting byte offset of the hole
4308 * @length: The length of the hole
4310 * Returns the number of blocks removed or negative on err
4312 int ext4_ext_punch_hole(struct file
*file
, loff_t offset
, loff_t length
)
4314 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
4315 struct super_block
*sb
= inode
->i_sb
;
4316 struct ext4_ext_cache cache_ex
;
4317 ext4_lblk_t first_block
, last_block
, num_blocks
, iblock
, max_blocks
;
4318 struct address_space
*mapping
= inode
->i_mapping
;
4319 struct ext4_map_blocks map
;
4321 loff_t first_page
, last_page
, page_len
;
4322 loff_t first_page_offset
, last_page_offset
;
4323 int ret
, credits
, blocks_released
, err
= 0;
4325 /* No need to punch hole beyond i_size */
4326 if (offset
>= inode
->i_size
)
4330 * If the hole extends beyond i_size, set the hole
4331 * to end after the page that contains i_size
4333 if (offset
+ length
> inode
->i_size
) {
4334 length
= inode
->i_size
+
4335 PAGE_CACHE_SIZE
- (inode
->i_size
& (PAGE_CACHE_SIZE
- 1)) -
4339 first_block
= (offset
+ sb
->s_blocksize
- 1) >>
4340 EXT4_BLOCK_SIZE_BITS(sb
);
4341 last_block
= (offset
+ length
) >> EXT4_BLOCK_SIZE_BITS(sb
);
4343 first_page
= (offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
4344 last_page
= (offset
+ length
) >> PAGE_CACHE_SHIFT
;
4346 first_page_offset
= first_page
<< PAGE_CACHE_SHIFT
;
4347 last_page_offset
= last_page
<< PAGE_CACHE_SHIFT
;
4350 * Write out all dirty pages to avoid race conditions
4351 * Then release them.
4353 if (mapping
->nrpages
&& mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
4354 err
= filemap_write_and_wait_range(mapping
,
4355 offset
, offset
+ length
- 1);
4361 /* Now release the pages */
4362 if (last_page_offset
> first_page_offset
) {
4363 truncate_inode_pages_range(mapping
, first_page_offset
,
4364 last_page_offset
-1);
4367 /* finish any pending end_io work */
4368 ext4_flush_completed_IO(inode
);
4370 credits
= ext4_writepage_trans_blocks(inode
);
4371 handle
= ext4_journal_start(inode
, credits
);
4373 return PTR_ERR(handle
);
4375 err
= ext4_orphan_add(handle
, inode
);
4380 * Now we need to zero out the non-page-aligned data in the
4381 * pages at the start and tail of the hole, and unmap the buffer
4382 * heads for the block aligned regions of the page that were
4383 * completely zeroed.
4385 if (first_page
> last_page
) {
4387 * If the file space being truncated is contained within a page
4388 * just zero out and unmap the middle of that page
4390 err
= ext4_discard_partial_page_buffers(handle
,
4391 mapping
, offset
, length
, 0);
4397 * zero out and unmap the partial page that contains
4398 * the start of the hole
4400 page_len
= first_page_offset
- offset
;
4402 err
= ext4_discard_partial_page_buffers(handle
, mapping
,
4403 offset
, page_len
, 0);
4409 * zero out and unmap the partial page that contains
4410 * the end of the hole
4412 page_len
= offset
+ length
- last_page_offset
;
4414 err
= ext4_discard_partial_page_buffers(handle
, mapping
,
4415 last_page_offset
, page_len
, 0);
4423 * If i_size is contained in the last page, we need to
4424 * unmap and zero the partial page after i_size
4426 if (inode
->i_size
>> PAGE_CACHE_SHIFT
== last_page
&&
4427 inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
4429 page_len
= PAGE_CACHE_SIZE
-
4430 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
4433 err
= ext4_discard_partial_page_buffers(handle
,
4434 mapping
, inode
->i_size
, page_len
, 0);
4441 /* If there are no blocks to remove, return now */
4442 if (first_block
>= last_block
)
4445 down_write(&EXT4_I(inode
)->i_data_sem
);
4446 ext4_ext_invalidate_cache(inode
);
4447 ext4_discard_preallocations(inode
);
4450 * Loop over all the blocks and identify blocks
4451 * that need to be punched out
4453 iblock
= first_block
;
4454 blocks_released
= 0;
4455 while (iblock
< last_block
) {
4456 max_blocks
= last_block
- iblock
;
4458 memset(&map
, 0, sizeof(map
));
4459 map
.m_lblk
= iblock
;
4460 map
.m_len
= max_blocks
;
4461 ret
= ext4_ext_map_blocks(handle
, inode
, &map
,
4462 EXT4_GET_BLOCKS_PUNCH_OUT_EXT
);
4465 blocks_released
+= ret
;
4467 } else if (ret
== 0) {
4469 * If map blocks could not find the block,
4470 * then it is in a hole. If the hole was
4471 * not already cached, then map blocks should
4472 * put it in the cache. So we can get the hole
4475 memset(&cache_ex
, 0, sizeof(cache_ex
));
4476 if ((ext4_ext_check_cache(inode
, iblock
, &cache_ex
)) &&
4477 !cache_ex
.ec_start
) {
4479 /* The hole is cached */
4480 num_blocks
= cache_ex
.ec_block
+
4481 cache_ex
.ec_len
- iblock
;
4484 /* The block could not be identified */
4489 /* Map blocks error */
4494 if (num_blocks
== 0) {
4495 /* This condition should never happen */
4496 ext_debug("Block lookup failed");
4501 iblock
+= num_blocks
;
4504 if (blocks_released
> 0) {
4505 ext4_ext_invalidate_cache(inode
);
4506 ext4_discard_preallocations(inode
);
4510 ext4_handle_sync(handle
);
4512 up_write(&EXT4_I(inode
)->i_data_sem
);
4515 ext4_orphan_del(handle
, inode
);
4516 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
4517 ext4_mark_inode_dirty(handle
, inode
);
4518 ext4_journal_stop(handle
);
4521 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
4522 __u64 start
, __u64 len
)
4524 ext4_lblk_t start_blk
;
4527 /* fallback to generic here if not in extents fmt */
4528 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
4529 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
4532 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
4535 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
4536 error
= ext4_xattr_fiemap(inode
, fieinfo
);
4538 ext4_lblk_t len_blks
;
4541 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
4542 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
4543 if (last_blk
>= EXT_MAX_BLOCKS
)
4544 last_blk
= EXT_MAX_BLOCKS
-1;
4545 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
4548 * Walk the extent tree gathering extent information.
4549 * ext4_ext_fiemap_cb will push extents back to user.
4551 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
4552 ext4_ext_fiemap_cb
, fieinfo
);