2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include <linux/fiemap.h>
44 #include "ext4_jbd2.h"
45 #include "ext4_extents.h"
47 static int ext4_ext_truncate_extend_restart(handle_t
*handle
,
53 if (!ext4_handle_valid(handle
))
55 if (handle
->h_buffer_credits
> needed
)
57 err
= ext4_journal_extend(handle
, needed
);
60 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
72 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
73 struct ext4_ext_path
*path
)
76 /* path points to block */
77 return ext4_journal_get_write_access(handle
, path
->p_bh
);
79 /* path points to leaf/index in inode body */
80 /* we use in-core data, no need to protect them */
90 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
91 struct ext4_ext_path
*path
)
95 /* path points to block */
96 err
= ext4_handle_dirty_metadata(handle
, inode
, path
->p_bh
);
98 /* path points to leaf/index in inode body */
99 err
= ext4_mark_inode_dirty(handle
, inode
);
104 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
105 struct ext4_ext_path
*path
,
108 struct ext4_inode_info
*ei
= EXT4_I(inode
);
109 ext4_fsblk_t bg_start
;
110 ext4_fsblk_t last_block
;
111 ext4_grpblk_t colour
;
112 ext4_group_t block_group
;
113 int flex_size
= ext4_flex_bg_size(EXT4_SB(inode
->i_sb
));
117 struct ext4_extent
*ex
;
118 depth
= path
->p_depth
;
121 * Try to predict block placement assuming that we are
122 * filling in a file which will eventually be
123 * non-sparse --- i.e., in the case of libbfd writing
124 * an ELF object sections out-of-order but in a way
125 * the eventually results in a contiguous object or
126 * executable file, or some database extending a table
127 * space file. However, this is actually somewhat
128 * non-ideal if we are writing a sparse file such as
129 * qemu or KVM writing a raw image file that is going
130 * to stay fairly sparse, since it will end up
131 * fragmenting the file system's free space. Maybe we
132 * should have some hueristics or some way to allow
133 * userspace to pass a hint to file system,
134 * especiially if the latter case turns out to be
137 ex
= path
[depth
].p_ext
;
139 ext4_fsblk_t ext_pblk
= ext4_ext_pblock(ex
);
140 ext4_lblk_t ext_block
= le32_to_cpu(ex
->ee_block
);
142 if (block
> ext_block
)
143 return ext_pblk
+ (block
- ext_block
);
145 return ext_pblk
- (ext_block
- block
);
148 /* it looks like index is empty;
149 * try to find starting block from index itself */
150 if (path
[depth
].p_bh
)
151 return path
[depth
].p_bh
->b_blocknr
;
154 /* OK. use inode's group */
155 block_group
= ei
->i_block_group
;
156 if (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) {
158 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
159 * block groups per flexgroup, reserve the first block
160 * group for directories and special files. Regular
161 * files will start at the second block group. This
162 * tends to speed up directory access and improves
165 block_group
&= ~(flex_size
-1);
166 if (S_ISREG(inode
->i_mode
))
169 bg_start
= ext4_group_first_block_no(inode
->i_sb
, block_group
);
170 last_block
= ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
) - 1;
173 * If we are doing delayed allocation, we don't need take
174 * colour into account.
176 if (test_opt(inode
->i_sb
, DELALLOC
))
179 if (bg_start
+ EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) <= last_block
)
180 colour
= (current
->pid
% 16) *
181 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
183 colour
= (current
->pid
% 16) * ((last_block
- bg_start
) / 16);
184 return bg_start
+ colour
+ block
;
188 * Allocation for a meta data block
191 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
192 struct ext4_ext_path
*path
,
193 struct ext4_extent
*ex
, int *err
)
195 ext4_fsblk_t goal
, newblock
;
197 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
198 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, NULL
, err
);
202 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
206 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
207 / sizeof(struct ext4_extent
);
209 #ifdef AGGRESSIVE_TEST
217 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
221 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
222 / sizeof(struct ext4_extent_idx
);
224 #ifdef AGGRESSIVE_TEST
232 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
236 size
= sizeof(EXT4_I(inode
)->i_data
);
237 size
-= sizeof(struct ext4_extent_header
);
238 size
/= sizeof(struct ext4_extent
);
240 #ifdef AGGRESSIVE_TEST
248 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
252 size
= sizeof(EXT4_I(inode
)->i_data
);
253 size
-= sizeof(struct ext4_extent_header
);
254 size
/= sizeof(struct ext4_extent_idx
);
256 #ifdef AGGRESSIVE_TEST
265 * Calculate the number of metadata blocks needed
266 * to allocate @blocks
267 * Worse case is one block per extent
269 int ext4_ext_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
271 struct ext4_inode_info
*ei
= EXT4_I(inode
);
274 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
275 / sizeof(struct ext4_extent_idx
));
278 * If the new delayed allocation block is contiguous with the
279 * previous da block, it can share index blocks with the
280 * previous block, so we only need to allocate a new index
281 * block every idxs leaf blocks. At ldxs**2 blocks, we need
282 * an additional index block, and at ldxs**3 blocks, yet
283 * another index blocks.
285 if (ei
->i_da_metadata_calc_len
&&
286 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
287 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
289 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
291 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
293 ei
->i_da_metadata_calc_len
= 0;
295 ei
->i_da_metadata_calc_len
++;
296 ei
->i_da_metadata_calc_last_lblock
++;
301 * In the worst case we need a new set of index blocks at
302 * every level of the inode's extent tree.
304 ei
->i_da_metadata_calc_len
= 1;
305 ei
->i_da_metadata_calc_last_lblock
= lblock
;
306 return ext_depth(inode
) + 1;
310 ext4_ext_max_entries(struct inode
*inode
, int depth
)
314 if (depth
== ext_depth(inode
)) {
316 max
= ext4_ext_space_root(inode
, 1);
318 max
= ext4_ext_space_root_idx(inode
, 1);
321 max
= ext4_ext_space_block(inode
, 1);
323 max
= ext4_ext_space_block_idx(inode
, 1);
329 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
331 ext4_fsblk_t block
= ext4_ext_pblock(ext
);
332 int len
= ext4_ext_get_actual_len(ext
);
334 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
337 static int ext4_valid_extent_idx(struct inode
*inode
,
338 struct ext4_extent_idx
*ext_idx
)
340 ext4_fsblk_t block
= ext4_idx_pblock(ext_idx
);
342 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
345 static int ext4_valid_extent_entries(struct inode
*inode
,
346 struct ext4_extent_header
*eh
,
349 struct ext4_extent
*ext
;
350 struct ext4_extent_idx
*ext_idx
;
351 unsigned short entries
;
352 if (eh
->eh_entries
== 0)
355 entries
= le16_to_cpu(eh
->eh_entries
);
359 ext
= EXT_FIRST_EXTENT(eh
);
361 if (!ext4_valid_extent(inode
, ext
))
367 ext_idx
= EXT_FIRST_INDEX(eh
);
369 if (!ext4_valid_extent_idx(inode
, ext_idx
))
378 static int __ext4_ext_check(const char *function
, unsigned int line
,
379 struct inode
*inode
, struct ext4_extent_header
*eh
,
382 const char *error_msg
;
385 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
386 error_msg
= "invalid magic";
389 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
390 error_msg
= "unexpected eh_depth";
393 if (unlikely(eh
->eh_max
== 0)) {
394 error_msg
= "invalid eh_max";
397 max
= ext4_ext_max_entries(inode
, depth
);
398 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
399 error_msg
= "too large eh_max";
402 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
403 error_msg
= "invalid eh_entries";
406 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
407 error_msg
= "invalid extent entries";
413 ext4_error_inode(inode
, function
, line
, 0,
414 "bad header/extent: %s - magic %x, "
415 "entries %u, max %u(%u), depth %u(%u)",
416 error_msg
, le16_to_cpu(eh
->eh_magic
),
417 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
418 max
, le16_to_cpu(eh
->eh_depth
), depth
);
423 #define ext4_ext_check(inode, eh, depth) \
424 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
426 int ext4_ext_check_inode(struct inode
*inode
)
428 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
432 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
434 int k
, l
= path
->p_depth
;
437 for (k
= 0; k
<= l
; k
++, path
++) {
439 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
440 ext4_idx_pblock(path
->p_idx
));
441 } else if (path
->p_ext
) {
442 ext_debug(" %d:[%d]%d:%llu ",
443 le32_to_cpu(path
->p_ext
->ee_block
),
444 ext4_ext_is_uninitialized(path
->p_ext
),
445 ext4_ext_get_actual_len(path
->p_ext
),
446 ext4_ext_pblock(path
->p_ext
));
453 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
455 int depth
= ext_depth(inode
);
456 struct ext4_extent_header
*eh
;
457 struct ext4_extent
*ex
;
463 eh
= path
[depth
].p_hdr
;
464 ex
= EXT_FIRST_EXTENT(eh
);
466 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
468 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
469 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
470 ext4_ext_is_uninitialized(ex
),
471 ext4_ext_get_actual_len(ex
), ext4_ext_pblock(ex
));
476 #define ext4_ext_show_path(inode, path)
477 #define ext4_ext_show_leaf(inode, path)
480 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
482 int depth
= path
->p_depth
;
485 for (i
= 0; i
<= depth
; i
++, path
++)
493 * ext4_ext_binsearch_idx:
494 * binary search for the closest index of the given block
495 * the header must be checked before calling this
498 ext4_ext_binsearch_idx(struct inode
*inode
,
499 struct ext4_ext_path
*path
, ext4_lblk_t block
)
501 struct ext4_extent_header
*eh
= path
->p_hdr
;
502 struct ext4_extent_idx
*r
, *l
, *m
;
505 ext_debug("binsearch for %u(idx): ", block
);
507 l
= EXT_FIRST_INDEX(eh
) + 1;
508 r
= EXT_LAST_INDEX(eh
);
511 if (block
< le32_to_cpu(m
->ei_block
))
515 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
516 m
, le32_to_cpu(m
->ei_block
),
517 r
, le32_to_cpu(r
->ei_block
));
521 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
522 ext4_idx_pblock(path
->p_idx
));
524 #ifdef CHECK_BINSEARCH
526 struct ext4_extent_idx
*chix
, *ix
;
529 chix
= ix
= EXT_FIRST_INDEX(eh
);
530 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
532 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
533 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
535 ix
, EXT_FIRST_INDEX(eh
));
536 printk(KERN_DEBUG
"%u <= %u\n",
537 le32_to_cpu(ix
->ei_block
),
538 le32_to_cpu(ix
[-1].ei_block
));
540 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
541 <= le32_to_cpu(ix
[-1].ei_block
));
542 if (block
< le32_to_cpu(ix
->ei_block
))
546 BUG_ON(chix
!= path
->p_idx
);
553 * ext4_ext_binsearch:
554 * binary search for closest extent of the given block
555 * the header must be checked before calling this
558 ext4_ext_binsearch(struct inode
*inode
,
559 struct ext4_ext_path
*path
, ext4_lblk_t block
)
561 struct ext4_extent_header
*eh
= path
->p_hdr
;
562 struct ext4_extent
*r
, *l
, *m
;
564 if (eh
->eh_entries
== 0) {
566 * this leaf is empty:
567 * we get such a leaf in split/add case
572 ext_debug("binsearch for %u: ", block
);
574 l
= EXT_FIRST_EXTENT(eh
) + 1;
575 r
= EXT_LAST_EXTENT(eh
);
579 if (block
< le32_to_cpu(m
->ee_block
))
583 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
584 m
, le32_to_cpu(m
->ee_block
),
585 r
, le32_to_cpu(r
->ee_block
));
589 ext_debug(" -> %d:%llu:[%d]%d ",
590 le32_to_cpu(path
->p_ext
->ee_block
),
591 ext4_ext_pblock(path
->p_ext
),
592 ext4_ext_is_uninitialized(path
->p_ext
),
593 ext4_ext_get_actual_len(path
->p_ext
));
595 #ifdef CHECK_BINSEARCH
597 struct ext4_extent
*chex
, *ex
;
600 chex
= ex
= EXT_FIRST_EXTENT(eh
);
601 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
602 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
603 <= le32_to_cpu(ex
[-1].ee_block
));
604 if (block
< le32_to_cpu(ex
->ee_block
))
608 BUG_ON(chex
!= path
->p_ext
);
614 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
616 struct ext4_extent_header
*eh
;
618 eh
= ext_inode_hdr(inode
);
621 eh
->eh_magic
= EXT4_EXT_MAGIC
;
622 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
623 ext4_mark_inode_dirty(handle
, inode
);
624 ext4_ext_invalidate_cache(inode
);
628 struct ext4_ext_path
*
629 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
630 struct ext4_ext_path
*path
)
632 struct ext4_extent_header
*eh
;
633 struct buffer_head
*bh
;
634 short int depth
, i
, ppos
= 0, alloc
= 0;
636 eh
= ext_inode_hdr(inode
);
637 depth
= ext_depth(inode
);
639 /* account possible depth increase */
641 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
644 return ERR_PTR(-ENOMEM
);
651 /* walk through the tree */
653 int need_to_validate
= 0;
655 ext_debug("depth %d: num %d, max %d\n",
656 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
658 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
659 path
[ppos
].p_block
= ext4_idx_pblock(path
[ppos
].p_idx
);
660 path
[ppos
].p_depth
= i
;
661 path
[ppos
].p_ext
= NULL
;
663 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
666 if (!bh_uptodate_or_lock(bh
)) {
667 if (bh_submit_read(bh
) < 0) {
671 /* validate the extent entries */
672 need_to_validate
= 1;
674 eh
= ext_block_hdr(bh
);
676 if (unlikely(ppos
> depth
)) {
678 EXT4_ERROR_INODE(inode
,
679 "ppos %d > depth %d", ppos
, depth
);
682 path
[ppos
].p_bh
= bh
;
683 path
[ppos
].p_hdr
= eh
;
686 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
690 path
[ppos
].p_depth
= i
;
691 path
[ppos
].p_ext
= NULL
;
692 path
[ppos
].p_idx
= NULL
;
695 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
696 /* if not an empty leaf */
697 if (path
[ppos
].p_ext
)
698 path
[ppos
].p_block
= ext4_ext_pblock(path
[ppos
].p_ext
);
700 ext4_ext_show_path(inode
, path
);
705 ext4_ext_drop_refs(path
);
708 return ERR_PTR(-EIO
);
712 * ext4_ext_insert_index:
713 * insert new index [@logical;@ptr] into the block at @curp;
714 * check where to insert: before @curp or after @curp
716 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
717 struct ext4_ext_path
*curp
,
718 int logical
, ext4_fsblk_t ptr
)
720 struct ext4_extent_idx
*ix
;
723 err
= ext4_ext_get_access(handle
, inode
, curp
);
727 if (unlikely(logical
== le32_to_cpu(curp
->p_idx
->ei_block
))) {
728 EXT4_ERROR_INODE(inode
,
729 "logical %d == ei_block %d!",
730 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
733 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
734 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
736 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
737 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
738 len
= len
< 0 ? 0 : len
;
739 ext_debug("insert new index %d after: %llu. "
740 "move %d from 0x%p to 0x%p\n",
742 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
743 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
745 ix
= curp
->p_idx
+ 1;
748 len
= len
* sizeof(struct ext4_extent_idx
);
749 len
= len
< 0 ? 0 : len
;
750 ext_debug("insert new index %d before: %llu. "
751 "move %d from 0x%p to 0x%p\n",
753 curp
->p_idx
, (curp
->p_idx
+ 1));
754 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
758 ix
->ei_block
= cpu_to_le32(logical
);
759 ext4_idx_store_pblock(ix
, ptr
);
760 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
762 if (unlikely(le16_to_cpu(curp
->p_hdr
->eh_entries
)
763 > le16_to_cpu(curp
->p_hdr
->eh_max
))) {
764 EXT4_ERROR_INODE(inode
,
765 "logical %d == ei_block %d!",
766 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
769 if (unlikely(ix
> EXT_LAST_INDEX(curp
->p_hdr
))) {
770 EXT4_ERROR_INODE(inode
, "ix > EXT_LAST_INDEX!");
774 err
= ext4_ext_dirty(handle
, inode
, curp
);
775 ext4_std_error(inode
->i_sb
, err
);
782 * inserts new subtree into the path, using free index entry
784 * - allocates all needed blocks (new leaf and all intermediate index blocks)
785 * - makes decision where to split
786 * - moves remaining extents and index entries (right to the split point)
787 * into the newly allocated blocks
788 * - initializes subtree
790 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
791 struct ext4_ext_path
*path
,
792 struct ext4_extent
*newext
, int at
)
794 struct buffer_head
*bh
= NULL
;
795 int depth
= ext_depth(inode
);
796 struct ext4_extent_header
*neh
;
797 struct ext4_extent_idx
*fidx
;
798 struct ext4_extent
*ex
;
800 ext4_fsblk_t newblock
, oldblock
;
802 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
805 /* make decision: where to split? */
806 /* FIXME: now decision is simplest: at current extent */
808 /* if current leaf will be split, then we should use
809 * border from split point */
810 if (unlikely(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
))) {
811 EXT4_ERROR_INODE(inode
, "p_ext > EXT_MAX_EXTENT!");
814 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
815 border
= path
[depth
].p_ext
[1].ee_block
;
816 ext_debug("leaf will be split."
817 " next leaf starts at %d\n",
818 le32_to_cpu(border
));
820 border
= newext
->ee_block
;
821 ext_debug("leaf will be added."
822 " next leaf starts at %d\n",
823 le32_to_cpu(border
));
827 * If error occurs, then we break processing
828 * and mark filesystem read-only. index won't
829 * be inserted and tree will be in consistent
830 * state. Next mount will repair buffers too.
834 * Get array to track all allocated blocks.
835 * We need this to handle errors and free blocks
838 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
842 /* allocate all needed blocks */
843 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
844 for (a
= 0; a
< depth
- at
; a
++) {
845 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
849 ablocks
[a
] = newblock
;
852 /* initialize new leaf */
853 newblock
= ablocks
[--a
];
854 if (unlikely(newblock
== 0)) {
855 EXT4_ERROR_INODE(inode
, "newblock == 0!");
859 bh
= sb_getblk(inode
->i_sb
, newblock
);
866 err
= ext4_journal_get_create_access(handle
, bh
);
870 neh
= ext_block_hdr(bh
);
872 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
873 neh
->eh_magic
= EXT4_EXT_MAGIC
;
875 ex
= EXT_FIRST_EXTENT(neh
);
877 /* move remainder of path[depth] to the new leaf */
878 if (unlikely(path
[depth
].p_hdr
->eh_entries
!=
879 path
[depth
].p_hdr
->eh_max
)) {
880 EXT4_ERROR_INODE(inode
, "eh_entries %d != eh_max %d!",
881 path
[depth
].p_hdr
->eh_entries
,
882 path
[depth
].p_hdr
->eh_max
);
886 /* start copy from next extent */
887 /* TODO: we could do it by single memmove */
890 while (path
[depth
].p_ext
<=
891 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
892 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
893 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
894 ext4_ext_pblock(path
[depth
].p_ext
),
895 ext4_ext_is_uninitialized(path
[depth
].p_ext
),
896 ext4_ext_get_actual_len(path
[depth
].p_ext
),
898 /*memmove(ex++, path[depth].p_ext++,
899 sizeof(struct ext4_extent));
905 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
906 le16_add_cpu(&neh
->eh_entries
, m
);
909 set_buffer_uptodate(bh
);
912 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
918 /* correct old leaf */
920 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
923 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
924 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
930 /* create intermediate indexes */
932 if (unlikely(k
< 0)) {
933 EXT4_ERROR_INODE(inode
, "k %d < 0!", k
);
938 ext_debug("create %d intermediate indices\n", k
);
939 /* insert new index into current index block */
940 /* current depth stored in i var */
944 newblock
= ablocks
[--a
];
945 bh
= sb_getblk(inode
->i_sb
, newblock
);
952 err
= ext4_journal_get_create_access(handle
, bh
);
956 neh
= ext_block_hdr(bh
);
957 neh
->eh_entries
= cpu_to_le16(1);
958 neh
->eh_magic
= EXT4_EXT_MAGIC
;
959 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
960 neh
->eh_depth
= cpu_to_le16(depth
- i
);
961 fidx
= EXT_FIRST_INDEX(neh
);
962 fidx
->ei_block
= border
;
963 ext4_idx_store_pblock(fidx
, oldblock
);
965 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
966 i
, newblock
, le32_to_cpu(border
), oldblock
);
971 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
972 EXT_MAX_INDEX(path
[i
].p_hdr
));
973 if (unlikely(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
974 EXT_LAST_INDEX(path
[i
].p_hdr
))) {
975 EXT4_ERROR_INODE(inode
,
976 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
977 le32_to_cpu(path
[i
].p_ext
->ee_block
));
981 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
982 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
983 le32_to_cpu(path
[i
].p_idx
->ei_block
),
984 ext4_idx_pblock(path
[i
].p_idx
),
986 /*memmove(++fidx, path[i].p_idx++,
987 sizeof(struct ext4_extent_idx));
989 BUG_ON(neh->eh_entries > neh->eh_max);*/
994 memmove(++fidx
, path
[i
].p_idx
- m
,
995 sizeof(struct ext4_extent_idx
) * m
);
996 le16_add_cpu(&neh
->eh_entries
, m
);
998 set_buffer_uptodate(bh
);
1001 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1007 /* correct old index */
1009 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
1012 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
1013 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1021 /* insert new index */
1022 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1023 le32_to_cpu(border
), newblock
);
1027 if (buffer_locked(bh
))
1033 /* free all allocated blocks in error case */
1034 for (i
= 0; i
< depth
; i
++) {
1037 ext4_free_blocks(handle
, inode
, 0, ablocks
[i
], 1,
1038 EXT4_FREE_BLOCKS_METADATA
);
1047 * ext4_ext_grow_indepth:
1048 * implements tree growing procedure:
1049 * - allocates new block
1050 * - moves top-level data (index block or leaf) into the new block
1051 * - initializes new top-level, creating index that points to the
1052 * just created block
1054 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1055 struct ext4_ext_path
*path
,
1056 struct ext4_extent
*newext
)
1058 struct ext4_ext_path
*curp
= path
;
1059 struct ext4_extent_header
*neh
;
1060 struct buffer_head
*bh
;
1061 ext4_fsblk_t newblock
;
1064 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
, newext
, &err
);
1068 bh
= sb_getblk(inode
->i_sb
, newblock
);
1071 ext4_std_error(inode
->i_sb
, err
);
1076 err
= ext4_journal_get_create_access(handle
, bh
);
1082 /* move top-level index/leaf into new block */
1083 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1085 /* set size of new block */
1086 neh
= ext_block_hdr(bh
);
1087 /* old root could have indexes or leaves
1088 * so calculate e_max right way */
1089 if (ext_depth(inode
))
1090 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1092 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1093 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1094 set_buffer_uptodate(bh
);
1097 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1101 /* create index in new top-level index: num,max,pointer */
1102 err
= ext4_ext_get_access(handle
, inode
, curp
);
1106 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1107 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1108 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1109 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1111 if (path
[0].p_hdr
->eh_depth
)
1112 curp
->p_idx
->ei_block
=
1113 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1115 curp
->p_idx
->ei_block
=
1116 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1117 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1119 neh
= ext_inode_hdr(inode
);
1120 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1121 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1122 le32_to_cpu(EXT_FIRST_INDEX(neh
)->ei_block
),
1123 ext4_idx_pblock(EXT_FIRST_INDEX(neh
)));
1125 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1126 err
= ext4_ext_dirty(handle
, inode
, curp
);
1134 * ext4_ext_create_new_leaf:
1135 * finds empty index and adds new leaf.
1136 * if no free index is found, then it requests in-depth growing.
1138 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1139 struct ext4_ext_path
*path
,
1140 struct ext4_extent
*newext
)
1142 struct ext4_ext_path
*curp
;
1143 int depth
, i
, err
= 0;
1146 i
= depth
= ext_depth(inode
);
1148 /* walk up to the tree and look for free index entry */
1149 curp
= path
+ depth
;
1150 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1155 /* we use already allocated block for index block,
1156 * so subsequent data blocks should be contiguous */
1157 if (EXT_HAS_FREE_INDEX(curp
)) {
1158 /* if we found index with free entry, then use that
1159 * entry: create all needed subtree and add new leaf */
1160 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
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
);
1172 /* tree is full, time to grow in depth */
1173 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1178 ext4_ext_drop_refs(path
);
1179 path
= ext4_ext_find_extent(inode
,
1180 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1183 err
= PTR_ERR(path
);
1188 * only first (depth 0 -> 1) produces free space;
1189 * in all other cases we have to split the grown tree
1191 depth
= ext_depth(inode
);
1192 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1193 /* now we need to split */
1203 * search the closest allocated block to the left for *logical
1204 * and returns it at @logical + it's physical address at @phys
1205 * if *logical is the smallest allocated block, the function
1206 * returns 0 at @phys
1207 * return value contains 0 (success) or error code
1209 static int ext4_ext_search_left(struct inode
*inode
,
1210 struct ext4_ext_path
*path
,
1211 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1213 struct ext4_extent_idx
*ix
;
1214 struct ext4_extent
*ex
;
1217 if (unlikely(path
== NULL
)) {
1218 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1221 depth
= path
->p_depth
;
1224 if (depth
== 0 && path
->p_ext
== NULL
)
1227 /* usually extent in the path covers blocks smaller
1228 * then *logical, but it can be that extent is the
1229 * first one in the file */
1231 ex
= path
[depth
].p_ext
;
1232 ee_len
= ext4_ext_get_actual_len(ex
);
1233 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1234 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1235 EXT4_ERROR_INODE(inode
,
1236 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1237 *logical
, le32_to_cpu(ex
->ee_block
));
1240 while (--depth
>= 0) {
1241 ix
= path
[depth
].p_idx
;
1242 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1243 EXT4_ERROR_INODE(inode
,
1244 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1245 ix
!= NULL
? ix
->ei_block
: 0,
1246 EXT_FIRST_INDEX(path
[depth
].p_hdr
) != NULL
?
1247 EXT_FIRST_INDEX(path
[depth
].p_hdr
)->ei_block
: 0,
1255 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1256 EXT4_ERROR_INODE(inode
,
1257 "logical %d < ee_block %d + ee_len %d!",
1258 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1262 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1263 *phys
= ext4_ext_pblock(ex
) + ee_len
- 1;
1268 * search the closest allocated block to the right for *logical
1269 * and returns it at @logical + it's physical address at @phys
1270 * if *logical is the smallest allocated block, the function
1271 * returns 0 at @phys
1272 * return value contains 0 (success) or error code
1274 static int ext4_ext_search_right(struct inode
*inode
,
1275 struct ext4_ext_path
*path
,
1276 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1278 struct buffer_head
*bh
= NULL
;
1279 struct ext4_extent_header
*eh
;
1280 struct ext4_extent_idx
*ix
;
1281 struct ext4_extent
*ex
;
1283 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1286 if (unlikely(path
== NULL
)) {
1287 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1290 depth
= path
->p_depth
;
1293 if (depth
== 0 && path
->p_ext
== NULL
)
1296 /* usually extent in the path covers blocks smaller
1297 * then *logical, but it can be that extent is the
1298 * first one in the file */
1300 ex
= path
[depth
].p_ext
;
1301 ee_len
= ext4_ext_get_actual_len(ex
);
1302 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1303 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1304 EXT4_ERROR_INODE(inode
,
1305 "first_extent(path[%d].p_hdr) != ex",
1309 while (--depth
>= 0) {
1310 ix
= path
[depth
].p_idx
;
1311 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1312 EXT4_ERROR_INODE(inode
,
1313 "ix != EXT_FIRST_INDEX *logical %d!",
1318 *logical
= le32_to_cpu(ex
->ee_block
);
1319 *phys
= ext4_ext_pblock(ex
);
1323 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1324 EXT4_ERROR_INODE(inode
,
1325 "logical %d < ee_block %d + ee_len %d!",
1326 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1330 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1331 /* next allocated block in this leaf */
1333 *logical
= le32_to_cpu(ex
->ee_block
);
1334 *phys
= ext4_ext_pblock(ex
);
1338 /* go up and search for index to the right */
1339 while (--depth
>= 0) {
1340 ix
= path
[depth
].p_idx
;
1341 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1345 /* we've gone up to the root and found no index to the right */
1349 /* we've found index to the right, let's
1350 * follow it and find the closest allocated
1351 * block to the right */
1353 block
= ext4_idx_pblock(ix
);
1354 while (++depth
< path
->p_depth
) {
1355 bh
= sb_bread(inode
->i_sb
, block
);
1358 eh
= ext_block_hdr(bh
);
1359 /* subtract from p_depth to get proper eh_depth */
1360 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1364 ix
= EXT_FIRST_INDEX(eh
);
1365 block
= ext4_idx_pblock(ix
);
1369 bh
= sb_bread(inode
->i_sb
, block
);
1372 eh
= ext_block_hdr(bh
);
1373 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1377 ex
= EXT_FIRST_EXTENT(eh
);
1378 *logical
= le32_to_cpu(ex
->ee_block
);
1379 *phys
= ext4_ext_pblock(ex
);
1385 * ext4_ext_next_allocated_block:
1386 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1387 * NOTE: it considers block number from index entry as
1388 * allocated block. Thus, index entries have to be consistent
1392 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1396 BUG_ON(path
== NULL
);
1397 depth
= path
->p_depth
;
1399 if (depth
== 0 && path
->p_ext
== NULL
)
1400 return EXT_MAX_BLOCK
;
1402 while (depth
>= 0) {
1403 if (depth
== path
->p_depth
) {
1405 if (path
[depth
].p_ext
!=
1406 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1407 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1410 if (path
[depth
].p_idx
!=
1411 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1412 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1417 return EXT_MAX_BLOCK
;
1421 * ext4_ext_next_leaf_block:
1422 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1424 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1425 struct ext4_ext_path
*path
)
1429 BUG_ON(path
== NULL
);
1430 depth
= path
->p_depth
;
1432 /* zero-tree has no leaf blocks at all */
1434 return EXT_MAX_BLOCK
;
1436 /* go to index block */
1439 while (depth
>= 0) {
1440 if (path
[depth
].p_idx
!=
1441 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1442 return (ext4_lblk_t
)
1443 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1447 return EXT_MAX_BLOCK
;
1451 * ext4_ext_correct_indexes:
1452 * if leaf gets modified and modified extent is first in the leaf,
1453 * then we have to correct all indexes above.
1454 * TODO: do we need to correct tree in all cases?
1456 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1457 struct ext4_ext_path
*path
)
1459 struct ext4_extent_header
*eh
;
1460 int depth
= ext_depth(inode
);
1461 struct ext4_extent
*ex
;
1465 eh
= path
[depth
].p_hdr
;
1466 ex
= path
[depth
].p_ext
;
1468 if (unlikely(ex
== NULL
|| eh
== NULL
)) {
1469 EXT4_ERROR_INODE(inode
,
1470 "ex %p == NULL or eh %p == NULL", ex
, eh
);
1475 /* there is no tree at all */
1479 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1480 /* we correct tree if first leaf got modified only */
1485 * TODO: we need correction if border is smaller than current one
1488 border
= path
[depth
].p_ext
->ee_block
;
1489 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1492 path
[k
].p_idx
->ei_block
= border
;
1493 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1498 /* change all left-side indexes */
1499 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1501 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1504 path
[k
].p_idx
->ei_block
= border
;
1505 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1514 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1515 struct ext4_extent
*ex2
)
1517 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1520 * Make sure that either both extents are uninitialized, or
1523 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1526 if (ext4_ext_is_uninitialized(ex1
))
1527 max_len
= EXT_UNINIT_MAX_LEN
;
1529 max_len
= EXT_INIT_MAX_LEN
;
1531 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1532 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1534 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1535 le32_to_cpu(ex2
->ee_block
))
1539 * To allow future support for preallocated extents to be added
1540 * as an RO_COMPAT feature, refuse to merge to extents if
1541 * this can result in the top bit of ee_len being set.
1543 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1545 #ifdef AGGRESSIVE_TEST
1546 if (ext1_ee_len
>= 4)
1550 if (ext4_ext_pblock(ex1
) + ext1_ee_len
== ext4_ext_pblock(ex2
))
1556 * This function tries to merge the "ex" extent to the next extent in the tree.
1557 * It always tries to merge towards right. If you want to merge towards
1558 * left, pass "ex - 1" as argument instead of "ex".
1559 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1560 * 1 if they got merged.
1562 static int ext4_ext_try_to_merge(struct inode
*inode
,
1563 struct ext4_ext_path
*path
,
1564 struct ext4_extent
*ex
)
1566 struct ext4_extent_header
*eh
;
1567 unsigned int depth
, len
;
1569 int uninitialized
= 0;
1571 depth
= ext_depth(inode
);
1572 BUG_ON(path
[depth
].p_hdr
== NULL
);
1573 eh
= path
[depth
].p_hdr
;
1575 while (ex
< EXT_LAST_EXTENT(eh
)) {
1576 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1578 /* merge with next extent! */
1579 if (ext4_ext_is_uninitialized(ex
))
1581 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1582 + ext4_ext_get_actual_len(ex
+ 1));
1584 ext4_ext_mark_uninitialized(ex
);
1586 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1587 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1588 * sizeof(struct ext4_extent
);
1589 memmove(ex
+ 1, ex
+ 2, len
);
1591 le16_add_cpu(&eh
->eh_entries
, -1);
1593 WARN_ON(eh
->eh_entries
== 0);
1594 if (!eh
->eh_entries
)
1595 EXT4_ERROR_INODE(inode
, "eh->eh_entries = 0!");
1602 * check if a portion of the "newext" extent overlaps with an
1605 * If there is an overlap discovered, it updates the length of the newext
1606 * such that there will be no overlap, and then returns 1.
1607 * If there is no overlap found, it returns 0.
1609 static unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1610 struct ext4_extent
*newext
,
1611 struct ext4_ext_path
*path
)
1614 unsigned int depth
, len1
;
1615 unsigned int ret
= 0;
1617 b1
= le32_to_cpu(newext
->ee_block
);
1618 len1
= ext4_ext_get_actual_len(newext
);
1619 depth
= ext_depth(inode
);
1620 if (!path
[depth
].p_ext
)
1622 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1625 * get the next allocated block if the extent in the path
1626 * is before the requested block(s)
1629 b2
= ext4_ext_next_allocated_block(path
);
1630 if (b2
== EXT_MAX_BLOCK
)
1634 /* check for wrap through zero on extent logical start block*/
1635 if (b1
+ len1
< b1
) {
1636 len1
= EXT_MAX_BLOCK
- b1
;
1637 newext
->ee_len
= cpu_to_le16(len1
);
1641 /* check for overlap */
1642 if (b1
+ len1
> b2
) {
1643 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1651 * ext4_ext_insert_extent:
1652 * tries to merge requsted extent into the existing extent or
1653 * inserts requested extent as new one into the tree,
1654 * creating new leaf in the no-space case.
1656 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1657 struct ext4_ext_path
*path
,
1658 struct ext4_extent
*newext
, int flag
)
1660 struct ext4_extent_header
*eh
;
1661 struct ext4_extent
*ex
, *fex
;
1662 struct ext4_extent
*nearex
; /* nearest extent */
1663 struct ext4_ext_path
*npath
= NULL
;
1664 int depth
, len
, err
;
1666 unsigned uninitialized
= 0;
1668 if (unlikely(ext4_ext_get_actual_len(newext
) == 0)) {
1669 EXT4_ERROR_INODE(inode
, "ext4_ext_get_actual_len(newext) == 0");
1672 depth
= ext_depth(inode
);
1673 ex
= path
[depth
].p_ext
;
1674 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1675 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1679 /* try to insert block into found extent and return */
1680 if (ex
&& !(flag
& EXT4_GET_BLOCKS_PRE_IO
)
1681 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1682 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1683 ext4_ext_is_uninitialized(newext
),
1684 ext4_ext_get_actual_len(newext
),
1685 le32_to_cpu(ex
->ee_block
),
1686 ext4_ext_is_uninitialized(ex
),
1687 ext4_ext_get_actual_len(ex
),
1688 ext4_ext_pblock(ex
));
1689 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1694 * ext4_can_extents_be_merged should have checked that either
1695 * both extents are uninitialized, or both aren't. Thus we
1696 * need to check only one of them here.
1698 if (ext4_ext_is_uninitialized(ex
))
1700 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1701 + ext4_ext_get_actual_len(newext
));
1703 ext4_ext_mark_uninitialized(ex
);
1704 eh
= path
[depth
].p_hdr
;
1710 depth
= ext_depth(inode
);
1711 eh
= path
[depth
].p_hdr
;
1712 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1715 /* probably next leaf has space for us? */
1716 fex
= EXT_LAST_EXTENT(eh
);
1717 next
= ext4_ext_next_leaf_block(inode
, path
);
1718 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1719 && next
!= EXT_MAX_BLOCK
) {
1720 ext_debug("next leaf block - %d\n", next
);
1721 BUG_ON(npath
!= NULL
);
1722 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1724 return PTR_ERR(npath
);
1725 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1726 eh
= npath
[depth
].p_hdr
;
1727 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1728 ext_debug("next leaf isnt full(%d)\n",
1729 le16_to_cpu(eh
->eh_entries
));
1733 ext_debug("next leaf has no free space(%d,%d)\n",
1734 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1738 * There is no free space in the found leaf.
1739 * We're gonna add a new leaf in the tree.
1741 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1744 depth
= ext_depth(inode
);
1745 eh
= path
[depth
].p_hdr
;
1748 nearex
= path
[depth
].p_ext
;
1750 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1755 /* there is no extent in this leaf, create first one */
1756 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1757 le32_to_cpu(newext
->ee_block
),
1758 ext4_ext_pblock(newext
),
1759 ext4_ext_is_uninitialized(newext
),
1760 ext4_ext_get_actual_len(newext
));
1761 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1762 } else if (le32_to_cpu(newext
->ee_block
)
1763 > le32_to_cpu(nearex
->ee_block
)) {
1764 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1765 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1766 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1767 len
= (len
- 1) * sizeof(struct ext4_extent
);
1768 len
= len
< 0 ? 0 : len
;
1769 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1770 "move %d from 0x%p to 0x%p\n",
1771 le32_to_cpu(newext
->ee_block
),
1772 ext4_ext_pblock(newext
),
1773 ext4_ext_is_uninitialized(newext
),
1774 ext4_ext_get_actual_len(newext
),
1775 nearex
, len
, nearex
+ 1, nearex
+ 2);
1776 memmove(nearex
+ 2, nearex
+ 1, len
);
1778 path
[depth
].p_ext
= nearex
+ 1;
1780 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1781 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1782 len
= len
< 0 ? 0 : len
;
1783 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1784 "move %d from 0x%p to 0x%p\n",
1785 le32_to_cpu(newext
->ee_block
),
1786 ext4_ext_pblock(newext
),
1787 ext4_ext_is_uninitialized(newext
),
1788 ext4_ext_get_actual_len(newext
),
1789 nearex
, len
, nearex
+ 1, nearex
+ 2);
1790 memmove(nearex
+ 1, nearex
, len
);
1791 path
[depth
].p_ext
= nearex
;
1794 le16_add_cpu(&eh
->eh_entries
, 1);
1795 nearex
= path
[depth
].p_ext
;
1796 nearex
->ee_block
= newext
->ee_block
;
1797 ext4_ext_store_pblock(nearex
, ext4_ext_pblock(newext
));
1798 nearex
->ee_len
= newext
->ee_len
;
1801 /* try to merge extents to the right */
1802 if (!(flag
& EXT4_GET_BLOCKS_PRE_IO
))
1803 ext4_ext_try_to_merge(inode
, path
, nearex
);
1805 /* try to merge extents to the left */
1807 /* time to correct all indexes above */
1808 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1812 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1816 ext4_ext_drop_refs(npath
);
1819 ext4_ext_invalidate_cache(inode
);
1823 static int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1824 ext4_lblk_t num
, ext_prepare_callback func
,
1827 struct ext4_ext_path
*path
= NULL
;
1828 struct ext4_ext_cache cbex
;
1829 struct ext4_extent
*ex
;
1830 ext4_lblk_t next
, start
= 0, end
= 0;
1831 ext4_lblk_t last
= block
+ num
;
1832 int depth
, exists
, err
= 0;
1834 BUG_ON(func
== NULL
);
1835 BUG_ON(inode
== NULL
);
1837 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1839 /* find extent for this block */
1840 down_read(&EXT4_I(inode
)->i_data_sem
);
1841 path
= ext4_ext_find_extent(inode
, block
, path
);
1842 up_read(&EXT4_I(inode
)->i_data_sem
);
1844 err
= PTR_ERR(path
);
1849 depth
= ext_depth(inode
);
1850 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1851 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1855 ex
= path
[depth
].p_ext
;
1856 next
= ext4_ext_next_allocated_block(path
);
1860 /* there is no extent yet, so try to allocate
1861 * all requested space */
1864 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1865 /* need to allocate space before found extent */
1867 end
= le32_to_cpu(ex
->ee_block
);
1868 if (block
+ num
< end
)
1870 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1871 + ext4_ext_get_actual_len(ex
)) {
1872 /* need to allocate space after found extent */
1877 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1879 * some part of requested space is covered
1883 end
= le32_to_cpu(ex
->ee_block
)
1884 + ext4_ext_get_actual_len(ex
);
1885 if (block
+ num
< end
)
1891 BUG_ON(end
<= start
);
1894 cbex
.ec_block
= start
;
1895 cbex
.ec_len
= end
- start
;
1898 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1899 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1900 cbex
.ec_start
= ext4_ext_pblock(ex
);
1903 if (unlikely(cbex
.ec_len
== 0)) {
1904 EXT4_ERROR_INODE(inode
, "cbex.ec_len == 0");
1908 err
= func(inode
, path
, &cbex
, ex
, cbdata
);
1909 ext4_ext_drop_refs(path
);
1914 if (err
== EXT_REPEAT
)
1916 else if (err
== EXT_BREAK
) {
1921 if (ext_depth(inode
) != depth
) {
1922 /* depth was changed. we have to realloc path */
1927 block
= cbex
.ec_block
+ cbex
.ec_len
;
1931 ext4_ext_drop_refs(path
);
1939 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1940 __u32 len
, ext4_fsblk_t start
)
1942 struct ext4_ext_cache
*cex
;
1944 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1945 cex
= &EXT4_I(inode
)->i_cached_extent
;
1946 cex
->ec_block
= block
;
1948 cex
->ec_start
= start
;
1949 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1953 * ext4_ext_put_gap_in_cache:
1954 * calculate boundaries of the gap that the requested block fits into
1955 * and cache this gap
1958 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1961 int depth
= ext_depth(inode
);
1964 struct ext4_extent
*ex
;
1966 ex
= path
[depth
].p_ext
;
1968 /* there is no extent yet, so gap is [0;-] */
1970 len
= EXT_MAX_BLOCK
;
1971 ext_debug("cache gap(whole file):");
1972 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1974 len
= le32_to_cpu(ex
->ee_block
) - block
;
1975 ext_debug("cache gap(before): %u [%u:%u]",
1977 le32_to_cpu(ex
->ee_block
),
1978 ext4_ext_get_actual_len(ex
));
1979 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1980 + ext4_ext_get_actual_len(ex
)) {
1982 lblock
= le32_to_cpu(ex
->ee_block
)
1983 + ext4_ext_get_actual_len(ex
);
1985 next
= ext4_ext_next_allocated_block(path
);
1986 ext_debug("cache gap(after): [%u:%u] %u",
1987 le32_to_cpu(ex
->ee_block
),
1988 ext4_ext_get_actual_len(ex
),
1990 BUG_ON(next
== lblock
);
1991 len
= next
- lblock
;
1997 ext_debug(" -> %u:%lu\n", lblock
, len
);
1998 ext4_ext_put_in_cache(inode
, lblock
, len
, 0);
2002 * Return 0 if cache is invalid; 1 if the cache is valid
2005 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
2006 struct ext4_extent
*ex
)
2008 struct ext4_ext_cache
*cex
;
2012 * We borrow i_block_reservation_lock to protect i_cached_extent
2014 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
2015 cex
= &EXT4_I(inode
)->i_cached_extent
;
2017 /* has cache valid data? */
2018 if (cex
->ec_len
== 0)
2021 if (in_range(block
, cex
->ec_block
, cex
->ec_len
)) {
2022 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
2023 ext4_ext_store_pblock(ex
, cex
->ec_start
);
2024 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
2025 ext_debug("%u cached by %u:%u:%llu\n",
2027 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
2031 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
2037 * removes index from the index block.
2038 * It's used in truncate case only, thus all requests are for
2039 * last index in the block only.
2041 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
2042 struct ext4_ext_path
*path
)
2047 /* free index block */
2049 leaf
= ext4_idx_pblock(path
->p_idx
);
2050 if (unlikely(path
->p_hdr
->eh_entries
== 0)) {
2051 EXT4_ERROR_INODE(inode
, "path->p_hdr->eh_entries == 0");
2054 err
= ext4_ext_get_access(handle
, inode
, path
);
2057 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
2058 err
= ext4_ext_dirty(handle
, inode
, path
);
2061 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
2062 ext4_free_blocks(handle
, inode
, 0, leaf
, 1,
2063 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
2068 * ext4_ext_calc_credits_for_single_extent:
2069 * This routine returns max. credits that needed to insert an extent
2070 * to the extent tree.
2071 * When pass the actual path, the caller should calculate credits
2074 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2075 struct ext4_ext_path
*path
)
2078 int depth
= ext_depth(inode
);
2081 /* probably there is space in leaf? */
2082 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2083 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2086 * There are some space in the leaf tree, no
2087 * need to account for leaf block credit
2089 * bitmaps and block group descriptor blocks
2090 * and other metadat blocks still need to be
2093 /* 1 bitmap, 1 block group descriptor */
2094 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2099 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2103 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2105 * if nrblocks are fit in a single extent (chunk flag is 1), then
2106 * in the worse case, each tree level index/leaf need to be changed
2107 * if the tree split due to insert a new extent, then the old tree
2108 * index/leaf need to be updated too
2110 * If the nrblocks are discontiguous, they could cause
2111 * the whole tree split more than once, but this is really rare.
2113 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2116 int depth
= ext_depth(inode
);
2126 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2127 struct ext4_extent
*ex
,
2128 ext4_lblk_t from
, ext4_lblk_t to
)
2130 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2131 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2133 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2134 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2135 #ifdef EXTENTS_STATS
2137 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2138 spin_lock(&sbi
->s_ext_stats_lock
);
2139 sbi
->s_ext_blocks
+= ee_len
;
2140 sbi
->s_ext_extents
++;
2141 if (ee_len
< sbi
->s_ext_min
)
2142 sbi
->s_ext_min
= ee_len
;
2143 if (ee_len
> sbi
->s_ext_max
)
2144 sbi
->s_ext_max
= ee_len
;
2145 if (ext_depth(inode
) > sbi
->s_depth_max
)
2146 sbi
->s_depth_max
= ext_depth(inode
);
2147 spin_unlock(&sbi
->s_ext_stats_lock
);
2150 if (from
>= le32_to_cpu(ex
->ee_block
)
2151 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2156 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2157 start
= ext4_ext_pblock(ex
) + ee_len
- num
;
2158 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2159 ext4_free_blocks(handle
, inode
, 0, start
, num
, flags
);
2160 } else if (from
== le32_to_cpu(ex
->ee_block
)
2161 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2162 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
2163 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2165 printk(KERN_INFO
"strange request: removal(2) "
2166 "%u-%u from %u:%u\n",
2167 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2173 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2174 struct ext4_ext_path
*path
, ext4_lblk_t start
)
2176 int err
= 0, correct_index
= 0;
2177 int depth
= ext_depth(inode
), credits
;
2178 struct ext4_extent_header
*eh
;
2179 ext4_lblk_t a
, b
, block
;
2181 ext4_lblk_t ex_ee_block
;
2182 unsigned short ex_ee_len
;
2183 unsigned uninitialized
= 0;
2184 struct ext4_extent
*ex
;
2186 /* the header must be checked already in ext4_ext_remove_space() */
2187 ext_debug("truncate since %u in leaf\n", start
);
2188 if (!path
[depth
].p_hdr
)
2189 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2190 eh
= path
[depth
].p_hdr
;
2191 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2192 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2195 /* find where to start removing */
2196 ex
= EXT_LAST_EXTENT(eh
);
2198 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2199 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2201 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2202 ex_ee_block
+ ex_ee_len
> start
) {
2204 if (ext4_ext_is_uninitialized(ex
))
2209 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2210 uninitialized
, ex_ee_len
);
2211 path
[depth
].p_ext
= ex
;
2213 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2214 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
2215 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
2217 ext_debug(" border %u:%u\n", a
, b
);
2219 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
2223 } else if (a
!= ex_ee_block
) {
2224 /* remove tail of the extent */
2225 block
= ex_ee_block
;
2227 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2228 /* remove head of the extent */
2231 /* there is no "make a hole" API yet */
2234 /* remove whole extent: excellent! */
2235 block
= ex_ee_block
;
2237 BUG_ON(a
!= ex_ee_block
);
2238 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
2242 * 3 for leaf, sb, and inode plus 2 (bmap and group
2243 * descriptor) for each block group; assume two block
2244 * groups plus ex_ee_len/blocks_per_block_group for
2247 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2248 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2250 credits
+= (ext_depth(inode
)) + 1;
2252 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2254 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2258 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2262 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2267 /* this extent is removed; mark slot entirely unused */
2268 ext4_ext_store_pblock(ex
, 0);
2269 le16_add_cpu(&eh
->eh_entries
, -1);
2272 ex
->ee_block
= cpu_to_le32(block
);
2273 ex
->ee_len
= cpu_to_le16(num
);
2275 * Do not mark uninitialized if all the blocks in the
2276 * extent have been removed.
2278 if (uninitialized
&& num
)
2279 ext4_ext_mark_uninitialized(ex
);
2281 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2285 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2286 ext4_ext_pblock(ex
));
2288 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2289 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2292 if (correct_index
&& eh
->eh_entries
)
2293 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2295 /* if this leaf is free, then we should
2296 * remove it from index block above */
2297 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2298 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2305 * ext4_ext_more_to_rm:
2306 * returns 1 if current index has to be freed (even partial)
2309 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2311 BUG_ON(path
->p_idx
== NULL
);
2313 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2317 * if truncate on deeper level happened, it wasn't partial,
2318 * so we have to consider current index for truncation
2320 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2325 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2327 struct super_block
*sb
= inode
->i_sb
;
2328 int depth
= ext_depth(inode
);
2329 struct ext4_ext_path
*path
;
2333 ext_debug("truncate since %u\n", start
);
2335 /* probably first extent we're gonna free will be last in block */
2336 handle
= ext4_journal_start(inode
, depth
+ 1);
2338 return PTR_ERR(handle
);
2341 ext4_ext_invalidate_cache(inode
);
2344 * We start scanning from right side, freeing all the blocks
2345 * after i_size and walking into the tree depth-wise.
2347 depth
= ext_depth(inode
);
2348 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2350 ext4_journal_stop(handle
);
2353 path
[0].p_depth
= depth
;
2354 path
[0].p_hdr
= ext_inode_hdr(inode
);
2355 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2361 while (i
>= 0 && err
== 0) {
2363 /* this is leaf block */
2364 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2365 /* root level has p_bh == NULL, brelse() eats this */
2366 brelse(path
[i
].p_bh
);
2367 path
[i
].p_bh
= NULL
;
2372 /* this is index block */
2373 if (!path
[i
].p_hdr
) {
2374 ext_debug("initialize header\n");
2375 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2378 if (!path
[i
].p_idx
) {
2379 /* this level hasn't been touched yet */
2380 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2381 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2382 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2384 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2386 /* we were already here, see at next index */
2390 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2391 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2393 if (ext4_ext_more_to_rm(path
+ i
)) {
2394 struct buffer_head
*bh
;
2395 /* go to the next level */
2396 ext_debug("move to level %d (block %llu)\n",
2397 i
+ 1, ext4_idx_pblock(path
[i
].p_idx
));
2398 memset(path
+ i
+ 1, 0, sizeof(*path
));
2399 bh
= sb_bread(sb
, ext4_idx_pblock(path
[i
].p_idx
));
2401 /* should we reset i_size? */
2405 if (WARN_ON(i
+ 1 > depth
)) {
2409 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2414 path
[i
+ 1].p_bh
= bh
;
2416 /* save actual number of indexes since this
2417 * number is changed at the next iteration */
2418 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2421 /* we finished processing this index, go up */
2422 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2423 /* index is empty, remove it;
2424 * handle must be already prepared by the
2425 * truncatei_leaf() */
2426 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2428 /* root level has p_bh == NULL, brelse() eats this */
2429 brelse(path
[i
].p_bh
);
2430 path
[i
].p_bh
= NULL
;
2432 ext_debug("return to level %d\n", i
);
2436 /* TODO: flexible tree reduction should be here */
2437 if (path
->p_hdr
->eh_entries
== 0) {
2439 * truncate to zero freed all the tree,
2440 * so we need to correct eh_depth
2442 err
= ext4_ext_get_access(handle
, inode
, path
);
2444 ext_inode_hdr(inode
)->eh_depth
= 0;
2445 ext_inode_hdr(inode
)->eh_max
=
2446 cpu_to_le16(ext4_ext_space_root(inode
, 0));
2447 err
= ext4_ext_dirty(handle
, inode
, path
);
2451 ext4_ext_drop_refs(path
);
2455 ext4_journal_stop(handle
);
2461 * called at mount time
2463 void ext4_ext_init(struct super_block
*sb
)
2466 * possible initialization would be here
2469 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2470 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2471 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2472 #ifdef AGGRESSIVE_TEST
2473 printk(", aggressive tests");
2475 #ifdef CHECK_BINSEARCH
2476 printk(", check binsearch");
2478 #ifdef EXTENTS_STATS
2483 #ifdef EXTENTS_STATS
2484 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2485 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2486 EXT4_SB(sb
)->s_ext_max
= 0;
2492 * called at umount time
2494 void ext4_ext_release(struct super_block
*sb
)
2496 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2499 #ifdef EXTENTS_STATS
2500 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2501 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2502 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2503 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2504 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2505 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2506 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2511 /* FIXME!! we need to try to merge to left or right after zero-out */
2512 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2514 ext4_fsblk_t ee_pblock
;
2515 unsigned int ee_len
;
2518 ee_len
= ext4_ext_get_actual_len(ex
);
2519 ee_pblock
= ext4_ext_pblock(ex
);
2521 ret
= sb_issue_zeroout(inode
->i_sb
, ee_pblock
, ee_len
, GFP_NOFS
);
2528 #define EXT4_EXT_ZERO_LEN 7
2530 * This function is called by ext4_ext_map_blocks() if someone tries to write
2531 * to an uninitialized extent. It may result in splitting the uninitialized
2532 * extent into multiple extents (upto three - one initialized and two
2534 * There are three possibilities:
2535 * a> There is no split required: Entire extent should be initialized
2536 * b> Splits in two extents: Write is happening at either end of the extent
2537 * c> Splits in three extents: Somone is writing in middle of the extent
2539 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2540 struct inode
*inode
,
2541 struct ext4_map_blocks
*map
,
2542 struct ext4_ext_path
*path
)
2544 struct ext4_extent
*ex
, newex
, orig_ex
;
2545 struct ext4_extent
*ex1
= NULL
;
2546 struct ext4_extent
*ex2
= NULL
;
2547 struct ext4_extent
*ex3
= NULL
;
2548 struct ext4_extent_header
*eh
;
2549 ext4_lblk_t ee_block
, eof_block
;
2550 unsigned int allocated
, ee_len
, depth
;
2551 ext4_fsblk_t newblock
;
2556 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2557 "block %llu, max_blocks %u\n", inode
->i_ino
,
2558 (unsigned long long)map
->m_lblk
, map
->m_len
);
2560 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2561 inode
->i_sb
->s_blocksize_bits
;
2562 if (eof_block
< map
->m_lblk
+ map
->m_len
)
2563 eof_block
= map
->m_lblk
+ map
->m_len
;
2565 depth
= ext_depth(inode
);
2566 eh
= path
[depth
].p_hdr
;
2567 ex
= path
[depth
].p_ext
;
2568 ee_block
= le32_to_cpu(ex
->ee_block
);
2569 ee_len
= ext4_ext_get_actual_len(ex
);
2570 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
2571 newblock
= map
->m_lblk
- ee_block
+ ext4_ext_pblock(ex
);
2574 orig_ex
.ee_block
= ex
->ee_block
;
2575 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2576 ext4_ext_store_pblock(&orig_ex
, ext4_ext_pblock(ex
));
2579 * It is safe to convert extent to initialized via explicit
2580 * zeroout only if extent is fully insde i_size or new_size.
2582 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2584 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2587 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2588 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
&& may_zeroout
) {
2589 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2591 goto fix_extent_len
;
2592 /* update the extent length and mark as initialized */
2593 ex
->ee_block
= orig_ex
.ee_block
;
2594 ex
->ee_len
= orig_ex
.ee_len
;
2595 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
2596 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2597 /* zeroed the full extent */
2601 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2602 if (map
->m_lblk
> ee_block
) {
2604 ex1
->ee_len
= cpu_to_le16(map
->m_lblk
- ee_block
);
2605 ext4_ext_mark_uninitialized(ex1
);
2609 * for sanity, update the length of the ex2 extent before
2610 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2611 * overlap of blocks.
2613 if (!ex1
&& allocated
> map
->m_len
)
2614 ex2
->ee_len
= cpu_to_le16(map
->m_len
);
2615 /* ex3: to ee_block + ee_len : uninitialised */
2616 if (allocated
> map
->m_len
) {
2617 unsigned int newdepth
;
2618 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2619 if (allocated
<= EXT4_EXT_ZERO_LEN
&& may_zeroout
) {
2621 * map->m_lblk == ee_block is handled by the zerouout
2623 * Mark first half uninitialized.
2624 * Mark second half initialized and zero out the
2625 * initialized extent
2627 ex
->ee_block
= orig_ex
.ee_block
;
2628 ex
->ee_len
= cpu_to_le16(ee_len
- allocated
);
2629 ext4_ext_mark_uninitialized(ex
);
2630 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
2631 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2634 ex3
->ee_block
= cpu_to_le32(map
->m_lblk
);
2635 ext4_ext_store_pblock(ex3
, newblock
);
2636 ex3
->ee_len
= cpu_to_le16(allocated
);
2637 err
= ext4_ext_insert_extent(handle
, inode
, path
,
2639 if (err
== -ENOSPC
) {
2640 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2642 goto fix_extent_len
;
2643 ex
->ee_block
= orig_ex
.ee_block
;
2644 ex
->ee_len
= orig_ex
.ee_len
;
2645 ext4_ext_store_pblock(ex
,
2646 ext4_ext_pblock(&orig_ex
));
2647 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2648 /* blocks available from map->m_lblk */
2652 goto fix_extent_len
;
2655 * We need to zero out the second half because
2656 * an fallocate request can update file size and
2657 * converting the second half to initialized extent
2658 * implies that we can leak some junk data to user
2661 err
= ext4_ext_zeroout(inode
, ex3
);
2664 * We should actually mark the
2665 * second half as uninit and return error
2666 * Insert would have changed the extent
2668 depth
= ext_depth(inode
);
2669 ext4_ext_drop_refs(path
);
2670 path
= ext4_ext_find_extent(inode
, map
->m_lblk
,
2673 err
= PTR_ERR(path
);
2676 /* get the second half extent details */
2677 ex
= path
[depth
].p_ext
;
2678 err
= ext4_ext_get_access(handle
, inode
,
2682 ext4_ext_mark_uninitialized(ex
);
2683 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2687 /* zeroed the second half */
2691 ex3
->ee_block
= cpu_to_le32(map
->m_lblk
+ map
->m_len
);
2692 ext4_ext_store_pblock(ex3
, newblock
+ map
->m_len
);
2693 ex3
->ee_len
= cpu_to_le16(allocated
- map
->m_len
);
2694 ext4_ext_mark_uninitialized(ex3
);
2695 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, 0);
2696 if (err
== -ENOSPC
&& may_zeroout
) {
2697 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2699 goto fix_extent_len
;
2700 /* update the extent length and mark as initialized */
2701 ex
->ee_block
= orig_ex
.ee_block
;
2702 ex
->ee_len
= orig_ex
.ee_len
;
2703 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
2704 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2705 /* zeroed the full extent */
2706 /* blocks available from map->m_lblk */
2710 goto fix_extent_len
;
2712 * The depth, and hence eh & ex might change
2713 * as part of the insert above.
2715 newdepth
= ext_depth(inode
);
2717 * update the extent length after successful insert of the
2720 ee_len
-= ext4_ext_get_actual_len(ex3
);
2721 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2722 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2725 ext4_ext_drop_refs(path
);
2726 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, path
);
2728 err
= PTR_ERR(path
);
2731 eh
= path
[depth
].p_hdr
;
2732 ex
= path
[depth
].p_ext
;
2736 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2740 allocated
= map
->m_len
;
2742 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2743 * to insert a extent in the middle zerout directly
2744 * otherwise give the extent a chance to merge to left
2746 if (le16_to_cpu(orig_ex
.ee_len
) <= EXT4_EXT_ZERO_LEN
&&
2747 map
->m_lblk
!= ee_block
&& may_zeroout
) {
2748 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2750 goto fix_extent_len
;
2751 /* update the extent length and mark as initialized */
2752 ex
->ee_block
= orig_ex
.ee_block
;
2753 ex
->ee_len
= orig_ex
.ee_len
;
2754 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
2755 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2756 /* zero out the first half */
2757 /* blocks available from map->m_lblk */
2762 * If there was a change of depth as part of the
2763 * insertion of ex3 above, we need to update the length
2764 * of the ex1 extent again here
2766 if (ex1
&& ex1
!= ex
) {
2768 ex1
->ee_len
= cpu_to_le16(map
->m_lblk
- ee_block
);
2769 ext4_ext_mark_uninitialized(ex1
);
2772 /* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */
2773 ex2
->ee_block
= cpu_to_le32(map
->m_lblk
);
2774 ext4_ext_store_pblock(ex2
, newblock
);
2775 ex2
->ee_len
= cpu_to_le16(allocated
);
2779 * New (initialized) extent starts from the first block
2780 * in the current extent. i.e., ex2 == ex
2781 * We have to see if it can be merged with the extent
2784 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2786 * To merge left, pass "ex2 - 1" to try_to_merge(),
2787 * since it merges towards right _only_.
2789 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2791 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2794 depth
= ext_depth(inode
);
2799 * Try to Merge towards right. This might be required
2800 * only when the whole extent is being written to.
2801 * i.e. ex2 == ex and ex3 == NULL.
2804 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2806 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2811 /* Mark modified extent as dirty */
2812 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2815 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, 0);
2816 if (err
== -ENOSPC
&& may_zeroout
) {
2817 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2819 goto fix_extent_len
;
2820 /* update the extent length and mark as initialized */
2821 ex
->ee_block
= orig_ex
.ee_block
;
2822 ex
->ee_len
= orig_ex
.ee_len
;
2823 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
2824 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2825 /* zero out the first half */
2828 goto fix_extent_len
;
2830 ext4_ext_show_leaf(inode
, path
);
2831 return err
? err
: allocated
;
2834 ex
->ee_block
= orig_ex
.ee_block
;
2835 ex
->ee_len
= orig_ex
.ee_len
;
2836 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
2837 ext4_ext_mark_uninitialized(ex
);
2838 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2843 * This function is called by ext4_ext_map_blocks() from
2844 * ext4_get_blocks_dio_write() when DIO to write
2845 * to an uninitialized extent.
2847 * Writing to an uninitized extent may result in splitting the uninitialized
2848 * extent into multiple /initialized uninitialized extents (up to three)
2849 * There are three possibilities:
2850 * a> There is no split required: Entire extent should be uninitialized
2851 * b> Splits in two extents: Write is happening at either end of the extent
2852 * c> Splits in three extents: Somone is writing in middle of the extent
2854 * One of more index blocks maybe needed if the extent tree grow after
2855 * the uninitialized extent split. To prevent ENOSPC occur at the IO
2856 * complete, we need to split the uninitialized extent before DIO submit
2857 * the IO. The uninitialized extent called at this time will be split
2858 * into three uninitialized extent(at most). After IO complete, the part
2859 * being filled will be convert to initialized by the end_io callback function
2860 * via ext4_convert_unwritten_extents().
2862 * Returns the size of uninitialized extent to be written on success.
2864 static int ext4_split_unwritten_extents(handle_t
*handle
,
2865 struct inode
*inode
,
2866 struct ext4_map_blocks
*map
,
2867 struct ext4_ext_path
*path
,
2870 struct ext4_extent
*ex
, newex
, orig_ex
;
2871 struct ext4_extent
*ex1
= NULL
;
2872 struct ext4_extent
*ex2
= NULL
;
2873 struct ext4_extent
*ex3
= NULL
;
2874 ext4_lblk_t ee_block
, eof_block
;
2875 unsigned int allocated
, ee_len
, depth
;
2876 ext4_fsblk_t newblock
;
2880 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2881 "block %llu, max_blocks %u\n", inode
->i_ino
,
2882 (unsigned long long)map
->m_lblk
, map
->m_len
);
2884 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2885 inode
->i_sb
->s_blocksize_bits
;
2886 if (eof_block
< map
->m_lblk
+ map
->m_len
)
2887 eof_block
= map
->m_lblk
+ map
->m_len
;
2889 depth
= ext_depth(inode
);
2890 ex
= path
[depth
].p_ext
;
2891 ee_block
= le32_to_cpu(ex
->ee_block
);
2892 ee_len
= ext4_ext_get_actual_len(ex
);
2893 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
2894 newblock
= map
->m_lblk
- ee_block
+ ext4_ext_pblock(ex
);
2897 orig_ex
.ee_block
= ex
->ee_block
;
2898 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2899 ext4_ext_store_pblock(&orig_ex
, ext4_ext_pblock(ex
));
2902 * It is safe to convert extent to initialized via explicit
2903 * zeroout only if extent is fully insde i_size or new_size.
2905 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2908 * If the uninitialized extent begins at the same logical
2909 * block where the write begins, and the write completely
2910 * covers the extent, then we don't need to split it.
2912 if ((map
->m_lblk
== ee_block
) && (allocated
<= map
->m_len
))
2915 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2918 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2919 if (map
->m_lblk
> ee_block
) {
2921 ex1
->ee_len
= cpu_to_le16(map
->m_lblk
- ee_block
);
2922 ext4_ext_mark_uninitialized(ex1
);
2926 * for sanity, update the length of the ex2 extent before
2927 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2928 * overlap of blocks.
2930 if (!ex1
&& allocated
> map
->m_len
)
2931 ex2
->ee_len
= cpu_to_le16(map
->m_len
);
2932 /* ex3: to ee_block + ee_len : uninitialised */
2933 if (allocated
> map
->m_len
) {
2934 unsigned int newdepth
;
2936 ex3
->ee_block
= cpu_to_le32(map
->m_lblk
+ map
->m_len
);
2937 ext4_ext_store_pblock(ex3
, newblock
+ map
->m_len
);
2938 ex3
->ee_len
= cpu_to_le16(allocated
- map
->m_len
);
2939 ext4_ext_mark_uninitialized(ex3
);
2940 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, flags
);
2941 if (err
== -ENOSPC
&& may_zeroout
) {
2942 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2944 goto fix_extent_len
;
2945 /* update the extent length and mark as initialized */
2946 ex
->ee_block
= orig_ex
.ee_block
;
2947 ex
->ee_len
= orig_ex
.ee_len
;
2948 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
2949 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2950 /* zeroed the full extent */
2951 /* blocks available from map->m_lblk */
2955 goto fix_extent_len
;
2957 * The depth, and hence eh & ex might change
2958 * as part of the insert above.
2960 newdepth
= ext_depth(inode
);
2962 * update the extent length after successful insert of the
2965 ee_len
-= ext4_ext_get_actual_len(ex3
);
2966 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2967 may_zeroout
= ee_block
+ ee_len
<= eof_block
;
2970 ext4_ext_drop_refs(path
);
2971 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, path
);
2973 err
= PTR_ERR(path
);
2976 ex
= path
[depth
].p_ext
;
2980 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2984 allocated
= map
->m_len
;
2987 * If there was a change of depth as part of the
2988 * insertion of ex3 above, we need to update the length
2989 * of the ex1 extent again here
2991 if (ex1
&& ex1
!= ex
) {
2993 ex1
->ee_len
= cpu_to_le16(map
->m_lblk
- ee_block
);
2994 ext4_ext_mark_uninitialized(ex1
);
2998 * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written
2999 * using direct I/O, uninitialised still.
3001 ex2
->ee_block
= cpu_to_le32(map
->m_lblk
);
3002 ext4_ext_store_pblock(ex2
, newblock
);
3003 ex2
->ee_len
= cpu_to_le16(allocated
);
3004 ext4_ext_mark_uninitialized(ex2
);
3007 /* Mark modified extent as dirty */
3008 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3009 ext_debug("out here\n");
3012 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3013 if (err
== -ENOSPC
&& may_zeroout
) {
3014 err
= ext4_ext_zeroout(inode
, &orig_ex
);
3016 goto fix_extent_len
;
3017 /* update the extent length and mark as initialized */
3018 ex
->ee_block
= orig_ex
.ee_block
;
3019 ex
->ee_len
= orig_ex
.ee_len
;
3020 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
3021 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3022 /* zero out the first half */
3025 goto fix_extent_len
;
3027 ext4_ext_show_leaf(inode
, path
);
3028 return err
? err
: allocated
;
3031 ex
->ee_block
= orig_ex
.ee_block
;
3032 ex
->ee_len
= orig_ex
.ee_len
;
3033 ext4_ext_store_pblock(ex
, ext4_ext_pblock(&orig_ex
));
3034 ext4_ext_mark_uninitialized(ex
);
3035 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3038 static int ext4_convert_unwritten_extents_endio(handle_t
*handle
,
3039 struct inode
*inode
,
3040 struct ext4_ext_path
*path
)
3042 struct ext4_extent
*ex
;
3043 struct ext4_extent_header
*eh
;
3048 depth
= ext_depth(inode
);
3049 eh
= path
[depth
].p_hdr
;
3050 ex
= path
[depth
].p_ext
;
3052 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3055 /* first mark the extent as initialized */
3056 ext4_ext_mark_initialized(ex
);
3059 * We have to see if it can be merged with the extent
3062 if (ex
> EXT_FIRST_EXTENT(eh
)) {
3064 * To merge left, pass "ex - 1" to try_to_merge(),
3065 * since it merges towards right _only_.
3067 ret
= ext4_ext_try_to_merge(inode
, path
, ex
- 1);
3069 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
3072 depth
= ext_depth(inode
);
3077 * Try to Merge towards right.
3079 ret
= ext4_ext_try_to_merge(inode
, path
, ex
);
3081 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
3084 depth
= ext_depth(inode
);
3086 /* Mark modified extent as dirty */
3087 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3089 ext4_ext_show_leaf(inode
, path
);
3093 static void unmap_underlying_metadata_blocks(struct block_device
*bdev
,
3094 sector_t block
, int count
)
3097 for (i
= 0; i
< count
; i
++)
3098 unmap_underlying_metadata(bdev
, block
+ i
);
3102 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3104 static int check_eofblocks_fl(handle_t
*handle
, struct inode
*inode
,
3106 struct ext4_ext_path
*path
,
3110 struct ext4_extent_header
*eh
;
3111 struct ext4_extent
*ex
, *last_ex
;
3113 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))
3116 depth
= ext_depth(inode
);
3117 eh
= path
[depth
].p_hdr
;
3118 ex
= path
[depth
].p_ext
;
3120 if (unlikely(!eh
->eh_entries
)) {
3121 EXT4_ERROR_INODE(inode
, "eh->eh_entries == 0 and "
3122 "EOFBLOCKS_FL set");
3125 last_ex
= EXT_LAST_EXTENT(eh
);
3127 * We should clear the EOFBLOCKS_FL flag if we are writing the
3128 * last block in the last extent in the file. We test this by
3129 * first checking to see if the caller to
3130 * ext4_ext_get_blocks() was interested in the last block (or
3131 * a block beyond the last block) in the current extent. If
3132 * this turns out to be false, we can bail out from this
3133 * function immediately.
3135 if (lblk
+ len
< le32_to_cpu(last_ex
->ee_block
) +
3136 ext4_ext_get_actual_len(last_ex
))
3139 * If the caller does appear to be planning to write at or
3140 * beyond the end of the current extent, we then test to see
3141 * if the current extent is the last extent in the file, by
3142 * checking to make sure it was reached via the rightmost node
3143 * at each level of the tree.
3145 for (i
= depth
-1; i
>= 0; i
--)
3146 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3148 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3149 return ext4_mark_inode_dirty(handle
, inode
);
3153 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3154 struct ext4_map_blocks
*map
,
3155 struct ext4_ext_path
*path
, int flags
,
3156 unsigned int allocated
, ext4_fsblk_t newblock
)
3160 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3162 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3163 "block %llu, max_blocks %u, flags %d, allocated %u",
3164 inode
->i_ino
, (unsigned long long)map
->m_lblk
, map
->m_len
,
3166 ext4_ext_show_leaf(inode
, path
);
3168 /* get_block() before submit the IO, split the extent */
3169 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3170 ret
= ext4_split_unwritten_extents(handle
, inode
, map
,
3173 * Flag the inode(non aio case) or end_io struct (aio case)
3174 * that this IO needs to convertion to written when IO is
3178 io
->flag
= EXT4_IO_END_UNWRITTEN
;
3180 ext4_set_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3181 if (ext4_should_dioread_nolock(inode
))
3182 map
->m_flags
|= EXT4_MAP_UNINIT
;
3185 /* IO end_io complete, convert the filled extent to written */
3186 if ((flags
& EXT4_GET_BLOCKS_CONVERT
)) {
3187 ret
= ext4_convert_unwritten_extents_endio(handle
, inode
,
3190 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3191 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
3197 /* buffered IO case */
3199 * repeat fallocate creation request
3200 * we already have an unwritten extent
3202 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3205 /* buffered READ or buffered write_begin() lookup */
3206 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3208 * We have blocks reserved already. We
3209 * return allocated blocks so that delalloc
3210 * won't do block reservation for us. But
3211 * the buffer head will be unmapped so that
3212 * a read from the block returns 0s.
3214 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
3218 /* buffered write, writepage time, convert*/
3219 ret
= ext4_ext_convert_to_initialized(handle
, inode
, map
, path
);
3221 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3222 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
,
3234 map
->m_flags
|= EXT4_MAP_NEW
;
3236 * if we allocated more blocks than requested
3237 * we need to make sure we unmap the extra block
3238 * allocated. The actual needed block will get
3239 * unmapped later when we find the buffer_head marked
3242 if (allocated
> map
->m_len
) {
3243 unmap_underlying_metadata_blocks(inode
->i_sb
->s_bdev
,
3244 newblock
+ map
->m_len
,
3245 allocated
- map
->m_len
);
3246 allocated
= map
->m_len
;
3250 * If we have done fallocate with the offset that is already
3251 * delayed allocated, we would have block reservation
3252 * and quota reservation done in the delayed write path.
3253 * But fallocate would have already updated quota and block
3254 * count for this offset. So cancel these reservation
3256 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3257 ext4_da_update_reserve_space(inode
, allocated
, 0);
3260 map
->m_flags
|= EXT4_MAP_MAPPED
;
3262 if (allocated
> map
->m_len
)
3263 allocated
= map
->m_len
;
3264 ext4_ext_show_leaf(inode
, path
);
3265 map
->m_pblk
= newblock
;
3266 map
->m_len
= allocated
;
3269 ext4_ext_drop_refs(path
);
3272 return err
? err
: allocated
;
3276 * Block allocation/map/preallocation routine for extents based files
3279 * Need to be called with
3280 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3281 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3283 * return > 0, number of of blocks already mapped/allocated
3284 * if create == 0 and these are pre-allocated blocks
3285 * buffer head is unmapped
3286 * otherwise blocks are mapped
3288 * return = 0, if plain look up failed (blocks have not been allocated)
3289 * buffer head is unmapped
3291 * return < 0, error case.
3293 int ext4_ext_map_blocks(handle_t
*handle
, struct inode
*inode
,
3294 struct ext4_map_blocks
*map
, int flags
)
3296 struct ext4_ext_path
*path
= NULL
;
3297 struct ext4_extent_header
*eh
;
3298 struct ext4_extent newex
, *ex
;
3299 ext4_fsblk_t newblock
;
3300 int err
= 0, depth
, ret
;
3301 unsigned int allocated
= 0;
3302 struct ext4_allocation_request ar
;
3303 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3305 ext_debug("blocks %u/%u requested for inode %lu\n",
3306 map
->m_lblk
, map
->m_len
, inode
->i_ino
);
3308 /* check in cache */
3309 if (ext4_ext_in_cache(inode
, map
->m_lblk
, &newex
)) {
3310 if (!newex
.ee_start_lo
&& !newex
.ee_start_hi
) {
3311 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3313 * block isn't allocated yet and
3314 * user doesn't want to allocate it
3318 /* we should allocate requested block */
3320 /* block is already allocated */
3321 newblock
= map
->m_lblk
3322 - le32_to_cpu(newex
.ee_block
)
3323 + ext4_ext_pblock(&newex
);
3324 /* number of remaining blocks in the extent */
3325 allocated
= ext4_ext_get_actual_len(&newex
) -
3326 (map
->m_lblk
- le32_to_cpu(newex
.ee_block
));
3331 /* find extent for this block */
3332 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, NULL
);
3334 err
= PTR_ERR(path
);
3339 depth
= ext_depth(inode
);
3342 * consistent leaf must not be empty;
3343 * this situation is possible, though, _during_ tree modification;
3344 * this is why assert can't be put in ext4_ext_find_extent()
3346 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
3347 EXT4_ERROR_INODE(inode
, "bad extent address "
3348 "lblock: %lu, depth: %d pblock %lld",
3349 (unsigned long) map
->m_lblk
, depth
,
3350 path
[depth
].p_block
);
3354 eh
= path
[depth
].p_hdr
;
3356 ex
= path
[depth
].p_ext
;
3358 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3359 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3360 unsigned short ee_len
;
3363 * Uninitialized extents are treated as holes, except that
3364 * we split out initialized portions during a write.
3366 ee_len
= ext4_ext_get_actual_len(ex
);
3367 /* if found extent covers block, simply return it */
3368 if (in_range(map
->m_lblk
, ee_block
, ee_len
)) {
3369 newblock
= map
->m_lblk
- ee_block
+ ee_start
;
3370 /* number of remaining blocks in the extent */
3371 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3372 ext_debug("%u fit into %u:%d -> %llu\n", map
->m_lblk
,
3373 ee_block
, ee_len
, newblock
);
3375 /* Do not put uninitialized extent in the cache */
3376 if (!ext4_ext_is_uninitialized(ex
)) {
3377 ext4_ext_put_in_cache(inode
, ee_block
,
3381 ret
= ext4_ext_handle_uninitialized_extents(handle
,
3382 inode
, map
, path
, flags
, allocated
,
3389 * requested block isn't allocated yet;
3390 * we couldn't try to create block if create flag is zero
3392 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3394 * put just found gap into cache to speed up
3395 * subsequent requests
3397 ext4_ext_put_gap_in_cache(inode
, path
, map
->m_lblk
);
3401 * Okay, we need to do block allocation.
3404 /* find neighbour allocated blocks */
3405 ar
.lleft
= map
->m_lblk
;
3406 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
3409 ar
.lright
= map
->m_lblk
;
3410 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
3415 * See if request is beyond maximum number of blocks we can have in
3416 * a single extent. For an initialized extent this limit is
3417 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3418 * EXT_UNINIT_MAX_LEN.
3420 if (map
->m_len
> EXT_INIT_MAX_LEN
&&
3421 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3422 map
->m_len
= EXT_INIT_MAX_LEN
;
3423 else if (map
->m_len
> EXT_UNINIT_MAX_LEN
&&
3424 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3425 map
->m_len
= EXT_UNINIT_MAX_LEN
;
3427 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3428 newex
.ee_block
= cpu_to_le32(map
->m_lblk
);
3429 newex
.ee_len
= cpu_to_le16(map
->m_len
);
3430 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
3432 allocated
= ext4_ext_get_actual_len(&newex
);
3434 allocated
= map
->m_len
;
3436 /* allocate new block */
3438 ar
.goal
= ext4_ext_find_goal(inode
, path
, map
->m_lblk
);
3439 ar
.logical
= map
->m_lblk
;
3441 if (S_ISREG(inode
->i_mode
))
3442 ar
.flags
= EXT4_MB_HINT_DATA
;
3444 /* disable in-core preallocation for non-regular files */
3446 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
3449 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3450 ar
.goal
, newblock
, allocated
);
3452 /* try to insert new extent into found leaf and return */
3453 ext4_ext_store_pblock(&newex
, newblock
);
3454 newex
.ee_len
= cpu_to_le16(ar
.len
);
3455 /* Mark uninitialized */
3456 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
3457 ext4_ext_mark_uninitialized(&newex
);
3459 * io_end structure was created for every IO write to an
3460 * uninitialized extent. To avoid unecessary conversion,
3461 * here we flag the IO that really needs the conversion.
3462 * For non asycn direct IO case, flag the inode state
3463 * that we need to perform convertion when IO is done.
3465 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3467 io
->flag
= EXT4_IO_END_UNWRITTEN
;
3469 ext4_set_inode_state(inode
,
3470 EXT4_STATE_DIO_UNWRITTEN
);
3472 if (ext4_should_dioread_nolock(inode
))
3473 map
->m_flags
|= EXT4_MAP_UNINIT
;
3476 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
, ar
.len
);
3480 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3482 /* free data blocks we just allocated */
3483 /* not a good idea to call discard here directly,
3484 * but otherwise we'd need to call it every free() */
3485 ext4_discard_preallocations(inode
);
3486 ext4_free_blocks(handle
, inode
, 0, ext4_ext_pblock(&newex
),
3487 ext4_ext_get_actual_len(&newex
), 0);
3491 /* previous routine could use block we allocated */
3492 newblock
= ext4_ext_pblock(&newex
);
3493 allocated
= ext4_ext_get_actual_len(&newex
);
3494 if (allocated
> map
->m_len
)
3495 allocated
= map
->m_len
;
3496 map
->m_flags
|= EXT4_MAP_NEW
;
3499 * Update reserved blocks/metadata blocks after successful
3500 * block allocation which had been deferred till now.
3502 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
)
3503 ext4_da_update_reserve_space(inode
, allocated
, 1);
3506 * Cache the extent and update transaction to commit on fdatasync only
3507 * when it is _not_ an uninitialized extent.
3509 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
3510 ext4_ext_put_in_cache(inode
, map
->m_lblk
, allocated
, newblock
);
3511 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3513 ext4_update_inode_fsync_trans(handle
, inode
, 0);
3515 if (allocated
> map
->m_len
)
3516 allocated
= map
->m_len
;
3517 ext4_ext_show_leaf(inode
, path
);
3518 map
->m_flags
|= EXT4_MAP_MAPPED
;
3519 map
->m_pblk
= newblock
;
3520 map
->m_len
= allocated
;
3523 ext4_ext_drop_refs(path
);
3526 return err
? err
: allocated
;
3529 void ext4_ext_truncate(struct inode
*inode
)
3531 struct address_space
*mapping
= inode
->i_mapping
;
3532 struct super_block
*sb
= inode
->i_sb
;
3533 ext4_lblk_t last_block
;
3538 * finish any pending end_io work so we won't run the risk of
3539 * converting any truncated blocks to initialized later
3541 ext4_flush_completed_IO(inode
);
3544 * probably first extent we're gonna free will be last in block
3546 err
= ext4_writepage_trans_blocks(inode
);
3547 handle
= ext4_journal_start(inode
, err
);
3551 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3552 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3554 if (ext4_orphan_add(handle
, inode
))
3557 down_write(&EXT4_I(inode
)->i_data_sem
);
3558 ext4_ext_invalidate_cache(inode
);
3560 ext4_discard_preallocations(inode
);
3563 * TODO: optimization is possible here.
3564 * Probably we need not scan at all,
3565 * because page truncation is enough.
3568 /* we have to know where to truncate from in crash case */
3569 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3570 ext4_mark_inode_dirty(handle
, inode
);
3572 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3573 >> EXT4_BLOCK_SIZE_BITS(sb
);
3574 err
= ext4_ext_remove_space(inode
, last_block
);
3576 /* In a multi-transaction truncate, we only make the final
3577 * transaction synchronous.
3580 ext4_handle_sync(handle
);
3583 up_write(&EXT4_I(inode
)->i_data_sem
);
3585 * If this was a simple ftruncate() and the file will remain alive,
3586 * then we need to clear up the orphan record which we created above.
3587 * However, if this was a real unlink then we were called by
3588 * ext4_delete_inode(), and we allow that function to clean up the
3589 * orphan info for us.
3592 ext4_orphan_del(handle
, inode
);
3594 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3595 ext4_mark_inode_dirty(handle
, inode
);
3596 ext4_journal_stop(handle
);
3599 static void ext4_falloc_update_inode(struct inode
*inode
,
3600 int mode
, loff_t new_size
, int update_ctime
)
3602 struct timespec now
;
3605 now
= current_fs_time(inode
->i_sb
);
3606 if (!timespec_equal(&inode
->i_ctime
, &now
))
3607 inode
->i_ctime
= now
;
3610 * Update only when preallocation was requested beyond
3613 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3614 if (new_size
> i_size_read(inode
))
3615 i_size_write(inode
, new_size
);
3616 if (new_size
> EXT4_I(inode
)->i_disksize
)
3617 ext4_update_i_disksize(inode
, new_size
);
3620 * Mark that we allocate beyond EOF so the subsequent truncate
3621 * can proceed even if the new size is the same as i_size.
3623 if (new_size
> i_size_read(inode
))
3624 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3630 * preallocate space for a file. This implements ext4's fallocate file
3631 * operation, which gets called from sys_fallocate system call.
3632 * For block-mapped files, posix_fallocate should fall back to the method
3633 * of writing zeroes to the required new blocks (the same behavior which is
3634 * expected for file systems which do not support fallocate() system call).
3636 long ext4_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
3638 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
3641 unsigned int max_blocks
;
3645 struct ext4_map_blocks map
;
3646 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3648 /* We only support the FALLOC_FL_KEEP_SIZE mode */
3649 if (mode
& ~FALLOC_FL_KEEP_SIZE
)
3653 * currently supporting (pre)allocate mode for extent-based
3656 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
3659 map
.m_lblk
= offset
>> blkbits
;
3661 * We can't just convert len to max_blocks because
3662 * If blocksize = 4096 offset = 3072 and len = 2048
3664 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3667 * credits to insert 1 extent into extent tree
3669 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3670 mutex_lock(&inode
->i_mutex
);
3671 ret
= inode_newsize_ok(inode
, (len
+ offset
));
3673 mutex_unlock(&inode
->i_mutex
);
3677 while (ret
>= 0 && ret
< max_blocks
) {
3678 map
.m_lblk
= map
.m_lblk
+ ret
;
3679 map
.m_len
= max_blocks
= max_blocks
- ret
;
3680 handle
= ext4_journal_start(inode
, credits
);
3681 if (IS_ERR(handle
)) {
3682 ret
= PTR_ERR(handle
);
3685 ret
= ext4_map_blocks(handle
, inode
, &map
,
3686 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
);
3690 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
3691 "returned error inode#%lu, block=%u, "
3692 "max_blocks=%u", __func__
,
3693 inode
->i_ino
, map
.m_lblk
, max_blocks
);
3695 ext4_mark_inode_dirty(handle
, inode
);
3696 ret2
= ext4_journal_stop(handle
);
3699 if ((map
.m_lblk
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3700 blkbits
) >> blkbits
))
3701 new_size
= offset
+ len
;
3703 new_size
= (map
.m_lblk
+ ret
) << blkbits
;
3705 ext4_falloc_update_inode(inode
, mode
, new_size
,
3706 (map
.m_flags
& EXT4_MAP_NEW
));
3707 ext4_mark_inode_dirty(handle
, inode
);
3708 ret2
= ext4_journal_stop(handle
);
3712 if (ret
== -ENOSPC
&&
3713 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3717 mutex_unlock(&inode
->i_mutex
);
3718 return ret
> 0 ? ret2
: ret
;
3722 * This function convert a range of blocks to written extents
3723 * The caller of this function will pass the start offset and the size.
3724 * all unwritten extents within this range will be converted to
3727 * This function is called from the direct IO end io call back
3728 * function, to convert the fallocated extents after IO is completed.
3729 * Returns 0 on success.
3731 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
3735 unsigned int max_blocks
;
3738 struct ext4_map_blocks map
;
3739 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3741 map
.m_lblk
= offset
>> blkbits
;
3743 * We can't just convert len to max_blocks because
3744 * If blocksize = 4096 offset = 3072 and len = 2048
3746 max_blocks
= ((EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
) -
3749 * credits to insert 1 extent into extent tree
3751 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3752 while (ret
>= 0 && ret
< max_blocks
) {
3754 map
.m_len
= (max_blocks
-= ret
);
3755 handle
= ext4_journal_start(inode
, credits
);
3756 if (IS_ERR(handle
)) {
3757 ret
= PTR_ERR(handle
);
3760 ret
= ext4_map_blocks(handle
, inode
, &map
,
3761 EXT4_GET_BLOCKS_IO_CONVERT_EXT
);
3764 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
3765 "returned error inode#%lu, block=%u, "
3766 "max_blocks=%u", __func__
,
3767 inode
->i_ino
, map
.m_lblk
, map
.m_len
);
3769 ext4_mark_inode_dirty(handle
, inode
);
3770 ret2
= ext4_journal_stop(handle
);
3771 if (ret
<= 0 || ret2
)
3774 return ret
> 0 ? ret2
: ret
;
3777 * Callback function called for each extent to gather FIEMAP information.
3779 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3780 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3783 struct fiemap_extent_info
*fieinfo
= data
;
3784 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3791 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3793 if (newex
->ec_start
== 0) {
3796 struct buffer_head
*bh
= NULL
;
3798 offset
= logical
>> PAGE_SHIFT
;
3799 page
= find_get_page(inode
->i_mapping
, offset
);
3800 if (!page
|| !page_has_buffers(page
))
3801 return EXT_CONTINUE
;
3803 bh
= page_buffers(page
);
3806 return EXT_CONTINUE
;
3808 if (buffer_delay(bh
)) {
3809 flags
|= FIEMAP_EXTENT_DELALLOC
;
3810 page_cache_release(page
);
3812 page_cache_release(page
);
3813 return EXT_CONTINUE
;
3817 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3818 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3820 if (ex
&& ext4_ext_is_uninitialized(ex
))
3821 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3824 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3826 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3827 * this also indicates no more allocated blocks.
3829 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3831 if (ext4_ext_next_allocated_block(path
) == EXT_MAX_BLOCK
||
3832 newex
->ec_block
+ newex
->ec_len
- 1 == EXT_MAX_BLOCK
) {
3833 loff_t size
= i_size_read(inode
);
3834 loff_t bs
= EXT4_BLOCK_SIZE(inode
->i_sb
);
3836 flags
|= FIEMAP_EXTENT_LAST
;
3837 if ((flags
& FIEMAP_EXTENT_DELALLOC
) &&
3838 logical
+length
> size
)
3839 length
= (size
- logical
+ bs
- 1) & ~(bs
-1);
3842 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3849 return EXT_CONTINUE
;
3852 /* fiemap flags we can handle specified here */
3853 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3855 static int ext4_xattr_fiemap(struct inode
*inode
,
3856 struct fiemap_extent_info
*fieinfo
)
3860 __u32 flags
= FIEMAP_EXTENT_LAST
;
3861 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3865 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
3866 struct ext4_iloc iloc
;
3867 int offset
; /* offset of xattr in inode */
3869 error
= ext4_get_inode_loc(inode
, &iloc
);
3872 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3873 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3874 EXT4_I(inode
)->i_extra_isize
;
3876 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3877 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3879 } else { /* external block */
3880 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3881 length
= inode
->i_sb
->s_blocksize
;
3885 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3887 return (error
< 0 ? error
: 0);
3890 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3891 __u64 start
, __u64 len
)
3893 ext4_lblk_t start_blk
;
3896 /* fallback to generic here if not in extents fmt */
3897 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
3898 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3901 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3904 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3905 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3907 ext4_lblk_t len_blks
;
3910 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3911 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
3912 if (last_blk
>= EXT_MAX_BLOCK
)
3913 last_blk
= EXT_MAX_BLOCK
-1;
3914 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
3917 * Walk the extent tree gathering extent information.
3918 * ext4_ext_fiemap_cb will push extents back to user.
3920 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3921 ext4_ext_fiemap_cb
, fieinfo
);