2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include <linux/fiemap.h>
44 #include "ext4_jbd2.h"
45 #include "ext4_extents.h"
50 * combine low and high parts of physical block number into ext4_fsblk_t
52 ext4_fsblk_t
ext_pblock(struct ext4_extent
*ex
)
56 block
= le32_to_cpu(ex
->ee_start_lo
);
57 block
|= ((ext4_fsblk_t
) le16_to_cpu(ex
->ee_start_hi
) << 31) << 1;
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
65 ext4_fsblk_t
idx_pblock(struct ext4_extent_idx
*ix
)
69 block
= le32_to_cpu(ix
->ei_leaf_lo
);
70 block
|= ((ext4_fsblk_t
) le16_to_cpu(ix
->ei_leaf_hi
) << 31) << 1;
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
79 void ext4_ext_store_pblock(struct ext4_extent
*ex
, ext4_fsblk_t pb
)
81 ex
->ee_start_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
82 ex
->ee_start_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
90 static void ext4_idx_store_pblock(struct ext4_extent_idx
*ix
, ext4_fsblk_t pb
)
92 ix
->ei_leaf_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
93 ix
->ei_leaf_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
96 static int ext4_ext_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
);
332 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, len
);
335 static int ext4_valid_extent_idx(struct inode
*inode
,
336 struct ext4_extent_idx
*ext_idx
)
338 ext4_fsblk_t block
= idx_pblock(ext_idx
);
340 return ext4_data_block_valid(EXT4_SB(inode
->i_sb
), block
, 1);
343 static int ext4_valid_extent_entries(struct inode
*inode
,
344 struct ext4_extent_header
*eh
,
347 struct ext4_extent
*ext
;
348 struct ext4_extent_idx
*ext_idx
;
349 unsigned short entries
;
350 if (eh
->eh_entries
== 0)
353 entries
= le16_to_cpu(eh
->eh_entries
);
357 ext
= EXT_FIRST_EXTENT(eh
);
359 if (!ext4_valid_extent(inode
, ext
))
365 ext_idx
= EXT_FIRST_INDEX(eh
);
367 if (!ext4_valid_extent_idx(inode
, ext_idx
))
376 static int __ext4_ext_check(const char *function
, struct inode
*inode
,
377 struct ext4_extent_header
*eh
,
380 const char *error_msg
;
383 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
384 error_msg
= "invalid magic";
387 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
388 error_msg
= "unexpected eh_depth";
391 if (unlikely(eh
->eh_max
== 0)) {
392 error_msg
= "invalid eh_max";
395 max
= ext4_ext_max_entries(inode
, depth
);
396 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
397 error_msg
= "too large eh_max";
400 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
401 error_msg
= "invalid eh_entries";
404 if (!ext4_valid_extent_entries(inode
, eh
, depth
)) {
405 error_msg
= "invalid extent entries";
411 ext4_error(inode
->i_sb
, function
,
412 "bad header/extent in inode #%lu: %s - magic %x, "
413 "entries %u, max %u(%u), depth %u(%u)",
414 inode
->i_ino
, error_msg
, le16_to_cpu(eh
->eh_magic
),
415 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
416 max
, le16_to_cpu(eh
->eh_depth
), depth
);
421 #define ext4_ext_check(inode, eh, depth) \
422 __ext4_ext_check(__func__, inode, eh, depth)
424 int ext4_ext_check_inode(struct inode
*inode
)
426 return ext4_ext_check(inode
, ext_inode_hdr(inode
), ext_depth(inode
));
430 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
432 int k
, l
= path
->p_depth
;
435 for (k
= 0; k
<= l
; k
++, path
++) {
437 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
438 idx_pblock(path
->p_idx
));
439 } else if (path
->p_ext
) {
440 ext_debug(" %d:%d:%llu ",
441 le32_to_cpu(path
->p_ext
->ee_block
),
442 ext4_ext_get_actual_len(path
->p_ext
),
443 ext_pblock(path
->p_ext
));
450 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
452 int depth
= ext_depth(inode
);
453 struct ext4_extent_header
*eh
;
454 struct ext4_extent
*ex
;
460 eh
= path
[depth
].p_hdr
;
461 ex
= EXT_FIRST_EXTENT(eh
);
463 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
464 ext_debug("%d:%d:%llu ", le32_to_cpu(ex
->ee_block
),
465 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
470 #define ext4_ext_show_path(inode, path)
471 #define ext4_ext_show_leaf(inode, path)
474 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
476 int depth
= path
->p_depth
;
479 for (i
= 0; i
<= depth
; i
++, path
++)
487 * ext4_ext_binsearch_idx:
488 * binary search for the closest index of the given block
489 * the header must be checked before calling this
492 ext4_ext_binsearch_idx(struct inode
*inode
,
493 struct ext4_ext_path
*path
, ext4_lblk_t block
)
495 struct ext4_extent_header
*eh
= path
->p_hdr
;
496 struct ext4_extent_idx
*r
, *l
, *m
;
499 ext_debug("binsearch for %u(idx): ", block
);
501 l
= EXT_FIRST_INDEX(eh
) + 1;
502 r
= EXT_LAST_INDEX(eh
);
505 if (block
< le32_to_cpu(m
->ei_block
))
509 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
510 m
, le32_to_cpu(m
->ei_block
),
511 r
, le32_to_cpu(r
->ei_block
));
515 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
516 idx_pblock(path
->p_idx
));
518 #ifdef CHECK_BINSEARCH
520 struct ext4_extent_idx
*chix
, *ix
;
523 chix
= ix
= EXT_FIRST_INDEX(eh
);
524 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
526 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
527 printk(KERN_DEBUG
"k=%d, ix=0x%p, "
529 ix
, EXT_FIRST_INDEX(eh
));
530 printk(KERN_DEBUG
"%u <= %u\n",
531 le32_to_cpu(ix
->ei_block
),
532 le32_to_cpu(ix
[-1].ei_block
));
534 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
535 <= le32_to_cpu(ix
[-1].ei_block
));
536 if (block
< le32_to_cpu(ix
->ei_block
))
540 BUG_ON(chix
!= path
->p_idx
);
547 * ext4_ext_binsearch:
548 * binary search for closest extent of the given block
549 * the header must be checked before calling this
552 ext4_ext_binsearch(struct inode
*inode
,
553 struct ext4_ext_path
*path
, ext4_lblk_t block
)
555 struct ext4_extent_header
*eh
= path
->p_hdr
;
556 struct ext4_extent
*r
, *l
, *m
;
558 if (eh
->eh_entries
== 0) {
560 * this leaf is empty:
561 * we get such a leaf in split/add case
566 ext_debug("binsearch for %u: ", block
);
568 l
= EXT_FIRST_EXTENT(eh
) + 1;
569 r
= EXT_LAST_EXTENT(eh
);
573 if (block
< le32_to_cpu(m
->ee_block
))
577 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
578 m
, le32_to_cpu(m
->ee_block
),
579 r
, le32_to_cpu(r
->ee_block
));
583 ext_debug(" -> %d:%llu:%d ",
584 le32_to_cpu(path
->p_ext
->ee_block
),
585 ext_pblock(path
->p_ext
),
586 ext4_ext_get_actual_len(path
->p_ext
));
588 #ifdef CHECK_BINSEARCH
590 struct ext4_extent
*chex
, *ex
;
593 chex
= ex
= EXT_FIRST_EXTENT(eh
);
594 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
595 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
596 <= le32_to_cpu(ex
[-1].ee_block
));
597 if (block
< le32_to_cpu(ex
->ee_block
))
601 BUG_ON(chex
!= path
->p_ext
);
607 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
609 struct ext4_extent_header
*eh
;
611 eh
= ext_inode_hdr(inode
);
614 eh
->eh_magic
= EXT4_EXT_MAGIC
;
615 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
));
616 ext4_mark_inode_dirty(handle
, inode
);
617 ext4_ext_invalidate_cache(inode
);
621 struct ext4_ext_path
*
622 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
623 struct ext4_ext_path
*path
)
625 struct ext4_extent_header
*eh
;
626 struct buffer_head
*bh
;
627 short int depth
, i
, ppos
= 0, alloc
= 0;
629 eh
= ext_inode_hdr(inode
);
630 depth
= ext_depth(inode
);
632 /* account possible depth increase */
634 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
637 return ERR_PTR(-ENOMEM
);
644 /* walk through the tree */
646 int need_to_validate
= 0;
648 ext_debug("depth %d: num %d, max %d\n",
649 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
651 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
652 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
653 path
[ppos
].p_depth
= i
;
654 path
[ppos
].p_ext
= NULL
;
656 bh
= sb_getblk(inode
->i_sb
, path
[ppos
].p_block
);
659 if (!bh_uptodate_or_lock(bh
)) {
660 if (bh_submit_read(bh
) < 0) {
664 /* validate the extent entries */
665 need_to_validate
= 1;
667 eh
= ext_block_hdr(bh
);
669 BUG_ON(ppos
> depth
);
670 path
[ppos
].p_bh
= bh
;
671 path
[ppos
].p_hdr
= eh
;
674 if (need_to_validate
&& ext4_ext_check(inode
, eh
, i
))
678 path
[ppos
].p_depth
= i
;
679 path
[ppos
].p_ext
= NULL
;
680 path
[ppos
].p_idx
= NULL
;
683 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
684 /* if not an empty leaf */
685 if (path
[ppos
].p_ext
)
686 path
[ppos
].p_block
= ext_pblock(path
[ppos
].p_ext
);
688 ext4_ext_show_path(inode
, path
);
693 ext4_ext_drop_refs(path
);
696 return ERR_PTR(-EIO
);
700 * ext4_ext_insert_index:
701 * insert new index [@logical;@ptr] into the block at @curp;
702 * check where to insert: before @curp or after @curp
704 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
705 struct ext4_ext_path
*curp
,
706 int logical
, ext4_fsblk_t ptr
)
708 struct ext4_extent_idx
*ix
;
711 err
= ext4_ext_get_access(handle
, inode
, curp
);
715 BUG_ON(logical
== le32_to_cpu(curp
->p_idx
->ei_block
));
716 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
717 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
719 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
720 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
721 len
= len
< 0 ? 0 : len
;
722 ext_debug("insert new index %d after: %llu. "
723 "move %d from 0x%p to 0x%p\n",
725 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
726 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
728 ix
= curp
->p_idx
+ 1;
731 len
= len
* sizeof(struct ext4_extent_idx
);
732 len
= len
< 0 ? 0 : len
;
733 ext_debug("insert new index %d before: %llu. "
734 "move %d from 0x%p to 0x%p\n",
736 curp
->p_idx
, (curp
->p_idx
+ 1));
737 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
741 ix
->ei_block
= cpu_to_le32(logical
);
742 ext4_idx_store_pblock(ix
, ptr
);
743 le16_add_cpu(&curp
->p_hdr
->eh_entries
, 1);
745 BUG_ON(le16_to_cpu(curp
->p_hdr
->eh_entries
)
746 > le16_to_cpu(curp
->p_hdr
->eh_max
));
747 BUG_ON(ix
> EXT_LAST_INDEX(curp
->p_hdr
));
749 err
= ext4_ext_dirty(handle
, inode
, curp
);
750 ext4_std_error(inode
->i_sb
, err
);
757 * inserts new subtree into the path, using free index entry
759 * - allocates all needed blocks (new leaf and all intermediate index blocks)
760 * - makes decision where to split
761 * - moves remaining extents and index entries (right to the split point)
762 * into the newly allocated blocks
763 * - initializes subtree
765 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
766 struct ext4_ext_path
*path
,
767 struct ext4_extent
*newext
, int at
)
769 struct buffer_head
*bh
= NULL
;
770 int depth
= ext_depth(inode
);
771 struct ext4_extent_header
*neh
;
772 struct ext4_extent_idx
*fidx
;
773 struct ext4_extent
*ex
;
775 ext4_fsblk_t newblock
, oldblock
;
777 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
780 /* make decision: where to split? */
781 /* FIXME: now decision is simplest: at current extent */
783 /* if current leaf will be split, then we should use
784 * border from split point */
785 BUG_ON(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
));
786 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
787 border
= path
[depth
].p_ext
[1].ee_block
;
788 ext_debug("leaf will be split."
789 " next leaf starts at %d\n",
790 le32_to_cpu(border
));
792 border
= newext
->ee_block
;
793 ext_debug("leaf will be added."
794 " next leaf starts at %d\n",
795 le32_to_cpu(border
));
799 * If error occurs, then we break processing
800 * and mark filesystem read-only. index won't
801 * be inserted and tree will be in consistent
802 * state. Next mount will repair buffers too.
806 * Get array to track all allocated blocks.
807 * We need this to handle errors and free blocks
810 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
814 /* allocate all needed blocks */
815 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
816 for (a
= 0; a
< depth
- at
; a
++) {
817 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
,
821 ablocks
[a
] = newblock
;
824 /* initialize new leaf */
825 newblock
= ablocks
[--a
];
826 BUG_ON(newblock
== 0);
827 bh
= sb_getblk(inode
->i_sb
, newblock
);
834 err
= ext4_journal_get_create_access(handle
, bh
);
838 neh
= ext_block_hdr(bh
);
840 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
841 neh
->eh_magic
= EXT4_EXT_MAGIC
;
843 ex
= EXT_FIRST_EXTENT(neh
);
845 /* move remainder of path[depth] to the new leaf */
846 BUG_ON(path
[depth
].p_hdr
->eh_entries
!= path
[depth
].p_hdr
->eh_max
);
847 /* start copy from next extent */
848 /* TODO: we could do it by single memmove */
851 while (path
[depth
].p_ext
<=
852 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
853 ext_debug("move %d:%llu:%d in new leaf %llu\n",
854 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
855 ext_pblock(path
[depth
].p_ext
),
856 ext4_ext_get_actual_len(path
[depth
].p_ext
),
858 /*memmove(ex++, path[depth].p_ext++,
859 sizeof(struct ext4_extent));
865 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
866 le16_add_cpu(&neh
->eh_entries
, m
);
869 set_buffer_uptodate(bh
);
872 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
878 /* correct old leaf */
880 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
883 le16_add_cpu(&path
[depth
].p_hdr
->eh_entries
, -m
);
884 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
890 /* create intermediate indexes */
894 ext_debug("create %d intermediate indices\n", k
);
895 /* insert new index into current index block */
896 /* current depth stored in i var */
900 newblock
= ablocks
[--a
];
901 bh
= sb_getblk(inode
->i_sb
, newblock
);
908 err
= ext4_journal_get_create_access(handle
, bh
);
912 neh
= ext_block_hdr(bh
);
913 neh
->eh_entries
= cpu_to_le16(1);
914 neh
->eh_magic
= EXT4_EXT_MAGIC
;
915 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
916 neh
->eh_depth
= cpu_to_le16(depth
- i
);
917 fidx
= EXT_FIRST_INDEX(neh
);
918 fidx
->ei_block
= border
;
919 ext4_idx_store_pblock(fidx
, oldblock
);
921 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
922 i
, newblock
, le32_to_cpu(border
), oldblock
);
927 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
928 EXT_MAX_INDEX(path
[i
].p_hdr
));
929 BUG_ON(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
930 EXT_LAST_INDEX(path
[i
].p_hdr
));
931 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
932 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
933 le32_to_cpu(path
[i
].p_idx
->ei_block
),
934 idx_pblock(path
[i
].p_idx
),
936 /*memmove(++fidx, path[i].p_idx++,
937 sizeof(struct ext4_extent_idx));
939 BUG_ON(neh->eh_entries > neh->eh_max);*/
944 memmove(++fidx
, path
[i
].p_idx
- m
,
945 sizeof(struct ext4_extent_idx
) * m
);
946 le16_add_cpu(&neh
->eh_entries
, m
);
948 set_buffer_uptodate(bh
);
951 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
957 /* correct old index */
959 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
962 le16_add_cpu(&path
[i
].p_hdr
->eh_entries
, -m
);
963 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
971 /* insert new index */
972 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
973 le32_to_cpu(border
), newblock
);
977 if (buffer_locked(bh
))
983 /* free all allocated blocks in error case */
984 for (i
= 0; i
< depth
; i
++) {
987 ext4_free_blocks(handle
, inode
, ablocks
[i
], 1, 1);
996 * ext4_ext_grow_indepth:
997 * implements tree growing procedure:
998 * - allocates new block
999 * - moves top-level data (index block or leaf) into the new block
1000 * - initializes new top-level, creating index that points to the
1001 * just created block
1003 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
1004 struct ext4_ext_path
*path
,
1005 struct ext4_extent
*newext
)
1007 struct ext4_ext_path
*curp
= path
;
1008 struct ext4_extent_header
*neh
;
1009 struct ext4_extent_idx
*fidx
;
1010 struct buffer_head
*bh
;
1011 ext4_fsblk_t newblock
;
1014 newblock
= ext4_ext_new_meta_block(handle
, inode
, path
, newext
, &err
);
1018 bh
= sb_getblk(inode
->i_sb
, newblock
);
1021 ext4_std_error(inode
->i_sb
, err
);
1026 err
= ext4_journal_get_create_access(handle
, bh
);
1032 /* move top-level index/leaf into new block */
1033 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
1035 /* set size of new block */
1036 neh
= ext_block_hdr(bh
);
1037 /* old root could have indexes or leaves
1038 * so calculate e_max right way */
1039 if (ext_depth(inode
))
1040 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
1042 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
1043 neh
->eh_magic
= EXT4_EXT_MAGIC
;
1044 set_buffer_uptodate(bh
);
1047 err
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1051 /* create index in new top-level index: num,max,pointer */
1052 err
= ext4_ext_get_access(handle
, inode
, curp
);
1056 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
1057 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
));
1058 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
1059 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
1061 if (path
[0].p_hdr
->eh_depth
)
1062 curp
->p_idx
->ei_block
=
1063 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
1065 curp
->p_idx
->ei_block
=
1066 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
1067 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
1069 neh
= ext_inode_hdr(inode
);
1070 fidx
= EXT_FIRST_INDEX(neh
);
1071 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1072 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
1073 le32_to_cpu(fidx
->ei_block
), idx_pblock(fidx
));
1075 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
1076 err
= ext4_ext_dirty(handle
, inode
, curp
);
1084 * ext4_ext_create_new_leaf:
1085 * finds empty index and adds new leaf.
1086 * if no free index is found, then it requests in-depth growing.
1088 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
1089 struct ext4_ext_path
*path
,
1090 struct ext4_extent
*newext
)
1092 struct ext4_ext_path
*curp
;
1093 int depth
, i
, err
= 0;
1096 i
= depth
= ext_depth(inode
);
1098 /* walk up to the tree and look for free index entry */
1099 curp
= path
+ depth
;
1100 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
1105 /* we use already allocated block for index block,
1106 * so subsequent data blocks should be contiguous */
1107 if (EXT_HAS_FREE_INDEX(curp
)) {
1108 /* if we found index with free entry, then use that
1109 * entry: create all needed subtree and add new leaf */
1110 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
1115 ext4_ext_drop_refs(path
);
1116 path
= ext4_ext_find_extent(inode
,
1117 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1120 err
= PTR_ERR(path
);
1122 /* tree is full, time to grow in depth */
1123 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1128 ext4_ext_drop_refs(path
);
1129 path
= ext4_ext_find_extent(inode
,
1130 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1133 err
= PTR_ERR(path
);
1138 * only first (depth 0 -> 1) produces free space;
1139 * in all other cases we have to split the grown tree
1141 depth
= ext_depth(inode
);
1142 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1143 /* now we need to split */
1153 * search the closest allocated block to the left for *logical
1154 * and returns it at @logical + it's physical address at @phys
1155 * if *logical is the smallest allocated block, the function
1156 * returns 0 at @phys
1157 * return value contains 0 (success) or error code
1160 ext4_ext_search_left(struct inode
*inode
, struct ext4_ext_path
*path
,
1161 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1163 struct ext4_extent_idx
*ix
;
1164 struct ext4_extent
*ex
;
1167 BUG_ON(path
== NULL
);
1168 depth
= path
->p_depth
;
1171 if (depth
== 0 && path
->p_ext
== NULL
)
1174 /* usually extent in the path covers blocks smaller
1175 * then *logical, but it can be that extent is the
1176 * first one in the file */
1178 ex
= path
[depth
].p_ext
;
1179 ee_len
= ext4_ext_get_actual_len(ex
);
1180 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1181 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1182 while (--depth
>= 0) {
1183 ix
= path
[depth
].p_idx
;
1184 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1189 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1191 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1192 *phys
= ext_pblock(ex
) + ee_len
- 1;
1197 * search the closest allocated block to the right for *logical
1198 * and returns it at @logical + it's physical address at @phys
1199 * if *logical is the smallest allocated block, the function
1200 * returns 0 at @phys
1201 * return value contains 0 (success) or error code
1204 ext4_ext_search_right(struct inode
*inode
, struct ext4_ext_path
*path
,
1205 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1207 struct buffer_head
*bh
= NULL
;
1208 struct ext4_extent_header
*eh
;
1209 struct ext4_extent_idx
*ix
;
1210 struct ext4_extent
*ex
;
1212 int depth
; /* Note, NOT eh_depth; depth from top of tree */
1215 BUG_ON(path
== NULL
);
1216 depth
= path
->p_depth
;
1219 if (depth
== 0 && path
->p_ext
== NULL
)
1222 /* usually extent in the path covers blocks smaller
1223 * then *logical, but it can be that extent is the
1224 * first one in the file */
1226 ex
= path
[depth
].p_ext
;
1227 ee_len
= ext4_ext_get_actual_len(ex
);
1228 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1229 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1230 while (--depth
>= 0) {
1231 ix
= path
[depth
].p_idx
;
1232 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1234 *logical
= le32_to_cpu(ex
->ee_block
);
1235 *phys
= ext_pblock(ex
);
1239 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1241 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1242 /* next allocated block in this leaf */
1244 *logical
= le32_to_cpu(ex
->ee_block
);
1245 *phys
= ext_pblock(ex
);
1249 /* go up and search for index to the right */
1250 while (--depth
>= 0) {
1251 ix
= path
[depth
].p_idx
;
1252 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1256 /* we've gone up to the root and found no index to the right */
1260 /* we've found index to the right, let's
1261 * follow it and find the closest allocated
1262 * block to the right */
1264 block
= idx_pblock(ix
);
1265 while (++depth
< path
->p_depth
) {
1266 bh
= sb_bread(inode
->i_sb
, block
);
1269 eh
= ext_block_hdr(bh
);
1270 /* subtract from p_depth to get proper eh_depth */
1271 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1275 ix
= EXT_FIRST_INDEX(eh
);
1276 block
= idx_pblock(ix
);
1280 bh
= sb_bread(inode
->i_sb
, block
);
1283 eh
= ext_block_hdr(bh
);
1284 if (ext4_ext_check(inode
, eh
, path
->p_depth
- depth
)) {
1288 ex
= EXT_FIRST_EXTENT(eh
);
1289 *logical
= le32_to_cpu(ex
->ee_block
);
1290 *phys
= ext_pblock(ex
);
1296 * ext4_ext_next_allocated_block:
1297 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1298 * NOTE: it considers block number from index entry as
1299 * allocated block. Thus, index entries have to be consistent
1303 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1307 BUG_ON(path
== NULL
);
1308 depth
= path
->p_depth
;
1310 if (depth
== 0 && path
->p_ext
== NULL
)
1311 return EXT_MAX_BLOCK
;
1313 while (depth
>= 0) {
1314 if (depth
== path
->p_depth
) {
1316 if (path
[depth
].p_ext
!=
1317 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1318 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1321 if (path
[depth
].p_idx
!=
1322 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1323 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1328 return EXT_MAX_BLOCK
;
1332 * ext4_ext_next_leaf_block:
1333 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1335 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1336 struct ext4_ext_path
*path
)
1340 BUG_ON(path
== NULL
);
1341 depth
= path
->p_depth
;
1343 /* zero-tree has no leaf blocks at all */
1345 return EXT_MAX_BLOCK
;
1347 /* go to index block */
1350 while (depth
>= 0) {
1351 if (path
[depth
].p_idx
!=
1352 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1353 return (ext4_lblk_t
)
1354 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1358 return EXT_MAX_BLOCK
;
1362 * ext4_ext_correct_indexes:
1363 * if leaf gets modified and modified extent is first in the leaf,
1364 * then we have to correct all indexes above.
1365 * TODO: do we need to correct tree in all cases?
1367 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1368 struct ext4_ext_path
*path
)
1370 struct ext4_extent_header
*eh
;
1371 int depth
= ext_depth(inode
);
1372 struct ext4_extent
*ex
;
1376 eh
= path
[depth
].p_hdr
;
1377 ex
= path
[depth
].p_ext
;
1382 /* there is no tree at all */
1386 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1387 /* we correct tree if first leaf got modified only */
1392 * TODO: we need correction if border is smaller than current one
1395 border
= path
[depth
].p_ext
->ee_block
;
1396 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1399 path
[k
].p_idx
->ei_block
= border
;
1400 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1405 /* change all left-side indexes */
1406 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1408 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1411 path
[k
].p_idx
->ei_block
= border
;
1412 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1421 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1422 struct ext4_extent
*ex2
)
1424 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1427 * Make sure that either both extents are uninitialized, or
1430 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1433 if (ext4_ext_is_uninitialized(ex1
))
1434 max_len
= EXT_UNINIT_MAX_LEN
;
1436 max_len
= EXT_INIT_MAX_LEN
;
1438 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1439 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1441 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1442 le32_to_cpu(ex2
->ee_block
))
1446 * To allow future support for preallocated extents to be added
1447 * as an RO_COMPAT feature, refuse to merge to extents if
1448 * this can result in the top bit of ee_len being set.
1450 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1452 #ifdef AGGRESSIVE_TEST
1453 if (ext1_ee_len
>= 4)
1457 if (ext_pblock(ex1
) + ext1_ee_len
== ext_pblock(ex2
))
1463 * This function tries to merge the "ex" extent to the next extent in the tree.
1464 * It always tries to merge towards right. If you want to merge towards
1465 * left, pass "ex - 1" as argument instead of "ex".
1466 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1467 * 1 if they got merged.
1469 int ext4_ext_try_to_merge(struct inode
*inode
,
1470 struct ext4_ext_path
*path
,
1471 struct ext4_extent
*ex
)
1473 struct ext4_extent_header
*eh
;
1474 unsigned int depth
, len
;
1476 int uninitialized
= 0;
1478 depth
= ext_depth(inode
);
1479 BUG_ON(path
[depth
].p_hdr
== NULL
);
1480 eh
= path
[depth
].p_hdr
;
1482 while (ex
< EXT_LAST_EXTENT(eh
)) {
1483 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1485 /* merge with next extent! */
1486 if (ext4_ext_is_uninitialized(ex
))
1488 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1489 + ext4_ext_get_actual_len(ex
+ 1));
1491 ext4_ext_mark_uninitialized(ex
);
1493 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1494 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1495 * sizeof(struct ext4_extent
);
1496 memmove(ex
+ 1, ex
+ 2, len
);
1498 le16_add_cpu(&eh
->eh_entries
, -1);
1500 WARN_ON(eh
->eh_entries
== 0);
1501 if (!eh
->eh_entries
)
1502 ext4_error(inode
->i_sb
, "ext4_ext_try_to_merge",
1503 "inode#%lu, eh->eh_entries = 0!", inode
->i_ino
);
1510 * check if a portion of the "newext" extent overlaps with an
1513 * If there is an overlap discovered, it updates the length of the newext
1514 * such that there will be no overlap, and then returns 1.
1515 * If there is no overlap found, it returns 0.
1517 unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1518 struct ext4_extent
*newext
,
1519 struct ext4_ext_path
*path
)
1522 unsigned int depth
, len1
;
1523 unsigned int ret
= 0;
1525 b1
= le32_to_cpu(newext
->ee_block
);
1526 len1
= ext4_ext_get_actual_len(newext
);
1527 depth
= ext_depth(inode
);
1528 if (!path
[depth
].p_ext
)
1530 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1533 * get the next allocated block if the extent in the path
1534 * is before the requested block(s)
1537 b2
= ext4_ext_next_allocated_block(path
);
1538 if (b2
== EXT_MAX_BLOCK
)
1542 /* check for wrap through zero on extent logical start block*/
1543 if (b1
+ len1
< b1
) {
1544 len1
= EXT_MAX_BLOCK
- b1
;
1545 newext
->ee_len
= cpu_to_le16(len1
);
1549 /* check for overlap */
1550 if (b1
+ len1
> b2
) {
1551 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1559 * ext4_ext_insert_extent:
1560 * tries to merge requsted extent into the existing extent or
1561 * inserts requested extent as new one into the tree,
1562 * creating new leaf in the no-space case.
1564 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1565 struct ext4_ext_path
*path
,
1566 struct ext4_extent
*newext
)
1568 struct ext4_extent_header
*eh
;
1569 struct ext4_extent
*ex
, *fex
;
1570 struct ext4_extent
*nearex
; /* nearest extent */
1571 struct ext4_ext_path
*npath
= NULL
;
1572 int depth
, len
, err
;
1574 unsigned uninitialized
= 0;
1576 BUG_ON(ext4_ext_get_actual_len(newext
) == 0);
1577 depth
= ext_depth(inode
);
1578 ex
= path
[depth
].p_ext
;
1579 BUG_ON(path
[depth
].p_hdr
== NULL
);
1581 /* try to insert block into found extent and return */
1582 if (ex
&& ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1583 ext_debug("append %d block to %d:%d (from %llu)\n",
1584 ext4_ext_get_actual_len(newext
),
1585 le32_to_cpu(ex
->ee_block
),
1586 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
1587 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1592 * ext4_can_extents_be_merged should have checked that either
1593 * both extents are uninitialized, or both aren't. Thus we
1594 * need to check only one of them here.
1596 if (ext4_ext_is_uninitialized(ex
))
1598 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1599 + ext4_ext_get_actual_len(newext
));
1601 ext4_ext_mark_uninitialized(ex
);
1602 eh
= path
[depth
].p_hdr
;
1608 depth
= ext_depth(inode
);
1609 eh
= path
[depth
].p_hdr
;
1610 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1613 /* probably next leaf has space for us? */
1614 fex
= EXT_LAST_EXTENT(eh
);
1615 next
= ext4_ext_next_leaf_block(inode
, path
);
1616 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1617 && next
!= EXT_MAX_BLOCK
) {
1618 ext_debug("next leaf block - %d\n", next
);
1619 BUG_ON(npath
!= NULL
);
1620 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1622 return PTR_ERR(npath
);
1623 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1624 eh
= npath
[depth
].p_hdr
;
1625 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1626 ext_debug("next leaf isnt full(%d)\n",
1627 le16_to_cpu(eh
->eh_entries
));
1631 ext_debug("next leaf has no free space(%d,%d)\n",
1632 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1636 * There is no free space in the found leaf.
1637 * We're gonna add a new leaf in the tree.
1639 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1642 depth
= ext_depth(inode
);
1643 eh
= path
[depth
].p_hdr
;
1646 nearex
= path
[depth
].p_ext
;
1648 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1653 /* there is no extent in this leaf, create first one */
1654 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1655 le32_to_cpu(newext
->ee_block
),
1657 ext4_ext_get_actual_len(newext
));
1658 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1659 } else if (le32_to_cpu(newext
->ee_block
)
1660 > le32_to_cpu(nearex
->ee_block
)) {
1661 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1662 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1663 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1664 len
= (len
- 1) * sizeof(struct ext4_extent
);
1665 len
= len
< 0 ? 0 : len
;
1666 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1667 "move %d from 0x%p to 0x%p\n",
1668 le32_to_cpu(newext
->ee_block
),
1670 ext4_ext_get_actual_len(newext
),
1671 nearex
, len
, nearex
+ 1, nearex
+ 2);
1672 memmove(nearex
+ 2, nearex
+ 1, len
);
1674 path
[depth
].p_ext
= nearex
+ 1;
1676 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1677 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1678 len
= len
< 0 ? 0 : len
;
1679 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1680 "move %d from 0x%p to 0x%p\n",
1681 le32_to_cpu(newext
->ee_block
),
1683 ext4_ext_get_actual_len(newext
),
1684 nearex
, len
, nearex
+ 1, nearex
+ 2);
1685 memmove(nearex
+ 1, nearex
, len
);
1686 path
[depth
].p_ext
= nearex
;
1689 le16_add_cpu(&eh
->eh_entries
, 1);
1690 nearex
= path
[depth
].p_ext
;
1691 nearex
->ee_block
= newext
->ee_block
;
1692 ext4_ext_store_pblock(nearex
, ext_pblock(newext
));
1693 nearex
->ee_len
= newext
->ee_len
;
1696 /* try to merge extents to the right */
1697 ext4_ext_try_to_merge(inode
, path
, nearex
);
1699 /* try to merge extents to the left */
1701 /* time to correct all indexes above */
1702 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1706 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1710 ext4_ext_drop_refs(npath
);
1713 ext4_ext_invalidate_cache(inode
);
1717 int ext4_ext_walk_space(struct inode
*inode
, ext4_lblk_t block
,
1718 ext4_lblk_t num
, ext_prepare_callback func
,
1721 struct ext4_ext_path
*path
= NULL
;
1722 struct ext4_ext_cache cbex
;
1723 struct ext4_extent
*ex
;
1724 ext4_lblk_t next
, start
= 0, end
= 0;
1725 ext4_lblk_t last
= block
+ num
;
1726 int depth
, exists
, err
= 0;
1728 BUG_ON(func
== NULL
);
1729 BUG_ON(inode
== NULL
);
1731 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1733 /* find extent for this block */
1734 path
= ext4_ext_find_extent(inode
, block
, path
);
1736 err
= PTR_ERR(path
);
1741 depth
= ext_depth(inode
);
1742 BUG_ON(path
[depth
].p_hdr
== NULL
);
1743 ex
= path
[depth
].p_ext
;
1744 next
= ext4_ext_next_allocated_block(path
);
1748 /* there is no extent yet, so try to allocate
1749 * all requested space */
1752 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1753 /* need to allocate space before found extent */
1755 end
= le32_to_cpu(ex
->ee_block
);
1756 if (block
+ num
< end
)
1758 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1759 + ext4_ext_get_actual_len(ex
)) {
1760 /* need to allocate space after found extent */
1765 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1767 * some part of requested space is covered
1771 end
= le32_to_cpu(ex
->ee_block
)
1772 + ext4_ext_get_actual_len(ex
);
1773 if (block
+ num
< end
)
1779 BUG_ON(end
<= start
);
1782 cbex
.ec_block
= start
;
1783 cbex
.ec_len
= end
- start
;
1785 cbex
.ec_type
= EXT4_EXT_CACHE_GAP
;
1787 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1788 cbex
.ec_len
= ext4_ext_get_actual_len(ex
);
1789 cbex
.ec_start
= ext_pblock(ex
);
1790 cbex
.ec_type
= EXT4_EXT_CACHE_EXTENT
;
1793 BUG_ON(cbex
.ec_len
== 0);
1794 err
= func(inode
, path
, &cbex
, ex
, cbdata
);
1795 ext4_ext_drop_refs(path
);
1800 if (err
== EXT_REPEAT
)
1802 else if (err
== EXT_BREAK
) {
1807 if (ext_depth(inode
) != depth
) {
1808 /* depth was changed. we have to realloc path */
1813 block
= cbex
.ec_block
+ cbex
.ec_len
;
1817 ext4_ext_drop_refs(path
);
1825 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1826 __u32 len
, ext4_fsblk_t start
, int type
)
1828 struct ext4_ext_cache
*cex
;
1830 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1831 cex
= &EXT4_I(inode
)->i_cached_extent
;
1832 cex
->ec_type
= type
;
1833 cex
->ec_block
= block
;
1835 cex
->ec_start
= start
;
1836 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1840 * ext4_ext_put_gap_in_cache:
1841 * calculate boundaries of the gap that the requested block fits into
1842 * and cache this gap
1845 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1848 int depth
= ext_depth(inode
);
1851 struct ext4_extent
*ex
;
1853 ex
= path
[depth
].p_ext
;
1855 /* there is no extent yet, so gap is [0;-] */
1857 len
= EXT_MAX_BLOCK
;
1858 ext_debug("cache gap(whole file):");
1859 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1861 len
= le32_to_cpu(ex
->ee_block
) - block
;
1862 ext_debug("cache gap(before): %u [%u:%u]",
1864 le32_to_cpu(ex
->ee_block
),
1865 ext4_ext_get_actual_len(ex
));
1866 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1867 + ext4_ext_get_actual_len(ex
)) {
1869 lblock
= le32_to_cpu(ex
->ee_block
)
1870 + ext4_ext_get_actual_len(ex
);
1872 next
= ext4_ext_next_allocated_block(path
);
1873 ext_debug("cache gap(after): [%u:%u] %u",
1874 le32_to_cpu(ex
->ee_block
),
1875 ext4_ext_get_actual_len(ex
),
1877 BUG_ON(next
== lblock
);
1878 len
= next
- lblock
;
1884 ext_debug(" -> %u:%lu\n", lblock
, len
);
1885 ext4_ext_put_in_cache(inode
, lblock
, len
, 0, EXT4_EXT_CACHE_GAP
);
1889 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1890 struct ext4_extent
*ex
)
1892 struct ext4_ext_cache
*cex
;
1893 int ret
= EXT4_EXT_CACHE_NO
;
1896 * We borrow i_block_reservation_lock to protect i_cached_extent
1898 spin_lock(&EXT4_I(inode
)->i_block_reservation_lock
);
1899 cex
= &EXT4_I(inode
)->i_cached_extent
;
1901 /* has cache valid data? */
1902 if (cex
->ec_type
== EXT4_EXT_CACHE_NO
)
1905 BUG_ON(cex
->ec_type
!= EXT4_EXT_CACHE_GAP
&&
1906 cex
->ec_type
!= EXT4_EXT_CACHE_EXTENT
);
1907 if (block
>= cex
->ec_block
&& block
< cex
->ec_block
+ cex
->ec_len
) {
1908 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
1909 ext4_ext_store_pblock(ex
, cex
->ec_start
);
1910 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
1911 ext_debug("%u cached by %u:%u:%llu\n",
1913 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
1917 spin_unlock(&EXT4_I(inode
)->i_block_reservation_lock
);
1923 * removes index from the index block.
1924 * It's used in truncate case only, thus all requests are for
1925 * last index in the block only.
1927 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
1928 struct ext4_ext_path
*path
)
1930 struct buffer_head
*bh
;
1934 /* free index block */
1936 leaf
= idx_pblock(path
->p_idx
);
1937 BUG_ON(path
->p_hdr
->eh_entries
== 0);
1938 err
= ext4_ext_get_access(handle
, inode
, path
);
1941 le16_add_cpu(&path
->p_hdr
->eh_entries
, -1);
1942 err
= ext4_ext_dirty(handle
, inode
, path
);
1945 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
1946 bh
= sb_find_get_block(inode
->i_sb
, leaf
);
1947 ext4_forget(handle
, 1, inode
, bh
, leaf
);
1948 ext4_free_blocks(handle
, inode
, leaf
, 1, 1);
1953 * ext4_ext_calc_credits_for_single_extent:
1954 * This routine returns max. credits that needed to insert an extent
1955 * to the extent tree.
1956 * When pass the actual path, the caller should calculate credits
1959 int ext4_ext_calc_credits_for_single_extent(struct inode
*inode
, int nrblocks
,
1960 struct ext4_ext_path
*path
)
1963 int depth
= ext_depth(inode
);
1966 /* probably there is space in leaf? */
1967 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
1968 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
)) {
1971 * There are some space in the leaf tree, no
1972 * need to account for leaf block credit
1974 * bitmaps and block group descriptor blocks
1975 * and other metadat blocks still need to be
1978 /* 1 bitmap, 1 block group descriptor */
1979 ret
= 2 + EXT4_META_TRANS_BLOCKS(inode
->i_sb
);
1983 return ext4_chunk_trans_blocks(inode
, nrblocks
);
1987 * How many index/leaf blocks need to change/allocate to modify nrblocks?
1989 * if nrblocks are fit in a single extent (chunk flag is 1), then
1990 * in the worse case, each tree level index/leaf need to be changed
1991 * if the tree split due to insert a new extent, then the old tree
1992 * index/leaf need to be updated too
1994 * If the nrblocks are discontiguous, they could cause
1995 * the whole tree split more than once, but this is really rare.
1997 int ext4_ext_index_trans_blocks(struct inode
*inode
, int nrblocks
, int chunk
)
2000 int depth
= ext_depth(inode
);
2010 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
2011 struct ext4_extent
*ex
,
2012 ext4_lblk_t from
, ext4_lblk_t to
)
2014 struct buffer_head
*bh
;
2015 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
2016 int i
, metadata
= 0;
2018 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
2020 #ifdef EXTENTS_STATS
2022 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2023 spin_lock(&sbi
->s_ext_stats_lock
);
2024 sbi
->s_ext_blocks
+= ee_len
;
2025 sbi
->s_ext_extents
++;
2026 if (ee_len
< sbi
->s_ext_min
)
2027 sbi
->s_ext_min
= ee_len
;
2028 if (ee_len
> sbi
->s_ext_max
)
2029 sbi
->s_ext_max
= ee_len
;
2030 if (ext_depth(inode
) > sbi
->s_depth_max
)
2031 sbi
->s_depth_max
= ext_depth(inode
);
2032 spin_unlock(&sbi
->s_ext_stats_lock
);
2035 if (from
>= le32_to_cpu(ex
->ee_block
)
2036 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2041 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
2042 start
= ext_pblock(ex
) + ee_len
- num
;
2043 ext_debug("free last %u blocks starting %llu\n", num
, start
);
2044 for (i
= 0; i
< num
; i
++) {
2045 bh
= sb_find_get_block(inode
->i_sb
, start
+ i
);
2046 ext4_forget(handle
, 0, inode
, bh
, start
+ i
);
2048 ext4_free_blocks(handle
, inode
, start
, num
, metadata
);
2049 } else if (from
== le32_to_cpu(ex
->ee_block
)
2050 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
2051 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
2052 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2054 printk(KERN_INFO
"strange request: removal(2) "
2055 "%u-%u from %u:%u\n",
2056 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
2062 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
2063 struct ext4_ext_path
*path
, ext4_lblk_t start
)
2065 int err
= 0, correct_index
= 0;
2066 int depth
= ext_depth(inode
), credits
;
2067 struct ext4_extent_header
*eh
;
2068 ext4_lblk_t a
, b
, block
;
2070 ext4_lblk_t ex_ee_block
;
2071 unsigned short ex_ee_len
;
2072 unsigned uninitialized
= 0;
2073 struct ext4_extent
*ex
;
2075 /* the header must be checked already in ext4_ext_remove_space() */
2076 ext_debug("truncate since %u in leaf\n", start
);
2077 if (!path
[depth
].p_hdr
)
2078 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
2079 eh
= path
[depth
].p_hdr
;
2082 /* find where to start removing */
2083 ex
= EXT_LAST_EXTENT(eh
);
2085 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2086 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2088 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
2089 ex_ee_block
+ ex_ee_len
> start
) {
2091 if (ext4_ext_is_uninitialized(ex
))
2096 ext_debug("remove ext %lu:%u\n", ex_ee_block
, ex_ee_len
);
2097 path
[depth
].p_ext
= ex
;
2099 a
= ex_ee_block
> start
? ex_ee_block
: start
;
2100 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
2101 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
2103 ext_debug(" border %u:%u\n", a
, b
);
2105 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
2109 } else if (a
!= ex_ee_block
) {
2110 /* remove tail of the extent */
2111 block
= ex_ee_block
;
2113 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
2114 /* remove head of the extent */
2117 /* there is no "make a hole" API yet */
2120 /* remove whole extent: excellent! */
2121 block
= ex_ee_block
;
2123 BUG_ON(a
!= ex_ee_block
);
2124 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
2128 * 3 for leaf, sb, and inode plus 2 (bmap and group
2129 * descriptor) for each block group; assume two block
2130 * groups plus ex_ee_len/blocks_per_block_group for
2133 credits
= 7 + 2*(ex_ee_len
/EXT4_BLOCKS_PER_GROUP(inode
->i_sb
));
2134 if (ex
== EXT_FIRST_EXTENT(eh
)) {
2136 credits
+= (ext_depth(inode
)) + 1;
2138 credits
+= 2 * EXT4_QUOTA_TRANS_BLOCKS(inode
->i_sb
);
2140 err
= ext4_ext_journal_restart(handle
, credits
);
2144 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2148 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
2153 /* this extent is removed; mark slot entirely unused */
2154 ext4_ext_store_pblock(ex
, 0);
2155 le16_add_cpu(&eh
->eh_entries
, -1);
2158 ex
->ee_block
= cpu_to_le32(block
);
2159 ex
->ee_len
= cpu_to_le16(num
);
2161 * Do not mark uninitialized if all the blocks in the
2162 * extent have been removed.
2164 if (uninitialized
&& num
)
2165 ext4_ext_mark_uninitialized(ex
);
2167 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2171 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
2174 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
2175 ex_ee_len
= ext4_ext_get_actual_len(ex
);
2178 if (correct_index
&& eh
->eh_entries
)
2179 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2181 /* if this leaf is free, then we should
2182 * remove it from index block above */
2183 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
2184 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
2191 * ext4_ext_more_to_rm:
2192 * returns 1 if current index has to be freed (even partial)
2195 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
2197 BUG_ON(path
->p_idx
== NULL
);
2199 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
2203 * if truncate on deeper level happened, it wasn't partial,
2204 * so we have to consider current index for truncation
2206 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
2211 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
2213 struct super_block
*sb
= inode
->i_sb
;
2214 int depth
= ext_depth(inode
);
2215 struct ext4_ext_path
*path
;
2219 ext_debug("truncate since %u\n", start
);
2221 /* probably first extent we're gonna free will be last in block */
2222 handle
= ext4_journal_start(inode
, depth
+ 1);
2224 return PTR_ERR(handle
);
2226 ext4_ext_invalidate_cache(inode
);
2229 * We start scanning from right side, freeing all the blocks
2230 * after i_size and walking into the tree depth-wise.
2232 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_NOFS
);
2234 ext4_journal_stop(handle
);
2237 path
[0].p_hdr
= ext_inode_hdr(inode
);
2238 if (ext4_ext_check(inode
, path
[0].p_hdr
, depth
)) {
2242 path
[0].p_depth
= depth
;
2244 while (i
>= 0 && err
== 0) {
2246 /* this is leaf block */
2247 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2248 /* root level has p_bh == NULL, brelse() eats this */
2249 brelse(path
[i
].p_bh
);
2250 path
[i
].p_bh
= NULL
;
2255 /* this is index block */
2256 if (!path
[i
].p_hdr
) {
2257 ext_debug("initialize header\n");
2258 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2261 if (!path
[i
].p_idx
) {
2262 /* this level hasn't been touched yet */
2263 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2264 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2265 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2267 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2269 /* we were already here, see at next index */
2273 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2274 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2276 if (ext4_ext_more_to_rm(path
+ i
)) {
2277 struct buffer_head
*bh
;
2278 /* go to the next level */
2279 ext_debug("move to level %d (block %llu)\n",
2280 i
+ 1, idx_pblock(path
[i
].p_idx
));
2281 memset(path
+ i
+ 1, 0, sizeof(*path
));
2282 bh
= sb_bread(sb
, idx_pblock(path
[i
].p_idx
));
2284 /* should we reset i_size? */
2288 if (WARN_ON(i
+ 1 > depth
)) {
2292 if (ext4_ext_check(inode
, ext_block_hdr(bh
),
2297 path
[i
+ 1].p_bh
= bh
;
2299 /* save actual number of indexes since this
2300 * number is changed at the next iteration */
2301 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2304 /* we finished processing this index, go up */
2305 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2306 /* index is empty, remove it;
2307 * handle must be already prepared by the
2308 * truncatei_leaf() */
2309 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2311 /* root level has p_bh == NULL, brelse() eats this */
2312 brelse(path
[i
].p_bh
);
2313 path
[i
].p_bh
= NULL
;
2315 ext_debug("return to level %d\n", i
);
2319 /* TODO: flexible tree reduction should be here */
2320 if (path
->p_hdr
->eh_entries
== 0) {
2322 * truncate to zero freed all the tree,
2323 * so we need to correct eh_depth
2325 err
= ext4_ext_get_access(handle
, inode
, path
);
2327 ext_inode_hdr(inode
)->eh_depth
= 0;
2328 ext_inode_hdr(inode
)->eh_max
=
2329 cpu_to_le16(ext4_ext_space_root(inode
));
2330 err
= ext4_ext_dirty(handle
, inode
, path
);
2334 ext4_ext_drop_refs(path
);
2336 ext4_journal_stop(handle
);
2342 * called at mount time
2344 void ext4_ext_init(struct super_block
*sb
)
2347 * possible initialization would be here
2350 if (EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
)) {
2351 printk(KERN_INFO
"EXT4-fs: file extents enabled");
2352 #ifdef AGGRESSIVE_TEST
2353 printk(", aggressive tests");
2355 #ifdef CHECK_BINSEARCH
2356 printk(", check binsearch");
2358 #ifdef EXTENTS_STATS
2362 #ifdef EXTENTS_STATS
2363 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2364 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2365 EXT4_SB(sb
)->s_ext_max
= 0;
2371 * called at umount time
2373 void ext4_ext_release(struct super_block
*sb
)
2375 if (!EXT4_HAS_INCOMPAT_FEATURE(sb
, EXT4_FEATURE_INCOMPAT_EXTENTS
))
2378 #ifdef EXTENTS_STATS
2379 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2380 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2381 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2382 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2383 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2384 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2385 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2390 static void bi_complete(struct bio
*bio
, int error
)
2392 complete((struct completion
*)bio
->bi_private
);
2395 /* FIXME!! we need to try to merge to left or right after zero-out */
2396 static int ext4_ext_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
2400 int blkbits
, blocksize
;
2402 struct completion event
;
2403 unsigned int ee_len
, len
, done
, offset
;
2406 blkbits
= inode
->i_blkbits
;
2407 blocksize
= inode
->i_sb
->s_blocksize
;
2408 ee_len
= ext4_ext_get_actual_len(ex
);
2409 ee_pblock
= ext_pblock(ex
);
2411 /* convert ee_pblock to 512 byte sectors */
2412 ee_pblock
= ee_pblock
<< (blkbits
- 9);
2414 while (ee_len
> 0) {
2416 if (ee_len
> BIO_MAX_PAGES
)
2417 len
= BIO_MAX_PAGES
;
2421 bio
= bio_alloc(GFP_NOIO
, len
);
2422 bio
->bi_sector
= ee_pblock
;
2423 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
2427 while (done
< len
) {
2428 ret
= bio_add_page(bio
, ZERO_PAGE(0),
2430 if (ret
!= blocksize
) {
2432 * We can't add any more pages because of
2433 * hardware limitations. Start a new bio.
2438 offset
+= blocksize
;
2439 if (offset
>= PAGE_CACHE_SIZE
)
2443 init_completion(&event
);
2444 bio
->bi_private
= &event
;
2445 bio
->bi_end_io
= bi_complete
;
2446 submit_bio(WRITE
, bio
);
2447 wait_for_completion(&event
);
2449 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
2457 ee_pblock
+= done
<< (blkbits
- 9);
2462 #define EXT4_EXT_ZERO_LEN 7
2465 * This function is called by ext4_ext_get_blocks() if someone tries to write
2466 * to an uninitialized extent. It may result in splitting the uninitialized
2467 * extent into multiple extents (upto three - one initialized and two
2469 * There are three possibilities:
2470 * a> There is no split required: Entire extent should be initialized
2471 * b> Splits in two extents: Write is happening at either end of the extent
2472 * c> Splits in three extents: Somone is writing in middle of the extent
2474 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2475 struct inode
*inode
,
2476 struct ext4_ext_path
*path
,
2478 unsigned int max_blocks
)
2480 struct ext4_extent
*ex
, newex
, orig_ex
;
2481 struct ext4_extent
*ex1
= NULL
;
2482 struct ext4_extent
*ex2
= NULL
;
2483 struct ext4_extent
*ex3
= NULL
;
2484 struct ext4_extent_header
*eh
;
2485 ext4_lblk_t ee_block
;
2486 unsigned int allocated
, ee_len
, depth
;
2487 ext4_fsblk_t newblock
;
2491 depth
= ext_depth(inode
);
2492 eh
= path
[depth
].p_hdr
;
2493 ex
= path
[depth
].p_ext
;
2494 ee_block
= le32_to_cpu(ex
->ee_block
);
2495 ee_len
= ext4_ext_get_actual_len(ex
);
2496 allocated
= ee_len
- (iblock
- ee_block
);
2497 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2499 orig_ex
.ee_block
= ex
->ee_block
;
2500 orig_ex
.ee_len
= cpu_to_le16(ee_len
);
2501 ext4_ext_store_pblock(&orig_ex
, ext_pblock(ex
));
2503 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2506 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2507 if (ee_len
<= 2*EXT4_EXT_ZERO_LEN
) {
2508 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2510 goto fix_extent_len
;
2511 /* update the extent length and mark as initialized */
2512 ex
->ee_block
= orig_ex
.ee_block
;
2513 ex
->ee_len
= orig_ex
.ee_len
;
2514 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2515 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2516 /* zeroed the full extent */
2520 /* ex1: ee_block to iblock - 1 : uninitialized */
2521 if (iblock
> ee_block
) {
2523 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2524 ext4_ext_mark_uninitialized(ex1
);
2528 * for sanity, update the length of the ex2 extent before
2529 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2530 * overlap of blocks.
2532 if (!ex1
&& allocated
> max_blocks
)
2533 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2534 /* ex3: to ee_block + ee_len : uninitialised */
2535 if (allocated
> max_blocks
) {
2536 unsigned int newdepth
;
2537 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2538 if (allocated
<= EXT4_EXT_ZERO_LEN
) {
2540 * iblock == ee_block is handled by the zerouout
2542 * Mark first half uninitialized.
2543 * Mark second half initialized and zero out the
2544 * initialized extent
2546 ex
->ee_block
= orig_ex
.ee_block
;
2547 ex
->ee_len
= cpu_to_le16(ee_len
- allocated
);
2548 ext4_ext_mark_uninitialized(ex
);
2549 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2550 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2553 ex3
->ee_block
= cpu_to_le32(iblock
);
2554 ext4_ext_store_pblock(ex3
, newblock
);
2555 ex3
->ee_len
= cpu_to_le16(allocated
);
2556 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
);
2557 if (err
== -ENOSPC
) {
2558 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2560 goto fix_extent_len
;
2561 ex
->ee_block
= orig_ex
.ee_block
;
2562 ex
->ee_len
= orig_ex
.ee_len
;
2563 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2564 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2565 /* blocks available from iblock */
2569 goto fix_extent_len
;
2572 * We need to zero out the second half because
2573 * an fallocate request can update file size and
2574 * converting the second half to initialized extent
2575 * implies that we can leak some junk data to user
2578 err
= ext4_ext_zeroout(inode
, ex3
);
2581 * We should actually mark the
2582 * second half as uninit and return error
2583 * Insert would have changed the extent
2585 depth
= ext_depth(inode
);
2586 ext4_ext_drop_refs(path
);
2587 path
= ext4_ext_find_extent(inode
,
2590 err
= PTR_ERR(path
);
2593 /* get the second half extent details */
2594 ex
= path
[depth
].p_ext
;
2595 err
= ext4_ext_get_access(handle
, inode
,
2599 ext4_ext_mark_uninitialized(ex
);
2600 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2604 /* zeroed the second half */
2608 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2609 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2610 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2611 ext4_ext_mark_uninitialized(ex3
);
2612 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
);
2613 if (err
== -ENOSPC
) {
2614 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2616 goto fix_extent_len
;
2617 /* update the extent length and mark as initialized */
2618 ex
->ee_block
= orig_ex
.ee_block
;
2619 ex
->ee_len
= orig_ex
.ee_len
;
2620 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2621 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2622 /* zeroed the full extent */
2623 /* blocks available from iblock */
2627 goto fix_extent_len
;
2629 * The depth, and hence eh & ex might change
2630 * as part of the insert above.
2632 newdepth
= ext_depth(inode
);
2634 * update the extent length after successful insert of the
2637 orig_ex
.ee_len
= cpu_to_le16(ee_len
-
2638 ext4_ext_get_actual_len(ex3
));
2640 ext4_ext_drop_refs(path
);
2641 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2643 err
= PTR_ERR(path
);
2646 eh
= path
[depth
].p_hdr
;
2647 ex
= path
[depth
].p_ext
;
2651 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2655 allocated
= max_blocks
;
2657 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2658 * to insert a extent in the middle zerout directly
2659 * otherwise give the extent a chance to merge to left
2661 if (le16_to_cpu(orig_ex
.ee_len
) <= EXT4_EXT_ZERO_LEN
&&
2662 iblock
!= ee_block
) {
2663 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2665 goto fix_extent_len
;
2666 /* update the extent length and mark as initialized */
2667 ex
->ee_block
= orig_ex
.ee_block
;
2668 ex
->ee_len
= orig_ex
.ee_len
;
2669 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2670 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2671 /* zero out the first half */
2672 /* blocks available from iblock */
2677 * If there was a change of depth as part of the
2678 * insertion of ex3 above, we need to update the length
2679 * of the ex1 extent again here
2681 if (ex1
&& ex1
!= ex
) {
2683 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2684 ext4_ext_mark_uninitialized(ex1
);
2687 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2688 ex2
->ee_block
= cpu_to_le32(iblock
);
2689 ext4_ext_store_pblock(ex2
, newblock
);
2690 ex2
->ee_len
= cpu_to_le16(allocated
);
2694 * New (initialized) extent starts from the first block
2695 * in the current extent. i.e., ex2 == ex
2696 * We have to see if it can be merged with the extent
2699 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2701 * To merge left, pass "ex2 - 1" to try_to_merge(),
2702 * since it merges towards right _only_.
2704 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2706 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2709 depth
= ext_depth(inode
);
2714 * Try to Merge towards right. This might be required
2715 * only when the whole extent is being written to.
2716 * i.e. ex2 == ex and ex3 == NULL.
2719 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2721 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2726 /* Mark modified extent as dirty */
2727 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2730 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2731 if (err
== -ENOSPC
) {
2732 err
= ext4_ext_zeroout(inode
, &orig_ex
);
2734 goto fix_extent_len
;
2735 /* update the extent length and mark as initialized */
2736 ex
->ee_block
= orig_ex
.ee_block
;
2737 ex
->ee_len
= orig_ex
.ee_len
;
2738 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2739 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2740 /* zero out the first half */
2743 goto fix_extent_len
;
2745 return err
? err
: allocated
;
2748 ex
->ee_block
= orig_ex
.ee_block
;
2749 ex
->ee_len
= orig_ex
.ee_len
;
2750 ext4_ext_store_pblock(ex
, ext_pblock(&orig_ex
));
2751 ext4_ext_mark_uninitialized(ex
);
2752 ext4_ext_dirty(handle
, inode
, path
+ depth
);
2757 * Block allocation/map/preallocation routine for extents based files
2760 * Need to be called with
2761 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2762 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2764 * return > 0, number of of blocks already mapped/allocated
2765 * if create == 0 and these are pre-allocated blocks
2766 * buffer head is unmapped
2767 * otherwise blocks are mapped
2769 * return = 0, if plain look up failed (blocks have not been allocated)
2770 * buffer head is unmapped
2772 * return < 0, error case.
2774 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
2776 unsigned int max_blocks
, struct buffer_head
*bh_result
,
2779 struct ext4_ext_path
*path
= NULL
;
2780 struct ext4_extent_header
*eh
;
2781 struct ext4_extent newex
, *ex
;
2782 ext4_fsblk_t newblock
;
2783 int err
= 0, depth
, ret
, cache_type
;
2784 unsigned int allocated
= 0;
2785 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
) {
2795 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
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 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
)
2863 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
2864 if (allocated
> max_blocks
)
2865 allocated
= max_blocks
;
2867 * We have blocks reserved already. We
2868 * return allocated blocks so that delalloc
2869 * won't do block reservation for us. But
2870 * the buffer head will be unmapped so that
2871 * a read from the block returns 0s.
2873 set_buffer_unwritten(bh_result
);
2874 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
2875 bh_result
->b_blocknr
= newblock
;
2879 ret
= ext4_ext_convert_to_initialized(handle
, inode
,
2892 * requested block isn't allocated yet;
2893 * we couldn't try to create block if create flag is zero
2895 if ((flags
& EXT4_GET_BLOCKS_CREATE
) == 0) {
2897 * put just found gap into cache to speed up
2898 * subsequent requests
2900 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
2904 * Okay, we need to do block allocation.
2907 /* find neighbour allocated blocks */
2909 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
2913 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
2918 * See if request is beyond maximum number of blocks we can have in
2919 * a single extent. For an initialized extent this limit is
2920 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2921 * EXT_UNINIT_MAX_LEN.
2923 if (max_blocks
> EXT_INIT_MAX_LEN
&&
2924 !(flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
2925 max_blocks
= EXT_INIT_MAX_LEN
;
2926 else if (max_blocks
> EXT_UNINIT_MAX_LEN
&&
2927 (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
))
2928 max_blocks
= EXT_UNINIT_MAX_LEN
;
2930 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2931 newex
.ee_block
= cpu_to_le32(iblock
);
2932 newex
.ee_len
= cpu_to_le16(max_blocks
);
2933 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
2935 allocated
= ext4_ext_get_actual_len(&newex
);
2937 allocated
= max_blocks
;
2939 /* allocate new block */
2941 ar
.goal
= ext4_ext_find_goal(inode
, path
, iblock
);
2942 ar
.logical
= iblock
;
2944 if (S_ISREG(inode
->i_mode
))
2945 ar
.flags
= EXT4_MB_HINT_DATA
;
2947 /* disable in-core preallocation for non-regular files */
2949 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
2952 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2953 ar
.goal
, newblock
, allocated
);
2955 /* try to insert new extent into found leaf and return */
2956 ext4_ext_store_pblock(&newex
, newblock
);
2957 newex
.ee_len
= cpu_to_le16(ar
.len
);
2958 if (flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) /* Mark uninitialized */
2959 ext4_ext_mark_uninitialized(&newex
);
2960 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2962 /* free data blocks we just allocated */
2963 /* not a good idea to call discard here directly,
2964 * but otherwise we'd need to call it every free() */
2965 ext4_discard_preallocations(inode
);
2966 ext4_free_blocks(handle
, inode
, ext_pblock(&newex
),
2967 ext4_ext_get_actual_len(&newex
), 0);
2971 /* previous routine could use block we allocated */
2972 newblock
= ext_pblock(&newex
);
2973 allocated
= ext4_ext_get_actual_len(&newex
);
2975 set_buffer_new(bh_result
);
2977 /* Cache only when it is _not_ an uninitialized extent */
2978 if ((flags
& EXT4_GET_BLOCKS_UNINIT_EXT
) == 0)
2979 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
2980 EXT4_EXT_CACHE_EXTENT
);
2982 if (allocated
> max_blocks
)
2983 allocated
= max_blocks
;
2984 ext4_ext_show_leaf(inode
, path
);
2985 set_buffer_mapped(bh_result
);
2986 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
2987 bh_result
->b_blocknr
= newblock
;
2990 ext4_ext_drop_refs(path
);
2993 return err
? err
: allocated
;
2996 void ext4_ext_truncate(struct inode
*inode
)
2998 struct address_space
*mapping
= inode
->i_mapping
;
2999 struct super_block
*sb
= inode
->i_sb
;
3000 ext4_lblk_t last_block
;
3005 * probably first extent we're gonna free will be last in block
3007 err
= ext4_writepage_trans_blocks(inode
);
3008 handle
= ext4_journal_start(inode
, err
);
3012 if (inode
->i_size
& (sb
->s_blocksize
- 1))
3013 ext4_block_truncate_page(handle
, mapping
, inode
->i_size
);
3015 if (ext4_orphan_add(handle
, inode
))
3018 down_write(&EXT4_I(inode
)->i_data_sem
);
3019 ext4_ext_invalidate_cache(inode
);
3021 ext4_discard_preallocations(inode
);
3024 * TODO: optimization is possible here.
3025 * Probably we need not scan at all,
3026 * because page truncation is enough.
3029 /* we have to know where to truncate from in crash case */
3030 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
3031 ext4_mark_inode_dirty(handle
, inode
);
3033 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
3034 >> EXT4_BLOCK_SIZE_BITS(sb
);
3035 err
= ext4_ext_remove_space(inode
, last_block
);
3037 /* In a multi-transaction truncate, we only make the final
3038 * transaction synchronous.
3041 ext4_handle_sync(handle
);
3044 up_write(&EXT4_I(inode
)->i_data_sem
);
3046 * If this was a simple ftruncate() and the file will remain alive,
3047 * then we need to clear up the orphan record which we created above.
3048 * However, if this was a real unlink then we were called by
3049 * ext4_delete_inode(), and we allow that function to clean up the
3050 * orphan info for us.
3053 ext4_orphan_del(handle
, inode
);
3055 inode
->i_mtime
= inode
->i_ctime
= ext4_current_time(inode
);
3056 ext4_mark_inode_dirty(handle
, inode
);
3057 ext4_journal_stop(handle
);
3060 static void ext4_falloc_update_inode(struct inode
*inode
,
3061 int mode
, loff_t new_size
, int update_ctime
)
3063 struct timespec now
;
3066 now
= current_fs_time(inode
->i_sb
);
3067 if (!timespec_equal(&inode
->i_ctime
, &now
))
3068 inode
->i_ctime
= now
;
3071 * Update only when preallocation was requested beyond
3074 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3075 if (new_size
> i_size_read(inode
))
3076 i_size_write(inode
, new_size
);
3077 if (new_size
> EXT4_I(inode
)->i_disksize
)
3078 ext4_update_i_disksize(inode
, new_size
);
3084 * preallocate space for a file. This implements ext4's fallocate inode
3085 * operation, which gets called from sys_fallocate system call.
3086 * For block-mapped files, posix_fallocate should fall back to the method
3087 * of writing zeroes to the required new blocks (the same behavior which is
3088 * expected for file systems which do not support fallocate() system call).
3090 long ext4_fallocate(struct inode
*inode
, int mode
, loff_t offset
, loff_t len
)
3095 unsigned int max_blocks
;
3099 struct buffer_head map_bh
;
3100 unsigned int credits
, blkbits
= inode
->i_blkbits
;
3103 * currently supporting (pre)allocate mode for extent-based
3106 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3109 /* preallocation to directories is currently not supported */
3110 if (S_ISDIR(inode
->i_mode
))
3113 block
= offset
>> blkbits
;
3115 * We can't just convert len to max_blocks because
3116 * If blocksize = 4096 offset = 3072 and len = 2048
3118 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
3121 * credits to insert 1 extent into extent tree
3123 credits
= ext4_chunk_trans_blocks(inode
, max_blocks
);
3124 mutex_lock(&inode
->i_mutex
);
3126 while (ret
>= 0 && ret
< max_blocks
) {
3127 block
= block
+ ret
;
3128 max_blocks
= max_blocks
- ret
;
3129 handle
= ext4_journal_start(inode
, credits
);
3130 if (IS_ERR(handle
)) {
3131 ret
= PTR_ERR(handle
);
3135 ret
= ext4_get_blocks(handle
, inode
, block
,
3136 max_blocks
, &map_bh
,
3137 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT
);
3141 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
3142 "returned error inode#%lu, block=%u, "
3143 "max_blocks=%u", __func__
,
3144 inode
->i_ino
, block
, max_blocks
);
3146 ext4_mark_inode_dirty(handle
, inode
);
3147 ret2
= ext4_journal_stop(handle
);
3150 if ((block
+ ret
) >= (EXT4_BLOCK_ALIGN(offset
+ len
,
3151 blkbits
) >> blkbits
))
3152 new_size
= offset
+ len
;
3154 new_size
= (block
+ ret
) << blkbits
;
3156 ext4_falloc_update_inode(inode
, mode
, new_size
,
3157 buffer_new(&map_bh
));
3158 ext4_mark_inode_dirty(handle
, inode
);
3159 ret2
= ext4_journal_stop(handle
);
3163 if (ret
== -ENOSPC
&&
3164 ext4_should_retry_alloc(inode
->i_sb
, &retries
)) {
3168 mutex_unlock(&inode
->i_mutex
);
3169 return ret
> 0 ? ret2
: ret
;
3173 * Callback function called for each extent to gather FIEMAP information.
3175 static int ext4_ext_fiemap_cb(struct inode
*inode
, struct ext4_ext_path
*path
,
3176 struct ext4_ext_cache
*newex
, struct ext4_extent
*ex
,
3179 struct fiemap_extent_info
*fieinfo
= data
;
3180 unsigned char blksize_bits
= inode
->i_sb
->s_blocksize_bits
;
3187 logical
= (__u64
)newex
->ec_block
<< blksize_bits
;
3189 if (newex
->ec_type
== EXT4_EXT_CACHE_GAP
) {
3192 struct buffer_head
*bh
= NULL
;
3194 offset
= logical
>> PAGE_SHIFT
;
3195 page
= find_get_page(inode
->i_mapping
, offset
);
3196 if (!page
|| !page_has_buffers(page
))
3197 return EXT_CONTINUE
;
3199 bh
= page_buffers(page
);
3202 return EXT_CONTINUE
;
3204 if (buffer_delay(bh
)) {
3205 flags
|= FIEMAP_EXTENT_DELALLOC
;
3206 page_cache_release(page
);
3208 page_cache_release(page
);
3209 return EXT_CONTINUE
;
3213 physical
= (__u64
)newex
->ec_start
<< blksize_bits
;
3214 length
= (__u64
)newex
->ec_len
<< blksize_bits
;
3216 if (ex
&& ext4_ext_is_uninitialized(ex
))
3217 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
3220 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3222 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3223 * this also indicates no more allocated blocks.
3225 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3227 if (ext4_ext_next_allocated_block(path
) == EXT_MAX_BLOCK
||
3228 newex
->ec_block
+ newex
->ec_len
- 1 == EXT_MAX_BLOCK
) {
3229 loff_t size
= i_size_read(inode
);
3230 loff_t bs
= EXT4_BLOCK_SIZE(inode
->i_sb
);
3232 flags
|= FIEMAP_EXTENT_LAST
;
3233 if ((flags
& FIEMAP_EXTENT_DELALLOC
) &&
3234 logical
+length
> size
)
3235 length
= (size
- logical
+ bs
- 1) & ~(bs
-1);
3238 error
= fiemap_fill_next_extent(fieinfo
, logical
, physical
,
3245 return EXT_CONTINUE
;
3248 /* fiemap flags we can handle specified here */
3249 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3251 static int ext4_xattr_fiemap(struct inode
*inode
,
3252 struct fiemap_extent_info
*fieinfo
)
3256 __u32 flags
= FIEMAP_EXTENT_LAST
;
3257 int blockbits
= inode
->i_sb
->s_blocksize_bits
;
3261 if (EXT4_I(inode
)->i_state
& EXT4_STATE_XATTR
) {
3262 struct ext4_iloc iloc
;
3263 int offset
; /* offset of xattr in inode */
3265 error
= ext4_get_inode_loc(inode
, &iloc
);
3268 physical
= iloc
.bh
->b_blocknr
<< blockbits
;
3269 offset
= EXT4_GOOD_OLD_INODE_SIZE
+
3270 EXT4_I(inode
)->i_extra_isize
;
3272 length
= EXT4_SB(inode
->i_sb
)->s_inode_size
- offset
;
3273 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
3274 } else { /* external block */
3275 physical
= EXT4_I(inode
)->i_file_acl
<< blockbits
;
3276 length
= inode
->i_sb
->s_blocksize
;
3280 error
= fiemap_fill_next_extent(fieinfo
, 0, physical
,
3282 return (error
< 0 ? error
: 0);
3285 int ext4_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
3286 __u64 start
, __u64 len
)
3288 ext4_lblk_t start_blk
;
3289 ext4_lblk_t len_blks
;
3292 /* fallback to generic here if not in extents fmt */
3293 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
3294 return generic_block_fiemap(inode
, fieinfo
, start
, len
,
3297 if (fiemap_check_flags(fieinfo
, EXT4_FIEMAP_FLAGS
))
3300 if (fieinfo
->fi_flags
& FIEMAP_FLAG_XATTR
) {
3301 error
= ext4_xattr_fiemap(inode
, fieinfo
);
3303 start_blk
= start
>> inode
->i_sb
->s_blocksize_bits
;
3304 len_blks
= len
>> inode
->i_sb
->s_blocksize_bits
;
3307 * Walk the extent tree gathering extent information.
3308 * ext4_ext_fiemap_cb will push extents back to user.
3310 down_read(&EXT4_I(inode
)->i_data_sem
);
3311 error
= ext4_ext_walk_space(inode
, start_blk
, len_blks
,
3312 ext4_ext_fiemap_cb
, fieinfo
);
3313 up_read(&EXT4_I(inode
)->i_data_sem
);