2 fs/ext3/extents.c | 1615 +++++++++++++++++++++++++++++++++++++++++++++
6 include/linux/ext3_fs.h | 18
7 include/linux/ext3_fs_i.h | 4
8 include/linux/ext3_fs_sb.h | 10
9 8 files changed, 1684 insertions(+), 8 deletions(-)
11 Index: linux-2.4.18-chaos/fs/ext3/extents.c
12 ===================================================================
13 --- linux-2.4.18-chaos.orig/fs/ext3/extents.c 2003-01-30 13:24:37.000000000 +0300
14 +++ linux-2.4.18-chaos/fs/ext3/extents.c 2004-01-13 16:11:00.000000000 +0300
18 + * linux/fs/ext3/extents.c
20 + * Extents support for EXT3
22 + * 07/08/2003 Alex Tomas <bzzz@tmi.comex.ru>
25 + * - ext3*_error() should be used in some situations
26 + * - find_goal() [to be tested and improved]
28 + * - we could leak allocated block in some error cases
29 + * - quick search for index/leaf in ext3_ext_find_extent()
31 + * - cache last found extent
32 + * - arch-independent
35 +#include <linux/module.h>
36 +#include <linux/fs.h>
37 +#include <linux/time.h>
38 +#include <linux/ext3_jbd.h>
39 +#include <linux/jbd.h>
40 +#include <linux/smp_lock.h>
41 +#include <linux/highuid.h>
42 +#include <linux/pagemap.h>
43 +#include <linux/quotaops.h>
44 +#include <linux/string.h>
45 +#include <linux/slab.h>
46 +#include <linux/locks.h>
49 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
50 + * become very little, so index split, in-depth growing and
51 + * other hard changes happens much more often
52 + * this is for debug purposes only
54 +#define AGRESSIVE_TEST_
57 + * if EXT_DEBUG defined you can use 'extdebug' mount option
58 + * to get lots of info what's going on
62 +#define ext_debug(inode,fmt,a...) \
64 + if (test_opt((inode)->i_sb, EXTDEBUG)) \
68 +#define ext_debug(inode,fmt,a...)
71 +#define EXT3_ALLOC_NEEDED 2 /* block bitmap + group descriptor */
74 + * ext3_inode has i_block array (total 60 bytes)
75 + * first 4 bytes are used to store:
76 + * - tree depth (0 mean there is no tree yet. all extents in the inode)
77 + * - number of alive extents in the inode
81 + * this is extent on-disk structure
82 + * it's used at the bottom of the tree
85 + __u32 e_block; /* first logical block extent covers */
86 + __u32 e_start; /* first physical block extents lives */
87 + __u32 e_num; /* number of blocks covered by extent */
91 + * this is index on-disk structure
92 + * it's used at all the levels, but the bottom
94 +struct ext3_extent_idx {
95 + __u32 e_block; /* index covers logical blocks from 'block' */
96 + __u32 e_leaf; /* pointer to the physical block of the next *
97 + * level. leaf or next index could bet here */
101 + * each block (leaves and indexes), even inode-stored has header
103 +struct ext3_extent_header {
104 + __u16 e_num; /* number of valid entries */
105 + __u16 e_max; /* capacity of store in entries */
109 + * array of ext3_ext_path contains path to some extent
110 + * creation/lookup routines use it for traversal/splitting/etc
111 + * truncate uses it to simulate recursive walking
113 +struct ext3_ext_path {
116 + struct ext3_extent *p_ext;
117 + struct ext3_extent_idx *p_idx;
118 + struct ext3_extent_header *p_hdr;
119 + struct buffer_head *p_bh;
122 +#define EXT_FIRST_EXTENT(__hdr__) \
123 + ((struct ext3_extent *) (((char *) (__hdr__)) + \
124 + sizeof(struct ext3_extent_header)))
125 +#define EXT_FIRST_INDEX(__hdr__) \
126 + ((struct ext3_extent_idx *) (((char *) (__hdr__)) + \
127 + sizeof(struct ext3_extent_header)))
128 +#define EXT_HAS_FREE_INDEX(__path__) \
129 + ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
130 +#define EXT_LAST_EXTENT(__hdr__) \
131 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
132 +#define EXT_LAST_INDEX(__hdr__) \
133 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
134 +#define EXT_MAX_EXTENT(__hdr__) \
135 + (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
136 +#define EXT_MAX_INDEX(__hdr__) \
137 + (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
140 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
147 +static int ext3_ext_get_access(handle_t *handle, struct inode *inode,
148 + struct ext3_ext_path *path)
151 + /* path points to block */
152 + return ext3_journal_get_write_access(handle, path->p_bh);
155 + /* path points to leaf/index in inode body */
165 +static int ext3_ext_dirty(handle_t *handle, struct inode *inode,
166 + struct ext3_ext_path *path)
169 + /* path points to block */
170 + return ext3_journal_dirty_metadata(handle, path->p_bh);
173 + /* path points to leaf/index in inode body */
174 + return ext3_mark_inode_dirty(handle, inode);
177 +static inline int ext3_ext_space_block(struct inode *inode)
181 + size = (inode->i_sb->s_blocksize - sizeof(struct ext3_extent_header))
182 + / sizeof(struct ext3_extent);
183 +#ifdef AGRESSIVE_TEST
184 + size = 6; /* FIXME: for debug, remove this line */
189 +static inline int ext3_ext_space_inode(struct inode *inode)
193 + size = (sizeof(EXT3_I(inode)->i_data) -
194 + sizeof(struct ext3_extent_header))
195 + / sizeof(struct ext3_extent);
196 +#ifdef AGRESSIVE_TEST
197 + size = 3; /* FIXME: for debug, remove this line */
202 +static inline int ext3_ext_space_inode_idx(struct inode *inode)
206 + size = (sizeof(EXT3_I(inode)->i_data) -
207 + sizeof(struct ext3_extent_header))
208 + / sizeof(struct ext3_extent_idx);
209 +#ifdef AGRESSIVE_TEST
210 + size = 4; /* FIXME: for debug, remove this line */
215 +static void ext3_ext_show_path(struct inode *inode, struct ext3_ext_path *path)
217 + int k, l = path->p_depth;
219 + ext_debug(inode, "path:");
220 + for (k = 0; k <= l; k++, path++) {
222 + ext_debug(inode, " %d->%d", path->p_idx->e_block,
223 + path->p_idx->e_leaf);
224 + } else if (path->p_ext) {
225 + ext_debug(inode, " %d:%d:%d",
226 + path->p_ext->e_block,
227 + path->p_ext->e_start,
228 + path->p_ext->e_num);
230 + ext_debug(inode, " []");
232 + ext_debug(inode, "\n");
235 +static void ext3_ext_show_leaf(struct inode *inode, struct ext3_ext_path *path)
237 + int depth = EXT3_I(inode)->i_depth;
238 + struct ext3_extent_header *eh = path[depth].p_hdr;
239 + struct ext3_extent *ex = EXT_FIRST_EXTENT(eh);
242 + for (i = 0; i < eh->e_num; i++, ex++) {
243 + ext_debug(inode, "%d:%d:%d ",
244 + ex->e_block, ex->e_start, ex->e_num);
246 + ext_debug(inode, "\n");
249 +static void ext3_ext_drop_refs(struct inode *inode, struct ext3_ext_path *path)
251 + int depth = path->p_depth;
254 + for (i = 0; i <= depth; i++, path++)
256 + brelse(path->p_bh);
261 +static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path)
263 + struct ext3_inode_info *ei = EXT3_I(inode);
264 + unsigned long bg_start;
265 + unsigned long colour;
269 + depth = path->p_depth;
270 + /* try to find previous block */
271 + if (path[depth].p_ext)
272 + return path[depth].p_ext->e_start +
273 + path[depth].p_ext->e_num - 1;
275 + /* it looks index is empty
276 + * try to find starting from index itself */
277 + if (path[depth].p_bh)
278 + return path[depth].p_bh->b_blocknr;
281 + /* OK. use inode's group */
282 + bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
283 + le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
284 + colour = (current->pid % 16) *
285 + (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
286 + return bg_start + colour;
289 +static struct ext3_ext_path *
290 +ext3_ext_find_extent(struct inode *inode, int block, struct ext3_ext_path *path)
292 + struct ext3_inode_info *ei = EXT3_I(inode);
293 + struct ext3_extent_header *eh = (void *) ei->i_data;
294 + struct ext3_extent_idx *ix;
295 + struct buffer_head *bh;
296 + struct ext3_extent *ex;
297 + int depth, i, k, ppos = 0, prev = 0;
299 + eh = (struct ext3_extent_header *) ei->i_data;
301 + /* initialize capacity of leaf in inode for first time */
302 + if (eh->e_max == 0)
303 + eh->e_max = ext3_ext_space_inode(inode);
304 + i = depth = ei->i_depth;
305 + EXT_ASSERT(i == 0 || eh->e_num > 0);
307 + /* account possible depth increase */
309 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
312 + return ERR_PTR(-ENOMEM);
314 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
316 + /* walk through the tree */
318 + ext_debug(inode, "depth %d: num %d, max %d\n",
319 + ppos, eh->e_num, eh->e_max);
320 + ix = EXT_FIRST_INDEX(eh);
322 + EXT_ASSERT(prev == 0 || ix->e_block == prev);
323 + path[ppos].p_idx = ix;
325 + EXT_ASSERT(eh->e_num <= eh->e_max);
326 + for (k = 0; k < eh->e_num; k++, ix++) {
327 + ext_debug(inode, "index: %d -> %d\n",
328 + ix->e_block, ix->e_leaf);
329 + EXT_ASSERT((k == 0 && prev <= (int)ix->e_block) ||
330 + (k > 0 && prev < (int)ix->e_block));
331 + if (block < ix->e_block)
333 + prev = ix->e_block;
334 + path[ppos].p_idx = ix;
336 + path[ppos].p_block = path[ppos].p_idx->e_leaf;
337 + path[ppos].p_depth = i;
338 + path[ppos].p_hdr = eh;
339 + path[ppos].p_ext = NULL;
341 + bh = sb_bread(inode->i_sb, path[ppos].p_block);
343 + ext3_ext_drop_refs(inode, path);
345 + return ERR_PTR(-EIO);
347 + eh = (struct ext3_extent_header *) bh->b_data;
349 + EXT_ASSERT(ppos <= depth);
350 + path[ppos].p_bh = bh;
354 + path[ppos].p_depth = i;
355 + path[ppos].p_hdr = eh;
356 + path[ppos].p_ext = NULL;
359 + ex = EXT_FIRST_EXTENT(eh);
361 + path[ppos].p_ext = ex;
362 + EXT_ASSERT(eh->e_num <= eh->e_max);
363 + for (k = 0; k < eh->e_num; k++, ex++) {
364 + EXT_ASSERT(ex->e_num < EXT3_BLOCKS_PER_GROUP(inode->i_sb));
365 + EXT_ASSERT((k == 0 && prev <= (int)ex->e_block) ||
366 + (k > 0 && prev < (int)ex->e_block));
367 + if (block < ex->e_block)
369 + prev = ex->e_block;
370 + path[ppos].p_ext = ex;
373 + ext3_ext_show_path(inode, path);
378 +static void ext3_ext_check_boundary(struct inode *inode,
379 + struct ext3_ext_path *curp,
380 + void *addr, int len)
387 + end = (void *) curp->p_hdr + inode->i_sb->s_blocksize;
389 + end = (void *) curp->p_hdr + sizeof(EXT3_I(inode)->i_data);
390 + if (((unsigned long) addr) + len > (unsigned long) end) {
391 + printk("overflow! 0x%p > 0x%p\n", addr + len, end);
394 + if ((unsigned long) addr < (unsigned long) curp->p_hdr) {
395 + printk("underflow! 0x%p < 0x%p\n", addr, curp->p_hdr);
401 + * insert new index [logical;ptr] into the block at cupr
402 + * it check where to insert: before curp or after curp
404 +static int ext3_ext_insert_index(handle_t *handle, struct inode *inode,
405 + struct ext3_ext_path *curp, int logical,
408 + struct ext3_extent_idx *ix;
411 + if ((err = ext3_ext_get_access(handle, inode, curp)))
414 + EXT_ASSERT(logical != curp->p_idx->e_block);
415 + len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
416 + if (logical > curp->p_idx->e_block) {
418 + len = (len - 1) * sizeof(struct ext3_extent_idx);
419 + len = len < 0 ? 0 : len;
420 + ext_debug(inode, "insert new index %d after: %d. "
421 + "move %d from 0x%p to 0x%p\n",
423 + (curp->p_idx + 1), (curp->p_idx + 2));
425 + ext3_ext_check_boundary(inode, curp, curp->p_idx + 2, len);
426 + memmove(curp->p_idx + 2, curp->p_idx + 1, len);
427 + ix = curp->p_idx + 1;
429 + /* insert before */
430 + len = len * sizeof(struct ext3_extent_idx);
431 + len = len < 0 ? 0 : len;
432 + ext_debug(inode, "insert new index %d before: %d. "
433 + "move %d from 0x%p to 0x%p\n",
435 + curp->p_idx, (curp->p_idx + 1));
437 + ext3_ext_check_boundary(inode, curp, curp->p_idx + 1, len);
438 + memmove(curp->p_idx + 1, curp->p_idx, len);
442 + ix->e_block = logical;
444 + curp->p_hdr->e_num++;
446 + err = ext3_ext_dirty(handle, inode, curp);
447 + ext3_std_error(inode->i_sb, err);
453 + * routine inserts new subtree into the path, using free index entry
455 + * - allocates all needed blocks (new leaf and all intermediate index blocks)
456 + * - makes decision where to split
457 + * - moves remaining extens and index entries (right to the split point)
458 + * into the newly allocated blocks
459 + * - initialize subtree
461 +static int ext3_ext_split(handle_t *handle, struct inode *inode,
462 + struct ext3_ext_path *path,
463 + struct ext3_extent *newext, int at)
465 + struct buffer_head *bh = NULL;
466 + int depth = EXT3_I(inode)->i_depth;
467 + struct ext3_extent_header *neh;
468 + struct ext3_extent_idx *fidx;
469 + struct ext3_extent *ex;
470 + int i = at, k, m, a;
471 + long newblock, oldblock, border;
472 + int *ablocks = NULL; /* array of allocated blocks */
475 + /* make decision: where to split? */
476 + /* FIXME: now desicion is simplest: at current extent */
478 + /* if current leaf will be splitted, then we should use
479 + * border from split point */
481 + if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
482 + border = path[depth].p_ext[1].e_block;
483 + ext_debug(inode, "leaf will be splitted."
484 + " next leaf starts at %d\n",
487 + border = newext->e_block;
488 + ext_debug(inode, "leaf will be added."
489 + " next leaf starts at %d\n",
494 + * if error occurs, then we break processing
495 + * and turn filesystem read-only. so, index won't
496 + * be inserted and tree will be in consistent
497 + * state. next mount will repair buffers too
501 + * get array to track all allocated blocks
502 + * we need this to handle errors and free blocks
505 + ablocks = kmalloc(sizeof(long) * depth, GFP_NOFS);
508 + memset(ablocks, 0, sizeof(long) * depth);
510 + /* allocate all needed blocks */
511 + ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at);
512 + newblock = 0; /* FIXME: something more sophisticated needed here */
513 + for (a = 0; newext->e_num > 0 && a < depth - at; a++) {
514 + newblock = ablocks[a] = newext->e_start++;
517 + for (; a < depth - at; a++) {
518 + newblock = ext3_new_block(handle, inode,
519 + newblock + 1, 0, 0, &err);
522 + ablocks[a] = newblock;
525 + /* initialize new leaf */
526 + newblock = ablocks[--a];
527 + EXT_ASSERT(newblock);
528 + bh = sb_getblk(inode->i_sb, newblock);
535 + if ((err = ext3_journal_get_create_access(handle, bh)))
538 + neh = (struct ext3_extent_header *) bh->b_data;
540 + neh->e_max = ext3_ext_space_block(inode);
541 + ex = EXT_FIRST_EXTENT(neh);
543 + /* move remain of path[depth] to the new leaf */
544 + EXT_ASSERT(path[depth].p_hdr->e_num ==
545 + path[depth].p_hdr->e_max);
546 + /* start copy from next extent */
547 + /* TODO: we could do it by single memmove */
549 + path[depth].p_ext++;
550 + while (path[depth].p_ext <=
551 + EXT_MAX_EXTENT(path[depth].p_hdr)) {
552 + ext_debug(inode, "move %d:%d:%d in new leaf\n",
553 + path[depth].p_ext->e_block,
554 + path[depth].p_ext->e_start,
555 + path[depth].p_ext->e_num);
556 + memmove(ex++, path[depth].p_ext++,
557 + sizeof(struct ext3_extent));
561 + mark_buffer_uptodate(bh, 1);
564 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
569 + /* correct old leaf */
571 + if ((err = ext3_ext_get_access(handle, inode, path)))
573 + path[depth].p_hdr->e_num -= m;
574 + if ((err = ext3_ext_dirty(handle, inode, path)))
579 + /* create intermediate indexes */
580 + k = depth - at - 1;
581 + EXT_ASSERT(k >= 0);
584 + "create %d intermediate indices\n", k);
585 + /* insert new index into current index block */
586 + /* current depth stored in i var */
589 + oldblock = newblock;
590 + newblock = ablocks[--a];
591 + bh = sb_getblk(inode->i_sb, newblock);
598 + if ((err = ext3_journal_get_create_access(handle, bh)))
601 + neh = (struct ext3_extent_header *) bh->b_data;
603 + neh->e_max = ext3_ext_space_block(inode);
604 + fidx = EXT_FIRST_INDEX(neh);
605 + fidx->e_block = border;
606 + fidx->e_leaf = oldblock;
609 + "int.index at %d (block %u): %d -> %d\n",
610 + i, (unsigned) newblock,
616 + ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx,
617 + EXT_MAX_INDEX(path[i].p_hdr));
618 + EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
619 + EXT_LAST_INDEX(path[i].p_hdr));
620 + while (path[i].p_idx <=
621 + EXT_MAX_INDEX(path[i].p_hdr)) {
622 + ext_debug(inode, "%d: move %d:%d in new index\n",
623 + i, path[i].p_idx->e_block,
624 + path[i].p_idx->e_leaf);
625 + memmove(++fidx, path[i].p_idx++,
626 + sizeof(struct ext3_extent_idx));
631 + mark_buffer_uptodate(bh, 1);
634 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
639 + /* correct old index */
641 + err = ext3_ext_get_access(handle,inode,path+i);
644 + path[i].p_hdr->e_num -= m;
645 + err = ext3_ext_dirty(handle, inode, path + i);
653 + /* insert new index */
655 + err = ext3_ext_insert_index(handle, inode, path + at,
660 + if (buffer_locked(bh))
666 + /* free all allocated blocks in error case */
667 + for (i = 0; i < depth; i++)
670 + ext3_free_blocks(handle, inode, ablocks[i], 1);
678 + * routine implements tree growing procedure:
679 + * - allocates new block
680 + * - moves top-level data (index block or leaf) into the new block
681 + * - initialize new top-level, creating index that points to the
682 + * just created block
684 +static int ext3_ext_grow_indepth(handle_t *handle, struct inode *inode,
685 + struct ext3_ext_path *path,
686 + struct ext3_extent *newext)
688 + struct buffer_head *bh;
689 + struct ext3_ext_path *curp = path;
690 + struct ext3_extent_header *neh;
691 + struct ext3_extent_idx *fidx;
696 + * use already allocated by the called block for new root block
698 + newblock = newext->e_start++;
699 + if (newext->e_num == 0) {
701 + * FIXME: if this may happen, then we have to handle
702 + * possible error and free allocated block
704 + printk("grow_indepth with zero blocks\n");
705 + newblock = ext3_new_block(handle, inode,
706 + newblock, 0, 0, &err);
710 + bh = sb_getblk(inode->i_sb, newblock);
713 + ext3_std_error(inode->i_sb, err);
718 + if ((err = ext3_journal_get_create_access(handle, bh))) {
723 + /* move top-level index/leaf into new block */
724 + len = sizeof(struct ext3_extent_header) +
725 + sizeof(struct ext3_extent) * curp->p_hdr->e_max;
726 + EXT_ASSERT(len >= 0 && len < 4096);
727 + memmove(bh->b_data, curp->p_hdr, len);
729 + /* set size of new block */
730 + neh = (struct ext3_extent_header *) bh->b_data;
731 + neh->e_max = ext3_ext_space_block(inode);
732 + mark_buffer_uptodate(bh, 1);
735 + if ((err = ext3_journal_dirty_metadata(handle, bh)))
738 + /* create index in new top-level index: num,max,pointer */
739 + if ((err = ext3_ext_get_access(handle, inode, curp)))
742 + curp->p_hdr->e_max = ext3_ext_space_inode_idx(inode);
743 + curp->p_hdr->e_num = 1;
744 + curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
745 + curp->p_idx->e_block = EXT_FIRST_EXTENT(path[0].p_hdr)->e_block;
746 + curp->p_idx->e_leaf = newblock;
748 + neh = (struct ext3_extent_header *) EXT3_I(inode)->i_data;
749 + fidx = EXT_FIRST_INDEX(neh);
750 + ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %d\n",
751 + neh->e_num, neh->e_max, fidx->e_block, fidx->e_leaf);
753 + EXT3_I(inode)->i_depth++;
754 + err = ext3_ext_dirty(handle, inode, curp);
762 + * routine finds empty index and adds new leaf. if no free index found
763 + * then it requests in-depth growing
765 +static int ext3_ext_create_new_leaf(handle_t *handle, struct inode *inode,
766 + struct ext3_ext_path *path,
767 + struct ext3_extent *newext)
769 + long newblock = newext->e_start;
770 + struct ext3_ext_path *curp;
771 + int depth, i, err = 0;
774 + i = depth = EXT3_I(inode)->i_depth;
776 + /* walk up to the tree and look for free index entry */
777 + curp = path + depth;
778 + while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
783 + /* we use already allocated block for index block
784 + * so, subsequent data blocks should be contigoues */
785 + if (EXT_HAS_FREE_INDEX(curp)) {
786 + /* if we found index with free entry, then use that
787 + * entry: create all needed subtree and add new leaf */
788 + err = ext3_ext_split(handle, inode, path, newext, i);
791 + ext3_ext_drop_refs(inode, path);
792 + path = ext3_ext_find_extent(inode, newext->e_block, path);
794 + err = PTR_ERR(path);
796 + /* tree is full, time to grow in depth */
797 + err = ext3_ext_grow_indepth(handle, inode, path, newext);
800 + ext3_ext_drop_refs(inode, path);
801 + path = ext3_ext_find_extent(inode, newext->e_block, path);
803 + err = PTR_ERR(path);
806 + * only first (depth 0 -> 1) produces free space
807 + * in all other cases we have to split growed tree
809 + depth = EXT3_I(inode)->i_depth;
810 + if (path[depth].p_hdr->e_num == path[depth].p_hdr->e_max) {
811 + /* now we need split */
820 + * probably we've used some blocks from extent
821 + * let's allocate new block for it
823 + if (newext->e_num == 0 && !err) {
825 + ext3_new_block(handle, inode, newblock,
827 + if (newext->e_start != 0)
835 + * returns next allocated block or 0xffffffff
836 + * NOTE: it consider block number from index entry as
837 + * allocated block. thus, index entries have to be consistent
840 +static inline unsigned ext3_ext_next_allocated_block(struct inode *inode,
841 + struct ext3_ext_path *path)
845 + EXT_ASSERT(path != NULL);
846 + depth = path->p_depth;
848 + if (depth == 0 && path->p_ext == NULL)
851 + /* FIXME: what if index isn't full ?! */
852 + while (depth >= 0) {
853 + if (depth == path->p_depth) {
855 + if (path[depth].p_ext !=
856 + EXT_LAST_EXTENT(path[depth].p_hdr))
857 + return path[depth].p_ext[1].e_block;
860 + if (path[depth].p_idx !=
861 + EXT_LAST_INDEX(path[depth].p_hdr))
862 + return path[depth].p_idx[1].e_block;
871 + * returns first allocated block from next leaf or 0xffffffff
873 +static unsigned ext3_ext_next_leaf_block(struct inode *inode,
874 + struct ext3_ext_path *path)
878 + EXT_ASSERT(path != NULL);
879 + depth = path->p_depth;
881 + /* zero-tree has no leaf blocks at all */
885 + /* go to index block */
888 + while (depth >= 0) {
889 + if (path[depth].p_idx !=
890 + EXT_LAST_INDEX(path[depth].p_hdr))
891 + return path[depth].p_idx[1].e_block;
899 + * if leaf gets modified and modified extent is first in the leaf
900 + * then we have to correct all indexes above
901 + * TODO: do we need to correct tree in all cases?
903 +int ext3_ext_correct_indexes(handle_t *handle, struct inode *inode,
904 + struct ext3_ext_path *path)
906 + int depth = EXT3_I(inode)->i_depth;
907 + struct ext3_extent_header *eh;
908 + struct ext3_extent *ex;
912 + eh = path[depth].p_hdr;
913 + ex = path[depth].p_ext;
919 + /* there is no tree at all */
923 + if (ex != EXT_FIRST_EXTENT(eh)) {
924 + /* we correct tree if first leaf got modified only */
929 + * TODO: we need correction if border is smaller then current one
932 + border = path[depth].p_ext->e_block;
933 + if ((err = ext3_ext_get_access(handle, inode, path + k)))
935 + path[k].p_idx->e_block = border;
936 + if ((err = ext3_ext_dirty(handle, inode, path + k)))
940 + /* change all left-side indexes */
941 + if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
943 + if ((err = ext3_ext_get_access(handle, inode, path + k)))
945 + path[k].p_idx->e_block = border;
946 + if ((err = ext3_ext_dirty(handle, inode, path + k)))
954 + * this routine tries to merge requsted extent into the existing
955 + * extent or inserts requested extent as new one into the tree,
956 + * creating new leaf in no-space case
958 +int ext3_ext_insert_extent(handle_t *handle, struct inode *inode,
959 + struct ext3_ext_path *path,
960 + struct ext3_extent *newext)
963 + struct ext3_extent_header * eh;
964 + struct ext3_extent *ex;
965 + struct ext3_extent *nearex; /* nearest extent */
966 + struct ext3_ext_path *npath = NULL;
969 + depth = EXT3_I(inode)->i_depth;
970 + if ((ex = path[depth].p_ext)) {
971 + /* try to insert block into found extent and return */
972 + if (ex->e_block + ex->e_num == newext->e_block &&
973 + ex->e_start + ex->e_num == newext->e_start) {
974 +#ifdef AGRESSIVE_TEST
975 + if (ex->e_num >= 2)
978 + if ((err = ext3_ext_get_access(handle, inode,
981 + ext_debug(inode, "append %d block to %d:%d (from %d)\n",
982 + newext->e_num, ex->e_block, ex->e_num,
984 + ex->e_num += newext->e_num;
985 + err = ext3_ext_dirty(handle, inode, path + depth);
991 + depth = EXT3_I(inode)->i_depth;
992 + eh = path[depth].p_hdr;
993 + if (eh->e_num == eh->e_max) {
994 + /* probably next leaf has space for us? */
995 + int next = ext3_ext_next_leaf_block(inode, path);
996 + if (next != 0xffffffff) {
997 + ext_debug(inode, "next leaf block - %d\n", next);
998 + EXT_ASSERT(!npath);
999 + npath = ext3_ext_find_extent(inode, next, NULL);
1000 + if (IS_ERR(npath))
1001 + return PTR_ERR(npath);
1002 + EXT_ASSERT(npath->p_depth == path->p_depth);
1003 + eh = npath[depth].p_hdr;
1004 + if (eh->e_num < eh->e_max) {
1006 + "next leaf has free ext(%d)\n",
1011 + ext_debug(inode, "next leaf hasno free space(%d,%d)\n",
1012 + eh->e_num, eh->e_max);
1015 + * there is no free space in found leaf
1016 + * we're gonna add new leaf in the tree
1018 + err = ext3_ext_create_new_leaf(handle, inode, path, newext);
1024 + nearex = path[depth].p_ext;
1026 + if ((err = ext3_ext_get_access(handle, inode, path + depth)))
1030 + /* there is no extent in this leaf, create first one */
1031 + ext_debug(inode, "first extent in the leaf: %d:%d:%d\n",
1032 + newext->e_block, newext->e_start,
1034 + path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1035 + } else if (newext->e_block > nearex->e_block) {
1036 + EXT_ASSERT(newext->e_block != nearex->e_block);
1037 + len = EXT_MAX_EXTENT(eh) - nearex;
1038 + len = (len - 1) * sizeof(struct ext3_extent);
1039 + len = len < 0 ? 0 : len;
1040 + ext_debug(inode, "insert %d:%d:%d after: nearest 0x%p, "
1041 + "move %d from 0x%p to 0x%p\n",
1042 + newext->e_block, newext->e_start, newext->e_num,
1043 + nearex, len, nearex + 1, nearex + 2);
1044 + ext3_ext_check_boundary(inode, path + depth, nearex + 2, len);
1045 + memmove(nearex + 2, nearex + 1, len);
1046 + path[depth].p_ext = nearex + 1;
1048 + EXT_ASSERT(newext->e_block != nearex->e_block);
1049 + len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1050 + len = len < 0 ? 0 : len;
1051 + ext_debug(inode, "insert %d:%d:%d before: nearest 0x%p, "
1052 + "move %d from 0x%p to 0x%p\n",
1053 + newext->e_block, newext->e_start, newext->e_num,
1054 + nearex, len, nearex + 1, nearex + 2);
1056 + memmove(nearex + 1, nearex, len);
1057 + path[depth].p_ext = nearex;
1062 + nearex = path[depth].p_ext;
1063 + nearex->e_block = newext->e_block;
1064 + nearex->e_start = newext->e_start;
1065 + nearex->e_num = newext->e_num;
1066 + EXT_ASSERT(nearex->e_num < EXT3_BLOCKS_PER_GROUP(inode->i_sb) &&
1067 + nearex->e_num > 0);
1069 + /* time to correct all indexes above */
1070 + err = ext3_ext_correct_indexes(handle, inode, path);
1073 + err = ext3_ext_dirty(handle, inode, path + depth);
1077 + ext3_ext_drop_refs(inode, npath);
1084 +int ext3_ext_get_block(handle_t *handle, struct inode *inode, long iblock,
1085 + struct buffer_head *bh_result, int create,
1086 + int extend_disksize)
1088 + struct ext3_ext_path *path;
1089 + int depth = EXT3_I(inode)->i_depth;
1090 + struct ext3_extent newex;
1091 + struct ext3_extent *ex;
1092 + int goal, newblock, err = 0;
1094 + ext_debug(inode, "block %d requested for inode %u, bh_result 0x%p\n",
1095 + (int) iblock, (unsigned) inode->i_ino, bh_result);
1096 + bh_result->b_state &= ~(1UL << BH_New);
1098 + down(&EXT3_I(inode)->i_ext_sem);
1100 + /* find extent for this block */
1101 + path = ext3_ext_find_extent(inode, iblock, NULL);
1102 + if (IS_ERR(path)) {
1103 + err = PTR_ERR(path);
1107 + if ((ex = path[depth].p_ext)) {
1108 + /* if found exent covers block, simple return it */
1109 + if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
1110 + newblock = iblock - ex->e_block + ex->e_start;
1111 + ext_debug(inode, "%d fit into %d:%d -> %d\n",
1112 + (int) iblock, ex->e_block, ex->e_num,
1119 + * we couldn't try to create block if create flag is zero
1124 + /* allocate new block */
1125 + goal = ext3_ext_find_goal(inode, path);
1126 + newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
1129 + ext_debug(inode, "allocate new block: goal %d, found %d\n",
1132 + /* try to insert new extent into found leaf and return */
1133 + newex.e_block = iblock;
1134 + newex.e_start = newblock;
1136 + err = ext3_ext_insert_extent(handle, inode, path, &newex);
1140 + /* previous routine could use block we allocated */
1141 + newblock = newex.e_start;
1142 + bh_result->b_state |= (1UL << BH_New);
1145 + ext3_ext_show_leaf(inode, path);
1146 + bh_result->b_dev = inode->i_dev;
1147 + bh_result->b_blocknr = newblock;
1148 + bh_result->b_state |= (1UL << BH_Mapped);
1150 + ext3_ext_drop_refs(inode, path);
1152 + up(&EXT3_I(inode)->i_ext_sem);
1158 + * returns 1 if current index have to be freed (even partial)
1160 +static int ext3_ext_more_to_truncate(struct inode *inode,
1161 + struct ext3_ext_path *path)
1163 + EXT_ASSERT(path->p_idx);
1165 + if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1169 + * if truncate on deeper level happened it it wasn't partial
1170 + * so we have to consider current index for truncation
1172 + if (path->p_hdr->e_num == path->p_block)
1176 + * put actual number of indexes to know is this number got
1177 + * changed at the next iteration
1179 + path->p_block = path->p_hdr->e_num;
1185 + * routine removes index from the index block
1186 + * it's used in truncate case only. thus all requests are for
1187 + * last index in the block only
1189 +int ext3_ext_remove_index(handle_t *handle, struct inode *inode,
1190 + struct ext3_ext_path *path)
1192 + struct buffer_head *bh;
1195 + /* free index block */
1197 + EXT_ASSERT(path->p_hdr->e_num);
1198 + if ((err = ext3_ext_get_access(handle, inode, path)))
1200 + path->p_hdr->e_num--;
1201 + if ((err = ext3_ext_dirty(handle, inode, path)))
1203 + bh = sb_get_hash_table(inode->i_sb, path->p_idx->e_leaf);
1204 + ext3_forget(handle, 0, inode, bh, path->p_idx->e_leaf);
1205 + ext3_free_blocks(handle, inode, path->p_idx->e_leaf, 1);
1207 + ext_debug(inode, "index is empty, remove it, free block %d\n",
1208 + path->p_idx->e_leaf);
1213 + * returns 1 if current extent needs to be freed (even partial)
1214 + * instead, returns 0
1216 +int ext3_ext_more_leaves_to_truncate(struct inode *inode,
1217 + struct ext3_ext_path *path)
1219 + unsigned blocksize = inode->i_sb->s_blocksize;
1220 + struct ext3_extent *ex = path->p_ext;
1225 + /* is there leave in the current leaf? */
1226 + if (ex < EXT_FIRST_EXTENT(path->p_hdr))
1229 + last_block = (inode->i_size + blocksize-1)
1230 + >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
1232 + if (last_block >= ex->e_block + ex->e_num)
1235 + /* seems it extent have to be freed */
1239 +handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
1243 + if (handle->h_buffer_credits > needed)
1245 + if (!ext3_journal_extend(handle, needed))
1247 + err = ext3_journal_restart(handle, needed);
1253 + * this routine calculate max number of blocks to be modified
1254 + * while freeing extent and is intended to be used in truncate path
1256 +static int ext3_ext_calc_credits(struct inode *inode,
1257 + struct ext3_ext_path *path,
1260 + int depth = EXT3_I(inode)->i_depth;
1264 + * extent couldn't cross group, so we will modify
1265 + * single bitmap block and single group descriptor
1270 + * if this is last extent in a leaf, then we have to
1271 + * free leaf block and remove pointer from index above.
1272 + * that pointer could be last in index block, so we'll
1273 + * have to remove it too. this way we could modify/free
1274 + * the whole path + root index (inode stored) will be
1277 + if (!path || (num == path->p_ext->e_num &&
1278 + path->p_ext == EXT_FIRST_EXTENT(path->p_hdr)))
1279 + needed += (depth * (EXT3_ALLOC_NEEDED + 1)) + 1;
1285 + * core of the truncate procedure:
1286 + * - calculated what part of each extent in the requested leaf
1287 + * need to be freed
1288 + * - frees and forgets these blocks
1290 + * TODO: we could optimize and free several extents during
1291 + * single journal_restart()-journal_restart() cycle
1293 +static int ext3_ext_truncate_leaf(handle_t *handle,
1294 + struct inode *inode,
1295 + struct ext3_ext_path *path,
1298 + unsigned blocksize = inode->i_sb->s_blocksize;
1300 + int i, err = 0, sf, num;
1302 + ext_debug(inode, "level %d - leaf\n", depth);
1305 + (struct ext3_extent_header *) path->p_bh->b_data;
1307 + EXT_ASSERT(path->p_hdr->e_num <= path->p_hdr->e_max);
1309 + last_block = (inode->i_size + blocksize-1)
1310 + >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
1311 + path->p_ext = EXT_LAST_EXTENT(path->p_hdr);
1312 + while (ext3_ext_more_leaves_to_truncate(inode, path)) {
1314 + /* what part of extent have to be freed? */
1315 + sf = last_block > path->p_ext->e_block ?
1316 + last_block : path->p_ext->e_block;
1318 + /* number of blocks from extent to be freed */
1319 + num = path->p_ext->e_block + path->p_ext->e_num - sf;
1321 + /* calc physical first physical block to be freed */
1322 + sf = path->p_ext->e_start + (sf - path->p_ext->e_block);
1324 + i = ext3_ext_calc_credits(inode, path, num);
1325 + handle = ext3_ext_journal_restart(handle, i);
1326 + if (IS_ERR(handle))
1327 + return PTR_ERR(handle);
1329 + ext_debug(inode, "free extent %d:%d:%d -> free %d:%d\n",
1330 + path->p_ext->e_block, path->p_ext->e_start,
1331 + path->p_ext->e_num, sf, num);
1332 + for (i = 0; i < num; i++) {
1333 + struct buffer_head *bh =
1334 + sb_get_hash_table(inode->i_sb, sf + i);
1335 + ext3_forget(handle, 0, inode, bh, sf + i);
1337 + ext3_free_blocks(handle, inode, sf, num);
1339 + /* collect extents usage stats */
1340 + spin_lock(&EXT3_SB(inode->i_sb)->s_ext_lock);
1341 + EXT3_SB(inode->i_sb)->s_ext_extents++;
1342 + EXT3_SB(inode->i_sb)->s_ext_blocks += num;
1343 + spin_unlock(&EXT3_SB(inode->i_sb)->s_ext_lock);
1345 + /* reduce extent */
1346 + if ((err = ext3_ext_get_access(handle, inode, path)))
1348 + path->p_ext->e_num -= num;
1349 + if (path->p_ext->e_num == 0)
1350 + path->p_hdr->e_num--;
1351 + if ((err = ext3_ext_dirty(handle, inode, path)))
1357 + /* if this leaf is free, then we should
1358 + * remove it from index block above */
1359 + if (path->p_hdr->e_num == 0 && depth > 0)
1360 + err = ext3_ext_remove_index(handle, inode, path);
1365 +static void ext3_ext_collect_stats(struct inode *inode)
1369 + /* skip inodes with old good bitmap */
1370 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
1373 + /* collect on full truncate only */
1374 + if (inode->i_size)
1377 + depth = EXT3_I(inode)->i_depth;
1378 + if (depth < EXT3_SB(inode->i_sb)->s_ext_mindepth)
1379 + EXT3_SB(inode->i_sb)->s_ext_mindepth = depth;
1380 + if (depth > EXT3_SB(inode->i_sb)->s_ext_maxdepth)
1381 + EXT3_SB(inode->i_sb)->s_ext_maxdepth = depth;
1382 + EXT3_SB(inode->i_sb)->s_ext_sum += depth;
1383 + EXT3_SB(inode->i_sb)->s_ext_count++;
1387 +void ext3_ext_truncate(struct inode * inode)
1389 + struct address_space *mapping = inode->i_mapping;
1390 + struct ext3_ext_path *path;
1391 + struct page * page;
1393 + int i, depth, err = 0;
1395 + ext3_ext_collect_stats(inode);
1398 + * We have to lock the EOF page here, because lock_page() nests
1399 + * outside journal_start().
1401 + if ((inode->i_size & (inode->i_sb->s_blocksize - 1)) == 0) {
1402 + /* Block boundary? Nothing to do */
1405 + page = grab_cache_page(mapping,
1406 + inode->i_size >> PAGE_CACHE_SHIFT);
1412 + * probably first extent we're gonna free will be last in block
1414 + i = ext3_ext_calc_credits(inode, NULL, 0);
1415 + handle = ext3_journal_start(inode, i);
1416 + if (IS_ERR(handle)) {
1418 + clear_highpage(page);
1419 + flush_dcache_page(page);
1420 + unlock_page(page);
1421 + page_cache_release(page);
1427 + ext3_block_truncate_page(handle, mapping, inode->i_size, page,
1428 + inode->i_sb->s_blocksize);
1430 + down(&EXT3_I(inode)->i_ext_sem);
1433 + * TODO: optimization is possible here
1434 + * probably we need not scaning at all,
1435 + * because page truncation is enough
1437 + if (ext3_orphan_add(handle, inode))
1440 + /* we have to know where to truncate from in crash case */
1441 + EXT3_I(inode)->i_disksize = inode->i_size;
1442 + ext3_mark_inode_dirty(handle, inode);
1445 + * we start scanning from right side freeing all the blocks
1446 + * after i_size and walking into the deep
1449 + depth = EXT3_I(inode)->i_depth;
1450 + path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1451 + if (IS_ERR(path)) {
1452 + ext3_error(inode->i_sb, "ext3_ext_truncate",
1453 + "Can't allocate path array");
1456 + memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1458 + path[i].p_hdr = (struct ext3_extent_header *) EXT3_I(inode)->i_data;
1459 + while (i >= 0 && err == 0) {
1461 + /* this is leaf block */
1462 + err = ext3_ext_truncate_leaf(handle, inode,
1464 + /* root level have p_bh == NULL, brelse() eats this */
1465 + brelse(path[i].p_bh);
1470 + /* this is index block */
1471 + if (!path[i].p_hdr) {
1473 + (struct ext3_extent_header *) path[i].p_bh->b_data;
1474 + ext_debug(inode, "initialize header\n");
1477 + EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1479 + if (!path[i].p_idx) {
1480 + /* this level hasn't touched yet */
1481 + path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1482 + path[i].p_block = path[i].p_hdr->e_num + 1;
1483 + ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n",
1484 + path[i].p_hdr, path[i].p_hdr->e_num);
1486 + /* we've already was here, see at next index */
1490 + ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n",
1491 + i, EXT_FIRST_INDEX(path[i].p_hdr),
1493 + if (ext3_ext_more_to_truncate(inode, path + i)) {
1494 + /* go to the next level */
1495 + ext_debug(inode, "move to level %d (block %d)\n", i+1,
1496 + path[i].p_idx->e_leaf);
1497 + memset(path + i + 1, 0, sizeof(*path));
1498 + path[i+1].p_bh = sb_bread(inode->i_sb,
1499 + path[i].p_idx->e_leaf);
1500 + if (!path[i+1].p_bh) {
1501 + /* should we reset i_size? */
1507 + /* we finish processing this index, go up */
1508 + if (path[i].p_hdr->e_num == 0 && i > 0) {
1509 + /* index is empty, remove it
1510 + * handle must be already prepared by the
1513 + err = ext3_ext_remove_index(handle, inode,
1516 + /* root level have p_bh == NULL, brelse() eats this */
1517 + brelse(path[i].p_bh);
1519 + ext_debug(inode, "return to level %d\n", i);
1523 + /* TODO: flexible tree reduction should be here */
1524 + if (path->p_hdr->e_num == 0) {
1526 + * truncate to zero freed all the tree
1527 + * so, we need to correct i_depth
1529 + EXT3_I(inode)->i_depth = 0;
1530 + path->p_hdr->e_max = 0;
1531 + ext3_mark_inode_dirty(handle, inode);
1536 + /* In a multi-transaction truncate, we only make the final
1537 + * transaction synchronous */
1538 + if (IS_SYNC(inode))
1539 + handle->h_sync = 1;
1543 + * If this was a simple ftruncate(), and the file will remain alive
1544 + * then we need to clear up the orphan record which we created above.
1545 + * However, if this was a real unlink then we were called by
1546 + * ext3_delete_inode(), and we allow that function to clean up the
1547 + * orphan info for us.
1549 + if (inode->i_nlink)
1550 + ext3_orphan_del(handle, inode);
1552 + up(&EXT3_I(inode)->i_ext_sem);
1553 + ext3_journal_stop(handle, inode);
1557 + * this routine calculate max number of blocks we could modify
1558 + * in order to allocate new block for an inode
1560 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
1562 + struct ext3_inode_info *ei = EXT3_I(inode);
1563 + int depth = ei->i_depth + 1;
1567 + * the worste case we're expecting is creation of the
1568 + * new root (growing in depth) with index splitting
1569 + * for splitting we have to consider depth + 1 because
1570 + * previous growing could increase it
1574 + * growing in depth:
1575 + * block allocation + new root + old root
1577 + needed = EXT3_ALLOC_NEEDED + 2;
1579 + /* index split. we may need:
1580 + * allocate intermediate indexes and new leaf
1581 + * change two blocks at each level, but root
1582 + * modify root block (inode)
1584 + needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1586 + /* caller want to allocate num blocks */
1589 +#ifdef CONFIG_QUOTA
1591 + * FIXME: real calculation should be here
1592 + * it depends on blockmap format of qouta file
1594 + needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1601 + * called at mount time
1603 +void ext3_ext_init(struct super_block *sb)
1606 + * possible initialization would be here
1609 + if (test_opt(sb, EXTENTS))
1610 + printk("EXT3-fs: file extents enabled\n");
1611 + spin_lock_init(&EXT3_SB(sb)->s_ext_lock);
1615 + * called at umount time
1617 +void ext3_ext_release(struct super_block *sb)
1619 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1621 + /* show collected stats */
1622 + if (sbi->s_ext_count && sbi->s_ext_extents)
1623 + printk("EXT3-fs: min depth - %d, max depth - %d, "
1624 + "ave. depth - %d, ave. blocks/extent - %d\n",
1625 + sbi->s_ext_mindepth,
1626 + sbi->s_ext_maxdepth,
1627 + sbi->s_ext_sum / sbi->s_ext_count,
1628 + sbi->s_ext_blocks / sbi->s_ext_extents);
1630 Index: linux-2.4.18-chaos/fs/ext3/ialloc.c
1631 ===================================================================
1632 --- linux-2.4.18-chaos.orig/fs/ext3/ialloc.c 2004-01-13 16:10:23.000000000 +0300
1633 +++ linux-2.4.18-chaos/fs/ext3/ialloc.c 2004-01-13 16:11:00.000000000 +0300
1634 @@ -573,6 +573,10 @@
1635 ei->i_prealloc_count = 0;
1637 ei->i_block_group = i;
1638 + if (test_opt(sb, EXTENTS))
1639 + EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
1641 + sema_init(&ei->i_ext_sem, 1);
1643 if (ei->i_flags & EXT3_SYNC_FL)
1644 inode->i_flags |= S_SYNC;
1645 Index: linux-2.4.18-chaos/fs/ext3/inode.c
1646 ===================================================================
1647 --- linux-2.4.18-chaos.orig/fs/ext3/inode.c 2004-01-13 16:10:23.000000000 +0300
1648 +++ linux-2.4.18-chaos/fs/ext3/inode.c 2004-01-13 16:11:00.000000000 +0300
1649 @@ -842,6 +842,15 @@
1654 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
1655 + struct buffer_head *bh, int create, int extend_disksize)
1657 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
1658 + return ext3_ext_get_block(handle, inode, block, bh, create, 1);
1659 + return ext3_get_block_handle(handle, inode, block, bh, create, 1);
1663 * The BKL is not held on entry here.
1666 handle = ext3_journal_current_handle();
1667 J_ASSERT(handle != 0);
1669 - ret = ext3_get_block_handle(handle, inode, iblock,
1670 + ret = ext3_get_block_wrap(handle, inode, iblock,
1671 bh_result, create, 1);
1678 - ret = ext3_get_block_handle(handle, inode, iblock,
1679 + ret = ext3_get_block_wrap(handle, inode, iblock,
1680 bh_result, create, 0);
1682 bh_result->b_size = (1 << inode->i_blkbits);
1685 dummy.b_blocknr = -1000;
1686 buffer_trace_init(&dummy.b_history);
1687 - *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
1688 + *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
1689 if (!*errp && buffer_mapped(&dummy)) {
1690 struct buffer_head *bh;
1691 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1692 @@ -1520,7 +1529,7 @@
1693 * This required during truncate. We need to physically zero the tail end
1694 * of that block so it doesn't yield old data if the file is later grown.
1696 -static int ext3_block_truncate_page(handle_t *handle,
1697 +int ext3_block_truncate_page(handle_t *handle,
1698 struct address_space *mapping, loff_t from,
1699 struct page *page, unsigned blocksize)
1701 @@ -1998,6 +2007,9 @@
1703 ext3_discard_prealloc(inode);
1705 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
1706 + return ext3_ext_truncate(inode);
1708 blocksize = inode->i_sb->s_blocksize;
1709 last_block = (inode->i_size + blocksize-1)
1710 >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
1711 @@ -2436,6 +2448,8 @@
1712 ei->i_prealloc_count = 0;
1714 ei->i_block_group = iloc.block_group;
1715 + ei->i_depth = raw_inode->osd2.linux2.l_i_depth;
1716 + sema_init(&ei->i_ext_sem, 1);
1719 * NOTE! The in-memory inode i_data array is in little-endian order
1720 @@ -2556,6 +2570,7 @@
1721 raw_inode->i_fsize = 0;
1724 + raw_inode->osd2.linux2.l_i_depth = ei->i_depth;
1725 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
1726 if (!S_ISREG(inode->i_mode)) {
1727 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
1728 @@ -2759,6 +2774,9 @@
1729 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
1732 + if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
1733 + return ext3_ext_writepage_trans_blocks(inode, bpp);
1735 if (ext3_should_journal_data(inode))
1736 ret = 3 * (bpp + indirects) + 2;
1738 @@ -3082,7 +3100,7 @@
1740 /* alloc blocks one by one */
1741 for (i = 0; i < nblocks; i++) {
1742 - ret = ext3_get_block_handle(handle, inode, blocks[i],
1743 + ret = ext3_get_block_wrap(handle, inode, blocks[i],
1747 @@ -3158,7 +3176,7 @@
1751 - rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1, 1);
1752 + rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1, 1);
1754 printk(KERN_INFO "ext3_map_inode_page: error %d "
1755 "allocating block %ld\n", rc, iblock);
1756 Index: linux-2.4.18-chaos/fs/ext3/Makefile
1757 ===================================================================
1758 --- linux-2.4.18-chaos.orig/fs/ext3/Makefile 2004-01-13 16:10:23.000000000 +0300
1759 +++ linux-2.4.18-chaos/fs/ext3/Makefile 2004-01-13 16:11:00.000000000 +0300
1761 export-objs := ext3-exports.o
1763 obj-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
1764 - ioctl.o namei.o super.o symlink.o xattr.o ext3-exports.o
1765 + ioctl.o namei.o super.o symlink.o xattr.o ext3-exports.o \
1767 obj-m := $(O_TARGET)
1769 include $(TOPDIR)/Rules.make
1770 Index: linux-2.4.18-chaos/fs/ext3/super.c
1771 ===================================================================
1772 --- linux-2.4.18-chaos.orig/fs/ext3/super.c 2004-01-13 16:10:23.000000000 +0300
1773 +++ linux-2.4.18-chaos/fs/ext3/super.c 2004-01-13 16:11:23.000000000 +0300
1776 J_ASSERT(sbi->s_delete_inodes == 0);
1778 + ext3_ext_release(sb);
1779 ext3_xattr_put_super(sb);
1780 journal_destroy(sbi->s_journal);
1781 if (!(sb->s_flags & MS_RDONLY)) {
1782 @@ -743,6 +744,12 @@
1786 + if (!strcmp (this_char, "extents"))
1787 + set_opt (sbi->s_mount_opt, EXTENTS);
1789 + if (!strcmp (this_char, "extdebug"))
1790 + set_opt (sbi->s_mount_opt, EXTDEBUG);
1792 if (!strcmp (this_char, "bsddf"))
1793 clear_opt (*mount_options, MINIX_DF);
1794 else if (!strcmp (this_char, "nouid32")) {
1795 @@ -1470,6 +1477,7 @@
1796 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
1797 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
1799 + ext3_ext_init(sb);
1803 Index: linux-2.4.18-chaos/include/linux/ext3_fs.h
1804 ===================================================================
1805 --- linux-2.4.18-chaos.orig/include/linux/ext3_fs.h 2004-01-13 16:10:23.000000000 +0300
1806 +++ linux-2.4.18-chaos/include/linux/ext3_fs.h 2004-01-13 16:11:00.000000000 +0300
1808 #define EXT3_IMAGIC_FL 0x00002000 /* AFS directory */
1809 #define EXT3_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
1810 #define EXT3_RESERVED_FL 0x80000000 /* reserved for ext3 lib */
1811 +#define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
1813 #define EXT3_FL_USER_VISIBLE 0x00005FFF /* User visible flags */
1814 #define EXT3_FL_USER_MODIFIABLE 0x000000FF /* User modifiable flags */
1817 __u8 l_i_frag; /* Fragment number */
1818 __u8 l_i_fsize; /* Fragment size */
1821 __u16 l_i_uid_high; /* these 2 fields */
1822 __u16 l_i_gid_high; /* were reserved2[0] */
1823 __u32 l_i_reserved2;
1825 #define EXT3_MOUNT_IOPEN 0x8000 /* Allow access via iopen */
1826 #define EXT3_MOUNT_IOPEN_NOPRIV 0x10000 /* Make iopen world-readable */
1827 #define EXT3_MOUNT_ASYNCDEL 0x20000 /* Delayed deletion */
1828 +#define EXT3_MOUNT_EXTENTS 0x40000 /* Extents support */
1829 +#define EXT3_MOUNT_EXTDEBUG 0x80000 /* Extents debug */
1831 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
1832 #ifndef _LINUX_EXT2_FS_H
1833 @@ -663,6 +666,12 @@
1834 extern void ext3_dirty_inode(struct inode *);
1835 extern int ext3_change_inode_journal_flag(struct inode *, int);
1836 extern void ext3_truncate (struct inode *);
1837 +extern int ext3_block_truncate_page(handle_t *handle,
1838 + struct address_space *mapping, loff_t from,
1839 + struct page *page, unsigned blocksize);
1840 +extern int ext3_forget(handle_t *handle, int is_metadata,
1841 + struct inode *inode, struct buffer_head *bh,
1843 #ifdef EXT3_DELETE_THREAD
1844 extern void ext3_truncate_thread(struct inode *inode);
1846 @@ -722,6 +731,13 @@
1848 extern struct inode_operations ext3_fast_symlink_inode_operations;
1851 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
1852 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
1853 + struct buffer_head *, int, int);
1854 +extern void ext3_ext_truncate(struct inode *);
1855 +extern void ext3_ext_init(struct super_block *);
1856 +extern void ext3_ext_release(struct super_block *);
1858 #endif /* __KERNEL__ */
1860 Index: linux-2.4.18-chaos/include/linux/ext3_fs_i.h
1861 ===================================================================
1862 --- linux-2.4.18-chaos.orig/include/linux/ext3_fs_i.h 2001-11-22 22:46:19.000000000 +0300
1863 +++ linux-2.4.18-chaos/include/linux/ext3_fs_i.h 2004-01-13 16:11:00.000000000 +0300
1865 * by other means, so we have truncate_sem.
1867 struct rw_semaphore truncate_sem;
1869 + /* extents-related data */
1870 + struct semaphore i_ext_sem;
1874 #endif /* _LINUX_EXT3_FS_I */
1875 Index: linux-2.4.18-chaos/include/linux/ext3_fs_sb.h
1876 ===================================================================
1877 --- linux-2.4.18-chaos.orig/include/linux/ext3_fs_sb.h 2004-01-13 16:10:21.000000000 +0300
1878 +++ linux-2.4.18-chaos/include/linux/ext3_fs_sb.h 2004-01-13 16:11:00.000000000 +0300
1880 wait_queue_head_t s_delete_thread_queue;
1881 wait_queue_head_t s_delete_waiter_queue;
1886 + int s_ext_mindepth;
1887 + int s_ext_maxdepth;
1890 + spinlock_t s_ext_lock;
1891 + int s_ext_extents;
1895 #endif /* _LINUX_EXT3_FS_SB */