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-02-06 10:19:27.000000000 +0300
7 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
9 + * This program is free software; you can redistribute it and/or modify
10 + * it under the terms of the GNU General Public License version 2 as
11 + * published by the Free Software Foundation.
13 + * This program is distributed in the hope that it will be useful,
14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 + * GNU General Public License for more details.
18 + * You should have received a copy of the GNU General Public Licens
19 + * along with this program; if not, write to the Free Software
20 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
24 + * Extents support for EXT3
27 + * - ext3_ext_walk_space() sould not use ext3_ext_find_extent()
28 + * - ext3_ext_calc_credits() could take 'mergable' into account
29 + * - ext3*_error() should be used in some situations
30 + * - find_goal() [to be tested and improved]
31 + * - smart tree reduction
32 + * - arch-independence
33 + * common on-disk format for big/little-endian arch
36 +#include <linux/module.h>
37 +#include <linux/fs.h>
38 +#include <linux/time.h>
39 +#include <linux/ext3_jbd.h>
40 +#include <linux/jbd.h>
41 +#include <linux/smp_lock.h>
42 +#include <linux/highuid.h>
43 +#include <linux/pagemap.h>
44 +#include <linux/quotaops.h>
45 +#include <linux/string.h>
46 +#include <linux/slab.h>
47 +#include <linux/locks.h>
48 +#include <linux/ext3_extents.h>
49 +#include <asm/uaccess.h>
51 +static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
55 + if (handle->h_buffer_credits > needed)
57 + if (!ext3_journal_extend(handle, needed))
59 + err = ext3_journal_restart(handle, needed);
65 +ext3_ext_get_access_for_root(handle_t *h, struct ext3_extents_tree *tree)
67 + if (tree->get_write_access)
68 + return tree->get_write_access(h,tree->buffer);
74 +ext3_ext_mark_root_dirty(handle_t *h, struct ext3_extents_tree *tree)
76 + if (tree->mark_buffer_dirty)
77 + return tree->mark_buffer_dirty(h,tree->buffer);
87 +static int ext3_ext_get_access(handle_t *handle,
88 + struct ext3_extents_tree *tree,
89 + struct ext3_ext_path *path)
94 + /* path points to block */
95 + err = ext3_journal_get_write_access(handle, path->p_bh);
97 + /* path points to leaf/index in inode body */
98 + err = ext3_ext_get_access_for_root(handle, tree);
109 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
110 + struct ext3_ext_path *path)
114 + /* path points to block */
115 + err =ext3_journal_dirty_metadata(handle, path->p_bh);
117 + /* path points to leaf/index in inode body */
118 + err = ext3_ext_mark_root_dirty(handle, tree);
124 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
125 + struct ext3_ext_path *path, struct ext3_extent *ex,
128 + int goal, depth, newblock;
129 + struct inode *inode;
132 + if (tree->new_block)
133 + return tree->new_block(handle, tree, path, ex, err);
135 + inode = tree->inode;
136 + depth = EXT_DEPTH(tree);
137 + if (path && depth > 0) {
138 + goal = path[depth-1].p_block;
140 + struct ext3_inode_info *ei = EXT3_I(inode);
141 + unsigned long bg_start;
142 + unsigned long colour;
144 + bg_start = (ei->i_block_group *
145 + EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
146 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
147 + colour = (current->pid % 16) *
148 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
149 + goal = bg_start + colour;
152 + newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
156 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
158 + struct ext3_extent_header *neh;
159 + neh = EXT_ROOT_HDR(tree);
160 + neh->e_generation++;
163 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
167 + size = (tree->inode->i_sb->s_blocksize -
168 + sizeof(struct ext3_extent_header))
169 + / sizeof(struct ext3_extent);
170 +#ifdef AGRESSIVE_TEST
176 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
180 + size = (tree->inode->i_sb->s_blocksize -
181 + sizeof(struct ext3_extent_header))
182 + / sizeof(struct ext3_extent_idx);
183 +#ifdef AGRESSIVE_TEST
189 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
193 + size = (tree->buffer_len - sizeof(struct ext3_extent_header))
194 + / sizeof(struct ext3_extent);
195 +#ifdef AGRESSIVE_TEST
201 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
205 + size = (tree->buffer_len -
206 + sizeof(struct ext3_extent_header))
207 + / sizeof(struct ext3_extent_idx);
208 +#ifdef AGRESSIVE_TEST
214 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
215 + struct ext3_ext_path *path)
218 + int k, l = path->p_depth;
220 + ext_debug(tree, "path:");
221 + for (k = 0; k <= l; k++, path++) {
223 + ext_debug(tree, " %d->%d", path->p_idx->e_block,
224 + path->p_idx->e_leaf);
225 + } else if (path->p_ext) {
226 + ext_debug(tree, " %d:%d:%d",
227 + path->p_ext->e_block,
228 + path->p_ext->e_num,
229 + path->p_ext->e_start);
231 + ext_debug(tree, " []");
233 + ext_debug(tree, "\n");
237 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
238 + struct ext3_ext_path *path)
241 + int depth = EXT_DEPTH(tree);
242 + struct ext3_extent_header *eh;
243 + struct ext3_extent *ex;
249 + eh = path[depth].p_hdr;
250 + ex = EXT_FIRST_EXTENT(eh);
252 + for (i = 0; i < eh->e_num; i++, ex++) {
253 + ext_debug(tree, "%d:%d:%d ",
254 + ex->e_block, ex->e_num, ex->e_start);
256 + ext_debug(tree, "\n");
260 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
262 + int depth = path->p_depth;
265 + for (i = 0; i <= depth; i++, path++)
267 + brelse(path->p_bh);
273 + * binary search for closest index by given block
276 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
277 + struct ext3_ext_path *path, int block)
279 + struct ext3_extent_header *eh = path->p_hdr;
280 + struct ext3_extent_idx *ix;
283 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
284 + EXT_ASSERT(eh->e_num <= eh->e_max);
285 + EXT_ASSERT(eh->e_num > 0);
287 + ext_debug(tree, "binsearch for %d(idx): ", block);
289 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
294 + if (block < ix[l + k].e_block)
298 + ext_debug(tree, "%d:%d:%d ", k, l, r);
303 + ext_debug(tree, " -> %d->%d ", path->p_idx->e_block, path->p_idx->e_leaf);
306 + if (block < ix->e_block)
308 + path->p_idx = ix++;
310 + ext_debug(tree, " -> %d->%d\n", path->p_idx->e_block,
311 + path->p_idx->e_leaf);
313 +#ifdef CHECK_BINSEARCH
315 + struct ext3_extent_idx *chix;
317 + chix = ix = EXT_FIRST_INDEX(eh);
318 + for (k = 0; k < eh->e_num; k++, ix++) {
319 + if (k != 0 && ix->e_block <= ix[-1].e_block) {
320 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
321 + ix, EXT_FIRST_INDEX(eh));
322 + printk("%u <= %u\n",
323 + ix->e_block,ix[-1].e_block);
325 + EXT_ASSERT(k == 0 || ix->e_block > ix[-1].e_block);
326 + if (block < ix->e_block)
330 + EXT_ASSERT(chix == path->p_idx);
337 + * binary search for closest extent by given block
340 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
341 + struct ext3_ext_path *path, int block)
343 + struct ext3_extent_header *eh = path->p_hdr;
344 + struct ext3_extent *ex;
347 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
348 + EXT_ASSERT(eh->e_num <= eh->e_max);
350 + if (eh->e_num == 0) {
352 + * this leaf is empty yet:
353 + * we get such a leaf in split/add case
358 + ext_debug(tree, "binsearch for %d: ", block);
360 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
365 + if (block < ex[l + k].e_block)
369 + ext_debug(tree, "%d:%d:%d ", k, l, r);
374 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->e_block,
375 + path->p_ext->e_start, path->p_ext->e_num);
378 + if (block < ex->e_block)
380 + path->p_ext = ex++;
382 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->e_block,
383 + path->p_ext->e_start, path->p_ext->e_num);
385 +#ifdef CHECK_BINSEARCH
387 + struct ext3_extent *chex;
389 + chex = ex = EXT_FIRST_EXTENT(eh);
390 + for (k = 0; k < eh->e_num; k++, ex++) {
391 + EXT_ASSERT(k == 0 || ex->e_block > ex[-1].e_block);
392 + if (block < ex->e_block)
396 + EXT_ASSERT(chex == path->p_ext);
402 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
404 + struct ext3_extent_header *eh;
406 + BUG_ON(tree->buffer_len == 0);
407 + ext3_ext_get_access_for_root(handle, tree);
408 + eh = EXT_ROOT_HDR(tree);
411 + eh->e_magic = EXT3_EXT_MAGIC;
412 + eh->e_max = ext3_ext_space_root(tree);
413 + ext3_ext_mark_root_dirty(handle, tree);
417 +struct ext3_ext_path *
418 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
419 + struct ext3_ext_path *path)
421 + struct ext3_extent_header *eh;
422 + struct buffer_head *bh;
423 + int depth, i, ppos = 0;
426 + EXT_ASSERT(tree->inode);
427 + EXT_ASSERT(tree->root);
429 + eh = EXT_ROOT_HDR(tree);
431 + i = depth = EXT_DEPTH(tree);
432 + EXT_ASSERT(eh->e_max);
433 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
434 + EXT_ASSERT(i == 0 || eh->e_num > 0);
436 + /* account possible depth increase */
438 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
441 + return ERR_PTR(-ENOMEM);
443 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
444 + path[0].p_hdr = eh;
446 + /* walk through the tree */
448 + ext_debug(tree, "depth %d: num %d, max %d\n",
449 + ppos, eh->e_num, eh->e_max);
450 + ext3_ext_binsearch_idx(tree, path + ppos, block);
451 + path[ppos].p_block = path[ppos].p_idx->e_leaf;
452 + path[ppos].p_depth = i;
453 + path[ppos].p_ext = NULL;
455 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
457 + ext3_ext_drop_refs(path);
459 + return ERR_PTR(-EIO);
461 + eh = EXT_BLOCK_HDR(bh);
463 + EXT_ASSERT(ppos <= depth);
464 + path[ppos].p_bh = bh;
465 + path[ppos].p_hdr = eh;
469 + path[ppos].p_depth = i;
470 + path[ppos].p_hdr = eh;
471 + path[ppos].p_ext = NULL;
474 + ext3_ext_binsearch(tree, path + ppos, block);
476 + ext3_ext_show_path(tree, path);
482 + * insert new index [logical;ptr] into the block at cupr
483 + * it check where to insert: before curp or after curp
485 +static int ext3_ext_insert_index(handle_t *handle,
486 + struct ext3_extents_tree *tree,
487 + struct ext3_ext_path *curp,
488 + int logical, int ptr)
490 + struct ext3_extent_idx *ix;
493 + if ((err = ext3_ext_get_access(handle, tree, curp)))
496 + EXT_ASSERT(logical != curp->p_idx->e_block);
497 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
498 + if (logical > curp->p_idx->e_block) {
500 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
501 + len = (len - 1) * sizeof(struct ext3_extent_idx);
502 + len = len < 0 ? 0 : len;
503 + ext_debug(tree, "insert new index %d after: %d. "
504 + "move %d from 0x%p to 0x%p\n",
506 + (curp->p_idx + 1), (curp->p_idx + 2));
507 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
509 + ix = curp->p_idx + 1;
511 + /* insert before */
512 + len = len * sizeof(struct ext3_extent_idx);
513 + len = len < 0 ? 0 : len;
514 + ext_debug(tree, "insert new index %d before: %d. "
515 + "move %d from 0x%p to 0x%p\n",
517 + curp->p_idx, (curp->p_idx + 1));
518 + memmove(curp->p_idx + 1, curp->p_idx, len);
522 + ix->e_block = logical;
524 + curp->p_hdr->e_num++;
526 + EXT_ASSERT(curp->p_hdr->e_num <= curp->p_hdr->e_max);
527 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
529 + err = ext3_ext_dirty(handle, tree, curp);
530 + ext3_std_error(tree->inode->i_sb, err);
536 + * routine inserts new subtree into the path, using free index entry
538 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
539 + * - makes decision where to split
540 + * - moves remaining extens and index entries (right to the split point)
541 + * into the newly allocated blocks
542 + * - initialize subtree
544 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
545 + struct ext3_ext_path *path,
546 + struct ext3_extent *newext, int at)
548 + struct buffer_head *bh = NULL;
549 + int depth = EXT_DEPTH(tree);
550 + struct ext3_extent_header *neh;
551 + struct ext3_extent_idx *fidx;
552 + struct ext3_extent *ex;
553 + int i = at, k, m, a;
554 + unsigned long newblock, oldblock, border;
555 + int *ablocks = NULL; /* array of allocated blocks */
558 + /* make decision: where to split? */
559 + /* FIXME: now desicion is simplest: at current extent */
561 + /* if current leaf will be splitted, then we should use
562 + * border from split point */
563 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
564 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
565 + border = path[depth].p_ext[1].e_block;
566 + ext_debug(tree, "leaf will be splitted."
567 + " next leaf starts at %d\n",
570 + border = newext->e_block;
571 + ext_debug(tree, "leaf will be added."
572 + " next leaf starts at %d\n",
577 + * if error occurs, then we break processing
578 + * and turn filesystem read-only. so, index won't
579 + * be inserted and tree will be in consistent
580 + * state. next mount will repair buffers too
584 + * get array to track all allocated blocks
585 + * we need this to handle errors and free blocks
588 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
591 + memset(ablocks, 0, sizeof(unsigned long) * depth);
593 + /* allocate all needed blocks */
594 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
595 + for (a = 0; a < depth - at; a++) {
596 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
599 + ablocks[a] = newblock;
602 + /* initialize new leaf */
603 + newblock = ablocks[--a];
604 + EXT_ASSERT(newblock);
605 + bh = sb_getblk(tree->inode->i_sb, newblock);
612 + if ((err = ext3_journal_get_create_access(handle, bh)))
615 + neh = EXT_BLOCK_HDR(bh);
617 + neh->e_max = ext3_ext_space_block(tree);
618 + neh->e_magic = EXT3_EXT_MAGIC;
620 + ex = EXT_FIRST_EXTENT(neh);
622 + /* move remain of path[depth] to the new leaf */
623 + EXT_ASSERT(path[depth].p_hdr->e_num == path[depth].p_hdr->e_max);
624 + /* start copy from next extent */
625 + /* TODO: we could do it by single memmove */
627 + path[depth].p_ext++;
628 + while (path[depth].p_ext <=
629 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
630 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
631 + path[depth].p_ext->e_block,
632 + path[depth].p_ext->e_start,
633 + path[depth].p_ext->e_num,
635 + memmove(ex++, path[depth].p_ext++,
636 + sizeof(struct ext3_extent));
640 + mark_buffer_uptodate(bh, 1);
643 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
648 + /* correct old leaf */
650 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
652 + path[depth].p_hdr->e_num -= m;
653 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
658 + /* create intermediate indexes */
659 + k = depth - at - 1;
660 + EXT_ASSERT(k >= 0);
662 + ext_debug(tree, "create %d intermediate indices\n", k);
663 + /* insert new index into current index block */
664 + /* current depth stored in i var */
667 + oldblock = newblock;
668 + newblock = ablocks[--a];
669 + bh = sb_getblk(tree->inode->i_sb, newblock);
676 + if ((err = ext3_journal_get_create_access(handle, bh)))
679 + neh = EXT_BLOCK_HDR(bh);
681 + neh->e_magic = EXT3_EXT_MAGIC;
682 + neh->e_max = ext3_ext_space_block_idx(tree);
683 + neh->e_depth = depth - i;
684 + fidx = EXT_FIRST_INDEX(neh);
685 + fidx->e_block = border;
686 + fidx->e_leaf = oldblock;
688 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
689 + i, newblock, border, oldblock);
694 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
695 + EXT_MAX_INDEX(path[i].p_hdr));
696 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
697 + EXT_LAST_INDEX(path[i].p_hdr));
698 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
699 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
700 + i, path[i].p_idx->e_block,
701 + path[i].p_idx->e_leaf, newblock);
702 + memmove(++fidx, path[i].p_idx++,
703 + sizeof(struct ext3_extent_idx));
705 + EXT_ASSERT(neh->e_num <= neh->e_max);
708 + mark_buffer_uptodate(bh, 1);
711 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
716 + /* correct old index */
718 + err = ext3_ext_get_access(handle, tree, path + i);
721 + path[i].p_hdr->e_num -= m;
722 + err = ext3_ext_dirty(handle, tree, path + i);
730 + /* insert new index */
732 + err = ext3_ext_insert_index(handle, tree, path + at,
737 + if (buffer_locked(bh))
743 + /* free all allocated blocks in error case */
744 + for (i = 0; i < depth; i++)
747 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
755 + * routine implements tree growing procedure:
756 + * - allocates new block
757 + * - moves top-level data (index block or leaf) into the new block
758 + * - initialize new top-level, creating index that points to the
759 + * just created block
761 +static int ext3_ext_grow_indepth(handle_t *handle,
762 + struct ext3_extents_tree *tree,
763 + struct ext3_ext_path *path,
764 + struct ext3_extent *newext)
766 + struct ext3_ext_path *curp = path;
767 + struct ext3_extent_header *neh;
768 + struct ext3_extent_idx *fidx;
769 + struct buffer_head *bh;
770 + unsigned long newblock;
773 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
777 + bh = sb_getblk(tree->inode->i_sb, newblock);
780 + ext3_std_error(tree->inode->i_sb, err);
785 + if ((err = ext3_journal_get_create_access(handle, bh))) {
790 + /* move top-level index/leaf into new block */
791 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
793 + /* set size of new block */
794 + neh = EXT_BLOCK_HDR(bh);
795 + /* old root could have indexes or leaves
796 + * so calculate e_max right way */
797 + if (EXT_DEPTH(tree))
798 + neh->e_max = ext3_ext_space_block_idx(tree);
800 + neh->e_max = ext3_ext_space_block(tree);
801 + neh->e_magic = EXT3_EXT_MAGIC;
802 + mark_buffer_uptodate(bh, 1);
805 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
808 + /* create index in new top-level index: num,max,pointer */
809 + if ((err = ext3_ext_get_access(handle, tree, curp)))
812 + curp->p_hdr->e_magic = EXT3_EXT_MAGIC;
813 + curp->p_hdr->e_max = ext3_ext_space_root_idx(tree);
814 + curp->p_hdr->e_num = 1;
815 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
816 + /* FIXME: it works, but actually path[0] can be index */
817 + curp->p_idx->e_block = EXT_FIRST_EXTENT(path[0].p_hdr)->e_block;
818 + curp->p_idx->e_leaf = newblock;
820 + neh = EXT_ROOT_HDR(tree);
821 + fidx = EXT_FIRST_INDEX(neh);
822 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
823 + neh->e_num, neh->e_max, fidx->e_block, fidx->e_leaf);
825 + neh->e_depth = path->p_depth + 1;
826 + err = ext3_ext_dirty(handle, tree, curp);
834 + * routine finds empty index and adds new leaf. if no free index found
835 + * then it requests in-depth growing
837 +static int ext3_ext_create_new_leaf(handle_t *handle,
838 + struct ext3_extents_tree *tree,
839 + struct ext3_ext_path *path,
840 + struct ext3_extent *newext)
842 + struct ext3_ext_path *curp;
843 + int depth, i, err = 0;
846 + i = depth = EXT_DEPTH(tree);
848 + /* walk up to the tree and look for free index entry */
849 + curp = path + depth;
850 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
855 + /* we use already allocated block for index block
856 + * so, subsequent data blocks should be contigoues */
857 + if (EXT_HAS_FREE_INDEX(curp)) {
858 + /* if we found index with free entry, then use that
859 + * entry: create all needed subtree and add new leaf */
860 + err = ext3_ext_split(handle, tree, path, newext, i);
863 + ext3_ext_drop_refs(path);
864 + path = ext3_ext_find_extent(tree, newext->e_block, path);
866 + err = PTR_ERR(path);
868 + /* tree is full, time to grow in depth */
869 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
872 + ext3_ext_drop_refs(path);
873 + path = ext3_ext_find_extent(tree, newext->e_block, path);
875 + err = PTR_ERR(path);
878 + * only first (depth 0 -> 1) produces free space
879 + * in all other cases we have to split growed tree
881 + depth = EXT_DEPTH(tree);
882 + if (path[depth].p_hdr->e_num == path[depth].p_hdr->e_max) {
883 + /* now we need split */
895 + * returns allocated block in subsequent extent or 0xffffffff
896 + * NOTE: it consider block number from index entry as
897 + * allocated block. thus, index entries have to be consistent
900 +static unsigned long
901 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
905 + EXT_ASSERT(path != NULL);
906 + depth = path->p_depth;
908 + if (depth == 0 && path->p_ext == NULL)
911 + /* FIXME: what if index isn't full ?! */
912 + while (depth >= 0) {
913 + if (depth == path->p_depth) {
915 + if (path[depth].p_ext !=
916 + EXT_LAST_EXTENT(path[depth].p_hdr))
917 + return path[depth].p_ext[1].e_block;
920 + if (path[depth].p_idx !=
921 + EXT_LAST_INDEX(path[depth].p_hdr))
922 + return path[depth].p_idx[1].e_block;
931 + * returns first allocated block from next leaf or 0xffffffff
933 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
934 + struct ext3_ext_path *path)
938 + EXT_ASSERT(path != NULL);
939 + depth = path->p_depth;
941 + /* zero-tree has no leaf blocks at all */
945 + /* go to index block */
948 + while (depth >= 0) {
949 + if (path[depth].p_idx !=
950 + EXT_LAST_INDEX(path[depth].p_hdr))
951 + return path[depth].p_idx[1].e_block;
959 + * if leaf gets modified and modified extent is first in the leaf
960 + * then we have to correct all indexes above
961 + * TODO: do we need to correct tree in all cases?
963 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
964 + struct ext3_ext_path *path)
966 + struct ext3_extent_header *eh;
967 + int depth = EXT_DEPTH(tree);
968 + struct ext3_extent *ex;
969 + unsigned long border;
972 + eh = path[depth].p_hdr;
973 + ex = path[depth].p_ext;
978 + /* there is no tree at all */
982 + if (ex != EXT_FIRST_EXTENT(eh)) {
983 + /* we correct tree if first leaf got modified only */
988 + * TODO: we need correction if border is smaller then current one
991 + border = path[depth].p_ext->e_block;
992 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
994 + path[k].p_idx->e_block = border;
995 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
999 + /* change all left-side indexes */
1000 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1002 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1004 + path[k].p_idx->e_block = border;
1005 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1013 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1014 + struct ext3_extent *ex1,
1015 + struct ext3_extent *ex2)
1017 + if (ex1->e_block + ex1->e_num != ex2->e_block)
1020 +#ifdef AGRESSIVE_TEST
1021 + if (ex1->e_num >= 4)
1025 + if (!tree->mergable)
1028 + return tree->mergable(ex1, ex2);
1032 + * this routine tries to merge requsted extent into the existing
1033 + * extent or inserts requested extent as new one into the tree,
1034 + * creating new leaf in no-space case
1036 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1037 + struct ext3_ext_path *path,
1038 + struct ext3_extent *newext)
1040 + struct ext3_extent_header * eh;
1041 + struct ext3_extent *ex, *fex;
1042 + struct ext3_extent *nearex; /* nearest extent */
1043 + struct ext3_ext_path *npath = NULL;
1044 + int depth, len, err, next;
1046 + depth = EXT_DEPTH(tree);
1047 + ex = path[depth].p_ext;
1048 + EXT_ASSERT(path[depth].p_hdr);
1050 + /* try to insert block into found extent and return */
1051 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1052 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1053 + newext->e_num, ex->e_block, ex->e_num,
1055 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1057 + ex->e_num += newext->e_num;
1058 + eh = path[depth].p_hdr;
1064 + depth = EXT_DEPTH(tree);
1065 + eh = path[depth].p_hdr;
1066 + if (eh->e_num < eh->e_max)
1069 + /* probably next leaf has space for us? */
1070 + fex = EXT_LAST_EXTENT(eh);
1071 + next = ext3_ext_next_leaf_block(tree, path);
1072 + if (newext->e_block > fex->e_block && next != 0xffffffff) {
1073 + ext_debug(tree, "next leaf block - %d\n", next);
1074 + EXT_ASSERT(!npath);
1075 + npath = ext3_ext_find_extent(tree, next, NULL);
1076 + if (IS_ERR(npath))
1077 + return PTR_ERR(npath);
1078 + EXT_ASSERT(npath->p_depth == path->p_depth);
1079 + eh = npath[depth].p_hdr;
1080 + if (eh->e_num < eh->e_max) {
1081 + ext_debug(tree, "next leaf isnt full(%d)\n",
1086 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1087 + eh->e_num, eh->e_max);
1091 + * there is no free space in found leaf
1092 + * we're gonna add new leaf in the tree
1094 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1097 + depth = EXT_DEPTH(tree);
1098 + eh = path[depth].p_hdr;
1101 + nearex = path[depth].p_ext;
1103 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1107 + /* there is no extent in this leaf, create first one */
1108 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1109 + newext->e_block, newext->e_start,
1111 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1112 + } else if (newext->e_block > nearex->e_block) {
1113 + EXT_ASSERT(newext->e_block != nearex->e_block);
1114 + if (nearex != EXT_LAST_EXTENT(eh)) {
1115 + len = EXT_MAX_EXTENT(eh) - nearex;
1116 + len = (len - 1) * sizeof(struct ext3_extent);
1117 + len = len < 0 ? 0 : len;
1118 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1119 + "move %d from 0x%p to 0x%p\n",
1120 + newext->e_block, newext->e_start,
1122 + nearex, len, nearex + 1, nearex + 2);
1123 + memmove(nearex + 2, nearex + 1, len);
1125 + path[depth].p_ext = nearex + 1;
1127 + EXT_ASSERT(newext->e_block != nearex->e_block);
1128 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1129 + len = len < 0 ? 0 : len;
1130 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1131 + "move %d from 0x%p to 0x%p\n",
1132 + newext->e_block, newext->e_start, newext->e_num,
1133 + nearex, len, nearex + 1, nearex + 2);
1134 + memmove(nearex + 1, nearex, len);
1135 + path[depth].p_ext = nearex;
1139 + nearex = path[depth].p_ext;
1140 + nearex->e_block = newext->e_block;
1141 + nearex->e_start = newext->e_start;
1142 + nearex->e_num = newext->e_num;
1145 + /* try to merge extents to the right */
1146 + while (nearex < EXT_LAST_EXTENT(eh)) {
1147 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1149 + /* merge with next extent! */
1150 + nearex->e_num += nearex[1].e_num;
1151 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1152 + len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1153 + * sizeof(struct ext3_extent);
1154 + memmove(nearex + 1, nearex + 2, len);
1157 + EXT_ASSERT(eh->e_num > 0);
1160 + /* try to merge extents to the left */
1162 + /* time to correct all indexes above */
1163 + err = ext3_ext_correct_indexes(handle, tree, path);
1167 + err = ext3_ext_dirty(handle, tree, path + depth);
1171 + ext3_ext_drop_refs(npath);
1174 + ext3_ext_tree_changed(tree);
1178 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1179 + unsigned long num, ext_prepare_callback func)
1181 + struct ext3_ext_path *path = NULL;
1182 + struct ext3_extent *ex, cbex;
1183 + unsigned long next, start = 0, end = 0;
1184 + unsigned long last = block + num;
1185 + int depth, exists, err = 0;
1189 + EXT_ASSERT(tree->inode);
1190 + EXT_ASSERT(tree->root);
1192 + while (block < last && block != 0xfffffffff) {
1193 + num = last - block;
1194 + /* find extent for this block */
1195 + path = ext3_ext_find_extent(tree, block, path);
1196 + if (IS_ERR(path)) {
1197 + err = PTR_ERR(path);
1202 + depth = EXT_DEPTH(tree);
1203 + EXT_ASSERT(path[depth].p_hdr);
1204 + ex = path[depth].p_ext;
1205 + next = ext3_ext_next_allocated_block(path);
1209 + /* there is no extent yet, so try to allocate
1210 + * all requested space */
1212 + end = block + num;
1213 + } else if (ex->e_block > block) {
1214 + /* need to allocate space before found extent */
1216 + end = ex->e_block;
1217 + if (block + num < end)
1218 + end = block + num;
1219 + } else if (block >= ex->e_block + ex->e_num) {
1220 + /* need to allocate space after found extent */
1222 + end = block + num;
1225 + } else if (block >= ex->e_block) {
1227 + * some part of requested space is covered
1231 + end = ex->e_block + ex->e_num;
1232 + if (block + num < end)
1233 + end = block + num;
1238 + EXT_ASSERT(end > start);
1241 + cbex.e_block = start;
1242 + cbex.e_num = end - start;
1247 + EXT_ASSERT(path[depth].p_hdr);
1248 + err = func(tree, path, &cbex, exists);
1249 + ext3_ext_drop_refs(path);
1253 + if (err == EXT_REPEAT)
1255 + else if (err == EXT_BREAK) {
1260 + if (EXT_DEPTH(tree) != depth) {
1261 + /* depth was changed. we have to realloc path */
1266 + block = cbex.e_block + cbex.e_num;
1270 + ext3_ext_drop_refs(path);
1278 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
1281 + tree->cex->e_num = 0;
1285 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, struct ext3_extent *ex)
1289 + EXT_ASSERT(ex->e_num);
1290 + tree->cex->e_block = ex->e_block;
1291 + tree->cex->e_start = ex->e_start;
1292 + tree->cex->e_num = ex->e_num;
1297 + * this routine calculate boundaries of the gap requested block fits into
1298 + * and cache this gap
1301 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1302 + struct ext3_ext_path *path,
1303 + unsigned long block)
1305 + int depth = EXT_DEPTH(tree);
1306 + struct ext3_extent *ex, gex;
1311 + ex = path[depth].p_ext;
1313 + /* there is no extent yet, so gap is [0;-] */
1315 + gex.e_num = 0xffffffff;
1316 + ext_debug(tree, "cache gap(whole file):");
1317 + } else if (block < ex->e_block) {
1318 + gex.e_block = block;
1319 + gex.e_num = ex->e_block - block;
1320 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1321 + (unsigned long) block,
1322 + (unsigned long) ex->e_block,
1323 + (unsigned long) ex->e_num);
1324 + } else if (block >= ex->e_block + ex->e_num) {
1325 + gex.e_block = ex->e_block + ex->e_num;
1326 + gex.e_num = ext3_ext_next_allocated_block(path);
1327 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1328 + (unsigned long) ex->e_block,
1329 + (unsigned long) ex->e_num,
1330 + (unsigned long) block);
1331 + EXT_ASSERT(gex.e_num > gex.e_block);
1332 + gex.e_num = gex.e_num - gex.e_block;
1337 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.e_block,
1338 + (unsigned long) gex.e_num);
1339 + gex.e_start = 0xffffffff;
1340 + ext3_ext_put_in_cache(tree, &gex);
1344 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1345 + struct ext3_extent *ex)
1347 + struct ext3_extent *cex = tree->cex;
1349 + /* is there cache storage at all? */
1353 + /* has cache valid data? */
1354 + if (cex->e_num == 0)
1357 + if (block >= cex->e_block && block < cex->e_block + cex->e_num) {
1358 + ex->e_block = cex->e_block;
1359 + ex->e_start = cex->e_start;
1360 + ex->e_num = cex->e_num;
1361 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1362 + (unsigned long) block,
1363 + (unsigned long) ex->e_block,
1364 + (unsigned long) ex->e_num,
1365 + (unsigned long) ex->e_start);
1369 + /* not in cache */
1374 + * routine removes index from the index block
1375 + * it's used in truncate case only. thus all requests are for
1376 + * last index in the block only
1378 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1379 + struct ext3_ext_path *path)
1381 + struct buffer_head *bh;
1384 + /* free index block */
1386 + EXT_ASSERT(path->p_hdr->e_num);
1387 + if ((err = ext3_ext_get_access(handle, tree, path)))
1389 + path->p_hdr->e_num--;
1390 + if ((err = ext3_ext_dirty(handle, tree, path)))
1392 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1393 + path->p_idx->e_leaf);
1394 + bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->e_leaf);
1395 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->e_leaf);
1396 + ext3_free_blocks(handle, tree->inode, path->p_idx->e_leaf, 1);
1400 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1401 + struct ext3_ext_path *path)
1403 + int depth = EXT_DEPTH(tree);
1407 + /* probably there is space in leaf? */
1408 + if (path[depth].p_hdr->e_num < path[depth].p_hdr->e_max)
1413 + * the worste case we're expecting is creation of the
1414 + * new root (growing in depth) with index splitting
1415 + * for splitting we have to consider depth + 1 because
1416 + * previous growing could increase it
1418 + depth = depth + 1;
1421 + * growing in depth:
1422 + * block allocation + new root + old root
1424 + needed = EXT3_ALLOC_NEEDED + 2;
1426 + /* index split. we may need:
1427 + * allocate intermediate indexes and new leaf
1428 + * change two blocks at each level, but root
1429 + * modify root block (inode)
1431 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1437 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1438 + struct ext3_ext_path *path, unsigned long start,
1439 + unsigned long end)
1441 + struct ext3_extent *ex, tex;
1442 + struct ext3_ext_path *npath;
1443 + int depth, creds, err;
1445 + depth = EXT_DEPTH(tree);
1446 + ex = path[depth].p_ext;
1448 + EXT_ASSERT(end < ex->e_block + ex->e_num - 1);
1449 + EXT_ASSERT(ex->e_block < start);
1451 + /* calculate tail extent */
1452 + tex.e_block = end + 1;
1453 + EXT_ASSERT(tex.e_block < ex->e_block + ex->e_num);
1454 + tex.e_num = ex->e_block + ex->e_num - tex.e_block;
1456 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1457 + handle = ext3_ext_journal_restart(handle, creds);
1458 + if (IS_ERR(handle))
1459 + return PTR_ERR(handle);
1461 + /* calculate head extent. use primary extent */
1462 + err = ext3_ext_get_access(handle, tree, path + depth);
1465 + ex->e_num = start - ex->e_block;
1466 + err = ext3_ext_dirty(handle, tree, path + depth);
1470 + /* FIXME: some callback to free underlying resource
1471 + * and correct e_start? */
1472 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1473 + ex->e_block, ex->e_num, tex.e_block, tex.e_num);
1475 + npath = ext3_ext_find_extent(tree, ex->e_block, NULL);
1476 + if (IS_ERR(npath))
1477 + return PTR_ERR(npath);
1478 + depth = EXT_DEPTH(tree);
1479 + EXT_ASSERT(npath[depth].p_ext->e_block == ex->e_block);
1480 + EXT_ASSERT(npath[depth].p_ext->e_num == ex->e_num);
1482 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1483 + ext3_ext_drop_refs(npath);
1491 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1492 + struct ext3_ext_path *path, unsigned long start,
1493 + unsigned long end)
1495 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1496 + int err = 0, correct_index = 0;
1497 + int depth = EXT_DEPTH(tree), credits;
1498 + struct ext3_extent_header *eh;
1499 + unsigned a, b, block, num;
1501 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1502 + if (!path[depth].p_hdr)
1503 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1504 + eh = path[depth].p_hdr;
1506 + EXT_ASSERT(eh->e_num <= eh->e_max);
1507 + EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
1509 + /* find where to start removing */
1510 + le = ex = EXT_LAST_EXTENT(eh);
1511 + while (ex != EXT_FIRST_EXTENT(eh)) {
1512 + if (ex->e_block <= end)
1517 + if (start > ex->e_block && end < ex->e_block + ex->e_num - 1) {
1518 + /* removal of internal part of the extent requested
1519 + * tail and head must be placed in different extent
1520 + * so, we have to insert one more extent */
1521 + path[depth].p_ext = ex;
1522 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1526 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1527 + ex->e_block + ex->e_num > start) {
1528 + ext_debug(tree, "remove ext %u:%u\n", ex->e_block, ex->e_num);
1529 + path[depth].p_ext = ex;
1531 + a = ex->e_block > start ? ex->e_block : start;
1532 + b = ex->e_block + ex->e_num - 1 < end ?
1533 + ex->e_block + ex->e_num - 1 : end;
1535 + ext_debug(tree, " border %u:%u\n", a, b);
1537 + if (a != ex->e_block && b != ex->e_block + ex->e_num - 1) {
1541 + } else if (a != ex->e_block) {
1542 + /* remove tail of the extent */
1543 + block = ex->e_block;
1545 + } else if (b != ex->e_block + ex->e_num - 1) {
1546 + /* remove head of the extent */
1550 + /* remove whole extent: excelent! */
1551 + block = ex->e_block;
1553 + EXT_ASSERT(a == ex->e_block &&
1554 + b == ex->e_block + ex->e_num - 1);
1557 + if (ex == EXT_FIRST_EXTENT(eh))
1558 + correct_index = 1;
1561 + if (correct_index)
1562 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1563 + if (tree->remove_extent_credits)
1564 + credits += tree->remove_extent_credits(tree, ex, a, b);
1566 + handle = ext3_ext_journal_restart(handle, credits);
1567 + if (IS_ERR(handle)) {
1568 + err = PTR_ERR(handle);
1572 + err = ext3_ext_get_access(handle, tree, path + depth);
1576 + if (tree->remove_extent)
1577 + err = tree->remove_extent(tree, ex, a, b);
1582 + /* this extent is removed entirely mark slot unused */
1588 + ex->e_block = block;
1591 + err = ext3_ext_dirty(handle, tree, path + depth);
1595 + ext_debug(tree, "new extent: %u:%u:%u\n",
1596 + ex->e_block, ex->e_num, ex->e_start);
1601 + /* reuse unused slots */
1603 + if (lu->e_start) {
1612 + if (correct_index && eh->e_num)
1613 + err = ext3_ext_correct_indexes(handle, tree, path);
1615 + /* if this leaf is free, then we should
1616 + * remove it from index block above */
1617 + if (err == 0 && eh->e_num == 0 && path[depth].p_bh != NULL)
1618 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1625 +static struct ext3_extent_idx *
1626 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1628 + struct ext3_extent_idx *ix;
1630 + ix = EXT_LAST_INDEX(hdr);
1631 + while (ix != EXT_FIRST_INDEX(hdr)) {
1632 + if (ix->e_block <= block)
1640 + * returns 1 if current index have to be freed (even partial)
1643 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1645 + EXT_ASSERT(path->p_idx);
1647 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1651 + * if truncate on deeper level happened it it wasn't partial
1652 + * so we have to consider current index for truncation
1654 + if (path->p_hdr->e_num == path->p_block)
1659 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1660 + unsigned long start, unsigned long end)
1662 + struct inode *inode = tree->inode;
1663 + struct super_block *sb = inode->i_sb;
1664 + int depth = EXT_DEPTH(tree);
1665 + struct ext3_ext_path *path;
1667 + int i = 0, err = 0;
1669 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1671 + /* probably first extent we're gonna free will be last in block */
1672 + handle = ext3_journal_start(inode, depth + 1);
1673 + if (IS_ERR(handle))
1674 + return PTR_ERR(handle);
1676 + ext3_ext_invalidate_cache(tree);
1679 + * we start scanning from right side freeing all the blocks
1680 + * after i_size and walking into the deep
1682 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1683 + if (IS_ERR(path)) {
1684 + ext3_error(sb, "ext3_ext_remove_space",
1685 + "Can't allocate path array");
1686 + ext3_journal_stop(handle, inode);
1689 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1690 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1692 + while (i >= 0 && err == 0) {
1694 + /* this is leaf block */
1695 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1696 + /* root level have p_bh == NULL, brelse() eats this */
1697 + brelse(path[i].p_bh);
1702 + /* this is index block */
1703 + if (!path[i].p_hdr) {
1704 + ext_debug(tree, "initialize header\n");
1705 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1708 + EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1709 + EXT_ASSERT(path[i].p_hdr->e_magic == EXT3_EXT_MAGIC);
1711 + if (!path[i].p_idx) {
1712 + /* this level hasn't touched yet */
1714 + ext3_ext_last_covered(path[i].p_hdr, end);
1715 + path[i].p_block = path[i].p_hdr->e_num + 1;
1716 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1717 + path[i].p_hdr, path[i].p_hdr->e_num);
1719 + /* we've already was here, see at next index */
1723 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1724 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1726 + if (ext3_ext_more_to_rm(path + i)) {
1727 + /* go to the next level */
1728 + ext_debug(tree, "move to level %d (block %d)\n",
1729 + i + 1, path[i].p_idx->e_leaf);
1730 + memset(path + i + 1, 0, sizeof(*path));
1731 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->e_leaf);
1732 + if (!path[i+1].p_bh) {
1733 + /* should we reset i_size? */
1737 + /* put actual number of indexes to know is this
1738 + * number got changed at the next iteration */
1739 + path[i].p_block = path[i].p_hdr->e_num;
1742 + /* we finish processing this index, go up */
1743 + if (path[i].p_hdr->e_num == 0 && i > 0) {
1744 + /* index is empty, remove it
1745 + * handle must be already prepared by the
1746 + * truncate_leaf() */
1747 + err = ext3_ext_rm_idx(handle, tree, path + i);
1749 + /* root level have p_bh == NULL, brelse() eats this */
1750 + brelse(path[i].p_bh);
1752 + ext_debug(tree, "return to level %d\n", i);
1756 + /* TODO: flexible tree reduction should be here */
1757 + if (path->p_hdr->e_num == 0) {
1759 + * truncate to zero freed all the tree
1760 + * so, we need to correct e_depth
1762 + err = ext3_ext_get_access(handle, tree, path);
1764 + EXT_ROOT_HDR(tree)->e_depth = 0;
1765 + err = ext3_ext_dirty(handle, tree, path);
1768 + ext3_ext_tree_changed(tree);
1771 + ext3_journal_stop(handle, inode);
1777 + * called at mount time
1779 +void ext3_ext_init(struct super_block *sb)
1782 + * possible initialization would be here
1785 + if (test_opt(sb, EXTENTS)) {
1786 + printk("EXT3-fs: file extents enabled");
1787 +#ifdef AGRESSIVE_TEST
1788 + printk(", agressive tests");
1790 +#ifdef CHECK_BINSEARCH
1791 + printk(", check binsearch");
1798 + * called at umount time
1800 +void ext3_ext_release(struct super_block *sb)
1804 +/************************************************************************
1805 + * VFS related routines
1806 + ************************************************************************/
1808 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1810 + /* we use in-core data, not bh */
1814 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1816 + struct inode *inode = buffer;
1817 + return ext3_mark_inode_dirty(handle, inode);
1820 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1821 + struct ext3_extent *ex2)
1823 + if (ex1->e_start + ex1->e_num == ex2->e_start)
1829 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1830 + struct ext3_extent *ex,
1831 + unsigned long from, unsigned long to)
1835 + /* at present, extent can't cross block group */;
1836 + needed = 3; /* bitmap + group desc + sb */
1838 +#ifdef CONFIG_QUOTA
1839 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1845 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1846 + struct ext3_extent *ex,
1847 + unsigned long from, unsigned long to)
1849 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1850 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1851 + struct buffer_head *bh;
1854 + if (IS_ERR(handle))
1855 + return PTR_ERR(handle);
1856 + if (from >= ex->e_block && to == ex->e_block + ex->e_num - 1) {
1857 + /* tail removal */
1858 + unsigned long num, start;
1859 + num = ex->e_block + ex->e_num - from;
1860 + start = ex->e_start + ex->e_num - num;
1861 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1863 + for (i = 0; i < num; i++) {
1864 + bh = sb_get_hash_table(tree->inode->i_sb, start + i);
1865 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1867 + ext3_free_blocks(handle, tree->inode, start, num);
1868 + } else if (from == ex->e_block && to <= ex->e_block + ex->e_num - 1) {
1869 + printk("strange request: removal %lu-%lu from %u:%u\n",
1870 + from, to, ex->e_block, ex->e_num);
1872 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1873 + from, to, ex->e_block, ex->e_num);
1875 + ext3_journal_stop(handle, tree->inode);
1879 +static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
1880 + unsigned long block)
1882 + struct ext3_inode_info *ei = EXT3_I(inode);
1883 + unsigned long bg_start;
1884 + unsigned long colour;
1888 + struct ext3_extent *ex;
1889 + depth = path->p_depth;
1891 + /* try to predict block placement */
1892 + if ((ex = path[depth].p_ext))
1893 + return ex->e_start + (block - ex->e_block);
1895 + /* it looks index is empty
1896 + * try to find starting from index itself */
1897 + if (path[depth].p_bh)
1898 + return path[depth].p_bh->b_blocknr;
1901 + /* OK. use inode's group */
1902 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1903 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1904 + colour = (current->pid % 16) *
1905 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1906 + return bg_start + colour + block;
1909 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1910 + struct ext3_ext_path *path,
1911 + struct ext3_extent *ex, int *err)
1913 + struct inode *inode = tree->inode;
1914 + int newblock, goal;
1918 + EXT_ASSERT(ex->e_start);
1919 + EXT_ASSERT(ex->e_num);
1921 + /* reuse block from the extent to order data/metadata */
1922 + newblock = ex->e_start++;
1924 + if (ex->e_num == 0) {
1926 + /* allocate new block for the extent */
1927 + goal = ext3_ext_find_goal(inode, path, ex->e_block);
1928 + ex->e_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1929 + if (ex->e_start == 0) {
1930 + /* error occured: restore old extent */
1931 + ex->e_start = newblock;
1938 +static void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1939 + struct inode *inode)
1941 + tree->inode = inode;
1942 + tree->root = (void *) EXT3_I(inode)->i_data;
1943 + tree->get_write_access = ext3_get_inode_write_access;
1944 + tree->mark_buffer_dirty = ext3_mark_buffer_dirty;
1945 + tree->mergable = ext3_ext_mergable;
1946 + tree->new_block = ext3_new_block_cb;
1947 + tree->remove_extent = ext3_remove_blocks;
1948 + tree->remove_extent_credits = ext3_remove_blocks_credits;
1949 + tree->buffer = (void *) inode;
1950 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1951 + tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1954 +#if EXT3_MULTIBLOCK_ALLOCATOR
1956 +ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
1957 + struct ext3_ext_path *path,
1958 + struct ext3_extent *newex, int exist)
1960 + struct inode *inode = tree->inode;
1961 + struct buffer_head *bh;
1962 + int count, err, goal;
1963 + unsigned long pblock;
1964 + unsigned long tgen;
1965 + loff_t new_i_size;
1970 + return EXT_CONTINUE;
1972 + tgen = EXT_GENERATION(tree);
1973 + count = ext3_ext_calc_credits_for_insert(tree, path);
1974 + up_write(&EXT3_I(inode)->truncate_sem);
1976 + handle = ext3_journal_start(inode, count + EXT3_ALLOC_NEEDED + 1);
1977 + if (IS_ERR(handle)) {
1978 + down_write(&EXT3_I(inode)->truncate_sem);
1979 + return PTR_ERR(handle);
1982 + if (tgen != EXT_GENERATION(tree)) {
1983 + /* the tree has changed. so path can be invalid at moment */
1984 + ext3_journal_stop(handle, inode);
1985 + down_write(&EXT3_I(inode)->truncate_sem);
1986 + return EXT_REPEAT;
1989 + down_write(&EXT3_I(inode)->truncate_sem);
1990 + goal = ext3_ext_find_goal(inode, path, newex->e_block);
1991 + count = newex->e_num;
1992 + pblock = ext3_new_blocks(handle, inode, &count, goal, &err);
1995 + EXT_ASSERT(count <= newex->e_num);
1997 + /* insert new extent */
1998 + newex->e_start = pblock;
1999 + newex->e_num = count;
2000 + err = ext3_ext_insert_extent(handle, tree, path, newex);
2004 + /* block have been allocated for data, so time to drop dirty
2005 + * in correspondend buffer_heads to prevent corruptions */
2006 + for (i = 0; i < newex->e_num; i++) {
2007 + bh = sb_get_hash_table(inode->i_sb, newex->e_start + i);
2009 + mark_buffer_clean(bh);
2010 + wait_on_buffer(bh);
2011 + clear_bit(BH_Req, &bh->b_state);
2016 + /* correct on-disk inode size */
2017 + if (newex->e_num > 0) {
2018 + new_i_size = (loff_t) newex->e_block + newex->e_num;
2019 + new_i_size = new_i_size << inode->i_blkbits;
2020 + if (new_i_size > EXT3_I(inode)->i_disksize) {
2021 + EXT3_I(inode)->i_disksize = new_i_size;
2022 + err = ext3_mark_inode_dirty(handle, inode);
2027 + ext3_journal_stop(handle, inode);
2032 +int ext3_ext_allocate_nblocks(struct inode *inode, unsigned long block,
2033 + unsigned long num)
2035 + struct ext3_extents_tree tree;
2038 + ext3_init_tree_desc(&tree, inode);
2039 + ext_debug(&tree, "blocks %lu-%lu requested for inode %u\n",
2040 + block, block + num,(unsigned) inode->i_ino);
2041 + down_write(&EXT3_I(inode)->truncate_sem);
2042 + err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
2043 + ext3_ext_invalidate_cache(&tree);
2044 + up_write(&EXT3_I(inode)->truncate_sem);
2050 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2051 + long iblock, struct buffer_head *bh_result,
2052 + int create, int extend_disksize)
2054 + struct ext3_ext_path *path = NULL;
2055 + struct ext3_extent newex;
2056 + struct ext3_extent *ex;
2057 + int goal, newblock, err = 0, depth;
2058 + struct ext3_extents_tree tree;
2060 + clear_bit(BH_New, &bh_result->b_state);
2061 + ext3_init_tree_desc(&tree, inode);
2062 + ext_debug(&tree, "block %d requested for inode %u\n",
2063 + (int) iblock, (unsigned) inode->i_ino);
2064 + down_write(&EXT3_I(inode)->truncate_sem);
2066 + /* check in cache */
2067 + if (ext3_ext_in_cache(&tree, iblock, &newex)) {
2068 + if (newex.e_start == 0xffffffff && !create) {
2069 + /* block isn't allocated yet and
2070 + * user don't want to allocate it */
2072 + } else if (newex.e_start) {
2073 + /* block is already allocated */
2074 + newblock = iblock - newex.e_block + newex.e_start;
2079 + /* find extent for this block */
2080 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2081 + if (IS_ERR(path)) {
2082 + err = PTR_ERR(path);
2087 + depth = EXT_DEPTH(&tree);
2090 + * consistent leaf must not be empty
2091 + * this situations is possible, though, _during_ tree modification
2092 + * this is why assert can't be put in ext3_ext_find_extent()
2094 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2096 + if ((ex = path[depth].p_ext)) {
2097 + /* if found exent covers block, simple return it */
2098 + if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
2099 + newblock = iblock - ex->e_block + ex->e_start;
2100 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2101 + (int) iblock, ex->e_block, ex->e_num,
2103 + ext3_ext_put_in_cache(&tree, ex);
2109 + * requested block isn't allocated yet
2110 + * we couldn't try to create block if create flag is zero
2113 + /* put just found gap into cache to speedup subsequest reqs */
2114 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2118 + /* allocate new block */
2119 + goal = ext3_ext_find_goal(inode, path, iblock);
2120 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2123 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2126 + /* try to insert new extent into found leaf and return */
2127 + newex.e_block = iblock;
2128 + newex.e_start = newblock;
2130 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2134 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2135 + EXT3_I(inode)->i_disksize = inode->i_size;
2137 + /* previous routine could use block we allocated */
2138 + newblock = newex.e_start;
2139 + set_bit(BH_New, &bh_result->b_state);
2141 + ext3_ext_put_in_cache(&tree, &newex);
2143 + ext3_ext_show_leaf(&tree, path);
2144 + set_bit(BH_Mapped, &bh_result->b_state);
2145 + bh_result->b_dev = inode->i_sb->s_dev;
2146 + bh_result->b_blocknr = newblock;
2149 + ext3_ext_drop_refs(path);
2152 + up_write(&EXT3_I(inode)->truncate_sem);
2157 +void ext3_ext_truncate(struct inode * inode)
2159 + struct address_space *mapping = inode->i_mapping;
2160 + struct super_block *sb = inode->i_sb;
2161 + struct ext3_extents_tree tree;
2162 + unsigned long last_block;
2166 + ext3_init_tree_desc(&tree, inode);
2169 + * probably first extent we're gonna free will be last in block
2171 + err = ext3_writepage_trans_blocks(inode) + 3;
2172 + handle = ext3_journal_start(inode, err);
2173 + if (IS_ERR(handle))
2176 + ext3_block_truncate_page(handle, mapping, inode->i_size);
2178 + down_write(&EXT3_I(inode)->truncate_sem);
2179 + ext3_ext_invalidate_cache(&tree);
2182 + * TODO: optimization is possible here
2183 + * probably we need not scaning at all,
2184 + * because page truncation is enough
2186 + if (ext3_orphan_add(handle, inode))
2189 + /* we have to know where to truncate from in crash case */
2190 + EXT3_I(inode)->i_disksize = inode->i_size;
2191 + ext3_mark_inode_dirty(handle, inode);
2193 + last_block = (inode->i_size + sb->s_blocksize - 1)
2194 + >> EXT3_BLOCK_SIZE_BITS(sb);
2195 + err = ext3_ext_remove_space(&tree, last_block, 0xffffffff);
2197 + /* In a multi-transaction truncate, we only make the final
2198 + * transaction synchronous */
2199 + if (IS_SYNC(inode))
2200 + handle->h_sync = 1;
2204 + * If this was a simple ftruncate(), and the file will remain alive
2205 + * then we need to clear up the orphan record which we created above.
2206 + * However, if this was a real unlink then we were called by
2207 + * ext3_delete_inode(), and we allow that function to clean up the
2208 + * orphan info for us.
2210 + if (inode->i_nlink)
2211 + ext3_orphan_del(handle, inode);
2213 + up_write(&EXT3_I(inode)->truncate_sem);
2214 + ext3_journal_stop(handle, inode);
2218 + * this routine calculate max number of blocks we could modify
2219 + * in order to allocate new block for an inode
2221 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2223 + struct ext3_extents_tree tree;
2226 + ext3_init_tree_desc(&tree, inode);
2228 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2230 + /* caller want to allocate num blocks */
2233 +#ifdef CONFIG_QUOTA
2235 + * FIXME: real calculation should be here
2236 + * it depends on blockmap format of qouta file
2238 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2244 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2246 + struct ext3_extents_tree tree;
2248 + ext3_init_tree_desc(&tree, inode);
2249 + ext3_extent_tree_init(handle, &tree);
2253 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2254 + struct ext3_ext_path *path,
2255 + struct ext3_extent *newex, int exist)
2257 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2260 + return EXT_CONTINUE;
2263 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2266 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2268 + buf->cur += sizeof(*newex);
2270 + buf->err = -EFAULT;
2273 + return EXT_CONTINUE;
2277 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2278 + struct ext3_ext_path *path,
2279 + struct ext3_extent *ex, int exist)
2281 + struct ext3_extent_tree_stats *buf =
2282 + (struct ext3_extent_tree_stats *) tree->private;
2286 + return EXT_CONTINUE;
2288 + depth = EXT_DEPTH(tree);
2289 + buf->extents_num++;
2290 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2292 + return EXT_CONTINUE;
2295 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2296 + unsigned long arg)
2300 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2303 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2304 + struct ext3_extent_buf buf;
2305 + struct ext3_extents_tree tree;
2307 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2310 + ext3_init_tree_desc(&tree, inode);
2311 + buf.cur = buf.buffer;
2313 + tree.private = &buf;
2314 + down_write(&EXT3_I(inode)->truncate_sem);
2315 + err = ext3_ext_walk_space(&tree, buf.start, 0xffffffff,
2316 + ext3_ext_store_extent_cb);
2317 + up_write(&EXT3_I(inode)->truncate_sem);
2320 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2321 + struct ext3_extent_tree_stats buf;
2322 + struct ext3_extents_tree tree;
2324 + ext3_init_tree_desc(&tree, inode);
2325 + down_write(&EXT3_I(inode)->truncate_sem);
2326 + buf.depth = EXT_DEPTH(&tree);
2327 + buf.extents_num = 0;
2329 + tree.private = &buf;
2330 + err = ext3_ext_walk_space(&tree, 0, 0xffffffff,
2331 + ext3_ext_collect_stats_cb);
2332 + up_write(&EXT3_I(inode)->truncate_sem);
2334 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2335 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2336 + struct ext3_extents_tree tree;
2337 + ext3_init_tree_desc(&tree, inode);
2338 + down_write(&EXT3_I(inode)->truncate_sem);
2339 + err = EXT_DEPTH(&tree);
2340 + up_write(&EXT3_I(inode)->truncate_sem);
2346 +EXPORT_SYMBOL(ext3_init_tree_desc);
2347 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2348 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2349 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2350 +EXPORT_SYMBOL(ext3_ext_walk_space);
2351 +EXPORT_SYMBOL(ext3_ext_find_goal);
2352 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2354 Index: linux-2.4.21-suse2/fs/ext3/ialloc.c
2355 ===================================================================
2356 --- linux-2.4.21-suse2.orig/fs/ext3/ialloc.c 2004-02-05 20:42:40.000000000 +0300
2357 +++ linux-2.4.21-suse2/fs/ext3/ialloc.c 2004-02-05 20:42:40.000000000 +0300
2358 @@ -592,6 +592,10 @@
2362 + if (test_opt(sb, EXTENTS)) {
2363 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2364 + ext3_extents_initialize_blockmap(handle, inode);
2366 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2369 Index: linux-2.4.21-suse2/fs/ext3/inode.c
2370 ===================================================================
2371 --- linux-2.4.21-suse2.orig/fs/ext3/inode.c 2004-02-05 20:42:40.000000000 +0300
2372 +++ linux-2.4.21-suse2/fs/ext3/inode.c 2004-02-05 20:42:40.000000000 +0300
2373 @@ -853,6 +853,18 @@
2378 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2379 + struct buffer_head *bh, int create, int extend_disksize)
2381 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2382 + return ext3_ext_get_block(handle, inode,
2383 + block, bh, create,
2385 + return ext3_get_block_handle(handle, inode,
2386 + block, bh, create, extend_disksize);
2390 * The BKL is not held on entry here.
2393 handle = ext3_journal_current_handle();
2394 J_ASSERT(handle != 0);
2396 - ret = ext3_get_block_handle(handle, inode, iblock,
2397 + ret = ext3_get_block_wrap(handle, inode, iblock,
2398 bh_result, create, 1);
2405 - ret = ext3_get_block_handle(handle, inode, iblock,
2406 + ret = ext3_get_block_wrap(handle, inode, iblock,
2407 bh_result, create, 0);
2409 bh_result->b_size = (1 << inode->i_blkbits);
2412 dummy.b_blocknr = -1000;
2413 buffer_trace_init(&dummy.b_history);
2414 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2415 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2416 if (!*errp && buffer_mapped(&dummy)) {
2417 struct buffer_head *bh;
2418 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2419 @@ -1502,7 +1514,7 @@
2420 * This required during truncate. We need to physically zero the tail end
2421 * of that block so it doesn't yield old data if the file is later grown.
2423 -static int ext3_block_truncate_page(handle_t *handle,
2424 +int ext3_block_truncate_page(handle_t *handle,
2425 struct address_space *mapping, loff_t from)
2427 unsigned long index = from >> PAGE_CACHE_SHIFT;
2428 @@ -1987,6 +1999,9 @@
2430 ext3_discard_prealloc(inode);
2432 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2433 + return ext3_ext_truncate(inode);
2435 handle = start_transaction(inode);
2437 return; /* AKPM: return what? */
2438 @@ -2663,6 +2678,9 @@
2439 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2442 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2443 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2445 if (ext3_should_journal_data(inode))
2446 ret = 3 * (bpp + indirects) + 2;
2448 @@ -3099,7 +3117,7 @@
2450 /* alloc blocks one by one */
2451 for (i = 0; i < nblocks; i++) {
2452 - ret = ext3_get_block_handle(handle, inode, blocks[i],
2453 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
2457 @@ -3175,7 +3193,7 @@
2461 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1, 1);
2462 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1, 1);
2464 printk(KERN_INFO "ext3_map_inode_page: error %d "
2465 "allocating block %ld\n", rc, iblock);
2466 Index: linux-2.4.21-suse2/fs/ext3/Makefile
2467 ===================================================================
2468 --- linux-2.4.21-suse2.orig/fs/ext3/Makefile 2004-02-05 20:42:39.000000000 +0300
2469 +++ linux-2.4.21-suse2/fs/ext3/Makefile 2004-02-05 20:43:47.000000000 +0300
2471 export-objs := ext3-exports.o
2473 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2474 - ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o
2475 + ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2477 +export-objs += extents.o
2478 obj-m := $(O_TARGET)
2480 export-objs += xattr.o
2481 Index: linux-2.4.21-suse2/fs/ext3/super.c
2482 ===================================================================
2483 --- linux-2.4.21-suse2.orig/fs/ext3/super.c 2004-02-05 20:42:40.000000000 +0300
2484 +++ linux-2.4.21-suse2/fs/ext3/super.c 2004-02-05 20:42:40.000000000 +0300
2488 J_ASSERT(sbi->s_delete_inodes == 0);
2489 + ext3_ext_release(sb);
2490 ext3_xattr_put_super(sb);
2491 journal_destroy(sbi->s_journal);
2492 if (!(sb->s_flags & MS_RDONLY)) {
2493 @@ -829,6 +830,10 @@
2497 + else if (!strcmp (this_char, "extents"))
2498 + set_opt (*mount_options, EXTENTS);
2499 + else if (!strcmp (this_char, "extdebug"))
2500 + set_opt (*mount_options, EXTDEBUG);
2501 else if (!strcmp (this_char, "grpid") ||
2502 !strcmp (this_char, "bsdgroups"))
2503 set_opt (*mount_options, GRPID);
2504 @@ -1523,6 +1528,8 @@
2505 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2508 + ext3_ext_init(sb);
2513 Index: linux-2.4.21-suse2/fs/ext3/ioctl.c
2514 ===================================================================
2515 --- linux-2.4.21-suse2.orig/fs/ext3/ioctl.c 2004-02-05 20:42:39.000000000 +0300
2516 +++ linux-2.4.21-suse2/fs/ext3/ioctl.c 2004-02-05 20:42:40.000000000 +0300
2517 @@ -174,6 +174,10 @@
2521 + case EXT3_IOC_GET_EXTENTS:
2522 + case EXT3_IOC_GET_TREE_STATS:
2523 + case EXT3_IOC_GET_TREE_DEPTH:
2524 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2528 Index: linux-2.4.21-suse2/include/linux/ext3_fs.h
2529 ===================================================================
2530 --- linux-2.4.21-suse2.orig/include/linux/ext3_fs.h 2004-02-05 20:42:40.000000000 +0300
2531 +++ linux-2.4.21-suse2/include/linux/ext3_fs.h 2004-02-05 20:42:40.000000000 +0300
2533 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2534 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2535 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2536 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2538 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2539 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2541 #ifdef CONFIG_JBD_DEBUG
2542 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2544 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 5, long)
2545 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 6, long)
2546 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 7, long)
2549 * Structure of an inode on the disk
2551 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
2552 #define EXT3_MOUNT_IOPEN 0x40000 /* Allow access via iopen */
2553 #define EXT3_MOUNT_IOPEN_NOPRIV 0x80000 /* Make iopen world-readable */
2554 +#define EXT3_MOUNT_EXTENTS 0x100000/* Extents support */
2555 +#define EXT3_MOUNT_EXTDEBUG 0x200000/* Extents debug */
2557 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2558 #ifndef _LINUX_EXT2_FS_H
2560 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2563 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2564 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2565 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2566 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2567 @@ -770,6 +777,14 @@
2568 extern struct inode_operations ext3_symlink_inode_operations;
2569 extern struct inode_operations ext3_fast_symlink_inode_operations;
2572 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2573 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2574 + struct buffer_head *, int, int);
2575 +extern void ext3_ext_truncate(struct inode *);
2576 +extern void ext3_ext_init(struct super_block *);
2577 +extern void ext3_ext_release(struct super_block *);
2578 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2580 #endif /* __KERNEL__ */
2582 Index: linux-2.4.21-suse2/include/linux/ext3_extents.h
2583 ===================================================================
2584 --- linux-2.4.21-suse2.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2585 +++ linux-2.4.21-suse2/include/linux/ext3_extents.h 2004-02-05 20:42:40.000000000 +0300
2588 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
2590 + * This program is free software; you can redistribute it and/or modify
2591 + * it under the terms of the GNU General Public License version 2 as
2592 + * published by the Free Software Foundation.
2594 + * This program is distributed in the hope that it will be useful,
2595 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2596 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2597 + * GNU General Public License for more details.
2599 + * You should have received a copy of the GNU General Public Licens
2600 + * along with this program; if not, write to the Free Software
2601 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2604 +#ifndef _LINUX_EXT3_EXTENTS
2605 +#define _LINUX_EXT3_EXTENTS
2608 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2609 + * become very little, so index split, in-depth growing and
2610 + * other hard changes happens much more often
2611 + * this is for debug purposes only
2613 +#define AGRESSIVE_TEST_
2616 + * if CHECK_BINSEARCH defined, then results of binary search
2617 + * will be checked by linear search
2619 +#define CHECK_BINSEARCH_
2622 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2623 + * to get lots of info what's going on
2627 +#define ext_debug(tree,fmt,a...) \
2629 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2630 + printk(fmt, ##a); \
2633 +#define ext_debug(tree,fmt,a...)
2637 + * if EXT_STATS is defined then stats numbers are collected
2638 + * these number will be displayed at umount time
2643 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2646 + * ext3_inode has i_block array (total 60 bytes)
2647 + * first 4 bytes are used to store:
2648 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2649 + * - number of alive extents in the inode
2653 + * this is extent on-disk structure
2654 + * it's used at the bottom of the tree
2656 +struct ext3_extent {
2657 + __u32 e_block; /* first logical block extent covers */
2658 + __u32 e_start; /* first physical block extents lives */
2659 + __u32 e_num; /* number of blocks covered by extent */
2663 + * this is index on-disk structure
2664 + * it's used at all the levels, but the bottom
2666 +struct ext3_extent_idx {
2667 + __u32 e_block; /* index covers logical blocks from 'block' */
2668 + __u32 e_leaf; /* pointer to the physical block of the next *
2669 + * level. leaf or next index could bet here */
2673 + * each block (leaves and indexes), even inode-stored has header
2675 +struct ext3_extent_header {
2676 + __u16 e_magic; /* probably will support different formats */
2677 + __u16 e_num; /* number of valid entries */
2678 + __u16 e_max; /* capacity of store in entries */
2679 + __u16 e_depth; /* has tree real underlaying blocks? */
2680 + __u32 e_generation; /* generation of the tree */
2683 +#define EXT3_EXT_MAGIC 0xf301
2686 + * array of ext3_ext_path contains path to some extent
2687 + * creation/lookup routines use it for traversal/splitting/etc
2688 + * truncate uses it to simulate recursive walking
2690 +struct ext3_ext_path {
2693 + struct ext3_extent *p_ext;
2694 + struct ext3_extent_idx *p_idx;
2695 + struct ext3_extent_header *p_hdr;
2696 + struct buffer_head *p_bh;
2700 + * structure for external API
2705 + * ext3_extents_tree is used to pass initial information
2706 + * to top-level extents API
2708 +struct ext3_extents_tree {
2709 + struct inode *inode; /* inode which tree belongs to */
2710 + void *root; /* ptr to data top of tree resides at */
2711 + void *buffer; /* will be passed as arg to ^^ routines */
2714 + struct ext3_extent *cex;/* last found extent */
2715 + int (*get_write_access)(handle_t *h, void *buffer);
2716 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2717 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2718 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2719 + struct ext3_extent *, unsigned long,
2721 + int (*remove_extent)(struct ext3_extents_tree *,
2722 + struct ext3_extent *, unsigned long,
2724 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2725 + struct ext3_ext_path *, struct ext3_extent *,
2730 + * to be called by ext3_ext_walk_space()
2731 + * negative retcode - error
2732 + * positive retcode - signal for ext3_ext_walk_space(), see below
2733 + * callback must return valid extent (passed or newly created)
2735 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2736 + struct ext3_ext_path *,
2737 + struct ext3_extent *, int);
2739 +#define EXT_CONTINUE 0
2740 +#define EXT_BREAK 1
2741 +#define EXT_REPEAT 2
2744 +#define EXT_FIRST_EXTENT(__hdr__) \
2745 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2746 + sizeof(struct ext3_extent_header)))
2747 +#define EXT_FIRST_INDEX(__hdr__) \
2748 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2749 + sizeof(struct ext3_extent_header)))
2750 +#define EXT_HAS_FREE_INDEX(__path__) \
2751 + ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
2752 +#define EXT_LAST_EXTENT(__hdr__) \
2753 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
2754 +#define EXT_LAST_INDEX(__hdr__) \
2755 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
2756 +#define EXT_MAX_EXTENT(__hdr__) \
2757 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
2758 +#define EXT_MAX_INDEX(__hdr__) \
2759 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
2761 +#define EXT_ROOT_HDR(tree) \
2762 + ((struct ext3_extent_header *) (tree)->root)
2763 +#define EXT_BLOCK_HDR(bh) \
2764 + ((struct ext3_extent_header *) (bh)->b_data)
2765 +#define EXT_DEPTH(_t_) \
2766 + (((struct ext3_extent_header *)((_t_)->root))->e_depth)
2767 +#define EXT_GENERATION(_t_) \
2768 + (((struct ext3_extent_header *)((_t_)->root))->e_generation)
2771 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2775 + * this structure is used to gather extents from the tree via ioctl
2777 +struct ext3_extent_buf {
2778 + unsigned long start;
2786 + * this structure is used to collect stats info about the tree
2788 +struct ext3_extent_tree_stats {
2794 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2795 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2796 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2797 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2798 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2799 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2801 +#endif /* _LINUX_EXT3_EXTENTS */
2803 Index: linux-2.4.21-suse2/include/linux/ext3_fs_i.h
2804 ===================================================================
2805 --- linux-2.4.21-suse2.orig/include/linux/ext3_fs_i.h 2004-02-05 20:42:40.000000000 +0300
2806 +++ linux-2.4.21-suse2/include/linux/ext3_fs_i.h 2004-02-05 20:47:04.000000000 +0300
2808 * by other means, so we have truncate_sem.
2810 struct rw_semaphore truncate_sem;
2812 + __u32 i_cached_extent[3];
2815 #endif /* _LINUX_EXT3_FS_I */