2 Index: linux-2.6.7/fs/ext3/extents.c
3 ===================================================================
4 --- linux-2.6.7.orig/fs/ext3/extents.c 2003-01-30 13:24:37.000000000 +0300
5 +++ linux-2.6.7/fs/ext3/extents.c 2004-09-12 18:53:38.000000000 +0400
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, 0, 0, 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, struct ext3_extent *ex)
1290 + EXT_ASSERT(ex->ee_len);
1291 + tree->cex->ee_block = ex->ee_block;
1292 + tree->cex->ee_start = ex->ee_start;
1293 + tree->cex->ee_len = ex->ee_len;
1298 + * this routine calculate boundaries of the gap requested block fits into
1299 + * and cache this gap
1302 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1303 + struct ext3_ext_path *path,
1304 + unsigned long block)
1306 + int depth = EXT_DEPTH(tree);
1307 + struct ext3_extent *ex, gex;
1312 + ex = path[depth].p_ext;
1314 + /* there is no extent yet, so gap is [0;-] */
1316 + gex.ee_len = EXT_CACHE_MARK;
1317 + ext_debug(tree, "cache gap(whole file):");
1318 + } else if (block < ex->ee_block) {
1319 + gex.ee_block = block;
1320 + gex.ee_len = ex->ee_block - block;
1321 + ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1322 + (unsigned long) block,
1323 + (unsigned long) ex->ee_block,
1324 + (unsigned long) ex->ee_len);
1325 + } else if (block >= ex->ee_block + ex->ee_len) {
1326 + gex.ee_block = ex->ee_block + ex->ee_len;
1327 + gex.ee_len = ext3_ext_next_allocated_block(path);
1328 + ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1329 + (unsigned long) ex->ee_block,
1330 + (unsigned long) ex->ee_len,
1331 + (unsigned long) block);
1332 + EXT_ASSERT(gex.ee_len > gex.ee_block);
1333 + gex.ee_len = gex.ee_len - gex.ee_block;
1338 + ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.ee_block,
1339 + (unsigned long) gex.ee_len);
1340 + gex.ee_start = EXT_CACHE_MARK;
1341 + ext3_ext_put_in_cache(tree, &gex);
1345 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1346 + struct ext3_extent *ex)
1348 + struct ext3_extent *cex = tree->cex;
1350 + /* is there cache storage at all? */
1354 + /* has cache valid data? */
1355 + if (cex->ee_len == 0)
1358 + if (block >= cex->ee_block && block < cex->ee_block + cex->ee_len) {
1359 + ex->ee_block = cex->ee_block;
1360 + ex->ee_start = cex->ee_start;
1361 + ex->ee_len = cex->ee_len;
1362 + ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1363 + (unsigned long) block,
1364 + (unsigned long) ex->ee_block,
1365 + (unsigned long) ex->ee_len,
1366 + (unsigned long) ex->ee_start);
1370 + /* not in cache */
1375 + * routine removes index from the index block
1376 + * it's used in truncate case only. thus all requests are for
1377 + * last index in the block only
1379 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1380 + struct ext3_ext_path *path)
1382 + struct buffer_head *bh;
1385 + /* free index block */
1387 + EXT_ASSERT(path->p_hdr->eh_entries);
1388 + if ((err = ext3_ext_get_access(handle, tree, path)))
1390 + path->p_hdr->eh_entries--;
1391 + if ((err = ext3_ext_dirty(handle, tree, path)))
1393 + ext_debug(tree, "index is empty, remove it, free block %d\n",
1394 + path->p_idx->ei_leaf);
1395 + bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
1396 + ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1397 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1401 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1402 + struct ext3_ext_path *path)
1404 + int depth = EXT_DEPTH(tree);
1408 + /* probably there is space in leaf? */
1409 + if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1414 + * the worste case we're expecting is creation of the
1415 + * new root (growing in depth) with index splitting
1416 + * for splitting we have to consider depth + 1 because
1417 + * previous growing could increase it
1419 + depth = depth + 1;
1422 + * growing in depth:
1423 + * block allocation + new root + old root
1425 + needed = EXT3_ALLOC_NEEDED + 2;
1427 + /* index split. we may need:
1428 + * allocate intermediate indexes and new leaf
1429 + * change two blocks at each level, but root
1430 + * modify root block (inode)
1432 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1438 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1439 + struct ext3_ext_path *path, unsigned long start,
1440 + unsigned long end)
1442 + struct ext3_extent *ex, tex;
1443 + struct ext3_ext_path *npath;
1444 + int depth, creds, err;
1446 + depth = EXT_DEPTH(tree);
1447 + ex = path[depth].p_ext;
1449 + EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1450 + EXT_ASSERT(ex->ee_block < start);
1452 + /* calculate tail extent */
1453 + tex.ee_block = end + 1;
1454 + EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1455 + tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1457 + creds = ext3_ext_calc_credits_for_insert(tree, path);
1458 + handle = ext3_ext_journal_restart(handle, creds);
1459 + if (IS_ERR(handle))
1460 + return PTR_ERR(handle);
1462 + /* calculate head extent. use primary extent */
1463 + err = ext3_ext_get_access(handle, tree, path + depth);
1466 + ex->ee_len = start - ex->ee_block;
1467 + err = ext3_ext_dirty(handle, tree, path + depth);
1471 + /* FIXME: some callback to free underlying resource
1472 + * and correct ee_start? */
1473 + ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1474 + ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1476 + npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1477 + if (IS_ERR(npath))
1478 + return PTR_ERR(npath);
1479 + depth = EXT_DEPTH(tree);
1480 + EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1481 + EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1483 + err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1484 + ext3_ext_drop_refs(npath);
1492 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1493 + struct ext3_ext_path *path, unsigned long start,
1494 + unsigned long end)
1496 + struct ext3_extent *ex, *fu = NULL, *lu, *le;
1497 + int err = 0, correct_index = 0;
1498 + int depth = EXT_DEPTH(tree), credits;
1499 + struct ext3_extent_header *eh;
1500 + unsigned a, b, block, num;
1502 + ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1503 + if (!path[depth].p_hdr)
1504 + path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1505 + eh = path[depth].p_hdr;
1507 + EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1508 + EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1510 + /* find where to start removing */
1511 + le = ex = EXT_LAST_EXTENT(eh);
1512 + while (ex != EXT_FIRST_EXTENT(eh)) {
1513 + if (ex->ee_block <= end)
1518 + if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1519 + /* removal of internal part of the extent requested
1520 + * tail and head must be placed in different extent
1521 + * so, we have to insert one more extent */
1522 + path[depth].p_ext = ex;
1523 + return ext3_ext_split_for_rm(handle, tree, path, start, end);
1527 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1528 + ex->ee_block + ex->ee_len > start) {
1529 + ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1530 + path[depth].p_ext = ex;
1532 + a = ex->ee_block > start ? ex->ee_block : start;
1533 + b = ex->ee_block + ex->ee_len - 1 < end ?
1534 + ex->ee_block + ex->ee_len - 1 : end;
1536 + ext_debug(tree, " border %u:%u\n", a, b);
1538 + if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1542 + } else if (a != ex->ee_block) {
1543 + /* remove tail of the extent */
1544 + block = ex->ee_block;
1546 + } else if (b != ex->ee_block + ex->ee_len - 1) {
1547 + /* remove head of the extent */
1551 + /* remove whole extent: excelent! */
1552 + block = ex->ee_block;
1554 + EXT_ASSERT(a == ex->ee_block &&
1555 + b == ex->ee_block + ex->ee_len - 1);
1558 + if (ex == EXT_FIRST_EXTENT(eh))
1559 + correct_index = 1;
1562 + if (correct_index)
1563 + credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1564 + if (tree->ops->remove_extent_credits)
1565 + credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1567 + handle = ext3_ext_journal_restart(handle, credits);
1568 + if (IS_ERR(handle)) {
1569 + err = PTR_ERR(handle);
1573 + err = ext3_ext_get_access(handle, tree, path + depth);
1577 + if (tree->ops->remove_extent)
1578 + err = tree->ops->remove_extent(tree, ex, a, b);
1583 + /* this extent is removed entirely mark slot unused */
1589 + ex->ee_block = block;
1592 + err = ext3_ext_dirty(handle, tree, path + depth);
1596 + ext_debug(tree, "new extent: %u:%u:%u\n",
1597 + ex->ee_block, ex->ee_len, ex->ee_start);
1602 + /* reuse unused slots */
1604 + if (lu->ee_start) {
1613 + if (correct_index && eh->eh_entries)
1614 + err = ext3_ext_correct_indexes(handle, tree, path);
1616 + /* if this leaf is free, then we should
1617 + * remove it from index block above */
1618 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1619 + err = ext3_ext_rm_idx(handle, tree, path + depth);
1626 +static struct ext3_extent_idx *
1627 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1629 + struct ext3_extent_idx *ix;
1631 + ix = EXT_LAST_INDEX(hdr);
1632 + while (ix != EXT_FIRST_INDEX(hdr)) {
1633 + if (ix->ei_block <= block)
1641 + * returns 1 if current index have to be freed (even partial)
1644 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1646 + EXT_ASSERT(path->p_idx);
1648 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1652 + * if truncate on deeper level happened it it wasn't partial
1653 + * so we have to consider current index for truncation
1655 + if (path->p_hdr->eh_entries == path->p_block)
1660 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1661 + unsigned long start, unsigned long end)
1663 + struct inode *inode = tree->inode;
1664 + struct super_block *sb = inode->i_sb;
1665 + int depth = EXT_DEPTH(tree);
1666 + struct ext3_ext_path *path;
1668 + int i = 0, err = 0;
1670 + ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1672 + /* probably first extent we're gonna free will be last in block */
1673 + handle = ext3_journal_start(inode, depth + 1);
1674 + if (IS_ERR(handle))
1675 + return PTR_ERR(handle);
1677 + ext3_ext_invalidate_cache(tree);
1680 + * we start scanning from right side freeing all the blocks
1681 + * after i_size and walking into the deep
1683 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1684 + if (IS_ERR(path)) {
1685 + ext3_error(sb, "ext3_ext_remove_space",
1686 + "Can't allocate path array");
1687 + ext3_journal_stop(handle);
1690 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1691 + path[i].p_hdr = EXT_ROOT_HDR(tree);
1693 + while (i >= 0 && err == 0) {
1695 + /* this is leaf block */
1696 + err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1697 + /* root level have p_bh == NULL, brelse() eats this */
1698 + brelse(path[i].p_bh);
1703 + /* this is index block */
1704 + if (!path[i].p_hdr) {
1705 + ext_debug(tree, "initialize header\n");
1706 + path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1709 + EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1710 + EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1712 + if (!path[i].p_idx) {
1713 + /* this level hasn't touched yet */
1715 + ext3_ext_last_covered(path[i].p_hdr, end);
1716 + path[i].p_block = path[i].p_hdr->eh_entries + 1;
1717 + ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1718 + path[i].p_hdr, path[i].p_hdr->eh_entries);
1720 + /* we've already was here, see at next index */
1724 + ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1725 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1727 + if (ext3_ext_more_to_rm(path + i)) {
1728 + /* go to the next level */
1729 + ext_debug(tree, "move to level %d (block %d)\n",
1730 + i + 1, path[i].p_idx->ei_leaf);
1731 + memset(path + i + 1, 0, sizeof(*path));
1732 + path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1733 + if (!path[i+1].p_bh) {
1734 + /* should we reset i_size? */
1738 + /* put actual number of indexes to know is this
1739 + * number got changed at the next iteration */
1740 + path[i].p_block = path[i].p_hdr->eh_entries;
1743 + /* we finish processing this index, go up */
1744 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1745 + /* index is empty, remove it
1746 + * handle must be already prepared by the
1747 + * truncatei_leaf() */
1748 + err = ext3_ext_rm_idx(handle, tree, path + i);
1750 + /* root level have p_bh == NULL, brelse() eats this */
1751 + brelse(path[i].p_bh);
1753 + ext_debug(tree, "return to level %d\n", i);
1757 + /* TODO: flexible tree reduction should be here */
1758 + if (path->p_hdr->eh_entries == 0) {
1760 + * truncate to zero freed all the tree
1761 + * so, we need to correct eh_depth
1763 + err = ext3_ext_get_access(handle, tree, path);
1765 + EXT_ROOT_HDR(tree)->eh_depth = 0;
1766 + EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1767 + err = ext3_ext_dirty(handle, tree, path);
1770 + ext3_ext_tree_changed(tree);
1773 + ext3_journal_stop(handle);
1778 +int ext3_ext_calc_metadata_amount(struct ext3_extents_tree *tree, int blocks)
1780 + int lcap, icap, rcap, leafs, idxs, num;
1782 + rcap = ext3_ext_space_root(tree);
1783 + if (blocks <= rcap) {
1784 + /* all extents fit to the root */
1788 + rcap = ext3_ext_space_root_idx(tree);
1789 + lcap = ext3_ext_space_block(tree);
1790 + icap = ext3_ext_space_block_idx(tree);
1792 + num = leafs = (blocks + lcap - 1) / lcap;
1793 + if (leafs <= rcap) {
1794 + /* all pointers to leafs fit to the root */
1798 + /* ok. we need separate index block(s) to link all leaf blocks */
1799 + idxs = (leafs + icap - 1) / icap;
1802 + idxs = (idxs + icap - 1) / icap;
1803 + } while (idxs > rcap);
1809 + * called at mount time
1811 +void ext3_ext_init(struct super_block *sb)
1814 + * possible initialization would be here
1817 + if (test_opt(sb, EXTENTS)) {
1818 + printk("EXT3-fs: file extents enabled");
1819 +#ifdef AGRESSIVE_TEST
1820 + printk(", agressive tests");
1822 +#ifdef CHECK_BINSEARCH
1823 + printk(", check binsearch");
1830 + * called at umount time
1832 +void ext3_ext_release(struct super_block *sb)
1836 +/************************************************************************
1837 + * VFS related routines
1838 + ************************************************************************/
1840 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1842 + /* we use in-core data, not bh */
1846 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1848 + struct inode *inode = buffer;
1849 + return ext3_mark_inode_dirty(handle, inode);
1852 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1853 + struct ext3_extent *ex2)
1855 + /* FIXME: support for large fs */
1856 + if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1862 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1863 + struct ext3_extent *ex,
1864 + unsigned long from, unsigned long to)
1868 + /* at present, extent can't cross block group */;
1869 + needed = 4; /* bitmap + group desc + sb + inode */
1871 +#ifdef CONFIG_QUOTA
1872 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1878 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1879 + struct ext3_extent *ex,
1880 + unsigned long from, unsigned long to)
1882 + int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1883 + handle_t *handle = ext3_journal_start(tree->inode, needed);
1884 + struct buffer_head *bh;
1887 + if (IS_ERR(handle))
1888 + return PTR_ERR(handle);
1889 + if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1890 + /* tail removal */
1891 + unsigned long num, start;
1892 + num = ex->ee_block + ex->ee_len - from;
1893 + start = ex->ee_start + ex->ee_len - num;
1894 + ext_debug(tree, "free last %lu blocks starting %lu\n",
1896 + for (i = 0; i < num; i++) {
1897 + bh = sb_find_get_block(tree->inode->i_sb, start + i);
1898 + ext3_forget(handle, 0, tree->inode, bh, start + i);
1900 + ext3_free_blocks(handle, tree->inode, start, num);
1901 + } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1902 + printk("strange request: removal %lu-%lu from %u:%u\n",
1903 + from, to, ex->ee_block, ex->ee_len);
1905 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1906 + from, to, ex->ee_block, ex->ee_len);
1908 + ext3_journal_stop(handle);
1912 +static int ext3_ext_find_goal(struct inode *inode,
1913 + struct ext3_ext_path *path, unsigned long block)
1915 + struct ext3_inode_info *ei = EXT3_I(inode);
1916 + unsigned long bg_start;
1917 + unsigned long colour;
1921 + struct ext3_extent *ex;
1922 + depth = path->p_depth;
1924 + /* try to predict block placement */
1925 + if ((ex = path[depth].p_ext))
1926 + return ex->ee_start + (block - ex->ee_block);
1928 + /* it looks index is empty
1929 + * try to find starting from index itself */
1930 + if (path[depth].p_bh)
1931 + return path[depth].p_bh->b_blocknr;
1934 + /* OK. use inode's group */
1935 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1936 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1937 + colour = (current->pid % 16) *
1938 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1939 + return bg_start + colour + block;
1942 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1943 + struct ext3_ext_path *path,
1944 + struct ext3_extent *ex, int *err)
1946 + struct inode *inode = tree->inode;
1947 + int newblock, goal;
1951 + EXT_ASSERT(ex->ee_start);
1952 + EXT_ASSERT(ex->ee_len);
1954 + /* reuse block from the extent to order data/metadata */
1955 + newblock = ex->ee_start++;
1957 + if (ex->ee_len == 0) {
1959 + /* allocate new block for the extent */
1960 + goal = ext3_ext_find_goal(inode, path, ex->ee_block);
1961 + ex->ee_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1962 + if (ex->ee_start == 0) {
1963 + /* error occured: restore old extent */
1964 + ex->ee_start = newblock;
1971 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
1972 + .get_write_access = ext3_get_inode_write_access,
1973 + .mark_buffer_dirty = ext3_mark_buffer_dirty,
1974 + .mergable = ext3_ext_mergable,
1975 + .new_block = ext3_new_block_cb,
1976 + .remove_extent = ext3_remove_blocks,
1977 + .remove_extent_credits = ext3_remove_blocks_credits,
1980 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1981 + struct inode *inode)
1983 + tree->inode = inode;
1984 + tree->root = (void *) EXT3_I(inode)->i_data;
1985 + tree->buffer = (void *) inode;
1986 + tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1987 + tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1988 + tree->ops = &ext3_blockmap_helpers;
1991 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
1992 + long iblock, struct buffer_head *bh_result,
1993 + int create, int extend_disksize)
1995 + struct ext3_ext_path *path = NULL;
1996 + struct ext3_extent newex;
1997 + struct ext3_extent *ex;
1998 + int goal, newblock, err = 0, depth;
1999 + struct ext3_extents_tree tree;
2001 + clear_buffer_new(bh_result);
2002 + ext3_init_tree_desc(&tree, inode);
2003 + ext_debug(&tree, "block %d requested for inode %u\n",
2004 + (int) iblock, (unsigned) inode->i_ino);
2005 + down(&EXT3_I(inode)->truncate_sem);
2007 + /* check in cache */
2008 + if (ext3_ext_in_cache(&tree, iblock, &newex)) {
2009 + if (newex.ee_start == EXT_CACHE_MARK) {
2010 + /* this is cached gap */
2012 + /* block isn't allocated yet and
2013 + * user don't want to allocate it */
2016 + /* we should allocate requested block */
2017 + } else if (newex.ee_start) {
2018 + /* block is already allocated */
2019 + newblock = iblock - newex.ee_block + newex.ee_start;
2024 + /* find extent for this block */
2025 + path = ext3_ext_find_extent(&tree, iblock, NULL);
2026 + if (IS_ERR(path)) {
2027 + err = PTR_ERR(path);
2032 + depth = EXT_DEPTH(&tree);
2035 + * consistent leaf must not be empty
2036 + * this situations is possible, though, _during_ tree modification
2037 + * this is why assert can't be put in ext3_ext_find_extent()
2039 + EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2041 + if ((ex = path[depth].p_ext)) {
2042 + /* if found exent covers block, simple return it */
2043 + if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2044 + newblock = iblock - ex->ee_block + ex->ee_start;
2045 + ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2046 + (int) iblock, ex->ee_block, ex->ee_len,
2048 + ext3_ext_put_in_cache(&tree, ex);
2054 + * requested block isn't allocated yet
2055 + * we couldn't try to create block if create flag is zero
2058 + /* put just found gap into cache to speedup subsequest reqs */
2059 + ext3_ext_put_gap_in_cache(&tree, path, iblock);
2063 + /* allocate new block */
2064 + goal = ext3_ext_find_goal(inode, path, iblock);
2065 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2068 + ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2071 + /* try to insert new extent into found leaf and return */
2072 + newex.ee_block = iblock;
2073 + newex.ee_start = newblock;
2075 + err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2079 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2080 + EXT3_I(inode)->i_disksize = inode->i_size;
2082 + /* previous routine could use block we allocated */
2083 + newblock = newex.ee_start;
2084 + set_buffer_new(bh_result);
2086 + ext3_ext_put_in_cache(&tree, &newex);
2088 + ext3_ext_show_leaf(&tree, path);
2089 + map_bh(bh_result, inode->i_sb, newblock);
2092 + ext3_ext_drop_refs(path);
2095 + up(&EXT3_I(inode)->truncate_sem);
2100 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2102 + struct address_space *mapping = inode->i_mapping;
2103 + struct super_block *sb = inode->i_sb;
2104 + struct ext3_extents_tree tree;
2105 + unsigned long last_block;
2109 + ext3_init_tree_desc(&tree, inode);
2112 + * probably first extent we're gonna free will be last in block
2114 + err = ext3_writepage_trans_blocks(inode) + 3;
2115 + handle = ext3_journal_start(inode, err);
2116 + if (IS_ERR(handle)) {
2118 + clear_highpage(page);
2119 + flush_dcache_page(page);
2120 + unlock_page(page);
2121 + page_cache_release(page);
2127 + ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2129 + down(&EXT3_I(inode)->truncate_sem);
2130 + ext3_ext_invalidate_cache(&tree);
2133 + * TODO: optimization is possible here
2134 + * probably we need not scaning at all,
2135 + * because page truncation is enough
2137 + if (ext3_orphan_add(handle, inode))
2140 + /* we have to know where to truncate from in crash case */
2141 + EXT3_I(inode)->i_disksize = inode->i_size;
2142 + ext3_mark_inode_dirty(handle, inode);
2144 + last_block = (inode->i_size + sb->s_blocksize - 1)
2145 + >> EXT3_BLOCK_SIZE_BITS(sb);
2146 + err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2148 + /* In a multi-transaction truncate, we only make the final
2149 + * transaction synchronous */
2150 + if (IS_SYNC(inode))
2151 + handle->h_sync = 1;
2155 + * If this was a simple ftruncate(), and the file will remain alive
2156 + * then we need to clear up the orphan record which we created above.
2157 + * However, if this was a real unlink then we were called by
2158 + * ext3_delete_inode(), and we allow that function to clean up the
2159 + * orphan info for us.
2161 + if (inode->i_nlink)
2162 + ext3_orphan_del(handle, inode);
2164 + up(&EXT3_I(inode)->truncate_sem);
2165 + ext3_journal_stop(handle);
2169 + * this routine calculate max number of blocks we could modify
2170 + * in order to allocate new block for an inode
2172 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2174 + struct ext3_extents_tree tree;
2177 + ext3_init_tree_desc(&tree, inode);
2179 + needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2181 + /* caller want to allocate num blocks */
2184 +#ifdef CONFIG_QUOTA
2186 + * FIXME: real calculation should be here
2187 + * it depends on blockmap format of qouta file
2189 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2195 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2197 + struct ext3_extents_tree tree;
2199 + ext3_init_tree_desc(&tree, inode);
2200 + ext3_extent_tree_init(handle, &tree);
2203 +int ext3_ext_calc_blockmap_metadata(struct inode *inode, int blocks)
2205 + struct ext3_extents_tree tree;
2207 + ext3_init_tree_desc(&tree, inode);
2208 + return ext3_ext_calc_metadata_amount(&tree, blocks);
2212 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2213 + struct ext3_ext_path *path,
2214 + struct ext3_extent *newex, int exist)
2216 + struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2219 + return EXT_CONTINUE;
2222 + if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2225 + if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2227 + buf->cur += sizeof(*newex);
2229 + buf->err = -EFAULT;
2232 + return EXT_CONTINUE;
2236 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2237 + struct ext3_ext_path *path,
2238 + struct ext3_extent *ex, int exist)
2240 + struct ext3_extent_tree_stats *buf =
2241 + (struct ext3_extent_tree_stats *) tree->private;
2245 + return EXT_CONTINUE;
2247 + depth = EXT_DEPTH(tree);
2248 + buf->extents_num++;
2249 + if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2251 + return EXT_CONTINUE;
2254 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2255 + unsigned long arg)
2259 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2262 + if (cmd == EXT3_IOC_GET_EXTENTS) {
2263 + struct ext3_extent_buf buf;
2264 + struct ext3_extents_tree tree;
2266 + if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2269 + ext3_init_tree_desc(&tree, inode);
2270 + buf.cur = buf.buffer;
2272 + tree.private = &buf;
2273 + down(&EXT3_I(inode)->truncate_sem);
2274 + err = ext3_ext_walk_space(&tree, buf.start, EXT_MAX_BLOCK,
2275 + ext3_ext_store_extent_cb);
2276 + up(&EXT3_I(inode)->truncate_sem);
2279 + } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2280 + struct ext3_extent_tree_stats buf;
2281 + struct ext3_extents_tree tree;
2283 + ext3_init_tree_desc(&tree, inode);
2284 + down(&EXT3_I(inode)->truncate_sem);
2285 + buf.depth = EXT_DEPTH(&tree);
2286 + buf.extents_num = 0;
2288 + tree.private = &buf;
2289 + err = ext3_ext_walk_space(&tree, 0, EXT_MAX_BLOCK,
2290 + ext3_ext_collect_stats_cb);
2291 + up(&EXT3_I(inode)->truncate_sem);
2293 + err = copy_to_user((void *) arg, &buf, sizeof(buf));
2294 + } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2295 + struct ext3_extents_tree tree;
2296 + ext3_init_tree_desc(&tree, inode);
2297 + down(&EXT3_I(inode)->truncate_sem);
2298 + err = EXT_DEPTH(&tree);
2299 + up(&EXT3_I(inode)->truncate_sem);
2305 +EXPORT_SYMBOL(ext3_init_tree_desc);
2306 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2307 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2308 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2309 +EXPORT_SYMBOL(ext3_ext_walk_space);
2310 +EXPORT_SYMBOL(ext3_ext_find_goal);
2311 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2313 Index: linux-2.6.7/fs/ext3/ialloc.c
2314 ===================================================================
2315 --- linux-2.6.7.orig/fs/ext3/ialloc.c 2004-09-12 18:53:35.000000000 +0400
2316 +++ linux-2.6.7/fs/ext3/ialloc.c 2004-09-12 19:09:54.000000000 +0400
2317 @@ -646,6 +646,17 @@
2318 DQUOT_FREE_INODE(inode);
2321 + if (test_opt(sb, EXTENTS)) {
2322 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2323 + ext3_extents_initialize_blockmap(handle, inode);
2324 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2325 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2326 + if (err) goto fail;
2327 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2328 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2329 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2332 err = ext3_mark_inode_dirty(handle, inode);
2334 ext3_std_error(sb, err);
2335 Index: linux-2.6.7/fs/ext3/inode.c
2336 ===================================================================
2337 --- linux-2.6.7.orig/fs/ext3/inode.c 2004-09-12 18:53:35.000000000 +0400
2338 +++ linux-2.6.7/fs/ext3/inode.c 2004-09-12 18:53:38.000000000 +0400
2339 @@ -857,6 +857,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 @@ -1669,7 +1680,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 @@ -2165,6 +2176,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 @@ -2888,6 +2902,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.7/fs/ext3/super.c
2418 ===================================================================
2419 --- linux-2.6.7.orig/fs/ext3/super.c 2004-09-12 18:53:35.000000000 +0400
2420 +++ linux-2.6.7/fs/ext3/super.c 2004-09-12 18:53:38.000000000 +0400
2422 struct ext3_super_block *es = sbi->s_es;
2425 + ext3_ext_release(sb);
2426 ext3_xattr_put_super(sb);
2427 journal_destroy(sbi->s_journal);
2428 if (!(sb->s_flags & MS_RDONLY)) {
2430 dynlock_init(&ei->i_htree_lock);
2431 sema_init(&ei->i_rename_sem, 1);
2432 sema_init(&ei->i_append_sem, 1);
2433 + ei->i_cached_extent[0] = 0;
2434 + ei->i_cached_extent[1] = 0;
2435 + ei->i_cached_extent[2] = 0;
2436 return &ei->vfs_inode;
2440 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
2441 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
2442 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_pdirops,
2443 - Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2444 + Opt_iopen, Opt_noiopen, Opt_iopen_nopriv, Opt_extents, Opt_extdebug,
2445 Opt_ignore, Opt_err,
2449 {Opt_iopen, "iopen"},
2450 {Opt_noiopen, "noiopen"},
2451 {Opt_iopen_nopriv, "iopen_nopriv"},
2452 + {Opt_extents, "extents"},
2453 + {Opt_extdebug, "extdebug"},
2457 @@ -921,6 +927,12 @@
2462 + set_opt (sbi->s_mount_opt, EXTENTS);
2464 + case Opt_extdebug:
2465 + set_opt (sbi->s_mount_opt, EXTDEBUG);
2469 "EXT3-fs: Unrecognized mount option \"%s\" "
2470 @@ -1593,6 +1605,8 @@
2471 percpu_counter_mod(&sbi->s_dirs_counter,
2472 ext3_count_dirs(sb));
2474 + ext3_ext_init(sb);
2479 Index: linux-2.6.7/fs/ext3/ioctl.c
2480 ===================================================================
2481 --- linux-2.6.7.orig/fs/ext3/ioctl.c 2004-09-12 18:53:32.000000000 +0400
2482 +++ linux-2.6.7/fs/ext3/ioctl.c 2004-09-12 18:53:38.000000000 +0400
2483 @@ -176,6 +176,10 @@
2487 + case EXT3_IOC_GET_EXTENTS:
2488 + case EXT3_IOC_GET_TREE_STATS:
2489 + case EXT3_IOC_GET_TREE_DEPTH:
2490 + return ext3_ext_ioctl(inode, filp, cmd, arg);
2494 Index: linux-2.6.7/fs/ext3/Makefile
2495 ===================================================================
2496 --- linux-2.6.7.orig/fs/ext3/Makefile 2004-09-12 18:53:33.000000000 +0400
2497 +++ linux-2.6.7/fs/ext3/Makefile 2004-09-12 18:53:38.000000000 +0400
2499 obj-$(CONFIG_EXT3_FS) += ext3.o
2501 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2502 - ioctl.o namei.o super.o symlink.o hash.o
2503 + ioctl.o namei.o super.o symlink.o hash.o extents.o
2505 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2506 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2507 Index: linux-2.6.7/include/linux/ext3_fs.h
2508 ===================================================================
2509 --- linux-2.6.7.orig/include/linux/ext3_fs.h 2004-09-12 18:53:37.000000000 +0400
2510 +++ linux-2.6.7/include/linux/ext3_fs.h 2004-09-12 19:13:48.000000000 +0400
2512 #define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2513 #define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2514 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2515 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2517 #define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
2518 #define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
2520 #ifdef CONFIG_JBD_DEBUG
2521 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
2523 +#define EXT3_IOC_GET_EXTENTS _IOR('f', 5, long)
2524 +#define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 6, long)
2525 +#define EXT3_IOC_GET_TREE_STATS _IOR('f', 7, long)
2528 * Structure of an inode on the disk
2530 #define EXT3_MOUNT_POSIX_ACL 0x8000 /* POSIX Access Control Lists */
2531 #define EXT3_MOUNT_IOPEN 0x40000 /* Allow access via iopen */
2532 #define EXT3_MOUNT_IOPEN_NOPRIV 0x80000 /* Make iopen world-readable */
2533 +#define EXT3_MOUNT_EXTENTS 0x10000 /* Extents support */
2534 +#define EXT3_MOUNT_EXTDEBUG 0x20000 /* Extents debug */
2536 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2538 @@ -518,11 +524,13 @@
2539 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2540 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2541 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
2542 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2544 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2545 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2546 EXT3_FEATURE_INCOMPAT_RECOVER| \
2547 - EXT3_FEATURE_INCOMPAT_META_BG)
2548 + EXT3_FEATURE_INCOMPAT_META_BG| \
2549 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2550 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2551 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2552 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2557 +extern int ext3_block_truncate_page(handle_t *, struct page *, struct address_space *, loff_t);
2558 extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2559 extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2560 extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2561 @@ -798,6 +807,14 @@
2562 extern struct inode_operations ext3_symlink_inode_operations;
2563 extern struct inode_operations ext3_fast_symlink_inode_operations;
2566 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2567 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2568 + struct buffer_head *, int, int);
2569 +extern void ext3_ext_truncate(struct inode *, struct page *);
2570 +extern void ext3_ext_init(struct super_block *);
2571 +extern void ext3_ext_release(struct super_block *);
2572 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2574 #endif /* __KERNEL__ */
2576 Index: linux-2.6.7/include/linux/ext3_extents.h
2577 ===================================================================
2578 --- linux-2.6.7.orig/include/linux/ext3_extents.h 2003-01-30 13:24:37.000000000 +0300
2579 +++ linux-2.6.7/include/linux/ext3_extents.h 2004-09-12 18:53:38.000000000 +0400
2582 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
2583 + * Written by Alex Tomas <alex@clusterfs.com>
2585 + * This program is free software; you can redistribute it and/or modify
2586 + * it under the terms of the GNU General Public License version 2 as
2587 + * published by the Free Software Foundation.
2589 + * This program is distributed in the hope that it will be useful,
2590 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2591 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2592 + * GNU General Public License for more details.
2594 + * You should have received a copy of the GNU General Public Licens
2595 + * along with this program; if not, write to the Free Software
2596 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2599 +#ifndef _LINUX_EXT3_EXTENTS
2600 +#define _LINUX_EXT3_EXTENTS
2603 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2604 + * become very little, so index split, in-depth growing and
2605 + * other hard changes happens much more often
2606 + * this is for debug purposes only
2608 +#define AGRESSIVE_TEST_
2611 + * if CHECK_BINSEARCH defined, then results of binary search
2612 + * will be checked by linear search
2614 +#define CHECK_BINSEARCH_
2617 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2618 + * to get lots of info what's going on
2622 +#define ext_debug(tree,fmt,a...) \
2624 + if (test_opt((tree)->inode->i_sb, EXTDEBUG)) \
2625 + printk(fmt, ##a); \
2628 +#define ext_debug(tree,fmt,a...)
2632 + * if EXT_STATS is defined then stats numbers are collected
2633 + * these number will be displayed at umount time
2638 +#define EXT3_ALLOC_NEEDED 3 /* block bitmap + group desc. + sb */
2641 + * ext3_inode has i_block array (total 60 bytes)
2642 + * first 4 bytes are used to store:
2643 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
2644 + * - number of alive extents in the inode
2648 + * this is extent on-disk structure
2649 + * it's used at the bottom of the tree
2651 +struct ext3_extent {
2652 + __u32 ee_block; /* first logical block extent covers */
2653 + __u16 ee_len; /* number of blocks covered by extent */
2654 + __u16 ee_start_hi; /* high 16 bits of physical block */
2655 + __u32 ee_start; /* low 32 bigs of physical block */
2659 + * this is index on-disk structure
2660 + * it's used at all the levels, but the bottom
2662 +struct ext3_extent_idx {
2663 + __u32 ei_block; /* index covers logical blocks from 'block' */
2664 + __u32 ei_leaf; /* pointer to the physical block of the next *
2665 + * level. leaf or next index could bet here */
2666 + __u16 ei_leaf_hi; /* high 16 bits of physical block */
2671 + * each block (leaves and indexes), even inode-stored has header
2673 +struct ext3_extent_header {
2674 + __u16 eh_magic; /* probably will support different formats */
2675 + __u16 eh_entries; /* number of valid entries */
2676 + __u16 eh_max; /* capacity of store in entries */
2677 + __u16 eh_depth; /* has tree real underlaying blocks? */
2678 + __u32 eh_generation; /* generation of the tree */
2681 +#define EXT3_EXT_MAGIC 0xf30a
2684 + * array of ext3_ext_path contains path to some extent
2685 + * creation/lookup routines use it for traversal/splitting/etc
2686 + * truncate uses it to simulate recursive walking
2688 +struct ext3_ext_path {
2691 + struct ext3_extent *p_ext;
2692 + struct ext3_extent_idx *p_idx;
2693 + struct ext3_extent_header *p_hdr;
2694 + struct buffer_head *p_bh;
2698 + * structure for external API
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_extent *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->ee_len = 0;
2817 +#endif /* _LINUX_EXT3_EXTENTS */
2819 Index: linux-2.6.7/include/linux/ext3_fs_i.h
2820 ===================================================================
2821 --- linux-2.6.7.orig/include/linux/ext3_fs_i.h 2004-09-12 18:53:35.000000000 +0400
2822 +++ linux-2.6.7/include/linux/ext3_fs_i.h 2004-09-12 18:53:38.000000000 +0400
2824 struct dynlock i_htree_lock;
2825 struct semaphore i_append_sem;
2826 struct semaphore i_rename_sem;
2828 + __u32 i_cached_extent[3];
2831 #endif /* _LINUX_EXT3_FS_I */
2834 fs/ext3/Makefile | 2
2835 fs/ext3/extents.c | 2306 +++++++++++++++++++++++++++++++++++++++++++
2836 fs/ext3/ialloc.c | 11
2837 fs/ext3/inode.c | 29
2839 fs/ext3/super.c | 16
2840 include/linux/ext3_extents.h | 238 ++++
2841 include/linux/ext3_fs.h | 19
2842 include/linux/ext3_fs_i.h | 2
2843 9 files changed, 2618 insertions(+), 9 deletions(-)