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