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