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/ext4_jbd2.h>
36 #include <linux/jbd.h>
37 #include <linux/highuid.h>
38 #include <linux/pagemap.h>
39 #include <linux/quotaops.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <linux/ext4_fs_extents.h>
43 #include <asm/uaccess.h>
48 * combine low and high parts of physical block number into ext4_fsblk_t
50 static ext4_fsblk_t
ext_pblock(struct ext4_extent
*ex
)
54 block
= le32_to_cpu(ex
->ee_start
);
55 block
|= ((ext4_fsblk_t
) le16_to_cpu(ex
->ee_start_hi
) << 31) << 1;
61 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
63 static ext4_fsblk_t
idx_pblock(struct ext4_extent_idx
*ix
)
67 block
= le32_to_cpu(ix
->ei_leaf
);
68 block
|= ((ext4_fsblk_t
) le16_to_cpu(ix
->ei_leaf_hi
) << 31) << 1;
73 * ext4_ext_store_pblock:
74 * stores a large physical block number into an extent struct,
75 * breaking it into parts
77 static void ext4_ext_store_pblock(struct ext4_extent
*ex
, ext4_fsblk_t pb
)
79 ex
->ee_start
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
80 ex
->ee_start_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
84 * ext4_idx_store_pblock:
85 * stores a large physical block number into an index struct,
86 * breaking it into parts
88 static void ext4_idx_store_pblock(struct ext4_extent_idx
*ix
, ext4_fsblk_t pb
)
90 ix
->ei_leaf
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
91 ix
->ei_leaf_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
94 static int ext4_ext_check_header(const char *function
, struct inode
*inode
,
95 struct ext4_extent_header
*eh
)
97 const char *error_msg
= NULL
;
99 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
100 error_msg
= "invalid magic";
103 if (unlikely(eh
->eh_max
== 0)) {
104 error_msg
= "invalid eh_max";
107 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
108 error_msg
= "invalid eh_entries";
114 ext4_error(inode
->i_sb
, function
,
115 "bad header in inode #%lu: %s - magic %x, "
116 "entries %u, max %u, depth %u",
117 inode
->i_ino
, error_msg
, le16_to_cpu(eh
->eh_magic
),
118 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
119 le16_to_cpu(eh
->eh_depth
));
124 static handle_t
*ext4_ext_journal_restart(handle_t
*handle
, int needed
)
128 if (handle
->h_buffer_credits
> needed
)
130 if (!ext4_journal_extend(handle
, needed
))
132 err
= ext4_journal_restart(handle
, needed
);
142 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
143 struct ext4_ext_path
*path
)
146 /* path points to block */
147 return ext4_journal_get_write_access(handle
, path
->p_bh
);
149 /* path points to leaf/index in inode body */
150 /* we use in-core data, no need to protect them */
160 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
161 struct ext4_ext_path
*path
)
165 /* path points to block */
166 err
= ext4_journal_dirty_metadata(handle
, path
->p_bh
);
168 /* path points to leaf/index in inode body */
169 err
= ext4_mark_inode_dirty(handle
, inode
);
174 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
175 struct ext4_ext_path
*path
,
178 struct ext4_inode_info
*ei
= EXT4_I(inode
);
179 ext4_fsblk_t bg_start
;
180 ext4_grpblk_t colour
;
184 struct ext4_extent
*ex
;
185 depth
= path
->p_depth
;
187 /* try to predict block placement */
188 ex
= path
[depth
].p_ext
;
190 return ext_pblock(ex
)+(block
-le32_to_cpu(ex
->ee_block
));
192 /* it looks like index is empty;
193 * try to find starting block from index itself */
194 if (path
[depth
].p_bh
)
195 return path
[depth
].p_bh
->b_blocknr
;
198 /* OK. use inode's group */
199 bg_start
= (ei
->i_block_group
* EXT4_BLOCKS_PER_GROUP(inode
->i_sb
)) +
200 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_first_data_block
);
201 colour
= (current
->pid
% 16) *
202 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
203 return bg_start
+ colour
+ block
;
207 ext4_ext_new_block(handle_t
*handle
, struct inode
*inode
,
208 struct ext4_ext_path
*path
,
209 struct ext4_extent
*ex
, int *err
)
211 ext4_fsblk_t goal
, newblock
;
213 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
214 newblock
= ext4_new_block(handle
, inode
, goal
, err
);
218 static int ext4_ext_space_block(struct inode
*inode
)
222 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
223 / sizeof(struct ext4_extent
);
224 #ifdef AGGRESSIVE_TEST
231 static int ext4_ext_space_block_idx(struct inode
*inode
)
235 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
236 / sizeof(struct ext4_extent_idx
);
237 #ifdef AGGRESSIVE_TEST
244 static int ext4_ext_space_root(struct inode
*inode
)
248 size
= sizeof(EXT4_I(inode
)->i_data
);
249 size
-= sizeof(struct ext4_extent_header
);
250 size
/= sizeof(struct ext4_extent
);
251 #ifdef AGGRESSIVE_TEST
258 static int ext4_ext_space_root_idx(struct inode
*inode
)
262 size
= sizeof(EXT4_I(inode
)->i_data
);
263 size
-= sizeof(struct ext4_extent_header
);
264 size
/= sizeof(struct ext4_extent_idx
);
265 #ifdef AGGRESSIVE_TEST
273 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
275 int k
, l
= path
->p_depth
;
278 for (k
= 0; k
<= l
; k
++, path
++) {
280 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
281 idx_pblock(path
->p_idx
));
282 } else if (path
->p_ext
) {
283 ext_debug(" %d:%d:%llu ",
284 le32_to_cpu(path
->p_ext
->ee_block
),
285 le16_to_cpu(path
->p_ext
->ee_len
),
286 ext_pblock(path
->p_ext
));
293 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
295 int depth
= ext_depth(inode
);
296 struct ext4_extent_header
*eh
;
297 struct ext4_extent
*ex
;
303 eh
= path
[depth
].p_hdr
;
304 ex
= EXT_FIRST_EXTENT(eh
);
306 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
307 ext_debug("%d:%d:%llu ", le32_to_cpu(ex
->ee_block
),
308 le16_to_cpu(ex
->ee_len
), ext_pblock(ex
));
313 #define ext4_ext_show_path(inode,path)
314 #define ext4_ext_show_leaf(inode,path)
317 static void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
319 int depth
= path
->p_depth
;
322 for (i
= 0; i
<= depth
; i
++, path
++)
330 * ext4_ext_binsearch_idx:
331 * binary search for the closest index of the given block
334 ext4_ext_binsearch_idx(struct inode
*inode
, struct ext4_ext_path
*path
, int block
)
336 struct ext4_extent_header
*eh
= path
->p_hdr
;
337 struct ext4_extent_idx
*r
, *l
, *m
;
339 BUG_ON(eh
->eh_magic
!= EXT4_EXT_MAGIC
);
340 BUG_ON(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
));
341 BUG_ON(le16_to_cpu(eh
->eh_entries
) <= 0);
343 ext_debug("binsearch for %d(idx): ", block
);
345 l
= EXT_FIRST_INDEX(eh
) + 1;
346 r
= EXT_FIRST_INDEX(eh
) + le16_to_cpu(eh
->eh_entries
) - 1;
349 if (block
< le32_to_cpu(m
->ei_block
))
353 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, l
->ei_block
,
354 m
, m
->ei_block
, r
, r
->ei_block
);
358 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
359 idx_block(path
->p_idx
));
361 #ifdef CHECK_BINSEARCH
363 struct ext4_extent_idx
*chix
, *ix
;
366 chix
= ix
= EXT_FIRST_INDEX(eh
);
367 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
369 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
370 printk("k=%d, ix=0x%p, first=0x%p\n", k
,
371 ix
, EXT_FIRST_INDEX(eh
));
373 le32_to_cpu(ix
->ei_block
),
374 le32_to_cpu(ix
[-1].ei_block
));
376 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
377 <= le32_to_cpu(ix
[-1].ei_block
));
378 if (block
< le32_to_cpu(ix
->ei_block
))
382 BUG_ON(chix
!= path
->p_idx
);
389 * ext4_ext_binsearch:
390 * binary search for closest extent of the given block
393 ext4_ext_binsearch(struct inode
*inode
, struct ext4_ext_path
*path
, int block
)
395 struct ext4_extent_header
*eh
= path
->p_hdr
;
396 struct ext4_extent
*r
, *l
, *m
;
398 BUG_ON(eh
->eh_magic
!= EXT4_EXT_MAGIC
);
399 BUG_ON(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
));
401 if (eh
->eh_entries
== 0) {
403 * this leaf is empty:
404 * we get such a leaf in split/add case
409 ext_debug("binsearch for %d: ", block
);
411 l
= EXT_FIRST_EXTENT(eh
) + 1;
412 r
= EXT_FIRST_EXTENT(eh
) + le16_to_cpu(eh
->eh_entries
) - 1;
416 if (block
< le32_to_cpu(m
->ee_block
))
420 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, l
->ee_block
,
421 m
, m
->ee_block
, r
, r
->ee_block
);
425 ext_debug(" -> %d:%llu:%d ",
426 le32_to_cpu(path
->p_ext
->ee_block
),
427 ext_pblock(path
->p_ext
),
428 le16_to_cpu(path
->p_ext
->ee_len
));
430 #ifdef CHECK_BINSEARCH
432 struct ext4_extent
*chex
, *ex
;
435 chex
= ex
= EXT_FIRST_EXTENT(eh
);
436 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
437 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
438 <= le32_to_cpu(ex
[-1].ee_block
));
439 if (block
< le32_to_cpu(ex
->ee_block
))
443 BUG_ON(chex
!= path
->p_ext
);
449 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
451 struct ext4_extent_header
*eh
;
453 eh
= ext_inode_hdr(inode
);
456 eh
->eh_magic
= EXT4_EXT_MAGIC
;
457 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
));
458 ext4_mark_inode_dirty(handle
, inode
);
459 ext4_ext_invalidate_cache(inode
);
463 struct ext4_ext_path
*
464 ext4_ext_find_extent(struct inode
*inode
, int block
, struct ext4_ext_path
*path
)
466 struct ext4_extent_header
*eh
;
467 struct buffer_head
*bh
;
468 short int depth
, i
, ppos
= 0, alloc
= 0;
470 eh
= ext_inode_hdr(inode
);
472 if (ext4_ext_check_header(__FUNCTION__
, inode
, eh
))
473 return ERR_PTR(-EIO
);
475 i
= depth
= ext_depth(inode
);
477 /* account possible depth increase */
479 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
482 return ERR_PTR(-ENOMEM
);
487 /* walk through the tree */
489 ext_debug("depth %d: num %d, max %d\n",
490 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
491 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
492 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
493 path
[ppos
].p_depth
= i
;
494 path
[ppos
].p_ext
= NULL
;
496 bh
= sb_bread(inode
->i_sb
, path
[ppos
].p_block
);
500 eh
= ext_block_hdr(bh
);
502 BUG_ON(ppos
> depth
);
503 path
[ppos
].p_bh
= bh
;
504 path
[ppos
].p_hdr
= eh
;
507 if (ext4_ext_check_header(__FUNCTION__
, inode
, eh
))
511 path
[ppos
].p_depth
= i
;
512 path
[ppos
].p_hdr
= eh
;
513 path
[ppos
].p_ext
= NULL
;
514 path
[ppos
].p_idx
= NULL
;
516 if (ext4_ext_check_header(__FUNCTION__
, inode
, eh
))
520 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
522 ext4_ext_show_path(inode
, path
);
527 ext4_ext_drop_refs(path
);
530 return ERR_PTR(-EIO
);
534 * ext4_ext_insert_index:
535 * insert new index [@logical;@ptr] into the block at @curp;
536 * check where to insert: before @curp or after @curp
538 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
539 struct ext4_ext_path
*curp
,
540 int logical
, ext4_fsblk_t ptr
)
542 struct ext4_extent_idx
*ix
;
545 err
= ext4_ext_get_access(handle
, inode
, curp
);
549 BUG_ON(logical
== le32_to_cpu(curp
->p_idx
->ei_block
));
550 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
551 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
553 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
554 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
555 len
= len
< 0 ? 0 : len
;
556 ext_debug("insert new index %d after: %d. "
557 "move %d from 0x%p to 0x%p\n",
559 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
560 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
562 ix
= curp
->p_idx
+ 1;
565 len
= len
* sizeof(struct ext4_extent_idx
);
566 len
= len
< 0 ? 0 : len
;
567 ext_debug("insert new index %d before: %d. "
568 "move %d from 0x%p to 0x%p\n",
570 curp
->p_idx
, (curp
->p_idx
+ 1));
571 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
575 ix
->ei_block
= cpu_to_le32(logical
);
576 ext4_idx_store_pblock(ix
, ptr
);
577 curp
->p_hdr
->eh_entries
= cpu_to_le16(le16_to_cpu(curp
->p_hdr
->eh_entries
)+1);
579 BUG_ON(le16_to_cpu(curp
->p_hdr
->eh_entries
)
580 > le16_to_cpu(curp
->p_hdr
->eh_max
));
581 BUG_ON(ix
> EXT_LAST_INDEX(curp
->p_hdr
));
583 err
= ext4_ext_dirty(handle
, inode
, curp
);
584 ext4_std_error(inode
->i_sb
, err
);
591 * inserts new subtree into the path, using free index entry
593 * - allocates all needed blocks (new leaf and all intermediate index blocks)
594 * - makes decision where to split
595 * - moves remaining extents and index entries (right to the split point)
596 * into the newly allocated blocks
597 * - initializes subtree
599 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
600 struct ext4_ext_path
*path
,
601 struct ext4_extent
*newext
, int at
)
603 struct buffer_head
*bh
= NULL
;
604 int depth
= ext_depth(inode
);
605 struct ext4_extent_header
*neh
;
606 struct ext4_extent_idx
*fidx
;
607 struct ext4_extent
*ex
;
609 ext4_fsblk_t newblock
, oldblock
;
611 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
614 /* make decision: where to split? */
615 /* FIXME: now decision is simplest: at current extent */
617 /* if current leaf will be split, then we should use
618 * border from split point */
619 BUG_ON(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
));
620 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
621 border
= path
[depth
].p_ext
[1].ee_block
;
622 ext_debug("leaf will be split."
623 " next leaf starts at %d\n",
624 le32_to_cpu(border
));
626 border
= newext
->ee_block
;
627 ext_debug("leaf will be added."
628 " next leaf starts at %d\n",
629 le32_to_cpu(border
));
633 * If error occurs, then we break processing
634 * and mark filesystem read-only. index won't
635 * be inserted and tree will be in consistent
636 * state. Next mount will repair buffers too.
640 * Get array to track all allocated blocks.
641 * We need this to handle errors and free blocks
644 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
648 /* allocate all needed blocks */
649 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
650 for (a
= 0; a
< depth
- at
; a
++) {
651 newblock
= ext4_ext_new_block(handle
, inode
, path
, newext
, &err
);
654 ablocks
[a
] = newblock
;
657 /* initialize new leaf */
658 newblock
= ablocks
[--a
];
659 BUG_ON(newblock
== 0);
660 bh
= sb_getblk(inode
->i_sb
, newblock
);
667 err
= ext4_journal_get_create_access(handle
, bh
);
671 neh
= ext_block_hdr(bh
);
673 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
674 neh
->eh_magic
= EXT4_EXT_MAGIC
;
676 ex
= EXT_FIRST_EXTENT(neh
);
678 /* move remainder of path[depth] to the new leaf */
679 BUG_ON(path
[depth
].p_hdr
->eh_entries
!= path
[depth
].p_hdr
->eh_max
);
680 /* start copy from next extent */
681 /* TODO: we could do it by single memmove */
684 while (path
[depth
].p_ext
<=
685 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
686 ext_debug("move %d:%llu:%d in new leaf %llu\n",
687 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
688 ext_pblock(path
[depth
].p_ext
),
689 le16_to_cpu(path
[depth
].p_ext
->ee_len
),
691 /*memmove(ex++, path[depth].p_ext++,
692 sizeof(struct ext4_extent));
698 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
699 neh
->eh_entries
= cpu_to_le16(le16_to_cpu(neh
->eh_entries
)+m
);
702 set_buffer_uptodate(bh
);
705 err
= ext4_journal_dirty_metadata(handle
, bh
);
711 /* correct old leaf */
713 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
716 path
[depth
].p_hdr
->eh_entries
=
717 cpu_to_le16(le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)-m
);
718 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
724 /* create intermediate indexes */
728 ext_debug("create %d intermediate indices\n", k
);
729 /* insert new index into current index block */
730 /* current depth stored in i var */
734 newblock
= ablocks
[--a
];
735 bh
= sb_getblk(inode
->i_sb
, (ext4_fsblk_t
)newblock
);
742 err
= ext4_journal_get_create_access(handle
, bh
);
746 neh
= ext_block_hdr(bh
);
747 neh
->eh_entries
= cpu_to_le16(1);
748 neh
->eh_magic
= EXT4_EXT_MAGIC
;
749 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
750 neh
->eh_depth
= cpu_to_le16(depth
- i
);
751 fidx
= EXT_FIRST_INDEX(neh
);
752 fidx
->ei_block
= border
;
753 ext4_idx_store_pblock(fidx
, oldblock
);
755 ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i
,
756 newblock
, (unsigned long) le32_to_cpu(border
),
762 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
763 EXT_MAX_INDEX(path
[i
].p_hdr
));
764 BUG_ON(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
765 EXT_LAST_INDEX(path
[i
].p_hdr
));
766 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
767 ext_debug("%d: move %d:%d in new index %llu\n", i
,
768 le32_to_cpu(path
[i
].p_idx
->ei_block
),
769 idx_pblock(path
[i
].p_idx
),
771 /*memmove(++fidx, path[i].p_idx++,
772 sizeof(struct ext4_extent_idx));
774 BUG_ON(neh->eh_entries > neh->eh_max);*/
779 memmove(++fidx
, path
[i
].p_idx
- m
,
780 sizeof(struct ext4_extent_idx
) * m
);
782 cpu_to_le16(le16_to_cpu(neh
->eh_entries
) + m
);
784 set_buffer_uptodate(bh
);
787 err
= ext4_journal_dirty_metadata(handle
, bh
);
793 /* correct old index */
795 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
798 path
[i
].p_hdr
->eh_entries
= cpu_to_le16(le16_to_cpu(path
[i
].p_hdr
->eh_entries
)-m
);
799 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
807 /* insert new index */
808 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
809 le32_to_cpu(border
), newblock
);
813 if (buffer_locked(bh
))
819 /* free all allocated blocks in error case */
820 for (i
= 0; i
< depth
; i
++) {
823 ext4_free_blocks(handle
, inode
, ablocks
[i
], 1);
832 * ext4_ext_grow_indepth:
833 * implements tree growing procedure:
834 * - allocates new block
835 * - moves top-level data (index block or leaf) into the new block
836 * - initializes new top-level, creating index that points to the
839 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
840 struct ext4_ext_path
*path
,
841 struct ext4_extent
*newext
)
843 struct ext4_ext_path
*curp
= path
;
844 struct ext4_extent_header
*neh
;
845 struct ext4_extent_idx
*fidx
;
846 struct buffer_head
*bh
;
847 ext4_fsblk_t newblock
;
850 newblock
= ext4_ext_new_block(handle
, inode
, path
, newext
, &err
);
854 bh
= sb_getblk(inode
->i_sb
, newblock
);
857 ext4_std_error(inode
->i_sb
, err
);
862 err
= ext4_journal_get_create_access(handle
, bh
);
868 /* move top-level index/leaf into new block */
869 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
871 /* set size of new block */
872 neh
= ext_block_hdr(bh
);
873 /* old root could have indexes or leaves
874 * so calculate e_max right way */
875 if (ext_depth(inode
))
876 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
878 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
879 neh
->eh_magic
= EXT4_EXT_MAGIC
;
880 set_buffer_uptodate(bh
);
883 err
= ext4_journal_dirty_metadata(handle
, bh
);
887 /* create index in new top-level index: num,max,pointer */
888 err
= ext4_ext_get_access(handle
, inode
, curp
);
892 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
893 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
));
894 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
895 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
896 /* FIXME: it works, but actually path[0] can be index */
897 curp
->p_idx
->ei_block
= EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
898 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
900 neh
= ext_inode_hdr(inode
);
901 fidx
= EXT_FIRST_INDEX(neh
);
902 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
903 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
904 le32_to_cpu(fidx
->ei_block
), idx_pblock(fidx
));
906 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
907 err
= ext4_ext_dirty(handle
, inode
, curp
);
915 * ext4_ext_create_new_leaf:
916 * finds empty index and adds new leaf.
917 * if no free index is found, then it requests in-depth growing.
919 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
920 struct ext4_ext_path
*path
,
921 struct ext4_extent
*newext
)
923 struct ext4_ext_path
*curp
;
924 int depth
, i
, err
= 0;
927 i
= depth
= ext_depth(inode
);
929 /* walk up to the tree and look for free index entry */
931 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
936 /* we use already allocated block for index block,
937 * so subsequent data blocks should be contiguous */
938 if (EXT_HAS_FREE_INDEX(curp
)) {
939 /* if we found index with free entry, then use that
940 * entry: create all needed subtree and add new leaf */
941 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
944 ext4_ext_drop_refs(path
);
945 path
= ext4_ext_find_extent(inode
,
946 le32_to_cpu(newext
->ee_block
),
951 /* tree is full, time to grow in depth */
952 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
957 ext4_ext_drop_refs(path
);
958 path
= ext4_ext_find_extent(inode
,
959 le32_to_cpu(newext
->ee_block
),
967 * only first (depth 0 -> 1) produces free space;
968 * in all other cases we have to split the grown tree
970 depth
= ext_depth(inode
);
971 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
972 /* now we need to split */
982 * ext4_ext_next_allocated_block:
983 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
984 * NOTE: it considers block number from index entry as
985 * allocated block. Thus, index entries have to be consistent
989 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
993 BUG_ON(path
== NULL
);
994 depth
= path
->p_depth
;
996 if (depth
== 0 && path
->p_ext
== NULL
)
997 return EXT_MAX_BLOCK
;
1000 if (depth
== path
->p_depth
) {
1002 if (path
[depth
].p_ext
!=
1003 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1004 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1007 if (path
[depth
].p_idx
!=
1008 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1009 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1014 return EXT_MAX_BLOCK
;
1018 * ext4_ext_next_leaf_block:
1019 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1021 static unsigned ext4_ext_next_leaf_block(struct inode
*inode
,
1022 struct ext4_ext_path
*path
)
1026 BUG_ON(path
== NULL
);
1027 depth
= path
->p_depth
;
1029 /* zero-tree has no leaf blocks at all */
1031 return EXT_MAX_BLOCK
;
1033 /* go to index block */
1036 while (depth
>= 0) {
1037 if (path
[depth
].p_idx
!=
1038 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1039 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1043 return EXT_MAX_BLOCK
;
1047 * ext4_ext_correct_indexes:
1048 * if leaf gets modified and modified extent is first in the leaf,
1049 * then we have to correct all indexes above.
1050 * TODO: do we need to correct tree in all cases?
1052 int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1053 struct ext4_ext_path
*path
)
1055 struct ext4_extent_header
*eh
;
1056 int depth
= ext_depth(inode
);
1057 struct ext4_extent
*ex
;
1061 eh
= path
[depth
].p_hdr
;
1062 ex
= path
[depth
].p_ext
;
1067 /* there is no tree at all */
1071 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1072 /* we correct tree if first leaf got modified only */
1077 * TODO: we need correction if border is smaller than current one
1080 border
= path
[depth
].p_ext
->ee_block
;
1081 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1084 path
[k
].p_idx
->ei_block
= border
;
1085 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1090 /* change all left-side indexes */
1091 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1093 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1096 path
[k
].p_idx
->ei_block
= border
;
1097 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1106 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1107 struct ext4_extent
*ex2
)
1109 if (le32_to_cpu(ex1
->ee_block
) + le16_to_cpu(ex1
->ee_len
) !=
1110 le32_to_cpu(ex2
->ee_block
))
1114 * To allow future support for preallocated extents to be added
1115 * as an RO_COMPAT feature, refuse to merge to extents if
1116 * this can result in the top bit of ee_len being set.
1118 if (le16_to_cpu(ex1
->ee_len
) + le16_to_cpu(ex2
->ee_len
) > EXT_MAX_LEN
)
1120 #ifdef AGGRESSIVE_TEST
1121 if (le16_to_cpu(ex1
->ee_len
) >= 4)
1125 if (ext_pblock(ex1
) + le16_to_cpu(ex1
->ee_len
) == ext_pblock(ex2
))
1131 * ext4_ext_insert_extent:
1132 * tries to merge requsted extent into the existing extent or
1133 * inserts requested extent as new one into the tree,
1134 * creating new leaf in the no-space case.
1136 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1137 struct ext4_ext_path
*path
,
1138 struct ext4_extent
*newext
)
1140 struct ext4_extent_header
* eh
;
1141 struct ext4_extent
*ex
, *fex
;
1142 struct ext4_extent
*nearex
; /* nearest extent */
1143 struct ext4_ext_path
*npath
= NULL
;
1144 int depth
, len
, err
, next
;
1146 BUG_ON(newext
->ee_len
== 0);
1147 depth
= ext_depth(inode
);
1148 ex
= path
[depth
].p_ext
;
1149 BUG_ON(path
[depth
].p_hdr
== NULL
);
1151 /* try to insert block into found extent and return */
1152 if (ex
&& ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1153 ext_debug("append %d block to %d:%d (from %llu)\n",
1154 le16_to_cpu(newext
->ee_len
),
1155 le32_to_cpu(ex
->ee_block
),
1156 le16_to_cpu(ex
->ee_len
), ext_pblock(ex
));
1157 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1160 ex
->ee_len
= cpu_to_le16(le16_to_cpu(ex
->ee_len
)
1161 + le16_to_cpu(newext
->ee_len
));
1162 eh
= path
[depth
].p_hdr
;
1168 depth
= ext_depth(inode
);
1169 eh
= path
[depth
].p_hdr
;
1170 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1173 /* probably next leaf has space for us? */
1174 fex
= EXT_LAST_EXTENT(eh
);
1175 next
= ext4_ext_next_leaf_block(inode
, path
);
1176 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1177 && next
!= EXT_MAX_BLOCK
) {
1178 ext_debug("next leaf block - %d\n", next
);
1179 BUG_ON(npath
!= NULL
);
1180 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1182 return PTR_ERR(npath
);
1183 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1184 eh
= npath
[depth
].p_hdr
;
1185 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1186 ext_debug("next leaf isnt full(%d)\n",
1187 le16_to_cpu(eh
->eh_entries
));
1191 ext_debug("next leaf has no free space(%d,%d)\n",
1192 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1196 * There is no free space in the found leaf.
1197 * We're gonna add a new leaf in the tree.
1199 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1202 depth
= ext_depth(inode
);
1203 eh
= path
[depth
].p_hdr
;
1206 nearex
= path
[depth
].p_ext
;
1208 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1213 /* there is no extent in this leaf, create first one */
1214 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1215 le32_to_cpu(newext
->ee_block
),
1217 le16_to_cpu(newext
->ee_len
));
1218 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1219 } else if (le32_to_cpu(newext
->ee_block
)
1220 > le32_to_cpu(nearex
->ee_block
)) {
1221 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1222 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1223 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1224 len
= (len
- 1) * sizeof(struct ext4_extent
);
1225 len
= len
< 0 ? 0 : len
;
1226 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1227 "move %d from 0x%p to 0x%p\n",
1228 le32_to_cpu(newext
->ee_block
),
1230 le16_to_cpu(newext
->ee_len
),
1231 nearex
, len
, nearex
+ 1, nearex
+ 2);
1232 memmove(nearex
+ 2, nearex
+ 1, len
);
1234 path
[depth
].p_ext
= nearex
+ 1;
1236 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1237 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1238 len
= len
< 0 ? 0 : len
;
1239 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1240 "move %d from 0x%p to 0x%p\n",
1241 le32_to_cpu(newext
->ee_block
),
1243 le16_to_cpu(newext
->ee_len
),
1244 nearex
, len
, nearex
+ 1, nearex
+ 2);
1245 memmove(nearex
+ 1, nearex
, len
);
1246 path
[depth
].p_ext
= nearex
;
1249 eh
->eh_entries
= cpu_to_le16(le16_to_cpu(eh
->eh_entries
)+1);
1250 nearex
= path
[depth
].p_ext
;
1251 nearex
->ee_block
= newext
->ee_block
;
1252 nearex
->ee_start
= newext
->ee_start
;
1253 nearex
->ee_start_hi
= newext
->ee_start_hi
;
1254 nearex
->ee_len
= newext
->ee_len
;
1257 /* try to merge extents to the right */
1258 while (nearex
< EXT_LAST_EXTENT(eh
)) {
1259 if (!ext4_can_extents_be_merged(inode
, nearex
, nearex
+ 1))
1261 /* merge with next extent! */
1262 nearex
->ee_len
= cpu_to_le16(le16_to_cpu(nearex
->ee_len
)
1263 + le16_to_cpu(nearex
[1].ee_len
));
1264 if (nearex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1265 len
= (EXT_LAST_EXTENT(eh
) - nearex
- 1)
1266 * sizeof(struct ext4_extent
);
1267 memmove(nearex
+ 1, nearex
+ 2, len
);
1269 eh
->eh_entries
= cpu_to_le16(le16_to_cpu(eh
->eh_entries
)-1);
1270 BUG_ON(eh
->eh_entries
== 0);
1273 /* try to merge extents to the left */
1275 /* time to correct all indexes above */
1276 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1280 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1284 ext4_ext_drop_refs(npath
);
1287 ext4_ext_tree_changed(inode
);
1288 ext4_ext_invalidate_cache(inode
);
1292 int ext4_ext_walk_space(struct inode
*inode
, unsigned long block
,
1293 unsigned long num
, ext_prepare_callback func
,
1296 struct ext4_ext_path
*path
= NULL
;
1297 struct ext4_ext_cache cbex
;
1298 struct ext4_extent
*ex
;
1299 unsigned long next
, start
= 0, end
= 0;
1300 unsigned long last
= block
+ num
;
1301 int depth
, exists
, err
= 0;
1303 BUG_ON(func
== NULL
);
1304 BUG_ON(inode
== NULL
);
1306 while (block
< last
&& block
!= EXT_MAX_BLOCK
) {
1308 /* find extent for this block */
1309 path
= ext4_ext_find_extent(inode
, block
, path
);
1311 err
= PTR_ERR(path
);
1316 depth
= ext_depth(inode
);
1317 BUG_ON(path
[depth
].p_hdr
== NULL
);
1318 ex
= path
[depth
].p_ext
;
1319 next
= ext4_ext_next_allocated_block(path
);
1323 /* there is no extent yet, so try to allocate
1324 * all requested space */
1327 } else if (le32_to_cpu(ex
->ee_block
) > block
) {
1328 /* need to allocate space before found extent */
1330 end
= le32_to_cpu(ex
->ee_block
);
1331 if (block
+ num
< end
)
1334 le32_to_cpu(ex
->ee_block
) + le16_to_cpu(ex
->ee_len
)) {
1335 /* need to allocate space after found extent */
1340 } else if (block
>= le32_to_cpu(ex
->ee_block
)) {
1342 * some part of requested space is covered
1346 end
= le32_to_cpu(ex
->ee_block
) + le16_to_cpu(ex
->ee_len
);
1347 if (block
+ num
< end
)
1353 BUG_ON(end
<= start
);
1356 cbex
.ec_block
= start
;
1357 cbex
.ec_len
= end
- start
;
1359 cbex
.ec_type
= EXT4_EXT_CACHE_GAP
;
1361 cbex
.ec_block
= le32_to_cpu(ex
->ee_block
);
1362 cbex
.ec_len
= le16_to_cpu(ex
->ee_len
);
1363 cbex
.ec_start
= ext_pblock(ex
);
1364 cbex
.ec_type
= EXT4_EXT_CACHE_EXTENT
;
1367 BUG_ON(cbex
.ec_len
== 0);
1368 err
= func(inode
, path
, &cbex
, cbdata
);
1369 ext4_ext_drop_refs(path
);
1373 if (err
== EXT_REPEAT
)
1375 else if (err
== EXT_BREAK
) {
1380 if (ext_depth(inode
) != depth
) {
1381 /* depth was changed. we have to realloc path */
1386 block
= cbex
.ec_block
+ cbex
.ec_len
;
1390 ext4_ext_drop_refs(path
);
1398 ext4_ext_put_in_cache(struct inode
*inode
, __u32 block
,
1399 __u32 len
, __u32 start
, int type
)
1401 struct ext4_ext_cache
*cex
;
1403 cex
= &EXT4_I(inode
)->i_cached_extent
;
1404 cex
->ec_type
= type
;
1405 cex
->ec_block
= block
;
1407 cex
->ec_start
= start
;
1411 * ext4_ext_put_gap_in_cache:
1412 * calculate boundaries of the gap that the requested block fits into
1413 * and cache this gap
1416 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1417 unsigned long block
)
1419 int depth
= ext_depth(inode
);
1420 unsigned long lblock
, len
;
1421 struct ext4_extent
*ex
;
1423 ex
= path
[depth
].p_ext
;
1425 /* there is no extent yet, so gap is [0;-] */
1427 len
= EXT_MAX_BLOCK
;
1428 ext_debug("cache gap(whole file):");
1429 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1431 len
= le32_to_cpu(ex
->ee_block
) - block
;
1432 ext_debug("cache gap(before): %lu [%lu:%lu]",
1433 (unsigned long) block
,
1434 (unsigned long) le32_to_cpu(ex
->ee_block
),
1435 (unsigned long) le16_to_cpu(ex
->ee_len
));
1436 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1437 + le16_to_cpu(ex
->ee_len
)) {
1438 lblock
= le32_to_cpu(ex
->ee_block
)
1439 + le16_to_cpu(ex
->ee_len
);
1440 len
= ext4_ext_next_allocated_block(path
);
1441 ext_debug("cache gap(after): [%lu:%lu] %lu",
1442 (unsigned long) le32_to_cpu(ex
->ee_block
),
1443 (unsigned long) le16_to_cpu(ex
->ee_len
),
1444 (unsigned long) block
);
1445 BUG_ON(len
== lblock
);
1452 ext_debug(" -> %lu:%lu\n", (unsigned long) lblock
, len
);
1453 ext4_ext_put_in_cache(inode
, lblock
, len
, 0, EXT4_EXT_CACHE_GAP
);
1457 ext4_ext_in_cache(struct inode
*inode
, unsigned long block
,
1458 struct ext4_extent
*ex
)
1460 struct ext4_ext_cache
*cex
;
1462 cex
= &EXT4_I(inode
)->i_cached_extent
;
1464 /* has cache valid data? */
1465 if (cex
->ec_type
== EXT4_EXT_CACHE_NO
)
1466 return EXT4_EXT_CACHE_NO
;
1468 BUG_ON(cex
->ec_type
!= EXT4_EXT_CACHE_GAP
&&
1469 cex
->ec_type
!= EXT4_EXT_CACHE_EXTENT
);
1470 if (block
>= cex
->ec_block
&& block
< cex
->ec_block
+ cex
->ec_len
) {
1471 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
1472 ext4_ext_store_pblock(ex
, cex
->ec_start
);
1473 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
1474 ext_debug("%lu cached by %lu:%lu:%llu\n",
1475 (unsigned long) block
,
1476 (unsigned long) cex
->ec_block
,
1477 (unsigned long) cex
->ec_len
,
1479 return cex
->ec_type
;
1483 return EXT4_EXT_CACHE_NO
;
1488 * removes index from the index block.
1489 * It's used in truncate case only, thus all requests are for
1490 * last index in the block only.
1492 int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
1493 struct ext4_ext_path
*path
)
1495 struct buffer_head
*bh
;
1499 /* free index block */
1501 leaf
= idx_pblock(path
->p_idx
);
1502 BUG_ON(path
->p_hdr
->eh_entries
== 0);
1503 err
= ext4_ext_get_access(handle
, inode
, path
);
1506 path
->p_hdr
->eh_entries
= cpu_to_le16(le16_to_cpu(path
->p_hdr
->eh_entries
)-1);
1507 err
= ext4_ext_dirty(handle
, inode
, path
);
1510 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
1511 bh
= sb_find_get_block(inode
->i_sb
, leaf
);
1512 ext4_forget(handle
, 1, inode
, bh
, leaf
);
1513 ext4_free_blocks(handle
, inode
, leaf
, 1);
1518 * ext4_ext_calc_credits_for_insert:
1519 * This routine returns max. credits that the extent tree can consume.
1520 * It should be OK for low-performance paths like ->writepage()
1521 * To allow many writing processes to fit into a single transaction,
1522 * the caller should calculate credits under truncate_mutex and
1523 * pass the actual path.
1525 int ext4_ext_calc_credits_for_insert(struct inode
*inode
,
1526 struct ext4_ext_path
*path
)
1531 /* probably there is space in leaf? */
1532 depth
= ext_depth(inode
);
1533 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
1534 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
))
1539 * given 32-bit logical block (4294967296 blocks), max. tree
1540 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1541 * Let's also add one more level for imbalance.
1545 /* allocation of new data block(s) */
1549 * tree can be full, so it would need to grow in depth:
1550 * we need one credit to modify old root, credits for
1551 * new root will be added in split accounting
1556 * Index split can happen, we would need:
1557 * allocate intermediate indexes (bitmap + group)
1558 * + change two blocks at each level, but root (already included)
1560 needed
+= (depth
* 2) + (depth
* 2);
1562 /* any allocation modifies superblock */
1568 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
1569 struct ext4_extent
*ex
,
1570 unsigned long from
, unsigned long to
)
1572 struct buffer_head
*bh
;
1575 #ifdef EXTENTS_STATS
1577 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1578 unsigned short ee_len
= le16_to_cpu(ex
->ee_len
);
1579 spin_lock(&sbi
->s_ext_stats_lock
);
1580 sbi
->s_ext_blocks
+= ee_len
;
1581 sbi
->s_ext_extents
++;
1582 if (ee_len
< sbi
->s_ext_min
)
1583 sbi
->s_ext_min
= ee_len
;
1584 if (ee_len
> sbi
->s_ext_max
)
1585 sbi
->s_ext_max
= ee_len
;
1586 if (ext_depth(inode
) > sbi
->s_depth_max
)
1587 sbi
->s_depth_max
= ext_depth(inode
);
1588 spin_unlock(&sbi
->s_ext_stats_lock
);
1591 if (from
>= le32_to_cpu(ex
->ee_block
)
1592 && to
== le32_to_cpu(ex
->ee_block
) + le16_to_cpu(ex
->ee_len
) - 1) {
1596 num
= le32_to_cpu(ex
->ee_block
) + le16_to_cpu(ex
->ee_len
) - from
;
1597 start
= ext_pblock(ex
) + le16_to_cpu(ex
->ee_len
) - num
;
1598 ext_debug("free last %lu blocks starting %llu\n", num
, start
);
1599 for (i
= 0; i
< num
; i
++) {
1600 bh
= sb_find_get_block(inode
->i_sb
, start
+ i
);
1601 ext4_forget(handle
, 0, inode
, bh
, start
+ i
);
1603 ext4_free_blocks(handle
, inode
, start
, num
);
1604 } else if (from
== le32_to_cpu(ex
->ee_block
)
1605 && to
<= le32_to_cpu(ex
->ee_block
) + le16_to_cpu(ex
->ee_len
) - 1) {
1606 printk("strange request: removal %lu-%lu from %u:%u\n",
1607 from
, to
, le32_to_cpu(ex
->ee_block
), le16_to_cpu(ex
->ee_len
));
1609 printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1610 from
, to
, le32_to_cpu(ex
->ee_block
), le16_to_cpu(ex
->ee_len
));
1616 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
1617 struct ext4_ext_path
*path
, unsigned long start
)
1619 int err
= 0, correct_index
= 0;
1620 int depth
= ext_depth(inode
), credits
;
1621 struct ext4_extent_header
*eh
;
1622 unsigned a
, b
, block
, num
;
1623 unsigned long ex_ee_block
;
1624 unsigned short ex_ee_len
;
1625 struct ext4_extent
*ex
;
1627 ext_debug("truncate since %lu in leaf\n", start
);
1628 if (!path
[depth
].p_hdr
)
1629 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
1630 eh
= path
[depth
].p_hdr
;
1632 BUG_ON(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
));
1633 BUG_ON(eh
->eh_magic
!= EXT4_EXT_MAGIC
);
1635 /* find where to start removing */
1636 ex
= EXT_LAST_EXTENT(eh
);
1638 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
1639 ex_ee_len
= le16_to_cpu(ex
->ee_len
);
1641 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
1642 ex_ee_block
+ ex_ee_len
> start
) {
1643 ext_debug("remove ext %lu:%u\n", ex_ee_block
, ex_ee_len
);
1644 path
[depth
].p_ext
= ex
;
1646 a
= ex_ee_block
> start
? ex_ee_block
: start
;
1647 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
1648 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
1650 ext_debug(" border %u:%u\n", a
, b
);
1652 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
1656 } else if (a
!= ex_ee_block
) {
1657 /* remove tail of the extent */
1658 block
= ex_ee_block
;
1660 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
1661 /* remove head of the extent */
1664 /* there is no "make a hole" API yet */
1667 /* remove whole extent: excellent! */
1668 block
= ex_ee_block
;
1670 BUG_ON(a
!= ex_ee_block
);
1671 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
1674 /* at present, extent can't cross block group: */
1675 /* leaf + bitmap + group desc + sb + inode */
1677 if (ex
== EXT_FIRST_EXTENT(eh
)) {
1679 credits
+= (ext_depth(inode
)) + 1;
1682 credits
+= 2 * EXT4_QUOTA_TRANS_BLOCKS(inode
->i_sb
);
1685 handle
= ext4_ext_journal_restart(handle
, credits
);
1686 if (IS_ERR(handle
)) {
1687 err
= PTR_ERR(handle
);
1691 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1695 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
1700 /* this extent is removed; mark slot entirely unused */
1701 ext4_ext_store_pblock(ex
, 0);
1702 eh
->eh_entries
= cpu_to_le16(le16_to_cpu(eh
->eh_entries
)-1);
1705 ex
->ee_block
= cpu_to_le32(block
);
1706 ex
->ee_len
= cpu_to_le16(num
);
1708 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1712 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
1715 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
1716 ex_ee_len
= le16_to_cpu(ex
->ee_len
);
1719 if (correct_index
&& eh
->eh_entries
)
1720 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1722 /* if this leaf is free, then we should
1723 * remove it from index block above */
1724 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
1725 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
1732 * ext4_ext_more_to_rm:
1733 * returns 1 if current index has to be freed (even partial)
1736 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
1738 BUG_ON(path
->p_idx
== NULL
);
1740 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
1744 * if truncate on deeper level happened, it wasn't partial,
1745 * so we have to consider current index for truncation
1747 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
1752 int ext4_ext_remove_space(struct inode
*inode
, unsigned long start
)
1754 struct super_block
*sb
= inode
->i_sb
;
1755 int depth
= ext_depth(inode
);
1756 struct ext4_ext_path
*path
;
1760 ext_debug("truncate since %lu\n", start
);
1762 /* probably first extent we're gonna free will be last in block */
1763 handle
= ext4_journal_start(inode
, depth
+ 1);
1765 return PTR_ERR(handle
);
1767 ext4_ext_invalidate_cache(inode
);
1770 * We start scanning from right side, freeing all the blocks
1771 * after i_size and walking into the tree depth-wise.
1773 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_KERNEL
);
1775 ext4_journal_stop(handle
);
1778 path
[0].p_hdr
= ext_inode_hdr(inode
);
1779 if (ext4_ext_check_header(__FUNCTION__
, inode
, path
[0].p_hdr
)) {
1783 path
[0].p_depth
= depth
;
1785 while (i
>= 0 && err
== 0) {
1787 /* this is leaf block */
1788 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
1789 /* root level has p_bh == NULL, brelse() eats this */
1790 brelse(path
[i
].p_bh
);
1791 path
[i
].p_bh
= NULL
;
1796 /* this is index block */
1797 if (!path
[i
].p_hdr
) {
1798 ext_debug("initialize header\n");
1799 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
1800 if (ext4_ext_check_header(__FUNCTION__
, inode
,
1807 BUG_ON(le16_to_cpu(path
[i
].p_hdr
->eh_entries
)
1808 > le16_to_cpu(path
[i
].p_hdr
->eh_max
));
1809 BUG_ON(path
[i
].p_hdr
->eh_magic
!= EXT4_EXT_MAGIC
);
1811 if (!path
[i
].p_idx
) {
1812 /* this level hasn't been touched yet */
1813 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
1814 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
1815 ext_debug("init index ptr: hdr 0x%p, num %d\n",
1817 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
1819 /* we were already here, see at next index */
1823 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
1824 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
1826 if (ext4_ext_more_to_rm(path
+ i
)) {
1827 /* go to the next level */
1828 ext_debug("move to level %d (block %llu)\n",
1829 i
+ 1, idx_pblock(path
[i
].p_idx
));
1830 memset(path
+ i
+ 1, 0, sizeof(*path
));
1832 sb_bread(sb
, idx_pblock(path
[i
].p_idx
));
1833 if (!path
[i
+1].p_bh
) {
1834 /* should we reset i_size? */
1839 /* save actual number of indexes since this
1840 * number is changed at the next iteration */
1841 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
1844 /* we finished processing this index, go up */
1845 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
1846 /* index is empty, remove it;
1847 * handle must be already prepared by the
1848 * truncatei_leaf() */
1849 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
1851 /* root level has p_bh == NULL, brelse() eats this */
1852 brelse(path
[i
].p_bh
);
1853 path
[i
].p_bh
= NULL
;
1855 ext_debug("return to level %d\n", i
);
1859 /* TODO: flexible tree reduction should be here */
1860 if (path
->p_hdr
->eh_entries
== 0) {
1862 * truncate to zero freed all the tree,
1863 * so we need to correct eh_depth
1865 err
= ext4_ext_get_access(handle
, inode
, path
);
1867 ext_inode_hdr(inode
)->eh_depth
= 0;
1868 ext_inode_hdr(inode
)->eh_max
=
1869 cpu_to_le16(ext4_ext_space_root(inode
));
1870 err
= ext4_ext_dirty(handle
, inode
, path
);
1874 ext4_ext_tree_changed(inode
);
1875 ext4_ext_drop_refs(path
);
1877 ext4_journal_stop(handle
);
1883 * called at mount time
1885 void ext4_ext_init(struct super_block
*sb
)
1888 * possible initialization would be here
1891 if (test_opt(sb
, EXTENTS
)) {
1892 printk("EXT4-fs: file extents enabled");
1893 #ifdef AGGRESSIVE_TEST
1894 printk(", aggressive tests");
1896 #ifdef CHECK_BINSEARCH
1897 printk(", check binsearch");
1899 #ifdef EXTENTS_STATS
1903 #ifdef EXTENTS_STATS
1904 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
1905 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
1906 EXT4_SB(sb
)->s_ext_max
= 0;
1912 * called at umount time
1914 void ext4_ext_release(struct super_block
*sb
)
1916 if (!test_opt(sb
, EXTENTS
))
1919 #ifdef EXTENTS_STATS
1920 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
1921 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1922 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
1923 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
1924 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
1925 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
1926 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
1931 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
1932 ext4_fsblk_t iblock
,
1933 unsigned long max_blocks
, struct buffer_head
*bh_result
,
1934 int create
, int extend_disksize
)
1936 struct ext4_ext_path
*path
= NULL
;
1937 struct ext4_extent newex
, *ex
;
1938 ext4_fsblk_t goal
, newblock
;
1940 unsigned long allocated
= 0;
1942 __clear_bit(BH_New
, &bh_result
->b_state
);
1943 ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock
,
1944 max_blocks
, (unsigned) inode
->i_ino
);
1945 mutex_lock(&EXT4_I(inode
)->truncate_mutex
);
1947 /* check in cache */
1948 goal
= ext4_ext_in_cache(inode
, iblock
, &newex
);
1950 if (goal
== EXT4_EXT_CACHE_GAP
) {
1952 /* block isn't allocated yet and
1953 * user doesn't want to allocate it */
1956 /* we should allocate requested block */
1957 } else if (goal
== EXT4_EXT_CACHE_EXTENT
) {
1958 /* block is already allocated */
1960 - le32_to_cpu(newex
.ee_block
)
1961 + ext_pblock(&newex
);
1962 /* number of remaining blocks in the extent */
1963 allocated
= le16_to_cpu(newex
.ee_len
) -
1964 (iblock
- le32_to_cpu(newex
.ee_block
));
1971 /* find extent for this block */
1972 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
1974 err
= PTR_ERR(path
);
1979 depth
= ext_depth(inode
);
1982 * consistent leaf must not be empty;
1983 * this situation is possible, though, _during_ tree modification;
1984 * this is why assert can't be put in ext4_ext_find_extent()
1986 BUG_ON(path
[depth
].p_ext
== NULL
&& depth
!= 0);
1988 ex
= path
[depth
].p_ext
;
1990 unsigned long ee_block
= le32_to_cpu(ex
->ee_block
);
1991 ext4_fsblk_t ee_start
= ext_pblock(ex
);
1992 unsigned short ee_len
= le16_to_cpu(ex
->ee_len
);
1995 * Allow future support for preallocated extents to be added
1996 * as an RO_COMPAT feature:
1997 * Uninitialized extents are treated as holes, except that
1998 * we avoid (fail) allocating new blocks during a write.
2000 if (ee_len
> EXT_MAX_LEN
)
2002 /* if found extent covers block, simply return it */
2003 if (iblock
>= ee_block
&& iblock
< ee_block
+ ee_len
) {
2004 newblock
= iblock
- ee_block
+ ee_start
;
2005 /* number of remaining blocks in the extent */
2006 allocated
= ee_len
- (iblock
- ee_block
);
2007 ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock
,
2008 ee_block
, ee_len
, newblock
);
2009 ext4_ext_put_in_cache(inode
, ee_block
, ee_len
,
2010 ee_start
, EXT4_EXT_CACHE_EXTENT
);
2016 * requested block isn't allocated yet;
2017 * we couldn't try to create block if create flag is zero
2020 /* put just found gap into cache to speed up
2021 * subsequent requests */
2022 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
2026 * Okay, we need to do block allocation. Lazily initialize the block
2027 * allocation info here if necessary.
2029 if (S_ISREG(inode
->i_mode
) && (!EXT4_I(inode
)->i_block_alloc_info
))
2030 ext4_init_block_alloc_info(inode
);
2032 /* allocate new block */
2033 goal
= ext4_ext_find_goal(inode
, path
, iblock
);
2034 allocated
= max_blocks
;
2035 newblock
= ext4_new_blocks(handle
, inode
, goal
, &allocated
, &err
);
2038 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2039 goal
, newblock
, allocated
);
2041 /* try to insert new extent into found leaf and return */
2042 newex
.ee_block
= cpu_to_le32(iblock
);
2043 ext4_ext_store_pblock(&newex
, newblock
);
2044 newex
.ee_len
= cpu_to_le16(allocated
);
2045 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2049 if (extend_disksize
&& inode
->i_size
> EXT4_I(inode
)->i_disksize
)
2050 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2052 /* previous routine could use block we allocated */
2053 newblock
= ext_pblock(&newex
);
2054 __set_bit(BH_New
, &bh_result
->b_state
);
2056 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
2057 EXT4_EXT_CACHE_EXTENT
);
2059 if (allocated
> max_blocks
)
2060 allocated
= max_blocks
;
2061 ext4_ext_show_leaf(inode
, path
);
2062 __set_bit(BH_Mapped
, &bh_result
->b_state
);
2063 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
2064 bh_result
->b_blocknr
= newblock
;
2067 ext4_ext_drop_refs(path
);
2070 mutex_unlock(&EXT4_I(inode
)->truncate_mutex
);
2072 return err
? err
: allocated
;
2075 void ext4_ext_truncate(struct inode
* inode
, struct page
*page
)
2077 struct address_space
*mapping
= inode
->i_mapping
;
2078 struct super_block
*sb
= inode
->i_sb
;
2079 unsigned long last_block
;
2084 * probably first extent we're gonna free will be last in block
2086 err
= ext4_writepage_trans_blocks(inode
) + 3;
2087 handle
= ext4_journal_start(inode
, err
);
2088 if (IS_ERR(handle
)) {
2090 clear_highpage(page
);
2091 flush_dcache_page(page
);
2093 page_cache_release(page
);
2099 ext4_block_truncate_page(handle
, page
, mapping
, inode
->i_size
);
2101 mutex_lock(&EXT4_I(inode
)->truncate_mutex
);
2102 ext4_ext_invalidate_cache(inode
);
2105 * TODO: optimization is possible here.
2106 * Probably we need not scan at all,
2107 * because page truncation is enough.
2109 if (ext4_orphan_add(handle
, inode
))
2112 /* we have to know where to truncate from in crash case */
2113 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2114 ext4_mark_inode_dirty(handle
, inode
);
2116 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
2117 >> EXT4_BLOCK_SIZE_BITS(sb
);
2118 err
= ext4_ext_remove_space(inode
, last_block
);
2120 /* In a multi-transaction truncate, we only make the final
2121 * transaction synchronous. */
2127 * If this was a simple ftruncate() and the file will remain alive,
2128 * then we need to clear up the orphan record which we created above.
2129 * However, if this was a real unlink then we were called by
2130 * ext4_delete_inode(), and we allow that function to clean up the
2131 * orphan info for us.
2134 ext4_orphan_del(handle
, inode
);
2136 mutex_unlock(&EXT4_I(inode
)->truncate_mutex
);
2137 ext4_journal_stop(handle
);
2141 * ext4_ext_writepage_trans_blocks:
2142 * calculate max number of blocks we could modify
2143 * in order to allocate new block for an inode
2145 int ext4_ext_writepage_trans_blocks(struct inode
*inode
, int num
)
2149 needed
= ext4_ext_calc_credits_for_insert(inode
, NULL
);
2151 /* caller wants to allocate num blocks, but note it includes sb */
2152 needed
= needed
* num
- (num
- 1);
2155 needed
+= 2 * EXT4_QUOTA_TRANS_BLOCKS(inode
->i_sb
);
2161 EXPORT_SYMBOL(ext4_mark_inode_dirty
);
2162 EXPORT_SYMBOL(ext4_ext_invalidate_cache
);
2163 EXPORT_SYMBOL(ext4_ext_insert_extent
);
2164 EXPORT_SYMBOL(ext4_ext_walk_space
);
2165 EXPORT_SYMBOL(ext4_ext_find_goal
);
2166 EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert
);