2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 * Extents support for EXT4
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
34 #include <linux/time.h>
35 #include <linux/ext4_jbd2.h>
36 #include <linux/jbd.h>
37 #include <linux/smp_lock.h>
38 #include <linux/highuid.h>
39 #include <linux/pagemap.h>
40 #include <linux/quotaops.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/ext4_fs_extents.h>
44 #include <asm/uaccess.h>
47 /* this macro combines low and hi parts of phys. blocknr into ext4_fsblk_t */
48 static inline ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
52 block = le32_to_cpu(ex->ee_start);
53 if (sizeof(ext4_fsblk_t) > 4)
54 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
58 /* this macro combines low and hi parts of phys. blocknr into ext4_fsblk_t */
59 static inline ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
63 block = le32_to_cpu(ix->ei_leaf);
64 if (sizeof(ext4_fsblk_t) > 4)
65 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
69 /* the routine stores large phys. blocknr into extent breaking it into parts */
70 static inline void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
72 ex->ee_start = cpu_to_le32((unsigned long) (pb & 0xffffffff));
73 if (sizeof(ext4_fsblk_t) > 4)
74 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
77 /* the routine stores large phys. blocknr into index breaking it into parts */
78 static inline void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
80 ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff));
81 if (sizeof(ext4_fsblk_t) > 4)
82 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
85 static int ext4_ext_check_header(const char *function, struct inode *inode,
86 struct ext4_extent_header *eh)
88 const char *error_msg = NULL;
90 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
91 error_msg = "invalid magic";
94 if (unlikely(eh->eh_max == 0)) {
95 error_msg = "invalid eh_max";
98 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
99 error_msg = "invalid eh_entries";
105 ext4_error(inode->i_sb, function,
106 "bad header in inode #%lu: %s - magic %x, "
107 "entries %u, max %u, depth %u",
108 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
109 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
110 le16_to_cpu(eh->eh_depth));
115 static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
119 if (handle->h_buffer_credits > needed)
121 if (!ext4_journal_extend(handle, needed))
123 err = ext4_journal_restart(handle, needed);
133 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
134 struct ext4_ext_path *path)
137 /* path points to block */
138 return ext4_journal_get_write_access(handle, path->p_bh);
140 /* path points to leaf/index in inode body */
141 /* we use in-core data, no need to protect them */
151 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
152 struct ext4_ext_path *path)
156 /* path points to block */
157 err = ext4_journal_dirty_metadata(handle, path->p_bh);
159 /* path points to leaf/index in inode body */
160 err = ext4_mark_inode_dirty(handle, inode);
165 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
166 struct ext4_ext_path *path,
169 struct ext4_inode_info *ei = EXT4_I(inode);
170 ext4_fsblk_t bg_start;
171 ext4_grpblk_t colour;
175 struct ext4_extent *ex;
176 depth = path->p_depth;
178 /* try to predict block placement */
179 if ((ex = path[depth].p_ext))
180 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
182 /* it looks index is empty
183 * try to find starting from index itself */
184 if (path[depth].p_bh)
185 return path[depth].p_bh->b_blocknr;
188 /* OK. use inode's group */
189 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
190 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
191 colour = (current->pid % 16) *
192 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
193 return bg_start + colour + block;
197 ext4_ext_new_block(handle_t *handle, struct inode *inode,
198 struct ext4_ext_path *path,
199 struct ext4_extent *ex, int *err)
201 ext4_fsblk_t goal, newblock;
203 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
204 newblock = ext4_new_block(handle, inode, goal, err);
208 static inline int ext4_ext_space_block(struct inode *inode)
212 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
213 / sizeof(struct ext4_extent);
214 #ifdef AGRESSIVE_TEST
221 static inline int ext4_ext_space_block_idx(struct inode *inode)
225 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
226 / sizeof(struct ext4_extent_idx);
227 #ifdef AGRESSIVE_TEST
234 static inline int ext4_ext_space_root(struct inode *inode)
238 size = sizeof(EXT4_I(inode)->i_data);
239 size -= sizeof(struct ext4_extent_header);
240 size /= sizeof(struct ext4_extent);
241 #ifdef AGRESSIVE_TEST
248 static inline int ext4_ext_space_root_idx(struct inode *inode)
252 size = sizeof(EXT4_I(inode)->i_data);
253 size -= sizeof(struct ext4_extent_header);
254 size /= sizeof(struct ext4_extent_idx);
255 #ifdef AGRESSIVE_TEST
263 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
265 int k, l = path->p_depth;
268 for (k = 0; k <= l; k++, path++) {
270 ext_debug(" %d->"E3FSBLK, le32_to_cpu(path->p_idx->ei_block),
271 idx_pblock(path->p_idx));
272 } else if (path->p_ext) {
273 ext_debug(" %d:%d:"E3FSBLK" ",
274 le32_to_cpu(path->p_ext->ee_block),
275 le16_to_cpu(path->p_ext->ee_len),
276 ext_pblock(path->p_ext));
283 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
285 int depth = ext_depth(inode);
286 struct ext4_extent_header *eh;
287 struct ext4_extent *ex;
293 eh = path[depth].p_hdr;
294 ex = EXT_FIRST_EXTENT(eh);
296 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
297 ext_debug("%d:%d:"E3FSBLK" ", le32_to_cpu(ex->ee_block),
298 le16_to_cpu(ex->ee_len), ext_pblock(ex));
303 #define ext4_ext_show_path(inode,path)
304 #define ext4_ext_show_leaf(inode,path)
307 static void ext4_ext_drop_refs(struct ext4_ext_path *path)
309 int depth = path->p_depth;
312 for (i = 0; i <= depth; i++, path++)
320 * binary search for closest index by given block
323 ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block)
325 struct ext4_extent_header *eh = path->p_hdr;
326 struct ext4_extent_idx *r, *l, *m;
328 BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
329 BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
330 BUG_ON(le16_to_cpu(eh->eh_entries) <= 0);
332 ext_debug("binsearch for %d(idx): ", block);
334 l = EXT_FIRST_INDEX(eh) + 1;
335 r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
338 if (block < le32_to_cpu(m->ei_block))
342 ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
343 m, m->ei_block, r, r->ei_block);
347 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
348 idx_block(path->p_idx));
350 #ifdef CHECK_BINSEARCH
352 struct ext4_extent_idx *chix, *ix;
355 chix = ix = EXT_FIRST_INDEX(eh);
356 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
358 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
359 printk("k=%d, ix=0x%p, first=0x%p\n", k,
360 ix, EXT_FIRST_INDEX(eh));
362 le32_to_cpu(ix->ei_block),
363 le32_to_cpu(ix[-1].ei_block));
365 BUG_ON(k && le32_to_cpu(ix->ei_block)
366 <= le32_to_cpu(ix[-1].ei_block));
367 if (block < le32_to_cpu(ix->ei_block))
371 BUG_ON(chix != path->p_idx);
378 * binary search for closest extent by given block
381 ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block)
383 struct ext4_extent_header *eh = path->p_hdr;
384 struct ext4_extent *r, *l, *m;
386 BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
387 BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
389 if (eh->eh_entries == 0) {
391 * this leaf is empty yet:
392 * we get such a leaf in split/add case
397 ext_debug("binsearch for %d: ", block);
399 l = EXT_FIRST_EXTENT(eh) + 1;
400 r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
404 if (block < le32_to_cpu(m->ee_block))
408 ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
409 m, m->ee_block, r, r->ee_block);
413 ext_debug(" -> %d:"E3FSBLK":%d ",
414 le32_to_cpu(path->p_ext->ee_block),
415 ext_pblock(path->p_ext),
416 le16_to_cpu(path->p_ext->ee_len));
418 #ifdef CHECK_BINSEARCH
420 struct ext4_extent *chex, *ex;
423 chex = ex = EXT_FIRST_EXTENT(eh);
424 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
425 BUG_ON(k && le32_to_cpu(ex->ee_block)
426 <= le32_to_cpu(ex[-1].ee_block));
427 if (block < le32_to_cpu(ex->ee_block))
431 BUG_ON(chex != path->p_ext);
437 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
439 struct ext4_extent_header *eh;
441 eh = ext_inode_hdr(inode);
444 eh->eh_magic = EXT4_EXT_MAGIC;
445 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
446 ext4_mark_inode_dirty(handle, inode);
447 ext4_ext_invalidate_cache(inode);
451 struct ext4_ext_path *
452 ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path)
454 struct ext4_extent_header *eh;
455 struct buffer_head *bh;
456 short int depth, i, ppos = 0, alloc = 0;
458 eh = ext_inode_hdr(inode);
460 if (ext4_ext_check_header(__FUNCTION__, inode, eh))
461 return ERR_PTR(-EIO);
463 i = depth = ext_depth(inode);
465 /* account possible depth increase */
467 path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 2),
470 return ERR_PTR(-ENOMEM);
473 memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
476 /* walk through the tree */
478 ext_debug("depth %d: num %d, max %d\n",
479 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
480 ext4_ext_binsearch_idx(inode, path + ppos, block);
481 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
482 path[ppos].p_depth = i;
483 path[ppos].p_ext = NULL;
485 bh = sb_bread(inode->i_sb, path[ppos].p_block);
489 eh = ext_block_hdr(bh);
491 BUG_ON(ppos > depth);
492 path[ppos].p_bh = bh;
493 path[ppos].p_hdr = eh;
496 if (ext4_ext_check_header(__FUNCTION__, inode, eh))
500 path[ppos].p_depth = i;
501 path[ppos].p_hdr = eh;
502 path[ppos].p_ext = NULL;
503 path[ppos].p_idx = NULL;
505 if (ext4_ext_check_header(__FUNCTION__, inode, eh))
509 ext4_ext_binsearch(inode, path + ppos, block);
511 ext4_ext_show_path(inode, path);
516 ext4_ext_drop_refs(path);
519 return ERR_PTR(-EIO);
523 * insert new index [logical;ptr] into the block at cupr
524 * it check where to insert: before curp or after curp
526 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
527 struct ext4_ext_path *curp,
528 int logical, ext4_fsblk_t ptr)
530 struct ext4_extent_idx *ix;
533 if ((err = ext4_ext_get_access(handle, inode, curp)))
536 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
537 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
538 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
540 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
541 len = (len - 1) * sizeof(struct ext4_extent_idx);
542 len = len < 0 ? 0 : len;
543 ext_debug("insert new index %d after: %d. "
544 "move %d from 0x%p to 0x%p\n",
546 (curp->p_idx + 1), (curp->p_idx + 2));
547 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
549 ix = curp->p_idx + 1;
552 len = len * sizeof(struct ext4_extent_idx);
553 len = len < 0 ? 0 : len;
554 ext_debug("insert new index %d before: %d. "
555 "move %d from 0x%p to 0x%p\n",
557 curp->p_idx, (curp->p_idx + 1));
558 memmove(curp->p_idx + 1, curp->p_idx, len);
562 ix->ei_block = cpu_to_le32(logical);
563 ext4_idx_store_pblock(ix, ptr);
564 curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
566 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
567 > le16_to_cpu(curp->p_hdr->eh_max));
568 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
570 err = ext4_ext_dirty(handle, inode, curp);
571 ext4_std_error(inode->i_sb, err);
577 * routine inserts new subtree into the path, using free index entry
579 * - allocates all needed blocks (new leaf and all intermediate index blocks)
580 * - makes decision where to split
581 * - moves remaining extens and index entries (right to the split point)
582 * into the newly allocated blocks
583 * - initialize subtree
585 static int ext4_ext_split(handle_t *handle, struct inode *inode,
586 struct ext4_ext_path *path,
587 struct ext4_extent *newext, int at)
589 struct buffer_head *bh = NULL;
590 int depth = ext_depth(inode);
591 struct ext4_extent_header *neh;
592 struct ext4_extent_idx *fidx;
593 struct ext4_extent *ex;
595 ext4_fsblk_t newblock, oldblock;
597 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
600 /* make decision: where to split? */
601 /* FIXME: now desicion is simplest: at current extent */
603 /* if current leaf will be splitted, then we should use
604 * border from split point */
605 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
606 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
607 border = path[depth].p_ext[1].ee_block;
608 ext_debug("leaf will be splitted."
609 " next leaf starts at %d\n",
610 le32_to_cpu(border));
612 border = newext->ee_block;
613 ext_debug("leaf will be added."
614 " next leaf starts at %d\n",
615 le32_to_cpu(border));
619 * if error occurs, then we break processing
620 * and turn filesystem read-only. so, index won't
621 * be inserted and tree will be in consistent
622 * state. next mount will repair buffers too
626 * get array to track all allocated blocks
627 * we need this to handle errors and free blocks
630 ablocks = kmalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
633 memset(ablocks, 0, sizeof(ext4_fsblk_t) * depth);
635 /* allocate all needed blocks */
636 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
637 for (a = 0; a < depth - at; a++) {
638 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
641 ablocks[a] = newblock;
644 /* initialize new leaf */
645 newblock = ablocks[--a];
646 BUG_ON(newblock == 0);
647 bh = sb_getblk(inode->i_sb, newblock);
654 if ((err = ext4_journal_get_create_access(handle, bh)))
657 neh = ext_block_hdr(bh);
659 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
660 neh->eh_magic = EXT4_EXT_MAGIC;
662 ex = EXT_FIRST_EXTENT(neh);
664 /* move remain of path[depth] to the new leaf */
665 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
666 /* start copy from next extent */
667 /* TODO: we could do it by single memmove */
670 while (path[depth].p_ext <=
671 EXT_MAX_EXTENT(path[depth].p_hdr)) {
672 ext_debug("move %d:"E3FSBLK":%d in new leaf "E3FSBLK"\n",
673 le32_to_cpu(path[depth].p_ext->ee_block),
674 ext_pblock(path[depth].p_ext),
675 le16_to_cpu(path[depth].p_ext->ee_len),
677 /*memmove(ex++, path[depth].p_ext++,
678 sizeof(struct ext4_extent));
684 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
685 neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
688 set_buffer_uptodate(bh);
691 if ((err = ext4_journal_dirty_metadata(handle, bh)))
696 /* correct old leaf */
698 if ((err = ext4_ext_get_access(handle, inode, path + depth)))
700 path[depth].p_hdr->eh_entries =
701 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
702 if ((err = ext4_ext_dirty(handle, inode, path + depth)))
707 /* create intermediate indexes */
711 ext_debug("create %d intermediate indices\n", k);
712 /* insert new index into current index block */
713 /* current depth stored in i var */
717 newblock = ablocks[--a];
718 bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
725 if ((err = ext4_journal_get_create_access(handle, bh)))
728 neh = ext_block_hdr(bh);
729 neh->eh_entries = cpu_to_le16(1);
730 neh->eh_magic = EXT4_EXT_MAGIC;
731 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
732 neh->eh_depth = cpu_to_le16(depth - i);
733 fidx = EXT_FIRST_INDEX(neh);
734 fidx->ei_block = border;
735 ext4_idx_store_pblock(fidx, oldblock);
737 ext_debug("int.index at %d (block "E3FSBLK"): %lu -> "E3FSBLK"\n", i,
738 newblock, (unsigned long) le32_to_cpu(border),
744 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
745 EXT_MAX_INDEX(path[i].p_hdr));
746 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
747 EXT_LAST_INDEX(path[i].p_hdr));
748 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
749 ext_debug("%d: move %d:%d in new index "E3FSBLK"\n", i,
750 le32_to_cpu(path[i].p_idx->ei_block),
751 idx_pblock(path[i].p_idx),
753 /*memmove(++fidx, path[i].p_idx++,
754 sizeof(struct ext4_extent_idx));
756 BUG_ON(neh->eh_entries > neh->eh_max);*/
761 memmove(++fidx, path[i].p_idx - m,
762 sizeof(struct ext4_extent_idx) * m);
764 cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
766 set_buffer_uptodate(bh);
769 if ((err = ext4_journal_dirty_metadata(handle, bh)))
774 /* correct old index */
776 err = ext4_ext_get_access(handle, inode, path + i);
779 path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
780 err = ext4_ext_dirty(handle, inode, path + i);
788 /* insert new index */
792 err = ext4_ext_insert_index(handle, inode, path + at,
793 le32_to_cpu(border), newblock);
797 if (buffer_locked(bh))
803 /* free all allocated blocks in error case */
804 for (i = 0; i < depth; i++) {
807 ext4_free_blocks(handle, inode, ablocks[i], 1);
816 * routine implements tree growing procedure:
817 * - allocates new block
818 * - moves top-level data (index block or leaf) into the new block
819 * - initialize new top-level, creating index that points to the
822 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
823 struct ext4_ext_path *path,
824 struct ext4_extent *newext)
826 struct ext4_ext_path *curp = path;
827 struct ext4_extent_header *neh;
828 struct ext4_extent_idx *fidx;
829 struct buffer_head *bh;
830 ext4_fsblk_t newblock;
833 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
837 bh = sb_getblk(inode->i_sb, newblock);
840 ext4_std_error(inode->i_sb, err);
845 if ((err = ext4_journal_get_create_access(handle, bh))) {
850 /* move top-level index/leaf into new block */
851 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
853 /* set size of new block */
854 neh = ext_block_hdr(bh);
855 /* old root could have indexes or leaves
856 * so calculate e_max right way */
857 if (ext_depth(inode))
858 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
860 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
861 neh->eh_magic = EXT4_EXT_MAGIC;
862 set_buffer_uptodate(bh);
865 if ((err = ext4_journal_dirty_metadata(handle, bh)))
868 /* create index in new top-level index: num,max,pointer */
869 if ((err = ext4_ext_get_access(handle, inode, curp)))
872 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
873 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
874 curp->p_hdr->eh_entries = cpu_to_le16(1);
875 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
876 /* FIXME: it works, but actually path[0] can be index */
877 curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
878 ext4_idx_store_pblock(curp->p_idx, newblock);
880 neh = ext_inode_hdr(inode);
881 fidx = EXT_FIRST_INDEX(neh);
882 ext_debug("new root: num %d(%d), lblock %d, ptr "E3FSBLK"\n",
883 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
884 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
886 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
887 err = ext4_ext_dirty(handle, inode, curp);
895 * routine finds empty index and adds new leaf. if no free index found
896 * then it requests in-depth growing
898 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
899 struct ext4_ext_path *path,
900 struct ext4_extent *newext)
902 struct ext4_ext_path *curp;
903 int depth, i, err = 0;
906 i = depth = ext_depth(inode);
908 /* walk up to the tree and look for free index entry */
910 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
915 /* we use already allocated block for index block
916 * so, subsequent data blocks should be contigoues */
917 if (EXT_HAS_FREE_INDEX(curp)) {
918 /* if we found index with free entry, then use that
919 * entry: create all needed subtree and add new leaf */
920 err = ext4_ext_split(handle, inode, path, newext, i);
923 ext4_ext_drop_refs(path);
924 path = ext4_ext_find_extent(inode,
925 le32_to_cpu(newext->ee_block),
930 /* tree is full, time to grow in depth */
931 err = ext4_ext_grow_indepth(handle, inode, path, newext);
936 ext4_ext_drop_refs(path);
937 path = ext4_ext_find_extent(inode,
938 le32_to_cpu(newext->ee_block),
946 * only first (depth 0 -> 1) produces free space
947 * in all other cases we have to split growed tree
949 depth = ext_depth(inode);
950 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
951 /* now we need split */
961 * returns allocated block in subsequent extent or EXT_MAX_BLOCK
962 * NOTE: it consider block number from index entry as
963 * allocated block. thus, index entries have to be consistent
967 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
971 BUG_ON(path == NULL);
972 depth = path->p_depth;
974 if (depth == 0 && path->p_ext == NULL)
975 return EXT_MAX_BLOCK;
978 if (depth == path->p_depth) {
980 if (path[depth].p_ext !=
981 EXT_LAST_EXTENT(path[depth].p_hdr))
982 return le32_to_cpu(path[depth].p_ext[1].ee_block);
985 if (path[depth].p_idx !=
986 EXT_LAST_INDEX(path[depth].p_hdr))
987 return le32_to_cpu(path[depth].p_idx[1].ei_block);
992 return EXT_MAX_BLOCK;
996 * returns first allocated block from next leaf or EXT_MAX_BLOCK
998 static unsigned ext4_ext_next_leaf_block(struct inode *inode,
999 struct ext4_ext_path *path)
1003 BUG_ON(path == NULL);
1004 depth = path->p_depth;
1006 /* zero-tree has no leaf blocks at all */
1008 return EXT_MAX_BLOCK;
1010 /* go to index block */
1013 while (depth >= 0) {
1014 if (path[depth].p_idx !=
1015 EXT_LAST_INDEX(path[depth].p_hdr))
1016 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1020 return EXT_MAX_BLOCK;
1024 * if leaf gets modified and modified extent is first in the leaf
1025 * then we have to correct all indexes above
1026 * TODO: do we need to correct tree in all cases?
1028 int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1029 struct ext4_ext_path *path)
1031 struct ext4_extent_header *eh;
1032 int depth = ext_depth(inode);
1033 struct ext4_extent *ex;
1037 eh = path[depth].p_hdr;
1038 ex = path[depth].p_ext;
1043 /* there is no tree at all */
1047 if (ex != EXT_FIRST_EXTENT(eh)) {
1048 /* we correct tree if first leaf got modified only */
1053 * TODO: we need correction if border is smaller then current one
1056 border = path[depth].p_ext->ee_block;
1057 if ((err = ext4_ext_get_access(handle, inode, path + k)))
1059 path[k].p_idx->ei_block = border;
1060 if ((err = ext4_ext_dirty(handle, inode, path + k)))
1064 /* change all left-side indexes */
1065 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1067 if ((err = ext4_ext_get_access(handle, inode, path + k)))
1069 path[k].p_idx->ei_block = border;
1070 if ((err = ext4_ext_dirty(handle, inode, path + k)))
1078 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1079 struct ext4_extent *ex2)
1081 if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len)
1082 != le32_to_cpu(ex2->ee_block))
1085 #ifdef AGRESSIVE_TEST
1086 if (le16_to_cpu(ex1->ee_len) >= 4)
1090 if (ext_pblock(ex1) + le16_to_cpu(ex1->ee_len) == ext_pblock(ex2))
1096 * this routine tries to merge requsted extent into the existing
1097 * extent or inserts requested extent as new one into the tree,
1098 * creating new leaf in no-space case
1100 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1101 struct ext4_ext_path *path,
1102 struct ext4_extent *newext)
1104 struct ext4_extent_header * eh;
1105 struct ext4_extent *ex, *fex;
1106 struct ext4_extent *nearex; /* nearest extent */
1107 struct ext4_ext_path *npath = NULL;
1108 int depth, len, err, next;
1110 BUG_ON(newext->ee_len == 0);
1111 depth = ext_depth(inode);
1112 ex = path[depth].p_ext;
1113 BUG_ON(path[depth].p_hdr == NULL);
1115 /* try to insert block into found extent and return */
1116 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
1117 ext_debug("append %d block to %d:%d (from "E3FSBLK")\n",
1118 le16_to_cpu(newext->ee_len),
1119 le32_to_cpu(ex->ee_block),
1120 le16_to_cpu(ex->ee_len), ext_pblock(ex));
1121 if ((err = ext4_ext_get_access(handle, inode, path + depth)))
1123 ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
1124 + le16_to_cpu(newext->ee_len));
1125 eh = path[depth].p_hdr;
1131 depth = ext_depth(inode);
1132 eh = path[depth].p_hdr;
1133 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1136 /* probably next leaf has space for us? */
1137 fex = EXT_LAST_EXTENT(eh);
1138 next = ext4_ext_next_leaf_block(inode, path);
1139 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1140 && next != EXT_MAX_BLOCK) {
1141 ext_debug("next leaf block - %d\n", next);
1142 BUG_ON(npath != NULL);
1143 npath = ext4_ext_find_extent(inode, next, NULL);
1145 return PTR_ERR(npath);
1146 BUG_ON(npath->p_depth != path->p_depth);
1147 eh = npath[depth].p_hdr;
1148 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1149 ext_debug("next leaf isnt full(%d)\n",
1150 le16_to_cpu(eh->eh_entries));
1154 ext_debug("next leaf has no free space(%d,%d)\n",
1155 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1159 * there is no free space in found leaf
1160 * we're gonna add new leaf in the tree
1162 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1165 depth = ext_depth(inode);
1166 eh = path[depth].p_hdr;
1169 nearex = path[depth].p_ext;
1171 if ((err = ext4_ext_get_access(handle, inode, path + depth)))
1175 /* there is no extent in this leaf, create first one */
1176 ext_debug("first extent in the leaf: %d:"E3FSBLK":%d\n",
1177 le32_to_cpu(newext->ee_block),
1179 le16_to_cpu(newext->ee_len));
1180 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1181 } else if (le32_to_cpu(newext->ee_block)
1182 > le32_to_cpu(nearex->ee_block)) {
1183 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1184 if (nearex != EXT_LAST_EXTENT(eh)) {
1185 len = EXT_MAX_EXTENT(eh) - nearex;
1186 len = (len - 1) * sizeof(struct ext4_extent);
1187 len = len < 0 ? 0 : len;
1188 ext_debug("insert %d:"E3FSBLK":%d after: nearest 0x%p, "
1189 "move %d from 0x%p to 0x%p\n",
1190 le32_to_cpu(newext->ee_block),
1192 le16_to_cpu(newext->ee_len),
1193 nearex, len, nearex + 1, nearex + 2);
1194 memmove(nearex + 2, nearex + 1, len);
1196 path[depth].p_ext = nearex + 1;
1198 BUG_ON(newext->ee_block == nearex->ee_block);
1199 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1200 len = len < 0 ? 0 : len;
1201 ext_debug("insert %d:"E3FSBLK":%d before: nearest 0x%p, "
1202 "move %d from 0x%p to 0x%p\n",
1203 le32_to_cpu(newext->ee_block),
1205 le16_to_cpu(newext->ee_len),
1206 nearex, len, nearex + 1, nearex + 2);
1207 memmove(nearex + 1, nearex, len);
1208 path[depth].p_ext = nearex;
1211 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1212 nearex = path[depth].p_ext;
1213 nearex->ee_block = newext->ee_block;
1214 nearex->ee_start = newext->ee_start;
1215 nearex->ee_start_hi = newext->ee_start_hi;
1216 nearex->ee_len = newext->ee_len;
1219 /* try to merge extents to the right */
1220 while (nearex < EXT_LAST_EXTENT(eh)) {
1221 if (!ext4_can_extents_be_merged(inode, nearex, nearex + 1))
1223 /* merge with next extent! */
1224 nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
1225 + le16_to_cpu(nearex[1].ee_len));
1226 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1227 len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1228 * sizeof(struct ext4_extent);
1229 memmove(nearex + 1, nearex + 2, len);
1231 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1232 BUG_ON(eh->eh_entries == 0);
1235 /* try to merge extents to the left */
1237 /* time to correct all indexes above */
1238 err = ext4_ext_correct_indexes(handle, inode, path);
1242 err = ext4_ext_dirty(handle, inode, path + depth);
1246 ext4_ext_drop_refs(npath);
1249 ext4_ext_tree_changed(inode);
1250 ext4_ext_invalidate_cache(inode);
1254 int ext4_ext_walk_space(struct inode *inode, unsigned long block,
1255 unsigned long num, ext_prepare_callback func,
1258 struct ext4_ext_path *path = NULL;
1259 struct ext4_ext_cache cbex;
1260 struct ext4_extent *ex;
1261 unsigned long next, start = 0, end = 0;
1262 unsigned long last = block + num;
1263 int depth, exists, err = 0;
1265 BUG_ON(func == NULL);
1266 BUG_ON(inode == NULL);
1268 while (block < last && block != EXT_MAX_BLOCK) {
1270 /* find extent for this block */
1271 path = ext4_ext_find_extent(inode, block, path);
1273 err = PTR_ERR(path);
1278 depth = ext_depth(inode);
1279 BUG_ON(path[depth].p_hdr == NULL);
1280 ex = path[depth].p_ext;
1281 next = ext4_ext_next_allocated_block(path);
1285 /* there is no extent yet, so try to allocate
1286 * all requested space */
1289 } else if (le32_to_cpu(ex->ee_block) > block) {
1290 /* need to allocate space before found extent */
1292 end = le32_to_cpu(ex->ee_block);
1293 if (block + num < end)
1296 le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
1297 /* need to allocate space after found extent */
1302 } else if (block >= le32_to_cpu(ex->ee_block)) {
1304 * some part of requested space is covered
1308 end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
1309 if (block + num < end)
1315 BUG_ON(end <= start);
1318 cbex.ec_block = start;
1319 cbex.ec_len = end - start;
1321 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1323 cbex.ec_block = le32_to_cpu(ex->ee_block);
1324 cbex.ec_len = le16_to_cpu(ex->ee_len);
1325 cbex.ec_start = ext_pblock(ex);
1326 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1329 BUG_ON(cbex.ec_len == 0);
1330 err = func(inode, path, &cbex, cbdata);
1331 ext4_ext_drop_refs(path);
1335 if (err == EXT_REPEAT)
1337 else if (err == EXT_BREAK) {
1342 if (ext_depth(inode) != depth) {
1343 /* depth was changed. we have to realloc path */
1348 block = cbex.ec_block + cbex.ec_len;
1352 ext4_ext_drop_refs(path);
1360 ext4_ext_put_in_cache(struct inode *inode, __u32 block,
1361 __u32 len, __u32 start, int type)
1363 struct ext4_ext_cache *cex;
1365 cex = &EXT4_I(inode)->i_cached_extent;
1366 cex->ec_type = type;
1367 cex->ec_block = block;
1369 cex->ec_start = start;
1373 * this routine calculate boundaries of the gap requested block fits into
1374 * and cache this gap
1377 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1378 unsigned long block)
1380 int depth = ext_depth(inode);
1381 unsigned long lblock, len;
1382 struct ext4_extent *ex;
1384 ex = path[depth].p_ext;
1386 /* there is no extent yet, so gap is [0;-] */
1388 len = EXT_MAX_BLOCK;
1389 ext_debug("cache gap(whole file):");
1390 } else if (block < le32_to_cpu(ex->ee_block)) {
1392 len = le32_to_cpu(ex->ee_block) - block;
1393 ext_debug("cache gap(before): %lu [%lu:%lu]",
1394 (unsigned long) block,
1395 (unsigned long) le32_to_cpu(ex->ee_block),
1396 (unsigned long) le16_to_cpu(ex->ee_len));
1397 } else if (block >= le32_to_cpu(ex->ee_block)
1398 + le16_to_cpu(ex->ee_len)) {
1399 lblock = le32_to_cpu(ex->ee_block)
1400 + le16_to_cpu(ex->ee_len);
1401 len = ext4_ext_next_allocated_block(path);
1402 ext_debug("cache gap(after): [%lu:%lu] %lu",
1403 (unsigned long) le32_to_cpu(ex->ee_block),
1404 (unsigned long) le16_to_cpu(ex->ee_len),
1405 (unsigned long) block);
1406 BUG_ON(len == lblock);
1413 ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
1414 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1418 ext4_ext_in_cache(struct inode *inode, unsigned long block,
1419 struct ext4_extent *ex)
1421 struct ext4_ext_cache *cex;
1423 cex = &EXT4_I(inode)->i_cached_extent;
1425 /* has cache valid data? */
1426 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1427 return EXT4_EXT_CACHE_NO;
1429 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1430 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1431 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1432 ex->ee_block = cpu_to_le32(cex->ec_block);
1433 ext4_ext_store_pblock(ex, cex->ec_start);
1434 ex->ee_len = cpu_to_le16(cex->ec_len);
1435 ext_debug("%lu cached by %lu:%lu:"E3FSBLK"\n",
1436 (unsigned long) block,
1437 (unsigned long) cex->ec_block,
1438 (unsigned long) cex->ec_len,
1440 return cex->ec_type;
1444 return EXT4_EXT_CACHE_NO;
1448 * routine removes index from the index block
1449 * it's used in truncate case only. thus all requests are for
1450 * last index in the block only
1452 int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1453 struct ext4_ext_path *path)
1455 struct buffer_head *bh;
1459 /* free index block */
1461 leaf = idx_pblock(path->p_idx);
1462 BUG_ON(path->p_hdr->eh_entries == 0);
1463 if ((err = ext4_ext_get_access(handle, inode, path)))
1465 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
1466 if ((err = ext4_ext_dirty(handle, inode, path)))
1468 ext_debug("index is empty, remove it, free block "E3FSBLK"\n", leaf);
1469 bh = sb_find_get_block(inode->i_sb, leaf);
1470 ext4_forget(handle, 1, inode, bh, leaf);
1471 ext4_free_blocks(handle, inode, leaf, 1);
1476 * This routine returns max. credits extent tree can consume.
1477 * It should be OK for low-performance paths like ->writepage()
1478 * To allow many writing process to fit a single transaction,
1479 * caller should calculate credits under truncate_mutex and
1482 int inline ext4_ext_calc_credits_for_insert(struct inode *inode,
1483 struct ext4_ext_path *path)
1488 /* probably there is space in leaf? */
1489 depth = ext_depth(inode);
1490 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1491 < le16_to_cpu(path[depth].p_hdr->eh_max))
1496 * given 32bit logical block (4294967296 blocks), max. tree
1497 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1498 * let's also add one more level for imbalance.
1502 /* allocation of new data block(s) */
1506 * tree can be full, so it'd need to grow in depth:
1507 * allocation + old root + new root
1509 needed += 2 + 1 + 1;
1512 * Index split can happen, we'd need:
1513 * allocate intermediate indexes (bitmap + group)
1514 * + change two blocks at each level, but root (already included)
1516 needed = (depth * 2) + (depth * 2);
1518 /* any allocation modifies superblock */
1524 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1525 struct ext4_extent *ex,
1526 unsigned long from, unsigned long to)
1528 struct buffer_head *bh;
1531 #ifdef EXTENTS_STATS
1533 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1534 unsigned short ee_len = le16_to_cpu(ex->ee_len);
1535 spin_lock(&sbi->s_ext_stats_lock);
1536 sbi->s_ext_blocks += ee_len;
1537 sbi->s_ext_extents++;
1538 if (ee_len < sbi->s_ext_min)
1539 sbi->s_ext_min = ee_len;
1540 if (ee_len > sbi->s_ext_max)
1541 sbi->s_ext_max = ee_len;
1542 if (ext_depth(inode) > sbi->s_depth_max)
1543 sbi->s_depth_max = ext_depth(inode);
1544 spin_unlock(&sbi->s_ext_stats_lock);
1547 if (from >= le32_to_cpu(ex->ee_block)
1548 && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1552 num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
1553 start = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - num;
1554 ext_debug("free last %lu blocks starting "E3FSBLK"\n", num, start);
1555 for (i = 0; i < num; i++) {
1556 bh = sb_find_get_block(inode->i_sb, start + i);
1557 ext4_forget(handle, 0, inode, bh, start + i);
1559 ext4_free_blocks(handle, inode, start, num);
1560 } else if (from == le32_to_cpu(ex->ee_block)
1561 && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1562 printk("strange request: removal %lu-%lu from %u:%u\n",
1563 from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1565 printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1566 from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1572 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1573 struct ext4_ext_path *path, unsigned long start)
1575 int err = 0, correct_index = 0;
1576 int depth = ext_depth(inode), credits;
1577 struct ext4_extent_header *eh;
1578 unsigned a, b, block, num;
1579 unsigned long ex_ee_block;
1580 unsigned short ex_ee_len;
1581 struct ext4_extent *ex;
1583 ext_debug("truncate since %lu in leaf\n", start);
1584 if (!path[depth].p_hdr)
1585 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1586 eh = path[depth].p_hdr;
1588 BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
1589 BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
1591 /* find where to start removing */
1592 ex = EXT_LAST_EXTENT(eh);
1594 ex_ee_block = le32_to_cpu(ex->ee_block);
1595 ex_ee_len = le16_to_cpu(ex->ee_len);
1597 while (ex >= EXT_FIRST_EXTENT(eh) &&
1598 ex_ee_block + ex_ee_len > start) {
1599 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1600 path[depth].p_ext = ex;
1602 a = ex_ee_block > start ? ex_ee_block : start;
1603 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1604 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1606 ext_debug(" border %u:%u\n", a, b);
1608 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1612 } else if (a != ex_ee_block) {
1613 /* remove tail of the extent */
1614 block = ex_ee_block;
1616 } else if (b != ex_ee_block + ex_ee_len - 1) {
1617 /* remove head of the extent */
1620 /* there is no "make a hole" API yet */
1623 /* remove whole extent: excellent! */
1624 block = ex_ee_block;
1626 BUG_ON(a != ex_ee_block);
1627 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1630 /* at present, extent can't cross block group */
1631 /* leaf + bitmap + group desc + sb + inode */
1633 if (ex == EXT_FIRST_EXTENT(eh)) {
1635 credits += (ext_depth(inode)) + 1;
1638 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1641 handle = ext4_ext_journal_restart(handle, credits);
1642 if (IS_ERR(handle)) {
1643 err = PTR_ERR(handle);
1647 err = ext4_ext_get_access(handle, inode, path + depth);
1651 err = ext4_remove_blocks(handle, inode, ex, a, b);
1656 /* this extent is removed entirely mark slot unused */
1657 ext4_ext_store_pblock(ex, 0);
1658 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1661 ex->ee_block = cpu_to_le32(block);
1662 ex->ee_len = cpu_to_le16(num);
1664 err = ext4_ext_dirty(handle, inode, path + depth);
1668 ext_debug("new extent: %u:%u:"E3FSBLK"\n", block, num,
1671 ex_ee_block = le32_to_cpu(ex->ee_block);
1672 ex_ee_len = le16_to_cpu(ex->ee_len);
1675 if (correct_index && eh->eh_entries)
1676 err = ext4_ext_correct_indexes(handle, inode, path);
1678 /* if this leaf is free, then we should
1679 * remove it from index block above */
1680 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1681 err = ext4_ext_rm_idx(handle, inode, path + depth);
1688 * returns 1 if current index have to be freed (even partial)
1691 ext4_ext_more_to_rm(struct ext4_ext_path *path)
1693 BUG_ON(path->p_idx == NULL);
1695 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1699 * if truncate on deeper level happened it it wasn't partial
1700 * so we have to consider current index for truncation
1702 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1707 int ext4_ext_remove_space(struct inode *inode, unsigned long start)
1709 struct super_block *sb = inode->i_sb;
1710 int depth = ext_depth(inode);
1711 struct ext4_ext_path *path;
1715 ext_debug("truncate since %lu\n", start);
1717 /* probably first extent we're gonna free will be last in block */
1718 handle = ext4_journal_start(inode, depth + 1);
1720 return PTR_ERR(handle);
1722 ext4_ext_invalidate_cache(inode);
1725 * we start scanning from right side freeing all the blocks
1726 * after i_size and walking into the deep
1728 path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
1730 ext4_journal_stop(handle);
1733 memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
1734 path[0].p_hdr = ext_inode_hdr(inode);
1735 if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
1739 path[0].p_depth = depth;
1741 while (i >= 0 && err == 0) {
1743 /* this is leaf block */
1744 err = ext4_ext_rm_leaf(handle, inode, path, start);
1745 /* root level have p_bh == NULL, brelse() eats this */
1746 brelse(path[i].p_bh);
1747 path[i].p_bh = NULL;
1752 /* this is index block */
1753 if (!path[i].p_hdr) {
1754 ext_debug("initialize header\n");
1755 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
1756 if (ext4_ext_check_header(__FUNCTION__, inode,
1763 BUG_ON(le16_to_cpu(path[i].p_hdr->eh_entries)
1764 > le16_to_cpu(path[i].p_hdr->eh_max));
1765 BUG_ON(path[i].p_hdr->eh_magic != EXT4_EXT_MAGIC);
1767 if (!path[i].p_idx) {
1768 /* this level hasn't touched yet */
1769 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1770 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
1771 ext_debug("init index ptr: hdr 0x%p, num %d\n",
1773 le16_to_cpu(path[i].p_hdr->eh_entries));
1775 /* we've already was here, see at next index */
1779 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
1780 i, EXT_FIRST_INDEX(path[i].p_hdr),
1782 if (ext4_ext_more_to_rm(path + i)) {
1783 /* go to the next level */
1784 ext_debug("move to level %d (block "E3FSBLK")\n",
1785 i + 1, idx_pblock(path[i].p_idx));
1786 memset(path + i + 1, 0, sizeof(*path));
1788 sb_bread(sb, idx_pblock(path[i].p_idx));
1789 if (!path[i+1].p_bh) {
1790 /* should we reset i_size? */
1795 /* put actual number of indexes to know is this
1796 * number got changed at the next iteration */
1797 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
1800 /* we finish processing this index, go up */
1801 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1802 /* index is empty, remove it
1803 * handle must be already prepared by the
1804 * truncatei_leaf() */
1805 err = ext4_ext_rm_idx(handle, inode, path + i);
1807 /* root level have p_bh == NULL, brelse() eats this */
1808 brelse(path[i].p_bh);
1809 path[i].p_bh = NULL;
1811 ext_debug("return to level %d\n", i);
1815 /* TODO: flexible tree reduction should be here */
1816 if (path->p_hdr->eh_entries == 0) {
1818 * truncate to zero freed all the tree
1819 * so, we need to correct eh_depth
1821 err = ext4_ext_get_access(handle, inode, path);
1823 ext_inode_hdr(inode)->eh_depth = 0;
1824 ext_inode_hdr(inode)->eh_max =
1825 cpu_to_le16(ext4_ext_space_root(inode));
1826 err = ext4_ext_dirty(handle, inode, path);
1830 ext4_ext_tree_changed(inode);
1831 ext4_ext_drop_refs(path);
1833 ext4_journal_stop(handle);
1839 * called at mount time
1841 void ext4_ext_init(struct super_block *sb)
1844 * possible initialization would be here
1847 if (test_opt(sb, EXTENTS)) {
1848 printk("EXT4-fs: file extents enabled");
1849 #ifdef AGRESSIVE_TEST
1850 printk(", agressive tests");
1852 #ifdef CHECK_BINSEARCH
1853 printk(", check binsearch");
1855 #ifdef EXTENTS_STATS
1859 #ifdef EXTENTS_STATS
1860 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
1861 EXT4_SB(sb)->s_ext_min = 1 << 30;
1862 EXT4_SB(sb)->s_ext_max = 0;
1868 * called at umount time
1870 void ext4_ext_release(struct super_block *sb)
1872 if (!test_opt(sb, EXTENTS))
1875 #ifdef EXTENTS_STATS
1876 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
1877 struct ext4_sb_info *sbi = EXT4_SB(sb);
1878 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
1879 sbi->s_ext_blocks, sbi->s_ext_extents,
1880 sbi->s_ext_blocks / sbi->s_ext_extents);
1881 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
1882 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
1887 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
1888 ext4_fsblk_t iblock,
1889 unsigned long max_blocks, struct buffer_head *bh_result,
1890 int create, int extend_disksize)
1892 struct ext4_ext_path *path = NULL;
1893 struct ext4_extent newex, *ex;
1894 ext4_fsblk_t goal, newblock;
1896 unsigned long allocated = 0;
1898 __clear_bit(BH_New, &bh_result->b_state);
1899 ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
1900 max_blocks, (unsigned) inode->i_ino);
1901 mutex_lock(&EXT4_I(inode)->truncate_mutex);
1903 /* check in cache */
1904 if ((goal = ext4_ext_in_cache(inode, iblock, &newex))) {
1905 if (goal == EXT4_EXT_CACHE_GAP) {
1907 /* block isn't allocated yet and
1908 * user don't want to allocate it */
1911 /* we should allocate requested block */
1912 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
1913 /* block is already allocated */
1915 - le32_to_cpu(newex.ee_block)
1916 + ext_pblock(&newex);
1917 /* number of remain blocks in the extent */
1918 allocated = le16_to_cpu(newex.ee_len) -
1919 (iblock - le32_to_cpu(newex.ee_block));
1926 /* find extent for this block */
1927 path = ext4_ext_find_extent(inode, iblock, NULL);
1929 err = PTR_ERR(path);
1934 depth = ext_depth(inode);
1937 * consistent leaf must not be empty
1938 * this situations is possible, though, _during_ tree modification
1939 * this is why assert can't be put in ext4_ext_find_extent()
1941 BUG_ON(path[depth].p_ext == NULL && depth != 0);
1943 if ((ex = path[depth].p_ext)) {
1944 unsigned long ee_block = le32_to_cpu(ex->ee_block);
1945 ext4_fsblk_t ee_start = ext_pblock(ex);
1946 unsigned short ee_len = le16_to_cpu(ex->ee_len);
1947 /* if found exent covers block, simple return it */
1948 if (iblock >= ee_block && iblock < ee_block + ee_len) {
1949 newblock = iblock - ee_block + ee_start;
1950 /* number of remain blocks in the extent */
1951 allocated = ee_len - (iblock - ee_block);
1952 ext_debug("%d fit into %lu:%d -> "E3FSBLK"\n", (int) iblock,
1953 ee_block, ee_len, newblock);
1954 ext4_ext_put_in_cache(inode, ee_block, ee_len,
1955 ee_start, EXT4_EXT_CACHE_EXTENT);
1961 * requested block isn't allocated yet
1962 * we couldn't try to create block if create flag is zero
1965 /* put just found gap into cache to speedup subsequest reqs */
1966 ext4_ext_put_gap_in_cache(inode, path, iblock);
1970 * Okay, we need to do block allocation. Lazily initialize the block
1971 * allocation info here if necessary
1973 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
1974 ext4_init_block_alloc_info(inode);
1976 /* allocate new block */
1977 goal = ext4_ext_find_goal(inode, path, iblock);
1978 allocated = max_blocks;
1979 newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
1982 ext_debug("allocate new block: goal "E3FSBLK", found "E3FSBLK"/%lu\n",
1983 goal, newblock, allocated);
1985 /* try to insert new extent into found leaf and return */
1986 newex.ee_block = cpu_to_le32(iblock);
1987 ext4_ext_store_pblock(&newex, newblock);
1988 newex.ee_len = cpu_to_le16(allocated);
1989 err = ext4_ext_insert_extent(handle, inode, path, &newex);
1993 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
1994 EXT4_I(inode)->i_disksize = inode->i_size;
1996 /* previous routine could use block we allocated */
1997 newblock = ext_pblock(&newex);
1998 __set_bit(BH_New, &bh_result->b_state);
2000 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2001 EXT4_EXT_CACHE_EXTENT);
2003 if (allocated > max_blocks)
2004 allocated = max_blocks;
2005 ext4_ext_show_leaf(inode, path);
2006 __set_bit(BH_Mapped, &bh_result->b_state);
2007 bh_result->b_bdev = inode->i_sb->s_bdev;
2008 bh_result->b_blocknr = newblock;
2011 ext4_ext_drop_refs(path);
2014 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2016 return err ? err : allocated;
2019 void ext4_ext_truncate(struct inode * inode, struct page *page)
2021 struct address_space *mapping = inode->i_mapping;
2022 struct super_block *sb = inode->i_sb;
2023 unsigned long last_block;
2028 * probably first extent we're gonna free will be last in block
2030 err = ext4_writepage_trans_blocks(inode) + 3;
2031 handle = ext4_journal_start(inode, err);
2032 if (IS_ERR(handle)) {
2034 clear_highpage(page);
2035 flush_dcache_page(page);
2037 page_cache_release(page);
2043 ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2045 mutex_lock(&EXT4_I(inode)->truncate_mutex);
2046 ext4_ext_invalidate_cache(inode);
2049 * TODO: optimization is possible here
2050 * probably we need not scaning at all,
2051 * because page truncation is enough
2053 if (ext4_orphan_add(handle, inode))
2056 /* we have to know where to truncate from in crash case */
2057 EXT4_I(inode)->i_disksize = inode->i_size;
2058 ext4_mark_inode_dirty(handle, inode);
2060 last_block = (inode->i_size + sb->s_blocksize - 1)
2061 >> EXT4_BLOCK_SIZE_BITS(sb);
2062 err = ext4_ext_remove_space(inode, last_block);
2064 /* In a multi-transaction truncate, we only make the final
2065 * transaction synchronous */
2071 * If this was a simple ftruncate(), and the file will remain alive
2072 * then we need to clear up the orphan record which we created above.
2073 * However, if this was a real unlink then we were called by
2074 * ext4_delete_inode(), and we allow that function to clean up the
2075 * orphan info for us.
2078 ext4_orphan_del(handle, inode);
2080 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2081 ext4_journal_stop(handle);
2085 * this routine calculate max number of blocks we could modify
2086 * in order to allocate new block for an inode
2088 int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2092 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2094 /* caller want to allocate num blocks, but note it includes sb */
2095 needed = needed * num - (num - 1);
2098 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2104 EXPORT_SYMBOL(ext4_mark_inode_dirty);
2105 EXPORT_SYMBOL(ext4_ext_invalidate_cache);
2106 EXPORT_SYMBOL(ext4_ext_insert_extent);
2107 EXPORT_SYMBOL(ext4_ext_walk_space);
2108 EXPORT_SYMBOL(ext4_ext_find_goal);
2109 EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert);