1 Index: linux-2.4.24/fs/ext3/extents.c
2 ===================================================================
3 --- linux-2.4.24.orig/fs/ext3/extents.c 2003-01-30 13:24:37.000000000 +0300
4 +++ linux-2.4.24/fs/ext3/extents.c 2004-11-03 00:36:44.894076664 +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;
174 + newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
178 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
180 + struct ext3_extent_header *neh;
181 + neh = EXT_ROOT_HDR(tree);
182 + neh->eh_generation++;
185 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
189 + size = (tree->inode->i_sb->s_blocksize -
190 + sizeof(struct ext3_extent_header)) /
191 + sizeof(struct ext3_extent);
192 +#ifdef AGRESSIVE_TEST
198 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
202 + size = (tree->inode->i_sb->s_blocksize -
203 + sizeof(struct ext3_extent_header)) /
204 + sizeof(struct ext3_extent_idx);
205 +#ifdef AGRESSIVE_TEST
211 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
215 + size = (tree->buffer_len - sizeof(struct ext3_extent_header)) /
216 + sizeof(struct ext3_extent);
217 +#ifdef AGRESSIVE_TEST
223 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
227 + size = (tree->buffer_len - sizeof(struct ext3_extent_header)) /
228 + sizeof(struct ext3_extent_idx);
229 +#ifdef AGRESSIVE_TEST
235 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
236 + struct ext3_ext_path *path)
239 + int k, l = path->p_depth;
241 + ext_debug(tree, "path:");
242 + for (k = 0; k <= l; k++, path++) {
244 + ext_debug(tree, " %d->%d", path->p_idx->ei_block,
245 + path->p_idx->ei_leaf);
246 + } else if (path->p_ext) {
247 + ext_debug(tree, " %d:%d:%d",
248 + path->p_ext->ee_block,
249 + path->p_ext->ee_len,
250 + path->p_ext->ee_start);
252 + ext_debug(tree, " []");
254 + ext_debug(tree, "\n");
258 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
259 + struct ext3_ext_path *path)
262 + int depth = EXT_DEPTH(tree);
263 + struct ext3_extent_header *eh;
264 + struct ext3_extent *ex;
270 + eh = path[depth].p_hdr;
271 + ex = EXT_FIRST_EXTENT(eh);
273 + for (i = 0; i < eh->eh_entries; i++, ex++) {
274 + ext_debug(tree, "%d:%d:%d ",
275 + ex->ee_block, ex->ee_len, ex->ee_start);
277 + ext_debug(tree, "\n");
281 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
283 + int depth = path->p_depth;
286 + for (i = 0; i <= depth; i++, path++) {
288 + brelse(path->p_bh);
295 + * binary search for closest index by given block
298 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
299 + struct ext3_ext_path *path, int block)
301 + struct ext3_extent_header *eh = path->p_hdr;
302 + struct ext3_extent_idx *ix;
305 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
306 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
307 + EXT_ASSERT(eh->eh_entries > 0);
309 + ext_debug(tree, "binsearch for %d(idx): ", block);
311 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
313 + r = k = eh->eh_entries;
316 + if (block < ix[l + k].ei_block)
320 + ext_debug(tree, "%d:%d:%d ", k, l, r);
325 + ext_debug(tree," -> %d->%d ",path->p_idx->ei_block,path->p_idx->ei_leaf);
328 + if (block < ix->ei_block)
330 + path->p_idx = ix++;
332 + ext_debug(tree, " -> %d->%d\n", path->p_idx->ei_block,
333 + path->p_idx->ei_leaf);
335 +#ifdef CHECK_BINSEARCH
337 + struct ext3_extent_idx *chix;
339 + chix = ix = EXT_FIRST_INDEX(eh);
340 + for (k = 0; k < eh->eh_entries; k++, ix++) {
341 + if (k != 0 && ix->ei_block <= ix[-1].ei_block) {
342 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
343 + ix, EXT_FIRST_INDEX(eh));
344 + printk("%u <= %u\n",
345 + ix->ei_block,ix[-1].ei_block);
347 + EXT_ASSERT(k == 0 || ix->ei_block > ix[-1].ei_block);
348 + if (block < ix->ei_block)
352 + EXT_ASSERT(chix == path->p_idx);
358 + * binary search for closest extent by given block
361 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
362 + struct ext3_ext_path *path, int block)
364 + struct ext3_extent_header *eh = path->p_hdr;
365 + struct ext3_extent *ex;
368 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
369 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
371 + if (eh->eh_entries == 0) {
373 + * this leaf is empty yet:
374 + * we get such a leaf in split/add case
379 + ext_debug(tree, "binsearch for %d: ", block);
381 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
383 + r = k = eh->eh_entries;
386 + if (block < ex[l + k].ee_block)
390 + ext_debug(tree, "%d:%d:%d ", k, l, r);
395 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->ee_block,
396 + path->p_ext->ee_start, path->p_ext->ee_len);
399 + if (block < ex->ee_block)
401 + path->p_ext = ex++;
403 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->ee_block,
404 + path->p_ext->ee_start, path->p_ext->ee_len);
406 +#ifdef CHECK_BINSEARCH
408 + struct ext3_extent *chex;
410 + chex = ex = EXT_FIRST_EXTENT(eh);
411 + for (k = 0; k < eh->eh_entries; k++, ex++) {
412 + EXT_ASSERT(k == 0 || ex->ee_block > ex[-1].ee_block);
413 + if (block < ex->ee_block)
417 + EXT_ASSERT(chex == path->p_ext);
422 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
424 + struct ext3_extent_header *eh;
426 + BUG_ON(tree->buffer_len == 0);
427 + ext3_ext_get_access_for_root(handle, tree);
428 + eh = EXT_ROOT_HDR(tree);
430 + eh->eh_entries = 0;
431 + eh->eh_magic = EXT3_EXT_MAGIC;
432 + eh->eh_max = ext3_ext_space_root(tree);
433 + ext3_ext_mark_root_dirty(handle, tree);
434 + ext3_ext_invalidate_cache(tree);
438 +struct ext3_ext_path *
439 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
440 + struct ext3_ext_path *path)
442 + struct ext3_extent_header *eh;
443 + struct buffer_head *bh;
444 + int depth, i, ppos = 0;
447 + EXT_ASSERT(tree->inode);
448 + EXT_ASSERT(tree->root);
450 + eh = EXT_ROOT_HDR(tree);
452 + if (ext3_ext_check_header(eh))
455 + i = depth = EXT_DEPTH(tree);
456 + EXT_ASSERT(eh->eh_max);
457 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
459 + /* account possible depth increase */
461 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
464 + return ERR_PTR(-ENOMEM);
466 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
467 + path[0].p_hdr = eh;
469 + /* walk through the tree */
471 + ext_debug(tree, "depth %d: num %d, max %d\n",
472 + ppos, eh->eh_entries, eh->eh_max);
473 + ext3_ext_binsearch_idx(tree, path + ppos, block);
474 + path[ppos].p_block = path[ppos].p_idx->ei_leaf;
475 + path[ppos].p_depth = i;
476 + path[ppos].p_ext = NULL;
478 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
481 + eh = EXT_BLOCK_HDR(bh);
483 + EXT_ASSERT(ppos <= depth);
484 + path[ppos].p_bh = bh;
485 + path[ppos].p_hdr = eh;
488 + if (ext3_ext_check_header(eh))
492 + path[ppos].p_depth = i;
493 + path[ppos].p_hdr = eh;
494 + path[ppos].p_ext = NULL;
495 + path[ppos].p_idx = NULL;
497 + if (ext3_ext_check_header(eh))
501 + ext3_ext_binsearch(tree, path + ppos, block);
503 + ext3_ext_show_path(tree, path);
508 + printk(KERN_ERR "EXT3-fs: header is corrupted!\n");
509 + ext3_ext_drop_refs(path);
511 + return ERR_PTR(-EIO);
515 + * insert new index [logical;ptr] into the block at cupr
516 + * it check where to insert: before curp or after curp
518 +static int ext3_ext_insert_index(handle_t *handle,
519 + struct ext3_extents_tree *tree,
520 + struct ext3_ext_path *curp,
521 + int logical, int ptr)
523 + struct ext3_extent_idx *ix;
526 + if ((err = ext3_ext_get_access(handle, tree, curp)))
529 + EXT_ASSERT(logical != curp->p_idx->ei_block);
530 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
531 + if (logical > curp->p_idx->ei_block) {
533 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
534 + len = (len - 1) * sizeof(struct ext3_extent_idx);
535 + len = len < 0 ? 0 : len;
536 + ext_debug(tree, "insert new index %d after: %d. "
537 + "move %d from 0x%p to 0x%p\n",
539 + (curp->p_idx + 1), (curp->p_idx + 2));
540 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
542 + ix = curp->p_idx + 1;
544 + /* insert before */
545 + len = len * sizeof(struct ext3_extent_idx);
546 + len = len < 0 ? 0 : len;
547 + ext_debug(tree, "insert new index %d before: %d. "
548 + "move %d from 0x%p to 0x%p\n",
550 + curp->p_idx, (curp->p_idx + 1));
551 + memmove(curp->p_idx + 1, curp->p_idx, len);
555 + ix->ei_block = logical;
557 + curp->p_hdr->eh_entries++;
559 + EXT_ASSERT(curp->p_hdr->eh_entries <= curp->p_hdr->eh_max);
560 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
562 + err = ext3_ext_dirty(handle, tree, curp);
563 + ext3_std_error(tree->inode->i_sb, err);
569 + * routine inserts new subtree into the path, using free index entry
571 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
572 + * - makes decision where to split
573 + * - moves remaining extens and index entries (right to the split point)
574 + * into the newly allocated blocks
575 + * - initialize subtree
577 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
578 + struct ext3_ext_path *path,
579 + struct ext3_extent *newext, int at)
581 + struct buffer_head *bh = NULL;
582 + int depth = EXT_DEPTH(tree);
583 + struct ext3_extent_header *neh;
584 + struct ext3_extent_idx *fidx;
585 + struct ext3_extent *ex;
586 + int i = at, k, m, a;
587 + unsigned long newblock, oldblock, border;
588 + int *ablocks = NULL; /* array of allocated blocks */
591 + /* make decision: where to split? */
592 + /* FIXME: now desicion is simplest: at current extent */
594 + /* if current leaf will be splitted, then we should use
595 + * border from split point */
596 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
597 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
598 + border = path[depth].p_ext[1].ee_block;
599 + ext_debug(tree, "leaf will be splitted."
600 + " next leaf starts at %d\n",
603 + border = newext->ee_block;
604 + ext_debug(tree, "leaf will be added."
605 + " next leaf starts at %d\n",
610 + * if error occurs, then we break processing
611 + * and turn filesystem read-only. so, index won't
612 + * be inserted and tree will be in consistent
613 + * state. next mount will repair buffers too
617 + * get array to track all allocated blocks
618 + * we need this to handle errors and free blocks
621 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
624 + memset(ablocks, 0, sizeof(unsigned long) * depth);
626 + /* allocate all needed blocks */
627 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
628 + for (a = 0; a < depth - at; a++) {
629 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
632 + ablocks[a] = newblock;
635 + /* initialize new leaf */
636 + newblock = ablocks[--a];
637 + EXT_ASSERT(newblock);
638 + bh = sb_getblk(tree->inode->i_sb, newblock);
645 + if ((err = ext3_journal_get_create_access(handle, bh)))
648 + neh = EXT_BLOCK_HDR(bh);
649 + neh->eh_entries = 0;
650 + neh->eh_max = ext3_ext_space_block(tree);
651 + neh->eh_magic = EXT3_EXT_MAGIC;
653 + ex = EXT_FIRST_EXTENT(neh);
655 + /* move remain of path[depth] to the new leaf */
656 + EXT_ASSERT(path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max);
657 + /* start copy from next extent */
658 + /* TODO: we could do it by single memmove */
660 + path[depth].p_ext++;
661 + while (path[depth].p_ext <=
662 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
663 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
664 + path[depth].p_ext->ee_block,
665 + path[depth].p_ext->ee_start,
666 + path[depth].p_ext->ee_len,
668 + memmove(ex++, path[depth].p_ext++, sizeof(struct ext3_extent));
672 + mark_buffer_uptodate(bh, 1);
675 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
680 + /* correct old leaf */
682 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
684 + path[depth].p_hdr->eh_entries -= m;
685 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
690 + /* create intermediate indexes */
691 + k = depth - at - 1;
692 + EXT_ASSERT(k >= 0);
694 + ext_debug(tree, "create %d intermediate indices\n", k);
695 + /* insert new index into current index block */
696 + /* current depth stored in i var */
699 + oldblock = newblock;
700 + newblock = ablocks[--a];
701 + bh = sb_getblk(tree->inode->i_sb, newblock);
708 + if ((err = ext3_journal_get_create_access(handle, bh)))
711 + neh = EXT_BLOCK_HDR(bh);
712 + neh->eh_entries = 1;
713 + neh->eh_magic = EXT3_EXT_MAGIC;
714 + neh->eh_max = ext3_ext_space_block_idx(tree);
715 + neh->eh_depth = depth - i;
716 + fidx = EXT_FIRST_INDEX(neh);
717 + fidx->ei_block = border;
718 + fidx->ei_leaf = oldblock;
720 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
721 + i, newblock, border, oldblock);
726 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
727 + EXT_MAX_INDEX(path[i].p_hdr));
728 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
729 + EXT_LAST_INDEX(path[i].p_hdr));
730 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
731 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
732 + i, path[i].p_idx->ei_block,
733 + path[i].p_idx->ei_leaf, newblock);
734 + memmove(++fidx, path[i].p_idx++,
735 + sizeof(struct ext3_extent_idx));
737 + EXT_ASSERT(neh->eh_entries <= neh->eh_max);
740 + mark_buffer_uptodate(bh, 1);
743 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
748 + /* correct old index */
750 + err = ext3_ext_get_access(handle, tree, path + i);
753 + path[i].p_hdr->eh_entries -= m;
754 + err = ext3_ext_dirty(handle, tree, path + i);
762 + /* insert new index */
764 + err = ext3_ext_insert_index(handle, tree, path + at,
769 + if (buffer_locked(bh))
775 + /* free all allocated blocks in error case */
776 + for (i = 0; i < depth; i++) {
779 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
788 + * routine implements tree growing procedure:
789 + * - allocates new block
790 + * - moves top-level data (index block or leaf) into the new block
791 + * - initialize new top-level, creating index that points to the
792 + * just created block
794 +static int ext3_ext_grow_indepth(handle_t *handle,
795 + struct ext3_extents_tree *tree,
796 + struct ext3_ext_path *path,
797 + struct ext3_extent *newext)
799 + struct ext3_ext_path *curp = path;
800 + struct ext3_extent_header *neh;
801 + struct ext3_extent_idx *fidx;
802 + struct buffer_head *bh;
803 + unsigned long newblock;
806 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
810 + bh = sb_getblk(tree->inode->i_sb, newblock);
813 + ext3_std_error(tree->inode->i_sb, err);
818 + if ((err = ext3_journal_get_create_access(handle, bh))) {
823 + /* move top-level index/leaf into new block */
824 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
826 + /* set size of new block */
827 + neh = EXT_BLOCK_HDR(bh);
828 + /* old root could have indexes or leaves
829 + * so calculate eh_max right way */
830 + if (EXT_DEPTH(tree))
831 + neh->eh_max = ext3_ext_space_block_idx(tree);
833 + neh->eh_max = ext3_ext_space_block(tree);
834 + neh->eh_magic = EXT3_EXT_MAGIC;
835 + mark_buffer_uptodate(bh, 1);
838 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
841 + /* create index in new top-level index: num,max,pointer */
842 + if ((err = ext3_ext_get_access(handle, tree, curp)))
845 + curp->p_hdr->eh_magic = EXT3_EXT_MAGIC;
846 + curp->p_hdr->eh_max = ext3_ext_space_root_idx(tree);
847 + curp->p_hdr->eh_entries = 1;
848 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
849 + /* FIXME: it works, but actually path[0] can be index */
850 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
851 + curp->p_idx->ei_leaf = newblock;
853 + neh = EXT_ROOT_HDR(tree);
854 + fidx = EXT_FIRST_INDEX(neh);
855 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
856 + neh->eh_entries, neh->eh_max, fidx->ei_block, fidx->ei_leaf);
858 + neh->eh_depth = path->p_depth + 1;
859 + err = ext3_ext_dirty(handle, tree, curp);
867 + * routine finds empty index and adds new leaf. if no free index found
868 + * then it requests in-depth growing
870 +static int ext3_ext_create_new_leaf(handle_t *handle,
871 + struct ext3_extents_tree *tree,
872 + struct ext3_ext_path *path,
873 + struct ext3_extent *newext)
875 + struct ext3_ext_path *curp;
876 + int depth, i, err = 0;
879 + i = depth = EXT_DEPTH(tree);
881 + /* walk up to the tree and look for free index entry */
882 + curp = path + depth;
883 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
888 + /* we use already allocated block for index block
889 + * so, subsequent data blocks should be contigoues */
890 + if (EXT_HAS_FREE_INDEX(curp)) {
891 + /* if we found index with free entry, then use that
892 + * entry: create all needed subtree and add new leaf */
893 + err = ext3_ext_split(handle, tree, path, newext, i);
896 + ext3_ext_drop_refs(path);
897 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
899 + err = PTR_ERR(path);
901 + /* tree is full, time to grow in depth */
902 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
905 + ext3_ext_drop_refs(path);
906 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
908 + err = PTR_ERR(path);
911 + * only first (depth 0 -> 1) produces free space
912 + * in all other cases we have to split growed tree
914 + depth = EXT_DEPTH(tree);
915 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
916 + /* now we need split */
928 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
929 + * NOTE: it consider block number from index entry as
930 + * allocated block. thus, index entries have to be consistent
933 +static unsigned long
934 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
938 + EXT_ASSERT(path != NULL);
939 + depth = path->p_depth;
941 + if (depth == 0 && path->p_ext == NULL)
942 + return EXT_MAX_BLOCK;
944 + /* FIXME: what if index isn't full ?! */
945 + while (depth >= 0) {
946 + if (depth == path->p_depth) {
948 + if (path[depth].p_ext !=
949 + EXT_LAST_EXTENT(path[depth].p_hdr))
950 + return path[depth].p_ext[1].ee_block;
953 + if (path[depth].p_idx !=
954 + EXT_LAST_INDEX(path[depth].p_hdr))
955 + return path[depth].p_idx[1].ei_block;
960 + return EXT_MAX_BLOCK;
964 + * returns first allocated block from next leaf or EXT_MAX_BLOCK
966 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
967 + struct ext3_ext_path *path)
971 + EXT_ASSERT(path != NULL);
972 + depth = path->p_depth;
974 + /* zero-tree has no leaf blocks at all */
976 + return EXT_MAX_BLOCK;
978 + /* go to index block */
981 + while (depth >= 0) {
982 + if (path[depth].p_idx !=
983 + EXT_LAST_INDEX(path[depth].p_hdr))
984 + return path[depth].p_idx[1].ei_block;
988 + return EXT_MAX_BLOCK;
992 + * if leaf gets modified and modified extent is first in the leaf
993 + * then we have to correct all indexes above
994 + * TODO: do we need to correct tree in all cases?
996 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
997 + struct ext3_ext_path *path)
999 + struct ext3_extent_header *eh;
1000 + int depth = EXT_DEPTH(tree);
1001 + struct ext3_extent *ex;
1002 + unsigned long border;
1005 + eh = path[depth].p_hdr;
1006 + ex = path[depth].p_ext;
1011 + /* there is no tree at all */
1015 + if (ex != EXT_FIRST_EXTENT(eh)) {
1016 + /* we correct tree if first leaf got modified only */
1021 + * TODO: we need correction if border is smaller then current one
1024 + border = path[depth].p_ext->ee_block;
1025 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1027 + path[k].p_idx->ei_block = border;
1028 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1032 + /* change all left-side indexes */
1033 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1035 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1037 + path[k].p_idx->ei_block = border;
1038 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1046 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1047 + struct ext3_extent *ex1,
1048 + struct ext3_extent *ex2)
1050 + if (ex1->ee_block + ex1->ee_len != ex2->ee_block)
1053 +#ifdef AGRESSIVE_TEST
1054 + if (ex1->ee_len >= 4)
1058 + if (!tree->ops->mergable)
1061 + return tree->ops->mergable(ex1, ex2);
1065 + * this routine tries to merge requsted extent into the existing
1066 + * extent or inserts requested extent as new one into the tree,
1067 + * creating new leaf in no-space case
1069 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1070 + struct ext3_ext_path *path,
1071 + struct ext3_extent *newext)
1073 + struct ext3_extent_header * eh;
1074 + struct ext3_extent *ex, *fex;
1075 + struct ext3_extent *nearex; /* nearest extent */
1076 + struct ext3_ext_path *npath = NULL;
1077 + int depth, len, err, next;
1079 + EXT_ASSERT(newext->ee_len > 0);
1080 + depth = EXT_DEPTH(tree);
1081 + ex = path[depth].p_ext;
1082 + EXT_ASSERT(path[depth].p_hdr);
1084 + /* try to insert block into found extent and return */
1085 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1086 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1087 + newext->ee_len, ex->ee_block, ex->ee_len,
1089 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1091 + ex->ee_len += newext->ee_len;
1092 + eh = path[depth].p_hdr;
1098 + depth = EXT_DEPTH(tree);
1099 + eh = path[depth].p_hdr;
1100 + if (eh->eh_entries < eh->eh_max)
1103 + /* probably next leaf has space for us? */
1104 + fex = EXT_LAST_EXTENT(eh);
1105 + next = ext3_ext_next_leaf_block(tree, path);
1106 + if (newext->ee_block > fex->ee_block && next != EXT_MAX_BLOCK) {
1107 + ext_debug(tree, "next leaf block - %d\n", next);
1108 + EXT_ASSERT(!npath);
1109 + npath = ext3_ext_find_extent(tree, next, NULL);
1110 + if (IS_ERR(npath))
1111 + return PTR_ERR(npath);
1112 + EXT_ASSERT(npath->p_depth == path->p_depth);
1113 + eh = npath[depth].p_hdr;
1114 + if (eh->eh_entries < eh->eh_max) {
1115 + ext_debug(tree, "next leaf isnt full(%d)\n",
1120 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1121 + eh->eh_entries, eh->eh_max);
1125 + * there is no free space in found leaf
1126 + * we're gonna add new leaf in the tree
1128 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1131 + depth = EXT_DEPTH(tree);
1132 + eh = path[depth].p_hdr;
1135 + nearex = path[depth].p_ext;
1137 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1141 + /* there is no extent in this leaf, create first one */
1142 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1143 + newext->ee_block, newext->ee_start,
1145 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1146 + } else if (newext->ee_block > nearex->ee_block) {
1147 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1148 + if (nearex != EXT_LAST_EXTENT(eh)) {
1149 + len = EXT_MAX_EXTENT(eh) - nearex;
1150 + len = (len - 1) * sizeof(struct ext3_extent);
1151 + len = len < 0 ? 0 : len;
1152 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1153 + "move %d from 0x%p to 0x%p\n",
1154 + newext->ee_block, newext->ee_start,
1156 + nearex, len, nearex + 1, nearex + 2);
1157 + memmove(nearex + 2, nearex + 1, len);
1159 + path[depth].p_ext = nearex + 1;
1161 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1162 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1163 + len = len < 0 ? 0 : len;
1164 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1165 + "move %d from 0x%p to 0x%p\n",
1166 + newext->ee_block, newext->ee_start, newext->ee_len,
1167 + nearex, len, nearex + 1, nearex + 2);
1168 + memmove(nearex + 1, nearex, len);
1169 + path[depth].p_ext = nearex;
1173 + nearex = path[depth].p_ext;
1174 + nearex->ee_block = newext->ee_block;
1175 + nearex->ee_start = newext->ee_start;
1176 + nearex->ee_len = newext->ee_len;
1177 + /* FIXME: support for large fs */
1178 + nearex->ee_start_hi = 0;
1181 + /* try to merge extents to the right */
1182 + while (nearex < EXT_LAST_EXTENT(eh)) {
1183 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1185 + /* merge with next extent! */
1186 + nearex->ee_len += nearex[1].ee_len;
1187 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1188 + len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1189 + sizeof(struct ext3_extent);
1190 + memmove(nearex + 1, nearex + 2, len);
1193 + EXT_ASSERT(eh->eh_entries > 0);
1196 + /* try to merge extents to the left */
1198 + /* time to correct all indexes above */
1199 + err = ext3_ext_correct_indexes(handle, tree, path);
1203 + err = ext3_ext_dirty(handle, tree, path + depth);
1207 + ext3_ext_drop_refs(npath);
1210 + ext3_ext_tree_changed(tree);
1211 + ext3_ext_invalidate_cache(tree);
1215 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1216 + unsigned long num, ext_prepare_callback func)
1218 + struct ext3_ext_path *path = NULL;
1219 + struct ext3_ext_cache cbex;
1220 + struct ext3_extent *ex;
1221 + unsigned long next, start = 0, end = 0;
1222 + unsigned long last = block + num;
1223 + int depth, exists, err = 0;
1227 + EXT_ASSERT(tree->inode);
1228 + EXT_ASSERT(tree->root);
1230 + while (block < last && block != EXT_MAX_BLOCK) {
1231 + num = last - block;
1232 + /* find extent for this block */
1233 + path = ext3_ext_find_extent(tree, block, path);
1234 + if (IS_ERR(path)) {
1235 + err = PTR_ERR(path);
1240 + depth = EXT_DEPTH(tree);
1241 + EXT_ASSERT(path[depth].p_hdr);
1242 + ex = path[depth].p_ext;
1243 + next = ext3_ext_next_allocated_block(path);
1247 + /* there is no extent yet, so try to allocate
1248 + * all requested space */
1250 + end = block + num;
1251 + } else if (ex->ee_block > block) {
1252 + /* need to allocate space before found extent */
1254 + end = ex->ee_block;
1255 + if (block + num < end)
1256 + end = block + num;
1257 + } else if (block >= ex->ee_block + ex->ee_len) {
1258 + /* need to allocate space after found extent */
1260 + end = block + num;
1263 + } else if (block >= ex->ee_block) {
1265 + * some part of requested space is covered
1269 + end = ex->ee_block + ex->ee_len;
1270 + if (block + num < end)
1271 + end = block + num;
1276 + EXT_ASSERT(end > start);
1279 + cbex.ec_block = start;
1280 + cbex.ec_len = end - start;
1281 + cbex.ec_start = 0;
1282 + cbex.ec_type = EXT3_EXT_CACHE_GAP;
1284 + cbex.ec_block = ex->ee_block;
1285 + cbex.ec_len = ex->ee_len;
1286 + cbex.ec_start = ex->ee_start;
1287 + cbex.ec_type = EXT3_EXT_CACHE_EXTENT;
1290 + EXT_ASSERT(cbex.ec_len > 0);
1291 + EXT_ASSERT(path[depth].p_hdr);
1292 + err = func(tree, path, &cbex);
1293 + ext3_ext_drop_refs(path);
1297 + if (err == EXT_REPEAT)
1299 + else if (err == EXT_BREAK) {
1304 + if (EXT_DEPTH(tree) != depth) {
1305 + /* depth was changed. we have to realloc path */
1310 + block = cbex.ec_block + cbex.ec_len;
1314 + ext3_ext_drop_refs(path);
1322 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, __u32 block,
1323 + __u32 len, __u32 start, int type)
1325 + EXT_ASSERT(len > 0);
1327 + tree->cex->ec_type = type;
1328 + tree->cex->ec_block = block;
1329 + tree->cex->ec_len = len;
1330 + tree->cex->ec_start = start;
1335 + * this routine calculate boundaries of the gap requested block fits into
1336 + * and cache this gap
1339 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1340 + struct ext3_ext_path *path,
1341 + unsigned long block)
1343 + int depth = EXT_DEPTH(tree);
1344 + unsigned long lblock, len;
1345 + struct ext3_extent *ex;
1350 + ex = path[depth].p_ext;
1352 + /* there is no extent yet, so gap is [0;-] */
1354 + len = EXT_MAX_BLOCK;
1355 + ext_debug(tree, "cache gap(whole file):");
1356 + } else if (block < ex->ee_block) {
1358 + len = ex->ee_block - block;
1359 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1360 + (unsigned long) block,
1361 + (unsigned long) ex->ee_block,
1362 + (unsigned long) ex->ee_len);
1363 + } else if (block >= ex->ee_block + ex->ee_len) {
1364 + lblock = ex->ee_block + ex->ee_len;
1365 + len = ext3_ext_next_allocated_block(path);
1366 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1367 + (unsigned long) ex->ee_block,
1368 + (unsigned long) ex->ee_len,
1369 + (unsigned long) block);
1370 + EXT_ASSERT(len > lblock);
1371 + len = len - lblock;
1377 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) lblock, len);
1378 + ext3_ext_put_in_cache(tree, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1382 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1383 + struct ext3_extent *ex)
1385 + struct ext3_ext_cache *cex = tree->cex;
1387 + /* is there cache storage at all? */
1389 + return EXT3_EXT_CACHE_NO;
1391 + /* has cache valid data? */
1392 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1393 + return EXT3_EXT_CACHE_NO;
1395 + EXT_ASSERT(cex->ec_type == EXT3_EXT_CACHE_GAP ||
1396 + cex->ec_type == EXT3_EXT_CACHE_EXTENT);
1397 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1398 + ex->ee_block = cex->ec_block;
1399 + ex->ee_start = cex->ec_start;
1400 + ex->ee_len = cex->ec_len;
1401 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1402 + (unsigned long) block,
1403 + (unsigned long) ex->ee_block,
1404 + (unsigned long) ex->ee_len,
1405 + (unsigned long) ex->ee_start);
1406 + return cex->ec_type;
1409 + /* not in cache */
1410 + return EXT3_EXT_CACHE_NO;
1414 + * routine removes index from the index block
1415 + * it's used in truncate case only. thus all requests are for
1416 + * last index in the block only
1418 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1419 + struct ext3_ext_path *path)
1421 + struct buffer_head *bh;
1424 + /* free index block */
1426 + EXT_ASSERT(path->p_hdr->eh_entries);
1427 + if ((err = ext3_ext_get_access(handle, tree, path)))
1429 + path->p_hdr->eh_entries--;
1430 + if ((err = ext3_ext_dirty(handle, tree, path)))
1432 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1433 + path->p_idx->ei_leaf);
1434 + bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->ei_leaf);
1435 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1436 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1440 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1441 + struct ext3_ext_path *path)
1443 + int depth = EXT_DEPTH(tree);
1447 + /* probably there is space in leaf? */
1448 + if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1453 + * the worste case we're expecting is creation of the
1454 + * new root (growing in depth) with index splitting
1455 + * for splitting we have to consider depth + 1 because
1456 + * previous growing could increase it
1458 + depth = depth + 1;
1461 + * growing in depth:
1462 + * block allocation + new root + old root
1464 + needed = EXT3_ALLOC_NEEDED + 2;
1466 + /* index split. we may need:
1467 + * allocate intermediate indexes and new leaf
1468 + * change two blocks at each level, but root
1469 + * modify root block (inode)
1471 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1477 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1478 + struct ext3_ext_path *path, unsigned long start,
1479 + unsigned long end)
1481 + struct ext3_extent *ex, tex;
1482 + struct ext3_ext_path *npath;
1483 + int depth, creds, err;
1485 + depth = EXT_DEPTH(tree);
1486 + ex = path[depth].p_ext;
1488 + EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1489 + EXT_ASSERT(ex->ee_block < start);
1491 + /* calculate tail extent */
1492 + tex.ee_block = end + 1;
1493 + EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1494 + tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1496 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1497 + handle = ext3_ext_journal_restart(handle, creds);
1498 + if (IS_ERR(handle))
1499 + return PTR_ERR(handle);
1501 + /* calculate head extent. use primary extent */
1502 + err = ext3_ext_get_access(handle, tree, path + depth);
1505 + ex->ee_len = start - ex->ee_block;
1506 + err = ext3_ext_dirty(handle, tree, path + depth);
1510 + /* FIXME: some callback to free underlying resource
1511 + * and correct ee_start? */
1512 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1513 + ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1515 + npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1516 + if (IS_ERR(npath))
1517 + return PTR_ERR(npath);
1518 + depth = EXT_DEPTH(tree);
1519 + EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1520 + EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1522 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1523 + ext3_ext_drop_refs(npath);
1530 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1531 + struct ext3_ext_path *path, unsigned long start,
1532 + unsigned long end)
1534 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1535 + int err = 0, correct_index = 0;
1536 + int depth = EXT_DEPTH(tree), credits;
1537 + struct ext3_extent_header *eh;
1538 + unsigned a, b, block, num;
1540 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1541 + if (!path[depth].p_hdr)
1542 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1543 + eh = path[depth].p_hdr;
1545 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1546 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1548 + /* find where to start removing */
1549 + le = ex = EXT_LAST_EXTENT(eh);
1550 + while (ex != EXT_FIRST_EXTENT(eh)) {
1551 + if (ex->ee_block <= end)
1556 + if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1557 + /* removal of internal part of the extent requested
1558 + * tail and head must be placed in different extent
1559 + * so, we have to insert one more extent */
1560 + path[depth].p_ext = ex;
1561 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1565 + while (ex >= EXT_FIRST_EXTENT(eh) && ex->ee_block + ex->ee_len > start) {
1566 + ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1567 + path[depth].p_ext = ex;
1569 + a = ex->ee_block > start ? ex->ee_block : start;
1570 + b = ex->ee_block + ex->ee_len - 1 < end ?
1571 + ex->ee_block + ex->ee_len - 1 : end;
1573 + ext_debug(tree, " border %u:%u\n", a, b);
1575 + if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1579 + } else if (a != ex->ee_block) {
1580 + /* remove tail of the extent */
1581 + block = ex->ee_block;
1583 + } else if (b != ex->ee_block + ex->ee_len - 1) {
1584 + /* remove head of the extent */
1588 + /* remove whole extent: excelent! */
1589 + block = ex->ee_block;
1591 + EXT_ASSERT(a == ex->ee_block &&
1592 + b == ex->ee_block + ex->ee_len - 1);
1595 + if (ex == EXT_FIRST_EXTENT(eh))
1596 + correct_index = 1;
1599 + if (correct_index)
1600 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1601 + if (tree->ops->remove_extent_credits)
1602 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1604 + handle = ext3_ext_journal_restart(handle, credits);
1605 + if (IS_ERR(handle)) {
1606 + err = PTR_ERR(handle);
1610 + err = ext3_ext_get_access(handle, tree, path + depth);
1614 + if (tree->ops->remove_extent)
1615 + err = tree->ops->remove_extent(tree, ex, a, b);
1620 + /* this extent is removed entirely mark slot unused */
1626 + ex->ee_block = block;
1629 + err = ext3_ext_dirty(handle, tree, path + depth);
1633 + ext_debug(tree, "new extent: %u:%u:%u\n",
1634 + ex->ee_block, ex->ee_len, ex->ee_start);
1639 + /* reuse unused slots */
1641 + if (lu->ee_start) {
1650 + if (correct_index && eh->eh_entries)
1651 + err = ext3_ext_correct_indexes(handle, tree, path);
1653 + /* if this leaf is free, then we should
1654 + * remove it from index block above */
1655 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1656 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1663 +static struct ext3_extent_idx *
1664 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1666 + struct ext3_extent_idx *ix;
1668 + ix = EXT_LAST_INDEX(hdr);
1669 + while (ix != EXT_FIRST_INDEX(hdr)) {
1670 + if (ix->ei_block <= block)
1678 + * returns 1 if current index have to be freed (even partial)
1681 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1683 + EXT_ASSERT(path->p_idx);
1685 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1689 + * if truncate on deeper level happened it it wasn't partial
1690 + * so we have to consider current index for truncation
1692 + if (path->p_hdr->eh_entries == path->p_block)
1697 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1698 + unsigned long start, unsigned long end)
1700 + struct inode *inode = tree->inode;
1701 + struct super_block *sb = inode->i_sb;
1702 + int depth = EXT_DEPTH(tree);
1703 + struct ext3_ext_path *path;
1705 + int i = 0, err = 0;
1707 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1709 + /* probably first extent we're gonna free will be last in block */
1710 + handle = ext3_journal_start(inode, depth + 1);
1711 + if (IS_ERR(handle))
1712 + return PTR_ERR(handle);
1714 + ext3_ext_invalidate_cache(tree);
1717 + * we start scanning from right side freeing all the blocks
1718 + * after i_size and walking into the deep
1720 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1721 + if (IS_ERR(path)) {
1722 + ext3_error(sb, __FUNCTION__, "Can't allocate path array");
1723 + ext3_journal_stop(handle, inode);
1726 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1727 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1729 + while (i >= 0 && err == 0) {
1731 + /* this is leaf block */
1732 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1733 + /* root level have p_bh == NULL, brelse() eats this */
1734 + brelse(path[i].p_bh);
1739 + /* this is index block */
1740 + if (!path[i].p_hdr) {
1741 + ext_debug(tree, "initialize header\n");
1742 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1745 + EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1746 + EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1748 + if (!path[i].p_idx) {
1749 + /* this level hasn't touched yet */
1751 + ext3_ext_last_covered(path[i].p_hdr, end);
1752 + path[i].p_block = path[i].p_hdr->eh_entries + 1;
1753 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1754 + path[i].p_hdr, path[i].p_hdr->eh_entries);
1756 + /* we've already was here, see at next index */
1760 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1761 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1763 + if (ext3_ext_more_to_rm(path + i)) {
1764 + /* go to the next level */
1765 + ext_debug(tree, "move to level %d (block %d)\n",
1766 + i + 1, path[i].p_idx->ei_leaf);
1767 + memset(path + i + 1, 0, sizeof(*path));
1768 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1769 + if (!path[i+1].p_bh) {
1770 + /* should we reset i_size? */
1774 + /* put actual number of indexes to know is this
1775 + * number got changed at the next iteration */
1776 + path[i].p_block = path[i].p_hdr->eh_entries;
1779 + /* we finish processing this index, go up */
1780 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1781 + /* index is empty, remove it
1782 + * handle must be already prepared by the
1783 + * truncatei_leaf() */
1784 + err = ext3_ext_rm_idx(handle, tree, path + i);
1786 + /* root level have p_bh == NULL, brelse() eats this */
1787 + brelse(path[i].p_bh);
1789 + ext_debug(tree, "return to level %d\n", i);
1793 + /* TODO: flexible tree reduction should be here */
1794 + if (path->p_hdr->eh_entries == 0) {
1796 + * truncate to zero freed all the tree
1797 + * so, we need to correct eh_depth
1799 + err = ext3_ext_get_access(handle, tree, path);
1801 + EXT_ROOT_HDR(tree)->eh_depth = 0;
1802 + EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1803 + err = ext3_ext_dirty(handle, tree, path);
1806 + ext3_ext_tree_changed(tree);
1809 + ext3_journal_stop(handle, inode);
1815 + * called at mount time
1817 +void ext3_ext_init(struct super_block *sb)
1820 + * possible initialization would be here
1823 + if (test_opt(sb, EXTENTS)) {
1824 + printk("EXT3-fs: file extents enabled");
1825 +#ifdef AGRESSIVE_TEST
1826 + printk(", agressive tests");
1828 +#ifdef CHECK_BINSEARCH
1829 + printk(", check binsearch");
1836 + * called at umount time
1838 +void ext3_ext_release(struct super_block *sb)
1842 +/************************************************************************
1843 + * VFS related routines
1844 + ************************************************************************/
1846 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1848 + /* we use in-core data, not bh */
1852 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1854 + struct inode *inode = buffer;
1855 + return ext3_mark_inode_dirty(handle, inode);
1858 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1859 + struct ext3_extent *ex2)
1861 + /* FIXME: support for large fs */
1862 + if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1868 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1869 + struct ext3_extent *ex,
1870 + unsigned long from, unsigned long to)
1874 + /* at present, extent can't cross block group */;
1875 + needed = 4; /* bitmap + group desc + sb + inode */
1877 +#ifdef CONFIG_QUOTA
1878 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1884 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1885 + struct ext3_extent *ex,
1886 + unsigned long from, unsigned long to)
1888 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1889 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1890 + struct buffer_head *bh;
1893 + if (IS_ERR(handle))
1894 + return PTR_ERR(handle);
1895 + if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1896 + /* tail removal */
1897 + unsigned long num, start;
1898 + num = ex->ee_block + ex->ee_len - from;
1899 + start = ex->ee_start + ex->ee_len - num;
1900 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1902 + for (i = 0; i < num; i++) {
1903 + bh = sb_get_hash_table(tree->inode->i_sb, start + i);
1904 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1906 + ext3_free_blocks(handle, tree->inode, start, num);
1907 + } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1908 + printk("strange request: removal %lu-%lu from %u:%u\n",
1909 + from, to, ex->ee_block, ex->ee_len);
1911 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1912 + from, to, ex->ee_block, ex->ee_len);
1914 + ext3_journal_stop(handle, tree->inode);
1918 +int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
1919 + unsigned long block)
1921 + struct ext3_inode_info *ei = EXT3_I(inode);
1922 + unsigned long bg_start;
1923 + unsigned long colour;
1927 + struct ext3_extent *ex;
1928 + depth = path->p_depth;
1930 + /* try to predict block placement */
1931 + if ((ex = path[depth].p_ext))
1932 + return ex->ee_start + (block - ex->ee_block);
1934 + /* it looks index is empty
1935 + * try to find starting from index itself */
1936 + if (path[depth].p_bh)
1937 + return path[depth].p_bh->b_blocknr;
1940 + /* OK. use inode's group */
1941 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1942 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1943 + colour = (current->pid % 16) *
1944 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1945 + return bg_start + colour + block;
1948 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1949 + struct ext3_ext_path *path,
1950 + struct ext3_extent *ex, int *err)
1952 + struct inode *inode = tree->inode;
1953 + int newblock, goal;
1957 + EXT_ASSERT(ex->ee_start);
1958 + EXT_ASSERT(ex->ee_len);
1960 + /* reuse block from the extent to order data/metadata */
1961 + newblock = ex->ee_start++;
1963 + if (ex->ee_len == 0) {
1965 + /* allocate new block for the extent */
1966 + goal = ext3_ext_find_goal(inode, path, ex->ee_block);
1967 + ex->ee_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1968 + if (ex->ee_start == 0) {
1969 + /* error occured: restore old extent */
1970 + ex->ee_start = newblock;
1977 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
1978 + .get_write_access = ext3_get_inode_write_access,
1979 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
1980 + .mergable = ext3_ext_mergable,
1981 + .new_block = ext3_new_block_cb,
1982 + .remove_extent = ext3_remove_blocks,
1983 + .remove_extent_credits = ext3_remove_blocks_credits,
1986 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1987 + struct inode *inode)
1989 + tree->inode = inode;
1990 + tree->root = (void *) EXT3_I(inode)->i_data;
1991 + tree->buffer = (void *) inode;
1992 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1993 + tree->cex = (struct ext3_ext_cache *) &EXT3_I(inode)->i_cached_extent;
1994 + tree->ops = &ext3_blockmap_helpers;
1997 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
1998 + long iblock, struct buffer_head *bh_result, int create)
2000 + struct ext3_ext_path *path = NULL;
2001 + struct ext3_extent newex;
2002 + struct ext3_extent *ex;
2003 + int goal, newblock, err = 0, depth;
2004 + struct ext3_extents_tree tree;
2006 + clear_bit(BH_New, &bh_result->b_state);
2007 + ext3_init_tree_desc(&tree, inode);
2008 + ext_debug(&tree, "block %d requested for inode %u\n",
2009 + (int) iblock, (unsigned) inode->i_ino);
2010 + down_write(&EXT3_I(inode)->truncate_sem);
2012 + /* check in cache */
2013 + if ((goal = ext3_ext_in_cache(&tree, iblock, &newex))) {
2014 + if (goal == EXT3_EXT_CACHE_GAP) {
2016 + /* block isn't allocated yet and
2017 + * user don't want to allocate it */
2020 + /* we should allocate requested block */
2021 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2022 + /* block is already allocated */
2023 + newblock = iblock - newex.ee_block + newex.ee_start;
2030 + /* find extent for this block */
2031 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2032 + if (IS_ERR(path)) {
2033 + err = PTR_ERR(path);
2038 + depth = EXT_DEPTH(&tree);
2041 + * consistent leaf must not be empty
2042 + * this situations is possible, though, _during_ tree modification
2043 + * this is why assert can't be put in ext3_ext_find_extent()
2045 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2047 + if ((ex = path[depth].p_ext)) {
2048 + /* if found exent covers block, simple return it */
2049 + if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2050 + newblock = iblock - ex->ee_block + ex->ee_start;
2051 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2052 + (int) iblock, ex->ee_block, ex->ee_len,
2054 + ext3_ext_put_in_cache(&tree, ex->ee_block,
2055 + ex->ee_len, ex->ee_start,
2056 + EXT3_EXT_CACHE_EXTENT);
2062 + * requested block isn't allocated yet
2063 + * we couldn't try to create block if create flag is zero
2066 + /* put just found gap into cache to speedup subsequest reqs */
2067 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2071 + /* allocate new block */
2072 + goal = ext3_ext_find_goal(inode, path, iblock);
2073 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2076 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2079 + /* try to insert new extent into found leaf and return */
2080 + newex.ee_block = iblock;
2081 + newex.ee_start = newblock;
2083 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2087 + if (inode->i_size > EXT3_I(inode)->i_disksize)
2088 + EXT3_I(inode)->i_disksize = inode->i_size;
2090 + /* previous routine could use block we allocated */
2091 + newblock = newex.ee_start;
2092 + set_bit(BH_New, &bh_result->b_state);
2094 + ext3_ext_put_in_cache(&tree, newex.ee_block, newex.ee_len,
2095 + newex.ee_start, EXT3_EXT_CACHE_EXTENT);
2097 + ext3_ext_show_leaf(&tree, path);
2098 + set_bit(BH_Mapped, &bh_result->b_state);
2099 + bh_result->b_dev = inode->i_sb->s_dev;
2100 + bh_result->b_blocknr = newblock;
2103 + ext3_ext_drop_refs(path);
2106 + up_write(&EXT3_I(inode)->truncate_sem);
2111 +void ext3_ext_truncate(struct inode * inode)
2113 + struct address_space *mapping = inode->i_mapping;
2114 + struct super_block *sb = inode->i_sb;
2115 + struct ext3_extents_tree tree;
2116 + unsigned long last_block;
2120 + ext3_init_tree_desc(&tree, inode);
2123 + * probably first extent we're gonna free will be last in block
2125 + err = ext3_writepage_trans_blocks(inode) + 3;
2126 + handle = ext3_journal_start(inode, err);
2127 + if (IS_ERR(handle))
2130 + ext3_block_truncate_page(handle, mapping, inode->i_size);
2132 + down_write(&EXT3_I(inode)->truncate_sem);
2133 + ext3_ext_invalidate_cache(&tree);
2136 + * TODO: optimization is possible here
2137 + * probably we need not scaning at all,
2138 + * because page truncation is enough
2140 + if (ext3_orphan_add(handle, inode))
2143 + /* we have to know where to truncate from in crash case */
2144 + EXT3_I(inode)->i_disksize = inode->i_size;
2145 + ext3_mark_inode_dirty(handle, inode);
2147 + last_block = (inode->i_size + sb->s_blocksize - 1) >>
2148 + EXT3_BLOCK_SIZE_BITS(sb);
2149 + err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2151 + /* In a multi-transaction truncate, we only make the final
2152 + * transaction synchronous */
2153 + if (IS_SYNC(inode))
2154 + handle->h_sync = 1;
2158 + * If this was a simple ftruncate(), and the file will remain alive
2159 + * then we need to clear up the orphan record which we created above.
2160 + * However, if this was a real unlink then we were called by
2161 + * ext3_delete_inode(), and we allow that function to clean up the
2162 + * orphan info for us.
2164 + if (inode->i_nlink)
2165 + ext3_orphan_del(handle, inode);
2167 + up_write(&EXT3_I(inode)->truncate_sem);
2168 + ext3_journal_stop(handle, inode);
2172 + * this routine calculate max number of blocks we could modify
2173 + * in order to allocate new block for an inode
2175 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2177 + struct ext3_extents_tree tree;
2180 + ext3_init_tree_desc(&tree, inode);
2182 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2184 + /* caller want to allocate num blocks */
2187 +#ifdef CONFIG_QUOTA
2189 + * FIXME: real calculation should be here
2190 + * it depends on blockmap format of qouta file
2192 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2198 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2200 + struct ext3_extents_tree tree;
2202 + ext3_init_tree_desc(&tree, inode);
2203 + ext3_extent_tree_init(handle, &tree);
2207 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2208 + struct ext3_ext_path *path,
2209 + struct ext3_ext_cache *newex)
2211 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2213 + if (newex->ec_type != EXT3_EXT_CACHE_EXTENT)
2214 + return EXT_CONTINUE;
2218 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2221 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2223 + buf->cur += sizeof(*newex);
2225 + buf->err = -EFAULT;
2228 + return EXT_CONTINUE;
2232 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2233 + struct ext3_ext_path *path,
2234 + struct ext3_ext_cache *ex)
2236 + struct ext3_extent_tree_stats *buf =
2237 + (struct ext3_extent_tree_stats *) tree->private;
2240 + if (ex->ec_type != EXT3_EXT_CACHE_EXTENT)
2241 + return EXT_CONTINUE;
2243 + depth = EXT_DEPTH(tree);
2244 + buf->extents_num++;
2245 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2247 + return EXT_CONTINUE;
2250 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2251 + unsigned long arg)
2255 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2258 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2259 + struct ext3_extent_buf buf;
2260 + struct ext3_extents_tree tree;
2262 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2265 + ext3_init_tree_desc(&tree, inode);
2266 + buf.cur = buf.buffer;
2268 + tree.private = &buf;
2269 + down_write(&EXT3_I(inode)->truncate_sem);
2270 + err = ext3_ext_walk_space(&tree, buf.start, EXT_MAX_BLOCK,
2271 + ext3_ext_store_extent_cb);
2272 + up_write(&EXT3_I(inode)->truncate_sem);
2275 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2276 + struct ext3_extent_tree_stats buf;
2277 + struct ext3_extents_tree tree;
2279 + ext3_init_tree_desc(&tree, inode);
2280 + down_write(&EXT3_I(inode)->truncate_sem);
2281 + buf.depth = EXT_DEPTH(&tree);
2282 + buf.extents_num = 0;
2284 + tree.private = &buf;
2285 + err = ext3_ext_walk_space(&tree, 0, EXT_MAX_BLOCK,
2286 + ext3_ext_collect_stats_cb);
2287 + up_write(&EXT3_I(inode)->truncate_sem);
2289 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2290 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2291 + struct ext3_extents_tree tree;
2292 + ext3_init_tree_desc(&tree, inode);
2293 + down_write(&EXT3_I(inode)->truncate_sem);
2294 + err = EXT_DEPTH(&tree);
2295 + up_write(&EXT3_I(inode)->truncate_sem);
2301 +EXPORT_SYMBOL(ext3_init_tree_desc);
2302 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2303 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2304 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2305 +EXPORT_SYMBOL(ext3_ext_walk_space);
2306 +EXPORT_SYMBOL(ext3_ext_find_goal);
2307 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2308 Index: linux-2.4.24/fs/ext3/ialloc.c
2309 ===================================================================
2310 --- linux-2.4.24.orig/fs/ext3/ialloc.c 2004-11-02 20:28:32.000000000 +0300
2311 +++ linux-2.4.24/fs/ext3/ialloc.c 2004-11-02 20:32:17.000000000 +0300
2312 @@ -592,6 +592,19 @@
2316 + if (test_opt(sb, EXTENTS) && S_ISREG(inode->i_mode)) {
2317 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2318 + memset(&EXT3_I(inode)->i_cached_extent, 0, sizeof(__u32) * 4);
2319 + ext3_extents_initialize_blockmap(handle, inode);
2320 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2321 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2322 + if (err) goto fail;
2323 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2324 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2325 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2329 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2332 Index: linux-2.4.24/fs/ext3/inode.c
2333 ===================================================================
2334 --- linux-2.4.24.orig/fs/ext3/inode.c 2004-11-02 20:28:33.000000000 +0300
2335 +++ linux-2.4.24/fs/ext3/inode.c 2004-11-02 20:32:17.000000000 +0300
2336 @@ -848,6 +848,15 @@
2341 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2342 + struct buffer_head *bh, int create)
2344 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2345 + return ext3_ext_get_block(handle, inode, block, bh, create);
2346 + return ext3_get_block_handle(handle, inode, block, bh, create);
2350 * The BKL is not held on entry here.
2353 handle = ext3_journal_current_handle();
2354 J_ASSERT(handle != 0);
2356 - ret = ext3_get_block_handle(handle, inode, iblock, bh_result, create);
2357 + ret = ext3_get_block_wrap(handle, inode, iblock, bh_result, create);
2363 dummy.b_blocknr = -1000;
2364 buffer_trace_init(&dummy.b_history);
2365 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create);
2366 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create);
2367 if (!*errp && buffer_mapped(&dummy)) {
2368 struct buffer_head *bh;
2369 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2370 @@ -1403,7 +1412,7 @@
2371 * This required during truncate. We need to physically zero the tail end
2372 * of that block so it doesn't yield old data if the file is later grown.
2374 -static int ext3_block_truncate_page(handle_t *handle,
2375 +int ext3_block_truncate_page(handle_t *handle,
2376 struct address_space *mapping, loff_t from)
2378 unsigned long index = from >> PAGE_CACHE_SHIFT;
2379 @@ -1889,6 +1898,9 @@
2381 ext3_discard_prealloc(inode);
2383 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2384 + return ext3_ext_truncate(inode);
2386 handle = start_transaction(inode);
2388 return; /* AKPM: return what? */
2389 @@ -2225,6 +2237,7 @@
2390 for (block = 0; block < EXT3_N_BLOCKS; block++)
2391 inode->u.ext3_i.i_data[block] = iloc.raw_inode->i_block[block];
2392 INIT_LIST_HEAD(&inode->u.ext3_i.i_orphan);
2393 + memset(&EXT3_I(inode)->i_cached_extent, 0, sizeof(__u32) * 4);
2395 if (EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE)
2396 inode->u.ext3_i.i_extra_isize =
2397 @@ -2537,6 +2550,9 @@
2398 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2401 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2402 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2404 if (ext3_should_journal_data(inode))
2405 ret = 3 * (bpp + indirects) + 2;
2407 @@ -2973,7 +2989,7 @@
2409 /* alloc blocks one by one */
2410 for (i = 0; i < nblocks; i++) {
2411 - ret = ext3_get_block_handle(handle, inode, blocks[i],
2412 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
2416 @@ -3049,7 +3065,7 @@
2420 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1);
2421 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1);
2423 printk(KERN_INFO "ext3_map_inode_page: error %d "
2424 "allocating block %ld\n", rc, iblock);
2425 Index: linux-2.4.24/fs/ext3/Makefile
2426 ===================================================================
2427 --- linux-2.4.24.orig/fs/ext3/Makefile 2004-11-02 20:28:32.000000000 +0300
2428 +++ linux-2.4.24/fs/ext3/Makefile 2004-11-02 20:32:17.000000000 +0300
2431 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2432 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2434 + xattr_trusted.o extents.o
2435 +export-objs += extents.o
2437 obj-m := $(O_TARGET)
2439 export-objs += xattr.o
2440 Index: linux-2.4.24/fs/ext3/super.c
2441 ===================================================================
2442 --- linux-2.4.24.orig/fs/ext3/super.c 2004-11-02 20:28:32.000000000 +0300
2443 +++ linux-2.4.24/fs/ext3/super.c 2004-11-02 20:32:17.000000000 +0300
2445 #ifdef EXT3_DELETE_THREAD
2446 J_ASSERT(sbi->s_delete_inodes == 0);
2448 + ext3_ext_release(sb);
2449 ext3_xattr_put_super(sb);
2450 journal_destroy(sbi->s_journal);
2451 if (!(sb->s_flags & MS_RDONLY)) {
2452 @@ -704,6 +705,10 @@
2456 + else if (!strcmp (this_char, "extents"))
2457 + set_opt (*mount_options, EXTENTS);
2458 + else if (!strcmp (this_char, "extdebug"))
2459 + set_opt (*mount_options, EXTDEBUG);
2460 else if (!strcmp (this_char, "grpid") ||
2461 !strcmp (this_char, "bsdgroups"))
2462 set_opt (*mount_options, GRPID);
2463 @@ -1395,6 +1400,8 @@
2464 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2467 + ext3_ext_init(sb);
2472 Index: linux-2.4.24/fs/ext3/ioctl.c
2473 ===================================================================
2474 --- linux-2.4.24.orig/fs/ext3/ioctl.c 2004-11-02 20:28:29.000000000 +0300
2475 +++ linux-2.4.24/fs/ext3/ioctl.c 2004-11-02 20:32:17.000000000 +0300
2476 @@ -174,6 +174,10 @@
2480 + case EXT3_IOC_GET_EXTENTS:
2481 + case EXT3_IOC_GET_TREE_STATS:
2482 + case EXT3_IOC_GET_TREE_DEPTH:
2483 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2487 Index: linux-2.4.24/include/linux/ext3_fs.h
2488 ===================================================================
2489 --- linux-2.4.24.orig/include/linux/ext3_fs.h 2004-11-02 20:28:32.000000000 +0300
2490 +++ linux-2.4.24/include/linux/ext3_fs.h 2004-11-02 20:32:17.000000000 +0300
2492 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
2493 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
2494 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2495 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2497 -#define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
2498 +#define EXT3_FL_USER_VISIBLE 0x00085FFF /* User visible flags */
2499 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
2503 #ifdef CONFIG_JBD_DEBUG
2504 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2506 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 5, long)
2507 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 6, long)
2508 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 7, long)
2511 * Structure of an inode on the disk
2513 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
2514 #define EXT3_MOUNT_IOPEN 0x40000 /* Allow access via iopen */
2515 #define EXT3_MOUNT_IOPEN_NOPRIV 0x80000 /* Make iopen world-readable */
2516 +#define EXT3_MOUNT_EXTENTS 0x100000/* Extents support */
2517 +#define EXT3_MOUNT_EXTDEBUG 0x200000/* Extents debug */
2519 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2520 #ifndef _LINUX_EXT2_FS_H
2521 @@ -503,10 +509,12 @@
2522 #define EXT3_FEATURE_INCOMPAT_FILETYPE 0x0002
2523 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2524 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2525 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2527 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2528 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2529 - EXT3_FEATURE_INCOMPAT_RECOVER)
2530 + EXT3_FEATURE_INCOMPAT_RECOVER| \
2531 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2532 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2533 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2534 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2536 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2539 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2540 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2541 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2542 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2543 @@ -769,6 +778,16 @@
2544 extern struct inode_operations ext3_symlink_inode_operations;
2545 extern struct inode_operations ext3_fast_symlink_inode_operations;
2548 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2549 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2550 + struct buffer_head *, int);
2551 +extern void ext3_ext_truncate(struct inode *);
2552 +extern void ext3_ext_init(struct super_block *);
2553 +extern void ext3_ext_release(struct super_block *);
2554 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2555 +extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
2556 + unsigned int cmd, unsigned long arg);
2558 #endif /* __KERNEL__ */
2560 Index: linux-2.4.24/include/linux/ext3_extents.h
2561 ===================================================================
2562 --- linux-2.4.24.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2563 +++ linux-2.4.24/include/linux/ext3_extents.h 2004-11-02 20:32:17.000000000 +0300
2566 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
2567 + * Written by Alex Tomas <alex@clusterfs.com>
2569 + * This program is free software; you can redistribute it and/or modify
2570 + * it under the terms of the GNU General Public License version 2 as
2571 + * published by the Free Software Foundation.
2573 + * This program is distributed in the hope that it will be useful,
2574 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2575 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2576 + * GNU General Public License for more details.
2578 + * You should have received a copy of the GNU General Public Licens
2579 + * along with this program; if not, write to the Free Software
2580 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2583 +#ifndef _LINUX_EXT3_EXTENTS
2584 +#define _LINUX_EXT3_EXTENTS
2587 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2588 + * become very little, so index split, in-depth growing and
2589 + * other hard changes happens much more often
2590 + * this is for debug purposes only
2592 +#define AGRESSIVE_TEST_
2595 + * if CHECK_BINSEARCH defined, then results of binary search
2596 + * will be checked by linear search
2598 +#define CHECK_BINSEARCH_
2601 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2602 + * to get lots of info what's going on
2606 +#define ext_debug(tree,fmt,a...) \
2608 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2609 + printk(fmt, ##a); \
2612 +#define ext_debug(tree,fmt,a...)
2616 + * if EXT_STATS is defined then stats numbers are collected
2617 + * these number will be displayed at umount time
2622 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2625 + * ext3_inode has i_block array (total 60 bytes)
2626 + * first 4 bytes are used to store:
2627 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2628 + * - number of alive extents in the inode
2632 + * this is extent on-disk structure
2633 + * it's used at the bottom of the tree
2635 +struct ext3_extent {
2636 + __u32 ee_block; /* first logical block extent covers */
2637 + __u16 ee_len; /* number of blocks covered by extent */
2638 + __u16 ee_start_hi; /* high 16 bits of physical block */
2639 + __u32 ee_start; /* low 32 bigs of physical block */
2643 + * this is index on-disk structure
2644 + * it's used at all the levels, but the bottom
2646 +struct ext3_extent_idx {
2647 + __u32 ei_block; /* index covers logical blocks from 'block' */
2648 + __u32 ei_leaf; /* pointer to the physical block of the next *
2649 + * level. leaf or next index could bet here */
2650 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
2655 + * each block (leaves and indexes), even inode-stored has header
2657 +struct ext3_extent_header {
2658 + __u16 eh_magic; /* probably will support different formats */
2659 + __u16 eh_entries; /* number of valid entries */
2660 + __u16 eh_max; /* capacity of store in entries */
2661 + __u16 eh_depth; /* has tree real underlaying blocks? */
2662 + __u32 eh_generation; /* generation of the tree */
2665 +#define EXT3_EXT_MAGIC 0xf30a
2668 + * array of ext3_ext_path contains path to some extent
2669 + * creation/lookup routines use it for traversal/splitting/etc
2670 + * truncate uses it to simulate recursive walking
2672 +struct ext3_ext_path {
2675 + struct ext3_extent *p_ext;
2676 + struct ext3_extent_idx *p_idx;
2677 + struct ext3_extent_header *p_hdr;
2678 + struct buffer_head *p_bh;
2682 + * structure for external API
2686 + * storage for cached extent
2688 +struct ext3_ext_cache {
2695 +#define EXT3_EXT_CACHE_NO 0
2696 +#define EXT3_EXT_CACHE_GAP 1
2697 +#define EXT3_EXT_CACHE_EXTENT 2
2700 + * ext3_extents_tree is used to pass initial information
2701 + * to top-level extents API
2703 +struct ext3_extents_helpers;
2704 +struct ext3_extents_tree {
2705 + struct inode *inode; /* inode which tree belongs to */
2706 + void *root; /* ptr to data top of tree resides at */
2707 + void *buffer; /* will be passed as arg to ^^ routines */
2710 + struct ext3_ext_cache *cex;/* last found extent */
2711 + struct ext3_extents_helpers *ops;
2714 +struct ext3_extents_helpers {
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_ext_cache *);
2739 +#define EXT_CONTINUE 0
2740 +#define EXT_BREAK 1
2741 +#define EXT_REPEAT 2
2744 +#define EXT_MAX_BLOCK 0xffffffff
2747 +#define EXT_FIRST_EXTENT(__hdr__) \
2748 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2749 + sizeof(struct ext3_extent_header)))
2750 +#define EXT_FIRST_INDEX(__hdr__) \
2751 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2752 + sizeof(struct ext3_extent_header)))
2753 +#define EXT_HAS_FREE_INDEX(__path__) \
2754 + ((__path__)->p_hdr->eh_entries < (__path__)->p_hdr->eh_max)
2755 +#define EXT_LAST_EXTENT(__hdr__) \
2756 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_entries - 1)
2757 +#define EXT_LAST_INDEX(__hdr__) \
2758 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_entries - 1)
2759 +#define EXT_MAX_EXTENT(__hdr__) \
2760 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_max - 1)
2761 +#define EXT_MAX_INDEX(__hdr__) \
2762 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_max - 1)
2764 +#define EXT_ROOT_HDR(tree) \
2765 + ((struct ext3_extent_header *) (tree)->root)
2766 +#define EXT_BLOCK_HDR(bh) \
2767 + ((struct ext3_extent_header *) (bh)->b_data)
2768 +#define EXT_DEPTH(_t_) \
2769 + (((struct ext3_extent_header *)((_t_)->root))->eh_depth)
2770 +#define EXT_GENERATION(_t_) \
2771 + (((struct ext3_extent_header *)((_t_)->root))->eh_generation)
2774 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2776 +#define EXT_CHECK_PATH(tree,path) \
2778 + int depth = EXT_DEPTH(tree); \
2779 + BUG_ON((unsigned long) (path) < __PAGE_OFFSET); \
2780 + BUG_ON((unsigned long) (path)[depth].p_idx < \
2781 + __PAGE_OFFSET && (path)[depth].p_idx != NULL); \
2782 + BUG_ON((unsigned long) (path)[depth].p_ext < \
2783 + __PAGE_OFFSET && (path)[depth].p_ext != NULL); \
2784 + BUG_ON((unsigned long) (path)[depth].p_hdr < __PAGE_OFFSET); \
2785 + BUG_ON((unsigned long) (path)[depth].p_bh < __PAGE_OFFSET \
2787 + BUG_ON((path)[0].p_depth != depth); \
2792 + * this structure is used to gather extents from the tree via ioctl
2794 +struct ext3_extent_buf {
2795 + unsigned long start;
2803 + * this structure is used to collect stats info about the tree
2805 +struct ext3_extent_tree_stats {
2811 +extern void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
2812 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2813 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2814 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2815 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2816 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2817 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2820 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
2823 + tree->cex->ec_type = EXT3_EXT_CACHE_NO;
2827 +#endif /* _LINUX_EXT3_EXTENTS */
2828 Index: linux-2.4.24/include/linux/ext3_fs_i.h
2829 ===================================================================
2830 --- linux-2.4.24.orig/include/linux/ext3_fs_i.h 2004-11-02 20:28:32.000000000 +0300
2831 +++ linux-2.4.24/include/linux/ext3_fs_i.h 2004-11-02 20:43:45.000000000 +0300
2833 * by other means, so we have truncate_sem.
2835 struct rw_semaphore truncate_sem;
2837 + __u32 i_cached_extent[4];
2840 #endif /* _LINUX_EXT3_FS_I */