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
);
2421 bio
->bi_sector
= ee_pblock
;
2422 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
2426 while (done
< len
) {
2427 ret
= bio_add_page(bio
, ZERO_PAGE(0),
2429 if (ret
!= blocksize
) {
2431 * We can't add any more pages because of
2432 * hardware limitations. Start a new bio.
2437 offset
+= blocksize
;
2438 if (offset
>= PAGE_CACHE_SIZE
)
2442 init_completion(&event
);
2443 bio
->bi_private
= &event
;
2444 bio
->bi_end_io
= bi_complete
;
2445 submit_bio(WRITE
, bio
);
2446 wait_for_completion(&event
);
2448 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2456 ee_pblock
+= done
<< (blkbits
- 9);
2461 #define EXT4_EXT_ZERO_LEN 7
2464 * This function is called by ext4_ext_get_blocks() if someone tries to write
2465 * to an uninitialized extent. It may result in splitting the uninitialized
2466 * extent into multiple extents (upto three - one initialized and two
2468 * There are three possibilities:
2469 * a> There is no split required: Entire extent should be initialized
2470 * b> Splits in two extents: Write is happening at either end of the extent
2471 * c> Splits in three extents: Somone is writing in middle of the extent
2473 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2474 struct inode
*inode
,
2475 struct ext4_ext_path
*path
,
2477 unsigned int max_blocks
)
2479 struct ext4_extent
*ex
, newex
, orig_ex
;
2480 struct ext4_extent
*ex1
= NULL
;
2481 struct ext4_extent
*ex2
= NULL
;
2482 struct ext4_extent
*ex3
= NULL
;
2483 struct ext4_extent_header
*eh
;
2484 ext4_lblk_t ee_block
;
2485 unsigned int allocated
, ee_len
, depth
;
2486 ext4_fsblk_t newblock
;
2490 depth
= ext_depth(inode
);
2491 eh
= path
[depth
].p_hdr
;
2492 ex
= path
[depth
].p_ext
;
2493 ee_block
= le32_to_cpu(ex
->ee_block
);
2494 ee_len
= ext4_ext_get_actual_len(ex
);
2495 allocated
= ee_len
- (iblock
- ee_block
);
2496 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2498 orig_ex
.ee_block
= ex
->ee_block
;
2499 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2500 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2502 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2505 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2506 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
) {
2507 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2509 goto fix_extent_len
;
2510 /* update the extent length and mark as initialized */
2511 ex
->ee_block
= orig_ex
.ee_block
;
2512 ex
->ee_len
= orig_ex
.ee_len
;
2513 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2514 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2515 /* zeroed the full extent */
2519 /* ex1: ee_block to iblock - 1 : uninitialized */
2520 if (iblock
> ee_block
) {
2522 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2523 ext4_ext_mark_uninitialized(ex1
);
2527 * for sanity, update the length of the ex2 extent before
2528 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2529 * overlap of blocks.
2531 if (!ex1
&& allocated
> max_blocks
)
2532 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2533 /* ex3: to ee_block + ee_len : uninitialised */
2534 if (allocated
> max_blocks
) {
2535 unsigned int newdepth
;
2536 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2537 if (allocated
<= EXT4_EXT_ZERO_LEN
) {
2539 * iblock == ee_block is handled by the zerouout
2541 * Mark first half uninitialized.
2542 * Mark second half initialized and zero out the
2543 * initialized extent
2545 ex
->ee_block
= orig_ex
.ee_block
;
2546 ex
->ee_len
= cpu_to_le16(ee_len
- allocated
);
2547 ext4_ext_mark_uninitialized(ex
);
2548 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2549 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2552 ex3
->ee_block
= cpu_to_le32(iblock
);
2553 ext4_ext_store_pblock(ex3
, newblock
);
2554 ex3
->ee_len
= cpu_to_le16(allocated
);
2555 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
);
2556 if (err
== -ENOSPC
) {
2557 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2559 goto fix_extent_len
;
2560 ex
->ee_block
= orig_ex
.ee_block
;
2561 ex
->ee_len
= orig_ex
.ee_len
;
2562 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2563 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2564 /* blocks available from iblock */
2568 goto fix_extent_len
;
2571 * We need to zero out the second half because
2572 * an fallocate request can update file size and
2573 * converting the second half to initialized extent
2574 * implies that we can leak some junk data to user
2577 err
= ext4_ext_zeroout(inode
, ex3
);
2580 * We should actually mark the
2581 * second half as uninit and return error
2582 * Insert would have changed the extent
2584 depth
= ext_depth(inode
);
2585 ext4_ext_drop_refs(path
);
2586 path
= ext4_ext_find_extent(inode
,
2589 err
= PTR_ERR(path
);
2592 /* get the second half extent details */
2593 ex
= path
[depth
].p_ext
;
2594 err
= ext4_ext_get_access(handle
, inode
,
2598 ext4_ext_mark_uninitialized(ex
);
2599 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2603 /* zeroed the second half */
2607 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2608 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2609 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2610 ext4_ext_mark_uninitialized(ex3
);
2611 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
);
2612 if (err
== -ENOSPC
) {
2613 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2615 goto fix_extent_len
;
2616 /* update the extent length and mark as initialized */
2617 ex
->ee_block
= orig_ex
.ee_block
;
2618 ex
->ee_len
= orig_ex
.ee_len
;
2619 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2620 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2621 /* zeroed the full extent */
2622 /* blocks available from iblock */
2626 goto fix_extent_len
;
2628 * The depth, and hence eh & ex might change
2629 * as part of the insert above.
2631 newdepth
= ext_depth(inode
);
2633 * update the extent length after successful insert of the
2636 orig_ex
.ee_len
= cpu_to_le16(ee_len
-
2637 ext4_ext_get_actual_len(ex3
));
2639 ext4_ext_drop_refs(path
);
2640 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2642 err
= PTR_ERR(path
);
2645 eh
= path
[depth
].p_hdr
;
2646 ex
= path
[depth
].p_ext
;
2650 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2654 allocated
= max_blocks
;
2656 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2657 * to insert a extent in the middle zerout directly
2658 * otherwise give the extent a chance to merge to left
2660 if (le16_to_cpu(orig_ex
.ee_len
) <= EXT4_EXT_ZERO_LEN
&&
2661 iblock
!= ee_block
) {
2662 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2664 goto fix_extent_len
;
2665 /* update the extent length and mark as initialized */
2666 ex
->ee_block
= orig_ex
.ee_block
;
2667 ex
->ee_len
= orig_ex
.ee_len
;
2668 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2669 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2670 /* zero out the first half */
2671 /* blocks available from iblock */
2676 * If there was a change of depth as part of the
2677 * insertion of ex3 above, we need to update the length
2678 * of the ex1 extent again here
2680 if (ex1
&& ex1
!= ex
) {
2682 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2683 ext4_ext_mark_uninitialized(ex1
);
2686 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2687 ex2
->ee_block
= cpu_to_le32(iblock
);
2688 ext4_ext_store_pblock(ex2
, newblock
);
2689 ex2
->ee_len
= cpu_to_le16(allocated
);
2693 * New (initialized) extent starts from the first block
2694 * in the current extent. i.e., ex2 == ex
2695 * We have to see if it can be merged with the extent
2698 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2700 * To merge left, pass "ex2 - 1" to try_to_merge(),
2701 * since it merges towards right _only_.
2703 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2705 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2708 depth
= ext_depth(inode
);
2713 * Try to Merge towards right. This might be required
2714 * only when the whole extent is being written to.
2715 * i.e. ex2 == ex and ex3 == NULL.
2718 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2720 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2725 /* Mark modified extent as dirty */
2726 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2729 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2730 if (err
== -ENOSPC
) {
2731 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2733 goto fix_extent_len
;
2734 /* update the extent length and mark as initialized */
2735 ex
->ee_block
= orig_ex
.ee_block
;
2736 ex
->ee_len
= orig_ex
.ee_len
;
2737 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2738 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2739 /* zero out the first half */
2742 goto fix_extent_len
;
2744 return err
? err
: allocated
;
2747 ex
->ee_block
= orig_ex
.ee_block
;
2748 ex
->ee_len
= orig_ex
.ee_len
;
2749 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2750 ext4_ext_mark_uninitialized(ex
);
2751 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2756 * Block allocation/map/preallocation routine for extents based files
2759 * Need to be called with
2760 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2761 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2763 * return > 0, number of of blocks already mapped/allocated
2764 * if create == 0 and these are pre-allocated blocks
2765 * buffer head is unmapped
2766 * otherwise blocks are mapped
2768 * return = 0, if plain look up failed (blocks have not been allocated)
2769 * buffer head is unmapped
2771 * return < 0, error case.
2773 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
2775 unsigned int max_blocks
, struct buffer_head
*bh_result
,
2776 int create
, int extend_disksize
)
2778 struct ext4_ext_path
*path
= NULL
;
2779 struct ext4_extent_header
*eh
;
2780 struct ext4_extent newex
, *ex
;
2781 ext4_fsblk_t newblock
;
2782 int err
= 0, depth
, ret
, cache_type
;
2783 unsigned int allocated
= 0;
2784 struct ext4_allocation_request ar
;
2787 __clear_bit(BH_New
, &bh_result
->b_state
);
2788 ext_debug("blocks %u/%u requested for inode %u\n",
2789 iblock
, max_blocks
, inode
->i_ino
);
2791 /* check in cache */
2792 cache_type
= ext4_ext_in_cache(inode
, iblock
, &newex
);
2794 if (cache_type
== EXT4_EXT_CACHE_GAP
) {
2797 * block isn't allocated yet and
2798 * user doesn't want to allocate it
2802 /* we should allocate requested block */
2803 } else if (cache_type
== EXT4_EXT_CACHE_EXTENT
) {
2804 /* block is already allocated */
2806 - le32_to_cpu(newex
.ee_block
)
2807 + ext_pblock(&newex
);
2808 /* number of remaining blocks in the extent */
2809 allocated
= ext4_ext_get_actual_len(&newex
) -
2810 (iblock
- le32_to_cpu(newex
.ee_block
));
2817 /* find extent for this block */
2818 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
2820 err
= PTR_ERR(path
);
2825 depth
= ext_depth(inode
);
2828 * consistent leaf must not be empty;
2829 * this situation is possible, though, _during_ tree modification;
2830 * this is why assert can't be put in ext4_ext_find_extent()
2832 BUG_ON(path
[depth
].p_ext
== NULL
&& depth
!= 0);
2833 eh
= path
[depth
].p_hdr
;
2835 ex
= path
[depth
].p_ext
;
2837 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
2838 ext4_fsblk_t ee_start
= ext_pblock(ex
);
2839 unsigned short ee_len
;
2842 * Uninitialized extents are treated as holes, except that
2843 * we split out initialized portions during a write.
2845 ee_len
= ext4_ext_get_actual_len(ex
);
2846 /* if found extent covers block, simply return it */
2847 if (iblock
>= ee_block
&& iblock
< ee_block
+ ee_len
) {
2848 newblock
= iblock
- ee_block
+ ee_start
;
2849 /* number of remaining blocks in the extent */
2850 allocated
= ee_len
- (iblock
- ee_block
);
2851 ext_debug("%u fit into %lu:%d -> %llu\n", iblock
,
2852 ee_block
, ee_len
, newblock
);
2854 /* Do not put uninitialized extent in the cache */
2855 if (!ext4_ext_is_uninitialized(ex
)) {
2856 ext4_ext_put_in_cache(inode
, ee_block
,
2858 EXT4_EXT_CACHE_EXTENT
);
2861 if (create
== EXT4_CREATE_UNINITIALIZED_EXT
)
2865 * We have blocks reserved already. We
2866 * return allocated blocks so that delalloc
2867 * won't do block reservation for us. But
2868 * the buffer head will be unmapped so that
2869 * a read from the block returns 0s.
2871 if (allocated
> max_blocks
)
2872 allocated
= max_blocks
;
2873 set_buffer_unwritten(bh_result
);
2877 ret
= ext4_ext_convert_to_initialized(handle
, inode
,
2890 * requested block isn't allocated yet;
2891 * we couldn't try to create block if create flag is zero
2895 * put just found gap into cache to speed up
2896 * subsequent requests
2898 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
2902 * Okay, we need to do block allocation.
2905 /* find neighbour allocated blocks */
2907 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
2911 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
2916 * See if request is beyond maximum number of blocks we can have in
2917 * a single extent. For an initialized extent this limit is
2918 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2919 * EXT_UNINIT_MAX_LEN.
2921 if (max_blocks
> EXT_INIT_MAX_LEN
&&
2922 create
!= EXT4_CREATE_UNINITIALIZED_EXT
)
2923 max_blocks
= EXT_INIT_MAX_LEN
;
2924 else if (max_blocks
> EXT_UNINIT_MAX_LEN
&&
2925 create
== EXT4_CREATE_UNINITIALIZED_EXT
)
2926 max_blocks
= EXT_UNINIT_MAX_LEN
;
2928 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2929 newex
.ee_block
= cpu_to_le32(iblock
);
2930 newex
.ee_len
= cpu_to_le16(max_blocks
);
2931 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
2933 allocated
= ext4_ext_get_actual_len(&newex
);
2935 allocated
= max_blocks
;
2937 /* allocate new block */
2939 ar
.goal
= ext4_ext_find_goal(inode
, path
, iblock
);
2940 ar
.logical
= iblock
;
2942 if (S_ISREG(inode
->i_mode
))
2943 ar
.flags
= EXT4_MB_HINT_DATA
;
2945 /* disable in-core preallocation for non-regular files */
2947 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
2950 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2951 ar
.goal
, newblock
, allocated
);
2953 /* try to insert new extent into found leaf and return */
2954 ext4_ext_store_pblock(&newex
, newblock
);
2955 newex
.ee_len
= cpu_to_le16(ar
.len
);
2956 if (create
== EXT4_CREATE_UNINITIALIZED_EXT
) /* Mark uninitialized */
2957 ext4_ext_mark_uninitialized(&newex
);
2958 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2960 /* free data blocks we just allocated */
2961 /* not a good idea to call discard here directly,
2962 * but otherwise we'd need to call it every free() */
2963 ext4_discard_preallocations(inode
);
2964 ext4_free_blocks(handle
, inode
, ext_pblock(&newex
),
2965 ext4_ext_get_actual_len(&newex
), 0);
2969 /* previous routine could use block we allocated */
2970 newblock
= ext_pblock(&newex
);
2971 allocated
= ext4_ext_get_actual_len(&newex
);
2973 if (extend_disksize
) {
2974 disksize
= ((loff_t
) iblock
+ ar
.len
) << inode
->i_blkbits
;
2975 if (disksize
> i_size_read(inode
))
2976 disksize
= i_size_read(inode
);
2977 if (disksize
> EXT4_I(inode
)->i_disksize
)
2978 EXT4_I(inode
)->i_disksize
= disksize
;
2981 set_buffer_new(bh_result
);
2983 /* Cache only when it is _not_ an uninitialized extent */
2984 if (create
!= EXT4_CREATE_UNINITIALIZED_EXT
)
2985 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
2986 EXT4_EXT_CACHE_EXTENT
);
2988 if (allocated
> max_blocks
)
2989 allocated
= max_blocks
;
2990 ext4_ext_show_leaf(inode
, path
);
2991 set_buffer_mapped(bh_result
);
2992 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
2993 bh_result
->b_blocknr
= newblock
;
2996 ext4_ext_drop_refs(path
);
2999 return err
? err
: allocated
;
3002 void ext4_ext_truncate(struct inode
*inode
)
3004 struct address_space
*mapping
= inode
->i_mapping
;
3005 struct super_block
*sb
= inode
->i_sb
;
3006 ext4_lblk_t last_block
;
3011 * probably first extent we're gonna free will be last in block
3013 err
= ext4_writepage_trans_blocks(inode
);
3014 handle
= ext4_journal_start(inode
, err
);
3018 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3019 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3021 if (ext4_orphan_add(handle
, inode
))
3024 down_write(&EXT4_I(inode
)->i_data_sem
);
3025 ext4_ext_invalidate_cache(inode
);
3027 ext4_discard_preallocations(inode
);
3030 * TODO: optimization is possible here.
3031 * Probably we need not scan at all,
3032 * because page truncation is enough.
3035 /* we have to know where to truncate from in crash case */
3036 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3037 ext4_mark_inode_dirty(handle
, inode
);
3039 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3040 >> EXT4_BLOCK_SIZE_BITS(sb
);
3041 err
= ext4_ext_remove_space(inode
, last_block
);
3043 /* In a multi-transaction truncate, we only make the final
3044 * transaction synchronous.
3047 ext4_handle_sync(handle
);
3050 up_write(&EXT4_I(inode
)->i_data_sem
);
3052 * If this was a simple ftruncate() and the file will remain alive,
3053 * then we need to clear up the orphan record which we created above.
3054 * However, if this was a real unlink then we were called by
3055 * ext4_delete_inode(), and we allow that function to clean up the
3056 * orphan info for us.
3059 ext4_orphan_del(handle
, inode
);
3061 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3062 ext4_mark_inode_dirty(handle
, inode
);
3063 ext4_journal_stop(handle
);
3066 static void ext4_falloc_update_inode(struct inode
*inode
,
3067 int mode
, loff_t new_size
, int update_ctime
)
3069 struct timespec now
;
3072 now
= current_fs_time(inode
->i_sb
);
3073 if (!timespec_equal(&inode
->i_ctime
, &now
))
3074 inode
->i_ctime
= now
;
3077 * Update only when preallocation was requested beyond
3080 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3081 if (new_size
> i_size_read(inode
))
3082 i_size_write(inode
, new_size
);
3083 if (new_size
> EXT4_I(inode
)->i_disksize
)
3084 ext4_update_i_disksize(inode
, new_size
);
3090 * preallocate space for a file. This implements ext4's fallocate inode
3091 * operation, which gets called from sys_fallocate system call.
3092 * For block-mapped files, posix_fallocate should fall back to the method
3093 * of writing zeroes to the required new blocks (the same behavior which is
3094 * expected for file systems which do not support fallocate() system call).
3096 long ext4_fallocate(struct inode
*inode
, int mode
, loff_t offset
, loff_t len
)
3101 unsigned int max_blocks
;
3105 struct buffer_head map_bh
;
3106 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3109 * currently supporting (pre)allocate mode for extent-based
3112 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3115 /* preallocation to directories is currently not supported */
3116 if (S_ISDIR(inode
->i_mode
))
3119 block
= offset
>> blkbits
;
3121 * We can't just convert len to max_blocks because
3122 * If blocksize = 4096 offset = 3072 and len = 2048
3124 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3127 * credits to insert 1 extent into extent tree
3129 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3130 mutex_lock(&inode
->i_mutex
);
3132 while (ret
>= 0 && ret
< max_blocks
) {
3133 block
= block
+ ret
;
3134 max_blocks
= max_blocks
- ret
;
3135 handle
= ext4_journal_start(inode
, credits
);
3136 if (IS_ERR(handle
)) {
3137 ret
= PTR_ERR(handle
);
3140 ret
= ext4_get_blocks_wrap(handle
, inode
, block
,
3141 max_blocks
, &map_bh
,
3142 EXT4_CREATE_UNINITIALIZED_EXT
, 0, 0);
3146 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3147 "returned error inode#%lu, block=%u, "
3148 "max_blocks=%u", __func__
,
3149 inode
->i_ino
, block
, max_blocks
);
3151 ext4_mark_inode_dirty(handle
, inode
);
3152 ret2
= ext4_journal_stop(handle
);
3155 if ((block
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3156 blkbits
) >> blkbits
))
3157 new_size
= offset
+ len
;
3159 new_size
= (block
+ ret
) << blkbits
;
3161 ext4_falloc_update_inode(inode
, mode
, new_size
,
3162 buffer_new(&map_bh
));
3163 ext4_mark_inode_dirty(handle
, inode
);
3164 ret2
= ext4_journal_stop(handle
);
3168 if (ret
== -ENOSPC
&&
3169 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3173 mutex_unlock(&inode
->i_mutex
);
3174 return ret
> 0 ? ret2
: ret
;
3178 * Callback function called for each extent to gather FIEMAP information.
3180 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3181 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3184 struct fiemap_extent_info
*fieinfo
= data
;
3185 unsigned long blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3192 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3194 if (newex
->ec_type
== EXT4_EXT_CACHE_GAP
) {
3197 struct buffer_head
*bh
= NULL
;
3199 offset
= logical
>> PAGE_SHIFT
;
3200 page
= find_get_page(inode
->i_mapping
, offset
);
3201 if (!page
|| !page_has_buffers(page
))
3202 return EXT_CONTINUE
;
3204 bh
= page_buffers(page
);
3207 return EXT_CONTINUE
;
3209 if (buffer_delay(bh
)) {
3210 flags
|= FIEMAP_EXTENT_DELALLOC
;
3211 page_cache_release(page
);
3213 page_cache_release(page
);
3214 return EXT_CONTINUE
;
3218 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3219 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3221 if (ex
&& ext4_ext_is_uninitialized(ex
))
3222 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3225 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3227 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3228 * this also indicates no more allocated blocks.
3230 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3232 if (logical
+ length
- 1 == EXT_MAX_BLOCK
||
3233 ext4_ext_next_allocated_block(path
) == EXT_MAX_BLOCK
)
3234 flags
|= FIEMAP_EXTENT_LAST
;
3236 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3243 return EXT_CONTINUE
;
3246 /* fiemap flags we can handle specified here */
3247 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3249 static int ext4_xattr_fiemap(struct inode
*inode
,
3250 struct fiemap_extent_info
*fieinfo
)
3254 __u32 flags
= FIEMAP_EXTENT_LAST
;
3255 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3259 if (EXT4_I(inode
)->i_state
& EXT4_STATE_XATTR
) {
3260 struct ext4_iloc iloc
;
3261 int offset
; /* offset of xattr in inode */
3263 error
= ext4_get_inode_loc(inode
, &iloc
);
3266 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3267 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3268 EXT4_I(inode
)->i_extra_isize
;
3270 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3271 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3272 } else { /* external block */
3273 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3274 length
= inode
->i_sb
->s_blocksize
;
3278 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3280 return (error
< 0 ? error
: 0);
3283 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3284 __u64 start
, __u64 len
)
3286 ext4_lblk_t start_blk
;
3287 ext4_lblk_t len_blks
;
3290 /* fallback to generic here if not in extents fmt */
3291 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3292 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3295 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3298 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3299 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3301 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3302 len_blks
= len
>> inode
->i_sb
->s_blocksize_bits
;
3305 * Walk the extent tree gathering extent information.
3306 * ext4_ext_fiemap_cb will push extents back to user.
3308 down_write(&EXT4_I(inode
)->i_data_sem
);
3309 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3310 ext4_ext_fiemap_cb
, fieinfo
);
3311 up_write(&EXT4_I(inode
)->i_data_sem
);