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