1 Index: linux-2.6.9-full/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.9-full.orig/include/linux/ext3_fs.h 2006-06-01 14:58:46.000000000 +0400
4 +++ linux-2.6.9-full/include/linux/ext3_fs.h 2006-10-24 12:54:31.000000000 +0400
5 @@ -57,6 +57,14 @@ struct statfs;
6 #define ext3_debug(f, a...) do {} while (0)
9 +#define EXT3_MULTIBLOCK_ALLOCATOR 1
11 +#define EXT3_MB_HINT_MERGE 1
12 +#define EXT3_MB_HINT_RESERVED 2
13 +#define EXT3_MB_HINT_METADATA 4
14 +#define EXT3_MB_HINT_FIRST 8
15 +#define EXT3_MB_HINT_BEST 16
18 * Special inodes numbers
20 @@ -365,6 +373,7 @@ struct ext3_inode {
21 #define EXT3_MOUNT_IOPEN_NOPRIV 0x100000/* Make iopen world-readable */
22 #define EXT3_MOUNT_EXTENTS 0x200000/* Extents support */
23 #define EXT3_MOUNT_EXTDEBUG 0x400000/* Extents debug */
24 +#define EXT3_MOUNT_MBALLOC 0x800000/* Buddy allocation support */
26 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
28 @@ -387,6 +396,14 @@ struct ext3_inode {
29 #define ext3_find_first_zero_bit ext2_find_first_zero_bit
30 #define ext3_find_next_zero_bit ext2_find_next_zero_bit
32 +#ifndef ext2_find_next_le_bit
33 +#ifdef __LITTLE_ENDIAN
34 +#define ext2_find_next_le_bit(addr, size, off) find_next_bit((addr), (size), (off))
36 +#error "mballoc needs a patch for big-endian systems - CFS bug 10634"
37 +#endif /* __LITTLE_ENDIAN */
38 +#endif /* !ext2_find_next_le_bit */
41 * Maximal mount counts between two filesystem checks
43 @@ -726,7 +743,8 @@ extern int ext3_bg_has_super(struct supe
44 extern unsigned long ext3_bg_num_gdb(struct super_block *sb, int group);
45 extern int ext3_new_block (handle_t *, struct inode *, unsigned long, int *);
46 extern void ext3_free_blocks (handle_t *, struct inode *, unsigned long,
48 + unsigned long, int);
49 +extern int ext3_new_block_old(handle_t *, struct inode *, unsigned long, int *);
50 extern void ext3_free_blocks_sb (handle_t *, struct super_block *,
51 unsigned long, unsigned long, int *);
52 extern unsigned long ext3_count_free_blocks (struct super_block *);
53 @@ -857,6 +874,17 @@ extern void ext3_extents_initialize_bloc
54 extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
55 unsigned int cmd, unsigned long arg);
58 +extern long ext3_mb_stats;
59 +extern long ext3_mb_max_to_scan;
60 +extern int ext3_mb_init(struct super_block *, int);
61 +extern int ext3_mb_release(struct super_block *);
62 +extern int ext3_mb_new_blocks(handle_t *, struct inode *, unsigned long, int *, int, int *);
63 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
64 +extern void ext3_mb_release_blocks(struct super_block *, int);
65 +int __init init_ext3_proc(void);
66 +void exit_ext3_proc(void);
68 #endif /* __KERNEL__ */
70 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
71 Index: linux-2.6.9-full/include/linux/ext3_fs_sb.h
72 ===================================================================
73 --- linux-2.6.9-full.orig/include/linux/ext3_fs_sb.h 2006-05-18 23:57:04.000000000 +0400
74 +++ linux-2.6.9-full/include/linux/ext3_fs_sb.h 2006-10-24 12:54:31.000000000 +0400
77 #include <linux/blockgroup_lock.h>
78 #include <linux/percpu_counter.h>
79 +#include <linux/list.h>
82 #include <linux/rbtree.h>
83 +#include <linux/proc_fs.h>
85 +struct ext3_buddy_group_blocks;
86 +struct ext3_mb_history;
87 +#define EXT3_BB_MAX_BLOCKS
90 * third extended-fs super-block data in memory
91 @@ -81,6 +87,43 @@ struct ext3_sb_info {
92 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
93 int s_jquota_fmt; /* Format of quota to use */
96 + /* for buddy allocator */
97 + struct ext3_group_info ***s_group_info;
98 + struct inode *s_buddy_cache;
99 + long s_blocks_reserved;
100 + spinlock_t s_reserve_lock;
101 + struct list_head s_active_transaction;
102 + struct list_head s_closed_transaction;
103 + struct list_head s_committed_transaction;
104 + spinlock_t s_md_lock;
105 + tid_t s_last_transaction;
107 + unsigned short *s_mb_offsets, *s_mb_maxs;
108 + unsigned long s_stripe;
110 + /* history to debug policy */
111 + struct ext3_mb_history *s_mb_history;
112 + int s_mb_history_cur;
113 + int s_mb_history_max;
114 + struct proc_dir_entry *s_mb_proc;
115 + spinlock_t s_mb_history_lock;
117 + /* stats for buddy allocator */
118 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
119 + atomic_t s_bal_success; /* we found long enough chunks */
120 + atomic_t s_bal_allocated; /* in blocks */
121 + atomic_t s_bal_ex_scanned; /* total extents scanned */
122 + atomic_t s_bal_goals; /* goal hits */
123 + atomic_t s_bal_breaks; /* too long searches */
124 + atomic_t s_bal_2orders; /* 2^order hits */
125 + spinlock_t s_bal_lock;
126 + unsigned long s_mb_buddies_generated;
127 + unsigned long long s_mb_generation_time;
130 +#define EXT3_GROUP_INFO(sb, group) \
131 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
132 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
134 #endif /* _LINUX_EXT3_FS_SB */
135 Index: linux-2.6.9-full/fs/ext3/super.c
136 ===================================================================
137 --- linux-2.6.9-full.orig/fs/ext3/super.c 2006-06-01 14:58:46.000000000 +0400
138 +++ linux-2.6.9-full/fs/ext3/super.c 2006-10-24 12:54:31.000000000 +0400
139 @@ -394,6 +394,7 @@ void ext3_put_super (struct super_block
140 struct ext3_super_block *es = sbi->s_es;
143 + ext3_mb_release(sb);
144 ext3_ext_release(sb);
145 ext3_xattr_put_super(sb);
146 journal_destroy(sbi->s_journal);
147 @@ -597,6 +598,7 @@ enum {
148 Opt_ignore, Opt_barrier, Opt_err, Opt_resize,
149 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
150 Opt_extents, Opt_noextents, Opt_extdebug,
151 + Opt_mballoc, Opt_nomballoc, Opt_stripe,
154 static match_table_t tokens = {
155 @@ -649,6 +651,9 @@ static match_table_t tokens = {
156 {Opt_extents, "extents"},
157 {Opt_noextents, "noextents"},
158 {Opt_extdebug, "extdebug"},
159 + {Opt_mballoc, "mballoc"},
160 + {Opt_nomballoc, "nomballoc"},
161 + {Opt_stripe, "stripe=%u"},
162 {Opt_barrier, "barrier=%u"},
164 {Opt_resize, "resize"},
165 @@ -962,6 +967,19 @@ clear_qf_name:
167 set_opt (sbi->s_mount_opt, EXTDEBUG);
170 + set_opt(sbi->s_mount_opt, MBALLOC);
172 + case Opt_nomballoc:
173 + clear_opt(sbi->s_mount_opt, MBALLOC);
176 + if (match_int(&args[0], &option))
180 + sbi->s_stripe = option;
184 "EXT3-fs: Unrecognized mount option \"%s\" "
185 @@ -1651,6 +1669,7 @@ static int ext3_fill_super (struct super
186 ext3_count_dirs(sb));
189 + ext3_mb_init(sb, needs_recovery);
193 @@ -2433,7 +2452,13 @@ static struct file_system_type ext3_fs_t
195 static int __init init_ext3_fs(void)
197 - int err = init_ext3_xattr();
200 + err = init_ext3_proc();
204 + err = init_ext3_xattr();
207 err = init_inodecache();
208 @@ -2455,6 +2480,7 @@ static void __exit exit_ext3_fs(void)
209 unregister_filesystem(&ext3_fs_type);
210 destroy_inodecache();
215 int ext3_prep_san_write(struct inode *inode, long *blocks,
216 Index: linux-2.6.9-full/fs/ext3/extents.c
217 ===================================================================
218 --- linux-2.6.9-full.orig/fs/ext3/extents.c 2006-06-01 14:58:46.000000000 +0400
219 +++ linux-2.6.9-full/fs/ext3/extents.c 2006-10-24 12:54:31.000000000 +0400
220 @@ -777,7 +777,7 @@ cleanup:
221 for (i = 0; i < depth; i++) {
224 - ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
225 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
229 @@ -1434,7 +1434,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
230 path->p_idx->ei_leaf);
231 bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
232 ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
233 - ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
234 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
238 @@ -1919,10 +1919,12 @@ ext3_remove_blocks(struct ext3_extents_t
239 int needed = ext3_remove_blocks_credits(tree, ex, from, to);
240 handle_t *handle = ext3_journal_start(tree->inode, needed);
241 struct buffer_head *bh;
243 + int i, metadata = 0;
246 return PTR_ERR(handle);
247 + if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
249 if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
251 unsigned long num, start;
252 @@ -1934,7 +1936,7 @@ ext3_remove_blocks(struct ext3_extents_t
253 bh = sb_find_get_block(tree->inode->i_sb, start + i);
254 ext3_forget(handle, 0, tree->inode, bh, start + i);
256 - ext3_free_blocks(handle, tree->inode, start, num);
257 + ext3_free_blocks(handle, tree->inode, start, num, metadata);
258 } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
259 printk("strange request: removal %lu-%lu from %u:%u\n",
260 from, to, ex->ee_block, ex->ee_len);
261 Index: linux-2.6.9-full/fs/ext3/inode.c
262 ===================================================================
263 --- linux-2.6.9-full.orig/fs/ext3/inode.c 2006-06-01 14:58:46.000000000 +0400
264 +++ linux-2.6.9-full/fs/ext3/inode.c 2006-10-24 12:54:31.000000000 +0400
265 @@ -572,7 +572,7 @@ static int ext3_alloc_branch(handle_t *h
266 ext3_journal_forget(handle, branch[i].bh);
268 for (i = 0; i < keys; i++)
269 - ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1);
270 + ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1, 1);
274 @@ -673,7 +673,7 @@ err_out:
276 for (i = 0; i < num; i++)
277 ext3_free_blocks(handle, inode,
278 - le32_to_cpu(where[i].key), 1);
279 + le32_to_cpu(where[i].key), 1, 1);
283 @@ -1831,7 +1831,7 @@ ext3_clear_blocks(handle_t *handle, stru
287 - ext3_free_blocks(handle, inode, block_to_free, count);
288 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
292 @@ -2004,7 +2004,7 @@ static void ext3_free_branches(handle_t
293 ext3_journal_test_restart(handle, inode);
296 - ext3_free_blocks(handle, inode, nr, 1);
297 + ext3_free_blocks(handle, inode, nr, 1, 1);
301 Index: linux-2.6.9-full/fs/ext3/balloc.c
302 ===================================================================
303 --- linux-2.6.9-full.orig/fs/ext3/balloc.c 2006-03-10 18:20:03.000000000 +0300
304 +++ linux-2.6.9-full/fs/ext3/balloc.c 2006-10-24 12:54:31.000000000 +0400
305 @@ -79,7 +79,7 @@ struct ext3_group_desc * ext3_get_group_
307 * Return buffer_head on success or NULL in case of failure.
309 -static struct buffer_head *
310 +struct buffer_head *
311 read_block_bitmap(struct super_block *sb, unsigned int block_group)
313 struct ext3_group_desc * desc;
314 @@ -451,24 +451,6 @@ error_return:
318 -/* Free given blocks, update quota and i_blocks field */
319 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
320 - unsigned long block, unsigned long count)
322 - struct super_block * sb;
323 - int dquot_freed_blocks;
327 - printk ("ext3_free_blocks: nonexistent device");
330 - ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
331 - if (dquot_freed_blocks)
332 - DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
337 * For ext3 allocations, we must not reuse any blocks which are
338 * allocated in the bitmap buffer's "last committed data" copy. This
339 @@ -1131,7 +1113,7 @@ int ext3_should_retry_alloc(struct super
340 * bitmap, and then for any free bit if that fails.
341 * This function also updates quota and i_blocks field.
343 -int ext3_new_block(handle_t *handle, struct inode *inode,
344 +int ext3_new_block_old(handle_t *handle, struct inode *inode,
345 unsigned long goal, int *errp)
347 struct buffer_head *bitmap_bh = NULL;
348 Index: linux-2.6.9-full/fs/ext3/xattr.c
349 ===================================================================
350 --- linux-2.6.9-full.orig/fs/ext3/xattr.c 2006-05-18 23:57:04.000000000 +0400
351 +++ linux-2.6.9-full/fs/ext3/xattr.c 2006-10-24 12:54:31.000000000 +0400
352 @@ -1281,7 +1281,7 @@ ext3_xattr_set_handle2(handle_t *handle,
353 new_bh = sb_getblk(sb, block);
356 - ext3_free_blocks(handle, inode, block, 1);
357 + ext3_free_blocks(handle, inode, block, 1, 1);
361 @@ -1328,7 +1328,7 @@ getblk_failed:
363 mb_cache_entry_free(ce);
364 ea_bdebug(old_bh, "freeing");
365 - ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1);
366 + ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1, 1);
368 /* ext3_forget() calls bforget() for us, but we
369 let our caller release old_bh, so we need to
370 @@ -1427,7 +1427,7 @@ ext3_xattr_delete_inode(handle_t *handle
371 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
373 mb_cache_entry_free(ce);
374 - ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1);
375 + ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1, 1);
377 ext3_forget(handle, 1, inode, bh, EXT3_I(inode)->i_file_acl);
379 Index: linux-2.6.9-full/fs/ext3/mballoc.c
380 ===================================================================
381 --- linux-2.6.9-full.orig/fs/ext3/mballoc.c 2006-10-23 18:07:54.821533176 +0400
382 +++ linux-2.6.9-full/fs/ext3/mballoc.c 2006-10-24 13:00:56.000000000 +0400
385 + * Copyright 2008 Sun Microsystems, Inc.
386 + * Written by Alex Tomas <alex@clusterfs.com>
388 + * This program is free software; you can redistribute it and/or modify
389 + * it under the terms of the GNU General Public License version 2 as
390 + * published by the Free Software Foundation.
392 + * This program is distributed in the hope that it will be useful,
393 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
394 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
395 + * GNU General Public License for more details.
397 + * You should have received a copy of the GNU General Public Licens
398 + * along with this program; if not, write to the Free Software
399 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
404 + * mballoc.c contains the multiblocks allocation routines
407 +#include <linux/config.h>
408 +#include <linux/time.h>
409 +#include <linux/fs.h>
410 +#include <linux/namei.h>
411 +#include <linux/jbd.h>
412 +#include <linux/ext3_fs.h>
413 +#include <linux/ext3_jbd.h>
414 +#include <linux/quotaops.h>
415 +#include <linux/buffer_head.h>
416 +#include <linux/module.h>
417 +#include <linux/swap.h>
418 +#include <linux/proc_fs.h>
419 +#include <linux/pagemap.h>
420 +#include <linux/seq_file.h>
424 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
425 + * - track min/max extents in each group for better group selection
426 + * - mb_mark_used() may allocate chunk right after splitting buddy
427 + * - special flag to advice allocator to look for requested + N blocks
428 + * this may improve interaction between extents and mballoc
429 + * - tree of groups sorted by number of free blocks
430 + * - percpu reservation code (hotpath)
435 + * with AGRESSIVE_CHECK allocator runs consistency checks over
436 + * structures. these checks slow things down a lot
438 +#define AGGRESSIVE_CHECK__
444 +#define mb_debug(fmt,a...) printk(fmt, ##a)
446 +#define mb_debug(fmt,a...)
450 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
451 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
453 +#define EXT3_MB_HISTORY
456 + * How long mballoc can look for a best extent (in found extents)
458 +long ext3_mb_max_to_scan = 500;
461 + * How long mballoc must look for a best extent
463 +long ext3_mb_min_to_scan = 30;
466 + * with 'ext3_mb_stats' allocator will collect stats that will be
467 + * shown at umount. The collecting costs though!
470 +long ext3_mb_stats = 1;
473 + * for which requests use 2^N search using buddies
475 +long ext3_mb_order2_reqs = 8;
477 +#ifdef EXT3_BB_MAX_BLOCKS
478 +#undef EXT3_BB_MAX_BLOCKS
480 +#define EXT3_BB_MAX_BLOCKS 30
482 +struct ext3_free_metadata {
483 + unsigned short group;
484 + unsigned short num;
485 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
486 + struct list_head list;
489 +struct ext3_group_info {
490 + unsigned long bb_state;
491 + unsigned long bb_tid;
492 + struct ext3_free_metadata *bb_md_cur;
493 + unsigned short bb_first_free;
494 + unsigned short bb_free;
495 + unsigned short bb_fragments;
496 + unsigned short bb_counters[];
500 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
501 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
503 +#define EXT3_MB_GRP_NEED_INIT(grp) \
504 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
506 +struct ext3_free_extent {
512 +struct ext3_allocation_context {
513 + struct super_block *ac_sb;
516 + struct ext3_free_extent ac_g_ex;
518 + /* the best found extent */
519 + struct ext3_free_extent ac_b_ex;
521 + /* number of iterations done. we have to track to limit searching */
522 + unsigned long ac_ex_scanned;
523 + __u16 ac_groups_scanned;
528 + __u8 ac_flags; /* allocation hints */
531 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
532 + * N > 0, the field stores N, otherwise 0 */
534 + struct page *ac_buddy_page;
535 + struct page *ac_bitmap_page;
538 +#define AC_STATUS_CONTINUE 1
539 +#define AC_STATUS_FOUND 2
540 +#define AC_STATUS_BREAK 3
542 +struct ext3_mb_history {
543 + struct ext3_free_extent goal; /* goal allocation */
544 + struct ext3_free_extent result; /* result allocation */
547 + __u16 found; /* how many extents have been found */
548 + __u16 groups; /* how many groups have been scanned */
549 + __u16 tail; /* what tail broke some buddy */
550 + __u16 buddy; /* buddy the tail ^^^ broke */
551 + __u8 cr; /* which phase the result extent was found at */
556 + struct page *bd_buddy_page;
558 + struct page *bd_bitmap_page;
560 + struct ext3_group_info *bd_info;
561 + struct super_block *bd_sb;
565 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
566 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
568 +#ifndef EXT3_MB_HISTORY
569 +#define ext3_mb_store_history(sb,ino,ac)
571 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
572 + struct ext3_allocation_context *ac);
575 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
577 +static struct proc_dir_entry *proc_root_ext3;
579 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
580 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
581 +void ext3_mb_free_committed_blocks(struct super_block *);
583 +#if BITS_PER_LONG == 64
584 +#define mb_correct_addr_and_bit(bit,addr) \
586 + bit += ((unsigned long) addr & 7UL) << 3; \
587 + addr = (void *) ((unsigned long) addr & ~7UL); \
589 +#elif BITS_PER_LONG == 32
590 +#define mb_correct_addr_and_bit(bit,addr) \
592 + bit += ((unsigned long) addr & 3UL) << 3; \
593 + addr = (void *) ((unsigned long) addr & ~3UL); \
596 +#error "how many bits you are?!"
599 +static inline int mb_test_bit(int bit, void *addr)
601 + mb_correct_addr_and_bit(bit,addr);
602 + return ext2_test_bit(bit, addr);
605 +static inline void mb_set_bit(int bit, void *addr)
607 + mb_correct_addr_and_bit(bit,addr);
608 + ext2_set_bit(bit, addr);
611 +static inline void mb_set_bit_atomic(int bit, void *addr)
613 + mb_correct_addr_and_bit(bit,addr);
614 + ext2_set_bit_atomic(NULL, bit, addr);
617 +static inline void mb_clear_bit(int bit, void *addr)
619 + mb_correct_addr_and_bit(bit,addr);
620 + ext2_clear_bit(bit, addr);
623 +static inline void mb_clear_bit_atomic(int bit, void *addr)
625 + mb_correct_addr_and_bit(bit,addr);
626 + ext2_clear_bit_atomic(NULL, bit, addr);
629 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
632 +#if BITS_PER_LONG == 64
633 + fix = ((unsigned long) addr & 7UL) << 3;
634 + addr = (void *) ((unsigned long) addr & ~7UL);
635 +#elif BITS_PER_LONG == 32
636 + fix = ((unsigned long) addr & 3UL) << 3;
637 + addr = (void *) ((unsigned long) addr & ~3UL);
639 +#error "how many bits you are?!"
643 + return ext2_find_next_zero_bit(addr, max, start) - fix;
646 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
650 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
651 + J_ASSERT(max != NULL);
653 + if (order > e3b->bd_blkbits + 1) {
658 + /* at order 0 we see each particular block */
659 + *max = 1 << (e3b->bd_blkbits + 3);
661 + return EXT3_MB_BITMAP(e3b);
663 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
664 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
669 +#ifdef AGGRESSIVE_CHECK
671 +static void mb_check_buddy(struct ext3_buddy *e3b)
673 + int order = e3b->bd_blkbits + 1;
674 + int max, max2, i, j, k, count;
675 + int fragments = 0, fstart;
676 + void *buddy, *buddy2;
678 + if (!test_opt(e3b->bd_sb, MBALLOC))
682 + static int mb_check_counter = 0;
683 + if (mb_check_counter++ % 300 != 0)
687 + while (order > 1) {
688 + buddy = mb_find_buddy(e3b, order, &max);
690 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
692 + J_ASSERT(buddy != buddy2);
693 + J_ASSERT(max * 2 == max2);
696 + for (i = 0; i < max; i++) {
698 + if (mb_test_bit(i, buddy)) {
699 + /* only single bit in buddy2 may be 1 */
700 + if (!mb_test_bit(i << 1, buddy2))
701 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
702 + else if (!mb_test_bit((i << 1) + 1, buddy2))
703 + J_ASSERT(mb_test_bit(i << 1, buddy2));
707 + /* both bits in buddy2 must be 0 */
708 + J_ASSERT(mb_test_bit(i << 1, buddy2));
709 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
711 + for (j = 0; j < (1 << order); j++) {
712 + k = (i * (1 << order)) + j;
713 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
717 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
722 + buddy = mb_find_buddy(e3b, 0, &max);
723 + for (i = 0; i < max; i++) {
724 + if (!mb_test_bit(i, buddy)) {
725 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
726 + if (fstart == -1) {
733 + /* check used bits only */
734 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
735 + buddy2 = mb_find_buddy(e3b, j, &max2);
737 + J_ASSERT(k < max2);
738 + J_ASSERT(mb_test_bit(k, buddy2));
741 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
742 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
746 +#define mb_check_buddy(e3b)
749 +/* find most significant bit */
750 +static int inline fmsb(unsigned short word)
764 + } while (word != 0);
770 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
771 + int len, struct ext3_group_info *grp)
773 + struct ext3_sb_info *sbi = EXT3_SB(sb);
774 + unsigned short min, max, chunk, border;
776 + mb_debug("mark %u/%u free\n", first, len);
777 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
779 + border = 2 << sb->s_blocksize_bits;
782 + /* find how many blocks can be covered since this position */
783 + max = ffs(first | border) - 1;
785 + /* find how many blocks of power 2 we need to mark */
788 + mb_debug(" %u/%u -> max %u, min %u\n",
789 + first & ((2 << sb->s_blocksize_bits) - 1),
796 + /* mark multiblock chunks only */
797 + grp->bb_counters[min]++;
799 + mb_debug(" set %u at %u \n", first >> min,
800 + sbi->s_mb_offsets[min]);
801 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
810 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
813 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
814 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
815 + unsigned short i = 0, first, len;
816 + unsigned free = 0, fragments = 0;
817 + unsigned long long period = get_cycles();
819 + i = mb_find_next_zero_bit(bitmap, max, 0);
820 + grp->bb_first_free = i;
824 + i = ext2_find_next_le_bit(bitmap, max, i);
828 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
830 + grp->bb_counters[0]++;
832 + i = mb_find_next_zero_bit(bitmap, max, i);
834 + grp->bb_fragments = fragments;
836 + /* bb_state shouldn't being modified because all
837 + * others waits for init completion on page lock */
838 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
839 + if (free != grp->bb_free) {
840 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
841 + group, free, grp->bb_free);
842 + grp->bb_free = free;
845 + period = get_cycles() - period;
846 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
847 + EXT3_SB(sb)->s_mb_buddies_generated++;
848 + EXT3_SB(sb)->s_mb_generation_time += period;
849 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
852 +static int ext3_mb_init_cache(struct page *page)
854 + int blocksize, blocks_per_page, groups_per_page;
855 + int err = 0, i, first_group, first_block;
856 + struct super_block *sb;
857 + struct buffer_head *bhs;
858 + struct buffer_head **bh;
859 + struct inode *inode;
860 + char *data, *bitmap;
862 + mb_debug("init page %lu\n", page->index);
864 + inode = page->mapping->host;
866 + blocksize = 1 << inode->i_blkbits;
867 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
869 + groups_per_page = blocks_per_page >> 1;
870 + if (groups_per_page == 0)
871 + groups_per_page = 1;
873 + /* allocate buffer_heads to read bitmaps */
874 + if (groups_per_page > 1) {
876 + i = sizeof(struct buffer_head *) * groups_per_page;
877 + bh = kmalloc(i, GFP_NOFS);
884 + first_group = page->index * blocks_per_page / 2;
886 + /* read all groups the page covers into the cache */
887 + for (i = 0; i < groups_per_page; i++) {
888 + struct ext3_group_desc * desc;
890 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
894 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
899 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
903 + if (buffer_uptodate(bh[i]))
906 + lock_buffer(bh[i]);
907 + if (buffer_uptodate(bh[i])) {
908 + unlock_buffer(bh[i]);
913 + bh[i]->b_end_io = end_buffer_read_sync;
914 + submit_bh(READ, bh[i]);
915 + mb_debug("read bitmap for group %u\n", first_group + i);
918 + /* wait for I/O completion */
919 + for (i = 0; i < groups_per_page && bh[i]; i++)
920 + wait_on_buffer(bh[i]);
923 + for (i = 0; i < groups_per_page && bh[i]; i++)
924 + if (!buffer_uptodate(bh[i]))
927 + first_block = page->index * blocks_per_page;
928 + for (i = 0; i < blocks_per_page; i++) {
931 + group = (first_block + i) >> 1;
932 + if (group >= EXT3_SB(sb)->s_groups_count)
935 + data = page_address(page) + (i * blocksize);
936 + bitmap = bh[group - first_group]->b_data;
938 + if ((first_block + i) & 1) {
939 + /* this is block of buddy */
940 + mb_debug("put buddy for group %u in page %lu/%x\n",
941 + group, page->index, i * blocksize);
942 + memset(data, 0xff, blocksize);
943 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
944 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
945 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
946 + ext3_mb_generate_buddy(sb, data, bitmap, group);
948 + /* this is block of bitmap */
949 + mb_debug("put bitmap for group %u in page %lu/%x\n",
950 + group, page->index, i * blocksize);
951 + memcpy(data, bitmap, blocksize);
954 + SetPageUptodate(page);
958 + for (i = 0; i < groups_per_page && bh[i]; i++)
966 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
967 + struct ext3_buddy *e3b)
969 + struct ext3_sb_info *sbi = EXT3_SB(sb);
970 + struct inode *inode = sbi->s_buddy_cache;
971 + int blocks_per_page, block, pnum, poff;
974 + mb_debug("load group %u\n", group);
976 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
978 + e3b->bd_blkbits = sb->s_blocksize_bits;
979 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
981 + e3b->bd_group = group;
982 + e3b->bd_buddy_page = NULL;
983 + e3b->bd_bitmap_page = NULL;
986 + pnum = block / blocks_per_page;
987 + poff = block % blocks_per_page;
989 + /* we could use find_or_create_page(), but it locks page
990 + * what we'd like to avoid in fast path ... */
991 + page = find_get_page(inode->i_mapping, pnum);
992 + if (page == NULL || !PageUptodate(page)) {
994 + page_cache_release(page);
995 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
997 + BUG_ON(page->mapping != inode->i_mapping);
998 + if (!PageUptodate(page))
999 + ext3_mb_init_cache(page);
1000 + unlock_page(page);
1003 + if (page == NULL || !PageUptodate(page))
1005 + e3b->bd_bitmap_page = page;
1006 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1007 + mark_page_accessed(page);
1010 + pnum = block / blocks_per_page;
1011 + poff = block % blocks_per_page;
1013 + page = find_get_page(inode->i_mapping, pnum);
1014 + if (page == NULL || !PageUptodate(page)) {
1016 + page_cache_release(page);
1017 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1019 + BUG_ON(page->mapping != inode->i_mapping);
1020 + if (!PageUptodate(page))
1021 + ext3_mb_init_cache(page);
1022 + unlock_page(page);
1025 + if (page == NULL || !PageUptodate(page))
1027 + e3b->bd_buddy_page = page;
1028 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1029 + mark_page_accessed(page);
1031 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1032 + J_ASSERT(e3b->bd_buddy_page != NULL);
1037 + if (e3b->bd_bitmap_page)
1038 + page_cache_release(e3b->bd_bitmap_page);
1039 + if (e3b->bd_buddy_page)
1040 + page_cache_release(e3b->bd_buddy_page);
1041 + e3b->bd_buddy = NULL;
1042 + e3b->bd_bitmap = NULL;
1046 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1048 + if (e3b->bd_bitmap_page)
1049 + page_cache_release(e3b->bd_bitmap_page);
1050 + if (e3b->bd_buddy_page)
1051 + page_cache_release(e3b->bd_buddy_page);
1056 +ext3_lock_group(struct super_block *sb, int group)
1058 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1059 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1063 +ext3_unlock_group(struct super_block *sb, int group)
1065 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1066 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1069 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1074 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1075 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1077 + bb = EXT3_MB_BUDDY(e3b);
1078 + while (order <= e3b->bd_blkbits + 1) {
1079 + block = block >> 1;
1080 + if (!mb_test_bit(block, bb)) {
1081 + /* this block is part of buddy of order 'order' */
1084 + bb += 1 << (e3b->bd_blkbits - order);
1090 +static inline void mb_clear_bits(void *bm, int cur, int len)
1095 + while (cur < len) {
1096 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1097 + /* fast path: clear whole word at once */
1098 + addr = bm + (cur >> 3);
1103 + mb_clear_bit_atomic(cur, bm);
1108 +static inline void mb_set_bits(void *bm, int cur, int len)
1113 + while (cur < len) {
1114 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1115 + /* fast path: clear whole word at once */
1116 + addr = bm + (cur >> 3);
1117 + *addr = 0xffffffff;
1121 + mb_set_bit_atomic(cur, bm);
1126 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1128 + int block = 0, max = 0, order;
1129 + void *buddy, *buddy2;
1131 + mb_check_buddy(e3b);
1133 + e3b->bd_info->bb_free += count;
1134 + if (first < e3b->bd_info->bb_first_free)
1135 + e3b->bd_info->bb_first_free = first;
1137 + /* let's maintain fragments counter */
1139 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1140 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1141 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1143 + e3b->bd_info->bb_fragments--;
1144 + else if (!block && !max)
1145 + e3b->bd_info->bb_fragments++;
1147 + /* let's maintain buddy itself */
1148 + while (count-- > 0) {
1152 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1153 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1154 + e3b->bd_info->bb_counters[order]++;
1156 + /* start of the buddy */
1157 + buddy = mb_find_buddy(e3b, order, &max);
1161 + if (mb_test_bit(block, buddy) ||
1162 + mb_test_bit(block + 1, buddy))
1165 + /* both the buddies are free, try to coalesce them */
1166 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1172 + /* for special purposes, we don't set
1173 + * free bits in bitmap */
1174 + mb_set_bit(block, buddy);
1175 + mb_set_bit(block + 1, buddy);
1177 + e3b->bd_info->bb_counters[order]--;
1178 + e3b->bd_info->bb_counters[order]--;
1180 + block = block >> 1;
1182 + e3b->bd_info->bb_counters[order]++;
1184 + mb_clear_bit(block, buddy2);
1188 + mb_check_buddy(e3b);
1193 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1194 + int needed, struct ext3_free_extent *ex)
1196 + int next = block, max, ord;
1199 + J_ASSERT(ex != NULL);
1201 + buddy = mb_find_buddy(e3b, order, &max);
1203 + J_ASSERT(block < max);
1204 + if (mb_test_bit(block, buddy)) {
1211 + if (likely(order == 0)) {
1212 + /* find actual order */
1213 + order = mb_find_order_for_block(e3b, block);
1214 + block = block >> order;
1217 + ex->fe_len = 1 << order;
1218 + ex->fe_start = block << order;
1219 + ex->fe_group = e3b->bd_group;
1221 + /* calc difference from given start */
1222 + next = next - ex->fe_start;
1223 + ex->fe_len -= next;
1224 + ex->fe_start += next;
1226 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1228 + if (block + 1 >= max)
1231 + next = (block + 1) * (1 << order);
1232 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1235 + ord = mb_find_order_for_block(e3b, next);
1238 + block = next >> order;
1239 + ex->fe_len += 1 << order;
1242 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1243 + return ex->fe_len;
1246 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1248 + int ord, mlen = 0, max = 0, cur;
1249 + int start = ex->fe_start;
1250 + int len = ex->fe_len;
1255 + mb_check_buddy(e3b);
1257 + e3b->bd_info->bb_free -= len;
1258 + if (e3b->bd_info->bb_first_free == start)
1259 + e3b->bd_info->bb_first_free += len;
1261 + /* let's maintain fragments counter */
1263 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1264 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1265 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1267 + e3b->bd_info->bb_fragments++;
1268 + else if (!mlen && !max)
1269 + e3b->bd_info->bb_fragments--;
1271 + /* let's maintain buddy itself */
1273 + ord = mb_find_order_for_block(e3b, start);
1275 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1276 + /* the whole chunk may be allocated at once! */
1278 + buddy = mb_find_buddy(e3b, ord, &max);
1279 + J_ASSERT((start >> ord) < max);
1280 + mb_set_bit(start >> ord, buddy);
1281 + e3b->bd_info->bb_counters[ord]--;
1284 + J_ASSERT(len >= 0);
1288 + /* store for history */
1290 + ret = len | (ord << 16);
1292 + /* we have to split large buddy */
1293 + J_ASSERT(ord > 0);
1294 + buddy = mb_find_buddy(e3b, ord, &max);
1295 + mb_set_bit(start >> ord, buddy);
1296 + e3b->bd_info->bb_counters[ord]--;
1299 + cur = (start >> ord) & ~1U;
1300 + buddy = mb_find_buddy(e3b, ord, &max);
1301 + mb_clear_bit(cur, buddy);
1302 + mb_clear_bit(cur + 1, buddy);
1303 + e3b->bd_info->bb_counters[ord]++;
1304 + e3b->bd_info->bb_counters[ord]++;
1307 + /* now drop all the bits in bitmap */
1308 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1310 + mb_check_buddy(e3b);
1316 + * Must be called under group lock!
1318 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1319 + struct ext3_buddy *e3b)
1321 + unsigned long ret;
1323 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1324 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1326 + ac->ac_status = AC_STATUS_FOUND;
1327 + ac->ac_tail = ret & 0xffff;
1328 + ac->ac_buddy = ret >> 16;
1330 + /* hold in-core structures until allocated
1331 + * blocks are marked non-free in on-disk bitmap */
1332 + ac->ac_buddy_page = e3b->bd_buddy_page;
1333 + page_cache_get(e3b->bd_buddy_page);
1334 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1335 + page_cache_get(e3b->bd_bitmap_page);
1339 + * The routine checks whether found extent is good enough. If it is,
1340 + * then the extent gets marked used and flag is set to the context
1341 + * to stop scanning. Otherwise, the extent is compared with the
1342 + * previous found extent and if new one is better, then it's stored
1343 + * in the context. Later, the best found extent will be used, if
1344 + * mballoc can't find good enough extent.
1346 + * FIXME: real allocation policy is to be designed yet!
1348 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1349 + struct ext3_free_extent *ex,
1350 + struct ext3_buddy *e3b)
1352 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1353 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1355 + J_ASSERT(ex->fe_len > 0);
1356 + J_ASSERT(ex->fe_len < EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1357 + J_ASSERT(ex->fe_start < EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1362 + * The special case - take what you catch first
1364 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1366 + ext3_mb_use_best_found(ac, e3b);
1371 + * Let's check whether the chunk is good enough
1373 + if (ex->fe_len == gex->fe_len) {
1375 + ext3_mb_use_best_found(ac, e3b);
1380 + * If this is first found extent, just store it in the context
1382 + if (bex->fe_len == 0) {
1388 + * If new found extent is better, store it in the context
1390 + if (bex->fe_len < gex->fe_len) {
1391 + /* if the request isn't satisfied, any found extent
1392 + * larger than previous best one is better */
1393 + if (ex->fe_len > bex->fe_len)
1395 + } else if (ex->fe_len > gex->fe_len) {
1396 + /* if the request is satisfied, then we try to find
1397 + * an extent that still satisfy the request, but is
1398 + * smaller than previous one */
1403 + * Let's scan at least few extents and don't pick up a first one
1405 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1406 + ac->ac_status = AC_STATUS_BREAK;
1409 + * We don't want to scan for a whole year
1411 + if (ac->ac_found > ext3_mb_max_to_scan)
1412 + ac->ac_status = AC_STATUS_BREAK;
1415 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1416 + struct ext3_buddy *e3b)
1418 + struct ext3_free_extent ex = ac->ac_b_ex;
1419 + int group = ex.fe_group, max, err;
1421 + J_ASSERT(ex.fe_len > 0);
1422 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1426 + ext3_lock_group(ac->ac_sb, group);
1427 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1431 + ext3_mb_use_best_found(ac, e3b);
1434 + ext3_unlock_group(ac->ac_sb, group);
1436 + ext3_mb_release_desc(e3b);
1441 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1442 + struct ext3_buddy *e3b)
1444 + int group = ac->ac_g_ex.fe_group, max, err;
1445 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1446 + struct ext3_super_block *es = sbi->s_es;
1447 + struct ext3_free_extent ex;
1449 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1453 + ext3_lock_group(ac->ac_sb, group);
1454 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1455 + ac->ac_g_ex.fe_len, &ex);
1457 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1458 + unsigned long start;
1459 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1460 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1461 + if (start % sbi->s_stripe == 0) {
1464 + ext3_mb_use_best_found(ac, e3b);
1466 + } else if (max >= ac->ac_g_ex.fe_len) {
1467 + J_ASSERT(ex.fe_len > 0);
1468 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1469 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1472 + ext3_mb_use_best_found(ac, e3b);
1473 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1474 + /* Sometimes, caller may want to merge even small
1475 + * number of blocks to an existing extent */
1476 + J_ASSERT(ex.fe_len > 0);
1477 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1478 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1481 + ext3_mb_use_best_found(ac, e3b);
1483 + ext3_unlock_group(ac->ac_sb, group);
1485 + ext3_mb_release_desc(e3b);
1491 + * The routine scans buddy structures (not bitmap!) from given order
1492 + * to max order and tries to find big enough chunk to satisfy the req
1494 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1495 + struct ext3_buddy *e3b)
1497 + struct super_block *sb = ac->ac_sb;
1498 + struct ext3_group_info *grp = e3b->bd_info;
1502 + J_ASSERT(ac->ac_2order > 0);
1503 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1504 + if (grp->bb_counters[i] == 0)
1507 + buddy = mb_find_buddy(e3b, i, &max);
1508 + if (buddy == NULL) {
1509 + printk(KERN_ALERT "looking for wrong order?\n");
1513 + k = mb_find_next_zero_bit(buddy, max, 0);
1514 + J_ASSERT(k < max);
1518 + ac->ac_b_ex.fe_len = 1 << i;
1519 + ac->ac_b_ex.fe_start = k << i;
1520 + ac->ac_b_ex.fe_group = e3b->bd_group;
1522 + ext3_mb_use_best_found(ac, e3b);
1523 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1525 + if (unlikely(ext3_mb_stats))
1526 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1533 + * The routine scans the group and measures all found extents.
1534 + * In order to optimize scanning, caller must pass number of
1535 + * free blocks in the group, so the routine can know upper limit.
1537 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1538 + struct ext3_buddy *e3b)
1540 + struct super_block *sb = ac->ac_sb;
1541 + void *bitmap = EXT3_MB_BITMAP(e3b);
1542 + struct ext3_free_extent ex;
1545 + free = e3b->bd_info->bb_free;
1546 + J_ASSERT(free > 0);
1548 + i = e3b->bd_info->bb_first_free;
1550 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1551 + i = mb_find_next_zero_bit(bitmap, EXT3_BLOCKS_PER_GROUP(sb), i);
1552 + if (i >= EXT3_BLOCKS_PER_GROUP(sb)) {
1553 + J_ASSERT(free == 0);
1557 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1558 + J_ASSERT(ex.fe_len > 0);
1559 + J_ASSERT(free >= ex.fe_len);
1561 + ext3_mb_measure_extent(ac, &ex, e3b);
1564 + free -= ex.fe_len;
1569 + * This is a special case for storages like raid5
1570 + * we try to find stripe-aligned chunks for stripe-size requests
1572 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1573 + struct ext3_buddy *e3b)
1575 + struct super_block *sb = ac->ac_sb;
1576 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1577 + void *bitmap = EXT3_MB_BITMAP(e3b);
1578 + struct ext3_free_extent ex;
1579 + unsigned long i, max;
1581 + J_ASSERT(sbi->s_stripe != 0);
1583 + /* find first stripe-aligned block */
1584 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb) +
1585 + le32_to_cpu(sbi->s_es->s_first_data_block);
1586 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1587 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block)) %
1588 + EXT3_BLOCKS_PER_GROUP(sb);
1590 + while (i < EXT3_BLOCKS_PER_GROUP(sb)) {
1591 + if (!mb_test_bit(i, bitmap)) {
1592 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1593 + if (max >= sbi->s_stripe) {
1596 + ext3_mb_use_best_found(ac, e3b);
1600 + i += sbi->s_stripe;
1604 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1605 + int group, int cr)
1607 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1608 + unsigned free, fragments, i, bits;
1610 + J_ASSERT(cr >= 0 && cr < 4);
1611 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1613 + free = grp->bb_free;
1614 + fragments = grp->bb_fragments;
1617 + if (fragments == 0)
1622 + J_ASSERT(ac->ac_2order != 0);
1623 + bits = ac->ac_sb->s_blocksize_bits + 1;
1624 + for (i = ac->ac_2order; i <= bits; i++)
1625 + if (grp->bb_counters[i] > 0)
1629 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1633 + if (free >= ac->ac_g_ex.fe_len)
1645 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1646 + unsigned long goal, int *len, int flags, int *errp)
1648 + struct buffer_head *bitmap_bh = NULL;
1649 + struct ext3_allocation_context ac;
1650 + int i, group, block, cr, err = 0;
1651 + struct ext3_group_desc *gdp;
1652 + struct ext3_super_block *es;
1653 + struct buffer_head *gdp_bh;
1654 + struct ext3_sb_info *sbi;
1655 + struct super_block *sb;
1656 + struct ext3_buddy e3b;
1658 + J_ASSERT(len != NULL);
1659 + J_ASSERT(*len > 0);
1663 + printk("ext3_mb_new_nblocks: nonexistent device");
1667 + if (!test_opt(sb, MBALLOC)) {
1668 + static int ext3_mballoc_warning = 0;
1669 + if (ext3_mballoc_warning == 0) {
1670 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1671 + "mballoc disabled!\n");
1672 + ext3_mballoc_warning++;
1675 + err = ext3_new_block_old(handle, inode, goal, errp);
1679 + ext3_mb_poll_new_transaction(sb, handle);
1681 + sbi = EXT3_SB(sb);
1682 + es = EXT3_SB(sb)->s_es;
1685 + * We can't allocate > group size
1687 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1688 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1690 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1691 + /* someone asks for non-reserved blocks */
1693 + err = ext3_mb_reserve_blocks(sb, 1);
1700 + ac.ac_buddy_page = NULL;
1701 + ac.ac_bitmap_page = NULL;
1704 + * Check quota for allocation of this blocks.
1706 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1714 + /* start searching from the goal */
1715 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1716 + goal >= le32_to_cpu(es->s_blocks_count))
1717 + goal = le32_to_cpu(es->s_first_data_block);
1718 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1719 + EXT3_BLOCKS_PER_GROUP(sb);
1720 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1721 + EXT3_BLOCKS_PER_GROUP(sb));
1723 + /* set up allocation goals */
1724 + ac.ac_b_ex.fe_group = 0;
1725 + ac.ac_b_ex.fe_start = 0;
1726 + ac.ac_b_ex.fe_len = 0;
1727 + ac.ac_status = AC_STATUS_CONTINUE;
1728 + ac.ac_groups_scanned = 0;
1729 + ac.ac_ex_scanned = 0;
1731 + ac.ac_sb = inode->i_sb;
1732 + ac.ac_g_ex.fe_group = group;
1733 + ac.ac_g_ex.fe_start = block;
1734 + ac.ac_g_ex.fe_len = *len;
1735 + ac.ac_flags = flags;
1737 + ac.ac_criteria = 0;
1739 + if (*len == 1 && sbi->s_stripe) {
1740 + /* looks like a metadata, let's use a dirty hack for raid5
1741 + * move all metadata in first groups in hope to hit cached
1742 + * sectors and thus avoid read-modify cycles in raid5 */
1743 + ac.ac_g_ex.fe_group = group = 0;
1746 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1748 + if (i >= ext3_mb_order2_reqs) {
1750 + if ((*len & (~(1 << i))) == 0)
1754 + /* first, try the goal */
1755 + err = ext3_mb_find_by_goal(&ac, &e3b);
1758 + if (ac.ac_status == AC_STATUS_FOUND)
1761 + /* Let's just scan groups to find more-less suitable blocks */
1762 + cr = ac.ac_2order ? 0 : 1;
1764 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1765 + ac.ac_criteria = cr;
1766 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1767 + if (group == EXT3_SB(sb)->s_groups_count)
1770 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1771 + /* we need full data about the group
1772 + * to make a good selection */
1773 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1776 + ext3_mb_release_desc(&e3b);
1779 + /* check is group good for our criteries */
1780 + if (!ext3_mb_good_group(&ac, group, cr))
1783 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1787 + ext3_lock_group(sb, group);
1788 + if (!ext3_mb_good_group(&ac, group, cr)) {
1789 + /* someone did allocation from this group */
1790 + ext3_unlock_group(sb, group);
1791 + ext3_mb_release_desc(&e3b);
1795 + ac.ac_groups_scanned++;
1797 + ext3_mb_simple_scan_group(&ac, &e3b);
1798 + else if (cr == 1 && *len == sbi->s_stripe)
1799 + ext3_mb_scan_aligned(&ac, &e3b);
1801 + ext3_mb_complex_scan_group(&ac, &e3b);
1803 + ext3_unlock_group(sb, group);
1805 + ext3_mb_release_desc(&e3b);
1807 + if (ac.ac_status != AC_STATUS_CONTINUE)
1812 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1813 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1815 + * We've been searching too long. Let's try to allocate
1816 + * the best chunk we've found so far
1819 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1820 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1821 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1822 + ac.ac_g_ex.fe_len);*/
1823 + ext3_mb_try_best_found(&ac, &e3b);
1824 + if (ac.ac_status != AC_STATUS_FOUND) {
1826 + * Someone more lucky has already allocated it.
1827 + * The only thing we can do is just take first
1829 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1831 + ac.ac_b_ex.fe_group = 0;
1832 + ac.ac_b_ex.fe_start = 0;
1833 + ac.ac_b_ex.fe_len = 0;
1834 + ac.ac_status = AC_STATUS_CONTINUE;
1835 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1841 + if (ac.ac_status != AC_STATUS_FOUND) {
1843 + * We aren't lucky definitely
1845 + DQUOT_FREE_BLOCK(inode, *len);
1849 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1850 + ac.ac_status, ac.ac_flags);
1851 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1852 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1853 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1854 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1855 + sbi->s_blocks_reserved, ac.ac_found);
1856 + printk("EXT3-fs: groups: ");
1857 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1858 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1865 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1867 + /* good news - free block(s) have been found. now it's time
1868 + * to mark block(s) in good old journaled bitmap */
1869 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1870 + + ac.ac_b_ex.fe_start
1871 + + le32_to_cpu(es->s_first_data_block);
1873 + /* we made a desicion, now mark found blocks in good old
1874 + * bitmap to be journaled */
1876 + ext3_debug("using block group %d(%d)\n",
1877 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1879 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1885 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1891 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1897 + err = ext3_journal_get_write_access(handle, gdp_bh);
1901 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1902 + + ac.ac_b_ex.fe_start
1903 + + le32_to_cpu(es->s_first_data_block);
1905 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1906 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1907 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1908 + EXT3_SB(sb)->s_itb_per_group))
1909 + ext3_error(sb, "ext3_new_block",
1910 + "Allocating block in system zone - "
1911 + "block = %u", block);
1912 +#ifdef AGGRESSIVE_CHECK
1913 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1914 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1916 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1918 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1919 + gdp->bg_free_blocks_count =
1920 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1921 + - ac.ac_b_ex.fe_len);
1922 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1923 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1925 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1928 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1934 + brelse(bitmap_bh);
1936 + /* drop non-allocated, but dquote'd blocks */
1937 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1938 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1940 + *len = ac.ac_b_ex.fe_len;
1941 + J_ASSERT(*len > 0);
1942 + J_ASSERT(block != 0);
1946 + /* if we've already allocated something, roll it back */
1947 + if (ac.ac_status == AC_STATUS_FOUND) {
1948 + /* FIXME: free blocks here */
1951 + DQUOT_FREE_BLOCK(inode, *len);
1952 + brelse(bitmap_bh);
1956 + if (ac.ac_buddy_page)
1957 + page_cache_release(ac.ac_buddy_page);
1958 + if (ac.ac_bitmap_page)
1959 + page_cache_release(ac.ac_bitmap_page);
1961 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1962 + /* block wasn't reserved before and we reserved it
1963 + * at the beginning of allocation. it doesn't matter
1964 + * whether we allocated anything or we failed: time
1965 + * to release reservation. NOTE: because I expect
1966 + * any multiblock request from delayed allocation
1967 + * path only, here is single block always */
1968 + ext3_mb_release_blocks(sb, 1);
1971 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1972 + atomic_inc(&sbi->s_bal_reqs);
1973 + atomic_add(*len, &sbi->s_bal_allocated);
1974 + if (*len >= ac.ac_g_ex.fe_len)
1975 + atomic_inc(&sbi->s_bal_success);
1976 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1977 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1978 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1979 + atomic_inc(&sbi->s_bal_goals);
1980 + if (ac.ac_found > ext3_mb_max_to_scan)
1981 + atomic_inc(&sbi->s_bal_breaks);
1984 + ext3_mb_store_history(sb, inode->i_ino, &ac);
1988 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1990 +#ifdef EXT3_MB_HISTORY
1991 +struct ext3_mb_proc_session {
1992 + struct ext3_mb_history *history;
1993 + struct super_block *sb;
1998 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1999 + struct ext3_mb_history *hs,
2002 + if (hs == s->history + s->max)
2004 + if (!first && hs == s->history + s->start)
2006 + while (hs->goal.fe_len == 0) {
2008 + if (hs == s->history + s->max)
2010 + if (hs == s->history + s->start)
2016 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2018 + struct ext3_mb_proc_session *s = seq->private;
2019 + struct ext3_mb_history *hs;
2023 + return SEQ_START_TOKEN;
2024 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2027 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2031 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2033 + struct ext3_mb_proc_session *s = seq->private;
2034 + struct ext3_mb_history *hs = v;
2037 + if (v == SEQ_START_TOKEN)
2038 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2040 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2043 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2045 + struct ext3_mb_history *hs = v;
2046 + char buf[20], buf2[20];
2048 + if (v == SEQ_START_TOKEN) {
2049 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2050 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2051 + "merge", "tail", "broken");
2055 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2056 + hs->goal.fe_start, hs->goal.fe_len);
2057 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2058 + hs->result.fe_start, hs->result.fe_len);
2059 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2060 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2061 + hs->cr, hs->merged ? "M" : "", hs->tail,
2062 + hs->buddy ? 1 << hs->buddy : 0);
2066 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2070 +static struct seq_operations ext3_mb_seq_history_ops = {
2071 + .start = ext3_mb_seq_history_start,
2072 + .next = ext3_mb_seq_history_next,
2073 + .stop = ext3_mb_seq_history_stop,
2074 + .show = ext3_mb_seq_history_show,
2077 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2079 + struct super_block *sb = PDE(inode)->data;
2080 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2081 + struct ext3_mb_proc_session *s;
2084 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2087 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2088 + s->history = kmalloc(size, GFP_KERNEL);
2089 + if (s->history == NULL) {
2094 + spin_lock(&sbi->s_mb_history_lock);
2095 + memcpy(s->history, sbi->s_mb_history, size);
2096 + s->max = sbi->s_mb_history_max;
2097 + s->start = sbi->s_mb_history_cur % s->max;
2098 + spin_unlock(&sbi->s_mb_history_lock);
2100 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2102 + struct seq_file *m = (struct seq_file *)file->private_data;
2105 + kfree(s->history);
2112 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2114 + struct seq_file *seq = (struct seq_file *)file->private_data;
2115 + struct ext3_mb_proc_session *s = seq->private;
2116 + kfree(s->history);
2118 + return seq_release(inode, file);
2121 +static struct file_operations ext3_mb_seq_history_fops = {
2122 + .owner = THIS_MODULE,
2123 + .open = ext3_mb_seq_history_open,
2125 + .llseek = seq_lseek,
2126 + .release = ext3_mb_seq_history_release,
2129 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2131 + struct super_block *sb = seq->private;
2132 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2135 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2139 + return (void *) group;
2142 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2144 + struct super_block *sb = seq->private;
2145 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2149 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2152 + return (void *) group;;
2155 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2157 + struct super_block *sb = seq->private;
2158 + long group = (long) v, i;
2160 + struct ext3_group_info info;
2161 + unsigned short counters[16];
2166 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2167 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2168 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2169 + "2^11", "2^12", "2^13");
2171 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2172 + sizeof(struct ext3_group_info);
2173 + ext3_lock_group(sb, group);
2174 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2175 + ext3_unlock_group(sb, group);
2177 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2180 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2181 + sg.info.bb_fragments, sg.info.bb_first_free);
2182 + for (i = 0; i <= 13; i++)
2183 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2184 + sg.info.bb_counters[i] : 0);
2185 + seq_printf(seq, " ]\n");
2190 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2194 +static struct seq_operations ext3_mb_seq_groups_ops = {
2195 + .start = ext3_mb_seq_groups_start,
2196 + .next = ext3_mb_seq_groups_next,
2197 + .stop = ext3_mb_seq_groups_stop,
2198 + .show = ext3_mb_seq_groups_show,
2201 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2203 + struct super_block *sb = PDE(inode)->data;
2206 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2208 + struct seq_file *m = (struct seq_file *)file->private_data;
2215 +static struct file_operations ext3_mb_seq_groups_fops = {
2216 + .owner = THIS_MODULE,
2217 + .open = ext3_mb_seq_groups_open,
2219 + .llseek = seq_lseek,
2220 + .release = seq_release,
2223 +static void ext3_mb_history_release(struct super_block *sb)
2225 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2228 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2229 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2230 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2231 + remove_proc_entry(name, proc_root_ext3);
2233 + if (sbi->s_mb_history)
2234 + kfree(sbi->s_mb_history);
2237 +static void ext3_mb_history_init(struct super_block *sb)
2239 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2243 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2244 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2245 + if (sbi->s_mb_proc != NULL) {
2246 + struct proc_dir_entry *p;
2247 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2249 + p->proc_fops = &ext3_mb_seq_history_fops;
2252 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2254 + p->proc_fops = &ext3_mb_seq_groups_fops;
2259 + sbi->s_mb_history_max = 1000;
2260 + sbi->s_mb_history_cur = 0;
2261 + spin_lock_init(&sbi->s_mb_history_lock);
2262 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2263 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2264 + if (likely(sbi->s_mb_history != NULL))
2265 + memset(sbi->s_mb_history, 0, i);
2266 + /* if we can't allocate history, then we simple won't use it */
2270 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2271 + struct ext3_allocation_context *ac)
2273 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2274 + struct ext3_mb_history h;
2276 + if (unlikely(sbi->s_mb_history == NULL))
2279 + h.pid = current->pid;
2281 + h.goal = ac->ac_g_ex;
2282 + h.result = ac->ac_b_ex;
2283 + h.found = ac->ac_found;
2284 + h.cr = ac->ac_criteria;
2285 + h.groups = ac->ac_groups_scanned;
2286 + h.tail = ac->ac_tail;
2287 + h.buddy = ac->ac_buddy;
2289 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2290 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2293 + spin_lock(&sbi->s_mb_history_lock);
2294 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2295 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2296 + sbi->s_mb_history_cur = 0;
2297 + spin_unlock(&sbi->s_mb_history_lock);
2301 +#define ext3_mb_history_release(sb)
2302 +#define ext3_mb_history_init(sb)
2305 +int ext3_mb_init_backend(struct super_block *sb)
2307 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2308 + int i, j, len, metalen;
2309 + int num_meta_group_infos =
2310 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2311 + EXT3_DESC_PER_BLOCK_BITS(sb);
2312 + struct ext3_group_info **meta_group_info;
2314 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2315 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2316 + * So a two level scheme suffices for now. */
2317 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2318 + num_meta_group_infos, GFP_KERNEL);
2319 + if (sbi->s_group_info == NULL) {
2320 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2323 + sbi->s_buddy_cache = new_inode(sb);
2324 + if (sbi->s_buddy_cache == NULL) {
2325 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2329 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2330 + for (i = 0; i < num_meta_group_infos; i++) {
2331 + if ((i + 1) == num_meta_group_infos)
2332 + metalen = sizeof(*meta_group_info) *
2333 + (sbi->s_groups_count -
2334 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2335 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2336 + if (meta_group_info == NULL) {
2337 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2339 + goto err_freemeta;
2341 + sbi->s_group_info[i] = meta_group_info;
2345 + * calculate needed size. if change bb_counters size,
2346 + * don't forget about ext3_mb_generate_buddy()
2348 + len = sizeof(struct ext3_group_info);
2349 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2350 + for (i = 0; i < sbi->s_groups_count; i++) {
2351 + struct ext3_group_desc * desc;
2354 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2355 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2357 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2358 + if (meta_group_info[j] == NULL) {
2359 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2361 + goto err_freebuddy;
2363 + desc = ext3_get_group_desc(sb, i, NULL);
2364 + if (desc == NULL) {
2365 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2366 + goto err_freebuddy;
2368 + memset(meta_group_info[j], 0, len);
2369 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2370 + &meta_group_info[j]->bb_state);
2371 + meta_group_info[j]->bb_free =
2372 + le16_to_cpu(desc->bg_free_blocks_count);
2379 + kfree(EXT3_GROUP_INFO(sb, i));
2382 + i = num_meta_group_infos;
2385 + kfree(sbi->s_group_info[i]);
2386 + iput(sbi->s_buddy_cache);
2388 + kfree(sbi->s_group_info);
2392 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2394 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2395 + struct inode *root = sb->s_root->d_inode;
2396 + unsigned i, offset, max;
2397 + struct dentry *dentry;
2399 + if (!test_opt(sb, MBALLOC))
2402 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2404 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2405 + if (sbi->s_mb_offsets == NULL) {
2406 + clear_opt(sbi->s_mount_opt, MBALLOC);
2409 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2410 + if (sbi->s_mb_maxs == NULL) {
2411 + clear_opt(sbi->s_mount_opt, MBALLOC);
2412 + kfree(sbi->s_mb_maxs);
2416 + /* order 0 is regular bitmap */
2417 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2418 + sbi->s_mb_offsets[0] = 0;
2422 + max = sb->s_blocksize << 2;
2424 + sbi->s_mb_offsets[i] = offset;
2425 + sbi->s_mb_maxs[i] = max;
2426 + offset += 1 << (sb->s_blocksize_bits - i);
2429 + } while (i <= sb->s_blocksize_bits + 1);
2431 + /* init file for buddy data */
2432 + if ((i = ext3_mb_init_backend(sb))) {
2433 + clear_opt(sbi->s_mount_opt, MBALLOC);
2434 + kfree(sbi->s_mb_offsets);
2435 + kfree(sbi->s_mb_maxs);
2439 + spin_lock_init(&sbi->s_reserve_lock);
2440 + spin_lock_init(&sbi->s_md_lock);
2441 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2442 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2443 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2444 + spin_lock_init(&sbi->s_bal_lock);
2446 + /* remove old on-disk buddy file */
2447 + down(&root->i_sem);
2448 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2449 + if (dentry->d_inode != NULL) {
2450 + i = vfs_unlink(root, dentry);
2452 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2457 + ext3_mb_history_init(sb);
2459 + printk("EXT3-fs: mballoc enabled\n");
2463 +int ext3_mb_release(struct super_block *sb)
2465 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2466 + int i, num_meta_group_infos;
2468 + if (!test_opt(sb, MBALLOC))
2471 + /* release freed, non-committed blocks */
2472 + spin_lock(&sbi->s_md_lock);
2473 + list_splice_init(&sbi->s_closed_transaction,
2474 + &sbi->s_committed_transaction);
2475 + list_splice_init(&sbi->s_active_transaction,
2476 + &sbi->s_committed_transaction);
2477 + spin_unlock(&sbi->s_md_lock);
2478 + ext3_mb_free_committed_blocks(sb);
2480 + if (sbi->s_group_info) {
2481 + for (i = 0; i < sbi->s_groups_count; i++)
2482 + kfree(EXT3_GROUP_INFO(sb, i));
2483 + num_meta_group_infos = (sbi->s_groups_count +
2484 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2485 + EXT3_DESC_PER_BLOCK_BITS(sb);
2486 + for (i = 0; i < num_meta_group_infos; i++)
2487 + kfree(sbi->s_group_info[i]);
2488 + kfree(sbi->s_group_info);
2490 + if (sbi->s_mb_offsets)
2491 + kfree(sbi->s_mb_offsets);
2492 + if (sbi->s_mb_maxs)
2493 + kfree(sbi->s_mb_maxs);
2494 + if (sbi->s_buddy_cache)
2495 + iput(sbi->s_buddy_cache);
2496 + if (sbi->s_blocks_reserved)
2497 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2498 + sbi->s_blocks_reserved);
2499 + if (ext3_mb_stats) {
2500 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2501 + atomic_read(&sbi->s_bal_allocated),
2502 + atomic_read(&sbi->s_bal_reqs),
2503 + atomic_read(&sbi->s_bal_success));
2504 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2505 + "%u 2^N hits, %u breaks\n",
2506 + atomic_read(&sbi->s_bal_ex_scanned),
2507 + atomic_read(&sbi->s_bal_goals),
2508 + atomic_read(&sbi->s_bal_2orders),
2509 + atomic_read(&sbi->s_bal_breaks));
2510 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2511 + sbi->s_mb_buddies_generated++,
2512 + sbi->s_mb_generation_time);
2515 + ext3_mb_history_release(sb);
2520 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2522 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2523 + int err, i, count = 0, count2 = 0;
2524 + struct ext3_free_metadata *md;
2525 + struct ext3_buddy e3b;
2527 + if (list_empty(&sbi->s_committed_transaction))
2530 + /* there is committed blocks to be freed yet */
2532 + /* get next array of blocks */
2534 + spin_lock(&sbi->s_md_lock);
2535 + if (!list_empty(&sbi->s_committed_transaction)) {
2536 + md = list_entry(sbi->s_committed_transaction.next,
2537 + struct ext3_free_metadata, list);
2538 + list_del(&md->list);
2540 + spin_unlock(&sbi->s_md_lock);
2545 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2546 + md->num, md->group, md);
2548 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2549 + /* we expect to find existing buddy because it's pinned */
2552 + /* there are blocks to put in buddy to make them really free */
2555 + ext3_lock_group(sb, md->group);
2556 + for (i = 0; i < md->num; i++) {
2557 + mb_debug(" %u", md->blocks[i]);
2558 + mb_free_blocks(&e3b, md->blocks[i], 1);
2561 + ext3_unlock_group(sb, md->group);
2563 + /* balance refcounts from ext3_mb_free_metadata() */
2564 + page_cache_release(e3b.bd_buddy_page);
2565 + page_cache_release(e3b.bd_bitmap_page);
2568 + ext3_mb_release_desc(&e3b);
2571 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2574 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2576 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2578 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2581 + /* new transaction! time to close last one and free blocks for
2582 + * committed transaction. we know that only transaction can be
2583 + * active, so previos transaction can be being logged and we
2584 + * know that transaction before previous is known to be already
2585 + * logged. this means that now we may free blocks freed in all
2586 + * transactions before previous one. hope I'm clear enough ... */
2588 + spin_lock(&sbi->s_md_lock);
2589 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2590 + mb_debug("new transaction %lu, old %lu\n",
2591 + (unsigned long) handle->h_transaction->t_tid,
2592 + (unsigned long) sbi->s_last_transaction);
2593 + list_splice_init(&sbi->s_closed_transaction,
2594 + &sbi->s_committed_transaction);
2595 + list_splice_init(&sbi->s_active_transaction,
2596 + &sbi->s_closed_transaction);
2597 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2599 + spin_unlock(&sbi->s_md_lock);
2601 + ext3_mb_free_committed_blocks(sb);
2604 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2605 + int group, int block, int count)
2607 + struct ext3_group_info *db = e3b->bd_info;
2608 + struct super_block *sb = e3b->bd_sb;
2609 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2610 + struct ext3_free_metadata *md;
2613 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2614 + J_ASSERT(e3b->bd_buddy_page != NULL);
2616 + ext3_lock_group(sb, group);
2617 + for (i = 0; i < count; i++) {
2618 + md = db->bb_md_cur;
2619 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2620 + db->bb_md_cur = NULL;
2625 + ext3_unlock_group(sb, group);
2626 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2630 + md->group = group;
2632 + ext3_lock_group(sb, group);
2633 + if (db->bb_md_cur == NULL) {
2634 + spin_lock(&sbi->s_md_lock);
2635 + list_add(&md->list, &sbi->s_active_transaction);
2636 + spin_unlock(&sbi->s_md_lock);
2637 + /* protect buddy cache from being freed,
2638 + * otherwise we'll refresh it from
2639 + * on-disk bitmap and lose not-yet-available
2641 + page_cache_get(e3b->bd_buddy_page);
2642 + page_cache_get(e3b->bd_bitmap_page);
2643 + db->bb_md_cur = md;
2644 + db->bb_tid = handle->h_transaction->t_tid;
2645 + mb_debug("new md 0x%p for group %u\n",
2649 + md = db->bb_md_cur;
2653 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2654 + md->blocks[md->num] = block + i;
2656 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2657 + /* no more space, put full container on a sb's list */
2658 + db->bb_md_cur = NULL;
2661 + ext3_unlock_group(sb, group);
2665 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2666 + unsigned long block, unsigned long count,
2667 + int metadata, int *freed)
2669 + struct buffer_head *bitmap_bh = NULL;
2670 + struct ext3_group_desc *gdp;
2671 + struct ext3_super_block *es;
2672 + unsigned long bit, overflow;
2673 + struct buffer_head *gd_bh;
2674 + unsigned long block_group;
2675 + struct ext3_sb_info *sbi;
2676 + struct super_block *sb;
2677 + struct ext3_buddy e3b;
2683 + printk ("ext3_free_blocks: nonexistent device");
2687 + ext3_mb_poll_new_transaction(sb, handle);
2689 + sbi = EXT3_SB(sb);
2690 + es = EXT3_SB(sb)->s_es;
2691 + if (block < le32_to_cpu(es->s_first_data_block) ||
2692 + block + count < block ||
2693 + block + count > le32_to_cpu(es->s_blocks_count)) {
2694 + ext3_error (sb, "ext3_free_blocks",
2695 + "Freeing blocks not in datazone - "
2696 + "block = %lu, count = %lu", block, count);
2697 + goto error_return;
2700 + ext3_debug("freeing block %lu\n", block);
2704 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2705 + EXT3_BLOCKS_PER_GROUP(sb);
2706 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2707 + EXT3_BLOCKS_PER_GROUP(sb);
2709 + * Check to see if we are freeing blocks across a group
2712 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2713 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2714 + count -= overflow;
2716 + brelse(bitmap_bh);
2717 + bitmap_bh = read_block_bitmap(sb, block_group);
2719 + goto error_return;
2720 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2722 + goto error_return;
2724 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2725 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2726 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2727 + EXT3_SB(sb)->s_itb_per_group) ||
2728 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2729 + EXT3_SB(sb)->s_itb_per_group))
2730 + ext3_error (sb, "ext3_free_blocks",
2731 + "Freeing blocks in system zones - "
2732 + "Block = %lu, count = %lu",
2735 + BUFFER_TRACE(bitmap_bh, "getting write access");
2736 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2738 + goto error_return;
2741 + * We are about to modify some metadata. Call the journal APIs
2742 + * to unshare ->b_data if a currently-committing transaction is
2745 + BUFFER_TRACE(gd_bh, "get_write_access");
2746 + err = ext3_journal_get_write_access(handle, gd_bh);
2748 + goto error_return;
2750 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2752 + goto error_return;
2754 +#ifdef AGGRESSIVE_CHECK
2757 + for (i = 0; i < count; i++)
2758 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2761 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2763 + /* We dirtied the bitmap block */
2764 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2765 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2768 + /* blocks being freed are metadata. these blocks shouldn't
2769 + * be used until this transaction is committed */
2770 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2772 + ext3_lock_group(sb, block_group);
2773 + mb_free_blocks(&e3b, bit, count);
2774 + ext3_unlock_group(sb, block_group);
2777 + spin_lock(sb_bgl_lock(sbi, block_group));
2778 + gdp->bg_free_blocks_count =
2779 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2780 + spin_unlock(sb_bgl_lock(sbi, block_group));
2781 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2783 + ext3_mb_release_desc(&e3b);
2787 + /* And the group descriptor block */
2788 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2789 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2790 + if (!err) err = ret;
2792 + if (overflow && !err) {
2799 + brelse(bitmap_bh);
2800 + ext3_std_error(sb, err);
2804 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2806 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2807 + int free, ret = -ENOSPC;
2809 + BUG_ON(blocks < 0);
2810 + spin_lock(&sbi->s_reserve_lock);
2811 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2812 + if (blocks <= free - sbi->s_blocks_reserved) {
2813 + sbi->s_blocks_reserved += blocks;
2816 + spin_unlock(&sbi->s_reserve_lock);
2820 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2822 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2824 + BUG_ON(blocks < 0);
2825 + spin_lock(&sbi->s_reserve_lock);
2826 + sbi->s_blocks_reserved -= blocks;
2827 + WARN_ON(sbi->s_blocks_reserved < 0);
2828 + if (sbi->s_blocks_reserved < 0)
2829 + sbi->s_blocks_reserved = 0;
2830 + spin_unlock(&sbi->s_reserve_lock);
2833 +int ext3_new_block(handle_t *handle, struct inode *inode,
2834 + unsigned long goal, int *errp)
2838 + if (!test_opt(inode->i_sb, MBALLOC)) {
2839 + ret = ext3_new_block_old(handle, inode, goal, errp);
2843 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2849 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2850 + unsigned long block, unsigned long count, int metadata)
2852 + struct super_block *sb;
2856 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2857 + ext3_free_blocks_sb(handle, sb, block, count, &freed);
2859 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2861 + DQUOT_FREE_BLOCK(inode, freed);
2865 +#define EXT3_ROOT "ext3"
2866 +#define EXT3_MB_STATS_NAME "mb_stats"
2867 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2868 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2869 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2871 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2872 + int count, int *eof, void *data)
2880 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2885 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2886 + unsigned long count, void *data)
2890 + if (count >= sizeof(str)) {
2891 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2892 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2893 + return -EOVERFLOW;
2896 + if (copy_from_user(str, buffer, count))
2899 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2900 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2904 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2905 + int count, int *eof, void *data)
2913 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2918 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2919 + unsigned long count, void *data)
2924 + if (count >= sizeof(str)) {
2925 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2926 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2927 + return -EOVERFLOW;
2930 + if (copy_from_user(str, buffer, count))
2933 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2934 + value = simple_strtol(str, NULL, 0);
2938 + ext3_mb_max_to_scan = value;
2943 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2944 + int count, int *eof, void *data)
2952 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2957 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2958 + unsigned long count, void *data)
2963 + if (count >= sizeof(str)) {
2964 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2965 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2966 + return -EOVERFLOW;
2969 + if (copy_from_user(str, buffer, count))
2972 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2973 + value = simple_strtol(str, NULL, 0);
2977 + ext3_mb_min_to_scan = value;
2982 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2983 + int count, int *eof, void *data)
2991 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
2996 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2997 + unsigned long count, void *data)
3002 + if (count >= sizeof(str)) {
3003 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
3004 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
3005 + return -EOVERFLOW;
3008 + if (copy_from_user(str, buffer, count))
3011 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
3012 + value = simple_strtol(str, NULL, 0);
3016 + ext3_mb_order2_reqs = value;
3021 +int __init init_ext3_proc(void)
3023 + struct proc_dir_entry *proc_ext3_mb_stats;
3024 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3025 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3026 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3028 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3029 + if (proc_root_ext3 == NULL) {
3030 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3034 + /* Initialize EXT3_MB_STATS_NAME */
3035 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3036 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3037 + if (proc_ext3_mb_stats == NULL) {
3038 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3039 + EXT3_MB_STATS_NAME);
3040 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3044 + proc_ext3_mb_stats->data = NULL;
3045 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3046 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3048 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3049 + proc_ext3_mb_max_to_scan = create_proc_entry(
3050 + EXT3_MB_MAX_TO_SCAN_NAME,
3051 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3052 + if (proc_ext3_mb_max_to_scan == NULL) {
3053 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3054 + EXT3_MB_MAX_TO_SCAN_NAME);
3055 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3056 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3060 + proc_ext3_mb_max_to_scan->data = NULL;
3061 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3062 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3064 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3065 + proc_ext3_mb_min_to_scan = create_proc_entry(
3066 + EXT3_MB_MIN_TO_SCAN_NAME,
3067 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3068 + if (proc_ext3_mb_min_to_scan == NULL) {
3069 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3070 + EXT3_MB_MIN_TO_SCAN_NAME);
3071 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3072 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3073 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3077 + proc_ext3_mb_min_to_scan->data = NULL;
3078 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3079 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3081 + /* Initialize EXT3_ORDER2_REQ */
3082 + proc_ext3_mb_order2_req = create_proc_entry(
3083 + EXT3_MB_ORDER2_REQ,
3084 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3085 + if (proc_ext3_mb_order2_req == NULL) {
3086 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3087 + EXT3_MB_ORDER2_REQ);
3088 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3089 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3090 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3091 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3095 + proc_ext3_mb_order2_req->data = NULL;
3096 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3097 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3102 +void exit_ext3_proc(void)
3104 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3105 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3106 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3107 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3108 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3110 Index: linux-2.6.9-full/fs/ext3/Makefile
3111 ===================================================================
3112 --- linux-2.6.9-full.orig/fs/ext3/Makefile 2006-06-01 14:58:46.000000000 +0400
3113 +++ linux-2.6.9-full/fs/ext3/Makefile 2006-10-24 12:54:31.000000000 +0400
3114 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
3116 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3117 ioctl.o namei.o super.o symlink.o hash.o resize.o \
3119 + extents.o mballoc.o
3121 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3122 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o