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-08-06 03:59:01.000000000 +0400
7 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
8 + * Written by Alex Tomas <alex@clusterfs.com>
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License version 2 as
12 + * published by the Free Software Foundation.
14 + * This program is distributed in the hope that it will be useful,
15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 + * GNU General Public License for more details.
19 + * You should have received a copy of the GNU General Public Licens
20 + * along with this program; if not, write to the Free Software
21 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
25 + * Extents support for EXT3
28 + * - ext3_ext_walk_space() sould not use ext3_ext_find_extent()
29 + * - ext3_ext_calc_credits() could take 'mergable' into account
30 + * - ext3*_error() should be used in some situations
31 + * - find_goal() [to be tested and improved]
32 + * - smart tree reduction
33 + * - arch-independence
34 + * common on-disk format for big/little-endian arch
37 +#include <linux/module.h>
38 +#include <linux/fs.h>
39 +#include <linux/time.h>
40 +#include <linux/ext3_jbd.h>
41 +#include <linux/jbd.h>
42 +#include <linux/locks.h>
43 +#include <linux/smp_lock.h>
44 +#include <linux/highuid.h>
45 +#include <linux/pagemap.h>
46 +#include <linux/quotaops.h>
47 +#include <linux/string.h>
48 +#include <linux/slab.h>
49 +#include <linux/ext3_extents.h>
50 +#include <asm/uaccess.h>
52 +static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
56 + if (handle->h_buffer_credits > needed)
58 + if (!ext3_journal_extend(handle, needed))
60 + err = ext3_journal_restart(handle, needed);
66 +ext3_ext_get_access_for_root(handle_t *h, struct ext3_extents_tree *tree)
68 + if (tree->ops->get_write_access)
69 + return tree->ops->get_write_access(h,tree->buffer);
75 +ext3_ext_mark_root_dirty(handle_t *h, struct ext3_extents_tree *tree)
77 + if (tree->ops->mark_buffer_dirty)
78 + return tree->ops->mark_buffer_dirty(h,tree->buffer);
88 +static int ext3_ext_get_access(handle_t *handle,
89 + struct ext3_extents_tree *tree,
90 + struct ext3_ext_path *path)
95 + /* path points to block */
96 + err = ext3_journal_get_write_access(handle, path->p_bh);
98 + /* path points to leaf/index in inode body */
99 + err = ext3_ext_get_access_for_root(handle, tree);
110 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
111 + struct ext3_ext_path *path)
115 + /* path points to block */
116 + err =ext3_journal_dirty_metadata(handle, path->p_bh);
118 + /* path points to leaf/index in inode body */
119 + err = ext3_ext_mark_root_dirty(handle, tree);
125 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
126 + struct ext3_ext_path *path, struct ext3_extent *ex,
129 + int goal, depth, newblock;
130 + struct inode *inode;
133 + if (tree->ops->new_block)
134 + return tree->ops->new_block(handle, tree, path, ex, err);
136 + inode = tree->inode;
137 + depth = EXT_DEPTH(tree);
138 + if (path && depth > 0) {
139 + goal = path[depth-1].p_block;
141 + struct ext3_inode_info *ei = EXT3_I(inode);
142 + unsigned long bg_start;
143 + unsigned long colour;
145 + bg_start = (ei->i_block_group *
146 + EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
147 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
148 + colour = (current->pid % 16) *
149 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
150 + goal = bg_start + colour;
153 + newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
157 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
159 + struct ext3_extent_header *neh;
160 + neh = EXT_ROOT_HDR(tree);
161 + neh->e_generation++;
164 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
168 + size = (tree->inode->i_sb->s_blocksize -
169 + sizeof(struct ext3_extent_header))
170 + / sizeof(struct ext3_extent);
171 +#ifdef AGRESSIVE_TEST
177 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
181 + size = (tree->inode->i_sb->s_blocksize -
182 + sizeof(struct ext3_extent_header))
183 + / sizeof(struct ext3_extent_idx);
184 +#ifdef AGRESSIVE_TEST
190 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
194 + size = (tree->buffer_len - sizeof(struct ext3_extent_header))
195 + / sizeof(struct ext3_extent);
196 +#ifdef AGRESSIVE_TEST
202 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
206 + size = (tree->buffer_len -
207 + sizeof(struct ext3_extent_header))
208 + / sizeof(struct ext3_extent_idx);
209 +#ifdef AGRESSIVE_TEST
215 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
216 + struct ext3_ext_path *path)
219 + int k, l = path->p_depth;
221 + ext_debug(tree, "path:");
222 + for (k = 0; k <= l; k++, path++) {
224 + ext_debug(tree, " %d->%d", path->p_idx->e_block,
225 + path->p_idx->e_leaf);
226 + } else if (path->p_ext) {
227 + ext_debug(tree, " %d:%d:%d",
228 + path->p_ext->e_block,
229 + path->p_ext->e_num,
230 + path->p_ext->e_start);
232 + ext_debug(tree, " []");
234 + ext_debug(tree, "\n");
238 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
239 + struct ext3_ext_path *path)
242 + int depth = EXT_DEPTH(tree);
243 + struct ext3_extent_header *eh;
244 + struct ext3_extent *ex;
250 + eh = path[depth].p_hdr;
251 + ex = EXT_FIRST_EXTENT(eh);
253 + for (i = 0; i < eh->e_num; i++, ex++) {
254 + ext_debug(tree, "%d:%d:%d ",
255 + ex->e_block, ex->e_num, ex->e_start);
257 + ext_debug(tree, "\n");
261 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
263 + int depth = path->p_depth;
266 + for (i = 0; i <= depth; i++, path++)
268 + brelse(path->p_bh);
274 + * binary search for closest index by given block
277 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
278 + struct ext3_ext_path *path, int block)
280 + struct ext3_extent_header *eh = path->p_hdr;
281 + struct ext3_extent_idx *ix;
284 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
285 + EXT_ASSERT(eh->e_num <= eh->e_max);
286 + EXT_ASSERT(eh->e_num > 0);
288 + ext_debug(tree, "binsearch for %d(idx): ", block);
290 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
295 + if (block < ix[l + k].e_block)
299 + ext_debug(tree, "%d:%d:%d ", k, l, r);
304 + ext_debug(tree, " -> %d->%d ", path->p_idx->e_block, path->p_idx->e_leaf);
307 + if (block < ix->e_block)
309 + path->p_idx = ix++;
311 + ext_debug(tree, " -> %d->%d\n", path->p_idx->e_block,
312 + path->p_idx->e_leaf);
314 +#ifdef CHECK_BINSEARCH
316 + struct ext3_extent_idx *chix;
318 + chix = ix = EXT_FIRST_INDEX(eh);
319 + for (k = 0; k < eh->e_num; k++, ix++) {
320 + if (k != 0 && ix->e_block <= ix[-1].e_block) {
321 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
322 + ix, EXT_FIRST_INDEX(eh));
323 + printk("%u <= %u\n",
324 + ix->e_block,ix[-1].e_block);
326 + EXT_ASSERT(k == 0 || ix->e_block > ix[-1].e_block);
327 + if (block < ix->e_block)
331 + EXT_ASSERT(chix == path->p_idx);
338 + * binary search for closest extent by given block
341 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
342 + struct ext3_ext_path *path, int block)
344 + struct ext3_extent_header *eh = path->p_hdr;
345 + struct ext3_extent *ex;
348 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
349 + EXT_ASSERT(eh->e_num <= eh->e_max);
351 + if (eh->e_num == 0) {
353 + * this leaf is empty yet:
354 + * we get such a leaf in split/add case
359 + ext_debug(tree, "binsearch for %d: ", block);
361 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
366 + if (block < ex[l + k].e_block)
370 + ext_debug(tree, "%d:%d:%d ", k, l, r);
375 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->e_block,
376 + path->p_ext->e_start, path->p_ext->e_num);
379 + if (block < ex->e_block)
381 + path->p_ext = ex++;
383 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->e_block,
384 + path->p_ext->e_start, path->p_ext->e_num);
386 +#ifdef CHECK_BINSEARCH
388 + struct ext3_extent *chex;
390 + chex = ex = EXT_FIRST_EXTENT(eh);
391 + for (k = 0; k < eh->e_num; k++, ex++) {
392 + EXT_ASSERT(k == 0 || ex->e_block > ex[-1].e_block);
393 + if (block < ex->e_block)
397 + EXT_ASSERT(chex == path->p_ext);
403 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
405 + struct ext3_extent_header *eh;
407 + BUG_ON(tree->buffer_len == 0);
408 + ext3_ext_get_access_for_root(handle, tree);
409 + eh = EXT_ROOT_HDR(tree);
412 + eh->e_magic = EXT3_EXT_MAGIC;
413 + eh->e_max = ext3_ext_space_root(tree);
414 + ext3_ext_mark_root_dirty(handle, tree);
415 + ext3_ext_invalidate_cache(tree);
419 +struct ext3_ext_path *
420 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
421 + struct ext3_ext_path *path)
423 + struct ext3_extent_header *eh;
424 + struct buffer_head *bh;
425 + int depth, i, ppos = 0;
428 + EXT_ASSERT(tree->inode);
429 + EXT_ASSERT(tree->root);
431 + eh = EXT_ROOT_HDR(tree);
433 + i = depth = EXT_DEPTH(tree);
434 + EXT_ASSERT(eh->e_max);
435 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
436 + EXT_ASSERT(i == 0 || eh->e_num > 0);
438 + /* account possible depth increase */
440 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
443 + return ERR_PTR(-ENOMEM);
445 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
446 + path[0].p_hdr = eh;
448 + /* walk through the tree */
450 + ext_debug(tree, "depth %d: num %d, max %d\n",
451 + ppos, eh->e_num, eh->e_max);
452 + ext3_ext_binsearch_idx(tree, path + ppos, block);
453 + path[ppos].p_block = path[ppos].p_idx->e_leaf;
454 + path[ppos].p_depth = i;
455 + path[ppos].p_ext = NULL;
457 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
459 + ext3_ext_drop_refs(path);
461 + return ERR_PTR(-EIO);
463 + eh = EXT_BLOCK_HDR(bh);
465 + EXT_ASSERT(ppos <= depth);
466 + path[ppos].p_bh = bh;
467 + path[ppos].p_hdr = eh;
471 + path[ppos].p_depth = i;
472 + path[ppos].p_hdr = eh;
473 + path[ppos].p_ext = NULL;
476 + ext3_ext_binsearch(tree, path + ppos, block);
478 + ext3_ext_show_path(tree, path);
484 + * insert new index [logical;ptr] into the block at cupr
485 + * it check where to insert: before curp or after curp
487 +static int ext3_ext_insert_index(handle_t *handle,
488 + struct ext3_extents_tree *tree,
489 + struct ext3_ext_path *curp,
490 + int logical, int ptr)
492 + struct ext3_extent_idx *ix;
495 + if ((err = ext3_ext_get_access(handle, tree, curp)))
498 + EXT_ASSERT(logical != curp->p_idx->e_block);
499 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
500 + if (logical > curp->p_idx->e_block) {
502 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
503 + len = (len - 1) * sizeof(struct ext3_extent_idx);
504 + len = len < 0 ? 0 : len;
505 + ext_debug(tree, "insert new index %d after: %d. "
506 + "move %d from 0x%p to 0x%p\n",
508 + (curp->p_idx + 1), (curp->p_idx + 2));
509 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
511 + ix = curp->p_idx + 1;
513 + /* insert before */
514 + len = len * sizeof(struct ext3_extent_idx);
515 + len = len < 0 ? 0 : len;
516 + ext_debug(tree, "insert new index %d before: %d. "
517 + "move %d from 0x%p to 0x%p\n",
519 + curp->p_idx, (curp->p_idx + 1));
520 + memmove(curp->p_idx + 1, curp->p_idx, len);
524 + ix->e_block = logical;
526 + curp->p_hdr->e_num++;
528 + EXT_ASSERT(curp->p_hdr->e_num <= curp->p_hdr->e_max);
529 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
531 + err = ext3_ext_dirty(handle, tree, curp);
532 + ext3_std_error(tree->inode->i_sb, err);
538 + * routine inserts new subtree into the path, using free index entry
540 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
541 + * - makes decision where to split
542 + * - moves remaining extens and index entries (right to the split point)
543 + * into the newly allocated blocks
544 + * - initialize subtree
546 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
547 + struct ext3_ext_path *path,
548 + struct ext3_extent *newext, int at)
550 + struct buffer_head *bh = NULL;
551 + int depth = EXT_DEPTH(tree);
552 + struct ext3_extent_header *neh;
553 + struct ext3_extent_idx *fidx;
554 + struct ext3_extent *ex;
555 + int i = at, k, m, a;
556 + unsigned long newblock, oldblock, border;
557 + int *ablocks = NULL; /* array of allocated blocks */
560 + /* make decision: where to split? */
561 + /* FIXME: now desicion is simplest: at current extent */
563 + /* if current leaf will be splitted, then we should use
564 + * border from split point */
565 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
566 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
567 + border = path[depth].p_ext[1].e_block;
568 + ext_debug(tree, "leaf will be splitted."
569 + " next leaf starts at %d\n",
572 + border = newext->e_block;
573 + ext_debug(tree, "leaf will be added."
574 + " next leaf starts at %d\n",
579 + * if error occurs, then we break processing
580 + * and turn filesystem read-only. so, index won't
581 + * be inserted and tree will be in consistent
582 + * state. next mount will repair buffers too
586 + * get array to track all allocated blocks
587 + * we need this to handle errors and free blocks
590 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
593 + memset(ablocks, 0, sizeof(unsigned long) * depth);
595 + /* allocate all needed blocks */
596 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
597 + for (a = 0; a < depth - at; a++) {
598 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
601 + ablocks[a] = newblock;
604 + /* initialize new leaf */
605 + newblock = ablocks[--a];
606 + EXT_ASSERT(newblock);
607 + bh = sb_getblk(tree->inode->i_sb, newblock);
614 + if ((err = ext3_journal_get_create_access(handle, bh)))
617 + neh = EXT_BLOCK_HDR(bh);
619 + neh->e_max = ext3_ext_space_block(tree);
620 + neh->e_magic = EXT3_EXT_MAGIC;
622 + ex = EXT_FIRST_EXTENT(neh);
624 + /* move remain of path[depth] to the new leaf */
625 + EXT_ASSERT(path[depth].p_hdr->e_num == path[depth].p_hdr->e_max);
626 + /* start copy from next extent */
627 + /* TODO: we could do it by single memmove */
629 + path[depth].p_ext++;
630 + while (path[depth].p_ext <=
631 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
632 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
633 + path[depth].p_ext->e_block,
634 + path[depth].p_ext->e_start,
635 + path[depth].p_ext->e_num,
637 + memmove(ex++, path[depth].p_ext++,
638 + sizeof(struct ext3_extent));
642 + mark_buffer_uptodate(bh, 1);
645 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
650 + /* correct old leaf */
652 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
654 + path[depth].p_hdr->e_num -= m;
655 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
660 + /* create intermediate indexes */
661 + k = depth - at - 1;
662 + EXT_ASSERT(k >= 0);
664 + ext_debug(tree, "create %d intermediate indices\n", k);
665 + /* insert new index into current index block */
666 + /* current depth stored in i var */
669 + oldblock = newblock;
670 + newblock = ablocks[--a];
671 + bh = sb_getblk(tree->inode->i_sb, newblock);
678 + if ((err = ext3_journal_get_create_access(handle, bh)))
681 + neh = EXT_BLOCK_HDR(bh);
683 + neh->e_magic = EXT3_EXT_MAGIC;
684 + neh->e_max = ext3_ext_space_block_idx(tree);
685 + neh->e_depth = depth - i;
686 + fidx = EXT_FIRST_INDEX(neh);
687 + fidx->e_block = border;
688 + fidx->e_leaf = oldblock;
690 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
691 + i, newblock, border, oldblock);
696 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
697 + EXT_MAX_INDEX(path[i].p_hdr));
698 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
699 + EXT_LAST_INDEX(path[i].p_hdr));
700 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
701 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
702 + i, path[i].p_idx->e_block,
703 + path[i].p_idx->e_leaf, newblock);
704 + memmove(++fidx, path[i].p_idx++,
705 + sizeof(struct ext3_extent_idx));
707 + EXT_ASSERT(neh->e_num <= neh->e_max);
710 + mark_buffer_uptodate(bh, 1);
713 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
718 + /* correct old index */
720 + err = ext3_ext_get_access(handle, tree, path + i);
723 + path[i].p_hdr->e_num -= m;
724 + err = ext3_ext_dirty(handle, tree, path + i);
732 + /* insert new index */
734 + err = ext3_ext_insert_index(handle, tree, path + at,
739 + if (buffer_locked(bh))
745 + /* free all allocated blocks in error case */
746 + for (i = 0; i < depth; i++) {
749 + 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->ops->mergable)
1031 + return tree->ops->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;
1051 + EXT_ASSERT(path[depth].p_hdr);
1053 + /* try to insert block into found extent and return */
1054 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1055 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1056 + newext->e_num, ex->e_block, ex->e_num,
1058 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1060 + ex->e_num += newext->e_num;
1061 + eh = path[depth].p_hdr;
1067 + depth = EXT_DEPTH(tree);
1068 + eh = path[depth].p_hdr;
1069 + if (eh->e_num < eh->e_max)
1072 + /* probably next leaf has space for us? */
1073 + fex = EXT_LAST_EXTENT(eh);
1074 + next = ext3_ext_next_leaf_block(tree, path);
1075 + if (newext->e_block > fex->e_block && next != 0xffffffff) {
1076 + ext_debug(tree, "next leaf block - %d\n", next);
1077 + EXT_ASSERT(!npath);
1078 + npath = ext3_ext_find_extent(tree, next, NULL);
1079 + if (IS_ERR(npath))
1080 + return PTR_ERR(npath);
1081 + EXT_ASSERT(npath->p_depth == path->p_depth);
1082 + eh = npath[depth].p_hdr;
1083 + if (eh->e_num < eh->e_max) {
1084 + ext_debug(tree, "next leaf isnt full(%d)\n",
1089 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1090 + eh->e_num, eh->e_max);
1094 + * there is no free space in found leaf
1095 + * we're gonna add new leaf in the tree
1097 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1100 + depth = EXT_DEPTH(tree);
1101 + eh = path[depth].p_hdr;
1104 + nearex = path[depth].p_ext;
1106 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1110 + /* there is no extent in this leaf, create first one */
1111 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1112 + newext->e_block, newext->e_start,
1114 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1115 + } else if (newext->e_block > nearex->e_block) {
1116 + EXT_ASSERT(newext->e_block != nearex->e_block);
1117 + if (nearex != EXT_LAST_EXTENT(eh)) {
1118 + len = EXT_MAX_EXTENT(eh) - nearex;
1119 + len = (len - 1) * sizeof(struct ext3_extent);
1120 + len = len < 0 ? 0 : len;
1121 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1122 + "move %d from 0x%p to 0x%p\n",
1123 + newext->e_block, newext->e_start,
1125 + nearex, len, nearex + 1, nearex + 2);
1126 + memmove(nearex + 2, nearex + 1, len);
1128 + path[depth].p_ext = nearex + 1;
1130 + EXT_ASSERT(newext->e_block != nearex->e_block);
1131 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1132 + len = len < 0 ? 0 : len;
1133 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1134 + "move %d from 0x%p to 0x%p\n",
1135 + newext->e_block, newext->e_start, newext->e_num,
1136 + nearex, len, nearex + 1, nearex + 2);
1137 + memmove(nearex + 1, nearex, len);
1138 + path[depth].p_ext = nearex;
1142 + nearex = path[depth].p_ext;
1143 + nearex->e_block = newext->e_block;
1144 + nearex->e_start = newext->e_start;
1145 + nearex->e_num = newext->e_num;
1148 + /* try to merge extents to the right */
1149 + while (nearex < EXT_LAST_EXTENT(eh)) {
1150 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1152 + /* merge with next extent! */
1153 + nearex->e_num += nearex[1].e_num;
1154 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1155 + len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1156 + * sizeof(struct ext3_extent);
1157 + memmove(nearex + 1, nearex + 2, len);
1160 + EXT_ASSERT(eh->e_num > 0);
1163 + /* try to merge extents to the left */
1165 + /* time to correct all indexes above */
1166 + err = ext3_ext_correct_indexes(handle, tree, path);
1170 + err = ext3_ext_dirty(handle, tree, path + depth);
1174 + ext3_ext_drop_refs(npath);
1177 + ext3_ext_tree_changed(tree);
1178 + ext3_ext_invalidate_cache(tree);
1182 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1183 + unsigned long num, ext_prepare_callback func)
1185 + struct ext3_ext_path *path = NULL;
1186 + struct ext3_extent *ex, cbex;
1187 + unsigned long next, start = 0, end = 0;
1188 + unsigned long last = block + num;
1189 + int depth, exists, err = 0;
1193 + EXT_ASSERT(tree->inode);
1194 + EXT_ASSERT(tree->root);
1196 + while (block < last && block != 0xfffffffff) {
1197 + num = last - block;
1198 + /* find extent for this block */
1199 + path = ext3_ext_find_extent(tree, block, path);
1200 + if (IS_ERR(path)) {
1201 + err = PTR_ERR(path);
1206 + depth = EXT_DEPTH(tree);
1207 + EXT_ASSERT(path[depth].p_hdr);
1208 + ex = path[depth].p_ext;
1209 + next = ext3_ext_next_allocated_block(path);
1213 + /* there is no extent yet, so try to allocate
1214 + * all requested space */
1216 + end = block + num;
1217 + } else if (ex->e_block > block) {
1218 + /* need to allocate space before found extent */
1220 + end = ex->e_block;
1221 + if (block + num < end)
1222 + end = block + num;
1223 + } else if (block >= ex->e_block + ex->e_num) {
1224 + /* need to allocate space after found extent */
1226 + end = block + num;
1229 + } else if (block >= ex->e_block) {
1231 + * some part of requested space is covered
1235 + end = ex->e_block + ex->e_num;
1236 + if (block + num < end)
1237 + end = block + num;
1242 + EXT_ASSERT(end > start);
1245 + cbex.e_block = start;
1246 + cbex.e_num = end - start;
1251 + EXT_ASSERT(path[depth].p_hdr);
1252 + err = func(tree, path, &cbex, exists);
1253 + ext3_ext_drop_refs(path);
1257 + if (err == EXT_REPEAT)
1259 + else if (err == EXT_BREAK) {
1264 + if (EXT_DEPTH(tree) != depth) {
1265 + /* depth was changed. we have to realloc path */
1270 + block = cbex.e_block + cbex.e_num;
1274 + ext3_ext_drop_refs(path);
1282 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, struct ext3_extent *ex)
1286 + EXT_ASSERT(ex->e_num);
1287 + tree->cex->e_block = ex->e_block;
1288 + tree->cex->e_start = ex->e_start;
1289 + tree->cex->e_num = ex->e_num;
1294 + * this routine calculate boundaries of the gap requested block fits into
1295 + * and cache this gap
1298 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1299 + struct ext3_ext_path *path,
1300 + unsigned long block)
1302 + int depth = EXT_DEPTH(tree);
1303 + struct ext3_extent *ex, gex;
1308 + ex = path[depth].p_ext;
1310 + /* there is no extent yet, so gap is [0;-] */
1312 + gex.e_num = 0xffffffff;
1313 + ext_debug(tree, "cache gap(whole file):");
1314 + } else if (block < ex->e_block) {
1315 + gex.e_block = block;
1316 + gex.e_num = ex->e_block - block;
1317 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1318 + (unsigned long) block,
1319 + (unsigned long) ex->e_block,
1320 + (unsigned long) ex->e_num);
1321 + } else if (block >= ex->e_block + ex->e_num) {
1322 + gex.e_block = ex->e_block + ex->e_num;
1323 + gex.e_num = ext3_ext_next_allocated_block(path);
1324 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1325 + (unsigned long) ex->e_block,
1326 + (unsigned long) ex->e_num,
1327 + (unsigned long) block);
1328 + EXT_ASSERT(gex.e_num > gex.e_block);
1329 + gex.e_num = gex.e_num - gex.e_block;
1334 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.e_block,
1335 + (unsigned long) gex.e_num);
1336 + gex.e_start = 0xffffffff;
1337 + ext3_ext_put_in_cache(tree, &gex);
1341 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1342 + struct ext3_extent *ex)
1344 + struct ext3_extent *cex = tree->cex;
1346 + /* is there cache storage at all? */
1350 + /* has cache valid data? */
1351 + if (cex->e_num == 0)
1354 + if (block >= cex->e_block && block < cex->e_block + cex->e_num) {
1355 + ex->e_block = cex->e_block;
1356 + ex->e_start = cex->e_start;
1357 + ex->e_num = cex->e_num;
1358 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1359 + (unsigned long) block,
1360 + (unsigned long) ex->e_block,
1361 + (unsigned long) ex->e_num,
1362 + (unsigned long) ex->e_start);
1366 + /* not in cache */
1371 + * routine removes index from the index block
1372 + * it's used in truncate case only. thus all requests are for
1373 + * last index in the block only
1375 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1376 + struct ext3_ext_path *path)
1378 + struct buffer_head *bh;
1381 + /* free index block */
1383 + EXT_ASSERT(path->p_hdr->e_num);
1384 + if ((err = ext3_ext_get_access(handle, tree, path)))
1386 + path->p_hdr->e_num--;
1387 + if ((err = ext3_ext_dirty(handle, tree, path)))
1389 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1390 + path->p_idx->e_leaf);
1391 + bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->e_leaf);
1392 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->e_leaf);
1393 + ext3_free_blocks(handle, tree->inode, path->p_idx->e_leaf, 1);
1397 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1398 + struct ext3_ext_path *path)
1400 + int depth = EXT_DEPTH(tree);
1404 + /* probably there is space in leaf? */
1405 + if (path[depth].p_hdr->e_num < path[depth].p_hdr->e_max)
1410 + * the worste case we're expecting is creation of the
1411 + * new root (growing in depth) with index splitting
1412 + * for splitting we have to consider depth + 1 because
1413 + * previous growing could increase it
1415 + depth = depth + 1;
1418 + * growing in depth:
1419 + * block allocation + new root + old root
1421 + needed = EXT3_ALLOC_NEEDED + 2;
1423 + /* index split. we may need:
1424 + * allocate intermediate indexes and new leaf
1425 + * change two blocks at each level, but root
1426 + * modify root block (inode)
1428 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1434 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1435 + struct ext3_ext_path *path, unsigned long start,
1436 + unsigned long end)
1438 + struct ext3_extent *ex, tex;
1439 + struct ext3_ext_path *npath;
1440 + int depth, creds, err;
1442 + depth = EXT_DEPTH(tree);
1443 + ex = path[depth].p_ext;
1445 + EXT_ASSERT(end < ex->e_block + ex->e_num - 1);
1446 + EXT_ASSERT(ex->e_block < start);
1448 + /* calculate tail extent */
1449 + tex.e_block = end + 1;
1450 + EXT_ASSERT(tex.e_block < ex->e_block + ex->e_num);
1451 + tex.e_num = ex->e_block + ex->e_num - tex.e_block;
1453 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1454 + handle = ext3_ext_journal_restart(handle, creds);
1455 + if (IS_ERR(handle))
1456 + return PTR_ERR(handle);
1458 + /* calculate head extent. use primary extent */
1459 + err = ext3_ext_get_access(handle, tree, path + depth);
1462 + ex->e_num = start - ex->e_block;
1463 + err = ext3_ext_dirty(handle, tree, path + depth);
1467 + /* FIXME: some callback to free underlying resource
1468 + * and correct e_start? */
1469 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1470 + ex->e_block, ex->e_num, tex.e_block, tex.e_num);
1472 + npath = ext3_ext_find_extent(tree, ex->e_block, NULL);
1473 + if (IS_ERR(npath))
1474 + return PTR_ERR(npath);
1475 + depth = EXT_DEPTH(tree);
1476 + EXT_ASSERT(npath[depth].p_ext->e_block == ex->e_block);
1477 + EXT_ASSERT(npath[depth].p_ext->e_num == ex->e_num);
1479 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1480 + ext3_ext_drop_refs(npath);
1488 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1489 + struct ext3_ext_path *path, unsigned long start,
1490 + unsigned long end)
1492 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1493 + int err = 0, correct_index = 0;
1494 + int depth = EXT_DEPTH(tree), credits;
1495 + struct ext3_extent_header *eh;
1496 + unsigned a, b, block, num;
1498 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1499 + if (!path[depth].p_hdr)
1500 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1501 + eh = path[depth].p_hdr;
1503 + EXT_ASSERT(eh->e_num <= eh->e_max);
1504 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
1506 + /* find where to start removing */
1507 + le = ex = EXT_LAST_EXTENT(eh);
1508 + while (ex != EXT_FIRST_EXTENT(eh)) {
1509 + if (ex->e_block <= end)
1514 + if (start > ex->e_block && end < ex->e_block + ex->e_num - 1) {
1515 + /* removal of internal part of the extent requested
1516 + * tail and head must be placed in different extent
1517 + * so, we have to insert one more extent */
1518 + path[depth].p_ext = ex;
1519 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1523 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1524 + ex->e_block + ex->e_num > start) {
1525 + ext_debug(tree, "remove ext %u:%u\n", ex->e_block, ex->e_num);
1526 + path[depth].p_ext = ex;
1528 + a = ex->e_block > start ? ex->e_block : start;
1529 + b = ex->e_block + ex->e_num - 1 < end ?
1530 + ex->e_block + ex->e_num - 1 : end;
1532 + ext_debug(tree, " border %u:%u\n", a, b);
1534 + if (a != ex->e_block && b != ex->e_block + ex->e_num - 1) {
1538 + } else if (a != ex->e_block) {
1539 + /* remove tail of the extent */
1540 + block = ex->e_block;
1542 + } else if (b != ex->e_block + ex->e_num - 1) {
1543 + /* remove head of the extent */
1547 + /* remove whole extent: excelent! */
1548 + block = ex->e_block;
1550 + EXT_ASSERT(a == ex->e_block &&
1551 + b == ex->e_block + ex->e_num - 1);
1554 + if (ex == EXT_FIRST_EXTENT(eh))
1555 + correct_index = 1;
1558 + if (correct_index)
1559 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1560 + if (tree->ops->remove_extent_credits)
1561 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1563 + handle = ext3_ext_journal_restart(handle, credits);
1564 + if (IS_ERR(handle)) {
1565 + err = PTR_ERR(handle);
1569 + err = ext3_ext_get_access(handle, tree, path + depth);
1573 + if (tree->ops->remove_extent)
1574 + err = tree->ops->remove_extent(tree, ex, a, b);
1579 + /* this extent is removed entirely mark slot unused */
1585 + ex->e_block = block;
1588 + err = ext3_ext_dirty(handle, tree, path + depth);
1592 + ext_debug(tree, "new extent: %u:%u:%u\n",
1593 + ex->e_block, ex->e_num, ex->e_start);
1598 + /* reuse unused slots */
1600 + if (lu->e_start) {
1609 + if (correct_index && eh->e_num)
1610 + err = ext3_ext_correct_indexes(handle, tree, path);
1612 + /* if this leaf is free, then we should
1613 + * remove it from index block above */
1614 + if (err == 0 && eh->e_num == 0 && path[depth].p_bh != NULL)
1615 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1622 +static struct ext3_extent_idx *
1623 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1625 + struct ext3_extent_idx *ix;
1627 + ix = EXT_LAST_INDEX(hdr);
1628 + while (ix != EXT_FIRST_INDEX(hdr)) {
1629 + if (ix->e_block <= block)
1637 + * returns 1 if current index have to be freed (even partial)
1640 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1642 + EXT_ASSERT(path->p_idx);
1644 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1648 + * if truncate on deeper level happened it it wasn't partial
1649 + * so we have to consider current index for truncation
1651 + if (path->p_hdr->e_num == path->p_block)
1656 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1657 + unsigned long start, unsigned long end)
1659 + struct inode *inode = tree->inode;
1660 + struct super_block *sb = inode->i_sb;
1661 + int depth = EXT_DEPTH(tree);
1662 + struct ext3_ext_path *path;
1664 + int i = 0, err = 0;
1666 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1668 + /* probably first extent we're gonna free will be last in block */
1669 + handle = ext3_journal_start(inode, depth + 1);
1670 + if (IS_ERR(handle))
1671 + return PTR_ERR(handle);
1673 + ext3_ext_invalidate_cache(tree);
1676 + * we start scanning from right side freeing all the blocks
1677 + * after i_size and walking into the deep
1679 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1680 + if (IS_ERR(path)) {
1681 + ext3_error(sb, "ext3_ext_remove_space",
1682 + "Can't allocate path array");
1683 + ext3_journal_stop(handle, inode);
1686 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1687 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1689 + while (i >= 0 && err == 0) {
1691 + /* this is leaf block */
1692 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1693 + /* root level have p_bh == NULL, brelse() eats this */
1694 + brelse(path[i].p_bh);
1699 + /* this is index block */
1700 + if (!path[i].p_hdr) {
1701 + ext_debug(tree, "initialize header\n");
1702 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1705 + EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1706 + EXT_ASSERT(path[i].p_hdr->e_magic == EXT3_EXT_MAGIC);
1708 + if (!path[i].p_idx) {
1709 + /* this level hasn't touched yet */
1711 + ext3_ext_last_covered(path[i].p_hdr, end);
1712 + path[i].p_block = path[i].p_hdr->e_num + 1;
1713 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1714 + path[i].p_hdr, path[i].p_hdr->e_num);
1716 + /* we've already was here, see at next index */
1720 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1721 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1723 + if (ext3_ext_more_to_rm(path + i)) {
1724 + /* go to the next level */
1725 + ext_debug(tree, "move to level %d (block %d)\n",
1726 + i + 1, path[i].p_idx->e_leaf);
1727 + memset(path + i + 1, 0, sizeof(*path));
1728 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->e_leaf);
1729 + if (!path[i+1].p_bh) {
1730 + /* should we reset i_size? */
1734 + /* put actual number of indexes to know is this
1735 + * number got changed at the next iteration */
1736 + path[i].p_block = path[i].p_hdr->e_num;
1739 + /* we finish processing this index, go up */
1740 + if (path[i].p_hdr->e_num == 0 && i > 0) {
1741 + /* index is empty, remove it
1742 + * handle must be already prepared by the
1743 + * truncate_leaf() */
1744 + err = ext3_ext_rm_idx(handle, tree, path + i);
1746 + /* root level have p_bh == NULL, brelse() eats this */
1747 + brelse(path[i].p_bh);
1749 + ext_debug(tree, "return to level %d\n", i);
1753 + /* TODO: flexible tree reduction should be here */
1754 + if (path->p_hdr->e_num == 0) {
1756 + * truncate to zero freed all the tree
1757 + * so, we need to correct e_depth
1759 + err = ext3_ext_get_access(handle, tree, path);
1761 + EXT_ROOT_HDR(tree)->e_depth = 0;
1762 + EXT_ROOT_HDR(tree)->e_max = ext3_ext_space_root(tree);
1763 + err = ext3_ext_dirty(handle, tree, path);
1766 + ext3_ext_tree_changed(tree);
1769 + ext3_journal_stop(handle, inode);
1775 + * called at mount time
1777 +void ext3_ext_init(struct super_block *sb)
1780 + * possible initialization would be here
1783 + if (test_opt(sb, EXTENTS)) {
1784 + printk("EXT3-fs: file extents enabled");
1785 +#ifdef AGRESSIVE_TEST
1786 + printk(", agressive tests");
1788 +#ifdef CHECK_BINSEARCH
1789 + printk(", check binsearch");
1796 + * called at umount time
1798 +void ext3_ext_release(struct super_block *sb)
1802 +/************************************************************************
1803 + * VFS related routines
1804 + ************************************************************************/
1806 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1808 + /* we use in-core data, not bh */
1812 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1814 + struct inode *inode = buffer;
1815 + return ext3_mark_inode_dirty(handle, inode);
1818 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1819 + struct ext3_extent *ex2)
1821 + if (ex1->e_start + ex1->e_num == ex2->e_start)
1827 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1828 + struct ext3_extent *ex,
1829 + unsigned long from, unsigned long to)
1833 + /* at present, extent can't cross block group */;
1834 + needed = 4; /* bitmap + group desc + sb + inode */
1836 +#ifdef CONFIG_QUOTA
1837 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1843 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1844 + struct ext3_extent *ex,
1845 + unsigned long from, unsigned long to)
1847 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1848 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1849 + struct buffer_head *bh;
1852 + if (IS_ERR(handle))
1853 + return PTR_ERR(handle);
1854 + if (from >= ex->e_block && to == ex->e_block + ex->e_num - 1) {
1855 + /* tail removal */
1856 + unsigned long num, start;
1857 + num = ex->e_block + ex->e_num - from;
1858 + start = ex->e_start + ex->e_num - num;
1859 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1861 + for (i = 0; i < num; i++) {
1862 + bh = sb_get_hash_table(tree->inode->i_sb, start + i);
1863 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1865 + ext3_free_blocks(handle, tree->inode, start, num);
1866 + } else if (from == ex->e_block && to <= ex->e_block + ex->e_num - 1) {
1867 + printk("strange request: removal %lu-%lu from %u:%u\n",
1868 + from, to, ex->e_block, ex->e_num);
1870 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1871 + from, to, ex->e_block, ex->e_num);
1873 + ext3_journal_stop(handle, tree->inode);
1877 +int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
1878 + unsigned long block)
1880 + struct ext3_inode_info *ei = EXT3_I(inode);
1881 + unsigned long bg_start;
1882 + unsigned long colour;
1886 + struct ext3_extent *ex;
1887 + depth = path->p_depth;
1889 + /* try to predict block placement */
1890 + if ((ex = path[depth].p_ext))
1891 + return ex->e_start + (block - ex->e_block);
1893 + /* it looks index is empty
1894 + * try to find starting from index itself */
1895 + if (path[depth].p_bh)
1896 + return path[depth].p_bh->b_blocknr;
1899 + /* OK. use inode's group */
1900 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1901 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1902 + colour = (current->pid % 16) *
1903 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1904 + return bg_start + colour + block;
1907 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1908 + struct ext3_ext_path *path,
1909 + struct ext3_extent *ex, int *err)
1911 + struct inode *inode = tree->inode;
1912 + int newblock, goal;
1916 + EXT_ASSERT(ex->e_start);
1917 + EXT_ASSERT(ex->e_num);
1919 + /* reuse block from the extent to order data/metadata */
1920 + newblock = ex->e_start++;
1922 + if (ex->e_num == 0) {
1924 + /* allocate new block for the extent */
1925 + goal = ext3_ext_find_goal(inode, path, ex->e_block);
1926 + ex->e_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1927 + if (ex->e_start == 0) {
1928 + /* error occured: restore old extent */
1929 + ex->e_start = newblock;
1936 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
1937 + .get_write_access = ext3_get_inode_write_access,
1938 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
1939 + .mergable = ext3_ext_mergable,
1940 + .new_block = ext3_new_block_cb,
1941 + .remove_extent = ext3_remove_blocks,
1942 + .remove_extent_credits = ext3_remove_blocks_credits,
1945 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1946 + struct inode *inode)
1948 + tree->inode = inode;
1949 + tree->root = (void *) EXT3_I(inode)->i_data;
1950 + tree->buffer = (void *) inode;
1951 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1952 + tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1953 + tree->ops = &ext3_blockmap_helpers;
1956 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
1957 + long iblock, struct buffer_head *bh_result, int create)
1959 + struct ext3_ext_path *path = NULL;
1960 + struct ext3_extent newex;
1961 + struct ext3_extent *ex;
1962 + int goal, newblock, err = 0, depth;
1963 + struct ext3_extents_tree tree;
1965 + clear_bit(BH_New, &bh_result->b_state);
1966 + ext3_init_tree_desc(&tree, inode);
1967 + ext_debug(&tree, "block %d requested for inode %u\n",
1968 + (int) iblock, (unsigned) inode->i_ino);
1969 + down_write(&EXT3_I(inode)->truncate_sem);
1971 + /* check in cache */
1972 + if (ext3_ext_in_cache(&tree, iblock, &newex)) {
1973 + if (newex.e_start == 0xffffffff) {
1974 + /* this is cached gap */
1976 + /* block isn't allocated yet and
1977 + * user don't want to allocate it */
1980 + /* we should allocate requested block */
1981 + } else if (newex.e_start) {
1982 + /* block is already allocated */
1983 + newblock = iblock - newex.e_block + newex.e_start;
1988 + /* find extent for this block */
1989 + path = ext3_ext_find_extent(&tree, iblock, NULL);
1990 + if (IS_ERR(path)) {
1991 + err = PTR_ERR(path);
1996 + depth = EXT_DEPTH(&tree);
1999 + * consistent leaf must not be empty
2000 + * this situations is possible, though, _during_ tree modification
2001 + * this is why assert can't be put in ext3_ext_find_extent()
2003 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2005 + if ((ex = path[depth].p_ext)) {
2006 + /* if found exent covers block, simple return it */
2007 + if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
2008 + newblock = iblock - ex->e_block + ex->e_start;
2009 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2010 + (int) iblock, ex->e_block, ex->e_num,
2012 + ext3_ext_put_in_cache(&tree, ex);
2018 + * requested block isn't allocated yet
2019 + * we couldn't try to create block if create flag is zero
2022 + /* put just found gap into cache to speedup subsequest reqs */
2023 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2027 + /* allocate new block */
2028 + goal = ext3_ext_find_goal(inode, path, iblock);
2029 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2032 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2035 + /* try to insert new extent into found leaf and return */
2036 + newex.e_block = iblock;
2037 + newex.e_start = newblock;
2039 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2043 + if (inode->i_size > EXT3_I(inode)->i_disksize)
2044 + EXT3_I(inode)->i_disksize = inode->i_size;
2046 + /* previous routine could use block we allocated */
2047 + newblock = newex.e_start;
2048 + set_bit(BH_New, &bh_result->b_state);
2050 + ext3_ext_put_in_cache(&tree, &newex);
2052 + ext3_ext_show_leaf(&tree, path);
2053 + set_bit(BH_Mapped, &bh_result->b_state);
2054 + bh_result->b_dev = inode->i_sb->s_dev;
2055 + bh_result->b_blocknr = newblock;
2058 + ext3_ext_drop_refs(path);
2061 + up_write(&EXT3_I(inode)->truncate_sem);
2066 +void ext3_ext_truncate(struct inode * inode)
2068 + struct address_space *mapping = inode->i_mapping;
2069 + struct super_block *sb = inode->i_sb;
2070 + struct ext3_extents_tree tree;
2071 + unsigned long last_block;
2075 + ext3_init_tree_desc(&tree, inode);
2078 + * probably first extent we're gonna free will be last in block
2080 + err = ext3_writepage_trans_blocks(inode) + 3;
2081 + handle = ext3_journal_start(inode, err);
2082 + if (IS_ERR(handle))
2085 + ext3_block_truncate_page(handle, mapping, inode->i_size);
2087 + down_write(&EXT3_I(inode)->truncate_sem);
2088 + ext3_ext_invalidate_cache(&tree);
2091 + * TODO: optimization is possible here
2092 + * probably we need not scaning at all,
2093 + * because page truncation is enough
2095 + if (ext3_orphan_add(handle, inode))
2098 + /* we have to know where to truncate from in crash case */
2099 + EXT3_I(inode)->i_disksize = inode->i_size;
2100 + ext3_mark_inode_dirty(handle, inode);
2102 + last_block = (inode->i_size + sb->s_blocksize - 1)
2103 + >> EXT3_BLOCK_SIZE_BITS(sb);
2104 + err = ext3_ext_remove_space(&tree, last_block, 0xffffffff);
2106 + /* In a multi-transaction truncate, we only make the final
2107 + * transaction synchronous */
2108 + if (IS_SYNC(inode))
2109 + handle->h_sync = 1;
2113 + * If this was a simple ftruncate(), and the file will remain alive
2114 + * then we need to clear up the orphan record which we created above.
2115 + * However, if this was a real unlink then we were called by
2116 + * ext3_delete_inode(), and we allow that function to clean up the
2117 + * orphan info for us.
2119 + if (inode->i_nlink)
2120 + ext3_orphan_del(handle, inode);
2122 + up_write(&EXT3_I(inode)->truncate_sem);
2123 + ext3_journal_stop(handle, inode);
2127 + * this routine calculate max number of blocks we could modify
2128 + * in order to allocate new block for an inode
2130 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2132 + struct ext3_extents_tree tree;
2135 + ext3_init_tree_desc(&tree, inode);
2137 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2139 + /* caller want to allocate num blocks */
2142 +#ifdef CONFIG_QUOTA
2144 + * FIXME: real calculation should be here
2145 + * it depends on blockmap format of qouta file
2147 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2153 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2155 + struct ext3_extents_tree tree;
2157 + ext3_init_tree_desc(&tree, inode);
2158 + ext3_extent_tree_init(handle, &tree);
2162 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2163 + struct ext3_ext_path *path,
2164 + struct ext3_extent *newex, int exist)
2166 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2169 + return EXT_CONTINUE;
2172 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2175 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2177 + buf->cur += sizeof(*newex);
2179 + buf->err = -EFAULT;
2182 + return EXT_CONTINUE;
2186 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2187 + struct ext3_ext_path *path,
2188 + struct ext3_extent *ex, int exist)
2190 + struct ext3_extent_tree_stats *buf =
2191 + (struct ext3_extent_tree_stats *) tree->private;
2195 + return EXT_CONTINUE;
2197 + depth = EXT_DEPTH(tree);
2198 + buf->extents_num++;
2199 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2201 + return EXT_CONTINUE;
2204 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2205 + unsigned long arg)
2209 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2212 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2213 + struct ext3_extent_buf buf;
2214 + struct ext3_extents_tree tree;
2216 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2219 + ext3_init_tree_desc(&tree, inode);
2220 + buf.cur = buf.buffer;
2222 + tree.private = &buf;
2223 + down_write(&EXT3_I(inode)->truncate_sem);
2224 + err = ext3_ext_walk_space(&tree, buf.start, 0xffffffff,
2225 + ext3_ext_store_extent_cb);
2226 + up_write(&EXT3_I(inode)->truncate_sem);
2229 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2230 + struct ext3_extent_tree_stats buf;
2231 + struct ext3_extents_tree tree;
2233 + ext3_init_tree_desc(&tree, inode);
2234 + down_write(&EXT3_I(inode)->truncate_sem);
2235 + buf.depth = EXT_DEPTH(&tree);
2236 + buf.extents_num = 0;
2238 + tree.private = &buf;
2239 + err = ext3_ext_walk_space(&tree, 0, 0xffffffff,
2240 + ext3_ext_collect_stats_cb);
2241 + up_write(&EXT3_I(inode)->truncate_sem);
2243 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2244 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2245 + struct ext3_extents_tree tree;
2246 + ext3_init_tree_desc(&tree, inode);
2247 + down_write(&EXT3_I(inode)->truncate_sem);
2248 + err = EXT_DEPTH(&tree);
2249 + up_write(&EXT3_I(inode)->truncate_sem);
2255 +EXPORT_SYMBOL(ext3_init_tree_desc);
2256 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2257 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2258 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2259 +EXPORT_SYMBOL(ext3_ext_walk_space);
2260 +EXPORT_SYMBOL(ext3_ext_find_goal);
2261 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2263 Index: linux-2.4.24/fs/ext3/ialloc.c
2264 ===================================================================
2265 --- linux-2.4.24.orig/fs/ext3/ialloc.c 2004-08-06 00:47:18.000000000 +0400
2266 +++ linux-2.4.24/fs/ext3/ialloc.c 2004-08-06 02:43:24.000000000 +0400
2267 @@ -593,10 +593,14 @@
2271 - err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2272 - if (err) goto fail;
2274 + if (test_opt(sb, EXTENTS)) {
2275 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2276 + ext3_extents_initialize_blockmap(handle, inode);
2279 + err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2280 + if (err) goto fail;
2283 if(DQUOT_ALLOC_INODE(inode)) {
2284 Index: linux-2.4.24/fs/ext3/inode.c
2285 ===================================================================
2286 --- linux-2.4.24.orig/fs/ext3/inode.c 2004-08-06 00:47:18.000000000 +0400
2287 +++ linux-2.4.24/fs/ext3/inode.c 2004-08-06 02:43:24.000000000 +0400
2288 @@ -848,6 +848,15 @@
2293 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2294 + struct buffer_head *bh, int create)
2296 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2297 + return ext3_ext_get_block(handle, inode, block, bh, create);
2298 + return ext3_get_block_handle(handle, inode, block, bh, create);
2302 * The BKL is not held on entry here.
2305 handle = ext3_journal_current_handle();
2306 J_ASSERT(handle != 0);
2308 - ret = ext3_get_block_handle(handle, inode, iblock, bh_result, create);
2309 + ret = ext3_get_block_wrap(handle, inode, iblock, bh_result, create);
2315 dummy.b_blocknr = -1000;
2316 buffer_trace_init(&dummy.b_history);
2317 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create);
2318 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create);
2319 if (!*errp && buffer_mapped(&dummy)) {
2320 struct buffer_head *bh;
2321 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2322 @@ -1403,7 +1412,7 @@
2323 * This required during truncate. We need to physically zero the tail end
2324 * of that block so it doesn't yield old data if the file is later grown.
2326 -static int ext3_block_truncate_page(handle_t *handle,
2327 +int ext3_block_truncate_page(handle_t *handle,
2328 struct address_space *mapping, loff_t from)
2330 unsigned long index = from >> PAGE_CACHE_SHIFT;
2331 @@ -1888,6 +1897,9 @@
2333 ext3_discard_prealloc(inode);
2335 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2336 + return ext3_ext_truncate(inode);
2338 handle = start_transaction(inode);
2340 return; /* AKPM: return what? */
2341 @@ -2547,6 +2559,9 @@
2342 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2345 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2346 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2348 if (ext3_should_journal_data(inode))
2349 ret = 3 * (bpp + indirects) + 2;
2351 @@ -2983,7 +2998,7 @@
2353 /* alloc blocks one by one */
2354 for (i = 0; i < nblocks; i++) {
2355 - ret = ext3_get_block_handle(handle, inode, blocks[i],
2356 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
2360 @@ -3059,7 +3074,7 @@
2364 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1);
2365 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1);
2367 printk(KERN_INFO "ext3_map_inode_page: error %d "
2368 "allocating block %ld\n", rc, iblock);
2369 Index: linux-2.4.24/fs/ext3/Makefile
2370 ===================================================================
2371 --- linux-2.4.24.orig/fs/ext3/Makefile 2004-08-06 00:47:18.000000000 +0400
2372 +++ linux-2.4.24/fs/ext3/Makefile 2004-08-06 02:43:24.000000000 +0400
2375 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2376 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2378 + xattr_trusted.o extents.o
2379 +export-objs += extents.o
2381 obj-m := $(O_TARGET)
2383 export-objs += xattr.o
2384 Index: linux-2.4.24/fs/ext3/super.c
2385 ===================================================================
2386 --- linux-2.4.24.orig/fs/ext3/super.c 2004-08-06 00:47:18.000000000 +0400
2387 +++ linux-2.4.24/fs/ext3/super.c 2004-08-06 02:43:24.000000000 +0400
2391 J_ASSERT(sbi->s_delete_inodes == 0);
2392 + ext3_ext_release(sb);
2393 ext3_xattr_put_super(sb);
2394 journal_destroy(sbi->s_journal);
2395 if (!(sb->s_flags & MS_RDONLY)) {
2396 @@ -702,6 +703,10 @@
2400 + else if (!strcmp (this_char, "extents"))
2401 + set_opt (*mount_options, EXTENTS);
2402 + else if (!strcmp (this_char, "extdebug"))
2403 + set_opt (*mount_options, EXTDEBUG);
2404 else if (!strcmp (this_char, "grpid") ||
2405 !strcmp (this_char, "bsdgroups"))
2406 set_opt (*mount_options, GRPID);
2407 @@ -1393,6 +1398,8 @@
2408 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2411 + ext3_ext_init(sb);
2416 Index: linux-2.4.24/fs/ext3/ioctl.c
2417 ===================================================================
2418 --- linux-2.4.24.orig/fs/ext3/ioctl.c 2004-08-06 00:47:17.000000000 +0400
2419 +++ linux-2.4.24/fs/ext3/ioctl.c 2004-08-06 02:43:24.000000000 +0400
2420 @@ -174,6 +174,10 @@
2424 + case EXT3_IOC_GET_EXTENTS:
2425 + case EXT3_IOC_GET_TREE_STATS:
2426 + case EXT3_IOC_GET_TREE_DEPTH:
2427 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2431 Index: linux-2.4.24/include/linux/ext3_fs.h
2432 ===================================================================
2433 --- linux-2.4.24.orig/include/linux/ext3_fs.h 2004-08-06 00:47:18.000000000 +0400
2434 +++ linux-2.4.24/include/linux/ext3_fs.h 2004-08-06 02:44:11.000000000 +0400
2436 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2437 #define EXT3_EXTRA_ISIZE 0x00008000 /* inode has initialized i_extra_isize */
2438 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2439 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2441 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2442 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2444 #ifdef CONFIG_JBD_DEBUG
2445 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2447 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 5, long)
2448 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 6, long)
2449 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 7, long)
2452 * Structure of an inode on the disk
2454 #define EXT3_MOUNT_IOPEN 0x8000 /* Allow access via iopen */
2455 #define EXT3_MOUNT_IOPEN_NOPRIV 0x10000 /* Make iopen world-readable */
2456 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
2457 +#define EXT3_MOUNT_EXTENTS 0x40000 /* Extents support */
2458 +#define EXT3_MOUNT_EXTDEBUG 0x80000 /* Extents debug */
2460 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2461 #ifndef _LINUX_EXT2_FS_H
2463 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2466 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2467 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2468 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2469 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2470 @@ -770,6 +777,14 @@
2471 extern struct inode_operations ext3_symlink_inode_operations;
2472 extern struct inode_operations ext3_fast_symlink_inode_operations;
2475 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2476 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2477 + struct buffer_head *, int);
2478 +extern void ext3_ext_truncate(struct inode *);
2479 +extern void ext3_ext_init(struct super_block *);
2480 +extern void ext3_ext_release(struct super_block *);
2481 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2483 #endif /* __KERNEL__ */
2485 Index: linux-2.4.24/include/linux/ext3_extents.h
2486 ===================================================================
2487 --- linux-2.4.24.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2488 +++ linux-2.4.24/include/linux/ext3_extents.h 2004-08-06 03:52:38.000000000 +0400
2491 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
2493 + * This program is free software; you can redistribute it and/or modify
2494 + * it under the terms of the GNU General Public License version 2 as
2495 + * published by the Free Software Foundation.
2497 + * This program is distributed in the hope that it will be useful,
2498 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2499 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2500 + * GNU General Public License for more details.
2502 + * You should have received a copy of the GNU General Public Licens
2503 + * along with this program; if not, write to the Free Software
2504 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2507 +#ifndef _LINUX_EXT3_EXTENTS
2508 +#define _LINUX_EXT3_EXTENTS
2511 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2512 + * become very little, so index split, in-depth growing and
2513 + * other hard changes happens much more often
2514 + * this is for debug purposes only
2516 +#define AGRESSIVE_TEST_
2519 + * if CHECK_BINSEARCH defined, then results of binary search
2520 + * will be checked by linear search
2522 +#define CHECK_BINSEARCH_
2525 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2526 + * to get lots of info what's going on
2530 +#define ext_debug(tree,fmt,a...) \
2532 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2533 + printk(fmt, ##a); \
2536 +#define ext_debug(tree,fmt,a...)
2540 + * if EXT_STATS is defined then stats numbers are collected
2541 + * these number will be displayed at umount time
2546 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2549 + * ext3_inode has i_block array (total 60 bytes)
2550 + * first 4 bytes are used to store:
2551 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2552 + * - number of alive extents in the inode
2556 + * this is extent on-disk structure
2557 + * it's used at the bottom of the tree
2559 +struct ext3_extent {
2560 + __u32 e_block; /* first logical block extent covers */
2561 + __u32 e_start; /* first physical block extents lives */
2562 + __u32 e_num; /* number of blocks covered by extent */
2566 + * this is index on-disk structure
2567 + * it's used at all the levels, but the bottom
2569 +struct ext3_extent_idx {
2570 + __u32 e_block; /* index covers logical blocks from 'block' */
2571 + __u32 e_leaf; /* pointer to the physical block of the next *
2572 + * level. leaf or next index could bet here */
2576 + * each block (leaves and indexes), even inode-stored has header
2578 +struct ext3_extent_header {
2579 + __u16 e_magic; /* probably will support different formats */
2580 + __u16 e_num; /* number of valid entries */
2581 + __u16 e_max; /* capacity of store in entries */
2582 + __u16 e_depth; /* has tree real underlaying blocks? */
2583 + __u32 e_generation; /* generation of the tree */
2586 +#define EXT3_EXT_MAGIC 0xf301
2589 + * array of ext3_ext_path contains path to some extent
2590 + * creation/lookup routines use it for traversal/splitting/etc
2591 + * truncate uses it to simulate recursive walking
2593 +struct ext3_ext_path {
2596 + struct ext3_extent *p_ext;
2597 + struct ext3_extent_idx *p_idx;
2598 + struct ext3_extent_header *p_hdr;
2599 + struct buffer_head *p_bh;
2603 + * structure for external API
2607 + * ext3_extents_tree is used to pass initial information
2608 + * to top-level extents API
2610 +struct ext3_extents_helpers;
2611 +struct ext3_extents_tree {
2612 + struct inode *inode; /* inode which tree belongs to */
2613 + void *root; /* ptr to data top of tree resides at */
2614 + void *buffer; /* will be passed as arg to ^^ routines */
2617 + struct ext3_extent *cex;/* last found extent */
2618 + struct ext3_extents_helpers *ops;
2621 +struct ext3_extents_helpers {
2622 + int (*get_write_access)(handle_t *h, void *buffer);
2623 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2624 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2625 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2626 + struct ext3_extent *, unsigned long,
2628 + int (*remove_extent)(struct ext3_extents_tree *,
2629 + struct ext3_extent *, unsigned long,
2631 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2632 + struct ext3_ext_path *, struct ext3_extent *,
2637 + * to be called by ext3_ext_walk_space()
2638 + * negative retcode - error
2639 + * positive retcode - signal for ext3_ext_walk_space(), see below
2640 + * callback must return valid extent (passed or newly created)
2642 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2643 + struct ext3_ext_path *,
2644 + struct ext3_extent *, int);
2646 +#define EXT_CONTINUE 0
2647 +#define EXT_BREAK 1
2648 +#define EXT_REPEAT 2
2651 +#define EXT_FIRST_EXTENT(__hdr__) \
2652 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2653 + sizeof(struct ext3_extent_header)))
2654 +#define EXT_FIRST_INDEX(__hdr__) \
2655 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2656 + sizeof(struct ext3_extent_header)))
2657 +#define EXT_HAS_FREE_INDEX(__path__) \
2658 + ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
2659 +#define EXT_LAST_EXTENT(__hdr__) \
2660 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
2661 +#define EXT_LAST_INDEX(__hdr__) \
2662 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
2663 +#define EXT_MAX_EXTENT(__hdr__) \
2664 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
2665 +#define EXT_MAX_INDEX(__hdr__) \
2666 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
2668 +#define EXT_ROOT_HDR(tree) \
2669 + ((struct ext3_extent_header *) (tree)->root)
2670 +#define EXT_BLOCK_HDR(bh) \
2671 + ((struct ext3_extent_header *) (bh)->b_data)
2672 +#define EXT_DEPTH(_t_) \
2673 + (((struct ext3_extent_header *)((_t_)->root))->e_depth)
2674 +#define EXT_GENERATION(_t_) \
2675 + (((struct ext3_extent_header *)((_t_)->root))->e_generation)
2678 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2682 + * this structure is used to gather extents from the tree via ioctl
2684 +struct ext3_extent_buf {
2685 + unsigned long start;
2693 + * this structure is used to collect stats info about the tree
2695 +struct ext3_extent_tree_stats {
2701 +void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
2702 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2703 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2704 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2705 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2706 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2707 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2708 +extern int ext3_ext_in_ea_alloc_space(struct inode *inode, int name_index, const char *eaname, unsigned long from,
2709 + unsigned long num);
2710 +extern int ext3_ext_in_ea_remove_space(struct inode *inode, int name_index, const char *eaname, unsigned long from,
2711 + unsigned long num);
2712 +extern int ext3_ext_in_ea_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
2713 +extern int ext3_ext_in_ea_get_extents(struct inode *inode, int name_index, const char *eaname, char **buf, int *size);
2714 +extern int ext3_ext_in_ea_get_extents_num(struct inode *inode, int name_index, const char *eaname, int *size);
2717 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
2720 + tree->cex->e_num = 0;
2724 +#endif /* _LINUX_EXT3_EXTENTS */
2726 Index: linux-2.4.24/include/linux/ext3_fs_i.h
2727 ===================================================================
2728 --- linux-2.4.24.orig/include/linux/ext3_fs_i.h 2004-08-06 00:47:18.000000000 +0400
2729 +++ linux-2.4.24/include/linux/ext3_fs_i.h 2004-08-06 02:43:24.000000000 +0400
2731 * by other means, so we have truncate_sem.
2733 struct rw_semaphore truncate_sem;
2735 + __u32 i_cached_extent[3];
2738 #endif /* _LINUX_EXT3_FS_I */