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_ext_truncate_extend_restart(handle_t
*handle
,
55 if (!ext4_handle_valid(handle
))
57 if (handle
->h_buffer_credits
> needed
)
59 err
= ext4_journal_extend(handle
, needed
);
62 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
74 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
75 struct ext4_ext_path
*path
)
78 /* path points to block */
79 return ext4_journal_get_write_access(handle
, path
->p_bh
);
81 /* path points to leaf/index in inode body */
82 /* we use in-core data, no need to protect them */
92 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
93 struct ext4_ext_path
*path
)
97 /* path points to block */
98 err
= ext4_handle_dirty_metadata(handle
, inode
, path
->p_bh
);
100 /* path points to leaf/index in inode body */
101 err
= ext4_mark_inode_dirty(handle
, inode
);
106 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
107 struct ext4_ext_path
*path
,
110 struct ext4_inode_info
*ei
= EXT4_I(inode
);
111 ext4_fsblk_t bg_start
;
112 ext4_fsblk_t last_block
;
113 ext4_grpblk_t colour
;
114 ext4_group_t block_group
;
115 int flex_size
= ext4_flex_bg_size(EXT4_SB(inode
->i_sb
));
119 struct ext4_extent
*ex
;
120 depth
= path
->p_depth
;
123 * Try to predict block placement assuming that we are
124 * filling in a file which will eventually be
125 * non-sparse --- i.e., in the case of libbfd writing
126 * an ELF object sections out-of-order but in a way
127 * the eventually results in a contiguous object or
128 * executable file, or some database extending a table
129 * space file. However, this is actually somewhat
130 * non-ideal if we are writing a sparse file such as
131 * qemu or KVM writing a raw image file that is going
132 * to stay fairly sparse, since it will end up
133 * fragmenting the file system's free space. Maybe we
134 * should have some hueristics or some way to allow
135 * userspace to pass a hint to file system,
136 * especially if the latter case turns out to be
139 ex
= path
[depth
].p_ext
;
141 ext4_fsblk_t ext_pblk
= ext4_ext_pblock(ex
);
142 ext4_lblk_t ext_block
= le32_to_cpu(ex
->ee_block
);
144 if (block
> ext_block
)
145 return ext_pblk
+ (block
- ext_block
);
147 return ext_pblk
- (ext_block
- block
);
150 /* it looks like index is empty;
151 * try to find starting block from index itself */
152 if (path
[depth
].p_bh
)
153 return path
[depth
].p_bh
->b_blocknr
;
156 /* OK. use inode's group */
157 block_group
= ei
->i_block_group
;
158 if (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) {
160 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
161 * block groups per flexgroup, reserve the first block
162 * group for directories and special files. Regular
163 * files will start at the second block group. This
164 * tends to speed up directory access and improves
167 block_group
&= ~(flex_size
-1);
168 if (S_ISREG(inode
->i_mode
))
171 bg_start
= ext4_group_first_block_no(inode
->i_sb
, block_group
);
172 last_block
= ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
) - 1;
175 * If we are doing delayed allocation, we don't need take
176 * colour into account.
178 if (test_opt(inode
->i_sb
, DELALLOC
))
181 if (bg_start
+ EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) <= last_block
)
182 colour
= (current
->pid
% 16) *
183 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
185 colour
= (current
->pid
% 16) * ((last_block
- bg_start
) / 16);
186 return bg_start
+ colour
+ block
;
190 * Allocation for a meta data block
193 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
194 struct ext4_ext_path
*path
,
195 struct ext4_extent
*ex
, int *err
)
197 ext4_fsblk_t goal
, newblock
;
199 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
200 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, NULL
, err
);
204 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
208 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
209 / sizeof(struct ext4_extent
);
211 #ifdef AGGRESSIVE_TEST
219 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
223 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
224 / sizeof(struct ext4_extent_idx
);
226 #ifdef AGGRESSIVE_TEST
234 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
238 size
= sizeof(EXT4_I(inode
)->i_data
);
239 size
-= sizeof(struct ext4_extent_header
);
240 size
/= sizeof(struct ext4_extent
);
242 #ifdef AGGRESSIVE_TEST
250 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
254 size
= sizeof(EXT4_I(inode
)->i_data
);
255 size
-= sizeof(struct ext4_extent_header
);
256 size
/= sizeof(struct ext4_extent_idx
);
258 #ifdef AGGRESSIVE_TEST
267 * Calculate the number of metadata blocks needed
268 * to allocate @blocks
269 * Worse case is one block per extent
271 int ext4_ext_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
273 struct ext4_inode_info
*ei
= EXT4_I(inode
);
276 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
277 / sizeof(struct ext4_extent_idx
));
280 * If the new delayed allocation block is contiguous with the
281 * previous da block, it can share index blocks with the
282 * previous block, so we only need to allocate a new index
283 * block every idxs leaf blocks. At ldxs**2 blocks, we need
284 * an additional index block, and at ldxs**3 blocks, yet
285 * another index blocks.
287 if (ei
->i_da_metadata_calc_len
&&
288 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
289 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
291 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
293 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
295 ei
->i_da_metadata_calc_len
= 0;
297 ei
->i_da_metadata_calc_len
++;
298 ei
->i_da_metadata_calc_last_lblock
++;
303 * In the worst case we need a new set of index blocks at
304 * every level of the inode's extent tree.
306 ei
->i_da_metadata_calc_len
= 1;
307 ei
->i_da_metadata_calc_last_lblock
= lblock
;
308 return ext_depth(inode
) + 1;
312 ext4_ext_max_entries(struct inode
*inode
, int depth
)
316 if (depth
== ext_depth(inode
)) {
318 max
= ext4_ext_space_root(inode
, 1);
320 max
= ext4_ext_space_root_idx(inode
, 1);
323 max
= ext4_ext_space_block(inode
, 1);
325 max
= ext4_ext_space_block_idx(inode
, 1);
331 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
333 ext4_fsblk_t block
= ext4_ext_pblock(ext
);
334 int len
= ext4_ext_get_actual_len(ext
);
336 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
339 static int ext4_valid_extent_idx(struct inode
*inode
,
340 struct ext4_extent_idx
*ext_idx
)
342 ext4_fsblk_t block
= ext4_idx_pblock(ext_idx
);
344 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
347 static int ext4_valid_extent_entries(struct inode
*inode
,
348 struct ext4_extent_header
*eh
,
351 struct ext4_extent
*ext
;
352 struct ext4_extent_idx
*ext_idx
;
353 unsigned short entries
;
354 if (eh
->eh_entries
== 0)
357 entries
= le16_to_cpu(eh
->eh_entries
);
361 ext
= EXT_FIRST_EXTENT(eh
);
363 if (!ext4_valid_extent(inode
, ext
))
369 ext_idx
= EXT_FIRST_INDEX(eh
);
371 if (!ext4_valid_extent_idx(inode
, ext_idx
))
380 static int __ext4_ext_check(const char *function
, unsigned int line
,
381 struct inode
*inode
, struct ext4_extent_header
*eh
,
384 const char *error_msg
;
387 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
388 error_msg
= "invalid magic";
391 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
392 error_msg
= "unexpected eh_depth";
395 if (unlikely(eh
->eh_max
== 0)) {
396 error_msg
= "invalid eh_max";
399 max
= ext4_ext_max_entries(inode
, depth
);
400 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
401 error_msg
= "too large eh_max";
404 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
405 error_msg
= "invalid eh_entries";
408 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
409 error_msg
= "invalid extent entries";
415 ext4_error_inode(inode
, function
, line
, 0,
416 "bad header/extent: %s - magic %x, "
417 "entries %u, max %u(%u), depth %u(%u)",
418 error_msg
, le16_to_cpu(eh
->eh_magic
),
419 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
420 max
, le16_to_cpu(eh
->eh_depth
), depth
);
425 #define ext4_ext_check(inode, eh, depth) \
426 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
428 int ext4_ext_check_inode(struct inode
*inode
)
430 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
434 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
436 int k
, l
= path
->p_depth
;
439 for (k
= 0; k
<= l
; k
++, path
++) {
441 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
442 ext4_idx_pblock(path
->p_idx
));
443 } else if (path
->p_ext
) {
444 ext_debug(" %d:[%d]%d:%llu ",
445 le32_to_cpu(path
->p_ext
->ee_block
),
446 ext4_ext_is_uninitialized(path
->p_ext
),
447 ext4_ext_get_actual_len(path
->p_ext
),
448 ext4_ext_pblock(path
->p_ext
));
455 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
457 int depth
= ext_depth(inode
);
458 struct ext4_extent_header
*eh
;
459 struct ext4_extent
*ex
;
465 eh
= path
[depth
].p_hdr
;
466 ex
= EXT_FIRST_EXTENT(eh
);
468 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
470 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
471 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
472 ext4_ext_is_uninitialized(ex
),
473 ext4_ext_get_actual_len(ex
), ext4_ext_pblock(ex
));
478 #define ext4_ext_show_path(inode, path)
479 #define ext4_ext_show_leaf(inode, path)
482 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
484 int depth
= path
->p_depth
;
487 for (i
= 0; i
<= depth
; i
++, path
++)
495 * ext4_ext_binsearch_idx:
496 * binary search for the closest index of the given block
497 * the header must be checked before calling this
500 ext4_ext_binsearch_idx(struct inode
*inode
,
501 struct ext4_ext_path
*path
, ext4_lblk_t block
)
503 struct ext4_extent_header
*eh
= path
->p_hdr
;
504 struct ext4_extent_idx
*r
, *l
, *m
;
507 ext_debug("binsearch for %u(idx): ", block
);
509 l
= EXT_FIRST_INDEX(eh
) + 1;
510 r
= EXT_LAST_INDEX(eh
);
513 if (block
< le32_to_cpu(m
->ei_block
))
517 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
518 m
, le32_to_cpu(m
->ei_block
),
519 r
, le32_to_cpu(r
->ei_block
));
523 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
524 ext4_idx_pblock(path
->p_idx
));
526 #ifdef CHECK_BINSEARCH
528 struct ext4_extent_idx
*chix
, *ix
;
531 chix
= ix
= EXT_FIRST_INDEX(eh
);
532 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
534 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
535 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
537 ix
, EXT_FIRST_INDEX(eh
));
538 printk(KERN_DEBUG
"%u <= %u\n",
539 le32_to_cpu(ix
->ei_block
),
540 le32_to_cpu(ix
[-1].ei_block
));
542 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
543 <= le32_to_cpu(ix
[-1].ei_block
));
544 if (block
< le32_to_cpu(ix
->ei_block
))
548 BUG_ON(chix
!= path
->p_idx
);
555 * ext4_ext_binsearch:
556 * binary search for closest extent of the given block
557 * the header must be checked before calling this
560 ext4_ext_binsearch(struct inode
*inode
,
561 struct ext4_ext_path
*path
, ext4_lblk_t block
)
563 struct ext4_extent_header
*eh
= path
->p_hdr
;
564 struct ext4_extent
*r
, *l
, *m
;
566 if (eh
->eh_entries
== 0) {
568 * this leaf is empty:
569 * we get such a leaf in split/add case
574 ext_debug("binsearch for %u: ", block
);
576 l
= EXT_FIRST_EXTENT(eh
) + 1;
577 r
= EXT_LAST_EXTENT(eh
);
581 if (block
< le32_to_cpu(m
->ee_block
))
585 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
586 m
, le32_to_cpu(m
->ee_block
),
587 r
, le32_to_cpu(r
->ee_block
));
591 ext_debug(" -> %d:%llu:[%d]%d ",
592 le32_to_cpu(path
->p_ext
->ee_block
),
593 ext4_ext_pblock(path
->p_ext
),
594 ext4_ext_is_uninitialized(path
->p_ext
),
595 ext4_ext_get_actual_len(path
->p_ext
));
597 #ifdef CHECK_BINSEARCH
599 struct ext4_extent
*chex
, *ex
;
602 chex
= ex
= EXT_FIRST_EXTENT(eh
);
603 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
604 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
605 <= le32_to_cpu(ex
[-1].ee_block
));
606 if (block
< le32_to_cpu(ex
->ee_block
))
610 BUG_ON(chex
!= path
->p_ext
);
616 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
618 struct ext4_extent_header
*eh
;
620 eh
= ext_inode_hdr(inode
);
623 eh
->eh_magic
= EXT4_EXT_MAGIC
;
624 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
625 ext4_mark_inode_dirty(handle
, inode
);
626 ext4_ext_invalidate_cache(inode
);
630 struct ext4_ext_path
*
631 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
632 struct ext4_ext_path
*path
)
634 struct ext4_extent_header
*eh
;
635 struct buffer_head
*bh
;
636 short int depth
, i
, ppos
= 0, alloc
= 0;
638 eh
= ext_inode_hdr(inode
);
639 depth
= ext_depth(inode
);
641 /* account possible depth increase */
643 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
646 return ERR_PTR(-ENOMEM
);
653 /* walk through the tree */
655 int need_to_validate
= 0;
657 ext_debug("depth %d: num %d, max %d\n",
658 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
660 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
661 path
[ppos
].p_block
= ext4_idx_pblock(path
[ppos
].p_idx
);
662 path
[ppos
].p_depth
= i
;
663 path
[ppos
].p_ext
= NULL
;
665 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
668 if (!bh_uptodate_or_lock(bh
)) {
669 trace_ext4_ext_load_extent(inode
, block
,
671 if (bh_submit_read(bh
) < 0) {
675 /* validate the extent entries */
676 need_to_validate
= 1;
678 eh
= ext_block_hdr(bh
);
680 if (unlikely(ppos
> depth
)) {
682 EXT4_ERROR_INODE(inode
,
683 "ppos %d > depth %d", ppos
, depth
);
686 path
[ppos
].p_bh
= bh
;
687 path
[ppos
].p_hdr
= eh
;
690 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
694 path
[ppos
].p_depth
= i
;
695 path
[ppos
].p_ext
= NULL
;
696 path
[ppos
].p_idx
= NULL
;
699 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
700 /* if not an empty leaf */
701 if (path
[ppos
].p_ext
)
702 path
[ppos
].p_block
= ext4_ext_pblock(path
[ppos
].p_ext
);
704 ext4_ext_show_path(inode
, path
);
709 ext4_ext_drop_refs(path
);
712 return ERR_PTR(-EIO
);
716 * ext4_ext_insert_index:
717 * insert new index [@logical;@ptr] into the block at @curp;
718 * check where to insert: before @curp or after @curp
720 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
721 struct ext4_ext_path
*curp
,
722 int logical
, ext4_fsblk_t ptr
)
724 struct ext4_extent_idx
*ix
;
727 err
= ext4_ext_get_access(handle
, inode
, curp
);
731 if (unlikely(logical
== le32_to_cpu(curp
->p_idx
->ei_block
))) {
732 EXT4_ERROR_INODE(inode
,
733 "logical %d == ei_block %d!",
734 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
737 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
738 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
740 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
741 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
742 len
= len
< 0 ? 0 : len
;
743 ext_debug("insert new index %d after: %llu. "
744 "move %d from 0x%p to 0x%p\n",
746 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
747 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
749 ix
= curp
->p_idx
+ 1;
752 len
= len
* sizeof(struct ext4_extent_idx
);
753 len
= len
< 0 ? 0 : len
;
754 ext_debug("insert new index %d before: %llu. "
755 "move %d from 0x%p to 0x%p\n",
757 curp
->p_idx
, (curp
->p_idx
+ 1));
758 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
762 ix
->ei_block
= cpu_to_le32(logical
);
763 ext4_idx_store_pblock(ix
, ptr
);
764 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
766 if (unlikely(le16_to_cpu(curp
->p_hdr
->eh_entries
)
767 > le16_to_cpu(curp
->p_hdr
->eh_max
))) {
768 EXT4_ERROR_INODE(inode
,
769 "logical %d == ei_block %d!",
770 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
773 if (unlikely(ix
> EXT_LAST_INDEX(curp
->p_hdr
))) {
774 EXT4_ERROR_INODE(inode
, "ix > EXT_LAST_INDEX!");
778 err
= ext4_ext_dirty(handle
, inode
, curp
);
779 ext4_std_error(inode
->i_sb
, err
);
786 * inserts new subtree into the path, using free index entry
788 * - allocates all needed blocks (new leaf and all intermediate index blocks)
789 * - makes decision where to split
790 * - moves remaining extents and index entries (right to the split point)
791 * into the newly allocated blocks
792 * - initializes subtree
794 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
795 struct ext4_ext_path
*path
,
796 struct ext4_extent
*newext
, int at
)
798 struct buffer_head
*bh
= NULL
;
799 int depth
= ext_depth(inode
);
800 struct ext4_extent_header
*neh
;
801 struct ext4_extent_idx
*fidx
;
802 struct ext4_extent
*ex
;
804 ext4_fsblk_t newblock
, oldblock
;
806 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
809 /* make decision: where to split? */
810 /* FIXME: now decision is simplest: at current extent */
812 /* if current leaf will be split, then we should use
813 * border from split point */
814 if (unlikely(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
))) {
815 EXT4_ERROR_INODE(inode
, "p_ext > EXT_MAX_EXTENT!");
818 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
819 border
= path
[depth
].p_ext
[1].ee_block
;
820 ext_debug("leaf will be split."
821 " next leaf starts at %d\n",
822 le32_to_cpu(border
));
824 border
= newext
->ee_block
;
825 ext_debug("leaf will be added."
826 " next leaf starts at %d\n",
827 le32_to_cpu(border
));
831 * If error occurs, then we break processing
832 * and mark filesystem read-only. index won't
833 * be inserted and tree will be in consistent
834 * state. Next mount will repair buffers too.
838 * Get array to track all allocated blocks.
839 * We need this to handle errors and free blocks
842 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
846 /* allocate all needed blocks */
847 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
848 for (a
= 0; a
< depth
- at
; a
++) {
849 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
853 ablocks
[a
] = newblock
;
856 /* initialize new leaf */
857 newblock
= ablocks
[--a
];
858 if (unlikely(newblock
== 0)) {
859 EXT4_ERROR_INODE(inode
, "newblock == 0!");
863 bh
= sb_getblk(inode
->i_sb
, newblock
);
870 err
= ext4_journal_get_create_access(handle
, bh
);
874 neh
= ext_block_hdr(bh
);
876 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
877 neh
->eh_magic
= EXT4_EXT_MAGIC
;
879 ex
= EXT_FIRST_EXTENT(neh
);
881 /* move remainder of path[depth] to the new leaf */
882 if (unlikely(path
[depth
].p_hdr
->eh_entries
!=
883 path
[depth
].p_hdr
->eh_max
)) {
884 EXT4_ERROR_INODE(inode
, "eh_entries %d != eh_max %d!",
885 path
[depth
].p_hdr
->eh_entries
,
886 path
[depth
].p_hdr
->eh_max
);
890 /* start copy from next extent */
891 /* TODO: we could do it by single memmove */
894 while (path
[depth
].p_ext
<=
895 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
896 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
897 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
898 ext4_ext_pblock(path
[depth
].p_ext
),
899 ext4_ext_is_uninitialized(path
[depth
].p_ext
),
900 ext4_ext_get_actual_len(path
[depth
].p_ext
),
902 /*memmove(ex++, path[depth].p_ext++,
903 sizeof(struct ext4_extent));
909 memmove(ex
, path
[depth
].p_ext
-m
, 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
);
975 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
976 EXT_MAX_INDEX(path
[i
].p_hdr
));
977 if (unlikely(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
978 EXT_LAST_INDEX(path
[i
].p_hdr
))) {
979 EXT4_ERROR_INODE(inode
,
980 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
981 le32_to_cpu(path
[i
].p_ext
->ee_block
));
985 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
986 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
987 le32_to_cpu(path
[i
].p_idx
->ei_block
),
988 ext4_idx_pblock(path
[i
].p_idx
),
990 /*memmove(++fidx, path[i].p_idx++,
991 sizeof(struct ext4_extent_idx));
993 BUG_ON(neh->eh_entries > neh->eh_max);*/
998 memmove(++fidx
, path
[i
].p_idx
- m
,
999 sizeof(struct ext4_extent_idx
) * m
);
1000 le16_add_cpu(&neh
->eh_entries
, m
);
1002 set_buffer_uptodate(bh
);
1005 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1011 /* correct old index */
1013 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
1016 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1017 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1025 /* insert new index */
1026 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1027 le32_to_cpu(border
), newblock
);
1031 if (buffer_locked(bh
))
1037 /* free all allocated blocks in error case */
1038 for (i
= 0; i
< depth
; i
++) {
1041 ext4_free_blocks(handle
, inode
, NULL
, ablocks
[i
], 1,
1042 EXT4_FREE_BLOCKS_METADATA
);
1051 * ext4_ext_grow_indepth:
1052 * implements tree growing procedure:
1053 * - allocates new block
1054 * - moves top-level data (index block or leaf) into the new block
1055 * - initializes new top-level, creating index that points to the
1056 * just created block
1058 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1059 struct ext4_ext_path
*path
,
1060 struct ext4_extent
*newext
)
1062 struct ext4_ext_path
*curp
= path
;
1063 struct ext4_extent_header
*neh
;
1064 struct buffer_head
*bh
;
1065 ext4_fsblk_t newblock
;
1068 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
, newext
, &err
);
1072 bh
= sb_getblk(inode
->i_sb
, newblock
);
1075 ext4_std_error(inode
->i_sb
, err
);
1080 err
= ext4_journal_get_create_access(handle
, bh
);
1086 /* move top-level index/leaf into new block */
1087 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1089 /* set size of new block */
1090 neh
= ext_block_hdr(bh
);
1091 /* old root could have indexes or leaves
1092 * so calculate e_max right way */
1093 if (ext_depth(inode
))
1094 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1096 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1097 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1098 set_buffer_uptodate(bh
);
1101 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1105 /* create index in new top-level index: num,max,pointer */
1106 err
= ext4_ext_get_access(handle
, inode
, curp
);
1110 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1111 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1112 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1113 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1115 if (path
[0].p_hdr
->eh_depth
)
1116 curp
->p_idx
->ei_block
=
1117 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1119 curp
->p_idx
->ei_block
=
1120 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1121 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1123 neh
= ext_inode_hdr(inode
);
1124 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1125 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1126 le32_to_cpu(EXT_FIRST_INDEX(neh
)->ei_block
),
1127 ext4_idx_pblock(EXT_FIRST_INDEX(neh
)));
1129 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1130 err
= ext4_ext_dirty(handle
, inode
, curp
);
1138 * ext4_ext_create_new_leaf:
1139 * finds empty index and adds new leaf.
1140 * if no free index is found, then it requests in-depth growing.
1142 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1143 struct ext4_ext_path
*path
,
1144 struct ext4_extent
*newext
)
1146 struct ext4_ext_path
*curp
;
1147 int depth
, i
, err
= 0;
1150 i
= depth
= ext_depth(inode
);
1152 /* walk up to the tree and look for free index entry */
1153 curp
= path
+ depth
;
1154 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1159 /* we use already allocated block for index block,
1160 * so subsequent data blocks should be contiguous */
1161 if (EXT_HAS_FREE_INDEX(curp
)) {
1162 /* if we found index with free entry, then use that
1163 * entry: create all needed subtree and add new leaf */
1164 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
1169 ext4_ext_drop_refs(path
);
1170 path
= ext4_ext_find_extent(inode
,
1171 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1174 err
= PTR_ERR(path
);
1176 /* tree is full, time to grow in depth */
1177 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1182 ext4_ext_drop_refs(path
);
1183 path
= ext4_ext_find_extent(inode
,
1184 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1187 err
= PTR_ERR(path
);
1192 * only first (depth 0 -> 1) produces free space;
1193 * in all other cases we have to split the grown tree
1195 depth
= ext_depth(inode
);
1196 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1197 /* now we need to split */
1207 * search the closest allocated block to the left for *logical
1208 * and returns it at @logical + it's physical address at @phys
1209 * if *logical is the smallest allocated block, the function
1210 * returns 0 at @phys
1211 * return value contains 0 (success) or error code
1213 static int ext4_ext_search_left(struct inode
*inode
,
1214 struct ext4_ext_path
*path
,
1215 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1217 struct ext4_extent_idx
*ix
;
1218 struct ext4_extent
*ex
;
1221 if (unlikely(path
== NULL
)) {
1222 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1225 depth
= path
->p_depth
;
1228 if (depth
== 0 && path
->p_ext
== NULL
)
1231 /* usually extent in the path covers blocks smaller
1232 * then *logical, but it can be that extent is the
1233 * first one in the file */
1235 ex
= path
[depth
].p_ext
;
1236 ee_len
= ext4_ext_get_actual_len(ex
);
1237 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1238 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1239 EXT4_ERROR_INODE(inode
,
1240 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1241 *logical
, le32_to_cpu(ex
->ee_block
));
1244 while (--depth
>= 0) {
1245 ix
= path
[depth
].p_idx
;
1246 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1247 EXT4_ERROR_INODE(inode
,
1248 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1249 ix
!= NULL
? ix
->ei_block
: 0,
1250 EXT_FIRST_INDEX(path
[depth
].p_hdr
) != NULL
?
1251 EXT_FIRST_INDEX(path
[depth
].p_hdr
)->ei_block
: 0,
1259 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1260 EXT4_ERROR_INODE(inode
,
1261 "logical %d < ee_block %d + ee_len %d!",
1262 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1266 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1267 *phys
= ext4_ext_pblock(ex
) + ee_len
- 1;
1272 * search the closest allocated block to the right for *logical
1273 * and returns it at @logical + it's physical address at @phys
1274 * if *logical is the smallest allocated block, the function
1275 * returns 0 at @phys
1276 * return value contains 0 (success) or error code
1278 static int ext4_ext_search_right(struct inode
*inode
,
1279 struct ext4_ext_path
*path
,
1280 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1282 struct buffer_head
*bh
= NULL
;
1283 struct ext4_extent_header
*eh
;
1284 struct ext4_extent_idx
*ix
;
1285 struct ext4_extent
*ex
;
1287 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1290 if (unlikely(path
== NULL
)) {
1291 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1294 depth
= path
->p_depth
;
1297 if (depth
== 0 && path
->p_ext
== NULL
)
1300 /* usually extent in the path covers blocks smaller
1301 * then *logical, but it can be that extent is the
1302 * first one in the file */
1304 ex
= path
[depth
].p_ext
;
1305 ee_len
= ext4_ext_get_actual_len(ex
);
1306 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1307 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1308 EXT4_ERROR_INODE(inode
,
1309 "first_extent(path[%d].p_hdr) != ex",
1313 while (--depth
>= 0) {
1314 ix
= path
[depth
].p_idx
;
1315 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1316 EXT4_ERROR_INODE(inode
,
1317 "ix != EXT_FIRST_INDEX *logical %d!",
1322 *logical
= le32_to_cpu(ex
->ee_block
);
1323 *phys
= ext4_ext_pblock(ex
);
1327 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1328 EXT4_ERROR_INODE(inode
,
1329 "logical %d < ee_block %d + ee_len %d!",
1330 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1334 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1335 /* next allocated block in this leaf */
1337 *logical
= le32_to_cpu(ex
->ee_block
);
1338 *phys
= ext4_ext_pblock(ex
);
1342 /* go up and search for index to the right */
1343 while (--depth
>= 0) {
1344 ix
= path
[depth
].p_idx
;
1345 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1349 /* we've gone up to the root and found no index to the right */
1353 /* we've found index to the right, let's
1354 * follow it and find the closest allocated
1355 * block to the right */
1357 block
= ext4_idx_pblock(ix
);
1358 while (++depth
< path
->p_depth
) {
1359 bh
= sb_bread(inode
->i_sb
, block
);
1362 eh
= ext_block_hdr(bh
);
1363 /* subtract from p_depth to get proper eh_depth */
1364 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1368 ix
= EXT_FIRST_INDEX(eh
);
1369 block
= ext4_idx_pblock(ix
);
1373 bh
= sb_bread(inode
->i_sb
, block
);
1376 eh
= ext_block_hdr(bh
);
1377 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1381 ex
= EXT_FIRST_EXTENT(eh
);
1382 *logical
= le32_to_cpu(ex
->ee_block
);
1383 *phys
= ext4_ext_pblock(ex
);
1389 * ext4_ext_next_allocated_block:
1390 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1391 * NOTE: it considers block number from index entry as
1392 * allocated block. Thus, index entries have to be consistent
1396 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1400 BUG_ON(path
== NULL
);
1401 depth
= path
->p_depth
;
1403 if (depth
== 0 && path
->p_ext
== NULL
)
1404 return EXT_MAX_BLOCK
;
1406 while (depth
>= 0) {
1407 if (depth
== path
->p_depth
) {
1409 if (path
[depth
].p_ext
!=
1410 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1411 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1414 if (path
[depth
].p_idx
!=
1415 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1416 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1421 return EXT_MAX_BLOCK
;
1425 * ext4_ext_next_leaf_block:
1426 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1428 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1429 struct ext4_ext_path
*path
)
1433 BUG_ON(path
== NULL
);
1434 depth
= path
->p_depth
;
1436 /* zero-tree has no leaf blocks at all */
1438 return EXT_MAX_BLOCK
;
1440 /* go to index block */
1443 while (depth
>= 0) {
1444 if (path
[depth
].p_idx
!=
1445 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1446 return (ext4_lblk_t
)
1447 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1451 return EXT_MAX_BLOCK
;
1455 * ext4_ext_correct_indexes:
1456 * if leaf gets modified and modified extent is first in the leaf,
1457 * then we have to correct all indexes above.
1458 * TODO: do we need to correct tree in all cases?
1460 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1461 struct ext4_ext_path
*path
)
1463 struct ext4_extent_header
*eh
;
1464 int depth
= ext_depth(inode
);
1465 struct ext4_extent
*ex
;
1469 eh
= path
[depth
].p_hdr
;
1470 ex
= path
[depth
].p_ext
;
1472 if (unlikely(ex
== NULL
|| eh
== NULL
)) {
1473 EXT4_ERROR_INODE(inode
,
1474 "ex %p == NULL or eh %p == NULL", ex
, eh
);
1479 /* there is no tree at all */
1483 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1484 /* we correct tree if first leaf got modified only */
1489 * TODO: we need correction if border is smaller than current one
1492 border
= path
[depth
].p_ext
->ee_block
;
1493 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1496 path
[k
].p_idx
->ei_block
= border
;
1497 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1502 /* change all left-side indexes */
1503 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1505 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1508 path
[k
].p_idx
->ei_block
= border
;
1509 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1518 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1519 struct ext4_extent
*ex2
)
1521 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1524 * Make sure that either both extents are uninitialized, or
1527 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1530 if (ext4_ext_is_uninitialized(ex1
))
1531 max_len
= EXT_UNINIT_MAX_LEN
;
1533 max_len
= EXT_INIT_MAX_LEN
;
1535 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1536 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1538 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1539 le32_to_cpu(ex2
->ee_block
))
1543 * To allow future support for preallocated extents to be added
1544 * as an RO_COMPAT feature, refuse to merge to extents if
1545 * this can result in the top bit of ee_len being set.
1547 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1549 #ifdef AGGRESSIVE_TEST
1550 if (ext1_ee_len
>= 4)
1554 if (ext4_ext_pblock(ex1
) + ext1_ee_len
== ext4_ext_pblock(ex2
))
1560 * This function tries to merge the "ex" extent to the next extent in the tree.
1561 * It always tries to merge towards right. If you want to merge towards
1562 * left, pass "ex - 1" as argument instead of "ex".
1563 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1564 * 1 if they got merged.
1566 static int ext4_ext_try_to_merge_right(struct inode
*inode
,
1567 struct ext4_ext_path
*path
,
1568 struct ext4_extent
*ex
)
1570 struct ext4_extent_header
*eh
;
1571 unsigned int depth
, len
;
1573 int uninitialized
= 0;
1575 depth
= ext_depth(inode
);
1576 BUG_ON(path
[depth
].p_hdr
== NULL
);
1577 eh
= path
[depth
].p_hdr
;
1579 while (ex
< EXT_LAST_EXTENT(eh
)) {
1580 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1582 /* merge with next extent! */
1583 if (ext4_ext_is_uninitialized(ex
))
1585 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1586 + ext4_ext_get_actual_len(ex
+ 1));
1588 ext4_ext_mark_uninitialized(ex
);
1590 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1591 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1592 * sizeof(struct ext4_extent
);
1593 memmove(ex
+ 1, ex
+ 2, len
);
1595 le16_add_cpu(&eh
->eh_entries
, -1);
1597 WARN_ON(eh
->eh_entries
== 0);
1598 if (!eh
->eh_entries
)
1599 EXT4_ERROR_INODE(inode
, "eh->eh_entries = 0!");
1606 * This function tries to merge the @ex extent to neighbours in the tree.
1607 * return 1 if merge left else 0.
1609 static int ext4_ext_try_to_merge(struct inode
*inode
,
1610 struct ext4_ext_path
*path
,
1611 struct ext4_extent
*ex
) {
1612 struct ext4_extent_header
*eh
;
1617 depth
= ext_depth(inode
);
1618 BUG_ON(path
[depth
].p_hdr
== NULL
);
1619 eh
= path
[depth
].p_hdr
;
1621 if (ex
> EXT_FIRST_EXTENT(eh
))
1622 merge_done
= ext4_ext_try_to_merge_right(inode
, path
, ex
- 1);
1625 ret
= ext4_ext_try_to_merge_right(inode
, path
, ex
);
1631 * check if a portion of the "newext" extent overlaps with an
1634 * If there is an overlap discovered, it updates the length of the newext
1635 * such that there will be no overlap, and then returns 1.
1636 * If there is no overlap found, it returns 0.
1638 static unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1639 struct ext4_extent
*newext
,
1640 struct ext4_ext_path
*path
)
1643 unsigned int depth
, len1
;
1644 unsigned int ret
= 0;
1646 b1
= le32_to_cpu(newext
->ee_block
);
1647 len1
= ext4_ext_get_actual_len(newext
);
1648 depth
= ext_depth(inode
);
1649 if (!path
[depth
].p_ext
)
1651 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1654 * get the next allocated block if the extent in the path
1655 * is before the requested block(s)
1658 b2
= ext4_ext_next_allocated_block(path
);
1659 if (b2
== EXT_MAX_BLOCK
)
1663 /* check for wrap through zero on extent logical start block*/
1664 if (b1
+ len1
< b1
) {
1665 len1
= EXT_MAX_BLOCK
- b1
;
1666 newext
->ee_len
= cpu_to_le16(len1
);
1670 /* check for overlap */
1671 if (b1
+ len1
> b2
) {
1672 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1680 * ext4_ext_insert_extent:
1681 * tries to merge requsted extent into the existing extent or
1682 * inserts requested extent as new one into the tree,
1683 * creating new leaf in the no-space case.
1685 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1686 struct ext4_ext_path
*path
,
1687 struct ext4_extent
*newext
, int flag
)
1689 struct ext4_extent_header
*eh
;
1690 struct ext4_extent
*ex
, *fex
;
1691 struct ext4_extent
*nearex
; /* nearest extent */
1692 struct ext4_ext_path
*npath
= NULL
;
1693 int depth
, len
, err
;
1695 unsigned uninitialized
= 0;
1697 if (unlikely(ext4_ext_get_actual_len(newext
) == 0)) {
1698 EXT4_ERROR_INODE(inode
, "ext4_ext_get_actual_len(newext) == 0");
1701 depth
= ext_depth(inode
);
1702 ex
= path
[depth
].p_ext
;
1703 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1704 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1708 /* try to insert block into found extent and return */
1709 if (ex
&& !(flag
& EXT4_GET_BLOCKS_PRE_IO
)
1710 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1711 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1712 ext4_ext_is_uninitialized(newext
),
1713 ext4_ext_get_actual_len(newext
),
1714 le32_to_cpu(ex
->ee_block
),
1715 ext4_ext_is_uninitialized(ex
),
1716 ext4_ext_get_actual_len(ex
),
1717 ext4_ext_pblock(ex
));
1718 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1723 * ext4_can_extents_be_merged should have checked that either
1724 * both extents are uninitialized, or both aren't. Thus we
1725 * need to check only one of them here.
1727 if (ext4_ext_is_uninitialized(ex
))
1729 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1730 + ext4_ext_get_actual_len(newext
));
1732 ext4_ext_mark_uninitialized(ex
);
1733 eh
= path
[depth
].p_hdr
;
1739 depth
= ext_depth(inode
);
1740 eh
= path
[depth
].p_hdr
;
1741 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1744 /* probably next leaf has space for us? */
1745 fex
= EXT_LAST_EXTENT(eh
);
1746 next
= ext4_ext_next_leaf_block(inode
, path
);
1747 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1748 && next
!= EXT_MAX_BLOCK
) {
1749 ext_debug("next leaf block - %d\n", next
);
1750 BUG_ON(npath
!= NULL
);
1751 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1753 return PTR_ERR(npath
);
1754 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1755 eh
= npath
[depth
].p_hdr
;
1756 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1757 ext_debug("next leaf isn't full(%d)\n",
1758 le16_to_cpu(eh
->eh_entries
));
1762 ext_debug("next leaf has no free space(%d,%d)\n",
1763 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1767 * There is no free space in the found leaf.
1768 * We're gonna add a new leaf in the tree.
1770 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1773 depth
= ext_depth(inode
);
1774 eh
= path
[depth
].p_hdr
;
1777 nearex
= path
[depth
].p_ext
;
1779 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1784 /* there is no extent in this leaf, create first one */
1785 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1786 le32_to_cpu(newext
->ee_block
),
1787 ext4_ext_pblock(newext
),
1788 ext4_ext_is_uninitialized(newext
),
1789 ext4_ext_get_actual_len(newext
));
1790 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1791 } else if (le32_to_cpu(newext
->ee_block
)
1792 > le32_to_cpu(nearex
->ee_block
)) {
1793 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1794 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1795 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1796 len
= (len
- 1) * sizeof(struct ext4_extent
);
1797 len
= len
< 0 ? 0 : len
;
1798 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1799 "move %d from 0x%p to 0x%p\n",
1800 le32_to_cpu(newext
->ee_block
),
1801 ext4_ext_pblock(newext
),
1802 ext4_ext_is_uninitialized(newext
),
1803 ext4_ext_get_actual_len(newext
),
1804 nearex
, len
, nearex
+ 1, nearex
+ 2);
1805 memmove(nearex
+ 2, nearex
+ 1, len
);
1807 path
[depth
].p_ext
= nearex
+ 1;
1809 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1810 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1811 len
= len
< 0 ? 0 : len
;
1812 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1813 "move %d from 0x%p to 0x%p\n",
1814 le32_to_cpu(newext
->ee_block
),
1815 ext4_ext_pblock(newext
),
1816 ext4_ext_is_uninitialized(newext
),
1817 ext4_ext_get_actual_len(newext
),
1818 nearex
, len
, nearex
+ 1, nearex
+ 2);
1819 memmove(nearex
+ 1, nearex
, len
);
1820 path
[depth
].p_ext
= nearex
;
1823 le16_add_cpu(&eh
->eh_entries
, 1);
1824 nearex
= path
[depth
].p_ext
;
1825 nearex
->ee_block
= newext
->ee_block
;
1826 ext4_ext_store_pblock(nearex
, ext4_ext_pblock(newext
));
1827 nearex
->ee_len
= newext
->ee_len
;
1830 /* try to merge extents to the right */
1831 if (!(flag
& EXT4_GET_BLOCKS_PRE_IO
))
1832 ext4_ext_try_to_merge(inode
, path
, nearex
);
1834 /* try to merge extents to the left */
1836 /* time to correct all indexes above */
1837 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1841 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1845 ext4_ext_drop_refs(npath
);
1848 ext4_ext_invalidate_cache(inode
);
1852 static int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1853 ext4_lblk_t num
, ext_prepare_callback func
,
1856 struct ext4_ext_path
*path
= NULL
;
1857 struct ext4_ext_cache cbex
;
1858 struct ext4_extent
*ex
;
1859 ext4_lblk_t next
, start
= 0, end
= 0;
1860 ext4_lblk_t last
= block
+ num
;
1861 int depth
, exists
, err
= 0;
1863 BUG_ON(func
== NULL
);
1864 BUG_ON(inode
== NULL
);
1866 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1868 /* find extent for this block */
1869 down_read(&EXT4_I(inode
)->i_data_sem
);
1870 path
= ext4_ext_find_extent(inode
, block
, path
);
1871 up_read(&EXT4_I(inode
)->i_data_sem
);
1873 err
= PTR_ERR(path
);
1878 depth
= ext_depth(inode
);
1879 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1880 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1884 ex
= path
[depth
].p_ext
;
1885 next
= ext4_ext_next_allocated_block(path
);
1889 /* there is no extent yet, so try to allocate
1890 * all requested space */
1893 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1894 /* need to allocate space before found extent */
1896 end
= le32_to_cpu(ex
->ee_block
);
1897 if (block
+ num
< end
)
1899 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1900 + ext4_ext_get_actual_len(ex
)) {
1901 /* need to allocate space after found extent */
1906 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1908 * some part of requested space is covered
1912 end
= le32_to_cpu(ex
->ee_block
)
1913 + ext4_ext_get_actual_len(ex
);
1914 if (block
+ num
< end
)
1920 BUG_ON(end
<= start
);
1923 cbex
.ec_block
= start
;
1924 cbex
.ec_len
= end
- start
;
1927 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1928 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1929 cbex
.ec_start
= ext4_ext_pblock(ex
);
1932 if (unlikely(cbex
.ec_len
== 0)) {
1933 EXT4_ERROR_INODE(inode
, "cbex.ec_len == 0");
1937 err
= func(inode
, path
, &cbex
, ex
, cbdata
);
1938 ext4_ext_drop_refs(path
);
1943 if (err
== EXT_REPEAT
)
1945 else if (err
== EXT_BREAK
) {
1950 if (ext_depth(inode
) != depth
) {
1951 /* depth was changed. we have to realloc path */
1956 block
= cbex
.ec_block
+ cbex
.ec_len
;
1960 ext4_ext_drop_refs(path
);
1968 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1969 __u32 len
, ext4_fsblk_t start
)
1971 struct ext4_ext_cache
*cex
;
1973 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1974 cex
= &EXT4_I(inode
)->i_cached_extent
;
1975 cex
->ec_block
= block
;
1977 cex
->ec_start
= start
;
1978 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1982 * ext4_ext_put_gap_in_cache:
1983 * calculate boundaries of the gap that the requested block fits into
1984 * and cache this gap
1987 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1990 int depth
= ext_depth(inode
);
1993 struct ext4_extent
*ex
;
1995 ex
= path
[depth
].p_ext
;
1997 /* there is no extent yet, so gap is [0;-] */
1999 len
= EXT_MAX_BLOCK
;
2000 ext_debug("cache gap(whole file):");
2001 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
2003 len
= le32_to_cpu(ex
->ee_block
) - block
;
2004 ext_debug("cache gap(before): %u [%u:%u]",
2006 le32_to_cpu(ex
->ee_block
),
2007 ext4_ext_get_actual_len(ex
));
2008 } else if (block
>= le32_to_cpu(ex
->ee_block
)
2009 + ext4_ext_get_actual_len(ex
)) {
2011 lblock
= le32_to_cpu(ex
->ee_block
)
2012 + ext4_ext_get_actual_len(ex
);
2014 next
= ext4_ext_next_allocated_block(path
);
2015 ext_debug("cache gap(after): [%u:%u] %u",
2016 le32_to_cpu(ex
->ee_block
),
2017 ext4_ext_get_actual_len(ex
),
2019 BUG_ON(next
== lblock
);
2020 len
= next
- lblock
;
2026 ext_debug(" -> %u:%lu\n", lblock
, len
);
2027 ext4_ext_put_in_cache(inode
, lblock
, len
, 0);
2031 * Return 0 if cache is invalid; 1 if the cache is valid
2034 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
2035 struct ext4_extent
*ex
)
2037 struct ext4_ext_cache
*cex
;
2038 struct ext4_sb_info
*sbi
;
2042 * We borrow i_block_reservation_lock to protect i_cached_extent
2044 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
2045 cex
= &EXT4_I(inode
)->i_cached_extent
;
2046 sbi
= EXT4_SB(inode
->i_sb
);
2048 /* has cache valid data? */
2049 if (cex
->ec_len
== 0)
2052 if (in_range(block
, cex
->ec_block
, cex
->ec_len
)) {
2053 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
2054 ext4_ext_store_pblock(ex
, cex
->ec_start
);
2055 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
2056 ext_debug("%u cached by %u:%u:%llu\n",
2058 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
2063 sbi
->extent_cache_misses
++;
2065 sbi
->extent_cache_hits
++;
2066 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
2072 * removes index from the index block.
2073 * It's used in truncate case only, thus all requests are for
2074 * last index in the block only.
2076 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
2077 struct ext4_ext_path
*path
)
2082 /* free index block */
2084 leaf
= ext4_idx_pblock(path
->p_idx
);
2085 if (unlikely(path
->p_hdr
->eh_entries
== 0)) {
2086 EXT4_ERROR_INODE(inode
, "path->p_hdr->eh_entries == 0");
2089 err
= ext4_ext_get_access(handle
, inode
, path
);
2092 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
2093 err
= ext4_ext_dirty(handle
, inode
, path
);
2096 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
2097 ext4_free_blocks(handle
, inode
, NULL
, leaf
, 1,
2098 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
2103 * ext4_ext_calc_credits_for_single_extent:
2104 * This routine returns max. credits that needed to insert an extent
2105 * to the extent tree.
2106 * When pass the actual path, the caller should calculate credits
2109 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2110 struct ext4_ext_path
*path
)
2113 int depth
= ext_depth(inode
);
2116 /* probably there is space in leaf? */
2117 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2118 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2121 * There are some space in the leaf tree, no
2122 * need to account for leaf block credit
2124 * bitmaps and block group descriptor blocks
2125 * and other metadat blocks still need to be
2128 /* 1 bitmap, 1 block group descriptor */
2129 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2134 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2138 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2140 * if nrblocks are fit in a single extent (chunk flag is 1), then
2141 * in the worse case, each tree level index/leaf need to be changed
2142 * if the tree split due to insert a new extent, then the old tree
2143 * index/leaf need to be updated too
2145 * If the nrblocks are discontiguous, they could cause
2146 * the whole tree split more than once, but this is really rare.
2148 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2151 int depth
= ext_depth(inode
);
2161 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2162 struct ext4_extent
*ex
,
2163 ext4_lblk_t from
, ext4_lblk_t to
)
2165 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2166 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2168 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2169 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2170 #ifdef EXTENTS_STATS
2172 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2173 spin_lock(&sbi
->s_ext_stats_lock
);
2174 sbi
->s_ext_blocks
+= ee_len
;
2175 sbi
->s_ext_extents
++;
2176 if (ee_len
< sbi
->s_ext_min
)
2177 sbi
->s_ext_min
= ee_len
;
2178 if (ee_len
> sbi
->s_ext_max
)
2179 sbi
->s_ext_max
= ee_len
;
2180 if (ext_depth(inode
) > sbi
->s_depth_max
)
2181 sbi
->s_depth_max
= ext_depth(inode
);
2182 spin_unlock(&sbi
->s_ext_stats_lock
);
2185 if (from
>= le32_to_cpu(ex
->ee_block
)
2186 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2191 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2192 start
= ext4_ext_pblock(ex
) + ee_len
- num
;
2193 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2194 ext4_free_blocks(handle
, inode
, NULL
, start
, num
, flags
);
2195 } else if (from
== le32_to_cpu(ex
->ee_block
)
2196 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2197 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
2198 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2200 printk(KERN_INFO
"strange request: removal(2) "
2201 "%u-%u from %u:%u\n",
2202 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2208 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2209 struct ext4_ext_path
*path
, ext4_lblk_t start
)
2211 int err
= 0, correct_index
= 0;
2212 int depth
= ext_depth(inode
), credits
;
2213 struct ext4_extent_header
*eh
;
2214 ext4_lblk_t a
, b
, block
;
2216 ext4_lblk_t ex_ee_block
;
2217 unsigned short ex_ee_len
;
2218 unsigned uninitialized
= 0;
2219 struct ext4_extent
*ex
;
2221 /* the header must be checked already in ext4_ext_remove_space() */
2222 ext_debug("truncate since %u in leaf\n", start
);
2223 if (!path
[depth
].p_hdr
)
2224 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2225 eh
= path
[depth
].p_hdr
;
2226 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2227 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2230 /* find where to start removing */
2231 ex
= EXT_LAST_EXTENT(eh
);
2233 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2234 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2236 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2237 ex_ee_block
+ ex_ee_len
> start
) {
2239 if (ext4_ext_is_uninitialized(ex
))
2244 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2245 uninitialized
, ex_ee_len
);
2246 path
[depth
].p_ext
= ex
;
2248 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2249 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
2250 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
2252 ext_debug(" border %u:%u\n", a
, b
);
2254 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
2258 } else if (a
!= ex_ee_block
) {
2259 /* remove tail of the extent */
2260 block
= ex_ee_block
;
2262 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2263 /* remove head of the extent */
2266 /* there is no "make a hole" API yet */
2269 /* remove whole extent: excellent! */
2270 block
= ex_ee_block
;
2272 BUG_ON(a
!= ex_ee_block
);
2273 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
2277 * 3 for leaf, sb, and inode plus 2 (bmap and group
2278 * descriptor) for each block group; assume two block
2279 * groups plus ex_ee_len/blocks_per_block_group for
2282 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2283 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2285 credits
+= (ext_depth(inode
)) + 1;
2287 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2289 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2293 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2297 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2302 /* this extent is removed; mark slot entirely unused */
2303 ext4_ext_store_pblock(ex
, 0);
2304 le16_add_cpu(&eh
->eh_entries
, -1);
2307 ex
->ee_block
= cpu_to_le32(block
);
2308 ex
->ee_len
= cpu_to_le16(num
);
2310 * Do not mark uninitialized if all the blocks in the
2311 * extent have been removed.
2313 if (uninitialized
&& num
)
2314 ext4_ext_mark_uninitialized(ex
);
2316 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2320 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2321 ext4_ext_pblock(ex
));
2323 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2324 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2327 if (correct_index
&& eh
->eh_entries
)
2328 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2330 /* if this leaf is free, then we should
2331 * remove it from index block above */
2332 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2333 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2340 * ext4_ext_more_to_rm:
2341 * returns 1 if current index has to be freed (even partial)
2344 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2346 BUG_ON(path
->p_idx
== NULL
);
2348 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2352 * if truncate on deeper level happened, it wasn't partial,
2353 * so we have to consider current index for truncation
2355 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2360 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2362 struct super_block
*sb
= inode
->i_sb
;
2363 int depth
= ext_depth(inode
);
2364 struct ext4_ext_path
*path
;
2368 ext_debug("truncate since %u\n", start
);
2370 /* probably first extent we're gonna free will be last in block */
2371 handle
= ext4_journal_start(inode
, depth
+ 1);
2373 return PTR_ERR(handle
);
2376 ext4_ext_invalidate_cache(inode
);
2379 * We start scanning from right side, freeing all the blocks
2380 * after i_size and walking into the tree depth-wise.
2382 depth
= ext_depth(inode
);
2383 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2385 ext4_journal_stop(handle
);
2388 path
[0].p_depth
= depth
;
2389 path
[0].p_hdr
= ext_inode_hdr(inode
);
2390 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2396 while (i
>= 0 && err
== 0) {
2398 /* this is leaf block */
2399 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2400 /* root level has p_bh == NULL, brelse() eats this */
2401 brelse(path
[i
].p_bh
);
2402 path
[i
].p_bh
= NULL
;
2407 /* this is index block */
2408 if (!path
[i
].p_hdr
) {
2409 ext_debug("initialize header\n");
2410 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2413 if (!path
[i
].p_idx
) {
2414 /* this level hasn't been touched yet */
2415 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2416 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2417 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2419 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2421 /* we were already here, see at next index */
2425 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2426 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2428 if (ext4_ext_more_to_rm(path
+ i
)) {
2429 struct buffer_head
*bh
;
2430 /* go to the next level */
2431 ext_debug("move to level %d (block %llu)\n",
2432 i
+ 1, ext4_idx_pblock(path
[i
].p_idx
));
2433 memset(path
+ i
+ 1, 0, sizeof(*path
));
2434 bh
= sb_bread(sb
, ext4_idx_pblock(path
[i
].p_idx
));
2436 /* should we reset i_size? */
2440 if (WARN_ON(i
+ 1 > depth
)) {
2444 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2449 path
[i
+ 1].p_bh
= bh
;
2451 /* save actual number of indexes since this
2452 * number is changed at the next iteration */
2453 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2456 /* we finished processing this index, go up */
2457 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2458 /* index is empty, remove it;
2459 * handle must be already prepared by the
2460 * truncatei_leaf() */
2461 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2463 /* root level has p_bh == NULL, brelse() eats this */
2464 brelse(path
[i
].p_bh
);
2465 path
[i
].p_bh
= NULL
;
2467 ext_debug("return to level %d\n", i
);
2471 /* TODO: flexible tree reduction should be here */
2472 if (path
->p_hdr
->eh_entries
== 0) {
2474 * truncate to zero freed all the tree,
2475 * so we need to correct eh_depth
2477 err
= ext4_ext_get_access(handle
, inode
, path
);
2479 ext_inode_hdr(inode
)->eh_depth
= 0;
2480 ext_inode_hdr(inode
)->eh_max
=
2481 cpu_to_le16(ext4_ext_space_root(inode
, 0));
2482 err
= ext4_ext_dirty(handle
, inode
, path
);
2486 ext4_ext_drop_refs(path
);
2490 ext4_journal_stop(handle
);
2496 * called at mount time
2498 void ext4_ext_init(struct super_block
*sb
)
2501 * possible initialization would be here
2504 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2505 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2506 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2507 #ifdef AGGRESSIVE_TEST
2508 printk(", aggressive tests");
2510 #ifdef CHECK_BINSEARCH
2511 printk(", check binsearch");
2513 #ifdef EXTENTS_STATS
2518 #ifdef EXTENTS_STATS
2519 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2520 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2521 EXT4_SB(sb
)->s_ext_max
= 0;
2527 * called at umount time
2529 void ext4_ext_release(struct super_block
*sb
)
2531 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2534 #ifdef EXTENTS_STATS
2535 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2536 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2537 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2538 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2539 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2540 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2541 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2546 /* FIXME!! we need to try to merge to left or right after zero-out */
2547 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2549 ext4_fsblk_t ee_pblock
;
2550 unsigned int ee_len
;
2553 ee_len
= ext4_ext_get_actual_len(ex
);
2554 ee_pblock
= ext4_ext_pblock(ex
);
2556 ret
= sb_issue_zeroout(inode
->i_sb
, ee_pblock
, ee_len
, GFP_NOFS
);
2564 * used by extent splitting.
2566 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2568 #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2569 #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2572 * ext4_split_extent_at() splits an extent at given block.
2574 * @handle: the journal handle
2575 * @inode: the file inode
2576 * @path: the path to the extent
2577 * @split: the logical block where the extent is splitted.
2578 * @split_flags: indicates if the extent could be zeroout if split fails, and
2579 * the states(init or uninit) of new extents.
2580 * @flags: flags used to insert new extent to extent tree.
2583 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2584 * of which are deterimined by split_flag.
2586 * There are two cases:
2587 * a> the extent are splitted into two extent.
2588 * b> split is not needed, and just mark the extent.
2590 * return 0 on success.
2592 static int ext4_split_extent_at(handle_t
*handle
,
2593 struct inode
*inode
,
2594 struct ext4_ext_path
*path
,
2599 ext4_fsblk_t newblock
;
2600 ext4_lblk_t ee_block
;
2601 struct ext4_extent
*ex
, newex
, orig_ex
;
2602 struct ext4_extent
*ex2
= NULL
;
2603 unsigned int ee_len
, depth
;
2606 ext_debug("ext4_split_extents_at: inode %lu, logical"
2607 "block %llu\n", inode
->i_ino
, (unsigned long long)split
);
2609 ext4_ext_show_leaf(inode
, path
);
2611 depth
= ext_depth(inode
);
2612 ex
= path
[depth
].p_ext
;
2613 ee_block
= le32_to_cpu(ex
->ee_block
);
2614 ee_len
= ext4_ext_get_actual_len(ex
);
2615 newblock
= split
- ee_block
+ ext4_ext_pblock(ex
);
2617 BUG_ON(split
< ee_block
|| split
>= (ee_block
+ ee_len
));
2619 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2623 if (split
== ee_block
) {
2625 * case b: block @split is the block that the extent begins with
2626 * then we just change the state of the extent, and splitting
2629 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2630 ext4_ext_mark_uninitialized(ex
);
2632 ext4_ext_mark_initialized(ex
);
2634 if (!(flags
& EXT4_GET_BLOCKS_PRE_IO
))
2635 ext4_ext_try_to_merge(inode
, path
, ex
);
2637 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2642 memcpy(&orig_ex
, ex
, sizeof(orig_ex
));
2643 ex
->ee_len
= cpu_to_le16(split
- ee_block
);
2644 if (split_flag
& EXT4_EXT_MARK_UNINIT1
)
2645 ext4_ext_mark_uninitialized(ex
);
2648 * path may lead to new leaf, not to original leaf any more
2649 * after ext4_ext_insert_extent() returns,
2651 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2653 goto fix_extent_len
;
2656 ex2
->ee_block
= cpu_to_le32(split
);
2657 ex2
->ee_len
= cpu_to_le16(ee_len
- (split
- ee_block
));
2658 ext4_ext_store_pblock(ex2
, newblock
);
2659 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2660 ext4_ext_mark_uninitialized(ex2
);
2662 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
2663 if (err
== -ENOSPC
&& (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2664 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2666 goto fix_extent_len
;
2667 /* update the extent length and mark as initialized */
2668 ex
->ee_len
= cpu_to_le32(ee_len
);
2669 ext4_ext_try_to_merge(inode
, path
, ex
);
2670 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2673 goto fix_extent_len
;
2676 ext4_ext_show_leaf(inode
, path
);
2680 ex
->ee_len
= orig_ex
.ee_len
;
2681 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2686 * ext4_split_extents() splits an extent and mark extent which is covered
2687 * by @map as split_flags indicates
2689 * It may result in splitting the extent into multiple extents (upto three)
2690 * There are three possibilities:
2691 * a> There is no split required
2692 * b> Splits in two extents: Split is happening at either end of the extent
2693 * c> Splits in three extents: Somone is splitting in middle of the extent
2696 static int ext4_split_extent(handle_t
*handle
,
2697 struct inode
*inode
,
2698 struct ext4_ext_path
*path
,
2699 struct ext4_map_blocks
*map
,
2703 ext4_lblk_t ee_block
;
2704 struct ext4_extent
*ex
;
2705 unsigned int ee_len
, depth
;
2708 int split_flag1
, flags1
;
2710 depth
= ext_depth(inode
);
2711 ex
= path
[depth
].p_ext
;
2712 ee_block
= le32_to_cpu(ex
->ee_block
);
2713 ee_len
= ext4_ext_get_actual_len(ex
);
2714 uninitialized
= ext4_ext_is_uninitialized(ex
);
2716 if (map
->m_lblk
+ map
->m_len
< ee_block
+ ee_len
) {
2717 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
?
2718 EXT4_EXT_MAY_ZEROOUT
: 0;
2719 flags1
= flags
| EXT4_GET_BLOCKS_PRE_IO
;
2721 split_flag1
|= EXT4_EXT_MARK_UNINIT1
|
2722 EXT4_EXT_MARK_UNINIT2
;
2723 err
= ext4_split_extent_at(handle
, inode
, path
,
2724 map
->m_lblk
+ map
->m_len
, split_flag1
, flags1
);
2729 ext4_ext_drop_refs(path
);
2730 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, path
);
2732 return PTR_ERR(path
);
2734 if (map
->m_lblk
>= ee_block
) {
2735 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
?
2736 EXT4_EXT_MAY_ZEROOUT
: 0;
2738 split_flag1
|= EXT4_EXT_MARK_UNINIT1
;
2739 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2740 split_flag1
|= EXT4_EXT_MARK_UNINIT2
;
2741 err
= ext4_split_extent_at(handle
, inode
, path
,
2742 map
->m_lblk
, split_flag1
, flags
);
2747 ext4_ext_show_leaf(inode
, path
);
2749 return err
? err
: map
->m_len
;
2752 #define EXT4_EXT_ZERO_LEN 7
2754 * This function is called by ext4_ext_map_blocks() if someone tries to write
2755 * to an uninitialized extent. It may result in splitting the uninitialized
2756 * extent into multiple extents (up to three - one initialized and two
2758 * There are three possibilities:
2759 * a> There is no split required: Entire extent should be initialized
2760 * b> Splits in two extents: Write is happening at either end of the extent
2761 * c> Splits in three extents: Somone is writing in middle of the extent
2763 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2764 struct inode
*inode
,
2765 struct ext4_map_blocks
*map
,
2766 struct ext4_ext_path
*path
)
2768 struct ext4_map_blocks split_map
;
2769 struct ext4_extent zero_ex
;
2770 struct ext4_extent
*ex
;
2771 ext4_lblk_t ee_block
, eof_block
;
2772 unsigned int allocated
, ee_len
, depth
;
2776 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2777 "block %llu, max_blocks %u\n", inode
->i_ino
,
2778 (unsigned long long)map
->m_lblk
, map
->m_len
);
2780 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2781 inode
->i_sb
->s_blocksize_bits
;
2782 if (eof_block
< map
->m_lblk
+ map
->m_len
)
2783 eof_block
= map
->m_lblk
+ map
->m_len
;
2785 depth
= ext_depth(inode
);
2786 ex
= path
[depth
].p_ext
;
2787 ee_block
= le32_to_cpu(ex
->ee_block
);
2788 ee_len
= ext4_ext_get_actual_len(ex
);
2789 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
2791 WARN_ON(map
->m_lblk
< ee_block
);
2793 * It is safe to convert extent to initialized via explicit
2794 * zeroout only if extent is fully insde i_size or new_size.
2796 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
2798 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2799 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
&&
2800 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2801 err
= ext4_ext_zeroout(inode
, ex
);
2805 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2808 ext4_ext_mark_initialized(ex
);
2809 ext4_ext_try_to_merge(inode
, path
, ex
);
2810 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2816 * 1. split the extent into three extents.
2817 * 2. split the extent into two extents, zeroout the first half.
2818 * 3. split the extent into two extents, zeroout the second half.
2819 * 4. split the extent into two extents with out zeroout.
2821 split_map
.m_lblk
= map
->m_lblk
;
2822 split_map
.m_len
= map
->m_len
;
2824 if (allocated
> map
->m_len
) {
2825 if (allocated
<= EXT4_EXT_ZERO_LEN
&&
2826 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2829 cpu_to_le32(map
->m_lblk
);
2830 zero_ex
.ee_len
= cpu_to_le16(allocated
);
2831 ext4_ext_store_pblock(&zero_ex
,
2832 ext4_ext_pblock(ex
) + map
->m_lblk
- ee_block
);
2833 err
= ext4_ext_zeroout(inode
, &zero_ex
);
2836 split_map
.m_lblk
= map
->m_lblk
;
2837 split_map
.m_len
= allocated
;
2838 } else if ((map
->m_lblk
- ee_block
+ map
->m_len
<
2839 EXT4_EXT_ZERO_LEN
) &&
2840 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2842 if (map
->m_lblk
!= ee_block
) {
2843 zero_ex
.ee_block
= ex
->ee_block
;
2844 zero_ex
.ee_len
= cpu_to_le16(map
->m_lblk
-
2846 ext4_ext_store_pblock(&zero_ex
,
2847 ext4_ext_pblock(ex
));
2848 err
= ext4_ext_zeroout(inode
, &zero_ex
);
2853 split_map
.m_lblk
= ee_block
;
2854 split_map
.m_len
= map
->m_lblk
- ee_block
+ map
->m_len
;
2855 allocated
= map
->m_len
;
2859 allocated
= ext4_split_extent(handle
, inode
, path
,
2860 &split_map
, split_flag
, 0);
2865 return err
? err
: allocated
;
2869 * This function is called by ext4_ext_map_blocks() from
2870 * ext4_get_blocks_dio_write() when DIO to write
2871 * to an uninitialized extent.
2873 * Writing to an uninitialized extent may result in splitting the uninitialized
2874 * extent into multiple /initialized uninitialized extents (up to three)
2875 * There are three possibilities:
2876 * a> There is no split required: Entire extent should be uninitialized
2877 * b> Splits in two extents: Write is happening at either end of the extent
2878 * c> Splits in three extents: Somone is writing in middle of the extent
2880 * One of more index blocks maybe needed if the extent tree grow after
2881 * the uninitialized extent split. To prevent ENOSPC occur at the IO
2882 * complete, we need to split the uninitialized extent before DIO submit
2883 * the IO. The uninitialized extent called at this time will be split
2884 * into three uninitialized extent(at most). After IO complete, the part
2885 * being filled will be convert to initialized by the end_io callback function
2886 * via ext4_convert_unwritten_extents().
2888 * Returns the size of uninitialized extent to be written on success.
2890 static int ext4_split_unwritten_extents(handle_t
*handle
,
2891 struct inode
*inode
,
2892 struct ext4_map_blocks
*map
,
2893 struct ext4_ext_path
*path
,
2896 ext4_lblk_t eof_block
;
2897 ext4_lblk_t ee_block
;
2898 struct ext4_extent
*ex
;
2899 unsigned int ee_len
;
2900 int split_flag
= 0, depth
;
2902 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2903 "block %llu, max_blocks %u\n", inode
->i_ino
,
2904 (unsigned long long)map
->m_lblk
, map
->m_len
);
2906 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2907 inode
->i_sb
->s_blocksize_bits
;
2908 if (eof_block
< map
->m_lblk
+ map
->m_len
)
2909 eof_block
= map
->m_lblk
+ map
->m_len
;
2911 * It is safe to convert extent to initialized via explicit
2912 * zeroout only if extent is fully insde i_size or new_size.
2914 depth
= ext_depth(inode
);
2915 ex
= path
[depth
].p_ext
;
2916 ee_block
= le32_to_cpu(ex
->ee_block
);
2917 ee_len
= ext4_ext_get_actual_len(ex
);
2919 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
2920 split_flag
|= EXT4_EXT_MARK_UNINIT2
;
2922 flags
|= EXT4_GET_BLOCKS_PRE_IO
;
2923 return ext4_split_extent(handle
, inode
, path
, map
, split_flag
, flags
);
2926 static int ext4_convert_unwritten_extents_endio(handle_t
*handle
,
2927 struct inode
*inode
,
2928 struct ext4_ext_path
*path
)
2930 struct ext4_extent
*ex
;
2931 struct ext4_extent_header
*eh
;
2935 depth
= ext_depth(inode
);
2936 eh
= path
[depth
].p_hdr
;
2937 ex
= path
[depth
].p_ext
;
2939 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
2940 "block %llu, max_blocks %u\n", inode
->i_ino
,
2941 (unsigned long long)le32_to_cpu(ex
->ee_block
),
2942 ext4_ext_get_actual_len(ex
));
2944 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2947 /* first mark the extent as initialized */
2948 ext4_ext_mark_initialized(ex
);
2950 /* note: ext4_ext_correct_indexes() isn't needed here because
2951 * borders are not changed
2953 ext4_ext_try_to_merge(inode
, path
, ex
);
2955 /* Mark modified extent as dirty */
2956 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2958 ext4_ext_show_leaf(inode
, path
);
2962 static void unmap_underlying_metadata_blocks(struct block_device
*bdev
,
2963 sector_t block
, int count
)
2966 for (i
= 0; i
< count
; i
++)
2967 unmap_underlying_metadata(bdev
, block
+ i
);
2971 * Handle EOFBLOCKS_FL flag, clearing it if necessary
2973 static int check_eofblocks_fl(handle_t
*handle
, struct inode
*inode
,
2975 struct ext4_ext_path
*path
,
2979 struct ext4_extent_header
*eh
;
2980 struct ext4_extent
*last_ex
;
2982 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))
2985 depth
= ext_depth(inode
);
2986 eh
= path
[depth
].p_hdr
;
2988 if (unlikely(!eh
->eh_entries
)) {
2989 EXT4_ERROR_INODE(inode
, "eh->eh_entries == 0 and "
2990 "EOFBLOCKS_FL set");
2993 last_ex
= EXT_LAST_EXTENT(eh
);
2995 * We should clear the EOFBLOCKS_FL flag if we are writing the
2996 * last block in the last extent in the file. We test this by
2997 * first checking to see if the caller to
2998 * ext4_ext_get_blocks() was interested in the last block (or
2999 * a block beyond the last block) in the current extent. If
3000 * this turns out to be false, we can bail out from this
3001 * function immediately.
3003 if (lblk
+ len
< le32_to_cpu(last_ex
->ee_block
) +
3004 ext4_ext_get_actual_len(last_ex
))
3007 * If the caller does appear to be planning to write at or
3008 * beyond the end of the current extent, we then test to see
3009 * if the current extent is the last extent in the file, by
3010 * checking to make sure it was reached via the rightmost node
3011 * at each level of the tree.
3013 for (i
= depth
-1; i
>= 0; i
--)
3014 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3016 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3017 return ext4_mark_inode_dirty(handle
, inode
);
3021 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3022 struct ext4_map_blocks
*map
,
3023 struct ext4_ext_path
*path
, int flags
,
3024 unsigned int allocated
, ext4_fsblk_t newblock
)
3028 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3030 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3031 "block %llu, max_blocks %u, flags %d, allocated %u",
3032 inode
->i_ino
, (unsigned long long)map
->m_lblk
, map
->m_len
,
3034 ext4_ext_show_leaf(inode
, path
);
3036 /* get_block() before submit the IO, split the extent */
3037 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3038 ret
= ext4_split_unwritten_extents(handle
, inode
, map
,
3041 * Flag the inode(non aio case) or end_io struct (aio case)
3042 * that this IO needs to conversion to written when IO is
3045 if (io
&& !(io
->flag
& EXT4_IO_END_UNWRITTEN
)) {
3046 io
->flag
= EXT4_IO_END_UNWRITTEN
;
3047 atomic_inc(&EXT4_I(inode
)->i_aiodio_unwritten
);
3049 ext4_set_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3050 if (ext4_should_dioread_nolock(inode
))
3051 map
->m_flags
|= EXT4_MAP_UNINIT
;
3054 /* IO end_io complete, convert the filled extent to written */
3055 if ((flags
& EXT4_GET_BLOCKS_CONVERT
)) {
3056 ret
= ext4_convert_unwritten_extents_endio(handle
, inode
,
3059 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3060 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
3066 /* buffered IO case */
3068 * repeat fallocate creation request
3069 * we already have an unwritten extent
3071 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3074 /* buffered READ or buffered write_begin() lookup */
3075 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3077 * We have blocks reserved already. We
3078 * return allocated blocks so that delalloc
3079 * won't do block reservation for us. But
3080 * the buffer head will be unmapped so that
3081 * a read from the block returns 0s.
3083 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
3087 /* buffered write, writepage time, convert*/
3088 ret
= ext4_ext_convert_to_initialized(handle
, inode
, map
, path
);
3090 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3091 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
,
3103 map
->m_flags
|= EXT4_MAP_NEW
;
3105 * if we allocated more blocks than requested
3106 * we need to make sure we unmap the extra block
3107 * allocated. The actual needed block will get
3108 * unmapped later when we find the buffer_head marked
3111 if (allocated
> map
->m_len
) {
3112 unmap_underlying_metadata_blocks(inode
->i_sb
->s_bdev
,
3113 newblock
+ map
->m_len
,
3114 allocated
- map
->m_len
);
3115 allocated
= map
->m_len
;
3119 * If we have done fallocate with the offset that is already
3120 * delayed allocated, we would have block reservation
3121 * and quota reservation done in the delayed write path.
3122 * But fallocate would have already updated quota and block
3123 * count for this offset. So cancel these reservation
3125 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3126 ext4_da_update_reserve_space(inode
, allocated
, 0);
3129 map
->m_flags
|= EXT4_MAP_MAPPED
;
3131 if (allocated
> map
->m_len
)
3132 allocated
= map
->m_len
;
3133 ext4_ext_show_leaf(inode
, path
);
3134 map
->m_pblk
= newblock
;
3135 map
->m_len
= allocated
;
3138 ext4_ext_drop_refs(path
);
3141 return err
? err
: allocated
;
3145 * Block allocation/map/preallocation routine for extents based files
3148 * Need to be called with
3149 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3150 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3152 * return > 0, number of of blocks already mapped/allocated
3153 * if create == 0 and these are pre-allocated blocks
3154 * buffer head is unmapped
3155 * otherwise blocks are mapped
3157 * return = 0, if plain look up failed (blocks have not been allocated)
3158 * buffer head is unmapped
3160 * return < 0, error case.
3162 int ext4_ext_map_blocks(handle_t
*handle
, struct inode
*inode
,
3163 struct ext4_map_blocks
*map
, int flags
)
3165 struct ext4_ext_path
*path
= NULL
;
3166 struct ext4_extent newex
, *ex
;
3167 ext4_fsblk_t newblock
= 0;
3168 int err
= 0, depth
, ret
;
3169 unsigned int allocated
= 0;
3170 struct ext4_allocation_request ar
;
3171 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3173 ext_debug("blocks %u/%u requested for inode %lu\n",
3174 map
->m_lblk
, map
->m_len
, inode
->i_ino
);
3175 trace_ext4_ext_map_blocks_enter(inode
, map
->m_lblk
, map
->m_len
, flags
);
3177 /* check in cache */
3178 if (ext4_ext_in_cache(inode
, map
->m_lblk
, &newex
)) {
3179 if (!newex
.ee_start_lo
&& !newex
.ee_start_hi
) {
3180 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3182 * block isn't allocated yet and
3183 * user doesn't want to allocate it
3187 /* we should allocate requested block */
3189 /* block is already allocated */
3190 newblock
= map
->m_lblk
3191 - le32_to_cpu(newex
.ee_block
)
3192 + ext4_ext_pblock(&newex
);
3193 /* number of remaining blocks in the extent */
3194 allocated
= ext4_ext_get_actual_len(&newex
) -
3195 (map
->m_lblk
- le32_to_cpu(newex
.ee_block
));
3200 /* find extent for this block */
3201 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, NULL
);
3203 err
= PTR_ERR(path
);
3208 depth
= ext_depth(inode
);
3211 * consistent leaf must not be empty;
3212 * this situation is possible, though, _during_ tree modification;
3213 * this is why assert can't be put in ext4_ext_find_extent()
3215 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
3216 EXT4_ERROR_INODE(inode
, "bad extent address "
3217 "lblock: %lu, depth: %d pblock %lld",
3218 (unsigned long) map
->m_lblk
, depth
,
3219 path
[depth
].p_block
);
3224 ex
= path
[depth
].p_ext
;
3226 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3227 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3228 unsigned short ee_len
;
3231 * Uninitialized extents are treated as holes, except that
3232 * we split out initialized portions during a write.
3234 ee_len
= ext4_ext_get_actual_len(ex
);
3235 /* if found extent covers block, simply return it */
3236 if (in_range(map
->m_lblk
, ee_block
, ee_len
)) {
3237 newblock
= map
->m_lblk
- ee_block
+ ee_start
;
3238 /* number of remaining blocks in the extent */
3239 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3240 ext_debug("%u fit into %u:%d -> %llu\n", map
->m_lblk
,
3241 ee_block
, ee_len
, newblock
);
3243 /* Do not put uninitialized extent in the cache */
3244 if (!ext4_ext_is_uninitialized(ex
)) {
3245 ext4_ext_put_in_cache(inode
, ee_block
,
3249 ret
= ext4_ext_handle_uninitialized_extents(handle
,
3250 inode
, map
, path
, flags
, allocated
,
3257 * requested block isn't allocated yet;
3258 * we couldn't try to create block if create flag is zero
3260 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3262 * put just found gap into cache to speed up
3263 * subsequent requests
3265 ext4_ext_put_gap_in_cache(inode
, path
, map
->m_lblk
);
3269 * Okay, we need to do block allocation.
3272 /* find neighbour allocated blocks */
3273 ar
.lleft
= map
->m_lblk
;
3274 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
3277 ar
.lright
= map
->m_lblk
;
3278 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
3283 * See if request is beyond maximum number of blocks we can have in
3284 * a single extent. For an initialized extent this limit is
3285 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3286 * EXT_UNINIT_MAX_LEN.
3288 if (map
->m_len
> EXT_INIT_MAX_LEN
&&
3289 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3290 map
->m_len
= EXT_INIT_MAX_LEN
;
3291 else if (map
->m_len
> EXT_UNINIT_MAX_LEN
&&
3292 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3293 map
->m_len
= EXT_UNINIT_MAX_LEN
;
3295 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3296 newex
.ee_block
= cpu_to_le32(map
->m_lblk
);
3297 newex
.ee_len
= cpu_to_le16(map
->m_len
);
3298 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
3300 allocated
= ext4_ext_get_actual_len(&newex
);
3302 allocated
= map
->m_len
;
3304 /* allocate new block */
3306 ar
.goal
= ext4_ext_find_goal(inode
, path
, map
->m_lblk
);
3307 ar
.logical
= map
->m_lblk
;
3309 if (S_ISREG(inode
->i_mode
))
3310 ar
.flags
= EXT4_MB_HINT_DATA
;
3312 /* disable in-core preallocation for non-regular files */
3314 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
3317 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3318 ar
.goal
, newblock
, allocated
);
3320 /* try to insert new extent into found leaf and return */
3321 ext4_ext_store_pblock(&newex
, newblock
);
3322 newex
.ee_len
= cpu_to_le16(ar
.len
);
3323 /* Mark uninitialized */
3324 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
3325 ext4_ext_mark_uninitialized(&newex
);
3327 * io_end structure was created for every IO write to an
3328 * uninitialized extent. To avoid unnecessary conversion,
3329 * here we flag the IO that really needs the conversion.
3330 * For non asycn direct IO case, flag the inode state
3331 * that we need to perform conversion when IO is done.
3333 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3334 if (io
&& !(io
->flag
& EXT4_IO_END_UNWRITTEN
)) {
3335 io
->flag
= EXT4_IO_END_UNWRITTEN
;
3336 atomic_inc(&EXT4_I(inode
)->i_aiodio_unwritten
);
3338 ext4_set_inode_state(inode
,
3339 EXT4_STATE_DIO_UNWRITTEN
);
3341 if (ext4_should_dioread_nolock(inode
))
3342 map
->m_flags
|= EXT4_MAP_UNINIT
;
3345 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
, ar
.len
);
3349 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3351 /* free data blocks we just allocated */
3352 /* not a good idea to call discard here directly,
3353 * but otherwise we'd need to call it every free() */
3354 ext4_discard_preallocations(inode
);
3355 ext4_free_blocks(handle
, inode
, NULL
, ext4_ext_pblock(&newex
),
3356 ext4_ext_get_actual_len(&newex
), 0);
3360 /* previous routine could use block we allocated */
3361 newblock
= ext4_ext_pblock(&newex
);
3362 allocated
= ext4_ext_get_actual_len(&newex
);
3363 if (allocated
> map
->m_len
)
3364 allocated
= map
->m_len
;
3365 map
->m_flags
|= EXT4_MAP_NEW
;
3368 * Update reserved blocks/metadata blocks after successful
3369 * block allocation which had been deferred till now.
3371 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3372 ext4_da_update_reserve_space(inode
, allocated
, 1);
3375 * Cache the extent and update transaction to commit on fdatasync only
3376 * when it is _not_ an uninitialized extent.
3378 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
3379 ext4_ext_put_in_cache(inode
, map
->m_lblk
, allocated
, newblock
);
3380 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3382 ext4_update_inode_fsync_trans(handle
, inode
, 0);
3384 if (allocated
> map
->m_len
)
3385 allocated
= map
->m_len
;
3386 ext4_ext_show_leaf(inode
, path
);
3387 map
->m_flags
|= EXT4_MAP_MAPPED
;
3388 map
->m_pblk
= newblock
;
3389 map
->m_len
= allocated
;
3392 ext4_ext_drop_refs(path
);
3395 trace_ext4_ext_map_blocks_exit(inode
, map
->m_lblk
,
3396 newblock
, map
->m_len
, err
? err
: allocated
);
3397 return err
? err
: allocated
;
3400 void ext4_ext_truncate(struct inode
*inode
)
3402 struct address_space
*mapping
= inode
->i_mapping
;
3403 struct super_block
*sb
= inode
->i_sb
;
3404 ext4_lblk_t last_block
;
3409 * finish any pending end_io work so we won't run the risk of
3410 * converting any truncated blocks to initialized later
3412 ext4_flush_completed_IO(inode
);
3415 * probably first extent we're gonna free will be last in block
3417 err
= ext4_writepage_trans_blocks(inode
);
3418 handle
= ext4_journal_start(inode
, err
);
3422 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3423 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3425 if (ext4_orphan_add(handle
, inode
))
3428 down_write(&EXT4_I(inode
)->i_data_sem
);
3429 ext4_ext_invalidate_cache(inode
);
3431 ext4_discard_preallocations(inode
);
3434 * TODO: optimization is possible here.
3435 * Probably we need not scan at all,
3436 * because page truncation is enough.
3439 /* we have to know where to truncate from in crash case */
3440 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3441 ext4_mark_inode_dirty(handle
, inode
);
3443 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3444 >> EXT4_BLOCK_SIZE_BITS(sb
);
3445 err
= ext4_ext_remove_space(inode
, last_block
);
3447 /* In a multi-transaction truncate, we only make the final
3448 * transaction synchronous.
3451 ext4_handle_sync(handle
);
3453 up_write(&EXT4_I(inode
)->i_data_sem
);
3457 * If this was a simple ftruncate() and the file will remain alive,
3458 * then we need to clear up the orphan record which we created above.
3459 * However, if this was a real unlink then we were called by
3460 * ext4_delete_inode(), and we allow that function to clean up the
3461 * orphan info for us.
3464 ext4_orphan_del(handle
, inode
);
3466 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3467 ext4_mark_inode_dirty(handle
, inode
);
3468 ext4_journal_stop(handle
);
3471 static void ext4_falloc_update_inode(struct inode
*inode
,
3472 int mode
, loff_t new_size
, int update_ctime
)
3474 struct timespec now
;
3477 now
= current_fs_time(inode
->i_sb
);
3478 if (!timespec_equal(&inode
->i_ctime
, &now
))
3479 inode
->i_ctime
= now
;
3482 * Update only when preallocation was requested beyond
3485 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3486 if (new_size
> i_size_read(inode
))
3487 i_size_write(inode
, new_size
);
3488 if (new_size
> EXT4_I(inode
)->i_disksize
)
3489 ext4_update_i_disksize(inode
, new_size
);
3492 * Mark that we allocate beyond EOF so the subsequent truncate
3493 * can proceed even if the new size is the same as i_size.
3495 if (new_size
> i_size_read(inode
))
3496 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3502 * preallocate space for a file. This implements ext4's fallocate file
3503 * operation, which gets called from sys_fallocate system call.
3504 * For block-mapped files, posix_fallocate should fall back to the method
3505 * of writing zeroes to the required new blocks (the same behavior which is
3506 * expected for file systems which do not support fallocate() system call).
3508 long ext4_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
3510 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
3513 unsigned int max_blocks
;
3517 struct ext4_map_blocks map
;
3518 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3520 /* We only support the FALLOC_FL_KEEP_SIZE mode */
3521 if (mode
& ~FALLOC_FL_KEEP_SIZE
)
3525 * currently supporting (pre)allocate mode for extent-based
3528 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
3531 trace_ext4_fallocate_enter(inode
, offset
, len
, mode
);
3532 map
.m_lblk
= offset
>> blkbits
;
3534 * We can't just convert len to max_blocks because
3535 * If blocksize = 4096 offset = 3072 and len = 2048
3537 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3540 * credits to insert 1 extent into extent tree
3542 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3543 mutex_lock(&inode
->i_mutex
);
3544 ret
= inode_newsize_ok(inode
, (len
+ offset
));
3546 mutex_unlock(&inode
->i_mutex
);
3547 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
, ret
);
3551 while (ret
>= 0 && ret
< max_blocks
) {
3552 map
.m_lblk
= map
.m_lblk
+ ret
;
3553 map
.m_len
= max_blocks
= max_blocks
- ret
;
3554 handle
= ext4_journal_start(inode
, credits
);
3555 if (IS_ERR(handle
)) {
3556 ret
= PTR_ERR(handle
);
3559 ret
= ext4_map_blocks(handle
, inode
, &map
,
3560 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
);
3564 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
3565 "returned error inode#%lu, block=%u, "
3566 "max_blocks=%u", __func__
,
3567 inode
->i_ino
, map
.m_lblk
, max_blocks
);
3569 ext4_mark_inode_dirty(handle
, inode
);
3570 ret2
= ext4_journal_stop(handle
);
3573 if ((map
.m_lblk
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3574 blkbits
) >> blkbits
))
3575 new_size
= offset
+ len
;
3577 new_size
= (map
.m_lblk
+ ret
) << blkbits
;
3579 ext4_falloc_update_inode(inode
, mode
, new_size
,
3580 (map
.m_flags
& EXT4_MAP_NEW
));
3581 ext4_mark_inode_dirty(handle
, inode
);
3582 ret2
= ext4_journal_stop(handle
);
3586 if (ret
== -ENOSPC
&&
3587 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3591 mutex_unlock(&inode
->i_mutex
);
3592 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
,
3593 ret
> 0 ? ret2
: ret
);
3594 return ret
> 0 ? ret2
: ret
;
3598 * This function convert a range of blocks to written extents
3599 * The caller of this function will pass the start offset and the size.
3600 * all unwritten extents within this range will be converted to
3603 * This function is called from the direct IO end io call back
3604 * function, to convert the fallocated extents after IO is completed.
3605 * Returns 0 on success.
3607 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
3611 unsigned int max_blocks
;
3614 struct ext4_map_blocks map
;
3615 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3617 map
.m_lblk
= offset
>> blkbits
;
3619 * We can't just convert len to max_blocks because
3620 * If blocksize = 4096 offset = 3072 and len = 2048
3622 max_blocks
= ((EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
) -
3625 * credits to insert 1 extent into extent tree
3627 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3628 while (ret
>= 0 && ret
< max_blocks
) {
3630 map
.m_len
= (max_blocks
-= ret
);
3631 handle
= ext4_journal_start(inode
, credits
);
3632 if (IS_ERR(handle
)) {
3633 ret
= PTR_ERR(handle
);
3636 ret
= ext4_map_blocks(handle
, inode
, &map
,
3637 EXT4_GET_BLOCKS_IO_CONVERT_EXT
);
3640 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
3641 "returned error inode#%lu, block=%u, "
3642 "max_blocks=%u", __func__
,
3643 inode
->i_ino
, map
.m_lblk
, map
.m_len
);
3645 ext4_mark_inode_dirty(handle
, inode
);
3646 ret2
= ext4_journal_stop(handle
);
3647 if (ret
<= 0 || ret2
)
3650 return ret
> 0 ? ret2
: ret
;
3654 * Callback function called for each extent to gather FIEMAP information.
3656 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3657 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3666 struct fiemap_extent_info
*fieinfo
= data
;
3667 unsigned char blksize_bits
;
3669 blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3670 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3672 if (newex
->ec_start
== 0) {
3674 * No extent in extent-tree contains block @newex->ec_start,
3675 * then the block may stay in 1)a hole or 2)delayed-extent.
3677 * Holes or delayed-extents are processed as follows.
3678 * 1. lookup dirty pages with specified range in pagecache.
3679 * If no page is got, then there is no delayed-extent and
3680 * return with EXT_CONTINUE.
3681 * 2. find the 1st mapped buffer,
3682 * 3. check if the mapped buffer is both in the request range
3683 * and a delayed buffer. If not, there is no delayed-extent,
3685 * 4. a delayed-extent is found, the extent will be collected.
3687 ext4_lblk_t end
= 0;
3688 pgoff_t last_offset
;
3691 pgoff_t start_index
= 0;
3692 struct page
**pages
= NULL
;
3693 struct buffer_head
*bh
= NULL
;
3694 struct buffer_head
*head
= NULL
;
3695 unsigned int nr_pages
= PAGE_SIZE
/ sizeof(struct page
*);
3697 pages
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3701 offset
= logical
>> PAGE_SHIFT
;
3703 last_offset
= offset
;
3705 ret
= find_get_pages_tag(inode
->i_mapping
, &offset
,
3706 PAGECACHE_TAG_DIRTY
, nr_pages
, pages
);
3708 if (!(flags
& FIEMAP_EXTENT_DELALLOC
)) {
3709 /* First time, try to find a mapped buffer. */
3712 for (index
= 0; index
< ret
; index
++)
3713 page_cache_release(pages
[index
]);
3716 return EXT_CONTINUE
;
3721 /* Try to find the 1st mapped buffer. */
3722 end
= ((__u64
)pages
[index
]->index
<< PAGE_SHIFT
) >>
3724 if (!page_has_buffers(pages
[index
]))
3726 head
= page_buffers(pages
[index
]);
3733 if (end
>= newex
->ec_block
+
3735 /* The buffer is out of
3736 * the request range.
3740 if (buffer_mapped(bh
) &&
3741 end
>= newex
->ec_block
) {
3742 start_index
= index
- 1;
3743 /* get the 1st mapped buffer. */
3744 goto found_mapped_buffer
;
3747 bh
= bh
->b_this_page
;
3749 } while (bh
!= head
);
3751 /* No mapped buffer in the range found in this page,
3752 * We need to look up next page.
3755 /* There is no page left, but we need to limit
3758 newex
->ec_len
= end
- newex
->ec_block
;
3763 /*Find contiguous delayed buffers. */
3764 if (ret
> 0 && pages
[0]->index
== last_offset
)
3765 head
= page_buffers(pages
[0]);
3771 found_mapped_buffer
:
3772 if (bh
!= NULL
&& buffer_delay(bh
)) {
3773 /* 1st or contiguous delayed buffer found. */
3774 if (!(flags
& FIEMAP_EXTENT_DELALLOC
)) {
3776 * 1st delayed buffer found, record
3777 * the start of extent.
3779 flags
|= FIEMAP_EXTENT_DELALLOC
;
3780 newex
->ec_block
= end
;
3781 logical
= (__u64
)end
<< blksize_bits
;
3783 /* Find contiguous delayed buffers. */
3785 if (!buffer_delay(bh
))
3786 goto found_delayed_extent
;
3787 bh
= bh
->b_this_page
;
3789 } while (bh
!= head
);
3791 for (; index
< ret
; index
++) {
3792 if (!page_has_buffers(pages
[index
])) {
3796 head
= page_buffers(pages
[index
]);
3802 if (pages
[index
]->index
!=
3803 pages
[start_index
]->index
+ index
3805 /* Blocks are not contiguous. */
3811 if (!buffer_delay(bh
))
3812 /* Delayed-extent ends. */
3813 goto found_delayed_extent
;
3814 bh
= bh
->b_this_page
;
3816 } while (bh
!= head
);
3818 } else if (!(flags
& FIEMAP_EXTENT_DELALLOC
))
3822 found_delayed_extent
:
3823 newex
->ec_len
= min(end
- newex
->ec_block
,
3824 (ext4_lblk_t
)EXT_INIT_MAX_LEN
);
3825 if (ret
== nr_pages
&& bh
!= NULL
&&
3826 newex
->ec_len
< EXT_INIT_MAX_LEN
&&
3828 /* Have not collected an extent and continue. */
3829 for (index
= 0; index
< ret
; index
++)
3830 page_cache_release(pages
[index
]);
3834 for (index
= 0; index
< ret
; index
++)
3835 page_cache_release(pages
[index
]);
3839 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3840 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3842 if (ex
&& ext4_ext_is_uninitialized(ex
))
3843 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3845 size
= i_size_read(inode
);
3846 if (logical
+ length
>= size
)
3847 flags
|= FIEMAP_EXTENT_LAST
;
3849 ret
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3855 return EXT_CONTINUE
;
3858 /* fiemap flags we can handle specified here */
3859 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3861 static int ext4_xattr_fiemap(struct inode
*inode
,
3862 struct fiemap_extent_info
*fieinfo
)
3866 __u32 flags
= FIEMAP_EXTENT_LAST
;
3867 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3871 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
3872 struct ext4_iloc iloc
;
3873 int offset
; /* offset of xattr in inode */
3875 error
= ext4_get_inode_loc(inode
, &iloc
);
3878 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3879 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3880 EXT4_I(inode
)->i_extra_isize
;
3882 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3883 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3885 } else { /* external block */
3886 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3887 length
= inode
->i_sb
->s_blocksize
;
3891 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3893 return (error
< 0 ? error
: 0);
3896 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3897 __u64 start
, __u64 len
)
3899 ext4_lblk_t start_blk
;
3902 /* fallback to generic here if not in extents fmt */
3903 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
3904 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3907 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3910 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3911 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3913 ext4_lblk_t len_blks
;
3916 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3917 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
3918 if (last_blk
>= EXT_MAX_BLOCK
)
3919 last_blk
= EXT_MAX_BLOCK
-1;
3920 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
3923 * Walk the extent tree gathering extent information.
3924 * ext4_ext_fiemap_cb will push extents back to user.
3926 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3927 ext4_ext_fiemap_cb
, fieinfo
);