2 Index: linux-2.6.5-7.311/fs/ext3/extents.c
3 ===================================================================
5 +++ linux-2.6.5-7.311/fs/ext3/extents.c
8 + * Copyright 2008 Sun Microsystems, Inc.
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_HDR_GEN(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 + ix->ei_leaf_hi = ix->ei_unused = 0;
565 + curp->p_hdr->eh_entries++;
567 + EXT_ASSERT(curp->p_hdr->eh_entries <= curp->p_hdr->eh_max);
568 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
570 + err = ext3_ext_dirty(handle, tree, curp);
571 + ext3_std_error(tree->inode->i_sb, err);
577 + * routine inserts new subtree into the path, using free index entry
579 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
580 + * - makes decision where to split
581 + * - moves remaining extens and index entries (right to the split point)
582 + * into the newly allocated blocks
583 + * - initialize subtree
585 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
586 + struct ext3_ext_path *path,
587 + struct ext3_extent *newext, int at)
589 + struct buffer_head *bh = NULL;
590 + int depth = EXT_DEPTH(tree);
591 + struct ext3_extent_header *neh;
592 + struct ext3_extent_idx *fidx;
593 + struct ext3_extent *ex;
594 + int i = at, k, m, a;
595 + unsigned long newblock, oldblock, border;
596 + int *ablocks = NULL; /* array of allocated blocks */
599 + /* make decision: where to split? */
600 + /* FIXME: now desicion is simplest: at current extent */
602 + /* if current leaf will be splitted, then we should use
603 + * border from split point */
604 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
605 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
606 + border = path[depth].p_ext[1].ee_block;
607 + ext_debug(tree, "leaf will be splitted."
608 + " next leaf starts at %d\n",
611 + border = newext->ee_block;
612 + ext_debug(tree, "leaf will be added."
613 + " next leaf starts at %d\n",
618 + * if error occurs, then we break processing
619 + * and turn filesystem read-only. so, index won't
620 + * be inserted and tree will be in consistent
621 + * state. next mount will repair buffers too
625 + * get array to track all allocated blocks
626 + * we need this to handle errors and free blocks
629 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
632 + memset(ablocks, 0, sizeof(unsigned long) * depth);
634 + /* allocate all needed blocks */
635 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
636 + for (a = 0; a < depth - at; a++) {
637 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
640 + ablocks[a] = newblock;
643 + /* initialize new leaf */
644 + newblock = ablocks[--a];
645 + EXT_ASSERT(newblock);
646 + bh = sb_getblk(tree->inode->i_sb, newblock);
653 + if ((err = ext3_journal_get_create_access(handle, bh)))
656 + neh = EXT_BLOCK_HDR(bh);
657 + neh->eh_entries = 0;
658 + neh->eh_max = ext3_ext_space_block(tree);
659 + neh->eh_magic = EXT3_EXT_MAGIC;
661 + ex = EXT_FIRST_EXTENT(neh);
663 + /* move remain of path[depth] to the new leaf */
664 + EXT_ASSERT(path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max);
665 + /* start copy from next extent */
666 + /* TODO: we could do it by single memmove */
668 + path[depth].p_ext++;
669 + while (path[depth].p_ext <=
670 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
671 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
672 + path[depth].p_ext->ee_block,
673 + path[depth].p_ext->ee_start,
674 + path[depth].p_ext->ee_len,
676 + memmove(ex++, path[depth].p_ext++, sizeof(struct ext3_extent));
680 + set_buffer_uptodate(bh);
683 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
688 + /* correct old leaf */
690 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
692 + path[depth].p_hdr->eh_entries -= m;
693 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
698 + /* create intermediate indexes */
699 + k = depth - at - 1;
700 + EXT_ASSERT(k >= 0);
702 + ext_debug(tree, "create %d intermediate indices\n", k);
703 + /* insert new index into current index block */
704 + /* current depth stored in i var */
707 + oldblock = newblock;
708 + newblock = ablocks[--a];
709 + bh = sb_getblk(tree->inode->i_sb, newblock);
716 + if ((err = ext3_journal_get_create_access(handle, bh)))
719 + neh = EXT_BLOCK_HDR(bh);
720 + neh->eh_entries = 1;
721 + neh->eh_magic = EXT3_EXT_MAGIC;
722 + neh->eh_max = ext3_ext_space_block_idx(tree);
723 + neh->eh_depth = depth - i;
724 + fidx = EXT_FIRST_INDEX(neh);
725 + fidx->ei_block = border;
726 + fidx->ei_leaf = oldblock;
727 + fidx->ei_leaf_hi = fidx->ei_unused = 0;
729 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
730 + i, newblock, border, oldblock);
735 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
736 + EXT_MAX_INDEX(path[i].p_hdr));
737 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
738 + EXT_LAST_INDEX(path[i].p_hdr));
739 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
740 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
741 + i, path[i].p_idx->ei_block,
742 + path[i].p_idx->ei_leaf, newblock);
743 + memmove(++fidx, path[i].p_idx++,
744 + sizeof(struct ext3_extent_idx));
746 + EXT_ASSERT(neh->eh_entries <= neh->eh_max);
749 + set_buffer_uptodate(bh);
752 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
757 + /* correct old index */
759 + err = ext3_ext_get_access(handle, tree, path + i);
762 + path[i].p_hdr->eh_entries -= m;
763 + err = ext3_ext_dirty(handle, tree, path + i);
771 + /* insert new index */
773 + err = ext3_ext_insert_index(handle, tree, path + at,
778 + if (buffer_locked(bh))
784 + /* free all allocated blocks in error case */
785 + for (i = 0; i < depth; i++) {
788 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
797 + * routine implements tree growing procedure:
798 + * - allocates new block
799 + * - moves top-level data (index block or leaf) into the new block
800 + * - initialize new top-level, creating index that points to the
801 + * just created block
803 +static int ext3_ext_grow_indepth(handle_t *handle,
804 + struct ext3_extents_tree *tree,
805 + struct ext3_ext_path *path,
806 + struct ext3_extent *newext)
808 + struct ext3_ext_path *curp = path;
809 + struct ext3_extent_header *neh;
810 + struct ext3_extent_idx *fidx;
811 + struct buffer_head *bh;
812 + unsigned long newblock;
815 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
819 + bh = sb_getblk(tree->inode->i_sb, newblock);
822 + ext3_std_error(tree->inode->i_sb, err);
827 + if ((err = ext3_journal_get_create_access(handle, bh))) {
832 + /* move top-level index/leaf into new block */
833 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
835 + /* set size of new block */
836 + neh = EXT_BLOCK_HDR(bh);
837 + /* old root could have indexes or leaves
838 + * so calculate eh_max right way */
839 + if (EXT_DEPTH(tree))
840 + neh->eh_max = ext3_ext_space_block_idx(tree);
842 + neh->eh_max = ext3_ext_space_block(tree);
843 + neh->eh_magic = EXT3_EXT_MAGIC;
844 + set_buffer_uptodate(bh);
847 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
850 + /* create index in new top-level index: num,max,pointer */
851 + if ((err = ext3_ext_get_access(handle, tree, curp)))
854 + curp->p_hdr->eh_magic = EXT3_EXT_MAGIC;
855 + curp->p_hdr->eh_max = ext3_ext_space_root_idx(tree);
856 + curp->p_hdr->eh_entries = 1;
857 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
858 + /* FIXME: it works, but actually path[0] can be index */
859 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
860 + curp->p_idx->ei_leaf = newblock;
861 + curp->p_idx->ei_leaf_hi = curp->p_idx->ei_unused = 0;
863 + neh = EXT_ROOT_HDR(tree);
864 + fidx = EXT_FIRST_INDEX(neh);
865 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
866 + neh->eh_entries, neh->eh_max, fidx->ei_block, fidx->ei_leaf);
868 + neh->eh_depth = path->p_depth + 1;
869 + err = ext3_ext_dirty(handle, tree, curp);
877 + * routine finds empty index and adds new leaf. if no free index found
878 + * then it requests in-depth growing
880 +static int ext3_ext_create_new_leaf(handle_t *handle,
881 + struct ext3_extents_tree *tree,
882 + struct ext3_ext_path *path,
883 + struct ext3_extent *newext)
885 + struct ext3_ext_path *curp;
886 + int depth, i, err = 0;
889 + i = depth = EXT_DEPTH(tree);
891 + /* walk up to the tree and look for free index entry */
892 + curp = path + depth;
893 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
898 + /* we use already allocated block for index block
899 + * so, subsequent data blocks should be contigoues */
900 + if (EXT_HAS_FREE_INDEX(curp)) {
901 + /* if we found index with free entry, then use that
902 + * entry: create all needed subtree and add new leaf */
903 + err = ext3_ext_split(handle, tree, path, newext, i);
906 + ext3_ext_drop_refs(path);
907 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
909 + err = PTR_ERR(path);
911 + /* tree is full, time to grow in depth */
912 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
915 + ext3_ext_drop_refs(path);
916 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
918 + err = PTR_ERR(path);
921 + * only first (depth 0 -> 1) produces free space
922 + * in all other cases we have to split growed tree
924 + depth = EXT_DEPTH(tree);
925 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
926 + /* now we need split */
938 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
939 + * NOTE: it consider block number from index entry as
940 + * allocated block. thus, index entries have to be consistent
943 +static unsigned long
944 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
948 + EXT_ASSERT(path != NULL);
949 + depth = path->p_depth;
951 + if (depth == 0 && path->p_ext == NULL)
952 + return EXT_MAX_BLOCK;
954 + /* FIXME: what if index isn't full ?! */
955 + while (depth >= 0) {
956 + if (depth == path->p_depth) {
958 + if (path[depth].p_ext !=
959 + EXT_LAST_EXTENT(path[depth].p_hdr))
960 + return path[depth].p_ext[1].ee_block;
963 + if (path[depth].p_idx !=
964 + EXT_LAST_INDEX(path[depth].p_hdr))
965 + return path[depth].p_idx[1].ei_block;
970 + return EXT_MAX_BLOCK;
974 + * returns first allocated block from next leaf or EXT_MAX_BLOCK
976 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
977 + struct ext3_ext_path *path)
981 + EXT_ASSERT(path != NULL);
982 + depth = path->p_depth;
984 + /* zero-tree has no leaf blocks at all */
986 + return EXT_MAX_BLOCK;
988 + /* go to index block */
991 + while (depth >= 0) {
992 + if (path[depth].p_idx !=
993 + EXT_LAST_INDEX(path[depth].p_hdr))
994 + return path[depth].p_idx[1].ei_block;
998 + return EXT_MAX_BLOCK;
1002 + * if leaf gets modified and modified extent is first in the leaf
1003 + * then we have to correct all indexes above
1004 + * TODO: do we need to correct tree in all cases?
1006 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
1007 + struct ext3_ext_path *path)
1009 + struct ext3_extent_header *eh;
1010 + int depth = EXT_DEPTH(tree);
1011 + struct ext3_extent *ex;
1012 + unsigned long border;
1015 + eh = path[depth].p_hdr;
1016 + ex = path[depth].p_ext;
1021 + /* there is no tree at all */
1025 + if (ex != EXT_FIRST_EXTENT(eh)) {
1026 + /* we correct tree if first leaf got modified only */
1031 + * TODO: we need correction if border is smaller then current one
1034 + border = path[depth].p_ext->ee_block;
1035 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1037 + path[k].p_idx->ei_block = border;
1038 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1042 + /* change all left-side indexes */
1043 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1045 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1047 + path[k].p_idx->ei_block = border;
1048 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1056 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1057 + struct ext3_extent *ex1,
1058 + struct ext3_extent *ex2)
1060 + if (ex1->ee_block + ex1->ee_len != ex2->ee_block)
1063 +#ifdef AGRESSIVE_TEST
1064 + if (ex1->ee_len >= 4)
1068 + if (!tree->ops->mergable)
1071 + return tree->ops->mergable(ex1, ex2);
1075 + * this routine tries to merge requsted extent into the existing
1076 + * extent or inserts requested extent as new one into the tree,
1077 + * creating new leaf in no-space case
1079 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1080 + struct ext3_ext_path *path,
1081 + struct ext3_extent *newext)
1083 + struct ext3_extent_header * eh;
1084 + struct ext3_extent *ex, *fex;
1085 + struct ext3_extent *nearex; /* nearest extent */
1086 + struct ext3_ext_path *npath = NULL;
1087 + int depth, len, err, next;
1089 + EXT_ASSERT(newext->ee_len > 0);
1090 + depth = EXT_DEPTH(tree);
1091 + ex = path[depth].p_ext;
1092 + EXT_ASSERT(path[depth].p_hdr);
1094 + /* try to insert block into found extent and return */
1095 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1096 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1097 + newext->ee_len, ex->ee_block, ex->ee_len,
1099 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1101 + ex->ee_len += newext->ee_len;
1102 + eh = path[depth].p_hdr;
1108 + depth = EXT_DEPTH(tree);
1109 + eh = path[depth].p_hdr;
1110 + if (eh->eh_entries < eh->eh_max)
1113 + /* probably next leaf has space for us? */
1114 + fex = EXT_LAST_EXTENT(eh);
1115 + next = ext3_ext_next_leaf_block(tree, path);
1116 + if (newext->ee_block > fex->ee_block && next != EXT_MAX_BLOCK) {
1117 + ext_debug(tree, "next leaf block - %d\n", next);
1118 + EXT_ASSERT(!npath);
1119 + npath = ext3_ext_find_extent(tree, next, NULL);
1120 + if (IS_ERR(npath))
1121 + return PTR_ERR(npath);
1122 + EXT_ASSERT(npath->p_depth == path->p_depth);
1123 + eh = npath[depth].p_hdr;
1124 + if (eh->eh_entries < eh->eh_max) {
1125 + ext_debug(tree, "next leaf isnt full(%d)\n",
1130 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1131 + eh->eh_entries, eh->eh_max);
1135 + * there is no free space in found leaf
1136 + * we're gonna add new leaf in the tree
1138 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1141 + depth = EXT_DEPTH(tree);
1142 + eh = path[depth].p_hdr;
1145 + nearex = path[depth].p_ext;
1147 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1151 + /* there is no extent in this leaf, create first one */
1152 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1153 + newext->ee_block, newext->ee_start,
1155 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1156 + } else if (newext->ee_block > nearex->ee_block) {
1157 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1158 + if (nearex != EXT_LAST_EXTENT(eh)) {
1159 + len = EXT_MAX_EXTENT(eh) - nearex;
1160 + len = (len - 1) * sizeof(struct ext3_extent);
1161 + len = len < 0 ? 0 : len;
1162 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1163 + "move %d from 0x%p to 0x%p\n",
1164 + newext->ee_block, newext->ee_start,
1166 + nearex, len, nearex + 1, nearex + 2);
1167 + memmove(nearex + 2, nearex + 1, len);
1169 + path[depth].p_ext = nearex + 1;
1171 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1172 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1173 + len = len < 0 ? 0 : len;
1174 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1175 + "move %d from 0x%p to 0x%p\n",
1176 + newext->ee_block, newext->ee_start, newext->ee_len,
1177 + nearex, len, nearex + 1, nearex + 2);
1178 + memmove(nearex + 1, nearex, len);
1179 + path[depth].p_ext = nearex;
1183 + nearex = path[depth].p_ext;
1184 + nearex->ee_block = newext->ee_block;
1185 + nearex->ee_start = newext->ee_start;
1186 + nearex->ee_len = newext->ee_len;
1187 + /* FIXME: support for large fs */
1188 + nearex->ee_start_hi = 0;
1191 + /* try to merge extents to the right */
1192 + while (nearex < EXT_LAST_EXTENT(eh)) {
1193 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1195 + /* merge with next extent! */
1196 + nearex->ee_len += nearex[1].ee_len;
1197 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1198 + len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1199 + sizeof(struct ext3_extent);
1200 + memmove(nearex + 1, nearex + 2, len);
1203 + EXT_ASSERT(eh->eh_entries > 0);
1206 + /* try to merge extents to the left */
1208 + /* time to correct all indexes above */
1209 + err = ext3_ext_correct_indexes(handle, tree, path);
1213 + err = ext3_ext_dirty(handle, tree, path + depth);
1217 + ext3_ext_drop_refs(npath);
1220 + ext3_ext_tree_changed(tree);
1221 + ext3_ext_invalidate_cache(tree);
1225 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1226 + unsigned long num, ext_prepare_callback func)
1228 + struct ext3_ext_path *path = NULL;
1229 + struct ext3_ext_cache cbex;
1230 + struct ext3_extent *ex;
1231 + unsigned long next, start = 0, end = 0;
1232 + unsigned long last = block + num;
1233 + int depth, exists, err = 0;
1237 + EXT_ASSERT(tree->inode);
1238 + EXT_ASSERT(tree->root);
1240 + while (block < last && block != EXT_MAX_BLOCK) {
1241 + num = last - block;
1242 + /* find extent for this block */
1243 + path = ext3_ext_find_extent(tree, block, path);
1244 + if (IS_ERR(path)) {
1245 + err = PTR_ERR(path);
1250 + depth = EXT_DEPTH(tree);
1251 + EXT_ASSERT(path[depth].p_hdr);
1252 + ex = path[depth].p_ext;
1253 + next = ext3_ext_next_allocated_block(path);
1257 + /* there is no extent yet, so try to allocate
1258 + * all requested space */
1260 + end = block + num;
1261 + } else if (ex->ee_block > block) {
1262 + /* need to allocate space before found extent */
1264 + end = ex->ee_block;
1265 + if (block + num < end)
1266 + end = block + num;
1267 + } else if (block >= ex->ee_block + ex->ee_len) {
1268 + /* need to allocate space after found extent */
1270 + end = block + num;
1273 + } else if (block >= ex->ee_block) {
1275 + * some part of requested space is covered
1279 + end = ex->ee_block + ex->ee_len;
1280 + if (block + num < end)
1281 + end = block + num;
1286 + EXT_ASSERT(end > start);
1289 + cbex.ec_block = start;
1290 + cbex.ec_len = end - start;
1291 + cbex.ec_start = 0;
1292 + cbex.ec_type = EXT3_EXT_CACHE_GAP;
1294 + cbex.ec_block = ex->ee_block;
1295 + cbex.ec_len = ex->ee_len;
1296 + cbex.ec_start = ex->ee_start;
1297 + cbex.ec_type = EXT3_EXT_CACHE_EXTENT;
1300 + EXT_ASSERT(cbex.ec_len > 0);
1301 + EXT_ASSERT(path[depth].p_hdr);
1302 + err = func(tree, path, &cbex);
1303 + ext3_ext_drop_refs(path);
1307 + if (err == EXT_REPEAT)
1309 + else if (err == EXT_BREAK) {
1314 + if (EXT_DEPTH(tree) != depth) {
1315 + /* depth was changed. we have to realloc path */
1320 + block = cbex.ec_block + cbex.ec_len;
1324 + ext3_ext_drop_refs(path);
1332 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, __u32 block,
1333 + __u32 len, __u32 start, int type)
1335 + EXT_ASSERT(len > 0);
1337 + tree->cex->ec_type = type;
1338 + tree->cex->ec_block = block;
1339 + tree->cex->ec_len = len;
1340 + tree->cex->ec_start = start;
1345 + * this routine calculate boundaries of the gap requested block fits into
1346 + * and cache this gap
1349 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1350 + struct ext3_ext_path *path,
1351 + unsigned long block)
1353 + int depth = EXT_DEPTH(tree);
1354 + unsigned long lblock, len;
1355 + struct ext3_extent *ex;
1360 + ex = path[depth].p_ext;
1362 + /* there is no extent yet, so gap is [0;-] */
1364 + len = EXT_MAX_BLOCK;
1365 + ext_debug(tree, "cache gap(whole file):");
1366 + } else if (block < ex->ee_block) {
1368 + len = ex->ee_block - block;
1369 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1370 + (unsigned long) block,
1371 + (unsigned long) ex->ee_block,
1372 + (unsigned long) ex->ee_len);
1373 + } else if (block >= ex->ee_block + ex->ee_len) {
1374 + lblock = ex->ee_block + ex->ee_len;
1375 + len = ext3_ext_next_allocated_block(path);
1376 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1377 + (unsigned long) ex->ee_block,
1378 + (unsigned long) ex->ee_len,
1379 + (unsigned long) block);
1380 + EXT_ASSERT(len > lblock);
1381 + len = len - lblock;
1387 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) lblock, len);
1388 + ext3_ext_put_in_cache(tree, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1392 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1393 + struct ext3_extent *ex)
1395 + struct ext3_ext_cache *cex = tree->cex;
1397 + /* is there cache storage at all? */
1399 + return EXT3_EXT_CACHE_NO;
1401 + /* has cache valid data? */
1402 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1403 + return EXT3_EXT_CACHE_NO;
1405 + EXT_ASSERT(cex->ec_type == EXT3_EXT_CACHE_GAP ||
1406 + cex->ec_type == EXT3_EXT_CACHE_EXTENT);
1407 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1408 + ex->ee_block = cex->ec_block;
1409 + ex->ee_start = cex->ec_start;
1410 + ex->ee_start_hi = 0;
1411 + ex->ee_len = cex->ec_len;
1412 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1413 + (unsigned long) block,
1414 + (unsigned long) ex->ee_block,
1415 + (unsigned long) ex->ee_len,
1416 + (unsigned long) ex->ee_start);
1417 + return cex->ec_type;
1420 + /* not in cache */
1421 + return EXT3_EXT_CACHE_NO;
1425 + * routine removes index from the index block
1426 + * it's used in truncate case only. thus all requests are for
1427 + * last index in the block only
1429 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1430 + struct ext3_ext_path *path)
1432 + struct buffer_head *bh;
1435 + /* free index block */
1437 + EXT_ASSERT(path->p_hdr->eh_entries);
1438 + if ((err = ext3_ext_get_access(handle, tree, path)))
1440 + path->p_hdr->eh_entries--;
1441 + if ((err = ext3_ext_dirty(handle, tree, path)))
1443 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1444 + path->p_idx->ei_leaf);
1445 + bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
1446 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1447 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1451 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1452 + struct ext3_ext_path *path)
1454 + int depth = EXT_DEPTH(tree);
1458 + /* probably there is space in leaf? */
1459 + if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1464 + * the worste case we're expecting is creation of the
1465 + * new root (growing in depth) with index splitting
1466 + * for splitting we have to consider depth + 1 because
1467 + * previous growing could increase it
1469 + depth = depth + 1;
1472 + * growing in depth:
1473 + * block allocation + new root + old root
1475 + needed = EXT3_ALLOC_NEEDED + 2;
1477 + /* index split. we may need:
1478 + * allocate intermediate indexes and new leaf
1479 + * change two blocks at each level, but root
1480 + * modify root block (inode)
1482 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1488 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1489 + struct ext3_ext_path *path, unsigned long start,
1490 + unsigned long end)
1492 + struct ext3_extent *ex, tex;
1493 + struct ext3_ext_path *npath;
1494 + int depth, creds, err;
1496 + depth = EXT_DEPTH(tree);
1497 + ex = path[depth].p_ext;
1499 + EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1500 + EXT_ASSERT(ex->ee_block < start);
1502 + /* calculate tail extent */
1503 + tex.ee_block = end + 1;
1504 + EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1505 + tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1507 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1508 + handle = ext3_ext_journal_restart(handle, creds);
1509 + if (IS_ERR(handle))
1510 + return PTR_ERR(handle);
1512 + /* calculate head extent. use primary extent */
1513 + err = ext3_ext_get_access(handle, tree, path + depth);
1516 + ex->ee_len = start - ex->ee_block;
1517 + err = ext3_ext_dirty(handle, tree, path + depth);
1521 + /* FIXME: some callback to free underlying resource
1522 + * and correct ee_start? */
1523 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1524 + ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1526 + npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1527 + if (IS_ERR(npath))
1528 + return PTR_ERR(npath);
1529 + depth = EXT_DEPTH(tree);
1530 + EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1531 + EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1533 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1534 + ext3_ext_drop_refs(npath);
1541 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1542 + struct ext3_ext_path *path, unsigned long start,
1543 + unsigned long end)
1545 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1546 + int err = 0, correct_index = 0;
1547 + int depth = EXT_DEPTH(tree), credits;
1548 + struct ext3_extent_header *eh;
1549 + unsigned a, b, block, num;
1551 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1552 + if (!path[depth].p_hdr)
1553 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1554 + eh = path[depth].p_hdr;
1556 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1557 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1559 + /* find where to start removing */
1560 + le = ex = EXT_LAST_EXTENT(eh);
1561 + while (ex != EXT_FIRST_EXTENT(eh)) {
1562 + if (ex->ee_block <= end)
1567 + if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1568 + /* removal of internal part of the extent requested
1569 + * tail and head must be placed in different extent
1570 + * so, we have to insert one more extent */
1571 + path[depth].p_ext = ex;
1572 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1576 + while (ex >= EXT_FIRST_EXTENT(eh) && ex->ee_block + ex->ee_len > start) {
1577 + ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1578 + path[depth].p_ext = ex;
1580 + a = ex->ee_block > start ? ex->ee_block : start;
1581 + b = ex->ee_block + ex->ee_len - 1 < end ?
1582 + ex->ee_block + ex->ee_len - 1 : end;
1584 + ext_debug(tree, " border %u:%u\n", a, b);
1586 + if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1590 + } else if (a != ex->ee_block) {
1591 + /* remove tail of the extent */
1592 + block = ex->ee_block;
1594 + } else if (b != ex->ee_block + ex->ee_len - 1) {
1595 + /* remove head of the extent */
1599 + /* remove whole extent: excelent! */
1600 + block = ex->ee_block;
1602 + EXT_ASSERT(a == ex->ee_block &&
1603 + b == ex->ee_block + ex->ee_len - 1);
1606 + if (ex == EXT_FIRST_EXTENT(eh))
1607 + correct_index = 1;
1610 + if (correct_index)
1611 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1612 + if (tree->ops->remove_extent_credits)
1613 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1615 + handle = ext3_ext_journal_restart(handle, credits);
1616 + if (IS_ERR(handle)) {
1617 + err = PTR_ERR(handle);
1621 + err = ext3_ext_get_access(handle, tree, path + depth);
1625 + if (tree->ops->remove_extent)
1626 + err = tree->ops->remove_extent(tree, ex, a, b);
1631 + /* this extent is removed entirely mark slot unused */
1632 + ex->ee_start = ex->ee_start_hi = 0;
1637 + ex->ee_block = block;
1640 + err = ext3_ext_dirty(handle, tree, path + depth);
1644 + ext_debug(tree, "new extent: %u:%u:%u\n",
1645 + ex->ee_block, ex->ee_len, ex->ee_start);
1650 + /* reuse unused slots */
1652 + if (lu->ee_start) {
1654 + lu->ee_start = lu->ee_start_hi = 0;
1661 + if (correct_index && eh->eh_entries)
1662 + err = ext3_ext_correct_indexes(handle, tree, path);
1664 + /* if this leaf is free, then we should
1665 + * remove it from index block above */
1666 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1667 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1674 +static struct ext3_extent_idx *
1675 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1677 + struct ext3_extent_idx *ix;
1679 + ix = EXT_LAST_INDEX(hdr);
1680 + while (ix != EXT_FIRST_INDEX(hdr)) {
1681 + if (ix->ei_block <= block)
1689 + * returns 1 if current index have to be freed (even partial)
1692 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1694 + EXT_ASSERT(path->p_idx);
1696 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1700 + * if truncate on deeper level happened it it wasn't partial
1701 + * so we have to consider current index for truncation
1703 + if (path->p_hdr->eh_entries == path->p_block)
1708 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1709 + unsigned long start, unsigned long end)
1711 + struct inode *inode = tree->inode;
1712 + struct super_block *sb = inode->i_sb;
1713 + int depth = EXT_DEPTH(tree);
1714 + struct ext3_ext_path *path;
1716 + int i = 0, err = 0;
1718 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1720 + /* probably first extent we're gonna free will be last in block */
1721 + handle = ext3_journal_start(inode, depth + 1);
1722 + if (IS_ERR(handle))
1723 + return PTR_ERR(handle);
1725 + ext3_ext_invalidate_cache(tree);
1728 + * we start scanning from right side freeing all the blocks
1729 + * after i_size and walking into the deep
1731 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1732 + if (IS_ERR(path)) {
1733 + ext3_error(sb, __FUNCTION__, "Can't allocate path array");
1734 + ext3_journal_stop(handle);
1737 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1738 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1740 + while (i >= 0 && err == 0) {
1742 + /* this is leaf block */
1743 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1744 + /* root level have p_bh == NULL, brelse() eats this */
1745 + brelse(path[i].p_bh);
1750 + /* this is index block */
1751 + if (!path[i].p_hdr) {
1752 + ext_debug(tree, "initialize header\n");
1753 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1756 + EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1757 + EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1759 + if (!path[i].p_idx) {
1760 + /* this level hasn't touched yet */
1762 + ext3_ext_last_covered(path[i].p_hdr, end);
1763 + path[i].p_block = path[i].p_hdr->eh_entries + 1;
1764 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1765 + path[i].p_hdr, path[i].p_hdr->eh_entries);
1767 + /* we've already was here, see at next index */
1771 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1772 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1774 + if (ext3_ext_more_to_rm(path + i)) {
1775 + /* go to the next level */
1776 + ext_debug(tree, "move to level %d (block %d)\n",
1777 + i + 1, path[i].p_idx->ei_leaf);
1778 + memset(path + i + 1, 0, sizeof(*path));
1779 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1780 + if (!path[i+1].p_bh) {
1781 + /* should we reset i_size? */
1785 + /* put actual number of indexes to know is this
1786 + * number got changed at the next iteration */
1787 + path[i].p_block = path[i].p_hdr->eh_entries;
1790 + /* we finish processing this index, go up */
1791 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1792 + /* index is empty, remove it
1793 + * handle must be already prepared by the
1794 + * truncatei_leaf() */
1795 + err = ext3_ext_rm_idx(handle, tree, path + i);
1797 + /* root level have p_bh == NULL, brelse() eats this */
1798 + brelse(path[i].p_bh);
1800 + ext_debug(tree, "return to level %d\n", i);
1804 + /* TODO: flexible tree reduction should be here */
1805 + if (path->p_hdr->eh_entries == 0) {
1807 + * truncate to zero freed all the tree
1808 + * so, we need to correct eh_depth
1810 + err = ext3_ext_get_access(handle, tree, path);
1812 + EXT_ROOT_HDR(tree)->eh_depth = 0;
1813 + EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1814 + err = ext3_ext_dirty(handle, tree, path);
1817 + ext3_ext_tree_changed(tree);
1820 + ext3_journal_stop(handle);
1825 +int ext3_ext_calc_metadata_amount(struct ext3_extents_tree *tree, int blocks)
1827 + int lcap, icap, rcap, leafs, idxs, num;
1829 + rcap = ext3_ext_space_root(tree);
1830 + if (blocks <= rcap) {
1831 + /* all extents fit to the root */
1835 + rcap = ext3_ext_space_root_idx(tree);
1836 + lcap = ext3_ext_space_block(tree);
1837 + icap = ext3_ext_space_block_idx(tree);
1839 + num = leafs = (blocks + lcap - 1) / lcap;
1840 + if (leafs <= rcap) {
1841 + /* all pointers to leafs fit to the root */
1845 + /* ok. we need separate index block(s) to link all leaf blocks */
1846 + idxs = (leafs + icap - 1) / icap;
1849 + idxs = (idxs + icap - 1) / icap;
1850 + } while (idxs > rcap);
1856 + * called at mount time
1858 +void ext3_ext_init(struct super_block *sb)
1861 + * possible initialization would be here
1864 + if (test_opt(sb, EXTENTS)) {
1865 + printk("EXT3-fs: file extents enabled");
1866 +#ifdef AGRESSIVE_TEST
1867 + printk(", agressive tests");
1869 +#ifdef CHECK_BINSEARCH
1870 + printk(", check binsearch");
1877 + * called at umount time
1879 +void ext3_ext_release(struct super_block *sb)
1883 +/************************************************************************
1884 + * VFS related routines
1885 + ************************************************************************/
1887 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1889 + /* we use in-core data, not bh */
1893 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1895 + struct inode *inode = buffer;
1896 + return ext3_mark_inode_dirty(handle, inode);
1899 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1900 + struct ext3_extent *ex2)
1902 + /* FIXME: support for large fs */
1903 + if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1909 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1910 + struct ext3_extent *ex,
1911 + unsigned long from, unsigned long to)
1915 + /* at present, extent can't cross block group */;
1916 + needed = 4; /* bitmap + group desc + sb + inode */
1918 +#ifdef CONFIG_QUOTA
1919 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1925 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1926 + struct ext3_extent *ex,
1927 + unsigned long from, unsigned long to)
1929 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1930 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1931 + struct buffer_head *bh;
1934 + if (IS_ERR(handle))
1935 + return PTR_ERR(handle);
1936 + if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1937 + /* tail removal */
1938 + unsigned long num, start;
1939 + num = ex->ee_block + ex->ee_len - from;
1940 + start = ex->ee_start + ex->ee_len - num;
1941 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1943 + for (i = 0; i < num; i++) {
1944 + bh = sb_find_get_block(tree->inode->i_sb, start + i);
1945 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1947 + ext3_free_blocks(handle, tree->inode, start, num);
1948 + } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1949 + printk("strange request: removal %lu-%lu from %u:%u\n",
1950 + from, to, ex->ee_block, ex->ee_len);
1952 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1953 + from, to, ex->ee_block, ex->ee_len);
1955 + ext3_journal_stop(handle);
1959 +static int ext3_ext_find_goal(struct inode *inode,
1960 + struct ext3_ext_path *path, unsigned long block)
1962 + struct ext3_inode_info *ei = EXT3_I(inode);
1963 + unsigned long bg_start;
1964 + unsigned long colour;
1968 + struct ext3_extent *ex;
1969 + depth = path->p_depth;
1971 + /* try to predict block placement */
1972 + if ((ex = path[depth].p_ext))
1973 + return ex->ee_start + (block - ex->ee_block);
1975 + /* it looks index is empty
1976 + * try to find starting from index itself */
1977 + if (path[depth].p_bh)
1978 + return path[depth].p_bh->b_blocknr;
1981 + /* OK. use inode's group */
1982 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1983 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1984 + colour = (current->pid % 16) *
1985 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1986 + return bg_start + colour + block;
1989 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1990 + struct ext3_ext_path *path,
1991 + struct ext3_extent *ex, int *err)
1993 + struct inode *inode = tree->inode;
1994 + int newblock, goal;
1998 + EXT_ASSERT(ex->ee_start);
1999 + EXT_ASSERT(ex->ee_len);
2001 + /* reuse block from the extent to order data/metadata */
2002 + newblock = ex->ee_start++;
2004 + if (ex->ee_len == 0) {
2006 + /* allocate new block for the extent */
2007 + goal = ext3_ext_find_goal(inode, path, ex->ee_block);
2008 + ex->ee_start = ext3_new_block(handle, inode, goal, err);
2009 + ex->ee_start_hi = 0;
2010 + if (ex->ee_start == 0) {
2011 + /* error occured: restore old extent */
2012 + ex->ee_start = newblock;
2019 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
2020 + .get_write_access = ext3_get_inode_write_access,
2021 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
2022 + .mergable = ext3_ext_mergable,
2023 + .new_block = ext3_new_block_cb,
2024 + .remove_extent = ext3_remove_blocks,
2025 + .remove_extent_credits = ext3_remove_blocks_credits,
2028 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
2029 + struct inode *inode)
2031 + tree->inode = inode;
2032 + tree->root = (void *) EXT3_I(inode)->i_data;
2033 + tree->buffer = (void *) inode;
2034 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
2035 + tree->cex = &EXT3_I(inode)->i_cached_extent;
2036 + tree->ops = &ext3_blockmap_helpers;
2039 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2040 + long iblock, struct buffer_head *bh_result,
2041 + int create, int extend_disksize)
2043 + struct ext3_ext_path *path = NULL;
2044 + struct ext3_extent newex;
2045 + struct ext3_extent *ex;
2046 + int goal, newblock, err = 0, depth;
2047 + struct ext3_extents_tree tree;
2049 + __clear_bit(BH_New, &bh_result->b_state);
2050 + ext3_init_tree_desc(&tree, inode);
2051 + ext_debug(&tree, "block %d requested for inode %u\n",
2052 + (int) iblock, (unsigned) inode->i_ino);
2053 + down(&EXT3_I(inode)->truncate_sem);
2055 + /* check in cache */
2056 + if ((goal = ext3_ext_in_cache(&tree, iblock, &newex))) {
2057 + if (goal == EXT3_EXT_CACHE_GAP) {
2059 + /* block isn't allocated yet and
2060 + * user don't want to allocate it */
2063 + /* we should allocate requested block */
2064 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2065 + /* block is already allocated */
2066 + newblock = iblock - newex.ee_block + newex.ee_start;
2073 + /* find extent for this block */
2074 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2075 + if (IS_ERR(path)) {
2076 + err = PTR_ERR(path);
2081 + depth = EXT_DEPTH(&tree);
2084 + * consistent leaf must not be empty
2085 + * this situations is possible, though, _during_ tree modification
2086 + * this is why assert can't be put in ext3_ext_find_extent()
2088 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2090 + if ((ex = path[depth].p_ext)) {
2091 + /* if found exent covers block, simple return it */
2092 + if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2093 + newblock = iblock - ex->ee_block + ex->ee_start;
2094 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2095 + (int) iblock, ex->ee_block, ex->ee_len,
2097 + ext3_ext_put_in_cache(&tree, ex->ee_block,
2098 + ex->ee_len, ex->ee_start,
2099 + EXT3_EXT_CACHE_EXTENT);
2105 + * requested block isn't allocated yet
2106 + * we couldn't try to create block if create flag is zero
2109 + /* put just found gap into cache to speedup subsequest reqs */
2110 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2114 + /* allocate new block */
2115 + goal = ext3_ext_find_goal(inode, path, iblock);
2116 + newblock = ext3_new_block(handle, inode, goal, &err);
2119 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2122 + /* try to insert new extent into found leaf and return */
2123 + newex.ee_block = iblock;
2124 + newex.ee_start = newblock;
2125 + newex.ee_start_hi = 0;
2127 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2131 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2132 + EXT3_I(inode)->i_disksize = inode->i_size;
2134 + /* previous routine could use block we allocated */
2135 + newblock = newex.ee_start;
2136 + __set_bit(BH_New, &bh_result->b_state);
2138 + ext3_ext_put_in_cache(&tree, newex.ee_block, newex.ee_len,
2139 + newex.ee_start, EXT3_EXT_CACHE_EXTENT);
2141 + ext3_ext_show_leaf(&tree, path);
2142 + __set_bit(BH_Mapped, &bh_result->b_state);
2143 + bh_result->b_bdev = inode->i_sb->s_bdev;
2144 + bh_result->b_blocknr = newblock;
2147 + ext3_ext_drop_refs(path);
2150 + up(&EXT3_I(inode)->truncate_sem);
2155 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2157 + struct address_space *mapping = inode->i_mapping;
2158 + struct super_block *sb = inode->i_sb;
2159 + struct ext3_extents_tree tree;
2160 + unsigned long last_block;
2164 + ext3_init_tree_desc(&tree, inode);
2167 + * probably first extent we're gonna free will be last in block
2169 + err = ext3_writepage_trans_blocks(inode) + 3;
2170 + handle = ext3_journal_start(inode, err);
2171 + if (IS_ERR(handle)) {
2173 + clear_highpage(page);
2174 + flush_dcache_page(page);
2175 + unlock_page(page);
2176 + page_cache_release(page);
2182 + ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2184 + down(&EXT3_I(inode)->truncate_sem);
2185 + ext3_ext_invalidate_cache(&tree);
2188 + * TODO: optimization is possible here
2189 + * probably we need not scaning at all,
2190 + * because page truncation is enough
2192 + if (ext3_orphan_add(handle, inode))
2195 + /* we have to know where to truncate from in crash case */
2196 + EXT3_I(inode)->i_disksize = inode->i_size;
2197 + ext3_mark_inode_dirty(handle, inode);
2199 + last_block = (inode->i_size + sb->s_blocksize - 1) >>
2200 + EXT3_BLOCK_SIZE_BITS(sb);
2201 + err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2203 + /* In a multi-transaction truncate, we only make the final
2204 + * transaction synchronous */
2205 + if (IS_SYNC(inode))
2206 + handle->h_sync = 1;
2210 + * If this was a simple ftruncate(), and the file will remain alive
2211 + * then we need to clear up the orphan record which we created above.
2212 + * However, if this was a real unlink then we were called by
2213 + * ext3_delete_inode(), and we allow that function to clean up the
2214 + * orphan info for us.
2216 + if (inode->i_nlink)
2217 + ext3_orphan_del(handle, inode);
2219 + up(&EXT3_I(inode)->truncate_sem);
2220 + ext3_journal_stop(handle);
2224 + * this routine calculate max number of blocks we could modify
2225 + * in order to allocate new block for an inode
2227 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2229 + struct ext3_extents_tree tree;
2232 + ext3_init_tree_desc(&tree, inode);
2234 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2236 + /* caller want to allocate num blocks */
2239 +#ifdef CONFIG_QUOTA
2241 + * FIXME: real calculation should be here
2242 + * it depends on blockmap format of qouta file
2244 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2250 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2252 + struct ext3_extents_tree tree;
2254 + ext3_init_tree_desc(&tree, inode);
2255 + ext3_extent_tree_init(handle, &tree);
2258 +int ext3_ext_calc_blockmap_metadata(struct inode *inode, int blocks)
2260 + struct ext3_extents_tree tree;
2262 + ext3_init_tree_desc(&tree, inode);
2263 + return ext3_ext_calc_metadata_amount(&tree, blocks);
2266 +EXPORT_SYMBOL(ext3_init_tree_desc);
2267 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2268 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2269 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2270 +EXPORT_SYMBOL(ext3_ext_walk_space);
2271 +EXPORT_SYMBOL(ext3_ext_find_goal);
2272 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2273 Index: linux-2.6.5-7.311/fs/ext3/ialloc.c
2274 ===================================================================
2275 --- linux-2.6.5-7.311.orig/fs/ext3/ialloc.c
2276 +++ linux-2.6.5-7.311/fs/ext3/ialloc.c
2277 @@ -603,7 +603,7 @@ got:
2278 ei->i_dir_start_lookup = 0;
2281 - ei->i_flags = EXT3_I(dir)->i_flags & ~EXT3_INDEX_FL;
2282 + ei->i_flags = EXT3_I(dir)->i_flags & ~(EXT3_INDEX_FL|EXT3_EXTENTS_FL);
2284 ei->i_flags &= ~(EXT3_IMMUTABLE_FL|EXT3_APPEND_FL);
2285 /* dirsync only applies to directories */
2286 @@ -648,6 +648,18 @@ got:
2287 DQUOT_FREE_INODE(inode);
2290 + if (test_opt(sb, EXTENTS) && S_ISREG(inode->i_mode)) {
2291 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2292 + ext3_extents_initialize_blockmap(handle, inode);
2293 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2294 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2295 + if (err) goto fail;
2296 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2297 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2298 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2302 err = ext3_mark_inode_dirty(handle, inode);
2304 ext3_std_error(sb, err);
2305 Index: linux-2.6.5-7.311/fs/ext3/inode.c
2306 ===================================================================
2307 --- linux-2.6.5-7.311.orig/fs/ext3/inode.c
2308 +++ linux-2.6.5-7.311/fs/ext3/inode.c
2309 @@ -798,6 +798,17 @@ changed:
2314 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2315 + struct buffer_head *bh, int create, int extend_disksize)
2317 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2318 + return ext3_ext_get_block(handle, inode, block, bh, create,
2320 + return ext3_get_block_handle(handle, inode, block, bh, create,
2324 static int ext3_get_block(struct inode *inode, sector_t iblock,
2325 struct buffer_head *bh_result, int create)
2327 @@ -808,8 +819,8 @@ static int ext3_get_block(struct inode *
2328 handle = ext3_journal_current_handle();
2329 J_ASSERT(handle != 0);
2331 - ret = ext3_get_block_handle(handle, inode, iblock,
2332 - bh_result, create, 1);
2333 + ret = ext3_get_block_wrap(handle, inode, iblock,
2334 + bh_result, create, 1);
2338 @@ -836,8 +847,8 @@ ext3_direct_io_get_blocks(struct inode *
2342 - ret = ext3_get_block_handle(handle, inode, iblock,
2343 - bh_result, create, 0);
2344 + ret = ext3_get_block_wrap(handle, inode, iblock,
2345 + bh_result, create, 0);
2347 bh_result->b_size = (1 << inode->i_blkbits);
2349 @@ -858,7 +869,7 @@ struct buffer_head *ext3_getblk(handle_t
2351 dummy.b_blocknr = -1000;
2352 buffer_trace_init(&dummy.b_history);
2353 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2354 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2355 if (!*errp && buffer_mapped(&dummy)) {
2356 struct buffer_head *bh;
2357 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2358 @@ -1604,7 +1615,7 @@ void ext3_set_aops(struct inode *inode)
2359 * This required during truncate. We need to physically zero the tail end
2360 * of that block so it doesn't yield old data if the file is later grown.
2362 -static int ext3_block_truncate_page(handle_t *handle, struct page *page,
2363 +int ext3_block_truncate_page(handle_t *handle, struct page *page,
2364 struct address_space *mapping, loff_t from)
2366 unsigned long index = from >> PAGE_CACHE_SHIFT;
2367 @@ -2100,6 +2111,9 @@ void ext3_truncate(struct inode * inode)
2371 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2372 + return ext3_ext_truncate(inode, page);
2374 handle = start_transaction(inode);
2375 if (IS_ERR(handle)) {
2377 @@ -2806,6 +2820,9 @@ int ext3_writepage_trans_blocks(struct i
2378 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2381 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2382 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2384 if (ext3_should_journal_data(inode))
2385 ret = 3 * (bpp + indirects) + 2;
2387 Index: linux-2.6.5-7.311/fs/ext3/Makefile
2388 ===================================================================
2389 --- linux-2.6.5-7.311.orig/fs/ext3/Makefile
2390 +++ linux-2.6.5-7.311/fs/ext3/Makefile
2392 obj-$(CONFIG_EXT3_FS) += ext3.o
2394 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2395 - ioctl.o namei.o super.o symlink.o hash.o
2396 + ioctl.o namei.o super.o symlink.o hash.o \
2399 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2400 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2401 Index: linux-2.6.5-7.311/fs/ext3/super.c
2402 ===================================================================
2403 --- linux-2.6.5-7.311.orig/fs/ext3/super.c
2404 +++ linux-2.6.5-7.311/fs/ext3/super.c
2405 @@ -389,6 +389,7 @@ void ext3_put_super (struct super_block
2406 struct ext3_super_block *es = sbi->s_es;
2409 + ext3_ext_release(sb);
2410 ext3_xattr_put_super(sb);
2411 journal_destroy(sbi->s_journal);
2412 if (!(sb->s_flags & MS_RDONLY)) {
2413 @@ -450,6 +451,8 @@ static struct inode *ext3_alloc_inode(st
2415 ei->i_rsv_window.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
2416 ei->vfs_inode.i_version = 1;
2418 + memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
2419 return &ei->vfs_inode;
2422 @@ -584,6 +587,7 @@ enum {
2423 Opt_ignore, Opt_barrier,
2425 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2426 + Opt_extents, Opt_noextents, Opt_extdebug,
2429 static match_table_t tokens = {
2430 @@ -627,6 +631,9 @@ static match_table_t tokens = {
2431 {Opt_iopen, "iopen"},
2432 {Opt_noiopen, "noiopen"},
2433 {Opt_iopen_nopriv, "iopen_nopriv"},
2434 + {Opt_extents, "extents"},
2435 + {Opt_noextents, "noextents"},
2436 + {Opt_extdebug, "extdebug"},
2437 {Opt_barrier, "barrier=%u"},
2440 @@ -843,6 +850,15 @@ static int parse_options (char * options
2445 + set_opt (sbi->s_mount_opt, EXTENTS);
2447 + case Opt_noextents:
2448 + clear_opt (sbi->s_mount_opt, EXTENTS);
2450 + case Opt_extdebug:
2451 + set_opt (sbi->s_mount_opt, EXTDEBUG);
2455 "EXT3-fs: Unrecognized mount option \"%s\" "
2456 @@ -1495,6 +1511,8 @@ static int ext3_fill_super (struct super
2457 percpu_counter_mod(&sbi->s_dirs_counter,
2458 ext3_count_dirs(sb));
2460 + ext3_ext_init(sb);
2465 Index: linux-2.6.5-7.311/include/linux/ext3_fs.h
2466 ===================================================================
2467 --- linux-2.6.5-7.311.orig/include/linux/ext3_fs.h
2468 +++ linux-2.6.5-7.311/include/linux/ext3_fs.h
2469 @@ -185,9 +185,10 @@ struct ext3_group_desc
2470 #define EXT3_NOTAIL_FL 0x00008000 /* file tail should not be merged */
2471 #define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2472 #define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2473 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2474 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2476 -#define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
2477 +#define EXT3_FL_USER_VISIBLE 0x000BDFFF /* User visible flags */
2478 #define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
2481 @@ -333,6 +334,8 @@ struct ext3_inode {
2482 #define EXT3_MOUNT_BARRIER 0x20000 /* Use block barriers */
2483 #define EXT3_MOUNT_IOPEN 0x80000 /* Allow access via iopen */
2484 #define EXT3_MOUNT_IOPEN_NOPRIV 0x100000/* Make iopen world-readable */
2485 +#define EXT3_MOUNT_EXTENTS 0x200000/* Extents support */
2486 +#define EXT3_MOUNT_EXTDEBUG 0x400000/* Extents debug */
2488 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2490 @@ -521,11 +524,13 @@ static inline struct ext3_inode_info *EX
2491 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2492 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2493 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
2494 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2496 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2497 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2498 EXT3_FEATURE_INCOMPAT_RECOVER| \
2499 - EXT3_FEATURE_INCOMPAT_META_BG)
2500 + EXT3_FEATURE_INCOMPAT_META_BG| \
2501 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2502 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2503 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2504 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2505 @@ -729,6 +734,9 @@ extern unsigned long ext3_count_free (st
2509 +extern int ext3_block_truncate_page(handle_t *, struct page *,
2510 + struct address_space *, loff_t);
2511 +extern int ext3_writepage_trans_blocks(struct inode *inode);
2512 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2513 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2514 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2515 @@ -802,6 +810,14 @@ extern struct inode_operations ext3_spec
2516 extern struct inode_operations ext3_symlink_inode_operations;
2517 extern struct inode_operations ext3_fast_symlink_inode_operations;
2520 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2521 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2522 + struct buffer_head *, int, int);
2523 +extern void ext3_ext_truncate(struct inode *, struct page *);
2524 +extern void ext3_ext_init(struct super_block *);
2525 +extern void ext3_ext_release(struct super_block *);
2526 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2528 #endif /* __KERNEL__ */
2530 Index: linux-2.6.5-7.311/include/linux/ext3_extents.h
2531 ===================================================================
2533 +++ linux-2.6.5-7.311/include/linux/ext3_extents.h
2536 + * Copyright 2008 Sun Microsystems, Inc.
2537 + * Written by Alex Tomas <alex@clusterfs.com>
2539 + * This program is free software; you can redistribute it and/or modify
2540 + * it under the terms of the GNU General Public License version 2 as
2541 + * published by the Free Software Foundation.
2543 + * This program is distributed in the hope that it will be useful,
2544 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2545 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2546 + * GNU General Public License for more details.
2548 + * You should have received a copy of the GNU General Public Licens
2549 + * along with this program; if not, write to the Free Software
2550 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2553 +#ifndef _LINUX_EXT3_EXTENTS
2554 +#define _LINUX_EXT3_EXTENTS
2557 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2558 + * become very little, so index split, in-depth growing and
2559 + * other hard changes happens much more often
2560 + * this is for debug purposes only
2562 +#define AGRESSIVE_TEST_
2565 + * if CHECK_BINSEARCH defined, then results of binary search
2566 + * will be checked by linear search
2568 +#define CHECK_BINSEARCH_
2571 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2572 + * to get lots of info what's going on
2576 +#define ext_debug(tree,fmt,a...) \
2578 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2579 + printk(fmt, ##a); \
2582 +#define ext_debug(tree,fmt,a...)
2586 + * if EXT_STATS is defined then stats numbers are collected
2587 + * these number will be displayed at umount time
2592 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2595 + * ext3_inode has i_block array (total 60 bytes)
2596 + * first 4 bytes are used to store:
2597 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2598 + * - number of alive extents in the inode
2602 + * this is extent on-disk structure
2603 + * it's used at the bottom of the tree
2605 +struct ext3_extent {
2606 + __u32 ee_block; /* first logical block extent covers */
2607 + __u16 ee_len; /* number of blocks covered by extent */
2608 + __u16 ee_start_hi; /* high 16 bits of physical block */
2609 + __u32 ee_start; /* low 32 bigs of physical block */
2613 + * this is index on-disk structure
2614 + * it's used at all the levels, but the bottom
2616 +struct ext3_extent_idx {
2617 + __u32 ei_block; /* index covers logical blocks from 'block' */
2618 + __u32 ei_leaf; /* pointer to the physical block of the next *
2619 + * level. leaf or next index could bet here */
2620 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
2625 + * each block (leaves and indexes), even inode-stored has header
2627 +struct ext3_extent_header {
2628 + __u16 eh_magic; /* probably will support different formats */
2629 + __u16 eh_entries; /* number of valid entries */
2630 + __u16 eh_max; /* capacity of store in entries */
2631 + __u16 eh_depth; /* has tree real underlaying blocks? */
2632 + __u32 eh_generation; /* flags(8 bits) | generation of the tree */
2635 +#define EXT3_EXT_MAGIC 0xf30a
2638 + * array of ext3_ext_path contains path to some extent
2639 + * creation/lookup routines use it for traversal/splitting/etc
2640 + * truncate uses it to simulate recursive walking
2642 +struct ext3_ext_path {
2645 + struct ext3_extent *p_ext;
2646 + struct ext3_extent_idx *p_idx;
2647 + struct ext3_extent_header *p_hdr;
2648 + struct buffer_head *p_bh;
2652 + * structure for external API
2656 + * storage for cached extent
2658 +struct ext3_ext_cache {
2665 +#define EXT3_EXT_CACHE_NO 0
2666 +#define EXT3_EXT_CACHE_GAP 1
2667 +#define EXT3_EXT_CACHE_EXTENT 2
2670 + * ext3_extents_tree is used to pass initial information
2671 + * to top-level extents API
2673 +struct ext3_extents_helpers;
2674 +struct ext3_extents_tree {
2675 + struct inode *inode; /* inode which tree belongs to */
2676 + void *root; /* ptr to data top of tree resides at */
2677 + void *buffer; /* will be passed as arg to ^^ routines */
2680 + struct ext3_ext_cache *cex;/* last found extent */
2681 + struct ext3_extents_helpers *ops;
2684 +struct ext3_extents_helpers {
2685 + int (*get_write_access)(handle_t *h, void *buffer);
2686 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2687 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2688 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2689 + struct ext3_extent *, unsigned long,
2691 + int (*remove_extent)(struct ext3_extents_tree *,
2692 + struct ext3_extent *, unsigned long,
2694 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2695 + struct ext3_ext_path *, struct ext3_extent *,
2700 + * to be called by ext3_ext_walk_space()
2701 + * negative retcode - error
2702 + * positive retcode - signal for ext3_ext_walk_space(), see below
2703 + * callback must return valid extent (passed or newly created)
2705 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2706 + struct ext3_ext_path *,
2707 + struct ext3_ext_cache *);
2709 +#define EXT_CONTINUE 0
2710 +#define EXT_BREAK 1
2711 +#define EXT_REPEAT 2
2714 +#define EXT_MAX_BLOCK 0xffffffff
2717 +#define EXT_FIRST_EXTENT(__hdr__) \
2718 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2719 + sizeof(struct ext3_extent_header)))
2720 +#define EXT_FIRST_INDEX(__hdr__) \
2721 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2722 + sizeof(struct ext3_extent_header)))
2723 +#define EXT_HAS_FREE_INDEX(__path__) \
2724 + ((__path__)->p_hdr->eh_entries < (__path__)->p_hdr->eh_max)
2725 +#define EXT_LAST_EXTENT(__hdr__) \
2726 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_entries - 1)
2727 +#define EXT_LAST_INDEX(__hdr__) \
2728 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_entries - 1)
2729 +#define EXT_MAX_EXTENT(__hdr__) \
2730 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_max - 1)
2731 +#define EXT_MAX_INDEX(__hdr__) \
2732 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_max - 1)
2733 +#define EXT_HDR_GEN(__hdr__) ((__hdr__)->eh_generation & 0x00ffffff)
2734 +#define EXT_FLAGS(__hdr__) ((__hdr__)->eh_generation >> 24)
2735 +#define EXT_FLAGS_CLR_UNKNOWN 0x7 /* Flags cleared on modification */
2737 +#define EXT_BLOCK_HDR(__bh__) ((struct ext3_extent_header *)(__bh__)->b_data)
2738 +#define EXT_ROOT_HDR(__tree__) ((struct ext3_extent_header *)(__tree__)->root)
2739 +#define EXT_DEPTH(__tree__) (EXT_ROOT_HDR(__tree__)->eh_depth)
2740 +#define EXT_GENERATION(__tree__) EXT_HDR_GEN(EXT_ROOT_HDR(__tree__))
2742 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2744 +#define EXT_CHECK_PATH(tree,path) \
2746 + int depth = EXT_DEPTH(tree); \
2747 + BUG_ON((unsigned long) (path) < __PAGE_OFFSET); \
2748 + BUG_ON((unsigned long) (path)[depth].p_idx < \
2749 + __PAGE_OFFSET && (path)[depth].p_idx != NULL); \
2750 + BUG_ON((unsigned long) (path)[depth].p_ext < \
2751 + __PAGE_OFFSET && (path)[depth].p_ext != NULL); \
2752 + BUG_ON((unsigned long) (path)[depth].p_hdr < __PAGE_OFFSET); \
2753 + BUG_ON((unsigned long) (path)[depth].p_bh < __PAGE_OFFSET \
2755 + BUG_ON((path)[0].p_depth != depth); \
2760 + * this structure is used to gather extents from the tree via ioctl
2762 +struct ext3_extent_buf {
2763 + unsigned long start;
2771 + * this structure is used to collect stats info about the tree
2773 +struct ext3_extent_tree_stats {
2779 +extern void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
2780 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2781 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2782 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2783 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2784 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2785 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2786 +extern int ext3_ext_calc_blockmap_metadata(struct inode *, int);
2789 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
2792 + tree->cex->ec_type = EXT3_EXT_CACHE_NO;
2796 +#endif /* _LINUX_EXT3_EXTENTS */
2797 Index: linux-2.6.5-7.311/include/linux/ext3_fs_i.h
2798 ===================================================================
2799 --- linux-2.6.5-7.311.orig/include/linux/ext3_fs_i.h
2800 +++ linux-2.6.5-7.311/include/linux/ext3_fs_i.h
2802 #include <linux/rwsem.h>
2803 #include <linux/rbtree.h>
2804 #include <linux/seqlock.h>
2805 +#include <linux/ext3_extents.h>
2807 struct reserve_window {
2808 __u32 _rsv_start; /* First byte reserved */
2809 @@ -128,6 +129,8 @@ struct ext3_inode_info {
2811 struct semaphore truncate_sem;
2812 struct inode vfs_inode;
2814 + struct ext3_ext_cache i_cached_extent;
2817 #endif /* _LINUX_EXT3_FS_I */