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