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"
50 * combine low and high parts of physical block number into ext4_fsblk_t
52 ext4_fsblk_t
ext_pblock(struct ext4_extent
*ex
)
56 block
= le32_to_cpu(ex
->ee_start_lo
);
57 block
|= ((ext4_fsblk_t
) le16_to_cpu(ex
->ee_start_hi
) << 31) << 1;
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
65 ext4_fsblk_t
idx_pblock(struct ext4_extent_idx
*ix
)
69 block
= le32_to_cpu(ix
->ei_leaf_lo
);
70 block
|= ((ext4_fsblk_t
) le16_to_cpu(ix
->ei_leaf_hi
) << 31) << 1;
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
79 void ext4_ext_store_pblock(struct ext4_extent
*ex
, ext4_fsblk_t pb
)
81 ex
->ee_start_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
82 ex
->ee_start_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
90 static void ext4_idx_store_pblock(struct ext4_extent_idx
*ix
, ext4_fsblk_t pb
)
92 ix
->ei_leaf_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
93 ix
->ei_leaf_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
96 static int ext4_ext_truncate_extend_restart(handle_t
*handle
,
102 if (!ext4_handle_valid(handle
))
104 if (handle
->h_buffer_credits
> needed
)
106 err
= ext4_journal_extend(handle
, needed
);
109 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
111 * We have dropped i_data_sem so someone might have cached again
112 * an extent we are going to truncate.
114 ext4_ext_invalidate_cache(inode
);
124 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
125 struct ext4_ext_path
*path
)
128 /* path points to block */
129 return ext4_journal_get_write_access(handle
, path
->p_bh
);
131 /* path points to leaf/index in inode body */
132 /* we use in-core data, no need to protect them */
142 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
143 struct ext4_ext_path
*path
)
147 /* path points to block */
148 err
= ext4_handle_dirty_metadata(handle
, inode
, path
->p_bh
);
150 /* path points to leaf/index in inode body */
151 err
= ext4_mark_inode_dirty(handle
, inode
);
156 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
157 struct ext4_ext_path
*path
,
160 struct ext4_inode_info
*ei
= EXT4_I(inode
);
161 ext4_fsblk_t bg_start
;
162 ext4_fsblk_t last_block
;
163 ext4_grpblk_t colour
;
164 ext4_group_t block_group
;
165 int flex_size
= ext4_flex_bg_size(EXT4_SB(inode
->i_sb
));
169 struct ext4_extent
*ex
;
170 depth
= path
->p_depth
;
172 /* try to predict block placement */
173 ex
= path
[depth
].p_ext
;
175 return ext_pblock(ex
)+(block
-le32_to_cpu(ex
->ee_block
));
177 /* it looks like index is empty;
178 * try to find starting block from index itself */
179 if (path
[depth
].p_bh
)
180 return path
[depth
].p_bh
->b_blocknr
;
183 /* OK. use inode's group */
184 block_group
= ei
->i_block_group
;
185 if (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) {
187 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
188 * block groups per flexgroup, reserve the first block
189 * group for directories and special files. Regular
190 * files will start at the second block group. This
191 * tends to speed up directory access and improves
194 block_group
&= ~(flex_size
-1);
195 if (S_ISREG(inode
->i_mode
))
198 bg_start
= (block_group
* EXT4_BLOCKS_PER_GROUP(inode
->i_sb
)) +
199 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_first_data_block
);
200 last_block
= ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
) - 1;
203 * If we are doing delayed allocation, we don't need take
204 * colour into account.
206 if (test_opt(inode
->i_sb
, DELALLOC
))
209 if (bg_start
+ EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) <= last_block
)
210 colour
= (current
->pid
% 16) *
211 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
213 colour
= (current
->pid
% 16) * ((last_block
- bg_start
) / 16);
214 return bg_start
+ colour
+ block
;
218 * Allocation for a meta data block
221 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
222 struct ext4_ext_path
*path
,
223 struct ext4_extent
*ex
, int *err
)
225 ext4_fsblk_t goal
, newblock
;
227 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
228 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, NULL
, err
);
232 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
236 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
237 / sizeof(struct ext4_extent
);
239 #ifdef AGGRESSIVE_TEST
247 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
251 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
252 / sizeof(struct ext4_extent_idx
);
254 #ifdef AGGRESSIVE_TEST
262 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
266 size
= sizeof(EXT4_I(inode
)->i_data
);
267 size
-= sizeof(struct ext4_extent_header
);
268 size
/= sizeof(struct ext4_extent
);
270 #ifdef AGGRESSIVE_TEST
278 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
282 size
= sizeof(EXT4_I(inode
)->i_data
);
283 size
-= sizeof(struct ext4_extent_header
);
284 size
/= sizeof(struct ext4_extent_idx
);
286 #ifdef AGGRESSIVE_TEST
295 * Calculate the number of metadata blocks needed
296 * to allocate @blocks
297 * Worse case is one block per extent
299 int ext4_ext_calc_metadata_amount(struct inode
*inode
, sector_t lblock
)
301 struct ext4_inode_info
*ei
= EXT4_I(inode
);
304 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
305 / sizeof(struct ext4_extent_idx
));
308 * If the new delayed allocation block is contiguous with the
309 * previous da block, it can share index blocks with the
310 * previous block, so we only need to allocate a new index
311 * block every idxs leaf blocks. At ldxs**2 blocks, we need
312 * an additional index block, and at ldxs**3 blocks, yet
313 * another index blocks.
315 if (ei
->i_da_metadata_calc_len
&&
316 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
317 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
319 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
321 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
323 ei
->i_da_metadata_calc_len
= 0;
325 ei
->i_da_metadata_calc_len
++;
326 ei
->i_da_metadata_calc_last_lblock
++;
331 * In the worst case we need a new set of index blocks at
332 * every level of the inode's extent tree.
334 ei
->i_da_metadata_calc_len
= 1;
335 ei
->i_da_metadata_calc_last_lblock
= lblock
;
336 return ext_depth(inode
) + 1;
340 ext4_ext_max_entries(struct inode
*inode
, int depth
)
344 if (depth
== ext_depth(inode
)) {
346 max
= ext4_ext_space_root(inode
, 1);
348 max
= ext4_ext_space_root_idx(inode
, 1);
351 max
= ext4_ext_space_block(inode
, 1);
353 max
= ext4_ext_space_block_idx(inode
, 1);
359 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
361 ext4_fsblk_t block
= ext_pblock(ext
);
362 int len
= ext4_ext_get_actual_len(ext
);
364 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
367 static int ext4_valid_extent_idx(struct inode
*inode
,
368 struct ext4_extent_idx
*ext_idx
)
370 ext4_fsblk_t block
= idx_pblock(ext_idx
);
372 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
375 static int ext4_valid_extent_entries(struct inode
*inode
,
376 struct ext4_extent_header
*eh
,
379 struct ext4_extent
*ext
;
380 struct ext4_extent_idx
*ext_idx
;
381 unsigned short entries
;
382 if (eh
->eh_entries
== 0)
385 entries
= le16_to_cpu(eh
->eh_entries
);
389 ext
= EXT_FIRST_EXTENT(eh
);
391 if (!ext4_valid_extent(inode
, ext
))
397 ext_idx
= EXT_FIRST_INDEX(eh
);
399 if (!ext4_valid_extent_idx(inode
, ext_idx
))
408 static int __ext4_ext_check(const char *function
, struct inode
*inode
,
409 struct ext4_extent_header
*eh
,
412 const char *error_msg
;
415 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
416 error_msg
= "invalid magic";
419 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
420 error_msg
= "unexpected eh_depth";
423 if (unlikely(eh
->eh_max
== 0)) {
424 error_msg
= "invalid eh_max";
427 max
= ext4_ext_max_entries(inode
, depth
);
428 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
429 error_msg
= "too large eh_max";
432 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
433 error_msg
= "invalid eh_entries";
436 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
437 error_msg
= "invalid extent entries";
443 ext4_error(inode
->i_sb
, function
,
444 "bad header/extent in inode #%lu: %s - magic %x, "
445 "entries %u, max %u(%u), depth %u(%u)",
446 inode
->i_ino
, error_msg
, le16_to_cpu(eh
->eh_magic
),
447 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
448 max
, le16_to_cpu(eh
->eh_depth
), depth
);
453 #define ext4_ext_check(inode, eh, depth) \
454 __ext4_ext_check(__func__, inode, eh, depth)
456 int ext4_ext_check_inode(struct inode
*inode
)
458 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
462 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
464 int k
, l
= path
->p_depth
;
467 for (k
= 0; k
<= l
; k
++, path
++) {
469 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
470 idx_pblock(path
->p_idx
));
471 } else if (path
->p_ext
) {
472 ext_debug(" %d:[%d]%d:%llu ",
473 le32_to_cpu(path
->p_ext
->ee_block
),
474 ext4_ext_is_uninitialized(path
->p_ext
),
475 ext4_ext_get_actual_len(path
->p_ext
),
476 ext_pblock(path
->p_ext
));
483 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
485 int depth
= ext_depth(inode
);
486 struct ext4_extent_header
*eh
;
487 struct ext4_extent
*ex
;
493 eh
= path
[depth
].p_hdr
;
494 ex
= EXT_FIRST_EXTENT(eh
);
496 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
498 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
499 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
500 ext4_ext_is_uninitialized(ex
),
501 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
506 #define ext4_ext_show_path(inode, path)
507 #define ext4_ext_show_leaf(inode, path)
510 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
512 int depth
= path
->p_depth
;
515 for (i
= 0; i
<= depth
; i
++, path
++)
523 * ext4_ext_binsearch_idx:
524 * binary search for the closest index of the given block
525 * the header must be checked before calling this
528 ext4_ext_binsearch_idx(struct inode
*inode
,
529 struct ext4_ext_path
*path
, ext4_lblk_t block
)
531 struct ext4_extent_header
*eh
= path
->p_hdr
;
532 struct ext4_extent_idx
*r
, *l
, *m
;
535 ext_debug("binsearch for %u(idx): ", block
);
537 l
= EXT_FIRST_INDEX(eh
) + 1;
538 r
= EXT_LAST_INDEX(eh
);
541 if (block
< le32_to_cpu(m
->ei_block
))
545 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
546 m
, le32_to_cpu(m
->ei_block
),
547 r
, le32_to_cpu(r
->ei_block
));
551 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
552 idx_pblock(path
->p_idx
));
554 #ifdef CHECK_BINSEARCH
556 struct ext4_extent_idx
*chix
, *ix
;
559 chix
= ix
= EXT_FIRST_INDEX(eh
);
560 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
562 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
563 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
565 ix
, EXT_FIRST_INDEX(eh
));
566 printk(KERN_DEBUG
"%u <= %u\n",
567 le32_to_cpu(ix
->ei_block
),
568 le32_to_cpu(ix
[-1].ei_block
));
570 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
571 <= le32_to_cpu(ix
[-1].ei_block
));
572 if (block
< le32_to_cpu(ix
->ei_block
))
576 BUG_ON(chix
!= path
->p_idx
);
583 * ext4_ext_binsearch:
584 * binary search for closest extent of the given block
585 * the header must be checked before calling this
588 ext4_ext_binsearch(struct inode
*inode
,
589 struct ext4_ext_path
*path
, ext4_lblk_t block
)
591 struct ext4_extent_header
*eh
= path
->p_hdr
;
592 struct ext4_extent
*r
, *l
, *m
;
594 if (eh
->eh_entries
== 0) {
596 * this leaf is empty:
597 * we get such a leaf in split/add case
602 ext_debug("binsearch for %u: ", block
);
604 l
= EXT_FIRST_EXTENT(eh
) + 1;
605 r
= EXT_LAST_EXTENT(eh
);
609 if (block
< le32_to_cpu(m
->ee_block
))
613 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
614 m
, le32_to_cpu(m
->ee_block
),
615 r
, le32_to_cpu(r
->ee_block
));
619 ext_debug(" -> %d:%llu:[%d]%d ",
620 le32_to_cpu(path
->p_ext
->ee_block
),
621 ext_pblock(path
->p_ext
),
622 ext4_ext_is_uninitialized(path
->p_ext
),
623 ext4_ext_get_actual_len(path
->p_ext
));
625 #ifdef CHECK_BINSEARCH
627 struct ext4_extent
*chex
, *ex
;
630 chex
= ex
= EXT_FIRST_EXTENT(eh
);
631 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
632 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
633 <= le32_to_cpu(ex
[-1].ee_block
));
634 if (block
< le32_to_cpu(ex
->ee_block
))
638 BUG_ON(chex
!= path
->p_ext
);
644 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
646 struct ext4_extent_header
*eh
;
648 eh
= ext_inode_hdr(inode
);
651 eh
->eh_magic
= EXT4_EXT_MAGIC
;
652 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
653 ext4_mark_inode_dirty(handle
, inode
);
654 ext4_ext_invalidate_cache(inode
);
658 struct ext4_ext_path
*
659 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
660 struct ext4_ext_path
*path
)
662 struct ext4_extent_header
*eh
;
663 struct buffer_head
*bh
;
664 short int depth
, i
, ppos
= 0, alloc
= 0;
666 eh
= ext_inode_hdr(inode
);
667 depth
= ext_depth(inode
);
669 /* account possible depth increase */
671 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
674 return ERR_PTR(-ENOMEM
);
681 /* walk through the tree */
683 int need_to_validate
= 0;
685 ext_debug("depth %d: num %d, max %d\n",
686 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
688 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
689 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
690 path
[ppos
].p_depth
= i
;
691 path
[ppos
].p_ext
= NULL
;
693 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
696 if (!bh_uptodate_or_lock(bh
)) {
697 if (bh_submit_read(bh
) < 0) {
701 /* validate the extent entries */
702 need_to_validate
= 1;
704 eh
= ext_block_hdr(bh
);
706 BUG_ON(ppos
> depth
);
707 path
[ppos
].p_bh
= bh
;
708 path
[ppos
].p_hdr
= eh
;
711 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
715 path
[ppos
].p_depth
= i
;
716 path
[ppos
].p_ext
= NULL
;
717 path
[ppos
].p_idx
= NULL
;
720 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
721 /* if not an empty leaf */
722 if (path
[ppos
].p_ext
)
723 path
[ppos
].p_block
= ext_pblock(path
[ppos
].p_ext
);
725 ext4_ext_show_path(inode
, path
);
730 ext4_ext_drop_refs(path
);
733 return ERR_PTR(-EIO
);
737 * ext4_ext_insert_index:
738 * insert new index [@logical;@ptr] into the block at @curp;
739 * check where to insert: before @curp or after @curp
741 int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
742 struct ext4_ext_path
*curp
,
743 int logical
, ext4_fsblk_t ptr
)
745 struct ext4_extent_idx
*ix
;
748 err
= ext4_ext_get_access(handle
, inode
, curp
);
752 BUG_ON(logical
== le32_to_cpu(curp
->p_idx
->ei_block
));
753 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
754 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
756 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
757 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
758 len
= len
< 0 ? 0 : len
;
759 ext_debug("insert new index %d after: %llu. "
760 "move %d from 0x%p to 0x%p\n",
762 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
763 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
765 ix
= curp
->p_idx
+ 1;
768 len
= len
* sizeof(struct ext4_extent_idx
);
769 len
= len
< 0 ? 0 : len
;
770 ext_debug("insert new index %d before: %llu. "
771 "move %d from 0x%p to 0x%p\n",
773 curp
->p_idx
, (curp
->p_idx
+ 1));
774 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
778 ix
->ei_block
= cpu_to_le32(logical
);
779 ext4_idx_store_pblock(ix
, ptr
);
780 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
782 BUG_ON(le16_to_cpu(curp
->p_hdr
->eh_entries
)
783 > le16_to_cpu(curp
->p_hdr
->eh_max
));
784 BUG_ON(ix
> EXT_LAST_INDEX(curp
->p_hdr
));
786 err
= ext4_ext_dirty(handle
, inode
, curp
);
787 ext4_std_error(inode
->i_sb
, err
);
794 * inserts new subtree into the path, using free index entry
796 * - allocates all needed blocks (new leaf and all intermediate index blocks)
797 * - makes decision where to split
798 * - moves remaining extents and index entries (right to the split point)
799 * into the newly allocated blocks
800 * - initializes subtree
802 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
803 struct ext4_ext_path
*path
,
804 struct ext4_extent
*newext
, int at
)
806 struct buffer_head
*bh
= NULL
;
807 int depth
= ext_depth(inode
);
808 struct ext4_extent_header
*neh
;
809 struct ext4_extent_idx
*fidx
;
810 struct ext4_extent
*ex
;
812 ext4_fsblk_t newblock
, oldblock
;
814 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
817 /* make decision: where to split? */
818 /* FIXME: now decision is simplest: at current extent */
820 /* if current leaf will be split, then we should use
821 * border from split point */
822 BUG_ON(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
));
823 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
824 border
= path
[depth
].p_ext
[1].ee_block
;
825 ext_debug("leaf will be split."
826 " next leaf starts at %d\n",
827 le32_to_cpu(border
));
829 border
= newext
->ee_block
;
830 ext_debug("leaf will be added."
831 " next leaf starts at %d\n",
832 le32_to_cpu(border
));
836 * If error occurs, then we break processing
837 * and mark filesystem read-only. index won't
838 * be inserted and tree will be in consistent
839 * state. Next mount will repair buffers too.
843 * Get array to track all allocated blocks.
844 * We need this to handle errors and free blocks
847 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
851 /* allocate all needed blocks */
852 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
853 for (a
= 0; a
< depth
- at
; a
++) {
854 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
858 ablocks
[a
] = newblock
;
861 /* initialize new leaf */
862 newblock
= ablocks
[--a
];
863 BUG_ON(newblock
== 0);
864 bh
= sb_getblk(inode
->i_sb
, newblock
);
871 err
= ext4_journal_get_create_access(handle
, bh
);
875 neh
= ext_block_hdr(bh
);
877 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
878 neh
->eh_magic
= EXT4_EXT_MAGIC
;
880 ex
= EXT_FIRST_EXTENT(neh
);
882 /* move remainder of path[depth] to the new leaf */
883 BUG_ON(path
[depth
].p_hdr
->eh_entries
!= path
[depth
].p_hdr
->eh_max
);
884 /* start copy from next extent */
885 /* TODO: we could do it by single memmove */
888 while (path
[depth
].p_ext
<=
889 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
890 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
891 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
892 ext_pblock(path
[depth
].p_ext
),
893 ext4_ext_is_uninitialized(path
[depth
].p_ext
),
894 ext4_ext_get_actual_len(path
[depth
].p_ext
),
896 /*memmove(ex++, path[depth].p_ext++,
897 sizeof(struct ext4_extent));
903 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
904 le16_add_cpu(&neh
->eh_entries
, m
);
907 set_buffer_uptodate(bh
);
910 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
916 /* correct old leaf */
918 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
921 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
922 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
928 /* create intermediate indexes */
932 ext_debug("create %d intermediate indices\n", k
);
933 /* insert new index into current index block */
934 /* current depth stored in i var */
938 newblock
= ablocks
[--a
];
939 bh
= sb_getblk(inode
->i_sb
, newblock
);
946 err
= ext4_journal_get_create_access(handle
, bh
);
950 neh
= ext_block_hdr(bh
);
951 neh
->eh_entries
= cpu_to_le16(1);
952 neh
->eh_magic
= EXT4_EXT_MAGIC
;
953 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
954 neh
->eh_depth
= cpu_to_le16(depth
- i
);
955 fidx
= EXT_FIRST_INDEX(neh
);
956 fidx
->ei_block
= border
;
957 ext4_idx_store_pblock(fidx
, oldblock
);
959 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
960 i
, newblock
, le32_to_cpu(border
), oldblock
);
965 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
966 EXT_MAX_INDEX(path
[i
].p_hdr
));
967 BUG_ON(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
968 EXT_LAST_INDEX(path
[i
].p_hdr
));
969 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
970 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
971 le32_to_cpu(path
[i
].p_idx
->ei_block
),
972 idx_pblock(path
[i
].p_idx
),
974 /*memmove(++fidx, path[i].p_idx++,
975 sizeof(struct ext4_extent_idx));
977 BUG_ON(neh->eh_entries > neh->eh_max);*/
982 memmove(++fidx
, path
[i
].p_idx
- m
,
983 sizeof(struct ext4_extent_idx
) * m
);
984 le16_add_cpu(&neh
->eh_entries
, m
);
986 set_buffer_uptodate(bh
);
989 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
995 /* correct old index */
997 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
1000 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1001 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1009 /* insert new index */
1010 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1011 le32_to_cpu(border
), newblock
);
1015 if (buffer_locked(bh
))
1021 /* free all allocated blocks in error case */
1022 for (i
= 0; i
< depth
; i
++) {
1025 ext4_free_blocks(handle
, inode
, ablocks
[i
], 1, 1);
1034 * ext4_ext_grow_indepth:
1035 * implements tree growing procedure:
1036 * - allocates new block
1037 * - moves top-level data (index block or leaf) into the new block
1038 * - initializes new top-level, creating index that points to the
1039 * just created block
1041 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1042 struct ext4_ext_path
*path
,
1043 struct ext4_extent
*newext
)
1045 struct ext4_ext_path
*curp
= path
;
1046 struct ext4_extent_header
*neh
;
1047 struct ext4_extent_idx
*fidx
;
1048 struct buffer_head
*bh
;
1049 ext4_fsblk_t newblock
;
1052 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
, newext
, &err
);
1056 bh
= sb_getblk(inode
->i_sb
, newblock
);
1059 ext4_std_error(inode
->i_sb
, err
);
1064 err
= ext4_journal_get_create_access(handle
, bh
);
1070 /* move top-level index/leaf into new block */
1071 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1073 /* set size of new block */
1074 neh
= ext_block_hdr(bh
);
1075 /* old root could have indexes or leaves
1076 * so calculate e_max right way */
1077 if (ext_depth(inode
))
1078 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1080 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1081 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1082 set_buffer_uptodate(bh
);
1085 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1089 /* create index in new top-level index: num,max,pointer */
1090 err
= ext4_ext_get_access(handle
, inode
, curp
);
1094 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1095 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1096 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1097 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1099 if (path
[0].p_hdr
->eh_depth
)
1100 curp
->p_idx
->ei_block
=
1101 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1103 curp
->p_idx
->ei_block
=
1104 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1105 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1107 neh
= ext_inode_hdr(inode
);
1108 fidx
= EXT_FIRST_INDEX(neh
);
1109 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1110 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1111 le32_to_cpu(fidx
->ei_block
), idx_pblock(fidx
));
1113 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1114 err
= ext4_ext_dirty(handle
, inode
, curp
);
1122 * ext4_ext_create_new_leaf:
1123 * finds empty index and adds new leaf.
1124 * if no free index is found, then it requests in-depth growing.
1126 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1127 struct ext4_ext_path
*path
,
1128 struct ext4_extent
*newext
)
1130 struct ext4_ext_path
*curp
;
1131 int depth
, i
, err
= 0;
1134 i
= depth
= ext_depth(inode
);
1136 /* walk up to the tree and look for free index entry */
1137 curp
= path
+ depth
;
1138 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1143 /* we use already allocated block for index block,
1144 * so subsequent data blocks should be contiguous */
1145 if (EXT_HAS_FREE_INDEX(curp
)) {
1146 /* if we found index with free entry, then use that
1147 * entry: create all needed subtree and add new leaf */
1148 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
1153 ext4_ext_drop_refs(path
);
1154 path
= ext4_ext_find_extent(inode
,
1155 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1158 err
= PTR_ERR(path
);
1160 /* tree is full, time to grow in depth */
1161 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1166 ext4_ext_drop_refs(path
);
1167 path
= ext4_ext_find_extent(inode
,
1168 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1171 err
= PTR_ERR(path
);
1176 * only first (depth 0 -> 1) produces free space;
1177 * in all other cases we have to split the grown tree
1179 depth
= ext_depth(inode
);
1180 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1181 /* now we need to split */
1191 * search the closest allocated block to the left for *logical
1192 * and returns it at @logical + it's physical address at @phys
1193 * if *logical is the smallest allocated block, the function
1194 * returns 0 at @phys
1195 * return value contains 0 (success) or error code
1198 ext4_ext_search_left(struct inode
*inode
, struct ext4_ext_path
*path
,
1199 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1201 struct ext4_extent_idx
*ix
;
1202 struct ext4_extent
*ex
;
1205 BUG_ON(path
== NULL
);
1206 depth
= path
->p_depth
;
1209 if (depth
== 0 && path
->p_ext
== NULL
)
1212 /* usually extent in the path covers blocks smaller
1213 * then *logical, but it can be that extent is the
1214 * first one in the file */
1216 ex
= path
[depth
].p_ext
;
1217 ee_len
= ext4_ext_get_actual_len(ex
);
1218 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1219 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1220 while (--depth
>= 0) {
1221 ix
= path
[depth
].p_idx
;
1222 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1227 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1229 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1230 *phys
= ext_pblock(ex
) + ee_len
- 1;
1235 * search the closest allocated block to the right for *logical
1236 * and returns it at @logical + it's physical address at @phys
1237 * if *logical is the smallest allocated block, the function
1238 * returns 0 at @phys
1239 * return value contains 0 (success) or error code
1242 ext4_ext_search_right(struct inode
*inode
, struct ext4_ext_path
*path
,
1243 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1245 struct buffer_head
*bh
= NULL
;
1246 struct ext4_extent_header
*eh
;
1247 struct ext4_extent_idx
*ix
;
1248 struct ext4_extent
*ex
;
1250 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1253 BUG_ON(path
== NULL
);
1254 depth
= path
->p_depth
;
1257 if (depth
== 0 && path
->p_ext
== NULL
)
1260 /* usually extent in the path covers blocks smaller
1261 * then *logical, but it can be that extent is the
1262 * first one in the file */
1264 ex
= path
[depth
].p_ext
;
1265 ee_len
= ext4_ext_get_actual_len(ex
);
1266 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1267 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1268 while (--depth
>= 0) {
1269 ix
= path
[depth
].p_idx
;
1270 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1272 *logical
= le32_to_cpu(ex
->ee_block
);
1273 *phys
= ext_pblock(ex
);
1277 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1279 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1280 /* next allocated block in this leaf */
1282 *logical
= le32_to_cpu(ex
->ee_block
);
1283 *phys
= ext_pblock(ex
);
1287 /* go up and search for index to the right */
1288 while (--depth
>= 0) {
1289 ix
= path
[depth
].p_idx
;
1290 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1294 /* we've gone up to the root and found no index to the right */
1298 /* we've found index to the right, let's
1299 * follow it and find the closest allocated
1300 * block to the right */
1302 block
= idx_pblock(ix
);
1303 while (++depth
< path
->p_depth
) {
1304 bh
= sb_bread(inode
->i_sb
, block
);
1307 eh
= ext_block_hdr(bh
);
1308 /* subtract from p_depth to get proper eh_depth */
1309 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1313 ix
= EXT_FIRST_INDEX(eh
);
1314 block
= idx_pblock(ix
);
1318 bh
= sb_bread(inode
->i_sb
, block
);
1321 eh
= ext_block_hdr(bh
);
1322 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1326 ex
= EXT_FIRST_EXTENT(eh
);
1327 *logical
= le32_to_cpu(ex
->ee_block
);
1328 *phys
= ext_pblock(ex
);
1334 * ext4_ext_next_allocated_block:
1335 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1336 * NOTE: it considers block number from index entry as
1337 * allocated block. Thus, index entries have to be consistent
1341 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1345 BUG_ON(path
== NULL
);
1346 depth
= path
->p_depth
;
1348 if (depth
== 0 && path
->p_ext
== NULL
)
1349 return EXT_MAX_BLOCK
;
1351 while (depth
>= 0) {
1352 if (depth
== path
->p_depth
) {
1354 if (path
[depth
].p_ext
!=
1355 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1356 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1359 if (path
[depth
].p_idx
!=
1360 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1361 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1366 return EXT_MAX_BLOCK
;
1370 * ext4_ext_next_leaf_block:
1371 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1373 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1374 struct ext4_ext_path
*path
)
1378 BUG_ON(path
== NULL
);
1379 depth
= path
->p_depth
;
1381 /* zero-tree has no leaf blocks at all */
1383 return EXT_MAX_BLOCK
;
1385 /* go to index block */
1388 while (depth
>= 0) {
1389 if (path
[depth
].p_idx
!=
1390 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1391 return (ext4_lblk_t
)
1392 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1396 return EXT_MAX_BLOCK
;
1400 * ext4_ext_correct_indexes:
1401 * if leaf gets modified and modified extent is first in the leaf,
1402 * then we have to correct all indexes above.
1403 * TODO: do we need to correct tree in all cases?
1405 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1406 struct ext4_ext_path
*path
)
1408 struct ext4_extent_header
*eh
;
1409 int depth
= ext_depth(inode
);
1410 struct ext4_extent
*ex
;
1414 eh
= path
[depth
].p_hdr
;
1415 ex
= path
[depth
].p_ext
;
1420 /* there is no tree at all */
1424 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1425 /* we correct tree if first leaf got modified only */
1430 * TODO: we need correction if border is smaller than current one
1433 border
= path
[depth
].p_ext
->ee_block
;
1434 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1437 path
[k
].p_idx
->ei_block
= border
;
1438 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1443 /* change all left-side indexes */
1444 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1446 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1449 path
[k
].p_idx
->ei_block
= border
;
1450 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1459 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1460 struct ext4_extent
*ex2
)
1462 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1465 * Make sure that either both extents are uninitialized, or
1468 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1471 if (ext4_ext_is_uninitialized(ex1
))
1472 max_len
= EXT_UNINIT_MAX_LEN
;
1474 max_len
= EXT_INIT_MAX_LEN
;
1476 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1477 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1479 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1480 le32_to_cpu(ex2
->ee_block
))
1484 * To allow future support for preallocated extents to be added
1485 * as an RO_COMPAT feature, refuse to merge to extents if
1486 * this can result in the top bit of ee_len being set.
1488 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1490 #ifdef AGGRESSIVE_TEST
1491 if (ext1_ee_len
>= 4)
1495 if (ext_pblock(ex1
) + ext1_ee_len
== ext_pblock(ex2
))
1501 * This function tries to merge the "ex" extent to the next extent in the tree.
1502 * It always tries to merge towards right. If you want to merge towards
1503 * left, pass "ex - 1" as argument instead of "ex".
1504 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1505 * 1 if they got merged.
1507 int ext4_ext_try_to_merge(struct inode
*inode
,
1508 struct ext4_ext_path
*path
,
1509 struct ext4_extent
*ex
)
1511 struct ext4_extent_header
*eh
;
1512 unsigned int depth
, len
;
1514 int uninitialized
= 0;
1516 depth
= ext_depth(inode
);
1517 BUG_ON(path
[depth
].p_hdr
== NULL
);
1518 eh
= path
[depth
].p_hdr
;
1520 while (ex
< EXT_LAST_EXTENT(eh
)) {
1521 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1523 /* merge with next extent! */
1524 if (ext4_ext_is_uninitialized(ex
))
1526 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1527 + ext4_ext_get_actual_len(ex
+ 1));
1529 ext4_ext_mark_uninitialized(ex
);
1531 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1532 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1533 * sizeof(struct ext4_extent
);
1534 memmove(ex
+ 1, ex
+ 2, len
);
1536 le16_add_cpu(&eh
->eh_entries
, -1);
1538 WARN_ON(eh
->eh_entries
== 0);
1539 if (!eh
->eh_entries
)
1540 ext4_error(inode
->i_sb
, "ext4_ext_try_to_merge",
1541 "inode#%lu, eh->eh_entries = 0!", inode
->i_ino
);
1548 * check if a portion of the "newext" extent overlaps with an
1551 * If there is an overlap discovered, it updates the length of the newext
1552 * such that there will be no overlap, and then returns 1.
1553 * If there is no overlap found, it returns 0.
1555 unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1556 struct ext4_extent
*newext
,
1557 struct ext4_ext_path
*path
)
1560 unsigned int depth
, len1
;
1561 unsigned int ret
= 0;
1563 b1
= le32_to_cpu(newext
->ee_block
);
1564 len1
= ext4_ext_get_actual_len(newext
);
1565 depth
= ext_depth(inode
);
1566 if (!path
[depth
].p_ext
)
1568 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1571 * get the next allocated block if the extent in the path
1572 * is before the requested block(s)
1575 b2
= ext4_ext_next_allocated_block(path
);
1576 if (b2
== EXT_MAX_BLOCK
)
1580 /* check for wrap through zero on extent logical start block*/
1581 if (b1
+ len1
< b1
) {
1582 len1
= EXT_MAX_BLOCK
- b1
;
1583 newext
->ee_len
= cpu_to_le16(len1
);
1587 /* check for overlap */
1588 if (b1
+ len1
> b2
) {
1589 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1597 * ext4_ext_insert_extent:
1598 * tries to merge requsted extent into the existing extent or
1599 * inserts requested extent as new one into the tree,
1600 * creating new leaf in the no-space case.
1602 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1603 struct ext4_ext_path
*path
,
1604 struct ext4_extent
*newext
, int flag
)
1606 struct ext4_extent_header
*eh
;
1607 struct ext4_extent
*ex
, *fex
;
1608 struct ext4_extent
*nearex
; /* nearest extent */
1609 struct ext4_ext_path
*npath
= NULL
;
1610 int depth
, len
, err
;
1612 unsigned uninitialized
= 0;
1614 BUG_ON(ext4_ext_get_actual_len(newext
) == 0);
1615 depth
= ext_depth(inode
);
1616 ex
= path
[depth
].p_ext
;
1617 BUG_ON(path
[depth
].p_hdr
== NULL
);
1619 /* try to insert block into found extent and return */
1620 if (ex
&& (flag
!= EXT4_GET_BLOCKS_DIO_CREATE_EXT
)
1621 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1622 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1623 ext4_ext_is_uninitialized(newext
),
1624 ext4_ext_get_actual_len(newext
),
1625 le32_to_cpu(ex
->ee_block
),
1626 ext4_ext_is_uninitialized(ex
),
1627 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
1628 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1633 * ext4_can_extents_be_merged should have checked that either
1634 * both extents are uninitialized, or both aren't. Thus we
1635 * need to check only one of them here.
1637 if (ext4_ext_is_uninitialized(ex
))
1639 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1640 + ext4_ext_get_actual_len(newext
));
1642 ext4_ext_mark_uninitialized(ex
);
1643 eh
= path
[depth
].p_hdr
;
1649 depth
= ext_depth(inode
);
1650 eh
= path
[depth
].p_hdr
;
1651 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1654 /* probably next leaf has space for us? */
1655 fex
= EXT_LAST_EXTENT(eh
);
1656 next
= ext4_ext_next_leaf_block(inode
, path
);
1657 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1658 && next
!= EXT_MAX_BLOCK
) {
1659 ext_debug("next leaf block - %d\n", next
);
1660 BUG_ON(npath
!= NULL
);
1661 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1663 return PTR_ERR(npath
);
1664 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1665 eh
= npath
[depth
].p_hdr
;
1666 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1667 ext_debug("next leaf isnt full(%d)\n",
1668 le16_to_cpu(eh
->eh_entries
));
1672 ext_debug("next leaf has no free space(%d,%d)\n",
1673 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1677 * There is no free space in the found leaf.
1678 * We're gonna add a new leaf in the tree.
1680 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1683 depth
= ext_depth(inode
);
1684 eh
= path
[depth
].p_hdr
;
1687 nearex
= path
[depth
].p_ext
;
1689 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1694 /* there is no extent in this leaf, create first one */
1695 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1696 le32_to_cpu(newext
->ee_block
),
1698 ext4_ext_is_uninitialized(newext
),
1699 ext4_ext_get_actual_len(newext
));
1700 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1701 } else if (le32_to_cpu(newext
->ee_block
)
1702 > le32_to_cpu(nearex
->ee_block
)) {
1703 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1704 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1705 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1706 len
= (len
- 1) * sizeof(struct ext4_extent
);
1707 len
= len
< 0 ? 0 : len
;
1708 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1709 "move %d from 0x%p to 0x%p\n",
1710 le32_to_cpu(newext
->ee_block
),
1712 ext4_ext_is_uninitialized(newext
),
1713 ext4_ext_get_actual_len(newext
),
1714 nearex
, len
, nearex
+ 1, nearex
+ 2);
1715 memmove(nearex
+ 2, nearex
+ 1, len
);
1717 path
[depth
].p_ext
= nearex
+ 1;
1719 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1720 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1721 len
= len
< 0 ? 0 : len
;
1722 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1723 "move %d from 0x%p to 0x%p\n",
1724 le32_to_cpu(newext
->ee_block
),
1726 ext4_ext_is_uninitialized(newext
),
1727 ext4_ext_get_actual_len(newext
),
1728 nearex
, len
, nearex
+ 1, nearex
+ 2);
1729 memmove(nearex
+ 1, nearex
, len
);
1730 path
[depth
].p_ext
= nearex
;
1733 le16_add_cpu(&eh
->eh_entries
, 1);
1734 nearex
= path
[depth
].p_ext
;
1735 nearex
->ee_block
= newext
->ee_block
;
1736 ext4_ext_store_pblock(nearex
, ext_pblock(newext
));
1737 nearex
->ee_len
= newext
->ee_len
;
1740 /* try to merge extents to the right */
1741 if (flag
!= EXT4_GET_BLOCKS_DIO_CREATE_EXT
)
1742 ext4_ext_try_to_merge(inode
, path
, nearex
);
1744 /* try to merge extents to the left */
1746 /* time to correct all indexes above */
1747 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1751 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1755 ext4_ext_drop_refs(npath
);
1758 ext4_ext_invalidate_cache(inode
);
1762 int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1763 ext4_lblk_t num
, ext_prepare_callback func
,
1766 struct ext4_ext_path
*path
= NULL
;
1767 struct ext4_ext_cache cbex
;
1768 struct ext4_extent
*ex
;
1769 ext4_lblk_t next
, start
= 0, end
= 0;
1770 ext4_lblk_t last
= block
+ num
;
1771 int depth
, exists
, err
= 0;
1773 BUG_ON(func
== NULL
);
1774 BUG_ON(inode
== NULL
);
1776 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1778 /* find extent for this block */
1779 down_read(&EXT4_I(inode
)->i_data_sem
);
1780 path
= ext4_ext_find_extent(inode
, block
, path
);
1781 up_read(&EXT4_I(inode
)->i_data_sem
);
1783 err
= PTR_ERR(path
);
1788 depth
= ext_depth(inode
);
1789 BUG_ON(path
[depth
].p_hdr
== NULL
);
1790 ex
= path
[depth
].p_ext
;
1791 next
= ext4_ext_next_allocated_block(path
);
1795 /* there is no extent yet, so try to allocate
1796 * all requested space */
1799 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1800 /* need to allocate space before found extent */
1802 end
= le32_to_cpu(ex
->ee_block
);
1803 if (block
+ num
< end
)
1805 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1806 + ext4_ext_get_actual_len(ex
)) {
1807 /* need to allocate space after found extent */
1812 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1814 * some part of requested space is covered
1818 end
= le32_to_cpu(ex
->ee_block
)
1819 + ext4_ext_get_actual_len(ex
);
1820 if (block
+ num
< end
)
1826 BUG_ON(end
<= start
);
1829 cbex
.ec_block
= start
;
1830 cbex
.ec_len
= end
- start
;
1832 cbex
.ec_type
= EXT4_EXT_CACHE_GAP
;
1834 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1835 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1836 cbex
.ec_start
= ext_pblock(ex
);
1837 cbex
.ec_type
= EXT4_EXT_CACHE_EXTENT
;
1840 BUG_ON(cbex
.ec_len
== 0);
1841 err
= func(inode
, path
, &cbex
, ex
, cbdata
);
1842 ext4_ext_drop_refs(path
);
1847 if (err
== EXT_REPEAT
)
1849 else if (err
== EXT_BREAK
) {
1854 if (ext_depth(inode
) != depth
) {
1855 /* depth was changed. we have to realloc path */
1860 block
= cbex
.ec_block
+ cbex
.ec_len
;
1864 ext4_ext_drop_refs(path
);
1872 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1873 __u32 len
, ext4_fsblk_t start
, int type
)
1875 struct ext4_ext_cache
*cex
;
1877 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1878 cex
= &EXT4_I(inode
)->i_cached_extent
;
1879 cex
->ec_type
= type
;
1880 cex
->ec_block
= block
;
1882 cex
->ec_start
= start
;
1883 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1887 * ext4_ext_put_gap_in_cache:
1888 * calculate boundaries of the gap that the requested block fits into
1889 * and cache this gap
1892 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1895 int depth
= ext_depth(inode
);
1898 struct ext4_extent
*ex
;
1900 ex
= path
[depth
].p_ext
;
1902 /* there is no extent yet, so gap is [0;-] */
1904 len
= EXT_MAX_BLOCK
;
1905 ext_debug("cache gap(whole file):");
1906 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1908 len
= le32_to_cpu(ex
->ee_block
) - block
;
1909 ext_debug("cache gap(before): %u [%u:%u]",
1911 le32_to_cpu(ex
->ee_block
),
1912 ext4_ext_get_actual_len(ex
));
1913 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1914 + ext4_ext_get_actual_len(ex
)) {
1916 lblock
= le32_to_cpu(ex
->ee_block
)
1917 + ext4_ext_get_actual_len(ex
);
1919 next
= ext4_ext_next_allocated_block(path
);
1920 ext_debug("cache gap(after): [%u:%u] %u",
1921 le32_to_cpu(ex
->ee_block
),
1922 ext4_ext_get_actual_len(ex
),
1924 BUG_ON(next
== lblock
);
1925 len
= next
- lblock
;
1931 ext_debug(" -> %u:%lu\n", lblock
, len
);
1932 ext4_ext_put_in_cache(inode
, lblock
, len
, 0, EXT4_EXT_CACHE_GAP
);
1936 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1937 struct ext4_extent
*ex
)
1939 struct ext4_ext_cache
*cex
;
1940 int ret
= EXT4_EXT_CACHE_NO
;
1943 * We borrow i_block_reservation_lock to protect i_cached_extent
1945 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1946 cex
= &EXT4_I(inode
)->i_cached_extent
;
1948 /* has cache valid data? */
1949 if (cex
->ec_type
== EXT4_EXT_CACHE_NO
)
1952 BUG_ON(cex
->ec_type
!= EXT4_EXT_CACHE_GAP
&&
1953 cex
->ec_type
!= EXT4_EXT_CACHE_EXTENT
);
1954 if (block
>= cex
->ec_block
&& block
< cex
->ec_block
+ cex
->ec_len
) {
1955 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
1956 ext4_ext_store_pblock(ex
, cex
->ec_start
);
1957 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
1958 ext_debug("%u cached by %u:%u:%llu\n",
1960 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
1964 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1970 * removes index from the index block.
1971 * It's used in truncate case only, thus all requests are for
1972 * last index in the block only.
1974 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
1975 struct ext4_ext_path
*path
)
1977 struct buffer_head
*bh
;
1981 /* free index block */
1983 leaf
= idx_pblock(path
->p_idx
);
1984 BUG_ON(path
->p_hdr
->eh_entries
== 0);
1985 err
= ext4_ext_get_access(handle
, inode
, path
);
1988 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
1989 err
= ext4_ext_dirty(handle
, inode
, path
);
1992 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
1993 bh
= sb_find_get_block(inode
->i_sb
, leaf
);
1994 ext4_forget(handle
, 1, inode
, bh
, leaf
);
1995 ext4_free_blocks(handle
, inode
, leaf
, 1, 1);
2000 * ext4_ext_calc_credits_for_single_extent:
2001 * This routine returns max. credits that needed to insert an extent
2002 * to the extent tree.
2003 * When pass the actual path, the caller should calculate credits
2006 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2007 struct ext4_ext_path
*path
)
2010 int depth
= ext_depth(inode
);
2013 /* probably there is space in leaf? */
2014 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2015 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2018 * There are some space in the leaf tree, no
2019 * need to account for leaf block credit
2021 * bitmaps and block group descriptor blocks
2022 * and other metadat blocks still need to be
2025 /* 1 bitmap, 1 block group descriptor */
2026 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2031 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2035 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2037 * if nrblocks are fit in a single extent (chunk flag is 1), then
2038 * in the worse case, each tree level index/leaf need to be changed
2039 * if the tree split due to insert a new extent, then the old tree
2040 * index/leaf need to be updated too
2042 * If the nrblocks are discontiguous, they could cause
2043 * the whole tree split more than once, but this is really rare.
2045 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2048 int depth
= ext_depth(inode
);
2058 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2059 struct ext4_extent
*ex
,
2060 ext4_lblk_t from
, ext4_lblk_t to
)
2062 struct buffer_head
*bh
;
2063 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2064 int i
, metadata
= 0;
2066 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2068 #ifdef EXTENTS_STATS
2070 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2071 spin_lock(&sbi
->s_ext_stats_lock
);
2072 sbi
->s_ext_blocks
+= ee_len
;
2073 sbi
->s_ext_extents
++;
2074 if (ee_len
< sbi
->s_ext_min
)
2075 sbi
->s_ext_min
= ee_len
;
2076 if (ee_len
> sbi
->s_ext_max
)
2077 sbi
->s_ext_max
= ee_len
;
2078 if (ext_depth(inode
) > sbi
->s_depth_max
)
2079 sbi
->s_depth_max
= ext_depth(inode
);
2080 spin_unlock(&sbi
->s_ext_stats_lock
);
2083 if (from
>= le32_to_cpu(ex
->ee_block
)
2084 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2089 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2090 start
= ext_pblock(ex
) + ee_len
- num
;
2091 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2092 for (i
= 0; i
< num
; i
++) {
2093 bh
= sb_find_get_block(inode
->i_sb
, start
+ i
);
2094 ext4_forget(handle
, metadata
, inode
, bh
, start
+ i
);
2096 ext4_free_blocks(handle
, inode
, start
, num
, metadata
);
2097 } else if (from
== le32_to_cpu(ex
->ee_block
)
2098 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2099 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
2100 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2102 printk(KERN_INFO
"strange request: removal(2) "
2103 "%u-%u from %u:%u\n",
2104 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2110 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2111 struct ext4_ext_path
*path
, ext4_lblk_t start
)
2113 int err
= 0, correct_index
= 0;
2114 int depth
= ext_depth(inode
), credits
;
2115 struct ext4_extent_header
*eh
;
2116 ext4_lblk_t a
, b
, block
;
2118 ext4_lblk_t ex_ee_block
;
2119 unsigned short ex_ee_len
;
2120 unsigned uninitialized
= 0;
2121 struct ext4_extent
*ex
;
2123 /* the header must be checked already in ext4_ext_remove_space() */
2124 ext_debug("truncate since %u in leaf\n", start
);
2125 if (!path
[depth
].p_hdr
)
2126 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2127 eh
= path
[depth
].p_hdr
;
2130 /* find where to start removing */
2131 ex
= EXT_LAST_EXTENT(eh
);
2133 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2134 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2136 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2137 ex_ee_block
+ ex_ee_len
> start
) {
2139 if (ext4_ext_is_uninitialized(ex
))
2144 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2145 uninitialized
, ex_ee_len
);
2146 path
[depth
].p_ext
= ex
;
2148 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2149 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
2150 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
2152 ext_debug(" border %u:%u\n", a
, b
);
2154 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
2158 } else if (a
!= ex_ee_block
) {
2159 /* remove tail of the extent */
2160 block
= ex_ee_block
;
2162 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2163 /* remove head of the extent */
2166 /* there is no "make a hole" API yet */
2169 /* remove whole extent: excellent! */
2170 block
= ex_ee_block
;
2172 BUG_ON(a
!= ex_ee_block
);
2173 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
2177 * 3 for leaf, sb, and inode plus 2 (bmap and group
2178 * descriptor) for each block group; assume two block
2179 * groups plus ex_ee_len/blocks_per_block_group for
2182 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2183 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2185 credits
+= (ext_depth(inode
)) + 1;
2187 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2189 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2193 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2197 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2202 /* this extent is removed; mark slot entirely unused */
2203 ext4_ext_store_pblock(ex
, 0);
2204 le16_add_cpu(&eh
->eh_entries
, -1);
2207 ex
->ee_block
= cpu_to_le32(block
);
2208 ex
->ee_len
= cpu_to_le16(num
);
2210 * Do not mark uninitialized if all the blocks in the
2211 * extent have been removed.
2213 if (uninitialized
&& num
)
2214 ext4_ext_mark_uninitialized(ex
);
2216 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2220 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2223 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2224 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2227 if (correct_index
&& eh
->eh_entries
)
2228 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2230 /* if this leaf is free, then we should
2231 * remove it from index block above */
2232 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2233 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2240 * ext4_ext_more_to_rm:
2241 * returns 1 if current index has to be freed (even partial)
2244 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2246 BUG_ON(path
->p_idx
== NULL
);
2248 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2252 * if truncate on deeper level happened, it wasn't partial,
2253 * so we have to consider current index for truncation
2255 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2260 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2262 struct super_block
*sb
= inode
->i_sb
;
2263 int depth
= ext_depth(inode
);
2264 struct ext4_ext_path
*path
;
2268 ext_debug("truncate since %u\n", start
);
2270 /* probably first extent we're gonna free will be last in block */
2271 handle
= ext4_journal_start(inode
, depth
+ 1);
2273 return PTR_ERR(handle
);
2275 ext4_ext_invalidate_cache(inode
);
2278 * We start scanning from right side, freeing all the blocks
2279 * after i_size and walking into the tree depth-wise.
2281 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2283 ext4_journal_stop(handle
);
2286 path
[0].p_hdr
= ext_inode_hdr(inode
);
2287 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2291 path
[0].p_depth
= depth
;
2293 while (i
>= 0 && err
== 0) {
2295 /* this is leaf block */
2296 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2297 /* root level has p_bh == NULL, brelse() eats this */
2298 brelse(path
[i
].p_bh
);
2299 path
[i
].p_bh
= NULL
;
2304 /* this is index block */
2305 if (!path
[i
].p_hdr
) {
2306 ext_debug("initialize header\n");
2307 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2310 if (!path
[i
].p_idx
) {
2311 /* this level hasn't been touched yet */
2312 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2313 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2314 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2316 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2318 /* we were already here, see at next index */
2322 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2323 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2325 if (ext4_ext_more_to_rm(path
+ i
)) {
2326 struct buffer_head
*bh
;
2327 /* go to the next level */
2328 ext_debug("move to level %d (block %llu)\n",
2329 i
+ 1, idx_pblock(path
[i
].p_idx
));
2330 memset(path
+ i
+ 1, 0, sizeof(*path
));
2331 bh
= sb_bread(sb
, idx_pblock(path
[i
].p_idx
));
2333 /* should we reset i_size? */
2337 if (WARN_ON(i
+ 1 > depth
)) {
2341 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2346 path
[i
+ 1].p_bh
= bh
;
2348 /* save actual number of indexes since this
2349 * number is changed at the next iteration */
2350 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2353 /* we finished processing this index, go up */
2354 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2355 /* index is empty, remove it;
2356 * handle must be already prepared by the
2357 * truncatei_leaf() */
2358 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2360 /* root level has p_bh == NULL, brelse() eats this */
2361 brelse(path
[i
].p_bh
);
2362 path
[i
].p_bh
= NULL
;
2364 ext_debug("return to level %d\n", i
);
2368 /* TODO: flexible tree reduction should be here */
2369 if (path
->p_hdr
->eh_entries
== 0) {
2371 * truncate to zero freed all the tree,
2372 * so we need to correct eh_depth
2374 err
= ext4_ext_get_access(handle
, inode
, path
);
2376 ext_inode_hdr(inode
)->eh_depth
= 0;
2377 ext_inode_hdr(inode
)->eh_max
=
2378 cpu_to_le16(ext4_ext_space_root(inode
, 0));
2379 err
= ext4_ext_dirty(handle
, inode
, path
);
2383 ext4_ext_drop_refs(path
);
2385 ext4_journal_stop(handle
);
2391 * called at mount time
2393 void ext4_ext_init(struct super_block
*sb
)
2396 * possible initialization would be here
2399 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2400 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2401 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2402 #ifdef AGGRESSIVE_TEST
2403 printk(", aggressive tests");
2405 #ifdef CHECK_BINSEARCH
2406 printk(", check binsearch");
2408 #ifdef EXTENTS_STATS
2413 #ifdef EXTENTS_STATS
2414 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2415 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2416 EXT4_SB(sb
)->s_ext_max
= 0;
2422 * called at umount time
2424 void ext4_ext_release(struct super_block
*sb
)
2426 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2429 #ifdef EXTENTS_STATS
2430 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2431 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2432 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2433 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2434 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2435 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2436 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2441 static void bi_complete(struct bio
*bio
, int error
)
2443 complete((struct completion
*)bio
->bi_private
);
2446 /* FIXME!! we need to try to merge to left or right after zero-out */
2447 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2451 int blkbits
, blocksize
;
2453 struct completion event
;
2454 unsigned int ee_len
, len
, done
, offset
;
2457 blkbits
= inode
->i_blkbits
;
2458 blocksize
= inode
->i_sb
->s_blocksize
;
2459 ee_len
= ext4_ext_get_actual_len(ex
);
2460 ee_pblock
= ext_pblock(ex
);
2462 /* convert ee_pblock to 512 byte sectors */
2463 ee_pblock
= ee_pblock
<< (blkbits
- 9);
2465 while (ee_len
> 0) {
2467 if (ee_len
> BIO_MAX_PAGES
)
2468 len
= BIO_MAX_PAGES
;
2472 bio
= bio_alloc(GFP_NOIO
, len
);
2473 bio
->bi_sector
= ee_pblock
;
2474 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
2478 while (done
< len
) {
2479 ret
= bio_add_page(bio
, ZERO_PAGE(0),
2481 if (ret
!= blocksize
) {
2483 * We can't add any more pages because of
2484 * hardware limitations. Start a new bio.
2489 offset
+= blocksize
;
2490 if (offset
>= PAGE_CACHE_SIZE
)
2494 init_completion(&event
);
2495 bio
->bi_private
= &event
;
2496 bio
->bi_end_io
= bi_complete
;
2497 submit_bio(WRITE
, bio
);
2498 wait_for_completion(&event
);
2500 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2508 ee_pblock
+= done
<< (blkbits
- 9);
2513 #define EXT4_EXT_ZERO_LEN 7
2515 * This function is called by ext4_ext_get_blocks() if someone tries to write
2516 * to an uninitialized extent. It may result in splitting the uninitialized
2517 * extent into multiple extents (upto three - one initialized and two
2519 * There are three possibilities:
2520 * a> There is no split required: Entire extent should be initialized
2521 * b> Splits in two extents: Write is happening at either end of the extent
2522 * c> Splits in three extents: Somone is writing in middle of the extent
2524 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2525 struct inode
*inode
,
2526 struct ext4_ext_path
*path
,
2528 unsigned int max_blocks
)
2530 struct ext4_extent
*ex
, newex
, orig_ex
;
2531 struct ext4_extent
*ex1
= NULL
;
2532 struct ext4_extent
*ex2
= NULL
;
2533 struct ext4_extent
*ex3
= NULL
;
2534 struct ext4_extent_header
*eh
;
2535 ext4_lblk_t ee_block
;
2536 unsigned int allocated
, ee_len
, depth
;
2537 ext4_fsblk_t newblock
;
2541 depth
= ext_depth(inode
);
2542 eh
= path
[depth
].p_hdr
;
2543 ex
= path
[depth
].p_ext
;
2544 ee_block
= le32_to_cpu(ex
->ee_block
);
2545 ee_len
= ext4_ext_get_actual_len(ex
);
2546 allocated
= ee_len
- (iblock
- ee_block
);
2547 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2549 orig_ex
.ee_block
= ex
->ee_block
;
2550 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2551 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2553 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2556 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2557 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
) {
2558 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2560 goto fix_extent_len
;
2561 /* update the extent length and mark as initialized */
2562 ex
->ee_block
= orig_ex
.ee_block
;
2563 ex
->ee_len
= orig_ex
.ee_len
;
2564 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2565 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2566 /* zeroed the full extent */
2570 /* ex1: ee_block to iblock - 1 : uninitialized */
2571 if (iblock
> ee_block
) {
2573 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2574 ext4_ext_mark_uninitialized(ex1
);
2578 * for sanity, update the length of the ex2 extent before
2579 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2580 * overlap of blocks.
2582 if (!ex1
&& allocated
> max_blocks
)
2583 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2584 /* ex3: to ee_block + ee_len : uninitialised */
2585 if (allocated
> max_blocks
) {
2586 unsigned int newdepth
;
2587 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2588 if (allocated
<= EXT4_EXT_ZERO_LEN
) {
2590 * iblock == ee_block is handled by the zerouout
2592 * Mark first half uninitialized.
2593 * Mark second half initialized and zero out the
2594 * initialized extent
2596 ex
->ee_block
= orig_ex
.ee_block
;
2597 ex
->ee_len
= cpu_to_le16(ee_len
- allocated
);
2598 ext4_ext_mark_uninitialized(ex
);
2599 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2600 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2603 ex3
->ee_block
= cpu_to_le32(iblock
);
2604 ext4_ext_store_pblock(ex3
, newblock
);
2605 ex3
->ee_len
= cpu_to_le16(allocated
);
2606 err
= ext4_ext_insert_extent(handle
, inode
, path
,
2608 if (err
== -ENOSPC
) {
2609 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2611 goto fix_extent_len
;
2612 ex
->ee_block
= orig_ex
.ee_block
;
2613 ex
->ee_len
= orig_ex
.ee_len
;
2614 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2615 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2616 /* blocks available from iblock */
2620 goto fix_extent_len
;
2623 * We need to zero out the second half because
2624 * an fallocate request can update file size and
2625 * converting the second half to initialized extent
2626 * implies that we can leak some junk data to user
2629 err
= ext4_ext_zeroout(inode
, ex3
);
2632 * We should actually mark the
2633 * second half as uninit and return error
2634 * Insert would have changed the extent
2636 depth
= ext_depth(inode
);
2637 ext4_ext_drop_refs(path
);
2638 path
= ext4_ext_find_extent(inode
,
2641 err
= PTR_ERR(path
);
2644 /* get the second half extent details */
2645 ex
= path
[depth
].p_ext
;
2646 err
= ext4_ext_get_access(handle
, inode
,
2650 ext4_ext_mark_uninitialized(ex
);
2651 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2655 /* zeroed the second half */
2659 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2660 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2661 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2662 ext4_ext_mark_uninitialized(ex3
);
2663 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, 0);
2664 if (err
== -ENOSPC
) {
2665 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2667 goto fix_extent_len
;
2668 /* update the extent length and mark as initialized */
2669 ex
->ee_block
= orig_ex
.ee_block
;
2670 ex
->ee_len
= orig_ex
.ee_len
;
2671 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2672 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2673 /* zeroed the full extent */
2674 /* blocks available from iblock */
2678 goto fix_extent_len
;
2680 * The depth, and hence eh & ex might change
2681 * as part of the insert above.
2683 newdepth
= ext_depth(inode
);
2685 * update the extent length after successful insert of the
2688 orig_ex
.ee_len
= cpu_to_le16(ee_len
-
2689 ext4_ext_get_actual_len(ex3
));
2691 ext4_ext_drop_refs(path
);
2692 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2694 err
= PTR_ERR(path
);
2697 eh
= path
[depth
].p_hdr
;
2698 ex
= path
[depth
].p_ext
;
2702 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2706 allocated
= max_blocks
;
2708 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2709 * to insert a extent in the middle zerout directly
2710 * otherwise give the extent a chance to merge to left
2712 if (le16_to_cpu(orig_ex
.ee_len
) <= EXT4_EXT_ZERO_LEN
&&
2713 iblock
!= ee_block
) {
2714 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2716 goto fix_extent_len
;
2717 /* update the extent length and mark as initialized */
2718 ex
->ee_block
= orig_ex
.ee_block
;
2719 ex
->ee_len
= orig_ex
.ee_len
;
2720 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2721 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2722 /* zero out the first half */
2723 /* blocks available from iblock */
2728 * If there was a change of depth as part of the
2729 * insertion of ex3 above, we need to update the length
2730 * of the ex1 extent again here
2732 if (ex1
&& ex1
!= ex
) {
2734 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2735 ext4_ext_mark_uninitialized(ex1
);
2738 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2739 ex2
->ee_block
= cpu_to_le32(iblock
);
2740 ext4_ext_store_pblock(ex2
, newblock
);
2741 ex2
->ee_len
= cpu_to_le16(allocated
);
2745 * New (initialized) extent starts from the first block
2746 * in the current extent. i.e., ex2 == ex
2747 * We have to see if it can be merged with the extent
2750 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2752 * To merge left, pass "ex2 - 1" to try_to_merge(),
2753 * since it merges towards right _only_.
2755 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2757 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2760 depth
= ext_depth(inode
);
2765 * Try to Merge towards right. This might be required
2766 * only when the whole extent is being written to.
2767 * i.e. ex2 == ex and ex3 == NULL.
2770 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2772 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2777 /* Mark modified extent as dirty */
2778 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2781 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, 0);
2782 if (err
== -ENOSPC
) {
2783 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2785 goto fix_extent_len
;
2786 /* update the extent length and mark as initialized */
2787 ex
->ee_block
= orig_ex
.ee_block
;
2788 ex
->ee_len
= orig_ex
.ee_len
;
2789 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2790 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2791 /* zero out the first half */
2794 goto fix_extent_len
;
2796 ext4_ext_show_leaf(inode
, path
);
2797 return err
? err
: allocated
;
2800 ex
->ee_block
= orig_ex
.ee_block
;
2801 ex
->ee_len
= orig_ex
.ee_len
;
2802 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2803 ext4_ext_mark_uninitialized(ex
);
2804 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2809 * This function is called by ext4_ext_get_blocks() from
2810 * ext4_get_blocks_dio_write() when DIO to write
2811 * to an uninitialized extent.
2813 * Writing to an uninitized extent may result in splitting the uninitialized
2814 * extent into multiple /intialized unintialized extents (up to three)
2815 * There are three possibilities:
2816 * a> There is no split required: Entire extent should be uninitialized
2817 * b> Splits in two extents: Write is happening at either end of the extent
2818 * c> Splits in three extents: Somone is writing in middle of the extent
2820 * One of more index blocks maybe needed if the extent tree grow after
2821 * the unintialized extent split. To prevent ENOSPC occur at the IO
2822 * complete, we need to split the uninitialized extent before DIO submit
2823 * the IO. The uninitilized extent called at this time will be split
2824 * into three uninitialized extent(at most). After IO complete, the part
2825 * being filled will be convert to initialized by the end_io callback function
2826 * via ext4_convert_unwritten_extents().
2828 * Returns the size of uninitialized extent to be written on success.
2830 static int ext4_split_unwritten_extents(handle_t
*handle
,
2831 struct inode
*inode
,
2832 struct ext4_ext_path
*path
,
2834 unsigned int max_blocks
,
2837 struct ext4_extent
*ex
, newex
, orig_ex
;
2838 struct ext4_extent
*ex1
= NULL
;
2839 struct ext4_extent
*ex2
= NULL
;
2840 struct ext4_extent
*ex3
= NULL
;
2841 struct ext4_extent_header
*eh
;
2842 ext4_lblk_t ee_block
;
2843 unsigned int allocated
, ee_len
, depth
;
2844 ext4_fsblk_t newblock
;
2847 ext_debug("ext4_split_unwritten_extents: inode %lu,"
2848 "iblock %llu, max_blocks %u\n", inode
->i_ino
,
2849 (unsigned long long)iblock
, max_blocks
);
2850 depth
= ext_depth(inode
);
2851 eh
= path
[depth
].p_hdr
;
2852 ex
= path
[depth
].p_ext
;
2853 ee_block
= le32_to_cpu(ex
->ee_block
);
2854 ee_len
= ext4_ext_get_actual_len(ex
);
2855 allocated
= ee_len
- (iblock
- ee_block
);
2856 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2858 orig_ex
.ee_block
= ex
->ee_block
;
2859 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2860 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2863 * If the uninitialized extent begins at the same logical
2864 * block where the write begins, and the write completely
2865 * covers the extent, then we don't need to split it.
2867 if ((iblock
== ee_block
) && (allocated
<= max_blocks
))
2870 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2873 /* ex1: ee_block to iblock - 1 : uninitialized */
2874 if (iblock
> ee_block
) {
2876 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2877 ext4_ext_mark_uninitialized(ex1
);
2881 * for sanity, update the length of the ex2 extent before
2882 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2883 * overlap of blocks.
2885 if (!ex1
&& allocated
> max_blocks
)
2886 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2887 /* ex3: to ee_block + ee_len : uninitialised */
2888 if (allocated
> max_blocks
) {
2889 unsigned int newdepth
;
2891 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2892 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2893 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2894 ext4_ext_mark_uninitialized(ex3
);
2895 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, flags
);
2896 if (err
== -ENOSPC
) {
2897 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2899 goto fix_extent_len
;
2900 /* update the extent length and mark as initialized */
2901 ex
->ee_block
= orig_ex
.ee_block
;
2902 ex
->ee_len
= orig_ex
.ee_len
;
2903 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2904 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2905 /* zeroed the full extent */
2906 /* blocks available from iblock */
2910 goto fix_extent_len
;
2912 * The depth, and hence eh & ex might change
2913 * as part of the insert above.
2915 newdepth
= ext_depth(inode
);
2917 * update the extent length after successful insert of the
2920 orig_ex
.ee_len
= cpu_to_le16(ee_len
-
2921 ext4_ext_get_actual_len(ex3
));
2923 ext4_ext_drop_refs(path
);
2924 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2926 err
= PTR_ERR(path
);
2929 eh
= path
[depth
].p_hdr
;
2930 ex
= path
[depth
].p_ext
;
2934 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2938 allocated
= max_blocks
;
2941 * If there was a change of depth as part of the
2942 * insertion of ex3 above, we need to update the length
2943 * of the ex1 extent again here
2945 if (ex1
&& ex1
!= ex
) {
2947 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2948 ext4_ext_mark_uninitialized(ex1
);
2952 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2953 * uninitialised still.
2955 ex2
->ee_block
= cpu_to_le32(iblock
);
2956 ext4_ext_store_pblock(ex2
, newblock
);
2957 ex2
->ee_len
= cpu_to_le16(allocated
);
2958 ext4_ext_mark_uninitialized(ex2
);
2961 /* Mark modified extent as dirty */
2962 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2963 ext_debug("out here\n");
2966 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
2967 if (err
== -ENOSPC
) {
2968 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2970 goto fix_extent_len
;
2971 /* update the extent length and mark as initialized */
2972 ex
->ee_block
= orig_ex
.ee_block
;
2973 ex
->ee_len
= orig_ex
.ee_len
;
2974 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2975 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2976 /* zero out the first half */
2979 goto fix_extent_len
;
2981 ext4_ext_show_leaf(inode
, path
);
2982 return err
? err
: allocated
;
2985 ex
->ee_block
= orig_ex
.ee_block
;
2986 ex
->ee_len
= orig_ex
.ee_len
;
2987 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2988 ext4_ext_mark_uninitialized(ex
);
2989 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2992 static int ext4_convert_unwritten_extents_dio(handle_t
*handle
,
2993 struct inode
*inode
,
2994 struct ext4_ext_path
*path
)
2996 struct ext4_extent
*ex
;
2997 struct ext4_extent_header
*eh
;
3002 depth
= ext_depth(inode
);
3003 eh
= path
[depth
].p_hdr
;
3004 ex
= path
[depth
].p_ext
;
3006 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3009 /* first mark the extent as initialized */
3010 ext4_ext_mark_initialized(ex
);
3013 * We have to see if it can be merged with the extent
3016 if (ex
> EXT_FIRST_EXTENT(eh
)) {
3018 * To merge left, pass "ex - 1" to try_to_merge(),
3019 * since it merges towards right _only_.
3021 ret
= ext4_ext_try_to_merge(inode
, path
, ex
- 1);
3023 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
3026 depth
= ext_depth(inode
);
3031 * Try to Merge towards right.
3033 ret
= ext4_ext_try_to_merge(inode
, path
, ex
);
3035 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
3038 depth
= ext_depth(inode
);
3040 /* Mark modified extent as dirty */
3041 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3043 ext4_ext_show_leaf(inode
, path
);
3047 static void unmap_underlying_metadata_blocks(struct block_device
*bdev
,
3048 sector_t block
, int count
)
3051 for (i
= 0; i
< count
; i
++)
3052 unmap_underlying_metadata(bdev
, block
+ i
);
3056 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3057 ext4_lblk_t iblock
, unsigned int max_blocks
,
3058 struct ext4_ext_path
*path
, int flags
,
3059 unsigned int allocated
, struct buffer_head
*bh_result
,
3060 ext4_fsblk_t newblock
)
3064 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3066 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3067 "block %llu, max_blocks %u, flags %d, allocated %u",
3068 inode
->i_ino
, (unsigned long long)iblock
, max_blocks
,
3070 ext4_ext_show_leaf(inode
, path
);
3072 /* DIO get_block() before submit the IO, split the extent */
3073 if (flags
== EXT4_GET_BLOCKS_DIO_CREATE_EXT
) {
3074 ret
= ext4_split_unwritten_extents(handle
,
3075 inode
, path
, iblock
,
3078 * Flag the inode(non aio case) or end_io struct (aio case)
3079 * that this IO needs to convertion to written when IO is
3083 io
->flag
= DIO_AIO_UNWRITTEN
;
3085 EXT4_I(inode
)->i_state
|= EXT4_STATE_DIO_UNWRITTEN
;
3088 /* async DIO end_io complete, convert the filled extent to written */
3089 if (flags
== EXT4_GET_BLOCKS_DIO_CONVERT_EXT
) {
3090 ret
= ext4_convert_unwritten_extents_dio(handle
, inode
,
3093 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3096 /* buffered IO case */
3098 * repeat fallocate creation request
3099 * we already have an unwritten extent
3101 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3104 /* buffered READ or buffered write_begin() lookup */
3105 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3107 * We have blocks reserved already. We
3108 * return allocated blocks so that delalloc
3109 * won't do block reservation for us. But
3110 * the buffer head will be unmapped so that
3111 * a read from the block returns 0s.
3113 set_buffer_unwritten(bh_result
);
3117 /* buffered write, writepage time, convert*/
3118 ret
= ext4_ext_convert_to_initialized(handle
, inode
,
3122 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3129 set_buffer_new(bh_result
);
3131 * if we allocated more blocks than requested
3132 * we need to make sure we unmap the extra block
3133 * allocated. The actual needed block will get
3134 * unmapped later when we find the buffer_head marked
3137 if (allocated
> max_blocks
) {
3138 unmap_underlying_metadata_blocks(inode
->i_sb
->s_bdev
,
3139 newblock
+ max_blocks
,
3140 allocated
- max_blocks
);
3141 allocated
= max_blocks
;
3145 * If we have done fallocate with the offset that is already
3146 * delayed allocated, we would have block reservation
3147 * and quota reservation done in the delayed write path.
3148 * But fallocate would have already updated quota and block
3149 * count for this offset. So cancel these reservation
3151 if (flags
& EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE
)
3152 ext4_da_update_reserve_space(inode
, allocated
, 0);
3155 set_buffer_mapped(bh_result
);
3157 if (allocated
> max_blocks
)
3158 allocated
= max_blocks
;
3159 ext4_ext_show_leaf(inode
, path
);
3160 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
3161 bh_result
->b_blocknr
= newblock
;
3164 ext4_ext_drop_refs(path
);
3167 return err
? err
: allocated
;
3170 * Block allocation/map/preallocation routine for extents based files
3173 * Need to be called with
3174 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3175 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3177 * return > 0, number of of blocks already mapped/allocated
3178 * if create == 0 and these are pre-allocated blocks
3179 * buffer head is unmapped
3180 * otherwise blocks are mapped
3182 * return = 0, if plain look up failed (blocks have not been allocated)
3183 * buffer head is unmapped
3185 * return < 0, error case.
3187 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
3189 unsigned int max_blocks
, struct buffer_head
*bh_result
,
3192 struct ext4_ext_path
*path
= NULL
;
3193 struct ext4_extent_header
*eh
;
3194 struct ext4_extent newex
, *ex
;
3195 ext4_fsblk_t newblock
;
3196 int err
= 0, depth
, ret
, cache_type
;
3197 unsigned int allocated
= 0;
3198 struct ext4_allocation_request ar
;
3199 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3201 __clear_bit(BH_New
, &bh_result
->b_state
);
3202 ext_debug("blocks %u/%u requested for inode %lu\n",
3203 iblock
, max_blocks
, inode
->i_ino
);
3205 /* check in cache */
3206 cache_type
= ext4_ext_in_cache(inode
, iblock
, &newex
);
3208 if (cache_type
== EXT4_EXT_CACHE_GAP
) {
3209 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3211 * block isn't allocated yet and
3212 * user doesn't want to allocate it
3216 /* we should allocate requested block */
3217 } else if (cache_type
== EXT4_EXT_CACHE_EXTENT
) {
3218 /* block is already allocated */
3220 - le32_to_cpu(newex
.ee_block
)
3221 + ext_pblock(&newex
);
3222 /* number of remaining blocks in the extent */
3223 allocated
= ext4_ext_get_actual_len(&newex
) -
3224 (iblock
- le32_to_cpu(newex
.ee_block
));
3231 /* find extent for this block */
3232 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
3234 err
= PTR_ERR(path
);
3239 depth
= ext_depth(inode
);
3242 * consistent leaf must not be empty;
3243 * this situation is possible, though, _during_ tree modification;
3244 * this is why assert can't be put in ext4_ext_find_extent()
3246 if (path
[depth
].p_ext
== NULL
&& depth
!= 0) {
3247 ext4_error(inode
->i_sb
, __func__
, "bad extent address "
3248 "inode: %lu, iblock: %d, depth: %d",
3249 inode
->i_ino
, iblock
, depth
);
3253 eh
= path
[depth
].p_hdr
;
3255 ex
= path
[depth
].p_ext
;
3257 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3258 ext4_fsblk_t ee_start
= ext_pblock(ex
);
3259 unsigned short ee_len
;
3262 * Uninitialized extents are treated as holes, except that
3263 * we split out initialized portions during a write.
3265 ee_len
= ext4_ext_get_actual_len(ex
);
3266 /* if found extent covers block, simply return it */
3267 if (iblock
>= ee_block
&& iblock
< ee_block
+ ee_len
) {
3268 newblock
= iblock
- ee_block
+ ee_start
;
3269 /* number of remaining blocks in the extent */
3270 allocated
= ee_len
- (iblock
- ee_block
);
3271 ext_debug("%u fit into %u:%d -> %llu\n", iblock
,
3272 ee_block
, ee_len
, newblock
);
3274 /* Do not put uninitialized extent in the cache */
3275 if (!ext4_ext_is_uninitialized(ex
)) {
3276 ext4_ext_put_in_cache(inode
, ee_block
,
3278 EXT4_EXT_CACHE_EXTENT
);
3281 ret
= ext4_ext_handle_uninitialized_extents(handle
,
3282 inode
, iblock
, max_blocks
, path
,
3283 flags
, allocated
, bh_result
, newblock
);
3289 * requested block isn't allocated yet;
3290 * we couldn't try to create block if create flag is zero
3292 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3294 * put just found gap into cache to speed up
3295 * subsequent requests
3297 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
3301 * Okay, we need to do block allocation.
3304 /* find neighbour allocated blocks */
3306 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
3310 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
3315 * See if request is beyond maximum number of blocks we can have in
3316 * a single extent. For an initialized extent this limit is
3317 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3318 * EXT_UNINIT_MAX_LEN.
3320 if (max_blocks
> EXT_INIT_MAX_LEN
&&
3321 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3322 max_blocks
= EXT_INIT_MAX_LEN
;
3323 else if (max_blocks
> EXT_UNINIT_MAX_LEN
&&
3324 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3325 max_blocks
= EXT_UNINIT_MAX_LEN
;
3327 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3328 newex
.ee_block
= cpu_to_le32(iblock
);
3329 newex
.ee_len
= cpu_to_le16(max_blocks
);
3330 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
3332 allocated
= ext4_ext_get_actual_len(&newex
);
3334 allocated
= max_blocks
;
3336 /* allocate new block */
3338 ar
.goal
= ext4_ext_find_goal(inode
, path
, iblock
);
3339 ar
.logical
= iblock
;
3341 if (S_ISREG(inode
->i_mode
))
3342 ar
.flags
= EXT4_MB_HINT_DATA
;
3344 /* disable in-core preallocation for non-regular files */
3346 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
3349 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3350 ar
.goal
, newblock
, allocated
);
3352 /* try to insert new extent into found leaf and return */
3353 ext4_ext_store_pblock(&newex
, newblock
);
3354 newex
.ee_len
= cpu_to_le16(ar
.len
);
3355 /* Mark uninitialized */
3356 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
3357 ext4_ext_mark_uninitialized(&newex
);
3359 * io_end structure was created for every async
3360 * direct IO write to the middle of the file.
3361 * To avoid unecessary convertion for every aio dio rewrite
3362 * to the mid of file, here we flag the IO that is really
3363 * need the convertion.
3364 * For non asycn direct IO case, flag the inode state
3365 * that we need to perform convertion when IO is done.
3367 if (flags
== EXT4_GET_BLOCKS_DIO_CREATE_EXT
) {
3369 io
->flag
= DIO_AIO_UNWRITTEN
;
3371 EXT4_I(inode
)->i_state
|=
3372 EXT4_STATE_DIO_UNWRITTEN
;;
3375 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3377 /* free data blocks we just allocated */
3378 /* not a good idea to call discard here directly,
3379 * but otherwise we'd need to call it every free() */
3380 ext4_discard_preallocations(inode
);
3381 ext4_free_blocks(handle
, inode
, ext_pblock(&newex
),
3382 ext4_ext_get_actual_len(&newex
), 0);
3386 /* previous routine could use block we allocated */
3387 newblock
= ext_pblock(&newex
);
3388 allocated
= ext4_ext_get_actual_len(&newex
);
3389 if (allocated
> max_blocks
)
3390 allocated
= max_blocks
;
3391 set_buffer_new(bh_result
);
3394 * Update reserved blocks/metadata blocks after successful
3395 * block allocation which had been deferred till now.
3397 if (flags
& EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE
)
3398 ext4_da_update_reserve_space(inode
, allocated
, 1);
3401 * Cache the extent and update transaction to commit on fdatasync only
3402 * when it is _not_ an uninitialized extent.
3404 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
3405 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
3406 EXT4_EXT_CACHE_EXTENT
);
3407 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3409 ext4_update_inode_fsync_trans(handle
, inode
, 0);
3411 if (allocated
> max_blocks
)
3412 allocated
= max_blocks
;
3413 ext4_ext_show_leaf(inode
, path
);
3414 set_buffer_mapped(bh_result
);
3415 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
3416 bh_result
->b_blocknr
= newblock
;
3419 ext4_ext_drop_refs(path
);
3422 return err
? err
: allocated
;
3425 void ext4_ext_truncate(struct inode
*inode
)
3427 struct address_space
*mapping
= inode
->i_mapping
;
3428 struct super_block
*sb
= inode
->i_sb
;
3429 ext4_lblk_t last_block
;
3434 * probably first extent we're gonna free will be last in block
3436 err
= ext4_writepage_trans_blocks(inode
);
3437 handle
= ext4_journal_start(inode
, err
);
3441 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3442 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3444 if (ext4_orphan_add(handle
, inode
))
3447 down_write(&EXT4_I(inode
)->i_data_sem
);
3448 ext4_ext_invalidate_cache(inode
);
3450 ext4_discard_preallocations(inode
);
3453 * TODO: optimization is possible here.
3454 * Probably we need not scan at all,
3455 * because page truncation is enough.
3458 /* we have to know where to truncate from in crash case */
3459 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3460 ext4_mark_inode_dirty(handle
, inode
);
3462 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3463 >> EXT4_BLOCK_SIZE_BITS(sb
);
3464 err
= ext4_ext_remove_space(inode
, last_block
);
3466 /* In a multi-transaction truncate, we only make the final
3467 * transaction synchronous.
3470 ext4_handle_sync(handle
);
3473 up_write(&EXT4_I(inode
)->i_data_sem
);
3475 * If this was a simple ftruncate() and the file will remain alive,
3476 * then we need to clear up the orphan record which we created above.
3477 * However, if this was a real unlink then we were called by
3478 * ext4_delete_inode(), and we allow that function to clean up the
3479 * orphan info for us.
3482 ext4_orphan_del(handle
, inode
);
3484 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3485 ext4_mark_inode_dirty(handle
, inode
);
3486 ext4_journal_stop(handle
);
3489 static void ext4_falloc_update_inode(struct inode
*inode
,
3490 int mode
, loff_t new_size
, int update_ctime
)
3492 struct timespec now
;
3495 now
= current_fs_time(inode
->i_sb
);
3496 if (!timespec_equal(&inode
->i_ctime
, &now
))
3497 inode
->i_ctime
= now
;
3500 * Update only when preallocation was requested beyond
3503 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3504 if (new_size
> i_size_read(inode
))
3505 i_size_write(inode
, new_size
);
3506 if (new_size
> EXT4_I(inode
)->i_disksize
)
3507 ext4_update_i_disksize(inode
, new_size
);
3513 * preallocate space for a file. This implements ext4's fallocate inode
3514 * operation, which gets called from sys_fallocate system call.
3515 * For block-mapped files, posix_fallocate should fall back to the method
3516 * of writing zeroes to the required new blocks (the same behavior which is
3517 * expected for file systems which do not support fallocate() system call).
3519 long ext4_fallocate(struct inode
*inode
, int mode
, loff_t offset
, loff_t len
)
3524 unsigned int max_blocks
;
3528 struct buffer_head map_bh
;
3529 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3532 * currently supporting (pre)allocate mode for extent-based
3535 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3538 /* preallocation to directories is currently not supported */
3539 if (S_ISDIR(inode
->i_mode
))
3542 block
= offset
>> blkbits
;
3544 * We can't just convert len to max_blocks because
3545 * If blocksize = 4096 offset = 3072 and len = 2048
3547 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3550 * credits to insert 1 extent into extent tree
3552 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3553 mutex_lock(&inode
->i_mutex
);
3555 while (ret
>= 0 && ret
< max_blocks
) {
3556 block
= block
+ ret
;
3557 max_blocks
= max_blocks
- ret
;
3558 handle
= ext4_journal_start(inode
, credits
);
3559 if (IS_ERR(handle
)) {
3560 ret
= PTR_ERR(handle
);
3564 ret
= ext4_get_blocks(handle
, inode
, block
,
3565 max_blocks
, &map_bh
,
3566 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
);
3570 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3571 "returned error inode#%lu, block=%u, "
3572 "max_blocks=%u", __func__
,
3573 inode
->i_ino
, block
, max_blocks
);
3575 ext4_mark_inode_dirty(handle
, inode
);
3576 ret2
= ext4_journal_stop(handle
);
3579 if ((block
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3580 blkbits
) >> blkbits
))
3581 new_size
= offset
+ len
;
3583 new_size
= (block
+ ret
) << blkbits
;
3585 ext4_falloc_update_inode(inode
, mode
, new_size
,
3586 buffer_new(&map_bh
));
3587 ext4_mark_inode_dirty(handle
, inode
);
3588 ret2
= ext4_journal_stop(handle
);
3592 if (ret
== -ENOSPC
&&
3593 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3597 mutex_unlock(&inode
->i_mutex
);
3598 return ret
> 0 ? ret2
: ret
;
3602 * This function convert a range of blocks to written extents
3603 * The caller of this function will pass the start offset and the size.
3604 * all unwritten extents within this range will be converted to
3607 * This function is called from the direct IO end io call back
3608 * function, to convert the fallocated extents after IO is completed.
3609 * Returns 0 on success.
3611 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
3616 unsigned int max_blocks
;
3619 struct buffer_head map_bh
;
3620 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3622 block
= offset
>> blkbits
;
3624 * We can't just convert len to max_blocks because
3625 * If blocksize = 4096 offset = 3072 and len = 2048
3627 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3630 * credits to insert 1 extent into extent tree
3632 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3633 while (ret
>= 0 && ret
< max_blocks
) {
3634 block
= block
+ ret
;
3635 max_blocks
= max_blocks
- ret
;
3636 handle
= ext4_journal_start(inode
, credits
);
3637 if (IS_ERR(handle
)) {
3638 ret
= PTR_ERR(handle
);
3642 ret
= ext4_get_blocks(handle
, inode
, block
,
3643 max_blocks
, &map_bh
,
3644 EXT4_GET_BLOCKS_DIO_CONVERT_EXT
);
3647 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3648 "returned error inode#%lu, block=%u, "
3649 "max_blocks=%u", __func__
,
3650 inode
->i_ino
, block
, max_blocks
);
3652 ext4_mark_inode_dirty(handle
, inode
);
3653 ret2
= ext4_journal_stop(handle
);
3654 if (ret
<= 0 || ret2
)
3657 return ret
> 0 ? ret2
: ret
;
3660 * Callback function called for each extent to gather FIEMAP information.
3662 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3663 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3666 struct fiemap_extent_info
*fieinfo
= data
;
3667 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3674 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3676 if (newex
->ec_type
== EXT4_EXT_CACHE_GAP
) {
3679 struct buffer_head
*bh
= NULL
;
3681 offset
= logical
>> PAGE_SHIFT
;
3682 page
= find_get_page(inode
->i_mapping
, offset
);
3683 if (!page
|| !page_has_buffers(page
))
3684 return EXT_CONTINUE
;
3686 bh
= page_buffers(page
);
3689 return EXT_CONTINUE
;
3691 if (buffer_delay(bh
)) {
3692 flags
|= FIEMAP_EXTENT_DELALLOC
;
3693 page_cache_release(page
);
3695 page_cache_release(page
);
3696 return EXT_CONTINUE
;
3700 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3701 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3703 if (ex
&& ext4_ext_is_uninitialized(ex
))
3704 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3707 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3709 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3710 * this also indicates no more allocated blocks.
3712 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3714 if (ext4_ext_next_allocated_block(path
) == EXT_MAX_BLOCK
||
3715 newex
->ec_block
+ newex
->ec_len
- 1 == EXT_MAX_BLOCK
) {
3716 loff_t size
= i_size_read(inode
);
3717 loff_t bs
= EXT4_BLOCK_SIZE(inode
->i_sb
);
3719 flags
|= FIEMAP_EXTENT_LAST
;
3720 if ((flags
& FIEMAP_EXTENT_DELALLOC
) &&
3721 logical
+length
> size
)
3722 length
= (size
- logical
+ bs
- 1) & ~(bs
-1);
3725 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3732 return EXT_CONTINUE
;
3735 /* fiemap flags we can handle specified here */
3736 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3738 static int ext4_xattr_fiemap(struct inode
*inode
,
3739 struct fiemap_extent_info
*fieinfo
)
3743 __u32 flags
= FIEMAP_EXTENT_LAST
;
3744 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3748 if (EXT4_I(inode
)->i_state
& EXT4_STATE_XATTR
) {
3749 struct ext4_iloc iloc
;
3750 int offset
; /* offset of xattr in inode */
3752 error
= ext4_get_inode_loc(inode
, &iloc
);
3755 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3756 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3757 EXT4_I(inode
)->i_extra_isize
;
3759 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3760 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3761 } else { /* external block */
3762 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3763 length
= inode
->i_sb
->s_blocksize
;
3767 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3769 return (error
< 0 ? error
: 0);
3772 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3773 __u64 start
, __u64 len
)
3775 ext4_lblk_t start_blk
;
3778 /* fallback to generic here if not in extents fmt */
3779 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3780 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3783 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3786 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3787 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3789 ext4_lblk_t len_blks
;
3792 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3793 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
3794 if (last_blk
>= EXT_MAX_BLOCK
)
3795 last_blk
= EXT_MAX_BLOCK
-1;
3796 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
3799 * Walk the extent tree gathering extent information.
3800 * ext4_ext_fiemap_cb will push extents back to user.
3802 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3803 ext4_ext_fiemap_cb
, fieinfo
);