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 static 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_journal_restart(handle_t
*handle
, int needed
)
100 if (!ext4_handle_valid(handle
))
102 if (handle
->h_buffer_credits
> needed
)
104 err
= ext4_journal_extend(handle
, needed
);
107 return ext4_journal_restart(handle
, needed
);
115 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
116 struct ext4_ext_path
*path
)
119 /* path points to block */
120 return ext4_journal_get_write_access(handle
, path
->p_bh
);
122 /* path points to leaf/index in inode body */
123 /* we use in-core data, no need to protect them */
133 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
134 struct ext4_ext_path
*path
)
138 /* path points to block */
139 err
= ext4_handle_dirty_metadata(handle
, inode
, path
->p_bh
);
141 /* path points to leaf/index in inode body */
142 err
= ext4_mark_inode_dirty(handle
, inode
);
147 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
148 struct ext4_ext_path
*path
,
151 struct ext4_inode_info
*ei
= EXT4_I(inode
);
152 ext4_fsblk_t bg_start
;
153 ext4_fsblk_t last_block
;
154 ext4_grpblk_t colour
;
155 ext4_group_t block_group
;
156 int flex_size
= ext4_flex_bg_size(EXT4_SB(inode
->i_sb
));
160 struct ext4_extent
*ex
;
161 depth
= path
->p_depth
;
163 /* try to predict block placement */
164 ex
= path
[depth
].p_ext
;
166 return ext_pblock(ex
)+(block
-le32_to_cpu(ex
->ee_block
));
168 /* it looks like index is empty;
169 * try to find starting block from index itself */
170 if (path
[depth
].p_bh
)
171 return path
[depth
].p_bh
->b_blocknr
;
174 /* OK. use inode's group */
175 block_group
= ei
->i_block_group
;
176 if (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) {
178 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
179 * block groups per flexgroup, reserve the first block
180 * group for directories and special files. Regular
181 * files will start at the second block group. This
182 * tends to speed up directory access and improves
185 block_group
&= ~(flex_size
-1);
186 if (S_ISREG(inode
->i_mode
))
189 bg_start
= (block_group
* EXT4_BLOCKS_PER_GROUP(inode
->i_sb
)) +
190 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_first_data_block
);
191 last_block
= ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
) - 1;
194 * If we are doing delayed allocation, we don't need take
195 * colour into account.
197 if (test_opt(inode
->i_sb
, DELALLOC
))
200 if (bg_start
+ EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) <= last_block
)
201 colour
= (current
->pid
% 16) *
202 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
204 colour
= (current
->pid
% 16) * ((last_block
- bg_start
) / 16);
205 return bg_start
+ colour
+ block
;
209 * Allocation for a meta data block
212 ext4_ext_new_meta_block(handle_t
*handle
, struct inode
*inode
,
213 struct ext4_ext_path
*path
,
214 struct ext4_extent
*ex
, int *err
)
216 ext4_fsblk_t goal
, newblock
;
218 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
219 newblock
= ext4_new_meta_blocks(handle
, inode
, goal
, NULL
, err
);
223 static int ext4_ext_space_block(struct inode
*inode
)
227 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
228 / sizeof(struct ext4_extent
);
229 #ifdef AGGRESSIVE_TEST
236 static int ext4_ext_space_block_idx(struct inode
*inode
)
240 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
241 / sizeof(struct ext4_extent_idx
);
242 #ifdef AGGRESSIVE_TEST
249 static int ext4_ext_space_root(struct inode
*inode
)
253 size
= sizeof(EXT4_I(inode
)->i_data
);
254 size
-= sizeof(struct ext4_extent_header
);
255 size
/= sizeof(struct ext4_extent
);
256 #ifdef AGGRESSIVE_TEST
263 static int ext4_ext_space_root_idx(struct inode
*inode
)
267 size
= sizeof(EXT4_I(inode
)->i_data
);
268 size
-= sizeof(struct ext4_extent_header
);
269 size
/= sizeof(struct ext4_extent_idx
);
270 #ifdef AGGRESSIVE_TEST
278 * Calculate the number of metadata blocks needed
279 * to allocate @blocks
280 * Worse case is one block per extent
282 int ext4_ext_calc_metadata_amount(struct inode
*inode
, int blocks
)
284 int lcap
, icap
, rcap
, leafs
, idxs
, num
;
285 int newextents
= blocks
;
287 rcap
= ext4_ext_space_root_idx(inode
);
288 lcap
= ext4_ext_space_block(inode
);
289 icap
= ext4_ext_space_block_idx(inode
);
291 /* number of new leaf blocks needed */
292 num
= leafs
= (newextents
+ lcap
- 1) / lcap
;
295 * Worse case, we need separate index block(s)
296 * to link all new leaf blocks
298 idxs
= (leafs
+ icap
- 1) / icap
;
301 idxs
= (idxs
+ icap
- 1) / icap
;
302 } while (idxs
> rcap
);
308 ext4_ext_max_entries(struct inode
*inode
, int depth
)
312 if (depth
== ext_depth(inode
)) {
314 max
= ext4_ext_space_root(inode
);
316 max
= ext4_ext_space_root_idx(inode
);
319 max
= ext4_ext_space_block(inode
);
321 max
= ext4_ext_space_block_idx(inode
);
327 static int ext4_valid_extent(struct inode
*inode
, struct ext4_extent
*ext
)
329 ext4_fsblk_t block
= ext_pblock(ext
);
330 int len
= ext4_ext_get_actual_len(ext
);
331 struct ext4_super_block
*es
= EXT4_SB(inode
->i_sb
)->s_es
;
332 if (unlikely(block
< le32_to_cpu(es
->s_first_data_block
) ||
333 ((block
+ len
) > ext4_blocks_count(es
))))
339 static int ext4_valid_extent_idx(struct inode
*inode
,
340 struct ext4_extent_idx
*ext_idx
)
342 ext4_fsblk_t block
= idx_pblock(ext_idx
);
343 struct ext4_super_block
*es
= EXT4_SB(inode
->i_sb
)->s_es
;
344 if (unlikely(block
< le32_to_cpu(es
->s_first_data_block
) ||
345 (block
>= ext4_blocks_count(es
))))
351 static int ext4_valid_extent_entries(struct inode
*inode
,
352 struct ext4_extent_header
*eh
,
355 struct ext4_extent
*ext
;
356 struct ext4_extent_idx
*ext_idx
;
357 unsigned short entries
;
358 if (eh
->eh_entries
== 0)
361 entries
= le16_to_cpu(eh
->eh_entries
);
365 ext
= EXT_FIRST_EXTENT(eh
);
367 if (!ext4_valid_extent(inode
, ext
))
373 ext_idx
= EXT_FIRST_INDEX(eh
);
375 if (!ext4_valid_extent_idx(inode
, ext_idx
))
384 static int __ext4_ext_check(const char *function
, struct inode
*inode
,
385 struct ext4_extent_header
*eh
,
388 const char *error_msg
;
391 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
392 error_msg
= "invalid magic";
395 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
396 error_msg
= "unexpected eh_depth";
399 if (unlikely(eh
->eh_max
== 0)) {
400 error_msg
= "invalid eh_max";
403 max
= ext4_ext_max_entries(inode
, depth
);
404 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
405 error_msg
= "too large eh_max";
408 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
409 error_msg
= "invalid eh_entries";
412 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
413 error_msg
= "invalid extent entries";
419 ext4_error(inode
->i_sb
, function
,
420 "bad header/extent in inode #%lu: %s - magic %x, "
421 "entries %u, max %u(%u), depth %u(%u)",
422 inode
->i_ino
, error_msg
, le16_to_cpu(eh
->eh_magic
),
423 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
424 max
, le16_to_cpu(eh
->eh_depth
), depth
);
429 #define ext4_ext_check(inode, eh, depth) \
430 __ext4_ext_check(__func__, inode, eh, depth)
432 int ext4_ext_check_inode(struct inode
*inode
)
434 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
438 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
440 int k
, l
= path
->p_depth
;
443 for (k
= 0; k
<= l
; k
++, path
++) {
445 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
446 idx_pblock(path
->p_idx
));
447 } else if (path
->p_ext
) {
448 ext_debug(" %d:%d:%llu ",
449 le32_to_cpu(path
->p_ext
->ee_block
),
450 ext4_ext_get_actual_len(path
->p_ext
),
451 ext_pblock(path
->p_ext
));
458 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
460 int depth
= ext_depth(inode
);
461 struct ext4_extent_header
*eh
;
462 struct ext4_extent
*ex
;
468 eh
= path
[depth
].p_hdr
;
469 ex
= EXT_FIRST_EXTENT(eh
);
471 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
472 ext_debug("%d:%d:%llu ", le32_to_cpu(ex
->ee_block
),
473 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
478 #define ext4_ext_show_path(inode, path)
479 #define ext4_ext_show_leaf(inode, path)
482 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
484 int depth
= path
->p_depth
;
487 for (i
= 0; i
<= depth
; i
++, path
++)
495 * ext4_ext_binsearch_idx:
496 * binary search for the closest index of the given block
497 * the header must be checked before calling this
500 ext4_ext_binsearch_idx(struct inode
*inode
,
501 struct ext4_ext_path
*path
, ext4_lblk_t block
)
503 struct ext4_extent_header
*eh
= path
->p_hdr
;
504 struct ext4_extent_idx
*r
, *l
, *m
;
507 ext_debug("binsearch for %u(idx): ", block
);
509 l
= EXT_FIRST_INDEX(eh
) + 1;
510 r
= EXT_LAST_INDEX(eh
);
513 if (block
< le32_to_cpu(m
->ei_block
))
517 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
518 m
, le32_to_cpu(m
->ei_block
),
519 r
, le32_to_cpu(r
->ei_block
));
523 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
524 idx_pblock(path
->p_idx
));
526 #ifdef CHECK_BINSEARCH
528 struct ext4_extent_idx
*chix
, *ix
;
531 chix
= ix
= EXT_FIRST_INDEX(eh
);
532 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
534 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
535 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
537 ix
, EXT_FIRST_INDEX(eh
));
538 printk(KERN_DEBUG
"%u <= %u\n",
539 le32_to_cpu(ix
->ei_block
),
540 le32_to_cpu(ix
[-1].ei_block
));
542 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
543 <= le32_to_cpu(ix
[-1].ei_block
));
544 if (block
< le32_to_cpu(ix
->ei_block
))
548 BUG_ON(chix
!= path
->p_idx
);
555 * ext4_ext_binsearch:
556 * binary search for closest extent of the given block
557 * the header must be checked before calling this
560 ext4_ext_binsearch(struct inode
*inode
,
561 struct ext4_ext_path
*path
, ext4_lblk_t block
)
563 struct ext4_extent_header
*eh
= path
->p_hdr
;
564 struct ext4_extent
*r
, *l
, *m
;
566 if (eh
->eh_entries
== 0) {
568 * this leaf is empty:
569 * we get such a leaf in split/add case
574 ext_debug("binsearch for %u: ", block
);
576 l
= EXT_FIRST_EXTENT(eh
) + 1;
577 r
= EXT_LAST_EXTENT(eh
);
581 if (block
< le32_to_cpu(m
->ee_block
))
585 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
586 m
, le32_to_cpu(m
->ee_block
),
587 r
, le32_to_cpu(r
->ee_block
));
591 ext_debug(" -> %d:%llu:%d ",
592 le32_to_cpu(path
->p_ext
->ee_block
),
593 ext_pblock(path
->p_ext
),
594 ext4_ext_get_actual_len(path
->p_ext
));
596 #ifdef CHECK_BINSEARCH
598 struct ext4_extent
*chex
, *ex
;
601 chex
= ex
= EXT_FIRST_EXTENT(eh
);
602 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
603 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
604 <= le32_to_cpu(ex
[-1].ee_block
));
605 if (block
< le32_to_cpu(ex
->ee_block
))
609 BUG_ON(chex
!= path
->p_ext
);
615 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
617 struct ext4_extent_header
*eh
;
619 eh
= ext_inode_hdr(inode
);
622 eh
->eh_magic
= EXT4_EXT_MAGIC
;
623 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
));
624 ext4_mark_inode_dirty(handle
, inode
);
625 ext4_ext_invalidate_cache(inode
);
629 struct ext4_ext_path
*
630 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
631 struct ext4_ext_path
*path
)
633 struct ext4_extent_header
*eh
;
634 struct buffer_head
*bh
;
635 short int depth
, i
, ppos
= 0, alloc
= 0;
637 eh
= ext_inode_hdr(inode
);
638 depth
= ext_depth(inode
);
640 /* account possible depth increase */
642 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
645 return ERR_PTR(-ENOMEM
);
652 /* walk through the tree */
654 int need_to_validate
= 0;
656 ext_debug("depth %d: num %d, max %d\n",
657 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
659 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
660 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
661 path
[ppos
].p_depth
= i
;
662 path
[ppos
].p_ext
= NULL
;
664 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
667 if (!bh_uptodate_or_lock(bh
)) {
668 if (bh_submit_read(bh
) < 0) {
672 /* validate the extent entries */
673 need_to_validate
= 1;
675 eh
= ext_block_hdr(bh
);
677 BUG_ON(ppos
> depth
);
678 path
[ppos
].p_bh
= bh
;
679 path
[ppos
].p_hdr
= eh
;
682 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
686 path
[ppos
].p_depth
= i
;
687 path
[ppos
].p_ext
= NULL
;
688 path
[ppos
].p_idx
= NULL
;
691 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
692 /* if not an empty leaf */
693 if (path
[ppos
].p_ext
)
694 path
[ppos
].p_block
= ext_pblock(path
[ppos
].p_ext
);
696 ext4_ext_show_path(inode
, path
);
701 ext4_ext_drop_refs(path
);
704 return ERR_PTR(-EIO
);
708 * ext4_ext_insert_index:
709 * insert new index [@logical;@ptr] into the block at @curp;
710 * check where to insert: before @curp or after @curp
712 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
713 struct ext4_ext_path
*curp
,
714 int logical
, ext4_fsblk_t ptr
)
716 struct ext4_extent_idx
*ix
;
719 err
= ext4_ext_get_access(handle
, inode
, curp
);
723 BUG_ON(logical
== le32_to_cpu(curp
->p_idx
->ei_block
));
724 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
725 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
727 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
728 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
729 len
= len
< 0 ? 0 : len
;
730 ext_debug("insert new index %d after: %llu. "
731 "move %d from 0x%p to 0x%p\n",
733 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
734 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
736 ix
= curp
->p_idx
+ 1;
739 len
= len
* sizeof(struct ext4_extent_idx
);
740 len
= len
< 0 ? 0 : len
;
741 ext_debug("insert new index %d before: %llu. "
742 "move %d from 0x%p to 0x%p\n",
744 curp
->p_idx
, (curp
->p_idx
+ 1));
745 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
749 ix
->ei_block
= cpu_to_le32(logical
);
750 ext4_idx_store_pblock(ix
, ptr
);
751 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
753 BUG_ON(le16_to_cpu(curp
->p_hdr
->eh_entries
)
754 > le16_to_cpu(curp
->p_hdr
->eh_max
));
755 BUG_ON(ix
> EXT_LAST_INDEX(curp
->p_hdr
));
757 err
= ext4_ext_dirty(handle
, inode
, curp
);
758 ext4_std_error(inode
->i_sb
, err
);
765 * inserts new subtree into the path, using free index entry
767 * - allocates all needed blocks (new leaf and all intermediate index blocks)
768 * - makes decision where to split
769 * - moves remaining extents and index entries (right to the split point)
770 * into the newly allocated blocks
771 * - initializes subtree
773 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
774 struct ext4_ext_path
*path
,
775 struct ext4_extent
*newext
, int at
)
777 struct buffer_head
*bh
= NULL
;
778 int depth
= ext_depth(inode
);
779 struct ext4_extent_header
*neh
;
780 struct ext4_extent_idx
*fidx
;
781 struct ext4_extent
*ex
;
783 ext4_fsblk_t newblock
, oldblock
;
785 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
788 /* make decision: where to split? */
789 /* FIXME: now decision is simplest: at current extent */
791 /* if current leaf will be split, then we should use
792 * border from split point */
793 BUG_ON(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
));
794 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
795 border
= path
[depth
].p_ext
[1].ee_block
;
796 ext_debug("leaf will be split."
797 " next leaf starts at %d\n",
798 le32_to_cpu(border
));
800 border
= newext
->ee_block
;
801 ext_debug("leaf will be added."
802 " next leaf starts at %d\n",
803 le32_to_cpu(border
));
807 * If error occurs, then we break processing
808 * and mark filesystem read-only. index won't
809 * be inserted and tree will be in consistent
810 * state. Next mount will repair buffers too.
814 * Get array to track all allocated blocks.
815 * We need this to handle errors and free blocks
818 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
822 /* allocate all needed blocks */
823 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
824 for (a
= 0; a
< depth
- at
; a
++) {
825 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
829 ablocks
[a
] = newblock
;
832 /* initialize new leaf */
833 newblock
= ablocks
[--a
];
834 BUG_ON(newblock
== 0);
835 bh
= sb_getblk(inode
->i_sb
, newblock
);
842 err
= ext4_journal_get_create_access(handle
, bh
);
846 neh
= ext_block_hdr(bh
);
848 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
849 neh
->eh_magic
= EXT4_EXT_MAGIC
;
851 ex
= EXT_FIRST_EXTENT(neh
);
853 /* move remainder of path[depth] to the new leaf */
854 BUG_ON(path
[depth
].p_hdr
->eh_entries
!= path
[depth
].p_hdr
->eh_max
);
855 /* start copy from next extent */
856 /* TODO: we could do it by single memmove */
859 while (path
[depth
].p_ext
<=
860 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
861 ext_debug("move %d:%llu:%d in new leaf %llu\n",
862 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
863 ext_pblock(path
[depth
].p_ext
),
864 ext4_ext_get_actual_len(path
[depth
].p_ext
),
866 /*memmove(ex++, path[depth].p_ext++,
867 sizeof(struct ext4_extent));
873 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
874 le16_add_cpu(&neh
->eh_entries
, m
);
877 set_buffer_uptodate(bh
);
880 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
886 /* correct old leaf */
888 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
891 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
892 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
898 /* create intermediate indexes */
902 ext_debug("create %d intermediate indices\n", k
);
903 /* insert new index into current index block */
904 /* current depth stored in i var */
908 newblock
= ablocks
[--a
];
909 bh
= sb_getblk(inode
->i_sb
, newblock
);
916 err
= ext4_journal_get_create_access(handle
, bh
);
920 neh
= ext_block_hdr(bh
);
921 neh
->eh_entries
= cpu_to_le16(1);
922 neh
->eh_magic
= EXT4_EXT_MAGIC
;
923 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
924 neh
->eh_depth
= cpu_to_le16(depth
- i
);
925 fidx
= EXT_FIRST_INDEX(neh
);
926 fidx
->ei_block
= border
;
927 ext4_idx_store_pblock(fidx
, oldblock
);
929 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
930 i
, newblock
, le32_to_cpu(border
), oldblock
);
935 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
936 EXT_MAX_INDEX(path
[i
].p_hdr
));
937 BUG_ON(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
938 EXT_LAST_INDEX(path
[i
].p_hdr
));
939 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
940 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
941 le32_to_cpu(path
[i
].p_idx
->ei_block
),
942 idx_pblock(path
[i
].p_idx
),
944 /*memmove(++fidx, path[i].p_idx++,
945 sizeof(struct ext4_extent_idx));
947 BUG_ON(neh->eh_entries > neh->eh_max);*/
952 memmove(++fidx
, path
[i
].p_idx
- m
,
953 sizeof(struct ext4_extent_idx
) * m
);
954 le16_add_cpu(&neh
->eh_entries
, m
);
956 set_buffer_uptodate(bh
);
959 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
965 /* correct old index */
967 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
970 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
971 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
979 /* insert new index */
980 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
981 le32_to_cpu(border
), newblock
);
985 if (buffer_locked(bh
))
991 /* free all allocated blocks in error case */
992 for (i
= 0; i
< depth
; i
++) {
995 ext4_free_blocks(handle
, inode
, ablocks
[i
], 1, 1);
1004 * ext4_ext_grow_indepth:
1005 * implements tree growing procedure:
1006 * - allocates new block
1007 * - moves top-level data (index block or leaf) into the new block
1008 * - initializes new top-level, creating index that points to the
1009 * just created block
1011 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1012 struct ext4_ext_path
*path
,
1013 struct ext4_extent
*newext
)
1015 struct ext4_ext_path
*curp
= path
;
1016 struct ext4_extent_header
*neh
;
1017 struct ext4_extent_idx
*fidx
;
1018 struct buffer_head
*bh
;
1019 ext4_fsblk_t newblock
;
1022 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
, newext
, &err
);
1026 bh
= sb_getblk(inode
->i_sb
, newblock
);
1029 ext4_std_error(inode
->i_sb
, err
);
1034 err
= ext4_journal_get_create_access(handle
, bh
);
1040 /* move top-level index/leaf into new block */
1041 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1043 /* set size of new block */
1044 neh
= ext_block_hdr(bh
);
1045 /* old root could have indexes or leaves
1046 * so calculate e_max right way */
1047 if (ext_depth(inode
))
1048 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
1050 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
1051 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1052 set_buffer_uptodate(bh
);
1055 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1059 /* create index in new top-level index: num,max,pointer */
1060 err
= ext4_ext_get_access(handle
, inode
, curp
);
1064 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1065 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
));
1066 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1067 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1069 if (path
[0].p_hdr
->eh_depth
)
1070 curp
->p_idx
->ei_block
=
1071 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1073 curp
->p_idx
->ei_block
=
1074 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1075 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1077 neh
= ext_inode_hdr(inode
);
1078 fidx
= EXT_FIRST_INDEX(neh
);
1079 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1080 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1081 le32_to_cpu(fidx
->ei_block
), idx_pblock(fidx
));
1083 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1084 err
= ext4_ext_dirty(handle
, inode
, curp
);
1092 * ext4_ext_create_new_leaf:
1093 * finds empty index and adds new leaf.
1094 * if no free index is found, then it requests in-depth growing.
1096 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1097 struct ext4_ext_path
*path
,
1098 struct ext4_extent
*newext
)
1100 struct ext4_ext_path
*curp
;
1101 int depth
, i
, err
= 0;
1104 i
= depth
= ext_depth(inode
);
1106 /* walk up to the tree and look for free index entry */
1107 curp
= path
+ depth
;
1108 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1113 /* we use already allocated block for index block,
1114 * so subsequent data blocks should be contiguous */
1115 if (EXT_HAS_FREE_INDEX(curp
)) {
1116 /* if we found index with free entry, then use that
1117 * entry: create all needed subtree and add new leaf */
1118 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
1123 ext4_ext_drop_refs(path
);
1124 path
= ext4_ext_find_extent(inode
,
1125 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1128 err
= PTR_ERR(path
);
1130 /* tree is full, time to grow in depth */
1131 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1136 ext4_ext_drop_refs(path
);
1137 path
= ext4_ext_find_extent(inode
,
1138 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1141 err
= PTR_ERR(path
);
1146 * only first (depth 0 -> 1) produces free space;
1147 * in all other cases we have to split the grown tree
1149 depth
= ext_depth(inode
);
1150 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1151 /* now we need to split */
1161 * search the closest allocated block to the left for *logical
1162 * and returns it at @logical + it's physical address at @phys
1163 * if *logical is the smallest allocated block, the function
1164 * returns 0 at @phys
1165 * return value contains 0 (success) or error code
1168 ext4_ext_search_left(struct inode
*inode
, struct ext4_ext_path
*path
,
1169 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1171 struct ext4_extent_idx
*ix
;
1172 struct ext4_extent
*ex
;
1175 BUG_ON(path
== NULL
);
1176 depth
= path
->p_depth
;
1179 if (depth
== 0 && path
->p_ext
== NULL
)
1182 /* usually extent in the path covers blocks smaller
1183 * then *logical, but it can be that extent is the
1184 * first one in the file */
1186 ex
= path
[depth
].p_ext
;
1187 ee_len
= ext4_ext_get_actual_len(ex
);
1188 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1189 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1190 while (--depth
>= 0) {
1191 ix
= path
[depth
].p_idx
;
1192 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1197 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1199 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1200 *phys
= ext_pblock(ex
) + ee_len
- 1;
1205 * search the closest allocated block to the right for *logical
1206 * and returns it at @logical + it's physical address at @phys
1207 * if *logical is the smallest allocated block, the function
1208 * returns 0 at @phys
1209 * return value contains 0 (success) or error code
1212 ext4_ext_search_right(struct inode
*inode
, struct ext4_ext_path
*path
,
1213 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1215 struct buffer_head
*bh
= NULL
;
1216 struct ext4_extent_header
*eh
;
1217 struct ext4_extent_idx
*ix
;
1218 struct ext4_extent
*ex
;
1220 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1223 BUG_ON(path
== NULL
);
1224 depth
= path
->p_depth
;
1227 if (depth
== 0 && path
->p_ext
== NULL
)
1230 /* usually extent in the path covers blocks smaller
1231 * then *logical, but it can be that extent is the
1232 * first one in the file */
1234 ex
= path
[depth
].p_ext
;
1235 ee_len
= ext4_ext_get_actual_len(ex
);
1236 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1237 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1238 while (--depth
>= 0) {
1239 ix
= path
[depth
].p_idx
;
1240 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1242 *logical
= le32_to_cpu(ex
->ee_block
);
1243 *phys
= ext_pblock(ex
);
1247 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1249 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1250 /* next allocated block in this leaf */
1252 *logical
= le32_to_cpu(ex
->ee_block
);
1253 *phys
= ext_pblock(ex
);
1257 /* go up and search for index to the right */
1258 while (--depth
>= 0) {
1259 ix
= path
[depth
].p_idx
;
1260 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1264 /* we've gone up to the root and found no index to the right */
1268 /* we've found index to the right, let's
1269 * follow it and find the closest allocated
1270 * block to the right */
1272 block
= idx_pblock(ix
);
1273 while (++depth
< path
->p_depth
) {
1274 bh
= sb_bread(inode
->i_sb
, block
);
1277 eh
= ext_block_hdr(bh
);
1278 /* subtract from p_depth to get proper eh_depth */
1279 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1283 ix
= EXT_FIRST_INDEX(eh
);
1284 block
= idx_pblock(ix
);
1288 bh
= sb_bread(inode
->i_sb
, block
);
1291 eh
= ext_block_hdr(bh
);
1292 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1296 ex
= EXT_FIRST_EXTENT(eh
);
1297 *logical
= le32_to_cpu(ex
->ee_block
);
1298 *phys
= ext_pblock(ex
);
1304 * ext4_ext_next_allocated_block:
1305 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1306 * NOTE: it considers block number from index entry as
1307 * allocated block. Thus, index entries have to be consistent
1311 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1315 BUG_ON(path
== NULL
);
1316 depth
= path
->p_depth
;
1318 if (depth
== 0 && path
->p_ext
== NULL
)
1319 return EXT_MAX_BLOCK
;
1321 while (depth
>= 0) {
1322 if (depth
== path
->p_depth
) {
1324 if (path
[depth
].p_ext
!=
1325 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1326 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1329 if (path
[depth
].p_idx
!=
1330 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1331 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1336 return EXT_MAX_BLOCK
;
1340 * ext4_ext_next_leaf_block:
1341 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1343 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1344 struct ext4_ext_path
*path
)
1348 BUG_ON(path
== NULL
);
1349 depth
= path
->p_depth
;
1351 /* zero-tree has no leaf blocks at all */
1353 return EXT_MAX_BLOCK
;
1355 /* go to index block */
1358 while (depth
>= 0) {
1359 if (path
[depth
].p_idx
!=
1360 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1361 return (ext4_lblk_t
)
1362 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1366 return EXT_MAX_BLOCK
;
1370 * ext4_ext_correct_indexes:
1371 * if leaf gets modified and modified extent is first in the leaf,
1372 * then we have to correct all indexes above.
1373 * TODO: do we need to correct tree in all cases?
1375 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1376 struct ext4_ext_path
*path
)
1378 struct ext4_extent_header
*eh
;
1379 int depth
= ext_depth(inode
);
1380 struct ext4_extent
*ex
;
1384 eh
= path
[depth
].p_hdr
;
1385 ex
= path
[depth
].p_ext
;
1390 /* there is no tree at all */
1394 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1395 /* we correct tree if first leaf got modified only */
1400 * TODO: we need correction if border is smaller than current one
1403 border
= path
[depth
].p_ext
->ee_block
;
1404 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1407 path
[k
].p_idx
->ei_block
= border
;
1408 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1413 /* change all left-side indexes */
1414 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1416 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1419 path
[k
].p_idx
->ei_block
= border
;
1420 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1429 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1430 struct ext4_extent
*ex2
)
1432 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1435 * Make sure that either both extents are uninitialized, or
1438 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1441 if (ext4_ext_is_uninitialized(ex1
))
1442 max_len
= EXT_UNINIT_MAX_LEN
;
1444 max_len
= EXT_INIT_MAX_LEN
;
1446 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1447 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1449 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1450 le32_to_cpu(ex2
->ee_block
))
1454 * To allow future support for preallocated extents to be added
1455 * as an RO_COMPAT feature, refuse to merge to extents if
1456 * this can result in the top bit of ee_len being set.
1458 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1460 #ifdef AGGRESSIVE_TEST
1461 if (ext1_ee_len
>= 4)
1465 if (ext_pblock(ex1
) + ext1_ee_len
== ext_pblock(ex2
))
1471 * This function tries to merge the "ex" extent to the next extent in the tree.
1472 * It always tries to merge towards right. If you want to merge towards
1473 * left, pass "ex - 1" as argument instead of "ex".
1474 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1475 * 1 if they got merged.
1477 int ext4_ext_try_to_merge(struct inode
*inode
,
1478 struct ext4_ext_path
*path
,
1479 struct ext4_extent
*ex
)
1481 struct ext4_extent_header
*eh
;
1482 unsigned int depth
, len
;
1484 int uninitialized
= 0;
1486 depth
= ext_depth(inode
);
1487 BUG_ON(path
[depth
].p_hdr
== NULL
);
1488 eh
= path
[depth
].p_hdr
;
1490 while (ex
< EXT_LAST_EXTENT(eh
)) {
1491 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1493 /* merge with next extent! */
1494 if (ext4_ext_is_uninitialized(ex
))
1496 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1497 + ext4_ext_get_actual_len(ex
+ 1));
1499 ext4_ext_mark_uninitialized(ex
);
1501 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1502 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1503 * sizeof(struct ext4_extent
);
1504 memmove(ex
+ 1, ex
+ 2, len
);
1506 le16_add_cpu(&eh
->eh_entries
, -1);
1508 WARN_ON(eh
->eh_entries
== 0);
1509 if (!eh
->eh_entries
)
1510 ext4_error(inode
->i_sb
, "ext4_ext_try_to_merge",
1511 "inode#%lu, eh->eh_entries = 0!", inode
->i_ino
);
1518 * check if a portion of the "newext" extent overlaps with an
1521 * If there is an overlap discovered, it updates the length of the newext
1522 * such that there will be no overlap, and then returns 1.
1523 * If there is no overlap found, it returns 0.
1525 unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1526 struct ext4_extent
*newext
,
1527 struct ext4_ext_path
*path
)
1530 unsigned int depth
, len1
;
1531 unsigned int ret
= 0;
1533 b1
= le32_to_cpu(newext
->ee_block
);
1534 len1
= ext4_ext_get_actual_len(newext
);
1535 depth
= ext_depth(inode
);
1536 if (!path
[depth
].p_ext
)
1538 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1541 * get the next allocated block if the extent in the path
1542 * is before the requested block(s)
1545 b2
= ext4_ext_next_allocated_block(path
);
1546 if (b2
== EXT_MAX_BLOCK
)
1550 /* check for wrap through zero on extent logical start block*/
1551 if (b1
+ len1
< b1
) {
1552 len1
= EXT_MAX_BLOCK
- b1
;
1553 newext
->ee_len
= cpu_to_le16(len1
);
1557 /* check for overlap */
1558 if (b1
+ len1
> b2
) {
1559 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1567 * ext4_ext_insert_extent:
1568 * tries to merge requsted extent into the existing extent or
1569 * inserts requested extent as new one into the tree,
1570 * creating new leaf in the no-space case.
1572 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1573 struct ext4_ext_path
*path
,
1574 struct ext4_extent
*newext
)
1576 struct ext4_extent_header
*eh
;
1577 struct ext4_extent
*ex
, *fex
;
1578 struct ext4_extent
*nearex
; /* nearest extent */
1579 struct ext4_ext_path
*npath
= NULL
;
1580 int depth
, len
, err
;
1582 unsigned uninitialized
= 0;
1584 BUG_ON(ext4_ext_get_actual_len(newext
) == 0);
1585 depth
= ext_depth(inode
);
1586 ex
= path
[depth
].p_ext
;
1587 BUG_ON(path
[depth
].p_hdr
== NULL
);
1589 /* try to insert block into found extent and return */
1590 if (ex
&& ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1591 ext_debug("append %d block to %d:%d (from %llu)\n",
1592 ext4_ext_get_actual_len(newext
),
1593 le32_to_cpu(ex
->ee_block
),
1594 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
1595 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1600 * ext4_can_extents_be_merged should have checked that either
1601 * both extents are uninitialized, or both aren't. Thus we
1602 * need to check only one of them here.
1604 if (ext4_ext_is_uninitialized(ex
))
1606 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1607 + ext4_ext_get_actual_len(newext
));
1609 ext4_ext_mark_uninitialized(ex
);
1610 eh
= path
[depth
].p_hdr
;
1616 depth
= ext_depth(inode
);
1617 eh
= path
[depth
].p_hdr
;
1618 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1621 /* probably next leaf has space for us? */
1622 fex
= EXT_LAST_EXTENT(eh
);
1623 next
= ext4_ext_next_leaf_block(inode
, path
);
1624 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1625 && next
!= EXT_MAX_BLOCK
) {
1626 ext_debug("next leaf block - %d\n", next
);
1627 BUG_ON(npath
!= NULL
);
1628 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1630 return PTR_ERR(npath
);
1631 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1632 eh
= npath
[depth
].p_hdr
;
1633 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1634 ext_debug("next leaf isnt full(%d)\n",
1635 le16_to_cpu(eh
->eh_entries
));
1639 ext_debug("next leaf has no free space(%d,%d)\n",
1640 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1644 * There is no free space in the found leaf.
1645 * We're gonna add a new leaf in the tree.
1647 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1650 depth
= ext_depth(inode
);
1651 eh
= path
[depth
].p_hdr
;
1654 nearex
= path
[depth
].p_ext
;
1656 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1661 /* there is no extent in this leaf, create first one */
1662 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1663 le32_to_cpu(newext
->ee_block
),
1665 ext4_ext_get_actual_len(newext
));
1666 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1667 } else if (le32_to_cpu(newext
->ee_block
)
1668 > le32_to_cpu(nearex
->ee_block
)) {
1669 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1670 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1671 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1672 len
= (len
- 1) * sizeof(struct ext4_extent
);
1673 len
= len
< 0 ? 0 : len
;
1674 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1675 "move %d from 0x%p to 0x%p\n",
1676 le32_to_cpu(newext
->ee_block
),
1678 ext4_ext_get_actual_len(newext
),
1679 nearex
, len
, nearex
+ 1, nearex
+ 2);
1680 memmove(nearex
+ 2, nearex
+ 1, len
);
1682 path
[depth
].p_ext
= nearex
+ 1;
1684 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1685 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1686 len
= len
< 0 ? 0 : len
;
1687 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1688 "move %d from 0x%p to 0x%p\n",
1689 le32_to_cpu(newext
->ee_block
),
1691 ext4_ext_get_actual_len(newext
),
1692 nearex
, len
, nearex
+ 1, nearex
+ 2);
1693 memmove(nearex
+ 1, nearex
, len
);
1694 path
[depth
].p_ext
= nearex
;
1697 le16_add_cpu(&eh
->eh_entries
, 1);
1698 nearex
= path
[depth
].p_ext
;
1699 nearex
->ee_block
= newext
->ee_block
;
1700 ext4_ext_store_pblock(nearex
, ext_pblock(newext
));
1701 nearex
->ee_len
= newext
->ee_len
;
1704 /* try to merge extents to the right */
1705 ext4_ext_try_to_merge(inode
, path
, nearex
);
1707 /* try to merge extents to the left */
1709 /* time to correct all indexes above */
1710 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1714 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1718 ext4_ext_drop_refs(npath
);
1721 ext4_ext_invalidate_cache(inode
);
1725 int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1726 ext4_lblk_t num
, ext_prepare_callback func
,
1729 struct ext4_ext_path
*path
= NULL
;
1730 struct ext4_ext_cache cbex
;
1731 struct ext4_extent
*ex
;
1732 ext4_lblk_t next
, start
= 0, end
= 0;
1733 ext4_lblk_t last
= block
+ num
;
1734 int depth
, exists
, err
= 0;
1736 BUG_ON(func
== NULL
);
1737 BUG_ON(inode
== NULL
);
1739 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1741 /* find extent for this block */
1742 path
= ext4_ext_find_extent(inode
, block
, path
);
1744 err
= PTR_ERR(path
);
1749 depth
= ext_depth(inode
);
1750 BUG_ON(path
[depth
].p_hdr
== NULL
);
1751 ex
= path
[depth
].p_ext
;
1752 next
= ext4_ext_next_allocated_block(path
);
1756 /* there is no extent yet, so try to allocate
1757 * all requested space */
1760 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1761 /* need to allocate space before found extent */
1763 end
= le32_to_cpu(ex
->ee_block
);
1764 if (block
+ num
< end
)
1766 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1767 + ext4_ext_get_actual_len(ex
)) {
1768 /* need to allocate space after found extent */
1773 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1775 * some part of requested space is covered
1779 end
= le32_to_cpu(ex
->ee_block
)
1780 + ext4_ext_get_actual_len(ex
);
1781 if (block
+ num
< end
)
1787 BUG_ON(end
<= start
);
1790 cbex
.ec_block
= start
;
1791 cbex
.ec_len
= end
- start
;
1793 cbex
.ec_type
= EXT4_EXT_CACHE_GAP
;
1795 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1796 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1797 cbex
.ec_start
= ext_pblock(ex
);
1798 cbex
.ec_type
= EXT4_EXT_CACHE_EXTENT
;
1801 BUG_ON(cbex
.ec_len
== 0);
1802 err
= func(inode
, path
, &cbex
, ex
, cbdata
);
1803 ext4_ext_drop_refs(path
);
1808 if (err
== EXT_REPEAT
)
1810 else if (err
== EXT_BREAK
) {
1815 if (ext_depth(inode
) != depth
) {
1816 /* depth was changed. we have to realloc path */
1821 block
= cbex
.ec_block
+ cbex
.ec_len
;
1825 ext4_ext_drop_refs(path
);
1833 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1834 __u32 len
, ext4_fsblk_t start
, int type
)
1836 struct ext4_ext_cache
*cex
;
1838 cex
= &EXT4_I(inode
)->i_cached_extent
;
1839 cex
->ec_type
= type
;
1840 cex
->ec_block
= block
;
1842 cex
->ec_start
= start
;
1846 * ext4_ext_put_gap_in_cache:
1847 * calculate boundaries of the gap that the requested block fits into
1848 * and cache this gap
1851 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1854 int depth
= ext_depth(inode
);
1857 struct ext4_extent
*ex
;
1859 ex
= path
[depth
].p_ext
;
1861 /* there is no extent yet, so gap is [0;-] */
1863 len
= EXT_MAX_BLOCK
;
1864 ext_debug("cache gap(whole file):");
1865 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1867 len
= le32_to_cpu(ex
->ee_block
) - block
;
1868 ext_debug("cache gap(before): %u [%u:%u]",
1870 le32_to_cpu(ex
->ee_block
),
1871 ext4_ext_get_actual_len(ex
));
1872 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1873 + ext4_ext_get_actual_len(ex
)) {
1875 lblock
= le32_to_cpu(ex
->ee_block
)
1876 + ext4_ext_get_actual_len(ex
);
1878 next
= ext4_ext_next_allocated_block(path
);
1879 ext_debug("cache gap(after): [%u:%u] %u",
1880 le32_to_cpu(ex
->ee_block
),
1881 ext4_ext_get_actual_len(ex
),
1883 BUG_ON(next
== lblock
);
1884 len
= next
- lblock
;
1890 ext_debug(" -> %u:%lu\n", lblock
, len
);
1891 ext4_ext_put_in_cache(inode
, lblock
, len
, 0, EXT4_EXT_CACHE_GAP
);
1895 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1896 struct ext4_extent
*ex
)
1898 struct ext4_ext_cache
*cex
;
1900 cex
= &EXT4_I(inode
)->i_cached_extent
;
1902 /* has cache valid data? */
1903 if (cex
->ec_type
== EXT4_EXT_CACHE_NO
)
1904 return EXT4_EXT_CACHE_NO
;
1906 BUG_ON(cex
->ec_type
!= EXT4_EXT_CACHE_GAP
&&
1907 cex
->ec_type
!= EXT4_EXT_CACHE_EXTENT
);
1908 if (block
>= cex
->ec_block
&& block
< cex
->ec_block
+ cex
->ec_len
) {
1909 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
1910 ext4_ext_store_pblock(ex
, cex
->ec_start
);
1911 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
1912 ext_debug("%u cached by %u:%u:%llu\n",
1914 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
1915 return cex
->ec_type
;
1919 return EXT4_EXT_CACHE_NO
;
1924 * removes index from the index block.
1925 * It's used in truncate case only, thus all requests are for
1926 * last index in the block only.
1928 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
1929 struct ext4_ext_path
*path
)
1931 struct buffer_head
*bh
;
1935 /* free index block */
1937 leaf
= idx_pblock(path
->p_idx
);
1938 BUG_ON(path
->p_hdr
->eh_entries
== 0);
1939 err
= ext4_ext_get_access(handle
, inode
, path
);
1942 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
1943 err
= ext4_ext_dirty(handle
, inode
, path
);
1946 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
1947 bh
= sb_find_get_block(inode
->i_sb
, leaf
);
1948 ext4_forget(handle
, 1, inode
, bh
, leaf
);
1949 ext4_free_blocks(handle
, inode
, leaf
, 1, 1);
1954 * ext4_ext_calc_credits_for_single_extent:
1955 * This routine returns max. credits that needed to insert an extent
1956 * to the extent tree.
1957 * When pass the actual path, the caller should calculate credits
1960 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
1961 struct ext4_ext_path
*path
)
1964 int depth
= ext_depth(inode
);
1967 /* probably there is space in leaf? */
1968 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
1969 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
1972 * There are some space in the leaf tree, no
1973 * need to account for leaf block credit
1975 * bitmaps and block group descriptor blocks
1976 * and other metadat blocks still need to be
1979 /* 1 bitmap, 1 block group descriptor */
1980 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
1984 return ext4_chunk_trans_blocks(inode
, nrblocks
);
1988 * How many index/leaf blocks need to change/allocate to modify nrblocks?
1990 * if nrblocks are fit in a single extent (chunk flag is 1), then
1991 * in the worse case, each tree level index/leaf need to be changed
1992 * if the tree split due to insert a new extent, then the old tree
1993 * index/leaf need to be updated too
1995 * If the nrblocks are discontiguous, they could cause
1996 * the whole tree split more than once, but this is really rare.
1998 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2001 int depth
= ext_depth(inode
);
2011 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2012 struct ext4_extent
*ex
,
2013 ext4_lblk_t from
, ext4_lblk_t to
)
2015 struct buffer_head
*bh
;
2016 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2017 int i
, metadata
= 0;
2019 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2021 #ifdef EXTENTS_STATS
2023 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2024 spin_lock(&sbi
->s_ext_stats_lock
);
2025 sbi
->s_ext_blocks
+= ee_len
;
2026 sbi
->s_ext_extents
++;
2027 if (ee_len
< sbi
->s_ext_min
)
2028 sbi
->s_ext_min
= ee_len
;
2029 if (ee_len
> sbi
->s_ext_max
)
2030 sbi
->s_ext_max
= ee_len
;
2031 if (ext_depth(inode
) > sbi
->s_depth_max
)
2032 sbi
->s_depth_max
= ext_depth(inode
);
2033 spin_unlock(&sbi
->s_ext_stats_lock
);
2036 if (from
>= le32_to_cpu(ex
->ee_block
)
2037 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2042 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2043 start
= ext_pblock(ex
) + ee_len
- num
;
2044 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2045 for (i
= 0; i
< num
; i
++) {
2046 bh
= sb_find_get_block(inode
->i_sb
, start
+ i
);
2047 ext4_forget(handle
, 0, inode
, bh
, start
+ i
);
2049 ext4_free_blocks(handle
, inode
, start
, num
, metadata
);
2050 } else if (from
== le32_to_cpu(ex
->ee_block
)
2051 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2052 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
2053 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2055 printk(KERN_INFO
"strange request: removal(2) "
2056 "%u-%u from %u:%u\n",
2057 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2063 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2064 struct ext4_ext_path
*path
, ext4_lblk_t start
)
2066 int err
= 0, correct_index
= 0;
2067 int depth
= ext_depth(inode
), credits
;
2068 struct ext4_extent_header
*eh
;
2069 ext4_lblk_t a
, b
, block
;
2071 ext4_lblk_t ex_ee_block
;
2072 unsigned short ex_ee_len
;
2073 unsigned uninitialized
= 0;
2074 struct ext4_extent
*ex
;
2076 /* the header must be checked already in ext4_ext_remove_space() */
2077 ext_debug("truncate since %u in leaf\n", start
);
2078 if (!path
[depth
].p_hdr
)
2079 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2080 eh
= path
[depth
].p_hdr
;
2083 /* find where to start removing */
2084 ex
= EXT_LAST_EXTENT(eh
);
2086 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2087 if (ext4_ext_is_uninitialized(ex
))
2089 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2091 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2092 ex_ee_block
+ ex_ee_len
> start
) {
2093 ext_debug("remove ext %lu:%u\n", ex_ee_block
, ex_ee_len
);
2094 path
[depth
].p_ext
= ex
;
2096 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2097 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
2098 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
2100 ext_debug(" border %u:%u\n", a
, b
);
2102 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
2106 } else if (a
!= ex_ee_block
) {
2107 /* remove tail of the extent */
2108 block
= ex_ee_block
;
2110 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2111 /* remove head of the extent */
2114 /* there is no "make a hole" API yet */
2117 /* remove whole extent: excellent! */
2118 block
= ex_ee_block
;
2120 BUG_ON(a
!= ex_ee_block
);
2121 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
2125 * 3 for leaf, sb, and inode plus 2 (bmap and group
2126 * descriptor) for each block group; assume two block
2127 * groups plus ex_ee_len/blocks_per_block_group for
2130 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2131 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2133 credits
+= (ext_depth(inode
)) + 1;
2135 credits
+= 2 * EXT4_QUOTA_TRANS_BLOCKS(inode
->i_sb
);
2137 err
= ext4_ext_journal_restart(handle
, credits
);
2141 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2145 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2150 /* this extent is removed; mark slot entirely unused */
2151 ext4_ext_store_pblock(ex
, 0);
2152 le16_add_cpu(&eh
->eh_entries
, -1);
2155 ex
->ee_block
= cpu_to_le32(block
);
2156 ex
->ee_len
= cpu_to_le16(num
);
2158 * Do not mark uninitialized if all the blocks in the
2159 * extent have been removed.
2161 if (uninitialized
&& num
)
2162 ext4_ext_mark_uninitialized(ex
);
2164 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2168 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2171 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2172 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2175 if (correct_index
&& eh
->eh_entries
)
2176 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2178 /* if this leaf is free, then we should
2179 * remove it from index block above */
2180 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2181 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2188 * ext4_ext_more_to_rm:
2189 * returns 1 if current index has to be freed (even partial)
2192 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2194 BUG_ON(path
->p_idx
== NULL
);
2196 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2200 * if truncate on deeper level happened, it wasn't partial,
2201 * so we have to consider current index for truncation
2203 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2208 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2210 struct super_block
*sb
= inode
->i_sb
;
2211 int depth
= ext_depth(inode
);
2212 struct ext4_ext_path
*path
;
2216 ext_debug("truncate since %u\n", start
);
2218 /* probably first extent we're gonna free will be last in block */
2219 handle
= ext4_journal_start(inode
, depth
+ 1);
2221 return PTR_ERR(handle
);
2223 ext4_ext_invalidate_cache(inode
);
2226 * We start scanning from right side, freeing all the blocks
2227 * after i_size and walking into the tree depth-wise.
2229 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2231 ext4_journal_stop(handle
);
2234 path
[0].p_hdr
= ext_inode_hdr(inode
);
2235 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2239 path
[0].p_depth
= depth
;
2241 while (i
>= 0 && err
== 0) {
2243 /* this is leaf block */
2244 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2245 /* root level has p_bh == NULL, brelse() eats this */
2246 brelse(path
[i
].p_bh
);
2247 path
[i
].p_bh
= NULL
;
2252 /* this is index block */
2253 if (!path
[i
].p_hdr
) {
2254 ext_debug("initialize header\n");
2255 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2258 if (!path
[i
].p_idx
) {
2259 /* this level hasn't been touched yet */
2260 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2261 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2262 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2264 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2266 /* we were already here, see at next index */
2270 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2271 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2273 if (ext4_ext_more_to_rm(path
+ i
)) {
2274 struct buffer_head
*bh
;
2275 /* go to the next level */
2276 ext_debug("move to level %d (block %llu)\n",
2277 i
+ 1, idx_pblock(path
[i
].p_idx
));
2278 memset(path
+ i
+ 1, 0, sizeof(*path
));
2279 bh
= sb_bread(sb
, idx_pblock(path
[i
].p_idx
));
2281 /* should we reset i_size? */
2285 if (WARN_ON(i
+ 1 > depth
)) {
2289 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2294 path
[i
+ 1].p_bh
= bh
;
2296 /* save actual number of indexes since this
2297 * number is changed at the next iteration */
2298 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2301 /* we finished processing this index, go up */
2302 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2303 /* index is empty, remove it;
2304 * handle must be already prepared by the
2305 * truncatei_leaf() */
2306 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2308 /* root level has p_bh == NULL, brelse() eats this */
2309 brelse(path
[i
].p_bh
);
2310 path
[i
].p_bh
= NULL
;
2312 ext_debug("return to level %d\n", i
);
2316 /* TODO: flexible tree reduction should be here */
2317 if (path
->p_hdr
->eh_entries
== 0) {
2319 * truncate to zero freed all the tree,
2320 * so we need to correct eh_depth
2322 err
= ext4_ext_get_access(handle
, inode
, path
);
2324 ext_inode_hdr(inode
)->eh_depth
= 0;
2325 ext_inode_hdr(inode
)->eh_max
=
2326 cpu_to_le16(ext4_ext_space_root(inode
));
2327 err
= ext4_ext_dirty(handle
, inode
, path
);
2331 ext4_ext_drop_refs(path
);
2333 ext4_journal_stop(handle
);
2339 * called at mount time
2341 void ext4_ext_init(struct super_block
*sb
)
2344 * possible initialization would be here
2347 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2348 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2349 #ifdef AGGRESSIVE_TEST
2350 printk(", aggressive tests");
2352 #ifdef CHECK_BINSEARCH
2353 printk(", check binsearch");
2355 #ifdef EXTENTS_STATS
2359 #ifdef EXTENTS_STATS
2360 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2361 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2362 EXT4_SB(sb
)->s_ext_max
= 0;
2368 * called at umount time
2370 void ext4_ext_release(struct super_block
*sb
)
2372 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2375 #ifdef EXTENTS_STATS
2376 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2377 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2378 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2379 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2380 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2381 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2382 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2387 static void bi_complete(struct bio
*bio
, int error
)
2389 complete((struct completion
*)bio
->bi_private
);
2392 /* FIXME!! we need to try to merge to left or right after zero-out */
2393 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2397 int blkbits
, blocksize
;
2399 struct completion event
;
2400 unsigned int ee_len
, len
, done
, offset
;
2403 blkbits
= inode
->i_blkbits
;
2404 blocksize
= inode
->i_sb
->s_blocksize
;
2405 ee_len
= ext4_ext_get_actual_len(ex
);
2406 ee_pblock
= ext_pblock(ex
);
2408 /* convert ee_pblock to 512 byte sectors */
2409 ee_pblock
= ee_pblock
<< (blkbits
- 9);
2411 while (ee_len
> 0) {
2413 if (ee_len
> BIO_MAX_PAGES
)
2414 len
= BIO_MAX_PAGES
;
2418 bio
= bio_alloc(GFP_NOIO
, len
);
2419 bio
->bi_sector
= ee_pblock
;
2420 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
2424 while (done
< len
) {
2425 ret
= bio_add_page(bio
, ZERO_PAGE(0),
2427 if (ret
!= blocksize
) {
2429 * We can't add any more pages because of
2430 * hardware limitations. Start a new bio.
2435 offset
+= blocksize
;
2436 if (offset
>= PAGE_CACHE_SIZE
)
2440 init_completion(&event
);
2441 bio
->bi_private
= &event
;
2442 bio
->bi_end_io
= bi_complete
;
2443 submit_bio(WRITE
, bio
);
2444 wait_for_completion(&event
);
2446 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2454 ee_pblock
+= done
<< (blkbits
- 9);
2459 #define EXT4_EXT_ZERO_LEN 7
2462 * This function is called by ext4_ext_get_blocks() if someone tries to write
2463 * to an uninitialized extent. It may result in splitting the uninitialized
2464 * extent into multiple extents (upto three - one initialized and two
2466 * There are three possibilities:
2467 * a> There is no split required: Entire extent should be initialized
2468 * b> Splits in two extents: Write is happening at either end of the extent
2469 * c> Splits in three extents: Somone is writing in middle of the extent
2471 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2472 struct inode
*inode
,
2473 struct ext4_ext_path
*path
,
2475 unsigned int max_blocks
)
2477 struct ext4_extent
*ex
, newex
, orig_ex
;
2478 struct ext4_extent
*ex1
= NULL
;
2479 struct ext4_extent
*ex2
= NULL
;
2480 struct ext4_extent
*ex3
= NULL
;
2481 struct ext4_extent_header
*eh
;
2482 ext4_lblk_t ee_block
;
2483 unsigned int allocated
, ee_len
, depth
;
2484 ext4_fsblk_t newblock
;
2488 depth
= ext_depth(inode
);
2489 eh
= path
[depth
].p_hdr
;
2490 ex
= path
[depth
].p_ext
;
2491 ee_block
= le32_to_cpu(ex
->ee_block
);
2492 ee_len
= ext4_ext_get_actual_len(ex
);
2493 allocated
= ee_len
- (iblock
- ee_block
);
2494 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2496 orig_ex
.ee_block
= ex
->ee_block
;
2497 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2498 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2500 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2503 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2504 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
) {
2505 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2507 goto fix_extent_len
;
2508 /* update the extent length and mark as initialized */
2509 ex
->ee_block
= orig_ex
.ee_block
;
2510 ex
->ee_len
= orig_ex
.ee_len
;
2511 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2512 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2513 /* zeroed the full extent */
2517 /* ex1: ee_block to iblock - 1 : uninitialized */
2518 if (iblock
> ee_block
) {
2520 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2521 ext4_ext_mark_uninitialized(ex1
);
2525 * for sanity, update the length of the ex2 extent before
2526 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2527 * overlap of blocks.
2529 if (!ex1
&& allocated
> max_blocks
)
2530 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2531 /* ex3: to ee_block + ee_len : uninitialised */
2532 if (allocated
> max_blocks
) {
2533 unsigned int newdepth
;
2534 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2535 if (allocated
<= EXT4_EXT_ZERO_LEN
) {
2537 * iblock == ee_block is handled by the zerouout
2539 * Mark first half uninitialized.
2540 * Mark second half initialized and zero out the
2541 * initialized extent
2543 ex
->ee_block
= orig_ex
.ee_block
;
2544 ex
->ee_len
= cpu_to_le16(ee_len
- allocated
);
2545 ext4_ext_mark_uninitialized(ex
);
2546 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2547 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2550 ex3
->ee_block
= cpu_to_le32(iblock
);
2551 ext4_ext_store_pblock(ex3
, newblock
);
2552 ex3
->ee_len
= cpu_to_le16(allocated
);
2553 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
);
2554 if (err
== -ENOSPC
) {
2555 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2557 goto fix_extent_len
;
2558 ex
->ee_block
= orig_ex
.ee_block
;
2559 ex
->ee_len
= orig_ex
.ee_len
;
2560 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2561 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2562 /* blocks available from iblock */
2566 goto fix_extent_len
;
2569 * We need to zero out the second half because
2570 * an fallocate request can update file size and
2571 * converting the second half to initialized extent
2572 * implies that we can leak some junk data to user
2575 err
= ext4_ext_zeroout(inode
, ex3
);
2578 * We should actually mark the
2579 * second half as uninit and return error
2580 * Insert would have changed the extent
2582 depth
= ext_depth(inode
);
2583 ext4_ext_drop_refs(path
);
2584 path
= ext4_ext_find_extent(inode
,
2587 err
= PTR_ERR(path
);
2590 /* get the second half extent details */
2591 ex
= path
[depth
].p_ext
;
2592 err
= ext4_ext_get_access(handle
, inode
,
2596 ext4_ext_mark_uninitialized(ex
);
2597 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2601 /* zeroed the second half */
2605 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2606 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2607 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2608 ext4_ext_mark_uninitialized(ex3
);
2609 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
);
2610 if (err
== -ENOSPC
) {
2611 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2613 goto fix_extent_len
;
2614 /* update the extent length and mark as initialized */
2615 ex
->ee_block
= orig_ex
.ee_block
;
2616 ex
->ee_len
= orig_ex
.ee_len
;
2617 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2618 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2619 /* zeroed the full extent */
2620 /* blocks available from iblock */
2624 goto fix_extent_len
;
2626 * The depth, and hence eh & ex might change
2627 * as part of the insert above.
2629 newdepth
= ext_depth(inode
);
2631 * update the extent length after successful insert of the
2634 orig_ex
.ee_len
= cpu_to_le16(ee_len
-
2635 ext4_ext_get_actual_len(ex3
));
2637 ext4_ext_drop_refs(path
);
2638 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2640 err
= PTR_ERR(path
);
2643 eh
= path
[depth
].p_hdr
;
2644 ex
= path
[depth
].p_ext
;
2648 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2652 allocated
= max_blocks
;
2654 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2655 * to insert a extent in the middle zerout directly
2656 * otherwise give the extent a chance to merge to left
2658 if (le16_to_cpu(orig_ex
.ee_len
) <= EXT4_EXT_ZERO_LEN
&&
2659 iblock
!= ee_block
) {
2660 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2662 goto fix_extent_len
;
2663 /* update the extent length and mark as initialized */
2664 ex
->ee_block
= orig_ex
.ee_block
;
2665 ex
->ee_len
= orig_ex
.ee_len
;
2666 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2667 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2668 /* zero out the first half */
2669 /* blocks available from iblock */
2674 * If there was a change of depth as part of the
2675 * insertion of ex3 above, we need to update the length
2676 * of the ex1 extent again here
2678 if (ex1
&& ex1
!= ex
) {
2680 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2681 ext4_ext_mark_uninitialized(ex1
);
2684 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2685 ex2
->ee_block
= cpu_to_le32(iblock
);
2686 ext4_ext_store_pblock(ex2
, newblock
);
2687 ex2
->ee_len
= cpu_to_le16(allocated
);
2691 * New (initialized) extent starts from the first block
2692 * in the current extent. i.e., ex2 == ex
2693 * We have to see if it can be merged with the extent
2696 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2698 * To merge left, pass "ex2 - 1" to try_to_merge(),
2699 * since it merges towards right _only_.
2701 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2703 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2706 depth
= ext_depth(inode
);
2711 * Try to Merge towards right. This might be required
2712 * only when the whole extent is being written to.
2713 * i.e. ex2 == ex and ex3 == NULL.
2716 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2718 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2723 /* Mark modified extent as dirty */
2724 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2727 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2728 if (err
== -ENOSPC
) {
2729 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2731 goto fix_extent_len
;
2732 /* update the extent length and mark as initialized */
2733 ex
->ee_block
= orig_ex
.ee_block
;
2734 ex
->ee_len
= orig_ex
.ee_len
;
2735 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2736 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2737 /* zero out the first half */
2740 goto fix_extent_len
;
2742 return err
? err
: allocated
;
2745 ex
->ee_block
= orig_ex
.ee_block
;
2746 ex
->ee_len
= orig_ex
.ee_len
;
2747 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2748 ext4_ext_mark_uninitialized(ex
);
2749 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2754 * Block allocation/map/preallocation routine for extents based files
2757 * Need to be called with
2758 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2759 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2761 * return > 0, number of of blocks already mapped/allocated
2762 * if create == 0 and these are pre-allocated blocks
2763 * buffer head is unmapped
2764 * otherwise blocks are mapped
2766 * return = 0, if plain look up failed (blocks have not been allocated)
2767 * buffer head is unmapped
2769 * return < 0, error case.
2771 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
2773 unsigned int max_blocks
, struct buffer_head
*bh_result
,
2774 int create
, int extend_disksize
)
2776 struct ext4_ext_path
*path
= NULL
;
2777 struct ext4_extent_header
*eh
;
2778 struct ext4_extent newex
, *ex
;
2779 ext4_fsblk_t newblock
;
2780 int err
= 0, depth
, ret
, cache_type
;
2781 unsigned int allocated
= 0;
2782 struct ext4_allocation_request ar
;
2785 __clear_bit(BH_New
, &bh_result
->b_state
);
2786 ext_debug("blocks %u/%u requested for inode %u\n",
2787 iblock
, max_blocks
, inode
->i_ino
);
2789 /* check in cache */
2790 cache_type
= ext4_ext_in_cache(inode
, iblock
, &newex
);
2792 if (cache_type
== EXT4_EXT_CACHE_GAP
) {
2795 * block isn't allocated yet and
2796 * user doesn't want to allocate it
2800 /* we should allocate requested block */
2801 } else if (cache_type
== EXT4_EXT_CACHE_EXTENT
) {
2802 /* block is already allocated */
2804 - le32_to_cpu(newex
.ee_block
)
2805 + ext_pblock(&newex
);
2806 /* number of remaining blocks in the extent */
2807 allocated
= ext4_ext_get_actual_len(&newex
) -
2808 (iblock
- le32_to_cpu(newex
.ee_block
));
2815 /* find extent for this block */
2816 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
2818 err
= PTR_ERR(path
);
2823 depth
= ext_depth(inode
);
2826 * consistent leaf must not be empty;
2827 * this situation is possible, though, _during_ tree modification;
2828 * this is why assert can't be put in ext4_ext_find_extent()
2830 BUG_ON(path
[depth
].p_ext
== NULL
&& depth
!= 0);
2831 eh
= path
[depth
].p_hdr
;
2833 ex
= path
[depth
].p_ext
;
2835 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
2836 ext4_fsblk_t ee_start
= ext_pblock(ex
);
2837 unsigned short ee_len
;
2840 * Uninitialized extents are treated as holes, except that
2841 * we split out initialized portions during a write.
2843 ee_len
= ext4_ext_get_actual_len(ex
);
2844 /* if found extent covers block, simply return it */
2845 if (iblock
>= ee_block
&& iblock
< ee_block
+ ee_len
) {
2846 newblock
= iblock
- ee_block
+ ee_start
;
2847 /* number of remaining blocks in the extent */
2848 allocated
= ee_len
- (iblock
- ee_block
);
2849 ext_debug("%u fit into %lu:%d -> %llu\n", iblock
,
2850 ee_block
, ee_len
, newblock
);
2852 /* Do not put uninitialized extent in the cache */
2853 if (!ext4_ext_is_uninitialized(ex
)) {
2854 ext4_ext_put_in_cache(inode
, ee_block
,
2856 EXT4_EXT_CACHE_EXTENT
);
2859 if (create
== EXT4_CREATE_UNINITIALIZED_EXT
)
2863 * We have blocks reserved already. We
2864 * return allocated blocks so that delalloc
2865 * won't do block reservation for us. But
2866 * the buffer head will be unmapped so that
2867 * a read from the block returns 0s.
2869 if (allocated
> max_blocks
)
2870 allocated
= max_blocks
;
2871 set_buffer_unwritten(bh_result
);
2875 ret
= ext4_ext_convert_to_initialized(handle
, inode
,
2888 * requested block isn't allocated yet;
2889 * we couldn't try to create block if create flag is zero
2893 * put just found gap into cache to speed up
2894 * subsequent requests
2896 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
2900 * Okay, we need to do block allocation.
2903 /* find neighbour allocated blocks */
2905 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
2909 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
2914 * See if request is beyond maximum number of blocks we can have in
2915 * a single extent. For an initialized extent this limit is
2916 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2917 * EXT_UNINIT_MAX_LEN.
2919 if (max_blocks
> EXT_INIT_MAX_LEN
&&
2920 create
!= EXT4_CREATE_UNINITIALIZED_EXT
)
2921 max_blocks
= EXT_INIT_MAX_LEN
;
2922 else if (max_blocks
> EXT_UNINIT_MAX_LEN
&&
2923 create
== EXT4_CREATE_UNINITIALIZED_EXT
)
2924 max_blocks
= EXT_UNINIT_MAX_LEN
;
2926 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2927 newex
.ee_block
= cpu_to_le32(iblock
);
2928 newex
.ee_len
= cpu_to_le16(max_blocks
);
2929 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
2931 allocated
= ext4_ext_get_actual_len(&newex
);
2933 allocated
= max_blocks
;
2935 /* allocate new block */
2937 ar
.goal
= ext4_ext_find_goal(inode
, path
, iblock
);
2938 ar
.logical
= iblock
;
2940 if (S_ISREG(inode
->i_mode
))
2941 ar
.flags
= EXT4_MB_HINT_DATA
;
2943 /* disable in-core preallocation for non-regular files */
2945 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
2948 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2949 ar
.goal
, newblock
, allocated
);
2951 /* try to insert new extent into found leaf and return */
2952 ext4_ext_store_pblock(&newex
, newblock
);
2953 newex
.ee_len
= cpu_to_le16(ar
.len
);
2954 if (create
== EXT4_CREATE_UNINITIALIZED_EXT
) /* Mark uninitialized */
2955 ext4_ext_mark_uninitialized(&newex
);
2956 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2958 /* free data blocks we just allocated */
2959 /* not a good idea to call discard here directly,
2960 * but otherwise we'd need to call it every free() */
2961 ext4_discard_preallocations(inode
);
2962 ext4_free_blocks(handle
, inode
, ext_pblock(&newex
),
2963 ext4_ext_get_actual_len(&newex
), 0);
2967 /* previous routine could use block we allocated */
2968 newblock
= ext_pblock(&newex
);
2969 allocated
= ext4_ext_get_actual_len(&newex
);
2971 if (extend_disksize
) {
2972 disksize
= ((loff_t
) iblock
+ ar
.len
) << inode
->i_blkbits
;
2973 if (disksize
> i_size_read(inode
))
2974 disksize
= i_size_read(inode
);
2975 if (disksize
> EXT4_I(inode
)->i_disksize
)
2976 EXT4_I(inode
)->i_disksize
= disksize
;
2979 set_buffer_new(bh_result
);
2981 /* Cache only when it is _not_ an uninitialized extent */
2982 if (create
!= EXT4_CREATE_UNINITIALIZED_EXT
)
2983 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
2984 EXT4_EXT_CACHE_EXTENT
);
2986 if (allocated
> max_blocks
)
2987 allocated
= max_blocks
;
2988 ext4_ext_show_leaf(inode
, path
);
2989 set_buffer_mapped(bh_result
);
2990 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
2991 bh_result
->b_blocknr
= newblock
;
2994 ext4_ext_drop_refs(path
);
2997 return err
? err
: allocated
;
3000 void ext4_ext_truncate(struct inode
*inode
)
3002 struct address_space
*mapping
= inode
->i_mapping
;
3003 struct super_block
*sb
= inode
->i_sb
;
3004 ext4_lblk_t last_block
;
3009 * probably first extent we're gonna free will be last in block
3011 err
= ext4_writepage_trans_blocks(inode
);
3012 handle
= ext4_journal_start(inode
, err
);
3016 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3017 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3019 if (ext4_orphan_add(handle
, inode
))
3022 down_write(&EXT4_I(inode
)->i_data_sem
);
3023 ext4_ext_invalidate_cache(inode
);
3025 ext4_discard_preallocations(inode
);
3028 * TODO: optimization is possible here.
3029 * Probably we need not scan at all,
3030 * because page truncation is enough.
3033 /* we have to know where to truncate from in crash case */
3034 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3035 ext4_mark_inode_dirty(handle
, inode
);
3037 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3038 >> EXT4_BLOCK_SIZE_BITS(sb
);
3039 err
= ext4_ext_remove_space(inode
, last_block
);
3041 /* In a multi-transaction truncate, we only make the final
3042 * transaction synchronous.
3045 ext4_handle_sync(handle
);
3048 up_write(&EXT4_I(inode
)->i_data_sem
);
3050 * If this was a simple ftruncate() and the file will remain alive,
3051 * then we need to clear up the orphan record which we created above.
3052 * However, if this was a real unlink then we were called by
3053 * ext4_delete_inode(), and we allow that function to clean up the
3054 * orphan info for us.
3057 ext4_orphan_del(handle
, inode
);
3059 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3060 ext4_mark_inode_dirty(handle
, inode
);
3061 ext4_journal_stop(handle
);
3064 static void ext4_falloc_update_inode(struct inode
*inode
,
3065 int mode
, loff_t new_size
, int update_ctime
)
3067 struct timespec now
;
3070 now
= current_fs_time(inode
->i_sb
);
3071 if (!timespec_equal(&inode
->i_ctime
, &now
))
3072 inode
->i_ctime
= now
;
3075 * Update only when preallocation was requested beyond
3078 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3079 if (new_size
> i_size_read(inode
))
3080 i_size_write(inode
, new_size
);
3081 if (new_size
> EXT4_I(inode
)->i_disksize
)
3082 ext4_update_i_disksize(inode
, new_size
);
3088 * preallocate space for a file. This implements ext4's fallocate inode
3089 * operation, which gets called from sys_fallocate system call.
3090 * For block-mapped files, posix_fallocate should fall back to the method
3091 * of writing zeroes to the required new blocks (the same behavior which is
3092 * expected for file systems which do not support fallocate() system call).
3094 long ext4_fallocate(struct inode
*inode
, int mode
, loff_t offset
, loff_t len
)
3099 unsigned int max_blocks
;
3103 struct buffer_head map_bh
;
3104 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3107 * currently supporting (pre)allocate mode for extent-based
3110 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3113 /* preallocation to directories is currently not supported */
3114 if (S_ISDIR(inode
->i_mode
))
3117 block
= offset
>> blkbits
;
3119 * We can't just convert len to max_blocks because
3120 * If blocksize = 4096 offset = 3072 and len = 2048
3122 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3125 * credits to insert 1 extent into extent tree
3127 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3128 mutex_lock(&inode
->i_mutex
);
3130 while (ret
>= 0 && ret
< max_blocks
) {
3131 block
= block
+ ret
;
3132 max_blocks
= max_blocks
- ret
;
3133 handle
= ext4_journal_start(inode
, credits
);
3134 if (IS_ERR(handle
)) {
3135 ret
= PTR_ERR(handle
);
3138 ret
= ext4_get_blocks_wrap(handle
, inode
, block
,
3139 max_blocks
, &map_bh
,
3140 EXT4_CREATE_UNINITIALIZED_EXT
, 0, 0);
3144 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3145 "returned error inode#%lu, block=%u, "
3146 "max_blocks=%u", __func__
,
3147 inode
->i_ino
, block
, max_blocks
);
3149 ext4_mark_inode_dirty(handle
, inode
);
3150 ret2
= ext4_journal_stop(handle
);
3153 if ((block
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3154 blkbits
) >> blkbits
))
3155 new_size
= offset
+ len
;
3157 new_size
= (block
+ ret
) << blkbits
;
3159 ext4_falloc_update_inode(inode
, mode
, new_size
,
3160 buffer_new(&map_bh
));
3161 ext4_mark_inode_dirty(handle
, inode
);
3162 ret2
= ext4_journal_stop(handle
);
3166 if (ret
== -ENOSPC
&&
3167 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3171 mutex_unlock(&inode
->i_mutex
);
3172 return ret
> 0 ? ret2
: ret
;
3176 * Callback function called for each extent to gather FIEMAP information.
3178 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3179 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3182 struct fiemap_extent_info
*fieinfo
= data
;
3183 unsigned long blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3190 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3192 if (newex
->ec_type
== EXT4_EXT_CACHE_GAP
) {
3195 struct buffer_head
*bh
= NULL
;
3197 offset
= logical
>> PAGE_SHIFT
;
3198 page
= find_get_page(inode
->i_mapping
, offset
);
3199 if (!page
|| !page_has_buffers(page
))
3200 return EXT_CONTINUE
;
3202 bh
= page_buffers(page
);
3205 return EXT_CONTINUE
;
3207 if (buffer_delay(bh
)) {
3208 flags
|= FIEMAP_EXTENT_DELALLOC
;
3209 page_cache_release(page
);
3211 page_cache_release(page
);
3212 return EXT_CONTINUE
;
3216 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3217 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3219 if (ex
&& ext4_ext_is_uninitialized(ex
))
3220 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3223 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3225 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3226 * this also indicates no more allocated blocks.
3228 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3230 if (logical
+ length
- 1 == EXT_MAX_BLOCK
||
3231 ext4_ext_next_allocated_block(path
) == EXT_MAX_BLOCK
)
3232 flags
|= FIEMAP_EXTENT_LAST
;
3234 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3241 return EXT_CONTINUE
;
3244 /* fiemap flags we can handle specified here */
3245 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3247 static int ext4_xattr_fiemap(struct inode
*inode
,
3248 struct fiemap_extent_info
*fieinfo
)
3252 __u32 flags
= FIEMAP_EXTENT_LAST
;
3253 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3257 if (EXT4_I(inode
)->i_state
& EXT4_STATE_XATTR
) {
3258 struct ext4_iloc iloc
;
3259 int offset
; /* offset of xattr in inode */
3261 error
= ext4_get_inode_loc(inode
, &iloc
);
3264 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3265 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3266 EXT4_I(inode
)->i_extra_isize
;
3268 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3269 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3270 } else { /* external block */
3271 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3272 length
= inode
->i_sb
->s_blocksize
;
3276 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3278 return (error
< 0 ? error
: 0);
3281 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3282 __u64 start
, __u64 len
)
3284 ext4_lblk_t start_blk
;
3285 ext4_lblk_t len_blks
;
3288 /* fallback to generic here if not in extents fmt */
3289 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3290 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3293 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3296 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3297 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3299 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3300 len_blks
= len
>> inode
->i_sb
->s_blocksize_bits
;
3303 * Walk the extent tree gathering extent information.
3304 * ext4_ext_fiemap_cb will push extents back to user.
3306 down_write(&EXT4_I(inode
)->i_data_sem
);
3307 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3308 ext4_ext_fiemap_cb
, fieinfo
);
3309 up_write(&EXT4_I(inode
)->i_data_sem
);