1 Index: linux-2.4.24/fs/ext3/extents.c
2 ===================================================================
3 --- linux-2.4.24.orig/fs/ext3/extents.c 2003-01-30 13:24:37.000000000 +0300
4 +++ linux-2.4.24/fs/ext3/extents.c 2004-01-29 16:18:31.000000000 +0300
7 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
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 EXT3
27 + * - ext3_ext_walk_space() sould not use ext3_ext_find_extent()
28 + * - ext3_ext_calc_credits() could take 'mergable' into account
29 + * - ext3*_error() should be used in some situations
30 + * - find_goal() [to be tested and improved]
31 + * - smart tree reduction
32 + * - arch-independence
33 + * common on-disk format for big/little-endian arch
36 +#include <linux/module.h>
37 +#include <linux/fs.h>
38 +#include <linux/time.h>
39 +#include <linux/ext3_jbd.h>
40 +#include <linux/jbd.h>
41 +#include <linux/smp_lock.h>
42 +#include <linux/highuid.h>
43 +#include <linux/pagemap.h>
44 +#include <linux/quotaops.h>
45 +#include <linux/string.h>
46 +#include <linux/slab.h>
47 +#include <linux/locks.h>
48 +#include <linux/ext3_extents.h>
49 +#include <asm/uaccess.h>
51 +static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
55 + if (handle->h_buffer_credits > needed)
57 + if (!ext3_journal_extend(handle, needed))
59 + err = ext3_journal_restart(handle, needed);
65 +ext3_ext_get_access_for_root(handle_t *h, struct ext3_extents_tree *tree)
67 + if (tree->get_write_access)
68 + return tree->get_write_access(h,tree->buffer);
74 +ext3_ext_mark_root_dirty(handle_t *h, struct ext3_extents_tree *tree)
76 + if (tree->mark_buffer_dirty)
77 + return tree->mark_buffer_dirty(h,tree->buffer);
87 +static int ext3_ext_get_access(handle_t *handle,
88 + struct ext3_extents_tree *tree,
89 + struct ext3_ext_path *path)
94 + /* path points to block */
95 + err = ext3_journal_get_write_access(handle, path->p_bh);
97 + /* path points to leaf/index in inode body */
98 + err = ext3_ext_get_access_for_root(handle, tree);
101 + printk("err=%d (%s:%d)\n", err, __FILE__, __LINE__);
111 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
112 + struct ext3_ext_path *path)
116 + /* path points to block */
117 + err =ext3_journal_dirty_metadata(handle, path->p_bh);
119 + /* path points to leaf/index in inode body */
120 + err = ext3_ext_mark_root_dirty(handle, tree);
123 + printk("err=%d (%s:%d)\n", err, __FILE__, __LINE__);
128 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
129 + struct ext3_ext_path *path, struct ext3_extent *ex,
132 + int goal, depth, newblock;
133 + struct inode *inode;
136 + if (tree->new_block)
137 + return tree->new_block(handle, tree, path, ex, err);
139 + inode = tree->inode;
140 + depth = EXT_DEPTH(tree);
141 + if (path && depth > 0) {
142 + goal = path[depth-1].p_block;
144 + struct ext3_inode_info *ei = EXT3_I(inode);
145 + unsigned long bg_start;
146 + unsigned long colour;
148 + bg_start = (ei->i_block_group *
149 + EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
150 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
151 + colour = (current->pid % 16) *
152 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
153 + goal = bg_start + colour;
156 + newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
160 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
162 + struct ext3_extent_header *neh;
163 + neh = EXT_ROOT_HDR(tree);
164 + neh->e_generation++;
167 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
171 + size = (tree->inode->i_sb->s_blocksize -
172 + sizeof(struct ext3_extent_header))
173 + / sizeof(struct ext3_extent);
174 +#ifdef AGRESSIVE_TEST
180 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
184 + size = (tree->inode->i_sb->s_blocksize -
185 + sizeof(struct ext3_extent_header))
186 + / sizeof(struct ext3_extent_idx);
187 +#ifdef AGRESSIVE_TEST
193 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
197 + size = (tree->buffer_len - sizeof(struct ext3_extent_header))
198 + / sizeof(struct ext3_extent);
199 +#ifdef AGRESSIVE_TEST
205 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
209 + size = (tree->buffer_len -
210 + sizeof(struct ext3_extent_header))
211 + / sizeof(struct ext3_extent_idx);
212 +#ifdef AGRESSIVE_TEST
218 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
219 + struct ext3_ext_path *path)
222 + int k, l = path->p_depth;
224 + ext_debug(tree, "path:");
225 + for (k = 0; k <= l; k++, path++) {
227 + ext_debug(tree, " %d->%d", path->p_idx->e_block,
228 + path->p_idx->e_leaf);
229 + } else if (path->p_ext) {
230 + ext_debug(tree, " %d:%d:%d",
231 + path->p_ext->e_block,
232 + path->p_ext->e_num,
233 + path->p_ext->e_start);
235 + ext_debug(tree, " []");
237 + ext_debug(tree, "\n");
241 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
242 + struct ext3_ext_path *path)
245 + int depth = EXT_DEPTH(tree);
246 + struct ext3_extent_header *eh;
247 + struct ext3_extent *ex;
253 + eh = path[depth].p_hdr;
254 + ex = EXT_FIRST_EXTENT(eh);
256 + for (i = 0; i < eh->e_num; i++, ex++) {
257 + ext_debug(tree, "%d:%d:%d ",
258 + ex->e_block, ex->e_num, ex->e_start);
260 + ext_debug(tree, "\n");
264 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
266 + int depth = path->p_depth;
269 + for (i = 0; i <= depth; i++, path++)
271 + brelse(path->p_bh);
277 + * binary search for closest index by given block
280 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
281 + struct ext3_ext_path *path, int block)
283 + struct ext3_extent_header *eh = path->p_hdr;
284 + struct ext3_extent_idx *ix;
287 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
288 + EXT_ASSERT(eh->e_num <= eh->e_max);
289 + EXT_ASSERT(eh->e_num > 0);
291 + ext_debug(tree, "binsearch for %d(idx): ", block);
293 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
298 + if (block < ix[l + k].e_block)
302 + ext_debug(tree, "%d:%d:%d ", k, l, r);
307 + ext_debug(tree, " -> %d->%d ", path->p_idx->e_block, path->p_idx->e_leaf);
310 + if (block < ix->e_block)
312 + path->p_idx = ix++;
314 + ext_debug(tree, " -> %d->%d\n", path->p_idx->e_block,
315 + path->p_idx->e_leaf);
317 +#ifdef CHECK_BINSEARCH
319 + struct ext3_extent_idx *chix;
321 + chix = ix = EXT_FIRST_INDEX(eh);
322 + for (k = 0; k < eh->e_num; k++, ix++) {
323 + if (k != 0 && ix->e_block <= ix[-1].e_block) {
324 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
325 + ix, EXT_FIRST_INDEX(eh));
326 + printk("%u <= %u\n",
327 + ix->e_block,ix[-1].e_block);
329 + EXT_ASSERT(k == 0 || ix->e_block > ix[-1].e_block);
330 + if (block < ix->e_block)
334 + EXT_ASSERT(chix == path->p_idx);
341 + * binary search for closest extent by given block
344 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
345 + struct ext3_ext_path *path, int block)
347 + struct ext3_extent_header *eh = path->p_hdr;
348 + struct ext3_extent *ex;
351 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
352 + EXT_ASSERT(eh->e_num <= eh->e_max);
354 + if (eh->e_num == 0) {
356 + * this leaf is empty yet:
357 + * we get such a leaf in split/add case
362 + ext_debug(tree, "binsearch for %d: ", block);
364 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
369 + if (block < ex[l + k].e_block)
373 + ext_debug(tree, "%d:%d:%d ", k, l, r);
378 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->e_block,
379 + path->p_ext->e_start, path->p_ext->e_num);
382 + if (block < ex->e_block)
384 + path->p_ext = ex++;
386 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->e_block,
387 + path->p_ext->e_start, path->p_ext->e_num);
389 +#ifdef CHECK_BINSEARCH
391 + struct ext3_extent *chex;
393 + chex = ex = EXT_FIRST_EXTENT(eh);
394 + for (k = 0; k < eh->e_num; k++, ex++) {
395 + EXT_ASSERT(k == 0 || ex->e_block > ex[-1].e_block);
396 + if (block < ex->e_block)
400 + EXT_ASSERT(chex == path->p_ext);
406 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
408 + struct ext3_extent_header *eh;
410 + BUG_ON(tree->buffer_len == 0);
411 + ext3_ext_get_access_for_root(handle, tree);
412 + eh = EXT_ROOT_HDR(tree);
415 + eh->e_magic = EXT3_EXT_MAGIC;
416 + eh->e_max = ext3_ext_space_root(tree);
417 + ext3_ext_mark_root_dirty(handle, tree);
421 +struct ext3_ext_path *
422 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
423 + struct ext3_ext_path *path)
425 + struct ext3_extent_header *eh;
426 + struct buffer_head *bh;
427 + int depth, i, ppos = 0;
430 + EXT_ASSERT(tree->inode);
431 + EXT_ASSERT(tree->root);
433 + eh = EXT_ROOT_HDR(tree);
434 + i = depth = EXT_DEPTH(tree);
435 + EXT_ASSERT(eh->e_max);
436 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
437 + EXT_ASSERT(i == 0 || eh->e_num > 0);
439 + /* account possible depth increase */
441 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
444 + return ERR_PTR(-ENOMEM);
446 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
447 + path[0].p_hdr = eh;
449 + /* walk through the tree */
451 + ext_debug(tree, "depth %d: num %d, max %d\n",
452 + ppos, eh->e_num, eh->e_max);
453 + ext3_ext_binsearch_idx(tree, path + ppos, block);
454 + path[ppos].p_block = path[ppos].p_idx->e_leaf;
455 + path[ppos].p_depth = i;
456 + path[ppos].p_ext = NULL;
458 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
460 + ext3_ext_drop_refs(path);
462 + return ERR_PTR(-EIO);
464 + eh = EXT_BLOCK_HDR(bh);
466 + EXT_ASSERT(ppos <= depth);
467 + path[ppos].p_bh = bh;
468 + path[ppos].p_hdr = eh;
472 + path[ppos].p_depth = i;
473 + path[ppos].p_hdr = eh;
474 + path[ppos].p_ext = NULL;
477 + ext3_ext_binsearch(tree, path + ppos, block);
479 + ext3_ext_show_path(tree, path);
485 + * insert new index [logical;ptr] into the block at cupr
486 + * it check where to insert: before curp or after curp
488 +static int ext3_ext_insert_index(handle_t *handle,
489 + struct ext3_extents_tree *tree,
490 + struct ext3_ext_path *curp,
491 + int logical, int ptr)
493 + struct ext3_extent_idx *ix;
496 + if ((err = ext3_ext_get_access(handle, tree, curp)))
499 + EXT_ASSERT(logical != curp->p_idx->e_block);
500 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
501 + if (logical > curp->p_idx->e_block) {
503 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
504 + len = (len - 1) * sizeof(struct ext3_extent_idx);
505 + len = len < 0 ? 0 : len;
506 + ext_debug(tree, "insert new index %d after: %d. "
507 + "move %d from 0x%p to 0x%p\n",
509 + (curp->p_idx + 1), (curp->p_idx + 2));
510 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
512 + ix = curp->p_idx + 1;
514 + /* insert before */
515 + len = len * sizeof(struct ext3_extent_idx);
516 + len = len < 0 ? 0 : len;
517 + ext_debug(tree, "insert new index %d before: %d. "
518 + "move %d from 0x%p to 0x%p\n",
520 + curp->p_idx, (curp->p_idx + 1));
521 + memmove(curp->p_idx + 1, curp->p_idx, len);
525 + ix->e_block = logical;
527 + curp->p_hdr->e_num++;
529 + EXT_ASSERT(curp->p_hdr->e_num <= curp->p_hdr->e_max);
530 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
532 + err = ext3_ext_dirty(handle, tree, curp);
533 + ext3_std_error(tree->inode->i_sb, err);
539 + * routine inserts new subtree into the path, using free index entry
541 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
542 + * - makes decision where to split
543 + * - moves remaining extens and index entries (right to the split point)
544 + * into the newly allocated blocks
545 + * - initialize subtree
547 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
548 + struct ext3_ext_path *path,
549 + struct ext3_extent *newext, int at)
551 + struct buffer_head *bh = NULL;
552 + int depth = EXT_DEPTH(tree);
553 + struct ext3_extent_header *neh;
554 + struct ext3_extent_idx *fidx;
555 + struct ext3_extent *ex;
556 + int i = at, k, m, a;
557 + unsigned long newblock, oldblock, border;
558 + int *ablocks = NULL; /* array of allocated blocks */
561 + /* make decision: where to split? */
562 + /* FIXME: now desicion is simplest: at current extent */
564 + /* if current leaf will be splitted, then we should use
565 + * border from split point */
566 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
567 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
568 + border = path[depth].p_ext[1].e_block;
569 + ext_debug(tree, "leaf will be splitted."
570 + " next leaf starts at %d\n",
573 + border = newext->e_block;
574 + ext_debug(tree, "leaf will be added."
575 + " next leaf starts at %d\n",
580 + * if error occurs, then we break processing
581 + * and turn filesystem read-only. so, index won't
582 + * be inserted and tree will be in consistent
583 + * state. next mount will repair buffers too
587 + * get array to track all allocated blocks
588 + * we need this to handle errors and free blocks
591 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
594 + memset(ablocks, 0, sizeof(unsigned long) * depth);
596 + /* allocate all needed blocks */
597 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
598 + for (a = 0; a < depth - at; a++) {
599 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
602 + ablocks[a] = newblock;
605 + /* initialize new leaf */
606 + newblock = ablocks[--a];
607 + EXT_ASSERT(newblock);
608 + bh = sb_getblk(tree->inode->i_sb, newblock);
615 + if ((err = ext3_journal_get_create_access(handle, bh)))
618 + neh = EXT_BLOCK_HDR(bh);
620 + neh->e_max = ext3_ext_space_block(tree);
621 + neh->e_magic = EXT3_EXT_MAGIC;
623 + ex = EXT_FIRST_EXTENT(neh);
625 + /* move remain of path[depth] to the new leaf */
626 + EXT_ASSERT(path[depth].p_hdr->e_num == path[depth].p_hdr->e_max);
627 + /* start copy from next extent */
628 + /* TODO: we could do it by single memmove */
630 + path[depth].p_ext++;
631 + while (path[depth].p_ext <=
632 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
633 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
634 + path[depth].p_ext->e_block,
635 + path[depth].p_ext->e_start,
636 + path[depth].p_ext->e_num,
638 + memmove(ex++, path[depth].p_ext++,
639 + sizeof(struct ext3_extent));
643 + mark_buffer_uptodate(bh, 1);
646 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
651 + /* correct old leaf */
653 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
655 + path[depth].p_hdr->e_num -= m;
656 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
661 + /* create intermediate indexes */
662 + k = depth - at - 1;
663 + EXT_ASSERT(k >= 0);
665 + ext_debug(tree, "create %d intermediate indices\n", k);
666 + /* insert new index into current index block */
667 + /* current depth stored in i var */
670 + oldblock = newblock;
671 + newblock = ablocks[--a];
672 + bh = sb_getblk(tree->inode->i_sb, newblock);
679 + if ((err = ext3_journal_get_create_access(handle, bh)))
682 + neh = EXT_BLOCK_HDR(bh);
684 + neh->e_magic = EXT3_EXT_MAGIC;
685 + neh->e_max = ext3_ext_space_block_idx(tree);
686 + neh->e_depth = depth - i;
687 + fidx = EXT_FIRST_INDEX(neh);
688 + fidx->e_block = border;
689 + fidx->e_leaf = oldblock;
691 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
692 + i, newblock, border, oldblock);
697 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
698 + EXT_MAX_INDEX(path[i].p_hdr));
699 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
700 + EXT_LAST_INDEX(path[i].p_hdr));
701 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
702 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
703 + i, path[i].p_idx->e_block,
704 + path[i].p_idx->e_leaf, newblock);
705 + memmove(++fidx, path[i].p_idx++,
706 + sizeof(struct ext3_extent_idx));
708 + EXT_ASSERT(neh->e_num <= neh->e_max);
711 + mark_buffer_uptodate(bh, 1);
714 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
719 + /* correct old index */
721 + err = ext3_ext_get_access(handle, tree, path + i);
724 + path[i].p_hdr->e_num -= m;
725 + err = ext3_ext_dirty(handle, tree, path + i);
733 + /* insert new index */
735 + err = ext3_ext_insert_index(handle, tree, path + at,
740 + if (buffer_locked(bh))
746 + /* free all allocated blocks in error case */
747 + for (i = 0; i < depth; i++)
750 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
758 + * routine implements tree growing procedure:
759 + * - allocates new block
760 + * - moves top-level data (index block or leaf) into the new block
761 + * - initialize new top-level, creating index that points to the
762 + * just created block
764 +static int ext3_ext_grow_indepth(handle_t *handle,
765 + struct ext3_extents_tree *tree,
766 + struct ext3_ext_path *path,
767 + struct ext3_extent *newext)
769 + struct ext3_ext_path *curp = path;
770 + struct ext3_extent_header *neh;
771 + struct ext3_extent_idx *fidx;
772 + struct buffer_head *bh;
773 + unsigned long newblock;
776 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
780 + bh = sb_getblk(tree->inode->i_sb, newblock);
783 + ext3_std_error(tree->inode->i_sb, err);
788 + if ((err = ext3_journal_get_create_access(handle, bh))) {
793 + /* move top-level index/leaf into new block */
794 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
796 + /* set size of new block */
797 + neh = EXT_BLOCK_HDR(bh);
798 + /* old root could have indexes or leaves
799 + * so calculate e_max right way */
800 + if (EXT_DEPTH(tree))
801 + neh->e_max = ext3_ext_space_block_idx(tree);
803 + neh->e_max = ext3_ext_space_block(tree);
804 + neh->e_magic = EXT3_EXT_MAGIC;
805 + mark_buffer_uptodate(bh, 1);
808 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
811 + /* create index in new top-level index: num,max,pointer */
812 + if ((err = ext3_ext_get_access(handle, tree, curp)))
815 + curp->p_hdr->e_magic = EXT3_EXT_MAGIC;
816 + curp->p_hdr->e_max = ext3_ext_space_root_idx(tree);
817 + curp->p_hdr->e_num = 1;
818 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
819 + /* FIXME: it works, but actually path[0] can be index */
820 + curp->p_idx->e_block = EXT_FIRST_EXTENT(path[0].p_hdr)->e_block;
821 + curp->p_idx->e_leaf = newblock;
823 + neh = EXT_ROOT_HDR(tree);
824 + fidx = EXT_FIRST_INDEX(neh);
825 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
826 + neh->e_num, neh->e_max, fidx->e_block, fidx->e_leaf);
828 + neh->e_depth = path->p_depth + 1;
829 + err = ext3_ext_dirty(handle, tree, curp);
837 + * routine finds empty index and adds new leaf. if no free index found
838 + * then it requests in-depth growing
840 +static int ext3_ext_create_new_leaf(handle_t *handle,
841 + struct ext3_extents_tree *tree,
842 + struct ext3_ext_path *path,
843 + struct ext3_extent *newext)
845 + struct ext3_ext_path *curp;
846 + int depth, i, err = 0;
849 + i = depth = EXT_DEPTH(tree);
851 + /* walk up to the tree and look for free index entry */
852 + curp = path + depth;
853 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
858 + /* we use already allocated block for index block
859 + * so, subsequent data blocks should be contigoues */
860 + if (EXT_HAS_FREE_INDEX(curp)) {
861 + /* if we found index with free entry, then use that
862 + * entry: create all needed subtree and add new leaf */
863 + err = ext3_ext_split(handle, tree, path, newext, i);
866 + ext3_ext_drop_refs(path);
867 + path = ext3_ext_find_extent(tree, newext->e_block, path);
869 + err = PTR_ERR(path);
871 + /* tree is full, time to grow in depth */
872 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
875 + ext3_ext_drop_refs(path);
876 + path = ext3_ext_find_extent(tree, newext->e_block, path);
878 + err = PTR_ERR(path);
881 + * only first (depth 0 -> 1) produces free space
882 + * in all other cases we have to split growed tree
884 + depth = EXT_DEPTH(tree);
885 + if (path[depth].p_hdr->e_num == path[depth].p_hdr->e_max) {
886 + /* now we need split */
898 + * returns allocated block in subsequent extent or 0xffffffff
899 + * NOTE: it consider block number from index entry as
900 + * allocated block. thus, index entries have to be consistent
903 +static unsigned long
904 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
908 + EXT_ASSERT(path != NULL);
909 + depth = path->p_depth;
911 + if (depth == 0 && path->p_ext == NULL)
914 + /* FIXME: what if index isn't full ?! */
915 + while (depth >= 0) {
916 + if (depth == path->p_depth) {
918 + if (path[depth].p_ext !=
919 + EXT_LAST_EXTENT(path[depth].p_hdr))
920 + return path[depth].p_ext[1].e_block;
923 + if (path[depth].p_idx !=
924 + EXT_LAST_INDEX(path[depth].p_hdr))
925 + return path[depth].p_idx[1].e_block;
934 + * returns first allocated block from next leaf or 0xffffffff
936 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
937 + struct ext3_ext_path *path)
941 + EXT_ASSERT(path != NULL);
942 + depth = path->p_depth;
944 + /* zero-tree has no leaf blocks at all */
948 + /* go to index block */
951 + while (depth >= 0) {
952 + if (path[depth].p_idx !=
953 + EXT_LAST_INDEX(path[depth].p_hdr))
954 + return path[depth].p_idx[1].e_block;
962 + * if leaf gets modified and modified extent is first in the leaf
963 + * then we have to correct all indexes above
964 + * TODO: do we need to correct tree in all cases?
966 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
967 + struct ext3_ext_path *path)
969 + struct ext3_extent_header *eh;
970 + int depth = EXT_DEPTH(tree);
971 + struct ext3_extent *ex;
972 + unsigned long border;
975 + eh = path[depth].p_hdr;
976 + ex = path[depth].p_ext;
981 + /* there is no tree at all */
985 + if (ex != EXT_FIRST_EXTENT(eh)) {
986 + /* we correct tree if first leaf got modified only */
991 + * TODO: we need correction if border is smaller then current one
994 + border = path[depth].p_ext->e_block;
995 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
997 + path[k].p_idx->e_block = border;
998 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1002 + /* change all left-side indexes */
1003 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1005 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1007 + path[k].p_idx->e_block = border;
1008 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1016 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1017 + struct ext3_extent *ex1,
1018 + struct ext3_extent *ex2)
1020 + if (ex1->e_block + ex1->e_num != ex2->e_block)
1023 +#ifdef AGRESSIVE_TEST
1024 + if (ex1->e_num >= 4)
1028 + if (!tree->mergable)
1031 + return tree->mergable(ex1, ex2);
1035 + * this routine tries to merge requsted extent into the existing
1036 + * extent or inserts requested extent as new one into the tree,
1037 + * creating new leaf in no-space case
1039 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1040 + struct ext3_ext_path *path,
1041 + struct ext3_extent *newext)
1043 + struct ext3_extent_header * eh;
1044 + struct ext3_extent *ex, *fex;
1045 + struct ext3_extent *nearex; /* nearest extent */
1046 + struct ext3_ext_path *npath = NULL;
1047 + int depth, len, err, next;
1049 + depth = EXT_DEPTH(tree);
1050 + ex = path[depth].p_ext;
1052 + /* try to insert block into found extent and return */
1053 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1054 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1055 + newext->e_num, ex->e_block, ex->e_num,
1057 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1059 + ex->e_num += newext->e_num;
1060 + err = ext3_ext_dirty(handle, tree, path + depth);
1061 + ext3_ext_tree_changed(tree);
1066 + depth = EXT_DEPTH(tree);
1067 + eh = path[depth].p_hdr;
1068 + if (eh->e_num < eh->e_max)
1071 + /* probably next leaf has space for us? */
1072 + fex = EXT_LAST_EXTENT(eh);
1073 + next = ext3_ext_next_leaf_block(tree, path);
1074 + if (newext->e_block > fex->e_block && next != 0xffffffff) {
1075 + ext_debug(tree, "next leaf block - %d\n", next);
1076 + EXT_ASSERT(!npath);
1077 + npath = ext3_ext_find_extent(tree, next, NULL);
1078 + if (IS_ERR(npath))
1079 + return PTR_ERR(npath);
1080 + EXT_ASSERT(npath->p_depth == path->p_depth);
1081 + eh = npath[depth].p_hdr;
1082 + if (eh->e_num < eh->e_max) {
1083 + ext_debug(tree, "next leaf isnt full(%d)\n",
1088 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1089 + eh->e_num, eh->e_max);
1093 + * there is no free space in found leaf
1094 + * we're gonna add new leaf in the tree
1096 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1099 + depth = EXT_DEPTH(tree);
1100 + eh = path[depth].p_hdr;
1103 + nearex = path[depth].p_ext;
1105 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1109 + /* there is no extent in this leaf, create first one */
1110 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1111 + newext->e_block, newext->e_start,
1113 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1114 + } else if (newext->e_block > nearex->e_block) {
1115 + EXT_ASSERT(newext->e_block != nearex->e_block);
1116 + if (nearex != EXT_LAST_EXTENT(eh)) {
1117 + len = EXT_MAX_EXTENT(eh) - nearex;
1118 + len = (len - 1) * sizeof(struct ext3_extent);
1119 + len = len < 0 ? 0 : len;
1120 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1121 + "move %d from 0x%p to 0x%p\n",
1122 + newext->e_block, newext->e_start,
1124 + nearex, len, nearex + 1, nearex + 2);
1125 + memmove(nearex + 2, nearex + 1, len);
1127 + path[depth].p_ext = nearex + 1;
1129 + EXT_ASSERT(newext->e_block != nearex->e_block);
1130 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1131 + len = len < 0 ? 0 : len;
1132 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1133 + "move %d from 0x%p to 0x%p\n",
1134 + newext->e_block, newext->e_start, newext->e_num,
1135 + nearex, len, nearex + 1, nearex + 2);
1136 + memmove(nearex + 1, nearex, len);
1137 + path[depth].p_ext = nearex;
1141 + nearex = path[depth].p_ext;
1142 + nearex->e_block = newext->e_block;
1143 + nearex->e_start = newext->e_start;
1144 + nearex->e_num = newext->e_num;
1146 + /* time to correct all indexes above */
1147 + err = ext3_ext_correct_indexes(handle, tree, path);
1151 + err = ext3_ext_dirty(handle, tree, path + depth);
1155 + ext3_ext_drop_refs(npath);
1159 + ext3_ext_tree_changed(tree);
1163 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1164 + unsigned long num, ext_prepare_callback func)
1166 + struct ext3_ext_path *path = NULL;
1167 + struct ext3_extent *ex, cbex;
1168 + unsigned long next, start = 0, end = 0;
1169 + unsigned long last = block + num;
1170 + int depth, exists, err = 0;
1174 + EXT_ASSERT(tree->inode);
1175 + EXT_ASSERT(tree->root);
1177 + while (block < last && block != 0xfffffffff) {
1178 + num = last - block;
1179 + /* find extent for this block */
1180 + path = ext3_ext_find_extent(tree, block, path);
1181 + if (IS_ERR(path)) {
1182 + err = PTR_ERR(path);
1187 + depth = EXT_DEPTH(tree);
1188 + ex = path[depth].p_ext;
1189 + next = ext3_ext_next_allocated_block(path);
1193 + /* there is no extent yet, so try to allocate
1194 + * all requested space */
1196 + end = block + num;
1197 + } else if (ex->e_block > block) {
1198 + /* need to allocate space before found extent */
1200 + end = ex->e_block;
1201 + if (block + num < end)
1202 + end = block + num;
1203 + } else if (block >= ex->e_block + ex->e_num) {
1204 + /* need to allocate space after found extent */
1206 + end = block + num;
1209 + } else if (block >= ex->e_block) {
1211 + * some part of requested space is covered
1215 + end = ex->e_block + ex->e_num;
1216 + if (block + num < end)
1217 + end = block + num;
1222 + EXT_ASSERT(end > start);
1225 + cbex.e_block = start;
1226 + cbex.e_num = end - start;
1231 + err = func(tree, path, &cbex, exists);
1232 + ext3_ext_drop_refs(path);
1236 + if (err == EXT_REPEAT)
1238 + else if (err == EXT_BREAK) {
1243 + if (EXT_DEPTH(tree) != depth) {
1244 + /* depth was changed. we have to realloc path */
1249 + block = cbex.e_block + cbex.e_num;
1253 + ext3_ext_drop_refs(path);
1261 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
1264 + tree->cex->e_num = 0;
1268 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, struct ext3_extent *ex)
1272 + EXT_ASSERT(ex->e_num);
1273 + tree->cex->e_block = ex->e_block;
1274 + tree->cex->e_start = ex->e_start;
1275 + tree->cex->e_num = ex->e_num;
1280 + * this routine calculate boundaries of the gap requested block fits into
1281 + * and cache this gap
1284 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1285 + struct ext3_ext_path *path,
1286 + unsigned long block)
1288 + int depth = EXT_DEPTH(tree);
1289 + struct ext3_extent *ex, gex;
1294 + ex = path[depth].p_ext;
1296 + /* there is no extent yet, so gap is [0;-] */
1298 + gex.e_num = 0xffffffff;
1299 + ext_debug(tree, "cache gap(whole file):");
1300 + } else if (block < ex->e_block) {
1301 + gex.e_block = block;
1302 + gex.e_num = ex->e_block - block;
1303 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1304 + (unsigned long) block,
1305 + (unsigned long) ex->e_block,
1306 + (unsigned long) ex->e_num);
1307 + } else if (block >= ex->e_block + ex->e_num) {
1308 + gex.e_block = ex->e_block + ex->e_num;
1309 + gex.e_num = ext3_ext_next_allocated_block(path);
1310 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1311 + (unsigned long) ex->e_block,
1312 + (unsigned long) ex->e_num,
1313 + (unsigned long) block);
1314 + EXT_ASSERT(gex.e_num > gex.e_block);
1315 + gex.e_num = gex.e_num - gex.e_block;
1320 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.e_block,
1321 + (unsigned long) gex.e_num);
1322 + gex.e_start = 0xffffffff;
1323 + ext3_ext_put_in_cache(tree, &gex);
1327 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1328 + struct ext3_extent *ex)
1330 + struct ext3_extent *cex = tree->cex;
1332 + /* is there cache storage at all? */
1336 + /* has cache valid data? */
1337 + if (cex->e_num == 0)
1340 + if (block >= cex->e_block && block < cex->e_block + cex->e_num) {
1341 + ex->e_block = cex->e_block;
1342 + ex->e_start = cex->e_start;
1343 + ex->e_num = cex->e_num;
1344 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1345 + (unsigned long) block,
1346 + (unsigned long) ex->e_block,
1347 + (unsigned long) ex->e_num,
1348 + (unsigned long) ex->e_start);
1352 + /* not in cache */
1357 + * routine removes index from the index block
1358 + * it's used in truncate case only. thus all requests are for
1359 + * last index in the block only
1361 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1362 + struct ext3_ext_path *path)
1364 + struct buffer_head *bh;
1367 + /* free index block */
1369 + EXT_ASSERT(path->p_hdr->e_num);
1370 + if ((err = ext3_ext_get_access(handle, tree, path)))
1372 + path->p_hdr->e_num--;
1373 + if ((err = ext3_ext_dirty(handle, tree, path)))
1375 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1376 + path->p_idx->e_leaf);
1377 + bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->e_leaf);
1378 + ext3_forget(handle, 0, tree->inode, bh, path->p_idx->e_leaf);
1379 + ext3_free_blocks(handle, tree->inode, path->p_idx->e_leaf, 1);
1383 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1384 + struct ext3_ext_path *path)
1386 + int depth = EXT_DEPTH(tree);
1390 + /* probably there is space in leaf? */
1391 + if (path[depth].p_hdr->e_num < path[depth].p_hdr->e_max)
1396 + * the worste case we're expecting is creation of the
1397 + * new root (growing in depth) with index splitting
1398 + * for splitting we have to consider depth + 1 because
1399 + * previous growing could increase it
1401 + depth = depth + 1;
1404 + * growing in depth:
1405 + * block allocation + new root + old root
1407 + needed = EXT3_ALLOC_NEEDED + 2;
1409 + /* index split. we may need:
1410 + * allocate intermediate indexes and new leaf
1411 + * change two blocks at each level, but root
1412 + * modify root block (inode)
1414 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1420 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1421 + struct ext3_ext_path *path, unsigned long start,
1422 + unsigned long end)
1424 + struct ext3_extent *ex, tex;
1425 + struct ext3_ext_path *npath;
1426 + int depth, creds, err;
1428 + depth = EXT_DEPTH(tree);
1429 + ex = path[depth].p_ext;
1431 + EXT_ASSERT(end < ex->e_block + ex->e_num - 1);
1432 + EXT_ASSERT(ex->e_block < start);
1434 + /* calculate tail extent */
1435 + tex.e_block = end + 1;
1436 + EXT_ASSERT(tex.e_block < ex->e_block + ex->e_num);
1437 + tex.e_num = ex->e_block + ex->e_num - tex.e_block;
1439 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1440 + handle = ext3_ext_journal_restart(handle, creds);
1441 + if (IS_ERR(handle))
1442 + return PTR_ERR(handle);
1444 + /* calculate head extent. use primary extent */
1445 + err = ext3_ext_get_access(handle, tree, path + depth);
1448 + ex->e_num = start - ex->e_block;
1449 + err = ext3_ext_dirty(handle, tree, path + depth);
1453 + /* FIXME: some callback to free underlying resource
1454 + * and correct e_start? */
1455 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1456 + ex->e_block, ex->e_num, tex.e_block, tex.e_num);
1458 + npath = ext3_ext_find_extent(tree, ex->e_block, NULL);
1459 + if (IS_ERR(npath))
1460 + return PTR_ERR(npath);
1461 + depth = EXT_DEPTH(tree);
1462 + EXT_ASSERT(npath[depth].p_ext->e_block == ex->e_block);
1463 + EXT_ASSERT(npath[depth].p_ext->e_num == ex->e_num);
1465 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1466 + ext3_ext_drop_refs(npath);
1474 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1475 + struct ext3_ext_path *path, unsigned long start,
1476 + unsigned long end)
1478 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1479 + int err = 0, correct_index = 0;
1480 + int depth = EXT_DEPTH(tree), credits;
1481 + struct ext3_extent_header *eh;
1482 + unsigned a, b, block, num;
1484 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1485 + if (!path[depth].p_hdr)
1486 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1487 + eh = path[depth].p_hdr;
1489 + EXT_ASSERT(eh->e_num <= eh->e_max);
1490 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
1492 + /* find where to start removing */
1493 + le = ex = EXT_LAST_EXTENT(eh);
1494 + while (ex != EXT_FIRST_EXTENT(eh)) {
1495 + if (ex->e_block <= end)
1500 + if (start > ex->e_block && end < ex->e_block + ex->e_num - 1) {
1501 + /* removal of internal part of the extent requested
1502 + * tail and head must be placed in different extent
1503 + * so, we have to insert one more extent */
1504 + path[depth].p_ext = ex;
1505 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1509 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1510 + ex->e_block + ex->e_num > start) {
1511 + ext_debug(tree, "remove ext %u:%u\n", ex->e_block, ex->e_num);
1512 + path[depth].p_ext = ex;
1514 + a = ex->e_block > start ? ex->e_block : start;
1515 + b = ex->e_block + ex->e_num - 1 < end ?
1516 + ex->e_block + ex->e_num - 1 : end;
1518 + ext_debug(tree, " border %u:%u\n", a, b);
1520 + if (a != ex->e_block && b != ex->e_block + ex->e_num - 1) {
1524 + } else if (a != ex->e_block) {
1525 + /* remove tail of the extent */
1526 + block = ex->e_block;
1528 + } else if (b != ex->e_block + ex->e_num - 1) {
1529 + /* remove head of the extent */
1533 + /* remove whole extent: excelent! */
1534 + block = ex->e_block;
1536 + EXT_ASSERT(a == ex->e_block &&
1537 + b == ex->e_block + ex->e_num - 1);
1540 + if (ex == EXT_FIRST_EXTENT(eh))
1541 + correct_index = 1;
1544 + if (correct_index)
1545 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1546 + if (tree->remove_extent_credits)
1547 + credits += tree->remove_extent_credits(tree, ex, a, b);
1549 + handle = ext3_ext_journal_restart(handle, credits);
1550 + if (IS_ERR(handle)) {
1551 + err = PTR_ERR(handle);
1555 + err = ext3_ext_get_access(handle, tree, path + depth);
1559 + if (tree->remove_extent)
1560 + err = tree->remove_extent(tree, ex, a, b);
1565 + /* this extent is removed entirely mark slot unused */
1571 + ex->e_block = block;
1574 + err = ext3_ext_dirty(handle, tree, path + depth);
1578 + ext_debug(tree, "new extent: %u:%u:%u\n",
1579 + ex->e_block, ex->e_num, ex->e_start);
1584 + /* reuse unused slots */
1586 + if (lu->e_start) {
1595 + if (correct_index && eh->e_num)
1596 + err = ext3_ext_correct_indexes(handle, tree, path);
1598 + /* if this leaf is free, then we should
1599 + * remove it from index block above */
1600 + if (err == 0 && eh->e_num == 0 && path[depth].p_bh != NULL)
1601 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1608 +static struct ext3_extent_idx *
1609 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1611 + struct ext3_extent_idx *ix;
1613 + ix = EXT_LAST_INDEX(hdr);
1614 + while (ix != EXT_FIRST_INDEX(hdr)) {
1615 + if (ix->e_block <= block)
1623 + * returns 1 if current index have to be freed (even partial)
1626 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1628 + EXT_ASSERT(path->p_idx);
1630 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1634 + * if truncate on deeper level happened it it wasn't partial
1635 + * so we have to consider current index for truncation
1637 + if (path->p_hdr->e_num == path->p_block)
1642 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1643 + unsigned long start, unsigned long end)
1645 + struct inode *inode = tree->inode;
1646 + struct super_block *sb = inode->i_sb;
1647 + int depth = EXT_DEPTH(tree);
1648 + struct ext3_ext_path *path;
1650 + int i = 0, err = 0;
1652 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1654 + /* probably first extent we're gonna free will be last in block */
1655 + handle = ext3_journal_start(inode, depth + 1);
1656 + if (IS_ERR(handle))
1657 + return PTR_ERR(handle);
1659 + ext3_ext_invalidate_cache(tree);
1662 + * we start scanning from right side freeing all the blocks
1663 + * after i_size and walking into the deep
1665 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1666 + if (IS_ERR(path)) {
1667 + ext3_error(sb, "ext3_ext_remove_space",
1668 + "Can't allocate path array");
1669 + ext3_journal_stop(handle, inode);
1672 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1673 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1675 + while (i >= 0 && err == 0) {
1677 + /* this is leaf block */
1678 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1679 + /* root level have p_bh == NULL, brelse() eats this */
1680 + brelse(path[i].p_bh);
1685 + /* this is index block */
1686 + if (!path[i].p_hdr) {
1687 + ext_debug(tree, "initialize header\n");
1688 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1691 + EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1692 + EXT_ASSERT(path[i].p_hdr->e_magic == EXT3_EXT_MAGIC);
1694 + if (!path[i].p_idx) {
1695 + /* this level hasn't touched yet */
1697 + ext3_ext_last_covered(path[i].p_hdr, end);
1698 + path[i].p_block = path[i].p_hdr->e_num + 1;
1699 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1700 + path[i].p_hdr, path[i].p_hdr->e_num);
1702 + /* we've already was here, see at next index */
1706 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1707 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1709 + if (ext3_ext_more_to_rm(path + i)) {
1710 + /* go to the next level */
1711 + ext_debug(tree, "move to level %d (block %d)\n",
1712 + i + 1, path[i].p_idx->e_leaf);
1713 + memset(path + i + 1, 0, sizeof(*path));
1714 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->e_leaf);
1715 + if (!path[i+1].p_bh) {
1716 + /* should we reset i_size? */
1720 + /* put actual number of indexes to know is this
1721 + * number got changed at the next iteration */
1722 + path[i].p_block = path[i].p_hdr->e_num;
1725 + /* we finish processing this index, go up */
1726 + if (path[i].p_hdr->e_num == 0 && i > 0) {
1727 + /* index is empty, remove it
1728 + * handle must be already prepared by the
1729 + * truncate_leaf() */
1730 + err = ext3_ext_rm_idx(handle, tree, path + i);
1732 + /* root level have p_bh == NULL, brelse() eats this */
1733 + brelse(path[i].p_bh);
1735 + ext_debug(tree, "return to level %d\n", i);
1739 + /* TODO: flexible tree reduction should be here */
1740 + if (path->p_hdr->e_num == 0) {
1742 + * truncate to zero freed all the tree
1743 + * so, we need to correct e_depth
1745 + err = ext3_ext_get_access(handle, tree, path);
1747 + EXT_ROOT_HDR(tree)->e_depth = 0;
1748 + err = ext3_ext_dirty(handle, tree, path);
1751 + ext3_ext_tree_changed(tree);
1754 + ext3_journal_stop(handle, inode);
1760 + * called at mount time
1762 +void ext3_ext_init(struct super_block *sb)
1765 + * possible initialization would be here
1768 + if (test_opt(sb, EXTENTS)) {
1769 + printk("EXT3-fs: file extents enabled");
1770 +#ifdef AGRESSIVE_TEST
1771 + printk(", agressive tests");
1773 +#ifdef CHECK_BINSEARCH
1774 + printk(", check binsearch");
1781 + * called at umount time
1783 +void ext3_ext_release(struct super_block *sb)
1787 +/************************************************************************
1788 + * VFS related routines
1789 + ************************************************************************/
1791 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1793 + /* we use in-core data, not bh */
1797 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1799 + struct inode *inode = buffer;
1800 + return ext3_mark_inode_dirty(handle, inode);
1803 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1804 + struct ext3_extent *ex2)
1806 + if (ex1->e_start + ex1->e_num == ex2->e_start)
1812 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1813 + struct ext3_extent *ex,
1814 + unsigned long from, unsigned long to)
1818 + /* at present, extent can't cross block group */;
1819 + needed = 3; /* bitmap + group desc + sb */
1821 +#ifdef CONFIG_QUOTA
1822 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1828 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1829 + struct ext3_extent *ex,
1830 + unsigned long from, unsigned long to)
1832 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1833 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1835 + if (IS_ERR(handle))
1836 + return PTR_ERR(handle);
1837 + if (from >= ex->e_block && to == ex->e_block + ex->e_num - 1) {
1838 + /* tail removal */
1839 + unsigned long num, start;
1840 + num = ex->e_block + ex->e_num - from;
1841 + start = ex->e_start + ex->e_num - num;
1842 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1844 + ext3_free_blocks(handle, tree->inode, start, num);
1845 + } else if (from == ex->e_block && to <= ex->e_block + ex->e_num - 1) {
1846 + printk("strange request: removal %lu-%lu from %u:%u\n",
1847 + from, to, ex->e_block, ex->e_num);
1849 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1850 + from, to, ex->e_block, ex->e_num);
1852 + ext3_journal_stop(handle, tree->inode);
1856 +static int ext3_ext_find_goal(struct inode *inode,
1857 + struct ext3_ext_path *path)
1859 + struct ext3_inode_info *ei = EXT3_I(inode);
1860 + unsigned long bg_start;
1861 + unsigned long colour;
1865 + depth = path->p_depth;
1866 + /* try to find previous block */
1867 + if (path[depth].p_ext)
1868 + return path[depth].p_ext->e_start +
1869 + path[depth].p_ext->e_num;
1871 + /* it looks index is empty
1872 + * try to find starting from index itself */
1873 + if (path[depth].p_bh)
1874 + return path[depth].p_bh->b_blocknr;
1877 + /* OK. use inode's group */
1878 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1879 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1880 + colour = (current->pid % 16) *
1881 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1882 + return bg_start + colour;
1885 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1886 + struct ext3_ext_path *path,
1887 + struct ext3_extent *ex, int *err)
1889 + struct inode *inode = tree->inode;
1890 + int newblock, goal;
1894 + EXT_ASSERT(ex->e_start);
1895 + EXT_ASSERT(ex->e_num);
1897 + /* reuse block from the extent to order data/metadata */
1898 + newblock = ex->e_start++;
1900 + if (ex->e_num == 0) {
1902 + /* allocate new block for the extent */
1903 + goal = ext3_ext_find_goal(inode, path);
1904 + ex->e_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1905 + if (ex->e_start == 0) {
1906 + /* error occured: restore old extent */
1907 + ex->e_start = newblock;
1914 +static void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1915 + struct inode *inode)
1917 + tree->inode = inode;
1918 + tree->root = (void *) EXT3_I(inode)->i_data;
1919 + tree->get_write_access = ext3_get_inode_write_access;
1920 + tree->mark_buffer_dirty = ext3_mark_buffer_dirty;
1921 + tree->mergable = ext3_ext_mergable;
1922 + tree->new_block = ext3_new_block_cb;
1923 + tree->remove_extent = ext3_remove_blocks;
1924 + tree->remove_extent_credits = ext3_remove_blocks_credits;
1925 + tree->buffer = (void *) inode;
1926 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1927 + tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1930 +#if EXT3_MULTIBLOCK_ALLOCATOR
1932 +ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
1933 + struct ext3_ext_path *path,
1934 + struct ext3_extent *newex, int exist)
1936 + struct inode *inode = tree->inode;
1937 + int count, err, goal;
1938 + loff_t new_i_size;
1940 + unsigned long pblock;
1941 + unsigned long tgen;
1944 + return EXT_CONTINUE;
1946 + tgen = EXT_GENERATION(tree);
1947 + count = ext3_ext_calc_credits_for_insert(tree, path);
1948 + up_write(&EXT3_I(inode)->truncate_sem);
1950 + handle = ext3_journal_start(inode, count + EXT3_ALLOC_NEEDED + 1);
1951 + if (IS_ERR(handle)) {
1952 + down_write(&EXT3_I(inode)->truncate_sem);
1953 + return PTR_ERR(handle);
1956 + if (tgen != EXT_GENERATION(tree)) {
1957 + /* the tree has changed. so path can be invalid at moment */
1958 + ext3_journal_stop(handle, inode);
1959 + down_write(&EXT3_I(inode)->truncate_sem);
1960 + return EXT_REPEAT;
1963 + down_write(&EXT3_I(inode)->truncate_sem);
1964 + goal = ext3_ext_find_goal(inode, path);
1965 + count = newex->e_num;
1966 + pblock = ext3_new_blocks(handle, inode, &count, goal, &err);
1969 + EXT_ASSERT(count <= newex->e_num);
1971 + /* insert new extent */
1972 + newex->e_start = pblock;
1973 + newex->e_num = count;
1974 + err = ext3_ext_insert_extent(handle, tree, path, newex);
1979 + /* correct on-disk inode size */
1980 + if (newex->e_num > 0) {
1981 + new_i_size = (loff_t) newex->e_block + newex->e_num;
1982 + new_i_size = new_i_size << inode->i_blkbits;
1983 + if (new_i_size > EXT3_I(inode)->i_disksize) {
1984 + EXT3_I(inode)->i_disksize = new_i_size;
1985 + err = ext3_mark_inode_dirty(handle, inode);
1990 + ext3_journal_stop(handle, inode);
1995 +int ext3_ext_allocate_nblocks(struct inode *inode, unsigned long block,
1996 + unsigned long num)
1998 + struct ext3_extents_tree tree;
2001 + ext3_init_tree_desc(&tree, inode);
2002 + ext_debug(&tree, "blocks %lu-%lu requested for inode %u\n",
2003 + block, block + num,(unsigned) inode->i_ino);
2004 + down_write(&EXT3_I(inode)->truncate_sem);
2005 + err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
2006 + ext3_ext_invalidate_cache(&tree);
2007 + up_write(&EXT3_I(inode)->truncate_sem);
2013 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2014 + long iblock, struct buffer_head *bh_result, int create)
2016 + struct ext3_ext_path *path = NULL;
2017 + struct ext3_extent newex;
2018 + struct ext3_extent *ex;
2019 + int goal, newblock, err = 0, depth;
2020 + struct ext3_extents_tree tree;
2022 + clear_bit(BH_New, &bh_result->b_state);
2023 + ext3_init_tree_desc(&tree, inode);
2024 + ext_debug(&tree, "block %d requested for inode %u\n",
2025 + (int) iblock, (unsigned) inode->i_ino);
2026 + down_write(&EXT3_I(inode)->truncate_sem);
2028 + /* check in cache */
2029 + if (ext3_ext_in_cache(&tree, iblock, &newex)) {
2030 + if (newex.e_start == 0xffffffff && !create) {
2031 + /* block isn't allocated yet and
2032 + * user don't want to allocate it */
2034 + } else if (newex.e_start) {
2035 + /* block is already allocated */
2036 + newblock = iblock - newex.e_block + newex.e_start;
2041 + /* find extent for this block */
2042 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2043 + if (IS_ERR(path)) {
2044 + err = PTR_ERR(path);
2049 + depth = EXT_DEPTH(&tree);
2052 + * consistent leaf must not be empty
2053 + * this situations is possible, though, _during_ tree modification
2054 + * this is why assert can't be put in ext3_ext_find_extent()
2056 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2058 + if ((ex = path[depth].p_ext)) {
2059 + /* if found exent covers block, simple return it */
2060 + if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
2061 + newblock = iblock - ex->e_block + ex->e_start;
2062 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2063 + (int) iblock, ex->e_block, ex->e_num,
2065 + ext3_ext_put_in_cache(&tree, ex);
2071 + * requested block isn't allocated yet
2072 + * we couldn't try to create block if create flag is zero
2075 + /* put just found gap into cache to speedup subsequest reqs */
2076 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2080 + /* allocate new block */
2081 + goal = ext3_ext_find_goal(inode, path);
2082 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2085 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2088 + /* try to insert new extent into found leaf and return */
2089 + newex.e_block = iblock;
2090 + newex.e_start = newblock;
2092 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2096 + if (inode->i_size > EXT3_I(inode)->i_disksize)
2097 + EXT3_I(inode)->i_disksize = inode->i_size;
2099 + /* previous routine could use block we allocated */
2100 + newblock = newex.e_start;
2101 + set_bit(BH_New, &bh_result->b_state);
2103 + ext3_ext_put_in_cache(&tree, &newex);
2105 + ext3_ext_show_leaf(&tree, path);
2106 + set_bit(BH_Mapped, &bh_result->b_state);
2107 + bh_result->b_dev = inode->i_sb->s_dev;
2108 + bh_result->b_blocknr = newblock;
2111 + ext3_ext_drop_refs(path);
2114 + up_write(&EXT3_I(inode)->truncate_sem);
2119 +void ext3_ext_truncate(struct inode * inode)
2121 + struct address_space *mapping = inode->i_mapping;
2122 + struct super_block *sb = inode->i_sb;
2123 + struct ext3_extents_tree tree;
2124 + unsigned long last_block;
2128 + ext3_init_tree_desc(&tree, inode);
2131 + * probably first extent we're gonna free will be last in block
2133 + err = ext3_writepage_trans_blocks(inode) + 3;
2134 + handle = ext3_journal_start(inode, err);
2135 + if (IS_ERR(handle))
2138 + ext3_block_truncate_page(handle, mapping, inode->i_size);
2140 + down_write(&EXT3_I(inode)->truncate_sem);
2141 + ext3_ext_invalidate_cache(&tree);
2144 + * TODO: optimization is possible here
2145 + * probably we need not scaning at all,
2146 + * because page truncation is enough
2148 + if (ext3_orphan_add(handle, inode))
2151 + /* we have to know where to truncate from in crash case */
2152 + EXT3_I(inode)->i_disksize = inode->i_size;
2153 + ext3_mark_inode_dirty(handle, inode);
2155 + last_block = (inode->i_size + sb->s_blocksize - 1)
2156 + >> EXT3_BLOCK_SIZE_BITS(sb);
2157 + err = ext3_ext_remove_space(&tree, last_block, 0xffffffff);
2159 + /* In a multi-transaction truncate, we only make the final
2160 + * transaction synchronous */
2161 + if (IS_SYNC(inode))
2162 + handle->h_sync = 1;
2166 + * If this was a simple ftruncate(), and the file will remain alive
2167 + * then we need to clear up the orphan record which we created above.
2168 + * However, if this was a real unlink then we were called by
2169 + * ext3_delete_inode(), and we allow that function to clean up the
2170 + * orphan info for us.
2172 + if (inode->i_nlink)
2173 + ext3_orphan_del(handle, inode);
2175 + up_write(&EXT3_I(inode)->truncate_sem);
2176 + ext3_journal_stop(handle, inode);
2180 + * this routine calculate max number of blocks we could modify
2181 + * in order to allocate new block for an inode
2183 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2185 + struct ext3_extents_tree tree;
2188 + ext3_init_tree_desc(&tree, inode);
2190 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2192 + /* caller want to allocate num blocks */
2195 +#ifdef CONFIG_QUOTA
2197 + * FIXME: real calculation should be here
2198 + * it depends on blockmap format of qouta file
2200 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2206 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2208 + struct ext3_extents_tree tree;
2210 + ext3_init_tree_desc(&tree, inode);
2211 + ext3_extent_tree_init(handle, &tree);
2215 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2216 + struct ext3_ext_path *path,
2217 + struct ext3_extent *newex, int exist)
2219 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2222 + return EXT_CONTINUE;
2225 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2228 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2230 + buf->cur += sizeof(*newex);
2232 + buf->err = -EFAULT;
2235 + return EXT_CONTINUE;
2239 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2240 + struct ext3_ext_path *path,
2241 + struct ext3_extent *ex, int exist)
2243 + struct ext3_extent_tree_stats *buf =
2244 + (struct ext3_extent_tree_stats *) tree->private;
2248 + return EXT_CONTINUE;
2250 + depth = EXT_DEPTH(tree);
2251 + buf->extents_num++;
2252 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2254 + return EXT_CONTINUE;
2257 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2258 + unsigned long arg)
2262 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2265 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2266 + struct ext3_extent_buf buf;
2267 + struct ext3_extents_tree tree;
2269 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2272 + ext3_init_tree_desc(&tree, inode);
2273 + buf.cur = buf.buffer;
2275 + tree.private = &buf;
2276 + down_write(&EXT3_I(inode)->truncate_sem);
2277 + err = ext3_ext_walk_space(&tree, buf.start, 0xffffffff,
2278 + ext3_ext_store_extent_cb);
2279 + up_write(&EXT3_I(inode)->truncate_sem);
2282 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2283 + struct ext3_extent_tree_stats buf;
2284 + struct ext3_extents_tree tree;
2286 + ext3_init_tree_desc(&tree, inode);
2287 + down_write(&EXT3_I(inode)->truncate_sem);
2288 + buf.depth = EXT_DEPTH(&tree);
2289 + buf.extents_num = 0;
2291 + tree.private = &buf;
2292 + err = ext3_ext_walk_space(&tree, 0, 0xffffffff,
2293 + ext3_ext_collect_stats_cb);
2294 + up_write(&EXT3_I(inode)->truncate_sem);
2296 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2297 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2298 + struct ext3_extents_tree tree;
2299 + ext3_init_tree_desc(&tree, inode);
2300 + down_write(&EXT3_I(inode)->truncate_sem);
2301 + err = EXT_DEPTH(&tree);
2302 + up_write(&EXT3_I(inode)->truncate_sem);
2308 Index: linux-2.4.24/fs/ext3/ialloc.c
2309 ===================================================================
2310 --- linux-2.4.24.orig/fs/ext3/ialloc.c 2004-01-14 02:58:45.000000000 +0300
2311 +++ linux-2.4.24/fs/ext3/ialloc.c 2004-01-26 23:17:19.000000000 +0300
2312 @@ -592,11 +592,13 @@
2316 + if (test_opt(sb, EXTENTS)) {
2317 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2318 + ext3_extents_initialize_blockmap(handle, inode);
2320 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2326 if(DQUOT_ALLOC_INODE(inode)) {
2328 Index: linux-2.4.24/fs/ext3/inode.c
2329 ===================================================================
2330 --- linux-2.4.24.orig/fs/ext3/inode.c 2004-01-14 02:58:45.000000000 +0300
2331 +++ linux-2.4.24/fs/ext3/inode.c 2004-01-26 23:17:19.000000000 +0300
2332 @@ -848,6 +848,15 @@
2337 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2338 + struct buffer_head *bh, int create)
2340 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2341 + return ext3_ext_get_block(handle, inode, block, bh, create);
2342 + return ext3_get_block_handle(handle, inode, block, bh, create);
2346 * The BKL is not held on entry here.
2349 handle = ext3_journal_current_handle();
2350 J_ASSERT(handle != 0);
2352 - ret = ext3_get_block_handle(handle, inode, iblock, bh_result, create);
2353 + ret = ext3_get_block_wrap(handle, inode, iblock, bh_result, create);
2359 dummy.b_blocknr = -1000;
2360 buffer_trace_init(&dummy.b_history);
2361 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create);
2362 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create);
2363 if (!*errp && buffer_mapped(&dummy)) {
2364 struct buffer_head *bh;
2365 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2366 @@ -1403,7 +1412,7 @@
2367 * This required during truncate. We need to physically zero the tail end
2368 * of that block so it doesn't yield old data if the file is later grown.
2370 -static int ext3_block_truncate_page(handle_t *handle,
2371 +int ext3_block_truncate_page(handle_t *handle,
2372 struct address_space *mapping, loff_t from)
2374 unsigned long index = from >> PAGE_CACHE_SHIFT;
2375 @@ -1888,6 +1897,9 @@
2377 ext3_discard_prealloc(inode);
2379 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2380 + return ext3_ext_truncate(inode);
2382 handle = start_transaction(inode);
2384 return; /* AKPM: return what? */
2385 @@ -2536,6 +2548,9 @@
2386 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2389 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2390 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2392 if (ext3_should_journal_data(inode))
2393 ret = 3 * (bpp + indirects) + 2;
2395 @@ -2972,7 +2987,7 @@
2397 /* alloc blocks one by one */
2398 for (i = 0; i < nblocks; i++) {
2399 - ret = ext3_get_block_handle(handle, inode, blocks[i],
2400 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
2404 @@ -3048,7 +3063,7 @@
2408 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1);
2409 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1);
2411 printk(KERN_INFO "ext3_map_inode_page: error %d "
2412 "allocating block %ld\n", rc, iblock);
2413 Index: linux-2.4.24/fs/ext3/Makefile
2414 ===================================================================
2415 --- linux-2.4.24.orig/fs/ext3/Makefile 2004-01-14 02:58:45.000000000 +0300
2416 +++ linux-2.4.24/fs/ext3/Makefile 2004-01-26 23:17:19.000000000 +0300
2419 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2420 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2422 + xattr_trusted.o extents.o
2423 obj-m := $(O_TARGET)
2425 export-objs += xattr.o
2426 Index: linux-2.4.24/fs/ext3/super.c
2427 ===================================================================
2428 --- linux-2.4.24.orig/fs/ext3/super.c 2004-01-14 02:58:45.000000000 +0300
2429 +++ linux-2.4.24/fs/ext3/super.c 2004-01-26 23:17:19.000000000 +0300
2433 J_ASSERT(sbi->s_delete_inodes == 0);
2434 + ext3_ext_release(sb);
2435 ext3_xattr_put_super(sb);
2436 journal_destroy(sbi->s_journal);
2437 if (!(sb->s_flags & MS_RDONLY)) {
2438 @@ -702,6 +703,10 @@
2442 + else if (!strcmp (this_char, "extents"))
2443 + set_opt (*mount_options, EXTENTS);
2444 + else if (!strcmp (this_char, "extdebug"))
2445 + set_opt (*mount_options, EXTDEBUG);
2446 else if (!strcmp (this_char, "grpid") ||
2447 !strcmp (this_char, "bsdgroups"))
2448 set_opt (*mount_options, GRPID);
2449 @@ -1392,6 +1397,8 @@
2450 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2453 + ext3_ext_init(sb);
2458 Index: linux-2.4.24/fs/ext3/ioctl.c
2459 ===================================================================
2460 --- linux-2.4.24.orig/fs/ext3/ioctl.c 2004-01-14 02:58:42.000000000 +0300
2461 +++ linux-2.4.24/fs/ext3/ioctl.c 2004-01-26 23:17:19.000000000 +0300
2462 @@ -174,6 +174,10 @@
2466 + case EXT3_IOC_GET_EXTENTS:
2467 + case EXT3_IOC_GET_TREE_STATS:
2468 + case EXT3_IOC_GET_TREE_DEPTH:
2469 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2473 Index: linux-2.4.24/include/linux/ext3_fs.h
2474 ===================================================================
2475 --- linux-2.4.24.orig/include/linux/ext3_fs.h 2004-01-14 02:58:45.000000000 +0300
2476 +++ linux-2.4.24/include/linux/ext3_fs.h 2004-01-26 23:17:19.000000000 +0300
2478 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2479 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2480 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2481 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2483 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2484 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2486 #ifdef CONFIG_JBD_DEBUG
2487 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2489 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 5, long)
2490 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 6, long)
2491 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 7, long)
2494 * Structure of an inode on the disk
2496 #define EXT3_MOUNT_IOPEN 0x8000 /* Allow access via iopen */
2497 #define EXT3_MOUNT_IOPEN_NOPRIV 0x10000 /* Make iopen world-readable */
2498 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
2499 +#define EXT3_MOUNT_EXTENTS 0x100000/* Extents support */
2500 +#define EXT3_MOUNT_EXTDEBUG 0x200000/* Extents debug */
2502 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2503 #ifndef _LINUX_EXT2_FS_H
2505 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2508 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2509 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2510 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2511 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2512 @@ -769,6 +776,14 @@
2513 extern struct inode_operations ext3_symlink_inode_operations;
2514 extern struct inode_operations ext3_fast_symlink_inode_operations;
2517 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2518 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2519 + struct buffer_head *, int);
2520 +extern void ext3_ext_truncate(struct inode *);
2521 +extern void ext3_ext_init(struct super_block *);
2522 +extern void ext3_ext_release(struct super_block *);
2523 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2525 #endif /* __KERNEL__ */
2527 Index: linux-2.4.24/include/linux/ext3_extents.h
2528 ===================================================================
2529 --- linux-2.4.24.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2530 +++ linux-2.4.24/include/linux/ext3_extents.h 2004-01-29 01:13:10.000000000 +0300
2533 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
2535 + * This program is free software; you can redistribute it and/or modify
2536 + * it under the terms of the GNU General Public License version 2 as
2537 + * published by the Free Software Foundation.
2539 + * This program is distributed in the hope that it will be useful,
2540 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2541 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2542 + * GNU General Public License for more details.
2544 + * You should have received a copy of the GNU General Public Licens
2545 + * along with this program; if not, write to the Free Software
2546 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2549 +#ifndef _LINUX_EXT3_EXTENTS
2550 +#define _LINUX_EXT3_EXTENTS
2553 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2554 + * become very little, so index split, in-depth growing and
2555 + * other hard changes happens much more often
2556 + * this is for debug purposes only
2558 +#define AGRESSIVE_TEST_
2561 + * if CHECK_BINSEARCH defined, then results of binary search
2562 + * will be checked by linear search
2564 +#define CHECK_BINSEARCH_
2567 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2568 + * to get lots of info what's going on
2572 +#define ext_debug(tree,fmt,a...) \
2574 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2575 + printk(fmt, ##a); \
2578 +#define ext_debug(tree,fmt,a...)
2582 + * if EXT_STATS is defined then stats numbers are collected
2583 + * these number will be displayed at umount time
2588 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2591 + * ext3_inode has i_block array (total 60 bytes)
2592 + * first 4 bytes are used to store:
2593 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2594 + * - number of alive extents in the inode
2598 + * this is extent on-disk structure
2599 + * it's used at the bottom of the tree
2601 +struct ext3_extent {
2602 + __u32 e_block; /* first logical block extent covers */
2603 + __u32 e_start; /* first physical block extents lives */
2604 + __u32 e_num; /* number of blocks covered by extent */
2608 + * this is index on-disk structure
2609 + * it's used at all the levels, but the bottom
2611 +struct ext3_extent_idx {
2612 + __u32 e_block; /* index covers logical blocks from 'block' */
2613 + __u32 e_leaf; /* pointer to the physical block of the next *
2614 + * level. leaf or next index could bet here */
2618 + * each block (leaves and indexes), even inode-stored has header
2620 +struct ext3_extent_header {
2621 + __u16 e_magic; /* probably will support different formats */
2622 + __u16 e_num; /* number of valid entries */
2623 + __u16 e_max; /* capacity of store in entries */
2624 + __u16 e_depth; /* has tree real underlaying blocks? */
2625 + __u32 e_generation; /* generation of the tree */
2628 +#define EXT3_EXT_MAGIC 0xf301
2631 + * array of ext3_ext_path contains path to some extent
2632 + * creation/lookup routines use it for traversal/splitting/etc
2633 + * truncate uses it to simulate recursive walking
2635 +struct ext3_ext_path {
2638 + struct ext3_extent *p_ext;
2639 + struct ext3_extent_idx *p_idx;
2640 + struct ext3_extent_header *p_hdr;
2641 + struct buffer_head *p_bh;
2645 + * structure for external API
2650 + * ext3_extents_tree is used to pass initial information
2651 + * to top-level extents API
2653 +struct ext3_extents_tree {
2654 + struct inode *inode; /* inode which tree belongs to */
2655 + void *root; /* ptr to data top of tree resides at */
2656 + void *buffer; /* will be passed as arg to ^^ routines */
2659 + struct ext3_extent *cex;/* last found extent */
2660 + int (*get_write_access)(handle_t *h, void *buffer);
2661 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2662 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2663 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2664 + struct ext3_extent *, unsigned long,
2666 + int (*remove_extent)(struct ext3_extents_tree *,
2667 + struct ext3_extent *, unsigned long,
2669 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2670 + struct ext3_ext_path *, struct ext3_extent *,
2675 + * to be called by ext3_ext_walk_space()
2676 + * negative retcode - error
2677 + * positive retcode - signal for ext3_ext_walk_space(), see below
2678 + * callback must return valid extent (passed or newly created)
2680 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2681 + struct ext3_ext_path *,
2682 + struct ext3_extent *, int);
2684 +#define EXT_CONTINUE 0
2685 +#define EXT_BREAK 1
2686 +#define EXT_REPEAT 2
2689 +#define EXT_FIRST_EXTENT(__hdr__) \
2690 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2691 + sizeof(struct ext3_extent_header)))
2692 +#define EXT_FIRST_INDEX(__hdr__) \
2693 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2694 + sizeof(struct ext3_extent_header)))
2695 +#define EXT_HAS_FREE_INDEX(__path__) \
2696 + ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
2697 +#define EXT_LAST_EXTENT(__hdr__) \
2698 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
2699 +#define EXT_LAST_INDEX(__hdr__) \
2700 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
2701 +#define EXT_MAX_EXTENT(__hdr__) \
2702 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
2703 +#define EXT_MAX_INDEX(__hdr__) \
2704 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
2706 +#define EXT_ROOT_HDR(tree) \
2707 + ((struct ext3_extent_header *) (tree)->root)
2708 +#define EXT_BLOCK_HDR(bh) \
2709 + ((struct ext3_extent_header *) (bh)->b_data)
2710 +#define EXT_DEPTH(_t_) \
2711 + (((struct ext3_extent_header *)((_t_)->root))->e_depth)
2712 +#define EXT_GENERATION(_t_) \
2713 + (((struct ext3_extent_header *)((_t_)->root))->e_generation)
2716 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2720 + * this structure is used to gather extents from the tree via ioctl
2722 +struct ext3_extent_buf {
2723 + unsigned long start;
2731 + * this structure is used to collect stats info about the tree
2733 +struct ext3_extent_tree_stats {
2739 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2740 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2741 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2742 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2743 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2744 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2746 +#endif /* _LINUX_EXT3_EXTENTS */
2748 Index: linux-2.4.24/include/linux/ext3_fs_i.h
2749 ===================================================================
2750 --- linux-2.4.24.orig/include/linux/ext3_fs_i.h 2004-01-24 19:30:22.000000000 +0300
2751 +++ linux-2.4.24/include/linux/ext3_fs_i.h 2004-01-26 23:17:19.000000000 +0300
2753 * by other means, so we have truncate_sem.
2755 struct rw_semaphore truncate_sem;
2757 + __u32 i_cached_extent[3];
2760 #endif /* _LINUX_EXT3_FS_I */