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