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