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