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/jbd2.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/falloc.h>
43 #include <linux/ext4_fs_extents.h>
44 #include <asm/uaccess.h>
49 * combine low and high parts of physical block number into ext4_fsblk_t
51 static ext4_fsblk_t
ext_pblock(struct ext4_extent
*ex
)
55 block
= le32_to_cpu(ex
->ee_start_lo
);
56 block
|= ((ext4_fsblk_t
) le16_to_cpu(ex
->ee_start_hi
) << 31) << 1;
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64 ext4_fsblk_t
idx_pblock(struct ext4_extent_idx
*ix
)
68 block
= le32_to_cpu(ix
->ei_leaf_lo
);
69 block
|= ((ext4_fsblk_t
) le16_to_cpu(ix
->ei_leaf_hi
) << 31) << 1;
74 * ext4_ext_store_pblock:
75 * stores a large physical block number into an extent struct,
76 * breaking it into parts
78 void ext4_ext_store_pblock(struct ext4_extent
*ex
, ext4_fsblk_t pb
)
80 ex
->ee_start_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
81 ex
->ee_start_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
85 * ext4_idx_store_pblock:
86 * stores a large physical block number into an index struct,
87 * breaking it into parts
89 static void ext4_idx_store_pblock(struct ext4_extent_idx
*ix
, ext4_fsblk_t pb
)
91 ix
->ei_leaf_lo
= cpu_to_le32((unsigned long) (pb
& 0xffffffff));
92 ix
->ei_leaf_hi
= cpu_to_le16((unsigned long) ((pb
>> 31) >> 1) & 0xffff);
95 static handle_t
*ext4_ext_journal_restart(handle_t
*handle
, int needed
)
99 if (handle
->h_buffer_credits
> needed
)
101 if (!ext4_journal_extend(handle
, needed
))
103 err
= ext4_journal_restart(handle
, needed
);
113 static int ext4_ext_get_access(handle_t
*handle
, struct inode
*inode
,
114 struct ext4_ext_path
*path
)
117 /* path points to block */
118 return ext4_journal_get_write_access(handle
, path
->p_bh
);
120 /* path points to leaf/index in inode body */
121 /* we use in-core data, no need to protect them */
131 static int ext4_ext_dirty(handle_t
*handle
, struct inode
*inode
,
132 struct ext4_ext_path
*path
)
136 /* path points to block */
137 err
= ext4_journal_dirty_metadata(handle
, path
->p_bh
);
139 /* path points to leaf/index in inode body */
140 err
= ext4_mark_inode_dirty(handle
, inode
);
145 static ext4_fsblk_t
ext4_ext_find_goal(struct inode
*inode
,
146 struct ext4_ext_path
*path
,
149 struct ext4_inode_info
*ei
= EXT4_I(inode
);
150 ext4_fsblk_t bg_start
;
151 <<<<<<< HEAD
:fs
/ext4
/extents
.c
153 ext4_fsblk_t last_block
;
154 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
155 ext4_grpblk_t colour
;
159 struct ext4_extent
*ex
;
160 depth
= path
->p_depth
;
162 /* try to predict block placement */
163 ex
= path
[depth
].p_ext
;
165 return ext_pblock(ex
)+(block
-le32_to_cpu(ex
->ee_block
));
167 /* it looks like index is empty;
168 * try to find starting block from index itself */
169 if (path
[depth
].p_bh
)
170 return path
[depth
].p_bh
->b_blocknr
;
173 /* OK. use inode's group */
174 bg_start
= (ei
->i_block_group
* EXT4_BLOCKS_PER_GROUP(inode
->i_sb
)) +
175 le32_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_first_data_block
);
176 <<<<<<< HEAD
:fs
/ext4
/extents
.c
177 colour
= (current
->pid
% 16) *
179 last_block
= ext4_blocks_count(EXT4_SB(inode
->i_sb
)->s_es
) - 1;
181 if (bg_start
+ EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) <= last_block
)
182 colour
= (current
->pid
% 16) *
183 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
184 (EXT4_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
185 <<<<<<< HEAD
:fs
/ext4
/extents
.c
188 colour
= (current
->pid
% 16) * ((last_block
- bg_start
) / 16);
189 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
190 return bg_start
+ colour
+ block
;
194 ext4_ext_new_block(handle_t
*handle
, struct inode
*inode
,
195 struct ext4_ext_path
*path
,
196 struct ext4_extent
*ex
, int *err
)
198 ext4_fsblk_t goal
, newblock
;
200 goal
= ext4_ext_find_goal(inode
, path
, le32_to_cpu(ex
->ee_block
));
201 newblock
= ext4_new_block(handle
, inode
, goal
, err
);
205 static int ext4_ext_space_block(struct inode
*inode
)
209 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
210 / sizeof(struct ext4_extent
);
211 #ifdef AGGRESSIVE_TEST
218 static int ext4_ext_space_block_idx(struct inode
*inode
)
222 size
= (inode
->i_sb
->s_blocksize
- sizeof(struct ext4_extent_header
))
223 / sizeof(struct ext4_extent_idx
);
224 #ifdef AGGRESSIVE_TEST
231 static int ext4_ext_space_root(struct inode
*inode
)
235 size
= sizeof(EXT4_I(inode
)->i_data
);
236 size
-= sizeof(struct ext4_extent_header
);
237 size
/= sizeof(struct ext4_extent
);
238 #ifdef AGGRESSIVE_TEST
245 static int ext4_ext_space_root_idx(struct inode
*inode
)
249 size
= sizeof(EXT4_I(inode
)->i_data
);
250 size
-= sizeof(struct ext4_extent_header
);
251 size
/= sizeof(struct ext4_extent_idx
);
252 #ifdef AGGRESSIVE_TEST
260 ext4_ext_max_entries(struct inode
*inode
, int depth
)
264 if (depth
== ext_depth(inode
)) {
266 max
= ext4_ext_space_root(inode
);
268 max
= ext4_ext_space_root_idx(inode
);
271 max
= ext4_ext_space_block(inode
);
273 max
= ext4_ext_space_block_idx(inode
);
279 static int __ext4_ext_check_header(const char *function
, struct inode
*inode
,
280 struct ext4_extent_header
*eh
,
283 const char *error_msg
;
286 if (unlikely(eh
->eh_magic
!= EXT4_EXT_MAGIC
)) {
287 error_msg
= "invalid magic";
290 if (unlikely(le16_to_cpu(eh
->eh_depth
) != depth
)) {
291 error_msg
= "unexpected eh_depth";
294 if (unlikely(eh
->eh_max
== 0)) {
295 error_msg
= "invalid eh_max";
298 max
= ext4_ext_max_entries(inode
, depth
);
299 if (unlikely(le16_to_cpu(eh
->eh_max
) > max
)) {
300 error_msg
= "too large eh_max";
303 if (unlikely(le16_to_cpu(eh
->eh_entries
) > le16_to_cpu(eh
->eh_max
))) {
304 error_msg
= "invalid eh_entries";
310 ext4_error(inode
->i_sb
, function
,
311 "bad header in inode #%lu: %s - magic %x, "
312 "entries %u, max %u(%u), depth %u(%u)",
313 inode
->i_ino
, error_msg
, le16_to_cpu(eh
->eh_magic
),
314 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
),
315 max
, le16_to_cpu(eh
->eh_depth
), depth
);
320 #define ext4_ext_check_header(inode, eh, depth) \
321 __ext4_ext_check_header(__FUNCTION__, inode, eh, depth)
324 static void ext4_ext_show_path(struct inode
*inode
, struct ext4_ext_path
*path
)
326 int k
, l
= path
->p_depth
;
329 for (k
= 0; k
<= l
; k
++, path
++) {
331 ext_debug(" %d->%llu", le32_to_cpu(path
->p_idx
->ei_block
),
332 idx_pblock(path
->p_idx
));
333 } else if (path
->p_ext
) {
334 ext_debug(" %d:%d:%llu ",
335 le32_to_cpu(path
->p_ext
->ee_block
),
336 ext4_ext_get_actual_len(path
->p_ext
),
337 ext_pblock(path
->p_ext
));
344 static void ext4_ext_show_leaf(struct inode
*inode
, struct ext4_ext_path
*path
)
346 int depth
= ext_depth(inode
);
347 struct ext4_extent_header
*eh
;
348 struct ext4_extent
*ex
;
354 eh
= path
[depth
].p_hdr
;
355 ex
= EXT_FIRST_EXTENT(eh
);
357 for (i
= 0; i
< le16_to_cpu(eh
->eh_entries
); i
++, ex
++) {
358 ext_debug("%d:%d:%llu ", le32_to_cpu(ex
->ee_block
),
359 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
364 #define ext4_ext_show_path(inode,path)
365 #define ext4_ext_show_leaf(inode,path)
368 <<<<<<< HEAD
:fs
/ext4
/extents
.c
369 static void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
371 void ext4_ext_drop_refs(struct ext4_ext_path
*path
)
372 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
374 int depth
= path
->p_depth
;
377 for (i
= 0; i
<= depth
; i
++, path
++)
385 * ext4_ext_binsearch_idx:
386 * binary search for the closest index of the given block
387 * the header must be checked before calling this
390 ext4_ext_binsearch_idx(struct inode
*inode
,
391 struct ext4_ext_path
*path
, ext4_lblk_t block
)
393 struct ext4_extent_header
*eh
= path
->p_hdr
;
394 struct ext4_extent_idx
*r
, *l
, *m
;
397 ext_debug("binsearch for %u(idx): ", block
);
399 l
= EXT_FIRST_INDEX(eh
) + 1;
400 r
= EXT_LAST_INDEX(eh
);
403 if (block
< le32_to_cpu(m
->ei_block
))
407 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ei_block
),
408 m
, le32_to_cpu(m
->ei_block
),
409 r
, le32_to_cpu(r
->ei_block
));
413 ext_debug(" -> %d->%lld ", le32_to_cpu(path
->p_idx
->ei_block
),
414 idx_pblock(path
->p_idx
));
416 #ifdef CHECK_BINSEARCH
418 struct ext4_extent_idx
*chix
, *ix
;
421 chix
= ix
= EXT_FIRST_INDEX(eh
);
422 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ix
++) {
424 le32_to_cpu(ix
->ei_block
) <= le32_to_cpu(ix
[-1].ei_block
)) {
425 printk("k=%d, ix=0x%p, first=0x%p\n", k
,
426 ix
, EXT_FIRST_INDEX(eh
));
428 le32_to_cpu(ix
->ei_block
),
429 le32_to_cpu(ix
[-1].ei_block
));
431 BUG_ON(k
&& le32_to_cpu(ix
->ei_block
)
432 <= le32_to_cpu(ix
[-1].ei_block
));
433 if (block
< le32_to_cpu(ix
->ei_block
))
437 BUG_ON(chix
!= path
->p_idx
);
444 * ext4_ext_binsearch:
445 * binary search for closest extent of the given block
446 * the header must be checked before calling this
449 ext4_ext_binsearch(struct inode
*inode
,
450 struct ext4_ext_path
*path
, ext4_lblk_t block
)
452 struct ext4_extent_header
*eh
= path
->p_hdr
;
453 struct ext4_extent
*r
, *l
, *m
;
455 if (eh
->eh_entries
== 0) {
457 * this leaf is empty:
458 * we get such a leaf in split/add case
463 ext_debug("binsearch for %u: ", block
);
465 l
= EXT_FIRST_EXTENT(eh
) + 1;
466 r
= EXT_LAST_EXTENT(eh
);
470 if (block
< le32_to_cpu(m
->ee_block
))
474 ext_debug("%p(%u):%p(%u):%p(%u) ", l
, le32_to_cpu(l
->ee_block
),
475 m
, le32_to_cpu(m
->ee_block
),
476 r
, le32_to_cpu(r
->ee_block
));
480 ext_debug(" -> %d:%llu:%d ",
481 le32_to_cpu(path
->p_ext
->ee_block
),
482 ext_pblock(path
->p_ext
),
483 ext4_ext_get_actual_len(path
->p_ext
));
485 #ifdef CHECK_BINSEARCH
487 struct ext4_extent
*chex
, *ex
;
490 chex
= ex
= EXT_FIRST_EXTENT(eh
);
491 for (k
= 0; k
< le16_to_cpu(eh
->eh_entries
); k
++, ex
++) {
492 BUG_ON(k
&& le32_to_cpu(ex
->ee_block
)
493 <= le32_to_cpu(ex
[-1].ee_block
));
494 if (block
< le32_to_cpu(ex
->ee_block
))
498 BUG_ON(chex
!= path
->p_ext
);
504 int ext4_ext_tree_init(handle_t
*handle
, struct inode
*inode
)
506 struct ext4_extent_header
*eh
;
508 eh
= ext_inode_hdr(inode
);
511 eh
->eh_magic
= EXT4_EXT_MAGIC
;
512 eh
->eh_max
= cpu_to_le16(ext4_ext_space_root(inode
));
513 ext4_mark_inode_dirty(handle
, inode
);
514 ext4_ext_invalidate_cache(inode
);
518 struct ext4_ext_path
*
519 ext4_ext_find_extent(struct inode
*inode
, ext4_lblk_t block
,
520 struct ext4_ext_path
*path
)
522 struct ext4_extent_header
*eh
;
523 struct buffer_head
*bh
;
524 short int depth
, i
, ppos
= 0, alloc
= 0;
526 eh
= ext_inode_hdr(inode
);
527 depth
= ext_depth(inode
);
528 if (ext4_ext_check_header(inode
, eh
, depth
))
529 return ERR_PTR(-EIO
);
532 /* account possible depth increase */
534 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 2),
537 return ERR_PTR(-ENOMEM
);
543 /* walk through the tree */
545 ext_debug("depth %d: num %d, max %d\n",
546 ppos
, le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
548 ext4_ext_binsearch_idx(inode
, path
+ ppos
, block
);
549 path
[ppos
].p_block
= idx_pblock(path
[ppos
].p_idx
);
550 path
[ppos
].p_depth
= i
;
551 path
[ppos
].p_ext
= NULL
;
553 bh
= sb_bread(inode
->i_sb
, path
[ppos
].p_block
);
557 eh
= ext_block_hdr(bh
);
559 BUG_ON(ppos
> depth
);
560 path
[ppos
].p_bh
= bh
;
561 path
[ppos
].p_hdr
= eh
;
564 if (ext4_ext_check_header(inode
, eh
, i
))
568 path
[ppos
].p_depth
= i
;
569 path
[ppos
].p_hdr
= eh
;
570 path
[ppos
].p_ext
= NULL
;
571 path
[ppos
].p_idx
= NULL
;
574 ext4_ext_binsearch(inode
, path
+ ppos
, block
);
576 ext4_ext_show_path(inode
, path
);
581 ext4_ext_drop_refs(path
);
584 return ERR_PTR(-EIO
);
588 * ext4_ext_insert_index:
589 * insert new index [@logical;@ptr] into the block at @curp;
590 * check where to insert: before @curp or after @curp
592 static int ext4_ext_insert_index(handle_t
*handle
, struct inode
*inode
,
593 struct ext4_ext_path
*curp
,
594 int logical
, ext4_fsblk_t ptr
)
596 struct ext4_extent_idx
*ix
;
599 err
= ext4_ext_get_access(handle
, inode
, curp
);
603 BUG_ON(logical
== le32_to_cpu(curp
->p_idx
->ei_block
));
604 len
= EXT_MAX_INDEX(curp
->p_hdr
) - curp
->p_idx
;
605 if (logical
> le32_to_cpu(curp
->p_idx
->ei_block
)) {
607 if (curp
->p_idx
!= EXT_LAST_INDEX(curp
->p_hdr
)) {
608 len
= (len
- 1) * sizeof(struct ext4_extent_idx
);
609 len
= len
< 0 ? 0 : len
;
610 ext_debug("insert new index %d after: %llu. "
611 "move %d from 0x%p to 0x%p\n",
613 (curp
->p_idx
+ 1), (curp
->p_idx
+ 2));
614 memmove(curp
->p_idx
+ 2, curp
->p_idx
+ 1, len
);
616 ix
= curp
->p_idx
+ 1;
619 len
= len
* sizeof(struct ext4_extent_idx
);
620 len
= len
< 0 ? 0 : len
;
621 ext_debug("insert new index %d before: %llu. "
622 "move %d from 0x%p to 0x%p\n",
624 curp
->p_idx
, (curp
->p_idx
+ 1));
625 memmove(curp
->p_idx
+ 1, curp
->p_idx
, len
);
629 ix
->ei_block
= cpu_to_le32(logical
);
630 ext4_idx_store_pblock(ix
, ptr
);
631 curp
->p_hdr
->eh_entries
= cpu_to_le16(le16_to_cpu(curp
->p_hdr
->eh_entries
)+1);
633 BUG_ON(le16_to_cpu(curp
->p_hdr
->eh_entries
)
634 > le16_to_cpu(curp
->p_hdr
->eh_max
));
635 BUG_ON(ix
> EXT_LAST_INDEX(curp
->p_hdr
));
637 err
= ext4_ext_dirty(handle
, inode
, curp
);
638 ext4_std_error(inode
->i_sb
, err
);
645 * inserts new subtree into the path, using free index entry
647 * - allocates all needed blocks (new leaf and all intermediate index blocks)
648 * - makes decision where to split
649 * - moves remaining extents and index entries (right to the split point)
650 * into the newly allocated blocks
651 * - initializes subtree
653 static int ext4_ext_split(handle_t
*handle
, struct inode
*inode
,
654 struct ext4_ext_path
*path
,
655 struct ext4_extent
*newext
, int at
)
657 struct buffer_head
*bh
= NULL
;
658 int depth
= ext_depth(inode
);
659 struct ext4_extent_header
*neh
;
660 struct ext4_extent_idx
*fidx
;
661 struct ext4_extent
*ex
;
663 ext4_fsblk_t newblock
, oldblock
;
665 ext4_fsblk_t
*ablocks
= NULL
; /* array of allocated blocks */
668 /* make decision: where to split? */
669 /* FIXME: now decision is simplest: at current extent */
671 /* if current leaf will be split, then we should use
672 * border from split point */
673 BUG_ON(path
[depth
].p_ext
> EXT_MAX_EXTENT(path
[depth
].p_hdr
));
674 if (path
[depth
].p_ext
!= EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
675 border
= path
[depth
].p_ext
[1].ee_block
;
676 ext_debug("leaf will be split."
677 " next leaf starts at %d\n",
678 le32_to_cpu(border
));
680 border
= newext
->ee_block
;
681 ext_debug("leaf will be added."
682 " next leaf starts at %d\n",
683 le32_to_cpu(border
));
687 * If error occurs, then we break processing
688 * and mark filesystem read-only. index won't
689 * be inserted and tree will be in consistent
690 * state. Next mount will repair buffers too.
694 * Get array to track all allocated blocks.
695 * We need this to handle errors and free blocks
698 ablocks
= kzalloc(sizeof(ext4_fsblk_t
) * depth
, GFP_NOFS
);
702 /* allocate all needed blocks */
703 ext_debug("allocate %d blocks for indexes/leaf\n", depth
- at
);
704 for (a
= 0; a
< depth
- at
; a
++) {
705 newblock
= ext4_ext_new_block(handle
, inode
, path
, newext
, &err
);
708 ablocks
[a
] = newblock
;
711 /* initialize new leaf */
712 newblock
= ablocks
[--a
];
713 BUG_ON(newblock
== 0);
714 bh
= sb_getblk(inode
->i_sb
, newblock
);
721 err
= ext4_journal_get_create_access(handle
, bh
);
725 neh
= ext_block_hdr(bh
);
727 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
728 neh
->eh_magic
= EXT4_EXT_MAGIC
;
730 ex
= EXT_FIRST_EXTENT(neh
);
732 /* move remainder of path[depth] to the new leaf */
733 BUG_ON(path
[depth
].p_hdr
->eh_entries
!= path
[depth
].p_hdr
->eh_max
);
734 /* start copy from next extent */
735 /* TODO: we could do it by single memmove */
738 while (path
[depth
].p_ext
<=
739 EXT_MAX_EXTENT(path
[depth
].p_hdr
)) {
740 ext_debug("move %d:%llu:%d in new leaf %llu\n",
741 le32_to_cpu(path
[depth
].p_ext
->ee_block
),
742 ext_pblock(path
[depth
].p_ext
),
743 ext4_ext_get_actual_len(path
[depth
].p_ext
),
745 /*memmove(ex++, path[depth].p_ext++,
746 sizeof(struct ext4_extent));
752 memmove(ex
, path
[depth
].p_ext
-m
, sizeof(struct ext4_extent
)*m
);
753 neh
->eh_entries
= cpu_to_le16(le16_to_cpu(neh
->eh_entries
)+m
);
756 set_buffer_uptodate(bh
);
759 err
= ext4_journal_dirty_metadata(handle
, bh
);
765 /* correct old leaf */
767 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
770 path
[depth
].p_hdr
->eh_entries
=
771 cpu_to_le16(le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)-m
);
772 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
778 /* create intermediate indexes */
782 ext_debug("create %d intermediate indices\n", k
);
783 /* insert new index into current index block */
784 /* current depth stored in i var */
788 newblock
= ablocks
[--a
];
789 bh
= sb_getblk(inode
->i_sb
, newblock
);
796 err
= ext4_journal_get_create_access(handle
, bh
);
800 neh
= ext_block_hdr(bh
);
801 neh
->eh_entries
= cpu_to_le16(1);
802 neh
->eh_magic
= EXT4_EXT_MAGIC
;
803 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
804 neh
->eh_depth
= cpu_to_le16(depth
- i
);
805 fidx
= EXT_FIRST_INDEX(neh
);
806 fidx
->ei_block
= border
;
807 ext4_idx_store_pblock(fidx
, oldblock
);
809 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
810 i
, newblock
, le32_to_cpu(border
), oldblock
);
815 ext_debug("cur 0x%p, last 0x%p\n", path
[i
].p_idx
,
816 EXT_MAX_INDEX(path
[i
].p_hdr
));
817 BUG_ON(EXT_MAX_INDEX(path
[i
].p_hdr
) !=
818 EXT_LAST_INDEX(path
[i
].p_hdr
));
819 while (path
[i
].p_idx
<= EXT_MAX_INDEX(path
[i
].p_hdr
)) {
820 ext_debug("%d: move %d:%llu in new index %llu\n", i
,
821 le32_to_cpu(path
[i
].p_idx
->ei_block
),
822 idx_pblock(path
[i
].p_idx
),
824 /*memmove(++fidx, path[i].p_idx++,
825 sizeof(struct ext4_extent_idx));
827 BUG_ON(neh->eh_entries > neh->eh_max);*/
832 memmove(++fidx
, path
[i
].p_idx
- m
,
833 sizeof(struct ext4_extent_idx
) * m
);
835 cpu_to_le16(le16_to_cpu(neh
->eh_entries
) + m
);
837 set_buffer_uptodate(bh
);
840 err
= ext4_journal_dirty_metadata(handle
, bh
);
846 /* correct old index */
848 err
= ext4_ext_get_access(handle
, inode
, path
+ i
);
851 path
[i
].p_hdr
->eh_entries
= cpu_to_le16(le16_to_cpu(path
[i
].p_hdr
->eh_entries
)-m
);
852 err
= ext4_ext_dirty(handle
, inode
, path
+ i
);
860 /* insert new index */
861 err
= ext4_ext_insert_index(handle
, inode
, path
+ at
,
862 le32_to_cpu(border
), newblock
);
866 if (buffer_locked(bh
))
872 /* free all allocated blocks in error case */
873 for (i
= 0; i
< depth
; i
++) {
876 ext4_free_blocks(handle
, inode
, ablocks
[i
], 1, 1);
885 * ext4_ext_grow_indepth:
886 * implements tree growing procedure:
887 * - allocates new block
888 * - moves top-level data (index block or leaf) into the new block
889 * - initializes new top-level, creating index that points to the
892 static int ext4_ext_grow_indepth(handle_t
*handle
, struct inode
*inode
,
893 struct ext4_ext_path
*path
,
894 struct ext4_extent
*newext
)
896 struct ext4_ext_path
*curp
= path
;
897 struct ext4_extent_header
*neh
;
898 struct ext4_extent_idx
*fidx
;
899 struct buffer_head
*bh
;
900 ext4_fsblk_t newblock
;
903 newblock
= ext4_ext_new_block(handle
, inode
, path
, newext
, &err
);
907 bh
= sb_getblk(inode
->i_sb
, newblock
);
910 ext4_std_error(inode
->i_sb
, err
);
915 err
= ext4_journal_get_create_access(handle
, bh
);
921 /* move top-level index/leaf into new block */
922 memmove(bh
->b_data
, curp
->p_hdr
, sizeof(EXT4_I(inode
)->i_data
));
924 /* set size of new block */
925 neh
= ext_block_hdr(bh
);
926 /* old root could have indexes or leaves
927 * so calculate e_max right way */
928 if (ext_depth(inode
))
929 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block_idx(inode
));
931 neh
->eh_max
= cpu_to_le16(ext4_ext_space_block(inode
));
932 neh
->eh_magic
= EXT4_EXT_MAGIC
;
933 set_buffer_uptodate(bh
);
936 err
= ext4_journal_dirty_metadata(handle
, bh
);
940 /* create index in new top-level index: num,max,pointer */
941 err
= ext4_ext_get_access(handle
, inode
, curp
);
945 curp
->p_hdr
->eh_magic
= EXT4_EXT_MAGIC
;
946 curp
->p_hdr
->eh_max
= cpu_to_le16(ext4_ext_space_root_idx(inode
));
947 curp
->p_hdr
->eh_entries
= cpu_to_le16(1);
948 curp
->p_idx
= EXT_FIRST_INDEX(curp
->p_hdr
);
950 if (path
[0].p_hdr
->eh_depth
)
951 curp
->p_idx
->ei_block
=
952 EXT_FIRST_INDEX(path
[0].p_hdr
)->ei_block
;
954 curp
->p_idx
->ei_block
=
955 EXT_FIRST_EXTENT(path
[0].p_hdr
)->ee_block
;
956 ext4_idx_store_pblock(curp
->p_idx
, newblock
);
958 neh
= ext_inode_hdr(inode
);
959 fidx
= EXT_FIRST_INDEX(neh
);
960 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
961 le16_to_cpu(neh
->eh_entries
), le16_to_cpu(neh
->eh_max
),
962 le32_to_cpu(fidx
->ei_block
), idx_pblock(fidx
));
964 neh
->eh_depth
= cpu_to_le16(path
->p_depth
+ 1);
965 err
= ext4_ext_dirty(handle
, inode
, curp
);
973 * ext4_ext_create_new_leaf:
974 * finds empty index and adds new leaf.
975 * if no free index is found, then it requests in-depth growing.
977 static int ext4_ext_create_new_leaf(handle_t
*handle
, struct inode
*inode
,
978 struct ext4_ext_path
*path
,
979 struct ext4_extent
*newext
)
981 struct ext4_ext_path
*curp
;
982 int depth
, i
, err
= 0;
985 i
= depth
= ext_depth(inode
);
987 /* walk up to the tree and look for free index entry */
989 while (i
> 0 && !EXT_HAS_FREE_INDEX(curp
)) {
994 /* we use already allocated block for index block,
995 * so subsequent data blocks should be contiguous */
996 if (EXT_HAS_FREE_INDEX(curp
)) {
997 /* if we found index with free entry, then use that
998 * entry: create all needed subtree and add new leaf */
999 err
= ext4_ext_split(handle
, inode
, path
, newext
, i
);
1002 ext4_ext_drop_refs(path
);
1003 path
= ext4_ext_find_extent(inode
,
1004 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1007 err
= PTR_ERR(path
);
1009 /* tree is full, time to grow in depth */
1010 err
= ext4_ext_grow_indepth(handle
, inode
, path
, newext
);
1015 ext4_ext_drop_refs(path
);
1016 path
= ext4_ext_find_extent(inode
,
1017 (ext4_lblk_t
)le32_to_cpu(newext
->ee_block
),
1020 err
= PTR_ERR(path
);
1025 * only first (depth 0 -> 1) produces free space;
1026 * in all other cases we have to split the grown tree
1028 depth
= ext_depth(inode
);
1029 if (path
[depth
].p_hdr
->eh_entries
== path
[depth
].p_hdr
->eh_max
) {
1030 /* now we need to split */
1040 * search the closest allocated block to the left for *logical
1041 * and returns it at @logical + it's physical address at @phys
1042 * if *logical is the smallest allocated block, the function
1043 * returns 0 at @phys
1044 * return value contains 0 (success) or error code
1047 ext4_ext_search_left(struct inode
*inode
, struct ext4_ext_path
*path
,
1048 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1050 struct ext4_extent_idx
*ix
;
1051 struct ext4_extent
*ex
;
1054 BUG_ON(path
== NULL
);
1055 depth
= path
->p_depth
;
1058 if (depth
== 0 && path
->p_ext
== NULL
)
1061 /* usually extent in the path covers blocks smaller
1062 * then *logical, but it can be that extent is the
1063 * first one in the file */
1065 ex
= path
[depth
].p_ext
;
1066 ee_len
= ext4_ext_get_actual_len(ex
);
1067 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1068 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1069 while (--depth
>= 0) {
1070 ix
= path
[depth
].p_idx
;
1071 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1076 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1078 *logical
= le32_to_cpu(ex
->ee_block
) + ee_len
- 1;
1079 *phys
= ext_pblock(ex
) + ee_len
- 1;
1084 * search the closest allocated block to the right for *logical
1085 * and returns it at @logical + it's physical address at @phys
1086 * if *logical is the smallest allocated block, the function
1087 * returns 0 at @phys
1088 * return value contains 0 (success) or error code
1091 ext4_ext_search_right(struct inode
*inode
, struct ext4_ext_path
*path
,
1092 ext4_lblk_t
*logical
, ext4_fsblk_t
*phys
)
1094 struct buffer_head
*bh
= NULL
;
1095 struct ext4_extent_header
*eh
;
1096 struct ext4_extent_idx
*ix
;
1097 struct ext4_extent
*ex
;
1101 BUG_ON(path
== NULL
);
1102 depth
= path
->p_depth
;
1105 if (depth
== 0 && path
->p_ext
== NULL
)
1108 /* usually extent in the path covers blocks smaller
1109 * then *logical, but it can be that extent is the
1110 * first one in the file */
1112 ex
= path
[depth
].p_ext
;
1113 ee_len
= ext4_ext_get_actual_len(ex
);
1114 if (*logical
< le32_to_cpu(ex
->ee_block
)) {
1115 BUG_ON(EXT_FIRST_EXTENT(path
[depth
].p_hdr
) != ex
);
1116 while (--depth
>= 0) {
1117 ix
= path
[depth
].p_idx
;
1118 BUG_ON(ix
!= EXT_FIRST_INDEX(path
[depth
].p_hdr
));
1120 *logical
= le32_to_cpu(ex
->ee_block
);
1121 *phys
= ext_pblock(ex
);
1125 BUG_ON(*logical
< (le32_to_cpu(ex
->ee_block
) + ee_len
));
1127 if (ex
!= EXT_LAST_EXTENT(path
[depth
].p_hdr
)) {
1128 /* next allocated block in this leaf */
1130 *logical
= le32_to_cpu(ex
->ee_block
);
1131 *phys
= ext_pblock(ex
);
1135 /* go up and search for index to the right */
1136 while (--depth
>= 0) {
1137 ix
= path
[depth
].p_idx
;
1138 if (ix
!= EXT_LAST_INDEX(path
[depth
].p_hdr
))
1143 /* we've gone up to the root and
1144 * found no index to the right */
1148 /* we've found index to the right, let's
1149 * follow it and find the closest allocated
1150 * block to the right */
1152 block
= idx_pblock(ix
);
1153 while (++depth
< path
->p_depth
) {
1154 bh
= sb_bread(inode
->i_sb
, block
);
1157 eh
= ext_block_hdr(bh
);
1158 if (ext4_ext_check_header(inode
, eh
, depth
)) {
1162 ix
= EXT_FIRST_INDEX(eh
);
1163 block
= idx_pblock(ix
);
1167 bh
= sb_bread(inode
->i_sb
, block
);
1170 eh
= ext_block_hdr(bh
);
1171 if (ext4_ext_check_header(inode
, eh
, path
->p_depth
- depth
)) {
1175 ex
= EXT_FIRST_EXTENT(eh
);
1176 *logical
= le32_to_cpu(ex
->ee_block
);
1177 *phys
= ext_pblock(ex
);
1184 * ext4_ext_next_allocated_block:
1185 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1186 * NOTE: it considers block number from index entry as
1187 * allocated block. Thus, index entries have to be consistent
1191 ext4_ext_next_allocated_block(struct ext4_ext_path
*path
)
1195 BUG_ON(path
== NULL
);
1196 depth
= path
->p_depth
;
1198 if (depth
== 0 && path
->p_ext
== NULL
)
1199 return EXT_MAX_BLOCK
;
1201 while (depth
>= 0) {
1202 if (depth
== path
->p_depth
) {
1204 if (path
[depth
].p_ext
!=
1205 EXT_LAST_EXTENT(path
[depth
].p_hdr
))
1206 return le32_to_cpu(path
[depth
].p_ext
[1].ee_block
);
1209 if (path
[depth
].p_idx
!=
1210 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1211 return le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1216 return EXT_MAX_BLOCK
;
1220 * ext4_ext_next_leaf_block:
1221 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1223 static ext4_lblk_t
ext4_ext_next_leaf_block(struct inode
*inode
,
1224 struct ext4_ext_path
*path
)
1228 BUG_ON(path
== NULL
);
1229 depth
= path
->p_depth
;
1231 /* zero-tree has no leaf blocks at all */
1233 return EXT_MAX_BLOCK
;
1235 /* go to index block */
1238 while (depth
>= 0) {
1239 if (path
[depth
].p_idx
!=
1240 EXT_LAST_INDEX(path
[depth
].p_hdr
))
1241 return (ext4_lblk_t
)
1242 le32_to_cpu(path
[depth
].p_idx
[1].ei_block
);
1246 return EXT_MAX_BLOCK
;
1250 * ext4_ext_correct_indexes:
1251 * if leaf gets modified and modified extent is first in the leaf,
1252 * then we have to correct all indexes above.
1253 * TODO: do we need to correct tree in all cases?
1255 static int ext4_ext_correct_indexes(handle_t
*handle
, struct inode
*inode
,
1256 struct ext4_ext_path
*path
)
1258 struct ext4_extent_header
*eh
;
1259 int depth
= ext_depth(inode
);
1260 struct ext4_extent
*ex
;
1264 eh
= path
[depth
].p_hdr
;
1265 ex
= path
[depth
].p_ext
;
1270 /* there is no tree at all */
1274 if (ex
!= EXT_FIRST_EXTENT(eh
)) {
1275 /* we correct tree if first leaf got modified only */
1280 * TODO: we need correction if border is smaller than current one
1283 border
= path
[depth
].p_ext
->ee_block
;
1284 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1287 path
[k
].p_idx
->ei_block
= border
;
1288 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1293 /* change all left-side indexes */
1294 if (path
[k
+1].p_idx
!= EXT_FIRST_INDEX(path
[k
+1].p_hdr
))
1296 err
= ext4_ext_get_access(handle
, inode
, path
+ k
);
1299 path
[k
].p_idx
->ei_block
= border
;
1300 err
= ext4_ext_dirty(handle
, inode
, path
+ k
);
1309 ext4_can_extents_be_merged(struct inode
*inode
, struct ext4_extent
*ex1
,
1310 struct ext4_extent
*ex2
)
1312 unsigned short ext1_ee_len
, ext2_ee_len
, max_len
;
1315 * Make sure that either both extents are uninitialized, or
1318 if (ext4_ext_is_uninitialized(ex1
) ^ ext4_ext_is_uninitialized(ex2
))
1321 if (ext4_ext_is_uninitialized(ex1
))
1322 max_len
= EXT_UNINIT_MAX_LEN
;
1324 max_len
= EXT_INIT_MAX_LEN
;
1326 ext1_ee_len
= ext4_ext_get_actual_len(ex1
);
1327 ext2_ee_len
= ext4_ext_get_actual_len(ex2
);
1329 if (le32_to_cpu(ex1
->ee_block
) + ext1_ee_len
!=
1330 le32_to_cpu(ex2
->ee_block
))
1334 * To allow future support for preallocated extents to be added
1335 * as an RO_COMPAT feature, refuse to merge to extents if
1336 * this can result in the top bit of ee_len being set.
1338 if (ext1_ee_len
+ ext2_ee_len
> max_len
)
1340 #ifdef AGGRESSIVE_TEST
1341 if (ext1_ee_len
>= 4)
1345 if (ext_pblock(ex1
) + ext1_ee_len
== ext_pblock(ex2
))
1351 * This function tries to merge the "ex" extent to the next extent in the tree.
1352 * It always tries to merge towards right. If you want to merge towards
1353 * left, pass "ex - 1" as argument instead of "ex".
1354 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1355 * 1 if they got merged.
1357 int ext4_ext_try_to_merge(struct inode
*inode
,
1358 struct ext4_ext_path
*path
,
1359 struct ext4_extent
*ex
)
1361 struct ext4_extent_header
*eh
;
1362 unsigned int depth
, len
;
1364 int uninitialized
= 0;
1366 depth
= ext_depth(inode
);
1367 BUG_ON(path
[depth
].p_hdr
== NULL
);
1368 eh
= path
[depth
].p_hdr
;
1370 while (ex
< EXT_LAST_EXTENT(eh
)) {
1371 if (!ext4_can_extents_be_merged(inode
, ex
, ex
+ 1))
1373 /* merge with next extent! */
1374 if (ext4_ext_is_uninitialized(ex
))
1376 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1377 + ext4_ext_get_actual_len(ex
+ 1));
1379 ext4_ext_mark_uninitialized(ex
);
1381 if (ex
+ 1 < EXT_LAST_EXTENT(eh
)) {
1382 len
= (EXT_LAST_EXTENT(eh
) - ex
- 1)
1383 * sizeof(struct ext4_extent
);
1384 memmove(ex
+ 1, ex
+ 2, len
);
1386 eh
->eh_entries
= cpu_to_le16(le16_to_cpu(eh
->eh_entries
) - 1);
1388 WARN_ON(eh
->eh_entries
== 0);
1389 if (!eh
->eh_entries
)
1390 ext4_error(inode
->i_sb
, "ext4_ext_try_to_merge",
1391 "inode#%lu, eh->eh_entries = 0!", inode
->i_ino
);
1398 * check if a portion of the "newext" extent overlaps with an
1401 * If there is an overlap discovered, it updates the length of the newext
1402 * such that there will be no overlap, and then returns 1.
1403 * If there is no overlap found, it returns 0.
1405 unsigned int ext4_ext_check_overlap(struct inode
*inode
,
1406 struct ext4_extent
*newext
,
1407 struct ext4_ext_path
*path
)
1410 unsigned int depth
, len1
;
1411 unsigned int ret
= 0;
1413 b1
= le32_to_cpu(newext
->ee_block
);
1414 len1
= ext4_ext_get_actual_len(newext
);
1415 depth
= ext_depth(inode
);
1416 if (!path
[depth
].p_ext
)
1418 b2
= le32_to_cpu(path
[depth
].p_ext
->ee_block
);
1421 * get the next allocated block if the extent in the path
1422 * is before the requested block(s)
1425 b2
= ext4_ext_next_allocated_block(path
);
1426 if (b2
== EXT_MAX_BLOCK
)
1430 /* check for wrap through zero on extent logical start block*/
1431 if (b1
+ len1
< b1
) {
1432 len1
= EXT_MAX_BLOCK
- b1
;
1433 newext
->ee_len
= cpu_to_le16(len1
);
1437 /* check for overlap */
1438 if (b1
+ len1
> b2
) {
1439 newext
->ee_len
= cpu_to_le16(b2
- b1
);
1447 * ext4_ext_insert_extent:
1448 * tries to merge requsted extent into the existing extent or
1449 * inserts requested extent as new one into the tree,
1450 * creating new leaf in the no-space case.
1452 int ext4_ext_insert_extent(handle_t
*handle
, struct inode
*inode
,
1453 struct ext4_ext_path
*path
,
1454 struct ext4_extent
*newext
)
1456 struct ext4_extent_header
* eh
;
1457 struct ext4_extent
*ex
, *fex
;
1458 struct ext4_extent
*nearex
; /* nearest extent */
1459 struct ext4_ext_path
*npath
= NULL
;
1460 int depth
, len
, err
;
1462 unsigned uninitialized
= 0;
1464 BUG_ON(ext4_ext_get_actual_len(newext
) == 0);
1465 depth
= ext_depth(inode
);
1466 ex
= path
[depth
].p_ext
;
1467 BUG_ON(path
[depth
].p_hdr
== NULL
);
1469 /* try to insert block into found extent and return */
1470 if (ex
&& ext4_can_extents_be_merged(inode
, ex
, newext
)) {
1471 ext_debug("append %d block to %d:%d (from %llu)\n",
1472 ext4_ext_get_actual_len(newext
),
1473 le32_to_cpu(ex
->ee_block
),
1474 ext4_ext_get_actual_len(ex
), ext_pblock(ex
));
1475 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1480 * ext4_can_extents_be_merged should have checked that either
1481 * both extents are uninitialized, or both aren't. Thus we
1482 * need to check only one of them here.
1484 if (ext4_ext_is_uninitialized(ex
))
1486 ex
->ee_len
= cpu_to_le16(ext4_ext_get_actual_len(ex
)
1487 + ext4_ext_get_actual_len(newext
));
1489 ext4_ext_mark_uninitialized(ex
);
1490 eh
= path
[depth
].p_hdr
;
1496 depth
= ext_depth(inode
);
1497 eh
= path
[depth
].p_hdr
;
1498 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
))
1501 /* probably next leaf has space for us? */
1502 fex
= EXT_LAST_EXTENT(eh
);
1503 next
= ext4_ext_next_leaf_block(inode
, path
);
1504 if (le32_to_cpu(newext
->ee_block
) > le32_to_cpu(fex
->ee_block
)
1505 && next
!= EXT_MAX_BLOCK
) {
1506 ext_debug("next leaf block - %d\n", next
);
1507 BUG_ON(npath
!= NULL
);
1508 npath
= ext4_ext_find_extent(inode
, next
, NULL
);
1510 return PTR_ERR(npath
);
1511 BUG_ON(npath
->p_depth
!= path
->p_depth
);
1512 eh
= npath
[depth
].p_hdr
;
1513 if (le16_to_cpu(eh
->eh_entries
) < le16_to_cpu(eh
->eh_max
)) {
1514 ext_debug("next leaf isnt full(%d)\n",
1515 le16_to_cpu(eh
->eh_entries
));
1519 ext_debug("next leaf has no free space(%d,%d)\n",
1520 le16_to_cpu(eh
->eh_entries
), le16_to_cpu(eh
->eh_max
));
1524 * There is no free space in the found leaf.
1525 * We're gonna add a new leaf in the tree.
1527 err
= ext4_ext_create_new_leaf(handle
, inode
, path
, newext
);
1530 depth
= ext_depth(inode
);
1531 eh
= path
[depth
].p_hdr
;
1534 nearex
= path
[depth
].p_ext
;
1536 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1541 /* there is no extent in this leaf, create first one */
1542 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1543 le32_to_cpu(newext
->ee_block
),
1545 ext4_ext_get_actual_len(newext
));
1546 path
[depth
].p_ext
= EXT_FIRST_EXTENT(eh
);
1547 } else if (le32_to_cpu(newext
->ee_block
)
1548 > le32_to_cpu(nearex
->ee_block
)) {
1549 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1550 if (nearex
!= EXT_LAST_EXTENT(eh
)) {
1551 len
= EXT_MAX_EXTENT(eh
) - nearex
;
1552 len
= (len
- 1) * sizeof(struct ext4_extent
);
1553 len
= len
< 0 ? 0 : len
;
1554 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1555 "move %d from 0x%p to 0x%p\n",
1556 le32_to_cpu(newext
->ee_block
),
1558 ext4_ext_get_actual_len(newext
),
1559 nearex
, len
, nearex
+ 1, nearex
+ 2);
1560 memmove(nearex
+ 2, nearex
+ 1, len
);
1562 path
[depth
].p_ext
= nearex
+ 1;
1564 BUG_ON(newext
->ee_block
== nearex
->ee_block
);
1565 len
= (EXT_MAX_EXTENT(eh
) - nearex
) * sizeof(struct ext4_extent
);
1566 len
= len
< 0 ? 0 : len
;
1567 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1568 "move %d from 0x%p to 0x%p\n",
1569 le32_to_cpu(newext
->ee_block
),
1571 ext4_ext_get_actual_len(newext
),
1572 nearex
, len
, nearex
+ 1, nearex
+ 2);
1573 memmove(nearex
+ 1, nearex
, len
);
1574 path
[depth
].p_ext
= nearex
;
1577 eh
->eh_entries
= cpu_to_le16(le16_to_cpu(eh
->eh_entries
)+1);
1578 nearex
= path
[depth
].p_ext
;
1579 nearex
->ee_block
= newext
->ee_block
;
1580 ext4_ext_store_pblock(nearex
, ext_pblock(newext
));
1581 nearex
->ee_len
= newext
->ee_len
;
1584 /* try to merge extents to the right */
1585 ext4_ext_try_to_merge(inode
, path
, nearex
);
1587 /* try to merge extents to the left */
1589 /* time to correct all indexes above */
1590 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1594 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1598 ext4_ext_drop_refs(npath
);
1601 ext4_ext_tree_changed(inode
);
1602 ext4_ext_invalidate_cache(inode
);
1607 ext4_ext_put_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1608 __u32 len
, ext4_fsblk_t start
, int type
)
1610 struct ext4_ext_cache
*cex
;
1612 cex
= &EXT4_I(inode
)->i_cached_extent
;
1613 cex
->ec_type
= type
;
1614 cex
->ec_block
= block
;
1616 cex
->ec_start
= start
;
1620 * ext4_ext_put_gap_in_cache:
1621 * calculate boundaries of the gap that the requested block fits into
1622 * and cache this gap
1625 ext4_ext_put_gap_in_cache(struct inode
*inode
, struct ext4_ext_path
*path
,
1628 int depth
= ext_depth(inode
);
1631 struct ext4_extent
*ex
;
1633 ex
= path
[depth
].p_ext
;
1635 /* there is no extent yet, so gap is [0;-] */
1637 len
= EXT_MAX_BLOCK
;
1638 ext_debug("cache gap(whole file):");
1639 } else if (block
< le32_to_cpu(ex
->ee_block
)) {
1641 len
= le32_to_cpu(ex
->ee_block
) - block
;
1642 ext_debug("cache gap(before): %u [%u:%u]",
1644 le32_to_cpu(ex
->ee_block
),
1645 ext4_ext_get_actual_len(ex
));
1646 } else if (block
>= le32_to_cpu(ex
->ee_block
)
1647 + ext4_ext_get_actual_len(ex
)) {
1649 lblock
= le32_to_cpu(ex
->ee_block
)
1650 + ext4_ext_get_actual_len(ex
);
1652 next
= ext4_ext_next_allocated_block(path
);
1653 ext_debug("cache gap(after): [%u:%u] %u",
1654 le32_to_cpu(ex
->ee_block
),
1655 ext4_ext_get_actual_len(ex
),
1657 BUG_ON(next
== lblock
);
1658 len
= next
- lblock
;
1664 ext_debug(" -> %u:%lu\n", lblock
, len
);
1665 ext4_ext_put_in_cache(inode
, lblock
, len
, 0, EXT4_EXT_CACHE_GAP
);
1669 ext4_ext_in_cache(struct inode
*inode
, ext4_lblk_t block
,
1670 struct ext4_extent
*ex
)
1672 struct ext4_ext_cache
*cex
;
1674 cex
= &EXT4_I(inode
)->i_cached_extent
;
1676 /* has cache valid data? */
1677 if (cex
->ec_type
== EXT4_EXT_CACHE_NO
)
1678 return EXT4_EXT_CACHE_NO
;
1680 BUG_ON(cex
->ec_type
!= EXT4_EXT_CACHE_GAP
&&
1681 cex
->ec_type
!= EXT4_EXT_CACHE_EXTENT
);
1682 if (block
>= cex
->ec_block
&& block
< cex
->ec_block
+ cex
->ec_len
) {
1683 ex
->ee_block
= cpu_to_le32(cex
->ec_block
);
1684 ext4_ext_store_pblock(ex
, cex
->ec_start
);
1685 ex
->ee_len
= cpu_to_le16(cex
->ec_len
);
1686 ext_debug("%u cached by %u:%u:%llu\n",
1688 cex
->ec_block
, cex
->ec_len
, cex
->ec_start
);
1689 return cex
->ec_type
;
1693 return EXT4_EXT_CACHE_NO
;
1698 * removes index from the index block.
1699 * It's used in truncate case only, thus all requests are for
1700 * last index in the block only.
1702 static int ext4_ext_rm_idx(handle_t
*handle
, struct inode
*inode
,
1703 struct ext4_ext_path
*path
)
1705 struct buffer_head
*bh
;
1709 /* free index block */
1711 leaf
= idx_pblock(path
->p_idx
);
1712 BUG_ON(path
->p_hdr
->eh_entries
== 0);
1713 err
= ext4_ext_get_access(handle
, inode
, path
);
1716 path
->p_hdr
->eh_entries
= cpu_to_le16(le16_to_cpu(path
->p_hdr
->eh_entries
)-1);
1717 err
= ext4_ext_dirty(handle
, inode
, path
);
1720 ext_debug("index is empty, remove it, free block %llu\n", leaf
);
1721 bh
= sb_find_get_block(inode
->i_sb
, leaf
);
1722 ext4_forget(handle
, 1, inode
, bh
, leaf
);
1723 ext4_free_blocks(handle
, inode
, leaf
, 1, 1);
1728 * ext4_ext_calc_credits_for_insert:
1729 * This routine returns max. credits that the extent tree can consume.
1730 * It should be OK for low-performance paths like ->writepage()
1731 * To allow many writing processes to fit into a single transaction,
1732 * the caller should calculate credits under i_data_sem and
1733 * pass the actual path.
1735 int ext4_ext_calc_credits_for_insert(struct inode
*inode
,
1736 struct ext4_ext_path
*path
)
1741 /* probably there is space in leaf? */
1742 depth
= ext_depth(inode
);
1743 if (le16_to_cpu(path
[depth
].p_hdr
->eh_entries
)
1744 < le16_to_cpu(path
[depth
].p_hdr
->eh_max
))
1749 * given 32-bit logical block (4294967296 blocks), max. tree
1750 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1751 * Let's also add one more level for imbalance.
1755 /* allocation of new data block(s) */
1759 * tree can be full, so it would need to grow in depth:
1760 * we need one credit to modify old root, credits for
1761 * new root will be added in split accounting
1766 * Index split can happen, we would need:
1767 * allocate intermediate indexes (bitmap + group)
1768 * + change two blocks at each level, but root (already included)
1770 needed
+= (depth
* 2) + (depth
* 2);
1772 /* any allocation modifies superblock */
1778 static int ext4_remove_blocks(handle_t
*handle
, struct inode
*inode
,
1779 struct ext4_extent
*ex
,
1780 ext4_lblk_t from
, ext4_lblk_t to
)
1782 struct buffer_head
*bh
;
1783 unsigned short ee_len
= ext4_ext_get_actual_len(ex
);
1784 int i
, metadata
= 0;
1786 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
1788 #ifdef EXTENTS_STATS
1790 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
1791 spin_lock(&sbi
->s_ext_stats_lock
);
1792 sbi
->s_ext_blocks
+= ee_len
;
1793 sbi
->s_ext_extents
++;
1794 if (ee_len
< sbi
->s_ext_min
)
1795 sbi
->s_ext_min
= ee_len
;
1796 if (ee_len
> sbi
->s_ext_max
)
1797 sbi
->s_ext_max
= ee_len
;
1798 if (ext_depth(inode
) > sbi
->s_depth_max
)
1799 sbi
->s_depth_max
= ext_depth(inode
);
1800 spin_unlock(&sbi
->s_ext_stats_lock
);
1803 if (from
>= le32_to_cpu(ex
->ee_block
)
1804 && to
== le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
1809 num
= le32_to_cpu(ex
->ee_block
) + ee_len
- from
;
1810 start
= ext_pblock(ex
) + ee_len
- num
;
1811 ext_debug("free last %u blocks starting %llu\n", num
, start
);
1812 for (i
= 0; i
< num
; i
++) {
1813 bh
= sb_find_get_block(inode
->i_sb
, start
+ i
);
1814 ext4_forget(handle
, 0, inode
, bh
, start
+ i
);
1816 ext4_free_blocks(handle
, inode
, start
, num
, metadata
);
1817 } else if (from
== le32_to_cpu(ex
->ee_block
)
1818 && to
<= le32_to_cpu(ex
->ee_block
) + ee_len
- 1) {
1819 printk(KERN_INFO
"strange request: removal %u-%u from %u:%u\n",
1820 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
1822 printk(KERN_INFO
"strange request: removal(2) "
1823 "%u-%u from %u:%u\n",
1824 from
, to
, le32_to_cpu(ex
->ee_block
), ee_len
);
1830 ext4_ext_rm_leaf(handle_t
*handle
, struct inode
*inode
,
1831 struct ext4_ext_path
*path
, ext4_lblk_t start
)
1833 int err
= 0, correct_index
= 0;
1834 int depth
= ext_depth(inode
), credits
;
1835 struct ext4_extent_header
*eh
;
1836 ext4_lblk_t a
, b
, block
;
1838 ext4_lblk_t ex_ee_block
;
1839 unsigned short ex_ee_len
;
1840 unsigned uninitialized
= 0;
1841 struct ext4_extent
*ex
;
1843 /* the header must be checked already in ext4_ext_remove_space() */
1844 ext_debug("truncate since %u in leaf\n", start
);
1845 if (!path
[depth
].p_hdr
)
1846 path
[depth
].p_hdr
= ext_block_hdr(path
[depth
].p_bh
);
1847 eh
= path
[depth
].p_hdr
;
1850 /* find where to start removing */
1851 ex
= EXT_LAST_EXTENT(eh
);
1853 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
1854 if (ext4_ext_is_uninitialized(ex
))
1856 ex_ee_len
= ext4_ext_get_actual_len(ex
);
1858 while (ex
>= EXT_FIRST_EXTENT(eh
) &&
1859 ex_ee_block
+ ex_ee_len
> start
) {
1860 ext_debug("remove ext %lu:%u\n", ex_ee_block
, ex_ee_len
);
1861 path
[depth
].p_ext
= ex
;
1863 a
= ex_ee_block
> start
? ex_ee_block
: start
;
1864 b
= ex_ee_block
+ ex_ee_len
- 1 < EXT_MAX_BLOCK
?
1865 ex_ee_block
+ ex_ee_len
- 1 : EXT_MAX_BLOCK
;
1867 ext_debug(" border %u:%u\n", a
, b
);
1869 if (a
!= ex_ee_block
&& b
!= ex_ee_block
+ ex_ee_len
- 1) {
1873 } else if (a
!= ex_ee_block
) {
1874 /* remove tail of the extent */
1875 block
= ex_ee_block
;
1877 } else if (b
!= ex_ee_block
+ ex_ee_len
- 1) {
1878 /* remove head of the extent */
1881 /* there is no "make a hole" API yet */
1884 /* remove whole extent: excellent! */
1885 block
= ex_ee_block
;
1887 BUG_ON(a
!= ex_ee_block
);
1888 BUG_ON(b
!= ex_ee_block
+ ex_ee_len
- 1);
1891 /* at present, extent can't cross block group: */
1892 /* leaf + bitmap + group desc + sb + inode */
1894 if (ex
== EXT_FIRST_EXTENT(eh
)) {
1896 credits
+= (ext_depth(inode
)) + 1;
1899 credits
+= 2 * EXT4_QUOTA_TRANS_BLOCKS(inode
->i_sb
);
1902 handle
= ext4_ext_journal_restart(handle
, credits
);
1903 if (IS_ERR(handle
)) {
1904 err
= PTR_ERR(handle
);
1908 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
1912 err
= ext4_remove_blocks(handle
, inode
, ex
, a
, b
);
1917 /* this extent is removed; mark slot entirely unused */
1918 ext4_ext_store_pblock(ex
, 0);
1919 eh
->eh_entries
= cpu_to_le16(le16_to_cpu(eh
->eh_entries
)-1);
1922 ex
->ee_block
= cpu_to_le32(block
);
1923 ex
->ee_len
= cpu_to_le16(num
);
1925 * Do not mark uninitialized if all the blocks in the
1926 * extent have been removed.
1928 if (uninitialized
&& num
)
1929 ext4_ext_mark_uninitialized(ex
);
1931 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
1935 ext_debug("new extent: %u:%u:%llu\n", block
, num
,
1938 ex_ee_block
= le32_to_cpu(ex
->ee_block
);
1939 ex_ee_len
= ext4_ext_get_actual_len(ex
);
1942 if (correct_index
&& eh
->eh_entries
)
1943 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
1945 /* if this leaf is free, then we should
1946 * remove it from index block above */
1947 if (err
== 0 && eh
->eh_entries
== 0 && path
[depth
].p_bh
!= NULL
)
1948 err
= ext4_ext_rm_idx(handle
, inode
, path
+ depth
);
1955 * ext4_ext_more_to_rm:
1956 * returns 1 if current index has to be freed (even partial)
1959 ext4_ext_more_to_rm(struct ext4_ext_path
*path
)
1961 BUG_ON(path
->p_idx
== NULL
);
1963 if (path
->p_idx
< EXT_FIRST_INDEX(path
->p_hdr
))
1967 * if truncate on deeper level happened, it wasn't partial,
1968 * so we have to consider current index for truncation
1970 if (le16_to_cpu(path
->p_hdr
->eh_entries
) == path
->p_block
)
1975 static int ext4_ext_remove_space(struct inode
*inode
, ext4_lblk_t start
)
1977 struct super_block
*sb
= inode
->i_sb
;
1978 int depth
= ext_depth(inode
);
1979 struct ext4_ext_path
*path
;
1983 ext_debug("truncate since %u\n", start
);
1985 /* probably first extent we're gonna free will be last in block */
1986 handle
= ext4_journal_start(inode
, depth
+ 1);
1988 return PTR_ERR(handle
);
1990 ext4_ext_invalidate_cache(inode
);
1993 * We start scanning from right side, freeing all the blocks
1994 * after i_size and walking into the tree depth-wise.
1996 path
= kzalloc(sizeof(struct ext4_ext_path
) * (depth
+ 1), GFP_KERNEL
);
1998 ext4_journal_stop(handle
);
2001 path
[0].p_hdr
= ext_inode_hdr(inode
);
2002 if (ext4_ext_check_header(inode
, path
[0].p_hdr
, depth
)) {
2006 path
[0].p_depth
= depth
;
2008 while (i
>= 0 && err
== 0) {
2010 /* this is leaf block */
2011 err
= ext4_ext_rm_leaf(handle
, inode
, path
, start
);
2012 /* root level has p_bh == NULL, brelse() eats this */
2013 brelse(path
[i
].p_bh
);
2014 path
[i
].p_bh
= NULL
;
2019 /* this is index block */
2020 if (!path
[i
].p_hdr
) {
2021 ext_debug("initialize header\n");
2022 path
[i
].p_hdr
= ext_block_hdr(path
[i
].p_bh
);
2025 if (!path
[i
].p_idx
) {
2026 /* this level hasn't been touched yet */
2027 path
[i
].p_idx
= EXT_LAST_INDEX(path
[i
].p_hdr
);
2028 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
)+1;
2029 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2031 le16_to_cpu(path
[i
].p_hdr
->eh_entries
));
2033 /* we were already here, see at next index */
2037 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2038 i
, EXT_FIRST_INDEX(path
[i
].p_hdr
),
2040 if (ext4_ext_more_to_rm(path
+ i
)) {
2041 struct buffer_head
*bh
;
2042 /* go to the next level */
2043 ext_debug("move to level %d (block %llu)\n",
2044 i
+ 1, idx_pblock(path
[i
].p_idx
));
2045 memset(path
+ i
+ 1, 0, sizeof(*path
));
2046 bh
= sb_bread(sb
, idx_pblock(path
[i
].p_idx
));
2048 /* should we reset i_size? */
2052 if (WARN_ON(i
+ 1 > depth
)) {
2056 if (ext4_ext_check_header(inode
, ext_block_hdr(bh
),
2061 path
[i
+ 1].p_bh
= bh
;
2063 /* save actual number of indexes since this
2064 * number is changed at the next iteration */
2065 path
[i
].p_block
= le16_to_cpu(path
[i
].p_hdr
->eh_entries
);
2068 /* we finished processing this index, go up */
2069 if (path
[i
].p_hdr
->eh_entries
== 0 && i
> 0) {
2070 /* index is empty, remove it;
2071 * handle must be already prepared by the
2072 * truncatei_leaf() */
2073 err
= ext4_ext_rm_idx(handle
, inode
, path
+ i
);
2075 /* root level has p_bh == NULL, brelse() eats this */
2076 brelse(path
[i
].p_bh
);
2077 path
[i
].p_bh
= NULL
;
2079 ext_debug("return to level %d\n", i
);
2083 /* TODO: flexible tree reduction should be here */
2084 if (path
->p_hdr
->eh_entries
== 0) {
2086 * truncate to zero freed all the tree,
2087 * so we need to correct eh_depth
2089 err
= ext4_ext_get_access(handle
, inode
, path
);
2091 ext_inode_hdr(inode
)->eh_depth
= 0;
2092 ext_inode_hdr(inode
)->eh_max
=
2093 cpu_to_le16(ext4_ext_space_root(inode
));
2094 err
= ext4_ext_dirty(handle
, inode
, path
);
2098 ext4_ext_tree_changed(inode
);
2099 ext4_ext_drop_refs(path
);
2101 ext4_journal_stop(handle
);
2107 * called at mount time
2109 void ext4_ext_init(struct super_block
*sb
)
2112 * possible initialization would be here
2115 if (test_opt(sb
, EXTENTS
)) {
2116 printk("EXT4-fs: file extents enabled");
2117 #ifdef AGGRESSIVE_TEST
2118 printk(", aggressive tests");
2120 #ifdef CHECK_BINSEARCH
2121 printk(", check binsearch");
2123 #ifdef EXTENTS_STATS
2127 #ifdef EXTENTS_STATS
2128 spin_lock_init(&EXT4_SB(sb
)->s_ext_stats_lock
);
2129 EXT4_SB(sb
)->s_ext_min
= 1 << 30;
2130 EXT4_SB(sb
)->s_ext_max
= 0;
2136 * called at umount time
2138 void ext4_ext_release(struct super_block
*sb
)
2140 if (!test_opt(sb
, EXTENTS
))
2143 #ifdef EXTENTS_STATS
2144 if (EXT4_SB(sb
)->s_ext_blocks
&& EXT4_SB(sb
)->s_ext_extents
) {
2145 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2146 printk(KERN_ERR
"EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2147 sbi
->s_ext_blocks
, sbi
->s_ext_extents
,
2148 sbi
->s_ext_blocks
/ sbi
->s_ext_extents
);
2149 printk(KERN_ERR
"EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2150 sbi
->s_ext_min
, sbi
->s_ext_max
, sbi
->s_depth_max
);
2156 * This function is called by ext4_ext_get_blocks() if someone tries to write
2157 * to an uninitialized extent. It may result in splitting the uninitialized
2158 * extent into multiple extents (upto three - one initialized and two
2160 * There are three possibilities:
2161 * a> There is no split required: Entire extent should be initialized
2162 * b> Splits in two extents: Write is happening at either end of the extent
2163 * c> Splits in three extents: Somone is writing in middle of the extent
2165 static int ext4_ext_convert_to_initialized(handle_t
*handle
,
2166 struct inode
*inode
,
2167 struct ext4_ext_path
*path
,
2169 unsigned long max_blocks
)
2171 struct ext4_extent
*ex
, newex
;
2172 struct ext4_extent
*ex1
= NULL
;
2173 struct ext4_extent
*ex2
= NULL
;
2174 struct ext4_extent
*ex3
= NULL
;
2175 struct ext4_extent_header
*eh
;
2176 ext4_lblk_t ee_block
;
2177 unsigned int allocated
, ee_len
, depth
;
2178 ext4_fsblk_t newblock
;
2182 depth
= ext_depth(inode
);
2183 eh
= path
[depth
].p_hdr
;
2184 ex
= path
[depth
].p_ext
;
2185 ee_block
= le32_to_cpu(ex
->ee_block
);
2186 ee_len
= ext4_ext_get_actual_len(ex
);
2187 allocated
= ee_len
- (iblock
- ee_block
);
2188 newblock
= iblock
- ee_block
+ ext_pblock(ex
);
2191 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2193 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2197 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2198 /* ex1: ee_block to iblock - 1 : uninitialized */
2199 if (iblock
> ee_block
) {
2201 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2202 ext4_ext_mark_uninitialized(ex1
);
2206 * for sanity, update the length of the ex2 extent before
2207 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2208 * overlap of blocks.
2210 if (!ex1
&& allocated
> max_blocks
)
2211 ex2
->ee_len
= cpu_to_le16(max_blocks
);
2212 /* ex3: to ee_block + ee_len : uninitialised */
2213 if (allocated
> max_blocks
) {
2214 unsigned int newdepth
;
2216 ex3
->ee_block
= cpu_to_le32(iblock
+ max_blocks
);
2217 ext4_ext_store_pblock(ex3
, newblock
+ max_blocks
);
2218 ex3
->ee_len
= cpu_to_le16(allocated
- max_blocks
);
2219 ext4_ext_mark_uninitialized(ex3
);
2220 err
= ext4_ext_insert_extent(handle
, inode
, path
, ex3
);
2224 * The depth, and hence eh & ex might change
2225 * as part of the insert above.
2227 newdepth
= ext_depth(inode
);
2228 if (newdepth
!= depth
) {
2230 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2231 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
2233 ext4_ext_drop_refs(path
);
2234 path
= ext4_ext_find_extent(inode
, iblock
, path
);
2235 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2237 err
= PTR_ERR(path
);
2238 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2241 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2244 eh
= path
[depth
].p_hdr
;
2245 ex
= path
[depth
].p_ext
;
2248 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2251 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2254 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2256 allocated
= max_blocks
;
2259 * If there was a change of depth as part of the
2260 * insertion of ex3 above, we need to update the length
2261 * of the ex1 extent again here
2263 if (ex1
&& ex1
!= ex
) {
2265 ex1
->ee_len
= cpu_to_le16(iblock
- ee_block
);
2266 ext4_ext_mark_uninitialized(ex1
);
2269 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2270 ex2
->ee_block
= cpu_to_le32(iblock
);
2271 ext4_ext_store_pblock(ex2
, newblock
);
2272 ex2
->ee_len
= cpu_to_le16(allocated
);
2275 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2276 err
= ext4_ext_get_access(handle
, inode
, path
+ depth
);
2280 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2282 * New (initialized) extent starts from the first block
2283 * in the current extent. i.e., ex2 == ex
2284 * We have to see if it can be merged with the extent
2287 if (ex2
> EXT_FIRST_EXTENT(eh
)) {
2289 * To merge left, pass "ex2 - 1" to try_to_merge(),
2290 * since it merges towards right _only_.
2292 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
- 1);
2294 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2297 depth
= ext_depth(inode
);
2302 * Try to Merge towards right. This might be required
2303 * only when the whole extent is being written to.
2304 * i.e. ex2 == ex and ex3 == NULL.
2307 ret
= ext4_ext_try_to_merge(inode
, path
, ex2
);
2309 err
= ext4_ext_correct_indexes(handle
, inode
, path
);
2314 /* Mark modified extent as dirty */
2315 err
= ext4_ext_dirty(handle
, inode
, path
+ depth
);
2318 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2320 return err
? err
: allocated
;
2324 <<<<<<< HEAD:fs/ext4/extents.c
2326 * Block allocation/map/preallocation routine for extents based files
2329 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2330 * Need to be called with
2331 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2332 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2333 <<<<<<< HEAD:fs/ext4/extents.c
2336 * return > 0, number of of blocks already mapped/allocated
2337 * if create == 0 and these are pre-allocated blocks
2338 * buffer head is unmapped
2339 * otherwise blocks are mapped
2341 * return = 0, if plain look up failed (blocks have not been allocated)
2342 * buffer head is unmapped
2344 * return < 0, error case.
2345 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2347 int ext4_ext_get_blocks(handle_t
*handle
, struct inode
*inode
,
2349 unsigned long max_blocks
, struct buffer_head
*bh_result
,
2350 int create
, int extend_disksize
)
2352 struct ext4_ext_path
*path
= NULL
;
2353 struct ext4_extent_header
*eh
;
2354 struct ext4_extent newex
, *ex
;
2355 ext4_fsblk_t goal
, newblock
;
2356 int err
= 0, depth
, ret
;
2357 unsigned long allocated
= 0;
2358 struct ext4_allocation_request ar
;
2360 __clear_bit(BH_New
, &bh_result
->b_state
);
2361 ext_debug("blocks %u/%lu requested for inode %u\n",
2362 iblock
, max_blocks
, inode
->i_ino
);
2364 /* check in cache */
2365 goal
= ext4_ext_in_cache(inode
, iblock
, &newex
);
2367 if (goal
== EXT4_EXT_CACHE_GAP
) {
2370 * block isn't allocated yet and
2371 * user doesn't want to allocate it
2375 /* we should allocate requested block */
2376 } else if (goal
== EXT4_EXT_CACHE_EXTENT
) {
2377 /* block is already allocated */
2379 - le32_to_cpu(newex
.ee_block
)
2380 + ext_pblock(&newex
);
2381 /* number of remaining blocks in the extent */
2382 allocated
= ext4_ext_get_actual_len(&newex
) -
2383 (iblock
- le32_to_cpu(newex
.ee_block
));
2390 /* find extent for this block */
2391 path
= ext4_ext_find_extent(inode
, iblock
, NULL
);
2393 err
= PTR_ERR(path
);
2398 depth
= ext_depth(inode
);
2401 * consistent leaf must not be empty;
2402 * this situation is possible, though, _during_ tree modification;
2403 * this is why assert can't be put in ext4_ext_find_extent()
2405 BUG_ON(path
[depth
].p_ext
== NULL
&& depth
!= 0);
2406 eh
= path
[depth
].p_hdr
;
2408 ex
= path
[depth
].p_ext
;
2410 ext4_lblk_t ee_block
= le32_to_cpu(ex
->ee_block
);
2411 ext4_fsblk_t ee_start
= ext_pblock(ex
);
2412 unsigned short ee_len
;
2415 * Uninitialized extents are treated as holes, except that
2416 * we split out initialized portions during a write.
2418 ee_len
= ext4_ext_get_actual_len(ex
);
2419 /* if found extent covers block, simply return it */
2420 if (iblock
>= ee_block
&& iblock
< ee_block
+ ee_len
) {
2421 newblock
= iblock
- ee_block
+ ee_start
;
2422 /* number of remaining blocks in the extent */
2423 allocated
= ee_len
- (iblock
- ee_block
);
2424 ext_debug("%u fit into %lu:%d -> %llu\n", iblock
,
2425 ee_block
, ee_len
, newblock
);
2427 /* Do not put uninitialized extent in the cache */
2428 if (!ext4_ext_is_uninitialized(ex
)) {
2429 ext4_ext_put_in_cache(inode
, ee_block
,
2431 EXT4_EXT_CACHE_EXTENT
);
2434 if (create
== EXT4_CREATE_UNINITIALIZED_EXT
)
2439 ret
= ext4_ext_convert_to_initialized(handle
, inode
,
2452 * requested block isn't allocated yet;
2453 * we couldn't try to create block if create flag is zero
2457 * put just found gap into cache to speed up
2458 * subsequent requests
2460 ext4_ext_put_gap_in_cache(inode
, path
, iblock
);
2464 * Okay, we need to do block allocation. Lazily initialize the block
2465 * allocation info here if necessary.
2467 if (S_ISREG(inode
->i_mode
) && (!EXT4_I(inode
)->i_block_alloc_info
))
2468 ext4_init_block_alloc_info(inode
);
2470 /* find neighbour allocated blocks */
2472 err
= ext4_ext_search_left(inode
, path
, &ar
.lleft
, &ar
.pleft
);
2476 err
= ext4_ext_search_right(inode
, path
, &ar
.lright
, &ar
.pright
);
2481 * See if request is beyond maximum number of blocks we can have in
2482 * a single extent. For an initialized extent this limit is
2483 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2484 * EXT_UNINIT_MAX_LEN.
2486 if (max_blocks
> EXT_INIT_MAX_LEN
&&
2487 create
!= EXT4_CREATE_UNINITIALIZED_EXT
)
2488 max_blocks
= EXT_INIT_MAX_LEN
;
2489 else if (max_blocks
> EXT_UNINIT_MAX_LEN
&&
2490 create
== EXT4_CREATE_UNINITIALIZED_EXT
)
2491 max_blocks
= EXT_UNINIT_MAX_LEN
;
2493 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2494 newex
.ee_block
= cpu_to_le32(iblock
);
2495 newex
.ee_len
= cpu_to_le16(max_blocks
);
2496 err
= ext4_ext_check_overlap(inode
, &newex
, path
);
2498 allocated
= ext4_ext_get_actual_len(&newex
);
2500 allocated
= max_blocks
;
2502 /* allocate new block */
2504 ar
.goal
= ext4_ext_find_goal(inode
, path
, iblock
);
2505 ar
.logical
= iblock
;
2507 if (S_ISREG(inode
->i_mode
))
2508 ar
.flags
= EXT4_MB_HINT_DATA
;
2510 /* disable in-core preallocation for non-regular files */
2512 newblock
= ext4_mb_new_blocks(handle
, &ar
, &err
);
2515 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2516 goal
, newblock
, allocated
);
2518 /* try to insert new extent into found leaf and return */
2519 ext4_ext_store_pblock(&newex
, newblock
);
2520 newex
.ee_len
= cpu_to_le16(ar
.len
);
2521 if (create
== EXT4_CREATE_UNINITIALIZED_EXT
) /* Mark uninitialized */
2522 ext4_ext_mark_uninitialized(&newex
);
2523 err
= ext4_ext_insert_extent(handle
, inode
, path
, &newex
);
2525 /* free data blocks we just allocated */
2526 /* not a good idea to call discard here directly,
2527 * but otherwise we'd need to call it every free() */
2528 ext4_mb_discard_inode_preallocations(inode
);
2529 ext4_free_blocks(handle
, inode
, ext_pblock(&newex
),
2530 ext4_ext_get_actual_len(&newex
), 0);
2534 if (extend_disksize
&& inode
->i_size
> EXT4_I(inode
)->i_disksize
)
2535 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2537 /* previous routine could use block we allocated */
2538 newblock
= ext_pblock(&newex
);
2539 allocated
= ext4_ext_get_actual_len(&newex
);
2541 __set_bit(BH_New
, &bh_result
->b_state
);
2543 /* Cache only when it is _not_ an uninitialized extent */
2544 if (create
!= EXT4_CREATE_UNINITIALIZED_EXT
)
2545 ext4_ext_put_in_cache(inode
, iblock
, allocated
, newblock
,
2546 EXT4_EXT_CACHE_EXTENT
);
2548 if (allocated
> max_blocks
)
2549 allocated
= max_blocks
;
2550 ext4_ext_show_leaf(inode
, path
);
2551 __set_bit(BH_Mapped
, &bh_result
->b_state
);
2552 bh_result
->b_bdev
= inode
->i_sb
->s_bdev
;
2553 bh_result
->b_blocknr
= newblock
;
2556 ext4_ext_drop_refs(path
);
2559 return err
? err
: allocated
;
2562 void ext4_ext_truncate(struct inode
* inode
, struct page
*page
)
2564 struct address_space
*mapping
= inode
->i_mapping
;
2565 struct super_block
*sb
= inode
->i_sb
;
2566 ext4_lblk_t last_block
;
2571 * probably first extent we're gonna free will be last in block
2573 err
= ext4_writepage_trans_blocks(inode
) + 3;
2574 handle
= ext4_journal_start(inode
, err
);
2575 if (IS_ERR(handle
)) {
2577 clear_highpage(page
);
2578 flush_dcache_page(page
);
2580 page_cache_release(page
);
2586 ext4_block_truncate_page(handle
, page
, mapping
, inode
->i_size
);
2588 down_write(&EXT4_I(inode
)->i_data_sem
);
2589 ext4_ext_invalidate_cache(inode
);
2591 ext4_mb_discard_inode_preallocations(inode
);
2594 * TODO: optimization is possible here.
2595 * Probably we need not scan at all,
2596 * because page truncation is enough.
2598 if (ext4_orphan_add(handle
, inode
))
2601 /* we have to know where to truncate from in crash case */
2602 EXT4_I(inode
)->i_disksize
= inode
->i_size
;
2603 ext4_mark_inode_dirty(handle
, inode
);
2605 last_block
= (inode
->i_size
+ sb
->s_blocksize
- 1)
2606 >> EXT4_BLOCK_SIZE_BITS(sb
);
2607 err
= ext4_ext_remove_space(inode
, last_block
);
2609 /* In a multi-transaction truncate, we only make the final
2610 * transaction synchronous.
2617 * If this was a simple ftruncate() and the file will remain alive,
2618 * then we need to clear up the orphan record which we created above.
2619 * However, if this was a real unlink then we were called by
2620 * ext4_delete_inode(), and we allow that function to clean up the
2621 * orphan info for us.
2624 ext4_orphan_del(handle
, inode
);
2626 up_write(&EXT4_I(inode
)->i_data_sem
);
2627 ext4_journal_stop(handle
);
2631 * ext4_ext_writepage_trans_blocks:
2632 * calculate max number of blocks we could modify
2633 * in order to allocate new block for an inode
2635 int ext4_ext_writepage_trans_blocks(struct inode
*inode
, int num
)
2639 needed
= ext4_ext_calc_credits_for_insert(inode
, NULL
);
2641 /* caller wants to allocate num blocks, but note it includes sb */
2642 needed
= needed
* num
- (num
- 1);
2645 needed
+= 2 * EXT4_QUOTA_TRANS_BLOCKS(inode
->i_sb
);
2652 * preallocate space for a file. This implements ext4's fallocate inode
2653 * operation, which gets called from sys_fallocate system call.
2654 * For block-mapped files, posix_fallocate should fall back to the method
2655 * of writing zeroes to the required new blocks (the same behavior which is
2656 * expected for file systems which do not support fallocate() system call).
2658 long ext4_fallocate(struct inode
*inode
, int mode
, loff_t offset
, loff_t len
)
2662 unsigned long max_blocks
;
2663 ext4_fsblk_t nblocks
= 0;
2667 struct buffer_head map_bh
;
2668 unsigned int credits
, blkbits
= inode
->i_blkbits
;
2671 * currently supporting (pre)allocate mode for extent-based
2674 if (!(EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
2677 /* preallocation to directories is currently not supported */
2678 if (S_ISDIR(inode
->i_mode
))
2681 block
= offset
>> blkbits
;
2682 max_blocks
= (EXT4_BLOCK_ALIGN(len
+ offset
, blkbits
) >> blkbits
)
2686 * credits to insert 1 extent into extent tree + buffers to be able to
2687 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2689 credits
= EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
) + 3;
2690 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2691 down_write((&EXT4_I(inode
)->i_data_sem
));
2693 mutex_lock(&inode
->i_mutex
);
2694 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2696 while (ret
>= 0 && ret
< max_blocks
) {
2697 block
= block
+ ret
;
2698 max_blocks
= max_blocks
- ret
;
2699 handle
= ext4_journal_start(inode
, credits
);
2700 if (IS_ERR(handle
)) {
2701 ret
= PTR_ERR(handle
);
2705 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2706 ret
= ext4_ext_get_blocks(handle
, inode
, block
,
2708 ret
= ext4_get_blocks_wrap(handle
, inode
, block
,
2709 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2710 max_blocks
, &map_bh
,
2711 EXT4_CREATE_UNINITIALIZED_EXT
, 0);
2712 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2715 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2717 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2718 ext4_error(inode
->i_sb
, "ext4_fallocate",
2719 "ext4_ext_get_blocks returned error: "
2720 "inode#%lu, block=%u, max_blocks=%lu",
2724 printk(KERN_ERR
"%s: ext4_ext_get_blocks "
2725 "returned error inode#%lu, block=%u, "
2726 "max_blocks=%lu", __func__
,
2727 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2728 inode
->i_ino
, block
, max_blocks
);
2729 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2733 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2734 ext4_mark_inode_dirty(handle
, inode
);
2735 ret2
= ext4_journal_stop(handle
);
2739 /* check wrap through sign-bit/zero here */
2740 if ((block
+ ret
) < 0 || (block
+ ret
) < block
) {
2742 ext4_mark_inode_dirty(handle
, inode
);
2743 ret2
= ext4_journal_stop(handle
);
2746 if (buffer_new(&map_bh
) && ((block
+ ret
) >
2747 (EXT4_BLOCK_ALIGN(i_size_read(inode
), blkbits
)
2749 nblocks
= nblocks
+ ret
;
2752 /* Update ctime if new blocks get allocated */
2754 struct timespec now
;
2756 now
= current_fs_time(inode
->i_sb
);
2757 if (!timespec_equal(&inode
->i_ctime
, &now
))
2758 inode
->i_ctime
= now
;
2761 ext4_mark_inode_dirty(handle
, inode
);
2762 ret2
= ext4_journal_stop(handle
);
2767 if (ret
== -ENOSPC
&& ext4_should_retry_alloc(inode
->i_sb
, &retries
))
2770 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2771 up_write((&EXT4_I(inode
)->i_data_sem
));
2773 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2775 * Time to update the file size.
2776 * Update only when preallocation was requested beyond the file size.
2778 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
2779 (offset
+ len
) > i_size_read(inode
)) {
2782 * if no error, we assume preallocation succeeded
2785 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2786 mutex_lock(&inode
->i_mutex
);
2788 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2789 i_size_write(inode
, offset
+ len
);
2790 EXT4_I(inode
)->i_disksize
= i_size_read(inode
);
2791 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2792 mutex_unlock(&inode
->i_mutex
);
2794 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2795 } else if (ret
< 0 && nblocks
) {
2796 /* Handle partial allocation scenario */
2799 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2800 mutex_lock(&inode
->i_mutex
);
2802 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2803 newsize
= (nblocks
<< blkbits
) + i_size_read(inode
);
2804 i_size_write(inode
, EXT4_BLOCK_ALIGN(newsize
, blkbits
));
2805 EXT4_I(inode
)->i_disksize
= i_size_read(inode
);
2806 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2807 mutex_unlock(&inode
->i_mutex
);
2809 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2813 <<<<<<< HEAD
:fs
/ext4
/extents
.c
2815 mutex_unlock(&inode
->i_mutex
);
2816 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:fs
/ext4
/extents
.c
2817 return ret
> 0 ? ret2
: ret
;