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