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 = EXT_ROOT_HDR(tree);
181 + neh->eh_generation = ((EXT_FLAGS(neh) & ~EXT_FLAGS_CLR_UNKNOWN) << 24) |
182 + (EXT_GENERATION(neh) + 1);
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)) {
453 + /* don't free previously allocated path
454 + * -- caller should take care */
459 + i = depth = EXT_DEPTH(tree);
460 + EXT_ASSERT(eh->eh_max);
461 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
463 + /* account possible depth increase */
465 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
468 + return ERR_PTR(-ENOMEM);
470 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
471 + path[0].p_hdr = eh;
473 + /* walk through the tree */
475 + ext_debug(tree, "depth %d: num %d, max %d\n",
476 + ppos, eh->eh_entries, eh->eh_max);
477 + ext3_ext_binsearch_idx(tree, path + ppos, block);
478 + path[ppos].p_block = path[ppos].p_idx->ei_leaf;
479 + path[ppos].p_depth = i;
480 + path[ppos].p_ext = NULL;
482 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
486 + eh = EXT_BLOCK_HDR(bh);
488 + EXT_ASSERT(ppos <= depth);
489 + path[ppos].p_bh = bh;
490 + path[ppos].p_hdr = eh;
493 + if (ext3_ext_check_header(eh))
497 + path[ppos].p_depth = i;
498 + path[ppos].p_hdr = eh;
499 + path[ppos].p_ext = NULL;
500 + path[ppos].p_idx = NULL;
502 + if (ext3_ext_check_header(eh))
506 + ext3_ext_binsearch(tree, path + ppos, block);
508 + ext3_ext_show_path(tree, path);
513 + printk(KERN_ERR "EXT3-fs: header is corrupted!\n");
515 + ext3_ext_drop_refs(path);
518 + return ERR_PTR(-EIO);
522 + * insert new index [logical;ptr] into the block at cupr
523 + * it check where to insert: before curp or after curp
525 +static int ext3_ext_insert_index(handle_t *handle,
526 + struct ext3_extents_tree *tree,
527 + struct ext3_ext_path *curp,
528 + int logical, int ptr)
530 + struct ext3_extent_idx *ix;
533 + if ((err = ext3_ext_get_access(handle, tree, curp)))
536 + EXT_ASSERT(logical != curp->p_idx->ei_block);
537 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
538 + if (logical > curp->p_idx->ei_block) {
540 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
541 + len = (len - 1) * sizeof(struct ext3_extent_idx);
542 + len = len < 0 ? 0 : len;
543 + ext_debug(tree, "insert new index %d after: %d. "
544 + "move %d from 0x%p to 0x%p\n",
546 + (curp->p_idx + 1), (curp->p_idx + 2));
547 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
549 + ix = curp->p_idx + 1;
551 + /* insert before */
552 + len = len * sizeof(struct ext3_extent_idx);
553 + len = len < 0 ? 0 : len;
554 + ext_debug(tree, "insert new index %d before: %d. "
555 + "move %d from 0x%p to 0x%p\n",
557 + curp->p_idx, (curp->p_idx + 1));
558 + memmove(curp->p_idx + 1, curp->p_idx, len);
562 + ix->ei_block = logical;
564 + curp->p_hdr->eh_entries++;
566 + EXT_ASSERT(curp->p_hdr->eh_entries <= curp->p_hdr->eh_max);
567 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
569 + err = ext3_ext_dirty(handle, tree, curp);
570 + ext3_std_error(tree->inode->i_sb, err);
576 + * routine inserts new subtree into the path, using free index entry
578 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
579 + * - makes decision where to split
580 + * - moves remaining extens and index entries (right to the split point)
581 + * into the newly allocated blocks
582 + * - initialize subtree
584 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
585 + struct ext3_ext_path *path,
586 + struct ext3_extent *newext, int at)
588 + struct buffer_head *bh = NULL;
589 + int depth = EXT_DEPTH(tree);
590 + struct ext3_extent_header *neh;
591 + struct ext3_extent_idx *fidx;
592 + struct ext3_extent *ex;
593 + int i = at, k, m, a;
594 + unsigned long newblock, oldblock, border;
595 + int *ablocks = NULL; /* array of allocated blocks */
598 + /* make decision: where to split? */
599 + /* FIXME: now desicion is simplest: at current extent */
601 + /* if current leaf will be splitted, then we should use
602 + * border from split point */
603 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
604 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
605 + border = path[depth].p_ext[1].ee_block;
606 + ext_debug(tree, "leaf will be splitted."
607 + " next leaf starts at %d\n",
610 + border = newext->ee_block;
611 + ext_debug(tree, "leaf will be added."
612 + " next leaf starts at %d\n",
617 + * if error occurs, then we break processing
618 + * and turn filesystem read-only. so, index won't
619 + * be inserted and tree will be in consistent
620 + * state. next mount will repair buffers too
624 + * get array to track all allocated blocks
625 + * we need this to handle errors and free blocks
628 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
631 + memset(ablocks, 0, sizeof(unsigned long) * depth);
633 + /* allocate all needed blocks */
634 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
635 + for (a = 0; a < depth - at; a++) {
636 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
639 + ablocks[a] = newblock;
642 + /* initialize new leaf */
643 + newblock = ablocks[--a];
644 + EXT_ASSERT(newblock);
645 + bh = sb_getblk(tree->inode->i_sb, newblock);
652 + if ((err = ext3_journal_get_create_access(handle, bh)))
655 + neh = EXT_BLOCK_HDR(bh);
656 + neh->eh_entries = 0;
657 + neh->eh_max = ext3_ext_space_block(tree);
658 + neh->eh_magic = EXT3_EXT_MAGIC;
660 + ex = EXT_FIRST_EXTENT(neh);
662 + /* move remain of path[depth] to the new leaf */
663 + EXT_ASSERT(path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max);
664 + /* start copy from next extent */
665 + /* TODO: we could do it by single memmove */
667 + path[depth].p_ext++;
668 + while (path[depth].p_ext <=
669 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
670 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
671 + path[depth].p_ext->ee_block,
672 + path[depth].p_ext->ee_start,
673 + path[depth].p_ext->ee_len,
675 + memmove(ex++, path[depth].p_ext++, sizeof(struct ext3_extent));
679 + set_buffer_uptodate(bh);
682 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
687 + /* correct old leaf */
689 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
691 + path[depth].p_hdr->eh_entries -= m;
692 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
697 + /* create intermediate indexes */
698 + k = depth - at - 1;
699 + EXT_ASSERT(k >= 0);
701 + ext_debug(tree, "create %d intermediate indices\n", k);
702 + /* insert new index into current index block */
703 + /* current depth stored in i var */
706 + oldblock = newblock;
707 + newblock = ablocks[--a];
708 + bh = sb_getblk(tree->inode->i_sb, newblock);
715 + if ((err = ext3_journal_get_create_access(handle, bh)))
718 + neh = EXT_BLOCK_HDR(bh);
719 + neh->eh_entries = 1;
720 + neh->eh_magic = EXT3_EXT_MAGIC;
721 + neh->eh_max = ext3_ext_space_block_idx(tree);
722 + neh->eh_depth = depth - i;
723 + fidx = EXT_FIRST_INDEX(neh);
724 + fidx->ei_block = border;
725 + fidx->ei_leaf = oldblock;
727 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
728 + i, newblock, border, oldblock);
733 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
734 + EXT_MAX_INDEX(path[i].p_hdr));
735 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
736 + EXT_LAST_INDEX(path[i].p_hdr));
737 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
738 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
739 + i, path[i].p_idx->ei_block,
740 + path[i].p_idx->ei_leaf, newblock);
741 + memmove(++fidx, path[i].p_idx++,
742 + sizeof(struct ext3_extent_idx));
744 + EXT_ASSERT(neh->eh_entries <= neh->eh_max);
747 + set_buffer_uptodate(bh);
750 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
755 + /* correct old index */
757 + err = ext3_ext_get_access(handle, tree, path + i);
760 + path[i].p_hdr->eh_entries -= m;
761 + err = ext3_ext_dirty(handle, tree, path + i);
769 + /* insert new index */
771 + err = ext3_ext_insert_index(handle, tree, path + at,
776 + if (buffer_locked(bh))
782 + /* free all allocated blocks in error case */
783 + for (i = 0; i < depth; i++) {
786 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
795 + * routine implements tree growing procedure:
796 + * - allocates new block
797 + * - moves top-level data (index block or leaf) into the new block
798 + * - initialize new top-level, creating index that points to the
799 + * just created block
801 +static int ext3_ext_grow_indepth(handle_t *handle,
802 + struct ext3_extents_tree *tree,
803 + struct ext3_ext_path *path,
804 + struct ext3_extent *newext)
806 + struct ext3_ext_path *curp = path;
807 + struct ext3_extent_header *neh;
808 + struct ext3_extent_idx *fidx;
809 + struct buffer_head *bh;
810 + unsigned long newblock;
813 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
817 + bh = sb_getblk(tree->inode->i_sb, newblock);
820 + ext3_std_error(tree->inode->i_sb, err);
825 + if ((err = ext3_journal_get_create_access(handle, bh))) {
830 + /* move top-level index/leaf into new block */
831 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
833 + /* set size of new block */
834 + neh = EXT_BLOCK_HDR(bh);
835 + /* old root could have indexes or leaves
836 + * so calculate eh_max right way */
837 + if (EXT_DEPTH(tree))
838 + neh->eh_max = ext3_ext_space_block_idx(tree);
840 + neh->eh_max = ext3_ext_space_block(tree);
841 + neh->eh_magic = EXT3_EXT_MAGIC;
842 + set_buffer_uptodate(bh);
845 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
848 + /* create index in new top-level index: num,max,pointer */
849 + if ((err = ext3_ext_get_access(handle, tree, curp)))
852 + curp->p_hdr->eh_magic = EXT3_EXT_MAGIC;
853 + curp->p_hdr->eh_max = ext3_ext_space_root_idx(tree);
854 + curp->p_hdr->eh_entries = 1;
855 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
856 + /* FIXME: it works, but actually path[0] can be index */
857 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
858 + curp->p_idx->ei_leaf = newblock;
860 + neh = EXT_ROOT_HDR(tree);
861 + fidx = EXT_FIRST_INDEX(neh);
862 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
863 + neh->eh_entries, neh->eh_max, fidx->ei_block, fidx->ei_leaf);
865 + neh->eh_depth = path->p_depth + 1;
866 + err = ext3_ext_dirty(handle, tree, curp);
874 + * routine finds empty index and adds new leaf. if no free index found
875 + * then it requests in-depth growing
877 +static int ext3_ext_create_new_leaf(handle_t *handle,
878 + struct ext3_extents_tree *tree,
879 + struct ext3_ext_path *path,
880 + struct ext3_extent *newext)
882 + struct ext3_ext_path *curp;
883 + int depth, i, err = 0;
886 + i = depth = EXT_DEPTH(tree);
888 + /* walk up to the tree and look for free index entry */
889 + curp = path + depth;
890 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
895 + /* we use already allocated block for index block
896 + * so, subsequent data blocks should be contigoues */
897 + if (EXT_HAS_FREE_INDEX(curp)) {
898 + /* if we found index with free entry, then use that
899 + * entry: create all needed subtree and add new leaf */
900 + err = ext3_ext_split(handle, tree, path, newext, i);
903 + ext3_ext_drop_refs(path);
904 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
906 + err = PTR_ERR(path);
908 + /* tree is full, time to grow in depth */
909 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
912 + ext3_ext_drop_refs(path);
913 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
915 + err = PTR_ERR(path);
918 + * only first (depth 0 -> 1) produces free space
919 + * in all other cases we have to split growed tree
921 + depth = EXT_DEPTH(tree);
922 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
923 + /* now we need split */
935 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
936 + * NOTE: it consider block number from index entry as
937 + * allocated block. thus, index entries have to be consistent
940 +static unsigned long
941 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
945 + EXT_ASSERT(path != NULL);
946 + depth = path->p_depth;
948 + if (depth == 0 && path->p_ext == NULL)
949 + return EXT_MAX_BLOCK;
951 + /* FIXME: what if index isn't full ?! */
952 + while (depth >= 0) {
953 + if (depth == path->p_depth) {
955 + if (path[depth].p_ext !=
956 + EXT_LAST_EXTENT(path[depth].p_hdr))
957 + return path[depth].p_ext[1].ee_block;
960 + if (path[depth].p_idx !=
961 + EXT_LAST_INDEX(path[depth].p_hdr))
962 + return path[depth].p_idx[1].ei_block;
967 + return EXT_MAX_BLOCK;
971 + * returns first allocated block from next leaf or EXT_MAX_BLOCK
973 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
974 + struct ext3_ext_path *path)
978 + EXT_ASSERT(path != NULL);
979 + depth = path->p_depth;
981 + /* zero-tree has no leaf blocks at all */
983 + return EXT_MAX_BLOCK;
985 + /* go to index block */
988 + while (depth >= 0) {
989 + if (path[depth].p_idx !=
990 + EXT_LAST_INDEX(path[depth].p_hdr))
991 + return path[depth].p_idx[1].ei_block;
995 + return EXT_MAX_BLOCK;
999 + * if leaf gets modified and modified extent is first in the leaf
1000 + * then we have to correct all indexes above
1001 + * TODO: do we need to correct tree in all cases?
1003 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
1004 + struct ext3_ext_path *path)
1006 + struct ext3_extent_header *eh;
1007 + int depth = EXT_DEPTH(tree);
1008 + struct ext3_extent *ex;
1009 + unsigned long border;
1012 + eh = path[depth].p_hdr;
1013 + ex = path[depth].p_ext;
1018 + /* there is no tree at all */
1022 + if (ex != EXT_FIRST_EXTENT(eh)) {
1023 + /* we correct tree if first leaf got modified only */
1028 + * TODO: we need correction if border is smaller then current one
1031 + border = path[depth].p_ext->ee_block;
1032 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1034 + path[k].p_idx->ei_block = border;
1035 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1039 + /* change all left-side indexes */
1040 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1042 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1044 + path[k].p_idx->ei_block = border;
1045 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1053 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1054 + struct ext3_extent *ex1,
1055 + struct ext3_extent *ex2)
1057 + if (ex1->ee_block + ex1->ee_len != ex2->ee_block)
1060 +#ifdef AGRESSIVE_TEST
1061 + if (ex1->ee_len >= 4)
1065 + if (!tree->ops->mergable)
1068 + return tree->ops->mergable(ex1, ex2);
1072 + * this routine tries to merge requsted extent into the existing
1073 + * extent or inserts requested extent as new one into the tree,
1074 + * creating new leaf in no-space case
1076 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1077 + struct ext3_ext_path *path,
1078 + struct ext3_extent *newext)
1080 + struct ext3_extent_header * eh;
1081 + struct ext3_extent *ex, *fex;
1082 + struct ext3_extent *nearex; /* nearest extent */
1083 + struct ext3_ext_path *npath = NULL;
1084 + int depth, len, err, next;
1086 + EXT_ASSERT(newext->ee_len > 0);
1087 + depth = EXT_DEPTH(tree);
1088 + ex = path[depth].p_ext;
1089 + EXT_ASSERT(path[depth].p_hdr);
1091 + /* try to insert block into found extent and return */
1092 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1093 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1094 + newext->ee_len, ex->ee_block, ex->ee_len,
1096 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1098 + ex->ee_len += newext->ee_len;
1099 + eh = path[depth].p_hdr;
1105 + depth = EXT_DEPTH(tree);
1106 + eh = path[depth].p_hdr;
1107 + if (eh->eh_entries < eh->eh_max)
1110 + /* probably next leaf has space for us? */
1111 + fex = EXT_LAST_EXTENT(eh);
1112 + next = ext3_ext_next_leaf_block(tree, path);
1113 + if (newext->ee_block > fex->ee_block && next != EXT_MAX_BLOCK) {
1114 + ext_debug(tree, "next leaf block - %d\n", next);
1115 + EXT_ASSERT(!npath);
1116 + npath = ext3_ext_find_extent(tree, next, NULL);
1117 + if (IS_ERR(npath))
1118 + return PTR_ERR(npath);
1119 + EXT_ASSERT(npath->p_depth == path->p_depth);
1120 + eh = npath[depth].p_hdr;
1121 + if (eh->eh_entries < eh->eh_max) {
1122 + ext_debug(tree, "next leaf isnt full(%d)\n",
1127 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1128 + eh->eh_entries, eh->eh_max);
1132 + * there is no free space in found leaf
1133 + * we're gonna add new leaf in the tree
1135 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1138 + depth = EXT_DEPTH(tree);
1139 + eh = path[depth].p_hdr;
1142 + nearex = path[depth].p_ext;
1144 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1148 + /* there is no extent in this leaf, create first one */
1149 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1150 + newext->ee_block, newext->ee_start,
1152 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1153 + } else if (newext->ee_block > nearex->ee_block) {
1154 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1155 + if (nearex != EXT_LAST_EXTENT(eh)) {
1156 + len = EXT_MAX_EXTENT(eh) - nearex;
1157 + len = (len - 1) * sizeof(struct ext3_extent);
1158 + len = len < 0 ? 0 : len;
1159 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1160 + "move %d from 0x%p to 0x%p\n",
1161 + newext->ee_block, newext->ee_start,
1163 + nearex, len, nearex + 1, nearex + 2);
1164 + memmove(nearex + 2, nearex + 1, len);
1166 + path[depth].p_ext = nearex + 1;
1168 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1169 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1170 + len = len < 0 ? 0 : len;
1171 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1172 + "move %d from 0x%p to 0x%p\n",
1173 + newext->ee_block, newext->ee_start, newext->ee_len,
1174 + nearex, len, nearex + 1, nearex + 2);
1175 + memmove(nearex + 1, nearex, len);
1176 + path[depth].p_ext = nearex;
1180 + nearex = path[depth].p_ext;
1181 + nearex->ee_block = newext->ee_block;
1182 + nearex->ee_start = newext->ee_start;
1183 + nearex->ee_len = newext->ee_len;
1184 + /* FIXME: support for large fs */
1185 + nearex->ee_start_hi = 0;
1188 + /* try to merge extents to the right */
1189 + while (nearex < EXT_LAST_EXTENT(eh)) {
1190 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1192 + /* merge with next extent! */
1193 + nearex->ee_len += nearex[1].ee_len;
1194 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1195 + len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1196 + sizeof(struct ext3_extent);
1197 + memmove(nearex + 1, nearex + 2, len);
1200 + EXT_ASSERT(eh->eh_entries > 0);
1203 + /* try to merge extents to the left */
1205 + /* time to correct all indexes above */
1206 + err = ext3_ext_correct_indexes(handle, tree, path);
1210 + err = ext3_ext_dirty(handle, tree, path + depth);
1214 + ext3_ext_drop_refs(npath);
1217 + ext3_ext_tree_changed(tree);
1218 + ext3_ext_invalidate_cache(tree);
1222 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1223 + unsigned long num, ext_prepare_callback func)
1225 + struct ext3_ext_path *path = NULL;
1226 + struct ext3_ext_cache cbex;
1227 + struct ext3_extent *ex;
1228 + unsigned long next, start = 0, end = 0;
1229 + unsigned long last = block + num;
1230 + int depth, exists, err = 0;
1234 + EXT_ASSERT(tree->inode);
1235 + EXT_ASSERT(tree->root);
1237 + while (block < last && block != EXT_MAX_BLOCK) {
1238 + num = last - block;
1239 + /* find extent for this block */
1240 + path = ext3_ext_find_extent(tree, block, path);
1241 + if (IS_ERR(path)) {
1242 + err = PTR_ERR(path);
1247 + depth = EXT_DEPTH(tree);
1248 + EXT_ASSERT(path[depth].p_hdr);
1249 + ex = path[depth].p_ext;
1250 + next = ext3_ext_next_allocated_block(path);
1254 + /* there is no extent yet, so try to allocate
1255 + * all requested space */
1257 + end = block + num;
1258 + } else if (ex->ee_block > block) {
1259 + /* need to allocate space before found extent */
1261 + end = ex->ee_block;
1262 + if (block + num < end)
1263 + end = block + num;
1264 + } else if (block >= ex->ee_block + ex->ee_len) {
1265 + /* need to allocate space after found extent */
1267 + end = block + num;
1270 + } else if (block >= ex->ee_block) {
1272 + * some part of requested space is covered
1276 + end = ex->ee_block + ex->ee_len;
1277 + if (block + num < end)
1278 + end = block + num;
1283 + EXT_ASSERT(end > start);
1286 + cbex.ec_block = start;
1287 + cbex.ec_len = end - start;
1288 + cbex.ec_start = 0;
1289 + cbex.ec_type = EXT3_EXT_CACHE_GAP;
1291 + cbex.ec_block = ex->ee_block;
1292 + cbex.ec_len = ex->ee_len;
1293 + cbex.ec_start = ex->ee_start;
1294 + cbex.ec_type = EXT3_EXT_CACHE_EXTENT;
1297 + EXT_ASSERT(cbex.ec_len > 0);
1298 + EXT_ASSERT(path[depth].p_hdr);
1299 + err = func(tree, path, &cbex);
1300 + ext3_ext_drop_refs(path);
1304 + if (err == EXT_REPEAT)
1306 + else if (err == EXT_BREAK) {
1311 + if (EXT_DEPTH(tree) != depth) {
1312 + /* depth was changed. we have to realloc path */
1317 + block = cbex.ec_block + cbex.ec_len;
1321 + ext3_ext_drop_refs(path);
1329 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, __u32 block,
1330 + __u32 len, __u32 start, int type)
1332 + EXT_ASSERT(len > 0);
1334 + tree->cex->ec_type = type;
1335 + tree->cex->ec_block = block;
1336 + tree->cex->ec_len = len;
1337 + tree->cex->ec_start = start;
1342 + * this routine calculate boundaries of the gap requested block fits into
1343 + * and cache this gap
1346 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1347 + struct ext3_ext_path *path,
1348 + unsigned long block)
1350 + int depth = EXT_DEPTH(tree);
1351 + unsigned long lblock, len;
1352 + struct ext3_extent *ex;
1357 + ex = path[depth].p_ext;
1359 + /* there is no extent yet, so gap is [0;-] */
1361 + len = EXT_MAX_BLOCK;
1362 + ext_debug(tree, "cache gap(whole file):");
1363 + } else if (block < ex->ee_block) {
1365 + len = ex->ee_block - block;
1366 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1367 + (unsigned long) block,
1368 + (unsigned long) ex->ee_block,
1369 + (unsigned long) ex->ee_len);
1370 + } else if (block >= ex->ee_block + ex->ee_len) {
1371 + lblock = ex->ee_block + ex->ee_len;
1372 + len = ext3_ext_next_allocated_block(path);
1373 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1374 + (unsigned long) ex->ee_block,
1375 + (unsigned long) ex->ee_len,
1376 + (unsigned long) block);
1377 + EXT_ASSERT(len > lblock);
1378 + len = len - lblock;
1384 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) lblock, len);
1385 + ext3_ext_put_in_cache(tree, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1389 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1390 + struct ext3_extent *ex)
1392 + struct ext3_ext_cache *cex = tree->cex;
1394 + /* is there cache storage at all? */
1396 + return EXT3_EXT_CACHE_NO;
1398 + /* has cache valid data? */
1399 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1400 + return EXT3_EXT_CACHE_NO;
1402 + EXT_ASSERT(cex->ec_type == EXT3_EXT_CACHE_GAP ||
1403 + cex->ec_type == EXT3_EXT_CACHE_EXTENT);
1404 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1405 + ex->ee_block = cex->ec_block;
1406 + ex->ee_start = cex->ec_start;
1407 + ex->ee_len = cex->ec_len;
1408 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1409 + (unsigned long) block,
1410 + (unsigned long) ex->ee_block,
1411 + (unsigned long) ex->ee_len,
1412 + (unsigned long) ex->ee_start);
1413 + return cex->ec_type;
1416 + /* not in cache */
1417 + return EXT3_EXT_CACHE_NO;
1421 + * routine removes index from the index block
1422 + * it's used in truncate case only. thus all requests are for
1423 + * last index in the block only
1425 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1426 + struct ext3_ext_path *path)
1428 + struct buffer_head *bh;
1431 + /* free index block */
1433 + EXT_ASSERT(path->p_hdr->eh_entries);
1434 + if ((err = ext3_ext_get_access(handle, tree, path)))
1436 + path->p_hdr->eh_entries--;
1437 + if ((err = ext3_ext_dirty(handle, tree, path)))
1439 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1440 + path->p_idx->ei_leaf);
1441 + bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
1442 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1443 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1447 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1448 + struct ext3_ext_path *path)
1450 + int depth = EXT_DEPTH(tree);
1454 + /* probably there is space in leaf? */
1455 + if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1460 + * the worste case we're expecting is creation of the
1461 + * new root (growing in depth) with index splitting
1462 + * for splitting we have to consider depth + 1 because
1463 + * previous growing could increase it
1465 + depth = depth + 1;
1468 + * growing in depth:
1469 + * block allocation + new root + old root
1471 + needed = EXT3_ALLOC_NEEDED + 2;
1473 + /* index split. we may need:
1474 + * allocate intermediate indexes and new leaf
1475 + * change two blocks at each level, but root
1476 + * modify root block (inode)
1478 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1484 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1485 + struct ext3_ext_path *path, unsigned long start,
1486 + unsigned long end)
1488 + struct ext3_extent *ex, tex;
1489 + struct ext3_ext_path *npath;
1490 + int depth, creds, err;
1492 + depth = EXT_DEPTH(tree);
1493 + ex = path[depth].p_ext;
1495 + EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1496 + EXT_ASSERT(ex->ee_block < start);
1498 + /* calculate tail extent */
1499 + tex.ee_block = end + 1;
1500 + EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1501 + tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1503 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1504 + handle = ext3_ext_journal_restart(handle, creds);
1505 + if (IS_ERR(handle))
1506 + return PTR_ERR(handle);
1508 + /* calculate head extent. use primary extent */
1509 + err = ext3_ext_get_access(handle, tree, path + depth);
1512 + ex->ee_len = start - ex->ee_block;
1513 + err = ext3_ext_dirty(handle, tree, path + depth);
1517 + /* FIXME: some callback to free underlying resource
1518 + * and correct ee_start? */
1519 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1520 + ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1522 + npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1523 + if (IS_ERR(npath))
1524 + return PTR_ERR(npath);
1525 + depth = EXT_DEPTH(tree);
1526 + EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1527 + EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1529 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1530 + ext3_ext_drop_refs(npath);
1537 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1538 + struct ext3_ext_path *path, unsigned long start,
1539 + unsigned long end)
1541 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1542 + int err = 0, correct_index = 0;
1543 + int depth = EXT_DEPTH(tree), credits;
1544 + struct ext3_extent_header *eh;
1545 + unsigned a, b, block, num;
1547 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1548 + if (!path[depth].p_hdr)
1549 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1550 + eh = path[depth].p_hdr;
1552 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1553 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1555 + /* find where to start removing */
1556 + le = ex = EXT_LAST_EXTENT(eh);
1557 + while (ex != EXT_FIRST_EXTENT(eh)) {
1558 + if (ex->ee_block <= end)
1563 + if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1564 + /* removal of internal part of the extent requested
1565 + * tail and head must be placed in different extent
1566 + * so, we have to insert one more extent */
1567 + path[depth].p_ext = ex;
1568 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1572 + while (ex >= EXT_FIRST_EXTENT(eh) && ex->ee_block + ex->ee_len > start) {
1573 + ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1574 + path[depth].p_ext = ex;
1576 + a = ex->ee_block > start ? ex->ee_block : start;
1577 + b = ex->ee_block + ex->ee_len - 1 < end ?
1578 + ex->ee_block + ex->ee_len - 1 : end;
1580 + ext_debug(tree, " border %u:%u\n", a, b);
1582 + if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1586 + } else if (a != ex->ee_block) {
1587 + /* remove tail of the extent */
1588 + block = ex->ee_block;
1590 + } else if (b != ex->ee_block + ex->ee_len - 1) {
1591 + /* remove head of the extent */
1595 + /* remove whole extent: excelent! */
1596 + block = ex->ee_block;
1598 + EXT_ASSERT(a == ex->ee_block &&
1599 + b == ex->ee_block + ex->ee_len - 1);
1602 + if (ex == EXT_FIRST_EXTENT(eh))
1603 + correct_index = 1;
1606 + if (correct_index)
1607 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1608 + if (tree->ops->remove_extent_credits)
1609 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1611 + handle = ext3_ext_journal_restart(handle, credits);
1612 + if (IS_ERR(handle)) {
1613 + err = PTR_ERR(handle);
1617 + err = ext3_ext_get_access(handle, tree, path + depth);
1621 + if (tree->ops->remove_extent)
1622 + err = tree->ops->remove_extent(tree, ex, a, b);
1627 + /* this extent is removed entirely mark slot unused */
1633 + ex->ee_block = block;
1636 + err = ext3_ext_dirty(handle, tree, path + depth);
1640 + ext_debug(tree, "new extent: %u:%u:%u\n",
1641 + ex->ee_block, ex->ee_len, ex->ee_start);
1646 + /* reuse unused slots */
1648 + if (lu->ee_start) {
1657 + if (correct_index && eh->eh_entries)
1658 + err = ext3_ext_correct_indexes(handle, tree, path);
1660 + /* if this leaf is free, then we should
1661 + * remove it from index block above */
1662 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1663 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1670 +static struct ext3_extent_idx *
1671 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1673 + struct ext3_extent_idx *ix;
1675 + ix = EXT_LAST_INDEX(hdr);
1676 + while (ix != EXT_FIRST_INDEX(hdr)) {
1677 + if (ix->ei_block <= block)
1685 + * returns 1 if current index have to be freed (even partial)
1688 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1690 + EXT_ASSERT(path->p_idx);
1692 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1696 + * if truncate on deeper level happened it it wasn't partial
1697 + * so we have to consider current index for truncation
1699 + if (path->p_hdr->eh_entries == path->p_block)
1704 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1705 + unsigned long start, unsigned long end)
1707 + struct inode *inode = tree->inode;
1708 + struct super_block *sb = inode->i_sb;
1709 + int depth = EXT_DEPTH(tree);
1710 + struct ext3_ext_path *path;
1712 + int i = 0, err = 0;
1714 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1716 + /* probably first extent we're gonna free will be last in block */
1717 + handle = ext3_journal_start(inode, depth + 1);
1718 + if (IS_ERR(handle))
1719 + return PTR_ERR(handle);
1721 + ext3_ext_invalidate_cache(tree);
1724 + * we start scanning from right side freeing all the blocks
1725 + * after i_size and walking into the deep
1727 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1728 + if (IS_ERR(path)) {
1729 + ext3_error(sb, __FUNCTION__, "Can't allocate path array");
1730 + ext3_journal_stop(handle);
1733 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1734 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1736 + while (i >= 0 && err == 0) {
1738 + /* this is leaf block */
1739 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1740 + /* root level have p_bh == NULL, brelse() eats this */
1741 + brelse(path[i].p_bh);
1746 + /* this is index block */
1747 + if (!path[i].p_hdr) {
1748 + ext_debug(tree, "initialize header\n");
1749 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1752 + EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1753 + EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1755 + if (!path[i].p_idx) {
1756 + /* this level hasn't touched yet */
1758 + ext3_ext_last_covered(path[i].p_hdr, end);
1759 + path[i].p_block = path[i].p_hdr->eh_entries + 1;
1760 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1761 + path[i].p_hdr, path[i].p_hdr->eh_entries);
1763 + /* we've already was here, see at next index */
1767 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1768 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1770 + if (ext3_ext_more_to_rm(path + i)) {
1771 + /* go to the next level */
1772 + ext_debug(tree, "move to level %d (block %d)\n",
1773 + i + 1, path[i].p_idx->ei_leaf);
1774 + memset(path + i + 1, 0, sizeof(*path));
1775 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1776 + if (!path[i+1].p_bh) {
1777 + /* should we reset i_size? */
1781 + /* put actual number of indexes to know is this
1782 + * number got changed at the next iteration */
1783 + path[i].p_block = path[i].p_hdr->eh_entries;
1786 + /* we finish processing this index, go up */
1787 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1788 + /* index is empty, remove it
1789 + * handle must be already prepared by the
1790 + * truncatei_leaf() */
1791 + err = ext3_ext_rm_idx(handle, tree, path + i);
1793 + /* root level have p_bh == NULL, brelse() eats this */
1794 + brelse(path[i].p_bh);
1796 + ext_debug(tree, "return to level %d\n", i);
1800 + /* TODO: flexible tree reduction should be here */
1801 + if (path->p_hdr->eh_entries == 0) {
1803 + * truncate to zero freed all the tree
1804 + * so, we need to correct eh_depth
1806 + err = ext3_ext_get_access(handle, tree, path);
1808 + EXT_ROOT_HDR(tree)->eh_depth = 0;
1809 + EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1810 + err = ext3_ext_dirty(handle, tree, path);
1813 + ext3_ext_tree_changed(tree);
1816 + ext3_journal_stop(handle);
1821 +int ext3_ext_calc_metadata_amount(struct ext3_extents_tree *tree, int blocks)
1823 + int lcap, icap, rcap, leafs, idxs, num;
1825 + rcap = ext3_ext_space_root(tree);
1826 + if (blocks <= rcap) {
1827 + /* all extents fit to the root */
1831 + rcap = ext3_ext_space_root_idx(tree);
1832 + lcap = ext3_ext_space_block(tree);
1833 + icap = ext3_ext_space_block_idx(tree);
1835 + num = leafs = (blocks + lcap - 1) / lcap;
1836 + if (leafs <= rcap) {
1837 + /* all pointers to leafs fit to the root */
1841 + /* ok. we need separate index block(s) to link all leaf blocks */
1842 + idxs = (leafs + icap - 1) / icap;
1845 + idxs = (idxs + icap - 1) / icap;
1846 + } while (idxs > rcap);
1852 + * called at mount time
1854 +void ext3_ext_init(struct super_block *sb)
1857 + * possible initialization would be here
1860 + if (test_opt(sb, EXTENTS)) {
1861 + printk("EXT3-fs: file extents enabled");
1862 +#ifdef AGRESSIVE_TEST
1863 + printk(", agressive tests");
1865 +#ifdef CHECK_BINSEARCH
1866 + printk(", check binsearch");
1873 + * called at umount time
1875 +void ext3_ext_release(struct super_block *sb)
1879 +/************************************************************************
1880 + * VFS related routines
1881 + ************************************************************************/
1883 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1885 + /* we use in-core data, not bh */
1889 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1891 + struct inode *inode = buffer;
1892 + return ext3_mark_inode_dirty(handle, inode);
1895 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1896 + struct ext3_extent *ex2)
1898 + /* FIXME: support for large fs */
1899 + if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1905 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1906 + struct ext3_extent *ex,
1907 + unsigned long from, unsigned long to)
1911 + /* at present, extent can't cross block group */;
1912 + needed = 4; /* bitmap + group desc + sb + inode */
1914 +#ifdef CONFIG_QUOTA
1915 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1921 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1922 + struct ext3_extent *ex,
1923 + unsigned long from, unsigned long to)
1925 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1926 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1927 + struct buffer_head *bh;
1930 + if (IS_ERR(handle))
1931 + return PTR_ERR(handle);
1932 + if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1933 + /* tail removal */
1934 + unsigned long num, start;
1935 + num = ex->ee_block + ex->ee_len - from;
1936 + start = ex->ee_start + ex->ee_len - num;
1937 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1939 + for (i = 0; i < num; i++) {
1940 + bh = sb_find_get_block(tree->inode->i_sb, start + i);
1941 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1943 + ext3_free_blocks(handle, tree->inode, start, num);
1944 + } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1945 + printk("strange request: removal %lu-%lu from %u:%u\n",
1946 + from, to, ex->ee_block, ex->ee_len);
1948 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1949 + from, to, ex->ee_block, ex->ee_len);
1951 + ext3_journal_stop(handle);
1955 +static int ext3_ext_find_goal(struct inode *inode,
1956 + struct ext3_ext_path *path, unsigned long block)
1958 + struct ext3_inode_info *ei = EXT3_I(inode);
1959 + unsigned long bg_start;
1960 + unsigned long colour;
1964 + struct ext3_extent *ex;
1965 + depth = path->p_depth;
1967 + /* try to predict block placement */
1968 + if ((ex = path[depth].p_ext))
1969 + return ex->ee_start + (block - ex->ee_block);
1971 + /* it looks index is empty
1972 + * try to find starting from index itself */
1973 + if (path[depth].p_bh)
1974 + return path[depth].p_bh->b_blocknr;
1977 + /* OK. use inode's group */
1978 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1979 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1980 + colour = (current->pid % 16) *
1981 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1982 + return bg_start + colour + block;
1985 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1986 + struct ext3_ext_path *path,
1987 + struct ext3_extent *ex, int *err)
1989 + struct inode *inode = tree->inode;
1990 + int newblock, goal;
1994 + EXT_ASSERT(ex->ee_start);
1995 + EXT_ASSERT(ex->ee_len);
1997 + /* reuse block from the extent to order data/metadata */
1998 + newblock = ex->ee_start++;
2000 + if (ex->ee_len == 0) {
2002 + /* allocate new block for the extent */
2003 + goal = ext3_ext_find_goal(inode, path, ex->ee_block);
2004 + ex->ee_start = ext3_new_block(handle, inode, goal, err);
2005 + if (ex->ee_start == 0) {
2006 + /* error occured: restore old extent */
2007 + ex->ee_start = newblock;
2014 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
2015 + .get_write_access = ext3_get_inode_write_access,
2016 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
2017 + .mergable = ext3_ext_mergable,
2018 + .new_block = ext3_new_block_cb,
2019 + .remove_extent = ext3_remove_blocks,
2020 + .remove_extent_credits = ext3_remove_blocks_credits,
2023 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
2024 + struct inode *inode)
2026 + tree->inode = inode;
2027 + tree->root = (void *) EXT3_I(inode)->i_data;
2028 + tree->buffer = (void *) inode;
2029 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
2030 + tree->cex = &EXT3_I(inode)->i_cached_extent;
2031 + tree->ops = &ext3_blockmap_helpers;
2034 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2035 + long iblock, struct buffer_head *bh_result,
2036 + int create, int extend_disksize)
2038 + struct ext3_ext_path *path = NULL;
2039 + struct ext3_extent newex;
2040 + struct ext3_extent *ex;
2041 + int goal, newblock, err = 0, depth;
2042 + struct ext3_extents_tree tree;
2044 + __clear_bit(BH_New, &bh_result->b_state);
2045 + ext3_init_tree_desc(&tree, inode);
2046 + ext_debug(&tree, "block %d requested for inode %u\n",
2047 + (int) iblock, (unsigned) inode->i_ino);
2048 + down(&EXT3_I(inode)->truncate_sem);
2050 + /* check in cache */
2051 + if ((goal = ext3_ext_in_cache(&tree, iblock, &newex))) {
2052 + if (goal == EXT3_EXT_CACHE_GAP) {
2054 + /* block isn't allocated yet and
2055 + * user don't want to allocate it */
2058 + /* we should allocate requested block */
2059 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2060 + /* block is already allocated */
2061 + newblock = iblock - newex.ee_block + newex.ee_start;
2068 + /* find extent for this block */
2069 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2070 + if (IS_ERR(path)) {
2071 + err = PTR_ERR(path);
2076 + depth = EXT_DEPTH(&tree);
2079 + * consistent leaf must not be empty
2080 + * this situations is possible, though, _during_ tree modification
2081 + * this is why assert can't be put in ext3_ext_find_extent()
2083 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2085 + if ((ex = path[depth].p_ext)) {
2086 + /* if found exent covers block, simple return it */
2087 + if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2088 + newblock = iblock - ex->ee_block + ex->ee_start;
2089 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2090 + (int) iblock, ex->ee_block, ex->ee_len,
2092 + ext3_ext_put_in_cache(&tree, ex->ee_block,
2093 + ex->ee_len, ex->ee_start,
2094 + EXT3_EXT_CACHE_EXTENT);
2100 + * requested block isn't allocated yet
2101 + * we couldn't try to create block if create flag is zero
2104 + /* put just found gap into cache to speedup subsequest reqs */
2105 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2109 + /* allocate new block */
2110 + goal = ext3_ext_find_goal(inode, path, iblock);
2111 + newblock = ext3_new_block(handle, inode, goal, &err);
2114 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2117 + /* try to insert new extent into found leaf and return */
2118 + newex.ee_block = iblock;
2119 + newex.ee_start = newblock;
2121 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2125 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2126 + EXT3_I(inode)->i_disksize = inode->i_size;
2128 + /* previous routine could use block we allocated */
2129 + newblock = newex.ee_start;
2130 + __set_bit(BH_New, &bh_result->b_state);
2132 + ext3_ext_put_in_cache(&tree, newex.ee_block, newex.ee_len,
2133 + newex.ee_start, EXT3_EXT_CACHE_EXTENT);
2135 + ext3_ext_show_leaf(&tree, path);
2136 + __set_bit(BH_Mapped, &bh_result->b_state);
2137 + bh_result->b_bdev = inode->i_sb->s_bdev;
2138 + bh_result->b_blocknr = newblock;
2141 + ext3_ext_drop_refs(path);
2144 + up(&EXT3_I(inode)->truncate_sem);
2149 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2151 + struct address_space *mapping = inode->i_mapping;
2152 + struct super_block *sb = inode->i_sb;
2153 + struct ext3_extents_tree tree;
2154 + unsigned long last_block;
2158 + ext3_init_tree_desc(&tree, inode);
2161 + * probably first extent we're gonna free will be last in block
2163 + err = ext3_writepage_trans_blocks(inode) + 3;
2164 + handle = ext3_journal_start(inode, err);
2165 + if (IS_ERR(handle)) {
2167 + clear_highpage(page);
2168 + flush_dcache_page(page);
2169 + unlock_page(page);
2170 + page_cache_release(page);
2176 + ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2178 + down(&EXT3_I(inode)->truncate_sem);
2179 + ext3_ext_invalidate_cache(&tree);
2182 + * TODO: optimization is possible here
2183 + * probably we need not scaning at all,
2184 + * because page truncation is enough
2186 + if (ext3_orphan_add(handle, inode))
2189 + /* we have to know where to truncate from in crash case */
2190 + EXT3_I(inode)->i_disksize = inode->i_size;
2191 + ext3_mark_inode_dirty(handle, inode);
2193 + last_block = (inode->i_size + sb->s_blocksize - 1) >>
2194 + EXT3_BLOCK_SIZE_BITS(sb);
2195 + err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2197 + /* In a multi-transaction truncate, we only make the final
2198 + * transaction synchronous */
2199 + if (IS_SYNC(inode))
2200 + handle->h_sync = 1;
2204 + * If this was a simple ftruncate(), and the file will remain alive
2205 + * then we need to clear up the orphan record which we created above.
2206 + * However, if this was a real unlink then we were called by
2207 + * ext3_delete_inode(), and we allow that function to clean up the
2208 + * orphan info for us.
2210 + if (inode->i_nlink)
2211 + ext3_orphan_del(handle, inode);
2213 + up(&EXT3_I(inode)->truncate_sem);
2214 + ext3_journal_stop(handle);
2218 + * this routine calculate max number of blocks we could modify
2219 + * in order to allocate new block for an inode
2221 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2223 + struct ext3_extents_tree tree;
2226 + ext3_init_tree_desc(&tree, inode);
2228 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2230 + /* caller want to allocate num blocks */
2233 +#ifdef CONFIG_QUOTA
2235 + * FIXME: real calculation should be here
2236 + * it depends on blockmap format of qouta file
2238 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2244 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2246 + struct ext3_extents_tree tree;
2248 + ext3_init_tree_desc(&tree, inode);
2249 + ext3_extent_tree_init(handle, &tree);
2252 +int ext3_ext_calc_blockmap_metadata(struct inode *inode, int blocks)
2254 + struct ext3_extents_tree tree;
2256 + ext3_init_tree_desc(&tree, inode);
2257 + return ext3_ext_calc_metadata_amount(&tree, blocks);
2261 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2262 + struct ext3_ext_path *path,
2263 + struct ext3_ext_cache *newex)
2265 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2267 + if (newex->ec_type != EXT3_EXT_CACHE_EXTENT)
2268 + return EXT_CONTINUE;
2272 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2275 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2277 + buf->cur += sizeof(*newex);
2279 + buf->err = -EFAULT;
2282 + return EXT_CONTINUE;
2286 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2287 + struct ext3_ext_path *path,
2288 + struct ext3_ext_cache *ex)
2290 + struct ext3_extent_tree_stats *buf =
2291 + (struct ext3_extent_tree_stats *) tree->private;
2294 + if (ex->ec_type != EXT3_EXT_CACHE_EXTENT)
2295 + return EXT_CONTINUE;
2297 + depth = EXT_DEPTH(tree);
2298 + buf->extents_num++;
2299 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2301 + return EXT_CONTINUE;
2304 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2305 + unsigned long arg)
2309 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2312 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2313 + struct ext3_extent_buf buf;
2314 + struct ext3_extents_tree tree;
2316 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2319 + ext3_init_tree_desc(&tree, inode);
2320 + buf.cur = buf.buffer;
2322 + tree.private = &buf;
2323 + down(&EXT3_I(inode)->truncate_sem);
2324 + err = ext3_ext_walk_space(&tree, buf.start, EXT_MAX_BLOCK,
2325 + ext3_ext_store_extent_cb);
2326 + up(&EXT3_I(inode)->truncate_sem);
2329 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2330 + struct ext3_extent_tree_stats buf;
2331 + struct ext3_extents_tree tree;
2333 + ext3_init_tree_desc(&tree, inode);
2334 + down(&EXT3_I(inode)->truncate_sem);
2335 + buf.depth = EXT_DEPTH(&tree);
2336 + buf.extents_num = 0;
2338 + tree.private = &buf;
2339 + err = ext3_ext_walk_space(&tree, 0, EXT_MAX_BLOCK,
2340 + ext3_ext_collect_stats_cb);
2341 + up(&EXT3_I(inode)->truncate_sem);
2343 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2344 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2345 + struct ext3_extents_tree tree;
2346 + ext3_init_tree_desc(&tree, inode);
2347 + down(&EXT3_I(inode)->truncate_sem);
2348 + err = EXT_DEPTH(&tree);
2349 + up(&EXT3_I(inode)->truncate_sem);
2355 +EXPORT_SYMBOL(ext3_init_tree_desc);
2356 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2357 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2358 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2359 +EXPORT_SYMBOL(ext3_ext_walk_space);
2360 +EXPORT_SYMBOL(ext3_ext_find_goal);
2361 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2362 Index: linux-2.6.5-sles9/fs/ext3/ialloc.c
2363 ===================================================================
2364 --- linux-2.6.5-sles9.orig/fs/ext3/ialloc.c 2005-02-23 01:01:52.366281264 +0300
2365 +++ linux-2.6.5-sles9/fs/ext3/ialloc.c 2005-02-23 01:02:37.398435336 +0300
2366 @@ -566,7 +566,7 @@ repeat:
2367 ei->i_dir_start_lookup = 0;
2370 - ei->i_flags = EXT3_I(dir)->i_flags & ~EXT3_INDEX_FL;
2371 + ei->i_flags = EXT3_I(dir)->i_flags & ~(EXT3_INDEX_FL|EXT3_EXTENTS_FL);
2373 ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
2374 /* dirsync only applies to directories */
2375 @@ -647,6 +647,18 @@
2376 DQUOT_FREE_INODE(inode);
2379 + if (test_opt(sb, EXTENTS) && S_ISREG(inode->i_mode)) {
2380 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2381 + ext3_extents_initialize_blockmap(handle, inode);
2382 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2383 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2384 + if (err) goto fail;
2385 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2386 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2387 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2391 err = ext3_mark_inode_dirty(handle, inode);
2393 ext3_std_error(sb, err);
2394 Index: linux-2.6.5-sles9/fs/ext3/inode.c
2395 ===================================================================
2396 --- linux-2.6.5-sles9.orig/fs/ext3/inode.c 2005-02-23 01:01:52.373280200 +0300
2397 +++ linux-2.6.5-sles9/fs/ext3/inode.c 2005-02-23 01:02:37.404434424 +0300
2398 @@ -796,6 +796,17 @@
2403 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2404 + struct buffer_head *bh, int create, int extend_disksize)
2406 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2407 + return ext3_ext_get_block(handle, inode, block, bh, create,
2409 + return ext3_get_block_handle(handle, inode, block, bh, create,
2413 static int ext3_get_block(struct inode *inode, sector_t iblock,
2414 struct buffer_head *bh_result, int create)
2417 handle = ext3_journal_current_handle();
2418 J_ASSERT(handle != 0);
2420 - ret = ext3_get_block_handle(handle, inode, iblock,
2421 - bh_result, create, 1);
2422 + ret = ext3_get_block_wrap(handle, inode, iblock,
2423 + bh_result, create, 1);
2431 - ret = ext3_get_block_handle(handle, inode, iblock,
2432 - bh_result, create, 0);
2433 + ret = ext3_get_block_wrap(handle, inode, iblock,
2434 + bh_result, create, 0);
2436 bh_result->b_size = (1 << inode->i_blkbits);
2440 dummy.b_blocknr = -1000;
2441 buffer_trace_init(&dummy.b_history);
2442 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2443 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2444 if (!*errp && buffer_mapped(&dummy)) {
2445 struct buffer_head *bh;
2446 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2447 @@ -1587,7 +1598,7 @@
2448 * This required during truncate. We need to physically zero the tail end
2449 * of that block so it doesn't yield old data if the file is later grown.
2451 -static int ext3_block_truncate_page(handle_t *handle, struct page *page,
2452 +int ext3_block_truncate_page(handle_t *handle, struct page *page,
2453 struct address_space *mapping, loff_t from)
2455 unsigned long index = from >> PAGE_CACHE_SHIFT;
2456 @@ -2083,6 +2094,9 @@
2460 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2461 + return ext3_ext_truncate(inode, page);
2463 handle = start_transaction(inode);
2464 if (IS_ERR(handle)) {
2466 @@ -2789,6 +2803,9 @@
2467 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2470 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2471 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2473 if (ext3_should_journal_data(inode))
2474 ret = 3 * (bpp + indirects) + 2;
2476 Index: linux-2.6.5-sles9/fs/ext3/Makefile
2477 ===================================================================
2478 --- linux-2.6.5-sles9.orig/fs/ext3/Makefile 2005-02-23 01:01:46.501172896 +0300
2479 +++ linux-2.6.5-sles9/fs/ext3/Makefile 2005-02-23 01:02:37.405434272 +0300
2481 obj-$(CONFIG_EXT3_FS) += ext3.o
2483 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2484 - ioctl.o namei.o super.o symlink.o hash.o
2485 + ioctl.o namei.o super.o symlink.o hash.o \
2488 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2489 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2490 Index: linux-2.6.5-sles9/fs/ext3/super.c
2491 ===================================================================
2492 --- linux-2.6.5-sles9.orig/fs/ext3/super.c 2005-02-23 01:02:34.072940888 +0300
2493 +++ linux-2.6.5-sles9/fs/ext3/super.c 2005-02-23 01:47:15.291333736 +0300
2495 struct ext3_super_block *es = sbi->s_es;
2498 + ext3_ext_release(sb);
2499 ext3_xattr_put_super(sb);
2500 journal_destroy(sbi->s_journal);
2501 if (!(sb->s_flags & MS_RDONLY)) {
2504 ei->i_rsv_window.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
2505 ei->vfs_inode.i_version = 1;
2507 + memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
2508 return &ei->vfs_inode;
2512 Opt_ignore, Opt_barrier,
2514 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2515 + Opt_extents, Opt_extdebug,
2518 static match_table_t tokens = {
2520 {Opt_iopen, "iopen"},
2521 {Opt_noiopen, "noiopen"},
2522 {Opt_iopen_nopriv, "iopen_nopriv"},
2523 + {Opt_extents, "extents"},
2524 + {Opt_extdebug, "extdebug"},
2525 {Opt_barrier, "barrier=%u"},
2528 @@ -797,6 +802,12 @@
2533 + set_opt (sbi->s_mount_opt, EXTENTS);
2535 + case Opt_extdebug:
2536 + set_opt (sbi->s_mount_opt, EXTDEBUG);
2540 "EXT3-fs: Unrecognized mount option \"%s\" "
2541 @@ -1449,6 +1460,8 @@
2542 percpu_counter_mod(&sbi->s_dirs_counter,
2543 ext3_count_dirs(sb));
2545 + ext3_ext_init(sb);
2550 Index: linux-2.6.5-sles9/fs/ext3/ioctl.c
2551 ===================================================================
2552 --- linux-2.6.5-sles9.orig/fs/ext3/ioctl.c 2005-02-23 01:01:42.887722224 +0300
2553 +++ linux-2.6.5-sles9/fs/ext3/ioctl.c 2005-02-23 01:02:37.412433208 +0300
2554 @@ -124,6 +124,10 @@
2555 err = ext3_change_inode_journal_flag(inode, jflag);
2558 + case EXT3_IOC_GET_EXTENTS:
2559 + case EXT3_IOC_GET_TREE_STATS:
2560 + case EXT3_IOC_GET_TREE_DEPTH:
2561 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2562 case EXT3_IOC_GETVERSION:
2563 case EXT3_IOC_GETVERSION_OLD:
2564 return put_user(inode->i_generation, (int *) arg);
2565 Index: linux-2.6.5-sles9/include/linux/ext3_fs.h
2566 ===================================================================
2567 --- linux-2.6.5-sles9.orig/include/linux/ext3_fs.h 2005-02-23 01:02:35.823674736 +0300
2568 +++ linux-2.6.5-sles9/include/linux/ext3_fs.h 2005-02-23 01:02:37.414432904 +0300
2570 #define EXT3_NOTAIL_FL 0x00008000 /* don't merge file tail */
2571 #define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2572 #define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2573 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2574 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2576 -#define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
2577 +#define EXT3_FL_USER_VISIBLE 0x000BDFFF /* User visible flags */
2578 #define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
2582 #define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
2583 #define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
2584 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 7, long)
2585 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 8, long)
2586 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 9, long)
2589 * Structure of an inode on the disk
2591 #define EXT3_MOUNT_BARRIER 0x20000 /* Use block barriers */
2592 #define EXT3_MOUNT_IOPEN 0x80000 /* Allow access via iopen */
2593 #define EXT3_MOUNT_IOPEN_NOPRIV 0x100000/* Make iopen world-readable */
2594 +#define EXT3_MOUNT_EXTENTS 0x200000/* Extents support */
2595 +#define EXT3_MOUNT_EXTDEBUG 0x400000/* Extents debug */
2597 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2599 @@ -503,11 +509,13 @@
2600 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2601 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2602 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
2603 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2605 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2606 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2607 EXT3_FEATURE_INCOMPAT_RECOVER| \
2608 - EXT3_FEATURE_INCOMPAT_META_BG)
2609 + EXT3_FEATURE_INCOMPAT_META_BG| \
2610 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2611 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2612 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2613 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2618 +extern int ext3_block_truncate_page(handle_t *, struct page *, struct address_space *, loff_t);
2619 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2620 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2621 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2622 @@ -802,6 +809,16 @@
2623 extern struct inode_operations ext3_symlink_inode_operations;
2624 extern struct inode_operations ext3_fast_symlink_inode_operations;
2627 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2628 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2629 + struct buffer_head *, int, int);
2630 +extern void ext3_ext_truncate(struct inode *, struct page *);
2631 +extern void ext3_ext_init(struct super_block *);
2632 +extern void ext3_ext_release(struct super_block *);
2633 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2634 +extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
2635 + unsigned int cmd, unsigned long arg);
2637 #endif /* __KERNEL__ */
2639 Index: linux-2.6.5-sles9/include/linux/ext3_extents.h
2640 ===================================================================
2641 --- linux-2.6.5-sles9.orig/include/linux/ext3_extents.h 2005-02-17 22:07:57.023609040 +0300
2642 +++ linux-2.6.5-sles9/include/linux/ext3_extents.h 2005-02-23 01:02:37.416432600 +0300
2645 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
2646 + * Written by Alex Tomas <alex@clusterfs.com>
2648 + * This program is free software; you can redistribute it and/or modify
2649 + * it under the terms of the GNU General Public License version 2 as
2650 + * published by the Free Software Foundation.
2652 + * This program is distributed in the hope that it will be useful,
2653 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2654 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2655 + * GNU General Public License for more details.
2657 + * You should have received a copy of the GNU General Public Licens
2658 + * along with this program; if not, write to the Free Software
2659 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2662 +#ifndef _LINUX_EXT3_EXTENTS
2663 +#define _LINUX_EXT3_EXTENTS
2666 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2667 + * become very little, so index split, in-depth growing and
2668 + * other hard changes happens much more often
2669 + * this is for debug purposes only
2671 +#define AGRESSIVE_TEST_
2674 + * if CHECK_BINSEARCH defined, then results of binary search
2675 + * will be checked by linear search
2677 +#define CHECK_BINSEARCH_
2680 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2681 + * to get lots of info what's going on
2685 +#define ext_debug(tree,fmt,a...) \
2687 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2688 + printk(fmt, ##a); \
2691 +#define ext_debug(tree,fmt,a...)
2695 + * if EXT_STATS is defined then stats numbers are collected
2696 + * these number will be displayed at umount time
2701 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2704 + * ext3_inode has i_block array (total 60 bytes)
2705 + * first 4 bytes are used to store:
2706 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2707 + * - number of alive extents in the inode
2711 + * this is extent on-disk structure
2712 + * it's used at the bottom of the tree
2714 +struct ext3_extent {
2715 + __u32 ee_block; /* first logical block extent covers */
2716 + __u16 ee_len; /* number of blocks covered by extent */
2717 + __u16 ee_start_hi; /* high 16 bits of physical block */
2718 + __u32 ee_start; /* low 32 bigs of physical block */
2722 + * this is index on-disk structure
2723 + * it's used at all the levels, but the bottom
2725 +struct ext3_extent_idx {
2726 + __u32 ei_block; /* index covers logical blocks from 'block' */
2727 + __u32 ei_leaf; /* pointer to the physical block of the next *
2728 + * level. leaf or next index could bet here */
2729 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
2734 + * each block (leaves and indexes), even inode-stored has header
2736 +struct ext3_extent_header {
2737 + __u16 eh_magic; /* probably will support different formats */
2738 + __u16 eh_entries; /* number of valid entries */
2739 + __u16 eh_max; /* capacity of store in entries */
2740 + __u16 eh_depth; /* has tree real underlaying blocks? */
2741 + __u32 eh_generation; /* flags(8 bits) | generation of the tree */
2744 +#define EXT3_EXT_MAGIC 0xf30a
2747 + * array of ext3_ext_path contains path to some extent
2748 + * creation/lookup routines use it for traversal/splitting/etc
2749 + * truncate uses it to simulate recursive walking
2751 +struct ext3_ext_path {
2754 + struct ext3_extent *p_ext;
2755 + struct ext3_extent_idx *p_idx;
2756 + struct ext3_extent_header *p_hdr;
2757 + struct buffer_head *p_bh;
2761 + * structure for external API
2765 + * storage for cached extent
2767 +struct ext3_ext_cache {
2774 +#define EXT3_EXT_CACHE_NO 0
2775 +#define EXT3_EXT_CACHE_GAP 1
2776 +#define EXT3_EXT_CACHE_EXTENT 2
2779 + * ext3_extents_tree is used to pass initial information
2780 + * to top-level extents API
2782 +struct ext3_extents_helpers;
2783 +struct ext3_extents_tree {
2784 + struct inode *inode; /* inode which tree belongs to */
2785 + void *root; /* ptr to data top of tree resides at */
2786 + void *buffer; /* will be passed as arg to ^^ routines */
2789 + struct ext3_ext_cache *cex;/* last found extent */
2790 + struct ext3_extents_helpers *ops;
2793 +struct ext3_extents_helpers {
2794 + int (*get_write_access)(handle_t *h, void *buffer);
2795 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2796 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2797 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2798 + struct ext3_extent *, unsigned long,
2800 + int (*remove_extent)(struct ext3_extents_tree *,
2801 + struct ext3_extent *, unsigned long,
2803 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2804 + struct ext3_ext_path *, struct ext3_extent *,
2809 + * to be called by ext3_ext_walk_space()
2810 + * negative retcode - error
2811 + * positive retcode - signal for ext3_ext_walk_space(), see below
2812 + * callback must return valid extent (passed or newly created)
2814 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2815 + struct ext3_ext_path *,
2816 + struct ext3_ext_cache *);
2818 +#define EXT_CONTINUE 0
2819 +#define EXT_BREAK 1
2820 +#define EXT_REPEAT 2
2823 +#define EXT_MAX_BLOCK 0xffffffff
2826 +#define EXT_FIRST_EXTENT(__hdr__) \
2827 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2828 + sizeof(struct ext3_extent_header)))
2829 +#define EXT_FIRST_INDEX(__hdr__) \
2830 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2831 + sizeof(struct ext3_extent_header)))
2832 +#define EXT_HAS_FREE_INDEX(__path__) \
2833 + ((__path__)->p_hdr->eh_entries < (__path__)->p_hdr->eh_max)
2834 +#define EXT_LAST_EXTENT(__hdr__) \
2835 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_entries - 1)
2836 +#define EXT_LAST_INDEX(__hdr__) \
2837 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_entries - 1)
2838 +#define EXT_MAX_EXTENT(__hdr__) \
2839 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_max - 1)
2840 +#define EXT_MAX_INDEX(__hdr__) \
2841 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_max - 1)
2842 +#define EXT_GENERATION(__hdr__) ((__hdr__)->eh_generation & 0x00ffffff)
2843 +#define EXT_FLAGS(__hdr__) ((__hdr__)->eh_generation >> 24)
2844 +#define EXT_FLAGS_CLR_UNKNOWN 0x7 /* Flags cleared on modification */
2846 +#define EXT_BLOCK_HDR(__bh__) ((struct ext3_extent_header *)(__bh__)->b_data)
2847 +#define EXT_ROOT_HDR(__tree__) ((struct ext3_extent_header *)(__tree__)->root)
2848 +#define EXT_DEPTH(__tree__) (EXT_ROOT_HDR(__tree__)->eh_depth)
2851 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2853 +#define EXT_CHECK_PATH(tree,path) \
2855 + int depth = EXT_DEPTH(tree); \
2856 + BUG_ON((unsigned long) (path) < __PAGE_OFFSET); \
2857 + BUG_ON((unsigned long) (path)[depth].p_idx < \
2858 + __PAGE_OFFSET && (path)[depth].p_idx != NULL); \
2859 + BUG_ON((unsigned long) (path)[depth].p_ext < \
2860 + __PAGE_OFFSET && (path)[depth].p_ext != NULL); \
2861 + BUG_ON((unsigned long) (path)[depth].p_hdr < __PAGE_OFFSET); \
2862 + BUG_ON((unsigned long) (path)[depth].p_bh < __PAGE_OFFSET \
2864 + BUG_ON((path)[0].p_depth != depth); \
2869 + * this structure is used to gather extents from the tree via ioctl
2871 +struct ext3_extent_buf {
2872 + unsigned long start;
2880 + * this structure is used to collect stats info about the tree
2882 +struct ext3_extent_tree_stats {
2888 +extern void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
2889 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2890 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2891 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2892 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2893 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2894 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2895 +extern int ext3_ext_calc_blockmap_metadata(struct inode *, int);
2898 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
2901 + tree->cex->ec_type = EXT3_EXT_CACHE_NO;
2905 +#endif /* _LINUX_EXT3_EXTENTS */
2906 Index: linux-2.6.5-sles9/include/linux/ext3_fs_i.h
2907 ===================================================================
2908 --- linux-2.6.5-sles9.orig/include/linux/ext3_fs_i.h 2005-02-23 01:01:52.425272296 +0300
2909 +++ linux-2.6.5-sles9/include/linux/ext3_fs_i.h 2005-02-23 01:45:55.611446920 +0300
2911 #include <linux/rwsem.h>
2912 #include <linux/rbtree.h>
2913 #include <linux/seqlock.h>
2914 +#include <linux/ext3_extents.h>
2916 struct reserve_window {
2917 __u32 _rsv_start; /* First byte reserved */
2920 struct semaphore truncate_sem;
2921 struct inode vfs_inode;
2923 + struct ext3_ext_cache i_cached_extent;
2926 #endif /* _LINUX_EXT3_FS_I */
2929 fs/ext3/Makefile | 2
2930 fs/ext3/extents.c | 2356 +++++++++++++++++++++++++++++++++++++++++++
2931 fs/ext3/ialloc.c | 4
2932 fs/ext3/inode.c | 29
2934 fs/ext3/super.c | 15
2935 include/linux/ext3_extents.h | 265 ++++
2936 include/linux/ext3_fs.h | 17
2937 include/linux/ext3_fs_i.h | 3
2938 9 files changed, 2687 insertions(+), 8 deletions(-)