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