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
);
121 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
122 struct ext4_ext_path
*path
)
125 /* path points to block */
126 return ext4_journal_get_write_access(handle
, path
->p_bh
);
128 /* path points to leaf/index in inode body */
129 /* we use in-core data, no need to protect them */
139 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
140 struct ext4_ext_path
*path
)
144 /* path points to block */
145 err
= ext4_handle_dirty_metadata(handle
, inode
, path
->p_bh
);
147 /* path points to leaf/index in inode body */
148 err
= ext4_mark_inode_dirty(handle
, inode
);
153 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
154 struct ext4_ext_path
*path
,
157 struct ext4_inode_info
*ei
= EXT4_I(inode
);
158 ext4_fsblk_t bg_start
;
159 ext4_fsblk_t last_block
;
160 ext4_grpblk_t colour
;
161 ext4_group_t block_group
;
162 int flex_size
= ext4_flex_bg_size(EXT4_SB(inode
->i_sb
));
166 struct ext4_extent
*ex
;
167 depth
= path
->p_depth
;
169 /* try to predict block placement */
170 ex
= path
[depth
].p_ext
;
172 return ext_pblock(ex
)+(block
-le32_to_cpu(ex
->ee_block
));
174 /* it looks like index is empty;
175 * try to find starting block from index itself */
176 if (path
[depth
].p_bh
)
177 return path
[depth
].p_bh
->b_blocknr
;
180 /* OK. use inode's group */
181 block_group
= ei
->i_block_group
;
182 if (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) {
184 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
185 * block groups per flexgroup, reserve the first block
186 * group for directories and special files. Regular
187 * files will start at the second block group. This
188 * tends to speed up directory access and improves
191 block_group
&= ~(flex_size
-1);
192 if (S_ISREG(inode
->i_mode
))
195 bg_start
= (block_group
* EXT4_BLOCKS_PER_GROUP(inode
->i_sb
)) +
196 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_first_data_block
);
197 last_block
= ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
) - 1;
200 * If we are doing delayed allocation, we don't need take
201 * colour into account.
203 if (test_opt(inode
->i_sb
, DELALLOC
))
206 if (bg_start
+ EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) <= last_block
)
207 colour
= (current
->pid
% 16) *
208 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
210 colour
= (current
->pid
% 16) * ((last_block
- bg_start
) / 16);
211 return bg_start
+ colour
+ block
;
215 * Allocation for a meta data block
218 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
219 struct ext4_ext_path
*path
,
220 struct ext4_extent
*ex
, int *err
)
222 ext4_fsblk_t goal
, newblock
;
224 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
225 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, NULL
, err
);
229 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
233 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
234 / sizeof(struct ext4_extent
);
236 #ifdef AGGRESSIVE_TEST
244 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
248 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
249 / sizeof(struct ext4_extent_idx
);
251 #ifdef AGGRESSIVE_TEST
259 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
263 size
= sizeof(EXT4_I(inode
)->i_data
);
264 size
-= sizeof(struct ext4_extent_header
);
265 size
/= sizeof(struct ext4_extent
);
267 #ifdef AGGRESSIVE_TEST
275 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
279 size
= sizeof(EXT4_I(inode
)->i_data
);
280 size
-= sizeof(struct ext4_extent_header
);
281 size
/= sizeof(struct ext4_extent_idx
);
283 #ifdef AGGRESSIVE_TEST
292 * Calculate the number of metadata blocks needed
293 * to allocate @blocks
294 * Worse case is one block per extent
296 int ext4_ext_calc_metadata_amount(struct inode
*inode
, sector_t lblock
)
298 struct ext4_inode_info
*ei
= EXT4_I(inode
);
301 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
302 / sizeof(struct ext4_extent_idx
));
305 * If the new delayed allocation block is contiguous with the
306 * previous da block, it can share index blocks with the
307 * previous block, so we only need to allocate a new index
308 * block every idxs leaf blocks. At ldxs**2 blocks, we need
309 * an additional index block, and at ldxs**3 blocks, yet
310 * another index blocks.
312 if (ei
->i_da_metadata_calc_len
&&
313 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
314 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
316 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
318 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
320 ei
->i_da_metadata_calc_len
= 0;
322 ei
->i_da_metadata_calc_len
++;
323 ei
->i_da_metadata_calc_last_lblock
++;
328 * In the worst case we need a new set of index blocks at
329 * every level of the inode's extent tree.
331 ei
->i_da_metadata_calc_len
= 1;
332 ei
->i_da_metadata_calc_last_lblock
= lblock
;
333 return ext_depth(inode
) + 1;
337 ext4_ext_max_entries(struct inode
*inode
, int depth
)
341 if (depth
== ext_depth(inode
)) {
343 max
= ext4_ext_space_root(inode
, 1);
345 max
= ext4_ext_space_root_idx(inode
, 1);
348 max
= ext4_ext_space_block(inode
, 1);
350 max
= ext4_ext_space_block_idx(inode
, 1);
356 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
358 ext4_fsblk_t block
= ext_pblock(ext
);
359 int len
= ext4_ext_get_actual_len(ext
);
363 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
366 static int ext4_valid_extent_idx(struct inode
*inode
,
367 struct ext4_extent_idx
*ext_idx
)
369 ext4_fsblk_t block
= idx_pblock(ext_idx
);
371 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
374 static int ext4_valid_extent_entries(struct inode
*inode
,
375 struct ext4_extent_header
*eh
,
378 struct ext4_extent
*ext
;
379 struct ext4_extent_idx
*ext_idx
;
380 unsigned short entries
;
381 if (eh
->eh_entries
== 0)
384 entries
= le16_to_cpu(eh
->eh_entries
);
388 ext
= EXT_FIRST_EXTENT(eh
);
390 if (!ext4_valid_extent(inode
, ext
))
396 ext_idx
= EXT_FIRST_INDEX(eh
);
398 if (!ext4_valid_extent_idx(inode
, ext_idx
))
407 static int __ext4_ext_check(const char *function
, struct inode
*inode
,
408 struct ext4_extent_header
*eh
,
411 const char *error_msg
;
414 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
415 error_msg
= "invalid magic";
418 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
419 error_msg
= "unexpected eh_depth";
422 if (unlikely(eh
->eh_max
== 0)) {
423 error_msg
= "invalid eh_max";
426 max
= ext4_ext_max_entries(inode
, depth
);
427 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
428 error_msg
= "too large eh_max";
431 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
432 error_msg
= "invalid eh_entries";
435 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
436 error_msg
= "invalid extent entries";
442 ext4_error(inode
->i_sb
, function
,
443 "bad header/extent in inode #%lu: %s - magic %x, "
444 "entries %u, max %u(%u), depth %u(%u)",
445 inode
->i_ino
, error_msg
, le16_to_cpu(eh
->eh_magic
),
446 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
447 max
, le16_to_cpu(eh
->eh_depth
), depth
);
452 #define ext4_ext_check(inode, eh, depth) \
453 __ext4_ext_check(__func__, inode, eh, depth)
455 int ext4_ext_check_inode(struct inode
*inode
)
457 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
461 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
463 int k
, l
= path
->p_depth
;
466 for (k
= 0; k
<= l
; k
++, path
++) {
468 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
469 idx_pblock(path
->p_idx
));
470 } else if (path
->p_ext
) {
471 ext_debug(" %d:[%d]%d:%llu ",
472 le32_to_cpu(path
->p_ext
->ee_block
),
473 ext4_ext_is_uninitialized(path
->p_ext
),
474 ext4_ext_get_actual_len(path
->p_ext
),
475 ext_pblock(path
->p_ext
));
482 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
484 int depth
= ext_depth(inode
);
485 struct ext4_extent_header
*eh
;
486 struct ext4_extent
*ex
;
492 eh
= path
[depth
].p_hdr
;
493 ex
= EXT_FIRST_EXTENT(eh
);
495 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
497 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
498 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
499 ext4_ext_is_uninitialized(ex
),
500 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
505 #define ext4_ext_show_path(inode, path)
506 #define ext4_ext_show_leaf(inode, path)
509 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
511 int depth
= path
->p_depth
;
514 for (i
= 0; i
<= depth
; i
++, path
++)
522 * ext4_ext_binsearch_idx:
523 * binary search for the closest index of the given block
524 * the header must be checked before calling this
527 ext4_ext_binsearch_idx(struct inode
*inode
,
528 struct ext4_ext_path
*path
, ext4_lblk_t block
)
530 struct ext4_extent_header
*eh
= path
->p_hdr
;
531 struct ext4_extent_idx
*r
, *l
, *m
;
534 ext_debug("binsearch for %u(idx): ", block
);
536 l
= EXT_FIRST_INDEX(eh
) + 1;
537 r
= EXT_LAST_INDEX(eh
);
540 if (block
< le32_to_cpu(m
->ei_block
))
544 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
545 m
, le32_to_cpu(m
->ei_block
),
546 r
, le32_to_cpu(r
->ei_block
));
550 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
551 idx_pblock(path
->p_idx
));
553 #ifdef CHECK_BINSEARCH
555 struct ext4_extent_idx
*chix
, *ix
;
558 chix
= ix
= EXT_FIRST_INDEX(eh
);
559 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
561 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
562 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
564 ix
, EXT_FIRST_INDEX(eh
));
565 printk(KERN_DEBUG
"%u <= %u\n",
566 le32_to_cpu(ix
->ei_block
),
567 le32_to_cpu(ix
[-1].ei_block
));
569 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
570 <= le32_to_cpu(ix
[-1].ei_block
));
571 if (block
< le32_to_cpu(ix
->ei_block
))
575 BUG_ON(chix
!= path
->p_idx
);
582 * ext4_ext_binsearch:
583 * binary search for closest extent of the given block
584 * the header must be checked before calling this
587 ext4_ext_binsearch(struct inode
*inode
,
588 struct ext4_ext_path
*path
, ext4_lblk_t block
)
590 struct ext4_extent_header
*eh
= path
->p_hdr
;
591 struct ext4_extent
*r
, *l
, *m
;
593 if (eh
->eh_entries
== 0) {
595 * this leaf is empty:
596 * we get such a leaf in split/add case
601 ext_debug("binsearch for %u: ", block
);
603 l
= EXT_FIRST_EXTENT(eh
) + 1;
604 r
= EXT_LAST_EXTENT(eh
);
608 if (block
< le32_to_cpu(m
->ee_block
))
612 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
613 m
, le32_to_cpu(m
->ee_block
),
614 r
, le32_to_cpu(r
->ee_block
));
618 ext_debug(" -> %d:%llu:[%d]%d ",
619 le32_to_cpu(path
->p_ext
->ee_block
),
620 ext_pblock(path
->p_ext
),
621 ext4_ext_is_uninitialized(path
->p_ext
),
622 ext4_ext_get_actual_len(path
->p_ext
));
624 #ifdef CHECK_BINSEARCH
626 struct ext4_extent
*chex
, *ex
;
629 chex
= ex
= EXT_FIRST_EXTENT(eh
);
630 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
631 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
632 <= le32_to_cpu(ex
[-1].ee_block
));
633 if (block
< le32_to_cpu(ex
->ee_block
))
637 BUG_ON(chex
!= path
->p_ext
);
643 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
645 struct ext4_extent_header
*eh
;
647 eh
= ext_inode_hdr(inode
);
650 eh
->eh_magic
= EXT4_EXT_MAGIC
;
651 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
652 ext4_mark_inode_dirty(handle
, inode
);
653 ext4_ext_invalidate_cache(inode
);
657 struct ext4_ext_path
*
658 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
659 struct ext4_ext_path
*path
)
661 struct ext4_extent_header
*eh
;
662 struct buffer_head
*bh
;
663 short int depth
, i
, ppos
= 0, alloc
= 0;
665 eh
= ext_inode_hdr(inode
);
666 depth
= ext_depth(inode
);
668 /* account possible depth increase */
670 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
673 return ERR_PTR(-ENOMEM
);
680 /* walk through the tree */
682 int need_to_validate
= 0;
684 ext_debug("depth %d: num %d, max %d\n",
685 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
687 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
688 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
689 path
[ppos
].p_depth
= i
;
690 path
[ppos
].p_ext
= NULL
;
692 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
695 if (!bh_uptodate_or_lock(bh
)) {
696 if (bh_submit_read(bh
) < 0) {
700 /* validate the extent entries */
701 need_to_validate
= 1;
703 eh
= ext_block_hdr(bh
);
705 BUG_ON(ppos
> depth
);
706 path
[ppos
].p_bh
= bh
;
707 path
[ppos
].p_hdr
= eh
;
710 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
714 path
[ppos
].p_depth
= i
;
715 path
[ppos
].p_ext
= NULL
;
716 path
[ppos
].p_idx
= NULL
;
719 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
720 /* if not an empty leaf */
721 if (path
[ppos
].p_ext
)
722 path
[ppos
].p_block
= ext_pblock(path
[ppos
].p_ext
);
724 ext4_ext_show_path(inode
, path
);
729 ext4_ext_drop_refs(path
);
732 return ERR_PTR(-EIO
);
736 * ext4_ext_insert_index:
737 * insert new index [@logical;@ptr] into the block at @curp;
738 * check where to insert: before @curp or after @curp
740 int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
741 struct ext4_ext_path
*curp
,
742 int logical
, ext4_fsblk_t ptr
)
744 struct ext4_extent_idx
*ix
;
747 err
= ext4_ext_get_access(handle
, inode
, curp
);
751 BUG_ON(logical
== le32_to_cpu(curp
->p_idx
->ei_block
));
752 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
753 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
755 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
756 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
757 len
= len
< 0 ? 0 : len
;
758 ext_debug("insert new index %d after: %llu. "
759 "move %d from 0x%p to 0x%p\n",
761 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
762 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
764 ix
= curp
->p_idx
+ 1;
767 len
= len
* sizeof(struct ext4_extent_idx
);
768 len
= len
< 0 ? 0 : len
;
769 ext_debug("insert new index %d before: %llu. "
770 "move %d from 0x%p to 0x%p\n",
772 curp
->p_idx
, (curp
->p_idx
+ 1));
773 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
777 ix
->ei_block
= cpu_to_le32(logical
);
778 ext4_idx_store_pblock(ix
, ptr
);
779 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
781 BUG_ON(le16_to_cpu(curp
->p_hdr
->eh_entries
)
782 > le16_to_cpu(curp
->p_hdr
->eh_max
));
783 BUG_ON(ix
> EXT_LAST_INDEX(curp
->p_hdr
));
785 err
= ext4_ext_dirty(handle
, inode
, curp
);
786 ext4_std_error(inode
->i_sb
, err
);
793 * inserts new subtree into the path, using free index entry
795 * - allocates all needed blocks (new leaf and all intermediate index blocks)
796 * - makes decision where to split
797 * - moves remaining extents and index entries (right to the split point)
798 * into the newly allocated blocks
799 * - initializes subtree
801 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
802 struct ext4_ext_path
*path
,
803 struct ext4_extent
*newext
, int at
)
805 struct buffer_head
*bh
= NULL
;
806 int depth
= ext_depth(inode
);
807 struct ext4_extent_header
*neh
;
808 struct ext4_extent_idx
*fidx
;
809 struct ext4_extent
*ex
;
811 ext4_fsblk_t newblock
, oldblock
;
813 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
816 /* make decision: where to split? */
817 /* FIXME: now decision is simplest: at current extent */
819 /* if current leaf will be split, then we should use
820 * border from split point */
821 BUG_ON(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
));
822 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
823 border
= path
[depth
].p_ext
[1].ee_block
;
824 ext_debug("leaf will be split."
825 " next leaf starts at %d\n",
826 le32_to_cpu(border
));
828 border
= newext
->ee_block
;
829 ext_debug("leaf will be added."
830 " next leaf starts at %d\n",
831 le32_to_cpu(border
));
835 * If error occurs, then we break processing
836 * and mark filesystem read-only. index won't
837 * be inserted and tree will be in consistent
838 * state. Next mount will repair buffers too.
842 * Get array to track all allocated blocks.
843 * We need this to handle errors and free blocks
846 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
850 /* allocate all needed blocks */
851 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
852 for (a
= 0; a
< depth
- at
; a
++) {
853 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
857 ablocks
[a
] = newblock
;
860 /* initialize new leaf */
861 newblock
= ablocks
[--a
];
862 BUG_ON(newblock
== 0);
863 bh
= sb_getblk(inode
->i_sb
, newblock
);
870 err
= ext4_journal_get_create_access(handle
, bh
);
874 neh
= ext_block_hdr(bh
);
876 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
877 neh
->eh_magic
= EXT4_EXT_MAGIC
;
879 ex
= EXT_FIRST_EXTENT(neh
);
881 /* move remainder of path[depth] to the new leaf */
882 BUG_ON(path
[depth
].p_hdr
->eh_entries
!= path
[depth
].p_hdr
->eh_max
);
883 /* start copy from next extent */
884 /* TODO: we could do it by single memmove */
887 while (path
[depth
].p_ext
<=
888 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
889 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
890 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
891 ext_pblock(path
[depth
].p_ext
),
892 ext4_ext_is_uninitialized(path
[depth
].p_ext
),
893 ext4_ext_get_actual_len(path
[depth
].p_ext
),
895 /*memmove(ex++, path[depth].p_ext++,
896 sizeof(struct ext4_extent));
902 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
903 le16_add_cpu(&neh
->eh_entries
, m
);
906 set_buffer_uptodate(bh
);
909 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
915 /* correct old leaf */
917 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
920 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
921 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
927 /* create intermediate indexes */
931 ext_debug("create %d intermediate indices\n", k
);
932 /* insert new index into current index block */
933 /* current depth stored in i var */
937 newblock
= ablocks
[--a
];
938 bh
= sb_getblk(inode
->i_sb
, newblock
);
945 err
= ext4_journal_get_create_access(handle
, bh
);
949 neh
= ext_block_hdr(bh
);
950 neh
->eh_entries
= cpu_to_le16(1);
951 neh
->eh_magic
= EXT4_EXT_MAGIC
;
952 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
953 neh
->eh_depth
= cpu_to_le16(depth
- i
);
954 fidx
= EXT_FIRST_INDEX(neh
);
955 fidx
->ei_block
= border
;
956 ext4_idx_store_pblock(fidx
, oldblock
);
958 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
959 i
, newblock
, le32_to_cpu(border
), oldblock
);
964 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
965 EXT_MAX_INDEX(path
[i
].p_hdr
));
966 BUG_ON(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
967 EXT_LAST_INDEX(path
[i
].p_hdr
));
968 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
969 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
970 le32_to_cpu(path
[i
].p_idx
->ei_block
),
971 idx_pblock(path
[i
].p_idx
),
973 /*memmove(++fidx, path[i].p_idx++,
974 sizeof(struct ext4_extent_idx));
976 BUG_ON(neh->eh_entries > neh->eh_max);*/
981 memmove(++fidx
, path
[i
].p_idx
- m
,
982 sizeof(struct ext4_extent_idx
) * m
);
983 le16_add_cpu(&neh
->eh_entries
, m
);
985 set_buffer_uptodate(bh
);
988 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
994 /* correct old index */
996 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
999 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1000 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1008 /* insert new index */
1009 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1010 le32_to_cpu(border
), newblock
);
1014 if (buffer_locked(bh
))
1020 /* free all allocated blocks in error case */
1021 for (i
= 0; i
< depth
; i
++) {
1024 ext4_free_blocks(handle
, inode
, ablocks
[i
], 1, 1);
1033 * ext4_ext_grow_indepth:
1034 * implements tree growing procedure:
1035 * - allocates new block
1036 * - moves top-level data (index block or leaf) into the new block
1037 * - initializes new top-level, creating index that points to the
1038 * just created block
1040 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1041 struct ext4_ext_path
*path
,
1042 struct ext4_extent
*newext
)
1044 struct ext4_ext_path
*curp
= path
;
1045 struct ext4_extent_header
*neh
;
1046 struct ext4_extent_idx
*fidx
;
1047 struct buffer_head
*bh
;
1048 ext4_fsblk_t newblock
;
1051 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
, newext
, &err
);
1055 bh
= sb_getblk(inode
->i_sb
, newblock
);
1058 ext4_std_error(inode
->i_sb
, err
);
1063 err
= ext4_journal_get_create_access(handle
, bh
);
1069 /* move top-level index/leaf into new block */
1070 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1072 /* set size of new block */
1073 neh
= ext_block_hdr(bh
);
1074 /* old root could have indexes or leaves
1075 * so calculate e_max right way */
1076 if (ext_depth(inode
))
1077 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1079 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1080 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1081 set_buffer_uptodate(bh
);
1084 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1088 /* create index in new top-level index: num,max,pointer */
1089 err
= ext4_ext_get_access(handle
, inode
, curp
);
1093 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1094 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1095 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1096 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1098 if (path
[0].p_hdr
->eh_depth
)
1099 curp
->p_idx
->ei_block
=
1100 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1102 curp
->p_idx
->ei_block
=
1103 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1104 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1106 neh
= ext_inode_hdr(inode
);
1107 fidx
= EXT_FIRST_INDEX(neh
);
1108 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1109 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1110 le32_to_cpu(fidx
->ei_block
), idx_pblock(fidx
));
1112 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1113 err
= ext4_ext_dirty(handle
, inode
, curp
);
1121 * ext4_ext_create_new_leaf:
1122 * finds empty index and adds new leaf.
1123 * if no free index is found, then it requests in-depth growing.
1125 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1126 struct ext4_ext_path
*path
,
1127 struct ext4_extent
*newext
)
1129 struct ext4_ext_path
*curp
;
1130 int depth
, i
, err
= 0;
1133 i
= depth
= ext_depth(inode
);
1135 /* walk up to the tree and look for free index entry */
1136 curp
= path
+ depth
;
1137 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1142 /* we use already allocated block for index block,
1143 * so subsequent data blocks should be contiguous */
1144 if (EXT_HAS_FREE_INDEX(curp
)) {
1145 /* if we found index with free entry, then use that
1146 * entry: create all needed subtree and add new leaf */
1147 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
1152 ext4_ext_drop_refs(path
);
1153 path
= ext4_ext_find_extent(inode
,
1154 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1157 err
= PTR_ERR(path
);
1159 /* tree is full, time to grow in depth */
1160 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1165 ext4_ext_drop_refs(path
);
1166 path
= ext4_ext_find_extent(inode
,
1167 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1170 err
= PTR_ERR(path
);
1175 * only first (depth 0 -> 1) produces free space;
1176 * in all other cases we have to split the grown tree
1178 depth
= ext_depth(inode
);
1179 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1180 /* now we need to split */
1190 * search the closest allocated block to the left for *logical
1191 * and returns it at @logical + it's physical address at @phys
1192 * if *logical is the smallest allocated block, the function
1193 * returns 0 at @phys
1194 * return value contains 0 (success) or error code
1197 ext4_ext_search_left(struct inode
*inode
, struct ext4_ext_path
*path
,
1198 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1200 struct ext4_extent_idx
*ix
;
1201 struct ext4_extent
*ex
;
1204 BUG_ON(path
== NULL
);
1205 depth
= path
->p_depth
;
1208 if (depth
== 0 && path
->p_ext
== NULL
)
1211 /* usually extent in the path covers blocks smaller
1212 * then *logical, but it can be that extent is the
1213 * first one in the file */
1215 ex
= path
[depth
].p_ext
;
1216 ee_len
= ext4_ext_get_actual_len(ex
);
1217 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1218 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1219 while (--depth
>= 0) {
1220 ix
= path
[depth
].p_idx
;
1221 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1226 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1228 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1229 *phys
= ext_pblock(ex
) + ee_len
- 1;
1234 * search the closest allocated block to the right for *logical
1235 * and returns it at @logical + it's physical address at @phys
1236 * if *logical is the smallest allocated block, the function
1237 * returns 0 at @phys
1238 * return value contains 0 (success) or error code
1241 ext4_ext_search_right(struct inode
*inode
, struct ext4_ext_path
*path
,
1242 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1244 struct buffer_head
*bh
= NULL
;
1245 struct ext4_extent_header
*eh
;
1246 struct ext4_extent_idx
*ix
;
1247 struct ext4_extent
*ex
;
1249 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1252 BUG_ON(path
== NULL
);
1253 depth
= path
->p_depth
;
1256 if (depth
== 0 && path
->p_ext
== NULL
)
1259 /* usually extent in the path covers blocks smaller
1260 * then *logical, but it can be that extent is the
1261 * first one in the file */
1263 ex
= path
[depth
].p_ext
;
1264 ee_len
= ext4_ext_get_actual_len(ex
);
1265 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1266 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1267 while (--depth
>= 0) {
1268 ix
= path
[depth
].p_idx
;
1269 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1271 *logical
= le32_to_cpu(ex
->ee_block
);
1272 *phys
= ext_pblock(ex
);
1276 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1278 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1279 /* next allocated block in this leaf */
1281 *logical
= le32_to_cpu(ex
->ee_block
);
1282 *phys
= ext_pblock(ex
);
1286 /* go up and search for index to the right */
1287 while (--depth
>= 0) {
1288 ix
= path
[depth
].p_idx
;
1289 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1293 /* we've gone up to the root and found no index to the right */
1297 /* we've found index to the right, let's
1298 * follow it and find the closest allocated
1299 * block to the right */
1301 block
= idx_pblock(ix
);
1302 while (++depth
< path
->p_depth
) {
1303 bh
= sb_bread(inode
->i_sb
, block
);
1306 eh
= ext_block_hdr(bh
);
1307 /* subtract from p_depth to get proper eh_depth */
1308 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1312 ix
= EXT_FIRST_INDEX(eh
);
1313 block
= idx_pblock(ix
);
1317 bh
= sb_bread(inode
->i_sb
, block
);
1320 eh
= ext_block_hdr(bh
);
1321 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1325 ex
= EXT_FIRST_EXTENT(eh
);
1326 *logical
= le32_to_cpu(ex
->ee_block
);
1327 *phys
= ext_pblock(ex
);
1333 * ext4_ext_next_allocated_block:
1334 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1335 * NOTE: it considers block number from index entry as
1336 * allocated block. Thus, index entries have to be consistent
1340 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1344 BUG_ON(path
== NULL
);
1345 depth
= path
->p_depth
;
1347 if (depth
== 0 && path
->p_ext
== NULL
)
1348 return EXT_MAX_BLOCK
;
1350 while (depth
>= 0) {
1351 if (depth
== path
->p_depth
) {
1353 if (path
[depth
].p_ext
!=
1354 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1355 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1358 if (path
[depth
].p_idx
!=
1359 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1360 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1365 return EXT_MAX_BLOCK
;
1369 * ext4_ext_next_leaf_block:
1370 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1372 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1373 struct ext4_ext_path
*path
)
1377 BUG_ON(path
== NULL
);
1378 depth
= path
->p_depth
;
1380 /* zero-tree has no leaf blocks at all */
1382 return EXT_MAX_BLOCK
;
1384 /* go to index block */
1387 while (depth
>= 0) {
1388 if (path
[depth
].p_idx
!=
1389 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1390 return (ext4_lblk_t
)
1391 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1395 return EXT_MAX_BLOCK
;
1399 * ext4_ext_correct_indexes:
1400 * if leaf gets modified and modified extent is first in the leaf,
1401 * then we have to correct all indexes above.
1402 * TODO: do we need to correct tree in all cases?
1404 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1405 struct ext4_ext_path
*path
)
1407 struct ext4_extent_header
*eh
;
1408 int depth
= ext_depth(inode
);
1409 struct ext4_extent
*ex
;
1413 eh
= path
[depth
].p_hdr
;
1414 ex
= path
[depth
].p_ext
;
1419 /* there is no tree at all */
1423 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1424 /* we correct tree if first leaf got modified only */
1429 * TODO: we need correction if border is smaller than current one
1432 border
= path
[depth
].p_ext
->ee_block
;
1433 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1436 path
[k
].p_idx
->ei_block
= border
;
1437 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1442 /* change all left-side indexes */
1443 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1445 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1448 path
[k
].p_idx
->ei_block
= border
;
1449 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1458 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1459 struct ext4_extent
*ex2
)
1461 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1464 * Make sure that either both extents are uninitialized, or
1467 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1470 if (ext4_ext_is_uninitialized(ex1
))
1471 max_len
= EXT_UNINIT_MAX_LEN
;
1473 max_len
= EXT_INIT_MAX_LEN
;
1475 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1476 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1478 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1479 le32_to_cpu(ex2
->ee_block
))
1483 * To allow future support for preallocated extents to be added
1484 * as an RO_COMPAT feature, refuse to merge to extents if
1485 * this can result in the top bit of ee_len being set.
1487 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1489 #ifdef AGGRESSIVE_TEST
1490 if (ext1_ee_len
>= 4)
1494 if (ext_pblock(ex1
) + ext1_ee_len
== ext_pblock(ex2
))
1500 * This function tries to merge the "ex" extent to the next extent in the tree.
1501 * It always tries to merge towards right. If you want to merge towards
1502 * left, pass "ex - 1" as argument instead of "ex".
1503 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1504 * 1 if they got merged.
1506 int ext4_ext_try_to_merge(struct inode
*inode
,
1507 struct ext4_ext_path
*path
,
1508 struct ext4_extent
*ex
)
1510 struct ext4_extent_header
*eh
;
1511 unsigned int depth
, len
;
1513 int uninitialized
= 0;
1515 depth
= ext_depth(inode
);
1516 BUG_ON(path
[depth
].p_hdr
== NULL
);
1517 eh
= path
[depth
].p_hdr
;
1519 while (ex
< EXT_LAST_EXTENT(eh
)) {
1520 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1522 /* merge with next extent! */
1523 if (ext4_ext_is_uninitialized(ex
))
1525 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1526 + ext4_ext_get_actual_len(ex
+ 1));
1528 ext4_ext_mark_uninitialized(ex
);
1530 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1531 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1532 * sizeof(struct ext4_extent
);
1533 memmove(ex
+ 1, ex
+ 2, len
);
1535 le16_add_cpu(&eh
->eh_entries
, -1);
1537 WARN_ON(eh
->eh_entries
== 0);
1538 if (!eh
->eh_entries
)
1539 ext4_error(inode
->i_sb
, "ext4_ext_try_to_merge",
1540 "inode#%lu, eh->eh_entries = 0!", inode
->i_ino
);
1547 * check if a portion of the "newext" extent overlaps with an
1550 * If there is an overlap discovered, it updates the length of the newext
1551 * such that there will be no overlap, and then returns 1.
1552 * If there is no overlap found, it returns 0.
1554 unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1555 struct ext4_extent
*newext
,
1556 struct ext4_ext_path
*path
)
1559 unsigned int depth
, len1
;
1560 unsigned int ret
= 0;
1562 b1
= le32_to_cpu(newext
->ee_block
);
1563 len1
= ext4_ext_get_actual_len(newext
);
1564 depth
= ext_depth(inode
);
1565 if (!path
[depth
].p_ext
)
1567 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1570 * get the next allocated block if the extent in the path
1571 * is before the requested block(s)
1574 b2
= ext4_ext_next_allocated_block(path
);
1575 if (b2
== EXT_MAX_BLOCK
)
1579 /* check for wrap through zero on extent logical start block*/
1580 if (b1
+ len1
< b1
) {
1581 len1
= EXT_MAX_BLOCK
- b1
;
1582 newext
->ee_len
= cpu_to_le16(len1
);
1586 /* check for overlap */
1587 if (b1
+ len1
> b2
) {
1588 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1596 * ext4_ext_insert_extent:
1597 * tries to merge requsted extent into the existing extent or
1598 * inserts requested extent as new one into the tree,
1599 * creating new leaf in the no-space case.
1601 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1602 struct ext4_ext_path
*path
,
1603 struct ext4_extent
*newext
, int flag
)
1605 struct ext4_extent_header
*eh
;
1606 struct ext4_extent
*ex
, *fex
;
1607 struct ext4_extent
*nearex
; /* nearest extent */
1608 struct ext4_ext_path
*npath
= NULL
;
1609 int depth
, len
, err
;
1611 unsigned uninitialized
= 0;
1613 BUG_ON(ext4_ext_get_actual_len(newext
) == 0);
1614 depth
= ext_depth(inode
);
1615 ex
= path
[depth
].p_ext
;
1616 BUG_ON(path
[depth
].p_hdr
== NULL
);
1618 /* try to insert block into found extent and return */
1619 if (ex
&& (flag
!= EXT4_GET_BLOCKS_DIO_CREATE_EXT
)
1620 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1621 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1622 ext4_ext_is_uninitialized(newext
),
1623 ext4_ext_get_actual_len(newext
),
1624 le32_to_cpu(ex
->ee_block
),
1625 ext4_ext_is_uninitialized(ex
),
1626 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
1627 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1632 * ext4_can_extents_be_merged should have checked that either
1633 * both extents are uninitialized, or both aren't. Thus we
1634 * need to check only one of them here.
1636 if (ext4_ext_is_uninitialized(ex
))
1638 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1639 + ext4_ext_get_actual_len(newext
));
1641 ext4_ext_mark_uninitialized(ex
);
1642 eh
= path
[depth
].p_hdr
;
1648 depth
= ext_depth(inode
);
1649 eh
= path
[depth
].p_hdr
;
1650 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1653 /* probably next leaf has space for us? */
1654 fex
= EXT_LAST_EXTENT(eh
);
1655 next
= ext4_ext_next_leaf_block(inode
, path
);
1656 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1657 && next
!= EXT_MAX_BLOCK
) {
1658 ext_debug("next leaf block - %d\n", next
);
1659 BUG_ON(npath
!= NULL
);
1660 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1662 return PTR_ERR(npath
);
1663 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1664 eh
= npath
[depth
].p_hdr
;
1665 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1666 ext_debug("next leaf isnt full(%d)\n",
1667 le16_to_cpu(eh
->eh_entries
));
1671 ext_debug("next leaf has no free space(%d,%d)\n",
1672 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1676 * There is no free space in the found leaf.
1677 * We're gonna add a new leaf in the tree.
1679 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1682 depth
= ext_depth(inode
);
1683 eh
= path
[depth
].p_hdr
;
1686 nearex
= path
[depth
].p_ext
;
1688 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1693 /* there is no extent in this leaf, create first one */
1694 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1695 le32_to_cpu(newext
->ee_block
),
1697 ext4_ext_is_uninitialized(newext
),
1698 ext4_ext_get_actual_len(newext
));
1699 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1700 } else if (le32_to_cpu(newext
->ee_block
)
1701 > le32_to_cpu(nearex
->ee_block
)) {
1702 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1703 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1704 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1705 len
= (len
- 1) * sizeof(struct ext4_extent
);
1706 len
= len
< 0 ? 0 : len
;
1707 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1708 "move %d from 0x%p to 0x%p\n",
1709 le32_to_cpu(newext
->ee_block
),
1711 ext4_ext_is_uninitialized(newext
),
1712 ext4_ext_get_actual_len(newext
),
1713 nearex
, len
, nearex
+ 1, nearex
+ 2);
1714 memmove(nearex
+ 2, nearex
+ 1, len
);
1716 path
[depth
].p_ext
= nearex
+ 1;
1718 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1719 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1720 len
= len
< 0 ? 0 : len
;
1721 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1722 "move %d from 0x%p to 0x%p\n",
1723 le32_to_cpu(newext
->ee_block
),
1725 ext4_ext_is_uninitialized(newext
),
1726 ext4_ext_get_actual_len(newext
),
1727 nearex
, len
, nearex
+ 1, nearex
+ 2);
1728 memmove(nearex
+ 1, nearex
, len
);
1729 path
[depth
].p_ext
= nearex
;
1732 le16_add_cpu(&eh
->eh_entries
, 1);
1733 nearex
= path
[depth
].p_ext
;
1734 nearex
->ee_block
= newext
->ee_block
;
1735 ext4_ext_store_pblock(nearex
, ext_pblock(newext
));
1736 nearex
->ee_len
= newext
->ee_len
;
1739 /* try to merge extents to the right */
1740 if (flag
!= EXT4_GET_BLOCKS_DIO_CREATE_EXT
)
1741 ext4_ext_try_to_merge(inode
, path
, nearex
);
1743 /* try to merge extents to the left */
1745 /* time to correct all indexes above */
1746 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1750 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1754 ext4_ext_drop_refs(npath
);
1757 ext4_ext_invalidate_cache(inode
);
1761 int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1762 ext4_lblk_t num
, ext_prepare_callback func
,
1765 struct ext4_ext_path
*path
= NULL
;
1766 struct ext4_ext_cache cbex
;
1767 struct ext4_extent
*ex
;
1768 ext4_lblk_t next
, start
= 0, end
= 0;
1769 ext4_lblk_t last
= block
+ num
;
1770 int depth
, exists
, err
= 0;
1772 BUG_ON(func
== NULL
);
1773 BUG_ON(inode
== NULL
);
1775 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1777 /* find extent for this block */
1778 down_read(&EXT4_I(inode
)->i_data_sem
);
1779 path
= ext4_ext_find_extent(inode
, block
, path
);
1780 up_read(&EXT4_I(inode
)->i_data_sem
);
1782 err
= PTR_ERR(path
);
1787 depth
= ext_depth(inode
);
1788 BUG_ON(path
[depth
].p_hdr
== NULL
);
1789 ex
= path
[depth
].p_ext
;
1790 next
= ext4_ext_next_allocated_block(path
);
1794 /* there is no extent yet, so try to allocate
1795 * all requested space */
1798 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1799 /* need to allocate space before found extent */
1801 end
= le32_to_cpu(ex
->ee_block
);
1802 if (block
+ num
< end
)
1804 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1805 + ext4_ext_get_actual_len(ex
)) {
1806 /* need to allocate space after found extent */
1811 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1813 * some part of requested space is covered
1817 end
= le32_to_cpu(ex
->ee_block
)
1818 + ext4_ext_get_actual_len(ex
);
1819 if (block
+ num
< end
)
1825 BUG_ON(end
<= start
);
1828 cbex
.ec_block
= start
;
1829 cbex
.ec_len
= end
- start
;
1831 cbex
.ec_type
= EXT4_EXT_CACHE_GAP
;
1833 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1834 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1835 cbex
.ec_start
= ext_pblock(ex
);
1836 cbex
.ec_type
= EXT4_EXT_CACHE_EXTENT
;
1839 BUG_ON(cbex
.ec_len
== 0);
1840 err
= func(inode
, path
, &cbex
, ex
, cbdata
);
1841 ext4_ext_drop_refs(path
);
1846 if (err
== EXT_REPEAT
)
1848 else if (err
== EXT_BREAK
) {
1853 if (ext_depth(inode
) != depth
) {
1854 /* depth was changed. we have to realloc path */
1859 block
= cbex
.ec_block
+ cbex
.ec_len
;
1863 ext4_ext_drop_refs(path
);
1871 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1872 __u32 len
, ext4_fsblk_t start
, int type
)
1874 struct ext4_ext_cache
*cex
;
1876 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1877 cex
= &EXT4_I(inode
)->i_cached_extent
;
1878 cex
->ec_type
= type
;
1879 cex
->ec_block
= block
;
1881 cex
->ec_start
= start
;
1882 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1886 * ext4_ext_put_gap_in_cache:
1887 * calculate boundaries of the gap that the requested block fits into
1888 * and cache this gap
1891 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1894 int depth
= ext_depth(inode
);
1897 struct ext4_extent
*ex
;
1899 ex
= path
[depth
].p_ext
;
1901 /* there is no extent yet, so gap is [0;-] */
1903 len
= EXT_MAX_BLOCK
;
1904 ext_debug("cache gap(whole file):");
1905 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1907 len
= le32_to_cpu(ex
->ee_block
) - block
;
1908 ext_debug("cache gap(before): %u [%u:%u]",
1910 le32_to_cpu(ex
->ee_block
),
1911 ext4_ext_get_actual_len(ex
));
1912 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1913 + ext4_ext_get_actual_len(ex
)) {
1915 lblock
= le32_to_cpu(ex
->ee_block
)
1916 + ext4_ext_get_actual_len(ex
);
1918 next
= ext4_ext_next_allocated_block(path
);
1919 ext_debug("cache gap(after): [%u:%u] %u",
1920 le32_to_cpu(ex
->ee_block
),
1921 ext4_ext_get_actual_len(ex
),
1923 BUG_ON(next
== lblock
);
1924 len
= next
- lblock
;
1930 ext_debug(" -> %u:%lu\n", lblock
, len
);
1931 ext4_ext_put_in_cache(inode
, lblock
, len
, 0, EXT4_EXT_CACHE_GAP
);
1935 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1936 struct ext4_extent
*ex
)
1938 struct ext4_ext_cache
*cex
;
1939 int ret
= EXT4_EXT_CACHE_NO
;
1942 * We borrow i_block_reservation_lock to protect i_cached_extent
1944 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1945 cex
= &EXT4_I(inode
)->i_cached_extent
;
1947 /* has cache valid data? */
1948 if (cex
->ec_type
== EXT4_EXT_CACHE_NO
)
1951 BUG_ON(cex
->ec_type
!= EXT4_EXT_CACHE_GAP
&&
1952 cex
->ec_type
!= EXT4_EXT_CACHE_EXTENT
);
1953 if (in_range(block
, cex
->ec_block
, cex
->ec_len
)) {
1954 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
1955 ext4_ext_store_pblock(ex
, cex
->ec_start
);
1956 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
1957 ext_debug("%u cached by %u:%u:%llu\n",
1959 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
1963 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1969 * removes index from the index block.
1970 * It's used in truncate case only, thus all requests are for
1971 * last index in the block only.
1973 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
1974 struct ext4_ext_path
*path
)
1976 struct buffer_head
*bh
;
1980 /* free index block */
1982 leaf
= idx_pblock(path
->p_idx
);
1983 BUG_ON(path
->p_hdr
->eh_entries
== 0);
1984 err
= ext4_ext_get_access(handle
, inode
, path
);
1987 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
1988 err
= ext4_ext_dirty(handle
, inode
, path
);
1991 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
1992 bh
= sb_find_get_block(inode
->i_sb
, leaf
);
1993 ext4_forget(handle
, 1, inode
, bh
, leaf
);
1994 ext4_free_blocks(handle
, inode
, leaf
, 1, 1);
1999 * ext4_ext_calc_credits_for_single_extent:
2000 * This routine returns max. credits that needed to insert an extent
2001 * to the extent tree.
2002 * When pass the actual path, the caller should calculate credits
2005 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2006 struct ext4_ext_path
*path
)
2009 int depth
= ext_depth(inode
);
2012 /* probably there is space in leaf? */
2013 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2014 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2017 * There are some space in the leaf tree, no
2018 * need to account for leaf block credit
2020 * bitmaps and block group descriptor blocks
2021 * and other metadat blocks still need to be
2024 /* 1 bitmap, 1 block group descriptor */
2025 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2030 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2034 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2036 * if nrblocks are fit in a single extent (chunk flag is 1), then
2037 * in the worse case, each tree level index/leaf need to be changed
2038 * if the tree split due to insert a new extent, then the old tree
2039 * index/leaf need to be updated too
2041 * If the nrblocks are discontiguous, they could cause
2042 * the whole tree split more than once, but this is really rare.
2044 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2047 int depth
= ext_depth(inode
);
2057 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2058 struct ext4_extent
*ex
,
2059 ext4_lblk_t from
, ext4_lblk_t to
)
2061 struct buffer_head
*bh
;
2062 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2063 int i
, metadata
= 0;
2065 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2067 #ifdef EXTENTS_STATS
2069 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2070 spin_lock(&sbi
->s_ext_stats_lock
);
2071 sbi
->s_ext_blocks
+= ee_len
;
2072 sbi
->s_ext_extents
++;
2073 if (ee_len
< sbi
->s_ext_min
)
2074 sbi
->s_ext_min
= ee_len
;
2075 if (ee_len
> sbi
->s_ext_max
)
2076 sbi
->s_ext_max
= ee_len
;
2077 if (ext_depth(inode
) > sbi
->s_depth_max
)
2078 sbi
->s_depth_max
= ext_depth(inode
);
2079 spin_unlock(&sbi
->s_ext_stats_lock
);
2082 if (from
>= le32_to_cpu(ex
->ee_block
)
2083 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2088 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2089 start
= ext_pblock(ex
) + ee_len
- num
;
2090 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2091 for (i
= 0; i
< num
; i
++) {
2092 bh
= sb_find_get_block(inode
->i_sb
, start
+ i
);
2093 ext4_forget(handle
, metadata
, inode
, bh
, start
+ i
);
2095 ext4_free_blocks(handle
, inode
, start
, num
, metadata
);
2096 } else if (from
== le32_to_cpu(ex
->ee_block
)
2097 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2098 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
2099 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2101 printk(KERN_INFO
"strange request: removal(2) "
2102 "%u-%u from %u:%u\n",
2103 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2109 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2110 struct ext4_ext_path
*path
, ext4_lblk_t start
)
2112 int err
= 0, correct_index
= 0;
2113 int depth
= ext_depth(inode
), credits
;
2114 struct ext4_extent_header
*eh
;
2115 ext4_lblk_t a
, b
, block
;
2117 ext4_lblk_t ex_ee_block
;
2118 unsigned short ex_ee_len
;
2119 unsigned uninitialized
= 0;
2120 struct ext4_extent
*ex
;
2122 /* the header must be checked already in ext4_ext_remove_space() */
2123 ext_debug("truncate since %u in leaf\n", start
);
2124 if (!path
[depth
].p_hdr
)
2125 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2126 eh
= path
[depth
].p_hdr
;
2129 /* find where to start removing */
2130 ex
= EXT_LAST_EXTENT(eh
);
2132 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2133 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2135 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2136 ex_ee_block
+ ex_ee_len
> start
) {
2138 if (ext4_ext_is_uninitialized(ex
))
2143 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2144 uninitialized
, ex_ee_len
);
2145 path
[depth
].p_ext
= ex
;
2147 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2148 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
2149 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
2151 ext_debug(" border %u:%u\n", a
, b
);
2153 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
2157 } else if (a
!= ex_ee_block
) {
2158 /* remove tail of the extent */
2159 block
= ex_ee_block
;
2161 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2162 /* remove head of the extent */
2165 /* there is no "make a hole" API yet */
2168 /* remove whole extent: excellent! */
2169 block
= ex_ee_block
;
2171 BUG_ON(a
!= ex_ee_block
);
2172 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
2176 * 3 for leaf, sb, and inode plus 2 (bmap and group
2177 * descriptor) for each block group; assume two block
2178 * groups plus ex_ee_len/blocks_per_block_group for
2181 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2182 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2184 credits
+= (ext_depth(inode
)) + 1;
2186 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2188 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2192 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2196 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2201 /* this extent is removed; mark slot entirely unused */
2202 ext4_ext_store_pblock(ex
, 0);
2203 le16_add_cpu(&eh
->eh_entries
, -1);
2206 ex
->ee_block
= cpu_to_le32(block
);
2207 ex
->ee_len
= cpu_to_le16(num
);
2209 * Do not mark uninitialized if all the blocks in the
2210 * extent have been removed.
2212 if (uninitialized
&& num
)
2213 ext4_ext_mark_uninitialized(ex
);
2215 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2219 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2222 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2223 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2226 if (correct_index
&& eh
->eh_entries
)
2227 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2229 /* if this leaf is free, then we should
2230 * remove it from index block above */
2231 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2232 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2239 * ext4_ext_more_to_rm:
2240 * returns 1 if current index has to be freed (even partial)
2243 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2245 BUG_ON(path
->p_idx
== NULL
);
2247 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2251 * if truncate on deeper level happened, it wasn't partial,
2252 * so we have to consider current index for truncation
2254 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2259 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2261 struct super_block
*sb
= inode
->i_sb
;
2262 int depth
= ext_depth(inode
);
2263 struct ext4_ext_path
*path
;
2267 ext_debug("truncate since %u\n", start
);
2269 /* probably first extent we're gonna free will be last in block */
2270 handle
= ext4_journal_start(inode
, depth
+ 1);
2272 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 depth
= ext_depth(inode
);
2282 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2284 ext4_journal_stop(handle
);
2287 path
[0].p_depth
= depth
;
2288 path
[0].p_hdr
= ext_inode_hdr(inode
);
2289 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2295 while (i
>= 0 && err
== 0) {
2297 /* this is leaf block */
2298 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2299 /* root level has p_bh == NULL, brelse() eats this */
2300 brelse(path
[i
].p_bh
);
2301 path
[i
].p_bh
= NULL
;
2306 /* this is index block */
2307 if (!path
[i
].p_hdr
) {
2308 ext_debug("initialize header\n");
2309 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2312 if (!path
[i
].p_idx
) {
2313 /* this level hasn't been touched yet */
2314 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2315 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2316 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2318 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2320 /* we were already here, see at next index */
2324 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2325 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2327 if (ext4_ext_more_to_rm(path
+ i
)) {
2328 struct buffer_head
*bh
;
2329 /* go to the next level */
2330 ext_debug("move to level %d (block %llu)\n",
2331 i
+ 1, idx_pblock(path
[i
].p_idx
));
2332 memset(path
+ i
+ 1, 0, sizeof(*path
));
2333 bh
= sb_bread(sb
, idx_pblock(path
[i
].p_idx
));
2335 /* should we reset i_size? */
2339 if (WARN_ON(i
+ 1 > depth
)) {
2343 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2348 path
[i
+ 1].p_bh
= bh
;
2350 /* save actual number of indexes since this
2351 * number is changed at the next iteration */
2352 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2355 /* we finished processing this index, go up */
2356 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2357 /* index is empty, remove it;
2358 * handle must be already prepared by the
2359 * truncatei_leaf() */
2360 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2362 /* root level has p_bh == NULL, brelse() eats this */
2363 brelse(path
[i
].p_bh
);
2364 path
[i
].p_bh
= NULL
;
2366 ext_debug("return to level %d\n", i
);
2370 /* TODO: flexible tree reduction should be here */
2371 if (path
->p_hdr
->eh_entries
== 0) {
2373 * truncate to zero freed all the tree,
2374 * so we need to correct eh_depth
2376 err
= ext4_ext_get_access(handle
, inode
, path
);
2378 ext_inode_hdr(inode
)->eh_depth
= 0;
2379 ext_inode_hdr(inode
)->eh_max
=
2380 cpu_to_le16(ext4_ext_space_root(inode
, 0));
2381 err
= ext4_ext_dirty(handle
, inode
, path
);
2385 ext4_ext_drop_refs(path
);
2389 ext4_journal_stop(handle
);
2395 * called at mount time
2397 void ext4_ext_init(struct super_block
*sb
)
2400 * possible initialization would be here
2403 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2404 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2405 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2406 #ifdef AGGRESSIVE_TEST
2407 printk(", aggressive tests");
2409 #ifdef CHECK_BINSEARCH
2410 printk(", check binsearch");
2412 #ifdef EXTENTS_STATS
2417 #ifdef EXTENTS_STATS
2418 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2419 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2420 EXT4_SB(sb
)->s_ext_max
= 0;
2426 * called at umount time
2428 void ext4_ext_release(struct super_block
*sb
)
2430 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2433 #ifdef EXTENTS_STATS
2434 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2435 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2436 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2437 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2438 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2439 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2440 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2445 static void bi_complete(struct bio
*bio
, int error
)
2447 complete((struct completion
*)bio
->bi_private
);
2450 /* FIXME!! we need to try to merge to left or right after zero-out */
2451 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2455 int blkbits
, blocksize
;
2457 struct completion event
;
2458 unsigned int ee_len
, len
, done
, offset
;
2461 blkbits
= inode
->i_blkbits
;
2462 blocksize
= inode
->i_sb
->s_blocksize
;
2463 ee_len
= ext4_ext_get_actual_len(ex
);
2464 ee_pblock
= ext_pblock(ex
);
2466 /* convert ee_pblock to 512 byte sectors */
2467 ee_pblock
= ee_pblock
<< (blkbits
- 9);
2469 while (ee_len
> 0) {
2471 if (ee_len
> BIO_MAX_PAGES
)
2472 len
= BIO_MAX_PAGES
;
2476 bio
= bio_alloc(GFP_NOIO
, len
);
2480 bio
->bi_sector
= ee_pblock
;
2481 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
2485 while (done
< len
) {
2486 ret
= bio_add_page(bio
, ZERO_PAGE(0),
2488 if (ret
!= blocksize
) {
2490 * We can't add any more pages because of
2491 * hardware limitations. Start a new bio.
2496 offset
+= blocksize
;
2497 if (offset
>= PAGE_CACHE_SIZE
)
2501 init_completion(&event
);
2502 bio
->bi_private
= &event
;
2503 bio
->bi_end_io
= bi_complete
;
2504 submit_bio(WRITE
, bio
);
2505 wait_for_completion(&event
);
2507 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
)) {
2513 ee_pblock
+= done
<< (blkbits
- 9);
2518 #define EXT4_EXT_ZERO_LEN 7
2520 * This function is called by ext4_ext_get_blocks() if someone tries to write
2521 * to an uninitialized extent. It may result in splitting the uninitialized
2522 * extent into multiple extents (upto three - one initialized and two
2524 * There are three possibilities:
2525 * a> There is no split required: Entire extent should be initialized
2526 * b> Splits in two extents: Write is happening at either end of the extent
2527 * c> Splits in three extents: Somone is writing in middle of the extent
2529 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2530 struct inode
*inode
,
2531 struct ext4_ext_path
*path
,
2533 unsigned int max_blocks
)
2535 struct ext4_extent
*ex
, newex
, orig_ex
;
2536 struct ext4_extent
*ex1
= NULL
;
2537 struct ext4_extent
*ex2
= NULL
;
2538 struct ext4_extent
*ex3
= NULL
;
2539 struct ext4_extent_header
*eh
;
2540 ext4_lblk_t ee_block
, eof_block
;
2541 unsigned int allocated
, ee_len
, depth
;
2542 ext4_fsblk_t newblock
;
2547 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2548 "block %llu, max_blocks %u\n", inode
->i_ino
,
2549 (unsigned long long)iblock
, max_blocks
);
2551 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2552 inode
->i_sb
->s_blocksize_bits
;
2553 if (eof_block
< iblock
+ max_blocks
)
2554 eof_block
= iblock
+ max_blocks
;
2556 depth
= ext_depth(inode
);
2557 eh
= path
[depth
].p_hdr
;
2558 ex
= path
[depth
].p_ext
;
2559 ee_block
= le32_to_cpu(ex
->ee_block
);
2560 ee_len
= ext4_ext_get_actual_len(ex
);
2561 allocated
= ee_len
- (iblock
- ee_block
);
2562 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2565 orig_ex
.ee_block
= ex
->ee_block
;
2566 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2567 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2570 * It is safe to convert extent to initialized via explicit
2571 * zeroout only if extent is fully insde i_size or new_size.
2573 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2575 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2578 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2579 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
&& may_zeroout
) {
2580 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2582 goto fix_extent_len
;
2583 /* update the extent length and mark as initialized */
2584 ex
->ee_block
= orig_ex
.ee_block
;
2585 ex
->ee_len
= orig_ex
.ee_len
;
2586 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2587 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2588 /* zeroed the full extent */
2592 /* ex1: ee_block to iblock - 1 : uninitialized */
2593 if (iblock
> ee_block
) {
2595 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2596 ext4_ext_mark_uninitialized(ex1
);
2597 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2601 * for sanity, update the length of the ex2 extent before
2602 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2603 * overlap of blocks.
2605 if (!ex1
&& allocated
> max_blocks
)
2606 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2607 /* ex3: to ee_block + ee_len : uninitialised */
2608 if (allocated
> max_blocks
) {
2609 unsigned int newdepth
;
2610 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2611 if (allocated
<= EXT4_EXT_ZERO_LEN
&& may_zeroout
) {
2613 * iblock == ee_block is handled by the zerouout
2615 * Mark first half uninitialized.
2616 * Mark second half initialized and zero out the
2617 * initialized extent
2619 ex
->ee_block
= orig_ex
.ee_block
;
2620 ex
->ee_len
= cpu_to_le16(ee_len
- allocated
);
2621 ext4_ext_mark_uninitialized(ex
);
2622 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2623 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2626 ex3
->ee_block
= cpu_to_le32(iblock
);
2627 ext4_ext_store_pblock(ex3
, newblock
);
2628 ex3
->ee_len
= cpu_to_le16(allocated
);
2629 err
= ext4_ext_insert_extent(handle
, inode
, path
,
2631 if (err
== -ENOSPC
) {
2632 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2634 goto fix_extent_len
;
2635 ex
->ee_block
= orig_ex
.ee_block
;
2636 ex
->ee_len
= orig_ex
.ee_len
;
2637 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2638 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2639 /* blocks available from iblock */
2643 goto fix_extent_len
;
2646 * We need to zero out the second half because
2647 * an fallocate request can update file size and
2648 * converting the second half to initialized extent
2649 * implies that we can leak some junk data to user
2652 err
= ext4_ext_zeroout(inode
, ex3
);
2655 * We should actually mark the
2656 * second half as uninit and return error
2657 * Insert would have changed the extent
2659 depth
= ext_depth(inode
);
2660 ext4_ext_drop_refs(path
);
2661 path
= ext4_ext_find_extent(inode
,
2664 err
= PTR_ERR(path
);
2667 /* get the second half extent details */
2668 ex
= path
[depth
].p_ext
;
2669 err
= ext4_ext_get_access(handle
, inode
,
2673 ext4_ext_mark_uninitialized(ex
);
2674 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2678 /* zeroed the second half */
2682 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2683 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2684 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2685 ext4_ext_mark_uninitialized(ex3
);
2686 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, 0);
2687 if (err
== -ENOSPC
&& may_zeroout
) {
2688 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2690 goto fix_extent_len
;
2691 /* update the extent length and mark as initialized */
2692 ex
->ee_block
= orig_ex
.ee_block
;
2693 ex
->ee_len
= orig_ex
.ee_len
;
2694 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2695 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2696 /* zeroed the full extent */
2697 /* blocks available from iblock */
2701 goto fix_extent_len
;
2703 * The depth, and hence eh & ex might change
2704 * as part of the insert above.
2706 newdepth
= ext_depth(inode
);
2708 * update the extent length after successful insert of the
2711 ee_len
-= ext4_ext_get_actual_len(ex3
);
2712 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2713 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2716 ext4_ext_drop_refs(path
);
2717 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2719 err
= PTR_ERR(path
);
2722 eh
= path
[depth
].p_hdr
;
2723 ex
= path
[depth
].p_ext
;
2727 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2731 allocated
= max_blocks
;
2733 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2734 * to insert a extent in the middle zerout directly
2735 * otherwise give the extent a chance to merge to left
2737 if (le16_to_cpu(orig_ex
.ee_len
) <= EXT4_EXT_ZERO_LEN
&&
2738 iblock
!= ee_block
&& may_zeroout
) {
2739 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2741 goto fix_extent_len
;
2742 /* update the extent length and mark as initialized */
2743 ex
->ee_block
= orig_ex
.ee_block
;
2744 ex
->ee_len
= orig_ex
.ee_len
;
2745 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2746 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2747 /* zero out the first half */
2748 /* blocks available from iblock */
2753 * If there was a change of depth as part of the
2754 * insertion of ex3 above, we need to update the length
2755 * of the ex1 extent again here
2757 if (ex1
&& ex1
!= ex
) {
2759 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2760 ext4_ext_mark_uninitialized(ex1
);
2763 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2764 ex2
->ee_block
= cpu_to_le32(iblock
);
2765 ext4_ext_store_pblock(ex2
, newblock
);
2766 ex2
->ee_len
= cpu_to_le16(allocated
);
2770 * New (initialized) extent starts from the first block
2771 * in the current extent. i.e., ex2 == ex
2772 * We have to see if it can be merged with the extent
2775 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2777 * To merge left, pass "ex2 - 1" to try_to_merge(),
2778 * since it merges towards right _only_.
2780 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2782 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2785 depth
= ext_depth(inode
);
2790 * Try to Merge towards right. This might be required
2791 * only when the whole extent is being written to.
2792 * i.e. ex2 == ex and ex3 == NULL.
2795 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2797 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2802 /* Mark modified extent as dirty */
2803 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2806 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, 0);
2807 if (err
== -ENOSPC
&& may_zeroout
) {
2808 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2810 goto fix_extent_len
;
2811 /* update the extent length and mark as initialized */
2812 ex
->ee_block
= orig_ex
.ee_block
;
2813 ex
->ee_len
= orig_ex
.ee_len
;
2814 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2815 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2816 /* zero out the first half */
2819 goto fix_extent_len
;
2821 ext4_ext_show_leaf(inode
, path
);
2822 return err
? err
: allocated
;
2825 ex
->ee_block
= orig_ex
.ee_block
;
2826 ex
->ee_len
= orig_ex
.ee_len
;
2827 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2828 ext4_ext_mark_uninitialized(ex
);
2829 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2834 * This function is called by ext4_ext_get_blocks() from
2835 * ext4_get_blocks_dio_write() when DIO to write
2836 * to an uninitialized extent.
2838 * Writing to an uninitized extent may result in splitting the uninitialized
2839 * extent into multiple /intialized unintialized extents (up to three)
2840 * There are three possibilities:
2841 * a> There is no split required: Entire extent should be uninitialized
2842 * b> Splits in two extents: Write is happening at either end of the extent
2843 * c> Splits in three extents: Somone is writing in middle of the extent
2845 * One of more index blocks maybe needed if the extent tree grow after
2846 * the unintialized extent split. To prevent ENOSPC occur at the IO
2847 * complete, we need to split the uninitialized extent before DIO submit
2848 * the IO. The uninitilized extent called at this time will be split
2849 * into three uninitialized extent(at most). After IO complete, the part
2850 * being filled will be convert to initialized by the end_io callback function
2851 * via ext4_convert_unwritten_extents().
2853 * Returns the size of uninitialized extent to be written on success.
2855 static int ext4_split_unwritten_extents(handle_t
*handle
,
2856 struct inode
*inode
,
2857 struct ext4_ext_path
*path
,
2859 unsigned int max_blocks
,
2862 struct ext4_extent
*ex
, newex
, orig_ex
;
2863 struct ext4_extent
*ex1
= NULL
;
2864 struct ext4_extent
*ex2
= NULL
;
2865 struct ext4_extent
*ex3
= NULL
;
2866 struct ext4_extent_header
*eh
;
2867 ext4_lblk_t ee_block
, eof_block
;
2868 unsigned int allocated
, ee_len
, depth
;
2869 ext4_fsblk_t newblock
;
2873 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2874 "block %llu, max_blocks %u\n", inode
->i_ino
,
2875 (unsigned long long)iblock
, max_blocks
);
2877 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2878 inode
->i_sb
->s_blocksize_bits
;
2879 if (eof_block
< iblock
+ max_blocks
)
2880 eof_block
= iblock
+ max_blocks
;
2882 depth
= ext_depth(inode
);
2883 eh
= path
[depth
].p_hdr
;
2884 ex
= path
[depth
].p_ext
;
2885 ee_block
= le32_to_cpu(ex
->ee_block
);
2886 ee_len
= ext4_ext_get_actual_len(ex
);
2887 allocated
= ee_len
- (iblock
- ee_block
);
2888 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2891 orig_ex
.ee_block
= ex
->ee_block
;
2892 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2893 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2896 * It is safe to convert extent to initialized via explicit
2897 * zeroout only if extent is fully insde i_size or new_size.
2899 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2902 * If the uninitialized extent begins at the same logical
2903 * block where the write begins, and the write completely
2904 * covers the extent, then we don't need to split it.
2906 if ((iblock
== ee_block
) && (allocated
<= max_blocks
))
2909 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2912 /* ex1: ee_block to iblock - 1 : uninitialized */
2913 if (iblock
> ee_block
) {
2915 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2916 ext4_ext_mark_uninitialized(ex1
);
2920 * for sanity, update the length of the ex2 extent before
2921 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2922 * overlap of blocks.
2924 if (!ex1
&& allocated
> max_blocks
)
2925 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2926 /* ex3: to ee_block + ee_len : uninitialised */
2927 if (allocated
> max_blocks
) {
2928 unsigned int newdepth
;
2930 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2931 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2932 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2933 ext4_ext_mark_uninitialized(ex3
);
2934 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, flags
);
2935 if (err
== -ENOSPC
&& may_zeroout
) {
2936 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2938 goto fix_extent_len
;
2939 /* update the extent length and mark as initialized */
2940 ex
->ee_block
= orig_ex
.ee_block
;
2941 ex
->ee_len
= orig_ex
.ee_len
;
2942 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2943 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2944 /* zeroed the full extent */
2945 /* blocks available from iblock */
2949 goto fix_extent_len
;
2951 * The depth, and hence eh & ex might change
2952 * as part of the insert above.
2954 newdepth
= ext_depth(inode
);
2956 * update the extent length after successful insert of the
2959 ee_len
-= ext4_ext_get_actual_len(ex3
);
2960 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2961 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2964 ext4_ext_drop_refs(path
);
2965 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2967 err
= PTR_ERR(path
);
2970 eh
= path
[depth
].p_hdr
;
2971 ex
= path
[depth
].p_ext
;
2975 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2979 allocated
= max_blocks
;
2982 * If there was a change of depth as part of the
2983 * insertion of ex3 above, we need to update the length
2984 * of the ex1 extent again here
2986 if (ex1
&& ex1
!= ex
) {
2988 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2989 ext4_ext_mark_uninitialized(ex1
);
2993 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2994 * uninitialised still.
2996 ex2
->ee_block
= cpu_to_le32(iblock
);
2997 ext4_ext_store_pblock(ex2
, newblock
);
2998 ex2
->ee_len
= cpu_to_le16(allocated
);
2999 ext4_ext_mark_uninitialized(ex2
);
3002 /* Mark modified extent as dirty */
3003 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3004 ext_debug("out here\n");
3007 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3008 if (err
== -ENOSPC
&& may_zeroout
) {
3009 err
= ext4_ext_zeroout(inode
, &orig_ex
);
3011 goto fix_extent_len
;
3012 /* update the extent length and mark as initialized */
3013 ex
->ee_block
= orig_ex
.ee_block
;
3014 ex
->ee_len
= orig_ex
.ee_len
;
3015 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
3016 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3017 /* zero out the first half */
3020 goto fix_extent_len
;
3022 ext4_ext_show_leaf(inode
, path
);
3023 return err
? err
: allocated
;
3026 ex
->ee_block
= orig_ex
.ee_block
;
3027 ex
->ee_len
= orig_ex
.ee_len
;
3028 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
3029 ext4_ext_mark_uninitialized(ex
);
3030 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3033 static int ext4_convert_unwritten_extents_dio(handle_t
*handle
,
3034 struct inode
*inode
,
3035 struct ext4_ext_path
*path
)
3037 struct ext4_extent
*ex
;
3038 struct ext4_extent_header
*eh
;
3043 depth
= ext_depth(inode
);
3044 eh
= path
[depth
].p_hdr
;
3045 ex
= path
[depth
].p_ext
;
3047 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3050 /* first mark the extent as initialized */
3051 ext4_ext_mark_initialized(ex
);
3054 * We have to see if it can be merged with the extent
3057 if (ex
> EXT_FIRST_EXTENT(eh
)) {
3059 * To merge left, pass "ex - 1" to try_to_merge(),
3060 * since it merges towards right _only_.
3062 ret
= ext4_ext_try_to_merge(inode
, path
, ex
- 1);
3064 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
3067 depth
= ext_depth(inode
);
3072 * Try to Merge towards right.
3074 ret
= ext4_ext_try_to_merge(inode
, path
, ex
);
3076 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
3079 depth
= ext_depth(inode
);
3081 /* Mark modified extent as dirty */
3082 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3084 ext4_ext_show_leaf(inode
, path
);
3088 static void unmap_underlying_metadata_blocks(struct block_device
*bdev
,
3089 sector_t block
, int count
)
3092 for (i
= 0; i
< count
; i
++)
3093 unmap_underlying_metadata(bdev
, block
+ i
);
3097 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3098 ext4_lblk_t iblock
, unsigned int max_blocks
,
3099 struct ext4_ext_path
*path
, int flags
,
3100 unsigned int allocated
, struct buffer_head
*bh_result
,
3101 ext4_fsblk_t newblock
)
3105 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3107 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3108 "block %llu, max_blocks %u, flags %d, allocated %u",
3109 inode
->i_ino
, (unsigned long long)iblock
, max_blocks
,
3111 ext4_ext_show_leaf(inode
, path
);
3113 /* DIO get_block() before submit the IO, split the extent */
3114 if (flags
== EXT4_GET_BLOCKS_DIO_CREATE_EXT
) {
3115 ret
= ext4_split_unwritten_extents(handle
,
3116 inode
, path
, iblock
,
3119 * Flag the inode(non aio case) or end_io struct (aio case)
3120 * that this IO needs to convertion to written when IO is
3124 io
->flag
= DIO_AIO_UNWRITTEN
;
3126 ext4_set_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3129 /* async DIO end_io complete, convert the filled extent to written */
3130 if (flags
== EXT4_GET_BLOCKS_DIO_CONVERT_EXT
) {
3131 ret
= ext4_convert_unwritten_extents_dio(handle
, inode
,
3134 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3137 /* buffered IO case */
3139 * repeat fallocate creation request
3140 * we already have an unwritten extent
3142 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3145 /* buffered READ or buffered write_begin() lookup */
3146 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3148 * We have blocks reserved already. We
3149 * return allocated blocks so that delalloc
3150 * won't do block reservation for us. But
3151 * the buffer head will be unmapped so that
3152 * a read from the block returns 0s.
3154 set_buffer_unwritten(bh_result
);
3158 /* buffered write, writepage time, convert*/
3159 ret
= ext4_ext_convert_to_initialized(handle
, inode
,
3163 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3170 set_buffer_new(bh_result
);
3172 * if we allocated more blocks than requested
3173 * we need to make sure we unmap the extra block
3174 * allocated. The actual needed block will get
3175 * unmapped later when we find the buffer_head marked
3178 if (allocated
> max_blocks
) {
3179 unmap_underlying_metadata_blocks(inode
->i_sb
->s_bdev
,
3180 newblock
+ max_blocks
,
3181 allocated
- max_blocks
);
3182 allocated
= max_blocks
;
3186 * If we have done fallocate with the offset that is already
3187 * delayed allocated, we would have block reservation
3188 * and quota reservation done in the delayed write path.
3189 * But fallocate would have already updated quota and block
3190 * count for this offset. So cancel these reservation
3192 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3193 ext4_da_update_reserve_space(inode
, allocated
, 0);
3196 set_buffer_mapped(bh_result
);
3198 if (allocated
> max_blocks
)
3199 allocated
= max_blocks
;
3200 ext4_ext_show_leaf(inode
, path
);
3201 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
3202 bh_result
->b_blocknr
= newblock
;
3205 ext4_ext_drop_refs(path
);
3208 return err
? err
: allocated
;
3211 * Block allocation/map/preallocation routine for extents based files
3214 * Need to be called with
3215 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3216 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3218 * return > 0, number of of blocks already mapped/allocated
3219 * if create == 0 and these are pre-allocated blocks
3220 * buffer head is unmapped
3221 * otherwise blocks are mapped
3223 * return = 0, if plain look up failed (blocks have not been allocated)
3224 * buffer head is unmapped
3226 * return < 0, error case.
3228 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
3230 unsigned int max_blocks
, struct buffer_head
*bh_result
,
3233 struct ext4_ext_path
*path
= NULL
;
3234 struct ext4_extent_header
*eh
;
3235 struct ext4_extent newex
, *ex
, *last_ex
;
3236 ext4_fsblk_t newblock
;
3237 int i
, err
= 0, depth
, ret
, cache_type
;
3238 unsigned int allocated
= 0;
3239 struct ext4_allocation_request ar
;
3240 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3242 __clear_bit(BH_New
, &bh_result
->b_state
);
3243 ext_debug("blocks %u/%u requested for inode %lu\n",
3244 iblock
, max_blocks
, inode
->i_ino
);
3246 /* check in cache */
3247 cache_type
= ext4_ext_in_cache(inode
, iblock
, &newex
);
3249 if (cache_type
== EXT4_EXT_CACHE_GAP
) {
3250 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3252 * block isn't allocated yet and
3253 * user doesn't want to allocate it
3257 /* we should allocate requested block */
3258 } else if (cache_type
== EXT4_EXT_CACHE_EXTENT
) {
3259 /* block is already allocated */
3261 - le32_to_cpu(newex
.ee_block
)
3262 + ext_pblock(&newex
);
3263 /* number of remaining blocks in the extent */
3264 allocated
= ext4_ext_get_actual_len(&newex
) -
3265 (iblock
- le32_to_cpu(newex
.ee_block
));
3272 /* find extent for this block */
3273 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
3275 err
= PTR_ERR(path
);
3280 depth
= ext_depth(inode
);
3283 * consistent leaf must not be empty;
3284 * this situation is possible, though, _during_ tree modification;
3285 * this is why assert can't be put in ext4_ext_find_extent()
3287 if (path
[depth
].p_ext
== NULL
&& depth
!= 0) {
3288 ext4_error(inode
->i_sb
, __func__
, "bad extent address "
3289 "inode: %lu, iblock: %lu, depth: %d",
3290 inode
->i_ino
, (unsigned long) iblock
, depth
);
3294 eh
= path
[depth
].p_hdr
;
3296 ex
= path
[depth
].p_ext
;
3298 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3299 ext4_fsblk_t ee_start
= ext_pblock(ex
);
3300 unsigned short ee_len
;
3303 * Uninitialized extents are treated as holes, except that
3304 * we split out initialized portions during a write.
3306 ee_len
= ext4_ext_get_actual_len(ex
);
3307 /* if found extent covers block, simply return it */
3308 if (in_range(iblock
, ee_block
, ee_len
)) {
3309 newblock
= iblock
- ee_block
+ ee_start
;
3310 /* number of remaining blocks in the extent */
3311 allocated
= ee_len
- (iblock
- ee_block
);
3312 ext_debug("%u fit into %u:%d -> %llu\n", iblock
,
3313 ee_block
, ee_len
, newblock
);
3315 /* Do not put uninitialized extent in the cache */
3316 if (!ext4_ext_is_uninitialized(ex
)) {
3317 ext4_ext_put_in_cache(inode
, ee_block
,
3319 EXT4_EXT_CACHE_EXTENT
);
3322 ret
= ext4_ext_handle_uninitialized_extents(handle
,
3323 inode
, iblock
, max_blocks
, path
,
3324 flags
, allocated
, bh_result
, newblock
);
3330 * requested block isn't allocated yet;
3331 * we couldn't try to create block if create flag is zero
3333 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3335 * put just found gap into cache to speed up
3336 * subsequent requests
3338 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
3342 * Okay, we need to do block allocation.
3345 /* find neighbour allocated blocks */
3347 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
3351 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
3356 * See if request is beyond maximum number of blocks we can have in
3357 * a single extent. For an initialized extent this limit is
3358 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3359 * EXT_UNINIT_MAX_LEN.
3361 if (max_blocks
> EXT_INIT_MAX_LEN
&&
3362 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3363 max_blocks
= EXT_INIT_MAX_LEN
;
3364 else if (max_blocks
> EXT_UNINIT_MAX_LEN
&&
3365 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3366 max_blocks
= EXT_UNINIT_MAX_LEN
;
3368 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3369 newex
.ee_block
= cpu_to_le32(iblock
);
3370 newex
.ee_len
= cpu_to_le16(max_blocks
);
3371 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
3373 allocated
= ext4_ext_get_actual_len(&newex
);
3375 allocated
= max_blocks
;
3377 /* allocate new block */
3379 ar
.goal
= ext4_ext_find_goal(inode
, path
, iblock
);
3380 ar
.logical
= iblock
;
3382 if (S_ISREG(inode
->i_mode
))
3383 ar
.flags
= EXT4_MB_HINT_DATA
;
3385 /* disable in-core preallocation for non-regular files */
3387 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
3390 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3391 ar
.goal
, newblock
, allocated
);
3393 /* try to insert new extent into found leaf and return */
3394 ext4_ext_store_pblock(&newex
, newblock
);
3395 newex
.ee_len
= cpu_to_le16(ar
.len
);
3396 /* Mark uninitialized */
3397 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
3398 ext4_ext_mark_uninitialized(&newex
);
3400 * io_end structure was created for every async
3401 * direct IO write to the middle of the file.
3402 * To avoid unecessary convertion for every aio dio rewrite
3403 * to the mid of file, here we flag the IO that is really
3404 * need the convertion.
3405 * For non asycn direct IO case, flag the inode state
3406 * that we need to perform convertion when IO is done.
3408 if (flags
== EXT4_GET_BLOCKS_DIO_CREATE_EXT
) {
3410 io
->flag
= DIO_AIO_UNWRITTEN
;
3412 ext4_set_inode_state(inode
,
3413 EXT4_STATE_DIO_UNWRITTEN
);
3417 if (unlikely(ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))) {
3418 if (unlikely(!eh
->eh_entries
)) {
3419 ext4_error(inode
->i_sb
, __func__
,
3420 "inode#%lu, eh->eh_entries = 0 and "
3421 "EOFBLOCKS_FL set", inode
->i_ino
);
3425 last_ex
= EXT_LAST_EXTENT(eh
);
3427 * If the current leaf block was reached by looking at
3428 * the last index block all the way down the tree, and
3429 * we are extending the inode beyond the last extent
3430 * in the current leaf block, then clear the
3431 * EOFBLOCKS_FL flag.
3433 for (i
= depth
-1; i
>= 0; i
--) {
3434 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3438 (iblock
+ ar
.len
> le32_to_cpu(last_ex
->ee_block
) +
3439 ext4_ext_get_actual_len(last_ex
)))
3440 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3442 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3444 /* free data blocks we just allocated */
3445 /* not a good idea to call discard here directly,
3446 * but otherwise we'd need to call it every free() */
3447 ext4_discard_preallocations(inode
);
3448 ext4_free_blocks(handle
, inode
, ext_pblock(&newex
),
3449 ext4_ext_get_actual_len(&newex
), 0);
3453 /* previous routine could use block we allocated */
3454 newblock
= ext_pblock(&newex
);
3455 allocated
= ext4_ext_get_actual_len(&newex
);
3456 if (allocated
> max_blocks
)
3457 allocated
= max_blocks
;
3458 set_buffer_new(bh_result
);
3461 * Update reserved blocks/metadata blocks after successful
3462 * block allocation which had been deferred till now.
3464 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3465 ext4_da_update_reserve_space(inode
, allocated
, 1);
3468 * Cache the extent and update transaction to commit on fdatasync only
3469 * when it is _not_ an uninitialized extent.
3471 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
3472 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
3473 EXT4_EXT_CACHE_EXTENT
);
3474 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3476 ext4_update_inode_fsync_trans(handle
, inode
, 0);
3478 if (allocated
> max_blocks
)
3479 allocated
= max_blocks
;
3480 ext4_ext_show_leaf(inode
, path
);
3481 set_buffer_mapped(bh_result
);
3482 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
3483 bh_result
->b_blocknr
= newblock
;
3486 ext4_ext_drop_refs(path
);
3489 return err
? err
: allocated
;
3492 void ext4_ext_truncate(struct inode
*inode
)
3494 struct address_space
*mapping
= inode
->i_mapping
;
3495 struct super_block
*sb
= inode
->i_sb
;
3496 ext4_lblk_t last_block
;
3501 * probably first extent we're gonna free will be last in block
3503 err
= ext4_writepage_trans_blocks(inode
);
3504 handle
= ext4_journal_start(inode
, err
);
3508 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3509 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3511 if (ext4_orphan_add(handle
, inode
))
3514 down_write(&EXT4_I(inode
)->i_data_sem
);
3515 ext4_ext_invalidate_cache(inode
);
3517 ext4_discard_preallocations(inode
);
3520 * TODO: optimization is possible here.
3521 * Probably we need not scan at all,
3522 * because page truncation is enough.
3525 /* we have to know where to truncate from in crash case */
3526 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3527 ext4_mark_inode_dirty(handle
, inode
);
3529 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3530 >> EXT4_BLOCK_SIZE_BITS(sb
);
3531 err
= ext4_ext_remove_space(inode
, last_block
);
3533 /* In a multi-transaction truncate, we only make the final
3534 * transaction synchronous.
3537 ext4_handle_sync(handle
);
3540 up_write(&EXT4_I(inode
)->i_data_sem
);
3542 * If this was a simple ftruncate() and the file will remain alive,
3543 * then we need to clear up the orphan record which we created above.
3544 * However, if this was a real unlink then we were called by
3545 * ext4_delete_inode(), and we allow that function to clean up the
3546 * orphan info for us.
3549 ext4_orphan_del(handle
, inode
);
3551 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3552 ext4_mark_inode_dirty(handle
, inode
);
3553 ext4_journal_stop(handle
);
3556 static void ext4_falloc_update_inode(struct inode
*inode
,
3557 int mode
, loff_t new_size
, int update_ctime
)
3559 struct timespec now
;
3562 now
= current_fs_time(inode
->i_sb
);
3563 if (!timespec_equal(&inode
->i_ctime
, &now
))
3564 inode
->i_ctime
= now
;
3567 * Update only when preallocation was requested beyond
3570 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3571 if (new_size
> i_size_read(inode
))
3572 i_size_write(inode
, new_size
);
3573 if (new_size
> EXT4_I(inode
)->i_disksize
)
3574 ext4_update_i_disksize(inode
, new_size
);
3577 * Mark that we allocate beyond EOF so the subsequent truncate
3578 * can proceed even if the new size is the same as i_size.
3580 if (new_size
> i_size_read(inode
))
3581 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3587 * preallocate space for a file. This implements ext4's fallocate inode
3588 * operation, which gets called from sys_fallocate system call.
3589 * For block-mapped files, posix_fallocate should fall back to the method
3590 * of writing zeroes to the required new blocks (the same behavior which is
3591 * expected for file systems which do not support fallocate() system call).
3593 long ext4_fallocate(struct inode
*inode
, int mode
, loff_t offset
, loff_t len
)
3598 unsigned int max_blocks
;
3602 struct buffer_head map_bh
;
3603 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3606 * currently supporting (pre)allocate mode for extent-based
3609 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
3612 /* preallocation to directories is currently not supported */
3613 if (S_ISDIR(inode
->i_mode
))
3616 block
= offset
>> blkbits
;
3618 * We can't just convert len to max_blocks because
3619 * If blocksize = 4096 offset = 3072 and len = 2048
3621 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3624 * credits to insert 1 extent into extent tree
3626 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3627 mutex_lock(&inode
->i_mutex
);
3628 ret
= inode_newsize_ok(inode
, (len
+ offset
));
3630 mutex_unlock(&inode
->i_mutex
);
3634 while (ret
>= 0 && ret
< max_blocks
) {
3635 block
= block
+ ret
;
3636 max_blocks
= max_blocks
- ret
;
3637 handle
= ext4_journal_start(inode
, credits
);
3638 if (IS_ERR(handle
)) {
3639 ret
= PTR_ERR(handle
);
3643 ret
= ext4_get_blocks(handle
, inode
, block
,
3644 max_blocks
, &map_bh
,
3645 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
);
3649 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3650 "returned error inode#%lu, block=%u, "
3651 "max_blocks=%u", __func__
,
3652 inode
->i_ino
, block
, max_blocks
);
3654 ext4_mark_inode_dirty(handle
, inode
);
3655 ret2
= ext4_journal_stop(handle
);
3658 if ((block
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3659 blkbits
) >> blkbits
))
3660 new_size
= offset
+ len
;
3662 new_size
= (block
+ ret
) << blkbits
;
3664 ext4_falloc_update_inode(inode
, mode
, new_size
,
3665 buffer_new(&map_bh
));
3666 ext4_mark_inode_dirty(handle
, inode
);
3667 ret2
= ext4_journal_stop(handle
);
3671 if (ret
== -ENOSPC
&&
3672 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3676 mutex_unlock(&inode
->i_mutex
);
3677 return ret
> 0 ? ret2
: ret
;
3681 * This function convert a range of blocks to written extents
3682 * The caller of this function will pass the start offset and the size.
3683 * all unwritten extents within this range will be converted to
3686 * This function is called from the direct IO end io call back
3687 * function, to convert the fallocated extents after IO is completed.
3688 * Returns 0 on success.
3690 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
3695 unsigned int max_blocks
;
3698 struct buffer_head map_bh
;
3699 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3701 block
= offset
>> blkbits
;
3703 * We can't just convert len to max_blocks because
3704 * If blocksize = 4096 offset = 3072 and len = 2048
3706 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3709 * credits to insert 1 extent into extent tree
3711 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3712 while (ret
>= 0 && ret
< max_blocks
) {
3713 block
= block
+ ret
;
3714 max_blocks
= max_blocks
- ret
;
3715 handle
= ext4_journal_start(inode
, credits
);
3716 if (IS_ERR(handle
)) {
3717 ret
= PTR_ERR(handle
);
3721 ret
= ext4_get_blocks(handle
, inode
, block
,
3722 max_blocks
, &map_bh
,
3723 EXT4_GET_BLOCKS_DIO_CONVERT_EXT
);
3726 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3727 "returned error inode#%lu, block=%u, "
3728 "max_blocks=%u", __func__
,
3729 inode
->i_ino
, block
, max_blocks
);
3731 ext4_mark_inode_dirty(handle
, inode
);
3732 ret2
= ext4_journal_stop(handle
);
3733 if (ret
<= 0 || ret2
)
3736 return ret
> 0 ? ret2
: ret
;
3739 * Callback function called for each extent to gather FIEMAP information.
3741 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3742 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3745 struct fiemap_extent_info
*fieinfo
= data
;
3746 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3753 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3755 if (newex
->ec_type
== EXT4_EXT_CACHE_GAP
) {
3758 struct buffer_head
*bh
= NULL
;
3760 offset
= logical
>> PAGE_SHIFT
;
3761 page
= find_get_page(inode
->i_mapping
, offset
);
3762 if (!page
|| !page_has_buffers(page
))
3763 return EXT_CONTINUE
;
3765 bh
= page_buffers(page
);
3768 return EXT_CONTINUE
;
3770 if (buffer_delay(bh
)) {
3771 flags
|= FIEMAP_EXTENT_DELALLOC
;
3772 page_cache_release(page
);
3774 page_cache_release(page
);
3775 return EXT_CONTINUE
;
3779 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3780 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3782 if (ex
&& ext4_ext_is_uninitialized(ex
))
3783 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3786 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3788 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3789 * this also indicates no more allocated blocks.
3791 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3793 if (ext4_ext_next_allocated_block(path
) == EXT_MAX_BLOCK
||
3794 newex
->ec_block
+ newex
->ec_len
- 1 == EXT_MAX_BLOCK
) {
3795 loff_t size
= i_size_read(inode
);
3796 loff_t bs
= EXT4_BLOCK_SIZE(inode
->i_sb
);
3798 flags
|= FIEMAP_EXTENT_LAST
;
3799 if ((flags
& FIEMAP_EXTENT_DELALLOC
) &&
3800 logical
+length
> size
)
3801 length
= (size
- logical
+ bs
- 1) & ~(bs
-1);
3804 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3811 return EXT_CONTINUE
;
3814 /* fiemap flags we can handle specified here */
3815 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3817 static int ext4_xattr_fiemap(struct inode
*inode
,
3818 struct fiemap_extent_info
*fieinfo
)
3822 __u32 flags
= FIEMAP_EXTENT_LAST
;
3823 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3827 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
3828 struct ext4_iloc iloc
;
3829 int offset
; /* offset of xattr in inode */
3831 error
= ext4_get_inode_loc(inode
, &iloc
);
3834 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3835 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3836 EXT4_I(inode
)->i_extra_isize
;
3838 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3839 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3841 } else { /* external block */
3842 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3843 length
= inode
->i_sb
->s_blocksize
;
3847 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3849 return (error
< 0 ? error
: 0);
3852 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3853 __u64 start
, __u64 len
)
3855 ext4_lblk_t start_blk
;
3858 /* fallback to generic here if not in extents fmt */
3859 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
3860 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3863 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3866 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3867 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3869 ext4_lblk_t len_blks
;
3872 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3873 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
3874 if (last_blk
>= EXT_MAX_BLOCK
)
3875 last_blk
= EXT_MAX_BLOCK
-1;
3876 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
3879 * Walk the extent tree gathering extent information.
3880 * ext4_ext_fiemap_cb will push extents back to user.
3882 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3883 ext4_ext_fiemap_cb
, fieinfo
);