1 Index: linux-2.6.16.i686/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.16.i686.orig/include/linux/ext3_fs.h 2006-05-30 22:55:32.000000000 +0800
4 +++ linux-2.6.16.i686/include/linux/ext3_fs.h 2006-05-30 23:02:59.000000000 +0800
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
21 #define EXT3_MOUNT_IOPEN_NOPRIV 0x800000/* Make iopen world-readable */
22 #define EXT3_MOUNT_EXTENTS 0x1000000/* Extents support */
23 #define EXT3_MOUNT_EXTDEBUG 0x2000000/* Extents debug */
24 +#define EXT3_MOUNT_MBALLOC 0x4000000/* Buddy allocation support */
26 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
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
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 *handle, struct inode *inode,
50 + unsigned long goal, int *errp);
51 extern void ext3_free_blocks_sb (handle_t *, struct super_block *,
52 unsigned long, unsigned long, int *);
53 extern unsigned long ext3_count_free_blocks (struct super_block *);
55 extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
56 unsigned int cmd, unsigned long arg);
59 +extern long ext3_mb_stats;
60 +extern long ext3_mb_max_to_scan;
61 +extern int ext3_mb_init(struct super_block *, int);
62 +extern int ext3_mb_release(struct super_block *);
63 +extern int ext3_mb_new_blocks(handle_t *, struct inode *, unsigned long, int *, int, int *);
64 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
65 +extern void ext3_mb_release_blocks(struct super_block *, int);
66 +int __init init_ext3_proc(void);
67 +void exit_ext3_proc(void);
69 #endif /* __KERNEL__ */
71 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
72 Index: linux-2.6.16.i686/include/linux/ext3_fs_sb.h
73 ===================================================================
74 --- linux-2.6.16.i686.orig/include/linux/ext3_fs_sb.h 2006-03-20 13:53:29.000000000 +0800
75 +++ linux-2.6.16.i686/include/linux/ext3_fs_sb.h 2006-05-30 23:02:59.000000000 +0800
77 #include <linux/wait.h>
78 #include <linux/blockgroup_lock.h>
79 #include <linux/percpu_counter.h>
80 +#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
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.16.i686/fs/ext3/super.c
136 ===================================================================
137 --- linux-2.6.16.i686.orig/fs/ext3/super.c 2006-05-30 22:55:32.000000000 +0800
138 +++ linux-2.6.16.i686/fs/ext3/super.c 2006-05-30 23:02:59.000000000 +0800
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);
148 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
149 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
150 Opt_extents, Opt_noextents, Opt_extdebug,
151 + Opt_mballoc, Opt_nomballoc, Opt_stripe,
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 @@ -1041,6 +1043,19 @@
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 @@ -1766,6 +1771,7 @@
186 ext3_count_dirs(sb));
189 + ext3_mb_init(sb, needs_recovery);
193 @@ -2699,7 +2705,13 @@
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 @@ -2721,6 +2733,7 @@
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.16.i686/fs/ext3/extents.c
217 ===================================================================
218 --- linux-2.6.16.i686.orig/fs/ext3/extents.c 2006-05-30 22:55:32.000000000 +0800
219 +++ linux-2.6.16.i686/fs/ext3/extents.c 2006-05-30 23:02:59.000000000 +0800
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 @@ -1428,7 +1428,7 @@
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 @@ -1913,10 +1913,12 @@
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 @@ -1928,7 +1930,7 @@
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.16.i686/fs/ext3/inode.c
262 ===================================================================
263 --- linux-2.6.16.i686.orig/fs/ext3/inode.c 2006-05-30 22:55:32.000000000 +0800
264 +++ linux-2.6.16.i686/fs/ext3/inode.c 2006-05-30 23:02:59.000000000 +0800
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 @@ -1862,7 +1862,7 @@
278 - ext3_free_blocks(handle, inode, block_to_free, count);
279 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
283 @@ -2035,7 +2035,7 @@
284 ext3_journal_test_restart(handle, inode);
287 - ext3_free_blocks(handle, inode, nr, 1);
288 + ext3_free_blocks(handle, inode, nr, 1, 1);
292 Index: linux-2.6.16.i686/fs/ext3/balloc.c
293 ===================================================================
294 --- linux-2.6.16.i686.orig/fs/ext3/balloc.c 2006-03-20 13:53:29.000000000 +0800
295 +++ linux-2.6.16.i686/fs/ext3/balloc.c 2006-05-30 23:02:59.000000000 +0800
298 * Return buffer_head on success or NULL in case of failure.
300 -static struct buffer_head *
301 +struct buffer_head *
302 read_block_bitmap(struct super_block *sb, unsigned int block_group)
304 struct ext3_group_desc * desc;
309 -/* Free given blocks, update quota and i_blocks field */
310 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
311 - unsigned long block, unsigned long count)
313 - struct super_block * sb;
314 - int dquot_freed_blocks;
318 - printk ("ext3_free_blocks: nonexistent device");
321 - ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
322 - if (dquot_freed_blocks)
323 - DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
328 * For ext3 allocations, we must not reuse any blocks which are
329 * allocated in the bitmap buffer's "last committed data" copy. This
330 @@ -1154,7 +1136,7 @@
331 * bitmap, and then for any free bit if that fails.
332 * This function also updates quota and i_blocks field.
334 -int ext3_new_block(handle_t *handle, struct inode *inode,
335 +int ext3_new_block_old(handle_t *handle, struct inode *inode,
336 unsigned long goal, int *errp)
338 struct buffer_head *bitmap_bh = NULL;
339 Index: linux-2.6.16.i686/fs/ext3/xattr.c
340 ===================================================================
341 --- linux-2.6.16.i686.orig/fs/ext3/xattr.c 2006-03-20 13:53:29.000000000 +0800
342 +++ linux-2.6.16.i686/fs/ext3/xattr.c 2006-05-30 23:02:59.000000000 +0800
344 ea_bdebug(bh, "refcount now=0; freeing");
346 mb_cache_entry_free(ce);
347 - ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
348 + ext3_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
350 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
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 Index: linux-2.6.16.i686/fs/ext3/mballoc.c
362 ===================================================================
363 --- linux-2.6.16.i686.orig/fs/ext3/mballoc.c 2006-05-31 04:14:15.752410384 +0800
364 +++ linux-2.6.16.i686/fs/ext3/mballoc.c 2006-05-30 23:03:38.000000000 +0800
367 + * Copyright 2008 Sun Microsystems, Inc.
368 + * Written by Alex Tomas <alex@clusterfs.com>
370 + * This program is free software; you can redistribute it and/or modify
371 + * it under the terms of the GNU General Public License version 2 as
372 + * published by the Free Software Foundation.
374 + * This program is distributed in the hope that it will be useful,
375 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
376 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
377 + * GNU General Public License for more details.
379 + * You should have received a copy of the GNU General Public Licens
380 + * along with this program; if not, write to the Free Software
381 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
386 + * mballoc.c contains the multiblocks allocation routines
389 +#include <linux/config.h>
390 +#include <linux/time.h>
391 +#include <linux/fs.h>
392 +#include <linux/namei.h>
393 +#include <linux/jbd.h>
394 +#include <linux/ext3_fs.h>
395 +#include <linux/ext3_jbd.h>
396 +#include <linux/quotaops.h>
397 +#include <linux/buffer_head.h>
398 +#include <linux/module.h>
399 +#include <linux/swap.h>
400 +#include <linux/proc_fs.h>
401 +#include <linux/pagemap.h>
402 +#include <linux/seq_file.h>
406 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
407 + * - track min/max extents in each group for better group selection
408 + * - mb_mark_used() may allocate chunk right after splitting buddy
409 + * - special flag to advice allocator to look for requested + N blocks
410 + * this may improve interaction between extents and mballoc
411 + * - tree of groups sorted by number of free blocks
412 + * - percpu reservation code (hotpath)
417 + * with AGRESSIVE_CHECK allocator runs consistency checks over
418 + * structures. these checks slow things down a lot
420 +#define AGGRESSIVE_CHECK__
426 +#define mb_debug(fmt,a...) printk(fmt, ##a)
428 +#define mb_debug(fmt,a...)
432 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
433 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
435 +#define EXT3_MB_HISTORY
438 + * How long mballoc can look for a best extent (in found extents)
440 +long ext3_mb_max_to_scan = 500;
443 + * How long mballoc must look for a best extent
445 +long ext3_mb_min_to_scan = 30;
448 + * with 'ext3_mb_stats' allocator will collect stats that will be
449 + * shown at umount. The collecting costs though!
452 +long ext3_mb_stats = 1;
455 + * for which requests use 2^N search using buddies
457 +long ext3_mb_order2_reqs = 8;
459 +#ifdef EXT3_BB_MAX_BLOCKS
460 +#undef EXT3_BB_MAX_BLOCKS
462 +#define EXT3_BB_MAX_BLOCKS 30
464 +struct ext3_free_metadata {
465 + unsigned short group;
466 + unsigned short num;
467 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
468 + struct list_head list;
471 +struct ext3_group_info {
472 + unsigned long bb_state;
473 + unsigned long bb_tid;
474 + struct ext3_free_metadata *bb_md_cur;
475 + unsigned short bb_first_free;
476 + unsigned short bb_free;
477 + unsigned short bb_fragments;
478 + unsigned short bb_counters[];
482 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
483 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
485 +#define EXT3_MB_GRP_NEED_INIT(grp) \
486 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
488 +struct ext3_free_extent {
494 +struct ext3_allocation_context {
495 + struct super_block *ac_sb;
498 + struct ext3_free_extent ac_g_ex;
500 + /* the best found extent */
501 + struct ext3_free_extent ac_b_ex;
503 + /* number of iterations done. we have to track to limit searching */
504 + unsigned long ac_ex_scanned;
505 + __u16 ac_groups_scanned;
510 + __u8 ac_flags; /* allocation hints */
513 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
514 + * N > 0, the field stores N, otherwise 0 */
516 + struct page *ac_buddy_page;
517 + struct page *ac_bitmap_page;
520 +#define AC_STATUS_CONTINUE 1
521 +#define AC_STATUS_FOUND 2
522 +#define AC_STATUS_BREAK 3
524 +struct ext3_mb_history {
525 + struct ext3_free_extent goal; /* goal allocation */
526 + struct ext3_free_extent result; /* result allocation */
529 + __u16 found; /* how many extents have been found */
530 + __u16 groups; /* how many groups have been scanned */
531 + __u16 tail; /* what tail broke some buddy */
532 + __u16 buddy; /* buddy the tail ^^^ broke */
533 + __u8 cr; /* which phase the result extent was found at */
538 + struct page *bd_buddy_page;
540 + struct page *bd_bitmap_page;
542 + struct ext3_group_info *bd_info;
543 + struct super_block *bd_sb;
547 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
548 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
550 +#ifndef EXT3_MB_HISTORY
551 +#define ext3_mb_store_history(sb,ino,ac)
553 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
554 + struct ext3_allocation_context *ac);
557 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
559 +static struct proc_dir_entry *proc_root_ext3;
561 +struct buffer_head *read_block_bitmap(struct super_block *, unsigned int);
562 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
563 +void ext3_mb_free_committed_blocks(struct super_block *);
565 +#if BITS_PER_LONG == 64
566 +#define mb_correct_addr_and_bit(bit,addr) \
568 + bit += ((unsigned long) addr & 7UL) << 3; \
569 + addr = (void *) ((unsigned long) addr & ~7UL); \
571 +#elif BITS_PER_LONG == 32
572 +#define mb_correct_addr_and_bit(bit,addr) \
574 + bit += ((unsigned long) addr & 3UL) << 3; \
575 + addr = (void *) ((unsigned long) addr & ~3UL); \
578 +#error "how many bits you are?!"
581 +static inline int mb_test_bit(int bit, void *addr)
583 + mb_correct_addr_and_bit(bit,addr);
584 + return ext2_test_bit(bit, addr);
587 +static inline void mb_set_bit(int bit, void *addr)
589 + mb_correct_addr_and_bit(bit,addr);
590 + ext2_set_bit(bit, addr);
593 +static inline void mb_set_bit_atomic(int bit, void *addr)
595 + mb_correct_addr_and_bit(bit,addr);
596 + ext2_set_bit_atomic(NULL, bit, addr);
599 +static inline void mb_clear_bit(int bit, void *addr)
601 + mb_correct_addr_and_bit(bit,addr);
602 + ext2_clear_bit(bit, addr);
605 +static inline void mb_clear_bit_atomic(int bit, void *addr)
607 + mb_correct_addr_and_bit(bit,addr);
608 + ext2_clear_bit_atomic(NULL, bit, addr);
611 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
614 +#if BITS_PER_LONG == 64
615 + fix = ((unsigned long) addr & 7UL) << 3;
616 + addr = (void *) ((unsigned long) addr & ~7UL);
617 +#elif BITS_PER_LONG == 32
618 + fix = ((unsigned long) addr & 3UL) << 3;
619 + addr = (void *) ((unsigned long) addr & ~3UL);
621 +#error "how many bits you are?!"
625 + return ext2_find_next_zero_bit(addr, max, start) - fix;
628 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
632 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
633 + J_ASSERT(max != NULL);
635 + if (order > e3b->bd_blkbits + 1) {
640 + /* at order 0 we see each particular block */
641 + *max = 1 << (e3b->bd_blkbits + 3);
643 + return EXT3_MB_BITMAP(e3b);
645 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
646 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
651 +#ifdef AGGRESSIVE_CHECK
653 +static void mb_check_buddy(struct ext3_buddy *e3b)
655 + int order = e3b->bd_blkbits + 1;
656 + int max, max2, i, j, k, count;
657 + int fragments = 0, fstart;
658 + void *buddy, *buddy2;
660 + if (!test_opt(e3b->bd_sb, MBALLOC))
664 + static int mb_check_counter = 0;
665 + if (mb_check_counter++ % 300 != 0)
669 + while (order > 1) {
670 + buddy = mb_find_buddy(e3b, order, &max);
672 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
674 + J_ASSERT(buddy != buddy2);
675 + J_ASSERT(max * 2 == max2);
678 + for (i = 0; i < max; i++) {
680 + if (mb_test_bit(i, buddy)) {
681 + /* only single bit in buddy2 may be 1 */
682 + if (!mb_test_bit(i << 1, buddy2))
683 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
684 + else if (!mb_test_bit((i << 1) + 1, buddy2))
685 + J_ASSERT(mb_test_bit(i << 1, buddy2));
689 + /* both bits in buddy2 must be 0 */
690 + J_ASSERT(mb_test_bit(i << 1, buddy2));
691 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
693 + for (j = 0; j < (1 << order); j++) {
694 + k = (i * (1 << order)) + j;
695 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
699 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
704 + buddy = mb_find_buddy(e3b, 0, &max);
705 + for (i = 0; i < max; i++) {
706 + if (!mb_test_bit(i, buddy)) {
707 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
708 + if (fstart == -1) {
715 + /* check used bits only */
716 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
717 + buddy2 = mb_find_buddy(e3b, j, &max2);
719 + J_ASSERT(k < max2);
720 + J_ASSERT(mb_test_bit(k, buddy2));
723 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
724 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
728 +#define mb_check_buddy(e3b)
731 +/* find most significant bit */
732 +static int inline fmsb(unsigned short word)
746 + } while (word != 0);
752 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
753 + int len, struct ext3_group_info *grp)
755 + struct ext3_sb_info *sbi = EXT3_SB(sb);
756 + unsigned short min, max, chunk, border;
758 + mb_debug("mark %u/%u free\n", first, len);
759 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
761 + border = 2 << sb->s_blocksize_bits;
764 + /* find how many blocks can be covered since this position */
765 + max = ffs(first | border) - 1;
767 + /* find how many blocks of power 2 we need to mark */
770 + mb_debug(" %u/%u -> max %u, min %u\n",
771 + first & ((2 << sb->s_blocksize_bits) - 1),
778 + /* mark multiblock chunks only */
779 + grp->bb_counters[min]++;
781 + mb_debug(" set %u at %u \n", first >> min,
782 + sbi->s_mb_offsets[min]);
783 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
792 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
795 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
796 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
797 + unsigned short i = 0, first, len;
798 + unsigned free = 0, fragments = 0;
799 + unsigned long long period = get_cycles();
801 + i = mb_find_next_zero_bit(bitmap, max, 0);
802 + grp->bb_first_free = i;
806 + i = ext2_find_next_le_bit(bitmap, max, i);
810 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
812 + grp->bb_counters[0]++;
814 + i = mb_find_next_zero_bit(bitmap, max, i);
816 + grp->bb_fragments = fragments;
818 + /* bb_state shouldn't being modified because all
819 + * others waits for init completion on page lock */
820 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
821 + if (free != grp->bb_free) {
822 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
823 + group, free, grp->bb_free);
824 + grp->bb_free = free;
827 + period = get_cycles() - period;
828 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
829 + EXT3_SB(sb)->s_mb_buddies_generated++;
830 + EXT3_SB(sb)->s_mb_generation_time += period;
831 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
834 +static int ext3_mb_init_cache(struct page *page)
836 + int blocksize, blocks_per_page, groups_per_page;
837 + int err = 0, i, first_group, first_block;
838 + struct super_block *sb;
839 + struct buffer_head *bhs;
840 + struct buffer_head **bh;
841 + struct inode *inode;
842 + char *data, *bitmap;
844 + mb_debug("init page %lu\n", page->index);
846 + inode = page->mapping->host;
848 + blocksize = 1 << inode->i_blkbits;
849 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
851 + groups_per_page = blocks_per_page >> 1;
852 + if (groups_per_page == 0)
853 + groups_per_page = 1;
855 + /* allocate buffer_heads to read bitmaps */
856 + if (groups_per_page > 1) {
858 + i = sizeof(struct buffer_head *) * groups_per_page;
859 + bh = kmalloc(i, GFP_NOFS);
866 + first_group = page->index * blocks_per_page / 2;
868 + /* read all groups the page covers into the cache */
869 + for (i = 0; i < groups_per_page; i++) {
870 + struct ext3_group_desc * desc;
872 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
876 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
881 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
885 + if (buffer_uptodate(bh[i]))
888 + lock_buffer(bh[i]);
889 + if (buffer_uptodate(bh[i])) {
890 + unlock_buffer(bh[i]);
895 + bh[i]->b_end_io = end_buffer_read_sync;
896 + submit_bh(READ, bh[i]);
897 + mb_debug("read bitmap for group %u\n", first_group + i);
900 + /* wait for I/O completion */
901 + for (i = 0; i < groups_per_page && bh[i]; i++)
902 + wait_on_buffer(bh[i]);
905 + for (i = 0; i < groups_per_page && bh[i]; i++)
906 + if (!buffer_uptodate(bh[i]))
909 + first_block = page->index * blocks_per_page;
910 + for (i = 0; i < blocks_per_page; i++) {
913 + group = (first_block + i) >> 1;
914 + if (group >= EXT3_SB(sb)->s_groups_count)
917 + data = page_address(page) + (i * blocksize);
918 + bitmap = bh[group - first_group]->b_data;
920 + if ((first_block + i) & 1) {
921 + /* this is block of buddy */
922 + mb_debug("put buddy for group %u in page %lu/%x\n",
923 + group, page->index, i * blocksize);
924 + memset(data, 0xff, blocksize);
925 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
926 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
927 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
928 + ext3_mb_generate_buddy(sb, data, bitmap, group);
930 + /* this is block of bitmap */
931 + mb_debug("put bitmap for group %u in page %lu/%x\n",
932 + group, page->index, i * blocksize);
933 + memcpy(data, bitmap, blocksize);
936 + SetPageUptodate(page);
940 + for (i = 0; i < groups_per_page && bh[i]; i++)
948 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
949 + struct ext3_buddy *e3b)
951 + struct ext3_sb_info *sbi = EXT3_SB(sb);
952 + struct inode *inode = sbi->s_buddy_cache;
953 + int blocks_per_page, block, pnum, poff;
956 + mb_debug("load group %u\n", group);
958 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
960 + e3b->bd_blkbits = sb->s_blocksize_bits;
961 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
963 + e3b->bd_group = group;
964 + e3b->bd_buddy_page = NULL;
965 + e3b->bd_bitmap_page = NULL;
968 + pnum = block / blocks_per_page;
969 + poff = block % blocks_per_page;
971 + /* we could use find_or_create_page(), but it locks page
972 + * what we'd like to avoid in fast path ... */
973 + page = find_get_page(inode->i_mapping, pnum);
974 + if (page == NULL || !PageUptodate(page)) {
976 + page_cache_release(page);
977 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
979 + BUG_ON(page->mapping != inode->i_mapping);
980 + if (!PageUptodate(page))
981 + ext3_mb_init_cache(page);
985 + if (page == NULL || !PageUptodate(page))
987 + e3b->bd_bitmap_page = page;
988 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
989 + mark_page_accessed(page);
992 + pnum = block / blocks_per_page;
993 + poff = block % blocks_per_page;
995 + page = find_get_page(inode->i_mapping, pnum);
996 + if (page == NULL || !PageUptodate(page)) {
998 + page_cache_release(page);
999 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1001 + BUG_ON(page->mapping != inode->i_mapping);
1002 + if (!PageUptodate(page))
1003 + ext3_mb_init_cache(page);
1004 + unlock_page(page);
1007 + if (page == NULL || !PageUptodate(page))
1009 + e3b->bd_buddy_page = page;
1010 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1011 + mark_page_accessed(page);
1013 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1014 + J_ASSERT(e3b->bd_buddy_page != NULL);
1019 + if (e3b->bd_bitmap_page)
1020 + page_cache_release(e3b->bd_bitmap_page);
1021 + if (e3b->bd_buddy_page)
1022 + page_cache_release(e3b->bd_buddy_page);
1023 + e3b->bd_buddy = NULL;
1024 + e3b->bd_bitmap = NULL;
1028 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1030 + if (e3b->bd_bitmap_page)
1031 + page_cache_release(e3b->bd_bitmap_page);
1032 + if (e3b->bd_buddy_page)
1033 + page_cache_release(e3b->bd_buddy_page);
1038 +ext3_lock_group(struct super_block *sb, int group)
1040 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1041 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1045 +ext3_unlock_group(struct super_block *sb, int group)
1047 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1048 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1051 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1056 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1057 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1059 + bb = EXT3_MB_BUDDY(e3b);
1060 + while (order <= e3b->bd_blkbits + 1) {
1061 + block = block >> 1;
1062 + if (!mb_test_bit(block, bb)) {
1063 + /* this block is part of buddy of order 'order' */
1066 + bb += 1 << (e3b->bd_blkbits - order);
1072 +static inline void mb_clear_bits(void *bm, int cur, int len)
1077 + while (cur < len) {
1078 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1079 + /* fast path: clear whole word at once */
1080 + addr = bm + (cur >> 3);
1085 + mb_clear_bit_atomic(cur, bm);
1090 +static inline void mb_set_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);
1099 + *addr = 0xffffffff;
1103 + mb_set_bit_atomic(cur, bm);
1108 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1110 + int block = 0, max = 0, order;
1111 + void *buddy, *buddy2;
1113 + mb_check_buddy(e3b);
1115 + e3b->bd_info->bb_free += count;
1116 + if (first < e3b->bd_info->bb_first_free)
1117 + e3b->bd_info->bb_first_free = first;
1119 + /* let's maintain fragments counter */
1121 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1122 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1123 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1125 + e3b->bd_info->bb_fragments--;
1126 + else if (!block && !max)
1127 + e3b->bd_info->bb_fragments++;
1129 + /* let's maintain buddy itself */
1130 + while (count-- > 0) {
1134 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1135 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1136 + e3b->bd_info->bb_counters[order]++;
1138 + /* start of the buddy */
1139 + buddy = mb_find_buddy(e3b, order, &max);
1143 + if (mb_test_bit(block, buddy) ||
1144 + mb_test_bit(block + 1, buddy))
1147 + /* both the buddies are free, try to coalesce them */
1148 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1154 + /* for special purposes, we don't set
1155 + * free bits in bitmap */
1156 + mb_set_bit(block, buddy);
1157 + mb_set_bit(block + 1, buddy);
1159 + e3b->bd_info->bb_counters[order]--;
1160 + e3b->bd_info->bb_counters[order]--;
1162 + block = block >> 1;
1164 + e3b->bd_info->bb_counters[order]++;
1166 + mb_clear_bit(block, buddy2);
1170 + mb_check_buddy(e3b);
1175 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1176 + int needed, struct ext3_free_extent *ex)
1178 + int next = block, max, ord;
1181 + J_ASSERT(ex != NULL);
1183 + buddy = mb_find_buddy(e3b, order, &max);
1185 + J_ASSERT(block < max);
1186 + if (mb_test_bit(block, buddy)) {
1193 + if (likely(order == 0)) {
1194 + /* find actual order */
1195 + order = mb_find_order_for_block(e3b, block);
1196 + block = block >> order;
1199 + ex->fe_len = 1 << order;
1200 + ex->fe_start = block << order;
1201 + ex->fe_group = e3b->bd_group;
1203 + /* calc difference from given start */
1204 + next = next - ex->fe_start;
1205 + ex->fe_len -= next;
1206 + ex->fe_start += next;
1208 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1210 + if (block + 1 >= max)
1213 + next = (block + 1) * (1 << order);
1214 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1217 + ord = mb_find_order_for_block(e3b, next);
1220 + block = next >> order;
1221 + ex->fe_len += 1 << order;
1224 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1225 + return ex->fe_len;
1228 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1230 + int ord, mlen = 0, max = 0, cur;
1231 + int start = ex->fe_start;
1232 + int len = ex->fe_len;
1237 + mb_check_buddy(e3b);
1239 + e3b->bd_info->bb_free -= len;
1240 + if (e3b->bd_info->bb_first_free == start)
1241 + e3b->bd_info->bb_first_free += len;
1243 + /* let's maintain fragments counter */
1245 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1246 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1247 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1249 + e3b->bd_info->bb_fragments++;
1250 + else if (!mlen && !max)
1251 + e3b->bd_info->bb_fragments--;
1253 + /* let's maintain buddy itself */
1255 + ord = mb_find_order_for_block(e3b, start);
1257 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1258 + /* the whole chunk may be allocated at once! */
1260 + buddy = mb_find_buddy(e3b, ord, &max);
1261 + J_ASSERT((start >> ord) < max);
1262 + mb_set_bit(start >> ord, buddy);
1263 + e3b->bd_info->bb_counters[ord]--;
1266 + J_ASSERT(len >= 0);
1270 + /* store for history */
1272 + ret = len | (ord << 16);
1274 + /* we have to split large buddy */
1275 + J_ASSERT(ord > 0);
1276 + buddy = mb_find_buddy(e3b, ord, &max);
1277 + mb_set_bit(start >> ord, buddy);
1278 + e3b->bd_info->bb_counters[ord]--;
1281 + cur = (start >> ord) & ~1U;
1282 + buddy = mb_find_buddy(e3b, ord, &max);
1283 + mb_clear_bit(cur, buddy);
1284 + mb_clear_bit(cur + 1, buddy);
1285 + e3b->bd_info->bb_counters[ord]++;
1286 + e3b->bd_info->bb_counters[ord]++;
1289 + /* now drop all the bits in bitmap */
1290 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1292 + mb_check_buddy(e3b);
1298 + * Must be called under group lock!
1300 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1301 + struct ext3_buddy *e3b)
1303 + unsigned long ret;
1305 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1306 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1308 + ac->ac_status = AC_STATUS_FOUND;
1309 + ac->ac_tail = ret & 0xffff;
1310 + ac->ac_buddy = ret >> 16;
1312 + /* hold in-core structures until allocated
1313 + * blocks are marked non-free in on-disk bitmap */
1314 + ac->ac_buddy_page = e3b->bd_buddy_page;
1315 + page_cache_get(e3b->bd_buddy_page);
1316 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1317 + page_cache_get(e3b->bd_bitmap_page);
1321 + * The routine checks whether found extent is good enough. If it is,
1322 + * then the extent gets marked used and flag is set to the context
1323 + * to stop scanning. Otherwise, the extent is compared with the
1324 + * previous found extent and if new one is better, then it's stored
1325 + * in the context. Later, the best found extent will be used, if
1326 + * mballoc can't find good enough extent.
1328 + * FIXME: real allocation policy is to be designed yet!
1330 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1331 + struct ext3_free_extent *ex,
1332 + struct ext3_buddy *e3b)
1334 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1335 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1337 + J_ASSERT(ex->fe_len > 0);
1338 + J_ASSERT(ex->fe_len < EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1339 + J_ASSERT(ex->fe_start < EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1344 + * The special case - take what you catch first
1346 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1348 + ext3_mb_use_best_found(ac, e3b);
1353 + * Let's check whether the chunk is good enough
1355 + if (ex->fe_len == gex->fe_len) {
1357 + ext3_mb_use_best_found(ac, e3b);
1362 + * If this is first found extent, just store it in the context
1364 + if (bex->fe_len == 0) {
1370 + * If new found extent is better, store it in the context
1372 + if (bex->fe_len < gex->fe_len) {
1373 + /* if the request isn't satisfied, any found extent
1374 + * larger than previous best one is better */
1375 + if (ex->fe_len > bex->fe_len)
1377 + } else if (ex->fe_len > gex->fe_len) {
1378 + /* if the request is satisfied, then we try to find
1379 + * an extent that still satisfy the request, but is
1380 + * smaller than previous one */
1385 + * Let's scan at least few extents and don't pick up a first one
1387 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1388 + ac->ac_status = AC_STATUS_BREAK;
1391 + * We don't want to scan for a whole year
1393 + if (ac->ac_found > ext3_mb_max_to_scan)
1394 + ac->ac_status = AC_STATUS_BREAK;
1397 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1398 + struct ext3_buddy *e3b)
1400 + struct ext3_free_extent ex = ac->ac_b_ex;
1401 + int group = ex.fe_group, max, err;
1403 + J_ASSERT(ex.fe_len > 0);
1404 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1408 + ext3_lock_group(ac->ac_sb, group);
1409 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1413 + ext3_mb_use_best_found(ac, e3b);
1416 + ext3_unlock_group(ac->ac_sb, group);
1418 + ext3_mb_release_desc(e3b);
1423 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1424 + struct ext3_buddy *e3b)
1426 + int group = ac->ac_g_ex.fe_group, max, err;
1427 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1428 + struct ext3_super_block *es = sbi->s_es;
1429 + struct ext3_free_extent ex;
1431 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1435 + ext3_lock_group(ac->ac_sb, group);
1436 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1437 + ac->ac_g_ex.fe_len, &ex);
1439 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1440 + unsigned long start;
1441 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1442 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1443 + if (start % sbi->s_stripe == 0) {
1446 + ext3_mb_use_best_found(ac, e3b);
1448 + } else if (max >= ac->ac_g_ex.fe_len) {
1449 + J_ASSERT(ex.fe_len > 0);
1450 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1451 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1454 + ext3_mb_use_best_found(ac, e3b);
1455 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1456 + /* Sometimes, caller may want to merge even small
1457 + * number of blocks to an existing extent */
1458 + J_ASSERT(ex.fe_len > 0);
1459 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1460 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1463 + ext3_mb_use_best_found(ac, e3b);
1465 + ext3_unlock_group(ac->ac_sb, group);
1467 + ext3_mb_release_desc(e3b);
1473 + * The routine scans buddy structures (not bitmap!) from given order
1474 + * to max order and tries to find big enough chunk to satisfy the req
1476 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1477 + struct ext3_buddy *e3b)
1479 + struct super_block *sb = ac->ac_sb;
1480 + struct ext3_group_info *grp = e3b->bd_info;
1484 + J_ASSERT(ac->ac_2order > 0);
1485 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1486 + if (grp->bb_counters[i] == 0)
1489 + buddy = mb_find_buddy(e3b, i, &max);
1490 + if (buddy == NULL) {
1491 + printk(KERN_ALERT "looking for wrong order?\n");
1495 + k = mb_find_next_zero_bit(buddy, max, 0);
1496 + J_ASSERT(k < max);
1500 + ac->ac_b_ex.fe_len = 1 << i;
1501 + ac->ac_b_ex.fe_start = k << i;
1502 + ac->ac_b_ex.fe_group = e3b->bd_group;
1504 + ext3_mb_use_best_found(ac, e3b);
1505 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1507 + if (unlikely(ext3_mb_stats))
1508 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1515 + * The routine scans the group and measures all found extents.
1516 + * In order to optimize scanning, caller must pass number of
1517 + * free blocks in the group, so the routine can know upper limit.
1519 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1520 + struct ext3_buddy *e3b)
1522 + struct super_block *sb = ac->ac_sb;
1523 + void *bitmap = EXT3_MB_BITMAP(e3b);
1524 + struct ext3_free_extent ex;
1527 + free = e3b->bd_info->bb_free;
1528 + J_ASSERT(free > 0);
1530 + i = e3b->bd_info->bb_first_free;
1532 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1533 + i = mb_find_next_zero_bit(bitmap, EXT3_BLOCKS_PER_GROUP(sb), i);
1534 + if (i >= EXT3_BLOCKS_PER_GROUP(sb)) {
1535 + J_ASSERT(free == 0);
1539 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1540 + J_ASSERT(ex.fe_len > 0);
1541 + J_ASSERT(free >= ex.fe_len);
1543 + ext3_mb_measure_extent(ac, &ex, e3b);
1546 + free -= ex.fe_len;
1551 + * This is a special case for storages like raid5
1552 + * we try to find stripe-aligned chunks for stripe-size requests
1554 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1555 + struct ext3_buddy *e3b)
1557 + struct super_block *sb = ac->ac_sb;
1558 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1559 + void *bitmap = EXT3_MB_BITMAP(e3b);
1560 + struct ext3_free_extent ex;
1561 + unsigned long i, max;
1563 + J_ASSERT(sbi->s_stripe != 0);
1565 + /* find first stripe-aligned block */
1566 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb)
1567 + + le32_to_cpu(sbi->s_es->s_first_data_block);
1568 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1569 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block))
1570 + % EXT3_BLOCKS_PER_GROUP(sb);
1572 + while (i < EXT3_BLOCKS_PER_GROUP(sb)) {
1573 + if (!mb_test_bit(i, bitmap)) {
1574 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1575 + if (max >= sbi->s_stripe) {
1578 + ext3_mb_use_best_found(ac, e3b);
1582 + i += sbi->s_stripe;
1586 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1587 + int group, int cr)
1589 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1590 + unsigned free, fragments, i, bits;
1592 + J_ASSERT(cr >= 0 && cr < 4);
1593 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1595 + free = grp->bb_free;
1596 + fragments = grp->bb_fragments;
1599 + if (fragments == 0)
1604 + J_ASSERT(ac->ac_2order != 0);
1605 + bits = ac->ac_sb->s_blocksize_bits + 1;
1606 + for (i = ac->ac_2order; i <= bits; i++)
1607 + if (grp->bb_counters[i] > 0)
1611 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1615 + if (free >= ac->ac_g_ex.fe_len)
1627 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1628 + unsigned long goal, int *len, int flags, int *errp)
1630 + struct buffer_head *bitmap_bh = NULL;
1631 + struct ext3_allocation_context ac;
1632 + int i, group, block, cr, err = 0;
1633 + struct ext3_group_desc *gdp;
1634 + struct ext3_super_block *es;
1635 + struct buffer_head *gdp_bh;
1636 + struct ext3_sb_info *sbi;
1637 + struct super_block *sb;
1638 + struct ext3_buddy e3b;
1640 + J_ASSERT(len != NULL);
1641 + J_ASSERT(*len > 0);
1645 + printk("ext3_mb_new_nblocks: nonexistent device");
1649 + if (!test_opt(sb, MBALLOC)) {
1650 + static int ext3_mballoc_warning = 0;
1651 + if (ext3_mballoc_warning == 0) {
1652 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1653 + "mballoc disabled!\n");
1654 + ext3_mballoc_warning++;
1657 + err = ext3_new_block_old(handle, inode, goal, errp);
1661 + ext3_mb_poll_new_transaction(sb, handle);
1663 + sbi = EXT3_SB(sb);
1664 + es = EXT3_SB(sb)->s_es;
1667 + * We can't allocate > group size
1669 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1670 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1672 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1673 + /* someone asks for non-reserved blocks */
1675 + err = ext3_mb_reserve_blocks(sb, 1);
1682 + ac.ac_buddy_page = NULL;
1683 + ac.ac_bitmap_page = NULL;
1686 + * Check quota for allocation of this blocks.
1688 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1696 + /* start searching from the goal */
1697 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1698 + goal >= le32_to_cpu(es->s_blocks_count))
1699 + goal = le32_to_cpu(es->s_first_data_block);
1700 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1701 + EXT3_BLOCKS_PER_GROUP(sb);
1702 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1703 + EXT3_BLOCKS_PER_GROUP(sb));
1705 + /* set up allocation goals */
1706 + ac.ac_b_ex.fe_group = 0;
1707 + ac.ac_b_ex.fe_start = 0;
1708 + ac.ac_b_ex.fe_len = 0;
1709 + ac.ac_status = AC_STATUS_CONTINUE;
1710 + ac.ac_groups_scanned = 0;
1711 + ac.ac_ex_scanned = 0;
1713 + ac.ac_sb = inode->i_sb;
1714 + ac.ac_g_ex.fe_group = group;
1715 + ac.ac_g_ex.fe_start = block;
1716 + ac.ac_g_ex.fe_len = *len;
1717 + ac.ac_flags = flags;
1719 + ac.ac_criteria = 0;
1721 + if (*len == 1 && sbi->s_stripe) {
1722 + /* looks like a metadata, let's use a dirty hack for raid5
1723 + * move all metadata in first groups in hope to hit cached
1724 + * sectors and thus avoid read-modify cycles in raid5 */
1725 + ac.ac_g_ex.fe_group = group = 0;
1728 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1730 + if (i >= ext3_mb_order2_reqs) {
1732 + if ((*len & (~(1 << i))) == 0)
1736 + /* first, try the goal */
1737 + err = ext3_mb_find_by_goal(&ac, &e3b);
1740 + if (ac.ac_status == AC_STATUS_FOUND)
1743 + /* Let's just scan groups to find more-less suitable blocks */
1744 + cr = ac.ac_2order ? 0 : 1;
1746 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1747 + ac.ac_criteria = cr;
1748 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1749 + if (group == EXT3_SB(sb)->s_groups_count)
1752 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1753 + /* we need full data about the group
1754 + * to make a good selection */
1755 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1758 + ext3_mb_release_desc(&e3b);
1761 + /* check is group good for our criteries */
1762 + if (!ext3_mb_good_group(&ac, group, cr))
1765 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1769 + ext3_lock_group(sb, group);
1770 + if (!ext3_mb_good_group(&ac, group, cr)) {
1771 + /* someone did allocation from this group */
1772 + ext3_unlock_group(sb, group);
1773 + ext3_mb_release_desc(&e3b);
1777 + ac.ac_groups_scanned++;
1779 + ext3_mb_simple_scan_group(&ac, &e3b);
1780 + else if (cr == 1 && *len == sbi->s_stripe)
1781 + ext3_mb_scan_aligned(&ac, &e3b);
1783 + ext3_mb_complex_scan_group(&ac, &e3b);
1785 + ext3_unlock_group(sb, group);
1787 + ext3_mb_release_desc(&e3b);
1789 + if (ac.ac_status != AC_STATUS_CONTINUE)
1794 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1795 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1797 + * We've been searching too long. Let's try to allocate
1798 + * the best chunk we've found so far
1801 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1802 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1803 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1804 + ac.ac_g_ex.fe_len);*/
1805 + ext3_mb_try_best_found(&ac, &e3b);
1806 + if (ac.ac_status != AC_STATUS_FOUND) {
1808 + * Someone more lucky has already allocated it.
1809 + * The only thing we can do is just take first
1811 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1813 + ac.ac_b_ex.fe_group = 0;
1814 + ac.ac_b_ex.fe_start = 0;
1815 + ac.ac_b_ex.fe_len = 0;
1816 + ac.ac_status = AC_STATUS_CONTINUE;
1817 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1823 + if (ac.ac_status != AC_STATUS_FOUND) {
1825 + * We aren't lucky definitely
1827 + DQUOT_FREE_BLOCK(inode, *len);
1831 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1832 + ac.ac_status, ac.ac_flags);
1833 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1834 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1835 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1836 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1837 + sbi->s_blocks_reserved, ac.ac_found);
1838 + printk("EXT3-fs: groups: ");
1839 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1840 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1847 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1849 + /* good news - free block(s) have been found. now it's time
1850 + * to mark block(s) in good old journaled bitmap */
1851 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1852 + + ac.ac_b_ex.fe_start
1853 + + le32_to_cpu(es->s_first_data_block);
1855 + /* we made a desicion, now mark found blocks in good old
1856 + * bitmap to be journaled */
1858 + ext3_debug("using block group %d(%d)\n",
1859 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1861 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1867 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1873 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1879 + err = ext3_journal_get_write_access(handle, gdp_bh);
1883 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1884 + + ac.ac_b_ex.fe_start
1885 + + le32_to_cpu(es->s_first_data_block);
1887 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1888 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1889 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1890 + EXT3_SB(sb)->s_itb_per_group))
1891 + ext3_error(sb, "ext3_new_block",
1892 + "Allocating block in system zone - "
1893 + "block = %u", block);
1894 +#ifdef AGGRESSIVE_CHECK
1895 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1896 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1898 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1900 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1901 + gdp->bg_free_blocks_count =
1902 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1903 + - ac.ac_b_ex.fe_len);
1904 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1905 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1907 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1910 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1916 + brelse(bitmap_bh);
1918 + /* drop non-allocated, but dquote'd blocks */
1919 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1920 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1922 + *len = ac.ac_b_ex.fe_len;
1923 + J_ASSERT(*len > 0);
1924 + J_ASSERT(block != 0);
1928 + /* if we've already allocated something, roll it back */
1929 + if (ac.ac_status == AC_STATUS_FOUND) {
1930 + /* FIXME: free blocks here */
1933 + DQUOT_FREE_BLOCK(inode, *len);
1934 + brelse(bitmap_bh);
1938 + if (ac.ac_buddy_page)
1939 + page_cache_release(ac.ac_buddy_page);
1940 + if (ac.ac_bitmap_page)
1941 + page_cache_release(ac.ac_bitmap_page);
1943 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1944 + /* block wasn't reserved before and we reserved it
1945 + * at the beginning of allocation. it doesn't matter
1946 + * whether we allocated anything or we failed: time
1947 + * to release reservation. NOTE: because I expect
1948 + * any multiblock request from delayed allocation
1949 + * path only, here is single block always */
1950 + ext3_mb_release_blocks(sb, 1);
1953 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1954 + atomic_inc(&sbi->s_bal_reqs);
1955 + atomic_add(*len, &sbi->s_bal_allocated);
1956 + if (*len >= ac.ac_g_ex.fe_len)
1957 + atomic_inc(&sbi->s_bal_success);
1958 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1959 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1960 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1961 + atomic_inc(&sbi->s_bal_goals);
1962 + if (ac.ac_found > ext3_mb_max_to_scan)
1963 + atomic_inc(&sbi->s_bal_breaks);
1966 + ext3_mb_store_history(sb, inode->i_ino, &ac);
1970 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1972 +#ifdef EXT3_MB_HISTORY
1973 +struct ext3_mb_proc_session {
1974 + struct ext3_mb_history *history;
1975 + struct super_block *sb;
1980 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1981 + struct ext3_mb_history *hs,
1984 + if (hs == s->history + s->max)
1986 + if (!first && hs == s->history + s->start)
1988 + while (hs->goal.fe_len == 0) {
1990 + if (hs == s->history + s->max)
1992 + if (hs == s->history + s->start)
1998 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2000 + struct ext3_mb_proc_session *s = seq->private;
2001 + struct ext3_mb_history *hs;
2005 + return SEQ_START_TOKEN;
2006 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2009 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2013 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2015 + struct ext3_mb_proc_session *s = seq->private;
2016 + struct ext3_mb_history *hs = v;
2019 + if (v == SEQ_START_TOKEN)
2020 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2022 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2025 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2027 + struct ext3_mb_history *hs = v;
2028 + char buf[20], buf2[20];
2030 + if (v == SEQ_START_TOKEN) {
2031 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2032 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2033 + "merge", "tail", "broken");
2037 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2038 + hs->goal.fe_start, hs->goal.fe_len);
2039 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2040 + hs->result.fe_start, hs->result.fe_len);
2041 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2042 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2043 + hs->cr, hs->merged ? "M" : "", hs->tail,
2044 + hs->buddy ? 1 << hs->buddy : 0);
2048 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2052 +static struct seq_operations ext3_mb_seq_history_ops = {
2053 + .start = ext3_mb_seq_history_start,
2054 + .next = ext3_mb_seq_history_next,
2055 + .stop = ext3_mb_seq_history_stop,
2056 + .show = ext3_mb_seq_history_show,
2059 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2061 + struct super_block *sb = PDE(inode)->data;
2062 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2063 + struct ext3_mb_proc_session *s;
2066 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2069 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2070 + s->history = kmalloc(size, GFP_KERNEL);
2071 + if (s->history == NULL) {
2076 + spin_lock(&sbi->s_mb_history_lock);
2077 + memcpy(s->history, sbi->s_mb_history, size);
2078 + s->max = sbi->s_mb_history_max;
2079 + s->start = sbi->s_mb_history_cur % s->max;
2080 + spin_unlock(&sbi->s_mb_history_lock);
2082 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2084 + struct seq_file *m = (struct seq_file *)file->private_data;
2087 + kfree(s->history);
2094 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2096 + struct seq_file *seq = (struct seq_file *)file->private_data;
2097 + struct ext3_mb_proc_session *s = seq->private;
2098 + kfree(s->history);
2100 + return seq_release(inode, file);
2103 +static struct file_operations ext3_mb_seq_history_fops = {
2104 + .owner = THIS_MODULE,
2105 + .open = ext3_mb_seq_history_open,
2107 + .llseek = seq_lseek,
2108 + .release = ext3_mb_seq_history_release,
2111 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2113 + struct super_block *sb = seq->private;
2114 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2117 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2121 + return (void *) group;
2124 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2126 + struct super_block *sb = seq->private;
2127 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2131 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2134 + return (void *) group;;
2137 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2139 + struct super_block *sb = seq->private;
2140 + long group = (long) v, i;
2142 + struct ext3_group_info info;
2143 + unsigned short counters[16];
2148 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2149 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2150 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2151 + "2^11", "2^12", "2^13");
2153 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2154 + sizeof(struct ext3_group_info);
2155 + ext3_lock_group(sb, group);
2156 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2157 + ext3_unlock_group(sb, group);
2159 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2162 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2163 + sg.info.bb_fragments, sg.info.bb_first_free);
2164 + for (i = 0; i <= 13; i++)
2165 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2166 + sg.info.bb_counters[i] : 0);
2167 + seq_printf(seq, " ]\n");
2172 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2176 +static struct seq_operations ext3_mb_seq_groups_ops = {
2177 + .start = ext3_mb_seq_groups_start,
2178 + .next = ext3_mb_seq_groups_next,
2179 + .stop = ext3_mb_seq_groups_stop,
2180 + .show = ext3_mb_seq_groups_show,
2183 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2185 + struct super_block *sb = PDE(inode)->data;
2188 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2190 + struct seq_file *m = (struct seq_file *)file->private_data;
2197 +static struct file_operations ext3_mb_seq_groups_fops = {
2198 + .owner = THIS_MODULE,
2199 + .open = ext3_mb_seq_groups_open,
2201 + .llseek = seq_lseek,
2202 + .release = seq_release,
2205 +static void ext3_mb_history_release(struct super_block *sb)
2207 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2210 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2211 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2212 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2213 + remove_proc_entry(name, proc_root_ext3);
2215 + if (sbi->s_mb_history)
2216 + kfree(sbi->s_mb_history);
2219 +static void ext3_mb_history_init(struct super_block *sb)
2221 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2225 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2226 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2227 + if (sbi->s_mb_proc != NULL) {
2228 + struct proc_dir_entry *p;
2229 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2231 + p->proc_fops = &ext3_mb_seq_history_fops;
2234 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2236 + p->proc_fops = &ext3_mb_seq_groups_fops;
2241 + sbi->s_mb_history_max = 1000;
2242 + sbi->s_mb_history_cur = 0;
2243 + spin_lock_init(&sbi->s_mb_history_lock);
2244 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2245 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2246 + if (likely(sbi->s_mb_history != NULL))
2247 + memset(sbi->s_mb_history, 0, i);
2248 + /* if we can't allocate history, then we simple won't use it */
2252 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2253 + struct ext3_allocation_context *ac)
2255 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2256 + struct ext3_mb_history h;
2258 + if (unlikely(sbi->s_mb_history == NULL))
2261 + h.pid = current->pid;
2263 + h.goal = ac->ac_g_ex;
2264 + h.result = ac->ac_b_ex;
2265 + h.found = ac->ac_found;
2266 + h.cr = ac->ac_criteria;
2267 + h.groups = ac->ac_groups_scanned;
2268 + h.tail = ac->ac_tail;
2269 + h.buddy = ac->ac_buddy;
2271 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2272 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2275 + spin_lock(&sbi->s_mb_history_lock);
2276 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2277 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2278 + sbi->s_mb_history_cur = 0;
2279 + spin_unlock(&sbi->s_mb_history_lock);
2283 +#define ext3_mb_history_release(sb)
2284 +#define ext3_mb_history_init(sb)
2287 +int ext3_mb_init_backend(struct super_block *sb)
2289 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2290 + int i, j, len, metalen;
2291 + int num_meta_group_infos =
2292 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2293 + EXT3_DESC_PER_BLOCK_BITS(sb);
2294 + struct ext3_group_info **meta_group_info;
2296 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2297 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2298 + * So a two level scheme suffices for now. */
2299 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2300 + num_meta_group_infos, GFP_KERNEL);
2301 + if (sbi->s_group_info == NULL) {
2302 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2305 + sbi->s_buddy_cache = new_inode(sb);
2306 + if (sbi->s_buddy_cache == NULL) {
2307 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2311 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2312 + for (i = 0; i < num_meta_group_infos; i++) {
2313 + if ((i + 1) == num_meta_group_infos)
2314 + metalen = sizeof(*meta_group_info) *
2315 + (sbi->s_groups_count -
2316 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2317 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2318 + if (meta_group_info == NULL) {
2319 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2321 + goto err_freemeta;
2323 + sbi->s_group_info[i] = meta_group_info;
2327 + * calculate needed size. if change bb_counters size,
2328 + * don't forget about ext3_mb_generate_buddy()
2330 + len = sizeof(struct ext3_group_info);
2331 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2332 + for (i = 0; i < sbi->s_groups_count; i++) {
2333 + struct ext3_group_desc * desc;
2336 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2337 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2339 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2340 + if (meta_group_info[j] == NULL) {
2341 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2343 + goto err_freebuddy;
2345 + desc = ext3_get_group_desc(sb, i, NULL);
2346 + if (desc == NULL) {
2347 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2348 + goto err_freebuddy;
2350 + memset(meta_group_info[j], 0, len);
2351 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2352 + &meta_group_info[j]->bb_state);
2353 + meta_group_info[j]->bb_free =
2354 + le16_to_cpu(desc->bg_free_blocks_count);
2361 + kfree(EXT3_GROUP_INFO(sb, i));
2364 + i = num_meta_group_infos;
2367 + kfree(sbi->s_group_info[i]);
2368 + iput(sbi->s_buddy_cache);
2370 + kfree(sbi->s_group_info);
2374 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2376 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2377 + struct inode *root = sb->s_root->d_inode;
2378 + unsigned i, offset, max;
2379 + struct dentry *dentry;
2381 + if (!test_opt(sb, MBALLOC))
2384 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2386 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2387 + if (sbi->s_mb_offsets == NULL) {
2388 + clear_opt(sbi->s_mount_opt, MBALLOC);
2391 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2392 + if (sbi->s_mb_maxs == NULL) {
2393 + clear_opt(sbi->s_mount_opt, MBALLOC);
2394 + kfree(sbi->s_mb_maxs);
2398 + /* order 0 is regular bitmap */
2399 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2400 + sbi->s_mb_offsets[0] = 0;
2404 + max = sb->s_blocksize << 2;
2406 + sbi->s_mb_offsets[i] = offset;
2407 + sbi->s_mb_maxs[i] = max;
2408 + offset += 1 << (sb->s_blocksize_bits - i);
2411 + } while (i <= sb->s_blocksize_bits + 1);
2413 + /* init file for buddy data */
2414 + if ((i = ext3_mb_init_backend(sb))) {
2415 + clear_opt(sbi->s_mount_opt, MBALLOC);
2416 + kfree(sbi->s_mb_offsets);
2417 + kfree(sbi->s_mb_maxs);
2421 + spin_lock_init(&sbi->s_reserve_lock);
2422 + spin_lock_init(&sbi->s_md_lock);
2423 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2424 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2425 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2426 + spin_lock_init(&sbi->s_bal_lock);
2428 + /* remove old on-disk buddy file */
2429 + mutex_lock(&root->i_mutex);
2430 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2431 + if (dentry->d_inode != NULL) {
2432 + i = vfs_unlink(root, dentry);
2434 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2437 + mutex_unlock(&root->i_mutex);
2439 + ext3_mb_history_init(sb);
2441 + printk("EXT3-fs: mballoc enabled\n");
2445 +int ext3_mb_release(struct super_block *sb)
2447 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2448 + int i, num_meta_group_infos;
2450 + if (!test_opt(sb, MBALLOC))
2453 + /* release freed, non-committed blocks */
2454 + spin_lock(&sbi->s_md_lock);
2455 + list_splice_init(&sbi->s_closed_transaction,
2456 + &sbi->s_committed_transaction);
2457 + list_splice_init(&sbi->s_active_transaction,
2458 + &sbi->s_committed_transaction);
2459 + spin_unlock(&sbi->s_md_lock);
2460 + ext3_mb_free_committed_blocks(sb);
2462 + if (sbi->s_group_info) {
2463 + for (i = 0; i < sbi->s_groups_count; i++)
2464 + kfree(EXT3_GROUP_INFO(sb, i));
2465 + num_meta_group_infos = (sbi->s_groups_count +
2466 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2467 + EXT3_DESC_PER_BLOCK_BITS(sb);
2468 + for (i = 0; i < num_meta_group_infos; i++)
2469 + kfree(sbi->s_group_info[i]);
2470 + kfree(sbi->s_group_info);
2472 + if (sbi->s_mb_offsets)
2473 + kfree(sbi->s_mb_offsets);
2474 + if (sbi->s_mb_maxs)
2475 + kfree(sbi->s_mb_maxs);
2476 + if (sbi->s_buddy_cache)
2477 + iput(sbi->s_buddy_cache);
2478 + if (sbi->s_blocks_reserved)
2479 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2480 + sbi->s_blocks_reserved);
2481 + if (ext3_mb_stats) {
2482 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2483 + atomic_read(&sbi->s_bal_allocated),
2484 + atomic_read(&sbi->s_bal_reqs),
2485 + atomic_read(&sbi->s_bal_success));
2486 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2487 + "%u 2^N hits, %u breaks\n",
2488 + atomic_read(&sbi->s_bal_ex_scanned),
2489 + atomic_read(&sbi->s_bal_goals),
2490 + atomic_read(&sbi->s_bal_2orders),
2491 + atomic_read(&sbi->s_bal_breaks));
2492 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2493 + sbi->s_mb_buddies_generated++,
2494 + sbi->s_mb_generation_time);
2497 + ext3_mb_history_release(sb);
2502 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2504 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2505 + int err, i, count = 0, count2 = 0;
2506 + struct ext3_free_metadata *md;
2507 + struct ext3_buddy e3b;
2509 + if (list_empty(&sbi->s_committed_transaction))
2512 + /* there is committed blocks to be freed yet */
2514 + /* get next array of blocks */
2516 + spin_lock(&sbi->s_md_lock);
2517 + if (!list_empty(&sbi->s_committed_transaction)) {
2518 + md = list_entry(sbi->s_committed_transaction.next,
2519 + struct ext3_free_metadata, list);
2520 + list_del(&md->list);
2522 + spin_unlock(&sbi->s_md_lock);
2527 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2528 + md->num, md->group, md);
2530 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2531 + /* we expect to find existing buddy because it's pinned */
2534 + /* there are blocks to put in buddy to make them really free */
2537 + ext3_lock_group(sb, md->group);
2538 + for (i = 0; i < md->num; i++) {
2539 + mb_debug(" %u", md->blocks[i]);
2540 + mb_free_blocks(&e3b, md->blocks[i], 1);
2543 + ext3_unlock_group(sb, md->group);
2545 + /* balance refcounts from ext3_mb_free_metadata() */
2546 + page_cache_release(e3b.bd_buddy_page);
2547 + page_cache_release(e3b.bd_bitmap_page);
2550 + ext3_mb_release_desc(&e3b);
2553 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2556 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2558 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2560 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2563 + /* new transaction! time to close last one and free blocks for
2564 + * committed transaction. we know that only transaction can be
2565 + * active, so previos transaction can be being logged and we
2566 + * know that transaction before previous is known to be already
2567 + * logged. this means that now we may free blocks freed in all
2568 + * transactions before previous one. hope I'm clear enough ... */
2570 + spin_lock(&sbi->s_md_lock);
2571 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2572 + mb_debug("new transaction %lu, old %lu\n",
2573 + (unsigned long) handle->h_transaction->t_tid,
2574 + (unsigned long) sbi->s_last_transaction);
2575 + list_splice_init(&sbi->s_closed_transaction,
2576 + &sbi->s_committed_transaction);
2577 + list_splice_init(&sbi->s_active_transaction,
2578 + &sbi->s_closed_transaction);
2579 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2581 + spin_unlock(&sbi->s_md_lock);
2583 + ext3_mb_free_committed_blocks(sb);
2586 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2587 + int group, int block, int count)
2589 + struct ext3_group_info *db = e3b->bd_info;
2590 + struct super_block *sb = e3b->bd_sb;
2591 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2592 + struct ext3_free_metadata *md;
2595 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2596 + J_ASSERT(e3b->bd_buddy_page != NULL);
2598 + ext3_lock_group(sb, group);
2599 + for (i = 0; i < count; i++) {
2600 + md = db->bb_md_cur;
2601 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2602 + db->bb_md_cur = NULL;
2607 + ext3_unlock_group(sb, group);
2608 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2612 + md->group = group;
2614 + ext3_lock_group(sb, group);
2615 + if (db->bb_md_cur == NULL) {
2616 + spin_lock(&sbi->s_md_lock);
2617 + list_add(&md->list, &sbi->s_active_transaction);
2618 + spin_unlock(&sbi->s_md_lock);
2619 + /* protect buddy cache from being freed,
2620 + * otherwise we'll refresh it from
2621 + * on-disk bitmap and lose not-yet-available
2623 + page_cache_get(e3b->bd_buddy_page);
2624 + page_cache_get(e3b->bd_bitmap_page);
2625 + db->bb_md_cur = md;
2626 + db->bb_tid = handle->h_transaction->t_tid;
2627 + mb_debug("new md 0x%p for group %u\n",
2631 + md = db->bb_md_cur;
2635 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2636 + md->blocks[md->num] = block + i;
2638 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2639 + /* no more space, put full container on a sb's list */
2640 + db->bb_md_cur = NULL;
2643 + ext3_unlock_group(sb, group);
2647 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2648 + unsigned long block, unsigned long count,
2649 + int metadata, int *freed)
2651 + struct buffer_head *bitmap_bh = NULL;
2652 + struct ext3_group_desc *gdp;
2653 + struct ext3_super_block *es;
2654 + unsigned long bit, overflow;
2655 + struct buffer_head *gd_bh;
2656 + unsigned long block_group;
2657 + struct ext3_sb_info *sbi;
2658 + struct super_block *sb;
2659 + struct ext3_buddy e3b;
2665 + printk ("ext3_free_blocks: nonexistent device");
2669 + ext3_mb_poll_new_transaction(sb, handle);
2671 + sbi = EXT3_SB(sb);
2672 + es = EXT3_SB(sb)->s_es;
2673 + if (block < le32_to_cpu(es->s_first_data_block) ||
2674 + block + count < block ||
2675 + block + count > le32_to_cpu(es->s_blocks_count)) {
2676 + ext3_error (sb, "ext3_free_blocks",
2677 + "Freeing blocks not in datazone - "
2678 + "block = %lu, count = %lu", block, count);
2679 + goto error_return;
2682 + ext3_debug("freeing block %lu\n", block);
2686 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2687 + EXT3_BLOCKS_PER_GROUP(sb);
2688 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2689 + EXT3_BLOCKS_PER_GROUP(sb);
2691 + * Check to see if we are freeing blocks across a group
2694 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2695 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2696 + count -= overflow;
2698 + brelse(bitmap_bh);
2699 + bitmap_bh = read_block_bitmap(sb, block_group);
2701 + goto error_return;
2702 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2704 + goto error_return;
2706 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2707 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2708 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2709 + EXT3_SB(sb)->s_itb_per_group) ||
2710 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2711 + EXT3_SB(sb)->s_itb_per_group))
2712 + ext3_error (sb, "ext3_free_blocks",
2713 + "Freeing blocks in system zones - "
2714 + "Block = %lu, count = %lu",
2717 + BUFFER_TRACE(bitmap_bh, "getting write access");
2718 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2720 + goto error_return;
2723 + * We are about to modify some metadata. Call the journal APIs
2724 + * to unshare ->b_data if a currently-committing transaction is
2727 + BUFFER_TRACE(gd_bh, "get_write_access");
2728 + err = ext3_journal_get_write_access(handle, gd_bh);
2730 + goto error_return;
2732 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2734 + goto error_return;
2736 +#ifdef AGGRESSIVE_CHECK
2739 + for (i = 0; i < count; i++)
2740 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2743 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2745 + /* We dirtied the bitmap block */
2746 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2747 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2750 + /* blocks being freed are metadata. these blocks shouldn't
2751 + * be used until this transaction is committed */
2752 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2754 + ext3_lock_group(sb, block_group);
2755 + mb_free_blocks(&e3b, bit, count);
2756 + ext3_unlock_group(sb, block_group);
2759 + spin_lock(sb_bgl_lock(sbi, block_group));
2760 + gdp->bg_free_blocks_count =
2761 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2762 + spin_unlock(sb_bgl_lock(sbi, block_group));
2763 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2765 + ext3_mb_release_desc(&e3b);
2769 + /* And the group descriptor block */
2770 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2771 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2772 + if (!err) err = ret;
2774 + if (overflow && !err) {
2781 + brelse(bitmap_bh);
2782 + ext3_std_error(sb, err);
2786 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2788 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2789 + int free, ret = -ENOSPC;
2791 + BUG_ON(blocks < 0);
2792 + spin_lock(&sbi->s_reserve_lock);
2793 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2794 + if (blocks <= free - sbi->s_blocks_reserved) {
2795 + sbi->s_blocks_reserved += blocks;
2798 + spin_unlock(&sbi->s_reserve_lock);
2802 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2804 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2806 + BUG_ON(blocks < 0);
2807 + spin_lock(&sbi->s_reserve_lock);
2808 + sbi->s_blocks_reserved -= blocks;
2809 + WARN_ON(sbi->s_blocks_reserved < 0);
2810 + if (sbi->s_blocks_reserved < 0)
2811 + sbi->s_blocks_reserved = 0;
2812 + spin_unlock(&sbi->s_reserve_lock);
2815 +int ext3_new_block(handle_t *handle, struct inode *inode,
2816 + unsigned long goal, int *errp)
2820 + if (!test_opt(inode->i_sb, MBALLOC)) {
2821 + ret = ext3_new_block_old(handle, inode, goal, errp);
2825 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2831 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2832 + unsigned long block, unsigned long count, int metadata)
2834 + struct super_block *sb;
2838 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2839 + ext3_free_blocks_sb(handle, sb, block, count, &freed);
2841 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2843 + DQUOT_FREE_BLOCK(inode, freed);
2847 +#define EXT3_ROOT "ext3"
2848 +#define EXT3_MB_STATS_NAME "mb_stats"
2849 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2850 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2851 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2853 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2854 + int count, int *eof, void *data)
2862 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2867 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2868 + unsigned long count, void *data)
2872 + if (count >= sizeof(str)) {
2873 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2874 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2875 + return -EOVERFLOW;
2878 + if (copy_from_user(str, buffer, count))
2881 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2882 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2886 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2887 + int count, int *eof, void *data)
2895 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2900 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2901 + unsigned long count, void *data)
2906 + if (count >= sizeof(str)) {
2907 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2908 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2909 + return -EOVERFLOW;
2912 + if (copy_from_user(str, buffer, count))
2915 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2916 + value = simple_strtol(str, NULL, 0);
2920 + ext3_mb_max_to_scan = value;
2925 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2926 + int count, int *eof, void *data)
2934 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2939 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2940 + unsigned long count, void *data)
2945 + if (count >= sizeof(str)) {
2946 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2947 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2948 + return -EOVERFLOW;
2951 + if (copy_from_user(str, buffer, count))
2954 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2955 + value = simple_strtol(str, NULL, 0);
2959 + ext3_mb_min_to_scan = value;
2964 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2965 + int count, int *eof, void *data)
2973 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
2978 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2979 + unsigned long count, void *data)
2984 + if (count >= sizeof(str)) {
2985 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2986 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2987 + return -EOVERFLOW;
2990 + if (copy_from_user(str, buffer, count))
2993 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2994 + value = simple_strtol(str, NULL, 0);
2998 + ext3_mb_order2_reqs = value;
3003 +int __init init_ext3_proc(void)
3005 + struct proc_dir_entry *proc_ext3_mb_stats;
3006 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3007 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3008 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3010 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3011 + if (proc_root_ext3 == NULL) {
3012 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3016 + /* Initialize EXT3_MB_STATS_NAME */
3017 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3018 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3019 + if (proc_ext3_mb_stats == NULL) {
3020 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3021 + EXT3_MB_STATS_NAME);
3022 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3026 + proc_ext3_mb_stats->data = NULL;
3027 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3028 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3030 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3031 + proc_ext3_mb_max_to_scan = create_proc_entry(
3032 + EXT3_MB_MAX_TO_SCAN_NAME,
3033 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3034 + if (proc_ext3_mb_max_to_scan == NULL) {
3035 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3036 + EXT3_MB_MAX_TO_SCAN_NAME);
3037 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3038 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3042 + proc_ext3_mb_max_to_scan->data = NULL;
3043 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3044 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3046 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3047 + proc_ext3_mb_min_to_scan = create_proc_entry(
3048 + EXT3_MB_MIN_TO_SCAN_NAME,
3049 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3050 + if (proc_ext3_mb_min_to_scan == NULL) {
3051 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3052 + EXT3_MB_MIN_TO_SCAN_NAME);
3053 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3054 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3055 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3059 + proc_ext3_mb_min_to_scan->data = NULL;
3060 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3061 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3063 + /* Initialize EXT3_ORDER2_REQ */
3064 + proc_ext3_mb_order2_req = create_proc_entry(
3065 + EXT3_MB_ORDER2_REQ,
3066 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3067 + if (proc_ext3_mb_order2_req == NULL) {
3068 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3069 + EXT3_MB_ORDER2_REQ);
3070 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
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_order2_req->data = NULL;
3078 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3079 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3084 +void exit_ext3_proc(void)
3086 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3087 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3088 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3089 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3090 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3092 Index: linux-2.6.16.i686/fs/ext3/Makefile
3093 ===================================================================
3094 --- linux-2.6.16.i686.orig/fs/ext3/Makefile 2006-05-30 22:55:32.000000000 +0800
3095 +++ linux-2.6.16.i686/fs/ext3/Makefile 2006-05-30 23:02:59.000000000 +0800
3098 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3099 ioctl.o namei.o super.o symlink.o hash.o resize.o \
3101 + extents.o mballoc.o
3103 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3104 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o