Whamcloud - gitweb
786ab5c1bcabd7c1e313329ab619f0d1a86ce358
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-extents-2.4.21-suse2.patch
1 Index: linux-2.4.21-suse2/fs/ext3/extents.c
2 ===================================================================
3 --- linux-2.4.21-suse2.orig/fs/ext3/extents.c   2003-01-30 13:24:37.000000000 +0300
4 +++ linux-2.4.21-suse2/fs/ext3/extents.c        2004-02-05 20:50:31.000000000 +0300
5 @@ -0,0 +1,2344 @@
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,
2048 +                       int create, int extend_disksize)
2049 +{
2050 +       struct ext3_ext_path *path = NULL;
2051 +       struct ext3_extent newex;
2052 +       struct ext3_extent *ex;
2053 +       int goal, newblock, err = 0, depth;
2054 +       struct ext3_extents_tree tree;
2055 +
2056 +       clear_bit(BH_New, &bh_result->b_state);
2057 +       ext3_init_tree_desc(&tree, inode);
2058 +       ext_debug(&tree, "block %d requested for inode %u\n",
2059 +                       (int) iblock, (unsigned) inode->i_ino);
2060 +       down_write(&EXT3_I(inode)->truncate_sem);
2061 +
2062 +       /* check in cache */
2063 +       if (ext3_ext_in_cache(&tree, iblock, &newex)) {
2064 +               if (newex.e_start == 0xffffffff && !create) {
2065 +                       /* block isn't allocated yet and
2066 +                        * user don't want to allocate it */
2067 +                       goto out2;
2068 +               } else if (newex.e_start) {
2069 +                       /* block is already allocated */
2070 +                       newblock = iblock - newex.e_block + newex.e_start;
2071 +                       goto out;
2072 +               }
2073 +       }
2074 +
2075 +       /* find extent for this block */
2076 +       path = ext3_ext_find_extent(&tree, iblock, NULL);
2077 +       if (IS_ERR(path)) {
2078 +               err = PTR_ERR(path);
2079 +               path = NULL;
2080 +               goto out2;
2081 +       }
2082 +
2083 +       depth = EXT_DEPTH(&tree);
2084 +
2085 +       /*
2086 +        * consistent leaf must not be empty
2087 +        * this situations is possible, though, _during_ tree modification
2088 +        * this is why assert can't be put in ext3_ext_find_extent()
2089 +        */
2090 +       EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2091 +
2092 +       if ((ex = path[depth].p_ext)) {
2093 +               /* if found exent covers block, simple return it */
2094 +               if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
2095 +                       newblock = iblock - ex->e_block + ex->e_start;
2096 +                       ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2097 +                                       (int) iblock, ex->e_block, ex->e_num,
2098 +                                       newblock);
2099 +                       ext3_ext_put_in_cache(&tree, ex);
2100 +                       goto out;
2101 +               }
2102 +       }
2103 +
2104 +       /*
2105 +        * requested block isn't allocated yet
2106 +        * we couldn't try to create block if create flag is zero 
2107 +        */
2108 +       if (!create) {
2109 +               /* put just found gap into cache to speedup subsequest reqs */
2110 +               ext3_ext_put_gap_in_cache(&tree, path, iblock);
2111 +               goto out2;
2112 +       }
2113 +
2114 +       /* allocate new block */
2115 +       goal = ext3_ext_find_goal(inode, path, iblock);
2116 +       newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2117 +       if (!newblock)
2118 +               goto out2;
2119 +       ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2120 +                       goal, newblock);
2121 +
2122 +       /* try to insert new extent into found leaf and return */
2123 +       newex.e_block = iblock;
2124 +       newex.e_start = newblock;
2125 +       newex.e_num = 1;
2126 +       err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2127 +       if (err)
2128 +               goto out2;
2129 +       
2130 +       if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2131 +               EXT3_I(inode)->i_disksize = inode->i_size;
2132 +
2133 +       /* previous routine could use block we allocated */
2134 +       newblock = newex.e_start;
2135 +       set_bit(BH_New, &bh_result->b_state);
2136 +
2137 +       ext3_ext_put_in_cache(&tree, &newex);
2138 +out:
2139 +       ext3_ext_show_leaf(&tree, path);
2140 +       set_bit(BH_Mapped, &bh_result->b_state);
2141 +       bh_result->b_dev = inode->i_sb->s_dev;
2142 +       bh_result->b_blocknr = newblock;
2143 +out2:
2144 +       if (path) {
2145 +               ext3_ext_drop_refs(path);
2146 +               kfree(path);
2147 +       }
2148 +       up_write(&EXT3_I(inode)->truncate_sem);
2149 +
2150 +       return err;     
2151 +}
2152 +
2153 +void ext3_ext_truncate(struct inode * inode)
2154 +{
2155 +       struct address_space *mapping = inode->i_mapping;
2156 +       struct super_block *sb = inode->i_sb;
2157 +       struct ext3_extents_tree tree;
2158 +       unsigned long last_block;
2159 +       handle_t *handle;
2160 +       int err = 0;
2161 +
2162 +       ext3_init_tree_desc(&tree, inode);
2163 +
2164 +       /*
2165 +        * probably first extent we're gonna free will be last in block
2166 +        */
2167 +       err = ext3_writepage_trans_blocks(inode) + 3;
2168 +       handle = ext3_journal_start(inode, err);
2169 +       if (IS_ERR(handle))
2170 +               return;
2171 +
2172 +       ext3_block_truncate_page(handle, mapping, inode->i_size);
2173 +
2174 +       down_write(&EXT3_I(inode)->truncate_sem);
2175 +       ext3_ext_invalidate_cache(&tree);
2176 +
2177 +       /* 
2178 +        * TODO: optimization is possible here
2179 +        * probably we need not scaning at all,
2180 +        * because page truncation is enough
2181 +        */
2182 +       if (ext3_orphan_add(handle, inode))
2183 +               goto out_stop;
2184 +
2185 +       /* we have to know where to truncate from in crash case */
2186 +       EXT3_I(inode)->i_disksize = inode->i_size;
2187 +       ext3_mark_inode_dirty(handle, inode);
2188 +
2189 +       last_block = (inode->i_size + sb->s_blocksize - 1)
2190 +                       >> EXT3_BLOCK_SIZE_BITS(sb);
2191 +       err = ext3_ext_remove_space(&tree, last_block, 0xffffffff);
2192 +       
2193 +       /* In a multi-transaction truncate, we only make the final
2194 +        * transaction synchronous */
2195 +       if (IS_SYNC(inode))
2196 +               handle->h_sync = 1;
2197 +
2198 +out_stop:
2199 +       /*
2200 +        * If this was a simple ftruncate(), and the file will remain alive
2201 +        * then we need to clear up the orphan record which we created above.
2202 +        * However, if this was a real unlink then we were called by
2203 +        * ext3_delete_inode(), and we allow that function to clean up the
2204 +        * orphan info for us.
2205 +        */
2206 +       if (inode->i_nlink)
2207 +               ext3_orphan_del(handle, inode);
2208 +
2209 +       up_write(&EXT3_I(inode)->truncate_sem);
2210 +       ext3_journal_stop(handle, inode);
2211 +}
2212 +
2213 +/*
2214 + * this routine calculate max number of blocks we could modify
2215 + * in order to allocate new block for an inode
2216 + */
2217 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2218 +{
2219 +       struct ext3_extents_tree tree;
2220 +       int needed;
2221 +       
2222 +       ext3_init_tree_desc(&tree, inode);
2223 +       
2224 +       needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2225 +
2226 +       /* caller want to allocate num blocks */
2227 +       needed *= num;
2228 +       
2229 +#ifdef CONFIG_QUOTA
2230 +       /* 
2231 +        * FIXME: real calculation should be here
2232 +        * it depends on blockmap format of qouta file
2233 +        */
2234 +       needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2235 +#endif
2236 +
2237 +       return needed;
2238 +}
2239 +
2240 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2241 +{
2242 +       struct ext3_extents_tree tree;
2243 +
2244 +       ext3_init_tree_desc(&tree, inode);
2245 +       ext3_extent_tree_init(handle, &tree);
2246 +}
2247 +
2248 +static int
2249 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2250 +                       struct ext3_ext_path *path,
2251 +                       struct ext3_extent *newex, int exist)
2252 +{
2253 +       struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2254 +
2255 +       if (!exist)
2256 +               return EXT_CONTINUE;
2257 +       if (buf->err < 0)
2258 +               return EXT_BREAK;
2259 +       if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2260 +               return EXT_BREAK;
2261 +
2262 +       if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2263 +               buf->err++;
2264 +               buf->cur += sizeof(*newex);
2265 +       } else {
2266 +               buf->err = -EFAULT;
2267 +               return EXT_BREAK;
2268 +       }
2269 +       return EXT_CONTINUE;
2270 +}
2271 +
2272 +static int
2273 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2274 +                       struct ext3_ext_path *path,
2275 +                       struct ext3_extent *ex, int exist)
2276 +{
2277 +       struct ext3_extent_tree_stats *buf =
2278 +               (struct ext3_extent_tree_stats *) tree->private;
2279 +       int depth;
2280 +
2281 +       if (!exist)
2282 +               return EXT_CONTINUE;
2283 +
2284 +       depth = EXT_DEPTH(tree);
2285 +       buf->extents_num++;
2286 +       if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2287 +               buf->leaf_num++;
2288 +       return EXT_CONTINUE;
2289 +}
2290 +
2291 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2292 +               unsigned long arg)
2293 +{
2294 +       int err = 0;
2295 +
2296 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2297 +               return -EINVAL;
2298 +
2299 +       if (cmd == EXT3_IOC_GET_EXTENTS) {
2300 +               struct ext3_extent_buf buf;
2301 +               struct ext3_extents_tree tree;
2302 +
2303 +               if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2304 +                       return -EFAULT;
2305 +
2306 +               ext3_init_tree_desc(&tree, inode);
2307 +               buf.cur = buf.buffer;
2308 +               buf.err = 0;
2309 +               tree.private = &buf;
2310 +               down_write(&EXT3_I(inode)->truncate_sem);
2311 +               err = ext3_ext_walk_space(&tree, buf.start, 0xffffffff,
2312 +                                               ext3_ext_store_extent_cb);
2313 +               up_write(&EXT3_I(inode)->truncate_sem);
2314 +               if (err == 0)
2315 +                       err = buf.err;
2316 +       } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2317 +               struct ext3_extent_tree_stats buf;
2318 +               struct ext3_extents_tree tree;
2319 +
2320 +               ext3_init_tree_desc(&tree, inode);
2321 +               down_write(&EXT3_I(inode)->truncate_sem);
2322 +               buf.depth = EXT_DEPTH(&tree);
2323 +               buf.extents_num = 0;
2324 +               buf.leaf_num = 0;
2325 +               tree.private = &buf;
2326 +               err = ext3_ext_walk_space(&tree, 0, 0xffffffff,
2327 +                                               ext3_ext_collect_stats_cb);
2328 +               up_write(&EXT3_I(inode)->truncate_sem);
2329 +               if (!err)
2330 +                       err = copy_to_user((void *) arg, &buf, sizeof(buf));
2331 +       } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2332 +               struct ext3_extents_tree tree;
2333 +               ext3_init_tree_desc(&tree, inode);
2334 +               down_write(&EXT3_I(inode)->truncate_sem);
2335 +               err = EXT_DEPTH(&tree);
2336 +               up_write(&EXT3_I(inode)->truncate_sem);
2337 +       }
2338 +
2339 +       return err;
2340 +}
2341 +
2342 +EXPORT_SYMBOL(ext3_init_tree_desc);
2343 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2344 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2345 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2346 +EXPORT_SYMBOL(ext3_ext_walk_space);
2347 +EXPORT_SYMBOL(ext3_ext_find_goal);
2348 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2349 +
2350 Index: linux-2.4.21-suse2/fs/ext3/ialloc.c
2351 ===================================================================
2352 --- linux-2.4.21-suse2.orig/fs/ext3/ialloc.c    2004-02-05 20:42:40.000000000 +0300
2353 +++ linux-2.4.21-suse2/fs/ext3/ialloc.c 2004-02-05 20:42:40.000000000 +0300
2354 @@ -592,6 +592,10 @@
2355                 iloc.bh = NULL;
2356                 goto fail;
2357         }
2358 +       if (test_opt(sb, EXTENTS)) {
2359 +               EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2360 +               ext3_extents_initialize_blockmap(handle, inode);
2361 +       }
2362         err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2363         if (err) goto fail;
2364   
2365 Index: linux-2.4.21-suse2/fs/ext3/inode.c
2366 ===================================================================
2367 --- linux-2.4.21-suse2.orig/fs/ext3/inode.c     2004-02-05 20:42:40.000000000 +0300
2368 +++ linux-2.4.21-suse2/fs/ext3/inode.c  2004-02-05 20:42:40.000000000 +0300
2369 @@ -853,6 +853,18 @@
2370         goto reread;
2371  }
2372  
2373 +static inline int
2374 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
2375 +               struct buffer_head *bh, int create, int extend_disksize)
2376 +{
2377 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2378 +               return ext3_ext_get_block(handle, inode,
2379 +                                               block, bh, create,
2380 +                                               extend_disksize);
2381 +       return ext3_get_block_handle(handle, inode,
2382 +                                       block, bh, create, extend_disksize);
2383 +}
2384 +
2385  /*
2386   * The BKL is not held on entry here.
2387   */
2388 @@ -866,7 +878,7 @@
2389                 handle = ext3_journal_current_handle();
2390                 J_ASSERT(handle != 0);
2391         }
2392 -       ret = ext3_get_block_handle(handle, inode, iblock,
2393 +       ret = ext3_get_block_wrap(handle, inode, iblock,
2394                                 bh_result, create, 1);
2395         return ret;
2396  }
2397 @@ -893,7 +905,7 @@
2398                 }
2399         }
2400         if (ret == 0)
2401 -               ret = ext3_get_block_handle(handle, inode, iblock,
2402 +               ret = ext3_get_block_wrap(handle, inode, iblock,
2403                                         bh_result, create, 0);
2404         if (ret == 0)
2405                 bh_result->b_size = (1 << inode->i_blkbits);
2406 @@ -915,7 +927,7 @@
2407         dummy.b_state = 0;
2408         dummy.b_blocknr = -1000;
2409         buffer_trace_init(&dummy.b_history);
2410 -       *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
2411 +       *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
2412         if (!*errp && buffer_mapped(&dummy)) {
2413                 struct buffer_head *bh;
2414                 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
2415 @@ -1502,7 +1514,7 @@
2416   * This required during truncate. We need to physically zero the tail end
2417   * of that block so it doesn't yield old data if the file is later grown.
2418   */
2419 -static int ext3_block_truncate_page(handle_t *handle,
2420 +int ext3_block_truncate_page(handle_t *handle,
2421                 struct address_space *mapping, loff_t from)
2422  {
2423         unsigned long index = from >> PAGE_CACHE_SHIFT;
2424 @@ -1987,6 +1999,9 @@
2425  
2426         ext3_discard_prealloc(inode);
2427  
2428 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2429 +               return ext3_ext_truncate(inode);
2430 +
2431         handle = start_transaction(inode);
2432         if (IS_ERR(handle))
2433                 return;         /* AKPM: return what? */
2434 @@ -2663,6 +2678,9 @@
2435         int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2436         int ret;
2437         
2438 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
2439 +               return ext3_ext_writepage_trans_blocks(inode, bpp);
2440 +
2441         if (ext3_should_journal_data(inode))
2442                 ret = 3 * (bpp + indirects) + 2;
2443         else
2444 @@ -3099,7 +3117,7 @@
2445  
2446         /* alloc blocks one by one */
2447         for (i = 0; i < nblocks; i++) {
2448 -               ret = ext3_get_block_handle(handle, inode, blocks[i],
2449 +               ret = ext3_get_block_wrap(handle, inode, blocks[i],
2450                                                 &bh_tmp, 1, 1);
2451                 if (ret)
2452                         break;
2453 @@ -3175,7 +3193,7 @@
2454                  if (blocks[i] != 0)
2455                          continue;
2456  
2457 -                rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1, 1);
2458 +                rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1, 1);
2459                  if (rc) {
2460                          printk(KERN_INFO "ext3_map_inode_page: error %d "
2461                                 "allocating block %ld\n", rc, iblock);
2462 Index: linux-2.4.21-suse2/fs/ext3/Makefile
2463 ===================================================================
2464 --- linux-2.4.21-suse2.orig/fs/ext3/Makefile    2004-02-05 20:42:39.000000000 +0300
2465 +++ linux-2.4.21-suse2/fs/ext3/Makefile 2004-02-05 20:43:47.000000000 +0300
2466 @@ -12,7 +12,9 @@
2467  export-objs := ext3-exports.o
2468  
2469  obj-y    := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2470 -               ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o
2471 +               ioctl.o namei.o super.o symlink.o hash.o ext3-exports.o \
2472 +               extents.o
2473 +export-objs += extents.o
2474  obj-m    := $(O_TARGET)
2475  
2476  export-objs += xattr.o
2477 Index: linux-2.4.21-suse2/fs/ext3/super.c
2478 ===================================================================
2479 --- linux-2.4.21-suse2.orig/fs/ext3/super.c     2004-02-05 20:42:40.000000000 +0300
2480 +++ linux-2.4.21-suse2/fs/ext3/super.c  2004-02-05 20:42:40.000000000 +0300
2481 @@ -624,6 +624,7 @@
2482         int i;
2483  
2484         J_ASSERT(sbi->s_delete_inodes == 0);
2485 +       ext3_ext_release(sb);
2486         ext3_xattr_put_super(sb);
2487         journal_destroy(sbi->s_journal);
2488         if (!(sb->s_flags & MS_RDONLY)) {
2489 @@ -829,6 +830,10 @@
2490                                 return 0;
2491                         }
2492                 }
2493 +               else if (!strcmp (this_char, "extents"))
2494 +                       set_opt (*mount_options, EXTENTS);
2495 +               else if (!strcmp (this_char, "extdebug"))
2496 +                       set_opt (*mount_options, EXTDEBUG);
2497                 else if (!strcmp (this_char, "grpid") ||
2498                          !strcmp (this_char, "bsdgroups"))
2499                         set_opt (*mount_options, GRPID);
2500 @@ -1523,6 +1528,8 @@
2501                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2502                 "writeback");
2503  
2504 +       ext3_ext_init(sb);
2505 +
2506         return sb;
2507  
2508  failed_mount3:
2509 Index: linux-2.4.21-suse2/fs/ext3/ioctl.c
2510 ===================================================================
2511 --- linux-2.4.21-suse2.orig/fs/ext3/ioctl.c     2004-02-05 20:42:39.000000000 +0300
2512 +++ linux-2.4.21-suse2/fs/ext3/ioctl.c  2004-02-05 20:42:40.000000000 +0300
2513 @@ -174,6 +174,10 @@
2514                         return ret;
2515                 }
2516  #endif
2517 +       case EXT3_IOC_GET_EXTENTS:
2518 +       case EXT3_IOC_GET_TREE_STATS:
2519 +       case EXT3_IOC_GET_TREE_DEPTH:
2520 +               return ext3_ext_ioctl(inode, filp, cmd, arg);
2521         default:
2522                 return -ENOTTY;
2523         }
2524 Index: linux-2.4.21-suse2/include/linux/ext3_fs.h
2525 ===================================================================
2526 --- linux-2.4.21-suse2.orig/include/linux/ext3_fs.h     2004-02-05 20:42:40.000000000 +0300
2527 +++ linux-2.4.21-suse2/include/linux/ext3_fs.h  2004-02-05 20:42:40.000000000 +0300
2528 @@ -184,6 +184,7 @@
2529  #define EXT3_IMAGIC_FL                 0x00002000 /* AFS directory */
2530  #define EXT3_JOURNAL_DATA_FL           0x00004000 /* file data should be journaled */
2531  #define EXT3_RESERVED_FL               0x80000000 /* reserved for ext3 lib */
2532 +#define EXT3_EXTENTS_FL                        0x00080000 /* Inode uses extents */
2533  
2534  #define EXT3_FL_USER_VISIBLE           0x00005FFF /* User visible flags */
2535  #define EXT3_FL_USER_MODIFIABLE                0x000000FF /* User modifiable flags */
2536 @@ -208,6 +209,9 @@
2537  #ifdef CONFIG_JBD_DEBUG
2538  #define EXT3_IOC_WAIT_FOR_READONLY     _IOR('f', 99, long)
2539  #endif
2540 +#define        EXT3_IOC_GET_EXTENTS            _IOR('f', 5, long)
2541 +#define        EXT3_IOC_GET_TREE_DEPTH         _IOR('f', 6, long)
2542 +#define        EXT3_IOC_GET_TREE_STATS         _IOR('f', 7, long)
2543  
2544  /*
2545   * Structure of an inode on the disk
2546 @@ -328,6 +332,8 @@
2547  #define EXT3_MOUNT_ASYNCDEL            0x20000 /* Delayed deletion */
2548  #define EXT3_MOUNT_IOPEN               0x40000 /* Allow access via iopen */
2549  #define EXT3_MOUNT_IOPEN_NOPRIV                0x80000 /* Make iopen world-readable */
2550 +#define EXT3_MOUNT_EXTENTS             0x100000/* Extents support */
2551 +#define EXT3_MOUNT_EXTDEBUG            0x200000/* Extents debug */
2552  
2553  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2554  #ifndef _LINUX_EXT2_FS_H
2555 @@ -689,6 +695,7 @@
2556  extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
2557  
2558  /* inode.c */
2559 +extern int ext3_block_truncate_page(handle_t *, struct address_space *, loff_t);
2560  extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2561  extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2562  extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2563 @@ -770,6 +777,14 @@
2564  extern struct inode_operations ext3_symlink_inode_operations;
2565  extern struct inode_operations ext3_fast_symlink_inode_operations;
2566  
2567 +/* extents.c */
2568 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2569 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2570 +                               struct buffer_head *, int, int);
2571 +extern void ext3_ext_truncate(struct inode *);
2572 +extern void ext3_ext_init(struct super_block *);
2573 +extern void ext3_ext_release(struct super_block *);
2574 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2575  
2576  #endif /* __KERNEL__ */
2577  
2578 Index: linux-2.4.21-suse2/include/linux/ext3_extents.h
2579 ===================================================================
2580 --- linux-2.4.21-suse2.orig/include/linux/ext3_extents.h        2003-01-30 13:24:37.000000000 +0300
2581 +++ linux-2.4.21-suse2/include/linux/ext3_extents.h     2004-02-05 20:42:40.000000000 +0300
2582 @@ -0,0 +1,216 @@
2583 +/*
2584 + * Copyright (C) 2003 Alex Tomas <alex@clusterfs.com>
2585 + *
2586 + * This program is free software; you can redistribute it and/or modify
2587 + * it under the terms of the GNU General Public License version 2 as
2588 + * published by the Free Software Foundation.
2589 + *
2590 + * This program is distributed in the hope that it will be useful,
2591 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2592 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2593 + * GNU General Public License for more details.
2594 + *
2595 + * You should have received a copy of the GNU General Public Licens
2596 + * along with this program; if not, write to the Free Software
2597 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
2598 + */
2599 +
2600 +#ifndef _LINUX_EXT3_EXTENTS
2601 +#define _LINUX_EXT3_EXTENTS
2602 +
2603 +/*
2604 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2605 + * become very little, so index split, in-depth growing and
2606 + * other hard changes happens much more often
2607 + * this is for debug purposes only
2608 + */
2609 +#define AGRESSIVE_TEST_
2610 +
2611 +/*
2612 + * if CHECK_BINSEARCH defined, then results of binary search
2613 + * will be checked by linear search
2614 + */
2615 +#define CHECK_BINSEARCH_
2616 +
2617 +/*
2618 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2619 + * to get lots of info what's going on
2620 + */
2621 +#define EXT_DEBUG
2622 +#ifdef EXT_DEBUG
2623 +#define ext_debug(tree,fmt,a...)                       \
2624 +do {                                                   \
2625 +       if (test_opt((tree)->inode->i_sb, EXTDEBUG))    \
2626 +               printk(fmt, ##a);                       \
2627 +} while (0);
2628 +#else
2629 +#define ext_debug(tree,fmt,a...)
2630 +#endif
2631 +
2632 +/*
2633 + * if EXT_STATS is defined then stats numbers are collected
2634 + * these number will be displayed at umount time
2635 + */
2636 +#define EXT_STATS_
2637 +
2638 +
2639 +#define EXT3_ALLOC_NEEDED      3       /* block bitmap + group desc. + sb */
2640 +
2641 +/*
2642 + * ext3_inode has i_block array (total 60 bytes)
2643 + * first 4 bytes are used to store:
2644 + *  - tree depth (0 mean there is no tree yet. all extents in the inode)
2645 + *  - number of alive extents in the inode
2646 + */
2647 +
2648 +/*
2649 + * this is extent on-disk structure
2650 + * it's used at the bottom of the tree
2651 + */
2652 +struct ext3_extent {
2653 +       __u32   e_block;        /* first logical block extent covers */
2654 +       __u32   e_start;        /* first physical block extents lives */
2655 +       __u32   e_num;          /* number of blocks covered by extent */
2656 +};
2657 +
2658 +/*
2659 + * this is index on-disk structure
2660 + * it's used at all the levels, but the bottom
2661 + */
2662 +struct ext3_extent_idx {
2663 +       __u32   e_block;        /* index covers logical blocks from 'block' */
2664 +       __u32   e_leaf;         /* pointer to the physical block of the next *
2665 +                                * level. leaf or next index could bet here */
2666 +};
2667 +
2668 +/*
2669 + * each block (leaves and indexes), even inode-stored has header
2670 + */
2671 +struct ext3_extent_header {    
2672 +       __u16   e_magic;        /* probably will support different formats */   
2673 +       __u16   e_num;          /* number of valid entries */
2674 +       __u16   e_max;          /* capacity of store in entries */
2675 +       __u16   e_depth;        /* has tree real underlaying blocks? */
2676 +       __u32   e_generation;   /* generation of the tree */
2677 +};
2678 +
2679 +#define EXT3_EXT_MAGIC         0xf301
2680 +
2681 +/*
2682 + * array of ext3_ext_path contains path to some extent
2683 + * creation/lookup routines use it for traversal/splitting/etc
2684 + * truncate uses it to simulate recursive walking
2685 + */
2686 +struct ext3_ext_path {
2687 +       __u32                           p_block;
2688 +       __u16                           p_depth;
2689 +       struct ext3_extent              *p_ext;
2690 +       struct ext3_extent_idx          *p_idx;
2691 +       struct ext3_extent_header       *p_hdr;
2692 +       struct buffer_head              *p_bh;
2693 +};
2694 +
2695 +/*
2696 + * structure for external API
2697 + */
2698 +
2699 +
2700 +/*
2701 + * ext3_extents_tree is used to pass initial information
2702 + * to top-level extents API
2703 + */
2704 +struct ext3_extents_tree {
2705 +       struct inode *inode;    /* inode which tree belongs to */
2706 +       void *root;             /* ptr to data top of tree resides at */
2707 +       void *buffer;           /* will be passed as arg to ^^ routines */
2708 +       int buffer_len;
2709 +       void *private;
2710 +       struct ext3_extent *cex;/* last found extent */
2711 +       int (*get_write_access)(handle_t *h, void *buffer);
2712 +       int (*mark_buffer_dirty)(handle_t *h, void *buffer);
2713 +       int (*mergable)(struct ext3_extent *ex1, struct ext3_extent *ex2);
2714 +       int (*remove_extent_credits)(struct ext3_extents_tree *,
2715 +                                       struct ext3_extent *, unsigned long,
2716 +                                       unsigned long);
2717 +       int (*remove_extent)(struct ext3_extents_tree *,
2718 +                               struct ext3_extent *, unsigned long,
2719 +                               unsigned long);
2720 +       int (*new_block)(handle_t *, struct ext3_extents_tree *,
2721 +                               struct ext3_ext_path *, struct ext3_extent *,
2722 +                               int *);
2723 +};
2724 +
2725 +/*
2726 + * to be called by ext3_ext_walk_space()
2727 + * negative retcode - error
2728 + * positive retcode - signal for ext3_ext_walk_space(), see below
2729 + * callback must return valid extent (passed or newly created)
2730 + */
2731 +typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
2732 +                                       struct ext3_ext_path *,
2733 +                                       struct ext3_extent *, int);
2734 +
2735 +#define EXT_CONTINUE   0
2736 +#define EXT_BREAK      1
2737 +#define EXT_REPEAT     2
2738 +
2739 +
2740 +#define EXT_FIRST_EXTENT(__hdr__) \
2741 +       ((struct ext3_extent *) (((char *) (__hdr__)) +         \
2742 +                                sizeof(struct ext3_extent_header)))
2743 +#define EXT_FIRST_INDEX(__hdr__) \
2744 +       ((struct ext3_extent_idx *) (((char *) (__hdr__)) +     \
2745 +                                    sizeof(struct ext3_extent_header)))
2746 +#define EXT_HAS_FREE_INDEX(__path__) \
2747 +       ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
2748 +#define EXT_LAST_EXTENT(__hdr__) \
2749 +       (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
2750 +#define EXT_LAST_INDEX(__hdr__) \
2751 +       (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
2752 +#define EXT_MAX_EXTENT(__hdr__) \
2753 +       (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
2754 +#define EXT_MAX_INDEX(__hdr__) \
2755 +       (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
2756 +
2757 +#define EXT_ROOT_HDR(tree) \
2758 +       ((struct ext3_extent_header *) (tree)->root)
2759 +#define EXT_BLOCK_HDR(bh) \
2760 +       ((struct ext3_extent_header *) (bh)->b_data)
2761 +#define EXT_DEPTH(_t_) \
2762 +       (((struct ext3_extent_header *)((_t_)->root))->e_depth)
2763 +#define EXT_GENERATION(_t_)    \
2764 +       (((struct ext3_extent_header *)((_t_)->root))->e_generation)
2765 +
2766 +
2767 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
2768 +
2769 +
2770 +/*
2771 + * this structure is used to gather extents from the tree via ioctl
2772 + */
2773 +struct ext3_extent_buf {
2774 +       unsigned long start;
2775 +       int buflen;
2776 +       void *buffer;
2777 +       void *cur;
2778 +       int err;
2779 +};
2780 +
2781 +/*
2782 + * this structure is used to collect stats info about the tree
2783 + */
2784 +struct ext3_extent_tree_stats {
2785 +       int depth;
2786 +       int extents_num;
2787 +       int leaf_num;
2788 +};
2789 +
2790 +extern int ext3_extent_tree_init(handle_t *, struct ext3_extents_tree *);
2791 +extern int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *, struct ext3_ext_path *);
2792 +extern int ext3_ext_insert_extent(handle_t *, struct ext3_extents_tree *, struct ext3_ext_path *, struct ext3_extent *);
2793 +extern int ext3_ext_walk_space(struct ext3_extents_tree *, unsigned long, unsigned long, ext_prepare_callback);
2794 +extern int ext3_ext_remove_space(struct ext3_extents_tree *, unsigned long, unsigned long);
2795 +extern struct ext3_ext_path * ext3_ext_find_extent(struct ext3_extents_tree *, int, struct ext3_ext_path *);
2796 +
2797 +#endif /* _LINUX_EXT3_EXTENTS */
2798 +
2799 Index: linux-2.4.21-suse2/include/linux/ext3_fs_i.h
2800 ===================================================================
2801 --- linux-2.4.21-suse2.orig/include/linux/ext3_fs_i.h   2004-02-05 20:42:40.000000000 +0300
2802 +++ linux-2.4.21-suse2/include/linux/ext3_fs_i.h        2004-02-05 20:47:04.000000000 +0300
2803 @@ -90,6 +90,8 @@
2804          * by other means, so we have truncate_sem.
2805          */
2806         struct rw_semaphore truncate_sem;
2807 +
2808 +       __u32 i_cached_extent[3];
2809  };
2810  
2811  #endif /* _LINUX_EXT3_FS_I */