2 Index: linux-2.6.5-sles9/fs/ext3/extents.c
3 ===================================================================
4 --- linux-2.6.5-sles9.orig/fs/ext3/extents.c 2005-02-17 22:07:57.023609040 +0300
5 +++ linux-2.6.5-sles9/fs/ext3/extents.c 2005-02-23 01:02:37.396435640 +0300
8 + * Copyright(c) 2003, 2004, 2005, Cluster File Systems, Inc, info@clusterfs.com
9 + * Written by Alex Tomas <alex@clusterfs.com>
11 + * This program is free software; you can redistribute it and/or modify
12 + * it under the terms of the GNU General Public License version 2 as
13 + * published by the Free Software Foundation.
15 + * This program is distributed in the hope that it will be useful,
16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 + * GNU General Public License for more details.
20 + * You should have received a copy of the GNU General Public Licens
21 + * along with this program; if not, write to the Free Software
22 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
26 + * Extents support for EXT3
29 + * - ext3_ext_walk_space() sould not use ext3_ext_find_extent()
30 + * - ext3_ext_calc_credits() could take 'mergable' into account
31 + * - ext3*_error() should be used in some situations
32 + * - find_goal() [to be tested and improved]
33 + * - smart tree reduction
34 + * - arch-independence
35 + * common on-disk format for big/little-endian arch
38 +#include <linux/module.h>
39 +#include <linux/fs.h>
40 +#include <linux/time.h>
41 +#include <linux/ext3_jbd.h>
42 +#include <linux/jbd.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, 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);
482 + eh = EXT_BLOCK_HDR(bh);
484 + EXT_ASSERT(ppos <= depth);
485 + path[ppos].p_bh = bh;
486 + path[ppos].p_hdr = eh;
489 + if (ext3_ext_check_header(eh))
493 + path[ppos].p_depth = i;
494 + path[ppos].p_hdr = eh;
495 + path[ppos].p_ext = NULL;
496 + path[ppos].p_idx = NULL;
498 + if (ext3_ext_check_header(eh))
502 + ext3_ext_binsearch(tree, path + ppos, block);
504 + ext3_ext_show_path(tree, path);
509 + printk(KERN_ERR "EXT3-fs: header is corrupted!\n");
510 + ext3_ext_drop_refs(path);
512 + return ERR_PTR(-EIO);
516 + * insert new index [logical;ptr] into the block at cupr
517 + * it check where to insert: before curp or after curp
519 +static int ext3_ext_insert_index(handle_t *handle,
520 + struct ext3_extents_tree *tree,
521 + struct ext3_ext_path *curp,
522 + int logical, int ptr)
524 + struct ext3_extent_idx *ix;
527 + if ((err = ext3_ext_get_access(handle, tree, curp)))
530 + EXT_ASSERT(logical != curp->p_idx->ei_block);
531 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
532 + if (logical > curp->p_idx->ei_block) {
534 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
535 + len = (len - 1) * sizeof(struct ext3_extent_idx);
536 + len = len < 0 ? 0 : len;
537 + ext_debug(tree, "insert new index %d after: %d. "
538 + "move %d from 0x%p to 0x%p\n",
540 + (curp->p_idx + 1), (curp->p_idx + 2));
541 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
543 + ix = curp->p_idx + 1;
545 + /* insert before */
546 + len = len * sizeof(struct ext3_extent_idx);
547 + len = len < 0 ? 0 : len;
548 + ext_debug(tree, "insert new index %d before: %d. "
549 + "move %d from 0x%p to 0x%p\n",
551 + curp->p_idx, (curp->p_idx + 1));
552 + memmove(curp->p_idx + 1, curp->p_idx, len);
556 + ix->ei_block = logical;
558 + curp->p_hdr->eh_entries++;
560 + EXT_ASSERT(curp->p_hdr->eh_entries <= curp->p_hdr->eh_max);
561 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
563 + err = ext3_ext_dirty(handle, tree, curp);
564 + ext3_std_error(tree->inode->i_sb, err);
570 + * routine inserts new subtree into the path, using free index entry
572 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
573 + * - makes decision where to split
574 + * - moves remaining extens and index entries (right to the split point)
575 + * into the newly allocated blocks
576 + * - initialize subtree
578 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
579 + struct ext3_ext_path *path,
580 + struct ext3_extent *newext, int at)
582 + struct buffer_head *bh = NULL;
583 + int depth = EXT_DEPTH(tree);
584 + struct ext3_extent_header *neh;
585 + struct ext3_extent_idx *fidx;
586 + struct ext3_extent *ex;
587 + int i = at, k, m, a;
588 + unsigned long newblock, oldblock, border;
589 + int *ablocks = NULL; /* array of allocated blocks */
592 + /* make decision: where to split? */
593 + /* FIXME: now desicion is simplest: at current extent */
595 + /* if current leaf will be splitted, then we should use
596 + * border from split point */
597 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
598 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
599 + border = path[depth].p_ext[1].ee_block;
600 + ext_debug(tree, "leaf will be splitted."
601 + " next leaf starts at %d\n",
604 + border = newext->ee_block;
605 + ext_debug(tree, "leaf will be added."
606 + " next leaf starts at %d\n",
611 + * if error occurs, then we break processing
612 + * and turn filesystem read-only. so, index won't
613 + * be inserted and tree will be in consistent
614 + * state. next mount will repair buffers too
618 + * get array to track all allocated blocks
619 + * we need this to handle errors and free blocks
622 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
625 + memset(ablocks, 0, sizeof(unsigned long) * depth);
627 + /* allocate all needed blocks */
628 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
629 + for (a = 0; a < depth - at; a++) {
630 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
633 + ablocks[a] = newblock;
636 + /* initialize new leaf */
637 + newblock = ablocks[--a];
638 + EXT_ASSERT(newblock);
639 + bh = sb_getblk(tree->inode->i_sb, newblock);
646 + if ((err = ext3_journal_get_create_access(handle, bh)))
649 + neh = EXT_BLOCK_HDR(bh);
650 + neh->eh_entries = 0;
651 + neh->eh_max = ext3_ext_space_block(tree);
652 + neh->eh_magic = EXT3_EXT_MAGIC;
654 + ex = EXT_FIRST_EXTENT(neh);
656 + /* move remain of path[depth] to the new leaf */
657 + EXT_ASSERT(path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max);
658 + /* start copy from next extent */
659 + /* TODO: we could do it by single memmove */
661 + path[depth].p_ext++;
662 + while (path[depth].p_ext <=
663 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
664 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
665 + path[depth].p_ext->ee_block,
666 + path[depth].p_ext->ee_start,
667 + path[depth].p_ext->ee_len,
669 + memmove(ex++, path[depth].p_ext++, sizeof(struct ext3_extent));
673 + set_buffer_uptodate(bh);
676 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
681 + /* correct old leaf */
683 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
685 + path[depth].p_hdr->eh_entries -= m;
686 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
691 + /* create intermediate indexes */
692 + k = depth - at - 1;
693 + EXT_ASSERT(k >= 0);
695 + ext_debug(tree, "create %d intermediate indices\n", k);
696 + /* insert new index into current index block */
697 + /* current depth stored in i var */
700 + oldblock = newblock;
701 + newblock = ablocks[--a];
702 + bh = sb_getblk(tree->inode->i_sb, newblock);
709 + if ((err = ext3_journal_get_create_access(handle, bh)))
712 + neh = EXT_BLOCK_HDR(bh);
713 + neh->eh_entries = 1;
714 + neh->eh_magic = EXT3_EXT_MAGIC;
715 + neh->eh_max = ext3_ext_space_block_idx(tree);
716 + neh->eh_depth = depth - i;
717 + fidx = EXT_FIRST_INDEX(neh);
718 + fidx->ei_block = border;
719 + fidx->ei_leaf = oldblock;
721 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
722 + i, newblock, border, oldblock);
727 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
728 + EXT_MAX_INDEX(path[i].p_hdr));
729 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
730 + EXT_LAST_INDEX(path[i].p_hdr));
731 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
732 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
733 + i, path[i].p_idx->ei_block,
734 + path[i].p_idx->ei_leaf, newblock);
735 + memmove(++fidx, path[i].p_idx++,
736 + sizeof(struct ext3_extent_idx));
738 + EXT_ASSERT(neh->eh_entries <= neh->eh_max);
741 + set_buffer_uptodate(bh);
744 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
749 + /* correct old index */
751 + err = ext3_ext_get_access(handle, tree, path + i);
754 + path[i].p_hdr->eh_entries -= m;
755 + err = ext3_ext_dirty(handle, tree, path + i);
763 + /* insert new index */
765 + err = ext3_ext_insert_index(handle, tree, path + at,
770 + if (buffer_locked(bh))
776 + /* free all allocated blocks in error case */
777 + for (i = 0; i < depth; i++) {
780 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
789 + * routine implements tree growing procedure:
790 + * - allocates new block
791 + * - moves top-level data (index block or leaf) into the new block
792 + * - initialize new top-level, creating index that points to the
793 + * just created block
795 +static int ext3_ext_grow_indepth(handle_t *handle,
796 + struct ext3_extents_tree *tree,
797 + struct ext3_ext_path *path,
798 + struct ext3_extent *newext)
800 + struct ext3_ext_path *curp = path;
801 + struct ext3_extent_header *neh;
802 + struct ext3_extent_idx *fidx;
803 + struct buffer_head *bh;
804 + unsigned long newblock;
807 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
811 + bh = sb_getblk(tree->inode->i_sb, newblock);
814 + ext3_std_error(tree->inode->i_sb, err);
819 + if ((err = ext3_journal_get_create_access(handle, bh))) {
824 + /* move top-level index/leaf into new block */
825 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
827 + /* set size of new block */
828 + neh = EXT_BLOCK_HDR(bh);
829 + /* old root could have indexes or leaves
830 + * so calculate eh_max right way */
831 + if (EXT_DEPTH(tree))
832 + neh->eh_max = ext3_ext_space_block_idx(tree);
834 + neh->eh_max = ext3_ext_space_block(tree);
835 + neh->eh_magic = EXT3_EXT_MAGIC;
836 + set_buffer_uptodate(bh);
839 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
842 + /* create index in new top-level index: num,max,pointer */
843 + if ((err = ext3_ext_get_access(handle, tree, curp)))
846 + curp->p_hdr->eh_magic = EXT3_EXT_MAGIC;
847 + curp->p_hdr->eh_max = ext3_ext_space_root_idx(tree);
848 + curp->p_hdr->eh_entries = 1;
849 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
850 + /* FIXME: it works, but actually path[0] can be index */
851 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
852 + curp->p_idx->ei_leaf = newblock;
854 + neh = EXT_ROOT_HDR(tree);
855 + fidx = EXT_FIRST_INDEX(neh);
856 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
857 + neh->eh_entries, neh->eh_max, fidx->ei_block, fidx->ei_leaf);
859 + neh->eh_depth = path->p_depth + 1;
860 + err = ext3_ext_dirty(handle, tree, curp);
868 + * routine finds empty index and adds new leaf. if no free index found
869 + * then it requests in-depth growing
871 +static int ext3_ext_create_new_leaf(handle_t *handle,
872 + struct ext3_extents_tree *tree,
873 + struct ext3_ext_path *path,
874 + struct ext3_extent *newext)
876 + struct ext3_ext_path *curp;
877 + int depth, i, err = 0;
880 + i = depth = EXT_DEPTH(tree);
882 + /* walk up to the tree and look for free index entry */
883 + curp = path + depth;
884 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
889 + /* we use already allocated block for index block
890 + * so, subsequent data blocks should be contigoues */
891 + if (EXT_HAS_FREE_INDEX(curp)) {
892 + /* if we found index with free entry, then use that
893 + * entry: create all needed subtree and add new leaf */
894 + err = ext3_ext_split(handle, tree, path, newext, i);
897 + ext3_ext_drop_refs(path);
898 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
900 + err = PTR_ERR(path);
902 + /* tree is full, time to grow in depth */
903 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
906 + ext3_ext_drop_refs(path);
907 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
909 + err = PTR_ERR(path);
912 + * only first (depth 0 -> 1) produces free space
913 + * in all other cases we have to split growed tree
915 + depth = EXT_DEPTH(tree);
916 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
917 + /* now we need split */
929 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
930 + * NOTE: it consider block number from index entry as
931 + * allocated block. thus, index entries have to be consistent
934 +static unsigned long
935 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
939 + EXT_ASSERT(path != NULL);
940 + depth = path->p_depth;
942 + if (depth == 0 && path->p_ext == NULL)
943 + return EXT_MAX_BLOCK;
945 + /* FIXME: what if index isn't full ?! */
946 + while (depth >= 0) {
947 + if (depth == path->p_depth) {
949 + if (path[depth].p_ext !=
950 + EXT_LAST_EXTENT(path[depth].p_hdr))
951 + return path[depth].p_ext[1].ee_block;
954 + if (path[depth].p_idx !=
955 + EXT_LAST_INDEX(path[depth].p_hdr))
956 + return path[depth].p_idx[1].ei_block;
961 + return EXT_MAX_BLOCK;
965 + * returns first allocated block from next leaf or EXT_MAX_BLOCK
967 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
968 + struct ext3_ext_path *path)
972 + EXT_ASSERT(path != NULL);
973 + depth = path->p_depth;
975 + /* zero-tree has no leaf blocks at all */
977 + return EXT_MAX_BLOCK;
979 + /* go to index block */
982 + while (depth >= 0) {
983 + if (path[depth].p_idx !=
984 + EXT_LAST_INDEX(path[depth].p_hdr))
985 + return path[depth].p_idx[1].ei_block;
989 + return EXT_MAX_BLOCK;
993 + * if leaf gets modified and modified extent is first in the leaf
994 + * then we have to correct all indexes above
995 + * TODO: do we need to correct tree in all cases?
997 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
998 + struct ext3_ext_path *path)
1000 + struct ext3_extent_header *eh;
1001 + int depth = EXT_DEPTH(tree);
1002 + struct ext3_extent *ex;
1003 + unsigned long border;
1006 + eh = path[depth].p_hdr;
1007 + ex = path[depth].p_ext;
1012 + /* there is no tree at all */
1016 + if (ex != EXT_FIRST_EXTENT(eh)) {
1017 + /* we correct tree if first leaf got modified only */
1022 + * TODO: we need correction if border is smaller then current one
1025 + border = path[depth].p_ext->ee_block;
1026 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1028 + path[k].p_idx->ei_block = border;
1029 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1033 + /* change all left-side indexes */
1034 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1036 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1038 + path[k].p_idx->ei_block = border;
1039 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1047 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1048 + struct ext3_extent *ex1,
1049 + struct ext3_extent *ex2)
1051 + if (ex1->ee_block + ex1->ee_len != ex2->ee_block)
1054 +#ifdef AGRESSIVE_TEST
1055 + if (ex1->ee_len >= 4)
1059 + if (!tree->ops->mergable)
1062 + return tree->ops->mergable(ex1, ex2);
1066 + * this routine tries to merge requsted extent into the existing
1067 + * extent or inserts requested extent as new one into the tree,
1068 + * creating new leaf in no-space case
1070 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1071 + struct ext3_ext_path *path,
1072 + struct ext3_extent *newext)
1074 + struct ext3_extent_header * eh;
1075 + struct ext3_extent *ex, *fex;
1076 + struct ext3_extent *nearex; /* nearest extent */
1077 + struct ext3_ext_path *npath = NULL;
1078 + int depth, len, err, next;
1080 + EXT_ASSERT(newext->ee_len > 0);
1081 + depth = EXT_DEPTH(tree);
1082 + ex = path[depth].p_ext;
1083 + EXT_ASSERT(path[depth].p_hdr);
1085 + /* try to insert block into found extent and return */
1086 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1087 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1088 + newext->ee_len, ex->ee_block, ex->ee_len,
1090 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1092 + ex->ee_len += newext->ee_len;
1093 + eh = path[depth].p_hdr;
1099 + depth = EXT_DEPTH(tree);
1100 + eh = path[depth].p_hdr;
1101 + if (eh->eh_entries < eh->eh_max)
1104 + /* probably next leaf has space for us? */
1105 + fex = EXT_LAST_EXTENT(eh);
1106 + next = ext3_ext_next_leaf_block(tree, path);
1107 + if (newext->ee_block > fex->ee_block && next != EXT_MAX_BLOCK) {
1108 + ext_debug(tree, "next leaf block - %d\n", next);
1109 + EXT_ASSERT(!npath);
1110 + npath = ext3_ext_find_extent(tree, next, NULL);
1111 + if (IS_ERR(npath))
1112 + return PTR_ERR(npath);
1113 + EXT_ASSERT(npath->p_depth == path->p_depth);
1114 + eh = npath[depth].p_hdr;
1115 + if (eh->eh_entries < eh->eh_max) {
1116 + ext_debug(tree, "next leaf isnt full(%d)\n",
1121 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1122 + eh->eh_entries, eh->eh_max);
1126 + * there is no free space in found leaf
1127 + * we're gonna add new leaf in the tree
1129 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1132 + depth = EXT_DEPTH(tree);
1133 + eh = path[depth].p_hdr;
1136 + nearex = path[depth].p_ext;
1138 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1142 + /* there is no extent in this leaf, create first one */
1143 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1144 + newext->ee_block, newext->ee_start,
1146 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1147 + } else if (newext->ee_block > nearex->ee_block) {
1148 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1149 + if (nearex != EXT_LAST_EXTENT(eh)) {
1150 + len = EXT_MAX_EXTENT(eh) - nearex;
1151 + len = (len - 1) * sizeof(struct ext3_extent);
1152 + len = len < 0 ? 0 : len;
1153 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1154 + "move %d from 0x%p to 0x%p\n",
1155 + newext->ee_block, newext->ee_start,
1157 + nearex, len, nearex + 1, nearex + 2);
1158 + memmove(nearex + 2, nearex + 1, len);
1160 + path[depth].p_ext = nearex + 1;
1162 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1163 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1164 + len = len < 0 ? 0 : len;
1165 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1166 + "move %d from 0x%p to 0x%p\n",
1167 + newext->ee_block, newext->ee_start, newext->ee_len,
1168 + nearex, len, nearex + 1, nearex + 2);
1169 + memmove(nearex + 1, nearex, len);
1170 + path[depth].p_ext = nearex;
1174 + nearex = path[depth].p_ext;
1175 + nearex->ee_block = newext->ee_block;
1176 + nearex->ee_start = newext->ee_start;
1177 + nearex->ee_len = newext->ee_len;
1178 + /* FIXME: support for large fs */
1179 + nearex->ee_start_hi = 0;
1182 + /* try to merge extents to the right */
1183 + while (nearex < EXT_LAST_EXTENT(eh)) {
1184 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1186 + /* merge with next extent! */
1187 + nearex->ee_len += nearex[1].ee_len;
1188 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1189 + len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1190 + sizeof(struct ext3_extent);
1191 + memmove(nearex + 1, nearex + 2, len);
1194 + EXT_ASSERT(eh->eh_entries > 0);
1197 + /* try to merge extents to the left */
1199 + /* time to correct all indexes above */
1200 + err = ext3_ext_correct_indexes(handle, tree, path);
1204 + err = ext3_ext_dirty(handle, tree, path + depth);
1208 + ext3_ext_drop_refs(npath);
1211 + ext3_ext_tree_changed(tree);
1212 + ext3_ext_invalidate_cache(tree);
1216 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1217 + unsigned long num, ext_prepare_callback func)
1219 + struct ext3_ext_path *path = NULL;
1220 + struct ext3_ext_cache cbex;
1221 + struct ext3_extent *ex;
1222 + unsigned long next, start = 0, end = 0;
1223 + unsigned long last = block + num;
1224 + int depth, exists, err = 0;
1228 + EXT_ASSERT(tree->inode);
1229 + EXT_ASSERT(tree->root);
1231 + while (block < last && block != EXT_MAX_BLOCK) {
1232 + num = last - block;
1233 + /* find extent for this block */
1234 + path = ext3_ext_find_extent(tree, block, path);
1235 + if (IS_ERR(path)) {
1236 + err = PTR_ERR(path);
1241 + depth = EXT_DEPTH(tree);
1242 + EXT_ASSERT(path[depth].p_hdr);
1243 + ex = path[depth].p_ext;
1244 + next = ext3_ext_next_allocated_block(path);
1248 + /* there is no extent yet, so try to allocate
1249 + * all requested space */
1251 + end = block + num;
1252 + } else if (ex->ee_block > block) {
1253 + /* need to allocate space before found extent */
1255 + end = ex->ee_block;
1256 + if (block + num < end)
1257 + end = block + num;
1258 + } else if (block >= ex->ee_block + ex->ee_len) {
1259 + /* need to allocate space after found extent */
1261 + end = block + num;
1264 + } else if (block >= ex->ee_block) {
1266 + * some part of requested space is covered
1270 + end = ex->ee_block + ex->ee_len;
1271 + if (block + num < end)
1272 + end = block + num;
1277 + EXT_ASSERT(end > start);
1280 + cbex.ec_block = start;
1281 + cbex.ec_len = end - start;
1282 + cbex.ec_start = 0;
1283 + cbex.ec_type = EXT3_EXT_CACHE_GAP;
1285 + cbex.ec_block = ex->ee_block;
1286 + cbex.ec_len = ex->ee_len;
1287 + cbex.ec_start = ex->ee_start;
1288 + cbex.ec_type = EXT3_EXT_CACHE_EXTENT;
1291 + EXT_ASSERT(cbex.ec_len > 0);
1292 + EXT_ASSERT(path[depth].p_hdr);
1293 + err = func(tree, path, &cbex);
1294 + ext3_ext_drop_refs(path);
1298 + if (err == EXT_REPEAT)
1300 + else if (err == EXT_BREAK) {
1305 + if (EXT_DEPTH(tree) != depth) {
1306 + /* depth was changed. we have to realloc path */
1311 + block = cbex.ec_block + cbex.ec_len;
1315 + ext3_ext_drop_refs(path);
1323 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, __u32 block,
1324 + __u32 len, __u32 start, int type)
1326 + EXT_ASSERT(len > 0);
1328 + tree->cex->ec_type = type;
1329 + tree->cex->ec_block = block;
1330 + tree->cex->ec_len = len;
1331 + tree->cex->ec_start = start;
1336 + * this routine calculate boundaries of the gap requested block fits into
1337 + * and cache this gap
1340 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1341 + struct ext3_ext_path *path,
1342 + unsigned long block)
1344 + int depth = EXT_DEPTH(tree);
1345 + unsigned long lblock, len;
1346 + struct ext3_extent *ex;
1351 + ex = path[depth].p_ext;
1353 + /* there is no extent yet, so gap is [0;-] */
1355 + len = EXT_MAX_BLOCK;
1356 + ext_debug(tree, "cache gap(whole file):");
1357 + } else if (block < ex->ee_block) {
1359 + len = ex->ee_block - block;
1360 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1361 + (unsigned long) block,
1362 + (unsigned long) ex->ee_block,
1363 + (unsigned long) ex->ee_len);
1364 + } else if (block >= ex->ee_block + ex->ee_len) {
1365 + lblock = ex->ee_block + ex->ee_len;
1366 + len = ext3_ext_next_allocated_block(path);
1367 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1368 + (unsigned long) ex->ee_block,
1369 + (unsigned long) ex->ee_len,
1370 + (unsigned long) block);
1371 + EXT_ASSERT(len > lblock);
1372 + len = len - lblock;
1378 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) lblock, len);
1379 + ext3_ext_put_in_cache(tree, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1383 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1384 + struct ext3_extent *ex)
1386 + struct ext3_ext_cache *cex = tree->cex;
1388 + /* is there cache storage at all? */
1390 + return EXT3_EXT_CACHE_NO;
1392 + /* has cache valid data? */
1393 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1394 + return EXT3_EXT_CACHE_NO;
1396 + EXT_ASSERT(cex->ec_type == EXT3_EXT_CACHE_GAP ||
1397 + cex->ec_type == EXT3_EXT_CACHE_EXTENT);
1398 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1399 + ex->ee_block = cex->ec_block;
1400 + ex->ee_start = cex->ec_start;
1401 + ex->ee_len = cex->ec_len;
1402 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1403 + (unsigned long) block,
1404 + (unsigned long) ex->ee_block,
1405 + (unsigned long) ex->ee_len,
1406 + (unsigned long) ex->ee_start);
1407 + return cex->ec_type;
1410 + /* not in cache */
1411 + return EXT3_EXT_CACHE_NO;
1415 + * routine removes index from the index block
1416 + * it's used in truncate case only. thus all requests are for
1417 + * last index in the block only
1419 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1420 + struct ext3_ext_path *path)
1422 + struct buffer_head *bh;
1425 + /* free index block */
1427 + EXT_ASSERT(path->p_hdr->eh_entries);
1428 + if ((err = ext3_ext_get_access(handle, tree, path)))
1430 + path->p_hdr->eh_entries--;
1431 + if ((err = ext3_ext_dirty(handle, tree, path)))
1433 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1434 + path->p_idx->ei_leaf);
1435 + bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
1436 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1437 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1441 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1442 + struct ext3_ext_path *path)
1444 + int depth = EXT_DEPTH(tree);
1448 + /* probably there is space in leaf? */
1449 + if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1454 + * the worste case we're expecting is creation of the
1455 + * new root (growing in depth) with index splitting
1456 + * for splitting we have to consider depth + 1 because
1457 + * previous growing could increase it
1459 + depth = depth + 1;
1462 + * growing in depth:
1463 + * block allocation + new root + old root
1465 + needed = EXT3_ALLOC_NEEDED + 2;
1467 + /* index split. we may need:
1468 + * allocate intermediate indexes and new leaf
1469 + * change two blocks at each level, but root
1470 + * modify root block (inode)
1472 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1478 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1479 + struct ext3_ext_path *path, unsigned long start,
1480 + unsigned long end)
1482 + struct ext3_extent *ex, tex;
1483 + struct ext3_ext_path *npath;
1484 + int depth, creds, err;
1486 + depth = EXT_DEPTH(tree);
1487 + ex = path[depth].p_ext;
1489 + EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1490 + EXT_ASSERT(ex->ee_block < start);
1492 + /* calculate tail extent */
1493 + tex.ee_block = end + 1;
1494 + EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1495 + tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1497 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1498 + handle = ext3_ext_journal_restart(handle, creds);
1499 + if (IS_ERR(handle))
1500 + return PTR_ERR(handle);
1502 + /* calculate head extent. use primary extent */
1503 + err = ext3_ext_get_access(handle, tree, path + depth);
1506 + ex->ee_len = start - ex->ee_block;
1507 + err = ext3_ext_dirty(handle, tree, path + depth);
1511 + /* FIXME: some callback to free underlying resource
1512 + * and correct ee_start? */
1513 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1514 + ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1516 + npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1517 + if (IS_ERR(npath))
1518 + return PTR_ERR(npath);
1519 + depth = EXT_DEPTH(tree);
1520 + EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1521 + EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1523 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1524 + ext3_ext_drop_refs(npath);
1531 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1532 + struct ext3_ext_path *path, unsigned long start,
1533 + unsigned long end)
1535 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1536 + int err = 0, correct_index = 0;
1537 + int depth = EXT_DEPTH(tree), credits;
1538 + struct ext3_extent_header *eh;
1539 + unsigned a, b, block, num;
1541 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1542 + if (!path[depth].p_hdr)
1543 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1544 + eh = path[depth].p_hdr;
1546 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1547 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1549 + /* find where to start removing */
1550 + le = ex = EXT_LAST_EXTENT(eh);
1551 + while (ex != EXT_FIRST_EXTENT(eh)) {
1552 + if (ex->ee_block <= end)
1557 + if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1558 + /* removal of internal part of the extent requested
1559 + * tail and head must be placed in different extent
1560 + * so, we have to insert one more extent */
1561 + path[depth].p_ext = ex;
1562 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1566 + while (ex >= EXT_FIRST_EXTENT(eh) && ex->ee_block + ex->ee_len > start) {
1567 + ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1568 + path[depth].p_ext = ex;
1570 + a = ex->ee_block > start ? ex->ee_block : start;
1571 + b = ex->ee_block + ex->ee_len - 1 < end ?
1572 + ex->ee_block + ex->ee_len - 1 : end;
1574 + ext_debug(tree, " border %u:%u\n", a, b);
1576 + if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1580 + } else if (a != ex->ee_block) {
1581 + /* remove tail of the extent */
1582 + block = ex->ee_block;
1584 + } else if (b != ex->ee_block + ex->ee_len - 1) {
1585 + /* remove head of the extent */
1589 + /* remove whole extent: excelent! */
1590 + block = ex->ee_block;
1592 + EXT_ASSERT(a == ex->ee_block &&
1593 + b == ex->ee_block + ex->ee_len - 1);
1596 + if (ex == EXT_FIRST_EXTENT(eh))
1597 + correct_index = 1;
1600 + if (correct_index)
1601 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1602 + if (tree->ops->remove_extent_credits)
1603 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1605 + handle = ext3_ext_journal_restart(handle, credits);
1606 + if (IS_ERR(handle)) {
1607 + err = PTR_ERR(handle);
1611 + err = ext3_ext_get_access(handle, tree, path + depth);
1615 + if (tree->ops->remove_extent)
1616 + err = tree->ops->remove_extent(tree, ex, a, b);
1621 + /* this extent is removed entirely mark slot unused */
1627 + ex->ee_block = block;
1630 + err = ext3_ext_dirty(handle, tree, path + depth);
1634 + ext_debug(tree, "new extent: %u:%u:%u\n",
1635 + ex->ee_block, ex->ee_len, ex->ee_start);
1640 + /* reuse unused slots */
1642 + if (lu->ee_start) {
1651 + if (correct_index && eh->eh_entries)
1652 + err = ext3_ext_correct_indexes(handle, tree, path);
1654 + /* if this leaf is free, then we should
1655 + * remove it from index block above */
1656 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1657 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1664 +static struct ext3_extent_idx *
1665 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1667 + struct ext3_extent_idx *ix;
1669 + ix = EXT_LAST_INDEX(hdr);
1670 + while (ix != EXT_FIRST_INDEX(hdr)) {
1671 + if (ix->ei_block <= block)
1679 + * returns 1 if current index have to be freed (even partial)
1682 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1684 + EXT_ASSERT(path->p_idx);
1686 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1690 + * if truncate on deeper level happened it it wasn't partial
1691 + * so we have to consider current index for truncation
1693 + if (path->p_hdr->eh_entries == path->p_block)
1698 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1699 + unsigned long start, unsigned long end)
1701 + struct inode *inode = tree->inode;
1702 + struct super_block *sb = inode->i_sb;
1703 + int depth = EXT_DEPTH(tree);
1704 + struct ext3_ext_path *path;
1706 + int i = 0, err = 0;
1708 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1710 + /* probably first extent we're gonna free will be last in block */
1711 + handle = ext3_journal_start(inode, depth + 1);
1712 + if (IS_ERR(handle))
1713 + return PTR_ERR(handle);
1715 + ext3_ext_invalidate_cache(tree);
1718 + * we start scanning from right side freeing all the blocks
1719 + * after i_size and walking into the deep
1721 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1722 + if (IS_ERR(path)) {
1723 + ext3_error(sb, __FUNCTION__, "Can't allocate path array");
1724 + ext3_journal_stop(handle);
1727 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1728 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1730 + while (i >= 0 && err == 0) {
1732 + /* this is leaf block */
1733 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1734 + /* root level have p_bh == NULL, brelse() eats this */
1735 + brelse(path[i].p_bh);
1740 + /* this is index block */
1741 + if (!path[i].p_hdr) {
1742 + ext_debug(tree, "initialize header\n");
1743 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1746 + EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1747 + EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1749 + if (!path[i].p_idx) {
1750 + /* this level hasn't touched yet */
1752 + ext3_ext_last_covered(path[i].p_hdr, end);
1753 + path[i].p_block = path[i].p_hdr->eh_entries + 1;
1754 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1755 + path[i].p_hdr, path[i].p_hdr->eh_entries);
1757 + /* we've already was here, see at next index */
1761 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1762 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1764 + if (ext3_ext_more_to_rm(path + i)) {
1765 + /* go to the next level */
1766 + ext_debug(tree, "move to level %d (block %d)\n",
1767 + i + 1, path[i].p_idx->ei_leaf);
1768 + memset(path + i + 1, 0, sizeof(*path));
1769 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1770 + if (!path[i+1].p_bh) {
1771 + /* should we reset i_size? */
1775 + /* put actual number of indexes to know is this
1776 + * number got changed at the next iteration */
1777 + path[i].p_block = path[i].p_hdr->eh_entries;
1780 + /* we finish processing this index, go up */
1781 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1782 + /* index is empty, remove it
1783 + * handle must be already prepared by the
1784 + * truncatei_leaf() */
1785 + err = ext3_ext_rm_idx(handle, tree, path + i);
1787 + /* root level have p_bh == NULL, brelse() eats this */
1788 + brelse(path[i].p_bh);
1790 + ext_debug(tree, "return to level %d\n", i);
1794 + /* TODO: flexible tree reduction should be here */
1795 + if (path->p_hdr->eh_entries == 0) {
1797 + * truncate to zero freed all the tree
1798 + * so, we need to correct eh_depth
1800 + err = ext3_ext_get_access(handle, tree, path);
1802 + EXT_ROOT_HDR(tree)->eh_depth = 0;
1803 + EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1804 + err = ext3_ext_dirty(handle, tree, path);
1807 + ext3_ext_tree_changed(tree);
1810 + ext3_journal_stop(handle);
1815 +int ext3_ext_calc_metadata_amount(struct ext3_extents_tree *tree, int blocks)
1817 + int lcap, icap, rcap, leafs, idxs, num;
1819 + rcap = ext3_ext_space_root(tree);
1820 + if (blocks <= rcap) {
1821 + /* all extents fit to the root */
1825 + rcap = ext3_ext_space_root_idx(tree);
1826 + lcap = ext3_ext_space_block(tree);
1827 + icap = ext3_ext_space_block_idx(tree);
1829 + num = leafs = (blocks + lcap - 1) / lcap;
1830 + if (leafs <= rcap) {
1831 + /* all pointers to leafs fit to the root */
1835 + /* ok. we need separate index block(s) to link all leaf blocks */
1836 + idxs = (leafs + icap - 1) / icap;
1839 + idxs = (idxs + icap - 1) / icap;
1840 + } while (idxs > rcap);
1846 + * called at mount time
1848 +void ext3_ext_init(struct super_block *sb)
1851 + * possible initialization would be here
1854 + if (test_opt(sb, EXTENTS)) {
1855 + printk("EXT3-fs: file extents enabled");
1856 +#ifdef AGRESSIVE_TEST
1857 + printk(", agressive tests");
1859 +#ifdef CHECK_BINSEARCH
1860 + printk(", check binsearch");
1867 + * called at umount time
1869 +void ext3_ext_release(struct super_block *sb)
1873 +/************************************************************************
1874 + * VFS related routines
1875 + ************************************************************************/
1877 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1879 + /* we use in-core data, not bh */
1883 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1885 + struct inode *inode = buffer;
1886 + return ext3_mark_inode_dirty(handle, inode);
1889 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1890 + struct ext3_extent *ex2)
1892 + /* FIXME: support for large fs */
1893 + if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1899 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1900 + struct ext3_extent *ex,
1901 + unsigned long from, unsigned long to)
1905 + /* at present, extent can't cross block group */;
1906 + needed = 4; /* bitmap + group desc + sb + inode */
1908 +#ifdef CONFIG_QUOTA
1909 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1915 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1916 + struct ext3_extent *ex,
1917 + unsigned long from, unsigned long to)
1919 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1920 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1921 + struct buffer_head *bh;
1924 + if (IS_ERR(handle))
1925 + return PTR_ERR(handle);
1926 + if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1927 + /* tail removal */
1928 + unsigned long num, start;
1929 + num = ex->ee_block + ex->ee_len - from;
1930 + start = ex->ee_start + ex->ee_len - num;
1931 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1933 + for (i = 0; i < num; i++) {
1934 + bh = sb_find_get_block(tree->inode->i_sb, start + i);
1935 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1937 + ext3_free_blocks(handle, tree->inode, start, num);
1938 + } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1939 + printk("strange request: removal %lu-%lu from %u:%u\n",
1940 + from, to, ex->ee_block, ex->ee_len);
1942 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1943 + from, to, ex->ee_block, ex->ee_len);
1945 + ext3_journal_stop(handle);
1949 +static int ext3_ext_find_goal(struct inode *inode,
1950 + struct ext3_ext_path *path, unsigned long block)
1952 + struct ext3_inode_info *ei = EXT3_I(inode);
1953 + unsigned long bg_start;
1954 + unsigned long colour;
1958 + struct ext3_extent *ex;
1959 + depth = path->p_depth;
1961 + /* try to predict block placement */
1962 + if ((ex = path[depth].p_ext))
1963 + return ex->ee_start + (block - ex->ee_block);
1965 + /* it looks index is empty
1966 + * try to find starting from index itself */
1967 + if (path[depth].p_bh)
1968 + return path[depth].p_bh->b_blocknr;
1971 + /* OK. use inode's group */
1972 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1973 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1974 + colour = (current->pid % 16) *
1975 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1976 + return bg_start + colour + block;
1979 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1980 + struct ext3_ext_path *path,
1981 + struct ext3_extent *ex, int *err)
1983 + struct inode *inode = tree->inode;
1984 + int newblock, goal;
1988 + EXT_ASSERT(ex->ee_start);
1989 + EXT_ASSERT(ex->ee_len);
1991 + /* reuse block from the extent to order data/metadata */
1992 + newblock = ex->ee_start++;
1994 + if (ex->ee_len == 0) {
1996 + /* allocate new block for the extent */
1997 + goal = ext3_ext_find_goal(inode, path, ex->ee_block);
1998 + ex->ee_start = ext3_new_block(handle, inode, goal, err);
1999 + if (ex->ee_start == 0) {
2000 + /* error occured: restore old extent */
2001 + ex->ee_start = newblock;
2008 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
2009 + .get_write_access = ext3_get_inode_write_access,
2010 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
2011 + .mergable = ext3_ext_mergable,
2012 + .new_block = ext3_new_block_cb,
2013 + .remove_extent = ext3_remove_blocks,
2014 + .remove_extent_credits = ext3_remove_blocks_credits,
2017 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
2018 + struct inode *inode)
2020 + tree->inode = inode;
2021 + tree->root = (void *) EXT3_I(inode)->i_data;
2022 + tree->buffer = (void *) inode;
2023 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
2024 + tree->cex = &EXT3_I(inode)->i_cached_extent;
2025 + tree->ops = &ext3_blockmap_helpers;
2028 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2029 + long iblock, struct buffer_head *bh_result,
2030 + int create, int extend_disksize)
2032 + struct ext3_ext_path *path = NULL;
2033 + struct ext3_extent newex;
2034 + struct ext3_extent *ex;
2035 + int goal, newblock, err = 0, depth;
2036 + struct ext3_extents_tree tree;
2038 + __clear_bit(BH_New, &bh_result->b_state);
2039 + ext3_init_tree_desc(&tree, inode);
2040 + ext_debug(&tree, "block %d requested for inode %u\n",
2041 + (int) iblock, (unsigned) inode->i_ino);
2042 + down(&EXT3_I(inode)->truncate_sem);
2044 + /* check in cache */
2045 + if ((goal = ext3_ext_in_cache(&tree, iblock, &newex))) {
2046 + if (goal == EXT3_EXT_CACHE_GAP) {
2048 + /* block isn't allocated yet and
2049 + * user don't want to allocate it */
2052 + /* we should allocate requested block */
2053 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2054 + /* block is already allocated */
2055 + newblock = iblock - newex.ee_block + newex.ee_start;
2062 + /* find extent for this block */
2063 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2064 + if (IS_ERR(path)) {
2065 + err = PTR_ERR(path);
2070 + depth = EXT_DEPTH(&tree);
2073 + * consistent leaf must not be empty
2074 + * this situations is possible, though, _during_ tree modification
2075 + * this is why assert can't be put in ext3_ext_find_extent()
2077 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2079 + if ((ex = path[depth].p_ext)) {
2080 + /* if found exent covers block, simple return it */
2081 + if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2082 + newblock = iblock - ex->ee_block + ex->ee_start;
2083 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2084 + (int) iblock, ex->ee_block, ex->ee_len,
2086 + ext3_ext_put_in_cache(&tree, ex->ee_block,
2087 + ex->ee_len, ex->ee_start,
2088 + EXT3_EXT_CACHE_EXTENT);
2094 + * requested block isn't allocated yet
2095 + * we couldn't try to create block if create flag is zero
2098 + /* put just found gap into cache to speedup subsequest reqs */
2099 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2103 + /* allocate new block */
2104 + goal = ext3_ext_find_goal(inode, path, iblock);
2105 + newblock = ext3_new_block(handle, inode, goal, &err);
2108 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2111 + /* try to insert new extent into found leaf and return */
2112 + newex.ee_block = iblock;
2113 + newex.ee_start = newblock;
2115 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2119 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2120 + EXT3_I(inode)->i_disksize = inode->i_size;
2122 + /* previous routine could use block we allocated */
2123 + newblock = newex.ee_start;
2124 + __set_bit(BH_New, &bh_result->b_state);
2126 + ext3_ext_put_in_cache(&tree, newex.ee_block, newex.ee_len,
2127 + newex.ee_start, EXT3_EXT_CACHE_EXTENT);
2129 + ext3_ext_show_leaf(&tree, path);
2130 + __set_bit(BH_Mapped, &bh_result->b_state);
2131 + bh_result->b_bdev = inode->i_sb->s_bdev;
2132 + bh_result->b_blocknr = newblock;
2135 + ext3_ext_drop_refs(path);
2138 + up(&EXT3_I(inode)->truncate_sem);
2143 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2145 + struct address_space *mapping = inode->i_mapping;
2146 + struct super_block *sb = inode->i_sb;
2147 + struct ext3_extents_tree tree;
2148 + unsigned long last_block;
2152 + ext3_init_tree_desc(&tree, inode);
2155 + * probably first extent we're gonna free will be last in block
2157 + err = ext3_writepage_trans_blocks(inode) + 3;
2158 + handle = ext3_journal_start(inode, err);
2159 + if (IS_ERR(handle)) {
2161 + clear_highpage(page);
2162 + flush_dcache_page(page);
2163 + unlock_page(page);
2164 + page_cache_release(page);
2170 + ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2172 + down(&EXT3_I(inode)->truncate_sem);
2173 + ext3_ext_invalidate_cache(&tree);
2176 + * TODO: optimization is possible here
2177 + * probably we need not scaning at all,
2178 + * because page truncation is enough
2180 + if (ext3_orphan_add(handle, inode))
2183 + /* we have to know where to truncate from in crash case */
2184 + EXT3_I(inode)->i_disksize = inode->i_size;
2185 + ext3_mark_inode_dirty(handle, inode);
2187 + last_block = (inode->i_size + sb->s_blocksize - 1) >>
2188 + EXT3_BLOCK_SIZE_BITS(sb);
2189 + err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2191 + /* In a multi-transaction truncate, we only make the final
2192 + * transaction synchronous */
2193 + if (IS_SYNC(inode))
2194 + handle->h_sync = 1;
2198 + * If this was a simple ftruncate(), and the file will remain alive
2199 + * then we need to clear up the orphan record which we created above.
2200 + * However, if this was a real unlink then we were called by
2201 + * ext3_delete_inode(), and we allow that function to clean up the
2202 + * orphan info for us.
2204 + if (inode->i_nlink)
2205 + ext3_orphan_del(handle, inode);
2207 + up(&EXT3_I(inode)->truncate_sem);
2208 + ext3_journal_stop(handle);
2212 + * this routine calculate max number of blocks we could modify
2213 + * in order to allocate new block for an inode
2215 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2217 + struct ext3_extents_tree tree;
2220 + ext3_init_tree_desc(&tree, inode);
2222 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2224 + /* caller want to allocate num blocks */
2227 +#ifdef CONFIG_QUOTA
2229 + * FIXME: real calculation should be here
2230 + * it depends on blockmap format of qouta file
2232 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2238 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2240 + struct ext3_extents_tree tree;
2242 + ext3_init_tree_desc(&tree, inode);
2243 + ext3_extent_tree_init(handle, &tree);
2246 +int ext3_ext_calc_blockmap_metadata(struct inode *inode, int blocks)
2248 + struct ext3_extents_tree tree;
2250 + ext3_init_tree_desc(&tree, inode);
2251 + return ext3_ext_calc_metadata_amount(&tree, blocks);
2255 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2256 + struct ext3_ext_path *path,
2257 + struct ext3_ext_cache *newex)
2259 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2261 + if (newex->ec_type != EXT3_EXT_CACHE_EXTENT)
2262 + return EXT_CONTINUE;
2266 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2269 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2271 + buf->cur += sizeof(*newex);
2273 + buf->err = -EFAULT;
2276 + return EXT_CONTINUE;
2280 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2281 + struct ext3_ext_path *path,
2282 + struct ext3_ext_cache *ex)
2284 + struct ext3_extent_tree_stats *buf =
2285 + (struct ext3_extent_tree_stats *) tree->private;
2288 + if (ex->ec_type != EXT3_EXT_CACHE_EXTENT)
2289 + return EXT_CONTINUE;
2291 + depth = EXT_DEPTH(tree);
2292 + buf->extents_num++;
2293 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2295 + return EXT_CONTINUE;
2298 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2299 + unsigned long arg)
2303 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2306 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2307 + struct ext3_extent_buf buf;
2308 + struct ext3_extents_tree tree;
2310 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2313 + ext3_init_tree_desc(&tree, inode);
2314 + buf.cur = buf.buffer;
2316 + tree.private = &buf;
2317 + down(&EXT3_I(inode)->truncate_sem);
2318 + err = ext3_ext_walk_space(&tree, buf.start, EXT_MAX_BLOCK,
2319 + ext3_ext_store_extent_cb);
2320 + up(&EXT3_I(inode)->truncate_sem);
2323 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2324 + struct ext3_extent_tree_stats buf;
2325 + struct ext3_extents_tree tree;
2327 + ext3_init_tree_desc(&tree, inode);
2328 + down(&EXT3_I(inode)->truncate_sem);
2329 + buf.depth = EXT_DEPTH(&tree);
2330 + buf.extents_num = 0;
2332 + tree.private = &buf;
2333 + err = ext3_ext_walk_space(&tree, 0, EXT_MAX_BLOCK,
2334 + ext3_ext_collect_stats_cb);
2335 + up(&EXT3_I(inode)->truncate_sem);
2337 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2338 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2339 + struct ext3_extents_tree tree;
2340 + ext3_init_tree_desc(&tree, inode);
2341 + down(&EXT3_I(inode)->truncate_sem);
2342 + err = EXT_DEPTH(&tree);
2343 + up(&EXT3_I(inode)->truncate_sem);
2349 +EXPORT_SYMBOL(ext3_init_tree_desc);
2350 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2351 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2352 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2353 +EXPORT_SYMBOL(ext3_ext_walk_space);
2354 +EXPORT_SYMBOL(ext3_ext_find_goal);
2355 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2356 Index: linux-2.6.5-sles9/fs/ext3/ialloc.c
2357 ===================================================================
2358 --- linux-2.6.5-sles9.orig/fs/ext3/ialloc.c 2005-02-23 01:01:52.366281264 +0300
2359 +++ linux-2.6.5-sles9/fs/ext3/ialloc.c 2005-02-23 01:02:37.398435336 +0300
2360 @@ -566,7 +566,7 @@ repeat:
2361 ei->i_dir_start_lookup = 0;
2364 - ei->i_flags = EXT3_I(dir)->i_flags & ~EXT3_INDEX_FL;
2365 + ei->i_flags = EXT3_I(dir)->i_flags & ~(EXT3_INDEX_FL|EXT3_EXTENTS_FL);
2367 ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
2368 /* dirsync only applies to directories */
2369 @@ -647,6 +647,18 @@
2370 DQUOT_FREE_INODE(inode);
2373 + if (test_opt(sb, EXTENTS) && S_ISREG(inode->i_mode)) {
2374 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2375 + ext3_extents_initialize_blockmap(handle, inode);
2376 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2377 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2378 + if (err) goto fail;
2379 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2380 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2381 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2385 err = ext3_mark_inode_dirty(handle, inode);
2387 ext3_std_error(sb, err);
2388 Index: linux-2.6.5-sles9/fs/ext3/inode.c
2389 ===================================================================
2390 --- linux-2.6.5-sles9.orig/fs/ext3/inode.c 2005-02-23 01:01:52.373280200 +0300
2391 +++ linux-2.6.5-sles9/fs/ext3/inode.c 2005-02-23 01:02:37.404434424 +0300
2392 @@ -796,6 +796,17 @@
2397 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2398 + struct buffer_head *bh, int create, int extend_disksize)
2400 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2401 + return ext3_ext_get_block(handle, inode, block, bh, create,
2403 + return ext3_get_block_handle(handle, inode, block, bh, create,
2407 static int ext3_get_block(struct inode *inode, sector_t iblock,
2408 struct buffer_head *bh_result, int create)
2411 handle = ext3_journal_current_handle();
2412 J_ASSERT(handle != 0);
2414 - ret = ext3_get_block_handle(handle, inode, iblock,
2415 - bh_result, create, 1);
2416 + ret = ext3_get_block_wrap(handle, inode, iblock,
2417 + bh_result, create, 1);
2425 - ret = ext3_get_block_handle(handle, inode, iblock,
2426 - bh_result, create, 0);
2427 + ret = ext3_get_block_wrap(handle, inode, iblock,
2428 + bh_result, create, 0);
2429 bh_result->b_size = (1 << inode->i_blkbits);
2434 dummy.b_blocknr = -1000;
2435 buffer_trace_init(&dummy.b_history);
2436 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2437 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2438 if (!*errp && buffer_mapped(&dummy)) {
2439 struct buffer_head *bh;
2440 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2441 @@ -1587,7 +1598,7 @@
2442 * This required during truncate. We need to physically zero the tail end
2443 * of that block so it doesn't yield old data if the file is later grown.
2445 -static int ext3_block_truncate_page(handle_t *handle, struct page *page,
2446 +int ext3_block_truncate_page(handle_t *handle, struct page *page,
2447 struct address_space *mapping, loff_t from)
2449 unsigned long index = from >> PAGE_CACHE_SHIFT;
2450 @@ -2083,6 +2094,9 @@
2454 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2455 + return ext3_ext_truncate(inode, page);
2457 handle = start_transaction(inode);
2458 if (IS_ERR(handle)) {
2460 @@ -2789,6 +2803,9 @@
2461 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2464 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2465 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2467 if (ext3_should_journal_data(inode))
2468 ret = 3 * (bpp + indirects) + 2;
2470 Index: linux-2.6.5-sles9/fs/ext3/Makefile
2471 ===================================================================
2472 --- linux-2.6.5-sles9.orig/fs/ext3/Makefile 2005-02-23 01:01:46.501172896 +0300
2473 +++ linux-2.6.5-sles9/fs/ext3/Makefile 2005-02-23 01:02:37.405434272 +0300
2475 obj-$(CONFIG_EXT3_FS) += ext3.o
2477 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
2478 - ioctl.o namei.o super.o symlink.o hash.o resize.o iopen.o
2479 + ioctl.o namei.o super.o symlink.o hash.o resize.o iopen.o \
2482 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2483 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2484 Index: linux-2.6.5-sles9/fs/ext3/super.c
2485 ===================================================================
2486 --- linux-2.6.5-sles9.orig/fs/ext3/super.c 2005-02-23 01:02:34.072940888 +0300
2487 +++ linux-2.6.5-sles9/fs/ext3/super.c 2005-02-23 01:47:15.291333736 +0300
2489 struct ext3_super_block *es = sbi->s_es;
2492 + ext3_ext_release(sb);
2493 ext3_xattr_put_super(sb);
2494 journal_destroy(sbi->s_journal);
2495 if (!(sb->s_flags & MS_RDONLY)) {
2497 dynlock_init(&ei->i_htree_lock);
2498 sema_init(&ei->i_rename_sem, 1);
2499 sema_init(&ei->i_append_sem, 1);
2500 + memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
2501 return &ei->vfs_inode;
2504 static void ext3_destroy_inode(struct inode *inode)
2506 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_pdirops,
2507 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2508 Opt_ignore, Opt_barrier, Opt_err, Opt_resize,
2509 + Opt_extents, Opt_extdebug,
2512 static match_table_t tokens = {
2514 {Opt_iopen, "iopen"},
2515 {Opt_noiopen, "noiopen"},
2516 {Opt_iopen_nopriv, "iopen_nopriv"},
2517 + {Opt_extents, "extents"},
2518 + {Opt_extdebug, "extdebug"},
2520 {Opt_resize, "resize"},
2522 @@ -797,6 +802,12 @@
2527 + set_opt (sbi->s_mount_opt, EXTENTS);
2529 + case Opt_extdebug:
2530 + set_opt (sbi->s_mount_opt, EXTDEBUG);
2534 "EXT3-fs: Unrecognized mount option \"%s\" "
2535 @@ -1449,6 +1460,8 @@
2536 percpu_counter_mod(&sbi->s_dirs_counter,
2537 ext3_count_dirs(sb));
2539 + ext3_ext_init(sb);
2544 Index: linux-2.6.5-sles9/fs/ext3/ioctl.c
2545 ===================================================================
2546 --- linux-2.6.5-sles9.orig/fs/ext3/ioctl.c 2005-02-23 01:01:42.887722224 +0300
2547 +++ linux-2.6.5-sles9/fs/ext3/ioctl.c 2005-02-23 01:02:37.412433208 +0300
2548 @@ -124,6 +124,10 @@
2549 err = ext3_change_inode_journal_flag(inode, jflag);
2552 + case EXT3_IOC_GET_EXTENTS:
2553 + case EXT3_IOC_GET_TREE_STATS:
2554 + case EXT3_IOC_GET_TREE_DEPTH:
2555 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2556 case EXT3_IOC_GETVERSION:
2557 case EXT3_IOC_GETVERSION_OLD:
2558 return put_user(inode->i_generation, (int *) arg);
2559 Index: linux-2.6.5-sles9/include/linux/ext3_fs.h
2560 ===================================================================
2561 --- linux-2.6.5-sles9.orig/include/linux/ext3_fs.h 2005-02-23 01:02:35.823674736 +0300
2562 +++ linux-2.6.5-sles9/include/linux/ext3_fs.h 2005-02-23 01:02:37.414432904 +0300
2564 #define EXT3_NOTAIL_FL 0x00008000 /* don't merge file tail */
2565 #define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2566 #define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2567 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2568 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2570 -#define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
2571 +#define EXT3_FL_USER_VISIBLE 0x000BDFFF /* User visible flags */
2572 #define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
2576 #define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
2577 #define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
2578 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 7, long)
2579 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 8, long)
2580 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 9, long)
2583 * Structure of an inode on the disk
2585 #define EXT3_MOUNT_BARRIER 0x20000 /* Use block barriers */
2586 #define EXT3_MOUNT_IOPEN 0x40000 /* Allow access via iopen */
2587 #define EXT3_MOUNT_IOPEN_NOPRIV 0x80000 /* Make iopen world-readable */
2588 +#define EXT3_MOUNT_EXTENTS 0x100000/* Extents support */
2589 +#define EXT3_MOUNT_EXTDEBUG 0x200000/* Extents debug */
2591 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2593 @@ -503,11 +509,13 @@
2594 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2595 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2596 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
2597 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2599 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2600 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2601 EXT3_FEATURE_INCOMPAT_RECOVER| \
2602 - EXT3_FEATURE_INCOMPAT_META_BG)
2603 + EXT3_FEATURE_INCOMPAT_META_BG| \
2604 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2605 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2606 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2607 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2612 +extern int ext3_block_truncate_page(handle_t *, struct page *, struct address_space *, loff_t);
2613 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2614 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2615 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2616 @@ -802,6 +809,16 @@
2617 extern struct inode_operations ext3_symlink_inode_operations;
2618 extern struct inode_operations ext3_fast_symlink_inode_operations;
2621 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2622 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2623 + struct buffer_head *, int, int);
2624 +extern void ext3_ext_truncate(struct inode *, struct page *);
2625 +extern void ext3_ext_init(struct super_block *);
2626 +extern void ext3_ext_release(struct super_block *);
2627 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2628 +extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
2629 + unsigned int cmd, unsigned long arg);
2631 #endif /* __KERNEL__ */
2633 Index: linux-2.6.5-sles9/include/linux/ext3_extents.h
2634 ===================================================================
2635 --- linux-2.6.5-sles9.orig/include/linux/ext3_extents.h 2005-02-17 22:07:57.023609040 +0300
2636 +++ linux-2.6.5-sles9/include/linux/ext3_extents.h 2005-02-23 01:02:37.416432600 +0300
2639 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
2640 + * Written by Alex Tomas <alex@clusterfs.com>
2642 + * This program is free software; you can redistribute it and/or modify
2643 + * it under the terms of the GNU General Public License version 2 as
2644 + * published by the Free Software Foundation.
2646 + * This program is distributed in the hope that it will be useful,
2647 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2648 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2649 + * GNU General Public License for more details.
2651 + * You should have received a copy of the GNU General Public Licens
2652 + * along with this program; if not, write to the Free Software
2653 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2656 +#ifndef _LINUX_EXT3_EXTENTS
2657 +#define _LINUX_EXT3_EXTENTS
2660 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2661 + * become very little, so index split, in-depth growing and
2662 + * other hard changes happens much more often
2663 + * this is for debug purposes only
2665 +#define AGRESSIVE_TEST_
2668 + * if CHECK_BINSEARCH defined, then results of binary search
2669 + * will be checked by linear search
2671 +#define CHECK_BINSEARCH_
2674 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2675 + * to get lots of info what's going on
2679 +#define ext_debug(tree,fmt,a...) \
2681 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2682 + printk(fmt, ##a); \
2685 +#define ext_debug(tree,fmt,a...)
2689 + * if EXT_STATS is defined then stats numbers are collected
2690 + * these number will be displayed at umount time
2695 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2698 + * ext3_inode has i_block array (total 60 bytes)
2699 + * first 4 bytes are used to store:
2700 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2701 + * - number of alive extents in the inode
2705 + * this is extent on-disk structure
2706 + * it's used at the bottom of the tree
2708 +struct ext3_extent {
2709 + __u32 ee_block; /* first logical block extent covers */
2710 + __u16 ee_len; /* number of blocks covered by extent */
2711 + __u16 ee_start_hi; /* high 16 bits of physical block */
2712 + __u32 ee_start; /* low 32 bigs of physical block */
2716 + * this is index on-disk structure
2717 + * it's used at all the levels, but the bottom
2719 +struct ext3_extent_idx {
2720 + __u32 ei_block; /* index covers logical blocks from 'block' */
2721 + __u32 ei_leaf; /* pointer to the physical block of the next *
2722 + * level. leaf or next index could bet here */
2723 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
2728 + * each block (leaves and indexes), even inode-stored has header
2730 +struct ext3_extent_header {
2731 + __u16 eh_magic; /* probably will support different formats */
2732 + __u16 eh_entries; /* number of valid entries */
2733 + __u16 eh_max; /* capacity of store in entries */
2734 + __u16 eh_depth; /* has tree real underlaying blocks? */
2735 + __u32 eh_generation; /* generation of the tree */
2738 +#define EXT3_EXT_MAGIC 0xf30a
2741 + * array of ext3_ext_path contains path to some extent
2742 + * creation/lookup routines use it for traversal/splitting/etc
2743 + * truncate uses it to simulate recursive walking
2745 +struct ext3_ext_path {
2748 + struct ext3_extent *p_ext;
2749 + struct ext3_extent_idx *p_idx;
2750 + struct ext3_extent_header *p_hdr;
2751 + struct buffer_head *p_bh;
2755 + * structure for external API
2759 + * storage for cached extent
2761 +struct ext3_ext_cache {
2768 +#define EXT3_EXT_CACHE_NO 0
2769 +#define EXT3_EXT_CACHE_GAP 1
2770 +#define EXT3_EXT_CACHE_EXTENT 2
2773 + * ext3_extents_tree is used to pass initial information
2774 + * to top-level extents API
2776 +struct ext3_extents_helpers;
2777 +struct ext3_extents_tree {
2778 + struct inode *inode; /* inode which tree belongs to */
2779 + void *root; /* ptr to data top of tree resides at */
2780 + void *buffer; /* will be passed as arg to ^^ routines */
2783 + struct ext3_ext_cache *cex;/* last found extent */
2784 + struct ext3_extents_helpers *ops;
2787 +struct ext3_extents_helpers {
2788 + int (*get_write_access)(handle_t *h, void *buffer);
2789 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2790 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2791 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2792 + struct ext3_extent *, unsigned long,
2794 + int (*remove_extent)(struct ext3_extents_tree *,
2795 + struct ext3_extent *, unsigned long,
2797 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2798 + struct ext3_ext_path *, struct ext3_extent *,
2803 + * to be called by ext3_ext_walk_space()
2804 + * negative retcode - error
2805 + * positive retcode - signal for ext3_ext_walk_space(), see below
2806 + * callback must return valid extent (passed or newly created)
2808 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2809 + struct ext3_ext_path *,
2810 + struct ext3_ext_cache *);
2812 +#define EXT_CONTINUE 0
2813 +#define EXT_BREAK 1
2814 +#define EXT_REPEAT 2
2817 +#define EXT_MAX_BLOCK 0xffffffff
2820 +#define EXT_FIRST_EXTENT(__hdr__) \
2821 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2822 + sizeof(struct ext3_extent_header)))
2823 +#define EXT_FIRST_INDEX(__hdr__) \
2824 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2825 + sizeof(struct ext3_extent_header)))
2826 +#define EXT_HAS_FREE_INDEX(__path__) \
2827 + ((__path__)->p_hdr->eh_entries < (__path__)->p_hdr->eh_max)
2828 +#define EXT_LAST_EXTENT(__hdr__) \
2829 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_entries - 1)
2830 +#define EXT_LAST_INDEX(__hdr__) \
2831 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_entries - 1)
2832 +#define EXT_MAX_EXTENT(__hdr__) \
2833 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_max - 1)
2834 +#define EXT_MAX_INDEX(__hdr__) \
2835 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_max - 1)
2837 +#define EXT_ROOT_HDR(tree) \
2838 + ((struct ext3_extent_header *) (tree)->root)
2839 +#define EXT_BLOCK_HDR(bh) \
2840 + ((struct ext3_extent_header *) (bh)->b_data)
2841 +#define EXT_DEPTH(_t_) \
2842 + (((struct ext3_extent_header *)((_t_)->root))->eh_depth)
2843 +#define EXT_GENERATION(_t_) \
2844 + (((struct ext3_extent_header *)((_t_)->root))->eh_generation)
2847 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2849 +#define EXT_CHECK_PATH(tree,path) \
2851 + int depth = EXT_DEPTH(tree); \
2852 + BUG_ON((unsigned long) (path) < __PAGE_OFFSET); \
2853 + BUG_ON((unsigned long) (path)[depth].p_idx < \
2854 + __PAGE_OFFSET && (path)[depth].p_idx != NULL); \
2855 + BUG_ON((unsigned long) (path)[depth].p_ext < \
2856 + __PAGE_OFFSET && (path)[depth].p_ext != NULL); \
2857 + BUG_ON((unsigned long) (path)[depth].p_hdr < __PAGE_OFFSET); \
2858 + BUG_ON((unsigned long) (path)[depth].p_bh < __PAGE_OFFSET \
2860 + BUG_ON((path)[0].p_depth != depth); \
2865 + * this structure is used to gather extents from the tree via ioctl
2867 +struct ext3_extent_buf {
2868 + unsigned long start;
2876 + * this structure is used to collect stats info about the tree
2878 +struct ext3_extent_tree_stats {
2884 +extern void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
2885 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2886 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2887 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2888 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2889 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2890 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2891 +extern int ext3_ext_calc_blockmap_metadata(struct inode *, int);
2894 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
2897 + tree->cex->ec_type = EXT3_EXT_CACHE_NO;
2901 +#endif /* _LINUX_EXT3_EXTENTS */
2902 Index: linux-2.6.5-sles9/include/linux/ext3_fs_i.h
2903 ===================================================================
2904 --- linux-2.6.5-sles9.orig/include/linux/ext3_fs_i.h 2005-02-23 01:01:52.425272296 +0300
2905 +++ linux-2.6.5-sles9/include/linux/ext3_fs_i.h 2005-02-23 01:45:55.611446920 +0300
2907 #include <linux/rbtree.h>
2908 #include <linux/seqlock.h>
2909 #include <linux/dynlocks.h>
2910 +#include <linux/ext3_extents.h>
2912 struct ext3_reserve_window {
2913 __u32 _rsv_start; /* First byte reserved */
2915 struct dynlock i_htree_lock;
2916 struct semaphore i_append_sem;
2917 struct semaphore i_rename_sem;
2919 + struct ext3_ext_cache i_cached_extent;
2922 #endif /* _LINUX_EXT3_FS_I */
2925 fs/ext3/Makefile | 2
2926 fs/ext3/extents.c | 2356 +++++++++++++++++++++++++++++++++++++++++++
2927 fs/ext3/ialloc.c | 4
2928 fs/ext3/inode.c | 29
2930 fs/ext3/super.c | 15
2931 include/linux/ext3_extents.h | 265 ++++
2932 include/linux/ext3_fs.h | 17
2933 include/linux/ext3_fs_i.h | 3
2934 9 files changed, 2687 insertions(+), 8 deletions(-)