2 Index: linux-2.6.5-sles9/fs/ext3/extents.c
3 ===================================================================
4 --- linux-2.6.5-sles9.orig/fs/ext3/extents.c 2003-01-30 13:24:37.000000000 +0300
5 +++ linux-2.6.5-sles9/fs/ext3/extents.c 2004-11-09 02:25:56.143726112 +0300
8 + * Copyright (c) 2003, 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>
52 +static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
56 + if (handle->h_buffer_credits > needed)
58 + if (!ext3_journal_extend(handle, needed))
60 + err = ext3_journal_restart(handle, needed);
66 +ext3_ext_get_access_for_root(handle_t *h, struct ext3_extents_tree *tree)
68 + if (tree->ops->get_write_access)
69 + return tree->ops->get_write_access(h,tree->buffer);
75 +ext3_ext_mark_root_dirty(handle_t *h, struct ext3_extents_tree *tree)
77 + if (tree->ops->mark_buffer_dirty)
78 + return tree->ops->mark_buffer_dirty(h,tree->buffer);
88 +static int ext3_ext_get_access(handle_t *handle,
89 + struct ext3_extents_tree *tree,
90 + struct ext3_ext_path *path)
95 + /* path points to block */
96 + err = ext3_journal_get_write_access(handle, path->p_bh);
98 + /* path points to leaf/index in inode body */
99 + err = ext3_ext_get_access_for_root(handle, tree);
110 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
111 + struct ext3_ext_path *path)
115 + /* path points to block */
116 + err =ext3_journal_dirty_metadata(handle, path->p_bh);
118 + /* path points to leaf/index in inode body */
119 + err = ext3_ext_mark_root_dirty(handle, tree);
125 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
126 + struct ext3_ext_path *path, struct ext3_extent *ex,
129 + int goal, depth, newblock;
130 + struct inode *inode;
133 + if (tree->ops->new_block)
134 + return tree->ops->new_block(handle, tree, path, ex, err);
136 + inode = tree->inode;
137 + depth = EXT_DEPTH(tree);
138 + if (path && depth > 0) {
139 + goal = path[depth-1].p_block;
141 + struct ext3_inode_info *ei = EXT3_I(inode);
142 + unsigned long bg_start;
143 + unsigned long colour;
145 + bg_start = (ei->i_block_group *
146 + EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
147 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
148 + colour = (current->pid % 16) *
149 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
150 + goal = bg_start + colour;
153 + newblock = ext3_new_block(handle, inode, goal, err);
157 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
159 + struct ext3_extent_header *neh;
160 + neh = EXT_ROOT_HDR(tree);
161 + neh->eh_generation++;
164 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
168 + size = (tree->inode->i_sb->s_blocksize -
169 + sizeof(struct ext3_extent_header))
170 + / sizeof(struct ext3_extent);
171 +#ifdef AGRESSIVE_TEST
177 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
181 + size = (tree->inode->i_sb->s_blocksize -
182 + sizeof(struct ext3_extent_header))
183 + / sizeof(struct ext3_extent_idx);
184 +#ifdef AGRESSIVE_TEST
190 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
194 + size = (tree->buffer_len - sizeof(struct ext3_extent_header))
195 + / sizeof(struct ext3_extent);
196 +#ifdef AGRESSIVE_TEST
202 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
206 + size = (tree->buffer_len -
207 + sizeof(struct ext3_extent_header))
208 + / sizeof(struct ext3_extent_idx);
209 +#ifdef AGRESSIVE_TEST
215 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
216 + struct ext3_ext_path *path)
219 + int k, l = path->p_depth;
221 + ext_debug(tree, "path:");
222 + for (k = 0; k <= l; k++, path++) {
224 + ext_debug(tree, " %d->%d", path->p_idx->ei_block,
225 + path->p_idx->ei_leaf);
226 + } else if (path->p_ext) {
227 + ext_debug(tree, " %d:%d:%d",
228 + path->p_ext->ee_block,
229 + path->p_ext->ee_len,
230 + path->p_ext->ee_start);
232 + ext_debug(tree, " []");
234 + ext_debug(tree, "\n");
238 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
239 + struct ext3_ext_path *path)
242 + int depth = EXT_DEPTH(tree);
243 + struct ext3_extent_header *eh;
244 + struct ext3_extent *ex;
250 + eh = path[depth].p_hdr;
251 + ex = EXT_FIRST_EXTENT(eh);
253 + for (i = 0; i < eh->eh_entries; i++, ex++) {
254 + ext_debug(tree, "%d:%d:%d ",
255 + ex->ee_block, ex->ee_len, ex->ee_start);
257 + ext_debug(tree, "\n");
261 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
263 + int depth = path->p_depth;
266 + for (i = 0; i <= depth; i++, path++)
268 + brelse(path->p_bh);
274 + * binary search for closest index by given block
277 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
278 + struct ext3_ext_path *path, int block)
280 + struct ext3_extent_header *eh = path->p_hdr;
281 + struct ext3_extent_idx *ix;
284 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
285 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
286 + EXT_ASSERT(eh->eh_entries > 0);
288 + ext_debug(tree, "binsearch for %d(idx): ", block);
290 + path->p_idx = ix = EXT_FIRST_INDEX(eh);
292 + r = k = eh->eh_entries;
295 + if (block < ix[l + k].ei_block)
299 + ext_debug(tree, "%d:%d:%d ", k, l, r);
304 + ext_debug(tree, " -> %d->%d ", path->p_idx->ei_block, path->p_idx->ei_leaf);
307 + if (block < ix->ei_block)
309 + path->p_idx = ix++;
311 + ext_debug(tree, " -> %d->%d\n", path->p_idx->ei_block,
312 + path->p_idx->ei_leaf);
314 +#ifdef CHECK_BINSEARCH
316 + struct ext3_extent_idx *chix;
318 + chix = ix = EXT_FIRST_INDEX(eh);
319 + for (k = 0; k < eh->eh_entries; k++, ix++) {
320 + if (k != 0 && ix->ei_block <= ix[-1].ei_block) {
321 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
322 + ix, EXT_FIRST_INDEX(eh));
323 + printk("%u <= %u\n",
324 + ix->ei_block,ix[-1].ei_block);
326 + EXT_ASSERT(k == 0 || ix->ei_block > ix[-1].ei_block);
327 + if (block < ix->ei_block)
331 + EXT_ASSERT(chix == path->p_idx);
338 + * binary search for closest extent by given block
341 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
342 + struct ext3_ext_path *path, int block)
344 + struct ext3_extent_header *eh = path->p_hdr;
345 + struct ext3_extent *ex;
348 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
349 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
351 + if (eh->eh_entries == 0) {
353 + * this leaf is empty yet:
354 + * we get such a leaf in split/add case
359 + ext_debug(tree, "binsearch for %d: ", block);
361 + path->p_ext = ex = EXT_FIRST_EXTENT(eh);
363 + r = k = eh->eh_entries;
366 + if (block < ex[l + k].ee_block)
370 + ext_debug(tree, "%d:%d:%d ", k, l, r);
375 + ext_debug(tree, " -> %d:%d:%d ", path->p_ext->ee_block,
376 + path->p_ext->ee_start, path->p_ext->ee_len);
379 + if (block < ex->ee_block)
381 + path->p_ext = ex++;
383 + ext_debug(tree, " -> %d:%d:%d\n", path->p_ext->ee_block,
384 + path->p_ext->ee_start, path->p_ext->ee_len);
386 +#ifdef CHECK_BINSEARCH
388 + struct ext3_extent *chex;
390 + chex = ex = EXT_FIRST_EXTENT(eh);
391 + for (k = 0; k < eh->eh_entries; k++, ex++) {
392 + EXT_ASSERT(k == 0 || ex->ee_block > ex[-1].ee_block);
393 + if (block < ex->ee_block)
397 + EXT_ASSERT(chex == path->p_ext);
403 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
405 + struct ext3_extent_header *eh;
407 + BUG_ON(tree->buffer_len == 0);
408 + ext3_ext_get_access_for_root(handle, tree);
409 + eh = EXT_ROOT_HDR(tree);
411 + eh->eh_entries = 0;
412 + eh->eh_magic = EXT3_EXT_MAGIC;
413 + eh->eh_max = ext3_ext_space_root(tree);
414 + ext3_ext_mark_root_dirty(handle, tree);
415 + ext3_ext_invalidate_cache(tree);
419 +struct ext3_ext_path *
420 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
421 + struct ext3_ext_path *path)
423 + struct ext3_extent_header *eh;
424 + struct buffer_head *bh;
425 + int depth, i, ppos = 0;
428 + EXT_ASSERT(tree->inode);
429 + EXT_ASSERT(tree->root);
431 + eh = EXT_ROOT_HDR(tree);
433 + i = depth = EXT_DEPTH(tree);
434 + EXT_ASSERT(eh->eh_max);
435 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
436 + EXT_ASSERT(i == 0 || eh->eh_entries > 0);
438 + /* account possible depth increase */
440 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
443 + return ERR_PTR(-ENOMEM);
445 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
446 + path[0].p_hdr = eh;
448 + /* walk through the tree */
450 + ext_debug(tree, "depth %d: num %d, max %d\n",
451 + ppos, eh->eh_entries, eh->eh_max);
452 + ext3_ext_binsearch_idx(tree, path + ppos, block);
453 + path[ppos].p_block = path[ppos].p_idx->ei_leaf;
454 + path[ppos].p_depth = i;
455 + path[ppos].p_ext = NULL;
457 + bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
459 + ext3_ext_drop_refs(path);
461 + return ERR_PTR(-EIO);
463 + eh = EXT_BLOCK_HDR(bh);
465 + EXT_ASSERT(ppos <= depth);
466 + path[ppos].p_bh = bh;
467 + path[ppos].p_hdr = eh;
471 + path[ppos].p_depth = i;
472 + path[ppos].p_hdr = eh;
473 + path[ppos].p_ext = NULL;
476 + ext3_ext_binsearch(tree, path + ppos, block);
478 + ext3_ext_show_path(tree, path);
484 + * insert new index [logical;ptr] into the block at cupr
485 + * it check where to insert: before curp or after curp
487 +static int ext3_ext_insert_index(handle_t *handle,
488 + struct ext3_extents_tree *tree,
489 + struct ext3_ext_path *curp,
490 + int logical, int ptr)
492 + struct ext3_extent_idx *ix;
495 + if ((err = ext3_ext_get_access(handle, tree, curp)))
498 + EXT_ASSERT(logical != curp->p_idx->ei_block);
499 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
500 + if (logical > curp->p_idx->ei_block) {
502 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
503 + len = (len - 1) * sizeof(struct ext3_extent_idx);
504 + len = len < 0 ? 0 : len;
505 + ext_debug(tree, "insert new index %d after: %d. "
506 + "move %d from 0x%p to 0x%p\n",
508 + (curp->p_idx + 1), (curp->p_idx + 2));
509 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
511 + ix = curp->p_idx + 1;
513 + /* insert before */
514 + len = len * sizeof(struct ext3_extent_idx);
515 + len = len < 0 ? 0 : len;
516 + ext_debug(tree, "insert new index %d before: %d. "
517 + "move %d from 0x%p to 0x%p\n",
519 + curp->p_idx, (curp->p_idx + 1));
520 + memmove(curp->p_idx + 1, curp->p_idx, len);
524 + ix->ei_block = logical;
526 + curp->p_hdr->eh_entries++;
528 + EXT_ASSERT(curp->p_hdr->eh_entries <= curp->p_hdr->eh_max);
529 + EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
531 + err = ext3_ext_dirty(handle, tree, curp);
532 + ext3_std_error(tree->inode->i_sb, err);
538 + * routine inserts new subtree into the path, using free index entry
540 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
541 + * - makes decision where to split
542 + * - moves remaining extens and index entries (right to the split point)
543 + * into the newly allocated blocks
544 + * - initialize subtree
546 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
547 + struct ext3_ext_path *path,
548 + struct ext3_extent *newext, int at)
550 + struct buffer_head *bh = NULL;
551 + int depth = EXT_DEPTH(tree);
552 + struct ext3_extent_header *neh;
553 + struct ext3_extent_idx *fidx;
554 + struct ext3_extent *ex;
555 + int i = at, k, m, a;
556 + unsigned long newblock, oldblock, border;
557 + int *ablocks = NULL; /* array of allocated blocks */
560 + /* make decision: where to split? */
561 + /* FIXME: now desicion is simplest: at current extent */
563 + /* if current leaf will be splitted, then we should use
564 + * border from split point */
565 + EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
566 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
567 + border = path[depth].p_ext[1].ee_block;
568 + ext_debug(tree, "leaf will be splitted."
569 + " next leaf starts at %d\n",
572 + border = newext->ee_block;
573 + ext_debug(tree, "leaf will be added."
574 + " next leaf starts at %d\n",
579 + * if error occurs, then we break processing
580 + * and turn filesystem read-only. so, index won't
581 + * be inserted and tree will be in consistent
582 + * state. next mount will repair buffers too
586 + * get array to track all allocated blocks
587 + * we need this to handle errors and free blocks
590 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
593 + memset(ablocks, 0, sizeof(unsigned long) * depth);
595 + /* allocate all needed blocks */
596 + ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
597 + for (a = 0; a < depth - at; a++) {
598 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
601 + ablocks[a] = newblock;
604 + /* initialize new leaf */
605 + newblock = ablocks[--a];
606 + EXT_ASSERT(newblock);
607 + bh = sb_getblk(tree->inode->i_sb, newblock);
614 + if ((err = ext3_journal_get_create_access(handle, bh)))
617 + neh = EXT_BLOCK_HDR(bh);
618 + neh->eh_entries = 0;
619 + neh->eh_max = ext3_ext_space_block(tree);
620 + neh->eh_magic = EXT3_EXT_MAGIC;
622 + ex = EXT_FIRST_EXTENT(neh);
624 + /* move remain of path[depth] to the new leaf */
625 + EXT_ASSERT(path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max);
626 + /* start copy from next extent */
627 + /* TODO: we could do it by single memmove */
629 + path[depth].p_ext++;
630 + while (path[depth].p_ext <=
631 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
632 + ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
633 + path[depth].p_ext->ee_block,
634 + path[depth].p_ext->ee_start,
635 + path[depth].p_ext->ee_len,
637 + memmove(ex++, path[depth].p_ext++,
638 + sizeof(struct ext3_extent));
642 + set_buffer_uptodate(bh);
645 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
650 + /* correct old leaf */
652 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
654 + path[depth].p_hdr->eh_entries -= m;
655 + if ((err = ext3_ext_dirty(handle, tree, path + depth)))
660 + /* create intermediate indexes */
661 + k = depth - at - 1;
662 + EXT_ASSERT(k >= 0);
664 + ext_debug(tree, "create %d intermediate indices\n", k);
665 + /* insert new index into current index block */
666 + /* current depth stored in i var */
669 + oldblock = newblock;
670 + newblock = ablocks[--a];
671 + bh = sb_getblk(tree->inode->i_sb, newblock);
678 + if ((err = ext3_journal_get_create_access(handle, bh)))
681 + neh = EXT_BLOCK_HDR(bh);
682 + neh->eh_entries = 1;
683 + neh->eh_magic = EXT3_EXT_MAGIC;
684 + neh->eh_max = ext3_ext_space_block_idx(tree);
685 + neh->eh_depth = depth - i;
686 + fidx = EXT_FIRST_INDEX(neh);
687 + fidx->ei_block = border;
688 + fidx->ei_leaf = oldblock;
690 + ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
691 + i, newblock, border, oldblock);
696 + ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
697 + EXT_MAX_INDEX(path[i].p_hdr));
698 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
699 + EXT_LAST_INDEX(path[i].p_hdr));
700 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
701 + ext_debug(tree, "%d: move %d:%d in new index %lu\n",
702 + i, path[i].p_idx->ei_block,
703 + path[i].p_idx->ei_leaf, newblock);
704 + memmove(++fidx, path[i].p_idx++,
705 + sizeof(struct ext3_extent_idx));
707 + EXT_ASSERT(neh->eh_entries <= neh->eh_max);
710 + set_buffer_uptodate(bh);
713 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
718 + /* correct old index */
720 + err = ext3_ext_get_access(handle, tree, path + i);
723 + path[i].p_hdr->eh_entries -= m;
724 + err = ext3_ext_dirty(handle, tree, path + i);
732 + /* insert new index */
734 + err = ext3_ext_insert_index(handle, tree, path + at,
739 + if (buffer_locked(bh))
745 + /* free all allocated blocks in error case */
746 + for (i = 0; i < depth; i++) {
749 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
758 + * routine implements tree growing procedure:
759 + * - allocates new block
760 + * - moves top-level data (index block or leaf) into the new block
761 + * - initialize new top-level, creating index that points to the
762 + * just created block
764 +static int ext3_ext_grow_indepth(handle_t *handle,
765 + struct ext3_extents_tree *tree,
766 + struct ext3_ext_path *path,
767 + struct ext3_extent *newext)
769 + struct ext3_ext_path *curp = path;
770 + struct ext3_extent_header *neh;
771 + struct ext3_extent_idx *fidx;
772 + struct buffer_head *bh;
773 + unsigned long newblock;
776 + newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
780 + bh = sb_getblk(tree->inode->i_sb, newblock);
783 + ext3_std_error(tree->inode->i_sb, err);
788 + if ((err = ext3_journal_get_create_access(handle, bh))) {
793 + /* move top-level index/leaf into new block */
794 + memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
796 + /* set size of new block */
797 + neh = EXT_BLOCK_HDR(bh);
798 + /* old root could have indexes or leaves
799 + * so calculate e_max right way */
800 + if (EXT_DEPTH(tree))
801 + neh->eh_max = ext3_ext_space_block_idx(tree);
803 + neh->eh_max = ext3_ext_space_block(tree);
804 + neh->eh_magic = EXT3_EXT_MAGIC;
805 + set_buffer_uptodate(bh);
808 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
811 + /* create index in new top-level index: num,max,pointer */
812 + if ((err = ext3_ext_get_access(handle, tree, curp)))
815 + curp->p_hdr->eh_magic = EXT3_EXT_MAGIC;
816 + curp->p_hdr->eh_max = ext3_ext_space_root_idx(tree);
817 + curp->p_hdr->eh_entries = 1;
818 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
819 + /* FIXME: it works, but actually path[0] can be index */
820 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
821 + curp->p_idx->ei_leaf = newblock;
823 + neh = EXT_ROOT_HDR(tree);
824 + fidx = EXT_FIRST_INDEX(neh);
825 + ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
826 + neh->eh_entries, neh->eh_max, fidx->ei_block, fidx->ei_leaf);
828 + neh->eh_depth = path->p_depth + 1;
829 + err = ext3_ext_dirty(handle, tree, curp);
837 + * routine finds empty index and adds new leaf. if no free index found
838 + * then it requests in-depth growing
840 +static int ext3_ext_create_new_leaf(handle_t *handle,
841 + struct ext3_extents_tree *tree,
842 + struct ext3_ext_path *path,
843 + struct ext3_extent *newext)
845 + struct ext3_ext_path *curp;
846 + int depth, i, err = 0;
849 + i = depth = EXT_DEPTH(tree);
851 + /* walk up to the tree and look for free index entry */
852 + curp = path + depth;
853 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
858 + /* we use already allocated block for index block
859 + * so, subsequent data blocks should be contigoues */
860 + if (EXT_HAS_FREE_INDEX(curp)) {
861 + /* if we found index with free entry, then use that
862 + * entry: create all needed subtree and add new leaf */
863 + err = ext3_ext_split(handle, tree, path, newext, i);
866 + ext3_ext_drop_refs(path);
867 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
869 + err = PTR_ERR(path);
871 + /* tree is full, time to grow in depth */
872 + err = ext3_ext_grow_indepth(handle, tree, path, newext);
875 + ext3_ext_drop_refs(path);
876 + path = ext3_ext_find_extent(tree, newext->ee_block, path);
878 + err = PTR_ERR(path);
881 + * only first (depth 0 -> 1) produces free space
882 + * in all other cases we have to split growed tree
884 + depth = EXT_DEPTH(tree);
885 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
886 + /* now we need split */
898 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
899 + * NOTE: it consider block number from index entry as
900 + * allocated block. thus, index entries have to be consistent
903 +static unsigned long
904 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
908 + EXT_ASSERT(path != NULL);
909 + depth = path->p_depth;
911 + if (depth == 0 && path->p_ext == NULL)
912 + return EXT_MAX_BLOCK;
914 + /* FIXME: what if index isn't full ?! */
915 + while (depth >= 0) {
916 + if (depth == path->p_depth) {
918 + if (path[depth].p_ext !=
919 + EXT_LAST_EXTENT(path[depth].p_hdr))
920 + return path[depth].p_ext[1].ee_block;
923 + if (path[depth].p_idx !=
924 + EXT_LAST_INDEX(path[depth].p_hdr))
925 + return path[depth].p_idx[1].ei_block;
930 + return EXT_MAX_BLOCK;
934 + * returns first allocated block from next leaf or EXT_MAX_BLOCK
936 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
937 + struct ext3_ext_path *path)
941 + EXT_ASSERT(path != NULL);
942 + depth = path->p_depth;
944 + /* zero-tree has no leaf blocks at all */
946 + return EXT_MAX_BLOCK;
948 + /* go to index block */
951 + while (depth >= 0) {
952 + if (path[depth].p_idx !=
953 + EXT_LAST_INDEX(path[depth].p_hdr))
954 + return path[depth].p_idx[1].ei_block;
958 + return EXT_MAX_BLOCK;
962 + * if leaf gets modified and modified extent is first in the leaf
963 + * then we have to correct all indexes above
964 + * TODO: do we need to correct tree in all cases?
966 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
967 + struct ext3_ext_path *path)
969 + struct ext3_extent_header *eh;
970 + int depth = EXT_DEPTH(tree);
971 + struct ext3_extent *ex;
972 + unsigned long border;
975 + eh = path[depth].p_hdr;
976 + ex = path[depth].p_ext;
981 + /* there is no tree at all */
985 + if (ex != EXT_FIRST_EXTENT(eh)) {
986 + /* we correct tree if first leaf got modified only */
991 + * TODO: we need correction if border is smaller then current one
994 + border = path[depth].p_ext->ee_block;
995 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
997 + path[k].p_idx->ei_block = border;
998 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1002 + /* change all left-side indexes */
1003 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1005 + if ((err = ext3_ext_get_access(handle, tree, path + k)))
1007 + path[k].p_idx->ei_block = border;
1008 + if ((err = ext3_ext_dirty(handle, tree, path + k)))
1016 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1017 + struct ext3_extent *ex1,
1018 + struct ext3_extent *ex2)
1020 + if (ex1->ee_block + ex1->ee_len != ex2->ee_block)
1023 +#ifdef AGRESSIVE_TEST
1024 + if (ex1->ee_len >= 4)
1028 + if (!tree->ops->mergable)
1031 + return tree->ops->mergable(ex1, ex2);
1035 + * this routine tries to merge requsted extent into the existing
1036 + * extent or inserts requested extent as new one into the tree,
1037 + * creating new leaf in no-space case
1039 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1040 + struct ext3_ext_path *path,
1041 + struct ext3_extent *newext)
1043 + struct ext3_extent_header * eh;
1044 + struct ext3_extent *ex, *fex;
1045 + struct ext3_extent *nearex; /* nearest extent */
1046 + struct ext3_ext_path *npath = NULL;
1047 + int depth, len, err, next;
1049 + EXT_ASSERT(newext->ee_len > 0);
1050 + EXT_ASSERT(newext->ee_len < EXT_CACHE_MARK);
1051 + depth = EXT_DEPTH(tree);
1052 + ex = path[depth].p_ext;
1053 + EXT_ASSERT(path[depth].p_hdr);
1055 + /* try to insert block into found extent and return */
1056 + if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1057 + ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1058 + newext->ee_len, ex->ee_block, ex->ee_len,
1060 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1062 + ex->ee_len += newext->ee_len;
1063 + eh = path[depth].p_hdr;
1069 + depth = EXT_DEPTH(tree);
1070 + eh = path[depth].p_hdr;
1071 + if (eh->eh_entries < eh->eh_max)
1074 + /* probably next leaf has space for us? */
1075 + fex = EXT_LAST_EXTENT(eh);
1076 + next = ext3_ext_next_leaf_block(tree, path);
1077 + if (newext->ee_block > fex->ee_block && next != EXT_MAX_BLOCK) {
1078 + ext_debug(tree, "next leaf block - %d\n", next);
1079 + EXT_ASSERT(!npath);
1080 + npath = ext3_ext_find_extent(tree, next, NULL);
1081 + if (IS_ERR(npath))
1082 + return PTR_ERR(npath);
1083 + EXT_ASSERT(npath->p_depth == path->p_depth);
1084 + eh = npath[depth].p_hdr;
1085 + if (eh->eh_entries < eh->eh_max) {
1086 + ext_debug(tree, "next leaf isnt full(%d)\n",
1091 + ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1092 + eh->eh_entries, eh->eh_max);
1096 + * there is no free space in found leaf
1097 + * we're gonna add new leaf in the tree
1099 + err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1102 + depth = EXT_DEPTH(tree);
1103 + eh = path[depth].p_hdr;
1106 + nearex = path[depth].p_ext;
1108 + if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1112 + /* there is no extent in this leaf, create first one */
1113 + ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1114 + newext->ee_block, newext->ee_start,
1116 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1117 + } else if (newext->ee_block > nearex->ee_block) {
1118 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1119 + if (nearex != EXT_LAST_EXTENT(eh)) {
1120 + len = EXT_MAX_EXTENT(eh) - nearex;
1121 + len = (len - 1) * sizeof(struct ext3_extent);
1122 + len = len < 0 ? 0 : len;
1123 + ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1124 + "move %d from 0x%p to 0x%p\n",
1125 + newext->ee_block, newext->ee_start,
1127 + nearex, len, nearex + 1, nearex + 2);
1128 + memmove(nearex + 2, nearex + 1, len);
1130 + path[depth].p_ext = nearex + 1;
1132 + EXT_ASSERT(newext->ee_block != nearex->ee_block);
1133 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1134 + len = len < 0 ? 0 : len;
1135 + ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1136 + "move %d from 0x%p to 0x%p\n",
1137 + newext->ee_block, newext->ee_start, newext->ee_len,
1138 + nearex, len, nearex + 1, nearex + 2);
1139 + memmove(nearex + 1, nearex, len);
1140 + path[depth].p_ext = nearex;
1144 + nearex = path[depth].p_ext;
1145 + nearex->ee_block = newext->ee_block;
1146 + nearex->ee_start = newext->ee_start;
1147 + nearex->ee_len = newext->ee_len;
1148 + /* FIXME: support for large fs */
1149 + nearex->ee_start_hi = 0;
1152 + /* try to merge extents to the right */
1153 + while (nearex < EXT_LAST_EXTENT(eh)) {
1154 + if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1156 + /* merge with next extent! */
1157 + nearex->ee_len += nearex[1].ee_len;
1158 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1159 + len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1160 + * sizeof(struct ext3_extent);
1161 + memmove(nearex + 1, nearex + 2, len);
1164 + EXT_ASSERT(eh->eh_entries > 0);
1167 + /* try to merge extents to the left */
1169 + /* time to correct all indexes above */
1170 + err = ext3_ext_correct_indexes(handle, tree, path);
1174 + err = ext3_ext_dirty(handle, tree, path + depth);
1178 + ext3_ext_drop_refs(npath);
1181 + ext3_ext_tree_changed(tree);
1182 + ext3_ext_invalidate_cache(tree);
1186 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1187 + unsigned long num, ext_prepare_callback func)
1189 + struct ext3_ext_path *path = NULL;
1190 + struct ext3_extent *ex, cbex;
1191 + unsigned long next, start = 0, end = 0;
1192 + unsigned long last = block + num;
1193 + int depth, exists, err = 0;
1197 + EXT_ASSERT(tree->inode);
1198 + EXT_ASSERT(tree->root);
1200 + while (block < last && block != EXT_MAX_BLOCK) {
1201 + num = last - block;
1202 + /* find extent for this block */
1203 + path = ext3_ext_find_extent(tree, block, path);
1204 + if (IS_ERR(path)) {
1205 + err = PTR_ERR(path);
1210 + depth = EXT_DEPTH(tree);
1211 + EXT_ASSERT(path[depth].p_hdr);
1212 + ex = path[depth].p_ext;
1213 + next = ext3_ext_next_allocated_block(path);
1217 + /* there is no extent yet, so try to allocate
1218 + * all requested space */
1220 + end = block + num;
1221 + } else if (ex->ee_block > block) {
1222 + /* need to allocate space before found extent */
1224 + end = ex->ee_block;
1225 + if (block + num < end)
1226 + end = block + num;
1227 + } else if (block >= ex->ee_block + ex->ee_len) {
1228 + /* need to allocate space after found extent */
1230 + end = block + num;
1233 + } else if (block >= ex->ee_block) {
1235 + * some part of requested space is covered
1239 + end = ex->ee_block + ex->ee_len;
1240 + if (block + num < end)
1241 + end = block + num;
1246 + EXT_ASSERT(end > start);
1249 + cbex.ee_block = start;
1250 + cbex.ee_len = end - start;
1251 + cbex.ee_start = 0;
1255 + EXT_ASSERT(path[depth].p_hdr);
1256 + err = func(tree, path, &cbex, exists);
1257 + ext3_ext_drop_refs(path);
1261 + if (err == EXT_REPEAT)
1263 + else if (err == EXT_BREAK) {
1268 + if (EXT_DEPTH(tree) != depth) {
1269 + /* depth was changed. we have to realloc path */
1274 + block = cbex.ee_block + cbex.ee_len;
1278 + ext3_ext_drop_refs(path);
1286 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, __u32 block,
1287 + __u32 len, __u32 start, int type)
1289 + EXT_ASSERT(len > 0);
1291 + tree->cex->ec_type = type;
1292 + tree->cex->ec_block = block;
1293 + tree->cex->ec_len = len;
1294 + tree->cex->ec_start = start;
1299 + * this routine calculate boundaries of the gap requested block fits into
1300 + * and cache this gap
1303 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1304 + struct ext3_ext_path *path,
1305 + unsigned long block)
1307 + int depth = EXT_DEPTH(tree);
1308 + unsigned long lblock, len;
1309 + struct ext3_extent *ex;
1314 + ex = path[depth].p_ext;
1316 + /* there is no extent yet, so gap is [0;-] */
1318 + len = EXT_MAX_BLOCK;
1319 + ext_debug(tree, "cache gap(whole file):");
1320 + } else if (block < ex->ee_block) {
1322 + len = ex->ee_block - block;
1323 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1324 + (unsigned long) block,
1325 + (unsigned long) ex->ee_block,
1326 + (unsigned long) ex->ee_len);
1327 + } else if (block >= ex->ee_block + ex->ee_len) {
1328 + lblock = ex->ee_block + ex->ee_len;
1329 + len = ext3_ext_next_allocated_block(path);
1330 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1331 + (unsigned long) ex->ee_block,
1332 + (unsigned long) ex->ee_len,
1333 + (unsigned long) block);
1334 + EXT_ASSERT(len > lblock);
1335 + len = len - lblock;
1341 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) lblock, len);
1342 + ext3_ext_put_in_cache(tree, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1346 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1347 + struct ext3_extent *ex)
1349 + struct ext3_ext_cache *cex = tree->cex;
1351 + /* is there cache storage at all? */
1353 + return EXT3_EXT_CACHE_NO;
1355 + /* has cache valid data? */
1356 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1357 + return EXT3_EXT_CACHE_NO;
1359 + EXT_ASSERT(cex->ec_type == EXT3_EXT_CACHE_GAP ||
1360 + cex->ec_type == EXT3_EXT_CACHE_EXTENT);
1361 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1362 + ex->ee_block = cex->ec_block;
1363 + ex->ee_start = cex->ec_start;
1364 + ex->ee_len = cex->ec_len;
1365 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1366 + (unsigned long) block,
1367 + (unsigned long) ex->ee_block,
1368 + (unsigned long) ex->ee_len,
1369 + (unsigned long) ex->ee_start);
1370 + return cex->ec_type;
1373 + /* not in cache */
1374 + return EXT3_EXT_CACHE_NO;
1378 + * routine removes index from the index block
1379 + * it's used in truncate case only. thus all requests are for
1380 + * last index in the block only
1382 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1383 + struct ext3_ext_path *path)
1385 + struct buffer_head *bh;
1388 + /* free index block */
1390 + EXT_ASSERT(path->p_hdr->eh_entries);
1391 + if ((err = ext3_ext_get_access(handle, tree, path)))
1393 + path->p_hdr->eh_entries--;
1394 + if ((err = ext3_ext_dirty(handle, tree, path)))
1396 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1397 + path->p_idx->ei_leaf);
1398 + bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
1399 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1400 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1404 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1405 + struct ext3_ext_path *path)
1407 + int depth = EXT_DEPTH(tree);
1411 + /* probably there is space in leaf? */
1412 + if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1417 + * the worste case we're expecting is creation of the
1418 + * new root (growing in depth) with index splitting
1419 + * for splitting we have to consider depth + 1 because
1420 + * previous growing could increase it
1422 + depth = depth + 1;
1425 + * growing in depth:
1426 + * block allocation + new root + old root
1428 + needed = EXT3_ALLOC_NEEDED + 2;
1430 + /* index split. we may need:
1431 + * allocate intermediate indexes and new leaf
1432 + * change two blocks at each level, but root
1433 + * modify root block (inode)
1435 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1441 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1442 + struct ext3_ext_path *path, unsigned long start,
1443 + unsigned long end)
1445 + struct ext3_extent *ex, tex;
1446 + struct ext3_ext_path *npath;
1447 + int depth, creds, err;
1449 + depth = EXT_DEPTH(tree);
1450 + ex = path[depth].p_ext;
1452 + EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1453 + EXT_ASSERT(ex->ee_block < start);
1455 + /* calculate tail extent */
1456 + tex.ee_block = end + 1;
1457 + EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1458 + tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1460 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1461 + handle = ext3_ext_journal_restart(handle, creds);
1462 + if (IS_ERR(handle))
1463 + return PTR_ERR(handle);
1465 + /* calculate head extent. use primary extent */
1466 + err = ext3_ext_get_access(handle, tree, path + depth);
1469 + ex->ee_len = start - ex->ee_block;
1470 + err = ext3_ext_dirty(handle, tree, path + depth);
1474 + /* FIXME: some callback to free underlying resource
1475 + * and correct ee_start? */
1476 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1477 + ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1479 + npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1480 + if (IS_ERR(npath))
1481 + return PTR_ERR(npath);
1482 + depth = EXT_DEPTH(tree);
1483 + EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1484 + EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1486 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1487 + ext3_ext_drop_refs(npath);
1495 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1496 + struct ext3_ext_path *path, unsigned long start,
1497 + unsigned long end)
1499 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1500 + int err = 0, correct_index = 0;
1501 + int depth = EXT_DEPTH(tree), credits;
1502 + struct ext3_extent_header *eh;
1503 + unsigned a, b, block, num;
1505 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1506 + if (!path[depth].p_hdr)
1507 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1508 + eh = path[depth].p_hdr;
1510 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1511 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1513 + /* find where to start removing */
1514 + le = ex = EXT_LAST_EXTENT(eh);
1515 + while (ex != EXT_FIRST_EXTENT(eh)) {
1516 + if (ex->ee_block <= end)
1521 + if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1522 + /* removal of internal part of the extent requested
1523 + * tail and head must be placed in different extent
1524 + * so, we have to insert one more extent */
1525 + path[depth].p_ext = ex;
1526 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1530 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1531 + ex->ee_block + ex->ee_len > start) {
1532 + ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1533 + path[depth].p_ext = ex;
1535 + a = ex->ee_block > start ? ex->ee_block : start;
1536 + b = ex->ee_block + ex->ee_len - 1 < end ?
1537 + ex->ee_block + ex->ee_len - 1 : end;
1539 + ext_debug(tree, " border %u:%u\n", a, b);
1541 + if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1545 + } else if (a != ex->ee_block) {
1546 + /* remove tail of the extent */
1547 + block = ex->ee_block;
1549 + } else if (b != ex->ee_block + ex->ee_len - 1) {
1550 + /* remove head of the extent */
1554 + /* remove whole extent: excelent! */
1555 + block = ex->ee_block;
1557 + EXT_ASSERT(a == ex->ee_block &&
1558 + b == ex->ee_block + ex->ee_len - 1);
1561 + if (ex == EXT_FIRST_EXTENT(eh))
1562 + correct_index = 1;
1565 + if (correct_index)
1566 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1567 + if (tree->ops->remove_extent_credits)
1568 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1570 + handle = ext3_ext_journal_restart(handle, credits);
1571 + if (IS_ERR(handle)) {
1572 + err = PTR_ERR(handle);
1576 + err = ext3_ext_get_access(handle, tree, path + depth);
1580 + if (tree->ops->remove_extent)
1581 + err = tree->ops->remove_extent(tree, ex, a, b);
1586 + /* this extent is removed entirely mark slot unused */
1592 + ex->ee_block = block;
1595 + err = ext3_ext_dirty(handle, tree, path + depth);
1599 + ext_debug(tree, "new extent: %u:%u:%u\n",
1600 + ex->ee_block, ex->ee_len, ex->ee_start);
1605 + /* reuse unused slots */
1607 + if (lu->ee_start) {
1616 + if (correct_index && eh->eh_entries)
1617 + err = ext3_ext_correct_indexes(handle, tree, path);
1619 + /* if this leaf is free, then we should
1620 + * remove it from index block above */
1621 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1622 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1629 +static struct ext3_extent_idx *
1630 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1632 + struct ext3_extent_idx *ix;
1634 + ix = EXT_LAST_INDEX(hdr);
1635 + while (ix != EXT_FIRST_INDEX(hdr)) {
1636 + if (ix->ei_block <= block)
1644 + * returns 1 if current index have to be freed (even partial)
1647 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1649 + EXT_ASSERT(path->p_idx);
1651 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1655 + * if truncate on deeper level happened it it wasn't partial
1656 + * so we have to consider current index for truncation
1658 + if (path->p_hdr->eh_entries == path->p_block)
1663 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1664 + unsigned long start, unsigned long end)
1666 + struct inode *inode = tree->inode;
1667 + struct super_block *sb = inode->i_sb;
1668 + int depth = EXT_DEPTH(tree);
1669 + struct ext3_ext_path *path;
1671 + int i = 0, err = 0;
1673 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1675 + /* probably first extent we're gonna free will be last in block */
1676 + handle = ext3_journal_start(inode, depth + 1);
1677 + if (IS_ERR(handle))
1678 + return PTR_ERR(handle);
1680 + ext3_ext_invalidate_cache(tree);
1683 + * we start scanning from right side freeing all the blocks
1684 + * after i_size and walking into the deep
1686 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1687 + if (IS_ERR(path)) {
1688 + ext3_error(sb, "ext3_ext_remove_space",
1689 + "Can't allocate path array");
1690 + ext3_journal_stop(handle);
1693 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1694 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1696 + while (i >= 0 && err == 0) {
1698 + /* this is leaf block */
1699 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1700 + /* root level have p_bh == NULL, brelse() eats this */
1701 + brelse(path[i].p_bh);
1706 + /* this is index block */
1707 + if (!path[i].p_hdr) {
1708 + ext_debug(tree, "initialize header\n");
1709 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1712 + EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1713 + EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1715 + if (!path[i].p_idx) {
1716 + /* this level hasn't touched yet */
1718 + ext3_ext_last_covered(path[i].p_hdr, end);
1719 + path[i].p_block = path[i].p_hdr->eh_entries + 1;
1720 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1721 + path[i].p_hdr, path[i].p_hdr->eh_entries);
1723 + /* we've already was here, see at next index */
1727 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1728 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1730 + if (ext3_ext_more_to_rm(path + i)) {
1731 + /* go to the next level */
1732 + ext_debug(tree, "move to level %d (block %d)\n",
1733 + i + 1, path[i].p_idx->ei_leaf);
1734 + memset(path + i + 1, 0, sizeof(*path));
1735 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1736 + if (!path[i+1].p_bh) {
1737 + /* should we reset i_size? */
1741 + /* put actual number of indexes to know is this
1742 + * number got changed at the next iteration */
1743 + path[i].p_block = path[i].p_hdr->eh_entries;
1746 + /* we finish processing this index, go up */
1747 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1748 + /* index is empty, remove it
1749 + * handle must be already prepared by the
1750 + * truncatei_leaf() */
1751 + err = ext3_ext_rm_idx(handle, tree, path + i);
1753 + /* root level have p_bh == NULL, brelse() eats this */
1754 + brelse(path[i].p_bh);
1756 + ext_debug(tree, "return to level %d\n", i);
1760 + /* TODO: flexible tree reduction should be here */
1761 + if (path->p_hdr->eh_entries == 0) {
1763 + * truncate to zero freed all the tree
1764 + * so, we need to correct eh_depth
1766 + err = ext3_ext_get_access(handle, tree, path);
1768 + EXT_ROOT_HDR(tree)->eh_depth = 0;
1769 + EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1770 + err = ext3_ext_dirty(handle, tree, path);
1773 + ext3_ext_tree_changed(tree);
1776 + ext3_journal_stop(handle);
1781 +int ext3_ext_calc_metadata_amount(struct ext3_extents_tree *tree, int blocks)
1783 + int lcap, icap, rcap, leafs, idxs, num;
1785 + rcap = ext3_ext_space_root(tree);
1786 + if (blocks <= rcap) {
1787 + /* all extents fit to the root */
1791 + rcap = ext3_ext_space_root_idx(tree);
1792 + lcap = ext3_ext_space_block(tree);
1793 + icap = ext3_ext_space_block_idx(tree);
1795 + num = leafs = (blocks + lcap - 1) / lcap;
1796 + if (leafs <= rcap) {
1797 + /* all pointers to leafs fit to the root */
1801 + /* ok. we need separate index block(s) to link all leaf blocks */
1802 + idxs = (leafs + icap - 1) / icap;
1805 + idxs = (idxs + icap - 1) / icap;
1806 + } while (idxs > rcap);
1812 + * called at mount time
1814 +void ext3_ext_init(struct super_block *sb)
1817 + * possible initialization would be here
1820 + if (test_opt(sb, EXTENTS)) {
1821 + printk("EXT3-fs: file extents enabled");
1822 +#ifdef AGRESSIVE_TEST
1823 + printk(", agressive tests");
1825 +#ifdef CHECK_BINSEARCH
1826 + printk(", check binsearch");
1833 + * called at umount time
1835 +void ext3_ext_release(struct super_block *sb)
1839 +/************************************************************************
1840 + * VFS related routines
1841 + ************************************************************************/
1843 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1845 + /* we use in-core data, not bh */
1849 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1851 + struct inode *inode = buffer;
1852 + return ext3_mark_inode_dirty(handle, inode);
1855 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1856 + struct ext3_extent *ex2)
1858 + /* FIXME: support for large fs */
1859 + if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1865 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1866 + struct ext3_extent *ex,
1867 + unsigned long from, unsigned long to)
1871 + /* at present, extent can't cross block group */;
1872 + needed = 4; /* bitmap + group desc + sb + inode */
1874 +#ifdef CONFIG_QUOTA
1875 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1881 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1882 + struct ext3_extent *ex,
1883 + unsigned long from, unsigned long to)
1885 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1886 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1887 + struct buffer_head *bh;
1890 + if (IS_ERR(handle))
1891 + return PTR_ERR(handle);
1892 + if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1893 + /* tail removal */
1894 + unsigned long num, start;
1895 + num = ex->ee_block + ex->ee_len - from;
1896 + start = ex->ee_start + ex->ee_len - num;
1897 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1899 + for (i = 0; i < num; i++) {
1900 + bh = sb_find_get_block(tree->inode->i_sb, start + i);
1901 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1903 + ext3_free_blocks(handle, tree->inode, start, num);
1904 + } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1905 + printk("strange request: removal %lu-%lu from %u:%u\n",
1906 + from, to, ex->ee_block, ex->ee_len);
1908 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1909 + from, to, ex->ee_block, ex->ee_len);
1911 + ext3_journal_stop(handle);
1915 +static int ext3_ext_find_goal(struct inode *inode,
1916 + struct ext3_ext_path *path, unsigned long block)
1918 + struct ext3_inode_info *ei = EXT3_I(inode);
1919 + unsigned long bg_start;
1920 + unsigned long colour;
1924 + struct ext3_extent *ex;
1925 + depth = path->p_depth;
1927 + /* try to predict block placement */
1928 + if ((ex = path[depth].p_ext))
1929 + return ex->ee_start + (block - ex->ee_block);
1931 + /* it looks index is empty
1932 + * try to find starting from index itself */
1933 + if (path[depth].p_bh)
1934 + return path[depth].p_bh->b_blocknr;
1937 + /* OK. use inode's group */
1938 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1939 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1940 + colour = (current->pid % 16) *
1941 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1942 + return bg_start + colour + block;
1945 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1946 + struct ext3_ext_path *path,
1947 + struct ext3_extent *ex, int *err)
1949 + struct inode *inode = tree->inode;
1950 + int newblock, goal;
1954 + EXT_ASSERT(ex->ee_start);
1955 + EXT_ASSERT(ex->ee_len);
1957 + /* reuse block from the extent to order data/metadata */
1958 + newblock = ex->ee_start++;
1960 + if (ex->ee_len == 0) {
1962 + /* allocate new block for the extent */
1963 + goal = ext3_ext_find_goal(inode, path, ex->ee_block);
1964 + ex->ee_start = ext3_new_block(handle, inode, goal, err);
1965 + if (ex->ee_start == 0) {
1966 + /* error occured: restore old extent */
1967 + ex->ee_start = newblock;
1974 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
1975 + .get_write_access = ext3_get_inode_write_access,
1976 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
1977 + .mergable = ext3_ext_mergable,
1978 + .new_block = ext3_new_block_cb,
1979 + .remove_extent = ext3_remove_blocks,
1980 + .remove_extent_credits = ext3_remove_blocks_credits,
1983 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1984 + struct inode *inode)
1986 + tree->inode = inode;
1987 + tree->root = (void *) EXT3_I(inode)->i_data;
1988 + tree->buffer = (void *) inode;
1989 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1990 + tree->cex = (struct ext3_ext_cache *) &EXT3_I(inode)->i_cached_extent;
1991 + tree->ops = &ext3_blockmap_helpers;
1994 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
1995 + long iblock, struct buffer_head *bh_result,
1996 + int create, int extend_disksize)
1998 + struct ext3_ext_path *path = NULL;
1999 + struct ext3_extent newex;
2000 + struct ext3_extent *ex;
2001 + int goal, newblock, err = 0, depth;
2002 + struct ext3_extents_tree tree;
2004 + clear_buffer_new(bh_result);
2005 + ext3_init_tree_desc(&tree, inode);
2006 + ext_debug(&tree, "block %d requested for inode %u\n",
2007 + (int) iblock, (unsigned) inode->i_ino);
2008 + down(&EXT3_I(inode)->truncate_sem);
2010 + /* check in cache */
2011 + if ((goal = ext3_ext_in_cache(&tree, iblock, &newex))) {
2012 + if (goal == EXT3_EXT_CACHE_GAP) {
2014 + /* block isn't allocated yet and
2015 + * user don't want to allocate it */
2018 + /* we should allocate requested block */
2019 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2020 + /* block is already allocated */
2021 + newblock = iblock - newex.ee_block + newex.ee_start;
2028 + /* find extent for this block */
2029 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2030 + if (IS_ERR(path)) {
2031 + err = PTR_ERR(path);
2036 + depth = EXT_DEPTH(&tree);
2039 + * consistent leaf must not be empty
2040 + * this situations is possible, though, _during_ tree modification
2041 + * this is why assert can't be put in ext3_ext_find_extent()
2043 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2045 + if ((ex = path[depth].p_ext)) {
2046 + /* if found exent covers block, simple return it */
2047 + if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2048 + newblock = iblock - ex->ee_block + ex->ee_start;
2049 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2050 + (int) iblock, ex->ee_block, ex->ee_len,
2052 + ext3_ext_put_in_cache(&tree, ex->ee_block,
2053 + ex->ee_len, ex->ee_start,
2054 + EXT3_EXT_CACHE_EXTENT);
2060 + * requested block isn't allocated yet
2061 + * we couldn't try to create block if create flag is zero
2064 + /* put just found gap into cache to speedup subsequest reqs */
2065 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2069 + /* allocate new block */
2070 + goal = ext3_ext_find_goal(inode, path, iblock);
2071 + newblock = ext3_new_block(handle, inode, goal, &err);
2074 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2077 + /* try to insert new extent into found leaf and return */
2078 + newex.ee_block = iblock;
2079 + newex.ee_start = newblock;
2081 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2085 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2086 + EXT3_I(inode)->i_disksize = inode->i_size;
2088 + /* previous routine could use block we allocated */
2089 + newblock = newex.ee_start;
2090 + set_buffer_new(bh_result);
2092 + ext3_ext_put_in_cache(&tree, newex.ee_block, newex.ee_len,
2093 + newex.ee_start, EXT3_EXT_CACHE_EXTENT);
2095 + ext3_ext_show_leaf(&tree, path);
2096 + map_bh(bh_result, inode->i_sb, newblock);
2099 + ext3_ext_drop_refs(path);
2102 + up(&EXT3_I(inode)->truncate_sem);
2107 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2109 + struct address_space *mapping = inode->i_mapping;
2110 + struct super_block *sb = inode->i_sb;
2111 + struct ext3_extents_tree tree;
2112 + unsigned long last_block;
2116 + ext3_init_tree_desc(&tree, inode);
2119 + * probably first extent we're gonna free will be last in block
2121 + err = ext3_writepage_trans_blocks(inode) + 3;
2122 + handle = ext3_journal_start(inode, err);
2123 + if (IS_ERR(handle)) {
2125 + clear_highpage(page);
2126 + flush_dcache_page(page);
2127 + unlock_page(page);
2128 + page_cache_release(page);
2134 + ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2136 + down(&EXT3_I(inode)->truncate_sem);
2137 + ext3_ext_invalidate_cache(&tree);
2140 + * TODO: optimization is possible here
2141 + * probably we need not scaning at all,
2142 + * because page truncation is enough
2144 + if (ext3_orphan_add(handle, inode))
2147 + /* we have to know where to truncate from in crash case */
2148 + EXT3_I(inode)->i_disksize = inode->i_size;
2149 + ext3_mark_inode_dirty(handle, inode);
2151 + last_block = (inode->i_size + sb->s_blocksize - 1)
2152 + >> EXT3_BLOCK_SIZE_BITS(sb);
2153 + err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2155 + /* In a multi-transaction truncate, we only make the final
2156 + * transaction synchronous */
2157 + if (IS_SYNC(inode))
2158 + handle->h_sync = 1;
2162 + * If this was a simple ftruncate(), and the file will remain alive
2163 + * then we need to clear up the orphan record which we created above.
2164 + * However, if this was a real unlink then we were called by
2165 + * ext3_delete_inode(), and we allow that function to clean up the
2166 + * orphan info for us.
2168 + if (inode->i_nlink)
2169 + ext3_orphan_del(handle, inode);
2171 + up(&EXT3_I(inode)->truncate_sem);
2172 + ext3_journal_stop(handle);
2176 + * this routine calculate max number of blocks we could modify
2177 + * in order to allocate new block for an inode
2179 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2181 + struct ext3_extents_tree tree;
2184 + ext3_init_tree_desc(&tree, inode);
2186 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2188 + /* caller want to allocate num blocks */
2191 +#ifdef CONFIG_QUOTA
2193 + * FIXME: real calculation should be here
2194 + * it depends on blockmap format of qouta file
2196 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2202 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2204 + struct ext3_extents_tree tree;
2206 + ext3_init_tree_desc(&tree, inode);
2207 + ext3_extent_tree_init(handle, &tree);
2210 +int ext3_ext_calc_blockmap_metadata(struct inode *inode, int blocks)
2212 + struct ext3_extents_tree tree;
2214 + ext3_init_tree_desc(&tree, inode);
2215 + return ext3_ext_calc_metadata_amount(&tree, blocks);
2219 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2220 + struct ext3_ext_path *path,
2221 + struct ext3_extent *newex, int exist)
2223 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2226 + return EXT_CONTINUE;
2229 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2232 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2234 + buf->cur += sizeof(*newex);
2236 + buf->err = -EFAULT;
2239 + return EXT_CONTINUE;
2243 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2244 + struct ext3_ext_path *path,
2245 + struct ext3_extent *ex, int exist)
2247 + struct ext3_extent_tree_stats *buf =
2248 + (struct ext3_extent_tree_stats *) tree->private;
2252 + return EXT_CONTINUE;
2254 + depth = EXT_DEPTH(tree);
2255 + buf->extents_num++;
2256 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2258 + return EXT_CONTINUE;
2261 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2262 + unsigned long arg)
2266 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2269 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2270 + struct ext3_extent_buf buf;
2271 + struct ext3_extents_tree tree;
2273 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2276 + ext3_init_tree_desc(&tree, inode);
2277 + buf.cur = buf.buffer;
2279 + tree.private = &buf;
2280 + down(&EXT3_I(inode)->truncate_sem);
2281 + err = ext3_ext_walk_space(&tree, buf.start, EXT_MAX_BLOCK,
2282 + ext3_ext_store_extent_cb);
2283 + up(&EXT3_I(inode)->truncate_sem);
2286 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2287 + struct ext3_extent_tree_stats buf;
2288 + struct ext3_extents_tree tree;
2290 + ext3_init_tree_desc(&tree, inode);
2291 + down(&EXT3_I(inode)->truncate_sem);
2292 + buf.depth = EXT_DEPTH(&tree);
2293 + buf.extents_num = 0;
2295 + tree.private = &buf;
2296 + err = ext3_ext_walk_space(&tree, 0, EXT_MAX_BLOCK,
2297 + ext3_ext_collect_stats_cb);
2298 + up(&EXT3_I(inode)->truncate_sem);
2300 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2301 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2302 + struct ext3_extents_tree tree;
2303 + ext3_init_tree_desc(&tree, inode);
2304 + down(&EXT3_I(inode)->truncate_sem);
2305 + err = EXT_DEPTH(&tree);
2306 + up(&EXT3_I(inode)->truncate_sem);
2312 +EXPORT_SYMBOL(ext3_init_tree_desc);
2313 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2314 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2315 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2316 +EXPORT_SYMBOL(ext3_ext_walk_space);
2317 +EXPORT_SYMBOL(ext3_ext_find_goal);
2318 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2320 Index: linux-2.6.5-sles9/fs/ext3/ialloc.c
2321 ===================================================================
2322 --- linux-2.6.5-sles9.orig/fs/ext3/ialloc.c 2004-11-09 02:22:55.763148128 +0300
2323 +++ linux-2.6.5-sles9/fs/ext3/ialloc.c 2004-11-09 02:23:21.587222272 +0300
2324 @@ -647,6 +647,10 @@
2325 DQUOT_FREE_INODE(inode);
2328 + if (test_opt(sb, EXTENTS)) {
2329 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2330 + ext3_extents_initialize_blockmap(handle, inode);
2332 err = ext3_mark_inode_dirty(handle, inode);
2334 ext3_std_error(sb, err);
2335 Index: linux-2.6.5-sles9/fs/ext3/inode.c
2336 ===================================================================
2337 --- linux-2.6.5-sles9.orig/fs/ext3/inode.c 2004-11-09 02:22:55.767147520 +0300
2338 +++ linux-2.6.5-sles9/fs/ext3/inode.c 2004-11-09 02:23:21.592221512 +0300
2339 @@ -796,6 +796,17 @@
2344 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2345 + struct buffer_head *bh, int create, int extend_disksize)
2347 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2348 + return ext3_ext_get_block(handle, inode, block, bh, create,
2350 + return ext3_get_block_handle(handle, inode, block, bh, create,
2354 static int ext3_get_block(struct inode *inode, sector_t iblock,
2355 struct buffer_head *bh_result, int create)
2358 handle = ext3_journal_current_handle();
2359 J_ASSERT(handle != 0);
2361 - ret = ext3_get_block_handle(handle, inode, iblock,
2362 - bh_result, create, 1);
2363 + ret = ext3_get_block_wrap(handle, inode, iblock,
2364 + bh_result, create, 1);
2372 - ret = ext3_get_block_handle(handle, inode, iblock,
2373 - bh_result, create, 0);
2374 + ret = ext3_get_block_wrap(handle, inode, iblock,
2375 + bh_result, create, 0);
2377 bh_result->b_size = (1 << inode->i_blkbits);
2381 dummy.b_blocknr = -1000;
2382 buffer_trace_init(&dummy.b_history);
2383 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2384 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2385 if (!*errp && buffer_mapped(&dummy)) {
2386 struct buffer_head *bh;
2387 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2388 @@ -1587,7 +1598,7 @@
2389 * This required during truncate. We need to physically zero the tail end
2390 * of that block so it doesn't yield old data if the file is later grown.
2392 -static int ext3_block_truncate_page(handle_t *handle, struct page *page,
2393 +int ext3_block_truncate_page(handle_t *handle, struct page *page,
2394 struct address_space *mapping, loff_t from)
2396 unsigned long index = from >> PAGE_CACHE_SHIFT;
2397 @@ -2083,6 +2094,9 @@
2401 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2402 + return ext3_ext_truncate(inode, page);
2404 handle = start_transaction(inode);
2405 if (IS_ERR(handle)) {
2407 @@ -2789,6 +2803,9 @@
2408 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2411 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2412 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2414 if (ext3_should_journal_data(inode))
2415 ret = 3 * (bpp + indirects) + 2;
2417 Index: linux-2.6.5-sles9/fs/ext3/Makefile
2418 ===================================================================
2419 --- linux-2.6.5-sles9.orig/fs/ext3/Makefile 2004-11-09 02:18:27.604914376 +0300
2420 +++ linux-2.6.5-sles9/fs/ext3/Makefile 2004-11-09 02:23:21.593221360 +0300
2422 obj-$(CONFIG_EXT3_FS) += ext3.o
2424 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2425 - ioctl.o namei.o super.o symlink.o hash.o
2426 + ioctl.o namei.o super.o symlink.o hash.o extents.o
2428 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2429 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2430 Index: linux-2.6.5-sles9/fs/ext3/super.c
2431 ===================================================================
2432 --- linux-2.6.5-sles9.orig/fs/ext3/super.c 2004-11-09 02:22:56.450043704 +0300
2433 +++ linux-2.6.5-sles9/fs/ext3/super.c 2004-11-09 02:23:21.597220752 +0300
2435 struct ext3_super_block *es = sbi->s_es;
2438 + ext3_ext_release(sb);
2439 ext3_xattr_put_super(sb);
2440 journal_destroy(sbi->s_journal);
2441 if (!(sb->s_flags & MS_RDONLY)) {
2442 @@ -447,6 +448,10 @@
2444 ei->i_rsv_window.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
2445 ei->vfs_inode.i_version = 1;
2446 + ei->i_cached_extent[0] = 0;
2447 + ei->i_cached_extent[1] = 0;
2448 + ei->i_cached_extent[2] = 0;
2449 + ei->i_cached_extent[3] = 0;
2450 return &ei->vfs_inode;
2454 Opt_commit, Opt_journal_update, Opt_journal_inum,
2455 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
2456 Opt_ignore, Opt_barrier, Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2458 + Opt_err, Opt_extents, Opt_extdebug
2461 static match_table_t tokens = {
2463 {Opt_iopen, "iopen"},
2464 {Opt_noiopen, "noiopen"},
2465 {Opt_iopen_nopriv, "iopen_nopriv"},
2466 + {Opt_extents, "extents"},
2467 + {Opt_extdebug, "extdebug"},
2471 @@ -797,6 +804,12 @@
2476 + set_opt (sbi->s_mount_opt, EXTENTS);
2478 + case Opt_extdebug:
2479 + set_opt (sbi->s_mount_opt, EXTDEBUG);
2483 "EXT3-fs: Unrecognized mount option \"%s\" "
2484 @@ -1449,6 +1462,8 @@
2485 percpu_counter_mod(&sbi->s_dirs_counter,
2486 ext3_count_dirs(sb));
2488 + ext3_ext_init(sb);
2493 Index: linux-2.6.5-sles9/fs/ext3/ioctl.c
2494 ===================================================================
2495 --- linux-2.6.5-sles9.orig/fs/ext3/ioctl.c 2004-11-09 02:15:44.610693264 +0300
2496 +++ linux-2.6.5-sles9/fs/ext3/ioctl.c 2004-11-09 02:23:52.991448104 +0300
2497 @@ -124,6 +124,10 @@
2498 err = ext3_change_inode_journal_flag(inode, jflag);
2501 + case EXT3_IOC_GET_EXTENTS:
2502 + case EXT3_IOC_GET_TREE_STATS:
2503 + case EXT3_IOC_GET_TREE_DEPTH:
2504 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2505 case EXT3_IOC_GETVERSION:
2506 case EXT3_IOC_GETVERSION_OLD:
2507 return put_user(inode->i_generation, (int *) arg);
2508 Index: linux-2.6.5-sles9/include/linux/ext3_fs.h
2509 ===================================================================
2510 --- linux-2.6.5-sles9.orig/include/linux/ext3_fs.h 2004-11-09 02:22:58.767691368 +0300
2511 +++ linux-2.6.5-sles9/include/linux/ext3_fs.h 2004-11-09 02:25:17.238640584 +0300
2513 #define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2514 #define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2515 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2516 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2518 #define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
2519 #define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
2522 #define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
2523 #define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
2524 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 7, long)
2525 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 8, long)
2526 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 9, long)
2529 * Structure of an inode on the disk
2531 #define EXT3_MOUNT_BARRIER 0x20000 /* Use block barriers */
2532 #define EXT3_MOUNT_IOPEN 0x40000 /* Allow access via iopen */
2533 #define EXT3_MOUNT_IOPEN_NOPRIV 0x80000 /* Make iopen world-readable */
2534 +#define EXT3_MOUNT_EXTENTS 0x100000/* Extents support */
2535 +#define EXT3_MOUNT_EXTDEBUG 0x200000/* Extents debug */
2537 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2543 +extern int ext3_block_truncate_page(handle_t *, struct page *, struct address_space *, loff_t);
2544 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2545 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2546 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2547 @@ -802,6 +809,14 @@
2548 extern struct inode_operations ext3_symlink_inode_operations;
2549 extern struct inode_operations ext3_fast_symlink_inode_operations;
2552 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2553 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2554 + struct buffer_head *, int, int);
2555 +extern void ext3_ext_truncate(struct inode *, struct page *);
2556 +extern void ext3_ext_init(struct super_block *);
2557 +extern void ext3_ext_release(struct super_block *);
2558 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2560 #endif /* __KERNEL__ */
2562 Index: linux-2.6.5-sles9/include/linux/ext3_extents.h
2563 ===================================================================
2564 --- linux-2.6.5-sles9.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2565 +++ linux-2.6.5-sles9/include/linux/ext3_extents.h 2004-11-09 02:23:21.606219384 +0300
2568 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
2569 + * Written by Alex Tomas <alex@clusterfs.com>
2571 + * This program is free software; you can redistribute it and/or modify
2572 + * it under the terms of the GNU General Public License version 2 as
2573 + * published by the Free Software Foundation.
2575 + * This program is distributed in the hope that it will be useful,
2576 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2577 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2578 + * GNU General Public License for more details.
2580 + * You should have received a copy of the GNU General Public Licens
2581 + * along with this program; if not, write to the Free Software
2582 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2585 +#ifndef _LINUX_EXT3_EXTENTS
2586 +#define _LINUX_EXT3_EXTENTS
2589 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2590 + * become very little, so index split, in-depth growing and
2591 + * other hard changes happens much more often
2592 + * this is for debug purposes only
2594 +#define AGRESSIVE_TEST_
2597 + * if CHECK_BINSEARCH defined, then results of binary search
2598 + * will be checked by linear search
2600 +#define CHECK_BINSEARCH_
2603 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2604 + * to get lots of info what's going on
2608 +#define ext_debug(tree,fmt,a...) \
2610 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2611 + printk(fmt, ##a); \
2614 +#define ext_debug(tree,fmt,a...)
2618 + * if EXT_STATS is defined then stats numbers are collected
2619 + * these number will be displayed at umount time
2624 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2627 + * ext3_inode has i_block array (total 60 bytes)
2628 + * first 4 bytes are used to store:
2629 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2630 + * - number of alive extents in the inode
2634 + * this is extent on-disk structure
2635 + * it's used at the bottom of the tree
2637 +struct ext3_extent {
2638 + __u32 ee_block; /* first logical block extent covers */
2639 + __u16 ee_len; /* number of blocks covered by extent */
2640 + __u16 ee_start_hi; /* high 16 bits of physical block */
2641 + __u32 ee_start; /* low 32 bigs of physical block */
2645 + * this is index on-disk structure
2646 + * it's used at all the levels, but the bottom
2648 +struct ext3_extent_idx {
2649 + __u32 ei_block; /* index covers logical blocks from 'block' */
2650 + __u32 ei_leaf; /* pointer to the physical block of the next *
2651 + * level. leaf or next index could bet here */
2652 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
2657 + * each block (leaves and indexes), even inode-stored has header
2659 +struct ext3_extent_header {
2660 + __u16 eh_magic; /* probably will support different formats */
2661 + __u16 eh_entries; /* number of valid entries */
2662 + __u16 eh_max; /* capacity of store in entries */
2663 + __u16 eh_depth; /* has tree real underlaying blocks? */
2664 + __u32 eh_generation; /* generation of the tree */
2667 +#define EXT3_EXT_MAGIC 0xf30a
2670 + * array of ext3_ext_path contains path to some extent
2671 + * creation/lookup routines use it for traversal/splitting/etc
2672 + * truncate uses it to simulate recursive walking
2674 +struct ext3_ext_path {
2677 + struct ext3_extent *p_ext;
2678 + struct ext3_extent_idx *p_idx;
2679 + struct ext3_extent_header *p_hdr;
2680 + struct buffer_head *p_bh;
2684 + * structure for external API
2688 + * storage for cached extent
2690 +struct ext3_ext_cache {
2697 +#define EXT3_EXT_CACHE_NO 0
2698 +#define EXT3_EXT_CACHE_GAP 1
2699 +#define EXT3_EXT_CACHE_EXTENT 2
2702 + * ext3_extents_tree is used to pass initial information
2703 + * to top-level extents API
2705 +struct ext3_extents_helpers;
2706 +struct ext3_extents_tree {
2707 + struct inode *inode; /* inode which tree belongs to */
2708 + void *root; /* ptr to data top of tree resides at */
2709 + void *buffer; /* will be passed as arg to ^^ routines */
2712 + struct ext3_ext_cache *cex;/* last found extent */
2713 + struct ext3_extents_helpers *ops;
2716 +struct ext3_extents_helpers {
2717 + int (*get_write_access)(handle_t *h, void *buffer);
2718 + int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2719 + int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2720 + int (*remove_extent_credits)(struct ext3_extents_tree *,
2721 + struct ext3_extent *, unsigned long,
2723 + int (*remove_extent)(struct ext3_extents_tree *,
2724 + struct ext3_extent *, unsigned long,
2726 + int (*new_block)(handle_t *, struct ext3_extents_tree *,
2727 + struct ext3_ext_path *, struct ext3_extent *,
2732 + * to be called by ext3_ext_walk_space()
2733 + * negative retcode - error
2734 + * positive retcode - signal for ext3_ext_walk_space(), see below
2735 + * callback must return valid extent (passed or newly created)
2737 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2738 + struct ext3_ext_path *,
2739 + struct ext3_extent *, int);
2741 +#define EXT_CONTINUE 0
2742 +#define EXT_BREAK 1
2743 +#define EXT_REPEAT 2
2746 +#define EXT_MAX_BLOCK 0xffffffff
2747 +#define EXT_CACHE_MARK 0xffff
2750 +#define EXT_FIRST_EXTENT(__hdr__) \
2751 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2752 + sizeof(struct ext3_extent_header)))
2753 +#define EXT_FIRST_INDEX(__hdr__) \
2754 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2755 + sizeof(struct ext3_extent_header)))
2756 +#define EXT_HAS_FREE_INDEX(__path__) \
2757 + ((__path__)->p_hdr->eh_entries < (__path__)->p_hdr->eh_max)
2758 +#define EXT_LAST_EXTENT(__hdr__) \
2759 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_entries - 1)
2760 +#define EXT_LAST_INDEX(__hdr__) \
2761 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_entries - 1)
2762 +#define EXT_MAX_EXTENT(__hdr__) \
2763 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->eh_max - 1)
2764 +#define EXT_MAX_INDEX(__hdr__) \
2765 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->eh_max - 1)
2767 +#define EXT_ROOT_HDR(tree) \
2768 + ((struct ext3_extent_header *) (tree)->root)
2769 +#define EXT_BLOCK_HDR(bh) \
2770 + ((struct ext3_extent_header *) (bh)->b_data)
2771 +#define EXT_DEPTH(_t_) \
2772 + (((struct ext3_extent_header *)((_t_)->root))->eh_depth)
2773 +#define EXT_GENERATION(_t_) \
2774 + (((struct ext3_extent_header *)((_t_)->root))->eh_generation)
2777 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2781 + * this structure is used to gather extents from the tree via ioctl
2783 +struct ext3_extent_buf {
2784 + unsigned long start;
2792 + * this structure is used to collect stats info about the tree
2794 +struct ext3_extent_tree_stats {
2800 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2801 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2802 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2803 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2804 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2805 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2806 +extern void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
2807 +extern int ext3_ext_calc_blockmap_metadata(struct inode *, int);
2810 +ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
2813 + tree->cex->ec_type = EXT3_EXT_CACHE_NO;
2817 +#endif /* _LINUX_EXT3_EXTENTS */
2819 Index: linux-2.6.5-sles9/include/linux/ext3_fs_i.h
2820 ===================================================================
2821 --- linux-2.6.5-sles9.orig/include/linux/ext3_fs_i.h 2004-11-09 02:22:55.780145544 +0300
2822 +++ linux-2.6.5-sles9/include/linux/ext3_fs_i.h 2004-11-09 02:23:21.606219384 +0300
2825 struct semaphore truncate_sem;
2826 struct inode vfs_inode;
2828 + __u32 i_cached_extent[4];
2831 #endif /* _LINUX_EXT3_FS_I */
2834 fs/ext3/Makefile | 2
2835 fs/ext3/extents.c | 2313 +++++++++++++++++++++++++++++++++++++++++++
2836 fs/ext3/ialloc.c | 4
2837 fs/ext3/inode.c | 29
2839 fs/ext3/super.c | 17
2840 include/linux/ext3_extents.h | 252 ++++
2841 include/linux/ext3_fs.h | 15
2842 include/linux/ext3_fs_i.h | 2
2843 9 files changed, 2630 insertions(+), 8 deletions(-)