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