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