Whamcloud - gitweb
- highmem-split-2.6.10-fc3.patch in order to make 3GB memory on mountain's
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-extents-2.6.7.patch
1 %patch
2 Index: linux-2.6.7/fs/ext3/extents.c
3 ===================================================================
4 --- linux-2.6.7.orig/fs/ext3/extents.c  2003-01-30 13:24:37.000000000 +0300
5 +++ linux-2.6.7/fs/ext3/extents.c       2004-09-12 18:53:38.000000000 +0400
6 @@ -0,0 +1,2306 @@
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, 0, 0, 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, struct ext3_extent *ex)
1287 +{
1288 +       if (tree->cex) {
1289 +               EXT_ASSERT(ex);
1290 +               EXT_ASSERT(ex->ee_len);
1291 +               tree->cex->ee_block = ex->ee_block;
1292 +               tree->cex->ee_start = ex->ee_start;
1293 +               tree->cex->ee_len = ex->ee_len;
1294 +       }
1295 +}
1296 +
1297 +/*
1298 + * this routine calculate boundaries of the gap requested block fits into
1299 + * and cache this gap
1300 + */
1301 +static inline void
1302 +ext3_ext_put_gap_in_cache(struct ext3_extents_tree *tree,
1303 +                               struct ext3_ext_path *path,
1304 +                               unsigned long block)
1305 +{
1306 +       int depth = EXT_DEPTH(tree);
1307 +       struct ext3_extent *ex, gex;
1308 +
1309 +       if (!tree->cex)
1310 +               return;
1311 +
1312 +       ex = path[depth].p_ext;
1313 +       if (ex == NULL) {
1314 +               /* there is no extent yet, so gap is [0;-] */
1315 +               gex.ee_block = 0;
1316 +               gex.ee_len = EXT_CACHE_MARK;
1317 +               ext_debug(tree, "cache gap(whole file):");
1318 +       } else if (block < ex->ee_block) {
1319 +               gex.ee_block = block;
1320 +               gex.ee_len = ex->ee_block - block;
1321 +               ext_debug(tree, "cache gap(before): %lu [%lu:%lu]",
1322 +                               (unsigned long) block,
1323 +                               (unsigned long) ex->ee_block,
1324 +                               (unsigned long) ex->ee_len);
1325 +       } else if (block >= ex->ee_block + ex->ee_len) {
1326 +               gex.ee_block = ex->ee_block + ex->ee_len;
1327 +               gex.ee_len = ext3_ext_next_allocated_block(path);
1328 +               ext_debug(tree, "cache gap(after): [%lu:%lu] %lu",
1329 +                               (unsigned long) ex->ee_block,
1330 +                               (unsigned long) ex->ee_len,
1331 +                               (unsigned long) block);
1332 +               EXT_ASSERT(gex.ee_len > gex.ee_block);
1333 +               gex.ee_len = gex.ee_len - gex.ee_block;
1334 +       } else {
1335 +               BUG();
1336 +       }
1337 +
1338 +       ext_debug(tree, " -> %lu:%lu\n", (unsigned long) gex.ee_block,
1339 +                       (unsigned long) gex.ee_len);
1340 +       gex.ee_start = EXT_CACHE_MARK;
1341 +       ext3_ext_put_in_cache(tree, &gex);
1342 +}
1343 +
1344 +static inline int
1345 +ext3_ext_in_cache(struct ext3_extents_tree *tree, unsigned long block,
1346 +                       struct ext3_extent *ex)
1347 +{
1348 +       struct ext3_extent *cex = tree->cex;
1349 +
1350 +       /* is there cache storage at all? */
1351 +       if (!cex)
1352 +               return 0;
1353 +
1354 +       /* has cache valid data? */
1355 +       if (cex->ee_len == 0)
1356 +               return 0;
1357 +
1358 +       if (block >= cex->ee_block && block < cex->ee_block + cex->ee_len) {
1359 +               ex->ee_block = cex->ee_block;
1360 +               ex->ee_start = cex->ee_start;
1361 +               ex->ee_len = cex->ee_len;
1362 +               ext_debug(tree, "%lu cached by %lu:%lu:%lu\n",
1363 +                               (unsigned long) block,
1364 +                               (unsigned long) ex->ee_block,
1365 +                               (unsigned long) ex->ee_len,
1366 +                               (unsigned long) ex->ee_start);
1367 +               return 1;
1368 +       }
1369 +
1370 +       /* not in cache */
1371 +       return 0;
1372 +}
1373 +
1374 +/*
1375 + * routine removes index from the index block
1376 + * it's used in truncate case only. thus all requests are for
1377 + * last index in the block only
1378 + */
1379 +int ext3_ext_rm_idx(handle_t *handle, struct ext3_extents_tree *tree,
1380 +                       struct ext3_ext_path *path)
1381 +{
1382 +       struct buffer_head *bh;
1383 +       int err;
1384 +       
1385 +       /* free index block */
1386 +       path--;
1387 +       EXT_ASSERT(path->p_hdr->eh_entries);
1388 +       if ((err = ext3_ext_get_access(handle, tree, path)))
1389 +               return err;
1390 +       path->p_hdr->eh_entries--;
1391 +       if ((err = ext3_ext_dirty(handle, tree, path)))
1392 +               return err;
1393 +       ext_debug(tree, "index is empty, remove it, free block %d\n",
1394 +                       path->p_idx->ei_leaf);
1395 +       bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
1396 +       ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
1397 +       ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
1398 +       return err;
1399 +}
1400 +
1401 +int ext3_ext_calc_credits_for_insert(struct ext3_extents_tree *tree,
1402 +                                       struct ext3_ext_path *path)
1403 +{
1404 +       int depth = EXT_DEPTH(tree);
1405 +       int needed;
1406 +
1407 +       if (path) {
1408 +               /* probably there is space in leaf? */
1409 +               if (path[depth].p_hdr->eh_entries < path[depth].p_hdr->eh_max)
1410 +                       return 1;
1411 +       }
1412 +       
1413 +       /*
1414 +        * the worste case we're expecting is creation of the
1415 +        * new root (growing in depth) with index splitting
1416 +        * for splitting we have to consider depth + 1 because
1417 +        * previous growing could increase it
1418 +        */
1419 +       depth = depth + 1;
1420 +
1421 +       /* 
1422 +        * growing in depth:
1423 +        * block allocation + new root + old root
1424 +        */
1425 +       needed = EXT3_ALLOC_NEEDED + 2;
1426 +
1427 +       /* index split. we may need:
1428 +        *   allocate intermediate indexes and new leaf
1429 +        *   change two blocks at each level, but root
1430 +        *   modify root block (inode)
1431 +        */
1432 +       needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1433 +
1434 +       return needed;
1435 +}
1436 +
1437 +static int
1438 +ext3_ext_split_for_rm(handle_t *handle, struct ext3_extents_tree *tree,
1439 +                       struct ext3_ext_path *path, unsigned long start,
1440 +                       unsigned long end)
1441 +{
1442 +       struct ext3_extent *ex, tex;
1443 +       struct ext3_ext_path *npath;
1444 +       int depth, creds, err;
1445 +
1446 +       depth = EXT_DEPTH(tree);
1447 +       ex = path[depth].p_ext;
1448 +       EXT_ASSERT(ex);
1449 +       EXT_ASSERT(end < ex->ee_block + ex->ee_len - 1);
1450 +       EXT_ASSERT(ex->ee_block < start);
1451 +
1452 +       /* calculate tail extent */
1453 +       tex.ee_block = end + 1;
1454 +       EXT_ASSERT(tex.ee_block < ex->ee_block + ex->ee_len);
1455 +       tex.ee_len = ex->ee_block + ex->ee_len - tex.ee_block;
1456 +
1457 +       creds = ext3_ext_calc_credits_for_insert(tree, path);
1458 +       handle = ext3_ext_journal_restart(handle, creds);
1459 +       if (IS_ERR(handle))
1460 +               return PTR_ERR(handle);
1461 +       
1462 +       /* calculate head extent. use primary extent */
1463 +       err = ext3_ext_get_access(handle, tree, path + depth);
1464 +       if (err)
1465 +               return err;
1466 +       ex->ee_len = start - ex->ee_block;
1467 +       err = ext3_ext_dirty(handle, tree, path + depth);
1468 +       if (err)
1469 +               return err;
1470 +
1471 +       /* FIXME: some callback to free underlying resource
1472 +        * and correct ee_start? */
1473 +       ext_debug(tree, "split extent: head %u:%u, tail %u:%u\n",
1474 +                       ex->ee_block, ex->ee_len, tex.ee_block, tex.ee_len);
1475 +
1476 +       npath = ext3_ext_find_extent(tree, ex->ee_block, NULL);
1477 +       if (IS_ERR(npath))
1478 +               return PTR_ERR(npath);
1479 +       depth = EXT_DEPTH(tree);
1480 +       EXT_ASSERT(npath[depth].p_ext->ee_block == ex->ee_block);
1481 +       EXT_ASSERT(npath[depth].p_ext->ee_len == ex->ee_len);
1482 +
1483 +       err = ext3_ext_insert_extent(handle, tree, npath, &tex);
1484 +       ext3_ext_drop_refs(npath);
1485 +       kfree(npath);
1486 +
1487 +       return err;
1488 +                       
1489 +}
1490 +
1491 +static int
1492 +ext3_ext_rm_leaf(handle_t *handle, struct ext3_extents_tree *tree,
1493 +                       struct ext3_ext_path *path, unsigned long start,
1494 +                       unsigned long end)
1495 +{
1496 +       struct ext3_extent *ex, *fu = NULL, *lu, *le;
1497 +       int err = 0, correct_index = 0;
1498 +       int depth = EXT_DEPTH(tree), credits;
1499 +       struct ext3_extent_header *eh;
1500 +       unsigned a, b, block, num;
1501 +
1502 +       ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
1503 +       if (!path[depth].p_hdr)
1504 +               path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
1505 +       eh = path[depth].p_hdr;
1506 +       EXT_ASSERT(eh);
1507 +       EXT_ASSERT(eh->eh_entries <= eh->eh_max);
1508 +       EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
1509 +       
1510 +       /* find where to start removing */
1511 +       le = ex = EXT_LAST_EXTENT(eh);
1512 +       while (ex != EXT_FIRST_EXTENT(eh)) {
1513 +               if (ex->ee_block <= end)
1514 +                       break;
1515 +               ex--;
1516 +       }
1517 +
1518 +       if (start > ex->ee_block && end < ex->ee_block + ex->ee_len - 1) {
1519 +               /* removal of internal part of the extent requested
1520 +                * tail and head must be placed in different extent
1521 +                * so, we have to insert one more extent */
1522 +               path[depth].p_ext = ex;
1523 +               return ext3_ext_split_for_rm(handle, tree, path, start, end);
1524 +       }
1525 +       
1526 +       lu = ex;
1527 +       while (ex >= EXT_FIRST_EXTENT(eh) &&
1528 +                       ex->ee_block + ex->ee_len > start) {
1529 +               ext_debug(tree, "remove ext %u:%u\n", ex->ee_block, ex->ee_len);
1530 +               path[depth].p_ext = ex;
1531 +       
1532 +               a = ex->ee_block > start ? ex->ee_block : start;
1533 +               b = ex->ee_block + ex->ee_len - 1 < end ?
1534 +                       ex->ee_block + ex->ee_len - 1 : end;
1535 +               
1536 +               ext_debug(tree, "  border %u:%u\n", a, b);
1537 +
1538 +               if (a != ex->ee_block && b != ex->ee_block + ex->ee_len - 1) {
1539 +                       block = 0;
1540 +                       num = 0;
1541 +                       BUG();
1542 +               } else if (a != ex->ee_block) {
1543 +                       /* remove tail of the extent */
1544 +                       block = ex->ee_block;
1545 +                       num = a - block;
1546 +               } else if (b != ex->ee_block + ex->ee_len - 1) {
1547 +                       /* remove head of the extent */
1548 +                       block = a;
1549 +                       num = b - a;
1550 +               } else {
1551 +                       /* remove whole extent: excelent! */
1552 +                       block = ex->ee_block; 
1553 +                       num = 0;
1554 +                       EXT_ASSERT(a == ex->ee_block &&
1555 +                                       b == ex->ee_block + ex->ee_len - 1);
1556 +               }
1557 +
1558 +               if (ex == EXT_FIRST_EXTENT(eh))
1559 +                       correct_index = 1;
1560 +
1561 +               credits = 1;
1562 +               if (correct_index)
1563 +                       credits += (EXT_DEPTH(tree) * EXT3_ALLOC_NEEDED) + 1;
1564 +               if (tree->ops->remove_extent_credits)
1565 +                       credits+=tree->ops->remove_extent_credits(tree,ex,a,b);
1566 +               
1567 +               handle = ext3_ext_journal_restart(handle, credits);
1568 +               if (IS_ERR(handle)) {
1569 +                       err = PTR_ERR(handle);
1570 +                       goto out;
1571 +               }
1572 +
1573 +               err = ext3_ext_get_access(handle, tree, path + depth);
1574 +               if (err)
1575 +                       goto out;
1576 +
1577 +               if (tree->ops->remove_extent)
1578 +                       err = tree->ops->remove_extent(tree, ex, a, b);
1579 +               if (err)
1580 +                       goto out;
1581 +
1582 +               if (num == 0) {
1583 +                       /* this extent is removed entirely mark slot unused */
1584 +                       ex->ee_start = 0;
1585 +                       eh->eh_entries--;
1586 +                       fu = ex;
1587 +               }
1588 +
1589 +               ex->ee_block = block;
1590 +               ex->ee_len = num;
1591 +
1592 +               err = ext3_ext_dirty(handle, tree, path + depth);
1593 +               if (err)
1594 +                       goto out;
1595 +
1596 +               ext_debug(tree, "new extent: %u:%u:%u\n",
1597 +                               ex->ee_block, ex->ee_len, ex->ee_start);
1598 +               ex--;
1599 +       }
1600 +
1601 +       if (fu) {
1602 +               /* reuse unused slots */
1603 +               while (lu < le) {
1604 +                       if (lu->ee_start) {
1605 +                               *fu = *lu;
1606 +                               lu->ee_start = 0;
1607 +                               fu++;
1608 +                       }
1609 +                       lu++;
1610 +               }
1611 +       }
1612 +
1613 +       if (correct_index && eh->eh_entries)
1614 +               err = ext3_ext_correct_indexes(handle, tree, path);
1615 +
1616 +       /* if this leaf is free, then we should
1617 +        * remove it from index block above */
1618 +       if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1619 +               err = ext3_ext_rm_idx(handle, tree, path + depth);
1620 +
1621 +out:
1622 +       return err;
1623 +}
1624 +
1625 +
1626 +static struct ext3_extent_idx *
1627 +ext3_ext_last_covered(struct ext3_extent_header *hdr, unsigned long block)
1628 +{
1629 +       struct ext3_extent_idx *ix;
1630 +       
1631 +       ix = EXT_LAST_INDEX(hdr);
1632 +       while (ix != EXT_FIRST_INDEX(hdr)) {
1633 +               if (ix->ei_block <= block)
1634 +                       break;
1635 +               ix--;
1636 +       }
1637 +       return ix;
1638 +}
1639 +
1640 +/*
1641 + * returns 1 if current index have to be freed (even partial)
1642 + */
1643 +static int inline
1644 +ext3_ext_more_to_rm(struct ext3_ext_path *path)
1645 +{
1646 +       EXT_ASSERT(path->p_idx);
1647 +
1648 +       if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1649 +               return 0;
1650 +
1651 +       /*
1652 +        * if truncate on deeper level happened it it wasn't partial
1653 +        * so we have to consider current index for truncation
1654 +        */
1655 +       if (path->p_hdr->eh_entries == path->p_block)
1656 +               return 0;
1657 +       return 1;
1658 +}
1659 +
1660 +int ext3_ext_remove_space(struct ext3_extents_tree *tree,
1661 +                               unsigned long start, unsigned long end)
1662 +{
1663 +       struct inode *inode = tree->inode;
1664 +       struct super_block *sb = inode->i_sb;
1665 +       int depth = EXT_DEPTH(tree);
1666 +       struct ext3_ext_path *path;
1667 +       handle_t *handle;
1668 +       int i = 0, err = 0;
1669 +
1670 +       ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
1671 +
1672 +       /* probably first extent we're gonna free will be last in block */
1673 +       handle = ext3_journal_start(inode, depth + 1);
1674 +       if (IS_ERR(handle))
1675 +               return PTR_ERR(handle);
1676 +
1677 +       ext3_ext_invalidate_cache(tree);
1678 +
1679 +       /*
1680 +        * we start scanning from right side freeing all the blocks
1681 +        * after i_size and walking into the deep
1682 +        */
1683 +       path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1684 +       if (IS_ERR(path)) {
1685 +               ext3_error(sb, "ext3_ext_remove_space",
1686 +                               "Can't allocate path array");
1687 +               ext3_journal_stop(handle);
1688 +               return -ENOMEM;
1689 +       }
1690 +       memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1691 +       path[i].p_hdr = EXT_ROOT_HDR(tree);
1692 +       
1693 +       while (i >= 0 && err == 0) {
1694 +               if (i == depth) {
1695 +                       /* this is leaf block */
1696 +                       err = ext3_ext_rm_leaf(handle, tree, path, start, end);
1697 +                       /* root level have p_bh == NULL, brelse() eats this */
1698 +                       brelse(path[i].p_bh);
1699 +                       i--;
1700 +                       continue;
1701 +               }
1702 +               
1703 +               /* this is index block */
1704 +               if (!path[i].p_hdr) {
1705 +                       ext_debug(tree, "initialize header\n");
1706 +                       path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
1707 +               }
1708 +
1709 +               EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
1710 +               EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
1711 +               
1712 +               if (!path[i].p_idx) {
1713 +                       /* this level hasn't touched yet */
1714 +                       path[i].p_idx =
1715 +                               ext3_ext_last_covered(path[i].p_hdr, end);
1716 +                       path[i].p_block = path[i].p_hdr->eh_entries + 1;
1717 +                       ext_debug(tree, "init index ptr: hdr 0x%p, num %d\n",
1718 +                                       path[i].p_hdr, path[i].p_hdr->eh_entries);
1719 +               } else {
1720 +                       /* we've already was here, see at next index */
1721 +                       path[i].p_idx--;
1722 +               }
1723 +
1724 +               ext_debug(tree, "level %d - index, first 0x%p, cur 0x%p\n",
1725 +                               i, EXT_FIRST_INDEX(path[i].p_hdr),
1726 +                               path[i].p_idx);
1727 +               if (ext3_ext_more_to_rm(path + i)) {
1728 +                       /* go to the next level */
1729 +                       ext_debug(tree, "move to level %d (block %d)\n",
1730 +                                       i + 1, path[i].p_idx->ei_leaf);
1731 +                       memset(path + i + 1, 0, sizeof(*path));
1732 +                       path[i+1].p_bh = sb_bread(sb, path[i].p_idx->ei_leaf);
1733 +                       if (!path[i+1].p_bh) {
1734 +                               /* should we reset i_size? */
1735 +                               err = -EIO;
1736 +                               break;
1737 +                       }
1738 +                       /* put actual number of indexes to know is this
1739 +                        * number got changed at the next iteration */
1740 +                       path[i].p_block = path[i].p_hdr->eh_entries;
1741 +                       i++;
1742 +               } else {
1743 +                       /* we finish processing this index, go up */
1744 +                       if (path[i].p_hdr->eh_entries == 0 && i > 0) {
1745 +                               /* index is empty, remove it
1746 +                                * handle must be already prepared by the
1747 +                                * truncatei_leaf() */
1748 +                               err = ext3_ext_rm_idx(handle, tree, path + i);
1749 +                       }
1750 +                       /* root level have p_bh == NULL, brelse() eats this */
1751 +                       brelse(path[i].p_bh);
1752 +                       i--;
1753 +                       ext_debug(tree, "return to level %d\n", i);
1754 +               }
1755 +       }
1756 +
1757 +       /* TODO: flexible tree reduction should be here */
1758 +       if (path->p_hdr->eh_entries == 0) {
1759 +               /*
1760 +                * truncate to zero freed all the tree
1761 +                * so, we need to correct eh_depth
1762 +                */
1763 +               err = ext3_ext_get_access(handle, tree, path);
1764 +               if (err == 0) {
1765 +                       EXT_ROOT_HDR(tree)->eh_depth = 0;
1766 +                       EXT_ROOT_HDR(tree)->eh_max = ext3_ext_space_root(tree);
1767 +                       err = ext3_ext_dirty(handle, tree, path);
1768 +               }
1769 +       }
1770 +       ext3_ext_tree_changed(tree);
1771 +
1772 +       kfree(path);
1773 +       ext3_journal_stop(handle);
1774 +
1775 +       return err;
1776 +}
1777 +
1778 +int ext3_ext_calc_metadata_amount(struct ext3_extents_tree *tree, int blocks)
1779 +{
1780 +       int lcap, icap, rcap, leafs, idxs, num;
1781 +
1782 +       rcap = ext3_ext_space_root(tree);
1783 +       if (blocks <= rcap) {
1784 +               /* all extents fit to the root */
1785 +               return 0;
1786 +       }
1787 +
1788 +       rcap = ext3_ext_space_root_idx(tree);
1789 +       lcap = ext3_ext_space_block(tree);
1790 +       icap = ext3_ext_space_block_idx(tree);
1791 +
1792 +       num = leafs = (blocks + lcap - 1) / lcap;
1793 +       if (leafs <= rcap) {
1794 +               /* all pointers to leafs fit to the root */
1795 +               return leafs;
1796 +       }
1797 +
1798 +       /* ok. we need separate index block(s) to link all leaf blocks */
1799 +       idxs = (leafs + icap - 1) / icap;
1800 +       do {
1801 +               num += idxs;
1802 +               idxs = (idxs + icap - 1) / icap;
1803 +       } while (idxs > rcap);
1804 +
1805 +       return num;
1806 +}
1807 +
1808 +/*
1809 + * called at mount time
1810 + */
1811 +void ext3_ext_init(struct super_block *sb)
1812 +{
1813 +       /*
1814 +        * possible initialization would be here
1815 +        */
1816 +
1817 +       if (test_opt(sb, EXTENTS)) {
1818 +               printk("EXT3-fs: file extents enabled");
1819 +#ifdef AGRESSIVE_TEST
1820 +               printk(", agressive tests");
1821 +#endif
1822 +#ifdef CHECK_BINSEARCH
1823 +               printk(", check binsearch");
1824 +#endif
1825 +               printk("\n");
1826 +       }
1827 +}
1828 +
1829 +/*
1830 + * called at umount time
1831 + */
1832 +void ext3_ext_release(struct super_block *sb)
1833 +{
1834 +}
1835 +
1836 +/************************************************************************
1837 + * VFS related routines
1838 + ************************************************************************/
1839 +
1840 +static int ext3_get_inode_write_access(handle_t *handle, void *buffer)
1841 +{
1842 +       /* we use in-core data, not bh */
1843 +       return 0;
1844 +}
1845 +
1846 +static int ext3_mark_buffer_dirty(handle_t *handle, void *buffer)
1847 +{
1848 +       struct inode *inode = buffer;
1849 +       return ext3_mark_inode_dirty(handle, inode);
1850 +}
1851 +
1852 +static int ext3_ext_mergable(struct ext3_extent *ex1,
1853 +                               struct ext3_extent *ex2)
1854 +{
1855 +       /* FIXME: support for large fs */
1856 +       if (ex1->ee_start + ex1->ee_len == ex2->ee_start)
1857 +               return 1;
1858 +       return 0;
1859 +}
1860 +
1861 +static int
1862 +ext3_remove_blocks_credits(struct ext3_extents_tree *tree,
1863 +                               struct ext3_extent *ex,
1864 +                               unsigned long from, unsigned long to)
1865 +{
1866 +       int needed;
1867 +       
1868 +       /* at present, extent can't cross block group */;
1869 +       needed = 4; /* bitmap + group desc + sb + inode */
1870 +
1871 +#ifdef CONFIG_QUOTA
1872 +       needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1873 +#endif
1874 +       return needed;
1875 +}
1876 +
1877 +static int
1878 +ext3_remove_blocks(struct ext3_extents_tree *tree,
1879 +                               struct ext3_extent *ex,
1880 +                               unsigned long from, unsigned long to)
1881 +{
1882 +       int needed = ext3_remove_blocks_credits(tree, ex, from, to);
1883 +       handle_t *handle = ext3_journal_start(tree->inode, needed);
1884 +       struct buffer_head *bh;
1885 +       int i;
1886 +
1887 +       if (IS_ERR(handle))
1888 +               return PTR_ERR(handle);
1889 +       if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
1890 +               /* tail removal */
1891 +               unsigned long num, start;
1892 +               num = ex->ee_block + ex->ee_len - from;
1893 +               start = ex->ee_start + ex->ee_len - num;
1894 +               ext_debug(tree, "free last %lu blocks starting %lu\n",
1895 +                               num, start);
1896 +               for (i = 0; i < num; i++) {
1897 +                       bh = sb_find_get_block(tree->inode->i_sb, start + i);
1898 +                       ext3_forget(handle, 0, tree->inode, bh, start + i);
1899 +               }
1900 +               ext3_free_blocks(handle, tree->inode, start, num);
1901 +       } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
1902 +               printk("strange request: removal %lu-%lu from %u:%u\n",
1903 +                       from, to, ex->ee_block, ex->ee_len);
1904 +       } else {
1905 +               printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1906 +                       from, to, ex->ee_block, ex->ee_len);
1907 +       }
1908 +       ext3_journal_stop(handle);
1909 +       return 0;
1910 +}
1911 +
1912 +static int ext3_ext_find_goal(struct inode *inode,
1913 +                               struct ext3_ext_path *path, unsigned long block)
1914 +{
1915 +       struct ext3_inode_info *ei = EXT3_I(inode);
1916 +       unsigned long bg_start;
1917 +       unsigned long colour;
1918 +       int depth;
1919 +       
1920 +       if (path) {
1921 +               struct ext3_extent *ex;
1922 +               depth = path->p_depth;
1923 +               
1924 +               /* try to predict block placement */
1925 +               if ((ex = path[depth].p_ext))
1926 +                       return ex->ee_start + (block - ex->ee_block);
1927 +
1928 +               /* it looks index is empty
1929 +                * try to find starting from index itself */
1930 +               if (path[depth].p_bh)
1931 +                       return path[depth].p_bh->b_blocknr;
1932 +       }
1933 +
1934 +       /* OK. use inode's group */
1935 +       bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
1936 +               le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
1937 +       colour = (current->pid % 16) *
1938 +                       (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
1939 +       return bg_start + colour + block;
1940 +}
1941 +
1942 +static int ext3_new_block_cb(handle_t *handle, struct ext3_extents_tree *tree,
1943 +                               struct ext3_ext_path *path,
1944 +                               struct ext3_extent *ex, int *err)
1945 +{
1946 +       struct inode *inode = tree->inode;
1947 +       int newblock, goal;
1948 +       
1949 +       EXT_ASSERT(path);
1950 +       EXT_ASSERT(ex);
1951 +       EXT_ASSERT(ex->ee_start);
1952 +       EXT_ASSERT(ex->ee_len);
1953 +       
1954 +       /* reuse block from the extent to order data/metadata */
1955 +       newblock = ex->ee_start++;
1956 +       ex->ee_len--;
1957 +       if (ex->ee_len == 0) {
1958 +               ex->ee_len = 1;
1959 +               /* allocate new block for the extent */
1960 +               goal = ext3_ext_find_goal(inode, path, ex->ee_block);
1961 +               ex->ee_start = ext3_new_block(handle, inode, goal, 0, 0, err);
1962 +               if (ex->ee_start == 0) {
1963 +                       /* error occured: restore old extent */
1964 +                       ex->ee_start = newblock;
1965 +                       return 0;
1966 +               }
1967 +       }
1968 +       return newblock;
1969 +}
1970 +
1971 +static struct ext3_extents_helpers ext3_blockmap_helpers = {
1972 +       .get_write_access       = ext3_get_inode_write_access,
1973 +       .mark_buffer_dirty      = ext3_mark_buffer_dirty,
1974 +       .mergable               = ext3_ext_mergable,
1975 +       .new_block              = ext3_new_block_cb,
1976 +       .remove_extent          = ext3_remove_blocks,
1977 +       .remove_extent_credits  = ext3_remove_blocks_credits,
1978 +};
1979 +
1980 +void ext3_init_tree_desc(struct ext3_extents_tree *tree,
1981 +                               struct inode *inode)
1982 +{
1983 +       tree->inode = inode;
1984 +       tree->root = (void *) EXT3_I(inode)->i_data;
1985 +       tree->buffer = (void *) inode;
1986 +       tree->buffer_len = sizeof(EXT3_I(inode)->i_data);
1987 +       tree->cex = (struct ext3_extent *) &EXT3_I(inode)->i_cached_extent;
1988 +       tree->ops = &ext3_blockmap_helpers;
1989 +}
1990 +
1991 +int ext3_ext_get_block(handle_t *handle, struct inode *inode,
1992 +                       long iblock, struct buffer_head *bh_result,
1993 +                       int create, int extend_disksize)
1994 +{
1995 +       struct ext3_ext_path *path = NULL;
1996 +       struct ext3_extent newex;
1997 +       struct ext3_extent *ex;
1998 +       int goal, newblock, err = 0, depth;
1999 +       struct ext3_extents_tree tree;
2000 +
2001 +       clear_buffer_new(bh_result);
2002 +       ext3_init_tree_desc(&tree, inode);
2003 +       ext_debug(&tree, "block %d requested for inode %u\n",
2004 +                       (int) iblock, (unsigned) inode->i_ino);
2005 +       down(&EXT3_I(inode)->truncate_sem);
2006 +
2007 +       /* check in cache */
2008 +       if (ext3_ext_in_cache(&tree, iblock, &newex)) {
2009 +               if (newex.ee_start == EXT_CACHE_MARK) {
2010 +                       /* this is cached gap */
2011 +                       if (!create) {
2012 +                               /* block isn't allocated yet and
2013 +                                * user don't want to allocate it */
2014 +                               goto out2;
2015 +                       }
2016 +                       /* we should allocate requested block */
2017 +               } else if (newex.ee_start) {
2018 +                       /* block is already allocated */
2019 +                       newblock = iblock - newex.ee_block + newex.ee_start;
2020 +                       goto out;
2021 +               }
2022 +       }
2023 +
2024 +       /* find extent for this block */
2025 +       path = ext3_ext_find_extent(&tree, iblock, NULL);
2026 +       if (IS_ERR(path)) {
2027 +               err = PTR_ERR(path);
2028 +               path = NULL;
2029 +               goto out2;
2030 +       }
2031 +
2032 +       depth = EXT_DEPTH(&tree);
2033 +
2034 +       /*
2035 +        * consistent leaf must not be empty
2036 +        * this situations is possible, though, _during_ tree modification
2037 +        * this is why assert can't be put in ext3_ext_find_extent()
2038 +        */
2039 +       EXT_ASSERT(path[depth].p_ext != NULL || depth == 0);
2040 +
2041 +       if ((ex = path[depth].p_ext)) {
2042 +               /* if found exent covers block, simple return it */
2043 +               if (iblock >= ex->ee_block && iblock < ex->ee_block + ex->ee_len) {
2044 +                       newblock = iblock - ex->ee_block + ex->ee_start;
2045 +                       ext_debug(&tree, "%d fit into %d:%d -> %d\n",
2046 +                                       (int) iblock, ex->ee_block, ex->ee_len,
2047 +                                       newblock);
2048 +                       ext3_ext_put_in_cache(&tree, ex);
2049 +                       goto out;
2050 +               }
2051 +       }
2052 +
2053 +       /*
2054 +        * requested block isn't allocated yet
2055 +        * we couldn't try to create block if create flag is zero 
2056 +        */
2057 +       if (!create) {
2058 +               /* put just found gap into cache to speedup subsequest reqs */
2059 +               ext3_ext_put_gap_in_cache(&tree, path, iblock);
2060 +               goto out2;
2061 +       }
2062 +
2063 +       /* allocate new block */
2064 +       goal = ext3_ext_find_goal(inode, path, iblock);
2065 +       newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
2066 +       if (!newblock)
2067 +               goto out2;
2068 +       ext_debug(&tree, "allocate new block: goal %d, found %d\n",
2069 +                       goal, newblock);
2070 +
2071 +       /* try to insert new extent into found leaf and return */
2072 +       newex.ee_block = iblock;
2073 +       newex.ee_start = newblock;
2074 +       newex.ee_len = 1;
2075 +       err = ext3_ext_insert_extent(handle, &tree, path, &newex);
2076 +       if (err)
2077 +               goto out2;
2078 +       
2079 +       if (extend_disksize && inode->i_size > EXT3_I(inode)->i_disksize)
2080 +               EXT3_I(inode)->i_disksize = inode->i_size;
2081 +
2082 +       /* previous routine could use block we allocated */
2083 +       newblock = newex.ee_start;
2084 +       set_buffer_new(bh_result);
2085 +
2086 +       ext3_ext_put_in_cache(&tree, &newex);
2087 +out:
2088 +       ext3_ext_show_leaf(&tree, path);
2089 +       map_bh(bh_result, inode->i_sb, newblock);
2090 +out2:
2091 +       if (path) {
2092 +               ext3_ext_drop_refs(path);
2093 +               kfree(path);
2094 +       }
2095 +       up(&EXT3_I(inode)->truncate_sem);
2096 +
2097 +       return err;     
2098 +}
2099 +
2100 +void ext3_ext_truncate(struct inode * inode, struct page *page)
2101 +{
2102 +       struct address_space *mapping = inode->i_mapping;
2103 +       struct super_block *sb = inode->i_sb;
2104 +       struct ext3_extents_tree tree;
2105 +       unsigned long last_block;
2106 +       handle_t *handle;
2107 +       int err = 0;
2108 +
2109 +       ext3_init_tree_desc(&tree, inode);
2110 +
2111 +       /*
2112 +        * probably first extent we're gonna free will be last in block
2113 +        */
2114 +       err = ext3_writepage_trans_blocks(inode) + 3;
2115 +       handle = ext3_journal_start(inode, err);
2116 +       if (IS_ERR(handle)) {
2117 +               if (page) {
2118 +                       clear_highpage(page);
2119 +                       flush_dcache_page(page);
2120 +                       unlock_page(page);
2121 +                       page_cache_release(page);
2122 +               }
2123 +               return;
2124 +       }
2125 +
2126 +       if (page)
2127 +               ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2128 +
2129 +       down(&EXT3_I(inode)->truncate_sem);
2130 +       ext3_ext_invalidate_cache(&tree);
2131 +
2132 +       /* 
2133 +        * TODO: optimization is possible here
2134 +        * probably we need not scaning at all,
2135 +        * because page truncation is enough
2136 +        */
2137 +       if (ext3_orphan_add(handle, inode))
2138 +               goto out_stop;
2139 +
2140 +       /* we have to know where to truncate from in crash case */
2141 +       EXT3_I(inode)->i_disksize = inode->i_size;
2142 +       ext3_mark_inode_dirty(handle, inode);
2143 +
2144 +       last_block = (inode->i_size + sb->s_blocksize - 1)
2145 +                       >> EXT3_BLOCK_SIZE_BITS(sb);
2146 +       err = ext3_ext_remove_space(&tree, last_block, EXT_MAX_BLOCK);
2147 +       
2148 +       /* In a multi-transaction truncate, we only make the final
2149 +        * transaction synchronous */
2150 +       if (IS_SYNC(inode))
2151 +               handle->h_sync = 1;
2152 +
2153 +out_stop:
2154 +       /*
2155 +        * If this was a simple ftruncate(), and the file will remain alive
2156 +        * then we need to clear up the orphan record which we created above.
2157 +        * However, if this was a real unlink then we were called by
2158 +        * ext3_delete_inode(), and we allow that function to clean up the
2159 +        * orphan info for us.
2160 +        */
2161 +       if (inode->i_nlink)
2162 +               ext3_orphan_del(handle, inode);
2163 +
2164 +       up(&EXT3_I(inode)->truncate_sem);
2165 +       ext3_journal_stop(handle);
2166 +}
2167 +
2168 +/*
2169 + * this routine calculate max number of blocks we could modify
2170 + * in order to allocate new block for an inode
2171 + */
2172 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
2173 +{
2174 +       struct ext3_extents_tree tree;
2175 +       int needed;
2176 +       
2177 +       ext3_init_tree_desc(&tree, inode);
2178 +       
2179 +       needed = ext3_ext_calc_credits_for_insert(&tree, NULL);
2180 +
2181 +       /* caller want to allocate num blocks */
2182 +       needed *= num;
2183 +       
2184 +#ifdef CONFIG_QUOTA
2185 +       /* 
2186 +        * FIXME: real calculation should be here
2187 +        * it depends on blockmap format of qouta file
2188 +        */
2189 +       needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
2190 +#endif
2191 +
2192 +       return needed;
2193 +}
2194 +
2195 +void ext3_extents_initialize_blockmap(handle_t *handle, struct inode *inode)
2196 +{
2197 +       struct ext3_extents_tree tree;
2198 +
2199 +       ext3_init_tree_desc(&tree, inode);
2200 +       ext3_extent_tree_init(handle, &tree);
2201 +}
2202 +
2203 +int ext3_ext_calc_blockmap_metadata(struct inode *inode, int blocks)
2204 +{
2205 +       struct ext3_extents_tree tree;
2206 +
2207 +       ext3_init_tree_desc(&tree, inode);
2208 +       return ext3_ext_calc_metadata_amount(&tree, blocks);
2209 +}
2210 +       
2211 +static int
2212 +ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
2213 +                       struct ext3_ext_path *path,
2214 +                       struct ext3_extent *newex, int exist)
2215 +{
2216 +       struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
2217 +
2218 +       if (!exist)
2219 +               return EXT_CONTINUE;
2220 +       if (buf->err < 0)
2221 +               return EXT_BREAK;
2222 +       if (buf->cur - buf->buffer + sizeof(*newex) > buf->buflen)
2223 +               return EXT_BREAK;
2224 +
2225 +       if (!copy_to_user(buf->cur, newex, sizeof(*newex))) {
2226 +               buf->err++;
2227 +               buf->cur += sizeof(*newex);
2228 +       } else {
2229 +               buf->err = -EFAULT;
2230 +               return EXT_BREAK;
2231 +       }
2232 +       return EXT_CONTINUE;
2233 +}
2234 +
2235 +static int
2236 +ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
2237 +                       struct ext3_ext_path *path,
2238 +                       struct ext3_extent *ex, int exist)
2239 +{
2240 +       struct ext3_extent_tree_stats *buf =
2241 +               (struct ext3_extent_tree_stats *) tree->private;
2242 +       int depth;
2243 +
2244 +       if (!exist)
2245 +               return EXT_CONTINUE;
2246 +
2247 +       depth = EXT_DEPTH(tree);
2248 +       buf->extents_num++;
2249 +       if (path[depth].p_ext == EXT_FIRST_EXTENT(path[depth].p_hdr))
2250 +               buf->leaf_num++;
2251 +       return EXT_CONTINUE;
2252 +}
2253 +
2254 +int ext3_ext_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
2255 +               unsigned long arg)
2256 +{
2257 +       int err = 0;
2258 +
2259 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
2260 +               return -EINVAL;
2261 +
2262 +       if (cmd == EXT3_IOC_GET_EXTENTS) {
2263 +               struct ext3_extent_buf buf;
2264 +               struct ext3_extents_tree tree;
2265 +
2266 +               if (copy_from_user(&buf, (void *) arg, sizeof(buf)))
2267 +                       return -EFAULT;
2268 +
2269 +               ext3_init_tree_desc(&tree, inode);
2270 +               buf.cur = buf.buffer;
2271 +               buf.err = 0;
2272 +               tree.private = &buf;
2273 +               down(&EXT3_I(inode)->truncate_sem);
2274 +               err = ext3_ext_walk_space(&tree, buf.start, EXT_MAX_BLOCK,
2275 +                                               ext3_ext_store_extent_cb);
2276 +               up(&EXT3_I(inode)->truncate_sem);
2277 +               if (err == 0)
2278 +                       err = buf.err;
2279 +       } else if (cmd == EXT3_IOC_GET_TREE_STATS) {
2280 +               struct ext3_extent_tree_stats buf;
2281 +               struct ext3_extents_tree tree;
2282 +
2283 +               ext3_init_tree_desc(&tree, inode);
2284 +               down(&EXT3_I(inode)->truncate_sem);
2285 +               buf.depth = EXT_DEPTH(&tree);
2286 +               buf.extents_num = 0;
2287 +               buf.leaf_num = 0;
2288 +               tree.private = &buf;
2289 +               err = ext3_ext_walk_space(&tree, 0, EXT_MAX_BLOCK,
2290 +                                               ext3_ext_collect_stats_cb);
2291 +               up(&EXT3_I(inode)->truncate_sem);
2292 +               if (!err)
2293 +                       err = copy_to_user((void *) arg, &buf, sizeof(buf));
2294 +       } else if (cmd == EXT3_IOC_GET_TREE_DEPTH) {
2295 +               struct ext3_extents_tree tree;
2296 +               ext3_init_tree_desc(&tree, inode);
2297 +               down(&EXT3_I(inode)->truncate_sem);
2298 +               err = EXT_DEPTH(&tree);
2299 +               up(&EXT3_I(inode)->truncate_sem);
2300 +       }
2301 +
2302 +       return err;
2303 +}
2304 +
2305 +EXPORT_SYMBOL(ext3_init_tree_desc);
2306 +EXPORT_SYMBOL(ext3_mark_inode_dirty);
2307 +EXPORT_SYMBOL(ext3_ext_invalidate_cache);
2308 +EXPORT_SYMBOL(ext3_ext_insert_extent);
2309 +EXPORT_SYMBOL(ext3_ext_walk_space);
2310 +EXPORT_SYMBOL(ext3_ext_find_goal);
2311 +EXPORT_SYMBOL(ext3_ext_calc_credits_for_insert);
2312 +
2313 Index: linux-2.6.7/fs/ext3/ialloc.c
2314 ===================================================================
2315 --- linux-2.6.7.orig/fs/ext3/ialloc.c   2004-09-12 18:53:35.000000000 +0400
2316 +++ linux-2.6.7/fs/ext3/ialloc.c        2004-09-12 19:09:54.000000000 +0400
2317 @@ -646,6 +646,17 @@
2318                 DQUOT_FREE_INODE(inode);
2319                 goto fail2;
2320         }
2321 +       if (test_opt(sb, EXTENTS)) {
2322 +               EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
2323 +               ext3_extents_initialize_blockmap(handle, inode);
2324 +               if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2325 +                       err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
2326 +                       if (err) goto fail;
2327 +                       EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_EXTENTS);
2328 +                       BUFFER_TRACE(EXT3_SB(sb)->s_sbh, "call ext3_journal_dirty_metadata");
2329 +                       err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
2330 +               }
2331 +       }
2332         err = ext3_mark_inode_dirty(handle, inode);
2333         if (err) {
2334                 ext3_std_error(sb, err);
2335 Index: linux-2.6.7/fs/ext3/inode.c
2336 ===================================================================
2337 --- linux-2.6.7.orig/fs/ext3/inode.c    2004-09-12 18:53:35.000000000 +0400
2338 +++ linux-2.6.7/fs/ext3/inode.c 2004-09-12 18:53:38.000000000 +0400
2339 @@ -857,6 +857,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 @@ -867,8 +878,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 @@ -894,8 +905,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 @@ -916,7 +927,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 @@ -1669,7 +1680,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 @@ -2165,6 +2176,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 @@ -2888,6 +2902,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.7/fs/ext3/super.c
2418 ===================================================================
2419 --- linux-2.6.7.orig/fs/ext3/super.c    2004-09-12 18:53:35.000000000 +0400
2420 +++ linux-2.6.7/fs/ext3/super.c 2004-09-12 18:53:38.000000000 +0400
2421 @@ -392,6 +392,7 @@
2422         struct ext3_super_block *es = sbi->s_es;
2423         int i;
2424  
2425 +       ext3_ext_release(sb);
2426         ext3_xattr_put_super(sb);
2427         journal_destroy(sbi->s_journal);
2428         if (!(sb->s_flags & MS_RDONLY)) {
2429 @@ -458,6 +459,9 @@
2430         dynlock_init(&ei->i_htree_lock);
2431         sema_init(&ei->i_rename_sem, 1);
2432         sema_init(&ei->i_append_sem, 1);
2433 +       ei->i_cached_extent[0] = 0;
2434 +       ei->i_cached_extent[1] = 0;
2435 +       ei->i_cached_extent[2] = 0;
2436         return &ei->vfs_inode;
2437  }
2438  
2439 @@ -592,7 +596,7 @@
2440         Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
2441         Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
2442         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_pdirops,
2443 -       Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2444 +       Opt_iopen, Opt_noiopen, Opt_iopen_nopriv, Opt_extents, Opt_extdebug,
2445         Opt_ignore, Opt_err,
2446  };
2447  
2448 @@ -642,6 +646,8 @@
2449         {Opt_iopen,  "iopen"},
2450         {Opt_noiopen,  "noiopen"},
2451         {Opt_iopen_nopriv,  "iopen_nopriv"},
2452 +       {Opt_extents, "extents"},
2453 +       {Opt_extdebug, "extdebug"},
2454         {Opt_err, NULL}
2455  };
2456  
2457 @@ -921,6 +927,12 @@
2458                         break;
2459                 case Opt_ignore:
2460                         break;
2461 +               case Opt_extents:
2462 +                       set_opt (sbi->s_mount_opt, EXTENTS);
2463 +                       break;
2464 +               case Opt_extdebug:
2465 +                       set_opt (sbi->s_mount_opt, EXTDEBUG);
2466 +                       break;
2467                 default:
2468                         printk (KERN_ERR
2469                                 "EXT3-fs: Unrecognized mount option \"%s\" "
2470 @@ -1593,6 +1605,8 @@
2471         percpu_counter_mod(&sbi->s_dirs_counter,
2472                 ext3_count_dirs(sb));
2473  
2474 +       ext3_ext_init(sb);
2475
2476         return 0;
2477  
2478  failed_mount3:
2479 Index: linux-2.6.7/fs/ext3/ioctl.c
2480 ===================================================================
2481 --- linux-2.6.7.orig/fs/ext3/ioctl.c    2004-09-12 18:53:32.000000000 +0400
2482 +++ linux-2.6.7/fs/ext3/ioctl.c 2004-09-12 18:53:38.000000000 +0400
2483 @@ -176,6 +176,10 @@
2484                         return ret;
2485                 }
2486  #endif
2487 +       case EXT3_IOC_GET_EXTENTS:
2488 +       case EXT3_IOC_GET_TREE_STATS:
2489 +       case EXT3_IOC_GET_TREE_DEPTH:
2490 +               return ext3_ext_ioctl(inode, filp, cmd, arg);
2491         default:
2492                 return -ENOTTY;
2493         }
2494 Index: linux-2.6.7/fs/ext3/Makefile
2495 ===================================================================
2496 --- linux-2.6.7.orig/fs/ext3/Makefile   2004-09-12 18:53:33.000000000 +0400
2497 +++ linux-2.6.7/fs/ext3/Makefile        2004-09-12 18:53:38.000000000 +0400
2498 @@ -5,7 +5,7 @@
2499  obj-$(CONFIG_EXT3_FS) += ext3.o
2500  
2501  ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2502 -          ioctl.o namei.o super.o symlink.o hash.o
2503 +          ioctl.o namei.o super.o symlink.o hash.o extents.o
2504  
2505  ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o
2506  ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2507 Index: linux-2.6.7/include/linux/ext3_fs.h
2508 ===================================================================
2509 --- linux-2.6.7.orig/include/linux/ext3_fs.h    2004-09-12 18:53:37.000000000 +0400
2510 +++ linux-2.6.7/include/linux/ext3_fs.h 2004-09-12 19:13:48.000000000 +0400
2511 @@ -186,6 +186,7 @@
2512  #define EXT3_DIRSYNC_FL                        0x00010000 /* dirsync behaviour (directories only) */
2513  #define EXT3_TOPDIR_FL                 0x00020000 /* Top of directory hierarchies*/
2514  #define EXT3_RESERVED_FL               0x80000000 /* reserved for ext3 lib */
2515 +#define EXT3_EXTENTS_FL                        0x00080000 /* Inode uses extents */
2516  
2517  #define EXT3_FL_USER_VISIBLE           0x0003DFFF /* User visible flags */
2518  #define EXT3_FL_USER_MODIFIABLE                0x000380FF /* User modifiable flags */
2519 @@ -209,6 +210,9 @@
2520  #ifdef CONFIG_JBD_DEBUG
2521  #define EXT3_IOC_WAIT_FOR_READONLY     _IOR('f', 99, long)
2522  #endif
2523 +#define        EXT3_IOC_GET_EXTENTS            _IOR('f', 5, long)
2524 +#define        EXT3_IOC_GET_TREE_DEPTH         _IOR('f', 6, long)
2525 +#define        EXT3_IOC_GET_TREE_STATS         _IOR('f', 7, long)
2526  
2527  /*
2528   * Structure of an inode on the disk
2529 @@ -330,6 +334,8 @@
2530  #define EXT3_MOUNT_POSIX_ACL           0x8000  /* POSIX Access Control Lists */
2531  #define EXT3_MOUNT_IOPEN               0x40000 /* Allow access via iopen */
2532  #define EXT3_MOUNT_IOPEN_NOPRIV                0x80000 /* Make iopen world-readable */
2533 +#define EXT3_MOUNT_EXTENTS             0x10000 /* Extents support */
2534 +#define EXT3_MOUNT_EXTDEBUG            0x20000 /* Extents debug */
2535  
2536  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2537  #ifndef clear_opt
2538 @@ -518,11 +524,13 @@
2539  #define EXT3_FEATURE_INCOMPAT_RECOVER          0x0004 /* Needs recovery */
2540  #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV      0x0008 /* Journal device */
2541  #define EXT3_FEATURE_INCOMPAT_META_BG          0x0010
2542 +#define EXT3_FEATURE_INCOMPAT_EXTENTS          0x0040 /* extents support */
2543  
2544  #define EXT3_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
2545  #define EXT3_FEATURE_INCOMPAT_SUPP     (EXT3_FEATURE_INCOMPAT_FILETYPE| \
2546                                          EXT3_FEATURE_INCOMPAT_RECOVER| \
2547 -                                        EXT3_FEATURE_INCOMPAT_META_BG)
2548 +                                        EXT3_FEATURE_INCOMPAT_META_BG| \
2549 +                                        EXT3_FEATURE_INCOMPAT_EXTENTS)
2550  #define EXT3_FEATURE_RO_COMPAT_SUPP    (EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER| \
2551                                          EXT3_FEATURE_RO_COMPAT_LARGE_FILE| \
2552                                          EXT3_FEATURE_RO_COMPAT_BTREE_DIR)
2553 @@ -725,6 +733,7 @@
2554  
2555  
2556  /* inode.c */
2557 +extern int ext3_block_truncate_page(handle_t *, struct page *, struct address_space *, loff_t);
2558  extern int ext3_forget(handle_t *, int, struct inode *, struct buffer_head *, int);
2559  extern struct buffer_head * ext3_getblk (handle_t *, struct inode *, long, int, int *);
2560  extern struct buffer_head * ext3_bread (handle_t *, struct inode *, int, int, int *);
2561 @@ -798,6 +807,14 @@
2562  extern struct inode_operations ext3_symlink_inode_operations;
2563  extern struct inode_operations ext3_fast_symlink_inode_operations;
2564  
2565 +/* extents.c */
2566 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
2567 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
2568 +                               struct buffer_head *, int, int);
2569 +extern void ext3_ext_truncate(struct inode *, struct page *);
2570 +extern void ext3_ext_init(struct super_block *);
2571 +extern void ext3_ext_release(struct super_block *);
2572 +extern void ext3_extents_initialize_blockmap(handle_t *, struct inode *);
2573  
2574  #endif /* __KERNEL__ */
2575  
2576 Index: linux-2.6.7/include/linux/ext3_extents.h
2577 ===================================================================
2578 --- linux-2.6.7.orig/include/linux/ext3_extents.h       2003-01-30 13:24:37.000000000 +0300
2579 +++ linux-2.6.7/include/linux/ext3_extents.h    2004-09-12 18:53:38.000000000 +0400
2580 @@ -0,0 +1,238 @@
2581 +/*
2582 + * Copyright (c) 2003, Cluster File Systems, Inc, info@clusterfs.com
2583 + * Written by Alex Tomas <alex@clusterfs.com>
2584 + *
2585 + * This program is free software; you can redistribute it and/or modify
2586 + * it under the terms of the GNU General Public License version 2 as
2587 + * published by the Free Software Foundation.
2588 + *
2589 + * This program is distributed in the hope that it will be useful,
2590 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2591 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2592 + * GNU General Public License for more details.
2593 + *
2594 + * You should have received a copy of the GNU General Public Licens
2595 + * along with this program; if not, write to the Free Software
2596 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
2597 + */
2598 +
2599 +#ifndef _LINUX_EXT3_EXTENTS
2600 +#define _LINUX_EXT3_EXTENTS
2601 +
2602 +/*
2603 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
2604 + * become very little, so index split, in-depth growing and
2605 + * other hard changes happens much more often
2606 + * this is for debug purposes only
2607 + */
2608 +#define AGRESSIVE_TEST_
2609 +
2610 +/*
2611 + * if CHECK_BINSEARCH defined, then results of binary search
2612 + * will be checked by linear search
2613 + */
2614 +#define CHECK_BINSEARCH_
2615 +
2616 +/*
2617 + * if EXT_DEBUG is defined you can use 'extdebug' mount option
2618 + * to get lots of info what's going on
2619 + */
2620 +#define EXT_DEBUG
2621 +#ifdef EXT_DEBUG
2622 +#define ext_debug(tree,fmt,a...)                       \
2623 +do {                                                   \
2624 +       if (test_opt((tree)->inode->i_sb, EXTDEBUG))    \
2625 +               printk(fmt, ##a);                       \
2626 +} while (0);
2627 +#else
2628 +#define ext_debug(tree,fmt,a...)
2629 +#endif
2630 +
2631 +/*
2632 + * if EXT_STATS is defined then stats numbers are collected
2633 + * these number will be displayed at umount time
2634 + */
2635 +#define EXT_STATS_
2636 +
2637 +
2638 +#define EXT3_ALLOC_NEEDED      3       /* block bitmap + group desc. + sb */
2639 +
2640 +/*
2641 + * ext3_inode has i_block array (total 60 bytes)
2642 + * first 4 bytes are used to store:
2643 + *  - tree depth (0 mean there is no tree yet. all extents in the inode)
2644 + *  - number of alive extents in the inode
2645 + */
2646 +
2647 +/*
2648 + * this is extent on-disk structure
2649 + * it's used at the bottom of the tree
2650 + */
2651 +struct ext3_extent {
2652 +       __u32   ee_block;       /* first logical block extent covers */
2653 +       __u16   ee_len;         /* number of blocks covered by extent */
2654 +       __u16   ee_start_hi;    /* high 16 bits of physical block */
2655 +       __u32   ee_start;       /* low 32 bigs of physical block */
2656 +};
2657 +
2658 +/*
2659 + * this is index on-disk structure
2660 + * it's used at all the levels, but the bottom
2661 + */
2662 +struct ext3_extent_idx {
2663 +       __u32   ei_block;       /* index covers logical blocks from 'block' */
2664 +       __u32   ei_leaf;        /* pointer to the physical block of the next *
2665 +                                * level. leaf or next index could bet here */
2666 +       __u16   ei_leaf_hi;     /* high 16 bits of physical block */
2667 +       __u16   ei_unused;
2668 +};
2669 +
2670 +/*
2671 + * each block (leaves and indexes), even inode-stored has header
2672 + */
2673 +struct ext3_extent_header {    
2674 +       __u16   eh_magic;       /* probably will support different formats */   
2675 +       __u16   eh_entries;     /* number of valid entries */
2676 +       __u16   eh_max;         /* capacity of store in entries */
2677 +       __u16   eh_depth;       /* has tree real underlaying blocks? */
2678 +       __u32   eh_generation;  /* generation of the tree */
2679 +};
2680 +
2681 +#define EXT3_EXT_MAGIC         0xf30a
2682 +
2683 +/*
2684 + * array of ext3_ext_path contains path to some extent
2685 + * creation/lookup routines use it for traversal/splitting/etc
2686 + * truncate uses it to simulate recursive walking
2687 + */
2688 +struct ext3_ext_path {
2689 +       __u32                           p_block;
2690 +       __u16                           p_depth;
2691 +       struct ext3_extent              *p_ext;
2692 +       struct ext3_extent_idx          *p_idx;
2693 +       struct ext3_extent_header       *p_hdr;
2694 +       struct buffer_head              *p_bh;
2695 +};
2696 +
2697 +/*
2698 + * structure for external API
2699 + */
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_extent *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->ee_len = 0;
2814 +}
2815 +
2816 +
2817 +#endif /* _LINUX_EXT3_EXTENTS */
2818 +
2819 Index: linux-2.6.7/include/linux/ext3_fs_i.h
2820 ===================================================================
2821 --- linux-2.6.7.orig/include/linux/ext3_fs_i.h  2004-09-12 18:53:35.000000000 +0400
2822 +++ linux-2.6.7/include/linux/ext3_fs_i.h       2004-09-12 18:53:38.000000000 +0400
2823 @@ -117,6 +117,8 @@
2824         struct dynlock i_htree_lock;
2825         struct semaphore i_append_sem;
2826         struct semaphore i_rename_sem;
2827 +
2828 +       __u32 i_cached_extent[3];
2829  };
2830  
2831  #endif /* _LINUX_EXT3_FS_I */
2832
2833 %diffstat
2834  fs/ext3/Makefile             |    2 
2835  fs/ext3/extents.c            | 2306 +++++++++++++++++++++++++++++++++++++++++++
2836  fs/ext3/ialloc.c             |   11 
2837  fs/ext3/inode.c              |   29 
2838  fs/ext3/ioctl.c              |    4 
2839  fs/ext3/super.c              |   16 
2840  include/linux/ext3_extents.h |  238 ++++
2841  include/linux/ext3_fs.h      |   19 
2842  include/linux/ext3_fs_i.h    |    2 
2843  9 files changed, 2618 insertions(+), 9 deletions(-)
2844