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