1 Index: linux-2.6.9-full/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.9-full.orig/include/linux/ext3_fs.h 2006-06-01 14:58:46.000000000 +0400
4 +++ linux-2.6.9-full/include/linux/ext3_fs.h 2006-10-24 12:54:31.000000000 +0400
5 @@ -57,6 +57,14 @@ struct statfs;
6 #define ext3_debug(f, a...) do {} while (0)
9 +#define EXT3_MULTIBLOCK_ALLOCATOR 1
11 +#define EXT3_MB_HINT_MERGE 1
12 +#define EXT3_MB_HINT_RESERVED 2
13 +#define EXT3_MB_HINT_METADATA 4
14 +#define EXT3_MB_HINT_FIRST 8
15 +#define EXT3_MB_HINT_BEST 16
18 * Special inodes numbers
20 @@ -365,6 +373,7 @@ struct ext3_inode {
21 #define EXT3_MOUNT_IOPEN_NOPRIV 0x100000/* Make iopen world-readable */
22 #define EXT3_MOUNT_EXTENTS 0x200000/* Extents support */
23 #define EXT3_MOUNT_EXTDEBUG 0x400000/* Extents debug */
24 +#define EXT3_MOUNT_MBALLOC 0x800000/* Buddy allocation support */
26 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
28 @@ -387,6 +396,14 @@ struct ext3_inode {
29 #define ext3_find_first_zero_bit ext2_find_first_zero_bit
30 #define ext3_find_next_zero_bit ext2_find_next_zero_bit
32 +#ifndef ext2_find_next_le_bit
33 +#ifdef __LITTLE_ENDIAN
34 +#define ext2_find_next_le_bit(addr, size, off) find_next_bit((addr), (size), (off))
36 +#error "mballoc needs a patch for big-endian systems - CFS bug 10634"
37 +#endif /* __LITTLE_ENDIAN */
38 +#endif /* !ext2_find_next_le_bit */
41 * Maximal mount counts between two filesystem checks
43 @@ -726,7 +743,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 @@ -857,6 +874,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.9-full/include/linux/ext3_fs_sb.h
71 ===================================================================
72 --- linux-2.6.9-full.orig/include/linux/ext3_fs_sb.h 2006-05-18 23:57:04.000000000 +0400
73 +++ linux-2.6.9-full/include/linux/ext3_fs_sb.h 2006-10-24 12:54:31.000000000 +0400
76 #include <linux/blockgroup_lock.h>
77 #include <linux/percpu_counter.h>
78 +#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 @@ -81,6 +87,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.9-full/fs/ext3/super.c
135 ===================================================================
136 --- linux-2.6.9-full.orig/fs/ext3/super.c 2006-06-01 14:58:46.000000000 +0400
137 +++ linux-2.6.9-full/fs/ext3/super.c 2006-10-24 12:54:31.000000000 +0400
138 @@ -394,6 +394,7 @@ void ext3_put_super (struct super_block
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 @@ -649,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 @@ -962,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 @@ -1651,6 +1669,7 @@ static int ext3_fill_super (struct super
185 ext3_count_dirs(sb));
188 + ext3_mb_init(sb, needs_recovery);
192 @@ -2433,7 +2452,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 @@ -2455,6 +2480,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.9-full/fs/ext3/extents.c
216 ===================================================================
217 --- linux-2.6.9-full.orig/fs/ext3/extents.c 2006-06-01 14:58:46.000000000 +0400
218 +++ linux-2.6.9-full/fs/ext3/extents.c 2006-10-24 12:54:31.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.9-full/fs/ext3/inode.c
261 ===================================================================
262 --- linux-2.6.9-full.orig/fs/ext3/inode.c 2006-06-01 14:58:46.000000000 +0400
263 +++ linux-2.6.9-full/fs/ext3/inode.c 2006-10-24 12:54:31.000000000 +0400
264 @@ -572,7 +572,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 @@ -673,7 +673,7 @@ err_out:
275 for (i = 0; i < num; i++)
276 ext3_free_blocks(handle, inode,
277 - le32_to_cpu(where[i].key), 1);
278 + le32_to_cpu(where[i].key), 1, 1);
282 @@ -1831,7 +1831,7 @@ ext3_clear_blocks(handle_t *handle, stru
286 - ext3_free_blocks(handle, inode, block_to_free, count);
287 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
291 @@ -2004,7 +2004,7 @@ static void ext3_free_branches(handle_t
292 ext3_journal_test_restart(handle, inode);
295 - ext3_free_blocks(handle, inode, nr, 1);
296 + ext3_free_blocks(handle, inode, nr, 1, 1);
300 Index: linux-2.6.9-full/fs/ext3/balloc.c
301 ===================================================================
302 --- linux-2.6.9-full.orig/fs/ext3/balloc.c 2006-03-10 18:20:03.000000000 +0300
303 +++ linux-2.6.9-full/fs/ext3/balloc.c 2006-10-24 12:54:31.000000000 +0400
304 @@ -79,7 +79,7 @@ struct ext3_group_desc * ext3_get_group_
306 * Return buffer_head on success or NULL in case of failure.
308 -static struct buffer_head *
309 +struct buffer_head *
310 read_block_bitmap(struct super_block *sb, unsigned int block_group)
312 struct ext3_group_desc * desc;
313 @@ -451,24 +451,6 @@ error_return:
317 -/* Free given blocks, update quota and i_blocks field */
318 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
319 - unsigned long block, unsigned long count)
321 - struct super_block * sb;
322 - int dquot_freed_blocks;
326 - printk ("ext3_free_blocks: nonexistent device");
329 - ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
330 - if (dquot_freed_blocks)
331 - DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
336 * For ext3 allocations, we must not reuse any blocks which are
337 * allocated in the bitmap buffer's "last committed data" copy. This
338 @@ -1131,7 +1113,7 @@ int ext3_should_retry_alloc(struct super
339 * bitmap, and then for any free bit if that fails.
340 * This function also updates quota and i_blocks field.
342 -int ext3_new_block(handle_t *handle, struct inode *inode,
343 +int ext3_new_block_old(handle_t *handle, struct inode *inode,
344 unsigned long goal, int *errp)
346 struct buffer_head *bitmap_bh = NULL;
347 Index: linux-2.6.9-full/fs/ext3/xattr.c
348 ===================================================================
349 --- linux-2.6.9-full.orig/fs/ext3/xattr.c 2006-05-18 23:57:04.000000000 +0400
350 +++ linux-2.6.9-full/fs/ext3/xattr.c 2006-10-24 12:54:31.000000000 +0400
351 @@ -1281,7 +1281,7 @@ ext3_xattr_set_handle2(handle_t *handle,
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 @@ -1328,7 +1328,7 @@ getblk_failed:
362 mb_cache_entry_free(ce);
363 ea_bdebug(old_bh, "freeing");
364 - ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1);
365 + ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1, 1);
367 /* ext3_forget() calls bforget() for us, but we
368 let our caller release old_bh, so we need to
369 @@ -1427,7 +1427,7 @@ ext3_xattr_delete_inode(handle_t *handle
370 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
372 mb_cache_entry_free(ce);
373 - ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1);
374 + ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1, 1);
376 ext3_forget(handle, 1, inode, bh, EXT3_I(inode)->i_file_acl);
378 Index: linux-2.6.9-full/fs/ext3/mballoc.c
379 ===================================================================
380 --- linux-2.6.9-full.orig/fs/ext3/mballoc.c 2006-10-23 18:07:54.821533176 +0400
381 +++ linux-2.6.9-full/fs/ext3/mballoc.c 2006-10-24 13:00:56.000000000 +0400
384 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
385 + * Written by Alex Tomas <alex@clusterfs.com>
387 + * This program is free software; you can redistribute it and/or modify
388 + * it under the terms of the GNU General Public License version 2 as
389 + * published by the Free Software Foundation.
391 + * This program is distributed in the hope that it will be useful,
392 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
393 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
394 + * GNU General Public License for more details.
396 + * You should have received a copy of the GNU General Public Licens
397 + * along with this program; if not, write to the Free Software
398 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
403 + * mballoc.c contains the multiblocks allocation routines
406 +#include <linux/config.h>
407 +#include <linux/time.h>
408 +#include <linux/fs.h>
409 +#include <linux/namei.h>
410 +#include <linux/jbd.h>
411 +#include <linux/ext3_fs.h>
412 +#include <linux/ext3_jbd.h>
413 +#include <linux/quotaops.h>
414 +#include <linux/buffer_head.h>
415 +#include <linux/module.h>
416 +#include <linux/swap.h>
417 +#include <linux/proc_fs.h>
418 +#include <linux/pagemap.h>
419 +#include <linux/seq_file.h>
423 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
424 + * - track min/max extents in each group for better group selection
425 + * - mb_mark_used() may allocate chunk right after splitting buddy
426 + * - special flag to advice allocator to look for requested + N blocks
427 + * this may improve interaction between extents and mballoc
428 + * - tree of groups sorted by number of free blocks
429 + * - percpu reservation code (hotpath)
434 + * with AGRESSIVE_CHECK allocator runs consistency checks over
435 + * structures. these checks slow things down a lot
437 +#define AGGRESSIVE_CHECK__
443 +#define mb_debug(fmt,a...) printk(fmt, ##a)
445 +#define mb_debug(fmt,a...)
449 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
450 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
452 +#define EXT3_MB_HISTORY
455 + * How long mballoc can look for a best extent (in found extents)
457 +long ext3_mb_max_to_scan = 500;
460 + * How long mballoc must look for a best extent
462 +long ext3_mb_min_to_scan = 30;
465 + * with 'ext3_mb_stats' allocator will collect stats that will be
466 + * shown at umount. The collecting costs though!
469 +long ext3_mb_stats = 1;
472 + * for which requests use 2^N search using buddies
474 +long ext3_mb_order2_reqs = 8;
476 +#ifdef EXT3_BB_MAX_BLOCKS
477 +#undef EXT3_BB_MAX_BLOCKS
479 +#define EXT3_BB_MAX_BLOCKS 30
481 +struct ext3_free_metadata {
482 + unsigned short group;
483 + unsigned short num;
484 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
485 + struct list_head list;
488 +struct ext3_group_info {
489 + unsigned long bb_state;
490 + unsigned long bb_tid;
491 + struct ext3_free_metadata *bb_md_cur;
492 + unsigned short bb_first_free;
493 + unsigned short bb_free;
494 + unsigned short bb_fragments;
495 + unsigned short bb_counters[];
499 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
500 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
502 +#define EXT3_MB_GRP_NEED_INIT(grp) \
503 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
505 +struct ext3_free_extent {
511 +struct ext3_allocation_context {
512 + struct super_block *ac_sb;
515 + struct ext3_free_extent ac_g_ex;
517 + /* the best found extent */
518 + struct ext3_free_extent ac_b_ex;
520 + /* number of iterations done. we have to track to limit searching */
521 + unsigned long ac_ex_scanned;
522 + __u16 ac_groups_scanned;
527 + __u8 ac_flags; /* allocation hints */
530 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
531 + * N > 0, the field stores N, otherwise 0 */
533 + struct page *ac_buddy_page;
534 + struct page *ac_bitmap_page;
537 +#define AC_STATUS_CONTINUE 1
538 +#define AC_STATUS_FOUND 2
539 +#define AC_STATUS_BREAK 3
541 +struct ext3_mb_history {
542 + struct ext3_free_extent goal; /* goal allocation */
543 + struct ext3_free_extent result; /* result allocation */
546 + __u16 found; /* how many extents have been found */
547 + __u16 groups; /* how many groups have been scanned */
548 + __u16 tail; /* what tail broke some buddy */
549 + __u16 buddy; /* buddy the tail ^^^ broke */
550 + __u8 cr; /* which phase the result extent was found at */
555 + struct page *bd_buddy_page;
557 + struct page *bd_bitmap_page;
559 + struct ext3_group_info *bd_info;
560 + struct super_block *bd_sb;
564 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
565 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
567 +#ifndef EXT3_MB_HISTORY
568 +#define ext3_mb_store_history(sb,ino,ac)
570 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
571 + struct ext3_allocation_context *ac);
574 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
576 +static struct proc_dir_entry *proc_root_ext3;
578 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
579 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
580 +int ext3_new_block_old(handle_t *, struct inode *, unsigned long, int *);
581 +int ext3_mb_reserve_blocks(struct super_block *, int);
582 +void ext3_mb_release_blocks(struct super_block *, int);
583 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
584 +void ext3_mb_free_committed_blocks(struct super_block *);
586 +#if BITS_PER_LONG == 64
587 +#define mb_correct_addr_and_bit(bit,addr) \
589 + bit += ((unsigned long) addr & 7UL) << 3; \
590 + addr = (void *) ((unsigned long) addr & ~7UL); \
592 +#elif BITS_PER_LONG == 32
593 +#define mb_correct_addr_and_bit(bit,addr) \
595 + bit += ((unsigned long) addr & 3UL) << 3; \
596 + addr = (void *) ((unsigned long) addr & ~3UL); \
599 +#error "how many bits you are?!"
602 +static inline int mb_test_bit(int bit, void *addr)
604 + mb_correct_addr_and_bit(bit,addr);
605 + return ext2_test_bit(bit, addr);
608 +static inline void mb_set_bit(int bit, void *addr)
610 + mb_correct_addr_and_bit(bit,addr);
611 + ext2_set_bit(bit, addr);
614 +static inline void mb_set_bit_atomic(int bit, void *addr)
616 + mb_correct_addr_and_bit(bit,addr);
617 + ext2_set_bit_atomic(NULL, bit, addr);
620 +static inline void mb_clear_bit(int bit, void *addr)
622 + mb_correct_addr_and_bit(bit,addr);
623 + ext2_clear_bit(bit, addr);
626 +static inline void mb_clear_bit_atomic(int bit, void *addr)
628 + mb_correct_addr_and_bit(bit,addr);
629 + ext2_clear_bit_atomic(NULL, bit, addr);
632 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
635 +#if BITS_PER_LONG == 64
636 + fix = ((unsigned long) addr & 7UL) << 3;
637 + addr = (void *) ((unsigned long) addr & ~7UL);
638 +#elif BITS_PER_LONG == 32
639 + fix = ((unsigned long) addr & 3UL) << 3;
640 + addr = (void *) ((unsigned long) addr & ~3UL);
642 +#error "how many bits you are?!"
646 + return ext2_find_next_zero_bit(addr, max, start) - fix;
649 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
653 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
654 + J_ASSERT(max != NULL);
656 + if (order > e3b->bd_blkbits + 1) {
661 + /* at order 0 we see each particular block */
662 + *max = 1 << (e3b->bd_blkbits + 3);
664 + return EXT3_MB_BITMAP(e3b);
666 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
667 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
672 +#ifdef AGGRESSIVE_CHECK
674 +static void mb_check_buddy(struct ext3_buddy *e3b)
676 + int order = e3b->bd_blkbits + 1;
677 + int max, max2, i, j, k, count;
678 + int fragments = 0, fstart;
679 + void *buddy, *buddy2;
681 + if (!test_opt(e3b->bd_sb, MBALLOC))
685 + static int mb_check_counter = 0;
686 + if (mb_check_counter++ % 300 != 0)
690 + while (order > 1) {
691 + buddy = mb_find_buddy(e3b, order, &max);
693 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
695 + J_ASSERT(buddy != buddy2);
696 + J_ASSERT(max * 2 == max2);
699 + for (i = 0; i < max; i++) {
701 + if (mb_test_bit(i, buddy)) {
702 + /* only single bit in buddy2 may be 1 */
703 + if (!mb_test_bit(i << 1, buddy2))
704 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
705 + else if (!mb_test_bit((i << 1) + 1, buddy2))
706 + J_ASSERT(mb_test_bit(i << 1, buddy2));
710 + /* both bits in buddy2 must be 0 */
711 + J_ASSERT(mb_test_bit(i << 1, buddy2));
712 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
714 + for (j = 0; j < (1 << order); j++) {
715 + k = (i * (1 << order)) + j;
716 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
720 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
725 + buddy = mb_find_buddy(e3b, 0, &max);
726 + for (i = 0; i < max; i++) {
727 + if (!mb_test_bit(i, buddy)) {
728 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
729 + if (fstart == -1) {
736 + /* check used bits only */
737 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
738 + buddy2 = mb_find_buddy(e3b, j, &max2);
740 + J_ASSERT(k < max2);
741 + J_ASSERT(mb_test_bit(k, buddy2));
744 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
745 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
749 +#define mb_check_buddy(e3b)
752 +/* find most significant bit */
753 +static int inline fmsb(unsigned short word)
767 + } while (word != 0);
773 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
774 + int len, struct ext3_group_info *grp)
776 + struct ext3_sb_info *sbi = EXT3_SB(sb);
777 + unsigned short min, max, chunk, border;
779 + mb_debug("mark %u/%u free\n", first, len);
780 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
782 + border = 2 << sb->s_blocksize_bits;
785 + /* find how many blocks can be covered since this position */
786 + max = ffs(first | border) - 1;
788 + /* find how many blocks of power 2 we need to mark */
791 + mb_debug(" %u/%u -> max %u, min %u\n",
792 + first & ((2 << sb->s_blocksize_bits) - 1),
799 + /* mark multiblock chunks only */
800 + grp->bb_counters[min]++;
802 + mb_debug(" set %u at %u \n", first >> min,
803 + sbi->s_mb_offsets[min]);
804 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
813 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
816 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
817 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
818 + unsigned short i = 0, first, len;
819 + unsigned free = 0, fragments = 0;
820 + unsigned long long period = get_cycles();
822 + i = mb_find_next_zero_bit(bitmap, max, 0);
823 + grp->bb_first_free = i;
827 + i = ext2_find_next_le_bit(bitmap, max, i);
831 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
833 + grp->bb_counters[0]++;
835 + i = mb_find_next_zero_bit(bitmap, max, i);
837 + grp->bb_fragments = fragments;
839 + /* bb_state shouldn't being modified because all
840 + * others waits for init completion on page lock */
841 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
842 + if (free != grp->bb_free) {
843 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
844 + group, free, grp->bb_free);
845 + grp->bb_free = free;
848 + period = get_cycles() - period;
849 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
850 + EXT3_SB(sb)->s_mb_buddies_generated++;
851 + EXT3_SB(sb)->s_mb_generation_time += period;
852 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
855 +static int ext3_mb_init_cache(struct page *page)
857 + int blocksize, blocks_per_page, groups_per_page;
858 + int err = 0, i, first_group, first_block;
859 + struct super_block *sb;
860 + struct buffer_head *bhs;
861 + struct buffer_head **bh;
862 + struct inode *inode;
863 + char *data, *bitmap;
865 + mb_debug("init page %lu\n", page->index);
867 + inode = page->mapping->host;
869 + blocksize = 1 << inode->i_blkbits;
870 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
872 + groups_per_page = blocks_per_page >> 1;
873 + if (groups_per_page == 0)
874 + groups_per_page = 1;
876 + /* allocate buffer_heads to read bitmaps */
877 + if (groups_per_page > 1) {
879 + i = sizeof(struct buffer_head *) * groups_per_page;
880 + bh = kmalloc(i, GFP_NOFS);
887 + first_group = page->index * blocks_per_page / 2;
889 + /* read all groups the page covers into the cache */
890 + for (i = 0; i < groups_per_page; i++) {
891 + struct ext3_group_desc * desc;
893 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
897 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
902 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
906 + if (buffer_uptodate(bh[i]))
909 + lock_buffer(bh[i]);
910 + if (buffer_uptodate(bh[i])) {
911 + unlock_buffer(bh[i]);
916 + bh[i]->b_end_io = end_buffer_read_sync;
917 + submit_bh(READ, bh[i]);
918 + mb_debug("read bitmap for group %u\n", first_group + i);
921 + /* wait for I/O completion */
922 + for (i = 0; i < groups_per_page && bh[i]; i++)
923 + wait_on_buffer(bh[i]);
926 + for (i = 0; i < groups_per_page && bh[i]; i++)
927 + if (!buffer_uptodate(bh[i]))
930 + first_block = page->index * blocks_per_page;
931 + for (i = 0; i < blocks_per_page; i++) {
934 + group = (first_block + i) >> 1;
935 + if (group >= EXT3_SB(sb)->s_groups_count)
938 + data = page_address(page) + (i * blocksize);
939 + bitmap = bh[group - first_group]->b_data;
941 + if ((first_block + i) & 1) {
942 + /* this is block of buddy */
943 + mb_debug("put buddy for group %u in page %lu/%x\n",
944 + group, page->index, i * blocksize);
945 + memset(data, 0xff, blocksize);
946 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
947 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
948 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
949 + ext3_mb_generate_buddy(sb, data, bitmap, group);
951 + /* this is block of bitmap */
952 + mb_debug("put bitmap for group %u in page %lu/%x\n",
953 + group, page->index, i * blocksize);
954 + memcpy(data, bitmap, blocksize);
957 + SetPageUptodate(page);
961 + for (i = 0; i < groups_per_page && bh[i]; i++)
969 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
970 + struct ext3_buddy *e3b)
972 + struct ext3_sb_info *sbi = EXT3_SB(sb);
973 + struct inode *inode = sbi->s_buddy_cache;
974 + int blocks_per_page, block, pnum, poff;
977 + mb_debug("load group %u\n", group);
979 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
981 + e3b->bd_blkbits = sb->s_blocksize_bits;
982 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
984 + e3b->bd_group = group;
985 + e3b->bd_buddy_page = NULL;
986 + e3b->bd_bitmap_page = NULL;
989 + pnum = block / blocks_per_page;
990 + poff = block % blocks_per_page;
992 + /* we could use find_or_create_page(), but it locks page
993 + * what we'd like to avoid in fast path ... */
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_bitmap_page = page;
1009 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1010 + mark_page_accessed(page);
1013 + pnum = block / blocks_per_page;
1014 + poff = block % blocks_per_page;
1016 + page = find_get_page(inode->i_mapping, pnum);
1017 + if (page == NULL || !PageUptodate(page)) {
1019 + page_cache_release(page);
1020 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1022 + BUG_ON(page->mapping != inode->i_mapping);
1023 + if (!PageUptodate(page))
1024 + ext3_mb_init_cache(page);
1025 + unlock_page(page);
1028 + if (page == NULL || !PageUptodate(page))
1030 + e3b->bd_buddy_page = page;
1031 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1032 + mark_page_accessed(page);
1034 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1035 + J_ASSERT(e3b->bd_buddy_page != NULL);
1040 + if (e3b->bd_bitmap_page)
1041 + page_cache_release(e3b->bd_bitmap_page);
1042 + if (e3b->bd_buddy_page)
1043 + page_cache_release(e3b->bd_buddy_page);
1044 + e3b->bd_buddy = NULL;
1045 + e3b->bd_bitmap = NULL;
1049 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1051 + if (e3b->bd_bitmap_page)
1052 + page_cache_release(e3b->bd_bitmap_page);
1053 + if (e3b->bd_buddy_page)
1054 + page_cache_release(e3b->bd_buddy_page);
1059 +ext3_lock_group(struct super_block *sb, int group)
1061 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1062 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1066 +ext3_unlock_group(struct super_block *sb, int group)
1068 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1069 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1072 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1077 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1078 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1080 + bb = EXT3_MB_BUDDY(e3b);
1081 + while (order <= e3b->bd_blkbits + 1) {
1082 + block = block >> 1;
1083 + if (!mb_test_bit(block, bb)) {
1084 + /* this block is part of buddy of order 'order' */
1087 + bb += 1 << (e3b->bd_blkbits - order);
1093 +static inline void mb_clear_bits(void *bm, int cur, int len)
1098 + while (cur < len) {
1099 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1100 + /* fast path: clear whole word at once */
1101 + addr = bm + (cur >> 3);
1106 + mb_clear_bit_atomic(cur, bm);
1111 +static inline void mb_set_bits(void *bm, int cur, int len)
1116 + while (cur < len) {
1117 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1118 + /* fast path: clear whole word at once */
1119 + addr = bm + (cur >> 3);
1120 + *addr = 0xffffffff;
1124 + mb_set_bit_atomic(cur, bm);
1129 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1131 + int block = 0, max = 0, order;
1132 + void *buddy, *buddy2;
1134 + mb_check_buddy(e3b);
1136 + e3b->bd_info->bb_free += count;
1137 + if (first < e3b->bd_info->bb_first_free)
1138 + e3b->bd_info->bb_first_free = first;
1140 + /* let's maintain fragments counter */
1142 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1143 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1144 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1146 + e3b->bd_info->bb_fragments--;
1147 + else if (!block && !max)
1148 + e3b->bd_info->bb_fragments++;
1150 + /* let's maintain buddy itself */
1151 + while (count-- > 0) {
1155 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1156 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1157 + e3b->bd_info->bb_counters[order]++;
1159 + /* start of the buddy */
1160 + buddy = mb_find_buddy(e3b, order, &max);
1164 + if (mb_test_bit(block, buddy) ||
1165 + mb_test_bit(block + 1, buddy))
1168 + /* both the buddies are free, try to coalesce them */
1169 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1175 + /* for special purposes, we don't set
1176 + * free bits in bitmap */
1177 + mb_set_bit(block, buddy);
1178 + mb_set_bit(block + 1, buddy);
1180 + e3b->bd_info->bb_counters[order]--;
1181 + e3b->bd_info->bb_counters[order]--;
1183 + block = block >> 1;
1185 + e3b->bd_info->bb_counters[order]++;
1187 + mb_clear_bit(block, buddy2);
1191 + mb_check_buddy(e3b);
1196 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1197 + int needed, struct ext3_free_extent *ex)
1199 + int next = block, max, ord;
1202 + J_ASSERT(ex != NULL);
1204 + buddy = mb_find_buddy(e3b, order, &max);
1206 + J_ASSERT(block < max);
1207 + if (mb_test_bit(block, buddy)) {
1214 + if (likely(order == 0)) {
1215 + /* find actual order */
1216 + order = mb_find_order_for_block(e3b, block);
1217 + block = block >> order;
1220 + ex->fe_len = 1 << order;
1221 + ex->fe_start = block << order;
1222 + ex->fe_group = e3b->bd_group;
1224 + /* calc difference from given start */
1225 + next = next - ex->fe_start;
1226 + ex->fe_len -= next;
1227 + ex->fe_start += next;
1229 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1231 + if (block + 1 >= max)
1234 + next = (block + 1) * (1 << order);
1235 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1238 + ord = mb_find_order_for_block(e3b, next);
1241 + block = next >> order;
1242 + ex->fe_len += 1 << order;
1245 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1246 + return ex->fe_len;
1249 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1251 + int ord, mlen = 0, max = 0, cur;
1252 + int start = ex->fe_start;
1253 + int len = ex->fe_len;
1258 + mb_check_buddy(e3b);
1260 + e3b->bd_info->bb_free -= len;
1261 + if (e3b->bd_info->bb_first_free == start)
1262 + e3b->bd_info->bb_first_free += len;
1264 + /* let's maintain fragments counter */
1266 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1267 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1268 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1270 + e3b->bd_info->bb_fragments++;
1271 + else if (!mlen && !max)
1272 + e3b->bd_info->bb_fragments--;
1274 + /* let's maintain buddy itself */
1276 + ord = mb_find_order_for_block(e3b, start);
1278 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1279 + /* the whole chunk may be allocated at once! */
1281 + buddy = mb_find_buddy(e3b, ord, &max);
1282 + J_ASSERT((start >> ord) < max);
1283 + mb_set_bit(start >> ord, buddy);
1284 + e3b->bd_info->bb_counters[ord]--;
1287 + J_ASSERT(len >= 0);
1291 + /* store for history */
1293 + ret = len | (ord << 16);
1295 + /* we have to split large buddy */
1296 + J_ASSERT(ord > 0);
1297 + buddy = mb_find_buddy(e3b, ord, &max);
1298 + mb_set_bit(start >> ord, buddy);
1299 + e3b->bd_info->bb_counters[ord]--;
1302 + cur = (start >> ord) & ~1U;
1303 + buddy = mb_find_buddy(e3b, ord, &max);
1304 + mb_clear_bit(cur, buddy);
1305 + mb_clear_bit(cur + 1, buddy);
1306 + e3b->bd_info->bb_counters[ord]++;
1307 + e3b->bd_info->bb_counters[ord]++;
1310 + /* now drop all the bits in bitmap */
1311 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1313 + mb_check_buddy(e3b);
1319 + * Must be called under group lock!
1321 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1322 + struct ext3_buddy *e3b)
1324 + unsigned long ret;
1326 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1327 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1329 + ac->ac_status = AC_STATUS_FOUND;
1330 + ac->ac_tail = ret & 0xffff;
1331 + ac->ac_buddy = ret >> 16;
1333 + /* hold in-core structures until allocated
1334 + * blocks are marked non-free in on-disk bitmap */
1335 + ac->ac_buddy_page = e3b->bd_buddy_page;
1336 + page_cache_get(e3b->bd_buddy_page);
1337 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1338 + page_cache_get(e3b->bd_bitmap_page);
1342 + * The routine checks whether found extent is good enough. If it is,
1343 + * then the extent gets marked used and flag is set to the context
1344 + * to stop scanning. Otherwise, the extent is compared with the
1345 + * previous found extent and if new one is better, then it's stored
1346 + * in the context. Later, the best found extent will be used, if
1347 + * mballoc can't find good enough extent.
1349 + * FIXME: real allocation policy is to be designed yet!
1351 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1352 + struct ext3_free_extent *ex,
1353 + struct ext3_buddy *e3b)
1355 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1356 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1358 + J_ASSERT(ex->fe_len > 0);
1359 + J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1360 + J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1365 + * The special case - take what you catch first
1367 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1369 + ext3_mb_use_best_found(ac, e3b);
1374 + * Let's check whether the chunk is good enough
1376 + if (ex->fe_len == gex->fe_len) {
1378 + ext3_mb_use_best_found(ac, e3b);
1383 + * If this is first found extent, just store it in the context
1385 + if (bex->fe_len == 0) {
1391 + * If new found extent is better, store it in the context
1393 + if (bex->fe_len < gex->fe_len) {
1394 + /* if the request isn't satisfied, any found extent
1395 + * larger than previous best one is better */
1396 + if (ex->fe_len > bex->fe_len)
1398 + } else if (ex->fe_len > gex->fe_len) {
1399 + /* if the request is satisfied, then we try to find
1400 + * an extent that still satisfy the request, but is
1401 + * smaller than previous one */
1406 + * Let's scan at least few extents and don't pick up a first one
1408 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1409 + ac->ac_status = AC_STATUS_BREAK;
1412 + * We don't want to scan for a whole year
1414 + if (ac->ac_found > ext3_mb_max_to_scan)
1415 + ac->ac_status = AC_STATUS_BREAK;
1418 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1419 + struct ext3_buddy *e3b)
1421 + struct ext3_free_extent ex = ac->ac_b_ex;
1422 + int group = ex.fe_group, max, err;
1424 + J_ASSERT(ex.fe_len > 0);
1425 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1429 + ext3_lock_group(ac->ac_sb, group);
1430 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1434 + ext3_mb_use_best_found(ac, e3b);
1437 + ext3_unlock_group(ac->ac_sb, group);
1439 + ext3_mb_release_desc(e3b);
1444 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1445 + struct ext3_buddy *e3b)
1447 + int group = ac->ac_g_ex.fe_group, max, err;
1448 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1449 + struct ext3_super_block *es = sbi->s_es;
1450 + struct ext3_free_extent ex;
1452 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1456 + ext3_lock_group(ac->ac_sb, group);
1457 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1458 + ac->ac_g_ex.fe_len, &ex);
1460 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1461 + unsigned long start;
1462 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1463 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1464 + if (start % sbi->s_stripe == 0) {
1467 + ext3_mb_use_best_found(ac, e3b);
1469 + } else if (max >= ac->ac_g_ex.fe_len) {
1470 + J_ASSERT(ex.fe_len > 0);
1471 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1472 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1475 + ext3_mb_use_best_found(ac, e3b);
1476 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1477 + /* Sometimes, caller may want to merge even small
1478 + * number of blocks to an existing extent */
1479 + J_ASSERT(ex.fe_len > 0);
1480 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1481 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1484 + ext3_mb_use_best_found(ac, e3b);
1486 + ext3_unlock_group(ac->ac_sb, group);
1488 + ext3_mb_release_desc(e3b);
1494 + * The routine scans buddy structures (not bitmap!) from given order
1495 + * to max order and tries to find big enough chunk to satisfy the req
1497 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1498 + struct ext3_buddy *e3b)
1500 + struct super_block *sb = ac->ac_sb;
1501 + struct ext3_group_info *grp = e3b->bd_info;
1505 + J_ASSERT(ac->ac_2order > 0);
1506 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1507 + if (grp->bb_counters[i] == 0)
1510 + buddy = mb_find_buddy(e3b, i, &max);
1511 + if (buddy == NULL) {
1512 + printk(KERN_ALERT "looking for wrong order?\n");
1516 + k = mb_find_next_zero_bit(buddy, max, 0);
1517 + J_ASSERT(k < max);
1521 + ac->ac_b_ex.fe_len = 1 << i;
1522 + ac->ac_b_ex.fe_start = k << i;
1523 + ac->ac_b_ex.fe_group = e3b->bd_group;
1525 + ext3_mb_use_best_found(ac, e3b);
1526 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1528 + if (unlikely(ext3_mb_stats))
1529 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1536 + * The routine scans the group and measures all found extents.
1537 + * In order to optimize scanning, caller must pass number of
1538 + * free blocks in the group, so the routine can know upper limit.
1540 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1541 + struct ext3_buddy *e3b)
1543 + struct super_block *sb = ac->ac_sb;
1544 + void *bitmap = EXT3_MB_BITMAP(e3b);
1545 + struct ext3_free_extent ex;
1548 + free = e3b->bd_info->bb_free;
1549 + J_ASSERT(free > 0);
1551 + i = e3b->bd_info->bb_first_free;
1553 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1554 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1555 + if (i >= sb->s_blocksize * 8) {
1556 + J_ASSERT(free == 0);
1560 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1561 + J_ASSERT(ex.fe_len > 0);
1562 + J_ASSERT(free >= ex.fe_len);
1564 + ext3_mb_measure_extent(ac, &ex, e3b);
1567 + free -= ex.fe_len;
1572 + * This is a special case for storages like raid5
1573 + * we try to find stripe-aligned chunks for stripe-size requests
1575 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1576 + struct ext3_buddy *e3b)
1578 + struct super_block *sb = ac->ac_sb;
1579 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1580 + void *bitmap = EXT3_MB_BITMAP(e3b);
1581 + struct ext3_free_extent ex;
1582 + unsigned long i, max;
1584 + J_ASSERT(sbi->s_stripe != 0);
1586 + /* find first stripe-aligned block */
1587 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb) +
1588 + le32_to_cpu(sbi->s_es->s_first_data_block);
1589 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1590 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block)) %
1591 + EXT3_BLOCKS_PER_GROUP(sb);
1593 + while (i < sb->s_blocksize * 8) {
1594 + if (!mb_test_bit(i, bitmap)) {
1595 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1596 + if (max >= sbi->s_stripe) {
1599 + ext3_mb_use_best_found(ac, e3b);
1603 + i += sbi->s_stripe;
1607 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1608 + int group, int cr)
1610 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1611 + unsigned free, fragments, i, bits;
1613 + J_ASSERT(cr >= 0 && cr < 4);
1614 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1616 + free = grp->bb_free;
1617 + fragments = grp->bb_fragments;
1620 + if (fragments == 0)
1625 + J_ASSERT(ac->ac_2order != 0);
1626 + bits = ac->ac_sb->s_blocksize_bits + 1;
1627 + for (i = ac->ac_2order; i <= bits; i++)
1628 + if (grp->bb_counters[i] > 0)
1632 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1636 + if (free >= ac->ac_g_ex.fe_len)
1648 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1649 + unsigned long goal, int *len, int flags, int *errp)
1651 + struct buffer_head *bitmap_bh = NULL;
1652 + struct ext3_allocation_context ac;
1653 + int i, group, block, cr, err = 0;
1654 + struct ext3_group_desc *gdp;
1655 + struct ext3_super_block *es;
1656 + struct buffer_head *gdp_bh;
1657 + struct ext3_sb_info *sbi;
1658 + struct super_block *sb;
1659 + struct ext3_buddy e3b;
1661 + J_ASSERT(len != NULL);
1662 + J_ASSERT(*len > 0);
1666 + printk("ext3_mb_new_nblocks: nonexistent device");
1670 + if (!test_opt(sb, MBALLOC)) {
1671 + static int ext3_mballoc_warning = 0;
1672 + if (ext3_mballoc_warning == 0) {
1673 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1674 + "mballoc disabled!\n");
1675 + ext3_mballoc_warning++;
1678 + err = ext3_new_block_old(handle, inode, goal, errp);
1682 + ext3_mb_poll_new_transaction(sb, handle);
1684 + sbi = EXT3_SB(sb);
1685 + es = EXT3_SB(sb)->s_es;
1688 + * We can't allocate > group size
1690 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1691 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1693 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1694 + /* someone asks for non-reserved blocks */
1696 + err = ext3_mb_reserve_blocks(sb, 1);
1703 + ac.ac_buddy_page = NULL;
1704 + ac.ac_bitmap_page = NULL;
1707 + * Check quota for allocation of this blocks.
1709 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1717 + /* start searching from the goal */
1718 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1719 + goal >= le32_to_cpu(es->s_blocks_count))
1720 + goal = le32_to_cpu(es->s_first_data_block);
1721 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1722 + EXT3_BLOCKS_PER_GROUP(sb);
1723 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1724 + EXT3_BLOCKS_PER_GROUP(sb));
1726 + /* set up allocation goals */
1727 + ac.ac_b_ex.fe_group = 0;
1728 + ac.ac_b_ex.fe_start = 0;
1729 + ac.ac_b_ex.fe_len = 0;
1730 + ac.ac_status = AC_STATUS_CONTINUE;
1731 + ac.ac_groups_scanned = 0;
1732 + ac.ac_ex_scanned = 0;
1734 + ac.ac_sb = inode->i_sb;
1735 + ac.ac_g_ex.fe_group = group;
1736 + ac.ac_g_ex.fe_start = block;
1737 + ac.ac_g_ex.fe_len = *len;
1738 + ac.ac_flags = flags;
1740 + ac.ac_criteria = 0;
1742 + if (*len == 1 && sbi->s_stripe) {
1743 + /* looks like a metadata, let's use a dirty hack for raid5
1744 + * move all metadata in first groups in hope to hit cached
1745 + * sectors and thus avoid read-modify cycles in raid5 */
1746 + ac.ac_g_ex.fe_group = group = 0;
1749 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1751 + if (i >= ext3_mb_order2_reqs) {
1753 + if ((*len & (~(1 << i))) == 0)
1757 + /* first, try the goal */
1758 + err = ext3_mb_find_by_goal(&ac, &e3b);
1761 + if (ac.ac_status == AC_STATUS_FOUND)
1764 + /* Let's just scan groups to find more-less suitable blocks */
1765 + cr = ac.ac_2order ? 0 : 1;
1767 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1768 + ac.ac_criteria = cr;
1769 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1770 + if (group == EXT3_SB(sb)->s_groups_count)
1773 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1774 + /* we need full data about the group
1775 + * to make a good selection */
1776 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1779 + ext3_mb_release_desc(&e3b);
1782 + /* check is group good for our criteries */
1783 + if (!ext3_mb_good_group(&ac, group, cr))
1786 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1790 + ext3_lock_group(sb, group);
1791 + if (!ext3_mb_good_group(&ac, group, cr)) {
1792 + /* someone did allocation from this group */
1793 + ext3_unlock_group(sb, group);
1794 + ext3_mb_release_desc(&e3b);
1798 + ac.ac_groups_scanned++;
1800 + ext3_mb_simple_scan_group(&ac, &e3b);
1801 + else if (cr == 1 && *len == sbi->s_stripe)
1802 + ext3_mb_scan_aligned(&ac, &e3b);
1804 + ext3_mb_complex_scan_group(&ac, &e3b);
1806 + ext3_unlock_group(sb, group);
1808 + ext3_mb_release_desc(&e3b);
1810 + if (ac.ac_status != AC_STATUS_CONTINUE)
1815 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1816 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1818 + * We've been searching too long. Let's try to allocate
1819 + * the best chunk we've found so far
1822 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1823 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1824 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1825 + ac.ac_g_ex.fe_len);*/
1826 + ext3_mb_try_best_found(&ac, &e3b);
1827 + if (ac.ac_status != AC_STATUS_FOUND) {
1829 + * Someone more lucky has already allocated it.
1830 + * The only thing we can do is just take first
1832 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1834 + ac.ac_b_ex.fe_group = 0;
1835 + ac.ac_b_ex.fe_start = 0;
1836 + ac.ac_b_ex.fe_len = 0;
1837 + ac.ac_status = AC_STATUS_CONTINUE;
1838 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1844 + if (ac.ac_status != AC_STATUS_FOUND) {
1846 + * We aren't lucky definitely
1848 + DQUOT_FREE_BLOCK(inode, *len);
1852 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1853 + ac.ac_status, ac.ac_flags);
1854 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1855 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1856 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1857 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1858 + sbi->s_blocks_reserved, ac.ac_found);
1859 + printk("EXT3-fs: groups: ");
1860 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1861 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1868 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1870 + /* good news - free block(s) have been found. now it's time
1871 + * to mark block(s) in good old journaled bitmap */
1872 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1873 + + ac.ac_b_ex.fe_start
1874 + + le32_to_cpu(es->s_first_data_block);
1876 + /* we made a desicion, now mark found blocks in good old
1877 + * bitmap to be journaled */
1879 + ext3_debug("using block group %d(%d)\n",
1880 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1882 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1888 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1894 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1900 + err = ext3_journal_get_write_access(handle, gdp_bh);
1904 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1905 + + ac.ac_b_ex.fe_start
1906 + + le32_to_cpu(es->s_first_data_block);
1908 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1909 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1910 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1911 + EXT3_SB(sb)->s_itb_per_group))
1912 + ext3_error(sb, "ext3_new_block",
1913 + "Allocating block in system zone - "
1914 + "block = %u", block);
1915 +#ifdef AGGRESSIVE_CHECK
1916 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1917 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1919 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1921 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1922 + gdp->bg_free_blocks_count =
1923 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1924 + - ac.ac_b_ex.fe_len);
1925 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1926 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1928 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1931 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1937 + brelse(bitmap_bh);
1939 + /* drop non-allocated, but dquote'd blocks */
1940 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1941 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1943 + *len = ac.ac_b_ex.fe_len;
1944 + J_ASSERT(*len > 0);
1945 + J_ASSERT(block != 0);
1949 + /* if we've already allocated something, roll it back */
1950 + if (ac.ac_status == AC_STATUS_FOUND) {
1951 + /* FIXME: free blocks here */
1954 + DQUOT_FREE_BLOCK(inode, *len);
1955 + brelse(bitmap_bh);
1959 + if (ac.ac_buddy_page)
1960 + page_cache_release(ac.ac_buddy_page);
1961 + if (ac.ac_bitmap_page)
1962 + page_cache_release(ac.ac_bitmap_page);
1964 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1965 + /* block wasn't reserved before and we reserved it
1966 + * at the beginning of allocation. it doesn't matter
1967 + * whether we allocated anything or we failed: time
1968 + * to release reservation. NOTE: because I expect
1969 + * any multiblock request from delayed allocation
1970 + * path only, here is single block always */
1971 + ext3_mb_release_blocks(sb, 1);
1974 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1975 + atomic_inc(&sbi->s_bal_reqs);
1976 + atomic_add(*len, &sbi->s_bal_allocated);
1977 + if (*len >= ac.ac_g_ex.fe_len)
1978 + atomic_inc(&sbi->s_bal_success);
1979 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1980 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1981 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1982 + atomic_inc(&sbi->s_bal_goals);
1983 + if (ac.ac_found > ext3_mb_max_to_scan)
1984 + atomic_inc(&sbi->s_bal_breaks);
1987 + ext3_mb_store_history(sb, inode->i_ino, &ac);
1991 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1993 +#ifdef EXT3_MB_HISTORY
1994 +struct ext3_mb_proc_session {
1995 + struct ext3_mb_history *history;
1996 + struct super_block *sb;
2001 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
2002 + struct ext3_mb_history *hs,
2005 + if (hs == s->history + s->max)
2007 + if (!first && hs == s->history + s->start)
2009 + while (hs->goal.fe_len == 0) {
2011 + if (hs == s->history + s->max)
2013 + if (hs == s->history + s->start)
2019 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2021 + struct ext3_mb_proc_session *s = seq->private;
2022 + struct ext3_mb_history *hs;
2026 + return SEQ_START_TOKEN;
2027 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2030 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2034 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2036 + struct ext3_mb_proc_session *s = seq->private;
2037 + struct ext3_mb_history *hs = v;
2040 + if (v == SEQ_START_TOKEN)
2041 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2043 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2046 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2048 + struct ext3_mb_history *hs = v;
2049 + char buf[20], buf2[20];
2051 + if (v == SEQ_START_TOKEN) {
2052 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2053 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2054 + "merge", "tail", "broken");
2058 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2059 + hs->goal.fe_start, hs->goal.fe_len);
2060 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2061 + hs->result.fe_start, hs->result.fe_len);
2062 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2063 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2064 + hs->cr, hs->merged ? "M" : "", hs->tail,
2065 + hs->buddy ? 1 << hs->buddy : 0);
2069 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2073 +static struct seq_operations ext3_mb_seq_history_ops = {
2074 + .start = ext3_mb_seq_history_start,
2075 + .next = ext3_mb_seq_history_next,
2076 + .stop = ext3_mb_seq_history_stop,
2077 + .show = ext3_mb_seq_history_show,
2080 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2082 + struct super_block *sb = PDE(inode)->data;
2083 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2084 + struct ext3_mb_proc_session *s;
2087 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2090 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2091 + s->history = kmalloc(size, GFP_KERNEL);
2097 + spin_lock(&sbi->s_mb_history_lock);
2098 + memcpy(s->history, sbi->s_mb_history, size);
2099 + s->max = sbi->s_mb_history_max;
2100 + s->start = sbi->s_mb_history_cur % s->max;
2101 + spin_unlock(&sbi->s_mb_history_lock);
2103 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2105 + struct seq_file *m = (struct seq_file *)file->private_data;
2108 + kfree(s->history);
2115 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2117 + struct seq_file *seq = (struct seq_file *)file->private_data;
2118 + struct ext3_mb_proc_session *s = seq->private;
2119 + kfree(s->history);
2121 + return seq_release(inode, file);
2124 +static struct file_operations ext3_mb_seq_history_fops = {
2125 + .owner = THIS_MODULE,
2126 + .open = ext3_mb_seq_history_open,
2128 + .llseek = seq_lseek,
2129 + .release = ext3_mb_seq_history_release,
2132 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2134 + struct super_block *sb = seq->private;
2135 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2138 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2142 + return (void *) group;
2145 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2147 + struct super_block *sb = seq->private;
2148 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2152 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2155 + return (void *) group;;
2158 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2160 + struct super_block *sb = seq->private;
2161 + long group = (long) v, i;
2163 + struct ext3_group_info info;
2164 + unsigned short counters[16];
2169 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2170 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2171 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2172 + "2^11", "2^12", "2^13");
2174 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2175 + sizeof(struct ext3_group_info);
2176 + ext3_lock_group(sb, group);
2177 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2178 + ext3_unlock_group(sb, group);
2180 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2183 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2184 + sg.info.bb_fragments, sg.info.bb_first_free);
2185 + for (i = 0; i <= 13; i++)
2186 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2187 + sg.info.bb_counters[i] : 0);
2188 + seq_printf(seq, " ]\n");
2193 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2197 +static struct seq_operations ext3_mb_seq_groups_ops = {
2198 + .start = ext3_mb_seq_groups_start,
2199 + .next = ext3_mb_seq_groups_next,
2200 + .stop = ext3_mb_seq_groups_stop,
2201 + .show = ext3_mb_seq_groups_show,
2204 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2206 + struct super_block *sb = PDE(inode)->data;
2209 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2211 + struct seq_file *m = (struct seq_file *)file->private_data;
2218 +static struct file_operations ext3_mb_seq_groups_fops = {
2219 + .owner = THIS_MODULE,
2220 + .open = ext3_mb_seq_groups_open,
2222 + .llseek = seq_lseek,
2223 + .release = seq_release,
2226 +static void ext3_mb_history_release(struct super_block *sb)
2228 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2231 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2232 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2233 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2234 + remove_proc_entry(name, proc_root_ext3);
2236 + if (sbi->s_mb_history)
2237 + kfree(sbi->s_mb_history);
2240 +static void ext3_mb_history_init(struct super_block *sb)
2242 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2246 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2247 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2248 + if (sbi->s_mb_proc != NULL) {
2249 + struct proc_dir_entry *p;
2250 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2252 + p->proc_fops = &ext3_mb_seq_history_fops;
2255 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2257 + p->proc_fops = &ext3_mb_seq_groups_fops;
2262 + sbi->s_mb_history_max = 1000;
2263 + sbi->s_mb_history_cur = 0;
2264 + spin_lock_init(&sbi->s_mb_history_lock);
2265 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2266 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2267 + memset(sbi->s_mb_history, 0, i);
2268 + /* if we can't allocate history, then we simple won't use it */
2272 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2273 + struct ext3_allocation_context *ac)
2275 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2276 + struct ext3_mb_history h;
2278 + if (likely(sbi->s_mb_history == NULL))
2281 + h.pid = current->pid;
2283 + h.goal = ac->ac_g_ex;
2284 + h.result = ac->ac_b_ex;
2285 + h.found = ac->ac_found;
2286 + h.cr = ac->ac_criteria;
2287 + h.groups = ac->ac_groups_scanned;
2288 + h.tail = ac->ac_tail;
2289 + h.buddy = ac->ac_buddy;
2291 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2292 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2295 + spin_lock(&sbi->s_mb_history_lock);
2296 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2297 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2298 + sbi->s_mb_history_cur = 0;
2299 + spin_unlock(&sbi->s_mb_history_lock);
2303 +#define ext3_mb_history_release(sb)
2304 +#define ext3_mb_history_init(sb)
2307 +int ext3_mb_init_backend(struct super_block *sb)
2309 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2310 + int i, j, len, metalen;
2311 + int num_meta_group_infos =
2312 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2313 + EXT3_DESC_PER_BLOCK_BITS(sb);
2314 + struct ext3_group_info **meta_group_info;
2316 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2317 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2318 + * So a two level scheme suffices for now. */
2319 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2320 + num_meta_group_infos, GFP_KERNEL);
2321 + if (sbi->s_group_info == NULL) {
2322 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2325 + sbi->s_buddy_cache = new_inode(sb);
2326 + if (sbi->s_buddy_cache == NULL) {
2327 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2331 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2332 + for (i = 0; i < num_meta_group_infos; i++) {
2333 + if ((i + 1) == num_meta_group_infos)
2334 + metalen = sizeof(*meta_group_info) *
2335 + (sbi->s_groups_count -
2336 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2337 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2338 + if (meta_group_info == NULL) {
2339 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2341 + goto err_freemeta;
2343 + sbi->s_group_info[i] = meta_group_info;
2347 + * calculate needed size. if change bb_counters size,
2348 + * don't forget about ext3_mb_generate_buddy()
2350 + len = sizeof(struct ext3_group_info);
2351 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2352 + for (i = 0; i < sbi->s_groups_count; i++) {
2353 + struct ext3_group_desc * desc;
2356 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2357 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2359 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2360 + if (meta_group_info[j] == NULL) {
2361 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2363 + goto err_freebuddy;
2365 + desc = ext3_get_group_desc(sb, i, NULL);
2366 + if (desc == NULL) {
2367 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2368 + goto err_freebuddy;
2370 + memset(meta_group_info[j], 0, len);
2371 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2372 + &meta_group_info[j]->bb_state);
2373 + meta_group_info[j]->bb_free =
2374 + le16_to_cpu(desc->bg_free_blocks_count);
2381 + kfree(EXT3_GROUP_INFO(sb, i));
2384 + i = num_meta_group_infos;
2387 + kfree(sbi->s_group_info[i]);
2388 + iput(sbi->s_buddy_cache);
2390 + kfree(sbi->s_group_info);
2394 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2396 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2397 + struct inode *root = sb->s_root->d_inode;
2398 + unsigned i, offset, max;
2399 + struct dentry *dentry;
2401 + if (!test_opt(sb, MBALLOC))
2404 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2406 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2407 + if (sbi->s_mb_offsets == NULL) {
2408 + clear_opt(sbi->s_mount_opt, MBALLOC);
2411 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2412 + if (sbi->s_mb_maxs == NULL) {
2413 + clear_opt(sbi->s_mount_opt, MBALLOC);
2414 + kfree(sbi->s_mb_maxs);
2418 + /* order 0 is regular bitmap */
2419 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2420 + sbi->s_mb_offsets[0] = 0;
2424 + max = sb->s_blocksize << 2;
2426 + sbi->s_mb_offsets[i] = offset;
2427 + sbi->s_mb_maxs[i] = max;
2428 + offset += 1 << (sb->s_blocksize_bits - i);
2431 + } while (i <= sb->s_blocksize_bits + 1);
2433 + /* init file for buddy data */
2434 + if ((i = ext3_mb_init_backend(sb))) {
2435 + clear_opt(sbi->s_mount_opt, MBALLOC);
2436 + kfree(sbi->s_mb_offsets);
2437 + kfree(sbi->s_mb_maxs);
2441 + spin_lock_init(&sbi->s_reserve_lock);
2442 + spin_lock_init(&sbi->s_md_lock);
2443 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2444 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2445 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2446 + spin_lock_init(&sbi->s_bal_lock);
2448 + /* remove old on-disk buddy file */
2449 + down(&root->i_sem);
2450 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2451 + if (dentry->d_inode != NULL) {
2452 + i = vfs_unlink(root, dentry);
2454 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2459 + ext3_mb_history_init(sb);
2461 + printk("EXT3-fs: mballoc enabled\n");
2465 +int ext3_mb_release(struct super_block *sb)
2467 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2468 + int i, num_meta_group_infos;
2470 + if (!test_opt(sb, MBALLOC))
2473 + /* release freed, non-committed blocks */
2474 + spin_lock(&sbi->s_md_lock);
2475 + list_splice_init(&sbi->s_closed_transaction,
2476 + &sbi->s_committed_transaction);
2477 + list_splice_init(&sbi->s_active_transaction,
2478 + &sbi->s_committed_transaction);
2479 + spin_unlock(&sbi->s_md_lock);
2480 + ext3_mb_free_committed_blocks(sb);
2482 + if (sbi->s_group_info) {
2483 + for (i = 0; i < sbi->s_groups_count; i++)
2484 + kfree(EXT3_GROUP_INFO(sb, i));
2485 + num_meta_group_infos = (sbi->s_groups_count +
2486 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2487 + EXT3_DESC_PER_BLOCK_BITS(sb);
2488 + for (i = 0; i < num_meta_group_infos; i++)
2489 + kfree(sbi->s_group_info[i]);
2490 + kfree(sbi->s_group_info);
2492 + if (sbi->s_mb_offsets)
2493 + kfree(sbi->s_mb_offsets);
2494 + if (sbi->s_mb_maxs)
2495 + kfree(sbi->s_mb_maxs);
2496 + if (sbi->s_buddy_cache)
2497 + iput(sbi->s_buddy_cache);
2498 + if (sbi->s_blocks_reserved)
2499 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2500 + sbi->s_blocks_reserved);
2501 + if (ext3_mb_stats) {
2502 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2503 + atomic_read(&sbi->s_bal_allocated),
2504 + atomic_read(&sbi->s_bal_reqs),
2505 + atomic_read(&sbi->s_bal_success));
2506 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2507 + "%u 2^N hits, %u breaks\n",
2508 + atomic_read(&sbi->s_bal_ex_scanned),
2509 + atomic_read(&sbi->s_bal_goals),
2510 + atomic_read(&sbi->s_bal_2orders),
2511 + atomic_read(&sbi->s_bal_breaks));
2512 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2513 + sbi->s_mb_buddies_generated++,
2514 + sbi->s_mb_generation_time);
2517 + ext3_mb_history_release(sb);
2522 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2524 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2525 + int err, i, count = 0, count2 = 0;
2526 + struct ext3_free_metadata *md;
2527 + struct ext3_buddy e3b;
2529 + if (list_empty(&sbi->s_committed_transaction))
2532 + /* there is committed blocks to be freed yet */
2534 + /* get next array of blocks */
2536 + spin_lock(&sbi->s_md_lock);
2537 + if (!list_empty(&sbi->s_committed_transaction)) {
2538 + md = list_entry(sbi->s_committed_transaction.next,
2539 + struct ext3_free_metadata, list);
2540 + list_del(&md->list);
2542 + spin_unlock(&sbi->s_md_lock);
2547 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2548 + md->num, md->group, md);
2550 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2551 + /* we expect to find existing buddy because it's pinned */
2554 + /* there are blocks to put in buddy to make them really free */
2557 + ext3_lock_group(sb, md->group);
2558 + for (i = 0; i < md->num; i++) {
2559 + mb_debug(" %u", md->blocks[i]);
2560 + mb_free_blocks(&e3b, md->blocks[i], 1);
2563 + ext3_unlock_group(sb, md->group);
2565 + /* balance refcounts from ext3_mb_free_metadata() */
2566 + page_cache_release(e3b.bd_buddy_page);
2567 + page_cache_release(e3b.bd_bitmap_page);
2570 + ext3_mb_release_desc(&e3b);
2573 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2576 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2578 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2580 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2583 + /* new transaction! time to close last one and free blocks for
2584 + * committed transaction. we know that only transaction can be
2585 + * active, so previos transaction can be being logged and we
2586 + * know that transaction before previous is known to be already
2587 + * logged. this means that now we may free blocks freed in all
2588 + * transactions before previous one. hope I'm clear enough ... */
2590 + spin_lock(&sbi->s_md_lock);
2591 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2592 + mb_debug("new transaction %lu, old %lu\n",
2593 + (unsigned long) handle->h_transaction->t_tid,
2594 + (unsigned long) sbi->s_last_transaction);
2595 + list_splice_init(&sbi->s_closed_transaction,
2596 + &sbi->s_committed_transaction);
2597 + list_splice_init(&sbi->s_active_transaction,
2598 + &sbi->s_closed_transaction);
2599 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2601 + spin_unlock(&sbi->s_md_lock);
2603 + ext3_mb_free_committed_blocks(sb);
2606 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2607 + int group, int block, int count)
2609 + struct ext3_group_info *db = e3b->bd_info;
2610 + struct super_block *sb = e3b->bd_sb;
2611 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2612 + struct ext3_free_metadata *md;
2615 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2616 + J_ASSERT(e3b->bd_buddy_page != NULL);
2618 + ext3_lock_group(sb, group);
2619 + for (i = 0; i < count; i++) {
2620 + md = db->bb_md_cur;
2621 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2622 + db->bb_md_cur = NULL;
2627 + ext3_unlock_group(sb, group);
2628 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2632 + md->group = group;
2634 + ext3_lock_group(sb, group);
2635 + if (db->bb_md_cur == NULL) {
2636 + spin_lock(&sbi->s_md_lock);
2637 + list_add(&md->list, &sbi->s_active_transaction);
2638 + spin_unlock(&sbi->s_md_lock);
2639 + /* protect buddy cache from being freed,
2640 + * otherwise we'll refresh it from
2641 + * on-disk bitmap and lose not-yet-available
2643 + page_cache_get(e3b->bd_buddy_page);
2644 + page_cache_get(e3b->bd_bitmap_page);
2645 + db->bb_md_cur = md;
2646 + db->bb_tid = handle->h_transaction->t_tid;
2647 + mb_debug("new md 0x%p for group %u\n",
2651 + md = db->bb_md_cur;
2655 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2656 + md->blocks[md->num] = block + i;
2658 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2659 + /* no more space, put full container on a sb's list */
2660 + db->bb_md_cur = NULL;
2663 + ext3_unlock_group(sb, group);
2667 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2668 + unsigned long block, unsigned long count,
2669 + int metadata, int *freed)
2671 + struct buffer_head *bitmap_bh = NULL;
2672 + struct ext3_group_desc *gdp;
2673 + struct ext3_super_block *es;
2674 + unsigned long bit, overflow;
2675 + struct buffer_head *gd_bh;
2676 + unsigned long block_group;
2677 + struct ext3_sb_info *sbi;
2678 + struct super_block *sb;
2679 + struct ext3_buddy e3b;
2685 + printk ("ext3_free_blocks: nonexistent device");
2689 + ext3_mb_poll_new_transaction(sb, handle);
2691 + sbi = EXT3_SB(sb);
2692 + es = EXT3_SB(sb)->s_es;
2693 + if (block < le32_to_cpu(es->s_first_data_block) ||
2694 + block + count < block ||
2695 + block + count > le32_to_cpu(es->s_blocks_count)) {
2696 + ext3_error (sb, "ext3_free_blocks",
2697 + "Freeing blocks not in datazone - "
2698 + "block = %lu, count = %lu", block, count);
2699 + goto error_return;
2702 + ext3_debug("freeing block %lu\n", block);
2706 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2707 + EXT3_BLOCKS_PER_GROUP(sb);
2708 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2709 + EXT3_BLOCKS_PER_GROUP(sb);
2711 + * Check to see if we are freeing blocks across a group
2714 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2715 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2716 + count -= overflow;
2718 + brelse(bitmap_bh);
2719 + bitmap_bh = read_block_bitmap(sb, block_group);
2721 + goto error_return;
2722 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2724 + goto error_return;
2726 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2727 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2728 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2729 + EXT3_SB(sb)->s_itb_per_group) ||
2730 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2731 + EXT3_SB(sb)->s_itb_per_group))
2732 + ext3_error (sb, "ext3_free_blocks",
2733 + "Freeing blocks in system zones - "
2734 + "Block = %lu, count = %lu",
2737 + BUFFER_TRACE(bitmap_bh, "getting write access");
2738 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2740 + goto error_return;
2743 + * We are about to modify some metadata. Call the journal APIs
2744 + * to unshare ->b_data if a currently-committing transaction is
2747 + BUFFER_TRACE(gd_bh, "get_write_access");
2748 + err = ext3_journal_get_write_access(handle, gd_bh);
2750 + goto error_return;
2752 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2754 + goto error_return;
2756 +#ifdef AGGRESSIVE_CHECK
2759 + for (i = 0; i < count; i++)
2760 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2763 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2765 + /* We dirtied the bitmap block */
2766 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2767 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2770 + /* blocks being freed are metadata. these blocks shouldn't
2771 + * be used until this transaction is committed */
2772 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2774 + ext3_lock_group(sb, block_group);
2775 + mb_free_blocks(&e3b, bit, count);
2776 + ext3_unlock_group(sb, block_group);
2779 + spin_lock(sb_bgl_lock(sbi, block_group));
2780 + gdp->bg_free_blocks_count =
2781 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2782 + spin_unlock(sb_bgl_lock(sbi, block_group));
2783 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2785 + ext3_mb_release_desc(&e3b);
2789 + /* And the group descriptor block */
2790 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2791 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2792 + if (!err) err = ret;
2794 + if (overflow && !err) {
2801 + brelse(bitmap_bh);
2802 + ext3_std_error(sb, err);
2806 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2808 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2809 + int free, ret = -ENOSPC;
2811 + BUG_ON(blocks < 0);
2812 + spin_lock(&sbi->s_reserve_lock);
2813 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2814 + if (blocks <= free - sbi->s_blocks_reserved) {
2815 + sbi->s_blocks_reserved += blocks;
2818 + spin_unlock(&sbi->s_reserve_lock);
2822 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2824 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2826 + BUG_ON(blocks < 0);
2827 + spin_lock(&sbi->s_reserve_lock);
2828 + sbi->s_blocks_reserved -= blocks;
2829 + WARN_ON(sbi->s_blocks_reserved < 0);
2830 + if (sbi->s_blocks_reserved < 0)
2831 + sbi->s_blocks_reserved = 0;
2832 + spin_unlock(&sbi->s_reserve_lock);
2835 +int ext3_new_block(handle_t *handle, struct inode *inode,
2836 + unsigned long goal, int *errp)
2840 + if (!test_opt(inode->i_sb, MBALLOC)) {
2841 + ret = ext3_new_block_old(handle, inode, goal, errp);
2845 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2851 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2852 + unsigned long block, unsigned long count, int metadata)
2854 + struct super_block *sb;
2858 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2859 + ext3_free_blocks_sb(handle, sb, block, count, &freed);
2861 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2863 + DQUOT_FREE_BLOCK(inode, freed);
2867 +#define EXT3_ROOT "ext3"
2868 +#define EXT3_MB_STATS_NAME "mb_stats"
2869 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2870 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2871 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2873 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2874 + int count, int *eof, void *data)
2882 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2887 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2888 + unsigned long count, void *data)
2892 + if (count >= sizeof(str)) {
2893 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2894 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2895 + return -EOVERFLOW;
2898 + if (copy_from_user(str, buffer, count))
2901 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2902 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2906 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2907 + int count, int *eof, void *data)
2915 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2920 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2921 + unsigned long count, void *data)
2926 + if (count >= sizeof(str)) {
2927 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2928 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2929 + return -EOVERFLOW;
2932 + if (copy_from_user(str, buffer, count))
2935 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2936 + value = simple_strtol(str, NULL, 0);
2940 + ext3_mb_max_to_scan = value;
2945 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2946 + int count, int *eof, void *data)
2954 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2959 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2960 + unsigned long count, void *data)
2965 + if (count >= sizeof(str)) {
2966 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2967 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2968 + return -EOVERFLOW;
2971 + if (copy_from_user(str, buffer, count))
2974 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2975 + value = simple_strtol(str, NULL, 0);
2979 + ext3_mb_min_to_scan = value;
2984 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2985 + int count, int *eof, void *data)
2993 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
2998 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2999 + unsigned long count, void *data)
3004 + if (count >= sizeof(str)) {
3005 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
3006 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
3007 + return -EOVERFLOW;
3010 + if (copy_from_user(str, buffer, count))
3013 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
3014 + value = simple_strtol(str, NULL, 0);
3018 + ext3_mb_order2_reqs = value;
3023 +int __init init_ext3_proc(void)
3025 + struct proc_dir_entry *proc_ext3_mb_stats;
3026 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3027 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3028 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3030 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3031 + if (proc_root_ext3 == NULL) {
3032 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3036 + /* Initialize EXT3_MB_STATS_NAME */
3037 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3038 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3039 + if (proc_ext3_mb_stats == NULL) {
3040 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3041 + EXT3_MB_STATS_NAME);
3042 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3046 + proc_ext3_mb_stats->data = NULL;
3047 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3048 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3050 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3051 + proc_ext3_mb_max_to_scan = create_proc_entry(
3052 + EXT3_MB_MAX_TO_SCAN_NAME,
3053 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3054 + if (proc_ext3_mb_max_to_scan == NULL) {
3055 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3056 + EXT3_MB_MAX_TO_SCAN_NAME);
3057 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3058 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3062 + proc_ext3_mb_max_to_scan->data = NULL;
3063 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3064 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3066 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3067 + proc_ext3_mb_min_to_scan = create_proc_entry(
3068 + EXT3_MB_MIN_TO_SCAN_NAME,
3069 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3070 + if (proc_ext3_mb_min_to_scan == NULL) {
3071 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3072 + EXT3_MB_MIN_TO_SCAN_NAME);
3073 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3074 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3075 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3079 + proc_ext3_mb_min_to_scan->data = NULL;
3080 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3081 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3083 + /* Initialize EXT3_ORDER2_REQ */
3084 + proc_ext3_mb_order2_req = create_proc_entry(
3085 + EXT3_MB_ORDER2_REQ,
3086 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3087 + if (proc_ext3_mb_order2_req == NULL) {
3088 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3089 + EXT3_MB_ORDER2_REQ);
3090 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3091 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3092 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3093 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3097 + proc_ext3_mb_order2_req->data = NULL;
3098 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3099 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3104 +void exit_ext3_proc(void)
3106 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3107 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3108 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3109 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3110 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3112 Index: linux-2.6.9-full/fs/ext3/Makefile
3113 ===================================================================
3114 --- linux-2.6.9-full.orig/fs/ext3/Makefile 2006-06-01 14:58:46.000000000 +0400
3115 +++ linux-2.6.9-full/fs/ext3/Makefile 2006-10-24 12:54:31.000000000 +0400
3116 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
3118 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3119 ioctl.o namei.o super.o symlink.o hash.o resize.o \
3121 + extents.o mballoc.o
3123 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3124 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o