1 Index: linux-2.4.21-rhel/fs/ext3/extents.c
2 ===================================================================
3 --- linux-2.4.21-rhel.orig/fs/ext3/extents.c 2005-03-02 22:42:20.659360368 +0300
4 +++ linux-2.4.21-rhel/fs/ext3/extents.c 2005-03-04 02:34:52.000000000 +0300
7 + * Copyright(c) 2003, 2004, 2005, Cluster File Systems, Inc, info@clusterfs.com
8 + * Written by Alex Tomas <alex@clusterfs.com>
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License version 2 as
12 + * published by the Free Software Foundation.
14 + * This program is distributed in the hope that it will be useful,
15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 + * GNU General Public License for more details.
19 + * You should have received a copy of the GNU General Public Licens
20 + * along with this program; if not, write to the Free Software
21 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
25 + * Extents support for EXT3
28 + * - ext3_ext_walk_space() sould not use ext3_ext_find_extent()
29 + * - ext3_ext_calc_credits() could take 'mergable' into account
30 + * - ext3*_error() should be used in some situations
31 + * - find_goal() [to be tested and improved]
32 + * - smart tree reduction
33 + * - arch-independence
34 + * common on-disk format for big/little-endian arch
37 +#include <linux/module.h>
38 +#include <linux/fs.h>
39 +#include <linux/time.h>
40 +#include <linux/ext3_jbd.h>
41 +#include <linux/jbd.h>
42 +#include <linux/locks.h>
43 +#include <linux/smp_lock.h>
44 +#include <linux/highuid.h>
45 +#include <linux/pagemap.h>
46 +#include <linux/quotaops.h>
47 +#include <linux/string.h>
48 +#include <linux/slab.h>
49 +#include <linux/ext3_extents.h>
50 +#include <asm/uaccess.h>
53 +static inline int ext3_ext_check_header(struct ext3_extent_header *eh)
55 + if (eh->eh_magic != EXT3_EXT_MAGIC) {
56 + printk(KERN_ERR "EXT3-fs: invalid magic = 0x%x\n",
57 + (unsigned)eh->eh_magic);
60 + if (eh->eh_max == 0) {
61 + printk(KERN_ERR "EXT3-fs: invalid eh_max = %u\n",
62 + (unsigned)eh->eh_max);
65 + if (eh->eh_entries > eh->eh_max) {
66 + printk(KERN_ERR "EXT3-fs: invalid eh_entries = %u\n",
67 + (unsigned)eh->eh_entries);
73 +static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
77 + if (handle->h_buffer_credits > needed)
79 + if (!ext3_journal_extend(handle, needed))
81 + err = ext3_journal_restart(handle, needed);
87 +ext3_ext_get_access_for_root(handle_t *h, struct ext3_extents_tree *tree)
89 + if (tree->ops->get_write_access)
90 + return tree->ops->get_write_access(h,tree->buffer);
96 +ext3_ext_mark_root_dirty(handle_t *h, struct ext3_extents_tree *tree)
98 + if (tree->ops->mark_buffer_dirty)
99 + return tree->ops->mark_buffer_dirty(h,tree->buffer);
109 +static int ext3_ext_get_access(handle_t *handle,
110 + struct ext3_extents_tree *tree,
111 + struct ext3_ext_path *path)
116 + /* path points to block */
117 + err = ext3_journal_get_write_access(handle, path->p_bh);
119 + /* path points to leaf/index in inode body */
120 + err = ext3_ext_get_access_for_root(handle, tree);
131 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
132 + struct ext3_ext_path *path)
136 + /* path points to block */
137 + err =ext3_journal_dirty_metadata(handle, path->p_bh);
139 + /* path points to leaf/index in inode body */
140 + err = ext3_ext_mark_root_dirty(handle, tree);
146 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
147 + struct ext3_ext_path *path, struct ext3_extent *ex,
150 + int goal, depth, newblock;
151 + struct inode *inode;
154 + if (tree->ops->new_block)
155 + return tree->ops->new_block(handle, tree, path, ex, err);
157 + inode = tree->inode;
158 + depth = EXT_DEPTH(tree);
159 + if (path && depth > 0) {
160 + goal = path[depth-1].p_block;
162 + struct ext3_inode_info *ei = EXT3_I(inode);
163 + unsigned long bg_start;
164 + unsigned long colour;
166 + bg_start = (ei->i_block_group *
167 + EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
168 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
169 + colour = (current->pid % 16) *
170 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
171 + goal = bg_start + colour;
175 + newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
180 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
182 + struct ext3_extent_header *neh = EXT_ROOT_HDR(tree);
183 + neh->eh_generation = ((EXT_FLAGS(neh) & ~EXT_FLAGS_CLR_UNKNOWN) << 24) |
184 + (EXT_HDR_GEN(neh) + 1);
187 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
191 + size = (tree->inode->i_sb->s_blocksize -
192 + sizeof(struct ext3_extent_header)) /
193 + sizeof(struct ext3_extent);
194 +#ifdef AGRESSIVE_TEST
200 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
204 + size = (tree->inode->i_sb->s_blocksize -
205 + sizeof(struct ext3_extent_header)) /
206 + sizeof(struct ext3_extent_idx);
207 +#ifdef AGRESSIVE_TEST
213 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
217 + size = (tree->buffer_len - sizeof(struct ext3_extent_header)) /
218 + sizeof(struct ext3_extent);
219 +#ifdef AGRESSIVE_TEST
225 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
229 + size = (tree->buffer_len - sizeof(struct ext3_extent_header)) /
230 + sizeof(struct ext3_extent_idx);
231 +#ifdef AGRESSIVE_TEST
237 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
238 + struct ext3_ext_path *path)
241 + int k, l = path->p_depth;
243 + ext_debug(tree, "path:");
244 + for (k = 0; k <= l; k++, path++) {
246 + ext_debug(tree, " %d->%d", path->p_idx->ei_block,
247 + path->p_idx->ei_leaf);
248 + } else if (path->p_ext) {
249 + ext_debug(tree, " %d:%d:%d",
250 + path->p_ext->ee_block,
251 + path->p_ext->ee_len,
252 + path->p_ext->ee_start);
254 + ext_debug(tree, " []");
256 + ext_debug(tree, "\n");
260 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
261 + struct ext3_ext_path *path)
264 + int depth = EXT_DEPTH(tree);
265 + struct ext3_extent_header *eh;
266 + struct ext3_extent *ex;
272 + eh = path[depth].p_hdr;
273 + ex = EXT_FIRST_EXTENT(eh);
275 + for (i = 0; i < eh->eh_entries; i++, ex++) {
276 + ext_debug(tree, "%d:%d:%d ",
277 + ex->ee_block, ex->ee_len, ex->ee_start);
279 + ext_debug(tree, "\n");
283 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
285 + int depth = path->p_depth;
288 + for (i = 0; i <= depth; i++, path++) {
290 + brelse(path->p_bh);
297 + * binary search for closest index by given block
300 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
301 + struct ext3_ext_path *path, int block)
303 + struct ext3_extent_header *eh = path->p_hdr;
304 + struct ext3_extent_idx *ix;
307 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
308 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
309 + EXT_ASSERT(eh->eh_entries > 0);
311 + ext_debug(tree, "binsearch for %d(idx): ", block);
313 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
315 + r = k = eh->eh_entries;
318 + if (block < ix[l + k].ei_block)
322 + ext_debug(tree, "%d:%d:%d ", k, l, r);
327 + ext_debug(tree," -> %d->%d ",path->p_idx->ei_block,path->p_idx->ei_leaf);
330 + if (block < ix->ei_block)
332 + path->p_idx = ix++;
334 + ext_debug(tree, " -> %d->%d\n", path->p_idx->ei_block,
335 + path->p_idx->ei_leaf);
337 +#ifdef CHECK_BINSEARCH
339 + struct ext3_extent_idx *chix;
341 + chix = ix = EXT_FIRST_INDEX(eh);
342 + for (k = 0; k < eh->eh_entries; k++, ix++) {
343 + if (k != 0 && ix->ei_block <= ix[-1].ei_block) {
344 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
345 + ix, EXT_FIRST_INDEX(eh));
346 + printk("%u <= %u\n",
347 + ix->ei_block,ix[-1].ei_block);
349 + EXT_ASSERT(k == 0 || ix->ei_block > ix[-1].ei_block);
350 + if (block < ix->ei_block)
354 + EXT_ASSERT(chix == path->p_idx);
360 + * binary search for closest extent by given block
363 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
364 + struct ext3_ext_path *path, int block)
366 + struct ext3_extent_header *eh = path->p_hdr;
367 + struct ext3_extent *ex;
370 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
371 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
373 + if (eh->eh_entries == 0) {
375 + * this leaf is empty yet:
376 + * we get such a leaf in split/add case
381 + ext_debug(tree, "binsearch for %d: ", block);
383 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
385 + r = k = eh->eh_entries;
388 + if (block < ex[l + k].ee_block)
392 + ext_debug(tree, "%d:%d:%d ", k, l, r);
397 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->ee_block,
398 + path->p_ext->ee_start, path->p_ext->ee_len);
401 + if (block < ex->ee_block)
403 + path->p_ext = ex++;
405 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->ee_block,
406 + path->p_ext->ee_start, path->p_ext->ee_len);
408 +#ifdef CHECK_BINSEARCH
410 + struct ext3_extent *chex;
412 + chex = ex = EXT_FIRST_EXTENT(eh);
413 + for (k = 0; k < eh->eh_entries; k++, ex++) {
414 + EXT_ASSERT(k == 0 || ex->ee_block > ex[-1].ee_block);
415 + if (block < ex->ee_block)
419 + EXT_ASSERT(chex == path->p_ext);
424 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
426 + struct ext3_extent_header *eh;
428 + BUG_ON(tree->buffer_len == 0);
429 + ext3_ext_get_access_for_root(handle, tree);
430 + eh = EXT_ROOT_HDR(tree);
432 + eh->eh_entries = 0;
433 + eh->eh_magic = EXT3_EXT_MAGIC;
434 + eh->eh_max = ext3_ext_space_root(tree);
435 + ext3_ext_mark_root_dirty(handle, tree);
436 + ext3_ext_invalidate_cache(tree);
440 +struct ext3_ext_path *
441 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
442 + struct ext3_ext_path *path)
444 + struct ext3_extent_header *eh;
445 + struct buffer_head *bh;
446 + int depth, i, ppos = 0;
449 + EXT_ASSERT(tree->inode);
450 + EXT_ASSERT(tree->root);
452 + eh = EXT_ROOT_HDR(tree);
454 + if (ext3_ext_check_header(eh))
457 + i = depth = EXT_DEPTH(tree);
458 + EXT_ASSERT(eh->eh_max);
459 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
461 + /* account possible depth increase */
463 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
466 + return ERR_PTR(-ENOMEM);
468 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
469 + path[0].p_hdr = eh;
471 + /* walk through the tree */
473 + ext_debug(tree, "depth %d: num %d, max %d\n",
474 + ppos, eh->eh_entries, eh->eh_max);
475 + ext3_ext_binsearch_idx(tree, path + ppos, block);
476 + path[ppos].p_block = path[ppos].p_idx->ei_leaf;
477 + path[ppos].p_depth = i;
478 + path[ppos].p_ext = NULL;
480 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
484 + eh = EXT_BLOCK_HDR(bh);
486 + EXT_ASSERT(ppos <= depth);
487 + path[ppos].p_bh = bh;
488 + path[ppos].p_hdr = eh;
491 + if (ext3_ext_check_header(eh))
495 + path[ppos].p_depth = i;
496 + path[ppos].p_hdr = eh;
497 + path[ppos].p_ext = NULL;
498 + path[ppos].p_idx = NULL;
500 + if (ext3_ext_check_header(eh))
504 + ext3_ext_binsearch(tree, path + ppos, block);
506 + ext3_ext_show_path(tree, path);
511 + printk(KERN_ERR "EXT3-fs: header is corrupted!\n");
512 + ext3_ext_drop_refs(path);
514 + return ERR_PTR(-EIO);
518 + * insert new index [logical;ptr] into the block at cupr
519 + * it check where to insert: before curp or after curp
521 +static int ext3_ext_insert_index(handle_t *handle,
522 + struct ext3_extents_tree *tree,
523 + struct ext3_ext_path *curp,
524 + int logical, int ptr)
526 + struct ext3_extent_idx *ix;
529 + if ((err = ext3_ext_get_access(handle, tree, curp)))
532 + EXT_ASSERT(logical != curp->p_idx->ei_block);
533 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
534 + if (logical > curp->p_idx->ei_block) {
536 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
537 + len = (len - 1) * sizeof(struct ext3_extent_idx);
538 + len = len < 0 ? 0 : len;
539 + ext_debug(tree, "insert new index %d after: %d. "
540 + "move %d from 0x%p to 0x%p\n",
542 + (curp->p_idx + 1), (curp->p_idx + 2));
543 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
545 + ix = curp->p_idx + 1;
547 + /* insert before */
548 + len = len * sizeof(struct ext3_extent_idx);
549 + len = len < 0 ? 0 : len;
550 + ext_debug(tree, "insert new index %d before: %d. "
551 + "move %d from 0x%p to 0x%p\n",
553 + curp->p_idx, (curp->p_idx + 1));
554 + memmove(curp->p_idx + 1, curp->p_idx, len);
558 + ix->ei_block = logical;
560 + ix->ei_leaf_hi = ix->ei_unused = 0;
561 + curp->p_hdr->eh_entries++;
563 + EXT_ASSERT(curp->p_hdr->eh_entries <= curp->p_hdr->eh_max);
564 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
566 + err = ext3_ext_dirty(handle, tree, curp);
567 + ext3_std_error(tree->inode->i_sb, err);
573 + * routine inserts new subtree into the path, using free index entry
575 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
576 + * - makes decision where to split
577 + * - moves remaining extens and index entries (right to the split point)
578 + * into the newly allocated blocks
579 + * - initialize subtree
581 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
582 + struct ext3_ext_path *path,
583 + struct ext3_extent *newext, int at)
585 + struct buffer_head *bh = NULL;
586 + int depth = EXT_DEPTH(tree);
587 + struct ext3_extent_header *neh;
588 + struct ext3_extent_idx *fidx;
589 + struct ext3_extent *ex;
590 + int i = at, k, m, a;
591 + unsigned long newblock, oldblock, border;
592 + int *ablocks = NULL; /* array of allocated blocks */
595 + /* make decision: where to split? */
596 + /* FIXME: now desicion is simplest: at current extent */
598 + /* if current leaf will be splitted, then we should use
599 + * border from split point */
600 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
601 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
602 + border = path[depth].p_ext[1].ee_block;
603 + ext_debug(tree, "leaf will be splitted."
604 + " next leaf starts at %d\n",
607 + border = newext->ee_block;
608 + ext_debug(tree, "leaf will be added."
609 + " next leaf starts at %d\n",
614 + * if error occurs, then we break processing
615 + * and turn filesystem read-only. so, index won't
616 + * be inserted and tree will be in consistent
617 + * state. next mount will repair buffers too
621 + * get array to track all allocated blocks
622 + * we need this to handle errors and free blocks
625 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
628 + memset(ablocks, 0, sizeof(unsigned long) * depth);
630 + /* allocate all needed blocks */
631 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
632 + for (a = 0; a < depth - at; a++) {
633 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
636 + ablocks[a] = newblock;
639 + /* initialize new leaf */
640 + newblock = ablocks[--a];
641 + EXT_ASSERT(newblock);
642 + bh = sb_getblk(tree->inode->i_sb, newblock);
649 + if ((err = ext3_journal_get_create_access(handle, bh)))
652 + neh = EXT_BLOCK_HDR(bh);
653 + neh->eh_entries = 0;
654 + neh->eh_max = ext3_ext_space_block(tree);
655 + neh->eh_magic = EXT3_EXT_MAGIC;
657 + ex = EXT_FIRST_EXTENT(neh);
659 + /* move remain of path[depth] to the new leaf */
660 + EXT_ASSERT(path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max);
661 + /* start copy from next extent */
662 + /* TODO: we could do it by single memmove */
664 + path[depth].p_ext++;
665 + while (path[depth].p_ext <=
666 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
667 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
668 + path[depth].p_ext->ee_block,
669 + path[depth].p_ext->ee_start,
670 + path[depth].p_ext->ee_len,
672 + memmove(ex++, path[depth].p_ext++, sizeof(struct ext3_extent));
676 + mark_buffer_uptodate(bh, 1);
679 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
684 + /* correct old leaf */
686 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
688 + path[depth].p_hdr->eh_entries -= m;
689 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
694 + /* create intermediate indexes */
695 + k = depth - at - 1;
696 + EXT_ASSERT(k >= 0);
698 + ext_debug(tree, "create %d intermediate indices\n", k);
699 + /* insert new index into current index block */
700 + /* current depth stored in i var */
703 + oldblock = newblock;
704 + newblock = ablocks[--a];
705 + bh = sb_getblk(tree->inode->i_sb, newblock);
712 + if ((err = ext3_journal_get_create_access(handle, bh)))
715 + neh = EXT_BLOCK_HDR(bh);
716 + neh->eh_entries = 1;
717 + neh->eh_magic = EXT3_EXT_MAGIC;
718 + neh->eh_max = ext3_ext_space_block_idx(tree);
719 + neh->eh_depth = depth - i;
720 + fidx = EXT_FIRST_INDEX(neh);
721 + fidx->ei_block = border;
722 + fidx->ei_leaf = oldblock;
723 + fidx->ei_leaf_hi = fidx->ei_unused = 0;
725 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
726 + i, newblock, border, oldblock);
731 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
732 + EXT_MAX_INDEX(path[i].p_hdr));
733 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
734 + EXT_LAST_INDEX(path[i].p_hdr));
735 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
736 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
737 + i, path[i].p_idx->ei_block,
738 + path[i].p_idx->ei_leaf, newblock);
739 + memmove(++fidx, path[i].p_idx++,
740 + sizeof(struct ext3_extent_idx));
742 + EXT_ASSERT(neh->eh_entries <= neh->eh_max);
745 + mark_buffer_uptodate(bh, 1);
748 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
753 + /* correct old index */
755 + err = ext3_ext_get_access(handle, tree, path + i);
758 + path[i].p_hdr->eh_entries -= m;
759 + err = ext3_ext_dirty(handle, tree, path + i);
767 + /* insert new index */
769 + err = ext3_ext_insert_index(handle, tree, path + at,
774 + if (buffer_locked(bh))
780 + /* free all allocated blocks in error case */
781 + for (i = 0; i < depth; i++) {
784 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
793 + * routine implements tree growing procedure:
794 + * - allocates new block
795 + * - moves top-level data (index block or leaf) into the new block
796 + * - initialize new top-level, creating index that points to the
797 + * just created block
799 +static int ext3_ext_grow_indepth(handle_t *handle,
800 + struct ext3_extents_tree *tree,
801 + struct ext3_ext_path *path,
802 + struct ext3_extent *newext)
804 + struct ext3_ext_path *curp = path;
805 + struct ext3_extent_header *neh;
806 + struct ext3_extent_idx *fidx;
807 + struct buffer_head *bh;
808 + unsigned long newblock;
811 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
815 + bh = sb_getblk(tree->inode->i_sb, newblock);
818 + ext3_std_error(tree->inode->i_sb, err);
823 + if ((err = ext3_journal_get_create_access(handle, bh))) {
828 + /* move top-level index/leaf into new block */
829 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
831 + /* set size of new block */
832 + neh = EXT_BLOCK_HDR(bh);
833 + /* old root could have indexes or leaves
834 + * so calculate eh_max right way */
835 + if (EXT_DEPTH(tree))
836 + neh->eh_max = ext3_ext_space_block_idx(tree);
838 + neh->eh_max = ext3_ext_space_block(tree);
839 + neh->eh_magic = EXT3_EXT_MAGIC;
840 + mark_buffer_uptodate(bh, 1);
843 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
846 + /* create index in new top-level index: num,max,pointer */
847 + if ((err = ext3_ext_get_access(handle, tree, curp)))
850 + curp->p_hdr->eh_magic = EXT3_EXT_MAGIC;
851 + curp->p_hdr->eh_max = ext3_ext_space_root_idx(tree);
852 + curp->p_hdr->eh_entries = 1;
853 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
854 + /* FIXME: it works, but actually path[0] can be index */
855 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
856 + curp->p_idx->ei_leaf = newblock;
857 + curp->p_idx->ei_leaf_hi = curp->p_idx->ei_unused = 0;
859 + neh = EXT_ROOT_HDR(tree);
860 + fidx = EXT_FIRST_INDEX(neh);
861 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
862 + neh->eh_entries, neh->eh_max, fidx->ei_block, fidx->ei_leaf);
864 + neh->eh_depth = path->p_depth + 1;
865 + err = ext3_ext_dirty(handle, tree, curp);
873 + * routine finds empty index and adds new leaf. if no free index found
874 + * then it requests in-depth growing
876 +static int ext3_ext_create_new_leaf(handle_t *handle,
877 + struct ext3_extents_tree *tree,
878 + struct ext3_ext_path *path,
879 + struct ext3_extent *newext)
881 + struct ext3_ext_path *curp;
882 + int depth, i, err = 0;
885 + i = depth = EXT_DEPTH(tree);
887 + /* walk up to the tree and look for free index entry */
888 + curp = path + depth;
889 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
894 + /* we use already allocated block for index block
895 + * so, subsequent data blocks should be contigoues */
896 + if (EXT_HAS_FREE_INDEX(curp)) {
897 + /* if we found index with free entry, then use that
898 + * entry: create all needed subtree and add new leaf */
899 + err = ext3_ext_split(handle, tree, path, newext, i);
902 + ext3_ext_drop_refs(path);
903 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
905 + err = PTR_ERR(path);
907 + /* tree is full, time to grow in depth */
908 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
911 + ext3_ext_drop_refs(path);
912 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
914 + err = PTR_ERR(path);
917 + * only first (depth 0 -> 1) produces free space
918 + * in all other cases we have to split growed tree
920 + depth = EXT_DEPTH(tree);
921 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
922 + /* now we need split */
934 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
935 + * NOTE: it consider block number from index entry as
936 + * allocated block. thus, index entries have to be consistent
939 +static unsigned long
940 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
944 + EXT_ASSERT(path != NULL);
945 + depth = path->p_depth;
947 + if (depth == 0 && path->p_ext == NULL)
948 + return EXT_MAX_BLOCK;
950 + /* FIXME: what if index isn't full ?! */
951 + while (depth >= 0) {
952 + if (depth == path->p_depth) {
954 + if (path[depth].p_ext !=
955 + EXT_LAST_EXTENT(path[depth].p_hdr))
956 + return path[depth].p_ext[1].ee_block;
959 + if (path[depth].p_idx !=
960 + EXT_LAST_INDEX(path[depth].p_hdr))
961 + return path[depth].p_idx[1].ei_block;
966 + return EXT_MAX_BLOCK;
970 + * returns first allocated block from next leaf or EXT_MAX_BLOCK
972 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
973 + struct ext3_ext_path *path)
977 + EXT_ASSERT(path != NULL);
978 + depth = path->p_depth;
980 + /* zero-tree has no leaf blocks at all */
982 + return EXT_MAX_BLOCK;
984 + /* go to index block */
987 + while (depth >= 0) {
988 + if (path[depth].p_idx !=
989 + EXT_LAST_INDEX(path[depth].p_hdr))
990 + return path[depth].p_idx[1].ei_block;
994 + return EXT_MAX_BLOCK;
998 + * if leaf gets modified and modified extent is first in the leaf
999 + * then we have to correct all indexes above
1000 + * TODO: do we need to correct tree in all cases?
1002 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
1003 + struct ext3_ext_path *path)
1005 + struct ext3_extent_header *eh;
1006 + int depth = EXT_DEPTH(tree);
1007 + struct ext3_extent *ex;
1008 + unsigned long border;
1011 + eh = path[depth].p_hdr;
1012 + ex = path[depth].p_ext;
1017 + /* there is no tree at all */
1021 + if (ex != EXT_FIRST_EXTENT(eh)) {
1022 + /* we correct tree if first leaf got modified only */
1027 + * TODO: we need correction if border is smaller then current one
1030 + border = path[depth].p_ext->ee_block;
1031 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1033 + path[k].p_idx->ei_block = border;
1034 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1038 + /* change all left-side indexes */
1039 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1041 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1043 + path[k].p_idx->ei_block = border;
1044 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1052 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1053 + struct ext3_extent *ex1,
1054 + struct ext3_extent *ex2)
1056 + if (ex1->ee_block + ex1->ee_len != ex2->ee_block)
1059 +#ifdef AGRESSIVE_TEST
1060 + if (ex1->ee_len >= 4)
1064 + if (!tree->ops->mergable)
1067 + return tree->ops->mergable(ex1, ex2);
1071 + * this routine tries to merge requsted extent into the existing
1072 + * extent or inserts requested extent as new one into the tree,
1073 + * creating new leaf in no-space case
1075 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1076 + struct ext3_ext_path *path,
1077 + struct ext3_extent *newext)
1079 + struct ext3_extent_header * eh;
1080 + struct ext3_extent *ex, *fex;
1081 + struct ext3_extent *nearex; /* nearest extent */
1082 + struct ext3_ext_path *npath = NULL;
1083 + int depth, len, err, next;
1085 + EXT_ASSERT(newext->ee_len > 0);
1086 + depth = EXT_DEPTH(tree);
1087 + ex = path[depth].p_ext;
1088 + EXT_ASSERT(path[depth].p_hdr);
1090 + /* try to insert block into found extent and return */
1091 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1092 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1093 + newext->ee_len, ex->ee_block, ex->ee_len,
1095 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1097 + ex->ee_len += newext->ee_len;
1098 + eh = path[depth].p_hdr;
1104 + depth = EXT_DEPTH(tree);
1105 + eh = path[depth].p_hdr;
1106 + if (eh->eh_entries < eh->eh_max)
1109 + /* probably next leaf has space for us? */
1110 + fex = EXT_LAST_EXTENT(eh);
1111 + next = ext3_ext_next_leaf_block(tree, path);
1112 + if (newext->ee_block > fex->ee_block && next != EXT_MAX_BLOCK) {
1113 + ext_debug(tree, "next leaf block - %d\n", next);
1114 + EXT_ASSERT(!npath);
1115 + npath = ext3_ext_find_extent(tree, next, NULL);
1116 + if (IS_ERR(npath))
1117 + return PTR_ERR(npath);
1118 + EXT_ASSERT(npath->p_depth == path->p_depth);
1119 + eh = npath[depth].p_hdr;
1120 + if (eh->eh_entries < eh->eh_max) {
1121 + ext_debug(tree, "next leaf isnt full(%d)\n",
1126 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1127 + eh->eh_entries, eh->eh_max);
1131 + * there is no free space in found leaf
1132 + * we're gonna add new leaf in the tree
1134 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1137 + depth = EXT_DEPTH(tree);
1138 + eh = path[depth].p_hdr;
1141 + nearex = path[depth].p_ext;
1143 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1147 + /* there is no extent in this leaf, create first one */
1148 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1149 + newext->ee_block, newext->ee_start,
1151 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1152 + } else if (newext->ee_block > nearex->ee_block) {
1153 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1154 + if (nearex != EXT_LAST_EXTENT(eh)) {
1155 + len = EXT_MAX_EXTENT(eh) - nearex;
1156 + len = (len - 1) * sizeof(struct ext3_extent);
1157 + len = len < 0 ? 0 : len;
1158 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1159 + "move %d from 0x%p to 0x%p\n",
1160 + newext->ee_block, newext->ee_start,
1162 + nearex, len, nearex + 1, nearex + 2);
1163 + memmove(nearex + 2, nearex + 1, len);
1165 + path[depth].p_ext = nearex + 1;
1167 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1168 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1169 + len = len < 0 ? 0 : len;
1170 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1171 + "move %d from 0x%p to 0x%p\n",
1172 + newext->ee_block, newext->ee_start, newext->ee_len,
1173 + nearex, len, nearex + 1, nearex + 2);
1174 + memmove(nearex + 1, nearex, len);
1175 + path[depth].p_ext = nearex;
1179 + nearex = path[depth].p_ext;
1180 + nearex->ee_block = newext->ee_block;
1181 + nearex->ee_start = newext->ee_start;
1182 + nearex->ee_len = newext->ee_len;
1183 + /* FIXME: support for large fs */
1184 + nearex->ee_start_hi = 0;
1187 + /* try to merge extents to the right */
1188 + while (nearex < EXT_LAST_EXTENT(eh)) {
1189 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1191 + /* merge with next extent! */
1192 + nearex->ee_len += nearex[1].ee_len;
1193 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1194 + len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1195 + sizeof(struct ext3_extent);
1196 + memmove(nearex + 1, nearex + 2, len);
1199 + EXT_ASSERT(eh->eh_entries > 0);
1202 + /* try to merge extents to the left */
1204 + /* time to correct all indexes above */
1205 + err = ext3_ext_correct_indexes(handle, tree, path);
1209 + err = ext3_ext_dirty(handle, tree, path + depth);
1213 + ext3_ext_drop_refs(npath);
1216 + ext3_ext_tree_changed(tree);
1217 + ext3_ext_invalidate_cache(tree);
1221 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1222 + unsigned long num, ext_prepare_callback func)
1224 + struct ext3_ext_path *path = NULL;
1225 + struct ext3_ext_cache cbex;
1226 + struct ext3_extent *ex;
1227 + unsigned long next, start = 0, end = 0;
1228 + unsigned long last = block + num;
1229 + int depth, exists, err = 0;
1233 + EXT_ASSERT(tree->inode);
1234 + EXT_ASSERT(tree->root);
1236 + while (block < last && block != EXT_MAX_BLOCK) {
1237 + num = last - block;
1238 + /* find extent for this block */
1239 + path = ext3_ext_find_extent(tree, block, path);
1240 + if (IS_ERR(path)) {
1241 + err = PTR_ERR(path);
1246 + depth = EXT_DEPTH(tree);
1247 + EXT_ASSERT(path[depth].p_hdr);
1248 + ex = path[depth].p_ext;
1249 + next = ext3_ext_next_allocated_block(path);
1253 + /* there is no extent yet, so try to allocate
1254 + * all requested space */
1256 + end = block + num;
1257 + } else if (ex->ee_block > block) {
1258 + /* need to allocate space before found extent */
1260 + end = ex->ee_block;
1261 + if (block + num < end)
1262 + end = block + num;
1263 + } else if (block >= ex->ee_block + ex->ee_len) {
1264 + /* need to allocate space after found extent */
1266 + end = block + num;
1269 + } else if (block >= ex->ee_block) {
1271 + * some part of requested space is covered
1275 + end = ex->ee_block + ex->ee_len;
1276 + if (block + num < end)
1277 + end = block + num;
1282 + EXT_ASSERT(end > start);
1285 + cbex.ec_block = start;
1286 + cbex.ec_len = end - start;
1287 + cbex.ec_start = 0;
1288 + cbex.ec_type = EXT3_EXT_CACHE_GAP;
1290 + cbex.ec_block = ex->ee_block;
1291 + cbex.ec_len = ex->ee_len;
1292 + cbex.ec_start = ex->ee_start;
1293 + cbex.ec_type = EXT3_EXT_CACHE_EXTENT;
1296 + EXT_ASSERT(cbex.ec_len > 0);
1297 + EXT_ASSERT(path[depth].p_hdr);
1298 + err = func(tree, path, &cbex);
1299 + ext3_ext_drop_refs(path);
1303 + if (err == EXT_REPEAT)
1305 + else if (err == EXT_BREAK) {
1310 + if (EXT_DEPTH(tree) != depth) {
1311 + /* depth was changed. we have to realloc path */
1316 + block = cbex.ec_block + cbex.ec_len;
1320 + ext3_ext_drop_refs(path);
1328 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, __u32 block,
1329 + __u32 len, __u32 start, int type)
1331 + EXT_ASSERT(len > 0);
1333 + tree->cex->ec_type = type;
1334 + tree->cex->ec_block = block;
1335 + tree->cex->ec_len = len;
1336 + tree->cex->ec_start = start;
1341 + * this routine calculate boundaries of the gap requested block fits into
1342 + * and cache this gap
1345 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1346 + struct ext3_ext_path *path,
1347 + unsigned long block)
1349 + int depth = EXT_DEPTH(tree);
1350 + unsigned long lblock, len;
1351 + struct ext3_extent *ex;
1356 + ex = path[depth].p_ext;
1358 + /* there is no extent yet, so gap is [0;-] */
1360 + len = EXT_MAX_BLOCK;
1361 + ext_debug(tree, "cache gap(whole file):");
1362 + } else if (block < ex->ee_block) {
1364 + len = ex->ee_block - block;
1365 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1366 + (unsigned long) block,
1367 + (unsigned long) ex->ee_block,
1368 + (unsigned long) ex->ee_len);
1369 + } else if (block >= ex->ee_block + ex->ee_len) {
1370 + lblock = ex->ee_block + ex->ee_len;
1371 + len = ext3_ext_next_allocated_block(path);
1372 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1373 + (unsigned long) ex->ee_block,
1374 + (unsigned long) ex->ee_len,
1375 + (unsigned long) block);
1376 + EXT_ASSERT(len > lblock);
1377 + len = len - lblock;
1383 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) lblock, len);
1384 + ext3_ext_put_in_cache(tree, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1388 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1389 + struct ext3_extent *ex)
1391 + struct ext3_ext_cache *cex = tree->cex;
1393 + /* is there cache storage at all? */
1395 + return EXT3_EXT_CACHE_NO;
1397 + /* has cache valid data? */
1398 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1399 + return EXT3_EXT_CACHE_NO;
1401 + EXT_ASSERT(cex->ec_type == EXT3_EXT_CACHE_GAP ||
1402 + cex->ec_type == EXT3_EXT_CACHE_EXTENT);
1403 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1404 + ex->ee_block = cex->ec_block;
1405 + ex->ee_start = cex->ec_start;
1406 + ex->ee_start_hi = 0;
1407 + ex->ee_len = cex->ec_len;
1408 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1409 + (unsigned long) block,
1410 + (unsigned long) ex->ee_block,
1411 + (unsigned long) ex->ee_len,
1412 + (unsigned long) ex->ee_start);
1413 + return cex->ec_type;
1416 + /* not in cache */
1417 + return EXT3_EXT_CACHE_NO;
1421 + * routine removes index from the index block
1422 + * it's used in truncate case only. thus all requests are for
1423 + * last index in the block only
1425 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1426 + struct ext3_ext_path *path)
1428 + struct buffer_head *bh;
1431 + /* free index block */
1433 + EXT_ASSERT(path->p_hdr->eh_entries);
1434 + if ((err = ext3_ext_get_access(handle, tree, path)))
1436 + path->p_hdr->eh_entries--;
1437 + if ((err = ext3_ext_dirty(handle, tree, path)))
1439 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1440 + path->p_idx->ei_leaf);
1441 + bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->ei_leaf);
1442 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1443 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1447 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1448 + struct ext3_ext_path *path)
1450 + int depth = EXT_DEPTH(tree);
1454 + /* probably there is space in leaf? */
1455 + if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1460 + * the worste case we're expecting is creation of the
1461 + * new root (growing in depth) with index splitting
1462 + * for splitting we have to consider depth + 1 because
1463 + * previous growing could increase it
1465 + depth = depth + 1;
1468 + * growing in depth:
1469 + * block allocation + new root + old root
1471 + needed = EXT3_ALLOC_NEEDED + 2;
1473 + /* index split. we may need:
1474 + * allocate intermediate indexes and new leaf
1475 + * change two blocks at each level, but root
1476 + * modify root block (inode)
1478 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1484 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1485 + struct ext3_ext_path *path, unsigned long start,
1486 + unsigned long end)
1488 + struct ext3_extent *ex, tex;
1489 + struct ext3_ext_path *npath;
1490 + int depth, creds, err;
1492 + depth = EXT_DEPTH(tree);
1493 + ex = path[depth].p_ext;
1495 + EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1496 + EXT_ASSERT(ex->ee_block < start);
1498 + /* calculate tail extent */
1499 + tex.ee_block = end + 1;
1500 + EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1501 + tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1503 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1504 + handle = ext3_ext_journal_restart(handle, creds);
1505 + if (IS_ERR(handle))
1506 + return PTR_ERR(handle);
1508 + /* calculate head extent. use primary extent */
1509 + err = ext3_ext_get_access(handle, tree, path + depth);
1512 + ex->ee_len = start - ex->ee_block;
1513 + err = ext3_ext_dirty(handle, tree, path + depth);
1517 + /* FIXME: some callback to free underlying resource
1518 + * and correct ee_start? */
1519 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1520 + ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1522 + npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1523 + if (IS_ERR(npath))
1524 + return PTR_ERR(npath);
1525 + depth = EXT_DEPTH(tree);
1526 + EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1527 + EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1529 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1530 + ext3_ext_drop_refs(npath);
1537 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1538 + struct ext3_ext_path *path, unsigned long start,
1539 + unsigned long end)
1541 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1542 + int err = 0, correct_index = 0;
1543 + int depth = EXT_DEPTH(tree), credits;
1544 + struct ext3_extent_header *eh;
1545 + unsigned a, b, block, num;
1547 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1548 + if (!path[depth].p_hdr)
1549 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1550 + eh = path[depth].p_hdr;
1552 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1553 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1555 + /* find where to start removing */
1556 + le = ex = EXT_LAST_EXTENT(eh);
1557 + while (ex != EXT_FIRST_EXTENT(eh)) {
1558 + if (ex->ee_block <= end)
1563 + if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1564 + /* removal of internal part of the extent requested
1565 + * tail and head must be placed in different extent
1566 + * so, we have to insert one more extent */
1567 + path[depth].p_ext = ex;
1568 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1572 + while (ex >= EXT_FIRST_EXTENT(eh) && ex->ee_block + ex->ee_len > start) {
1573 + ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1574 + path[depth].p_ext = ex;
1576 + a = ex->ee_block > start ? ex->ee_block : start;
1577 + b = ex->ee_block + ex->ee_len - 1 < end ?
1578 + ex->ee_block + ex->ee_len - 1 : end;
1580 + ext_debug(tree, " border %u:%u\n", a, b);
1582 + if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1586 + } else if (a != ex->ee_block) {
1587 + /* remove tail of the extent */
1588 + block = ex->ee_block;
1590 + } else if (b != ex->ee_block + ex->ee_len - 1) {
1591 + /* remove head of the extent */
1595 + /* remove whole extent: excelent! */
1596 + block = ex->ee_block;
1598 + EXT_ASSERT(a == ex->ee_block &&
1599 + b == ex->ee_block + ex->ee_len - 1);
1602 + if (ex == EXT_FIRST_EXTENT(eh))
1603 + correct_index = 1;
1606 + if (correct_index)
1607 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1608 + if (tree->ops->remove_extent_credits)
1609 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1611 + handle = ext3_ext_journal_restart(handle, credits);
1612 + if (IS_ERR(handle)) {
1613 + err = PTR_ERR(handle);
1617 + err = ext3_ext_get_access(handle, tree, path + depth);
1621 + if (tree->ops->remove_extent)
1622 + err = tree->ops->remove_extent(tree, ex, a, b);
1627 + /* this extent is removed entirely mark slot unused */
1628 + ex->ee_start = ex->ee_start_hi = 0;
1633 + ex->ee_block = block;
1636 + err = ext3_ext_dirty(handle, tree, path + depth);
1640 + ext_debug(tree, "new extent: %u:%u:%u\n",
1641 + ex->ee_block, ex->ee_len, ex->ee_start);
1646 + /* reuse unused slots */
1648 + if (lu->ee_start) {
1650 + lu->ee_start = lu->ee_start_hi = 0;
1657 + if (correct_index && eh->eh_entries)
1658 + err = ext3_ext_correct_indexes(handle, tree, path);
1660 + /* if this leaf is free, then we should
1661 + * remove it from index block above */
1662 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1663 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1670 +static struct ext3_extent_idx *
1671 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1673 + struct ext3_extent_idx *ix;
1675 + ix = EXT_LAST_INDEX(hdr);
1676 + while (ix != EXT_FIRST_INDEX(hdr)) {
1677 + if (ix->ei_block <= block)
1685 + * returns 1 if current index have to be freed (even partial)
1688 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1690 + EXT_ASSERT(path->p_idx);
1692 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1696 + * if truncate on deeper level happened it it wasn't partial
1697 + * so we have to consider current index for truncation
1699 + if (path->p_hdr->eh_entries == path->p_block)
1704 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1705 + unsigned long start, unsigned long end)
1707 + struct inode *inode = tree->inode;
1708 + struct super_block *sb = inode->i_sb;
1709 + int depth = EXT_DEPTH(tree);
1710 + struct ext3_ext_path *path;
1712 + int i = 0, err = 0;
1714 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1716 + /* probably first extent we're gonna free will be last in block */
1717 + handle = ext3_journal_start(inode, depth + 1);
1718 + if (IS_ERR(handle))
1719 + return PTR_ERR(handle);
1721 + ext3_ext_invalidate_cache(tree);
1724 + * we start scanning from right side freeing all the blocks
1725 + * after i_size and walking into the deep
1727 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1728 + if (IS_ERR(path)) {
1729 + ext3_error(sb, __FUNCTION__, "Can't allocate path array");
1730 + ext3_journal_stop(handle, inode);
1733 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1734 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1736 + while (i >= 0 && err == 0) {
1738 + /* this is leaf block */
1739 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1740 + /* root level have p_bh == NULL, brelse() eats this */
1741 + brelse(path[i].p_bh);
1746 + /* this is index block */
1747 + if (!path[i].p_hdr) {
1748 + ext_debug(tree, "initialize header\n");
1749 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1752 + EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1753 + EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1755 + if (!path[i].p_idx) {
1756 + /* this level hasn't touched yet */
1758 + ext3_ext_last_covered(path[i].p_hdr, end);
1759 + path[i].p_block = path[i].p_hdr->eh_entries + 1;
1760 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1761 + path[i].p_hdr, path[i].p_hdr->eh_entries);
1763 + /* we've already was here, see at next index */
1767 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1768 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1770 + if (ext3_ext_more_to_rm(path + i)) {
1771 + /* go to the next level */
1772 + ext_debug(tree, "move to level %d (block %d)\n",
1773 + i + 1, path[i].p_idx->ei_leaf);
1774 + memset(path + i + 1, 0, sizeof(*path));
1775 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1776 + if (!path[i+1].p_bh) {
1777 + /* should we reset i_size? */
1781 + /* put actual number of indexes to know is this
1782 + * number got changed at the next iteration */
1783 + path[i].p_block = path[i].p_hdr->eh_entries;
1786 + /* we finish processing this index, go up */
1787 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1788 + /* index is empty, remove it
1789 + * handle must be already prepared by the
1790 + * truncatei_leaf() */
1791 + err = ext3_ext_rm_idx(handle, tree, path + i);
1793 + /* root level have p_bh == NULL, brelse() eats this */
1794 + brelse(path[i].p_bh);
1796 + ext_debug(tree, "return to level %d\n", i);
1800 + /* TODO: flexible tree reduction should be here */
1801 + if (path->p_hdr->eh_entries == 0) {
1803 + * truncate to zero freed all the tree
1804 + * so, we need to correct eh_depth
1806 + err = ext3_ext_get_access(handle, tree, path);
1808 + EXT_ROOT_HDR(tree)->eh_depth = 0;
1809 + EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1810 + err = ext3_ext_dirty(handle, tree, path);
1813 + ext3_ext_tree_changed(tree);
1816 + ext3_journal_stop(handle, inode);
1822 + * called at mount time
1824 +void ext3_ext_init(struct super_block *sb)
1827 + * possible initialization would be here
1830 + if (test_opt(sb, EXTENTS)) {
1831 + printk("EXT3-fs: file extents enabled");
1832 +#ifdef AGRESSIVE_TEST
1833 + printk(", agressive tests");
1835 +#ifdef CHECK_BINSEARCH
1836 + printk(", check binsearch");
1843 + * called at umount time
1845 +void ext3_ext_release(struct super_block *sb)
1849 +/************************************************************************
1850 + * VFS related routines
1851 + ************************************************************************/
1853 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1855 + /* we use in-core data, not bh */
1859 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1861 + struct inode *inode = buffer;
1862 + return ext3_mark_inode_dirty(handle, inode);
1865 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1866 + struct ext3_extent *ex2)
1868 + /* FIXME: support for large fs */
1869 + if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1875 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1876 + struct ext3_extent *ex,
1877 + unsigned long from, unsigned long to)
1881 + /* at present, extent can't cross block group */;
1882 + needed = 4; /* bitmap + group desc + sb + inode */
1884 +#ifdef CONFIG_QUOTA
1885 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1891 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1892 + struct ext3_extent *ex,
1893 + unsigned long from, unsigned long to)
1895 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1896 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1897 + struct buffer_head *bh;
1900 + if (IS_ERR(handle))
1901 + return PTR_ERR(handle);
1902 + if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1903 + /* tail removal */
1904 + unsigned long num, start;
1905 + num = ex->ee_block + ex->ee_len - from;
1906 + start = ex->ee_start + ex->ee_len - num;
1907 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1909 + for (i = 0; i < num; i++) {
1910 + bh = sb_get_hash_table(tree->inode->i_sb, start + i);
1911 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1913 + ext3_free_blocks(handle, tree->inode, start, num);
1914 + } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1915 + printk("strange request: removal %lu-%lu from %u:%u\n",
1916 + from, to, ex->ee_block, ex->ee_len);
1918 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1919 + from, to, ex->ee_block, ex->ee_len);
1921 + ext3_journal_stop(handle, tree->inode);
1925 +int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
1926 + unsigned long block)
1928 + struct ext3_inode_info *ei = EXT3_I(inode);
1929 + unsigned long bg_start;
1930 + unsigned long colour;
1934 + struct ext3_extent *ex;
1935 + depth = path->p_depth;
1937 + /* try to predict block placement */
1938 + if ((ex = path[depth].p_ext))
1939 + return ex->ee_start + (block - ex->ee_block);
1941 + /* it looks index is empty
1942 + * try to find starting from index itself */
1943 + if (path[depth].p_bh)
1944 + return path[depth].p_bh->b_blocknr;
1947 + /* OK. use inode's group */
1948 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1949 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1950 + colour = (current->pid % 16) *
1951 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1952 + return bg_start + colour + block;
1955 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1956 + struct ext3_ext_path *path,
1957 + struct ext3_extent *ex, int *err)
1959 + struct inode *inode = tree->inode;
1960 + int newblock, goal;
1964 + EXT_ASSERT(ex->ee_start);
1965 + EXT_ASSERT(ex->ee_len);
1967 + /* reuse block from the extent to order data/metadata */
1968 + newblock = ex->ee_start++;
1970 + if (ex->ee_len == 0) {
1972 + /* allocate new block for the extent */
1973 + goal = ext3_ext_find_goal(inode, path, ex->ee_block);
1975 + ex->ee_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1976 + ex->ee_start_hi = 0;
1978 + if (ex->ee_start == 0) {
1979 + /* error occured: restore old extent */
1980 + ex->ee_start = newblock;
1987 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
1988 + .get_write_access = ext3_get_inode_write_access,
1989 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
1990 + .mergable = ext3_ext_mergable,
1991 + .new_block = ext3_new_block_cb,
1992 + .remove_extent = ext3_remove_blocks,
1993 + .remove_extent_credits = ext3_remove_blocks_credits,
1996 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1997 + struct inode *inode)
1999 + tree->inode = inode;
2000 + tree->root = (void *) EXT3_I(inode)->i_data;
2001 + tree->buffer = (void *) inode;
2002 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
2003 + tree->cex = (struct ext3_ext_cache *) &EXT3_I(inode)->i_cached_extent;
2004 + tree->ops = &ext3_blockmap_helpers;
2007 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2008 + long iblock, struct buffer_head *bh_result, int create)
2010 + struct ext3_ext_path *path = NULL;
2011 + struct ext3_extent newex;
2012 + struct ext3_extent *ex;
2013 + int goal, newblock, err = 0, depth;
2014 + struct ext3_extents_tree tree;
2016 + clear_bit(BH_New, &bh_result->b_state);
2017 + ext3_init_tree_desc(&tree, inode);
2018 + ext_debug(&tree, "block %d requested for inode %u\n",
2019 + (int) iblock, (unsigned) inode->i_ino);
2020 + down_write(&EXT3_I(inode)->truncate_sem);
2022 + /* check in cache */
2023 + if ((goal = ext3_ext_in_cache(&tree, iblock, &newex))) {
2024 + if (goal == EXT3_EXT_CACHE_GAP) {
2026 + /* block isn't allocated yet and
2027 + * user don't want to allocate it */
2030 + /* we should allocate requested block */
2031 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2032 + /* block is already allocated */
2033 + newblock = iblock - newex.ee_block + newex.ee_start;
2040 + /* find extent for this block */
2041 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2042 + if (IS_ERR(path)) {
2043 + err = PTR_ERR(path);
2048 + depth = EXT_DEPTH(&tree);
2051 + * consistent leaf must not be empty
2052 + * this situations is possible, though, _during_ tree modification
2053 + * this is why assert can't be put in ext3_ext_find_extent()
2055 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2057 + if ((ex = path[depth].p_ext)) {
2058 + /* if found exent covers block, simple return it */
2059 + if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2060 + newblock = iblock - ex->ee_block + ex->ee_start;
2061 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2062 + (int) iblock, ex->ee_block, ex->ee_len,
2064 + ext3_ext_put_in_cache(&tree, ex->ee_block,
2065 + ex->ee_len, ex->ee_start,
2066 + EXT3_EXT_CACHE_EXTENT);
2072 + * requested block isn't allocated yet
2073 + * we couldn't try to create block if create flag is zero
2076 + /* put just found gap into cache to speedup subsequest reqs */
2077 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2081 + /* allocate new block */
2082 + goal = ext3_ext_find_goal(inode, path, iblock);
2084 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2088 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2091 + /* try to insert new extent into found leaf and return */
2092 + newex.ee_block = iblock;
2093 + newex.ee_start = newblock;
2094 + newex.ee_start_hi = 0;
2096 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2100 + if (inode->i_size > EXT3_I(inode)->i_disksize)
2101 + EXT3_I(inode)->i_disksize = inode->i_size;
2103 + /* previous routine could use block we allocated */
2104 + newblock = newex.ee_start;
2105 + set_bit(BH_New, &bh_result->b_state);
2107 + ext3_ext_put_in_cache(&tree, newex.ee_block, newex.ee_len,
2108 + newex.ee_start, EXT3_EXT_CACHE_EXTENT);
2110 + ext3_ext_show_leaf(&tree, path);
2111 + set_bit(BH_Mapped, &bh_result->b_state);
2112 + bh_result->b_dev = inode->i_sb->s_dev;
2113 + bh_result->b_blocknr = newblock;
2116 + ext3_ext_drop_refs(path);
2119 + up_write(&EXT3_I(inode)->truncate_sem);
2124 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2126 + struct address_space *mapping = inode->i_mapping;
2127 + struct super_block *sb = inode->i_sb;
2128 + struct ext3_extents_tree tree;
2129 + unsigned long last_block;
2133 + ext3_init_tree_desc(&tree, inode);
2136 + * probably first extent we're gonna free will be last in block
2138 + err = ext3_writepage_trans_blocks(inode) + 3;
2139 + handle = ext3_journal_start(inode, err);
2140 + if (IS_ERR(handle))
2144 + ext3_block_truncate_page(handle, mapping, inode->i_size, page,
2145 + inode->i_sb->s_blocksize);
2147 + down_write(&EXT3_I(inode)->truncate_sem);
2148 + ext3_ext_invalidate_cache(&tree);
2151 + * TODO: optimization is possible here
2152 + * probably we need not scaning at all,
2153 + * because page truncation is enough
2155 + if (ext3_orphan_add(handle, inode))
2158 + /* we have to know where to truncate from in crash case */
2159 + EXT3_I(inode)->i_disksize = inode->i_size;
2160 + ext3_mark_inode_dirty(handle, inode);
2162 + last_block = (inode->i_size + sb->s_blocksize - 1) >>
2163 + EXT3_BLOCK_SIZE_BITS(sb);
2164 + err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2166 + /* In a multi-transaction truncate, we only make the final
2167 + * transaction synchronous */
2168 + if (IS_SYNC(inode))
2169 + handle->h_sync = 1;
2173 + * If this was a simple ftruncate(), and the file will remain alive
2174 + * then we need to clear up the orphan record which we created above.
2175 + * However, if this was a real unlink then we were called by
2176 + * ext3_delete_inode(), and we allow that function to clean up the
2177 + * orphan info for us.
2179 + if (inode->i_nlink)
2180 + ext3_orphan_del(handle, inode);
2182 + up_write(&EXT3_I(inode)->truncate_sem);
2183 + ext3_journal_stop(handle, inode);
2189 + page_cache_release(page);
2194 + * this routine calculate max number of blocks we could modify
2195 + * in order to allocate new block for an inode
2197 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2199 + struct ext3_extents_tree tree;
2202 + ext3_init_tree_desc(&tree, inode);
2204 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2206 + /* caller want to allocate num blocks */
2209 +#ifdef CONFIG_QUOTA
2211 + * FIXME: real calculation should be here
2212 + * it depends on blockmap format of qouta file
2214 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2220 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2222 + struct ext3_extents_tree tree;
2224 + ext3_init_tree_desc(&tree, inode);
2225 + ext3_extent_tree_init(handle, &tree);
2229 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2230 + struct ext3_ext_path *path,
2231 + struct ext3_ext_cache *newex)
2233 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2235 + if (newex->ec_type != EXT3_EXT_CACHE_EXTENT)
2236 + return EXT_CONTINUE;
2240 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2243 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2245 + buf->cur += sizeof(*newex);
2247 + buf->err = -EFAULT;
2250 + return EXT_CONTINUE;
2254 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2255 + struct ext3_ext_path *path,
2256 + struct ext3_ext_cache *ex)
2258 + struct ext3_extent_tree_stats *buf =
2259 + (struct ext3_extent_tree_stats *) tree->private;
2262 + if (ex->ec_type != EXT3_EXT_CACHE_EXTENT)
2263 + return EXT_CONTINUE;
2265 + depth = EXT_DEPTH(tree);
2266 + buf->extents_num++;
2267 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2269 + return EXT_CONTINUE;
2272 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2273 + unsigned long arg)
2277 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2280 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2281 + struct ext3_extent_buf buf;
2282 + struct ext3_extents_tree tree;
2284 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2287 + ext3_init_tree_desc(&tree, inode);
2288 + buf.cur = buf.buffer;
2290 + tree.private = &buf;
2291 + down_write(&EXT3_I(inode)->truncate_sem);
2292 + err = ext3_ext_walk_space(&tree, buf.start, EXT_MAX_BLOCK,
2293 + ext3_ext_store_extent_cb);
2294 + up_write(&EXT3_I(inode)->truncate_sem);
2297 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2298 + struct ext3_extent_tree_stats buf;
2299 + struct ext3_extents_tree tree;
2301 + ext3_init_tree_desc(&tree, inode);
2302 + down_write(&EXT3_I(inode)->truncate_sem);
2303 + buf.depth = EXT_DEPTH(&tree);
2304 + buf.extents_num = 0;
2306 + tree.private = &buf;
2307 + err = ext3_ext_walk_space(&tree, 0, EXT_MAX_BLOCK,
2308 + ext3_ext_collect_stats_cb);
2309 + up_write(&EXT3_I(inode)->truncate_sem);
2311 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2312 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2313 + struct ext3_extents_tree tree;
2314 + ext3_init_tree_desc(&tree, inode);
2315 + down_write(&EXT3_I(inode)->truncate_sem);
2316 + err = EXT_DEPTH(&tree);
2317 + up_write(&EXT3_I(inode)->truncate_sem);
2323 +EXPORT_SYMBOL(ext3_init_tree_desc);
2324 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2325 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2326 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2327 +EXPORT_SYMBOL(ext3_ext_walk_space);
2328 +EXPORT_SYMBOL(ext3_ext_find_goal);
2329 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2330 Index: linux-2.4.21-rhel/fs/ext3/ialloc.c
2331 ===================================================================
2332 --- linux-2.4.21-rhel.orig/fs/ext3/ialloc.c 2005-03-04 00:44:34.000000000 +0300
2333 +++ linux-2.4.21-rhel/fs/ext3/ialloc.c 2005-03-04 00:44:35.000000000 +0300
2334 @@ -553,7 +553,7 @@ repeat:
2335 inode->i_blksize = PAGE_SIZE;
2336 inode->i_blocks = 0;
2337 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2338 - ei->i_flags = EXT3_I(dir)->i_flags & ~EXT3_INDEX_FL;
2339 + ei->i_flags = EXT3_I(dir)->i_flags & ~(EXT3_INDEX_FL|EXT3_EXTENTS_FL);
2341 ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
2342 #ifdef EXT3_FRAGMENTS
2343 @@ -596,6 +596,19 @@
2347 + if (test_opt(sb, EXTENTS) && S_ISREG(inode->i_mode)) {
2348 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2349 + memset(&EXT3_I(inode)->i_cached_extent, 0, sizeof(__u32) * 4);
2350 + ext3_extents_initialize_blockmap(handle, inode);
2351 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2352 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2353 + if (err) goto fail;
2354 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2355 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2356 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2360 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2363 Index: linux-2.4.21-rhel/fs/ext3/inode.c
2364 ===================================================================
2365 --- linux-2.4.21-rhel.orig/fs/ext3/inode.c 2005-03-04 00:44:34.000000000 +0300
2366 +++ linux-2.4.21-rhel/fs/ext3/inode.c 2005-03-04 00:44:35.000000000 +0300
2367 @@ -859,6 +859,16 @@
2372 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2373 + struct buffer_head *bh, int create, int extend_disksize)
2375 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2376 + return ext3_ext_get_block(handle, inode, block, bh, create);
2377 + return ext3_get_block_handle(handle, inode, block, bh, create,
2382 * The BKL is not held on entry here.
2385 handle = ext3_journal_current_handle();
2386 J_ASSERT(handle != 0);
2388 - ret = ext3_get_block_handle(handle, inode, iblock,
2389 + ret = ext3_get_block_wrap(handle, inode, iblock,
2390 bh_result, create, 1);
2395 dummy.b_blocknr = -1000;
2396 buffer_trace_init(&dummy.b_history);
2397 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2398 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2399 if (!*errp && buffer_mapped(&dummy)) {
2400 struct buffer_head *bh;
2401 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2402 @@ -1564,7 +1574,7 @@
2403 * This required during truncate. We need to physically zero the tail end
2404 * of that block so it doesn't yield old data if the file is later grown.
2406 -static int ext3_block_truncate_page(handle_t *handle,
2407 +int ext3_block_truncate_page(handle_t *handle,
2408 struct address_space *mapping, loff_t from,
2409 struct page *page, unsigned blocksize)
2411 @@ -2049,6 +2059,9 @@
2412 (inode->i_mapping, inode->i_size, blocksize);
2416 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2417 + return ext3_ext_truncate(inode, page);
2419 handle = start_transaction(inode);
2421 @@ -2502,6 +2515,7 @@
2422 for (block = 0; block < EXT3_N_BLOCKS; block++)
2423 ei->i_data[block] = iloc.raw_inode->i_block[block];
2424 INIT_LIST_HEAD(&ei->i_orphan);
2425 + memset(&EXT3_I(inode)->i_cached_extent, 0, sizeof(__u32) * 4);
2427 if (EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE)
2428 EXT3_I(inode)->i_extra_isize =
2429 @@ -2842,6 +2856,9 @@
2430 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2433 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2434 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2436 if (ext3_should_journal_data(inode))
2437 ret = 3 * (bpp + indirects) + 2;
2439 @@ -3166,7 +3183,7 @@
2441 /* alloc blocks one by one */
2442 for (i = 0; i < nblocks; i++) {
2443 - ret = ext3_get_block_handle(handle, inode, blocks[i],
2444 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
2448 @@ -3242,7 +3259,7 @@
2452 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1, 1);
2453 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1, 1);
2455 printk(KERN_INFO "ext3_map_inode_page: error %d "
2456 "allocating block %ld\n", rc, iblock);
2457 Index: linux-2.4.21-rhel/fs/ext3/Makefile
2458 ===================================================================
2459 --- linux-2.4.21-rhel.orig/fs/ext3/Makefile 2005-03-04 00:44:33.000000000 +0300
2460 +++ linux-2.4.21-rhel/fs/ext3/Makefile 2005-03-04 00:44:35.000000000 +0300
2465 -export-objs := ext3-exports.o
2466 +export-objs := ext3-exports.o extents.o
2468 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2469 - ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o
2470 + ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2472 obj-m := $(O_TARGET)
2474 export-objs += xattr.o
2475 Index: linux-2.4.21-rhel/fs/ext3/super.c
2476 ===================================================================
2477 --- linux-2.4.21-rhel.orig/fs/ext3/super.c 2005-03-04 00:44:34.000000000 +0300
2478 +++ linux-2.4.21-rhel/fs/ext3/super.c 2005-03-04 00:44:35.000000000 +0300
2480 #ifdef EXT3_DELETE_THREAD
2481 J_ASSERT(sbi->s_delete_inodes == 0);
2483 + ext3_ext_release(sb);
2484 ext3_xattr_put_super(sb);
2485 journal_destroy(sbi->s_journal);
2486 if (!(sb->s_flags & MS_RDONLY)) {
2487 @@ -755,6 +756,12 @@
2491 + else if (!strcmp (this_char, "extents"))
2492 + set_opt (*mount_options, EXTENTS);
2493 + else if (!strcmp (this_char, "noextents"))
2494 + clear_opt (*mount_options, EXTENTS);
2495 + else if (!strcmp (this_char, "extdebug"))
2496 + set_opt (*mount_options, EXTDEBUG);
2497 else if (!strcmp (this_char, "grpid") ||
2498 !strcmp (this_char, "bsdgroups"))
2499 set_opt (*mount_options, GRPID);
2500 @@ -1450,6 +1455,8 @@
2501 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2504 + ext3_ext_init(sb);
2509 Index: linux-2.4.21-rhel/fs/ext3/ioctl.c
2510 ===================================================================
2511 --- linux-2.4.21-rhel.orig/fs/ext3/ioctl.c 2005-03-04 00:44:32.000000000 +0300
2512 +++ linux-2.4.21-rhel/fs/ext3/ioctl.c 2005-03-04 00:44:35.000000000 +0300
2513 @@ -173,6 +173,10 @@
2517 + case EXT3_IOC_GET_EXTENTS:
2518 + case EXT3_IOC_GET_TREE_STATS:
2519 + case EXT3_IOC_GET_TREE_DEPTH:
2520 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2524 Index: linux-2.4.21-rhel/include/linux/ext3_fs.h
2525 ===================================================================
2526 --- linux-2.4.21-rhel.orig/include/linux/ext3_fs.h 2005-03-04 00:44:34.000000000 +0300
2527 +++ linux-2.4.21-rhel/include/linux/ext3_fs.h 2005-03-04 00:44:35.000000000 +0300
2529 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2530 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2531 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2532 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2534 -#define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2535 +#define EXT3_FL_USER_VISIBLE 0x00085FFF /* User visible flags */
2536 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2540 #ifdef CONFIG_JBD_DEBUG
2541 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2543 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 7, long)
2544 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 8, long)
2545 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 9, long)
2548 * Structure of an inode on the disk
2550 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
2551 #define EXT3_MOUNT_IOPEN 0x80000 /* Allow access via iopen */
2552 #define EXT3_MOUNT_IOPEN_NOPRIV 0x100000/* Make iopen world-readable */
2553 +#define EXT3_MOUNT_EXTENTS 0x200000/* Extents support */
2554 +#define EXT3_MOUNT_EXTDEBUG 0x400000/* Extents debug */
2556 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2557 #ifndef _LINUX_EXT2_FS_H
2558 @@ -504,10 +510,12 @@
2559 #define EXT3_FEATURE_INCOMPAT_FILETYPE 0x0002
2560 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2561 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2562 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2564 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2565 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2566 - EXT3_FEATURE_INCOMPAT_RECOVER)
2567 + EXT3_FEATURE_INCOMPAT_RECOVER| \
2568 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2569 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2570 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2571 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2573 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2576 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t,
2577 + struct page *, unsigned);
2578 +extern int ext3_writepage_trans_blocks(struct inode *inode);
2579 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2580 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2581 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2582 @@ -770,6 +780,16 @@
2583 extern struct inode_operations ext3_symlink_inode_operations;
2584 extern struct inode_operations ext3_fast_symlink_inode_operations;
2587 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2588 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2589 + struct buffer_head *, int);
2590 +extern void ext3_ext_truncate(struct inode *, struct page *);
2591 +extern void ext3_ext_init(struct super_block *);
2592 +extern void ext3_ext_release(struct super_block *);
2593 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2594 +extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
2595 + unsigned int cmd, unsigned long arg);
2597 #endif /* __KERNEL__ */
2599 Index: linux-2.4.21-rhel/include/linux/ext3_extents.h
2600 ===================================================================
2601 --- linux-2.4.21-rhel.orig/include/linux/ext3_extents.h 2005-03-02 22:42:20.659360368 +0300
2602 +++ linux-2.4.21-rhel/include/linux/ext3_extents.h 2005-03-04 02:34:52.000000000 +0300
2605 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
2606 + * Written by Alex Tomas <alex@clusterfs.com>
2608 + * This program is free software; you can redistribute it and/or modify
2609 + * it under the terms of the GNU General Public License version 2 as
2610 + * published by the Free Software Foundation.
2612 + * This program is distributed in the hope that it will be useful,
2613 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2614 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2615 + * GNU General Public License for more details.
2617 + * You should have received a copy of the GNU General Public Licens
2618 + * along with this program; if not, write to the Free Software
2619 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2622 +#ifndef _LINUX_EXT3_EXTENTS
2623 +#define _LINUX_EXT3_EXTENTS
2626 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2627 + * become very little, so index split, in-depth growing and
2628 + * other hard changes happens much more often
2629 + * this is for debug purposes only
2631 +#define AGRESSIVE_TEST_
2634 + * if CHECK_BINSEARCH defined, then results of binary search
2635 + * will be checked by linear search
2637 +#define CHECK_BINSEARCH_
2640 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2641 + * to get lots of info what's going on
2645 +#define ext_debug(tree,fmt,a...) \
2647 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2648 + printk(fmt, ##a); \
2651 +#define ext_debug(tree,fmt,a...)
2655 + * if EXT_STATS is defined then stats numbers are collected
2656 + * these number will be displayed at umount time
2661 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2664 + * ext3_inode has i_block array (total 60 bytes)
2665 + * first 4 bytes are used to store:
2666 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2667 + * - number of alive extents in the inode
2671 + * this is extent on-disk structure
2672 + * it's used at the bottom of the tree
2674 +struct ext3_extent {
2675 + __u32 ee_block; /* first logical block extent covers */
2676 + __u16 ee_len; /* number of blocks covered by extent */
2677 + __u16 ee_start_hi; /* high 16 bits of physical block */
2678 + __u32 ee_start; /* low 32 bigs of physical block */
2682 + * this is index on-disk structure
2683 + * it's used at all the levels, but the bottom
2685 +struct ext3_extent_idx {
2686 + __u32 ei_block; /* index covers logical blocks from 'block' */
2687 + __u32 ei_leaf; /* pointer to the physical block of the next *
2688 + * level. leaf or next index could bet here */
2689 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
2694 + * each block (leaves and indexes), even inode-stored has header
2696 +struct ext3_extent_header {
2697 + __u16 eh_magic; /* probably will support different formats */
2698 + __u16 eh_entries; /* number of valid entries */
2699 + __u16 eh_max; /* capacity of store in entries */
2700 + __u16 eh_depth; /* has tree real underlaying blocks? */
2701 + __u32 eh_generation; /* flags(8 bits) | generation of the tree */
2704 +#define EXT3_EXT_MAGIC 0xf30a
2707 + * array of ext3_ext_path contains path to some extent
2708 + * creation/lookup routines use it for traversal/splitting/etc
2709 + * truncate uses it to simulate recursive walking
2711 +struct ext3_ext_path {
2714 + struct ext3_extent *p_ext;
2715 + struct ext3_extent_idx *p_idx;
2716 + struct ext3_extent_header *p_hdr;
2717 + struct buffer_head *p_bh;
2721 + * structure for external API
2725 + * storage for cached extent
2727 +struct ext3_ext_cache {
2734 +#define EXT3_EXT_CACHE_NO 0
2735 +#define EXT3_EXT_CACHE_GAP 1
2736 +#define EXT3_EXT_CACHE_EXTENT 2
2739 + * ext3_extents_tree is used to pass initial information
2740 + * to top-level extents API
2742 +struct ext3_extents_helpers;
2743 +struct ext3_extents_tree {
2744 + struct inode *inode; /* inode which tree belongs to */
2745 + void *root; /* ptr to data top of tree resides at */
2746 + void *buffer; /* will be passed as arg to ^^ routines */
2749 + struct ext3_ext_cache *cex;/* last found extent */
2750 + struct ext3_extents_helpers *ops;
2753 +struct ext3_extents_helpers {
2754 + int (*get_write_access)(handle_t *h, void *buffer);
2755 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2756 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2757 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2758 + struct ext3_extent *, unsigned long,
2760 + int (*remove_extent)(struct ext3_extents_tree *,
2761 + struct ext3_extent *, unsigned long,
2763 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2764 + struct ext3_ext_path *, struct ext3_extent *,
2769 + * to be called by ext3_ext_walk_space()
2770 + * negative retcode - error
2771 + * positive retcode - signal for ext3_ext_walk_space(), see below
2772 + * callback must return valid extent (passed or newly created)
2774 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2775 + struct ext3_ext_path *,
2776 + struct ext3_ext_cache *);
2778 +#define EXT_CONTINUE 0
2779 +#define EXT_BREAK 1
2780 +#define EXT_REPEAT 2
2783 +#define EXT_MAX_BLOCK 0xffffffff
2786 +#define EXT_FIRST_EXTENT(__hdr__) \
2787 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2788 + sizeof(struct ext3_extent_header)))
2789 +#define EXT_FIRST_INDEX(__hdr__) \
2790 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2791 + sizeof(struct ext3_extent_header)))
2792 +#define EXT_HAS_FREE_INDEX(__path__) \
2793 + ((__path__)->p_hdr->eh_entries < (__path__)->p_hdr->eh_max)
2794 +#define EXT_LAST_EXTENT(__hdr__) \
2795 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_entries - 1)
2796 +#define EXT_LAST_INDEX(__hdr__) \
2797 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_entries - 1)
2798 +#define EXT_MAX_EXTENT(__hdr__) \
2799 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_max - 1)
2800 +#define EXT_MAX_INDEX(__hdr__) \
2801 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_max - 1)
2802 +#define EXT_HDR_GEN(__hdr__) ((__hdr__)->eh_generation & 0x00ffffff)
2803 +#define EXT_FLAGS(__hdr__) ((__hdr__)->eh_generation >> 24)
2804 +#define EXT_FLAGS_CLR_UNKNOWN 0x7 /* Flags cleared on modification */
2806 +#define EXT_BLOCK_HDR(__bh__) ((struct ext3_extent_header *)(__bh__)->b_data)
2807 +#define EXT_ROOT_HDR(__tree__) ((struct ext3_extent_header *)(__tree__)->root)
2808 +#define EXT_DEPTH(__tree__) (EXT_ROOT_HDR(__tree__)->eh_depth)
2809 +#define EXT_GENERATION(__tree__) EXT_HDR_GEN(EXT_ROOT_HDR(__tree__))
2811 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2813 +#define EXT_CHECK_PATH(tree,path) \
2815 + int depth = EXT_DEPTH(tree); \
2816 + BUG_ON((unsigned long) (path) < __PAGE_OFFSET); \
2817 + BUG_ON((unsigned long) (path)[depth].p_idx < \
2818 + __PAGE_OFFSET && (path)[depth].p_idx != NULL); \
2819 + BUG_ON((unsigned long) (path)[depth].p_ext < \
2820 + __PAGE_OFFSET && (path)[depth].p_ext != NULL); \
2821 + BUG_ON((unsigned long) (path)[depth].p_hdr < __PAGE_OFFSET); \
2822 + BUG_ON((unsigned long) (path)[depth].p_bh < __PAGE_OFFSET \
2824 + BUG_ON((path)[0].p_depth != depth); \
2829 + * this structure is used to gather extents from the tree via ioctl
2831 +struct ext3_extent_buf {
2832 + unsigned long start;
2840 + * this structure is used to collect stats info about the tree
2842 +struct ext3_extent_tree_stats {
2848 +extern void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
2849 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2850 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2851 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2852 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2853 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2854 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2857 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
2860 + tree->cex->ec_type = EXT3_EXT_CACHE_NO;
2864 +#endif /* _LINUX_EXT3_EXTENTS */
2865 Index: linux-2.4.21-rhel/include/linux/ext3_fs_i.h
2866 ===================================================================
2867 --- linux-2.4.21-rhel.orig/include/linux/ext3_fs_i.h 2005-03-04 00:44:34.000000000 +0300
2868 +++ linux-2.4.21-rhel/include/linux/ext3_fs_i.h 2005-03-04 01:56:36.000000000 +0300
2870 * by other means, so we have truncate_sem.
2872 struct rw_semaphore truncate_sem;
2874 + __u32 i_cached_extent[4];
2877 #endif /* _LINUX_EXT3_FS_I */