2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include <linux/fiemap.h>
44 #include "ext4_jbd2.h"
45 #include "ext4_extents.h"
50 * combine low and high parts of physical block number into ext4_fsblk_t
52 ext4_fsblk_t
ext_pblock(struct ext4_extent
*ex
)
56 block
= le32_to_cpu(ex
->ee_start_lo
);
57 block
|= ((ext4_fsblk_t
) le16_to_cpu(ex
->ee_start_hi
) << 31) << 1;
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
65 ext4_fsblk_t
idx_pblock(struct ext4_extent_idx
*ix
)
69 block
= le32_to_cpu(ix
->ei_leaf_lo
);
70 block
|= ((ext4_fsblk_t
) le16_to_cpu(ix
->ei_leaf_hi
) << 31) << 1;
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
79 void ext4_ext_store_pblock(struct ext4_extent
*ex
, ext4_fsblk_t pb
)
81 ex
->ee_start_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
82 ex
->ee_start_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
90 static void ext4_idx_store_pblock(struct ext4_extent_idx
*ix
, ext4_fsblk_t pb
)
92 ix
->ei_leaf_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
93 ix
->ei_leaf_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
96 static int ext4_ext_truncate_extend_restart(handle_t
*handle
,
102 if (!ext4_handle_valid(handle
))
104 if (handle
->h_buffer_credits
> needed
)
106 err
= ext4_journal_extend(handle
, needed
);
109 err
= ext4_truncate_restart_trans(handle
, inode
, needed
);
111 * We have dropped i_data_sem so someone might have cached again
112 * an extent we are going to truncate.
114 ext4_ext_invalidate_cache(inode
);
124 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
125 struct ext4_ext_path
*path
)
128 /* path points to block */
129 return ext4_journal_get_write_access(handle
, path
->p_bh
);
131 /* path points to leaf/index in inode body */
132 /* we use in-core data, no need to protect them */
142 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
143 struct ext4_ext_path
*path
)
147 /* path points to block */
148 err
= ext4_handle_dirty_metadata(handle
, inode
, path
->p_bh
);
150 /* path points to leaf/index in inode body */
151 err
= ext4_mark_inode_dirty(handle
, inode
);
156 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
157 struct ext4_ext_path
*path
,
160 struct ext4_inode_info
*ei
= EXT4_I(inode
);
161 ext4_fsblk_t bg_start
;
162 ext4_fsblk_t last_block
;
163 ext4_grpblk_t colour
;
164 ext4_group_t block_group
;
165 int flex_size
= ext4_flex_bg_size(EXT4_SB(inode
->i_sb
));
169 struct ext4_extent
*ex
;
170 depth
= path
->p_depth
;
172 /* try to predict block placement */
173 ex
= path
[depth
].p_ext
;
175 return ext_pblock(ex
)+(block
-le32_to_cpu(ex
->ee_block
));
177 /* it looks like index is empty;
178 * try to find starting block from index itself */
179 if (path
[depth
].p_bh
)
180 return path
[depth
].p_bh
->b_blocknr
;
183 /* OK. use inode's group */
184 block_group
= ei
->i_block_group
;
185 if (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) {
187 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
188 * block groups per flexgroup, reserve the first block
189 * group for directories and special files. Regular
190 * files will start at the second block group. This
191 * tends to speed up directory access and improves
194 block_group
&= ~(flex_size
-1);
195 if (S_ISREG(inode
->i_mode
))
198 bg_start
= (block_group
* EXT4_BLOCKS_PER_GROUP(inode
->i_sb
)) +
199 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_first_data_block
);
200 last_block
= ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
) - 1;
203 * If we are doing delayed allocation, we don't need take
204 * colour into account.
206 if (test_opt(inode
->i_sb
, DELALLOC
))
209 if (bg_start
+ EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) <= last_block
)
210 colour
= (current
->pid
% 16) *
211 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
213 colour
= (current
->pid
% 16) * ((last_block
- bg_start
) / 16);
214 return bg_start
+ colour
+ block
;
218 * Allocation for a meta data block
221 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
222 struct ext4_ext_path
*path
,
223 struct ext4_extent
*ex
, int *err
)
225 ext4_fsblk_t goal
, newblock
;
227 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
228 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, NULL
, err
);
232 static int ext4_ext_space_block(struct inode
*inode
)
236 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
237 / sizeof(struct ext4_extent
);
238 #ifdef AGGRESSIVE_TEST
245 static int ext4_ext_space_block_idx(struct inode
*inode
)
249 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
250 / sizeof(struct ext4_extent_idx
);
251 #ifdef AGGRESSIVE_TEST
258 static int ext4_ext_space_root(struct inode
*inode
)
262 size
= sizeof(EXT4_I(inode
)->i_data
);
263 size
-= sizeof(struct ext4_extent_header
);
264 size
/= sizeof(struct ext4_extent
);
265 #ifdef AGGRESSIVE_TEST
272 static int ext4_ext_space_root_idx(struct inode
*inode
)
276 size
= sizeof(EXT4_I(inode
)->i_data
);
277 size
-= sizeof(struct ext4_extent_header
);
278 size
/= sizeof(struct ext4_extent_idx
);
279 #ifdef AGGRESSIVE_TEST
287 * Calculate the number of metadata blocks needed
288 * to allocate @blocks
289 * Worse case is one block per extent
291 int ext4_ext_calc_metadata_amount(struct inode
*inode
, int blocks
)
293 int lcap
, icap
, rcap
, leafs
, idxs
, num
;
294 int newextents
= blocks
;
296 rcap
= ext4_ext_space_root_idx(inode
);
297 lcap
= ext4_ext_space_block(inode
);
298 icap
= ext4_ext_space_block_idx(inode
);
300 /* number of new leaf blocks needed */
301 num
= leafs
= (newextents
+ lcap
- 1) / lcap
;
304 * Worse case, we need separate index block(s)
305 * to link all new leaf blocks
307 idxs
= (leafs
+ icap
- 1) / icap
;
310 idxs
= (idxs
+ icap
- 1) / icap
;
311 } while (idxs
> rcap
);
317 ext4_ext_max_entries(struct inode
*inode
, int depth
)
321 if (depth
== ext_depth(inode
)) {
323 max
= ext4_ext_space_root(inode
);
325 max
= ext4_ext_space_root_idx(inode
);
328 max
= ext4_ext_space_block(inode
);
330 max
= ext4_ext_space_block_idx(inode
);
336 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
338 ext4_fsblk_t block
= ext_pblock(ext
);
339 int len
= ext4_ext_get_actual_len(ext
);
341 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
344 static int ext4_valid_extent_idx(struct inode
*inode
,
345 struct ext4_extent_idx
*ext_idx
)
347 ext4_fsblk_t block
= idx_pblock(ext_idx
);
349 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
352 static int ext4_valid_extent_entries(struct inode
*inode
,
353 struct ext4_extent_header
*eh
,
356 struct ext4_extent
*ext
;
357 struct ext4_extent_idx
*ext_idx
;
358 unsigned short entries
;
359 if (eh
->eh_entries
== 0)
362 entries
= le16_to_cpu(eh
->eh_entries
);
366 ext
= EXT_FIRST_EXTENT(eh
);
368 if (!ext4_valid_extent(inode
, ext
))
374 ext_idx
= EXT_FIRST_INDEX(eh
);
376 if (!ext4_valid_extent_idx(inode
, ext_idx
))
385 static int __ext4_ext_check(const char *function
, struct inode
*inode
,
386 struct ext4_extent_header
*eh
,
389 const char *error_msg
;
392 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
393 error_msg
= "invalid magic";
396 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
397 error_msg
= "unexpected eh_depth";
400 if (unlikely(eh
->eh_max
== 0)) {
401 error_msg
= "invalid eh_max";
404 max
= ext4_ext_max_entries(inode
, depth
);
405 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
406 error_msg
= "too large eh_max";
409 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
410 error_msg
= "invalid eh_entries";
413 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
414 error_msg
= "invalid extent entries";
420 ext4_error(inode
->i_sb
, function
,
421 "bad header/extent in inode #%lu: %s - magic %x, "
422 "entries %u, max %u(%u), depth %u(%u)",
423 inode
->i_ino
, error_msg
, le16_to_cpu(eh
->eh_magic
),
424 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
425 max
, le16_to_cpu(eh
->eh_depth
), depth
);
430 #define ext4_ext_check(inode, eh, depth) \
431 __ext4_ext_check(__func__, inode, eh, depth)
433 int ext4_ext_check_inode(struct inode
*inode
)
435 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
439 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
441 int k
, l
= path
->p_depth
;
444 for (k
= 0; k
<= l
; k
++, path
++) {
446 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
447 idx_pblock(path
->p_idx
));
448 } else if (path
->p_ext
) {
449 ext_debug(" %d:%d:%llu ",
450 le32_to_cpu(path
->p_ext
->ee_block
),
451 ext4_ext_get_actual_len(path
->p_ext
),
452 ext_pblock(path
->p_ext
));
459 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
461 int depth
= ext_depth(inode
);
462 struct ext4_extent_header
*eh
;
463 struct ext4_extent
*ex
;
469 eh
= path
[depth
].p_hdr
;
470 ex
= EXT_FIRST_EXTENT(eh
);
472 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
473 ext_debug("%d:%d:%llu ", le32_to_cpu(ex
->ee_block
),
474 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
479 #define ext4_ext_show_path(inode, path)
480 #define ext4_ext_show_leaf(inode, path)
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 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 ",
593 le32_to_cpu(path
->p_ext
->ee_block
),
594 ext_pblock(path
->p_ext
),
595 ext4_ext_get_actual_len(path
->p_ext
));
597 #ifdef CHECK_BINSEARCH
599 struct ext4_extent
*chex
, *ex
;
602 chex
= ex
= EXT_FIRST_EXTENT(eh
);
603 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
604 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
605 <= le32_to_cpu(ex
[-1].ee_block
));
606 if (block
< le32_to_cpu(ex
->ee_block
))
610 BUG_ON(chex
!= path
->p_ext
);
616 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
618 struct ext4_extent_header
*eh
;
620 eh
= ext_inode_hdr(inode
);
623 eh
->eh_magic
= EXT4_EXT_MAGIC
;
624 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
));
625 ext4_mark_inode_dirty(handle
, inode
);
626 ext4_ext_invalidate_cache(inode
);
630 struct ext4_ext_path
*
631 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
632 struct ext4_ext_path
*path
)
634 struct ext4_extent_header
*eh
;
635 struct buffer_head
*bh
;
636 short int depth
, i
, ppos
= 0, alloc
= 0;
638 eh
= ext_inode_hdr(inode
);
639 depth
= ext_depth(inode
);
641 /* account possible depth increase */
643 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
646 return ERR_PTR(-ENOMEM
);
653 /* walk through the tree */
655 int need_to_validate
= 0;
657 ext_debug("depth %d: num %d, max %d\n",
658 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
660 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
661 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
662 path
[ppos
].p_depth
= i
;
663 path
[ppos
].p_ext
= NULL
;
665 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
668 if (!bh_uptodate_or_lock(bh
)) {
669 if (bh_submit_read(bh
) < 0) {
673 /* validate the extent entries */
674 need_to_validate
= 1;
676 eh
= ext_block_hdr(bh
);
678 BUG_ON(ppos
> depth
);
679 path
[ppos
].p_bh
= bh
;
680 path
[ppos
].p_hdr
= eh
;
683 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
687 path
[ppos
].p_depth
= i
;
688 path
[ppos
].p_ext
= NULL
;
689 path
[ppos
].p_idx
= NULL
;
692 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
693 /* if not an empty leaf */
694 if (path
[ppos
].p_ext
)
695 path
[ppos
].p_block
= ext_pblock(path
[ppos
].p_ext
);
697 ext4_ext_show_path(inode
, path
);
702 ext4_ext_drop_refs(path
);
705 return ERR_PTR(-EIO
);
709 * ext4_ext_insert_index:
710 * insert new index [@logical;@ptr] into the block at @curp;
711 * check where to insert: before @curp or after @curp
713 int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
714 struct ext4_ext_path
*curp
,
715 int logical
, ext4_fsblk_t ptr
)
717 struct ext4_extent_idx
*ix
;
720 err
= ext4_ext_get_access(handle
, inode
, curp
);
724 BUG_ON(logical
== le32_to_cpu(curp
->p_idx
->ei_block
));
725 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
726 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
728 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
729 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
730 len
= len
< 0 ? 0 : len
;
731 ext_debug("insert new index %d after: %llu. "
732 "move %d from 0x%p to 0x%p\n",
734 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
735 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
737 ix
= curp
->p_idx
+ 1;
740 len
= len
* sizeof(struct ext4_extent_idx
);
741 len
= len
< 0 ? 0 : len
;
742 ext_debug("insert new index %d before: %llu. "
743 "move %d from 0x%p to 0x%p\n",
745 curp
->p_idx
, (curp
->p_idx
+ 1));
746 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
750 ix
->ei_block
= cpu_to_le32(logical
);
751 ext4_idx_store_pblock(ix
, ptr
);
752 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
754 BUG_ON(le16_to_cpu(curp
->p_hdr
->eh_entries
)
755 > le16_to_cpu(curp
->p_hdr
->eh_max
));
756 BUG_ON(ix
> EXT_LAST_INDEX(curp
->p_hdr
));
758 err
= ext4_ext_dirty(handle
, inode
, curp
);
759 ext4_std_error(inode
->i_sb
, err
);
766 * inserts new subtree into the path, using free index entry
768 * - allocates all needed blocks (new leaf and all intermediate index blocks)
769 * - makes decision where to split
770 * - moves remaining extents and index entries (right to the split point)
771 * into the newly allocated blocks
772 * - initializes subtree
774 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
775 struct ext4_ext_path
*path
,
776 struct ext4_extent
*newext
, int at
)
778 struct buffer_head
*bh
= NULL
;
779 int depth
= ext_depth(inode
);
780 struct ext4_extent_header
*neh
;
781 struct ext4_extent_idx
*fidx
;
782 struct ext4_extent
*ex
;
784 ext4_fsblk_t newblock
, oldblock
;
786 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
789 /* make decision: where to split? */
790 /* FIXME: now decision is simplest: at current extent */
792 /* if current leaf will be split, then we should use
793 * border from split point */
794 BUG_ON(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
));
795 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
796 border
= path
[depth
].p_ext
[1].ee_block
;
797 ext_debug("leaf will be split."
798 " next leaf starts at %d\n",
799 le32_to_cpu(border
));
801 border
= newext
->ee_block
;
802 ext_debug("leaf will be added."
803 " next leaf starts at %d\n",
804 le32_to_cpu(border
));
808 * If error occurs, then we break processing
809 * and mark filesystem read-only. index won't
810 * be inserted and tree will be in consistent
811 * state. Next mount will repair buffers too.
815 * Get array to track all allocated blocks.
816 * We need this to handle errors and free blocks
819 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
823 /* allocate all needed blocks */
824 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
825 for (a
= 0; a
< depth
- at
; a
++) {
826 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
830 ablocks
[a
] = newblock
;
833 /* initialize new leaf */
834 newblock
= ablocks
[--a
];
835 BUG_ON(newblock
== 0);
836 bh
= sb_getblk(inode
->i_sb
, newblock
);
843 err
= ext4_journal_get_create_access(handle
, bh
);
847 neh
= ext_block_hdr(bh
);
849 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
850 neh
->eh_magic
= EXT4_EXT_MAGIC
;
852 ex
= EXT_FIRST_EXTENT(neh
);
854 /* move remainder of path[depth] to the new leaf */
855 BUG_ON(path
[depth
].p_hdr
->eh_entries
!= path
[depth
].p_hdr
->eh_max
);
856 /* start copy from next extent */
857 /* TODO: we could do it by single memmove */
860 while (path
[depth
].p_ext
<=
861 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
862 ext_debug("move %d:%llu:%d in new leaf %llu\n",
863 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
864 ext_pblock(path
[depth
].p_ext
),
865 ext4_ext_get_actual_len(path
[depth
].p_ext
),
867 /*memmove(ex++, path[depth].p_ext++,
868 sizeof(struct ext4_extent));
874 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
875 le16_add_cpu(&neh
->eh_entries
, m
);
878 set_buffer_uptodate(bh
);
881 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
887 /* correct old leaf */
889 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
892 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
893 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
899 /* create intermediate indexes */
903 ext_debug("create %d intermediate indices\n", k
);
904 /* insert new index into current index block */
905 /* current depth stored in i var */
909 newblock
= ablocks
[--a
];
910 bh
= sb_getblk(inode
->i_sb
, newblock
);
917 err
= ext4_journal_get_create_access(handle
, bh
);
921 neh
= ext_block_hdr(bh
);
922 neh
->eh_entries
= cpu_to_le16(1);
923 neh
->eh_magic
= EXT4_EXT_MAGIC
;
924 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
925 neh
->eh_depth
= cpu_to_le16(depth
- i
);
926 fidx
= EXT_FIRST_INDEX(neh
);
927 fidx
->ei_block
= border
;
928 ext4_idx_store_pblock(fidx
, oldblock
);
930 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
931 i
, newblock
, le32_to_cpu(border
), oldblock
);
936 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
937 EXT_MAX_INDEX(path
[i
].p_hdr
));
938 BUG_ON(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
939 EXT_LAST_INDEX(path
[i
].p_hdr
));
940 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
941 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
942 le32_to_cpu(path
[i
].p_idx
->ei_block
),
943 idx_pblock(path
[i
].p_idx
),
945 /*memmove(++fidx, path[i].p_idx++,
946 sizeof(struct ext4_extent_idx));
948 BUG_ON(neh->eh_entries > neh->eh_max);*/
953 memmove(++fidx
, path
[i
].p_idx
- m
,
954 sizeof(struct ext4_extent_idx
) * m
);
955 le16_add_cpu(&neh
->eh_entries
, m
);
957 set_buffer_uptodate(bh
);
960 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
966 /* correct old index */
968 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
971 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
972 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
980 /* insert new index */
981 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
982 le32_to_cpu(border
), newblock
);
986 if (buffer_locked(bh
))
992 /* free all allocated blocks in error case */
993 for (i
= 0; i
< depth
; i
++) {
996 ext4_free_blocks(handle
, inode
, ablocks
[i
], 1, 1);
1005 * ext4_ext_grow_indepth:
1006 * implements tree growing procedure:
1007 * - allocates new block
1008 * - moves top-level data (index block or leaf) into the new block
1009 * - initializes new top-level, creating index that points to the
1010 * just created block
1012 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1013 struct ext4_ext_path
*path
,
1014 struct ext4_extent
*newext
)
1016 struct ext4_ext_path
*curp
= path
;
1017 struct ext4_extent_header
*neh
;
1018 struct ext4_extent_idx
*fidx
;
1019 struct buffer_head
*bh
;
1020 ext4_fsblk_t newblock
;
1023 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
, newext
, &err
);
1027 bh
= sb_getblk(inode
->i_sb
, newblock
);
1030 ext4_std_error(inode
->i_sb
, err
);
1035 err
= ext4_journal_get_create_access(handle
, bh
);
1041 /* move top-level index/leaf into new block */
1042 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1044 /* set size of new block */
1045 neh
= ext_block_hdr(bh
);
1046 /* old root could have indexes or leaves
1047 * so calculate e_max right way */
1048 if (ext_depth(inode
))
1049 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
1051 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
1052 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1053 set_buffer_uptodate(bh
);
1056 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1060 /* create index in new top-level index: num,max,pointer */
1061 err
= ext4_ext_get_access(handle
, inode
, curp
);
1065 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1066 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
));
1067 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1068 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1070 if (path
[0].p_hdr
->eh_depth
)
1071 curp
->p_idx
->ei_block
=
1072 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1074 curp
->p_idx
->ei_block
=
1075 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1076 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1078 neh
= ext_inode_hdr(inode
);
1079 fidx
= EXT_FIRST_INDEX(neh
);
1080 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1081 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1082 le32_to_cpu(fidx
->ei_block
), idx_pblock(fidx
));
1084 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1085 err
= ext4_ext_dirty(handle
, inode
, curp
);
1093 * ext4_ext_create_new_leaf:
1094 * finds empty index and adds new leaf.
1095 * if no free index is found, then it requests in-depth growing.
1097 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1098 struct ext4_ext_path
*path
,
1099 struct ext4_extent
*newext
)
1101 struct ext4_ext_path
*curp
;
1102 int depth
, i
, err
= 0;
1105 i
= depth
= ext_depth(inode
);
1107 /* walk up to the tree and look for free index entry */
1108 curp
= path
+ depth
;
1109 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1114 /* we use already allocated block for index block,
1115 * so subsequent data blocks should be contiguous */
1116 if (EXT_HAS_FREE_INDEX(curp
)) {
1117 /* if we found index with free entry, then use that
1118 * entry: create all needed subtree and add new leaf */
1119 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
1124 ext4_ext_drop_refs(path
);
1125 path
= ext4_ext_find_extent(inode
,
1126 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1129 err
= PTR_ERR(path
);
1131 /* tree is full, time to grow in depth */
1132 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1137 ext4_ext_drop_refs(path
);
1138 path
= ext4_ext_find_extent(inode
,
1139 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1142 err
= PTR_ERR(path
);
1147 * only first (depth 0 -> 1) produces free space;
1148 * in all other cases we have to split the grown tree
1150 depth
= ext_depth(inode
);
1151 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1152 /* now we need to split */
1162 * search the closest allocated block to the left for *logical
1163 * and returns it at @logical + it's physical address at @phys
1164 * if *logical is the smallest allocated block, the function
1165 * returns 0 at @phys
1166 * return value contains 0 (success) or error code
1169 ext4_ext_search_left(struct inode
*inode
, struct ext4_ext_path
*path
,
1170 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1172 struct ext4_extent_idx
*ix
;
1173 struct ext4_extent
*ex
;
1176 BUG_ON(path
== NULL
);
1177 depth
= path
->p_depth
;
1180 if (depth
== 0 && path
->p_ext
== NULL
)
1183 /* usually extent in the path covers blocks smaller
1184 * then *logical, but it can be that extent is the
1185 * first one in the file */
1187 ex
= path
[depth
].p_ext
;
1188 ee_len
= ext4_ext_get_actual_len(ex
);
1189 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1190 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1191 while (--depth
>= 0) {
1192 ix
= path
[depth
].p_idx
;
1193 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1198 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1200 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1201 *phys
= ext_pblock(ex
) + ee_len
- 1;
1206 * search the closest allocated block to the right for *logical
1207 * and returns it at @logical + it's physical address at @phys
1208 * if *logical is the smallest allocated block, the function
1209 * returns 0 at @phys
1210 * return value contains 0 (success) or error code
1213 ext4_ext_search_right(struct inode
*inode
, struct ext4_ext_path
*path
,
1214 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1216 struct buffer_head
*bh
= NULL
;
1217 struct ext4_extent_header
*eh
;
1218 struct ext4_extent_idx
*ix
;
1219 struct ext4_extent
*ex
;
1221 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1224 BUG_ON(path
== NULL
);
1225 depth
= path
->p_depth
;
1228 if (depth
== 0 && path
->p_ext
== NULL
)
1231 /* usually extent in the path covers blocks smaller
1232 * then *logical, but it can be that extent is the
1233 * first one in the file */
1235 ex
= path
[depth
].p_ext
;
1236 ee_len
= ext4_ext_get_actual_len(ex
);
1237 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1238 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1239 while (--depth
>= 0) {
1240 ix
= path
[depth
].p_idx
;
1241 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1243 *logical
= le32_to_cpu(ex
->ee_block
);
1244 *phys
= ext_pblock(ex
);
1248 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1250 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1251 /* next allocated block in this leaf */
1253 *logical
= le32_to_cpu(ex
->ee_block
);
1254 *phys
= ext_pblock(ex
);
1258 /* go up and search for index to the right */
1259 while (--depth
>= 0) {
1260 ix
= path
[depth
].p_idx
;
1261 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1265 /* we've gone up to the root and found no index to the right */
1269 /* we've found index to the right, let's
1270 * follow it and find the closest allocated
1271 * block to the right */
1273 block
= idx_pblock(ix
);
1274 while (++depth
< path
->p_depth
) {
1275 bh
= sb_bread(inode
->i_sb
, block
);
1278 eh
= ext_block_hdr(bh
);
1279 /* subtract from p_depth to get proper eh_depth */
1280 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1284 ix
= EXT_FIRST_INDEX(eh
);
1285 block
= idx_pblock(ix
);
1289 bh
= sb_bread(inode
->i_sb
, block
);
1292 eh
= ext_block_hdr(bh
);
1293 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1297 ex
= EXT_FIRST_EXTENT(eh
);
1298 *logical
= le32_to_cpu(ex
->ee_block
);
1299 *phys
= ext_pblock(ex
);
1305 * ext4_ext_next_allocated_block:
1306 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1307 * NOTE: it considers block number from index entry as
1308 * allocated block. Thus, index entries have to be consistent
1312 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1316 BUG_ON(path
== NULL
);
1317 depth
= path
->p_depth
;
1319 if (depth
== 0 && path
->p_ext
== NULL
)
1320 return EXT_MAX_BLOCK
;
1322 while (depth
>= 0) {
1323 if (depth
== path
->p_depth
) {
1325 if (path
[depth
].p_ext
!=
1326 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1327 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1330 if (path
[depth
].p_idx
!=
1331 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1332 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1337 return EXT_MAX_BLOCK
;
1341 * ext4_ext_next_leaf_block:
1342 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1344 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1345 struct ext4_ext_path
*path
)
1349 BUG_ON(path
== NULL
);
1350 depth
= path
->p_depth
;
1352 /* zero-tree has no leaf blocks at all */
1354 return EXT_MAX_BLOCK
;
1356 /* go to index block */
1359 while (depth
>= 0) {
1360 if (path
[depth
].p_idx
!=
1361 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1362 return (ext4_lblk_t
)
1363 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1367 return EXT_MAX_BLOCK
;
1371 * ext4_ext_correct_indexes:
1372 * if leaf gets modified and modified extent is first in the leaf,
1373 * then we have to correct all indexes above.
1374 * TODO: do we need to correct tree in all cases?
1376 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1377 struct ext4_ext_path
*path
)
1379 struct ext4_extent_header
*eh
;
1380 int depth
= ext_depth(inode
);
1381 struct ext4_extent
*ex
;
1385 eh
= path
[depth
].p_hdr
;
1386 ex
= path
[depth
].p_ext
;
1391 /* there is no tree at all */
1395 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1396 /* we correct tree if first leaf got modified only */
1401 * TODO: we need correction if border is smaller than current one
1404 border
= path
[depth
].p_ext
->ee_block
;
1405 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1408 path
[k
].p_idx
->ei_block
= border
;
1409 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1414 /* change all left-side indexes */
1415 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1417 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1420 path
[k
].p_idx
->ei_block
= border
;
1421 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1430 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1431 struct ext4_extent
*ex2
)
1433 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1436 * Make sure that either both extents are uninitialized, or
1439 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1442 if (ext4_ext_is_uninitialized(ex1
))
1443 max_len
= EXT_UNINIT_MAX_LEN
;
1445 max_len
= EXT_INIT_MAX_LEN
;
1447 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1448 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1450 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1451 le32_to_cpu(ex2
->ee_block
))
1455 * To allow future support for preallocated extents to be added
1456 * as an RO_COMPAT feature, refuse to merge to extents if
1457 * this can result in the top bit of ee_len being set.
1459 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1461 #ifdef AGGRESSIVE_TEST
1462 if (ext1_ee_len
>= 4)
1466 if (ext_pblock(ex1
) + ext1_ee_len
== ext_pblock(ex2
))
1472 * This function tries to merge the "ex" extent to the next extent in the tree.
1473 * It always tries to merge towards right. If you want to merge towards
1474 * left, pass "ex - 1" as argument instead of "ex".
1475 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1476 * 1 if they got merged.
1478 int ext4_ext_try_to_merge(struct inode
*inode
,
1479 struct ext4_ext_path
*path
,
1480 struct ext4_extent
*ex
)
1482 struct ext4_extent_header
*eh
;
1483 unsigned int depth
, len
;
1485 int uninitialized
= 0;
1487 depth
= ext_depth(inode
);
1488 BUG_ON(path
[depth
].p_hdr
== NULL
);
1489 eh
= path
[depth
].p_hdr
;
1491 while (ex
< EXT_LAST_EXTENT(eh
)) {
1492 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1494 /* merge with next extent! */
1495 if (ext4_ext_is_uninitialized(ex
))
1497 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1498 + ext4_ext_get_actual_len(ex
+ 1));
1500 ext4_ext_mark_uninitialized(ex
);
1502 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1503 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1504 * sizeof(struct ext4_extent
);
1505 memmove(ex
+ 1, ex
+ 2, len
);
1507 le16_add_cpu(&eh
->eh_entries
, -1);
1509 WARN_ON(eh
->eh_entries
== 0);
1510 if (!eh
->eh_entries
)
1511 ext4_error(inode
->i_sb
, "ext4_ext_try_to_merge",
1512 "inode#%lu, eh->eh_entries = 0!", inode
->i_ino
);
1519 * check if a portion of the "newext" extent overlaps with an
1522 * If there is an overlap discovered, it updates the length of the newext
1523 * such that there will be no overlap, and then returns 1.
1524 * If there is no overlap found, it returns 0.
1526 unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1527 struct ext4_extent
*newext
,
1528 struct ext4_ext_path
*path
)
1531 unsigned int depth
, len1
;
1532 unsigned int ret
= 0;
1534 b1
= le32_to_cpu(newext
->ee_block
);
1535 len1
= ext4_ext_get_actual_len(newext
);
1536 depth
= ext_depth(inode
);
1537 if (!path
[depth
].p_ext
)
1539 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1542 * get the next allocated block if the extent in the path
1543 * is before the requested block(s)
1546 b2
= ext4_ext_next_allocated_block(path
);
1547 if (b2
== EXT_MAX_BLOCK
)
1551 /* check for wrap through zero on extent logical start block*/
1552 if (b1
+ len1
< b1
) {
1553 len1
= EXT_MAX_BLOCK
- b1
;
1554 newext
->ee_len
= cpu_to_le16(len1
);
1558 /* check for overlap */
1559 if (b1
+ len1
> b2
) {
1560 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1568 * ext4_ext_insert_extent:
1569 * tries to merge requsted extent into the existing extent or
1570 * inserts requested extent as new one into the tree,
1571 * creating new leaf in the no-space case.
1573 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1574 struct ext4_ext_path
*path
,
1575 struct ext4_extent
*newext
, int flag
)
1577 struct ext4_extent_header
*eh
;
1578 struct ext4_extent
*ex
, *fex
;
1579 struct ext4_extent
*nearex
; /* nearest extent */
1580 struct ext4_ext_path
*npath
= NULL
;
1581 int depth
, len
, err
;
1583 unsigned uninitialized
= 0;
1585 BUG_ON(ext4_ext_get_actual_len(newext
) == 0);
1586 depth
= ext_depth(inode
);
1587 ex
= path
[depth
].p_ext
;
1588 BUG_ON(path
[depth
].p_hdr
== NULL
);
1590 /* try to insert block into found extent and return */
1591 if (ex
&& (flag
!= EXT4_GET_BLOCKS_DIO_CREATE_EXT
)
1592 && ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1593 ext_debug("append %d block to %d:%d (from %llu)\n",
1594 ext4_ext_get_actual_len(newext
),
1595 le32_to_cpu(ex
->ee_block
),
1596 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
1597 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1602 * ext4_can_extents_be_merged should have checked that either
1603 * both extents are uninitialized, or both aren't. Thus we
1604 * need to check only one of them here.
1606 if (ext4_ext_is_uninitialized(ex
))
1608 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1609 + ext4_ext_get_actual_len(newext
));
1611 ext4_ext_mark_uninitialized(ex
);
1612 eh
= path
[depth
].p_hdr
;
1618 depth
= ext_depth(inode
);
1619 eh
= path
[depth
].p_hdr
;
1620 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1623 /* probably next leaf has space for us? */
1624 fex
= EXT_LAST_EXTENT(eh
);
1625 next
= ext4_ext_next_leaf_block(inode
, path
);
1626 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1627 && next
!= EXT_MAX_BLOCK
) {
1628 ext_debug("next leaf block - %d\n", next
);
1629 BUG_ON(npath
!= NULL
);
1630 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1632 return PTR_ERR(npath
);
1633 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1634 eh
= npath
[depth
].p_hdr
;
1635 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1636 ext_debug("next leaf isnt full(%d)\n",
1637 le16_to_cpu(eh
->eh_entries
));
1641 ext_debug("next leaf has no free space(%d,%d)\n",
1642 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1646 * There is no free space in the found leaf.
1647 * We're gonna add a new leaf in the tree.
1649 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1652 depth
= ext_depth(inode
);
1653 eh
= path
[depth
].p_hdr
;
1656 nearex
= path
[depth
].p_ext
;
1658 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1663 /* there is no extent in this leaf, create first one */
1664 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1665 le32_to_cpu(newext
->ee_block
),
1667 ext4_ext_get_actual_len(newext
));
1668 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1669 } else if (le32_to_cpu(newext
->ee_block
)
1670 > le32_to_cpu(nearex
->ee_block
)) {
1671 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1672 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1673 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1674 len
= (len
- 1) * sizeof(struct ext4_extent
);
1675 len
= len
< 0 ? 0 : len
;
1676 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1677 "move %d from 0x%p to 0x%p\n",
1678 le32_to_cpu(newext
->ee_block
),
1680 ext4_ext_get_actual_len(newext
),
1681 nearex
, len
, nearex
+ 1, nearex
+ 2);
1682 memmove(nearex
+ 2, nearex
+ 1, len
);
1684 path
[depth
].p_ext
= nearex
+ 1;
1686 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1687 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1688 len
= len
< 0 ? 0 : len
;
1689 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1690 "move %d from 0x%p to 0x%p\n",
1691 le32_to_cpu(newext
->ee_block
),
1693 ext4_ext_get_actual_len(newext
),
1694 nearex
, len
, nearex
+ 1, nearex
+ 2);
1695 memmove(nearex
+ 1, nearex
, len
);
1696 path
[depth
].p_ext
= nearex
;
1699 le16_add_cpu(&eh
->eh_entries
, 1);
1700 nearex
= path
[depth
].p_ext
;
1701 nearex
->ee_block
= newext
->ee_block
;
1702 ext4_ext_store_pblock(nearex
, ext_pblock(newext
));
1703 nearex
->ee_len
= newext
->ee_len
;
1706 /* try to merge extents to the right */
1707 if (flag
!= EXT4_GET_BLOCKS_DIO_CREATE_EXT
)
1708 ext4_ext_try_to_merge(inode
, path
, nearex
);
1710 /* try to merge extents to the left */
1712 /* time to correct all indexes above */
1713 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1717 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1721 ext4_ext_drop_refs(npath
);
1724 ext4_ext_invalidate_cache(inode
);
1728 int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1729 ext4_lblk_t num
, ext_prepare_callback func
,
1732 struct ext4_ext_path
*path
= NULL
;
1733 struct ext4_ext_cache cbex
;
1734 struct ext4_extent
*ex
;
1735 ext4_lblk_t next
, start
= 0, end
= 0;
1736 ext4_lblk_t last
= block
+ num
;
1737 int depth
, exists
, err
= 0;
1739 BUG_ON(func
== NULL
);
1740 BUG_ON(inode
== NULL
);
1742 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1744 /* find extent for this block */
1745 down_read(&EXT4_I(inode
)->i_data_sem
);
1746 path
= ext4_ext_find_extent(inode
, block
, path
);
1747 up_read(&EXT4_I(inode
)->i_data_sem
);
1749 err
= PTR_ERR(path
);
1754 depth
= ext_depth(inode
);
1755 BUG_ON(path
[depth
].p_hdr
== NULL
);
1756 ex
= path
[depth
].p_ext
;
1757 next
= ext4_ext_next_allocated_block(path
);
1761 /* there is no extent yet, so try to allocate
1762 * all requested space */
1765 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1766 /* need to allocate space before found extent */
1768 end
= le32_to_cpu(ex
->ee_block
);
1769 if (block
+ num
< end
)
1771 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1772 + ext4_ext_get_actual_len(ex
)) {
1773 /* need to allocate space after found extent */
1778 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1780 * some part of requested space is covered
1784 end
= le32_to_cpu(ex
->ee_block
)
1785 + ext4_ext_get_actual_len(ex
);
1786 if (block
+ num
< end
)
1792 BUG_ON(end
<= start
);
1795 cbex
.ec_block
= start
;
1796 cbex
.ec_len
= end
- start
;
1798 cbex
.ec_type
= EXT4_EXT_CACHE_GAP
;
1800 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1801 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1802 cbex
.ec_start
= ext_pblock(ex
);
1803 cbex
.ec_type
= EXT4_EXT_CACHE_EXTENT
;
1806 BUG_ON(cbex
.ec_len
== 0);
1807 err
= func(inode
, path
, &cbex
, ex
, cbdata
);
1808 ext4_ext_drop_refs(path
);
1813 if (err
== EXT_REPEAT
)
1815 else if (err
== EXT_BREAK
) {
1820 if (ext_depth(inode
) != depth
) {
1821 /* depth was changed. we have to realloc path */
1826 block
= cbex
.ec_block
+ cbex
.ec_len
;
1830 ext4_ext_drop_refs(path
);
1838 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1839 __u32 len
, ext4_fsblk_t start
, int type
)
1841 struct ext4_ext_cache
*cex
;
1843 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1844 cex
= &EXT4_I(inode
)->i_cached_extent
;
1845 cex
->ec_type
= type
;
1846 cex
->ec_block
= block
;
1848 cex
->ec_start
= start
;
1849 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1853 * ext4_ext_put_gap_in_cache:
1854 * calculate boundaries of the gap that the requested block fits into
1855 * and cache this gap
1858 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1861 int depth
= ext_depth(inode
);
1864 struct ext4_extent
*ex
;
1866 ex
= path
[depth
].p_ext
;
1868 /* there is no extent yet, so gap is [0;-] */
1870 len
= EXT_MAX_BLOCK
;
1871 ext_debug("cache gap(whole file):");
1872 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1874 len
= le32_to_cpu(ex
->ee_block
) - block
;
1875 ext_debug("cache gap(before): %u [%u:%u]",
1877 le32_to_cpu(ex
->ee_block
),
1878 ext4_ext_get_actual_len(ex
));
1879 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1880 + ext4_ext_get_actual_len(ex
)) {
1882 lblock
= le32_to_cpu(ex
->ee_block
)
1883 + ext4_ext_get_actual_len(ex
);
1885 next
= ext4_ext_next_allocated_block(path
);
1886 ext_debug("cache gap(after): [%u:%u] %u",
1887 le32_to_cpu(ex
->ee_block
),
1888 ext4_ext_get_actual_len(ex
),
1890 BUG_ON(next
== lblock
);
1891 len
= next
- lblock
;
1897 ext_debug(" -> %u:%lu\n", lblock
, len
);
1898 ext4_ext_put_in_cache(inode
, lblock
, len
, 0, EXT4_EXT_CACHE_GAP
);
1902 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1903 struct ext4_extent
*ex
)
1905 struct ext4_ext_cache
*cex
;
1906 int ret
= EXT4_EXT_CACHE_NO
;
1909 * We borrow i_block_reservation_lock to protect i_cached_extent
1911 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1912 cex
= &EXT4_I(inode
)->i_cached_extent
;
1914 /* has cache valid data? */
1915 if (cex
->ec_type
== EXT4_EXT_CACHE_NO
)
1918 BUG_ON(cex
->ec_type
!= EXT4_EXT_CACHE_GAP
&&
1919 cex
->ec_type
!= EXT4_EXT_CACHE_EXTENT
);
1920 if (block
>= cex
->ec_block
&& block
< cex
->ec_block
+ cex
->ec_len
) {
1921 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
1922 ext4_ext_store_pblock(ex
, cex
->ec_start
);
1923 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
1924 ext_debug("%u cached by %u:%u:%llu\n",
1926 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
1930 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1936 * removes index from the index block.
1937 * It's used in truncate case only, thus all requests are for
1938 * last index in the block only.
1940 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
1941 struct ext4_ext_path
*path
)
1943 struct buffer_head
*bh
;
1947 /* free index block */
1949 leaf
= idx_pblock(path
->p_idx
);
1950 BUG_ON(path
->p_hdr
->eh_entries
== 0);
1951 err
= ext4_ext_get_access(handle
, inode
, path
);
1954 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
1955 err
= ext4_ext_dirty(handle
, inode
, path
);
1958 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
1959 bh
= sb_find_get_block(inode
->i_sb
, leaf
);
1960 ext4_forget(handle
, 1, inode
, bh
, leaf
);
1961 ext4_free_blocks(handle
, inode
, leaf
, 1, 1);
1966 * ext4_ext_calc_credits_for_single_extent:
1967 * This routine returns max. credits that needed to insert an extent
1968 * to the extent tree.
1969 * When pass the actual path, the caller should calculate credits
1972 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
1973 struct ext4_ext_path
*path
)
1976 int depth
= ext_depth(inode
);
1979 /* probably there is space in leaf? */
1980 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
1981 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
1984 * There are some space in the leaf tree, no
1985 * need to account for leaf block credit
1987 * bitmaps and block group descriptor blocks
1988 * and other metadat blocks still need to be
1991 /* 1 bitmap, 1 block group descriptor */
1992 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
1997 return ext4_chunk_trans_blocks(inode
, nrblocks
);
2001 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2003 * if nrblocks are fit in a single extent (chunk flag is 1), then
2004 * in the worse case, each tree level index/leaf need to be changed
2005 * if the tree split due to insert a new extent, then the old tree
2006 * index/leaf need to be updated too
2008 * If the nrblocks are discontiguous, they could cause
2009 * the whole tree split more than once, but this is really rare.
2011 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2014 int depth
= ext_depth(inode
);
2024 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2025 struct ext4_extent
*ex
,
2026 ext4_lblk_t from
, ext4_lblk_t to
)
2028 struct buffer_head
*bh
;
2029 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2030 int i
, metadata
= 0;
2032 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2034 #ifdef EXTENTS_STATS
2036 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2037 spin_lock(&sbi
->s_ext_stats_lock
);
2038 sbi
->s_ext_blocks
+= ee_len
;
2039 sbi
->s_ext_extents
++;
2040 if (ee_len
< sbi
->s_ext_min
)
2041 sbi
->s_ext_min
= ee_len
;
2042 if (ee_len
> sbi
->s_ext_max
)
2043 sbi
->s_ext_max
= ee_len
;
2044 if (ext_depth(inode
) > sbi
->s_depth_max
)
2045 sbi
->s_depth_max
= ext_depth(inode
);
2046 spin_unlock(&sbi
->s_ext_stats_lock
);
2049 if (from
>= le32_to_cpu(ex
->ee_block
)
2050 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2055 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2056 start
= ext_pblock(ex
) + ee_len
- num
;
2057 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2058 for (i
= 0; i
< num
; i
++) {
2059 bh
= sb_find_get_block(inode
->i_sb
, start
+ i
);
2060 ext4_forget(handle
, metadata
, inode
, bh
, start
+ i
);
2062 ext4_free_blocks(handle
, inode
, start
, num
, metadata
);
2063 } else if (from
== le32_to_cpu(ex
->ee_block
)
2064 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2065 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
2066 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2068 printk(KERN_INFO
"strange request: removal(2) "
2069 "%u-%u from %u:%u\n",
2070 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2076 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2077 struct ext4_ext_path
*path
, ext4_lblk_t start
)
2079 int err
= 0, correct_index
= 0;
2080 int depth
= ext_depth(inode
), credits
;
2081 struct ext4_extent_header
*eh
;
2082 ext4_lblk_t a
, b
, block
;
2084 ext4_lblk_t ex_ee_block
;
2085 unsigned short ex_ee_len
;
2086 unsigned uninitialized
= 0;
2087 struct ext4_extent
*ex
;
2089 /* the header must be checked already in ext4_ext_remove_space() */
2090 ext_debug("truncate since %u in leaf\n", start
);
2091 if (!path
[depth
].p_hdr
)
2092 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2093 eh
= path
[depth
].p_hdr
;
2096 /* find where to start removing */
2097 ex
= EXT_LAST_EXTENT(eh
);
2099 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2100 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2102 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2103 ex_ee_block
+ ex_ee_len
> start
) {
2105 if (ext4_ext_is_uninitialized(ex
))
2110 ext_debug("remove ext %lu:%u\n", ex_ee_block
, ex_ee_len
);
2111 path
[depth
].p_ext
= ex
;
2113 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2114 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
2115 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
2117 ext_debug(" border %u:%u\n", a
, b
);
2119 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
2123 } else if (a
!= ex_ee_block
) {
2124 /* remove tail of the extent */
2125 block
= ex_ee_block
;
2127 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2128 /* remove head of the extent */
2131 /* there is no "make a hole" API yet */
2134 /* remove whole extent: excellent! */
2135 block
= ex_ee_block
;
2137 BUG_ON(a
!= ex_ee_block
);
2138 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
2142 * 3 for leaf, sb, and inode plus 2 (bmap and group
2143 * descriptor) for each block group; assume two block
2144 * groups plus ex_ee_len/blocks_per_block_group for
2147 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2148 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2150 credits
+= (ext_depth(inode
)) + 1;
2152 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(inode
->i_sb
);
2154 err
= ext4_ext_truncate_extend_restart(handle
, inode
, credits
);
2158 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2162 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2167 /* this extent is removed; mark slot entirely unused */
2168 ext4_ext_store_pblock(ex
, 0);
2169 le16_add_cpu(&eh
->eh_entries
, -1);
2172 ex
->ee_block
= cpu_to_le32(block
);
2173 ex
->ee_len
= cpu_to_le16(num
);
2175 * Do not mark uninitialized if all the blocks in the
2176 * extent have been removed.
2178 if (uninitialized
&& num
)
2179 ext4_ext_mark_uninitialized(ex
);
2181 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2185 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2188 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2189 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2192 if (correct_index
&& eh
->eh_entries
)
2193 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2195 /* if this leaf is free, then we should
2196 * remove it from index block above */
2197 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2198 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2205 * ext4_ext_more_to_rm:
2206 * returns 1 if current index has to be freed (even partial)
2209 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2211 BUG_ON(path
->p_idx
== NULL
);
2213 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2217 * if truncate on deeper level happened, it wasn't partial,
2218 * so we have to consider current index for truncation
2220 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2225 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2227 struct super_block
*sb
= inode
->i_sb
;
2228 int depth
= ext_depth(inode
);
2229 struct ext4_ext_path
*path
;
2233 ext_debug("truncate since %u\n", start
);
2235 /* probably first extent we're gonna free will be last in block */
2236 handle
= ext4_journal_start(inode
, depth
+ 1);
2238 return PTR_ERR(handle
);
2240 ext4_ext_invalidate_cache(inode
);
2243 * We start scanning from right side, freeing all the blocks
2244 * after i_size and walking into the tree depth-wise.
2246 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2248 ext4_journal_stop(handle
);
2251 path
[0].p_hdr
= ext_inode_hdr(inode
);
2252 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2256 path
[0].p_depth
= depth
;
2258 while (i
>= 0 && err
== 0) {
2260 /* this is leaf block */
2261 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2262 /* root level has p_bh == NULL, brelse() eats this */
2263 brelse(path
[i
].p_bh
);
2264 path
[i
].p_bh
= NULL
;
2269 /* this is index block */
2270 if (!path
[i
].p_hdr
) {
2271 ext_debug("initialize header\n");
2272 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2275 if (!path
[i
].p_idx
) {
2276 /* this level hasn't been touched yet */
2277 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2278 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2279 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2281 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2283 /* we were already here, see at next index */
2287 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2288 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2290 if (ext4_ext_more_to_rm(path
+ i
)) {
2291 struct buffer_head
*bh
;
2292 /* go to the next level */
2293 ext_debug("move to level %d (block %llu)\n",
2294 i
+ 1, idx_pblock(path
[i
].p_idx
));
2295 memset(path
+ i
+ 1, 0, sizeof(*path
));
2296 bh
= sb_bread(sb
, idx_pblock(path
[i
].p_idx
));
2298 /* should we reset i_size? */
2302 if (WARN_ON(i
+ 1 > depth
)) {
2306 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2311 path
[i
+ 1].p_bh
= bh
;
2313 /* save actual number of indexes since this
2314 * number is changed at the next iteration */
2315 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2318 /* we finished processing this index, go up */
2319 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2320 /* index is empty, remove it;
2321 * handle must be already prepared by the
2322 * truncatei_leaf() */
2323 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2325 /* root level has p_bh == NULL, brelse() eats this */
2326 brelse(path
[i
].p_bh
);
2327 path
[i
].p_bh
= NULL
;
2329 ext_debug("return to level %d\n", i
);
2333 /* TODO: flexible tree reduction should be here */
2334 if (path
->p_hdr
->eh_entries
== 0) {
2336 * truncate to zero freed all the tree,
2337 * so we need to correct eh_depth
2339 err
= ext4_ext_get_access(handle
, inode
, path
);
2341 ext_inode_hdr(inode
)->eh_depth
= 0;
2342 ext_inode_hdr(inode
)->eh_max
=
2343 cpu_to_le16(ext4_ext_space_root(inode
));
2344 err
= ext4_ext_dirty(handle
, inode
, path
);
2348 ext4_ext_drop_refs(path
);
2350 ext4_journal_stop(handle
);
2356 * called at mount time
2358 void ext4_ext_init(struct super_block
*sb
)
2361 * possible initialization would be here
2364 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2365 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2366 #ifdef AGGRESSIVE_TEST
2367 printk(", aggressive tests");
2369 #ifdef CHECK_BINSEARCH
2370 printk(", check binsearch");
2372 #ifdef EXTENTS_STATS
2376 #ifdef EXTENTS_STATS
2377 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2378 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2379 EXT4_SB(sb
)->s_ext_max
= 0;
2385 * called at umount time
2387 void ext4_ext_release(struct super_block
*sb
)
2389 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2392 #ifdef EXTENTS_STATS
2393 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2394 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2395 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2396 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2397 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2398 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2399 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2404 static void bi_complete(struct bio
*bio
, int error
)
2406 complete((struct completion
*)bio
->bi_private
);
2409 /* FIXME!! we need to try to merge to left or right after zero-out */
2410 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2414 int blkbits
, blocksize
;
2416 struct completion event
;
2417 unsigned int ee_len
, len
, done
, offset
;
2420 blkbits
= inode
->i_blkbits
;
2421 blocksize
= inode
->i_sb
->s_blocksize
;
2422 ee_len
= ext4_ext_get_actual_len(ex
);
2423 ee_pblock
= ext_pblock(ex
);
2425 /* convert ee_pblock to 512 byte sectors */
2426 ee_pblock
= ee_pblock
<< (blkbits
- 9);
2428 while (ee_len
> 0) {
2430 if (ee_len
> BIO_MAX_PAGES
)
2431 len
= BIO_MAX_PAGES
;
2435 bio
= bio_alloc(GFP_NOIO
, len
);
2436 bio
->bi_sector
= ee_pblock
;
2437 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
2441 while (done
< len
) {
2442 ret
= bio_add_page(bio
, ZERO_PAGE(0),
2444 if (ret
!= blocksize
) {
2446 * We can't add any more pages because of
2447 * hardware limitations. Start a new bio.
2452 offset
+= blocksize
;
2453 if (offset
>= PAGE_CACHE_SIZE
)
2457 init_completion(&event
);
2458 bio
->bi_private
= &event
;
2459 bio
->bi_end_io
= bi_complete
;
2460 submit_bio(WRITE
, bio
);
2461 wait_for_completion(&event
);
2463 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2471 ee_pblock
+= done
<< (blkbits
- 9);
2476 #define EXT4_EXT_ZERO_LEN 7
2478 * This function is called by ext4_ext_get_blocks() if someone tries to write
2479 * to an uninitialized extent. It may result in splitting the uninitialized
2480 * extent into multiple extents (upto three - one initialized and two
2482 * There are three possibilities:
2483 * a> There is no split required: Entire extent should be initialized
2484 * b> Splits in two extents: Write is happening at either end of the extent
2485 * c> Splits in three extents: Somone is writing in middle of the extent
2487 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2488 struct inode
*inode
,
2489 struct ext4_ext_path
*path
,
2491 unsigned int max_blocks
)
2493 struct ext4_extent
*ex
, newex
, orig_ex
;
2494 struct ext4_extent
*ex1
= NULL
;
2495 struct ext4_extent
*ex2
= NULL
;
2496 struct ext4_extent
*ex3
= NULL
;
2497 struct ext4_extent_header
*eh
;
2498 ext4_lblk_t ee_block
;
2499 unsigned int allocated
, ee_len
, depth
;
2500 ext4_fsblk_t newblock
;
2504 depth
= ext_depth(inode
);
2505 eh
= path
[depth
].p_hdr
;
2506 ex
= path
[depth
].p_ext
;
2507 ee_block
= le32_to_cpu(ex
->ee_block
);
2508 ee_len
= ext4_ext_get_actual_len(ex
);
2509 allocated
= ee_len
- (iblock
- ee_block
);
2510 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2512 orig_ex
.ee_block
= ex
->ee_block
;
2513 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2514 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2516 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2519 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2520 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
) {
2521 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2523 goto fix_extent_len
;
2524 /* update the extent length and mark as initialized */
2525 ex
->ee_block
= orig_ex
.ee_block
;
2526 ex
->ee_len
= orig_ex
.ee_len
;
2527 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2528 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2529 /* zeroed the full extent */
2533 /* ex1: ee_block to iblock - 1 : uninitialized */
2534 if (iblock
> ee_block
) {
2536 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2537 ext4_ext_mark_uninitialized(ex1
);
2541 * for sanity, update the length of the ex2 extent before
2542 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2543 * overlap of blocks.
2545 if (!ex1
&& allocated
> max_blocks
)
2546 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2547 /* ex3: to ee_block + ee_len : uninitialised */
2548 if (allocated
> max_blocks
) {
2549 unsigned int newdepth
;
2550 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2551 if (allocated
<= EXT4_EXT_ZERO_LEN
) {
2553 * iblock == ee_block is handled by the zerouout
2555 * Mark first half uninitialized.
2556 * Mark second half initialized and zero out the
2557 * initialized extent
2559 ex
->ee_block
= orig_ex
.ee_block
;
2560 ex
->ee_len
= cpu_to_le16(ee_len
- allocated
);
2561 ext4_ext_mark_uninitialized(ex
);
2562 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2563 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2566 ex3
->ee_block
= cpu_to_le32(iblock
);
2567 ext4_ext_store_pblock(ex3
, newblock
);
2568 ex3
->ee_len
= cpu_to_le16(allocated
);
2569 err
= ext4_ext_insert_extent(handle
, inode
, path
,
2571 if (err
== -ENOSPC
) {
2572 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2574 goto fix_extent_len
;
2575 ex
->ee_block
= orig_ex
.ee_block
;
2576 ex
->ee_len
= orig_ex
.ee_len
;
2577 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2578 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2579 /* blocks available from iblock */
2583 goto fix_extent_len
;
2586 * We need to zero out the second half because
2587 * an fallocate request can update file size and
2588 * converting the second half to initialized extent
2589 * implies that we can leak some junk data to user
2592 err
= ext4_ext_zeroout(inode
, ex3
);
2595 * We should actually mark the
2596 * second half as uninit and return error
2597 * Insert would have changed the extent
2599 depth
= ext_depth(inode
);
2600 ext4_ext_drop_refs(path
);
2601 path
= ext4_ext_find_extent(inode
,
2604 err
= PTR_ERR(path
);
2607 /* get the second half extent details */
2608 ex
= path
[depth
].p_ext
;
2609 err
= ext4_ext_get_access(handle
, inode
,
2613 ext4_ext_mark_uninitialized(ex
);
2614 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2618 /* zeroed the second half */
2622 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2623 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2624 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2625 ext4_ext_mark_uninitialized(ex3
);
2626 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, 0);
2627 if (err
== -ENOSPC
) {
2628 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2630 goto fix_extent_len
;
2631 /* update the extent length and mark as initialized */
2632 ex
->ee_block
= orig_ex
.ee_block
;
2633 ex
->ee_len
= orig_ex
.ee_len
;
2634 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2635 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2636 /* zeroed the full extent */
2637 /* blocks available from iblock */
2641 goto fix_extent_len
;
2643 * The depth, and hence eh & ex might change
2644 * as part of the insert above.
2646 newdepth
= ext_depth(inode
);
2648 * update the extent length after successful insert of the
2651 orig_ex
.ee_len
= cpu_to_le16(ee_len
-
2652 ext4_ext_get_actual_len(ex3
));
2654 ext4_ext_drop_refs(path
);
2655 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2657 err
= PTR_ERR(path
);
2660 eh
= path
[depth
].p_hdr
;
2661 ex
= path
[depth
].p_ext
;
2665 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2669 allocated
= max_blocks
;
2671 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2672 * to insert a extent in the middle zerout directly
2673 * otherwise give the extent a chance to merge to left
2675 if (le16_to_cpu(orig_ex
.ee_len
) <= EXT4_EXT_ZERO_LEN
&&
2676 iblock
!= ee_block
) {
2677 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2679 goto fix_extent_len
;
2680 /* update the extent length and mark as initialized */
2681 ex
->ee_block
= orig_ex
.ee_block
;
2682 ex
->ee_len
= orig_ex
.ee_len
;
2683 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2684 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2685 /* zero out the first half */
2686 /* blocks available from iblock */
2691 * If there was a change of depth as part of the
2692 * insertion of ex3 above, we need to update the length
2693 * of the ex1 extent again here
2695 if (ex1
&& ex1
!= ex
) {
2697 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2698 ext4_ext_mark_uninitialized(ex1
);
2701 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2702 ex2
->ee_block
= cpu_to_le32(iblock
);
2703 ext4_ext_store_pblock(ex2
, newblock
);
2704 ex2
->ee_len
= cpu_to_le16(allocated
);
2708 * New (initialized) extent starts from the first block
2709 * in the current extent. i.e., ex2 == ex
2710 * We have to see if it can be merged with the extent
2713 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2715 * To merge left, pass "ex2 - 1" to try_to_merge(),
2716 * since it merges towards right _only_.
2718 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2720 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2723 depth
= ext_depth(inode
);
2728 * Try to Merge towards right. This might be required
2729 * only when the whole extent is being written to.
2730 * i.e. ex2 == ex and ex3 == NULL.
2733 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2735 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2740 /* Mark modified extent as dirty */
2741 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2744 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, 0);
2745 if (err
== -ENOSPC
) {
2746 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2748 goto fix_extent_len
;
2749 /* update the extent length and mark as initialized */
2750 ex
->ee_block
= orig_ex
.ee_block
;
2751 ex
->ee_len
= orig_ex
.ee_len
;
2752 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2753 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2754 /* zero out the first half */
2757 goto fix_extent_len
;
2759 return err
? err
: allocated
;
2762 ex
->ee_block
= orig_ex
.ee_block
;
2763 ex
->ee_len
= orig_ex
.ee_len
;
2764 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2765 ext4_ext_mark_uninitialized(ex
);
2766 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2771 * This function is called by ext4_ext_get_blocks() from
2772 * ext4_get_blocks_dio_write() when DIO to write
2773 * to an uninitialized extent.
2775 * Writing to an uninitized extent may result in splitting the uninitialized
2776 * extent into multiple /intialized unintialized extents (up to three)
2777 * There are three possibilities:
2778 * a> There is no split required: Entire extent should be uninitialized
2779 * b> Splits in two extents: Write is happening at either end of the extent
2780 * c> Splits in three extents: Somone is writing in middle of the extent
2782 * One of more index blocks maybe needed if the extent tree grow after
2783 * the unintialized extent split. To prevent ENOSPC occur at the IO
2784 * complete, we need to split the uninitialized extent before DIO submit
2785 * the IO. The uninitilized extent called at this time will be split
2786 * into three uninitialized extent(at most). After IO complete, the part
2787 * being filled will be convert to initialized by the end_io callback function
2788 * via ext4_convert_unwritten_extents().
2790 * Returns the size of uninitialized extent to be written on success.
2792 static int ext4_split_unwritten_extents(handle_t
*handle
,
2793 struct inode
*inode
,
2794 struct ext4_ext_path
*path
,
2796 unsigned int max_blocks
,
2799 struct ext4_extent
*ex
, newex
, orig_ex
;
2800 struct ext4_extent
*ex1
= NULL
;
2801 struct ext4_extent
*ex2
= NULL
;
2802 struct ext4_extent
*ex3
= NULL
;
2803 struct ext4_extent_header
*eh
;
2804 ext4_lblk_t ee_block
;
2805 unsigned int allocated
, ee_len
, depth
;
2806 ext4_fsblk_t newblock
;
2809 ext_debug("ext4_split_unwritten_extents: inode %lu,"
2810 "iblock %llu, max_blocks %u\n", inode
->i_ino
,
2811 (unsigned long long)iblock
, max_blocks
);
2812 depth
= ext_depth(inode
);
2813 eh
= path
[depth
].p_hdr
;
2814 ex
= path
[depth
].p_ext
;
2815 ee_block
= le32_to_cpu(ex
->ee_block
);
2816 ee_len
= ext4_ext_get_actual_len(ex
);
2817 allocated
= ee_len
- (iblock
- ee_block
);
2818 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2820 orig_ex
.ee_block
= ex
->ee_block
;
2821 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2822 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2825 * If the uninitialized extent begins at the same logical
2826 * block where the write begins, and the write completely
2827 * covers the extent, then we don't need to split it.
2829 if ((iblock
== ee_block
) && (allocated
<= max_blocks
))
2832 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2835 /* ex1: ee_block to iblock - 1 : uninitialized */
2836 if (iblock
> ee_block
) {
2838 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2839 ext4_ext_mark_uninitialized(ex1
);
2843 * for sanity, update the length of the ex2 extent before
2844 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2845 * overlap of blocks.
2847 if (!ex1
&& allocated
> max_blocks
)
2848 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2849 /* ex3: to ee_block + ee_len : uninitialised */
2850 if (allocated
> max_blocks
) {
2851 unsigned int newdepth
;
2853 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2854 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2855 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2856 ext4_ext_mark_uninitialized(ex3
);
2857 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
, flags
);
2858 if (err
== -ENOSPC
) {
2859 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2861 goto fix_extent_len
;
2862 /* update the extent length and mark as initialized */
2863 ex
->ee_block
= orig_ex
.ee_block
;
2864 ex
->ee_len
= orig_ex
.ee_len
;
2865 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2866 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2867 /* zeroed the full extent */
2868 /* blocks available from iblock */
2872 goto fix_extent_len
;
2874 * The depth, and hence eh & ex might change
2875 * as part of the insert above.
2877 newdepth
= ext_depth(inode
);
2879 * update the extent length after successful insert of the
2882 orig_ex
.ee_len
= cpu_to_le16(ee_len
-
2883 ext4_ext_get_actual_len(ex3
));
2885 ext4_ext_drop_refs(path
);
2886 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2888 err
= PTR_ERR(path
);
2891 eh
= path
[depth
].p_hdr
;
2892 ex
= path
[depth
].p_ext
;
2896 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2900 allocated
= max_blocks
;
2903 * If there was a change of depth as part of the
2904 * insertion of ex3 above, we need to update the length
2905 * of the ex1 extent again here
2907 if (ex1
&& ex1
!= ex
) {
2909 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2910 ext4_ext_mark_uninitialized(ex1
);
2914 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2915 * uninitialised still.
2917 ex2
->ee_block
= cpu_to_le32(iblock
);
2918 ext4_ext_store_pblock(ex2
, newblock
);
2919 ex2
->ee_len
= cpu_to_le16(allocated
);
2920 ext4_ext_mark_uninitialized(ex2
);
2923 /* Mark modified extent as dirty */
2924 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2925 ext_debug("out here\n");
2928 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
2929 if (err
== -ENOSPC
) {
2930 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2932 goto fix_extent_len
;
2933 /* update the extent length and mark as initialized */
2934 ex
->ee_block
= orig_ex
.ee_block
;
2935 ex
->ee_len
= orig_ex
.ee_len
;
2936 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2937 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2938 /* zero out the first half */
2941 goto fix_extent_len
;
2943 ext4_ext_show_leaf(inode
, path
);
2944 return err
? err
: allocated
;
2947 ex
->ee_block
= orig_ex
.ee_block
;
2948 ex
->ee_len
= orig_ex
.ee_len
;
2949 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2950 ext4_ext_mark_uninitialized(ex
);
2951 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2954 static int ext4_convert_unwritten_extents_dio(handle_t
*handle
,
2955 struct inode
*inode
,
2956 struct ext4_ext_path
*path
)
2958 struct ext4_extent
*ex
;
2959 struct ext4_extent_header
*eh
;
2964 depth
= ext_depth(inode
);
2965 eh
= path
[depth
].p_hdr
;
2966 ex
= path
[depth
].p_ext
;
2968 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2971 /* first mark the extent as initialized */
2972 ext4_ext_mark_initialized(ex
);
2975 * We have to see if it can be merged with the extent
2978 if (ex
> EXT_FIRST_EXTENT(eh
)) {
2980 * To merge left, pass "ex - 1" to try_to_merge(),
2981 * since it merges towards right _only_.
2983 ret
= ext4_ext_try_to_merge(inode
, path
, ex
- 1);
2985 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2988 depth
= ext_depth(inode
);
2993 * Try to Merge towards right.
2995 ret
= ext4_ext_try_to_merge(inode
, path
, ex
);
2997 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
3000 depth
= ext_depth(inode
);
3002 /* Mark modified extent as dirty */
3003 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
3005 ext4_ext_show_leaf(inode
, path
);
3010 ext4_ext_handle_uninitialized_extents(handle_t
*handle
, struct inode
*inode
,
3011 ext4_lblk_t iblock
, unsigned int max_blocks
,
3012 struct ext4_ext_path
*path
, int flags
,
3013 unsigned int allocated
, struct buffer_head
*bh_result
,
3014 ext4_fsblk_t newblock
)
3018 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3020 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3021 "block %llu, max_blocks %u, flags %d, allocated %u",
3022 inode
->i_ino
, (unsigned long long)iblock
, max_blocks
,
3024 ext4_ext_show_leaf(inode
, path
);
3026 /* DIO get_block() before submit the IO, split the extent */
3027 if (flags
== EXT4_GET_BLOCKS_DIO_CREATE_EXT
) {
3028 ret
= ext4_split_unwritten_extents(handle
,
3029 inode
, path
, iblock
,
3032 * Flag the inode(non aio case) or end_io struct (aio case)
3033 * that this IO needs to convertion to written when IO is
3037 io
->flag
= DIO_AIO_UNWRITTEN
;
3039 EXT4_I(inode
)->i_state
|= EXT4_STATE_DIO_UNWRITTEN
;
3042 /* async DIO end_io complete, convert the filled extent to written */
3043 if (flags
== EXT4_GET_BLOCKS_DIO_CONVERT_EXT
) {
3044 ret
= ext4_convert_unwritten_extents_dio(handle
, inode
,
3047 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3050 /* buffered IO case */
3052 * repeat fallocate creation request
3053 * we already have an unwritten extent
3055 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
3058 /* buffered READ or buffered write_begin() lookup */
3059 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3061 * We have blocks reserved already. We
3062 * return allocated blocks so that delalloc
3063 * won't do block reservation for us. But
3064 * the buffer head will be unmapped so that
3065 * a read from the block returns 0s.
3067 set_buffer_unwritten(bh_result
);
3071 /* buffered write, writepage time, convert*/
3072 ret
= ext4_ext_convert_to_initialized(handle
, inode
,
3076 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3083 set_buffer_new(bh_result
);
3085 set_buffer_mapped(bh_result
);
3087 if (allocated
> max_blocks
)
3088 allocated
= max_blocks
;
3089 ext4_ext_show_leaf(inode
, path
);
3090 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
3091 bh_result
->b_blocknr
= newblock
;
3094 ext4_ext_drop_refs(path
);
3097 return err
? err
: allocated
;
3100 * Block allocation/map/preallocation routine for extents based files
3103 * Need to be called with
3104 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3105 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3107 * return > 0, number of of blocks already mapped/allocated
3108 * if create == 0 and these are pre-allocated blocks
3109 * buffer head is unmapped
3110 * otherwise blocks are mapped
3112 * return = 0, if plain look up failed (blocks have not been allocated)
3113 * buffer head is unmapped
3115 * return < 0, error case.
3117 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
3119 unsigned int max_blocks
, struct buffer_head
*bh_result
,
3122 struct ext4_ext_path
*path
= NULL
;
3123 struct ext4_extent_header
*eh
;
3124 struct ext4_extent newex
, *ex
;
3125 ext4_fsblk_t newblock
;
3126 int err
= 0, depth
, ret
, cache_type
;
3127 unsigned int allocated
= 0;
3128 struct ext4_allocation_request ar
;
3129 ext4_io_end_t
*io
= EXT4_I(inode
)->cur_aio_dio
;
3131 __clear_bit(BH_New
, &bh_result
->b_state
);
3132 ext_debug("blocks %u/%u requested for inode %u\n",
3133 iblock
, max_blocks
, inode
->i_ino
);
3135 /* check in cache */
3136 cache_type
= ext4_ext_in_cache(inode
, iblock
, &newex
);
3138 if (cache_type
== EXT4_EXT_CACHE_GAP
) {
3139 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3141 * block isn't allocated yet and
3142 * user doesn't want to allocate it
3146 /* we should allocate requested block */
3147 } else if (cache_type
== EXT4_EXT_CACHE_EXTENT
) {
3148 /* block is already allocated */
3150 - le32_to_cpu(newex
.ee_block
)
3151 + ext_pblock(&newex
);
3152 /* number of remaining blocks in the extent */
3153 allocated
= ext4_ext_get_actual_len(&newex
) -
3154 (iblock
- le32_to_cpu(newex
.ee_block
));
3161 /* find extent for this block */
3162 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
3164 err
= PTR_ERR(path
);
3169 depth
= ext_depth(inode
);
3172 * consistent leaf must not be empty;
3173 * this situation is possible, though, _during_ tree modification;
3174 * this is why assert can't be put in ext4_ext_find_extent()
3176 BUG_ON(path
[depth
].p_ext
== NULL
&& depth
!= 0);
3177 eh
= path
[depth
].p_hdr
;
3179 ex
= path
[depth
].p_ext
;
3181 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
3182 ext4_fsblk_t ee_start
= ext_pblock(ex
);
3183 unsigned short ee_len
;
3186 * Uninitialized extents are treated as holes, except that
3187 * we split out initialized portions during a write.
3189 ee_len
= ext4_ext_get_actual_len(ex
);
3190 /* if found extent covers block, simply return it */
3191 if (iblock
>= ee_block
&& iblock
< ee_block
+ ee_len
) {
3192 newblock
= iblock
- ee_block
+ ee_start
;
3193 /* number of remaining blocks in the extent */
3194 allocated
= ee_len
- (iblock
- ee_block
);
3195 ext_debug("%u fit into %lu:%d -> %llu\n", iblock
,
3196 ee_block
, ee_len
, newblock
);
3198 /* Do not put uninitialized extent in the cache */
3199 if (!ext4_ext_is_uninitialized(ex
)) {
3200 ext4_ext_put_in_cache(inode
, ee_block
,
3202 EXT4_EXT_CACHE_EXTENT
);
3205 ret
= ext4_ext_handle_uninitialized_extents(handle
,
3206 inode
, iblock
, max_blocks
, path
,
3207 flags
, allocated
, bh_result
, newblock
);
3213 * requested block isn't allocated yet;
3214 * we couldn't try to create block if create flag is zero
3216 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
3218 * put just found gap into cache to speed up
3219 * subsequent requests
3221 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
3225 * Okay, we need to do block allocation.
3228 /* find neighbour allocated blocks */
3230 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
3234 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
3239 * See if request is beyond maximum number of blocks we can have in
3240 * a single extent. For an initialized extent this limit is
3241 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3242 * EXT_UNINIT_MAX_LEN.
3244 if (max_blocks
> EXT_INIT_MAX_LEN
&&
3245 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3246 max_blocks
= EXT_INIT_MAX_LEN
;
3247 else if (max_blocks
> EXT_UNINIT_MAX_LEN
&&
3248 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
3249 max_blocks
= EXT_UNINIT_MAX_LEN
;
3251 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3252 newex
.ee_block
= cpu_to_le32(iblock
);
3253 newex
.ee_len
= cpu_to_le16(max_blocks
);
3254 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
3256 allocated
= ext4_ext_get_actual_len(&newex
);
3258 allocated
= max_blocks
;
3260 /* allocate new block */
3262 ar
.goal
= ext4_ext_find_goal(inode
, path
, iblock
);
3263 ar
.logical
= iblock
;
3265 if (S_ISREG(inode
->i_mode
))
3266 ar
.flags
= EXT4_MB_HINT_DATA
;
3268 /* disable in-core preallocation for non-regular files */
3270 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
3273 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
3274 ar
.goal
, newblock
, allocated
);
3276 /* try to insert new extent into found leaf and return */
3277 ext4_ext_store_pblock(&newex
, newblock
);
3278 newex
.ee_len
= cpu_to_le16(ar
.len
);
3279 /* Mark uninitialized */
3280 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
){
3281 ext4_ext_mark_uninitialized(&newex
);
3283 * io_end structure was created for every async
3284 * direct IO write to the middle of the file.
3285 * To avoid unecessary convertion for every aio dio rewrite
3286 * to the mid of file, here we flag the IO that is really
3287 * need the convertion.
3288 * For non asycn direct IO case, flag the inode state
3289 * that we need to perform convertion when IO is done.
3291 if (flags
== EXT4_GET_BLOCKS_DIO_CREATE_EXT
) {
3293 io
->flag
= DIO_AIO_UNWRITTEN
;
3295 EXT4_I(inode
)->i_state
|=
3296 EXT4_STATE_DIO_UNWRITTEN
;;
3299 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
, flags
);
3301 /* free data blocks we just allocated */
3302 /* not a good idea to call discard here directly,
3303 * but otherwise we'd need to call it every free() */
3304 ext4_discard_preallocations(inode
);
3305 ext4_free_blocks(handle
, inode
, ext_pblock(&newex
),
3306 ext4_ext_get_actual_len(&newex
), 0);
3310 /* previous routine could use block we allocated */
3311 newblock
= ext_pblock(&newex
);
3312 allocated
= ext4_ext_get_actual_len(&newex
);
3313 set_buffer_new(bh_result
);
3316 * Cache the extent and update transaction to commit on fdatasync only
3317 * when it is _not_ an uninitialized extent.
3319 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0) {
3320 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
3321 EXT4_EXT_CACHE_EXTENT
);
3322 ext4_update_inode_fsync_trans(handle
, inode
, 1);
3324 ext4_update_inode_fsync_trans(handle
, inode
, 0);
3326 if (allocated
> max_blocks
)
3327 allocated
= max_blocks
;
3328 ext4_ext_show_leaf(inode
, path
);
3329 set_buffer_mapped(bh_result
);
3330 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
3331 bh_result
->b_blocknr
= newblock
;
3334 ext4_ext_drop_refs(path
);
3337 return err
? err
: allocated
;
3340 void ext4_ext_truncate(struct inode
*inode
)
3342 struct address_space
*mapping
= inode
->i_mapping
;
3343 struct super_block
*sb
= inode
->i_sb
;
3344 ext4_lblk_t last_block
;
3349 * probably first extent we're gonna free will be last in block
3351 err
= ext4_writepage_trans_blocks(inode
);
3352 handle
= ext4_journal_start(inode
, err
);
3356 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3357 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3359 if (ext4_orphan_add(handle
, inode
))
3362 down_write(&EXT4_I(inode
)->i_data_sem
);
3363 ext4_ext_invalidate_cache(inode
);
3365 ext4_discard_preallocations(inode
);
3368 * TODO: optimization is possible here.
3369 * Probably we need not scan at all,
3370 * because page truncation is enough.
3373 /* we have to know where to truncate from in crash case */
3374 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3375 ext4_mark_inode_dirty(handle
, inode
);
3377 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3378 >> EXT4_BLOCK_SIZE_BITS(sb
);
3379 err
= ext4_ext_remove_space(inode
, last_block
);
3381 /* In a multi-transaction truncate, we only make the final
3382 * transaction synchronous.
3385 ext4_handle_sync(handle
);
3388 up_write(&EXT4_I(inode
)->i_data_sem
);
3390 * If this was a simple ftruncate() and the file will remain alive,
3391 * then we need to clear up the orphan record which we created above.
3392 * However, if this was a real unlink then we were called by
3393 * ext4_delete_inode(), and we allow that function to clean up the
3394 * orphan info for us.
3397 ext4_orphan_del(handle
, inode
);
3399 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3400 ext4_mark_inode_dirty(handle
, inode
);
3401 ext4_journal_stop(handle
);
3404 static void ext4_falloc_update_inode(struct inode
*inode
,
3405 int mode
, loff_t new_size
, int update_ctime
)
3407 struct timespec now
;
3410 now
= current_fs_time(inode
->i_sb
);
3411 if (!timespec_equal(&inode
->i_ctime
, &now
))
3412 inode
->i_ctime
= now
;
3415 * Update only when preallocation was requested beyond
3418 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3419 if (new_size
> i_size_read(inode
))
3420 i_size_write(inode
, new_size
);
3421 if (new_size
> EXT4_I(inode
)->i_disksize
)
3422 ext4_update_i_disksize(inode
, new_size
);
3428 * preallocate space for a file. This implements ext4's fallocate inode
3429 * operation, which gets called from sys_fallocate system call.
3430 * For block-mapped files, posix_fallocate should fall back to the method
3431 * of writing zeroes to the required new blocks (the same behavior which is
3432 * expected for file systems which do not support fallocate() system call).
3434 long ext4_fallocate(struct inode
*inode
, int mode
, loff_t offset
, loff_t len
)
3439 unsigned int max_blocks
;
3443 struct buffer_head map_bh
;
3444 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3447 * currently supporting (pre)allocate mode for extent-based
3450 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3453 /* preallocation to directories is currently not supported */
3454 if (S_ISDIR(inode
->i_mode
))
3457 block
= offset
>> blkbits
;
3459 * We can't just convert len to max_blocks because
3460 * If blocksize = 4096 offset = 3072 and len = 2048
3462 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3465 * credits to insert 1 extent into extent tree
3467 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3468 mutex_lock(&inode
->i_mutex
);
3470 while (ret
>= 0 && ret
< max_blocks
) {
3471 block
= block
+ ret
;
3472 max_blocks
= max_blocks
- ret
;
3473 handle
= ext4_journal_start(inode
, credits
);
3474 if (IS_ERR(handle
)) {
3475 ret
= PTR_ERR(handle
);
3479 ret
= ext4_get_blocks(handle
, inode
, block
,
3480 max_blocks
, &map_bh
,
3481 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
);
3485 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3486 "returned error inode#%lu, block=%u, "
3487 "max_blocks=%u", __func__
,
3488 inode
->i_ino
, block
, max_blocks
);
3490 ext4_mark_inode_dirty(handle
, inode
);
3491 ret2
= ext4_journal_stop(handle
);
3494 if ((block
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3495 blkbits
) >> blkbits
))
3496 new_size
= offset
+ len
;
3498 new_size
= (block
+ ret
) << blkbits
;
3500 ext4_falloc_update_inode(inode
, mode
, new_size
,
3501 buffer_new(&map_bh
));
3502 ext4_mark_inode_dirty(handle
, inode
);
3503 ret2
= ext4_journal_stop(handle
);
3507 if (ret
== -ENOSPC
&&
3508 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3512 mutex_unlock(&inode
->i_mutex
);
3513 return ret
> 0 ? ret2
: ret
;
3517 * This function convert a range of blocks to written extents
3518 * The caller of this function will pass the start offset and the size.
3519 * all unwritten extents within this range will be converted to
3522 * This function is called from the direct IO end io call back
3523 * function, to convert the fallocated extents after IO is completed.
3524 * Returns 0 on success.
3526 int ext4_convert_unwritten_extents(struct inode
*inode
, loff_t offset
,
3531 unsigned int max_blocks
;
3534 struct buffer_head map_bh
;
3535 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3537 block
= offset
>> blkbits
;
3539 * We can't just convert len to max_blocks because
3540 * If blocksize = 4096 offset = 3072 and len = 2048
3542 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3545 * credits to insert 1 extent into extent tree
3547 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3548 while (ret
>= 0 && ret
< max_blocks
) {
3549 block
= block
+ ret
;
3550 max_blocks
= max_blocks
- ret
;
3551 handle
= ext4_journal_start(inode
, credits
);
3552 if (IS_ERR(handle
)) {
3553 ret
= PTR_ERR(handle
);
3557 ret
= ext4_get_blocks(handle
, inode
, block
,
3558 max_blocks
, &map_bh
,
3559 EXT4_GET_BLOCKS_DIO_CONVERT_EXT
);
3562 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3563 "returned error inode#%lu, block=%u, "
3564 "max_blocks=%u", __func__
,
3565 inode
->i_ino
, block
, max_blocks
);
3567 ext4_mark_inode_dirty(handle
, inode
);
3568 ret2
= ext4_journal_stop(handle
);
3569 if (ret
<= 0 || ret2
)
3572 return ret
> 0 ? ret2
: ret
;
3575 * Callback function called for each extent to gather FIEMAP information.
3577 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3578 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3581 struct fiemap_extent_info
*fieinfo
= data
;
3582 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3589 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3591 if (newex
->ec_type
== EXT4_EXT_CACHE_GAP
) {
3594 struct buffer_head
*bh
= NULL
;
3596 offset
= logical
>> PAGE_SHIFT
;
3597 page
= find_get_page(inode
->i_mapping
, offset
);
3598 if (!page
|| !page_has_buffers(page
))
3599 return EXT_CONTINUE
;
3601 bh
= page_buffers(page
);
3604 return EXT_CONTINUE
;
3606 if (buffer_delay(bh
)) {
3607 flags
|= FIEMAP_EXTENT_DELALLOC
;
3608 page_cache_release(page
);
3610 page_cache_release(page
);
3611 return EXT_CONTINUE
;
3615 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3616 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3618 if (ex
&& ext4_ext_is_uninitialized(ex
))
3619 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3622 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3624 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3625 * this also indicates no more allocated blocks.
3627 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3629 if (ext4_ext_next_allocated_block(path
) == EXT_MAX_BLOCK
||
3630 newex
->ec_block
+ newex
->ec_len
- 1 == EXT_MAX_BLOCK
) {
3631 loff_t size
= i_size_read(inode
);
3632 loff_t bs
= EXT4_BLOCK_SIZE(inode
->i_sb
);
3634 flags
|= FIEMAP_EXTENT_LAST
;
3635 if ((flags
& FIEMAP_EXTENT_DELALLOC
) &&
3636 logical
+length
> size
)
3637 length
= (size
- logical
+ bs
- 1) & ~(bs
-1);
3640 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3647 return EXT_CONTINUE
;
3650 /* fiemap flags we can handle specified here */
3651 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3653 static int ext4_xattr_fiemap(struct inode
*inode
,
3654 struct fiemap_extent_info
*fieinfo
)
3658 __u32 flags
= FIEMAP_EXTENT_LAST
;
3659 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3663 if (EXT4_I(inode
)->i_state
& EXT4_STATE_XATTR
) {
3664 struct ext4_iloc iloc
;
3665 int offset
; /* offset of xattr in inode */
3667 error
= ext4_get_inode_loc(inode
, &iloc
);
3670 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3671 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3672 EXT4_I(inode
)->i_extra_isize
;
3674 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3675 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3676 } else { /* external block */
3677 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3678 length
= inode
->i_sb
->s_blocksize
;
3682 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3684 return (error
< 0 ? error
: 0);
3687 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3688 __u64 start
, __u64 len
)
3690 ext4_lblk_t start_blk
;
3691 ext4_lblk_t len_blks
;
3694 /* fallback to generic here if not in extents fmt */
3695 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3696 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3699 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3702 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3703 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3705 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3706 len_blks
= len
>> inode
->i_sb
->s_blocksize_bits
;
3709 * Walk the extent tree gathering extent information.
3710 * ext4_ext_fiemap_cb will push extents back to user.
3712 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3713 ext4_ext_fiemap_cb
, fieinfo
);