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