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
33 #include <linux/time.h>
34 #include <linux/jbd2.h>
35 #include <linux/highuid.h>
36 #include <linux/pagemap.h>
37 #include <linux/quotaops.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include <linux/falloc.h>
41 #include <asm/uaccess.h>
42 #include <linux/fiemap.h>
43 #include "ext4_jbd2.h"
45 #include <trace/events/ext4.h>
48 * used by extent splitting.
50 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
52 #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
53 #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
55 static int ext4_split_extent(handle_t *handle,
57 struct ext4_ext_path *path,
58 struct ext4_map_blocks *map,
62 static int ext4_split_extent_at(handle_t *handle,
64 struct ext4_ext_path *path,
69 static int ext4_ext_truncate_extend_restart(handle_t *handle,
75 if (!ext4_handle_valid(handle))
77 if (handle->h_buffer_credits > needed)
79 err = ext4_journal_extend(handle, needed);
82 err = ext4_truncate_restart_trans(handle, inode, needed);
94 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
95 struct ext4_ext_path *path)
98 /* path points to block */
99 return ext4_journal_get_write_access(handle, path->p_bh);
101 /* path points to leaf/index in inode body */
102 /* we use in-core data, no need to protect them */
112 #define ext4_ext_dirty(handle, inode, path) \
113 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
114 static int __ext4_ext_dirty(const char *where, unsigned int line,
115 handle_t *handle, struct inode *inode,
116 struct ext4_ext_path *path)
120 /* path points to block */
121 err = __ext4_handle_dirty_metadata(where, line, handle,
124 /* path points to leaf/index in inode body */
125 err = ext4_mark_inode_dirty(handle, inode);
130 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
131 struct ext4_ext_path *path,
135 int depth = path->p_depth;
136 struct ext4_extent *ex;
139 * Try to predict block placement assuming that we are
140 * filling in a file which will eventually be
141 * non-sparse --- i.e., in the case of libbfd writing
142 * an ELF object sections out-of-order but in a way
143 * the eventually results in a contiguous object or
144 * executable file, or some database extending a table
145 * space file. However, this is actually somewhat
146 * non-ideal if we are writing a sparse file such as
147 * qemu or KVM writing a raw image file that is going
148 * to stay fairly sparse, since it will end up
149 * fragmenting the file system's free space. Maybe we
150 * should have some hueristics or some way to allow
151 * userspace to pass a hint to file system,
152 * especially if the latter case turns out to be
155 ex = path[depth].p_ext;
157 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
158 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
160 if (block > ext_block)
161 return ext_pblk + (block - ext_block);
163 return ext_pblk - (ext_block - block);
166 /* it looks like index is empty;
167 * try to find starting block from index itself */
168 if (path[depth].p_bh)
169 return path[depth].p_bh->b_blocknr;
172 /* OK. use inode's group */
173 return ext4_inode_to_goal_block(inode);
177 * Allocation for a meta data block
180 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
181 struct ext4_ext_path *path,
182 struct ext4_extent *ex, int *err, unsigned int flags)
184 ext4_fsblk_t goal, newblock;
186 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
187 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
192 static inline int ext4_ext_space_block(struct inode *inode, int check)
196 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
197 / sizeof(struct ext4_extent);
198 #ifdef AGGRESSIVE_TEST
199 if (!check && size > 6)
205 static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
209 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
210 / sizeof(struct ext4_extent_idx);
211 #ifdef AGGRESSIVE_TEST
212 if (!check && size > 5)
218 static inline int ext4_ext_space_root(struct inode *inode, int check)
222 size = sizeof(EXT4_I(inode)->i_data);
223 size -= sizeof(struct ext4_extent_header);
224 size /= sizeof(struct ext4_extent);
225 #ifdef AGGRESSIVE_TEST
226 if (!check && size > 3)
232 static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
236 size = sizeof(EXT4_I(inode)->i_data);
237 size -= sizeof(struct ext4_extent_header);
238 size /= sizeof(struct ext4_extent_idx);
239 #ifdef AGGRESSIVE_TEST
240 if (!check && size > 4)
247 * Calculate the number of metadata blocks needed
248 * to allocate @blocks
249 * Worse case is one block per extent
251 int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
253 struct ext4_inode_info *ei = EXT4_I(inode);
256 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
257 / sizeof(struct ext4_extent_idx));
260 * If the new delayed allocation block is contiguous with the
261 * previous da block, it can share index blocks with the
262 * previous block, so we only need to allocate a new index
263 * block every idxs leaf blocks. At ldxs**2 blocks, we need
264 * an additional index block, and at ldxs**3 blocks, yet
265 * another index blocks.
267 if (ei->i_da_metadata_calc_len &&
268 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
271 if ((ei->i_da_metadata_calc_len % idxs) == 0)
273 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
275 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
277 ei->i_da_metadata_calc_len = 0;
279 ei->i_da_metadata_calc_len++;
280 ei->i_da_metadata_calc_last_lblock++;
285 * In the worst case we need a new set of index blocks at
286 * every level of the inode's extent tree.
288 ei->i_da_metadata_calc_len = 1;
289 ei->i_da_metadata_calc_last_lblock = lblock;
290 return ext_depth(inode) + 1;
294 ext4_ext_max_entries(struct inode *inode, int depth)
298 if (depth == ext_depth(inode)) {
300 max = ext4_ext_space_root(inode, 1);
302 max = ext4_ext_space_root_idx(inode, 1);
305 max = ext4_ext_space_block(inode, 1);
307 max = ext4_ext_space_block_idx(inode, 1);
313 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
315 ext4_fsblk_t block = ext4_ext_pblock(ext);
316 int len = ext4_ext_get_actual_len(ext);
320 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
323 static int ext4_valid_extent_idx(struct inode *inode,
324 struct ext4_extent_idx *ext_idx)
326 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
328 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
331 static int ext4_valid_extent_entries(struct inode *inode,
332 struct ext4_extent_header *eh,
335 unsigned short entries;
336 if (eh->eh_entries == 0)
339 entries = le16_to_cpu(eh->eh_entries);
343 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
345 if (!ext4_valid_extent(inode, ext))
351 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
353 if (!ext4_valid_extent_idx(inode, ext_idx))
362 static int __ext4_ext_check(const char *function, unsigned int line,
363 struct inode *inode, struct ext4_extent_header *eh,
366 const char *error_msg;
369 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
370 error_msg = "invalid magic";
373 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
374 error_msg = "unexpected eh_depth";
377 if (unlikely(eh->eh_max == 0)) {
378 error_msg = "invalid eh_max";
381 max = ext4_ext_max_entries(inode, depth);
382 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
383 error_msg = "too large eh_max";
386 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
387 error_msg = "invalid eh_entries";
390 if (!ext4_valid_extent_entries(inode, eh, depth)) {
391 error_msg = "invalid extent entries";
397 ext4_error_inode(inode, function, line, 0,
398 "bad header/extent: %s - magic %x, "
399 "entries %u, max %u(%u), depth %u(%u)",
400 error_msg, le16_to_cpu(eh->eh_magic),
401 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
402 max, le16_to_cpu(eh->eh_depth), depth);
407 #define ext4_ext_check(inode, eh, depth) \
408 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
410 int ext4_ext_check_inode(struct inode *inode)
412 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
416 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
418 int k, l = path->p_depth;
421 for (k = 0; k <= l; k++, path++) {
423 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
424 ext4_idx_pblock(path->p_idx));
425 } else if (path->p_ext) {
426 ext_debug(" %d:[%d]%d:%llu ",
427 le32_to_cpu(path->p_ext->ee_block),
428 ext4_ext_is_uninitialized(path->p_ext),
429 ext4_ext_get_actual_len(path->p_ext),
430 ext4_ext_pblock(path->p_ext));
437 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
439 int depth = ext_depth(inode);
440 struct ext4_extent_header *eh;
441 struct ext4_extent *ex;
447 eh = path[depth].p_hdr;
448 ex = EXT_FIRST_EXTENT(eh);
450 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
452 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
453 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
454 ext4_ext_is_uninitialized(ex),
455 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
460 static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
461 ext4_fsblk_t newblock, int level)
463 int depth = ext_depth(inode);
464 struct ext4_extent *ex;
466 if (depth != level) {
467 struct ext4_extent_idx *idx;
468 idx = path[level].p_idx;
469 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
470 ext_debug("%d: move %d:%llu in new index %llu\n", level,
471 le32_to_cpu(idx->ei_block),
472 ext4_idx_pblock(idx),
480 ex = path[depth].p_ext;
481 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
482 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
483 le32_to_cpu(ex->ee_block),
485 ext4_ext_is_uninitialized(ex),
486 ext4_ext_get_actual_len(ex),
493 #define ext4_ext_show_path(inode, path)
494 #define ext4_ext_show_leaf(inode, path)
495 #define ext4_ext_show_move(inode, path, newblock, level)
498 void ext4_ext_drop_refs(struct ext4_ext_path *path)
500 int depth = path->p_depth;
503 for (i = 0; i <= depth; i++, path++)
511 * ext4_ext_binsearch_idx:
512 * binary search for the closest index of the given block
513 * the header must be checked before calling this
516 ext4_ext_binsearch_idx(struct inode *inode,
517 struct ext4_ext_path *path, ext4_lblk_t block)
519 struct ext4_extent_header *eh = path->p_hdr;
520 struct ext4_extent_idx *r, *l, *m;
523 ext_debug("binsearch for %u(idx): ", block);
525 l = EXT_FIRST_INDEX(eh) + 1;
526 r = EXT_LAST_INDEX(eh);
529 if (block < le32_to_cpu(m->ei_block))
533 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
534 m, le32_to_cpu(m->ei_block),
535 r, le32_to_cpu(r->ei_block));
539 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
540 ext4_idx_pblock(path->p_idx));
542 #ifdef CHECK_BINSEARCH
544 struct ext4_extent_idx *chix, *ix;
547 chix = ix = EXT_FIRST_INDEX(eh);
548 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
550 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
551 printk(KERN_DEBUG "k=%d, ix=0x%p, "
553 ix, EXT_FIRST_INDEX(eh));
554 printk(KERN_DEBUG "%u <= %u\n",
555 le32_to_cpu(ix->ei_block),
556 le32_to_cpu(ix[-1].ei_block));
558 BUG_ON(k && le32_to_cpu(ix->ei_block)
559 <= le32_to_cpu(ix[-1].ei_block));
560 if (block < le32_to_cpu(ix->ei_block))
564 BUG_ON(chix != path->p_idx);
571 * ext4_ext_binsearch:
572 * binary search for closest extent of the given block
573 * the header must be checked before calling this
576 ext4_ext_binsearch(struct inode *inode,
577 struct ext4_ext_path *path, ext4_lblk_t block)
579 struct ext4_extent_header *eh = path->p_hdr;
580 struct ext4_extent *r, *l, *m;
582 if (eh->eh_entries == 0) {
584 * this leaf is empty:
585 * we get such a leaf in split/add case
590 ext_debug("binsearch for %u: ", block);
592 l = EXT_FIRST_EXTENT(eh) + 1;
593 r = EXT_LAST_EXTENT(eh);
597 if (block < le32_to_cpu(m->ee_block))
601 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
602 m, le32_to_cpu(m->ee_block),
603 r, le32_to_cpu(r->ee_block));
607 ext_debug(" -> %d:%llu:[%d]%d ",
608 le32_to_cpu(path->p_ext->ee_block),
609 ext4_ext_pblock(path->p_ext),
610 ext4_ext_is_uninitialized(path->p_ext),
611 ext4_ext_get_actual_len(path->p_ext));
613 #ifdef CHECK_BINSEARCH
615 struct ext4_extent *chex, *ex;
618 chex = ex = EXT_FIRST_EXTENT(eh);
619 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
620 BUG_ON(k && le32_to_cpu(ex->ee_block)
621 <= le32_to_cpu(ex[-1].ee_block));
622 if (block < le32_to_cpu(ex->ee_block))
626 BUG_ON(chex != path->p_ext);
632 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
634 struct ext4_extent_header *eh;
636 eh = ext_inode_hdr(inode);
639 eh->eh_magic = EXT4_EXT_MAGIC;
640 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
641 ext4_mark_inode_dirty(handle, inode);
642 ext4_ext_invalidate_cache(inode);
646 struct ext4_ext_path *
647 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
648 struct ext4_ext_path *path)
650 struct ext4_extent_header *eh;
651 struct buffer_head *bh;
652 short int depth, i, ppos = 0, alloc = 0;
654 eh = ext_inode_hdr(inode);
655 depth = ext_depth(inode);
657 /* account possible depth increase */
659 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
662 return ERR_PTR(-ENOMEM);
669 /* walk through the tree */
671 int need_to_validate = 0;
673 ext_debug("depth %d: num %d, max %d\n",
674 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
676 ext4_ext_binsearch_idx(inode, path + ppos, block);
677 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
678 path[ppos].p_depth = i;
679 path[ppos].p_ext = NULL;
681 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
684 if (!bh_uptodate_or_lock(bh)) {
685 trace_ext4_ext_load_extent(inode, block,
687 if (bh_submit_read(bh) < 0) {
691 /* validate the extent entries */
692 need_to_validate = 1;
694 eh = ext_block_hdr(bh);
696 if (unlikely(ppos > depth)) {
698 EXT4_ERROR_INODE(inode,
699 "ppos %d > depth %d", ppos, depth);
702 path[ppos].p_bh = bh;
703 path[ppos].p_hdr = eh;
706 if (need_to_validate && ext4_ext_check(inode, eh, i))
710 path[ppos].p_depth = i;
711 path[ppos].p_ext = NULL;
712 path[ppos].p_idx = NULL;
715 ext4_ext_binsearch(inode, path + ppos, block);
716 /* if not an empty leaf */
717 if (path[ppos].p_ext)
718 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
720 ext4_ext_show_path(inode, path);
725 ext4_ext_drop_refs(path);
728 return ERR_PTR(-EIO);
732 * ext4_ext_insert_index:
733 * insert new index [@logical;@ptr] into the block at @curp;
734 * check where to insert: before @curp or after @curp
736 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
737 struct ext4_ext_path *curp,
738 int logical, ext4_fsblk_t ptr)
740 struct ext4_extent_idx *ix;
743 err = ext4_ext_get_access(handle, inode, curp);
747 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
748 EXT4_ERROR_INODE(inode,
749 "logical %d == ei_block %d!",
750 logical, le32_to_cpu(curp->p_idx->ei_block));
754 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
755 >= le16_to_cpu(curp->p_hdr->eh_max))) {
756 EXT4_ERROR_INODE(inode,
757 "eh_entries %d >= eh_max %d!",
758 le16_to_cpu(curp->p_hdr->eh_entries),
759 le16_to_cpu(curp->p_hdr->eh_max));
763 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
765 ext_debug("insert new index %d after: %llu\n", logical, ptr);
766 ix = curp->p_idx + 1;
769 ext_debug("insert new index %d before: %llu\n", logical, ptr);
773 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
776 ext_debug("insert new index %d: "
777 "move %d indices from 0x%p to 0x%p\n",
778 logical, len, ix, ix + 1);
779 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
782 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
783 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
787 ix->ei_block = cpu_to_le32(logical);
788 ext4_idx_store_pblock(ix, ptr);
789 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
791 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
792 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
796 err = ext4_ext_dirty(handle, inode, curp);
797 ext4_std_error(inode->i_sb, err);
804 * inserts new subtree into the path, using free index entry
806 * - allocates all needed blocks (new leaf and all intermediate index blocks)
807 * - makes decision where to split
808 * - moves remaining extents and index entries (right to the split point)
809 * into the newly allocated blocks
810 * - initializes subtree
812 static int ext4_ext_split(handle_t *handle, struct inode *inode,
814 struct ext4_ext_path *path,
815 struct ext4_extent *newext, int at)
817 struct buffer_head *bh = NULL;
818 int depth = ext_depth(inode);
819 struct ext4_extent_header *neh;
820 struct ext4_extent_idx *fidx;
822 ext4_fsblk_t newblock, oldblock;
824 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
827 /* make decision: where to split? */
828 /* FIXME: now decision is simplest: at current extent */
830 /* if current leaf will be split, then we should use
831 * border from split point */
832 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
833 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
836 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
837 border = path[depth].p_ext[1].ee_block;
838 ext_debug("leaf will be split."
839 " next leaf starts at %d\n",
840 le32_to_cpu(border));
842 border = newext->ee_block;
843 ext_debug("leaf will be added."
844 " next leaf starts at %d\n",
845 le32_to_cpu(border));
849 * If error occurs, then we break processing
850 * and mark filesystem read-only. index won't
851 * be inserted and tree will be in consistent
852 * state. Next mount will repair buffers too.
856 * Get array to track all allocated blocks.
857 * We need this to handle errors and free blocks
860 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
864 /* allocate all needed blocks */
865 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
866 for (a = 0; a < depth - at; a++) {
867 newblock = ext4_ext_new_meta_block(handle, inode, path,
868 newext, &err, flags);
871 ablocks[a] = newblock;
874 /* initialize new leaf */
875 newblock = ablocks[--a];
876 if (unlikely(newblock == 0)) {
877 EXT4_ERROR_INODE(inode, "newblock == 0!");
881 bh = sb_getblk(inode->i_sb, newblock);
888 err = ext4_journal_get_create_access(handle, bh);
892 neh = ext_block_hdr(bh);
894 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
895 neh->eh_magic = EXT4_EXT_MAGIC;
898 /* move remainder of path[depth] to the new leaf */
899 if (unlikely(path[depth].p_hdr->eh_entries !=
900 path[depth].p_hdr->eh_max)) {
901 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
902 path[depth].p_hdr->eh_entries,
903 path[depth].p_hdr->eh_max);
907 /* start copy from next extent */
908 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
909 ext4_ext_show_move(inode, path, newblock, depth);
911 struct ext4_extent *ex;
912 ex = EXT_FIRST_EXTENT(neh);
913 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
914 le16_add_cpu(&neh->eh_entries, m);
917 set_buffer_uptodate(bh);
920 err = ext4_handle_dirty_metadata(handle, inode, bh);
926 /* correct old leaf */
928 err = ext4_ext_get_access(handle, inode, path + depth);
931 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
932 err = ext4_ext_dirty(handle, inode, path + depth);
938 /* create intermediate indexes */
940 if (unlikely(k < 0)) {
941 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
946 ext_debug("create %d intermediate indices\n", k);
947 /* insert new index into current index block */
948 /* current depth stored in i var */
952 newblock = ablocks[--a];
953 bh = sb_getblk(inode->i_sb, newblock);
960 err = ext4_journal_get_create_access(handle, bh);
964 neh = ext_block_hdr(bh);
965 neh->eh_entries = cpu_to_le16(1);
966 neh->eh_magic = EXT4_EXT_MAGIC;
967 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
968 neh->eh_depth = cpu_to_le16(depth - i);
969 fidx = EXT_FIRST_INDEX(neh);
970 fidx->ei_block = border;
971 ext4_idx_store_pblock(fidx, oldblock);
973 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
974 i, newblock, le32_to_cpu(border), oldblock);
976 /* move remainder of path[i] to the new index block */
977 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
978 EXT_LAST_INDEX(path[i].p_hdr))) {
979 EXT4_ERROR_INODE(inode,
980 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
981 le32_to_cpu(path[i].p_ext->ee_block));
985 /* start copy indexes */
986 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
987 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
988 EXT_MAX_INDEX(path[i].p_hdr));
989 ext4_ext_show_move(inode, path, newblock, i);
991 memmove(++fidx, path[i].p_idx,
992 sizeof(struct ext4_extent_idx) * m);
993 le16_add_cpu(&neh->eh_entries, m);
995 set_buffer_uptodate(bh);
998 err = ext4_handle_dirty_metadata(handle, inode, bh);
1004 /* correct old index */
1006 err = ext4_ext_get_access(handle, inode, path + i);
1009 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1010 err = ext4_ext_dirty(handle, inode, path + i);
1018 /* insert new index */
1019 err = ext4_ext_insert_index(handle, inode, path + at,
1020 le32_to_cpu(border), newblock);
1024 if (buffer_locked(bh))
1030 /* free all allocated blocks in error case */
1031 for (i = 0; i < depth; i++) {
1034 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
1035 EXT4_FREE_BLOCKS_METADATA);
1044 * ext4_ext_grow_indepth:
1045 * implements tree growing procedure:
1046 * - allocates new block
1047 * - moves top-level data (index block or leaf) into the new block
1048 * - initializes new top-level, creating index that points to the
1049 * just created block
1051 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1053 struct ext4_extent *newext)
1055 struct ext4_extent_header *neh;
1056 struct buffer_head *bh;
1057 ext4_fsblk_t newblock;
1060 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
1061 newext, &err, flags);
1065 bh = sb_getblk(inode->i_sb, newblock);
1068 ext4_std_error(inode->i_sb, err);
1073 err = ext4_journal_get_create_access(handle, bh);
1079 /* move top-level index/leaf into new block */
1080 memmove(bh->b_data, EXT4_I(inode)->i_data,
1081 sizeof(EXT4_I(inode)->i_data));
1083 /* set size of new block */
1084 neh = ext_block_hdr(bh);
1085 /* old root could have indexes or leaves
1086 * so calculate e_max right way */
1087 if (ext_depth(inode))
1088 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1090 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1091 neh->eh_magic = EXT4_EXT_MAGIC;
1092 set_buffer_uptodate(bh);
1095 err = ext4_handle_dirty_metadata(handle, inode, bh);
1099 /* Update top-level index: num,max,pointer */
1100 neh = ext_inode_hdr(inode);
1101 neh->eh_entries = cpu_to_le16(1);
1102 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1103 if (neh->eh_depth == 0) {
1104 /* Root extent block becomes index block */
1105 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1106 EXT_FIRST_INDEX(neh)->ei_block =
1107 EXT_FIRST_EXTENT(neh)->ee_block;
1109 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1110 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1111 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1112 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1114 neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1);
1115 ext4_mark_inode_dirty(handle, inode);
1123 * ext4_ext_create_new_leaf:
1124 * finds empty index and adds new leaf.
1125 * if no free index is found, then it requests in-depth growing.
1127 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1129 struct ext4_ext_path *path,
1130 struct ext4_extent *newext)
1132 struct ext4_ext_path *curp;
1133 int depth, i, err = 0;
1136 i = depth = ext_depth(inode);
1138 /* walk up to the tree and look for free index entry */
1139 curp = path + depth;
1140 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1145 /* we use already allocated block for index block,
1146 * so subsequent data blocks should be contiguous */
1147 if (EXT_HAS_FREE_INDEX(curp)) {
1148 /* if we found index with free entry, then use that
1149 * entry: create all needed subtree and add new leaf */
1150 err = ext4_ext_split(handle, inode, flags, path, newext, i);
1155 ext4_ext_drop_refs(path);
1156 path = ext4_ext_find_extent(inode,
1157 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1160 err = PTR_ERR(path);
1162 /* tree is full, time to grow in depth */
1163 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
1168 ext4_ext_drop_refs(path);
1169 path = ext4_ext_find_extent(inode,
1170 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1173 err = PTR_ERR(path);
1178 * only first (depth 0 -> 1) produces free space;
1179 * in all other cases we have to split the grown tree
1181 depth = ext_depth(inode);
1182 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1183 /* now we need to split */
1193 * search the closest allocated block to the left for *logical
1194 * and returns it at @logical + it's physical address at @phys
1195 * if *logical is the smallest allocated block, the function
1196 * returns 0 at @phys
1197 * return value contains 0 (success) or error code
1199 static int ext4_ext_search_left(struct inode *inode,
1200 struct ext4_ext_path *path,
1201 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1203 struct ext4_extent_idx *ix;
1204 struct ext4_extent *ex;
1207 if (unlikely(path == NULL)) {
1208 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1211 depth = path->p_depth;
1214 if (depth == 0 && path->p_ext == NULL)
1217 /* usually extent in the path covers blocks smaller
1218 * then *logical, but it can be that extent is the
1219 * first one in the file */
1221 ex = path[depth].p_ext;
1222 ee_len = ext4_ext_get_actual_len(ex);
1223 if (*logical < le32_to_cpu(ex->ee_block)) {
1224 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1225 EXT4_ERROR_INODE(inode,
1226 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1227 *logical, le32_to_cpu(ex->ee_block));
1230 while (--depth >= 0) {
1231 ix = path[depth].p_idx;
1232 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1233 EXT4_ERROR_INODE(inode,
1234 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1235 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
1236 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1237 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
1245 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1246 EXT4_ERROR_INODE(inode,
1247 "logical %d < ee_block %d + ee_len %d!",
1248 *logical, le32_to_cpu(ex->ee_block), ee_len);
1252 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1253 *phys = ext4_ext_pblock(ex) + ee_len - 1;
1258 * search the closest allocated block to the right for *logical
1259 * and returns it at @logical + it's physical address at @phys
1260 * if *logical is the largest allocated block, the function
1261 * returns 0 at @phys
1262 * return value contains 0 (success) or error code
1264 static int ext4_ext_search_right(struct inode *inode,
1265 struct ext4_ext_path *path,
1266 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1267 struct ext4_extent **ret_ex)
1269 struct buffer_head *bh = NULL;
1270 struct ext4_extent_header *eh;
1271 struct ext4_extent_idx *ix;
1272 struct ext4_extent *ex;
1274 int depth; /* Note, NOT eh_depth; depth from top of tree */
1277 if (unlikely(path == NULL)) {
1278 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1281 depth = path->p_depth;
1284 if (depth == 0 && path->p_ext == NULL)
1287 /* usually extent in the path covers blocks smaller
1288 * then *logical, but it can be that extent is the
1289 * first one in the file */
1291 ex = path[depth].p_ext;
1292 ee_len = ext4_ext_get_actual_len(ex);
1293 if (*logical < le32_to_cpu(ex->ee_block)) {
1294 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1295 EXT4_ERROR_INODE(inode,
1296 "first_extent(path[%d].p_hdr) != ex",
1300 while (--depth >= 0) {
1301 ix = path[depth].p_idx;
1302 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1303 EXT4_ERROR_INODE(inode,
1304 "ix != EXT_FIRST_INDEX *logical %d!",
1312 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1313 EXT4_ERROR_INODE(inode,
1314 "logical %d < ee_block %d + ee_len %d!",
1315 *logical, le32_to_cpu(ex->ee_block), ee_len);
1319 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1320 /* next allocated block in this leaf */
1325 /* go up and search for index to the right */
1326 while (--depth >= 0) {
1327 ix = path[depth].p_idx;
1328 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1332 /* we've gone up to the root and found no index to the right */
1336 /* we've found index to the right, let's
1337 * follow it and find the closest allocated
1338 * block to the right */
1340 block = ext4_idx_pblock(ix);
1341 while (++depth < path->p_depth) {
1342 bh = sb_bread(inode->i_sb, block);
1345 eh = ext_block_hdr(bh);
1346 /* subtract from p_depth to get proper eh_depth */
1347 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1351 ix = EXT_FIRST_INDEX(eh);
1352 block = ext4_idx_pblock(ix);
1356 bh = sb_bread(inode->i_sb, block);
1359 eh = ext_block_hdr(bh);
1360 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1364 ex = EXT_FIRST_EXTENT(eh);
1366 *logical = le32_to_cpu(ex->ee_block);
1367 *phys = ext4_ext_pblock(ex);
1375 * ext4_ext_next_allocated_block:
1376 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1377 * NOTE: it considers block number from index entry as
1378 * allocated block. Thus, index entries have to be consistent
1382 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1386 BUG_ON(path == NULL);
1387 depth = path->p_depth;
1389 if (depth == 0 && path->p_ext == NULL)
1390 return EXT_MAX_BLOCKS;
1392 while (depth >= 0) {
1393 if (depth == path->p_depth) {
1395 if (path[depth].p_ext &&
1396 path[depth].p_ext !=
1397 EXT_LAST_EXTENT(path[depth].p_hdr))
1398 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1401 if (path[depth].p_idx !=
1402 EXT_LAST_INDEX(path[depth].p_hdr))
1403 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1408 return EXT_MAX_BLOCKS;
1412 * ext4_ext_next_leaf_block:
1413 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1415 static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
1419 BUG_ON(path == NULL);
1420 depth = path->p_depth;
1422 /* zero-tree has no leaf blocks at all */
1424 return EXT_MAX_BLOCKS;
1426 /* go to index block */
1429 while (depth >= 0) {
1430 if (path[depth].p_idx !=
1431 EXT_LAST_INDEX(path[depth].p_hdr))
1432 return (ext4_lblk_t)
1433 le32_to_cpu(path[depth].p_idx[1].ei_block);
1437 return EXT_MAX_BLOCKS;
1441 * ext4_ext_correct_indexes:
1442 * if leaf gets modified and modified extent is first in the leaf,
1443 * then we have to correct all indexes above.
1444 * TODO: do we need to correct tree in all cases?
1446 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1447 struct ext4_ext_path *path)
1449 struct ext4_extent_header *eh;
1450 int depth = ext_depth(inode);
1451 struct ext4_extent *ex;
1455 eh = path[depth].p_hdr;
1456 ex = path[depth].p_ext;
1458 if (unlikely(ex == NULL || eh == NULL)) {
1459 EXT4_ERROR_INODE(inode,
1460 "ex %p == NULL or eh %p == NULL", ex, eh);
1465 /* there is no tree at all */
1469 if (ex != EXT_FIRST_EXTENT(eh)) {
1470 /* we correct tree if first leaf got modified only */
1475 * TODO: we need correction if border is smaller than current one
1478 border = path[depth].p_ext->ee_block;
1479 err = ext4_ext_get_access(handle, inode, path + k);
1482 path[k].p_idx->ei_block = border;
1483 err = ext4_ext_dirty(handle, inode, path + k);
1488 /* change all left-side indexes */
1489 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1491 err = ext4_ext_get_access(handle, inode, path + k);
1494 path[k].p_idx->ei_block = border;
1495 err = ext4_ext_dirty(handle, inode, path + k);
1504 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1505 struct ext4_extent *ex2)
1507 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1510 * Make sure that either both extents are uninitialized, or
1513 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1516 if (ext4_ext_is_uninitialized(ex1))
1517 max_len = EXT_UNINIT_MAX_LEN;
1519 max_len = EXT_INIT_MAX_LEN;
1521 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1522 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1524 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1525 le32_to_cpu(ex2->ee_block))
1529 * To allow future support for preallocated extents to be added
1530 * as an RO_COMPAT feature, refuse to merge to extents if
1531 * this can result in the top bit of ee_len being set.
1533 if (ext1_ee_len + ext2_ee_len > max_len)
1535 #ifdef AGGRESSIVE_TEST
1536 if (ext1_ee_len >= 4)
1540 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
1546 * This function tries to merge the "ex" extent to the next extent in the tree.
1547 * It always tries to merge towards right. If you want to merge towards
1548 * left, pass "ex - 1" as argument instead of "ex".
1549 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1550 * 1 if they got merged.
1552 static int ext4_ext_try_to_merge_right(struct inode *inode,
1553 struct ext4_ext_path *path,
1554 struct ext4_extent *ex)
1556 struct ext4_extent_header *eh;
1557 unsigned int depth, len;
1559 int uninitialized = 0;
1561 depth = ext_depth(inode);
1562 BUG_ON(path[depth].p_hdr == NULL);
1563 eh = path[depth].p_hdr;
1565 while (ex < EXT_LAST_EXTENT(eh)) {
1566 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1568 /* merge with next extent! */
1569 if (ext4_ext_is_uninitialized(ex))
1571 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1572 + ext4_ext_get_actual_len(ex + 1));
1574 ext4_ext_mark_uninitialized(ex);
1576 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1577 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1578 * sizeof(struct ext4_extent);
1579 memmove(ex + 1, ex + 2, len);
1581 le16_add_cpu(&eh->eh_entries, -1);
1583 WARN_ON(eh->eh_entries == 0);
1584 if (!eh->eh_entries)
1585 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
1592 * This function tries to merge the @ex extent to neighbours in the tree.
1593 * return 1 if merge left else 0.
1595 static int ext4_ext_try_to_merge(struct inode *inode,
1596 struct ext4_ext_path *path,
1597 struct ext4_extent *ex) {
1598 struct ext4_extent_header *eh;
1603 depth = ext_depth(inode);
1604 BUG_ON(path[depth].p_hdr == NULL);
1605 eh = path[depth].p_hdr;
1607 if (ex > EXT_FIRST_EXTENT(eh))
1608 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1611 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1617 * check if a portion of the "newext" extent overlaps with an
1620 * If there is an overlap discovered, it updates the length of the newext
1621 * such that there will be no overlap, and then returns 1.
1622 * If there is no overlap found, it returns 0.
1624 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1625 struct inode *inode,
1626 struct ext4_extent *newext,
1627 struct ext4_ext_path *path)
1630 unsigned int depth, len1;
1631 unsigned int ret = 0;
1633 b1 = le32_to_cpu(newext->ee_block);
1634 len1 = ext4_ext_get_actual_len(newext);
1635 depth = ext_depth(inode);
1636 if (!path[depth].p_ext)
1638 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1639 b2 &= ~(sbi->s_cluster_ratio - 1);
1642 * get the next allocated block if the extent in the path
1643 * is before the requested block(s)
1646 b2 = ext4_ext_next_allocated_block(path);
1647 if (b2 == EXT_MAX_BLOCKS)
1649 b2 &= ~(sbi->s_cluster_ratio - 1);
1652 /* check for wrap through zero on extent logical start block*/
1653 if (b1 + len1 < b1) {
1654 len1 = EXT_MAX_BLOCKS - b1;
1655 newext->ee_len = cpu_to_le16(len1);
1659 /* check for overlap */
1660 if (b1 + len1 > b2) {
1661 newext->ee_len = cpu_to_le16(b2 - b1);
1669 * ext4_ext_insert_extent:
1670 * tries to merge requsted extent into the existing extent or
1671 * inserts requested extent as new one into the tree,
1672 * creating new leaf in the no-space case.
1674 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1675 struct ext4_ext_path *path,
1676 struct ext4_extent *newext, int flag)
1678 struct ext4_extent_header *eh;
1679 struct ext4_extent *ex, *fex;
1680 struct ext4_extent *nearex; /* nearest extent */
1681 struct ext4_ext_path *npath = NULL;
1682 int depth, len, err;
1684 unsigned uninitialized = 0;
1687 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1688 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1691 depth = ext_depth(inode);
1692 ex = path[depth].p_ext;
1693 if (unlikely(path[depth].p_hdr == NULL)) {
1694 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1698 /* try to insert block into found extent and return */
1699 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
1700 && ext4_can_extents_be_merged(inode, ex, newext)) {
1701 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
1702 ext4_ext_is_uninitialized(newext),
1703 ext4_ext_get_actual_len(newext),
1704 le32_to_cpu(ex->ee_block),
1705 ext4_ext_is_uninitialized(ex),
1706 ext4_ext_get_actual_len(ex),
1707 ext4_ext_pblock(ex));
1708 err = ext4_ext_get_access(handle, inode, path + depth);
1713 * ext4_can_extents_be_merged should have checked that either
1714 * both extents are uninitialized, or both aren't. Thus we
1715 * need to check only one of them here.
1717 if (ext4_ext_is_uninitialized(ex))
1719 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1720 + ext4_ext_get_actual_len(newext));
1722 ext4_ext_mark_uninitialized(ex);
1723 eh = path[depth].p_hdr;
1728 depth = ext_depth(inode);
1729 eh = path[depth].p_hdr;
1730 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1733 /* probably next leaf has space for us? */
1734 fex = EXT_LAST_EXTENT(eh);
1735 next = EXT_MAX_BLOCKS;
1736 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
1737 next = ext4_ext_next_leaf_block(path);
1738 if (next != EXT_MAX_BLOCKS) {
1739 ext_debug("next leaf block - %u\n", next);
1740 BUG_ON(npath != NULL);
1741 npath = ext4_ext_find_extent(inode, next, NULL);
1743 return PTR_ERR(npath);
1744 BUG_ON(npath->p_depth != path->p_depth);
1745 eh = npath[depth].p_hdr;
1746 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1747 ext_debug("next leaf isn't full(%d)\n",
1748 le16_to_cpu(eh->eh_entries));
1752 ext_debug("next leaf has no free space(%d,%d)\n",
1753 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1757 * There is no free space in the found leaf.
1758 * We're gonna add a new leaf in the tree.
1760 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1761 flags = EXT4_MB_USE_ROOT_BLOCKS;
1762 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
1765 depth = ext_depth(inode);
1766 eh = path[depth].p_hdr;
1769 nearex = path[depth].p_ext;
1771 err = ext4_ext_get_access(handle, inode, path + depth);
1776 /* there is no extent in this leaf, create first one */
1777 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
1778 le32_to_cpu(newext->ee_block),
1779 ext4_ext_pblock(newext),
1780 ext4_ext_is_uninitialized(newext),
1781 ext4_ext_get_actual_len(newext));
1782 nearex = EXT_FIRST_EXTENT(eh);
1784 if (le32_to_cpu(newext->ee_block)
1785 > le32_to_cpu(nearex->ee_block)) {
1787 ext_debug("insert %u:%llu:[%d]%d before: "
1789 le32_to_cpu(newext->ee_block),
1790 ext4_ext_pblock(newext),
1791 ext4_ext_is_uninitialized(newext),
1792 ext4_ext_get_actual_len(newext),
1797 BUG_ON(newext->ee_block == nearex->ee_block);
1798 ext_debug("insert %u:%llu:[%d]%d after: "
1800 le32_to_cpu(newext->ee_block),
1801 ext4_ext_pblock(newext),
1802 ext4_ext_is_uninitialized(newext),
1803 ext4_ext_get_actual_len(newext),
1806 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1808 ext_debug("insert %u:%llu:[%d]%d: "
1809 "move %d extents from 0x%p to 0x%p\n",
1810 le32_to_cpu(newext->ee_block),
1811 ext4_ext_pblock(newext),
1812 ext4_ext_is_uninitialized(newext),
1813 ext4_ext_get_actual_len(newext),
1814 len, nearex, nearex + 1);
1815 memmove(nearex + 1, nearex,
1816 len * sizeof(struct ext4_extent));
1820 le16_add_cpu(&eh->eh_entries, 1);
1821 path[depth].p_ext = nearex;
1822 nearex->ee_block = newext->ee_block;
1823 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
1824 nearex->ee_len = newext->ee_len;
1827 /* try to merge extents to the right */
1828 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
1829 ext4_ext_try_to_merge(inode, path, nearex);
1831 /* try to merge extents to the left */
1833 /* time to correct all indexes above */
1834 err = ext4_ext_correct_indexes(handle, inode, path);
1838 err = ext4_ext_dirty(handle, inode, path + depth);
1842 ext4_ext_drop_refs(npath);
1845 ext4_ext_invalidate_cache(inode);
1849 static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1850 ext4_lblk_t num, ext_prepare_callback func,
1853 struct ext4_ext_path *path = NULL;
1854 struct ext4_ext_cache cbex;
1855 struct ext4_extent *ex;
1856 ext4_lblk_t next, start = 0, end = 0;
1857 ext4_lblk_t last = block + num;
1858 int depth, exists, err = 0;
1860 BUG_ON(func == NULL);
1861 BUG_ON(inode == NULL);
1863 while (block < last && block != EXT_MAX_BLOCKS) {
1865 /* find extent for this block */
1866 down_read(&EXT4_I(inode)->i_data_sem);
1867 path = ext4_ext_find_extent(inode, block, path);
1868 up_read(&EXT4_I(inode)->i_data_sem);
1870 err = PTR_ERR(path);
1875 depth = ext_depth(inode);
1876 if (unlikely(path[depth].p_hdr == NULL)) {
1877 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1881 ex = path[depth].p_ext;
1882 next = ext4_ext_next_allocated_block(path);
1886 /* there is no extent yet, so try to allocate
1887 * all requested space */
1890 } else if (le32_to_cpu(ex->ee_block) > block) {
1891 /* need to allocate space before found extent */
1893 end = le32_to_cpu(ex->ee_block);
1894 if (block + num < end)
1896 } else if (block >= le32_to_cpu(ex->ee_block)
1897 + ext4_ext_get_actual_len(ex)) {
1898 /* need to allocate space after found extent */
1903 } else if (block >= le32_to_cpu(ex->ee_block)) {
1905 * some part of requested space is covered
1909 end = le32_to_cpu(ex->ee_block)
1910 + ext4_ext_get_actual_len(ex);
1911 if (block + num < end)
1917 BUG_ON(end <= start);
1920 cbex.ec_block = start;
1921 cbex.ec_len = end - start;
1924 cbex.ec_block = le32_to_cpu(ex->ee_block);
1925 cbex.ec_len = ext4_ext_get_actual_len(ex);
1926 cbex.ec_start = ext4_ext_pblock(ex);
1929 if (unlikely(cbex.ec_len == 0)) {
1930 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1934 err = func(inode, next, &cbex, ex, cbdata);
1935 ext4_ext_drop_refs(path);
1940 if (err == EXT_REPEAT)
1942 else if (err == EXT_BREAK) {
1947 if (ext_depth(inode) != depth) {
1948 /* depth was changed. we have to realloc path */
1953 block = cbex.ec_block + cbex.ec_len;
1957 ext4_ext_drop_refs(path);
1965 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1966 __u32 len, ext4_fsblk_t start)
1968 struct ext4_ext_cache *cex;
1970 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1971 trace_ext4_ext_put_in_cache(inode, block, len, start);
1972 cex = &EXT4_I(inode)->i_cached_extent;
1973 cex->ec_block = block;
1975 cex->ec_start = start;
1976 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1980 * ext4_ext_put_gap_in_cache:
1981 * calculate boundaries of the gap that the requested block fits into
1982 * and cache this gap
1985 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1988 int depth = ext_depth(inode);
1991 struct ext4_extent *ex;
1993 ex = path[depth].p_ext;
1995 /* there is no extent yet, so gap is [0;-] */
1997 len = EXT_MAX_BLOCKS;
1998 ext_debug("cache gap(whole file):");
1999 } else if (block < le32_to_cpu(ex->ee_block)) {
2001 len = le32_to_cpu(ex->ee_block) - block;
2002 ext_debug("cache gap(before): %u [%u:%u]",
2004 le32_to_cpu(ex->ee_block),
2005 ext4_ext_get_actual_len(ex));
2006 } else if (block >= le32_to_cpu(ex->ee_block)
2007 + ext4_ext_get_actual_len(ex)) {
2009 lblock = le32_to_cpu(ex->ee_block)
2010 + ext4_ext_get_actual_len(ex);
2012 next = ext4_ext_next_allocated_block(path);
2013 ext_debug("cache gap(after): [%u:%u] %u",
2014 le32_to_cpu(ex->ee_block),
2015 ext4_ext_get_actual_len(ex),
2017 BUG_ON(next == lblock);
2018 len = next - lblock;
2024 ext_debug(" -> %u:%lu\n", lblock, len);
2025 ext4_ext_put_in_cache(inode, lblock, len, 0);
2029 * ext4_ext_check_cache()
2030 * Checks to see if the given block is in the cache.
2031 * If it is, the cached extent is stored in the given
2032 * cache extent pointer. If the cached extent is a hole,
2033 * this routine should be used instead of
2034 * ext4_ext_in_cache if the calling function needs to
2035 * know the size of the hole.
2037 * @inode: The files inode
2038 * @block: The block to look for in the cache
2039 * @ex: Pointer where the cached extent will be stored
2040 * if it contains block
2042 * Return 0 if cache is invalid; 1 if the cache is valid
2044 static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2045 struct ext4_ext_cache *ex){
2046 struct ext4_ext_cache *cex;
2047 struct ext4_sb_info *sbi;
2051 * We borrow i_block_reservation_lock to protect i_cached_extent
2053 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
2054 cex = &EXT4_I(inode)->i_cached_extent;
2055 sbi = EXT4_SB(inode->i_sb);
2057 /* has cache valid data? */
2058 if (cex->ec_len == 0)
2061 if (in_range(block, cex->ec_block, cex->ec_len)) {
2062 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
2063 ext_debug("%u cached by %u:%u:%llu\n",
2065 cex->ec_block, cex->ec_len, cex->ec_start);
2070 sbi->extent_cache_misses++;
2072 sbi->extent_cache_hits++;
2073 trace_ext4_ext_in_cache(inode, block, ret);
2074 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2079 * ext4_ext_in_cache()
2080 * Checks to see if the given block is in the cache.
2081 * If it is, the cached extent is stored in the given
2084 * @inode: The files inode
2085 * @block: The block to look for in the cache
2086 * @ex: Pointer where the cached extent will be stored
2087 * if it contains block
2089 * Return 0 if cache is invalid; 1 if the cache is valid
2092 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2093 struct ext4_extent *ex)
2095 struct ext4_ext_cache cex;
2098 if (ext4_ext_check_cache(inode, block, &cex)) {
2099 ex->ee_block = cpu_to_le32(cex.ec_block);
2100 ext4_ext_store_pblock(ex, cex.ec_start);
2101 ex->ee_len = cpu_to_le16(cex.ec_len);
2111 * removes index from the index block.
2113 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
2114 struct ext4_ext_path *path)
2119 /* free index block */
2121 leaf = ext4_idx_pblock(path->p_idx);
2122 if (unlikely(path->p_hdr->eh_entries == 0)) {
2123 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2126 err = ext4_ext_get_access(handle, inode, path);
2130 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2131 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2132 len *= sizeof(struct ext4_extent_idx);
2133 memmove(path->p_idx, path->p_idx + 1, len);
2136 le16_add_cpu(&path->p_hdr->eh_entries, -1);
2137 err = ext4_ext_dirty(handle, inode, path);
2140 ext_debug("index is empty, remove it, free block %llu\n", leaf);
2141 trace_ext4_ext_rm_idx(inode, leaf);
2143 ext4_free_blocks(handle, inode, NULL, leaf, 1,
2144 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
2149 * ext4_ext_calc_credits_for_single_extent:
2150 * This routine returns max. credits that needed to insert an extent
2151 * to the extent tree.
2152 * When pass the actual path, the caller should calculate credits
2155 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2156 struct ext4_ext_path *path)
2159 int depth = ext_depth(inode);
2162 /* probably there is space in leaf? */
2163 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2164 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2167 * There are some space in the leaf tree, no
2168 * need to account for leaf block credit
2170 * bitmaps and block group descriptor blocks
2171 * and other metadata blocks still need to be
2174 /* 1 bitmap, 1 block group descriptor */
2175 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2180 return ext4_chunk_trans_blocks(inode, nrblocks);
2184 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2186 * if nrblocks are fit in a single extent (chunk flag is 1), then
2187 * in the worse case, each tree level index/leaf need to be changed
2188 * if the tree split due to insert a new extent, then the old tree
2189 * index/leaf need to be updated too
2191 * If the nrblocks are discontiguous, they could cause
2192 * the whole tree split more than once, but this is really rare.
2194 int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
2197 int depth = ext_depth(inode);
2207 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2208 struct ext4_extent *ex,
2209 ext4_fsblk_t *partial_cluster,
2210 ext4_lblk_t from, ext4_lblk_t to)
2212 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2213 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2215 int flags = EXT4_FREE_BLOCKS_FORGET;
2217 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2218 flags |= EXT4_FREE_BLOCKS_METADATA;
2220 * For bigalloc file systems, we never free a partial cluster
2221 * at the beginning of the extent. Instead, we make a note
2222 * that we tried freeing the cluster, and check to see if we
2223 * need to free it on a subsequent call to ext4_remove_blocks,
2224 * or at the end of the ext4_truncate() operation.
2226 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2228 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
2230 * If we have a partial cluster, and it's different from the
2231 * cluster of the last block, we need to explicitly free the
2232 * partial cluster here.
2234 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2235 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2236 ext4_free_blocks(handle, inode, NULL,
2237 EXT4_C2B(sbi, *partial_cluster),
2238 sbi->s_cluster_ratio, flags);
2239 *partial_cluster = 0;
2242 #ifdef EXTENTS_STATS
2244 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2245 spin_lock(&sbi->s_ext_stats_lock);
2246 sbi->s_ext_blocks += ee_len;
2247 sbi->s_ext_extents++;
2248 if (ee_len < sbi->s_ext_min)
2249 sbi->s_ext_min = ee_len;
2250 if (ee_len > sbi->s_ext_max)
2251 sbi->s_ext_max = ee_len;
2252 if (ext_depth(inode) > sbi->s_depth_max)
2253 sbi->s_depth_max = ext_depth(inode);
2254 spin_unlock(&sbi->s_ext_stats_lock);
2257 if (from >= le32_to_cpu(ex->ee_block)
2258 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2262 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2263 pblk = ext4_ext_pblock(ex) + ee_len - num;
2264 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2265 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2267 * If the block range to be freed didn't start at the
2268 * beginning of a cluster, and we removed the entire
2269 * extent, save the partial cluster here, since we
2270 * might need to delete if we determine that the
2271 * truncate operation has removed all of the blocks in
2274 if (pblk & (sbi->s_cluster_ratio - 1) &&
2276 *partial_cluster = EXT4_B2C(sbi, pblk);
2278 *partial_cluster = 0;
2279 } else if (from == le32_to_cpu(ex->ee_block)
2280 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2286 start = ext4_ext_pblock(ex);
2288 ext_debug("free first %u blocks starting %llu\n", num, start);
2289 ext4_free_blocks(handle, inode, NULL, start, num, flags);
2292 printk(KERN_INFO "strange request: removal(2) "
2293 "%u-%u from %u:%u\n",
2294 from, to, le32_to_cpu(ex->ee_block), ee_len);
2301 * ext4_ext_rm_leaf() Removes the extents associated with the
2302 * blocks appearing between "start" and "end", and splits the extents
2303 * if "start" and "end" appear in the same extent
2305 * @handle: The journal handle
2306 * @inode: The files inode
2307 * @path: The path to the leaf
2308 * @start: The first block to remove
2309 * @end: The last block to remove
2312 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2313 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2314 ext4_lblk_t start, ext4_lblk_t end)
2316 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2317 int err = 0, correct_index = 0;
2318 int depth = ext_depth(inode), credits;
2319 struct ext4_extent_header *eh;
2322 ext4_lblk_t ex_ee_block;
2323 unsigned short ex_ee_len;
2324 unsigned uninitialized = 0;
2325 struct ext4_extent *ex;
2327 /* the header must be checked already in ext4_ext_remove_space() */
2328 ext_debug("truncate since %u in leaf to %u\n", start, end);
2329 if (!path[depth].p_hdr)
2330 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2331 eh = path[depth].p_hdr;
2332 if (unlikely(path[depth].p_hdr == NULL)) {
2333 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2336 /* find where to start removing */
2337 ex = EXT_LAST_EXTENT(eh);
2339 ex_ee_block = le32_to_cpu(ex->ee_block);
2340 ex_ee_len = ext4_ext_get_actual_len(ex);
2342 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2344 while (ex >= EXT_FIRST_EXTENT(eh) &&
2345 ex_ee_block + ex_ee_len > start) {
2347 if (ext4_ext_is_uninitialized(ex))
2352 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2353 uninitialized, ex_ee_len);
2354 path[depth].p_ext = ex;
2356 a = ex_ee_block > start ? ex_ee_block : start;
2357 b = ex_ee_block+ex_ee_len - 1 < end ?
2358 ex_ee_block+ex_ee_len - 1 : end;
2360 ext_debug(" border %u:%u\n", a, b);
2362 /* If this extent is beyond the end of the hole, skip it */
2363 if (end < ex_ee_block) {
2365 ex_ee_block = le32_to_cpu(ex->ee_block);
2366 ex_ee_len = ext4_ext_get_actual_len(ex);
2368 } else if (b != ex_ee_block + ex_ee_len - 1) {
2369 EXT4_ERROR_INODE(inode,
2370 "can not handle truncate %u:%u "
2372 start, end, ex_ee_block,
2373 ex_ee_block + ex_ee_len - 1);
2376 } else if (a != ex_ee_block) {
2377 /* remove tail of the extent */
2378 num = a - ex_ee_block;
2380 /* remove whole extent: excellent! */
2384 * 3 for leaf, sb, and inode plus 2 (bmap and group
2385 * descriptor) for each block group; assume two block
2386 * groups plus ex_ee_len/blocks_per_block_group for
2389 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2390 if (ex == EXT_FIRST_EXTENT(eh)) {
2392 credits += (ext_depth(inode)) + 1;
2394 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2396 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2400 err = ext4_ext_get_access(handle, inode, path + depth);
2404 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2410 /* this extent is removed; mark slot entirely unused */
2411 ext4_ext_store_pblock(ex, 0);
2413 ex->ee_len = cpu_to_le16(num);
2415 * Do not mark uninitialized if all the blocks in the
2416 * extent have been removed.
2418 if (uninitialized && num)
2419 ext4_ext_mark_uninitialized(ex);
2421 * If the extent was completely released,
2422 * we need to remove it from the leaf
2425 if (end != EXT_MAX_BLOCKS - 1) {
2427 * For hole punching, we need to scoot all the
2428 * extents up when an extent is removed so that
2429 * we dont have blank extents in the middle
2431 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2432 sizeof(struct ext4_extent));
2434 /* Now get rid of the one at the end */
2435 memset(EXT_LAST_EXTENT(eh), 0,
2436 sizeof(struct ext4_extent));
2438 le16_add_cpu(&eh->eh_entries, -1);
2440 *partial_cluster = 0;
2442 err = ext4_ext_dirty(handle, inode, path + depth);
2446 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
2447 ext4_ext_pblock(ex));
2449 ex_ee_block = le32_to_cpu(ex->ee_block);
2450 ex_ee_len = ext4_ext_get_actual_len(ex);
2453 if (correct_index && eh->eh_entries)
2454 err = ext4_ext_correct_indexes(handle, inode, path);
2457 * If there is still a entry in the leaf node, check to see if
2458 * it references the partial cluster. This is the only place
2459 * where it could; if it doesn't, we can free the cluster.
2461 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2462 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2463 *partial_cluster)) {
2464 int flags = EXT4_FREE_BLOCKS_FORGET;
2466 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2467 flags |= EXT4_FREE_BLOCKS_METADATA;
2469 ext4_free_blocks(handle, inode, NULL,
2470 EXT4_C2B(sbi, *partial_cluster),
2471 sbi->s_cluster_ratio, flags);
2472 *partial_cluster = 0;
2475 /* if this leaf is free, then we should
2476 * remove it from index block above */
2477 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2478 err = ext4_ext_rm_idx(handle, inode, path + depth);
2485 * ext4_ext_more_to_rm:
2486 * returns 1 if current index has to be freed (even partial)
2489 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2491 BUG_ON(path->p_idx == NULL);
2493 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2497 * if truncate on deeper level happened, it wasn't partial,
2498 * so we have to consider current index for truncation
2500 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2505 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2508 struct super_block *sb = inode->i_sb;
2509 int depth = ext_depth(inode);
2510 struct ext4_ext_path *path;
2511 ext4_fsblk_t partial_cluster = 0;
2515 ext_debug("truncate since %u to %u\n", start, end);
2517 /* probably first extent we're gonna free will be last in block */
2518 handle = ext4_journal_start(inode, depth + 1);
2520 return PTR_ERR(handle);
2523 ext4_ext_invalidate_cache(inode);
2525 trace_ext4_ext_remove_space(inode, start, depth);
2528 * Check if we are removing extents inside the extent tree. If that
2529 * is the case, we are going to punch a hole inside the extent tree
2530 * so we have to check whether we need to split the extent covering
2531 * the last block to remove so we can easily remove the part of it
2532 * in ext4_ext_rm_leaf().
2534 if (end < EXT_MAX_BLOCKS - 1) {
2535 struct ext4_extent *ex;
2536 ext4_lblk_t ee_block;
2538 /* find extent for this block */
2539 path = ext4_ext_find_extent(inode, end, NULL);
2541 ext4_journal_stop(handle);
2542 return PTR_ERR(path);
2544 depth = ext_depth(inode);
2545 ex = path[depth].p_ext;
2549 ee_block = le32_to_cpu(ex->ee_block);
2552 * See if the last block is inside the extent, if so split
2553 * the extent at 'end' block so we can easily remove the
2554 * tail of the first part of the split extent in
2555 * ext4_ext_rm_leaf().
2557 if (end >= ee_block &&
2558 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2561 if (ext4_ext_is_uninitialized(ex))
2562 split_flag = EXT4_EXT_MARK_UNINIT1 |
2563 EXT4_EXT_MARK_UNINIT2;
2566 * Split the extent in two so that 'end' is the last
2567 * block in the first new extent
2569 err = ext4_split_extent_at(handle, inode, path,
2570 end + 1, split_flag,
2571 EXT4_GET_BLOCKS_PRE_IO |
2572 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
2577 ext4_ext_drop_refs(path);
2583 * We start scanning from right side, freeing all the blocks
2584 * after i_size and walking into the tree depth-wise.
2586 depth = ext_depth(inode);
2587 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2589 ext4_journal_stop(handle);
2592 path[0].p_depth = depth;
2593 path[0].p_hdr = ext_inode_hdr(inode);
2595 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2601 while (i >= 0 && err == 0) {
2603 /* this is leaf block */
2604 err = ext4_ext_rm_leaf(handle, inode, path,
2605 &partial_cluster, start,
2607 /* root level has p_bh == NULL, brelse() eats this */
2608 brelse(path[i].p_bh);
2609 path[i].p_bh = NULL;
2614 /* this is index block */
2615 if (!path[i].p_hdr) {
2616 ext_debug("initialize header\n");
2617 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2620 if (!path[i].p_idx) {
2621 /* this level hasn't been touched yet */
2622 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2623 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2624 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2626 le16_to_cpu(path[i].p_hdr->eh_entries));
2628 /* we were already here, see at next index */
2632 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2633 i, EXT_FIRST_INDEX(path[i].p_hdr),
2635 if (ext4_ext_more_to_rm(path + i)) {
2636 struct buffer_head *bh;
2637 /* go to the next level */
2638 ext_debug("move to level %d (block %llu)\n",
2639 i + 1, ext4_idx_pblock(path[i].p_idx));
2640 memset(path + i + 1, 0, sizeof(*path));
2641 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
2643 /* should we reset i_size? */
2647 if (WARN_ON(i + 1 > depth)) {
2651 if (ext4_ext_check(inode, ext_block_hdr(bh),
2656 path[i + 1].p_bh = bh;
2658 /* save actual number of indexes since this
2659 * number is changed at the next iteration */
2660 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2663 /* we finished processing this index, go up */
2664 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2665 /* index is empty, remove it;
2666 * handle must be already prepared by the
2667 * truncatei_leaf() */
2668 err = ext4_ext_rm_idx(handle, inode, path + i);
2670 /* root level has p_bh == NULL, brelse() eats this */
2671 brelse(path[i].p_bh);
2672 path[i].p_bh = NULL;
2674 ext_debug("return to level %d\n", i);
2678 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2679 path->p_hdr->eh_entries);
2681 /* If we still have something in the partial cluster and we have removed
2682 * even the first extent, then we should free the blocks in the partial
2683 * cluster as well. */
2684 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2685 int flags = EXT4_FREE_BLOCKS_FORGET;
2687 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2688 flags |= EXT4_FREE_BLOCKS_METADATA;
2690 ext4_free_blocks(handle, inode, NULL,
2691 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2692 EXT4_SB(sb)->s_cluster_ratio, flags);
2693 partial_cluster = 0;
2696 /* TODO: flexible tree reduction should be here */
2697 if (path->p_hdr->eh_entries == 0) {
2699 * truncate to zero freed all the tree,
2700 * so we need to correct eh_depth
2702 err = ext4_ext_get_access(handle, inode, path);
2704 ext_inode_hdr(inode)->eh_depth = 0;
2705 ext_inode_hdr(inode)->eh_max =
2706 cpu_to_le16(ext4_ext_space_root(inode, 0));
2707 err = ext4_ext_dirty(handle, inode, path);
2711 ext4_ext_drop_refs(path);
2715 ext4_journal_stop(handle);
2721 * called at mount time
2723 void ext4_ext_init(struct super_block *sb)
2726 * possible initialization would be here
2729 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2730 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2731 printk(KERN_INFO "EXT4-fs: file extents enabled"
2732 #ifdef AGGRESSIVE_TEST
2733 ", aggressive tests"
2735 #ifdef CHECK_BINSEARCH
2738 #ifdef EXTENTS_STATS
2743 #ifdef EXTENTS_STATS
2744 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2745 EXT4_SB(sb)->s_ext_min = 1 << 30;
2746 EXT4_SB(sb)->s_ext_max = 0;
2752 * called at umount time
2754 void ext4_ext_release(struct super_block *sb)
2756 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2759 #ifdef EXTENTS_STATS
2760 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2761 struct ext4_sb_info *sbi = EXT4_SB(sb);
2762 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2763 sbi->s_ext_blocks, sbi->s_ext_extents,
2764 sbi->s_ext_blocks / sbi->s_ext_extents);
2765 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2766 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2771 /* FIXME!! we need to try to merge to left or right after zero-out */
2772 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2774 ext4_fsblk_t ee_pblock;
2775 unsigned int ee_len;
2778 ee_len = ext4_ext_get_actual_len(ex);
2779 ee_pblock = ext4_ext_pblock(ex);
2781 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
2789 * ext4_split_extent_at() splits an extent at given block.
2791 * @handle: the journal handle
2792 * @inode: the file inode
2793 * @path: the path to the extent
2794 * @split: the logical block where the extent is splitted.
2795 * @split_flags: indicates if the extent could be zeroout if split fails, and
2796 * the states(init or uninit) of new extents.
2797 * @flags: flags used to insert new extent to extent tree.
2800 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2801 * of which are deterimined by split_flag.
2803 * There are two cases:
2804 * a> the extent are splitted into two extent.
2805 * b> split is not needed, and just mark the extent.
2807 * return 0 on success.
2809 static int ext4_split_extent_at(handle_t *handle,
2810 struct inode *inode,
2811 struct ext4_ext_path *path,
2816 ext4_fsblk_t newblock;
2817 ext4_lblk_t ee_block;
2818 struct ext4_extent *ex, newex, orig_ex;
2819 struct ext4_extent *ex2 = NULL;
2820 unsigned int ee_len, depth;
2823 ext_debug("ext4_split_extents_at: inode %lu, logical"
2824 "block %llu\n", inode->i_ino, (unsigned long long)split);
2826 ext4_ext_show_leaf(inode, path);
2828 depth = ext_depth(inode);
2829 ex = path[depth].p_ext;
2830 ee_block = le32_to_cpu(ex->ee_block);
2831 ee_len = ext4_ext_get_actual_len(ex);
2832 newblock = split - ee_block + ext4_ext_pblock(ex);
2834 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2836 err = ext4_ext_get_access(handle, inode, path + depth);
2840 if (split == ee_block) {
2842 * case b: block @split is the block that the extent begins with
2843 * then we just change the state of the extent, and splitting
2846 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2847 ext4_ext_mark_uninitialized(ex);
2849 ext4_ext_mark_initialized(ex);
2851 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2852 ext4_ext_try_to_merge(inode, path, ex);
2854 err = ext4_ext_dirty(handle, inode, path + depth);
2859 memcpy(&orig_ex, ex, sizeof(orig_ex));
2860 ex->ee_len = cpu_to_le16(split - ee_block);
2861 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2862 ext4_ext_mark_uninitialized(ex);
2865 * path may lead to new leaf, not to original leaf any more
2866 * after ext4_ext_insert_extent() returns,
2868 err = ext4_ext_dirty(handle, inode, path + depth);
2870 goto fix_extent_len;
2873 ex2->ee_block = cpu_to_le32(split);
2874 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2875 ext4_ext_store_pblock(ex2, newblock);
2876 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2877 ext4_ext_mark_uninitialized(ex2);
2879 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2880 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2881 err = ext4_ext_zeroout(inode, &orig_ex);
2883 goto fix_extent_len;
2884 /* update the extent length and mark as initialized */
2885 ex->ee_len = cpu_to_le32(ee_len);
2886 ext4_ext_try_to_merge(inode, path, ex);
2887 err = ext4_ext_dirty(handle, inode, path + depth);
2890 goto fix_extent_len;
2893 ext4_ext_show_leaf(inode, path);
2897 ex->ee_len = orig_ex.ee_len;
2898 ext4_ext_dirty(handle, inode, path + depth);
2903 * ext4_split_extents() splits an extent and mark extent which is covered
2904 * by @map as split_flags indicates
2906 * It may result in splitting the extent into multiple extents (upto three)
2907 * There are three possibilities:
2908 * a> There is no split required
2909 * b> Splits in two extents: Split is happening at either end of the extent
2910 * c> Splits in three extents: Somone is splitting in middle of the extent
2913 static int ext4_split_extent(handle_t *handle,
2914 struct inode *inode,
2915 struct ext4_ext_path *path,
2916 struct ext4_map_blocks *map,
2920 ext4_lblk_t ee_block;
2921 struct ext4_extent *ex;
2922 unsigned int ee_len, depth;
2925 int split_flag1, flags1;
2927 depth = ext_depth(inode);
2928 ex = path[depth].p_ext;
2929 ee_block = le32_to_cpu(ex->ee_block);
2930 ee_len = ext4_ext_get_actual_len(ex);
2931 uninitialized = ext4_ext_is_uninitialized(ex);
2933 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2934 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2935 EXT4_EXT_MAY_ZEROOUT : 0;
2936 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2938 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2939 EXT4_EXT_MARK_UNINIT2;
2940 err = ext4_split_extent_at(handle, inode, path,
2941 map->m_lblk + map->m_len, split_flag1, flags1);
2946 ext4_ext_drop_refs(path);
2947 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2949 return PTR_ERR(path);
2951 if (map->m_lblk >= ee_block) {
2952 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2953 EXT4_EXT_MAY_ZEROOUT : 0;
2955 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2956 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2957 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2958 err = ext4_split_extent_at(handle, inode, path,
2959 map->m_lblk, split_flag1, flags);
2964 ext4_ext_show_leaf(inode, path);
2966 return err ? err : map->m_len;
2969 #define EXT4_EXT_ZERO_LEN 7
2971 * This function is called by ext4_ext_map_blocks() if someone tries to write
2972 * to an uninitialized extent. It may result in splitting the uninitialized
2973 * extent into multiple extents (up to three - one initialized and two
2975 * There are three possibilities:
2976 * a> There is no split required: Entire extent should be initialized
2977 * b> Splits in two extents: Write is happening at either end of the extent
2978 * c> Splits in three extents: Somone is writing in middle of the extent
2981 * - The extent pointed to by 'path' is uninitialized.
2982 * - The extent pointed to by 'path' contains a superset
2983 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
2985 * Post-conditions on success:
2986 * - the returned value is the number of blocks beyond map->l_lblk
2987 * that are allocated and initialized.
2988 * It is guaranteed to be >= map->m_len.
2990 static int ext4_ext_convert_to_initialized(handle_t *handle,
2991 struct inode *inode,
2992 struct ext4_map_blocks *map,
2993 struct ext4_ext_path *path)
2995 struct ext4_extent_header *eh;
2996 struct ext4_map_blocks split_map;
2997 struct ext4_extent zero_ex;
2998 struct ext4_extent *ex;
2999 ext4_lblk_t ee_block, eof_block;
3000 unsigned int ee_len, depth;
3005 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3006 "block %llu, max_blocks %u\n", inode->i_ino,
3007 (unsigned long long)map->m_lblk, map->m_len);
3009 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3010 inode->i_sb->s_blocksize_bits;
3011 if (eof_block < map->m_lblk + map->m_len)
3012 eof_block = map->m_lblk + map->m_len;
3014 depth = ext_depth(inode);
3015 eh = path[depth].p_hdr;
3016 ex = path[depth].p_ext;
3017 ee_block = le32_to_cpu(ex->ee_block);
3018 ee_len = ext4_ext_get_actual_len(ex);
3019 allocated = ee_len - (map->m_lblk - ee_block);
3021 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3023 /* Pre-conditions */
3024 BUG_ON(!ext4_ext_is_uninitialized(ex));
3025 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
3028 * Attempt to transfer newly initialized blocks from the currently
3029 * uninitialized extent to its left neighbor. This is much cheaper
3030 * than an insertion followed by a merge as those involve costly
3031 * memmove() calls. This is the common case in steady state for
3032 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3035 * Limitations of the current logic:
3036 * - L1: we only deal with writes at the start of the extent.
3037 * The approach could be extended to writes at the end
3038 * of the extent but this scenario was deemed less common.
3039 * - L2: we do not deal with writes covering the whole extent.
3040 * This would require removing the extent if the transfer
3042 * - L3: we only attempt to merge with an extent stored in the
3043 * same extent tree node.
3045 if ((map->m_lblk == ee_block) && /*L1*/
3046 (map->m_len < ee_len) && /*L2*/
3047 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
3048 struct ext4_extent *prev_ex;
3049 ext4_lblk_t prev_lblk;
3050 ext4_fsblk_t prev_pblk, ee_pblk;
3051 unsigned int prev_len, write_len;
3054 prev_lblk = le32_to_cpu(prev_ex->ee_block);
3055 prev_len = ext4_ext_get_actual_len(prev_ex);
3056 prev_pblk = ext4_ext_pblock(prev_ex);
3057 ee_pblk = ext4_ext_pblock(ex);
3058 write_len = map->m_len;
3061 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3062 * upon those conditions:
3063 * - C1: prev_ex is initialized,
3064 * - C2: prev_ex is logically abutting ex,
3065 * - C3: prev_ex is physically abutting ex,
3066 * - C4: prev_ex can receive the additional blocks without
3067 * overflowing the (initialized) length limit.
3069 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3070 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3071 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3072 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3073 err = ext4_ext_get_access(handle, inode, path + depth);
3077 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3080 /* Shift the start of ex by 'write_len' blocks */
3081 ex->ee_block = cpu_to_le32(ee_block + write_len);
3082 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3083 ex->ee_len = cpu_to_le16(ee_len - write_len);
3084 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3086 /* Extend prev_ex by 'write_len' blocks */
3087 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3089 /* Mark the block containing both extents as dirty */
3090 ext4_ext_dirty(handle, inode, path + depth);
3092 /* Update path to point to the right extent */
3093 path[depth].p_ext = prev_ex;
3095 /* Result: number of initialized blocks past m_lblk */
3096 allocated = write_len;
3101 WARN_ON(map->m_lblk < ee_block);
3103 * It is safe to convert extent to initialized via explicit
3104 * zeroout only if extent is fully insde i_size or new_size.
3106 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3108 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
3109 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3110 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3111 err = ext4_ext_zeroout(inode, ex);
3115 err = ext4_ext_get_access(handle, inode, path + depth);
3118 ext4_ext_mark_initialized(ex);
3119 ext4_ext_try_to_merge(inode, path, ex);
3120 err = ext4_ext_dirty(handle, inode, path + depth);
3126 * 1. split the extent into three extents.
3127 * 2. split the extent into two extents, zeroout the first half.
3128 * 3. split the extent into two extents, zeroout the second half.
3129 * 4. split the extent into two extents with out zeroout.
3131 split_map.m_lblk = map->m_lblk;
3132 split_map.m_len = map->m_len;
3134 if (allocated > map->m_len) {
3135 if (allocated <= EXT4_EXT_ZERO_LEN &&
3136 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3139 cpu_to_le32(map->m_lblk);
3140 zero_ex.ee_len = cpu_to_le16(allocated);
3141 ext4_ext_store_pblock(&zero_ex,
3142 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3143 err = ext4_ext_zeroout(inode, &zero_ex);
3146 split_map.m_lblk = map->m_lblk;
3147 split_map.m_len = allocated;
3148 } else if ((map->m_lblk - ee_block + map->m_len <
3149 EXT4_EXT_ZERO_LEN) &&
3150 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3152 if (map->m_lblk != ee_block) {
3153 zero_ex.ee_block = ex->ee_block;
3154 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3156 ext4_ext_store_pblock(&zero_ex,
3157 ext4_ext_pblock(ex));
3158 err = ext4_ext_zeroout(inode, &zero_ex);
3163 split_map.m_lblk = ee_block;
3164 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3165 allocated = map->m_len;
3169 allocated = ext4_split_extent(handle, inode, path,
3170 &split_map, split_flag, 0);
3175 return err ? err : allocated;
3179 * This function is called by ext4_ext_map_blocks() from
3180 * ext4_get_blocks_dio_write() when DIO to write
3181 * to an uninitialized extent.
3183 * Writing to an uninitialized extent may result in splitting the uninitialized
3184 * extent into multiple /initialized uninitialized extents (up to three)
3185 * There are three possibilities:
3186 * a> There is no split required: Entire extent should be uninitialized
3187 * b> Splits in two extents: Write is happening at either end of the extent
3188 * c> Splits in three extents: Somone is writing in middle of the extent
3190 * One of more index blocks maybe needed if the extent tree grow after
3191 * the uninitialized extent split. To prevent ENOSPC occur at the IO
3192 * complete, we need to split the uninitialized extent before DIO submit
3193 * the IO. The uninitialized extent called at this time will be split
3194 * into three uninitialized extent(at most). After IO complete, the part
3195 * being filled will be convert to initialized by the end_io callback function
3196 * via ext4_convert_unwritten_extents().
3198 * Returns the size of uninitialized extent to be written on success.
3200 static int ext4_split_unwritten_extents(handle_t *handle,
3201 struct inode *inode,
3202 struct ext4_map_blocks *map,
3203 struct ext4_ext_path *path,
3206 ext4_lblk_t eof_block;
3207 ext4_lblk_t ee_block;
3208 struct ext4_extent *ex;
3209 unsigned int ee_len;
3210 int split_flag = 0, depth;
3212 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3213 "block %llu, max_blocks %u\n", inode->i_ino,
3214 (unsigned long long)map->m_lblk, map->m_len);
3216 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3217 inode->i_sb->s_blocksize_bits;
3218 if (eof_block < map->m_lblk + map->m_len)
3219 eof_block = map->m_lblk + map->m_len;
3221 * It is safe to convert extent to initialized via explicit
3222 * zeroout only if extent is fully insde i_size or new_size.
3224 depth = ext_depth(inode);
3225 ex = path[depth].p_ext;
3226 ee_block = le32_to_cpu(ex->ee_block);
3227 ee_len = ext4_ext_get_actual_len(ex);
3229 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3230 split_flag |= EXT4_EXT_MARK_UNINIT2;
3232 flags |= EXT4_GET_BLOCKS_PRE_IO;
3233 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
3236 static int ext4_convert_unwritten_extents_endio(handle_t *handle,
3237 struct inode *inode,
3238 struct ext4_ext_path *path)
3240 struct ext4_extent *ex;
3244 depth = ext_depth(inode);
3245 ex = path[depth].p_ext;
3247 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"