1 Index: linux-2.4.21-suse2/fs/ext3/extents.c
2 ===================================================================
3 --- linux-2.4.21-suse2.orig/fs/ext3/extents.c 2003-01-30 13:24:37.000000000 +0300
4 +++ linux-2.4.21-suse2/fs/ext3/extents.c 2004-01-26 22:51:30.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)
92 + /* path points to block */
93 + return ext3_journal_get_write_access(handle, path->p_bh);
96 + /* path points to leaf/index in inode body */
97 + return ext3_ext_get_access_for_root(handle, tree);
106 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
107 + struct ext3_ext_path *path)
110 + /* path points to block */
111 + return ext3_journal_dirty_metadata(handle, path->p_bh);
114 + /* path points to leaf/index in inode body */
115 + return ext3_ext_mark_root_dirty(handle, tree);
119 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
120 + struct ext3_ext_path *path, struct ext3_extent *ex,
123 + int goal, depth, newblock;
124 + struct inode *inode;
127 + if (tree->new_block)
128 + return tree->new_block(handle, tree, path, ex, err);
130 + inode = tree->inode;
131 + depth = EXT_DEPTH(tree);
132 + if (path && depth > 0) {
133 + goal = path[depth-1].p_block;
135 + struct ext3_inode_info *ei = EXT3_I(inode);
136 + unsigned long bg_start;
137 + unsigned long colour;
139 + bg_start = (ei->i_block_group *
140 + EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
141 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
142 + colour = (current->pid % 16) *
143 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
144 + goal = bg_start + colour;
147 + newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
151 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
155 + size = (tree->inode->i_sb->s_blocksize -
156 + sizeof(struct ext3_extent_header))
157 + / sizeof(struct ext3_extent);
158 +#ifdef AGRESSIVE_TEST
164 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
168 + size = (tree->inode->i_sb->s_blocksize -
169 + sizeof(struct ext3_extent_header))
170 + / sizeof(struct ext3_extent_idx);
171 +#ifdef AGRESSIVE_TEST
177 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
181 + size = (tree->buffer_len - sizeof(struct ext3_extent_header))
182 + / sizeof(struct ext3_extent);
183 +#ifdef AGRESSIVE_TEST
189 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
193 + size = (tree->buffer_len -
194 + sizeof(struct ext3_extent_header))
195 + / sizeof(struct ext3_extent_idx);
196 +#ifdef AGRESSIVE_TEST
202 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
203 + struct ext3_ext_path *path)
206 + int k, l = path->p_depth;
208 + ext_debug(tree, "path:");
209 + for (k = 0; k <= l; k++, path++) {
211 + ext_debug(tree, " %d->%d", path->p_idx->e_block,
212 + path->p_idx->e_leaf);
213 + } else if (path->p_ext) {
214 + ext_debug(tree, " %d:%d:%d",
215 + path->p_ext->e_block,
216 + path->p_ext->e_num,
217 + path->p_ext->e_start);
219 + ext_debug(tree, " []");
221 + ext_debug(tree, "\n");
225 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
226 + struct ext3_ext_path *path)
229 + int depth = EXT_DEPTH(tree);
230 + struct ext3_extent_header *eh;
231 + struct ext3_extent *ex;
237 + eh = path[depth].p_hdr;
238 + ex = EXT_FIRST_EXTENT(eh);
240 + for (i = 0; i < eh->e_num; i++, ex++) {
241 + ext_debug(tree, "%d:%d:%d ",
242 + ex->e_block, ex->e_num, ex->e_start);
244 + ext_debug(tree, "\n");
248 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
250 + int depth = path->p_depth;
253 + for (i = 0; i <= depth; i++, path++)
255 + brelse(path->p_bh);
261 + * binary search for closest index by given block
264 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
265 + struct ext3_ext_path *path, int block)
267 + struct ext3_extent_header *eh = path->p_hdr;
268 + struct ext3_extent_idx *ix;
271 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
272 + EXT_ASSERT(eh->e_num <= eh->e_max);
273 + EXT_ASSERT(eh->e_num > 0);
275 + ext_debug(tree, "binsearch for %d(idx): ", block);
277 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
282 + if (block < ix[l + k].e_block)
286 + ext_debug(tree, "%d:%d:%d ", k, l, r);
291 + ext_debug(tree, " -> %d->%d ", path->p_idx->e_block, path->p_idx->e_leaf);
294 + if (block < ix->e_block)
296 + path->p_idx = ix++;
298 + ext_debug(tree, " -> %d->%d\n", path->p_idx->e_block,
299 + path->p_idx->e_leaf);
301 +#ifdef CHECK_BINSEARCH
303 + struct ext3_extent_idx *chix;
305 + chix = ix = EXT_FIRST_INDEX(eh);
306 + for (k = 0; k < eh->e_num; k++, ix++) {
307 + if (k != 0 && ix->e_block <= ix[-1].e_block) {
308 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
309 + ix, EXT_FIRST_INDEX(eh));
310 + printk("%u <= %u\n",
311 + ix->e_block,ix[-1].e_block);
313 + EXT_ASSERT(k == 0 || ix->e_block > ix[-1].e_block);
314 + if (block < ix->e_block)
318 + EXT_ASSERT(chix == path->p_idx);
325 + * binary search for closest extent by given block
328 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
329 + struct ext3_ext_path *path, int block)
331 + struct ext3_extent_header *eh = path->p_hdr;
332 + struct ext3_extent *ex;
335 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
336 + EXT_ASSERT(eh->e_num <= eh->e_max);
338 + if (eh->e_num == 0) {
340 + * this leaf is empty yet:
341 + * we get such a leaf in split/add case
346 + ext_debug(tree, "binsearch for %d: ", block);
348 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
353 + if (block < ex[l + k].e_block)
357 + ext_debug(tree, "%d:%d:%d ", k, l, r);
362 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->e_block,
363 + path->p_ext->e_start, path->p_ext->e_num);
366 + if (block < ex->e_block)
368 + path->p_ext = ex++;
370 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->e_block,
371 + path->p_ext->e_start, path->p_ext->e_num);
373 +#ifdef CHECK_BINSEARCH
375 + struct ext3_extent *chex;
377 + chex = ex = EXT_FIRST_EXTENT(eh);
378 + for (k = 0; k < eh->e_num; k++, ex++) {
379 + EXT_ASSERT(k == 0 || ex->e_block > ex[-1].e_block);
380 + if (block < ex->e_block)
384 + EXT_ASSERT(chex == path->p_ext);
390 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
392 + struct ext3_extent_header *eh;
394 + BUG_ON(tree->buffer_len == 0);
395 + ext3_ext_get_access_for_root(handle, tree);
396 + eh = EXT_ROOT_HDR(tree);
399 + eh->e_magic = EXT3_EXT_MAGIC;
400 + eh->e_max = ext3_ext_space_root(tree);
401 + ext3_ext_mark_root_dirty(handle, tree);
405 +struct ext3_ext_path *
406 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
407 + struct ext3_ext_path *path)
409 + struct ext3_extent_header *eh;
410 + struct buffer_head *bh;
411 + int depth, i, ppos = 0;
414 + EXT_ASSERT(tree->inode);
415 + EXT_ASSERT(tree->root);
417 + eh = EXT_ROOT_HDR(tree);
418 + i = depth = EXT_DEPTH(tree);
419 + EXT_ASSERT(eh->e_max);
420 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
421 + EXT_ASSERT(i == 0 || eh->e_num > 0);
423 + /* account possible depth increase */
425 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
428 + return ERR_PTR(-ENOMEM);
430 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
431 + path[0].p_hdr = eh;
433 + /* walk through the tree */
435 + ext_debug(tree, "depth %d: num %d, max %d\n",
436 + ppos, eh->e_num, eh->e_max);
437 + ext3_ext_binsearch_idx(tree, path + ppos, block);
438 + path[ppos].p_block = path[ppos].p_idx->e_leaf;
439 + path[ppos].p_depth = i;
440 + path[ppos].p_ext = NULL;
442 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
444 + ext3_ext_drop_refs(path);
446 + return ERR_PTR(-EIO);
448 + eh = EXT_BLOCK_HDR(bh);
450 + EXT_ASSERT(ppos <= depth);
451 + path[ppos].p_bh = bh;
452 + path[ppos].p_hdr = eh;
456 + path[ppos].p_depth = i;
457 + path[ppos].p_hdr = eh;
458 + path[ppos].p_ext = NULL;
461 + ext3_ext_binsearch(tree, path + ppos, block);
463 + ext3_ext_show_path(tree, path);
469 + * insert new index [logical;ptr] into the block at cupr
470 + * it check where to insert: before curp or after curp
472 +static int ext3_ext_insert_index(handle_t *handle,
473 + struct ext3_extents_tree *tree,
474 + struct ext3_ext_path *curp,
475 + int logical, int ptr)
477 + struct ext3_extent_idx *ix;
480 + if ((err = ext3_ext_get_access(handle, tree, curp)))
483 + EXT_ASSERT(logical != curp->p_idx->e_block);
484 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
485 + if (logical > curp->p_idx->e_block) {
487 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
488 + len = (len - 1) * sizeof(struct ext3_extent_idx);
489 + len = len < 0 ? 0 : len;
490 + ext_debug(tree, "insert new index %d after: %d. "
491 + "move %d from 0x%p to 0x%p\n",
493 + (curp->p_idx + 1), (curp->p_idx + 2));
494 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
496 + ix = curp->p_idx + 1;
498 + /* insert before */
499 + len = len * sizeof(struct ext3_extent_idx);
500 + len = len < 0 ? 0 : len;
501 + ext_debug(tree, "insert new index %d before: %d. "
502 + "move %d from 0x%p to 0x%p\n",
504 + curp->p_idx, (curp->p_idx + 1));
505 + memmove(curp->p_idx + 1, curp->p_idx, len);
509 + ix->e_block = logical;
511 + curp->p_hdr->e_num++;
513 + EXT_ASSERT(curp->p_hdr->e_num <= curp->p_hdr->e_max);
514 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
516 + err = ext3_ext_dirty(handle, tree, curp);
517 + ext3_std_error(tree->inode->i_sb, err);
523 + * routine inserts new subtree into the path, using free index entry
525 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
526 + * - makes decision where to split
527 + * - moves remaining extens and index entries (right to the split point)
528 + * into the newly allocated blocks
529 + * - initialize subtree
531 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
532 + struct ext3_ext_path *path,
533 + struct ext3_extent *newext, int at)
535 + struct buffer_head *bh = NULL;
536 + int depth = EXT_DEPTH(tree);
537 + struct ext3_extent_header *neh;
538 + struct ext3_extent_idx *fidx;
539 + struct ext3_extent *ex;
540 + int i = at, k, m, a;
541 + unsigned long newblock, oldblock, border;
542 + int *ablocks = NULL; /* array of allocated blocks */
545 + /* make decision: where to split? */
546 + /* FIXME: now desicion is simplest: at current extent */
548 + /* if current leaf will be splitted, then we should use
549 + * border from split point */
550 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
551 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
552 + border = path[depth].p_ext[1].e_block;
553 + ext_debug(tree, "leaf will be splitted."
554 + " next leaf starts at %d\n",
557 + border = newext->e_block;
558 + ext_debug(tree, "leaf will be added."
559 + " next leaf starts at %d\n",
564 + * if error occurs, then we break processing
565 + * and turn filesystem read-only. so, index won't
566 + * be inserted and tree will be in consistent
567 + * state. next mount will repair buffers too
571 + * get array to track all allocated blocks
572 + * we need this to handle errors and free blocks
575 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
578 + memset(ablocks, 0, sizeof(unsigned long) * depth);
580 + /* allocate all needed blocks */
581 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
582 + for (a = 0; a < depth - at; a++) {
583 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
586 + ablocks[a] = newblock;
589 + /* initialize new leaf */
590 + newblock = ablocks[--a];
591 + EXT_ASSERT(newblock);
592 + bh = sb_getblk(tree->inode->i_sb, newblock);
599 + if ((err = ext3_journal_get_create_access(handle, bh)))
602 + neh = EXT_BLOCK_HDR(bh);
604 + neh->e_max = ext3_ext_space_block(tree);
605 + neh->e_magic = EXT3_EXT_MAGIC;
607 + ex = EXT_FIRST_EXTENT(neh);
609 + /* move remain of path[depth] to the new leaf */
610 + EXT_ASSERT(path[depth].p_hdr->e_num == path[depth].p_hdr->e_max);
611 + /* start copy from next extent */
612 + /* TODO: we could do it by single memmove */
614 + path[depth].p_ext++;
615 + while (path[depth].p_ext <=
616 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
617 + ext_debug(tree, "move %d:%d:%d in new leaf\n",
618 + path[depth].p_ext->e_block,
619 + path[depth].p_ext->e_start,
620 + path[depth].p_ext->e_num);
621 + memmove(ex++, path[depth].p_ext++,
622 + sizeof(struct ext3_extent));
626 + mark_buffer_uptodate(bh, 1);
629 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
634 + /* correct old leaf */
636 + if ((err = ext3_ext_get_access(handle, tree, path)))
638 + path[depth].p_hdr->e_num -= m;
639 + if ((err = ext3_ext_dirty(handle, tree, path)))
644 + /* create intermediate indexes */
645 + k = depth - at - 1;
646 + EXT_ASSERT(k >= 0);
648 + ext_debug(tree, "create %d intermediate indices\n", k);
649 + /* insert new index into current index block */
650 + /* current depth stored in i var */
653 + oldblock = newblock;
654 + newblock = ablocks[--a];
655 + bh = sb_getblk(tree->inode->i_sb, newblock);
662 + if ((err = ext3_journal_get_create_access(handle, bh)))
665 + neh = EXT_BLOCK_HDR(bh);
667 + neh->e_magic = EXT3_EXT_MAGIC;
668 + neh->e_max = ext3_ext_space_block_idx(tree);
669 + neh->e_depth = depth - i;
670 + fidx = EXT_FIRST_INDEX(neh);
671 + fidx->e_block = border;
672 + fidx->e_leaf = oldblock;
674 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
675 + i, newblock, border, oldblock);
680 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
681 + EXT_MAX_INDEX(path[i].p_hdr));
682 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
683 + EXT_LAST_INDEX(path[i].p_hdr));
684 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
685 + ext_debug(tree, "%d: move %d:%d in new index\n",
686 + i, path[i].p_idx->e_block,
687 + path[i].p_idx->e_leaf);
688 + memmove(++fidx, path[i].p_idx++,
689 + sizeof(struct ext3_extent_idx));
691 + EXT_ASSERT(neh->e_num <= neh->e_max);
694 + mark_buffer_uptodate(bh, 1);
697 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
702 + /* correct old index */
704 + err = ext3_ext_get_access(handle, tree, path + i);
707 + path[i].p_hdr->e_num -= m;
708 + err = ext3_ext_dirty(handle, tree, path + i);
716 + /* insert new index */
718 + err = ext3_ext_insert_index(handle, tree, path + at,
723 + if (buffer_locked(bh))
729 + /* free all allocated blocks in error case */
730 + for (i = 0; i < depth; i++)
733 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
741 + * routine implements tree growing procedure:
742 + * - allocates new block
743 + * - moves top-level data (index block or leaf) into the new block
744 + * - initialize new top-level, creating index that points to the
745 + * just created block
747 +static int ext3_ext_grow_indepth(handle_t *handle,
748 + struct ext3_extents_tree *tree,
749 + struct ext3_ext_path *path,
750 + struct ext3_extent *newext)
752 + struct ext3_ext_path *curp = path;
753 + struct ext3_extent_header *neh;
754 + struct ext3_extent_idx *fidx;
755 + struct buffer_head *bh;
756 + unsigned long newblock;
759 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
763 + bh = sb_getblk(tree->inode->i_sb, newblock);
766 + ext3_std_error(tree->inode->i_sb, err);
771 + if ((err = ext3_journal_get_create_access(handle, bh))) {
776 + /* move top-level index/leaf into new block */
777 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
779 + /* set size of new block */
780 + neh = EXT_BLOCK_HDR(bh);
781 + /* old root could have indexes or leaves
782 + * so calculate e_max right way */
783 + if (EXT_DEPTH(tree))
784 + neh->e_max = ext3_ext_space_block_idx(tree);
786 + neh->e_max = ext3_ext_space_block(tree);
787 + neh->e_magic = EXT3_EXT_MAGIC;
788 + mark_buffer_uptodate(bh, 1);
791 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
794 + /* create index in new top-level index: num,max,pointer */
795 + if ((err = ext3_ext_get_access(handle, tree, curp)))
798 + curp->p_hdr->e_magic = EXT3_EXT_MAGIC;
799 + curp->p_hdr->e_max = ext3_ext_space_root_idx(tree);
800 + curp->p_hdr->e_num = 1;
801 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
802 + /* FIXME: it works, but actually path[0] can be index */
803 + curp->p_idx->e_block = EXT_FIRST_EXTENT(path[0].p_hdr)->e_block;
804 + curp->p_idx->e_leaf = newblock;
806 + neh = EXT_ROOT_HDR(tree);
807 + fidx = EXT_FIRST_INDEX(neh);
808 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
809 + neh->e_num, neh->e_max, fidx->e_block, fidx->e_leaf);
811 + neh->e_depth = path->p_depth + 1;
812 + err = ext3_ext_dirty(handle, tree, curp);
820 + * routine finds empty index and adds new leaf. if no free index found
821 + * then it requests in-depth growing
823 +static int ext3_ext_create_new_leaf(handle_t *handle,
824 + struct ext3_extents_tree *tree,
825 + struct ext3_ext_path *path,
826 + struct ext3_extent *newext)
828 + struct ext3_ext_path *curp;
829 + int depth, i, err = 0;
832 + i = depth = EXT_DEPTH(tree);
834 + /* walk up to the tree and look for free index entry */
835 + curp = path + depth;
836 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
841 + /* we use already allocated block for index block
842 + * so, subsequent data blocks should be contigoues */
843 + if (EXT_HAS_FREE_INDEX(curp)) {
844 + /* if we found index with free entry, then use that
845 + * entry: create all needed subtree and add new leaf */
846 + err = ext3_ext_split(handle, tree, path, newext, i);
849 + ext3_ext_drop_refs(path);
850 + path = ext3_ext_find_extent(tree, newext->e_block, path);
852 + err = PTR_ERR(path);
854 + /* tree is full, time to grow in depth */
855 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
858 + ext3_ext_drop_refs(path);
859 + path = ext3_ext_find_extent(tree, newext->e_block, path);
861 + err = PTR_ERR(path);
864 + * only first (depth 0 -> 1) produces free space
865 + * in all other cases we have to split growed tree
867 + depth = EXT_DEPTH(tree);
868 + if (path[depth].p_hdr->e_num == path[depth].p_hdr->e_max) {
869 + /* now we need split */
881 + * returns allocated block in subsequent extent or 0xffffffff
882 + * NOTE: it consider block number from index entry as
883 + * allocated block. thus, index entries have to be consistent
886 +static unsigned long
887 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
891 + EXT_ASSERT(path != NULL);
892 + depth = path->p_depth;
894 + if (depth == 0 && path->p_ext == NULL)
897 + /* FIXME: what if index isn't full ?! */
898 + while (depth >= 0) {
899 + if (depth == path->p_depth) {
901 + if (path[depth].p_ext !=
902 + EXT_LAST_EXTENT(path[depth].p_hdr))
903 + return path[depth].p_ext[1].e_block;
906 + if (path[depth].p_idx !=
907 + EXT_LAST_INDEX(path[depth].p_hdr))
908 + return path[depth].p_idx[1].e_block;
917 + * returns first allocated block from next leaf or 0xffffffff
919 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
920 + struct ext3_ext_path *path)
924 + EXT_ASSERT(path != NULL);
925 + depth = path->p_depth;
927 + /* zero-tree has no leaf blocks at all */
931 + /* go to index block */
934 + while (depth >= 0) {
935 + if (path[depth].p_idx !=
936 + EXT_LAST_INDEX(path[depth].p_hdr))
937 + return path[depth].p_idx[1].e_block;
945 + * if leaf gets modified and modified extent is first in the leaf
946 + * then we have to correct all indexes above
947 + * TODO: do we need to correct tree in all cases?
949 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
950 + struct ext3_ext_path *path)
952 + struct ext3_extent_header *eh;
953 + int depth = EXT_DEPTH(tree);
954 + struct ext3_extent *ex;
955 + unsigned long border;
958 + eh = path[depth].p_hdr;
959 + ex = path[depth].p_ext;
964 + /* there is no tree at all */
968 + if (ex != EXT_FIRST_EXTENT(eh)) {
969 + /* we correct tree if first leaf got modified only */
974 + * TODO: we need correction if border is smaller then current one
977 + border = path[depth].p_ext->e_block;
978 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
980 + path[k].p_idx->e_block = border;
981 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
985 + /* change all left-side indexes */
986 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
988 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
990 + path[k].p_idx->e_block = border;
991 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
999 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1000 + struct ext3_extent *ex1,
1001 + struct ext3_extent *ex2)
1003 + if (ex1->e_block + ex1->e_num != ex2->e_block)
1006 +#ifdef AGRESSIVE_TEST
1007 + if (ex1->e_num >= 4)
1011 + if (!tree->mergable)
1014 + return tree->mergable(ex1, ex2);
1018 + * this routine tries to merge requsted extent into the existing
1019 + * extent or inserts requested extent as new one into the tree,
1020 + * creating new leaf in no-space case
1022 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1023 + struct ext3_ext_path *path,
1024 + struct ext3_extent *newext)
1026 + struct ext3_extent_header * eh;
1027 + struct ext3_extent *ex, *fex;
1028 + struct ext3_extent *nearex; /* nearest extent */
1029 + struct ext3_ext_path *npath = NULL;
1030 + int depth, len, err, next;
1032 + depth = EXT_DEPTH(tree);
1033 + ex = path[depth].p_ext;
1035 + /* try to insert block into found extent and return */
1036 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1037 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1038 + newext->e_num, ex->e_block, ex->e_num,
1040 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1042 + ex->e_num += newext->e_num;
1043 + err = ext3_ext_dirty(handle, tree, path + depth);
1048 + depth = EXT_DEPTH(tree);
1049 + eh = path[depth].p_hdr;
1050 + if (eh->e_num < eh->e_max)
1053 + /* probably next leaf has space for us? */
1054 + fex = EXT_LAST_EXTENT(eh);
1055 + next = ext3_ext_next_leaf_block(tree, path);
1056 + if (newext->e_block > fex->e_block && next != 0xffffffff) {
1057 + ext_debug(tree, "next leaf block - %d\n", next);
1058 + EXT_ASSERT(!npath);
1059 + npath = ext3_ext_find_extent(tree, next, NULL);
1060 + if (IS_ERR(npath))
1061 + return PTR_ERR(npath);
1062 + EXT_ASSERT(npath->p_depth == path->p_depth);
1063 + eh = npath[depth].p_hdr;
1064 + if (eh->e_num < eh->e_max) {
1065 + ext_debug(tree, "next leaf isnt full(%d)\n",
1070 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1071 + eh->e_num, eh->e_max);
1075 + * there is no free space in found leaf
1076 + * we're gonna add new leaf in the tree
1078 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1081 + depth = EXT_DEPTH(tree);
1082 + eh = path[depth].p_hdr;
1085 + nearex = path[depth].p_ext;
1087 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1091 + /* there is no extent in this leaf, create first one */
1092 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1093 + newext->e_block, newext->e_start,
1095 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1096 + } else if (newext->e_block > nearex->e_block) {
1097 + EXT_ASSERT(newext->e_block != nearex->e_block);
1098 + if (nearex != EXT_LAST_EXTENT(eh)) {
1099 + len = EXT_MAX_EXTENT(eh) - nearex;
1100 + len = (len - 1) * sizeof(struct ext3_extent);
1101 + len = len < 0 ? 0 : len;
1102 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1103 + "move %d from 0x%p to 0x%p\n",
1104 + newext->e_block, newext->e_start,
1106 + nearex, len, nearex + 1, nearex + 2);
1107 + memmove(nearex + 2, nearex + 1, len);
1109 + path[depth].p_ext = nearex + 1;
1111 + EXT_ASSERT(newext->e_block != nearex->e_block);
1112 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1113 + len = len < 0 ? 0 : len;
1114 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1115 + "move %d from 0x%p to 0x%p\n",
1116 + newext->e_block, newext->e_start, newext->e_num,
1117 + nearex, len, nearex + 1, nearex + 2);
1118 + memmove(nearex + 1, nearex, len);
1119 + path[depth].p_ext = nearex;
1123 + nearex = path[depth].p_ext;
1124 + nearex->e_block = newext->e_block;
1125 + nearex->e_start = newext->e_start;
1126 + nearex->e_num = newext->e_num;
1128 + /* time to correct all indexes above */
1129 + err = ext3_ext_correct_indexes(handle, tree, path);
1133 + err = ext3_ext_dirty(handle, tree, path + depth);
1137 + ext3_ext_drop_refs(npath);
1144 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1145 + unsigned long num, ext_prepare_callback func)
1147 + struct ext3_ext_path *path = NULL;
1148 + struct ext3_extent *ex, cbex;
1149 + unsigned long next, start = 0, end = 0;
1150 + int depth, exists, err = 0;
1154 + EXT_ASSERT(tree->inode);
1155 + EXT_ASSERT(tree->root);
1157 + while (num > 0 && block != 0xfffffffff) {
1158 + /* find extent for this block */
1159 + path = ext3_ext_find_extent(tree, block, path);
1160 + if (IS_ERR(path)) {
1161 + err = PTR_ERR(path);
1165 + depth = EXT_DEPTH(tree);
1166 + ex = path[depth].p_ext;
1167 + next = ext3_ext_next_allocated_block(path);
1171 + /* there is no extent yet, so try to allocate
1172 + * all requested space */
1174 + end = block + num - 1;
1175 + } else if (ex->e_block > block) {
1176 + /* need to allocate space before found extent */
1178 + end = ex->e_block - 1;
1179 + if (block + num - 1 < end)
1180 + end = block + num - 1;
1181 + } else if (block >= ex->e_block + ex->e_num) {
1182 + /* need to allocate space after found extent */
1184 + end = block + num - 1;
1187 + } else if (block >= ex->e_block) {
1189 + * some part of requested space is covered
1193 + end = ex->e_block + ex->e_num - 1;
1194 + if (block + num - 1 < end)
1195 + end = block + num - 1;
1202 + cbex.e_block = start;
1203 + cbex.e_num = end - start + 1;
1208 + err = func(tree, path, &cbex, exists);
1212 + if (err == EXT_BREAK) {
1217 + if (EXT_DEPTH(tree) != depth) {
1218 + /* depth was changed. we have to realloc path */
1219 + ext3_ext_drop_refs(path);
1224 + block += cbex.e_num;
1225 + num -= cbex.e_num;
1229 + ext3_ext_drop_refs(path);
1237 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
1240 + tree->cex->e_num = 0;
1244 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, struct ext3_extent *ex)
1248 + EXT_ASSERT(ex->e_num);
1249 + tree->cex->e_block = ex->e_block;
1250 + tree->cex->e_start = ex->e_start;
1251 + tree->cex->e_num = ex->e_num;
1256 + * this routine calculate boundaries of the gap requested block fits into
1257 + * and cache this gap
1260 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1261 + struct ext3_ext_path *path,
1262 + unsigned long block)
1264 + int depth = EXT_DEPTH(tree);
1265 + struct ext3_extent *ex, gex;
1270 + ex = path[depth].p_ext;
1272 + /* there is no extent yet, so gap is [0;-] */
1274 + gex.e_num = 0xffffffff;
1275 + ext_debug(tree, "cache gap(whole file):");
1276 + } else if (block < ex->e_block) {
1277 + gex.e_block = block;
1278 + gex.e_num = ex->e_block - block;
1279 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1280 + (unsigned long) block,
1281 + (unsigned long) ex->e_block,
1282 + (unsigned long) ex->e_num);
1283 + } else if (block >= ex->e_block + ex->e_num) {
1284 + gex.e_block = ex->e_block + ex->e_num;
1285 + gex.e_num = ext3_ext_next_allocated_block(path);
1286 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1287 + (unsigned long) ex->e_block,
1288 + (unsigned long) ex->e_num,
1289 + (unsigned long) block);
1290 + EXT_ASSERT(gex.e_num > gex.e_block);
1291 + gex.e_num = gex.e_num - gex.e_block;
1296 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.e_block,
1297 + (unsigned long) gex.e_num);
1298 + gex.e_start = 0xffffffff;
1299 + ext3_ext_put_in_cache(tree, &gex);
1303 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1304 + struct ext3_extent *ex)
1306 + struct ext3_extent *cex = tree->cex;
1308 + /* is there cache storage at all? */
1312 + /* has cache valid data? */
1313 + if (cex->e_num == 0)
1316 + if (block >= cex->e_block && block < cex->e_block + cex->e_num) {
1317 + ex->e_block = cex->e_block;
1318 + ex->e_start = cex->e_start;
1319 + ex->e_num = cex->e_num;
1320 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1321 + (unsigned long) block,
1322 + (unsigned long) ex->e_block,
1323 + (unsigned long) ex->e_num,
1324 + (unsigned long) ex->e_start);
1328 + /* not in cache */
1333 + * routine removes index from the index block
1334 + * it's used in truncate case only. thus all requests are for
1335 + * last index in the block only
1337 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1338 + struct ext3_ext_path *path)
1340 + struct buffer_head *bh;
1343 + /* free index block */
1345 + EXT_ASSERT(path->p_hdr->e_num);
1346 + if ((err = ext3_ext_get_access(handle, tree, path)))
1348 + path->p_hdr->e_num--;
1349 + if ((err = ext3_ext_dirty(handle, tree, path)))
1351 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1352 + path->p_idx->e_leaf);
1353 + bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->e_leaf);
1354 + ext3_forget(handle, 0, tree->inode, bh, path->p_idx->e_leaf);
1355 + ext3_free_blocks(handle, tree->inode, path->p_idx->e_leaf, 1);
1359 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1360 + struct ext3_ext_path *path)
1362 + int depth = EXT_DEPTH(tree);
1366 + /* probably there is space in leaf? */
1367 + if (path[depth].p_hdr->e_num < path[depth].p_hdr->e_max)
1372 + * the worste case we're expecting is creation of the
1373 + * new root (growing in depth) with index splitting
1374 + * for splitting we have to consider depth + 1 because
1375 + * previous growing could increase it
1377 + depth = depth + 1;
1380 + * growing in depth:
1381 + * block allocation + new root + old root
1383 + needed = EXT3_ALLOC_NEEDED + 2;
1385 + /* index split. we may need:
1386 + * allocate intermediate indexes and new leaf
1387 + * change two blocks at each level, but root
1388 + * modify root block (inode)
1390 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1396 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1397 + struct ext3_ext_path *path, unsigned long start,
1398 + unsigned long end)
1400 + struct ext3_extent *ex, tex;
1401 + struct ext3_ext_path *npath;
1402 + int depth, creds, err;
1404 + depth = EXT_DEPTH(tree);
1405 + ex = path[depth].p_ext;
1407 + EXT_ASSERT(end < ex->e_block + ex->e_num - 1);
1408 + EXT_ASSERT(ex->e_block < start);
1410 + /* calculate tail extent */
1411 + tex.e_block = end + 1;
1412 + EXT_ASSERT(tex.e_block < ex->e_block + ex->e_num);
1413 + tex.e_num = ex->e_block + ex->e_num - tex.e_block;
1415 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1416 + handle = ext3_ext_journal_restart(handle, creds);
1417 + if (IS_ERR(handle))
1418 + return PTR_ERR(handle);
1420 + /* calculate head extent. use primary extent */
1421 + err = ext3_ext_get_access(handle, tree, path + depth);
1424 + ex->e_num = start - ex->e_block;
1425 + err = ext3_ext_dirty(handle, tree, path + depth);
1429 + /* FIXME: some callback to free underlying resource
1430 + * and correct e_start? */
1431 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1432 + ex->e_block, ex->e_num, tex.e_block, tex.e_num);
1434 + npath = ext3_ext_find_extent(tree, ex->e_block, NULL);
1435 + if (IS_ERR(npath))
1436 + return PTR_ERR(npath);
1437 + depth = EXT_DEPTH(tree);
1438 + EXT_ASSERT(npath[depth].p_ext->e_block == ex->e_block);
1439 + EXT_ASSERT(npath[depth].p_ext->e_num == ex->e_num);
1441 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1442 + ext3_ext_drop_refs(npath);
1450 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1451 + struct ext3_ext_path *path, unsigned long start,
1452 + unsigned long end)
1454 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1455 + int err = 0, correct_index = 0;
1456 + int depth = EXT_DEPTH(tree), credits;
1457 + struct ext3_extent_header *eh;
1458 + unsigned a, b, block, num;
1460 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1461 + if (!path[depth].p_hdr)
1462 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1463 + eh = path[depth].p_hdr;
1465 + EXT_ASSERT(eh->e_num <= eh->e_max);
1466 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
1468 + /* find where to start removing */
1469 + le = ex = EXT_LAST_EXTENT(eh);
1470 + while (ex != EXT_FIRST_EXTENT(eh)) {
1471 + if (ex->e_block <= end)
1476 + if (start > ex->e_block && end < ex->e_block + ex->e_num - 1) {
1477 + /* removal of internal part of the extent requested
1478 + * tail and head must be placed in different extent
1479 + * so, we have to insert one more extent */
1480 + path[depth].p_ext = ex;
1481 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1485 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1486 + ex->e_block + ex->e_num > start) {
1487 + ext_debug(tree, "remove ext %u:%u\n", ex->e_block, ex->e_num);
1488 + path[depth].p_ext = ex;
1490 + a = ex->e_block > start ? ex->e_block : start;
1491 + b = ex->e_block + ex->e_num - 1 < end ?
1492 + ex->e_block + ex->e_num - 1 : end;
1494 + ext_debug(tree, " border %u:%u\n", a, b);
1496 + if (a != ex->e_block && b != ex->e_block + ex->e_num - 1) {
1500 + } else if (a != ex->e_block) {
1501 + /* remove tail of the extent */
1502 + block = ex->e_block;
1504 + } else if (b != ex->e_block + ex->e_num - 1) {
1505 + /* remove head of the extent */
1509 + /* remove whole extent: excelent! */
1510 + block = ex->e_block;
1512 + EXT_ASSERT(a == ex->e_block &&
1513 + b == ex->e_block + ex->e_num - 1);
1516 + if (ex == EXT_FIRST_EXTENT(eh))
1517 + correct_index = 1;
1520 + if (correct_index)
1521 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1522 + if (tree->remove_extent_credits)
1523 + credits += tree->remove_extent_credits(tree, ex, a, b);
1525 + handle = ext3_ext_journal_restart(handle, credits);
1526 + if (IS_ERR(handle)) {
1527 + err = PTR_ERR(handle);
1531 + err = ext3_ext_get_access(handle, tree, path + depth);
1535 + if (tree->remove_extent)
1536 + err = tree->remove_extent(tree, ex, a, b);
1541 + /* this extent is removed entirely mark slot unused */
1547 + ex->e_block = block;
1550 + err = ext3_ext_dirty(handle, tree, path + depth);
1554 + ext_debug(tree, "new extent: %u:%u:%u\n",
1555 + ex->e_block, ex->e_num, ex->e_start);
1560 + /* reuse unused slots */
1562 + if (lu->e_start) {
1571 + if (correct_index && eh->e_num)
1572 + err = ext3_ext_correct_indexes(handle, tree, path);
1574 + /* if this leaf is free, then we should
1575 + * remove it from index block above */
1576 + if (err == 0 && eh->e_num == 0 && path[depth].p_bh != NULL)
1577 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1584 +static struct ext3_extent_idx *
1585 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1587 + struct ext3_extent_idx *ix;
1589 + ix = EXT_LAST_INDEX(hdr);
1590 + while (ix != EXT_FIRST_INDEX(hdr)) {
1591 + if (ix->e_block <= block)
1599 + * returns 1 if current index have to be freed (even partial)
1602 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1604 + EXT_ASSERT(path->p_idx);
1606 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1610 + * if truncate on deeper level happened it it wasn't partial
1611 + * so we have to consider current index for truncation
1613 + if (path->p_hdr->e_num == path->p_block)
1618 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1619 + unsigned long start, unsigned long end)
1621 + struct inode *inode = tree->inode;
1622 + struct super_block *sb = inode->i_sb;
1623 + int depth = EXT_DEPTH(tree);
1624 + struct ext3_ext_path *path;
1626 + int i = 0, err = 0;
1628 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1630 + /* probably first extent we're gonna free will be last in block */
1631 + handle = ext3_journal_start(inode, depth + 1);
1632 + if (IS_ERR(handle))
1633 + return PTR_ERR(handle);
1635 + ext3_ext_invalidate_cache(tree);
1638 + * we start scanning from right side freeing all the blocks
1639 + * after i_size and walking into the deep
1641 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1642 + if (IS_ERR(path)) {
1643 + ext3_error(sb, "ext3_ext_remove_space",
1644 + "Can't allocate path array");
1645 + ext3_journal_stop(handle, inode);
1648 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1649 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1651 + while (i >= 0 && err == 0) {
1653 + /* this is leaf block */
1654 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1655 + /* root level have p_bh == NULL, brelse() eats this */
1656 + brelse(path[i].p_bh);
1661 + /* this is index block */
1662 + if (!path[i].p_hdr) {
1663 + ext_debug(tree, "initialize header\n");
1664 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1667 + EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1668 + EXT_ASSERT(path[i].p_hdr->e_magic == EXT3_EXT_MAGIC);
1670 + if (!path[i].p_idx) {
1671 + /* this level hasn't touched yet */
1673 + ext3_ext_last_covered(path[i].p_hdr, end);
1674 + path[i].p_block = path[i].p_hdr->e_num + 1;
1675 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1676 + path[i].p_hdr, path[i].p_hdr->e_num);
1678 + /* we've already was here, see at next index */
1682 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1683 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1685 + if (ext3_ext_more_to_rm(path + i)) {
1686 + /* go to the next level */
1687 + ext_debug(tree, "move to level %d (block %d)\n",
1688 + i + 1, path[i].p_idx->e_leaf);
1689 + memset(path + i + 1, 0, sizeof(*path));
1690 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->e_leaf);
1691 + if (!path[i+1].p_bh) {
1692 + /* should we reset i_size? */
1696 + /* put actual number of indexes to know is this
1697 + * number got changed at the next iteration */
1698 + path[i].p_block = path[i].p_hdr->e_num;
1701 + /* we finish processing this index, go up */
1702 + if (path[i].p_hdr->e_num == 0 && i > 0) {
1703 + /* index is empty, remove it
1704 + * handle must be already prepared by the
1705 + * truncate_leaf() */
1706 + err = ext3_ext_rm_idx(handle, tree, path + i);
1708 + /* root level have p_bh == NULL, brelse() eats this */
1709 + brelse(path[i].p_bh);
1711 + ext_debug(tree, "return to level %d\n", i);
1715 + /* TODO: flexible tree reduction should be here */
1716 + if (path->p_hdr->e_num == 0) {
1718 + * truncate to zero freed all the tree
1719 + * so, we need to correct e_depth
1721 + err = ext3_ext_get_access(handle, tree, path);
1723 + EXT_ROOT_HDR(tree)->e_depth = 0;
1724 + err = ext3_ext_dirty(handle, tree, path);
1729 + ext3_journal_stop(handle, inode);
1735 + * called at mount time
1737 +void ext3_ext_init(struct super_block *sb)
1740 + * possible initialization would be here
1743 + if (test_opt(sb, EXTENTS)) {
1744 + printk("EXT3-fs: file extents enabled");
1745 +#ifdef AGRESSIVE_TEST
1746 + printk(", agressive tests");
1748 +#ifdef CHECK_BINSEARCH
1749 + printk(", check binsearch");
1756 + * called at umount time
1758 +void ext3_ext_release(struct super_block *sb)
1762 +/************************************************************************
1763 + * VFS related routines
1764 + ************************************************************************/
1766 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1768 + /* we use in-core data, not bh */
1772 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1774 + struct inode *inode = buffer;
1775 + ext3_mark_inode_dirty(handle, inode);
1779 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1780 + struct ext3_extent *ex2)
1782 + if (ex1->e_start + ex1->e_num == ex2->e_start)
1788 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1789 + struct ext3_extent *ex,
1790 + unsigned long from, unsigned long to)
1794 + /* at present, extent can't cross block group */;
1795 + needed = 3; /* bitmap + group desc + sb */
1797 +#ifdef CONFIG_QUOTA
1798 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1804 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1805 + struct ext3_extent *ex,
1806 + unsigned long from, unsigned long to)
1808 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1809 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1811 + if (IS_ERR(handle))
1812 + return PTR_ERR(handle);
1813 + if (from >= ex->e_block && to == ex->e_block + ex->e_num - 1) {
1814 + /* tail removal */
1815 + unsigned long num, start;
1816 + num = ex->e_block + ex->e_num - from;
1817 + start = ex->e_start + ex->e_num - num;
1818 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1820 + ext3_free_blocks(handle, tree->inode, start, num);
1821 + } else if (from == ex->e_block && to <= ex->e_block + ex->e_num - 1) {
1822 + printk("strange request: removal %lu-%lu from %u:%u\n",
1823 + from, to, ex->e_block, ex->e_num);
1825 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1826 + from, to, ex->e_block, ex->e_num);
1828 + ext3_journal_stop(handle, tree->inode);
1832 +static int ext3_ext_find_goal(struct inode *inode,
1833 + struct ext3_ext_path *path)
1835 + struct ext3_inode_info *ei = EXT3_I(inode);
1836 + unsigned long bg_start;
1837 + unsigned long colour;
1841 + depth = path->p_depth;
1842 + /* try to find previous block */
1843 + if (path[depth].p_ext)
1844 + return path[depth].p_ext->e_start +
1845 + path[depth].p_ext->e_num - 1;
1847 + /* it looks index is empty
1848 + * try to find starting from index itself */
1849 + if (path[depth].p_bh)
1850 + return path[depth].p_bh->b_blocknr;
1853 + /* OK. use inode's group */
1854 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1855 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1856 + colour = (current->pid % 16) *
1857 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1858 + return bg_start + colour;
1861 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1862 + struct ext3_ext_path *path,
1863 + struct ext3_extent *ex, int *err)
1865 + struct inode *inode = tree->inode;
1866 + int newblock, goal;
1870 + EXT_ASSERT(ex->e_start);
1871 + EXT_ASSERT(ex->e_num);
1873 + /* reuse block from the extent to order data/metadata */
1874 + newblock = ex->e_start++;
1876 + if (ex->e_num == 0) {
1878 + /* allocate new block for the extent */
1879 + goal = ext3_ext_find_goal(inode, path);
1880 + ex->e_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1881 + if (ex->e_start == 0) {
1882 + /* error occured: restore old extent */
1883 + ex->e_start = newblock;
1890 +static void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1891 + struct inode *inode)
1893 + tree->inode = inode;
1894 + tree->root = (void *) EXT3_I(inode)->i_data;
1895 + tree->get_write_access = ext3_get_inode_write_access;
1896 + tree->mark_buffer_dirty = ext3_mark_buffer_dirty;
1897 + tree->mergable = ext3_ext_mergable;
1898 + tree->new_block = ext3_new_block_cb;
1899 + tree->remove_extent = ext3_remove_blocks;
1900 + tree->remove_extent_credits = ext3_remove_blocks_credits;
1901 + tree->buffer = (void *) inode;
1902 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1903 + tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1908 +ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
1909 + struct ext3_ext_path *path,
1910 + struct ext3_extent *newex, int exist)
1912 + struct inode *inode = tree->inode;
1913 + int count, err, goal;
1914 + loff_t new_i_size;
1916 + unsigned long pblock;
1919 + return EXT_CONTINUE;
1921 + count = ext3_ext_calc_credits_for_insert(tree, path);
1922 + handle = ext3_journal_start(inode, count + EXT3_ALLOC_NEEDED + 1);
1923 + if (IS_ERR(handle))
1924 + return PTR_ERR(handle);
1926 + goal = ext3_ext_find_goal(inode, path);
1927 + count = newex->e_num;
1928 +#ifdef EXT3_MULTIBLOCK_ALLOCATOR
1929 + pblock = ext3_new_block(handle, inode, goal, &count, NULL, &err);
1930 + EXT_ASSERT(count <= num);
1931 + /* FIXME: error handling here */
1932 + EXT_ASSERT(err == 0);
1937 + /* insert new extent */
1938 + newex->e_start = pblock;
1939 + newex->e_num = count;
1940 + err = ext3_ext_insert_extent(handle, tree, path, newex);
1944 + /* correct on-disk inode size */
1945 + if (newex->e_num > 0) {
1946 + new_i_size = (loff_t) newex->e_block + newex->e_num;
1947 + new_i_size = new_i_size << inode->i_blkbits;
1948 + if (new_i_size > i_size_read(inode))
1949 + new_i_size = i_size_read(inode);
1950 + if (new_i_size > EXT3_I(inode)->i_disksize) {
1951 + EXT3_I(inode)->i_disksize = new_i_size;
1952 + err = ext3_mark_inode_dirty(handle, inode);
1957 + ext3_journal_stop(handle, inode);
1962 +int ext3_ext_allocate_nblocks(struct inode *inode, unsigned long block,
1963 + unsigned long num)
1965 + struct ext3_extents_tree tree;
1968 + ext_debug(&tree, "blocks %lu-%lu requested for inode %u\n",
1969 + block, block + num,(unsigned) inode->i_ino);
1971 + ext3_init_tree_desc(&tree, inode);
1972 + down(&EXT3_I(inode)->truncate_sem);
1973 + err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
1974 + ext3_ext_invalidate_cache(&tree);
1975 + up(&EXT3_I(inode)->truncate_sem);
1981 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
1982 + long iblock, struct buffer_head *bh_result,
1983 + int create, int extend_disksize)
1985 + struct ext3_ext_path *path = NULL;
1986 + struct ext3_extent newex;
1987 + struct ext3_extent *ex;
1988 + int goal, newblock, err = 0, depth;
1989 + struct ext3_extents_tree tree;
1991 + clear_bit(BH_New, &bh_result->b_state);
1992 + ext3_init_tree_desc(&tree, inode);
1993 + ext_debug(&tree, "block %d requested for inode %u\n",
1994 + (int) iblock, (unsigned) inode->i_ino);
1995 + down_write(&EXT3_I(inode)->truncate_sem);
1997 + /* check in cache */
1998 + if (ext3_ext_in_cache(&tree, iblock, &newex)) {
1999 + if (newex.e_start == 0xffffffff && !create) {
2000 + /* block isn't allocated yet and
2001 + * user don't want to allocate it */
2003 + } else if (newex.e_start) {
2004 + /* block is already allocated */
2005 + newblock = iblock - newex.e_block + newex.e_start;
2010 + /* find extent for this block */
2011 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2012 + if (IS_ERR(path)) {
2013 + err = PTR_ERR(path);
2017 + depth = EXT_DEPTH(&tree);
2020 + * consistent leaf must not be empty
2021 + * this situations is possible, though, _during_ tree modification
2022 + * this is why assert can't be put in ext3_ext_find_extent()
2024 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2026 + if ((ex = path[depth].p_ext)) {
2027 + /* if found exent covers block, simple return it */
2028 + if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
2029 + newblock = iblock - ex->e_block + ex->e_start;
2030 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2031 + (int) iblock, ex->e_block, ex->e_num,
2033 + ext3_ext_put_in_cache(&tree, ex);
2039 + * requested block isn't allocated yet
2040 + * we couldn't try to create block if create flag is zero
2043 + /* put just found gap into cache to speedup subsequest reqs */
2044 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2048 + /* allocate new block */
2049 + goal = ext3_ext_find_goal(inode, path);
2050 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2053 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2056 + /* try to insert new extent into found leaf and return */
2057 + newex.e_block = iblock;
2058 + newex.e_start = newblock;
2060 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2064 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2065 + EXT3_I(inode)->i_disksize = inode->i_size;
2067 + /* previous routine could use block we allocated */
2068 + newblock = newex.e_start;
2069 + set_bit(BH_New, &bh_result->b_state);
2071 + ext3_ext_put_in_cache(&tree, &newex);
2073 + ext3_ext_show_leaf(&tree, path);
2074 + set_bit(BH_Mapped, &bh_result->b_state);
2075 + bh_result->b_dev = inode->i_sb->s_dev;
2076 + bh_result->b_blocknr = newblock;
2079 + ext3_ext_drop_refs(path);
2082 + up_write(&EXT3_I(inode)->truncate_sem);
2087 +void ext3_ext_truncate(struct inode * inode)
2089 + struct address_space *mapping = inode->i_mapping;
2090 + struct super_block *sb = inode->i_sb;
2091 + struct ext3_extents_tree tree;
2092 + unsigned long last_block;
2096 + ext3_init_tree_desc(&tree, inode);
2099 + * probably first extent we're gonna free will be last in block
2101 + err = ext3_writepage_trans_blocks(inode) + 3;
2102 + handle = ext3_journal_start(inode, err);
2103 + if (IS_ERR(handle))
2106 + ext3_block_truncate_page(handle, mapping, inode->i_size);
2108 + down_write(&EXT3_I(inode)->truncate_sem);
2109 + ext3_ext_invalidate_cache(&tree);
2112 + * TODO: optimization is possible here
2113 + * probably we need not scaning at all,
2114 + * because page truncation is enough
2116 + if (ext3_orphan_add(handle, inode))
2119 + /* we have to know where to truncate from in crash case */
2120 + EXT3_I(inode)->i_disksize = inode->i_size;
2121 + ext3_mark_inode_dirty(handle, inode);
2123 + last_block = (inode->i_size + sb->s_blocksize - 1)
2124 + >> EXT3_BLOCK_SIZE_BITS(sb);
2125 + err = ext3_ext_remove_space(&tree, last_block, 0xffffffff);
2127 + /* In a multi-transaction truncate, we only make the final
2128 + * transaction synchronous */
2129 + if (IS_SYNC(inode))
2130 + handle->h_sync = 1;
2134 + * If this was a simple ftruncate(), and the file will remain alive
2135 + * then we need to clear up the orphan record which we created above.
2136 + * However, if this was a real unlink then we were called by
2137 + * ext3_delete_inode(), and we allow that function to clean up the
2138 + * orphan info for us.
2140 + if (inode->i_nlink)
2141 + ext3_orphan_del(handle, inode);
2143 + up_write(&EXT3_I(inode)->truncate_sem);
2144 + ext3_journal_stop(handle, inode);
2148 + * this routine calculate max number of blocks we could modify
2149 + * in order to allocate new block for an inode
2151 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2153 + struct ext3_extents_tree tree;
2156 + ext3_init_tree_desc(&tree, inode);
2158 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2160 + /* caller want to allocate num blocks */
2163 +#ifdef CONFIG_QUOTA
2165 + * FIXME: real calculation should be here
2166 + * it depends on blockmap format of qouta file
2168 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2174 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2176 + struct ext3_extents_tree tree;
2178 + ext3_init_tree_desc(&tree, inode);
2179 + ext3_extent_tree_init(handle, &tree);
2183 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2184 + struct ext3_ext_path *path,
2185 + struct ext3_extent *newex, int exist)
2187 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2190 + return EXT_CONTINUE;
2193 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2196 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2198 + buf->cur += sizeof(*newex);
2200 + buf->err = -EFAULT;
2203 + return EXT_CONTINUE;
2207 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2208 + struct ext3_ext_path *path,
2209 + struct ext3_extent *ex, int exist)
2211 + struct ext3_extent_tree_stats *buf =
2212 + (struct ext3_extent_tree_stats *) tree->private;
2216 + return EXT_CONTINUE;
2218 + depth = EXT_DEPTH(tree);
2219 + buf->extents_num++;
2220 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2222 + return EXT_CONTINUE;
2225 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2226 + unsigned long arg)
2230 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2231 + struct ext3_extent_buf buf;
2232 + struct ext3_extents_tree tree;
2234 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2237 + ext3_init_tree_desc(&tree, inode);
2238 + buf.cur = buf.buffer;
2240 + tree.private = &buf;
2241 + err = ext3_ext_walk_space(&tree, buf.start, 0xffffffff,
2242 + ext3_ext_store_extent_cb);
2245 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2246 + struct ext3_extent_tree_stats buf;
2247 + struct ext3_extents_tree tree;
2249 + ext3_init_tree_desc(&tree, inode);
2250 + buf.depth = EXT_DEPTH(&tree);
2251 + buf.extents_num = 0;
2253 + tree.private = &buf;
2254 + err = ext3_ext_walk_space(&tree, 0, 0xffffffff,
2255 + ext3_ext_collect_stats_cb);
2257 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2258 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2259 + struct ext3_extents_tree tree;
2260 + ext3_init_tree_desc(&tree, inode);
2261 + err = EXT_DEPTH(&tree);
2267 Index: linux-2.4.21-suse2/fs/ext3/ialloc.c
2268 ===================================================================
2269 --- linux-2.4.21-suse2.orig/fs/ext3/ialloc.c 2004-01-23 19:04:17.000000000 +0300
2270 +++ linux-2.4.21-suse2/fs/ext3/ialloc.c 2004-01-24 20:10:25.000000000 +0300
2271 @@ -592,6 +592,10 @@
2275 + if (test_opt(sb, EXTENTS)) {
2276 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2277 + ext3_extents_initialize_blockmap(handle, inode);
2279 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2282 Index: linux-2.4.21-suse2/fs/ext3/inode.c
2283 ===================================================================
2284 --- linux-2.4.21-suse2.orig/fs/ext3/inode.c 2004-01-23 19:04:17.000000000 +0300
2285 +++ linux-2.4.21-suse2/fs/ext3/inode.c 2004-01-24 20:10:25.000000000 +0300
2286 @@ -853,6 +853,18 @@
2291 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2292 + struct buffer_head *bh, int create, int extend_disksize)
2294 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2295 + return ext3_ext_get_block(handle, inode,
2296 + block, bh, create,
2298 + return ext3_get_block_handle(handle, inode,
2299 + block, bh, create, extend_disksize);
2303 * The BKL is not held on entry here.
2306 handle = ext3_journal_current_handle();
2307 J_ASSERT(handle != 0);
2309 - ret = ext3_get_block_handle(handle, inode, iblock,
2310 + ret = ext3_get_block_wrap(handle, inode, iblock,
2311 bh_result, create, 1);
2318 - ret = ext3_get_block_handle(handle, inode, iblock,
2319 + ret = ext3_get_block_wrap(handle, inode, iblock,
2320 bh_result, create, 0);
2322 bh_result->b_size = (1 << inode->i_blkbits);
2325 dummy.b_blocknr = -1000;
2326 buffer_trace_init(&dummy.b_history);
2327 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2328 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2329 if (!*errp && buffer_mapped(&dummy)) {
2330 struct buffer_head *bh;
2331 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2332 @@ -1502,7 +1514,7 @@
2333 * This required during truncate. We need to physically zero the tail end
2334 * of that block so it doesn't yield old data if the file is later grown.
2336 -static int ext3_block_truncate_page(handle_t *handle,
2337 +int ext3_block_truncate_page(handle_t *handle,
2338 struct address_space *mapping, loff_t from)
2340 unsigned long index = from >> PAGE_CACHE_SHIFT;
2341 @@ -1987,6 +1999,9 @@
2343 ext3_discard_prealloc(inode);
2345 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2346 + return ext3_ext_truncate(inode);
2348 handle = start_transaction(inode);
2350 return; /* AKPM: return what? */
2351 @@ -2663,6 +2678,9 @@
2352 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2355 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2356 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2358 if (ext3_should_journal_data(inode))
2359 ret = 3 * (bpp + indirects) + 2;
2361 @@ -3099,7 +3117,7 @@
2363 /* alloc blocks one by one */
2364 for (i = 0; i < nblocks; i++) {
2365 - ret = ext3_get_block_handle(handle, inode, blocks[i],
2366 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
2370 @@ -3175,7 +3193,7 @@
2374 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1, 1);
2375 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1, 1);
2377 printk(KERN_INFO "ext3_map_inode_page: error %d "
2378 "allocating block %ld\n", rc, iblock);
2379 Index: linux-2.4.21-suse2/fs/ext3/Makefile
2380 ===================================================================
2381 --- linux-2.4.21-suse2.orig/fs/ext3/Makefile 2004-01-13 17:45:20.000000000 +0300
2382 +++ linux-2.4.21-suse2/fs/ext3/Makefile 2004-01-24 20:10:25.000000000 +0300
2384 export-objs := ext3-exports.o
2386 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2387 - ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o
2388 + ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2390 obj-m := $(O_TARGET)
2392 export-objs += xattr.o
2393 Index: linux-2.4.21-suse2/fs/ext3/super.c
2394 ===================================================================
2395 --- linux-2.4.21-suse2.orig/fs/ext3/super.c 2004-01-23 19:04:17.000000000 +0300
2396 +++ linux-2.4.21-suse2/fs/ext3/super.c 2004-01-24 20:10:25.000000000 +0300
2400 J_ASSERT(sbi->s_delete_inodes == 0);
2401 + ext3_ext_release(sb);
2402 ext3_xattr_put_super(sb);
2403 journal_destroy(sbi->s_journal);
2404 if (!(sb->s_flags & MS_RDONLY)) {
2405 @@ -829,6 +830,10 @@
2409 + else if (!strcmp (this_char, "extents"))
2410 + set_opt (*mount_options, EXTENTS);
2411 + else if (!strcmp (this_char, "extdebug"))
2412 + set_opt (*mount_options, EXTDEBUG);
2413 else if (!strcmp (this_char, "grpid") ||
2414 !strcmp (this_char, "bsdgroups"))
2415 set_opt (*mount_options, GRPID);
2416 @@ -1523,6 +1528,8 @@
2417 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2420 + ext3_ext_init(sb);
2425 Index: linux-2.4.21-suse2/fs/ext3/ioctl.c
2426 ===================================================================
2427 --- linux-2.4.21-suse2.orig/fs/ext3/ioctl.c 2004-01-13 17:45:18.000000000 +0300
2428 +++ linux-2.4.21-suse2/fs/ext3/ioctl.c 2004-01-24 20:10:25.000000000 +0300
2429 @@ -174,6 +174,10 @@
2433 + case EXT3_IOC_GET_EXTENTS:
2434 + case EXT3_IOC_GET_TREE_STATS:
2435 + case EXT3_IOC_GET_TREE_DEPTH:
2436 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2440 Index: linux-2.4.21-suse2/include/linux/ext3_fs.h
2441 ===================================================================
2442 --- linux-2.4.21-suse2.orig/include/linux/ext3_fs.h 2004-01-23 19:04:17.000000000 +0300
2443 +++ linux-2.4.21-suse2/include/linux/ext3_fs.h 2004-01-24 20:10:25.000000000 +0300
2445 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2446 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2447 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2448 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2450 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2451 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2453 #ifdef CONFIG_JBD_DEBUG
2454 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2456 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 5, long)
2457 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 6, long)
2458 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 7, long)
2461 * Structure of an inode on the disk
2463 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
2464 #define EXT3_MOUNT_IOPEN 0x40000 /* Allow access via iopen */
2465 #define EXT3_MOUNT_IOPEN_NOPRIV 0x80000 /* Make iopen world-readable */
2466 +#define EXT3_MOUNT_EXTENTS 0x100000/* Extents support */
2467 +#define EXT3_MOUNT_EXTDEBUG 0x200000/* Extents debug */
2469 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2470 #ifndef _LINUX_EXT2_FS_H
2472 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2475 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2476 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2477 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2478 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2479 @@ -770,6 +777,14 @@
2480 extern struct inode_operations ext3_symlink_inode_operations;
2481 extern struct inode_operations ext3_fast_symlink_inode_operations;
2484 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2485 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2486 + struct buffer_head *, int, int);
2487 +extern void ext3_ext_truncate(struct inode *);
2488 +extern void ext3_ext_init(struct super_block *);
2489 +extern void ext3_ext_release(struct super_block *);
2490 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2492 #endif /* __KERNEL__ */
2494 Index: linux-2.4.21-suse2/include/linux/ext3_extents.h
2495 ===================================================================
2496 --- linux-2.4.21-suse2.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2497 +++ linux-2.4.21-suse2/include/linux/ext3_extents.h 2004-01-24 20:10:25.000000000 +0300
2500 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
2502 + * This program is free software; you can redistribute it and/or modify
2503 + * it under the terms of the GNU General Public License version 2 as
2504 + * published by the Free Software Foundation.
2506 + * This program is distributed in the hope that it will be useful,
2507 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2508 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2509 + * GNU General Public License for more details.
2511 + * You should have received a copy of the GNU General Public Licens
2512 + * along with this program; if not, write to the Free Software
2513 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2516 +#ifndef _LINUX_EXT3_EXTENTS
2517 +#define _LINUX_EXT3_EXTENTS
2520 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2521 + * become very little, so index split, in-depth growing and
2522 + * other hard changes happens much more often
2523 + * this is for debug purposes only
2525 +#define AGRESSIVE_TEST_
2528 + * if CHECK_BINSEARCH defined, then results of binary search
2529 + * will be checked by linear search
2531 +#define CHECK_BINSEARCH_
2534 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2535 + * to get lots of info what's going on
2539 +#define ext_debug(tree,fmt,a...) \
2541 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2542 + printk(fmt, ##a); \
2545 +#define ext_debug(tree,fmt,a...)
2549 + * if EXT_STATS is defined then stats numbers are collected
2550 + * these number will be displayed at umount time
2555 +#define EXT3_ALLOC_NEEDED 2 /* block bitmap + group descriptor */
2558 + * ext3_inode has i_block array (total 60 bytes)
2559 + * first 4 bytes are used to store:
2560 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2561 + * - number of alive extents in the inode
2565 + * this is extent on-disk structure
2566 + * it's used at the bottom of the tree
2568 +struct ext3_extent {
2569 + __u32 e_block; /* first logical block extent covers */
2570 + __u32 e_start; /* first physical block extents lives */
2571 + __u32 e_num; /* number of blocks covered by extent */
2575 + * this is index on-disk structure
2576 + * it's used at all the levels, but the bottom
2578 +struct ext3_extent_idx {
2579 + __u32 e_block; /* index covers logical blocks from 'block' */
2580 + __u32 e_leaf; /* pointer to the physical block of the next *
2581 + * level. leaf or next index could bet here */
2585 + * each block (leaves and indexes), even inode-stored has header
2587 +struct ext3_extent_header {
2588 + __u16 e_magic; /* probably will support different formats */
2589 + __u16 e_num; /* number of valid entries */
2590 + __u16 e_max; /* capacity of store in entries */
2591 + __u16 e_depth; /* has tree real underlaying blocks? */
2594 +#define EXT3_EXT_MAGIC 0xf301
2597 + * array of ext3_ext_path contains path to some extent
2598 + * creation/lookup routines use it for traversal/splitting/etc
2599 + * truncate uses it to simulate recursive walking
2601 +struct ext3_ext_path {
2604 + struct ext3_extent *p_ext;
2605 + struct ext3_extent_idx *p_idx;
2606 + struct ext3_extent_header *p_hdr;
2607 + struct buffer_head *p_bh;
2611 + * structure for external API
2616 + * ext3_extents_tree is used to pass initial information
2617 + * to top-level extents API
2619 +struct ext3_extents_tree {
2620 + struct inode *inode; /* inode which tree belongs to */
2621 + void *root; /* ptr to data top of tree resides at */
2622 + void *buffer; /* will be passed as arg to ^^ routines */
2625 + struct ext3_extent *cex;/* last found extent */
2626 + int (*get_write_access)(handle_t *h, void *buffer);
2627 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2628 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2629 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2630 + struct ext3_extent *, unsigned long,
2632 + int (*remove_extent)(struct ext3_extents_tree *,
2633 + struct ext3_extent *, unsigned long,
2635 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2636 + struct ext3_ext_path *, struct ext3_extent *,
2641 + * to be called by ext3_ext_walk_space()
2642 + * negative retcode - error
2643 + * positive retcode - signal for ext3_ext_walk_space(), see below
2644 + * callback must return valid extent (passed or newly created)
2646 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2647 + struct ext3_ext_path *,
2648 + struct ext3_extent *, int);
2650 +#define EXT_CONTINUE 0
2651 +#define EXT_BREAK 1
2654 +#define EXT_FIRST_EXTENT(__hdr__) \
2655 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2656 + sizeof(struct ext3_extent_header)))
2657 +#define EXT_FIRST_INDEX(__hdr__) \
2658 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2659 + sizeof(struct ext3_extent_header)))
2660 +#define EXT_HAS_FREE_INDEX(__path__) \
2661 + ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
2662 +#define EXT_LAST_EXTENT(__hdr__) \
2663 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
2664 +#define EXT_LAST_INDEX(__hdr__) \
2665 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
2666 +#define EXT_MAX_EXTENT(__hdr__) \
2667 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
2668 +#define EXT_MAX_INDEX(__hdr__) \
2669 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
2671 +#define EXT_ROOT_HDR(tree) \
2672 + ((struct ext3_extent_header *) (tree)->root)
2673 +#define EXT_BLOCK_HDR(bh) \
2674 + ((struct ext3_extent_header *) (bh)->b_data)
2675 +#define EXT_DEPTH(_t_) \
2676 + (((struct ext3_extent_header *)((_t_)->root))->e_depth)
2679 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2683 + * this structure is used to gather extents from the tree via ioctl
2685 +struct ext3_extent_buf {
2686 + unsigned long start;
2694 + * this structure is used to collect stats info about the tree
2696 +struct ext3_extent_tree_stats {
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 *);
2709 +#endif /* _LINUX_EXT3_EXTENTS */
2711 Index: linux-2.4.21-suse2/include/linux/ext3_fs_i.h
2712 ===================================================================
2713 --- linux-2.4.21-suse2.orig/include/linux/ext3_fs_i.h 2004-01-23 19:04:17.000000000 +0300
2714 +++ linux-2.4.21-suse2/include/linux/ext3_fs_i.h 2004-01-24 20:10:25.000000000 +0300
2716 * by other means, so we have truncate_sem.
2718 struct rw_semaphore truncate_sem;
2720 + __u32 i_cached_extent[3];
2723 #endif /* _LINUX_EXT3_FS_I */