1 Index: linux-stage/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-stage.orig/include/linux/ext3_fs.h 2006-07-16 02:29:43.000000000 +0800
4 +++ linux-stage/include/linux/ext3_fs.h 2006-07-16 02:29:49.000000000 +0800
6 #define ext3_debug(f, a...) do {} while (0)
9 +#define EXT3_MULTIBLOCK_ALLOCATOR 1
11 +#define EXT3_MB_HINT_MERGE 1
12 +#define EXT3_MB_HINT_RESERVED 2
13 +#define EXT3_MB_HINT_METADATA 4
14 +#define EXT3_MB_HINT_FIRST 8
15 +#define EXT3_MB_HINT_BEST 16
18 * Special inodes numbers
20 @@ -379,6 +387,7 @@ struct ext3_inode {
21 #define EXT3_MOUNT_IOPEN_NOPRIV 0x800000/* Make iopen world-readable */
22 #define EXT3_MOUNT_EXTENTS 0x1000000/* Extents support */
23 #define EXT3_MOUNT_EXTDEBUG 0x2000000/* Extents debug */
24 +#define EXT3_MOUNT_MBALLOC 0x4000000/* Buddy allocation support */
26 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
29 #define ext3_find_first_zero_bit ext2_find_first_zero_bit
30 #define ext3_find_next_zero_bit ext2_find_next_zero_bit
32 +#ifndef ext2_find_next_le_bit
33 +#ifdef __LITTLE_ENDIAN
34 +#define ext2_find_next_le_bit(addr, size, off) find_next_bit((addr), (size), (off))
36 +#error "mballoc needs a patch for big-endian systems - CFS bug 10634"
37 +#endif /* __LITTLE_ENDIAN */
38 +#endif /* !ext2_find_next_le_bit */
41 * Maximal mount counts between two filesystem checks
43 @@ -749,12 +758,12 @@ ext3_group_first_block_no(struct super_b
45 extern int ext3_bg_has_super(struct super_block *sb, int group);
46 extern unsigned long ext3_bg_num_gdb(struct super_block *sb, int group);
47 -extern ext3_fsblk_t ext3_new_block (handle_t *handle, struct inode *inode,
48 +extern ext3_fsblk_t ext3_new_block_old(handle_t *handle, struct inode *inode,
49 ext3_fsblk_t goal, int *errp);
50 extern ext3_fsblk_t ext3_new_blocks (handle_t *handle, struct inode *inode,
51 ext3_fsblk_t goal, unsigned long *count, int *errp);
52 extern void ext3_free_blocks (handle_t *handle, struct inode *inode,
53 - ext3_fsblk_t block, unsigned long count);
54 + ext3_fsblk_t block, unsigned long count, int metadata);
55 extern void ext3_free_blocks_sb (handle_t *handle, struct super_block *sb,
56 ext3_fsblk_t block, unsigned long count,
57 unsigned long *pdquot_freed_blocks);
58 @@ -881,6 +890,21 @@ extern void ext3_extents_initialize_bloc
59 extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
60 unsigned int cmd, unsigned long arg);
63 +extern long ext3_mb_stats;
64 +extern long ext3_mb_max_to_scan;
65 +extern int ext3_mb_init(struct super_block *sb, int needs_recovery);
66 +extern int ext3_mb_release(struct super_block *sb);
67 +extern ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
68 + ext3_fsblk_t goal, int *errp);
69 +extern ext3_fsblk_t ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
70 + ext3_fsblk_t goal, int *len, int flags,
72 +extern int ext3_mb_reserve_blocks(struct super_block *sb, int);
73 +extern void ext3_mb_release_blocks(struct super_block *sb, int);
74 +int __init init_ext3_proc(void);
75 +void exit_ext3_proc(void);
77 #endif /* __KERNEL__ */
79 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
80 Index: linux-stage/include/linux/ext3_fs_sb.h
81 ===================================================================
82 --- linux-stage.orig/include/linux/ext3_fs_sb.h 2006-07-16 02:29:43.000000000 +0800
83 +++ linux-stage/include/linux/ext3_fs_sb.h 2006-07-16 02:29:49.000000000 +0800
85 #include <linux/wait.h>
86 #include <linux/blockgroup_lock.h>
87 #include <linux/percpu_counter.h>
88 +#include <linux/list.h>
90 #include <linux/rbtree.h>
91 +#include <linux/proc_fs.h>
93 +struct ext3_buddy_group_blocks;
94 +struct ext3_mb_history;
95 +#define EXT3_BB_MAX_BLOCKS
98 * third extended-fs super-block data in memory
99 @@ -78,6 +84,43 @@ struct ext3_sb_info {
100 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
101 int s_jquota_fmt; /* Format of quota to use */
104 + /* for buddy allocator */
105 + struct ext3_group_info ***s_group_info;
106 + struct inode *s_buddy_cache;
107 + long s_blocks_reserved;
108 + spinlock_t s_reserve_lock;
109 + struct list_head s_active_transaction;
110 + struct list_head s_closed_transaction;
111 + struct list_head s_committed_transaction;
112 + spinlock_t s_md_lock;
113 + tid_t s_last_transaction;
115 + unsigned short *s_mb_offsets, *s_mb_maxs;
116 + unsigned long s_stripe;
118 + /* history to debug policy */
119 + struct ext3_mb_history *s_mb_history;
120 + int s_mb_history_cur;
121 + int s_mb_history_max;
122 + struct proc_dir_entry *s_mb_proc;
123 + spinlock_t s_mb_history_lock;
125 + /* stats for buddy allocator */
126 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
127 + atomic_t s_bal_success; /* we found long enough chunks */
128 + atomic_t s_bal_allocated; /* in blocks */
129 + atomic_t s_bal_ex_scanned; /* total extents scanned */
130 + atomic_t s_bal_goals; /* goal hits */
131 + atomic_t s_bal_breaks; /* too long searches */
132 + atomic_t s_bal_2orders; /* 2^order hits */
133 + spinlock_t s_bal_lock;
134 + unsigned long s_mb_buddies_generated;
135 + unsigned long long s_mb_generation_time;
138 +#define EXT3_GROUP_INFO(sb, group) \
139 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
140 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
142 #endif /* _LINUX_EXT3_FS_SB */
143 Index: linux-stage/fs/ext3/super.c
144 ===================================================================
145 --- linux-stage.orig/fs/ext3/super.c 2006-07-16 02:29:43.000000000 +0800
146 +++ linux-stage/fs/ext3/super.c 2006-07-16 02:29:49.000000000 +0800
147 @@ -391,6 +391,7 @@ static void ext3_put_super (struct super
148 struct ext3_super_block *es = sbi->s_es;
151 + ext3_mb_release(sb);
152 ext3_ext_release(sb);
153 ext3_xattr_put_super(sb);
154 journal_destroy(sbi->s_journal);
155 @@ -642,6 +643,7 @@ enum {
156 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
157 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
158 Opt_extents, Opt_noextents, Opt_extdebug,
159 + Opt_mballoc, Opt_nomballoc, Opt_stripe,
163 @@ -696,6 +697,9 @@ static match_table_t tokens = {
164 {Opt_extents, "extents"},
165 {Opt_noextents, "noextents"},
166 {Opt_extdebug, "extdebug"},
167 + {Opt_mballoc, "mballoc"},
168 + {Opt_nomballoc, "nomballoc"},
169 + {Opt_stripe, "stripe=%u"},
170 {Opt_barrier, "barrier=%u"},
172 {Opt_resize, "resize"},
173 @@ -1047,6 +1049,19 @@ clear_qf_name:
175 set_opt (sbi->s_mount_opt, EXTDEBUG);
178 + set_opt(sbi->s_mount_opt, MBALLOC);
180 + case Opt_nomballoc:
181 + clear_opt(sbi->s_mount_opt, MBALLOC);
184 + if (match_int(&args[0], &option))
188 + sbi->s_stripe = option;
192 "EXT3-fs: Unrecognized mount option \"%s\" "
193 @@ -1773,6 +1778,7 @@ static int ext3_fill_super (struct super
197 + ext3_mb_init(sb, needs_recovery);
201 @@ -2712,7 +2718,13 @@ static struct file_system_type ext3_fs_t
203 static int __init init_ext3_fs(void)
205 - int err = init_ext3_xattr();
208 + err = init_ext3_proc();
212 + err = init_ext3_xattr();
215 err = init_inodecache();
216 @@ -2734,6 +2746,7 @@ static void __exit exit_ext3_fs(void)
217 unregister_filesystem(&ext3_fs_type);
218 destroy_inodecache();
223 int ext3_prep_san_write(struct inode *inode, long *blocks,
224 Index: linux-stage/fs/ext3/extents.c
225 ===================================================================
226 --- linux-stage.orig/fs/ext3/extents.c 2006-07-16 02:29:43.000000000 +0800
227 +++ linux-stage/fs/ext3/extents.c 2006-07-16 02:29:49.000000000 +0800
228 @@ -771,7 +771,7 @@ cleanup:
229 for (i = 0; i < depth; i++) {
232 - ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
233 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
237 @@ -1428,7 +1428,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
238 path->p_idx->ei_leaf);
239 bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
240 ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
241 - ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
242 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
246 @@ -1913,10 +1913,12 @@ ext3_remove_blocks(struct ext3_extents_t
247 int needed = ext3_remove_blocks_credits(tree, ex, from, to);
248 handle_t *handle = ext3_journal_start(tree->inode, needed);
249 struct buffer_head *bh;
251 + int i, metadata = 0;
254 return PTR_ERR(handle);
255 + if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
257 if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
259 unsigned long num, start;
260 @@ -1928,7 +1930,7 @@ ext3_remove_blocks(struct ext3_extents_t
261 bh = sb_find_get_block(tree->inode->i_sb, start + i);
262 ext3_forget(handle, 0, tree->inode, bh, start + i);
264 - ext3_free_blocks(handle, tree->inode, start, num);
265 + ext3_free_blocks(handle, tree->inode, start, num, metadata);
266 } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
267 printk("strange request: removal %lu-%lu from %u:%u\n",
268 from, to, ex->ee_block, ex->ee_len);
269 Index: linux-stage/fs/ext3/inode.c
270 ===================================================================
271 --- linux-stage.orig/fs/ext3/inode.c 2006-07-16 02:29:43.000000000 +0800
272 +++ linux-stage/fs/ext3/inode.c 2006-07-16 02:29:49.000000000 +0800
273 @@ -562,7 +562,7 @@ static int ext3_alloc_blocks(handle_t *h
276 for (i = 0; i <index; i++)
277 - ext3_free_blocks(handle, inode, new_blocks[i], 1);
278 + ext3_free_blocks(handle, inode, new_blocks[i], 1, 1);
282 @@ -661,9 +661,9 @@ failed:
283 ext3_journal_forget(handle, branch[i].bh);
285 for (i = 0; i <indirect_blks; i++)
286 - ext3_free_blocks(handle, inode, new_blocks[i], 1);
287 + ext3_free_blocks(handle, inode, new_blocks[i], 1, 1);
289 - ext3_free_blocks(handle, inode, new_blocks[i], num);
290 + ext3_free_blocks(handle, inode, new_blocks[i], num, 1);
294 @@ -760,9 +760,9 @@ err_out:
295 for (i = 1; i <= num; i++) {
296 BUFFER_TRACE(where[i].bh, "call journal_forget");
297 ext3_journal_forget(handle, where[i].bh);
298 - ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
299 + ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1,1);
301 - ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
302 + ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 1);
306 @@ -2007,7 +2007,7 @@ static void ext3_clear_blocks(handle_t *
310 - ext3_free_blocks(handle, inode, block_to_free, count);
311 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
315 @@ -2180,7 +2180,7 @@ static void ext3_free_branches(handle_t
316 ext3_journal_test_restart(handle, inode);
319 - ext3_free_blocks(handle, inode, nr, 1);
320 + ext3_free_blocks(handle, inode, nr, 1, 1);
324 Index: linux-stage/fs/ext3/balloc.c
325 ===================================================================
326 --- linux-stage.orig/fs/ext3/balloc.c 2006-07-16 02:29:43.000000000 +0800
327 +++ linux-stage/fs/ext3/balloc.c 2006-07-16 02:33:13.000000000 +0800
328 @@ -79,7 +79,7 @@ struct ext3_group_desc * ext3_get_group_
330 * Return buffer_head on success or NULL in case of failure.
332 -static struct buffer_head *
333 +struct buffer_head *
334 read_block_bitmap(struct super_block *sb, unsigned int block_group)
336 struct ext3_group_desc * desc;
337 @@ -490,24 +490,6 @@ error_return:
341 -/* Free given blocks, update quota and i_blocks field */
342 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
343 - ext3_fsblk_t block, unsigned long count)
345 - struct super_block * sb;
346 - unsigned long dquot_freed_blocks;
350 - printk ("ext3_free_blocks: nonexistent device");
353 - ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
354 - if (dquot_freed_blocks)
355 - DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
360 * For ext3 allocations, we must not reuse any blocks which are
361 * allocated in the bitmap buffer's "last committed data" copy. This
362 @@ -1463,7 +1445,7 @@ out:
366 -ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
367 +ext3_fsblk_t ext3_new_block_old(handle_t *handle, struct inode *inode,
368 ext3_fsblk_t goal, int *errp)
370 unsigned long count = 1;
371 Index: linux-stage/fs/ext3/xattr.c
372 ===================================================================
373 --- linux-stage.orig/fs/ext3/xattr.c 2006-07-16 02:29:43.000000000 +0800
374 +++ linux-stage/fs/ext3/xattr.c 2006-07-16 02:29:49.000000000 +0800
375 @@ -484,7 +484,7 @@ ext3_xattr_release_block(handle_t *handl
376 ea_bdebug(bh, "refcount now=0; freeing");
378 mb_cache_entry_free(ce);
379 - ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
380 + ext3_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
382 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
384 @@ -805,7 +805,7 @@ inserted:
385 new_bh = sb_getblk(sb, block);
388 - ext3_free_blocks(handle, inode, block, 1);
389 + ext3_free_blocks(handle, inode, block, 1, 1);
393 Index: linux-stage/fs/ext3/mballoc.c
394 ===================================================================
395 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
396 +++ linux-stage/fs/ext3/mballoc.c 2006-07-16 02:29:49.000000000 +0800
399 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
400 + * Written by Alex Tomas <alex@clusterfs.com>
402 + * This program is free software; you can redistribute it and/or modify
403 + * it under the terms of the GNU General Public License version 2 as
404 + * published by the Free Software Foundation.
406 + * This program is distributed in the hope that it will be useful,
407 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
408 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
409 + * GNU General Public License for more details.
411 + * You should have received a copy of the GNU General Public Licens
412 + * along with this program; if not, write to the Free Software
413 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
418 + * mballoc.c contains the multiblocks allocation routines
421 +#include <linux/config.h>
422 +#include <linux/time.h>
423 +#include <linux/fs.h>
424 +#include <linux/namei.h>
425 +#include <linux/jbd.h>
426 +#include <linux/ext3_fs.h>
427 +#include <linux/ext3_jbd.h>
428 +#include <linux/quotaops.h>
429 +#include <linux/buffer_head.h>
430 +#include <linux/module.h>
431 +#include <linux/swap.h>
432 +#include <linux/proc_fs.h>
433 +#include <linux/pagemap.h>
434 +#include <linux/seq_file.h>
438 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
439 + * - track min/max extents in each group for better group selection
440 + * - mb_mark_used() may allocate chunk right after splitting buddy
441 + * - special flag to advice allocator to look for requested + N blocks
442 + * this may improve interaction between extents and mballoc
443 + * - tree of groups sorted by number of free blocks
444 + * - percpu reservation code (hotpath)
449 + * with AGRESSIVE_CHECK allocator runs consistency checks over
450 + * structures. these checks slow things down a lot
452 +#define AGGRESSIVE_CHECK__
458 +#define mb_debug(fmt,a...) printk(fmt, ##a)
460 +#define mb_debug(fmt,a...)
464 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
465 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
467 +#define EXT3_MB_HISTORY
470 + * How long mballoc can look for a best extent (in found extents)
472 +long ext3_mb_max_to_scan = 500;
475 + * How long mballoc must look for a best extent
477 +long ext3_mb_min_to_scan = 30;
480 + * with 'ext3_mb_stats' allocator will collect stats that will be
481 + * shown at umount. The collecting costs though!
484 +long ext3_mb_stats = 1;
487 + * for which requests use 2^N search using buddies
489 +long ext3_mb_order2_reqs = 8;
491 +#ifdef EXT3_BB_MAX_BLOCKS
492 +#undef EXT3_BB_MAX_BLOCKS
494 +#define EXT3_BB_MAX_BLOCKS 30
496 +struct ext3_free_metadata {
497 + unsigned short group;
498 + unsigned short num;
499 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
500 + struct list_head list;
503 +struct ext3_group_info {
504 + unsigned long bb_state;
505 + unsigned long bb_tid;
506 + struct ext3_free_metadata *bb_md_cur;
507 + unsigned short bb_first_free;
508 + unsigned short bb_free;
509 + unsigned short bb_fragments;
510 + unsigned short bb_counters[];
514 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
515 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
517 +#define EXT3_MB_GRP_NEED_INIT(grp) \
518 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
520 +struct ext3_free_extent {
526 +struct ext3_allocation_context {
527 + struct super_block *ac_sb;
530 + struct ext3_free_extent ac_g_ex;
532 + /* the best found extent */
533 + struct ext3_free_extent ac_b_ex;
535 + /* number of iterations done. we have to track to limit searching */
536 + unsigned long ac_ex_scanned;
537 + __u16 ac_groups_scanned;
542 + __u8 ac_flags; /* allocation hints */
545 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
546 + * N > 0, the field stores N, otherwise 0 */
548 + struct page *ac_buddy_page;
549 + struct page *ac_bitmap_page;
552 +#define AC_STATUS_CONTINUE 1
553 +#define AC_STATUS_FOUND 2
554 +#define AC_STATUS_BREAK 3
556 +struct ext3_mb_history {
557 + struct ext3_free_extent goal; /* goal allocation */
558 + struct ext3_free_extent result; /* result allocation */
561 + __u16 found; /* how many extents have been found */
562 + __u16 groups; /* how many groups have been scanned */
563 + __u16 tail; /* what tail broke some buddy */
564 + __u16 buddy; /* buddy the tail ^^^ broke */
565 + __u8 cr; /* which phase the result extent was found at */
570 + struct page *bd_buddy_page;
572 + struct page *bd_bitmap_page;
574 + struct ext3_group_info *bd_info;
575 + struct super_block *bd_sb;
579 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
580 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
582 +#ifndef EXT3_MB_HISTORY
583 +#define ext3_mb_store_history(sb,ino,ac)
585 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
586 + struct ext3_allocation_context *ac);
589 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
591 +static struct proc_dir_entry *proc_root_ext3;
593 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
594 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
595 +int ext3_mb_reserve_blocks(struct super_block *, int);
596 +void ext3_mb_release_blocks(struct super_block *, int);
597 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
598 +void ext3_mb_free_committed_blocks(struct super_block *);
600 +#if BITS_PER_LONG == 64
601 +#define mb_correct_addr_and_bit(bit,addr) \
603 + bit += ((unsigned long) addr & 7UL) << 3; \
604 + addr = (void *) ((unsigned long) addr & ~7UL); \
606 +#elif BITS_PER_LONG == 32
607 +#define mb_correct_addr_and_bit(bit,addr) \
609 + bit += ((unsigned long) addr & 3UL) << 3; \
610 + addr = (void *) ((unsigned long) addr & ~3UL); \
613 +#error "how many bits you are?!"
616 +static inline int mb_test_bit(int bit, void *addr)
618 + mb_correct_addr_and_bit(bit,addr);
619 + return ext2_test_bit(bit, addr);
622 +static inline void mb_set_bit(int bit, void *addr)
624 + mb_correct_addr_and_bit(bit,addr);
625 + ext2_set_bit(bit, addr);
628 +static inline void mb_set_bit_atomic(int bit, void *addr)
630 + mb_correct_addr_and_bit(bit,addr);
631 + ext2_set_bit_atomic(NULL, bit, addr);
634 +static inline void mb_clear_bit(int bit, void *addr)
636 + mb_correct_addr_and_bit(bit,addr);
637 + ext2_clear_bit(bit, addr);
640 +static inline void mb_clear_bit_atomic(int bit, void *addr)
642 + mb_correct_addr_and_bit(bit,addr);
643 + ext2_clear_bit_atomic(NULL, bit, addr);
646 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
649 +#if BITS_PER_LONG == 64
650 + fix = ((unsigned long) addr & 7UL) << 3;
651 + addr = (void *) ((unsigned long) addr & ~7UL);
652 +#elif BITS_PER_LONG == 32
653 + fix = ((unsigned long) addr & 3UL) << 3;
654 + addr = (void *) ((unsigned long) addr & ~3UL);
656 +#error "how many bits you are?!"
660 + return ext2_find_next_zero_bit(addr, max, start) - fix;
663 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
667 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
668 + J_ASSERT(max != NULL);
670 + if (order > e3b->bd_blkbits + 1) {
675 + /* at order 0 we see each particular block */
676 + *max = 1 << (e3b->bd_blkbits + 3);
678 + return EXT3_MB_BITMAP(e3b);
680 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
681 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
686 +#ifdef AGGRESSIVE_CHECK
688 +static void mb_check_buddy(struct ext3_buddy *e3b)
690 + int order = e3b->bd_blkbits + 1;
691 + int max, max2, i, j, k, count;
692 + int fragments = 0, fstart;
693 + void *buddy, *buddy2;
695 + if (!test_opt(e3b->bd_sb, MBALLOC))
699 + static int mb_check_counter = 0;
700 + if (mb_check_counter++ % 300 != 0)
704 + while (order > 1) {
705 + buddy = mb_find_buddy(e3b, order, &max);
707 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
709 + J_ASSERT(buddy != buddy2);
710 + J_ASSERT(max * 2 == max2);
713 + for (i = 0; i < max; i++) {
715 + if (mb_test_bit(i, buddy)) {
716 + /* only single bit in buddy2 may be 1 */
717 + if (!mb_test_bit(i << 1, buddy2))
718 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
719 + else if (!mb_test_bit((i << 1) + 1, buddy2))
720 + J_ASSERT(mb_test_bit(i << 1, buddy2));
724 + /* both bits in buddy2 must be 0 */
725 + J_ASSERT(mb_test_bit(i << 1, buddy2));
726 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
728 + for (j = 0; j < (1 << order); j++) {
729 + k = (i * (1 << order)) + j;
730 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
734 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
739 + buddy = mb_find_buddy(e3b, 0, &max);
740 + for (i = 0; i < max; i++) {
741 + if (!mb_test_bit(i, buddy)) {
742 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
743 + if (fstart == -1) {
750 + /* check used bits only */
751 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
752 + buddy2 = mb_find_buddy(e3b, j, &max2);
754 + J_ASSERT(k < max2);
755 + J_ASSERT(mb_test_bit(k, buddy2));
758 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
759 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
763 +#define mb_check_buddy(e3b)
766 +/* find most significant bit */
767 +static int inline fmsb(unsigned short word)
781 + } while (word != 0);
787 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
788 + int len, struct ext3_group_info *grp)
790 + struct ext3_sb_info *sbi = EXT3_SB(sb);
791 + unsigned short min, max, chunk, border;
793 + mb_debug("mark %u/%u free\n", first, len);
794 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
796 + border = 2 << sb->s_blocksize_bits;
799 + /* find how many blocks can be covered since this position */
800 + max = ffs(first | border) - 1;
802 + /* find how many blocks of power 2 we need to mark */
805 + mb_debug(" %u/%u -> max %u, min %u\n",
806 + first & ((2 << sb->s_blocksize_bits) - 1),
813 + /* mark multiblock chunks only */
814 + grp->bb_counters[min]++;
816 + mb_debug(" set %u at %u \n", first >> min,
817 + sbi->s_mb_offsets[min]);
818 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
827 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
830 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
831 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
832 + unsigned short i = 0, first, len;
833 + unsigned free = 0, fragments = 0;
834 + unsigned long long period = get_cycles();
836 + i = mb_find_next_zero_bit(bitmap, max, 0);
837 + grp->bb_first_free = i;
841 + i = ext2_find_next_le_bit(bitmap, max, i);
845 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
847 + grp->bb_counters[0]++;
849 + i = mb_find_next_zero_bit(bitmap, max, i);
851 + grp->bb_fragments = fragments;
853 + /* bb_state shouldn't being modified because all
854 + * others waits for init completion on page lock */
855 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
856 + if (free != grp->bb_free) {
857 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
858 + group, free, grp->bb_free);
859 + grp->bb_free = free;
862 + period = get_cycles() - period;
863 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
864 + EXT3_SB(sb)->s_mb_buddies_generated++;
865 + EXT3_SB(sb)->s_mb_generation_time += period;
866 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
869 +static int ext3_mb_init_cache(struct page *page)
871 + int blocksize, blocks_per_page, groups_per_page;
872 + int err = 0, i, first_group, first_block;
873 + struct super_block *sb;
874 + struct buffer_head *bhs;
875 + struct buffer_head **bh;
876 + struct inode *inode;
877 + char *data, *bitmap;
879 + mb_debug("init page %lu\n", page->index);
881 + inode = page->mapping->host;
883 + blocksize = 1 << inode->i_blkbits;
884 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
886 + groups_per_page = blocks_per_page >> 1;
887 + if (groups_per_page == 0)
888 + groups_per_page = 1;
890 + /* allocate buffer_heads to read bitmaps */
891 + if (groups_per_page > 1) {
893 + i = sizeof(struct buffer_head *) * groups_per_page;
894 + bh = kmalloc(i, GFP_NOFS);
901 + first_group = page->index * blocks_per_page / 2;
903 + /* read all groups the page covers into the cache */
904 + for (i = 0; i < groups_per_page; i++) {
905 + struct ext3_group_desc * desc;
907 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
911 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
916 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
920 + if (buffer_uptodate(bh[i]))
923 + lock_buffer(bh[i]);
924 + if (buffer_uptodate(bh[i])) {
925 + unlock_buffer(bh[i]);
930 + bh[i]->b_end_io = end_buffer_read_sync;
931 + submit_bh(READ, bh[i]);
932 + mb_debug("read bitmap for group %u\n", first_group + i);
935 + /* wait for I/O completion */
936 + for (i = 0; i < groups_per_page && bh[i]; i++)
937 + wait_on_buffer(bh[i]);
940 + for (i = 0; i < groups_per_page && bh[i]; i++)
941 + if (!buffer_uptodate(bh[i]))
944 + first_block = page->index * blocks_per_page;
945 + for (i = 0; i < blocks_per_page; i++) {
948 + group = (first_block + i) >> 1;
949 + if (group >= EXT3_SB(sb)->s_groups_count)
952 + data = page_address(page) + (i * blocksize);
953 + bitmap = bh[group - first_group]->b_data;
955 + if ((first_block + i) & 1) {
956 + /* this is block of buddy */
957 + mb_debug("put buddy for group %u in page %lu/%x\n",
958 + group, page->index, i * blocksize);
959 + memset(data, 0xff, blocksize);
960 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
961 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
962 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
963 + ext3_mb_generate_buddy(sb, data, bitmap, group);
965 + /* this is block of bitmap */
966 + mb_debug("put bitmap for group %u in page %lu/%x\n",
967 + group, page->index, i * blocksize);
968 + memcpy(data, bitmap, blocksize);
971 + SetPageUptodate(page);
975 + for (i = 0; i < groups_per_page && bh[i]; i++)
983 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
984 + struct ext3_buddy *e3b)
986 + struct ext3_sb_info *sbi = EXT3_SB(sb);
987 + struct inode *inode = sbi->s_buddy_cache;
988 + int blocks_per_page, block, pnum, poff;
991 + mb_debug("load group %u\n", group);
993 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
995 + e3b->bd_blkbits = sb->s_blocksize_bits;
996 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
998 + e3b->bd_group = group;
999 + e3b->bd_buddy_page = NULL;
1000 + e3b->bd_bitmap_page = NULL;
1002 + block = group * 2;
1003 + pnum = block / blocks_per_page;
1004 + poff = block % blocks_per_page;
1006 + /* we could use find_or_create_page(), but it locks page
1007 + * what we'd like to avoid in fast path ... */
1008 + page = find_get_page(inode->i_mapping, pnum);
1009 + if (page == NULL || !PageUptodate(page)) {
1011 + page_cache_release(page);
1012 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1014 + BUG_ON(page->mapping != inode->i_mapping);
1015 + if (!PageUptodate(page))
1016 + ext3_mb_init_cache(page);
1017 + unlock_page(page);
1020 + if (page == NULL || !PageUptodate(page))
1022 + e3b->bd_bitmap_page = page;
1023 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1024 + mark_page_accessed(page);
1027 + pnum = block / blocks_per_page;
1028 + poff = block % blocks_per_page;
1030 + page = find_get_page(inode->i_mapping, pnum);
1031 + if (page == NULL || !PageUptodate(page)) {
1033 + page_cache_release(page);
1034 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1036 + BUG_ON(page->mapping != inode->i_mapping);
1037 + if (!PageUptodate(page))
1038 + ext3_mb_init_cache(page);
1039 + unlock_page(page);
1042 + if (page == NULL || !PageUptodate(page))
1044 + e3b->bd_buddy_page = page;
1045 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1046 + mark_page_accessed(page);
1048 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1049 + J_ASSERT(e3b->bd_buddy_page != NULL);
1054 + if (e3b->bd_bitmap_page)
1055 + page_cache_release(e3b->bd_bitmap_page);
1056 + if (e3b->bd_buddy_page)
1057 + page_cache_release(e3b->bd_buddy_page);
1058 + e3b->bd_buddy = NULL;
1059 + e3b->bd_bitmap = NULL;
1063 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1065 + if (e3b->bd_bitmap_page)
1066 + page_cache_release(e3b->bd_bitmap_page);
1067 + if (e3b->bd_buddy_page)
1068 + page_cache_release(e3b->bd_buddy_page);
1073 +ext3_lock_group(struct super_block *sb, int group)
1075 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1076 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1080 +ext3_unlock_group(struct super_block *sb, int group)
1082 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1083 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1086 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1091 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1092 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1094 + bb = EXT3_MB_BUDDY(e3b);
1095 + while (order <= e3b->bd_blkbits + 1) {
1096 + block = block >> 1;
1097 + if (!mb_test_bit(block, bb)) {
1098 + /* this block is part of buddy of order 'order' */
1101 + bb += 1 << (e3b->bd_blkbits - order);
1107 +static inline void mb_clear_bits(void *bm, int cur, int len)
1112 + while (cur < len) {
1113 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1114 + /* fast path: clear whole word at once */
1115 + addr = bm + (cur >> 3);
1120 + mb_clear_bit_atomic(cur, bm);
1125 +static inline void mb_set_bits(void *bm, int cur, int len)
1130 + while (cur < len) {
1131 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1132 + /* fast path: clear whole word at once */
1133 + addr = bm + (cur >> 3);
1134 + *addr = 0xffffffff;
1138 + mb_set_bit_atomic(cur, bm);
1143 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1145 + int block = 0, max = 0, order;
1146 + void *buddy, *buddy2;
1148 + mb_check_buddy(e3b);
1150 + e3b->bd_info->bb_free += count;
1151 + if (first < e3b->bd_info->bb_first_free)
1152 + e3b->bd_info->bb_first_free = first;
1154 + /* let's maintain fragments counter */
1156 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1157 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1158 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1160 + e3b->bd_info->bb_fragments--;
1161 + else if (!block && !max)
1162 + e3b->bd_info->bb_fragments++;
1164 + /* let's maintain buddy itself */
1165 + while (count-- > 0) {
1169 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1170 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1171 + e3b->bd_info->bb_counters[order]++;
1173 + /* start of the buddy */
1174 + buddy = mb_find_buddy(e3b, order, &max);
1178 + if (mb_test_bit(block, buddy) ||
1179 + mb_test_bit(block + 1, buddy))
1182 + /* both the buddies are free, try to coalesce them */
1183 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1189 + /* for special purposes, we don't set
1190 + * free bits in bitmap */
1191 + mb_set_bit(block, buddy);
1192 + mb_set_bit(block + 1, buddy);
1194 + e3b->bd_info->bb_counters[order]--;
1195 + e3b->bd_info->bb_counters[order]--;
1197 + block = block >> 1;
1199 + e3b->bd_info->bb_counters[order]++;
1201 + mb_clear_bit(block, buddy2);
1205 + mb_check_buddy(e3b);
1210 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1211 + int needed, struct ext3_free_extent *ex)
1213 + int next = block, max, ord;
1216 + J_ASSERT(ex != NULL);
1218 + buddy = mb_find_buddy(e3b, order, &max);
1220 + J_ASSERT(block < max);
1221 + if (mb_test_bit(block, buddy)) {
1228 + if (likely(order == 0)) {
1229 + /* find actual order */
1230 + order = mb_find_order_for_block(e3b, block);
1231 + block = block >> order;
1234 + ex->fe_len = 1 << order;
1235 + ex->fe_start = block << order;
1236 + ex->fe_group = e3b->bd_group;
1238 + /* calc difference from given start */
1239 + next = next - ex->fe_start;
1240 + ex->fe_len -= next;
1241 + ex->fe_start += next;
1243 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1245 + if (block + 1 >= max)
1248 + next = (block + 1) * (1 << order);
1249 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1252 + ord = mb_find_order_for_block(e3b, next);
1255 + block = next >> order;
1256 + ex->fe_len += 1 << order;
1259 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1260 + return ex->fe_len;
1263 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1265 + int ord, mlen = 0, max = 0, cur;
1266 + int start = ex->fe_start;
1267 + int len = ex->fe_len;
1272 + mb_check_buddy(e3b);
1274 + e3b->bd_info->bb_free -= len;
1275 + if (e3b->bd_info->bb_first_free == start)
1276 + e3b->bd_info->bb_first_free += len;
1278 + /* let's maintain fragments counter */
1280 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1281 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1282 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1284 + e3b->bd_info->bb_fragments++;
1285 + else if (!mlen && !max)
1286 + e3b->bd_info->bb_fragments--;
1288 + /* let's maintain buddy itself */
1290 + ord = mb_find_order_for_block(e3b, start);
1292 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1293 + /* the whole chunk may be allocated at once! */
1295 + buddy = mb_find_buddy(e3b, ord, &max);
1296 + J_ASSERT((start >> ord) < max);
1297 + mb_set_bit(start >> ord, buddy);
1298 + e3b->bd_info->bb_counters[ord]--;
1301 + J_ASSERT(len >= 0);
1305 + /* store for history */
1307 + ret = len | (ord << 16);
1309 + /* we have to split large buddy */
1310 + J_ASSERT(ord > 0);
1311 + buddy = mb_find_buddy(e3b, ord, &max);
1312 + mb_set_bit(start >> ord, buddy);
1313 + e3b->bd_info->bb_counters[ord]--;
1316 + cur = (start >> ord) & ~1U;
1317 + buddy = mb_find_buddy(e3b, ord, &max);
1318 + mb_clear_bit(cur, buddy);
1319 + mb_clear_bit(cur + 1, buddy);
1320 + e3b->bd_info->bb_counters[ord]++;
1321 + e3b->bd_info->bb_counters[ord]++;
1324 + /* now drop all the bits in bitmap */
1325 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1327 + mb_check_buddy(e3b);
1333 + * Must be called under group lock!
1335 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1336 + struct ext3_buddy *e3b)
1338 + unsigned long ret;
1340 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1341 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1343 + ac->ac_status = AC_STATUS_FOUND;
1344 + ac->ac_tail = ret & 0xffff;
1345 + ac->ac_buddy = ret >> 16;
1347 + /* hold in-core structures until allocated
1348 + * blocks are marked non-free in on-disk bitmap */
1349 + ac->ac_buddy_page = e3b->bd_buddy_page;
1350 + page_cache_get(e3b->bd_buddy_page);
1351 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1352 + page_cache_get(e3b->bd_bitmap_page);
1356 + * The routine checks whether found extent is good enough. If it is,
1357 + * then the extent gets marked used and flag is set to the context
1358 + * to stop scanning. Otherwise, the extent is compared with the
1359 + * previous found extent and if new one is better, then it's stored
1360 + * in the context. Later, the best found extent will be used, if
1361 + * mballoc can't find good enough extent.
1363 + * FIXME: real allocation policy is to be designed yet!
1365 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1366 + struct ext3_free_extent *ex,
1367 + struct ext3_buddy *e3b)
1369 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1370 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1372 + J_ASSERT(ex->fe_len > 0);
1373 + J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1374 + J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1379 + * The special case - take what you catch first
1381 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1383 + ext3_mb_use_best_found(ac, e3b);
1388 + * Let's check whether the chunk is good enough
1390 + if (ex->fe_len == gex->fe_len) {
1392 + ext3_mb_use_best_found(ac, e3b);
1397 + * If this is first found extent, just store it in the context
1399 + if (bex->fe_len == 0) {
1405 + * If new found extent is better, store it in the context
1407 + if (bex->fe_len < gex->fe_len) {
1408 + /* if the request isn't satisfied, any found extent
1409 + * larger than previous best one is better */
1410 + if (ex->fe_len > bex->fe_len)
1412 + } else if (ex->fe_len > gex->fe_len) {
1413 + /* if the request is satisfied, then we try to find
1414 + * an extent that still satisfy the request, but is
1415 + * smaller than previous one */
1420 + * Let's scan at least few extents and don't pick up a first one
1422 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1423 + ac->ac_status = AC_STATUS_BREAK;
1426 + * We don't want to scan for a whole year
1428 + if (ac->ac_found > ext3_mb_max_to_scan)
1429 + ac->ac_status = AC_STATUS_BREAK;
1432 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1433 + struct ext3_buddy *e3b)
1435 + struct ext3_free_extent ex = ac->ac_b_ex;
1436 + int group = ex.fe_group, max, err;
1438 + J_ASSERT(ex.fe_len > 0);
1439 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1443 + ext3_lock_group(ac->ac_sb, group);
1444 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1448 + ext3_mb_use_best_found(ac, e3b);
1451 + ext3_unlock_group(ac->ac_sb, group);
1453 + ext3_mb_release_desc(e3b);
1458 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1459 + struct ext3_buddy *e3b)
1461 + int group = ac->ac_g_ex.fe_group, max, err;
1462 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1463 + struct ext3_super_block *es = sbi->s_es;
1464 + struct ext3_free_extent ex;
1466 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1470 + ext3_lock_group(ac->ac_sb, group);
1471 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1472 + ac->ac_g_ex.fe_len, &ex);
1474 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1475 + ext3_fsblk_t start;
1476 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1477 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1478 + if (start % sbi->s_stripe == 0) {
1481 + ext3_mb_use_best_found(ac, e3b);
1483 + } else if (max >= ac->ac_g_ex.fe_len) {
1484 + J_ASSERT(ex.fe_len > 0);
1485 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1486 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1489 + ext3_mb_use_best_found(ac, e3b);
1490 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1491 + /* Sometimes, caller may want to merge even small
1492 + * number of blocks to an existing extent */
1493 + J_ASSERT(ex.fe_len > 0);
1494 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1495 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1498 + ext3_mb_use_best_found(ac, e3b);
1500 + ext3_unlock_group(ac->ac_sb, group);
1502 + ext3_mb_release_desc(e3b);
1508 + * The routine scans buddy structures (not bitmap!) from given order
1509 + * to max order and tries to find big enough chunk to satisfy the req
1511 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1512 + struct ext3_buddy *e3b)
1514 + struct super_block *sb = ac->ac_sb;
1515 + struct ext3_group_info *grp = e3b->bd_info;
1519 + J_ASSERT(ac->ac_2order > 0);
1520 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1521 + if (grp->bb_counters[i] == 0)
1524 + buddy = mb_find_buddy(e3b, i, &max);
1525 + if (buddy == NULL) {
1526 + printk(KERN_ALERT "looking for wrong order?\n");
1530 + k = mb_find_next_zero_bit(buddy, max, 0);
1531 + J_ASSERT(k < max);
1535 + ac->ac_b_ex.fe_len = 1 << i;
1536 + ac->ac_b_ex.fe_start = k << i;
1537 + ac->ac_b_ex.fe_group = e3b->bd_group;
1539 + ext3_mb_use_best_found(ac, e3b);
1540 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1542 + if (unlikely(ext3_mb_stats))
1543 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1550 + * The routine scans the group and measures all found extents.
1551 + * In order to optimize scanning, caller must pass number of
1552 + * free blocks in the group, so the routine can know upper limit.
1554 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1555 + struct ext3_buddy *e3b)
1557 + struct super_block *sb = ac->ac_sb;
1558 + void *bitmap = EXT3_MB_BITMAP(e3b);
1559 + struct ext3_free_extent ex;
1562 + free = e3b->bd_info->bb_free;
1563 + J_ASSERT(free > 0);
1565 + i = e3b->bd_info->bb_first_free;
1567 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1568 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1569 + if (i >= sb->s_blocksize * 8) {
1570 + J_ASSERT(free == 0);
1574 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1575 + J_ASSERT(ex.fe_len > 0);
1576 + J_ASSERT(free >= ex.fe_len);
1578 + ext3_mb_measure_extent(ac, &ex, e3b);
1581 + free -= ex.fe_len;
1586 + * This is a special case for storages like raid5
1587 + * we try to find stripe-aligned chunks for stripe-size requests
1589 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1590 + struct ext3_buddy *e3b)
1592 + struct super_block *sb = ac->ac_sb;
1593 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1594 + void *bitmap = EXT3_MB_BITMAP(e3b);
1595 + struct ext3_free_extent ex;
1596 + ext3_fsblk_t i, max;
1598 + J_ASSERT(sbi->s_stripe != 0);
1600 + /* find first stripe-aligned block */
1601 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb) +
1602 + le32_to_cpu(sbi->s_es->s_first_data_block);
1603 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1604 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block)) %
1605 + EXT3_BLOCKS_PER_GROUP(sb);
1607 + while (i < sb->s_blocksize * 8) {
1608 + if (!mb_test_bit(i, bitmap)) {
1609 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1610 + if (max >= sbi->s_stripe) {
1613 + ext3_mb_use_best_found(ac, e3b);
1617 + i += sbi->s_stripe;
1621 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1622 + int group, int cr)
1624 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1625 + unsigned free, fragments, i, bits;
1627 + J_ASSERT(cr >= 0 && cr < 4);
1628 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1630 + free = grp->bb_free;
1631 + fragments = grp->bb_fragments;
1634 + if (fragments == 0)
1639 + J_ASSERT(ac->ac_2order != 0);
1640 + bits = ac->ac_sb->s_blocksize_bits + 1;
1641 + for (i = ac->ac_2order; i <= bits; i++)
1642 + if (grp->bb_counters[i] > 0)
1646 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1650 + if (free >= ac->ac_g_ex.fe_len)
1662 +ext3_fsblk_t ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1663 + ext3_fsblk_t goal, int *len,int flags,int *errp)
1665 + struct buffer_head *bitmap_bh = NULL;
1666 + struct ext3_allocation_context ac;
1667 + int i, group, cr, err = 0;
1668 + struct ext3_group_desc *gdp;
1669 + struct ext3_super_block *es;
1670 + struct buffer_head *gdp_bh;
1671 + struct ext3_sb_info *sbi;
1672 + struct super_block *sb;
1673 + struct ext3_buddy e3b;
1674 + ext3_fsblk_t block;
1676 + J_ASSERT(len != NULL);
1677 + J_ASSERT(*len > 0);
1681 + printk("ext3_mb_new_nblocks: nonexistent device");
1685 + if (!test_opt(sb, MBALLOC)) {
1686 + static int ext3_mballoc_warning = 0;
1687 + if (ext3_mballoc_warning == 0) {
1688 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1689 + "mballoc disabled!\n");
1690 + ext3_mballoc_warning++;
1693 + err = ext3_new_block_old(handle, inode, goal, errp);
1697 + ext3_mb_poll_new_transaction(sb, handle);
1699 + sbi = EXT3_SB(sb);
1700 + es = EXT3_SB(sb)->s_es;
1703 + * We can't allocate > group size
1705 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1706 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1708 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1709 + /* someone asks for non-reserved blocks */
1711 + err = ext3_mb_reserve_blocks(sb, 1);
1718 + ac.ac_buddy_page = NULL;
1719 + ac.ac_bitmap_page = NULL;
1722 + * Check quota for allocation of this blocks.
1724 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1732 + /* start searching from the goal */
1733 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1734 + goal >= le32_to_cpu(es->s_blocks_count))
1735 + goal = le32_to_cpu(es->s_first_data_block);
1736 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1737 + EXT3_BLOCKS_PER_GROUP(sb);
1738 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1739 + EXT3_BLOCKS_PER_GROUP(sb));
1741 + /* set up allocation goals */
1742 + ac.ac_b_ex.fe_group = 0;
1743 + ac.ac_b_ex.fe_start = 0;
1744 + ac.ac_b_ex.fe_len = 0;
1745 + ac.ac_status = AC_STATUS_CONTINUE;
1746 + ac.ac_groups_scanned = 0;
1747 + ac.ac_ex_scanned = 0;
1749 + ac.ac_sb = inode->i_sb;
1750 + ac.ac_g_ex.fe_group = group;
1751 + ac.ac_g_ex.fe_start = block;
1752 + ac.ac_g_ex.fe_len = *len;
1753 + ac.ac_flags = flags;
1755 + ac.ac_criteria = 0;
1757 + if (*len == 1 && sbi->s_stripe) {
1758 + /* looks like a metadata, let's use a dirty hack for raid5
1759 + * move all metadata in first groups in hope to hit cached
1760 + * sectors and thus avoid read-modify cycles in raid5 */
1761 + ac.ac_g_ex.fe_group = group = 0;
1764 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1766 + if (i >= ext3_mb_order2_reqs) {
1768 + if ((*len & (~(1 << i))) == 0)
1772 + /* first, try the goal */
1773 + err = ext3_mb_find_by_goal(&ac, &e3b);
1776 + if (ac.ac_status == AC_STATUS_FOUND)
1779 + /* Let's just scan groups to find more-less suitable blocks */
1780 + cr = ac.ac_2order ? 0 : 1;
1782 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1783 + ac.ac_criteria = cr;
1784 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1785 + if (group == EXT3_SB(sb)->s_groups_count)
1788 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1789 + /* we need full data about the group
1790 + * to make a good selection */
1791 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1794 + ext3_mb_release_desc(&e3b);
1797 + /* check is group good for our criteries */
1798 + if (!ext3_mb_good_group(&ac, group, cr))
1801 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1805 + ext3_lock_group(sb, group);
1806 + if (!ext3_mb_good_group(&ac, group, cr)) {
1807 + /* someone did allocation from this group */
1808 + ext3_unlock_group(sb, group);
1809 + ext3_mb_release_desc(&e3b);
1813 + ac.ac_groups_scanned++;
1815 + ext3_mb_simple_scan_group(&ac, &e3b);
1816 + else if (cr == 1 && *len == sbi->s_stripe)
1817 + ext3_mb_scan_aligned(&ac, &e3b);
1819 + ext3_mb_complex_scan_group(&ac, &e3b);
1821 + ext3_unlock_group(sb, group);
1823 + ext3_mb_release_desc(&e3b);
1825 + if (ac.ac_status != AC_STATUS_CONTINUE)
1830 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1831 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1833 + * We've been searching too long. Let's try to allocate
1834 + * the best chunk we've found so far
1837 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1838 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1839 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1840 + ac.ac_g_ex.fe_len);*/
1841 + ext3_mb_try_best_found(&ac, &e3b);
1842 + if (ac.ac_status != AC_STATUS_FOUND) {
1844 + * Someone more lucky has already allocated it.
1845 + * The only thing we can do is just take first
1847 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1849 + ac.ac_b_ex.fe_group = 0;
1850 + ac.ac_b_ex.fe_start = 0;
1851 + ac.ac_b_ex.fe_len = 0;
1852 + ac.ac_status = AC_STATUS_CONTINUE;
1853 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1859 + if (ac.ac_status != AC_STATUS_FOUND) {
1861 + * We aren't lucky definitely
1863 + DQUOT_FREE_BLOCK(inode, *len);
1867 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1868 + ac.ac_status, ac.ac_flags);
1869 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1870 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1871 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1872 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1873 + sbi->s_blocks_reserved, ac.ac_found);
1874 + printk("EXT3-fs: groups: ");
1875 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1876 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1883 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1885 + /* good news - free block(s) have been found. now it's time
1886 + * to mark block(s) in good old journaled bitmap */
1887 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1888 + + ac.ac_b_ex.fe_start
1889 + + le32_to_cpu(es->s_first_data_block);
1891 + /* we made a desicion, now mark found blocks in good old
1892 + * bitmap to be journaled */
1894 + ext3_debug("using block group %d(%d)\n",
1895 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1897 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1903 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1909 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1915 + err = ext3_journal_get_write_access(handle, gdp_bh);
1919 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1920 + + ac.ac_b_ex.fe_start
1921 + + le32_to_cpu(es->s_first_data_block);
1923 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1924 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1925 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1926 + EXT3_SB(sb)->s_itb_per_group))
1927 + ext3_error(sb, "ext3_new_block",
1928 + "Allocating block in system zone - "
1929 + "block = "E3FSBLK, block);
1930 +#ifdef AGGRESSIVE_CHECK
1931 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1932 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1934 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1936 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1937 + gdp->bg_free_blocks_count =
1938 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1939 + - ac.ac_b_ex.fe_len);
1940 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1941 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1943 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1946 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1952 + brelse(bitmap_bh);
1954 + /* drop non-allocated, but dquote'd blocks */
1955 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1956 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1958 + *len = ac.ac_b_ex.fe_len;
1959 + J_ASSERT(*len > 0);
1960 + J_ASSERT(block != 0);
1964 + /* if we've already allocated something, roll it back */
1965 + if (ac.ac_status == AC_STATUS_FOUND) {
1966 + /* FIXME: free blocks here */
1969 + DQUOT_FREE_BLOCK(inode, *len);
1970 + brelse(bitmap_bh);
1974 + if (ac.ac_buddy_page)
1975 + page_cache_release(ac.ac_buddy_page);
1976 + if (ac.ac_bitmap_page)
1977 + page_cache_release(ac.ac_bitmap_page);
1979 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1980 + /* block wasn't reserved before and we reserved it
1981 + * at the beginning of allocation. it doesn't matter
1982 + * whether we allocated anything or we failed: time
1983 + * to release reservation. NOTE: because I expect
1984 + * any multiblock request from delayed allocation
1985 + * path only, here is single block always */
1986 + ext3_mb_release_blocks(sb, 1);
1989 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1990 + atomic_inc(&sbi->s_bal_reqs);
1991 + atomic_add(*len, &sbi->s_bal_allocated);
1992 + if (*len >= ac.ac_g_ex.fe_len)
1993 + atomic_inc(&sbi->s_bal_success);
1994 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1995 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1996 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1997 + atomic_inc(&sbi->s_bal_goals);
1998 + if (ac.ac_found > ext3_mb_max_to_scan)
1999 + atomic_inc(&sbi->s_bal_breaks);
2002 + ext3_mb_store_history(sb, inode->i_ino, &ac);
2006 +EXPORT_SYMBOL(ext3_mb_new_blocks);
2008 +#ifdef EXT3_MB_HISTORY
2009 +struct ext3_mb_proc_session {
2010 + struct ext3_mb_history *history;
2011 + struct super_block *sb;
2016 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
2017 + struct ext3_mb_history *hs,
2020 + if (hs == s->history + s->max)
2022 + if (!first && hs == s->history + s->start)
2024 + while (hs->goal.fe_len == 0) {
2026 + if (hs == s->history + s->max)
2028 + if (hs == s->history + s->start)
2034 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2036 + struct ext3_mb_proc_session *s = seq->private;
2037 + struct ext3_mb_history *hs;
2041 + return SEQ_START_TOKEN;
2042 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2045 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2049 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2051 + struct ext3_mb_proc_session *s = seq->private;
2052 + struct ext3_mb_history *hs = v;
2055 + if (v == SEQ_START_TOKEN)
2056 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2058 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2061 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2063 + struct ext3_mb_history *hs = v;
2064 + char buf[20], buf2[20];
2066 + if (v == SEQ_START_TOKEN) {
2067 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2068 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2069 + "merge", "tail", "broken");
2073 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2074 + hs->goal.fe_start, hs->goal.fe_len);
2075 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2076 + hs->result.fe_start, hs->result.fe_len);
2077 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2078 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2079 + hs->cr, hs->merged ? "M" : "", hs->tail,
2080 + hs->buddy ? 1 << hs->buddy : 0);
2084 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2088 +static struct seq_operations ext3_mb_seq_history_ops = {
2089 + .start = ext3_mb_seq_history_start,
2090 + .next = ext3_mb_seq_history_next,
2091 + .stop = ext3_mb_seq_history_stop,
2092 + .show = ext3_mb_seq_history_show,
2095 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2097 + struct super_block *sb = PDE(inode)->data;
2098 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2099 + struct ext3_mb_proc_session *s;
2102 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2105 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2106 + s->history = kmalloc(size, GFP_KERNEL);
2112 + spin_lock(&sbi->s_mb_history_lock);
2113 + memcpy(s->history, sbi->s_mb_history, size);
2114 + s->max = sbi->s_mb_history_max;
2115 + s->start = sbi->s_mb_history_cur % s->max;
2116 + spin_unlock(&sbi->s_mb_history_lock);
2118 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2120 + struct seq_file *m = (struct seq_file *)file->private_data;
2123 + kfree(s->history);
2130 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2132 + struct seq_file *seq = (struct seq_file *)file->private_data;
2133 + struct ext3_mb_proc_session *s = seq->private;
2134 + kfree(s->history);
2136 + return seq_release(inode, file);
2139 +static struct file_operations ext3_mb_seq_history_fops = {
2140 + .owner = THIS_MODULE,
2141 + .open = ext3_mb_seq_history_open,
2143 + .llseek = seq_lseek,
2144 + .release = ext3_mb_seq_history_release,
2147 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2149 + struct super_block *sb = seq->private;
2150 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2153 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2157 + return (void *) group;
2160 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2162 + struct super_block *sb = seq->private;
2163 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2167 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2170 + return (void *) group;;
2173 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2175 + struct super_block *sb = seq->private;
2176 + long group = (long) v, i;
2178 + struct ext3_group_info info;
2179 + unsigned short counters[16];
2184 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2185 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2186 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2187 + "2^11", "2^12", "2^13");
2189 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2190 + sizeof(struct ext3_group_info);
2191 + ext3_lock_group(sb, group);
2192 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2193 + ext3_unlock_group(sb, group);
2195 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2198 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2199 + sg.info.bb_fragments, sg.info.bb_first_free);
2200 + for (i = 0; i <= 13; i++)
2201 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2202 + sg.info.bb_counters[i] : 0);
2203 + seq_printf(seq, " ]\n");
2208 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2212 +static struct seq_operations ext3_mb_seq_groups_ops = {
2213 + .start = ext3_mb_seq_groups_start,
2214 + .next = ext3_mb_seq_groups_next,
2215 + .stop = ext3_mb_seq_groups_stop,
2216 + .show = ext3_mb_seq_groups_show,
2219 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2221 + struct super_block *sb = PDE(inode)->data;
2224 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2226 + struct seq_file *m = (struct seq_file *)file->private_data;
2233 +static struct file_operations ext3_mb_seq_groups_fops = {
2234 + .owner = THIS_MODULE,
2235 + .open = ext3_mb_seq_groups_open,
2237 + .llseek = seq_lseek,
2238 + .release = seq_release,
2241 +static void ext3_mb_history_release(struct super_block *sb)
2243 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2246 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2247 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2248 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2249 + remove_proc_entry(name, proc_root_ext3);
2251 + if (sbi->s_mb_history)
2252 + kfree(sbi->s_mb_history);
2255 +static void ext3_mb_history_init(struct super_block *sb)
2257 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2261 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2262 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2263 + if (sbi->s_mb_proc != NULL) {
2264 + struct proc_dir_entry *p;
2265 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2267 + p->proc_fops = &ext3_mb_seq_history_fops;
2270 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2272 + p->proc_fops = &ext3_mb_seq_groups_fops;
2277 + sbi->s_mb_history_max = 1000;
2278 + sbi->s_mb_history_cur = 0;
2279 + spin_lock_init(&sbi->s_mb_history_lock);
2280 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2281 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2282 + memset(sbi->s_mb_history, 0, i);
2283 + /* if we can't allocate history, then we simple won't use it */
2287 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2288 + struct ext3_allocation_context *ac)
2290 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2291 + struct ext3_mb_history h;
2293 + if (likely(sbi->s_mb_history == NULL))
2296 + h.pid = current->pid;
2298 + h.goal = ac->ac_g_ex;
2299 + h.result = ac->ac_b_ex;
2300 + h.found = ac->ac_found;
2301 + h.cr = ac->ac_criteria;
2302 + h.groups = ac->ac_groups_scanned;
2303 + h.tail = ac->ac_tail;
2304 + h.buddy = ac->ac_buddy;
2306 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2307 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2310 + spin_lock(&sbi->s_mb_history_lock);
2311 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2312 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2313 + sbi->s_mb_history_cur = 0;
2314 + spin_unlock(&sbi->s_mb_history_lock);
2318 +#define ext3_mb_history_release(sb)
2319 +#define ext3_mb_history_init(sb)
2322 +int ext3_mb_init_backend(struct super_block *sb)
2324 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2325 + int i, j, len, metalen;
2326 + int num_meta_group_infos =
2327 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2328 + EXT3_DESC_PER_BLOCK_BITS(sb);
2329 + struct ext3_group_info **meta_group_info;
2331 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2332 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2333 + * So a two level scheme suffices for now. */
2334 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2335 + num_meta_group_infos, GFP_KERNEL);
2336 + if (sbi->s_group_info == NULL) {
2337 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2340 + sbi->s_buddy_cache = new_inode(sb);
2341 + if (sbi->s_buddy_cache == NULL) {
2342 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2346 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2347 + for (i = 0; i < num_meta_group_infos; i++) {
2348 + if ((i + 1) == num_meta_group_infos)
2349 + metalen = sizeof(*meta_group_info) *
2350 + (sbi->s_groups_count -
2351 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2352 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2353 + if (meta_group_info == NULL) {
2354 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2356 + goto err_freemeta;
2358 + sbi->s_group_info[i] = meta_group_info;
2362 + * calculate needed size. if change bb_counters size,
2363 + * don't forget about ext3_mb_generate_buddy()
2365 + len = sizeof(struct ext3_group_info);
2366 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2367 + for (i = 0; i < sbi->s_groups_count; i++) {
2368 + struct ext3_group_desc * desc;
2371 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2372 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2374 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2375 + if (meta_group_info[j] == NULL) {
2376 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2378 + goto err_freebuddy;
2380 + desc = ext3_get_group_desc(sb, i, NULL);
2381 + if (desc == NULL) {
2382 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2383 + goto err_freebuddy;
2385 + memset(meta_group_info[j], 0, len);
2386 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2387 + &meta_group_info[j]->bb_state);
2388 + meta_group_info[j]->bb_free =
2389 + le16_to_cpu(desc->bg_free_blocks_count);
2396 + kfree(EXT3_GROUP_INFO(sb, i));
2399 + i = num_meta_group_infos;
2402 + kfree(sbi->s_group_info[i]);
2403 + iput(sbi->s_buddy_cache);
2405 + kfree(sbi->s_group_info);
2409 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2411 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2412 + struct inode *root = sb->s_root->d_inode;
2413 + unsigned i, offset, max;
2414 + struct dentry *dentry;
2416 + if (!test_opt(sb, MBALLOC))
2419 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2421 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2422 + if (sbi->s_mb_offsets == NULL) {
2423 + clear_opt(sbi->s_mount_opt, MBALLOC);
2426 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2427 + if (sbi->s_mb_maxs == NULL) {
2428 + clear_opt(sbi->s_mount_opt, MBALLOC);
2429 + kfree(sbi->s_mb_maxs);
2433 + /* order 0 is regular bitmap */
2434 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2435 + sbi->s_mb_offsets[0] = 0;
2439 + max = sb->s_blocksize << 2;
2441 + sbi->s_mb_offsets[i] = offset;
2442 + sbi->s_mb_maxs[i] = max;
2443 + offset += 1 << (sb->s_blocksize_bits - i);
2446 + } while (i <= sb->s_blocksize_bits + 1);
2448 + /* init file for buddy data */
2449 + if ((i = ext3_mb_init_backend(sb))) {
2450 + clear_opt(sbi->s_mount_opt, MBALLOC);
2451 + kfree(sbi->s_mb_offsets);
2452 + kfree(sbi->s_mb_maxs);
2456 + spin_lock_init(&sbi->s_reserve_lock);
2457 + spin_lock_init(&sbi->s_md_lock);
2458 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2459 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2460 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2461 + spin_lock_init(&sbi->s_bal_lock);
2463 + /* remove old on-disk buddy file */
2464 + mutex_lock(&root->i_mutex);
2465 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2466 + if (dentry->d_inode != NULL) {
2467 + i = vfs_unlink(root, dentry);
2469 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2472 + mutex_unlock(&root->i_mutex);
2474 + ext3_mb_history_init(sb);
2476 + printk("EXT3-fs: mballoc enabled\n");
2480 +int ext3_mb_release(struct super_block *sb)
2482 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2483 + int i, num_meta_group_infos;
2485 + if (!test_opt(sb, MBALLOC))
2488 + /* release freed, non-committed blocks */
2489 + spin_lock(&sbi->s_md_lock);
2490 + list_splice_init(&sbi->s_closed_transaction,
2491 + &sbi->s_committed_transaction);
2492 + list_splice_init(&sbi->s_active_transaction,
2493 + &sbi->s_committed_transaction);
2494 + spin_unlock(&sbi->s_md_lock);
2495 + ext3_mb_free_committed_blocks(sb);
2497 + if (sbi->s_group_info) {
2498 + for (i = 0; i < sbi->s_groups_count; i++)
2499 + kfree(EXT3_GROUP_INFO(sb, i));
2500 + num_meta_group_infos = (sbi->s_groups_count +
2501 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2502 + EXT3_DESC_PER_BLOCK_BITS(sb);
2503 + for (i = 0; i < num_meta_group_infos; i++)
2504 + kfree(sbi->s_group_info[i]);
2505 + kfree(sbi->s_group_info);
2507 + if (sbi->s_mb_offsets)
2508 + kfree(sbi->s_mb_offsets);
2509 + if (sbi->s_mb_maxs)
2510 + kfree(sbi->s_mb_maxs);
2511 + if (sbi->s_buddy_cache)
2512 + iput(sbi->s_buddy_cache);
2513 + if (sbi->s_blocks_reserved)
2514 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2515 + sbi->s_blocks_reserved);
2516 + if (ext3_mb_stats) {
2517 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2518 + atomic_read(&sbi->s_bal_allocated),
2519 + atomic_read(&sbi->s_bal_reqs),
2520 + atomic_read(&sbi->s_bal_success));
2521 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2522 + "%u 2^N hits, %u breaks\n",
2523 + atomic_read(&sbi->s_bal_ex_scanned),
2524 + atomic_read(&sbi->s_bal_goals),
2525 + atomic_read(&sbi->s_bal_2orders),
2526 + atomic_read(&sbi->s_bal_breaks));
2527 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2528 + sbi->s_mb_buddies_generated++,
2529 + sbi->s_mb_generation_time);
2532 + ext3_mb_history_release(sb);
2537 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2539 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2540 + int err, i, count = 0, count2 = 0;
2541 + struct ext3_free_metadata *md;
2542 + struct ext3_buddy e3b;
2544 + if (list_empty(&sbi->s_committed_transaction))
2547 + /* there is committed blocks to be freed yet */
2549 + /* get next array of blocks */
2551 + spin_lock(&sbi->s_md_lock);
2552 + if (!list_empty(&sbi->s_committed_transaction)) {
2553 + md = list_entry(sbi->s_committed_transaction.next,
2554 + struct ext3_free_metadata, list);
2555 + list_del(&md->list);
2557 + spin_unlock(&sbi->s_md_lock);
2562 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2563 + md->num, md->group, md);
2565 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2566 + /* we expect to find existing buddy because it's pinned */
2569 + /* there are blocks to put in buddy to make them really free */
2572 + ext3_lock_group(sb, md->group);
2573 + for (i = 0; i < md->num; i++) {
2574 + mb_debug(" %u", md->blocks[i]);
2575 + mb_free_blocks(&e3b, md->blocks[i], 1);
2578 + ext3_unlock_group(sb, md->group);
2580 + /* balance refcounts from ext3_mb_free_metadata() */
2581 + page_cache_release(e3b.bd_buddy_page);
2582 + page_cache_release(e3b.bd_bitmap_page);
2585 + ext3_mb_release_desc(&e3b);
2588 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2591 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2593 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2595 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2598 + /* new transaction! time to close last one and free blocks for
2599 + * committed transaction. we know that only transaction can be
2600 + * active, so previos transaction can be being logged and we
2601 + * know that transaction before previous is known to be already
2602 + * logged. this means that now we may free blocks freed in all
2603 + * transactions before previous one. hope I'm clear enough ... */
2605 + spin_lock(&sbi->s_md_lock);
2606 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2607 + mb_debug("new transaction %lu, old %lu\n",
2608 + (unsigned long) handle->h_transaction->t_tid,
2609 + (unsigned long) sbi->s_last_transaction);
2610 + list_splice_init(&sbi->s_closed_transaction,
2611 + &sbi->s_committed_transaction);
2612 + list_splice_init(&sbi->s_active_transaction,
2613 + &sbi->s_closed_transaction);
2614 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2616 + spin_unlock(&sbi->s_md_lock);
2618 + ext3_mb_free_committed_blocks(sb);
2621 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2622 + int group, int block, int count)
2624 + struct ext3_group_info *db = e3b->bd_info;
2625 + struct super_block *sb = e3b->bd_sb;
2626 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2627 + struct ext3_free_metadata *md;
2630 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2631 + J_ASSERT(e3b->bd_buddy_page != NULL);
2633 + ext3_lock_group(sb, group);
2634 + for (i = 0; i < count; i++) {
2635 + md = db->bb_md_cur;
2636 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2637 + db->bb_md_cur = NULL;
2642 + ext3_unlock_group(sb, group);
2643 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2647 + md->group = group;
2649 + ext3_lock_group(sb, group);
2650 + if (db->bb_md_cur == NULL) {
2651 + spin_lock(&sbi->s_md_lock);
2652 + list_add(&md->list, &sbi->s_active_transaction);
2653 + spin_unlock(&sbi->s_md_lock);
2654 + /* protect buddy cache from being freed,
2655 + * otherwise we'll refresh it from
2656 + * on-disk bitmap and lose not-yet-available
2658 + page_cache_get(e3b->bd_buddy_page);
2659 + page_cache_get(e3b->bd_bitmap_page);
2660 + db->bb_md_cur = md;
2661 + db->bb_tid = handle->h_transaction->t_tid;
2662 + mb_debug("new md 0x%p for group %u\n",
2666 + md = db->bb_md_cur;
2670 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2671 + md->blocks[md->num] = block + i;
2673 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2674 + /* no more space, put full container on a sb's list */
2675 + db->bb_md_cur = NULL;
2678 + ext3_unlock_group(sb, group);
2682 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2683 + ext3_fsblk_t block, unsigned long count,
2684 + int metadata, unsigned long *freed)
2686 + struct buffer_head *bitmap_bh = NULL;
2687 + struct ext3_group_desc *gdp;
2688 + struct ext3_super_block *es;
2689 + unsigned long bit, overflow;
2690 + struct buffer_head *gd_bh;
2691 + unsigned long block_group;
2692 + struct ext3_sb_info *sbi;
2693 + struct super_block *sb;
2694 + struct ext3_buddy e3b;
2700 + printk ("ext3_free_blocks: nonexistent device");
2704 + ext3_mb_poll_new_transaction(sb, handle);
2706 + sbi = EXT3_SB(sb);
2707 + es = EXT3_SB(sb)->s_es;
2708 + if (block < le32_to_cpu(es->s_first_data_block) ||
2709 + block + count < block ||
2710 + block + count > le32_to_cpu(es->s_blocks_count)) {
2711 + ext3_error (sb, "ext3_free_blocks",
2712 + "Freeing blocks not in datazone - "
2713 + "block = %lu, count = %lu", block, count);
2714 + goto error_return;
2717 + ext3_debug("freeing block %lu\n", block);
2721 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2722 + EXT3_BLOCKS_PER_GROUP(sb);
2723 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2724 + EXT3_BLOCKS_PER_GROUP(sb);
2726 + * Check to see if we are freeing blocks across a group
2729 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2730 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2731 + count -= overflow;
2733 + brelse(bitmap_bh);
2734 + bitmap_bh = read_block_bitmap(sb, block_group);
2736 + goto error_return;
2737 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2739 + goto error_return;
2741 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2742 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2743 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2744 + EXT3_SB(sb)->s_itb_per_group) ||
2745 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2746 + EXT3_SB(sb)->s_itb_per_group))
2747 + ext3_error (sb, "ext3_free_blocks",
2748 + "Freeing blocks in system zones - "
2749 + "Block = %lu, count = %lu",
2752 + BUFFER_TRACE(bitmap_bh, "getting write access");
2753 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2755 + goto error_return;
2758 + * We are about to modify some metadata. Call the journal APIs
2759 + * to unshare ->b_data if a currently-committing transaction is
2762 + BUFFER_TRACE(gd_bh, "get_write_access");
2763 + err = ext3_journal_get_write_access(handle, gd_bh);
2765 + goto error_return;
2767 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2769 + goto error_return;
2771 +#ifdef AGGRESSIVE_CHECK
2774 + for (i = 0; i < count; i++)
2775 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2778 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2780 + /* We dirtied the bitmap block */
2781 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2782 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2785 + /* blocks being freed are metadata. these blocks shouldn't
2786 + * be used until this transaction is committed */
2787 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2789 + ext3_lock_group(sb, block_group);
2790 + mb_free_blocks(&e3b, bit, count);
2791 + ext3_unlock_group(sb, block_group);
2794 + spin_lock(sb_bgl_lock(sbi, block_group));
2795 + gdp->bg_free_blocks_count =
2796 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2797 + spin_unlock(sb_bgl_lock(sbi, block_group));
2798 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2800 + ext3_mb_release_desc(&e3b);
2804 + /* And the group descriptor block */
2805 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2806 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2807 + if (!err) err = ret;
2809 + if (overflow && !err) {
2816 + brelse(bitmap_bh);
2817 + ext3_std_error(sb, err);
2821 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2823 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2824 + int free, ret = -ENOSPC;
2826 + BUG_ON(blocks < 0);
2827 + spin_lock(&sbi->s_reserve_lock);
2828 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2829 + if (blocks <= free - sbi->s_blocks_reserved) {
2830 + sbi->s_blocks_reserved += blocks;
2833 + spin_unlock(&sbi->s_reserve_lock);
2837 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2839 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2841 + BUG_ON(blocks < 0);
2842 + spin_lock(&sbi->s_reserve_lock);
2843 + sbi->s_blocks_reserved -= blocks;
2844 + WARN_ON(sbi->s_blocks_reserved < 0);
2845 + if (sbi->s_blocks_reserved < 0)
2846 + sbi->s_blocks_reserved = 0;
2847 + spin_unlock(&sbi->s_reserve_lock);
2850 +ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
2851 + ext3_fsblk_t goal, int *errp)
2856 + if (!test_opt(inode->i_sb, MBALLOC)) {
2857 + ret = ext3_new_block_old(handle, inode, goal, errp);
2861 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2866 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2867 + ext3_fsblk_t block, unsigned long count, int metadata)
2869 + struct super_block *sb;
2870 + unsigned long freed;
2873 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2874 + ext3_free_blocks_sb(handle, sb, block, count, &freed);
2876 + ext3_mb_free_blocks(handle, inode, block, count, metadata,
2879 + DQUOT_FREE_BLOCK(inode, freed);
2883 +#define EXT3_ROOT "ext3"
2884 +#define EXT3_MB_STATS_NAME "mb_stats"
2885 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2886 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2887 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2889 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2890 + int count, int *eof, void *data)
2898 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2903 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2904 + unsigned long count, void *data)
2908 + if (count >= sizeof(str)) {
2909 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2910 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2911 + return -EOVERFLOW;
2914 + if (copy_from_user(str, buffer, count))
2917 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2918 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2922 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2923 + int count, int *eof, void *data)
2931 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2936 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2937 + unsigned long count, void *data)
2942 + if (count >= sizeof(str)) {
2943 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2944 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2945 + return -EOVERFLOW;
2948 + if (copy_from_user(str, buffer, count))
2951 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2952 + value = simple_strtol(str, NULL, 0);
2956 + ext3_mb_max_to_scan = value;
2961 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2962 + int count, int *eof, void *data)
2970 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2975 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2976 + unsigned long count, void *data)
2981 + if (count >= sizeof(str)) {
2982 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2983 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2984 + return -EOVERFLOW;
2987 + if (copy_from_user(str, buffer, count))
2990 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2991 + value = simple_strtol(str, NULL, 0);
2995 + ext3_mb_min_to_scan = value;
3000 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
3001 + int count, int *eof, void *data)
3009 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
3014 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
3015 + unsigned long count, void *data)
3020 + if (count >= sizeof(str)) {
3021 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
3022 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
3023 + return -EOVERFLOW;
3026 + if (copy_from_user(str, buffer, count))
3029 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
3030 + value = simple_strtol(str, NULL, 0);
3034 + ext3_mb_order2_reqs = value;
3039 +int __init init_ext3_proc(void)
3041 + struct proc_dir_entry *proc_ext3_mb_stats;
3042 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3043 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3044 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3046 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3047 + if (proc_root_ext3 == NULL) {
3048 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3052 + /* Initialize EXT3_MB_STATS_NAME */
3053 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3054 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3055 + if (proc_ext3_mb_stats == NULL) {
3056 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3057 + EXT3_MB_STATS_NAME);
3058 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3062 + proc_ext3_mb_stats->data = NULL;
3063 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3064 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3066 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3067 + proc_ext3_mb_max_to_scan = create_proc_entry(
3068 + EXT3_MB_MAX_TO_SCAN_NAME,
3069 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3070 + if (proc_ext3_mb_max_to_scan == NULL) {
3071 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3072 + EXT3_MB_MAX_TO_SCAN_NAME);
3073 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3074 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3078 + proc_ext3_mb_max_to_scan->data = NULL;
3079 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3080 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3082 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3083 + proc_ext3_mb_min_to_scan = create_proc_entry(
3084 + EXT3_MB_MIN_TO_SCAN_NAME,
3085 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3086 + if (proc_ext3_mb_min_to_scan == NULL) {
3087 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3088 + EXT3_MB_MIN_TO_SCAN_NAME);
3089 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3090 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3091 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3095 + proc_ext3_mb_min_to_scan->data = NULL;
3096 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3097 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3099 + /* Initialize EXT3_ORDER2_REQ */
3100 + proc_ext3_mb_order2_req = create_proc_entry(
3101 + EXT3_MB_ORDER2_REQ,
3102 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3103 + if (proc_ext3_mb_order2_req == NULL) {
3104 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3105 + EXT3_MB_ORDER2_REQ);
3106 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3107 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3108 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3109 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3113 + proc_ext3_mb_order2_req->data = NULL;
3114 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3115 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3120 +void exit_ext3_proc(void)
3122 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3123 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3124 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3125 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3126 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3128 Index: linux-stage/fs/ext3/Makefile
3129 ===================================================================
3130 --- linux-stage.orig/fs/ext3/Makefile 2006-07-16 02:29:43.000000000 +0800
3131 +++ linux-stage/fs/ext3/Makefile 2006-07-16 02:29:49.000000000 +0800
3132 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
3134 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3135 ioctl.o namei.o super.o symlink.o hash.o resize.o \
3137 + extents.o mballoc.o
3139 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3140 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o