1 Index: linux-2.6.12.6-bull/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.12.6-bull.orig/include/linux/ext3_fs.h 2006-04-29 20:39:09.000000000 +0400
4 +++ linux-2.6.12.6-bull/include/linux/ext3_fs.h 2006-04-29 20:39:10.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 @@ -366,6 +374,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 @@ -727,7 +736,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 @@ -848,6 +857,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.12.6-bull/include/linux/ext3_fs_sb.h
72 ===================================================================
73 --- linux-2.6.12.6-bull.orig/include/linux/ext3_fs_sb.h 2005-08-29 20:55:27.000000000 +0400
74 +++ linux-2.6.12.6-bull/include/linux/ext3_fs_sb.h 2006-04-29 20:39:10.000000000 +0400
76 #include <linux/wait.h>
77 #include <linux/blockgroup_lock.h>
78 #include <linux/percpu_counter.h>
79 +#include <linux/list.h>
81 #include <linux/rbtree.h>
82 +#include <linux/proc_fs.h>
84 +struct ext3_buddy_group_blocks;
85 +struct ext3_mb_history;
86 +#define EXT3_BB_MAX_BLOCKS
89 * third extended-fs super-block data in memory
90 @@ -78,6 +84,43 @@ struct ext3_sb_info {
91 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
92 int s_jquota_fmt; /* Format of quota to use */
95 + /* for buddy allocator */
96 + struct ext3_group_info ***s_group_info;
97 + struct inode *s_buddy_cache;
98 + long s_blocks_reserved;
99 + spinlock_t s_reserve_lock;
100 + struct list_head s_active_transaction;
101 + struct list_head s_closed_transaction;
102 + struct list_head s_committed_transaction;
103 + spinlock_t s_md_lock;
104 + tid_t s_last_transaction;
106 + unsigned short *s_mb_offsets, *s_mb_maxs;
107 + unsigned long s_stripe;
109 + /* history to debug policy */
110 + struct ext3_mb_history *s_mb_history;
111 + int s_mb_history_cur;
112 + int s_mb_history_max;
113 + struct proc_dir_entry *s_mb_proc;
114 + spinlock_t s_mb_history_lock;
116 + /* stats for buddy allocator */
117 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
118 + atomic_t s_bal_success; /* we found long enough chunks */
119 + atomic_t s_bal_allocated; /* in blocks */
120 + atomic_t s_bal_ex_scanned; /* total extents scanned */
121 + atomic_t s_bal_goals; /* goal hits */
122 + atomic_t s_bal_breaks; /* too long searches */
123 + atomic_t s_bal_2orders; /* 2^order hits */
124 + spinlock_t s_bal_lock;
125 + unsigned long s_mb_buddies_generated;
126 + unsigned long long s_mb_generation_time;
129 +#define EXT3_GROUP_INFO(sb, group) \
130 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
131 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
133 #endif /* _LINUX_EXT3_FS_SB */
134 Index: linux-2.6.12.6-bull/fs/ext3/super.c
135 ===================================================================
136 --- linux-2.6.12.6-bull.orig/fs/ext3/super.c 2006-04-29 20:39:09.000000000 +0400
137 +++ linux-2.6.12.6-bull/fs/ext3/super.c 2006-04-29 20:39:10.000000000 +0400
138 @@ -387,6 +387,7 @@ static void ext3_put_super (struct super
139 struct ext3_super_block *es = sbi->s_es;
142 + ext3_mb_release(sb);
143 ext3_ext_release(sb);
144 ext3_xattr_put_super(sb);
145 journal_destroy(sbi->s_journal);
146 @@ -597,6 +598,7 @@ enum {
147 Opt_ignore, Opt_barrier, Opt_err, Opt_resize,
148 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
149 Opt_extents, Opt_noextents, Opt_extdebug,
150 + Opt_mballoc, Opt_nomballoc, Opt_stripe,
153 static match_table_t tokens = {
154 @@ -650,6 +651,9 @@ static match_table_t tokens = {
155 {Opt_extents, "extents"},
156 {Opt_noextents, "noextents"},
157 {Opt_extdebug, "extdebug"},
158 + {Opt_mballoc, "mballoc"},
159 + {Opt_nomballoc, "nomballoc"},
160 + {Opt_stripe, "stripe=%u"},
161 {Opt_barrier, "barrier=%u"},
163 {Opt_resize, "resize"},
164 @@ -965,6 +967,19 @@ clear_qf_name:
166 set_opt (sbi->s_mount_opt, EXTDEBUG);
169 + set_opt(sbi->s_mount_opt, MBALLOC);
171 + case Opt_nomballoc:
172 + clear_opt(sbi->s_mount_opt, MBALLOC);
175 + if (match_int(&args[0], &option))
179 + sbi->s_stripe = option;
183 "EXT3-fs: Unrecognized mount option \"%s\" "
184 @@ -1670,6 +1675,7 @@ static int ext3_fill_super (struct super
185 ext3_count_dirs(sb));
188 + ext3_mb_init(sb, needs_recovery);
192 @@ -2549,7 +2555,13 @@ static struct file_system_type ext3_fs_t
194 static int __init init_ext3_fs(void)
196 - int err = init_ext3_xattr();
199 + err = init_ext3_proc();
203 + err = init_ext3_xattr();
206 err = init_inodecache();
207 @@ -2571,6 +2583,7 @@ static void __exit exit_ext3_fs(void)
208 unregister_filesystem(&ext3_fs_type);
209 destroy_inodecache();
214 int ext3_prep_san_write(struct inode *inode, long *blocks,
215 Index: linux-2.6.12.6-bull/fs/ext3/extents.c
216 ===================================================================
217 --- linux-2.6.12.6-bull.orig/fs/ext3/extents.c 2006-04-29 20:39:09.000000000 +0400
218 +++ linux-2.6.12.6-bull/fs/ext3/extents.c 2006-04-29 20:39:10.000000000 +0400
219 @@ -777,7 +777,7 @@ cleanup:
220 for (i = 0; i < depth; i++) {
223 - ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
224 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
228 @@ -1434,7 +1434,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
229 path->p_idx->ei_leaf);
230 bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
231 ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
232 - ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
233 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
237 @@ -1919,10 +1919,12 @@ ext3_remove_blocks(struct ext3_extents_t
238 int needed = ext3_remove_blocks_credits(tree, ex, from, to);
239 handle_t *handle = ext3_journal_start(tree->inode, needed);
240 struct buffer_head *bh;
242 + int i, metadata = 0;
245 return PTR_ERR(handle);
246 + if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
248 if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
250 unsigned long num, start;
251 @@ -1934,7 +1936,7 @@ ext3_remove_blocks(struct ext3_extents_t
252 bh = sb_find_get_block(tree->inode->i_sb, start + i);
253 ext3_forget(handle, 0, tree->inode, bh, start + i);
255 - ext3_free_blocks(handle, tree->inode, start, num);
256 + ext3_free_blocks(handle, tree->inode, start, num, metadata);
257 } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
258 printk("strange request: removal %lu-%lu from %u:%u\n",
259 from, to, ex->ee_block, ex->ee_len);
260 Index: linux-2.6.12.6-bull/fs/ext3/inode.c
261 ===================================================================
262 --- linux-2.6.12.6-bull.orig/fs/ext3/inode.c 2006-04-29 20:39:09.000000000 +0400
263 +++ linux-2.6.12.6-bull/fs/ext3/inode.c 2006-04-29 20:39:10.000000000 +0400
264 @@ -564,7 +564,7 @@ static int ext3_alloc_branch(handle_t *h
265 ext3_journal_forget(handle, branch[i].bh);
267 for (i = 0; i < keys; i++)
268 - ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1);
269 + ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1, 1);
273 @@ -1850,7 +1850,7 @@ ext3_clear_blocks(handle_t *handle, stru
277 - ext3_free_blocks(handle, inode, block_to_free, count);
278 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
282 @@ -2023,7 +2023,7 @@ static void ext3_free_branches(handle_t
283 ext3_journal_test_restart(handle, inode);
286 - ext3_free_blocks(handle, inode, nr, 1);
287 + ext3_free_blocks(handle, inode, nr, 1, 1);
291 Index: linux-2.6.12.6-bull/fs/ext3/balloc.c
292 ===================================================================
293 --- linux-2.6.12.6-bull.orig/fs/ext3/balloc.c 2005-08-29 20:55:27.000000000 +0400
294 +++ linux-2.6.12.6-bull/fs/ext3/balloc.c 2006-04-29 20:39:10.000000000 +0400
295 @@ -79,7 +79,7 @@ struct ext3_group_desc * ext3_get_group_
297 * Return buffer_head on success or NULL in case of failure.
299 -static struct buffer_head *
300 +struct buffer_head *
301 read_block_bitmap(struct super_block *sb, unsigned int block_group)
303 struct ext3_group_desc * desc;
304 @@ -490,24 +490,6 @@ error_return:
308 -/* Free given blocks, update quota and i_blocks field */
309 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
310 - unsigned long block, unsigned long count)
312 - struct super_block * sb;
313 - int dquot_freed_blocks;
317 - printk ("ext3_free_blocks: nonexistent device");
320 - ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
321 - if (dquot_freed_blocks)
322 - DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
327 * For ext3 allocations, we must not reuse any blocks which are
328 * allocated in the bitmap buffer's "last committed data" copy. This
329 @@ -1162,7 +1144,7 @@ int ext3_should_retry_alloc(struct super
330 * bitmap, and then for any free bit if that fails.
331 * This function also updates quota and i_blocks field.
333 -int ext3_new_block(handle_t *handle, struct inode *inode,
334 +int ext3_new_block_old(handle_t *handle, struct inode *inode,
335 unsigned long goal, int *errp)
337 struct buffer_head *bitmap_bh = NULL;
338 Index: linux-2.6.12.6-bull/fs/ext3/xattr.c
339 ===================================================================
340 --- linux-2.6.12.6-bull.orig/fs/ext3/xattr.c 2005-08-29 20:55:27.000000000 +0400
341 +++ linux-2.6.12.6-bull/fs/ext3/xattr.c 2006-04-29 20:39:10.000000000 +0400
342 @@ -484,7 +484,7 @@ ext3_xattr_release_block(handle_t *handl
343 ea_bdebug(bh, "refcount now=0; freeing");
345 mb_cache_entry_free(ce);
346 - ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
347 + ext3_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
349 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
351 @@ -804,7 +804,7 @@ inserted:
352 new_bh = sb_getblk(sb, block);
355 - ext3_free_blocks(handle, inode, block, 1);
356 + ext3_free_blocks(handle, inode, block, 1, 1);
360 Index: linux-2.6.12.6-bull/fs/ext3/mballoc.c
361 ===================================================================
362 --- linux-2.6.12.6-bull.orig/fs/ext3/mballoc.c 2006-04-22 17:31:47.543334750 +0400
363 +++ linux-2.6.12.6-bull/fs/ext3/mballoc.c 2006-04-30 01:24:11.000000000 +0400
366 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
367 + * Written by Alex Tomas <alex@clusterfs.com>
369 + * This program is free software; you can redistribute it and/or modify
370 + * it under the terms of the GNU General Public License version 2 as
371 + * published by the Free Software Foundation.
373 + * This program is distributed in the hope that it will be useful,
374 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
375 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376 + * GNU General Public License for more details.
378 + * You should have received a copy of the GNU General Public Licens
379 + * along with this program; if not, write to the Free Software
380 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
385 + * mballoc.c contains the multiblocks allocation routines
388 +#include <linux/config.h>
389 +#include <linux/time.h>
390 +#include <linux/fs.h>
391 +#include <linux/namei.h>
392 +#include <linux/jbd.h>
393 +#include <linux/ext3_fs.h>
394 +#include <linux/ext3_jbd.h>
395 +#include <linux/quotaops.h>
396 +#include <linux/buffer_head.h>
397 +#include <linux/module.h>
398 +#include <linux/swap.h>
399 +#include <linux/proc_fs.h>
400 +#include <linux/pagemap.h>
401 +#include <linux/seq_file.h>
405 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
406 + * - track min/max extents in each group for better group selection
407 + * - mb_mark_used() may allocate chunk right after splitting buddy
408 + * - special flag to advice allocator to look for requested + N blocks
409 + * this may improve interaction between extents and mballoc
410 + * - tree of groups sorted by number of free blocks
411 + * - percpu reservation code (hotpath)
416 + * with AGRESSIVE_CHECK allocator runs consistency checks over
417 + * structures. these checks slow things down a lot
419 +#define AGGRESSIVE_CHECK__
425 +#define mb_debug(fmt,a...) printk(fmt, ##a)
427 +#define mb_debug(fmt,a...)
431 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
432 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
434 +#define EXT3_MB_HISTORY
437 + * How long mballoc can look for a best extent (in found extents)
439 +long ext3_mb_max_to_scan = 500;
442 + * How long mballoc must look for a best extent
444 +long ext3_mb_min_to_scan = 30;
447 + * with 'ext3_mb_stats' allocator will collect stats that will be
448 + * shown at umount. The collecting costs though!
451 +long ext3_mb_stats = 1;
454 + * for which requests use 2^N search using buddies
456 +long ext3_mb_order2_reqs = 8;
458 +#ifdef EXT3_BB_MAX_BLOCKS
459 +#undef EXT3_BB_MAX_BLOCKS
461 +#define EXT3_BB_MAX_BLOCKS 30
463 +struct ext3_free_metadata {
464 + unsigned short group;
465 + unsigned short num;
466 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
467 + struct list_head list;
470 +struct ext3_group_info {
471 + unsigned long bb_state;
472 + unsigned long bb_tid;
473 + struct ext3_free_metadata *bb_md_cur;
474 + unsigned short bb_first_free;
475 + unsigned short bb_free;
476 + unsigned short bb_fragments;
477 + unsigned short bb_counters[];
481 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
482 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
484 +#define EXT3_MB_GRP_NEED_INIT(grp) \
485 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
487 +struct ext3_free_extent {
493 +struct ext3_allocation_context {
494 + struct super_block *ac_sb;
497 + struct ext3_free_extent ac_g_ex;
499 + /* the best found extent */
500 + struct ext3_free_extent ac_b_ex;
502 + /* number of iterations done. we have to track to limit searching */
503 + unsigned long ac_ex_scanned;
504 + __u16 ac_groups_scanned;
509 + __u8 ac_flags; /* allocation hints */
512 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
513 + * N > 0, the field stores N, otherwise 0 */
515 + struct page *ac_buddy_page;
516 + struct page *ac_bitmap_page;
519 +#define AC_STATUS_CONTINUE 1
520 +#define AC_STATUS_FOUND 2
521 +#define AC_STATUS_BREAK 3
523 +struct ext3_mb_history {
524 + struct ext3_free_extent goal; /* goal allocation */
525 + struct ext3_free_extent result; /* result allocation */
528 + __u16 found; /* how many extents have been found */
529 + __u16 groups; /* how many groups have been scanned */
530 + __u16 tail; /* what tail broke some buddy */
531 + __u16 buddy; /* buddy the tail ^^^ broke */
532 + __u8 cr; /* which phase the result extent was found at */
537 + struct page *bd_buddy_page;
539 + struct page *bd_bitmap_page;
541 + struct ext3_group_info *bd_info;
542 + struct super_block *bd_sb;
546 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
547 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
549 +#ifndef EXT3_MB_HISTORY
550 +#define ext3_mb_store_history(sb,ino,ac)
552 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
553 + struct ext3_allocation_context *ac);
556 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
558 +static struct proc_dir_entry *proc_root_ext3;
560 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
561 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
562 +void ext3_mb_free_committed_blocks(struct super_block *);
564 +#if BITS_PER_LONG == 64
565 +#define mb_correct_addr_and_bit(bit,addr) \
567 + bit += ((unsigned long) addr & 7UL) << 3; \
568 + addr = (void *) ((unsigned long) addr & ~7UL); \
570 +#elif BITS_PER_LONG == 32
571 +#define mb_correct_addr_and_bit(bit,addr) \
573 + bit += ((unsigned long) addr & 3UL) << 3; \
574 + addr = (void *) ((unsigned long) addr & ~3UL); \
577 +#error "how many bits you are?!"
580 +static inline int mb_test_bit(int bit, void *addr)
582 + mb_correct_addr_and_bit(bit,addr);
583 + return ext2_test_bit(bit, addr);
586 +static inline void mb_set_bit(int bit, void *addr)
588 + mb_correct_addr_and_bit(bit,addr);
589 + ext2_set_bit(bit, addr);
592 +static inline void mb_set_bit_atomic(int bit, void *addr)
594 + mb_correct_addr_and_bit(bit,addr);
595 + ext2_set_bit_atomic(NULL, bit, addr);
598 +static inline void mb_clear_bit(int bit, void *addr)
600 + mb_correct_addr_and_bit(bit,addr);
601 + ext2_clear_bit(bit, addr);
604 +static inline void mb_clear_bit_atomic(int bit, void *addr)
606 + mb_correct_addr_and_bit(bit,addr);
607 + ext2_clear_bit_atomic(NULL, bit, addr);
610 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
613 +#if BITS_PER_LONG == 64
614 + fix = ((unsigned long) addr & 7UL) << 3;
615 + addr = (void *) ((unsigned long) addr & ~7UL);
616 +#elif BITS_PER_LONG == 32
617 + fix = ((unsigned long) addr & 3UL) << 3;
618 + addr = (void *) ((unsigned long) addr & ~3UL);
620 +#error "how many bits you are?!"
624 + return ext2_find_next_zero_bit(addr, max, start) - fix;
627 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
631 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
632 + J_ASSERT(max != NULL);
634 + if (order > e3b->bd_blkbits + 1) {
639 + /* at order 0 we see each particular block */
640 + *max = 1 << (e3b->bd_blkbits + 3);
642 + return EXT3_MB_BITMAP(e3b);
644 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
645 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
650 +#ifdef AGGRESSIVE_CHECK
652 +static void mb_check_buddy(struct ext3_buddy *e3b)
654 + int order = e3b->bd_blkbits + 1;
655 + int max, max2, i, j, k, count;
656 + int fragments = 0, fstart;
657 + void *buddy, *buddy2;
659 + if (!test_opt(e3b->bd_sb, MBALLOC))
663 + static int mb_check_counter = 0;
664 + if (mb_check_counter++ % 300 != 0)
668 + while (order > 1) {
669 + buddy = mb_find_buddy(e3b, order, &max);
671 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
673 + J_ASSERT(buddy != buddy2);
674 + J_ASSERT(max * 2 == max2);
677 + for (i = 0; i < max; i++) {
679 + if (mb_test_bit(i, buddy)) {
680 + /* only single bit in buddy2 may be 1 */
681 + if (!mb_test_bit(i << 1, buddy2))
682 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
683 + else if (!mb_test_bit((i << 1) + 1, buddy2))
684 + J_ASSERT(mb_test_bit(i << 1, buddy2));
688 + /* both bits in buddy2 must be 0 */
689 + J_ASSERT(mb_test_bit(i << 1, buddy2));
690 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
692 + for (j = 0; j < (1 << order); j++) {
693 + k = (i * (1 << order)) + j;
694 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
698 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
703 + buddy = mb_find_buddy(e3b, 0, &max);
704 + for (i = 0; i < max; i++) {
705 + if (!mb_test_bit(i, buddy)) {
706 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
707 + if (fstart == -1) {
714 + /* check used bits only */
715 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
716 + buddy2 = mb_find_buddy(e3b, j, &max2);
718 + J_ASSERT(k < max2);
719 + J_ASSERT(mb_test_bit(k, buddy2));
722 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
723 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
727 +#define mb_check_buddy(e3b)
730 +/* find most significant bit */
731 +static int inline fmsb(unsigned short word)
745 + } while (word != 0);
751 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
752 + int len, struct ext3_group_info *grp)
754 + struct ext3_sb_info *sbi = EXT3_SB(sb);
755 + unsigned short min, max, chunk, border;
757 + mb_debug("mark %u/%u free\n", first, len);
758 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
760 + border = 2 << sb->s_blocksize_bits;
763 + /* find how many blocks can be covered since this position */
764 + max = ffs(first | border) - 1;
766 + /* find how many blocks of power 2 we need to mark */
769 + mb_debug(" %u/%u -> max %u, min %u\n",
770 + first & ((2 << sb->s_blocksize_bits) - 1),
777 + /* mark multiblock chunks only */
778 + grp->bb_counters[min]++;
780 + mb_debug(" set %u at %u \n", first >> min,
781 + sbi->s_mb_offsets[min]);
782 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
791 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
794 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
795 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
796 + unsigned short i = 0, first, len;
797 + unsigned free = 0, fragments = 0;
798 + unsigned long long period = get_cycles();
800 + i = mb_find_next_zero_bit(bitmap, max, 0);
801 + grp->bb_first_free = i;
805 + i = ext2_find_next_le_bit(bitmap, max, i);
809 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
811 + grp->bb_counters[0]++;
813 + i = mb_find_next_zero_bit(bitmap, max, i);
815 + grp->bb_fragments = fragments;
817 + /* bb_state shouldn't being modified because all
818 + * others waits for init completion on page lock */
819 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
820 + if (free != grp->bb_free) {
821 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
822 + group, free, grp->bb_free);
823 + grp->bb_free = free;
826 + period = get_cycles() - period;
827 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
828 + EXT3_SB(sb)->s_mb_buddies_generated++;
829 + EXT3_SB(sb)->s_mb_generation_time += period;
830 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
833 +static int ext3_mb_init_cache(struct page *page)
835 + int blocksize, blocks_per_page, groups_per_page;
836 + int err = 0, i, first_group, first_block;
837 + struct super_block *sb;
838 + struct buffer_head *bhs;
839 + struct buffer_head **bh;
840 + struct inode *inode;
841 + char *data, *bitmap;
843 + mb_debug("init page %lu\n", page->index);
845 + inode = page->mapping->host;
847 + blocksize = 1 << inode->i_blkbits;
848 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
850 + groups_per_page = blocks_per_page >> 1;
851 + if (groups_per_page == 0)
852 + groups_per_page = 1;
854 + /* allocate buffer_heads to read bitmaps */
855 + if (groups_per_page > 1) {
857 + i = sizeof(struct buffer_head *) * groups_per_page;
858 + bh = kmalloc(i, GFP_NOFS);
865 + first_group = page->index * blocks_per_page / 2;
867 + /* read all groups the page covers into the cache */
868 + for (i = 0; i < groups_per_page; i++) {
869 + struct ext3_group_desc * desc;
871 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
875 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
880 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
884 + if (buffer_uptodate(bh[i]))
887 + lock_buffer(bh[i]);
888 + if (buffer_uptodate(bh[i])) {
889 + unlock_buffer(bh[i]);
894 + bh[i]->b_end_io = end_buffer_read_sync;
895 + submit_bh(READ, bh[i]);
896 + mb_debug("read bitmap for group %u\n", first_group + i);
899 + /* wait for I/O completion */
900 + for (i = 0; i < groups_per_page && bh[i]; i++)
901 + wait_on_buffer(bh[i]);
904 + for (i = 0; i < groups_per_page && bh[i]; i++)
905 + if (!buffer_uptodate(bh[i]))
908 + first_block = page->index * blocks_per_page;
909 + for (i = 0; i < blocks_per_page; i++) {
912 + group = (first_block + i) >> 1;
913 + if (group >= EXT3_SB(sb)->s_groups_count)
916 + data = page_address(page) + (i * blocksize);
917 + bitmap = bh[group - first_group]->b_data;
919 + if ((first_block + i) & 1) {
920 + /* this is block of buddy */
921 + mb_debug("put buddy for group %u in page %lu/%x\n",
922 + group, page->index, i * blocksize);
923 + memset(data, 0xff, blocksize);
924 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
925 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
926 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
927 + ext3_mb_generate_buddy(sb, data, bitmap, group);
929 + /* this is block of bitmap */
930 + mb_debug("put bitmap for group %u in page %lu/%x\n",
931 + group, page->index, i * blocksize);
932 + memcpy(data, bitmap, blocksize);
935 + SetPageUptodate(page);
939 + for (i = 0; i < groups_per_page && bh[i]; i++)
947 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
948 + struct ext3_buddy *e3b)
950 + struct ext3_sb_info *sbi = EXT3_SB(sb);
951 + struct inode *inode = sbi->s_buddy_cache;
952 + int blocks_per_page, block, pnum, poff;
955 + mb_debug("load group %u\n", group);
957 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
959 + e3b->bd_blkbits = sb->s_blocksize_bits;
960 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
962 + e3b->bd_group = group;
963 + e3b->bd_buddy_page = NULL;
964 + e3b->bd_bitmap_page = NULL;
967 + pnum = block / blocks_per_page;
968 + poff = block % blocks_per_page;
970 + /* we could use find_or_create_page(), but it locks page
971 + * what we'd like to avoid in fast path ... */
972 + page = find_get_page(inode->i_mapping, pnum);
973 + if (page == NULL || !PageUptodate(page)) {
975 + page_cache_release(page);
976 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
978 + BUG_ON(page->mapping != inode->i_mapping);
979 + if (!PageUptodate(page))
980 + ext3_mb_init_cache(page);
984 + if (page == NULL || !PageUptodate(page))
986 + e3b->bd_bitmap_page = page;
987 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
988 + mark_page_accessed(page);
991 + pnum = block / blocks_per_page;
992 + poff = block % blocks_per_page;
994 + page = find_get_page(inode->i_mapping, pnum);
995 + if (page == NULL || !PageUptodate(page)) {
997 + page_cache_release(page);
998 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1000 + BUG_ON(page->mapping != inode->i_mapping);
1001 + if (!PageUptodate(page))
1002 + ext3_mb_init_cache(page);
1003 + unlock_page(page);
1006 + if (page == NULL || !PageUptodate(page))
1008 + e3b->bd_buddy_page = page;
1009 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1010 + mark_page_accessed(page);
1012 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1013 + J_ASSERT(e3b->bd_buddy_page != NULL);
1018 + if (e3b->bd_bitmap_page)
1019 + page_cache_release(e3b->bd_bitmap_page);
1020 + if (e3b->bd_buddy_page)
1021 + page_cache_release(e3b->bd_buddy_page);
1022 + e3b->bd_buddy = NULL;
1023 + e3b->bd_bitmap = NULL;
1027 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1029 + if (e3b->bd_bitmap_page)
1030 + page_cache_release(e3b->bd_bitmap_page);
1031 + if (e3b->bd_buddy_page)
1032 + page_cache_release(e3b->bd_buddy_page);
1037 +ext3_lock_group(struct super_block *sb, int group)
1039 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1040 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1044 +ext3_unlock_group(struct super_block *sb, int group)
1046 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1047 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1050 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1055 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1056 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1058 + bb = EXT3_MB_BUDDY(e3b);
1059 + while (order <= e3b->bd_blkbits + 1) {
1060 + block = block >> 1;
1061 + if (!mb_test_bit(block, bb)) {
1062 + /* this block is part of buddy of order 'order' */
1065 + bb += 1 << (e3b->bd_blkbits - order);
1071 +static inline void mb_clear_bits(void *bm, int cur, int len)
1076 + while (cur < len) {
1077 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1078 + /* fast path: clear whole word at once */
1079 + addr = bm + (cur >> 3);
1084 + mb_clear_bit_atomic(cur, bm);
1089 +static inline void mb_set_bits(void *bm, int cur, int len)
1094 + while (cur < len) {
1095 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1096 + /* fast path: clear whole word at once */
1097 + addr = bm + (cur >> 3);
1098 + *addr = 0xffffffff;
1102 + mb_set_bit_atomic(cur, bm);
1107 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1109 + int block = 0, max = 0, order;
1110 + void *buddy, *buddy2;
1112 + mb_check_buddy(e3b);
1114 + e3b->bd_info->bb_free += count;
1115 + if (first < e3b->bd_info->bb_first_free)
1116 + e3b->bd_info->bb_first_free = first;
1118 + /* let's maintain fragments counter */
1120 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1121 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1122 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1124 + e3b->bd_info->bb_fragments--;
1125 + else if (!block && !max)
1126 + e3b->bd_info->bb_fragments++;
1128 + /* let's maintain buddy itself */
1129 + while (count-- > 0) {
1133 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1134 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1135 + e3b->bd_info->bb_counters[order]++;
1137 + /* start of the buddy */
1138 + buddy = mb_find_buddy(e3b, order, &max);
1142 + if (mb_test_bit(block, buddy) ||
1143 + mb_test_bit(block + 1, buddy))
1146 + /* both the buddies are free, try to coalesce them */
1147 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1153 + /* for special purposes, we don't set
1154 + * free bits in bitmap */
1155 + mb_set_bit(block, buddy);
1156 + mb_set_bit(block + 1, buddy);
1158 + e3b->bd_info->bb_counters[order]--;
1159 + e3b->bd_info->bb_counters[order]--;
1161 + block = block >> 1;
1163 + e3b->bd_info->bb_counters[order]++;
1165 + mb_clear_bit(block, buddy2);
1169 + mb_check_buddy(e3b);
1174 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1175 + int needed, struct ext3_free_extent *ex)
1177 + int next = block, max, ord;
1180 + J_ASSERT(ex != NULL);
1182 + buddy = mb_find_buddy(e3b, order, &max);
1184 + J_ASSERT(block < max);
1185 + if (mb_test_bit(block, buddy)) {
1192 + if (likely(order == 0)) {
1193 + /* find actual order */
1194 + order = mb_find_order_for_block(e3b, block);
1195 + block = block >> order;
1198 + ex->fe_len = 1 << order;
1199 + ex->fe_start = block << order;
1200 + ex->fe_group = e3b->bd_group;
1202 + /* calc difference from given start */
1203 + next = next - ex->fe_start;
1204 + ex->fe_len -= next;
1205 + ex->fe_start += next;
1207 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1209 + if (block + 1 >= max)
1212 + next = (block + 1) * (1 << order);
1213 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1216 + ord = mb_find_order_for_block(e3b, next);
1219 + block = next >> order;
1220 + ex->fe_len += 1 << order;
1223 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1224 + return ex->fe_len;
1227 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1229 + int ord, mlen = 0, max = 0, cur;
1230 + int start = ex->fe_start;
1231 + int len = ex->fe_len;
1236 + mb_check_buddy(e3b);
1238 + e3b->bd_info->bb_free -= len;
1239 + if (e3b->bd_info->bb_first_free == start)
1240 + e3b->bd_info->bb_first_free += len;
1242 + /* let's maintain fragments counter */
1244 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1245 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1246 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1248 + e3b->bd_info->bb_fragments++;
1249 + else if (!mlen && !max)
1250 + e3b->bd_info->bb_fragments--;
1252 + /* let's maintain buddy itself */
1254 + ord = mb_find_order_for_block(e3b, start);
1256 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1257 + /* the whole chunk may be allocated at once! */
1259 + buddy = mb_find_buddy(e3b, ord, &max);
1260 + J_ASSERT((start >> ord) < max);
1261 + mb_set_bit(start >> ord, buddy);
1262 + e3b->bd_info->bb_counters[ord]--;
1265 + J_ASSERT(len >= 0);
1269 + /* store for history */
1271 + ret = len | (ord << 16);
1273 + /* we have to split large buddy */
1274 + J_ASSERT(ord > 0);
1275 + buddy = mb_find_buddy(e3b, ord, &max);
1276 + mb_set_bit(start >> ord, buddy);
1277 + e3b->bd_info->bb_counters[ord]--;
1280 + cur = (start >> ord) & ~1U;
1281 + buddy = mb_find_buddy(e3b, ord, &max);
1282 + mb_clear_bit(cur, buddy);
1283 + mb_clear_bit(cur + 1, buddy);
1284 + e3b->bd_info->bb_counters[ord]++;
1285 + e3b->bd_info->bb_counters[ord]++;
1288 + /* now drop all the bits in bitmap */
1289 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1291 + mb_check_buddy(e3b);
1297 + * Must be called under group lock!
1299 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1300 + struct ext3_buddy *e3b)
1302 + unsigned long ret;
1304 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1305 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1307 + ac->ac_status = AC_STATUS_FOUND;
1308 + ac->ac_tail = ret & 0xffff;
1309 + ac->ac_buddy = ret >> 16;
1311 + /* hold in-core structures until allocated
1312 + * blocks are marked non-free in on-disk bitmap */
1313 + ac->ac_buddy_page = e3b->bd_buddy_page;
1314 + page_cache_get(e3b->bd_buddy_page);
1315 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1316 + page_cache_get(e3b->bd_bitmap_page);
1320 + * The routine checks whether found extent is good enough. If it is,
1321 + * then the extent gets marked used and flag is set to the context
1322 + * to stop scanning. Otherwise, the extent is compared with the
1323 + * previous found extent and if new one is better, then it's stored
1324 + * in the context. Later, the best found extent will be used, if
1325 + * mballoc can't find good enough extent.
1327 + * FIXME: real allocation policy is to be designed yet!
1329 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1330 + struct ext3_free_extent *ex,
1331 + struct ext3_buddy *e3b)
1333 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1334 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1336 + J_ASSERT(ex->fe_len > 0);
1337 + J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1338 + J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1343 + * The special case - take what you catch first
1345 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1347 + ext3_mb_use_best_found(ac, e3b);
1352 + * Let's check whether the chunk is good enough
1354 + if (ex->fe_len == gex->fe_len) {
1356 + ext3_mb_use_best_found(ac, e3b);
1361 + * If this is first found extent, just store it in the context
1363 + if (bex->fe_len == 0) {
1369 + * If new found extent is better, store it in the context
1371 + if (bex->fe_len < gex->fe_len) {
1372 + /* if the request isn't satisfied, any found extent
1373 + * larger than previous best one is better */
1374 + if (ex->fe_len > bex->fe_len)
1376 + } else if (ex->fe_len > gex->fe_len) {
1377 + /* if the request is satisfied, then we try to find
1378 + * an extent that still satisfy the request, but is
1379 + * smaller than previous one */
1384 + * Let's scan at least few extents and don't pick up a first one
1386 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1387 + ac->ac_status = AC_STATUS_BREAK;
1390 + * We don't want to scan for a whole year
1392 + if (ac->ac_found > ext3_mb_max_to_scan)
1393 + ac->ac_status = AC_STATUS_BREAK;
1396 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1397 + struct ext3_buddy *e3b)
1399 + struct ext3_free_extent ex = ac->ac_b_ex;
1400 + int group = ex.fe_group, max, err;
1402 + J_ASSERT(ex.fe_len > 0);
1403 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1407 + ext3_lock_group(ac->ac_sb, group);
1408 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1412 + ext3_mb_use_best_found(ac, e3b);
1415 + ext3_unlock_group(ac->ac_sb, group);
1417 + ext3_mb_release_desc(e3b);
1422 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1423 + struct ext3_buddy *e3b)
1425 + int group = ac->ac_g_ex.fe_group, max, err;
1426 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1427 + struct ext3_super_block *es = sbi->s_es;
1428 + struct ext3_free_extent ex;
1430 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1434 + ext3_lock_group(ac->ac_sb, group);
1435 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1436 + ac->ac_g_ex.fe_len, &ex);
1438 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1439 + unsigned long start;
1440 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1441 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1442 + if (start % sbi->s_stripe == 0) {
1445 + ext3_mb_use_best_found(ac, e3b);
1447 + } else if (max >= ac->ac_g_ex.fe_len) {
1448 + J_ASSERT(ex.fe_len > 0);
1449 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1450 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1453 + ext3_mb_use_best_found(ac, e3b);
1454 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1455 + /* Sometimes, caller may want to merge even small
1456 + * number of blocks to an existing extent */
1457 + J_ASSERT(ex.fe_len > 0);
1458 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1459 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1462 + ext3_mb_use_best_found(ac, e3b);
1464 + ext3_unlock_group(ac->ac_sb, group);
1466 + ext3_mb_release_desc(e3b);
1472 + * The routine scans buddy structures (not bitmap!) from given order
1473 + * to max order and tries to find big enough chunk to satisfy the req
1475 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1476 + struct ext3_buddy *e3b)
1478 + struct super_block *sb = ac->ac_sb;
1479 + struct ext3_group_info *grp = e3b->bd_info;
1483 + J_ASSERT(ac->ac_2order > 0);
1484 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1485 + if (grp->bb_counters[i] == 0)
1488 + buddy = mb_find_buddy(e3b, i, &max);
1489 + if (buddy == NULL) {
1490 + printk(KERN_ALERT "looking for wrong order?\n");
1494 + k = mb_find_next_zero_bit(buddy, max, 0);
1495 + J_ASSERT(k < max);
1499 + ac->ac_b_ex.fe_len = 1 << i;
1500 + ac->ac_b_ex.fe_start = k << i;
1501 + ac->ac_b_ex.fe_group = e3b->bd_group;
1503 + ext3_mb_use_best_found(ac, e3b);
1504 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1506 + if (unlikely(ext3_mb_stats))
1507 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1514 + * The routine scans the group and measures all found extents.
1515 + * In order to optimize scanning, caller must pass number of
1516 + * free blocks in the group, so the routine can know upper limit.
1518 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1519 + struct ext3_buddy *e3b)
1521 + struct super_block *sb = ac->ac_sb;
1522 + void *bitmap = EXT3_MB_BITMAP(e3b);
1523 + struct ext3_free_extent ex;
1526 + free = e3b->bd_info->bb_free;
1527 + J_ASSERT(free > 0);
1529 + i = e3b->bd_info->bb_first_free;
1531 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1532 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1533 + if (i >= sb->s_blocksize * 8) {
1534 + J_ASSERT(free == 0);
1538 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1539 + J_ASSERT(ex.fe_len > 0);
1540 + J_ASSERT(free >= ex.fe_len);
1542 + ext3_mb_measure_extent(ac, &ex, e3b);
1545 + free -= ex.fe_len;
1550 + * This is a special case for storages like raid5
1551 + * we try to find stripe-aligned chunks for stripe-size requests
1553 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1554 + struct ext3_buddy *e3b)
1556 + struct super_block *sb = ac->ac_sb;
1557 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1558 + void *bitmap = EXT3_MB_BITMAP(e3b);
1559 + struct ext3_free_extent ex;
1560 + unsigned long i, max;
1562 + J_ASSERT(sbi->s_stripe != 0);
1564 + /* find first stripe-aligned block */
1565 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb) +
1566 + le32_to_cpu(sbi->s_es->s_first_data_block);
1567 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1568 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block)) %
1569 + EXT3_BLOCKS_PER_GROUP(sb);
1571 + while (i < sb->s_blocksize * 8) {
1572 + if (!mb_test_bit(i, bitmap)) {
1573 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1574 + if (max >= sbi->s_stripe) {
1577 + ext3_mb_use_best_found(ac, e3b);
1581 + i += sbi->s_stripe;
1585 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1586 + int group, int cr)
1588 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1589 + unsigned free, fragments, i, bits;
1591 + J_ASSERT(cr >= 0 && cr < 4);
1592 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1594 + free = grp->bb_free;
1595 + fragments = grp->bb_fragments;
1598 + if (fragments == 0)
1603 + J_ASSERT(ac->ac_2order != 0);
1604 + bits = ac->ac_sb->s_blocksize_bits + 1;
1605 + for (i = ac->ac_2order; i <= bits; i++)
1606 + if (grp->bb_counters[i] > 0)
1610 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1614 + if (free >= ac->ac_g_ex.fe_len)
1626 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1627 + unsigned long goal, int *len, int flags, int *errp)
1629 + struct buffer_head *bitmap_bh = NULL;
1630 + struct ext3_allocation_context ac;
1631 + int i, group, block, cr, err = 0;
1632 + struct ext3_group_desc *gdp;
1633 + struct ext3_super_block *es;
1634 + struct buffer_head *gdp_bh;
1635 + struct ext3_sb_info *sbi;
1636 + struct super_block *sb;
1637 + struct ext3_buddy e3b;
1639 + J_ASSERT(len != NULL);
1640 + J_ASSERT(*len > 0);
1644 + printk("ext3_mb_new_nblocks: nonexistent device");
1648 + if (!test_opt(sb, MBALLOC)) {
1649 + static int ext3_mballoc_warning = 0;
1650 + if (ext3_mballoc_warning == 0) {
1651 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1652 + "mballoc disabled!\n");
1653 + ext3_mballoc_warning++;
1656 + err = ext3_new_block_old(handle, inode, goal, errp);
1660 + ext3_mb_poll_new_transaction(sb, handle);
1662 + sbi = EXT3_SB(sb);
1663 + es = EXT3_SB(sb)->s_es;
1666 + * We can't allocate > group size
1668 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1669 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1671 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1672 + /* someone asks for non-reserved blocks */
1674 + err = ext3_mb_reserve_blocks(sb, 1);
1681 + ac.ac_buddy_page = NULL;
1682 + ac.ac_bitmap_page = NULL;
1685 + * Check quota for allocation of this blocks.
1687 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1695 + /* start searching from the goal */
1696 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1697 + goal >= le32_to_cpu(es->s_blocks_count))
1698 + goal = le32_to_cpu(es->s_first_data_block);
1699 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1700 + EXT3_BLOCKS_PER_GROUP(sb);
1701 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1702 + EXT3_BLOCKS_PER_GROUP(sb));
1704 + /* set up allocation goals */
1705 + ac.ac_b_ex.fe_group = 0;
1706 + ac.ac_b_ex.fe_start = 0;
1707 + ac.ac_b_ex.fe_len = 0;
1708 + ac.ac_status = AC_STATUS_CONTINUE;
1709 + ac.ac_groups_scanned = 0;
1710 + ac.ac_ex_scanned = 0;
1712 + ac.ac_sb = inode->i_sb;
1713 + ac.ac_g_ex.fe_group = group;
1714 + ac.ac_g_ex.fe_start = block;
1715 + ac.ac_g_ex.fe_len = *len;
1716 + ac.ac_flags = flags;
1718 + ac.ac_criteria = 0;
1720 + if (*len == 1 && sbi->s_stripe) {
1721 + /* looks like a metadata, let's use a dirty hack for raid5
1722 + * move all metadata in first groups in hope to hit cached
1723 + * sectors and thus avoid read-modify cycles in raid5 */
1724 + ac.ac_g_ex.fe_group = group = 0;
1727 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1729 + if (i >= ext3_mb_order2_reqs) {
1731 + if ((*len & (~(1 << i))) == 0)
1735 + /* first, try the goal */
1736 + err = ext3_mb_find_by_goal(&ac, &e3b);
1739 + if (ac.ac_status == AC_STATUS_FOUND)
1742 + /* Let's just scan groups to find more-less suitable blocks */
1743 + cr = ac.ac_2order ? 0 : 1;
1745 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1746 + ac.ac_criteria = cr;
1747 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1748 + if (group == EXT3_SB(sb)->s_groups_count)
1751 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1752 + /* we need full data about the group
1753 + * to make a good selection */
1754 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1757 + ext3_mb_release_desc(&e3b);
1760 + /* check is group good for our criteries */
1761 + if (!ext3_mb_good_group(&ac, group, cr))
1764 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1768 + ext3_lock_group(sb, group);
1769 + if (!ext3_mb_good_group(&ac, group, cr)) {
1770 + /* someone did allocation from this group */
1771 + ext3_unlock_group(sb, group);
1772 + ext3_mb_release_desc(&e3b);
1776 + ac.ac_groups_scanned++;
1778 + ext3_mb_simple_scan_group(&ac, &e3b);
1779 + else if (cr == 1 && *len == sbi->s_stripe)
1780 + ext3_mb_scan_aligned(&ac, &e3b);
1782 + ext3_mb_complex_scan_group(&ac, &e3b);
1784 + ext3_unlock_group(sb, group);
1786 + ext3_mb_release_desc(&e3b);
1788 + if (ac.ac_status != AC_STATUS_CONTINUE)
1793 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1794 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1796 + * We've been searching too long. Let's try to allocate
1797 + * the best chunk we've found so far
1800 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1801 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1802 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1803 + ac.ac_g_ex.fe_len);*/
1804 + ext3_mb_try_best_found(&ac, &e3b);
1805 + if (ac.ac_status != AC_STATUS_FOUND) {
1807 + * Someone more lucky has already allocated it.
1808 + * The only thing we can do is just take first
1810 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1812 + ac.ac_b_ex.fe_group = 0;
1813 + ac.ac_b_ex.fe_start = 0;
1814 + ac.ac_b_ex.fe_len = 0;
1815 + ac.ac_status = AC_STATUS_CONTINUE;
1816 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1822 + if (ac.ac_status != AC_STATUS_FOUND) {
1824 + * We aren't lucky definitely
1826 + DQUOT_FREE_BLOCK(inode, *len);
1830 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1831 + ac.ac_status, ac.ac_flags);
1832 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1833 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1834 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1835 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1836 + sbi->s_blocks_reserved, ac.ac_found);
1837 + printk("EXT3-fs: groups: ");
1838 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1839 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1846 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1848 + /* good news - free block(s) have been found. now it's time
1849 + * to mark block(s) in good old journaled bitmap */
1850 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1851 + + ac.ac_b_ex.fe_start
1852 + + le32_to_cpu(es->s_first_data_block);
1854 + /* we made a desicion, now mark found blocks in good old
1855 + * bitmap to be journaled */
1857 + ext3_debug("using block group %d(%d)\n",
1858 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1860 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1866 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1872 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1878 + err = ext3_journal_get_write_access(handle, gdp_bh);
1882 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1883 + + ac.ac_b_ex.fe_start
1884 + + le32_to_cpu(es->s_first_data_block);
1886 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1887 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1888 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1889 + EXT3_SB(sb)->s_itb_per_group))
1890 + ext3_error(sb, "ext3_new_block",
1891 + "Allocating block in system zone - "
1892 + "block = %u", block);
1893 +#ifdef AGGRESSIVE_CHECK
1894 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1895 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1897 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1899 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1900 + gdp->bg_free_blocks_count =
1901 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1902 + - ac.ac_b_ex.fe_len);
1903 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1904 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1906 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1909 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1915 + brelse(bitmap_bh);
1917 + /* drop non-allocated, but dquote'd blocks */
1918 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1919 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1921 + *len = ac.ac_b_ex.fe_len;
1922 + J_ASSERT(*len > 0);
1923 + J_ASSERT(block != 0);
1927 + /* if we've already allocated something, roll it back */
1928 + if (ac.ac_status == AC_STATUS_FOUND) {
1929 + /* FIXME: free blocks here */
1932 + DQUOT_FREE_BLOCK(inode, *len);
1933 + brelse(bitmap_bh);
1937 + if (ac.ac_buddy_page)
1938 + page_cache_release(ac.ac_buddy_page);
1939 + if (ac.ac_bitmap_page)
1940 + page_cache_release(ac.ac_bitmap_page);
1942 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1943 + /* block wasn't reserved before and we reserved it
1944 + * at the beginning of allocation. it doesn't matter
1945 + * whether we allocated anything or we failed: time
1946 + * to release reservation. NOTE: because I expect
1947 + * any multiblock request from delayed allocation
1948 + * path only, here is single block always */
1949 + ext3_mb_release_blocks(sb, 1);
1952 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1953 + atomic_inc(&sbi->s_bal_reqs);
1954 + atomic_add(*len, &sbi->s_bal_allocated);
1955 + if (*len >= ac.ac_g_ex.fe_len)
1956 + atomic_inc(&sbi->s_bal_success);
1957 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1958 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1959 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1960 + atomic_inc(&sbi->s_bal_goals);
1961 + if (ac.ac_found > ext3_mb_max_to_scan)
1962 + atomic_inc(&sbi->s_bal_breaks);
1965 + ext3_mb_store_history(sb, inode->i_ino, &ac);
1969 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1971 +#ifdef EXT3_MB_HISTORY
1972 +struct ext3_mb_proc_session {
1973 + struct ext3_mb_history *history;
1974 + struct super_block *sb;
1979 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1980 + struct ext3_mb_history *hs,
1983 + if (hs == s->history + s->max)
1985 + if (!first && hs == s->history + s->start)
1987 + while (hs->goal.fe_len == 0) {
1989 + if (hs == s->history + s->max)
1991 + if (hs == s->history + s->start)
1997 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
1999 + struct ext3_mb_proc_session *s = seq->private;
2000 + struct ext3_mb_history *hs;
2004 + return SEQ_START_TOKEN;
2005 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2008 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2012 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2014 + struct ext3_mb_proc_session *s = seq->private;
2015 + struct ext3_mb_history *hs = v;
2018 + if (v == SEQ_START_TOKEN)
2019 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2021 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2024 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2026 + struct ext3_mb_history *hs = v;
2027 + char buf[20], buf2[20];
2029 + if (v == SEQ_START_TOKEN) {
2030 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2031 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2032 + "merge", "tail", "broken");
2036 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2037 + hs->goal.fe_start, hs->goal.fe_len);
2038 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2039 + hs->result.fe_start, hs->result.fe_len);
2040 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2041 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2042 + hs->cr, hs->merged ? "M" : "", hs->tail,
2043 + hs->buddy ? 1 << hs->buddy : 0);
2047 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2051 +static struct seq_operations ext3_mb_seq_history_ops = {
2052 + .start = ext3_mb_seq_history_start,
2053 + .next = ext3_mb_seq_history_next,
2054 + .stop = ext3_mb_seq_history_stop,
2055 + .show = ext3_mb_seq_history_show,
2058 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2060 + struct super_block *sb = PDE(inode)->data;
2061 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2062 + struct ext3_mb_proc_session *s;
2065 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2068 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2069 + s->history = kmalloc(size, GFP_KERNEL);
2070 + if (s->history == NULL) {
2075 + spin_lock(&sbi->s_mb_history_lock);
2076 + memcpy(s->history, sbi->s_mb_history, size);
2077 + s->max = sbi->s_mb_history_max;
2078 + s->start = sbi->s_mb_history_cur % s->max;
2079 + spin_unlock(&sbi->s_mb_history_lock);
2081 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2083 + struct seq_file *m = (struct seq_file *)file->private_data;
2086 + kfree(s->history);
2093 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2095 + struct seq_file *seq = (struct seq_file *)file->private_data;
2096 + struct ext3_mb_proc_session *s = seq->private;
2097 + kfree(s->history);
2099 + return seq_release(inode, file);
2102 +static struct file_operations ext3_mb_seq_history_fops = {
2103 + .owner = THIS_MODULE,
2104 + .open = ext3_mb_seq_history_open,
2106 + .llseek = seq_lseek,
2107 + .release = ext3_mb_seq_history_release,
2110 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2112 + struct super_block *sb = seq->private;
2113 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2116 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2120 + return (void *) group;
2123 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2125 + struct super_block *sb = seq->private;
2126 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2130 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2133 + return (void *) group;;
2136 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2138 + struct super_block *sb = seq->private;
2139 + long group = (long) v, i;
2141 + struct ext3_group_info info;
2142 + unsigned short counters[16];
2147 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2148 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2149 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2150 + "2^11", "2^12", "2^13");
2152 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2153 + sizeof(struct ext3_group_info);
2154 + ext3_lock_group(sb, group);
2155 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2156 + ext3_unlock_group(sb, group);
2158 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2161 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2162 + sg.info.bb_fragments, sg.info.bb_first_free);
2163 + for (i = 0; i <= 13; i++)
2164 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2165 + sg.info.bb_counters[i] : 0);
2166 + seq_printf(seq, " ]\n");
2171 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2175 +static struct seq_operations ext3_mb_seq_groups_ops = {
2176 + .start = ext3_mb_seq_groups_start,
2177 + .next = ext3_mb_seq_groups_next,
2178 + .stop = ext3_mb_seq_groups_stop,
2179 + .show = ext3_mb_seq_groups_show,
2182 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2184 + struct super_block *sb = PDE(inode)->data;
2187 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2189 + struct seq_file *m = (struct seq_file *)file->private_data;
2196 +static struct file_operations ext3_mb_seq_groups_fops = {
2197 + .owner = THIS_MODULE,
2198 + .open = ext3_mb_seq_groups_open,
2200 + .llseek = seq_lseek,
2201 + .release = seq_release,
2204 +static void ext3_mb_history_release(struct super_block *sb)
2206 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2209 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2210 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2211 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2212 + remove_proc_entry(name, proc_root_ext3);
2214 + if (sbi->s_mb_history)
2215 + kfree(sbi->s_mb_history);
2218 +static void ext3_mb_history_init(struct super_block *sb)
2220 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2224 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2225 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2226 + if (sbi->s_mb_proc != NULL) {
2227 + struct proc_dir_entry *p;
2228 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2230 + p->proc_fops = &ext3_mb_seq_history_fops;
2233 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2235 + p->proc_fops = &ext3_mb_seq_groups_fops;
2240 + sbi->s_mb_history_max = 1000;
2241 + sbi->s_mb_history_cur = 0;
2242 + spin_lock_init(&sbi->s_mb_history_lock);
2243 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2244 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2245 + memset(sbi->s_mb_history, 0, i);
2246 + /* if we can't allocate history, then we simple won't use it */
2250 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2251 + struct ext3_allocation_context *ac)
2253 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2254 + struct ext3_mb_history h;
2256 + if (likely(sbi->s_mb_history == NULL))
2259 + h.pid = current->pid;
2261 + h.goal = ac->ac_g_ex;
2262 + h.result = ac->ac_b_ex;
2263 + h.found = ac->ac_found;
2264 + h.cr = ac->ac_criteria;
2265 + h.groups = ac->ac_groups_scanned;
2266 + h.tail = ac->ac_tail;
2267 + h.buddy = ac->ac_buddy;
2269 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2270 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2273 + spin_lock(&sbi->s_mb_history_lock);
2274 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2275 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2276 + sbi->s_mb_history_cur = 0;
2277 + spin_unlock(&sbi->s_mb_history_lock);
2281 +#define ext3_mb_history_release(sb)
2282 +#define ext3_mb_history_init(sb)
2285 +int ext3_mb_init_backend(struct super_block *sb)
2287 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2288 + int i, j, len, metalen;
2289 + int num_meta_group_infos =
2290 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2291 + EXT3_DESC_PER_BLOCK_BITS(sb);
2292 + struct ext3_group_info **meta_group_info;
2294 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2295 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2296 + * So a two level scheme suffices for now. */
2297 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2298 + num_meta_group_infos, GFP_KERNEL);
2299 + if (sbi->s_group_info == NULL) {
2300 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2303 + sbi->s_buddy_cache = new_inode(sb);
2304 + if (sbi->s_buddy_cache == NULL) {
2305 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2309 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2310 + for (i = 0; i < num_meta_group_infos; i++) {
2311 + if ((i + 1) == num_meta_group_infos)
2312 + metalen = sizeof(*meta_group_info) *
2313 + (sbi->s_groups_count -
2314 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2315 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2316 + if (meta_group_info == NULL) {
2317 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2319 + goto err_freemeta;
2321 + sbi->s_group_info[i] = meta_group_info;
2325 + * calculate needed size. if change bb_counters size,
2326 + * don't forget about ext3_mb_generate_buddy()
2328 + len = sizeof(struct ext3_group_info);
2329 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2330 + for (i = 0; i < sbi->s_groups_count; i++) {
2331 + struct ext3_group_desc * desc;
2334 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2335 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2337 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2338 + if (meta_group_info[j] == NULL) {
2339 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2341 + goto err_freebuddy;
2343 + desc = ext3_get_group_desc(sb, i, NULL);
2344 + if (desc == NULL) {
2345 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2346 + goto err_freebuddy;
2348 + memset(meta_group_info[j], 0, len);
2349 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2350 + &meta_group_info[j]->bb_state);
2351 + meta_group_info[j]->bb_free =
2352 + le16_to_cpu(desc->bg_free_blocks_count);
2359 + kfree(EXT3_GROUP_INFO(sb, i));
2362 + i = num_meta_group_infos;
2365 + kfree(sbi->s_group_info[i]);
2366 + iput(sbi->s_buddy_cache);
2368 + kfree(sbi->s_group_info);
2372 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2374 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2375 + struct inode *root = sb->s_root->d_inode;
2376 + unsigned i, offset, max;
2377 + struct dentry *dentry;
2379 + if (!test_opt(sb, MBALLOC))
2382 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2384 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2385 + if (sbi->s_mb_offsets == NULL) {
2386 + clear_opt(sbi->s_mount_opt, MBALLOC);
2389 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2390 + if (sbi->s_mb_maxs == NULL) {
2391 + clear_opt(sbi->s_mount_opt, MBALLOC);
2392 + kfree(sbi->s_mb_maxs);
2396 + /* order 0 is regular bitmap */
2397 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2398 + sbi->s_mb_offsets[0] = 0;
2402 + max = sb->s_blocksize << 2;
2404 + sbi->s_mb_offsets[i] = offset;
2405 + sbi->s_mb_maxs[i] = max;
2406 + offset += 1 << (sb->s_blocksize_bits - i);
2409 + } while (i <= sb->s_blocksize_bits + 1);
2411 + /* init file for buddy data */
2412 + if ((i = ext3_mb_init_backend(sb))) {
2413 + clear_opt(sbi->s_mount_opt, MBALLOC);
2414 + kfree(sbi->s_mb_offsets);
2415 + kfree(sbi->s_mb_maxs);
2419 + spin_lock_init(&sbi->s_reserve_lock);
2420 + spin_lock_init(&sbi->s_md_lock);
2421 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2422 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2423 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2424 + spin_lock_init(&sbi->s_bal_lock);
2426 + /* remove old on-disk buddy file */
2427 + down(&root->i_sem);
2428 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2429 + if (dentry->d_inode != NULL) {
2430 + i = vfs_unlink(root, dentry);
2432 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2437 + ext3_mb_history_init(sb);
2439 + printk("EXT3-fs: mballoc enabled\n");
2443 +int ext3_mb_release(struct super_block *sb)
2445 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2446 + int i, num_meta_group_infos;
2448 + if (!test_opt(sb, MBALLOC))
2451 + /* release freed, non-committed blocks */
2452 + spin_lock(&sbi->s_md_lock);
2453 + list_splice_init(&sbi->s_closed_transaction,
2454 + &sbi->s_committed_transaction);
2455 + list_splice_init(&sbi->s_active_transaction,
2456 + &sbi->s_committed_transaction);
2457 + spin_unlock(&sbi->s_md_lock);
2458 + ext3_mb_free_committed_blocks(sb);
2460 + if (sbi->s_group_info) {
2461 + for (i = 0; i < sbi->s_groups_count; i++)
2462 + kfree(EXT3_GROUP_INFO(sb, i));
2463 + num_meta_group_infos = (sbi->s_groups_count +
2464 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2465 + EXT3_DESC_PER_BLOCK_BITS(sb);
2466 + for (i = 0; i < num_meta_group_infos; i++)
2467 + kfree(sbi->s_group_info[i]);
2468 + kfree(sbi->s_group_info);
2470 + if (sbi->s_mb_offsets)
2471 + kfree(sbi->s_mb_offsets);
2472 + if (sbi->s_mb_maxs)
2473 + kfree(sbi->s_mb_maxs);
2474 + if (sbi->s_buddy_cache)
2475 + iput(sbi->s_buddy_cache);
2476 + if (sbi->s_blocks_reserved)
2477 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2478 + sbi->s_blocks_reserved);
2479 + if (ext3_mb_stats) {
2480 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2481 + atomic_read(&sbi->s_bal_allocated),
2482 + atomic_read(&sbi->s_bal_reqs),
2483 + atomic_read(&sbi->s_bal_success));
2484 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2485 + "%u 2^N hits, %u breaks\n",
2486 + atomic_read(&sbi->s_bal_ex_scanned),
2487 + atomic_read(&sbi->s_bal_goals),
2488 + atomic_read(&sbi->s_bal_2orders),
2489 + atomic_read(&sbi->s_bal_breaks));
2490 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2491 + sbi->s_mb_buddies_generated++,
2492 + sbi->s_mb_generation_time);
2495 + ext3_mb_history_release(sb);
2500 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2502 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2503 + int err, i, count = 0, count2 = 0;
2504 + struct ext3_free_metadata *md;
2505 + struct ext3_buddy e3b;
2507 + if (list_empty(&sbi->s_committed_transaction))
2510 + /* there is committed blocks to be freed yet */
2512 + /* get next array of blocks */
2514 + spin_lock(&sbi->s_md_lock);
2515 + if (!list_empty(&sbi->s_committed_transaction)) {
2516 + md = list_entry(sbi->s_committed_transaction.next,
2517 + struct ext3_free_metadata, list);
2518 + list_del(&md->list);
2520 + spin_unlock(&sbi->s_md_lock);
2525 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2526 + md->num, md->group, md);
2528 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2529 + /* we expect to find existing buddy because it's pinned */
2532 + /* there are blocks to put in buddy to make them really free */
2535 + ext3_lock_group(sb, md->group);
2536 + for (i = 0; i < md->num; i++) {
2537 + mb_debug(" %u", md->blocks[i]);
2538 + mb_free_blocks(&e3b, md->blocks[i], 1);
2541 + ext3_unlock_group(sb, md->group);
2543 + /* balance refcounts from ext3_mb_free_metadata() */
2544 + page_cache_release(e3b.bd_buddy_page);
2545 + page_cache_release(e3b.bd_bitmap_page);
2548 + ext3_mb_release_desc(&e3b);
2551 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2554 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2556 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2558 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2561 + /* new transaction! time to close last one and free blocks for
2562 + * committed transaction. we know that only transaction can be
2563 + * active, so previos transaction can be being logged and we
2564 + * know that transaction before previous is known to be already
2565 + * logged. this means that now we may free blocks freed in all
2566 + * transactions before previous one. hope I'm clear enough ... */
2568 + spin_lock(&sbi->s_md_lock);
2569 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2570 + mb_debug("new transaction %lu, old %lu\n",
2571 + (unsigned long) handle->h_transaction->t_tid,
2572 + (unsigned long) sbi->s_last_transaction);
2573 + list_splice_init(&sbi->s_closed_transaction,
2574 + &sbi->s_committed_transaction);
2575 + list_splice_init(&sbi->s_active_transaction,
2576 + &sbi->s_closed_transaction);
2577 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2579 + spin_unlock(&sbi->s_md_lock);
2581 + ext3_mb_free_committed_blocks(sb);
2584 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2585 + int group, int block, int count)
2587 + struct ext3_group_info *db = e3b->bd_info;
2588 + struct super_block *sb = e3b->bd_sb;
2589 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2590 + struct ext3_free_metadata *md;
2593 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2594 + J_ASSERT(e3b->bd_buddy_page != NULL);
2596 + ext3_lock_group(sb, group);
2597 + for (i = 0; i < count; i++) {
2598 + md = db->bb_md_cur;
2599 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2600 + db->bb_md_cur = NULL;
2605 + ext3_unlock_group(sb, group);
2606 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2610 + md->group = group;
2612 + ext3_lock_group(sb, group);
2613 + if (db->bb_md_cur == NULL) {
2614 + spin_lock(&sbi->s_md_lock);
2615 + list_add(&md->list, &sbi->s_active_transaction);
2616 + spin_unlock(&sbi->s_md_lock);
2617 + /* protect buddy cache from being freed,
2618 + * otherwise we'll refresh it from
2619 + * on-disk bitmap and lose not-yet-available
2621 + page_cache_get(e3b->bd_buddy_page);
2622 + page_cache_get(e3b->bd_bitmap_page);
2623 + db->bb_md_cur = md;
2624 + db->bb_tid = handle->h_transaction->t_tid;
2625 + mb_debug("new md 0x%p for group %u\n",
2629 + md = db->bb_md_cur;
2633 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2634 + md->blocks[md->num] = block + i;
2636 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2637 + /* no more space, put full container on a sb's list */
2638 + db->bb_md_cur = NULL;
2641 + ext3_unlock_group(sb, group);
2645 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2646 + unsigned long block, unsigned long count,
2647 + int metadata, int *freed)
2649 + struct buffer_head *bitmap_bh = NULL;
2650 + struct ext3_group_desc *gdp;
2651 + struct ext3_super_block *es;
2652 + unsigned long bit, overflow;
2653 + struct buffer_head *gd_bh;
2654 + unsigned long block_group;
2655 + struct ext3_sb_info *sbi;
2656 + struct super_block *sb;
2657 + struct ext3_buddy e3b;
2663 + printk ("ext3_free_blocks: nonexistent device");
2667 + ext3_mb_poll_new_transaction(sb, handle);
2669 + sbi = EXT3_SB(sb);
2670 + es = EXT3_SB(sb)->s_es;
2671 + if (block < le32_to_cpu(es->s_first_data_block) ||
2672 + block + count < block ||
2673 + block + count > le32_to_cpu(es->s_blocks_count)) {
2674 + ext3_error (sb, "ext3_free_blocks",
2675 + "Freeing blocks not in datazone - "
2676 + "block = %lu, count = %lu", block, count);
2677 + goto error_return;
2680 + ext3_debug("freeing block %lu\n", block);
2684 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2685 + EXT3_BLOCKS_PER_GROUP(sb);
2686 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2687 + EXT3_BLOCKS_PER_GROUP(sb);
2689 + * Check to see if we are freeing blocks across a group
2692 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2693 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2694 + count -= overflow;
2696 + brelse(bitmap_bh);
2697 + bitmap_bh = read_block_bitmap(sb, block_group);
2699 + goto error_return;
2700 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2702 + goto error_return;
2704 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2705 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2706 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2707 + EXT3_SB(sb)->s_itb_per_group) ||
2708 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2709 + EXT3_SB(sb)->s_itb_per_group))
2710 + ext3_error (sb, "ext3_free_blocks",
2711 + "Freeing blocks in system zones - "
2712 + "Block = %lu, count = %lu",
2715 + BUFFER_TRACE(bitmap_bh, "getting write access");
2716 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2718 + goto error_return;
2721 + * We are about to modify some metadata. Call the journal APIs
2722 + * to unshare ->b_data if a currently-committing transaction is
2725 + BUFFER_TRACE(gd_bh, "get_write_access");
2726 + err = ext3_journal_get_write_access(handle, gd_bh);
2728 + goto error_return;
2730 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2732 + goto error_return;
2734 +#ifdef AGGRESSIVE_CHECK
2737 + for (i = 0; i < count; i++)
2738 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2741 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2743 + /* We dirtied the bitmap block */
2744 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2745 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2748 + /* blocks being freed are metadata. these blocks shouldn't
2749 + * be used until this transaction is committed */
2750 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2752 + ext3_lock_group(sb, block_group);
2753 + mb_free_blocks(&e3b, bit, count);
2754 + ext3_unlock_group(sb, block_group);
2757 + spin_lock(sb_bgl_lock(sbi, block_group));
2758 + gdp->bg_free_blocks_count =
2759 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2760 + spin_unlock(sb_bgl_lock(sbi, block_group));
2761 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2763 + ext3_mb_release_desc(&e3b);
2767 + /* And the group descriptor block */
2768 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2769 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2770 + if (!err) err = ret;
2772 + if (overflow && !err) {
2779 + brelse(bitmap_bh);
2780 + ext3_std_error(sb, err);
2784 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2786 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2787 + int free, ret = -ENOSPC;
2789 + BUG_ON(blocks < 0);
2790 + spin_lock(&sbi->s_reserve_lock);
2791 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2792 + if (blocks <= free - sbi->s_blocks_reserved) {
2793 + sbi->s_blocks_reserved += blocks;
2796 + spin_unlock(&sbi->s_reserve_lock);
2800 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2802 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2804 + BUG_ON(blocks < 0);
2805 + spin_lock(&sbi->s_reserve_lock);
2806 + sbi->s_blocks_reserved -= blocks;
2807 + WARN_ON(sbi->s_blocks_reserved < 0);
2808 + if (sbi->s_blocks_reserved < 0)
2809 + sbi->s_blocks_reserved = 0;
2810 + spin_unlock(&sbi->s_reserve_lock);
2813 +int ext3_new_block(handle_t *handle, struct inode *inode,
2814 + unsigned long goal, int *errp)
2818 + if (!test_opt(inode->i_sb, MBALLOC)) {
2819 + ret = ext3_new_block_old(handle, inode, goal, errp);
2823 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2829 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2830 + unsigned long block, unsigned long count, int metadata)
2832 + struct super_block *sb;
2836 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2837 + ext3_free_blocks_sb(handle, sb, block, count, &freed);
2839 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2841 + DQUOT_FREE_BLOCK(inode, freed);
2845 +#define EXT3_ROOT "ext3"
2846 +#define EXT3_MB_STATS_NAME "mb_stats"
2847 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2848 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2849 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2851 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2852 + int count, int *eof, void *data)
2860 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2865 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2866 + unsigned long count, void *data)
2870 + if (count >= sizeof(str)) {
2871 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2872 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2873 + return -EOVERFLOW;
2876 + if (copy_from_user(str, buffer, count))
2879 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2880 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2884 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2885 + int count, int *eof, void *data)
2893 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2898 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2899 + unsigned long count, void *data)
2904 + if (count >= sizeof(str)) {
2905 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2906 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2907 + return -EOVERFLOW;
2910 + if (copy_from_user(str, buffer, count))
2913 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2914 + value = simple_strtol(str, NULL, 0);
2918 + ext3_mb_max_to_scan = value;
2923 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2924 + int count, int *eof, void *data)
2932 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2937 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2938 + unsigned long count, void *data)
2943 + if (count >= sizeof(str)) {
2944 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2945 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2946 + return -EOVERFLOW;
2949 + if (copy_from_user(str, buffer, count))
2952 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2953 + value = simple_strtol(str, NULL, 0);
2957 + ext3_mb_min_to_scan = value;
2962 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2963 + int count, int *eof, void *data)
2971 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
2976 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2977 + unsigned long count, void *data)
2982 + if (count >= sizeof(str)) {
2983 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2984 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2985 + return -EOVERFLOW;
2988 + if (copy_from_user(str, buffer, count))
2991 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2992 + value = simple_strtol(str, NULL, 0);
2996 + ext3_mb_order2_reqs = value;
3001 +int __init init_ext3_proc(void)
3003 + struct proc_dir_entry *proc_ext3_mb_stats;
3004 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3005 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3006 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3008 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3009 + if (proc_root_ext3 == NULL) {
3010 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3014 + /* Initialize EXT3_MB_STATS_NAME */
3015 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3016 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3017 + if (proc_ext3_mb_stats == NULL) {
3018 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3019 + EXT3_MB_STATS_NAME);
3020 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3024 + proc_ext3_mb_stats->data = NULL;
3025 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3026 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3028 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3029 + proc_ext3_mb_max_to_scan = create_proc_entry(
3030 + EXT3_MB_MAX_TO_SCAN_NAME,
3031 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3032 + if (proc_ext3_mb_max_to_scan == NULL) {
3033 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3034 + EXT3_MB_MAX_TO_SCAN_NAME);
3035 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3036 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3040 + proc_ext3_mb_max_to_scan->data = NULL;
3041 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3042 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3044 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3045 + proc_ext3_mb_min_to_scan = create_proc_entry(
3046 + EXT3_MB_MIN_TO_SCAN_NAME,
3047 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3048 + if (proc_ext3_mb_min_to_scan == NULL) {
3049 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3050 + EXT3_MB_MIN_TO_SCAN_NAME);
3051 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3052 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3053 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3057 + proc_ext3_mb_min_to_scan->data = NULL;
3058 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3059 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3061 + /* Initialize EXT3_ORDER2_REQ */
3062 + proc_ext3_mb_order2_req = create_proc_entry(
3063 + EXT3_MB_ORDER2_REQ,
3064 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3065 + if (proc_ext3_mb_order2_req == NULL) {
3066 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3067 + EXT3_MB_ORDER2_REQ);
3068 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3069 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3070 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3071 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3075 + proc_ext3_mb_order2_req->data = NULL;
3076 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3077 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3082 +void exit_ext3_proc(void)
3084 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3085 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3086 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3087 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3088 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3090 Index: linux-2.6.12.6-bull/fs/ext3/Makefile
3091 ===================================================================
3092 --- linux-2.6.12.6-bull.orig/fs/ext3/Makefile 2006-04-29 20:39:09.000000000 +0400
3093 +++ linux-2.6.12.6-bull/fs/ext3/Makefile 2006-04-29 20:39:10.000000000 +0400
3094 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
3096 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3097 ioctl.o namei.o super.o symlink.o hash.o resize.o \
3099 + extents.o mballoc.o
3101 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3102 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o