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,10 @@ extern int ext3_bg_has_super(struct supe
44 extern unsigned long ext3_bg_num_gdb(struct super_block *sb, int group);
45 extern int ext3_new_block (handle_t *, struct inode *, unsigned long, int *);
46 extern void ext3_free_blocks (handle_t *, struct inode *, unsigned long,
48 + unsigned long, int);
49 +extern int ext3_new_block_old(handle_t *, struct inode *, unsigned long, int *);
50 +extern void ext3_free_blocks_old(handle_t *, struct inode *, unsigned long,
52 extern unsigned long ext3_count_free_blocks (struct super_block *);
53 extern void ext3_check_blocks_bitmap (struct super_block *);
54 extern struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
55 @@ -824,6 +843,17 @@ extern void ext3_extents_initialize_bloc
56 extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
57 unsigned int cmd, unsigned long arg);
60 +extern long ext3_mb_stats;
61 +extern long ext3_mb_max_to_scan;
62 +extern int ext3_mb_init(struct super_block *, int);
63 +extern int ext3_mb_release(struct super_block *);
64 +extern int ext3_mb_new_blocks(handle_t *, struct inode *, unsigned long, int *, int, int *);
65 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
66 +extern void ext3_mb_release_blocks(struct super_block *, int);
67 +int __init init_ext3_proc(void);
68 +void exit_ext3_proc(void);
70 #endif /* __KERNEL__ */
72 #define EXT3_IOC_CREATE_INUM _IOW('f', 5, long)
73 Index: linux-2.6.5-7.282-full/include/linux/ext3_fs_sb.h
74 ===================================================================
75 --- linux-2.6.5-7.282-full.orig/include/linux/ext3_fs_sb.h 2006-10-24 22:18:28.000000000 +0400
76 +++ linux-2.6.5-7.282-full/include/linux/ext3_fs_sb.h 2006-10-24 22:18:28.000000000 +0400
79 #include <linux/blockgroup_lock.h>
80 #include <linux/percpu_counter.h>
81 +#include <linux/list.h>
84 #include <linux/rbtree.h>
85 +#include <linux/proc_fs.h>
87 +struct ext3_buddy_group_blocks;
88 +struct ext3_mb_history;
89 +#define EXT3_BB_MAX_BLOCKS
92 * third extended-fs super-block data in memory
93 @@ -78,6 +84,43 @@ struct ext3_sb_info {
94 struct timer_list turn_ro_timer; /* For turning read-only (crash simulation) */
95 wait_queue_head_t ro_wait_queue; /* For people waiting for the fs to go read-only */
98 + /* for buddy allocator */
99 + struct ext3_group_info ***s_group_info;
100 + struct inode *s_buddy_cache;
101 + long s_blocks_reserved;
102 + spinlock_t s_reserve_lock;
103 + struct list_head s_active_transaction;
104 + struct list_head s_closed_transaction;
105 + struct list_head s_committed_transaction;
106 + spinlock_t s_md_lock;
107 + tid_t s_last_transaction;
109 + unsigned short *s_mb_offsets, *s_mb_maxs;
110 + unsigned long s_stripe;
112 + /* history to debug policy */
113 + struct ext3_mb_history *s_mb_history;
114 + int s_mb_history_cur;
115 + int s_mb_history_max;
116 + struct proc_dir_entry *s_mb_proc;
117 + spinlock_t s_mb_history_lock;
119 + /* stats for buddy allocator */
120 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
121 + atomic_t s_bal_success; /* we found long enough chunks */
122 + atomic_t s_bal_allocated; /* in blocks */
123 + atomic_t s_bal_ex_scanned; /* total extents scanned */
124 + atomic_t s_bal_goals; /* goal hits */
125 + atomic_t s_bal_breaks; /* too long searches */
126 + atomic_t s_bal_2orders; /* 2^order hits */
127 + spinlock_t s_bal_lock;
128 + unsigned long s_mb_buddies_generated;
129 + unsigned long long s_mb_generation_time;
132 +#define EXT3_GROUP_INFO(sb, group) \
133 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
134 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
136 #endif /* _LINUX_EXT3_FS_SB */
137 Index: linux-2.6.5-7.282-full/fs/ext3/super.c
138 ===================================================================
139 --- linux-2.6.5-7.282-full.orig/fs/ext3/super.c 2006-10-24 22:18:28.000000000 +0400
140 +++ linux-2.6.5-7.282-full/fs/ext3/super.c 2006-10-24 22:18:28.000000000 +0400
141 @@ -389,6 +389,7 @@ void ext3_put_super (struct super_block
142 struct ext3_super_block *es = sbi->s_es;
145 + ext3_mb_release(sb);
146 ext3_ext_release(sb);
147 ext3_xattr_put_super(sb);
148 journal_destroy(sbi->s_journal);
149 @@ -588,6 +589,7 @@ enum {
151 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
152 Opt_extents, Opt_noextents, Opt_extdebug,
153 + Opt_mballoc, Opt_nomballoc, Opt_stripe,
156 static match_table_t tokens = {
157 @@ -634,6 +636,9 @@ static match_table_t tokens = {
158 {Opt_extents, "extents"},
159 {Opt_noextents, "noextents"},
160 {Opt_extdebug, "extdebug"},
161 + {Opt_mballoc, "mballoc"},
162 + {Opt_nomballoc, "nomballoc"},
163 + {Opt_stripe, "stripe=%u"},
164 {Opt_barrier, "barrier=%u"},
167 @@ -859,6 +864,19 @@ static int parse_options (char * options
169 set_opt (sbi->s_mount_opt, EXTDEBUG);
172 + set_opt(sbi->s_mount_opt, MBALLOC);
174 + case Opt_nomballoc:
175 + clear_opt(sbi->s_mount_opt, MBALLOC);
178 + if (match_int(&args[0], &option))
182 + sbi->s_stripe = option;
186 "EXT3-fs: Unrecognized mount option \"%s\" "
187 @@ -1512,6 +1530,7 @@ static int ext3_fill_super (struct super
188 ext3_count_dirs(sb));
191 + ext3_mb_init(sb, needs_recovery);
195 @@ -2160,7 +2179,13 @@ static struct file_system_type ext3_fs_t
197 static int __init init_ext3_fs(void)
199 - int err = init_ext3_xattr();
202 + err = init_ext3_proc();
206 + err = init_ext3_xattr();
209 err = init_inodecache();
210 @@ -2189,6 +2214,7 @@ static void __exit exit_ext3_fs(void)
211 unregister_filesystem(&ext3_fs_type);
212 destroy_inodecache();
217 int ext3_prep_san_write(struct inode *inode, long *blocks,
218 Index: linux-2.6.5-7.282-full/fs/ext3/extents.c
219 ===================================================================
220 --- linux-2.6.5-7.282-full.orig/fs/ext3/extents.c 2006-10-24 22:18:28.000000000 +0400
221 +++ linux-2.6.5-7.282-full/fs/ext3/extents.c 2006-10-24 22:18:28.000000000 +0400
222 @@ -779,7 +779,7 @@ cleanup:
223 for (i = 0; i < depth; i++) {
226 - ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
227 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
231 @@ -1438,7 +1438,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
232 path->p_idx->ei_leaf);
233 bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
234 ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
235 - ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
236 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
240 @@ -1923,10 +1923,12 @@ ext3_remove_blocks(struct ext3_extents_t
241 int needed = ext3_remove_blocks_credits(tree, ex, from, to);
242 handle_t *handle = ext3_journal_start(tree->inode, needed);
243 struct buffer_head *bh;
245 + int i, metadata = 0;
248 return PTR_ERR(handle);
249 + if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
251 if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
253 unsigned long num, start;
254 @@ -1938,7 +1940,7 @@ ext3_remove_blocks(struct ext3_extents_t
255 bh = sb_find_get_block(tree->inode->i_sb, start + i);
256 ext3_forget(handle, 0, tree->inode, bh, start + i);
258 - ext3_free_blocks(handle, tree->inode, start, num);
259 + ext3_free_blocks(handle, tree->inode, start, num, metadata);
260 } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
261 printk("strange request: removal %lu-%lu from %u:%u\n",
262 from, to, ex->ee_block, ex->ee_len);
263 Index: linux-2.6.5-7.282-full/fs/ext3/inode.c
264 ===================================================================
265 --- linux-2.6.5-7.282-full.orig/fs/ext3/inode.c 2006-10-24 22:18:28.000000000 +0400
266 +++ linux-2.6.5-7.282-full/fs/ext3/inode.c 2006-10-24 22:18:28.000000000 +0400
267 @@ -574,7 +574,7 @@ static int ext3_alloc_branch(handle_t *h
268 ext3_journal_forget(handle, branch[i].bh);
270 for (i = 0; i < keys; i++)
271 - ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1);
272 + ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1, 1);
276 @@ -675,7 +675,7 @@ err_out:
278 for (i = 0; i < num; i++)
279 ext3_free_blocks(handle, inode,
280 - le32_to_cpu(where[i].key), 1);
281 + le32_to_cpu(where[i].key), 1, 1);
285 @@ -1837,7 +1837,7 @@ ext3_clear_blocks(handle_t *handle, stru
289 - ext3_free_blocks(handle, inode, block_to_free, count);
290 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
294 @@ -2008,7 +2008,7 @@ static void ext3_free_branches(handle_t
295 ext3_journal_test_restart(handle, inode);
298 - ext3_free_blocks(handle, inode, nr, 1);
299 + ext3_free_blocks(handle, inode, nr, 1, 1);
303 Index: linux-2.6.5-7.282-full/fs/ext3/balloc.c
304 ===================================================================
305 --- linux-2.6.5-7.282-full.orig/fs/ext3/balloc.c 2006-08-30 18:12:13.000000000 +0400
306 +++ linux-2.6.5-7.282-full/fs/ext3/balloc.c 2006-10-24 22:18:28.000000000 +0400
307 @@ -78,7 +78,7 @@ struct ext3_group_desc * ext3_get_group_
309 * Return buffer_head on success or NULL in case of failure.
311 -static struct buffer_head *
312 +struct buffer_head *
313 read_block_bitmap(struct super_block *sb, unsigned int block_group)
315 struct ext3_group_desc * desc;
316 @@ -274,7 +274,7 @@ void ext3_discard_reservation(struct ino
319 /* Free given blocks, update quota and i_blocks field */
320 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
321 +void ext3_free_blocks_old(handle_t *handle, struct inode *inode,
322 unsigned long block, unsigned long count)
324 struct buffer_head *bitmap_bh = NULL;
325 @@ -1142,7 +1142,7 @@ int ext3_should_retry_alloc(struct super
326 * bitmap, and then for any free bit if that fails.
327 * This function also updates quota and i_blocks field.
329 -int ext3_new_block(handle_t *handle, struct inode *inode,
330 +int ext3_new_block_old(handle_t *handle, struct inode *inode,
331 unsigned long goal, int *errp)
333 struct buffer_head *bitmap_bh = NULL;
334 Index: linux-2.6.5-7.282-full/fs/ext3/xattr.c
335 ===================================================================
336 --- linux-2.6.5-7.282-full.orig/fs/ext3/xattr.c 2006-10-24 22:18:28.000000000 +0400
337 +++ linux-2.6.5-7.282-full/fs/ext3/xattr.c 2006-10-24 22:18:28.000000000 +0400
338 @@ -1371,7 +1371,7 @@ ext3_xattr_set_handle2(handle_t *handle,
339 new_bh = sb_getblk(sb, block);
342 - ext3_free_blocks(handle, inode, block, 1);
343 + ext3_free_blocks(handle, inode, block, 1, 1);
347 @@ -1411,7 +1411,7 @@ getblk_failed:
348 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
349 /* Free the old block. */
350 ea_bdebug(old_bh, "freeing");
351 - ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1);
352 + ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1, 1);
354 /* ext3_forget() calls bforget() for us, but we
355 let our caller release old_bh, so we need to
356 @@ -1519,7 +1519,7 @@ ext3_xattr_delete_inode(handle_t *handle
357 mb_cache_entry_free(ce);
360 - ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1);
361 + ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1, 1);
363 ext3_forget(handle, 1, inode, bh, EXT3_I(inode)->i_file_acl);
365 Index: linux-2.6.5-7.282-full/fs/ext3/mballoc.c
366 ===================================================================
367 --- linux-2.6.5-7.282-full.orig/fs/ext3/mballoc.c 2006-10-23 18:07:54.821533176 +0400
368 +++ linux-2.6.5-7.282-full/fs/ext3/mballoc.c 2006-10-24 22:20:45.000000000 +0400
371 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
372 + * Written by Alex Tomas <alex@clusterfs.com>
374 + * This program is free software; you can redistribute it and/or modify
375 + * it under the terms of the GNU General Public License version 2 as
376 + * published by the Free Software Foundation.
378 + * This program is distributed in the hope that it will be useful,
379 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
380 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
381 + * GNU General Public License for more details.
383 + * You should have received a copy of the GNU General Public Licens
384 + * along with this program; if not, write to the Free Software
385 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
390 + * mballoc.c contains the multiblocks allocation routines
393 +#include <linux/config.h>
394 +#include <linux/time.h>
395 +#include <linux/fs.h>
396 +#include <linux/namei.h>
397 +#include <linux/jbd.h>
398 +#include <linux/ext3_fs.h>
399 +#include <linux/ext3_jbd.h>
400 +#include <linux/quotaops.h>
401 +#include <linux/buffer_head.h>
402 +#include <linux/module.h>
403 +#include <linux/swap.h>
404 +#include <linux/proc_fs.h>
405 +#include <linux/pagemap.h>
406 +#include <linux/seq_file.h>
410 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
411 + * - track min/max extents in each group for better group selection
412 + * - mb_mark_used() may allocate chunk right after splitting buddy
413 + * - special flag to advice allocator to look for requested + N blocks
414 + * this may improve interaction between extents and mballoc
415 + * - tree of groups sorted by number of free blocks
416 + * - percpu reservation code (hotpath)
421 + * with AGRESSIVE_CHECK allocator runs consistency checks over
422 + * structures. these checks slow things down a lot
424 +#define AGGRESSIVE_CHECK__
430 +#define mb_debug(fmt,a...) printk(fmt, ##a)
432 +#define mb_debug(fmt,a...)
436 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
437 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
439 +#define EXT3_MB_HISTORY
442 + * How long mballoc can look for a best extent (in found extents)
444 +long ext3_mb_max_to_scan = 500;
447 + * How long mballoc must look for a best extent
449 +long ext3_mb_min_to_scan = 30;
452 + * with 'ext3_mb_stats' allocator will collect stats that will be
453 + * shown at umount. The collecting costs though!
456 +long ext3_mb_stats = 1;
459 + * for which requests use 2^N search using buddies
461 +long ext3_mb_order2_reqs = 8;
463 +#ifdef EXT3_BB_MAX_BLOCKS
464 +#undef EXT3_BB_MAX_BLOCKS
466 +#define EXT3_BB_MAX_BLOCKS 30
468 +struct ext3_free_metadata {
469 + unsigned short group;
470 + unsigned short num;
471 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
472 + struct list_head list;
475 +struct ext3_group_info {
476 + unsigned long bb_state;
477 + unsigned long bb_tid;
478 + struct ext3_free_metadata *bb_md_cur;
479 + unsigned short bb_first_free;
480 + unsigned short bb_free;
481 + unsigned short bb_fragments;
482 + unsigned short bb_counters[];
486 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
487 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
489 +#define EXT3_MB_GRP_NEED_INIT(grp) \
490 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
492 +struct ext3_free_extent {
498 +struct ext3_allocation_context {
499 + struct super_block *ac_sb;
502 + struct ext3_free_extent ac_g_ex;
504 + /* the best found extent */
505 + struct ext3_free_extent ac_b_ex;
507 + /* number of iterations done. we have to track to limit searching */
508 + unsigned long ac_ex_scanned;
509 + __u16 ac_groups_scanned;
514 + __u8 ac_flags; /* allocation hints */
517 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
518 + * N > 0, the field stores N, otherwise 0 */
520 + struct page *ac_buddy_page;
521 + struct page *ac_bitmap_page;
524 +#define AC_STATUS_CONTINUE 1
525 +#define AC_STATUS_FOUND 2
526 +#define AC_STATUS_BREAK 3
528 +struct ext3_mb_history {
529 + struct ext3_free_extent goal; /* goal allocation */
530 + struct ext3_free_extent result; /* result allocation */
533 + __u16 found; /* how many extents have been found */
534 + __u16 groups; /* how many groups have been scanned */
535 + __u16 tail; /* what tail broke some buddy */
536 + __u16 buddy; /* buddy the tail ^^^ broke */
537 + __u8 cr; /* which phase the result extent was found at */
542 + struct page *bd_buddy_page;
544 + struct page *bd_bitmap_page;
546 + struct ext3_group_info *bd_info;
547 + struct super_block *bd_sb;
551 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
552 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
554 +#ifndef EXT3_MB_HISTORY
555 +#define ext3_mb_store_history(sb,ino,ac)
557 +static void ext3_mb_store_history(struct super_block *, unsigned ino,
558 + struct ext3_allocation_context *ac);
561 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
563 +static struct proc_dir_entry *proc_root_ext3;
565 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
566 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
567 +void ext3_mb_free_committed_blocks(struct super_block *);
569 +#if BITS_PER_LONG == 64
570 +#define mb_correct_addr_and_bit(bit,addr) \
572 + bit += ((unsigned long) addr & 7UL) << 3; \
573 + addr = (void *) ((unsigned long) addr & ~7UL); \
575 +#elif BITS_PER_LONG == 32
576 +#define mb_correct_addr_and_bit(bit,addr) \
578 + bit += ((unsigned long) addr & 3UL) << 3; \
579 + addr = (void *) ((unsigned long) addr & ~3UL); \
582 +#error "how many bits you are?!"
585 +static inline int mb_test_bit(int bit, void *addr)
587 + mb_correct_addr_and_bit(bit,addr);
588 + return ext2_test_bit(bit, addr);
591 +static inline void mb_set_bit(int bit, void *addr)
593 + mb_correct_addr_and_bit(bit,addr);
594 + ext2_set_bit(bit, addr);
597 +static inline void mb_set_bit_atomic(int bit, void *addr)
599 + mb_correct_addr_and_bit(bit,addr);
600 + ext2_set_bit_atomic(NULL, bit, addr);
603 +static inline void mb_clear_bit(int bit, void *addr)
605 + mb_correct_addr_and_bit(bit,addr);
606 + ext2_clear_bit(bit, addr);
609 +static inline void mb_clear_bit_atomic(int bit, void *addr)
611 + mb_correct_addr_and_bit(bit,addr);
612 + ext2_clear_bit_atomic(NULL, bit, addr);
615 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
618 +#if BITS_PER_LONG == 64
619 + fix = ((unsigned long) addr & 7UL) << 3;
620 + addr = (void *) ((unsigned long) addr & ~7UL);
621 +#elif BITS_PER_LONG == 32
622 + fix = ((unsigned long) addr & 3UL) << 3;
623 + addr = (void *) ((unsigned long) addr & ~3UL);
625 +#error "how many bits you are?!"
629 + return ext2_find_next_zero_bit(addr, max, start) - fix;
632 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
636 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
637 + J_ASSERT(max != NULL);
639 + if (order > e3b->bd_blkbits + 1) {
644 + /* at order 0 we see each particular block */
645 + *max = 1 << (e3b->bd_blkbits + 3);
647 + return EXT3_MB_BITMAP(e3b);
649 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
650 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
655 +#ifdef AGGRESSIVE_CHECK
657 +static void mb_check_buddy(struct ext3_buddy *e3b)
659 + int order = e3b->bd_blkbits + 1;
660 + int max, max2, i, j, k, count;
661 + int fragments = 0, fstart;
662 + void *buddy, *buddy2;
664 + if (!test_opt(e3b->bd_sb, MBALLOC))
668 + static int mb_check_counter = 0;
669 + if (mb_check_counter++ % 300 != 0)
673 + while (order > 1) {
674 + buddy = mb_find_buddy(e3b, order, &max);
676 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
678 + J_ASSERT(buddy != buddy2);
679 + J_ASSERT(max * 2 == max2);
682 + for (i = 0; i < max; i++) {
684 + if (mb_test_bit(i, buddy)) {
685 + /* only single bit in buddy2 may be 1 */
686 + if (!mb_test_bit(i << 1, buddy2))
687 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
688 + else if (!mb_test_bit((i << 1) + 1, buddy2))
689 + J_ASSERT(mb_test_bit(i << 1, buddy2));
693 + /* both bits in buddy2 must be 0 */
694 + J_ASSERT(mb_test_bit(i << 1, buddy2));
695 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
697 + for (j = 0; j < (1 << order); j++) {
698 + k = (i * (1 << order)) + j;
699 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
703 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
708 + buddy = mb_find_buddy(e3b, 0, &max);
709 + for (i = 0; i < max; i++) {
710 + if (!mb_test_bit(i, buddy)) {
711 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
712 + if (fstart == -1) {
719 + /* check used bits only */
720 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
721 + buddy2 = mb_find_buddy(e3b, j, &max2);
723 + J_ASSERT(k < max2);
724 + J_ASSERT(mb_test_bit(k, buddy2));
727 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
728 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
732 +#define mb_check_buddy(e3b)
735 +/* find most significant bit */
736 +static int inline fmsb(unsigned short word)
750 + } while (word != 0);
756 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
757 + int len, struct ext3_group_info *grp)
759 + struct ext3_sb_info *sbi = EXT3_SB(sb);
760 + unsigned short min, max, chunk, border;
762 + mb_debug("mark %u/%u free\n", first, len);
763 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
765 + border = 2 << sb->s_blocksize_bits;
768 + /* find how many blocks can be covered since this position */
769 + max = ffs(first | border) - 1;
771 + /* find how many blocks of power 2 we need to mark */
774 + mb_debug(" %u/%u -> max %u, min %u\n",
775 + first & ((2 << sb->s_blocksize_bits) - 1),
782 + /* mark multiblock chunks only */
783 + grp->bb_counters[min]++;
785 + mb_debug(" set %u at %u \n", first >> min,
786 + sbi->s_mb_offsets[min]);
787 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
796 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
799 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
800 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
801 + unsigned short i = 0, first, len;
802 + unsigned free = 0, fragments = 0;
803 + unsigned long long period = get_cycles();
805 + i = mb_find_next_zero_bit(bitmap, max, 0);
806 + grp->bb_first_free = i;
810 + i = ext2_find_next_le_bit(bitmap, max, i);
814 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
816 + grp->bb_counters[0]++;
818 + i = mb_find_next_zero_bit(bitmap, max, i);
820 + grp->bb_fragments = fragments;
822 + /* bb_state shouldn't being modified because all
823 + * others waits for init completion on page lock */
824 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
825 + if (free != grp->bb_free) {
826 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
827 + group, free, grp->bb_free);
828 + grp->bb_free = free;
831 + period = get_cycles() - period;
832 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
833 + EXT3_SB(sb)->s_mb_buddies_generated++;
834 + EXT3_SB(sb)->s_mb_generation_time += period;
835 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
838 +static int ext3_mb_init_cache(struct page *page)
840 + int blocksize, blocks_per_page, groups_per_page;
841 + int err = 0, i, first_group, first_block;
842 + struct super_block *sb;
843 + struct buffer_head *bhs;
844 + struct buffer_head **bh;
845 + struct inode *inode;
846 + char *data, *bitmap;
848 + mb_debug("init page %lu\n", page->index);
850 + inode = page->mapping->host;
852 + blocksize = 1 << inode->i_blkbits;
853 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
855 + groups_per_page = blocks_per_page >> 1;
856 + if (groups_per_page == 0)
857 + groups_per_page = 1;
859 + /* allocate buffer_heads to read bitmaps */
860 + if (groups_per_page > 1) {
862 + i = sizeof(struct buffer_head *) * groups_per_page;
863 + bh = kmalloc(i, GFP_NOFS);
870 + first_group = page->index * blocks_per_page / 2;
872 + /* read all groups the page covers into the cache */
873 + for (i = 0; i < groups_per_page; i++) {
874 + struct ext3_group_desc * desc;
876 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
880 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
885 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
889 + if (buffer_uptodate(bh[i]))
892 + lock_buffer(bh[i]);
893 + if (buffer_uptodate(bh[i])) {
894 + unlock_buffer(bh[i]);
899 + bh[i]->b_end_io = end_buffer_read_sync;
900 + submit_bh(READ, bh[i]);
901 + mb_debug("read bitmap for group %u\n", first_group + i);
904 + /* wait for I/O completion */
905 + for (i = 0; i < groups_per_page && bh[i]; i++)
906 + wait_on_buffer(bh[i]);
909 + for (i = 0; i < groups_per_page && bh[i]; i++)
910 + if (!buffer_uptodate(bh[i]))
913 + first_block = page->index * blocks_per_page;
914 + for (i = 0; i < blocks_per_page; i++) {
917 + group = (first_block + i) >> 1;
918 + if (group >= EXT3_SB(sb)->s_groups_count)
921 + data = page_address(page) + (i * blocksize);
922 + bitmap = bh[group - first_group]->b_data;
924 + if ((first_block + i) & 1) {
925 + /* this is block of buddy */
926 + mb_debug("put buddy for group %u in page %lu/%x\n",
927 + group, page->index, i * blocksize);
928 + memset(data, 0xff, blocksize);
929 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
930 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
931 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
932 + ext3_mb_generate_buddy(sb, data, bitmap, group);
934 + /* this is block of bitmap */
935 + mb_debug("put bitmap for group %u in page %lu/%x\n",
936 + group, page->index, i * blocksize);
937 + memcpy(data, bitmap, blocksize);
940 + SetPageUptodate(page);
944 + for (i = 0; i < groups_per_page && bh[i]; i++)
952 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
953 + struct ext3_buddy *e3b)
955 + struct ext3_sb_info *sbi = EXT3_SB(sb);
956 + struct inode *inode = sbi->s_buddy_cache;
957 + int blocks_per_page, block, pnum, poff;
960 + mb_debug("load group %u\n", group);
962 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
964 + e3b->bd_blkbits = sb->s_blocksize_bits;
965 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
967 + e3b->bd_group = group;
968 + e3b->bd_buddy_page = NULL;
969 + e3b->bd_bitmap_page = NULL;
972 + pnum = block / blocks_per_page;
973 + poff = block % blocks_per_page;
975 + /* we could use find_or_create_page(), but it locks page
976 + * what we'd like to avoid in fast path ... */
977 + page = find_get_page(inode->i_mapping, pnum);
978 + if (page == NULL || !PageUptodate(page)) {
980 + page_cache_release(page);
981 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
983 + BUG_ON(page->mapping != inode->i_mapping);
984 + if (!PageUptodate(page))
985 + ext3_mb_init_cache(page);
989 + if (page == NULL || !PageUptodate(page))
991 + e3b->bd_bitmap_page = page;
992 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
993 + mark_page_accessed(page);
996 + pnum = block / blocks_per_page;
997 + poff = block % blocks_per_page;
999 + page = find_get_page(inode->i_mapping, pnum);
1000 + if (page == NULL || !PageUptodate(page)) {
1002 + page_cache_release(page);
1003 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1005 + BUG_ON(page->mapping != inode->i_mapping);
1006 + if (!PageUptodate(page))
1007 + ext3_mb_init_cache(page);
1008 + unlock_page(page);
1011 + if (page == NULL || !PageUptodate(page))
1013 + e3b->bd_buddy_page = page;
1014 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1015 + mark_page_accessed(page);
1017 + J_ASSERT(e3b->bd_bitmap_page != NULL);
1018 + J_ASSERT(e3b->bd_buddy_page != NULL);
1023 + if (e3b->bd_bitmap_page)
1024 + page_cache_release(e3b->bd_bitmap_page);
1025 + if (e3b->bd_buddy_page)
1026 + page_cache_release(e3b->bd_buddy_page);
1027 + e3b->bd_buddy = NULL;
1028 + e3b->bd_bitmap = NULL;
1032 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1034 + if (e3b->bd_bitmap_page)
1035 + page_cache_release(e3b->bd_bitmap_page);
1036 + if (e3b->bd_buddy_page)
1037 + page_cache_release(e3b->bd_buddy_page);
1042 +ext3_lock_group(struct super_block *sb, int group)
1044 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
1045 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1049 +ext3_unlock_group(struct super_block *sb, int group)
1051 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
1052 + &EXT3_GROUP_INFO(sb, group)->bb_state);
1055 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1060 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
1061 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
1063 + bb = EXT3_MB_BUDDY(e3b);
1064 + while (order <= e3b->bd_blkbits + 1) {
1065 + block = block >> 1;
1066 + if (!mb_test_bit(block, bb)) {
1067 + /* this block is part of buddy of order 'order' */
1070 + bb += 1 << (e3b->bd_blkbits - order);
1076 +static inline void mb_clear_bits(void *bm, int cur, int len)
1081 + while (cur < len) {
1082 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1083 + /* fast path: clear whole word at once */
1084 + addr = bm + (cur >> 3);
1089 + mb_clear_bit_atomic(cur, bm);
1094 +static inline void mb_set_bits(void *bm, int cur, int len)
1099 + while (cur < len) {
1100 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1101 + /* fast path: clear whole word at once */
1102 + addr = bm + (cur >> 3);
1103 + *addr = 0xffffffff;
1107 + mb_set_bit_atomic(cur, bm);
1112 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
1114 + int block = 0, max = 0, order;
1115 + void *buddy, *buddy2;
1117 + mb_check_buddy(e3b);
1119 + e3b->bd_info->bb_free += count;
1120 + if (first < e3b->bd_info->bb_first_free)
1121 + e3b->bd_info->bb_first_free = first;
1123 + /* let's maintain fragments counter */
1125 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1126 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1127 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1129 + e3b->bd_info->bb_fragments--;
1130 + else if (!block && !max)
1131 + e3b->bd_info->bb_fragments++;
1133 + /* let's maintain buddy itself */
1134 + while (count-- > 0) {
1138 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
1139 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1140 + e3b->bd_info->bb_counters[order]++;
1142 + /* start of the buddy */
1143 + buddy = mb_find_buddy(e3b, order, &max);
1147 + if (mb_test_bit(block, buddy) ||
1148 + mb_test_bit(block + 1, buddy))
1151 + /* both the buddies are free, try to coalesce them */
1152 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1158 + /* for special purposes, we don't set
1159 + * free bits in bitmap */
1160 + mb_set_bit(block, buddy);
1161 + mb_set_bit(block + 1, buddy);
1163 + e3b->bd_info->bb_counters[order]--;
1164 + e3b->bd_info->bb_counters[order]--;
1166 + block = block >> 1;
1168 + e3b->bd_info->bb_counters[order]++;
1170 + mb_clear_bit(block, buddy2);
1174 + mb_check_buddy(e3b);
1179 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1180 + int needed, struct ext3_free_extent *ex)
1182 + int next = block, max, ord;
1185 + J_ASSERT(ex != NULL);
1187 + buddy = mb_find_buddy(e3b, order, &max);
1189 + J_ASSERT(block < max);
1190 + if (mb_test_bit(block, buddy)) {
1197 + if (likely(order == 0)) {
1198 + /* find actual order */
1199 + order = mb_find_order_for_block(e3b, block);
1200 + block = block >> order;
1203 + ex->fe_len = 1 << order;
1204 + ex->fe_start = block << order;
1205 + ex->fe_group = e3b->bd_group;
1207 + /* calc difference from given start */
1208 + next = next - ex->fe_start;
1209 + ex->fe_len -= next;
1210 + ex->fe_start += next;
1212 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1214 + if (block + 1 >= max)
1217 + next = (block + 1) * (1 << order);
1218 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1221 + ord = mb_find_order_for_block(e3b, next);
1224 + block = next >> order;
1225 + ex->fe_len += 1 << order;
1228 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
1229 + return ex->fe_len;
1232 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1234 + int ord, mlen = 0, max = 0, cur;
1235 + int start = ex->fe_start;
1236 + int len = ex->fe_len;
1241 + mb_check_buddy(e3b);
1243 + e3b->bd_info->bb_free -= len;
1244 + if (e3b->bd_info->bb_first_free == start)
1245 + e3b->bd_info->bb_first_free += len;
1247 + /* let's maintain fragments counter */
1249 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1250 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1251 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1253 + e3b->bd_info->bb_fragments++;
1254 + else if (!mlen && !max)
1255 + e3b->bd_info->bb_fragments--;
1257 + /* let's maintain buddy itself */
1259 + ord = mb_find_order_for_block(e3b, start);
1261 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1262 + /* the whole chunk may be allocated at once! */
1264 + buddy = mb_find_buddy(e3b, ord, &max);
1265 + J_ASSERT((start >> ord) < max);
1266 + mb_set_bit(start >> ord, buddy);
1267 + e3b->bd_info->bb_counters[ord]--;
1270 + J_ASSERT(len >= 0);
1274 + /* store for history */
1276 + ret = len | (ord << 16);
1278 + /* we have to split large buddy */
1279 + J_ASSERT(ord > 0);
1280 + buddy = mb_find_buddy(e3b, ord, &max);
1281 + mb_set_bit(start >> ord, buddy);
1282 + e3b->bd_info->bb_counters[ord]--;
1285 + cur = (start >> ord) & ~1U;
1286 + buddy = mb_find_buddy(e3b, ord, &max);
1287 + mb_clear_bit(cur, buddy);
1288 + mb_clear_bit(cur + 1, buddy);
1289 + e3b->bd_info->bb_counters[ord]++;
1290 + e3b->bd_info->bb_counters[ord]++;
1293 + /* now drop all the bits in bitmap */
1294 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1296 + mb_check_buddy(e3b);
1302 + * Must be called under group lock!
1304 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1305 + struct ext3_buddy *e3b)
1307 + unsigned long ret;
1309 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1310 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1312 + ac->ac_status = AC_STATUS_FOUND;
1313 + ac->ac_tail = ret & 0xffff;
1314 + ac->ac_buddy = ret >> 16;
1316 + /* hold in-core structures until allocated
1317 + * blocks are marked non-free in on-disk bitmap */
1318 + ac->ac_buddy_page = e3b->bd_buddy_page;
1319 + page_cache_get(e3b->bd_buddy_page);
1320 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1321 + page_cache_get(e3b->bd_bitmap_page);
1325 + * The routine checks whether found extent is good enough. If it is,
1326 + * then the extent gets marked used and flag is set to the context
1327 + * to stop scanning. Otherwise, the extent is compared with the
1328 + * previous found extent and if new one is better, then it's stored
1329 + * in the context. Later, the best found extent will be used, if
1330 + * mballoc can't find good enough extent.
1332 + * FIXME: real allocation policy is to be designed yet!
1334 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1335 + struct ext3_free_extent *ex,
1336 + struct ext3_buddy *e3b)
1338 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1339 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1341 + J_ASSERT(ex->fe_len > 0);
1342 + J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1343 + J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
1348 + * The special case - take what you catch first
1350 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1352 + ext3_mb_use_best_found(ac, e3b);
1357 + * Let's check whether the chunk is good enough
1359 + if (ex->fe_len == gex->fe_len) {
1361 + ext3_mb_use_best_found(ac, e3b);
1366 + * If this is first found extent, just store it in the context
1368 + if (bex->fe_len == 0) {
1374 + * If new found extent is better, store it in the context
1376 + if (bex->fe_len < gex->fe_len) {
1377 + /* if the request isn't satisfied, any found extent
1378 + * larger than previous best one is better */
1379 + if (ex->fe_len > bex->fe_len)
1381 + } else if (ex->fe_len > gex->fe_len) {
1382 + /* if the request is satisfied, then we try to find
1383 + * an extent that still satisfy the request, but is
1384 + * smaller than previous one */
1389 + * Let's scan at least few extents and don't pick up a first one
1391 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1392 + ac->ac_status = AC_STATUS_BREAK;
1395 + * We don't want to scan for a whole year
1397 + if (ac->ac_found > ext3_mb_max_to_scan)
1398 + ac->ac_status = AC_STATUS_BREAK;
1401 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1402 + struct ext3_buddy *e3b)
1404 + struct ext3_free_extent ex = ac->ac_b_ex;
1405 + int group = ex.fe_group, max, err;
1407 + J_ASSERT(ex.fe_len > 0);
1408 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1412 + ext3_lock_group(ac->ac_sb, group);
1413 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1417 + ext3_mb_use_best_found(ac, e3b);
1420 + ext3_unlock_group(ac->ac_sb, group);
1422 + ext3_mb_release_desc(e3b);
1427 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1428 + struct ext3_buddy *e3b)
1430 + int group = ac->ac_g_ex.fe_group, max, err;
1431 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1432 + struct ext3_super_block *es = sbi->s_es;
1433 + struct ext3_free_extent ex;
1435 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1439 + ext3_lock_group(ac->ac_sb, group);
1440 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1441 + ac->ac_g_ex.fe_len, &ex);
1443 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1444 + unsigned long start;
1445 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1446 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1447 + if (start % sbi->s_stripe == 0) {
1450 + ext3_mb_use_best_found(ac, e3b);
1452 + } else if (max >= ac->ac_g_ex.fe_len) {
1453 + J_ASSERT(ex.fe_len > 0);
1454 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1455 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1458 + ext3_mb_use_best_found(ac, e3b);
1459 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1460 + /* Sometimes, caller may want to merge even small
1461 + * number of blocks to an existing extent */
1462 + J_ASSERT(ex.fe_len > 0);
1463 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1464 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1467 + ext3_mb_use_best_found(ac, e3b);
1469 + ext3_unlock_group(ac->ac_sb, group);
1471 + ext3_mb_release_desc(e3b);
1477 + * The routine scans buddy structures (not bitmap!) from given order
1478 + * to max order and tries to find big enough chunk to satisfy the req
1480 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1481 + struct ext3_buddy *e3b)
1483 + struct super_block *sb = ac->ac_sb;
1484 + struct ext3_group_info *grp = e3b->bd_info;
1488 + J_ASSERT(ac->ac_2order > 0);
1489 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1490 + if (grp->bb_counters[i] == 0)
1493 + buddy = mb_find_buddy(e3b, i, &max);
1494 + if (buddy == NULL) {
1495 + printk(KERN_ALERT "looking for wrong order?\n");
1499 + k = mb_find_next_zero_bit(buddy, max, 0);
1500 + J_ASSERT(k < max);
1504 + ac->ac_b_ex.fe_len = 1 << i;
1505 + ac->ac_b_ex.fe_start = k << i;
1506 + ac->ac_b_ex.fe_group = e3b->bd_group;
1508 + ext3_mb_use_best_found(ac, e3b);
1509 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1511 + if (unlikely(ext3_mb_stats))
1512 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1519 + * The routine scans the group and measures all found extents.
1520 + * In order to optimize scanning, caller must pass number of
1521 + * free blocks in the group, so the routine can know upper limit.
1523 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1524 + struct ext3_buddy *e3b)
1526 + struct super_block *sb = ac->ac_sb;
1527 + void *bitmap = EXT3_MB_BITMAP(e3b);
1528 + struct ext3_free_extent ex;
1531 + free = e3b->bd_info->bb_free;
1532 + J_ASSERT(free > 0);
1534 + i = e3b->bd_info->bb_first_free;
1536 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1537 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1538 + if (i >= sb->s_blocksize * 8) {
1539 + J_ASSERT(free == 0);
1543 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1544 + J_ASSERT(ex.fe_len > 0);
1545 + J_ASSERT(free >= ex.fe_len);
1547 + ext3_mb_measure_extent(ac, &ex, e3b);
1550 + free -= ex.fe_len;
1555 + * This is a special case for storages like raid5
1556 + * we try to find stripe-aligned chunks for stripe-size requests
1558 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1559 + struct ext3_buddy *e3b)
1561 + struct super_block *sb = ac->ac_sb;
1562 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1563 + void *bitmap = EXT3_MB_BITMAP(e3b);
1564 + struct ext3_free_extent ex;
1565 + unsigned long i, max;
1567 + J_ASSERT(sbi->s_stripe != 0);
1569 + /* find first stripe-aligned block */
1570 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb)
1571 + + le32_to_cpu(sbi->s_es->s_first_data_block);
1572 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1573 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block))
1574 + % EXT3_BLOCKS_PER_GROUP(sb);
1576 + while (i < sb->s_blocksize * 8) {
1577 + if (!mb_test_bit(i, bitmap)) {
1578 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1579 + if (max >= sbi->s_stripe) {
1582 + ext3_mb_use_best_found(ac, e3b);
1586 + i += sbi->s_stripe;
1590 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1591 + int group, int cr)
1593 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1594 + unsigned free, fragments, i, bits;
1596 + J_ASSERT(cr >= 0 && cr < 4);
1597 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1599 + free = grp->bb_free;
1600 + fragments = grp->bb_fragments;
1603 + if (fragments == 0)
1608 + J_ASSERT(ac->ac_2order != 0);
1609 + bits = ac->ac_sb->s_blocksize_bits + 1;
1610 + for (i = ac->ac_2order; i <= bits; i++)
1611 + if (grp->bb_counters[i] > 0)
1615 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1619 + if (free >= ac->ac_g_ex.fe_len)
1631 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1632 + unsigned long goal, int *len, int flags, int *errp)
1634 + struct buffer_head *bitmap_bh = NULL;
1635 + struct ext3_allocation_context ac;
1636 + int i, group, block, cr, err = 0;
1637 + struct ext3_group_desc *gdp;
1638 + struct ext3_super_block *es;
1639 + struct buffer_head *gdp_bh;
1640 + struct ext3_sb_info *sbi;
1641 + struct super_block *sb;
1642 + struct ext3_buddy e3b;
1644 + J_ASSERT(len != NULL);
1645 + J_ASSERT(*len > 0);
1649 + printk("ext3_mb_new_nblocks: nonexistent device");
1653 + if (!test_opt(sb, MBALLOC)) {
1654 + static int ext3_mballoc_warning = 0;
1655 + if (ext3_mballoc_warning == 0) {
1656 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1657 + "mballoc disabled!\n");
1658 + ext3_mballoc_warning++;
1661 + err = ext3_new_block_old(handle, inode, goal, errp);
1665 + ext3_mb_poll_new_transaction(sb, handle);
1667 + sbi = EXT3_SB(sb);
1668 + es = EXT3_SB(sb)->s_es;
1671 + * We can't allocate > group size
1673 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1674 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1676 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1677 + /* someone asks for non-reserved blocks */
1679 + err = ext3_mb_reserve_blocks(sb, 1);
1686 + ac.ac_buddy_page = NULL;
1687 + ac.ac_bitmap_page = NULL;
1690 + * Check quota for allocation of this blocks.
1692 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1700 + /* start searching from the goal */
1701 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1702 + goal >= le32_to_cpu(es->s_blocks_count))
1703 + goal = le32_to_cpu(es->s_first_data_block);
1704 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1705 + EXT3_BLOCKS_PER_GROUP(sb);
1706 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1707 + EXT3_BLOCKS_PER_GROUP(sb));
1709 + /* set up allocation goals */
1710 + ac.ac_b_ex.fe_group = 0;
1711 + ac.ac_b_ex.fe_start = 0;
1712 + ac.ac_b_ex.fe_len = 0;
1713 + ac.ac_status = AC_STATUS_CONTINUE;
1714 + ac.ac_groups_scanned = 0;
1715 + ac.ac_ex_scanned = 0;
1717 + ac.ac_sb = inode->i_sb;
1718 + ac.ac_g_ex.fe_group = group;
1719 + ac.ac_g_ex.fe_start = block;
1720 + ac.ac_g_ex.fe_len = *len;
1721 + ac.ac_flags = flags;
1723 + ac.ac_criteria = 0;
1725 + if (*len == 1 && sbi->s_stripe) {
1726 + /* looks like a metadata, let's use a dirty hack for raid5
1727 + * move all metadata in first groups in hope to hit cached
1728 + * sectors and thus avoid read-modify cycles in raid5 */
1729 + ac.ac_g_ex.fe_group = group = 0;
1732 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1734 + if (i >= ext3_mb_order2_reqs) {
1736 + if ((*len & (~(1 << i))) == 0)
1740 + /* first, try the goal */
1741 + err = ext3_mb_find_by_goal(&ac, &e3b);
1744 + if (ac.ac_status == AC_STATUS_FOUND)
1747 + /* Let's just scan groups to find more-less suitable blocks */
1748 + cr = ac.ac_2order ? 0 : 1;
1750 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1751 + ac.ac_criteria = cr;
1752 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1753 + if (group == EXT3_SB(sb)->s_groups_count)
1756 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1757 + /* we need full data about the group
1758 + * to make a good selection */
1759 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1762 + ext3_mb_release_desc(&e3b);
1765 + /* check is group good for our criteries */
1766 + if (!ext3_mb_good_group(&ac, group, cr))
1769 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1773 + ext3_lock_group(sb, group);
1774 + if (!ext3_mb_good_group(&ac, group, cr)) {
1775 + /* someone did allocation from this group */
1776 + ext3_unlock_group(sb, group);
1777 + ext3_mb_release_desc(&e3b);
1781 + ac.ac_groups_scanned++;
1783 + ext3_mb_simple_scan_group(&ac, &e3b);
1784 + else if (cr == 1 && *len == sbi->s_stripe)
1785 + ext3_mb_scan_aligned(&ac, &e3b);
1787 + ext3_mb_complex_scan_group(&ac, &e3b);
1789 + ext3_unlock_group(sb, group);
1791 + ext3_mb_release_desc(&e3b);
1793 + if (ac.ac_status != AC_STATUS_CONTINUE)
1798 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1799 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1801 + * We've been searching too long. Let's try to allocate
1802 + * the best chunk we've found so far
1805 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1806 + printk(KERN_DEBUG "EXT3-fs: too long searching at "
1807 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1808 + ac.ac_g_ex.fe_len);*/
1809 + ext3_mb_try_best_found(&ac, &e3b);
1810 + if (ac.ac_status != AC_STATUS_FOUND) {
1812 + * Someone more lucky has already allocated it.
1813 + * The only thing we can do is just take first
1815 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
1817 + ac.ac_b_ex.fe_group = 0;
1818 + ac.ac_b_ex.fe_start = 0;
1819 + ac.ac_b_ex.fe_len = 0;
1820 + ac.ac_status = AC_STATUS_CONTINUE;
1821 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1827 + if (ac.ac_status != AC_STATUS_FOUND) {
1829 + * We aren't lucky definitely
1831 + DQUOT_FREE_BLOCK(inode, *len);
1835 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
1836 + ac.ac_status, ac.ac_flags);
1837 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d cr %d\n",
1838 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1839 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1840 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1841 + sbi->s_blocks_reserved, ac.ac_found);
1842 + printk("EXT3-fs: groups: ");
1843 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1844 + printk("%d: %d ", i, EXT3_GROUP_INFO(sb, i)->bb_free);
1851 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1853 + /* good news - free block(s) have been found. now it's time
1854 + * to mark block(s) in good old journaled bitmap */
1855 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1856 + + ac.ac_b_ex.fe_start
1857 + + le32_to_cpu(es->s_first_data_block);
1859 + /* we made a desicion, now mark found blocks in good old
1860 + * bitmap to be journaled */
1862 + ext3_debug("using block group %d(%d)\n",
1863 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1865 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1871 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1877 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1883 + err = ext3_journal_get_write_access(handle, gdp_bh);
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 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1892 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1893 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1894 + EXT3_SB(sb)->s_itb_per_group))
1895 + ext3_error(sb, "ext3_new_block",
1896 + "Allocating block in system zone - "
1897 + "block = %u", block);
1898 +#ifdef AGGRESSIVE_CHECK
1899 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1900 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1902 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1904 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1905 + gdp->bg_free_blocks_count =
1906 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1907 + - ac.ac_b_ex.fe_len);
1908 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1909 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1911 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1914 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1920 + brelse(bitmap_bh);
1922 + /* drop non-allocated, but dquote'd blocks */
1923 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1924 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1926 + *len = ac.ac_b_ex.fe_len;
1927 + J_ASSERT(*len > 0);
1928 + J_ASSERT(block != 0);
1932 + /* if we've already allocated something, roll it back */
1933 + if (ac.ac_status == AC_STATUS_FOUND) {
1934 + /* FIXME: free blocks here */
1937 + DQUOT_FREE_BLOCK(inode, *len);
1938 + brelse(bitmap_bh);
1942 + if (ac.ac_buddy_page)
1943 + page_cache_release(ac.ac_buddy_page);
1944 + if (ac.ac_bitmap_page)
1945 + page_cache_release(ac.ac_bitmap_page);
1947 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1948 + /* block wasn't reserved before and we reserved it
1949 + * at the beginning of allocation. it doesn't matter
1950 + * whether we allocated anything or we failed: time
1951 + * to release reservation. NOTE: because I expect
1952 + * any multiblock request from delayed allocation
1953 + * path only, here is single block always */
1954 + ext3_mb_release_blocks(sb, 1);
1957 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1958 + atomic_inc(&sbi->s_bal_reqs);
1959 + atomic_add(*len, &sbi->s_bal_allocated);
1960 + if (*len >= ac.ac_g_ex.fe_len)
1961 + atomic_inc(&sbi->s_bal_success);
1962 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1963 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1964 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1965 + atomic_inc(&sbi->s_bal_goals);
1966 + if (ac.ac_found > ext3_mb_max_to_scan)
1967 + atomic_inc(&sbi->s_bal_breaks);
1970 + ext3_mb_store_history(sb, inode->i_ino, &ac);
1974 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1976 +#ifdef EXT3_MB_HISTORY
1977 +struct ext3_mb_proc_session {
1978 + struct ext3_mb_history *history;
1979 + struct super_block *sb;
1984 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1985 + struct ext3_mb_history *hs,
1988 + if (hs == s->history + s->max)
1990 + if (!first && hs == s->history + s->start)
1992 + while (hs->goal.fe_len == 0) {
1994 + if (hs == s->history + s->max)
1996 + if (hs == s->history + s->start)
2002 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2004 + struct ext3_mb_proc_session *s = seq->private;
2005 + struct ext3_mb_history *hs;
2009 + return SEQ_START_TOKEN;
2010 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2013 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2017 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2019 + struct ext3_mb_proc_session *s = seq->private;
2020 + struct ext3_mb_history *hs = v;
2023 + if (v == SEQ_START_TOKEN)
2024 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2026 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2029 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2031 + struct ext3_mb_history *hs = v;
2032 + char buf[20], buf2[20];
2034 + if (v == SEQ_START_TOKEN) {
2035 + seq_printf(seq, "%-5s %-8s %-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
2036 + "pid", "inode", "goal", "result", "found", "grps", "cr",
2037 + "merge", "tail", "broken");
2041 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
2042 + hs->goal.fe_start, hs->goal.fe_len);
2043 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
2044 + hs->result.fe_start, hs->result.fe_len);
2045 + seq_printf(seq, "%-5u %-8u %-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n",
2046 + hs->pid, hs->ino, buf, buf2, hs->found, hs->groups,
2047 + hs->cr, hs->merged ? "M" : "", hs->tail,
2048 + hs->buddy ? 1 << hs->buddy : 0);
2052 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2056 +static struct seq_operations ext3_mb_seq_history_ops = {
2057 + .start = ext3_mb_seq_history_start,
2058 + .next = ext3_mb_seq_history_next,
2059 + .stop = ext3_mb_seq_history_stop,
2060 + .show = ext3_mb_seq_history_show,
2063 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2065 + struct super_block *sb = PDE(inode)->data;
2066 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2067 + struct ext3_mb_proc_session *s;
2070 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2073 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2074 + s->history = kmalloc(size, GFP_KERNEL);
2080 + spin_lock(&sbi->s_mb_history_lock);
2081 + memcpy(s->history, sbi->s_mb_history, size);
2082 + s->max = sbi->s_mb_history_max;
2083 + s->start = sbi->s_mb_history_cur % s->max;
2084 + spin_unlock(&sbi->s_mb_history_lock);
2086 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2088 + struct seq_file *m = (struct seq_file *)file->private_data;
2091 + kfree(s->history);
2098 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2100 + struct seq_file *seq = (struct seq_file *)file->private_data;
2101 + struct ext3_mb_proc_session *s = seq->private;
2102 + kfree(s->history);
2104 + return seq_release(inode, file);
2107 +static struct file_operations ext3_mb_seq_history_fops = {
2108 + .owner = THIS_MODULE,
2109 + .open = ext3_mb_seq_history_open,
2111 + .llseek = seq_lseek,
2112 + .release = ext3_mb_seq_history_release,
2115 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2117 + struct super_block *sb = seq->private;
2118 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2121 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2125 + return (void *) group;
2128 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2130 + struct super_block *sb = seq->private;
2131 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2135 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2138 + return (void *) group;;
2141 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2143 + struct super_block *sb = seq->private;
2144 + long group = (long) v, i;
2146 + struct ext3_group_info info;
2147 + unsigned short counters[16];
2152 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s [ %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2153 + "group", "free", "frags", "first", "2^0", "2^1", "2^2",
2154 + "2^3", "2^4", "2^5", "2^6", "2^7", "2^8", "2^9", "2^10",
2155 + "2^11", "2^12", "2^13");
2157 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2158 + sizeof(struct ext3_group_info);
2159 + ext3_lock_group(sb, group);
2160 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2161 + ext3_unlock_group(sb, group);
2163 + if (EXT3_MB_GRP_NEED_INIT(&sg.info))
2166 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2167 + sg.info.bb_fragments, sg.info.bb_first_free);
2168 + for (i = 0; i <= 13; i++)
2169 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2170 + sg.info.bb_counters[i] : 0);
2171 + seq_printf(seq, " ]\n");
2176 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2180 +static struct seq_operations ext3_mb_seq_groups_ops = {
2181 + .start = ext3_mb_seq_groups_start,
2182 + .next = ext3_mb_seq_groups_next,
2183 + .stop = ext3_mb_seq_groups_stop,
2184 + .show = ext3_mb_seq_groups_show,
2187 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2189 + struct super_block *sb = PDE(inode)->data;
2192 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2194 + struct seq_file *m = (struct seq_file *)file->private_data;
2201 +static struct file_operations ext3_mb_seq_groups_fops = {
2202 + .owner = THIS_MODULE,
2203 + .open = ext3_mb_seq_groups_open,
2205 + .llseek = seq_lseek,
2206 + .release = seq_release,
2209 +static void ext3_mb_history_release(struct super_block *sb)
2211 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2214 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2215 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2216 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2217 + remove_proc_entry(name, proc_root_ext3);
2219 + if (sbi->s_mb_history)
2220 + kfree(sbi->s_mb_history);
2223 +static void ext3_mb_history_init(struct super_block *sb)
2225 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2229 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
2230 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
2231 + if (sbi->s_mb_proc != NULL) {
2232 + struct proc_dir_entry *p;
2233 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2235 + p->proc_fops = &ext3_mb_seq_history_fops;
2238 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2240 + p->proc_fops = &ext3_mb_seq_groups_fops;
2245 + sbi->s_mb_history_max = 1000;
2246 + sbi->s_mb_history_cur = 0;
2247 + spin_lock_init(&sbi->s_mb_history_lock);
2248 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2249 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2250 + memset(sbi->s_mb_history, 0, i);
2251 + /* if we can't allocate history, then we simple won't use it */
2255 +ext3_mb_store_history(struct super_block *sb, unsigned ino,
2256 + struct ext3_allocation_context *ac)
2258 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2259 + struct ext3_mb_history h;
2261 + if (likely(sbi->s_mb_history == NULL))
2264 + h.pid = current->pid;
2266 + h.goal = ac->ac_g_ex;
2267 + h.result = ac->ac_b_ex;
2268 + h.found = ac->ac_found;
2269 + h.cr = ac->ac_criteria;
2270 + h.groups = ac->ac_groups_scanned;
2271 + h.tail = ac->ac_tail;
2272 + h.buddy = ac->ac_buddy;
2274 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2275 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2278 + spin_lock(&sbi->s_mb_history_lock);
2279 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2280 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2281 + sbi->s_mb_history_cur = 0;
2282 + spin_unlock(&sbi->s_mb_history_lock);
2286 +#define ext3_mb_history_release(sb)
2287 +#define ext3_mb_history_init(sb)
2290 +int ext3_mb_init_backend(struct super_block *sb)
2292 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2293 + int i, j, len, metalen;
2294 + int num_meta_group_infos =
2295 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2296 + EXT3_DESC_PER_BLOCK_BITS(sb);
2297 + struct ext3_group_info **meta_group_info;
2299 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2300 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2301 + * So a two level scheme suffices for now. */
2302 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2303 + num_meta_group_infos, GFP_KERNEL);
2304 + if (sbi->s_group_info == NULL) {
2305 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2308 + sbi->s_buddy_cache = new_inode(sb);
2309 + if (sbi->s_buddy_cache == NULL) {
2310 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2314 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2315 + for (i = 0; i < num_meta_group_infos; i++) {
2316 + if ((i + 1) == num_meta_group_infos)
2317 + metalen = sizeof(*meta_group_info) *
2318 + (sbi->s_groups_count -
2319 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2320 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2321 + if (meta_group_info == NULL) {
2322 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2324 + goto err_freemeta;
2326 + sbi->s_group_info[i] = meta_group_info;
2330 + * calculate needed size. if change bb_counters size,
2331 + * don't forget about ext3_mb_generate_buddy()
2333 + len = sizeof(struct ext3_group_info);
2334 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2335 + for (i = 0; i < sbi->s_groups_count; i++) {
2336 + struct ext3_group_desc * desc;
2339 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2340 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2342 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2343 + if (meta_group_info[j] == NULL) {
2344 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2346 + goto err_freebuddy;
2348 + desc = ext3_get_group_desc(sb, i, NULL);
2349 + if (desc == NULL) {
2350 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2351 + goto err_freebuddy;
2353 + memset(meta_group_info[j], 0, len);
2354 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2355 + &meta_group_info[j]->bb_state);
2356 + meta_group_info[j]->bb_free =
2357 + le16_to_cpu(desc->bg_free_blocks_count);
2364 + kfree(EXT3_GROUP_INFO(sb, i));
2367 + i = num_meta_group_infos;
2370 + kfree(sbi->s_group_info[i]);
2371 + iput(sbi->s_buddy_cache);
2373 + kfree(sbi->s_group_info);
2377 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2379 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2380 + struct inode *root = sb->s_root->d_inode;
2381 + unsigned i, offset, max;
2382 + struct dentry *dentry;
2384 + if (!test_opt(sb, MBALLOC))
2387 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2389 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2390 + if (sbi->s_mb_offsets == NULL) {
2391 + clear_opt(sbi->s_mount_opt, MBALLOC);
2394 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2395 + if (sbi->s_mb_maxs == NULL) {
2396 + clear_opt(sbi->s_mount_opt, MBALLOC);
2397 + kfree(sbi->s_mb_maxs);
2401 + /* order 0 is regular bitmap */
2402 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2403 + sbi->s_mb_offsets[0] = 0;
2407 + max = sb->s_blocksize << 2;
2409 + sbi->s_mb_offsets[i] = offset;
2410 + sbi->s_mb_maxs[i] = max;
2411 + offset += 1 << (sb->s_blocksize_bits - i);
2414 + } while (i <= sb->s_blocksize_bits + 1);
2416 + /* init file for buddy data */
2417 + if ((i = ext3_mb_init_backend(sb))) {
2418 + clear_opt(sbi->s_mount_opt, MBALLOC);
2419 + kfree(sbi->s_mb_offsets);
2420 + kfree(sbi->s_mb_maxs);
2424 + spin_lock_init(&sbi->s_reserve_lock);
2425 + spin_lock_init(&sbi->s_md_lock);
2426 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2427 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2428 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2429 + spin_lock_init(&sbi->s_bal_lock);
2431 + /* remove old on-disk buddy file */
2432 + down(&root->i_sem);
2433 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
2434 + if (dentry->d_inode != NULL) {
2435 + i = vfs_unlink(root, dentry);
2437 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
2442 + ext3_mb_history_init(sb);
2444 + printk("EXT3-fs: mballoc enabled\n");
2448 +int ext3_mb_release(struct super_block *sb)
2450 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2451 + int i, num_meta_group_infos;
2453 + if (!test_opt(sb, MBALLOC))
2456 + /* release freed, non-committed blocks */
2457 + spin_lock(&sbi->s_md_lock);
2458 + list_splice_init(&sbi->s_closed_transaction,
2459 + &sbi->s_committed_transaction);
2460 + list_splice_init(&sbi->s_active_transaction,
2461 + &sbi->s_committed_transaction);
2462 + spin_unlock(&sbi->s_md_lock);
2463 + ext3_mb_free_committed_blocks(sb);
2465 + if (sbi->s_group_info) {
2466 + for (i = 0; i < sbi->s_groups_count; i++)
2467 + kfree(EXT3_GROUP_INFO(sb, i));
2468 + num_meta_group_infos = (sbi->s_groups_count +
2469 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2470 + EXT3_DESC_PER_BLOCK_BITS(sb);
2471 + for (i = 0; i < num_meta_group_infos; i++)
2472 + kfree(sbi->s_group_info[i]);
2473 + kfree(sbi->s_group_info);
2475 + if (sbi->s_mb_offsets)
2476 + kfree(sbi->s_mb_offsets);
2477 + if (sbi->s_mb_maxs)
2478 + kfree(sbi->s_mb_maxs);
2479 + if (sbi->s_buddy_cache)
2480 + iput(sbi->s_buddy_cache);
2481 + if (sbi->s_blocks_reserved)
2482 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
2483 + sbi->s_blocks_reserved);
2484 + if (ext3_mb_stats) {
2485 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2486 + atomic_read(&sbi->s_bal_allocated),
2487 + atomic_read(&sbi->s_bal_reqs),
2488 + atomic_read(&sbi->s_bal_success));
2489 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2490 + "%u 2^N hits, %u breaks\n",
2491 + atomic_read(&sbi->s_bal_ex_scanned),
2492 + atomic_read(&sbi->s_bal_goals),
2493 + atomic_read(&sbi->s_bal_2orders),
2494 + atomic_read(&sbi->s_bal_breaks));
2495 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2496 + sbi->s_mb_buddies_generated++,
2497 + sbi->s_mb_generation_time);
2500 + ext3_mb_history_release(sb);
2505 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2507 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2508 + int err, i, count = 0, count2 = 0;
2509 + struct ext3_free_metadata *md;
2510 + struct ext3_buddy e3b;
2512 + if (list_empty(&sbi->s_committed_transaction))
2515 + /* there is committed blocks to be freed yet */
2517 + /* get next array of blocks */
2519 + spin_lock(&sbi->s_md_lock);
2520 + if (!list_empty(&sbi->s_committed_transaction)) {
2521 + md = list_entry(sbi->s_committed_transaction.next,
2522 + struct ext3_free_metadata, list);
2523 + list_del(&md->list);
2525 + spin_unlock(&sbi->s_md_lock);
2530 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2531 + md->num, md->group, md);
2533 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2534 + /* we expect to find existing buddy because it's pinned */
2537 + /* there are blocks to put in buddy to make them really free */
2540 + ext3_lock_group(sb, md->group);
2541 + for (i = 0; i < md->num; i++) {
2542 + mb_debug(" %u", md->blocks[i]);
2543 + mb_free_blocks(&e3b, md->blocks[i], 1);
2546 + ext3_unlock_group(sb, md->group);
2548 + /* balance refcounts from ext3_mb_free_metadata() */
2549 + page_cache_release(e3b.bd_buddy_page);
2550 + page_cache_release(e3b.bd_bitmap_page);
2553 + ext3_mb_release_desc(&e3b);
2556 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2559 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
2561 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2563 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
2566 + /* new transaction! time to close last one and free blocks for
2567 + * committed transaction. we know that only transaction can be
2568 + * active, so previos transaction can be being logged and we
2569 + * know that transaction before previous is known to be already
2570 + * logged. this means that now we may free blocks freed in all
2571 + * transactions before previous one. hope I'm clear enough ... */
2573 + spin_lock(&sbi->s_md_lock);
2574 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
2575 + mb_debug("new transaction %lu, old %lu\n",
2576 + (unsigned long) handle->h_transaction->t_tid,
2577 + (unsigned long) sbi->s_last_transaction);
2578 + list_splice_init(&sbi->s_closed_transaction,
2579 + &sbi->s_committed_transaction);
2580 + list_splice_init(&sbi->s_active_transaction,
2581 + &sbi->s_closed_transaction);
2582 + sbi->s_last_transaction = handle->h_transaction->t_tid;
2584 + spin_unlock(&sbi->s_md_lock);
2586 + ext3_mb_free_committed_blocks(sb);
2589 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
2590 + int group, int block, int count)
2592 + struct ext3_group_info *db = e3b->bd_info;
2593 + struct super_block *sb = e3b->bd_sb;
2594 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2595 + struct ext3_free_metadata *md;
2598 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2599 + J_ASSERT(e3b->bd_buddy_page != NULL);
2601 + ext3_lock_group(sb, group);
2602 + for (i = 0; i < count; i++) {
2603 + md = db->bb_md_cur;
2604 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2605 + db->bb_md_cur = NULL;
2610 + ext3_unlock_group(sb, group);
2611 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2615 + md->group = group;
2617 + ext3_lock_group(sb, group);
2618 + if (db->bb_md_cur == NULL) {
2619 + spin_lock(&sbi->s_md_lock);
2620 + list_add(&md->list, &sbi->s_active_transaction);
2621 + spin_unlock(&sbi->s_md_lock);
2622 + /* protect buddy cache from being freed,
2623 + * otherwise we'll refresh it from
2624 + * on-disk bitmap and lose not-yet-available
2626 + page_cache_get(e3b->bd_buddy_page);
2627 + page_cache_get(e3b->bd_bitmap_page);
2628 + db->bb_md_cur = md;
2629 + db->bb_tid = handle->h_transaction->t_tid;
2630 + mb_debug("new md 0x%p for group %u\n",
2634 + md = db->bb_md_cur;
2638 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2639 + md->blocks[md->num] = block + i;
2641 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2642 + /* no more space, put full container on a sb's list */
2643 + db->bb_md_cur = NULL;
2646 + ext3_unlock_group(sb, group);
2650 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2651 + unsigned long block, unsigned long count,
2652 + int metadata, int *freed)
2654 + struct buffer_head *bitmap_bh = NULL;
2655 + struct ext3_group_desc *gdp;
2656 + struct ext3_super_block *es;
2657 + unsigned long bit, overflow;
2658 + struct buffer_head *gd_bh;
2659 + unsigned long block_group;
2660 + struct ext3_sb_info *sbi;
2661 + struct super_block *sb;
2662 + struct ext3_buddy e3b;
2668 + printk ("ext3_free_blocks: nonexistent device");
2672 + ext3_mb_poll_new_transaction(sb, handle);
2674 + sbi = EXT3_SB(sb);
2675 + es = EXT3_SB(sb)->s_es;
2676 + if (block < le32_to_cpu(es->s_first_data_block) ||
2677 + block + count < block ||
2678 + block + count > le32_to_cpu(es->s_blocks_count)) {
2679 + ext3_error (sb, "ext3_free_blocks",
2680 + "Freeing blocks not in datazone - "
2681 + "block = %lu, count = %lu", block, count);
2682 + goto error_return;
2685 + ext3_debug("freeing block %lu\n", block);
2689 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2690 + EXT3_BLOCKS_PER_GROUP(sb);
2691 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2692 + EXT3_BLOCKS_PER_GROUP(sb);
2694 + * Check to see if we are freeing blocks across a group
2697 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2698 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2699 + count -= overflow;
2701 + brelse(bitmap_bh);
2702 + bitmap_bh = read_block_bitmap(sb, block_group);
2704 + goto error_return;
2705 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2707 + goto error_return;
2709 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2710 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2711 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2712 + EXT3_SB(sb)->s_itb_per_group) ||
2713 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2714 + EXT3_SB(sb)->s_itb_per_group))
2715 + ext3_error (sb, "ext3_free_blocks",
2716 + "Freeing blocks in system zones - "
2717 + "Block = %lu, count = %lu",
2720 + BUFFER_TRACE(bitmap_bh, "getting write access");
2721 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2723 + goto error_return;
2726 + * We are about to modify some metadata. Call the journal APIs
2727 + * to unshare ->b_data if a currently-committing transaction is
2730 + BUFFER_TRACE(gd_bh, "get_write_access");
2731 + err = ext3_journal_get_write_access(handle, gd_bh);
2733 + goto error_return;
2735 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2737 + goto error_return;
2739 +#ifdef AGGRESSIVE_CHECK
2742 + for (i = 0; i < count; i++)
2743 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2746 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2748 + /* We dirtied the bitmap block */
2749 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2750 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2753 + /* blocks being freed are metadata. these blocks shouldn't
2754 + * be used until this transaction is committed */
2755 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2757 + ext3_lock_group(sb, block_group);
2758 + mb_free_blocks(&e3b, bit, count);
2759 + ext3_unlock_group(sb, block_group);
2762 + spin_lock(sb_bgl_lock(sbi, block_group));
2763 + gdp->bg_free_blocks_count =
2764 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2765 + spin_unlock(sb_bgl_lock(sbi, block_group));
2766 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2768 + ext3_mb_release_desc(&e3b);
2772 + /* And the group descriptor block */
2773 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2774 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2775 + if (!err) err = ret;
2777 + if (overflow && !err) {
2784 + brelse(bitmap_bh);
2785 + ext3_std_error(sb, err);
2789 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2791 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2792 + int free, ret = -ENOSPC;
2794 + BUG_ON(blocks < 0);
2795 + spin_lock(&sbi->s_reserve_lock);
2796 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2797 + if (blocks <= free - sbi->s_blocks_reserved) {
2798 + sbi->s_blocks_reserved += blocks;
2801 + spin_unlock(&sbi->s_reserve_lock);
2805 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2807 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2809 + BUG_ON(blocks < 0);
2810 + spin_lock(&sbi->s_reserve_lock);
2811 + sbi->s_blocks_reserved -= blocks;
2812 + WARN_ON(sbi->s_blocks_reserved < 0);
2813 + if (sbi->s_blocks_reserved < 0)
2814 + sbi->s_blocks_reserved = 0;
2815 + spin_unlock(&sbi->s_reserve_lock);
2818 +int ext3_new_block(handle_t *handle, struct inode *inode,
2819 + unsigned long goal, int *errp)
2823 + if (!test_opt(inode->i_sb, MBALLOC)) {
2824 + ret = ext3_new_block_old(handle, inode, goal, errp);
2828 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2834 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2835 + unsigned long block, unsigned long count, int metadata)
2837 + struct super_block *sb;
2841 + if (!test_opt(sb, MBALLOC) || !EXT3_SB(sb)->s_group_info)
2842 + ext3_free_blocks_old(handle, inode, block, count);
2844 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2846 + DQUOT_FREE_BLOCK(inode, freed);
2851 +#define EXT3_ROOT "ext3"
2852 +#define EXT3_MB_STATS_NAME "mb_stats"
2853 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2854 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2855 +#define EXT3_MB_ORDER2_REQ "mb_order2_req"
2857 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2858 + int count, int *eof, void *data)
2866 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2871 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2872 + unsigned long count, void *data)
2876 + if (count >= sizeof(str)) {
2877 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2878 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2879 + return -EOVERFLOW;
2882 + if (copy_from_user(str, buffer, count))
2885 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2886 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2890 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2891 + int count, int *eof, void *data)
2899 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2904 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2905 + unsigned long count, void *data)
2910 + if (count >= sizeof(str)) {
2911 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2912 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2913 + return -EOVERFLOW;
2916 + if (copy_from_user(str, buffer, count))
2919 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2920 + value = simple_strtol(str, NULL, 0);
2924 + ext3_mb_max_to_scan = value;
2929 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2930 + int count, int *eof, void *data)
2938 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2943 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2944 + unsigned long count, void *data)
2949 + if (count >= sizeof(str)) {
2950 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2951 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2952 + return -EOVERFLOW;
2955 + if (copy_from_user(str, buffer, count))
2958 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2959 + value = simple_strtol(str, NULL, 0);
2963 + ext3_mb_min_to_scan = value;
2968 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2969 + int count, int *eof, void *data)
2977 + len = sprintf(page, "%ld\n", ext3_mb_order2_reqs);
2982 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2983 + unsigned long count, void *data)
2988 + if (count >= sizeof(str)) {
2989 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2990 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2991 + return -EOVERFLOW;
2994 + if (copy_from_user(str, buffer, count))
2997 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2998 + value = simple_strtol(str, NULL, 0);
3002 + ext3_mb_order2_reqs = value;
3007 +int __init init_ext3_proc(void)
3009 + struct proc_dir_entry *proc_ext3_mb_stats;
3010 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
3011 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
3012 + struct proc_dir_entry *proc_ext3_mb_order2_req;
3014 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3015 + if (proc_root_ext3 == NULL) {
3016 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3020 + /* Initialize EXT3_MB_STATS_NAME */
3021 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
3022 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3023 + if (proc_ext3_mb_stats == NULL) {
3024 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3025 + EXT3_MB_STATS_NAME);
3026 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3030 + proc_ext3_mb_stats->data = NULL;
3031 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
3032 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
3034 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
3035 + proc_ext3_mb_max_to_scan = create_proc_entry(
3036 + EXT3_MB_MAX_TO_SCAN_NAME,
3037 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3038 + if (proc_ext3_mb_max_to_scan == NULL) {
3039 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3040 + EXT3_MB_MAX_TO_SCAN_NAME);
3041 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3042 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3046 + proc_ext3_mb_max_to_scan->data = NULL;
3047 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
3048 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
3050 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
3051 + proc_ext3_mb_min_to_scan = create_proc_entry(
3052 + EXT3_MB_MIN_TO_SCAN_NAME,
3053 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3054 + if (proc_ext3_mb_min_to_scan == NULL) {
3055 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3056 + EXT3_MB_MIN_TO_SCAN_NAME);
3057 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3058 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3059 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3063 + proc_ext3_mb_min_to_scan->data = NULL;
3064 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
3065 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
3067 + /* Initialize EXT3_ORDER2_REQ */
3068 + proc_ext3_mb_order2_req = create_proc_entry(
3069 + EXT3_MB_ORDER2_REQ,
3070 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
3071 + if (proc_ext3_mb_order2_req == NULL) {
3072 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n",
3073 + EXT3_MB_ORDER2_REQ);
3074 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3075 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3076 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3077 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3081 + proc_ext3_mb_order2_req->data = NULL;
3082 + proc_ext3_mb_order2_req->read_proc = ext3_mb_order2_req_read;
3083 + proc_ext3_mb_order2_req->write_proc = ext3_mb_order2_req_write;
3088 +void exit_ext3_proc(void)
3090 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
3091 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
3092 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
3093 + remove_proc_entry(EXT3_MB_ORDER2_REQ, proc_root_ext3);
3094 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3096 Index: linux-2.6.5-7.282-full/fs/ext3/Makefile
3097 ===================================================================
3098 --- linux-2.6.5-7.282-full.orig/fs/ext3/Makefile 2006-10-24 22:18:28.000000000 +0400
3099 +++ linux-2.6.5-7.282-full/fs/ext3/Makefile 2006-10-24 22:18:28.000000000 +0400
3100 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
3102 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
3103 ioctl.o namei.o super.o symlink.o hash.o \
3105 + extents.o mballoc.o
3107 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
3108 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o