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