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