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,7 @@ 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 void ext3_free_blocks_sb (handle_t *, struct super_block *,
50 unsigned long, unsigned long, int *);
51 extern unsigned long ext3_count_free_blocks (struct super_block *);
52 @@ -848,6 +857,17 @@ extern void ext3_extents_initialize_bloc
53 extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
54 unsigned int cmd, unsigned long arg);
57 +extern long ext3_mb_stats;
58 +extern long ext3_mb_max_to_scan;
59 +extern int ext3_mb_init(struct super_block *, int);
60 +extern int ext3_mb_release(struct super_block *);
61 +extern int ext3_mb_new_blocks(handle_t *, struct inode *, unsigned long, int *, int, int *);
62 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
63 +extern void ext3_mb_release_blocks(struct super_block *, int);
64 +int __init init_ext3_proc(void);
65 +void exit_ext3_proc(void);
67 #endif /* __KERNEL__ */
69 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
70 Index: linux-2.6.12.6-bull/include/linux/ext3_fs_sb.h
71 ===================================================================
72 --- linux-2.6.12.6-bull.orig/include/linux/ext3_fs_sb.h 2005-08-29 20:55:27.000000000 +0400
73 +++ linux-2.6.12.6-bull/include/linux/ext3_fs_sb.h 2006-04-29 20:39:10.000000000 +0400
75 #include <linux/wait.h>
76 #include <linux/blockgroup_lock.h>
77 #include <linux/percpu_counter.h>
78 +#include <linux/list.h>
80 #include <linux/rbtree.h>
81 +#include <linux/proc_fs.h>
83 +struct ext3_buddy_group_blocks;
84 +struct ext3_mb_history;
85 +#define EXT3_BB_MAX_BLOCKS
88 * third extended-fs super-block data in memory
89 @@ -78,6 +84,43 @@ struct ext3_sb_info {
90 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
91 int s_jquota_fmt; /* Format of quota to use */
94 + /* for buddy allocator */
95 + struct ext3_group_info ***s_group_info;
96 + struct inode *s_buddy_cache;
97 + long s_blocks_reserved;
98 + spinlock_t s_reserve_lock;
99 + struct list_head s_active_transaction;
100 + struct list_head s_closed_transaction;
101 + struct list_head s_committed_transaction;
102 + spinlock_t s_md_lock;
103 + tid_t s_last_transaction;
105 + unsigned short *s_mb_offsets, *s_mb_maxs;
106 + unsigned long s_stripe;
108 + /* history to debug policy */
109 + struct ext3_mb_history *s_mb_history;
110 + int s_mb_history_cur;
111 + int s_mb_history_max;
112 + struct proc_dir_entry *s_mb_proc;
113 + spinlock_t s_mb_history_lock;
115 + /* stats for buddy allocator */
116 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
117 + atomic_t s_bal_success; /* we found long enough chunks */
118 + atomic_t s_bal_allocated; /* in blocks */
119 + atomic_t s_bal_ex_scanned; /* total extents scanned */
120 + atomic_t s_bal_goals; /* goal hits */
121 + atomic_t s_bal_breaks; /* too long searches */
122 + atomic_t s_bal_2orders; /* 2^order hits */
123 + spinlock_t s_bal_lock;
124 + unsigned long s_mb_buddies_generated;
125 + unsigned long long s_mb_generation_time;
128 +#define EXT3_GROUP_INFO(sb, group) \
129 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
130 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
132 #endif /* _LINUX_EXT3_FS_SB */
133 Index: linux-2.6.12.6-bull/fs/ext3/super.c
134 ===================================================================
135 --- linux-2.6.12.6-bull.orig/fs/ext3/super.c 2006-04-29 20:39:09.000000000 +0400
136 +++ linux-2.6.12.6-bull/fs/ext3/super.c 2006-04-29 20:39:10.000000000 +0400
137 @@ -387,6 +387,7 @@ static void ext3_put_super (struct super
138 struct ext3_super_block *es = sbi->s_es;
141 + ext3_mb_release(sb);
142 ext3_ext_release(sb);
143 ext3_xattr_put_super(sb);
144 journal_destroy(sbi->s_journal);
145 @@ -597,6 +598,7 @@ enum {
146 Opt_ignore, Opt_barrier, Opt_err, Opt_resize,
147 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
148 Opt_extents, Opt_noextents, Opt_extdebug,
149 + Opt_mballoc, Opt_nomballoc, Opt_stripe,
152 static match_table_t tokens = {
153 @@ -650,6 +651,9 @@ static match_table_t tokens = {
154 {Opt_extents, "extents"},
155 {Opt_noextents, "noextents"},
156 {Opt_extdebug, "extdebug"},
157 + {Opt_mballoc, "mballoc"},
158 + {Opt_nomballoc, "nomballoc"},
159 + {Opt_stripe, "stripe=%u"},
160 {Opt_barrier, "barrier=%u"},
162 {Opt_resize, "resize"},
163 @@ -965,6 +967,19 @@ clear_qf_name:
165 set_opt (sbi->s_mount_opt, EXTDEBUG);
168 + set_opt(sbi->s_mount_opt, MBALLOC);
170 + case Opt_nomballoc:
171 + clear_opt(sbi->s_mount_opt, MBALLOC);
174 + if (match_int(&args[0], &option))
178 + sbi->s_stripe = option;
182 "EXT3-fs: Unrecognized mount option \"%s\" "
183 @@ -1670,6 +1675,7 @@ static int ext3_fill_super (struct super
184 ext3_count_dirs(sb));
187 + ext3_mb_init(sb, needs_recovery);
191 @@ -2549,7 +2555,13 @@ static struct file_system_type ext3_fs_t
193 static int __init init_ext3_fs(void)
195 - int err = init_ext3_xattr();
198 + err = init_ext3_proc();
202 + err = init_ext3_xattr();
205 err = init_inodecache();
206 @@ -2571,6 +2583,7 @@ static void __exit exit_ext3_fs(void)
207 unregister_filesystem(&ext3_fs_type);
208 destroy_inodecache();
213 int ext3_prep_san_write(struct inode *inode, long *blocks,
214 Index: linux-2.6.12.6-bull/fs/ext3/extents.c
215 ===================================================================
216 --- linux-2.6.12.6-bull.orig/fs/ext3/extents.c 2006-04-29 20:39:09.000000000 +0400
217 +++ linux-2.6.12.6-bull/fs/ext3/extents.c 2006-04-29 20:39:10.000000000 +0400
218 @@ -777,7 +777,7 @@ cleanup:
219 for (i = 0; i < depth; i++) {
222 - ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
223 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
227 @@ -1434,7 +1434,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
228 path->p_idx->ei_leaf);
229 bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
230 ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
231 - ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
232 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
236 @@ -1919,10 +1919,12 @@ ext3_remove_blocks(struct ext3_extents_t
237 int needed = ext3_remove_blocks_credits(tree, ex, from, to);
238 handle_t *handle = ext3_journal_start(tree->inode, needed);
239 struct buffer_head *bh;
241 + int i, metadata = 0;
244 return PTR_ERR(handle);
245 + if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
247 if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
249 unsigned long num, start;
250 @@ -1934,7 +1936,7 @@ ext3_remove_blocks(struct ext3_extents_t
251 bh = sb_find_get_block(tree->inode->i_sb, start + i);
252 ext3_forget(handle, 0, tree->inode, bh, start + i);
254 - ext3_free_blocks(handle, tree->inode, start, num);
255 + ext3_free_blocks(handle, tree->inode, start, num, metadata);
256 } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
257 printk("strange request: removal %lu-%lu from %u:%u\n",
258 from, to, ex->ee_block, ex->ee_len);
259 Index: linux-2.6.12.6-bull/fs/ext3/inode.c
260 ===================================================================
261 --- linux-2.6.12.6-bull.orig/fs/ext3/inode.c 2006-04-29 20:39:09.000000000 +0400
262 +++ linux-2.6.12.6-bull/fs/ext3/inode.c 2006-04-29 20:39:10.000000000 +0400
263 @@ -564,7 +564,7 @@ static int ext3_alloc_branch(handle_t *h
264 ext3_journal_forget(handle, branch[i].bh);
266 for (i = 0; i < keys; i++)
267 - ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1);
268 + ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1, 1);
272 @@ -1850,7 +1850,7 @@ ext3_clear_blocks(handle_t *handle, stru
276 - ext3_free_blocks(handle, inode, block_to_free, count);
277 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
281 @@ -2023,7 +2023,7 @@ static void ext3_free_branches(handle_t
282 ext3_journal_test_restart(handle, inode);
285 - ext3_free_blocks(handle, inode, nr, 1);
286 + ext3_free_blocks(handle, inode, nr, 1, 1);
290 Index: linux-2.6.12.6-bull/fs/ext3/balloc.c
291 ===================================================================
292 --- linux-2.6.12.6-bull.orig/fs/ext3/balloc.c 2005-08-29 20:55:27.000000000 +0400
293 +++ linux-2.6.12.6-bull/fs/ext3/balloc.c 2006-04-29 20:39:10.000000000 +0400
294 @@ -79,7 +79,7 @@ struct ext3_group_desc * ext3_get_group_
296 * Return buffer_head on success or NULL in case of failure.
298 -static struct buffer_head *
299 +struct buffer_head *
300 read_block_bitmap(struct super_block *sb, unsigned int block_group)
302 struct ext3_group_desc * desc;
303 @@ -490,24 +490,6 @@ error_return:
307 -/* Free given blocks, update quota and i_blocks field */
308 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
309 - unsigned long block, unsigned long count)
311 - struct super_block * sb;
312 - int dquot_freed_blocks;
316 - printk ("ext3_free_blocks: nonexistent device");
319 - ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
320 - if (dquot_freed_blocks)
321 - DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
326 * For ext3 allocations, we must not reuse any blocks which are
327 * allocated in the bitmap buffer's "last committed data" copy. This
328 @@ -1162,7 +1144,7 @@ int ext3_should_retry_alloc(struct super
329 * bitmap, and then for any free bit if that fails.
330 * This function also updates quota and i_blocks field.
332 -int ext3_new_block(handle_t *handle, struct inode *inode,
333 +int ext3_new_block_old(handle_t *handle, struct inode *inode,
334 unsigned long goal, int *errp)
336 struct buffer_head *bitmap_bh = NULL;
337 Index: linux-2.6.12.6-bull/fs/ext3/xattr.c
338 ===================================================================
339 --- linux-2.6.12.6-bull.orig/fs/ext3/xattr.c 2005-08-29 20:55:27.000000000 +0400
340 +++ linux-2.6.12.6-bull/fs/ext3/xattr.c 2006-04-29 20:39:10.000000000 +0400
341 @@ -484,7 +484,7 @@ ext3_xattr_release_block(handle_t *handl
342 ea_bdebug(bh, "refcount now=0; freeing");
344 mb_cache_entry_free(ce);
345 - ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
346 + ext3_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
348 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
350 @@ -804,7 +804,7 @@ inserted:
351 new_bh = sb_getblk(sb, block);
354 - ext3_free_blocks(handle, inode, block, 1);
355 + ext3_free_blocks(handle, inode, block, 1, 1);
359 Index: linux-2.6.12.6-bull/fs/ext3/mballoc.c
360 ===================================================================
361 --- linux-2.6.12.6-bull.orig/fs/ext3/mballoc.c 2006-04-22 17:31:47.543334750 +0400
362 +++ linux-2.6.12.6-bull/fs/ext3/mballoc.c 2006-04-30 01:24:11.000000000 +0400
365 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
366 + * Written by Alex Tomas <alex@clusterfs.com>
368 + * This program is free software; you can redistribute it and/or modify
369 + * it under the terms of the GNU General Public License version 2 as
370 + * published by the Free Software Foundation.
372 + * This program is distributed in the hope that it will be useful,
373 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
374 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
375 + * GNU General Public License for more details.
377 + * You should have received a copy of the GNU General Public Licens
378 + * along with this program; if not, write to the Free Software
379 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
384 + * mballoc.c contains the multiblocks allocation routines
387 +#include <linux/config.h>
388 +#include <linux/time.h>
389 +#include <linux/fs.h>
390 +#include <linux/namei.h>
391 +#include <linux/jbd.h>
392 +#include <linux/ext3_fs.h>
393 +#include <linux/ext3_jbd.h>
394 +#include <linux/quotaops.h>
395 +#include <linux/buffer_head.h>
396 +#include <linux/module.h>
397 +#include <linux/swap.h>
398 +#include <linux/proc_fs.h>
399 +#include <linux/pagemap.h>
400 +#include <linux/seq_file.h>
404 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
405 + * - track min/max extents in each group for better group selection
406 + * - mb_mark_used() may allocate chunk right after splitting buddy
407 + * - special flag to advice allocator to look for requested + N blocks
408 + * this may improve interaction between extents and mballoc
409 + * - tree of groups sorted by number of free blocks
410 + * - percpu reservation code (hotpath)
415 + * with AGRESSIVE_CHECK allocator runs consistency checks over
416 + * structures. these checks slow things down a lot
418 +#define AGGRESSIVE_CHECK__
424 +#define mb_debug(fmt,a...) printk(fmt, ##a)
426 +#define mb_debug(fmt,a...)
430 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
431 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
433 +#define EXT3_MB_HISTORY
436 + * How long mballoc can look for a best extent (in found extents)
438 +long ext3_mb_max_to_scan = 500;
441 + * How long mballoc must look for a best extent
443 +long ext3_mb_min_to_scan = 30;
446 + * with 'ext3_mb_stats' allocator will collect stats that will be
447 + * shown at umount. The collecting costs though!
450 +long ext3_mb_stats = 1;
453 + * for which requests use 2^N search using buddies
455 +long ext3_mb_order2_reqs = 8;
457 +#ifdef EXT3_BB_MAX_BLOCKS
458 +#undef EXT3_BB_MAX_BLOCKS
460 +#define EXT3_BB_MAX_BLOCKS 30
462 +struct ext3_free_metadata {
463 + unsigned short group;
464 + unsigned short num;
465 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
466 + struct list_head list;
469 +struct ext3_group_info {
470 + unsigned long bb_state;
471 + unsigned long bb_tid;
472 + struct ext3_free_metadata *bb_md_cur;
473 + unsigned short bb_first_free;
474 + unsigned short bb_free;
475 + unsigned short bb_fragments;
476 + unsigned short bb_counters[];
480 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
481 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
483 +#define EXT3_MB_GRP_NEED_INIT(grp) \
484 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
486 +struct ext3_free_extent {
492 +struct ext3_allocation_context {
493 + struct super_block *ac_sb;
496 + struct ext3_free_extent ac_g_ex;
498 + /* the best found extent */
499 + struct ext3_free_extent ac_b_ex;
501 + /* number of iterations done. we have to track to limit searching */
502 + unsigned long ac_ex_scanned;
503 + __u16 ac_groups_scanned;
508 + __u8 ac_flags; /* allocation hints */
511 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
512 + * N > 0, the field stores N, otherwise 0 */
514 + struct page *ac_buddy_page;
515 + struct page *ac_bitmap_page;
518 +#define AC_STATUS_CONTINUE 1
519 +#define AC_STATUS_FOUND 2
520 +#define AC_STATUS_BREAK 3
522 +struct ext3_mb_history {
523 + struct ext3_free_extent goal; /* goal allocation */
524 + struct ext3_free_extent result; /* result allocation */
527 + __u16 found; /* how many extents have been found */
528 + __u16 groups; /* how many groups have been scanned */
529 + __u16 tail; /* what tail broke some buddy */
530 + __u16 buddy; /* buddy the tail ^^^ broke */
531 + __u8 cr; /* which phase the result extent was found at */
536 + struct page *bd_buddy_page;
538 + struct page *bd_bitmap_page;
540 + struct ext3_group_info *bd_info;
541 + struct super_block *bd_sb;
545 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
546 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
548 +#ifndef EXT3_MB_HISTORY
549 +#define ext3_mb_store_history(sb,ino,ac)
551 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
552 + struct ext3_allocation_context *ac);
555 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
557 +static struct proc_dir_entry *proc_root_ext3;
559 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
560 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
561 +int ext3_new_block_old(handle_t *, struct inode *, unsigned long, int *);
562 +int ext3_mb_reserve_blocks(struct super_block *, int);
563 +void ext3_mb_release_blocks(struct super_block *, int);
564 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
565 +void ext3_mb_free_committed_blocks(struct super_block *);
567 +#if BITS_PER_LONG == 64
568 +#define mb_correct_addr_and_bit(bit,addr) \
570 + bit += ((unsigned long) addr & 7UL) << 3; \
571 + addr = (void *) ((unsigned long) addr & ~7UL); \
573 +#elif BITS_PER_LONG == 32
574 +#define mb_correct_addr_and_bit(bit,addr) \
576 + bit += ((unsigned long) addr & 3UL) << 3; \
577 + addr = (void *) ((unsigned long) addr & ~3UL); \
580 +#error "how many bits you are?!"
583 +static inline int mb_test_bit(int bit, void *addr)
585 + mb_correct_addr_and_bit(bit,addr);
586 + return ext2_test_bit(bit, addr);
589 +static inline void mb_set_bit(int bit, void *addr)
591 + mb_correct_addr_and_bit(bit,addr);
592 + ext2_set_bit(bit, addr);
595 +static inline void mb_set_bit_atomic(int bit, void *addr)
597 + mb_correct_addr_and_bit(bit,addr);
598 + ext2_set_bit_atomic(NULL, bit, addr);
601 +static inline void mb_clear_bit(int bit, void *addr)
603 + mb_correct_addr_and_bit(bit,addr);
604 + ext2_clear_bit(bit, addr);
607 +static inline void mb_clear_bit_atomic(int bit, void *addr)
609 + mb_correct_addr_and_bit(bit,addr);
610 + ext2_clear_bit_atomic(NULL, bit, addr);
613 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
616 +#if BITS_PER_LONG == 64
617 + fix = ((unsigned long) addr & 7UL) << 3;
618 + addr = (void *) ((unsigned long) addr & ~7UL);
619 +#elif BITS_PER_LONG == 32
620 + fix = ((unsigned long) addr & 3UL) << 3;
621 + addr = (void *) ((unsigned long) addr & ~3UL);
623 +#error "how many bits you are?!"
627 + return ext2_find_next_zero_bit(addr, max, start) - fix;
630 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
634 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
635 + J_ASSERT(max != NULL);
637 + if (order > e3b->bd_blkbits + 1) {
642 + /* at order 0 we see each particular block */
643 + *max = 1 << (e3b->bd_blkbits + 3);
645 + return EXT3_MB_BITMAP(e3b);
647 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
648 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
653 +#ifdef AGGRESSIVE_CHECK
655 +static void mb_check_buddy(struct ext3_buddy *e3b)
657 + int order = e3b->bd_blkbits + 1;
658 + int max, max2, i, j, k, count;
659 + int fragments = 0, fstart;
660 + void *buddy, *buddy2;
662 + if (!test_opt(e3b->bd_sb, MBALLOC))
666 + static int mb_check_counter = 0;
667 + if (mb_check_counter++ % 300 != 0)
671 + while (order > 1) {
672 + buddy = mb_find_buddy(e3b, order, &max);
674 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
676 + J_ASSERT(buddy != buddy2);
677 + J_ASSERT(max * 2 == max2);
680 + for (i = 0; i < max; i++) {
682 + if (mb_test_bit(i, buddy)) {
683 + /* only single bit in buddy2 may be 1 */
684 + if (!mb_test_bit(i << 1, buddy2))
685 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
686 + else if (!mb_test_bit((i << 1) + 1, buddy2))
687 + J_ASSERT(mb_test_bit(i << 1, buddy2));
691 + /* both bits in buddy2 must be 0 */
692 + J_ASSERT(mb_test_bit(i << 1, buddy2));
693 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
695 + for (j = 0; j < (1 << order); j++) {
696 + k = (i * (1 << order)) + j;
697 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
701 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
706 + buddy = mb_find_buddy(e3b, 0, &max);
707 + for (i = 0; i < max; i++) {
708 + if (!mb_test_bit(i, buddy)) {
709 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
710 + if (fstart == -1) {
717 + /* check used bits only */
718 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
719 + buddy2 = mb_find_buddy(e3b, j, &max2);
721 + J_ASSERT(k < max2);
722 + J_ASSERT(mb_test_bit(k, buddy2));
725 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
726 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
730 +#define mb_check_buddy(e3b)
733 +/* find most significant bit */
734 +static int inline fmsb(unsigned short word)
748 + } while (word != 0);
754 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
755 + int len, struct ext3_group_info *grp)
757 + struct ext3_sb_info *sbi = EXT3_SB(sb);
758 + unsigned short min, max, chunk, border;
760 + mb_debug("mark %u/%u free\n", first, len);
761 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
763 + border = 2 << sb->s_blocksize_bits;
766 + /* find how many blocks can be covered since this position */
767 + max = ffs(first | border) - 1;
769 + /* find how many blocks of power 2 we need to mark */
772 + mb_debug(" %u/%u -> max %u, min %u\n",
773 + first & ((2 << sb->s_blocksize_bits) - 1),
780 + /* mark multiblock chunks only */
781 + grp->bb_counters[min]++;
783 + mb_debug(" set %u at %u \n", first >> min,
784 + sbi->s_mb_offsets[min]);
785 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
794 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
797 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
798 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
799 + unsigned short i = 0, first, len;
800 + unsigned free = 0, fragments = 0;
801 + unsigned long long period = get_cycles();
803 + i = mb_find_next_zero_bit(bitmap, max, 0);
804 + grp->bb_first_free = i;
808 + i = ext2_find_next_le_bit(bitmap, max, i);
812 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
814 + grp->bb_counters[0]++;
816 + i = mb_find_next_zero_bit(bitmap, max, i);
818 + grp->bb_fragments = fragments;
820 + /* bb_state shouldn't being modified because all
821 + * others waits for init completion on page lock */
822 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
823 + if (free != grp->bb_free) {
824 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
825 + group, free, grp->bb_free);
826 + grp->bb_free = free;
829 + period = get_cycles() - period;
830 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
831 + EXT3_SB(sb)->s_mb_buddies_generated++;
832 + EXT3_SB(sb)->s_mb_generation_time += period;
833 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
836 +static int ext3_mb_init_cache(struct page *page)
838 + int blocksize, blocks_per_page, groups_per_page;
839 + int err = 0, i, first_group, first_block;
840 + struct super_block *sb;
841 + struct buffer_head *bhs;
842 + struct buffer_head **bh;
843 + struct inode *inode;
844 + char *data, *bitmap;
846 + mb_debug("init page %lu\n", page->index);
848 + inode = page->mapping->host;
850 + blocksize = 1 << inode->i_blkbits;
851 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
853 + groups_per_page = blocks_per_page >> 1;
854 + if (groups_per_page == 0)
855 + groups_per_page = 1;
857 + /* allocate buffer_heads to read bitmaps */
858 + if (groups_per_page > 1) {
860 + i = sizeof(struct buffer_head *) * groups_per_page;
861 + bh = kmalloc(i, GFP_NOFS);
868 + first_group = page->index * blocks_per_page / 2;
870 + /* read all groups the page covers into the cache */
871 + for (i = 0; i < groups_per_page; i++) {
872 + struct ext3_group_desc * desc;
874 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
878 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
883 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
887 + if (buffer_uptodate(bh[i]))
890 + lock_buffer(bh[i]);
891 + if (buffer_uptodate(bh[i])) {
892 + unlock_buffer(bh[i]);
897 + bh[i]->b_end_io = end_buffer_read_sync;
898 + submit_bh(READ, bh[i]);
899 + mb_debug("read bitmap for group %u\n", first_group + i);
902 + /* wait for I/O completion */
903 + for (i = 0; i < groups_per_page && bh[i]; i++)
904 + wait_on_buffer(bh[i]);
907 + for (i = 0; i < groups_per_page && bh[i]; i++)
908 + if (!buffer_uptodate(bh[i]))
911 + first_block = page->index * blocks_per_page;
912 + for (i = 0; i < blocks_per_page; i++) {
915 + group = (first_block + i) >> 1;
916 + if (group >= EXT3_SB(sb)->s_groups_count)
919 + data = page_address(page) + (i * blocksize);
920 + bitmap = bh[group - first_group]->b_data;
922 + if ((first_block + i) & 1) {
923 + /* this is block of buddy */
924 + mb_debug("put buddy for group %u in page %lu/%x\n",
925 + group, page->index, i * blocksize);
926 + memset(data, 0xff, blocksize);
927 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
928 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
929 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
930 + ext3_mb_generate_buddy(sb, data, bitmap, group);
932 + /* this is block of bitmap */
933 + mb_debug("put bitmap for group %u in page %lu/%x\n",
934 + group, page->index, i * blocksize);
935 + memcpy(data, bitmap, blocksize);
938 + SetPageUptodate(page);
942 + for (i = 0; i < groups_per_page && bh[i]; i++)
950 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
951 + struct ext3_buddy *e3b)
953 + struct ext3_sb_info *sbi = EXT3_SB(sb);
954 + struct inode *inode = sbi->s_buddy_cache;
955 + int blocks_per_page, block, pnum, poff;
958 + mb_debug("load group %u\n", group);
960 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
962 + e3b->bd_blkbits = sb->s_blocksize_bits;
963 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
965 + e3b->bd_group = group;
966 + e3b->bd_buddy_page = NULL;
967 + e3b->bd_bitmap_page = NULL;
970 + pnum = block / blocks_per_page;
971 + poff = block % blocks_per_page;
973 + /* we could use find_or_create_page(), but it locks page
974 + * what we'd like to avoid in fast path ... */
975 + page = find_get_page(inode->i_mapping, pnum);
976 + if (page == NULL || !PageUptodate(page)) {
978 + page_cache_release(page);
979 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
981 + BUG_ON(page->mapping != inode->i_mapping);
982 + if (!PageUptodate(page))
983 + ext3_mb_init_cache(page);
987 + if (page == NULL || !PageUptodate(page))
989 + e3b->bd_bitmap_page = page;
990 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
991 + mark_page_accessed(page);
994 + pnum = block / blocks_per_page;
995 + poff = block % blocks_per_page;
997 + page = find_get_page(inode->i_mapping, pnum);
998 + if (page == NULL || !PageUptodate(page)) {
1000 + page_cache_release(page);
1001 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1003 + BUG_ON(page->mapping != inode->i_mapping);
1004 + if (!PageUptodate(page))
1005 + ext3_mb_init_cache(page);
1006 + unlock_page(page);
1009 + if (page == NULL || !PageUptodate(page))
1011 + e3b->bd_buddy_page = page;
1012 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1013 + mark_page_accessed(page);
1015 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1016 + J_ASSERT(e3b->bd_buddy_page != NULL);
1021 + if (e3b->bd_bitmap_page)
1022 + page_cache_release(e3b->bd_bitmap_page);
1023 + if (e3b->bd_buddy_page)
1024 + page_cache_release(e3b->bd_buddy_page);
1025 + e3b->bd_buddy = NULL;
1026 + e3b->bd_bitmap = NULL;
1030 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1032 + if (e3b->bd_bitmap_page)
1033 + page_cache_release(e3b->bd_bitmap_page);
1034 + if (e3b->bd_buddy_page)
1035 + page_cache_release(e3b->bd_buddy_page);
1040 +ext3_lock_group(struct super_block *sb, int group)
1042 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1043 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1047 +ext3_unlock_group(struct super_block *sb, int group)
1049 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1050 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1053 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1058 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1059 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1061 + bb = EXT3_MB_BUDDY(e3b);
1062 + while (order <= e3b->bd_blkbits + 1) {
1063 + block = block >> 1;
1064 + if (!mb_test_bit(block, bb)) {
1065 + /* this block is part of buddy of order 'order' */
1068 + bb += 1 << (e3b->bd_blkbits - order);
1074 +static inline void mb_clear_bits(void *bm, int cur, int len)
1079 + while (cur < len) {
1080 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1081 + /* fast path: clear whole word at once */
1082 + addr = bm + (cur >> 3);
1087 + mb_clear_bit_atomic(cur, bm);
1092 +static inline void mb_set_bits(void *bm, int cur, int len)
1097 + while (cur < len) {
1098 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1099 + /* fast path: clear whole word at once */
1100 + addr = bm + (cur >> 3);
1101 + *addr = 0xffffffff;
1105 + mb_set_bit_atomic(cur, bm);
1110 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1112 + int block = 0, max = 0, order;
1113 + void *buddy, *buddy2;
1115 + mb_check_buddy(e3b);
1117 + e3b->bd_info->bb_free += count;
1118 + if (first < e3b->bd_info->bb_first_free)
1119 + e3b->bd_info->bb_first_free = first;
1121 + /* let's maintain fragments counter */
1123 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1124 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1125 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1127 + e3b->bd_info->bb_fragments--;
1128 + else if (!block && !max)
1129 + e3b->bd_info->bb_fragments++;
1131 + /* let's maintain buddy itself */
1132 + while (count-- > 0) {
1136 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1137 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1138 + e3b->bd_info->bb_counters[order]++;
1140 + /* start of the buddy */
1141 + buddy = mb_find_buddy(e3b, order, &max);
1145 + if (mb_test_bit(block, buddy) ||
1146 + mb_test_bit(block + 1, buddy))
1149 + /* both the buddies are free, try to coalesce them */
1150 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1156 + /* for special purposes, we don't set
1157 + * free bits in bitmap */
1158 + mb_set_bit(block, buddy);
1159 + mb_set_bit(block + 1, buddy);
1161 + e3b->bd_info->bb_counters[order]--;
1162 + e3b->bd_info->bb_counters[order]--;
1164 + block = block >> 1;
1166 + e3b->bd_info->bb_counters[order]++;
1168 + mb_clear_bit(block, buddy2);
1172 + mb_check_buddy(e3b);
1177 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1178 + int needed, struct ext3_free_extent *ex)
1180 + int next = block, max, ord;
1183 + J_ASSERT(ex != NULL);
1185 + buddy = mb_find_buddy(e3b, order, &max);
1187 + J_ASSERT(block < max);
1188 + if (mb_test_bit(block, buddy)) {
1195 + if (likely(order == 0)) {
1196 + /* find actual order */
1197 + order = mb_find_order_for_block(e3b, block);
1198 + block = block >> order;
1201 + ex->fe_len = 1 << order;
1202 + ex->fe_start = block << order;
1203 + ex->fe_group = e3b->bd_group;
1205 + /* calc difference from given start */
1206 + next = next - ex->fe_start;
1207 + ex->fe_len -= next;
1208 + ex->fe_start += next;
1210 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1212 + if (block + 1 >= max)
1215 + next = (block + 1) * (1 << order);
1216 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1219 + ord = mb_find_order_for_block(e3b, next);
1222 + block = next >> order;
1223 + ex->fe_len += 1 << order;
1226 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1227 + return ex->fe_len;
1230 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1232 + int ord, mlen = 0, max = 0, cur;
1233 + int start = ex->fe_start;
1234 + int len = ex->fe_len;
1239 + mb_check_buddy(e3b);
1241 + e3b->bd_info->bb_free -= len;
1242 + if (e3b->bd_info->bb_first_free == start)
1243 + e3b->bd_info->bb_first_free += len;
1245 + /* let's maintain fragments counter */
1247 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1248 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1249 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1251 + e3b->bd_info->bb_fragments++;
1252 + else if (!mlen && !max)
1253 + e3b->bd_info->bb_fragments--;
1255 + /* let's maintain buddy itself */
1257 + ord = mb_find_order_for_block(e3b, start);
1259 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1260 + /* the whole chunk may be allocated at once! */
1262 + buddy = mb_find_buddy(e3b, ord, &max);
1263 + J_ASSERT((start >> ord) < max);
1264 + mb_set_bit(start >> ord, buddy);
1265 + e3b->bd_info->bb_counters[ord]--;
1268 + J_ASSERT(len >= 0);
1272 + /* store for history */
1274 + ret = len | (ord << 16);
1276 + /* we have to split large buddy */
1277 + J_ASSERT(ord > 0);
1278 + buddy = mb_find_buddy(e3b, ord, &max);
1279 + mb_set_bit(start >> ord, buddy);
1280 + e3b->bd_info->bb_counters[ord]--;
1283 + cur = (start >> ord) & ~1U;
1284 + buddy = mb_find_buddy(e3b, ord, &max);
1285 + mb_clear_bit(cur, buddy);
1286 + mb_clear_bit(cur + 1, buddy);
1287 + e3b->bd_info->bb_counters[ord]++;
1288 + e3b->bd_info->bb_counters[ord]++;
1291 + /* now drop all the bits in bitmap */
1292 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1294 + mb_check_buddy(e3b);
1300 + * Must be called under group lock!
1302 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1303 + struct ext3_buddy *e3b)
1305 + unsigned long ret;
1307 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1308 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1310 + ac->ac_status = AC_STATUS_FOUND;
1311 + ac->ac_tail = ret & 0xffff;
1312 + ac->ac_buddy = ret >> 16;
1314 + /* hold in-core structures until allocated
1315 + * blocks are marked non-free in on-disk bitmap */
1316 + ac->ac_buddy_page = e3b->bd_buddy_page;
1317 + page_cache_get(e3b->bd_buddy_page);
1318 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1319 + page_cache_get(e3b->bd_bitmap_page);
1323 + * The routine checks whether found extent is good enough. If it is,
1324 + * then the extent gets marked used and flag is set to the context
1325 + * to stop scanning. Otherwise, the extent is compared with the
1326 + * previous found extent and if new one is better, then it's stored
1327 + * in the context. Later, the best found extent will be used, if
1328 + * mballoc can't find good enough extent.
1330 + * FIXME: real allocation policy is to be designed yet!
1332 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1333 + struct ext3_free_extent *ex,
1334 + struct ext3_buddy *e3b)
1336 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1337 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1339 + J_ASSERT(ex->fe_len > 0);
1340 + J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1341 + J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1346 + * The special case - take what you catch first
1348 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1350 + ext3_mb_use_best_found(ac, e3b);
1355 + * Let's check whether the chunk is good enough
1357 + if (ex->fe_len == gex->fe_len) {
1359 + ext3_mb_use_best_found(ac, e3b);
1364 + * If this is first found extent, just store it in the context
1366 + if (bex->fe_len == 0) {
1372 + * If new found extent is better, store it in the context
1374 + if (bex->fe_len < gex->fe_len) {
1375 + /* if the request isn't satisfied, any found extent
1376 + * larger than previous best one is better */
1377 + if (ex->fe_len > bex->fe_len)
1379 + } else if (ex->fe_len > gex->fe_len) {
1380 + /* if the request is satisfied, then we try to find
1381 + * an extent that still satisfy the request, but is
1382 + * smaller than previous one */
1387 + * Let's scan at least few extents and don't pick up a first one
1389 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1390 + ac->ac_status = AC_STATUS_BREAK;
1393 + * We don't want to scan for a whole year
1395 + if (ac->ac_found > ext3_mb_max_to_scan)
1396 + ac->ac_status = AC_STATUS_BREAK;
1399 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1400 + struct ext3_buddy *e3b)
1402 + struct ext3_free_extent ex = ac->ac_b_ex;
1403 + int group = ex.fe_group, max, err;
1405 + J_ASSERT(ex.fe_len > 0);
1406 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1410 + ext3_lock_group(ac->ac_sb, group);
1411 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1415 + ext3_mb_use_best_found(ac, e3b);
1418 + ext3_unlock_group(ac->ac_sb, group);
1420 + ext3_mb_release_desc(e3b);
1425 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1426 + struct ext3_buddy *e3b)
1428 + int group = ac->ac_g_ex.fe_group, max, err;
1429 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1430 + struct ext3_super_block *es = sbi->s_es;
1431 + struct ext3_free_extent ex;
1433 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1437 + ext3_lock_group(ac->ac_sb, group);
1438 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1439 + ac->ac_g_ex.fe_len, &ex);
1441 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1442 + unsigned long start;
1443 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1444 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1445 + if (start % sbi->s_stripe == 0) {
1448 + ext3_mb_use_best_found(ac, e3b);
1450 + } else if (max >= ac->ac_g_ex.fe_len) {
1451 + J_ASSERT(ex.fe_len > 0);
1452 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1453 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1456 + ext3_mb_use_best_found(ac, e3b);
1457 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1458 + /* Sometimes, caller may want to merge even small
1459 + * number of blocks to an existing extent */
1460 + J_ASSERT(ex.fe_len > 0);
1461 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1462 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1465 + ext3_mb_use_best_found(ac, e3b);
1467 + ext3_unlock_group(ac->ac_sb, group);
1469 + ext3_mb_release_desc(e3b);
1475 + * The routine scans buddy structures (not bitmap!) from given order
1476 + * to max order and tries to find big enough chunk to satisfy the req
1478 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1479 + struct ext3_buddy *e3b)
1481 + struct super_block *sb = ac->ac_sb;
1482 + struct ext3_group_info *grp = e3b->bd_info;
1486 + J_ASSERT(ac->ac_2order > 0);
1487 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1488 + if (grp->bb_counters[i] == 0)
1491 + buddy = mb_find_buddy(e3b, i, &max);
1492 + if (buddy == NULL) {
1493 + printk(KERN_ALERT "looking for wrong order?\n");
1497 + k = mb_find_next_zero_bit(buddy, max, 0);
1498 + J_ASSERT(k < max);
1502 + ac->ac_b_ex.fe_len = 1 << i;
1503 + ac->ac_b_ex.fe_start = k << i;
1504 + ac->ac_b_ex.fe_group = e3b->bd_group;
1506 + ext3_mb_use_best_found(ac, e3b);
1507 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1509 + if (unlikely(ext3_mb_stats))
1510 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1517 + * The routine scans the group and measures all found extents.
1518 + * In order to optimize scanning, caller must pass number of
1519 + * free blocks in the group, so the routine can know upper limit.
1521 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1522 + struct ext3_buddy *e3b)
1524 + struct super_block *sb = ac->ac_sb;
1525 + void *bitmap = EXT3_MB_BITMAP(e3b);
1526 + struct ext3_free_extent ex;
1529 + free = e3b->bd_info->bb_free;
1530 + J_ASSERT(free > 0);
1532 + i = e3b->bd_info->bb_first_free;
1534 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1535 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1536 + if (i >= sb->s_blocksize * 8) {
1537 + J_ASSERT(free == 0);
1541 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1542 + J_ASSERT(ex.fe_len > 0);
1543 + J_ASSERT(free >= ex.fe_len);
1545 + ext3_mb_measure_extent(ac, &ex, e3b);
1548 + free -= ex.fe_len;
1553 + * This is a special case for storages like raid5
1554 + * we try to find stripe-aligned chunks for stripe-size requests
1556 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1557 + struct ext3_buddy *e3b)
1559 + struct super_block *sb = ac->ac_sb;
1560 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1561 + void *bitmap = EXT3_MB_BITMAP(e3b);
1562 + struct ext3_free_extent ex;
1563 + unsigned long i, max;
1565 + J_ASSERT(sbi->s_stripe != 0);
1567 + /* find first stripe-aligned block */
1568 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb) +
1569 + le32_to_cpu(sbi->s_es->s_first_data_block);
1570 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1571 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block)) %
1572 + EXT3_BLOCKS_PER_GROUP(sb);
1574 + while (i < sb->s_blocksize * 8) {
1575 + if (!mb_test_bit(i, bitmap)) {
1576 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1577 + if (max >= sbi->s_stripe) {
1580 + ext3_mb_use_best_found(ac, e3b);
1584 + i += sbi->s_stripe;
1588 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1589 + int group, int cr)
1591 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1592 + unsigned free, fragments, i, bits;
1594 + J_ASSERT(cr >= 0 && cr < 4);
1595 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1597 + free = grp->bb_free;
1598 + fragments = grp->bb_fragments;
1601 + if (fragments == 0)
1606 + J_ASSERT(ac->ac_2order != 0);
1607 + bits = ac->ac_sb->s_blocksize_bits + 1;
1608 + for (i = ac->ac_2order; i <= bits; i++)
1609 + if (grp->bb_counters[i] > 0)
1613 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1617 + if (free >= ac->ac_g_ex.fe_len)
1629 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1630 + unsigned long goal, int *len, int flags, int *errp)
1632 + struct buffer_head *bitmap_bh = NULL;
1633 + struct ext3_allocation_context ac;
1634 + int i, group, block, cr, err = 0;
1635 + struct ext3_group_desc *gdp;
1636 + struct ext3_super_block *es;
1637 + struct buffer_head *gdp_bh;
1638 + struct ext3_sb_info *sbi;
1639 + struct super_block *sb;
1640 + struct ext3_buddy e3b;
1642 + J_ASSERT(len != NULL);
1643 + J_ASSERT(*len > 0);
1647 + printk("ext3_mb_new_nblocks: nonexistent device");
1651 + if (!test_opt(sb, MBALLOC)) {
1652 + static int ext3_mballoc_warning = 0;
1653 + if (ext3_mballoc_warning == 0) {
1654 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1655 + "mballoc disabled!\n");
1656 + ext3_mballoc_warning++;
1659 + err = ext3_new_block_old(handle, inode, goal, errp);
1663 + ext3_mb_poll_new_transaction(sb, handle);
1665 + sbi = EXT3_SB(sb);
1666 + es = EXT3_SB(sb)->s_es;
1669 + * We can't allocate > group size
1671 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1672 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1674 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1675 + /* someone asks for non-reserved blocks */
1677 + err = ext3_mb_reserve_blocks(sb, 1);
1684 + ac.ac_buddy_page = NULL;
1685 + ac.ac_bitmap_page = NULL;
1688 + * Check quota for allocation of this blocks.
1690 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1698 + /* start searching from the goal */
1699 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1700 + goal >= le32_to_cpu(es->s_blocks_count))
1701 + goal = le32_to_cpu(es->s_first_data_block);
1702 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1703 + EXT3_BLOCKS_PER_GROUP(sb);
1704 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1705 + EXT3_BLOCKS_PER_GROUP(sb));
1707 + /* set up allocation goals */
1708 + ac.ac_b_ex.fe_group = 0;
1709 + ac.ac_b_ex.fe_start = 0;
1710 + ac.ac_b_ex.fe_len = 0;
1711 + ac.ac_status = AC_STATUS_CONTINUE;
1712 + ac.ac_groups_scanned = 0;
1713 + ac.ac_ex_scanned = 0;
1715 + ac.ac_sb = inode->i_sb;
1716 + ac.ac_g_ex.fe_group = group;
1717 + ac.ac_g_ex.fe_start = block;
1718 + ac.ac_g_ex.fe_len = *len;
1719 + ac.ac_flags = flags;
1721 + ac.ac_criteria = 0;
1723 + if (*len == 1 && sbi->s_stripe) {
1724 + /* looks like a metadata, let's use a dirty hack for raid5
1725 + * move all metadata in first groups in hope to hit cached
1726 + * sectors and thus avoid read-modify cycles in raid5 */
1727 + ac.ac_g_ex.fe_group = group = 0;
1730 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1732 + if (i >= ext3_mb_order2_reqs) {
1734 + if ((*len & (~(1 << i))) == 0)
1738 + /* first, try the goal */
1739 + err = ext3_mb_find_by_goal(&ac, &e3b);
1742 + if (ac.ac_status == AC_STATUS_FOUND)
1745 + /* Let's just scan groups to find more-less suitable blocks */
1746 + cr = ac.ac_2order ? 0 : 1;
1748 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1749 + ac.ac_criteria = cr;
1750 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1751 + if (group == EXT3_SB(sb)->s_groups_count)
1754 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1755 + /* we need full data about the group
1756 + * to make a good selection */
1757 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1760 + ext3_mb_release_desc(&e3b);
1763 + /* check is group good for our criteries */
1764 + if (!ext3_mb_good_group(&ac, group, cr))
1767 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1771 + ext3_lock_group(sb, group);
1772 + if (!ext3_mb_good_group(&ac, group, cr)) {
1773 + /* someone did allocation from this group */
1774 + ext3_unlock_group(sb, group);
1775 + ext3_mb_release_desc(&e3b);
1779 + ac.ac_groups_scanned++;
1781 + ext3_mb_simple_scan_group(&ac, &e3b);
1782 + else if (cr == 1 && *len == sbi->s_stripe)
1783 + ext3_mb_scan_aligned(&ac, &e3b);
1785 + ext3_mb_complex_scan_group(&ac, &e3b);
1787 + ext3_unlock_group(sb, group);
1789 + ext3_mb_release_desc(&e3b);
1791 + if (ac.ac_status != AC_STATUS_CONTINUE)
1796 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1797 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1799 + * We've been searching too long. Let's try to allocate
1800 + * the best chunk we've found so far
1803 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1804 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1805 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1806 + ac.ac_g_ex.fe_len);*/
1807 + ext3_mb_try_best_found(&ac, &e3b);
1808 + if (ac.ac_status != AC_STATUS_FOUND) {
1810 + * Someone more lucky has already allocated it.
1811 + * The only thing we can do is just take first
1813 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1815 + ac.ac_b_ex.fe_group = 0;
1816 + ac.ac_b_ex.fe_start = 0;
1817 + ac.ac_b_ex.fe_len = 0;
1818 + ac.ac_status = AC_STATUS_CONTINUE;
1819 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1825 + if (ac.ac_status != AC_STATUS_FOUND) {
1827 + * We aren't lucky definitely
1829 + DQUOT_FREE_BLOCK(inode, *len);
1833 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1834 + ac.ac_status, ac.ac_flags);
1835 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1836 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1837 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1838 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1839 + sbi->s_blocks_reserved, ac.ac_found);
1840 + printk("EXT3-fs: groups: ");
1841 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1842 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1849 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1851 + /* good news - free block(s) have been found. now it's time
1852 + * to mark block(s) in good old journaled bitmap */
1853 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1854 + + ac.ac_b_ex.fe_start
1855 + + le32_to_cpu(es->s_first_data_block);
1857 + /* we made a desicion, now mark found blocks in good old
1858 + * bitmap to be journaled */
1860 + ext3_debug("using block group %d(%d)\n",
1861 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1863 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1869 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1875 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1881 + err = ext3_journal_get_write_access(handle, gdp_bh);
1885 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1886 + + ac.ac_b_ex.fe_start
1887 + + le32_to_cpu(es->s_first_data_block);
1889 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1890 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1891 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1892 + EXT3_SB(sb)->s_itb_per_group))
1893 + ext3_error(sb, "ext3_new_block",
1894 + "Allocating block in system zone - "
1895 + "block = %u", block);
1896 +#ifdef AGGRESSIVE_CHECK
1897 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1898 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1900 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1902 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1903 + gdp->bg_free_blocks_count =
1904 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1905 + - ac.ac_b_ex.fe_len);
1906 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1907 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1909 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1912 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1918 + brelse(bitmap_bh);
1920 + /* drop non-allocated, but dquote'd blocks */
1921 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1922 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1924 + *len = ac.ac_b_ex.fe_len;
1925 + J_ASSERT(*len > 0);
1926 + J_ASSERT(block != 0);
1930 + /* if we've already allocated something, roll it back */
1931 + if (ac.ac_status == AC_STATUS_FOUND) {
1932 + /* FIXME: free blocks here */
1935 + DQUOT_FREE_BLOCK(inode, *len);
1936 + brelse(bitmap_bh);
1940 + if (ac.ac_buddy_page)
1941 + page_cache_release(ac.ac_buddy_page);
1942 + if (ac.ac_bitmap_page)
1943 + page_cache_release(ac.ac_bitmap_page);
1945 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1946 + /* block wasn't reserved before and we reserved it
1947 + * at the beginning of allocation. it doesn't matter
1948 + * whether we allocated anything or we failed: time
1949 + * to release reservation. NOTE: because I expect
1950 + * any multiblock request from delayed allocation
1951 + * path only, here is single block always */
1952 + ext3_mb_release_blocks(sb, 1);
1955 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1956 + atomic_inc(&sbi->s_bal_reqs);
1957 + atomic_add(*len, &sbi->s_bal_allocated);
1958 + if (*len >= ac.ac_g_ex.fe_len)
1959 + atomic_inc(&sbi->s_bal_success);
1960 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1961 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1962 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1963 + atomic_inc(&sbi->s_bal_goals);
1964 + if (ac.ac_found > ext3_mb_max_to_scan)
1965 + atomic_inc(&sbi->s_bal_breaks);
1968 + ext3_mb_store_history(sb, inode->i_ino, &ac);
1972 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1974 +#ifdef EXT3_MB_HISTORY
1975 +struct ext3_mb_proc_session {
1976 + struct ext3_mb_history *history;
1977 + struct super_block *sb;
1982 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1983 + struct ext3_mb_history *hs,
1986 + if (hs == s->history + s->max)
1988 + if (!first && hs == s->history + s->start)
1990 + while (hs->goal.fe_len == 0) {
1992 + if (hs == s->history + s->max)
1994 + if (hs == s->history + s->start)
2000 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2002 + struct ext3_mb_proc_session *s = seq->private;
2003 + struct ext3_mb_history *hs;
2007 + return SEQ_START_TOKEN;
2008 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2011 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2015 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2017 + struct ext3_mb_proc_session *s = seq->private;
2018 + struct ext3_mb_history *hs = v;
2021 + if (v == SEQ_START_TOKEN)
2022 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2024 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2027 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2029 + struct ext3_mb_history *hs = v;
2030 + char buf[20], buf2[20];
2032 + if (v == SEQ_START_TOKEN) {
2033 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2034 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2035 + "merge", "tail", "broken");
2039 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2040 + hs->goal.fe_start, hs->goal.fe_len);
2041 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2042 + hs->result.fe_start, hs->result.fe_len);
2043 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2044 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2045 + hs->cr, hs->merged ? "M" : "", hs->tail,
2046 + hs->buddy ? 1 << hs->buddy : 0);
2050 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2054 +static struct seq_operations ext3_mb_seq_history_ops = {
2055 + .start = ext3_mb_seq_history_start,
2056 + .next = ext3_mb_seq_history_next,
2057 + .stop = ext3_mb_seq_history_stop,
2058 + .show = ext3_mb_seq_history_show,
2061 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2063 + struct super_block *sb = PDE(inode)->data;
2064 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2065 + struct ext3_mb_proc_session *s;
2068 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2071 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2072 + s->history = kmalloc(size, GFP_KERNEL);
2078 + spin_lock(&sbi->s_mb_history_lock);
2079 + memcpy(s->history, sbi->s_mb_history, size);
2080 + s->max = sbi->s_mb_history_max;
2081 + s->start = sbi->s_mb_history_cur % s->max;
2082 + spin_unlock(&sbi->s_mb_history_lock);
2084 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2086 + struct seq_file *m = (struct seq_file *)file->private_data;
2089 + kfree(s->history);
2096 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2098 + struct seq_file *seq = (struct seq_file *)file->private_data;
2099 + struct ext3_mb_proc_session *s = seq->private;
2100 + kfree(s->history);
2102 + return seq_release(inode, file);
2105 +static struct file_operations ext3_mb_seq_history_fops = {
2106 + .owner = THIS_MODULE,
2107 + .open = ext3_mb_seq_history_open,
2109 + .llseek = seq_lseek,
2110 + .release = ext3_mb_seq_history_release,
2113 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2115 + struct super_block *sb = seq->private;
2116 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2119 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2123 + return (void *) group;
2126 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2128 + struct super_block *sb = seq->private;
2129 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2133 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2136 + return (void *) group;;
2139 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2141 + struct super_block *sb = seq->private;
2142 + long group = (long) v, i;
2144 + struct ext3_group_info info;
2145 + unsigned short counters[16];
2150 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2151 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2152 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2153 + "2^11", "2^12", "2^13");
2155 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2156 + sizeof(struct ext3_group_info);
2157 + ext3_lock_group(sb, group);
2158 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2159 + ext3_unlock_group(sb, group);
2161 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2164 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2165 + sg.info.bb_fragments, sg.info.bb_first_free);
2166 + for (i = 0; i <= 13; i++)
2167 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2168 + sg.info.bb_counters[i] : 0);
2169 + seq_printf(seq, " ]\n");
2174 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2178 +static struct seq_operations ext3_mb_seq_groups_ops = {
2179 + .start = ext3_mb_seq_groups_start,
2180 + .next = ext3_mb_seq_groups_next,
2181 + .stop = ext3_mb_seq_groups_stop,
2182 + .show = ext3_mb_seq_groups_show,
2185 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2187 + struct super_block *sb = PDE(inode)->data;
2190 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2192 + struct seq_file *m = (struct seq_file *)file->private_data;
2199 +static struct file_operations ext3_mb_seq_groups_fops = {
2200 + .owner = THIS_MODULE,
2201 + .open = ext3_mb_seq_groups_open,
2203 + .llseek = seq_lseek,
2204 + .release = seq_release,
2207 +static void ext3_mb_history_release(struct super_block *sb)
2209 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2212 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2213 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2214 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2215 + remove_proc_entry(name, proc_root_ext3);
2217 + if (sbi->s_mb_history)
2218 + kfree(sbi->s_mb_history);
2221 +static void ext3_mb_history_init(struct super_block *sb)
2223 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2227 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2228 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2229 + if (sbi->s_mb_proc != NULL) {
2230 + struct proc_dir_entry *p;
2231 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2233 + p->proc_fops = &ext3_mb_seq_history_fops;
2236 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2238 + p->proc_fops = &ext3_mb_seq_groups_fops;
2243 + sbi->s_mb_history_max = 1000;
2244 + sbi->s_mb_history_cur = 0;
2245 + spin_lock_init(&sbi->s_mb_history_lock);
2246 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2247 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2248 + memset(sbi->s_mb_history, 0, i);
2249 + /* if we can't allocate history, then we simple won't use it */
2253 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2254 + struct ext3_allocation_context *ac)
2256 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2257 + struct ext3_mb_history h;
2259 + if (likely(sbi->s_mb_history == NULL))
2262 + h.pid = current->pid;
2264 + h.goal = ac->ac_g_ex;
2265 + h.result = ac->ac_b_ex;
2266 + h.found = ac->ac_found;
2267 + h.cr = ac->ac_criteria;
2268 + h.groups = ac->ac_groups_scanned;
2269 + h.tail = ac->ac_tail;
2270 + h.buddy = ac->ac_buddy;
2272 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2273 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2276 + spin_lock(&sbi->s_mb_history_lock);
2277 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2278 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2279 + sbi->s_mb_history_cur = 0;
2280 + spin_unlock(&sbi->s_mb_history_lock);
2284 +#define ext3_mb_history_release(sb)
2285 +#define ext3_mb_history_init(sb)
2288 +int ext3_mb_init_backend(struct super_block *sb)
2290 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2291 + int i, j, len, metalen;
2292 + int num_meta_group_infos =
2293 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2294 + EXT3_DESC_PER_BLOCK_BITS(sb);
2295 + struct ext3_group_info **meta_group_info;
2297 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2298 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2299 + * So a two level scheme suffices for now. */
2300 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2301 + num_meta_group_infos, GFP_KERNEL);
2302 + if (sbi->s_group_info == NULL) {
2303 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2306 + sbi->s_buddy_cache = new_inode(sb);
2307 + if (sbi->s_buddy_cache == NULL) {
2308 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2312 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2313 + for (i = 0; i < num_meta_group_infos; i++) {
2314 + if ((i + 1) == num_meta_group_infos)
2315 + metalen = sizeof(*meta_group_info) *
2316 + (sbi->s_groups_count -
2317 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2318 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2319 + if (meta_group_info == NULL) {
2320 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2322 + goto err_freemeta;
2324 + sbi->s_group_info[i] = meta_group_info;
2328 + * calculate needed size. if change bb_counters size,
2329 + * don't forget about ext3_mb_generate_buddy()
2331 + len = sizeof(struct ext3_group_info);
2332 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2333 + for (i = 0; i < sbi->s_groups_count; i++) {
2334 + struct ext3_group_desc * desc;
2337 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2338 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2340 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2341 + if (meta_group_info[j] == NULL) {
2342 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2344 + goto err_freebuddy;
2346 + desc = ext3_get_group_desc(sb, i, NULL);
2347 + if (desc == NULL) {
2348 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2349 + goto err_freebuddy;
2351 + memset(meta_group_info[j], 0, len);
2352 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2353 + &meta_group_info[j]->bb_state);
2354 + meta_group_info[j]->bb_free =
2355 + le16_to_cpu(desc->bg_free_blocks_count);
2362 + kfree(EXT3_GROUP_INFO(sb, i));
2365 + i = num_meta_group_infos;
2368 + kfree(sbi->s_group_info[i]);
2369 + iput(sbi->s_buddy_cache);
2371 + kfree(sbi->s_group_info);
2375 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2377 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2378 + struct inode *root = sb->s_root->d_inode;
2379 + unsigned i, offset, max;
2380 + struct dentry *dentry;
2382 + if (!test_opt(sb, MBALLOC))
2385 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2387 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2388 + if (sbi->s_mb_offsets == NULL) {
2389 + clear_opt(sbi->s_mount_opt, MBALLOC);
2392 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2393 + if (sbi->s_mb_maxs == NULL) {
2394 + clear_opt(sbi->s_mount_opt, MBALLOC);
2395 + kfree(sbi->s_mb_maxs);
2399 + /* order 0 is regular bitmap */
2400 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2401 + sbi->s_mb_offsets[0] = 0;
2405 + max = sb->s_blocksize << 2;
2407 + sbi->s_mb_offsets[i] = offset;
2408 + sbi->s_mb_maxs[i] = max;
2409 + offset += 1 << (sb->s_blocksize_bits - i);
2412 + } while (i <= sb->s_blocksize_bits + 1);
2414 + /* init file for buddy data */
2415 + if ((i = ext3_mb_init_backend(sb))) {
2416 + clear_opt(sbi->s_mount_opt, MBALLOC);
2417 + kfree(sbi->s_mb_offsets);
2418 + kfree(sbi->s_mb_maxs);
2422 + spin_lock_init(&sbi->s_reserve_lock);
2423 + spin_lock_init(&sbi->s_md_lock);
2424 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2425 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2426 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2427 + spin_lock_init(&sbi->s_bal_lock);
2429 + /* remove old on-disk buddy file */
2430 + down(&root->i_sem);
2431 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2432 + if (dentry->d_inode != NULL) {
2433 + i = vfs_unlink(root, dentry);
2435 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2440 + ext3_mb_history_init(sb);
2442 + printk("EXT3-fs: mballoc enabled\n");
2446 +int ext3_mb_release(struct super_block *sb)
2448 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2449 + int i, num_meta_group_infos;
2451 + if (!test_opt(sb, MBALLOC))
2454 + /* release freed, non-committed blocks */
2455 + spin_lock(&sbi->s_md_lock);
2456 + list_splice_init(&sbi->s_closed_transaction,
2457 + &sbi->s_committed_transaction);
2458 + list_splice_init(&sbi->s_active_transaction,
2459 + &sbi->s_committed_transaction);
2460 + spin_unlock(&sbi->s_md_lock);
2461 + ext3_mb_free_committed_blocks(sb);
2463 + if (sbi->s_group_info) {
2464 + for (i = 0; i < sbi->s_groups_count; i++)
2465 + kfree(EXT3_GROUP_INFO(sb, i));
2466 + num_meta_group_infos = (sbi->s_groups_count +
2467 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2468 + EXT3_DESC_PER_BLOCK_BITS(sb);
2469 + for (i = 0; i < num_meta_group_infos; i++)
2470 + kfree(sbi->s_group_info[i]);
2471 + kfree(sbi->s_group_info);
2473 + if (sbi->s_mb_offsets)
2474 + kfree(sbi->s_mb_offsets);
2475 + if (sbi->s_mb_maxs)
2476 + kfree(sbi->s_mb_maxs);
2477 + if (sbi->s_buddy_cache)
2478 + iput(sbi->s_buddy_cache);
2479 + if (sbi->s_blocks_reserved)
2480 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2481 + sbi->s_blocks_reserved);
2482 + if (ext3_mb_stats) {
2483 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2484 + atomic_read(&sbi->s_bal_allocated),
2485 + atomic_read(&sbi->s_bal_reqs),
2486 + atomic_read(&sbi->s_bal_success));
2487 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2488 + "%u 2^N hits, %u breaks\n",
2489 + atomic_read(&sbi->s_bal_ex_scanned),
2490 + atomic_read(&sbi->s_bal_goals),
2491 + atomic_read(&sbi->s_bal_2orders),
2492 + atomic_read(&sbi->s_bal_breaks));
2493 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2494 + sbi->s_mb_buddies_generated++,
2495 + sbi->s_mb_generation_time);
2498 + ext3_mb_history_release(sb);
2503 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2505 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2506 + int err, i, count = 0, count2 = 0;
2507 + struct ext3_free_metadata *md;
2508 + struct ext3_buddy e3b;
2510 + if (list_empty(&sbi->s_committed_transaction))
2513 + /* there is committed blocks to be freed yet */
2515 + /* get next array of blocks */
2517 + spin_lock(&sbi->s_md_lock);
2518 + if (!list_empty(&sbi->s_committed_transaction)) {
2519 + md = list_entry(sbi->s_committed_transaction.next,
2520 + struct ext3_free_metadata, list);
2521 + list_del(&md->list);
2523 + spin_unlock(&sbi->s_md_lock);
2528 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2529 + md->num, md->group, md);
2531 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2532 + /* we expect to find existing buddy because it's pinned */
2535 + /* there are blocks to put in buddy to make them really free */
2538 + ext3_lock_group(sb, md->group);
2539 + for (i = 0; i < md->num; i++) {
2540 + mb_debug(" %u", md->blocks[i]);
2541 + mb_free_blocks(&e3b, md->blocks[i], 1);
2544 + ext3_unlock_group(sb, md->group);
2546 + /* balance refcounts from ext3_mb_free_metadata() */
2547 + page_cache_release(e3b.bd_buddy_page);
2548 + page_cache_release(e3b.bd_bitmap_page);
2551 + ext3_mb_release_desc(&e3b);
2554 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2557 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2559 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2561 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2564 + /* new transaction! time to close last one and free blocks for
2565 + * committed transaction. we know that only transaction can be
2566 + * active, so previos transaction can be being logged and we
2567 + * know that transaction before previous is known to be already
2568 + * logged. this means that now we may free blocks freed in all
2569 + * transactions before previous one. hope I'm clear enough ... */
2571 + spin_lock(&sbi->s_md_lock);
2572 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2573 + mb_debug("new transaction %lu, old %lu\n",
2574 + (unsigned long) handle->h_transaction->t_tid,
2575 + (unsigned long) sbi->s_last_transaction);
2576 + list_splice_init(&sbi->s_closed_transaction,
2577 + &sbi->s_committed_transaction);
2578 + list_splice_init(&sbi->s_active_transaction,
2579 + &sbi->s_closed_transaction);
2580 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2582 + spin_unlock(&sbi->s_md_lock);
2584 + ext3_mb_free_committed_blocks(sb);
2587 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2588 + int group, int block, int count)
2590 + struct ext3_group_info *db = e3b->bd_info;
2591 + struct super_block *sb = e3b->bd_sb;
2592 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2593 + struct ext3_free_metadata *md;
2596 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2597 + J_ASSERT(e3b->bd_buddy_page != NULL);
2599 + ext3_lock_group(sb, group);
2600 + for (i = 0; i < count; i++) {
2601 + md = db->bb_md_cur;
2602 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2603 + db->bb_md_cur = NULL;
2608 + ext3_unlock_group(sb, group);
2609 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2613 + md->group = group;
2615 + ext3_lock_group(sb, group);
2616 + if (db->bb_md_cur == NULL) {
2617 + spin_lock(&sbi->s_md_lock);
2618 + list_add(&md->list, &sbi->s_active_transaction);
2619 + spin_unlock(&sbi->s_md_lock);
2620 + /* protect buddy cache from being freed,
2621 + * otherwise we'll refresh it from
2622 + * on-disk bitmap and lose not-yet-available
2624 + page_cache_get(e3b->bd_buddy_page);
2625 + page_cache_get(e3b->bd_bitmap_page);
2626 + db->bb_md_cur = md;
2627 + db->bb_tid = handle->h_transaction->t_tid;
2628 + mb_debug("new md 0x%p for group %u\n",
2632 + md = db->bb_md_cur;
2636 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2637 + md->blocks[md->num] = block + i;
2639 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2640 + /* no more space, put full container on a sb's list */
2641 + db->bb_md_cur = NULL;
2644 + ext3_unlock_group(sb, group);
2648 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2649 + unsigned long block, unsigned long count,
2650 + int metadata, int *freed)
2652 + struct buffer_head *bitmap_bh = NULL;
2653 + struct ext3_group_desc *gdp;
2654 + struct ext3_super_block *es;
2655 + unsigned long bit, overflow;
2656 + struct buffer_head *gd_bh;
2657 + unsigned long block_group;
2658 + struct ext3_sb_info *sbi;
2659 + struct super_block *sb;
2660 + struct ext3_buddy e3b;
2666 + printk ("ext3_free_blocks: nonexistent device");
2670 + ext3_mb_poll_new_transaction(sb, handle);
2672 + sbi = EXT3_SB(sb);
2673 + es = EXT3_SB(sb)->s_es;
2674 + if (block < le32_to_cpu(es->s_first_data_block) ||
2675 + block + count < block ||
2676 + block + count > le32_to_cpu(es->s_blocks_count)) {
2677 + ext3_error (sb, "ext3_free_blocks",
2678 + "Freeing blocks not in datazone - "
2679 + "block = %lu, count = %lu", block, count);
2680 + goto error_return;
2683 + ext3_debug("freeing block %lu\n", block);
2687 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2688 + EXT3_BLOCKS_PER_GROUP(sb);
2689 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2690 + EXT3_BLOCKS_PER_GROUP(sb);
2692 + * Check to see if we are freeing blocks across a group
2695 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2696 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2697 + count -= overflow;
2699 + brelse(bitmap_bh);
2700 + bitmap_bh = read_block_bitmap(sb, block_group);
2702 + goto error_return;
2703 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2705 + goto error_return;
2707 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2708 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2709 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2710 + EXT3_SB(sb)->s_itb_per_group) ||
2711 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2712 + EXT3_SB(sb)->s_itb_per_group))
2713 + ext3_error (sb, "ext3_free_blocks",
2714 + "Freeing blocks in system zones - "
2715 + "Block = %lu, count = %lu",
2718 + BUFFER_TRACE(bitmap_bh, "getting write access");
2719 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2721 + goto error_return;
2724 + * We are about to modify some metadata. Call the journal APIs
2725 + * to unshare ->b_data if a currently-committing transaction is
2728 + BUFFER_TRACE(gd_bh, "get_write_access");
2729 + err = ext3_journal_get_write_access(handle, gd_bh);
2731 + goto error_return;
2733 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2735 + goto error_return;
2737 +#ifdef AGGRESSIVE_CHECK
2740 + for (i = 0; i < count; i++)
2741 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2744 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2746 + /* We dirtied the bitmap block */
2747 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2748 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2751 + /* blocks being freed are metadata. these blocks shouldn't
2752 + * be used until this transaction is committed */
2753 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2755 + ext3_lock_group(sb, block_group);
2756 + mb_free_blocks(&e3b, bit, count);
2757 + ext3_unlock_group(sb, block_group);
2760 + spin_lock(sb_bgl_lock(sbi, block_group));
2761 + gdp->bg_free_blocks_count =
2762 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2763 + spin_unlock(sb_bgl_lock(sbi, block_group));
2764 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2766 + ext3_mb_release_desc(&e3b);
2770 + /* And the group descriptor block */
2771 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2772 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2773 + if (!err) err = ret;
2775 + if (overflow && !err) {
2782 + brelse(bitmap_bh);
2783 + ext3_std_error(sb, err);
2787 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2789 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2790 + int free, ret = -ENOSPC;
2792 + BUG_ON(blocks < 0);
2793 + spin_lock(&sbi->s_reserve_lock);
2794 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2795 + if (blocks <= free - sbi->s_blocks_reserved) {
2796 + sbi->s_blocks_reserved += blocks;
2799 + spin_unlock(&sbi->s_reserve_lock);
2803 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2805 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2807 + BUG_ON(blocks < 0);
2808 + spin_lock(&sbi->s_reserve_lock);
2809 + sbi->s_blocks_reserved -= blocks;
2810 + WARN_ON(sbi->s_blocks_reserved < 0);
2811 + if (sbi->s_blocks_reserved < 0)
2812 + sbi->s_blocks_reserved = 0;
2813 + spin_unlock(&sbi->s_reserve_lock);
2816 +int ext3_new_block(handle_t *handle, struct inode *inode,
2817 + unsigned long goal, int *errp)
2821 + if (!test_opt(inode->i_sb, MBALLOC)) {
2822 + ret = ext3_new_block_old(handle, inode, goal, errp);
2826 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2832 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2833 + unsigned long block, unsigned long count, int metadata)
2835 + struct super_block *sb;
2839 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2840 + ext3_free_blocks_sb(handle, sb, block, count, &freed);
2842 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2844 + DQUOT_FREE_BLOCK(inode, freed);
2848 +#define EXT3_ROOT "ext3"
2849 +#define EXT3_MB_STATS_NAME "mb_stats"
2850 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2851 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2852 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2854 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2855 + int count, int *eof, void *data)
2863 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2868 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2869 + unsigned long count, void *data)
2873 + if (count >= sizeof(str)) {
2874 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2875 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2876 + return -EOVERFLOW;
2879 + if (copy_from_user(str, buffer, count))
2882 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2883 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2887 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2888 + int count, int *eof, void *data)
2896 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2901 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2902 + unsigned long count, void *data)
2907 + if (count >= sizeof(str)) {
2908 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2909 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2910 + return -EOVERFLOW;
2913 + if (copy_from_user(str, buffer, count))
2916 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2917 + value = simple_strtol(str, NULL, 0);
2921 + ext3_mb_max_to_scan = value;
2926 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2927 + int count, int *eof, void *data)
2935 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2940 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2941 + unsigned long count, void *data)
2946 + if (count >= sizeof(str)) {
2947 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2948 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2949 + return -EOVERFLOW;
2952 + if (copy_from_user(str, buffer, count))
2955 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2956 + value = simple_strtol(str, NULL, 0);
2960 + ext3_mb_min_to_scan = value;
2965 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2966 + int count, int *eof, void *data)
2974 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
2979 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2980 + unsigned long count, void *data)
2985 + if (count >= sizeof(str)) {
2986 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2987 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2988 + return -EOVERFLOW;
2991 + if (copy_from_user(str, buffer, count))
2994 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2995 + value = simple_strtol(str, NULL, 0);
2999 + ext3_mb_order2_reqs = value;
3004 +int __init init_ext3_proc(void)
3006 + struct proc_dir_entry *proc_ext3_mb_stats;
3007 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3008 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3009 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3011 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3012 + if (proc_root_ext3 == NULL) {
3013 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3017 + /* Initialize EXT3_MB_STATS_NAME */
3018 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3019 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3020 + if (proc_ext3_mb_stats == NULL) {
3021 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3022 + EXT3_MB_STATS_NAME);
3023 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3027 + proc_ext3_mb_stats->data = NULL;
3028 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3029 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3031 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3032 + proc_ext3_mb_max_to_scan = create_proc_entry(
3033 + EXT3_MB_MAX_TO_SCAN_NAME,
3034 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3035 + if (proc_ext3_mb_max_to_scan == NULL) {
3036 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3037 + EXT3_MB_MAX_TO_SCAN_NAME);
3038 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3039 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3043 + proc_ext3_mb_max_to_scan->data = NULL;
3044 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3045 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3047 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3048 + proc_ext3_mb_min_to_scan = create_proc_entry(
3049 + EXT3_MB_MIN_TO_SCAN_NAME,
3050 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3051 + if (proc_ext3_mb_min_to_scan == NULL) {
3052 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3053 + EXT3_MB_MIN_TO_SCAN_NAME);
3054 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3055 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3056 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3060 + proc_ext3_mb_min_to_scan->data = NULL;
3061 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3062 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3064 + /* Initialize EXT3_ORDER2_REQ */
3065 + proc_ext3_mb_order2_req = create_proc_entry(
3066 + EXT3_MB_ORDER2_REQ,
3067 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3068 + if (proc_ext3_mb_order2_req == NULL) {
3069 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3070 + EXT3_MB_ORDER2_REQ);
3071 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3072 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3073 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3074 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3078 + proc_ext3_mb_order2_req->data = NULL;
3079 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3080 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3085 +void exit_ext3_proc(void)
3087 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3088 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3089 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3090 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3091 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3093 Index: linux-2.6.12.6-bull/fs/ext3/Makefile
3094 ===================================================================
3095 --- linux-2.6.12.6-bull.orig/fs/ext3/Makefile 2006-04-29 20:39:09.000000000 +0400
3096 +++ linux-2.6.12.6-bull/fs/ext3/Makefile 2006-04-29 20:39:10.000000000 +0400
3097 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
3099 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3100 ioctl.o namei.o super.o symlink.o hash.o resize.o \
3102 + extents.o mballoc.o
3104 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3105 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o