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