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