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