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