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