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