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