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-02-05 20:31:18.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);
109 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
110 + struct ext3_ext_path *path)
114 + /* path points to block */
115 + err =ext3_journal_dirty_metadata(handle, path->p_bh);
117 + /* path points to leaf/index in inode body */
118 + err = ext3_ext_mark_root_dirty(handle, tree);
124 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
125 + struct ext3_ext_path *path, struct ext3_extent *ex,
128 + int goal, depth, newblock;
129 + struct inode *inode;
132 + if (tree->new_block)
133 + return tree->new_block(handle, tree, path, ex, err);
135 + inode = tree->inode;
136 + depth = EXT_DEPTH(tree);
137 + if (path && depth > 0) {
138 + goal = path[depth-1].p_block;
140 + struct ext3_inode_info *ei = EXT3_I(inode);
141 + unsigned long bg_start;
142 + unsigned long colour;
144 + bg_start = (ei->i_block_group *
145 + EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
146 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
147 + colour = (current->pid % 16) *
148 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
149 + goal = bg_start + colour;
152 + newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
156 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
158 + struct ext3_extent_header *neh;
159 + neh = EXT_ROOT_HDR(tree);
160 + neh->e_generation++;
163 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
167 + size = (tree->inode->i_sb->s_blocksize -
168 + sizeof(struct ext3_extent_header))
169 + / sizeof(struct ext3_extent);
170 +#ifdef AGRESSIVE_TEST
176 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
180 + size = (tree->inode->i_sb->s_blocksize -
181 + sizeof(struct ext3_extent_header))
182 + / sizeof(struct ext3_extent_idx);
183 +#ifdef AGRESSIVE_TEST
189 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
193 + size = (tree->buffer_len - sizeof(struct ext3_extent_header))
194 + / sizeof(struct ext3_extent);
195 +#ifdef AGRESSIVE_TEST
201 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
205 + size = (tree->buffer_len -
206 + sizeof(struct ext3_extent_header))
207 + / sizeof(struct ext3_extent_idx);
208 +#ifdef AGRESSIVE_TEST
214 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
215 + struct ext3_ext_path *path)
218 + int k, l = path->p_depth;
220 + ext_debug(tree, "path:");
221 + for (k = 0; k <= l; k++, path++) {
223 + ext_debug(tree, " %d->%d", path->p_idx->e_block,
224 + path->p_idx->e_leaf);
225 + } else if (path->p_ext) {
226 + ext_debug(tree, " %d:%d:%d",
227 + path->p_ext->e_block,
228 + path->p_ext->e_num,
229 + path->p_ext->e_start);
231 + ext_debug(tree, " []");
233 + ext_debug(tree, "\n");
237 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
238 + struct ext3_ext_path *path)
241 + int depth = EXT_DEPTH(tree);
242 + struct ext3_extent_header *eh;
243 + struct ext3_extent *ex;
249 + eh = path[depth].p_hdr;
250 + ex = EXT_FIRST_EXTENT(eh);
252 + for (i = 0; i < eh->e_num; i++, ex++) {
253 + ext_debug(tree, "%d:%d:%d ",
254 + ex->e_block, ex->e_num, ex->e_start);
256 + ext_debug(tree, "\n");
260 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
262 + int depth = path->p_depth;
265 + for (i = 0; i <= depth; i++, path++)
267 + brelse(path->p_bh);
273 + * binary search for closest index by given block
276 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
277 + struct ext3_ext_path *path, int block)
279 + struct ext3_extent_header *eh = path->p_hdr;
280 + struct ext3_extent_idx *ix;
283 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
284 + EXT_ASSERT(eh->e_num <= eh->e_max);
285 + EXT_ASSERT(eh->e_num > 0);
287 + ext_debug(tree, "binsearch for %d(idx): ", block);
289 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
294 + if (block < ix[l + k].e_block)
298 + ext_debug(tree, "%d:%d:%d ", k, l, r);
303 + ext_debug(tree, " -> %d->%d ", path->p_idx->e_block, path->p_idx->e_leaf);
306 + if (block < ix->e_block)
308 + path->p_idx = ix++;
310 + ext_debug(tree, " -> %d->%d\n", path->p_idx->e_block,
311 + path->p_idx->e_leaf);
313 +#ifdef CHECK_BINSEARCH
315 + struct ext3_extent_idx *chix;
317 + chix = ix = EXT_FIRST_INDEX(eh);
318 + for (k = 0; k < eh->e_num; k++, ix++) {
319 + if (k != 0 && ix->e_block <= ix[-1].e_block) {
320 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
321 + ix, EXT_FIRST_INDEX(eh));
322 + printk("%u <= %u\n",
323 + ix->e_block,ix[-1].e_block);
325 + EXT_ASSERT(k == 0 || ix->e_block > ix[-1].e_block);
326 + if (block < ix->e_block)
330 + EXT_ASSERT(chix == path->p_idx);
337 + * binary search for closest extent by given block
340 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
341 + struct ext3_ext_path *path, int block)
343 + struct ext3_extent_header *eh = path->p_hdr;
344 + struct ext3_extent *ex;
347 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
348 + EXT_ASSERT(eh->e_num <= eh->e_max);
350 + if (eh->e_num == 0) {
352 + * this leaf is empty yet:
353 + * we get such a leaf in split/add case
358 + ext_debug(tree, "binsearch for %d: ", block);
360 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
365 + if (block < ex[l + k].e_block)
369 + ext_debug(tree, "%d:%d:%d ", k, l, r);
374 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->e_block,
375 + path->p_ext->e_start, path->p_ext->e_num);
378 + if (block < ex->e_block)
380 + path->p_ext = ex++;
382 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->e_block,
383 + path->p_ext->e_start, path->p_ext->e_num);
385 +#ifdef CHECK_BINSEARCH
387 + struct ext3_extent *chex;
389 + chex = ex = EXT_FIRST_EXTENT(eh);
390 + for (k = 0; k < eh->e_num; k++, ex++) {
391 + EXT_ASSERT(k == 0 || ex->e_block > ex[-1].e_block);
392 + if (block < ex->e_block)
396 + EXT_ASSERT(chex == path->p_ext);
402 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
404 + struct ext3_extent_header *eh;
406 + BUG_ON(tree->buffer_len == 0);
407 + ext3_ext_get_access_for_root(handle, tree);
408 + eh = EXT_ROOT_HDR(tree);
411 + eh->e_magic = EXT3_EXT_MAGIC;
412 + eh->e_max = ext3_ext_space_root(tree);
413 + ext3_ext_mark_root_dirty(handle, tree);
417 +struct ext3_ext_path *
418 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
419 + struct ext3_ext_path *path)
421 + struct ext3_extent_header *eh;
422 + struct buffer_head *bh;
423 + int depth, i, ppos = 0;
426 + EXT_ASSERT(tree->inode);
427 + EXT_ASSERT(tree->root);
429 + eh = EXT_ROOT_HDR(tree);
430 + i = depth = EXT_DEPTH(tree);
431 + EXT_ASSERT(eh->e_max);
432 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
433 + EXT_ASSERT(i == 0 || eh->e_num > 0);
435 + /* account possible depth increase */
437 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
440 + return ERR_PTR(-ENOMEM);
442 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
443 + path[0].p_hdr = eh;
445 + /* walk through the tree */
447 + ext_debug(tree, "depth %d: num %d, max %d\n",
448 + ppos, eh->e_num, eh->e_max);
449 + ext3_ext_binsearch_idx(tree, path + ppos, block);
450 + path[ppos].p_block = path[ppos].p_idx->e_leaf;
451 + path[ppos].p_depth = i;
452 + path[ppos].p_ext = NULL;
454 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
456 + ext3_ext_drop_refs(path);
458 + return ERR_PTR(-EIO);
460 + eh = EXT_BLOCK_HDR(bh);
462 + EXT_ASSERT(ppos <= depth);
463 + path[ppos].p_bh = bh;
464 + path[ppos].p_hdr = eh;
468 + path[ppos].p_depth = i;
469 + path[ppos].p_hdr = eh;
470 + path[ppos].p_ext = NULL;
473 + ext3_ext_binsearch(tree, path + ppos, block);
475 + ext3_ext_show_path(tree, path);
481 + * insert new index [logical;ptr] into the block at cupr
482 + * it check where to insert: before curp or after curp
484 +static int ext3_ext_insert_index(handle_t *handle,
485 + struct ext3_extents_tree *tree,
486 + struct ext3_ext_path *curp,
487 + int logical, int ptr)
489 + struct ext3_extent_idx *ix;
492 + if ((err = ext3_ext_get_access(handle, tree, curp)))
495 + EXT_ASSERT(logical != curp->p_idx->e_block);
496 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
497 + if (logical > curp->p_idx->e_block) {
499 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
500 + len = (len - 1) * sizeof(struct ext3_extent_idx);
501 + len = len < 0 ? 0 : len;
502 + ext_debug(tree, "insert new index %d after: %d. "
503 + "move %d from 0x%p to 0x%p\n",
505 + (curp->p_idx + 1), (curp->p_idx + 2));
506 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
508 + ix = curp->p_idx + 1;
510 + /* insert before */
511 + len = len * sizeof(struct ext3_extent_idx);
512 + len = len < 0 ? 0 : len;
513 + ext_debug(tree, "insert new index %d before: %d. "
514 + "move %d from 0x%p to 0x%p\n",
516 + curp->p_idx, (curp->p_idx + 1));
517 + memmove(curp->p_idx + 1, curp->p_idx, len);
521 + ix->e_block = logical;
523 + curp->p_hdr->e_num++;
525 + EXT_ASSERT(curp->p_hdr->e_num <= curp->p_hdr->e_max);
526 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
528 + err = ext3_ext_dirty(handle, tree, curp);
529 + ext3_std_error(tree->inode->i_sb, err);
535 + * routine inserts new subtree into the path, using free index entry
537 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
538 + * - makes decision where to split
539 + * - moves remaining extens and index entries (right to the split point)
540 + * into the newly allocated blocks
541 + * - initialize subtree
543 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
544 + struct ext3_ext_path *path,
545 + struct ext3_extent *newext, int at)
547 + struct buffer_head *bh = NULL;
548 + int depth = EXT_DEPTH(tree);
549 + struct ext3_extent_header *neh;
550 + struct ext3_extent_idx *fidx;
551 + struct ext3_extent *ex;
552 + int i = at, k, m, a;
553 + unsigned long newblock, oldblock, border;
554 + int *ablocks = NULL; /* array of allocated blocks */
557 + /* make decision: where to split? */
558 + /* FIXME: now desicion is simplest: at current extent */
560 + /* if current leaf will be splitted, then we should use
561 + * border from split point */
562 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
563 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
564 + border = path[depth].p_ext[1].e_block;
565 + ext_debug(tree, "leaf will be splitted."
566 + " next leaf starts at %d\n",
569 + border = newext->e_block;
570 + ext_debug(tree, "leaf will be added."
571 + " next leaf starts at %d\n",
576 + * if error occurs, then we break processing
577 + * and turn filesystem read-only. so, index won't
578 + * be inserted and tree will be in consistent
579 + * state. next mount will repair buffers too
583 + * get array to track all allocated blocks
584 + * we need this to handle errors and free blocks
587 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
590 + memset(ablocks, 0, sizeof(unsigned long) * depth);
592 + /* allocate all needed blocks */
593 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
594 + for (a = 0; a < depth - at; a++) {
595 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
598 + ablocks[a] = newblock;
601 + /* initialize new leaf */
602 + newblock = ablocks[--a];
603 + EXT_ASSERT(newblock);
604 + bh = sb_getblk(tree->inode->i_sb, newblock);
611 + if ((err = ext3_journal_get_create_access(handle, bh)))
614 + neh = EXT_BLOCK_HDR(bh);
616 + neh->e_max = ext3_ext_space_block(tree);
617 + neh->e_magic = EXT3_EXT_MAGIC;
619 + ex = EXT_FIRST_EXTENT(neh);
621 + /* move remain of path[depth] to the new leaf */
622 + EXT_ASSERT(path[depth].p_hdr->e_num == path[depth].p_hdr->e_max);
623 + /* start copy from next extent */
624 + /* TODO: we could do it by single memmove */
626 + path[depth].p_ext++;
627 + while (path[depth].p_ext <=
628 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
629 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
630 + path[depth].p_ext->e_block,
631 + path[depth].p_ext->e_start,
632 + path[depth].p_ext->e_num,
634 + memmove(ex++, path[depth].p_ext++,
635 + sizeof(struct ext3_extent));
639 + mark_buffer_uptodate(bh, 1);
642 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
647 + /* correct old leaf */
649 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
651 + path[depth].p_hdr->e_num -= m;
652 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
657 + /* create intermediate indexes */
658 + k = depth - at - 1;
659 + EXT_ASSERT(k >= 0);
661 + ext_debug(tree, "create %d intermediate indices\n", k);
662 + /* insert new index into current index block */
663 + /* current depth stored in i var */
666 + oldblock = newblock;
667 + newblock = ablocks[--a];
668 + bh = sb_getblk(tree->inode->i_sb, newblock);
675 + if ((err = ext3_journal_get_create_access(handle, bh)))
678 + neh = EXT_BLOCK_HDR(bh);
680 + neh->e_magic = EXT3_EXT_MAGIC;
681 + neh->e_max = ext3_ext_space_block_idx(tree);
682 + neh->e_depth = depth - i;
683 + fidx = EXT_FIRST_INDEX(neh);
684 + fidx->e_block = border;
685 + fidx->e_leaf = oldblock;
687 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
688 + i, newblock, border, oldblock);
693 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
694 + EXT_MAX_INDEX(path[i].p_hdr));
695 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
696 + EXT_LAST_INDEX(path[i].p_hdr));
697 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
698 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
699 + i, path[i].p_idx->e_block,
700 + path[i].p_idx->e_leaf, newblock);
701 + memmove(++fidx, path[i].p_idx++,
702 + sizeof(struct ext3_extent_idx));
704 + EXT_ASSERT(neh->e_num <= neh->e_max);
707 + mark_buffer_uptodate(bh, 1);
710 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
715 + /* correct old index */
717 + err = ext3_ext_get_access(handle, tree, path + i);
720 + path[i].p_hdr->e_num -= m;
721 + err = ext3_ext_dirty(handle, tree, path + i);
729 + /* insert new index */
731 + err = ext3_ext_insert_index(handle, tree, path + at,
736 + if (buffer_locked(bh))
742 + /* free all allocated blocks in error case */
743 + for (i = 0; i < depth; i++)
746 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
754 + * routine implements tree growing procedure:
755 + * - allocates new block
756 + * - moves top-level data (index block or leaf) into the new block
757 + * - initialize new top-level, creating index that points to the
758 + * just created block
760 +static int ext3_ext_grow_indepth(handle_t *handle,
761 + struct ext3_extents_tree *tree,
762 + struct ext3_ext_path *path,
763 + struct ext3_extent *newext)
765 + struct ext3_ext_path *curp = path;
766 + struct ext3_extent_header *neh;
767 + struct ext3_extent_idx *fidx;
768 + struct buffer_head *bh;
769 + unsigned long newblock;
772 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
776 + bh = sb_getblk(tree->inode->i_sb, newblock);
779 + ext3_std_error(tree->inode->i_sb, err);
784 + if ((err = ext3_journal_get_create_access(handle, bh))) {
789 + /* move top-level index/leaf into new block */
790 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
792 + /* set size of new block */
793 + neh = EXT_BLOCK_HDR(bh);
794 + /* old root could have indexes or leaves
795 + * so calculate e_max right way */
796 + if (EXT_DEPTH(tree))
797 + neh->e_max = ext3_ext_space_block_idx(tree);
799 + neh->e_max = ext3_ext_space_block(tree);
800 + neh->e_magic = EXT3_EXT_MAGIC;
801 + mark_buffer_uptodate(bh, 1);
804 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
807 + /* create index in new top-level index: num,max,pointer */
808 + if ((err = ext3_ext_get_access(handle, tree, curp)))
811 + curp->p_hdr->e_magic = EXT3_EXT_MAGIC;
812 + curp->p_hdr->e_max = ext3_ext_space_root_idx(tree);
813 + curp->p_hdr->e_num = 1;
814 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
815 + /* FIXME: it works, but actually path[0] can be index */
816 + curp->p_idx->e_block = EXT_FIRST_EXTENT(path[0].p_hdr)->e_block;
817 + curp->p_idx->e_leaf = newblock;
819 + neh = EXT_ROOT_HDR(tree);
820 + fidx = EXT_FIRST_INDEX(neh);
821 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
822 + neh->e_num, neh->e_max, fidx->e_block, fidx->e_leaf);
824 + neh->e_depth = path->p_depth + 1;
825 + err = ext3_ext_dirty(handle, tree, curp);
833 + * routine finds empty index and adds new leaf. if no free index found
834 + * then it requests in-depth growing
836 +static int ext3_ext_create_new_leaf(handle_t *handle,
837 + struct ext3_extents_tree *tree,
838 + struct ext3_ext_path *path,
839 + struct ext3_extent *newext)
841 + struct ext3_ext_path *curp;
842 + int depth, i, err = 0;
845 + i = depth = EXT_DEPTH(tree);
847 + /* walk up to the tree and look for free index entry */
848 + curp = path + depth;
849 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
854 + /* we use already allocated block for index block
855 + * so, subsequent data blocks should be contigoues */
856 + if (EXT_HAS_FREE_INDEX(curp)) {
857 + /* if we found index with free entry, then use that
858 + * entry: create all needed subtree and add new leaf */
859 + err = ext3_ext_split(handle, tree, path, newext, i);
862 + ext3_ext_drop_refs(path);
863 + path = ext3_ext_find_extent(tree, newext->e_block, path);
865 + err = PTR_ERR(path);
867 + /* tree is full, time to grow in depth */
868 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
871 + ext3_ext_drop_refs(path);
872 + path = ext3_ext_find_extent(tree, newext->e_block, path);
874 + err = PTR_ERR(path);
877 + * only first (depth 0 -> 1) produces free space
878 + * in all other cases we have to split growed tree
880 + depth = EXT_DEPTH(tree);
881 + if (path[depth].p_hdr->e_num == path[depth].p_hdr->e_max) {
882 + /* now we need split */
894 + * returns allocated block in subsequent extent or 0xffffffff
895 + * NOTE: it consider block number from index entry as
896 + * allocated block. thus, index entries have to be consistent
899 +static unsigned long
900 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
904 + EXT_ASSERT(path != NULL);
905 + depth = path->p_depth;
907 + if (depth == 0 && path->p_ext == NULL)
910 + /* FIXME: what if index isn't full ?! */
911 + while (depth >= 0) {
912 + if (depth == path->p_depth) {
914 + if (path[depth].p_ext !=
915 + EXT_LAST_EXTENT(path[depth].p_hdr))
916 + return path[depth].p_ext[1].e_block;
919 + if (path[depth].p_idx !=
920 + EXT_LAST_INDEX(path[depth].p_hdr))
921 + return path[depth].p_idx[1].e_block;
930 + * returns first allocated block from next leaf or 0xffffffff
932 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
933 + struct ext3_ext_path *path)
937 + EXT_ASSERT(path != NULL);
938 + depth = path->p_depth;
940 + /* zero-tree has no leaf blocks at all */
944 + /* go to index block */
947 + while (depth >= 0) {
948 + if (path[depth].p_idx !=
949 + EXT_LAST_INDEX(path[depth].p_hdr))
950 + return path[depth].p_idx[1].e_block;
958 + * if leaf gets modified and modified extent is first in the leaf
959 + * then we have to correct all indexes above
960 + * TODO: do we need to correct tree in all cases?
962 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
963 + struct ext3_ext_path *path)
965 + struct ext3_extent_header *eh;
966 + int depth = EXT_DEPTH(tree);
967 + struct ext3_extent *ex;
968 + unsigned long border;
971 + eh = path[depth].p_hdr;
972 + ex = path[depth].p_ext;
977 + /* there is no tree at all */
981 + if (ex != EXT_FIRST_EXTENT(eh)) {
982 + /* we correct tree if first leaf got modified only */
987 + * TODO: we need correction if border is smaller then current one
990 + border = path[depth].p_ext->e_block;
991 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
993 + path[k].p_idx->e_block = border;
994 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
998 + /* change all left-side indexes */
999 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1001 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1003 + path[k].p_idx->e_block = border;
1004 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1012 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1013 + struct ext3_extent *ex1,
1014 + struct ext3_extent *ex2)
1016 + if (ex1->e_block + ex1->e_num != ex2->e_block)
1019 +#ifdef AGRESSIVE_TEST
1020 + if (ex1->e_num >= 4)
1024 + if (!tree->mergable)
1027 + return tree->mergable(ex1, ex2);
1031 + * this routine tries to merge requsted extent into the existing
1032 + * extent or inserts requested extent as new one into the tree,
1033 + * creating new leaf in no-space case
1035 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1036 + struct ext3_ext_path *path,
1037 + struct ext3_extent *newext)
1039 + struct ext3_extent_header * eh;
1040 + struct ext3_extent *ex, *fex;
1041 + struct ext3_extent *nearex; /* nearest extent */
1042 + struct ext3_ext_path *npath = NULL;
1043 + int depth, len, err, next;
1045 + depth = EXT_DEPTH(tree);
1046 + ex = path[depth].p_ext;
1048 + /* try to insert block into found extent and return */
1049 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1050 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1051 + newext->e_num, ex->e_block, ex->e_num,
1053 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1055 + ex->e_num += newext->e_num;
1056 + eh = path[depth].p_hdr;
1062 + depth = EXT_DEPTH(tree);
1063 + eh = path[depth].p_hdr;
1064 + if (eh->e_num < eh->e_max)
1067 + /* probably next leaf has space for us? */
1068 + fex = EXT_LAST_EXTENT(eh);
1069 + next = ext3_ext_next_leaf_block(tree, path);
1070 + if (newext->e_block > fex->e_block && next != 0xffffffff) {
1071 + ext_debug(tree, "next leaf block - %d\n", next);
1072 + EXT_ASSERT(!npath);
1073 + npath = ext3_ext_find_extent(tree, next, NULL);
1074 + if (IS_ERR(npath))
1075 + return PTR_ERR(npath);
1076 + EXT_ASSERT(npath->p_depth == path->p_depth);
1077 + eh = npath[depth].p_hdr;
1078 + if (eh->e_num < eh->e_max) {
1079 + ext_debug(tree, "next leaf isnt full(%d)\n",
1084 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1085 + eh->e_num, eh->e_max);
1089 + * there is no free space in found leaf
1090 + * we're gonna add new leaf in the tree
1092 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1095 + depth = EXT_DEPTH(tree);
1096 + eh = path[depth].p_hdr;
1099 + nearex = path[depth].p_ext;
1101 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1105 + /* there is no extent in this leaf, create first one */
1106 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1107 + newext->e_block, newext->e_start,
1109 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1110 + } else if (newext->e_block > nearex->e_block) {
1111 + EXT_ASSERT(newext->e_block != nearex->e_block);
1112 + if (nearex != EXT_LAST_EXTENT(eh)) {
1113 + len = EXT_MAX_EXTENT(eh) - nearex;
1114 + len = (len - 1) * sizeof(struct ext3_extent);
1115 + len = len < 0 ? 0 : len;
1116 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1117 + "move %d from 0x%p to 0x%p\n",
1118 + newext->e_block, newext->e_start,
1120 + nearex, len, nearex + 1, nearex + 2);
1121 + memmove(nearex + 2, nearex + 1, len);
1123 + path[depth].p_ext = nearex + 1;
1125 + EXT_ASSERT(newext->e_block != nearex->e_block);
1126 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1127 + len = len < 0 ? 0 : len;
1128 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1129 + "move %d from 0x%p to 0x%p\n",
1130 + newext->e_block, newext->e_start, newext->e_num,
1131 + nearex, len, nearex + 1, nearex + 2);
1132 + memmove(nearex + 1, nearex, len);
1133 + path[depth].p_ext = nearex;
1137 + nearex = path[depth].p_ext;
1138 + nearex->e_block = newext->e_block;
1139 + nearex->e_start = newext->e_start;
1140 + nearex->e_num = newext->e_num;
1143 + /* try to merge extents to the right */
1144 + while (nearex < EXT_LAST_EXTENT(eh)) {
1145 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1147 + /* merge with next extent! */
1148 + nearex->e_num += nearex[1].e_num;
1149 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1150 + len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1151 + * sizeof(struct ext3_extent);
1152 + memmove(nearex + 1, nearex + 2, len);
1155 + EXT_ASSERT(eh->e_num > 0);
1158 + /* try to merge extents to the left */
1160 + /* time to correct all indexes above */
1161 + err = ext3_ext_correct_indexes(handle, tree, path);
1165 + err = ext3_ext_dirty(handle, tree, path + depth);
1169 + ext3_ext_drop_refs(npath);
1172 + ext3_ext_tree_changed(tree);
1176 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1177 + unsigned long num, ext_prepare_callback func)
1179 + struct ext3_ext_path *path = NULL;
1180 + struct ext3_extent *ex, cbex;
1181 + unsigned long next, start = 0, end = 0;
1182 + unsigned long last = block + num;
1183 + int depth, exists, err = 0;
1187 + EXT_ASSERT(tree->inode);
1188 + EXT_ASSERT(tree->root);
1190 + while (block < last && block != 0xfffffffff) {
1191 + num = last - block;
1192 + /* find extent for this block */
1193 + path = ext3_ext_find_extent(tree, block, path);
1194 + if (IS_ERR(path)) {
1195 + err = PTR_ERR(path);
1200 + depth = EXT_DEPTH(tree);
1201 + ex = path[depth].p_ext;
1202 + next = ext3_ext_next_allocated_block(path);
1206 + /* there is no extent yet, so try to allocate
1207 + * all requested space */
1209 + end = block + num;
1210 + } else if (ex->e_block > block) {
1211 + /* need to allocate space before found extent */
1213 + end = ex->e_block;
1214 + if (block + num < end)
1215 + end = block + num;
1216 + } else if (block >= ex->e_block + ex->e_num) {
1217 + /* need to allocate space after found extent */
1219 + end = block + num;
1222 + } else if (block >= ex->e_block) {
1224 + * some part of requested space is covered
1228 + end = ex->e_block + ex->e_num;
1229 + if (block + num < end)
1230 + end = block + num;
1235 + EXT_ASSERT(end > start);
1238 + cbex.e_block = start;
1239 + cbex.e_num = end - start;
1244 + err = func(tree, path, &cbex, exists);
1245 + ext3_ext_drop_refs(path);
1249 + if (err == EXT_REPEAT)
1251 + else if (err == EXT_BREAK) {
1256 + if (EXT_DEPTH(tree) != depth) {
1257 + /* depth was changed. we have to realloc path */
1262 + block = cbex.e_block + cbex.e_num;
1266 + ext3_ext_drop_refs(path);
1274 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
1277 + tree->cex->e_num = 0;
1281 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, struct ext3_extent *ex)
1285 + EXT_ASSERT(ex->e_num);
1286 + tree->cex->e_block = ex->e_block;
1287 + tree->cex->e_start = ex->e_start;
1288 + tree->cex->e_num = ex->e_num;
1293 + * this routine calculate boundaries of the gap requested block fits into
1294 + * and cache this gap
1297 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1298 + struct ext3_ext_path *path,
1299 + unsigned long block)
1301 + int depth = EXT_DEPTH(tree);
1302 + struct ext3_extent *ex, gex;
1307 + ex = path[depth].p_ext;
1309 + /* there is no extent yet, so gap is [0;-] */
1311 + gex.e_num = 0xffffffff;
1312 + ext_debug(tree, "cache gap(whole file):");
1313 + } else if (block < ex->e_block) {
1314 + gex.e_block = block;
1315 + gex.e_num = ex->e_block - block;
1316 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1317 + (unsigned long) block,
1318 + (unsigned long) ex->e_block,
1319 + (unsigned long) ex->e_num);
1320 + } else if (block >= ex->e_block + ex->e_num) {
1321 + gex.e_block = ex->e_block + ex->e_num;
1322 + gex.e_num = ext3_ext_next_allocated_block(path);
1323 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1324 + (unsigned long) ex->e_block,
1325 + (unsigned long) ex->e_num,
1326 + (unsigned long) block);
1327 + EXT_ASSERT(gex.e_num > gex.e_block);
1328 + gex.e_num = gex.e_num - gex.e_block;
1333 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.e_block,
1334 + (unsigned long) gex.e_num);
1335 + gex.e_start = 0xffffffff;
1336 + ext3_ext_put_in_cache(tree, &gex);
1340 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1341 + struct ext3_extent *ex)
1343 + struct ext3_extent *cex = tree->cex;
1345 + /* is there cache storage at all? */
1349 + /* has cache valid data? */
1350 + if (cex->e_num == 0)
1353 + if (block >= cex->e_block && block < cex->e_block + cex->e_num) {
1354 + ex->e_block = cex->e_block;
1355 + ex->e_start = cex->e_start;
1356 + ex->e_num = cex->e_num;
1357 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1358 + (unsigned long) block,
1359 + (unsigned long) ex->e_block,
1360 + (unsigned long) ex->e_num,
1361 + (unsigned long) ex->e_start);
1365 + /* not in cache */
1370 + * routine removes index from the index block
1371 + * it's used in truncate case only. thus all requests are for
1372 + * last index in the block only
1374 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1375 + struct ext3_ext_path *path)
1377 + struct buffer_head *bh;
1380 + /* free index block */
1382 + EXT_ASSERT(path->p_hdr->e_num);
1383 + if ((err = ext3_ext_get_access(handle, tree, path)))
1385 + path->p_hdr->e_num--;
1386 + if ((err = ext3_ext_dirty(handle, tree, path)))
1388 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1389 + path->p_idx->e_leaf);
1390 + bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->e_leaf);
1391 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->e_leaf);
1392 + ext3_free_blocks(handle, tree->inode, path->p_idx->e_leaf, 1);
1396 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1397 + struct ext3_ext_path *path)
1399 + int depth = EXT_DEPTH(tree);
1403 + /* probably there is space in leaf? */
1404 + if (path[depth].p_hdr->e_num < path[depth].p_hdr->e_max)
1409 + * the worste case we're expecting is creation of the
1410 + * new root (growing in depth) with index splitting
1411 + * for splitting we have to consider depth + 1 because
1412 + * previous growing could increase it
1414 + depth = depth + 1;
1417 + * growing in depth:
1418 + * block allocation + new root + old root
1420 + needed = EXT3_ALLOC_NEEDED + 2;
1422 + /* index split. we may need:
1423 + * allocate intermediate indexes and new leaf
1424 + * change two blocks at each level, but root
1425 + * modify root block (inode)
1427 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1433 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1434 + struct ext3_ext_path *path, unsigned long start,
1435 + unsigned long end)
1437 + struct ext3_extent *ex, tex;
1438 + struct ext3_ext_path *npath;
1439 + int depth, creds, err;
1441 + depth = EXT_DEPTH(tree);
1442 + ex = path[depth].p_ext;
1444 + EXT_ASSERT(end < ex->e_block + ex->e_num - 1);
1445 + EXT_ASSERT(ex->e_block < start);
1447 + /* calculate tail extent */
1448 + tex.e_block = end + 1;
1449 + EXT_ASSERT(tex.e_block < ex->e_block + ex->e_num);
1450 + tex.e_num = ex->e_block + ex->e_num - tex.e_block;
1452 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1453 + handle = ext3_ext_journal_restart(handle, creds);
1454 + if (IS_ERR(handle))
1455 + return PTR_ERR(handle);
1457 + /* calculate head extent. use primary extent */
1458 + err = ext3_ext_get_access(handle, tree, path + depth);
1461 + ex->e_num = start - ex->e_block;
1462 + err = ext3_ext_dirty(handle, tree, path + depth);
1466 + /* FIXME: some callback to free underlying resource
1467 + * and correct e_start? */
1468 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1469 + ex->e_block, ex->e_num, tex.e_block, tex.e_num);
1471 + npath = ext3_ext_find_extent(tree, ex->e_block, NULL);
1472 + if (IS_ERR(npath))
1473 + return PTR_ERR(npath);
1474 + depth = EXT_DEPTH(tree);
1475 + EXT_ASSERT(npath[depth].p_ext->e_block == ex->e_block);
1476 + EXT_ASSERT(npath[depth].p_ext->e_num == ex->e_num);
1478 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1479 + ext3_ext_drop_refs(npath);
1487 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1488 + struct ext3_ext_path *path, unsigned long start,
1489 + unsigned long end)
1491 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1492 + int err = 0, correct_index = 0;
1493 + int depth = EXT_DEPTH(tree), credits;
1494 + struct ext3_extent_header *eh;
1495 + unsigned a, b, block, num;
1497 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1498 + if (!path[depth].p_hdr)
1499 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1500 + eh = path[depth].p_hdr;
1502 + EXT_ASSERT(eh->e_num <= eh->e_max);
1503 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
1505 + /* find where to start removing */
1506 + le = ex = EXT_LAST_EXTENT(eh);
1507 + while (ex != EXT_FIRST_EXTENT(eh)) {
1508 + if (ex->e_block <= end)
1513 + if (start > ex->e_block && end < ex->e_block + ex->e_num - 1) {
1514 + /* removal of internal part of the extent requested
1515 + * tail and head must be placed in different extent
1516 + * so, we have to insert one more extent */
1517 + path[depth].p_ext = ex;
1518 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1522 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1523 + ex->e_block + ex->e_num > start) {
1524 + ext_debug(tree, "remove ext %u:%u\n", ex->e_block, ex->e_num);
1525 + path[depth].p_ext = ex;
1527 + a = ex->e_block > start ? ex->e_block : start;
1528 + b = ex->e_block + ex->e_num - 1 < end ?
1529 + ex->e_block + ex->e_num - 1 : end;
1531 + ext_debug(tree, " border %u:%u\n", a, b);
1533 + if (a != ex->e_block && b != ex->e_block + ex->e_num - 1) {
1537 + } else if (a != ex->e_block) {
1538 + /* remove tail of the extent */
1539 + block = ex->e_block;
1541 + } else if (b != ex->e_block + ex->e_num - 1) {
1542 + /* remove head of the extent */
1546 + /* remove whole extent: excelent! */
1547 + block = ex->e_block;
1549 + EXT_ASSERT(a == ex->e_block &&
1550 + b == ex->e_block + ex->e_num - 1);
1553 + if (ex == EXT_FIRST_EXTENT(eh))
1554 + correct_index = 1;
1557 + if (correct_index)
1558 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1559 + if (tree->remove_extent_credits)
1560 + credits += tree->remove_extent_credits(tree, ex, a, b);
1562 + handle = ext3_ext_journal_restart(handle, credits);
1563 + if (IS_ERR(handle)) {
1564 + err = PTR_ERR(handle);
1568 + err = ext3_ext_get_access(handle, tree, path + depth);
1572 + if (tree->remove_extent)
1573 + err = tree->remove_extent(tree, ex, a, b);
1578 + /* this extent is removed entirely mark slot unused */
1584 + ex->e_block = block;
1587 + err = ext3_ext_dirty(handle, tree, path + depth);
1591 + ext_debug(tree, "new extent: %u:%u:%u\n",
1592 + ex->e_block, ex->e_num, ex->e_start);
1597 + /* reuse unused slots */
1599 + if (lu->e_start) {
1608 + if (correct_index && eh->e_num)
1609 + err = ext3_ext_correct_indexes(handle, tree, path);
1611 + /* if this leaf is free, then we should
1612 + * remove it from index block above */
1613 + if (err == 0 && eh->e_num == 0 && path[depth].p_bh != NULL)
1614 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1621 +static struct ext3_extent_idx *
1622 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1624 + struct ext3_extent_idx *ix;
1626 + ix = EXT_LAST_INDEX(hdr);
1627 + while (ix != EXT_FIRST_INDEX(hdr)) {
1628 + if (ix->e_block <= block)
1636 + * returns 1 if current index have to be freed (even partial)
1639 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1641 + EXT_ASSERT(path->p_idx);
1643 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1647 + * if truncate on deeper level happened it it wasn't partial
1648 + * so we have to consider current index for truncation
1650 + if (path->p_hdr->e_num == path->p_block)
1655 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1656 + unsigned long start, unsigned long end)
1658 + struct inode *inode = tree->inode;
1659 + struct super_block *sb = inode->i_sb;
1660 + int depth = EXT_DEPTH(tree);
1661 + struct ext3_ext_path *path;
1663 + int i = 0, err = 0;
1665 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1667 + /* probably first extent we're gonna free will be last in block */
1668 + handle = ext3_journal_start(inode, depth + 1);
1669 + if (IS_ERR(handle))
1670 + return PTR_ERR(handle);
1672 + ext3_ext_invalidate_cache(tree);
1675 + * we start scanning from right side freeing all the blocks
1676 + * after i_size and walking into the deep
1678 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1679 + if (IS_ERR(path)) {
1680 + ext3_error(sb, "ext3_ext_remove_space",
1681 + "Can't allocate path array");
1682 + ext3_journal_stop(handle, inode);
1685 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1686 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1688 + while (i >= 0 && err == 0) {
1690 + /* this is leaf block */
1691 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1692 + /* root level have p_bh == NULL, brelse() eats this */
1693 + brelse(path[i].p_bh);
1698 + /* this is index block */
1699 + if (!path[i].p_hdr) {
1700 + ext_debug(tree, "initialize header\n");
1701 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1704 + EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1705 + EXT_ASSERT(path[i].p_hdr->e_magic == EXT3_EXT_MAGIC);
1707 + if (!path[i].p_idx) {
1708 + /* this level hasn't touched yet */
1710 + ext3_ext_last_covered(path[i].p_hdr, end);
1711 + path[i].p_block = path[i].p_hdr->e_num + 1;
1712 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1713 + path[i].p_hdr, path[i].p_hdr->e_num);
1715 + /* we've already was here, see at next index */
1719 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1720 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1722 + if (ext3_ext_more_to_rm(path + i)) {
1723 + /* go to the next level */
1724 + ext_debug(tree, "move to level %d (block %d)\n",
1725 + i + 1, path[i].p_idx->e_leaf);
1726 + memset(path + i + 1, 0, sizeof(*path));
1727 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->e_leaf);
1728 + if (!path[i+1].p_bh) {
1729 + /* should we reset i_size? */
1733 + /* put actual number of indexes to know is this
1734 + * number got changed at the next iteration */
1735 + path[i].p_block = path[i].p_hdr->e_num;
1738 + /* we finish processing this index, go up */
1739 + if (path[i].p_hdr->e_num == 0 && i > 0) {
1740 + /* index is empty, remove it
1741 + * handle must be already prepared by the
1742 + * truncate_leaf() */
1743 + err = ext3_ext_rm_idx(handle, tree, path + i);
1745 + /* root level have p_bh == NULL, brelse() eats this */
1746 + brelse(path[i].p_bh);
1748 + ext_debug(tree, "return to level %d\n", i);
1752 + /* TODO: flexible tree reduction should be here */
1753 + if (path->p_hdr->e_num == 0) {
1755 + * truncate to zero freed all the tree
1756 + * so, we need to correct e_depth
1758 + err = ext3_ext_get_access(handle, tree, path);
1760 + EXT_ROOT_HDR(tree)->e_depth = 0;
1761 + err = ext3_ext_dirty(handle, tree, path);
1764 + ext3_ext_tree_changed(tree);
1767 + ext3_journal_stop(handle, inode);
1773 + * called at mount time
1775 +void ext3_ext_init(struct super_block *sb)
1778 + * possible initialization would be here
1781 + if (test_opt(sb, EXTENTS)) {
1782 + printk("EXT3-fs: file extents enabled");
1783 +#ifdef AGRESSIVE_TEST
1784 + printk(", agressive tests");
1786 +#ifdef CHECK_BINSEARCH
1787 + printk(", check binsearch");
1794 + * called at umount time
1796 +void ext3_ext_release(struct super_block *sb)
1800 +/************************************************************************
1801 + * VFS related routines
1802 + ************************************************************************/
1804 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1806 + /* we use in-core data, not bh */
1810 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1812 + struct inode *inode = buffer;
1813 + return ext3_mark_inode_dirty(handle, inode);
1816 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1817 + struct ext3_extent *ex2)
1819 + if (ex1->e_start + ex1->e_num == ex2->e_start)
1825 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1826 + struct ext3_extent *ex,
1827 + unsigned long from, unsigned long to)
1831 + /* at present, extent can't cross block group */;
1832 + needed = 3; /* bitmap + group desc + sb */
1834 +#ifdef CONFIG_QUOTA
1835 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1841 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1842 + struct ext3_extent *ex,
1843 + unsigned long from, unsigned long to)
1845 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1846 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1847 + struct buffer_head *bh;
1850 + if (IS_ERR(handle))
1851 + return PTR_ERR(handle);
1852 + if (from >= ex->e_block && to == ex->e_block + ex->e_num - 1) {
1853 + /* tail removal */
1854 + unsigned long num, start;
1855 + num = ex->e_block + ex->e_num - from;
1856 + start = ex->e_start + ex->e_num - num;
1857 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1859 + for (i = 0; i < num; i++) {
1860 + bh = sb_get_hash_table(tree->inode->i_sb, start + i);
1861 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1863 + ext3_free_blocks(handle, tree->inode, start, num);
1864 + } else if (from == ex->e_block && to <= ex->e_block + ex->e_num - 1) {
1865 + printk("strange request: removal %lu-%lu from %u:%u\n",
1866 + from, to, ex->e_block, ex->e_num);
1868 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1869 + from, to, ex->e_block, ex->e_num);
1871 + ext3_journal_stop(handle, tree->inode);
1875 +static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
1876 + unsigned long block)
1878 + struct ext3_inode_info *ei = EXT3_I(inode);
1879 + unsigned long bg_start;
1880 + unsigned long colour;
1884 + struct ext3_extent *ex;
1885 + depth = path->p_depth;
1887 + /* try to predict block placement */
1888 + if ((ex = path[depth].p_ext))
1889 + return ex->e_start + (block - ex->e_block);
1891 + /* it looks index is empty
1892 + * try to find starting from index itself */
1893 + if (path[depth].p_bh)
1894 + return path[depth].p_bh->b_blocknr;
1897 + /* OK. use inode's group */
1898 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1899 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1900 + colour = (current->pid % 16) *
1901 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1902 + return bg_start + colour + block;
1905 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1906 + struct ext3_ext_path *path,
1907 + struct ext3_extent *ex, int *err)
1909 + struct inode *inode = tree->inode;
1910 + int newblock, goal;
1914 + EXT_ASSERT(ex->e_start);
1915 + EXT_ASSERT(ex->e_num);
1917 + /* reuse block from the extent to order data/metadata */
1918 + newblock = ex->e_start++;
1920 + if (ex->e_num == 0) {
1922 + /* allocate new block for the extent */
1923 + goal = ext3_ext_find_goal(inode, path, ex->e_block);
1924 + ex->e_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1925 + if (ex->e_start == 0) {
1926 + /* error occured: restore old extent */
1927 + ex->e_start = newblock;
1934 +static void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1935 + struct inode *inode)
1937 + tree->inode = inode;
1938 + tree->root = (void *) EXT3_I(inode)->i_data;
1939 + tree->get_write_access = ext3_get_inode_write_access;
1940 + tree->mark_buffer_dirty = ext3_mark_buffer_dirty;
1941 + tree->mergable = ext3_ext_mergable;
1942 + tree->new_block = ext3_new_block_cb;
1943 + tree->remove_extent = ext3_remove_blocks;
1944 + tree->remove_extent_credits = ext3_remove_blocks_credits;
1945 + tree->buffer = (void *) inode;
1946 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1947 + tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1950 +#if EXT3_MULTIBLOCK_ALLOCATOR
1952 +ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
1953 + struct ext3_ext_path *path,
1954 + struct ext3_extent *newex, int exist)
1956 + struct inode *inode = tree->inode;
1957 + struct buffer_head *bh;
1958 + int count, err, goal;
1959 + unsigned long pblock;
1960 + unsigned long tgen;
1961 + loff_t new_i_size;
1966 + return EXT_CONTINUE;
1968 + tgen = EXT_GENERATION(tree);
1969 + count = ext3_ext_calc_credits_for_insert(tree, path);
1970 + up_write(&EXT3_I(inode)->truncate_sem);
1972 + handle = ext3_journal_start(inode, count + EXT3_ALLOC_NEEDED + 1);
1973 + if (IS_ERR(handle)) {
1974 + down_write(&EXT3_I(inode)->truncate_sem);
1975 + return PTR_ERR(handle);
1978 + if (tgen != EXT_GENERATION(tree)) {
1979 + /* the tree has changed. so path can be invalid at moment */
1980 + ext3_journal_stop(handle, inode);
1981 + down_write(&EXT3_I(inode)->truncate_sem);
1982 + return EXT_REPEAT;
1985 + down_write(&EXT3_I(inode)->truncate_sem);
1986 + goal = ext3_ext_find_goal(inode, path, newex->e_block);
1987 + count = newex->e_num;
1988 + pblock = ext3_new_blocks(handle, inode, &count, goal, &err);
1991 + EXT_ASSERT(count <= newex->e_num);
1993 + /* insert new extent */
1994 + newex->e_start = pblock;
1995 + newex->e_num = count;
1996 + err = ext3_ext_insert_extent(handle, tree, path, newex);
2000 + /* block have been allocated for data, so time to drop dirty
2001 + * in correspondend buffer_heads to prevent corruptions */
2002 + for (i = 0; i < newex->e_num; i++) {
2003 + bh = sb_get_hash_table(inode->i_sb, newex->e_start + i);
2005 + mark_buffer_clean(bh);
2006 + wait_on_buffer(bh);
2007 + clear_bit(BH_Req, &bh->b_state);
2012 + /* correct on-disk inode size */
2013 + if (newex->e_num > 0) {
2014 + new_i_size = (loff_t) newex->e_block + newex->e_num;
2015 + new_i_size = new_i_size << inode->i_blkbits;
2016 + if (new_i_size > EXT3_I(inode)->i_disksize) {
2017 + EXT3_I(inode)->i_disksize = new_i_size;
2018 + err = ext3_mark_inode_dirty(handle, inode);
2023 + ext3_journal_stop(handle, inode);
2028 +int ext3_ext_allocate_nblocks(struct inode *inode, unsigned long block,
2029 + unsigned long num)
2031 + struct ext3_extents_tree tree;
2034 + ext3_init_tree_desc(&tree, inode);
2035 + ext_debug(&tree, "blocks %lu-%lu requested for inode %u\n",
2036 + block, block + num,(unsigned) inode->i_ino);
2037 + down_write(&EXT3_I(inode)->truncate_sem);
2038 + err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
2039 + ext3_ext_invalidate_cache(&tree);
2040 + up_write(&EXT3_I(inode)->truncate_sem);
2046 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2047 + long iblock, struct buffer_head *bh_result, int create)
2049 + struct ext3_ext_path *path = NULL;
2050 + struct ext3_extent newex;
2051 + struct ext3_extent *ex;
2052 + int goal, newblock, err = 0, depth;
2053 + struct ext3_extents_tree tree;
2055 + clear_bit(BH_New, &bh_result->b_state);
2056 + ext3_init_tree_desc(&tree, inode);
2057 + ext_debug(&tree, "block %d requested for inode %u\n",
2058 + (int) iblock, (unsigned) inode->i_ino);
2059 + down_write(&EXT3_I(inode)->truncate_sem);
2061 + /* check in cache */
2062 + if (ext3_ext_in_cache(&tree, iblock, &newex)) {
2063 + if (newex.e_start == 0xffffffff && !create) {
2064 + /* block isn't allocated yet and
2065 + * user don't want to allocate it */
2067 + } else if (newex.e_start) {
2068 + /* block is already allocated */
2069 + newblock = iblock - newex.e_block + newex.e_start;
2074 + /* find extent for this block */
2075 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2076 + if (IS_ERR(path)) {
2077 + err = PTR_ERR(path);
2082 + depth = EXT_DEPTH(&tree);
2085 + * consistent leaf must not be empty
2086 + * this situations is possible, though, _during_ tree modification
2087 + * this is why assert can't be put in ext3_ext_find_extent()
2089 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2091 + if ((ex = path[depth].p_ext)) {
2092 + /* if found exent covers block, simple return it */
2093 + if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
2094 + newblock = iblock - ex->e_block + ex->e_start;
2095 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2096 + (int) iblock, ex->e_block, ex->e_num,
2098 + ext3_ext_put_in_cache(&tree, ex);
2104 + * requested block isn't allocated yet
2105 + * we couldn't try to create block if create flag is zero
2108 + /* put just found gap into cache to speedup subsequest reqs */
2109 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2113 + /* allocate new block */
2114 + goal = ext3_ext_find_goal(inode, path, iblock);
2115 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2118 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2121 + /* try to insert new extent into found leaf and return */
2122 + newex.e_block = iblock;
2123 + newex.e_start = newblock;
2125 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2129 + if (inode->i_size > EXT3_I(inode)->i_disksize)
2130 + EXT3_I(inode)->i_disksize = inode->i_size;
2132 + /* previous routine could use block we allocated */
2133 + newblock = newex.e_start;
2134 + set_bit(BH_New, &bh_result->b_state);
2136 + ext3_ext_put_in_cache(&tree, &newex);
2138 + ext3_ext_show_leaf(&tree, path);
2139 + set_bit(BH_Mapped, &bh_result->b_state);
2140 + bh_result->b_dev = inode->i_sb->s_dev;
2141 + bh_result->b_blocknr = newblock;
2144 + ext3_ext_drop_refs(path);
2147 + up_write(&EXT3_I(inode)->truncate_sem);
2152 +void ext3_ext_truncate(struct inode * inode)
2154 + struct address_space *mapping = inode->i_mapping;
2155 + struct super_block *sb = inode->i_sb;
2156 + struct ext3_extents_tree tree;
2157 + unsigned long last_block;
2161 + ext3_init_tree_desc(&tree, inode);
2164 + * probably first extent we're gonna free will be last in block
2166 + err = ext3_writepage_trans_blocks(inode) + 3;
2167 + handle = ext3_journal_start(inode, err);
2168 + if (IS_ERR(handle))
2171 + ext3_block_truncate_page(handle, mapping, inode->i_size);
2173 + down_write(&EXT3_I(inode)->truncate_sem);
2174 + ext3_ext_invalidate_cache(&tree);
2177 + * TODO: optimization is possible here
2178 + * probably we need not scaning at all,
2179 + * because page truncation is enough
2181 + if (ext3_orphan_add(handle, inode))
2184 + /* we have to know where to truncate from in crash case */
2185 + EXT3_I(inode)->i_disksize = inode->i_size;
2186 + ext3_mark_inode_dirty(handle, inode);
2188 + last_block = (inode->i_size + sb->s_blocksize - 1)
2189 + >> EXT3_BLOCK_SIZE_BITS(sb);
2190 + err = ext3_ext_remove_space(&tree, last_block, 0xffffffff);
2192 + /* In a multi-transaction truncate, we only make the final
2193 + * transaction synchronous */
2194 + if (IS_SYNC(inode))
2195 + handle->h_sync = 1;
2199 + * If this was a simple ftruncate(), and the file will remain alive
2200 + * then we need to clear up the orphan record which we created above.
2201 + * However, if this was a real unlink then we were called by
2202 + * ext3_delete_inode(), and we allow that function to clean up the
2203 + * orphan info for us.
2205 + if (inode->i_nlink)
2206 + ext3_orphan_del(handle, inode);
2208 + up_write(&EXT3_I(inode)->truncate_sem);
2209 + ext3_journal_stop(handle, inode);
2213 + * this routine calculate max number of blocks we could modify
2214 + * in order to allocate new block for an inode
2216 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2218 + struct ext3_extents_tree tree;
2221 + ext3_init_tree_desc(&tree, inode);
2223 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2225 + /* caller want to allocate num blocks */
2228 +#ifdef CONFIG_QUOTA
2230 + * FIXME: real calculation should be here
2231 + * it depends on blockmap format of qouta file
2233 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2239 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2241 + struct ext3_extents_tree tree;
2243 + ext3_init_tree_desc(&tree, inode);
2244 + ext3_extent_tree_init(handle, &tree);
2248 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2249 + struct ext3_ext_path *path,
2250 + struct ext3_extent *newex, int exist)
2252 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2255 + return EXT_CONTINUE;
2258 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2261 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2263 + buf->cur += sizeof(*newex);
2265 + buf->err = -EFAULT;
2268 + return EXT_CONTINUE;
2272 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2273 + struct ext3_ext_path *path,
2274 + struct ext3_extent *ex, int exist)
2276 + struct ext3_extent_tree_stats *buf =
2277 + (struct ext3_extent_tree_stats *) tree->private;
2281 + return EXT_CONTINUE;
2283 + depth = EXT_DEPTH(tree);
2284 + buf->extents_num++;
2285 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2287 + return EXT_CONTINUE;
2290 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2291 + unsigned long arg)
2295 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2298 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2299 + struct ext3_extent_buf buf;
2300 + struct ext3_extents_tree tree;
2302 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2305 + ext3_init_tree_desc(&tree, inode);
2306 + buf.cur = buf.buffer;
2308 + tree.private = &buf;
2309 + down_write(&EXT3_I(inode)->truncate_sem);
2310 + err = ext3_ext_walk_space(&tree, buf.start, 0xffffffff,
2311 + ext3_ext_store_extent_cb);
2312 + up_write(&EXT3_I(inode)->truncate_sem);
2315 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2316 + struct ext3_extent_tree_stats buf;
2317 + struct ext3_extents_tree tree;
2319 + ext3_init_tree_desc(&tree, inode);
2320 + down_write(&EXT3_I(inode)->truncate_sem);
2321 + buf.depth = EXT_DEPTH(&tree);
2322 + buf.extents_num = 0;
2324 + tree.private = &buf;
2325 + err = ext3_ext_walk_space(&tree, 0, 0xffffffff,
2326 + ext3_ext_collect_stats_cb);
2327 + up_write(&EXT3_I(inode)->truncate_sem);
2329 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2330 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2331 + struct ext3_extents_tree tree;
2332 + ext3_init_tree_desc(&tree, inode);
2333 + down_write(&EXT3_I(inode)->truncate_sem);
2334 + err = EXT_DEPTH(&tree);
2335 + up_write(&EXT3_I(inode)->truncate_sem);
2341 +EXPORT_SYMBOL(ext3_init_tree_desc);
2342 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2343 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2344 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2345 +EXPORT_SYMBOL(ext3_ext_walk_space);
2346 +EXPORT_SYMBOL(ext3_ext_find_goal);
2347 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2349 Index: linux-2.4.24/fs/ext3/ialloc.c
2350 ===================================================================
2351 --- linux-2.4.24.orig/fs/ext3/ialloc.c 2004-01-14 02:58:45.000000000 +0300
2352 +++ linux-2.4.24/fs/ext3/ialloc.c 2004-01-26 23:17:19.000000000 +0300
2353 @@ -592,11 +592,13 @@
2357 + if (test_opt(sb, EXTENTS)) {
2358 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2359 + ext3_extents_initialize_blockmap(handle, inode);
2361 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2367 if(DQUOT_ALLOC_INODE(inode)) {
2369 Index: linux-2.4.24/fs/ext3/inode.c
2370 ===================================================================
2371 --- linux-2.4.24.orig/fs/ext3/inode.c 2004-01-14 02:58:45.000000000 +0300
2372 +++ linux-2.4.24/fs/ext3/inode.c 2004-01-26 23:17:19.000000000 +0300
2373 @@ -848,6 +848,15 @@
2378 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2379 + struct buffer_head *bh, int create)
2381 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2382 + return ext3_ext_get_block(handle, inode, block, bh, create);
2383 + return ext3_get_block_handle(handle, inode, block, bh, create);
2387 * The BKL is not held on entry here.
2390 handle = ext3_journal_current_handle();
2391 J_ASSERT(handle != 0);
2393 - ret = ext3_get_block_handle(handle, inode, iblock, bh_result, create);
2394 + ret = ext3_get_block_wrap(handle, inode, iblock, bh_result, create);
2400 dummy.b_blocknr = -1000;
2401 buffer_trace_init(&dummy.b_history);
2402 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create);
2403 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create);
2404 if (!*errp && buffer_mapped(&dummy)) {
2405 struct buffer_head *bh;
2406 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2407 @@ -1403,7 +1412,7 @@
2408 * This required during truncate. We need to physically zero the tail end
2409 * of that block so it doesn't yield old data if the file is later grown.
2411 -static int ext3_block_truncate_page(handle_t *handle,
2412 +int ext3_block_truncate_page(handle_t *handle,
2413 struct address_space *mapping, loff_t from)
2415 unsigned long index = from >> PAGE_CACHE_SHIFT;
2416 @@ -1888,6 +1897,9 @@
2418 ext3_discard_prealloc(inode);
2420 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2421 + return ext3_ext_truncate(inode);
2423 handle = start_transaction(inode);
2425 return; /* AKPM: return what? */
2426 @@ -2536,6 +2548,9 @@
2427 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2430 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2431 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2433 if (ext3_should_journal_data(inode))
2434 ret = 3 * (bpp + indirects) + 2;
2436 @@ -2972,7 +2987,7 @@
2438 /* alloc blocks one by one */
2439 for (i = 0; i < nblocks; i++) {
2440 - ret = ext3_get_block_handle(handle, inode, blocks[i],
2441 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
2445 @@ -3048,7 +3063,7 @@
2449 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1);
2450 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1);
2452 printk(KERN_INFO "ext3_map_inode_page: error %d "
2453 "allocating block %ld\n", rc, iblock);
2454 Index: linux-2.4.24/fs/ext3/Makefile
2455 ===================================================================
2456 --- linux-2.4.24.orig/fs/ext3/Makefile 2004-01-14 02:58:45.000000000 +0300
2457 +++ linux-2.4.24/fs/ext3/Makefile 2004-02-05 18:44:25.000000000 +0300
2460 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2461 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2463 + xattr_trusted.o extents.o
2464 +export-objs += extents.o
2466 obj-m := $(O_TARGET)
2468 export-objs += xattr.o
2469 Index: linux-2.4.24/fs/ext3/super.c
2470 ===================================================================
2471 --- linux-2.4.24.orig/fs/ext3/super.c 2004-01-14 02:58:45.000000000 +0300
2472 +++ linux-2.4.24/fs/ext3/super.c 2004-01-26 23:17:19.000000000 +0300
2476 J_ASSERT(sbi->s_delete_inodes == 0);
2477 + ext3_ext_release(sb);
2478 ext3_xattr_put_super(sb);
2479 journal_destroy(sbi->s_journal);
2480 if (!(sb->s_flags & MS_RDONLY)) {
2481 @@ -702,6 +703,10 @@
2485 + else if (!strcmp (this_char, "extents"))
2486 + set_opt (*mount_options, EXTENTS);
2487 + else if (!strcmp (this_char, "extdebug"))
2488 + set_opt (*mount_options, EXTDEBUG);
2489 else if (!strcmp (this_char, "grpid") ||
2490 !strcmp (this_char, "bsdgroups"))
2491 set_opt (*mount_options, GRPID);
2492 @@ -1392,6 +1397,8 @@
2493 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2496 + ext3_ext_init(sb);
2501 Index: linux-2.4.24/fs/ext3/ioctl.c
2502 ===================================================================
2503 --- linux-2.4.24.orig/fs/ext3/ioctl.c 2004-01-14 02:58:42.000000000 +0300
2504 +++ linux-2.4.24/fs/ext3/ioctl.c 2004-01-26 23:17:19.000000000 +0300
2505 @@ -174,6 +174,10 @@
2509 + case EXT3_IOC_GET_EXTENTS:
2510 + case EXT3_IOC_GET_TREE_STATS:
2511 + case EXT3_IOC_GET_TREE_DEPTH:
2512 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2516 Index: linux-2.4.24/include/linux/ext3_fs.h
2517 ===================================================================
2518 --- linux-2.4.24.orig/include/linux/ext3_fs.h 2004-01-14 02:58:45.000000000 +0300
2519 +++ linux-2.4.24/include/linux/ext3_fs.h 2004-01-30 00:09:37.000000000 +0300
2521 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2522 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2523 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2524 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2526 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2527 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2529 #ifdef CONFIG_JBD_DEBUG
2530 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2532 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 5, long)
2533 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 6, long)
2534 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 7, long)
2537 * Structure of an inode on the disk
2539 #define EXT3_MOUNT_IOPEN 0x8000 /* Allow access via iopen */
2540 #define EXT3_MOUNT_IOPEN_NOPRIV 0x10000 /* Make iopen world-readable */
2541 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
2542 +#define EXT3_MOUNT_EXTENTS 0x100000/* Extents support */
2543 +#define EXT3_MOUNT_EXTDEBUG 0x200000/* Extents debug */
2545 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2546 #ifndef _LINUX_EXT2_FS_H
2548 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2551 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2552 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2553 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2554 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2555 @@ -769,6 +776,14 @@
2556 extern struct inode_operations ext3_symlink_inode_operations;
2557 extern struct inode_operations ext3_fast_symlink_inode_operations;
2560 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2561 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2562 + struct buffer_head *, int);
2563 +extern void ext3_ext_truncate(struct inode *);
2564 +extern void ext3_ext_init(struct super_block *);
2565 +extern void ext3_ext_release(struct super_block *);
2566 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2568 #endif /* __KERNEL__ */
2570 Index: linux-2.4.24/include/linux/ext3_extents.h
2571 ===================================================================
2572 --- linux-2.4.24.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2573 +++ linux-2.4.24/include/linux/ext3_extents.h 2004-02-05 20:31:08.000000000 +0300
2576 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
2578 + * This program is free software; you can redistribute it and/or modify
2579 + * it under the terms of the GNU General Public License version 2 as
2580 + * published by the Free Software Foundation.
2582 + * This program is distributed in the hope that it will be useful,
2583 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2584 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2585 + * GNU General Public License for more details.
2587 + * You should have received a copy of the GNU General Public Licens
2588 + * along with this program; if not, write to the Free Software
2589 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2592 +#ifndef _LINUX_EXT3_EXTENTS
2593 +#define _LINUX_EXT3_EXTENTS
2596 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2597 + * become very little, so index split, in-depth growing and
2598 + * other hard changes happens much more often
2599 + * this is for debug purposes only
2601 +#define AGRESSIVE_TEST_
2604 + * if CHECK_BINSEARCH defined, then results of binary search
2605 + * will be checked by linear search
2607 +#define CHECK_BINSEARCH_
2610 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2611 + * to get lots of info what's going on
2615 +#define ext_debug(tree,fmt,a...) \
2617 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2618 + printk(fmt, ##a); \
2621 +#define ext_debug(tree,fmt,a...)
2625 + * if EXT_STATS is defined then stats numbers are collected
2626 + * these number will be displayed at umount time
2631 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2634 + * ext3_inode has i_block array (total 60 bytes)
2635 + * first 4 bytes are used to store:
2636 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2637 + * - number of alive extents in the inode
2641 + * this is extent on-disk structure
2642 + * it's used at the bottom of the tree
2644 +struct ext3_extent {
2645 + __u32 e_block; /* first logical block extent covers */
2646 + __u32 e_start; /* first physical block extents lives */
2647 + __u32 e_num; /* number of blocks covered by extent */
2651 + * this is index on-disk structure
2652 + * it's used at all the levels, but the bottom
2654 +struct ext3_extent_idx {
2655 + __u32 e_block; /* index covers logical blocks from 'block' */
2656 + __u32 e_leaf; /* pointer to the physical block of the next *
2657 + * level. leaf or next index could bet here */
2661 + * each block (leaves and indexes), even inode-stored has header
2663 +struct ext3_extent_header {
2664 + __u16 e_magic; /* probably will support different formats */
2665 + __u16 e_num; /* number of valid entries */
2666 + __u16 e_max; /* capacity of store in entries */
2667 + __u16 e_depth; /* has tree real underlaying blocks? */
2668 + __u32 e_generation; /* generation of the tree */
2671 +#define EXT3_EXT_MAGIC 0xf301
2674 + * array of ext3_ext_path contains path to some extent
2675 + * creation/lookup routines use it for traversal/splitting/etc
2676 + * truncate uses it to simulate recursive walking
2678 +struct ext3_ext_path {
2681 + struct ext3_extent *p_ext;
2682 + struct ext3_extent_idx *p_idx;
2683 + struct ext3_extent_header *p_hdr;
2684 + struct buffer_head *p_bh;
2688 + * structure for external API
2693 + * ext3_extents_tree is used to pass initial information
2694 + * to top-level extents API
2696 +struct ext3_extents_tree {
2697 + struct inode *inode; /* inode which tree belongs to */
2698 + void *root; /* ptr to data top of tree resides at */
2699 + void *buffer; /* will be passed as arg to ^^ routines */
2702 + struct ext3_extent *cex;/* last found extent */
2703 + int (*get_write_access)(handle_t *h, void *buffer);
2704 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2705 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2706 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2707 + struct ext3_extent *, unsigned long,
2709 + int (*remove_extent)(struct ext3_extents_tree *,
2710 + struct ext3_extent *, unsigned long,
2712 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2713 + struct ext3_ext_path *, struct ext3_extent *,
2718 + * to be called by ext3_ext_walk_space()
2719 + * negative retcode - error
2720 + * positive retcode - signal for ext3_ext_walk_space(), see below
2721 + * callback must return valid extent (passed or newly created)
2723 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2724 + struct ext3_ext_path *,
2725 + struct ext3_extent *, int);
2727 +#define EXT_CONTINUE 0
2728 +#define EXT_BREAK 1
2729 +#define EXT_REPEAT 2
2732 +#define EXT_FIRST_EXTENT(__hdr__) \
2733 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2734 + sizeof(struct ext3_extent_header)))
2735 +#define EXT_FIRST_INDEX(__hdr__) \
2736 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2737 + sizeof(struct ext3_extent_header)))
2738 +#define EXT_HAS_FREE_INDEX(__path__) \
2739 + ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
2740 +#define EXT_LAST_EXTENT(__hdr__) \
2741 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
2742 +#define EXT_LAST_INDEX(__hdr__) \
2743 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
2744 +#define EXT_MAX_EXTENT(__hdr__) \
2745 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
2746 +#define EXT_MAX_INDEX(__hdr__) \
2747 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
2749 +#define EXT_ROOT_HDR(tree) \
2750 + ((struct ext3_extent_header *) (tree)->root)
2751 +#define EXT_BLOCK_HDR(bh) \
2752 + ((struct ext3_extent_header *) (bh)->b_data)
2753 +#define EXT_DEPTH(_t_) \
2754 + (((struct ext3_extent_header *)((_t_)->root))->e_depth)
2755 +#define EXT_GENERATION(_t_) \
2756 + (((struct ext3_extent_header *)((_t_)->root))->e_generation)
2759 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2763 + * this structure is used to gather extents from the tree via ioctl
2765 +struct ext3_extent_buf {
2766 + unsigned long start;
2774 + * this structure is used to collect stats info about the tree
2776 +struct ext3_extent_tree_stats {
2782 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2783 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2784 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2785 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2786 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2787 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2789 +#endif /* _LINUX_EXT3_EXTENTS */
2791 Index: linux-2.4.24/include/linux/ext3_fs_i.h
2792 ===================================================================
2793 --- linux-2.4.24.orig/include/linux/ext3_fs_i.h 2004-01-24 19:30:22.000000000 +0300
2794 +++ linux-2.4.24/include/linux/ext3_fs_i.h 2004-01-26 23:17:19.000000000 +0300
2796 * by other means, so we have truncate_sem.
2798 struct rw_semaphore truncate_sem;
2800 + __u32 i_cached_extent[3];
2803 #endif /* _LINUX_EXT3_FS_I */