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