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 (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
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, 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";
271 + ext3_error(inode->i_sb, function,
272 + "bad header in inode #%lu: %s - magic %x, "
273 + "entries %u, max %u(%u), depth %u(%u)",
274 + inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
275 + le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
276 + max, le16_to_cpu(eh->eh_depth), depth);
281 +#define ext3_ext_check_header(inode,eh,depth) \
282 + __ext3_ext_check_header(__FUNCTION__,inode,eh,depth)
285 +static void ext3_ext_show_path(struct inode *inode, struct ext3_ext_path *path)
287 + int k, l = path->p_depth;
289 + ext_debug(inode, "path:");
290 + for (k = 0; k <= l; k++, path++) {
292 + ext_debug(inode, " %d->%d", le32_to_cpu(path->p_idx->ei_block),
293 + le32_to_cpu(path->p_idx->ei_leaf));
294 + } else if (path->p_ext) {
295 + ext_debug(inode, " %d:%d:%d",
296 + le32_to_cpu(path->p_ext->ee_block),
297 + le16_to_cpu(path->p_ext->ee_len),
298 + le32_to_cpu(path->p_ext->ee_start));
300 + ext_debug(inode, " []");
302 + ext_debug(inode, "\n");
305 +static void ext3_ext_show_leaf(struct inode *inode, struct ext3_ext_path *path)
307 + int depth = ext_depth(inode);
308 + struct ext3_extent_header *eh;
309 + struct ext3_extent *ex;
315 + eh = path[depth].p_hdr;
316 + ex = EXT_FIRST_EXTENT(eh);
318 + for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
319 + ext_debug(inode, "%d:%d:%d ", le32_to_cpu(ex->ee_block),
320 + le16_to_cpu(ex->ee_len),
321 + le32_to_cpu(ex->ee_start));
323 + ext_debug(inode, "\n");
326 +#define ext3_ext_show_path(inode,path)
327 +#define ext3_ext_show_leaf(inode,path)
330 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
332 + int depth = path->p_depth;
335 + for (i = 0; i <= depth; i++, path++)
337 + brelse(path->p_bh);
343 + * binary search for closest index by given block
344 + * the header must be checked before calling this
347 +ext3_ext_binsearch_idx(struct inode *inode, struct ext3_ext_path *path, int block)
349 + struct ext3_extent_header *eh = path->p_hdr;
350 + struct ext3_extent_idx *r, *l, *m;
352 + ext_debug(inode, "binsearch for %d(idx): ", block);
354 + l = EXT_FIRST_INDEX(eh) + 1;
355 + r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
357 + m = l + (r - l) / 2;
358 + if (block < le32_to_cpu(m->ei_block))
362 + ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
363 + m, m->ei_block, r, r->ei_block);
366 + path->p_idx = l - 1;
367 + ext_debug(inode, " -> %d->%d ", le32_to_cpu(path->p_idx->ei_block),
368 + le32_to_cpu(path->p_idx->ei_leaf));
370 +#ifdef CHECK_BINSEARCH
372 + struct ext3_extent_idx *chix, *ix;
375 + chix = ix = EXT_FIRST_INDEX(eh);
376 + for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
378 + le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
379 + printk("k=%d, ix=0x%p, first=0x%p\n", k,
380 + ix, EXT_FIRST_INDEX(eh));
381 + printk("%u <= %u\n",
382 + le32_to_cpu(ix->ei_block),
383 + le32_to_cpu(ix[-1].ei_block));
385 + BUG_ON(k && le32_to_cpu(ix->ei_block)
386 + <= le32_to_cpu(ix[-1].ei_block));
387 + if (block < le32_to_cpu(ix->ei_block))
391 + BUG_ON(chix != path->p_idx);
398 + * binary search for closest extent by given block
399 + * the header must be checked before calling this
402 +ext3_ext_binsearch(struct inode *inode, struct ext3_ext_path *path, int block)
404 + struct ext3_extent_header *eh = path->p_hdr;
405 + struct ext3_extent *r, *l, *m;
407 + if (eh->eh_entries == 0) {
409 + * this leaf is empty yet:
410 + * we get such a leaf in split/add case
415 + ext_debug(inode, "binsearch for %d: ", block);
417 + l = EXT_FIRST_EXTENT(eh) + 1;
418 + r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
421 + m = l + (r - l) / 2;
422 + if (block < le32_to_cpu(m->ee_block))
426 + ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
427 + m, m->ee_block, r, r->ee_block);
430 + path->p_ext = l - 1;
431 + ext_debug(inode, " -> %d:%d:%d ",
432 + le32_to_cpu(path->p_ext->ee_block),
433 + le32_to_cpu(path->p_ext->ee_start),
434 + le16_to_cpu(path->p_ext->ee_len));
436 +#ifdef CHECK_BINSEARCH
438 + struct ext3_extent *chex, *ex;
441 + chex = ex = EXT_FIRST_EXTENT(eh);
442 + for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
443 + BUG_ON(k && le32_to_cpu(ex->ee_block)
444 + <= le32_to_cpu(ex[-1].ee_block));
445 + if (block < le32_to_cpu(ex->ee_block))
449 + BUG_ON(chex != path->p_ext);
455 +int ext3_ext_tree_init(handle_t *handle, struct inode *inode)
457 + struct ext3_extent_header *eh;
459 + eh = ext_inode_hdr(inode);
461 + eh->eh_entries = 0;
462 + eh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
463 + eh->eh_max = cpu_to_le16(ext3_ext_space_root(inode));
464 + ext3_mark_inode_dirty(handle, inode);
465 + ext3_ext_invalidate_cache(inode);
469 +struct ext3_ext_path *
470 +ext3_ext_find_extent(struct inode *inode, int block, struct ext3_ext_path *path)
472 + struct ext3_extent_header *eh;
473 + struct buffer_head *bh;
474 + short int depth, i, ppos = 0, alloc = 0;
476 + eh = ext_inode_hdr(inode);
477 + i = depth = ext_depth(inode);
478 + if (ext3_ext_check_header(inode, eh, depth))
479 + return ERR_PTR(-EIO);
481 + /* account possible depth increase */
483 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
486 + return ERR_PTR(-ENOMEM);
489 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
490 + path[0].p_hdr = eh;
492 + /* walk through the tree */
494 + ext_debug(inode, "depth %d: num %d, max %d\n",
495 + ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
497 + ext3_ext_binsearch_idx(inode, path + ppos, block);
498 + path[ppos].p_block = le32_to_cpu(path[ppos].p_idx->ei_leaf);
499 + path[ppos].p_depth = i;
500 + path[ppos].p_ext = NULL;
502 + bh = sb_bread(inode->i_sb, path[ppos].p_block);
506 + eh = ext_block_hdr(bh);
508 + BUG_ON(ppos > depth);
509 + path[ppos].p_bh = bh;
510 + path[ppos].p_hdr = eh;
513 + if (ext3_ext_check_header(inode, eh, i))
517 + path[ppos].p_depth = i;
518 + path[ppos].p_hdr = eh;
519 + path[ppos].p_ext = NULL;
520 + path[ppos].p_idx = NULL;
523 + ext3_ext_binsearch(inode, path + ppos, block);
525 + ext3_ext_show_path(inode, path);
530 + ext3_ext_drop_refs(path);
533 + return ERR_PTR(-EIO);
537 + * insert new index [logical;ptr] into the block at cupr
538 + * it check where to insert: before curp or after curp
540 +static int ext3_ext_insert_index(handle_t *handle, struct inode *inode,
541 + struct ext3_ext_path *curp,
542 + int logical, int ptr)
544 + struct ext3_extent_idx *ix;
547 + if ((err = ext3_ext_get_access(handle, inode, curp)))
550 + BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
551 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
552 + if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
554 + if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
555 + len = (len - 1) * sizeof(struct ext3_extent_idx);
556 + len = len < 0 ? 0 : len;
557 + ext_debug(inode, "insert new index %d after: %d. "
558 + "move %d from 0x%p to 0x%p\n",
560 + (curp->p_idx + 1), (curp->p_idx + 2));
561 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
563 + ix = curp->p_idx + 1;
565 + /* insert before */
566 + len = len * sizeof(struct ext3_extent_idx);
567 + len = len < 0 ? 0 : len;
568 + ext_debug(inode, "insert new index %d before: %d. "
569 + "move %d from 0x%p to 0x%p\n",
571 + curp->p_idx, (curp->p_idx + 1));
572 + memmove(curp->p_idx + 1, curp->p_idx, len);
576 + ix->ei_block = cpu_to_le32(logical);
577 + ix->ei_leaf = cpu_to_le32(ptr);
578 + ix->ei_leaf_hi = ix->ei_unused = 0;
579 + curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
581 + BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
582 + > le16_to_cpu(curp->p_hdr->eh_max));
583 + BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
585 + err = ext3_ext_dirty(handle, inode, curp);
586 + ext3_std_error(inode->i_sb, err);
592 + * routine inserts new subtree into the path, using free index entry
594 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
595 + * - makes decision where to split
596 + * - moves remaining extens and index entries (right to the split point)
597 + * into the newly allocated blocks
598 + * - initialize subtree
600 +static int ext3_ext_split(handle_t *handle, struct inode *inode,
601 + struct ext3_ext_path *path,
602 + struct ext3_extent *newext, int at)
604 + struct buffer_head *bh = NULL;
605 + int depth = ext_depth(inode);
606 + struct ext3_extent_header *neh;
607 + struct ext3_extent_idx *fidx;
608 + struct ext3_extent *ex;
609 + int i = at, k, m, a;
610 + unsigned long newblock, oldblock;
612 + int *ablocks = NULL; /* array of allocated blocks */
615 + /* make decision: where to split? */
616 + /* FIXME: now desicion is simplest: at current extent */
618 + /* if current leaf will be splitted, then we should use
619 + * border from split point */
620 + BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
621 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
622 + border = path[depth].p_ext[1].ee_block;
623 + ext_debug(inode, "leaf will be splitted."
624 + " next leaf starts at %d\n",
625 + le32_to_cpu(border));
627 + border = newext->ee_block;
628 + ext_debug(inode, "leaf will be added."
629 + " next leaf starts at %d\n",
630 + le32_to_cpu(border));
634 + * if error occurs, then we break processing
635 + * and turn filesystem read-only. so, index won't
636 + * be inserted and tree will be in consistent
637 + * state. next mount will repair buffers too
641 + * get array to track all allocated blocks
642 + * we need this to handle errors and free blocks
645 + ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
648 + memset(ablocks, 0, sizeof(unsigned long) * depth);
650 + /* allocate all needed blocks */
651 + ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at);
652 + for (a = 0; a < depth - at; a++) {
653 + newblock = ext3_ext_new_block(handle, inode, path, newext, &err);
656 + ablocks[a] = newblock;
659 + /* initialize new leaf */
660 + newblock = ablocks[--a];
661 + BUG_ON(newblock == 0);
662 + bh = sb_getblk(inode->i_sb, newblock);
669 + if ((err = ext3_journal_get_create_access(handle, bh)))
672 + neh = ext_block_hdr(bh);
673 + neh->eh_entries = 0;
674 + neh->eh_max = cpu_to_le16(ext3_ext_space_block(inode));
675 + neh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
677 + ex = EXT_FIRST_EXTENT(neh);
679 + /* move remain of path[depth] to the new leaf */
680 + BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
681 + /* start copy from next extent */
682 + /* TODO: we could do it by single memmove */
684 + path[depth].p_ext++;
685 + while (path[depth].p_ext <=
686 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
687 + ext_debug(inode, "move %d:%d:%d in new leaf %lu\n",
688 + le32_to_cpu(path[depth].p_ext->ee_block),
689 + le32_to_cpu(path[depth].p_ext->ee_start),
690 + le16_to_cpu(path[depth].p_ext->ee_len),
692 + /*memmove(ex++, path[depth].p_ext++,
693 + sizeof(struct ext3_extent));
694 + neh->eh_entries++;*/
695 + path[depth].p_ext++;
699 + memmove(ex, path[depth].p_ext-m, sizeof(struct ext3_extent)*m);
700 + neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
703 + set_buffer_uptodate(bh);
706 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
711 + /* correct old leaf */
713 + if ((err = ext3_ext_get_access(handle, inode, path + depth)))
715 + path[depth].p_hdr->eh_entries =
716 + cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
717 + if ((err = ext3_ext_dirty(handle, inode, path + depth)))
722 + /* create intermediate indexes */
723 + k = depth - at - 1;
726 + ext_debug(inode, "create %d intermediate indices\n", k);
727 + /* insert new index into current index block */
728 + /* current depth stored in i var */
731 + oldblock = newblock;
732 + newblock = ablocks[--a];
733 + bh = sb_getblk(inode->i_sb, newblock);
740 + if ((err = ext3_journal_get_create_access(handle, bh)))
743 + neh = ext_block_hdr(bh);
744 + neh->eh_entries = cpu_to_le16(1);
745 + neh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
746 + neh->eh_max = cpu_to_le16(ext3_ext_space_block_idx(inode));
747 + neh->eh_depth = cpu_to_le16(depth - i);
748 + fidx = EXT_FIRST_INDEX(neh);
749 + fidx->ei_block = border;
750 + fidx->ei_leaf = cpu_to_le32(oldblock);
751 + fidx->ei_leaf_hi = fidx->ei_unused = 0;
753 + ext_debug(inode, "int.index at %d (block %lu): %lu -> %lu\n", i,
754 + newblock, (unsigned long) le32_to_cpu(border),
760 + ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx,
761 + EXT_MAX_INDEX(path[i].p_hdr));
762 + BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
763 + EXT_LAST_INDEX(path[i].p_hdr));
764 + while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
765 + ext_debug(inode, "%d: move %d:%d in new index %lu\n", i,
766 + le32_to_cpu(path[i].p_idx->ei_block),
767 + le32_to_cpu(path[i].p_idx->ei_leaf),
769 + /*memmove(++fidx, path[i].p_idx++,
770 + sizeof(struct ext3_extent_idx));
772 + BUG_ON(neh->eh_entries > neh->eh_max);*/
777 + memmove(++fidx, path[i].p_idx - m,
778 + sizeof(struct ext3_extent_idx) * m);
780 + cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
782 + set_buffer_uptodate(bh);
785 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
790 + /* correct old index */
792 + err = ext3_ext_get_access(handle, inode, path + i);
795 + path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
796 + err = ext3_ext_dirty(handle, inode, path + i);
804 + /* insert new index */
808 + err = ext3_ext_insert_index(handle, inode, path + at,
809 + le32_to_cpu(border), newblock);
813 + if (buffer_locked(bh))
819 + /* free all allocated blocks in error case */
820 + for (i = 0; i < depth; i++) {
823 + ext3_free_blocks(handle, inode, ablocks[i], 1);
832 + * routine implements tree growing procedure:
833 + * - allocates new block
834 + * - moves top-level data (index block or leaf) into the new block
835 + * - initialize new top-level, creating index that points to the
836 + * just created block
838 +static int ext3_ext_grow_indepth(handle_t *handle, struct inode *inode,
839 + struct ext3_ext_path *path,
840 + struct ext3_extent *newext)
842 + struct ext3_ext_path *curp = path;
843 + struct ext3_extent_header *neh;
844 + struct ext3_extent_idx *fidx;
845 + struct buffer_head *bh;
846 + unsigned long newblock;
849 + newblock = ext3_ext_new_block(handle, inode, path, newext, &err);
853 + bh = sb_getblk(inode->i_sb, newblock);
856 + ext3_std_error(inode->i_sb, err);
861 + if ((err = ext3_journal_get_create_access(handle, bh))) {
866 + /* move top-level index/leaf into new block */
867 + memmove(bh->b_data, curp->p_hdr, sizeof(EXT3_I(inode)->i_data));
869 + /* set size of new block */
870 + neh = ext_block_hdr(bh);
871 + /* old root could have indexes or leaves
872 + * so calculate e_max right way */
873 + if (ext_depth(inode))
874 + neh->eh_max = cpu_to_le16(ext3_ext_space_block_idx(inode));
876 + neh->eh_max = cpu_to_le16(ext3_ext_space_block(inode));
877 + neh->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
878 + set_buffer_uptodate(bh);
881 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
884 + /* create index in new top-level index: num,max,pointer */
885 + if ((err = ext3_ext_get_access(handle, inode, curp)))
888 + curp->p_hdr->eh_magic = cpu_to_le16(EXT3_EXT_MAGIC);
889 + curp->p_hdr->eh_max = cpu_to_le16(ext3_ext_space_root_idx(inode));
890 + curp->p_hdr->eh_entries = cpu_to_le16(1);
891 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
892 + /* FIXME: it works, but actually path[0] can be index */
893 + curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
894 + curp->p_idx->ei_leaf = cpu_to_le32(newblock);
895 + curp->p_idx->ei_leaf_hi = curp->p_idx->ei_unused = 0;
897 + neh = ext_inode_hdr(inode);
898 + fidx = EXT_FIRST_INDEX(neh);
899 + ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %d\n",
900 + le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
901 + le32_to_cpu(fidx->ei_block), le32_to_cpu(fidx->ei_leaf));
903 + neh->eh_depth = cpu_to_le16(path->p_depth + 1);
904 + err = ext3_ext_dirty(handle, inode, curp);
912 + * routine finds empty index and adds new leaf. if no free index found
913 + * then it requests in-depth growing
915 +static int ext3_ext_create_new_leaf(handle_t *handle, struct inode *inode,
916 + struct ext3_ext_path *path,
917 + struct ext3_extent *newext)
919 + struct ext3_ext_path *curp;
920 + int depth, i, err = 0;
923 + i = depth = ext_depth(inode);
925 + /* walk up to the tree and look for free index entry */
926 + curp = path + depth;
927 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
932 + /* we use already allocated block for index block
933 + * so, subsequent data blocks should be contigoues */
934 + if (EXT_HAS_FREE_INDEX(curp)) {
935 + /* if we found index with free entry, then use that
936 + * entry: create all needed subtree and add new leaf */
937 + err = ext3_ext_split(handle, inode, path, newext, i);
942 + ext3_ext_drop_refs(path);
943 + path = ext3_ext_find_extent(inode,
944 + le32_to_cpu(newext->ee_block),
947 + err = PTR_ERR(path);
949 + /* tree is full, time to grow in depth */
950 + err = ext3_ext_grow_indepth(handle, inode, path, newext);
955 + ext3_ext_drop_refs(path);
956 + path = ext3_ext_find_extent(inode,
957 + le32_to_cpu(newext->ee_block),
959 + if (IS_ERR(path)) {
960 + err = PTR_ERR(path);
965 + * only first (depth 0 -> 1) produces free space
966 + * in all other cases we have to split growed tree
968 + depth = ext_depth(inode);
969 + if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
970 + /* now we need split */
980 + * search the closest allocated block to the left for *logical
981 + * and returns it at @logical + it's physical address at @phys
982 + * if *logical is the smallest allocated block, the function
983 + * returns 0 at @phys
984 + * return value contains 0 (success) or error code
987 +ext3_ext_search_left(struct inode *inode, struct ext3_ext_path *path,
988 + unsigned long *logical, unsigned long *phys)
990 + struct ext3_extent_idx *ix;
991 + struct ext3_extent *ex;
994 + BUG_ON(path == NULL);
995 + depth = path->p_depth;
998 + if (depth == 0 && path->p_ext == NULL)
1001 + /* usually extent in the path covers blocks smaller
1002 + * then *logical, but it can be that extent is the
1003 + * first one in the file */
1005 + ex = path[depth].p_ext;
1006 + if (*logical < le32_to_cpu(ex->ee_block)) {
1007 + BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1008 + while (--depth >= 0) {
1009 + ix = path[depth].p_idx;
1010 + BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1015 + BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
1017 + *logical = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1;
1018 + *phys = le32_to_cpu(ex->ee_start) + le16_to_cpu(ex->ee_len) - 1;
1021 +EXPORT_SYMBOL(ext3_ext_search_left);
1024 + * search the closest allocated block to the right for *logical
1025 + * and returns it at @logical + it's physical address at @phys
1026 + * if *logical is the smallest allocated block, the function
1027 + * returns 0 at @phys
1028 + * return value contains 0 (success) or error code
1031 +ext3_ext_search_right(struct inode *inode, struct ext3_ext_path *path,
1032 + unsigned long *logical, unsigned long *phys)
1034 + struct buffer_head *bh = NULL;
1035 + struct ext3_extent_header *eh;
1036 + struct ext3_extent_idx *ix;
1037 + struct ext3_extent *ex;
1038 + unsigned long block;
1041 + BUG_ON(path == NULL);
1042 + depth = path->p_depth;
1045 + if (depth == 0 && path->p_ext == NULL)
1048 + /* usually extent in the path covers blocks smaller
1049 + * then *logical, but it can be that extent is the
1050 + * first one in the file */
1052 + ex = path[depth].p_ext;
1053 + if (*logical < le32_to_cpu(ex->ee_block)) {
1054 + BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1055 + while (--depth >= 0) {
1056 + ix = path[depth].p_idx;
1057 + BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1059 + *logical = le32_to_cpu(ex->ee_block);
1060 + *phys = le32_to_cpu(ex->ee_start);
1064 + BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
1066 + if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1067 + /* next allocated block in this leaf */
1069 + *logical = le32_to_cpu(ex->ee_block);
1070 + *phys = le32_to_cpu(ex->ee_start);
1074 + /* go up and search for index to the right */
1075 + while (--depth >= 0) {
1076 + ix = path[depth].p_idx;
1077 + if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1082 + /* we've gone up to the root and
1083 + * found no index to the right */
1087 + /* we've found index to the right, let's
1088 + * follow it and find the closest allocated
1089 + * block to the right */
1091 + block = le32_to_cpu(ix->ei_leaf);
1092 + while (++depth < path->p_depth) {
1093 + bh = sb_bread(inode->i_sb, block);
1096 + eh = ext_block_hdr(bh);
1097 + if (ext3_ext_check_header(inode, eh, path->p_depth - depth)) {
1101 + ix = EXT_FIRST_INDEX(eh);
1102 + block = le32_to_cpu(ix->ei_leaf);
1106 + bh = sb_bread(inode->i_sb, block);
1109 + eh = ext_block_hdr(bh);
1110 + if (ext3_ext_check_header(inode, eh, 0)) {
1114 + ex = EXT_FIRST_EXTENT(eh);
1115 + *logical = le32_to_cpu(ex->ee_block);
1116 + *phys = le32_to_cpu(ex->ee_start);
1121 +EXPORT_SYMBOL(ext3_ext_search_right);
1126 + * returns allocated block in subsequent extent or EXT_MAX_BLOCK
1127 + * NOTE: it consider block number from index entry as
1128 + * allocated block. thus, index entries have to be consistent
1131 +static unsigned long
1132 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
1136 + BUG_ON(path == NULL);
1137 + depth = path->p_depth;
1139 + if (depth == 0 && path->p_ext == NULL)
1140 + return EXT_MAX_BLOCK;
1142 + while (depth >= 0) {
1143 + if (depth == path->p_depth) {
1145 + if (path[depth].p_ext !=
1146 + EXT_LAST_EXTENT(path[depth].p_hdr))
1147 + return le32_to_cpu(path[depth].p_ext[1].ee_block);
1150 + if (path[depth].p_idx !=
1151 + EXT_LAST_INDEX(path[depth].p_hdr))
1152 + return le32_to_cpu(path[depth].p_idx[1].ei_block);
1157 + return EXT_MAX_BLOCK;
1161 + * returns first allocated block from next leaf or EXT_MAX_BLOCK
1163 +static unsigned ext3_ext_next_leaf_block(struct inode *inode,
1164 + struct ext3_ext_path *path)
1168 + BUG_ON(path == NULL);
1169 + depth = path->p_depth;
1171 + /* zero-tree has no leaf blocks at all */
1173 + return EXT_MAX_BLOCK;
1175 + /* go to index block */
1178 + while (depth >= 0) {
1179 + if (path[depth].p_idx !=
1180 + EXT_LAST_INDEX(path[depth].p_hdr))
1181 + return le32_to_cpu(path[depth].p_idx[1].ei_block);
1185 + return EXT_MAX_BLOCK;
1189 + * if leaf gets modified and modified extent is first in the leaf
1190 + * then we have to correct all indexes above
1191 + * TODO: do we need to correct tree in all cases?
1193 +int ext3_ext_correct_indexes(handle_t *handle, struct inode *inode,
1194 + struct ext3_ext_path *path)
1196 + struct ext3_extent_header *eh;
1197 + int depth = ext_depth(inode);
1198 + struct ext3_extent *ex;
1202 + eh = path[depth].p_hdr;
1203 + ex = path[depth].p_ext;
1204 + BUG_ON(ex == NULL);
1205 + BUG_ON(eh == NULL);
1208 + /* there is no tree at all */
1212 + if (ex != EXT_FIRST_EXTENT(eh)) {
1213 + /* we correct tree if first leaf got modified only */
1218 + * TODO: we need correction if border is smaller then current one
1221 + border = path[depth].p_ext->ee_block;
1222 + if ((err = ext3_ext_get_access(handle, inode, path + k)))
1224 + path[k].p_idx->ei_block = border;
1225 + if ((err = ext3_ext_dirty(handle, inode, path + k)))
1229 + /* change all left-side indexes */
1230 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1232 + if ((err = ext3_ext_get_access(handle, inode, path + k)))
1234 + path[k].p_idx->ei_block = border;
1235 + if ((err = ext3_ext_dirty(handle, inode, path + k)))
1243 +ext3_can_extents_be_merged(struct inode *inode, struct ext3_extent *ex1,
1244 + struct ext3_extent *ex2)
1246 + /* FIXME: 48bit support */
1247 + if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len)
1248 + != le32_to_cpu(ex2->ee_block))
1251 +#ifdef AGRESSIVE_TEST
1252 + if (le16_to_cpu(ex1->ee_len) >= 4)
1256 + if (le32_to_cpu(ex1->ee_start) + le16_to_cpu(ex1->ee_len)
1257 + == le32_to_cpu(ex2->ee_start))
1263 + * this routine tries to merge requsted extent into the existing
1264 + * extent or inserts requested extent as new one into the tree,
1265 + * creating new leaf in no-space case
1267 +int ext3_ext_insert_extent(handle_t *handle, struct inode *inode,
1268 + struct ext3_ext_path *path,
1269 + struct ext3_extent *newext)
1271 + struct ext3_extent_header * eh;
1272 + struct ext3_extent *ex, *fex;
1273 + struct ext3_extent *nearex; /* nearest extent */
1274 + struct ext3_ext_path *npath = NULL;
1275 + int depth, len, err, next;
1277 + BUG_ON(newext->ee_len == 0);
1278 + depth = ext_depth(inode);
1279 + ex = path[depth].p_ext;
1280 + BUG_ON(path[depth].p_hdr == NULL);
1282 + /* try to insert block into found extent and return */
1283 + if (ex && ext3_can_extents_be_merged(inode, ex, newext)) {
1284 + ext_debug(inode, "append %d block to %d:%d (from %d)\n",
1285 + le16_to_cpu(newext->ee_len),
1286 + le32_to_cpu(ex->ee_block),
1287 + le16_to_cpu(ex->ee_len),
1288 + le32_to_cpu(ex->ee_start));
1289 + if ((err = ext3_ext_get_access(handle, inode, path + depth)))
1291 + ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
1292 + + le16_to_cpu(newext->ee_len));
1293 + eh = path[depth].p_hdr;
1299 + depth = ext_depth(inode);
1300 + eh = path[depth].p_hdr;
1301 + if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1304 + /* probably next leaf has space for us? */
1305 + fex = EXT_LAST_EXTENT(eh);
1306 + next = ext3_ext_next_leaf_block(inode, path);
1307 + if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1308 + && next != EXT_MAX_BLOCK) {
1309 + ext_debug(inode, "next leaf block - %d\n", next);
1310 + BUG_ON(npath != NULL);
1311 + npath = ext3_ext_find_extent(inode, next, NULL);
1312 + if (IS_ERR(npath))
1313 + return PTR_ERR(npath);
1314 + BUG_ON(npath->p_depth != path->p_depth);
1315 + eh = npath[depth].p_hdr;
1316 + if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1317 + ext_debug(inode, "next leaf isnt full(%d)\n",
1318 + le16_to_cpu(eh->eh_entries));
1322 + ext_debug(inode, "next leaf has no free space(%d,%d)\n",
1323 + le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1327 + * there is no free space in found leaf
1328 + * we're gonna add new leaf in the tree
1330 + err = ext3_ext_create_new_leaf(handle, inode, path, newext);
1333 + depth = ext_depth(inode);
1334 + eh = path[depth].p_hdr;
1337 + nearex = path[depth].p_ext;
1339 + if ((err = ext3_ext_get_access(handle, inode, path + depth)))
1343 + /* there is no extent in this leaf, create first one */
1344 + ext_debug(inode, "first extent in the leaf: %d:%d:%d\n",
1345 + le32_to_cpu(newext->ee_block),
1346 + le32_to_cpu(newext->ee_start),
1347 + le16_to_cpu(newext->ee_len));
1348 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1349 + } else if (le32_to_cpu(newext->ee_block)
1350 + > le32_to_cpu(nearex->ee_block)) {
1351 + /* BUG_ON(newext->ee_block == nearex->ee_block); */
1352 + if (nearex != EXT_LAST_EXTENT(eh)) {
1353 + len = EXT_MAX_EXTENT(eh) - nearex;
1354 + len = (len - 1) * sizeof(struct ext3_extent);
1355 + len = len < 0 ? 0 : len;
1356 + ext_debug(inode, "insert %d:%d:%d after: nearest 0x%p, "
1357 + "move %d from 0x%p to 0x%p\n",
1358 + le32_to_cpu(newext->ee_block),
1359 + le32_to_cpu(newext->ee_start),
1360 + le16_to_cpu(newext->ee_len),
1361 + nearex, len, nearex + 1, nearex + 2);
1362 + memmove(nearex + 2, nearex + 1, len);
1364 + path[depth].p_ext = nearex + 1;
1366 + BUG_ON(newext->ee_block == nearex->ee_block);
1367 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1368 + len = len < 0 ? 0 : len;
1369 + ext_debug(inode, "insert %d:%d:%d before: nearest 0x%p, "
1370 + "move %d from 0x%p to 0x%p\n",
1371 + le32_to_cpu(newext->ee_block),
1372 + le32_to_cpu(newext->ee_start),
1373 + le16_to_cpu(newext->ee_len),
1374 + nearex, len, nearex + 1, nearex + 2);
1375 + memmove(nearex + 1, nearex, len);
1376 + path[depth].p_ext = nearex;
1379 + eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1380 + nearex = path[depth].p_ext;
1381 + nearex->ee_block = newext->ee_block;
1382 + nearex->ee_start = newext->ee_start;
1383 + nearex->ee_len = newext->ee_len;
1384 + /* FIXME: support for large fs */
1385 + nearex->ee_start_hi = 0;
1388 + /* try to merge extents to the right */
1389 + while (nearex < EXT_LAST_EXTENT(eh)) {
1390 + if (!ext3_can_extents_be_merged(inode, nearex, nearex + 1))
1392 + /* merge with next extent! */
1393 + nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
1394 + + le16_to_cpu(nearex[1].ee_len));
1395 + if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1396 + len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1397 + * sizeof(struct ext3_extent);
1398 + memmove(nearex + 1, nearex + 2, len);
1400 + eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1401 + BUG_ON(eh->eh_entries == 0);
1404 + /* try to merge extents to the left */
1406 + /* time to correct all indexes above */
1407 + err = ext3_ext_correct_indexes(handle, inode, path);
1411 + err = ext3_ext_dirty(handle, inode, path + depth);
1415 + ext3_ext_drop_refs(npath);
1418 + ext3_ext_tree_changed(inode);
1419 + ext3_ext_invalidate_cache(inode);
1423 +int ext3_ext_walk_space(struct inode *inode, unsigned long block,
1424 + unsigned long num, ext_prepare_callback func,
1427 + struct ext3_ext_path *path = NULL;
1428 + struct ext3_ext_cache cbex;
1429 + struct ext3_extent *ex;
1430 + unsigned long next, start = 0, end = 0;
1431 + unsigned long last = block + num;
1432 + int depth, exists, err = 0;
1434 + BUG_ON(func == NULL);
1435 + BUG_ON(inode == NULL);
1437 + while (block < last && block != EXT_MAX_BLOCK) {
1438 + num = last - block;
1439 + /* find extent for this block */
1440 + path = ext3_ext_find_extent(inode, block, path);
1441 + if (IS_ERR(path)) {
1442 + err = PTR_ERR(path);
1447 + depth = ext_depth(inode);
1448 + BUG_ON(path[depth].p_hdr == NULL);
1449 + ex = path[depth].p_ext;
1450 + next = ext3_ext_next_allocated_block(path);
1454 + /* there is no extent yet, so try to allocate
1455 + * all requested space */
1457 + end = block + num;
1458 + } else if (le32_to_cpu(ex->ee_block) > block) {
1459 + /* need to allocate space before found extent */
1461 + end = le32_to_cpu(ex->ee_block);
1462 + if (block + num < end)
1463 + end = block + num;
1464 + } else if (block >=
1465 + le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
1466 + /* need to allocate space after found extent */
1468 + end = block + num;
1471 + } else if (block >= le32_to_cpu(ex->ee_block)) {
1473 + * some part of requested space is covered
1477 + end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
1478 + if (block + num < end)
1479 + end = block + num;
1484 + BUG_ON(end <= start);
1487 + cbex.ec_block = start;
1488 + cbex.ec_len = end - start;
1489 + cbex.ec_start = 0;
1490 + cbex.ec_type = EXT3_EXT_CACHE_GAP;
1492 + cbex.ec_block = le32_to_cpu(ex->ee_block);
1493 + cbex.ec_len = le16_to_cpu(ex->ee_len);
1494 + cbex.ec_start = le32_to_cpu(ex->ee_start);
1495 + cbex.ec_type = EXT3_EXT_CACHE_EXTENT;
1498 + BUG_ON(cbex.ec_len == 0);
1499 + err = func(inode, path, &cbex, cbdata);
1500 + ext3_ext_drop_refs(path);
1504 + if (err == EXT_REPEAT)
1506 + else if (err == EXT_BREAK) {
1511 + if (ext_depth(inode) != depth) {
1512 + /* depth was changed. we have to realloc path */
1517 + block = cbex.ec_block + cbex.ec_len;
1521 + ext3_ext_drop_refs(path);
1529 +ext3_ext_put_in_cache(struct inode *inode, __u32 block,
1530 + __u32 len, __u32 start, int type)
1532 + struct ext3_ext_cache *cex;
1534 + cex = &EXT3_I(inode)->i_cached_extent;
1535 + cex->ec_type = type;
1536 + cex->ec_block = block;
1537 + cex->ec_len = len;
1538 + cex->ec_start = start;
1542 + * this routine calculate boundaries of the gap requested block fits into
1543 + * and cache this gap
1546 +ext3_ext_put_gap_in_cache(struct inode *inode, struct ext3_ext_path *path,
1547 + unsigned long block)
1549 + int depth = ext_depth(inode);
1550 + unsigned long lblock, len;
1551 + struct ext3_extent *ex;
1553 + ex = path[depth].p_ext;
1555 + /* there is no extent yet, so gap is [0;-] */
1557 + len = EXT_MAX_BLOCK;
1558 + ext_debug(inode, "cache gap(whole file):");
1559 + } else if (block < le32_to_cpu(ex->ee_block)) {
1561 + len = le32_to_cpu(ex->ee_block) - block;
1562 + ext_debug(inode, "cache gap(before): %lu [%lu:%lu]",
1563 + (unsigned long) block,
1564 + (unsigned long) le32_to_cpu(ex->ee_block),
1565 + (unsigned long) le16_to_cpu(ex->ee_len));
1566 + } else if (block >= le32_to_cpu(ex->ee_block)
1567 + + le16_to_cpu(ex->ee_len)) {
1568 + lblock = le32_to_cpu(ex->ee_block)
1569 + + le16_to_cpu(ex->ee_len);
1570 + len = ext3_ext_next_allocated_block(path);
1571 + ext_debug(inode, "cache gap(after): [%lu:%lu] %lu",
1572 + (unsigned long) le32_to_cpu(ex->ee_block),
1573 + (unsigned long) le16_to_cpu(ex->ee_len),
1574 + (unsigned long) block);
1575 + BUG_ON(len == lblock);
1576 + len = len - lblock;
1582 + ext_debug(inode, " -> %lu:%lu\n", (unsigned long) lblock, len);
1583 + ext3_ext_put_in_cache(inode, lblock, len, 0, EXT3_EXT_CACHE_GAP);
1587 +ext3_ext_in_cache(struct inode *inode, unsigned long block,
1588 + struct ext3_extent *ex)
1590 + struct ext3_ext_cache *cex;
1592 + cex = &EXT3_I(inode)->i_cached_extent;
1594 + /* has cache valid data? */
1595 + if (cex->ec_type == EXT3_EXT_CACHE_NO)
1596 + return EXT3_EXT_CACHE_NO;
1598 + BUG_ON(cex->ec_type != EXT3_EXT_CACHE_GAP &&
1599 + cex->ec_type != EXT3_EXT_CACHE_EXTENT);
1600 + if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1601 + ex->ee_block = cpu_to_le32(cex->ec_block);
1602 + ex->ee_start = cpu_to_le32(cex->ec_start);
1603 + ex->ee_start_hi = 0;
1604 + ex->ee_len = cpu_to_le16(cex->ec_len);
1605 + ext_debug(inode, "%lu cached by %lu:%lu:%lu\n",
1606 + (unsigned long) block,
1607 + (unsigned long) cex->ec_block,
1608 + (unsigned long) cex->ec_len,
1609 + (unsigned long) cex->ec_start);
1610 + return cex->ec_type;
1613 + /* not in cache */
1614 + return EXT3_EXT_CACHE_NO;
1618 + * routine removes index from the index block
1619 + * it's used in truncate case only. thus all requests are for
1620 + * last index in the block only
1622 +int ext3_ext_rm_idx(handle_t *handle, struct inode *inode,
1623 + struct ext3_ext_path *path)
1625 + struct buffer_head *bh;
1627 + unsigned long leaf;
1629 + /* free index block */
1631 + leaf = le32_to_cpu(path->p_idx->ei_leaf);
1632 + BUG_ON(path->p_hdr->eh_entries == 0);
1633 + if ((err = ext3_ext_get_access(handle, inode, path)))
1635 + path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
1636 + if ((err = ext3_ext_dirty(handle, inode, path)))
1638 + ext_debug(inode, "index is empty, remove it, free block %lu\n", leaf);
1639 + bh = sb_find_get_block(inode->i_sb, leaf);
1640 + ext3_forget(handle, 1, inode, bh, leaf);
1641 + ext3_free_blocks(handle, inode, leaf, 1);
1646 + * This routine returns max. credits extent tree can consume.
1647 + * It should be OK for low-performance paths like ->writepage()
1648 + * To allow many writing process to fit a single transaction,
1649 + * caller should calculate credits under truncate_mutex and
1650 + * pass actual path.
1652 +int inline ext3_ext_calc_credits_for_insert(struct inode *inode,
1653 + struct ext3_ext_path *path)
1655 + int depth, needed;
1658 + /* probably there is space in leaf? */
1659 + depth = ext_depth(inode);
1660 + if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1661 + < le16_to_cpu(path[depth].p_hdr->eh_max))
1666 + * given 32bit logical block (4294967296 blocks), max. tree
1667 + * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1668 + * let's also add one more level for imbalance.
1672 + /* allocation of new data block(s) */
1676 + * tree can be full, so it'd need to grow in depth:
1677 + * we need one credit to modify old root, credits for
1678 + * new root will be added in split accounting
1683 + * Index split can happen, we'd need:
1684 + * allocate intermediate indexes (bitmap + group)
1685 + * + change two blocks at each level, but root (already included)
1687 + needed += (depth * 2) + (depth * 2);
1689 + /* any allocation modifies superblock */
1695 +static int ext3_remove_blocks(handle_t *handle, struct inode *inode,
1696 + struct ext3_extent *ex,
1697 + unsigned long from, unsigned long to)
1699 + struct buffer_head *bh;
1702 +#ifdef EXTENTS_STATS
1704 + struct ext3_sb_info *sbi = EXT3_SB(inode->i_sb);
1705 + unsigned short ee_len = le16_to_cpu(ex->ee_len);
1706 + spin_lock(&sbi->s_ext_stats_lock);
1707 + sbi->s_ext_blocks += ee_len;
1708 + sbi->s_ext_extents++;
1709 + if (ee_len < sbi->s_ext_min)
1710 + sbi->s_ext_min = ee_len;
1711 + if (ee_len > sbi->s_ext_max)
1712 + sbi->s_ext_max = ee_len;
1713 + if (ext_depth(inode) > sbi->s_depth_max)
1714 + sbi->s_depth_max = ext_depth(inode);
1715 + spin_unlock(&sbi->s_ext_stats_lock);
1718 + if (from >= le32_to_cpu(ex->ee_block)
1719 + && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1720 + /* tail removal */
1721 + unsigned long num, start;
1722 + num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
1723 + start = le32_to_cpu(ex->ee_start) + le16_to_cpu(ex->ee_len) - num;
1724 + ext_debug(inode, "free last %lu blocks starting %lu\n", num, start);
1725 + for (i = 0; i < num; i++) {
1726 + bh = sb_find_get_block(inode->i_sb, start + i);
1727 + ext3_forget(handle, 0, inode, bh, start + i);
1729 + ext3_free_blocks(handle, inode, start, num);
1730 + } else if (from == le32_to_cpu(ex->ee_block)
1731 + && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1732 + printk("strange request: removal %lu-%lu from %u:%u\n",
1733 + from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1735 + printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1736 + from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1742 +ext3_ext_rm_leaf(handle_t *handle, struct inode *inode,
1743 + struct ext3_ext_path *path, unsigned long start)
1745 + int err = 0, correct_index = 0;
1746 + int depth = ext_depth(inode), credits;
1747 + struct ext3_extent_header *eh;
1748 + unsigned a, b, block, num;
1749 + unsigned long ex_ee_block;
1750 + unsigned short ex_ee_len;
1751 + struct ext3_extent *ex;
1753 + /* the header must be checked already in ext3_ext_remove_space() */
1754 + ext_debug(inode, "truncate since %lu in leaf\n", start);
1755 + if (!path[depth].p_hdr)
1756 + path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1757 + eh = path[depth].p_hdr;
1758 + BUG_ON(eh == NULL);
1760 + /* find where to start removing */
1761 + ex = EXT_LAST_EXTENT(eh);
1763 + ex_ee_block = le32_to_cpu(ex->ee_block);
1764 + ex_ee_len = le16_to_cpu(ex->ee_len);
1766 + while (ex >= EXT_FIRST_EXTENT(eh) &&
1767 + ex_ee_block + ex_ee_len > start) {
1768 + ext_debug(inode, "remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1769 + path[depth].p_ext = ex;
1771 + a = ex_ee_block > start ? ex_ee_block : start;
1772 + b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1773 + ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1775 + ext_debug(inode, " border %u:%u\n", a, b);
1777 + if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1781 + } else if (a != ex_ee_block) {
1782 + /* remove tail of the extent */
1783 + block = ex_ee_block;
1785 + } else if (b != ex_ee_block + ex_ee_len - 1) {
1786 + /* remove head of the extent */
1789 + /* there is no "make a hole" API yet */
1792 + /* remove whole extent: excellent! */
1793 + block = ex_ee_block;
1795 + BUG_ON(a != ex_ee_block);
1796 + BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1799 + /* at present, extent can't cross block group */
1800 + /* leaf + bitmap + group desc + sb + inode */
1802 + if (ex == EXT_FIRST_EXTENT(eh)) {
1803 + correct_index = 1;
1804 + credits += (ext_depth(inode)) + 1;
1806 +#ifdef CONFIG_QUOTA
1807 + credits += 2 * EXT3_QUOTA_TRANS_BLOCKS(inode->i_sb);
1810 + handle = ext3_ext_journal_restart(handle, credits);
1811 + if (IS_ERR(handle)) {
1812 + err = PTR_ERR(handle);
1816 + err = ext3_ext_get_access(handle, inode, path + depth);
1820 + err = ext3_remove_blocks(handle, inode, ex, a, b);
1825 + /* this extent is removed entirely mark slot unused */
1826 + ex->ee_start = ex->ee_start_hi = 0;
1827 + eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1830 + ex->ee_block = cpu_to_le32(block);
1831 + ex->ee_len = cpu_to_le16(num);
1833 + err = ext3_ext_dirty(handle, inode, path + depth);
1837 + ext_debug(inode, "new extent: %u:%u:%u\n", block, num,
1838 + le32_to_cpu(ex->ee_start));
1840 + ex_ee_block = le32_to_cpu(ex->ee_block);
1841 + ex_ee_len = le16_to_cpu(ex->ee_len);
1844 + if (correct_index && eh->eh_entries)
1845 + err = ext3_ext_correct_indexes(handle, inode, path);
1847 + /* if this leaf is free, then we should
1848 + * remove it from index block above */
1849 + if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1850 + err = ext3_ext_rm_idx(handle, inode, path + depth);
1857 + * returns 1 if current index have to be freed (even partial)
1860 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1862 + BUG_ON(path->p_idx == NULL);
1864 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1868 + * if truncate on deeper level happened it it wasn't partial
1869 + * so we have to consider current index for truncation
1871 + if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1876 +int ext3_ext_remove_space(struct inode *inode, unsigned long start)
1878 + struct super_block *sb = inode->i_sb;
1879 + int depth = ext_depth(inode);
1880 + struct ext3_ext_path *path;
1882 + int i = 0, err = 0;
1884 + ext_debug(inode, "truncate since %lu\n", start);
1886 + /* probably first extent we're gonna free will be last in block */
1887 + handle = ext3_journal_start(inode, depth + 1);
1888 + if (IS_ERR(handle))
1889 + return PTR_ERR(handle);
1891 + ext3_ext_invalidate_cache(inode);
1894 + * we start scanning from right side freeing all the blocks
1895 + * after i_size and walking into the deep
1897 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1898 + if (path == NULL) {
1899 + ext3_journal_stop(handle);
1902 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1903 + path[0].p_hdr = ext_inode_hdr(inode);
1904 + if (ext3_ext_check_header(inode, path[0].p_hdr, depth)) {
1908 + path[0].p_depth = depth;
1910 + while (i >= 0 && err == 0) {
1912 + /* this is leaf block */
1913 + err = ext3_ext_rm_leaf(handle, inode, path, start);
1914 + /* root level have p_bh == NULL, brelse() eats this */
1915 + brelse(path[i].p_bh);
1916 + path[i].p_bh = NULL;
1921 + /* this is index block */
1922 + if (!path[i].p_hdr) {
1923 + ext_debug(inode, "initialize header\n");
1924 + path[i].p_hdr = ext_block_hdr(path[i].p_bh);
1927 + if (!path[i].p_idx) {
1928 + /* this level hasn't touched yet */
1929 + path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1930 + path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
1931 + ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n",
1933 + le16_to_cpu(path[i].p_hdr->eh_entries));
1935 + /* we've already was here, see at next index */
1939 + ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n",
1940 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1942 + if (ext3_ext_more_to_rm(path + i)) {
1943 + struct buffer_head *bh;
1944 + /* go to the next level */
1945 + ext_debug(inode, "move to level %d (block %d)\n",
1946 + i + 1, le32_to_cpu(path[i].p_idx->ei_leaf));
1947 + memset(path + i + 1, 0, sizeof(*path));
1948 + bh = sb_bread(sb, le32_to_cpu(path[i].p_idx->ei_leaf));
1950 + /* should we reset i_size? */
1954 + BUG_ON(i + 1 > depth);
1955 + if (ext3_ext_check_header(inode, ext_block_hdr(bh),
1960 + path[i+1].p_bh = bh;
1962 + /* put actual number of indexes to know is this
1963 + * number got changed at the next iteration */
1964 + path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
1967 + /* we finish processing this index, go up */
1968 + if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1969 + /* index is empty, remove it
1970 + * handle must be already prepared by the
1971 + * truncatei_leaf() */
1972 + err = ext3_ext_rm_idx(handle, inode, path + i);
1974 + /* root level have p_bh == NULL, brelse() eats this */
1975 + brelse(path[i].p_bh);
1976 + path[i].p_bh = NULL;
1978 + ext_debug(inode, "return to level %d\n", i);
1982 + /* TODO: flexible tree reduction should be here */
1983 + if (path->p_hdr->eh_entries == 0) {
1985 + * truncate to zero freed all the tree
1986 + * so, we need to correct eh_depth
1988 + err = ext3_ext_get_access(handle, inode, path);
1990 + ext_inode_hdr(inode)->eh_depth = 0;
1991 + ext_inode_hdr(inode)->eh_max =
1992 + cpu_to_le16(ext3_ext_space_root(inode));
1993 + err = ext3_ext_dirty(handle, inode, path);
1997 + ext3_ext_tree_changed(inode);
1998 + ext3_ext_drop_refs(path);
2000 + ext3_journal_stop(handle);
2006 + * called at mount time
2008 +void ext3_ext_init(struct super_block *sb)
2011 + * possible initialization would be here
2014 + if (test_opt(sb, EXTENTS)) {
2015 + printk("EXT3-fs: file extents enabled");
2016 +#ifdef AGRESSIVE_TEST
2017 + printk(", agressive tests");
2019 +#ifdef CHECK_BINSEARCH
2020 + printk(", check binsearch");
2022 +#ifdef EXTENTS_STATS
2023 + printk(", stats");
2026 +#ifdef EXTENTS_STATS
2027 + spin_lock_init(&EXT3_SB(sb)->s_ext_stats_lock);
2028 + EXT3_SB(sb)->s_ext_min = 1 << 30;
2029 + EXT3_SB(sb)->s_ext_max = 0;
2035 + * called at umount time
2037 +void ext3_ext_release(struct super_block *sb)
2039 + if (!test_opt(sb, EXTENTS))
2042 +#ifdef EXTENTS_STATS
2043 + if (EXT3_SB(sb)->s_ext_blocks && EXT3_SB(sb)->s_ext_extents) {
2044 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2045 + printk(KERN_ERR "EXT3-fs: %lu blocks in %lu extents (%lu ave)\n",
2046 + sbi->s_ext_blocks, sbi->s_ext_extents,
2047 + sbi->s_ext_blocks / sbi->s_ext_extents);
2048 + printk(KERN_ERR "EXT3-fs: extents: %lu min, %lu max, max depth %lu\n",
2049 + sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2054 +int ext3_ext_get_blocks(handle_t *handle, struct inode *inode, sector_t iblock,
2055 + unsigned long max_blocks, struct buffer_head *bh_result,
2056 + int create, int extend_disksize)
2058 + struct ext3_ext_path *path = NULL;
2059 + struct ext3_extent newex, *ex;
2060 + int goal, newblock, err = 0, depth;
2061 + unsigned long allocated = 0;
2062 + unsigned long next;
2064 + __clear_bit(BH_New, &bh_result->b_state);
2065 + ext_debug(inode, "blocks %d/%lu requested for inode %u\n", (int) iblock,
2066 + max_blocks, (unsigned) inode->i_ino);
2067 + mutex_lock(&EXT3_I(inode)->truncate_mutex);
2069 + /* check in cache */
2070 + if ((goal = ext3_ext_in_cache(inode, iblock, &newex))) {
2071 + if (goal == EXT3_EXT_CACHE_GAP) {
2073 + /* block isn't allocated yet and
2074 + * user don't want to allocate it */
2077 + /* we should allocate requested block */
2078 + } else if (goal == EXT3_EXT_CACHE_EXTENT) {
2079 + /* block is already allocated */
2081 + - le32_to_cpu(newex.ee_block)
2082 + + le32_to_cpu(newex.ee_start);
2083 + /* number of remain blocks in the extent */
2084 + BUG_ON(iblock < le32_to_cpu(newex.ee_block));
2085 + allocated = le16_to_cpu(newex.ee_len) -
2086 + (iblock - le32_to_cpu(newex.ee_block));
2093 + /* find extent for this block */
2094 + path = ext3_ext_find_extent(inode, iblock, NULL);
2095 + if (IS_ERR(path)) {
2096 + err = PTR_ERR(path);
2101 + depth = ext_depth(inode);
2104 + * consistent leaf must not be empty
2105 + * this situations is possible, though, _during_ tree modification
2106 + * this is why assert can't be put in ext3_ext_find_extent()
2108 + BUG_ON(path[depth].p_ext == NULL && depth != 0);
2110 + if ((ex = path[depth].p_ext)) {
2111 + unsigned long ee_block = le32_to_cpu(ex->ee_block);
2112 + unsigned long ee_start = le32_to_cpu(ex->ee_start);
2113 + unsigned short ee_len = le16_to_cpu(ex->ee_len);
2114 + /* if found exent covers block, simple return it */
2115 + if (iblock >= ee_block && iblock < ee_block + ee_len) {
2116 + newblock = iblock - ee_block + ee_start;
2117 + /* number of remain blocks in the extent */
2118 + allocated = ee_len - (iblock - ee_block);
2119 + ext_debug(inode, "%d fit into %lu:%d -> %d\n", (int) iblock,
2120 + ee_block, ee_len, newblock);
2121 + ext3_ext_put_in_cache(inode, ee_block, ee_len,
2122 + ee_start, EXT3_EXT_CACHE_EXTENT);
2128 + * requested block isn't allocated yet
2129 + * we couldn't try to create block if create flag is zero
2132 + /* put just found gap into cache to speedup subsequest reqs */
2133 + ext3_ext_put_gap_in_cache(inode, path, iblock);
2138 + * Okay, we need to do block allocation. Lazily initialize the block
2139 + * allocation info here if necessary
2141 + if (S_ISREG(inode->i_mode) && (!EXT3_I(inode)->i_block_alloc_info))
2142 + ext3_init_block_alloc_info(inode);
2144 + /* find next allocated block so that we know how many
2145 + * blocks we can allocate without ovelapping next extent */
2146 + BUG_ON(iblock < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
2147 + next = ext3_ext_next_allocated_block(path);
2148 + BUG_ON(next <= iblock);
2149 + allocated = next - iblock;
2150 + if (allocated > max_blocks)
2151 + allocated = max_blocks;
2153 + /* allocate new block */
2154 + goal = ext3_ext_find_goal(inode, path, iblock);
2155 + newblock = ext3_new_blocks(handle, inode, goal, &allocated, &err);
2158 + ext_debug(inode, "allocate new block: goal %d, found %d/%lu\n",
2159 + goal, newblock, allocated);
2161 + /* try to insert new extent into found leaf and return */
2162 + newex.ee_block = cpu_to_le32(iblock);
2163 + newex.ee_start = cpu_to_le32(newblock);
2164 + newex.ee_start_hi = 0;
2165 + newex.ee_len = cpu_to_le16(allocated);
2166 + err = ext3_ext_insert_extent(handle, inode, path, &newex);
2168 + /* free data blocks we just allocated */
2169 + ext3_free_blocks(handle, inode, le32_to_cpu(newex.ee_start),
2170 + le16_to_cpu(newex.ee_len));
2174 + if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2175 + EXT3_I(inode)->i_disksize = inode->i_size;
2177 + /* previous routine could use block we allocated */
2178 + newblock = le32_to_cpu(newex.ee_start);
2179 + __set_bit(BH_New, &bh_result->b_state);
2181 + ext3_ext_put_in_cache(inode, iblock, allocated, newblock,
2182 + EXT3_EXT_CACHE_EXTENT);
2184 + if (allocated > max_blocks)
2185 + allocated = max_blocks;
2186 + ext3_ext_show_leaf(inode, path);
2187 + __set_bit(BH_Mapped, &bh_result->b_state);
2188 + bh_result->b_bdev = inode->i_sb->s_bdev;
2189 + bh_result->b_blocknr = newblock;
2190 + bh_result->b_size = (allocated << inode->i_blkbits);
2193 + ext3_ext_drop_refs(path);
2196 + mutex_unlock(&EXT3_I(inode)->truncate_mutex);
2198 + return err ? err : allocated;
2201 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2203 + struct address_space *mapping = inode->i_mapping;
2204 + struct super_block *sb = inode->i_sb;
2205 + unsigned long last_block;
2210 + * probably first extent we're gonna free will be last in block
2212 + err = ext3_writepage_trans_blocks(inode) + 3;
2213 + handle = ext3_journal_start(inode, err);
2214 + if (IS_ERR(handle)) {
2216 + clear_highpage(page);
2217 + flush_dcache_page(page);
2218 + unlock_page(page);
2219 + page_cache_release(page);
2225 + ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2227 + mutex_lock(&EXT3_I(inode)->truncate_mutex);
2228 + ext3_ext_invalidate_cache(inode);
2231 + * TODO: optimization is possible here
2232 + * probably we need not scaning at all,
2233 + * because page truncation is enough
2235 + if (ext3_orphan_add(handle, inode))
2238 + /* we have to know where to truncate from in crash case */
2239 + EXT3_I(inode)->i_disksize = inode->i_size;
2240 + ext3_mark_inode_dirty(handle, inode);
2242 + last_block = (inode->i_size + sb->s_blocksize - 1)
2243 + >> EXT3_BLOCK_SIZE_BITS(sb);
2244 + err = ext3_ext_remove_space(inode, last_block);
2246 + /* In a multi-transaction truncate, we only make the final
2247 + * transaction synchronous */
2248 + if (IS_SYNC(inode))
2249 + handle->h_sync = 1;
2253 + * If this was a simple ftruncate(), and the file will remain alive
2254 + * then we need to clear up the orphan record which we created above.
2255 + * However, if this was a real unlink then we were called by
2256 + * ext3_delete_inode(), and we allow that function to clean up the
2257 + * orphan info for us.
2259 + if (inode->i_nlink)
2260 + ext3_orphan_del(handle, inode);
2262 + mutex_unlock(&EXT3_I(inode)->truncate_mutex);
2263 + ext3_journal_stop(handle);
2267 + * this routine calculate max number of blocks we could modify
2268 + * in order to allocate new block for an inode
2270 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2274 + needed = ext3_ext_calc_credits_for_insert(inode, NULL);
2276 + /* caller want to allocate num blocks, but note it includes sb */
2277 + needed = needed * num - (num - 1);
2279 +#ifdef CONFIG_QUOTA
2280 + needed += 2 * EXT3_QUOTA_TRANS_BLOCKS(inode->i_sb);
2286 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2287 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2288 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2289 +EXPORT_SYMBOL(ext3_ext_walk_space);
2290 +EXPORT_SYMBOL(ext3_ext_find_goal);
2291 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2292 Index: linux-2.6.18.8/fs/ext3/ialloc.c
2293 ===================================================================
2294 --- linux-2.6.18.8.orig/fs/ext3/ialloc.c 2007-07-17 09:18:09.000000000 +0200
2295 +++ linux-2.6.18.8/fs/ext3/ialloc.c 2007-07-17 11:08:09.000000000 +0200
2296 @@ -652,6 +652,17 @@ got:
2297 ext3_std_error(sb, err);
2298 goto fail_free_drop;
2300 + if (test_opt(sb, EXTENTS) && S_ISREG(inode->i_mode)) {
2301 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2302 + ext3_ext_tree_init(handle, inode);
2303 + if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2304 + err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2305 + if (err) goto fail;
2306 + EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2307 + BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2308 + err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2312 ext3_debug("allocating inode %lu\n", inode->i_ino);
2314 Index: linux-2.6.18.8/fs/ext3/inode.c
2315 ===================================================================
2316 --- linux-2.6.18.8.orig/fs/ext3/inode.c 2007-07-17 09:18:12.000000000 +0200
2317 +++ linux-2.6.18.8/fs/ext3/inode.c 2007-07-17 11:08:11.000000000 +0200
2322 -static int ext3_writepage_trans_blocks(struct inode *inode);
2325 * Test whether an inode is a fast symlink.
2327 @@ -804,6 +802,7 @@ int ext3_get_blocks_handle(handle_t *han
2328 ext3_fsblk_t first_block = 0;
2331 + J_ASSERT(!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL));
2332 J_ASSERT(handle != NULL || create == 0);
2333 depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
2335 @@ -984,12 +983,10 @@ static int ext3_get_block(struct inode *
2339 - ret = ext3_get_blocks_handle(handle, inode, iblock,
2340 + ret = ext3_get_blocks_wrap(handle, inode, iblock,
2341 max_blocks, bh_result, create, 0);
2343 - bh_result->b_size = (ret << inode->i_blkbits);
2350 @@ -1008,7 +1005,7 @@ struct buffer_head *ext3_getblk(handle_t
2352 dummy.b_blocknr = -1000;
2353 buffer_trace_init(&dummy.b_history);
2354 - err = ext3_get_blocks_handle(handle, inode, block, 1,
2355 + err = ext3_get_blocks_wrap(handle, inode, block, 1,
2358 * ext3_get_blocks_handle() returns number of blocks
2359 @@ -1759,7 +1756,7 @@ void ext3_set_aops(struct inode *inode)
2360 * This required during truncate. We need to physically zero the tail end
2361 * of that block so it doesn't yield old data if the file is later grown.
2363 -static int ext3_block_truncate_page(handle_t *handle, struct page *page,
2364 +int ext3_block_truncate_page(handle_t *handle, struct page *page,
2365 struct address_space *mapping, loff_t from)
2367 ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
2368 @@ -2263,6 +2260,9 @@ void ext3_truncate(struct inode *inode)
2372 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2373 + return ext3_ext_truncate(inode, page);
2375 handle = start_transaction(inode);
2376 if (IS_ERR(handle)) {
2378 @@ -3008,12 +3008,15 @@ err_out:
2379 * block and work out the exact number of indirects which are touched. Pah.
2382 -static int ext3_writepage_trans_blocks(struct inode *inode)
2383 +int ext3_writepage_trans_blocks(struct inode *inode)
2385 int bpp = ext3_journal_blocks_per_page(inode);
2386 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2389 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2390 + return ext3_ext_writepage_trans_blocks(inode, bpp);
2392 if (ext3_should_journal_data(inode))
2393 ret = 3 * (bpp + indirects) + 2;
2395 @@ -3260,7 +3263,7 @@ int ext3_map_inode_page(struct inode *in
2399 - rc = ext3_get_blocks_handle(handle, inode, iblock, 1, &dummy, 1, 1);
2400 + rc = ext3_get_blocks_wrap(handle, inode, iblock, 1, &dummy, 1, 1);
2402 printk(KERN_INFO "ext3_map_inode_page: error reading "
2403 "block %ld\n", iblock);
2404 Index: linux-2.6.18.8/fs/ext3/Makefile
2405 ===================================================================
2406 --- linux-2.6.18.8.orig/fs/ext3/Makefile 2007-07-17 09:18:11.000000000 +0200
2407 +++ linux-2.6.18.8/fs/ext3/Makefile 2007-07-17 11:08:11.000000000 +0200
2409 obj-$(CONFIG_EXT3_FS) += ext3.o
2411 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2412 - ioctl.o namei.o super.o symlink.o hash.o resize.o
2413 + ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o
2415 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2416 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2417 Index: linux-2.6.18.8/fs/ext3/super.c
2418 ===================================================================
2419 --- linux-2.6.18.8.orig/fs/ext3/super.c 2007-07-17 09:18:12.000000000 +0200
2420 +++ linux-2.6.18.8/fs/ext3/super.c 2007-07-17 11:08:12.000000000 +0200
2421 @@ -391,6 +391,7 @@ static void ext3_put_super (struct super
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)) {
2429 @@ -455,6 +456,8 @@ static struct inode *ext3_alloc_inode(st
2431 ei->i_block_alloc_info = NULL;
2432 ei->vfs_inode.i_version = 1;
2434 + memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
2435 return &ei->vfs_inode;
2438 @@ -680,7 +683,8 @@ enum {
2439 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
2440 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
2441 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2444 + Opt_extents, Opt_noextents, Opt_extdebug,
2447 static match_table_t tokens = {
2448 @@ -733,6 +737,9 @@ static match_table_t tokens = {
2449 {Opt_noiopen, "noiopen"},
2450 {Opt_iopen_nopriv, "iopen_nopriv"},
2451 {Opt_barrier, "barrier=%u"},
2452 + {Opt_extents, "extents"},
2453 + {Opt_noextents, "noextents"},
2454 + {Opt_extdebug, "extdebug"},
2456 {Opt_resize, "resize"},
2458 @@ -1077,6 +1084,15 @@ clear_qf_name:
2460 clear_opt(sbi->s_mount_opt, NOBH);
2463 + set_opt (sbi->s_mount_opt, EXTENTS);
2465 + case Opt_noextents:
2466 + clear_opt (sbi->s_mount_opt, EXTENTS);
2468 + case Opt_extdebug:
2469 + set_opt (sbi->s_mount_opt, EXTDEBUG);
2473 "EXT3-fs: Unrecognized mount option \"%s\" "
2474 @@ -1806,6 +1822,8 @@ static int ext3_fill_super (struct super
2475 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2478 + ext3_ext_init(sb);
2483 Index: linux-2.6.18.8/include/linux/ext3_extents.h
2484 ===================================================================
2485 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2486 +++ linux-2.6.18.8/include/linux/ext3_extents.h 2007-07-17 09:18:14.000000000 +0200
2489 + * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
2490 + * Written by Alex Tomas <alex@clusterfs.com>
2492 + * This program is free software; you can redistribute it and/or modify
2493 + * it under the terms of the GNU General Public License version 2 as
2494 + * published by the Free Software Foundation.
2496 + * This program is distributed in the hope that it will be useful,
2497 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2498 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2499 + * GNU General Public License for more details.
2501 + * You should have received a copy of the GNU General Public Licens
2502 + * along with this program; if not, write to the Free Software
2503 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
2506 +#ifndef _LINUX_EXT3_EXTENTS
2507 +#define _LINUX_EXT3_EXTENTS
2509 +#include <linux/ext3_fs.h>
2512 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2513 + * become very little, so index split, in-depth growing and
2514 + * other hard changes happens much more often
2515 + * this is for debug purposes only
2517 +#define AGRESSIVE_TEST_
2520 + * with EXTENTS_STATS defined number of blocks and extents
2521 + * are collected in truncate path. they'll be showed at
2524 +#define EXTENTS_STATS__
2527 + * if CHECK_BINSEARCH defined, then results of binary search
2528 + * will be checked by linear search
2530 +#define CHECK_BINSEARCH__
2533 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2534 + * to get lots of info what's going on
2538 +#define ext_debug(inode,fmt,a...) \
2540 + if (test_opt(inode->i_sb, EXTDEBUG)) \
2541 + printk(fmt, ##a); \
2544 +#define ext_debug(inode,fmt,a...)
2549 + * if EXT_STATS is defined then stats numbers are collected
2550 + * these number will be displayed at umount time
2555 + * define EXT3_ALLOC_NEEDED to 0 since block bitmap, group desc. and sb
2556 + * are now accounted in ext3_ext_calc_credits_for_insert()
2558 +#define EXT3_ALLOC_NEEDED 0
2561 + * ext3_inode has i_block array (60 bytes total)
2562 + * first 12 bytes store ext3_extent_header
2563 + * the remain stores array of ext3_extent
2567 + * this is extent on-disk structure
2568 + * it's used at the bottom of the tree
2570 +struct ext3_extent {
2571 + __le32 ee_block; /* first logical block extent covers */
2572 + __le16 ee_len; /* number of blocks covered by extent */
2573 + __le16 ee_start_hi; /* high 16 bits of physical block */
2574 + __le32 ee_start; /* low 32 bigs of physical block */
2578 + * this is index on-disk structure
2579 + * it's used at all the levels, but the bottom
2581 +struct ext3_extent_idx {
2582 + __le32 ei_block; /* index covers logical blocks from 'block' */
2583 + __le32 ei_leaf; /* pointer to the physical block of the next *
2584 + * level. leaf or next index could bet here */
2585 + __le16 ei_leaf_hi; /* high 16 bits of physical block */
2590 + * each block (leaves and indexes), even inode-stored has header
2592 +struct ext3_extent_header {
2593 + __le16 eh_magic; /* probably will support different formats */
2594 + __le16 eh_entries; /* number of valid entries */
2595 + __le16 eh_max; /* capacity of store in entries */
2596 + __le16 eh_depth; /* has tree real underlaying blocks? */
2597 + __le32 eh_generation; /* flags(8 bits) | generation of the tree */
2600 +#define EXT3_EXT_MAGIC 0xf30a
2603 + * array of ext3_ext_path contains path to some extent
2604 + * creation/lookup routines use it for traversal/splitting/etc
2605 + * truncate uses it to simulate recursive walking
2607 +struct ext3_ext_path {
2610 + struct ext3_extent *p_ext;
2611 + struct ext3_extent_idx *p_idx;
2612 + struct ext3_extent_header *p_hdr;
2613 + struct buffer_head *p_bh;
2617 + * structure for external API
2620 +#define EXT3_EXT_CACHE_NO 0
2621 +#define EXT3_EXT_CACHE_GAP 1
2622 +#define EXT3_EXT_CACHE_EXTENT 2
2623 +#define EXT3_EXT_HAS_NO_TREE /* ext3_extents_tree struct is not used*/
2626 + * to be called by ext3_ext_walk_space()
2627 + * negative retcode - error
2628 + * positive retcode - signal for ext3_ext_walk_space(), see below
2629 + * callback must return valid extent (passed or newly created)
2631 +typedef int (*ext_prepare_callback)(struct inode *, struct ext3_ext_path *,
2632 + struct ext3_ext_cache *,
2635 +#define EXT_CONTINUE 0
2636 +#define EXT_BREAK 1
2637 +#define EXT_REPEAT 2
2640 +#define EXT_MAX_BLOCK 0xffffffff
2642 +#define EXT_FLAGS_CLR_UNKNOWN 0x7 /* Flags cleared on modification */
2643 +#define EXT_HDR_GEN_BITS 24
2644 +#define EXT_HDR_GEN_MASK ((1 << EXT_HDR_GEN_BITS) - 1)
2646 +#define EXT_FIRST_EXTENT(__hdr__) \
2647 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
2648 + sizeof(struct ext3_extent_header)))
2649 +#define EXT_FIRST_INDEX(__hdr__) \
2650 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
2651 + sizeof(struct ext3_extent_header)))
2652 +#define EXT_HAS_FREE_INDEX(__path__) \
2653 + (le16_to_cpu((__path__)->p_hdr->eh_entries) \
2654 + < le16_to_cpu((__path__)->p_hdr->eh_max))
2655 +#define EXT_LAST_EXTENT(__hdr__) \
2656 + (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1)
2657 +#define EXT_LAST_INDEX(__hdr__) \
2658 + (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_entries) - 1)
2659 +#define EXT_MAX_EXTENT(__hdr__) \
2660 + (EXT_FIRST_EXTENT((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)
2661 +#define EXT_MAX_INDEX(__hdr__) \
2662 + (EXT_FIRST_INDEX((__hdr__)) + le16_to_cpu((__hdr__)->eh_max) - 1)
2665 +static inline struct ext3_extent_header *ext_inode_hdr(struct inode *inode)
2667 + return (struct ext3_extent_header *) EXT3_I(inode)->i_data;
2670 +static inline struct ext3_extent_header *ext_block_hdr(struct buffer_head *bh)
2672 + return (struct ext3_extent_header *) bh->b_data;
2675 +static inline unsigned short ext_depth(struct inode *inode)
2677 + return le16_to_cpu(ext_inode_hdr(inode)->eh_depth);
2680 +static inline unsigned short ext_flags(struct ext3_extent_header *neh)
2682 + return le16_to_cpu(neh->eh_generation) >> EXT_HDR_GEN_BITS;
2685 +static inline unsigned short ext_hdr_gen(struct ext3_extent_header *neh)
2687 + return le16_to_cpu(neh->eh_generation) & EXT_HDR_GEN_MASK;
2690 +static inline unsigned short ext_generation(struct inode *inode)
2692 + return ext_hdr_gen(ext_inode_hdr(inode));
2695 +static inline void ext3_ext_tree_changed(struct inode *inode)
2697 + struct ext3_extent_header *neh = ext_inode_hdr(inode);
2698 + neh->eh_generation = cpu_to_le32(
2699 + ((ext_flags(neh) & ~EXT_FLAGS_CLR_UNKNOWN) << EXT_HDR_GEN_BITS)
2700 + | ((ext_hdr_gen(neh) + 1) & EXT_HDR_GEN_MASK));
2704 +ext3_ext_invalidate_cache(struct inode *inode)
2706 + EXT3_I(inode)->i_cached_extent.ec_type = EXT3_EXT_CACHE_NO;
2709 +extern int ext3_ext_search_left(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
2710 +extern int ext3_ext_search_right(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
2711 +extern int ext3_extent_tree_init(handle_t *, struct inode *);
2712 +extern int ext3_ext_calc_credits_for_insert(struct inode *, struct ext3_ext_path *);
2713 +extern int ext3_ext_insert_extent(handle_t *, struct inode *, struct ext3_ext_path *, struct ext3_extent *);
2714 +extern int ext3_ext_walk_space(struct inode *, unsigned long, unsigned long, ext_prepare_callback, void *);
2715 +extern struct ext3_ext_path * ext3_ext_find_extent(struct inode *, int, struct ext3_ext_path *);
2717 +#endif /* _LINUX_EXT3_EXTENTS */
2719 Index: linux-2.6.18.8/include/linux/ext3_fs.h
2720 ===================================================================
2721 --- linux-2.6.18.8.orig/include/linux/ext3_fs.h 2007-07-17 09:18:13.000000000 +0200
2722 +++ linux-2.6.18.8/include/linux/ext3_fs.h 2007-07-17 11:08:12.000000000 +0200
2723 @@ -182,8 +182,9 @@ struct ext3_group_desc
2724 #define EXT3_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
2725 #define EXT3_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
2726 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
2727 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
2729 -#define EXT3_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
2730 +#define EXT3_FL_USER_VISIBLE 0x000BDFFF /* User visible flags */
2731 #define EXT3_FL_USER_MODIFIABLE 0x000380FF /* User modifiable flags */
2734 @@ -373,6 +374,8 @@ struct ext3_inode {
2735 #define EXT3_MOUNT_GRPQUOTA 0x200000 /* "old" group quota */
2736 #define EXT3_MOUNT_IOPEN 0x400000 /* Allow access via iopen */
2737 #define EXT3_MOUNT_IOPEN_NOPRIV 0x800000/* Make iopen world-readable */
2738 +#define EXT3_MOUNT_EXTENTS 0x2000000/* Extents support */
2739 +#define EXT3_MOUNT_EXTDEBUG 0x4000000/* Extents debug */
2741 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2743 @@ -572,11 +575,13 @@ static inline int ext3_valid_inum(struct
2744 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
2745 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
2746 #define EXT3_FEATURE_INCOMPAT_META_BG 0x0010
2747 +#define EXT3_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
2749 #define EXT3_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
2750 #define EXT3_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2751 EXT3_FEATURE_INCOMPAT_RECOVER| \
2752 - EXT3_FEATURE_INCOMPAT_META_BG)
2753 + EXT3_FEATURE_INCOMPAT_META_BG| \
2754 + EXT3_FEATURE_INCOMPAT_EXTENTS)
2755 #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2756 EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2757 EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2758 @@ -816,6 +821,9 @@ extern int ext3_get_inode_loc(struct ino
2759 extern void ext3_truncate (struct inode *);
2760 extern void ext3_set_inode_flags(struct inode *);
2761 extern void ext3_set_aops(struct inode *inode);
2762 +extern int ext3_writepage_trans_blocks(struct inode *);
2763 +extern int ext3_block_truncate_page(handle_t *handle, struct page *page,
2764 + struct address_space *mapping, loff_t from);
2767 extern int ext3_ioctl (struct inode *, struct file *, unsigned int,
2768 @@ -869,6 +877,30 @@ extern struct inode_operations ext3_spec
2769 extern struct inode_operations ext3_symlink_inode_operations;
2770 extern struct inode_operations ext3_fast_symlink_inode_operations;
2773 +extern int ext3_ext_tree_init(handle_t *handle, struct inode *);
2774 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2775 +extern int ext3_ext_get_blocks(handle_t *, struct inode *, sector_t,
2776 + unsigned long, struct buffer_head *, int, int);
2777 +extern void ext3_ext_truncate(struct inode *, struct page *);
2778 +extern void ext3_ext_init(struct super_block *);
2779 +extern void ext3_ext_release(struct super_block *);
2781 +ext3_get_blocks_wrap(handle_t *handle, struct inode *inode, sector_t block,
2782 + unsigned long max_blocks, struct buffer_head *bh,
2783 + int create, int extend_disksize)
2786 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2787 + return ext3_ext_get_blocks(handle, inode, block, max_blocks,
2788 + bh, create, extend_disksize);
2789 + ret = ext3_get_blocks_handle(handle, inode, block, max_blocks, bh, create,
2792 + bh->b_size = (ret << inode->i_blkbits);
2797 #endif /* __KERNEL__ */
2799 Index: linux-2.6.18.8/include/linux/ext3_fs_i.h
2800 ===================================================================
2801 --- linux-2.6.18.8.orig/include/linux/ext3_fs_i.h 2007-02-24 00:52:30.000000000 +0100
2802 +++ linux-2.6.18.8/include/linux/ext3_fs_i.h 2007-07-17 11:08:11.000000000 +0200
2803 @@ -65,6 +65,16 @@ struct ext3_block_alloc_info {
2804 #define rsv_end rsv_window._rsv_end
2807 + * storage for cached extent
2809 +struct ext3_ext_cache {
2812 + __u32 ec_len; /* must be 32bit to return holes */
2817 * third extended file system inode data in memory
2819 struct ext3_inode_info {
2820 @@ -142,6 +152,8 @@ struct ext3_inode_info {
2822 struct mutex truncate_mutex;
2823 struct inode vfs_inode;
2825 + struct ext3_ext_cache i_cached_extent;
2828 #endif /* _LINUX_EXT3_FS_I */
2829 Index: linux-2.6.18.8/include/linux/ext3_fs_sb.h
2830 ===================================================================
2831 --- linux-2.6.18.8.orig/include/linux/ext3_fs_sb.h 2007-02-24 00:52:30.000000000 +0100
2832 +++ linux-2.6.18.8/include/linux/ext3_fs_sb.h 2007-07-17 11:08:12.000000000 +0200
2833 @@ -78,6 +78,16 @@ struct ext3_sb_info {
2834 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
2835 int s_jquota_fmt; /* Format of quota to use */
2838 +#ifdef EXTENTS_STATS
2839 + /* ext3 extents stats */
2840 + unsigned long s_ext_min;
2841 + unsigned long s_ext_max;
2842 + unsigned long s_depth_max;
2843 + spinlock_t s_ext_stats_lock;
2844 + unsigned long s_ext_blocks;
2845 + unsigned long s_ext_extents;
2849 #endif /* _LINUX_EXT3_FS_SB */
2850 Index: linux-2.6.18.8/include/linux/ext3_jbd.h
2851 ===================================================================
2852 --- linux-2.6.18.8.orig/include/linux/ext3_jbd.h 2007-02-24 00:52:30.000000000 +0100
2853 +++ linux-2.6.18.8/include/linux/ext3_jbd.h 2007-07-17 09:18:14.000000000 +0200
2856 /* Define the number of blocks we need to account to a transaction to
2857 * modify one block of data.
2860 * We may have to touch one inode, one bitmap buffer, up to three
2861 * indirection blocks, the group and superblock summaries, and the data
2862 - * block to complete the transaction. */
2863 + * block to complete the transaction.
2865 + * For extents-enabled fs we may have to allocate and modify upto
2866 + * 5 levels of tree + root which is stored in inode. */
2868 +#define EXT3_SINGLEDATA_TRANS_BLOCKS(sb) \
2869 + (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS) \
2870 + || test_opt(sb, EXTENTS) ? 27U : 8U)
2872 -#define EXT3_SINGLEDATA_TRANS_BLOCKS 8U
2873 +/* Indicate that EXT3_SINGLEDATA_TRANS_BLOCKS takes the sb as argument */
2874 +#define EXT3_SINGLEDATA_TRANS_BLOCKS_HAS_SB
2876 /* Extended attribute operations touch at most two data buffers,
2877 * two bitmap buffers, and two group summaries, in addition to the inode
2879 * superblock only gets updated once, of course, so don't bother
2880 * counting that again for the quota updates. */
2882 -#define EXT3_DATA_TRANS_BLOCKS(sb) (EXT3_SINGLEDATA_TRANS_BLOCKS + \
2883 +#define EXT3_DATA_TRANS_BLOCKS(sb) (EXT3_SINGLEDATA_TRANS_BLOCKS(sb) + \
2884 EXT3_XATTR_TRANS_BLOCKS - 2 + \
2885 2*EXT3_QUOTA_TRANS_BLOCKS(sb))
2888 /* Amount of blocks needed for quota insert/delete - we do some block writes
2889 * but inode, sb and group updates are done only once */
2890 #define EXT3_QUOTA_INIT_BLOCKS(sb) (test_opt(sb, QUOTA) ? (DQUOT_INIT_ALLOC*\
2891 - (EXT3_SINGLEDATA_TRANS_BLOCKS-3)+3+DQUOT_INIT_REWRITE) : 0)
2892 + (EXT3_SINGLEDATA_TRANS_BLOCKS(sb)-3)+3+DQUOT_INIT_REWRITE) : 0)
2893 #define EXT3_QUOTA_DEL_BLOCKS(sb) (test_opt(sb, QUOTA) ? (DQUOT_DEL_ALLOC*\
2894 - (EXT3_SINGLEDATA_TRANS_BLOCKS-3)+3+DQUOT_DEL_REWRITE) : 0)
2895 + (EXT3_SINGLEDATA_TRANS_BLOCKS(sb)-3)+3+DQUOT_DEL_REWRITE) : 0)
2897 #define EXT3_QUOTA_TRANS_BLOCKS(sb) 0
2898 #define EXT3_QUOTA_INIT_BLOCKS(sb) 0