Whamcloud - gitweb
land b1_5 onto HEAD
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-mballoc2-2.6.18-vanilla.patch
1 Index: linux-stage/fs/ext3/mballoc.c
2 ===================================================================
3 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
4 +++ linux-stage/fs/ext3/mballoc.c       2006-07-16 02:29:49.000000000 +0800
5 @@ -0,0 +1,2434 @@
6 +/*
7 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
8 + * Written by Alex Tomas <alex@clusterfs.com>
9 + *
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License version 2 as
12 + * published by the Free Software Foundation.
13 + *
14 + * This program is distributed in the hope that it will be useful,
15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 + * GNU General Public License for more details.
18 + *
19 + * You should have received a copy of the GNU General Public Licens
20 + * along with this program; if not, write to the Free Software
21 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
22 + */
23 +
24 +
25 +/*
26 + * mballoc.c contains the multiblocks allocation routines
27 + */
28 +
29 +#include <linux/config.h>
30 +#include <linux/time.h>
31 +#include <linux/fs.h>
32 +#include <linux/namei.h>
33 +#include <linux/jbd.h>
34 +#include <linux/ext3_fs.h>
35 +#include <linux/ext3_jbd.h>
36 +#include <linux/quotaops.h>
37 +#include <linux/buffer_head.h>
38 +#include <linux/module.h>
39 +#include <linux/swap.h>
40 +#include <linux/proc_fs.h>
41 +#include <linux/pagemap.h>
42 +#include <linux/seq_file.h>
43 +
44 +/*
45 + * TODO:
46 + *   - bitmap read-ahead (proposed by Oleg Drokin aka green)
47 + *   - track min/max extents in each group for better group selection
48 + *   - mb_mark_used() may allocate chunk right after splitting buddy
49 + *   - special flag to advice allocator to look for requested + N blocks
50 + *     this may improve interaction between extents and mballoc
51 + *   - tree of groups sorted by number of free blocks
52 + *   - percpu reservation code (hotpath)
53 + *   - error handling
54 + */
55 +
56 +/*
57 + * with AGRESSIVE_CHECK allocator runs consistency checks over
58 + * structures. these checks slow things down a lot
59 + */
60 +#define AGGRESSIVE_CHECK__
61 +
62 +/*
63 + */
64 +#define MB_DEBUG__
65 +#ifdef MB_DEBUG
66 +#define mb_debug(fmt,a...)     printk(fmt, ##a)
67 +#else
68 +#define mb_debug(fmt,a...)
69 +#endif
70 +
71 +/*
72 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
73 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
74 + */
75 +#define EXT3_MB_HISTORY
76 +
77 +/*
78 + * How long mballoc can look for a best extent (in found extents)
79 + */
80 +long ext3_mb_max_to_scan = 500;
81 +
82 +/*
83 + * How long mballoc must look for a best extent
84 + */
85 +long ext3_mb_min_to_scan = 30;
86 +
87 +/*
88 + * with 'ext3_mb_stats' allocator will collect stats that will be
89 + * shown at umount. The collecting costs though!
90 + */
91 +
92 +long ext3_mb_stats = 1;
93 +
94 +#ifdef EXT3_BB_MAX_BLOCKS
95 +#undef EXT3_BB_MAX_BLOCKS
96 +#endif
97 +#define EXT3_BB_MAX_BLOCKS     30
98 +
99 +struct ext3_free_metadata {
100 +       unsigned short group;
101 +       unsigned short num;
102 +       unsigned short blocks[EXT3_BB_MAX_BLOCKS];
103 +       struct list_head list;
104 +};
105 +
106 +struct ext3_group_info {
107 +       unsigned long   bb_state;
108 +       unsigned long   bb_tid;
109 +       struct ext3_free_metadata *bb_md_cur;
110 +       unsigned short  bb_first_free;
111 +       unsigned short  bb_free;
112 +       unsigned short  bb_fragments;
113 +       unsigned short  bb_counters[];
114 +};
115 +
116 +
117 +#define EXT3_GROUP_INFO_NEED_INIT_BIT  0
118 +#define EXT3_GROUP_INFO_LOCKED_BIT     1
119 +
120 +#define EXT3_MB_GRP_NEED_INIT(grp)     \
121 +       (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
122 +
123 +struct ext3_free_extent {
124 +       __u16 fe_start;
125 +       __u16 fe_len;
126 +       __u16 fe_group;
127 +};
128 +
129 +struct ext3_allocation_context {
130 +       struct super_block *ac_sb;
131 +
132 +       /* search goals */
133 +       struct ext3_free_extent ac_g_ex;
134 +       
135 +       /* the best found extent */
136 +       struct ext3_free_extent ac_b_ex;
137 +       
138 +       /* number of iterations done. we have to track to limit searching */
139 +       unsigned long ac_ex_scanned;
140 +       __u16 ac_groups_scanned;
141 +       __u16 ac_found;
142 +       __u16 ac_tail;
143 +       __u16 ac_buddy;
144 +       __u8 ac_status; 
145 +       __u8 ac_flags;          /* allocation hints */
146 +       __u8 ac_criteria;
147 +       __u8 ac_repeats;
148 +       __u8 ac_2order;         /* if request is to allocate 2^N blocks and
149 +                                * N > 0, the field stores N, otherwise 0 */
150 +};
151 +
152 +#define AC_STATUS_CONTINUE     1
153 +#define AC_STATUS_FOUND                2
154 +#define AC_STATUS_BREAK                3
155 +
156 +struct ext3_mb_history {
157 +       struct ext3_free_extent goal;   /* goal allocation */
158 +       struct ext3_free_extent result; /* result allocation */
159 +       __u16 found;    /* how many extents have been found */
160 +       __u16 groups;   /* how many groups have been scanned */
161 +       __u16 tail;     /* what tail broke some buddy */
162 +       __u16 buddy;    /* buddy the tail ^^^ broke */
163 +       __u8 cr;        /* which phase the result extent was found at */
164 +       __u8 merged;
165 +};
166 +
167 +struct ext3_buddy {
168 +       struct page *bd_buddy_page;
169 +       void *bd_buddy;
170 +       struct page *bd_bitmap_page;
171 +       void *bd_bitmap;
172 +       struct ext3_group_info *bd_info;
173 +       struct super_block *bd_sb;
174 +       __u16 bd_blkbits;
175 +       __u16 bd_group;
176 +};
177 +#define EXT3_MB_BITMAP(e3b)    ((e3b)->bd_bitmap)
178 +#define EXT3_MB_BUDDY(e3b)     ((e3b)->bd_buddy)
179 +
180 +#ifndef EXT3_MB_HISTORY
181 +#define ext3_mb_store_history(sb,ac)
182 +#else
183 +static void ext3_mb_store_history(struct super_block *,
184 +                               struct ext3_allocation_context *ac);
185 +#endif
186 +
187 +#define in_range(b, first, len)        ((b) >= (first) && (b) <= (first) + (len) - 1)
188 +
189 +static struct proc_dir_entry *proc_root_ext3;
190 +
191 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
192 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
193 +int ext3_new_block_old(handle_t *, struct inode *, unsigned long, int *);
194 +int ext3_mb_reserve_blocks(struct super_block *, int);
195 +void ext3_mb_release_blocks(struct super_block *, int);
196 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
197 +void ext3_mb_free_committed_blocks(struct super_block *);
198 +
199 +#if BITS_PER_LONG == 64
200 +#define mb_correct_addr_and_bit(bit,addr)              \
201 +{                                                      \
202 +       bit += ((unsigned long) addr & 7UL) << 3;       \
203 +       addr = (void *) ((unsigned long) addr & ~7UL);  \
204 +}
205 +#elif BITS_PER_LONG == 32
206 +#define mb_correct_addr_and_bit(bit,addr)              \
207 +{                                                      \
208 +       bit += ((unsigned long) addr & 3UL) << 3;       \
209 +       addr = (void *) ((unsigned long) addr & ~3UL);  \
210 +}
211 +#else
212 +#error "how many bits you are?!"
213 +#endif
214 +
215 +static inline int mb_test_bit(int bit, void *addr)
216 +{
217 +       mb_correct_addr_and_bit(bit,addr);
218 +       return ext2_test_bit(bit, addr);
219 +}
220 +
221 +static inline void mb_set_bit(int bit, void *addr)
222 +{
223 +       mb_correct_addr_and_bit(bit,addr);
224 +       ext2_set_bit(bit, addr);
225 +}
226 +
227 +static inline void mb_set_bit_atomic(int bit, void *addr)
228 +{
229 +       mb_correct_addr_and_bit(bit,addr);
230 +       ext2_set_bit_atomic(NULL, bit, addr);
231 +}
232 +
233 +static inline void mb_clear_bit(int bit, void *addr)
234 +{
235 +       mb_correct_addr_and_bit(bit,addr);
236 +       ext2_clear_bit(bit, addr);
237 +}
238 +
239 +static inline void mb_clear_bit_atomic(int bit, void *addr)
240 +{
241 +       mb_correct_addr_and_bit(bit,addr);
242 +       ext2_clear_bit_atomic(NULL, bit, addr);
243 +}
244 +
245 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
246 +{
247 +       int fix;
248 +#if BITS_PER_LONG == 64
249 +       fix = ((unsigned long) addr & 7UL) << 3;
250 +       addr = (void *) ((unsigned long) addr & ~7UL);
251 +#elif BITS_PER_LONG == 32
252 +       fix = ((unsigned long) addr & 3UL) << 3;
253 +       addr = (void *) ((unsigned long) addr & ~3UL);
254 +#else
255 +#error "how many bits you are?!"
256 +#endif
257 +       max += fix;
258 +       start += fix;
259 +       return ext2_find_next_zero_bit(addr, max, start) - fix;
260 +}
261 +
262 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
263 +{
264 +       char *bb;
265 +
266 +       J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
267 +       J_ASSERT(max != NULL);
268 +
269 +       if (order > e3b->bd_blkbits + 1) {
270 +               *max = 0;
271 +               return NULL;
272 +       }
273 +
274 +       /* at order 0 we see each particular block */
275 +       *max = 1 << (e3b->bd_blkbits + 3);
276 +       if (order == 0)
277 +               return EXT3_MB_BITMAP(e3b);
278 +
279 +       bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
280 +       *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
281 +
282 +       return bb;
283 +}
284 +
285 +#ifdef AGGRESSIVE_CHECK
286 +
287 +static void mb_check_buddy(struct ext3_buddy *e3b)
288 +{
289 +       int order = e3b->bd_blkbits + 1;
290 +       int max, max2, i, j, k, count;
291 +       int fragments = 0, fstart;
292 +       void *buddy, *buddy2;
293 +
294 +       if (!test_opt(e3b->bd_sb, MBALLOC))
295 +               return;
296 +
297 +       {
298 +               static int mb_check_counter = 0;
299 +               if (mb_check_counter++ % 300 != 0)
300 +                       return;
301 +       }
302 +       
303 +       while (order > 1) {
304 +               buddy = mb_find_buddy(e3b, order, &max);
305 +               J_ASSERT(buddy);
306 +               buddy2 = mb_find_buddy(e3b, order - 1, &max2);
307 +               J_ASSERT(buddy2);
308 +               J_ASSERT(buddy != buddy2);
309 +               J_ASSERT(max * 2 == max2);
310 +
311 +               count = 0;
312 +               for (i = 0; i < max; i++) {
313 +
314 +                       if (mb_test_bit(i, buddy)) {
315 +                               /* only single bit in buddy2 may be 1 */
316 +                               if (!mb_test_bit(i << 1, buddy2))
317 +                                       J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
318 +                               else if (!mb_test_bit((i << 1) + 1, buddy2))
319 +                                       J_ASSERT(mb_test_bit(i << 1, buddy2));
320 +                               continue;
321 +                       }
322 +
323 +                       /* both bits in buddy2 must be 0 */
324 +                       J_ASSERT(mb_test_bit(i << 1, buddy2));
325 +                       J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
326 +
327 +                       for (j = 0; j < (1 << order); j++) {
328 +                               k = (i * (1 << order)) + j;
329 +                               J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
330 +                       }
331 +                       count++;
332 +               }
333 +               J_ASSERT(e3b->bd_info->bb_counters[order] == count);
334 +               order--;
335 +       }
336 +
337 +       fstart = -1;
338 +       buddy = mb_find_buddy(e3b, 0, &max);
339 +       for (i = 0; i < max; i++) {
340 +               if (!mb_test_bit(i, buddy)) {
341 +                       J_ASSERT(i >= e3b->bd_info->bb_first_free);
342 +                       if (fstart == -1) {
343 +                               fragments++;
344 +                               fstart = i;
345 +                       }
346 +                       continue;
347 +               }
348 +               fstart = -1;
349 +               /* check used bits only */
350 +               for (j = 0; j < e3b->bd_blkbits + 1; j++) {
351 +                       buddy2 = mb_find_buddy(e3b, j, &max2);
352 +                       k = i >> j;
353 +                       J_ASSERT(k < max2);
354 +                       J_ASSERT(mb_test_bit(k, buddy2));
355 +               }
356 +       }
357 +       J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
358 +       J_ASSERT(e3b->bd_info->bb_fragments == fragments);
359 +}
360 +
361 +#else
362 +#define mb_check_buddy(e3b)
363 +#endif
364 +
365 +/* find most significant bit */
366 +static int inline fmsb(unsigned short word)
367 +{
368 +       int order;
369 +
370 +       if (word > 255) {
371 +               order = 7;
372 +               word >>= 8;
373 +       } else {
374 +               order = -1;
375 +       }
376 +
377 +       do {
378 +               order++;
379 +               word >>= 1;
380 +       } while (word != 0);
381 +
382 +       return order;
383 +}
384 +
385 +static void inline
386 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
387 +                               int len, struct ext3_group_info *grp)
388 +{
389 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
390 +       unsigned short min, max, chunk, border;
391 +
392 +       mb_debug("mark %u/%u free\n", first, len);
393 +       J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
394 +
395 +       border = 2 << sb->s_blocksize_bits;
396 +
397 +       while (len > 0) {
398 +               /* find how many blocks can be covered since this position */
399 +               max = ffs(first | border) - 1;
400 +
401 +               /* find how many blocks of power 2 we need to mark */
402 +               min = fmsb(len);
403 +
404 +               mb_debug("  %u/%u -> max %u, min %u\n",
405 +                       first & ((2 << sb->s_blocksize_bits) - 1),
406 +                       len, max, min);
407 +
408 +               if (max < min)
409 +                       min = max;
410 +               chunk = 1 << min;
411 +
412 +               /* mark multiblock chunks only */
413 +               grp->bb_counters[min]++;
414 +               if (min > 0) {
415 +                       mb_debug("    set %u at %u \n", first >> min,
416 +                               sbi->s_mb_offsets[min]);
417 +                       mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
418 +               }
419 +
420 +               len -= chunk;
421 +               first += chunk;
422 +       }
423 +}
424 +
425 +static void
426 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
427 +                       struct ext3_group_info *grp)
428 +{
429 +       unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
430 +       unsigned short i = 0, first, len;
431 +       unsigned free = 0, fragments = 0;
432 +       unsigned long long period = get_cycles();
433 +
434 +       i = mb_find_next_zero_bit(bitmap, max, 0);
435 +       grp->bb_first_free = i;
436 +       while (i < max) {
437 +               fragments++;
438 +               first = i;
439 +               i = find_next_bit(bitmap, max, i);
440 +               len = i - first;
441 +               free += len;
442 +               if (len > 1) 
443 +                       ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
444 +               else
445 +                       grp->bb_counters[0]++;
446 +               if (i < max)
447 +                       i = mb_find_next_zero_bit(bitmap, max, i);
448 +       }
449 +       grp->bb_fragments = fragments;
450 +
451 +       /* bb_state shouldn't being modified because all
452 +        * others waits for init completion on page lock */
453 +       clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
454 +       if (free != grp->bb_free) {
455 +               printk("EXT3-fs: %u blocks in bitmap, %u in group descriptor\n",
456 +                       free, grp->bb_free);
457 +               grp->bb_free = free;
458 +       }
459 +
460 +       period = get_cycles() - period;
461 +       spin_lock(&EXT3_SB(sb)->s_bal_lock);
462 +       EXT3_SB(sb)->s_mb_buddies_generated++;
463 +       EXT3_SB(sb)->s_mb_generation_time += period;
464 +       spin_unlock(&EXT3_SB(sb)->s_bal_lock);
465 +}
466 +
467 +static int ext3_mb_init_cache(struct page *page)
468 +{
469 +       int blocksize, blocks_per_page, groups_per_page;
470 +       int err = 0, i, first_group, first_block;
471 +       struct super_block *sb;
472 +       struct buffer_head *bhs;
473 +       struct buffer_head **bh;
474 +       struct inode *inode;
475 +       char *data, *bitmap;
476 +
477 +       mb_debug("init page %lu\n", page->index);
478 +
479 +       inode = page->mapping->host;
480 +       sb = inode->i_sb;
481 +       blocksize = 1 << inode->i_blkbits;
482 +       blocks_per_page = PAGE_CACHE_SIZE / blocksize;
483 +       
484 +       groups_per_page = blocks_per_page >> 1;
485 +       if (groups_per_page == 0)
486 +               groups_per_page = 1;
487 +
488 +       /* allocate buffer_heads to read bitmaps */
489 +       if (groups_per_page > 1) {
490 +               err = -ENOMEM;
491 +               i = sizeof(struct buffer_head *) * groups_per_page;
492 +               bh = kmalloc(i, GFP_NOFS);
493 +               if (bh == NULL)
494 +                       goto out;
495 +               memset(bh, 0, i);
496 +       } else
497 +               bh = &bhs;
498 +       
499 +       first_group = page->index * blocks_per_page / 2;
500 +       
501 +       /* read all groups the page covers into the cache */
502 +       for (i = 0; i < groups_per_page; i++) {
503 +               struct ext3_group_desc * desc;
504 +
505 +               if (first_group + i >= EXT3_SB(sb)->s_groups_count)
506 +                       break;
507 +
508 +               err = -EIO;
509 +               desc = ext3_get_group_desc(sb, first_group + i, NULL);
510 +               if (desc == NULL)
511 +                       goto out;
512 +
513 +               err = -ENOMEM;
514 +               bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
515 +               if (bh[i] == NULL)
516 +                       goto out;
517 +
518 +               if (buffer_uptodate(bh[i]))
519 +                       continue;
520 +
521 +               lock_buffer(bh[i]);
522 +               if (buffer_uptodate(bh[i])) {
523 +                       unlock_buffer(bh[i]);
524 +                       continue;
525 +               }
526 +
527 +               get_bh(bh[i]);
528 +               bh[i]->b_end_io = end_buffer_read_sync;
529 +               submit_bh(READ, bh[i]);
530 +               mb_debug("read bitmap for group %u\n", first_group + i);
531 +       }
532 +
533 +       /* wait for I/O completion */
534 +       for (i = 0; i < groups_per_page && bh[i]; i++)
535 +               wait_on_buffer(bh[i]);
536 +
537 +       /* XXX: I/O error handling here */
538 +
539 +       first_block = page->index * blocks_per_page;
540 +       for (i = 0; i < blocks_per_page; i++) {
541 +               int group;
542 +
543 +               group = (first_block + i) >> 1;
544 +               if (group >= EXT3_SB(sb)->s_groups_count)
545 +                       break;
546 +
547 +               data = page_address(page) + (i * blocksize);
548 +               bitmap = bh[group - first_group]->b_data;
549 +
550 +               if ((first_block + i) & 1) {
551 +                       /* this is block of buddy */
552 +                       mb_debug("put buddy for group %u in page %lu/%x\n",
553 +                               group, page->index, i * blocksize);
554 +                       memset(data, 0xff, blocksize);
555 +                       EXT3_SB(sb)->s_group_info[group]->bb_fragments = 0;
556 +                       memset(EXT3_SB(sb)->s_group_info[group]->bb_counters, 0,
557 +                               sizeof(unsigned short)*(sb->s_blocksize_bits+2));
558 +                       ext3_mb_generate_buddy(sb, data, bitmap,
559 +                                               EXT3_SB(sb)->s_group_info[group]);
560 +               } else {
561 +                       /* this is block of bitmap */
562 +                       mb_debug("put bitmap for group %u in page %lu/%x\n",
563 +                               group, page->index, i * blocksize);
564 +                       memcpy(data, bitmap, blocksize);
565 +               }
566 +       }
567 +       SetPageUptodate(page);
568 +
569 +out:
570 +       for (i = 0; i < groups_per_page && bh[i]; i++)
571 +               brelse(bh[i]);
572 +       if (bh && bh != &bhs)
573 +               kfree(bh);
574 +       return err;
575 +}
576 +
577 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
578 +               struct ext3_buddy *e3b)
579 +{
580 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
581 +       struct inode *inode = sbi->s_buddy_cache;
582 +       int blocks_per_page, block, pnum, poff;
583 +       struct page *page;
584 +
585 +       mb_debug("load group %u\n", group);
586 +
587 +       blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
588 +
589 +       e3b->bd_blkbits = sb->s_blocksize_bits;
590 +       e3b->bd_info = sbi->s_group_info[group];
591 +       e3b->bd_sb = sb;
592 +       e3b->bd_group = group;
593 +       e3b->bd_buddy_page = NULL;
594 +       e3b->bd_bitmap_page = NULL;
595 +
596 +       block = group * 2;
597 +       pnum = block / blocks_per_page;
598 +       poff = block % blocks_per_page;
599 +
600 +       page = find_get_page(inode->i_mapping, pnum);
601 +       if (page == NULL || !PageUptodate(page)) {
602 +               if (page)
603 +                       page_cache_release(page);
604 +               page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
605 +               if (page) {
606 +                       if (!PageUptodate(page))
607 +                               ext3_mb_init_cache(page);
608 +                       unlock_page(page);
609 +               }
610 +       }
611 +       if (page == NULL || !PageUptodate(page))
612 +               goto err;
613 +       e3b->bd_bitmap_page = page;
614 +       e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
615 +       mark_page_accessed(page);
616 +
617 +       block++;
618 +       pnum = block / blocks_per_page;
619 +       poff = block % blocks_per_page;
620 +
621 +       page = find_get_page(inode->i_mapping, pnum);
622 +       if (page == NULL || !PageUptodate(page)) {
623 +               if (page)
624 +                       page_cache_release(page);
625 +               page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
626 +               if (page) {
627 +                       if (!PageUptodate(page))
628 +                               ext3_mb_init_cache(page);
629 +                       unlock_page(page);
630 +               }
631 +       }
632 +       if (page == NULL || !PageUptodate(page))
633 +               goto err;
634 +       e3b->bd_buddy_page = page;
635 +       e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
636 +       mark_page_accessed(page);
637 +
638 +       J_ASSERT(e3b->bd_bitmap_page != NULL);
639 +       J_ASSERT(e3b->bd_buddy_page != NULL);
640 +
641 +       return 0;
642 +
643 +err:
644 +       if (e3b->bd_bitmap_page)
645 +               page_cache_release(e3b->bd_bitmap_page);
646 +       if (e3b->bd_buddy_page)
647 +               page_cache_release(e3b->bd_buddy_page);
648 +       e3b->bd_buddy = NULL;
649 +       e3b->bd_bitmap = NULL;
650 +       return -EIO;
651 +}
652 +
653 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
654 +{
655 +       if (e3b->bd_bitmap_page)
656 +               page_cache_release(e3b->bd_bitmap_page);
657 +       if (e3b->bd_buddy_page)
658 +               page_cache_release(e3b->bd_buddy_page);
659 +}
660 +
661 +
662 +static inline void
663 +ext3_lock_group(struct super_block *sb, int group)
664 +{
665 +       bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
666 +                       &EXT3_SB(sb)->s_group_info[group]->bb_state);
667 +}
668 +
669 +static inline void
670 +ext3_unlock_group(struct super_block *sb, int group)
671 +{
672 +       bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
673 +                       &EXT3_SB(sb)->s_group_info[group]->bb_state);
674 +}
675 +
676 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
677 +{
678 +       int order = 1;
679 +       void *bb;
680 +
681 +       J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
682 +       J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
683 +
684 +       bb = EXT3_MB_BUDDY(e3b);
685 +       while (order <= e3b->bd_blkbits + 1) {
686 +               block = block >> 1;
687 +               if (!mb_test_bit(block, bb)) {
688 +                       /* this block is part of buddy of order 'order' */
689 +                       return order;
690 +               }
691 +               bb += 1 << (e3b->bd_blkbits - order);
692 +               order++;
693 +       }
694 +       return 0;
695 +}
696 +
697 +static inline void mb_clear_bits(void *bm, int cur, int len)
698 +{
699 +       __u32 *addr;
700 +
701 +       len = cur + len;
702 +       while (cur < len) {
703 +               if ((cur & 31) == 0 && (len - cur) >= 32) {
704 +                       /* fast path: clear whole word at once */
705 +                       addr = bm + (cur >> 3);
706 +                       *addr = 0;
707 +                       cur += 32;
708 +                       continue;
709 +               }
710 +               mb_clear_bit_atomic(cur, bm);
711 +               cur++;
712 +       }
713 +}
714 +
715 +static inline void mb_set_bits(void *bm, int cur, int len)
716 +{
717 +       __u32 *addr;
718 +
719 +       len = cur + len;
720 +       while (cur < len) {
721 +               if ((cur & 31) == 0 && (len - cur) >= 32) {
722 +                       /* fast path: clear whole word at once */
723 +                       addr = bm + (cur >> 3);
724 +                       *addr = 0xffffffff;
725 +                       cur += 32;
726 +                       continue;
727 +               }
728 +               mb_set_bit_atomic(cur, bm);
729 +               cur++;
730 +       }
731 +}
732 +
733 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
734 +{
735 +       int block = 0, max = 0, order;
736 +       void *buddy, *buddy2;
737 +
738 +       mb_check_buddy(e3b);
739 +
740 +       e3b->bd_info->bb_free += count;
741 +       if (first < e3b->bd_info->bb_first_free)
742 +               e3b->bd_info->bb_first_free = first;
743 +
744 +       /* let's maintain fragments counter */
745 +       if (first != 0)
746 +               block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
747 +       if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
748 +               max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
749 +       if (block && max)
750 +               e3b->bd_info->bb_fragments--;
751 +       else if (!block && !max)
752 +               e3b->bd_info->bb_fragments++;
753 +
754 +       /* let's maintain buddy itself */
755 +       while (count-- > 0) {
756 +               block = first++;
757 +               order = 0;
758 +
759 +               J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
760 +               mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
761 +               e3b->bd_info->bb_counters[order]++;
762 +
763 +               /* start of the buddy */
764 +               buddy = mb_find_buddy(e3b, order, &max);
765 +
766 +               do {
767 +                       block &= ~1UL;
768 +                       if (mb_test_bit(block, buddy) ||
769 +                                       mb_test_bit(block + 1, buddy))
770 +                               break;
771 +
772 +                       /* both the buddies are free, try to coalesce them */
773 +                       buddy2 = mb_find_buddy(e3b, order + 1, &max);
774 +
775 +                       if (!buddy2)
776 +                               break;
777 +
778 +                       if (order > 0) {
779 +                               /* for special purposes, we don't set
780 +                                * free bits in bitmap */
781 +                               mb_set_bit(block, buddy);
782 +                               mb_set_bit(block + 1, buddy);
783 +                       }
784 +                       e3b->bd_info->bb_counters[order]--;
785 +                       e3b->bd_info->bb_counters[order]--;
786 +
787 +                       block = block >> 1;
788 +                       order++;
789 +                       e3b->bd_info->bb_counters[order]++;
790 +
791 +                       mb_clear_bit(block, buddy2);
792 +                       buddy = buddy2;
793 +               } while (1);
794 +       }
795 +       mb_check_buddy(e3b);
796 +
797 +       return 0;
798 +}
799 +
800 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
801 +                               int needed, struct ext3_free_extent *ex)
802 +{
803 +       int next, max, ord;
804 +       void *buddy;
805 +
806 +       J_ASSERT(ex != NULL);
807 +
808 +       buddy = mb_find_buddy(e3b, order, &max);
809 +       J_ASSERT(buddy);
810 +       J_ASSERT(block < max);
811 +       if (mb_test_bit(block, buddy)) {
812 +               ex->fe_len = 0;
813 +               ex->fe_start = 0;
814 +               ex->fe_group = 0;
815 +               return 0;
816 +       }
817 +
818 +       if (likely(order == 0)) {
819 +               /* find actual order */
820 +               order = mb_find_order_for_block(e3b, block);
821 +               block = block >> order;
822 +       }
823 +
824 +       ex->fe_len = 1 << order;
825 +       ex->fe_start = block << order;
826 +       ex->fe_group = e3b->bd_group;
827 +
828 +       while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
829 +
830 +               if (block + 1 >= max)
831 +                       break;
832 +
833 +               next = (block + 1) * (1 << order);
834 +               if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
835 +                       break;
836 +
837 +               ord = mb_find_order_for_block(e3b, next);
838 +
839 +               order = ord;
840 +               block = next >> order;
841 +               ex->fe_len += 1 << order;
842 +       }
843 +
844 +       J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
845 +       return ex->fe_len;
846 +}
847 +
848 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
849 +{
850 +       int ord, mlen = 0, max = 0, cur;
851 +       int start = ex->fe_start;
852 +       int len = ex->fe_len;
853 +       unsigned ret = 0;
854 +       int len0 = len;
855 +       void *buddy;
856 +
857 +       mb_check_buddy(e3b);
858 +
859 +       e3b->bd_info->bb_free -= len;
860 +       if (e3b->bd_info->bb_first_free == start)
861 +               e3b->bd_info->bb_first_free += len;
862 +
863 +       /* let's maintain fragments counter */
864 +       if (start != 0)
865 +               mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
866 +       if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
867 +               max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
868 +       if (mlen && max)
869 +               e3b->bd_info->bb_fragments++;
870 +       else if (!mlen && !max)
871 +               e3b->bd_info->bb_fragments--;
872 +
873 +       /* let's maintain buddy itself */
874 +       while (len) {
875 +               ord = mb_find_order_for_block(e3b, start);
876 +
877 +               if (((start >> ord) << ord) == start && len >= (1 << ord)) {
878 +                       /* the whole chunk may be allocated at once! */
879 +                       mlen = 1 << ord;
880 +                       buddy = mb_find_buddy(e3b, ord, &max);
881 +                       J_ASSERT((start >> ord) < max);
882 +                       mb_set_bit(start >> ord, buddy);
883 +                       e3b->bd_info->bb_counters[ord]--;
884 +                       start += mlen;
885 +                       len -= mlen;
886 +                       J_ASSERT(len >= 0);
887 +                       continue;
888 +               }
889 +
890 +               /* store for history */
891 +               if (ret == 0)
892 +                       ret = len | (ord << 16);
893 +
894 +               /* we have to split large buddy */
895 +               J_ASSERT(ord > 0);
896 +               buddy = mb_find_buddy(e3b, ord, &max);
897 +               mb_set_bit(start >> ord, buddy);
898 +               e3b->bd_info->bb_counters[ord]--;
899 +
900 +               ord--;
901 +               cur = (start >> ord) & ~1U;
902 +               buddy = mb_find_buddy(e3b, ord, &max);
903 +               mb_clear_bit(cur, buddy);
904 +               mb_clear_bit(cur + 1, buddy);
905 +               e3b->bd_info->bb_counters[ord]++;
906 +               e3b->bd_info->bb_counters[ord]++;
907 +       }
908 +
909 +       /* now drop all the bits in bitmap */
910 +       mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
911 +
912 +       mb_check_buddy(e3b);
913 +
914 +       return ret;
915 +}
916 +
917 +/*
918 + * Must be called under group lock!
919 + */
920 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
921 +                                       struct ext3_buddy *e3b)
922 +{
923 +       unsigned long ret;
924 +
925 +       ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
926 +       ret = mb_mark_used(e3b, &ac->ac_b_ex);
927 +
928 +       ac->ac_status = AC_STATUS_FOUND;
929 +       ac->ac_tail = ret & 0xffff;
930 +       ac->ac_buddy = ret >> 16;
931 +}
932 +
933 +/*
934 + * The routine checks whether found extent is good enough. If it is,
935 + * then the extent gets marked used and flag is set to the context
936 + * to stop scanning. Otherwise, the extent is compared with the
937 + * previous found extent and if new one is better, then it's stored
938 + * in the context. Later, the best found extent will be used, if
939 + * mballoc can't find good enough extent.
940 + *
941 + * FIXME: real allocation policy is to be designed yet!
942 + */
943 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
944 +                                       struct ext3_free_extent *ex,
945 +                                       struct ext3_buddy *e3b)
946 +{
947 +       struct ext3_free_extent *bex = &ac->ac_b_ex;
948 +       struct ext3_free_extent *gex = &ac->ac_g_ex;
949 +
950 +       J_ASSERT(ex->fe_len > 0);
951 +       J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
952 +       J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
953 +
954 +       ac->ac_found++;
955 +
956 +       /*
957 +        * The special case - take what you catch first
958 +        */
959 +       if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
960 +               *bex = *ex;
961 +               ext3_mb_use_best_found(ac, e3b);
962 +               return;
963 +       }
964 +
965 +       /*
966 +        * Let's check whether the chuck is good enough
967 +        */
968 +       if (ex->fe_len == gex->fe_len) {
969 +               *bex = *ex;
970 +               ext3_mb_use_best_found(ac, e3b);
971 +               return;
972 +       }
973 +
974 +       /*
975 +        * If this is first found extent, just store it in the context
976 +        */
977 +       if (bex->fe_len == 0) {
978 +               *bex = *ex;
979 +               return;
980 +       }
981 +
982 +       /*
983 +        * If new found extent is better, store it in the context
984 +        */
985 +       if (bex->fe_len < gex->fe_len) {
986 +               /* if the request isn't satisfied, any found extent
987 +                * larger than previous best one is better */
988 +               if (ex->fe_len > bex->fe_len)
989 +                       *bex = *ex;
990 +       } else if (ex->fe_len > gex->fe_len) {
991 +               /* if the request is satisfied, then we try to find
992 +                * an extent that still satisfy the request, but is
993 +                * smaller than previous one */
994 +               *bex = *ex;
995 +       }
996 +
997 +       /*
998 +        * Let's scan at least few extents and don't pick up a first one
999 +        */
1000 +       if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1001 +               ac->ac_status = AC_STATUS_BREAK;
1002 +
1003 +       /*
1004 +        * We don't want to scan for a whole year
1005 +        */
1006 +       if (ac->ac_found > ext3_mb_max_to_scan)
1007 +               ac->ac_status = AC_STATUS_BREAK;
1008 +}
1009 +
1010 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1011 +                                       struct ext3_buddy *e3b)
1012 +{
1013 +       struct ext3_free_extent ex = ac->ac_b_ex;
1014 +       int group = ex.fe_group, max, err;
1015 +
1016 +       J_ASSERT(ex.fe_len > 0);
1017 +       err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1018 +       if (err)
1019 +               return err;
1020 +
1021 +       ext3_lock_group(ac->ac_sb, group);
1022 +       max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1023 +       
1024 +       if (max > 0) {
1025 +               ac->ac_b_ex = ex;
1026 +               ext3_mb_use_best_found(ac, e3b);
1027 +       }
1028 +
1029 +       ext3_unlock_group(ac->ac_sb, group);
1030 +
1031 +       ext3_mb_release_desc(e3b);
1032 +
1033 +       return 0;
1034 +}
1035 +
1036 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1037 +                               struct ext3_buddy *e3b)
1038 +{
1039 +       int group = ac->ac_g_ex.fe_group, max, err;
1040 +       struct ext3_free_extent ex;
1041 +
1042 +       err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1043 +       if (err)
1044 +               return err;
1045 +
1046 +       ext3_lock_group(ac->ac_sb, group);
1047 +       max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1048 +                               ac->ac_g_ex.fe_len, &ex);
1049 +       
1050 +       if (max > 0) {
1051 +               J_ASSERT(ex.fe_len > 0);
1052 +               J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1053 +               J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1054 +               ac->ac_found++;
1055 +               ac->ac_b_ex = ex;
1056 +               ext3_mb_use_best_found(ac, e3b);
1057 +       }
1058 +       ext3_unlock_group(ac->ac_sb, group);
1059 +
1060 +       ext3_mb_release_desc(e3b);
1061 +
1062 +       return 0;
1063 +}
1064 +
1065 +/*
1066 + * The routine scans buddy structures (not bitmap!) from given order
1067 + * to max order and tries to find big enough chunk to satisfy the req
1068 + */
1069 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1070 +                                       struct ext3_buddy *e3b)
1071 +{
1072 +       struct super_block *sb = ac->ac_sb;
1073 +       struct ext3_group_info *grp = e3b->bd_info;
1074 +       void *buddy;
1075 +       int i, k, max;
1076 +
1077 +       J_ASSERT(ac->ac_2order > 0);
1078 +       for (i = ac->ac_2order; i < sb->s_blocksize_bits + 1; i++) {
1079 +               if (grp->bb_counters[i] == 0)
1080 +                       continue;
1081 +
1082 +               buddy = mb_find_buddy(e3b, i, &max);
1083 +               if (buddy == NULL) {
1084 +                       printk(KERN_ALERT "looking for wrong order?\n");
1085 +                       break;
1086 +               }
1087 +
1088 +               k = mb_find_next_zero_bit(buddy, max, 0);
1089 +               J_ASSERT(k < max);
1090 +
1091 +               ac->ac_found++;
1092 +
1093 +               ac->ac_b_ex.fe_len = 1 << i;
1094 +               ac->ac_b_ex.fe_start = k << i;
1095 +               ac->ac_b_ex.fe_group = e3b->bd_group;
1096 +               
1097 +               ext3_mb_use_best_found(ac, e3b);
1098 +               J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1099 +
1100 +               if (unlikely(ext3_mb_stats))
1101 +                       atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1102 +
1103 +               break;
1104 +       }
1105 +}
1106 +
1107 +/*
1108 + * The routine scans the group and measures all found extents.
1109 + * In order to optimize scanning, caller must pass number of
1110 + * free blocks in the group, so the routine can know upper limit.
1111 + */
1112 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1113 +                                       struct ext3_buddy *e3b)
1114 +{
1115 +       struct super_block *sb = ac->ac_sb;
1116 +       void *bitmap = EXT3_MB_BITMAP(e3b);
1117 +       struct ext3_free_extent ex;
1118 +       int i, free;
1119 +
1120 +       free = e3b->bd_info->bb_free;
1121 +       J_ASSERT(free > 0);
1122 +
1123 +       i = e3b->bd_info->bb_first_free;
1124 +
1125 +       while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1126 +               i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1127 +               if (i >= sb->s_blocksize * 8) {
1128 +                       J_ASSERT(free == 0);
1129 +                       break;
1130 +               }
1131 +
1132 +               mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1133 +               J_ASSERT(ex.fe_len > 0);
1134 +               J_ASSERT(free >= ex.fe_len);
1135 +
1136 +               ext3_mb_measure_extent(ac, &ex, e3b);
1137 +
1138 +               i += ex.fe_len;
1139 +               free -= ex.fe_len;
1140 +       }
1141 +}
1142 +
1143 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1144 +                               int group, int cr)
1145 +{
1146 +       struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1147 +       struct ext3_group_info *grp = sbi->s_group_info[group];
1148 +       unsigned free, fragments, i, bits;
1149 +
1150 +       J_ASSERT(cr >= 0 && cr < 4);
1151 +       J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1152 +
1153 +       free = grp->bb_free;
1154 +       fragments = grp->bb_fragments;
1155 +       if (free == 0)
1156 +               return 0;
1157 +       if (fragments == 0)
1158 +               return 0;
1159 +
1160 +       switch (cr) {
1161 +               case 0:
1162 +                       J_ASSERT(ac->ac_2order != 0);
1163 +                       bits = ac->ac_sb->s_blocksize_bits + 1;
1164 +                       for (i = ac->ac_2order; i < bits; i++)
1165 +                               if (grp->bb_counters[i] > 0)
1166 +                                       return 1;
1167 +               case 1:
1168 +                       if ((free / fragments) >= ac->ac_g_ex.fe_len)
1169 +                               return 1;
1170 +               case 2:
1171 +                       if (free >= ac->ac_g_ex.fe_len)
1172 +                               return 1;
1173 +               case 3:
1174 +                       return 1;
1175 +               default:
1176 +                       BUG();
1177 +       }
1178 +
1179 +       return 0;
1180 +}
1181 +
1182 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1183 +                      unsigned long goal, int *len, int flags, int *errp)
1184 +{
1185 +       struct buffer_head *bitmap_bh = NULL;
1186 +       struct ext3_allocation_context ac;
1187 +       int i, group, block, cr, err = 0;
1188 +       struct ext3_group_desc *gdp;
1189 +       struct ext3_super_block *es;
1190 +       struct buffer_head *gdp_bh;
1191 +       struct ext3_sb_info *sbi;
1192 +       struct super_block *sb;
1193 +       struct ext3_buddy e3b;
1194 +
1195 +       J_ASSERT(len != NULL);
1196 +       J_ASSERT(*len > 0);
1197 +
1198 +       sb = inode->i_sb;
1199 +       if (!sb) {
1200 +               printk("ext3_mb_new_nblocks: nonexistent device");
1201 +               return 0;
1202 +       }
1203 +
1204 +       if (!test_opt(sb, MBALLOC)) {
1205 +               static int ext3_mballoc_warning = 0;
1206 +               if (ext3_mballoc_warning == 0) {
1207 +                       printk(KERN_ERR "EXT3-fs: multiblock request with "
1208 +                               "mballoc disabled!\n");
1209 +                       ext3_mballoc_warning++;
1210 +               }
1211 +               *len = 1;
1212 +               err = ext3_new_block_old(handle, inode, goal, errp);
1213 +               return err;
1214 +       }
1215 +
1216 +       ext3_mb_poll_new_transaction(sb, handle);
1217 +
1218 +       sbi = EXT3_SB(sb);
1219 +       es = EXT3_SB(sb)->s_es;
1220 +
1221 +       /*
1222 +        * We can't allocate > group size
1223 +        */
1224 +       if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1225 +               *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1226 +
1227 +       if (!(flags & EXT3_MB_HINT_RESERVED)) {
1228 +               /* someone asks for non-reserved blocks */
1229 +               BUG_ON(*len > 1);
1230 +               err = ext3_mb_reserve_blocks(sb, 1);
1231 +               if (err) {
1232 +                       *errp = err;
1233 +                       return 0;
1234 +               }
1235 +       }
1236 +
1237 +       /*
1238 +        * Check quota for allocation of this blocks.
1239 +        */
1240 +       while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1241 +               *len -= 1;
1242 +       if (*len == 0) {
1243 +               *errp = -EDQUOT;
1244 +               block = 0;
1245 +               goto out;
1246 +       }
1247 +
1248 +       /* start searching from the goal */
1249 +       if (goal < le32_to_cpu(es->s_first_data_block) ||
1250 +           goal >= le32_to_cpu(es->s_blocks_count))
1251 +               goal = le32_to_cpu(es->s_first_data_block);
1252 +       group = (goal - le32_to_cpu(es->s_first_data_block)) /
1253 +                       EXT3_BLOCKS_PER_GROUP(sb);
1254 +       block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1255 +                       EXT3_BLOCKS_PER_GROUP(sb));
1256 +
1257 +       /* set up allocation goals */
1258 +       ac.ac_b_ex.fe_group = 0;
1259 +       ac.ac_b_ex.fe_start = 0;
1260 +       ac.ac_b_ex.fe_len = 0;
1261 +       ac.ac_status = AC_STATUS_CONTINUE;
1262 +       ac.ac_groups_scanned = 0;
1263 +       ac.ac_ex_scanned = 0;
1264 +       ac.ac_found = 0;
1265 +       ac.ac_sb = inode->i_sb;
1266 +       ac.ac_g_ex.fe_group = group;
1267 +       ac.ac_g_ex.fe_start = block;
1268 +       ac.ac_g_ex.fe_len = *len;
1269 +       ac.ac_flags = flags;
1270 +       ac.ac_2order = 0;
1271 +       ac.ac_criteria = 0;
1272 +
1273 +       /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1274 +       i = ffs(*len);
1275 +       if (i >= 8) {
1276 +               i--;
1277 +               if ((*len & (~(1 << i))) == 0)
1278 +                       ac.ac_2order = i;
1279 +       }
1280 +
1281 +       /* Sometimes, caller may want to merge even small
1282 +        * number of blocks to an existing extent */
1283 +       if (ac.ac_flags & EXT3_MB_HINT_MERGE) {
1284 +               err = ext3_mb_find_by_goal(&ac, &e3b);
1285 +               if (err)
1286 +                       goto out_err;
1287 +               if (ac.ac_status == AC_STATUS_FOUND)
1288 +                       goto found;
1289 +       }
1290 +
1291 +       /* Let's just scan groups to find more-less suitable blocks */
1292 +       cr = ac.ac_2order ? 0 : 1;
1293 +repeat:
1294 +       for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1295 +               ac.ac_criteria = cr;
1296 +               for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1297 +                       if (group == EXT3_SB(sb)->s_groups_count)
1298 +                               group = 0;
1299 +
1300 +                       if (EXT3_MB_GRP_NEED_INIT(sbi->s_group_info[group])) {
1301 +                               /* we need full data about the group
1302 +                                * to make a good selection */
1303 +                               err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1304 +                               if (err)
1305 +                                       goto out_err;
1306 +                               ext3_mb_release_desc(&e3b);
1307 +                       }
1308 +                               
1309 +                       /* check is group good for our criteries */
1310 +                       if (!ext3_mb_good_group(&ac, group, cr))
1311 +                               continue;
1312 +
1313 +                       err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1314 +                       if (err)
1315 +                               goto out_err;
1316 +
1317 +                       ext3_lock_group(sb, group);
1318 +                       if (!ext3_mb_good_group(&ac, group, cr)) {
1319 +                               /* someone did allocation from this group */
1320 +                               ext3_unlock_group(sb, group);
1321 +                               ext3_mb_release_desc(&e3b);
1322 +                               continue;
1323 +                       }
1324 +
1325 +                       ac.ac_groups_scanned++;
1326 +                       if (cr == 0)
1327 +                               ext3_mb_simple_scan_group(&ac, &e3b);
1328 +                       else
1329 +                               ext3_mb_complex_scan_group(&ac, &e3b);
1330 +
1331 +                       ext3_unlock_group(sb, group);
1332 +
1333 +                       ext3_mb_release_desc(&e3b);
1334 +
1335 +                       if (err)
1336 +                               goto out_err;
1337 +                       if (ac.ac_status != AC_STATUS_CONTINUE)
1338 +                               break;
1339 +               }
1340 +       }
1341 +
1342 +       if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1343 +                       !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1344 +               /*
1345 +                * We've been searching too long. Let's try to allocate
1346 +                * the best chunk we've found so far
1347 +                */
1348 +
1349 +               /*if (ac.ac_found > ext3_mb_max_to_scan)
1350 +                       printk(KERN_ERR "EXT3-fs: too long searching at "
1351 +                               "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1352 +                               ac.ac_g_ex.fe_len);*/
1353 +               ext3_mb_try_best_found(&ac, &e3b);
1354 +               if (ac.ac_status != AC_STATUS_FOUND) {
1355 +                       /*
1356 +                        * Someone more lucky has already allocated it.
1357 +                        * The only thing we can do is just take first
1358 +                        * found block(s)
1359 +                       printk(KERN_ERR "EXT3-fs: and someone won our chunk\n");
1360 +                        */
1361 +                       ac.ac_b_ex.fe_group = 0;
1362 +                       ac.ac_b_ex.fe_start = 0;
1363 +                       ac.ac_b_ex.fe_len = 0;
1364 +                       ac.ac_status = AC_STATUS_CONTINUE;
1365 +                       ac.ac_flags |= EXT3_MB_HINT_FIRST;
1366 +                       cr = 3;
1367 +                       goto repeat;
1368 +               }
1369 +       }
1370 +
1371 +       if (ac.ac_status != AC_STATUS_FOUND) {
1372 +               /*
1373 +                * We aren't lucky definitely
1374 +                */
1375 +               DQUOT_FREE_BLOCK(inode, *len);
1376 +               *errp = -ENOSPC;
1377 +               block = 0;
1378 +#if 1
1379 +               printk(KERN_ERR "EXT3-fs: cant allocate: status %d, flags %d\n",
1380 +                       ac.ac_status, ac.ac_flags);
1381 +               printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d, cr %d\n",
1382 +                       ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1383 +                       ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1384 +               printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1385 +                       sbi->s_blocks_reserved, ac.ac_found);
1386 +               printk("EXT3-fs: groups: ");
1387 +               for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1388 +                       printk("%d: %d ", i,
1389 +                               sbi->s_group_info[i]->bb_free);
1390 +               printk("\n");
1391 +#endif
1392 +               goto out;
1393 +       }
1394 +
1395 +found:
1396 +       J_ASSERT(ac.ac_b_ex.fe_len > 0);
1397 +
1398 +       /* good news - free block(s) have been found. now it's time
1399 +        * to mark block(s) in good old journaled bitmap */
1400 +       block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1401 +                       + ac.ac_b_ex.fe_start
1402 +                       + le32_to_cpu(es->s_first_data_block);
1403 +
1404 +       /* we made a desicion, now mark found blocks in good old
1405 +        * bitmap to be journaled */
1406 +
1407 +       ext3_debug("using block group %d(%d)\n",
1408 +                       ac.ac_b_group.group, gdp->bg_free_blocks_count);
1409 +
1410 +       bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1411 +       if (!bitmap_bh) {
1412 +               *errp = -EIO;
1413 +               goto out_err;
1414 +       }
1415 +
1416 +       err = ext3_journal_get_write_access(handle, bitmap_bh);
1417 +       if (err) {
1418 +               *errp = err;
1419 +               goto out_err;
1420 +       }
1421 +
1422 +       gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1423 +       if (!gdp) {
1424 +               *errp = -EIO;
1425 +               goto out_err;
1426 +       }
1427 +       
1428 +       err = ext3_journal_get_write_access(handle, gdp_bh);
1429 +       if (err)
1430 +               goto out_err;
1431 +
1432 +       block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1433 +                       + ac.ac_b_ex.fe_start
1434 +                       + le32_to_cpu(es->s_first_data_block);
1435 +
1436 +       if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1437 +           block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1438 +           in_range(block, le32_to_cpu(gdp->bg_inode_table),
1439 +                     EXT3_SB(sb)->s_itb_per_group))
1440 +               ext3_error(sb, "ext3_new_block",
1441 +                           "Allocating block in system zone - "
1442 +                           "block = %u", block);
1443 +#ifdef AGGRESSIVE_CHECK
1444 +       for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1445 +               J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1446 +#endif
1447 +       mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1448 +
1449 +       spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1450 +       gdp->bg_free_blocks_count =
1451 +                       cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1452 +                                       - ac.ac_b_ex.fe_len);
1453 +       spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1454 +       percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1455 +
1456 +       err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1457 +       if (err)
1458 +               goto out_err;
1459 +       err = ext3_journal_dirty_metadata(handle, gdp_bh);
1460 +       if (err)
1461 +               goto out_err;
1462 +
1463 +       sb->s_dirt = 1;
1464 +       *errp = 0;
1465 +       brelse(bitmap_bh);
1466 +
1467 +       /* drop non-allocated, but dquote'd blocks */
1468 +       J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1469 +       DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1470 +
1471 +       *len = ac.ac_b_ex.fe_len;
1472 +       J_ASSERT(*len > 0);
1473 +       J_ASSERT(block != 0);
1474 +       goto out;
1475 +
1476 +out_err:
1477 +       /* if we've already allocated something, roll it back */
1478 +       if (ac.ac_status == AC_STATUS_FOUND) {
1479 +               /* FIXME: free blocks here */
1480 +       }
1481 +
1482 +       DQUOT_FREE_BLOCK(inode, *len);
1483 +       brelse(bitmap_bh);
1484 +       *errp = err;
1485 +       block = 0;
1486 +out:
1487 +       if (!(flags & EXT3_MB_HINT_RESERVED)) {
1488 +               /* block wasn't reserved before and we reserved it
1489 +                * at the beginning of allocation. it doesn't matter
1490 +                * whether we allocated anything or we failed: time
1491 +                * to release reservation. NOTE: because I expect
1492 +                * any multiblock request from delayed allocation
1493 +                * path only, here is single block always */
1494 +               ext3_mb_release_blocks(sb, 1);
1495 +       }
1496 +       
1497 +       if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1498 +               atomic_inc(&sbi->s_bal_reqs);
1499 +               atomic_add(*len, &sbi->s_bal_allocated);
1500 +               if (*len >= ac.ac_g_ex.fe_len)
1501 +                       atomic_inc(&sbi->s_bal_success);
1502 +               atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1503 +               if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1504 +                               ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1505 +                       atomic_inc(&sbi->s_bal_goals);
1506 +               if (ac.ac_found > ext3_mb_max_to_scan)
1507 +                       atomic_inc(&sbi->s_bal_breaks);
1508 +       }
1509 +
1510 +       ext3_mb_store_history(sb, &ac);
1511 +
1512 +       return block;
1513 +}
1514 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1515 +
1516 +#ifdef EXT3_MB_HISTORY
1517 +struct ext3_mb_proc_session {
1518 +       struct ext3_mb_history *history;
1519 +       struct super_block *sb;
1520 +       int start;
1521 +       int max;
1522 +};
1523 +
1524 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1525 +                                       struct ext3_mb_history *hs,
1526 +                                       int first)
1527 +{
1528 +       if (hs == s->history + s->max)
1529 +               hs = s->history;
1530 +       if (!first && hs == s->history + s->start)
1531 +               return NULL;
1532 +       while (hs->goal.fe_len == 0) {
1533 +               hs++;
1534 +               if (hs == s->history + s->max)
1535 +                       hs = s->history;
1536 +               if (hs == s->history + s->start)
1537 +                       return NULL;
1538 +       }
1539 +       return hs;
1540 +}
1541 +
1542 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
1543 +{
1544 +       struct ext3_mb_proc_session *s = seq->private;
1545 +       struct ext3_mb_history *hs;
1546 +       int l = *pos;
1547 +
1548 +       if (l == 0)
1549 +               return SEQ_START_TOKEN;
1550 +       hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
1551 +       if (!hs)
1552 +               return NULL;
1553 +       while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
1554 +       return hs;
1555 +}
1556 +
1557 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
1558 +{
1559 +       struct ext3_mb_proc_session *s = seq->private;
1560 +       struct ext3_mb_history *hs = v;
1561 +
1562 +       ++*pos;
1563 +       if (v == SEQ_START_TOKEN)
1564 +               return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
1565 +       else
1566 +               return ext3_mb_history_skip_empty(s, ++hs, 0);
1567 +}
1568 +
1569 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
1570 +{
1571 +       struct ext3_mb_history *hs = v;
1572 +       char buf[20], buf2[20];
1573 +
1574 +       if (v == SEQ_START_TOKEN) {
1575 +               seq_printf(seq, "%-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
1576 +                        "goal", "result", "found", "grps", "cr", "merge",
1577 +                        "tail", "broken");
1578 +               return 0;
1579 +       }
1580 +
1581 +       sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
1582 +               hs->goal.fe_start, hs->goal.fe_len);
1583 +       sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
1584 +               hs->result.fe_start, hs->result.fe_len);
1585 +       seq_printf(seq, "%-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n", buf,
1586 +                       buf2, hs->found, hs->groups, hs->cr, 
1587 +                       hs->merged ? "M" : "", hs->tail,
1588 +                       hs->buddy ? 1 << hs->buddy : 0);
1589 +       return 0;
1590 +}
1591 +
1592 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
1593 +{
1594 +}
1595 +
1596 +static struct seq_operations ext3_mb_seq_history_ops = {
1597 +       .start  = ext3_mb_seq_history_start,
1598 +       .next   = ext3_mb_seq_history_next,
1599 +       .stop   = ext3_mb_seq_history_stop,
1600 +       .show   = ext3_mb_seq_history_show,
1601 +};
1602 +
1603 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
1604 +{
1605 +       struct super_block *sb = PDE(inode)->data;
1606 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1607 +       struct ext3_mb_proc_session *s;
1608 +       int rc, size;
1609 +
1610 +       s = kmalloc(sizeof(*s), GFP_KERNEL);
1611 +       if (s == NULL)
1612 +               return -EIO;
1613 +       size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
1614 +       s->history = kmalloc(size, GFP_KERNEL);
1615 +       if (s == NULL) {
1616 +               kfree(s);
1617 +               return -EIO;
1618 +       }
1619 +
1620 +       spin_lock(&sbi->s_mb_history_lock);
1621 +       memcpy(s->history, sbi->s_mb_history, size);
1622 +       s->max = sbi->s_mb_history_max;
1623 +       s->start = sbi->s_mb_history_cur % s->max;
1624 +       spin_unlock(&sbi->s_mb_history_lock);
1625 +       
1626 +       rc = seq_open(file, &ext3_mb_seq_history_ops);
1627 +       if (rc == 0) {
1628 +               struct seq_file *m = (struct seq_file *)file->private_data;
1629 +               m->private = s;
1630 +       } else {
1631 +               kfree(s->history);
1632 +               kfree(s);
1633 +       }
1634 +       return rc;
1635 +
1636 +}
1637 +
1638 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
1639 +{
1640 +       struct seq_file *seq = (struct seq_file *)file->private_data;
1641 +       struct ext3_mb_proc_session *s = seq->private;
1642 +       kfree(s->history);
1643 +       kfree(s);
1644 +       return seq_release(inode, file);
1645 +}
1646 +
1647 +static struct file_operations ext3_mb_seq_history_fops = {
1648 +       .owner          = THIS_MODULE,
1649 +       .open           = ext3_mb_seq_history_open,
1650 +       .read           = seq_read,
1651 +       .llseek         = seq_lseek,
1652 +       .release        = ext3_mb_seq_history_release,
1653 +};
1654 +
1655 +static void ext3_mb_history_release(struct super_block *sb)
1656 +{
1657 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1658 +       char name[64];
1659 +
1660 +       snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
1661 +       remove_proc_entry("mb_history", sbi->s_mb_proc);
1662 +       remove_proc_entry(name, proc_root_ext3);
1663 +
1664 +       if (sbi->s_mb_history)
1665 +               kfree(sbi->s_mb_history);
1666 +}
1667 +
1668 +static void ext3_mb_history_init(struct super_block *sb)
1669 +{
1670 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1671 +       char name[64];
1672 +       int i;
1673 +
1674 +       snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
1675 +       sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
1676 +       if (sbi->s_mb_proc != NULL) {
1677 +               struct proc_dir_entry *p;
1678 +               p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
1679 +               if (p) {
1680 +                       p->proc_fops = &ext3_mb_seq_history_fops;
1681 +                       p->data = sb;
1682 +               }
1683 +       }
1684 +
1685 +       sbi->s_mb_history_max = 1000;
1686 +       sbi->s_mb_history_cur = 0;
1687 +       spin_lock_init(&sbi->s_mb_history_lock);
1688 +       i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
1689 +       sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
1690 +       memset(sbi->s_mb_history, 0, i);
1691 +       /* if we can't allocate history, then we simple won't use it */
1692 +}
1693 +
1694 +static void
1695 +ext3_mb_store_history(struct super_block *sb, struct ext3_allocation_context *ac)
1696 +{
1697 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1698 +       struct ext3_mb_history h;
1699 +
1700 +       if (likely(sbi->s_mb_history == NULL))
1701 +               return;
1702 +
1703 +       h.goal = ac->ac_g_ex;
1704 +       h.result = ac->ac_b_ex;
1705 +       h.found = ac->ac_found;
1706 +       h.cr = ac->ac_criteria;
1707 +       h.groups = ac->ac_groups_scanned;
1708 +       h.tail = ac->ac_tail;
1709 +       h.buddy = ac->ac_buddy;
1710 +       h.merged = 0;
1711 +       if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
1712 +                       ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
1713 +               h.merged = 1;
1714 +
1715 +       spin_lock(&sbi->s_mb_history_lock);
1716 +       memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
1717 +       if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
1718 +               sbi->s_mb_history_cur = 0;
1719 +       spin_unlock(&sbi->s_mb_history_lock);
1720 +}
1721 +
1722 +#else
1723 +#define ext3_mb_history_release(sb)
1724 +#define ext3_mb_history_init(sb)
1725 +#endif
1726 +
1727 +int ext3_mb_init_backend(struct super_block *sb)
1728 +{
1729 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1730 +       int i, len;
1731 +
1732 +       len = sizeof(struct ext3_buddy_group_blocks *) * sbi->s_groups_count;
1733 +       sbi->s_group_info = kmalloc(len, GFP_KERNEL);
1734 +       if (sbi->s_group_info == NULL) {
1735 +               printk(KERN_ERR "EXT3-fs: can't allocate mem for buddy\n");
1736 +               return -ENOMEM;
1737 +       }
1738 +       memset(sbi->s_group_info, 0, len);
1739 +
1740 +       sbi->s_buddy_cache = new_inode(sb);
1741 +       if (sbi->s_buddy_cache == NULL) {
1742 +               printk(KERN_ERR "EXT3-fs: can't get new inode\n");
1743 +               kfree(sbi->s_group_info);
1744 +               return -ENOMEM;
1745 +       }
1746 +
1747 +       /* 
1748 +        * calculate needed size. if change bb_counters size,
1749 +        * don't forget about ext3_mb_generate_buddy()
1750 +        */
1751 +       len = sizeof(struct ext3_group_info);
1752 +       len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
1753 +       for (i = 0; i < sbi->s_groups_count; i++) {
1754 +               struct ext3_group_desc * desc;
1755 +
1756 +               sbi->s_group_info[i] = kmalloc(len, GFP_KERNEL);
1757 +               if (sbi->s_group_info[i] == NULL) {
1758 +                       printk(KERN_ERR "EXT3-fs: cant allocate mem for buddy\n");
1759 +                       goto err_out;
1760 +               }
1761 +               desc = ext3_get_group_desc(sb, i, NULL);
1762 +               if (desc == NULL) {
1763 +                       printk(KERN_ERR "EXT3-fs: cant read descriptor %u\n", i);
1764 +                       goto err_out;
1765 +               }
1766 +               memset(sbi->s_group_info[i], 0, len);
1767 +               set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
1768 +                       &sbi->s_group_info[i]->bb_state);
1769 +               sbi->s_group_info[i]->bb_free =
1770 +                       le16_to_cpu(desc->bg_free_blocks_count);
1771 +       }
1772 +
1773 +       return 0;
1774 +
1775 +err_out:
1776 +       while (--i >= 0)
1777 +               kfree(sbi->s_group_info[i]);
1778 +       iput(sbi->s_buddy_cache);
1779 +
1780 +       return -ENOMEM;
1781 +}
1782 +
1783 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
1784 +{
1785 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1786 +       struct inode *root = sb->s_root->d_inode;
1787 +       unsigned i, offset, max;
1788 +       struct dentry *dentry;
1789 +
1790 +       if (!test_opt(sb, MBALLOC))
1791 +               return 0;
1792 +
1793 +       i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
1794 +
1795 +       sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
1796 +       if (sbi->s_mb_offsets == NULL) {
1797 +               clear_opt(sbi->s_mount_opt, MBALLOC);
1798 +               return -ENOMEM;
1799 +       }
1800 +       sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
1801 +       if (sbi->s_mb_maxs == NULL) {
1802 +               clear_opt(sbi->s_mount_opt, MBALLOC);
1803 +               kfree(sbi->s_mb_maxs);
1804 +               return -ENOMEM;
1805 +       }
1806 +
1807 +        /* order 0 is regular bitmap */
1808 +       sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
1809 +       sbi->s_mb_offsets[0] = 0;
1810 +
1811 +       i = 1;
1812 +       offset = 0;
1813 +       max = sb->s_blocksize << 2;
1814 +       do {
1815 +               sbi->s_mb_offsets[i] = offset;
1816 +               sbi->s_mb_maxs[i] = max;
1817 +               offset += 1 << (sb->s_blocksize_bits - i);
1818 +               max = max >> 1;
1819 +               i++;
1820 +       } while (i <= sb->s_blocksize_bits + 1);
1821 +       
1822 +
1823 +       /* init file for buddy data */
1824 +       if ((i = ext3_mb_init_backend(sb))) {
1825 +               clear_opt(sbi->s_mount_opt, MBALLOC);
1826 +               kfree(sbi->s_mb_offsets);
1827 +               kfree(sbi->s_mb_maxs);
1828 +               return i;
1829 +       }
1830 +
1831 +       spin_lock_init(&sbi->s_reserve_lock);
1832 +       spin_lock_init(&sbi->s_md_lock);
1833 +       INIT_LIST_HEAD(&sbi->s_active_transaction);
1834 +       INIT_LIST_HEAD(&sbi->s_closed_transaction);
1835 +       INIT_LIST_HEAD(&sbi->s_committed_transaction);
1836 +       spin_lock_init(&sbi->s_bal_lock);
1837 +
1838 +       /* remove old on-disk buddy file */
1839 +       mutex_lock(&root->i_mutex);
1840 +       dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
1841 +       if (dentry->d_inode != NULL) {
1842 +               i = vfs_unlink(root, dentry);
1843 +               if (i != 0)
1844 +                       printk("EXT3-fs: can't remove .buddy file: %d\n", i);
1845 +       }
1846 +       dput(dentry);
1847 +       mutex_unlock(&root->i_mutex);
1848 +
1849 +       ext3_mb_history_init(sb);
1850 +
1851 +       printk("EXT3-fs: mballoc enabled\n");
1852 +       return 0;
1853 +}
1854 +
1855 +int ext3_mb_release(struct super_block *sb)
1856 +{
1857 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1858 +       int i;
1859 +       
1860 +       if (!test_opt(sb, MBALLOC))
1861 +               return 0;
1862 +
1863 +       /* release freed, non-committed blocks */
1864 +       spin_lock(&sbi->s_md_lock);
1865 +       list_splice_init(&sbi->s_closed_transaction,
1866 +                       &sbi->s_committed_transaction);
1867 +       list_splice_init(&sbi->s_active_transaction,
1868 +                       &sbi->s_committed_transaction);
1869 +       spin_unlock(&sbi->s_md_lock);
1870 +       ext3_mb_free_committed_blocks(sb);
1871 +
1872 +       if (sbi->s_group_info) {
1873 +               for (i = 0; i < sbi->s_groups_count; i++) {
1874 +                       if (sbi->s_group_info[i] == NULL)
1875 +                               continue;
1876 +                       kfree(sbi->s_group_info[i]);
1877 +               }
1878 +               kfree(sbi->s_group_info);
1879 +       }
1880 +       if (sbi->s_mb_offsets)
1881 +               kfree(sbi->s_mb_offsets);
1882 +       if (sbi->s_mb_maxs)
1883 +               kfree(sbi->s_mb_maxs);
1884 +       if (sbi->s_buddy_cache)
1885 +               iput(sbi->s_buddy_cache);
1886 +       if (sbi->s_blocks_reserved)
1887 +               printk("ext3-fs: %ld blocks being reserved at umount!\n",
1888 +                               sbi->s_blocks_reserved);
1889 +       if (ext3_mb_stats) {
1890 +               printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
1891 +                       atomic_read(&sbi->s_bal_allocated),
1892 +                       atomic_read(&sbi->s_bal_reqs),
1893 +                       atomic_read(&sbi->s_bal_success));
1894 +               printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
1895 +                       "%u 2^N hits, %u breaks\n",
1896 +                       atomic_read(&sbi->s_bal_ex_scanned),
1897 +                       atomic_read(&sbi->s_bal_goals),
1898 +                       atomic_read(&sbi->s_bal_2orders),
1899 +                       atomic_read(&sbi->s_bal_breaks));
1900 +               printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
1901 +                       sbi->s_mb_buddies_generated++,
1902 +                       sbi->s_mb_generation_time);
1903 +       }
1904 +
1905 +       ext3_mb_history_release(sb);
1906 +
1907 +       return 0;
1908 +}
1909 +
1910 +void ext3_mb_free_committed_blocks(struct super_block *sb)
1911 +{
1912 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1913 +       int err, i, count = 0, count2 = 0;
1914 +       struct ext3_free_metadata *md;
1915 +       struct ext3_buddy e3b;
1916 +
1917 +       if (list_empty(&sbi->s_committed_transaction))
1918 +               return;
1919 +
1920 +       /* there is committed blocks to be freed yet */
1921 +       do {
1922 +               /* get next array of blocks */
1923 +               md = NULL;
1924 +               spin_lock(&sbi->s_md_lock);
1925 +               if (!list_empty(&sbi->s_committed_transaction)) {
1926 +                       md = list_entry(sbi->s_committed_transaction.next,
1927 +                                       struct ext3_free_metadata, list);
1928 +                       list_del(&md->list);
1929 +               }
1930 +               spin_unlock(&sbi->s_md_lock);
1931 +
1932 +               if (md == NULL)
1933 +                       break;
1934 +
1935 +               mb_debug("gonna free %u blocks in group %u (0x%p):",
1936 +                               md->num, md->group, md);
1937 +
1938 +               err = ext3_mb_load_buddy(sb, md->group, &e3b);
1939 +               BUG_ON(err != 0);
1940 +
1941 +               /* there are blocks to put in buddy to make them really free */
1942 +               count += md->num;
1943 +               count2++;
1944 +               ext3_lock_group(sb, md->group);
1945 +               for (i = 0; i < md->num; i++) {
1946 +                       mb_debug(" %u", md->blocks[i]);
1947 +                       mb_free_blocks(&e3b, md->blocks[i], 1);
1948 +               }
1949 +               mb_debug("\n");
1950 +               ext3_unlock_group(sb, md->group);
1951 +
1952 +               /* balance refcounts from ext3_mb_free_metadata() */
1953 +               page_cache_release(e3b.bd_buddy_page);
1954 +               page_cache_release(e3b.bd_bitmap_page);
1955 +
1956 +               kfree(md);
1957 +               ext3_mb_release_desc(&e3b);
1958 +
1959 +       } while (md);
1960 +       mb_debug("freed %u blocks in %u structures\n", count, count2);
1961 +}
1962 +
1963 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
1964 +{
1965 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1966 +
1967 +       if (sbi->s_last_transaction == handle->h_transaction->t_tid)
1968 +               return;
1969 +
1970 +       /* new transaction! time to close last one and free blocks for
1971 +        * committed transaction. we know that only transaction can be
1972 +        * active, so previos transaction can be being logged and we
1973 +        * know that transaction before previous is known to be already
1974 +        * logged. this means that now we may free blocks freed in all
1975 +        * transactions before previous one. hope I'm clear enough ... */
1976 +
1977 +       spin_lock(&sbi->s_md_lock);
1978 +       if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
1979 +               mb_debug("new transaction %lu, old %lu\n",
1980 +                               (unsigned long) handle->h_transaction->t_tid,
1981 +                               (unsigned long) sbi->s_last_transaction);
1982 +               list_splice_init(&sbi->s_closed_transaction,
1983 +                                       &sbi->s_committed_transaction);
1984 +               list_splice_init(&sbi->s_active_transaction,
1985 +                                       &sbi->s_closed_transaction);
1986 +               sbi->s_last_transaction = handle->h_transaction->t_tid;
1987 +       }
1988 +       spin_unlock(&sbi->s_md_lock);
1989 +
1990 +       ext3_mb_free_committed_blocks(sb);
1991 +}
1992 +
1993 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
1994 +                               int group, int block, int count)
1995 +{
1996 +       struct ext3_group_info *db = e3b->bd_info;
1997 +       struct super_block *sb = e3b->bd_sb;
1998 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1999 +       struct ext3_free_metadata *md;
2000 +       int i;
2001 +
2002 +       J_ASSERT(e3b->bd_bitmap_page != NULL);
2003 +       J_ASSERT(e3b->bd_buddy_page != NULL);
2004 +
2005 +       ext3_lock_group(sb, group);
2006 +       for (i = 0; i < count; i++) {
2007 +               md = db->bb_md_cur;
2008 +               if (md && db->bb_tid != handle->h_transaction->t_tid) {
2009 +                       db->bb_md_cur = NULL;
2010 +                       md = NULL;
2011 +               }
2012 +
2013 +               if (md == NULL) {
2014 +                       ext3_unlock_group(sb, group);
2015 +                       md = kmalloc(sizeof(*md), GFP_KERNEL);
2016 +                       if (md == NULL)
2017 +                               return -ENOMEM;
2018 +                       md->num = 0;
2019 +                       md->group = group;
2020 +
2021 +                       ext3_lock_group(sb, group);
2022 +                       if (db->bb_md_cur == NULL) {
2023 +                               spin_lock(&sbi->s_md_lock);
2024 +                               list_add(&md->list, &sbi->s_active_transaction);
2025 +                               spin_unlock(&sbi->s_md_lock);
2026 +                               /* protect buddy cache from being freed,
2027 +                                * otherwise we'll refresh it from
2028 +                                * on-disk bitmap and lose not-yet-available
2029 +                                * blocks */
2030 +                               page_cache_get(e3b->bd_buddy_page);
2031 +                               page_cache_get(e3b->bd_bitmap_page);
2032 +                               db->bb_md_cur = md;
2033 +                               db->bb_tid = handle->h_transaction->t_tid;
2034 +                               mb_debug("new md 0x%p for group %u\n",
2035 +                                                       md, md->group);
2036 +                       } else {
2037 +                               kfree(md);
2038 +                               md = db->bb_md_cur;
2039 +                       }
2040 +               }
2041 +
2042 +               BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2043 +               md->blocks[md->num] = block + i;
2044 +               md->num++;
2045 +               if (md->num == EXT3_BB_MAX_BLOCKS) {
2046 +                       /* no more space, put full container on a sb's list */
2047 +                       db->bb_md_cur = NULL;
2048 +               }
2049 +       }
2050 +       ext3_unlock_group(sb, group);
2051 +       return 0;
2052 +}
2053 +
2054 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2055 +                       unsigned long block, unsigned long count,
2056 +                       int metadata, int *freed)
2057 +{
2058 +       struct buffer_head *bitmap_bh = NULL;
2059 +       struct ext3_group_desc *gdp;
2060 +       struct ext3_super_block *es;
2061 +       unsigned long bit, overflow;
2062 +       struct buffer_head *gd_bh;
2063 +       unsigned long block_group;
2064 +       struct ext3_sb_info *sbi;
2065 +       struct super_block *sb;
2066 +       struct ext3_buddy e3b;
2067 +       int err = 0, ret;
2068 +
2069 +       *freed = 0;
2070 +       sb = inode->i_sb;
2071 +       if (!sb) {
2072 +               printk ("ext3_free_blocks: nonexistent device");
2073 +               return;
2074 +       }
2075 +
2076 +       ext3_mb_poll_new_transaction(sb, handle);
2077 +
2078 +       sbi = EXT3_SB(sb);
2079 +       es = EXT3_SB(sb)->s_es;
2080 +       if (block < le32_to_cpu(es->s_first_data_block) ||
2081 +           block + count < block ||
2082 +           block + count > le32_to_cpu(es->s_blocks_count)) {
2083 +               ext3_error (sb, "ext3_free_blocks",
2084 +                           "Freeing blocks not in datazone - "
2085 +                           "block = %lu, count = %lu", block, count);
2086 +               goto error_return;
2087 +       }
2088 +
2089 +       ext3_debug("freeing block %lu\n", block);
2090 +
2091 +do_more:
2092 +       overflow = 0;
2093 +       block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2094 +                     EXT3_BLOCKS_PER_GROUP(sb);
2095 +       bit = (block - le32_to_cpu(es->s_first_data_block)) %
2096 +                     EXT3_BLOCKS_PER_GROUP(sb);
2097 +       /*
2098 +        * Check to see if we are freeing blocks across a group
2099 +        * boundary.
2100 +        */
2101 +       if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2102 +               overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2103 +               count -= overflow;
2104 +       }
2105 +       brelse(bitmap_bh);
2106 +       bitmap_bh = read_block_bitmap(sb, block_group);
2107 +       if (!bitmap_bh)
2108 +               goto error_return;
2109 +       gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2110 +       if (!gdp)
2111 +               goto error_return;
2112 +
2113 +       if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2114 +           in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2115 +           in_range (block, le32_to_cpu(gdp->bg_inode_table),
2116 +                     EXT3_SB(sb)->s_itb_per_group) ||
2117 +           in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2118 +                     EXT3_SB(sb)->s_itb_per_group))
2119 +               ext3_error (sb, "ext3_free_blocks",
2120 +                           "Freeing blocks in system zones - "
2121 +                           "Block = %lu, count = %lu",
2122 +                           block, count);
2123 +
2124 +       BUFFER_TRACE(bitmap_bh, "getting write access");
2125 +       err = ext3_journal_get_write_access(handle, bitmap_bh);
2126 +       if (err)
2127 +               goto error_return;
2128 +
2129 +       /*
2130 +        * We are about to modify some metadata.  Call the journal APIs
2131 +        * to unshare ->b_data if a currently-committing transaction is
2132 +        * using it
2133 +        */
2134 +       BUFFER_TRACE(gd_bh, "get_write_access");
2135 +       err = ext3_journal_get_write_access(handle, gd_bh);
2136 +       if (err)
2137 +               goto error_return;
2138 +
2139 +       err = ext3_mb_load_buddy(sb, block_group, &e3b);
2140 +       if (err)
2141 +               goto error_return;
2142 +
2143 +#ifdef AGGRESSIVE_CHECK
2144 +       {
2145 +               int i;
2146 +               for (i = 0; i < count; i++)
2147 +                       J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2148 +       }
2149 +#endif
2150 +       mb_clear_bits(bitmap_bh->b_data, bit, count);
2151 +
2152 +       /* We dirtied the bitmap block */
2153 +       BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2154 +       err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2155 +
2156 +       if (metadata) {
2157 +               /* blocks being freed are metadata. these blocks shouldn't
2158 +                * be used until this transaction is committed */
2159 +               ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2160 +       } else { 
2161 +               ext3_lock_group(sb, block_group);
2162 +               mb_free_blocks(&e3b, bit, count);
2163 +               ext3_unlock_group(sb, block_group);
2164 +       }
2165 +
2166 +       spin_lock(sb_bgl_lock(sbi, block_group));
2167 +       gdp->bg_free_blocks_count =
2168 +               cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2169 +       spin_unlock(sb_bgl_lock(sbi, block_group));
2170 +       percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2171 +       
2172 +       ext3_mb_release_desc(&e3b);
2173 +
2174 +       *freed = count;
2175 +
2176 +       /* And the group descriptor block */
2177 +       BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2178 +       ret = ext3_journal_dirty_metadata(handle, gd_bh);
2179 +       if (!err) err = ret;
2180 +
2181 +       if (overflow && !err) {
2182 +               block += count;
2183 +               count = overflow;
2184 +               goto do_more;
2185 +       }
2186 +       sb->s_dirt = 1;
2187 +error_return:
2188 +       brelse(bitmap_bh);
2189 +       ext3_std_error(sb, err);
2190 +       return;
2191 +}
2192 +
2193 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2194 +{
2195 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2196 +       int free, ret = -ENOSPC;
2197 +
2198 +       BUG_ON(blocks < 0);
2199 +       spin_lock(&sbi->s_reserve_lock);
2200 +       free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2201 +       if (blocks <= free - sbi->s_blocks_reserved) {
2202 +               sbi->s_blocks_reserved += blocks;
2203 +               ret = 0;
2204 +       }
2205 +       spin_unlock(&sbi->s_reserve_lock);
2206 +       return ret;
2207 +}
2208 +
2209 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2210 +{
2211 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2212 +
2213 +       BUG_ON(blocks < 0);
2214 +       spin_lock(&sbi->s_reserve_lock);
2215 +       sbi->s_blocks_reserved -= blocks;
2216 +       WARN_ON(sbi->s_blocks_reserved < 0);
2217 +       if (sbi->s_blocks_reserved < 0)
2218 +               sbi->s_blocks_reserved = 0;
2219 +       spin_unlock(&sbi->s_reserve_lock);
2220 +}
2221 +
2222 +int ext3_new_block(handle_t *handle, struct inode *inode,
2223 +               unsigned long goal, int *errp)
2224 +{
2225 +       int ret, len;
2226 +
2227 +       if (!test_opt(inode->i_sb, MBALLOC)) {
2228 +               ret = ext3_new_block_old(handle, inode, goal, errp);
2229 +               goto out;
2230 +       }
2231 +       len = 1;
2232 +       ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2233 +out:
2234 +       return ret;
2235 +}
2236 +
2237 +
2238 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2239 +                       unsigned long block, unsigned long count, int metadata)
2240 +{
2241 +       struct super_block *sb;
2242 +       int freed;
2243 +
2244 +       sb = inode->i_sb;
2245 +       if (!test_opt(sb, MBALLOC))
2246 +               ext3_free_blocks_sb(handle, sb, block, count, &freed);
2247 +       else
2248 +               ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2249 +       if (freed)
2250 +               DQUOT_FREE_BLOCK(inode, freed);
2251 +       return;
2252 +}
2253 +
2254 +#define EXT3_ROOT                 "ext3"
2255 +#define EXT3_MB_STATS_NAME        "mb_stats"
2256 +#define EXT3_MB_MAX_TO_SCAN_NAME  "mb_max_to_scan"
2257 +#define EXT3_MB_MIN_TO_SCAN_NAME  "mb_min_to_scan"
2258 +
2259 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2260 +               int count, int *eof, void *data)
2261 +{
2262 +       int len;
2263 +
2264 +       *eof = 1;
2265 +       if (off != 0)
2266 +               return 0;
2267 +
2268 +       len = sprintf(page, "%ld\n", ext3_mb_stats);
2269 +       *start = page;
2270 +       return len;
2271 +}
2272 +
2273 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2274 +               unsigned long count, void *data)
2275 +{
2276 +       char str[32];
2277 +
2278 +       if (count >= sizeof(str)) {
2279 +               printk(KERN_ERR "EXT3: %s string to long, max %u bytes\n",
2280 +                      EXT3_MB_STATS_NAME, (int)sizeof(str));
2281 +               return -EOVERFLOW;
2282 +       }
2283 +
2284 +       if (copy_from_user(str, buffer, count))
2285 +               return -EFAULT;
2286 +
2287 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2288 +       ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2289 +       return count;
2290 +}
2291 +
2292 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2293 +               int count, int *eof, void *data)
2294 +{
2295 +       int len;
2296 +
2297 +       *eof = 1;
2298 +       if (off != 0)
2299 +               return 0;
2300 +
2301 +       len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2302 +       *start = page;
2303 +       return len;
2304 +}
2305 +
2306 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2307 +               unsigned long count, void *data)
2308 +{
2309 +       char str[32];
2310 +       long value;
2311 +
2312 +       if (count >= sizeof(str)) {
2313 +               printk(KERN_ERR "EXT3: %s string to long, max %u bytes\n",
2314 +                      EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2315 +               return -EOVERFLOW;
2316 +       }
2317 +
2318 +       if (copy_from_user(str, buffer, count))
2319 +               return -EFAULT;
2320 +
2321 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2322 +       value = simple_strtol(str, NULL, 0);
2323 +       if (value <= 0)
2324 +               return -ERANGE;
2325 +
2326 +       ext3_mb_max_to_scan = value;    
2327 +
2328 +       return count;
2329 +}
2330 +
2331 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2332 +               int count, int *eof, void *data)
2333 +{
2334 +       int len;
2335 +
2336 +       *eof = 1;
2337 +       if (off != 0)
2338 +               return 0;
2339 +
2340 +       len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2341 +       *start = page;
2342 +       return len;
2343 +}
2344 +
2345 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2346 +               unsigned long count, void *data)
2347 +{
2348 +       char str[32];
2349 +       long value;
2350 +
2351 +       if (count >= sizeof(str)) {
2352 +               printk(KERN_ERR "EXT3: %s string to long, max %u bytes\n",
2353 +                      EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2354 +               return -EOVERFLOW;
2355 +       }
2356 +
2357 +       if (copy_from_user(str, buffer, count))
2358 +               return -EFAULT;
2359 +
2360 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2361 +       value = simple_strtol(str, NULL, 0);
2362 +       if (value <= 0)
2363 +               return -ERANGE;
2364 +
2365 +       ext3_mb_min_to_scan = value;    
2366 +
2367 +       return count;
2368 +}
2369 +
2370 +int __init init_ext3_proc(void)
2371 +{
2372 +       struct proc_dir_entry *proc_ext3_mb_stats;
2373 +       struct proc_dir_entry *proc_ext3_mb_max_to_scan;
2374 +       struct proc_dir_entry *proc_ext3_mb_min_to_scan;
2375 +
2376 +       proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
2377 +       if (proc_root_ext3 == NULL) {
2378 +               printk(KERN_ERR "EXT3: Unable to create %s\n", EXT3_ROOT);
2379 +               return -EIO;
2380 +       }
2381 +
2382 +       /* Initialize EXT3_MB_STATS_NAME */
2383 +       proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
2384 +                       S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
2385 +       if (proc_ext3_mb_stats == NULL) {
2386 +               printk(KERN_ERR "EXT3: Unable to create %s\n",
2387 +                               EXT3_MB_STATS_NAME);
2388 +               remove_proc_entry(EXT3_ROOT, proc_root_fs);
2389 +               return -EIO;
2390 +       }
2391 +
2392 +       proc_ext3_mb_stats->data = NULL;
2393 +       proc_ext3_mb_stats->read_proc  = ext3_mb_stats_read;
2394 +       proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
2395 +
2396 +       /* Initialize EXT3_MAX_TO_SCAN_NAME */
2397 +       proc_ext3_mb_max_to_scan = create_proc_entry(
2398 +                       EXT3_MB_MAX_TO_SCAN_NAME,
2399 +                       S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
2400 +       if (proc_ext3_mb_max_to_scan == NULL) {
2401 +               printk(KERN_ERR "EXT3: Unable to create %s\n",
2402 +                               EXT3_MB_MAX_TO_SCAN_NAME);
2403 +               remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
2404 +               remove_proc_entry(EXT3_ROOT, proc_root_fs);
2405 +               return -EIO;
2406 +       }
2407 +
2408 +       proc_ext3_mb_max_to_scan->data = NULL;
2409 +       proc_ext3_mb_max_to_scan->read_proc  = ext3_mb_max_to_scan_read;
2410 +       proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
2411 +
2412 +       /* Initialize EXT3_MIN_TO_SCAN_NAME */
2413 +       proc_ext3_mb_min_to_scan = create_proc_entry(
2414 +                       EXT3_MB_MIN_TO_SCAN_NAME,
2415 +                       S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
2416 +       if (proc_ext3_mb_min_to_scan == NULL) {
2417 +               printk(KERN_ERR "EXT3: Unable to create %s\n",
2418 +                               EXT3_MB_MIN_TO_SCAN_NAME);
2419 +               remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
2420 +               remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
2421 +               remove_proc_entry(EXT3_ROOT, proc_root_fs);
2422 +               return -EIO;
2423 +       }
2424 +
2425 +       proc_ext3_mb_min_to_scan->data = NULL;
2426 +       proc_ext3_mb_min_to_scan->read_proc  = ext3_mb_min_to_scan_read;
2427 +       proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
2428 +
2429 +       return 0;
2430 +}
2431 +
2432 +void exit_ext3_proc(void)
2433 +{
2434 +       remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
2435 +       remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
2436 +       remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
2437 +       remove_proc_entry(EXT3_ROOT, proc_root_fs);
2438 +}
2439 +
2440 Index: linux-stage/fs/ext3/extents.c
2441 ===================================================================
2442 --- linux-stage.orig/fs/ext3/extents.c  2006-07-16 02:29:43.000000000 +0800
2443 +++ linux-stage/fs/ext3/extents.c       2006-07-16 02:29:49.000000000 +0800
2444 @@ -771,7 +771,7 @@ cleanup:
2445                 for (i = 0; i < depth; i++) {
2446                         if (!ablocks[i])
2447                                 continue;
2448 -                       ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
2449 +                       ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
2450                 }
2451         }
2452         kfree(ablocks);
2453 @@ -1428,7 +1428,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
2454                   path->p_idx->ei_leaf);
2455         bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
2456         ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
2457 -       ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
2458 +       ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
2459         return err;
2460  }
2461  
2462 @@ -1913,10 +1913,12 @@ ext3_remove_blocks(struct ext3_extents_t
2463         int needed = ext3_remove_blocks_credits(tree, ex, from, to);
2464         handle_t *handle = ext3_journal_start(tree->inode, needed);
2465         struct buffer_head *bh;
2466 -       int i;
2467 +       int i, metadata = 0;
2468  
2469         if (IS_ERR(handle))
2470                 return PTR_ERR(handle);
2471 +       if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
2472 +               metadata = 1;
2473         if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
2474                 /* tail removal */
2475                 unsigned long num, start;
2476 @@ -1928,7 +1930,7 @@ ext3_remove_blocks(struct ext3_extents_t
2477                         bh = sb_find_get_block(tree->inode->i_sb, start + i);
2478                         ext3_forget(handle, 0, tree->inode, bh, start + i);
2479                 }
2480 -               ext3_free_blocks(handle, tree->inode, start, num);
2481 +               ext3_free_blocks(handle, tree->inode, start, num, metadata);
2482         } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
2483                 printk("strange request: removal %lu-%lu from %u:%u\n",
2484                        from, to, ex->ee_block, ex->ee_len);
2485 Index: linux-stage/fs/ext3/xattr.c
2486 ===================================================================
2487 --- linux-stage.orig/fs/ext3/xattr.c    2006-07-16 02:29:43.000000000 +0800
2488 +++ linux-stage/fs/ext3/xattr.c 2006-07-16 02:29:49.000000000 +0800
2489 @@ -484,7 +484,7 @@ ext3_xattr_release_block(handle_t *handl
2490                 ea_bdebug(bh, "refcount now=0; freeing");
2491                 if (ce)
2492                         mb_cache_entry_free(ce);
2493 -               ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
2494 +               ext3_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
2495                 get_bh(bh);
2496                 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
2497         } else {
2498 @@ -805,7 +805,7 @@ inserted:
2499                         new_bh = sb_getblk(sb, block);
2500                         if (!new_bh) {
2501  getblk_failed:
2502 -                               ext3_free_blocks(handle, inode, block, 1);
2503 +                               ext3_free_blocks(handle, inode, block, 1, 1);
2504                                 error = -EIO;
2505                                 goto cleanup;
2506                         }
2507 Index: linux-stage/fs/ext3/balloc.c
2508 ===================================================================
2509 --- linux-stage.orig/fs/ext3/balloc.c   2006-07-16 02:29:43.000000000 +0800
2510 +++ linux-stage/fs/ext3/balloc.c        2006-07-16 02:33:13.000000000 +0800
2511 @@ -79,7 +79,7 @@ struct ext3_group_desc * ext3_get_group_
2512   *
2513   * Return buffer_head on success or NULL in case of failure.
2514   */
2515 -static struct buffer_head *
2516 +struct buffer_head *
2517  read_block_bitmap(struct super_block *sb, unsigned int block_group)
2518  {
2519         struct ext3_group_desc * desc;
2520 @@ -490,24 +490,6 @@ error_return:
2521         return;
2522  }
2523  
2524 -/* Free given blocks, update quota and i_blocks field */
2525 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
2526 -                       ext3_fsblk_t block, unsigned long count)
2527 -{
2528 -       struct super_block * sb;
2529 -       unsigned long dquot_freed_blocks;
2530 -
2531 -       sb = inode->i_sb;
2532 -       if (!sb) {
2533 -               printk ("ext3_free_blocks: nonexistent device");
2534 -               return;
2535 -       }
2536 -       ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
2537 -       if (dquot_freed_blocks)
2538 -               DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
2539 -       return;
2540 -}
2541 -
2542  /*
2543   * For ext3 allocations, we must not reuse any blocks which are
2544   * allocated in the bitmap buffer's "last committed data" copy.  This
2545 @@ -1463,7 +1445,7 @@ out:
2546         return 0;
2547  }
2548  
2549 -ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
2550 +ext3_fsblk_t ext3_new_block_old(handle_t *handle, struct inode *inode,
2551                         ext3_fsblk_t goal, int *errp)
2552  {
2553         unsigned long count = 1;
2554 Index: linux-stage/fs/ext3/super.c
2555 ===================================================================
2556 --- linux-stage.orig/fs/ext3/super.c    2006-07-16 02:29:43.000000000 +0800
2557 +++ linux-stage/fs/ext3/super.c 2006-07-16 02:29:49.000000000 +0800
2558 @@ -391,6 +391,7 @@ static void ext3_put_super (struct super
2559         struct ext3_super_block *es = sbi->s_es;
2560         int i;
2561  
2562 +       ext3_mb_release(sb);
2563         ext3_ext_release(sb);
2564         ext3_xattr_put_super(sb);
2565         journal_destroy(sbi->s_journal);
2566 @@ -641,7 +642,7 @@ enum {
2567         Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
2568         Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
2569         Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2570 -       Opt_extents, Opt_extdebug,
2571 +       Opt_extents, Opt_extdebug, Opt_mballoc,
2572         Opt_grpquota
2573  };
2574  
2575 @@ -696,6 +697,7 @@ static match_table_t tokens = {
2576         {Opt_iopen_nopriv, "iopen_nopriv"},
2577         {Opt_extents, "extents"},
2578         {Opt_extdebug, "extdebug"},
2579 +       {Opt_mballoc, "mballoc"},
2580         {Opt_barrier, "barrier=%u"},
2581         {Opt_err, NULL},
2582         {Opt_resize, "resize"},
2583 @@ -1047,6 +1049,9 @@ clear_qf_name:
2584                 case Opt_extdebug:
2585                         set_opt (sbi->s_mount_opt, EXTDEBUG);
2586                         break;
2587 +               case Opt_mballoc:
2588 +                       set_opt (sbi->s_mount_opt, MBALLOC);
2589 +                       break;
2590                 default:
2591                         printk (KERN_ERR
2592                                 "EXT3-fs: Unrecognized mount option \"%s\" "
2593 @@ -1773,6 +1778,7 @@ static int ext3_fill_super (struct super
2594                 "writeback");
2595  
2596         ext3_ext_init(sb);
2597 +       ext3_mb_init(sb, needs_recovery);
2598         lock_kernel();
2599         return 0;
2600  
2601 @@ -2712,7 +2718,13 @@ static struct file_system_type ext3_fs_t
2602  
2603  static int __init init_ext3_fs(void)
2604  {
2605 -       int err = init_ext3_xattr();
2606 +       int err;
2607 +
2608 +       err = init_ext3_proc();
2609 +       if (err)
2610 +               return err;
2611 +
2612 +       err = init_ext3_xattr();
2613         if (err)
2614                 return err;
2615         err = init_inodecache();
2616 @@ -2734,6 +2746,7 @@ static void __exit exit_ext3_fs(void)
2617         unregister_filesystem(&ext3_fs_type);
2618         destroy_inodecache();
2619         exit_ext3_xattr();
2620 +       exit_ext3_proc();
2621  }
2622  
2623  int ext3_prep_san_write(struct inode *inode, long *blocks,
2624 Index: linux-stage/fs/ext3/Makefile
2625 ===================================================================
2626 --- linux-stage.orig/fs/ext3/Makefile   2006-07-16 02:29:43.000000000 +0800
2627 +++ linux-stage/fs/ext3/Makefile        2006-07-16 02:29:49.000000000 +0800
2628 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
2629  
2630  ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2631            ioctl.o namei.o super.o symlink.o hash.o resize.o \
2632 -          extents.o
2633 +          extents.o mballoc.o
2634  
2635  ext3-$(CONFIG_EXT3_FS_XATTR)    += xattr.o xattr_user.o xattr_trusted.o
2636  ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2637 Index: linux-stage/include/linux/ext3_fs.h
2638 ===================================================================
2639 --- linux-stage.orig/include/linux/ext3_fs.h    2006-07-16 02:29:43.000000000 +0800
2640 +++ linux-stage/include/linux/ext3_fs.h 2006-07-16 02:29:49.000000000 +0800
2641 @@ -53,6 +53,14 @@
2642  #define ext3_debug(f, a...)    do {} while (0)
2643  #endif
2644  
2645 +#define EXT3_MULTIBLOCK_ALLOCATOR      1
2646 +
2647 +#define EXT3_MB_HINT_MERGE             1
2648 +#define EXT3_MB_HINT_RESERVED          2
2649 +#define EXT3_MB_HINT_METADATA          4
2650 +#define EXT3_MB_HINT_FIRST             8
2651 +#define EXT3_MB_HINT_BEST              16
2652 +
2653  /*
2654   * Special inodes numbers
2655   */
2656 @@ -379,6 +387,7 @@ struct ext3_inode {
2657  #define EXT3_MOUNT_IOPEN_NOPRIV                0x800000/* Make iopen world-readable */
2658  #define EXT3_MOUNT_EXTENTS             0x1000000/* Extents support */
2659  #define EXT3_MOUNT_EXTDEBUG            0x2000000/* Extents debug */
2660 +#define EXT3_MOUNT_MBALLOC             0x4000000/* Buddy allocation support */
2661  
2662  /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2663  #ifndef clear_opt
2664 @@ -749,12 +758,12 @@ ext3_group_first_block_no(struct super_b
2665  /* balloc.c */
2666  extern int ext3_bg_has_super(struct super_block *sb, int group);
2667  extern unsigned long ext3_bg_num_gdb(struct super_block *sb, int group);
2668 -extern ext3_fsblk_t ext3_new_block (handle_t *handle, struct inode *inode,
2669 -                       ext3_fsblk_t goal, int *errp);
2670 +//extern ext3_fsblk_t ext3_new_block (handle_t *handle, struct inode *inode,
2671 +//                     ext3_fsblk_t goal, int *errp);
2672  extern ext3_fsblk_t ext3_new_blocks (handle_t *handle, struct inode *inode,
2673                         ext3_fsblk_t goal, unsigned long *count, int *errp);
2674  extern void ext3_free_blocks (handle_t *handle, struct inode *inode,
2675 -                       ext3_fsblk_t block, unsigned long count);
2676 +                       ext3_fsblk_t block, unsigned long count, int metadata);
2677  extern void ext3_free_blocks_sb (handle_t *handle, struct super_block *sb,
2678                                  ext3_fsblk_t block, unsigned long count,
2679                                 unsigned long *pdquot_freed_blocks);
2680 @@ -881,6 +890,17 @@ extern void ext3_extents_initialize_bloc
2681  extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
2682                           unsigned int cmd, unsigned long arg);
2683  
2684 +/* mballoc.c */
2685 +extern long ext3_mb_stats;
2686 +extern long ext3_mb_max_to_scan;
2687 +extern int ext3_mb_init(struct super_block *, int);
2688 +extern int ext3_mb_release(struct super_block *);
2689 +extern int ext3_mb_new_blocks(handle_t *, struct inode *, unsigned long, int *, int, int *);
2690 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
2691 +extern void ext3_mb_release_blocks(struct super_block *, int);
2692 +int __init init_ext3_proc(void);
2693 +void exit_ext3_proc(void);
2694 +
2695  #endif /* __KERNEL__ */
2696  
2697  /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
2698 Index: linux-stage/include/linux/ext3_fs_sb.h
2699 ===================================================================
2700 --- linux-stage.orig/include/linux/ext3_fs_sb.h 2006-07-16 02:29:43.000000000 +0800
2701 +++ linux-stage/include/linux/ext3_fs_sb.h      2006-07-16 02:29:49.000000000 +0800
2702 @@ -21,8 +21,14 @@
2703  #include <linux/wait.h>
2704  #include <linux/blockgroup_lock.h>
2705  #include <linux/percpu_counter.h>
2706 +#include <linux/list.h>
2707  #endif
2708  #include <linux/rbtree.h>
2709 +#include <linux/proc_fs.h>
2710 +
2711 +struct ext3_buddy_group_blocks;
2712 +struct ext3_mb_history;
2713 +#define EXT3_BB_MAX_BLOCKS
2714  
2715  /*
2716   * third extended-fs super-block data in memory
2717 @@ -78,6 +84,38 @@ struct ext3_sb_info {
2718         char *s_qf_names[MAXQUOTAS];            /* Names of quota files with journalled quota */
2719         int s_jquota_fmt;                       /* Format of quota to use */
2720  #endif
2721 +
2722 +       /* for buddy allocator */
2723 +       struct ext3_group_info **s_group_info;
2724 +       struct inode *s_buddy_cache;
2725 +       long s_blocks_reserved;
2726 +       spinlock_t s_reserve_lock;
2727 +       struct list_head s_active_transaction;
2728 +       struct list_head s_closed_transaction;
2729 +       struct list_head s_committed_transaction;
2730 +       spinlock_t s_md_lock;
2731 +       tid_t s_last_transaction;
2732 +       int s_mb_factor;
2733 +       unsigned short *s_mb_offsets, *s_mb_maxs;
2734 +
2735 +       /* history to debug policy */
2736 +       struct ext3_mb_history *s_mb_history;
2737 +       int s_mb_history_cur;
2738 +       int s_mb_history_max;
2739 +       struct proc_dir_entry *s_mb_proc;
2740 +       spinlock_t s_mb_history_lock;
2741 +
2742 +       /* stats for buddy allocator */
2743 +       atomic_t s_bal_reqs;    /* number of reqs with len > 1 */
2744 +       atomic_t s_bal_success; /* we found long enough chunks */
2745 +       atomic_t s_bal_allocated;       /* in blocks */
2746 +       atomic_t s_bal_ex_scanned;      /* total extents scanned */
2747 +       atomic_t s_bal_goals;   /* goal hits */
2748 +       atomic_t s_bal_breaks;  /* too long searches */
2749 +       atomic_t s_bal_2orders; /* 2^order hits */
2750 +       spinlock_t s_bal_lock;
2751 +       unsigned long s_mb_buddies_generated;
2752 +       unsigned long long s_mb_generation_time;
2753  };
2754  
2755  #endif /* _LINUX_EXT3_FS_SB */
2756 Index: linux-stage/fs/ext3/inode.c
2757 ===================================================================
2758 --- linux-stage.orig/fs/ext3/inode.c    2006-07-16 02:29:43.000000000 +0800
2759 +++ linux-stage/fs/ext3/inode.c 2006-07-16 02:29:49.000000000 +0800
2760 @@ -562,7 +562,7 @@ static int ext3_alloc_blocks(handle_t *h
2761         return ret;
2762  failed_out:
2763         for (i = 0; i <index; i++)
2764 -               ext3_free_blocks(handle, inode, new_blocks[i], 1);
2765 +               ext3_free_blocks(handle, inode, new_blocks[i], 1, 1);
2766         return ret;
2767  }
2768  
2769 @@ -661,9 +661,9 @@ failed:
2770                 ext3_journal_forget(handle, branch[i].bh);
2771         }
2772         for (i = 0; i <indirect_blks; i++)
2773 -               ext3_free_blocks(handle, inode, new_blocks[i], 1);
2774 +               ext3_free_blocks(handle, inode, new_blocks[i], 1, 1);
2775  
2776 -       ext3_free_blocks(handle, inode, new_blocks[i], num);
2777 +       ext3_free_blocks(handle, inode, new_blocks[i], num, 1);
2778  
2779         return err;
2780  }
2781 @@ -760,9 +760,9 @@ err_out:
2782         for (i = 1; i <= num; i++) {
2783                 BUFFER_TRACE(where[i].bh, "call journal_forget");
2784                 ext3_journal_forget(handle, where[i].bh);
2785 -               ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
2786 +               ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1,1);
2787         }
2788 -       ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
2789 +       ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 1);
2790  
2791         return err;
2792  }
2793 @@ -2007,7 +2007,7 @@ static void ext3_clear_blocks(handle_t *
2794                 }
2795         }
2796  
2797 -       ext3_free_blocks(handle, inode, block_to_free, count);
2798 +       ext3_free_blocks(handle, inode, block_to_free, count, 1);
2799  }
2800  
2801  /**
2802 @@ -2180,7 +2180,7 @@ static void ext3_free_branches(handle_t 
2803                                 ext3_journal_test_restart(handle, inode);
2804                         }
2805  
2806 -                       ext3_free_blocks(handle, inode, nr, 1);
2807 +                       ext3_free_blocks(handle, inode, nr, 1, 1);
2808  
2809                         if (parent_bh) {
2810                                 /*