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 "ext4_jbd2.h"
44 #include "ext4_extents.h"
49 * combine low and high parts of physical block number into ext4_fsblk_t
51 static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
55 block = le32_to_cpu(ex->ee_start_lo);
56 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64 ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
68 block = le32_to_cpu(ix->ei_leaf_lo);
69 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
74 * ext4_ext_store_pblock:
75 * stores a large physical block number into an extent struct,
76 * breaking it into parts
78 void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
80 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
81 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
85 * ext4_idx_store_pblock:
86 * stores a large physical block number into an index struct,
87 * breaking it into parts
89 static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
91 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
92 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
95 static int ext4_ext_journal_restart(handle_t *handle, int needed)
99 if (handle->h_buffer_credits > needed)
101 err = ext4_journal_extend(handle, needed);
104 return ext4_journal_restart(handle, needed);
112 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
113 struct ext4_ext_path *path)
116 /* path points to block */
117 return ext4_journal_get_write_access(handle, path->p_bh);
119 /* path points to leaf/index in inode body */
120 /* we use in-core data, no need to protect them */
130 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
131 struct ext4_ext_path *path)
135 /* path points to block */
136 err = ext4_journal_dirty_metadata(handle, path->p_bh);
138 /* path points to leaf/index in inode body */
139 err = ext4_mark_inode_dirty(handle, inode);
144 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
145 struct ext4_ext_path *path,
148 struct ext4_inode_info *ei = EXT4_I(inode);
149 ext4_fsblk_t bg_start;
150 ext4_fsblk_t last_block;
151 ext4_grpblk_t colour;
155 struct ext4_extent *ex;
156 depth = path->p_depth;
158 /* try to predict block placement */
159 ex = path[depth].p_ext;
161 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
163 /* it looks like index is empty;
164 * try to find starting block from index itself */
165 if (path[depth].p_bh)
166 return path[depth].p_bh->b_blocknr;
169 /* OK. use inode's group */
170 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
171 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
172 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
174 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
175 colour = (current->pid % 16) *
176 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
178 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
179 return bg_start + colour + block;
183 * Allocation for a meta data block
186 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
187 struct ext4_ext_path *path,
188 struct ext4_extent *ex, int *err)
190 ext4_fsblk_t goal, newblock;
192 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
193 newblock = ext4_new_meta_block(handle, inode, goal, err);
197 static int ext4_ext_space_block(struct inode *inode)
201 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
202 / sizeof(struct ext4_extent);
203 #ifdef AGGRESSIVE_TEST
210 static int ext4_ext_space_block_idx(struct inode *inode)
214 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
215 / sizeof(struct ext4_extent_idx);
216 #ifdef AGGRESSIVE_TEST
223 static int ext4_ext_space_root(struct inode *inode)
227 size = sizeof(EXT4_I(inode)->i_data);
228 size -= sizeof(struct ext4_extent_header);
229 size /= sizeof(struct ext4_extent);
230 #ifdef AGGRESSIVE_TEST
237 static int ext4_ext_space_root_idx(struct inode *inode)
241 size = sizeof(EXT4_I(inode)->i_data);
242 size -= sizeof(struct ext4_extent_header);
243 size /= sizeof(struct ext4_extent_idx);
244 #ifdef AGGRESSIVE_TEST
252 * Calculate the number of metadata blocks needed
253 * to allocate @blocks
254 * Worse case is one block per extent
256 int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
258 int lcap, icap, rcap, leafs, idxs, num;
259 int newextents = blocks;
261 rcap = ext4_ext_space_root_idx(inode);
262 lcap = ext4_ext_space_block(inode);
263 icap = ext4_ext_space_block_idx(inode);
265 /* number of new leaf blocks needed */
266 num = leafs = (newextents + lcap - 1) / lcap;
269 * Worse case, we need separate index block(s)
270 * to link all new leaf blocks
272 idxs = (leafs + icap - 1) / icap;
275 idxs = (idxs + icap - 1) / icap;
276 } while (idxs > rcap);
282 ext4_ext_max_entries(struct inode *inode, int depth)
286 if (depth == ext_depth(inode)) {
288 max = ext4_ext_space_root(inode);
290 max = ext4_ext_space_root_idx(inode);
293 max = ext4_ext_space_block(inode);
295 max = ext4_ext_space_block_idx(inode);
301 static int __ext4_ext_check_header(const char *function, struct inode *inode,
302 struct ext4_extent_header *eh,
305 const char *error_msg;
308 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
309 error_msg = "invalid magic";
312 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
313 error_msg = "unexpected eh_depth";
316 if (unlikely(eh->eh_max == 0)) {
317 error_msg = "invalid eh_max";
320 max = ext4_ext_max_entries(inode, depth);
321 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
322 error_msg = "too large eh_max";
325 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
326 error_msg = "invalid eh_entries";
332 ext4_error(inode->i_sb, function,
333 "bad header in inode #%lu: %s - magic %x, "
334 "entries %u, max %u(%u), depth %u(%u)",
335 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
336 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
337 max, le16_to_cpu(eh->eh_depth), depth);
342 #define ext4_ext_check_header(inode, eh, depth) \
343 __ext4_ext_check_header(__func__, inode, eh, depth)
346 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
348 int k, l = path->p_depth;
351 for (k = 0; k <= l; k++, path++) {
353 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
354 idx_pblock(path->p_idx));
355 } else if (path->p_ext) {
356 ext_debug(" %d:%d:%llu ",
357 le32_to_cpu(path->p_ext->ee_block),
358 ext4_ext_get_actual_len(path->p_ext),
359 ext_pblock(path->p_ext));
366 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
368 int depth = ext_depth(inode);
369 struct ext4_extent_header *eh;
370 struct ext4_extent *ex;
376 eh = path[depth].p_hdr;
377 ex = EXT_FIRST_EXTENT(eh);
379 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
380 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
381 ext4_ext_get_actual_len(ex), ext_pblock(ex));
386 #define ext4_ext_show_path(inode,path)
387 #define ext4_ext_show_leaf(inode,path)
390 void ext4_ext_drop_refs(struct ext4_ext_path *path)
392 int depth = path->p_depth;
395 for (i = 0; i <= depth; i++, path++)
403 * ext4_ext_binsearch_idx:
404 * binary search for the closest index of the given block
405 * the header must be checked before calling this
408 ext4_ext_binsearch_idx(struct inode *inode,
409 struct ext4_ext_path *path, ext4_lblk_t block)
411 struct ext4_extent_header *eh = path->p_hdr;
412 struct ext4_extent_idx *r, *l, *m;
415 ext_debug("binsearch for %u(idx): ", block);
417 l = EXT_FIRST_INDEX(eh) + 1;
418 r = EXT_LAST_INDEX(eh);
421 if (block < le32_to_cpu(m->ei_block))
425 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
426 m, le32_to_cpu(m->ei_block),
427 r, le32_to_cpu(r->ei_block));
431 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
432 idx_pblock(path->p_idx));
434 #ifdef CHECK_BINSEARCH
436 struct ext4_extent_idx *chix, *ix;
439 chix = ix = EXT_FIRST_INDEX(eh);
440 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
442 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
443 printk("k=%d, ix=0x%p, first=0x%p\n", k,
444 ix, EXT_FIRST_INDEX(eh));
446 le32_to_cpu(ix->ei_block),
447 le32_to_cpu(ix[-1].ei_block));
449 BUG_ON(k && le32_to_cpu(ix->ei_block)
450 <= le32_to_cpu(ix[-1].ei_block));
451 if (block < le32_to_cpu(ix->ei_block))
455 BUG_ON(chix != path->p_idx);
462 * ext4_ext_binsearch:
463 * binary search for closest extent of the given block
464 * the header must be checked before calling this
467 ext4_ext_binsearch(struct inode *inode,
468 struct ext4_ext_path *path, ext4_lblk_t block)
470 struct ext4_extent_header *eh = path->p_hdr;
471 struct ext4_extent *r, *l, *m;
473 if (eh->eh_entries == 0) {
475 * this leaf is empty:
476 * we get such a leaf in split/add case
481 ext_debug("binsearch for %u: ", block);
483 l = EXT_FIRST_EXTENT(eh) + 1;
484 r = EXT_LAST_EXTENT(eh);
488 if (block < le32_to_cpu(m->ee_block))
492 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
493 m, le32_to_cpu(m->ee_block),
494 r, le32_to_cpu(r->ee_block));
498 ext_debug(" -> %d:%llu:%d ",
499 le32_to_cpu(path->p_ext->ee_block),
500 ext_pblock(path->p_ext),
501 ext4_ext_get_actual_len(path->p_ext));
503 #ifdef CHECK_BINSEARCH
505 struct ext4_extent *chex, *ex;
508 chex = ex = EXT_FIRST_EXTENT(eh);
509 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
510 BUG_ON(k && le32_to_cpu(ex->ee_block)
511 <= le32_to_cpu(ex[-1].ee_block));
512 if (block < le32_to_cpu(ex->ee_block))
516 BUG_ON(chex != path->p_ext);
522 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
524 struct ext4_extent_header *eh;
526 eh = ext_inode_hdr(inode);
529 eh->eh_magic = EXT4_EXT_MAGIC;
530 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
531 ext4_mark_inode_dirty(handle, inode);
532 ext4_ext_invalidate_cache(inode);
536 struct ext4_ext_path *
537 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
538 struct ext4_ext_path *path)
540 struct ext4_extent_header *eh;
541 struct buffer_head *bh;
542 short int depth, i, ppos = 0, alloc = 0;
544 eh = ext_inode_hdr(inode);
545 depth = ext_depth(inode);
546 if (ext4_ext_check_header(inode, eh, depth))
547 return ERR_PTR(-EIO);
550 /* account possible depth increase */
552 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
555 return ERR_PTR(-ENOMEM);
562 /* walk through the tree */
564 ext_debug("depth %d: num %d, max %d\n",
565 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
567 ext4_ext_binsearch_idx(inode, path + ppos, block);
568 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
569 path[ppos].p_depth = i;
570 path[ppos].p_ext = NULL;
572 bh = sb_bread(inode->i_sb, path[ppos].p_block);
576 eh = ext_block_hdr(bh);
578 BUG_ON(ppos > depth);
579 path[ppos].p_bh = bh;
580 path[ppos].p_hdr = eh;
583 if (ext4_ext_check_header(inode, eh, i))
587 path[ppos].p_depth = i;
588 path[ppos].p_ext = NULL;
589 path[ppos].p_idx = NULL;
592 ext4_ext_binsearch(inode, path + ppos, block);
593 /* if not an empty leaf */
594 if (path[ppos].p_ext)
595 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
597 ext4_ext_show_path(inode, path);
602 ext4_ext_drop_refs(path);
605 return ERR_PTR(-EIO);
609 * ext4_ext_insert_index:
610 * insert new index [@logical;@ptr] into the block at @curp;
611 * check where to insert: before @curp or after @curp
613 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
614 struct ext4_ext_path *curp,
615 int logical, ext4_fsblk_t ptr)
617 struct ext4_extent_idx *ix;
620 err = ext4_ext_get_access(handle, inode, curp);
624 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
625 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
626 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
628 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
629 len = (len - 1) * sizeof(struct ext4_extent_idx);
630 len = len < 0 ? 0 : len;
631 ext_debug("insert new index %d after: %llu. "
632 "move %d from 0x%p to 0x%p\n",
634 (curp->p_idx + 1), (curp->p_idx + 2));
635 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
637 ix = curp->p_idx + 1;
640 len = len * sizeof(struct ext4_extent_idx);
641 len = len < 0 ? 0 : len;
642 ext_debug("insert new index %d before: %llu. "
643 "move %d from 0x%p to 0x%p\n",
645 curp->p_idx, (curp->p_idx + 1));
646 memmove(curp->p_idx + 1, curp->p_idx, len);
650 ix->ei_block = cpu_to_le32(logical);
651 ext4_idx_store_pblock(ix, ptr);
652 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
654 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
655 > le16_to_cpu(curp->p_hdr->eh_max));
656 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
658 err = ext4_ext_dirty(handle, inode, curp);
659 ext4_std_error(inode->i_sb, err);
666 * inserts new subtree into the path, using free index entry
668 * - allocates all needed blocks (new leaf and all intermediate index blocks)
669 * - makes decision where to split
670 * - moves remaining extents and index entries (right to the split point)
671 * into the newly allocated blocks
672 * - initializes subtree
674 static int ext4_ext_split(handle_t *handle, struct inode *inode,
675 struct ext4_ext_path *path,
676 struct ext4_extent *newext, int at)
678 struct buffer_head *bh = NULL;
679 int depth = ext_depth(inode);
680 struct ext4_extent_header *neh;
681 struct ext4_extent_idx *fidx;
682 struct ext4_extent *ex;
684 ext4_fsblk_t newblock, oldblock;
686 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
689 /* make decision: where to split? */
690 /* FIXME: now decision is simplest: at current extent */
692 /* if current leaf will be split, then we should use
693 * border from split point */
694 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
695 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
696 border = path[depth].p_ext[1].ee_block;
697 ext_debug("leaf will be split."
698 " next leaf starts at %d\n",
699 le32_to_cpu(border));
701 border = newext->ee_block;
702 ext_debug("leaf will be added."
703 " next leaf starts at %d\n",
704 le32_to_cpu(border));
708 * If error occurs, then we break processing
709 * and mark filesystem read-only. index won't
710 * be inserted and tree will be in consistent
711 * state. Next mount will repair buffers too.
715 * Get array to track all allocated blocks.
716 * We need this to handle errors and free blocks
719 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
723 /* allocate all needed blocks */
724 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
725 for (a = 0; a < depth - at; a++) {
726 newblock = ext4_ext_new_meta_block(handle, inode, path,
730 ablocks[a] = newblock;
733 /* initialize new leaf */
734 newblock = ablocks[--a];
735 BUG_ON(newblock == 0);
736 bh = sb_getblk(inode->i_sb, newblock);
743 err = ext4_journal_get_create_access(handle, bh);
747 neh = ext_block_hdr(bh);
749 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
750 neh->eh_magic = EXT4_EXT_MAGIC;
752 ex = EXT_FIRST_EXTENT(neh);
754 /* move remainder of path[depth] to the new leaf */
755 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
756 /* start copy from next extent */
757 /* TODO: we could do it by single memmove */
760 while (path[depth].p_ext <=
761 EXT_MAX_EXTENT(path[depth].p_hdr)) {
762 ext_debug("move %d:%llu:%d in new leaf %llu\n",
763 le32_to_cpu(path[depth].p_ext->ee_block),
764 ext_pblock(path[depth].p_ext),
765 ext4_ext_get_actual_len(path[depth].p_ext),
767 /*memmove(ex++, path[depth].p_ext++,
768 sizeof(struct ext4_extent));
774 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
775 le16_add_cpu(&neh->eh_entries, m);
778 set_buffer_uptodate(bh);
781 err = ext4_journal_dirty_metadata(handle, bh);
787 /* correct old leaf */
789 err = ext4_ext_get_access(handle, inode, path + depth);
792 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
793 err = ext4_ext_dirty(handle, inode, path + depth);
799 /* create intermediate indexes */
803 ext_debug("create %d intermediate indices\n", k);
804 /* insert new index into current index block */
805 /* current depth stored in i var */
809 newblock = ablocks[--a];
810 bh = sb_getblk(inode->i_sb, newblock);
817 err = ext4_journal_get_create_access(handle, bh);
821 neh = ext_block_hdr(bh);
822 neh->eh_entries = cpu_to_le16(1);
823 neh->eh_magic = EXT4_EXT_MAGIC;
824 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
825 neh->eh_depth = cpu_to_le16(depth - i);
826 fidx = EXT_FIRST_INDEX(neh);
827 fidx->ei_block = border;
828 ext4_idx_store_pblock(fidx, oldblock);
830 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
831 i, newblock, le32_to_cpu(border), oldblock);
836 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
837 EXT_MAX_INDEX(path[i].p_hdr));
838 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
839 EXT_LAST_INDEX(path[i].p_hdr));
840 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
841 ext_debug("%d: move %d:%llu in new index %llu\n", i,
842 le32_to_cpu(path[i].p_idx->ei_block),
843 idx_pblock(path[i].p_idx),
845 /*memmove(++fidx, path[i].p_idx++,
846 sizeof(struct ext4_extent_idx));
848 BUG_ON(neh->eh_entries > neh->eh_max);*/
853 memmove(++fidx, path[i].p_idx - m,
854 sizeof(struct ext4_extent_idx) * m);
855 le16_add_cpu(&neh->eh_entries, m);
857 set_buffer_uptodate(bh);
860 err = ext4_journal_dirty_metadata(handle, bh);
866 /* correct old index */
868 err = ext4_ext_get_access(handle, inode, path + i);
871 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
872 err = ext4_ext_dirty(handle, inode, path + i);
880 /* insert new index */
881 err = ext4_ext_insert_index(handle, inode, path + at,
882 le32_to_cpu(border), newblock);
886 if (buffer_locked(bh))
892 /* free all allocated blocks in error case */
893 for (i = 0; i < depth; i++) {
896 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
905 * ext4_ext_grow_indepth:
906 * implements tree growing procedure:
907 * - allocates new block
908 * - moves top-level data (index block or leaf) into the new block
909 * - initializes new top-level, creating index that points to the
912 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
913 struct ext4_ext_path *path,
914 struct ext4_extent *newext)
916 struct ext4_ext_path *curp = path;
917 struct ext4_extent_header *neh;
918 struct ext4_extent_idx *fidx;
919 struct buffer_head *bh;
920 ext4_fsblk_t newblock;
923 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
927 bh = sb_getblk(inode->i_sb, newblock);
930 ext4_std_error(inode->i_sb, err);
935 err = ext4_journal_get_create_access(handle, bh);
941 /* move top-level index/leaf into new block */
942 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
944 /* set size of new block */
945 neh = ext_block_hdr(bh);
946 /* old root could have indexes or leaves
947 * so calculate e_max right way */
948 if (ext_depth(inode))
949 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
951 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
952 neh->eh_magic = EXT4_EXT_MAGIC;
953 set_buffer_uptodate(bh);
956 err = ext4_journal_dirty_metadata(handle, bh);
960 /* create index in new top-level index: num,max,pointer */
961 err = ext4_ext_get_access(handle, inode, curp);
965 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
966 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
967 curp->p_hdr->eh_entries = cpu_to_le16(1);
968 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
970 if (path[0].p_hdr->eh_depth)
971 curp->p_idx->ei_block =
972 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
974 curp->p_idx->ei_block =
975 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
976 ext4_idx_store_pblock(curp->p_idx, newblock);
978 neh = ext_inode_hdr(inode);
979 fidx = EXT_FIRST_INDEX(neh);
980 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
981 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
982 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
984 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
985 err = ext4_ext_dirty(handle, inode, curp);
993 * ext4_ext_create_new_leaf:
994 * finds empty index and adds new leaf.
995 * if no free index is found, then it requests in-depth growing.
997 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
998 struct ext4_ext_path *path,
999 struct ext4_extent *newext)
1001 struct ext4_ext_path *curp;
1002 int depth, i, err = 0;
1005 i = depth = ext_depth(inode);
1007 /* walk up to the tree and look for free index entry */
1008 curp = path + depth;
1009 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1014 /* we use already allocated block for index block,
1015 * so subsequent data blocks should be contiguous */
1016 if (EXT_HAS_FREE_INDEX(curp)) {
1017 /* if we found index with free entry, then use that
1018 * entry: create all needed subtree and add new leaf */
1019 err = ext4_ext_split(handle, inode, path, newext, i);
1024 ext4_ext_drop_refs(path);
1025 path = ext4_ext_find_extent(inode,
1026 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1029 err = PTR_ERR(path);
1031 /* tree is full, time to grow in depth */
1032 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1037 ext4_ext_drop_refs(path);
1038 path = ext4_ext_find_extent(inode,
1039 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1042 err = PTR_ERR(path);
1047 * only first (depth 0 -> 1) produces free space;
1048 * in all other cases we have to split the grown tree
1050 depth = ext_depth(inode);
1051 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1052 /* now we need to split */
1062 * search the closest allocated block to the left for *logical
1063 * and returns it at @logical + it's physical address at @phys
1064 * if *logical is the smallest allocated block, the function
1065 * returns 0 at @phys
1066 * return value contains 0 (success) or error code
1069 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1070 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1072 struct ext4_extent_idx *ix;
1073 struct ext4_extent *ex;
1076 BUG_ON(path == NULL);
1077 depth = path->p_depth;
1080 if (depth == 0 && path->p_ext == NULL)
1083 /* usually extent in the path covers blocks smaller
1084 * then *logical, but it can be that extent is the
1085 * first one in the file */
1087 ex = path[depth].p_ext;
1088 ee_len = ext4_ext_get_actual_len(ex);
1089 if (*logical < le32_to_cpu(ex->ee_block)) {
1090 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1091 while (--depth >= 0) {
1092 ix = path[depth].p_idx;
1093 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1098 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1100 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1101 *phys = ext_pblock(ex) + ee_len - 1;
1106 * search the closest allocated block to the right for *logical
1107 * and returns it at @logical + it's physical address at @phys
1108 * if *logical is the smallest allocated block, the function
1109 * returns 0 at @phys
1110 * return value contains 0 (success) or error code
1113 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1114 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1116 struct buffer_head *bh = NULL;
1117 struct ext4_extent_header *eh;
1118 struct ext4_extent_idx *ix;
1119 struct ext4_extent *ex;
1123 BUG_ON(path == NULL);
1124 depth = path->p_depth;
1127 if (depth == 0 && path->p_ext == NULL)
1130 /* usually extent in the path covers blocks smaller
1131 * then *logical, but it can be that extent is the
1132 * first one in the file */
1134 ex = path[depth].p_ext;
1135 ee_len = ext4_ext_get_actual_len(ex);
1136 if (*logical < le32_to_cpu(ex->ee_block)) {
1137 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1138 while (--depth >= 0) {
1139 ix = path[depth].p_idx;
1140 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1142 *logical = le32_to_cpu(ex->ee_block);
1143 *phys = ext_pblock(ex);
1147 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1149 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1150 /* next allocated block in this leaf */
1152 *logical = le32_to_cpu(ex->ee_block);
1153 *phys = ext_pblock(ex);
1157 /* go up and search for index to the right */
1158 while (--depth >= 0) {
1159 ix = path[depth].p_idx;
1160 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1165 /* we've gone up to the root and
1166 * found no index to the right */
1170 /* we've found index to the right, let's
1171 * follow it and find the closest allocated
1172 * block to the right */
1174 block = idx_pblock(ix);
1175 while (++depth < path->p_depth) {
1176 bh = sb_bread(inode->i_sb, block);
1179 eh = ext_block_hdr(bh);
1180 if (ext4_ext_check_header(inode, eh, depth)) {
1184 ix = EXT_FIRST_INDEX(eh);
1185 block = idx_pblock(ix);
1189 bh = sb_bread(inode->i_sb, block);
1192 eh = ext_block_hdr(bh);
1193 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1197 ex = EXT_FIRST_EXTENT(eh);
1198 *logical = le32_to_cpu(ex->ee_block);
1199 *phys = ext_pblock(ex);
1206 * ext4_ext_next_allocated_block:
1207 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1208 * NOTE: it considers block number from index entry as
1209 * allocated block. Thus, index entries have to be consistent
1213 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1217 BUG_ON(path == NULL);
1218 depth = path->p_depth;
1220 if (depth == 0 && path->p_ext == NULL)
1221 return EXT_MAX_BLOCK;
1223 while (depth >= 0) {
1224 if (depth == path->p_depth) {
1226 if (path[depth].p_ext !=
1227 EXT_LAST_EXTENT(path[depth].p_hdr))
1228 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1231 if (path[depth].p_idx !=
1232 EXT_LAST_INDEX(path[depth].p_hdr))
1233 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1238 return EXT_MAX_BLOCK;
1242 * ext4_ext_next_leaf_block:
1243 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1245 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1246 struct ext4_ext_path *path)
1250 BUG_ON(path == NULL);
1251 depth = path->p_depth;
1253 /* zero-tree has no leaf blocks at all */
1255 return EXT_MAX_BLOCK;
1257 /* go to index block */
1260 while (depth >= 0) {
1261 if (path[depth].p_idx !=
1262 EXT_LAST_INDEX(path[depth].p_hdr))
1263 return (ext4_lblk_t)
1264 le32_to_cpu(path[depth].p_idx[1].ei_block);
1268 return EXT_MAX_BLOCK;
1272 * ext4_ext_correct_indexes:
1273 * if leaf gets modified and modified extent is first in the leaf,
1274 * then we have to correct all indexes above.
1275 * TODO: do we need to correct tree in all cases?
1277 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1278 struct ext4_ext_path *path)
1280 struct ext4_extent_header *eh;
1281 int depth = ext_depth(inode);
1282 struct ext4_extent *ex;
1286 eh = path[depth].p_hdr;
1287 ex = path[depth].p_ext;
1292 /* there is no tree at all */
1296 if (ex != EXT_FIRST_EXTENT(eh)) {
1297 /* we correct tree if first leaf got modified only */
1302 * TODO: we need correction if border is smaller than current one
1305 border = path[depth].p_ext->ee_block;
1306 err = ext4_ext_get_access(handle, inode, path + k);
1309 path[k].p_idx->ei_block = border;
1310 err = ext4_ext_dirty(handle, inode, path + k);
1315 /* change all left-side indexes */
1316 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1318 err = ext4_ext_get_access(handle, inode, path + k);
1321 path[k].p_idx->ei_block = border;
1322 err = ext4_ext_dirty(handle, inode, path + k);
1331 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1332 struct ext4_extent *ex2)
1334 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1337 * Make sure that either both extents are uninitialized, or
1340 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1343 if (ext4_ext_is_uninitialized(ex1))
1344 max_len = EXT_UNINIT_MAX_LEN;
1346 max_len = EXT_INIT_MAX_LEN;
1348 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1349 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1351 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1352 le32_to_cpu(ex2->ee_block))
1356 * To allow future support for preallocated extents to be added
1357 * as an RO_COMPAT feature, refuse to merge to extents if
1358 * this can result in the top bit of ee_len being set.
1360 if (ext1_ee_len + ext2_ee_len > max_len)
1362 #ifdef AGGRESSIVE_TEST
1363 if (ext1_ee_len >= 4)
1367 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1373 * This function tries to merge the "ex" extent to the next extent in the tree.
1374 * It always tries to merge towards right. If you want to merge towards
1375 * left, pass "ex - 1" as argument instead of "ex".
1376 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1377 * 1 if they got merged.
1379 int ext4_ext_try_to_merge(struct inode *inode,
1380 struct ext4_ext_path *path,
1381 struct ext4_extent *ex)
1383 struct ext4_extent_header *eh;
1384 unsigned int depth, len;
1386 int uninitialized = 0;
1388 depth = ext_depth(inode);
1389 BUG_ON(path[depth].p_hdr == NULL);
1390 eh = path[depth].p_hdr;
1392 while (ex < EXT_LAST_EXTENT(eh)) {
1393 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1395 /* merge with next extent! */
1396 if (ext4_ext_is_uninitialized(ex))
1398 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1399 + ext4_ext_get_actual_len(ex + 1));
1401 ext4_ext_mark_uninitialized(ex);
1403 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1404 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1405 * sizeof(struct ext4_extent);
1406 memmove(ex + 1, ex + 2, len);
1408 le16_add_cpu(&eh->eh_entries, -1);
1410 WARN_ON(eh->eh_entries == 0);
1411 if (!eh->eh_entries)
1412 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1413 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1420 * check if a portion of the "newext" extent overlaps with an
1423 * If there is an overlap discovered, it updates the length of the newext
1424 * such that there will be no overlap, and then returns 1.
1425 * If there is no overlap found, it returns 0.
1427 unsigned int ext4_ext_check_overlap(struct inode *inode,
1428 struct ext4_extent *newext,
1429 struct ext4_ext_path *path)
1432 unsigned int depth, len1;
1433 unsigned int ret = 0;
1435 b1 = le32_to_cpu(newext->ee_block);
1436 len1 = ext4_ext_get_actual_len(newext);
1437 depth = ext_depth(inode);
1438 if (!path[depth].p_ext)
1440 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1443 * get the next allocated block if the extent in the path
1444 * is before the requested block(s)
1447 b2 = ext4_ext_next_allocated_block(path);
1448 if (b2 == EXT_MAX_BLOCK)
1452 /* check for wrap through zero on extent logical start block*/
1453 if (b1 + len1 < b1) {
1454 len1 = EXT_MAX_BLOCK - b1;
1455 newext->ee_len = cpu_to_le16(len1);
1459 /* check for overlap */
1460 if (b1 + len1 > b2) {
1461 newext->ee_len = cpu_to_le16(b2 - b1);
1469 * ext4_ext_insert_extent:
1470 * tries to merge requsted extent into the existing extent or
1471 * inserts requested extent as new one into the tree,
1472 * creating new leaf in the no-space case.
1474 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1475 struct ext4_ext_path *path,
1476 struct ext4_extent *newext)
1478 struct ext4_extent_header * eh;
1479 struct ext4_extent *ex, *fex;
1480 struct ext4_extent *nearex; /* nearest extent */
1481 struct ext4_ext_path *npath = NULL;
1482 int depth, len, err;
1484 unsigned uninitialized = 0;
1486 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1487 depth = ext_depth(inode);
1488 ex = path[depth].p_ext;
1489 BUG_ON(path[depth].p_hdr == NULL);
1491 /* try to insert block into found extent and return */
1492 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
1493 ext_debug("append %d block to %d:%d (from %llu)\n",
1494 ext4_ext_get_actual_len(newext),
1495 le32_to_cpu(ex->ee_block),
1496 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1497 err = ext4_ext_get_access(handle, inode, path + depth);
1502 * ext4_can_extents_be_merged should have checked that either
1503 * both extents are uninitialized, or both aren't. Thus we
1504 * need to check only one of them here.
1506 if (ext4_ext_is_uninitialized(ex))
1508 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1509 + ext4_ext_get_actual_len(newext));
1511 ext4_ext_mark_uninitialized(ex);
1512 eh = path[depth].p_hdr;
1518 depth = ext_depth(inode);
1519 eh = path[depth].p_hdr;
1520 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1523 /* probably next leaf has space for us? */
1524 fex = EXT_LAST_EXTENT(eh);
1525 next = ext4_ext_next_leaf_block(inode, path);
1526 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1527 && next != EXT_MAX_BLOCK) {
1528 ext_debug("next leaf block - %d\n", next);
1529 BUG_ON(npath != NULL);
1530 npath = ext4_ext_find_extent(inode, next, NULL);
1532 return PTR_ERR(npath);
1533 BUG_ON(npath->p_depth != path->p_depth);
1534 eh = npath[depth].p_hdr;
1535 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1536 ext_debug("next leaf isnt full(%d)\n",
1537 le16_to_cpu(eh->eh_entries));
1541 ext_debug("next leaf has no free space(%d,%d)\n",
1542 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1546 * There is no free space in the found leaf.
1547 * We're gonna add a new leaf in the tree.
1549 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1552 depth = ext_depth(inode);
1553 eh = path[depth].p_hdr;
1556 nearex = path[depth].p_ext;
1558 err = ext4_ext_get_access(handle, inode, path + depth);
1563 /* there is no extent in this leaf, create first one */
1564 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1565 le32_to_cpu(newext->ee_block),
1567 ext4_ext_get_actual_len(newext));
1568 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1569 } else if (le32_to_cpu(newext->ee_block)
1570 > le32_to_cpu(nearex->ee_block)) {
1571 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1572 if (nearex != EXT_LAST_EXTENT(eh)) {
1573 len = EXT_MAX_EXTENT(eh) - nearex;
1574 len = (len - 1) * sizeof(struct ext4_extent);
1575 len = len < 0 ? 0 : len;
1576 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1577 "move %d from 0x%p to 0x%p\n",
1578 le32_to_cpu(newext->ee_block),
1580 ext4_ext_get_actual_len(newext),
1581 nearex, len, nearex + 1, nearex + 2);
1582 memmove(nearex + 2, nearex + 1, len);
1584 path[depth].p_ext = nearex + 1;
1586 BUG_ON(newext->ee_block == nearex->ee_block);
1587 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1588 len = len < 0 ? 0 : len;
1589 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1590 "move %d from 0x%p to 0x%p\n",
1591 le32_to_cpu(newext->ee_block),
1593 ext4_ext_get_actual_len(newext),
1594 nearex, len, nearex + 1, nearex + 2);
1595 memmove(nearex + 1, nearex, len);
1596 path[depth].p_ext = nearex;
1599 le16_add_cpu(&eh->eh_entries, 1);
1600 nearex = path[depth].p_ext;
1601 nearex->ee_block = newext->ee_block;
1602 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1603 nearex->ee_len = newext->ee_len;
1606 /* try to merge extents to the right */
1607 ext4_ext_try_to_merge(inode, path, nearex);
1609 /* try to merge extents to the left */
1611 /* time to correct all indexes above */
1612 err = ext4_ext_correct_indexes(handle, inode, path);
1616 err = ext4_ext_dirty(handle, inode, path + depth);
1620 ext4_ext_drop_refs(npath);
1623 ext4_ext_tree_changed(inode);
1624 ext4_ext_invalidate_cache(inode);
1629 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1630 __u32 len, ext4_fsblk_t start, int type)
1632 struct ext4_ext_cache *cex;
1634 cex = &EXT4_I(inode)->i_cached_extent;
1635 cex->ec_type = type;
1636 cex->ec_block = block;
1638 cex->ec_start = start;
1642 * ext4_ext_put_gap_in_cache:
1643 * calculate boundaries of the gap that the requested block fits into
1644 * and cache this gap
1647 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1650 int depth = ext_depth(inode);
1653 struct ext4_extent *ex;
1655 ex = path[depth].p_ext;
1657 /* there is no extent yet, so gap is [0;-] */
1659 len = EXT_MAX_BLOCK;
1660 ext_debug("cache gap(whole file):");
1661 } else if (block < le32_to_cpu(ex->ee_block)) {
1663 len = le32_to_cpu(ex->ee_block) - block;
1664 ext_debug("cache gap(before): %u [%u:%u]",
1666 le32_to_cpu(ex->ee_block),
1667 ext4_ext_get_actual_len(ex));
1668 } else if (block >= le32_to_cpu(ex->ee_block)
1669 + ext4_ext_get_actual_len(ex)) {
1671 lblock = le32_to_cpu(ex->ee_block)
1672 + ext4_ext_get_actual_len(ex);
1674 next = ext4_ext_next_allocated_block(path);
1675 ext_debug("cache gap(after): [%u:%u] %u",
1676 le32_to_cpu(ex->ee_block),
1677 ext4_ext_get_actual_len(ex),
1679 BUG_ON(next == lblock);
1680 len = next - lblock;
1686 ext_debug(" -> %u:%lu\n", lblock, len);
1687 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1691 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1692 struct ext4_extent *ex)
1694 struct ext4_ext_cache *cex;
1696 cex = &EXT4_I(inode)->i_cached_extent;
1698 /* has cache valid data? */
1699 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1700 return EXT4_EXT_CACHE_NO;
1702 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1703 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1704 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1705 ex->ee_block = cpu_to_le32(cex->ec_block);
1706 ext4_ext_store_pblock(ex, cex->ec_start);
1707 ex->ee_len = cpu_to_le16(cex->ec_len);
1708 ext_debug("%u cached by %u:%u:%llu\n",
1710 cex->ec_block, cex->ec_len, cex->ec_start);
1711 return cex->ec_type;
1715 return EXT4_EXT_CACHE_NO;
1720 * removes index from the index block.
1721 * It's used in truncate case only, thus all requests are for
1722 * last index in the block only.
1724 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1725 struct ext4_ext_path *path)
1727 struct buffer_head *bh;
1731 /* free index block */
1733 leaf = idx_pblock(path->p_idx);
1734 BUG_ON(path->p_hdr->eh_entries == 0);
1735 err = ext4_ext_get_access(handle, inode, path);
1738 le16_add_cpu(&path->p_hdr->eh_entries, -1);
1739 err = ext4_ext_dirty(handle, inode, path);
1742 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1743 bh = sb_find_get_block(inode->i_sb, leaf);
1744 ext4_forget(handle, 1, inode, bh, leaf);
1745 ext4_free_blocks(handle, inode, leaf, 1, 1);
1750 * ext4_ext_calc_credits_for_insert:
1751 * This routine returns max. credits that the extent tree can consume.
1752 * It should be OK for low-performance paths like ->writepage()
1753 * To allow many writing processes to fit into a single transaction,
1754 * the caller should calculate credits under i_data_sem and
1755 * pass the actual path.
1757 int ext4_ext_calc_credits_for_insert(struct inode *inode,
1758 struct ext4_ext_path *path)
1763 /* probably there is space in leaf? */
1764 depth = ext_depth(inode);
1765 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1766 < le16_to_cpu(path[depth].p_hdr->eh_max))
1771 * given 32-bit logical block (4294967296 blocks), max. tree
1772 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1773 * Let's also add one more level for imbalance.
1777 /* allocation of new data block(s) */
1781 * tree can be full, so it would need to grow in depth:
1782 * we need one credit to modify old root, credits for
1783 * new root will be added in split accounting
1788 * Index split can happen, we would need:
1789 * allocate intermediate indexes (bitmap + group)
1790 * + change two blocks at each level, but root (already included)
1792 needed += (depth * 2) + (depth * 2);
1794 /* any allocation modifies superblock */
1800 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1801 struct ext4_extent *ex,
1802 ext4_lblk_t from, ext4_lblk_t to)
1804 struct buffer_head *bh;
1805 unsigned short ee_len = ext4_ext_get_actual_len(ex);
1806 int i, metadata = 0;
1808 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1810 #ifdef EXTENTS_STATS
1812 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1813 spin_lock(&sbi->s_ext_stats_lock);
1814 sbi->s_ext_blocks += ee_len;
1815 sbi->s_ext_extents++;
1816 if (ee_len < sbi->s_ext_min)
1817 sbi->s_ext_min = ee_len;
1818 if (ee_len > sbi->s_ext_max)
1819 sbi->s_ext_max = ee_len;
1820 if (ext_depth(inode) > sbi->s_depth_max)
1821 sbi->s_depth_max = ext_depth(inode);
1822 spin_unlock(&sbi->s_ext_stats_lock);
1825 if (from >= le32_to_cpu(ex->ee_block)
1826 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1831 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1832 start = ext_pblock(ex) + ee_len - num;
1833 ext_debug("free last %u blocks starting %llu\n", num, start);
1834 for (i = 0; i < num; i++) {
1835 bh = sb_find_get_block(inode->i_sb, start + i);
1836 ext4_forget(handle, 0, inode, bh, start + i);
1838 ext4_free_blocks(handle, inode, start, num, metadata);
1839 } else if (from == le32_to_cpu(ex->ee_block)
1840 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1841 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
1842 from, to, le32_to_cpu(ex->ee_block), ee_len);
1844 printk(KERN_INFO "strange request: removal(2) "
1845 "%u-%u from %u:%u\n",
1846 from, to, le32_to_cpu(ex->ee_block), ee_len);
1852 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1853 struct ext4_ext_path *path, ext4_lblk_t start)
1855 int err = 0, correct_index = 0;
1856 int depth = ext_depth(inode), credits;
1857 struct ext4_extent_header *eh;
1858 ext4_lblk_t a, b, block;
1860 ext4_lblk_t ex_ee_block;
1861 unsigned short ex_ee_len;
1862 unsigned uninitialized = 0;
1863 struct ext4_extent *ex;
1865 /* the header must be checked already in ext4_ext_remove_space() */
1866 ext_debug("truncate since %u in leaf\n", start);
1867 if (!path[depth].p_hdr)
1868 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1869 eh = path[depth].p_hdr;
1872 /* find where to start removing */
1873 ex = EXT_LAST_EXTENT(eh);
1875 ex_ee_block = le32_to_cpu(ex->ee_block);
1876 if (ext4_ext_is_uninitialized(ex))
1878 ex_ee_len = ext4_ext_get_actual_len(ex);
1880 while (ex >= EXT_FIRST_EXTENT(eh) &&
1881 ex_ee_block + ex_ee_len > start) {
1882 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1883 path[depth].p_ext = ex;
1885 a = ex_ee_block > start ? ex_ee_block : start;
1886 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1887 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1889 ext_debug(" border %u:%u\n", a, b);
1891 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1895 } else if (a != ex_ee_block) {
1896 /* remove tail of the extent */
1897 block = ex_ee_block;
1899 } else if (b != ex_ee_block + ex_ee_len - 1) {
1900 /* remove head of the extent */
1903 /* there is no "make a hole" API yet */
1906 /* remove whole extent: excellent! */
1907 block = ex_ee_block;
1909 BUG_ON(a != ex_ee_block);
1910 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1913 /* at present, extent can't cross block group: */
1914 /* leaf + bitmap + group desc + sb + inode */
1916 if (ex == EXT_FIRST_EXTENT(eh)) {
1918 credits += (ext_depth(inode)) + 1;
1921 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1924 err = ext4_ext_journal_restart(handle, credits);
1928 err = ext4_ext_get_access(handle, inode, path + depth);
1932 err = ext4_remove_blocks(handle, inode, ex, a, b);
1937 /* this extent is removed; mark slot entirely unused */
1938 ext4_ext_store_pblock(ex, 0);
1939 le16_add_cpu(&eh->eh_entries, -1);
1942 ex->ee_block = cpu_to_le32(block);
1943 ex->ee_len = cpu_to_le16(num);
1945 * Do not mark uninitialized if all the blocks in the
1946 * extent have been removed.
1948 if (uninitialized && num)
1949 ext4_ext_mark_uninitialized(ex);
1951 err = ext4_ext_dirty(handle, inode, path + depth);
1955 ext_debug("new extent: %u:%u:%llu\n", block, num,
1958 ex_ee_block = le32_to_cpu(ex->ee_block);
1959 ex_ee_len = ext4_ext_get_actual_len(ex);
1962 if (correct_index && eh->eh_entries)
1963 err = ext4_ext_correct_indexes(handle, inode, path);
1965 /* if this leaf is free, then we should
1966 * remove it from index block above */
1967 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1968 err = ext4_ext_rm_idx(handle, inode, path + depth);
1975 * ext4_ext_more_to_rm:
1976 * returns 1 if current index has to be freed (even partial)
1979 ext4_ext_more_to_rm(struct ext4_ext_path *path)
1981 BUG_ON(path->p_idx == NULL);
1983 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1987 * if truncate on deeper level happened, it wasn't partial,
1988 * so we have to consider current index for truncation
1990 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1995 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
1997 struct super_block *sb = inode->i_sb;
1998 int depth = ext_depth(inode);
1999 struct ext4_ext_path *path;
2003 ext_debug("truncate since %u\n", start);
2005 /* probably first extent we're gonna free will be last in block */
2006 handle = ext4_journal_start(inode, depth + 1);
2008 return PTR_ERR(handle);
2010 ext4_ext_invalidate_cache(inode);
2013 * We start scanning from right side, freeing all the blocks
2014 * after i_size and walking into the tree depth-wise.
2016 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2018 ext4_journal_stop(handle);
2021 path[0].p_hdr = ext_inode_hdr(inode);
2022 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
2026 path[0].p_depth = depth;
2028 while (i >= 0 && err == 0) {
2030 /* this is leaf block */
2031 err = ext4_ext_rm_leaf(handle, inode, path, start);
2032 /* root level has p_bh == NULL, brelse() eats this */
2033 brelse(path[i].p_bh);
2034 path[i].p_bh = NULL;
2039 /* this is index block */
2040 if (!path[i].p_hdr) {
2041 ext_debug("initialize header\n");
2042 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2045 if (!path[i].p_idx) {
2046 /* this level hasn't been touched yet */
2047 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2048 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2049 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2051 le16_to_cpu(path[i].p_hdr->eh_entries));
2053 /* we were already here, see at next index */
2057 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2058 i, EXT_FIRST_INDEX(path[i].p_hdr),
2060 if (ext4_ext_more_to_rm(path + i)) {
2061 struct buffer_head *bh;
2062 /* go to the next level */
2063 ext_debug("move to level %d (block %llu)\n",
2064 i + 1, idx_pblock(path[i].p_idx));
2065 memset(path + i + 1, 0, sizeof(*path));
2066 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2068 /* should we reset i_size? */
2072 if (WARN_ON(i + 1 > depth)) {
2076 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2081 path[i + 1].p_bh = bh;
2083 /* save actual number of indexes since this
2084 * number is changed at the next iteration */
2085 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2088 /* we finished processing this index, go up */
2089 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2090 /* index is empty, remove it;
2091 * handle must be already prepared by the
2092 * truncatei_leaf() */
2093 err = ext4_ext_rm_idx(handle, inode, path + i);
2095 /* root level has p_bh == NULL, brelse() eats this */
2096 brelse(path[i].p_bh);
2097 path[i].p_bh = NULL;
2099 ext_debug("return to level %d\n", i);
2103 /* TODO: flexible tree reduction should be here */
2104 if (path->p_hdr->eh_entries == 0) {
2106 * truncate to zero freed all the tree,
2107 * so we need to correct eh_depth
2109 err = ext4_ext_get_access(handle, inode, path);
2111 ext_inode_hdr(inode)->eh_depth = 0;
2112 ext_inode_hdr(inode)->eh_max =
2113 cpu_to_le16(ext4_ext_space_root(inode));
2114 err = ext4_ext_dirty(handle, inode, path);
2118 ext4_ext_tree_changed(inode);
2119 ext4_ext_drop_refs(path);
2121 ext4_journal_stop(handle);
2127 * called at mount time
2129 void ext4_ext_init(struct super_block *sb)
2132 * possible initialization would be here
2135 if (test_opt(sb, EXTENTS)) {
2136 printk("EXT4-fs: file extents enabled");
2137 #ifdef AGGRESSIVE_TEST
2138 printk(", aggressive tests");
2140 #ifdef CHECK_BINSEARCH
2141 printk(", check binsearch");
2143 #ifdef EXTENTS_STATS
2147 #ifdef EXTENTS_STATS
2148 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2149 EXT4_SB(sb)->s_ext_min = 1 << 30;
2150 EXT4_SB(sb)->s_ext_max = 0;
2156 * called at umount time
2158 void ext4_ext_release(struct super_block *sb)
2160 if (!test_opt(sb, EXTENTS))
2163 #ifdef EXTENTS_STATS
2164 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2165 struct ext4_sb_info *sbi = EXT4_SB(sb);
2166 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2167 sbi->s_ext_blocks, sbi->s_ext_extents,
2168 sbi->s_ext_blocks / sbi->s_ext_extents);
2169 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2170 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2175 static void bi_complete(struct bio *bio, int error)
2177 complete((struct completion *)bio->bi_private);
2180 /* FIXME!! we need to try to merge to left or right after zero-out */
2181 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2185 int blkbits, blocksize;
2187 struct completion event;
2188 unsigned int ee_len, len, done, offset;
2191 blkbits = inode->i_blkbits;
2192 blocksize = inode->i_sb->s_blocksize;
2193 ee_len = ext4_ext_get_actual_len(ex);
2194 ee_pblock = ext_pblock(ex);
2196 /* convert ee_pblock to 512 byte sectors */
2197 ee_pblock = ee_pblock << (blkbits - 9);
2199 while (ee_len > 0) {
2201 if (ee_len > BIO_MAX_PAGES)
2202 len = BIO_MAX_PAGES;
2206 bio = bio_alloc(GFP_NOIO, len);
2209 bio->bi_sector = ee_pblock;
2210 bio->bi_bdev = inode->i_sb->s_bdev;
2214 while (done < len) {
2215 ret = bio_add_page(bio, ZERO_PAGE(0),
2217 if (ret != blocksize) {
2219 * We can't add any more pages because of
2220 * hardware limitations. Start a new bio.
2225 offset += blocksize;
2226 if (offset >= PAGE_CACHE_SIZE)
2230 init_completion(&event);
2231 bio->bi_private = &event;
2232 bio->bi_end_io = bi_complete;
2233 submit_bio(WRITE, bio);
2234 wait_for_completion(&event);
2236 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2244 ee_pblock += done << (blkbits - 9);
2249 #define EXT4_EXT_ZERO_LEN 7
2252 * This function is called by ext4_ext_get_blocks() if someone tries to write
2253 * to an uninitialized extent. It may result in splitting the uninitialized
2254 * extent into multiple extents (upto three - one initialized and two
2256 * There are three possibilities:
2257 * a> There is no split required: Entire extent should be initialized
2258 * b> Splits in two extents: Write is happening at either end of the extent
2259 * c> Splits in three extents: Somone is writing in middle of the extent
2261 static int ext4_ext_convert_to_initialized(handle_t *handle,
2262 struct inode *inode,
2263 struct ext4_ext_path *path,
2265 unsigned long max_blocks)
2267 struct ext4_extent *ex, newex, orig_ex;
2268 struct ext4_extent *ex1 = NULL;
2269 struct ext4_extent *ex2 = NULL;
2270 struct ext4_extent *ex3 = NULL;
2271 struct ext4_extent_header *eh;
2272 ext4_lblk_t ee_block;
2273 unsigned int allocated, ee_len, depth;
2274 ext4_fsblk_t newblock;
2278 depth = ext_depth(inode);
2279 eh = path[depth].p_hdr;
2280 ex = path[depth].p_ext;
2281 ee_block = le32_to_cpu(ex->ee_block);
2282 ee_len = ext4_ext_get_actual_len(ex);
2283 allocated = ee_len - (iblock - ee_block);
2284 newblock = iblock - ee_block + ext_pblock(ex);
2286 orig_ex.ee_block = ex->ee_block;
2287 orig_ex.ee_len = cpu_to_le16(ee_len);
2288 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2290 err = ext4_ext_get_access(handle, inode, path + depth);
2293 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2294 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2295 err = ext4_ext_zeroout(inode, &orig_ex);
2297 goto fix_extent_len;
2298 /* update the extent length and mark as initialized */
2299 ex->ee_block = orig_ex.ee_block;
2300 ex->ee_len = orig_ex.ee_len;
2301 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2302 ext4_ext_dirty(handle, inode, path + depth);
2303 /* zeroed the full extent */
2307 /* ex1: ee_block to iblock - 1 : uninitialized */
2308 if (iblock > ee_block) {
2310 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2311 ext4_ext_mark_uninitialized(ex1);
2315 * for sanity, update the length of the ex2 extent before
2316 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2317 * overlap of blocks.
2319 if (!ex1 && allocated > max_blocks)
2320 ex2->ee_len = cpu_to_le16(max_blocks);
2321 /* ex3: to ee_block + ee_len : uninitialised */
2322 if (allocated > max_blocks) {
2323 unsigned int newdepth;
2324 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2325 if (allocated <= EXT4_EXT_ZERO_LEN) {
2326 /* Mark first half uninitialized.
2327 * Mark second half initialized and zero out the
2328 * initialized extent
2330 ex->ee_block = orig_ex.ee_block;
2331 ex->ee_len = cpu_to_le16(ee_len - allocated);
2332 ext4_ext_mark_uninitialized(ex);
2333 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2334 ext4_ext_dirty(handle, inode, path + depth);
2337 ex3->ee_block = cpu_to_le32(iblock);
2338 ext4_ext_store_pblock(ex3, newblock);
2339 ex3->ee_len = cpu_to_le16(allocated);
2340 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2341 if (err == -ENOSPC) {
2342 err = ext4_ext_zeroout(inode, &orig_ex);
2344 goto fix_extent_len;
2345 ex->ee_block = orig_ex.ee_block;
2346 ex->ee_len = orig_ex.ee_len;
2347 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2348 ext4_ext_dirty(handle, inode, path + depth);
2349 /* zeroed the full extent */
2353 goto fix_extent_len;
2356 * We need to zero out the second half because
2357 * an fallocate request can update file size and
2358 * converting the second half to initialized extent
2359 * implies that we can leak some junk data to user
2362 err = ext4_ext_zeroout(inode, ex3);
2365 * We should actually mark the
2366 * second half as uninit and return error
2367 * Insert would have changed the extent
2369 depth = ext_depth(inode);
2370 ext4_ext_drop_refs(path);
2371 path = ext4_ext_find_extent(inode,
2374 err = PTR_ERR(path);
2377 ex = path[depth].p_ext;
2378 err = ext4_ext_get_access(handle, inode,
2382 ext4_ext_mark_uninitialized(ex);
2383 ext4_ext_dirty(handle, inode, path + depth);
2387 /* zeroed the second half */
2391 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2392 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2393 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2394 ext4_ext_mark_uninitialized(ex3);
2395 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2396 if (err == -ENOSPC) {
2397 err = ext4_ext_zeroout(inode, &orig_ex);
2399 goto fix_extent_len;
2400 /* update the extent length and mark as initialized */
2401 ex->ee_block = orig_ex.ee_block;
2402 ex->ee_len = orig_ex.ee_len;
2403 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2404 ext4_ext_dirty(handle, inode, path + depth);
2405 /* zeroed the full extent */
2409 goto fix_extent_len;
2411 * The depth, and hence eh & ex might change
2412 * as part of the insert above.
2414 newdepth = ext_depth(inode);
2416 * update the extent length after successfull insert of the
2419 orig_ex.ee_len = cpu_to_le16(ee_len -
2420 ext4_ext_get_actual_len(ex3));
2421 if (newdepth != depth) {
2423 ext4_ext_drop_refs(path);
2424 path = ext4_ext_find_extent(inode, iblock, path);
2426 err = PTR_ERR(path);
2429 eh = path[depth].p_hdr;
2430 ex = path[depth].p_ext;
2434 err = ext4_ext_get_access(handle, inode, path + depth);
2438 allocated = max_blocks;
2440 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2441 * to insert a extent in the middle zerout directly
2442 * otherwise give the extent a chance to merge to left
2444 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2445 iblock != ee_block) {
2446 err = ext4_ext_zeroout(inode, &orig_ex);
2448 goto fix_extent_len;
2449 /* update the extent length and mark as initialized */
2450 ex->ee_block = orig_ex.ee_block;
2451 ex->ee_len = orig_ex.ee_len;
2452 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2453 ext4_ext_dirty(handle, inode, path + depth);
2454 /* zero out the first half */
2459 * If there was a change of depth as part of the
2460 * insertion of ex3 above, we need to update the length
2461 * of the ex1 extent again here
2463 if (ex1 && ex1 != ex) {
2465 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2466 ext4_ext_mark_uninitialized(ex1);
2469 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2470 ex2->ee_block = cpu_to_le32(iblock);
2471 ext4_ext_store_pblock(ex2, newblock);
2472 ex2->ee_len = cpu_to_le16(allocated);
2476 * New (initialized) extent starts from the first block
2477 * in the current extent. i.e., ex2 == ex
2478 * We have to see if it can be merged with the extent
2481 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2483 * To merge left, pass "ex2 - 1" to try_to_merge(),
2484 * since it merges towards right _only_.
2486 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2488 err = ext4_ext_correct_indexes(handle, inode, path);
2491 depth = ext_depth(inode);
2496 * Try to Merge towards right. This might be required
2497 * only when the whole extent is being written to.
2498 * i.e. ex2 == ex and ex3 == NULL.
2501 ret = ext4_ext_try_to_merge(inode, path, ex2);
2503 err = ext4_ext_correct_indexes(handle, inode, path);
2508 /* Mark modified extent as dirty */
2509 err = ext4_ext_dirty(handle, inode, path + depth);
2512 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2513 if (err == -ENOSPC) {
2514 err = ext4_ext_zeroout(inode, &orig_ex);
2516 goto fix_extent_len;
2517 /* update the extent length and mark as initialized */
2518 ex->ee_block = orig_ex.ee_block;
2519 ex->ee_len = orig_ex.ee_len;
2520 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2521 ext4_ext_dirty(handle, inode, path + depth);
2522 /* zero out the first half */
2525 goto fix_extent_len;
2527 return err ? err : allocated;
2530 ex->ee_block = orig_ex.ee_block;
2531 ex->ee_len = orig_ex.ee_len;
2532 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2533 ext4_ext_mark_uninitialized(ex);
2534 ext4_ext_dirty(handle, inode, path + depth);
2539 * Block allocation/map/preallocation routine for extents based files
2542 * Need to be called with
2543 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2544 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2546 * return > 0, number of of blocks already mapped/allocated
2547 * if create == 0 and these are pre-allocated blocks
2548 * buffer head is unmapped
2549 * otherwise blocks are mapped
2551 * return = 0, if plain look up failed (blocks have not been allocated)
2552 * buffer head is unmapped
2554 * return < 0, error case.
2556 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2558 unsigned long max_blocks, struct buffer_head *bh_result,
2559 int create, int extend_disksize)
2561 struct ext4_ext_path *path = NULL;
2562 struct ext4_extent_header *eh;
2563 struct ext4_extent newex, *ex;
2564 ext4_fsblk_t goal, newblock;
2565 int err = 0, depth, ret;
2566 unsigned long allocated = 0;
2567 struct ext4_allocation_request ar;
2569 __clear_bit(BH_New, &bh_result->b_state);
2570 ext_debug("blocks %u/%lu requested for inode %u\n",
2571 iblock, max_blocks, inode->i_ino);
2573 /* check in cache */
2574 goal = ext4_ext_in_cache(inode, iblock, &newex);
2576 if (goal == EXT4_EXT_CACHE_GAP) {
2579 * block isn't allocated yet and
2580 * user doesn't want to allocate it
2584 /* we should allocate requested block */
2585 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2586 /* block is already allocated */
2588 - le32_to_cpu(newex.ee_block)
2589 + ext_pblock(&newex);
2590 /* number of remaining blocks in the extent */
2591 allocated = ext4_ext_get_actual_len(&newex) -
2592 (iblock - le32_to_cpu(newex.ee_block));
2599 /* find extent for this block */
2600 path = ext4_ext_find_extent(inode, iblock, NULL);
2602 err = PTR_ERR(path);
2607 depth = ext_depth(inode);
2610 * consistent leaf must not be empty;
2611 * this situation is possible, though, _during_ tree modification;
2612 * this is why assert can't be put in ext4_ext_find_extent()
2614 BUG_ON(path[depth].p_ext == NULL && depth != 0);
2615 eh = path[depth].p_hdr;
2617 ex = path[depth].p_ext;
2619 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2620 ext4_fsblk_t ee_start = ext_pblock(ex);
2621 unsigned short ee_len;
2624 * Uninitialized extents are treated as holes, except that
2625 * we split out initialized portions during a write.
2627 ee_len = ext4_ext_get_actual_len(ex);
2628 /* if found extent covers block, simply return it */
2629 if (iblock >= ee_block && iblock < ee_block + ee_len) {
2630 newblock = iblock - ee_block + ee_start;
2631 /* number of remaining blocks in the extent */
2632 allocated = ee_len - (iblock - ee_block);
2633 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2634 ee_block, ee_len, newblock);
2636 /* Do not put uninitialized extent in the cache */
2637 if (!ext4_ext_is_uninitialized(ex)) {
2638 ext4_ext_put_in_cache(inode, ee_block,
2640 EXT4_EXT_CACHE_EXTENT);
2643 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2647 * We have blocks reserved already. We
2648 * return allocated blocks so that delalloc
2649 * won't do block reservation for us. But
2650 * the buffer head will be unmapped so that
2651 * a read from the block returns 0s.
2653 if (allocated > max_blocks)
2654 allocated = max_blocks;
2655 set_buffer_unwritten(bh_result);
2659 ret = ext4_ext_convert_to_initialized(handle, inode,
2672 * requested block isn't allocated yet;
2673 * we couldn't try to create block if create flag is zero
2677 * put just found gap into cache to speed up
2678 * subsequent requests
2680 ext4_ext_put_gap_in_cache(inode, path, iblock);
2684 * Okay, we need to do block allocation. Lazily initialize the block
2685 * allocation info here if necessary.
2687 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2688 ext4_init_block_alloc_info(inode);
2690 /* find neighbour allocated blocks */
2692 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2696 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2701 * See if request is beyond maximum number of blocks we can have in
2702 * a single extent. For an initialized extent this limit is
2703 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2704 * EXT_UNINIT_MAX_LEN.
2706 if (max_blocks > EXT_INIT_MAX_LEN &&
2707 create != EXT4_CREATE_UNINITIALIZED_EXT)
2708 max_blocks = EXT_INIT_MAX_LEN;
2709 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2710 create == EXT4_CREATE_UNINITIALIZED_EXT)
2711 max_blocks = EXT_UNINIT_MAX_LEN;
2713 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2714 newex.ee_block = cpu_to_le32(iblock);
2715 newex.ee_len = cpu_to_le16(max_blocks);
2716 err = ext4_ext_check_overlap(inode, &newex, path);
2718 allocated = ext4_ext_get_actual_len(&newex);
2720 allocated = max_blocks;
2722 /* allocate new block */
2724 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2725 ar.logical = iblock;
2727 if (S_ISREG(inode->i_mode))
2728 ar.flags = EXT4_MB_HINT_DATA;
2730 /* disable in-core preallocation for non-regular files */
2732 newblock = ext4_mb_new_blocks(handle, &ar, &err);
2735 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2736 goal, newblock, allocated);
2738 /* try to insert new extent into found leaf and return */
2739 ext4_ext_store_pblock(&newex, newblock);
2740 newex.ee_len = cpu_to_le16(ar.len);
2741 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2742 ext4_ext_mark_uninitialized(&newex);
2743 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2745 /* free data blocks we just allocated */
2746 /* not a good idea to call discard here directly,
2747 * but otherwise we'd need to call it every free() */
2748 ext4_mb_discard_inode_preallocations(inode);
2749 ext4_free_blocks(handle, inode, ext_pblock(&newex),
2750 ext4_ext_get_actual_len(&newex), 0);
2754 /* previous routine could use block we allocated */
2755 newblock = ext_pblock(&newex);
2756 allocated = ext4_ext_get_actual_len(&newex);
2758 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2759 EXT4_I(inode)->i_disksize = inode->i_size;
2761 set_buffer_new(bh_result);
2763 /* Cache only when it is _not_ an uninitialized extent */
2764 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2765 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2766 EXT4_EXT_CACHE_EXTENT);
2768 if (allocated > max_blocks)
2769 allocated = max_blocks;
2770 ext4_ext_show_leaf(inode, path);
2771 set_buffer_mapped(bh_result);
2772 bh_result->b_bdev = inode->i_sb->s_bdev;
2773 bh_result->b_blocknr = newblock;
2776 ext4_ext_drop_refs(path);
2779 return err ? err : allocated;
2782 void ext4_ext_truncate(struct inode *inode)
2784 struct address_space *mapping = inode->i_mapping;
2785 struct super_block *sb = inode->i_sb;
2786 ext4_lblk_t last_block;
2791 * probably first extent we're gonna free will be last in block
2793 err = ext4_writepage_trans_blocks(inode) + 3;
2794 handle = ext4_journal_start(inode, err);
2798 if (inode->i_size & (sb->s_blocksize - 1))
2799 ext4_block_truncate_page(handle, mapping, inode->i_size);
2801 if (ext4_orphan_add(handle, inode))
2804 down_write(&EXT4_I(inode)->i_data_sem);
2805 ext4_ext_invalidate_cache(inode);
2807 ext4_mb_discard_inode_preallocations(inode);
2810 * TODO: optimization is possible here.
2811 * Probably we need not scan at all,
2812 * because page truncation is enough.
2815 /* we have to know where to truncate from in crash case */
2816 EXT4_I(inode)->i_disksize = inode->i_size;
2817 ext4_mark_inode_dirty(handle, inode);
2819 last_block = (inode->i_size + sb->s_blocksize - 1)
2820 >> EXT4_BLOCK_SIZE_BITS(sb);
2821 err = ext4_ext_remove_space(inode, last_block);
2823 /* In a multi-transaction truncate, we only make the final
2824 * transaction synchronous.
2830 up_write(&EXT4_I(inode)->i_data_sem);
2832 * If this was a simple ftruncate() and the file will remain alive,
2833 * then we need to clear up the orphan record which we created above.
2834 * However, if this was a real unlink then we were called by
2835 * ext4_delete_inode(), and we allow that function to clean up the
2836 * orphan info for us.
2839 ext4_orphan_del(handle, inode);
2841 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2842 ext4_mark_inode_dirty(handle, inode);
2843 ext4_journal_stop(handle);
2847 * ext4_ext_writepage_trans_blocks:
2848 * calculate max number of blocks we could modify
2849 * in order to allocate new block for an inode
2851 int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2855 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2857 /* caller wants to allocate num blocks, but note it includes sb */
2858 needed = needed * num - (num - 1);
2861 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2867 static void ext4_falloc_update_inode(struct inode *inode,
2868 int mode, loff_t new_size, int update_ctime)
2870 struct timespec now;
2873 now = current_fs_time(inode->i_sb);
2874 if (!timespec_equal(&inode->i_ctime, &now))
2875 inode->i_ctime = now;
2878 * Update only when preallocation was requested beyond
2881 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2882 new_size > i_size_read(inode)) {
2883 i_size_write(inode, new_size);
2884 EXT4_I(inode)->i_disksize = new_size;
2890 * preallocate space for a file. This implements ext4's fallocate inode
2891 * operation, which gets called from sys_fallocate system call.
2892 * For block-mapped files, posix_fallocate should fall back to the method
2893 * of writing zeroes to the required new blocks (the same behavior which is
2894 * expected for file systems which do not support fallocate() system call).
2896 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2901 unsigned long max_blocks;
2905 struct buffer_head map_bh;
2906 unsigned int credits, blkbits = inode->i_blkbits;
2909 * currently supporting (pre)allocate mode for extent-based
2912 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2915 /* preallocation to directories is currently not supported */
2916 if (S_ISDIR(inode->i_mode))
2919 block = offset >> blkbits;
2921 * We can't just convert len to max_blocks because
2922 * If blocksize = 4096 offset = 3072 and len = 2048
2924 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2927 * credits to insert 1 extent into extent tree + buffers to be able to
2928 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2930 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
2931 mutex_lock(&inode->i_mutex);
2933 while (ret >= 0 && ret < max_blocks) {
2934 block = block + ret;
2935 max_blocks = max_blocks - ret;
2936 handle = ext4_journal_start(inode, credits);
2937 if (IS_ERR(handle)) {
2938 ret = PTR_ERR(handle);
2941 ret = ext4_get_blocks_wrap(handle, inode, block,
2942 max_blocks, &map_bh,
2943 EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
2947 printk(KERN_ERR "%s: ext4_ext_get_blocks "
2948 "returned error inode#%lu, block=%u, "
2949 "max_blocks=%lu", __func__,
2950 inode->i_ino, block, max_blocks);
2952 ext4_mark_inode_dirty(handle, inode);
2953 ret2 = ext4_journal_stop(handle);
2956 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2957 blkbits) >> blkbits))
2958 new_size = offset + len;
2960 new_size = (block + ret) << blkbits;
2962 ext4_falloc_update_inode(inode, mode, new_size,
2963 buffer_new(&map_bh));
2964 ext4_mark_inode_dirty(handle, inode);
2965 ret2 = ext4_journal_stop(handle);
2969 if (ret == -ENOSPC &&
2970 ext4_should_retry_alloc(inode->i_sb, &retries)) {
2974 mutex_unlock(&inode->i_mutex);
2975 return ret > 0 ? ret2 : ret;