1 Index: linux-2.6.5-7.282-full/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.5-7.282-full.orig/include/linux/ext3_fs.h 2006-10-24 22:18:28.000000000 +0400
4 +++ linux-2.6.5-7.282-full/include/linux/ext3_fs.h 2006-10-24 22:18:28.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 @@ -339,6 +347,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 @@ -361,6 +370,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 @@ -700,7 +717,9 @@ 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_old(handle_t *, struct inode *, unsigned long,
51 extern unsigned long ext3_count_free_blocks (struct super_block *);
52 extern void ext3_check_blocks_bitmap (struct super_block *);
53 extern struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
54 @@ -824,6 +843,17 @@ extern void ext3_extents_initialize_bloc
55 extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
56 unsigned int cmd, unsigned long arg);
59 +extern long ext3_mb_stats;
60 +extern long ext3_mb_max_to_scan;
61 +extern int ext3_mb_init(struct super_block *, int);
62 +extern int ext3_mb_release(struct super_block *);
63 +extern int ext3_mb_new_blocks(handle_t *, struct inode *, unsigned long, int *, int, int *);
64 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
65 +extern void ext3_mb_release_blocks(struct super_block *, int);
66 +int __init init_ext3_proc(void);
67 +void exit_ext3_proc(void);
69 #endif /* __KERNEL__ */
71 #define EXT3_IOC_CREATE_INUM _IOW('f', 5, long)
72 Index: linux-2.6.5-7.282-full/include/linux/ext3_fs_sb.h
73 ===================================================================
74 --- linux-2.6.5-7.282-full.orig/include/linux/ext3_fs_sb.h 2006-10-24 22:18:28.000000000 +0400
75 +++ linux-2.6.5-7.282-full/include/linux/ext3_fs_sb.h 2006-10-24 22:18:28.000000000 +0400
78 #include <linux/blockgroup_lock.h>
79 #include <linux/percpu_counter.h>
80 +#include <linux/list.h>
83 #include <linux/rbtree.h>
84 +#include <linux/proc_fs.h>
86 +struct ext3_buddy_group_blocks;
87 +struct ext3_mb_history;
88 +#define EXT3_BB_MAX_BLOCKS
91 * third extended-fs super-block data in memory
92 @@ -78,6 +84,43 @@ struct ext3_sb_info {
93 struct timer_list turn_ro_timer; /* For turning read-only (crash simulation) */
94 wait_queue_head_t ro_wait_queue; /* For people waiting for the fs to go read-only */
97 + /* for buddy allocator */
98 + struct ext3_group_info ***s_group_info;
99 + struct inode *s_buddy_cache;
100 + long s_blocks_reserved;
101 + spinlock_t s_reserve_lock;
102 + struct list_head s_active_transaction;
103 + struct list_head s_closed_transaction;
104 + struct list_head s_committed_transaction;
105 + spinlock_t s_md_lock;
106 + tid_t s_last_transaction;
108 + unsigned short *s_mb_offsets, *s_mb_maxs;
109 + unsigned long s_stripe;
111 + /* history to debug policy */
112 + struct ext3_mb_history *s_mb_history;
113 + int s_mb_history_cur;
114 + int s_mb_history_max;
115 + struct proc_dir_entry *s_mb_proc;
116 + spinlock_t s_mb_history_lock;
118 + /* stats for buddy allocator */
119 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
120 + atomic_t s_bal_success; /* we found long enough chunks */
121 + atomic_t s_bal_allocated; /* in blocks */
122 + atomic_t s_bal_ex_scanned; /* total extents scanned */
123 + atomic_t s_bal_goals; /* goal hits */
124 + atomic_t s_bal_breaks; /* too long searches */
125 + atomic_t s_bal_2orders; /* 2^order hits */
126 + spinlock_t s_bal_lock;
127 + unsigned long s_mb_buddies_generated;
128 + unsigned long long s_mb_generation_time;
131 +#define EXT3_GROUP_INFO(sb, group) \
132 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
133 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
135 #endif /* _LINUX_EXT3_FS_SB */
136 Index: linux-2.6.5-7.282-full/fs/ext3/super.c
137 ===================================================================
138 --- linux-2.6.5-7.282-full.orig/fs/ext3/super.c 2006-10-24 22:18:28.000000000 +0400
139 +++ linux-2.6.5-7.282-full/fs/ext3/super.c 2006-10-24 22:18:28.000000000 +0400
140 @@ -389,6 +389,7 @@ void ext3_put_super (struct super_block
141 struct ext3_super_block *es = sbi->s_es;
144 + ext3_mb_release(sb);
145 ext3_ext_release(sb);
146 ext3_xattr_put_super(sb);
147 journal_destroy(sbi->s_journal);
148 @@ -588,6 +589,7 @@ enum {
150 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
151 Opt_extents, Opt_noextents, Opt_extdebug,
152 + Opt_mballoc, Opt_nomballoc, Opt_stripe,
155 static match_table_t tokens = {
156 @@ -634,6 +636,9 @@ static match_table_t tokens = {
157 {Opt_extents, "extents"},
158 {Opt_noextents, "noextents"},
159 {Opt_extdebug, "extdebug"},
160 + {Opt_mballoc, "mballoc"},
161 + {Opt_nomballoc, "nomballoc"},
162 + {Opt_stripe, "stripe=%u"},
163 {Opt_barrier, "barrier=%u"},
166 @@ -859,6 +864,19 @@ static int parse_options (char * options
168 set_opt (sbi->s_mount_opt, EXTDEBUG);
171 + set_opt(sbi->s_mount_opt, MBALLOC);
173 + case Opt_nomballoc:
174 + clear_opt(sbi->s_mount_opt, MBALLOC);
177 + if (match_int(&args[0], &option))
181 + sbi->s_stripe = option;
185 "EXT3-fs: Unrecognized mount option \"%s\" "
186 @@ -1512,6 +1530,7 @@ static int ext3_fill_super (struct super
187 ext3_count_dirs(sb));
190 + ext3_mb_init(sb, needs_recovery);
194 @@ -2160,7 +2179,13 @@ static struct file_system_type ext3_fs_t
196 static int __init init_ext3_fs(void)
198 - int err = init_ext3_xattr();
201 + err = init_ext3_proc();
205 + err = init_ext3_xattr();
208 err = init_inodecache();
209 @@ -2189,6 +2214,7 @@ static void __exit exit_ext3_fs(void)
210 unregister_filesystem(&ext3_fs_type);
211 destroy_inodecache();
216 int ext3_prep_san_write(struct inode *inode, long *blocks,
217 Index: linux-2.6.5-7.282-full/fs/ext3/extents.c
218 ===================================================================
219 --- linux-2.6.5-7.282-full.orig/fs/ext3/extents.c 2006-10-24 22:18:28.000000000 +0400
220 +++ linux-2.6.5-7.282-full/fs/ext3/extents.c 2006-10-24 22:18:28.000000000 +0400
221 @@ -779,7 +779,7 @@ cleanup:
222 for (i = 0; i < depth; i++) {
225 - ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
226 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
230 @@ -1438,7 +1438,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
231 path->p_idx->ei_leaf);
232 bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
233 ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
234 - ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
235 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
239 @@ -1923,10 +1923,12 @@ ext3_remove_blocks(struct ext3_extents_t
240 int needed = ext3_remove_blocks_credits(tree, ex, from, to);
241 handle_t *handle = ext3_journal_start(tree->inode, needed);
242 struct buffer_head *bh;
244 + int i, metadata = 0;
247 return PTR_ERR(handle);
248 + if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
250 if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
252 unsigned long num, start;
253 @@ -1938,7 +1940,7 @@ ext3_remove_blocks(struct ext3_extents_t
254 bh = sb_find_get_block(tree->inode->i_sb, start + i);
255 ext3_forget(handle, 0, tree->inode, bh, start + i);
257 - ext3_free_blocks(handle, tree->inode, start, num);
258 + ext3_free_blocks(handle, tree->inode, start, num, metadata);
259 } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
260 printk("strange request: removal %lu-%lu from %u:%u\n",
261 from, to, ex->ee_block, ex->ee_len);
262 Index: linux-2.6.5-7.282-full/fs/ext3/inode.c
263 ===================================================================
264 --- linux-2.6.5-7.282-full.orig/fs/ext3/inode.c 2006-10-24 22:18:28.000000000 +0400
265 +++ linux-2.6.5-7.282-full/fs/ext3/inode.c 2006-10-24 22:18:28.000000000 +0400
266 @@ -574,7 +574,7 @@ static int ext3_alloc_branch(handle_t *h
267 ext3_journal_forget(handle, branch[i].bh);
269 for (i = 0; i < keys; i++)
270 - ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1);
271 + ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1, 1);
275 @@ -675,7 +675,7 @@ err_out:
277 for (i = 0; i < num; i++)
278 ext3_free_blocks(handle, inode,
279 - le32_to_cpu(where[i].key), 1);
280 + le32_to_cpu(where[i].key), 1, 1);
284 @@ -1837,7 +1837,7 @@ ext3_clear_blocks(handle_t *handle, stru
288 - ext3_free_blocks(handle, inode, block_to_free, count);
289 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
293 @@ -2008,7 +2008,7 @@ static void ext3_free_branches(handle_t
294 ext3_journal_test_restart(handle, inode);
297 - ext3_free_blocks(handle, inode, nr, 1);
298 + ext3_free_blocks(handle, inode, nr, 1, 1);
302 Index: linux-2.6.5-7.282-full/fs/ext3/balloc.c
303 ===================================================================
304 --- linux-2.6.5-7.282-full.orig/fs/ext3/balloc.c 2006-08-30 18:12:13.000000000 +0400
305 +++ linux-2.6.5-7.282-full/fs/ext3/balloc.c 2006-10-24 22:18:28.000000000 +0400
306 @@ -78,7 +78,7 @@ struct ext3_group_desc * ext3_get_group_
308 * Return buffer_head on success or NULL in case of failure.
310 -static struct buffer_head *
311 +struct buffer_head *
312 read_block_bitmap(struct super_block *sb, unsigned int block_group)
314 struct ext3_group_desc * desc;
315 @@ -274,7 +274,7 @@ void ext3_discard_reservation(struct ino
318 /* Free given blocks, update quota and i_blocks field */
319 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
320 +void ext3_free_blocks_old(handle_t *handle, struct inode *inode,
321 unsigned long block, unsigned long count)
323 struct buffer_head *bitmap_bh = NULL;
324 @@ -1142,7 +1142,7 @@ int ext3_should_retry_alloc(struct super
325 * bitmap, and then for any free bit if that fails.
326 * This function also updates quota and i_blocks field.
328 -int ext3_new_block(handle_t *handle, struct inode *inode,
329 +int ext3_new_block_old(handle_t *handle, struct inode *inode,
330 unsigned long goal, int *errp)
332 struct buffer_head *bitmap_bh = NULL;
333 Index: linux-2.6.5-7.282-full/fs/ext3/xattr.c
334 ===================================================================
335 --- linux-2.6.5-7.282-full.orig/fs/ext3/xattr.c 2006-10-24 22:18:28.000000000 +0400
336 +++ linux-2.6.5-7.282-full/fs/ext3/xattr.c 2006-10-24 22:18:28.000000000 +0400
337 @@ -1371,7 +1371,7 @@ ext3_xattr_set_handle2(handle_t *handle,
338 new_bh = sb_getblk(sb, block);
341 - ext3_free_blocks(handle, inode, block, 1);
342 + ext3_free_blocks(handle, inode, block, 1, 1);
346 @@ -1411,7 +1411,7 @@ getblk_failed:
347 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
348 /* Free the old block. */
349 ea_bdebug(old_bh, "freeing");
350 - ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1);
351 + ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1, 1);
353 /* ext3_forget() calls bforget() for us, but we
354 let our caller release old_bh, so we need to
355 @@ -1519,7 +1519,7 @@ ext3_xattr_delete_inode(handle_t *handle
356 mb_cache_entry_free(ce);
359 - ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1);
360 + ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1, 1);
362 ext3_forget(handle, 1, inode, bh, EXT3_I(inode)->i_file_acl);
364 Index: linux-2.6.5-7.282-full/fs/ext3/mballoc.c
365 ===================================================================
366 --- linux-2.6.5-7.282-full.orig/fs/ext3/mballoc.c 2006-10-23 18:07:54.821533176 +0400
367 +++ linux-2.6.5-7.282-full/fs/ext3/mballoc.c 2006-10-24 22:20:45.000000000 +0400
370 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
371 + * Written by Alex Tomas <alex@clusterfs.com>
373 + * This program is free software; you can redistribute it and/or modify
374 + * it under the terms of the GNU General Public License version 2 as
375 + * published by the Free Software Foundation.
377 + * This program is distributed in the hope that it will be useful,
378 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
379 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
380 + * GNU General Public License for more details.
382 + * You should have received a copy of the GNU General Public Licens
383 + * along with this program; if not, write to the Free Software
384 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
389 + * mballoc.c contains the multiblocks allocation routines
392 +#include <linux/config.h>
393 +#include <linux/time.h>
394 +#include <linux/fs.h>
395 +#include <linux/namei.h>
396 +#include <linux/jbd.h>
397 +#include <linux/ext3_fs.h>
398 +#include <linux/ext3_jbd.h>
399 +#include <linux/quotaops.h>
400 +#include <linux/buffer_head.h>
401 +#include <linux/module.h>
402 +#include <linux/swap.h>
403 +#include <linux/proc_fs.h>
404 +#include <linux/pagemap.h>
405 +#include <linux/seq_file.h>
409 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
410 + * - track min/max extents in each group for better group selection
411 + * - mb_mark_used() may allocate chunk right after splitting buddy
412 + * - special flag to advice allocator to look for requested + N blocks
413 + * this may improve interaction between extents and mballoc
414 + * - tree of groups sorted by number of free blocks
415 + * - percpu reservation code (hotpath)
420 + * with AGRESSIVE_CHECK allocator runs consistency checks over
421 + * structures. these checks slow things down a lot
423 +#define AGGRESSIVE_CHECK__
429 +#define mb_debug(fmt,a...) printk(fmt, ##a)
431 +#define mb_debug(fmt,a...)
435 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
436 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
438 +#define EXT3_MB_HISTORY
441 + * How long mballoc can look for a best extent (in found extents)
443 +long ext3_mb_max_to_scan = 500;
446 + * How long mballoc must look for a best extent
448 +long ext3_mb_min_to_scan = 30;
451 + * with 'ext3_mb_stats' allocator will collect stats that will be
452 + * shown at umount. The collecting costs though!
455 +long ext3_mb_stats = 1;
458 + * for which requests use 2^N search using buddies
460 +long ext3_mb_order2_reqs = 8;
462 +#ifdef EXT3_BB_MAX_BLOCKS
463 +#undef EXT3_BB_MAX_BLOCKS
465 +#define EXT3_BB_MAX_BLOCKS 30
467 +struct ext3_free_metadata {
468 + unsigned short group;
469 + unsigned short num;
470 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
471 + struct list_head list;
474 +struct ext3_group_info {
475 + unsigned long bb_state;
476 + unsigned long bb_tid;
477 + struct ext3_free_metadata *bb_md_cur;
478 + unsigned short bb_first_free;
479 + unsigned short bb_free;
480 + unsigned short bb_fragments;
481 + unsigned short bb_counters[];
485 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
486 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
488 +#define EXT3_MB_GRP_NEED_INIT(grp) \
489 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
491 +struct ext3_free_extent {
497 +struct ext3_allocation_context {
498 + struct super_block *ac_sb;
501 + struct ext3_free_extent ac_g_ex;
503 + /* the best found extent */
504 + struct ext3_free_extent ac_b_ex;
506 + /* number of iterations done. we have to track to limit searching */
507 + unsigned long ac_ex_scanned;
508 + __u16 ac_groups_scanned;
513 + __u8 ac_flags; /* allocation hints */
516 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
517 + * N > 0, the field stores N, otherwise 0 */
519 + struct page *ac_buddy_page;
520 + struct page *ac_bitmap_page;
523 +#define AC_STATUS_CONTINUE 1
524 +#define AC_STATUS_FOUND 2
525 +#define AC_STATUS_BREAK 3
527 +struct ext3_mb_history {
528 + struct ext3_free_extent goal; /* goal allocation */
529 + struct ext3_free_extent result; /* result allocation */
532 + __u16 found; /* how many extents have been found */
533 + __u16 groups; /* how many groups have been scanned */
534 + __u16 tail; /* what tail broke some buddy */
535 + __u16 buddy; /* buddy the tail ^^^ broke */
536 + __u8 cr; /* which phase the result extent was found at */
541 + struct page *bd_buddy_page;
543 + struct page *bd_bitmap_page;
545 + struct ext3_group_info *bd_info;
546 + struct super_block *bd_sb;
550 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
551 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
553 +#ifndef EXT3_MB_HISTORY
554 +#define ext3_mb_store_history(sb,ino,ac)
556 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
557 + struct ext3_allocation_context *ac);
560 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
562 +static struct proc_dir_entry *proc_root_ext3;
564 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
565 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
566 +int ext3_new_block_old(handle_t *, struct inode *, unsigned long, int *);
567 +int ext3_mb_reserve_blocks(struct super_block *, int);
568 +void ext3_mb_release_blocks(struct super_block *, int);
569 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
570 +void ext3_mb_free_committed_blocks(struct super_block *);
572 +#if BITS_PER_LONG == 64
573 +#define mb_correct_addr_and_bit(bit,addr) \
575 + bit += ((unsigned long) addr & 7UL) << 3; \
576 + addr = (void *) ((unsigned long) addr & ~7UL); \
578 +#elif BITS_PER_LONG == 32
579 +#define mb_correct_addr_and_bit(bit,addr) \
581 + bit += ((unsigned long) addr & 3UL) << 3; \
582 + addr = (void *) ((unsigned long) addr & ~3UL); \
585 +#error "how many bits you are?!"
588 +static inline int mb_test_bit(int bit, void *addr)
590 + mb_correct_addr_and_bit(bit,addr);
591 + return ext2_test_bit(bit, addr);
594 +static inline void mb_set_bit(int bit, void *addr)
596 + mb_correct_addr_and_bit(bit,addr);
597 + ext2_set_bit(bit, addr);
600 +static inline void mb_set_bit_atomic(int bit, void *addr)
602 + mb_correct_addr_and_bit(bit,addr);
603 + ext2_set_bit_atomic(NULL, bit, addr);
606 +static inline void mb_clear_bit(int bit, void *addr)
608 + mb_correct_addr_and_bit(bit,addr);
609 + ext2_clear_bit(bit, addr);
612 +static inline void mb_clear_bit_atomic(int bit, void *addr)
614 + mb_correct_addr_and_bit(bit,addr);
615 + ext2_clear_bit_atomic(NULL, bit, addr);
618 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
621 +#if BITS_PER_LONG == 64
622 + fix = ((unsigned long) addr & 7UL) << 3;
623 + addr = (void *) ((unsigned long) addr & ~7UL);
624 +#elif BITS_PER_LONG == 32
625 + fix = ((unsigned long) addr & 3UL) << 3;
626 + addr = (void *) ((unsigned long) addr & ~3UL);
628 +#error "how many bits you are?!"
632 + return ext2_find_next_zero_bit(addr, max, start) - fix;
635 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
639 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
640 + J_ASSERT(max != NULL);
642 + if (order > e3b->bd_blkbits + 1) {
647 + /* at order 0 we see each particular block */
648 + *max = 1 << (e3b->bd_blkbits + 3);
650 + return EXT3_MB_BITMAP(e3b);
652 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
653 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
658 +#ifdef AGGRESSIVE_CHECK
660 +static void mb_check_buddy(struct ext3_buddy *e3b)
662 + int order = e3b->bd_blkbits + 1;
663 + int max, max2, i, j, k, count;
664 + int fragments = 0, fstart;
665 + void *buddy, *buddy2;
667 + if (!test_opt(e3b->bd_sb, MBALLOC))
671 + static int mb_check_counter = 0;
672 + if (mb_check_counter++ % 300 != 0)
676 + while (order > 1) {
677 + buddy = mb_find_buddy(e3b, order, &max);
679 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
681 + J_ASSERT(buddy != buddy2);
682 + J_ASSERT(max * 2 == max2);
685 + for (i = 0; i < max; i++) {
687 + if (mb_test_bit(i, buddy)) {
688 + /* only single bit in buddy2 may be 1 */
689 + if (!mb_test_bit(i << 1, buddy2))
690 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
691 + else if (!mb_test_bit((i << 1) + 1, buddy2))
692 + J_ASSERT(mb_test_bit(i << 1, buddy2));
696 + /* both bits in buddy2 must be 0 */
697 + J_ASSERT(mb_test_bit(i << 1, buddy2));
698 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
700 + for (j = 0; j < (1 << order); j++) {
701 + k = (i * (1 << order)) + j;
702 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
706 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
711 + buddy = mb_find_buddy(e3b, 0, &max);
712 + for (i = 0; i < max; i++) {
713 + if (!mb_test_bit(i, buddy)) {
714 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
715 + if (fstart == -1) {
722 + /* check used bits only */
723 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
724 + buddy2 = mb_find_buddy(e3b, j, &max2);
726 + J_ASSERT(k < max2);
727 + J_ASSERT(mb_test_bit(k, buddy2));
730 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
731 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
735 +#define mb_check_buddy(e3b)
738 +/* find most significant bit */
739 +static int inline fmsb(unsigned short word)
753 + } while (word != 0);
759 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
760 + int len, struct ext3_group_info *grp)
762 + struct ext3_sb_info *sbi = EXT3_SB(sb);
763 + unsigned short min, max, chunk, border;
765 + mb_debug("mark %u/%u free\n", first, len);
766 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
768 + border = 2 << sb->s_blocksize_bits;
771 + /* find how many blocks can be covered since this position */
772 + max = ffs(first | border) - 1;
774 + /* find how many blocks of power 2 we need to mark */
777 + mb_debug(" %u/%u -> max %u, min %u\n",
778 + first & ((2 << sb->s_blocksize_bits) - 1),
785 + /* mark multiblock chunks only */
786 + grp->bb_counters[min]++;
788 + mb_debug(" set %u at %u \n", first >> min,
789 + sbi->s_mb_offsets[min]);
790 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
799 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
802 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
803 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
804 + unsigned short i = 0, first, len;
805 + unsigned free = 0, fragments = 0;
806 + unsigned long long period = get_cycles();
808 + i = mb_find_next_zero_bit(bitmap, max, 0);
809 + grp->bb_first_free = i;
813 + i = ext2_find_next_le_bit(bitmap, max, i);
817 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
819 + grp->bb_counters[0]++;
821 + i = mb_find_next_zero_bit(bitmap, max, i);
823 + grp->bb_fragments = fragments;
825 + /* bb_state shouldn't being modified because all
826 + * others waits for init completion on page lock */
827 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
828 + if (free != grp->bb_free) {
829 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
830 + group, free, grp->bb_free);
831 + grp->bb_free = free;
834 + period = get_cycles() - period;
835 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
836 + EXT3_SB(sb)->s_mb_buddies_generated++;
837 + EXT3_SB(sb)->s_mb_generation_time += period;
838 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
841 +static int ext3_mb_init_cache(struct page *page)
843 + int blocksize, blocks_per_page, groups_per_page;
844 + int err = 0, i, first_group, first_block;
845 + struct super_block *sb;
846 + struct buffer_head *bhs;
847 + struct buffer_head **bh;
848 + struct inode *inode;
849 + char *data, *bitmap;
851 + mb_debug("init page %lu\n", page->index);
853 + inode = page->mapping->host;
855 + blocksize = 1 << inode->i_blkbits;
856 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
858 + groups_per_page = blocks_per_page >> 1;
859 + if (groups_per_page == 0)
860 + groups_per_page = 1;
862 + /* allocate buffer_heads to read bitmaps */
863 + if (groups_per_page > 1) {
865 + i = sizeof(struct buffer_head *) * groups_per_page;
866 + bh = kmalloc(i, GFP_NOFS);
873 + first_group = page->index * blocks_per_page / 2;
875 + /* read all groups the page covers into the cache */
876 + for (i = 0; i < groups_per_page; i++) {
877 + struct ext3_group_desc * desc;
879 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
883 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
888 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
892 + if (buffer_uptodate(bh[i]))
895 + lock_buffer(bh[i]);
896 + if (buffer_uptodate(bh[i])) {
897 + unlock_buffer(bh[i]);
902 + bh[i]->b_end_io = end_buffer_read_sync;
903 + submit_bh(READ, bh[i]);
904 + mb_debug("read bitmap for group %u\n", first_group + i);
907 + /* wait for I/O completion */
908 + for (i = 0; i < groups_per_page && bh[i]; i++)
909 + wait_on_buffer(bh[i]);
912 + for (i = 0; i < groups_per_page && bh[i]; i++)
913 + if (!buffer_uptodate(bh[i]))
916 + first_block = page->index * blocks_per_page;
917 + for (i = 0; i < blocks_per_page; i++) {
920 + group = (first_block + i) >> 1;
921 + if (group >= EXT3_SB(sb)->s_groups_count)
924 + data = page_address(page) + (i * blocksize);
925 + bitmap = bh[group - first_group]->b_data;
927 + if ((first_block + i) & 1) {
928 + /* this is block of buddy */
929 + mb_debug("put buddy for group %u in page %lu/%x\n",
930 + group, page->index, i * blocksize);
931 + memset(data, 0xff, blocksize);
932 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
933 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
934 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
935 + ext3_mb_generate_buddy(sb, data, bitmap, group);
937 + /* this is block of bitmap */
938 + mb_debug("put bitmap for group %u in page %lu/%x\n",
939 + group, page->index, i * blocksize);
940 + memcpy(data, bitmap, blocksize);
943 + SetPageUptodate(page);
947 + for (i = 0; i < groups_per_page && bh[i]; i++)
955 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
956 + struct ext3_buddy *e3b)
958 + struct ext3_sb_info *sbi = EXT3_SB(sb);
959 + struct inode *inode = sbi->s_buddy_cache;
960 + int blocks_per_page, block, pnum, poff;
963 + mb_debug("load group %u\n", group);
965 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
967 + e3b->bd_blkbits = sb->s_blocksize_bits;
968 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
970 + e3b->bd_group = group;
971 + e3b->bd_buddy_page = NULL;
972 + e3b->bd_bitmap_page = NULL;
975 + pnum = block / blocks_per_page;
976 + poff = block % blocks_per_page;
978 + /* we could use find_or_create_page(), but it locks page
979 + * what we'd like to avoid in fast path ... */
980 + page = find_get_page(inode->i_mapping, pnum);
981 + if (page == NULL || !PageUptodate(page)) {
983 + page_cache_release(page);
984 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
986 + BUG_ON(page->mapping != inode->i_mapping);
987 + if (!PageUptodate(page))
988 + ext3_mb_init_cache(page);
992 + if (page == NULL || !PageUptodate(page))
994 + e3b->bd_bitmap_page = page;
995 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
996 + mark_page_accessed(page);
999 + pnum = block / blocks_per_page;
1000 + poff = block % blocks_per_page;
1002 + page = find_get_page(inode->i_mapping, pnum);
1003 + if (page == NULL || !PageUptodate(page)) {
1005 + page_cache_release(page);
1006 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1008 + BUG_ON(page->mapping != inode->i_mapping);
1009 + if (!PageUptodate(page))
1010 + ext3_mb_init_cache(page);
1011 + unlock_page(page);
1014 + if (page == NULL || !PageUptodate(page))
1016 + e3b->bd_buddy_page = page;
1017 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1018 + mark_page_accessed(page);
1020 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1021 + J_ASSERT(e3b->bd_buddy_page != NULL);
1026 + if (e3b->bd_bitmap_page)
1027 + page_cache_release(e3b->bd_bitmap_page);
1028 + if (e3b->bd_buddy_page)
1029 + page_cache_release(e3b->bd_buddy_page);
1030 + e3b->bd_buddy = NULL;
1031 + e3b->bd_bitmap = NULL;
1035 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1037 + if (e3b->bd_bitmap_page)
1038 + page_cache_release(e3b->bd_bitmap_page);
1039 + if (e3b->bd_buddy_page)
1040 + page_cache_release(e3b->bd_buddy_page);
1045 +ext3_lock_group(struct super_block *sb, int group)
1047 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1048 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1052 +ext3_unlock_group(struct super_block *sb, int group)
1054 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1055 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1058 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1063 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1064 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1066 + bb = EXT3_MB_BUDDY(e3b);
1067 + while (order <= e3b->bd_blkbits + 1) {
1068 + block = block >> 1;
1069 + if (!mb_test_bit(block, bb)) {
1070 + /* this block is part of buddy of order 'order' */
1073 + bb += 1 << (e3b->bd_blkbits - order);
1079 +static inline void mb_clear_bits(void *bm, int cur, int len)
1084 + while (cur < len) {
1085 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1086 + /* fast path: clear whole word at once */
1087 + addr = bm + (cur >> 3);
1092 + mb_clear_bit_atomic(cur, bm);
1097 +static inline void mb_set_bits(void *bm, int cur, int len)
1102 + while (cur < len) {
1103 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1104 + /* fast path: clear whole word at once */
1105 + addr = bm + (cur >> 3);
1106 + *addr = 0xffffffff;
1110 + mb_set_bit_atomic(cur, bm);
1115 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1117 + int block = 0, max = 0, order;
1118 + void *buddy, *buddy2;
1120 + mb_check_buddy(e3b);
1122 + e3b->bd_info->bb_free += count;
1123 + if (first < e3b->bd_info->bb_first_free)
1124 + e3b->bd_info->bb_first_free = first;
1126 + /* let's maintain fragments counter */
1128 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1129 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1130 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1132 + e3b->bd_info->bb_fragments--;
1133 + else if (!block && !max)
1134 + e3b->bd_info->bb_fragments++;
1136 + /* let's maintain buddy itself */
1137 + while (count-- > 0) {
1141 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1142 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1143 + e3b->bd_info->bb_counters[order]++;
1145 + /* start of the buddy */
1146 + buddy = mb_find_buddy(e3b, order, &max);
1150 + if (mb_test_bit(block, buddy) ||
1151 + mb_test_bit(block + 1, buddy))
1154 + /* both the buddies are free, try to coalesce them */
1155 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1161 + /* for special purposes, we don't set
1162 + * free bits in bitmap */
1163 + mb_set_bit(block, buddy);
1164 + mb_set_bit(block + 1, buddy);
1166 + e3b->bd_info->bb_counters[order]--;
1167 + e3b->bd_info->bb_counters[order]--;
1169 + block = block >> 1;
1171 + e3b->bd_info->bb_counters[order]++;
1173 + mb_clear_bit(block, buddy2);
1177 + mb_check_buddy(e3b);
1182 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1183 + int needed, struct ext3_free_extent *ex)
1185 + int next = block, max, ord;
1188 + J_ASSERT(ex != NULL);
1190 + buddy = mb_find_buddy(e3b, order, &max);
1192 + J_ASSERT(block < max);
1193 + if (mb_test_bit(block, buddy)) {
1200 + if (likely(order == 0)) {
1201 + /* find actual order */
1202 + order = mb_find_order_for_block(e3b, block);
1203 + block = block >> order;
1206 + ex->fe_len = 1 << order;
1207 + ex->fe_start = block << order;
1208 + ex->fe_group = e3b->bd_group;
1210 + /* calc difference from given start */
1211 + next = next - ex->fe_start;
1212 + ex->fe_len -= next;
1213 + ex->fe_start += next;
1215 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1217 + if (block + 1 >= max)
1220 + next = (block + 1) * (1 << order);
1221 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1224 + ord = mb_find_order_for_block(e3b, next);
1227 + block = next >> order;
1228 + ex->fe_len += 1 << order;
1231 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1232 + return ex->fe_len;
1235 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1237 + int ord, mlen = 0, max = 0, cur;
1238 + int start = ex->fe_start;
1239 + int len = ex->fe_len;
1244 + mb_check_buddy(e3b);
1246 + e3b->bd_info->bb_free -= len;
1247 + if (e3b->bd_info->bb_first_free == start)
1248 + e3b->bd_info->bb_first_free += len;
1250 + /* let's maintain fragments counter */
1252 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1253 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1254 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1256 + e3b->bd_info->bb_fragments++;
1257 + else if (!mlen && !max)
1258 + e3b->bd_info->bb_fragments--;
1260 + /* let's maintain buddy itself */
1262 + ord = mb_find_order_for_block(e3b, start);
1264 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1265 + /* the whole chunk may be allocated at once! */
1267 + buddy = mb_find_buddy(e3b, ord, &max);
1268 + J_ASSERT((start >> ord) < max);
1269 + mb_set_bit(start >> ord, buddy);
1270 + e3b->bd_info->bb_counters[ord]--;
1273 + J_ASSERT(len >= 0);
1277 + /* store for history */
1279 + ret = len | (ord << 16);
1281 + /* we have to split large buddy */
1282 + J_ASSERT(ord > 0);
1283 + buddy = mb_find_buddy(e3b, ord, &max);
1284 + mb_set_bit(start >> ord, buddy);
1285 + e3b->bd_info->bb_counters[ord]--;
1288 + cur = (start >> ord) & ~1U;
1289 + buddy = mb_find_buddy(e3b, ord, &max);
1290 + mb_clear_bit(cur, buddy);
1291 + mb_clear_bit(cur + 1, buddy);
1292 + e3b->bd_info->bb_counters[ord]++;
1293 + e3b->bd_info->bb_counters[ord]++;
1296 + /* now drop all the bits in bitmap */
1297 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1299 + mb_check_buddy(e3b);
1305 + * Must be called under group lock!
1307 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1308 + struct ext3_buddy *e3b)
1310 + unsigned long ret;
1312 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1313 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1315 + ac->ac_status = AC_STATUS_FOUND;
1316 + ac->ac_tail = ret & 0xffff;
1317 + ac->ac_buddy = ret >> 16;
1319 + /* hold in-core structures until allocated
1320 + * blocks are marked non-free in on-disk bitmap */
1321 + ac->ac_buddy_page = e3b->bd_buddy_page;
1322 + page_cache_get(e3b->bd_buddy_page);
1323 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1324 + page_cache_get(e3b->bd_bitmap_page);
1328 + * The routine checks whether found extent is good enough. If it is,
1329 + * then the extent gets marked used and flag is set to the context
1330 + * to stop scanning. Otherwise, the extent is compared with the
1331 + * previous found extent and if new one is better, then it's stored
1332 + * in the context. Later, the best found extent will be used, if
1333 + * mballoc can't find good enough extent.
1335 + * FIXME: real allocation policy is to be designed yet!
1337 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1338 + struct ext3_free_extent *ex,
1339 + struct ext3_buddy *e3b)
1341 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1342 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1344 + J_ASSERT(ex->fe_len > 0);
1345 + J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1346 + J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1351 + * The special case - take what you catch first
1353 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1355 + ext3_mb_use_best_found(ac, e3b);
1360 + * Let's check whether the chunk is good enough
1362 + if (ex->fe_len == gex->fe_len) {
1364 + ext3_mb_use_best_found(ac, e3b);
1369 + * If this is first found extent, just store it in the context
1371 + if (bex->fe_len == 0) {
1377 + * If new found extent is better, store it in the context
1379 + if (bex->fe_len < gex->fe_len) {
1380 + /* if the request isn't satisfied, any found extent
1381 + * larger than previous best one is better */
1382 + if (ex->fe_len > bex->fe_len)
1384 + } else if (ex->fe_len > gex->fe_len) {
1385 + /* if the request is satisfied, then we try to find
1386 + * an extent that still satisfy the request, but is
1387 + * smaller than previous one */
1392 + * Let's scan at least few extents and don't pick up a first one
1394 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1395 + ac->ac_status = AC_STATUS_BREAK;
1398 + * We don't want to scan for a whole year
1400 + if (ac->ac_found > ext3_mb_max_to_scan)
1401 + ac->ac_status = AC_STATUS_BREAK;
1404 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1405 + struct ext3_buddy *e3b)
1407 + struct ext3_free_extent ex = ac->ac_b_ex;
1408 + int group = ex.fe_group, max, err;
1410 + J_ASSERT(ex.fe_len > 0);
1411 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1415 + ext3_lock_group(ac->ac_sb, group);
1416 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1420 + ext3_mb_use_best_found(ac, e3b);
1423 + ext3_unlock_group(ac->ac_sb, group);
1425 + ext3_mb_release_desc(e3b);
1430 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1431 + struct ext3_buddy *e3b)
1433 + int group = ac->ac_g_ex.fe_group, max, err;
1434 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1435 + struct ext3_super_block *es = sbi->s_es;
1436 + struct ext3_free_extent ex;
1438 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1442 + ext3_lock_group(ac->ac_sb, group);
1443 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1444 + ac->ac_g_ex.fe_len, &ex);
1446 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1447 + unsigned long start;
1448 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1449 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1450 + if (start % sbi->s_stripe == 0) {
1453 + ext3_mb_use_best_found(ac, e3b);
1455 + } else if (max >= ac->ac_g_ex.fe_len) {
1456 + J_ASSERT(ex.fe_len > 0);
1457 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1458 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1461 + ext3_mb_use_best_found(ac, e3b);
1462 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1463 + /* Sometimes, caller may want to merge even small
1464 + * number of blocks to an existing extent */
1465 + J_ASSERT(ex.fe_len > 0);
1466 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1467 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1470 + ext3_mb_use_best_found(ac, e3b);
1472 + ext3_unlock_group(ac->ac_sb, group);
1474 + ext3_mb_release_desc(e3b);
1480 + * The routine scans buddy structures (not bitmap!) from given order
1481 + * to max order and tries to find big enough chunk to satisfy the req
1483 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1484 + struct ext3_buddy *e3b)
1486 + struct super_block *sb = ac->ac_sb;
1487 + struct ext3_group_info *grp = e3b->bd_info;
1491 + J_ASSERT(ac->ac_2order > 0);
1492 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1493 + if (grp->bb_counters[i] == 0)
1496 + buddy = mb_find_buddy(e3b, i, &max);
1497 + if (buddy == NULL) {
1498 + printk(KERN_ALERT "looking for wrong order?\n");
1502 + k = mb_find_next_zero_bit(buddy, max, 0);
1503 + J_ASSERT(k < max);
1507 + ac->ac_b_ex.fe_len = 1 << i;
1508 + ac->ac_b_ex.fe_start = k << i;
1509 + ac->ac_b_ex.fe_group = e3b->bd_group;
1511 + ext3_mb_use_best_found(ac, e3b);
1512 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1514 + if (unlikely(ext3_mb_stats))
1515 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1522 + * The routine scans the group and measures all found extents.
1523 + * In order to optimize scanning, caller must pass number of
1524 + * free blocks in the group, so the routine can know upper limit.
1526 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1527 + struct ext3_buddy *e3b)
1529 + struct super_block *sb = ac->ac_sb;
1530 + void *bitmap = EXT3_MB_BITMAP(e3b);
1531 + struct ext3_free_extent ex;
1534 + free = e3b->bd_info->bb_free;
1535 + J_ASSERT(free > 0);
1537 + i = e3b->bd_info->bb_first_free;
1539 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1540 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1541 + if (i >= sb->s_blocksize * 8) {
1542 + J_ASSERT(free == 0);
1546 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1547 + J_ASSERT(ex.fe_len > 0);
1548 + J_ASSERT(free >= ex.fe_len);
1550 + ext3_mb_measure_extent(ac, &ex, e3b);
1553 + free -= ex.fe_len;
1558 + * This is a special case for storages like raid5
1559 + * we try to find stripe-aligned chunks for stripe-size requests
1561 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1562 + struct ext3_buddy *e3b)
1564 + struct super_block *sb = ac->ac_sb;
1565 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1566 + void *bitmap = EXT3_MB_BITMAP(e3b);
1567 + struct ext3_free_extent ex;
1568 + unsigned long i, max;
1570 + J_ASSERT(sbi->s_stripe != 0);
1572 + /* find first stripe-aligned block */
1573 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb)
1574 + + le32_to_cpu(sbi->s_es->s_first_data_block);
1575 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1576 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block))
1577 + % EXT3_BLOCKS_PER_GROUP(sb);
1579 + while (i < sb->s_blocksize * 8) {
1580 + if (!mb_test_bit(i, bitmap)) {
1581 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1582 + if (max >= sbi->s_stripe) {
1585 + ext3_mb_use_best_found(ac, e3b);
1589 + i += sbi->s_stripe;
1593 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1594 + int group, int cr)
1596 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1597 + unsigned free, fragments, i, bits;
1599 + J_ASSERT(cr >= 0 && cr < 4);
1600 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1602 + free = grp->bb_free;
1603 + fragments = grp->bb_fragments;
1606 + if (fragments == 0)
1611 + J_ASSERT(ac->ac_2order != 0);
1612 + bits = ac->ac_sb->s_blocksize_bits + 1;
1613 + for (i = ac->ac_2order; i <= bits; i++)
1614 + if (grp->bb_counters[i] > 0)
1618 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1622 + if (free >= ac->ac_g_ex.fe_len)
1634 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1635 + unsigned long goal, int *len, int flags, int *errp)
1637 + struct buffer_head *bitmap_bh = NULL;
1638 + struct ext3_allocation_context ac;
1639 + int i, group, block, cr, err = 0;
1640 + struct ext3_group_desc *gdp;
1641 + struct ext3_super_block *es;
1642 + struct buffer_head *gdp_bh;
1643 + struct ext3_sb_info *sbi;
1644 + struct super_block *sb;
1645 + struct ext3_buddy e3b;
1647 + J_ASSERT(len != NULL);
1648 + J_ASSERT(*len > 0);
1652 + printk("ext3_mb_new_nblocks: nonexistent device");
1656 + if (!test_opt(sb, MBALLOC)) {
1657 + static int ext3_mballoc_warning = 0;
1658 + if (ext3_mballoc_warning == 0) {
1659 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1660 + "mballoc disabled!\n");
1661 + ext3_mballoc_warning++;
1664 + err = ext3_new_block_old(handle, inode, goal, errp);
1668 + ext3_mb_poll_new_transaction(sb, handle);
1670 + sbi = EXT3_SB(sb);
1671 + es = EXT3_SB(sb)->s_es;
1674 + * We can't allocate > group size
1676 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1677 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1679 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1680 + /* someone asks for non-reserved blocks */
1682 + err = ext3_mb_reserve_blocks(sb, 1);
1689 + ac.ac_buddy_page = NULL;
1690 + ac.ac_bitmap_page = NULL;
1693 + * Check quota for allocation of this blocks.
1695 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1703 + /* start searching from the goal */
1704 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1705 + goal >= le32_to_cpu(es->s_blocks_count))
1706 + goal = le32_to_cpu(es->s_first_data_block);
1707 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1708 + EXT3_BLOCKS_PER_GROUP(sb);
1709 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1710 + EXT3_BLOCKS_PER_GROUP(sb));
1712 + /* set up allocation goals */
1713 + ac.ac_b_ex.fe_group = 0;
1714 + ac.ac_b_ex.fe_start = 0;
1715 + ac.ac_b_ex.fe_len = 0;
1716 + ac.ac_status = AC_STATUS_CONTINUE;
1717 + ac.ac_groups_scanned = 0;
1718 + ac.ac_ex_scanned = 0;
1720 + ac.ac_sb = inode->i_sb;
1721 + ac.ac_g_ex.fe_group = group;
1722 + ac.ac_g_ex.fe_start = block;
1723 + ac.ac_g_ex.fe_len = *len;
1724 + ac.ac_flags = flags;
1726 + ac.ac_criteria = 0;
1728 + if (*len == 1 && sbi->s_stripe) {
1729 + /* looks like a metadata, let's use a dirty hack for raid5
1730 + * move all metadata in first groups in hope to hit cached
1731 + * sectors and thus avoid read-modify cycles in raid5 */
1732 + ac.ac_g_ex.fe_group = group = 0;
1735 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1737 + if (i >= ext3_mb_order2_reqs) {
1739 + if ((*len & (~(1 << i))) == 0)
1743 + /* first, try the goal */
1744 + err = ext3_mb_find_by_goal(&ac, &e3b);
1747 + if (ac.ac_status == AC_STATUS_FOUND)
1750 + /* Let's just scan groups to find more-less suitable blocks */
1751 + cr = ac.ac_2order ? 0 : 1;
1753 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1754 + ac.ac_criteria = cr;
1755 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1756 + if (group == EXT3_SB(sb)->s_groups_count)
1759 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1760 + /* we need full data about the group
1761 + * to make a good selection */
1762 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1765 + ext3_mb_release_desc(&e3b);
1768 + /* check is group good for our criteries */
1769 + if (!ext3_mb_good_group(&ac, group, cr))
1772 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1776 + ext3_lock_group(sb, group);
1777 + if (!ext3_mb_good_group(&ac, group, cr)) {
1778 + /* someone did allocation from this group */
1779 + ext3_unlock_group(sb, group);
1780 + ext3_mb_release_desc(&e3b);
1784 + ac.ac_groups_scanned++;
1786 + ext3_mb_simple_scan_group(&ac, &e3b);
1787 + else if (cr == 1 && *len == sbi->s_stripe)
1788 + ext3_mb_scan_aligned(&ac, &e3b);
1790 + ext3_mb_complex_scan_group(&ac, &e3b);
1792 + ext3_unlock_group(sb, group);
1794 + ext3_mb_release_desc(&e3b);
1796 + if (ac.ac_status != AC_STATUS_CONTINUE)
1801 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1802 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1804 + * We've been searching too long. Let's try to allocate
1805 + * the best chunk we've found so far
1808 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1809 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1810 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1811 + ac.ac_g_ex.fe_len);*/
1812 + ext3_mb_try_best_found(&ac, &e3b);
1813 + if (ac.ac_status != AC_STATUS_FOUND) {
1815 + * Someone more lucky has already allocated it.
1816 + * The only thing we can do is just take first
1818 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1820 + ac.ac_b_ex.fe_group = 0;
1821 + ac.ac_b_ex.fe_start = 0;
1822 + ac.ac_b_ex.fe_len = 0;
1823 + ac.ac_status = AC_STATUS_CONTINUE;
1824 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1830 + if (ac.ac_status != AC_STATUS_FOUND) {
1832 + * We aren't lucky definitely
1834 + DQUOT_FREE_BLOCK(inode, *len);
1838 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1839 + ac.ac_status, ac.ac_flags);
1840 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1841 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1842 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1843 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1844 + sbi->s_blocks_reserved, ac.ac_found);
1845 + printk("EXT3-fs: groups: ");
1846 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1847 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1854 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1856 + /* good news - free block(s) have been found. now it's time
1857 + * to mark block(s) in good old journaled bitmap */
1858 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1859 + + ac.ac_b_ex.fe_start
1860 + + le32_to_cpu(es->s_first_data_block);
1862 + /* we made a desicion, now mark found blocks in good old
1863 + * bitmap to be journaled */
1865 + ext3_debug("using block group %d(%d)\n",
1866 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1868 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1874 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1880 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1886 + err = ext3_journal_get_write_access(handle, gdp_bh);
1890 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1891 + + ac.ac_b_ex.fe_start
1892 + + le32_to_cpu(es->s_first_data_block);
1894 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1895 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1896 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1897 + EXT3_SB(sb)->s_itb_per_group))
1898 + ext3_error(sb, "ext3_new_block",
1899 + "Allocating block in system zone - "
1900 + "block = %u", block);
1901 +#ifdef AGGRESSIVE_CHECK
1902 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1903 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1905 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1907 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1908 + gdp->bg_free_blocks_count =
1909 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1910 + - ac.ac_b_ex.fe_len);
1911 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1912 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1914 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1917 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1923 + brelse(bitmap_bh);
1925 + /* drop non-allocated, but dquote'd blocks */
1926 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1927 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1929 + *len = ac.ac_b_ex.fe_len;
1930 + J_ASSERT(*len > 0);
1931 + J_ASSERT(block != 0);
1935 + /* if we've already allocated something, roll it back */
1936 + if (ac.ac_status == AC_STATUS_FOUND) {
1937 + /* FIXME: free blocks here */
1940 + DQUOT_FREE_BLOCK(inode, *len);
1941 + brelse(bitmap_bh);
1945 + if (ac.ac_buddy_page)
1946 + page_cache_release(ac.ac_buddy_page);
1947 + if (ac.ac_bitmap_page)
1948 + page_cache_release(ac.ac_bitmap_page);
1950 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1951 + /* block wasn't reserved before and we reserved it
1952 + * at the beginning of allocation. it doesn't matter
1953 + * whether we allocated anything or we failed: time
1954 + * to release reservation. NOTE: because I expect
1955 + * any multiblock request from delayed allocation
1956 + * path only, here is single block always */
1957 + ext3_mb_release_blocks(sb, 1);
1960 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1961 + atomic_inc(&sbi->s_bal_reqs);
1962 + atomic_add(*len, &sbi->s_bal_allocated);
1963 + if (*len >= ac.ac_g_ex.fe_len)
1964 + atomic_inc(&sbi->s_bal_success);
1965 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1966 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1967 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1968 + atomic_inc(&sbi->s_bal_goals);
1969 + if (ac.ac_found > ext3_mb_max_to_scan)
1970 + atomic_inc(&sbi->s_bal_breaks);
1973 + ext3_mb_store_history(sb, inode->i_ino, &ac);
1977 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1979 +#ifdef EXT3_MB_HISTORY
1980 +struct ext3_mb_proc_session {
1981 + struct ext3_mb_history *history;
1982 + struct super_block *sb;
1987 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1988 + struct ext3_mb_history *hs,
1991 + if (hs == s->history + s->max)
1993 + if (!first && hs == s->history + s->start)
1995 + while (hs->goal.fe_len == 0) {
1997 + if (hs == s->history + s->max)
1999 + if (hs == s->history + s->start)
2005 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2007 + struct ext3_mb_proc_session *s = seq->private;
2008 + struct ext3_mb_history *hs;
2012 + return SEQ_START_TOKEN;
2013 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2016 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2020 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2022 + struct ext3_mb_proc_session *s = seq->private;
2023 + struct ext3_mb_history *hs = v;
2026 + if (v == SEQ_START_TOKEN)
2027 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2029 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2032 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2034 + struct ext3_mb_history *hs = v;
2035 + char buf[20], buf2[20];
2037 + if (v == SEQ_START_TOKEN) {
2038 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2039 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2040 + "merge", "tail", "broken");
2044 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2045 + hs->goal.fe_start, hs->goal.fe_len);
2046 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2047 + hs->result.fe_start, hs->result.fe_len);
2048 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2049 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2050 + hs->cr, hs->merged ? "M" : "", hs->tail,
2051 + hs->buddy ? 1 << hs->buddy : 0);
2055 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2059 +static struct seq_operations ext3_mb_seq_history_ops = {
2060 + .start = ext3_mb_seq_history_start,
2061 + .next = ext3_mb_seq_history_next,
2062 + .stop = ext3_mb_seq_history_stop,
2063 + .show = ext3_mb_seq_history_show,
2066 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2068 + struct super_block *sb = PDE(inode)->data;
2069 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2070 + struct ext3_mb_proc_session *s;
2073 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2076 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2077 + s->history = kmalloc(size, GFP_KERNEL);
2083 + spin_lock(&sbi->s_mb_history_lock);
2084 + memcpy(s->history, sbi->s_mb_history, size);
2085 + s->max = sbi->s_mb_history_max;
2086 + s->start = sbi->s_mb_history_cur % s->max;
2087 + spin_unlock(&sbi->s_mb_history_lock);
2089 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2091 + struct seq_file *m = (struct seq_file *)file->private_data;
2094 + kfree(s->history);
2101 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2103 + struct seq_file *seq = (struct seq_file *)file->private_data;
2104 + struct ext3_mb_proc_session *s = seq->private;
2105 + kfree(s->history);
2107 + return seq_release(inode, file);
2110 +static struct file_operations ext3_mb_seq_history_fops = {
2111 + .owner = THIS_MODULE,
2112 + .open = ext3_mb_seq_history_open,
2114 + .llseek = seq_lseek,
2115 + .release = ext3_mb_seq_history_release,
2118 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2120 + struct super_block *sb = seq->private;
2121 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2124 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2128 + return (void *) group;
2131 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2133 + struct super_block *sb = seq->private;
2134 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2138 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2141 + return (void *) group;;
2144 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2146 + struct super_block *sb = seq->private;
2147 + long group = (long) v, i;
2149 + struct ext3_group_info info;
2150 + unsigned short counters[16];
2155 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2156 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2157 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2158 + "2^11", "2^12", "2^13");
2160 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2161 + sizeof(struct ext3_group_info);
2162 + ext3_lock_group(sb, group);
2163 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2164 + ext3_unlock_group(sb, group);
2166 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2169 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2170 + sg.info.bb_fragments, sg.info.bb_first_free);
2171 + for (i = 0; i <= 13; i++)
2172 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2173 + sg.info.bb_counters[i] : 0);
2174 + seq_printf(seq, " ]\n");
2179 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2183 +static struct seq_operations ext3_mb_seq_groups_ops = {
2184 + .start = ext3_mb_seq_groups_start,
2185 + .next = ext3_mb_seq_groups_next,
2186 + .stop = ext3_mb_seq_groups_stop,
2187 + .show = ext3_mb_seq_groups_show,
2190 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2192 + struct super_block *sb = PDE(inode)->data;
2195 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2197 + struct seq_file *m = (struct seq_file *)file->private_data;
2204 +static struct file_operations ext3_mb_seq_groups_fops = {
2205 + .owner = THIS_MODULE,
2206 + .open = ext3_mb_seq_groups_open,
2208 + .llseek = seq_lseek,
2209 + .release = seq_release,
2212 +static void ext3_mb_history_release(struct super_block *sb)
2214 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2217 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2218 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2219 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2220 + remove_proc_entry(name, proc_root_ext3);
2222 + if (sbi->s_mb_history)
2223 + kfree(sbi->s_mb_history);
2226 +static void ext3_mb_history_init(struct super_block *sb)
2228 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2232 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2233 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2234 + if (sbi->s_mb_proc != NULL) {
2235 + struct proc_dir_entry *p;
2236 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2238 + p->proc_fops = &ext3_mb_seq_history_fops;
2241 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2243 + p->proc_fops = &ext3_mb_seq_groups_fops;
2248 + sbi->s_mb_history_max = 1000;
2249 + sbi->s_mb_history_cur = 0;
2250 + spin_lock_init(&sbi->s_mb_history_lock);
2251 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2252 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2253 + memset(sbi->s_mb_history, 0, i);
2254 + /* if we can't allocate history, then we simple won't use it */
2258 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2259 + struct ext3_allocation_context *ac)
2261 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2262 + struct ext3_mb_history h;
2264 + if (likely(sbi->s_mb_history == NULL))
2267 + h.pid = current->pid;
2269 + h.goal = ac->ac_g_ex;
2270 + h.result = ac->ac_b_ex;
2271 + h.found = ac->ac_found;
2272 + h.cr = ac->ac_criteria;
2273 + h.groups = ac->ac_groups_scanned;
2274 + h.tail = ac->ac_tail;
2275 + h.buddy = ac->ac_buddy;
2277 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2278 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2281 + spin_lock(&sbi->s_mb_history_lock);
2282 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2283 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2284 + sbi->s_mb_history_cur = 0;
2285 + spin_unlock(&sbi->s_mb_history_lock);
2289 +#define ext3_mb_history_release(sb)
2290 +#define ext3_mb_history_init(sb)
2293 +int ext3_mb_init_backend(struct super_block *sb)
2295 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2296 + int i, j, len, metalen;
2297 + int num_meta_group_infos =
2298 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2299 + EXT3_DESC_PER_BLOCK_BITS(sb);
2300 + struct ext3_group_info **meta_group_info;
2302 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2303 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2304 + * So a two level scheme suffices for now. */
2305 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2306 + num_meta_group_infos, GFP_KERNEL);
2307 + if (sbi->s_group_info == NULL) {
2308 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2311 + sbi->s_buddy_cache = new_inode(sb);
2312 + if (sbi->s_buddy_cache == NULL) {
2313 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2317 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2318 + for (i = 0; i < num_meta_group_infos; i++) {
2319 + if ((i + 1) == num_meta_group_infos)
2320 + metalen = sizeof(*meta_group_info) *
2321 + (sbi->s_groups_count -
2322 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2323 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2324 + if (meta_group_info == NULL) {
2325 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2327 + goto err_freemeta;
2329 + sbi->s_group_info[i] = meta_group_info;
2333 + * calculate needed size. if change bb_counters size,
2334 + * don't forget about ext3_mb_generate_buddy()
2336 + len = sizeof(struct ext3_group_info);
2337 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2338 + for (i = 0; i < sbi->s_groups_count; i++) {
2339 + struct ext3_group_desc * desc;
2342 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2343 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2345 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2346 + if (meta_group_info[j] == NULL) {
2347 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2349 + goto err_freebuddy;
2351 + desc = ext3_get_group_desc(sb, i, NULL);
2352 + if (desc == NULL) {
2353 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2354 + goto err_freebuddy;
2356 + memset(meta_group_info[j], 0, len);
2357 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2358 + &meta_group_info[j]->bb_state);
2359 + meta_group_info[j]->bb_free =
2360 + le16_to_cpu(desc->bg_free_blocks_count);
2367 + kfree(EXT3_GROUP_INFO(sb, i));
2370 + i = num_meta_group_infos;
2373 + kfree(sbi->s_group_info[i]);
2374 + iput(sbi->s_buddy_cache);
2376 + kfree(sbi->s_group_info);
2380 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2382 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2383 + struct inode *root = sb->s_root->d_inode;
2384 + unsigned i, offset, max;
2385 + struct dentry *dentry;
2387 + if (!test_opt(sb, MBALLOC))
2390 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2392 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2393 + if (sbi->s_mb_offsets == NULL) {
2394 + clear_opt(sbi->s_mount_opt, MBALLOC);
2397 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2398 + if (sbi->s_mb_maxs == NULL) {
2399 + clear_opt(sbi->s_mount_opt, MBALLOC);
2400 + kfree(sbi->s_mb_maxs);
2404 + /* order 0 is regular bitmap */
2405 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2406 + sbi->s_mb_offsets[0] = 0;
2410 + max = sb->s_blocksize << 2;
2412 + sbi->s_mb_offsets[i] = offset;
2413 + sbi->s_mb_maxs[i] = max;
2414 + offset += 1 << (sb->s_blocksize_bits - i);
2417 + } while (i <= sb->s_blocksize_bits + 1);
2419 + /* init file for buddy data */
2420 + if ((i = ext3_mb_init_backend(sb))) {
2421 + clear_opt(sbi->s_mount_opt, MBALLOC);
2422 + kfree(sbi->s_mb_offsets);
2423 + kfree(sbi->s_mb_maxs);
2427 + spin_lock_init(&sbi->s_reserve_lock);
2428 + spin_lock_init(&sbi->s_md_lock);
2429 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2430 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2431 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2432 + spin_lock_init(&sbi->s_bal_lock);
2434 + /* remove old on-disk buddy file */
2435 + down(&root->i_sem);
2436 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2437 + if (dentry->d_inode != NULL) {
2438 + i = vfs_unlink(root, dentry);
2440 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2445 + ext3_mb_history_init(sb);
2447 + printk("EXT3-fs: mballoc enabled\n");
2451 +int ext3_mb_release(struct super_block *sb)
2453 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2454 + int i, num_meta_group_infos;
2456 + if (!test_opt(sb, MBALLOC))
2459 + /* release freed, non-committed blocks */
2460 + spin_lock(&sbi->s_md_lock);
2461 + list_splice_init(&sbi->s_closed_transaction,
2462 + &sbi->s_committed_transaction);
2463 + list_splice_init(&sbi->s_active_transaction,
2464 + &sbi->s_committed_transaction);
2465 + spin_unlock(&sbi->s_md_lock);
2466 + ext3_mb_free_committed_blocks(sb);
2468 + if (sbi->s_group_info) {
2469 + for (i = 0; i < sbi->s_groups_count; i++)
2470 + kfree(EXT3_GROUP_INFO(sb, i));
2471 + num_meta_group_infos = (sbi->s_groups_count +
2472 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2473 + EXT3_DESC_PER_BLOCK_BITS(sb);
2474 + for (i = 0; i < num_meta_group_infos; i++)
2475 + kfree(sbi->s_group_info[i]);
2476 + kfree(sbi->s_group_info);
2478 + if (sbi->s_mb_offsets)
2479 + kfree(sbi->s_mb_offsets);
2480 + if (sbi->s_mb_maxs)
2481 + kfree(sbi->s_mb_maxs);
2482 + if (sbi->s_buddy_cache)
2483 + iput(sbi->s_buddy_cache);
2484 + if (sbi->s_blocks_reserved)
2485 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2486 + sbi->s_blocks_reserved);
2487 + if (ext3_mb_stats) {
2488 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2489 + atomic_read(&sbi->s_bal_allocated),
2490 + atomic_read(&sbi->s_bal_reqs),
2491 + atomic_read(&sbi->s_bal_success));
2492 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2493 + "%u 2^N hits, %u breaks\n",
2494 + atomic_read(&sbi->s_bal_ex_scanned),
2495 + atomic_read(&sbi->s_bal_goals),
2496 + atomic_read(&sbi->s_bal_2orders),
2497 + atomic_read(&sbi->s_bal_breaks));
2498 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2499 + sbi->s_mb_buddies_generated++,
2500 + sbi->s_mb_generation_time);
2503 + ext3_mb_history_release(sb);
2508 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2510 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2511 + int err, i, count = 0, count2 = 0;
2512 + struct ext3_free_metadata *md;
2513 + struct ext3_buddy e3b;
2515 + if (list_empty(&sbi->s_committed_transaction))
2518 + /* there is committed blocks to be freed yet */
2520 + /* get next array of blocks */
2522 + spin_lock(&sbi->s_md_lock);
2523 + if (!list_empty(&sbi->s_committed_transaction)) {
2524 + md = list_entry(sbi->s_committed_transaction.next,
2525 + struct ext3_free_metadata, list);
2526 + list_del(&md->list);
2528 + spin_unlock(&sbi->s_md_lock);
2533 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2534 + md->num, md->group, md);
2536 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2537 + /* we expect to find existing buddy because it's pinned */
2540 + /* there are blocks to put in buddy to make them really free */
2543 + ext3_lock_group(sb, md->group);
2544 + for (i = 0; i < md->num; i++) {
2545 + mb_debug(" %u", md->blocks[i]);
2546 + mb_free_blocks(&e3b, md->blocks[i], 1);
2549 + ext3_unlock_group(sb, md->group);
2551 + /* balance refcounts from ext3_mb_free_metadata() */
2552 + page_cache_release(e3b.bd_buddy_page);
2553 + page_cache_release(e3b.bd_bitmap_page);
2556 + ext3_mb_release_desc(&e3b);
2559 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2562 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2564 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2566 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2569 + /* new transaction! time to close last one and free blocks for
2570 + * committed transaction. we know that only transaction can be
2571 + * active, so previos transaction can be being logged and we
2572 + * know that transaction before previous is known to be already
2573 + * logged. this means that now we may free blocks freed in all
2574 + * transactions before previous one. hope I'm clear enough ... */
2576 + spin_lock(&sbi->s_md_lock);
2577 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2578 + mb_debug("new transaction %lu, old %lu\n",
2579 + (unsigned long) handle->h_transaction->t_tid,
2580 + (unsigned long) sbi->s_last_transaction);
2581 + list_splice_init(&sbi->s_closed_transaction,
2582 + &sbi->s_committed_transaction);
2583 + list_splice_init(&sbi->s_active_transaction,
2584 + &sbi->s_closed_transaction);
2585 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2587 + spin_unlock(&sbi->s_md_lock);
2589 + ext3_mb_free_committed_blocks(sb);
2592 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2593 + int group, int block, int count)
2595 + struct ext3_group_info *db = e3b->bd_info;
2596 + struct super_block *sb = e3b->bd_sb;
2597 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2598 + struct ext3_free_metadata *md;
2601 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2602 + J_ASSERT(e3b->bd_buddy_page != NULL);
2604 + ext3_lock_group(sb, group);
2605 + for (i = 0; i < count; i++) {
2606 + md = db->bb_md_cur;
2607 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2608 + db->bb_md_cur = NULL;
2613 + ext3_unlock_group(sb, group);
2614 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2618 + md->group = group;
2620 + ext3_lock_group(sb, group);
2621 + if (db->bb_md_cur == NULL) {
2622 + spin_lock(&sbi->s_md_lock);
2623 + list_add(&md->list, &sbi->s_active_transaction);
2624 + spin_unlock(&sbi->s_md_lock);
2625 + /* protect buddy cache from being freed,
2626 + * otherwise we'll refresh it from
2627 + * on-disk bitmap and lose not-yet-available
2629 + page_cache_get(e3b->bd_buddy_page);
2630 + page_cache_get(e3b->bd_bitmap_page);
2631 + db->bb_md_cur = md;
2632 + db->bb_tid = handle->h_transaction->t_tid;
2633 + mb_debug("new md 0x%p for group %u\n",
2637 + md = db->bb_md_cur;
2641 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2642 + md->blocks[md->num] = block + i;
2644 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2645 + /* no more space, put full container on a sb's list */
2646 + db->bb_md_cur = NULL;
2649 + ext3_unlock_group(sb, group);
2653 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2654 + unsigned long block, unsigned long count,
2655 + int metadata, int *freed)
2657 + struct buffer_head *bitmap_bh = NULL;
2658 + struct ext3_group_desc *gdp;
2659 + struct ext3_super_block *es;
2660 + unsigned long bit, overflow;
2661 + struct buffer_head *gd_bh;
2662 + unsigned long block_group;
2663 + struct ext3_sb_info *sbi;
2664 + struct super_block *sb;
2665 + struct ext3_buddy e3b;
2671 + printk ("ext3_free_blocks: nonexistent device");
2675 + ext3_mb_poll_new_transaction(sb, handle);
2677 + sbi = EXT3_SB(sb);
2678 + es = EXT3_SB(sb)->s_es;
2679 + if (block < le32_to_cpu(es->s_first_data_block) ||
2680 + block + count < block ||
2681 + block + count > le32_to_cpu(es->s_blocks_count)) {
2682 + ext3_error (sb, "ext3_free_blocks",
2683 + "Freeing blocks not in datazone - "
2684 + "block = %lu, count = %lu", block, count);
2685 + goto error_return;
2688 + ext3_debug("freeing block %lu\n", block);
2692 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2693 + EXT3_BLOCKS_PER_GROUP(sb);
2694 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2695 + EXT3_BLOCKS_PER_GROUP(sb);
2697 + * Check to see if we are freeing blocks across a group
2700 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2701 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2702 + count -= overflow;
2704 + brelse(bitmap_bh);
2705 + bitmap_bh = read_block_bitmap(sb, block_group);
2707 + goto error_return;
2708 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2710 + goto error_return;
2712 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2713 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2714 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2715 + EXT3_SB(sb)->s_itb_per_group) ||
2716 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2717 + EXT3_SB(sb)->s_itb_per_group))
2718 + ext3_error (sb, "ext3_free_blocks",
2719 + "Freeing blocks in system zones - "
2720 + "Block = %lu, count = %lu",
2723 + BUFFER_TRACE(bitmap_bh, "getting write access");
2724 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2726 + goto error_return;
2729 + * We are about to modify some metadata. Call the journal APIs
2730 + * to unshare ->b_data if a currently-committing transaction is
2733 + BUFFER_TRACE(gd_bh, "get_write_access");
2734 + err = ext3_journal_get_write_access(handle, gd_bh);
2736 + goto error_return;
2738 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2740 + goto error_return;
2742 +#ifdef AGGRESSIVE_CHECK
2745 + for (i = 0; i < count; i++)
2746 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2749 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2751 + /* We dirtied the bitmap block */
2752 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2753 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2756 + /* blocks being freed are metadata. these blocks shouldn't
2757 + * be used until this transaction is committed */
2758 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2760 + ext3_lock_group(sb, block_group);
2761 + mb_free_blocks(&e3b, bit, count);
2762 + ext3_unlock_group(sb, block_group);
2765 + spin_lock(sb_bgl_lock(sbi, block_group));
2766 + gdp->bg_free_blocks_count =
2767 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2768 + spin_unlock(sb_bgl_lock(sbi, block_group));
2769 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2771 + ext3_mb_release_desc(&e3b);
2775 + /* And the group descriptor block */
2776 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2777 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2778 + if (!err) err = ret;
2780 + if (overflow && !err) {
2787 + brelse(bitmap_bh);
2788 + ext3_std_error(sb, err);
2792 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2794 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2795 + int free, ret = -ENOSPC;
2797 + BUG_ON(blocks < 0);
2798 + spin_lock(&sbi->s_reserve_lock);
2799 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2800 + if (blocks <= free - sbi->s_blocks_reserved) {
2801 + sbi->s_blocks_reserved += blocks;
2804 + spin_unlock(&sbi->s_reserve_lock);
2808 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2810 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2812 + BUG_ON(blocks < 0);
2813 + spin_lock(&sbi->s_reserve_lock);
2814 + sbi->s_blocks_reserved -= blocks;
2815 + WARN_ON(sbi->s_blocks_reserved < 0);
2816 + if (sbi->s_blocks_reserved < 0)
2817 + sbi->s_blocks_reserved = 0;
2818 + spin_unlock(&sbi->s_reserve_lock);
2821 +int ext3_new_block(handle_t *handle, struct inode *inode,
2822 + unsigned long goal, int *errp)
2826 + if (!test_opt(inode->i_sb, MBALLOC)) {
2827 + ret = ext3_new_block_old(handle, inode, goal, errp);
2831 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2837 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2838 + unsigned long block, unsigned long count, int metadata)
2840 + struct super_block *sb;
2844 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2845 + ext3_free_blocks_old(handle, inode, block, count);
2847 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2849 + DQUOT_FREE_BLOCK(inode, freed);
2854 +#define EXT3_ROOT "ext3"
2855 +#define EXT3_MB_STATS_NAME "mb_stats"
2856 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2857 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2858 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2860 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2861 + int count, int *eof, void *data)
2869 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2874 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2875 + unsigned long count, void *data)
2879 + if (count >= sizeof(str)) {
2880 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2881 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2882 + return -EOVERFLOW;
2885 + if (copy_from_user(str, buffer, count))
2888 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2889 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2893 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2894 + int count, int *eof, void *data)
2902 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2907 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2908 + unsigned long count, void *data)
2913 + if (count >= sizeof(str)) {
2914 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2915 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2916 + return -EOVERFLOW;
2919 + if (copy_from_user(str, buffer, count))
2922 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2923 + value = simple_strtol(str, NULL, 0);
2927 + ext3_mb_max_to_scan = value;
2932 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2933 + int count, int *eof, void *data)
2941 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2946 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2947 + unsigned long count, void *data)
2952 + if (count >= sizeof(str)) {
2953 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2954 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2955 + return -EOVERFLOW;
2958 + if (copy_from_user(str, buffer, count))
2961 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2962 + value = simple_strtol(str, NULL, 0);
2966 + ext3_mb_min_to_scan = value;
2971 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2972 + int count, int *eof, void *data)
2980 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
2985 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2986 + unsigned long count, void *data)
2991 + if (count >= sizeof(str)) {
2992 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2993 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2994 + return -EOVERFLOW;
2997 + if (copy_from_user(str, buffer, count))
3000 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
3001 + value = simple_strtol(str, NULL, 0);
3005 + ext3_mb_order2_reqs = value;
3010 +int __init init_ext3_proc(void)
3012 + struct proc_dir_entry *proc_ext3_mb_stats;
3013 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3014 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3015 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3017 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3018 + if (proc_root_ext3 == NULL) {
3019 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3023 + /* Initialize EXT3_MB_STATS_NAME */
3024 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3025 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3026 + if (proc_ext3_mb_stats == NULL) {
3027 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3028 + EXT3_MB_STATS_NAME);
3029 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3033 + proc_ext3_mb_stats->data = NULL;
3034 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3035 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3037 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3038 + proc_ext3_mb_max_to_scan = create_proc_entry(
3039 + EXT3_MB_MAX_TO_SCAN_NAME,
3040 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3041 + if (proc_ext3_mb_max_to_scan == NULL) {
3042 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3043 + EXT3_MB_MAX_TO_SCAN_NAME);
3044 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3045 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3049 + proc_ext3_mb_max_to_scan->data = NULL;
3050 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3051 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3053 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3054 + proc_ext3_mb_min_to_scan = create_proc_entry(
3055 + EXT3_MB_MIN_TO_SCAN_NAME,
3056 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3057 + if (proc_ext3_mb_min_to_scan == NULL) {
3058 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3059 + EXT3_MB_MIN_TO_SCAN_NAME);
3060 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3061 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3062 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3066 + proc_ext3_mb_min_to_scan->data = NULL;
3067 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3068 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3070 + /* Initialize EXT3_ORDER2_REQ */
3071 + proc_ext3_mb_order2_req = create_proc_entry(
3072 + EXT3_MB_ORDER2_REQ,
3073 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3074 + if (proc_ext3_mb_order2_req == NULL) {
3075 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3076 + EXT3_MB_ORDER2_REQ);
3077 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3078 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3079 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3080 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3084 + proc_ext3_mb_order2_req->data = NULL;
3085 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3086 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3091 +void exit_ext3_proc(void)
3093 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3094 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3095 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3096 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3097 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3099 Index: linux-2.6.5-7.282-full/fs/ext3/Makefile
3100 ===================================================================
3101 --- linux-2.6.5-7.282-full.orig/fs/ext3/Makefile 2006-10-24 22:18:28.000000000 +0400
3102 +++ linux-2.6.5-7.282-full/fs/ext3/Makefile 2006-10-24 22:18:28.000000000 +0400
3103 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
3105 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3106 ioctl.o namei.o super.o symlink.o hash.o \
3108 + extents.o mballoc.o
3110 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3111 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o