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