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