1 Index: linux-2.6.18.8/fs/ext3/dir.c
2 ===================================================================
3 --- linux-2.6.18.8.orig/fs/ext3/dir.c 2007-02-24 00:52:30.000000000 +0100
4 +++ linux-2.6.18.8/fs/ext3/dir.c 2007-07-17 09:18:14.000000000 +0200
5 @@ -131,8 +131,7 @@ static int ext3_readdir(struct file * fi
6 struct buffer_head *bh = NULL;
9 - err = ext3_get_blocks_handle(NULL, inode, blk, 1,
11 + err = ext3_get_blocks_wrap(NULL, inode, blk, 1, &map_bh, 0, 0);
13 page_cache_readahead(sb->s_bdev->bd_inode->i_mapping,
15 Index: linux-2.6.18.8/fs/ext3/extents.c
16 ===================================================================
17 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
18 +++ linux-2.6.18.8/fs/ext3/extents.c 2007-07-17 11:08:59.000000000 +0200
21 + * Copyright 2008, Sun Microsystems, Inc.
22 + * Written by Alex Tomas <alex@clusterfs.com>
24 + * Architecture independence:
25 + * Copyright (c) 2005, Bull S.A.
26 + * Written by Pierre Peiffer <pierre.peiffer@bull.net>
28 + * This program is free software; you can redistribute it and/or modify
29 + * it under the terms of the GNU General Public License version 2 as
30 + * published by the Free Software Foundation.
32 + * This program is distributed in the hope that it will be useful,
33 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 + * GNU General Public License for more details.
37 + * You should have received a copy of the GNU General Public Licens
38 + * along with this program; if not, write to the Free Software
39 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
43 + * Extents support for EXT3
46 + * - ext3*_error() should be used in some situations
47 + * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
48 + * - smart tree reduction
51 +#include <linux/module.h>
52 +#include <linux/fs.h>
53 +#include <linux/time.h>
54 +#include <linux/ext3_jbd.h>
55 +#include <linux/jbd.h>
56 +#include <linux/smp_lock.h>
57 +#include <linux/highuid.h>
58 +#include <linux/pagemap.h>
59 +#include <linux/quotaops.h>
60 +#include <linux/string.h>
61 +#include <linux/slab.h>
62 +#include <linux/ext3_extents.h>
63 +#include <asm/uaccess.h>
66 +static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
70 + if (handle->h_buffer_credits > needed)
72 + if (!ext3_journal_extend(handle, needed))
74 + err = ext3_journal_restart(handle, needed);
84 +static int ext3_ext_get_access(handle_t *handle, struct inode *inode,
85 + struct ext3_ext_path *path)
88 + /* path points to block */
89 + return ext3_journal_get_write_access(handle, path->p_bh);
91 + /* path points to leaf/index in inode body */
92 + /* we use in-core data, no need to protect them */
102 +static int ext3_ext_dirty(handle_t *handle, struct inode *inode,
103 + struct ext3_ext_path *path)
107 + /* path points to block */
108 + err = ext3_journal_dirty_metadata(handle, path->p_bh);
110 + /* path points to leaf/index in inode body */
111 + err = ext3_mark_inode_dirty(handle, inode);
116 +static int ext3_ext_find_goal(struct inode *inode,
117 + struct ext3_ext_path *path,
118 + unsigned long block)
120 + struct ext3_inode_info *ei = EXT3_I(inode);
121 + unsigned long bg_start;
122 + unsigned long colour;
126 + struct ext3_extent *ex;
127 + depth = path->p_depth;
129 + /* try to predict block placement */
130 + if ((ex = path[depth].p_ext))
131 + return le32_to_cpu(ex->ee_start)
132 + + (block - le32_to_cpu(ex->ee_block));
134 + /* it looks index is empty
135 + * try to find starting from index itself */
136 + if (path[depth].p_bh)
137 + return path[depth].p_bh->b_blocknr;
140 + /* OK. use inode's group */
141 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
142 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
143 + colour = (current->pid % 16) *
144 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
145 + return bg_start + colour + block;
149 +ext3_ext_new_block(handle_t *handle, struct inode *inode,
150 + struct ext3_ext_path *path,
151 + struct ext3_extent *ex, int *err)
153 + int goal, newblock;
155 + goal = ext3_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
156 + newblock = ext3_new_block(handle, inode, goal, err);
160 +static inline int ext3_ext_space_block(struct inode *inode)
164 + size = (inode->i_sb->s_blocksize - sizeof(struct ext3_extent_header))
165 + / sizeof(struct ext3_extent);
166 +#ifdef AGRESSIVE_TEST
173 +static inline int ext3_ext_space_block_idx(struct inode *inode)
177 + size = (inode->i_sb->s_blocksize - sizeof(struct ext3_extent_header))
178 + / sizeof(struct ext3_extent_idx);
179 +#ifdef AGRESSIVE_TEST
186 +static inline int ext3_ext_space_root(struct inode *inode)
190 + size = sizeof(EXT3_I(inode)->i_data);
191 + size -= sizeof(struct ext3_extent_header);
192 + size /= sizeof(struct ext3_extent);
193 +#ifdef AGRESSIVE_TEST
200 +static inline int ext3_ext_space_root_idx(struct inode *inode)
204 + size = sizeof(EXT3_I(inode)->i_data);
205 + size -= sizeof(struct ext3_extent_header);
206 + size /= sizeof(struct ext3_extent_idx);
207 +#ifdef AGRESSIVE_TEST
215 +ext3_ext_max_entries(struct inode *inode, int depth)
219 + if (depth == ext_depth(inode)) {
221 + max = ext3_ext_space_root(inode);
223 + max = ext3_ext_space_root_idx(inode);
226 + max = ext3_ext_space_block(inode);
228 + max = ext3_ext_space_block_idx(inode);
234 +static int __ext3_ext_check_header(const char *function, int line, struct inode *inode,
235 + struct ext3_extent_header *eh,
238 + const char *error_msg = NULL;
241 + if (unlikely(eh->eh_magic != cpu_to_le16(EXT3_EXT_MAGIC))) {
242 + error_msg = "invalid magic";
245 + if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
246 + error_msg = "unexpected eh_depth";
249 + if (unlikely(eh->eh_max == 0)) {
250 + error_msg = "invalid eh_max";
253 + max = ext3_ext_max_entries(inode, depth);
254 +#ifdef AGRESSIVE_TEST
255 + if (eh->eh_max > 3) {
256 + /* inode probably got extent without defining AGRESSIVE_TEST */
260 + if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
261 + error_msg = "too large eh_max";
264 + if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
265 + error_msg = "invalid eh_entries";
268 + if (unlikely((eh->eh_entries == 0) && (eh->eh_depth != 0))) {
269 + error_msg = "invalid index, eh_entries=0 && eh_depth != 0";
275 + ext3_error(inode->i_sb, function,
276 + ":%d: bad header in inode #%lu: %s - magic %x, "
277 + "entries %u, max %u(%u), depth %u(%u)", line,
278 + inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
279 + le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
280 + max, le16_to_cpu(eh->eh_depth), depth);
285 +#define ext3_ext_check_header(inode,eh,depth) \
286 + __ext3_ext_check_header(__FUNCTION__,__LINE__,inode,eh,depth)
289 +static void ext3_ext_show_path(struct inode *inode, struct ext3_ext_path *path)
291 + int k, l = path->p_depth;
293 + ext_debug(inode, "path:");
294 + for (k = 0; k <= l; k++, path++) {
296 + ext_debug(inode, " %d->%d", le32_to_cpu(path->p_idx->ei_block),
297 + le32_to_cpu(path->p_idx->ei_leaf));
298 + } else if (path->p_ext) {
299 + ext_debug(inode, " %d:%d:%d",
300 + le32_to_cpu(path->p_ext->ee_block),
301 + le16_to_cpu(path->p_ext->ee_len),
302 + le32_to_cpu(path->p_ext->ee_start));
304 + ext_debug(inode, " []");
306 + ext_debug(inode, "\n");
309 +static void ext3_ext_show_leaf(struct inode *inode, struct ext3_ext_path *path)
311 + int depth = ext_depth(inode);
312 + struct ext3_extent_header *eh;
313 + struct ext3_extent *ex;
319 + eh = path[depth].p_hdr;
320 + ex = EXT_FIRST_EXTENT(eh);
322 + for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
323 + ext_debug(inode, "%d:%d:%d ", le32_to_cpu(ex->ee_block),
324 + le16_to_cpu(ex->ee_len),
325 + le32_to_cpu(ex->ee_start));
327 + ext_debug(inode, "\n");
330 +#define ext3_ext_show_path(inode,path)
331 +#define ext3_ext_show_leaf(inode,path)
334 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
336 + int depth = path->p_depth;
339 + for (i = 0; i <= depth; i++, path++)
341 + brelse(path->p_bh);
347 + * binary search for closest index by given block
348 + * the header must be checked before calling this
351 +ext3_ext_binsearch_idx(struct inode *inode, struct ext3_ext_path *path, int block)
353 + struct ext3_extent_header *eh = path->p_hdr;
354 + struct ext3_extent_idx *r, *l, *m;
356 + ext_debug(inode, "binsearch for %d(idx): ", block);
358 + l = EXT_FIRST_INDEX(eh) + 1;
359 + r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
361 + m = l + (r - l) / 2;
362 + if (block < le32_to_cpu(m->ei_block))
366 + ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
367 + m, m->ei_block, r, r->ei_block);
370 + path->p_idx = l - 1;
371 + ext_debug(inode, " -> %d->%d ", le32_to_cpu(path->p_idx->ei_block),
372 + le32_to_cpu(path->p_idx->ei_leaf));
374 +#ifdef CHECK_BINSEARCH
376 + struct ext3_extent_idx *chix, *ix;
379 + chix = ix = EXT_FIRST_INDEX(eh);
380 + for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
382 + le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
383 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
384 + ix, EXT_FIRST_INDEX(eh));
385 + printk("%u <= %u\n",
386 + le32_to_cpu(ix->ei_block),
387 + le32_to_cpu(ix[-1].ei_block));
389 + BUG_ON(k && le32_to_cpu(ix->ei_block)
390 + <= le32_to_cpu(ix[-1].ei_block));
391 + if (block < le32_to_cpu(ix->ei_block))
395 + BUG_ON(chix != path->p_idx);
402 + * binary search for closest extent by given block
403 + * the header must be checked before calling this
406 +ext3_ext_binsearch(struct inode *inode, struct ext3_ext_path *path, int block)
408 + struct ext3_extent_header *eh = path->p_hdr;
409 + struct ext3_extent *r, *l, *m;
411 + if (eh->eh_entries == 0) {
413 + * this leaf is empty yet:
414 + * we get such a leaf in split/add case
419 + ext_debug(inode, "binsearch for %d: ", block);
421 + l = EXT_FIRST_EXTENT(eh) + 1;
422 + r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
425 + m = l + (r - l) / 2;
426 + if (block < le32_to_cpu(m->ee_block))
430 + ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
431 + m, m->ee_block, r, r->ee_block);
434 + path->p_ext = l - 1;
435 + ext_debug(inode, " -> %d:%d:%d ",
436 + le32_to_cpu(path->p_ext->ee_block),
437 + le32_to_cpu(path->p_ext->ee_start),
438 + le16_to_cpu(path->p_ext->ee_len));
440 +#ifdef CHECK_BINSEARCH
442 + struct ext3_extent *chex, *ex;
445 + chex = ex = EXT_FIRST_EXTENT(eh);
446 + for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
447 + BUG_ON(k && le32_to_cpu(ex->ee_block)
448 + <= le32_to_cpu(ex[-1].ee_block));
449 + if (block < le32_to_cpu(ex->ee_block))
453 + BUG_ON(chex != path->p_ext);
459 +int ext3_ext_tree_init(handle_t *handle, struct inode *inode)
461 + struct ext3_extent_header *eh;
463 + eh = ext_inode_hdr(inode);
465 + eh->eh_entries = 0;
466 + eh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
467 + eh->eh_max = cpu_to_le16(ext3_ext_space_root(inode));
468 + ext3_mark_inode_dirty(handle, inode);
469 + ext3_ext_invalidate_cache(inode);
473 +struct ext3_ext_path *
474 +ext3_ext_find_extent(struct inode *inode, int block, struct ext3_ext_path *path)
476 + struct ext3_extent_header *eh;
477 + struct buffer_head *bh;
478 + short int depth, i, ppos = 0, alloc = 0;
480 + eh = ext_inode_hdr(inode);
481 + i = depth = ext_depth(inode);
482 + if (ext3_ext_check_header(inode, eh, depth))
483 + return ERR_PTR(-EIO);
485 + /* account possible depth increase */
487 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
490 + return ERR_PTR(-ENOMEM);
493 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
494 + path[0].p_hdr = eh;
496 + /* walk through the tree */
498 + ext_debug(inode, "depth %d: num %d, max %d\n",
499 + ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
501 + ext3_ext_binsearch_idx(inode, path + ppos, block);
502 + path[ppos].p_block = le32_to_cpu(path[ppos].p_idx->ei_leaf);
503 + path[ppos].p_depth = i;
504 + path[ppos].p_ext = NULL;
506 + bh = sb_bread(inode->i_sb, path[ppos].p_block);
510 + eh = ext_block_hdr(bh);
512 + BUG_ON(ppos > depth);
513 + path[ppos].p_bh = bh;
514 + path[ppos].p_hdr = eh;
517 + if (ext3_ext_check_header(inode, eh, i))
521 + path[ppos].p_depth = i;
522 + path[ppos].p_hdr = eh;
523 + path[ppos].p_ext = NULL;
524 + path[ppos].p_idx = NULL;
527 + ext3_ext_binsearch(inode, path + ppos, block);
529 + ext3_ext_show_path(inode, path);
534 + ext3_ext_drop_refs(path);
537 + return ERR_PTR(-EIO);
541 + * insert new index [logical;ptr] into the block at cupr
542 + * it check where to insert: before curp or after curp
544 +static int ext3_ext_insert_index(handle_t *handle, struct inode *inode,
545 + struct ext3_ext_path *curp,
546 + int logical, int ptr)
548 + struct ext3_extent_idx *ix;
551 + if ((err = ext3_ext_get_access(handle, inode, curp)))
554 + BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
555 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
556 + if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
558 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
559 + len = (len - 1) * sizeof(struct ext3_extent_idx);
560 + len = len < 0 ? 0 : len;
561 + ext_debug(inode, "insert new index %d after: %d. "
562 + "move %d from 0x%p to 0x%p\n",
564 + (curp->p_idx + 1), (curp->p_idx + 2));
565 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
567 + ix = curp->p_idx + 1;
569 + /* insert before */
570 + len = len * sizeof(struct ext3_extent_idx);
571 + len = len < 0 ? 0 : len;
572 + ext_debug(inode, "insert new index %d before: %d. "
573 + "move %d from 0x%p to 0x%p\n",
575 + curp->p_idx, (curp->p_idx + 1));
576 + memmove(curp->p_idx + 1, curp->p_idx, len);
580 + ix->ei_block = cpu_to_le32(logical);
581 + ix->ei_leaf = cpu_to_le32(ptr);
582 + ix->ei_leaf_hi = ix->ei_unused = 0;
583 + curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
585 + BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
586 + > le16_to_cpu(curp->p_hdr->eh_max));
587 + BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
589 + err = ext3_ext_dirty(handle, inode, curp);
590 + ext3_std_error(inode->i_sb, err);
596 + * routine inserts new subtree into the path, using free index entry
598 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
599 + * - makes decision where to split
600 + * - moves remaining extens and index entries (right to the split point)
601 + * into the newly allocated blocks
602 + * - initialize subtree
604 +static int ext3_ext_split(handle_t *handle, struct inode *inode,
605 + struct ext3_ext_path *path,
606 + struct ext3_extent *newext, int at)
608 + struct buffer_head *bh = NULL;
609 + int depth = ext_depth(inode);
610 + struct ext3_extent_header *neh;
611 + struct ext3_extent_idx *fidx;
612 + struct ext3_extent *ex;
613 + int i = at, k, m, a;
614 + unsigned long newblock, oldblock;
616 + int *ablocks = NULL; /* array of allocated blocks */
619 + /* make decision: where to split? */
620 + /* FIXME: now desicion is simplest: at current extent */
622 + /* if current leaf will be splitted, then we should use
623 + * border from split point */
624 + BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
625 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
626 + border = path[depth].p_ext[1].ee_block;
627 + ext_debug(inode, "leaf will be splitted."
628 + " next leaf starts at %d\n",
629 + le32_to_cpu(border));
631 + border = newext->ee_block;
632 + ext_debug(inode, "leaf will be added."
633 + " next leaf starts at %d\n",
634 + le32_to_cpu(border));
638 + * if error occurs, then we break processing
639 + * and turn filesystem read-only. so, index won't
640 + * be inserted and tree will be in consistent
641 + * state. next mount will repair buffers too
645 + * get array to track all allocated blocks
646 + * we need this to handle errors and free blocks
649 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
652 + memset(ablocks, 0, sizeof(unsigned long) * depth);
654 + /* allocate all needed blocks */
655 + ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at);
656 + for (a = 0; a < depth - at; a++) {
657 + newblock = ext3_ext_new_block(handle, inode, path, newext, &err);
660 + ablocks[a] = newblock;
663 + /* initialize new leaf */
664 + newblock = ablocks[--a];
665 + BUG_ON(newblock == 0);
666 + bh = sb_getblk(inode->i_sb, newblock);
673 + if ((err = ext3_journal_get_create_access(handle, bh)))
676 + neh = ext_block_hdr(bh);
677 + neh->eh_entries = 0;
678 + neh->eh_max = cpu_to_le16(ext3_ext_space_block(inode));
679 + neh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
681 + ex = EXT_FIRST_EXTENT(neh);
683 + /* move remain of path[depth] to the new leaf */
684 + BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
685 + /* start copy from next extent */
686 + /* TODO: we could do it by single memmove */
688 + path[depth].p_ext++;
689 + while (path[depth].p_ext <=
690 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
691 + ext_debug(inode, "move %d:%d:%d in new leaf %lu\n",
692 + le32_to_cpu(path[depth].p_ext->ee_block),
693 + le32_to_cpu(path[depth].p_ext->ee_start),
694 + le16_to_cpu(path[depth].p_ext->ee_len),
696 + /*memmove(ex++, path[depth].p_ext++,
697 + sizeof(struct ext3_extent));
698 + neh->eh_entries++;*/
699 + path[depth].p_ext++;
703 + memmove(ex, path[depth].p_ext-m, sizeof(struct ext3_extent)*m);
704 + neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
707 + set_buffer_uptodate(bh);
710 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
715 + /* correct old leaf */
717 + if ((err = ext3_ext_get_access(handle, inode, path + depth)))
719 + path[depth].p_hdr->eh_entries =
720 + cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
721 + if ((err = ext3_ext_dirty(handle, inode, path + depth)))
726 + /* create intermediate indexes */
727 + k = depth - at - 1;
730 + ext_debug(inode, "create %d intermediate indices\n", k);
731 + /* insert new index into current index block */
732 + /* current depth stored in i var */
735 + oldblock = newblock;
736 + newblock = ablocks[--a];
737 + bh = sb_getblk(inode->i_sb, newblock);
744 + if ((err = ext3_journal_get_create_access(handle, bh)))
747 + neh = ext_block_hdr(bh);
748 + neh->eh_entries = cpu_to_le16(1);
749 + neh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
750 + neh->eh_max = cpu_to_le16(ext3_ext_space_block_idx(inode));
751 + neh->eh_depth = cpu_to_le16(depth - i);
752 + fidx = EXT_FIRST_INDEX(neh);
753 + fidx->ei_block = border;
754 + fidx->ei_leaf = cpu_to_le32(oldblock);
755 + fidx->ei_leaf_hi = fidx->ei_unused = 0;
757 + ext_debug(inode, "int.index at %d (block %lu): %lu -> %lu\n", i,
758 + newblock, (unsigned long) le32_to_cpu(border),
764 + ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx,
765 + EXT_MAX_INDEX(path[i].p_hdr));
766 + BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
767 + EXT_LAST_INDEX(path[i].p_hdr));
768 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
769 + ext_debug(inode, "%d: move %d:%d in new index %lu\n", i,
770 + le32_to_cpu(path[i].p_idx->ei_block),
771 + le32_to_cpu(path[i].p_idx->ei_leaf),
773 + /*memmove(++fidx, path[i].p_idx++,
774 + sizeof(struct ext3_extent_idx));
776 + BUG_ON(neh->eh_entries > neh->eh_max);*/
781 + memmove(++fidx, path[i].p_idx - m,
782 + sizeof(struct ext3_extent_idx) * m);
784 + cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
786 + set_buffer_uptodate(bh);
789 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
794 + /* correct old index */
796 + err = ext3_ext_get_access(handle, inode, path + i);
799 + path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
800 + err = ext3_ext_dirty(handle, inode, path + i);
808 + /* insert new index */
812 + err = ext3_ext_insert_index(handle, inode, path + at,
813 + le32_to_cpu(border), newblock);
817 + if (buffer_locked(bh))
823 + /* free all allocated blocks in error case */
824 + for (i = 0; i < depth; i++) {
827 + ext3_free_blocks(handle, inode, ablocks[i], 1);
836 + * routine implements tree growing procedure:
837 + * - allocates new block
838 + * - moves top-level data (index block or leaf) into the new block
839 + * - initialize new top-level, creating index that points to the
840 + * just created block
842 +static int ext3_ext_grow_indepth(handle_t *handle, struct inode *inode,
843 + struct ext3_ext_path *path,
844 + struct ext3_extent *newext)
846 + struct ext3_ext_path *curp = path;
847 + struct ext3_extent_header *neh;
848 + struct ext3_extent_idx *fidx;
849 + struct buffer_head *bh;
850 + unsigned long newblock;
853 + newblock = ext3_ext_new_block(handle, inode, path, newext, &err);
857 + bh = sb_getblk(inode->i_sb, newblock);
860 + ext3_std_error(inode->i_sb, err);
865 + if ((err = ext3_journal_get_create_access(handle, bh))) {
870 + /* move top-level index/leaf into new block */
871 + memmove(bh->b_data, curp->p_hdr, sizeof(EXT3_I(inode)->i_data));
873 + /* set size of new block */
874 + neh = ext_block_hdr(bh);
875 + /* old root could have indexes or leaves
876 + * so calculate e_max right way */
877 + if (ext_depth(inode))
878 + neh->eh_max = cpu_to_le16(ext3_ext_space_block_idx(inode));
880 + neh->eh_max = cpu_to_le16(ext3_ext_space_block(inode));
881 + neh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
882 + set_buffer_uptodate(bh);
885 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
888 + /* create index in new top-level index: num,max,pointer */
889 + if ((err = ext3_ext_get_access(handle, inode, curp)))
892 + curp->p_hdr->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
893 + curp->p_hdr->eh_max = cpu_to_le16(ext3_ext_space_root_idx(inode));
894 + curp->p_hdr->eh_entries = cpu_to_le16(1);
895 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
896 + /* FIXME: it works, but actually path[0] can be index */
897 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
898 + curp->p_idx->ei_leaf = cpu_to_le32(newblock);
899 + curp->p_idx->ei_leaf_hi = curp->p_idx->ei_unused = 0;
901 + neh = ext_inode_hdr(inode);
902 + fidx = EXT_FIRST_INDEX(neh);
903 + ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %d\n",
904 + le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
905 + le32_to_cpu(fidx->ei_block), le32_to_cpu(fidx->ei_leaf));
907 + neh->eh_depth = cpu_to_le16(path->p_depth + 1);
908 + err = ext3_ext_dirty(handle, inode, curp);
916 + * routine finds empty index and adds new leaf. if no free index found
917 + * then it requests in-depth growing
919 +static int ext3_ext_create_new_leaf(handle_t *handle, struct inode *inode,
920 + struct ext3_ext_path *path,
921 + struct ext3_extent *newext)
923 + struct ext3_ext_path *curp;
924 + int depth, i, err = 0;
927 + i = depth = ext_depth(inode);
929 + /* walk up to the tree and look for free index entry */
930 + curp = path + depth;
931 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
936 + /* we use already allocated block for index block
937 + * so, subsequent data blocks should be contigoues */
938 + if (EXT_HAS_FREE_INDEX(curp)) {
939 + /* if we found index with free entry, then use that
940 + * entry: create all needed subtree and add new leaf */
941 + err = ext3_ext_split(handle, inode, path, newext, i);
946 + ext3_ext_drop_refs(path);
947 + path = ext3_ext_find_extent(inode,
948 + le32_to_cpu(newext->ee_block),
951 + err = PTR_ERR(path);
953 + /* tree is full, time to grow in depth */
954 + err = ext3_ext_grow_indepth(handle, inode, path, newext);
959 + ext3_ext_drop_refs(path);
960 + path = ext3_ext_find_extent(inode,
961 + le32_to_cpu(newext->ee_block),
963 + if (IS_ERR(path)) {
964 + err = PTR_ERR(path);
969 + * only first (depth 0 -> 1) produces free space
970 + * in all other cases we have to split growed tree
972 + depth = ext_depth(inode);
973 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
974 + /* now we need split */
984 + * search the closest allocated block to the left for *logical
985 + * and returns it at @logical + it's physical address at @phys
986 + * if *logical is the smallest allocated block, the function
987 + * returns 0 at @phys
988 + * return value contains 0 (success) or error code
991 +ext3_ext_search_left(struct inode *inode, struct ext3_ext_path *path,
992 + unsigned long *logical, unsigned long *phys)
994 + struct ext3_extent_idx *ix;
995 + struct ext3_extent *ex;
998 + BUG_ON(path == NULL);
999 + depth = path->p_depth;
1002 + if (depth == 0 && path->p_ext == NULL)
1005 + /* usually extent in the path covers blocks smaller
1006 + * then *logical, but it can be that extent is the
1007 + * first one in the file */
1009 + ex = path[depth].p_ext;
1010 + if (*logical < le32_to_cpu(ex->ee_block)) {
1011 + BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1012 + while (--depth >= 0) {
1013 + ix = path[depth].p_idx;
1014 + BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1019 + BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
1021 + *logical = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1;
1022 + *phys = le32_to_cpu(ex->ee_start) + le16_to_cpu(ex->ee_len) - 1;
1025 +EXPORT_SYMBOL(ext3_ext_search_left);
1028 + * search the closest allocated block to the right for *logical
1029 + * and returns it at @logical + it's physical address at @phys
1030 + * if *logical is the smallest allocated block, the function
1031 + * returns 0 at @phys
1032 + * return value contains 0 (success) or error code
1035 +ext3_ext_search_right(struct inode *inode, struct ext3_ext_path *path,
1036 + unsigned long *logical, unsigned long *phys)
1038 + struct buffer_head *bh = NULL;
1039 + struct ext3_extent_header *eh;
1040 + struct ext3_extent_idx *ix = NULL;
1041 + struct ext3_extent *ex;
1042 + unsigned long block;
1045 + BUG_ON(path == NULL);
1046 + depth = path->p_depth;
1049 + if (depth == 0 && path->p_ext == NULL)
1052 + /* usually extent in the path covers blocks smaller
1053 + * then *logical, but it can be that extent is the
1054 + * first one in the file */
1056 + ex = path[depth].p_ext;
1057 + if (*logical < le32_to_cpu(ex->ee_block)) {
1058 + BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1059 + while (--depth >= 0) {
1060 + ix = path[depth].p_idx;
1061 + BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1063 + *logical = le32_to_cpu(ex->ee_block);
1064 + *phys = le32_to_cpu(ex->ee_start);
1068 + BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
1070 + if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1071 + /* next allocated block in this leaf */
1073 + *logical = le32_to_cpu(ex->ee_block);
1074 + *phys = le32_to_cpu(ex->ee_start);
1078 + /* go up and search for index to the right */
1079 + while (--depth >= 0) {
1080 + ix = path[depth].p_idx;
1081 + if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1086 + /* we've gone up to the root and
1087 + * found no index to the right */
1091 + /* we've found index to the right, let's
1092 + * follow it and find the closest allocated
1093 + * block to the right */
1095 + block = le32_to_cpu(ix->ei_leaf);
1096 + while (++depth < path->p_depth) {
1097 + bh = sb_bread(inode->i_sb, block);
1100 + eh = ext_block_hdr(bh);
1101 + if (ext3_ext_check_header(inode, eh, path->p_depth - depth)) {
1105 + ix = EXT_FIRST_INDEX(eh);
1106 + block = le32_to_cpu(ix->ei_leaf);
1110 + bh = sb_bread(inode->i_sb, block);
1113 + eh = ext_block_hdr(bh);
1114 + if (ext3_ext_check_header(inode, eh, 0)) {
1118 + ex = EXT_FIRST_EXTENT(eh);
1119 + *logical = le32_to_cpu(ex->ee_block);
1120 + *phys = le32_to_cpu(ex->ee_start);
1125 +EXPORT_SYMBOL(ext3_ext_search_right);
1130 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
1131 + * NOTE: it consider block number from index entry as
1132 + * allocated block. thus, index entries have to be consistent
1135 +static unsigned long
1136 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
1140 + BUG_ON(path == NULL);
1141 + depth = path->p_depth;
1143 + if (depth == 0 && path->p_ext == NULL)
1144 + return EXT_MAX_BLOCK;
1146 + while (depth >= 0) {
1147 + if (depth == path->p_depth) {
1149 + if (path[depth].p_ext !=
1150 + EXT_LAST_EXTENT(path[depth].p_hdr))
1151 + return le32_to_cpu(path[depth].p_ext[1].ee_block);
1154 + if (path[depth].p_idx !=
1155 + EXT_LAST_INDEX(path[depth].p_hdr))
1156 + return le32_to_cpu(path[depth].p_idx[1].ei_block);
1161 + return EXT_MAX_BLOCK;
1165 + * returns first allocated block from next leaf or EXT_UNSET_BLOCK
1167 +static unsigned ext3_ext_next_leaf_block(struct inode *inode,
1168 + struct ext3_ext_path *path)
1172 + BUG_ON(path == NULL);
1173 + depth = path->p_depth;
1175 + /* zero-tree has no leaf blocks at all */
1177 + return EXT_UNSET_BLOCK;
1179 + /* go to index block */
1182 + while (depth >= 0) {
1183 + if (path[depth].p_idx !=
1184 + EXT_LAST_INDEX(path[depth].p_hdr))
1185 + return le32_to_cpu(path[depth].p_idx[1].ei_block);
1189 + return EXT_UNSET_BLOCK;
1193 + * if leaf gets modified and modified extent is first in the leaf
1194 + * then we have to correct all indexes above
1195 + * TODO: do we need to correct tree in all cases?
1197 +int ext3_ext_correct_indexes(handle_t *handle, struct inode *inode,
1198 + struct ext3_ext_path *path)
1200 + struct ext3_extent_header *eh;
1201 + int depth = ext_depth(inode);
1202 + struct ext3_extent *ex;
1206 + eh = path[depth].p_hdr;
1207 + ex = path[depth].p_ext;
1208 + BUG_ON(ex == NULL);
1209 + BUG_ON(eh == NULL);
1212 + /* there is no tree at all */
1216 + if (ex != EXT_FIRST_EXTENT(eh)) {
1217 + /* we correct tree if first leaf got modified only */
1222 + * TODO: we need correction if border is smaller then current one
1225 + border = path[depth].p_ext->ee_block;
1226 + if ((err = ext3_ext_get_access(handle, inode, path + k)))
1228 + path[k].p_idx->ei_block = border;
1229 + if ((err = ext3_ext_dirty(handle, inode, path + k)))
1233 + /* change all left-side indexes */
1234 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1236 + if ((err = ext3_ext_get_access(handle, inode, path + k)))
1238 + path[k].p_idx->ei_block = border;
1239 + if ((err = ext3_ext_dirty(handle, inode, path + k)))
1247 +ext3_can_extents_be_merged(struct inode *inode, struct ext3_extent *ex1,
1248 + struct ext3_extent *ex2)
1250 + /* FIXME: 48bit support */
1251 + if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len) !=
1252 + le32_to_cpu(ex2->ee_block))
1255 +#ifdef AGRESSIVE_TEST
1256 + if (le16_to_cpu(ex1->ee_len) >= 4)
1260 + if (le32_to_cpu(ex1->ee_start) + le16_to_cpu(ex1->ee_len) ==
1261 + le32_to_cpu(ex2->ee_start))
1267 + * this routine tries to merge requsted extent into the existing
1268 + * extent or inserts requested extent as new one into the tree,
1269 + * creating new leaf in no-space case
1271 +int ext3_ext_insert_extent(handle_t *handle, struct inode *inode,
1272 + struct ext3_ext_path *path,
1273 + struct ext3_extent *newext)
1275 + struct ext3_extent_header * eh;
1276 + struct ext3_extent *ex, *fex;
1277 + struct ext3_extent *nearex; /* nearest extent */
1278 + struct ext3_ext_path *npath = NULL;
1279 + int depth, len, err, next;
1281 + BUG_ON(newext->ee_len == 0);
1282 + depth = ext_depth(inode);
1283 + ex = path[depth].p_ext;
1284 + BUG_ON(path[depth].p_hdr == NULL);
1286 + /* try to insert block into found extent and return */
1287 + if (ex && ext3_can_extents_be_merged(inode, ex, newext)) {
1288 + ext_debug(inode, "append %d block to %d:%d (from %d)\n",
1289 + le16_to_cpu(newext->ee_len),
1290 + le32_to_cpu(ex->ee_block),
1291 + le16_to_cpu(ex->ee_len),
1292 + le32_to_cpu(ex->ee_start));
1293 + if ((err = ext3_ext_get_access(handle, inode, path + depth)))
1295 + ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
1296 + + le16_to_cpu(newext->ee_len));
1297 + eh = path[depth].p_hdr;
1303 + depth = ext_depth(inode);
1304 + eh = path[depth].p_hdr;
1305 + if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1308 + /* probably next leaf has space for us? */
1309 + fex = EXT_LAST_EXTENT(eh);
1310 + next = ext3_ext_next_leaf_block(inode, path);
1311 + if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1312 + && next != EXT_UNSET_BLOCK) {
1313 + ext_debug(inode, "next leaf block - %d\n", next);
1314 + BUG_ON(npath != NULL);
1315 + npath = ext3_ext_find_extent(inode, next, NULL);
1316 + if (IS_ERR(npath))
1317 + return PTR_ERR(npath);
1318 + BUG_ON(npath->p_depth != path->p_depth);
1319 + eh = npath[depth].p_hdr;
1320 + if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1321 + ext_debug(inode, "next leaf isnt full(%d)\n",
1322 + le16_to_cpu(eh->eh_entries));
1326 + ext_debug(inode, "next leaf has no free space(%d,%d)\n",
1327 + le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1331 + * there is no free space in found leaf
1332 + * we're gonna add new leaf in the tree
1334 + err = ext3_ext_create_new_leaf(handle, inode, path, newext);
1337 + depth = ext_depth(inode);
1338 + eh = path[depth].p_hdr;
1341 + nearex = path[depth].p_ext;
1343 + if ((err = ext3_ext_get_access(handle, inode, path + depth)))
1347 + /* there is no extent in this leaf, create first one */
1348 + ext_debug(inode, "first extent in the leaf: %d:%d:%d\n",
1349 + le32_to_cpu(newext->ee_block),
1350 + le32_to_cpu(newext->ee_start),
1351 + le16_to_cpu(newext->ee_len));
1352 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1353 + } else if (le32_to_cpu(newext->ee_block)
1354 + > le32_to_cpu(nearex->ee_block)) {
1355 + /* BUG_ON(newext->ee_block == nearex->ee_block); */
1356 + if (nearex != EXT_LAST_EXTENT(eh)) {
1357 + len = EXT_MAX_EXTENT(eh) - nearex;
1358 + len = (len - 1) * sizeof(struct ext3_extent);
1359 + len = len < 0 ? 0 : len;
1360 + ext_debug(inode, "insert %d:%d:%d after: nearest 0x%p, "
1361 + "move %d from 0x%p to 0x%p\n",
1362 + le32_to_cpu(newext->ee_block),
1363 + le32_to_cpu(newext->ee_start),
1364 + le16_to_cpu(newext->ee_len),
1365 + nearex, len, nearex + 1, nearex + 2);
1366 + memmove(nearex + 2, nearex + 1, len);
1368 + path[depth].p_ext = nearex + 1;
1370 + BUG_ON(newext->ee_block == nearex->ee_block);
1371 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1372 + len = len < 0 ? 0 : len;
1373 + ext_debug(inode, "insert %d:%d:%d before: nearest 0x%p, "
1374 + "move %d from 0x%p to 0x%p\n",
1375 + le32_to_cpu(newext->ee_block),
1376 + le32_to_cpu(newext->ee_start),
1377 + le16_to_cpu(newext->ee_len),
1378 + nearex, len, nearex + 1, nearex + 2);
1379 + memmove(nearex + 1, nearex, len);
1380 + path[depth].p_ext = nearex;
1383 + eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1384 + nearex = path[depth].p_ext;
1385 + nearex->ee_block = newext->ee_block;
1386 + nearex->ee_start = newext->ee_start;
1387 + nearex->ee_len = newext->ee_len;
1388 + /* FIXME: support for large fs */
1389 + nearex->ee_start_hi = 0;
1392 + /* try to merge extents to the right */
1393 + while (nearex < EXT_LAST_EXTENT(eh)) {
1394 + if (!ext3_can_extents_be_merged(inode, nearex, nearex + 1))
1396 + /* merge with next extent! */
1397 + nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
1398 + + le16_to_cpu(nearex[1].ee_len));
1399 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1400 + len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1401 + * sizeof(struct ext3_extent);
1402 + memmove(nearex + 1, nearex + 2, len);
1404 + eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1405 + BUG_ON(eh->eh_entries == 0);
1408 + /* try to merge extents to the left */
1410 + /* time to correct all indexes above */
1411 + err = ext3_ext_correct_indexes(handle, inode, path);
1415 + err = ext3_ext_dirty(handle, inode, path + depth);
1419 + ext3_ext_drop_refs(npath);
1422 + ext3_ext_tree_changed(inode);
1423 + ext3_ext_invalidate_cache(inode);
1427 +int ext3_ext_walk_space(struct inode *inode, unsigned long block,
1428 + unsigned long num, ext_prepare_callback func,
1431 + struct ext3_ext_path *path = NULL;
1432 + struct ext3_ext_cache cbex;
1433 + struct ext3_extent *ex;
1434 + unsigned long next, start = 0, end = 0;
1435 + unsigned long last = block + num;
1436 + int depth, exists, err = 0;
1438 + BUG_ON(func == NULL);
1439 + BUG_ON(inode == NULL);
1441 + while (block < last && block != EXT_MAX_BLOCK) {
1442 + num = last - block;
1443 + /* find extent for this block */
1444 + path = ext3_ext_find_extent(inode, block, path);
1445 + if (IS_ERR(path)) {
1446 + err = PTR_ERR(path);
1451 + depth = ext_depth(inode);
1452 + BUG_ON(path[depth].p_hdr == NULL);
1453 + ex = path[depth].p_ext;
1454 + next = ext3_ext_next_allocated_block(path);
1458 + /* there is no extent yet, so try to allocate
1459 + * all requested space */
1461 + end = block + num;
1462 + } else if (le32_to_cpu(ex->ee_block) > block) {
1463 + /* need to allocate space before found extent */
1465 + end = le32_to_cpu(ex->ee_block);
1466 + if (block + num < end)
1467 + end = block + num;
1468 + } else if (block >=
1469 + le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
1470 + /* need to allocate space after found extent */
1472 + end = block + num;
1475 + } else if (block >= le32_to_cpu(ex->ee_block)) {
1477 + * some part of requested space is covered
1481 + end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
1482 + if (block + num < end)
1483 + end = block + num;
1488 + BUG_ON(end <= start);
1491 + cbex.ec_block = start;
1492 + cbex.ec_len = end - start;
1493 + cbex.ec_start = 0;
1494 + cbex.ec_type = EXT3_EXT_CACHE_GAP;
1496 + cbex.ec_block = le32_to_cpu(ex->ee_block);
1497 + cbex.ec_len = le16_to_cpu(ex->ee_len);
1498 + cbex.ec_start = le32_to_cpu(ex->ee_start);
1499 + cbex.ec_type = EXT3_EXT_CACHE_EXTENT;
1502 + BUG_ON(cbex.ec_len == 0);
1503 + err = func(inode, path, &cbex, cbdata);
1504 + ext3_ext_drop_refs(path);
1508 + if (err == EXT_REPEAT)
1510 + else if (err == EXT_BREAK) {
1515 + if (ext_depth(inode) != depth) {
1516 + /* depth was changed. we have to realloc path */
1521 + block = cbex.ec_block + cbex.ec_len;
1525 + ext3_ext_drop_refs(path);
1533 +ext3_ext_put_in_cache(struct inode *inode, __u32 block,
1534 + __u32 len, __u32 start, int type)
1536 + struct ext3_ext_cache *cex;
1538 + cex = &EXT3_I(inode)->i_cached_extent;
1539 + cex->ec_type = type;
1540 + cex->ec_block = block;
1541 + cex->ec_len = len;
1542 + cex->ec_start = start;
1546 + * this routine calculate boundaries of the gap requested block fits into
1547 + * and cache this gap
1550 +ext3_ext_put_gap_in_cache(struct inode *inode, struct ext3_ext_path *path,
1551 + unsigned long block)
1553 + int depth = ext_depth(inode);
1554 + unsigned long lblock, len;
1555 + struct ext3_extent *ex;
1557 + ex = path[depth].p_ext;
1559 + /* there is no extent yet, so gap is [0;-] */
1561 + len = EXT_MAX_BLOCK;
1562 + ext_debug(inode, "cache gap(whole file):");
1563 + } else if (block < le32_to_cpu(ex->ee_block)) {
1565 + len = le32_to_cpu(ex->ee_block) - block;
1566 + ext_debug(inode, "cache gap(before): %lu [%lu:%lu]",
1567 + (unsigned long) block,
1568 + (unsigned long) le32_to_cpu(ex->ee_block),
1569 + (unsigned long) le16_to_cpu(ex->ee_len));
1570 + } else if (block >= le32_to_cpu(ex->ee_block)
1571 + + le16_to_cpu(ex->ee_len)) {
1572 + lblock = le32_to_cpu(ex->ee_block)
1573 + + le16_to_cpu(ex->ee_len);
1574 + len = ext3_ext_next_allocated_block(path);
1575 + ext_debug(inode, "cache gap(after): [%lu:%lu] %lu",
1576 + (unsigned long) le32_to_cpu(ex->ee_block),
1577 + (unsigned long) le16_to_cpu(ex->ee_len),
1578 + (unsigned long) block);
1579 + BUG_ON(len == lblock);
1580 + len = len - lblock;
1586 + ext_debug(inode, " -> %lu:%lu\n", (unsigned long) lblock, len);
1587 + ext3_ext_put_in_cache(inode, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1591 +ext3_ext_in_cache(struct inode *inode, unsigned long block,
1592 + struct ext3_extent *ex)
1594 + struct ext3_ext_cache *cex;
1596 + cex = &EXT3_I(inode)->i_cached_extent;
1598 + /* has cache valid data? */
1599 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1600 + return EXT3_EXT_CACHE_NO;
1602 + BUG_ON(cex->ec_type != EXT3_EXT_CACHE_GAP &&
1603 + cex->ec_type != EXT3_EXT_CACHE_EXTENT);
1604 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1605 + ex->ee_block = cpu_to_le32(cex->ec_block);
1606 + ex->ee_start = cpu_to_le32(cex->ec_start);
1607 + ex->ee_start_hi = 0;
1608 + ex->ee_len = cpu_to_le16(cex->ec_len);
1609 + ext_debug(inode, "%lu cached by %lu:%lu:%lu\n",
1610 + (unsigned long) block,
1611 + (unsigned long) cex->ec_block,
1612 + (unsigned long) cex->ec_len,
1613 + (unsigned long) cex->ec_start);
1614 + return cex->ec_type;
1617 + /* not in cache */
1618 + return EXT3_EXT_CACHE_NO;
1622 + * routine removes index from the index block
1623 + * it's used in truncate case only. thus all requests are for
1624 + * last index in the block only
1626 +int ext3_ext_rm_idx(handle_t *handle, struct inode *inode,
1627 + struct ext3_ext_path *path)
1629 + struct buffer_head *bh;
1631 + unsigned long leaf;
1633 + /* free index block */
1635 + leaf = le32_to_cpu(path->p_idx->ei_leaf);
1636 + BUG_ON(path->p_hdr->eh_entries == 0);
1637 + if ((err = ext3_ext_get_access(handle, inode, path)))
1639 + path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
1640 + if ((err = ext3_ext_dirty(handle, inode, path)))
1642 + ext_debug(inode, "index is empty, remove it, free block %lu\n", leaf);
1643 + bh = sb_find_get_block(inode->i_sb, leaf);
1644 + ext3_forget(handle, 1, inode, bh, leaf);
1645 + ext3_free_blocks(handle, inode, leaf, 1);
1650 + * This routine returns max. credits extent tree can consume.
1651 + * It should be OK for low-performance paths like ->writepage()
1652 + * To allow many writing process to fit a single transaction,
1653 + * caller should calculate credits under truncate_mutex and
1654 + * pass actual path.
1656 +int inline ext3_ext_calc_credits_for_insert(struct inode *inode,
1657 + struct ext3_ext_path *path)
1659 + int depth, needed;
1662 + /* probably there is space in leaf? */
1663 + depth = ext_depth(inode);
1664 + if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1665 + < le16_to_cpu(path[depth].p_hdr->eh_max))
1670 + * given 32bit logical block (4294967296 blocks), max. tree
1671 + * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1672 + * let's also add one more level for imbalance.
1676 + /* allocation of new data block(s) */
1680 + * tree can be full, so it'd need to grow in depth:
1681 + * we need one credit to modify old root, credits for
1682 + * new root will be added in split accounting
1687 + * Index split can happen, we'd need:
1688 + * allocate intermediate indexes (bitmap + group)
1689 + * + change two blocks at each level, but root (already included)
1691 + needed += (depth * 2) + (depth * 2);
1693 + /* any allocation modifies superblock */
1699 +static int ext3_remove_blocks(handle_t *handle, struct inode *inode,
1700 + struct ext3_extent *ex,
1701 + unsigned long from, unsigned long to)
1703 + struct buffer_head *bh;
1706 +#ifdef EXTENTS_STATS
1708 + struct ext3_sb_info *sbi = EXT3_SB(inode->i_sb);
1709 + unsigned short ee_len = le16_to_cpu(ex->ee_len);
1710 + spin_lock(&sbi->s_ext_stats_lock);
1711 + sbi->s_ext_blocks += ee_len;
1712 + sbi->s_ext_extents++;
1713 + if (ee_len < sbi->s_ext_min)
1714 + sbi->s_ext_min = ee_len;
1715 + if (ee_len > sbi->s_ext_max)
1716 + sbi->s_ext_max = ee_len;
1717 + if (ext_depth(inode) > sbi->s_depth_max)
1718 + sbi->s_depth_max = ext_depth(inode);
1719 + spin_unlock(&sbi->s_ext_stats_lock);
1722 + if (from >= le32_to_cpu(ex->ee_block)
1723 + && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1724 + /* tail removal */
1725 + unsigned long num, start;
1726 + num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
1727 + start = le32_to_cpu(ex->ee_start) + le16_to_cpu(ex->ee_len) - num;
1728 + ext_debug(inode, "free last %lu blocks starting %lu\n", num, start);
1729 + for (i = 0; i < num; i++) {
1730 + bh = sb_find_get_block(inode->i_sb, start + i);
1731 + ext3_forget(handle, 0, inode, bh, start + i);
1733 + ext3_free_blocks(handle, inode, start, num);
1734 + } else if (from == le32_to_cpu(ex->ee_block)
1735 + && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1736 + printk("strange request: removal %lu-%lu from %u:%u\n",
1737 + from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1739 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1740 + from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1746 +ext3_ext_rm_leaf(handle_t *handle, struct inode *inode,
1747 + struct ext3_ext_path *path, unsigned long start)
1749 + int err = 0, correct_index = 0;
1750 + int depth = ext_depth(inode), credits;
1751 + struct ext3_extent_header *eh;
1752 + unsigned a, b, block, num;
1753 + unsigned long ex_ee_block;
1754 + unsigned short ex_ee_len;
1755 + struct ext3_extent *ex;
1757 + /* the header must be checked already in ext3_ext_remove_space() */
1758 + ext_debug(inode, "truncate since %lu in leaf\n", start);
1759 + if (!path[depth].p_hdr)
1760 + path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1761 + eh = path[depth].p_hdr;
1762 + BUG_ON(eh == NULL);
1764 + /* find where to start removing */
1765 + ex = EXT_LAST_EXTENT(eh);
1767 + ex_ee_block = le32_to_cpu(ex->ee_block);
1768 + ex_ee_len = le16_to_cpu(ex->ee_len);
1770 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1771 + ex_ee_block + ex_ee_len > start) {
1772 + ext_debug(inode, "remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1773 + path[depth].p_ext = ex;
1775 + a = ex_ee_block > start ? ex_ee_block : start;
1776 + b = (unsigned long long)ex_ee_block + ex_ee_len - 1 <
1777 + EXT_MAX_BLOCK ? ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1779 + ext_debug(inode, " border %u:%u\n", a, b);
1781 + if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1785 + } else if (a != ex_ee_block) {
1786 + /* remove tail of the extent */
1787 + block = ex_ee_block;
1789 + } else if (b != ex_ee_block + ex_ee_len - 1) {
1790 + /* remove head of the extent */
1793 + /* there is no "make a hole" API yet */
1796 + /* remove whole extent: excellent! */
1797 + block = ex_ee_block;
1799 + BUG_ON(a != ex_ee_block);
1800 + BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1803 + /* at present, extent can't cross block group */
1804 + /* leaf + bitmap + group desc + sb + inode */
1806 + if (ex == EXT_FIRST_EXTENT(eh)) {
1807 + correct_index = 1;
1808 + credits += (ext_depth(inode)) + 1;
1810 +#ifdef CONFIG_QUOTA
1811 + credits += 2 * EXT3_QUOTA_TRANS_BLOCKS(inode->i_sb);
1814 + handle = ext3_ext_journal_restart(handle, credits);
1815 + if (IS_ERR(handle)) {
1816 + err = PTR_ERR(handle);
1820 + err = ext3_ext_get_access(handle, inode, path + depth);
1824 + err = ext3_remove_blocks(handle, inode, ex, a, b);
1829 + /* this extent is removed entirely mark slot unused */
1830 + ex->ee_start = ex->ee_start_hi = 0;
1831 + eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1834 + ex->ee_block = cpu_to_le32(block);
1835 + ex->ee_len = cpu_to_le16(num);
1837 + err = ext3_ext_dirty(handle, inode, path + depth);
1841 + ext_debug(inode, "new extent: %u:%u:%u\n", block, num,
1842 + le32_to_cpu(ex->ee_start));
1844 + ex_ee_block = le32_to_cpu(ex->ee_block);
1845 + ex_ee_len = le16_to_cpu(ex->ee_len);
1848 + if (correct_index && eh->eh_entries)
1849 + err = ext3_ext_correct_indexes(handle, inode, path);
1851 + /* if this leaf is free, then we should
1852 + * remove it from index block above */
1853 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1854 + err = ext3_ext_rm_idx(handle, inode, path + depth);
1861 + * returns 1 if current index have to be freed (even partial)
1864 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1866 + BUG_ON(path->p_idx == NULL);
1868 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1872 + * if truncate on deeper level happened it it wasn't partial
1873 + * so we have to consider current index for truncation
1875 + if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1880 +int ext3_ext_remove_space(struct inode *inode, unsigned long start)
1882 + struct super_block *sb = inode->i_sb;
1883 + int depth = ext_depth(inode);
1884 + struct ext3_ext_path *path;
1886 + int i = 0, err = 0;
1888 + ext_debug(inode, "truncate since %lu\n", start);
1890 + /* probably first extent we're gonna free will be last in block */
1891 + handle = ext3_journal_start(inode, depth + 1);
1892 + if (IS_ERR(handle))
1893 + return PTR_ERR(handle);
1895 + ext3_ext_invalidate_cache(inode);
1898 + * we start scanning from right side freeing all the blocks
1899 + * after i_size and walking into the deep
1901 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1902 + if (path == NULL) {
1903 + ext3_journal_stop(handle);
1906 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1907 + path[0].p_hdr = ext_inode_hdr(inode);
1908 + if (ext3_ext_check_header(inode, path[0].p_hdr, depth)) {
1912 + path[0].p_depth = depth;
1914 + while (i >= 0 && err == 0) {
1916 + /* this is leaf block */
1917 + err = ext3_ext_rm_leaf(handle, inode, path, start);
1918 + /* root level have p_bh == NULL, brelse() eats this */
1919 + brelse(path[i].p_bh);
1920 + path[i].p_bh = NULL;
1925 + /* this is index block */
1926 + if (!path[i].p_hdr) {
1927 + ext_debug(inode, "initialize header\n");
1928 + path[i].p_hdr = ext_block_hdr(path[i].p_bh);
1931 + if (!path[i].p_idx) {
1932 + /* this level hasn't touched yet */
1933 + path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1934 + path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
1935 + ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n",
1937 + le16_to_cpu(path[i].p_hdr->eh_entries));
1939 + /* we've already was here, see at next index */
1943 + ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n",
1944 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1946 + if (ext3_ext_more_to_rm(path + i)) {
1947 + struct buffer_head *bh;
1948 + /* go to the next level */
1949 + ext_debug(inode, "move to level %d (block %d)\n",
1950 + i + 1, le32_to_cpu(path[i].p_idx->ei_leaf));
1951 + memset(path + i + 1, 0, sizeof(*path));
1952 + bh = sb_bread(sb, le32_to_cpu(path[i].p_idx->ei_leaf));
1954 + /* should we reset i_size? */
1958 + BUG_ON(i + 1 > depth);
1959 + if (ext3_ext_check_header(inode, ext_block_hdr(bh),
1964 + path[i+1].p_bh = bh;
1966 + /* put actual number of indexes to know is this
1967 + * number got changed at the next iteration */
1968 + path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
1971 + /* we finish processing this index, go up */
1972 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1973 + /* index is empty, remove it
1974 + * handle must be already prepared by the
1975 + * truncatei_leaf() */
1976 + err = ext3_ext_rm_idx(handle, inode, path + i);
1978 + /* root level have p_bh == NULL, brelse() eats this */
1979 + brelse(path[i].p_bh);
1980 + path[i].p_bh = NULL;
1982 + ext_debug(inode, "return to level %d\n", i);
1986 + /* TODO: flexible tree reduction should be here */
1987 + if (path->p_hdr->eh_entries == 0) {
1989 + * truncate to zero freed all the tree
1990 + * so, we need to correct eh_depth
1992 + err = ext3_ext_get_access(handle, inode, path);
1994 + ext_inode_hdr(inode)->eh_depth = 0;
1995 + ext_inode_hdr(inode)->eh_max =
1996 + cpu_to_le16(ext3_ext_space_root(inode));
1997 + err = ext3_ext_dirty(handle, inode, path);
2001 + ext3_ext_tree_changed(inode);
2002 + ext3_ext_drop_refs(path);
2004 + ext3_journal_stop(handle);
2010 + * called at mount time
2012 +void ext3_ext_init(struct super_block *sb)
2015 + * possible initialization would be here
2018 + if (test_opt(sb, EXTENTS)) {
2019 + printk("EXT3-fs: file extents enabled");
2020 +#ifdef AGRESSIVE_TEST
2021 + printk(", agressive tests");
2023 +#ifdef CHECK_BINSEARCH
2024 + printk(", check binsearch");
2026 +#ifdef EXTENTS_STATS
2027 + printk(", stats");
2030 +#ifdef EXTENTS_STATS
2031 + spin_lock_init(&EXT3_SB(sb)->s_ext_stats_lock);
2032 + EXT3_SB(sb)->s_ext_min = 1 << 30;
2033 + EXT3_SB(sb)->s_ext_max = 0;
2039 + * called at umount time
2041 +void ext3_ext_release(struct super_block *sb)
2043 + if (!test_opt(sb, EXTENTS))
2046 +#ifdef EXTENTS_STATS
2047 + if (EXT3_SB(sb)->s_ext_blocks && EXT3_SB(sb)->s_ext_extents) {
2048 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2049 + printk(KERN_ERR "EXT3-fs: %lu blocks in %lu extents (%lu ave)\n",
2050 + sbi->s_ext_blocks, sbi->s_ext_extents,
2051 + sbi->s_ext_blocks / sbi->s_ext_extents);
2052 + printk(KERN_ERR "EXT3-fs: extents: %lu min, %lu max, max depth %lu\n",
2053 + sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2058 +int ext3_ext_get_blocks(handle_t *handle, struct inode *inode, sector_t iblock,
2059 + unsigned long max_blocks, struct buffer_head *bh_result,
2060 + int create, int extend_disksize)
2062 + struct ext3_ext_path *path = NULL;
2063 + struct ext3_extent newex, *ex;
2064 + int goal, newblock, err = 0, depth;
2065 + unsigned long allocated = 0;
2066 + unsigned long next;
2068 + __clear_bit(BH_New, &bh_result->b_state);
2069 + ext_debug(inode, "blocks %d/%lu requested for inode %u\n", (int) iblock,
2070 + max_blocks, (unsigned) inode->i_ino);
2071 + mutex_lock(&EXT3_I(inode)->truncate_mutex);
2073 + /* check in cache */
2074 + if ((goal = ext3_ext_in_cache(inode, iblock, &newex))) {
2075 + if (goal == EXT3_EXT_CACHE_GAP) {
2077 + /* block isn't allocated yet and
2078 + * user don't want to allocate it */
2081 + /* we should allocate requested block */
2082 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2083 + /* block is already allocated */
2085 + - le32_to_cpu(newex.ee_block)
2086 + + le32_to_cpu(newex.ee_start);
2087 + /* number of remain blocks in the extent */
2088 + BUG_ON(iblock < le32_to_cpu(newex.ee_block));
2089 + allocated = le16_to_cpu(newex.ee_len) -
2090 + (iblock - le32_to_cpu(newex.ee_block));
2097 + /* find extent for this block */
2098 + path = ext3_ext_find_extent(inode, iblock, NULL);
2099 + if (IS_ERR(path)) {
2100 + err = PTR_ERR(path);
2105 + depth = ext_depth(inode);
2108 + * consistent leaf must not be empty
2109 + * this situations is possible, though, _during_ tree modification
2110 + * this is why assert can't be put in ext3_ext_find_extent()
2112 + BUG_ON(path[depth].p_ext == NULL && depth != 0);
2114 + if ((ex = path[depth].p_ext)) {
2115 + unsigned long ee_block = le32_to_cpu(ex->ee_block);
2116 + unsigned long ee_start = le32_to_cpu(ex->ee_start);
2117 + unsigned short ee_len = le16_to_cpu(ex->ee_len);
2118 + /* if found exent covers block, simple return it */
2119 + if (iblock >= ee_block && iblock < ee_block + ee_len) {
2120 + newblock = iblock - ee_block + ee_start;
2121 + /* number of remain blocks in the extent */
2122 + allocated = ee_len - (iblock - ee_block);
2123 + ext_debug(inode, "%d fit into %lu:%d -> %d\n", (int) iblock,
2124 + ee_block, ee_len, newblock);
2125 + ext3_ext_put_in_cache(inode, ee_block, ee_len,
2126 + ee_start, EXT3_EXT_CACHE_EXTENT);
2132 + * requested block isn't allocated yet
2133 + * we couldn't try to create block if create flag is zero
2136 + /* put just found gap into cache to speedup subsequest reqs */
2137 + ext3_ext_put_gap_in_cache(inode, path, iblock);
2142 + * Okay, we need to do block allocation. Lazily initialize the block
2143 + * allocation info here if necessary
2145 + if (S_ISREG(inode->i_mode) && (!EXT3_I(inode)->i_block_alloc_info))
2146 + ext3_init_block_alloc_info(inode);
2148 + /* find next allocated block so that we know how many
2149 + * blocks we can allocate without ovelapping next extent */
2150 + BUG_ON(iblock < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
2151 + next = ext3_ext_next_allocated_block(path);
2152 + BUG_ON(next <= iblock);
2153 + allocated = next - iblock;
2154 + if (allocated > max_blocks)
2155 + allocated = max_blocks;
2157 + /* allocate new block */
2158 + goal = ext3_ext_find_goal(inode, path, iblock);
2159 + newblock = ext3_new_blocks(handle, inode, goal, &allocated, &err);
2162 + ext_debug(inode, "allocate new block: goal %d, found %d/%lu\n",
2163 + goal, newblock, allocated);
2165 + /* try to insert new extent into found leaf and return */
2166 + newex.ee_block = cpu_to_le32(iblock);
2167 + newex.ee_start = cpu_to_le32(newblock);
2168 + newex.ee_start_hi = 0;
2169 + newex.ee_len = cpu_to_le16(allocated);
2170 + err = ext3_ext_insert_extent(handle, inode, path, &newex);
2172 + /* free data blocks we just allocated */
2173 + ext3_free_blocks(handle, inode, le32_to_cpu(newex.ee_start),
2174 + le16_to_cpu(newex.ee_len));
2178 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2179 + EXT3_I(inode)->i_disksize = inode->i_size;
2181 + /* previous routine could use block we allocated */
2182 + newblock = le32_to_cpu(newex.ee_start);
2183 + __set_bit(BH_New, &bh_result->b_state);
2185 + ext3_ext_put_in_cache(inode, iblock, allocated, newblock,
2186 + EXT3_EXT_CACHE_EXTENT);
2188 + if (allocated > max_blocks)
2189 + allocated = max_blocks;
2190 + ext3_ext_show_leaf(inode, path);
2191 + __set_bit(BH_Mapped, &bh_result->b_state);
2192 + bh_result->b_bdev = inode->i_sb->s_bdev;
2193 + bh_result->b_blocknr = newblock;
2194 + bh_result->b_size = (allocated << inode->i_blkbits);
2197 + ext3_ext_drop_refs(path);
2200 + mutex_unlock(&EXT3_I(inode)->truncate_mutex);
2202 + return err ? err : allocated;
2205 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2207 + struct address_space *mapping = inode->i_mapping;
2208 + struct super_block *sb = inode->i_sb;
2209 + unsigned long last_block;
2214 + * probably first extent we're gonna free will be last in block
2216 + err = ext3_writepage_trans_blocks(inode) + 3;
2217 + handle = ext3_journal_start(inode, err);
2218 + if (IS_ERR(handle)) {
2220 + clear_highpage(page);
2221 + flush_dcache_page(page);
2222 + unlock_page(page);
2223 + page_cache_release(page);
2229 + ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2231 + mutex_lock(&EXT3_I(inode)->truncate_mutex);
2232 + ext3_ext_invalidate_cache(inode);
2235 + * TODO: optimization is possible here
2236 + * probably we need not scaning at all,
2237 + * because page truncation is enough
2239 + if (ext3_orphan_add(handle, inode))
2242 + /* we have to know where to truncate from in crash case */
2243 + EXT3_I(inode)->i_disksize = inode->i_size;
2244 + ext3_mark_inode_dirty(handle, inode);
2246 + last_block = (inode->i_size + sb->s_blocksize - 1)
2247 + >> EXT3_BLOCK_SIZE_BITS(sb);
2248 + err = ext3_ext_remove_space(inode, last_block);
2250 + /* In a multi-transaction truncate, we only make the final
2251 + * transaction synchronous */
2252 + if (IS_SYNC(inode))
2253 + handle->h_sync = 1;
2257 + * If this was a simple ftruncate(), and the file will remain alive
2258 + * then we need to clear up the orphan record which we created above.
2259 + * However, if this was a real unlink then we were called by
2260 + * ext3_delete_inode(), and we allow that function to clean up the
2261 + * orphan info for us.
2263 + if (inode->i_nlink)
2264 + ext3_orphan_del(handle, inode);
2266 + mutex_unlock(&EXT3_I(inode)->truncate_mutex);
2267 + ext3_journal_stop(handle);
2271 + * this routine calculate max number of blocks we could modify
2272 + * in order to allocate new block for an inode
2274 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2278 + needed = ext3_ext_calc_credits_for_insert(inode, NULL);
2280 + /* caller want to allocate num blocks, but note it includes sb */
2281 + needed = needed * num - (num - 1);
2283 +#ifdef CONFIG_QUOTA
2284 + needed += 2 * EXT3_QUOTA_TRANS_BLOCKS(inode->i_sb);
2290 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2291 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2292 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2293 +EXPORT_SYMBOL(ext3_ext_walk_space);
2294 +EXPORT_SYMBOL(ext3_ext_find_goal);
2295 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2296 Index: linux-2.6.18.8/fs/ext3/ialloc.c
2297 ===================================================================
2298 --- linux-2.6.18.8.orig/fs/ext3/ialloc.c 2007-07-17 09:18:09.000000000 +0200
2299 +++ linux-2.6.18.8/fs/ext3/ialloc.c 2007-07-17 11:08:09.000000000 +0200
2300 @@ -652,6 +652,17 @@ got:
2301 ext3_std_error(sb, err);
2302 goto fail_free_drop;
2304 + if (test_opt(sb, EXTENTS) && S_ISREG(inode->i_mode)) {
2305 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2306 + ext3_ext_tree_init(handle, inode);
2307 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2308 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2309 + if (err) goto fail;
2310 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2311 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2312 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2316 ext3_debug("allocating inode %lu\n", inode->i_ino);
2318 Index: linux-2.6.18.8/fs/ext3/inode.c
2319 ===================================================================
2320 --- linux-2.6.18.8.orig/fs/ext3/inode.c 2007-07-17 09:18:12.000000000 +0200
2321 +++ linux-2.6.18.8/fs/ext3/inode.c 2007-07-17 11:08:11.000000000 +0200
2326 -static int ext3_writepage_trans_blocks(struct inode *inode);
2329 * Test whether an inode is a fast symlink.
2331 @@ -804,6 +802,7 @@ int ext3_get_blocks_handle(handle_t *han
2332 ext3_fsblk_t first_block = 0;
2335 + J_ASSERT(!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL));
2336 J_ASSERT(handle != NULL || create == 0);
2337 depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
2339 @@ -984,12 +983,10 @@ static int ext3_get_block(struct inode *
2343 - ret = ext3_get_blocks_handle(handle, inode, iblock,
2344 + ret = ext3_get_blocks_wrap(handle, inode, iblock,
2345 max_blocks, bh_result, create, 0);
2347 - bh_result->b_size = (ret << inode->i_blkbits);
2352 ext3_journal_stop(handle);
2354 @@ -1008,7 +1005,7 @@ struct buffer_head *ext3_getblk(handle_t
2356 dummy.b_blocknr = -1000;
2357 buffer_trace_init(&dummy.b_history);
2358 - err = ext3_get_blocks_handle(handle, inode, block, 1,
2359 + err = ext3_get_blocks_wrap(handle, inode, block, 1,
2362 * ext3_get_blocks_handle() returns number of blocks
2363 @@ -1759,7 +1756,7 @@ void ext3_set_aops(struct inode *inode)
2364 * This required during truncate. We need to physically zero the tail end
2365 * of that block so it doesn't yield old data if the file is later grown.
2367 -static int ext3_block_truncate_page(handle_t *handle, struct page *page,
2368 +int ext3_block_truncate_page(handle_t *handle, struct page *page,
2369 struct address_space *mapping, loff_t from)
2371 ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
2372 @@ -2263,6 +2260,9 @@ void ext3_truncate(struct inode *inode)
2376 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2377 + return ext3_ext_truncate(inode, page);
2379 handle = start_transaction(inode);
2380 if (IS_ERR(handle)) {
2382 @@ -3008,12 +3008,15 @@ err_out:
2383 * block and work out the exact number of indirects which are touched. Pah.
2386 -static int ext3_writepage_trans_blocks(struct inode *inode)
2387 +int ext3_writepage_trans_blocks(struct inode *inode)
2389 int bpp = ext3_journal_blocks_per_page(inode);
2390 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2393 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2394 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2396 if (ext3_should_journal_data(inode))
2397 ret = 3 * (bpp + indirects) + 2;
2399 @@ -3260,7 +3263,7 @@ int ext3_map_inode_page(struct inode *in
2403 - rc = ext3_get_blocks_handle(handle, inode, iblock, 1, &dummy, 1, 1);
2404 + rc = ext3_get_blocks_wrap(handle, inode, iblock, 1, &dummy, 1, 1);
2406 printk(KERN_INFO "ext3_map_inode_page: error reading "
2407 "block %ld\n", iblock);
2408 Index: linux-2.6.18.8/fs/ext3/Makefile
2409 ===================================================================
2410 --- linux-2.6.18.8.orig/fs/ext3/Makefile 2007-07-17 09:18:11.000000000 +0200
2411 +++ linux-2.6.18.8/fs/ext3/Makefile 2007-07-17 11:08:11.000000000 +0200
2413 obj-$(CONFIG_EXT3_FS) += ext3.o
2415 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2416 - ioctl.o namei.o super.o symlink.o hash.o resize.o
2417 + ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o
2419 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2420 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2421 Index: linux-2.6.18.8/fs/ext3/super.c
2422 ===================================================================
2423 --- linux-2.6.18.8.orig/fs/ext3/super.c 2007-07-17 09:18:12.000000000 +0200
2424 +++ linux-2.6.18.8/fs/ext3/super.c 2007-07-17 11:08:12.000000000 +0200
2425 @@ -391,6 +391,7 @@ static void ext3_put_super (struct super
2426 struct ext3_super_block *es = sbi->s_es;
2429 + ext3_ext_release(sb);
2430 ext3_xattr_put_super(sb);
2431 journal_destroy(sbi->s_journal);
2432 if (!(sb->s_flags & MS_RDONLY)) {
2433 @@ -455,6 +456,8 @@ static struct inode *ext3_alloc_inode(st
2435 ei->i_block_alloc_info = NULL;
2436 ei->vfs_inode.i_version = 1;
2438 + memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
2439 return &ei->vfs_inode;
2442 @@ -680,7 +683,8 @@ enum {
2443 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
2444 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
2445 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2448 + Opt_extents, Opt_noextents, Opt_extdebug,
2451 static match_table_t tokens = {
2452 @@ -733,6 +737,9 @@ static match_table_t tokens = {
2453 {Opt_noiopen, "noiopen"},
2454 {Opt_iopen_nopriv, "iopen_nopriv"},
2455 {Opt_barrier, "barrier=%u"},
2456 + {Opt_extents, "extents"},
2457 + {Opt_noextents, "noextents"},
2458 + {Opt_extdebug, "extdebug"},
2460 {Opt_resize, "resize"},
2462 @@ -1077,6 +1084,15 @@ clear_qf_name:
2464 clear_opt(sbi->s_mount_opt, NOBH);
2467 + set_opt (sbi->s_mount_opt, EXTENTS);
2469 + case Opt_noextents:
2470 + clear_opt (sbi->s_mount_opt, EXTENTS);
2472 + case Opt_extdebug:
2473 + set_opt (sbi->s_mount_opt, EXTDEBUG);
2477 "EXT3-fs: Unrecognized mount option \"%s\" "
2478 @@ -1806,6 +1822,8 @@ static int ext3_fill_super (struct super
2479 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2482 + ext3_ext_init(sb);
2487 Index: linux-2.6.18.8/include/linux/ext3_extents.h
2488 ===================================================================
2489 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2490 +++ linux-2.6.18.8/include/linux/ext3_extents.h 2007-07-17 09:18:14.000000000 +0200
2493 + * Copyright 2008 Sun Microsystems, Inc.
2494 + * Written by Alex Tomas <alex@clusterfs.com>
2496 + * This program is free software; you can redistribute it and/or modify
2497 + * it under the terms of the GNU General Public License version 2 as
2498 + * published by the Free Software Foundation.
2500 + * This program is distributed in the hope that it will be useful,
2501 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2502 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2503 + * GNU General Public License for more details.
2505 + * You should have received a copy of the GNU General Public Licens
2506 + * along with this program; if not, write to the Free Software
2507 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2510 +#ifndef _LINUX_EXT3_EXTENTS
2511 +#define _LINUX_EXT3_EXTENTS
2513 +#include <linux/ext3_fs.h>
2516 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2517 + * become very little, so index split, in-depth growing and
2518 + * other hard changes happens much more often
2519 + * this is for debug purposes only
2521 +#define AGRESSIVE_TEST_
2524 + * with EXTENTS_STATS defined number of blocks and extents
2525 + * are collected in truncate path. they'll be showed at
2528 +#define EXTENTS_STATS__
2531 + * if CHECK_BINSEARCH defined, then results of binary search
2532 + * will be checked by linear search
2534 +#define CHECK_BINSEARCH__
2537 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2538 + * to get lots of info what's going on
2542 +#define ext_debug(inode,fmt,a...) \
2544 + if (test_opt(inode->i_sb, EXTDEBUG)) \
2545 + printk(fmt, ##a); \
2548 +#define ext_debug(inode,fmt,a...)
2553 + * if EXT_STATS is defined then stats numbers are collected
2554 + * these number will be displayed at umount time
2559 + * define EXT3_ALLOC_NEEDED to 0 since block bitmap, group desc. and sb
2560 + * are now accounted in ext3_ext_calc_credits_for_insert()
2562 +#define EXT3_ALLOC_NEEDED 0
2565 + * ext3_inode has i_block array (60 bytes total)
2566 + * first 12 bytes store ext3_extent_header
2567 + * the remain stores array of ext3_extent
2571 + * this is extent on-disk structure
2572 + * it's used at the bottom of the tree
2574 +struct ext3_extent {
2575 + __le32 ee_block; /* first logical block extent covers */
2576 + __le16 ee_len; /* number of blocks covered by extent */
2577 + __le16 ee_start_hi; /* high 16 bits of physical block */
2578 + __le32 ee_start; /* low 32 bigs of physical block */
2582 + * this is index on-disk structure
2583 + * it's used at all the levels, but the bottom
2585 +struct ext3_extent_idx {
2586 + __le32 ei_block; /* index covers logical blocks from 'block' */
2587 + __le32 ei_leaf; /* pointer to the physical block of the next *
2588 + * level. leaf or next index could bet here */
2589 + __le16 ei_leaf_hi; /* high 16 bits of physical block */
2594 + * each block (leaves and indexes), even inode-stored has header
2596 +struct ext3_extent_header {
2597 + __le16 eh_magic; /* probably will support different formats */
2598 + __le16 eh_entries; /* number of valid entries */
2599 + __le16 eh_max; /* capacity of store in entries */
2600 + __le16 eh_depth; /* has tree real underlaying blocks? */
2601 + __le32 eh_generation; /* flags(8 bits) | generation of the tree */
2604 +#define EXT3_EXT_MAGIC 0xf30a
2607 + * array of ext3_ext_path contains path to some extent
2608 + * creation/lookup routines use it for traversal/splitting/etc
2609 + * truncate uses it to simulate recursive walking
2611 +struct ext3_ext_path {
2614 + struct ext3_extent *p_ext;
2615 + struct ext3_extent_idx *p_idx;
2616 + struct ext3_extent_header *p_hdr;
2617 + struct buffer_head *p_bh;
2621 + * structure for external API
2624 +#define EXT3_EXT_CACHE_NO 0
2625 +#define EXT3_EXT_CACHE_GAP 1
2626 +#define EXT3_EXT_CACHE_EXTENT 2
2627 +#define EXT3_EXT_HAS_NO_TREE /* ext3_extents_tree struct is not used*/
2630 + * to be called by ext3_ext_walk_space()
2631 + * negative retcode - error
2632 + * positive retcode - signal for ext3_ext_walk_space(), see below
2633 + * callback must return valid extent (passed or newly created)
2635 +typedef int (*ext_prepare_callback)(struct inode *, struct ext3_ext_path *,
2636 + struct ext3_ext_cache *,
2639 +#define EXT_CONTINUE 0
2640 +#define EXT_BREAK 1
2641 +#define EXT_REPEAT 2
2644 +#define EXT_MAX_BLOCK 0xffffffff
2645 +#define EXT_UNSET_BLOCK 1
2647 +#define EXT_FLAGS_CLR_UNKNOWN 0x7 /* Flags cleared on modification */
2648 +#define EXT_HDR_GEN_BITS 24
2649 +#define EXT_HDR_GEN_MASK ((1 << EXT_HDR_GEN_BITS) - 1)
2651 +#define EXT_FIRST_EXTENT(__hdr__) \
2652 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2653 + sizeof(struct ext3_extent_header)))
2654 +#define EXT_FIRST_INDEX(__hdr__) \
2655 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2656 + sizeof(struct ext3_extent_header)))
2657 +#define EXT_HAS_FREE_INDEX(__path__) \
2658 + (le16_to_cpu((__path__)->p_hdr->eh_entries) < \
2659 + le16_to_cpu((__path__)->p_hdr->eh_max))
2660 +#define EXT_LAST_EXTENT(__hdr__) \
2661 + (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1)
2662 +#define EXT_LAST_INDEX(__hdr__) \
2663 + (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1)
2664 +#define EXT_MAX_EXTENT(__hdr__) \
2665 + (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)
2666 +#define EXT_MAX_INDEX(__hdr__) \
2667 + (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)
2670 +static inline struct ext3_extent_header *ext_inode_hdr(struct inode *inode)
2672 + return (struct ext3_extent_header *) EXT3_I(inode)->i_data;
2675 +static inline struct ext3_extent_header *ext_block_hdr(struct buffer_head *bh)
2677 + return (struct ext3_extent_header *) bh->b_data;
2680 +static inline unsigned short ext_depth(struct inode *inode)
2682 + return le16_to_cpu(ext_inode_hdr(inode)->eh_depth);
2685 +static inline unsigned short ext_flags(struct ext3_extent_header *neh)
2687 + return le16_to_cpu(neh->eh_generation) >> EXT_HDR_GEN_BITS;
2690 +static inline unsigned short ext_hdr_gen(struct ext3_extent_header *neh)
2692 + return le16_to_cpu(neh->eh_generation) & EXT_HDR_GEN_MASK;
2695 +static inline unsigned short ext_generation(struct inode *inode)
2697 + return ext_hdr_gen(ext_inode_hdr(inode));
2700 +static inline void ext3_ext_tree_changed(struct inode *inode)
2702 + struct ext3_extent_header *neh = ext_inode_hdr(inode);
2703 + neh->eh_generation = cpu_to_le32(
2704 + ((ext_flags(neh) & ~EXT_FLAGS_CLR_UNKNOWN) << EXT_HDR_GEN_BITS)
2705 + | ((ext_hdr_gen(neh) + 1) & EXT_HDR_GEN_MASK));
2709 +ext3_ext_invalidate_cache(struct inode *inode)
2711 + EXT3_I(inode)->i_cached_extent.ec_type = EXT3_EXT_CACHE_NO;
2714 +extern int ext3_ext_search_left(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
2715 +extern int ext3_ext_search_right(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
2716 +extern int ext3_extent_tree_init(handle_t *, struct inode *);
2717 +extern int ext3_ext_calc_credits_for_insert(struct inode *, struct ext3_ext_path *);
2718 +extern int ext3_ext_insert_extent(handle_t *, struct inode *, struct ext3_ext_path *, struct ext3_extent *);
2719 +extern int ext3_ext_walk_space(struct inode *, unsigned long, unsigned long, ext_prepare_callback, void *);
2720 +extern struct ext3_ext_path * ext3_ext_find_extent(struct inode *, int, struct ext3_ext_path *);
2722 +#endif /* _LINUX_EXT3_EXTENTS */
2724 Index: linux-2.6.18.8/include/linux/ext3_fs.h
2725 ===================================================================
2726 --- linux-2.6.18.8.orig/include/linux/ext3_fs.h 2007-07-17 09:18:13.000000000 +0200
2727 +++ linux-2.6.18.8/include/linux/ext3_fs.h 2007-07-17 11:08:12.000000000 +0200
2728 @@ -182,8 +182,9 @@ struct ext3_group_desc
2729 #define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2730 #define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2731 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2732 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2734 -#define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
2735 +#define EXT3_FL_USER_VISIBLE 0x000BDFFF /* User visible flags */
2736 #define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
2739 @@ -373,6 +374,8 @@ struct ext3_inode {
2740 #define EXT3_MOUNT_GRPQUOTA 0x200000 /* "old" group quota */
2741 #define EXT3_MOUNT_IOPEN 0x400000 /* Allow access via iopen */
2742 #define EXT3_MOUNT_IOPEN_NOPRIV 0x800000/* Make iopen world-readable */
2743 +#define EXT3_MOUNT_EXTENTS 0x2000000/* Extents support */
2744 +#define EXT3_MOUNT_EXTDEBUG 0x4000000/* Extents debug */
2746 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2748 @@ -572,11 +575,13 @@ static inline int ext3_valid_inum(struct
2749 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2750 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2751 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
2752 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2754 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2755 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2756 EXT3_FEATURE_INCOMPAT_RECOVER| \
2757 - EXT3_FEATURE_INCOMPAT_META_BG)
2758 + EXT3_FEATURE_INCOMPAT_META_BG| \
2759 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2760 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2761 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2762 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2763 @@ -816,6 +821,9 @@ extern int ext3_get_inode_loc(struct ino
2764 extern void ext3_set_aops(struct inode *inode);
2765 extern int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2766 u64 start, u64 len);
2767 +extern int ext3_writepage_trans_blocks(struct inode *);
2768 +extern int ext3_block_truncate_page(handle_t *handle, struct page *page,
2769 + struct address_space *mapping, loff_t from);
2772 extern int ext3_ioctl (struct inode *, struct file *, unsigned int,
2773 @@ -869,6 +877,30 @@ extern struct inode_operations ext3_spec
2774 extern struct inode_operations ext3_symlink_inode_operations;
2775 extern struct inode_operations ext3_fast_symlink_inode_operations;
2778 +extern int ext3_ext_tree_init(handle_t *handle, struct inode *);
2779 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2780 +extern int ext3_ext_get_blocks(handle_t *, struct inode *, sector_t,
2781 + unsigned long, struct buffer_head *, int, int);
2782 +extern void ext3_ext_truncate(struct inode *, struct page *);
2783 +extern void ext3_ext_init(struct super_block *);
2784 +extern void ext3_ext_release(struct super_block *);
2786 +ext3_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
2787 + unsigned long max_blocks, struct buffer_head *bh,
2788 + int create, int extend_disksize)
2791 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2792 + return ext3_ext_get_blocks(handle, inode, block, max_blocks,
2793 + bh, create, extend_disksize);
2794 + ret = ext3_get_blocks_handle(handle, inode, block, max_blocks, bh, create,
2797 + bh->b_size = (ret << inode->i_blkbits);
2802 #endif /* __KERNEL__ */
2804 Index: linux-2.6.18.8/include/linux/ext3_fs_i.h
2805 ===================================================================
2806 --- linux-2.6.18.8.orig/include/linux/ext3_fs_i.h 2007-02-24 00:52:30.000000000 +0100
2807 +++ linux-2.6.18.8/include/linux/ext3_fs_i.h 2007-07-17 11:08:11.000000000 +0200
2808 @@ -65,6 +65,16 @@ struct ext3_block_alloc_info {
2809 #define rsv_end rsv_window._rsv_end
2812 + * storage for cached extent
2814 +struct ext3_ext_cache {
2817 + __u32 ec_len; /* must be 32bit to return holes */
2822 * third extended file system inode data in memory
2824 struct ext3_inode_info {
2825 @@ -142,6 +152,8 @@ struct ext3_inode_info {
2827 struct mutex truncate_mutex;
2828 struct inode vfs_inode;
2830 + struct ext3_ext_cache i_cached_extent;
2833 #endif /* _LINUX_EXT3_FS_I */
2834 Index: linux-2.6.18.8/include/linux/ext3_fs_sb.h
2835 ===================================================================
2836 --- linux-2.6.18.8.orig/include/linux/ext3_fs_sb.h 2007-02-24 00:52:30.000000000 +0100
2837 +++ linux-2.6.18.8/include/linux/ext3_fs_sb.h 2007-07-17 11:08:12.000000000 +0200
2838 @@ -78,6 +78,16 @@ struct ext3_sb_info {
2839 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
2840 int s_jquota_fmt; /* Format of quota to use */
2843 +#ifdef EXTENTS_STATS
2844 + /* ext3 extents stats */
2845 + unsigned long s_ext_min;
2846 + unsigned long s_ext_max;
2847 + unsigned long s_depth_max;
2848 + spinlock_t s_ext_stats_lock;
2849 + unsigned long s_ext_blocks;
2850 + unsigned long s_ext_extents;
2854 #endif /* _LINUX_EXT3_FS_SB */
2855 Index: linux-2.6.18.8/include/linux/ext3_jbd.h
2856 ===================================================================
2857 --- linux-2.6.18.8.orig/include/linux/ext3_jbd.h 2007-02-24 00:52:30.000000000 +0100
2858 +++ linux-2.6.18.8/include/linux/ext3_jbd.h 2007-07-17 09:18:14.000000000 +0200
2861 /* Define the number of blocks we need to account to a transaction to
2862 * modify one block of data.
2865 * We may have to touch one inode, one bitmap buffer, up to three
2866 * indirection blocks, the group and superblock summaries, and the data
2867 - * block to complete the transaction. */
2868 + * block to complete the transaction.
2870 + * For extents-enabled fs we may have to allocate and modify upto
2871 + * 5 levels of tree + root which is stored in inode. */
2873 +#define EXT3_SINGLEDATA_TRANS_BLOCKS(sb) \
2874 + (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS) \
2875 + || test_opt(sb, EXTENTS) ? 27U : 8U)
2877 -#define EXT3_SINGLEDATA_TRANS_BLOCKS 8U
2878 +/* Indicate that EXT3_SINGLEDATA_TRANS_BLOCKS takes the sb as argument */
2879 +#define EXT3_SINGLEDATA_TRANS_BLOCKS_HAS_SB
2881 /* Extended attribute operations touch at most two data buffers,
2882 * two bitmap buffers, and two group summaries, in addition to the inode
2884 * superblock only gets updated once, of course, so don't bother
2885 * counting that again for the quota updates. */
2887 -#define EXT3_DATA_TRANS_BLOCKS(sb) (EXT3_SINGLEDATA_TRANS_BLOCKS + \
2888 +#define EXT3_DATA_TRANS_BLOCKS(sb) (EXT3_SINGLEDATA_TRANS_BLOCKS(sb) + \
2889 EXT3_XATTR_TRANS_BLOCKS - 2 + \
2890 2*EXT3_QUOTA_TRANS_BLOCKS(sb))
2893 /* Amount of blocks needed for quota insert/delete - we do some block writes
2894 * but inode, sb and group updates are done only once */
2895 #define EXT3_QUOTA_INIT_BLOCKS(sb) (test_opt(sb, QUOTA) ? (DQUOT_INIT_ALLOC*\
2896 - (EXT3_SINGLEDATA_TRANS_BLOCKS-3)+3+DQUOT_INIT_REWRITE) : 0)
2897 + (EXT3_SINGLEDATA_TRANS_BLOCKS(sb)-3)+3+DQUOT_INIT_REWRITE) : 0)
2898 #define EXT3_QUOTA_DEL_BLOCKS(sb) (test_opt(sb, QUOTA) ? (DQUOT_DEL_ALLOC*\
2899 - (EXT3_SINGLEDATA_TRANS_BLOCKS-3)+3+DQUOT_DEL_REWRITE) : 0)
2900 + (EXT3_SINGLEDATA_TRANS_BLOCKS(sb)-3)+3+DQUOT_DEL_REWRITE) : 0)
2902 #define EXT3_QUOTA_TRANS_BLOCKS(sb) 0
2903 #define EXT3_QUOTA_INIT_BLOCKS(sb) 0