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