Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-extents-2.4.24.patch
1 Index: linux-2.4.24-mb34/fs/ext3/extents.c
2 ===================================================================
3 --- linux-2.4.24-mb34.orig/fs/ext3/extents.c    1969-12-31 16:00:00.000000000 -0800
4 +++ linux-2.4.24-mb34/fs/ext3/extents.c 2004-05-05 14:27:07.000000000 -0700
5 @@ -0,0 +1,2346 @@
6 +/*
7 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
8 + *
9 + * This program is free software; you can redistribute it and/or modify
10 + * it under the terms of the GNU General Public License version 2 as
11 + * published by the Free Software Foundation.
12 + *
13 + * This program is distributed in the hope that it will be useful,
14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 + * GNU General Public License for more details.
17 + *
18 + * You should have received a copy of the GNU General Public Licens
19 + * along with this program; if not, write to the Free Software
20 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
21 + */
22 +
23 +/*
24 + * Extents support for EXT3
25 + *
26 + * TODO:
27 + *   - ext3_ext_walk_space() sould not use ext3_ext_find_extent()
28 + *   - ext3_ext_calc_credits() could take 'mergable' into account
29 + *   - ext3*_error() should be used in some situations
30 + *   - find_goal() [to be tested and improved]
31 + *   - smart tree reduction
32 + *   - arch-independence
33 + *     common on-disk format for big/little-endian arch
34 + */
35 +
36 +#include <linux/module.h>
37 +#include <linux/fs.h>
38 +#include <linux/time.h>
39 +#include <linux/ext3_jbd.h>
40 +#include <linux/jbd.h>
41 +#include <linux/smp_lock.h>
42 +#include <linux/highuid.h>
43 +#include <linux/pagemap.h>
44 +#include <linux/quotaops.h>
45 +#include <linux/string.h>
46 +#include <linux/slab.h>
47 +#include <linux/locks.h>
48 +#include <linux/ext3_extents.h>
49 +#include <asm/uaccess.h>
50 +
51 +static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
52 +{
53 +       int err;
54 +
55 +       if (handle->h_buffer_credits > needed)
56 +               return handle;
57 +       if (!ext3_journal_extend(handle, needed))
58 +               return handle;
59 +       err = ext3_journal_restart(handle, needed);
60 +       
61 +       return handle;
62 +}
63 +
64 +static int inline
65 +ext3_ext_get_access_for_root(handle_t *h, struct ext3_extents_tree *tree)
66 +{
67 +       if (tree->get_write_access)
68 +               return tree->get_write_access(h,tree->buffer);
69 +       else
70 +               return 0;
71 +}
72 +
73 +static int inline
74 +ext3_ext_mark_root_dirty(handle_t *h, struct ext3_extents_tree *tree)
75 +{
76 +       if (tree->mark_buffer_dirty)
77 +               return tree->mark_buffer_dirty(h,tree->buffer);
78 +       else
79 +               return 0;
80 +}
81 +
82 +/*
83 + * could return:
84 + *  - EROFS
85 + *  - ENOMEM
86 + */
87 +static int ext3_ext_get_access(handle_t *handle,
88 +                               struct ext3_extents_tree *tree,
89 +                               struct ext3_ext_path *path)
90 +{
91 +       int err;
92 +
93 +       if (path->p_bh) {
94 +               /* path points to block */
95 +               err = ext3_journal_get_write_access(handle, path->p_bh);
96 +       } else {
97 +               /* path points to leaf/index in inode body */
98 +               err = ext3_ext_get_access_for_root(handle, tree);
99 +       }
100 +       return err;
101 +}
102 +
103 +/*
104 + * could return:
105 + *  - EROFS
106 + *  - ENOMEM
107 + *  - EIO
108 + */
109 +static int ext3_ext_dirty(handle_t *handle, struct ext3_extents_tree *tree,
110 +                               struct ext3_ext_path *path)
111 +{
112 +       int err;
113 +       if (path->p_bh) {
114 +               /* path points to block */
115 +               err =ext3_journal_dirty_metadata(handle, path->p_bh);
116 +       } else {
117 +               /* path points to leaf/index in inode body */
118 +               err = ext3_ext_mark_root_dirty(handle, tree);
119 +       }
120 +       return err;
121 +}
122 +
123 +static int inline
124 +ext3_ext_new_block(handle_t *handle, struct ext3_extents_tree *tree,
125 +                       struct ext3_ext_path *path, struct ext3_extent *ex,
126 +                       int *err)
127 +{
128 +       int goal, depth, newblock;
129 +       struct inode *inode;
130 +
131 +       EXT_ASSERT(tree);
132 +       if (tree->new_block)
133 +               return tree->new_block(handle, tree, path, ex, err);
134 +
135 +       inode = tree->inode;
136 +       depth = EXT_DEPTH(tree);
137 +       if (path && depth > 0) {
138 +               goal = path[depth-1].p_block;
139 +       } else {
140 +               struct ext3_inode_info *ei = EXT3_I(inode);
141 +               unsigned long bg_start;
142 +               unsigned long colour;
143 +
144 +               bg_start = (ei->i_block_group *
145 +                               EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
146 +                       le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
147 +               colour = (current->pid % 16) *
148 +                       (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
149 +               goal = bg_start + colour;
150 +       }
151 +
152 +       newblock = ext3_new_block(handle, inode, goal, 0, 0, err);
153 +       return newblock;
154 +}
155 +
156 +static inline void ext3_ext_tree_changed(struct ext3_extents_tree *tree)
157 +{
158 +       struct ext3_extent_header *neh;
159 +       neh = EXT_ROOT_HDR(tree);
160 +       neh->e_generation++;
161 +}
162 +
163 +static inline int ext3_ext_space_block(struct ext3_extents_tree *tree)
164 +{
165 +       int size;
166 +
167 +       size = (tree->inode->i_sb->s_blocksize -
168 +                       sizeof(struct ext3_extent_header))
169 +                               / sizeof(struct ext3_extent);
170 +#ifdef AGRESSIVE_TEST
171 +       size = 6;
172 +#endif
173 +       return size;
174 +}
175 +
176 +static inline int ext3_ext_space_block_idx(struct ext3_extents_tree *tree)
177 +{
178 +       int size;
179 +
180 +       size = (tree->inode->i_sb->s_blocksize -
181 +                       sizeof(struct ext3_extent_header))
182 +                               / sizeof(struct ext3_extent_idx);
183 +#ifdef AGRESSIVE_TEST
184 +       size = 5;
185 +#endif
186 +       return size;
187 +}
188 +
189 +static inline int ext3_ext_space_root(struct ext3_extents_tree *tree)
190 +{
191 +       int size;
192 +
193 +       size = (tree->buffer_len - sizeof(struct ext3_extent_header))
194 +                       / sizeof(struct ext3_extent);
195 +#ifdef AGRESSIVE_TEST
196 +       size = 3;
197 +#endif
198 +       return size;
199 +}
200 +
201 +static inline int ext3_ext_space_root_idx(struct ext3_extents_tree *tree)
202 +{
203 +       int size;
204 +
205 +       size = (tree->buffer_len -
206 +                       sizeof(struct ext3_extent_header))
207 +                       / sizeof(struct ext3_extent_idx);
208 +#ifdef AGRESSIVE_TEST
209 +       size = 4;
210 +#endif
211 +       return size;
212 +}
213 +
214 +static void ext3_ext_show_path(struct ext3_extents_tree *tree,
215 +                               struct ext3_ext_path *path)
216 +{
217 +#ifdef EXT_DEBUG
218 +       int k, l = path->p_depth;
219 +
220 +       ext_debug(tree, "path:");
221 +       for (k = 0; k <= l; k++, path++) {
222 +               if (path->p_idx) {
223 +                       ext_debug(tree, "  %d->%d", path->p_idx->e_block,
224 +                                       path->p_idx->e_leaf);
225 +               } else if (path->p_ext) {
226 +                       ext_debug(tree, "  %d:%d:%d",
227 +                                       path->p_ext->e_block,
228 +                                       path->p_ext->e_num,
229 +                                       path->p_ext->e_start);
230 +               } else
231 +                       ext_debug(tree, "  []");
232 +       }
233 +       ext_debug(tree, "\n");
234 +#endif
235 +}
236 +
237 +static void ext3_ext_show_leaf(struct ext3_extents_tree *tree,
238 +                               struct ext3_ext_path *path)
239 +{
240 +#ifdef EXT_DEBUG
241 +       int depth = EXT_DEPTH(tree);
242 +       struct ext3_extent_header *eh;
243 +       struct ext3_extent *ex;
244 +       int i;
245 +
246 +       if (!path)
247 +               return;
248 +
249 +       eh = path[depth].p_hdr;
250 +       ex = EXT_FIRST_EXTENT(eh);
251 +
252 +       for (i = 0; i < eh->e_num; i++, ex++) {
253 +               ext_debug(tree, "%d:%d:%d ",
254 +                               ex->e_block, ex->e_num, ex->e_start);
255 +       }
256 +       ext_debug(tree, "\n");
257 +#endif
258 +}
259 +
260 +static void ext3_ext_drop_refs(struct ext3_ext_path *path)
261 +{
262 +       int depth = path->p_depth;
263 +       int i;
264 +
265 +       for (i = 0; i <= depth; i++, path++)
266 +               if (path->p_bh) {
267 +                       brelse(path->p_bh);
268 +                       path->p_bh = NULL;
269 +               }
270 +}
271 +
272 +/*
273 + * binary search for closest index by given block
274 + */
275 +static inline void
276 +ext3_ext_binsearch_idx(struct ext3_extents_tree *tree,
277 +                       struct ext3_ext_path *path, int block)
278 +{
279 +       struct ext3_extent_header *eh = path->p_hdr;
280 +       struct ext3_extent_idx *ix;
281 +       int l = 0, k, r;
282 +
283 +       EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
284 +       EXT_ASSERT(eh->e_num <= eh->e_max);
285 +       EXT_ASSERT(eh->e_num > 0);
286 +
287 +       ext_debug(tree, "binsearch for %d(idx):  ", block);
288 +
289 +       path->p_idx = ix = EXT_FIRST_INDEX(eh);
290 +
291 +       r = k = eh->e_num;
292 +       while (k > 1) {
293 +               k = (r - l) / 2;
294 +               if (block < ix[l + k].e_block)
295 +                       r -= k;
296 +               else
297 +                       l += k;
298 +               ext_debug(tree, "%d:%d:%d ", k, l, r);
299 +       }
300 +
301 +       ix += l;
302 +       path->p_idx = ix;
303 +       ext_debug(tree, "  -> %d->%d ", path->p_idx->e_block, path->p_idx->e_leaf);
304 +
305 +       while (l++ < r) {
306 +               if (block < ix->e_block) 
307 +                       break;
308 +               path->p_idx = ix++;
309 +       }
310 +       ext_debug(tree, "  -> %d->%d\n", path->p_idx->e_block,
311 +                       path->p_idx->e_leaf);
312 +
313 +#ifdef CHECK_BINSEARCH 
314 +       {
315 +               struct ext3_extent_idx *chix;
316 +
317 +               chix = ix = EXT_FIRST_INDEX(eh);
318 +               for (k = 0; k < eh->e_num; k++, ix++) {
319 +                       if (k != 0 && ix->e_block <= ix[-1].e_block) {
320 +                               printk("k=%d, ix=0x%p, first=0x%p\n", k,
321 +                                       ix, EXT_FIRST_INDEX(eh));
322 +                               printk("%u <= %u\n",
323 +                                       ix->e_block,ix[-1].e_block);
324 +                       }
325 +                       EXT_ASSERT(k == 0 || ix->e_block > ix[-1].e_block);
326 +                       if (block < ix->e_block) 
327 +                               break;
328 +                       chix = ix;
329 +               }
330 +               EXT_ASSERT(chix == path->p_idx);
331 +       }
332 +#endif
333 +
334 +}
335 +
336 +/*
337 + * binary search for closest extent by given block
338 + */
339 +static inline void
340 +ext3_ext_binsearch(struct ext3_extents_tree *tree,
341 +                       struct ext3_ext_path *path, int block)
342 +{
343 +       struct ext3_extent_header *eh = path->p_hdr;
344 +       struct ext3_extent *ex;
345 +       int l = 0, k, r;
346 +
347 +       EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
348 +       EXT_ASSERT(eh->e_num <= eh->e_max);
349 +
350 +       if (eh->e_num == 0) {
351 +               /*
352 +                * this leaf is empty yet:
353 +                *  we get such a leaf in split/add case
354 +                */
355 +               return;
356 +       }
357 +       
358 +       ext_debug(tree, "binsearch for %d:  ", block);
359 +
360 +       path->p_ext = ex = EXT_FIRST_EXTENT(eh);
361 +
362 +       r = k = eh->e_num;
363 +       while (k > 1) {
364 +               k = (r - l) / 2;
365 +               if (block < ex[l + k].e_block)
366 +                       r -= k;
367 +               else
368 +                       l += k;
369 +               ext_debug(tree, "%d:%d:%d ", k, l, r);
370 +       }
371 +
372 +       ex += l;
373 +       path->p_ext = ex;
374 +       ext_debug(tree, "  -> %d:%d:%d ", path->p_ext->e_block,
375 +                       path->p_ext->e_start, path->p_ext->e_num);
376 +
377 +       while (l++ < r) {
378 +               if (block < ex->e_block) 
379 +                       break;
380 +               path->p_ext = ex++;
381 +       }
382 +       ext_debug(tree, "  -> %d:%d:%d\n", path->p_ext->e_block,
383 +                       path->p_ext->e_start, path->p_ext->e_num);
384 +
385 +#ifdef CHECK_BINSEARCH 
386 +       {
387 +               struct ext3_extent *chex;
388 +
389 +               chex = ex = EXT_FIRST_EXTENT(eh);
390 +               for (k = 0; k < eh->e_num; k++, ex++) {
391 +                       EXT_ASSERT(k == 0 || ex->e_block > ex[-1].e_block);
392 +                       if (block < ex->e_block) 
393 +                               break;
394 +                       chex = ex;
395 +               }
396 +               EXT_ASSERT(chex == path->p_ext);
397 +       }
398 +#endif
399 +
400 +}
401 +
402 +int ext3_extent_tree_init(handle_t *handle, struct ext3_extents_tree *tree)
403 +{
404 +       struct ext3_extent_header *eh;
405 +
406 +       BUG_ON(tree->buffer_len == 0);
407 +       ext3_ext_get_access_for_root(handle, tree);
408 +       eh = EXT_ROOT_HDR(tree);
409 +       eh->e_depth = 0;
410 +       eh->e_num = 0;
411 +       eh->e_magic = EXT3_EXT_MAGIC;
412 +       eh->e_max = ext3_ext_space_root(tree);
413 +       ext3_ext_mark_root_dirty(handle, tree);
414 +       return 0;
415 +}
416 +
417 +struct ext3_ext_path *
418 +ext3_ext_find_extent(struct ext3_extents_tree *tree, int block,
419 +                       struct ext3_ext_path *path)
420 +{
421 +       struct ext3_extent_header *eh;
422 +       struct buffer_head *bh;
423 +       int depth, i, ppos = 0;
424 +
425 +       EXT_ASSERT(tree);
426 +       EXT_ASSERT(tree->inode);
427 +       EXT_ASSERT(tree->root);
428 +
429 +       eh = EXT_ROOT_HDR(tree);
430 +       EXT_ASSERT(eh);
431 +       i = depth = EXT_DEPTH(tree);
432 +       EXT_ASSERT(eh->e_max);
433 +       EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
434 +       EXT_ASSERT(i == 0 || eh->e_num > 0);
435 +       
436 +       /* account possible depth increase */
437 +       if (!path) {
438 +               path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
439 +                               GFP_NOFS);
440 +               if (!path)
441 +                       return ERR_PTR(-ENOMEM);
442 +       }
443 +       memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
444 +       path[0].p_hdr = eh;
445 +
446 +       /* walk through the tree */
447 +       while (i) {
448 +               ext_debug(tree, "depth %d: num %d, max %d\n",
449 +                               ppos, eh->e_num, eh->e_max);
450 +               ext3_ext_binsearch_idx(tree, path + ppos, block);
451 +               path[ppos].p_block = path[ppos].p_idx->e_leaf;
452 +               path[ppos].p_depth = i;
453 +               path[ppos].p_ext = NULL;
454 +
455 +               bh = sb_bread(tree->inode->i_sb, path[ppos].p_block);
456 +               if (!bh) {
457 +                       ext3_ext_drop_refs(path);
458 +                       kfree(path);
459 +                       return ERR_PTR(-EIO);
460 +               }
461 +               eh = EXT_BLOCK_HDR(bh);
462 +               ppos++;
463 +               EXT_ASSERT(ppos <= depth);
464 +               path[ppos].p_bh = bh;
465 +               path[ppos].p_hdr = eh;
466 +               i--;
467 +       }
468 +
469 +       path[ppos].p_depth = i;
470 +       path[ppos].p_hdr = eh;
471 +       path[ppos].p_ext = NULL;
472 +
473 +       /* find extent */
474 +       ext3_ext_binsearch(tree, path + ppos, block);
475 +
476 +       ext3_ext_show_path(tree, path);
477 +
478 +       return path;
479 +}
480 +
481 +/*
482 + * insert new index [logical;ptr] into the block at cupr
483 + * it check where to insert: before curp or after curp
484 + */
485 +static int ext3_ext_insert_index(handle_t *handle,
486 +                               struct ext3_extents_tree *tree,
487 +                               struct ext3_ext_path *curp,
488 +                               int logical, int ptr)
489 +{
490 +       struct ext3_extent_idx *ix;
491 +       int len, err;
492 +
493 +       if ((err = ext3_ext_get_access(handle, tree, curp)))
494 +               return err;
495 +
496 +       EXT_ASSERT(logical != curp->p_idx->e_block);
497 +       len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
498 +       if (logical > curp->p_idx->e_block) {
499 +               /* insert after */
500 +               if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
501 +                       len = (len - 1) * sizeof(struct ext3_extent_idx);
502 +                       len = len < 0 ? 0 : len;
503 +                       ext_debug(tree, "insert new index %d after: %d. "
504 +                                       "move %d from 0x%p to 0x%p\n",
505 +                                       logical, ptr, len,
506 +                                       (curp->p_idx + 1), (curp->p_idx + 2));
507 +                       memmove(curp->p_idx + 2, curp->p_idx + 1, len);
508 +               }
509 +               ix = curp->p_idx + 1;
510 +       } else {
511 +               /* insert before */
512 +               len = len * sizeof(struct ext3_extent_idx);
513 +               len = len < 0 ? 0 : len;
514 +               ext_debug(tree, "insert new index %d before: %d. "
515 +                               "move %d from 0x%p to 0x%p\n",
516 +                               logical, ptr, len,
517 +                               curp->p_idx, (curp->p_idx + 1));
518 +               memmove(curp->p_idx + 1, curp->p_idx, len);
519 +               ix = curp->p_idx;
520 +       }
521 +
522 +       ix->e_block = logical;
523 +       ix->e_leaf = ptr;
524 +       curp->p_hdr->e_num++;
525 +
526 +       EXT_ASSERT(curp->p_hdr->e_num <= curp->p_hdr->e_max);
527 +       EXT_ASSERT(ix <= EXT_LAST_INDEX(curp->p_hdr));
528 +
529 +       err = ext3_ext_dirty(handle, tree, curp);
530 +       ext3_std_error(tree->inode->i_sb, err);
531 +
532 +       return err;
533 +}
534 +
535 +/*
536 + * routine inserts new subtree into the path, using free index entry
537 + * at depth 'at:
538 + *  - allocates all needed blocks (new leaf and all intermediate index blocks)
539 + *  - makes decision where to split
540 + *  - moves remaining extens and index entries (right to the split point)
541 + *    into the newly allocated blocks
542 + *  - initialize subtree
543 + */
544 +static int ext3_ext_split(handle_t *handle, struct ext3_extents_tree *tree,
545 +                               struct ext3_ext_path *path,
546 +                               struct ext3_extent *newext, int at)
547 +{
548 +       struct buffer_head *bh = NULL;
549 +       int depth = EXT_DEPTH(tree);
550 +       struct ext3_extent_header *neh;
551 +       struct ext3_extent_idx *fidx;
552 +       struct ext3_extent *ex;
553 +       int i = at, k, m, a;
554 +       unsigned long newblock, oldblock, border;
555 +       int *ablocks = NULL; /* array of allocated blocks */
556 +       int err = 0;
557 +
558 +       /* make decision: where to split? */
559 +       /* FIXME: now desicion is simplest: at current extent */
560 +
561 +       /* if current leaf will be splitted, then we should use 
562 +        * border from split point */
563 +       EXT_ASSERT(path[depth].p_ext <= EXT_MAX_EXTENT(path[depth].p_hdr));
564 +       if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
565 +               border = path[depth].p_ext[1].e_block;
566 +               ext_debug(tree, "leaf will be splitted."
567 +                               " next leaf starts at %d\n",
568 +                               (int)border);
569 +       } else {
570 +               border = newext->e_block;
571 +               ext_debug(tree, "leaf will be added."
572 +                               " next leaf starts at %d\n",
573 +                               (int)border);
574 +       }
575 +
576 +       /* 
577 +        * if error occurs, then we break processing
578 +        * and turn filesystem read-only. so, index won't
579 +        * be inserted and tree will be in consistent
580 +        * state. next mount will repair buffers too
581 +        */
582 +
583 +       /*
584 +        * get array to track all allocated blocks
585 +        * we need this to handle errors and free blocks
586 +        * upon them
587 +        */
588 +       ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
589 +       if (!ablocks)
590 +               return -ENOMEM;
591 +       memset(ablocks, 0, sizeof(unsigned long) * depth);
592 +
593 +       /* allocate all needed blocks */
594 +       ext_debug(tree, "allocate %d blocks for indexes/leaf\n", depth - at);
595 +       for (a = 0; a < depth - at; a++) {
596 +               newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
597 +               if (newblock == 0)
598 +                       goto cleanup;
599 +               ablocks[a] = newblock;
600 +       }
601 +
602 +       /* initialize new leaf */
603 +       newblock = ablocks[--a];
604 +       EXT_ASSERT(newblock);
605 +       bh = sb_getblk(tree->inode->i_sb, newblock);
606 +       if (!bh) {
607 +               err = -EIO;
608 +               goto cleanup;
609 +       }
610 +       lock_buffer(bh);
611 +
612 +       if ((err = ext3_journal_get_create_access(handle, bh)))
613 +               goto cleanup;
614 +
615 +       neh = EXT_BLOCK_HDR(bh);
616 +       neh->e_num = 0;
617 +       neh->e_max = ext3_ext_space_block(tree);
618 +       neh->e_magic = EXT3_EXT_MAGIC;
619 +       neh->e_depth = 0;
620 +       ex = EXT_FIRST_EXTENT(neh);
621 +
622 +       /* move remain of path[depth] to the new leaf */
623 +       EXT_ASSERT(path[depth].p_hdr->e_num == path[depth].p_hdr->e_max);
624 +       /* start copy from next extent */
625 +       /* TODO: we could do it by single memmove */
626 +       m = 0;
627 +       path[depth].p_ext++;
628 +       while (path[depth].p_ext <=
629 +                       EXT_MAX_EXTENT(path[depth].p_hdr)) {
630 +               ext_debug(tree, "move %d:%d:%d in new leaf %lu\n",
631 +                               path[depth].p_ext->e_block,
632 +                               path[depth].p_ext->e_start,
633 +                               path[depth].p_ext->e_num,
634 +                               newblock);
635 +               memmove(ex++, path[depth].p_ext++,
636 +                               sizeof(struct ext3_extent));
637 +               neh->e_num++;
638 +               m++;
639 +       }
640 +       mark_buffer_uptodate(bh, 1);
641 +       unlock_buffer(bh);
642 +
643 +       if ((err = ext3_journal_dirty_metadata(handle, bh)))
644 +               goto cleanup;   
645 +       brelse(bh);
646 +       bh = NULL;
647 +
648 +       /* correct old leaf */
649 +       if (m) {
650 +               if ((err = ext3_ext_get_access(handle, tree, path + depth)))
651 +                       goto cleanup;
652 +               path[depth].p_hdr->e_num -= m;
653 +               if ((err = ext3_ext_dirty(handle, tree, path + depth)))
654 +                       goto cleanup;
655 +               
656 +       }
657 +
658 +       /* create intermediate indexes */
659 +       k = depth - at - 1;
660 +       EXT_ASSERT(k >= 0);
661 +       if (k)
662 +               ext_debug(tree, "create %d intermediate indices\n", k);
663 +       /* insert new index into current index block */
664 +       /* current depth stored in i var */
665 +       i = depth - 1;
666 +       while (k--) {
667 +               oldblock = newblock;
668 +               newblock = ablocks[--a];
669 +               bh = sb_getblk(tree->inode->i_sb, newblock);
670 +               if (!bh) {
671 +                       err = -EIO;
672 +                       goto cleanup;
673 +               }
674 +               lock_buffer(bh);
675 +
676 +               if ((err = ext3_journal_get_create_access(handle, bh)))
677 +                       goto cleanup;
678 +
679 +               neh = EXT_BLOCK_HDR(bh);
680 +               neh->e_num = 1;
681 +               neh->e_magic = EXT3_EXT_MAGIC;
682 +               neh->e_max = ext3_ext_space_block_idx(tree);
683 +               neh->e_depth = depth - i; 
684 +               fidx = EXT_FIRST_INDEX(neh);
685 +               fidx->e_block = border;
686 +               fidx->e_leaf = oldblock;
687 +
688 +               ext_debug(tree, "int.index at %d (block %lu): %lu -> %lu\n",
689 +                               i, newblock, border, oldblock);
690 +               /* copy indexes */
691 +               m = 0;
692 +               path[i].p_idx++;
693 +
694 +               ext_debug(tree, "cur 0x%p, last 0x%p\n", path[i].p_idx,
695 +                               EXT_MAX_INDEX(path[i].p_hdr));
696 +               EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
697 +                               EXT_LAST_INDEX(path[i].p_hdr));
698 +               while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
699 +                       ext_debug(tree, "%d: move %d:%d in new index %lu\n",
700 +                                       i, path[i].p_idx->e_block,
701 +                                       path[i].p_idx->e_leaf, newblock);
702 +                       memmove(++fidx, path[i].p_idx++,
703 +                                       sizeof(struct ext3_extent_idx));
704 +                       neh->e_num++;
705 +                       EXT_ASSERT(neh->e_num <= neh->e_max);
706 +                       m++;
707 +               }
708 +               mark_buffer_uptodate(bh, 1);
709 +               unlock_buffer(bh);
710 +
711 +               if ((err = ext3_journal_dirty_metadata(handle, bh)))
712 +                       goto cleanup;
713 +               brelse(bh);
714 +               bh = NULL;
715 +
716 +               /* correct old index */
717 +               if (m) {
718 +                       err = ext3_ext_get_access(handle, tree, path + i);
719 +                       if (err)
720 +                               goto cleanup;
721 +                       path[i].p_hdr->e_num -= m;
722 +                       err = ext3_ext_dirty(handle, tree, path + i);
723 +                       if (err)
724 +                               goto cleanup;
725 +               }
726 +
727 +               i--;
728 +       }
729 +
730 +       /* insert new index */
731 +       if (!err)
732 +               err = ext3_ext_insert_index(handle, tree, path + at,
733 +                                               border, newblock);
734 +
735 +cleanup:
736 +       if (bh) {
737 +               if (buffer_locked(bh))
738 +                       unlock_buffer(bh);
739 +               brelse(bh);
740 +       }
741 +
742 +       if (err) {
743 +               /* free all allocated blocks in error case */
744 +               for (i = 0; i < depth; i++)
745 +                       if (!ablocks[i])
746 +                               continue;
747 +                       ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
748 +       }
749 +       kfree(ablocks);
750 +
751 +       return err;
752 +}
753 +
754 +/*
755 + * routine implements tree growing procedure:
756 + *  - allocates new block
757 + *  - moves top-level data (index block or leaf) into the new block
758 + *  - initialize new top-level, creating index that points to the
759 + *    just created block
760 + */
761 +static int ext3_ext_grow_indepth(handle_t *handle,
762 +                                       struct ext3_extents_tree *tree,
763 +                                       struct ext3_ext_path *path,
764 +                                       struct ext3_extent *newext)
765 +{
766 +       struct ext3_ext_path *curp = path;
767 +       struct ext3_extent_header *neh;
768 +       struct ext3_extent_idx *fidx;
769 +       struct buffer_head *bh;
770 +       unsigned long newblock;
771 +       int err = 0;
772 +
773 +       newblock = ext3_ext_new_block(handle, tree, path, newext, &err);
774 +       if (newblock == 0)
775 +               return err;
776 +
777 +       bh = sb_getblk(tree->inode->i_sb, newblock);
778 +       if (!bh) {
779 +               err = -EIO;
780 +               ext3_std_error(tree->inode->i_sb, err);
781 +               return err;
782 +       }
783 +       lock_buffer(bh);
784 +
785 +       if ((err = ext3_journal_get_create_access(handle, bh))) {
786 +               unlock_buffer(bh);
787 +               goto out;       
788 +       }
789 +
790 +       /* move top-level index/leaf into new block */
791 +       memmove(bh->b_data, curp->p_hdr, tree->buffer_len);
792 +
793 +       /* set size of new block */
794 +       neh = EXT_BLOCK_HDR(bh);
795 +       /* old root could have indexes or leaves
796 +        * so calculate e_max right way */
797 +       if (EXT_DEPTH(tree))
798 +               neh->e_max = ext3_ext_space_block_idx(tree);
799 +       else
800 +               neh->e_max = ext3_ext_space_block(tree);
801 +       neh->e_magic = EXT3_EXT_MAGIC;
802 +       mark_buffer_uptodate(bh, 1);
803 +       unlock_buffer(bh);
804 +
805 +       if ((err = ext3_journal_dirty_metadata(handle, bh)))
806 +               goto out;
807 +
808 +       /* create index in new top-level index: num,max,pointer */
809 +       if ((err = ext3_ext_get_access(handle, tree, curp)))
810 +               goto out;
811 +
812 +       curp->p_hdr->e_magic = EXT3_EXT_MAGIC;
813 +       curp->p_hdr->e_max = ext3_ext_space_root_idx(tree);
814 +       curp->p_hdr->e_num = 1;
815 +       curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
816 +       /* FIXME: it works, but actually path[0] can be index */
817 +       curp->p_idx->e_block = EXT_FIRST_EXTENT(path[0].p_hdr)->e_block;
818 +       curp->p_idx->e_leaf = newblock;
819 +
820 +       neh = EXT_ROOT_HDR(tree);
821 +       fidx = EXT_FIRST_INDEX(neh);
822 +       ext_debug(tree, "new root: num %d(%d), lblock %d, ptr %d\n",
823 +                       neh->e_num, neh->e_max, fidx->e_block, fidx->e_leaf); 
824 +
825 +       neh->e_depth = path->p_depth + 1;
826 +       err = ext3_ext_dirty(handle, tree, curp);
827 +out:
828 +       brelse(bh);
829 +
830 +       return err;
831 +}
832 +
833 +/*
834 + * routine finds empty index and adds new leaf. if no free index found
835 + * then it requests in-depth growing
836 + */
837 +static int ext3_ext_create_new_leaf(handle_t *handle,
838 +                                       struct ext3_extents_tree *tree,
839 +                                       struct ext3_ext_path *path,
840 +                                       struct ext3_extent *newext)
841 +{
842 +       struct ext3_ext_path *curp;
843 +       int depth, i, err = 0;
844 +
845 +repeat:
846 +       i = depth = EXT_DEPTH(tree);
847 +       
848 +       /* walk up to the tree and look for free index entry */
849 +       curp = path + depth;
850 +       while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
851 +               i--;
852 +               curp--;
853 +       }
854 +
855 +       /* we use already allocated block for index block
856 +        * so, subsequent data blocks should be contigoues */
857 +       if (EXT_HAS_FREE_INDEX(curp)) {
858 +               /* if we found index with free entry, then use that
859 +                * entry: create all needed subtree and add new leaf */
860 +               err = ext3_ext_split(handle, tree, path, newext, i);
861 +
862 +               /* refill path */
863 +               ext3_ext_drop_refs(path);
864 +               path = ext3_ext_find_extent(tree, newext->e_block, path);
865 +               if (IS_ERR(path))
866 +                       err = PTR_ERR(path);
867 +       } else {
868 +               /* tree is full, time to grow in depth */
869 +               err = ext3_ext_grow_indepth(handle, tree, path, newext);
870 +
871 +               /* refill path */
872 +               ext3_ext_drop_refs(path);
873 +               path = ext3_ext_find_extent(tree, newext->e_block, path);
874 +               if (IS_ERR(path))
875 +                       err = PTR_ERR(path);
876 +
877 +               /*
878 +                * only first (depth 0 -> 1) produces free space
879 +                * in all other cases we have to split growed tree
880 +                */
881 +               depth = EXT_DEPTH(tree);
882 +               if (path[depth].p_hdr->e_num == path[depth].p_hdr->e_max) {
883 +                       /* now we need split */
884 +                       goto repeat;
885 +               }
886 +       }
887 +
888 +       if (err)
889 +               return err;
890 +
891 +       return 0;
892 +}
893 +
894 +/*
895 + * returns allocated block in subsequent extent or 0xffffffff
896 + * NOTE: it consider block number from index entry as
897 + * allocated block. thus, index entries have to be consistent
898 + * with leafs
899 + */
900 +static unsigned long
901 +ext3_ext_next_allocated_block(struct ext3_ext_path *path)
902 +{
903 +       int depth;
904 +
905 +       EXT_ASSERT(path != NULL);
906 +       depth = path->p_depth;
907 +
908 +       if (depth == 0 && path->p_ext == NULL)
909 +               return 0xffffffff;
910 +
911 +       /* FIXME: what if index isn't full ?! */
912 +       while (depth >= 0) {
913 +               if (depth == path->p_depth) {
914 +                       /* leaf */
915 +                       if (path[depth].p_ext !=
916 +                                       EXT_LAST_EXTENT(path[depth].p_hdr))
917 +                               return path[depth].p_ext[1].e_block;
918 +               } else {
919 +                       /* index */
920 +                       if (path[depth].p_idx !=
921 +                                       EXT_LAST_INDEX(path[depth].p_hdr))
922 +                               return path[depth].p_idx[1].e_block;
923 +               }
924 +               depth--;        
925 +       }
926 +
927 +       return 0xffffffff;
928 +}
929 +
930 +/*
931 + * returns first allocated block from next leaf or 0xffffffff
932 + */
933 +static unsigned ext3_ext_next_leaf_block(struct ext3_extents_tree *tree,
934 +                                               struct ext3_ext_path *path)
935 +{
936 +       int depth;
937 +
938 +       EXT_ASSERT(path != NULL);
939 +       depth = path->p_depth;
940 +
941 +       /* zero-tree has no leaf blocks at all */
942 +       if (depth == 0)
943 +               return 0xffffffff;
944 +
945 +       /* go to index block */
946 +       depth--;
947 +       
948 +       while (depth >= 0) {
949 +               if (path[depth].p_idx !=
950 +                               EXT_LAST_INDEX(path[depth].p_hdr))
951 +                       return path[depth].p_idx[1].e_block;
952 +               depth--;        
953 +       }
954 +
955 +       return 0xffffffff;
956 +}
957 +
958 +/*
959 + * if leaf gets modified and modified extent is first in the leaf
960 + * then we have to correct all indexes above
961 + * TODO: do we need to correct tree in all cases?
962 + */
963 +int ext3_ext_correct_indexes(handle_t *handle, struct ext3_extents_tree *tree,
964 +                               struct ext3_ext_path *path)
965 +{
966 +       struct ext3_extent_header *eh;
967 +       int depth = EXT_DEPTH(tree);    
968 +       struct ext3_extent *ex;
969 +       unsigned long border;
970 +       int k, err = 0;
971 +       
972 +       eh = path[depth].p_hdr;
973 +       ex = path[depth].p_ext;
974 +       EXT_ASSERT(ex);
975 +       EXT_ASSERT(eh);
976 +       
977 +       if (depth == 0) {
978 +               /* there is no tree at all */
979 +               return 0;
980 +       }
981 +       
982 +       if (ex != EXT_FIRST_EXTENT(eh)) {
983 +               /* we correct tree if first leaf got modified only */
984 +               return 0;
985 +       }
986 +       
987 +       /*
988 +        * TODO: we need correction if border is smaller then current one
989 +        */
990 +       k = depth - 1;
991 +       border = path[depth].p_ext->e_block;
992 +       if ((err = ext3_ext_get_access(handle, tree, path + k)))
993 +               return err;
994 +       path[k].p_idx->e_block = border;
995 +       if ((err = ext3_ext_dirty(handle, tree, path + k)))
996 +               return err;
997 +
998 +       while (k--) {
999 +               /* change all left-side indexes */
1000 +               if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1001 +                       break;
1002 +               if ((err = ext3_ext_get_access(handle, tree, path + k)))
1003 +                       break;
1004 +               path[k].p_idx->e_block = border;
1005 +               if ((err = ext3_ext_dirty(handle, tree, path + k)))
1006 +                       break;
1007 +       }
1008 +
1009 +       return err;
1010 +}
1011 +
1012 +static int inline
1013 +ext3_can_extents_be_merged(struct ext3_extents_tree *tree,
1014 +                               struct ext3_extent *ex1,
1015 +                               struct ext3_extent *ex2)
1016 +{
1017 +       if (ex1->e_block + ex1->e_num != ex2->e_block)
1018 +               return 0;
1019 +
1020 +#ifdef AGRESSIVE_TEST
1021 +       if (ex1->e_num >= 4)
1022 +               return 0;
1023 +#endif
1024 +
1025 +       if (!tree->mergable)
1026 +               return 1;
1027 +
1028 +       return tree->mergable(ex1, ex2);
1029 +}
1030 +
1031 +/*
1032 + * this routine tries to merge requsted extent into the existing
1033 + * extent or inserts requested extent as new one into the tree,
1034 + * creating new leaf in no-space case
1035 + */
1036 +int ext3_ext_insert_extent(handle_t *handle, struct ext3_extents_tree *tree,
1037 +                               struct ext3_ext_path *path,
1038 +                               struct ext3_extent *newext)
1039 +{
1040 +       struct ext3_extent_header * eh;
1041 +       struct ext3_extent *ex, *fex;
1042 +       struct ext3_extent *nearex; /* nearest extent */
1043 +       struct ext3_ext_path *npath = NULL;
1044 +       int depth, len, err, next;
1045 +
1046 +       depth = EXT_DEPTH(tree);
1047 +       ex = path[depth].p_ext;
1048 +       EXT_ASSERT(path[depth].p_hdr);
1049 +
1050 +       /* try to insert block into found extent and return */
1051 +       if (ex && ext3_can_extents_be_merged(tree, ex, newext)) {
1052 +               ext_debug(tree, "append %d block to %d:%d (from %d)\n",
1053 +                               newext->e_num, ex->e_block, ex->e_num,
1054 +                               ex->e_start);
1055 +               if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1056 +                       return err;
1057 +               ex->e_num += newext->e_num;
1058 +               eh = path[depth].p_hdr;
1059 +               nearex = ex;
1060 +               goto merge;
1061 +       }
1062 +
1063 +repeat:
1064 +       depth = EXT_DEPTH(tree);
1065 +       eh = path[depth].p_hdr;
1066 +       if (eh->e_num < eh->e_max)
1067 +               goto has_space;
1068 +
1069 +       /* probably next leaf has space for us? */
1070 +       fex = EXT_LAST_EXTENT(eh);
1071 +       next = ext3_ext_next_leaf_block(tree, path);
1072 +       if (newext->e_block > fex->e_block && next != 0xffffffff) {
1073 +               ext_debug(tree, "next leaf block - %d\n", next);
1074 +               EXT_ASSERT(!npath);
1075 +               npath = ext3_ext_find_extent(tree, next, NULL);
1076 +               if (IS_ERR(npath))
1077 +                       return PTR_ERR(npath);
1078 +               EXT_ASSERT(npath->p_depth == path->p_depth);
1079 +               eh = npath[depth].p_hdr;
1080 +               if (eh->e_num < eh->e_max) {
1081 +                       ext_debug(tree, "next leaf isnt full(%d)\n",
1082 +                                       eh->e_num);
1083 +                       path = npath;
1084 +                       goto repeat;
1085 +               }
1086 +               ext_debug(tree, "next leaf hasno free space(%d,%d)\n",
1087 +                               eh->e_num, eh->e_max);
1088 +       }
1089 +
1090 +       /*
1091 +        * there is no free space in found leaf
1092 +        * we're gonna add new leaf in the tree
1093 +        */
1094 +       err = ext3_ext_create_new_leaf(handle, tree, path, newext);
1095 +       if (err)
1096 +               goto cleanup;
1097 +       depth = EXT_DEPTH(tree);
1098 +       eh = path[depth].p_hdr;
1099 +
1100 +has_space:
1101 +       nearex = path[depth].p_ext;
1102 +
1103 +       if ((err = ext3_ext_get_access(handle, tree, path + depth)))
1104 +               goto cleanup;
1105 +
1106 +       if (!nearex) {
1107 +               /* there is no extent in this leaf, create first one */
1108 +               ext_debug(tree, "first extent in the leaf: %d:%d:%d\n",
1109 +                               newext->e_block, newext->e_start,
1110 +                               newext->e_num);
1111 +               path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1112 +       } else if (newext->e_block > nearex->e_block) {
1113 +               EXT_ASSERT(newext->e_block != nearex->e_block);
1114 +               if (nearex != EXT_LAST_EXTENT(eh)) {
1115 +                       len = EXT_MAX_EXTENT(eh) - nearex;
1116 +                       len = (len - 1) * sizeof(struct ext3_extent);
1117 +                       len = len < 0 ? 0 : len;
1118 +                       ext_debug(tree, "insert %d:%d:%d after: nearest 0x%p, "
1119 +                                       "move %d from 0x%p to 0x%p\n",
1120 +                                       newext->e_block, newext->e_start,
1121 +                                       newext->e_num,
1122 +                                       nearex, len, nearex + 1, nearex + 2);
1123 +                       memmove(nearex + 2, nearex + 1, len);
1124 +               }
1125 +               path[depth].p_ext = nearex + 1;
1126 +       } else {
1127 +               EXT_ASSERT(newext->e_block != nearex->e_block);
1128 +               len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1129 +               len = len < 0 ? 0 : len;
1130 +               ext_debug(tree, "insert %d:%d:%d before: nearest 0x%p, "
1131 +                               "move %d from 0x%p to 0x%p\n",
1132 +                               newext->e_block, newext->e_start, newext->e_num,
1133 +                               nearex, len, nearex + 1, nearex + 2);
1134 +               memmove(nearex + 1, nearex, len);
1135 +               path[depth].p_ext = nearex;
1136 +       }
1137 +
1138 +       eh->e_num++;
1139 +       nearex = path[depth].p_ext;
1140 +       nearex->e_block = newext->e_block;
1141 +       nearex->e_start = newext->e_start;
1142 +       nearex->e_num = newext->e_num;
1143 +
1144 +merge:
1145 +       /* try to merge extents to the right */
1146 +       while (nearex < EXT_LAST_EXTENT(eh)) {
1147 +               if (!ext3_can_extents_be_merged(tree, nearex, nearex + 1))
1148 +                       break;
1149 +               /* merge with next extent! */
1150 +               nearex->e_num += nearex[1].e_num;
1151 +               if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1152 +                       len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1153 +                                       * sizeof(struct ext3_extent);
1154 +                       memmove(nearex + 1, nearex + 2, len);
1155 +               }
1156 +               eh->e_num--;
1157 +               EXT_ASSERT(eh->e_num > 0);
1158 +       }
1159 +
1160 +       /* try to merge extents to the left */
1161 +
1162 +       /* time to correct all indexes above */
1163 +       err = ext3_ext_correct_indexes(handle, tree, path);
1164 +       if (err)
1165 +               goto cleanup;
1166 +
1167 +       err = ext3_ext_dirty(handle, tree, path + depth);
1168 +
1169 +cleanup:
1170 +       if (npath) {
1171 +               ext3_ext_drop_refs(npath);
1172 +               kfree(npath);
1173 +       }
1174 +       ext3_ext_tree_changed(tree);
1175 +       return err;
1176 +}
1177 +
1178 +int ext3_ext_walk_space(struct ext3_extents_tree *tree, unsigned long block,
1179 +                       unsigned long num, ext_prepare_callback func)
1180 +{
1181 +       struct ext3_ext_path *path = NULL;
1182 +       struct ext3_extent *ex, cbex;
1183 +       unsigned long next, start = 0, end = 0;
1184 +       unsigned long last = block + num;
1185 +       int depth, exists, err = 0;
1186 +
1187 +       EXT_ASSERT(tree);
1188 +       EXT_ASSERT(func);
1189 +       EXT_ASSERT(tree->inode);
1190 +       EXT_ASSERT(tree->root);
1191 +
1192 +       while (block < last && block != 0xfffffffff) {
1193 +               num = last - block;
1194 +               /* find extent for this block */
1195 +               path = ext3_ext_find_extent(tree, block, path);
1196 +               if (IS_ERR(path)) {
1197 +                       err = PTR_ERR(path);
1198 +                       path = NULL;
1199 +                       break;
1200 +               }
1201 +
1202 +               depth = EXT_DEPTH(tree);
1203 +               EXT_ASSERT(path[depth].p_hdr);
1204 +               ex = path[depth].p_ext;
1205 +               next = ext3_ext_next_allocated_block(path);
1206 +
1207 +               exists = 0;
1208 +               if (!ex) {
1209 +                       /* there is no extent yet, so try to allocate
1210 +                        * all requested space */
1211 +                       start = block;
1212 +                       end = block + num;
1213 +               } else if (ex->e_block > block) {
1214 +                       /* need to allocate space before found extent */
1215 +                       start = block;
1216 +                       end = ex->e_block;
1217 +                       if (block + num < end)
1218 +                               end = block + num;
1219 +               } else if (block >= ex->e_block + ex->e_num) {
1220 +                       /* need to allocate space after found extent */
1221 +                       start = block;
1222 +                       end = block + num;
1223 +                       if (end >= next)
1224 +                               end = next;
1225 +               } else if (block >= ex->e_block) {
1226 +                       /* 
1227 +                        * some part of requested space is covered
1228 +                        * by found extent
1229 +                        */
1230 +                       start = block;
1231 +                       end = ex->e_block + ex->e_num;
1232 +                       if (block + num < end)
1233 +                               end = block + num;
1234 +                       exists = 1;
1235 +               } else {
1236 +                       BUG();
1237 +               }
1238 +               EXT_ASSERT(end > start);
1239 +
1240 +               if (!exists) {
1241 +                       cbex.e_block = start;
1242 +                       cbex.e_num = end - start;
1243 +                       cbex.e_start = 0;
1244 +               } else
1245 +                       cbex = *ex;
1246 +
1247 +               EXT_ASSERT(path[depth].p_hdr);
1248 +               err = func(tree, path, &cbex, exists);
1249 +               ext3_ext_drop_refs(path);
1250 +
1251 +               if (err < 0)
1252 +                       break;
1253 +               if (err == EXT_REPEAT)
1254 +                       continue;
1255 +               else if (err == EXT_BREAK) {
1256 +                       err = 0;
1257 +                       break;
1258 +               }
1259 +
1260 +               if (EXT_DEPTH(tree) != depth) {
1261 +                       /* depth was changed. we have to realloc path */
1262 +                       kfree(path);
1263 +                       path = NULL;
1264 +               }
1265 +
1266 +               block = cbex.e_block + cbex.e_num;
1267 +       }
1268 +
1269 +       if (path) {
1270 +               ext3_ext_drop_refs(path);
1271 +               kfree(path);
1272 +       }
1273 +
1274 +       return err;
1275 +}
1276 +
1277 +void ext3_ext_invalidate_cache(struct ext3_extents_tree *tree)
1278 +{
1279 +       if (tree->cex)
1280 +               tree->cex->e_num = 0;
1281 +}
1282 +
1283 +static inline void
1284 +ext3_ext_put_in_cache(struct ext3_extents_tree *tree, struct ext3_extent *ex)
1285 +{
1286 +       if (tree->cex) {
1287 +               EXT_ASSERT(ex);
1288 +               EXT_ASSERT(ex->e_num);
1289 +               tree->cex->e_block = ex->e_block;
1290 +               tree->cex->e_start = ex->e_start;
1291 +               tree->cex->e_num = ex->e_num;
1292 +       }
1293 +}
1294 +
1295 +/*
1296 + * this routine calculate boundaries of the gap requested block fits into
1297 + * and cache this gap
1298 + */
1299 +static inline void
1300 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1301 +                               struct ext3_ext_path *path,
1302 +                               unsigned long block)
1303 +{
1304 +       int depth = EXT_DEPTH(tree);
1305 +       struct ext3_extent *ex, gex;
1306 +
1307 +       if (!tree->cex)
1308 +               return;
1309 +
1310 +       ex = path[depth].p_ext;
1311 +       if (ex == NULL) {
1312 +               /* there is no extent yet, so gap is [0;-] */
1313 +               gex.e_block = 0;
1314 +               gex.e_num = 0xffffffff;
1315 +               ext_debug(tree, "cache gap(whole file):");
1316 +       } else if (block < ex->e_block) {
1317 +               gex.e_block = block;
1318 +               gex.e_num = ex->e_block - block;
1319 +               ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1320 +                               (unsigned long) block,
1321 +                               (unsigned long) ex->e_block,
1322 +                               (unsigned long) ex->e_num);
1323 +       } else if (block >= ex->e_block + ex->e_num) {
1324 +               gex.e_block = ex->e_block + ex->e_num;
1325 +               gex.e_num = ext3_ext_next_allocated_block(path);
1326 +               ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1327 +                               (unsigned long) ex->e_block,
1328 +                               (unsigned long) ex->e_num,
1329 +                               (unsigned long) block);
1330 +               EXT_ASSERT(gex.e_num > gex.e_block);
1331 +               gex.e_num = gex.e_num - gex.e_block;
1332 +       } else {
1333 +               BUG();
1334 +       }
1335 +
1336 +       ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.e_block,
1337 +                       (unsigned long) gex.e_num);
1338 +       gex.e_start = 0xffffffff;
1339 +       ext3_ext_put_in_cache(tree, &gex);
1340 +}
1341 +
1342 +static inline int
1343 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1344 +                       struct ext3_extent *ex)
1345 +{
1346 +       struct ext3_extent *cex = tree->cex;
1347 +
1348 +       /* is there cache storage at all? */
1349 +       if (!cex)
1350 +               return 0;
1351 +
1352 +       /* has cache valid data? */
1353 +       if (cex->e_num == 0)
1354 +               return 0;
1355 +
1356 +       if (block >= cex->e_block && block < cex->e_block + cex->e_num) {
1357 +               ex->e_block = cex->e_block;
1358 +               ex->e_start = cex->e_start;
1359 +               ex->e_num = cex->e_num;
1360 +               ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1361 +                               (unsigned long) block,
1362 +                               (unsigned long) ex->e_block,
1363 +                               (unsigned long) ex->e_num,
1364 +                               (unsigned long) ex->e_start);
1365 +               return 1;
1366 +       }
1367 +
1368 +       /* not in cache */
1369 +       return 0;
1370 +}
1371 +
1372 +/*
1373 + * routine removes index from the index block
1374 + * it's used in truncate case only. thus all requests are for
1375 + * last index in the block only
1376 + */
1377 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1378 +                       struct ext3_ext_path *path)
1379 +{
1380 +       struct buffer_head *bh;
1381 +       int err;
1382 +       
1383 +       /* free index block */
1384 +       path--;
1385 +       EXT_ASSERT(path->p_hdr->e_num);
1386 +       if ((err = ext3_ext_get_access(handle, tree, path)))
1387 +               return err;
1388 +       path->p_hdr->e_num--;
1389 +       if ((err = ext3_ext_dirty(handle, tree, path)))
1390 +               return err;
1391 +       ext_debug(tree, "index is empty, remove it, free block %d\n",
1392 +                       path->p_idx->e_leaf);
1393 +       bh = sb_get_hash_table(tree->inode->i_sb, path->p_idx->e_leaf);
1394 +       ext3_forget(handle, 1, tree->inode, bh, path->p_idx->e_leaf);
1395 +       ext3_free_blocks(handle, tree->inode, path->p_idx->e_leaf, 1);
1396 +       return err;
1397 +}
1398 +
1399 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1400 +                                       struct ext3_ext_path *path)
1401 +{
1402 +       int depth = EXT_DEPTH(tree);
1403 +       int needed;
1404 +
1405 +       if (path) {
1406 +               /* probably there is space in leaf? */
1407 +               if (path[depth].p_hdr->e_num < path[depth].p_hdr->e_max)
1408 +                       return 1;
1409 +       }
1410 +       
1411 +       /*
1412 +        * the worste case we're expecting is creation of the
1413 +        * new root (growing in depth) with index splitting
1414 +        * for splitting we have to consider depth + 1 because
1415 +        * previous growing could increase it
1416 +        */
1417 +       depth = depth + 1;
1418 +
1419 +       /* 
1420 +        * growing in depth:
1421 +        * block allocation + new root + old root
1422 +        */
1423 +       needed = EXT3_ALLOC_NEEDED + 2;
1424 +
1425 +       /* index split. we may need:
1426 +        *   allocate intermediate indexes and new leaf
1427 +        *   change two blocks at each level, but root
1428 +        *   modify root block (inode)
1429 +        */
1430 +       needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1431 +
1432 +       return needed;
1433 +}
1434 +
1435 +static int
1436 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1437 +                       struct ext3_ext_path *path, unsigned long start,
1438 +                       unsigned long end)
1439 +{
1440 +       struct ext3_extent *ex, tex;
1441 +       struct ext3_ext_path *npath;
1442 +       int depth, creds, err;
1443 +
1444 +       depth = EXT_DEPTH(tree);
1445 +       ex = path[depth].p_ext;
1446 +       EXT_ASSERT(ex);
1447 +       EXT_ASSERT(end < ex->e_block + ex->e_num - 1);
1448 +       EXT_ASSERT(ex->e_block < start);
1449 +
1450 +       /* calculate tail extent */
1451 +       tex.e_block = end + 1;
1452 +       EXT_ASSERT(tex.e_block < ex->e_block + ex->e_num);
1453 +       tex.e_num = ex->e_block + ex->e_num - tex.e_block;
1454 +
1455 +       creds = ext3_ext_calc_credits_for_insert(tree, path);
1456 +       handle = ext3_ext_journal_restart(handle, creds);
1457 +       if (IS_ERR(handle))
1458 +               return PTR_ERR(handle);
1459 +       
1460 +       /* calculate head extent. use primary extent */
1461 +       err = ext3_ext_get_access(handle, tree, path + depth);
1462 +       if (err)
1463 +               return err;
1464 +       ex->e_num = start - ex->e_block;
1465 +       err = ext3_ext_dirty(handle, tree, path + depth);
1466 +       if (err)
1467 +               return err;
1468 +
1469 +       /* FIXME: some callback to free underlying resource
1470 +        * and correct e_start? */
1471 +       ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1472 +                       ex->e_block, ex->e_num, tex.e_block, tex.e_num);
1473 +
1474 +       npath = ext3_ext_find_extent(tree, ex->e_block, NULL);
1475 +       if (IS_ERR(npath))
1476 +               return PTR_ERR(npath);
1477 +       depth = EXT_DEPTH(tree);
1478 +       EXT_ASSERT(npath[depth].p_ext->e_block == ex->e_block);
1479 +       EXT_ASSERT(npath[depth].p_ext->e_num == ex->e_num);
1480 +
1481 +       err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1482 +       ext3_ext_drop_refs(npath);
1483 +       kfree(npath);
1484 +
1485 +       return err;
1486 +                       
1487 +}
1488 +
1489 +static int
1490 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1491 +                       struct ext3_ext_path *path, unsigned long start,
1492 +                       unsigned long end)
1493 +{
1494 +       struct ext3_extent *ex, *fu = NULL, *lu, *le;
1495 +       int err = 0, correct_index = 0;
1496 +       int depth = EXT_DEPTH(tree), credits;
1497 +       struct ext3_extent_header *eh;
1498 +       unsigned a, b, block, num;
1499 +
1500 +       ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1501 +       if (!path[depth].p_hdr)
1502 +               path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1503 +       eh = path[depth].p_hdr;
1504 +       EXT_ASSERT(eh);
1505 +       EXT_ASSERT(eh->e_num <= eh->e_max);
1506 +       EXT_ASSERT(eh->e_magic == EXT3_EXT_MAGIC);
1507 +       
1508 +       /* find where to start removing */
1509 +       le = ex = EXT_LAST_EXTENT(eh);
1510 +       while (ex != EXT_FIRST_EXTENT(eh)) {
1511 +               if (ex->e_block <= end)
1512 +                       break;
1513 +               ex--;
1514 +       }
1515 +
1516 +       if (start > ex->e_block && end < ex->e_block + ex->e_num - 1) {
1517 +               /* removal of internal part of the extent requested
1518 +                * tail and head must be placed in different extent
1519 +                * so, we have to insert one more extent */
1520 +               path[depth].p_ext = ex;
1521 +               return ext3_ext_split_for_rm(handle, tree, path, start, end);
1522 +       }
1523 +       
1524 +       lu = ex;
1525 +       while (ex >= EXT_FIRST_EXTENT(eh) &&
1526 +                       ex->e_block + ex->e_num > start) {
1527 +               ext_debug(tree, "remove ext %u:%u\n", ex->e_block, ex->e_num);
1528 +               path[depth].p_ext = ex;
1529 +       
1530 +               a = ex->e_block > start ? ex->e_block : start;
1531 +               b = ex->e_block + ex->e_num - 1 < end ?
1532 +                       ex->e_block + ex->e_num - 1 : end;
1533 +               
1534 +               ext_debug(tree, "  border %u:%u\n", a, b);
1535 +
1536 +               if (a != ex->e_block && b != ex->e_block + ex->e_num - 1) {
1537 +                       block = 0;
1538 +                       num = 0;
1539 +                       BUG();
1540 +               } else if (a != ex->e_block) {
1541 +                       /* remove tail of the extent */
1542 +                       block = ex->e_block;
1543 +                       num = a - block;
1544 +               } else if (b != ex->e_block + ex->e_num - 1) {
1545 +                       /* remove head of the extent */
1546 +                       block = a;
1547 +                       num = b - a;
1548 +               } else {
1549 +                       /* remove whole extent: excelent! */
1550 +                       block = ex->e_block; 
1551 +                       num = 0;
1552 +                       EXT_ASSERT(a == ex->e_block &&
1553 +                                       b == ex->e_block + ex->e_num - 1);
1554 +               }
1555 +
1556 +               if (ex == EXT_FIRST_EXTENT(eh))
1557 +                       correct_index = 1;
1558 +
1559 +               credits = 1;
1560 +               if (correct_index)
1561 +                       credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1562 +               if (tree->remove_extent_credits)
1563 +                       credits += tree->remove_extent_credits(tree, ex, a, b);
1564 +               
1565 +               handle = ext3_ext_journal_restart(handle, credits);
1566 +               if (IS_ERR(handle)) {
1567 +                       err = PTR_ERR(handle);
1568 +                       goto out;
1569 +               }
1570 +
1571 +               err = ext3_ext_get_access(handle, tree, path + depth);
1572 +               if (err)
1573 +                       goto out;
1574 +
1575 +               if (tree->remove_extent)
1576 +                       err = tree->remove_extent(tree, ex, a, b);
1577 +               if (err)
1578 +                       goto out;
1579 +
1580 +               if (num == 0) {
1581 +                       /* this extent is removed entirely mark slot unused */
1582 +                       ex->e_start = 0;
1583 +                       eh->e_num--;
1584 +                       fu = ex;
1585 +               }
1586 +
1587 +               ex->e_block = block;
1588 +               ex->e_num = num;
1589 +
1590 +               err = ext3_ext_dirty(handle, tree, path + depth);
1591 +               if (err)
1592 +                       goto out;
1593 +
1594 +               ext_debug(tree, "new extent: %u:%u:%u\n",
1595 +                               ex->e_block, ex->e_num, ex->e_start);
1596 +               ex--;
1597 +       }
1598 +
1599 +       if (fu) {
1600 +               /* reuse unused slots */
1601 +               while (lu < le) {
1602 +                       if (lu->e_start) {
1603 +                               *fu = *lu;
1604 +                               lu->e_start = 0;
1605 +                               fu++;
1606 +                       }
1607 +                       lu++;
1608 +               }
1609 +       }
1610 +
1611 +       if (correct_index && eh->e_num)
1612 +               err = ext3_ext_correct_indexes(handle, tree, path);
1613 +
1614 +       /* if this leaf is free, then we should
1615 +        * remove it from index block above */
1616 +       if (err == 0 && eh->e_num == 0 && path[depth].p_bh != NULL)
1617 +               err = ext3_ext_rm_idx(handle, tree, path + depth);
1618 +
1619 +out:
1620 +       return err;
1621 +}
1622 +
1623 +
1624 +static struct ext3_extent_idx *
1625 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1626 +{
1627 +       struct ext3_extent_idx *ix;
1628 +       
1629 +       ix = EXT_LAST_INDEX(hdr);
1630 +       while (ix != EXT_FIRST_INDEX(hdr)) {
1631 +               if (ix->e_block <= block)
1632 +                       break;
1633 +               ix--;
1634 +       }
1635 +       return ix;
1636 +}
1637 +
1638 +/*
1639 + * returns 1 if current index have to be freed (even partial)
1640 + */
1641 +static int inline
1642 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1643 +{
1644 +       EXT_ASSERT(path->p_idx);
1645 +
1646 +       if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1647 +               return 0;
1648 +
1649 +       /*
1650 +        * if truncate on deeper level happened it it wasn't partial
1651 +        * so we have to consider current index for truncation
1652 +        */
1653 +       if (path->p_hdr->e_num == path->p_block)
1654 +               return 0;
1655 +       return 1;
1656 +}
1657 +
1658 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1659 +                               unsigned long start, unsigned long end)
1660 +{
1661 +       struct inode *inode = tree->inode;
1662 +       struct super_block *sb = inode->i_sb;
1663 +       int depth = EXT_DEPTH(tree);
1664 +       struct ext3_ext_path *path;
1665 +       handle_t *handle;
1666 +       int i = 0, err = 0;
1667 +
1668 +       ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1669 +
1670 +       /* probably first extent we're gonna free will be last in block */
1671 +       handle = ext3_journal_start(inode, depth + 1);
1672 +       if (IS_ERR(handle))
1673 +               return PTR_ERR(handle);
1674 +
1675 +       ext3_ext_invalidate_cache(tree);
1676 +
1677 +       /*
1678 +        * we start scanning from right side freeing all the blocks
1679 +        * after i_size and walking into the deep
1680 +        */
1681 +       path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1682 +       if (IS_ERR(path)) {
1683 +               ext3_error(sb, "ext3_ext_remove_space",
1684 +                               "Can't allocate path array");
1685 +               ext3_journal_stop(handle, inode);
1686 +               return -ENOMEM;
1687 +       }
1688 +       memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1689 +       path[i].p_hdr = EXT_ROOT_HDR(tree);
1690 +       
1691 +       while (i >= 0 && err == 0) {
1692 +               if (i == depth) {
1693 +                       /* this is leaf block */
1694 +                       err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1695 +                       /* root level have p_bh == NULL, brelse() eats this */
1696 +                       brelse(path[i].p_bh);
1697 +                       i--;
1698 +                       continue;
1699 +               }
1700 +               
1701 +               /* this is index block */
1702 +               if (!path[i].p_hdr) {
1703 +                       ext_debug(tree, "initialize header\n");
1704 +                       path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1705 +               }
1706 +
1707 +               EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1708 +               EXT_ASSERT(path[i].p_hdr->e_magic == EXT3_EXT_MAGIC);
1709 +               
1710 +               if (!path[i].p_idx) {
1711 +                       /* this level hasn't touched yet */
1712 +                       path[i].p_idx =
1713 +                               ext3_ext_last_covered(path[i].p_hdr, end);
1714 +                       path[i].p_block = path[i].p_hdr->e_num + 1;
1715 +                       ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1716 +                                       path[i].p_hdr, path[i].p_hdr->e_num);
1717 +               } else {
1718 +                       /* we've already was here, see at next index */
1719 +                       path[i].p_idx--;
1720 +               }
1721 +
1722 +               ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1723 +                               i, EXT_FIRST_INDEX(path[i].p_hdr),
1724 +                               path[i].p_idx);
1725 +               if (ext3_ext_more_to_rm(path + i)) {
1726 +                       /* go to the next level */
1727 +                       ext_debug(tree, "move to level %d (block %d)\n",
1728 +                                       i + 1, path[i].p_idx->e_leaf);
1729 +                       memset(path + i + 1, 0, sizeof(*path));
1730 +                       path[i+1].p_bh = sb_bread(sb, path[i].p_idx->e_leaf);
1731 +                       if (!path[i+1].p_bh) {
1732 +                               /* should we reset i_size? */
1733 +                               err = -EIO;
1734 +                               break;
1735 +                       }
1736 +                       /* put actual number of indexes to know is this
1737 +                        * number got changed at the next iteration */
1738 +                       path[i].p_block = path[i].p_hdr->e_num;
1739 +                       i++;
1740 +               } else {
1741 +                       /* we finish processing this index, go up */
1742 +                       if (path[i].p_hdr->e_num == 0 && i > 0) {
1743 +                               /* index is empty, remove it
1744 +                                * handle must be already prepared by the
1745 +                                * truncate_leaf() */
1746 +                               err = ext3_ext_rm_idx(handle, tree, path + i);
1747 +                       }
1748 +                       /* root level have p_bh == NULL, brelse() eats this */
1749 +                       brelse(path[i].p_bh);
1750 +                       i--;
1751 +                       ext_debug(tree, "return to level %d\n", i);
1752 +               }
1753 +       }
1754 +
1755 +       /* TODO: flexible tree reduction should be here */
1756 +       if (path->p_hdr->e_num == 0) {
1757 +               /*
1758 +                * truncate to zero freed all the tree
1759 +                * so, we need to correct e_depth
1760 +                */
1761 +               err = ext3_ext_get_access(handle, tree, path);
1762 +               if (err == 0) {
1763 +                       EXT_ROOT_HDR(tree)->e_depth = 0;
1764 +                       err = ext3_ext_dirty(handle, tree, path);
1765 +               }
1766 +       }
1767 +       ext3_ext_tree_changed(tree);
1768 +
1769 +       kfree(path);
1770 +       ext3_journal_stop(handle, inode);
1771 +
1772 +       return err;
1773 +}
1774 +
1775 +/*
1776 + * called at mount time
1777 + */
1778 +void ext3_ext_init(struct super_block *sb)
1779 +{
1780 +       /*
1781 +        * possible initialization would be here
1782 +        */
1783 +
1784 +       if (test_opt(sb, EXTENTS)) {
1785 +               printk("EXT3-fs: file extents enabled");
1786 +#ifdef AGRESSIVE_TEST
1787 +               printk(", agressive tests");
1788 +#endif
1789 +#ifdef CHECK_BINSEARCH
1790 +               printk(", check binsearch");
1791 +#endif
1792 +               printk("\n");
1793 +       }
1794 +}
1795 +
1796 +/*
1797 + * called at umount time
1798 + */
1799 +void ext3_ext_release(struct super_block *sb)
1800 +{
1801 +}
1802 +
1803 +/************************************************************************
1804 + * VFS related routines
1805 + ************************************************************************/
1806 +
1807 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1808 +{
1809 +       /* we use in-core data, not bh */
1810 +       return 0;
1811 +}
1812 +
1813 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1814 +{
1815 +       struct inode *inode = buffer;
1816 +       return ext3_mark_inode_dirty(handle, inode);
1817 +}
1818 +
1819 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1820 +                               struct ext3_extent *ex2)
1821 +{
1822 +       if (ex1->e_start + ex1->e_num == ex2->e_start)
1823 +               return 1;
1824 +       return 0;
1825 +}
1826 +
1827 +static int
1828 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1829 +                               struct ext3_extent *ex,
1830 +                               unsigned long from, unsigned long to)
1831 +{
1832 +       int needed;
1833 +       
1834 +       /* at present, extent can't cross block group */;
1835 +       needed = 3; /* bitmap + group desc + sb */
1836 +
1837 +#ifdef CONFIG_QUOTA
1838 +       needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1839 +#endif
1840 +       return needed;
1841 +}
1842 +
1843 +static int
1844 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1845 +                               struct ext3_extent *ex,
1846 +                               unsigned long from, unsigned long to)
1847 +{
1848 +       int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1849 +       handle_t *handle = ext3_journal_start(tree->inode, needed);
1850 +       struct buffer_head *bh;
1851 +       int i;
1852 +
1853 +       if (IS_ERR(handle))
1854 +               return PTR_ERR(handle);
1855 +       if (from >= ex->e_block && to == ex->e_block + ex->e_num - 1) {
1856 +               /* tail removal */
1857 +               unsigned long num, start;
1858 +               num = ex->e_block + ex->e_num - from;
1859 +               start = ex->e_start + ex->e_num - num;
1860 +               ext_debug(tree, "free last %lu blocks starting %lu\n",
1861 +                               num, start);
1862 +               for (i = 0; i < num; i++) {
1863 +                       bh = sb_get_hash_table(tree->inode->i_sb, start + i);
1864 +                       ext3_forget(handle, 0, tree->inode, bh, start + i);
1865 +               }
1866 +               ext3_free_blocks(handle, tree->inode, start, num);
1867 +       } else if (from == ex->e_block && to <= ex->e_block + ex->e_num - 1) {
1868 +               printk("strange request: removal %lu-%lu from %u:%u\n",
1869 +                       from, to, ex->e_block, ex->e_num);
1870 +       } else {
1871 +               printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1872 +                       from, to, ex->e_block, ex->e_num);
1873 +       }
1874 +       ext3_journal_stop(handle, tree->inode);
1875 +       return 0;
1876 +}
1877 +
1878 +int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
1879 +                       unsigned long block)
1880 +{
1881 +       struct ext3_inode_info *ei = EXT3_I(inode);
1882 +       unsigned long bg_start;
1883 +       unsigned long colour;
1884 +       int depth;
1885 +       
1886 +       if (path) {
1887 +               struct ext3_extent *ex;
1888 +               depth = path->p_depth;
1889 +               
1890 +               /* try to predict block placement */
1891 +               if ((ex = path[depth].p_ext))
1892 +                       return ex->e_start + (block - ex->e_block);
1893 +
1894 +               /* it looks index is empty
1895 +                * try to find starting from index itself */
1896 +               if (path[depth].p_bh)
1897 +                       return path[depth].p_bh->b_blocknr;
1898 +       }
1899 +
1900 +       /* OK. use inode's group */
1901 +       bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1902 +               le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1903 +       colour = (current->pid % 16) *
1904 +                       (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1905 +       return bg_start + colour + block;
1906 +}
1907 +
1908 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1909 +                               struct ext3_ext_path *path,
1910 +                               struct ext3_extent *ex, int *err)
1911 +{
1912 +       struct inode *inode = tree->inode;
1913 +       int newblock, goal;
1914 +       
1915 +       EXT_ASSERT(path);
1916 +       EXT_ASSERT(ex);
1917 +       EXT_ASSERT(ex->e_start);
1918 +       EXT_ASSERT(ex->e_num);
1919 +       
1920 +       /* reuse block from the extent to order data/metadata */
1921 +       newblock = ex->e_start++;
1922 +       ex->e_num--;
1923 +       if (ex->e_num == 0) {
1924 +               ex->e_num = 1;
1925 +               /* allocate new block for the extent */
1926 +               goal = ext3_ext_find_goal(inode, path, ex->e_block);
1927 +               ex->e_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1928 +               if (ex->e_start == 0) {
1929 +                       /* error occured: restore old extent */
1930 +                       ex->e_start = newblock;
1931 +                       return 0;
1932 +               }
1933 +       }
1934 +       return newblock;
1935 +}
1936 +
1937 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1938 +                               struct inode *inode)
1939 +{
1940 +       tree->inode = inode;
1941 +       tree->root = (void *) EXT3_I(inode)->i_data;
1942 +       tree->get_write_access = ext3_get_inode_write_access;
1943 +       tree->mark_buffer_dirty = ext3_mark_buffer_dirty;
1944 +       tree->mergable = ext3_ext_mergable;
1945 +       tree->new_block = ext3_new_block_cb;
1946 +       tree->remove_extent = ext3_remove_blocks;
1947 +       tree->remove_extent_credits = ext3_remove_blocks_credits;
1948 +       tree->buffer = (void *) inode;
1949 +       tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1950 +       tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1951 +}
1952 +
1953 +#if EXT3_MULTIBLOCK_ALLOCATOR
1954 +static int
1955 +ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
1956 +                       struct ext3_ext_path *path,
1957 +                       struct ext3_extent *newex, int exist)
1958 +{
1959 +       struct inode *inode = tree->inode;
1960 +       struct buffer_head *bh;
1961 +       int count, err, goal;
1962 +       unsigned long pblock;
1963 +       unsigned long tgen;
1964 +       loff_t new_i_size;
1965 +       handle_t *handle;
1966 +       int i;
1967 +
1968 +       if (exist)
1969 +               return EXT_CONTINUE;
1970 +
1971 +       tgen = EXT_GENERATION(tree);
1972 +       count = ext3_ext_calc_credits_for_insert(tree, path);
1973 +       up_write(&EXT3_I(inode)->truncate_sem);
1974 +
1975 +       handle = ext3_journal_start(inode, count + EXT3_ALLOC_NEEDED + 1);
1976 +       if (IS_ERR(handle)) {
1977 +               down_write(&EXT3_I(inode)->truncate_sem);
1978 +               return PTR_ERR(handle);
1979 +       }
1980 +
1981 +       if (tgen != EXT_GENERATION(tree)) {
1982 +               /* the tree has changed. so path can be invalid at moment */
1983 +               ext3_journal_stop(handle, inode);
1984 +               down_write(&EXT3_I(inode)->truncate_sem);
1985 +               return EXT_REPEAT;
1986 +       }
1987 +
1988 +       down_write(&EXT3_I(inode)->truncate_sem);
1989 +       goal = ext3_ext_find_goal(inode, path, newex->e_block);
1990 +       count = newex->e_num;
1991 +       pblock = ext3_new_blocks(handle, inode, &count, goal, &err);
1992 +       if (!pblock)
1993 +               goto out;
1994 +       EXT_ASSERT(count <= newex->e_num);
1995 +
1996 +       /* insert new extent */
1997 +       newex->e_start = pblock;
1998 +       newex->e_num = count;
1999 +       err = ext3_ext_insert_extent(handle, tree, path, newex);
2000 +       if (err)
2001 +               goto out;
2002 +
2003 +       /* block have been allocated for data, so time to drop dirty
2004 +        * in correspondend buffer_heads to prevent corruptions */
2005 +       for (i = 0; i < newex->e_num; i++) {
2006 +               bh = sb_get_hash_table(inode->i_sb, newex->e_start + i);
2007 +               if (bh) {
2008 +                       mark_buffer_clean(bh);
2009 +                       wait_on_buffer(bh);
2010 +                       clear_bit(BH_Req, &bh->b_state);
2011 +                       __brelse(bh);
2012 +               }
2013 +       }
2014 +
2015 +       /* correct on-disk inode size */
2016 +       if (newex->e_num > 0) {
2017 +               new_i_size = (loff_t) newex->e_block + newex->e_num;
2018 +               new_i_size = new_i_size << inode->i_blkbits;
2019 +               if (new_i_size > EXT3_I(inode)->i_disksize) {
2020 +                       EXT3_I(inode)->i_disksize = new_i_size;
2021 +                       err = ext3_mark_inode_dirty(handle, inode);
2022 +               }
2023 +       }
2024 +
2025 +out:
2026 +       ext3_journal_stop(handle, inode);
2027 +       return err;
2028 +}
2029 +
2030 +
2031 +int ext3_ext_allocate_nblocks(struct inode *inode, unsigned long block,
2032 +                               unsigned long num)
2033 +{
2034 +       struct ext3_extents_tree tree;
2035 +       int err;
2036 +
2037 +       ext3_init_tree_desc(&tree, inode);
2038 +       ext_debug(&tree, "blocks %lu-%lu requested for inode %u\n",
2039 +                       block, block + num,(unsigned) inode->i_ino);
2040 +       down_write(&EXT3_I(inode)->truncate_sem);
2041 +       err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
2042 +       ext3_ext_invalidate_cache(&tree);
2043 +       up_write(&EXT3_I(inode)->truncate_sem);
2044 +
2045 +       return err;
2046 +}
2047 +#endif
2048 +
2049 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
2050 +                       long iblock, struct buffer_head *bh_result, int create)
2051 +{
2052 +       struct ext3_ext_path *path = NULL;
2053 +       struct ext3_extent newex;
2054 +       struct ext3_extent *ex;
2055 +       int goal, newblock, err = 0, depth;
2056 +       struct ext3_extents_tree tree;
2057 +
2058 +       clear_bit(BH_New, &bh_result->b_state);
2059 +       ext3_init_tree_desc(&tree, inode);
2060 +       ext_debug(&tree, "block %d requested for inode %u\n",
2061 +                       (int) iblock, (unsigned) inode->i_ino);
2062 +       down_write(&EXT3_I(inode)->truncate_sem);
2063 +
2064 +       /* check in cache */
2065 +       if (ext3_ext_in_cache(&tree, iblock, &newex)) {
2066 +               if (newex.e_start == 0xffffffff && !create) {
2067 +                       /* block isn't allocated yet and
2068 +                        * user don't want to allocate it */
2069 +                       goto out2;
2070 +               } else if (newex.e_start) {
2071 +                       /* block is already allocated */
2072 +                       newblock = iblock - newex.e_block + newex.e_start;
2073 +                       goto out;
2074 +               }
2075 +       }
2076 +
2077 +       /* find extent for this block */
2078 +       path = ext3_ext_find_extent(&tree, iblock, NULL);
2079 +       if (IS_ERR(path)) {
2080 +               err = PTR_ERR(path);
2081 +               path = NULL;
2082 +               goto out2;
2083 +       }
2084 +
2085 +       depth = EXT_DEPTH(&tree);
2086 +
2087 +       /*
2088 +        * consistent leaf must not be empty
2089 +        * this situations is possible, though, _during_ tree modification
2090 +        * this is why assert can't be put in ext3_ext_find_extent()
2091 +        */
2092 +       EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2093 +
2094 +       if ((ex = path[depth].p_ext)) {
2095 +               /* if found exent covers block, simple return it */
2096 +               if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
2097 +                       newblock = iblock - ex->e_block + ex->e_start;
2098 +                       ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2099 +                                       (int) iblock, ex->e_block, ex->e_num,
2100 +                                       newblock);
2101 +                       ext3_ext_put_in_cache(&tree, ex);
2102 +                       goto out;
2103 +               }
2104 +       }
2105 +
2106 +       /*
2107 +        * requested block isn't allocated yet
2108 +        * we couldn't try to create block if create flag is zero 
2109 +        */
2110 +       if (!create) {
2111 +               /* put just found gap into cache to speedup subsequest reqs */
2112 +               ext3_ext_put_gap_in_cache(&tree, path, iblock);
2113 +               goto out2;
2114 +       }
2115 +
2116 +       /* allocate new block */
2117 +       goal = ext3_ext_find_goal(inode, path, iblock);
2118 +       newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2119 +       if (!newblock)
2120 +               goto out2;
2121 +       ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2122 +                       goal, newblock);
2123 +
2124 +       /* try to insert new extent into found leaf and return */
2125 +       newex.e_block = iblock;
2126 +       newex.e_start = newblock;
2127 +       newex.e_num = 1;
2128 +       err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2129 +       if (err)
2130 +               goto out2;
2131 +       
2132 +       if (inode->i_size > EXT3_I(inode)->i_disksize)
2133 +               EXT3_I(inode)->i_disksize = inode->i_size;
2134 +
2135 +       /* previous routine could use block we allocated */
2136 +       newblock = newex.e_start;
2137 +       set_bit(BH_New, &bh_result->b_state);
2138 +
2139 +       ext3_ext_put_in_cache(&tree, &newex);
2140 +out:
2141 +       ext3_ext_show_leaf(&tree, path);
2142 +       set_bit(BH_Mapped, &bh_result->b_state);
2143 +       bh_result->b_dev = inode->i_sb->s_dev;
2144 +       bh_result->b_blocknr = newblock;
2145 +out2:
2146 +       if (path) {
2147 +               ext3_ext_drop_refs(path);
2148 +               kfree(path);
2149 +       }
2150 +       up_write(&EXT3_I(inode)->truncate_sem);
2151 +
2152 +       return err;     
2153 +}
2154 +
2155 +void ext3_ext_truncate(struct inode * inode)
2156 +{
2157 +       struct address_space *mapping = inode->i_mapping;
2158 +       struct super_block *sb = inode->i_sb;
2159 +       struct ext3_extents_tree tree;
2160 +       unsigned long last_block;
2161 +       handle_t *handle;
2162 +       int err = 0;
2163 +
2164 +       ext3_init_tree_desc(&tree, inode);
2165 +
2166 +       /*
2167 +        * probably first extent we're gonna free will be last in block
2168 +        */
2169 +       err = ext3_writepage_trans_blocks(inode) + 3;
2170 +       handle = ext3_journal_start(inode, err);
2171 +       if (IS_ERR(handle))
2172 +               return;
2173 +
2174 +       ext3_block_truncate_page(handle, mapping, inode->i_size);
2175 +
2176 +       down_write(&EXT3_I(inode)->truncate_sem);
2177 +       ext3_ext_invalidate_cache(&tree);
2178 +
2179 +       /* 
2180 +        * TODO: optimization is possible here
2181 +        * probably we need not scaning at all,
2182 +        * because page truncation is enough
2183 +        */
2184 +       if (ext3_orphan_add(handle, inode))
2185 +               goto out_stop;
2186 +
2187 +       /* we have to know where to truncate from in crash case */
2188 +       EXT3_I(inode)->i_disksize = inode->i_size;
2189 +       ext3_mark_inode_dirty(handle, inode);
2190 +
2191 +       last_block = (inode->i_size + sb->s_blocksize - 1)
2192 +                       >> EXT3_BLOCK_SIZE_BITS(sb);
2193 +       err = ext3_ext_remove_space(&tree, last_block, 0xffffffff);
2194 +       
2195 +       /* In a multi-transaction truncate, we only make the final
2196 +        * transaction synchronous */
2197 +       if (IS_SYNC(inode))
2198 +               handle->h_sync = 1;
2199 +
2200 +out_stop:
2201 +       /*
2202 +        * If this was a simple ftruncate(), and the file will remain alive
2203 +        * then we need to clear up the orphan record which we created above.
2204 +        * However, if this was a real unlink then we were called by
2205 +        * ext3_delete_inode(), and we allow that function to clean up the
2206 +        * orphan info for us.
2207 +        */
2208 +       if (inode->i_nlink)
2209 +               ext3_orphan_del(handle, inode);
2210 +
2211 +       up_write(&EXT3_I(inode)->truncate_sem);
2212 +       ext3_journal_stop(handle, inode);
2213 +}
2214 +
2215 +/*
2216 + * this routine calculate max number of blocks we could modify
2217 + * in order to allocate new block for an inode
2218 + */
2219 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2220 +{
2221 +       struct ext3_extents_tree tree;
2222 +       int needed;
2223 +       
2224 +       ext3_init_tree_desc(&tree, inode);
2225 +       
2226 +       needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2227 +
2228 +       /* caller want to allocate num blocks */
2229 +       needed *= num;
2230 +       
2231 +#ifdef CONFIG_QUOTA
2232 +       /* 
2233 +        * FIXME: real calculation should be here
2234 +        * it depends on blockmap format of qouta file
2235 +        */
2236 +       needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2237 +#endif
2238 +
2239 +       return needed;
2240 +}
2241 +
2242 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2243 +{
2244 +       struct ext3_extents_tree tree;
2245 +
2246 +       ext3_init_tree_desc(&tree, inode);
2247 +       ext3_extent_tree_init(handle, &tree);
2248 +}
2249 +
2250 +static int
2251 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2252 +                       struct ext3_ext_path *path,
2253 +                       struct ext3_extent *newex, int exist)
2254 +{
2255 +       struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2256 +
2257 +       if (!exist)
2258 +               return EXT_CONTINUE;
2259 +       if (buf->err < 0)
2260 +               return EXT_BREAK;
2261 +       if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2262 +               return EXT_BREAK;
2263 +
2264 +       if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2265 +               buf->err++;
2266 +               buf->cur += sizeof(*newex);
2267 +       } else {
2268 +               buf->err = -EFAULT;
2269 +               return EXT_BREAK;
2270 +       }
2271 +       return EXT_CONTINUE;
2272 +}
2273 +
2274 +static int
2275 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2276 +                       struct ext3_ext_path *path,
2277 +                       struct ext3_extent *ex, int exist)
2278 +{
2279 +       struct ext3_extent_tree_stats *buf =
2280 +               (struct ext3_extent_tree_stats *) tree->private;
2281 +       int depth;
2282 +
2283 +       if (!exist)
2284 +               return EXT_CONTINUE;
2285 +
2286 +       depth = EXT_DEPTH(tree);
2287 +       buf->extents_num++;
2288 +       if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2289 +               buf->leaf_num++;
2290 +       return EXT_CONTINUE;
2291 +}
2292 +
2293 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2294 +               unsigned long arg)
2295 +{
2296 +       int err = 0;
2297 +
2298 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2299 +               return -EINVAL;
2300 +
2301 +       if (cmd == EXT3_IOC_GET_EXTENTS) {
2302 +               struct ext3_extent_buf buf;
2303 +               struct ext3_extents_tree tree;
2304 +
2305 +               if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2306 +                       return -EFAULT;
2307 +
2308 +               ext3_init_tree_desc(&tree, inode);
2309 +               buf.cur = buf.buffer;
2310 +               buf.err = 0;
2311 +               tree.private = &buf;
2312 +               down_write(&EXT3_I(inode)->truncate_sem);
2313 +               err = ext3_ext_walk_space(&tree, buf.start, 0xffffffff,
2314 +                                               ext3_ext_store_extent_cb);
2315 +               up_write(&EXT3_I(inode)->truncate_sem);
2316 +               if (err == 0)
2317 +                       err = buf.err;
2318 +       } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2319 +               struct ext3_extent_tree_stats buf;
2320 +               struct ext3_extents_tree tree;
2321 +
2322 +               ext3_init_tree_desc(&tree, inode);
2323 +               down_write(&EXT3_I(inode)->truncate_sem);
2324 +               buf.depth = EXT_DEPTH(&tree);
2325 +               buf.extents_num = 0;
2326 +               buf.leaf_num = 0;
2327 +               tree.private = &buf;
2328 +               err = ext3_ext_walk_space(&tree, 0, 0xffffffff,
2329 +                                               ext3_ext_collect_stats_cb);
2330 +               up_write(&EXT3_I(inode)->truncate_sem);
2331 +               if (!err)
2332 +                       err = copy_to_user((void *) arg, &buf, sizeof(buf));
2333 +       } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2334 +               struct ext3_extents_tree tree;
2335 +               ext3_init_tree_desc(&tree, inode);
2336 +               down_write(&EXT3_I(inode)->truncate_sem);
2337 +               err = EXT_DEPTH(&tree);
2338 +               up_write(&EXT3_I(inode)->truncate_sem);
2339 +       }
2340 +
2341 +       return err;
2342 +}
2343 +
2344 +EXPORT_SYMBOL(ext3_init_tree_desc);
2345 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2346 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2347 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2348 +EXPORT_SYMBOL(ext3_ext_walk_space);
2349 +EXPORT_SYMBOL(ext3_ext_find_goal);
2350 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2351 +
2352 Index: linux-2.4.24-mb34/fs/ext3/ialloc.c
2353 ===================================================================
2354 --- linux-2.4.24-mb34.orig/fs/ext3/ialloc.c     2004-05-05 13:47:40.000000000 -0700
2355 +++ linux-2.4.24-mb34/fs/ext3/ialloc.c  2004-05-05 13:51:27.000000000 -0700
2356 @@ -592,10 +592,14 @@
2357                 iloc.bh = NULL;
2358                 goto fail;
2359         }
2360 -       err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2361 -       if (err) goto fail;
2362  
2363 +       if (test_opt(sb, EXTENTS)) {
2364 +               EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2365 +               ext3_extents_initialize_blockmap(handle, inode);
2366 +       }
2367  
2368 +       err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2369 +       if (err) goto fail;
2370         
2371         unlock_super (sb);
2372         if(DQUOT_ALLOC_INODE(inode)) {
2373 Index: linux-2.4.24-mb34/fs/ext3/inode.c
2374 ===================================================================
2375 --- linux-2.4.24-mb34.orig/fs/ext3/inode.c      2004-05-05 13:47:41.000000000 -0700
2376 +++ linux-2.4.24-mb34/fs/ext3/inode.c   2004-05-05 13:49:40.000000000 -0700
2377 @@ -848,6 +848,15 @@
2378         goto reread;
2379  }
2380  
2381 +static inline int
2382 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2383 +               struct buffer_head *bh, int create)
2384 +{
2385 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2386 +               return ext3_ext_get_block(handle, inode, block, bh, create);
2387 +       return ext3_get_block_handle(handle, inode, block, bh, create);
2388 +}
2389 +
2390  /*
2391   * The BKL is not held on entry here.
2392   */
2393 @@ -861,7 +870,7 @@
2394                 handle = ext3_journal_current_handle();
2395                 J_ASSERT(handle != 0);
2396         }
2397 -       ret = ext3_get_block_handle(handle, inode, iblock, bh_result, create);
2398 +       ret = ext3_get_block_wrap(handle, inode, iblock, bh_result, create);
2399         return ret;
2400  }
2401  
2402 @@ -879,7 +888,7 @@
2403         dummy.b_state = 0;
2404         dummy.b_blocknr = -1000;
2405         buffer_trace_init(&dummy.b_history);
2406 -       *errp = ext3_get_block_handle(handle, inode, block, &dummy, create);
2407 +       *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create);
2408         if (!*errp && buffer_mapped(&dummy)) {
2409                 struct buffer_head *bh;
2410                 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2411 @@ -1403,7 +1412,7 @@
2412   * This required during truncate. We need to physically zero the tail end
2413   * of that block so it doesn't yield old data if the file is later grown.
2414   */
2415 -static int ext3_block_truncate_page(handle_t *handle,
2416 +int ext3_block_truncate_page(handle_t *handle,
2417                 struct address_space *mapping, loff_t from)
2418  {
2419         unsigned long index = from >> PAGE_CACHE_SHIFT;
2420 @@ -1889,6 +1898,9 @@
2421  
2422         ext3_discard_prealloc(inode);
2423  
2424 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2425 +               return ext3_ext_truncate(inode);
2426 +
2427         handle = start_transaction(inode);
2428         if (IS_ERR(handle))
2429                 return;         /* AKPM: return what? */
2430 @@ -2537,6 +2549,9 @@
2431         int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2432         int ret;
2433         
2434 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2435 +               return ext3_ext_writepage_trans_blocks(inode, bpp);
2436 +
2437         if (ext3_should_journal_data(inode))
2438                 ret = 3 * (bpp + indirects) + 2;
2439         else
2440 @@ -2973,7 +2988,7 @@
2441  
2442         /* alloc blocks one by one */
2443         for (i = 0; i < nblocks; i++) {
2444 -               ret = ext3_get_block_handle(handle, inode, blocks[i],
2445 +               ret = ext3_get_block_wrap(handle, inode, blocks[i],
2446                                                 &bh_tmp, 1);
2447                 if (ret)
2448                         break;
2449 @@ -3049,7 +3064,7 @@
2450                  if (blocks[i] != 0)
2451                          continue;
2452  
2453 -                rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1);
2454 +                rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1);
2455                  if (rc) {
2456                          printk(KERN_INFO "ext3_map_inode_page: error %d "
2457                                 "allocating block %ld\n", rc, iblock);
2458 Index: linux-2.4.24-mb34/fs/ext3/Makefile
2459 ===================================================================
2460 --- linux-2.4.24-mb34.orig/fs/ext3/Makefile     2004-05-05 13:47:40.000000000 -0700
2461 +++ linux-2.4.24-mb34/fs/ext3/Makefile  2004-05-05 13:49:40.000000000 -0700
2462 @@ -13,7 +13,9 @@
2463  
2464  obj-y    := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2465                 ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2466 -               xattr_trusted.o
2467 +               xattr_trusted.o extents.o
2468 +export-objs += extents.o
2469 +
2470  obj-m    := $(O_TARGET)
2471  
2472  export-objs += xattr.o
2473 Index: linux-2.4.24-mb34/fs/ext3/super.c
2474 ===================================================================
2475 --- linux-2.4.24-mb34.orig/fs/ext3/super.c      2004-05-05 13:47:40.000000000 -0700
2476 +++ linux-2.4.24-mb34/fs/ext3/super.c   2004-05-05 13:49:40.000000000 -0700
2477 @@ -530,6 +530,7 @@
2478         int i;
2479  
2480         J_ASSERT(sbi->s_delete_inodes == 0);
2481 +       ext3_ext_release(sb);
2482         ext3_xattr_put_super(sb);
2483         journal_destroy(sbi->s_journal);
2484         if (!(sb->s_flags & MS_RDONLY)) {
2485 @@ -702,6 +703,10 @@
2486                                 return 0;
2487                         }
2488                 }
2489 +               else if (!strcmp (this_char, "extents"))
2490 +                       set_opt (*mount_options, EXTENTS);
2491 +               else if (!strcmp (this_char, "extdebug"))
2492 +                       set_opt (*mount_options, EXTDEBUG);
2493                 else if (!strcmp (this_char, "grpid") ||
2494                          !strcmp (this_char, "bsdgroups"))
2495                         set_opt (*mount_options, GRPID);
2496 @@ -1393,6 +1398,8 @@
2497                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2498                 "writeback");
2499  
2500 +       ext3_ext_init(sb);
2501 +
2502         return sb;
2503  
2504  failed_mount3:
2505 Index: linux-2.4.24-mb34/fs/ext3/ioctl.c
2506 ===================================================================
2507 --- linux-2.4.24-mb34.orig/fs/ext3/ioctl.c      2004-05-05 13:47:38.000000000 -0700
2508 +++ linux-2.4.24-mb34/fs/ext3/ioctl.c   2004-05-05 13:49:40.000000000 -0700
2509 @@ -174,6 +174,10 @@
2510                         return ret;
2511                 }
2512  #endif
2513 +       case EXT3_IOC_GET_EXTENTS:
2514 +       case EXT3_IOC_GET_TREE_STATS:
2515 +       case EXT3_IOC_GET_TREE_DEPTH:
2516 +               return ext3_ext_ioctl(inode, filp, cmd, arg);
2517         default:
2518                 return -ENOTTY;
2519         }
2520 Index: linux-2.4.24-mb34/include/linux/ext3_fs.h
2521 ===================================================================
2522 --- linux-2.4.24-mb34.orig/include/linux/ext3_fs.h      2004-05-05 13:47:40.000000000 -0700
2523 +++ linux-2.4.24-mb34/include/linux/ext3_fs.h   2004-05-05 13:49:40.000000000 -0700
2524 @@ -184,6 +184,7 @@
2525  #define EXT3_IMAGIC_FL                 0x00002000 /* AFS directory */
2526  #define EXT3_JOURNAL_DATA_FL           0x00004000 /* file data should be journaled */
2527  #define EXT3_RESERVED_FL               0x80000000 /* reserved for ext3 lib */
2528 +#define EXT3_EXTENTS_FL                        0x00080000 /* Inode uses extents */
2529  
2530  #define EXT3_FL_USER_VISIBLE           0x00005FFF /* User visible flags */
2531  #define EXT3_FL_USER_MODIFIABLE                0x000000FF /* User modifiable flags */
2532 @@ -208,6 +209,9 @@
2533  #ifdef CONFIG_JBD_DEBUG
2534  #define EXT3_IOC_WAIT_FOR_READONLY     _IOR('f', 99, long)
2535  #endif
2536 +#define        EXT3_IOC_GET_EXTENTS            _IOR('f', 5, long)
2537 +#define        EXT3_IOC_GET_TREE_DEPTH         _IOR('f', 6, long)
2538 +#define        EXT3_IOC_GET_TREE_STATS         _IOR('f', 7, long)
2539  
2540  /*
2541   * Structure of an inode on the disk
2542 @@ -327,6 +331,8 @@
2543  #define EXT3_MOUNT_IOPEN               0x8000  /* Allow access via iopen */
2544  #define EXT3_MOUNT_IOPEN_NOPRIV                0x10000 /* Make iopen world-readable */
2545  #define EXT3_MOUNT_ASYNCDEL            0x20000 /* Delayed deletion */
2546 +#define EXT3_MOUNT_EXTENTS             0x100000/* Extents support */
2547 +#define EXT3_MOUNT_EXTDEBUG            0x200000/* Extents debug */
2548  
2549  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2550  #ifndef _LINUX_EXT2_FS_H
2551 @@ -688,6 +694,7 @@
2552  extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2553  
2554  /* inode.c */
2555 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2556  extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2557  extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2558  extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2559 @@ -769,6 +776,14 @@
2560  extern struct inode_operations ext3_symlink_inode_operations;
2561  extern struct inode_operations ext3_fast_symlink_inode_operations;
2562  
2563 +/* extents.c */
2564 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2565 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2566 +                               struct buffer_head *, int);
2567 +extern void ext3_ext_truncate(struct inode *);
2568 +extern void ext3_ext_init(struct super_block *);
2569 +extern void ext3_ext_release(struct super_block *);
2570 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2571  
2572  #endif /* __KERNEL__ */
2573  
2574 Index: linux-2.4.24-mb34/include/linux/ext3_extents.h
2575 ===================================================================
2576 --- linux-2.4.24-mb34.orig/include/linux/ext3_extents.h 1969-12-31 16:00:00.000000000 -0800
2577 +++ linux-2.4.24-mb34/include/linux/ext3_extents.h      2004-05-05 14:27:50.000000000 -0700
2578 @@ -0,0 +1,219 @@
2579 +/*
2580 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
2581 + *
2582 + * This program is free software; you can redistribute it and/or modify
2583 + * it under the terms of the GNU General Public License version 2 as
2584 + * published by the Free Software Foundation.
2585 + *
2586 + * This program is distributed in the hope that it will be useful,
2587 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2588 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2589 + * GNU General Public License for more details.
2590 + *
2591 + * You should have received a copy of the GNU General Public Licens
2592 + * along with this program; if not, write to the Free Software
2593 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
2594 + */
2595 +
2596 +#ifndef _LINUX_EXT3_EXTENTS
2597 +#define _LINUX_EXT3_EXTENTS
2598 +
2599 +/*
2600 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2601 + * become very little, so index split, in-depth growing and
2602 + * other hard changes happens much more often
2603 + * this is for debug purposes only
2604 + */
2605 +#define AGRESSIVE_TEST_
2606 +
2607 +/*
2608 + * if CHECK_BINSEARCH defined, then results of binary search
2609 + * will be checked by linear search
2610 + */
2611 +#define CHECK_BINSEARCH_
2612 +
2613 +/*
2614 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2615 + * to get lots of info what's going on
2616 + */
2617 +#define EXT_DEBUG
2618 +#ifdef EXT_DEBUG
2619 +#define ext_debug(tree,fmt,a...)                       \
2620 +do {                                                   \
2621 +       if (test_opt((tree)->inode->i_sb, EXTDEBUG))    \
2622 +               printk(fmt, ##a);                       \
2623 +} while (0);
2624 +#else
2625 +#define ext_debug(tree,fmt,a...)
2626 +#endif
2627 +
2628 +/*
2629 + * if EXT_STATS is defined then stats numbers are collected
2630 + * these number will be displayed at umount time
2631 + */
2632 +#define EXT_STATS_
2633 +
2634 +
2635 +#define EXT3_ALLOC_NEEDED      3       /* block bitmap + group desc. + sb */
2636 +
2637 +/*
2638 + * ext3_inode has i_block array (total 60 bytes)
2639 + * first 4 bytes are used to store:
2640 + *  - tree depth (0 mean there is no tree yet. all extents in the inode)
2641 + *  - number of alive extents in the inode
2642 + */
2643 +
2644 +/*
2645 + * this is extent on-disk structure
2646 + * it's used at the bottom of the tree
2647 + */
2648 +struct ext3_extent {
2649 +       __u32   e_block;        /* first logical block extent covers */
2650 +       __u32   e_start;        /* first physical block extents lives */
2651 +       __u32   e_num;          /* number of blocks covered by extent */
2652 +};
2653 +
2654 +/*
2655 + * this is index on-disk structure
2656 + * it's used at all the levels, but the bottom
2657 + */
2658 +struct ext3_extent_idx {
2659 +       __u32   e_block;        /* index covers logical blocks from 'block' */
2660 +       __u32   e_leaf;         /* pointer to the physical block of the next *
2661 +                                * level. leaf or next index could bet here */
2662 +};
2663 +
2664 +/*
2665 + * each block (leaves and indexes), even inode-stored has header
2666 + */
2667 +struct ext3_extent_header {    
2668 +       __u16   e_magic;        /* probably will support different formats */   
2669 +       __u16   e_num;          /* number of valid entries */
2670 +       __u16   e_max;          /* capacity of store in entries */
2671 +       __u16   e_depth;        /* has tree real underlaying blocks? */
2672 +       __u32   e_generation;   /* generation of the tree */
2673 +};
2674 +
2675 +#define EXT3_EXT_MAGIC         0xf301
2676 +
2677 +/*
2678 + * array of ext3_ext_path contains path to some extent
2679 + * creation/lookup routines use it for traversal/splitting/etc
2680 + * truncate uses it to simulate recursive walking
2681 + */
2682 +struct ext3_ext_path {
2683 +       __u32                           p_block;
2684 +       __u16                           p_depth;
2685 +       struct ext3_extent              *p_ext;
2686 +       struct ext3_extent_idx          *p_idx;
2687 +       struct ext3_extent_header       *p_hdr;
2688 +       struct buffer_head              *p_bh;
2689 +};
2690 +
2691 +/*
2692 + * structure for external API
2693 + */
2694 +
2695 +
2696 +/*
2697 + * ext3_extents_tree is used to pass initial information
2698 + * to top-level extents API
2699 + */
2700 +struct ext3_extents_tree {
2701 +       struct inode *inode;    /* inode which tree belongs to */
2702 +       void *root;             /* ptr to data top of tree resides at */
2703 +       void *buffer;           /* will be passed as arg to ^^ routines */
2704 +       int buffer_len;
2705 +       void *private;
2706 +       struct ext3_extent *cex;/* last found extent */
2707 +       int (*get_write_access)(handle_t *h, void *buffer);
2708 +       int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2709 +       int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2710 +       int (*remove_extent_credits)(struct ext3_extents_tree *,
2711 +                                       struct ext3_extent *, unsigned long,
2712 +                                       unsigned long);
2713 +       int (*remove_extent)(struct ext3_extents_tree *,
2714 +                               struct ext3_extent *, unsigned long,
2715 +                               unsigned long);
2716 +       int (*new_block)(handle_t *, struct ext3_extents_tree *,
2717 +                               struct ext3_ext_path *, struct ext3_extent *,
2718 +                               int *);
2719 +};
2720 +
2721 +/*
2722 + * to be called by ext3_ext_walk_space()
2723 + * negative retcode - error
2724 + * positive retcode - signal for ext3_ext_walk_space(), see below
2725 + * callback must return valid extent (passed or newly created)
2726 + */
2727 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2728 +                                       struct ext3_ext_path *,
2729 +                                       struct ext3_extent *, int);
2730 +
2731 +#define EXT_CONTINUE   0
2732 +#define EXT_BREAK      1
2733 +#define EXT_REPEAT     2
2734 +
2735 +
2736 +#define EXT_FIRST_EXTENT(__hdr__) \
2737 +       ((struct ext3_extent *) (((char *) (__hdr__)) +         \
2738 +                                sizeof(struct ext3_extent_header)))
2739 +#define EXT_FIRST_INDEX(__hdr__) \
2740 +       ((struct ext3_extent_idx *) (((char *) (__hdr__)) +     \
2741 +                                    sizeof(struct ext3_extent_header)))
2742 +#define EXT_HAS_FREE_INDEX(__path__) \
2743 +       ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
2744 +#define EXT_LAST_EXTENT(__hdr__) \
2745 +       (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
2746 +#define EXT_LAST_INDEX(__hdr__) \
2747 +       (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
2748 +#define EXT_MAX_EXTENT(__hdr__) \
2749 +       (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
2750 +#define EXT_MAX_INDEX(__hdr__) \
2751 +       (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
2752 +
2753 +#define EXT_ROOT_HDR(tree) \
2754 +       ((struct ext3_extent_header *) (tree)->root)
2755 +#define EXT_BLOCK_HDR(bh) \
2756 +       ((struct ext3_extent_header *) (bh)->b_data)
2757 +#define EXT_DEPTH(_t_) \
2758 +       (((struct ext3_extent_header *)((_t_)->root))->e_depth)
2759 +#define EXT_GENERATION(_t_)    \
2760 +       (((struct ext3_extent_header *)((_t_)->root))->e_generation)
2761 +
2762 +
2763 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2764 +
2765 +
2766 +/*
2767 + * this structure is used to gather extents from the tree via ioctl
2768 + */
2769 +struct ext3_extent_buf {
2770 +       unsigned long start;
2771 +       int buflen;
2772 +       void *buffer;
2773 +       void *cur;
2774 +       int err;
2775 +};
2776 +
2777 +/*
2778 + * this structure is used to collect stats info about the tree
2779 + */
2780 +struct ext3_extent_tree_stats {
2781 +       int depth;
2782 +       int extents_num;
2783 +       int leaf_num;
2784 +};
2785 +
2786 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2787 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2788 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2789 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2790 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2791 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2792 +int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
2793 +                       unsigned long block);
2794 +void ext3_init_tree_desc(struct ext3_extents_tree *tree, struct inode *inode);
2795 +void ext3_ext_invalidate_cache(struct ext3_extents_tree *tree);
2796 +
2797 +#endif /* _LINUX_EXT3_EXTENTS */
2798 Index: linux-2.4.24-mb34/include/linux/ext3_fs_i.h
2799 ===================================================================
2800 --- linux-2.4.24-mb34.orig/include/linux/ext3_fs_i.h    2004-05-05 13:47:40.000000000 -0700
2801 +++ linux-2.4.24-mb34/include/linux/ext3_fs_i.h 2004-05-05 13:53:43.000000000 -0700
2802 @@ -76,6 +76,8 @@
2803          * by other means, so we have truncate_sem.
2804          */
2805         struct rw_semaphore truncate_sem;
2806 +
2807 +       __u32 i_cached_extent[3];
2808  };
2809  
2810  #endif /* _LINUX_EXT3_FS_I */