Whamcloud - gitweb
c020054986f381d1f6b72fcc8cc9c8e1167b73a9
[fs/lustre-release.git] / lustre / kernel_patches / patches / ext3-extents-2.4.18-chaos.patch
1  fs/ext3/Makefile           |    3 
2  fs/ext3/extents.c          | 1615 +++++++++++++++++++++++++++++++++++++++++++++
3  fs/ext3/ialloc.c           |    4 
4  fs/ext3/inode.c            |   30 
5  fs/ext3/super.c            |    8 
6  include/linux/ext3_fs.h    |   18 
7  include/linux/ext3_fs_i.h  |    4 
8  include/linux/ext3_fs_sb.h |   10 
9  8 files changed, 1684 insertions(+), 8 deletions(-)
10
11 --- /dev/null   2003-01-30 13:24:37.000000000 +0300
12 +++ linux-2.4.18-chaos-alexey/fs/ext3/extents.c 2003-09-23 18:08:59.000000000 +0400
13 @@ -0,0 +1,1614 @@
14 +/*
15 + *
16 + * linux/fs/ext3/extents.c
17 + *
18 + * Extents support for EXT3
19 + *
20 + * 07/08/2003    Alex Tomas <bzzz@tmi.comex.ru>
21 + * 
22 + * TODO:
23 + *   - ext3*_error() should be used in some situations
24 + *   - find_goal() [to be tested and improved]
25 + *   - error handling
26 + *   - we could leak allocated block in some error cases
27 + *   - quick search for index/leaf in ext3_ext_find_extent()
28 + *   - tree reduction
29 + *   - cache last found extent
30 + *   - arch-independent
31 + */
32 +
33 +#include <linux/module.h>
34 +#include <linux/fs.h>
35 +#include <linux/time.h>
36 +#include <linux/ext3_jbd.h>
37 +#include <linux/jbd.h>
38 +#include <linux/smp_lock.h>
39 +#include <linux/highuid.h>
40 +#include <linux/pagemap.h>
41 +#include <linux/quotaops.h>
42 +#include <linux/string.h>
43 +#include <linux/slab.h>
44 +#include <linux/locks.h>
45 +
46 +/*
47 + * with AGRESSIVE_TEST defined capacity of index/leaf blocks
48 + * become very little, so index split, in-depth growing and
49 + * other hard changes happens much more often
50 + * this is for debug purposes only
51 + */
52 +#define AGRESSIVE_TEST_
53 +
54 +/*
55 + * if EXT_DEBUG defined you can use 'extdebug' mount option
56 + * to get lots of info what's going on
57 + */
58 +#define EXT_DEBUG
59 +#ifdef EXT_DEBUG
60 +#define ext_debug(inode,fmt,a...)              \
61 +do {                                           \
62 +       if (test_opt((inode)->i_sb, EXTDEBUG))  \
63 +               printk(fmt, ##a);               \
64 +} while (0);
65 +#else
66 +#define ext_debug(inode,fmt,a...)
67 +#endif
68 +
69 +#define EXT3_ALLOC_NEEDED      2       /* block bitmap + group descriptor */
70 +
71 +/*
72 + * ext3_inode has i_block array (total 60 bytes)
73 + * first 4 bytes are used to store:
74 + *  - tree depth (0 mean there is no tree yet. all extents in the inode)
75 + *  - number of alive extents in the inode
76 + */
77 +
78 +/*
79 + * this is extent on-disk structure
80 + * it's used at the bottom of the tree
81 + */
82 +struct ext3_extent {
83 +       __u32   e_block;        /* first logical block extent covers */
84 +       __u32   e_start;        /* first physical block extents lives */
85 +       __u32   e_num;          /* number of blocks covered by extent */
86 +};
87 +
88 +/*
89 + * this is index on-disk structure
90 + * it's used at all the levels, but the bottom
91 + */
92 +struct ext3_extent_idx {
93 +       __u32   e_block;        /* index covers logical blocks from 'block' */
94 +       __u32   e_leaf;         /* pointer to the physical block of the next *
95 +                                * level. leaf or next index could bet here */
96 +};
97 +
98 +/*
99 + * each block (leaves and indexes), even inode-stored has header
100 + */
101 +struct ext3_extent_header {    
102 +       __u16   e_num;          /* number of valid entries */
103 +       __u16   e_max;          /* capacity of store in entries */
104 +};
105 +
106 +/*
107 + * array of ext3_ext_path contains path to some extent
108 + * creation/lookup routines use it for traversal/splitting/etc
109 + * truncate uses it to simulate recursive walking
110 + */
111 +struct ext3_ext_path {
112 +       __u32                           p_block;
113 +       __u16                           p_depth;
114 +       struct ext3_extent              *p_ext;
115 +       struct ext3_extent_idx          *p_idx;
116 +       struct ext3_extent_header       *p_hdr;
117 +       struct buffer_head              *p_bh;
118 +};
119 +
120 +#define EXT_FIRST_EXTENT(__hdr__) \
121 +       ((struct ext3_extent *) (((char *) (__hdr__)) +         \
122 +                                sizeof(struct ext3_extent_header)))
123 +#define EXT_FIRST_INDEX(__hdr__) \
124 +       ((struct ext3_extent_idx *) (((char *) (__hdr__)) +     \
125 +                                    sizeof(struct ext3_extent_header)))
126 +#define EXT_HAS_FREE_INDEX(__path__) \
127 +       ((__path__)->p_hdr->e_num < (__path__)->p_hdr->e_max)
128 +#define EXT_LAST_EXTENT(__hdr__) \
129 +       (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_num - 1)
130 +#define EXT_LAST_INDEX(__hdr__) \
131 +       (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_num - 1)
132 +#define EXT_MAX_EXTENT(__hdr__) \
133 +       (EXT_FIRST_EXTENT((__hdr__)) + (__hdr__)->e_max - 1)
134 +#define EXT_MAX_INDEX(__hdr__) \
135 +       (EXT_FIRST_INDEX((__hdr__)) + (__hdr__)->e_max - 1)
136 +
137 +
138 +#define EXT_ASSERT(__x__) if (!(__x__)) BUG();
139 +
140 +/*
141 + * could return:
142 + *  - EROFS
143 + *  - ENOMEM
144 + */
145 +static int ext3_ext_get_access(handle_t *handle, struct inode *inode,
146 +                               struct ext3_ext_path *path)
147 +{
148 +       if (path->p_bh) {
149 +               /* path points to block */
150 +               return ext3_journal_get_write_access(handle, path->p_bh);
151 +       }
152 +
153 +       /* path points to leaf/index in inode body */
154 +       return 0;
155 +}
156 +
157 +/*
158 + * could return:
159 + *  - EROFS
160 + *  - ENOMEM
161 + *  - EIO
162 + */
163 +static int ext3_ext_dirty(handle_t *handle, struct inode *inode,
164 +                               struct ext3_ext_path *path)
165 +{
166 +       if (path->p_bh) {
167 +               /* path points to block */
168 +               return ext3_journal_dirty_metadata(handle, path->p_bh);
169 +       }
170 +
171 +       /* path points to leaf/index in inode body */
172 +       return ext3_mark_inode_dirty(handle, inode);
173 +}
174 +
175 +static inline int ext3_ext_space_block(struct inode *inode)
176 +{
177 +       int size;
178 +
179 +       size = (inode->i_sb->s_blocksize - sizeof(struct ext3_extent_header))
180 +               / sizeof(struct ext3_extent);
181 +#ifdef AGRESSIVE_TEST
182 +       size = 6; /* FIXME: for debug, remove this line */
183 +#endif
184 +       return size;
185 +}
186 +
187 +static inline int ext3_ext_space_inode(struct inode *inode)
188 +{
189 +       int size;
190 +
191 +       size = (sizeof(EXT3_I(inode)->i_data) -
192 +                       sizeof(struct ext3_extent_header))
193 +                       / sizeof(struct ext3_extent);
194 +#ifdef AGRESSIVE_TEST
195 +       size = 3; /* FIXME: for debug, remove this line */
196 +#endif
197 +       return size;
198 +}
199 +
200 +static inline int ext3_ext_space_inode_idx(struct inode *inode)
201 +{
202 +       int size;
203 +
204 +       size = (sizeof(EXT3_I(inode)->i_data) -
205 +                       sizeof(struct ext3_extent_header))
206 +                       / sizeof(struct ext3_extent_idx);
207 +#ifdef AGRESSIVE_TEST
208 +       size = 4; /* FIXME: for debug, remove this line */
209 +#endif
210 +       return size;
211 +}
212 +
213 +static void ext3_ext_show_path(struct inode *inode, struct ext3_ext_path *path)
214 +{
215 +       int k, l = path->p_depth;
216 +
217 +       ext_debug(inode, "path:");
218 +       for (k = 0; k <= l; k++, path++) {
219 +               if (path->p_idx) {
220 +                       ext_debug(inode, "  %d->%d", path->p_idx->e_block,
221 +                                       path->p_idx->e_leaf);
222 +               } else if (path->p_ext) {
223 +                       ext_debug(inode, "  %d:%d:%d",
224 +                                       path->p_ext->e_block,
225 +                                       path->p_ext->e_start,
226 +                                       path->p_ext->e_num);
227 +               } else
228 +                       ext_debug(inode, "  []");
229 +       }
230 +       ext_debug(inode, "\n");
231 +}
232 +
233 +static void ext3_ext_show_leaf(struct inode *inode, struct ext3_ext_path *path)
234 +{
235 +       int depth = EXT3_I(inode)->i_depth;
236 +       struct ext3_extent_header *eh = path[depth].p_hdr;
237 +       struct ext3_extent *ex = EXT_FIRST_EXTENT(eh);
238 +       int i;
239 +
240 +       for (i = 0; i < eh->e_num; i++, ex++) {
241 +               ext_debug(inode, "%d:%d:%d ",
242 +                               ex->e_block, ex->e_start, ex->e_num);
243 +       }
244 +       ext_debug(inode, "\n");
245 +}
246 +
247 +static void ext3_ext_drop_refs(struct inode *inode, struct ext3_ext_path *path)
248 +{
249 +       int depth = path->p_depth;
250 +       int i;
251 +
252 +       for (i = 0; i <= depth; i++, path++)
253 +               if (path->p_bh) {
254 +                       brelse(path->p_bh);
255 +                       path->p_bh = NULL;
256 +               }
257 +}
258 +
259 +static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path)
260 +{
261 +       struct ext3_inode_info *ei = EXT3_I(inode);
262 +       unsigned long bg_start;
263 +       unsigned long colour;
264 +       int depth;
265 +       
266 +       if (path) {
267 +               depth = path->p_depth;
268 +               /* try to find previous block */
269 +               if (path[depth].p_ext)
270 +                       return path[depth].p_ext->e_start +
271 +                               path[depth].p_ext->e_num - 1;
272 +               
273 +               /* it looks index is empty
274 +                * try to find starting from index itself */
275 +               if (path[depth].p_bh)
276 +                       return path[depth].p_bh->b_blocknr;
277 +       }
278 +
279 +       /* OK. use inode's group */
280 +       bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
281 +               le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
282 +       colour = (current->pid % 16) *
283 +                       (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
284 +       return bg_start + colour;
285 +}
286 +
287 +static struct ext3_ext_path *
288 +ext3_ext_find_extent(struct inode *inode, int block, struct ext3_ext_path *path)
289 +{
290 +       struct ext3_inode_info *ei = EXT3_I(inode);
291 +       struct ext3_extent_header *eh = (void *) ei->i_data;
292 +       struct ext3_extent_idx *ix;
293 +       struct buffer_head *bh;
294 +       struct ext3_extent *ex;
295 +       int depth, i, k, ppos = 0, prev = 0;
296 +       
297 +       eh = (struct ext3_extent_header *) ei->i_data;
298 +
299 +       /* initialize capacity of leaf in inode for first time */
300 +       if (eh->e_max == 0)
301 +               eh->e_max = ext3_ext_space_inode(inode);
302 +       i = depth = ei->i_depth;
303 +       EXT_ASSERT(i == 0 || eh->e_num > 0);
304 +       
305 +       /* account possible depth increase */
306 +       if (!path) {
307 +               path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
308 +                               GFP_NOFS);
309 +               if (!path)
310 +                       return ERR_PTR(-ENOMEM);
311 +       }
312 +       memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
313 +
314 +       /* walk through the tree */
315 +       while (i) {
316 +               ext_debug(inode, "depth %d: num %d, max %d\n",
317 +                               ppos, eh->e_num, eh->e_max);
318 +               ix = EXT_FIRST_INDEX(eh);
319 +               if (eh->e_num) {
320 +                       EXT_ASSERT(prev == 0 || ix->e_block == prev);
321 +                       path[ppos].p_idx = ix;
322 +               }
323 +               EXT_ASSERT(eh->e_num <= eh->e_max);
324 +               for (k = 0; k < eh->e_num; k++, ix++) {
325 +                       ext_debug(inode, "index: %d -> %d\n",
326 +                                       ix->e_block, ix->e_leaf);
327 +                       EXT_ASSERT((k == 0 && prev <= (int)ix->e_block) ||
328 +                                       (k > 0 && prev < (int)ix->e_block));
329 +                       if (block < ix->e_block)
330 +                               break;
331 +                       prev = ix->e_block;
332 +                       path[ppos].p_idx = ix;
333 +               }
334 +               path[ppos].p_block = path[ppos].p_idx->e_leaf;
335 +               path[ppos].p_depth = i;
336 +               path[ppos].p_hdr = eh;
337 +               path[ppos].p_ext = NULL;
338 +
339 +               bh = sb_bread(inode->i_sb, path[ppos].p_block);
340 +               if (!bh) {
341 +                       ext3_ext_drop_refs(inode, path);
342 +                       kfree(path);
343 +                       return ERR_PTR(-EIO);
344 +               }
345 +               eh = (struct ext3_extent_header *) bh->b_data;
346 +               ppos++;
347 +               EXT_ASSERT(ppos <= depth);
348 +               path[ppos].p_bh = bh;
349 +               i--;
350 +       }
351 +
352 +       path[ppos].p_depth = i;
353 +       path[ppos].p_hdr = eh;
354 +       path[ppos].p_ext = NULL;
355 +       
356 +       /* find extent */
357 +       ex = EXT_FIRST_EXTENT(eh);
358 +       if (eh->e_num)
359 +               path[ppos].p_ext = ex;
360 +       EXT_ASSERT(eh->e_num <= eh->e_max);
361 +       for (k = 0; k < eh->e_num; k++, ex++) {
362 +               EXT_ASSERT(ex->e_num < EXT3_BLOCKS_PER_GROUP(inode->i_sb));
363 +               EXT_ASSERT((k == 0 && prev <= (int)ex->e_block) ||
364 +                               (k > 0 && prev < (int)ex->e_block));
365 +               if (block < ex->e_block) 
366 +                       break;
367 +               prev = ex->e_block;
368 +               path[ppos].p_ext = ex;
369 +       }
370 +
371 +       ext3_ext_show_path(inode, path);
372 +
373 +       return path;
374 +}
375 +
376 +static void ext3_ext_check_boundary(struct inode *inode,
377 +                                       struct ext3_ext_path *curp,
378 +                                       void *addr, int len)
379 +{
380 +       void *end;
381 +
382 +       if (!len)
383 +               return;
384 +       if (curp->p_bh)
385 +               end = (void *) curp->p_hdr + inode->i_sb->s_blocksize;
386 +       else
387 +               end = (void *) curp->p_hdr + sizeof(EXT3_I(inode)->i_data);
388 +       if (((unsigned long) addr) + len > (unsigned long) end) {
389 +               printk("overflow! 0x%p > 0x%p\n", addr + len, end);
390 +               BUG();
391 +       }
392 +       if ((unsigned long) addr < (unsigned long) curp->p_hdr) {
393 +               printk("underflow! 0x%p < 0x%p\n", addr, curp->p_hdr);
394 +               BUG();
395 +       }
396 +}
397 +
398 +/*
399 + * insert new index [logical;ptr] into the block at cupr
400 + * it check where to insert: before curp or after curp
401 + */
402 +static int ext3_ext_insert_index(handle_t *handle, struct inode *inode,
403 +                               struct ext3_ext_path *curp, int logical,
404 +                               int ptr)
405 +{
406 +       struct ext3_extent_idx *ix;
407 +       int len, err;
408 +
409 +       if ((err = ext3_ext_get_access(handle, inode, curp)))
410 +               return err;
411 +
412 +       EXT_ASSERT(logical != curp->p_idx->e_block);
413 +       len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
414 +       if (logical > curp->p_idx->e_block) {
415 +               /* insert after */
416 +               len = (len - 1) * sizeof(struct ext3_extent_idx);
417 +               len = len < 0 ? 0 : len;
418 +               ext_debug(inode, "insert new index %d after: %d. "
419 +                               "move %d from 0x%p to 0x%p\n",
420 +                               logical, ptr, len,
421 +                               (curp->p_idx + 1), (curp->p_idx + 2));
422 +
423 +               ext3_ext_check_boundary(inode, curp, curp->p_idx + 2, len);
424 +               memmove(curp->p_idx + 2, curp->p_idx + 1, len);
425 +               ix = curp->p_idx + 1;
426 +       } else {
427 +               /* insert before */
428 +               len = len * sizeof(struct ext3_extent_idx);
429 +               len = len < 0 ? 0 : len;
430 +               ext_debug(inode, "insert new index %d before: %d. "
431 +                               "move %d from 0x%p to 0x%p\n",
432 +                               logical, ptr, len,
433 +                               curp->p_idx, (curp->p_idx + 1));
434 +
435 +               ext3_ext_check_boundary(inode, curp, curp->p_idx + 1, len);
436 +               memmove(curp->p_idx + 1, curp->p_idx, len);
437 +               ix = curp->p_idx;
438 +       }
439 +
440 +       ix->e_block = logical;
441 +       ix->e_leaf = ptr;
442 +       curp->p_hdr->e_num++;
443 +
444 +       err = ext3_ext_dirty(handle, inode, curp);
445 +       ext3_std_error(inode->i_sb, err);
446 +
447 +       return err;
448 +}
449 +
450 +/*
451 + * routine inserts new subtree into the path, using free index entry
452 + * at depth 'at:
453 + *  - allocates all needed blocks (new leaf and all intermediate index blocks)
454 + *  - makes decision where to split
455 + *  - moves remaining extens and index entries (right to the split point)
456 + *    into the newly allocated blocks
457 + *  - initialize subtree
458 + */
459 +static int ext3_ext_split(handle_t *handle, struct inode *inode,
460 +                               struct ext3_ext_path *path,
461 +                               struct ext3_extent *newext, int at)
462 +{
463 +       struct buffer_head *bh = NULL;
464 +       int depth = EXT3_I(inode)->i_depth;
465 +       struct ext3_extent_header *neh;
466 +       struct ext3_extent_idx *fidx;
467 +       struct ext3_extent *ex;
468 +       int i = at, k, m, a;
469 +       long newblock, oldblock, border;
470 +       int *ablocks = NULL; /* array of allocated blocks */
471 +       int err = 0;
472 +
473 +       /* make decision: where to split? */
474 +       /* FIXME: now desicion is simplest: at current extent */
475 +
476 +       /* if current leaf will be splitted, then we should use 
477 +        * border from split point */
478 +       
479 +       if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
480 +               border = path[depth].p_ext[1].e_block;
481 +               ext_debug(inode, "leaf will be splitted."
482 +                               " next leaf starts at %d\n",
483 +                               (int)border);
484 +       } else {
485 +               border = newext->e_block;
486 +               ext_debug(inode, "leaf will be added."
487 +                               " next leaf starts at %d\n",
488 +                               (int)border);
489 +       }
490 +
491 +       /* 
492 +        * if error occurs, then we break processing
493 +        * and turn filesystem read-only. so, index won't
494 +        * be inserted and tree will be in consistent
495 +        * state. next mount will repair buffers too
496 +        */
497 +
498 +       /*
499 +        * get array to track all allocated blocks
500 +        * we need this to handle errors and free blocks
501 +        * upon them
502 +        */
503 +       ablocks = kmalloc(sizeof(long) * depth, GFP_NOFS);
504 +       if (!ablocks)
505 +               return -ENOMEM;
506 +       memset(ablocks, 0, sizeof(long) * depth);
507 +
508 +       /* allocate all needed blocks */
509 +       ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at);
510 +       newblock = 0; /* FIXME: something more sophisticated needed here */ 
511 +       for (a = 0; newext->e_num > 0 && a < depth - at; a++) {
512 +               newblock = ablocks[a] = newext->e_start++;
513 +               newext->e_num--;
514 +       }
515 +       for (; a < depth - at; a++) {
516 +               newblock = ext3_new_block(handle, inode,
517 +                                               newblock + 1, 0, 0, &err);
518 +               if (newblock == 0)
519 +                       goto cleanup;
520 +               ablocks[a] = newblock;
521 +       }
522 +
523 +       /* initialize new leaf */
524 +       newblock = ablocks[--a];
525 +       EXT_ASSERT(newblock);
526 +       bh = sb_getblk(inode->i_sb, newblock);
527 +       if (!bh) {
528 +               err = -EIO;
529 +               goto cleanup;
530 +       }
531 +       lock_buffer(bh);
532 +
533 +       if ((err = ext3_journal_get_create_access(handle, bh)))
534 +               goto cleanup;
535 +
536 +       neh = (struct ext3_extent_header *) bh->b_data;
537 +       neh->e_num = 0;
538 +       neh->e_max = ext3_ext_space_block(inode);
539 +       ex = EXT_FIRST_EXTENT(neh);
540 +
541 +       /* move remain of path[depth] to the new leaf */
542 +       EXT_ASSERT(path[depth].p_hdr->e_num ==
543 +                       path[depth].p_hdr->e_max);
544 +       /* start copy from next extent */
545 +       /* TODO: we could do it by single memmove */
546 +       m = 0;
547 +       path[depth].p_ext++;
548 +       while (path[depth].p_ext <=
549 +                       EXT_MAX_EXTENT(path[depth].p_hdr)) {
550 +               ext_debug(inode, "move %d:%d:%d in new leaf\n",
551 +                               path[depth].p_ext->e_block,
552 +                               path[depth].p_ext->e_start,
553 +                               path[depth].p_ext->e_num);
554 +               memmove(ex++, path[depth].p_ext++,
555 +                               sizeof(struct ext3_extent));
556 +               neh->e_num++;
557 +               m++;
558 +       }
559 +       mark_buffer_uptodate(bh, 1);
560 +       unlock_buffer(bh);
561 +
562 +       if ((err = ext3_journal_dirty_metadata(handle, bh)))
563 +               goto cleanup;   
564 +       brelse(bh);
565 +       bh = NULL;
566 +
567 +       /* correct old leaf */
568 +       if (m) {
569 +               if ((err = ext3_ext_get_access(handle, inode, path)))
570 +                       goto cleanup;
571 +               path[depth].p_hdr->e_num -= m;
572 +               if ((err = ext3_ext_dirty(handle, inode, path)))
573 +                       goto cleanup;
574 +               
575 +       }
576 +
577 +       /* create intermediate indexes */
578 +       k = depth - at - 1;
579 +       EXT_ASSERT(k >= 0);
580 +       if (k)
581 +               ext_debug(inode,
582 +                               "create %d intermediate indices\n", k);
583 +       /* insert new index into current index block */
584 +       /* current depth stored in i var */
585 +       i = depth - 1;
586 +       while (k--) {
587 +               oldblock = newblock;
588 +               newblock = ablocks[--a];
589 +               bh = sb_getblk(inode->i_sb, newblock);
590 +               if (!bh) {
591 +                       err = -EIO;
592 +                       goto cleanup;
593 +               }
594 +               lock_buffer(bh);
595 +
596 +               if ((err = ext3_journal_get_create_access(handle, bh)))
597 +                       goto cleanup;
598 +
599 +               neh = (struct ext3_extent_header *) bh->b_data;
600 +               neh->e_num = 1;
601 +               neh->e_max = ext3_ext_space_block(inode);
602 +               fidx = EXT_FIRST_INDEX(neh);
603 +               fidx->e_block = border;
604 +               fidx->e_leaf = oldblock;
605 +
606 +               ext_debug(inode,
607 +                               "int.index at %d (block %u): %d -> %d\n",
608 +                               i, (unsigned) newblock,
609 +                               (int) border,
610 +                               (int) oldblock);
611 +               /* copy indexes */
612 +               m = 0;
613 +               path[i].p_idx++;
614 +               ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx,
615 +                               EXT_MAX_INDEX(path[i].p_hdr));
616 +               EXT_ASSERT(EXT_MAX_INDEX(path[i].p_hdr) ==
617 +                               EXT_LAST_INDEX(path[i].p_hdr));
618 +               while (path[i].p_idx <=
619 +                               EXT_MAX_INDEX(path[i].p_hdr)) {
620 +                       ext_debug(inode, "%d: move %d:%d in new index\n",
621 +                                       i, path[i].p_idx->e_block,
622 +                                       path[i].p_idx->e_leaf);
623 +                       memmove(++fidx, path[i].p_idx++,
624 +                                       sizeof(struct ext3_extent_idx));
625 +                       neh->e_num++;
626 +                       m++;
627 +               }
628 +
629 +               mark_buffer_uptodate(bh, 1);
630 +               unlock_buffer(bh);
631 +
632 +               if ((err = ext3_journal_dirty_metadata(handle, bh)))
633 +                       goto cleanup;
634 +               brelse(bh);
635 +               bh = NULL;
636 +
637 +               /* correct old index */
638 +               if (m) {
639 +                       err = ext3_ext_get_access(handle,inode,path+i);
640 +                       if (err)
641 +                               goto cleanup;
642 +                       path[i].p_hdr->e_num -= m;
643 +                       err = ext3_ext_dirty(handle, inode, path + i);
644 +                       if (err)
645 +                               goto cleanup;
646 +               }
647 +
648 +               i--;
649 +       }
650 +
651 +       /* insert new index */
652 +       if (!err)
653 +               err = ext3_ext_insert_index(handle, inode, path + at,
654 +                                               border, newblock);
655 +
656 +cleanup:
657 +       if (bh) {
658 +               if (buffer_locked(bh))
659 +                       unlock_buffer(bh);
660 +               brelse(bh);
661 +       }
662 +
663 +       if (err) {
664 +               /* free all allocated blocks in error case */
665 +               for (i = 0; i < depth; i++)
666 +                       if (!ablocks[i])
667 +                               continue;
668 +                       ext3_free_blocks(handle, inode, ablocks[i], 1);
669 +       }
670 +       kfree(ablocks);
671 +
672 +       return err;
673 +}
674 +
675 +/*
676 + * routine implements tree growing procedure:
677 + *  - allocates new block
678 + *  - moves top-level data (index block or leaf) into the new block
679 + *  - initialize new top-level, creating index that points to the
680 + *    just created block
681 + */
682 +static int ext3_ext_grow_indepth(handle_t *handle, struct inode *inode,
683 +                                       struct ext3_ext_path *path,
684 +                                       struct ext3_extent *newext)
685 +{
686 +       struct buffer_head *bh;
687 +       struct ext3_ext_path *curp = path;
688 +       struct ext3_extent_header *neh;
689 +       struct ext3_extent_idx *fidx;
690 +       int len, err = 0;
691 +       long newblock;
692 +
693 +       /*
694 +        * use already allocated by the called block for new root block
695 +        */
696 +       newblock = newext->e_start++;
697 +       if (newext->e_num == 0) {
698 +               /* 
699 +                * FIXME: if this may happen, then we have to handle
700 +                * possible error and free allocated block
701 +                */
702 +               printk("grow_indepth with zero blocks\n");
703 +               newblock = ext3_new_block(handle, inode,
704 +                                               newblock, 0, 0, &err);
705 +       } else
706 +               newext->e_num--;
707 +       
708 +       bh = sb_getblk(inode->i_sb, newblock);
709 +       if (!bh) {
710 +               err = -EIO;
711 +               ext3_std_error(inode->i_sb, err);
712 +               return err;
713 +       }
714 +       lock_buffer(bh);
715 +
716 +       if ((err = ext3_journal_get_create_access(handle, bh))) {
717 +               unlock_buffer(bh);
718 +               goto out;       
719 +       }
720 +
721 +       /* move top-level index/leaf into new block */
722 +       len = sizeof(struct ext3_extent_header) +
723 +               sizeof(struct ext3_extent) * curp->p_hdr->e_max;
724 +       EXT_ASSERT(len >= 0 && len < 4096);
725 +       memmove(bh->b_data, curp->p_hdr, len);
726 +
727 +       /* set size of new block */
728 +       neh = (struct ext3_extent_header *) bh->b_data;
729 +       neh->e_max = ext3_ext_space_block(inode);
730 +       mark_buffer_uptodate(bh, 1);
731 +       unlock_buffer(bh);
732 +
733 +       if ((err = ext3_journal_dirty_metadata(handle, bh)))
734 +               goto out;
735 +
736 +       /* create index in new top-level index: num,max,pointer */
737 +       if ((err = ext3_ext_get_access(handle, inode, curp)))
738 +               goto out;
739 +
740 +       curp->p_hdr->e_max = ext3_ext_space_inode_idx(inode);
741 +       curp->p_hdr->e_num = 1;
742 +       curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
743 +       curp->p_idx->e_block = EXT_FIRST_EXTENT(path[0].p_hdr)->e_block;
744 +       curp->p_idx->e_leaf = newblock;
745 +
746 +       neh = (struct ext3_extent_header *) EXT3_I(inode)->i_data;
747 +       fidx = EXT_FIRST_INDEX(neh);
748 +       ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %d\n",
749 +                       neh->e_num, neh->e_max, fidx->e_block, fidx->e_leaf); 
750 +
751 +       EXT3_I(inode)->i_depth++;
752 +       err = ext3_ext_dirty(handle, inode, curp);
753 +out:
754 +       brelse(bh);
755 +
756 +       return err;
757 +}
758 +
759 +/*
760 + * routine finds empty index and adds new leaf. if no free index found
761 + * then it requests in-depth growing
762 + */
763 +static int ext3_ext_create_new_leaf(handle_t *handle, struct inode *inode,
764 +                                       struct ext3_ext_path *path,
765 +                                       struct ext3_extent *newext)
766 +{
767 +       long newblock = newext->e_start;
768 +       struct ext3_ext_path *curp;
769 +       int depth, i, err = 0;
770 +
771 +repeat:
772 +       i = depth = EXT3_I(inode)->i_depth;
773 +       
774 +       /* walk up to the tree and look for free index entry */
775 +       curp = path + depth;
776 +       while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
777 +               i--;
778 +               curp--;
779 +       }
780 +
781 +       /* we use already allocated block for index block
782 +        * so, subsequent data blocks should be contigoues */
783 +       if (EXT_HAS_FREE_INDEX(curp)) {
784 +               /* if we found index with free entry, then use that
785 +                * entry: create all needed subtree and add new leaf */
786 +               err = ext3_ext_split(handle, inode, path, newext, i);
787 +
788 +               /* refill path */
789 +               ext3_ext_drop_refs(inode, path);
790 +               path = ext3_ext_find_extent(inode, newext->e_block, path);
791 +               if (IS_ERR(path))
792 +                       err = PTR_ERR(path);
793 +       } else {
794 +               /* tree is full, time to grow in depth */
795 +               err = ext3_ext_grow_indepth(handle, inode, path, newext);
796 +
797 +               /* refill path */
798 +               ext3_ext_drop_refs(inode, path);
799 +               path = ext3_ext_find_extent(inode, newext->e_block, path);
800 +               if (IS_ERR(path))
801 +                       err = PTR_ERR(path);
802 +       
803 +               /*
804 +                * only first (depth 0 -> 1) produces free space
805 +                * in all other cases we have to split growed tree
806 +                */
807 +               depth = EXT3_I(inode)->i_depth;
808 +               if (path[depth].p_hdr->e_num == path[depth].p_hdr->e_max) {
809 +                       /* now we need split */
810 +                       goto repeat;
811 +               }
812 +       }
813 +
814 +       if (err)
815 +               return err;
816 +
817 +       /*
818 +        * probably we've used some blocks from extent
819 +        * let's allocate new block for it
820 +        */
821 +       if (newext->e_num == 0 && !err) {
822 +               newext->e_start =
823 +                       ext3_new_block(handle, inode, newblock,
824 +                                       0, 0, &err);
825 +               if (newext->e_start != 0)
826 +                       newext->e_num = 1;
827 +       }
828 +
829 +       return 0;
830 +}
831 +
832 +/*
833 + * returns next allocated block or 0xffffffff
834 + * NOTE: it consider block number from index entry as
835 + * allocated block. thus, index entries have to be consistent
836 + * with leafs
837 + */
838 +static inline unsigned ext3_ext_next_allocated_block(struct inode *inode,
839 +                                               struct ext3_ext_path *path)
840 +{
841 +       int depth;
842 +
843 +       EXT_ASSERT(path != NULL);
844 +       depth = path->p_depth;
845 +
846 +       if (depth == 0 && path->p_ext == NULL)
847 +               return 0xffffffff;
848 +
849 +       /* FIXME: what if index isn't full ?! */
850 +       while (depth >= 0) {
851 +               if (depth == path->p_depth) {
852 +                       /* leaf */
853 +                       if (path[depth].p_ext !=
854 +                                       EXT_LAST_EXTENT(path[depth].p_hdr))
855 +                               return path[depth].p_ext[1].e_block;
856 +               } else {
857 +                       /* index */
858 +                       if (path[depth].p_idx !=
859 +                                       EXT_LAST_INDEX(path[depth].p_hdr))
860 +                               return path[depth].p_idx[1].e_block;
861 +               }
862 +               depth--;        
863 +       }
864 +
865 +       return 0xffffffff;
866 +}
867 +
868 +/*
869 + * returns first allocated block from next leaf or 0xffffffff
870 + */
871 +static unsigned ext3_ext_next_leaf_block(struct inode *inode,
872 +                                               struct ext3_ext_path *path)
873 +{
874 +       int depth;
875 +
876 +       EXT_ASSERT(path != NULL);
877 +       depth = path->p_depth;
878 +
879 +       /* zero-tree has no leaf blocks at all */
880 +       if (depth == 0)
881 +               return 0xffffffff;
882 +
883 +       /* go to index block */
884 +       depth--;
885 +       
886 +       while (depth >= 0) {
887 +               if (path[depth].p_idx !=
888 +                               EXT_LAST_INDEX(path[depth].p_hdr))
889 +                       return path[depth].p_idx[1].e_block;
890 +               depth--;        
891 +       }
892 +
893 +       return 0xffffffff;
894 +}
895 +
896 +/*
897 + * if leaf gets modified and modified extent is first in the leaf
898 + * then we have to correct all indexes above
899 + * TODO: do we need to correct tree in all cases?
900 + */
901 +int ext3_ext_correct_indexes(handle_t *handle, struct inode *inode,
902 +                               struct ext3_ext_path *path)
903 +{
904 +       int depth = EXT3_I(inode)->i_depth;     
905 +       struct ext3_extent_header *eh;
906 +       struct ext3_extent *ex;
907 +       long border;
908 +       int k, err = 0;
909 +       
910 +       eh = path[depth].p_hdr;
911 +       ex = path[depth].p_ext;
912 +
913 +       EXT_ASSERT(ex);
914 +       EXT_ASSERT(eh);
915 +       
916 +       if (depth == 0) {
917 +               /* there is no tree at all */
918 +               return 0;
919 +       }
920 +       
921 +       if (ex != EXT_FIRST_EXTENT(eh)) {
922 +               /* we correct tree if first leaf got modified only */
923 +               return 0;
924 +       }
925 +       
926 +       /*
927 +        * TODO: we need correction if border is smaller then current one
928 +        */
929 +       k = depth - 1;
930 +       border = path[depth].p_ext->e_block;
931 +       if ((err = ext3_ext_get_access(handle, inode, path + k)))
932 +               return err;
933 +       path[k].p_idx->e_block = border;
934 +       if ((err = ext3_ext_dirty(handle, inode, path + k)))
935 +               return err;
936 +
937 +       while (k--) {
938 +               /* change all left-side indexes */
939 +               if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
940 +                       break;
941 +               if ((err = ext3_ext_get_access(handle, inode, path + k)))
942 +                       break;
943 +               path[k].p_idx->e_block = border;
944 +               if ((err = ext3_ext_dirty(handle, inode, path + k)))
945 +                       break;
946 +       }
947 +
948 +       return err;
949 +}
950 +
951 +/*
952 + * this routine tries to merge requsted extent into the existing
953 + * extent or inserts requested extent as new one into the tree,
954 + * creating new leaf in no-space case
955 + */
956 +int ext3_ext_insert_extent(handle_t *handle, struct inode *inode,
957 +                               struct ext3_ext_path *path,
958 +                               struct ext3_extent *newext)
959 +{
960 +       int depth, len;
961 +       struct ext3_extent_header * eh;
962 +       struct ext3_extent *ex;
963 +       struct ext3_extent *nearex; /* nearest extent */
964 +       struct ext3_ext_path *npath = NULL;
965 +       int err;
966 +
967 +       depth = EXT3_I(inode)->i_depth; 
968 +       if ((ex = path[depth].p_ext)) {
969 +               /* try to insert block into found extent and return */
970 +               if (ex->e_block + ex->e_num == newext->e_block &&
971 +                               ex->e_start + ex->e_num == newext->e_start) {
972 +#ifdef AGRESSIVE_TEST
973 +                       if (ex->e_num >= 2)
974 +                               goto repeat;
975 +#endif
976 +                       if ((err = ext3_ext_get_access(handle, inode,
977 +                                                       path + depth)))
978 +                               return err;
979 +                       ext_debug(inode, "append %d block to %d:%d (from %d)\n",
980 +                                       newext->e_num, ex->e_block, ex->e_num,
981 +                                       ex->e_start);
982 +                       ex->e_num += newext->e_num;
983 +                       err = ext3_ext_dirty(handle, inode, path + depth);
984 +                       return err;
985 +               }
986 +       }
987 +
988 +repeat:
989 +       depth = EXT3_I(inode)->i_depth; 
990 +       eh = path[depth].p_hdr;
991 +       if (eh->e_num == eh->e_max) {
992 +               /* probably next leaf has space for us? */
993 +               int next = ext3_ext_next_leaf_block(inode, path);
994 +               if (next != 0xffffffff) {
995 +                       ext_debug(inode, "next leaf block - %d\n", next);
996 +                       EXT_ASSERT(!npath);
997 +                       npath = ext3_ext_find_extent(inode, next, NULL);
998 +                       if (IS_ERR(npath))
999 +                               return PTR_ERR(npath);
1000 +                       EXT_ASSERT(npath->p_depth == path->p_depth);
1001 +                       eh = npath[depth].p_hdr;
1002 +                       if (eh->e_num < eh->e_max) {
1003 +                               ext_debug(inode,
1004 +                                               "next leaf has free ext(%d)\n",
1005 +                                               eh->e_num);
1006 +                               path = npath;
1007 +                               goto repeat;
1008 +                       }
1009 +                       ext_debug(inode, "next leaf hasno free space(%d,%d)\n",
1010 +                                       eh->e_num, eh->e_max);
1011 +               }
1012 +               /*
1013 +                * there is no free space in found leaf
1014 +                * we're gonna add new leaf in the tree
1015 +                */
1016 +               err = ext3_ext_create_new_leaf(handle, inode, path, newext);
1017 +               if (err)
1018 +                       goto cleanup;
1019 +               goto repeat;
1020 +       }
1021 +
1022 +       nearex = path[depth].p_ext;
1023 +
1024 +       if ((err = ext3_ext_get_access(handle, inode, path + depth)))
1025 +               goto cleanup;
1026 +
1027 +       if (!nearex) {
1028 +               /* there is no extent in this leaf, create first one */
1029 +               ext_debug(inode, "first extent in the leaf: %d:%d:%d\n",
1030 +                               newext->e_block, newext->e_start,
1031 +                               newext->e_num);
1032 +               path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1033 +       } else if (newext->e_block > nearex->e_block) {
1034 +               EXT_ASSERT(newext->e_block != nearex->e_block);
1035 +               len = EXT_MAX_EXTENT(eh) - nearex;
1036 +               len = (len - 1) * sizeof(struct ext3_extent);
1037 +               len = len < 0 ? 0 : len;
1038 +               ext_debug(inode, "insert %d:%d:%d after: nearest 0x%p, "
1039 +                               "move %d from 0x%p to 0x%p\n",
1040 +                               newext->e_block, newext->e_start, newext->e_num,
1041 +                               nearex, len, nearex + 1, nearex + 2);
1042 +               ext3_ext_check_boundary(inode, path + depth, nearex + 2, len);
1043 +               memmove(nearex + 2, nearex + 1, len);
1044 +               path[depth].p_ext = nearex + 1;
1045 +       } else {
1046 +               EXT_ASSERT(newext->e_block != nearex->e_block);
1047 +               len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext3_extent);
1048 +               len = len < 0 ? 0 : len;
1049 +               ext_debug(inode, "insert %d:%d:%d before: nearest 0x%p, "
1050 +                               "move %d from 0x%p to 0x%p\n",
1051 +                               newext->e_block, newext->e_start, newext->e_num,
1052 +                               nearex, len, nearex + 1, nearex + 2);
1053 +               
1054 +               memmove(nearex + 1, nearex, len);
1055 +               path[depth].p_ext = nearex;
1056 +       }
1057 +
1058 +       if (!err) {
1059 +               eh->e_num++;
1060 +               nearex = path[depth].p_ext;
1061 +               nearex->e_block = newext->e_block;
1062 +               nearex->e_start = newext->e_start;
1063 +               nearex->e_num = newext->e_num;
1064 +               EXT_ASSERT(nearex->e_num < EXT3_BLOCKS_PER_GROUP(inode->i_sb) &&
1065 +                               nearex->e_num > 0);
1066 +
1067 +               /* time to correct all indexes above */
1068 +               err = ext3_ext_correct_indexes(handle, inode, path);
1069 +       }
1070 +
1071 +       err = ext3_ext_dirty(handle, inode, path + depth);
1072 +
1073 +cleanup:
1074 +       if (npath) {
1075 +               ext3_ext_drop_refs(inode, npath);
1076 +               kfree(npath);
1077 +       }
1078 +               
1079 +       return err;
1080 +}
1081 +
1082 +int ext3_ext_get_block(handle_t *handle, struct inode *inode, long iblock,
1083 +                       struct buffer_head *bh_result, int create,
1084 +                       int extend_disksize)
1085 +{
1086 +       struct ext3_ext_path *path;
1087 +       int depth = EXT3_I(inode)->i_depth;
1088 +       struct ext3_extent newex;
1089 +       struct ext3_extent *ex;
1090 +       int goal, newblock, err = 0;
1091 +
1092 +       ext_debug(inode, "block %d requested for inode %u, bh_result 0x%p\n",
1093 +                       (int) iblock, (unsigned) inode->i_ino, bh_result);
1094 +       bh_result->b_state &= ~(1UL << BH_New);
1095 +
1096 +       down(&EXT3_I(inode)->i_ext_sem);
1097 +
1098 +       /* find extent for this block */
1099 +       path = ext3_ext_find_extent(inode, iblock, NULL);
1100 +       if (IS_ERR(path)) {
1101 +               err = PTR_ERR(path);
1102 +               goto out2;
1103 +       }
1104 +
1105 +       if ((ex = path[depth].p_ext)) {
1106 +               /* if found exent covers block, simple return it */
1107 +               if (iblock >= ex->e_block && iblock < ex->e_block + ex->e_num) {
1108 +                       newblock = iblock - ex->e_block + ex->e_start;
1109 +                       ext_debug(inode, "%d fit into %d:%d -> %d\n",
1110 +                                       (int) iblock, ex->e_block, ex->e_num,
1111 +                                       newblock);
1112 +                       goto out;
1113 +               }
1114 +       }
1115 +
1116 +       /*
1117 +        * we couldn't try to create block if create flag is zero 
1118 +        */
1119 +       if (!create) 
1120 +               goto out2;
1121 +
1122 +       /* allocate new block */
1123 +       goal = ext3_ext_find_goal(inode, path);
1124 +       newblock = ext3_new_block(handle, inode, goal, 0, 0, &err);
1125 +       if (!newblock)
1126 +               goto out2;
1127 +       ext_debug(inode, "allocate new block: goal %d, found %d\n",
1128 +                       goal, newblock);
1129 +
1130 +       /* try to insert new extent into found leaf and return */
1131 +       newex.e_block = iblock;
1132 +       newex.e_start = newblock;
1133 +       newex.e_num = 1;
1134 +       err = ext3_ext_insert_extent(handle, inode, path, &newex);
1135 +       if (err)
1136 +               goto out2;
1137 +       
1138 +       /* previous routine could use block we allocated */
1139 +       newblock = newex.e_start;
1140 +       bh_result->b_state |= (1UL << BH_New);
1141 +
1142 +out:
1143 +       ext3_ext_show_leaf(inode, path);
1144 +       bh_result->b_dev = inode->i_dev;
1145 +       bh_result->b_blocknr = newblock;
1146 +       bh_result->b_state |= (1UL << BH_Mapped);
1147 +out2:
1148 +       ext3_ext_drop_refs(inode, path);
1149 +       kfree(path);
1150 +       up(&EXT3_I(inode)->i_ext_sem);
1151 +
1152 +       return err;     
1153 +}
1154 +
1155 +/*
1156 + * returns 1 if current index have to be freed (even partial)
1157 + */
1158 +static int ext3_ext_more_to_truncate(struct inode *inode,
1159 +                               struct ext3_ext_path *path)
1160 +{
1161 +       EXT_ASSERT(path->p_idx);
1162 +
1163 +       if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1164 +               return 0;
1165 +
1166 +       /*
1167 +        * if truncate on deeper level happened it it wasn't partial
1168 +        * so we have to consider current index for truncation
1169 +        */
1170 +       if (path->p_hdr->e_num == path->p_block)
1171 +               return 0;
1172 +
1173 +       /*
1174 +        * put actual number of indexes to know is this number got
1175 +        * changed at the next iteration
1176 +        */
1177 +       path->p_block = path->p_hdr->e_num;
1178 +       
1179 +       return 1;
1180 +}
1181 +
1182 +/*
1183 + * routine removes index from the index block
1184 + * it's used in truncate case only. thus all requests are for
1185 + * last index in the block only
1186 + */
1187 +int ext3_ext_remove_index(handle_t *handle, struct inode *inode,
1188 +                                       struct ext3_ext_path *path)
1189 +{
1190 +       struct buffer_head *bh;
1191 +       int err;
1192 +       
1193 +       /* free index block */
1194 +       path--;
1195 +       EXT_ASSERT(path->p_hdr->e_num);
1196 +       if ((err = ext3_ext_get_access(handle, inode, path)))
1197 +               return err;
1198 +       path->p_hdr->e_num--;
1199 +       if ((err = ext3_ext_dirty(handle, inode, path)))
1200 +               return err;
1201 +       bh = sb_get_hash_table(inode->i_sb, path->p_idx->e_leaf);
1202 +       ext3_forget(handle, 0, inode, bh, path->p_idx->e_leaf);
1203 +       ext3_free_blocks(handle, inode, path->p_idx->e_leaf, 1);
1204 +
1205 +       ext_debug(inode, "index is empty, remove it, free block %d\n",
1206 +                       path->p_idx->e_leaf);
1207 +       return err;
1208 +}
1209 +
1210 +/*
1211 + * returns 1 if current extent needs to be freed (even partial)
1212 + * instead, returns 0
1213 + */
1214 +int ext3_ext_more_leaves_to_truncate(struct inode *inode,
1215 +                                       struct ext3_ext_path *path)
1216 +{
1217 +       unsigned blocksize = inode->i_sb->s_blocksize;
1218 +       struct ext3_extent *ex = path->p_ext;
1219 +       int last_block; 
1220 +
1221 +       EXT_ASSERT(ex);
1222 +
1223 +       /* is there leave in the current leaf? */
1224 +       if (ex < EXT_FIRST_EXTENT(path->p_hdr))
1225 +               return 0;
1226 +       
1227 +       last_block = (inode->i_size + blocksize-1)
1228 +                       >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
1229 +
1230 +       if (last_block >= ex->e_block + ex->e_num)
1231 +               return 0;
1232 +
1233 +       /* seems it extent have to be freed */
1234 +       return 1;
1235 +}
1236 +
1237 +handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
1238 +{
1239 +       int err;
1240 +
1241 +       if (handle->h_buffer_credits > needed)
1242 +               return handle;
1243 +       if (!ext3_journal_extend(handle, needed))
1244 +               return handle;
1245 +       err = ext3_journal_restart(handle, needed);
1246 +       
1247 +       return handle;
1248 +}
1249 +
1250 +/*
1251 + * this routine calculate max number of blocks to be modified
1252 + * while freeing extent and is intended to be used in truncate path
1253 + */
1254 +static int ext3_ext_calc_credits(struct inode *inode,
1255 +                                       struct ext3_ext_path *path,
1256 +                                       int num)
1257 +{
1258 +       int depth = EXT3_I(inode)->i_depth;
1259 +       int needed;
1260 +       
1261 +       /*
1262 +        * extent couldn't cross group, so we will modify
1263 +        * single bitmap block and single group descriptor
1264 +        */
1265 +       needed = 3;
1266 +
1267 +       /*
1268 +        * if this is last extent in a leaf, then we have to
1269 +        * free leaf block and remove pointer from index above.
1270 +        * that pointer could be last in index block, so we'll
1271 +        * have to remove it too. this way we could modify/free
1272 +        * the whole path + root index (inode stored) will be
1273 +        * modified
1274 +        */
1275 +       if (!path || (num == path->p_ext->e_num &&
1276 +                               path->p_ext == EXT_FIRST_EXTENT(path->p_hdr)))
1277 +               needed += (depth * (EXT3_ALLOC_NEEDED + 1)) + 1;
1278 +
1279 +       return needed;
1280 +}
1281 +
1282 +/*
1283 + * core of the truncate procedure:
1284 + * - calculated what part of each extent in the requested leaf
1285 + *   need to be freed
1286 + * - frees and forgets these blocks
1287 + *
1288 + * TODO: we could optimize and free several extents during
1289 + *       single journal_restart()-journal_restart() cycle
1290 + */
1291 +static int ext3_ext_truncate_leaf(handle_t *handle,
1292 +                                       struct inode *inode,
1293 +                                       struct ext3_ext_path *path,
1294 +                                       int depth)
1295 +{
1296 +       unsigned blocksize = inode->i_sb->s_blocksize;
1297 +       int last_block; 
1298 +       int i, err = 0, sf, num;
1299 +
1300 +       ext_debug(inode, "level %d - leaf\n", depth);
1301 +       if (!path->p_hdr)
1302 +               path->p_hdr =
1303 +                       (struct ext3_extent_header *) path->p_bh->b_data;
1304 +
1305 +       EXT_ASSERT(path->p_hdr->e_num <= path->p_hdr->e_max);
1306 +       
1307 +       last_block = (inode->i_size + blocksize-1)
1308 +                                       >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
1309 +       path->p_ext = EXT_LAST_EXTENT(path->p_hdr);
1310 +       while (ext3_ext_more_leaves_to_truncate(inode, path)) {
1311 +
1312 +               /* what part of extent have to be freed? */
1313 +               sf = last_block > path->p_ext->e_block ?
1314 +                       last_block : path->p_ext->e_block;
1315 +
1316 +               /* number of blocks from extent to be freed */
1317 +               num = path->p_ext->e_block + path->p_ext->e_num - sf;
1318 +
1319 +               /* calc physical first physical block to be freed */
1320 +               sf = path->p_ext->e_start + (sf - path->p_ext->e_block);
1321 +
1322 +               i = ext3_ext_calc_credits(inode, path, num);
1323 +               handle = ext3_ext_journal_restart(handle, i);
1324 +               if (IS_ERR(handle))
1325 +                       return PTR_ERR(handle);
1326 +               
1327 +               ext_debug(inode, "free extent %d:%d:%d -> free %d:%d\n",
1328 +                               path->p_ext->e_block, path->p_ext->e_start,
1329 +                               path->p_ext->e_num, sf, num);
1330 +               for (i = 0; i < num; i++) {
1331 +                       struct buffer_head *bh =
1332 +                               sb_get_hash_table(inode->i_sb, sf + i);
1333 +                       ext3_forget(handle, 0, inode, bh, sf + i);
1334 +               }
1335 +               ext3_free_blocks(handle, inode, sf, num);
1336 +
1337 +               /* collect extents usage stats */
1338 +               spin_lock(&EXT3_SB(inode->i_sb)->s_ext_lock);
1339 +               EXT3_SB(inode->i_sb)->s_ext_extents++;
1340 +               EXT3_SB(inode->i_sb)->s_ext_blocks += num;
1341 +               spin_unlock(&EXT3_SB(inode->i_sb)->s_ext_lock);
1342 +
1343 +               /* reduce extent */
1344 +               if ((err = ext3_ext_get_access(handle, inode, path)))
1345 +                       return err;
1346 +               path->p_ext->e_num -= num;
1347 +               if (path->p_ext->e_num == 0)
1348 +                       path->p_hdr->e_num--;
1349 +               if ((err = ext3_ext_dirty(handle, inode, path)))
1350 +                       return err;
1351 +
1352 +               path->p_ext--;
1353 +       }
1354 +       
1355 +       /* if this leaf is free, then we should
1356 +        * remove it from index block above */
1357 +       if (path->p_hdr->e_num == 0 && depth > 0) 
1358 +               err = ext3_ext_remove_index(handle, inode, path);
1359 +
1360 +       return err;
1361 +}
1362 +
1363 +static void ext3_ext_collect_stats(struct inode *inode)
1364 +{
1365 +       int depth;
1366 +       
1367 +       /* skip inodes with old good bitmap */
1368 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
1369 +               return;
1370 +       
1371 +       /* collect on full truncate only */
1372 +       if (inode->i_size)
1373 +               return;
1374 +
1375 +       depth = EXT3_I(inode)->i_depth;
1376 +       if (depth < EXT3_SB(inode->i_sb)->s_ext_mindepth)
1377 +                EXT3_SB(inode->i_sb)->s_ext_mindepth = depth;
1378 +       if (depth > EXT3_SB(inode->i_sb)->s_ext_maxdepth)
1379 +                EXT3_SB(inode->i_sb)->s_ext_maxdepth = depth;
1380 +       EXT3_SB(inode->i_sb)->s_ext_sum += depth;
1381 +       EXT3_SB(inode->i_sb)->s_ext_count++;
1382 +       
1383 +}
1384 +
1385 +void ext3_ext_truncate(struct inode * inode)
1386 +{
1387 +       struct address_space *mapping = inode->i_mapping;
1388 +       struct ext3_ext_path *path;
1389 +       struct page * page;
1390 +       handle_t *handle;
1391 +       int i, depth, err = 0;
1392 +
1393 +       ext3_ext_collect_stats(inode);
1394 +
1395 +       /*
1396 +        * We have to lock the EOF page here, because lock_page() nests
1397 +        * outside journal_start().
1398 +        */
1399 +       if ((inode->i_size & (inode->i_sb->s_blocksize - 1)) == 0) {
1400 +               /* Block boundary? Nothing to do */
1401 +               page = NULL;
1402 +       } else {
1403 +               page = grab_cache_page(mapping,
1404 +                               inode->i_size >> PAGE_CACHE_SHIFT);
1405 +               if (!page)
1406 +                       return;
1407 +       }
1408 +
1409 +       /*
1410 +        * probably first extent we're gonna free will be last in block
1411 +        */
1412 +       i = ext3_ext_calc_credits(inode, NULL, 0);
1413 +       handle = ext3_journal_start(inode, i);
1414 +       if (IS_ERR(handle)) {
1415 +               if (page) {
1416 +                       clear_highpage(page);
1417 +                       flush_dcache_page(page);
1418 +                       unlock_page(page);
1419 +                       page_cache_release(page);
1420 +               }
1421 +               return;
1422 +       }
1423 +
1424 +       if (page)
1425 +               ext3_block_truncate_page(handle, mapping, inode->i_size, page,
1426 +                                               inode->i_sb->s_blocksize);
1427 +
1428 +       down(&EXT3_I(inode)->i_ext_sem);
1429 +
1430 +       /* 
1431 +        * TODO: optimization is possible here
1432 +        * probably we need not scaning at all,
1433 +        * because page truncation is enough
1434 +        */
1435 +       if (ext3_orphan_add(handle, inode))
1436 +               goto out_stop;
1437 +
1438 +       /* we have to know where to truncate from in crash case */
1439 +       EXT3_I(inode)->i_disksize = inode->i_size;
1440 +       ext3_mark_inode_dirty(handle, inode);
1441 +
1442 +       /*
1443 +        * we start scanning from right side freeing all the blocks
1444 +        * after i_size and walking into the deep
1445 +        */
1446 +       i = 0;
1447 +       depth = EXT3_I(inode)->i_depth;
1448 +       path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 1), GFP_KERNEL);
1449 +       if (IS_ERR(path)) {
1450 +               ext3_error(inode->i_sb, "ext3_ext_truncate",
1451 +                               "Can't allocate path array");
1452 +               goto out_stop;
1453 +       }
1454 +       memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
1455 +
1456 +       path[i].p_hdr = (struct ext3_extent_header *) EXT3_I(inode)->i_data;
1457 +       while (i >= 0 && err == 0) {
1458 +               if (i == depth) {
1459 +                       /* this is leaf block */
1460 +                       err = ext3_ext_truncate_leaf(handle, inode,
1461 +                                                       path + i, i);
1462 +                       /* root level have p_bh == NULL, brelse() eats this */
1463 +                       brelse(path[i].p_bh);
1464 +                       i--;
1465 +                       continue;
1466 +               }
1467 +               
1468 +               /* this is index block */
1469 +               if (!path[i].p_hdr) {
1470 +                       path[i].p_hdr =
1471 +                               (struct ext3_extent_header *) path[i].p_bh->b_data;
1472 +                       ext_debug(inode, "initialize header\n");
1473 +               }
1474 +
1475 +               EXT_ASSERT(path[i].p_hdr->e_num <= path[i].p_hdr->e_max);
1476 +               
1477 +               if (!path[i].p_idx) {
1478 +                       /* this level hasn't touched yet */
1479 +                       path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1480 +                       path[i].p_block = path[i].p_hdr->e_num + 1;
1481 +                       ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n",
1482 +                                       path[i].p_hdr, path[i].p_hdr->e_num);
1483 +               } else {
1484 +                       /* we've already was here, see at next index */
1485 +                       path[i].p_idx--;
1486 +               }
1487 +
1488 +               ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n",
1489 +                               i, EXT_FIRST_INDEX(path[i].p_hdr),
1490 +                               path[i].p_idx);
1491 +               if (ext3_ext_more_to_truncate(inode, path + i)) {
1492 +                       /* go to the next level */
1493 +                       ext_debug(inode, "move to level %d (block %d)\n", i+1,
1494 +                                       path[i].p_idx->e_leaf);
1495 +                       memset(path + i + 1, 0, sizeof(*path));
1496 +                       path[i+1].p_bh = sb_bread(inode->i_sb,
1497 +                                                       path[i].p_idx->e_leaf);
1498 +                       if (!path[i+1].p_bh) {
1499 +                               /* should we reset i_size? */
1500 +                               err = -EIO;
1501 +                               break;
1502 +                       }
1503 +                       i++;
1504 +               } else {
1505 +                       /* we finish processing this index, go up */
1506 +                       if (path[i].p_hdr->e_num == 0 && i > 0) {
1507 +                               /* index is empty, remove it
1508 +                                * handle must be already prepared by the
1509 +                                * truncate_leaf()
1510 +                                */
1511 +                               err = ext3_ext_remove_index(handle, inode,
1512 +                                                               path + i);
1513 +                       }
1514 +                       /* root level have p_bh == NULL, brelse() eats this */
1515 +                       brelse(path[i].p_bh);
1516 +                       i--;
1517 +                       ext_debug(inode, "return to level %d\n", i);
1518 +               }
1519 +       }
1520 +
1521 +       /* TODO: flexible tree reduction should be here */
1522 +       if (path->p_hdr->e_num == 0) {
1523 +               /*
1524 +                * truncate to zero freed all the tree
1525 +                * so, we need to correct i_depth
1526 +                */
1527 +               EXT3_I(inode)->i_depth = 0;
1528 +               path->p_hdr->e_max = 0;
1529 +               ext3_mark_inode_dirty(handle, inode);
1530 +       }
1531 +
1532 +       kfree(path);
1533 +
1534 +       /* In a multi-transaction truncate, we only make the final
1535 +        * transaction synchronous */
1536 +       if (IS_SYNC(inode))
1537 +               handle->h_sync = 1;
1538 +
1539 +out_stop:
1540 +       /*
1541 +        * If this was a simple ftruncate(), and the file will remain alive
1542 +        * then we need to clear up the orphan record which we created above.
1543 +        * However, if this was a real unlink then we were called by
1544 +        * ext3_delete_inode(), and we allow that function to clean up the
1545 +        * orphan info for us.
1546 +        */
1547 +       if (inode->i_nlink)
1548 +               ext3_orphan_del(handle, inode);
1549 +
1550 +       up(&EXT3_I(inode)->i_ext_sem);
1551 +       ext3_journal_stop(handle, inode);
1552 +}
1553 +
1554 +/*
1555 + * this routine calculate max number of blocks we could modify
1556 + * in order to allocate new block for an inode
1557 + */
1558 +int ext3_ext_writepage_trans_blocks(struct inode *inode, int num)
1559 +{
1560 +       struct ext3_inode_info *ei = EXT3_I(inode);
1561 +       int depth = ei->i_depth + 1;
1562 +       int needed;
1563 +       
1564 +       /*
1565 +        * the worste case we're expecting is creation of the
1566 +        * new root (growing in depth) with index splitting
1567 +        * for splitting we have to consider depth + 1 because
1568 +        * previous growing could increase it
1569 +        */
1570 +
1571 +       /* 
1572 +        * growing in depth:
1573 +        * block allocation + new root + old root
1574 +        */
1575 +       needed = EXT3_ALLOC_NEEDED + 2;
1576 +
1577 +       /* index split. we may need:
1578 +        *   allocate intermediate indexes and new leaf
1579 +        *   change two blocks at each level, but root
1580 +        *   modify root block (inode)
1581 +        */
1582 +       needed += (depth * EXT3_ALLOC_NEEDED) + (2 * depth) + 1;
1583 +
1584 +       /* caller want to allocate num blocks */
1585 +       needed *= num;
1586 +       
1587 +#ifdef CONFIG_QUOTA
1588 +       /* 
1589 +        * FIXME: real calculation should be here
1590 +        * it depends on blockmap format of qouta file
1591 +        */
1592 +       needed += 2 * EXT3_SINGLEDATA_TRANS_BLOCKS;
1593 +#endif
1594 +
1595 +       return needed;
1596 +}
1597 +
1598 +/*
1599 + * called at mount time
1600 + */
1601 +void ext3_ext_init(struct super_block *sb)
1602 +{
1603 +       /*
1604 +        * possible initialization would be here
1605 +        */
1606 +
1607 +       if (test_opt(sb, EXTENTS))
1608 +               printk("EXT3-fs: file extents enabled\n");
1609 +       spin_lock_init(&EXT3_SB(sb)->s_ext_lock);
1610 +}
1611 +
1612 +/*
1613 + * called at umount time
1614 + */
1615 +void ext3_ext_release(struct super_block *sb)
1616 +{
1617 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1618 +
1619 +       /* show collected stats */
1620 +       if (sbi->s_ext_count && sbi->s_ext_extents)
1621 +               printk("EXT3-fs: min depth - %d, max depth - %d, "
1622 +                               "ave. depth - %d, ave. blocks/extent - %d\n",
1623 +                               sbi->s_ext_mindepth,
1624 +                               sbi->s_ext_maxdepth,
1625 +                               sbi->s_ext_sum / sbi->s_ext_count,
1626 +                               sbi->s_ext_blocks / sbi->s_ext_extents);
1627 +}
1628 +
1629 --- linux-2.4.18-chaos/fs/ext3/ialloc.c~ext3-extents-2.4.18-chaos       2003-09-19 22:07:14.000000000 +0400
1630 +++ linux-2.4.18-chaos-alexey/fs/ext3/ialloc.c  2003-09-20 00:18:43.000000000 +0400
1631 @@ -573,6 +573,10 @@ repeat:
1632         ei->i_prealloc_count = 0;
1633  #endif
1634         ei->i_block_group = i;
1635 +       if (test_opt(sb, EXTENTS))
1636 +               EXT3_I(inode)->i_flags |= EXT3_EXTENTS_FL;
1637 +       ei->i_depth = 0;
1638 +       sema_init(&ei->i_ext_sem, 1);
1639  
1640         if (ei->i_flags & EXT3_SYNC_FL)
1641                 inode->i_flags |= S_SYNC;
1642 --- linux-2.4.18-chaos/fs/ext3/inode.c~ext3-extents-2.4.18-chaos        2003-09-19 22:07:14.000000000 +0400
1643 +++ linux-2.4.18-chaos-alexey/fs/ext3/inode.c   2003-09-22 15:40:30.000000000 +0400
1644 @@ -842,6 +842,15 @@ changed:
1645         goto reread;
1646  }
1647  
1648 +static inline int
1649 +ext3_get_block_wrap(handle_t *handle, struct inode *inode, long block,
1650 +               struct buffer_head *bh, int create, int extend_disksize)
1651 +{
1652 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
1653 +               return ext3_ext_get_block(handle, inode, block, bh, create, 1);
1654 +       return ext3_get_block_handle(handle, inode, block, bh, create, 1);
1655 +}
1656 +
1657  /*
1658   * The BKL is not held on entry here.
1659   */
1660 @@ -855,7 +864,7 @@ static int ext3_get_block(struct inode *
1661                 handle = ext3_journal_current_handle();
1662                 J_ASSERT(handle != 0);
1663         }
1664 -       ret = ext3_get_block_handle(handle, inode, iblock,
1665 +       ret = ext3_get_block_wrap(handle, inode, iblock,
1666                                 bh_result, create, 1);
1667         return ret;
1668  }
1669 @@ -882,7 +891,7 @@ ext3_direct_io_get_block(struct inode *i
1670                 }
1671         }
1672         if (ret == 0)
1673 -               ret = ext3_get_block_handle(handle, inode, iblock,
1674 +               ret = ext3_get_block_wrap(handle, inode, iblock,
1675                                         bh_result, create, 0);
1676         if (ret == 0)
1677                 bh_result->b_size = (1 << inode->i_blkbits);
1678 @@ -904,7 +913,7 @@ struct buffer_head *ext3_getblk(handle_t
1679         dummy.b_state = 0;
1680         dummy.b_blocknr = -1000;
1681         buffer_trace_init(&dummy.b_history);
1682 -       *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
1683 +       *errp = ext3_get_block_wrap(handle, inode, block, &dummy, create, 1);
1684         if (!*errp && buffer_mapped(&dummy)) {
1685                 struct buffer_head *bh;
1686                 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1687 @@ -1520,7 +1529,7 @@ ext3_block_truncate_page_prepare(struct 
1688   * This required during truncate. We need to physically zero the tail end
1689   * of that block so it doesn't yield old data if the file is later grown.
1690   */
1691 -static int ext3_block_truncate_page(handle_t *handle,
1692 +int ext3_block_truncate_page(handle_t *handle,
1693                                     struct address_space *mapping, loff_t from,
1694                                     struct page *page, unsigned blocksize)
1695  {
1696 @@ -1998,6 +2007,9 @@ void ext3_truncate(struct inode * inode)
1697  
1698         ext3_discard_prealloc(inode);
1699  
1700 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
1701 +               return ext3_ext_truncate(inode);
1702 +
1703         blocksize = inode->i_sb->s_blocksize;
1704         last_block = (inode->i_size + blocksize-1)
1705                                         >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
1706 @@ -2436,6 +2448,8 @@ void ext3_read_inode(struct inode * inod
1707         ei->i_prealloc_count = 0;
1708  #endif
1709         ei->i_block_group = iloc.block_group;
1710 +       ei->i_depth = raw_inode->osd2.linux2.l_i_depth;
1711 +       sema_init(&ei->i_ext_sem, 1);
1712  
1713         /*
1714          * NOTE! The in-memory inode i_data array is in little-endian order
1715 @@ -2556,6 +2570,7 @@ static int ext3_do_update_inode(handle_t
1716                 raw_inode->i_fsize = 0;
1717         }
1718  #endif
1719 +       raw_inode->osd2.linux2.l_i_depth = ei->i_depth;
1720         raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
1721         if (!S_ISREG(inode->i_mode)) {
1722                 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
1723 @@ -2759,6 +2774,9 @@ int ext3_writepage_trans_blocks(struct i
1724         int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
1725         int ret;
1726         
1727 +       if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL)
1728 +               return ext3_ext_writepage_trans_blocks(inode, bpp);
1729 +
1730         if (ext3_should_journal_data(inode))
1731                 ret = 3 * (bpp + indirects) + 2;
1732         else
1733 @@ -3082,7 +3100,7 @@ int ext3_prep_san_write(struct inode *in
1734  
1735         /* alloc blocks one by one */
1736         for (i = 0; i < nblocks; i++) {
1737 -               ret = ext3_get_block_handle(handle, inode, blocks[i],
1738 +               ret = ext3_get_block_wrap(handle, inode, blocks[i],
1739                                                 &bh_tmp, 1, 1);
1740                 if (ret)
1741                         break;
1742 @@ -3143,7 +3161,7 @@ int ext3_map_inode_page(struct inode *in
1743                  if (blocks[i] != 0)
1744                          continue;
1745  
1746 -                rc = ext3_get_block_handle(handle, inode, iblock, &bh, 1, 1);
1747 +                rc = ext3_get_block_wrap(handle, inode, iblock, &bh, 1, 1);
1748                  if (rc) {
1749                          printk(KERN_INFO "ext3_map_inode_page: error %d "
1750                                 "allocating block %ld\n", rc, iblock);
1751 --- linux-2.4.18-chaos/fs/ext3/Makefile~ext3-extents-2.4.18-chaos       2003-09-19 22:07:14.000000000 +0400
1752 +++ linux-2.4.18-chaos-alexey/fs/ext3/Makefile  2003-09-20 00:18:43.000000000 +0400
1753 @@ -12,7 +12,8 @@ O_TARGET := ext3.o
1754  export-objs := ext3-exports.o
1755  
1756  obj-y    := balloc.o iopen.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \
1757 -               ioctl.o namei.o super.o symlink.o xattr.o ext3-exports.o
1758 +               ioctl.o namei.o super.o symlink.o xattr.o ext3-exports.o \
1759 +               extents.o
1760  obj-m    := $(O_TARGET)
1761  
1762  include $(TOPDIR)/Rules.make
1763 --- linux-2.4.18-chaos/fs/ext3/super.c~ext3-extents-2.4.18-chaos        2003-09-19 22:07:15.000000000 +0400
1764 +++ linux-2.4.18-chaos-alexey/fs/ext3/super.c   2003-09-20 00:18:43.000000000 +0400
1765 @@ -619,6 +619,7 @@ void ext3_put_super (struct super_block 
1766         kdev_t j_dev = sbi->s_journal->j_dev;
1767         int i;
1768  
1769 +       ext3_ext_release(sb);
1770         ext3_stop_delete_thread(sbi);
1771         ext3_xattr_put_super(sb);
1772         journal_destroy(sbi->s_journal);
1773 @@ -741,6 +742,12 @@ static int parse_options (char * options
1774                 else
1775  #endif
1776  
1777 +               if (!strcmp (this_char, "extents"))
1778 +                       set_opt (sbi->s_mount_opt, EXTENTS);
1779 +               else
1780 +               if (!strcmp (this_char, "extdebug"))
1781 +                       set_opt (sbi->s_mount_opt, EXTDEBUG);
1782 +               else
1783                 if (!strcmp (this_char, "bsddf"))
1784                         clear_opt (*mount_options, MINIX_DF);
1785                 else if (!strcmp (this_char, "nouid32")) {
1786 @@ -1468,6 +1475,7 @@ struct super_block * ext3_read_super (st
1787                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
1788                 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
1789                 "writeback");
1790 +       ext3_ext_init(sb);
1791  
1792         return sb;
1793  
1794 --- linux-2.4.18-chaos/include/linux/ext3_fs.h~ext3-extents-2.4.18-chaos        2003-09-19 22:07:14.000000000 +0400
1795 +++ linux-2.4.18-chaos-alexey/include/linux/ext3_fs.h   2003-09-20 00:18:43.000000000 +0400
1796 @@ -183,6 +183,7 @@ struct ext3_group_desc
1797  #define EXT3_IMAGIC_FL                 0x00002000 /* AFS directory */
1798  #define EXT3_JOURNAL_DATA_FL           0x00004000 /* file data should be journaled */
1799  #define EXT3_RESERVED_FL               0x80000000 /* reserved for ext3 lib */
1800 +#define EXT3_EXTENTS_FL                        0x00080000 /* Inode uses extents */
1801  
1802  #define EXT3_FL_USER_VISIBLE           0x00005FFF /* User visible flags */
1803  #define EXT3_FL_USER_MODIFIABLE                0x000000FF /* User modifiable flags */
1804 @@ -243,7 +244,7 @@ struct ext3_inode {
1805                 struct {
1806                         __u8    l_i_frag;       /* Fragment number */
1807                         __u8    l_i_fsize;      /* Fragment size */
1808 -                       __u16   i_pad1;
1809 +                       __u16   l_i_depth;
1810                         __u16   l_i_uid_high;   /* these 2 fields    */
1811                         __u16   l_i_gid_high;   /* were reserved2[0] */
1812                         __u32   l_i_reserved2;
1813 @@ -324,6 +325,8 @@ struct ext3_inode {
1814  #define EXT3_MOUNT_IOPEN               0x8000  /* Allow access via iopen */
1815  #define EXT3_MOUNT_IOPEN_NOPRIV                0x10000 /* Make iopen world-readable */
1816  #define EXT3_MOUNT_ASYNCDEL            0x20000 /* Delayed deletion */
1817 +#define EXT3_MOUNT_EXTENTS             0x40000 /* Extents support */
1818 +#define EXT3_MOUNT_EXTDEBUG            0x80000 /* Extents debug */
1819  
1820  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
1821  #ifndef _LINUX_EXT2_FS_H
1822 @@ -663,6 +666,12 @@ extern void ext3_discard_prealloc (struc
1823  extern void ext3_dirty_inode(struct inode *);
1824  extern int ext3_change_inode_journal_flag(struct inode *, int);
1825  extern void ext3_truncate (struct inode *);
1826 +extern int ext3_block_truncate_page(handle_t *handle,
1827 +                                   struct address_space *mapping, loff_t from,
1828 +                                   struct page *page, unsigned blocksize);
1829 +extern int ext3_forget(handle_t *handle, int is_metadata,
1830 +                      struct inode *inode, struct buffer_head *bh,
1831 +                      int blocknr);
1832  #ifdef EXT3_DELETE_THREAD
1833  extern void ext3_truncate_thread(struct inode *inode);
1834  #endif
1835 @@ -722,6 +731,13 @@ extern struct inode_operations ext3_dir_
1836  /* symlink.c */
1837  extern struct inode_operations ext3_fast_symlink_inode_operations;
1838  
1839 +/* extents.c */
1840 +extern int ext3_ext_writepage_trans_blocks(struct inode *, int);
1841 +extern int ext3_ext_get_block(handle_t *, struct inode *, long,
1842 +                               struct buffer_head *, int, int);
1843 +extern void ext3_ext_truncate(struct inode *);
1844 +extern void ext3_ext_init(struct super_block *);
1845 +extern void ext3_ext_release(struct super_block *);
1846  
1847  #endif /* __KERNEL__ */
1848  
1849 --- linux-2.4.18-chaos/include/linux/ext3_fs_i.h~ext3-extents-2.4.18-chaos      2001-11-22 22:46:19.000000000 +0300
1850 +++ linux-2.4.18-chaos-alexey/include/linux/ext3_fs_i.h 2003-09-20 00:18:43.000000000 +0400
1851 @@ -73,6 +73,10 @@ struct ext3_inode_info {
1852          * by other means, so we have truncate_sem.
1853          */
1854         struct rw_semaphore truncate_sem;
1855 +
1856 +       /* extents-related data */
1857 +       struct semaphore i_ext_sem;
1858 +       __u16 i_depth;
1859  };
1860  
1861  #endif /* _LINUX_EXT3_FS_I */
1862 --- linux-2.4.18-chaos/include/linux/ext3_fs_sb.h~ext3-extents-2.4.18-chaos     2003-09-19 22:07:13.000000000 +0400
1863 +++ linux-2.4.18-chaos-alexey/include/linux/ext3_fs_sb.h        2003-09-20 00:18:43.000000000 +0400
1864 @@ -84,6 +84,16 @@ struct ext3_sb_info {
1865         wait_queue_head_t s_delete_thread_queue;
1866         wait_queue_head_t s_delete_waiter_queue;
1867  #endif
1868 +
1869 +       /* extents */
1870 +       int s_ext_debug;
1871 +       int s_ext_mindepth;
1872 +       int s_ext_maxdepth;
1873 +       int s_ext_sum;
1874 +       int s_ext_count;
1875 +       spinlock_t s_ext_lock;
1876 +       int s_ext_extents;
1877 +       int s_ext_blocks;
1878  };
1879  
1880  #endif /* _LINUX_EXT3_FS_SB */
1881
1882 _