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