2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include <linux/fiemap.h>
44 #include "ext4_jbd2.h"
45 #include "ext4_extents.h"
50 * combine low and high parts of physical block number into ext4_fsblk_t
52 ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
56 block = le32_to_cpu(ex->ee_start_lo);
57 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
65 ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
69 block = le32_to_cpu(ix->ei_leaf_lo);
70 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
79 void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
81 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
82 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
90 static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
92 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
93 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
96 static int ext4_ext_truncate_extend_restart(handle_t *handle,
102 if (!ext4_handle_valid(handle))
104 if (handle->h_buffer_credits > needed)
106 err = ext4_journal_extend(handle, needed);
109 err = ext4_truncate_restart_trans(handle, inode, needed);
121 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
122 struct ext4_ext_path *path)
125 /* path points to block */
126 return ext4_journal_get_write_access(handle, path->p_bh);
128 /* path points to leaf/index in inode body */
129 /* we use in-core data, no need to protect them */
139 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
140 struct ext4_ext_path *path)
144 /* path points to block */
145 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
147 /* path points to leaf/index in inode body */
148 err = ext4_mark_inode_dirty(handle, inode);
153 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
154 struct ext4_ext_path *path,
157 struct ext4_inode_info *ei = EXT4_I(inode);
158 ext4_fsblk_t bg_start;
159 ext4_fsblk_t last_block;
160 ext4_grpblk_t colour;
161 ext4_group_t block_group;
162 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
166 struct ext4_extent *ex;
167 depth = path->p_depth;
169 /* try to predict block placement */
170 ex = path[depth].p_ext;
172 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
174 /* it looks like index is empty;
175 * try to find starting block from index itself */
176 if (path[depth].p_bh)
177 return path[depth].p_bh->b_blocknr;
180 /* OK. use inode's group */
181 block_group = ei->i_block_group;
182 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
184 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
185 * block groups per flexgroup, reserve the first block
186 * group for directories and special files. Regular
187 * files will start at the second block group. This
188 * tends to speed up directory access and improves
191 block_group &= ~(flex_size-1);
192 if (S_ISREG(inode->i_mode))
195 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
196 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
199 * If we are doing delayed allocation, we don't need take
200 * colour into account.
202 if (test_opt(inode->i_sb, DELALLOC))
205 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
206 colour = (current->pid % 16) *
207 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
209 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
210 return bg_start + colour + block;
214 * Allocation for a meta data block
217 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
218 struct ext4_ext_path *path,
219 struct ext4_extent *ex, int *err)
221 ext4_fsblk_t goal, newblock;
223 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
224 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
228 static inline int ext4_ext_space_block(struct inode *inode, int check)
232 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
233 / sizeof(struct ext4_extent);
235 #ifdef AGGRESSIVE_TEST
243 static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
247 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
248 / sizeof(struct ext4_extent_idx);
250 #ifdef AGGRESSIVE_TEST
258 static inline int ext4_ext_space_root(struct inode *inode, int check)
262 size = sizeof(EXT4_I(inode)->i_data);
263 size -= sizeof(struct ext4_extent_header);
264 size /= sizeof(struct ext4_extent);
266 #ifdef AGGRESSIVE_TEST
274 static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
278 size = sizeof(EXT4_I(inode)->i_data);
279 size -= sizeof(struct ext4_extent_header);
280 size /= sizeof(struct ext4_extent_idx);
282 #ifdef AGGRESSIVE_TEST
291 * Calculate the number of metadata blocks needed
292 * to allocate @blocks
293 * Worse case is one block per extent
295 int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock)
297 struct ext4_inode_info *ei = EXT4_I(inode);
300 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
301 / sizeof(struct ext4_extent_idx));
304 * If the new delayed allocation block is contiguous with the
305 * previous da block, it can share index blocks with the
306 * previous block, so we only need to allocate a new index
307 * block every idxs leaf blocks. At ldxs**2 blocks, we need
308 * an additional index block, and at ldxs**3 blocks, yet
309 * another index blocks.
311 if (ei->i_da_metadata_calc_len &&
312 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
313 if ((ei->i_da_metadata_calc_len % idxs) == 0)
315 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
317 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
319 ei->i_da_metadata_calc_len = 0;
321 ei->i_da_metadata_calc_len++;
322 ei->i_da_metadata_calc_last_lblock++;
327 * In the worst case we need a new set of index blocks at
328 * every level of the inode's extent tree.
330 ei->i_da_metadata_calc_len = 1;
331 ei->i_da_metadata_calc_last_lblock = lblock;
332 return ext_depth(inode) + 1;
336 ext4_ext_max_entries(struct inode *inode, int depth)
340 if (depth == ext_depth(inode)) {
342 max = ext4_ext_space_root(inode, 1);
344 max = ext4_ext_space_root_idx(inode, 1);
347 max = ext4_ext_space_block(inode, 1);
349 max = ext4_ext_space_block_idx(inode, 1);
355 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
357 ext4_fsblk_t block = ext_pblock(ext);
358 int len = ext4_ext_get_actual_len(ext);
360 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
363 static int ext4_valid_extent_idx(struct inode *inode,
364 struct ext4_extent_idx *ext_idx)
366 ext4_fsblk_t block = idx_pblock(ext_idx);
368 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
371 static int ext4_valid_extent_entries(struct inode *inode,
372 struct ext4_extent_header *eh,
375 struct ext4_extent *ext;
376 struct ext4_extent_idx *ext_idx;
377 unsigned short entries;
378 if (eh->eh_entries == 0)
381 entries = le16_to_cpu(eh->eh_entries);
385 ext = EXT_FIRST_EXTENT(eh);
387 if (!ext4_valid_extent(inode, ext))
393 ext_idx = EXT_FIRST_INDEX(eh);
395 if (!ext4_valid_extent_idx(inode, ext_idx))
404 static int __ext4_ext_check(const char *function, unsigned int line,
405 struct inode *inode, struct ext4_extent_header *eh,
408 const char *error_msg;
411 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
412 error_msg = "invalid magic";
415 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
416 error_msg = "unexpected eh_depth";
419 if (unlikely(eh->eh_max == 0)) {
420 error_msg = "invalid eh_max";
423 max = ext4_ext_max_entries(inode, depth);
424 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
425 error_msg = "too large eh_max";
428 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
429 error_msg = "invalid eh_entries";
432 if (!ext4_valid_extent_entries(inode, eh, depth)) {
433 error_msg = "invalid extent entries";
439 ext4_error_inode(inode, function, line, 0,
440 "bad header/extent: %s - magic %x, "
441 "entries %u, max %u(%u), depth %u(%u)",
442 error_msg, le16_to_cpu(eh->eh_magic),
443 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
444 max, le16_to_cpu(eh->eh_depth), depth);
449 #define ext4_ext_check(inode, eh, depth) \
450 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
452 int ext4_ext_check_inode(struct inode *inode)
454 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
458 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
460 int k, l = path->p_depth;
463 for (k = 0; k <= l; k++, path++) {
465 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
466 idx_pblock(path->p_idx));
467 } else if (path->p_ext) {
468 ext_debug(" %d:[%d]%d:%llu ",
469 le32_to_cpu(path->p_ext->ee_block),
470 ext4_ext_is_uninitialized(path->p_ext),
471 ext4_ext_get_actual_len(path->p_ext),
472 ext_pblock(path->p_ext));
479 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
481 int depth = ext_depth(inode);
482 struct ext4_extent_header *eh;
483 struct ext4_extent *ex;
489 eh = path[depth].p_hdr;
490 ex = EXT_FIRST_EXTENT(eh);
492 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
494 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
495 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
496 ext4_ext_is_uninitialized(ex),
497 ext4_ext_get_actual_len(ex), ext_pblock(ex));
502 #define ext4_ext_show_path(inode, path)
503 #define ext4_ext_show_leaf(inode, path)
506 void ext4_ext_drop_refs(struct ext4_ext_path *path)
508 int depth = path->p_depth;
511 for (i = 0; i <= depth; i++, path++)
519 * ext4_ext_binsearch_idx:
520 * binary search for the closest index of the given block
521 * the header must be checked before calling this
524 ext4_ext_binsearch_idx(struct inode *inode,
525 struct ext4_ext_path *path, ext4_lblk_t block)
527 struct ext4_extent_header *eh = path->p_hdr;
528 struct ext4_extent_idx *r, *l, *m;
531 ext_debug("binsearch for %u(idx): ", block);
533 l = EXT_FIRST_INDEX(eh) + 1;
534 r = EXT_LAST_INDEX(eh);
537 if (block < le32_to_cpu(m->ei_block))
541 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
542 m, le32_to_cpu(m->ei_block),
543 r, le32_to_cpu(r->ei_block));
547 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
548 idx_pblock(path->p_idx));
550 #ifdef CHECK_BINSEARCH
552 struct ext4_extent_idx *chix, *ix;
555 chix = ix = EXT_FIRST_INDEX(eh);
556 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
558 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
559 printk(KERN_DEBUG "k=%d, ix=0x%p, "
561 ix, EXT_FIRST_INDEX(eh));
562 printk(KERN_DEBUG "%u <= %u\n",
563 le32_to_cpu(ix->ei_block),
564 le32_to_cpu(ix[-1].ei_block));
566 BUG_ON(k && le32_to_cpu(ix->ei_block)
567 <= le32_to_cpu(ix[-1].ei_block));
568 if (block < le32_to_cpu(ix->ei_block))
572 BUG_ON(chix != path->p_idx);
579 * ext4_ext_binsearch:
580 * binary search for closest extent of the given block
581 * the header must be checked before calling this
584 ext4_ext_binsearch(struct inode *inode,
585 struct ext4_ext_path *path, ext4_lblk_t block)
587 struct ext4_extent_header *eh = path->p_hdr;
588 struct ext4_extent *r, *l, *m;
590 if (eh->eh_entries == 0) {
592 * this leaf is empty:
593 * we get such a leaf in split/add case
598 ext_debug("binsearch for %u: ", block);
600 l = EXT_FIRST_EXTENT(eh) + 1;
601 r = EXT_LAST_EXTENT(eh);
605 if (block < le32_to_cpu(m->ee_block))
609 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
610 m, le32_to_cpu(m->ee_block),
611 r, le32_to_cpu(r->ee_block));
615 ext_debug(" -> %d:%llu:[%d]%d ",
616 le32_to_cpu(path->p_ext->ee_block),
617 ext_pblock(path->p_ext),
618 ext4_ext_is_uninitialized(path->p_ext),
619 ext4_ext_get_actual_len(path->p_ext));
621 #ifdef CHECK_BINSEARCH
623 struct ext4_extent *chex, *ex;
626 chex = ex = EXT_FIRST_EXTENT(eh);
627 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
628 BUG_ON(k && le32_to_cpu(ex->ee_block)
629 <= le32_to_cpu(ex[-1].ee_block));
630 if (block < le32_to_cpu(ex->ee_block))
634 BUG_ON(chex != path->p_ext);
640 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
642 struct ext4_extent_header *eh;
644 eh = ext_inode_hdr(inode);
647 eh->eh_magic = EXT4_EXT_MAGIC;
648 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
649 ext4_mark_inode_dirty(handle, inode);
650 ext4_ext_invalidate_cache(inode);
654 struct ext4_ext_path *
655 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
656 struct ext4_ext_path *path)
658 struct ext4_extent_header *eh;
659 struct buffer_head *bh;
660 short int depth, i, ppos = 0, alloc = 0;
662 eh = ext_inode_hdr(inode);
663 depth = ext_depth(inode);
665 /* account possible depth increase */
667 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
670 return ERR_PTR(-ENOMEM);
677 /* walk through the tree */
679 int need_to_validate = 0;
681 ext_debug("depth %d: num %d, max %d\n",
682 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
684 ext4_ext_binsearch_idx(inode, path + ppos, block);
685 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
686 path[ppos].p_depth = i;
687 path[ppos].p_ext = NULL;
689 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
692 if (!bh_uptodate_or_lock(bh)) {
693 if (bh_submit_read(bh) < 0) {
697 /* validate the extent entries */
698 need_to_validate = 1;
700 eh = ext_block_hdr(bh);
702 if (unlikely(ppos > depth)) {
704 EXT4_ERROR_INODE(inode,
705 "ppos %d > depth %d", ppos, depth);
708 path[ppos].p_bh = bh;
709 path[ppos].p_hdr = eh;
712 if (need_to_validate && ext4_ext_check(inode, eh, i))
716 path[ppos].p_depth = i;
717 path[ppos].p_ext = NULL;
718 path[ppos].p_idx = NULL;
721 ext4_ext_binsearch(inode, path + ppos, block);
722 /* if not an empty leaf */
723 if (path[ppos].p_ext)
724 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
726 ext4_ext_show_path(inode, path);
731 ext4_ext_drop_refs(path);
734 return ERR_PTR(-EIO);
738 * ext4_ext_insert_index:
739 * insert new index [@logical;@ptr] into the block at @curp;
740 * check where to insert: before @curp or after @curp
742 int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
743 struct ext4_ext_path *curp,
744 int logical, ext4_fsblk_t ptr)
746 struct ext4_extent_idx *ix;
749 err = ext4_ext_get_access(handle, inode, curp);
753 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
754 EXT4_ERROR_INODE(inode,
755 "logical %d == ei_block %d!",
756 logical, le32_to_cpu(curp->p_idx->ei_block));
759 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
760 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
762 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
763 len = (len - 1) * sizeof(struct ext4_extent_idx);
764 len = len < 0 ? 0 : len;
765 ext_debug("insert new index %d after: %llu. "
766 "move %d from 0x%p to 0x%p\n",
768 (curp->p_idx + 1), (curp->p_idx + 2));
769 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
771 ix = curp->p_idx + 1;
774 len = len * sizeof(struct ext4_extent_idx);
775 len = len < 0 ? 0 : len;
776 ext_debug("insert new index %d before: %llu. "
777 "move %d from 0x%p to 0x%p\n",
779 curp->p_idx, (curp->p_idx + 1));
780 memmove(curp->p_idx + 1, curp->p_idx, len);
784 ix->ei_block = cpu_to_le32(logical);
785 ext4_idx_store_pblock(ix, ptr);
786 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
788 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
789 > le16_to_cpu(curp->p_hdr->eh_max))) {
790 EXT4_ERROR_INODE(inode,
791 "logical %d == ei_block %d!",
792 logical, le32_to_cpu(curp->p_idx->ei_block));
795 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
796 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
800 err = ext4_ext_dirty(handle, inode, curp);
801 ext4_std_error(inode->i_sb, err);
808 * inserts new subtree into the path, using free index entry
810 * - allocates all needed blocks (new leaf and all intermediate index blocks)
811 * - makes decision where to split
812 * - moves remaining extents and index entries (right to the split point)
813 * into the newly allocated blocks
814 * - initializes subtree
816 static int ext4_ext_split(handle_t *handle, struct inode *inode,
817 struct ext4_ext_path *path,
818 struct ext4_extent *newext, int at)
820 struct buffer_head *bh = NULL;
821 int depth = ext_depth(inode);
822 struct ext4_extent_header *neh;
823 struct ext4_extent_idx *fidx;
824 struct ext4_extent *ex;
826 ext4_fsblk_t newblock, oldblock;
828 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
831 /* make decision: where to split? */
832 /* FIXME: now decision is simplest: at current extent */
834 /* if current leaf will be split, then we should use
835 * border from split point */
836 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
837 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
840 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
841 border = path[depth].p_ext[1].ee_block;
842 ext_debug("leaf will be split."
843 " next leaf starts at %d\n",
844 le32_to_cpu(border));
846 border = newext->ee_block;
847 ext_debug("leaf will be added."
848 " next leaf starts at %d\n",
849 le32_to_cpu(border));
853 * If error occurs, then we break processing
854 * and mark filesystem read-only. index won't
855 * be inserted and tree will be in consistent
856 * state. Next mount will repair buffers too.
860 * Get array to track all allocated blocks.
861 * We need this to handle errors and free blocks
864 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
868 /* allocate all needed blocks */
869 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
870 for (a = 0; a < depth - at; a++) {
871 newblock = ext4_ext_new_meta_block(handle, inode, path,
875 ablocks[a] = newblock;
878 /* initialize new leaf */
879 newblock = ablocks[--a];
880 if (unlikely(newblock == 0)) {
881 EXT4_ERROR_INODE(inode, "newblock == 0!");
885 bh = sb_getblk(inode->i_sb, newblock);
892 err = ext4_journal_get_create_access(handle, bh);
896 neh = ext_block_hdr(bh);
898 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
899 neh->eh_magic = EXT4_EXT_MAGIC;
901 ex = EXT_FIRST_EXTENT(neh);
903 /* move remainder of path[depth] to the new leaf */
904 if (unlikely(path[depth].p_hdr->eh_entries !=
905 path[depth].p_hdr->eh_max)) {
906 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
907 path[depth].p_hdr->eh_entries,
908 path[depth].p_hdr->eh_max);
912 /* start copy from next extent */
913 /* TODO: we could do it by single memmove */
916 while (path[depth].p_ext <=
917 EXT_MAX_EXTENT(path[depth].p_hdr)) {
918 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
919 le32_to_cpu(path[depth].p_ext->ee_block),
920 ext_pblock(path[depth].p_ext),
921 ext4_ext_is_uninitialized(path[depth].p_ext),
922 ext4_ext_get_actual_len(path[depth].p_ext),
924 /*memmove(ex++, path[depth].p_ext++,
925 sizeof(struct ext4_extent));
931 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
932 le16_add_cpu(&neh->eh_entries, m);
935 set_buffer_uptodate(bh);
938 err = ext4_handle_dirty_metadata(handle, inode, bh);
944 /* correct old leaf */
946 err = ext4_ext_get_access(handle, inode, path + depth);
949 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
950 err = ext4_ext_dirty(handle, inode, path + depth);
956 /* create intermediate indexes */
958 if (unlikely(k < 0)) {
959 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
964 ext_debug("create %d intermediate indices\n", k);
965 /* insert new index into current index block */
966 /* current depth stored in i var */
970 newblock = ablocks[--a];
971 bh = sb_getblk(inode->i_sb, newblock);
978 err = ext4_journal_get_create_access(handle, bh);
982 neh = ext_block_hdr(bh);
983 neh->eh_entries = cpu_to_le16(1);
984 neh->eh_magic = EXT4_EXT_MAGIC;
985 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
986 neh->eh_depth = cpu_to_le16(depth - i);
987 fidx = EXT_FIRST_INDEX(neh);
988 fidx->ei_block = border;
989 ext4_idx_store_pblock(fidx, oldblock);
991 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
992 i, newblock, le32_to_cpu(border), oldblock);
997 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
998 EXT_MAX_INDEX(path[i].p_hdr));
999 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1000 EXT_LAST_INDEX(path[i].p_hdr))) {
1001 EXT4_ERROR_INODE(inode,
1002 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1003 le32_to_cpu(path[i].p_ext->ee_block));
1007 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
1008 ext_debug("%d: move %d:%llu in new index %llu\n", i,
1009 le32_to_cpu(path[i].p_idx->ei_block),
1010 idx_pblock(path[i].p_idx),
1012 /*memmove(++fidx, path[i].p_idx++,
1013 sizeof(struct ext4_extent_idx));
1015 BUG_ON(neh->eh_entries > neh->eh_max);*/
1020 memmove(++fidx, path[i].p_idx - m,
1021 sizeof(struct ext4_extent_idx) * m);
1022 le16_add_cpu(&neh->eh_entries, m);
1024 set_buffer_uptodate(bh);
1027 err = ext4_handle_dirty_metadata(handle, inode, bh);
1033 /* correct old index */
1035 err = ext4_ext_get_access(handle, inode, path + i);
1038 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1039 err = ext4_ext_dirty(handle, inode, path + i);
1047 /* insert new index */
1048 err = ext4_ext_insert_index(handle, inode, path + at,
1049 le32_to_cpu(border), newblock);
1053 if (buffer_locked(bh))
1059 /* free all allocated blocks in error case */
1060 for (i = 0; i < depth; i++) {
1063 ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
1064 EXT4_FREE_BLOCKS_METADATA);
1073 * ext4_ext_grow_indepth:
1074 * implements tree growing procedure:
1075 * - allocates new block
1076 * - moves top-level data (index block or leaf) into the new block
1077 * - initializes new top-level, creating index that points to the
1078 * just created block
1080 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1081 struct ext4_ext_path *path,
1082 struct ext4_extent *newext)
1084 struct ext4_ext_path *curp = path;
1085 struct ext4_extent_header *neh;
1086 struct buffer_head *bh;
1087 ext4_fsblk_t newblock;
1090 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1094 bh = sb_getblk(inode->i_sb, newblock);
1097 ext4_std_error(inode->i_sb, err);
1102 err = ext4_journal_get_create_access(handle, bh);
1108 /* move top-level index/leaf into new block */
1109 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1111 /* set size of new block */
1112 neh = ext_block_hdr(bh);
1113 /* old root could have indexes or leaves
1114 * so calculate e_max right way */
1115 if (ext_depth(inode))
1116 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1118 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1119 neh->eh_magic = EXT4_EXT_MAGIC;
1120 set_buffer_uptodate(bh);
1123 err = ext4_handle_dirty_metadata(handle, inode, bh);
1127 /* create index in new top-level index: num,max,pointer */
1128 err = ext4_ext_get_access(handle, inode, curp);
1132 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1133 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1134 curp->p_hdr->eh_entries = cpu_to_le16(1);
1135 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
1137 if (path[0].p_hdr->eh_depth)
1138 curp->p_idx->ei_block =
1139 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1141 curp->p_idx->ei_block =
1142 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1143 ext4_idx_store_pblock(curp->p_idx, newblock);
1145 neh = ext_inode_hdr(inode);
1146 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1147 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1148 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1149 idx_pblock(EXT_FIRST_INDEX(neh)));
1151 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1152 err = ext4_ext_dirty(handle, inode, curp);
1160 * ext4_ext_create_new_leaf:
1161 * finds empty index and adds new leaf.
1162 * if no free index is found, then it requests in-depth growing.
1164 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1165 struct ext4_ext_path *path,
1166 struct ext4_extent *newext)
1168 struct ext4_ext_path *curp;
1169 int depth, i, err = 0;
1172 i = depth = ext_depth(inode);
1174 /* walk up to the tree and look for free index entry */
1175 curp = path + depth;
1176 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1181 /* we use already allocated block for index block,
1182 * so subsequent data blocks should be contiguous */
1183 if (EXT_HAS_FREE_INDEX(curp)) {
1184 /* if we found index with free entry, then use that
1185 * entry: create all needed subtree and add new leaf */
1186 err = ext4_ext_split(handle, inode, path, newext, i);
1191 ext4_ext_drop_refs(path);
1192 path = ext4_ext_find_extent(inode,
1193 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1196 err = PTR_ERR(path);
1198 /* tree is full, time to grow in depth */
1199 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1204 ext4_ext_drop_refs(path);
1205 path = ext4_ext_find_extent(inode,
1206 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1209 err = PTR_ERR(path);
1214 * only first (depth 0 -> 1) produces free space;
1215 * in all other cases we have to split the grown tree
1217 depth = ext_depth(inode);
1218 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1219 /* now we need to split */
1229 * search the closest allocated block to the left for *logical
1230 * and returns it at @logical + it's physical address at @phys
1231 * if *logical is the smallest allocated block, the function
1232 * returns 0 at @phys
1233 * return value contains 0 (success) or error code
1236 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1237 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1239 struct ext4_extent_idx *ix;
1240 struct ext4_extent *ex;
1243 if (unlikely(path == NULL)) {
1244 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1247 depth = path->p_depth;
1250 if (depth == 0 && path->p_ext == NULL)
1253 /* usually extent in the path covers blocks smaller
1254 * then *logical, but it can be that extent is the
1255 * first one in the file */
1257 ex = path[depth].p_ext;
1258 ee_len = ext4_ext_get_actual_len(ex);
1259 if (*logical < le32_to_cpu(ex->ee_block)) {
1260 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1261 EXT4_ERROR_INODE(inode,
1262 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1263 *logical, le32_to_cpu(ex->ee_block));
1266 while (--depth >= 0) {
1267 ix = path[depth].p_idx;
1268 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1269 EXT4_ERROR_INODE(inode,
1270 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1271 ix != NULL ? ix->ei_block : 0,
1272 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1273 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1281 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1282 EXT4_ERROR_INODE(inode,
1283 "logical %d < ee_block %d + ee_len %d!",
1284 *logical, le32_to_cpu(ex->ee_block), ee_len);
1288 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1289 *phys = ext_pblock(ex) + ee_len - 1;
1294 * search the closest allocated block to the right for *logical
1295 * and returns it at @logical + it's physical address at @phys
1296 * if *logical is the smallest allocated block, the function
1297 * returns 0 at @phys
1298 * return value contains 0 (success) or error code
1301 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1302 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1304 struct buffer_head *bh = NULL;
1305 struct ext4_extent_header *eh;
1306 struct ext4_extent_idx *ix;
1307 struct ext4_extent *ex;
1309 int depth; /* Note, NOT eh_depth; depth from top of tree */
1312 if (unlikely(path == NULL)) {
1313 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1316 depth = path->p_depth;
1319 if (depth == 0 && path->p_ext == NULL)
1322 /* usually extent in the path covers blocks smaller
1323 * then *logical, but it can be that extent is the
1324 * first one in the file */
1326 ex = path[depth].p_ext;
1327 ee_len = ext4_ext_get_actual_len(ex);
1328 if (*logical < le32_to_cpu(ex->ee_block)) {
1329 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1330 EXT4_ERROR_INODE(inode,
1331 "first_extent(path[%d].p_hdr) != ex",
1335 while (--depth >= 0) {
1336 ix = path[depth].p_idx;
1337 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1338 EXT4_ERROR_INODE(inode,
1339 "ix != EXT_FIRST_INDEX *logical %d!",
1344 *logical = le32_to_cpu(ex->ee_block);
1345 *phys = ext_pblock(ex);
1349 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1350 EXT4_ERROR_INODE(inode,
1351 "logical %d < ee_block %d + ee_len %d!",
1352 *logical, le32_to_cpu(ex->ee_block), ee_len);
1356 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1357 /* next allocated block in this leaf */
1359 *logical = le32_to_cpu(ex->ee_block);
1360 *phys = ext_pblock(ex);
1364 /* go up and search for index to the right */
1365 while (--depth >= 0) {
1366 ix = path[depth].p_idx;
1367 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1371 /* we've gone up to the root and found no index to the right */
1375 /* we've found index to the right, let's
1376 * follow it and find the closest allocated
1377 * block to the right */
1379 block = idx_pblock(ix);
1380 while (++depth < path->p_depth) {
1381 bh = sb_bread(inode->i_sb, block);
1384 eh = ext_block_hdr(bh);
1385 /* subtract from p_depth to get proper eh_depth */
1386 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1390 ix = EXT_FIRST_INDEX(eh);
1391 block = idx_pblock(ix);
1395 bh = sb_bread(inode->i_sb, block);
1398 eh = ext_block_hdr(bh);
1399 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1403 ex = EXT_FIRST_EXTENT(eh);
1404 *logical = le32_to_cpu(ex->ee_block);
1405 *phys = ext_pblock(ex);
1411 * ext4_ext_next_allocated_block:
1412 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1413 * NOTE: it considers block number from index entry as
1414 * allocated block. Thus, index entries have to be consistent
1418 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1422 BUG_ON(path == NULL);
1423 depth = path->p_depth;
1425 if (depth == 0 && path->p_ext == NULL)
1426 return EXT_MAX_BLOCK;
1428 while (depth >= 0) {
1429 if (depth == path->p_depth) {
1431 if (path[depth].p_ext !=
1432 EXT_LAST_EXTENT(path[depth].p_hdr))
1433 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1436 if (path[depth].p_idx !=
1437 EXT_LAST_INDEX(path[depth].p_hdr))
1438 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1443 return EXT_MAX_BLOCK;
1447 * ext4_ext_next_leaf_block:
1448 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1450 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1451 struct ext4_ext_path *path)
1455 BUG_ON(path == NULL);
1456 depth = path->p_depth;
1458 /* zero-tree has no leaf blocks at all */
1460 return EXT_MAX_BLOCK;
1462 /* go to index block */
1465 while (depth >= 0) {
1466 if (path[depth].p_idx !=
1467 EXT_LAST_INDEX(path[depth].p_hdr))
1468 return (ext4_lblk_t)
1469 le32_to_cpu(path[depth].p_idx[1].ei_block);
1473 return EXT_MAX_BLOCK;
1477 * ext4_ext_correct_indexes:
1478 * if leaf gets modified and modified extent is first in the leaf,
1479 * then we have to correct all indexes above.
1480 * TODO: do we need to correct tree in all cases?
1482 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1483 struct ext4_ext_path *path)
1485 struct ext4_extent_header *eh;
1486 int depth = ext_depth(inode);
1487 struct ext4_extent *ex;
1491 eh = path[depth].p_hdr;
1492 ex = path[depth].p_ext;
1494 if (unlikely(ex == NULL || eh == NULL)) {
1495 EXT4_ERROR_INODE(inode,
1496 "ex %p == NULL or eh %p == NULL", ex, eh);
1501 /* there is no tree at all */
1505 if (ex != EXT_FIRST_EXTENT(eh)) {
1506 /* we correct tree if first leaf got modified only */
1511 * TODO: we need correction if border is smaller than current one
1514 border = path[depth].p_ext->ee_block;
1515 err = ext4_ext_get_access(handle, inode, path + k);
1518 path[k].p_idx->ei_block = border;
1519 err = ext4_ext_dirty(handle, inode, path + k);
1524 /* change all left-side indexes */
1525 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1527 err = ext4_ext_get_access(handle, inode, path + k);
1530 path[k].p_idx->ei_block = border;
1531 err = ext4_ext_dirty(handle, inode, path + k);
1540 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1541 struct ext4_extent *ex2)
1543 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1546 * Make sure that either both extents are uninitialized, or
1549 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1552 if (ext4_ext_is_uninitialized(ex1))
1553 max_len = EXT_UNINIT_MAX_LEN;
1555 max_len = EXT_INIT_MAX_LEN;
1557 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1558 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1560 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1561 le32_to_cpu(ex2->ee_block))
1565 * To allow future support for preallocated extents to be added
1566 * as an RO_COMPAT feature, refuse to merge to extents if
1567 * this can result in the top bit of ee_len being set.
1569 if (ext1_ee_len + ext2_ee_len > max_len)
1571 #ifdef AGGRESSIVE_TEST
1572 if (ext1_ee_len >= 4)
1576 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1582 * This function tries to merge the "ex" extent to the next extent in the tree.
1583 * It always tries to merge towards right. If you want to merge towards
1584 * left, pass "ex - 1" as argument instead of "ex".
1585 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1586 * 1 if they got merged.
1588 int ext4_ext_try_to_merge(struct inode *inode,
1589 struct ext4_ext_path *path,
1590 struct ext4_extent *ex)
1592 struct ext4_extent_header *eh;
1593 unsigned int depth, len;
1595 int uninitialized = 0;
1597 depth = ext_depth(inode);
1598 BUG_ON(path[depth].p_hdr == NULL);
1599 eh = path[depth].p_hdr;
1601 while (ex < EXT_LAST_EXTENT(eh)) {
1602 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1604 /* merge with next extent! */
1605 if (ext4_ext_is_uninitialized(ex))
1607 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1608 + ext4_ext_get_actual_len(ex + 1));
1610 ext4_ext_mark_uninitialized(ex);
1612 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1613 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1614 * sizeof(struct ext4_extent);
1615 memmove(ex + 1, ex + 2, len);
1617 le16_add_cpu(&eh->eh_entries, -1);
1619 WARN_ON(eh->eh_entries == 0);
1620 if (!eh->eh_entries)
1621 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
1628 * check if a portion of the "newext" extent overlaps with an
1631 * If there is an overlap discovered, it updates the length of the newext
1632 * such that there will be no overlap, and then returns 1.
1633 * If there is no overlap found, it returns 0.
1635 unsigned int ext4_ext_check_overlap(struct inode *inode,
1636 struct ext4_extent *newext,
1637 struct ext4_ext_path *path)
1640 unsigned int depth, len1;
1641 unsigned int ret = 0;
1643 b1 = le32_to_cpu(newext->ee_block);
1644 len1 = ext4_ext_get_actual_len(newext);
1645 depth = ext_depth(inode);
1646 if (!path[depth].p_ext)
1648 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1651 * get the next allocated block if the extent in the path
1652 * is before the requested block(s)
1655 b2 = ext4_ext_next_allocated_block(path);
1656 if (b2 == EXT_MAX_BLOCK)
1660 /* check for wrap through zero on extent logical start block*/
1661 if (b1 + len1 < b1) {
1662 len1 = EXT_MAX_BLOCK - b1;
1663 newext->ee_len = cpu_to_le16(len1);
1667 /* check for overlap */
1668 if (b1 + len1 > b2) {
1669 newext->ee_len = cpu_to_le16(b2 - b1);
1677 * ext4_ext_insert_extent:
1678 * tries to merge requsted extent into the existing extent or
1679 * inserts requested extent as new one into the tree,
1680 * creating new leaf in the no-space case.
1682 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1683 struct ext4_ext_path *path,
1684 struct ext4_extent *newext, int flag)
1686 struct ext4_extent_header *eh;
1687 struct ext4_extent *ex, *fex;
1688 struct ext4_extent *nearex; /* nearest extent */
1689 struct ext4_ext_path *npath = NULL;
1690 int depth, len, err;
1692 unsigned uninitialized = 0;
1694 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1695 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1698 depth = ext_depth(inode);
1699 ex = path[depth].p_ext;
1700 if (unlikely(path[depth].p_hdr == NULL)) {
1701 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1705 /* try to insert block into found extent and return */
1706 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
1707 && ext4_can_extents_be_merged(inode, ex, newext)) {
1708 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1709 ext4_ext_is_uninitialized(newext),
1710 ext4_ext_get_actual_len(newext),
1711 le32_to_cpu(ex->ee_block),
1712 ext4_ext_is_uninitialized(ex),
1713 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1714 err = ext4_ext_get_access(handle, inode, path + depth);
1719 * ext4_can_extents_be_merged should have checked that either
1720 * both extents are uninitialized, or both aren't. Thus we
1721 * need to check only one of them here.
1723 if (ext4_ext_is_uninitialized(ex))
1725 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1726 + ext4_ext_get_actual_len(newext));
1728 ext4_ext_mark_uninitialized(ex);
1729 eh = path[depth].p_hdr;
1735 depth = ext_depth(inode);
1736 eh = path[depth].p_hdr;
1737 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1740 /* probably next leaf has space for us? */
1741 fex = EXT_LAST_EXTENT(eh);
1742 next = ext4_ext_next_leaf_block(inode, path);
1743 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1744 && next != EXT_MAX_BLOCK) {
1745 ext_debug("next leaf block - %d\n", next);
1746 BUG_ON(npath != NULL);
1747 npath = ext4_ext_find_extent(inode, next, NULL);
1749 return PTR_ERR(npath);
1750 BUG_ON(npath->p_depth != path->p_depth);
1751 eh = npath[depth].p_hdr;
1752 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1753 ext_debug("next leaf isnt full(%d)\n",
1754 le16_to_cpu(eh->eh_entries));
1758 ext_debug("next leaf has no free space(%d,%d)\n",
1759 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1763 * There is no free space in the found leaf.
1764 * We're gonna add a new leaf in the tree.
1766 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1769 depth = ext_depth(inode);
1770 eh = path[depth].p_hdr;
1773 nearex = path[depth].p_ext;
1775 err = ext4_ext_get_access(handle, inode, path + depth);
1780 /* there is no extent in this leaf, create first one */
1781 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1782 le32_to_cpu(newext->ee_block),
1784 ext4_ext_is_uninitialized(newext),
1785 ext4_ext_get_actual_len(newext));
1786 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1787 } else if (le32_to_cpu(newext->ee_block)
1788 > le32_to_cpu(nearex->ee_block)) {
1789 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1790 if (nearex != EXT_LAST_EXTENT(eh)) {
1791 len = EXT_MAX_EXTENT(eh) - nearex;
1792 len = (len - 1) * sizeof(struct ext4_extent);
1793 len = len < 0 ? 0 : len;
1794 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1795 "move %d from 0x%p to 0x%p\n",
1796 le32_to_cpu(newext->ee_block),
1798 ext4_ext_is_uninitialized(newext),
1799 ext4_ext_get_actual_len(newext),
1800 nearex, len, nearex + 1, nearex + 2);
1801 memmove(nearex + 2, nearex + 1, len);
1803 path[depth].p_ext = nearex + 1;
1805 BUG_ON(newext->ee_block == nearex->ee_block);
1806 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1807 len = len < 0 ? 0 : len;
1808 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1809 "move %d from 0x%p to 0x%p\n",
1810 le32_to_cpu(newext->ee_block),
1812 ext4_ext_is_uninitialized(newext),
1813 ext4_ext_get_actual_len(newext),
1814 nearex, len, nearex + 1, nearex + 2);
1815 memmove(nearex + 1, nearex, len);
1816 path[depth].p_ext = nearex;
1819 le16_add_cpu(&eh->eh_entries, 1);
1820 nearex = path[depth].p_ext;
1821 nearex->ee_block = newext->ee_block;
1822 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1823 nearex->ee_len = newext->ee_len;
1826 /* try to merge extents to the right */
1827 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
1828 ext4_ext_try_to_merge(inode, path, nearex);
1830 /* try to merge extents to the left */
1832 /* time to correct all indexes above */
1833 err = ext4_ext_correct_indexes(handle, inode, path);
1837 err = ext4_ext_dirty(handle, inode, path + depth);
1841 ext4_ext_drop_refs(npath);
1844 ext4_ext_invalidate_cache(inode);
1848 int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1849 ext4_lblk_t num, ext_prepare_callback func,
1852 struct ext4_ext_path *path = NULL;
1853 struct ext4_ext_cache cbex;
1854 struct ext4_extent *ex;
1855 ext4_lblk_t next, start = 0, end = 0;
1856 ext4_lblk_t last = block + num;
1857 int depth, exists, err = 0;
1859 BUG_ON(func == NULL);
1860 BUG_ON(inode == NULL);
1862 while (block < last && block != EXT_MAX_BLOCK) {
1864 /* find extent for this block */
1865 down_read(&EXT4_I(inode)->i_data_sem);
1866 path = ext4_ext_find_extent(inode, block, path);
1867 up_read(&EXT4_I(inode)->i_data_sem);
1869 err = PTR_ERR(path);
1874 depth = ext_depth(inode);
1875 if (unlikely(path[depth].p_hdr == NULL)) {
1876 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1880 ex = path[depth].p_ext;
1881 next = ext4_ext_next_allocated_block(path);
1885 /* there is no extent yet, so try to allocate
1886 * all requested space */
1889 } else if (le32_to_cpu(ex->ee_block) > block) {
1890 /* need to allocate space before found extent */
1892 end = le32_to_cpu(ex->ee_block);
1893 if (block + num < end)
1895 } else if (block >= le32_to_cpu(ex->ee_block)
1896 + ext4_ext_get_actual_len(ex)) {
1897 /* need to allocate space after found extent */
1902 } else if (block >= le32_to_cpu(ex->ee_block)) {
1904 * some part of requested space is covered
1908 end = le32_to_cpu(ex->ee_block)
1909 + ext4_ext_get_actual_len(ex);
1910 if (block + num < end)
1916 BUG_ON(end <= start);
1919 cbex.ec_block = start;
1920 cbex.ec_len = end - start;
1922 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1924 cbex.ec_block = le32_to_cpu(ex->ee_block);
1925 cbex.ec_len = ext4_ext_get_actual_len(ex);
1926 cbex.ec_start = ext_pblock(ex);
1927 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1930 if (unlikely(cbex.ec_len == 0)) {
1931 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1935 err = func(inode, path, &cbex, ex, cbdata);
1936 ext4_ext_drop_refs(path);
1941 if (err == EXT_REPEAT)
1943 else if (err == EXT_BREAK) {
1948 if (ext_depth(inode) != depth) {
1949 /* depth was changed. we have to realloc path */
1954 block = cbex.ec_block + cbex.ec_len;
1958 ext4_ext_drop_refs(path);
1966 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1967 __u32 len, ext4_fsblk_t start, int type)
1969 struct ext4_ext_cache *cex;
1971 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1972 cex = &EXT4_I(inode)->i_cached_extent;
1973 cex->ec_type = type;
1974 cex->ec_block = block;
1976 cex->ec_start = start;
1977 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1981 * ext4_ext_put_gap_in_cache:
1982 * calculate boundaries of the gap that the requested block fits into
1983 * and cache this gap
1986 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1989 int depth = ext_depth(inode);
1992 struct ext4_extent *ex;
1994 ex = path[depth].p_ext;
1996 /* there is no extent yet, so gap is [0;-] */
1998 len = EXT_MAX_BLOCK;
1999 ext_debug("cache gap(whole file):");
2000 } else if (block < le32_to_cpu(ex->ee_block)) {
2002 len = le32_to_cpu(ex->ee_block) - block;
2003 ext_debug("cache gap(before): %u [%u:%u]",
2005 le32_to_cpu(ex->ee_block),
2006 ext4_ext_get_actual_len(ex));
2007 } else if (block >= le32_to_cpu(ex->ee_block)
2008 + ext4_ext_get_actual_len(ex)) {
2010 lblock = le32_to_cpu(ex->ee_block)
2011 + ext4_ext_get_actual_len(ex);
2013 next = ext4_ext_next_allocated_block(path);
2014 ext_debug("cache gap(after): [%u:%u] %u",
2015 le32_to_cpu(ex->ee_block),
2016 ext4_ext_get_actual_len(ex),
2018 BUG_ON(next == lblock);
2019 len = next - lblock;
2025 ext_debug(" -> %u:%lu\n", lblock, len);
2026 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
2030 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2031 struct ext4_extent *ex)
2033 struct ext4_ext_cache *cex;
2034 int ret = EXT4_EXT_CACHE_NO;
2037 * We borrow i_block_reservation_lock to protect i_cached_extent
2039 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
2040 cex = &EXT4_I(inode)->i_cached_extent;
2042 /* has cache valid data? */
2043 if (cex->ec_type == EXT4_EXT_CACHE_NO)
2046 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
2047 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
2048 if (in_range(block, cex->ec_block, cex->ec_len)) {
2049 ex->ee_block = cpu_to_le32(cex->ec_block);
2050 ext4_ext_store_pblock(ex, cex->ec_start);
2051 ex->ee_len = cpu_to_le16(cex->ec_len);
2052 ext_debug("%u cached by %u:%u:%llu\n",
2054 cex->ec_block, cex->ec_len, cex->ec_start);
2058 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2064 * removes index from the index block.
2065 * It's used in truncate case only, thus all requests are for
2066 * last index in the block only.
2068 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
2069 struct ext4_ext_path *path)
2074 /* free index block */
2076 leaf = idx_pblock(path->p_idx);
2077 if (unlikely(path->p_hdr->eh_entries == 0)) {
2078 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2081 err = ext4_ext_get_access(handle, inode, path);
2084 le16_add_cpu(&path->p_hdr->eh_entries, -1);
2085 err = ext4_ext_dirty(handle, inode, path);
2088 ext_debug("index is empty, remove it, free block %llu\n", leaf);
2089 ext4_free_blocks(handle, inode, 0, leaf, 1,
2090 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
2095 * ext4_ext_calc_credits_for_single_extent:
2096 * This routine returns max. credits that needed to insert an extent
2097 * to the extent tree.
2098 * When pass the actual path, the caller should calculate credits
2101 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2102 struct ext4_ext_path *path)
2105 int depth = ext_depth(inode);
2108 /* probably there is space in leaf? */
2109 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2110 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2113 * There are some space in the leaf tree, no
2114 * need to account for leaf block credit
2116 * bitmaps and block group descriptor blocks
2117 * and other metadat blocks still need to be
2120 /* 1 bitmap, 1 block group descriptor */
2121 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2126 return ext4_chunk_trans_blocks(inode, nrblocks);
2130 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2132 * if nrblocks are fit in a single extent (chunk flag is 1), then
2133 * in the worse case, each tree level index/leaf need to be changed
2134 * if the tree split due to insert a new extent, then the old tree
2135 * index/leaf need to be updated too
2137 * If the nrblocks are discontiguous, they could cause
2138 * the whole tree split more than once, but this is really rare.
2140 int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
2143 int depth = ext_depth(inode);
2153 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2154 struct ext4_extent *ex,
2155 ext4_lblk_t from, ext4_lblk_t to)
2157 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2158 int flags = EXT4_FREE_BLOCKS_FORGET;
2160 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2161 flags |= EXT4_FREE_BLOCKS_METADATA;
2162 #ifdef EXTENTS_STATS
2164 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2165 spin_lock(&sbi->s_ext_stats_lock);
2166 sbi->s_ext_blocks += ee_len;
2167 sbi->s_ext_extents++;
2168 if (ee_len < sbi->s_ext_min)
2169 sbi->s_ext_min = ee_len;
2170 if (ee_len > sbi->s_ext_max)
2171 sbi->s_ext_max = ee_len;
2172 if (ext_depth(inode) > sbi->s_depth_max)
2173 sbi->s_depth_max = ext_depth(inode);
2174 spin_unlock(&sbi->s_ext_stats_lock);
2177 if (from >= le32_to_cpu(ex->ee_block)
2178 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2183 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2184 start = ext_pblock(ex) + ee_len - num;
2185 ext_debug("free last %u blocks starting %llu\n", num, start);
2186 ext4_free_blocks(handle, inode, 0, start, num, flags);
2187 } else if (from == le32_to_cpu(ex->ee_block)
2188 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2189 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
2190 from, to, le32_to_cpu(ex->ee_block), ee_len);
2192 printk(KERN_INFO "strange request: removal(2) "
2193 "%u-%u from %u:%u\n",
2194 from, to, le32_to_cpu(ex->ee_block), ee_len);
2200 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2201 struct ext4_ext_path *path, ext4_lblk_t start)
2203 int err = 0, correct_index = 0;
2204 int depth = ext_depth(inode), credits;
2205 struct ext4_extent_header *eh;
2206 ext4_lblk_t a, b, block;
2208 ext4_lblk_t ex_ee_block;
2209 unsigned short ex_ee_len;
2210 unsigned uninitialized = 0;
2211 struct ext4_extent *ex;
2213 /* the header must be checked already in ext4_ext_remove_space() */
2214 ext_debug("truncate since %u in leaf\n", start);
2215 if (!path[depth].p_hdr)
2216 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2217 eh = path[depth].p_hdr;
2218 if (unlikely(path[depth].p_hdr == NULL)) {
2219 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2222 /* find where to start removing */
2223 ex = EXT_LAST_EXTENT(eh);
2225 ex_ee_block = le32_to_cpu(ex->ee_block);
2226 ex_ee_len = ext4_ext_get_actual_len(ex);
2228 while (ex >= EXT_FIRST_EXTENT(eh) &&
2229 ex_ee_block + ex_ee_len > start) {
2231 if (ext4_ext_is_uninitialized(ex))
2236 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2237 uninitialized, ex_ee_len);
2238 path[depth].p_ext = ex;
2240 a = ex_ee_block > start ? ex_ee_block : start;
2241 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2242 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2244 ext_debug(" border %u:%u\n", a, b);
2246 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2250 } else if (a != ex_ee_block) {
2251 /* remove tail of the extent */
2252 block = ex_ee_block;
2254 } else if (b != ex_ee_block + ex_ee_len - 1) {
2255 /* remove head of the extent */
2258 /* there is no "make a hole" API yet */
2261 /* remove whole extent: excellent! */
2262 block = ex_ee_block;
2264 BUG_ON(a != ex_ee_block);
2265 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2269 * 3 for leaf, sb, and inode plus 2 (bmap and group
2270 * descriptor) for each block group; assume two block
2271 * groups plus ex_ee_len/blocks_per_block_group for
2274 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2275 if (ex == EXT_FIRST_EXTENT(eh)) {
2277 credits += (ext_depth(inode)) + 1;
2279 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2281 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2285 err = ext4_ext_get_access(handle, inode, path + depth);
2289 err = ext4_remove_blocks(handle, inode, ex, a, b);
2294 /* this extent is removed; mark slot entirely unused */
2295 ext4_ext_store_pblock(ex, 0);
2296 le16_add_cpu(&eh->eh_entries, -1);
2299 ex->ee_block = cpu_to_le32(block);
2300 ex->ee_len = cpu_to_le16(num);
2302 * Do not mark uninitialized if all the blocks in the
2303 * extent have been removed.
2305 if (uninitialized && num)
2306 ext4_ext_mark_uninitialized(ex);
2308 err = ext4_ext_dirty(handle, inode, path + depth);
2312 ext_debug("new extent: %u:%u:%llu\n", block, num,
2315 ex_ee_block = le32_to_cpu(ex->ee_block);
2316 ex_ee_len = ext4_ext_get_actual_len(ex);
2319 if (correct_index && eh->eh_entries)
2320 err = ext4_ext_correct_indexes(handle, inode, path);
2322 /* if this leaf is free, then we should
2323 * remove it from index block above */
2324 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2325 err = ext4_ext_rm_idx(handle, inode, path + depth);
2332 * ext4_ext_more_to_rm:
2333 * returns 1 if current index has to be freed (even partial)
2336 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2338 BUG_ON(path->p_idx == NULL);
2340 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2344 * if truncate on deeper level happened, it wasn't partial,
2345 * so we have to consider current index for truncation
2347 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2352 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2354 struct super_block *sb = inode->i_sb;
2355 int depth = ext_depth(inode);
2356 struct ext4_ext_path *path;
2360 ext_debug("truncate since %u\n", start);
2362 /* probably first extent we're gonna free will be last in block */
2363 handle = ext4_journal_start(inode, depth + 1);
2365 return PTR_ERR(handle);
2368 ext4_ext_invalidate_cache(inode);
2371 * We start scanning from right side, freeing all the blocks
2372 * after i_size and walking into the tree depth-wise.
2374 depth = ext_depth(inode);
2375 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2377 ext4_journal_stop(handle);
2380 path[0].p_depth = depth;
2381 path[0].p_hdr = ext_inode_hdr(inode);
2382 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2388 while (i >= 0 && err == 0) {
2390 /* this is leaf block */
2391 err = ext4_ext_rm_leaf(handle, inode, path, start);
2392 /* root level has p_bh == NULL, brelse() eats this */
2393 brelse(path[i].p_bh);
2394 path[i].p_bh = NULL;
2399 /* this is index block */
2400 if (!path[i].p_hdr) {
2401 ext_debug("initialize header\n");
2402 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2405 if (!path[i].p_idx) {
2406 /* this level hasn't been touched yet */
2407 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2408 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2409 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2411 le16_to_cpu(path[i].p_hdr->eh_entries));
2413 /* we were already here, see at next index */
2417 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2418 i, EXT_FIRST_INDEX(path[i].p_hdr),
2420 if (ext4_ext_more_to_rm(path + i)) {
2421 struct buffer_head *bh;
2422 /* go to the next level */
2423 ext_debug("move to level %d (block %llu)\n",
2424 i + 1, idx_pblock(path[i].p_idx));
2425 memset(path + i + 1, 0, sizeof(*path));
2426 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2428 /* should we reset i_size? */
2432 if (WARN_ON(i + 1 > depth)) {
2436 if (ext4_ext_check(inode, ext_block_hdr(bh),
2441 path[i + 1].p_bh = bh;
2443 /* save actual number of indexes since this
2444 * number is changed at the next iteration */
2445 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2448 /* we finished processing this index, go up */
2449 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2450 /* index is empty, remove it;
2451 * handle must be already prepared by the
2452 * truncatei_leaf() */
2453 err = ext4_ext_rm_idx(handle, inode, path + i);
2455 /* root level has p_bh == NULL, brelse() eats this */
2456 brelse(path[i].p_bh);
2457 path[i].p_bh = NULL;
2459 ext_debug("return to level %d\n", i);
2463 /* TODO: flexible tree reduction should be here */
2464 if (path->p_hdr->eh_entries == 0) {
2466 * truncate to zero freed all the tree,
2467 * so we need to correct eh_depth
2469 err = ext4_ext_get_access(handle, inode, path);
2471 ext_inode_hdr(inode)->eh_depth = 0;
2472 ext_inode_hdr(inode)->eh_max =
2473 cpu_to_le16(ext4_ext_space_root(inode, 0));
2474 err = ext4_ext_dirty(handle, inode, path);
2478 ext4_ext_drop_refs(path);
2482 ext4_journal_stop(handle);
2488 * called at mount time
2490 void ext4_ext_init(struct super_block *sb)
2493 * possible initialization would be here
2496 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2497 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2498 printk(KERN_INFO "EXT4-fs: file extents enabled");
2499 #ifdef AGGRESSIVE_TEST
2500 printk(", aggressive tests");
2502 #ifdef CHECK_BINSEARCH
2503 printk(", check binsearch");
2505 #ifdef EXTENTS_STATS
2510 #ifdef EXTENTS_STATS
2511 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2512 EXT4_SB(sb)->s_ext_min = 1 << 30;
2513 EXT4_SB(sb)->s_ext_max = 0;
2519 * called at umount time
2521 void ext4_ext_release(struct super_block *sb)
2523 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2526 #ifdef EXTENTS_STATS
2527 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2528 struct ext4_sb_info *sbi = EXT4_SB(sb);
2529 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2530 sbi->s_ext_blocks, sbi->s_ext_extents,
2531 sbi->s_ext_blocks / sbi->s_ext_extents);
2532 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2533 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2538 /* FIXME!! we need to try to merge to left or right after zero-out */
2539 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2541 ext4_fsblk_t ee_pblock;
2542 unsigned int ee_len;
2545 ee_len = ext4_ext_get_actual_len(ex);
2546 ee_pblock = ext_pblock(ex);
2548 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len,
2549 GFP_NOFS, BLKDEV_IFL_WAIT);
2556 #define EXT4_EXT_ZERO_LEN 7
2558 * This function is called by ext4_ext_map_blocks() if someone tries to write
2559 * to an uninitialized extent. It may result in splitting the uninitialized
2560 * extent into multiple extents (upto three - one initialized and two
2562 * There are three possibilities:
2563 * a> There is no split required: Entire extent should be initialized
2564 * b> Splits in two extents: Write is happening at either end of the extent
2565 * c> Splits in three extents: Somone is writing in middle of the extent
2567 static int ext4_ext_convert_to_initialized(handle_t *handle,
2568 struct inode *inode,
2569 struct ext4_map_blocks *map,
2570 struct ext4_ext_path *path)
2572 struct ext4_extent *ex, newex, orig_ex;
2573 struct ext4_extent *ex1 = NULL;
2574 struct ext4_extent *ex2 = NULL;
2575 struct ext4_extent *ex3 = NULL;
2576 struct ext4_extent_header *eh;
2577 ext4_lblk_t ee_block, eof_block;
2578 unsigned int allocated, ee_len, depth;
2579 ext4_fsblk_t newblock;
2584 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2585 "block %llu, max_blocks %u\n", inode->i_ino,
2586 (unsigned long long)map->m_lblk, map->m_len);
2588 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2589 inode->i_sb->s_blocksize_bits;
2590 if (eof_block < map->m_lblk + map->m_len)
2591 eof_block = map->m_lblk + map->m_len;
2593 depth = ext_depth(inode);
2594 eh = path[depth].p_hdr;
2595 ex = path[depth].p_ext;
2596 ee_block = le32_to_cpu(ex->ee_block);
2597 ee_len = ext4_ext_get_actual_len(ex);
2598 allocated = ee_len - (map->m_lblk - ee_block);
2599 newblock = map->m_lblk - ee_block + ext_pblock(ex);
2602 orig_ex.ee_block = ex->ee_block;
2603 orig_ex.ee_len = cpu_to_le16(ee_len);
2604 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2607 * It is safe to convert extent to initialized via explicit
2608 * zeroout only if extent is fully insde i_size or new_size.
2610 may_zeroout = ee_block + ee_len <= eof_block;
2612 err = ext4_ext_get_access(handle, inode, path + depth);
2615 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2616 if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) {
2617 err = ext4_ext_zeroout(inode, &orig_ex);
2619 goto fix_extent_len;
2620 /* update the extent length and mark as initialized */
2621 ex->ee_block = orig_ex.ee_block;
2622 ex->ee_len = orig_ex.ee_len;
2623 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2624 ext4_ext_dirty(handle, inode, path + depth);
2625 /* zeroed the full extent */
2629 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2630 if (map->m_lblk > ee_block) {
2632 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
2633 ext4_ext_mark_uninitialized(ex1);
2637 * for sanity, update the length of the ex2 extent before
2638 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2639 * overlap of blocks.
2641 if (!ex1 && allocated > map->m_len)
2642 ex2->ee_len = cpu_to_le16(map->m_len);
2643 /* ex3: to ee_block + ee_len : uninitialised */
2644 if (allocated > map->m_len) {
2645 unsigned int newdepth;
2646 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2647 if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) {
2649 * map->m_lblk == ee_block is handled by the zerouout
2651 * Mark first half uninitialized.
2652 * Mark second half initialized and zero out the
2653 * initialized extent
2655 ex->ee_block = orig_ex.ee_block;
2656 ex->ee_len = cpu_to_le16(ee_len - allocated);
2657 ext4_ext_mark_uninitialized(ex);
2658 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2659 ext4_ext_dirty(handle, inode, path + depth);
2662 ex3->ee_block = cpu_to_le32(map->m_lblk);
2663 ext4_ext_store_pblock(ex3, newblock);
2664 ex3->ee_len = cpu_to_le16(allocated);
2665 err = ext4_ext_insert_extent(handle, inode, path,
2667 if (err == -ENOSPC) {
2668 err = ext4_ext_zeroout(inode, &orig_ex);
2670 goto fix_extent_len;
2671 ex->ee_block = orig_ex.ee_block;
2672 ex->ee_len = orig_ex.ee_len;
2673 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2674 ext4_ext_dirty(handle, inode, path + depth);
2675 /* blocks available from map->m_lblk */
2679 goto fix_extent_len;
2682 * We need to zero out the second half because
2683 * an fallocate request can update file size and
2684 * converting the second half to initialized extent
2685 * implies that we can leak some junk data to user
2688 err = ext4_ext_zeroout(inode, ex3);
2691 * We should actually mark the
2692 * second half as uninit and return error
2693 * Insert would have changed the extent
2695 depth = ext_depth(inode);
2696 ext4_ext_drop_refs(path);
2697 path = ext4_ext_find_extent(inode, map->m_lblk,
2700 err = PTR_ERR(path);
2703 /* get the second half extent details */
2704 ex = path[depth].p_ext;
2705 err = ext4_ext_get_access(handle, inode,
2709 ext4_ext_mark_uninitialized(ex);
2710 ext4_ext_dirty(handle, inode, path + depth);
2714 /* zeroed the second half */
2718 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2719 ext4_ext_store_pblock(ex3, newblock + map->m_len);
2720 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
2721 ext4_ext_mark_uninitialized(ex3);
2722 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
2723 if (err == -ENOSPC && may_zeroout) {
2724 err = ext4_ext_zeroout(inode, &orig_ex);
2726 goto fix_extent_len;
2727 /* update the extent length and mark as initialized */
2728 ex->ee_block = orig_ex.ee_block;
2729 ex->ee_len = orig_ex.ee_len;
2730 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2731 ext4_ext_dirty(handle, inode, path + depth);
2732 /* zeroed the full extent */
2733 /* blocks available from map->m_lblk */
2737 goto fix_extent_len;
2739 * The depth, and hence eh & ex might change
2740 * as part of the insert above.
2742 newdepth = ext_depth(inode);
2744 * update the extent length after successful insert of the
2747 ee_len -= ext4_ext_get_actual_len(ex3);
2748 orig_ex.ee_len = cpu_to_le16(ee_len);
2749 may_zeroout = ee_block + ee_len <= eof_block;
2752 ext4_ext_drop_refs(path);
2753 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2755 err = PTR_ERR(path);
2758 eh = path[depth].p_hdr;
2759 ex = path[depth].p_ext;
2763 err = ext4_ext_get_access(handle, inode, path + depth);
2767 allocated = map->m_len;
2769 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2770 * to insert a extent in the middle zerout directly
2771 * otherwise give the extent a chance to merge to left
2773 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2774 map->m_lblk != ee_block && may_zeroout) {
2775 err = ext4_ext_zeroout(inode, &orig_ex);
2777 goto fix_extent_len;
2778 /* update the extent length and mark as initialized */
2779 ex->ee_block = orig_ex.ee_block;
2780 ex->ee_len = orig_ex.ee_len;
2781 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2782 ext4_ext_dirty(handle, inode, path + depth);
2783 /* zero out the first half */
2784 /* blocks available from map->m_lblk */
2789 * If there was a change of depth as part of the
2790 * insertion of ex3 above, we need to update the length
2791 * of the ex1 extent again here
2793 if (ex1 && ex1 != ex) {
2795 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
2796 ext4_ext_mark_uninitialized(ex1);
2799 /* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */
2800 ex2->ee_block = cpu_to_le32(map->m_lblk);
2801 ext4_ext_store_pblock(ex2, newblock);
2802 ex2->ee_len = cpu_to_le16(allocated);
2806 * New (initialized) extent starts from the first block
2807 * in the current extent. i.e., ex2 == ex
2808 * We have to see if it can be merged with the extent
2811 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2813 * To merge left, pass "ex2 - 1" to try_to_merge(),
2814 * since it merges towards right _only_.
2816 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2818 err = ext4_ext_correct_indexes(handle, inode, path);
2821 depth = ext_depth(inode);
2826 * Try to Merge towards right. This might be required
2827 * only when the whole extent is being written to.
2828 * i.e. ex2 == ex and ex3 == NULL.
2831 ret = ext4_ext_try_to_merge(inode, path, ex2);
2833 err = ext4_ext_correct_indexes(handle, inode, path);
2838 /* Mark modified extent as dirty */
2839 err = ext4_ext_dirty(handle, inode, path + depth);
2842 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
2843 if (err == -ENOSPC && may_zeroout) {
2844 err = ext4_ext_zeroout(inode, &orig_ex);
2846 goto fix_extent_len;
2847 /* update the extent length and mark as initialized */
2848 ex->ee_block = orig_ex.ee_block;
2849 ex->ee_len = orig_ex.ee_len;
2850 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2851 ext4_ext_dirty(handle, inode, path + depth);
2852 /* zero out the first half */
2855 goto fix_extent_len;
2857 ext4_ext_show_leaf(inode, path);
2858 return err ? err : allocated;
2861 ex->ee_block = orig_ex.ee_block;
2862 ex->ee_len = orig_ex.ee_len;
2863 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2864 ext4_ext_mark_uninitialized(ex);
2865 ext4_ext_dirty(handle, inode, path + depth);
2870 * This function is called by ext4_ext_map_blocks() from
2871 * ext4_get_blocks_dio_write() when DIO to write
2872 * to an uninitialized extent.
2874 * Writing to an uninitized extent may result in splitting the uninitialized
2875 * extent into multiple /intialized unintialized extents (up to three)
2876 * There are three possibilities:
2877 * a> There is no split required: Entire extent should be uninitialized
2878 * b> Splits in two extents: Write is happening at either end of the extent
2879 * c> Splits in three extents: Somone is writing in middle of the extent
2881 * One of more index blocks maybe needed if the extent tree grow after
2882 * the unintialized extent split. To prevent ENOSPC occur at the IO
2883 * complete, we need to split the uninitialized extent before DIO submit
2884 * the IO. The uninitialized extent called at this time will be split
2885 * into three uninitialized extent(at most). After IO complete, the part
2886 * being filled will be convert to initialized by the end_io callback function
2887 * via ext4_convert_unwritten_extents().
2889 * Returns the size of uninitialized extent to be written on success.
2891 static int ext4_split_unwritten_extents(handle_t *handle,
2892 struct inode *inode,
2893 struct ext4_map_blocks *map,
2894 struct ext4_ext_path *path,
2897 struct ext4_extent *ex, newex, orig_ex;
2898 struct ext4_extent *ex1 = NULL;
2899 struct ext4_extent *ex2 = NULL;
2900 struct ext4_extent *ex3 = NULL;
2901 ext4_lblk_t ee_block, eof_block;
2902 unsigned int allocated, ee_len, depth;
2903 ext4_fsblk_t newblock;
2907 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2908 "block %llu, max_blocks %u\n", inode->i_ino,
2909 (unsigned long long)map->m_lblk, map->m_len);
2911 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2912 inode->i_sb->s_blocksize_bits;
2913 if (eof_block < map->m_lblk + map->m_len)
2914 eof_block = map->m_lblk + map->m_len;
2916 depth = ext_depth(inode);
2917 ex = path[depth].p_ext;
2918 ee_block = le32_to_cpu(ex->ee_block);
2919 ee_len = ext4_ext_get_actual_len(ex);
2920 allocated = ee_len - (map->m_lblk - ee_block);
2921 newblock = map->m_lblk - ee_block + ext_pblock(ex);
2924 orig_ex.ee_block = ex->ee_block;
2925 orig_ex.ee_len = cpu_to_le16(ee_len);
2926 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2929 * It is safe to convert extent to initialized via explicit
2930 * zeroout only if extent is fully insde i_size or new_size.
2932 may_zeroout = ee_block + ee_len <= eof_block;
2935 * If the uninitialized extent begins at the same logical
2936 * block where the write begins, and the write completely
2937 * covers the extent, then we don't need to split it.
2939 if ((map->m_lblk == ee_block) && (allocated <= map->m_len))
2942 err = ext4_ext_get_access(handle, inode, path + depth);
2945 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2946 if (map->m_lblk > ee_block) {
2948 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
2949 ext4_ext_mark_uninitialized(ex1);
2953 * for sanity, update the length of the ex2 extent before
2954 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2955 * overlap of blocks.
2957 if (!ex1 && allocated > map->m_len)
2958 ex2->ee_len = cpu_to_le16(map->m_len);
2959 /* ex3: to ee_block + ee_len : uninitialised */
2960 if (allocated > map->m_len) {
2961 unsigned int newdepth;
2963 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2964 ext4_ext_store_pblock(ex3, newblock + map->m_len);
2965 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
2966 ext4_ext_mark_uninitialized(ex3);
2967 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
2968 if (err == -ENOSPC && may_zeroout) {
2969 err = ext4_ext_zeroout(inode, &orig_ex);
2971 goto fix_extent_len;
2972 /* update the extent length and mark as initialized */
2973 ex->ee_block = orig_ex.ee_block;
2974 ex->ee_len = orig_ex.ee_len;
2975 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2976 ext4_ext_dirty(handle, inode, path + depth);
2977 /* zeroed the full extent */
2978 /* blocks available from map->m_lblk */
2982 goto fix_extent_len;
2984 * The depth, and hence eh & ex might change
2985 * as part of the insert above.
2987 newdepth = ext_depth(inode);
2989 * update the extent length after successful insert of the
2992 ee_len -= ext4_ext_get_actual_len(ex3);
2993 orig_ex.ee_len = cpu_to_le16(ee_len);
2994 may_zeroout = ee_block + ee_len <= eof_block;
2997 ext4_ext_drop_refs(path);
2998 path = ext4_ext_find_extent(inode, map->m_lblk, path);
3000 err = PTR_ERR(path);
3003 ex = path[depth].p_ext;
3007 err = ext4_ext_get_access(handle, inode, path + depth);
3011 allocated = map->m_len;
3014 * If there was a change of depth as part of the
3015 * insertion of ex3 above, we need to update the length
3016 * of the ex1 extent again here
3018 if (ex1 && ex1 != ex) {
3020 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
3021 ext4_ext_mark_uninitialized(ex1);
3025 * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written
3026 * using direct I/O, uninitialised still.
3028 ex2->ee_block = cpu_to_le32(map->m_lblk);
3029 ext4_ext_store_pblock(ex2, newblock);
3030 ex2->ee_len = cpu_to_le16(allocated);
3031 ext4_ext_mark_uninitialized(ex2);
3034 /* Mark modified extent as dirty */
3035 err = ext4_ext_dirty(handle, inode, path + depth);
3036 ext_debug("out here\n");
3039 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3040 if (err == -ENOSPC && may_zeroout) {
3041 err = ext4_ext_zeroout(inode, &orig_ex);
3043 goto fix_extent_len;
3044 /* update the extent length and mark as initialized */
3045 ex->ee_block = orig_ex.ee_block;
3046 ex->ee_len = orig_ex.ee_len;
3047 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3048 ext4_ext_dirty(handle, inode, path + depth);
3049 /* zero out the first half */
3052 goto fix_extent_len;
3054 ext4_ext_show_leaf(inode, path);
3055 return err ? err : allocated;
3058 ex->ee_block = orig_ex.ee_block;
3059 ex->ee_len = orig_ex.ee_len;
3060 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3061 ext4_ext_mark_uninitialized(ex);
3062 ext4_ext_dirty(handle, inode, path + depth);
3065 static int ext4_convert_unwritten_extents_endio(handle_t *handle,
3066 struct inode *inode,
3067 struct ext4_ext_path *path)
3069 struct ext4_extent *ex;
3070 struct ext4_extent_header *eh;
3075 depth = ext_depth(inode);
3076 eh = path[depth].p_hdr;
3077 ex = path[depth].p_ext;
3079 err = ext4_ext_get_access(handle, inode, path + depth);
3082 /* first mark the extent as initialized */
3083 ext4_ext_mark_initialized(ex);
3086 * We have to see if it can be merged with the extent
3089 if (ex > EXT_FIRST_EXTENT(eh)) {
3091 * To merge left, pass "ex - 1" to try_to_merge(),
3092 * since it merges towards right _only_.
3094 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3096 err = ext4_ext_correct_indexes(handle, inode, path);
3099 depth = ext_depth(inode);
3104 * Try to Merge towards right.
3106 ret = ext4_ext_try_to_merge(inode, path, ex);
3108 err = ext4_ext_correct_indexes(handle, inode, path);
3111 depth = ext_depth(inode);
3113 /* Mark modified extent as dirty */
3114 err = ext4_ext_dirty(handle, inode, path + depth);
3116 ext4_ext_show_leaf(inode, path);
3120 static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3121 sector_t block, int count)
3124 for (i = 0; i < count; i++)
3125 unmap_underlying_metadata(bdev, block + i);
3129 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3131 static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
3132 struct ext4_map_blocks *map,
3133 struct ext4_ext_path *path,
3137 struct ext4_extent_header *eh;
3138 struct ext4_extent *ex, *last_ex;
3140 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3143 depth = ext_depth(inode);
3144 eh = path[depth].p_hdr;
3145 ex = path[depth].p_ext;
3147 if (unlikely(!eh->eh_entries)) {
3148 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3149 "EOFBLOCKS_FL set");
3152 last_ex = EXT_LAST_EXTENT(eh);
3154 * We should clear the EOFBLOCKS_FL flag if we are writing the
3155 * last block in the last extent in the file. We test this by
3156 * first checking to see if the caller to
3157 * ext4_ext_get_blocks() was interested in the last block (or
3158 * a block beyond the last block) in the current extent. If
3159 * this turns out to be false, we can bail out from this
3160 * function immediately.
3162 if (map->m_lblk + len < le32_to_cpu(last_ex->ee_block) +
3163 ext4_ext_get_actual_len(last_ex))
3166 * If the caller does appear to be planning to write at or
3167 * beyond the end of the current extent, we then test to see
3168 * if the current extent is the last extent in the file, by
3169 * checking to make sure it was reached via the rightmost node
3170 * at each level of the tree.
3172 for (i = depth-1; i >= 0; i--)
3173 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3175 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3176 return ext4_mark_inode_dirty(handle, inode);
3180 ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3181 struct ext4_map_blocks *map,
3182 struct ext4_ext_path *path, int flags,
3183 unsigned int allocated, ext4_fsblk_t newblock)
3187 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3189 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3190 "block %llu, max_blocks %u, flags %d, allocated %u",
3191 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
3193 ext4_ext_show_leaf(inode, path);
3195 /* get_block() before submit the IO, split the extent */
3196 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
3197 ret = ext4_split_unwritten_extents(handle, inode, map,
3200 * Flag the inode(non aio case) or end_io struct (aio case)
3201 * that this IO needs to convertion to written when IO is
3205 io->flag = EXT4_IO_UNWRITTEN;
3207 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3208 if (ext4_should_dioread_nolock(inode))
3209 map->m_flags |= EXT4_MAP_UNINIT;
3212 /* IO end_io complete, convert the filled extent to written */
3213 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
3214 ret = ext4_convert_unwritten_extents_endio(handle, inode,
3217 ext4_update_inode_fsync_trans(handle, inode, 1);
3218 err = check_eofblocks_fl(handle, inode, map, path,
3224 /* buffered IO case */
3226 * repeat fallocate creation request
3227 * we already have an unwritten extent
3229 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3232 /* buffered READ or buffered write_begin() lookup */
3233 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3235 * We have blocks reserved already. We
3236 * return allocated blocks so that delalloc
3237 * won't do block reservation for us. But
3238 * the buffer head will be unmapped so that
3239 * a read from the block returns 0s.
3241 map->m_flags |= EXT4_MAP_UNWRITTEN;
3245 /* buffered write, writepage time, convert*/
3246 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
3248 ext4_update_inode_fsync_trans(handle, inode, 1);
3249 err = check_eofblocks_fl(handle, inode, map, path, map->m_len);
3260 map->m_flags |= EXT4_MAP_NEW;
3262 * if we allocated more blocks than requested
3263 * we need to make sure we unmap the extra block
3264 * allocated. The actual needed block will get
3265 * unmapped later when we find the buffer_head marked
3268 if (allocated > map->m_len) {
3269 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
3270 newblock + map->m_len,
3271 allocated - map->m_len);
3272 allocated = map->m_len;
3276 * If we have done fallocate with the offset that is already
3277 * delayed allocated, we would have block reservation
3278 * and quota reservation done in the delayed write path.
3279 * But fallocate would have already updated quota and block
3280 * count for this offset. So cancel these reservation
3282 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
3283 ext4_da_update_reserve_space(inode, allocated, 0);
3286 map->m_flags |= EXT4_MAP_MAPPED;
3288 if (allocated > map->m_len)
3289 allocated = map->m_len;
3290 ext4_ext_show_leaf(inode, path);
3291 map->m_pblk = newblock;
3292 map->m_len = allocated;
3295 ext4_ext_drop_refs(path);
3298 return err ? err : allocated;
3302 * Block allocation/map/preallocation routine for extents based files
3305 * Need to be called with
3306 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3307 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3309 * return > 0, number of of blocks already mapped/allocated
3310 * if create == 0 and these are pre-allocated blocks
3311 * buffer head is unmapped
3312 * otherwise blocks are mapped
3314 * return = 0, if plain look up failed (blocks have not been allocated)
3315 * buffer head is unmapped
3317 * return < 0, error case.
3319 int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3320 struct ext4_map_blocks *map, int flags)
3322 struct ext4_ext_path *path = NULL;
3323 struct ext4_extent_header *eh;
3324 struct ext4_extent newex, *ex;
3325 ext4_fsblk_t newblock;
3326 int err = 0, depth, ret, cache_type;
3327 unsigned int allocated = 0;
3328 struct ext4_allocation_request ar;
3329 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3331 ext_debug("blocks %u/%u requested for inode %lu\n",
3332 map->m_lblk, map->m_len, inode->i_ino);
3334 /* check in cache */
3335 cache_type = ext4_ext_in_cache(inode, map->m_lblk, &newex);
3337 if (cache_type == EXT4_EXT_CACHE_GAP) {
3338 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3340 * block isn't allocated yet and
3341 * user doesn't want to allocate it
3345 /* we should allocate requested block */
3346 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
3347 /* block is already allocated */
3348 newblock = map->m_lblk
3349 - le32_to_cpu(newex.ee_block)
3350 + ext_pblock(&newex);
3351 /* number of remaining blocks in the extent */
3352 allocated = ext4_ext_get_actual_len(&newex) -
3353 (map->m_lblk - le32_to_cpu(newex.ee_block));
3360 /* find extent for this block */
3361 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
3363 err = PTR_ERR(path);
3368 depth = ext_depth(inode);
3371 * consistent leaf must not be empty;
3372 * this situation is possible, though, _during_ tree modification;
3373 * this is why assert can't be put in ext4_ext_find_extent()
3375 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3376 EXT4_ERROR_INODE(inode, "bad extent address "
3377 "lblock: %lu, depth: %d pblock %lld",
3378 (unsigned long) map->m_lblk, depth,
3379 path[depth].p_block);
3383 eh = path[depth].p_hdr;
3385 ex = path[depth].p_ext;
3387 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3388 ext4_fsblk_t ee_start = ext_pblock(ex);
3389 unsigned short ee_len;
3392 * Uninitialized extents are treated as holes, except that
3393 * we split out initialized portions during a write.
3395 ee_len = ext4_ext_get_actual_len(ex);
3396 /* if found extent covers block, simply return it */
3397 if (in_range(map->m_lblk, ee_block, ee_len)) {
3398 newblock = map->m_lblk - ee_block + ee_start;
3399 /* number of remaining blocks in the extent */
3400 allocated = ee_len - (map->m_lblk - ee_block);
3401 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3402 ee_block, ee_len, newblock);
3404 /* Do not put uninitialized extent in the cache */
3405 if (!ext4_ext_is_uninitialized(ex)) {
3406 ext4_ext_put_in_cache(inode, ee_block,
3408 EXT4_EXT_CACHE_EXTENT);
3411 ret = ext4_ext_handle_uninitialized_extents(handle,
3412 inode, map, path, flags, allocated,
3419 * requested block isn't allocated yet;
3420 * we couldn't try to create block if create flag is zero
3422 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3424 * put just found gap into cache to speed up
3425 * subsequent requests
3427 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
3431 * Okay, we need to do block allocation.
3434 /* find neighbour allocated blocks */
3435 ar.lleft = map->m_lblk;
3436 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3439 ar.lright = map->m_lblk;
3440 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3445 * See if request is beyond maximum number of blocks we can have in
3446 * a single extent. For an initialized extent this limit is
3447 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3448 * EXT_UNINIT_MAX_LEN.
3450 if (map->m_len > EXT_INIT_MAX_LEN &&
3451 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3452 map->m_len = EXT_INIT_MAX_LEN;
3453 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
3454 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3455 map->m_len = EXT_UNINIT_MAX_LEN;
3457 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3458 newex.ee_block = cpu_to_le32(map->m_lblk);
3459 newex.ee_len = cpu_to_le16(map->m_len);
3460 err = ext4_ext_check_overlap(inode, &newex, path);
3462 allocated = ext4_ext_get_actual_len(&newex);
3464 allocated = map->m_len;
3466 /* allocate new block */
3468 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3469 ar.logical = map->m_lblk;
3471 if (S_ISREG(inode->i_mode))
3472 ar.flags = EXT4_MB_HINT_DATA;
3474 /* disable in-core preallocation for non-regular files */
3476 newblock = ext4_mb_new_blocks(handle, &ar, &err);
3479 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3480 ar.goal, newblock, allocated);
3482 /* try to insert new extent into found leaf and return */
3483 ext4_ext_store_pblock(&newex, newblock);
3484 newex.ee_len = cpu_to_le16(ar.len);
3485 /* Mark uninitialized */
3486 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
3487 ext4_ext_mark_uninitialized(&newex);
3489 * io_end structure was created for every IO write to an
3490 * uninitialized extent. To avoid unecessary conversion,
3491 * here we flag the IO that really needs the conversion.
3492 * For non asycn direct IO case, flag the inode state
3493 * that we need to perform convertion when IO is done.
3495 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
3497 io->flag = EXT4_IO_UNWRITTEN;
3499 ext4_set_inode_state(inode,
3500 EXT4_STATE_DIO_UNWRITTEN);
3502 if (ext4_should_dioread_nolock(inode))
3503 map->m_flags |= EXT4_MAP_UNINIT;
3506 err = check_eofblocks_fl(handle, inode, map, path, ar.len);
3510 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3512 /* free data blocks we just allocated */
3513 /* not a good idea to call discard here directly,
3514 * but otherwise we'd need to call it every free() */
3515 ext4_discard_preallocations(inode);
3516 ext4_free_blocks(handle, inode, 0, ext_pblock(&newex),
3517 ext4_ext_get_actual_len(&newex), 0);
3521 /* previous routine could use block we allocated */
3522 newblock = ext_pblock(&newex);
3523 allocated = ext4_ext_get_actual_len(&newex);
3524 if (allocated > map->m_len)
3525 allocated = map->m_len;
3526 map->m_flags |= EXT4_MAP_NEW;
3529 * Update reserved blocks/metadata blocks after successful
3530 * block allocation which had been deferred till now.
3532 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
3533 ext4_da_update_reserve_space(inode, allocated, 1);
3536 * Cache the extent and update transaction to commit on fdatasync only
3537 * when it is _not_ an uninitialized extent.
3539 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
3540 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock,
3541 EXT4_EXT_CACHE_EXTENT);
3542 ext4_update_inode_fsync_trans(handle, inode, 1);
3544 ext4_update_inode_fsync_trans(handle, inode, 0);
3546 if (allocated > map->m_len)
3547 allocated = map->m_len;
3548 ext4_ext_show_leaf(inode, path);
3549 map->m_flags |= EXT4_MAP_MAPPED;
3550 map->m_pblk = newblock;
3551 map->m_len = allocated;
3554 ext4_ext_drop_refs(path);
3557 return err ? err : allocated;
3560 void ext4_ext_truncate(struct inode *inode)
3562 struct address_space *mapping = inode->i_mapping;
3563 struct super_block *sb = inode->i_sb;
3564 ext4_lblk_t last_block;
3569 * probably first extent we're gonna free will be last in block
3571 err = ext4_writepage_trans_blocks(inode);
3572 handle = ext4_journal_start(inode, err);
3576 if (inode->i_size & (sb->s_blocksize - 1))
3577 ext4_block_truncate_page(handle, mapping, inode->i_size);
3579 if (ext4_orphan_add(handle, inode))
3582 down_write(&EXT4_I(inode)->i_data_sem);
3583 ext4_ext_invalidate_cache(inode);
3585 ext4_discard_preallocations(inode);
3588 * TODO: optimization is possible here.
3589 * Probably we need not scan at all,
3590 * because page truncation is enough.
3593 /* we have to know where to truncate from in crash case */
3594 EXT4_I(inode)->i_disksize = inode->i_size;
3595 ext4_mark_inode_dirty(handle, inode);
3597 last_block = (inode->i_size + sb->s_blocksize - 1)
3598 >> EXT4_BLOCK_SIZE_BITS(sb);
3599 err = ext4_ext_remove_space(inode, last_block);
3601 /* In a multi-transaction truncate, we only make the final
3602 * transaction synchronous.
3605 ext4_handle_sync(handle);
3608 up_write(&EXT4_I(inode)->i_data_sem);
3610 * If this was a simple ftruncate() and the file will remain alive,
3611 * then we need to clear up the orphan record which we created above.
3612 * However, if this was a real unlink then we were called by
3613 * ext4_delete_inode(), and we allow that function to clean up the
3614 * orphan info for us.
3617 ext4_orphan_del(handle, inode);
3619 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3620 ext4_mark_inode_dirty(handle, inode);
3621 ext4_journal_stop(handle);
3624 static void ext4_falloc_update_inode(struct inode *inode,
3625 int mode, loff_t new_size, int update_ctime)
3627 struct timespec now;
3630 now = current_fs_time(inode->i_sb);
3631 if (!timespec_equal(&inode->i_ctime, &now))
3632 inode->i_ctime = now;
3635 * Update only when preallocation was requested beyond
3638 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3639 if (new_size > i_size_read(inode))
3640 i_size_write(inode, new_size);
3641 if (new_size > EXT4_I(inode)->i_disksize)
3642 ext4_update_i_disksize(inode, new_size);
3645 * Mark that we allocate beyond EOF so the subsequent truncate
3646 * can proceed even if the new size is the same as i_size.
3648 if (new_size > i_size_read(inode))
3649 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3655 * preallocate space for a file. This implements ext4's fallocate inode
3656 * operation, which gets called from sys_fallocate system call.
3657 * For block-mapped files, posix_fallocate should fall back to the method
3658 * of writing zeroes to the required new blocks (the same behavior which is
3659 * expected for file systems which do not support fallocate() system call).
3661 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3665 unsigned int max_blocks;
3669 struct ext4_map_blocks map;
3670 unsigned int credits, blkbits = inode->i_blkbits;
3673 * currently supporting (pre)allocate mode for extent-based
3676 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
3679 /* preallocation to directories is currently not supported */
3680 if (S_ISDIR(inode->i_mode))
3683 map.m_lblk = offset >> blkbits;
3685 * We can't just convert len to max_blocks because
3686 * If blocksize = 4096 offset = 3072 and len = 2048
3688 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3691 * credits to insert 1 extent into extent tree
3693 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3694 mutex_lock(&inode->i_mutex);
3695 ret = inode_newsize_ok(inode, (len + offset));
3697 mutex_unlock(&inode->i_mutex);
3701 while (ret >= 0 && ret < max_blocks) {
3702 map.m_lblk = map.m_lblk + ret;
3703 map.m_len = max_blocks = max_blocks - ret;
3704 handle = ext4_journal_start(inode, credits);
3705 if (IS_ERR(handle)) {
3706 ret = PTR_ERR(handle);
3709 ret = ext4_map_blocks(handle, inode, &map,
3710 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
3714 printk(KERN_ERR "%s: ext4_ext_map_blocks "
3715 "returned error inode#%lu, block=%u, "
3716 "max_blocks=%u", __func__,
3717 inode->i_ino, block, max_blocks);
3719 ext4_mark_inode_dirty(handle, inode);
3720 ret2 = ext4_journal_stop(handle);
3723 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3724 blkbits) >> blkbits))
3725 new_size = offset + len;
3727 new_size = (map.m_lblk + ret) << blkbits;
3729 ext4_falloc_update_inode(inode, mode, new_size,
3730 (map.m_flags & EXT4_MAP_NEW));
3731 ext4_mark_inode_dirty(handle, inode);
3732 ret2 = ext4_journal_stop(handle);
3736 if (ret == -ENOSPC &&
3737 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3741 mutex_unlock(&inode->i_mutex);
3742 return ret > 0 ? ret2 : ret;
3746 * This function convert a range of blocks to written extents
3747 * The caller of this function will pass the start offset and the size.
3748 * all unwritten extents within this range will be converted to
3751 * This function is called from the direct IO end io call back
3752 * function, to convert the fallocated extents after IO is completed.
3753 * Returns 0 on success.
3755 int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
3759 unsigned int max_blocks;
3762 struct ext4_map_blocks map;
3763 unsigned int credits, blkbits = inode->i_blkbits;
3765 map.m_lblk = offset >> blkbits;
3767 * We can't just convert len to max_blocks because
3768 * If blocksize = 4096 offset = 3072 and len = 2048
3770 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
3773 * credits to insert 1 extent into extent tree
3775 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3776 while (ret >= 0 && ret < max_blocks) {
3778 map.m_len = (max_blocks -= ret);
3779 handle = ext4_journal_start(inode, credits);
3780 if (IS_ERR(handle)) {
3781 ret = PTR_ERR(handle);
3784 ret = ext4_map_blocks(handle, inode, &map,
3785 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
3788 printk(KERN_ERR "%s: ext4_ext_map_blocks "
3789 "returned error inode#%lu, block=%u, "
3790 "max_blocks=%u", __func__,
3791 inode->i_ino, map.m_lblk, map.m_len);
3793 ext4_mark_inode_dirty(handle, inode);
3794 ret2 = ext4_journal_stop(handle);
3795 if (ret <= 0 || ret2 )
3798 return ret > 0 ? ret2 : ret;
3801 * Callback function called for each extent to gather FIEMAP information.
3803 static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
3804 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3807 struct fiemap_extent_info *fieinfo = data;
3808 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
3815 logical = (__u64)newex->ec_block << blksize_bits;
3817 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3820 struct buffer_head *bh = NULL;
3822 offset = logical >> PAGE_SHIFT;
3823 page = find_get_page(inode->i_mapping, offset);
3824 if (!page || !page_has_buffers(page))
3825 return EXT_CONTINUE;
3827 bh = page_buffers(page);
3830 return EXT_CONTINUE;
3832 if (buffer_delay(bh)) {
3833 flags |= FIEMAP_EXTENT_DELALLOC;
3834 page_cache_release(page);
3836 page_cache_release(page);
3837 return EXT_CONTINUE;
3841 physical = (__u64)newex->ec_start << blksize_bits;
3842 length = (__u64)newex->ec_len << blksize_bits;
3844 if (ex && ext4_ext_is_uninitialized(ex))
3845 flags |= FIEMAP_EXTENT_UNWRITTEN;
3848 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3850 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3851 * this also indicates no more allocated blocks.
3853 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3855 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
3856 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3857 loff_t size = i_size_read(inode);
3858 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3860 flags |= FIEMAP_EXTENT_LAST;
3861 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3862 logical+length > size)
3863 length = (size - logical + bs - 1) & ~(bs-1);
3866 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3873 return EXT_CONTINUE;
3876 /* fiemap flags we can handle specified here */
3877 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3879 static int ext4_xattr_fiemap(struct inode *inode,
3880 struct fiemap_extent_info *fieinfo)
3884 __u32 flags = FIEMAP_EXTENT_LAST;
3885 int blockbits = inode->i_sb->s_blocksize_bits;
3889 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
3890 struct ext4_iloc iloc;
3891 int offset; /* offset of xattr in inode */
3893 error = ext4_get_inode_loc(inode, &iloc);
3896 physical = iloc.bh->b_blocknr << blockbits;
3897 offset = EXT4_GOOD_OLD_INODE_SIZE +
3898 EXT4_I(inode)->i_extra_isize;
3900 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3901 flags |= FIEMAP_EXTENT_DATA_INLINE;
3903 } else { /* external block */
3904 physical = EXT4_I(inode)->i_file_acl << blockbits;
3905 length = inode->i_sb->s_blocksize;
3909 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3911 return (error < 0 ? error : 0);
3914 int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3915 __u64 start, __u64 len)
3917 ext4_lblk_t start_blk;
3920 /* fallback to generic here if not in extents fmt */
3921 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
3922 return generic_block_fiemap(inode, fieinfo, start, len,
3925 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3928 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3929 error = ext4_xattr_fiemap(inode, fieinfo);
3931 ext4_lblk_t len_blks;
3934 start_blk = start >> inode->i_sb->s_blocksize_bits;
3935 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
3936 if (last_blk >= EXT_MAX_BLOCK)
3937 last_blk = EXT_MAX_BLOCK-1;
3938 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
3941 * Walk the extent tree gathering extent information.
3942 * ext4_ext_fiemap_cb will push extents back to user.
3944 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3945 ext4_ext_fiemap_cb, fieinfo);