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
33 #include <linux/time.h>
34 #include <linux/jbd2.h>
35 #include <linux/highuid.h>
36 #include <linux/pagemap.h>
37 #include <linux/quotaops.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include <linux/falloc.h>
41 #include <asm/uaccess.h>
42 #include <linux/fiemap.h>
43 #include "ext4_jbd2.h"
45 #include <trace/events/ext4.h>
47 static int ext4_split_extent(handle_t
*handle
,
49 struct ext4_ext_path
*path
,
50 struct ext4_map_blocks
*map
,
54 static int ext4_ext_truncate_extend_restart(handle_t
*handle
,
60 if (!ext4_handle_valid(handle
))
62 if (handle
->h_buffer_credits
> needed
)
64 err
= ext4_journal_extend(handle
, needed
);
67 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
79 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
80 struct ext4_ext_path
*path
)
83 /* path points to block */
84 return ext4_journal_get_write_access(handle
, path
->p_bh
);
86 /* path points to leaf/index in inode body */
87 /* we use in-core data, no need to protect them */
97 #define ext4_ext_dirty(handle, inode, path) \
98 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
99 static int __ext4_ext_dirty(const char *where
, unsigned int line
,
100 handle_t
*handle
, struct inode
*inode
,
101 struct ext4_ext_path
*path
)
105 /* path points to block */
106 err
= __ext4_handle_dirty_metadata(where
, line
, handle
,
109 /* path points to leaf/index in inode body */
110 err
= ext4_mark_inode_dirty(handle
, inode
);
115 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
116 struct ext4_ext_path
*path
,
120 int depth
= path
->p_depth
;
121 struct ext4_extent
*ex
;
124 * Try to predict block placement assuming that we are
125 * filling in a file which will eventually be
126 * non-sparse --- i.e., in the case of libbfd writing
127 * an ELF object sections out-of-order but in a way
128 * the eventually results in a contiguous object or
129 * executable file, or some database extending a table
130 * space file. However, this is actually somewhat
131 * non-ideal if we are writing a sparse file such as
132 * qemu or KVM writing a raw image file that is going
133 * to stay fairly sparse, since it will end up
134 * fragmenting the file system's free space. Maybe we
135 * should have some hueristics or some way to allow
136 * userspace to pass a hint to file system,
137 * especially if the latter case turns out to be
140 ex
= path
[depth
].p_ext
;
142 ext4_fsblk_t ext_pblk
= ext4_ext_pblock(ex
);
143 ext4_lblk_t ext_block
= le32_to_cpu(ex
->ee_block
);
145 if (block
> ext_block
)
146 return ext_pblk
+ (block
- ext_block
);
148 return ext_pblk
- (ext_block
- block
);
151 /* it looks like index is empty;
152 * try to find starting block from index itself */
153 if (path
[depth
].p_bh
)
154 return path
[depth
].p_bh
->b_blocknr
;
157 /* OK. use inode's group */
158 return ext4_inode_to_goal_block(inode
);
162 * Allocation for a meta data block
165 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
166 struct ext4_ext_path
*path
,
167 struct ext4_extent
*ex
, int *err
, unsigned int flags
)
169 ext4_fsblk_t goal
, newblock
;
171 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
172 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, flags
,
177 static inline int ext4_ext_space_block(struct inode
*inode
, int check
)
181 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
182 / sizeof(struct ext4_extent
);
183 #ifdef AGGRESSIVE_TEST
184 if (!check
&& size
> 6)
190 static inline int ext4_ext_space_block_idx(struct inode
*inode
, int check
)
194 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
195 / sizeof(struct ext4_extent_idx
);
196 #ifdef AGGRESSIVE_TEST
197 if (!check
&& size
> 5)
203 static inline int ext4_ext_space_root(struct inode
*inode
, int check
)
207 size
= sizeof(EXT4_I(inode
)->i_data
);
208 size
-= sizeof(struct ext4_extent_header
);
209 size
/= sizeof(struct ext4_extent
);
210 #ifdef AGGRESSIVE_TEST
211 if (!check
&& size
> 3)
217 static inline int ext4_ext_space_root_idx(struct inode
*inode
, int check
)
221 size
= sizeof(EXT4_I(inode
)->i_data
);
222 size
-= sizeof(struct ext4_extent_header
);
223 size
/= sizeof(struct ext4_extent_idx
);
224 #ifdef AGGRESSIVE_TEST
225 if (!check
&& size
> 4)
232 * Calculate the number of metadata blocks needed
233 * to allocate @blocks
234 * Worse case is one block per extent
236 int ext4_ext_calc_metadata_amount(struct inode
*inode
, ext4_lblk_t lblock
)
238 struct ext4_inode_info
*ei
= EXT4_I(inode
);
241 idxs
= ((inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
242 / sizeof(struct ext4_extent_idx
));
245 * If the new delayed allocation block is contiguous with the
246 * previous da block, it can share index blocks with the
247 * previous block, so we only need to allocate a new index
248 * block every idxs leaf blocks. At ldxs**2 blocks, we need
249 * an additional index block, and at ldxs**3 blocks, yet
250 * another index blocks.
252 if (ei
->i_da_metadata_calc_len
&&
253 ei
->i_da_metadata_calc_last_lblock
+1 == lblock
) {
256 if ((ei
->i_da_metadata_calc_len
% idxs
) == 0)
258 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
)) == 0)
260 if ((ei
->i_da_metadata_calc_len
% (idxs
*idxs
*idxs
)) == 0) {
262 ei
->i_da_metadata_calc_len
= 0;
264 ei
->i_da_metadata_calc_len
++;
265 ei
->i_da_metadata_calc_last_lblock
++;
270 * In the worst case we need a new set of index blocks at
271 * every level of the inode's extent tree.
273 ei
->i_da_metadata_calc_len
= 1;
274 ei
->i_da_metadata_calc_last_lblock
= lblock
;
275 return ext_depth(inode
) + 1;
279 ext4_ext_max_entries(struct inode
*inode
, int depth
)
283 if (depth
== ext_depth(inode
)) {
285 max
= ext4_ext_space_root(inode
, 1);
287 max
= ext4_ext_space_root_idx(inode
, 1);
290 max
= ext4_ext_space_block(inode
, 1);
292 max
= ext4_ext_space_block_idx(inode
, 1);
298 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
300 ext4_fsblk_t block
= ext4_ext_pblock(ext
);
301 int len
= ext4_ext_get_actual_len(ext
);
305 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
308 static int ext4_valid_extent_idx(struct inode
*inode
,
309 struct ext4_extent_idx
*ext_idx
)
311 ext4_fsblk_t block
= ext4_idx_pblock(ext_idx
);
313 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
316 static int ext4_valid_extent_entries(struct inode
*inode
,
317 struct ext4_extent_header
*eh
,
320 unsigned short entries
;
321 if (eh
->eh_entries
== 0)
324 entries
= le16_to_cpu(eh
->eh_entries
);
328 struct ext4_extent
*ext
= EXT_FIRST_EXTENT(eh
);
330 if (!ext4_valid_extent(inode
, ext
))
336 struct ext4_extent_idx
*ext_idx
= EXT_FIRST_INDEX(eh
);
338 if (!ext4_valid_extent_idx(inode
, ext_idx
))
347 static int __ext4_ext_check(const char *function
, unsigned int line
,
348 struct inode
*inode
, struct ext4_extent_header
*eh
,
351 const char *error_msg
;
354 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
355 error_msg
= "invalid magic";
358 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
359 error_msg
= "unexpected eh_depth";
362 if (unlikely(eh
->eh_max
== 0)) {
363 error_msg
= "invalid eh_max";
366 max
= ext4_ext_max_entries(inode
, depth
);
367 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
368 error_msg
= "too large eh_max";
371 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
372 error_msg
= "invalid eh_entries";
375 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
376 error_msg
= "invalid extent entries";
382 ext4_error_inode(inode
, function
, line
, 0,
383 "bad header/extent: %s - magic %x, "
384 "entries %u, max %u(%u), depth %u(%u)",
385 error_msg
, le16_to_cpu(eh
->eh_magic
),
386 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
387 max
, le16_to_cpu(eh
->eh_depth
), depth
);
392 #define ext4_ext_check(inode, eh, depth) \
393 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
395 int ext4_ext_check_inode(struct inode
*inode
)
397 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
401 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
403 int k
, l
= path
->p_depth
;
406 for (k
= 0; k
<= l
; k
++, path
++) {
408 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
409 ext4_idx_pblock(path
->p_idx
));
410 } else if (path
->p_ext
) {
411 ext_debug(" %d:[%d]%d:%llu ",
412 le32_to_cpu(path
->p_ext
->ee_block
),
413 ext4_ext_is_uninitialized(path
->p_ext
),
414 ext4_ext_get_actual_len(path
->p_ext
),
415 ext4_ext_pblock(path
->p_ext
));
422 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
424 int depth
= ext_depth(inode
);
425 struct ext4_extent_header
*eh
;
426 struct ext4_extent
*ex
;
432 eh
= path
[depth
].p_hdr
;
433 ex
= EXT_FIRST_EXTENT(eh
);
435 ext_debug("Displaying leaf extents for inode %lu\n", inode
->i_ino
);
437 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
438 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex
->ee_block
),
439 ext4_ext_is_uninitialized(ex
),
440 ext4_ext_get_actual_len(ex
), ext4_ext_pblock(ex
));
445 static void ext4_ext_show_move(struct inode
*inode
, struct ext4_ext_path
*path
,
446 ext4_fsblk_t newblock
, int level
)
448 int depth
= ext_depth(inode
);
449 struct ext4_extent
*ex
;
451 if (depth
!= level
) {
452 struct ext4_extent_idx
*idx
;
453 idx
= path
[level
].p_idx
;
454 while (idx
<= EXT_MAX_INDEX(path
[level
].p_hdr
)) {
455 ext_debug("%d: move %d:%llu in new index %llu\n", level
,
456 le32_to_cpu(idx
->ei_block
),
457 ext4_idx_pblock(idx
),
465 ex
= path
[depth
].p_ext
;
466 while (ex
<= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
467 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
468 le32_to_cpu(ex
->ee_block
),
470 ext4_ext_is_uninitialized(ex
),
471 ext4_ext_get_actual_len(ex
),
478 #define ext4_ext_show_path(inode, path)
479 #define ext4_ext_show_leaf(inode, path)
480 #define ext4_ext_show_move(inode, path, newblock, level)
483 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
485 int depth
= path
->p_depth
;
488 for (i
= 0; i
<= depth
; i
++, path
++)
496 * ext4_ext_binsearch_idx:
497 * binary search for the closest index of the given block
498 * the header must be checked before calling this
501 ext4_ext_binsearch_idx(struct inode
*inode
,
502 struct ext4_ext_path
*path
, ext4_lblk_t block
)
504 struct ext4_extent_header
*eh
= path
->p_hdr
;
505 struct ext4_extent_idx
*r
, *l
, *m
;
508 ext_debug("binsearch for %u(idx): ", block
);
510 l
= EXT_FIRST_INDEX(eh
) + 1;
511 r
= EXT_LAST_INDEX(eh
);
514 if (block
< le32_to_cpu(m
->ei_block
))
518 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
519 m
, le32_to_cpu(m
->ei_block
),
520 r
, le32_to_cpu(r
->ei_block
));
524 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
525 ext4_idx_pblock(path
->p_idx
));
527 #ifdef CHECK_BINSEARCH
529 struct ext4_extent_idx
*chix
, *ix
;
532 chix
= ix
= EXT_FIRST_INDEX(eh
);
533 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
535 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
536 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
538 ix
, EXT_FIRST_INDEX(eh
));
539 printk(KERN_DEBUG
"%u <= %u\n",
540 le32_to_cpu(ix
->ei_block
),
541 le32_to_cpu(ix
[-1].ei_block
));
543 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
544 <= le32_to_cpu(ix
[-1].ei_block
));
545 if (block
< le32_to_cpu(ix
->ei_block
))
549 BUG_ON(chix
!= path
->p_idx
);
556 * ext4_ext_binsearch:
557 * binary search for closest extent of the given block
558 * the header must be checked before calling this
561 ext4_ext_binsearch(struct inode
*inode
,
562 struct ext4_ext_path
*path
, ext4_lblk_t block
)
564 struct ext4_extent_header
*eh
= path
->p_hdr
;
565 struct ext4_extent
*r
, *l
, *m
;
567 if (eh
->eh_entries
== 0) {
569 * this leaf is empty:
570 * we get such a leaf in split/add case
575 ext_debug("binsearch for %u: ", block
);
577 l
= EXT_FIRST_EXTENT(eh
) + 1;
578 r
= EXT_LAST_EXTENT(eh
);
582 if (block
< le32_to_cpu(m
->ee_block
))
586 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
587 m
, le32_to_cpu(m
->ee_block
),
588 r
, le32_to_cpu(r
->ee_block
));
592 ext_debug(" -> %d:%llu:[%d]%d ",
593 le32_to_cpu(path
->p_ext
->ee_block
),
594 ext4_ext_pblock(path
->p_ext
),
595 ext4_ext_is_uninitialized(path
->p_ext
),
596 ext4_ext_get_actual_len(path
->p_ext
));
598 #ifdef CHECK_BINSEARCH
600 struct ext4_extent
*chex
, *ex
;
603 chex
= ex
= EXT_FIRST_EXTENT(eh
);
604 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
605 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
606 <= le32_to_cpu(ex
[-1].ee_block
));
607 if (block
< le32_to_cpu(ex
->ee_block
))
611 BUG_ON(chex
!= path
->p_ext
);
617 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
619 struct ext4_extent_header
*eh
;
621 eh
= ext_inode_hdr(inode
);
624 eh
->eh_magic
= EXT4_EXT_MAGIC
;
625 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
, 0));
626 ext4_mark_inode_dirty(handle
, inode
);
627 ext4_ext_invalidate_cache(inode
);
631 struct ext4_ext_path
*
632 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
633 struct ext4_ext_path
*path
)
635 struct ext4_extent_header
*eh
;
636 struct buffer_head
*bh
;
637 short int depth
, i
, ppos
= 0, alloc
= 0;
639 eh
= ext_inode_hdr(inode
);
640 depth
= ext_depth(inode
);
642 /* account possible depth increase */
644 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
647 return ERR_PTR(-ENOMEM
);
654 /* walk through the tree */
656 int need_to_validate
= 0;
658 ext_debug("depth %d: num %d, max %d\n",
659 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
661 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
662 path
[ppos
].p_block
= ext4_idx_pblock(path
[ppos
].p_idx
);
663 path
[ppos
].p_depth
= i
;
664 path
[ppos
].p_ext
= NULL
;
666 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
669 if (!bh_uptodate_or_lock(bh
)) {
670 trace_ext4_ext_load_extent(inode
, block
,
672 if (bh_submit_read(bh
) < 0) {
676 /* validate the extent entries */
677 need_to_validate
= 1;
679 eh
= ext_block_hdr(bh
);
681 if (unlikely(ppos
> depth
)) {
683 EXT4_ERROR_INODE(inode
,
684 "ppos %d > depth %d", ppos
, depth
);
687 path
[ppos
].p_bh
= bh
;
688 path
[ppos
].p_hdr
= eh
;
691 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
695 path
[ppos
].p_depth
= i
;
696 path
[ppos
].p_ext
= NULL
;
697 path
[ppos
].p_idx
= NULL
;
700 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
701 /* if not an empty leaf */
702 if (path
[ppos
].p_ext
)
703 path
[ppos
].p_block
= ext4_ext_pblock(path
[ppos
].p_ext
);
705 ext4_ext_show_path(inode
, path
);
710 ext4_ext_drop_refs(path
);
713 return ERR_PTR(-EIO
);
717 * ext4_ext_insert_index:
718 * insert new index [@logical;@ptr] into the block at @curp;
719 * check where to insert: before @curp or after @curp
721 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
722 struct ext4_ext_path
*curp
,
723 int logical
, ext4_fsblk_t ptr
)
725 struct ext4_extent_idx
*ix
;
728 err
= ext4_ext_get_access(handle
, inode
, curp
);
732 if (unlikely(logical
== le32_to_cpu(curp
->p_idx
->ei_block
))) {
733 EXT4_ERROR_INODE(inode
,
734 "logical %d == ei_block %d!",
735 logical
, le32_to_cpu(curp
->p_idx
->ei_block
));
739 if (unlikely(le16_to_cpu(curp
->p_hdr
->eh_entries
)
740 >= le16_to_cpu(curp
->p_hdr
->eh_max
))) {
741 EXT4_ERROR_INODE(inode
,
742 "eh_entries %d >= eh_max %d!",
743 le16_to_cpu(curp
->p_hdr
->eh_entries
),
744 le16_to_cpu(curp
->p_hdr
->eh_max
));
748 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
750 ext_debug("insert new index %d after: %llu\n", logical
, ptr
);
751 ix
= curp
->p_idx
+ 1;
754 ext_debug("insert new index %d before: %llu\n", logical
, ptr
);
758 len
= EXT_LAST_INDEX(curp
->p_hdr
) - ix
+ 1;
761 ext_debug("insert new index %d: "
762 "move %d indices from 0x%p to 0x%p\n",
763 logical
, len
, ix
, ix
+ 1);
764 memmove(ix
+ 1, ix
, len
* sizeof(struct ext4_extent_idx
));
767 if (unlikely(ix
> EXT_MAX_INDEX(curp
->p_hdr
))) {
768 EXT4_ERROR_INODE(inode
, "ix > EXT_MAX_INDEX!");
772 ix
->ei_block
= cpu_to_le32(logical
);
773 ext4_idx_store_pblock(ix
, ptr
);
774 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
776 if (unlikely(ix
> EXT_LAST_INDEX(curp
->p_hdr
))) {
777 EXT4_ERROR_INODE(inode
, "ix > EXT_LAST_INDEX!");
781 err
= ext4_ext_dirty(handle
, inode
, curp
);
782 ext4_std_error(inode
->i_sb
, err
);
789 * inserts new subtree into the path, using free index entry
791 * - allocates all needed blocks (new leaf and all intermediate index blocks)
792 * - makes decision where to split
793 * - moves remaining extents and index entries (right to the split point)
794 * into the newly allocated blocks
795 * - initializes subtree
797 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
799 struct ext4_ext_path
*path
,
800 struct ext4_extent
*newext
, int at
)
802 struct buffer_head
*bh
= NULL
;
803 int depth
= ext_depth(inode
);
804 struct ext4_extent_header
*neh
;
805 struct ext4_extent_idx
*fidx
;
807 ext4_fsblk_t newblock
, oldblock
;
809 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
812 /* make decision: where to split? */
813 /* FIXME: now decision is simplest: at current extent */
815 /* if current leaf will be split, then we should use
816 * border from split point */
817 if (unlikely(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
))) {
818 EXT4_ERROR_INODE(inode
, "p_ext > EXT_MAX_EXTENT!");
821 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
822 border
= path
[depth
].p_ext
[1].ee_block
;
823 ext_debug("leaf will be split."
824 " next leaf starts at %d\n",
825 le32_to_cpu(border
));
827 border
= newext
->ee_block
;
828 ext_debug("leaf will be added."
829 " next leaf starts at %d\n",
830 le32_to_cpu(border
));
834 * If error occurs, then we break processing
835 * and mark filesystem read-only. index won't
836 * be inserted and tree will be in consistent
837 * state. Next mount will repair buffers too.
841 * Get array to track all allocated blocks.
842 * We need this to handle errors and free blocks
845 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
849 /* allocate all needed blocks */
850 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
851 for (a
= 0; a
< depth
- at
; a
++) {
852 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
853 newext
, &err
, flags
);
856 ablocks
[a
] = newblock
;
859 /* initialize new leaf */
860 newblock
= ablocks
[--a
];
861 if (unlikely(newblock
== 0)) {
862 EXT4_ERROR_INODE(inode
, "newblock == 0!");
866 bh
= sb_getblk(inode
->i_sb
, newblock
);
873 err
= ext4_journal_get_create_access(handle
, bh
);
877 neh
= ext_block_hdr(bh
);
879 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
880 neh
->eh_magic
= EXT4_EXT_MAGIC
;
883 /* move remainder of path[depth] to the new leaf */
884 if (unlikely(path
[depth
].p_hdr
->eh_entries
!=
885 path
[depth
].p_hdr
->eh_max
)) {
886 EXT4_ERROR_INODE(inode
, "eh_entries %d != eh_max %d!",
887 path
[depth
].p_hdr
->eh_entries
,
888 path
[depth
].p_hdr
->eh_max
);
892 /* start copy from next extent */
893 m
= EXT_MAX_EXTENT(path
[depth
].p_hdr
) - path
[depth
].p_ext
++;
894 ext4_ext_show_move(inode
, path
, newblock
, depth
);
896 struct ext4_extent
*ex
;
897 ex
= EXT_FIRST_EXTENT(neh
);
898 memmove(ex
, path
[depth
].p_ext
, sizeof(struct ext4_extent
) * m
);
899 le16_add_cpu(&neh
->eh_entries
, m
);
902 set_buffer_uptodate(bh
);
905 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
911 /* correct old leaf */
913 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
916 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
917 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
923 /* create intermediate indexes */
925 if (unlikely(k
< 0)) {
926 EXT4_ERROR_INODE(inode
, "k %d < 0!", k
);
931 ext_debug("create %d intermediate indices\n", k
);
932 /* insert new index into current index block */
933 /* current depth stored in i var */
937 newblock
= ablocks
[--a
];
938 bh
= sb_getblk(inode
->i_sb
, newblock
);
945 err
= ext4_journal_get_create_access(handle
, bh
);
949 neh
= ext_block_hdr(bh
);
950 neh
->eh_entries
= cpu_to_le16(1);
951 neh
->eh_magic
= EXT4_EXT_MAGIC
;
952 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
953 neh
->eh_depth
= cpu_to_le16(depth
- i
);
954 fidx
= EXT_FIRST_INDEX(neh
);
955 fidx
->ei_block
= border
;
956 ext4_idx_store_pblock(fidx
, oldblock
);
958 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
959 i
, newblock
, le32_to_cpu(border
), oldblock
);
961 /* move remainder of path[i] to the new index block */
962 if (unlikely(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
963 EXT_LAST_INDEX(path
[i
].p_hdr
))) {
964 EXT4_ERROR_INODE(inode
,
965 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
966 le32_to_cpu(path
[i
].p_ext
->ee_block
));
970 /* start copy indexes */
971 m
= EXT_MAX_INDEX(path
[i
].p_hdr
) - path
[i
].p_idx
++;
972 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
973 EXT_MAX_INDEX(path
[i
].p_hdr
));
974 ext4_ext_show_move(inode
, path
, newblock
, i
);
976 memmove(++fidx
, path
[i
].p_idx
,
977 sizeof(struct ext4_extent_idx
) * m
);
978 le16_add_cpu(&neh
->eh_entries
, m
);
980 set_buffer_uptodate(bh
);
983 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
989 /* correct old index */
991 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
994 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
995 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
1003 /* insert new index */
1004 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
1005 le32_to_cpu(border
), newblock
);
1009 if (buffer_locked(bh
))
1015 /* free all allocated blocks in error case */
1016 for (i
= 0; i
< depth
; i
++) {
1019 ext4_free_blocks(handle
, inode
, NULL
, ablocks
[i
], 1,
1020 EXT4_FREE_BLOCKS_METADATA
);
1029 * ext4_ext_grow_indepth:
1030 * implements tree growing procedure:
1031 * - allocates new block
1032 * - moves top-level data (index block or leaf) into the new block
1033 * - initializes new top-level, creating index that points to the
1034 * just created block
1036 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1038 struct ext4_extent
*newext
)
1040 struct ext4_extent_header
*neh
;
1041 struct buffer_head
*bh
;
1042 ext4_fsblk_t newblock
;
1045 newblock
= ext4_ext_new_meta_block(handle
, inode
, NULL
,
1046 newext
, &err
, flags
);
1050 bh
= sb_getblk(inode
->i_sb
, newblock
);
1053 ext4_std_error(inode
->i_sb
, err
);
1058 err
= ext4_journal_get_create_access(handle
, bh
);
1064 /* move top-level index/leaf into new block */
1065 memmove(bh
->b_data
, EXT4_I(inode
)->i_data
,
1066 sizeof(EXT4_I(inode
)->i_data
));
1068 /* set size of new block */
1069 neh
= ext_block_hdr(bh
);
1070 /* old root could have indexes or leaves
1071 * so calculate e_max right way */
1072 if (ext_depth(inode
))
1073 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
, 0));
1075 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
, 0));
1076 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1077 set_buffer_uptodate(bh
);
1080 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1084 /* Update top-level index: num,max,pointer */
1085 neh
= ext_inode_hdr(inode
);
1086 neh
->eh_entries
= cpu_to_le16(1);
1087 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh
), newblock
);
1088 if (neh
->eh_depth
== 0) {
1089 /* Root extent block becomes index block */
1090 neh
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
, 0));
1091 EXT_FIRST_INDEX(neh
)->ei_block
=
1092 EXT_FIRST_EXTENT(neh
)->ee_block
;
1094 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1095 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1096 le32_to_cpu(EXT_FIRST_INDEX(neh
)->ei_block
),
1097 ext4_idx_pblock(EXT_FIRST_INDEX(neh
)));
1099 neh
->eh_depth
= cpu_to_le16(le16_to_cpu(neh
->eh_depth
) + 1);
1100 ext4_mark_inode_dirty(handle
, inode
);
1108 * ext4_ext_create_new_leaf:
1109 * finds empty index and adds new leaf.
1110 * if no free index is found, then it requests in-depth growing.
1112 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1114 struct ext4_ext_path
*path
,
1115 struct ext4_extent
*newext
)
1117 struct ext4_ext_path
*curp
;
1118 int depth
, i
, err
= 0;
1121 i
= depth
= ext_depth(inode
);
1123 /* walk up to the tree and look for free index entry */
1124 curp
= path
+ depth
;
1125 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1130 /* we use already allocated block for index block,
1131 * so subsequent data blocks should be contiguous */
1132 if (EXT_HAS_FREE_INDEX(curp
)) {
1133 /* if we found index with free entry, then use that
1134 * entry: create all needed subtree and add new leaf */
1135 err
= ext4_ext_split(handle
, inode
, flags
, path
, newext
, i
);
1140 ext4_ext_drop_refs(path
);
1141 path
= ext4_ext_find_extent(inode
,
1142 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1145 err
= PTR_ERR(path
);
1147 /* tree is full, time to grow in depth */
1148 err
= ext4_ext_grow_indepth(handle
, inode
, flags
, newext
);
1153 ext4_ext_drop_refs(path
);
1154 path
= ext4_ext_find_extent(inode
,
1155 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1158 err
= PTR_ERR(path
);
1163 * only first (depth 0 -> 1) produces free space;
1164 * in all other cases we have to split the grown tree
1166 depth
= ext_depth(inode
);
1167 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1168 /* now we need to split */
1178 * search the closest allocated block to the left for *logical
1179 * and returns it at @logical + it's physical address at @phys
1180 * if *logical is the smallest allocated block, the function
1181 * returns 0 at @phys
1182 * return value contains 0 (success) or error code
1184 static int ext4_ext_search_left(struct inode
*inode
,
1185 struct ext4_ext_path
*path
,
1186 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1188 struct ext4_extent_idx
*ix
;
1189 struct ext4_extent
*ex
;
1192 if (unlikely(path
== NULL
)) {
1193 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1196 depth
= path
->p_depth
;
1199 if (depth
== 0 && path
->p_ext
== NULL
)
1202 /* usually extent in the path covers blocks smaller
1203 * then *logical, but it can be that extent is the
1204 * first one in the file */
1206 ex
= path
[depth
].p_ext
;
1207 ee_len
= ext4_ext_get_actual_len(ex
);
1208 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1209 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1210 EXT4_ERROR_INODE(inode
,
1211 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1212 *logical
, le32_to_cpu(ex
->ee_block
));
1215 while (--depth
>= 0) {
1216 ix
= path
[depth
].p_idx
;
1217 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1218 EXT4_ERROR_INODE(inode
,
1219 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1220 ix
!= NULL
? le32_to_cpu(ix
->ei_block
) : 0,
1221 EXT_FIRST_INDEX(path
[depth
].p_hdr
) != NULL
?
1222 le32_to_cpu(EXT_FIRST_INDEX(path
[depth
].p_hdr
)->ei_block
) : 0,
1230 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1231 EXT4_ERROR_INODE(inode
,
1232 "logical %d < ee_block %d + ee_len %d!",
1233 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1237 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1238 *phys
= ext4_ext_pblock(ex
) + ee_len
- 1;
1243 * search the closest allocated block to the right for *logical
1244 * and returns it at @logical + it's physical address at @phys
1245 * if *logical is the largest allocated block, the function
1246 * returns 0 at @phys
1247 * return value contains 0 (success) or error code
1249 static int ext4_ext_search_right(struct inode
*inode
,
1250 struct ext4_ext_path
*path
,
1251 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
,
1252 struct ext4_extent
**ret_ex
)
1254 struct buffer_head
*bh
= NULL
;
1255 struct ext4_extent_header
*eh
;
1256 struct ext4_extent_idx
*ix
;
1257 struct ext4_extent
*ex
;
1259 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1262 if (unlikely(path
== NULL
)) {
1263 EXT4_ERROR_INODE(inode
, "path == NULL *logical %d!", *logical
);
1266 depth
= path
->p_depth
;
1269 if (depth
== 0 && path
->p_ext
== NULL
)
1272 /* usually extent in the path covers blocks smaller
1273 * then *logical, but it can be that extent is the
1274 * first one in the file */
1276 ex
= path
[depth
].p_ext
;
1277 ee_len
= ext4_ext_get_actual_len(ex
);
1278 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1279 if (unlikely(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
)) {
1280 EXT4_ERROR_INODE(inode
,
1281 "first_extent(path[%d].p_hdr) != ex",
1285 while (--depth
>= 0) {
1286 ix
= path
[depth
].p_idx
;
1287 if (unlikely(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
))) {
1288 EXT4_ERROR_INODE(inode
,
1289 "ix != EXT_FIRST_INDEX *logical %d!",
1297 if (unlikely(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
))) {
1298 EXT4_ERROR_INODE(inode
,
1299 "logical %d < ee_block %d + ee_len %d!",
1300 *logical
, le32_to_cpu(ex
->ee_block
), ee_len
);
1304 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1305 /* next allocated block in this leaf */
1310 /* go up and search for index to the right */
1311 while (--depth
>= 0) {
1312 ix
= path
[depth
].p_idx
;
1313 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1317 /* we've gone up to the root and found no index to the right */
1321 /* we've found index to the right, let's
1322 * follow it and find the closest allocated
1323 * block to the right */
1325 block
= ext4_idx_pblock(ix
);
1326 while (++depth
< path
->p_depth
) {
1327 bh
= sb_bread(inode
->i_sb
, block
);
1330 eh
= ext_block_hdr(bh
);
1331 /* subtract from p_depth to get proper eh_depth */
1332 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1336 ix
= EXT_FIRST_INDEX(eh
);
1337 block
= ext4_idx_pblock(ix
);
1341 bh
= sb_bread(inode
->i_sb
, block
);
1344 eh
= ext_block_hdr(bh
);
1345 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1349 ex
= EXT_FIRST_EXTENT(eh
);
1351 *logical
= le32_to_cpu(ex
->ee_block
);
1352 *phys
= ext4_ext_pblock(ex
);
1360 * ext4_ext_next_allocated_block:
1361 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1362 * NOTE: it considers block number from index entry as
1363 * allocated block. Thus, index entries have to be consistent
1367 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1371 BUG_ON(path
== NULL
);
1372 depth
= path
->p_depth
;
1374 if (depth
== 0 && path
->p_ext
== NULL
)
1375 return EXT_MAX_BLOCKS
;
1377 while (depth
>= 0) {
1378 if (depth
== path
->p_depth
) {
1380 if (path
[depth
].p_ext
&&
1381 path
[depth
].p_ext
!=
1382 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1383 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1386 if (path
[depth
].p_idx
!=
1387 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1388 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1393 return EXT_MAX_BLOCKS
;
1397 * ext4_ext_next_leaf_block:
1398 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1400 static ext4_lblk_t
ext4_ext_next_leaf_block(struct ext4_ext_path
*path
)
1404 BUG_ON(path
== NULL
);
1405 depth
= path
->p_depth
;
1407 /* zero-tree has no leaf blocks at all */
1409 return EXT_MAX_BLOCKS
;
1411 /* go to index block */
1414 while (depth
>= 0) {
1415 if (path
[depth
].p_idx
!=
1416 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1417 return (ext4_lblk_t
)
1418 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1422 return EXT_MAX_BLOCKS
;
1426 * ext4_ext_correct_indexes:
1427 * if leaf gets modified and modified extent is first in the leaf,
1428 * then we have to correct all indexes above.
1429 * TODO: do we need to correct tree in all cases?
1431 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1432 struct ext4_ext_path
*path
)
1434 struct ext4_extent_header
*eh
;
1435 int depth
= ext_depth(inode
);
1436 struct ext4_extent
*ex
;
1440 eh
= path
[depth
].p_hdr
;
1441 ex
= path
[depth
].p_ext
;
1443 if (unlikely(ex
== NULL
|| eh
== NULL
)) {
1444 EXT4_ERROR_INODE(inode
,
1445 "ex %p == NULL or eh %p == NULL", ex
, eh
);
1450 /* there is no tree at all */
1454 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1455 /* we correct tree if first leaf got modified only */
1460 * TODO: we need correction if border is smaller than current one
1463 border
= path
[depth
].p_ext
->ee_block
;
1464 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1467 path
[k
].p_idx
->ei_block
= border
;
1468 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1473 /* change all left-side indexes */
1474 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1476 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1479 path
[k
].p_idx
->ei_block
= border
;
1480 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1489 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1490 struct ext4_extent
*ex2
)
1492 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1495 * Make sure that either both extents are uninitialized, or
1498 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1501 if (ext4_ext_is_uninitialized(ex1
))
1502 max_len
= EXT_UNINIT_MAX_LEN
;
1504 max_len
= EXT_INIT_MAX_LEN
;
1506 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1507 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1509 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1510 le32_to_cpu(ex2
->ee_block
))
1514 * To allow future support for preallocated extents to be added
1515 * as an RO_COMPAT feature, refuse to merge to extents if
1516 * this can result in the top bit of ee_len being set.
1518 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1520 #ifdef AGGRESSIVE_TEST
1521 if (ext1_ee_len
>= 4)
1525 if (ext4_ext_pblock(ex1
) + ext1_ee_len
== ext4_ext_pblock(ex2
))
1531 * This function tries to merge the "ex" extent to the next extent in the tree.
1532 * It always tries to merge towards right. If you want to merge towards
1533 * left, pass "ex - 1" as argument instead of "ex".
1534 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1535 * 1 if they got merged.
1537 static int ext4_ext_try_to_merge_right(struct inode
*inode
,
1538 struct ext4_ext_path
*path
,
1539 struct ext4_extent
*ex
)
1541 struct ext4_extent_header
*eh
;
1542 unsigned int depth
, len
;
1544 int uninitialized
= 0;
1546 depth
= ext_depth(inode
);
1547 BUG_ON(path
[depth
].p_hdr
== NULL
);
1548 eh
= path
[depth
].p_hdr
;
1550 while (ex
< EXT_LAST_EXTENT(eh
)) {
1551 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1553 /* merge with next extent! */
1554 if (ext4_ext_is_uninitialized(ex
))
1556 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1557 + ext4_ext_get_actual_len(ex
+ 1));
1559 ext4_ext_mark_uninitialized(ex
);
1561 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1562 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1563 * sizeof(struct ext4_extent
);
1564 memmove(ex
+ 1, ex
+ 2, len
);
1566 le16_add_cpu(&eh
->eh_entries
, -1);
1568 WARN_ON(eh
->eh_entries
== 0);
1569 if (!eh
->eh_entries
)
1570 EXT4_ERROR_INODE(inode
, "eh->eh_entries = 0!");
1577 * This function tries to merge the @ex extent to neighbours in the tree.
1578 * return 1 if merge left else 0.
1580 static int ext4_ext_try_to_merge(struct inode
*inode
,
1581 struct ext4_ext_path
*path
,
1582 struct ext4_extent
*ex
) {
1583 struct ext4_extent_header
*eh
;
1588 depth
= ext_depth(inode
);
1589 BUG_ON(path
[depth
].p_hdr
== NULL
);
1590 eh
= path
[depth
].p_hdr
;
1592 if (ex
> EXT_FIRST_EXTENT(eh
))
1593 merge_done
= ext4_ext_try_to_merge_right(inode
, path
, ex
- 1);
1596 ret
= ext4_ext_try_to_merge_right(inode
, path
, ex
);
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 ext4_sb_info
*sbi
,
1610 struct inode
*inode
,
1611 struct ext4_extent
*newext
,
1612 struct ext4_ext_path
*path
)
1615 unsigned int depth
, len1
;
1616 unsigned int ret
= 0;
1618 b1
= le32_to_cpu(newext
->ee_block
);
1619 len1
= ext4_ext_get_actual_len(newext
);
1620 depth
= ext_depth(inode
);
1621 if (!path
[depth
].p_ext
)
1623 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1624 b2
&= ~(sbi
->s_cluster_ratio
- 1);
1627 * get the next allocated block if the extent in the path
1628 * is before the requested block(s)
1631 b2
= ext4_ext_next_allocated_block(path
);
1632 if (b2
== EXT_MAX_BLOCKS
)
1634 b2
&= ~(sbi
->s_cluster_ratio
- 1);
1637 /* check for wrap through zero on extent logical start block*/
1638 if (b1
+ len1
< b1
) {
1639 len1
= EXT_MAX_BLOCKS
- b1
;
1640 newext
->ee_len
= cpu_to_le16(len1
);
1644 /* check for overlap */
1645 if (b1
+ len1
> b2
) {
1646 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1654 * ext4_ext_insert_extent:
1655 * tries to merge requsted extent into the existing extent or
1656 * inserts requested extent as new one into the tree,
1657 * creating new leaf in the no-space case.
1659 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1660 struct ext4_ext_path
*path
,
1661 struct ext4_extent
*newext
, int flag
)
1663 struct ext4_extent_header
*eh
;
1664 struct ext4_extent
*ex
, *fex
;
1665 struct ext4_extent
*nearex
; /* nearest extent */
1666 struct ext4_ext_path
*npath
= NULL
;
1667 int depth
, len
, err
;
1669 unsigned uninitialized
= 0;
1672 if (unlikely(ext4_ext_get_actual_len(newext
) == 0)) {
1673 EXT4_ERROR_INODE(inode
, "ext4_ext_get_actual_len(newext) == 0");
1676 depth
= ext_depth(inode
);
1677 ex
= path
[depth
].p_ext
;
1678 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1679 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1683 /* try to insert block into found extent and return */
1684 if (ex
&& !(flag
& EXT4_GET_BLOCKS_PRE_IO
)
1685 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1686 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
1687 ext4_ext_is_uninitialized(newext
),
1688 ext4_ext_get_actual_len(newext
),
1689 le32_to_cpu(ex
->ee_block
),
1690 ext4_ext_is_uninitialized(ex
),
1691 ext4_ext_get_actual_len(ex
),
1692 ext4_ext_pblock(ex
));
1693 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1698 * ext4_can_extents_be_merged should have checked that either
1699 * both extents are uninitialized, or both aren't. Thus we
1700 * need to check only one of them here.
1702 if (ext4_ext_is_uninitialized(ex
))
1704 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1705 + ext4_ext_get_actual_len(newext
));
1707 ext4_ext_mark_uninitialized(ex
);
1708 eh
= path
[depth
].p_hdr
;
1713 depth
= ext_depth(inode
);
1714 eh
= path
[depth
].p_hdr
;
1715 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1718 /* probably next leaf has space for us? */
1719 fex
= EXT_LAST_EXTENT(eh
);
1720 next
= EXT_MAX_BLOCKS
;
1721 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
))
1722 next
= ext4_ext_next_leaf_block(path
);
1723 if (next
!= EXT_MAX_BLOCKS
) {
1724 ext_debug("next leaf block - %u\n", next
);
1725 BUG_ON(npath
!= NULL
);
1726 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1728 return PTR_ERR(npath
);
1729 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1730 eh
= npath
[depth
].p_hdr
;
1731 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1732 ext_debug("next leaf isn't full(%d)\n",
1733 le16_to_cpu(eh
->eh_entries
));
1737 ext_debug("next leaf has no free space(%d,%d)\n",
1738 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1742 * There is no free space in the found leaf.
1743 * We're gonna add a new leaf in the tree.
1745 if (flag
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
)
1746 flags
= EXT4_MB_USE_ROOT_BLOCKS
;
1747 err
= ext4_ext_create_new_leaf(handle
, inode
, flags
, path
, newext
);
1750 depth
= ext_depth(inode
);
1751 eh
= path
[depth
].p_hdr
;
1754 nearex
= path
[depth
].p_ext
;
1756 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1761 /* there is no extent in this leaf, create first one */
1762 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
1763 le32_to_cpu(newext
->ee_block
),
1764 ext4_ext_pblock(newext
),
1765 ext4_ext_is_uninitialized(newext
),
1766 ext4_ext_get_actual_len(newext
));
1767 nearex
= EXT_FIRST_EXTENT(eh
);
1769 if (le32_to_cpu(newext
->ee_block
)
1770 > le32_to_cpu(nearex
->ee_block
)) {
1772 ext_debug("insert %u:%llu:[%d]%d before: "
1774 le32_to_cpu(newext
->ee_block
),
1775 ext4_ext_pblock(newext
),
1776 ext4_ext_is_uninitialized(newext
),
1777 ext4_ext_get_actual_len(newext
),
1782 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1783 ext_debug("insert %u:%llu:[%d]%d after: "
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
),
1791 len
= EXT_LAST_EXTENT(eh
) - nearex
+ 1;
1793 ext_debug("insert %u:%llu:[%d]%d: "
1794 "move %d extents from 0x%p to 0x%p\n",
1795 le32_to_cpu(newext
->ee_block
),
1796 ext4_ext_pblock(newext
),
1797 ext4_ext_is_uninitialized(newext
),
1798 ext4_ext_get_actual_len(newext
),
1799 len
, nearex
, nearex
+ 1);
1800 memmove(nearex
+ 1, nearex
,
1801 len
* sizeof(struct ext4_extent
));
1805 le16_add_cpu(&eh
->eh_entries
, 1);
1806 path
[depth
].p_ext
= nearex
;
1807 nearex
->ee_block
= newext
->ee_block
;
1808 ext4_ext_store_pblock(nearex
, ext4_ext_pblock(newext
));
1809 nearex
->ee_len
= newext
->ee_len
;
1812 /* try to merge extents to the right */
1813 if (!(flag
& EXT4_GET_BLOCKS_PRE_IO
))
1814 ext4_ext_try_to_merge(inode
, path
, nearex
);
1816 /* try to merge extents to the left */
1818 /* time to correct all indexes above */
1819 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1823 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1827 ext4_ext_drop_refs(npath
);
1830 ext4_ext_invalidate_cache(inode
);
1834 static int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1835 ext4_lblk_t num
, ext_prepare_callback func
,
1838 struct ext4_ext_path
*path
= NULL
;
1839 struct ext4_ext_cache cbex
;
1840 struct ext4_extent
*ex
;
1841 ext4_lblk_t next
, start
= 0, end
= 0;
1842 ext4_lblk_t last
= block
+ num
;
1843 int depth
, exists
, err
= 0;
1845 BUG_ON(func
== NULL
);
1846 BUG_ON(inode
== NULL
);
1848 while (block
< last
&& block
!= EXT_MAX_BLOCKS
) {
1850 /* find extent for this block */
1851 down_read(&EXT4_I(inode
)->i_data_sem
);
1852 path
= ext4_ext_find_extent(inode
, block
, path
);
1853 up_read(&EXT4_I(inode
)->i_data_sem
);
1855 err
= PTR_ERR(path
);
1860 depth
= ext_depth(inode
);
1861 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
1862 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
1866 ex
= path
[depth
].p_ext
;
1867 next
= ext4_ext_next_allocated_block(path
);
1871 /* there is no extent yet, so try to allocate
1872 * all requested space */
1875 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1876 /* need to allocate space before found extent */
1878 end
= le32_to_cpu(ex
->ee_block
);
1879 if (block
+ num
< end
)
1881 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1882 + ext4_ext_get_actual_len(ex
)) {
1883 /* need to allocate space after found extent */
1888 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1890 * some part of requested space is covered
1894 end
= le32_to_cpu(ex
->ee_block
)
1895 + ext4_ext_get_actual_len(ex
);
1896 if (block
+ num
< end
)
1902 BUG_ON(end
<= start
);
1905 cbex
.ec_block
= start
;
1906 cbex
.ec_len
= end
- start
;
1909 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1910 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1911 cbex
.ec_start
= ext4_ext_pblock(ex
);
1914 if (unlikely(cbex
.ec_len
== 0)) {
1915 EXT4_ERROR_INODE(inode
, "cbex.ec_len == 0");
1919 err
= func(inode
, next
, &cbex
, ex
, cbdata
);
1920 ext4_ext_drop_refs(path
);
1925 if (err
== EXT_REPEAT
)
1927 else if (err
== EXT_BREAK
) {
1932 if (ext_depth(inode
) != depth
) {
1933 /* depth was changed. we have to realloc path */
1938 block
= cbex
.ec_block
+ cbex
.ec_len
;
1942 ext4_ext_drop_refs(path
);
1950 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1951 __u32 len
, ext4_fsblk_t start
)
1953 struct ext4_ext_cache
*cex
;
1955 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1956 trace_ext4_ext_put_in_cache(inode
, block
, len
, start
);
1957 cex
= &EXT4_I(inode
)->i_cached_extent
;
1958 cex
->ec_block
= block
;
1960 cex
->ec_start
= start
;
1961 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1965 * ext4_ext_put_gap_in_cache:
1966 * calculate boundaries of the gap that the requested block fits into
1967 * and cache this gap
1970 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1973 int depth
= ext_depth(inode
);
1976 struct ext4_extent
*ex
;
1978 ex
= path
[depth
].p_ext
;
1980 /* there is no extent yet, so gap is [0;-] */
1982 len
= EXT_MAX_BLOCKS
;
1983 ext_debug("cache gap(whole file):");
1984 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1986 len
= le32_to_cpu(ex
->ee_block
) - block
;
1987 ext_debug("cache gap(before): %u [%u:%u]",
1989 le32_to_cpu(ex
->ee_block
),
1990 ext4_ext_get_actual_len(ex
));
1991 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1992 + ext4_ext_get_actual_len(ex
)) {
1994 lblock
= le32_to_cpu(ex
->ee_block
)
1995 + ext4_ext_get_actual_len(ex
);
1997 next
= ext4_ext_next_allocated_block(path
);
1998 ext_debug("cache gap(after): [%u:%u] %u",
1999 le32_to_cpu(ex
->ee_block
),
2000 ext4_ext_get_actual_len(ex
),
2002 BUG_ON(next
== lblock
);
2003 len
= next
- lblock
;
2009 ext_debug(" -> %u:%lu\n", lblock
, len
);
2010 ext4_ext_put_in_cache(inode
, lblock
, len
, 0);
2014 * ext4_ext_check_cache()
2015 * Checks to see if the given block is in the cache.
2016 * If it is, the cached extent is stored in the given
2017 * cache extent pointer. If the cached extent is a hole,
2018 * this routine should be used instead of
2019 * ext4_ext_in_cache if the calling function needs to
2020 * know the size of the hole.
2022 * @inode: The files inode
2023 * @block: The block to look for in the cache
2024 * @ex: Pointer where the cached extent will be stored
2025 * if it contains block
2027 * Return 0 if cache is invalid; 1 if the cache is valid
2029 static int ext4_ext_check_cache(struct inode
*inode
, ext4_lblk_t block
,
2030 struct ext4_ext_cache
*ex
){
2031 struct ext4_ext_cache
*cex
;
2032 struct ext4_sb_info
*sbi
;
2036 * We borrow i_block_reservation_lock to protect i_cached_extent
2038 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
2039 cex
= &EXT4_I(inode
)->i_cached_extent
;
2040 sbi
= EXT4_SB(inode
->i_sb
);
2042 /* has cache valid data? */
2043 if (cex
->ec_len
== 0)
2046 if (in_range(block
, cex
->ec_block
, cex
->ec_len
)) {
2047 memcpy(ex
, cex
, sizeof(struct ext4_ext_cache
));
2048 ext_debug("%u cached by %u:%u:%llu\n",
2050 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
2054 trace_ext4_ext_in_cache(inode
, block
, ret
);
2055 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
2060 * ext4_ext_in_cache()
2061 * Checks to see if the given block is in the cache.
2062 * If it is, the cached extent is stored in the given
2065 * @inode: The files inode
2066 * @block: The block to look for in the cache
2067 * @ex: Pointer where the cached extent will be stored
2068 * if it contains block
2070 * Return 0 if cache is invalid; 1 if the cache is valid
2073 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
2074 struct ext4_extent
*ex
)
2076 struct ext4_ext_cache cex
;
2079 if (ext4_ext_check_cache(inode
, block
, &cex
)) {
2080 ex
->ee_block
= cpu_to_le32(cex
.ec_block
);
2081 ext4_ext_store_pblock(ex
, cex
.ec_start
);
2082 ex
->ee_len
= cpu_to_le16(cex
.ec_len
);
2092 * removes index from the index block.
2094 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
2095 struct ext4_ext_path
*path
)
2100 /* free index block */
2102 leaf
= ext4_idx_pblock(path
->p_idx
);
2103 if (unlikely(path
->p_hdr
->eh_entries
== 0)) {
2104 EXT4_ERROR_INODE(inode
, "path->p_hdr->eh_entries == 0");
2107 err
= ext4_ext_get_access(handle
, inode
, path
);
2111 if (path
->p_idx
!= EXT_LAST_INDEX(path
->p_hdr
)) {
2112 int len
= EXT_LAST_INDEX(path
->p_hdr
) - path
->p_idx
;
2113 len
*= sizeof(struct ext4_extent_idx
);
2114 memmove(path
->p_idx
, path
->p_idx
+ 1, len
);
2117 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
2118 err
= ext4_ext_dirty(handle
, inode
, path
);
2121 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
2122 trace_ext4_ext_rm_idx(inode
, leaf
);
2124 ext4_free_blocks(handle
, inode
, NULL
, leaf
, 1,
2125 EXT4_FREE_BLOCKS_METADATA
| EXT4_FREE_BLOCKS_FORGET
);
2130 * ext4_ext_calc_credits_for_single_extent:
2131 * This routine returns max. credits that needed to insert an extent
2132 * to the extent tree.
2133 * When pass the actual path, the caller should calculate credits
2136 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
2137 struct ext4_ext_path
*path
)
2140 int depth
= ext_depth(inode
);
2143 /* probably there is space in leaf? */
2144 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
2145 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
2148 * There are some space in the leaf tree, no
2149 * need to account for leaf block credit
2151 * bitmaps and block group descriptor blocks
2152 * and other metadata blocks still need to be
2155 /* 1 bitmap, 1 block group descriptor */
2156 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
2161 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2165 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2167 * if nrblocks are fit in a single extent (chunk flag is 1), then
2168 * in the worse case, each tree level index/leaf need to be changed
2169 * if the tree split due to insert a new extent, then the old tree
2170 * index/leaf need to be updated too
2172 * If the nrblocks are discontiguous, they could cause
2173 * the whole tree split more than once, but this is really rare.
2175 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2178 int depth
= ext_depth(inode
);
2188 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2189 struct ext4_extent
*ex
,
2190 ext4_fsblk_t
*partial_cluster
,
2191 ext4_lblk_t from
, ext4_lblk_t to
)
2193 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2194 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2196 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2198 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2199 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2201 * For bigalloc file systems, we never free a partial cluster
2202 * at the beginning of the extent. Instead, we make a note
2203 * that we tried freeing the cluster, and check to see if we
2204 * need to free it on a subsequent call to ext4_remove_blocks,
2205 * or at the end of the ext4_truncate() operation.
2207 flags
|= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
;
2209 trace_ext4_remove_blocks(inode
, ex
, from
, to
, *partial_cluster
);
2211 * If we have a partial cluster, and it's different from the
2212 * cluster of the last block, we need to explicitly free the
2213 * partial cluster here.
2215 pblk
= ext4_ext_pblock(ex
) + ee_len
- 1;
2216 if (*partial_cluster
&& (EXT4_B2C(sbi
, pblk
) != *partial_cluster
)) {
2217 ext4_free_blocks(handle
, inode
, NULL
,
2218 EXT4_C2B(sbi
, *partial_cluster
),
2219 sbi
->s_cluster_ratio
, flags
);
2220 *partial_cluster
= 0;
2223 #ifdef EXTENTS_STATS
2225 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2226 spin_lock(&sbi
->s_ext_stats_lock
);
2227 sbi
->s_ext_blocks
+= ee_len
;
2228 sbi
->s_ext_extents
++;
2229 if (ee_len
< sbi
->s_ext_min
)
2230 sbi
->s_ext_min
= ee_len
;
2231 if (ee_len
> sbi
->s_ext_max
)
2232 sbi
->s_ext_max
= ee_len
;
2233 if (ext_depth(inode
) > sbi
->s_depth_max
)
2234 sbi
->s_depth_max
= ext_depth(inode
);
2235 spin_unlock(&sbi
->s_ext_stats_lock
);
2238 if (from
>= le32_to_cpu(ex
->ee_block
)
2239 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2243 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2244 pblk
= ext4_ext_pblock(ex
) + ee_len
- num
;
2245 ext_debug("free last %u blocks starting %llu\n", num
, pblk
);
2246 ext4_free_blocks(handle
, inode
, NULL
, pblk
, num
, flags
);
2248 * If the block range to be freed didn't start at the
2249 * beginning of a cluster, and we removed the entire
2250 * extent, save the partial cluster here, since we
2251 * might need to delete if we determine that the
2252 * truncate operation has removed all of the blocks in
2255 if (pblk
& (sbi
->s_cluster_ratio
- 1) &&
2257 *partial_cluster
= EXT4_B2C(sbi
, pblk
);
2259 *partial_cluster
= 0;
2260 } else if (from
== le32_to_cpu(ex
->ee_block
)
2261 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2267 start
= ext4_ext_pblock(ex
);
2269 ext_debug("free first %u blocks starting %llu\n", num
, start
);
2270 ext4_free_blocks(handle
, inode
, NULL
, start
, num
, flags
);
2273 printk(KERN_INFO
"strange request: removal(2) "
2274 "%u-%u from %u:%u\n",
2275 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2282 * ext4_ext_rm_leaf() Removes the extents associated with the
2283 * blocks appearing between "start" and "end", and splits the extents
2284 * if "start" and "end" appear in the same extent
2286 * @handle: The journal handle
2287 * @inode: The files inode
2288 * @path: The path to the leaf
2289 * @start: The first block to remove
2290 * @end: The last block to remove
2293 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2294 struct ext4_ext_path
*path
, ext4_fsblk_t
*partial_cluster
,
2295 ext4_lblk_t start
, ext4_lblk_t end
)
2297 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2298 int err
= 0, correct_index
= 0;
2299 int depth
= ext_depth(inode
), credits
;
2300 struct ext4_extent_header
*eh
;
2303 ext4_lblk_t ex_ee_block
;
2304 unsigned short ex_ee_len
;
2305 unsigned uninitialized
= 0;
2306 struct ext4_extent
*ex
;
2308 /* the header must be checked already in ext4_ext_remove_space() */
2309 ext_debug("truncate since %u in leaf\n", start
);
2310 if (!path
[depth
].p_hdr
)
2311 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2312 eh
= path
[depth
].p_hdr
;
2313 if (unlikely(path
[depth
].p_hdr
== NULL
)) {
2314 EXT4_ERROR_INODE(inode
, "path[%d].p_hdr == NULL", depth
);
2317 /* find where to start removing */
2318 ex
= EXT_LAST_EXTENT(eh
);
2320 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2321 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2323 trace_ext4_ext_rm_leaf(inode
, start
, ex
, *partial_cluster
);
2325 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2326 ex_ee_block
+ ex_ee_len
> start
) {
2328 if (ext4_ext_is_uninitialized(ex
))
2333 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block
,
2334 uninitialized
, ex_ee_len
);
2335 path
[depth
].p_ext
= ex
;
2337 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2338 b
= ex_ee_block
+ex_ee_len
- 1 < end
?
2339 ex_ee_block
+ex_ee_len
- 1 : end
;
2341 ext_debug(" border %u:%u\n", a
, b
);
2343 /* If this extent is beyond the end of the hole, skip it */
2344 if (end
<= ex_ee_block
) {
2346 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2347 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2349 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2350 EXT4_ERROR_INODE(inode
," bad truncate %u:%u\n",
2354 } else if (a
!= ex_ee_block
) {
2355 /* remove tail of the extent */
2356 num
= a
- ex_ee_block
;
2358 /* remove whole extent: excellent! */
2362 * 3 for leaf, sb, and inode plus 2 (bmap and group
2363 * descriptor) for each block group; assume two block
2364 * groups plus ex_ee_len/blocks_per_block_group for
2367 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2368 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2370 credits
+= (ext_depth(inode
)) + 1;
2372 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2374 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2378 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2382 err
= ext4_remove_blocks(handle
, inode
, ex
, partial_cluster
,
2388 /* this extent is removed; mark slot entirely unused */
2389 ext4_ext_store_pblock(ex
, 0);
2391 ex
->ee_len
= cpu_to_le16(num
);
2393 * Do not mark uninitialized if all the blocks in the
2394 * extent have been removed.
2396 if (uninitialized
&& num
)
2397 ext4_ext_mark_uninitialized(ex
);
2399 * If the extent was completely released,
2400 * we need to remove it from the leaf
2403 if (end
!= EXT_MAX_BLOCKS
- 1) {
2405 * For hole punching, we need to scoot all the
2406 * extents up when an extent is removed so that
2407 * we dont have blank extents in the middle
2409 memmove(ex
, ex
+1, (EXT_LAST_EXTENT(eh
) - ex
) *
2410 sizeof(struct ext4_extent
));
2412 /* Now get rid of the one at the end */
2413 memset(EXT_LAST_EXTENT(eh
), 0,
2414 sizeof(struct ext4_extent
));
2416 le16_add_cpu(&eh
->eh_entries
, -1);
2418 *partial_cluster
= 0;
2420 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2424 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block
, num
,
2425 ext4_ext_pblock(ex
));
2427 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2428 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2431 if (correct_index
&& eh
->eh_entries
)
2432 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2435 * If there is still a entry in the leaf node, check to see if
2436 * it references the partial cluster. This is the only place
2437 * where it could; if it doesn't, we can free the cluster.
2439 if (*partial_cluster
&& ex
>= EXT_FIRST_EXTENT(eh
) &&
2440 (EXT4_B2C(sbi
, ext4_ext_pblock(ex
) + ex_ee_len
- 1) !=
2441 *partial_cluster
)) {
2442 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2444 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2445 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2447 ext4_free_blocks(handle
, inode
, NULL
,
2448 EXT4_C2B(sbi
, *partial_cluster
),
2449 sbi
->s_cluster_ratio
, flags
);
2450 *partial_cluster
= 0;
2453 /* if this leaf is free, then we should
2454 * remove it from index block above */
2455 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2456 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2463 * ext4_ext_more_to_rm:
2464 * returns 1 if current index has to be freed (even partial)
2467 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2469 BUG_ON(path
->p_idx
== NULL
);
2471 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2475 * if truncate on deeper level happened, it wasn't partial,
2476 * so we have to consider current index for truncation
2478 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2483 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2485 struct super_block
*sb
= inode
->i_sb
;
2486 int depth
= ext_depth(inode
);
2487 struct ext4_ext_path
*path
;
2488 ext4_fsblk_t partial_cluster
= 0;
2492 ext_debug("truncate since %u\n", start
);
2494 /* probably first extent we're gonna free will be last in block */
2495 handle
= ext4_journal_start(inode
, depth
+ 1);
2497 return PTR_ERR(handle
);
2500 ext4_ext_invalidate_cache(inode
);
2502 trace_ext4_ext_remove_space(inode
, start
, depth
);
2505 * We start scanning from right side, freeing all the blocks
2506 * after i_size and walking into the tree depth-wise.
2508 depth
= ext_depth(inode
);
2509 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2511 ext4_journal_stop(handle
);
2514 path
[0].p_depth
= depth
;
2515 path
[0].p_hdr
= ext_inode_hdr(inode
);
2516 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2522 while (i
>= 0 && err
== 0) {
2524 /* this is leaf block */
2525 err
= ext4_ext_rm_leaf(handle
, inode
, path
,
2526 &partial_cluster
, start
,
2527 EXT_MAX_BLOCKS
- 1);
2528 /* root level has p_bh == NULL, brelse() eats this */
2529 brelse(path
[i
].p_bh
);
2530 path
[i
].p_bh
= NULL
;
2535 /* this is index block */
2536 if (!path
[i
].p_hdr
) {
2537 ext_debug("initialize header\n");
2538 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2541 if (!path
[i
].p_idx
) {
2542 /* this level hasn't been touched yet */
2543 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2544 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2545 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2547 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2549 /* we were already here, see at next index */
2553 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2554 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2556 if (ext4_ext_more_to_rm(path
+ i
)) {
2557 struct buffer_head
*bh
;
2558 /* go to the next level */
2559 ext_debug("move to level %d (block %llu)\n",
2560 i
+ 1, ext4_idx_pblock(path
[i
].p_idx
));
2561 memset(path
+ i
+ 1, 0, sizeof(*path
));
2562 bh
= sb_bread(sb
, ext4_idx_pblock(path
[i
].p_idx
));
2564 /* should we reset i_size? */
2568 if (WARN_ON(i
+ 1 > depth
)) {
2572 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2577 path
[i
+ 1].p_bh
= bh
;
2579 /* save actual number of indexes since this
2580 * number is changed at the next iteration */
2581 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2584 /* we finished processing this index, go up */
2585 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2586 /* index is empty, remove it;
2587 * handle must be already prepared by the
2588 * truncatei_leaf() */
2589 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2591 /* root level has p_bh == NULL, brelse() eats this */
2592 brelse(path
[i
].p_bh
);
2593 path
[i
].p_bh
= NULL
;
2595 ext_debug("return to level %d\n", i
);
2599 trace_ext4_ext_remove_space_done(inode
, start
, depth
, partial_cluster
,
2600 path
->p_hdr
->eh_entries
);
2602 /* If we still have something in the partial cluster and we have removed
2603 * even the first extent, then we should free the blocks in the partial
2604 * cluster as well. */
2605 if (partial_cluster
&& path
->p_hdr
->eh_entries
== 0) {
2606 int flags
= EXT4_FREE_BLOCKS_FORGET
;
2608 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2609 flags
|= EXT4_FREE_BLOCKS_METADATA
;
2611 ext4_free_blocks(handle
, inode
, NULL
,
2612 EXT4_C2B(EXT4_SB(sb
), partial_cluster
),
2613 EXT4_SB(sb
)->s_cluster_ratio
, flags
);
2614 partial_cluster
= 0;
2617 /* TODO: flexible tree reduction should be here */
2618 if (path
->p_hdr
->eh_entries
== 0) {
2620 * truncate to zero freed all the tree,
2621 * so we need to correct eh_depth
2623 err
= ext4_ext_get_access(handle
, inode
, path
);
2625 ext_inode_hdr(inode
)->eh_depth
= 0;
2626 ext_inode_hdr(inode
)->eh_max
=
2627 cpu_to_le16(ext4_ext_space_root(inode
, 0));
2628 err
= ext4_ext_dirty(handle
, inode
, path
);
2632 ext4_ext_drop_refs(path
);
2636 ext4_journal_stop(handle
);
2642 * called at mount time
2644 void ext4_ext_init(struct super_block
*sb
)
2647 * possible initialization would be here
2650 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2651 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2652 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2653 #ifdef AGGRESSIVE_TEST
2654 printk(", aggressive tests");
2656 #ifdef CHECK_BINSEARCH
2657 printk(", check binsearch");
2659 #ifdef EXTENTS_STATS
2664 #ifdef EXTENTS_STATS
2665 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2666 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2667 EXT4_SB(sb
)->s_ext_max
= 0;
2673 * called at umount time
2675 void ext4_ext_release(struct super_block
*sb
)
2677 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2680 #ifdef EXTENTS_STATS
2681 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2682 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2683 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2684 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2685 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2686 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2687 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2692 /* FIXME!! we need to try to merge to left or right after zero-out */
2693 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2695 ext4_fsblk_t ee_pblock
;
2696 unsigned int ee_len
;
2699 ee_len
= ext4_ext_get_actual_len(ex
);
2700 ee_pblock
= ext4_ext_pblock(ex
);
2702 ret
= sb_issue_zeroout(inode
->i_sb
, ee_pblock
, ee_len
, GFP_NOFS
);
2710 * used by extent splitting.
2712 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2714 #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2715 #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2718 * ext4_split_extent_at() splits an extent at given block.
2720 * @handle: the journal handle
2721 * @inode: the file inode
2722 * @path: the path to the extent
2723 * @split: the logical block where the extent is splitted.
2724 * @split_flags: indicates if the extent could be zeroout if split fails, and
2725 * the states(init or uninit) of new extents.
2726 * @flags: flags used to insert new extent to extent tree.
2729 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2730 * of which are deterimined by split_flag.
2732 * There are two cases:
2733 * a> the extent are splitted into two extent.
2734 * b> split is not needed, and just mark the extent.
2736 * return 0 on success.
2738 static int ext4_split_extent_at(handle_t
*handle
,
2739 struct inode
*inode
,
2740 struct ext4_ext_path
*path
,
2745 ext4_fsblk_t newblock
;
2746 ext4_lblk_t ee_block
;
2747 struct ext4_extent
*ex
, newex
, orig_ex
;
2748 struct ext4_extent
*ex2
= NULL
;
2749 unsigned int ee_len
, depth
;
2752 ext_debug("ext4_split_extents_at: inode %lu, logical"
2753 "block %llu\n", inode
->i_ino
, (unsigned long long)split
);
2755 ext4_ext_show_leaf(inode
, path
);
2757 depth
= ext_depth(inode
);
2758 ex
= path
[depth
].p_ext
;
2759 ee_block
= le32_to_cpu(ex
->ee_block
);
2760 ee_len
= ext4_ext_get_actual_len(ex
);
2761 newblock
= split
- ee_block
+ ext4_ext_pblock(ex
);
2763 BUG_ON(split
< ee_block
|| split
>= (ee_block
+ ee_len
));
2765 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2769 if (split
== ee_block
) {
2771 * case b: block @split is the block that the extent begins with
2772 * then we just change the state of the extent, and splitting
2775 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2776 ext4_ext_mark_uninitialized(ex
);
2778 ext4_ext_mark_initialized(ex
);
2780 if (!(flags
& EXT4_GET_BLOCKS_PRE_IO
))
2781 ext4_ext_try_to_merge(inode
, path
, ex
);
2783 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2788 memcpy(&orig_ex
, ex
, sizeof(orig_ex
));
2789 ex
->ee_len
= cpu_to_le16(split
- ee_block
);
2790 if (split_flag
& EXT4_EXT_MARK_UNINIT1
)
2791 ext4_ext_mark_uninitialized(ex
);
2794 * path may lead to new leaf, not to original leaf any more
2795 * after ext4_ext_insert_extent() returns,
2797 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2799 goto fix_extent_len
;
2802 ex2
->ee_block
= cpu_to_le32(split
);
2803 ex2
->ee_len
= cpu_to_le16(ee_len
- (split
- ee_block
));
2804 ext4_ext_store_pblock(ex2
, newblock
);
2805 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2806 ext4_ext_mark_uninitialized(ex2
);
2808 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
2809 if (err
== -ENOSPC
&& (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
2810 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2812 goto fix_extent_len
;
2813 /* update the extent length and mark as initialized */
2814 ex
->ee_len
= cpu_to_le16(ee_len
);
2815 ext4_ext_try_to_merge(inode
, path
, ex
);
2816 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2819 goto fix_extent_len
;
2822 ext4_ext_show_leaf(inode
, path
);
2826 ex
->ee_len
= orig_ex
.ee_len
;
2827 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2832 * ext4_split_extents() splits an extent and mark extent which is covered
2833 * by @map as split_flags indicates
2835 * It may result in splitting the extent into multiple extents (upto three)
2836 * There are three possibilities:
2837 * a> There is no split required
2838 * b> Splits in two extents: Split is happening at either end of the extent
2839 * c> Splits in three extents: Somone is splitting in middle of the extent
2842 static int ext4_split_extent(handle_t
*handle
,
2843 struct inode
*inode
,
2844 struct ext4_ext_path
*path
,
2845 struct ext4_map_blocks
*map
,
2849 ext4_lblk_t ee_block
;
2850 struct ext4_extent
*ex
;
2851 unsigned int ee_len
, depth
;
2854 int split_flag1
, flags1
;
2856 depth
= ext_depth(inode
);
2857 ex
= path
[depth
].p_ext
;
2858 ee_block
= le32_to_cpu(ex
->ee_block
);
2859 ee_len
= ext4_ext_get_actual_len(ex
);
2860 uninitialized
= ext4_ext_is_uninitialized(ex
);
2862 if (map
->m_lblk
+ map
->m_len
< ee_block
+ ee_len
) {
2863 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
?
2864 EXT4_EXT_MAY_ZEROOUT
: 0;
2865 flags1
= flags
| EXT4_GET_BLOCKS_PRE_IO
;
2867 split_flag1
|= EXT4_EXT_MARK_UNINIT1
|
2868 EXT4_EXT_MARK_UNINIT2
;
2869 err
= ext4_split_extent_at(handle
, inode
, path
,
2870 map
->m_lblk
+ map
->m_len
, split_flag1
, flags1
);
2875 ext4_ext_drop_refs(path
);
2876 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, path
);
2878 return PTR_ERR(path
);
2880 if (map
->m_lblk
>= ee_block
) {
2881 split_flag1
= split_flag
& EXT4_EXT_MAY_ZEROOUT
?
2882 EXT4_EXT_MAY_ZEROOUT
: 0;
2884 split_flag1
|= EXT4_EXT_MARK_UNINIT1
;
2885 if (split_flag
& EXT4_EXT_MARK_UNINIT2
)
2886 split_flag1
|= EXT4_EXT_MARK_UNINIT2
;
2887 err
= ext4_split_extent_at(handle
, inode
, path
,
2888 map
->m_lblk
, split_flag1
, flags
);
2893 ext4_ext_show_leaf(inode
, path
);
2895 return err
? err
: map
->m_len
;
2898 #define EXT4_EXT_ZERO_LEN 7
2900 * This function is called by ext4_ext_map_blocks() if someone tries to write
2901 * to an uninitialized extent. It may result in splitting the uninitialized
2902 * extent into multiple extents (up to three - one initialized and two
2904 * There are three possibilities:
2905 * a> There is no split required: Entire extent should be initialized
2906 * b> Splits in two extents: Write is happening at either end of the extent
2907 * c> Splits in three extents: Somone is writing in middle of the extent
2910 * - The extent pointed to by 'path' is uninitialized.
2911 * - The extent pointed to by 'path' contains a superset
2912 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
2914 * Post-conditions on success:
2915 * - the returned value is the number of blocks beyond map->l_lblk
2916 * that are allocated and initialized.
2917 * It is guaranteed to be >= map->m_len.
2919 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2920 struct inode
*inode
,
2921 struct ext4_map_blocks
*map
,
2922 struct ext4_ext_path
*path
)
2924 struct ext4_extent_header
*eh
;
2925 struct ext4_map_blocks split_map
;
2926 struct ext4_extent zero_ex
;
2927 struct ext4_extent
*ex
;
2928 ext4_lblk_t ee_block
, eof_block
;
2929 unsigned int ee_len
, depth
;
2934 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2935 "block %llu, max_blocks %u\n", inode
->i_ino
,
2936 (unsigned long long)map
->m_lblk
, map
->m_len
);
2938 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
2939 inode
->i_sb
->s_blocksize_bits
;
2940 if (eof_block
< map
->m_lblk
+ map
->m_len
)
2941 eof_block
= map
->m_lblk
+ map
->m_len
;
2943 depth
= ext_depth(inode
);
2944 eh
= path
[depth
].p_hdr
;
2945 ex
= path
[depth
].p_ext
;
2946 ee_block
= le32_to_cpu(ex
->ee_block
);
2947 ee_len
= ext4_ext_get_actual_len(ex
);
2948 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
2950 trace_ext4_ext_convert_to_initialized_enter(inode
, map
, ex
);
2952 /* Pre-conditions */
2953 BUG_ON(!ext4_ext_is_uninitialized(ex
));
2954 BUG_ON(!in_range(map
->m_lblk
, ee_block
, ee_len
));
2957 * Attempt to transfer newly initialized blocks from the currently
2958 * uninitialized extent to its left neighbor. This is much cheaper
2959 * than an insertion followed by a merge as those involve costly
2960 * memmove() calls. This is the common case in steady state for
2961 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
2964 * Limitations of the current logic:
2965 * - L1: we only deal with writes at the start of the extent.
2966 * The approach could be extended to writes at the end
2967 * of the extent but this scenario was deemed less common.
2968 * - L2: we do not deal with writes covering the whole extent.
2969 * This would require removing the extent if the transfer
2971 * - L3: we only attempt to merge with an extent stored in the
2972 * same extent tree node.
2974 if ((map
->m_lblk
== ee_block
) && /*L1*/
2975 (map
->m_len
< ee_len
) && /*L2*/
2976 (ex
> EXT_FIRST_EXTENT(eh
))) { /*L3*/
2977 struct ext4_extent
*prev_ex
;
2978 ext4_lblk_t prev_lblk
;
2979 ext4_fsblk_t prev_pblk
, ee_pblk
;
2980 unsigned int prev_len
, write_len
;
2983 prev_lblk
= le32_to_cpu(prev_ex
->ee_block
);
2984 prev_len
= ext4_ext_get_actual_len(prev_ex
);
2985 prev_pblk
= ext4_ext_pblock(prev_ex
);
2986 ee_pblk
= ext4_ext_pblock(ex
);
2987 write_len
= map
->m_len
;
2990 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
2991 * upon those conditions:
2992 * - C1: prev_ex is initialized,
2993 * - C2: prev_ex is logically abutting ex,
2994 * - C3: prev_ex is physically abutting ex,
2995 * - C4: prev_ex can receive the additional blocks without
2996 * overflowing the (initialized) length limit.
2998 if ((!ext4_ext_is_uninitialized(prev_ex
)) && /*C1*/
2999 ((prev_lblk
+ prev_len
) == ee_block
) && /*C2*/
3000 ((prev_pblk
+ prev_len
) == ee_pblk
) && /*C3*/
3001 (prev_len
< (EXT_INIT_MAX_LEN
- write_len
))) { /*C4*/
3002 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3006 trace_ext4_ext_convert_to_initialized_fastpath(inode
,
3009 /* Shift the start of ex by 'write_len' blocks */
3010 ex
->ee_block
= cpu_to_le32(ee_block
+ write_len
);
3011 ext4_ext_store_pblock(ex
, ee_pblk
+ write_len
);
3012 ex
->ee_len
= cpu_to_le16(ee_len
- write_len
);
3013 ext4_ext_mark_uninitialized(ex
); /* Restore the flag */
3015 /* Extend prev_ex by 'write_len' blocks */
3016 prev_ex
->ee_len
= cpu_to_le16(prev_len
+ write_len
);
3018 /* Mark the block containing both extents as dirty */
3019 ext4_ext_dirty(handle
, inode
, path
+ depth
);
3021 /* Update path to point to the right extent */
3022 path
[depth
].p_ext
= prev_ex
;
3024 /* Result: number of initialized blocks past m_lblk */
3025 allocated
= write_len
;
3030 WARN_ON(map
->m_lblk
< ee_block
);
3032 * It is safe to convert extent to initialized via explicit
3033 * zeroout only if extent is fully insde i_size or new_size.
3035 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
3037 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
3038 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
&&
3039 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
3040 err
= ext4_ext_zeroout(inode
, ex
);
3044 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3047 ext4_ext_mark_initialized(ex
);
3048 ext4_ext_try_to_merge(inode
, path
, ex
);
3049 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3055 * 1. split the extent into three extents.
3056 * 2. split the extent into two extents, zeroout the first half.
3057 * 3. split the extent into two extents, zeroout the second half.
3058 * 4. split the extent into two extents with out zeroout.
3060 split_map
.m_lblk
= map
->m_lblk
;
3061 split_map
.m_len
= map
->m_len
;
3063 if (allocated
> map
->m_len
) {
3064 if (allocated
<= EXT4_EXT_ZERO_LEN
&&
3065 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
3068 cpu_to_le32(map
->m_lblk
);
3069 zero_ex
.ee_len
= cpu_to_le16(allocated
);
3070 ext4_ext_store_pblock(&zero_ex
,
3071 ext4_ext_pblock(ex
) + map
->m_lblk
- ee_block
);
3072 err
= ext4_ext_zeroout(inode
, &zero_ex
);
3075 split_map
.m_lblk
= map
->m_lblk
;
3076 split_map
.m_len
= allocated
;
3077 } else if ((map
->m_lblk
- ee_block
+ map
->m_len
<
3078 EXT4_EXT_ZERO_LEN
) &&
3079 (EXT4_EXT_MAY_ZEROOUT
& split_flag
)) {
3081 if (map
->m_lblk
!= ee_block
) {
3082 zero_ex
.ee_block
= ex
->ee_block
;
3083 zero_ex
.ee_len
= cpu_to_le16(map
->m_lblk
-
3085 ext4_ext_store_pblock(&zero_ex
,
3086 ext4_ext_pblock(ex
));
3087 err
= ext4_ext_zeroout(inode
, &zero_ex
);
3092 split_map
.m_lblk
= ee_block
;
3093 split_map
.m_len
= map
->m_lblk
- ee_block
+ map
->m_len
;
3094 allocated
= map
->m_len
;
3098 allocated
= ext4_split_extent(handle
, inode
, path
,
3099 &split_map
, split_flag
, 0);
3104 return err
? err
: allocated
;
3108 * This function is called by ext4_ext_map_blocks() from
3109 * ext4_get_blocks_dio_write() when DIO to write
3110 * to an uninitialized extent.
3112 * Writing to an uninitialized extent may result in splitting the uninitialized
3113 * extent into multiple /initialized uninitialized extents (up to three)
3114 * There are three possibilities:
3115 * a> There is no split required: Entire extent should be uninitialized
3116 * b> Splits in two extents: Write is happening at either end of the extent
3117 * c> Splits in three extents: Somone is writing in middle of the extent
3119 * One of more index blocks maybe needed if the extent tree grow after
3120 * the uninitialized extent split. To prevent ENOSPC occur at the IO
3121 * complete, we need to split the uninitialized extent before DIO submit
3122 * the IO. The uninitialized extent called at this time will be split
3123 * into three uninitialized extent(at most). After IO complete, the part
3124 * being filled will be convert to initialized by the end_io callback function
3125 * via ext4_convert_unwritten_extents().
3127 * Returns the size of uninitialized extent to be written on success.
3129 static int ext4_split_unwritten_extents(handle_t
*handle
,
3130 struct inode
*inode
,
3131 struct ext4_map_blocks
*map
,
3132 struct ext4_ext_path
*path
,
3135 ext4_lblk_t eof_block
;
3136 ext4_lblk_t ee_block
;
3137 struct ext4_extent
*ex
;
3138 unsigned int ee_len
;
3139 int split_flag
= 0, depth
;
3141 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3142 "block %llu, max_blocks %u\n", inode
->i_ino
,
3143 (unsigned long long)map
->m_lblk
, map
->m_len
);
3145 eof_block
= (inode
->i_size
+ inode
->i_sb
->s_blocksize
- 1) >>
3146 inode
->i_sb
->s_blocksize_bits
;
3147 if (eof_block
< map
->m_lblk
+ map
->m_len
)
3148 eof_block
= map
->m_lblk
+ map
->m_len
;
3150 * It is safe to convert extent to initialized via explicit
3151 * zeroout only if extent is fully insde i_size or new_size.
3153 depth
= ext_depth(inode
);
3154 ex
= path
[depth
].p_ext
;
3155 ee_block
= le32_to_cpu(ex
->ee_block
);
3156 ee_len
= ext4_ext_get_actual_len(ex
);
3158 split_flag
|= ee_block
+ ee_len
<= eof_block
? EXT4_EXT_MAY_ZEROOUT
: 0;
3159 split_flag
|= EXT4_EXT_MARK_UNINIT2
;
3161 flags
|= EXT4_GET_BLOCKS_PRE_IO
;
3162 return ext4_split_extent(handle
, inode
, path
, map
, split_flag
, flags
);
3165 static int ext4_convert_unwritten_extents_endio(handle_t
*handle
,
3166 struct inode
*inode
,
3167 struct ext4_ext_path
*path
)
3169 struct ext4_extent
*ex
;
3173 depth
= ext_depth(inode
);
3174 ex
= path
[depth
].p_ext
;
3176 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3177 "block %llu, max_blocks %u\n", inode
->i_ino
,
3178 (unsigned long long)le32_to_cpu(ex
->ee_block
),
3179 ext4_ext_get_actual_len(ex
));
3181 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
3184 /* first mark the extent as initialized */
3185 ext4_ext_mark_initialized(ex
);
3187 /* note: ext4_ext_correct_indexes() isn't needed here because
3188 * borders are not changed
3190 ext4_ext_try_to_merge(inode
, path
, ex
);
3192 /* Mark modified extent as dirty */
3193 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3195 ext4_ext_show_leaf(inode
, path
);
3199 static void unmap_underlying_metadata_blocks(struct block_device
*bdev
,
3200 sector_t block
, int count
)
3203 for (i
= 0; i
< count
; i
++)
3204 unmap_underlying_metadata(bdev
, block
+ i
);
3208 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3210 static int check_eofblocks_fl(handle_t
*handle
, struct inode
*inode
,
3212 struct ext4_ext_path
*path
,
3216 struct ext4_extent_header
*eh
;
3217 struct ext4_extent
*last_ex
;
3219 if (!ext4_test_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
))
3222 depth
= ext_depth(inode
);
3223 eh
= path
[depth
].p_hdr
;
3225 if (unlikely(!eh
->eh_entries
)) {
3226 EXT4_ERROR_INODE(inode
, "eh->eh_entries == 0 and "
3227 "EOFBLOCKS_FL set");
3230 last_ex
= EXT_LAST_EXTENT(eh
);
3232 * We should clear the EOFBLOCKS_FL flag if we are writing the
3233 * last block in the last extent in the file. We test this by
3234 * first checking to see if the caller to
3235 * ext4_ext_get_blocks() was interested in the last block (or
3236 * a block beyond the last block) in the current extent. If
3237 * this turns out to be false, we can bail out from this
3238 * function immediately.
3240 if (lblk
+ len
< le32_to_cpu(last_ex
->ee_block
) +
3241 ext4_ext_get_actual_len(last_ex
))
3244 * If the caller does appear to be planning to write at or
3245 * beyond the end of the current extent, we then test to see
3246 * if the current extent is the last extent in the file, by
3247 * checking to make sure it was reached via the rightmost node
3248 * at each level of the tree.
3250 for (i
= depth
-1; i
>= 0; i
--)
3251 if (path
[i
].p_idx
!= EXT_LAST_INDEX(path
[i
].p_hdr
))
3253 ext4_clear_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
3254 return ext4_mark_inode_dirty(handle
, inode
);
3258 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3260 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3261 * whether there are any buffers marked for delayed allocation. It returns '1'
3262 * on the first delalloc'ed buffer head found. If no buffer head in the given
3263 * range is marked for delalloc, it returns 0.
3264 * lblk_start should always be <= lblk_end.
3265 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3266 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3267 * block sooner). This is useful when blocks are truncated sequentially from
3268 * lblk_start towards lblk_end.
3270 static int ext4_find_delalloc_range(struct inode
*inode
,
3271 ext4_lblk_t lblk_start
,
3272 ext4_lblk_t lblk_end
,
3273 int search_hint_reverse
)
3275 struct address_space
*mapping
= inode
->i_mapping
;
3276 struct buffer_head
*head
, *bh
= NULL
;
3278 ext4_lblk_t i
, pg_lblk
;
3281 if (!test_opt(inode
->i_sb
, DELALLOC
))
3284 /* reverse search wont work if fs block size is less than page size */
3285 if (inode
->i_blkbits
< PAGE_CACHE_SHIFT
)
3286 search_hint_reverse
= 0;
3288 if (search_hint_reverse
)
3293 index
= i
>> (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
3295 while ((i
>= lblk_start
) && (i
<= lblk_end
)) {
3296 page
= find_get_page(mapping
, index
);
3300 if (!page_has_buffers(page
))
3303 head
= page_buffers(page
);
3308 pg_lblk
= index
<< (PAGE_CACHE_SHIFT
-
3311 if (unlikely(pg_lblk
< lblk_start
)) {
3313 * This is possible when fs block size is less
3314 * than page size and our cluster starts/ends in
3315 * middle of the page. So we need to skip the
3316 * initial few blocks till we reach the 'lblk'
3322 /* Check if the buffer is delayed allocated and that it
3323 * is not yet mapped. (when da-buffers are mapped during
3324 * their writeout, their da_mapped bit is set.)
3326 if (buffer_delay(bh
) && !buffer_da_mapped(bh
)) {
3327 page_cache_release(page
);
3328 trace_ext4_find_delalloc_range(inode
,
3329 lblk_start
, lblk_end
,
3330 search_hint_reverse
,
3334 if (search_hint_reverse
)
3338 } while ((i
>= lblk_start
) && (i
<= lblk_end
) &&
3339 ((bh
= bh
->b_this_page
) != head
));
3342 page_cache_release(page
);
3344 * Move to next page. 'i' will be the first lblk in the next
3347 if (search_hint_reverse
)
3351 i
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
3354 trace_ext4_find_delalloc_range(inode
, lblk_start
, lblk_end
,
3355 search_hint_reverse
, 0, 0);
3359 int ext4_find_delalloc_cluster(struct inode
*inode
, ext4_lblk_t lblk
,
3360 int search_hint_reverse
)
3362 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3363 ext4_lblk_t lblk_start
, lblk_end
;
3364 lblk_start
= lblk
& (~(sbi
->s_cluster_ratio
- 1));
3365 lblk_end
= lblk_start
+ sbi
->s_cluster_ratio
- 1;
3367 return ext4_find_delalloc_range(inode
, lblk_start
, lblk_end
,
3368 search_hint_reverse
);
3372 * Determines how many complete clusters (out of those specified by the 'map')
3373 * are under delalloc and were reserved quota for.
3374 * This function is called when we are writing out the blocks that were
3375 * originally written with their allocation delayed, but then the space was
3376 * allocated using fallocate() before the delayed allocation could be resolved.
3377 * The cases to look for are:
3378 * ('=' indicated delayed allocated blocks
3379 * '-' indicates non-delayed allocated blocks)
3380 * (a) partial clusters towards beginning and/or end outside of allocated range
3381 * are not delalloc'ed.
3383 * |----c---=|====c====|====c====|===-c----|
3384 * |++++++ allocated ++++++|
3385 * ==> 4 complete clusters in above example
3387 * (b) partial cluster (outside of allocated range) towards either end is
3388 * marked for delayed allocation. In this case, we will exclude that
3391 * |----====c========|========c========|
3392 * |++++++ allocated ++++++|
3393 * ==> 1 complete clusters in above example
3396 * |================c================|
3397 * |++++++ allocated ++++++|
3398 * ==> 0 complete clusters in above example
3400 * The ext4_da_update_reserve_space will be called only if we
3401 * determine here that there were some "entire" clusters that span
3402 * this 'allocated' range.
3403 * In the non-bigalloc case, this function will just end up returning num_blks
3404 * without ever calling ext4_find_delalloc_range.
3407 get_reserved_cluster_alloc(struct inode
*inode
, ext4_lblk_t lblk_start
,
3408 unsigned int num_blks
)
3410 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3411 ext4_lblk_t alloc_cluster_start
, alloc_cluster_end
;
3412 ext4_lblk_t lblk_from
, lblk_to
, c_offset
;
3413 unsigned int allocated_clusters
= 0;
3415 alloc_cluster_start
= EXT4_B2C(sbi
, lblk_start
);
3416 alloc_cluster_end
= EXT4_B2C(sbi
, lblk_start
+ num_blks
- 1);
3418 /* max possible clusters for this allocation */
3419 allocated_clusters
= alloc_cluster_end
- alloc_cluster_start
+ 1;
3421 trace_ext4_get_reserved_cluster_alloc(inode
, lblk_start
, num_blks
);
3423 /* Check towards left side */
3424 c_offset
= lblk_start
& (sbi
->s_cluster_ratio
- 1);
3426 lblk_from
= lblk_start
& (~(sbi
->s_cluster_ratio
- 1));
3427 lblk_to
= lblk_from
+ c_offset
- 1;
3429 if (ext4_find_delalloc_range(inode
, lblk_from
, lblk_to
, 0))
3430 allocated_clusters
--;
3433 /* Now check towards right. */
3434 c_offset
= (lblk_start
+ num_blks
) & (sbi
->s_cluster_ratio
- 1);
3435 if (allocated_clusters
&& c_offset
) {
3436 lblk_from
= lblk_start
+ num_blks
;
3437 lblk_to
= lblk_from
+ (sbi
->s_cluster_ratio
- c_offset
) - 1;
3439 if (ext4_find_delalloc_range(inode
, lblk_from
, lblk_to
, 0))
3440 allocated_clusters
--;
3443 return allocated_clusters
;
3447 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3448 struct ext4_map_blocks
*map
,
3449 struct ext4_ext_path
*path
, int flags
,
3450 unsigned int allocated
, ext4_fsblk_t newblock
)
3454 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3456 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3457 "block %llu, max_blocks %u, flags %x, allocated %u\n",
3458 inode
->i_ino
, (unsigned long long)map
->m_lblk
, map
->m_len
,
3460 ext4_ext_show_leaf(inode
, path
);
3462 trace_ext4_ext_handle_uninitialized_extents(inode
, map
, allocated
,
3465 /* get_block() before submit the IO, split the extent */
3466 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
3467 ret
= ext4_split_unwritten_extents(handle
, inode
, map
,
3470 * Flag the inode(non aio case) or end_io struct (aio case)
3471 * that this IO needs to conversion to written when IO is
3475 ext4_set_io_unwritten_flag(inode
, io
);
3477 ext4_set_inode_state(inode
, EXT4_STATE_DIO_UNWRITTEN
);
3478 if (ext4_should_dioread_nolock(inode
))
3479 map
->m_flags
|= EXT4_MAP_UNINIT
;
3482 /* IO end_io complete, convert the filled extent to written */
3483 if ((flags
& EXT4_GET_BLOCKS_CONVERT
)) {
3484 ret
= ext4_convert_unwritten_extents_endio(handle
, inode
,
3487 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3488 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
3494 /* buffered IO case */
3496 * repeat fallocate creation request
3497 * we already have an unwritten extent
3499 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3502 /* buffered READ or buffered write_begin() lookup */
3503 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3505 * We have blocks reserved already. We
3506 * return allocated blocks so that delalloc
3507 * won't do block reservation for us. But
3508 * the buffer head will be unmapped so that
3509 * a read from the block returns 0s.
3511 map
->m_flags
|= EXT4_MAP_UNWRITTEN
;
3515 /* buffered write, writepage time, convert*/
3516 ret
= ext4_ext_convert_to_initialized(handle
, inode
, map
, path
);
3518 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3525 map
->m_flags
|= EXT4_MAP_NEW
;
3527 * if we allocated more blocks than requested
3528 * we need to make sure we unmap the extra block
3529 * allocated. The actual needed block will get
3530 * unmapped later when we find the buffer_head marked
3533 if (allocated
> map
->m_len
) {
3534 unmap_underlying_metadata_blocks(inode
->i_sb
->s_bdev
,
3535 newblock
+ map
->m_len
,
3536 allocated
- map
->m_len
);
3537 allocated
= map
->m_len
;
3541 * If we have done fallocate with the offset that is already
3542 * delayed allocated, we would have block reservation
3543 * and quota reservation done in the delayed write path.
3544 * But fallocate would have already updated quota and block
3545 * count for this offset. So cancel these reservation
3547 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) {
3548 unsigned int reserved_clusters
;
3549 reserved_clusters
= get_reserved_cluster_alloc(inode
,
3550 map
->m_lblk
, map
->m_len
);
3551 if (reserved_clusters
)
3552 ext4_da_update_reserve_space(inode
,
3558 map
->m_flags
|= EXT4_MAP_MAPPED
;
3559 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0) {
3560 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
, path
,
3566 if (allocated
> map
->m_len
)
3567 allocated
= map
->m_len
;
3568 ext4_ext_show_leaf(inode
, path
);
3569 map
->m_pblk
= newblock
;
3570 map
->m_len
= allocated
;
3573 ext4_ext_drop_refs(path
);
3576 return err
? err
: allocated
;
3580 * get_implied_cluster_alloc - check to see if the requested
3581 * allocation (in the map structure) overlaps with a cluster already
3582 * allocated in an extent.
3583 * @sb The filesystem superblock structure
3584 * @map The requested lblk->pblk mapping
3585 * @ex The extent structure which might contain an implied
3586 * cluster allocation
3588 * This function is called by ext4_ext_map_blocks() after we failed to
3589 * find blocks that were already in the inode's extent tree. Hence,
3590 * we know that the beginning of the requested region cannot overlap
3591 * the extent from the inode's extent tree. There are three cases we
3592 * want to catch. The first is this case:
3594 * |--- cluster # N--|
3595 * |--- extent ---| |---- requested region ---|
3598 * The second case that we need to test for is this one:
3600 * |--------- cluster # N ----------------|
3601 * |--- requested region --| |------- extent ----|
3602 * |=======================|
3604 * The third case is when the requested region lies between two extents
3605 * within the same cluster:
3606 * |------------- cluster # N-------------|
3607 * |----- ex -----| |---- ex_right ----|
3608 * |------ requested region ------|
3609 * |================|
3611 * In each of the above cases, we need to set the map->m_pblk and
3612 * map->m_len so it corresponds to the return the extent labelled as
3613 * "|====|" from cluster #N, since it is already in use for data in
3614 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3615 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3616 * as a new "allocated" block region. Otherwise, we will return 0 and
3617 * ext4_ext_map_blocks() will then allocate one or more new clusters
3618 * by calling ext4_mb_new_blocks().
3620 static int get_implied_cluster_alloc(struct super_block
*sb
,
3621 struct ext4_map_blocks
*map
,
3622 struct ext4_extent
*ex
,
3623 struct ext4_ext_path
*path
)
3625 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3626 ext4_lblk_t c_offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
-1);
3627 ext4_lblk_t ex_cluster_start
, ex_cluster_end
;
3628 ext4_lblk_t rr_cluster_start
;
3629 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3630 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3631 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
3633 /* The extent passed in that we are trying to match */
3634 ex_cluster_start
= EXT4_B2C(sbi
, ee_block
);
3635 ex_cluster_end
= EXT4_B2C(sbi
, ee_block
+ ee_len
- 1);
3637 /* The requested region passed into ext4_map_blocks() */
3638 rr_cluster_start
= EXT4_B2C(sbi
, map
->m_lblk
);
3640 if ((rr_cluster_start
== ex_cluster_end
) ||
3641 (rr_cluster_start
== ex_cluster_start
)) {
3642 if (rr_cluster_start
== ex_cluster_end
)
3643 ee_start
+= ee_len
- 1;
3644 map
->m_pblk
= (ee_start
& ~(sbi
->s_cluster_ratio
- 1)) +
3646 map
->m_len
= min(map
->m_len
,
3647 (unsigned) sbi
->s_cluster_ratio
- c_offset
);
3649 * Check for and handle this case:
3651 * |--------- cluster # N-------------|
3652 * |------- extent ----|
3653 * |--- requested region ---|
3657 if (map
->m_lblk
< ee_block
)
3658 map
->m_len
= min(map
->m_len
, ee_block
- map
->m_lblk
);
3661 * Check for the case where there is already another allocated
3662 * block to the right of 'ex' but before the end of the cluster.
3664 * |------------- cluster # N-------------|
3665 * |----- ex -----| |---- ex_right ----|
3666 * |------ requested region ------|
3667 * |================|
3669 if (map
->m_lblk
> ee_block
) {
3670 ext4_lblk_t next
= ext4_ext_next_allocated_block(path
);
3671 map
->m_len
= min(map
->m_len
, next
- map
->m_lblk
);
3674 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 1);
3678 trace_ext4_get_implied_cluster_alloc_exit(sb
, map
, 0);
3684 * Block allocation/map/preallocation routine for extents based files
3687 * Need to be called with
3688 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3689 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3691 * return > 0, number of of blocks already mapped/allocated
3692 * if create == 0 and these are pre-allocated blocks
3693 * buffer head is unmapped
3694 * otherwise blocks are mapped
3696 * return = 0, if plain look up failed (blocks have not been allocated)
3697 * buffer head is unmapped
3699 * return < 0, error case.
3701 int ext4_ext_map_blocks(handle_t
*handle
, struct inode
*inode
,
3702 struct ext4_map_blocks
*map
, int flags
)
3704 struct ext4_ext_path
*path
= NULL
;
3705 struct ext4_extent newex
, *ex
, *ex2
;
3706 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
3707 ext4_fsblk_t newblock
= 0;
3708 int free_on_err
= 0, err
= 0, depth
, ret
;
3709 unsigned int allocated
= 0, offset
= 0;
3710 unsigned int allocated_clusters
= 0;
3711 unsigned int punched_out
= 0;
3712 unsigned int result
= 0;
3713 struct ext4_allocation_request ar
;
3714 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3715 ext4_lblk_t cluster_offset
;
3717 ext_debug("blocks %u/%u requested for inode %lu\n",
3718 map
->m_lblk
, map
->m_len
, inode
->i_ino
);
3719 trace_ext4_ext_map_blocks_enter(inode
, map
->m_lblk
, map
->m_len
, flags
);
3721 /* check in cache */
3722 if (!(flags
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
) &&
3723 ext4_ext_in_cache(inode
, map
->m_lblk
, &newex
)) {
3724 if (!newex
.ee_start_lo
&& !newex
.ee_start_hi
) {
3725 if ((sbi
->s_cluster_ratio
> 1) &&
3726 ext4_find_delalloc_cluster(inode
, map
->m_lblk
, 0))
3727 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3729 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3731 * block isn't allocated yet and
3732 * user doesn't want to allocate it
3736 /* we should allocate requested block */
3738 /* block is already allocated */
3739 if (sbi
->s_cluster_ratio
> 1)
3740 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3741 newblock
= map
->m_lblk
3742 - le32_to_cpu(newex
.ee_block
)
3743 + ext4_ext_pblock(&newex
);
3744 /* number of remaining blocks in the extent */
3745 allocated
= ext4_ext_get_actual_len(&newex
) -
3746 (map
->m_lblk
- le32_to_cpu(newex
.ee_block
));
3751 /* find extent for this block */
3752 path
= ext4_ext_find_extent(inode
, map
->m_lblk
, NULL
);
3754 err
= PTR_ERR(path
);
3759 depth
= ext_depth(inode
);
3762 * consistent leaf must not be empty;
3763 * this situation is possible, though, _during_ tree modification;
3764 * this is why assert can't be put in ext4_ext_find_extent()
3766 if (unlikely(path
[depth
].p_ext
== NULL
&& depth
!= 0)) {
3767 EXT4_ERROR_INODE(inode
, "bad extent address "
3768 "lblock: %lu, depth: %d pblock %lld",
3769 (unsigned long) map
->m_lblk
, depth
,
3770 path
[depth
].p_block
);
3775 ex
= path
[depth
].p_ext
;
3777 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3778 ext4_fsblk_t ee_start
= ext4_ext_pblock(ex
);
3779 unsigned short ee_len
;
3782 * Uninitialized extents are treated as holes, except that
3783 * we split out initialized portions during a write.
3785 ee_len
= ext4_ext_get_actual_len(ex
);
3787 trace_ext4_ext_show_extent(inode
, ee_block
, ee_start
, ee_len
);
3789 /* if found extent covers block, simply return it */
3790 if (in_range(map
->m_lblk
, ee_block
, ee_len
)) {
3791 struct ext4_map_blocks punch_map
;
3792 ext4_fsblk_t partial_cluster
= 0;
3794 newblock
= map
->m_lblk
- ee_block
+ ee_start
;
3795 /* number of remaining blocks in the extent */
3796 allocated
= ee_len
- (map
->m_lblk
- ee_block
);
3797 ext_debug("%u fit into %u:%d -> %llu\n", map
->m_lblk
,
3798 ee_block
, ee_len
, newblock
);
3800 if ((flags
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
) == 0) {
3802 * Do not put uninitialized extent
3805 if (!ext4_ext_is_uninitialized(ex
)) {
3806 ext4_ext_put_in_cache(inode
, ee_block
,
3810 ret
= ext4_ext_handle_uninitialized_extents(
3811 handle
, inode
, map
, path
, flags
,
3812 allocated
, newblock
);
3817 * Punch out the map length, but only to the
3820 punched_out
= allocated
< map
->m_len
?
3821 allocated
: map
->m_len
;
3824 * Sense extents need to be converted to
3825 * uninitialized, they must fit in an
3826 * uninitialized extent
3828 if (punched_out
> EXT_UNINIT_MAX_LEN
)
3829 punched_out
= EXT_UNINIT_MAX_LEN
;
3831 punch_map
.m_lblk
= map
->m_lblk
;
3832 punch_map
.m_pblk
= newblock
;
3833 punch_map
.m_len
= punched_out
;
3834 punch_map
.m_flags
= 0;
3836 /* Check to see if the extent needs to be split */
3837 if (punch_map
.m_len
!= ee_len
||
3838 punch_map
.m_lblk
!= ee_block
) {
3840 ret
= ext4_split_extent(handle
, inode
,
3841 path
, &punch_map
, 0,
3842 EXT4_GET_BLOCKS_PUNCH_OUT_EXT
|
3843 EXT4_GET_BLOCKS_PRE_IO
);
3850 * find extent for the block at
3851 * the start of the hole
3853 ext4_ext_drop_refs(path
);
3856 path
= ext4_ext_find_extent(inode
,
3859 err
= PTR_ERR(path
);
3864 depth
= ext_depth(inode
);
3865 ex
= path
[depth
].p_ext
;
3866 ee_len
= ext4_ext_get_actual_len(ex
);
3867 ee_block
= le32_to_cpu(ex
->ee_block
);
3868 ee_start
= ext4_ext_pblock(ex
);
3872 ext4_ext_mark_uninitialized(ex
);
3874 ext4_ext_invalidate_cache(inode
);
3876 err
= ext4_ext_rm_leaf(handle
, inode
, path
,
3877 &partial_cluster
, map
->m_lblk
,
3878 map
->m_lblk
+ punched_out
);
3880 if (!err
&& path
->p_hdr
->eh_entries
== 0) {
3882 * Punch hole freed all of this sub tree,
3883 * so we need to correct eh_depth
3885 err
= ext4_ext_get_access(handle
, inode
, path
);
3887 ext_inode_hdr(inode
)->eh_depth
= 0;
3888 ext_inode_hdr(inode
)->eh_max
=
3889 cpu_to_le16(ext4_ext_space_root(
3892 err
= ext4_ext_dirty(
3893 handle
, inode
, path
);
3901 if ((sbi
->s_cluster_ratio
> 1) &&
3902 ext4_find_delalloc_cluster(inode
, map
->m_lblk
, 0))
3903 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3906 * requested block isn't allocated yet;
3907 * we couldn't try to create block if create flag is zero
3909 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3911 * put just found gap into cache to speed up
3912 * subsequent requests
3914 ext4_ext_put_gap_in_cache(inode
, path
, map
->m_lblk
);
3919 * Okay, we need to do block allocation.
3921 map
->m_flags
&= ~EXT4_MAP_FROM_CLUSTER
;
3922 newex
.ee_block
= cpu_to_le32(map
->m_lblk
);
3923 cluster_offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
-1);
3926 * If we are doing bigalloc, check to see if the extent returned
3927 * by ext4_ext_find_extent() implies a cluster we can use.
3929 if (cluster_offset
&& ex
&&
3930 get_implied_cluster_alloc(inode
->i_sb
, map
, ex
, path
)) {
3931 ar
.len
= allocated
= map
->m_len
;
3932 newblock
= map
->m_pblk
;
3933 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3934 goto got_allocated_blocks
;
3937 /* find neighbour allocated blocks */
3938 ar
.lleft
= map
->m_lblk
;
3939 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
3942 ar
.lright
= map
->m_lblk
;
3944 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
, &ex2
);
3948 /* Check if the extent after searching to the right implies a
3949 * cluster we can use. */
3950 if ((sbi
->s_cluster_ratio
> 1) && ex2
&&
3951 get_implied_cluster_alloc(inode
->i_sb
, map
, ex2
, path
)) {
3952 ar
.len
= allocated
= map
->m_len
;
3953 newblock
= map
->m_pblk
;
3954 map
->m_flags
|= EXT4_MAP_FROM_CLUSTER
;
3955 goto got_allocated_blocks
;
3959 * See if request is beyond maximum number of blocks we can have in
3960 * a single extent. For an initialized extent this limit is
3961 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3962 * EXT_UNINIT_MAX_LEN.
3964 if (map
->m_len
> EXT_INIT_MAX_LEN
&&
3965 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3966 map
->m_len
= EXT_INIT_MAX_LEN
;
3967 else if (map
->m_len
> EXT_UNINIT_MAX_LEN
&&
3968 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3969 map
->m_len
= EXT_UNINIT_MAX_LEN
;
3971 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3972 newex
.ee_len
= cpu_to_le16(map
->m_len
);
3973 err
= ext4_ext_check_overlap(sbi
, inode
, &newex
, path
);
3975 allocated
= ext4_ext_get_actual_len(&newex
);
3977 allocated
= map
->m_len
;
3979 /* allocate new block */
3981 ar
.goal
= ext4_ext_find_goal(inode
, path
, map
->m_lblk
);
3982 ar
.logical
= map
->m_lblk
;
3984 * We calculate the offset from the beginning of the cluster
3985 * for the logical block number, since when we allocate a
3986 * physical cluster, the physical block should start at the
3987 * same offset from the beginning of the cluster. This is
3988 * needed so that future calls to get_implied_cluster_alloc()
3991 offset
= map
->m_lblk
& (sbi
->s_cluster_ratio
- 1);
3992 ar
.len
= EXT4_NUM_B2C(sbi
, offset
+allocated
);
3994 ar
.logical
-= offset
;
3995 if (S_ISREG(inode
->i_mode
))
3996 ar
.flags
= EXT4_MB_HINT_DATA
;
3998 /* disable in-core preallocation for non-regular files */
4000 if (flags
& EXT4_GET_BLOCKS_NO_NORMALIZE
)
4001 ar
.flags
|= EXT4_MB_HINT_NOPREALLOC
;
4002 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
4005 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
4006 ar
.goal
, newblock
, allocated
);
4008 allocated_clusters
= ar
.len
;
4009 ar
.len
= EXT4_C2B(sbi
, ar
.len
) - offset
;
4010 if (ar
.len
> allocated
)
4013 got_allocated_blocks
:
4014 /* try to insert new extent into found leaf and return */
4015 ext4_ext_store_pblock(&newex
, newblock
+ offset
);
4016 newex
.ee_len
= cpu_to_le16(ar
.len
);
4017 /* Mark uninitialized */
4018 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
4019 ext4_ext_mark_uninitialized(&newex
);
4021 * io_end structure was created for every IO write to an
4022 * uninitialized extent. To avoid unnecessary conversion,
4023 * here we flag the IO that really needs the conversion.
4024 * For non asycn direct IO case, flag the inode state
4025 * that we need to perform conversion when IO is done.
4027 if ((flags
& EXT4_GET_BLOCKS_PRE_IO
)) {
4029 ext4_set_io_unwritten_flag(inode
, io
);
4031 ext4_set_inode_state(inode
,
4032 EXT4_STATE_DIO_UNWRITTEN
);
4034 if (ext4_should_dioread_nolock(inode
))
4035 map
->m_flags
|= EXT4_MAP_UNINIT
;
4039 if ((flags
& EXT4_GET_BLOCKS_KEEP_SIZE
) == 0)
4040 err
= check_eofblocks_fl(handle
, inode
, map
->m_lblk
,
4043 err
= ext4_ext_insert_extent(handle
, inode
, path
,
4045 if (err
&& free_on_err
) {
4046 int fb_flags
= flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
?
4047 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
: 0;
4048 /* free data blocks we just allocated */
4049 /* not a good idea to call discard here directly,
4050 * but otherwise we'd need to call it every free() */
4051 ext4_discard_preallocations(inode
);
4052 ext4_free_blocks(handle
, inode
, NULL
, ext4_ext_pblock(&newex
),
4053 ext4_ext_get_actual_len(&newex
), fb_flags
);
4057 /* previous routine could use block we allocated */
4058 newblock
= ext4_ext_pblock(&newex
);
4059 allocated
= ext4_ext_get_actual_len(&newex
);
4060 if (allocated
> map
->m_len
)
4061 allocated
= map
->m_len
;
4062 map
->m_flags
|= EXT4_MAP_NEW
;
4065 * Update reserved blocks/metadata blocks after successful
4066 * block allocation which had been deferred till now.
4068 if (flags
& EXT4_GET_BLOCKS_DELALLOC_RESERVE
) {
4069 unsigned int reserved_clusters
;
4071 * Check how many clusters we had reserved this allocated range
4073 reserved_clusters
= get_reserved_cluster_alloc(inode
,
4074 map
->m_lblk
, allocated
);
4075 if (map
->m_flags
& EXT4_MAP_FROM_CLUSTER
) {
4076 if (reserved_clusters
) {
4078 * We have clusters reserved for this range.
4079 * But since we are not doing actual allocation
4080 * and are simply using blocks from previously
4081 * allocated cluster, we should release the
4082 * reservation and not claim quota.
4084 ext4_da_update_reserve_space(inode
,
4085 reserved_clusters
, 0);
4088 BUG_ON(allocated_clusters
< reserved_clusters
);
4089 /* We will claim quota for all newly allocated blocks.*/
4090 ext4_da_update_reserve_space(inode
, allocated_clusters
,
4092 if (reserved_clusters
< allocated_clusters
) {
4093 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4094 int reservation
= allocated_clusters
-
4097 * It seems we claimed few clusters outside of
4098 * the range of this allocation. We should give
4099 * it back to the reservation pool. This can
4100 * happen in the following case:
4102 * * Suppose s_cluster_ratio is 4 (i.e., each
4103 * cluster has 4 blocks. Thus, the clusters
4104 * are [0-3],[4-7],[8-11]...
4105 * * First comes delayed allocation write for
4106 * logical blocks 10 & 11. Since there were no
4107 * previous delayed allocated blocks in the
4108 * range [8-11], we would reserve 1 cluster
4110 * * Next comes write for logical blocks 3 to 8.
4111 * In this case, we will reserve 2 clusters
4112 * (for [0-3] and [4-7]; and not for [8-11] as
4113 * that range has a delayed allocated blocks.
4114 * Thus total reserved clusters now becomes 3.
4115 * * Now, during the delayed allocation writeout
4116 * time, we will first write blocks [3-8] and
4117 * allocate 3 clusters for writing these
4118 * blocks. Also, we would claim all these
4119 * three clusters above.
4120 * * Now when we come here to writeout the
4121 * blocks [10-11], we would expect to claim
4122 * the reservation of 1 cluster we had made
4123 * (and we would claim it since there are no
4124 * more delayed allocated blocks in the range
4125 * [8-11]. But our reserved cluster count had
4126 * already gone to 0.
4128 * Thus, at the step 4 above when we determine
4129 * that there are still some unwritten delayed
4130 * allocated blocks outside of our current
4131 * block range, we should increment the
4132 * reserved clusters count so that when the
4133 * remaining blocks finally gets written, we
4136 dquot_reserve_block(inode
,
4137 EXT4_C2B(sbi
, reservation
));
4138 spin_lock(&ei
->i_block_reservation_lock
);
4139 ei
->i_reserved_data_blocks
+= reservation
;
4140 spin_unlock(&ei
->i_block_reservation_lock
);
4146 * Cache the extent and update transaction to commit on fdatasync only
4147 * when it is _not_ an uninitialized extent.
4149 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
4150 ext4_ext_put_in_cache(inode
, map
->m_lblk
, allocated
, newblock
);
4151 ext4_update_inode_fsync_trans(handle
, inode
, 1);
4153 ext4_update_inode_fsync_trans(handle
, inode
, 0);
4155 if (allocated
> map
->m_len
)
4156 allocated
= map
->m_len
;
4157 ext4_ext_show_leaf(inode
, path
);
4158 map
->m_flags
|= EXT4_MAP_MAPPED
;
4159 map
->m_pblk
= newblock
;
4160 map
->m_len
= allocated
;
4163 ext4_ext_drop_refs(path
);
4166 result
= (flags
& EXT4_GET_BLOCKS_PUNCH_OUT_EXT
) ?
4167 punched_out
: allocated
;
4169 trace_ext4_ext_map_blocks_exit(inode
, map
->m_lblk
,
4170 newblock
, map
->m_len
, err
? err
: result
);
4172 return err
? err
: result
;
4175 void ext4_ext_truncate(struct inode
*inode
)
4177 struct address_space
*mapping
= inode
->i_mapping
;
4178 struct super_block
*sb
= inode
->i_sb
;
4179 ext4_lblk_t last_block
;
4185 * finish any pending end_io work so we won't run the risk of
4186 * converting any truncated blocks to initialized later
4188 ext4_flush_completed_IO(inode
);
4191 * probably first extent we're gonna free will be last in block
4193 err
= ext4_writepage_trans_blocks(inode
);
4194 handle
= ext4_journal_start(inode
, err
);
4198 if (inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
4199 page_len
= PAGE_CACHE_SIZE
-
4200 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
4202 err
= ext4_discard_partial_page_buffers(handle
,
4203 mapping
, inode
->i_size
, page_len
, 0);
4209 if (ext4_orphan_add(handle
, inode
))
4212 down_write(&EXT4_I(inode
)->i_data_sem
);
4213 ext4_ext_invalidate_cache(inode
);
4215 ext4_discard_preallocations(inode
);
4218 * TODO: optimization is possible here.
4219 * Probably we need not scan at all,
4220 * because page truncation is enough.
4223 /* we have to know where to truncate from in crash case */
4224 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
4225 ext4_mark_inode_dirty(handle
, inode
);
4227 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
4228 >> EXT4_BLOCK_SIZE_BITS(sb
);
4229 err
= ext4_ext_remove_space(inode
, last_block
);
4231 /* In a multi-transaction truncate, we only make the final
4232 * transaction synchronous.
4235 ext4_handle_sync(handle
);
4237 up_write(&EXT4_I(inode
)->i_data_sem
);
4241 * If this was a simple ftruncate() and the file will remain alive,
4242 * then we need to clear up the orphan record which we created above.
4243 * However, if this was a real unlink then we were called by
4244 * ext4_delete_inode(), and we allow that function to clean up the
4245 * orphan info for us.
4248 ext4_orphan_del(handle
, inode
);
4250 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
4251 ext4_mark_inode_dirty(handle
, inode
);
4252 ext4_journal_stop(handle
);
4255 static void ext4_falloc_update_inode(struct inode
*inode
,
4256 int mode
, loff_t new_size
, int update_ctime
)
4258 struct timespec now
;
4261 now
= current_fs_time(inode
->i_sb
);
4262 if (!timespec_equal(&inode
->i_ctime
, &now
))
4263 inode
->i_ctime
= now
;
4266 * Update only when preallocation was requested beyond
4269 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
4270 if (new_size
> i_size_read(inode
))
4271 i_size_write(inode
, new_size
);
4272 if (new_size
> EXT4_I(inode
)->i_disksize
)
4273 ext4_update_i_disksize(inode
, new_size
);
4276 * Mark that we allocate beyond EOF so the subsequent truncate
4277 * can proceed even if the new size is the same as i_size.
4279 if (new_size
> i_size_read(inode
))
4280 ext4_set_inode_flag(inode
, EXT4_INODE_EOFBLOCKS
);
4286 * preallocate space for a file. This implements ext4's fallocate file
4287 * operation, which gets called from sys_fallocate system call.
4288 * For block-mapped files, posix_fallocate should fall back to the method
4289 * of writing zeroes to the required new blocks (the same behavior which is
4290 * expected for file systems which do not support fallocate() system call).
4292 long ext4_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
4294 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
4297 unsigned int max_blocks
;
4302 struct ext4_map_blocks map
;
4303 unsigned int credits
, blkbits
= inode
->i_blkbits
;
4306 * currently supporting (pre)allocate mode for extent-based
4309 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
4312 /* Return error if mode is not supported */
4313 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
4316 if (mode
& FALLOC_FL_PUNCH_HOLE
)
4317 return ext4_punch_hole(file
, offset
, len
);
4319 trace_ext4_fallocate_enter(inode
, offset
, len
, mode
);
4320 map
.m_lblk
= offset
>> blkbits
;
4322 * We can't just convert len to max_blocks because
4323 * If blocksize = 4096 offset = 3072 and len = 2048
4325 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
4328 * credits to insert 1 extent into extent tree
4330 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
4331 mutex_lock(&inode
->i_mutex
);
4332 ret
= inode_newsize_ok(inode
, (len
+ offset
));
4334 mutex_unlock(&inode
->i_mutex
);
4335 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
, ret
);
4338 flags
= EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
;
4339 if (mode
& FALLOC_FL_KEEP_SIZE
)
4340 flags
|= EXT4_GET_BLOCKS_KEEP_SIZE
;
4342 * Don't normalize the request if it can fit in one extent so
4343 * that it doesn't get unnecessarily split into multiple
4346 if (len
<= EXT_UNINIT_MAX_LEN
<< blkbits
)
4347 flags
|= EXT4_GET_BLOCKS_NO_NORMALIZE
;
4349 while (ret
>= 0 && ret
< max_blocks
) {
4350 map
.m_lblk
= map
.m_lblk
+ ret
;
4351 map
.m_len
= max_blocks
= max_blocks
- ret
;
4352 handle
= ext4_journal_start(inode
, credits
);
4353 if (IS_ERR(handle
)) {
4354 ret
= PTR_ERR(handle
);
4357 ret
= ext4_map_blocks(handle
, inode
, &map
, flags
);
4361 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
4362 "returned error inode#%lu, block=%u, "
4363 "max_blocks=%u", __func__
,
4364 inode
->i_ino
, map
.m_lblk
, max_blocks
);
4366 ext4_mark_inode_dirty(handle
, inode
);
4367 ret2
= ext4_journal_stop(handle
);
4370 if ((map
.m_lblk
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
4371 blkbits
) >> blkbits
))
4372 new_size
= offset
+ len
;
4374 new_size
= ((loff_t
) map
.m_lblk
+ ret
) << blkbits
;
4376 ext4_falloc_update_inode(inode
, mode
, new_size
,
4377 (map
.m_flags
& EXT4_MAP_NEW
));
4378 ext4_mark_inode_dirty(handle
, inode
);
4379 ret2
= ext4_journal_stop(handle
);
4383 if (ret
== -ENOSPC
&&
4384 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
4388 mutex_unlock(&inode
->i_mutex
);
4389 trace_ext4_fallocate_exit(inode
, offset
, max_blocks
,
4390 ret
> 0 ? ret2
: ret
);
4391 return ret
> 0 ? ret2
: ret
;
4395 * This function convert a range of blocks to written extents
4396 * The caller of this function will pass the start offset and the size.
4397 * all unwritten extents within this range will be converted to
4400 * This function is called from the direct IO end io call back
4401 * function, to convert the fallocated extents after IO is completed.
4402 * Returns 0 on success.
4404 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
4408 unsigned int max_blocks
;
4411 struct ext4_map_blocks map
;
4412 unsigned int credits
, blkbits
= inode
->i_blkbits
;
4414 map
.m_lblk
= offset
>> blkbits
;
4416 * We can't just convert len to max_blocks because
4417 * If blocksize = 4096 offset = 3072 and len = 2048
4419 max_blocks
= ((EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
) -
4422 * credits to insert 1 extent into extent tree
4424 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
4425 while (ret
>= 0 && ret
< max_blocks
) {
4427 map
.m_len
= (max_blocks
-= ret
);
4428 handle
= ext4_journal_start(inode
, credits
);
4429 if (IS_ERR(handle
)) {
4430 ret
= PTR_ERR(handle
);
4433 ret
= ext4_map_blocks(handle
, inode
, &map
,
4434 EXT4_GET_BLOCKS_IO_CONVERT_EXT
);
4437 printk(KERN_ERR
"%s: ext4_ext_map_blocks "
4438 "returned error inode#%lu, block=%u, "
4439 "max_blocks=%u", __func__
,
4440 inode
->i_ino
, map
.m_lblk
, map
.m_len
);
4442 ext4_mark_inode_dirty(handle
, inode
);
4443 ret2
= ext4_journal_stop(handle
);
4444 if (ret
<= 0 || ret2
)
4447 return ret
> 0 ? ret2
: ret
;
4451 * Callback function called for each extent to gather FIEMAP information.
4453 static int ext4_ext_fiemap_cb(struct inode
*inode
, ext4_lblk_t next
,
4454 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
4462 struct fiemap_extent_info
*fieinfo
= data
;
4463 unsigned char blksize_bits
;
4465 blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
4466 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
4468 if (newex
->ec_start
== 0) {
4470 * No extent in extent-tree contains block @newex->ec_start,
4471 * then the block may stay in 1)a hole or 2)delayed-extent.
4473 * Holes or delayed-extents are processed as follows.
4474 * 1. lookup dirty pages with specified range in pagecache.
4475 * If no page is got, then there is no delayed-extent and
4476 * return with EXT_CONTINUE.
4477 * 2. find the 1st mapped buffer,
4478 * 3. check if the mapped buffer is both in the request range
4479 * and a delayed buffer. If not, there is no delayed-extent,
4481 * 4. a delayed-extent is found, the extent will be collected.
4483 ext4_lblk_t end
= 0;
4484 pgoff_t last_offset
;
4487 pgoff_t start_index
= 0;
4488 struct page
**pages
= NULL
;
4489 struct buffer_head
*bh
= NULL
;
4490 struct buffer_head
*head
= NULL
;
4491 unsigned int nr_pages
= PAGE_SIZE
/ sizeof(struct page
*);
4493 pages
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4497 offset
= logical
>> PAGE_SHIFT
;
4499 last_offset
= offset
;
4501 ret
= find_get_pages_tag(inode
->i_mapping
, &offset
,
4502 PAGECACHE_TAG_DIRTY
, nr_pages
, pages
);
4504 if (!(flags
& FIEMAP_EXTENT_DELALLOC
)) {
4505 /* First time, try to find a mapped buffer. */
4508 for (index
= 0; index
< ret
; index
++)
4509 page_cache_release(pages
[index
]);
4512 return EXT_CONTINUE
;
4517 /* Try to find the 1st mapped buffer. */
4518 end
= ((__u64
)pages
[index
]->index
<< PAGE_SHIFT
) >>
4520 if (!page_has_buffers(pages
[index
]))
4522 head
= page_buffers(pages
[index
]);
4529 if (end
>= newex
->ec_block
+
4531 /* The buffer is out of
4532 * the request range.
4536 if (buffer_mapped(bh
) &&
4537 end
>= newex
->ec_block
) {
4538 start_index
= index
- 1;
4539 /* get the 1st mapped buffer. */
4540 goto found_mapped_buffer
;
4543 bh
= bh
->b_this_page
;
4545 } while (bh
!= head
);
4547 /* No mapped buffer in the range found in this page,
4548 * We need to look up next page.
4551 /* There is no page left, but we need to limit
4554 newex
->ec_len
= end
- newex
->ec_block
;
4559 /*Find contiguous delayed buffers. */
4560 if (ret
> 0 && pages
[0]->index
== last_offset
)
4561 head
= page_buffers(pages
[0]);
4567 found_mapped_buffer
:
4568 if (bh
!= NULL
&& buffer_delay(bh
)) {
4569 /* 1st or contiguous delayed buffer found. */
4570 if (!(flags
& FIEMAP_EXTENT_DELALLOC
)) {
4572 * 1st delayed buffer found, record
4573 * the start of extent.
4575 flags
|= FIEMAP_EXTENT_DELALLOC
;
4576 newex
->ec_block
= end
;
4577 logical
= (__u64
)end
<< blksize_bits
;
4579 /* Find contiguous delayed buffers. */
4581 if (!buffer_delay(bh
))
4582 goto found_delayed_extent
;
4583 bh
= bh
->b_this_page
;
4585 } while (bh
!= head
);
4587 for (; index
< ret
; index
++) {
4588 if (!page_has_buffers(pages
[index
])) {
4592 head
= page_buffers(pages
[index
]);
4598 if (pages
[index
]->index
!=
4599 pages
[start_index
]->index
+ index
4601 /* Blocks are not contiguous. */
4607 if (!buffer_delay(bh
))
4608 /* Delayed-extent ends. */
4609 goto found_delayed_extent
;
4610 bh
= bh
->b_this_page
;
4612 } while (bh
!= head
);
4614 } else if (!(flags
& FIEMAP_EXTENT_DELALLOC
))
4618 found_delayed_extent
:
4619 newex
->ec_len
= min(end
- newex
->ec_block
,
4620 (ext4_lblk_t
)EXT_INIT_MAX_LEN
);
4621 if (ret
== nr_pages
&& bh
!= NULL
&&
4622 newex
->ec_len
< EXT_INIT_MAX_LEN
&&
4624 /* Have not collected an extent and continue. */
4625 for (index
= 0; index
< ret
; index
++)
4626 page_cache_release(pages
[index
]);
4630 for (index
= 0; index
< ret
; index
++)
4631 page_cache_release(pages
[index
]);
4635 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
4636 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
4638 if (ex
&& ext4_ext_is_uninitialized(ex
))
4639 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
4641 if (next
== EXT_MAX_BLOCKS
)
4642 flags
|= FIEMAP_EXTENT_LAST
;
4644 ret
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
4650 return EXT_CONTINUE
;
4652 /* fiemap flags we can handle specified here */
4653 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4655 static int ext4_xattr_fiemap(struct inode
*inode
,
4656 struct fiemap_extent_info
*fieinfo
)
4660 __u32 flags
= FIEMAP_EXTENT_LAST
;
4661 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
4665 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
4666 struct ext4_iloc iloc
;
4667 int offset
; /* offset of xattr in inode */
4669 error
= ext4_get_inode_loc(inode
, &iloc
);
4672 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
4673 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
4674 EXT4_I(inode
)->i_extra_isize
;
4676 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
4677 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
4679 } else { /* external block */
4680 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
4681 length
= inode
->i_sb
->s_blocksize
;
4685 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
4687 return (error
< 0 ? error
: 0);
4691 * ext4_ext_punch_hole
4693 * Punches a hole of "length" bytes in a file starting
4696 * @inode: The inode of the file to punch a hole in
4697 * @offset: The starting byte offset of the hole
4698 * @length: The length of the hole
4700 * Returns the number of blocks removed or negative on err
4702 int ext4_ext_punch_hole(struct file
*file
, loff_t offset
, loff_t length
)
4704 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
4705 struct super_block
*sb
= inode
->i_sb
;
4706 struct ext4_ext_cache cache_ex
;
4707 ext4_lblk_t first_block
, last_block
, num_blocks
, iblock
, max_blocks
;
4708 struct address_space
*mapping
= inode
->i_mapping
;
4709 struct ext4_map_blocks map
;
4711 loff_t first_page
, last_page
, page_len
;
4712 loff_t first_page_offset
, last_page_offset
;
4713 int ret
, credits
, blocks_released
, err
= 0;
4715 /* No need to punch hole beyond i_size */
4716 if (offset
>= inode
->i_size
)
4720 * If the hole extends beyond i_size, set the hole
4721 * to end after the page that contains i_size
4723 if (offset
+ length
> inode
->i_size
) {
4724 length
= inode
->i_size
+
4725 PAGE_CACHE_SIZE
- (inode
->i_size
& (PAGE_CACHE_SIZE
- 1)) -
4729 first_block
= (offset
+ sb
->s_blocksize
- 1) >>
4730 EXT4_BLOCK_SIZE_BITS(sb
);
4731 last_block
= (offset
+ length
) >> EXT4_BLOCK_SIZE_BITS(sb
);
4733 first_page
= (offset
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
4734 last_page
= (offset
+ length
) >> PAGE_CACHE_SHIFT
;
4736 first_page_offset
= first_page
<< PAGE_CACHE_SHIFT
;
4737 last_page_offset
= last_page
<< PAGE_CACHE_SHIFT
;
4740 * Write out all dirty pages to avoid race conditions
4741 * Then release them.
4743 if (mapping
->nrpages
&& mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
)) {
4744 err
= filemap_write_and_wait_range(mapping
,
4745 offset
, offset
+ length
- 1);
4751 /* Now release the pages */
4752 if (last_page_offset
> first_page_offset
) {
4753 truncate_inode_pages_range(mapping
, first_page_offset
,
4754 last_page_offset
-1);
4757 /* finish any pending end_io work */
4758 ext4_flush_completed_IO(inode
);
4760 credits
= ext4_writepage_trans_blocks(inode
);
4761 handle
= ext4_journal_start(inode
, credits
);
4763 return PTR_ERR(handle
);
4765 err
= ext4_orphan_add(handle
, inode
);
4770 * Now we need to zero out the non-page-aligned data in the
4771 * pages at the start and tail of the hole, and unmap the buffer
4772 * heads for the block aligned regions of the page that were
4773 * completely zeroed.
4775 if (first_page
> last_page
) {
4777 * If the file space being truncated is contained within a page
4778 * just zero out and unmap the middle of that page
4780 err
= ext4_discard_partial_page_buffers(handle
,
4781 mapping
, offset
, length
, 0);
4787 * zero out and unmap the partial page that contains
4788 * the start of the hole
4790 page_len
= first_page_offset
- offset
;
4792 err
= ext4_discard_partial_page_buffers(handle
, mapping
,
4793 offset
, page_len
, 0);
4799 * zero out and unmap the partial page that contains
4800 * the end of the hole
4802 page_len
= offset
+ length
- last_page_offset
;
4804 err
= ext4_discard_partial_page_buffers(handle
, mapping
,
4805 last_page_offset
, page_len
, 0);
4813 * If i_size is contained in the last page, we need to
4814 * unmap and zero the partial page after i_size
4816 if (inode
->i_size
>> PAGE_CACHE_SHIFT
== last_page
&&
4817 inode
->i_size
% PAGE_CACHE_SIZE
!= 0) {
4819 page_len
= PAGE_CACHE_SIZE
-
4820 (inode
->i_size
& (PAGE_CACHE_SIZE
- 1));
4823 err
= ext4_discard_partial_page_buffers(handle
,
4824 mapping
, inode
->i_size
, page_len
, 0);
4831 /* If there are no blocks to remove, return now */
4832 if (first_block
>= last_block
)
4835 down_write(&EXT4_I(inode
)->i_data_sem
);
4836 ext4_ext_invalidate_cache(inode
);
4837 ext4_discard_preallocations(inode
);
4840 * Loop over all the blocks and identify blocks
4841 * that need to be punched out
4843 iblock
= first_block
;
4844 blocks_released
= 0;
4845 while (iblock
< last_block
) {
4846 max_blocks
= last_block
- iblock
;
4848 memset(&map
, 0, sizeof(map
));
4849 map
.m_lblk
= iblock
;
4850 map
.m_len
= max_blocks
;
4851 ret
= ext4_ext_map_blocks(handle
, inode
, &map
,
4852 EXT4_GET_BLOCKS_PUNCH_OUT_EXT
);
4855 blocks_released
+= ret
;
4857 } else if (ret
== 0) {
4859 * If map blocks could not find the block,
4860 * then it is in a hole. If the hole was
4861 * not already cached, then map blocks should
4862 * put it in the cache. So we can get the hole
4865 memset(&cache_ex
, 0, sizeof(cache_ex
));
4866 if ((ext4_ext_check_cache(inode
, iblock
, &cache_ex
)) &&
4867 !cache_ex
.ec_start
) {
4869 /* The hole is cached */
4870 num_blocks
= cache_ex
.ec_block
+
4871 cache_ex
.ec_len
- iblock
;
4874 /* The block could not be identified */
4879 /* Map blocks error */
4884 if (num_blocks
== 0) {
4885 /* This condition should never happen */
4886 ext_debug("Block lookup failed");
4891 iblock
+= num_blocks
;
4894 if (blocks_released
> 0) {
4895 ext4_ext_invalidate_cache(inode
);
4896 ext4_discard_preallocations(inode
);
4900 ext4_handle_sync(handle
);
4902 up_write(&EXT4_I(inode
)->i_data_sem
);
4905 ext4_orphan_del(handle
, inode
);
4906 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
4907 ext4_mark_inode_dirty(handle
, inode
);
4908 ext4_journal_stop(handle
);
4911 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
4912 __u64 start
, __u64 len
)
4914 ext4_lblk_t start_blk
;
4917 /* fallback to generic here if not in extents fmt */
4918 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
4919 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
4922 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
4925 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
4926 error
= ext4_xattr_fiemap(inode
, fieinfo
);
4928 ext4_lblk_t len_blks
;
4931 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
4932 last_blk
= (start
+ len
- 1) >> inode
->i_sb
->s_blocksize_bits
;
4933 if (last_blk
>= EXT_MAX_BLOCKS
)
4934 last_blk
= EXT_MAX_BLOCKS
-1;
4935 len_blks
= ((ext4_lblk_t
) last_blk
) - start_blk
+ 1;
4938 * Walk the extent tree gathering extent information.
4939 * ext4_ext_fiemap_cb will push extents back to user.
4941 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
4942 ext4_ext_fiemap_cb
, fieinfo
);