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