Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-mballoc3-core.patch
1 Index: linux-2.6.9-full/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.9-full.orig/include/linux/ext3_fs.h       2007-06-08 23:44:08.000000000 +0400
4 +++ linux-2.6.9-full/include/linux/ext3_fs.h    2007-10-17 22:25:01.000000000 +0400
5 @@ -57,6 +57,30 @@ struct statfs;
6  #define ext3_debug(f, a...)    do {} while (0)
7  #endif
8  
9 +#define EXT3_MULTIBLOCK_ALLOCATOR      1
10 +
11 +#define EXT3_MB_HINT_MERGE             1       /* prefer goal again. length */
12 +#define EXT3_MB_HINT_RESERVED          2       /* blocks already reserved */
13 +#define EXT3_MB_HINT_METADATA          4       /* metadata is being allocated */
14 +#define EXT3_MB_HINT_FIRST             8       /* first blocks in the file */
15 +#define EXT3_MB_HINT_BEST              16      /* search for the best chunk */
16 +#define EXT3_MB_HINT_DATA              32      /* data is being allocated */
17 +#define EXT3_MB_HINT_NOPREALLOC                64      /* don't preallocate (for tails) */
18 +#define EXT3_MB_HINT_GROUP_ALLOC       128     /* allocate for locality group */
19 +#define EXT3_MB_HINT_GOAL_ONLY         256     /* allocate goal blocks or none */
20 +
21 +struct ext3_allocation_request {
22 +       struct inode *inode;    /* target inode for block we're allocating */
23 +       unsigned long logical;  /* logical block in target inode */
24 +       unsigned long goal;     /* phys. target (a hint) */
25 +       unsigned long lleft;    /* the closest logical allocated block to the left */
26 +       unsigned long pleft;    /* phys. block for ^^^ */
27 +       unsigned long lright;   /* the closest logical allocated block to the right */
28 +       unsigned long pright;   /* phys. block for ^^^ */
29 +       unsigned long len;      /* how many blocks we want to allocate */
30 +       unsigned long flags;    /* flags. see above EXT3_MB_HINT_* */
31 +};
32 +
33  /*
34   * Special inodes numbers
35   */
36 @@ -387,6 +411,14 @@ struct ext3_inode {
37  #define ext3_find_first_zero_bit       ext2_find_first_zero_bit
38  #define ext3_find_next_zero_bit                ext2_find_next_zero_bit
39  
40 +#ifndef ext2_find_next_le_bit
41 +#ifdef __LITTLE_ENDIAN
42 +#define ext2_find_next_le_bit(addr, size, off) find_next_bit((addr), (size), (off))
43 +#else
44 +#error "mballoc needs a patch for big-endian systems - CFS bug 10634"
45 +#endif /* __LITTLE_ENDIAN */
46 +#endif /* !ext2_find_next_le_bit */
47 +
48  /*
49   * Maximal mount counts between two filesystem checks
50   */
51 @@ -763,6 +795,20 @@ extern unsigned long ext3_count_dirs (st
52  extern void ext3_check_inodes_bitmap (struct super_block *);
53  extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
54  
55 +/* mballoc.c */
56 +extern long ext3_mb_stats;
57 +extern long ext3_mb_max_to_scan;
58 +extern int ext3_mb_init(struct super_block *, int);
59 +extern int ext3_mb_release(struct super_block *);
60 +extern unsigned long ext3_mb_new_blocks(handle_t *, struct ext3_allocation_request *, int *);
61 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
62 +extern void ext3_mb_release_blocks(struct super_block *, int);
63 +extern void ext3_mb_release_blocks(struct super_block *, int);
64 +extern void ext3_mb_discard_inode_preallocations(struct inode *);
65 +extern int __init init_ext3_mb_proc(void);
66 +extern void exit_ext3_mb_proc(void);
67 +extern void ext3_mb_free_blocks(handle_t *, struct inode *, unsigned long, unsigned long, int, int *);
68 +
69  
70  /* inode.c */
71  extern int ext3_block_truncate_page(handle_t *, struct page *,
72 @@ -804,6 +850,10 @@ extern int ext3_group_extend(struct supe
73                                 unsigned long n_blocks_count);
74  
75  /* super.c */
76 +extern struct proc_dir_entry *proc_root_ext3;
77 +extern int __init init_ext3_proc(void);
78 +extern void exit_ext3_proc(void);
79 +
80  extern void ext3_error (struct super_block *, const char *, const char *, ...)
81         __attribute__ ((format (printf, 3, 4)));
82  extern void __ext3_std_error (struct super_block *, const char *, int);
83 Index: linux-2.6.9-full/include/linux/ext3_fs_sb.h
84 ===================================================================
85 --- linux-2.6.9-full.orig/include/linux/ext3_fs_sb.h    2007-06-08 23:44:07.000000000 +0400
86 +++ linux-2.6.9-full/include/linux/ext3_fs_sb.h 2007-10-17 22:25:01.000000000 +0400
87 @@ -81,6 +81,61 @@ struct ext3_sb_info {
88         char *s_qf_names[MAXQUOTAS];            /* Names of quota files with journalled quota */
89         int s_jquota_fmt;                       /* Format of quota to use */
90  #endif
91 +
92 +       /* for buddy allocator */
93 +       struct ext3_group_info ***s_group_info;
94 +       struct inode *s_buddy_cache;
95 +       long s_blocks_reserved;
96 +       spinlock_t s_reserve_lock;
97 +       struct list_head s_active_transaction;
98 +       struct list_head s_closed_transaction;
99 +       struct list_head s_committed_transaction;
100 +       spinlock_t s_md_lock;
101 +       tid_t s_last_transaction;
102 +       unsigned short *s_mb_offsets, *s_mb_maxs;
103 +
104 +       /* tunables */
105 +       unsigned long s_mb_factor;
106 +       unsigned long s_stripe;
107 +       unsigned long s_mb_stream_request;
108 +       unsigned long s_mb_max_to_scan;
109 +       unsigned long s_mb_min_to_scan;
110 +       unsigned long s_mb_max_groups_to_scan;
111 +       unsigned long s_mb_stats;
112 +       unsigned long s_mb_order2_reqs;
113 +
114 +       /* history to debug policy */
115 +       struct ext3_mb_history *s_mb_history;
116 +       int s_mb_history_cur;
117 +       int s_mb_history_max;
118 +       int s_mb_history_num;
119 +       struct proc_dir_entry *s_dev_proc;
120 +       spinlock_t s_mb_history_lock;
121 +       int s_mb_history_filter;
122 +
123 +       /* stats for buddy allocator */
124 +       spinlock_t s_mb_pa_lock;
125 +       atomic_t s_bal_reqs;    /* number of reqs with len > 1 */
126 +       atomic_t s_bal_success; /* we found long enough chunks */
127 +       atomic_t s_bal_allocated;       /* in blocks */
128 +       atomic_t s_bal_ex_scanned;      /* total extents scanned */
129 +       atomic_t s_bal_goals;   /* goal hits */
130 +       atomic_t s_bal_breaks;  /* too long searches */
131 +       atomic_t s_bal_2orders; /* 2^order hits */
132 +       spinlock_t s_bal_lock;
133 +       unsigned long s_mb_buddies_generated;
134 +       unsigned long long s_mb_generation_time;
135 +       atomic_t s_mb_lost_chunks;
136 +       atomic_t s_mb_preallocated;
137 +       atomic_t s_mb_discarded;
138 +
139 +       /* locality groups */
140 +       struct ext3_locality_group *s_locality_groups;
141 +
142  };
143  
144 +#define EXT3_GROUP_INFO(sb, group)                                        \
145 +       EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
146 +                                [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
147 +
148  #endif /* _LINUX_EXT3_FS_SB */
149 Index: linux-2.6.9-full/fs/ext3/super.c
150 ===================================================================
151 --- linux-2.6.9-full.orig/fs/ext3/super.c       2007-06-08 23:44:08.000000000 +0400
152 +++ linux-2.6.9-full/fs/ext3/super.c    2007-10-17 22:26:27.000000000 +0400
153 @@ -394,6 +394,7 @@ void ext3_put_super (struct super_block 
154         struct ext3_super_block *es = sbi->s_es;
155         int i;
156  
157 +       ext3_mb_release(sb);
158         ext3_ext_release(sb);
159         ext3_xattr_put_super(sb);
160         journal_destroy(sbi->s_journal);
161 @@ -438,6 +439,8 @@ void ext3_put_super (struct super_block 
162                 invalidate_bdev(sbi->journal_bdev, 0);
163                 ext3_blkdev_remove(sbi);
164         }
165 +       remove_proc_entry(sb->s_id, proc_root_ext3);
166 +       sbi->s_dev_proc = NULL;
167         sb->s_fs_info = NULL;
168         kfree(sbi);
169         return;
170 @@ -463,6 +466,8 @@ static struct inode *ext3_alloc_inode(st
171         ei->vfs_inode.i_version = 1;
172         
173         memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
174 +       INIT_LIST_HEAD(&ei->i_prealloc_list);
175 +       spin_lock_init(&ei->i_prealloc_lock);
176         return &ei->vfs_inode;
177  }
178  
179 @@ -1353,6 +1358,13 @@ static int ext3_fill_super (struct super
180         sbi->s_mount_opt = 0;
181         sbi->s_resuid = EXT3_DEF_RESUID;
182         sbi->s_resgid = EXT3_DEF_RESGID;
183 +       sbi->s_dev_proc = proc_mkdir(sb->s_id, proc_root_ext3);
184 +       if (sbi->s_dev_proc == NULL) {
185 +               printk(KERN_ERR "EXT3-fs: Unable to create %s\n", sb->s_id);
186 +               sb->s_fs_info = NULL;
187 +               kfree(sbi);
188 +               return -ENOMEM;
189 +       }
190  
191         blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
192         if (!blocksize) {
193 @@ -1729,6 +1741,8 @@ failed_mount:
194         ext3_blkdev_remove(sbi);
195         brelse(bh);
196  out_fail:
197 +       remove_proc_entry(sb->s_id, proc_root_ext3);
198 +       sbi->s_dev_proc = NULL;
199         sb->s_fs_info = NULL;
200         kfree(sbi);
201         return -EINVAL;
202 @@ -2593,9 +2607,47 @@ static struct file_system_type ext3_fs_t
203         .fs_flags       = FS_REQUIRES_DEV,
204  };
205  
206 +#define EXT3_ROOT "ext3"
207 +struct proc_dir_entry *proc_root_ext3;
208 +
209 +int __init init_ext3_proc(void)
210 +{
211 +       struct proc_dir_entry *proc;
212 +       int ret;
213 +
214 +       if ((ret = init_ext3_mb_proc()))
215 +               goto out;
216 +
217 +       proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
218 +       if (proc_root_ext3 == NULL) {
219 +               printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
220 +               ret = -ENOMEM;
221 +               goto out_mb_proc;
222 +       }
223 +
224 +       return 0;
225 +
226 +out_mb_proc:
227 +       exit_ext3_mb_proc();
228 +out:
229 +       return ret;
230 +}
231 +
232 +void exit_ext3_proc(void)
233 +{
234 +       exit_ext3_mb_proc();
235 +       remove_proc_entry(EXT3_ROOT, proc_root_fs);
236 +}
237 +
238  static int __init init_ext3_fs(void)
239  {
240 -       int err = init_ext3_xattr();
241 +       int err;
242 +
243 +       err = init_ext3_proc();
244 +       if (err)
245 +               return err;
246 +
247 +       err = init_ext3_xattr();
248         if (err)
249                 return err;
250         err = init_inodecache();
251 @@ -2617,6 +2669,7 @@ static void __exit exit_ext3_fs(void)
252         unregister_filesystem(&ext3_fs_type);
253         destroy_inodecache();
254         exit_ext3_xattr();
255 +       exit_ext3_proc();
256  }
257  
258  int ext3_map_inode_page(struct inode *inode, struct page *page,
259 Index: linux-2.6.9-full/fs/ext3/mballoc.c
260 ===================================================================
261 --- linux-2.6.9-full.orig/fs/ext3/mballoc.c     2007-10-17 21:59:51.072534980 +0400
262 +++ linux-2.6.9-full/fs/ext3/mballoc.c  2007-10-17 23:09:22.000000000 +0400
263 @@ -0,0 +1,4380 @@
264 +/*
265 + * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
266 + * Written by Alex Tomas <alex@clusterfs.com>
267 + *
268 + * This program is free software; you can redistribute it and/or modify
269 + * it under the terms of the GNU General Public License version 2 as
270 + * published by the Free Software Foundation.
271 + *
272 + * This program is distributed in the hope that it will be useful,
273 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
274 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
275 + * GNU General Public License for more details.
276 + *
277 + * You should have received a copy of the GNU General Public Licens
278 + * along with this program; if not, write to the Free Software
279 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
280 + */
281 +
282 +
283 +/*
284 + * mballoc.c contains the multiblocks allocation routines
285 + */
286 +
287 +#include <linux/time.h>
288 +#include <linux/fs.h>
289 +#include <linux/namei.h>
290 +#include <linux/ext3_jbd.h>
291 +#include <linux/jbd.h>
292 +#include <linux/ext3_fs.h>
293 +#include <linux/quotaops.h>
294 +#include <linux/buffer_head.h>
295 +#include <linux/module.h>
296 +#include <linux/swap.h>
297 +#include <linux/proc_fs.h>
298 +#include <linux/pagemap.h>
299 +#include <linux/seq_file.h>
300 +#include <linux/version.h>
301 +
302 +/*
303 + * MUSTDO:
304 + *   - test ext3_ext_search_left() and ext3_ext_search_right()
305 + *   - search for metadata in few groups
306 + *
307 + * TODO v4:
308 + *   - normalization should take into account whether file is still open
309 + *   - discard preallocations if no free space left (policy?)
310 + *   - don't normalize tails
311 + *   - quota
312 + *   - reservation for superuser
313 + *
314 + * TODO v3:
315 + *   - bitmap read-ahead (proposed by Oleg Drokin aka green)
316 + *   - track min/max extents in each group for better group selection
317 + *   - mb_mark_used() may allocate chunk right after splitting buddy
318 + *   - tree of groups sorted by number of free blocks
319 + *   - error handling
320 + */
321 +
322 +/*
323 + * mballoc operates on the following data:
324 + *  - on-disk bitmap
325 + *  - in-core buddy (actually includes buddy and bitmap)
326 + *  - preallocation descriptors (PAs)
327 + *
328 + * there are two types of preallocations:
329 + *  - inode
330 + *    assiged to specific inode and can be used for this inode only.
331 + *    it describes part of inode's space preallocated to specific
332 + *    physical blocks. any block from that preallocated can be used
333 + *    independent. the descriptor just tracks number of blocks left
334 + *    unused. so, before taking some block from descriptor, one must
335 + *    make sure corresponded logical block isn't allocated yet. this
336 + *    also means that freeing any block within descriptor's range
337 + *    must discard all preallocated blocks.
338 + *  - locality group
339 + *    assigned to specific locality group which does not translate to
340 + *    permanent set of inodes: inode can join and leave group. space
341 + *    from this type of preallocation can be used for any inode. thus
342 + *    it's consumed from the beginning to the end.
343 + *
344 + * relation between them can be expressed as:
345 + *    in-core buddy = on-disk bitmap + preallocation descriptors
346 + *
347 + * this mean blocks mballoc considers used are:
348 + *  - allocated blocks (persistent)
349 + *  - preallocated blocks (non-persistent)
350 + *
351 + * consistency in mballoc world means that at any time a block is either
352 + * free or used in ALL structures. notice: "any time" should not be read
353 + * literally -- time is discrete and delimited by locks.
354 + *
355 + *  to keep it simple, we don't use block numbers, instead we count number of
356 + *  blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
357 + *
358 + * all operations can be expressed as:
359 + *  - init buddy:                      buddy = on-disk + PAs
360 + *  - new PA:                          buddy += N; PA = N
361 + *  - use inode PA:                    on-disk += N; PA -= N
362 + *  - discard inode PA                 buddy -= on-disk - PA; PA = 0
363 + *  - use locality group PA            on-disk += N; PA -= N
364 + *  - discard locality group PA                buddy -= PA; PA = 0
365 + *  note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
366 + *        is used in real operation because we can't know actual used
367 + *        bits from PA, only from on-disk bitmap
368 + *
369 + * if we follow this strict logic, then all operations above should be atomic.
370 + * given some of them can block, we'd have to use something like semaphores
371 + * killing performance on high-end SMP hardware. let's try to relax it using
372 + * the following knowledge:
373 + *  1) if buddy is referenced, it's already initialized
374 + *  2) while block is used in buddy and the buddy is referenced,
375 + *     nobody can re-allocate that block
376 + *  3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
377 + *     bit set and PA claims same block, it's OK. IOW, one can set bit in
378 + *     on-disk bitmap if buddy has same bit set or/and PA covers corresponded
379 + *     block
380 + *
381 + * so, now we're building a concurrency table:
382 + *  - init buddy vs.
383 + *    - new PA
384 + *      blocks for PA are allocated in the buddy, buddy must be referenced
385 + *      until PA is linked to allocation group to avoid concurrent buddy init
386 + *    - use inode PA
387 + *      we need to make sure that either on-disk bitmap or PA has uptodate data
388 + *      given (3) we care that PA-=N operation doesn't interfere with init
389 + *    - discard inode PA
390 + *      the simplest way would be to have buddy initialized by the discard
391 + *    - use locality group PA
392 + *      again PA-=N must be serialized with init
393 + *    - discard locality group PA
394 + *      the simplest way would be to have buddy initialized by the discard
395 + *  - new PA vs.
396 + *    - use inode PA
397 + *      i_truncate_mutex serializes them
398 + *    - discard inode PA
399 + *      discard process must wait until PA isn't used by another process
400 + *    - use locality group PA
401 + *      some mutex should serialize them
402 + *    - discard locality group PA
403 + *      discard process must wait until PA isn't used by another process
404 + *  - use inode PA
405 + *    - use inode PA
406 + *      i_truncate_mutex or another mutex should serializes them
407 + *    - discard inode PA
408 + *      discard process must wait until PA isn't used by another process
409 + *    - use locality group PA
410 + *      nothing wrong here -- they're different PAs covering different blocks
411 + *    - discard locality group PA
412 + *      discard process must wait until PA isn't used by another process
413 + *
414 + * now we're ready to make few consequences:
415 + *  - PA is referenced and while it is no discard is possible
416 + *  - PA is referenced until block isn't marked in on-disk bitmap
417 + *  - PA changes only after on-disk bitmap
418 + *  - discard must not compete with init. either init is done before
419 + *    any discard or they're serialized somehow
420 + *  - buddy init as sum of on-disk bitmap and PAs is done atomically
421 + *
422 + * a special case when we've used PA to emptiness. no need to modify buddy
423 + * in this case, but we should care about concurrent init
424 + *
425 + */
426 +
427 + /*
428 + * Logic in few words:
429 + *
430 + *  - allocation:
431 + *    load group
432 + *    find blocks
433 + *    mark bits in on-disk bitmap
434 + *    release group
435 + *
436 + *  - use preallocation:
437 + *    find proper PA (per-inode or group)
438 + *    load group
439 + *    mark bits in on-disk bitmap
440 + *    release group
441 + *    release PA
442 + *
443 + *  - free:
444 + *    load group
445 + *    mark bits in on-disk bitmap
446 + *    release group
447 + *
448 + *  - discard preallocations in group:
449 + *    mark PAs deleted
450 + *    move them onto local list
451 + *    load on-disk bitmap
452 + *    load group
453 + *    remove PA from object (inode or locality group)
454 + *    mark free blocks in-core
455 + *
456 + *  - discard inode's preallocations:
457 + */
458 +
459 +/*
460 + * Locking rules
461 + *
462 + * Locks:
463 + *  - bitlock on a group       (group)
464 + *  - object (inode/locality)  (object)
465 + *  - per-pa lock              (pa)
466 + *
467 + * Paths:
468 + *  - new pa
469 + *    object
470 + *    group
471 + *
472 + *  - find and use pa:
473 + *    pa
474 + *
475 + *  - release consumed pa:
476 + *    pa
477 + *    group
478 + *    object
479 + *
480 + *  - generate in-core bitmap:
481 + *    group
482 + *        pa
483 + *
484 + *  - discard all for given object (inode, locality group):
485 + *    object
486 + *        pa
487 + *    group
488 + *
489 + *  - discard all for given group:
490 + *    group
491 + *        pa
492 + *    group
493 + *        object
494 + *
495 + */
496 +
497 +/*
498 + * with AGGRESSIVE_CHECK allocator runs consistency checks over
499 + * structures. these checks slow things down a lot
500 + */
501 +#define AGGRESSIVE_CHECK__
502 +
503 +/*
504 + * with DOUBLE_CHECK defined mballoc creates persistent in-core
505 + * bitmaps, maintains and uses them to check for double allocations
506 + */
507 +#define DOUBLE_CHECK__
508 +
509 +/*
510 + */
511 +#define MB_DEBUG__
512 +#ifdef MB_DEBUG
513 +#define mb_debug(fmt,a...)     printk(fmt, ##a)
514 +#else
515 +#define mb_debug(fmt,a...)
516 +#endif
517 +
518 +/*
519 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
520 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
521 + */
522 +#define EXT3_MB_HISTORY
523 +#define EXT3_MB_HISTORY_ALLOC          1       /* allocation */
524 +#define EXT3_MB_HISTORY_PREALLOC       2       /* preallocated blocks used */
525 +#define EXT3_MB_HISTORY_DISCARD                4       /* preallocation discarded */
526 +#define EXT3_MB_HISTORY_FREE           8       /* free */
527 +
528 +#define EXT3_MB_HISTORY_DEFAULT                (EXT3_MB_HISTORY_ALLOC | \
529 +                                        EXT3_MB_HISTORY_PREALLOC | \
530 +                                        EXT3_MB_HISTORY_DISCARD | \
531 +                                        EXT3_MB_HISTORY_FREE)
532 +
533 +/*
534 + * How long mballoc can look for a best extent (in found extents)
535 + */
536 +#define MB_DEFAULT_MAX_TO_SCAN         200
537 +
538 +/*
539 + * How long mballoc must look for a best extent
540 + */
541 +#define MB_DEFAULT_MIN_TO_SCAN         10
542 +
543 +/*
544 + * How many groups mballoc will scan looking for the best chunk
545 + */
546 +#define MB_DEFAULT_MAX_GROUPS_TO_SCAN  5
547 +
548 +/*
549 + * with 'ext3_mb_stats' allocator will collect stats that will be
550 + * shown at umount. The collecting costs though!
551 + */
552 +#define MB_DEFAULT_STATS               1
553 +
554 +/*
555 + * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
556 + * by the stream allocator, which purpose is to pack requests
557 + * as close each to other as possible to produce smooth I/O traffic
558 + */
559 +#define MB_DEFAULT_STREAM_THRESHOLD    16      /* 64K */
560 +
561 +/*
562 + * for which requests use 2^N search using buddies
563 + */
564 +#define MB_DEFAULT_ORDER2_REQS         8
565 +
566 +/*
567 + * default stripe size = 1MB
568 + */
569 +#define MB_DEFAULT_STRIPE              256
570 +
571 +static kmem_cache_t *ext3_pspace_cachep = NULL;
572 +
573 +#ifdef EXT3_BB_MAX_BLOCKS
574 +#undef EXT3_BB_MAX_BLOCKS
575 +#endif
576 +#define EXT3_BB_MAX_BLOCKS     30
577 +
578 +struct ext3_free_metadata {
579 +       unsigned short group;
580 +       unsigned short num;
581 +       unsigned short blocks[EXT3_BB_MAX_BLOCKS];
582 +       struct list_head list;
583 +};
584 +
585 +struct ext3_group_info {
586 +       unsigned long   bb_state;
587 +       unsigned long   bb_tid;
588 +       struct ext3_free_metadata *bb_md_cur;
589 +       unsigned short  bb_first_free;
590 +       unsigned short  bb_free;
591 +       unsigned short  bb_fragments;
592 +       struct          list_head bb_prealloc_list;
593 +#ifdef DOUBLE_CHECK
594 +       void            *bb_bitmap;
595 +#endif
596 +       unsigned short  bb_counters[];
597 +};
598 +
599 +#define EXT3_GROUP_INFO_NEED_INIT_BIT  0
600 +#define EXT3_GROUP_INFO_LOCKED_BIT     1
601 +
602 +#define EXT3_MB_GRP_NEED_INIT(grp)     \
603 +       (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
604 +
605 +
606 +struct ext3_prealloc_space {
607 +       struct list_head        pa_inode_list;
608 +       struct list_head        pa_group_list;
609 +       union {
610 +               struct list_head pa_tmp_list;
611 +               struct rcu_head pa_rcu;
612 +       } u;
613 +       spinlock_t              pa_lock;
614 +       atomic_t                pa_count;
615 +       unsigned                pa_deleted;
616 +       unsigned long           pa_pstart;      /* phys. block */
617 +       unsigned long           pa_lstart;      /* log. block */
618 +       unsigned short          pa_len;         /* len of preallocated chunk */
619 +       unsigned short          pa_free;        /* how many blocks are free */
620 +       unsigned short          pa_linear;      /* consumed in one direction
621 +                                                * strictly, for group prealloc */
622 +       spinlock_t              *pa_obj_lock;
623 +       struct inode            *pa_inode;      /* hack, for history only */
624 +};
625 +
626 +
627 +struct ext3_free_extent {
628 +       unsigned long fe_logical;
629 +       unsigned long fe_start;
630 +       unsigned long fe_group;
631 +       unsigned long fe_len;
632 +};
633 +
634 +/*
635 + * Locality group:
636 + *   we try to group all related changes together
637 + *   so that writeback can flush/allocate them together as well
638 + */
639 +struct ext3_locality_group {
640 +       /* for allocator */
641 +       struct semaphore        lg_sem;         /* to serialize allocates */
642 +       struct list_head        lg_prealloc_list;/* list of preallocations */
643 +       spinlock_t              lg_prealloc_lock;
644 +};
645 +
646 +struct ext3_allocation_context {
647 +       struct inode *ac_inode;
648 +       struct super_block *ac_sb;
649 +
650 +       /* original request */
651 +       struct ext3_free_extent ac_o_ex;
652 +
653 +       /* goal request (after normalization) */
654 +       struct ext3_free_extent ac_g_ex;
655 +
656 +       /* the best found extent */
657 +       struct ext3_free_extent ac_b_ex;
658 +
659 +       /* copy of the bext found extent taken before preallocation efforts */
660 +       struct ext3_free_extent ac_f_ex;
661 +
662 +       /* number of iterations done. we have to track to limit searching */
663 +       unsigned long ac_ex_scanned;
664 +       __u16 ac_groups_scanned;
665 +       __u16 ac_found;
666 +       __u16 ac_tail;
667 +       __u16 ac_buddy;
668 +       __u16 ac_flags;         /* allocation hints */
669 +       __u8 ac_status;
670 +       __u8 ac_criteria;
671 +       __u8 ac_repeats;
672 +       __u8 ac_2order;         /* if request is to allocate 2^N blocks and
673 +                                * N > 0, the field stores N, otherwise 0 */
674 +       __u8 ac_op;             /* operation, for history only */
675 +       struct page *ac_bitmap_page;
676 +       struct page *ac_buddy_page;
677 +       struct ext3_prealloc_space *ac_pa;
678 +       struct ext3_locality_group *ac_lg;
679 +};
680 +
681 +#define AC_STATUS_CONTINUE     1
682 +#define AC_STATUS_FOUND                2
683 +#define AC_STATUS_BREAK                3
684 +
685 +struct ext3_mb_history {
686 +       struct ext3_free_extent orig;   /* orig allocation */
687 +       struct ext3_free_extent goal;   /* goal allocation */
688 +       struct ext3_free_extent result; /* result allocation */
689 +       unsigned pid;
690 +       unsigned ino;
691 +       __u16 found;    /* how many extents have been found */
692 +       __u16 groups;   /* how many groups have been scanned */
693 +       __u16 tail;     /* what tail broke some buddy */
694 +       __u16 buddy;    /* buddy the tail ^^^ broke */
695 +       __u16 flags;
696 +       __u8 cr:3;      /* which phase the result extent was found at */
697 +       __u8 op:4;
698 +       __u8 merged:1;
699 +};
700 +
701 +struct ext3_buddy {
702 +       struct page *bd_buddy_page;
703 +       void *bd_buddy;
704 +       struct page *bd_bitmap_page;
705 +       void *bd_bitmap;
706 +       struct ext3_group_info *bd_info;
707 +       struct super_block *bd_sb;
708 +       __u16 bd_blkbits;
709 +       __u16 bd_group;
710 +};
711 +#define EXT3_MB_BITMAP(e3b)    ((e3b)->bd_bitmap)
712 +#define EXT3_MB_BUDDY(e3b)     ((e3b)->bd_buddy)
713 +
714 +#ifndef EXT3_MB_HISTORY
715 +#define ext3_mb_store_history(ac)
716 +#else
717 +static void ext3_mb_store_history(struct ext3_allocation_context *ac);
718 +#endif
719 +
720 +#define in_range(b, first, len)        ((b) >= (first) && (b) <= (first) + (len) - 1)
721 +
722 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
723 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
724 +unsigned long ext3_new_blocks_old(handle_t *handle, struct inode *inode,
725 +                       unsigned long goal, unsigned long *count, int *errp);
726 +void ext3_mb_release_blocks(struct super_block *, int);
727 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
728 +void ext3_mb_free_committed_blocks(struct super_block *);
729 +void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group);
730 +void ext3_mb_free_consumed_preallocations(struct ext3_allocation_context *ac);
731 +void ext3_mb_return_to_preallocation(struct inode *inode, struct ext3_buddy *e3b,
732 +                                       sector_t block, int count);
733 +void ext3_mb_show_ac(struct ext3_allocation_context *ac);
734 +void ext3_mb_check_with_pa(struct ext3_buddy *e3b, int first, int count);
735 +void ext3_mb_put_pa(struct ext3_allocation_context *, struct super_block *, struct ext3_prealloc_space *pa);
736 +int ext3_mb_init_per_dev_proc(struct super_block *sb);
737 +int ext3_mb_destroy_per_dev_proc(struct super_block *sb);
738 +
739 +/*
740 + * Calculate the block group number and offset, given a block number
741 + */
742 +static void ext3_get_group_no_and_offset(struct super_block *sb,
743 +                                       unsigned long blocknr,
744 +                                       unsigned long *blockgrpp,
745 +                                       unsigned long *offsetp)
746 +{
747 +       struct ext3_super_block *es = EXT3_SB(sb)->s_es;
748 +       unsigned long offset;
749 +
750 +       blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
751 +       offset = blocknr % EXT3_BLOCKS_PER_GROUP(sb);
752 +       blocknr = blocknr / EXT3_BLOCKS_PER_GROUP(sb);
753 +       if (offsetp)
754 +               *offsetp = offset;
755 +       if (blockgrpp)
756 +               *blockgrpp = blocknr;
757 +
758 +}
759 +
760 +static inline void
761 +ext3_lock_group(struct super_block *sb, int group)
762 +{
763 +       bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
764 +                     &EXT3_GROUP_INFO(sb, group)->bb_state);
765 +}
766 +
767 +static inline void
768 +ext3_unlock_group(struct super_block *sb, int group)
769 +{
770 +       bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
771 +                       &EXT3_GROUP_INFO(sb, group)->bb_state);
772 +}
773 +
774 +static inline int
775 +ext3_is_group_locked(struct super_block *sb, int group)
776 +{
777 +       return bit_spin_is_locked(EXT3_GROUP_INFO_LOCKED_BIT,
778 +                                       &EXT3_GROUP_INFO(sb, group)->bb_state);
779 +}
780 +
781 +unsigned long ext3_grp_offs_to_block(struct super_block *sb,
782 +                                       struct ext3_free_extent *fex)
783 +{
784 +       unsigned long block;
785 +
786 +       block = (unsigned long) fex->fe_group * EXT3_BLOCKS_PER_GROUP(sb)
787 +                       + fex->fe_start
788 +                       + le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
789 +       return block;
790 +}
791 +
792 +#if BITS_PER_LONG == 64
793 +#define mb_correct_addr_and_bit(bit,addr)              \
794 +{                                                      \
795 +       bit += ((unsigned long) addr & 7UL) << 3;       \
796 +       addr = (void *) ((unsigned long) addr & ~7UL);  \
797 +}
798 +#elif BITS_PER_LONG == 32
799 +#define mb_correct_addr_and_bit(bit,addr)              \
800 +{                                                      \
801 +       bit += ((unsigned long) addr & 3UL) << 3;       \
802 +       addr = (void *) ((unsigned long) addr & ~3UL);  \
803 +}
804 +#else
805 +#error "how many bits you are?!"
806 +#endif
807 +
808 +static inline int mb_test_bit(int bit, void *addr)
809 +{
810 +       mb_correct_addr_and_bit(bit,addr);
811 +       return ext2_test_bit(bit, addr);
812 +}
813 +
814 +static inline void mb_set_bit(int bit, void *addr)
815 +{
816 +       mb_correct_addr_and_bit(bit,addr);
817 +       ext2_set_bit(bit, addr);
818 +}
819 +
820 +static inline void mb_set_bit_atomic(spinlock_t *lock, int bit, void *addr)
821 +{
822 +       mb_correct_addr_and_bit(bit,addr);
823 +       ext2_set_bit_atomic(lock, bit, addr);
824 +}
825 +
826 +static inline void mb_clear_bit(int bit, void *addr)
827 +{
828 +       mb_correct_addr_and_bit(bit,addr);
829 +       ext2_clear_bit(bit, addr);
830 +}
831 +
832 +static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
833 +{
834 +       mb_correct_addr_and_bit(bit,addr);
835 +       ext2_clear_bit_atomic(lock, bit, addr);
836 +}
837 +
838 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
839 +{
840 +       int fix;
841 +#if BITS_PER_LONG == 64
842 +       fix = ((unsigned long) addr & 7UL) << 3;
843 +       addr = (void *) ((unsigned long) addr & ~7UL);
844 +#elif BITS_PER_LONG == 32
845 +       fix = ((unsigned long) addr & 3UL) << 3;
846 +       addr = (void *) ((unsigned long) addr & ~3UL);
847 +#else
848 +#error "how many bits you are?!"
849 +#endif
850 +       max += fix;
851 +       start += fix;
852 +       return ext2_find_next_zero_bit(addr, max, start) - fix;
853 +}
854 +
855 +static inline int mb_find_next_bit(void *addr, int max, int start)
856 +{
857 +       int fix;
858 +#if BITS_PER_LONG == 64
859 +       fix = ((unsigned long) addr & 7UL) << 3;
860 +       addr = (void *) ((unsigned long) addr & ~7UL);
861 +#elif BITS_PER_LONG == 32
862 +       fix = ((unsigned long) addr & 3UL) << 3;
863 +       addr = (void *) ((unsigned long) addr & ~3UL);
864 +#else
865 +#error "how many bits you are?!"
866 +#endif
867 +       max += fix;
868 +       start += fix;
869 +
870 +#ifdef __BIG_ENDIAN
871 +#else
872 +       return find_next_bit(addr, max, start) - fix;
873 +#endif
874 +}
875 +
876 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
877 +{
878 +       char *bb;
879 +
880 +       BUG_ON(EXT3_MB_BITMAP(e3b) == EXT3_MB_BUDDY(e3b));
881 +       BUG_ON(max == NULL);
882 +
883 +       if (order > e3b->bd_blkbits + 1) {
884 +               *max = 0;
885 +               return NULL;
886 +       }
887 +
888 +       /* at order 0 we see each particular block */
889 +       *max = 1 << (e3b->bd_blkbits + 3);
890 +       if (order == 0)
891 +               return EXT3_MB_BITMAP(e3b);
892 +
893 +       bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
894 +       *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
895 +
896 +       return bb;
897 +}
898 +
899 +#ifdef DOUBLE_CHECK
900 +void mb_free_blocks_double(struct inode *inode, struct ext3_buddy *e3b,
901 +                          int first, int count)
902 +{
903 +       int i;
904 +       struct super_block *sb = e3b->bd_sb;
905 +
906 +       if (unlikely(e3b->bd_info->bb_bitmap == NULL))
907 +               return;
908 +       BUG_ON(!ext3_is_group_locked(sb, e3b->bd_group));
909 +       for (i = 0; i < count; i++) {
910 +               if (!mb_test_bit(first + i, e3b->bd_info->bb_bitmap)) {
911 +                       unsigned long blocknr;
912 +                       blocknr = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb);
913 +                       blocknr += first + i;
914 +                       blocknr +=
915 +                           le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
916 +
917 +                       ext3_error(sb, __FUNCTION__, "double-free of inode"
918 +                                  " %lu's block %lu(bit %u in group %u)\n",
919 +                                  inode ? inode->i_ino : 0, blocknr,
920 +                                  first + i, e3b->bd_group);
921 +               }
922 +               mb_clear_bit(first + i, e3b->bd_info->bb_bitmap);
923 +       }
924 +}
925 +
926 +void mb_mark_used_double(struct ext3_buddy *e3b, int first, int count)
927 +{
928 +       int i;
929 +       if (unlikely(e3b->bd_info->bb_bitmap == NULL))
930 +               return;
931 +       BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
932 +       for (i = 0; i < count; i++) {
933 +               BUG_ON(mb_test_bit(first + i, e3b->bd_info->bb_bitmap));
934 +               mb_set_bit(first + i, e3b->bd_info->bb_bitmap);
935 +       }
936 +}
937 +
938 +void mb_cmp_bitmaps(struct ext3_buddy *e3b, void *bitmap)
939 +{
940 +       if (memcmp(e3b->bd_info->bb_bitmap, bitmap, e3b->bd_sb->s_blocksize)) {
941 +               unsigned char *b1, *b2;
942 +               int i;
943 +               b1 = (unsigned char *) e3b->bd_info->bb_bitmap;
944 +               b2 = (unsigned char *) bitmap;
945 +               for (i = 0; i < e3b->bd_sb->s_blocksize; i++) {
946 +                       if (b1[i] != b2[i]) {
947 +                               printk("corruption in group %u at byte %u(%u): "
948 +                                      "%x in copy != %x on disk/prealloc\n",
949 +                                       e3b->bd_group, i, i * 8, b1[i], b2[i]);
950 +                               BUG();
951 +                       }
952 +               }
953 +       }
954 +}
955 +
956 +#else
957 +#define mb_free_blocks_double(a,b,c,d)
958 +#define mb_mark_used_double(a,b,c)
959 +#define mb_cmp_bitmaps(a,b)
960 +#endif
961 +
962 +#ifdef AGGRESSIVE_CHECK
963 +
964 +#define MB_CHECK_ASSERT(assert)                                                \
965 +do {                                                                   \
966 +       if (!(assert)) {                                                \
967 +               printk (KERN_EMERG                                      \
968 +                       "Assertion failure in %s() at %s:%d: \"%s\"\n", \
969 +                       function, file, line, # assert);                \
970 +               BUG();                                                  \
971 +       }                                                               \
972 +} while (0)
973 +
974 +static int __mb_check_buddy(struct ext3_buddy *e3b, char *file,
975 +                               const char *function, int line)
976 +{
977 +       struct super_block *sb = e3b->bd_sb;
978 +       int order = e3b->bd_blkbits + 1;
979 +       int max, max2, i, j, k, count;
980 +       struct ext3_group_info *grp;
981 +       int fragments = 0, fstart;
982 +       struct list_head *cur;
983 +       void *buddy, *buddy2;
984 +
985 +       if (!test_opt(sb, MBALLOC))
986 +               return 0;
987 +
988 +       {
989 +               static int mb_check_counter = 0;
990 +               if (mb_check_counter++ % 100 != 0)
991 +                       return 0;
992 +       }
993 +
994 +       while (order > 1) {
995 +               buddy = mb_find_buddy(e3b, order, &max);
996 +               MB_CHECK_ASSERT(buddy);
997 +               buddy2 = mb_find_buddy(e3b, order - 1, &max2);
998 +               MB_CHECK_ASSERT(buddy2);
999 +               MB_CHECK_ASSERT(buddy != buddy2);
1000 +               MB_CHECK_ASSERT(max * 2 == max2);
1001 +
1002 +               count = 0;
1003 +               for (i = 0; i < max; i++) {
1004 +
1005 +                       if (mb_test_bit(i, buddy)) {
1006 +                               /* only single bit in buddy2 may be 1 */
1007 +                               if (!mb_test_bit(i << 1, buddy2))
1008 +                                       MB_CHECK_ASSERT(mb_test_bit((i<<1)+1, buddy2));
1009 +                               else if (!mb_test_bit((i << 1) + 1, buddy2))
1010 +                                       MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
1011 +                               continue;
1012 +                       }
1013 +
1014 +                       /* both bits in buddy2 must be 0 */
1015 +                       MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
1016 +                       MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
1017 +
1018 +                       for (j = 0; j < (1 << order); j++) {
1019 +                               k = (i * (1 << order)) + j;
1020 +                               MB_CHECK_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
1021 +                       }
1022 +                       count++;
1023 +               }
1024 +               MB_CHECK_ASSERT(e3b->bd_info->bb_counters[order] == count);
1025 +               order--;
1026 +       }
1027 +
1028 +       fstart = -1;
1029 +       buddy = mb_find_buddy(e3b, 0, &max);
1030 +       for (i = 0; i < max; i++) {
1031 +               if (!mb_test_bit(i, buddy)) {
1032 +                       MB_CHECK_ASSERT(i >= e3b->bd_info->bb_first_free);
1033 +                       if (fstart == -1) {
1034 +                               fragments++;
1035 +                               fstart = i;
1036 +                       }
1037 +                       continue;
1038 +               }
1039 +               fstart = -1;
1040 +               /* check used bits only */
1041 +               for (j = 0; j < e3b->bd_blkbits + 1; j++) {
1042 +                       buddy2 = mb_find_buddy(e3b, j, &max2);
1043 +                       k = i >> j;
1044 +                       MB_CHECK_ASSERT(k < max2);
1045 +                       MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
1046 +               }
1047 +       }
1048 +       MB_CHECK_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
1049 +       MB_CHECK_ASSERT(e3b->bd_info->bb_fragments == fragments);
1050 +
1051 +       grp = EXT3_GROUP_INFO(sb, e3b->bd_group);
1052 +       buddy = mb_find_buddy(e3b, 0, &max);
1053 +       list_for_each(cur, &grp->bb_prealloc_list) {
1054 +               unsigned long groupnr;
1055 +               struct ext3_prealloc_space *pa;
1056 +               pa = list_entry(cur, struct ext3_prealloc_space, group_list);
1057 +               ext3_get_group_no_and_offset(sb, pa->pstart, &groupnr, &k);
1058 +               MB_CHECK_ASSERT(groupnr == e3b->bd_group);
1059 +               for (i = 0; i < pa->len; i++)
1060 +                       MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
1061 +       }
1062 +       return 0;
1063 +}
1064 +#undef MB_CHECK_ASSERT
1065 +#define mb_check_buddy(e3b) __mb_check_buddy(e3b,__FILE__,__FUNCTION__,__LINE__)
1066 +#else
1067 +#define mb_check_buddy(e3b)
1068 +#endif
1069 +
1070 +/* find most significant bit */
1071 +static int inline fmsb(unsigned short word)
1072 +{
1073 +       int order;
1074 +
1075 +       if (word > 255) {
1076 +               order = 7;
1077 +               word >>= 8;
1078 +       } else {
1079 +               order = -1;
1080 +       }
1081 +
1082 +       do {
1083 +               order++;
1084 +               word >>= 1;
1085 +       } while (word != 0);
1086 +
1087 +       return order;
1088 +}
1089 +
1090 +static void inline
1091 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
1092 +                               int len, struct ext3_group_info *grp)
1093 +{
1094 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1095 +       unsigned short min, max, chunk, border;
1096 +
1097 +       BUG_ON(len >= EXT3_BLOCKS_PER_GROUP(sb));
1098 +
1099 +       border = 2 << sb->s_blocksize_bits;
1100 +
1101 +       while (len > 0) {
1102 +               /* find how many blocks can be covered since this position */
1103 +               max = ffs(first | border) - 1;
1104 +
1105 +               /* find how many blocks of power 2 we need to mark */
1106 +               min = fmsb(len);
1107 +
1108 +               if (max < min)
1109 +                       min = max;
1110 +               chunk = 1 << min;
1111 +
1112 +               /* mark multiblock chunks only */
1113 +               grp->bb_counters[min]++;
1114 +               if (min > 0)
1115 +                       mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
1116 +
1117 +               len -= chunk;
1118 +               first += chunk;
1119 +       }
1120 +}
1121 +
1122 +static void
1123 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
1124 +                       int group)
1125 +{
1126 +       struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
1127 +       unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
1128 +       unsigned short i = 0, first, len;
1129 +       unsigned free = 0, fragments = 0;
1130 +       unsigned long long period = get_cycles();
1131 +
1132 +       /* initialize buddy from bitmap which is aggregation
1133 +        * of on-disk bitmap and preallocations */
1134 +       i = mb_find_next_zero_bit(bitmap, max, 0);
1135 +       grp->bb_first_free = i;
1136 +       while (i < max) {
1137 +               fragments++;
1138 +               first = i;
1139 +               i = ext2_find_next_le_bit(bitmap, max, i);
1140 +               len = i - first;
1141 +               free += len;
1142 +               if (len > 1)
1143 +                       ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
1144 +               else
1145 +                       grp->bb_counters[0]++;
1146 +               if (i < max)
1147 +                       i = mb_find_next_zero_bit(bitmap, max, i);
1148 +       }
1149 +       grp->bb_fragments = fragments;
1150 +
1151 +       if (free != grp->bb_free) {
1152 +               printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
1153 +                       group, free, grp->bb_free);
1154 +               grp->bb_free = free;
1155 +       }
1156 +
1157 +       clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
1158 +
1159 +       period = get_cycles() - period;
1160 +       spin_lock(&EXT3_SB(sb)->s_bal_lock);
1161 +       EXT3_SB(sb)->s_mb_buddies_generated++;
1162 +       EXT3_SB(sb)->s_mb_generation_time += period;
1163 +       spin_unlock(&EXT3_SB(sb)->s_bal_lock);
1164 +}
1165 +
1166 +static int ext3_mb_init_cache(struct page *page, char *incore)
1167 +{
1168 +       int blocksize, blocks_per_page, groups_per_page;
1169 +       int err = 0, i, first_group, first_block;
1170 +       struct super_block *sb;
1171 +       struct buffer_head *bhs;
1172 +       struct buffer_head **bh;
1173 +       struct inode *inode;
1174 +       char *data, *bitmap;
1175 +
1176 +       mb_debug("init page %lu\n", page->index);
1177 +
1178 +       inode = page->mapping->host;
1179 +       sb = inode->i_sb;
1180 +       blocksize = 1 << inode->i_blkbits;
1181 +       blocks_per_page = PAGE_CACHE_SIZE / blocksize;
1182 +
1183 +       groups_per_page = blocks_per_page >> 1;
1184 +       if (groups_per_page == 0)
1185 +               groups_per_page = 1;
1186 +
1187 +       /* allocate buffer_heads to read bitmaps */
1188 +       if (groups_per_page > 1) {
1189 +               err = -ENOMEM;
1190 +               i = sizeof(struct buffer_head *) * groups_per_page;
1191 +               bh = kmalloc(i, GFP_NOFS);
1192 +               if (bh == NULL)
1193 +                       goto out;
1194 +               memset(bh, 0, i);
1195 +       } else
1196 +               bh = &bhs;
1197 +
1198 +       first_group = page->index * blocks_per_page / 2;
1199 +
1200 +       /* read all groups the page covers into the cache */
1201 +       for (i = 0; i < groups_per_page; i++) {
1202 +               struct ext3_group_desc * desc;
1203 +
1204 +               if (first_group + i >= EXT3_SB(sb)->s_groups_count)
1205 +                       break;
1206 +
1207 +               err = -EIO;
1208 +               desc = ext3_get_group_desc(sb, first_group + i, NULL);
1209 +               if (desc == NULL)
1210 +                       goto out;
1211 +
1212 +               err = -ENOMEM;
1213 +               bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
1214 +               if (bh[i] == NULL)
1215 +                       goto out;
1216 +
1217 +               if (buffer_uptodate(bh[i]))
1218 +                       continue;
1219 +
1220 +               lock_buffer(bh[i]);
1221 +               if (buffer_uptodate(bh[i])) {
1222 +                       unlock_buffer(bh[i]);
1223 +                       continue;
1224 +               }
1225 +
1226 +               get_bh(bh[i]);
1227 +               bh[i]->b_end_io = end_buffer_read_sync;
1228 +               submit_bh(READ, bh[i]);
1229 +               mb_debug("read bitmap for group %u\n", first_group + i);
1230 +       }
1231 +
1232 +       /* wait for I/O completion */
1233 +       for (i = 0; i < groups_per_page && bh[i]; i++)
1234 +               wait_on_buffer(bh[i]);
1235 +
1236 +       err = -EIO;
1237 +       for (i = 0; i < groups_per_page && bh[i]; i++)
1238 +               if (!buffer_uptodate(bh[i]))
1239 +                       goto out;
1240 +
1241 +       first_block = page->index * blocks_per_page;
1242 +       for (i = 0; i < blocks_per_page; i++) {
1243 +               int group;
1244 +
1245 +               group = (first_block + i) >> 1;
1246 +               if (group >= EXT3_SB(sb)->s_groups_count)
1247 +                       break;
1248 +
1249 +               data = page_address(page) + (i * blocksize);
1250 +               bitmap = bh[group - first_group]->b_data;
1251 +
1252 +               if ((first_block + i) & 1) {
1253 +                       /* this is block of buddy */
1254 +                       BUG_ON(incore == NULL);
1255 +                       mb_debug("put buddy for group %u in page %lu/%x\n",
1256 +                               group, page->index, i * blocksize);
1257 +                       memset(data, 0xff, blocksize);
1258 +                       EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
1259 +                       memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
1260 +                              sizeof(unsigned short)*(sb->s_blocksize_bits+2));
1261 +                       ext3_mb_generate_buddy(sb, data, incore, group);
1262 +                       incore = NULL;
1263 +               } else {
1264 +                       /* this is block of bitmap */
1265 +                       BUG_ON(incore != NULL);
1266 +                       mb_debug("put bitmap for group %u in page %lu/%x\n",
1267 +                               group, page->index, i * blocksize);
1268 +
1269 +                       /* see comments in ext3_mb_put_pa() */
1270 +                       ext3_lock_group(sb, group);
1271 +                       memcpy(data, bitmap, blocksize);
1272 +
1273 +                       /* mark all preallocated blocks used in in-core bitmap */
1274 +                       ext3_mb_generate_from_pa(sb, data, group);
1275 +                       ext3_unlock_group(sb, group);
1276 +
1277 +                       incore = data;
1278 +               }
1279 +       }
1280 +       SetPageUptodate(page);
1281 +
1282 +out:
1283 +       if (bh) {
1284 +               for (i = 0; i < groups_per_page && bh[i]; i++)
1285 +                       brelse(bh[i]);
1286 +               if (bh != &bhs)
1287 +                       kfree(bh);
1288 +       }
1289 +       return err;
1290 +}
1291 +
1292 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
1293 +               struct ext3_buddy *e3b)
1294 +{
1295 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1296 +       struct inode *inode = sbi->s_buddy_cache;
1297 +       int blocks_per_page, block, pnum, poff;
1298 +       struct page *page;
1299 +
1300 +       mb_debug("load group %u\n", group);
1301 +
1302 +       blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1303 +
1304 +       e3b->bd_blkbits = sb->s_blocksize_bits;
1305 +       e3b->bd_info = EXT3_GROUP_INFO(sb, group);
1306 +       e3b->bd_sb = sb;
1307 +       e3b->bd_group = group;
1308 +       e3b->bd_buddy_page = NULL;
1309 +       e3b->bd_bitmap_page = NULL;
1310 +
1311 +       block = group * 2;
1312 +       pnum = block / blocks_per_page;
1313 +       poff = block % blocks_per_page;
1314 +
1315 +       /* we could use find_or_create_page(), but it locks page
1316 +        * what we'd like to avoid in fast path ... */
1317 +       page = find_get_page(inode->i_mapping, pnum);
1318 +       if (page == NULL || !PageUptodate(page)) {
1319 +               if (page)
1320 +                       page_cache_release(page);
1321 +               page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1322 +               if (page) {
1323 +                       BUG_ON(page->mapping != inode->i_mapping);
1324 +                       if (!PageUptodate(page)) {
1325 +                               ext3_mb_init_cache(page, NULL);
1326 +                               mb_cmp_bitmaps(e3b, page_address(page) +
1327 +                                              (poff * sb->s_blocksize));
1328 +                       }
1329 +                       unlock_page(page);
1330 +               }
1331 +       }
1332 +       if (page == NULL || !PageUptodate(page))
1333 +               goto err;
1334 +       e3b->bd_bitmap_page = page;
1335 +       e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1336 +       mark_page_accessed(page);
1337 +
1338 +       block++;
1339 +       pnum = block / blocks_per_page;
1340 +       poff = block % blocks_per_page;
1341 +
1342 +       page = find_get_page(inode->i_mapping, pnum);
1343 +       if (page == NULL || !PageUptodate(page)) {
1344 +               if (page)
1345 +                       page_cache_release(page);
1346 +               page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1347 +               if (page) {
1348 +                       BUG_ON(page->mapping != inode->i_mapping);
1349 +                       if (!PageUptodate(page))
1350 +                               ext3_mb_init_cache(page, e3b->bd_bitmap);
1351 +
1352 +                       unlock_page(page);
1353 +               }
1354 +       }
1355 +       if (page == NULL || !PageUptodate(page))
1356 +               goto err;
1357 +       e3b->bd_buddy_page = page;
1358 +       e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1359 +       mark_page_accessed(page);
1360 +
1361 +       BUG_ON(e3b->bd_bitmap_page == NULL);
1362 +       BUG_ON(e3b->bd_buddy_page == NULL);
1363 +
1364 +       return 0;
1365 +
1366 +err:
1367 +       if (e3b->bd_bitmap_page)
1368 +               page_cache_release(e3b->bd_bitmap_page);
1369 +       if (e3b->bd_buddy_page)
1370 +               page_cache_release(e3b->bd_buddy_page);
1371 +       e3b->bd_buddy = NULL;
1372 +       e3b->bd_bitmap = NULL;
1373 +       return -EIO;
1374 +}
1375 +
1376 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1377 +{
1378 +       if (e3b->bd_bitmap_page)
1379 +               page_cache_release(e3b->bd_bitmap_page);
1380 +       if (e3b->bd_buddy_page)
1381 +               page_cache_release(e3b->bd_buddy_page);
1382 +}
1383 +
1384 +
1385 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1386 +{
1387 +       int order = 1;
1388 +       void *bb;
1389 +
1390 +       BUG_ON(EXT3_MB_BITMAP(e3b) == EXT3_MB_BUDDY(e3b));
1391 +       BUG_ON(block >= (1 << (e3b->bd_blkbits + 3)));
1392 +
1393 +       bb = EXT3_MB_BUDDY(e3b);
1394 +       while (order <= e3b->bd_blkbits + 1) {
1395 +               block = block >> 1;
1396 +               if (!mb_test_bit(block, bb)) {
1397 +                       /* this block is part of buddy of order 'order' */
1398 +                       return order;
1399 +               }
1400 +               bb += 1 << (e3b->bd_blkbits - order);
1401 +               order++;
1402 +       }
1403 +       return 0;
1404 +}
1405 +
1406 +static inline void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
1407 +{
1408 +       __u32 *addr;
1409 +
1410 +       len = cur + len;
1411 +       while (cur < len) {
1412 +               if ((cur & 31) == 0 && (len - cur) >= 32) {
1413 +                       /* fast path: clear whole word at once */
1414 +                       addr = bm + (cur >> 3);
1415 +                       *addr = 0;
1416 +                       cur += 32;
1417 +                       continue;
1418 +               }
1419 +               mb_clear_bit_atomic(lock, cur, bm);
1420 +               cur++;
1421 +       }
1422 +}
1423 +
1424 +static inline void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
1425 +{
1426 +       __u32 *addr;
1427 +
1428 +       len = cur + len;
1429 +       while (cur < len) {
1430 +               if ((cur & 31) == 0 && (len - cur) >= 32) {
1431 +                       /* fast path: clear whole word at once */
1432 +                       addr = bm + (cur >> 3);
1433 +                       *addr = 0xffffffff;
1434 +                       cur += 32;
1435 +                       continue;
1436 +               }
1437 +               mb_set_bit_atomic(lock, cur, bm);
1438 +               cur++;
1439 +       }
1440 +}
1441 +
1442 +static int mb_free_blocks(struct inode *inode, struct ext3_buddy *e3b,
1443 +                         int first, int count)
1444 +{
1445 +       int block = 0, max = 0, order;
1446 +       void *buddy, *buddy2;
1447 +       struct super_block *sb = e3b->bd_sb;
1448 +
1449 +       BUG_ON(first + count > (sb->s_blocksize << 3));
1450 +       BUG_ON(!ext3_is_group_locked(sb, e3b->bd_group));
1451 +       mb_check_buddy(e3b);
1452 +       mb_free_blocks_double(inode, e3b, first, count);
1453 +
1454 +       e3b->bd_info->bb_free += count;
1455 +       if (first < e3b->bd_info->bb_first_free)
1456 +               e3b->bd_info->bb_first_free = first;
1457 +
1458 +       /* let's maintain fragments counter */
1459 +       if (first != 0)
1460 +               block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1461 +       if (first + count < EXT3_SB(sb)->s_mb_maxs[0])
1462 +               max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1463 +       if (block && max)
1464 +               e3b->bd_info->bb_fragments--;
1465 +       else if (!block && !max)
1466 +               e3b->bd_info->bb_fragments++;
1467 +
1468 +       /* let's maintain buddy itself */
1469 +       while (count-- > 0) {
1470 +               block = first++;
1471 +               order = 0;
1472 +
1473 +               if (!mb_test_bit(block, EXT3_MB_BITMAP(e3b))) {
1474 +                       unsigned long blocknr;
1475 +                       blocknr = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb);
1476 +                       blocknr += block;
1477 +                       blocknr +=
1478 +                           le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
1479 +
1480 +                       ext3_error(sb, __FUNCTION__, "double-free of inode"
1481 +                                  " %lu's block %lu(bit %u in group %u)\n",
1482 +                                  inode ? inode->i_ino : 0, blocknr, block,
1483 +                                  e3b->bd_group);
1484 +               }
1485 +               mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1486 +               e3b->bd_info->bb_counters[order]++;
1487 +
1488 +               /* start of the buddy */
1489 +               buddy = mb_find_buddy(e3b, order, &max);
1490 +
1491 +               do {
1492 +                       block &= ~1UL;
1493 +                       if (mb_test_bit(block, buddy) ||
1494 +                                       mb_test_bit(block + 1, buddy))
1495 +                               break;
1496 +
1497 +                       /* both the buddies are free, try to coalesce them */
1498 +                       buddy2 = mb_find_buddy(e3b, order + 1, &max);
1499 +
1500 +                       if (!buddy2)
1501 +                               break;
1502 +
1503 +                       if (order > 0) {
1504 +                               /* for special purposes, we don't set
1505 +                                * free bits in bitmap */
1506 +                               mb_set_bit(block, buddy);
1507 +                               mb_set_bit(block + 1, buddy);
1508 +                       }
1509 +                       e3b->bd_info->bb_counters[order]--;
1510 +                       e3b->bd_info->bb_counters[order]--;
1511 +
1512 +                       block = block >> 1;
1513 +                       order++;
1514 +                       e3b->bd_info->bb_counters[order]++;
1515 +
1516 +                       mb_clear_bit(block, buddy2);
1517 +                       buddy = buddy2;
1518 +               } while (1);
1519 +       }
1520 +       mb_check_buddy(e3b);
1521 +
1522 +       return 0;
1523 +}
1524 +
1525 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1526 +                               int needed, struct ext3_free_extent *ex)
1527 +{
1528 +       int next = block, max, ord;
1529 +       void *buddy;
1530 +
1531 +       BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
1532 +       BUG_ON(ex == NULL);
1533 +
1534 +       buddy = mb_find_buddy(e3b, order, &max);
1535 +       BUG_ON(buddy == NULL);
1536 +       BUG_ON(block >= max);
1537 +       if (mb_test_bit(block, buddy)) {
1538 +               ex->fe_len = 0;
1539 +               ex->fe_start = 0;
1540 +               ex->fe_group = 0;
1541 +               return 0;
1542 +       }
1543 +
1544 +       if (likely(order == 0)) {
1545 +               /* find actual order */
1546 +               order = mb_find_order_for_block(e3b, block);
1547 +               block = block >> order;
1548 +       }
1549 +
1550 +       ex->fe_len = 1 << order;
1551 +       ex->fe_start = block << order;
1552 +       ex->fe_group = e3b->bd_group;
1553 +
1554 +       /* calc difference from given start */
1555 +       next = next - ex->fe_start;
1556 +       ex->fe_len -= next;
1557 +       ex->fe_start += next;
1558 +
1559 +       while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1560 +
1561 +               if (block + 1 >= max)
1562 +                       break;
1563 +
1564 +               next = (block + 1) * (1 << order);
1565 +               if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1566 +                       break;
1567 +
1568 +               ord = mb_find_order_for_block(e3b, next);
1569 +
1570 +               order = ord;
1571 +               block = next >> order;
1572 +               ex->fe_len += 1 << order;
1573 +       }
1574 +
1575 +       BUG_ON(ex->fe_start + ex->fe_len > (1 << (e3b->bd_blkbits + 3)));
1576 +       return ex->fe_len;
1577 +}
1578 +
1579 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1580 +{
1581 +       int ord, mlen = 0, max = 0, cur;
1582 +       int start = ex->fe_start;
1583 +       int len = ex->fe_len;
1584 +       unsigned ret = 0;
1585 +       int len0 = len;
1586 +       void *buddy;
1587 +
1588 +       BUG_ON(start + len > (e3b->bd_sb->s_blocksize << 3));
1589 +       BUG_ON(e3b->bd_group != ex->fe_group);
1590 +       BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
1591 +       mb_check_buddy(e3b);
1592 +       mb_mark_used_double(e3b, start, len);
1593 +
1594 +       e3b->bd_info->bb_free -= len;
1595 +       if (e3b->bd_info->bb_first_free == start)
1596 +               e3b->bd_info->bb_first_free += len;
1597 +
1598 +       /* let's maintain fragments counter */
1599 +       if (start != 0)
1600 +               mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1601 +       if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1602 +               max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1603 +       if (mlen && max)
1604 +               e3b->bd_info->bb_fragments++;
1605 +       else if (!mlen && !max)
1606 +               e3b->bd_info->bb_fragments--;
1607 +
1608 +       /* let's maintain buddy itself */
1609 +       while (len) {
1610 +               ord = mb_find_order_for_block(e3b, start);
1611 +
1612 +               if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1613 +                       /* the whole chunk may be allocated at once! */
1614 +                       mlen = 1 << ord;
1615 +                       buddy = mb_find_buddy(e3b, ord, &max);
1616 +                       BUG_ON((start >> ord) >= max);
1617 +                       mb_set_bit(start >> ord, buddy);
1618 +                       e3b->bd_info->bb_counters[ord]--;
1619 +                       start += mlen;
1620 +                       len -= mlen;
1621 +                       BUG_ON(len < 0);
1622 +                       continue;
1623 +               }
1624 +
1625 +               /* store for history */
1626 +               if (ret == 0)
1627 +                       ret = len | (ord << 16);
1628 +
1629 +               /* we have to split large buddy */
1630 +               BUG_ON(ord <= 0);
1631 +               buddy = mb_find_buddy(e3b, ord, &max);
1632 +               mb_set_bit(start >> ord, buddy);
1633 +               e3b->bd_info->bb_counters[ord]--;
1634 +
1635 +               ord--;
1636 +               cur = (start >> ord) & ~1U;
1637 +               buddy = mb_find_buddy(e3b, ord, &max);
1638 +               mb_clear_bit(cur, buddy);
1639 +               mb_clear_bit(cur + 1, buddy);
1640 +               e3b->bd_info->bb_counters[ord]++;
1641 +               e3b->bd_info->bb_counters[ord]++;
1642 +       }
1643 +
1644 +       mb_set_bits(sb_bgl_lock(EXT3_SB(e3b->bd_sb), ex->fe_group),
1645 +                   EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1646 +       mb_check_buddy(e3b);
1647 +
1648 +       return ret;
1649 +}
1650 +
1651 +/*
1652 + * Must be called under group lock!
1653 + */
1654 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1655 +                                       struct ext3_buddy *e3b)
1656 +{
1657 +       unsigned long ret;
1658 +
1659 +       BUG_ON(ac->ac_b_ex.fe_group != e3b->bd_group);
1660 +       BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1661 +
1662 +       ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1663 +       ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1664 +       ret = mb_mark_used(e3b, &ac->ac_b_ex);
1665 +
1666 +       /* preallocation can change ac_b_ex, thus we store actually
1667 +        * allocated blocks for history */
1668 +       ac->ac_f_ex = ac->ac_b_ex;
1669 +
1670 +       ac->ac_status = AC_STATUS_FOUND;
1671 +       ac->ac_tail = ret & 0xffff;
1672 +       ac->ac_buddy = ret >> 16;
1673 +
1674 +       /* XXXXXXX: SUCH A HORRIBLE **CK */
1675 +       ac->ac_bitmap_page = e3b->bd_bitmap_page;
1676 +       get_page(ac->ac_bitmap_page);
1677 +       ac->ac_buddy_page = e3b->bd_buddy_page;
1678 +       get_page(ac->ac_buddy_page);
1679 +}
1680 +
1681 +/*
1682 + * regular allocator, for general purposes allocation
1683 + */
1684 +
1685 +void ext3_mb_check_limits(struct ext3_allocation_context *ac,
1686 +                                       struct ext3_buddy *e3b,
1687 +                                       int finish_group)
1688 +{
1689 +       struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1690 +       struct ext3_free_extent *bex = &ac->ac_b_ex;
1691 +       struct ext3_free_extent *gex = &ac->ac_g_ex;
1692 +       struct ext3_free_extent ex;
1693 +       int max;
1694 +
1695 +       /*
1696 +        * We don't want to scan for a whole year
1697 +        */
1698 +       if (ac->ac_found > sbi->s_mb_max_to_scan &&
1699 +                       !(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1700 +               ac->ac_status = AC_STATUS_BREAK;
1701 +               return;
1702 +       }
1703 +
1704 +       /*
1705 +        * Haven't found good chunk so far, let's continue
1706 +        */
1707 +       if (bex->fe_len < gex->fe_len)
1708 +               return;
1709 +
1710 +       if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1711 +                       && bex->fe_group == e3b->bd_group) {
1712 +               /* recheck chunk's availability - we don't know
1713 +                * when it was found (within this lock-unlock
1714 +                * period or not) */
1715 +               max = mb_find_extent(e3b, 0, bex->fe_start, gex->fe_len, &ex);
1716 +               if (max >= gex->fe_len) {
1717 +                       ext3_mb_use_best_found(ac, e3b);
1718 +                       return;
1719 +               }
1720 +       }
1721 +}
1722 +
1723 +/*
1724 + * The routine checks whether found extent is good enough. If it is,
1725 + * then the extent gets marked used and flag is set to the context
1726 + * to stop scanning. Otherwise, the extent is compared with the
1727 + * previous found extent and if new one is better, then it's stored
1728 + * in the context. Later, the best found extent will be used, if
1729 + * mballoc can't find good enough extent.
1730 + *
1731 + * FIXME: real allocation policy is to be designed yet!
1732 + */
1733 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1734 +                                       struct ext3_free_extent *ex,
1735 +                                       struct ext3_buddy *e3b)
1736 +{
1737 +       struct ext3_free_extent *bex = &ac->ac_b_ex;
1738 +       struct ext3_free_extent *gex = &ac->ac_g_ex;
1739 +
1740 +       BUG_ON(ex->fe_len <= 0);
1741 +       BUG_ON(ex->fe_len >= EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1742 +       BUG_ON(ex->fe_start >= EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1743 +       BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1744 +
1745 +       ac->ac_found++;
1746 +
1747 +       /*
1748 +        * The special case - take what you catch first
1749 +        */
1750 +       if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1751 +               *bex = *ex;
1752 +               ext3_mb_use_best_found(ac, e3b);
1753 +               return;
1754 +       }
1755 +
1756 +       /*
1757 +        * Let's check whether the chuck is good enough
1758 +        */
1759 +       if (ex->fe_len == gex->fe_len) {
1760 +               *bex = *ex;
1761 +               ext3_mb_use_best_found(ac, e3b);
1762 +               return;
1763 +       }
1764 +
1765 +       /*
1766 +        * If this is first found extent, just store it in the context
1767 +        */
1768 +       if (bex->fe_len == 0) {
1769 +               *bex = *ex;
1770 +               return;
1771 +       }
1772 +
1773 +       /*
1774 +        * If new found extent is better, store it in the context
1775 +        */
1776 +       if (bex->fe_len < gex->fe_len) {
1777 +               /* if the request isn't satisfied, any found extent
1778 +                * larger than previous best one is better */
1779 +               if (ex->fe_len > bex->fe_len)
1780 +                       *bex = *ex;
1781 +       } else if (ex->fe_len > gex->fe_len) {
1782 +               /* if the request is satisfied, then we try to find
1783 +                * an extent that still satisfy the request, but is
1784 +                * smaller than previous one */
1785 +               *bex = *ex;
1786 +       }
1787 +
1788 +       ext3_mb_check_limits(ac, e3b, 0);
1789 +}
1790 +
1791 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1792 +                                       struct ext3_buddy *e3b)
1793 +{
1794 +       struct ext3_free_extent ex = ac->ac_b_ex;
1795 +       int group = ex.fe_group, max, err;
1796 +
1797 +       BUG_ON(ex.fe_len <= 0);
1798 +       err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1799 +       if (err)
1800 +               return err;
1801 +
1802 +       ext3_lock_group(ac->ac_sb, group);
1803 +       max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1804 +
1805 +       if (max > 0) {
1806 +               ac->ac_b_ex = ex;
1807 +               ext3_mb_use_best_found(ac, e3b);
1808 +       }
1809 +
1810 +       ext3_unlock_group(ac->ac_sb, group);
1811 +       ext3_mb_release_desc(e3b);
1812 +
1813 +       return 0;
1814 +}
1815 +
1816 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1817 +                               struct ext3_buddy *e3b)
1818 +{
1819 +       int group = ac->ac_g_ex.fe_group, max, err;
1820 +       struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1821 +       struct ext3_super_block *es = sbi->s_es;
1822 +       struct ext3_free_extent ex;
1823 +
1824 +       err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1825 +       if (err)
1826 +               return err;
1827 +
1828 +       ext3_lock_group(ac->ac_sb, group);
1829 +       max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1830 +                            ac->ac_g_ex.fe_len, &ex);
1831 +
1832 +       if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1833 +               unsigned long start;
1834 +               start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1835 +                       ex.fe_start + le32_to_cpu(es->s_first_data_block));
1836 +               if (start % sbi->s_stripe == 0) {
1837 +                       ac->ac_found++;
1838 +                       ac->ac_b_ex = ex;
1839 +                       ext3_mb_use_best_found(ac, e3b);
1840 +               }
1841 +       } else if (max >= ac->ac_g_ex.fe_len) {
1842 +               BUG_ON(ex.fe_len <= 0);
1843 +               BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1844 +               BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1845 +               ac->ac_found++;
1846 +               ac->ac_b_ex = ex;
1847 +               ext3_mb_use_best_found(ac, e3b);
1848 +       } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1849 +               /* Sometimes, caller may want to merge even small
1850 +                * number of blocks to an existing extent */
1851 +               BUG_ON(ex.fe_len <= 0);
1852 +               BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1853 +               BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1854 +               ac->ac_found++;
1855 +               ac->ac_b_ex = ex;
1856 +               ext3_mb_use_best_found(ac, e3b);
1857 +       }
1858 +       ext3_unlock_group(ac->ac_sb, group);
1859 +       ext3_mb_release_desc(e3b);
1860 +
1861 +       return 0;
1862 +}
1863 +
1864 +/*
1865 + * The routine scans buddy structures (not bitmap!) from given order
1866 + * to max order and tries to find big enough chunk to satisfy the req
1867 + */
1868 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1869 +                                       struct ext3_buddy *e3b)
1870 +{
1871 +       struct super_block *sb = ac->ac_sb;
1872 +       struct ext3_group_info *grp = e3b->bd_info;
1873 +       void *buddy;
1874 +       int i, k, max;
1875 +
1876 +       BUG_ON(ac->ac_2order <= 0);
1877 +       for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1878 +               if (grp->bb_counters[i] == 0)
1879 +                       continue;
1880 +
1881 +               buddy = mb_find_buddy(e3b, i, &max);
1882 +               BUG_ON(buddy == NULL);
1883 +
1884 +               k = mb_find_next_zero_bit(buddy, max, 0);
1885 +               BUG_ON(k >= max);
1886 +
1887 +               ac->ac_found++;
1888 +
1889 +               ac->ac_b_ex.fe_len = 1 << i;
1890 +               ac->ac_b_ex.fe_start = k << i;
1891 +               ac->ac_b_ex.fe_group = e3b->bd_group;
1892 +
1893 +               ext3_mb_use_best_found(ac, e3b);
1894 +
1895 +               BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1896 +
1897 +               if (EXT3_SB(sb)->s_mb_stats)
1898 +                       atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1899 +
1900 +               break;
1901 +       }
1902 +}
1903 +
1904 +/*
1905 + * The routine scans the group and measures all found extents.
1906 + * In order to optimize scanning, caller must pass number of
1907 + * free blocks in the group, so the routine can know upper limit.
1908 + */
1909 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1910 +                                       struct ext3_buddy *e3b)
1911 +{
1912 +       struct super_block *sb = ac->ac_sb;
1913 +       void *bitmap = EXT3_MB_BITMAP(e3b);
1914 +       struct ext3_free_extent ex;
1915 +       int i, free;
1916 +
1917 +       free = e3b->bd_info->bb_free;
1918 +       BUG_ON(free <= 0);
1919 +
1920 +       i = e3b->bd_info->bb_first_free;
1921 +
1922 +       while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1923 +               i = mb_find_next_zero_bit(bitmap, EXT3_BLOCKS_PER_GROUP(sb), i);
1924 +               if (i >= EXT3_BLOCKS_PER_GROUP(sb)) {
1925 +                       BUG_ON(free != 0);
1926 +                       break;
1927 +               }
1928 +
1929 +               mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1930 +               BUG_ON(ex.fe_len <= 0);
1931 +               BUG_ON(free < ex.fe_len);
1932 +
1933 +               ext3_mb_measure_extent(ac, &ex, e3b);
1934 +
1935 +               i += ex.fe_len;
1936 +               free -= ex.fe_len;
1937 +       }
1938 +
1939 +       ext3_mb_check_limits(ac, e3b, 1);
1940 +}
1941 +
1942 +/*
1943 + * This is a special case for storages like raid5
1944 + * we try to find stripe-aligned chunks for stripe-size requests
1945 + */
1946 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1947 +                                struct ext3_buddy *e3b)
1948 +{
1949 +       struct super_block *sb = ac->ac_sb;
1950 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
1951 +       void *bitmap = EXT3_MB_BITMAP(e3b);
1952 +       struct ext3_free_extent ex;
1953 +       unsigned long i, max;
1954 +
1955 +       BUG_ON(sbi->s_stripe == 0);
1956 +
1957 +       /* find first stripe-aligned block */
1958 +       i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb)
1959 +               + le32_to_cpu(sbi->s_es->s_first_data_block);
1960 +       i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1961 +       i = (i - le32_to_cpu(sbi->s_es->s_first_data_block))
1962 +                       % EXT3_BLOCKS_PER_GROUP(sb);
1963 +
1964 +       while (i < EXT3_BLOCKS_PER_GROUP(sb)) {
1965 +               if (!mb_test_bit(i, bitmap)) {
1966 +                       max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1967 +                       if (max >= sbi->s_stripe) {
1968 +                               ac->ac_found++;
1969 +                               ac->ac_b_ex = ex;
1970 +                               ext3_mb_use_best_found(ac, e3b);
1971 +                               break;
1972 +                       }
1973 +               }
1974 +               i += sbi->s_stripe;
1975 +       }
1976 +}
1977 +
1978 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1979 +                               int group, int cr)
1980 +{
1981 +       struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1982 +       unsigned free, fragments, i, bits;
1983 +
1984 +       BUG_ON(cr < 0 || cr >= 4);
1985 +       BUG_ON(EXT3_MB_GRP_NEED_INIT(grp));
1986 +
1987 +       free = grp->bb_free;
1988 +       fragments = grp->bb_fragments;
1989 +       if (free == 0)
1990 +               return 0;
1991 +       if (fragments == 0)
1992 +               return 0;
1993 +
1994 +       switch (cr) {
1995 +               case 0:
1996 +                       BUG_ON(ac->ac_2order == 0);
1997 +                       bits = ac->ac_sb->s_blocksize_bits + 1;
1998 +                       for (i = ac->ac_2order; i <= bits; i++)
1999 +                               if (grp->bb_counters[i] > 0)
2000 +                                       return 1;
2001 +                       break;
2002 +               case 1:
2003 +                       if ((free / fragments) >= ac->ac_g_ex.fe_len)
2004 +                               return 1;
2005 +                       break;
2006 +               case 2:
2007 +                       if (free >= ac->ac_g_ex.fe_len)
2008 +                               return 1;
2009 +                       break;
2010 +               case 3:
2011 +                       return 1;
2012 +               default:
2013 +                       BUG();
2014 +       }
2015 +
2016 +       return 0;
2017 +}
2018 +
2019 +int ext3_mb_regular_allocator(struct ext3_allocation_context *ac)
2020 +{
2021 +       int group, i, cr, err = 0;
2022 +       struct ext3_sb_info *sbi;
2023 +       struct super_block *sb;
2024 +       struct ext3_buddy e3b;
2025 +
2026 +       sb = ac->ac_sb;
2027 +       sbi = EXT3_SB(sb);
2028 +       BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2029 +
2030 +       /* first, try the goal */
2031 +       err = ext3_mb_find_by_goal(ac, &e3b);
2032 +       if (err || ac->ac_status == AC_STATUS_FOUND)
2033 +               goto out;
2034 +
2035 +       if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
2036 +               goto out;
2037 +
2038 +       i = ffs(ac->ac_g_ex.fe_len);
2039 +       ac->ac_2order = 0;
2040 +       if (i >= sbi->s_mb_order2_reqs) {
2041 +               i--;
2042 +               if ((ac->ac_g_ex.fe_len & (~(1 << i))) == 0)
2043 +                       ac->ac_2order = i;
2044 +       }
2045 +
2046 +       group = ac->ac_g_ex.fe_group;
2047 +
2048 +       /* Let's just scan groups to find more-less suitable blocks */
2049 +       cr = ac->ac_2order ? 0 : 1;
2050 +repeat:
2051 +       for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2052 +               ac->ac_criteria = cr;
2053 +               for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
2054 +                       struct ext3_group_info *grp;
2055 +
2056 +                       if (group == EXT3_SB(sb)->s_groups_count)
2057 +                               group = 0;
2058 +
2059 +                       /* quick check to skip empty groups */
2060 +                       grp = EXT3_GROUP_INFO(ac->ac_sb, group);
2061 +                       if (grp->bb_free == 0)
2062 +                               continue;
2063 +
2064 +                       if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
2065 +                               /* we need full data about the group
2066 +                                * to make a good selection */
2067 +                               err = ext3_mb_load_buddy(sb, group, &e3b);
2068 +                               if (err)
2069 +                                       goto out;
2070 +                               ext3_mb_release_desc(&e3b);
2071 +                       }
2072 +
2073 +                       /* check is group good for our criteries */
2074 +                       if (!ext3_mb_good_group(ac, group, cr))
2075 +                               continue;
2076 +
2077 +                       err = ext3_mb_load_buddy(sb, group, &e3b);
2078 +                       if (err)
2079 +                               goto out;
2080 +
2081 +                       ext3_lock_group(sb, group);
2082 +                       if (!ext3_mb_good_group(ac, group, cr)) {
2083 +                               /* someone did allocation from this group */
2084 +                               ext3_unlock_group(sb, group);
2085 +                               ext3_mb_release_desc(&e3b);
2086 +                               continue;
2087 +                       }
2088 +
2089 +                       ac->ac_groups_scanned++;
2090 +                       if (cr == 0)
2091 +                               ext3_mb_simple_scan_group(ac, &e3b);
2092 +                       else if (cr == 1 && ac->ac_g_ex.fe_len == sbi->s_stripe)
2093 +                               ext3_mb_scan_aligned(ac, &e3b);
2094 +                       else
2095 +                               ext3_mb_complex_scan_group(ac, &e3b);
2096 +
2097 +                       ext3_unlock_group(sb, group);
2098 +                       ext3_mb_release_desc(&e3b);
2099 +
2100 +                       if (ac->ac_status != AC_STATUS_CONTINUE)
2101 +                               break;
2102 +               }
2103 +       }
2104 +
2105 +       if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2106 +           !(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
2107 +               /*
2108 +                * We've been searching too long. Let's try to allocate
2109 +                * the best chunk we've found so far
2110 +                */
2111 +
2112 +               ext3_mb_try_best_found(ac, &e3b);
2113 +               if (ac->ac_status != AC_STATUS_FOUND) {
2114 +                       /*
2115 +                        * Someone more lucky has already allocated it.
2116 +                        * The only thing we can do is just take first
2117 +                        * found block(s)
2118 +                       printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
2119 +                        */
2120 +                       ac->ac_b_ex.fe_group = 0;
2121 +                       ac->ac_b_ex.fe_start = 0;
2122 +                       ac->ac_b_ex.fe_len = 0;
2123 +                       ac->ac_status = AC_STATUS_CONTINUE;
2124 +                       ac->ac_flags |= EXT3_MB_HINT_FIRST;
2125 +                       cr = 3;
2126 +                       atomic_inc(&sbi->s_mb_lost_chunks);
2127 +                       goto repeat;
2128 +               }
2129 +       }
2130 +out:
2131 +       return err;
2132 +}
2133 +
2134 +#ifdef EXT3_MB_HISTORY
2135 +struct ext3_mb_proc_session {
2136 +       struct ext3_mb_history *history;
2137 +       struct super_block *sb;
2138 +       int start;
2139 +       int max;
2140 +};
2141 +
2142 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
2143 +                                       struct ext3_mb_history *hs,
2144 +                                       int first)
2145 +{
2146 +       if (hs == s->history + s->max)
2147 +               hs = s->history;
2148 +       if (!first && hs == s->history + s->start)
2149 +               return NULL;
2150 +       while (hs->orig.fe_len == 0) {
2151 +               hs++;
2152 +               if (hs == s->history + s->max)
2153 +                       hs = s->history;
2154 +               if (hs == s->history + s->start)
2155 +                       return NULL;
2156 +       }
2157 +       return hs;
2158 +}
2159 +
2160 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2161 +{
2162 +       struct ext3_mb_proc_session *s = seq->private;
2163 +       struct ext3_mb_history *hs;
2164 +       int l = *pos;
2165 +
2166 +       if (l == 0)
2167 +               return SEQ_START_TOKEN;
2168 +       hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2169 +       if (!hs)
2170 +               return NULL;
2171 +       while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2172 +       return hs;
2173 +}
2174 +
2175 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2176 +{
2177 +       struct ext3_mb_proc_session *s = seq->private;
2178 +       struct ext3_mb_history *hs = v;
2179 +
2180 +       ++*pos;
2181 +       if (v == SEQ_START_TOKEN)
2182 +               return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2183 +       else
2184 +               return ext3_mb_history_skip_empty(s, ++hs, 0);
2185 +}
2186 +
2187 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2188 +{
2189 +       char buf[25], buf2[25], buf3[25], *fmt;
2190 +       struct ext3_mb_history *hs = v;
2191 +
2192 +       if (v == SEQ_START_TOKEN) {
2193 +               seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2194 +                               "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2195 +                          "pid", "inode", "original", "goal", "result","found",
2196 +                          "grps", "cr", "flags", "merge", "tail", "broken");
2197 +               return 0;
2198 +       }
2199 +
2200 +       if (hs->op == EXT3_MB_HISTORY_ALLOC) {
2201 +               fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2202 +                       "%-5u %-5s %-5u %-6u\n";
2203 +               sprintf(buf2, "%lu/%lu/%lu@%lu", hs->result.fe_group,
2204 +                       hs->result.fe_start, hs->result.fe_len,
2205 +                       hs->result.fe_logical);
2206 +               sprintf(buf, "%lu/%lu/%lu@%lu", hs->orig.fe_group,
2207 +                       hs->orig.fe_start, hs->orig.fe_len,
2208 +                       hs->orig.fe_logical);
2209 +               sprintf(buf3, "%lu/%lu/%lu@%lu", hs->goal.fe_group,
2210 +                       hs->goal.fe_start, hs->goal.fe_len,
2211 +                       hs->goal.fe_logical);
2212 +               seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2213 +                               hs->found, hs->groups, hs->cr, hs->flags,
2214 +                               hs->merged ? "M" : "", hs->tail,
2215 +                               hs->buddy ? 1 << hs->buddy : 0);
2216 +       } else if (hs->op == EXT3_MB_HISTORY_PREALLOC) {
2217 +               fmt = "%-5u %-8u %-23s %-23s %-23s\n";
2218 +               sprintf(buf2, "%lu/%lu/%lu@%lu", hs->result.fe_group,
2219 +                       hs->result.fe_start, hs->result.fe_len,
2220 +                       hs->result.fe_logical);
2221 +               sprintf(buf, "%lu/%lu/%lu@%lu", hs->orig.fe_group,
2222 +                       hs->orig.fe_start, hs->orig.fe_len,
2223 +                       hs->orig.fe_logical);
2224 +               seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2225 +       } else if (hs->op == EXT3_MB_HISTORY_DISCARD) {
2226 +               sprintf(buf2, "%lu/%lu/%lu", hs->result.fe_group,
2227 +                       hs->result.fe_start, hs->result.fe_len);
2228 +               seq_printf(seq, "%-5u %-8u %-23s discard\n",
2229 +                               hs->pid, hs->ino, buf2);
2230 +       } else if (hs->op == EXT3_MB_HISTORY_FREE) {
2231 +               sprintf(buf2, "%lu/%lu/%lu", hs->result.fe_group,
2232 +                       hs->result.fe_start, hs->result.fe_len);
2233 +               seq_printf(seq, "%-5u %-8u %-23s free\n",
2234 +                               hs->pid, hs->ino, buf2);
2235 +       }
2236 +       return 0;
2237 +}
2238 +
2239 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2240 +{
2241 +}
2242 +
2243 +static struct seq_operations ext3_mb_seq_history_ops = {
2244 +       .start  = ext3_mb_seq_history_start,
2245 +       .next   = ext3_mb_seq_history_next,
2246 +       .stop   = ext3_mb_seq_history_stop,
2247 +       .show   = ext3_mb_seq_history_show,
2248 +};
2249 +
2250 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2251 +{
2252 +       struct super_block *sb = PDE(inode)->data;
2253 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2254 +       struct ext3_mb_proc_session *s;
2255 +       int rc, size;
2256 +
2257 +       s = kmalloc(sizeof(*s), GFP_KERNEL);
2258 +       if (s == NULL)
2259 +               return -ENOMEM;
2260 +       s->sb = sb;
2261 +       size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2262 +       s->history = kmalloc(size, GFP_KERNEL);
2263 +       if (s->history == NULL) {
2264 +               kfree(s);
2265 +               return -ENOMEM;
2266 +       }
2267 +
2268 +       spin_lock(&sbi->s_mb_history_lock);
2269 +       memcpy(s->history, sbi->s_mb_history, size);
2270 +       s->max = sbi->s_mb_history_max;
2271 +       s->start = sbi->s_mb_history_cur % s->max;
2272 +       spin_unlock(&sbi->s_mb_history_lock);
2273 +
2274 +       rc = seq_open(file, &ext3_mb_seq_history_ops);
2275 +       if (rc == 0) {
2276 +               struct seq_file *m = (struct seq_file *)file->private_data;
2277 +               m->private = s;
2278 +       } else {
2279 +               kfree(s->history);
2280 +               kfree(s);
2281 +       }
2282 +       return rc;
2283 +
2284 +}
2285 +
2286 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2287 +{
2288 +       struct seq_file *seq = (struct seq_file *)file->private_data;
2289 +       struct ext3_mb_proc_session *s = seq->private;
2290 +       kfree(s->history);
2291 +       kfree(s);
2292 +       return seq_release(inode, file);
2293 +}
2294 +
2295 +static ssize_t ext3_mb_seq_history_write(struct file *file,
2296 +                               const char __user *buffer,
2297 +                               size_t count, loff_t *ppos)
2298 +{
2299 +       struct seq_file *seq = (struct seq_file *)file->private_data;
2300 +       struct ext3_mb_proc_session *s = seq->private;
2301 +       struct super_block *sb = s->sb;
2302 +       char str[32];
2303 +       int value;
2304 +
2305 +       if (count >= sizeof(str)) {
2306 +               printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2307 +                               "mb_history", (int)sizeof(str));
2308 +               return -EOVERFLOW;
2309 +       }
2310 +
2311 +       if (copy_from_user(str, buffer, count))
2312 +               return -EFAULT;
2313 +
2314 +       value = simple_strtol(str, NULL, 0);
2315 +       if (value < 0)
2316 +               return -ERANGE;
2317 +       EXT3_SB(sb)->s_mb_history_filter = value;
2318 +
2319 +       return count;
2320 +}
2321 +
2322 +static struct file_operations ext3_mb_seq_history_fops = {
2323 +       .owner          = THIS_MODULE,
2324 +       .open           = ext3_mb_seq_history_open,
2325 +       .read           = seq_read,
2326 +       .write          = ext3_mb_seq_history_write,
2327 +       .llseek         = seq_lseek,
2328 +       .release        = ext3_mb_seq_history_release,
2329 +};
2330 +
2331 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2332 +{
2333 +       struct super_block *sb = seq->private;
2334 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2335 +       long group;
2336 +
2337 +       if (*pos < 0 || *pos >= sbi->s_groups_count)
2338 +               return NULL;
2339 +
2340 +       group = *pos + 1;
2341 +       return (void *) group;
2342 +}
2343 +
2344 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2345 +{
2346 +       struct super_block *sb = seq->private;
2347 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2348 +       long group;
2349 +
2350 +       ++*pos;
2351 +       if (*pos < 0 || *pos >= sbi->s_groups_count)
2352 +               return NULL;
2353 +       group = *pos + 1;
2354 +       return (void *) group;;
2355 +}
2356 +
2357 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2358 +{
2359 +       struct super_block *sb = seq->private;
2360 +       long group = (long) v;
2361 +       int i, err;
2362 +       struct ext3_buddy e3b;
2363 +       struct sg {
2364 +               struct ext3_group_info info;
2365 +               unsigned short counters[16];
2366 +       } sg;
2367 +
2368 +       group--;
2369 +       if (group == 0)
2370 +               seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2371 +                               "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2372 +                                 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2373 +                          "group", "free", "frags", "first",
2374 +                          "2^0", "2^1", "2^2", "2^3", "2^4", "2^5","2^6",
2375 +                          "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2376 +
2377 +       i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2378 +               sizeof(struct ext3_group_info);
2379 +       err = ext3_mb_load_buddy(sb, group, &e3b);
2380 +       if (err) {
2381 +               seq_printf(seq, "#%-5lu: I/O error\n", group);
2382 +               return 0;
2383 +       }
2384 +       ext3_lock_group(sb, group);
2385 +       memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2386 +       ext3_unlock_group(sb, group);
2387 +       ext3_mb_release_desc(&e3b);
2388 +
2389 +       seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2390 +                       sg.info.bb_fragments, sg.info.bb_first_free);
2391 +       for (i = 0; i <= 13; i++)
2392 +               seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2393 +                               sg.info.bb_counters[i] : 0);
2394 +       seq_printf(seq, " ]\n");
2395 +
2396 +       return 0;
2397 +}
2398 +
2399 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2400 +{
2401 +}
2402 +
2403 +static struct seq_operations ext3_mb_seq_groups_ops = {
2404 +       .start  = ext3_mb_seq_groups_start,
2405 +       .next   = ext3_mb_seq_groups_next,
2406 +       .stop   = ext3_mb_seq_groups_stop,
2407 +       .show   = ext3_mb_seq_groups_show,
2408 +};
2409 +
2410 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2411 +{
2412 +       struct super_block *sb = PDE(inode)->data;
2413 +       int rc;
2414 +
2415 +       rc = seq_open(file, &ext3_mb_seq_groups_ops);
2416 +       if (rc == 0) {
2417 +               struct seq_file *m = (struct seq_file *)file->private_data;
2418 +               m->private = sb;
2419 +       }
2420 +       return rc;
2421 +
2422 +}
2423 +
2424 +static struct file_operations ext3_mb_seq_groups_fops = {
2425 +       .owner          = THIS_MODULE,
2426 +       .open           = ext3_mb_seq_groups_open,
2427 +       .read           = seq_read,
2428 +       .llseek         = seq_lseek,
2429 +       .release        = seq_release,
2430 +};
2431 +
2432 +static void ext3_mb_history_release(struct super_block *sb)
2433 +{
2434 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2435 +
2436 +       remove_proc_entry("mb_groups", sbi->s_dev_proc);
2437 +       remove_proc_entry("mb_history", sbi->s_dev_proc);
2438 +
2439 +       if (sbi->s_mb_history)
2440 +               kfree(sbi->s_mb_history);
2441 +}
2442 +
2443 +static void ext3_mb_history_init(struct super_block *sb)
2444 +{
2445 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2446 +       int i;
2447 +
2448 +       if (sbi->s_dev_proc != NULL) {
2449 +               struct proc_dir_entry *p;
2450 +               p = create_proc_entry("mb_history", S_IRUGO, sbi->s_dev_proc);
2451 +               if (p) {
2452 +                       p->proc_fops = &ext3_mb_seq_history_fops;
2453 +                       p->data = sb;
2454 +               }
2455 +               p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_dev_proc);
2456 +               if (p) {
2457 +                       p->proc_fops = &ext3_mb_seq_groups_fops;
2458 +                       p->data = sb;
2459 +               }
2460 +       }
2461 +
2462 +       sbi->s_mb_history_max = 1000;
2463 +       sbi->s_mb_history_cur = 0;
2464 +       spin_lock_init(&sbi->s_mb_history_lock);
2465 +       i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2466 +       sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2467 +       if (likely(sbi->s_mb_history != NULL))
2468 +               memset(sbi->s_mb_history, 0, i);
2469 +       /* if we can't allocate history, then we simple won't use it */
2470 +}
2471 +
2472 +static void
2473 +ext3_mb_store_history(struct ext3_allocation_context *ac)
2474 +{
2475 +       struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
2476 +       struct ext3_mb_history h;
2477 +
2478 +       if (unlikely(sbi->s_mb_history == NULL))
2479 +               return;
2480 +
2481 +       if (!(ac->ac_op & sbi->s_mb_history_filter))
2482 +               return;
2483 +
2484 +       h.op = ac->ac_op;
2485 +       h.pid = current->pid;
2486 +       h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2487 +       h.orig = ac->ac_o_ex;
2488 +       h.result = ac->ac_b_ex;
2489 +       h.flags = ac->ac_flags;
2490 +       h.merged = 0;
2491 +       if (ac->ac_op == EXT3_MB_HISTORY_ALLOC) {
2492 +               if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2493 +                               ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2494 +                       h.merged = 1;
2495 +               h.goal = ac->ac_g_ex;
2496 +               h.result = ac->ac_f_ex;
2497 +       }
2498 +
2499 +       spin_lock(&sbi->s_mb_history_lock);
2500 +       memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2501 +       if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2502 +               sbi->s_mb_history_cur = 0;
2503 +       spin_unlock(&sbi->s_mb_history_lock);
2504 +}
2505 +
2506 +#else
2507 +#define ext3_mb_history_release(sb)
2508 +#define ext3_mb_history_init(sb)
2509 +#endif
2510 +
2511 +int ext3_mb_init_backend(struct super_block *sb)
2512 +{
2513 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2514 +       int i, j, len, metalen;
2515 +       int num_meta_group_infos =
2516 +               (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2517 +                       EXT3_DESC_PER_BLOCK_BITS(sb);
2518 +       struct ext3_group_info **meta_group_info;
2519 +
2520 +       /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2521 +        * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2522 +        * So a two level scheme suffices for now. */
2523 +       sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2524 +                                   num_meta_group_infos, GFP_KERNEL);
2525 +       if (sbi->s_group_info == NULL) {
2526 +               printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2527 +               return -ENOMEM;
2528 +       }
2529 +       sbi->s_buddy_cache = new_inode(sb);
2530 +       if (sbi->s_buddy_cache == NULL) {
2531 +               printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2532 +               goto err_freesgi;
2533 +       }
2534 +       EXT3_I(sbi->s_buddy_cache)->i_disksize = 0;
2535 +
2536 +       metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2537 +       for (i = 0; i < num_meta_group_infos; i++) {
2538 +               if ((i + 1) == num_meta_group_infos)
2539 +                       metalen = sizeof(*meta_group_info) *
2540 +                               (sbi->s_groups_count -
2541 +                                       (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2542 +               meta_group_info = kmalloc(metalen, GFP_KERNEL);
2543 +               if (meta_group_info == NULL) {
2544 +                       printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2545 +                              "buddy group\n");
2546 +                       goto err_freemeta;
2547 +               }
2548 +               sbi->s_group_info[i] = meta_group_info;
2549 +       }
2550 +
2551 +       /*
2552 +        * calculate needed size. if change bb_counters size,
2553 +        * don't forget about ext3_mb_generate_buddy()
2554 +        */
2555 +       len = sizeof(struct ext3_group_info);
2556 +       len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2557 +       for (i = 0; i < sbi->s_groups_count; i++) {
2558 +               struct ext3_group_desc * desc;
2559 +
2560 +               meta_group_info =
2561 +                       sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2562 +               j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2563 +
2564 +               meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2565 +               if (meta_group_info[j] == NULL) {
2566 +                       printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2567 +                       i--;
2568 +                       goto err_freebuddy;
2569 +               }
2570 +               desc = ext3_get_group_desc(sb, i, NULL);
2571 +               if (desc == NULL) {
2572 +                       printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2573 +                       goto err_freebuddy;
2574 +               }
2575 +               memset(meta_group_info[j], 0, len);
2576 +               set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2577 +                       &meta_group_info[j]->bb_state);
2578 +
2579 +               /* initialize bb_free to be able to skip
2580 +                * empty groups without initialization */
2581 +               meta_group_info[j]->bb_free =
2582 +                       le16_to_cpu(desc->bg_free_blocks_count);
2583 +
2584 +               INIT_LIST_HEAD(&meta_group_info[j]->bb_prealloc_list);
2585 +
2586 +#ifdef DOUBLE_CHECK
2587 +               {
2588 +                       struct buffer_head *bh;
2589 +                       meta_group_info[j]->bb_bitmap =
2590 +                               kmalloc(sb->s_blocksize, GFP_KERNEL);
2591 +                       BUG_ON(meta_group_info[j]->bb_bitmap == NULL);
2592 +                       bh = read_block_bitmap(sb, i);
2593 +                       BUG_ON(bh == NULL);
2594 +                       memcpy(meta_group_info[j]->bb_bitmap, bh->b_data,
2595 +                                       sb->s_blocksize);
2596 +                       brelse(bh);
2597 +               }
2598 +#endif
2599 +
2600 +       }
2601 +
2602 +       return 0;
2603 +
2604 +err_freebuddy:
2605 +       while (i >= 0) {
2606 +               kfree(EXT3_GROUP_INFO(sb, i));
2607 +               i--;
2608 +       }
2609 +       i = num_meta_group_infos;
2610 +err_freemeta:
2611 +       while (--i >= 0)
2612 +               kfree(sbi->s_group_info[i]);
2613 +       iput(sbi->s_buddy_cache);
2614 +err_freesgi:
2615 +       kfree(sbi->s_group_info);
2616 +       return -ENOMEM;
2617 +}
2618 +
2619 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2620 +{
2621 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2622 +       unsigned i, offset, max;
2623 +
2624 +       if (!test_opt(sb, MBALLOC))
2625 +               return 0;
2626 +
2627 +       i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2628 +
2629 +       sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2630 +       if (sbi->s_mb_offsets == NULL) {
2631 +               clear_opt(sbi->s_mount_opt, MBALLOC);
2632 +               return -ENOMEM;
2633 +       }
2634 +       sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2635 +       if (sbi->s_mb_maxs == NULL) {
2636 +               clear_opt(sbi->s_mount_opt, MBALLOC);
2637 +               kfree(sbi->s_mb_maxs);
2638 +               return -ENOMEM;
2639 +       }
2640 +
2641 +       /* order 0 is regular bitmap */
2642 +       sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2643 +       sbi->s_mb_offsets[0] = 0;
2644 +
2645 +       i = 1;
2646 +       offset = 0;
2647 +       max = sb->s_blocksize << 2;
2648 +       do {
2649 +               sbi->s_mb_offsets[i] = offset;
2650 +               sbi->s_mb_maxs[i] = max;
2651 +               offset += 1 << (sb->s_blocksize_bits - i);
2652 +               max = max >> 1;
2653 +               i++;
2654 +       } while (i <= sb->s_blocksize_bits + 1);
2655 +
2656 +       /* init file for buddy data */
2657 +       if ((i = ext3_mb_init_backend(sb))) {
2658 +               clear_opt(sbi->s_mount_opt, MBALLOC);
2659 +               kfree(sbi->s_mb_offsets);
2660 +               kfree(sbi->s_mb_maxs);
2661 +               return i;
2662 +       }
2663 +
2664 +       spin_lock_init(&sbi->s_md_lock);
2665 +       INIT_LIST_HEAD(&sbi->s_active_transaction);
2666 +       INIT_LIST_HEAD(&sbi->s_closed_transaction);
2667 +       INIT_LIST_HEAD(&sbi->s_committed_transaction);
2668 +       spin_lock_init(&sbi->s_bal_lock);
2669 +
2670 +       sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2671 +       sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2672 +       sbi->s_mb_max_groups_to_scan = MB_DEFAULT_MAX_GROUPS_TO_SCAN;
2673 +       sbi->s_mb_stats = MB_DEFAULT_STATS;
2674 +       sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2675 +       sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2676 +       sbi->s_mb_history_filter = EXT3_MB_HISTORY_DEFAULT;
2677 +
2678 +       i = sizeof(struct ext3_locality_group) * num_possible_cpus();
2679 +       sbi->s_locality_groups = kmalloc(i, GFP_NOFS);
2680 +       if (sbi->s_locality_groups == NULL) {
2681 +               clear_opt(sbi->s_mount_opt, MBALLOC);
2682 +               kfree(sbi->s_mb_offsets);
2683 +               kfree(sbi->s_mb_maxs);
2684 +               return -ENOMEM;
2685 +       }
2686 +       for (i = 0; i < num_possible_cpus(); i++) {
2687 +               struct ext3_locality_group *lg;
2688 +               lg = &sbi->s_locality_groups[i];
2689 +               sema_init(&lg->lg_sem, 1);
2690 +               INIT_LIST_HEAD(&lg->lg_prealloc_list);
2691 +               spin_lock_init(&lg->lg_prealloc_lock);
2692 +       }
2693 +
2694 +       ext3_mb_init_per_dev_proc(sb);
2695 +       ext3_mb_history_init(sb);
2696 +
2697 +       printk("EXT3-fs: mballoc enabled\n");
2698 +       return 0;
2699 +}
2700 +
2701 +void ext3_mb_cleanup_pa(struct ext3_group_info *grp)
2702 +{
2703 +       struct ext3_prealloc_space *pa;
2704 +       struct list_head *cur, *tmp;
2705 +       int count = 0;
2706 +
2707 +       list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2708 +               pa = list_entry(cur, struct ext3_prealloc_space, pa_group_list);
2709 +               list_del_rcu(&pa->pa_group_list);
2710 +               count++;
2711 +               kfree(pa);
2712 +       }
2713 +       if (count)
2714 +               mb_debug("mballoc: %u PAs left\n", count);
2715 +
2716 +}
2717 +
2718 +int ext3_mb_release(struct super_block *sb)
2719 +{
2720 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2721 +       int i, num_meta_group_infos;
2722 +
2723 +       if (!test_opt(sb, MBALLOC))
2724 +               return 0;
2725 +
2726 +       /* release freed, non-committed blocks */
2727 +       spin_lock(&sbi->s_md_lock);
2728 +       list_splice_init(&sbi->s_closed_transaction,
2729 +                       &sbi->s_committed_transaction);
2730 +       list_splice_init(&sbi->s_active_transaction,
2731 +                       &sbi->s_committed_transaction);
2732 +       spin_unlock(&sbi->s_md_lock);
2733 +       ext3_mb_free_committed_blocks(sb);
2734 +
2735 +       if (sbi->s_group_info) {
2736 +               for (i = 0; i < sbi->s_groups_count; i++) {
2737 +#ifdef DOUBLE_CHECK
2738 +                       if (EXT3_GROUP_INFO(sb, i)->bb_bitmap)
2739 +                               kfree(EXT3_GROUP_INFO(sb, i)->bb_bitmap);
2740 +#endif
2741 +                       ext3_mb_cleanup_pa(EXT3_GROUP_INFO(sb, i));
2742 +                       kfree(EXT3_GROUP_INFO(sb, i));
2743 +               }
2744 +               num_meta_group_infos = (sbi->s_groups_count +
2745 +                               EXT3_DESC_PER_BLOCK(sb) - 1) >>
2746 +                       EXT3_DESC_PER_BLOCK_BITS(sb);
2747 +               for (i = 0; i < num_meta_group_infos; i++)
2748 +                       kfree(sbi->s_group_info[i]);
2749 +               kfree(sbi->s_group_info);
2750 +       }
2751 +       if (sbi->s_mb_offsets)
2752 +               kfree(sbi->s_mb_offsets);
2753 +       if (sbi->s_mb_maxs)
2754 +               kfree(sbi->s_mb_maxs);
2755 +       if (sbi->s_buddy_cache)
2756 +               iput(sbi->s_buddy_cache);
2757 +       if (sbi->s_mb_stats) {
2758 +               printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2759 +                               atomic_read(&sbi->s_bal_allocated),
2760 +                               atomic_read(&sbi->s_bal_reqs),
2761 +                               atomic_read(&sbi->s_bal_success));
2762 +               printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2763 +                               "%u 2^N hits, %u breaks, %u lost\n",
2764 +                               atomic_read(&sbi->s_bal_ex_scanned),
2765 +                               atomic_read(&sbi->s_bal_goals),
2766 +                               atomic_read(&sbi->s_bal_2orders),
2767 +                               atomic_read(&sbi->s_bal_breaks),
2768 +                               atomic_read(&sbi->s_mb_lost_chunks));
2769 +               printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2770 +                               sbi->s_mb_buddies_generated++,
2771 +                               sbi->s_mb_generation_time);
2772 +               printk("EXT3-fs: mballoc: %u preallocated, %u discarded\n",
2773 +                               atomic_read(&sbi->s_mb_preallocated),
2774 +                               atomic_read(&sbi->s_mb_discarded));
2775 +       }
2776 +
2777 +       if (sbi->s_locality_groups)
2778 +               kfree(sbi->s_locality_groups);
2779 +
2780 +       ext3_mb_history_release(sb);
2781 +       ext3_mb_destroy_per_dev_proc(sb);
2782 +
2783 +       return 0;
2784 +}
2785 +
2786 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2787 +{
2788 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
2789 +       int err, i, count = 0, count2 = 0;
2790 +       struct ext3_free_metadata *md;
2791 +       struct ext3_buddy e3b;
2792 +
2793 +       if (list_empty(&sbi->s_committed_transaction))
2794 +               return;
2795 +
2796 +       /* there is committed blocks to be freed yet */
2797 +       do {
2798 +               /* get next array of blocks */
2799 +               md = NULL;
2800 +               spin_lock(&sbi->s_md_lock);
2801 +               if (!list_empty(&sbi->s_committed_transaction)) {
2802 +                       md = list_entry(sbi->s_committed_transaction.next,
2803 +                                       struct ext3_free_metadata, list);
2804 +                       list_del(&md->list);
2805 +               }
2806 +               spin_unlock(&sbi->s_md_lock);
2807 +
2808 +               if (md == NULL)
2809 +                       break;
2810 +
2811 +               mb_debug("gonna free %u blocks in group %u (0x%p):",
2812 +                               md->num, md->group, md);
2813 +
2814 +               err = ext3_mb_load_buddy(sb, md->group, &e3b);
2815 +               /* we expect to find existing buddy because it's pinned */
2816 +               BUG_ON(err != 0);
2817 +
2818 +               /* there are blocks to put in buddy to make them really free */
2819 +               count += md->num;
2820 +               count2++;
2821 +               ext3_lock_group(sb, md->group);
2822 +               for (i = 0; i < md->num; i++) {
2823 +                       mb_debug(" %u", md->blocks[i]);
2824 +                       err = mb_free_blocks(NULL, &e3b, md->blocks[i], 1);
2825 +                       BUG_ON(err != 0);
2826 +               }
2827 +               mb_debug("\n");
2828 +               ext3_unlock_group(sb, md->group);
2829 +
2830 +               /* balance refcounts from ext3_mb_free_metadata() */
2831 +               page_cache_release(e3b.bd_buddy_page);
2832 +               page_cache_release(e3b.bd_bitmap_page);
2833 +
2834 +               kfree(md);
2835 +               ext3_mb_release_desc(&e3b);
2836 +
2837 +       } while (md);
2838 +
2839 +       mb_debug("freed %u blocks in %u structures\n", count, count2);
2840 +}
2841 +
2842 +#define EXT3_MB_STATS_NAME             "stats"
2843 +#define EXT3_MB_MAX_TO_SCAN_NAME       "max_to_scan"
2844 +#define EXT3_MB_MIN_TO_SCAN_NAME       "min_to_scan"
2845 +#define EXT3_MB_ORDER2_REQ             "order2_req"
2846 +#define EXT3_MB_STREAM_REQ             "stream_req"
2847 +
2848 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2849 +               int count, int *eof, void *data)
2850 +{
2851 +       struct ext3_sb_info *sbi = data;
2852 +       int len;
2853 +
2854 +       *eof = 1;
2855 +       if (off != 0)
2856 +               return 0;
2857 +
2858 +       len = sprintf(page, "%ld\n", sbi->s_mb_stats);
2859 +       *start = page;
2860 +       return len;
2861 +}
2862 +
2863 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2864 +               unsigned long count, void *data)
2865 +{
2866 +       struct ext3_sb_info *sbi = data;
2867 +       char str[32];
2868 +
2869 +       if (count >= sizeof(str)) {
2870 +               printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2871 +                               EXT3_MB_STATS_NAME, (int)sizeof(str));
2872 +               return -EOVERFLOW;
2873 +       }
2874 +
2875 +       if (copy_from_user(str, buffer, count))
2876 +               return -EFAULT;
2877 +
2878 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2879 +       sbi->s_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2880 +       return count;
2881 +}
2882 +
2883 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2884 +               int count, int *eof, void *data)
2885 +{
2886 +       struct ext3_sb_info *sbi = data;
2887 +       int len;
2888 +
2889 +       *eof = 1;
2890 +       if (off != 0)
2891 +               return 0;
2892 +
2893 +       len = sprintf(page, "%ld\n", sbi->s_mb_max_to_scan);
2894 +       *start = page;
2895 +       return len;
2896 +}
2897 +
2898 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2899 +               unsigned long count, void *data)
2900 +{
2901 +       struct ext3_sb_info *sbi = data;
2902 +       char str[32];
2903 +       long value;
2904 +
2905 +       if (count >= sizeof(str)) {
2906 +               printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2907 +                               EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2908 +               return -EOVERFLOW;
2909 +       }
2910 +
2911 +       if (copy_from_user(str, buffer, count))
2912 +               return -EFAULT;
2913 +
2914 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2915 +       value = simple_strtol(str, NULL, 0);
2916 +       if (value <= 0)
2917 +               return -ERANGE;
2918 +
2919 +       sbi->s_mb_max_to_scan = value;
2920 +
2921 +       return count;
2922 +}
2923 +
2924 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2925 +               int count, int *eof, void *data)
2926 +{
2927 +       struct ext3_sb_info *sbi = data;
2928 +       int len;
2929 +
2930 +       *eof = 1;
2931 +       if (off != 0)
2932 +               return 0;
2933 +
2934 +       len = sprintf(page, "%ld\n", sbi->s_mb_min_to_scan);
2935 +       *start = page;
2936 +       return len;
2937 +}
2938 +
2939 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2940 +               unsigned long count, void *data)
2941 +{
2942 +       struct ext3_sb_info *sbi = data;
2943 +       char str[32];
2944 +       long value;
2945 +
2946 +       if (count >= sizeof(str)) {
2947 +               printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2948 +                               EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2949 +               return -EOVERFLOW;
2950 +       }
2951 +
2952 +       if (copy_from_user(str, buffer, count))
2953 +               return -EFAULT;
2954 +
2955 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2956 +       value = simple_strtol(str, NULL, 0);
2957 +       if (value <= 0)
2958 +               return -ERANGE;
2959 +
2960 +       sbi->s_mb_order2_reqs = value;
2961 +
2962 +       return count;
2963 +}
2964 +
2965 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2966 +               int count, int *eof, void *data)
2967 +{
2968 +       struct ext3_sb_info *sbi = data;
2969 +       int len;
2970 +
2971 +       *eof = 1;
2972 +       if (off != 0)
2973 +               return 0;
2974 +
2975 +       len = sprintf(page, "%ld\n", sbi->s_mb_order2_reqs);
2976 +       *start = page;
2977 +       return len;
2978 +}
2979 +
2980 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2981 +               unsigned long count, void *data)
2982 +{
2983 +       struct ext3_sb_info *sbi = data;
2984 +       char str[32];
2985 +       long value;
2986 +
2987 +       if (count >= sizeof(str)) {
2988 +               printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2989 +                               EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2990 +               return -EOVERFLOW;
2991 +       }
2992 +
2993 +       if (copy_from_user(str, buffer, count))
2994 +               return -EFAULT;
2995 +
2996 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2997 +       value = simple_strtol(str, NULL, 0);
2998 +       if (value <= 0)
2999 +               return -ERANGE;
3000 +
3001 +       sbi->s_mb_min_to_scan = value;
3002 +
3003 +       return count;
3004 +}
3005 +
3006 +static int ext3_mb_stream_req_read(char *page, char **start, off_t off,
3007 +               int count, int *eof, void *data)
3008 +{
3009 +       struct ext3_sb_info *sbi = data;
3010 +       int len;
3011 +
3012 +       *eof = 1;
3013 +       if (off != 0)
3014 +               return 0;
3015 +
3016 +       len = sprintf(page, "%ld\n", sbi->s_mb_stream_request);
3017 +       *start = page;
3018 +       return len;
3019 +}
3020 +
3021 +static int ext3_mb_stream_req_write(struct file *file, const char *buffer,
3022 +               unsigned long count, void *data)
3023 +{
3024 +       struct ext3_sb_info *sbi = data;
3025 +       char str[32];
3026 +       long value;
3027 +
3028 +       if (count >= sizeof(str)) {
3029 +               printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
3030 +                               EXT3_MB_STREAM_REQ, (int)sizeof(str));
3031 +               return -EOVERFLOW;
3032 +       }
3033 +
3034 +       if (copy_from_user(str, buffer, count))
3035 +               return -EFAULT;
3036 +
3037 +       /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
3038 +       value = simple_strtol(str, NULL, 0);
3039 +       if (value <= 0)
3040 +               return -ERANGE;
3041 +
3042 +       sbi->s_mb_stream_request = value;
3043 +
3044 +       return count;
3045 +}
3046 +
3047 +int ext3_mb_init_per_dev_proc(struct super_block *sb)
3048 +{
3049 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
3050 +       mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
3051 +       struct proc_dir_entry *proc;
3052 +       char *name;
3053 +
3054 +       name = EXT3_MB_STATS_NAME;
3055 +       proc = create_proc_entry(name, mode, sbi->s_dev_proc);
3056 +       if (proc == NULL)
3057 +               goto err_out;
3058 +       proc->data = sbi;
3059 +       proc->read_proc  = ext3_mb_stats_read;
3060 +       proc->write_proc = ext3_mb_stats_write;
3061 +
3062 +       name = EXT3_MB_MAX_TO_SCAN_NAME;
3063 +       proc = create_proc_entry(name, mode, sbi->s_dev_proc);
3064 +       if (proc == NULL)
3065 +               goto err_out;
3066 +       proc->data = sbi;
3067 +       proc->read_proc  = ext3_mb_max_to_scan_read;
3068 +       proc->write_proc = ext3_mb_max_to_scan_write;
3069 +
3070 +       name = EXT3_MB_MIN_TO_SCAN_NAME;
3071 +       proc = create_proc_entry(name, mode, sbi->s_dev_proc);
3072 +       if (proc == NULL)
3073 +               goto err_out;
3074 +       proc->data = sbi;
3075 +       proc->read_proc  = ext3_mb_min_to_scan_read;
3076 +       proc->write_proc = ext3_mb_min_to_scan_write;
3077 +
3078 +       name = EXT3_MB_ORDER2_REQ;
3079 +       proc = create_proc_entry(name, mode, sbi->s_dev_proc);
3080 +       if (proc == NULL)
3081 +               goto err_out;
3082 +       proc->data = sbi;
3083 +       proc->read_proc  = ext3_mb_order2_req_read;
3084 +       proc->write_proc = ext3_mb_order2_req_write;
3085 +
3086 +       name = EXT3_MB_STREAM_REQ;
3087 +       proc = create_proc_entry(name, mode, sbi->s_dev_proc);
3088 +       if (proc == NULL)
3089 +               goto err_out;
3090 +       proc->data = sbi;
3091 +       proc->read_proc  = ext3_mb_stream_req_read;
3092 +       proc->write_proc = ext3_mb_stream_req_write;
3093 +
3094 +       return 0;
3095 +
3096 +err_out:
3097 +       printk(KERN_ERR "EXT3-fs: Unable to create %s\n", name);
3098 +       remove_proc_entry(EXT3_MB_STREAM_REQ, sbi->s_dev_proc);
3099 +       remove_proc_entry(EXT3_MB_ORDER2_REQ, sbi->s_dev_proc);
3100 +       remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, sbi->s_dev_proc);
3101 +       remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, sbi->s_dev_proc);
3102 +       remove_proc_entry(EXT3_MB_STATS_NAME, sbi->s_dev_proc);
3103 +
3104 +       return -ENOMEM;
3105 +}
3106 +
3107 +int ext3_mb_destroy_per_dev_proc(struct super_block *sb)
3108 +{
3109 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
3110 +
3111 +       if (sbi->s_dev_proc == NULL)
3112 +               return -EINVAL;
3113 +
3114 +       remove_proc_entry(EXT3_MB_STREAM_REQ, sbi->s_dev_proc);
3115 +       remove_proc_entry(EXT3_MB_ORDER2_REQ, sbi->s_dev_proc);
3116 +       remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, sbi->s_dev_proc);
3117 +       remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, sbi->s_dev_proc);
3118 +       remove_proc_entry(EXT3_MB_STATS_NAME, sbi->s_dev_proc);
3119 +
3120 +       return 0;
3121 +}
3122 +
3123 +int __init init_ext3_mb_proc(void)
3124 +{
3125 +       ext3_pspace_cachep =
3126 +               kmem_cache_create("ext3_prealloc_space",
3127 +                                    sizeof(struct ext3_prealloc_space),
3128 +                                    0, SLAB_RECLAIM_ACCOUNT, NULL, NULL);
3129 +       if (ext3_pspace_cachep == NULL)
3130 +               return -ENOMEM;
3131 +
3132 +       return 0;
3133 +}
3134 +
3135 +void exit_ext3_mb_proc(void)
3136 +{
3137 +       /* XXX: synchronize_rcu(); */
3138 +       kmem_cache_destroy(ext3_pspace_cachep);
3139 +}
3140 +
3141 +
3142 +/*
3143 + * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
3144 + * Returns 0 if success or error code
3145 + */
3146 +int ext3_mb_mark_diskspace_used(struct ext3_allocation_context *ac, handle_t *handle)
3147 +{
3148 +       struct buffer_head *bitmap_bh = NULL;
3149 +       struct ext3_super_block *es;
3150 +       struct ext3_group_desc *gdp;
3151 +       struct buffer_head *gdp_bh;
3152 +       struct ext3_sb_info *sbi;
3153 +       struct super_block *sb;
3154 +       sector_t block;
3155 +       int err;
3156 +
3157 +       BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3158 +       BUG_ON(ac->ac_b_ex.fe_len <= 0);
3159 +
3160 +       sb = ac->ac_sb;
3161 +       sbi = EXT3_SB(sb);
3162 +       es = sbi->s_es;
3163 +
3164 +       ext3_debug("using block group %d(%d)\n", ac->ac_b_group.group,
3165 +                       gdp->bg_free_blocks_count);
3166 +
3167 +       err = -EIO;
3168 +       bitmap_bh = read_block_bitmap(sb, ac->ac_b_ex.fe_group);
3169 +       if (!bitmap_bh)
3170 +               goto out_err;
3171 +
3172 +       err = ext3_journal_get_write_access(handle, bitmap_bh);
3173 +       if (err)
3174 +               goto out_err;
3175 +
3176 +       err = -EIO;
3177 +       gdp = ext3_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
3178 +       if (!gdp)
3179 +               goto out_err;
3180 +
3181 +       err = ext3_journal_get_write_access(handle, gdp_bh);
3182 +       if (err)
3183 +               goto out_err;
3184 +
3185 +       block = ac->ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
3186 +               + ac->ac_b_ex.fe_start
3187 +               + le32_to_cpu(es->s_first_data_block);
3188 +
3189 +       if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
3190 +                       block == le32_to_cpu(gdp->bg_inode_bitmap) ||
3191 +                       in_range(block, le32_to_cpu(gdp->bg_inode_table),
3192 +                               EXT3_SB(sb)->s_itb_per_group))
3193 +               ext3_error(sb, __FUNCTION__,
3194 +                          "Allocating block in system zone - block = %lu",
3195 +                          (unsigned long) block);
3196 +#ifdef AGGRESSIVE_CHECK
3197 +       {
3198 +               int i;
3199 +               for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3200 +                       BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3201 +                                               bitmap_bh->b_data));
3202 +               }
3203 +       }
3204 +#endif
3205 +       mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
3206 +                   ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
3207 +
3208 +       spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3209 +       gdp->bg_free_blocks_count =
3210 +               cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
3211 +                               - ac->ac_b_ex.fe_len);
3212 +       spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3213 +       percpu_counter_mod(&sbi->s_freeblocks_counter, - ac->ac_b_ex.fe_len);
3214 +
3215 +       err = ext3_journal_dirty_metadata(handle, bitmap_bh);
3216 +       if (err)
3217 +               goto out_err;
3218 +       err = ext3_journal_dirty_metadata(handle, gdp_bh);
3219 +
3220 +out_err:
3221 +       sb->s_dirt = 1;
3222 +       brelse(bitmap_bh);
3223 +       return err;
3224 +}
3225 +
3226 +/*
3227 + * here we normalize request for locality group
3228 + * XXX: should we try to preallocate more than the group has now?
3229 + */
3230 +void ext3_mb_normalize_group_request(struct ext3_allocation_context *ac)
3231 +{
3232 +       struct super_block *sb = ac->ac_sb;
3233 +       struct ext3_locality_group *lg = ac->ac_lg;
3234 +
3235 +       BUG_ON(lg == NULL);
3236 +       if (EXT3_SB(sb)->s_stripe)
3237 +               ac->ac_g_ex.fe_len = EXT3_SB(sb)->s_stripe;
3238 +       else
3239 +               ac->ac_g_ex.fe_len = (1024 * 1024) >> sb->s_blocksize_bits;
3240 +
3241 +       mb_debug("#%u: goal %u blocks for locality group\n",
3242 +               current->pid, ac->ac_g_ex.fe_len);
3243 +}
3244 +
3245 +/*
3246 + * Normalization means making request better in terms of
3247 + * size and alignment
3248 + */
3249 +void ext3_mb_normalize_request(struct ext3_allocation_context *ac,
3250 +                               struct ext3_allocation_request *ar)
3251 +{
3252 +       struct ext3_inode_info *ei = EXT3_I(ac->ac_inode);
3253 +       loff_t start, end, size, orig_size, orig_start;
3254 +       struct list_head *cur;
3255 +       int bsbits, max;
3256 +
3257 +       /* do normalize only data requests, metadata requests
3258 +          do not need preallocation */
3259 +       if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
3260 +               return;
3261 +
3262 +       /* sometime caller may want exact blocks */
3263 +       if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
3264 +               return;
3265 +
3266 +       /* caller may indicate that preallocation isn't
3267 +        * required (it's a tail, for example) */
3268 +       if (ac->ac_flags & EXT3_MB_HINT_NOPREALLOC)
3269 +               return;
3270 +
3271 +       if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
3272 +               return ext3_mb_normalize_group_request(ac);
3273 +
3274 +       bsbits = ac->ac_sb->s_blocksize_bits;
3275 +
3276 +       /* first, let's learn actual file size
3277 +        * given current request is allocated */
3278 +       size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3279 +       size = size << bsbits;
3280 +       if (size < i_size_read(ac->ac_inode))
3281 +               size = i_size_read(ac->ac_inode);
3282 +
3283 +       /* max available blocks in a free group */
3284 +       max = EXT3_BLOCKS_PER_GROUP(ac->ac_sb) - 1 - 1
3285 +               - EXT3_SB(ac->ac_sb)->s_itb_per_group;
3286 +
3287 +#define NRL_CHECK_SIZE(req,size,max,bits)      \
3288 +               (req <= (size) || max <= ((size) >> bits))
3289 +
3290 +       /* first, try to predict filesize */
3291 +       /* XXX: should this table be tunable? */
3292 +       start = 0;
3293 +       if (size <= 16 * 1024) {
3294 +               size = 16 * 1024;
3295 +       } else if (size <= 32 * 1024) {
3296 +               size = 32 * 1024;
3297 +       } else if (size <= 64 * 1024) {
3298 +               size = 64 * 1024;
3299 +       } else if (size <= 128 * 1024) {
3300 +               size = 128 * 1024;
3301 +       } else if (size <= 256 * 1024) {
3302 +               size = 256 * 1024;
3303 +       } else if (size <= 512 * 1024) {
3304 +               size = 512 * 1024;
3305 +       } else if (size <= 1024 * 1024) {
3306 +               size = 1024 * 1024;
3307 +       } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, bsbits)) {
3308 +               start = ac->ac_o_ex.fe_logical << bsbits;
3309 +               start = (start / (1024 * 1024)) * (1024 * 1024);
3310 +               size = 1024 * 1024;
3311 +       } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, bsbits)) {
3312 +               start = ac->ac_o_ex.fe_logical << bsbits;
3313 +               start = (start / (4 * (1024 * 1024))) * 4 * (1024 * 1024);
3314 +               size = 4 * 1024 * 1024;
3315 +       } else if(NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,(8<<20)>>bsbits,max,bsbits)){
3316 +               start = ac->ac_o_ex.fe_logical;
3317 +               start = start << bsbits;
3318 +               start = (start / (8 * (1024 * 1024))) * 8 * (1024 * 1024);
3319 +               size = 8 * 1024 * 1024;
3320 +       } else {
3321 +               start = ac->ac_o_ex.fe_logical;
3322 +               start = start << bsbits;
3323 +               size = ac->ac_o_ex.fe_len << bsbits;
3324 +       }
3325 +       orig_size = size = size >> bsbits;
3326 +       orig_start = start = start >> bsbits;
3327 +
3328 +       /* don't cover already allocated blocks in selected range */
3329 +       if (ar->pleft && start <= ar->lleft) {
3330 +               size -= ar->lleft + 1 - start;
3331 +               start = ar->lleft + 1;
3332 +       }
3333 +       if (ar->pright && start + size - 1 >= ar->lright)
3334 +               size -= start + size - ar->lright;
3335 +
3336 +       end = start + size;
3337 +
3338 +       /* check we don't cross already preallocated blocks */
3339 +       rcu_read_lock();
3340 +       list_for_each_rcu(cur, &ei->i_prealloc_list) {
3341 +               struct ext3_prealloc_space *pa;
3342 +               unsigned long pa_end;
3343 +
3344 +               pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3345 +
3346 +               if (pa->pa_deleted)
3347 +                       continue;
3348 +               spin_lock(&pa->pa_lock);
3349 +               if (pa->pa_deleted) {
3350 +                       spin_unlock(&pa->pa_lock);
3351 +                       continue;
3352 +               }
3353 +
3354 +               pa_end = pa->pa_lstart + pa->pa_len;
3355 +
3356 +               /* PA must not overlap original request */
3357 +               BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3358 +                       ac->ac_o_ex.fe_logical < pa->pa_lstart));
3359 +
3360 +               /* skip PA normalized request doesn't overlap with */
3361 +               if (pa->pa_lstart >= end) {
3362 +                       spin_unlock(&pa->pa_lock);
3363 +                       continue;
3364 +               }
3365 +               if (pa_end <= start) {
3366 +                       spin_unlock(&pa->pa_lock);
3367 +                       continue;
3368 +               }
3369 +               BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3370 +
3371 +               if (pa_end <= ac->ac_o_ex.fe_logical) {
3372 +                       BUG_ON(pa_end < start);
3373 +                       start = pa_end;
3374 +               }
3375 +
3376 +               if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3377 +                       BUG_ON(pa->pa_lstart > end);
3378 +                       end = pa->pa_lstart;
3379 +               }
3380 +               spin_unlock(&pa->pa_lock);
3381 +       }
3382 +       rcu_read_unlock();
3383 +       size = end - start;
3384 +
3385 +       /* XXX: extra loop to check we really don't overlap preallocations */
3386 +       rcu_read_lock();
3387 +       list_for_each_rcu(cur, &ei->i_prealloc_list) {
3388 +               struct ext3_prealloc_space *pa;
3389 +               unsigned long pa_end;
3390 +               pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3391 +               spin_lock(&pa->pa_lock);
3392 +               if (pa->pa_deleted == 0) {
3393 +                       pa_end = pa->pa_lstart + pa->pa_len;
3394 +                       BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3395 +               }
3396 +               spin_unlock(&pa->pa_lock);
3397 +       }
3398 +       rcu_read_unlock();
3399 +
3400 +       if (start + size <= ac->ac_o_ex.fe_logical &&
3401 +                       start > ac->ac_o_ex.fe_logical) {
3402 +               printk("start %lu, size %lu, fe_logical %lu\n",
3403 +                       (unsigned long) start, (unsigned long) size,
3404 +                       (unsigned long) ac->ac_o_ex.fe_logical);
3405 +       }
3406 +       BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3407 +                       start > ac->ac_o_ex.fe_logical);
3408 +
3409 +       /* now prepare goal request */
3410 +       BUG_ON(size <= 0 || size >= EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
3411 +       if (size < ac->ac_o_ex.fe_len) {
3412 +               /* XXX: don't normalize tails? */
3413 +       }
3414 +
3415 +       /* XXX: is it better to align blocks WRT to logical placement
3416 +        * or satisfy big request as is */
3417 +       ac->ac_g_ex.fe_logical = start;
3418 +       ac->ac_g_ex.fe_len = size;
3419 +
3420 +       mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3421 +               (unsigned) orig_size, (unsigned) start);
3422 +}
3423 +
3424 +void ext3_mb_collect_stats(struct ext3_allocation_context *ac)
3425 +{
3426 +       struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
3427 +
3428 +       if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3429 +               atomic_inc(&sbi->s_bal_reqs);
3430 +               atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3431 +               if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3432 +                       atomic_inc(&sbi->s_bal_success);
3433 +               atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3434 +               if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3435 +                               ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3436 +                       atomic_inc(&sbi->s_bal_goals);
3437 +               if (ac->ac_found > sbi->s_mb_max_to_scan)
3438 +                       atomic_inc(&sbi->s_bal_breaks);
3439 +       }
3440 +
3441 +       ext3_mb_store_history(ac);
3442 +}
3443 +
3444 +/*
3445 + * use blocks preallocated to inode
3446 + */
3447 +void ext3_mb_use_inode_pa(struct ext3_allocation_context *ac,
3448 +                               struct ext3_prealloc_space *pa)
3449 +{
3450 +       unsigned long start, len;
3451 +
3452 +       /* found preallocated blocks, use them */
3453 +       start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3454 +       len = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3455 +       len = len - start;
3456 +       ext3_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3457 +                                       &ac->ac_b_ex.fe_start);
3458 +       ac->ac_b_ex.fe_len = len;
3459 +       ac->ac_status = AC_STATUS_FOUND;
3460 +       ac->ac_pa = pa;
3461 +
3462 +       BUG_ON(start < pa->pa_pstart);
3463 +       BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3464 +       BUG_ON(pa->pa_free < len);
3465 +       pa->pa_free -= len;
3466 +
3467 +       mb_debug("use %lu/%lu from inode pa %p\n", start, len, pa);
3468 +}
3469 +
3470 +/*
3471 + * use blocks preallocated to locality group
3472 + */
3473 +void ext3_mb_use_group_pa(struct ext3_allocation_context *ac,
3474 +                               struct ext3_prealloc_space *pa)
3475 +{
3476 +       unsigned len = ac->ac_o_ex.fe_len;
3477 +
3478 +       ext3_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3479 +                                       &ac->ac_b_ex.fe_group,
3480 +                                       &ac->ac_b_ex.fe_start);
3481 +       ac->ac_b_ex.fe_len = len;
3482 +       ac->ac_status = AC_STATUS_FOUND;
3483 +       ac->ac_pa = pa;
3484 +
3485 +       /* we don't correct pa_pstart or pa_plen here to avoid
3486 +        * possible race when tte group is being loaded concurrently
3487 +        * instead we correct pa later, after blocks are marked
3488 +        * in on-disk bitmap -- see ext3_mb_release_context() */
3489 +       mb_debug("use %lu/%lu from group pa %p\n", pa->pa_lstart-len, len, pa);
3490 +}
3491 +
3492 +/*
3493 + * search goal blocks in preallocated space
3494 + */
3495 +int ext3_mb_use_preallocated(struct ext3_allocation_context *ac)
3496 +{
3497 +       struct ext3_inode_info *ei = EXT3_I(ac->ac_inode);
3498 +       struct ext3_locality_group *lg;
3499 +       struct ext3_prealloc_space *pa;
3500 +       struct list_head *cur;
3501 +
3502 +       /* only data can be preallocated */
3503 +       if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
3504 +               return 0;
3505 +
3506 +       /* first, try per-file preallocation */
3507 +       rcu_read_lock();
3508 +       list_for_each_rcu(cur, &ei->i_prealloc_list) {
3509 +               pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3510 +
3511 +               /* all fields in this condition don't change,
3512 +                * so we can skip locking for them */
3513 +               if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3514 +                       ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3515 +                       continue;
3516 +
3517 +               /* found preallocated blocks, use them */
3518 +               spin_lock(&pa->pa_lock);
3519 +               if (pa->pa_deleted == 0 && pa->pa_free) {
3520 +                       atomic_inc(&pa->pa_count);
3521 +                       ext3_mb_use_inode_pa(ac, pa);
3522 +                       spin_unlock(&pa->pa_lock);
3523 +                       ac->ac_criteria = 10;
3524 +                       rcu_read_unlock();
3525 +                       return 1;
3526 +               }
3527 +               spin_unlock(&pa->pa_lock);
3528 +       }
3529 +       rcu_read_unlock();
3530 +
3531 +       /* can we use group allocation? */
3532 +       if (!(ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC))
3533 +               return 0;
3534 +
3535 +       /* inode may have no locality group for some reason */
3536 +       lg = ac->ac_lg;
3537 +       if (lg == NULL)
3538 +               return 0;
3539 +
3540 +       rcu_read_lock();
3541 +       list_for_each_rcu(cur, &lg->lg_prealloc_list) {
3542 +               pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3543 +               spin_lock(&pa->pa_lock);
3544 +               if (pa->pa_deleted == 0 && pa->pa_free >= ac->ac_o_ex.fe_len) {
3545 +                       atomic_inc(&pa->pa_count);
3546 +                       ext3_mb_use_group_pa(ac, pa);
3547 +                       spin_unlock(&pa->pa_lock);
3548 +                       ac->ac_criteria = 20;
3549 +                       rcu_read_unlock();
3550 +                       return 1;
3551 +               }
3552 +               spin_unlock(&pa->pa_lock);
3553 +       }
3554 +       rcu_read_unlock();
3555 +
3556 +       return 0;
3557 +}
3558 +
3559 +/*
3560 + * the function goes through all preallocation in this group and marks them
3561 + * used in in-core bitmap. buddy must be generated from this bitmap
3562 + */
3563 +void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group)
3564 +{
3565 +       struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
3566 +       struct ext3_prealloc_space *pa;
3567 +       struct list_head *cur;
3568 +       unsigned long groupnr;
3569 +       unsigned long start;
3570 +       int preallocated = 0, count = 0, len;
3571 +
3572 +       /* all form of preallocation discards first load group,
3573 +        * so the only competing code is preallocation use.
3574 +        * we don't need any locking here
3575 +        * notice we do NOT ignore preallocations with pa_deleted
3576 +        * otherwise we could leave used blocks available for
3577 +        * allocation in buddy when concurrent ext3_mb_put_pa()
3578 +        * is dropping preallocation
3579 +        */
3580 +       list_for_each_rcu(cur, &grp->bb_prealloc_list) {
3581 +               pa = list_entry(cur, struct ext3_prealloc_space, pa_group_list);
3582 +               spin_lock(&pa->pa_lock);
3583 +               ext3_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &start);
3584 +               len = pa->pa_len;
3585 +               spin_unlock(&pa->pa_lock);
3586 +               if (unlikely(len == 0))
3587 +                       continue;
3588 +               BUG_ON(groupnr != group && len != 0);
3589 +               mb_set_bits(sb_bgl_lock(EXT3_SB(sb), group), bitmap, start,len);
3590 +               preallocated += len;
3591 +               count++;
3592 +       }
3593 +       mb_debug("prellocated %u for group %u\n", preallocated, group);
3594 +}
3595 +
3596 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,5)
3597 +static void ext3_mb_pa_callback(struct rcu_head *head)
3598 +{
3599 +       struct ext3_prealloc_space *pa;
3600 +       pa = container_of(head, struct ext3_prealloc_space, u.pa_rcu);
3601 +       kmem_cache_free(ext3_pspace_cachep, pa);
3602 +}
3603 +#define mb_call_rcu(__pa)      call_rcu(&(__pa)->u.pa_rcu, ext3_mb_pa_callback)
3604 +#else
3605 +static void ext3_mb_pa_callback(void *pa)
3606 +{
3607 +       kmem_cache_free(ext3_pspace_cachep, pa);
3608 +}
3609 +#define mb_call_rcu(__pa)      call_rcu(&(__pa)->u.pa_rcu, ext3_mb_pa_callback, pa)
3610 +#endif
3611 +
3612 +/*
3613 + * drops a reference to preallocated space descriptor
3614 + * if this was the last reference and the space is consumed
3615 + */
3616 +void ext3_mb_put_pa(struct ext3_allocation_context *ac,
3617 +                       struct super_block *sb, struct ext3_prealloc_space *pa)
3618 +{
3619 +       unsigned long grp;
3620 +
3621 +       if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3622 +               return;
3623 +
3624 +       /* in this short window concurrent discard can set pa_deleted */
3625 +       spin_lock(&pa->pa_lock);
3626 +       if (pa->pa_deleted == 0) {
3627 +               spin_unlock(&pa->pa_lock);
3628 +               return;
3629 +       }
3630 +
3631 +       pa->pa_deleted = 1;
3632 +       spin_unlock(&pa->pa_lock);
3633 +
3634 +       /* -1 is to protect from crossing allocation group */
3635 +       ext3_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
3636 +
3637 +       /*
3638 +        * possible race:
3639 +        *
3640 +        *  P1 (buddy init)                     P2 (regular allocation)
3641 +        *                                      find block B in PA
3642 +        *  copy on-disk bitmap to buddy
3643 +        *                                      mark B in on-disk bitmap
3644 +        *                                      drop PA from group
3645 +        *  mark all PAs in buddy
3646 +        *
3647 +        * thus, P1 initializes buddy with B available. to prevent this
3648 +        * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3649 +        * against that pair
3650 +        */
3651 +       ext3_lock_group(sb, grp);
3652 +       list_del_rcu(&pa->pa_group_list);
3653 +       ext3_unlock_group(sb, grp);
3654 +
3655 +       spin_lock(pa->pa_obj_lock);
3656 +       list_del_rcu(&pa->pa_inode_list);
3657 +       spin_unlock(pa->pa_obj_lock);
3658 +
3659 +       mb_call_rcu(pa);
3660 +}
3661 +
3662 +/*
3663 + * creates new preallocated space for given inode
3664 + */
3665 +int ext3_mb_new_inode_pa(struct ext3_allocation_context *ac)
3666 +{
3667 +       struct super_block *sb = ac->ac_sb;
3668 +       struct ext3_prealloc_space *pa;
3669 +       struct ext3_group_info *grp;
3670 +       struct ext3_inode_info *ei;
3671 +
3672 +       /* preallocate only when found space is larger then requested */
3673 +       BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3674 +       BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3675 +       BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3676 +
3677 +       pa = kmem_cache_alloc(ext3_pspace_cachep, GFP_NOFS);
3678 +       if (pa == NULL)
3679 +               return -ENOMEM;
3680 +
3681 +       if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3682 +               int winl, wins, win, offs;
3683 +
3684 +               /* we can't allocate as much as normalizer wants.
3685 +                * so, found space must get proper lstart
3686 +                * to cover original request */
3687 +               BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3688 +               BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3689 +
3690 +               /* we're limited by original request in that
3691 +                * logical block must be covered any way
3692 +                * winl is window we can move our chunk within */
3693 +               winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3694 +
3695 +               /* also, we should cover whole original request */
3696 +               wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3697 +
3698 +               /* the smallest one defines real window */
3699 +               win = min(winl, wins);
3700 +
3701 +               offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3702 +               if (offs && offs < win)
3703 +                       win = offs;
3704 +
3705 +               ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3706 +               BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3707 +               BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3708 +       }
3709 +
3710 +       /* preallocation can change ac_b_ex, thus we store actually
3711 +        * allocated blocks for history */
3712 +       ac->ac_f_ex = ac->ac_b_ex;
3713 +
3714 +       pa->pa_lstart = ac->ac_b_ex.fe_logical;
3715 +       pa->pa_pstart = ext3_grp_offs_to_block(sb, &ac->ac_b_ex);
3716 +       pa->pa_len = ac->ac_b_ex.fe_len;
3717 +       pa->pa_free = pa->pa_len;
3718 +       atomic_set(&pa->pa_count, 1);
3719 +       spin_lock_init(&pa->pa_lock);
3720 +       pa->pa_deleted = 0;
3721 +       pa->pa_linear = 0;
3722 +
3723 +       mb_debug("new inode pa %p: %lu/%lu for %lu\n", pa,
3724 +                       pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3725 +
3726 +       ext3_mb_use_inode_pa(ac, pa);
3727 +       atomic_add(pa->pa_free, &EXT3_SB(sb)->s_mb_preallocated);
3728 +
3729 +       ei = EXT3_I(ac->ac_inode);
3730 +       grp = EXT3_GROUP_INFO(sb, ac->ac_b_ex.fe_group);
3731 +
3732 +       pa->pa_obj_lock = &ei->i_prealloc_lock;
3733 +       pa->pa_inode = ac->ac_inode;
3734 +
3735 +       ext3_lock_group(sb, ac->ac_b_ex.fe_group);
3736 +       list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
3737 +       ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
3738 +
3739 +       spin_lock(pa->pa_obj_lock);
3740 +       list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3741 +       spin_unlock(pa->pa_obj_lock);
3742 +
3743 +       return 0;
3744 +}
3745 +
3746 +/*
3747 + * creates new preallocated space for locality group inodes belongs to
3748 + */
3749 +int ext3_mb_new_group_pa(struct ext3_allocation_context *ac)
3750 +{
3751 +       struct super_block *sb = ac->ac_sb;
3752 +       struct ext3_locality_group *lg;
3753 +       struct ext3_prealloc_space *pa;
3754 +       struct ext3_group_info *grp;
3755 +
3756 +       /* preallocate only when found space is larger then requested */
3757 +       BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3758 +       BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3759 +       BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3760 +
3761 +       BUG_ON(ext3_pspace_cachep == NULL);
3762 +       pa = kmem_cache_alloc(ext3_pspace_cachep, GFP_NOFS);
3763 +       if (pa == NULL)
3764 +               return -ENOMEM;
3765 +
3766 +       /* preallocation can change ac_b_ex, thus we store actually
3767 +        * allocated blocks for history */
3768 +       ac->ac_f_ex = ac->ac_b_ex;
3769 +
3770 +       pa->pa_pstart = ext3_grp_offs_to_block(sb, &ac->ac_b_ex);
3771 +       pa->pa_lstart = pa->pa_pstart;
3772 +       pa->pa_len = ac->ac_b_ex.fe_len;
3773 +       pa->pa_free = pa->pa_len;
3774 +       atomic_set(&pa->pa_count, 1);
3775 +       spin_lock_init(&pa->pa_lock);
3776 +       pa->pa_deleted = 0;
3777 +       pa->pa_linear = 1;
3778 +
3779 +       mb_debug("new group pa %p: %lu/%lu for %lu\n", pa,
3780 +                       pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3781 +
3782 +       ext3_mb_use_group_pa(ac, pa);
3783 +       atomic_add(pa->pa_free, &EXT3_SB(sb)->s_mb_preallocated);
3784 +
3785 +       grp = EXT3_GROUP_INFO(sb, ac->ac_b_ex.fe_group);
3786 +       lg = ac->ac_lg;
3787 +       BUG_ON(lg == NULL);
3788 +
3789 +       pa->pa_obj_lock = &lg->lg_prealloc_lock;
3790 +       pa->pa_inode = NULL;
3791 +
3792 +       ext3_lock_group(sb, ac->ac_b_ex.fe_group);
3793 +       list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
3794 +       ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
3795 +
3796 +       spin_lock(pa->pa_obj_lock);
3797 +       list_add_tail_rcu(&pa->pa_inode_list, &lg->lg_prealloc_list);
3798 +       spin_unlock(pa->pa_obj_lock);
3799 +
3800 +       return 0;
3801 +}
3802 +
3803 +int ext3_mb_new_preallocation(struct ext3_allocation_context *ac)
3804 +{
3805 +       int err;
3806 +
3807 +       if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
3808 +               err = ext3_mb_new_group_pa(ac);
3809 +       else
3810 +               err = ext3_mb_new_inode_pa(ac);
3811 +       return err;
3812 +}
3813 +
3814 +/*
3815 + * finds all unused blocks in on-disk bitmap, frees them in
3816 + * in-core bitmap and buddy.
3817 + * @pa must be unlinked from inode and group lists, so that
3818 + * nobody else can find/use it.
3819 + * the caller MUST hold group/inode locks.
3820 + * TODO: optimize the case when there are no in-core structures yet
3821 + */
3822 +int ext3_mb_release_inode_pa(struct ext3_buddy *e3b,
3823 +                               struct buffer_head *bitmap_bh,
3824 +                               struct ext3_prealloc_space *pa)
3825 +{
3826 +       struct ext3_allocation_context ac;
3827 +       struct super_block *sb = e3b->bd_sb;
3828 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
3829 +       unsigned long bit, end, next, group;
3830 +       sector_t start;
3831 +       int err = 0, free = 0;
3832 +
3833 +       BUG_ON(pa->pa_deleted == 0);
3834 +       ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3835 +       BUG_ON(group != e3b->bd_group && pa->pa_len != 0);
3836 +       end = bit + pa->pa_len;
3837 +
3838 +       ac.ac_sb = sb;
3839 +       ac.ac_inode = pa->pa_inode;
3840 +       ac.ac_op = EXT3_MB_HISTORY_DISCARD;
3841 +
3842 +       while (bit < end) {
3843 +               bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
3844 +               if (bit >= end)
3845 +                       break;
3846 +               next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
3847 +               if (next > end)
3848 +                       next = end;
3849 +               start = group * EXT3_BLOCKS_PER_GROUP(sb) + bit +
3850 +                               le32_to_cpu(sbi->s_es->s_first_data_block);
3851 +               mb_debug("    free preallocated %u/%u in group %u\n",
3852 +                               (unsigned) start, (unsigned) next - bit,
3853 +                               (unsigned) group);
3854 +               free += next - bit;
3855 +
3856 +               ac.ac_b_ex.fe_group = group;
3857 +               ac.ac_b_ex.fe_start = bit;
3858 +               ac.ac_b_ex.fe_len = next - bit;
3859 +               ac.ac_b_ex.fe_logical = 0;
3860 +               ext3_mb_store_history(&ac);
3861 +
3862 +               mb_free_blocks(pa->pa_inode, e3b, bit, next - bit);
3863 +               bit = next + 1;
3864 +       }
3865 +       if (free != pa->pa_free) {
3866 +               printk("pa %p: logic %lu, phys. %lu, len %lu\n",
3867 +                       pa, (unsigned long) pa->pa_lstart,
3868 +                       (unsigned long) pa->pa_pstart,
3869 +                       (unsigned long) pa->pa_len);
3870 +               printk("free %u, pa_free %u\n", free, pa->pa_free);
3871 +       }
3872 +       BUG_ON(free != pa->pa_free);
3873 +       atomic_add(free, &sbi->s_mb_discarded);
3874 +
3875 +       return err;
3876 +}
3877 +
3878 +int ext3_mb_release_group_pa(struct ext3_buddy *e3b,
3879 +                               struct ext3_prealloc_space *pa)
3880 +{
3881 +       struct ext3_allocation_context ac;
3882 +       struct super_block *sb = e3b->bd_sb;
3883 +       unsigned long bit, group;
3884 +
3885 +       ac.ac_op = EXT3_MB_HISTORY_DISCARD;
3886 +
3887 +       BUG_ON(pa->pa_deleted == 0);
3888 +       ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3889 +       BUG_ON(group != e3b->bd_group && pa->pa_len != 0);
3890 +       mb_free_blocks(pa->pa_inode, e3b, bit, pa->pa_len);
3891 +       atomic_add(pa->pa_len, &EXT3_SB(sb)->s_mb_discarded);
3892 +
3893 +       ac.ac_sb = sb;
3894 +       ac.ac_inode = NULL;
3895 +       ac.ac_b_ex.fe_group = group;
3896 +       ac.ac_b_ex.fe_start = bit;
3897 +       ac.ac_b_ex.fe_len = pa->pa_len;
3898 +       ac.ac_b_ex.fe_logical = 0;
3899 +       ext3_mb_store_history(&ac);
3900 +
3901 +       return 0;
3902 +}
3903 +
3904 +/*
3905 + * releases all preallocations in given group
3906 + *
3907 + * first, we need to decide discard policy:
3908 + * - when do we discard
3909 + *   1) ENOSPC
3910 + * - how many do we discard
3911 + *   1) how many requested
3912 + */
3913 +int ext3_mb_discard_group_preallocations(struct super_block *sb,
3914 +                                               int group, int needed)
3915 +{
3916 +       struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
3917 +       struct buffer_head *bitmap_bh = NULL;
3918 +       struct ext3_prealloc_space *pa, *tmp;
3919 +       struct list_head list;
3920 +       struct ext3_buddy e3b;
3921 +       int err, busy, free = 0;
3922 +
3923 +       mb_debug("discard preallocation for group %lu\n", group);
3924 +
3925 +       if (list_empty(&grp->bb_prealloc_list))
3926 +               return 0;
3927 +
3928 +       bitmap_bh = read_block_bitmap(sb, group);
3929 +       if (bitmap_bh == NULL) {
3930 +               /* error handling here */
3931 +               ext3_mb_release_desc(&e3b);
3932 +               BUG_ON(bitmap_bh == NULL);
3933 +       }
3934 +
3935 +       err = ext3_mb_load_buddy(sb, group, &e3b);
3936 +       BUG_ON(err != 0); /* error handling here */
3937 +
3938 +       if (needed == 0)
3939 +               needed = EXT3_BLOCKS_PER_GROUP(sb) + 1;
3940 +
3941 +       grp = EXT3_GROUP_INFO(sb, group);
3942 +       INIT_LIST_HEAD(&list);
3943 +
3944 +repeat:
3945 +       busy = 0;
3946 +       ext3_lock_group(sb, group);
3947 +       list_for_each_entry_safe (pa, tmp, &grp->bb_prealloc_list, pa_group_list) {
3948 +               spin_lock(&pa->pa_lock);
3949 +               if (atomic_read(&pa->pa_count)) {
3950 +                       spin_unlock(&pa->pa_lock);
3951 +                       busy = 1;
3952 +                       continue;
3953 +               }
3954 +               if (pa->pa_deleted) {
3955 +                       spin_unlock(&pa->pa_lock);
3956 +                       continue;
3957 +               }
3958 +
3959 +               /* seems this one can be freed ... */
3960 +               pa->pa_deleted = 1;
3961 +
3962 +               /* we can trust pa_free ... */
3963 +               free += pa->pa_free;
3964 +
3965 +               spin_unlock(&pa->pa_lock);
3966 +
3967 +               list_del_rcu(&pa->pa_group_list);
3968 +               list_add(&pa->u.pa_tmp_list, &list);
3969 +       }
3970 +
3971 +       /* if we still need more blocks and some PAs were used, try again */
3972 +       if (free < needed && busy) {
3973 +               ext3_unlock_group(sb, group);
3974 +               goto repeat;
3975 +       }
3976 +
3977 +       /* found anything to free? */
3978 +       if (list_empty(&list)) {
3979 +               BUG_ON(free != 0);
3980 +               goto out;
3981 +       }
3982 +
3983 +       /* now free all selected PAs */
3984 +       list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3985 +
3986 +               /* remove from object (inode or locality group) */
3987 +               spin_lock(pa->pa_obj_lock);
3988 +               list_del_rcu(&pa->pa_inode_list);
3989 +               spin_unlock(pa->pa_obj_lock);
3990 +
3991 +               if (pa->pa_linear)
3992 +                       ext3_mb_release_group_pa(&e3b, pa);
3993 +               else
3994 +                       ext3_mb_release_inode_pa(&e3b, bitmap_bh, pa);
3995 +
3996 +               list_del(&pa->u.pa_tmp_list);
3997 +               mb_call_rcu(pa);
3998 +       }
3999 +
4000 +out:
4001 +       ext3_unlock_group(sb, group);
4002 +       ext3_mb_release_desc(&e3b);
4003 +       brelse(bitmap_bh);
4004 +       return free;
4005 +}
4006 +
4007 +/*
4008 + * releases all non-used preallocated blocks for given inode
4009 + */
4010 +void ext3_mb_discard_inode_preallocations(struct inode *inode)
4011 +{
4012 +       struct ext3_inode_info *ei = EXT3_I(inode);
4013 +       struct super_block *sb = inode->i_sb;
4014 +       struct buffer_head *bitmap_bh = NULL;
4015 +       struct ext3_prealloc_space *pa, *tmp;
4016 +       unsigned long group = 0;
4017 +       struct list_head list;
4018 +       struct ext3_buddy e3b;
4019 +       int err;
4020 +
4021 +       if (!test_opt(sb, MBALLOC) || !S_ISREG(inode->i_mode)) {
4022 +               /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4023 +               return;
4024 +       }
4025 +
4026 +       mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
4027 +
4028 +       INIT_LIST_HEAD(&list);
4029 +
4030 +repeat:
4031 +       /* first, collect all pa's in the inode */
4032 +       spin_lock(&ei->i_prealloc_lock);
4033 +       while (!list_empty(&ei->i_prealloc_list)) {
4034 +               pa = list_entry(ei->i_prealloc_list.next,
4035 +                               struct ext3_prealloc_space, pa_inode_list);
4036 +               BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4037 +               spin_lock(&pa->pa_lock);
4038 +               if (atomic_read(&pa->pa_count)) {
4039 +                       /* this shouldn't happen often - nobody should
4040 +                        * use preallocation while we're discarding it */
4041 +                       spin_unlock(&pa->pa_lock);
4042 +                       spin_unlock(&ei->i_prealloc_lock);
4043 +                       current->state = TASK_UNINTERRUPTIBLE;
4044 +                       schedule_timeout(HZ);
4045 +                       goto repeat;
4046 +
4047 +               }
4048 +               if (pa->pa_deleted == 0) {
4049 +                       pa->pa_deleted = 1;
4050 +                       spin_unlock(&pa->pa_lock);
4051 +                       list_del_rcu(&pa->pa_inode_list);
4052 +                       list_add(&pa->u.pa_tmp_list, &list);
4053 +                       continue;
4054 +               }
4055 +
4056 +               /* someone is deleting pa right now */
4057 +               spin_unlock(&pa->pa_lock);
4058 +               spin_unlock(&ei->i_prealloc_lock);
4059 +
4060 +               /* we have to wait here because pa_deleted
4061 +                * doesn't mean pa is already unlinked from
4062 +                * the list. as we might be called from
4063 +                * ->clear_inode() the inode will get freed
4064 +                * and concurrent thread which is unlinking
4065 +                * pa from inode's list may access already
4066 +                * freed memory, bad-bad-bad */
4067 +
4068 +               /* XXX: if this happens too often, we can
4069 +                * add a flag to force wait only in case
4070 +                * of ->clear_inode(), but not in case of
4071 +                * regular truncate */
4072 +               current->state = TASK_UNINTERRUPTIBLE;
4073 +               schedule_timeout(HZ);
4074 +               goto repeat;
4075 +       }
4076 +       spin_unlock(&ei->i_prealloc_lock);
4077 +
4078 +       list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4079 +               BUG_ON(pa->pa_linear != 0);
4080 +               ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4081 +
4082 +               err = ext3_mb_load_buddy(sb, group, &e3b);
4083 +               BUG_ON(err != 0); /* error handling here */
4084 +
4085 +               bitmap_bh = read_block_bitmap(sb, group);
4086 +
4087 +               ext3_lock_group(sb, group);
4088 +               list_del_rcu(&pa->pa_group_list);
4089 +
4090 +               /* can be NULL due to IO error, at worst
4091 +                * we leave some free blocks unavailable
4092 +                * do not go RO - no need for */
4093 +               if (bitmap_bh != NULL)
4094 +                       ext3_mb_release_inode_pa(&e3b, bitmap_bh, pa);
4095 +               ext3_unlock_group(sb, group);
4096 +
4097 +               ext3_mb_release_desc(&e3b);
4098 +               brelse(bitmap_bh);
4099 +
4100 +               list_del(&pa->u.pa_tmp_list);
4101 +               mb_call_rcu(pa);
4102 +       }
4103 +}
4104 +
4105 +/*
4106 + * finds all preallocated spaces and return blocks being freed to them
4107 + * if preallocated space becomes full (no block is used from the space)
4108 + * then the function frees space in buddy
4109 + * XXX: at the moment, truncate (which is the only way to free blocks)
4110 + * discards all preallocations
4111 + */
4112 +void ext3_mb_return_to_preallocation(struct inode *inode, struct ext3_buddy *e3b,
4113 +                                       sector_t block, int count)
4114 +{
4115 +       BUG_ON(!list_empty(&EXT3_I(inode)->i_prealloc_list));
4116 +}
4117 +
4118 +void ext3_mb_show_ac(struct ext3_allocation_context *ac)
4119 +{
4120 +#if 0
4121 +       struct super_block *sb = ac->ac_sb;
4122 +       int i;
4123 +
4124 +       printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
4125 +                       ac->ac_status, ac->ac_flags);
4126 +       printk(KERN_ERR "EXT3-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4127 +                       "best %lu/%lu/%lu@%lu cr %d\n",
4128 +                       ac->ac_o_ex.fe_group, ac->ac_o_ex.fe_start,
4129 +                       ac->ac_o_ex.fe_len, ac->ac_o_ex.fe_logical,
4130 +                       ac->ac_g_ex.fe_group, ac->ac_g_ex.fe_start,
4131 +                       ac->ac_g_ex.fe_len, ac->ac_g_ex.fe_logical,
4132 +                       ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start,
4133 +                       ac->ac_b_ex.fe_len, ac->ac_b_ex.fe_logical,
4134 +                       ac->ac_criteria);
4135 +       printk(KERN_ERR "EXT3-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4136 +               ac->ac_found);
4137 +       printk("EXT3-fs: groups: ");
4138 +       for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
4139 +               struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, i);
4140 +               struct ext3_prealloc_space *pa;
4141 +               unsigned long start;
4142 +               struct list_head *cur;
4143 +               list_for_each_rcu(cur, &grp->bb_prealloc_list) {
4144 +                       pa = list_entry(cur, struct ext3_prealloc_space,
4145 +                                       pa_group_list);
4146 +                       spin_lock(&pa->pa_lock);
4147 +                       ext3_get_group_no_and_offset(sb, pa->pa_pstart, NULL, &start);
4148 +                       spin_unlock(&pa->pa_lock);
4149 +                       printk("PA:%u:%lu:%u ", i, start, pa->pa_len);
4150 +               }
4151 +
4152 +               if (grp->bb_free == 0)
4153 +                       continue;
4154 +               printk("%d: %d/%d ", i, grp->bb_free, grp->bb_fragments);
4155 +       }
4156 +       printk("\n");
4157 +       //dump_stack();
4158 +#endif
4159 +}
4160 +
4161 +void ext3_mb_group_or_file(struct ext3_allocation_context *ac)
4162 +{
4163 +       struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
4164 +       int bsbits = ac->ac_sb->s_blocksize_bits;
4165 +       loff_t size, isize;
4166 +
4167 +       if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
4168 +               return;
4169 +
4170 +       size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4171 +       isize = i_size_read(ac->ac_inode) >> bsbits;
4172 +       if (size < isize)
4173 +               size = isize;
4174 +
4175 +       /* don't use group allocation for large files */
4176 +       if (size >= sbi->s_mb_stream_request)
4177 +               return;
4178 +
4179 +       if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
4180 +               return;
4181 +
4182 +       BUG_ON(ac->ac_lg != NULL);
4183 +       ac->ac_lg = &sbi->s_locality_groups[smp_processor_id()];
4184 +
4185 +       /* we're going to use group allocation */
4186 +       ac->ac_flags |= EXT3_MB_HINT_GROUP_ALLOC;
4187 +
4188 +       /* serialize all allocations in the group */
4189 +       down(&ac->ac_lg->lg_sem);
4190 +}
4191 +
4192 +int ext3_mb_initialize_context(struct ext3_allocation_context *ac,
4193 +                               struct ext3_allocation_request *ar)
4194 +{
4195 +       struct super_block *sb = ar->inode->i_sb;
4196 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
4197 +       struct ext3_super_block *es = sbi->s_es;
4198 +       unsigned long group, len, goal;
4199 +       unsigned long block;
4200 +
4201 +       /* we can't allocate > group size */
4202 +       len = ar->len;
4203 +       if (len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
4204 +               len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
4205 +
4206 +       /* start searching from the goal */
4207 +       goal = ar->goal;
4208 +       if (goal < le32_to_cpu(es->s_first_data_block) ||
4209 +                       goal >= le32_to_cpu(es->s_blocks_count))
4210 +               goal = le32_to_cpu(es->s_first_data_block);
4211 +       ext3_get_group_no_and_offset(sb, goal, &group, &block);
4212 +
4213 +       /* set up allocation goals */
4214 +       ac->ac_b_ex.fe_logical = ar->logical;
4215 +       ac->ac_b_ex.fe_group = 0;
4216 +       ac->ac_b_ex.fe_start = 0;
4217 +       ac->ac_b_ex.fe_len = 0;
4218 +       ac->ac_status = AC_STATUS_CONTINUE;
4219 +       ac->ac_groups_scanned = 0;
4220 +       ac->ac_ex_scanned = 0;
4221 +       ac->ac_found = 0;
4222 +       ac->ac_sb = sb;
4223 +       ac->ac_inode = ar->inode;
4224 +       ac->ac_o_ex.fe_logical = ar->logical;
4225 +       ac->ac_o_ex.fe_group = group;
4226 +       ac->ac_o_ex.fe_start = block;
4227 +       ac->ac_o_ex.fe_len = len;
4228 +       ac->ac_g_ex.fe_logical = ar->logical;
4229 +       ac->ac_g_ex.fe_group = group;
4230 +       ac->ac_g_ex.fe_start = block;
4231 +       ac->ac_g_ex.fe_len = len;
4232 +       ac->ac_f_ex.fe_len = 0;
4233 +       ac->ac_flags = ar->flags;
4234 +       ac->ac_2order = 0;
4235 +       ac->ac_criteria = 0;
4236 +       ac->ac_pa = NULL;
4237 +       ac->ac_bitmap_page = NULL;
4238 +       ac->ac_buddy_page = NULL;
4239 +       ac->ac_lg = NULL;
4240 +
4241 +       /* we have to define context: we'll we work with a file or
4242 +        * locality group. this is a policy, actually */
4243 +       ext3_mb_group_or_file(ac);
4244 +
4245 +       mb_debug("init ac: %u blocks @ %llu, goal %llu, flags %x, 2^%d, "
4246 +                       "left: %llu/%llu, right %llu/%llu to %swritable\n",
4247 +                       (unsigned) ar->len, (unsigned) ar->logical,
4248 +                       (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4249 +                       (unsigned) ar->lleft, (unsigned) ar->pleft,
4250 +                       (unsigned) ar->lright, (unsigned) ar->pright,
4251 +                       atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4252 +       return 0;
4253 +
4254 +}
4255 +
4256 +/*
4257 + * release all resource we used in allocation
4258 + */
4259 +int ext3_mb_release_context(struct ext3_allocation_context *ac)
4260 +{
4261 +       if (ac->ac_pa) {
4262 +               if (ac->ac_pa->pa_linear) {
4263 +                       /* see comment in ext3_mb_use_group_pa() */
4264 +                       spin_lock(&ac->ac_pa->pa_lock);
4265 +                       ac->ac_pa->pa_pstart += ac->ac_b_ex.fe_len;
4266 +                       ac->ac_pa->pa_lstart += ac->ac_b_ex.fe_len;
4267 +                       ac->ac_pa->pa_free -= ac->ac_b_ex.fe_len;
4268 +                       ac->ac_pa->pa_len -= ac->ac_b_ex.fe_len;
4269 +                       spin_unlock(&ac->ac_pa->pa_lock);
4270 +               }
4271 +               ext3_mb_put_pa(ac, ac->ac_sb, ac->ac_pa);
4272 +       }
4273 +       if (ac->ac_bitmap_page)
4274 +               page_cache_release(ac->ac_bitmap_page);
4275 +       if (ac->ac_buddy_page)
4276 +               page_cache_release(ac->ac_buddy_page);
4277 +       if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
4278 +               up(&ac->ac_lg->lg_sem);
4279 +       ext3_mb_collect_stats(ac);
4280 +       return 0;
4281 +}
4282 +
4283 +int ext3_mb_discard_preallocations(struct super_block *sb, int needed)
4284 +{
4285 +       int i, ret, freed = 0;
4286 +
4287 +       for (i = 0; i < EXT3_SB(sb)->s_groups_count && needed > 0; i++) {
4288 +               ret = ext3_mb_discard_group_preallocations(sb, i, needed);
4289 +               freed += ret;
4290 +               needed -= ret;
4291 +       }
4292 +
4293 +       return freed;
4294 +}
4295 +
4296 +/*
4297 + * Main entry point into mballoc to allocate blocks
4298 + * it tries to use preallocation first, then falls back
4299 + * to usual allocation
4300 + */
4301 +unsigned long ext3_mb_new_blocks(handle_t *handle,
4302 +                                struct ext3_allocation_request *ar, int *errp)
4303 +{
4304 +       struct ext3_allocation_context ac;
4305 +       struct ext3_sb_info *sbi;
4306 +       struct super_block *sb;
4307 +       unsigned long block = 0;
4308 +       int freed, inquota;
4309 +
4310 +       sb = ar->inode->i_sb;
4311 +       sbi = EXT3_SB(sb);
4312 +
4313 +       if (!test_opt(sb, MBALLOC)) {
4314 +               static int ext3_mballoc_warning = 0;
4315 +               if (ext3_mballoc_warning++ == 0)
4316 +                       printk(KERN_ERR "EXT3-fs: multiblock request with "
4317 +                                       "mballoc disabled!\n");
4318 +               ar->len = 1;
4319 +               block = ext3_new_block_old(handle, ar->inode, ar->goal, errp);
4320 +               return block;
4321 +       }
4322 +
4323 +       while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
4324 +               ar->flags |= EXT3_MB_HINT_NOPREALLOC;
4325 +               ar->len--;
4326 +       }
4327 +       if (ar->len == 0) {
4328 +               *errp = -EDQUOT;
4329 +               return 0;
4330 +       }
4331 +       inquota = ar->len;
4332 +
4333 +       ext3_mb_poll_new_transaction(sb, handle);
4334 +
4335 +       if ((*errp = ext3_mb_initialize_context(&ac, ar))) {
4336 +               ar->len = 0;
4337 +               goto out;
4338 +       }
4339 +
4340 +       ac.ac_op = EXT3_MB_HISTORY_PREALLOC;
4341 +       if (!ext3_mb_use_preallocated(&ac)) {
4342 +
4343 +               ac.ac_op = EXT3_MB_HISTORY_ALLOC;
4344 +               ext3_mb_normalize_request(&ac, ar);
4345 +
4346 +repeat:
4347 +               /* allocate space in core */
4348 +               ext3_mb_regular_allocator(&ac);
4349 +
4350 +               /* as we've just preallocated more space than
4351 +                * user requested orinally, we store allocated
4352 +                * space in a special descriptor */
4353 +               if (ac.ac_status == AC_STATUS_FOUND &&
4354 +                               ac.ac_o_ex.fe_len < ac.ac_b_ex.fe_len)
4355 +                       ext3_mb_new_preallocation(&ac);
4356 +       }
4357 +
4358 +       if (likely(ac.ac_status == AC_STATUS_FOUND)) {
4359 +               ext3_mb_mark_diskspace_used(&ac, handle);
4360 +               *errp = 0;
4361 +               block = ext3_grp_offs_to_block(sb, &ac.ac_b_ex);
4362 +               ar->len = ac.ac_b_ex.fe_len;
4363 +       } else {
4364 +               freed  = ext3_mb_discard_preallocations(sb, ac.ac_o_ex.fe_len);
4365 +               if (freed)
4366 +                       goto repeat;
4367 +               *errp = -ENOSPC;
4368 +               ac.ac_b_ex.fe_len = 0;
4369 +               ar->len = 0;
4370 +               ext3_mb_show_ac(&ac);
4371 +       }
4372 +
4373 +       ext3_mb_release_context(&ac);
4374 +
4375 +out:
4376 +       if (ar->len < inquota)
4377 +               DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
4378 +
4379 +       return block;
4380 +}
4381 +EXPORT_SYMBOL(ext3_mb_new_blocks);
4382 +
4383 +int ext3_new_block(handle_t *handle, struct inode *inode,
4384 +                  unsigned long goal, int *errp)
4385 +{
4386 +       struct ext3_allocation_request ar;
4387 +       unsigned long ret;
4388 +
4389 +       if (!test_opt(inode->i_sb, MBALLOC)) {
4390 +               ret = ext3_new_block_old(handle, inode, goal, errp);
4391 +               return ret;
4392 +       }
4393 +
4394 +       ar.inode = inode;
4395 +       ar.goal = goal;
4396 +       ar.len = 1;
4397 +       ar.logical = 0;
4398 +       ar.lleft = 0;
4399 +       ar.pleft = 0;
4400 +       ar.lright = 0;
4401 +       ar.pright = 0;
4402 +       ar.flags = 0;
4403 +       ret = ext3_mb_new_blocks(handle, &ar, errp);
4404 +       return ret;
4405 +}
4406 +
4407 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
4408 +{
4409 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
4410 +
4411 +       if (sbi->s_last_transaction == handle->h_transaction->t_tid)
4412 +               return;
4413 +
4414 +       /* new transaction! time to close last one and free blocks for
4415 +        * committed transaction. we know that only transaction can be
4416 +        * active, so previos transaction can be being logged and we
4417 +        * know that transaction before previous is known to be already
4418 +        * logged. this means that now we may free blocks freed in all
4419 +        * transactions before previous one. hope I'm clear enough ... */
4420 +
4421 +       spin_lock(&sbi->s_md_lock);
4422 +       if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
4423 +               mb_debug("new transaction %lu, old %lu\n",
4424 +                               (unsigned long) handle->h_transaction->t_tid,
4425 +                               (unsigned long) sbi->s_last_transaction);
4426 +               list_splice_init(&sbi->s_closed_transaction,
4427 +                               &sbi->s_committed_transaction);
4428 +               list_splice_init(&sbi->s_active_transaction,
4429 +                               &sbi->s_closed_transaction);
4430 +               sbi->s_last_transaction = handle->h_transaction->t_tid;
4431 +       }
4432 +       spin_unlock(&sbi->s_md_lock);
4433 +
4434 +       ext3_mb_free_committed_blocks(sb);
4435 +}
4436 +
4437 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
4438 +                         int group, int block, int count)
4439 +{
4440 +       struct ext3_group_info *db = e3b->bd_info;
4441 +       struct super_block *sb = e3b->bd_sb;
4442 +       struct ext3_sb_info *sbi = EXT3_SB(sb);
4443 +       struct ext3_free_metadata *md;
4444 +       int i;
4445 +
4446 +       BUG_ON(e3b->bd_bitmap_page == NULL);
4447 +       BUG_ON(e3b->bd_buddy_page == NULL);
4448 +
4449 +       ext3_lock_group(sb, group);
4450 +       for (i = 0; i < count; i++) {
4451 +               md = db->bb_md_cur;
4452 +               if (md && db->bb_tid != handle->h_transaction->t_tid) {
4453 +                       db->bb_md_cur = NULL;
4454 +                       md = NULL;
4455 +               }
4456 +
4457 +               if (md == NULL) {
4458 +                       ext3_unlock_group(sb, group);
4459 +                       md = kmalloc(sizeof(*md), GFP_KERNEL);
4460 +                       if (md == NULL)
4461 +                               return -ENOMEM;
4462 +                       md->num = 0;
4463 +                       md->group = group;
4464 +
4465 +                       ext3_lock_group(sb, group);
4466 +                       if (db->bb_md_cur == NULL) {
4467 +                               spin_lock(&sbi->s_md_lock);
4468 +                               list_add(&md->list, &sbi->s_active_transaction);
4469 +                               spin_unlock(&sbi->s_md_lock);
4470 +                               /* protect buddy cache from being freed,
4471 +                                * otherwise we'll refresh it from
4472 +                                * on-disk bitmap and lose not-yet-available
4473 +                                * blocks */
4474 +                               page_cache_get(e3b->bd_buddy_page);
4475 +                               page_cache_get(e3b->bd_bitmap_page);
4476 +                               db->bb_md_cur = md;
4477 +                               db->bb_tid = handle->h_transaction->t_tid;
4478 +                               mb_debug("new md 0x%p for group %u\n",
4479 +                                               md, md->group);
4480 +                       } else {
4481 +                               kfree(md);
4482 +                               md = db->bb_md_cur;
4483 +                       }
4484 +               }
4485 +
4486 +               BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
4487 +               md->blocks[md->num] = block + i;
4488 +               md->num++;
4489 +               if (md->num == EXT3_BB_MAX_BLOCKS) {
4490 +                       /* no more space, put full container on a sb's list */
4491 +                       db->bb_md_cur = NULL;
4492 +               }
4493 +       }
4494 +       ext3_unlock_group(sb, group);
4495 +       return 0;
4496 +}
4497 +
4498 +/*
4499 + * Main entry point into mballoc to free blocks
4500 + */
4501 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
4502 +                       unsigned long block, unsigned long count,
4503 +                       int metadata, int *freed)
4504 +{
4505 +       struct buffer_head *bitmap_bh = NULL;
4506 +       struct super_block *sb = inode->i_sb;
4507 +       struct ext3_allocation_context ac;
4508 +       struct ext3_group_desc *gdp;
4509 +       struct ext3_super_block *es;
4510 +       unsigned long bit, overflow;
4511 +       struct buffer_head *gd_bh;
4512 +       unsigned long block_group;
4513 +       struct ext3_sb_info *sbi;
4514 +       struct ext3_buddy e3b;
4515 +       int err = 0, ret;
4516 +
4517 +       *freed = 0;
4518 +
4519 +       ext3_mb_poll_new_transaction(sb, handle);
4520 +
4521 +       sbi = EXT3_SB(sb);
4522 +       es = EXT3_SB(sb)->s_es;
4523 +       if (block < le32_to_cpu(es->s_first_data_block) ||
4524 +           block + count < block ||
4525 +           block + count > le32_to_cpu(es->s_blocks_count)) {
4526 +               ext3_error (sb, __FUNCTION__,
4527 +                           "Freeing blocks not in datazone - "
4528 +                           "block = %lu, count = %lu", block, count);
4529 +               goto error_return;
4530 +       }
4531 +
4532 +       ext3_debug("freeing block %lu\n", block);
4533 +
4534 +       ac.ac_op = EXT3_MB_HISTORY_FREE;
4535 +       ac.ac_inode = inode;
4536 +       ac.ac_sb = sb;
4537 +
4538 +do_more:
4539 +       overflow = 0;
4540 +       ext3_get_group_no_and_offset(sb, block, &block_group, &bit);
4541 +
4542 +       /*
4543 +        * Check to see if we are freeing blocks across a group
4544 +        * boundary.
4545 +        */
4546 +       if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
4547 +               overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
4548 +               count -= overflow;
4549 +       }
4550 +       brelse(bitmap_bh);
4551 +       bitmap_bh = read_block_bitmap(sb, block_group);
4552 +       if (!bitmap_bh)
4553 +               goto error_return;
4554 +       gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
4555 +       if (!gdp)
4556 +               goto error_return;
4557 +
4558 +       if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
4559 +           in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
4560 +           in_range (block, le32_to_cpu(gdp->bg_inode_table),
4561 +                     EXT3_SB(sb)->s_itb_per_group) ||
4562 +           in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
4563 +                     EXT3_SB(sb)->s_itb_per_group))
4564 +               ext3_error(sb, __FUNCTION__,
4565 +                          "Freeing blocks in system zone - "
4566 +                          "Block = %lu, count = %lu", block, count);
4567 +
4568 +       BUFFER_TRACE(bitmap_bh, "getting write access");
4569 +       err = ext3_journal_get_write_access(handle, bitmap_bh);
4570 +       if (err)
4571 +               goto error_return;
4572 +
4573 +       /*
4574 +        * We are about to modify some metadata.  Call the journal APIs
4575 +        * to unshare ->b_data if a currently-committing transaction is
4576 +        * using it
4577 +        */
4578 +       BUFFER_TRACE(gd_bh, "get_write_access");
4579 +       err = ext3_journal_get_write_access(handle, gd_bh);
4580 +       if (err)
4581 +               goto error_return;
4582 +
4583 +       err = ext3_mb_load_buddy(sb, block_group, &e3b);
4584 +       if (err)
4585 +               goto error_return;
4586 +
4587 +#ifdef AGGRESSIVE_CHECK
4588 +       {
4589 +               int i;
4590 +               for (i = 0; i < count; i++)
4591 +                       BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4592 +       }
4593 +#endif
4594 +       mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data, bit,
4595 +                       count);
4596 +
4597 +       /* We dirtied the bitmap block */
4598 +       BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4599 +       err = ext3_journal_dirty_metadata(handle, bitmap_bh);
4600 +
4601 +       ac.ac_b_ex.fe_group = block_group;
4602 +       ac.ac_b_ex.fe_start = bit;
4603 +       ac.ac_b_ex.fe_len = count;
4604 +       ext3_mb_store_history(&ac);
4605 +
4606 +       if (metadata) {
4607 +               /* blocks being freed are metadata. these blocks shouldn't
4608 +                * be used until this transaction is committed */
4609 +               ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
4610 +       } else {
4611 +               ext3_lock_group(sb, block_group);
4612 +               err = mb_free_blocks(inode, &e3b, bit, count);
4613 +               ext3_mb_return_to_preallocation(inode, &e3b, block, count);
4614 +               ext3_unlock_group(sb, block_group);
4615 +               BUG_ON(err != 0);
4616 +       }
4617 +
4618 +       spin_lock(sb_bgl_lock(sbi, block_group));
4619 +       gdp->bg_free_blocks_count =
4620 +               cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
4621 +       spin_unlock(sb_bgl_lock(sbi, block_group));
4622 +       percpu_counter_mod(&sbi->s_freeblocks_counter, count);
4623 +
4624 +       ext3_mb_release_desc(&e3b);
4625 +
4626 +       *freed += count;
4627 +
4628 +       /* And the group descriptor block */
4629 +       BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4630 +       ret = ext3_journal_dirty_metadata(handle, gd_bh);
4631 +       if (!err) err = ret;
4632 +
4633 +       if (overflow && !err) {
4634 +               block += count;
4635 +               count = overflow;
4636 +               goto do_more;
4637 +       }
4638 +       sb->s_dirt = 1;
4639 +error_return:
4640 +       brelse(bitmap_bh);
4641 +       ext3_std_error(sb, err);
4642 +       return;
4643 +}