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