1 Index: linux-stage/fs/ext3/mballoc.c
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-stage/fs/ext3/mballoc.c 2006-07-16 02:29:49.000000000 +0800
7 + * Copyright (c) 2003-2005, Cluster File Systems, Inc, info@clusterfs.com
8 + * Written by Alex Tomas <alex@clusterfs.com>
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License version 2 as
12 + * published by the Free Software Foundation.
14 + * This program is distributed in the hope that it will be useful,
15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 + * GNU General Public License for more details.
19 + * You should have received a copy of the GNU General Public Licens
20 + * along with this program; if not, write to the Free Software
21 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
26 + * mballoc.c contains the multiblocks allocation routines
29 +#include <linux/config.h>
30 +#include <linux/time.h>
31 +#include <linux/fs.h>
32 +#include <linux/namei.h>
33 +#include <linux/jbd.h>
34 +#include <linux/ext3_fs.h>
35 +#include <linux/ext3_jbd.h>
36 +#include <linux/quotaops.h>
37 +#include <linux/buffer_head.h>
38 +#include <linux/module.h>
39 +#include <linux/swap.h>
40 +#include <linux/proc_fs.h>
41 +#include <linux/pagemap.h>
42 +#include <linux/seq_file.h>
46 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
47 + * - track min/max extents in each group for better group selection
48 + * - mb_mark_used() may allocate chunk right after splitting buddy
49 + * - special flag to advice allocator to look for requested + N blocks
50 + * this may improve interaction between extents and mballoc
51 + * - tree of groups sorted by number of free blocks
52 + * - percpu reservation code (hotpath)
57 + * with AGRESSIVE_CHECK allocator runs consistency checks over
58 + * structures. these checks slow things down a lot
60 +#define AGGRESSIVE_CHECK__
66 +#define mb_debug(fmt,a...) printk(fmt, ##a)
68 +#define mb_debug(fmt,a...)
72 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
73 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
75 +#define EXT3_MB_HISTORY
78 + * How long mballoc can look for a best extent (in found extents)
80 +long ext3_mb_max_to_scan = 500;
83 + * How long mballoc must look for a best extent
85 +long ext3_mb_min_to_scan = 30;
88 + * with 'ext3_mb_stats' allocator will collect stats that will be
89 + * shown at umount. The collecting costs though!
92 +long ext3_mb_stats = 1;
94 +#ifdef EXT3_BB_MAX_BLOCKS
95 +#undef EXT3_BB_MAX_BLOCKS
97 +#define EXT3_BB_MAX_BLOCKS 30
99 +struct ext3_free_metadata {
100 + unsigned short group;
101 + unsigned short num;
102 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
103 + struct list_head list;
106 +struct ext3_group_info {
107 + unsigned long bb_state;
108 + unsigned long bb_tid;
109 + struct ext3_free_metadata *bb_md_cur;
110 + unsigned short bb_first_free;
111 + unsigned short bb_free;
112 + unsigned short bb_fragments;
113 + unsigned short bb_counters[];
117 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
118 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
120 +#define EXT3_MB_GRP_NEED_INIT(grp) \
121 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
123 +struct ext3_free_extent {
129 +struct ext3_allocation_context {
130 + struct super_block *ac_sb;
133 + struct ext3_free_extent ac_g_ex;
135 + /* the best found extent */
136 + struct ext3_free_extent ac_b_ex;
138 + /* number of iterations done. we have to track to limit searching */
139 + unsigned long ac_ex_scanned;
140 + __u16 ac_groups_scanned;
145 + __u8 ac_flags; /* allocation hints */
148 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
149 + * N > 0, the field stores N, otherwise 0 */
152 +#define AC_STATUS_CONTINUE 1
153 +#define AC_STATUS_FOUND 2
154 +#define AC_STATUS_BREAK 3
156 +struct ext3_mb_history {
157 + struct ext3_free_extent goal; /* goal allocation */
158 + struct ext3_free_extent result; /* result allocation */
159 + __u16 found; /* how many extents have been found */
160 + __u16 groups; /* how many groups have been scanned */
161 + __u16 tail; /* what tail broke some buddy */
162 + __u16 buddy; /* buddy the tail ^^^ broke */
163 + __u8 cr; /* which phase the result extent was found at */
168 + struct page *bd_buddy_page;
170 + struct page *bd_bitmap_page;
172 + struct ext3_group_info *bd_info;
173 + struct super_block *bd_sb;
177 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
178 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
180 +#ifndef EXT3_MB_HISTORY
181 +#define ext3_mb_store_history(sb,ac)
183 +static void ext3_mb_store_history(struct super_block *,
184 + struct ext3_allocation_context *ac);
187 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
189 +static struct proc_dir_entry *proc_root_ext3;
191 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
192 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
193 +int ext3_new_block_old(handle_t *, struct inode *, unsigned long, int *);
194 +int ext3_mb_reserve_blocks(struct super_block *, int);
195 +void ext3_mb_release_blocks(struct super_block *, int);
196 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
197 +void ext3_mb_free_committed_blocks(struct super_block *);
199 +#if BITS_PER_LONG == 64
200 +#define mb_correct_addr_and_bit(bit,addr) \
202 + bit += ((unsigned long) addr & 7UL) << 3; \
203 + addr = (void *) ((unsigned long) addr & ~7UL); \
205 +#elif BITS_PER_LONG == 32
206 +#define mb_correct_addr_and_bit(bit,addr) \
208 + bit += ((unsigned long) addr & 3UL) << 3; \
209 + addr = (void *) ((unsigned long) addr & ~3UL); \
212 +#error "how many bits you are?!"
215 +static inline int mb_test_bit(int bit, void *addr)
217 + mb_correct_addr_and_bit(bit,addr);
218 + return ext2_test_bit(bit, addr);
221 +static inline void mb_set_bit(int bit, void *addr)
223 + mb_correct_addr_and_bit(bit,addr);
224 + ext2_set_bit(bit, addr);
227 +static inline void mb_set_bit_atomic(int bit, void *addr)
229 + mb_correct_addr_and_bit(bit,addr);
230 + ext2_set_bit_atomic(NULL, bit, addr);
233 +static inline void mb_clear_bit(int bit, void *addr)
235 + mb_correct_addr_and_bit(bit,addr);
236 + ext2_clear_bit(bit, addr);
239 +static inline void mb_clear_bit_atomic(int bit, void *addr)
241 + mb_correct_addr_and_bit(bit,addr);
242 + ext2_clear_bit_atomic(NULL, bit, addr);
245 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
248 +#if BITS_PER_LONG == 64
249 + fix = ((unsigned long) addr & 7UL) << 3;
250 + addr = (void *) ((unsigned long) addr & ~7UL);
251 +#elif BITS_PER_LONG == 32
252 + fix = ((unsigned long) addr & 3UL) << 3;
253 + addr = (void *) ((unsigned long) addr & ~3UL);
255 +#error "how many bits you are?!"
259 + return ext2_find_next_zero_bit(addr, max, start) - fix;
262 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
266 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
267 + J_ASSERT(max != NULL);
269 + if (order > e3b->bd_blkbits + 1) {
274 + /* at order 0 we see each particular block */
275 + *max = 1 << (e3b->bd_blkbits + 3);
277 + return EXT3_MB_BITMAP(e3b);
279 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
280 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
285 +#ifdef AGGRESSIVE_CHECK
287 +static void mb_check_buddy(struct ext3_buddy *e3b)
289 + int order = e3b->bd_blkbits + 1;
290 + int max, max2, i, j, k, count;
291 + int fragments = 0, fstart;
292 + void *buddy, *buddy2;
294 + if (!test_opt(e3b->bd_sb, MBALLOC))
298 + static int mb_check_counter = 0;
299 + if (mb_check_counter++ % 300 != 0)
303 + while (order > 1) {
304 + buddy = mb_find_buddy(e3b, order, &max);
306 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
308 + J_ASSERT(buddy != buddy2);
309 + J_ASSERT(max * 2 == max2);
312 + for (i = 0; i < max; i++) {
314 + if (mb_test_bit(i, buddy)) {
315 + /* only single bit in buddy2 may be 1 */
316 + if (!mb_test_bit(i << 1, buddy2))
317 + J_ASSERT(mb_test_bit((i<<1)+1, buddy2));
318 + else if (!mb_test_bit((i << 1) + 1, buddy2))
319 + J_ASSERT(mb_test_bit(i << 1, buddy2));
323 + /* both bits in buddy2 must be 0 */
324 + J_ASSERT(mb_test_bit(i << 1, buddy2));
325 + J_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
327 + for (j = 0; j < (1 << order); j++) {
328 + k = (i * (1 << order)) + j;
329 + J_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
333 + J_ASSERT(e3b->bd_info->bb_counters[order] == count);
338 + buddy = mb_find_buddy(e3b, 0, &max);
339 + for (i = 0; i < max; i++) {
340 + if (!mb_test_bit(i, buddy)) {
341 + J_ASSERT(i >= e3b->bd_info->bb_first_free);
342 + if (fstart == -1) {
349 + /* check used bits only */
350 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
351 + buddy2 = mb_find_buddy(e3b, j, &max2);
353 + J_ASSERT(k < max2);
354 + J_ASSERT(mb_test_bit(k, buddy2));
357 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
358 + J_ASSERT(e3b->bd_info->bb_fragments == fragments);
362 +#define mb_check_buddy(e3b)
365 +/* find most significant bit */
366 +static int inline fmsb(unsigned short word)
380 + } while (word != 0);
386 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
387 + int len, struct ext3_group_info *grp)
389 + struct ext3_sb_info *sbi = EXT3_SB(sb);
390 + unsigned short min, max, chunk, border;
392 + mb_debug("mark %u/%u free\n", first, len);
393 + J_ASSERT(len < EXT3_BLOCKS_PER_GROUP(sb));
395 + border = 2 << sb->s_blocksize_bits;
398 + /* find how many blocks can be covered since this position */
399 + max = ffs(first | border) - 1;
401 + /* find how many blocks of power 2 we need to mark */
404 + mb_debug(" %u/%u -> max %u, min %u\n",
405 + first & ((2 << sb->s_blocksize_bits) - 1),
412 + /* mark multiblock chunks only */
413 + grp->bb_counters[min]++;
415 + mb_debug(" set %u at %u \n", first >> min,
416 + sbi->s_mb_offsets[min]);
417 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
426 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
427 + struct ext3_group_info *grp)
429 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
430 + unsigned short i = 0, first, len;
431 + unsigned free = 0, fragments = 0;
432 + unsigned long long period = get_cycles();
434 + i = mb_find_next_zero_bit(bitmap, max, 0);
435 + grp->bb_first_free = i;
439 + i = find_next_bit(bitmap, max, i);
443 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
445 + grp->bb_counters[0]++;
447 + i = mb_find_next_zero_bit(bitmap, max, i);
449 + grp->bb_fragments = fragments;
451 + /* bb_state shouldn't being modified because all
452 + * others waits for init completion on page lock */
453 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
454 + if (free != grp->bb_free) {
455 + printk("EXT3-fs: %u blocks in bitmap, %u in group descriptor\n",
456 + free, grp->bb_free);
457 + grp->bb_free = free;
460 + period = get_cycles() - period;
461 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
462 + EXT3_SB(sb)->s_mb_buddies_generated++;
463 + EXT3_SB(sb)->s_mb_generation_time += period;
464 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
467 +static int ext3_mb_init_cache(struct page *page)
469 + int blocksize, blocks_per_page, groups_per_page;
470 + int err = 0, i, first_group, first_block;
471 + struct super_block *sb;
472 + struct buffer_head *bhs;
473 + struct buffer_head **bh;
474 + struct inode *inode;
475 + char *data, *bitmap;
477 + mb_debug("init page %lu\n", page->index);
479 + inode = page->mapping->host;
481 + blocksize = 1 << inode->i_blkbits;
482 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
484 + groups_per_page = blocks_per_page >> 1;
485 + if (groups_per_page == 0)
486 + groups_per_page = 1;
488 + /* allocate buffer_heads to read bitmaps */
489 + if (groups_per_page > 1) {
491 + i = sizeof(struct buffer_head *) * groups_per_page;
492 + bh = kmalloc(i, GFP_NOFS);
499 + first_group = page->index * blocks_per_page / 2;
501 + /* read all groups the page covers into the cache */
502 + for (i = 0; i < groups_per_page; i++) {
503 + struct ext3_group_desc * desc;
505 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
509 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
514 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
518 + if (buffer_uptodate(bh[i]))
521 + lock_buffer(bh[i]);
522 + if (buffer_uptodate(bh[i])) {
523 + unlock_buffer(bh[i]);
528 + bh[i]->b_end_io = end_buffer_read_sync;
529 + submit_bh(READ, bh[i]);
530 + mb_debug("read bitmap for group %u\n", first_group + i);
533 + /* wait for I/O completion */
534 + for (i = 0; i < groups_per_page && bh[i]; i++)
535 + wait_on_buffer(bh[i]);
537 + /* XXX: I/O error handling here */
539 + first_block = page->index * blocks_per_page;
540 + for (i = 0; i < blocks_per_page; i++) {
543 + group = (first_block + i) >> 1;
544 + if (group >= EXT3_SB(sb)->s_groups_count)
547 + data = page_address(page) + (i * blocksize);
548 + bitmap = bh[group - first_group]->b_data;
550 + if ((first_block + i) & 1) {
551 + /* this is block of buddy */
552 + mb_debug("put buddy for group %u in page %lu/%x\n",
553 + group, page->index, i * blocksize);
554 + memset(data, 0xff, blocksize);
555 + EXT3_SB(sb)->s_group_info[group]->bb_fragments = 0;
556 + memset(EXT3_SB(sb)->s_group_info[group]->bb_counters, 0,
557 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
558 + ext3_mb_generate_buddy(sb, data, bitmap,
559 + EXT3_SB(sb)->s_group_info[group]);
561 + /* this is block of bitmap */
562 + mb_debug("put bitmap for group %u in page %lu/%x\n",
563 + group, page->index, i * blocksize);
564 + memcpy(data, bitmap, blocksize);
567 + SetPageUptodate(page);
570 + for (i = 0; i < groups_per_page && bh[i]; i++)
572 + if (bh && bh != &bhs)
577 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
578 + struct ext3_buddy *e3b)
580 + struct ext3_sb_info *sbi = EXT3_SB(sb);
581 + struct inode *inode = sbi->s_buddy_cache;
582 + int blocks_per_page, block, pnum, poff;
585 + mb_debug("load group %u\n", group);
587 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
589 + e3b->bd_blkbits = sb->s_blocksize_bits;
590 + e3b->bd_info = sbi->s_group_info[group];
592 + e3b->bd_group = group;
593 + e3b->bd_buddy_page = NULL;
594 + e3b->bd_bitmap_page = NULL;
597 + pnum = block / blocks_per_page;
598 + poff = block % blocks_per_page;
600 + page = find_get_page(inode->i_mapping, pnum);
601 + if (page == NULL || !PageUptodate(page)) {
603 + page_cache_release(page);
604 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
606 + if (!PageUptodate(page))
607 + ext3_mb_init_cache(page);
611 + if (page == NULL || !PageUptodate(page))
613 + e3b->bd_bitmap_page = page;
614 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
615 + mark_page_accessed(page);
618 + pnum = block / blocks_per_page;
619 + poff = block % blocks_per_page;
621 + page = find_get_page(inode->i_mapping, pnum);
622 + if (page == NULL || !PageUptodate(page)) {
624 + page_cache_release(page);
625 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
627 + if (!PageUptodate(page))
628 + ext3_mb_init_cache(page);
632 + if (page == NULL || !PageUptodate(page))
634 + e3b->bd_buddy_page = page;
635 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
636 + mark_page_accessed(page);
638 + J_ASSERT(e3b->bd_bitmap_page != NULL);
639 + J_ASSERT(e3b->bd_buddy_page != NULL);
644 + if (e3b->bd_bitmap_page)
645 + page_cache_release(e3b->bd_bitmap_page);
646 + if (e3b->bd_buddy_page)
647 + page_cache_release(e3b->bd_buddy_page);
648 + e3b->bd_buddy = NULL;
649 + e3b->bd_bitmap = NULL;
653 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
655 + if (e3b->bd_bitmap_page)
656 + page_cache_release(e3b->bd_bitmap_page);
657 + if (e3b->bd_buddy_page)
658 + page_cache_release(e3b->bd_buddy_page);
663 +ext3_lock_group(struct super_block *sb, int group)
665 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
666 + &EXT3_SB(sb)->s_group_info[group]->bb_state);
670 +ext3_unlock_group(struct super_block *sb, int group)
672 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
673 + &EXT3_SB(sb)->s_group_info[group]->bb_state);
676 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
681 + J_ASSERT(EXT3_MB_BITMAP(e3b) != EXT3_MB_BUDDY(e3b));
682 + J_ASSERT(block < (1 << (e3b->bd_blkbits + 3)));
684 + bb = EXT3_MB_BUDDY(e3b);
685 + while (order <= e3b->bd_blkbits + 1) {
686 + block = block >> 1;
687 + if (!mb_test_bit(block, bb)) {
688 + /* this block is part of buddy of order 'order' */
691 + bb += 1 << (e3b->bd_blkbits - order);
697 +static inline void mb_clear_bits(void *bm, int cur, int len)
702 + while (cur < len) {
703 + if ((cur & 31) == 0 && (len - cur) >= 32) {
704 + /* fast path: clear whole word at once */
705 + addr = bm + (cur >> 3);
710 + mb_clear_bit_atomic(cur, bm);
715 +static inline void mb_set_bits(void *bm, int cur, int len)
720 + while (cur < len) {
721 + if ((cur & 31) == 0 && (len - cur) >= 32) {
722 + /* fast path: clear whole word at once */
723 + addr = bm + (cur >> 3);
724 + *addr = 0xffffffff;
728 + mb_set_bit_atomic(cur, bm);
733 +static int mb_free_blocks(struct ext3_buddy *e3b, int first, int count)
735 + int block = 0, max = 0, order;
736 + void *buddy, *buddy2;
738 + mb_check_buddy(e3b);
740 + e3b->bd_info->bb_free += count;
741 + if (first < e3b->bd_info->bb_first_free)
742 + e3b->bd_info->bb_first_free = first;
744 + /* let's maintain fragments counter */
746 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
747 + if (first + count < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
748 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
750 + e3b->bd_info->bb_fragments--;
751 + else if (!block && !max)
752 + e3b->bd_info->bb_fragments++;
754 + /* let's maintain buddy itself */
755 + while (count-- > 0) {
759 + J_ASSERT(mb_test_bit(block, EXT3_MB_BITMAP(e3b)));
760 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
761 + e3b->bd_info->bb_counters[order]++;
763 + /* start of the buddy */
764 + buddy = mb_find_buddy(e3b, order, &max);
768 + if (mb_test_bit(block, buddy) ||
769 + mb_test_bit(block + 1, buddy))
772 + /* both the buddies are free, try to coalesce them */
773 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
779 + /* for special purposes, we don't set
780 + * free bits in bitmap */
781 + mb_set_bit(block, buddy);
782 + mb_set_bit(block + 1, buddy);
784 + e3b->bd_info->bb_counters[order]--;
785 + e3b->bd_info->bb_counters[order]--;
787 + block = block >> 1;
789 + e3b->bd_info->bb_counters[order]++;
791 + mb_clear_bit(block, buddy2);
795 + mb_check_buddy(e3b);
800 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
801 + int needed, struct ext3_free_extent *ex)
803 + int next, max, ord;
806 + J_ASSERT(ex != NULL);
808 + buddy = mb_find_buddy(e3b, order, &max);
810 + J_ASSERT(block < max);
811 + if (mb_test_bit(block, buddy)) {
818 + if (likely(order == 0)) {
819 + /* find actual order */
820 + order = mb_find_order_for_block(e3b, block);
821 + block = block >> order;
824 + ex->fe_len = 1 << order;
825 + ex->fe_start = block << order;
826 + ex->fe_group = e3b->bd_group;
828 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
830 + if (block + 1 >= max)
833 + next = (block + 1) * (1 << order);
834 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
837 + ord = mb_find_order_for_block(e3b, next);
840 + block = next >> order;
841 + ex->fe_len += 1 << order;
844 + J_ASSERT(ex->fe_start + ex->fe_len <= (1 << (e3b->bd_blkbits + 3)));
848 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
850 + int ord, mlen = 0, max = 0, cur;
851 + int start = ex->fe_start;
852 + int len = ex->fe_len;
857 + mb_check_buddy(e3b);
859 + e3b->bd_info->bb_free -= len;
860 + if (e3b->bd_info->bb_first_free == start)
861 + e3b->bd_info->bb_first_free += len;
863 + /* let's maintain fragments counter */
865 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
866 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
867 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
869 + e3b->bd_info->bb_fragments++;
870 + else if (!mlen && !max)
871 + e3b->bd_info->bb_fragments--;
873 + /* let's maintain buddy itself */
875 + ord = mb_find_order_for_block(e3b, start);
877 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
878 + /* the whole chunk may be allocated at once! */
880 + buddy = mb_find_buddy(e3b, ord, &max);
881 + J_ASSERT((start >> ord) < max);
882 + mb_set_bit(start >> ord, buddy);
883 + e3b->bd_info->bb_counters[ord]--;
886 + J_ASSERT(len >= 0);
890 + /* store for history */
892 + ret = len | (ord << 16);
894 + /* we have to split large buddy */
896 + buddy = mb_find_buddy(e3b, ord, &max);
897 + mb_set_bit(start >> ord, buddy);
898 + e3b->bd_info->bb_counters[ord]--;
901 + cur = (start >> ord) & ~1U;
902 + buddy = mb_find_buddy(e3b, ord, &max);
903 + mb_clear_bit(cur, buddy);
904 + mb_clear_bit(cur + 1, buddy);
905 + e3b->bd_info->bb_counters[ord]++;
906 + e3b->bd_info->bb_counters[ord]++;
909 + /* now drop all the bits in bitmap */
910 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
912 + mb_check_buddy(e3b);
918 + * Must be called under group lock!
920 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
921 + struct ext3_buddy *e3b)
925 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
926 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
928 + ac->ac_status = AC_STATUS_FOUND;
929 + ac->ac_tail = ret & 0xffff;
930 + ac->ac_buddy = ret >> 16;
934 + * The routine checks whether found extent is good enough. If it is,
935 + * then the extent gets marked used and flag is set to the context
936 + * to stop scanning. Otherwise, the extent is compared with the
937 + * previous found extent and if new one is better, then it's stored
938 + * in the context. Later, the best found extent will be used, if
939 + * mballoc can't find good enough extent.
941 + * FIXME: real allocation policy is to be designed yet!
943 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
944 + struct ext3_free_extent *ex,
945 + struct ext3_buddy *e3b)
947 + struct ext3_free_extent *bex = &ac->ac_b_ex;
948 + struct ext3_free_extent *gex = &ac->ac_g_ex;
950 + J_ASSERT(ex->fe_len > 0);
951 + J_ASSERT(ex->fe_len < (1 << ac->ac_sb->s_blocksize_bits) * 8);
952 + J_ASSERT(ex->fe_start < (1 << ac->ac_sb->s_blocksize_bits) * 8);
957 + * The special case - take what you catch first
959 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
961 + ext3_mb_use_best_found(ac, e3b);
966 + * Let's check whether the chuck is good enough
968 + if (ex->fe_len == gex->fe_len) {
970 + ext3_mb_use_best_found(ac, e3b);
975 + * If this is first found extent, just store it in the context
977 + if (bex->fe_len == 0) {
983 + * If new found extent is better, store it in the context
985 + if (bex->fe_len < gex->fe_len) {
986 + /* if the request isn't satisfied, any found extent
987 + * larger than previous best one is better */
988 + if (ex->fe_len > bex->fe_len)
990 + } else if (ex->fe_len > gex->fe_len) {
991 + /* if the request is satisfied, then we try to find
992 + * an extent that still satisfy the request, but is
993 + * smaller than previous one */
998 + * Let's scan at least few extents and don't pick up a first one
1000 + if (bex->fe_len > gex->fe_len && ac->ac_found > ext3_mb_min_to_scan)
1001 + ac->ac_status = AC_STATUS_BREAK;
1004 + * We don't want to scan for a whole year
1006 + if (ac->ac_found > ext3_mb_max_to_scan)
1007 + ac->ac_status = AC_STATUS_BREAK;
1010 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1011 + struct ext3_buddy *e3b)
1013 + struct ext3_free_extent ex = ac->ac_b_ex;
1014 + int group = ex.fe_group, max, err;
1016 + J_ASSERT(ex.fe_len > 0);
1017 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1021 + ext3_lock_group(ac->ac_sb, group);
1022 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1026 + ext3_mb_use_best_found(ac, e3b);
1029 + ext3_unlock_group(ac->ac_sb, group);
1031 + ext3_mb_release_desc(e3b);
1036 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1037 + struct ext3_buddy *e3b)
1039 + int group = ac->ac_g_ex.fe_group, max, err;
1040 + struct ext3_free_extent ex;
1042 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1046 + ext3_lock_group(ac->ac_sb, group);
1047 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1048 + ac->ac_g_ex.fe_len, &ex);
1051 + J_ASSERT(ex.fe_len > 0);
1052 + J_ASSERT(ex.fe_group == ac->ac_g_ex.fe_group);
1053 + J_ASSERT(ex.fe_start == ac->ac_g_ex.fe_start);
1056 + ext3_mb_use_best_found(ac, e3b);
1058 + ext3_unlock_group(ac->ac_sb, group);
1060 + ext3_mb_release_desc(e3b);
1066 + * The routine scans buddy structures (not bitmap!) from given order
1067 + * to max order and tries to find big enough chunk to satisfy the req
1069 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1070 + struct ext3_buddy *e3b)
1072 + struct super_block *sb = ac->ac_sb;
1073 + struct ext3_group_info *grp = e3b->bd_info;
1077 + J_ASSERT(ac->ac_2order > 0);
1078 + for (i = ac->ac_2order; i < sb->s_blocksize_bits + 1; i++) {
1079 + if (grp->bb_counters[i] == 0)
1082 + buddy = mb_find_buddy(e3b, i, &max);
1083 + if (buddy == NULL) {
1084 + printk(KERN_ALERT "looking for wrong order?\n");
1088 + k = mb_find_next_zero_bit(buddy, max, 0);
1089 + J_ASSERT(k < max);
1093 + ac->ac_b_ex.fe_len = 1 << i;
1094 + ac->ac_b_ex.fe_start = k << i;
1095 + ac->ac_b_ex.fe_group = e3b->bd_group;
1097 + ext3_mb_use_best_found(ac, e3b);
1098 + J_ASSERT(ac->ac_b_ex.fe_len == ac->ac_g_ex.fe_len);
1100 + if (unlikely(ext3_mb_stats))
1101 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1108 + * The routine scans the group and measures all found extents.
1109 + * In order to optimize scanning, caller must pass number of
1110 + * free blocks in the group, so the routine can know upper limit.
1112 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1113 + struct ext3_buddy *e3b)
1115 + struct super_block *sb = ac->ac_sb;
1116 + void *bitmap = EXT3_MB_BITMAP(e3b);
1117 + struct ext3_free_extent ex;
1120 + free = e3b->bd_info->bb_free;
1121 + J_ASSERT(free > 0);
1123 + i = e3b->bd_info->bb_first_free;
1125 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1126 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1127 + if (i >= sb->s_blocksize * 8) {
1128 + J_ASSERT(free == 0);
1132 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1133 + J_ASSERT(ex.fe_len > 0);
1134 + J_ASSERT(free >= ex.fe_len);
1136 + ext3_mb_measure_extent(ac, &ex, e3b);
1139 + free -= ex.fe_len;
1143 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1144 + int group, int cr)
1146 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1147 + struct ext3_group_info *grp = sbi->s_group_info[group];
1148 + unsigned free, fragments, i, bits;
1150 + J_ASSERT(cr >= 0 && cr < 4);
1151 + J_ASSERT(!EXT3_MB_GRP_NEED_INIT(grp));
1153 + free = grp->bb_free;
1154 + fragments = grp->bb_fragments;
1157 + if (fragments == 0)
1162 + J_ASSERT(ac->ac_2order != 0);
1163 + bits = ac->ac_sb->s_blocksize_bits + 1;
1164 + for (i = ac->ac_2order; i < bits; i++)
1165 + if (grp->bb_counters[i] > 0)
1168 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1171 + if (free >= ac->ac_g_ex.fe_len)
1182 +int ext3_mb_new_blocks(handle_t *handle, struct inode *inode,
1183 + unsigned long goal, int *len, int flags, int *errp)
1185 + struct buffer_head *bitmap_bh = NULL;
1186 + struct ext3_allocation_context ac;
1187 + int i, group, block, cr, err = 0;
1188 + struct ext3_group_desc *gdp;
1189 + struct ext3_super_block *es;
1190 + struct buffer_head *gdp_bh;
1191 + struct ext3_sb_info *sbi;
1192 + struct super_block *sb;
1193 + struct ext3_buddy e3b;
1195 + J_ASSERT(len != NULL);
1196 + J_ASSERT(*len > 0);
1200 + printk("ext3_mb_new_nblocks: nonexistent device");
1204 + if (!test_opt(sb, MBALLOC)) {
1205 + static int ext3_mballoc_warning = 0;
1206 + if (ext3_mballoc_warning == 0) {
1207 + printk(KERN_ERR "EXT3-fs: multiblock request with "
1208 + "mballoc disabled!\n");
1209 + ext3_mballoc_warning++;
1212 + err = ext3_new_block_old(handle, inode, goal, errp);
1216 + ext3_mb_poll_new_transaction(sb, handle);
1218 + sbi = EXT3_SB(sb);
1219 + es = EXT3_SB(sb)->s_es;
1222 + * We can't allocate > group size
1224 + if (*len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
1225 + *len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
1227 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1228 + /* someone asks for non-reserved blocks */
1230 + err = ext3_mb_reserve_blocks(sb, 1);
1238 + * Check quota for allocation of this blocks.
1240 + while (*len && DQUOT_ALLOC_BLOCK(inode, *len))
1248 + /* start searching from the goal */
1249 + if (goal < le32_to_cpu(es->s_first_data_block) ||
1250 + goal >= le32_to_cpu(es->s_blocks_count))
1251 + goal = le32_to_cpu(es->s_first_data_block);
1252 + group = (goal - le32_to_cpu(es->s_first_data_block)) /
1253 + EXT3_BLOCKS_PER_GROUP(sb);
1254 + block = ((goal - le32_to_cpu(es->s_first_data_block)) %
1255 + EXT3_BLOCKS_PER_GROUP(sb));
1257 + /* set up allocation goals */
1258 + ac.ac_b_ex.fe_group = 0;
1259 + ac.ac_b_ex.fe_start = 0;
1260 + ac.ac_b_ex.fe_len = 0;
1261 + ac.ac_status = AC_STATUS_CONTINUE;
1262 + ac.ac_groups_scanned = 0;
1263 + ac.ac_ex_scanned = 0;
1265 + ac.ac_sb = inode->i_sb;
1266 + ac.ac_g_ex.fe_group = group;
1267 + ac.ac_g_ex.fe_start = block;
1268 + ac.ac_g_ex.fe_len = *len;
1269 + ac.ac_flags = flags;
1271 + ac.ac_criteria = 0;
1273 + /* probably, the request is for 2^8+ blocks (1/2/3/... MB) */
1277 + if ((*len & (~(1 << i))) == 0)
1281 + /* Sometimes, caller may want to merge even small
1282 + * number of blocks to an existing extent */
1283 + if (ac.ac_flags & EXT3_MB_HINT_MERGE) {
1284 + err = ext3_mb_find_by_goal(&ac, &e3b);
1287 + if (ac.ac_status == AC_STATUS_FOUND)
1291 + /* Let's just scan groups to find more-less suitable blocks */
1292 + cr = ac.ac_2order ? 0 : 1;
1294 + for (; cr < 4 && ac.ac_status == AC_STATUS_CONTINUE; cr++) {
1295 + ac.ac_criteria = cr;
1296 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1297 + if (group == EXT3_SB(sb)->s_groups_count)
1300 + if (EXT3_MB_GRP_NEED_INIT(sbi->s_group_info[group])) {
1301 + /* we need full data about the group
1302 + * to make a good selection */
1303 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1306 + ext3_mb_release_desc(&e3b);
1309 + /* check is group good for our criteries */
1310 + if (!ext3_mb_good_group(&ac, group, cr))
1313 + err = ext3_mb_load_buddy(ac.ac_sb, group, &e3b);
1317 + ext3_lock_group(sb, group);
1318 + if (!ext3_mb_good_group(&ac, group, cr)) {
1319 + /* someone did allocation from this group */
1320 + ext3_unlock_group(sb, group);
1321 + ext3_mb_release_desc(&e3b);
1325 + ac.ac_groups_scanned++;
1327 + ext3_mb_simple_scan_group(&ac, &e3b);
1329 + ext3_mb_complex_scan_group(&ac, &e3b);
1331 + ext3_unlock_group(sb, group);
1333 + ext3_mb_release_desc(&e3b);
1337 + if (ac.ac_status != AC_STATUS_CONTINUE)
1342 + if (ac.ac_b_ex.fe_len > 0 && ac.ac_status != AC_STATUS_FOUND &&
1343 + !(ac.ac_flags & EXT3_MB_HINT_FIRST)) {
1345 + * We've been searching too long. Let's try to allocate
1346 + * the best chunk we've found so far
1349 + /*if (ac.ac_found > ext3_mb_max_to_scan)
1350 + printk(KERN_ERR "EXT3-fs: too long searching at "
1351 + "%u (%d/%d)\n", cr, ac.ac_b_ex.fe_len,
1352 + ac.ac_g_ex.fe_len);*/
1353 + ext3_mb_try_best_found(&ac, &e3b);
1354 + if (ac.ac_status != AC_STATUS_FOUND) {
1356 + * Someone more lucky has already allocated it.
1357 + * The only thing we can do is just take first
1359 + printk(KERN_ERR "EXT3-fs: and someone won our chunk\n");
1361 + ac.ac_b_ex.fe_group = 0;
1362 + ac.ac_b_ex.fe_start = 0;
1363 + ac.ac_b_ex.fe_len = 0;
1364 + ac.ac_status = AC_STATUS_CONTINUE;
1365 + ac.ac_flags |= EXT3_MB_HINT_FIRST;
1371 + if (ac.ac_status != AC_STATUS_FOUND) {
1373 + * We aren't lucky definitely
1375 + DQUOT_FREE_BLOCK(inode, *len);
1379 + printk(KERN_ERR "EXT3-fs: cant allocate: status %d, flags %d\n",
1380 + ac.ac_status, ac.ac_flags);
1381 + printk(KERN_ERR "EXT3-fs: goal %d, best found %d/%d/%d, cr %d\n",
1382 + ac.ac_g_ex.fe_len, ac.ac_b_ex.fe_group,
1383 + ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len, cr);
1384 + printk(KERN_ERR "EXT3-fs: %lu block reserved, %d found\n",
1385 + sbi->s_blocks_reserved, ac.ac_found);
1386 + printk("EXT3-fs: groups: ");
1387 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++)
1388 + printk("%d: %d ", i,
1389 + sbi->s_group_info[i]->bb_free);
1396 + J_ASSERT(ac.ac_b_ex.fe_len > 0);
1398 + /* good news - free block(s) have been found. now it's time
1399 + * to mark block(s) in good old journaled bitmap */
1400 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1401 + + ac.ac_b_ex.fe_start
1402 + + le32_to_cpu(es->s_first_data_block);
1404 + /* we made a desicion, now mark found blocks in good old
1405 + * bitmap to be journaled */
1407 + ext3_debug("using block group %d(%d)\n",
1408 + ac.ac_b_group.group, gdp->bg_free_blocks_count);
1410 + bitmap_bh = read_block_bitmap(sb, ac.ac_b_ex.fe_group);
1416 + err = ext3_journal_get_write_access(handle, bitmap_bh);
1422 + gdp = ext3_get_group_desc(sb, ac.ac_b_ex.fe_group, &gdp_bh);
1428 + err = ext3_journal_get_write_access(handle, gdp_bh);
1432 + block = ac.ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
1433 + + ac.ac_b_ex.fe_start
1434 + + le32_to_cpu(es->s_first_data_block);
1436 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
1437 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
1438 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
1439 + EXT3_SB(sb)->s_itb_per_group))
1440 + ext3_error(sb, "ext3_new_block",
1441 + "Allocating block in system zone - "
1442 + "block = %u", block);
1443 +#ifdef AGGRESSIVE_CHECK
1444 + for (i = 0; i < ac.ac_b_ex.fe_len; i++)
1445 + J_ASSERT(!mb_test_bit(ac.ac_b_ex.fe_start + i, bitmap_bh->b_data));
1447 + mb_set_bits(bitmap_bh->b_data, ac.ac_b_ex.fe_start, ac.ac_b_ex.fe_len);
1449 + spin_lock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1450 + gdp->bg_free_blocks_count =
1451 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
1452 + - ac.ac_b_ex.fe_len);
1453 + spin_unlock(sb_bgl_lock(sbi, ac.ac_b_ex.fe_group));
1454 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac.ac_b_ex.fe_len);
1456 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
1459 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
1465 + brelse(bitmap_bh);
1467 + /* drop non-allocated, but dquote'd blocks */
1468 + J_ASSERT(*len >= ac.ac_b_ex.fe_len);
1469 + DQUOT_FREE_BLOCK(inode, *len - ac.ac_b_ex.fe_len);
1471 + *len = ac.ac_b_ex.fe_len;
1472 + J_ASSERT(*len > 0);
1473 + J_ASSERT(block != 0);
1477 + /* if we've already allocated something, roll it back */
1478 + if (ac.ac_status == AC_STATUS_FOUND) {
1479 + /* FIXME: free blocks here */
1482 + DQUOT_FREE_BLOCK(inode, *len);
1483 + brelse(bitmap_bh);
1487 + if (!(flags & EXT3_MB_HINT_RESERVED)) {
1488 + /* block wasn't reserved before and we reserved it
1489 + * at the beginning of allocation. it doesn't matter
1490 + * whether we allocated anything or we failed: time
1491 + * to release reservation. NOTE: because I expect
1492 + * any multiblock request from delayed allocation
1493 + * path only, here is single block always */
1494 + ext3_mb_release_blocks(sb, 1);
1497 + if (unlikely(ext3_mb_stats) && ac.ac_g_ex.fe_len > 1) {
1498 + atomic_inc(&sbi->s_bal_reqs);
1499 + atomic_add(*len, &sbi->s_bal_allocated);
1500 + if (*len >= ac.ac_g_ex.fe_len)
1501 + atomic_inc(&sbi->s_bal_success);
1502 + atomic_add(ac.ac_found, &sbi->s_bal_ex_scanned);
1503 + if (ac.ac_g_ex.fe_start == ac.ac_b_ex.fe_start &&
1504 + ac.ac_g_ex.fe_group == ac.ac_b_ex.fe_group)
1505 + atomic_inc(&sbi->s_bal_goals);
1506 + if (ac.ac_found > ext3_mb_max_to_scan)
1507 + atomic_inc(&sbi->s_bal_breaks);
1510 + ext3_mb_store_history(sb, &ac);
1514 +EXPORT_SYMBOL(ext3_mb_new_blocks);
1516 +#ifdef EXT3_MB_HISTORY
1517 +struct ext3_mb_proc_session {
1518 + struct ext3_mb_history *history;
1519 + struct super_block *sb;
1524 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
1525 + struct ext3_mb_history *hs,
1528 + if (hs == s->history + s->max)
1530 + if (!first && hs == s->history + s->start)
1532 + while (hs->goal.fe_len == 0) {
1534 + if (hs == s->history + s->max)
1536 + if (hs == s->history + s->start)
1542 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
1544 + struct ext3_mb_proc_session *s = seq->private;
1545 + struct ext3_mb_history *hs;
1549 + return SEQ_START_TOKEN;
1550 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
1553 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
1557 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
1559 + struct ext3_mb_proc_session *s = seq->private;
1560 + struct ext3_mb_history *hs = v;
1563 + if (v == SEQ_START_TOKEN)
1564 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
1566 + return ext3_mb_history_skip_empty(s, ++hs, 0);
1569 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
1571 + struct ext3_mb_history *hs = v;
1572 + char buf[20], buf2[20];
1574 + if (v == SEQ_START_TOKEN) {
1575 + seq_printf(seq, "%-17s %-17s %-5s %-5s %-2s %-5s %-5s %-6s\n",
1576 + "goal", "result", "found", "grps", "cr", "merge",
1577 + "tail", "broken");
1581 + sprintf(buf, "%u/%u/%u", hs->goal.fe_group,
1582 + hs->goal.fe_start, hs->goal.fe_len);
1583 + sprintf(buf2, "%u/%u/%u", hs->result.fe_group,
1584 + hs->result.fe_start, hs->result.fe_len);
1585 + seq_printf(seq, "%-17s %-17s %-5u %-5u %-2u %-5s %-5u %-6u\n", buf,
1586 + buf2, hs->found, hs->groups, hs->cr,
1587 + hs->merged ? "M" : "", hs->tail,
1588 + hs->buddy ? 1 << hs->buddy : 0);
1592 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
1596 +static struct seq_operations ext3_mb_seq_history_ops = {
1597 + .start = ext3_mb_seq_history_start,
1598 + .next = ext3_mb_seq_history_next,
1599 + .stop = ext3_mb_seq_history_stop,
1600 + .show = ext3_mb_seq_history_show,
1603 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
1605 + struct super_block *sb = PDE(inode)->data;
1606 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1607 + struct ext3_mb_proc_session *s;
1610 + s = kmalloc(sizeof(*s), GFP_KERNEL);
1613 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
1614 + s->history = kmalloc(size, GFP_KERNEL);
1620 + spin_lock(&sbi->s_mb_history_lock);
1621 + memcpy(s->history, sbi->s_mb_history, size);
1622 + s->max = sbi->s_mb_history_max;
1623 + s->start = sbi->s_mb_history_cur % s->max;
1624 + spin_unlock(&sbi->s_mb_history_lock);
1626 + rc = seq_open(file, &ext3_mb_seq_history_ops);
1628 + struct seq_file *m = (struct seq_file *)file->private_data;
1631 + kfree(s->history);
1638 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
1640 + struct seq_file *seq = (struct seq_file *)file->private_data;
1641 + struct ext3_mb_proc_session *s = seq->private;
1642 + kfree(s->history);
1644 + return seq_release(inode, file);
1647 +static struct file_operations ext3_mb_seq_history_fops = {
1648 + .owner = THIS_MODULE,
1649 + .open = ext3_mb_seq_history_open,
1651 + .llseek = seq_lseek,
1652 + .release = ext3_mb_seq_history_release,
1655 +static void ext3_mb_history_release(struct super_block *sb)
1657 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1660 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
1661 + remove_proc_entry("mb_history", sbi->s_mb_proc);
1662 + remove_proc_entry(name, proc_root_ext3);
1664 + if (sbi->s_mb_history)
1665 + kfree(sbi->s_mb_history);
1668 +static void ext3_mb_history_init(struct super_block *sb)
1670 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1674 + snprintf(name, sizeof(name) - 1, "%s", bdevname(sb->s_bdev, name));
1675 + sbi->s_mb_proc = proc_mkdir(name, proc_root_ext3);
1676 + if (sbi->s_mb_proc != NULL) {
1677 + struct proc_dir_entry *p;
1678 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
1680 + p->proc_fops = &ext3_mb_seq_history_fops;
1685 + sbi->s_mb_history_max = 1000;
1686 + sbi->s_mb_history_cur = 0;
1687 + spin_lock_init(&sbi->s_mb_history_lock);
1688 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
1689 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
1690 + memset(sbi->s_mb_history, 0, i);
1691 + /* if we can't allocate history, then we simple won't use it */
1695 +ext3_mb_store_history(struct super_block *sb, struct ext3_allocation_context *ac)
1697 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1698 + struct ext3_mb_history h;
1700 + if (likely(sbi->s_mb_history == NULL))
1703 + h.goal = ac->ac_g_ex;
1704 + h.result = ac->ac_b_ex;
1705 + h.found = ac->ac_found;
1706 + h.cr = ac->ac_criteria;
1707 + h.groups = ac->ac_groups_scanned;
1708 + h.tail = ac->ac_tail;
1709 + h.buddy = ac->ac_buddy;
1711 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
1712 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
1715 + spin_lock(&sbi->s_mb_history_lock);
1716 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
1717 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
1718 + sbi->s_mb_history_cur = 0;
1719 + spin_unlock(&sbi->s_mb_history_lock);
1723 +#define ext3_mb_history_release(sb)
1724 +#define ext3_mb_history_init(sb)
1727 +int ext3_mb_init_backend(struct super_block *sb)
1729 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1732 + len = sizeof(struct ext3_buddy_group_blocks *) * sbi->s_groups_count;
1733 + sbi->s_group_info = kmalloc(len, GFP_KERNEL);
1734 + if (sbi->s_group_info == NULL) {
1735 + printk(KERN_ERR "EXT3-fs: can't allocate mem for buddy\n");
1738 + memset(sbi->s_group_info, 0, len);
1740 + sbi->s_buddy_cache = new_inode(sb);
1741 + if (sbi->s_buddy_cache == NULL) {
1742 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
1743 + kfree(sbi->s_group_info);
1748 + * calculate needed size. if change bb_counters size,
1749 + * don't forget about ext3_mb_generate_buddy()
1751 + len = sizeof(struct ext3_group_info);
1752 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
1753 + for (i = 0; i < sbi->s_groups_count; i++) {
1754 + struct ext3_group_desc * desc;
1756 + sbi->s_group_info[i] = kmalloc(len, GFP_KERNEL);
1757 + if (sbi->s_group_info[i] == NULL) {
1758 + printk(KERN_ERR "EXT3-fs: cant allocate mem for buddy\n");
1761 + desc = ext3_get_group_desc(sb, i, NULL);
1762 + if (desc == NULL) {
1763 + printk(KERN_ERR "EXT3-fs: cant read descriptor %u\n", i);
1766 + memset(sbi->s_group_info[i], 0, len);
1767 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
1768 + &sbi->s_group_info[i]->bb_state);
1769 + sbi->s_group_info[i]->bb_free =
1770 + le16_to_cpu(desc->bg_free_blocks_count);
1777 + kfree(sbi->s_group_info[i]);
1778 + iput(sbi->s_buddy_cache);
1783 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
1785 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1786 + struct inode *root = sb->s_root->d_inode;
1787 + unsigned i, offset, max;
1788 + struct dentry *dentry;
1790 + if (!test_opt(sb, MBALLOC))
1793 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
1795 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
1796 + if (sbi->s_mb_offsets == NULL) {
1797 + clear_opt(sbi->s_mount_opt, MBALLOC);
1800 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
1801 + if (sbi->s_mb_maxs == NULL) {
1802 + clear_opt(sbi->s_mount_opt, MBALLOC);
1803 + kfree(sbi->s_mb_maxs);
1807 + /* order 0 is regular bitmap */
1808 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
1809 + sbi->s_mb_offsets[0] = 0;
1813 + max = sb->s_blocksize << 2;
1815 + sbi->s_mb_offsets[i] = offset;
1816 + sbi->s_mb_maxs[i] = max;
1817 + offset += 1 << (sb->s_blocksize_bits - i);
1820 + } while (i <= sb->s_blocksize_bits + 1);
1823 + /* init file for buddy data */
1824 + if ((i = ext3_mb_init_backend(sb))) {
1825 + clear_opt(sbi->s_mount_opt, MBALLOC);
1826 + kfree(sbi->s_mb_offsets);
1827 + kfree(sbi->s_mb_maxs);
1831 + spin_lock_init(&sbi->s_reserve_lock);
1832 + spin_lock_init(&sbi->s_md_lock);
1833 + INIT_LIST_HEAD(&sbi->s_active_transaction);
1834 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
1835 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
1836 + spin_lock_init(&sbi->s_bal_lock);
1838 + /* remove old on-disk buddy file */
1839 + mutex_lock(&root->i_mutex);
1840 + dentry = lookup_one_len(".buddy", sb->s_root, strlen(".buddy"));
1841 + if (dentry->d_inode != NULL) {
1842 + i = vfs_unlink(root, dentry);
1844 + printk("EXT3-fs: can't remove .buddy file: %d\n", i);
1847 + mutex_unlock(&root->i_mutex);
1849 + ext3_mb_history_init(sb);
1851 + printk("EXT3-fs: mballoc enabled\n");
1855 +int ext3_mb_release(struct super_block *sb)
1857 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1860 + if (!test_opt(sb, MBALLOC))
1863 + /* release freed, non-committed blocks */
1864 + spin_lock(&sbi->s_md_lock);
1865 + list_splice_init(&sbi->s_closed_transaction,
1866 + &sbi->s_committed_transaction);
1867 + list_splice_init(&sbi->s_active_transaction,
1868 + &sbi->s_committed_transaction);
1869 + spin_unlock(&sbi->s_md_lock);
1870 + ext3_mb_free_committed_blocks(sb);
1872 + if (sbi->s_group_info) {
1873 + for (i = 0; i < sbi->s_groups_count; i++) {
1874 + if (sbi->s_group_info[i] == NULL)
1876 + kfree(sbi->s_group_info[i]);
1878 + kfree(sbi->s_group_info);
1880 + if (sbi->s_mb_offsets)
1881 + kfree(sbi->s_mb_offsets);
1882 + if (sbi->s_mb_maxs)
1883 + kfree(sbi->s_mb_maxs);
1884 + if (sbi->s_buddy_cache)
1885 + iput(sbi->s_buddy_cache);
1886 + if (sbi->s_blocks_reserved)
1887 + printk("ext3-fs: %ld blocks being reserved at umount!\n",
1888 + sbi->s_blocks_reserved);
1889 + if (ext3_mb_stats) {
1890 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
1891 + atomic_read(&sbi->s_bal_allocated),
1892 + atomic_read(&sbi->s_bal_reqs),
1893 + atomic_read(&sbi->s_bal_success));
1894 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
1895 + "%u 2^N hits, %u breaks\n",
1896 + atomic_read(&sbi->s_bal_ex_scanned),
1897 + atomic_read(&sbi->s_bal_goals),
1898 + atomic_read(&sbi->s_bal_2orders),
1899 + atomic_read(&sbi->s_bal_breaks));
1900 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
1901 + sbi->s_mb_buddies_generated++,
1902 + sbi->s_mb_generation_time);
1905 + ext3_mb_history_release(sb);
1910 +void ext3_mb_free_committed_blocks(struct super_block *sb)
1912 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1913 + int err, i, count = 0, count2 = 0;
1914 + struct ext3_free_metadata *md;
1915 + struct ext3_buddy e3b;
1917 + if (list_empty(&sbi->s_committed_transaction))
1920 + /* there is committed blocks to be freed yet */
1922 + /* get next array of blocks */
1924 + spin_lock(&sbi->s_md_lock);
1925 + if (!list_empty(&sbi->s_committed_transaction)) {
1926 + md = list_entry(sbi->s_committed_transaction.next,
1927 + struct ext3_free_metadata, list);
1928 + list_del(&md->list);
1930 + spin_unlock(&sbi->s_md_lock);
1935 + mb_debug("gonna free %u blocks in group %u (0x%p):",
1936 + md->num, md->group, md);
1938 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
1941 + /* there are blocks to put in buddy to make them really free */
1944 + ext3_lock_group(sb, md->group);
1945 + for (i = 0; i < md->num; i++) {
1946 + mb_debug(" %u", md->blocks[i]);
1947 + mb_free_blocks(&e3b, md->blocks[i], 1);
1950 + ext3_unlock_group(sb, md->group);
1952 + /* balance refcounts from ext3_mb_free_metadata() */
1953 + page_cache_release(e3b.bd_buddy_page);
1954 + page_cache_release(e3b.bd_bitmap_page);
1957 + ext3_mb_release_desc(&e3b);
1960 + mb_debug("freed %u blocks in %u structures\n", count, count2);
1963 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
1965 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1967 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
1970 + /* new transaction! time to close last one and free blocks for
1971 + * committed transaction. we know that only transaction can be
1972 + * active, so previos transaction can be being logged and we
1973 + * know that transaction before previous is known to be already
1974 + * logged. this means that now we may free blocks freed in all
1975 + * transactions before previous one. hope I'm clear enough ... */
1977 + spin_lock(&sbi->s_md_lock);
1978 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
1979 + mb_debug("new transaction %lu, old %lu\n",
1980 + (unsigned long) handle->h_transaction->t_tid,
1981 + (unsigned long) sbi->s_last_transaction);
1982 + list_splice_init(&sbi->s_closed_transaction,
1983 + &sbi->s_committed_transaction);
1984 + list_splice_init(&sbi->s_active_transaction,
1985 + &sbi->s_closed_transaction);
1986 + sbi->s_last_transaction = handle->h_transaction->t_tid;
1988 + spin_unlock(&sbi->s_md_lock);
1990 + ext3_mb_free_committed_blocks(sb);
1993 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
1994 + int group, int block, int count)
1996 + struct ext3_group_info *db = e3b->bd_info;
1997 + struct super_block *sb = e3b->bd_sb;
1998 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1999 + struct ext3_free_metadata *md;
2002 + J_ASSERT(e3b->bd_bitmap_page != NULL);
2003 + J_ASSERT(e3b->bd_buddy_page != NULL);
2005 + ext3_lock_group(sb, group);
2006 + for (i = 0; i < count; i++) {
2007 + md = db->bb_md_cur;
2008 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
2009 + db->bb_md_cur = NULL;
2014 + ext3_unlock_group(sb, group);
2015 + md = kmalloc(sizeof(*md), GFP_KERNEL);
2019 + md->group = group;
2021 + ext3_lock_group(sb, group);
2022 + if (db->bb_md_cur == NULL) {
2023 + spin_lock(&sbi->s_md_lock);
2024 + list_add(&md->list, &sbi->s_active_transaction);
2025 + spin_unlock(&sbi->s_md_lock);
2026 + /* protect buddy cache from being freed,
2027 + * otherwise we'll refresh it from
2028 + * on-disk bitmap and lose not-yet-available
2030 + page_cache_get(e3b->bd_buddy_page);
2031 + page_cache_get(e3b->bd_bitmap_page);
2032 + db->bb_md_cur = md;
2033 + db->bb_tid = handle->h_transaction->t_tid;
2034 + mb_debug("new md 0x%p for group %u\n",
2038 + md = db->bb_md_cur;
2042 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
2043 + md->blocks[md->num] = block + i;
2045 + if (md->num == EXT3_BB_MAX_BLOCKS) {
2046 + /* no more space, put full container on a sb's list */
2047 + db->bb_md_cur = NULL;
2050 + ext3_unlock_group(sb, group);
2054 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
2055 + unsigned long block, unsigned long count,
2056 + int metadata, int *freed)
2058 + struct buffer_head *bitmap_bh = NULL;
2059 + struct ext3_group_desc *gdp;
2060 + struct ext3_super_block *es;
2061 + unsigned long bit, overflow;
2062 + struct buffer_head *gd_bh;
2063 + unsigned long block_group;
2064 + struct ext3_sb_info *sbi;
2065 + struct super_block *sb;
2066 + struct ext3_buddy e3b;
2072 + printk ("ext3_free_blocks: nonexistent device");
2076 + ext3_mb_poll_new_transaction(sb, handle);
2078 + sbi = EXT3_SB(sb);
2079 + es = EXT3_SB(sb)->s_es;
2080 + if (block < le32_to_cpu(es->s_first_data_block) ||
2081 + block + count < block ||
2082 + block + count > le32_to_cpu(es->s_blocks_count)) {
2083 + ext3_error (sb, "ext3_free_blocks",
2084 + "Freeing blocks not in datazone - "
2085 + "block = %lu, count = %lu", block, count);
2086 + goto error_return;
2089 + ext3_debug("freeing block %lu\n", block);
2093 + block_group = (block - le32_to_cpu(es->s_first_data_block)) /
2094 + EXT3_BLOCKS_PER_GROUP(sb);
2095 + bit = (block - le32_to_cpu(es->s_first_data_block)) %
2096 + EXT3_BLOCKS_PER_GROUP(sb);
2098 + * Check to see if we are freeing blocks across a group
2101 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
2102 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
2103 + count -= overflow;
2105 + brelse(bitmap_bh);
2106 + bitmap_bh = read_block_bitmap(sb, block_group);
2108 + goto error_return;
2109 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
2111 + goto error_return;
2113 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
2114 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
2115 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
2116 + EXT3_SB(sb)->s_itb_per_group) ||
2117 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
2118 + EXT3_SB(sb)->s_itb_per_group))
2119 + ext3_error (sb, "ext3_free_blocks",
2120 + "Freeing blocks in system zones - "
2121 + "Block = %lu, count = %lu",
2124 + BUFFER_TRACE(bitmap_bh, "getting write access");
2125 + err = ext3_journal_get_write_access(handle, bitmap_bh);
2127 + goto error_return;
2130 + * We are about to modify some metadata. Call the journal APIs
2131 + * to unshare ->b_data if a currently-committing transaction is
2134 + BUFFER_TRACE(gd_bh, "get_write_access");
2135 + err = ext3_journal_get_write_access(handle, gd_bh);
2137 + goto error_return;
2139 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
2141 + goto error_return;
2143 +#ifdef AGGRESSIVE_CHECK
2146 + for (i = 0; i < count; i++)
2147 + J_ASSERT(mb_test_bit(bit + i, bitmap_bh->b_data));
2150 + mb_clear_bits(bitmap_bh->b_data, bit, count);
2152 + /* We dirtied the bitmap block */
2153 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
2154 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
2157 + /* blocks being freed are metadata. these blocks shouldn't
2158 + * be used until this transaction is committed */
2159 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
2161 + ext3_lock_group(sb, block_group);
2162 + mb_free_blocks(&e3b, bit, count);
2163 + ext3_unlock_group(sb, block_group);
2166 + spin_lock(sb_bgl_lock(sbi, block_group));
2167 + gdp->bg_free_blocks_count =
2168 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
2169 + spin_unlock(sb_bgl_lock(sbi, block_group));
2170 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
2172 + ext3_mb_release_desc(&e3b);
2176 + /* And the group descriptor block */
2177 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
2178 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
2179 + if (!err) err = ret;
2181 + if (overflow && !err) {
2188 + brelse(bitmap_bh);
2189 + ext3_std_error(sb, err);
2193 +int ext3_mb_reserve_blocks(struct super_block *sb, int blocks)
2195 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2196 + int free, ret = -ENOSPC;
2198 + BUG_ON(blocks < 0);
2199 + spin_lock(&sbi->s_reserve_lock);
2200 + free = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2201 + if (blocks <= free - sbi->s_blocks_reserved) {
2202 + sbi->s_blocks_reserved += blocks;
2205 + spin_unlock(&sbi->s_reserve_lock);
2209 +void ext3_mb_release_blocks(struct super_block *sb, int blocks)
2211 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2213 + BUG_ON(blocks < 0);
2214 + spin_lock(&sbi->s_reserve_lock);
2215 + sbi->s_blocks_reserved -= blocks;
2216 + WARN_ON(sbi->s_blocks_reserved < 0);
2217 + if (sbi->s_blocks_reserved < 0)
2218 + sbi->s_blocks_reserved = 0;
2219 + spin_unlock(&sbi->s_reserve_lock);
2222 +int ext3_new_block(handle_t *handle, struct inode *inode,
2223 + unsigned long goal, int *errp)
2227 + if (!test_opt(inode->i_sb, MBALLOC)) {
2228 + ret = ext3_new_block_old(handle, inode, goal, errp);
2232 + ret = ext3_mb_new_blocks(handle, inode, goal, &len, 0, errp);
2238 +void ext3_free_blocks(handle_t *handle, struct inode * inode,
2239 + unsigned long block, unsigned long count, int metadata)
2241 + struct super_block *sb;
2245 + if (!test_opt(sb, MBALLOC))
2246 + ext3_free_blocks_sb(handle, sb, block, count, &freed);
2248 + ext3_mb_free_blocks(handle, inode, block, count, metadata, &freed);
2250 + DQUOT_FREE_BLOCK(inode, freed);
2254 +#define EXT3_ROOT "ext3"
2255 +#define EXT3_MB_STATS_NAME "mb_stats"
2256 +#define EXT3_MB_MAX_TO_SCAN_NAME "mb_max_to_scan"
2257 +#define EXT3_MB_MIN_TO_SCAN_NAME "mb_min_to_scan"
2259 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2260 + int count, int *eof, void *data)
2268 + len = sprintf(page, "%ld\n", ext3_mb_stats);
2273 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2274 + unsigned long count, void *data)
2278 + if (count >= sizeof(str)) {
2279 + printk(KERN_ERR "EXT3: %s string to long, max %u bytes\n",
2280 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2281 + return -EOVERFLOW;
2284 + if (copy_from_user(str, buffer, count))
2287 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2288 + ext3_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2292 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2293 + int count, int *eof, void *data)
2301 + len = sprintf(page, "%ld\n", ext3_mb_max_to_scan);
2306 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2307 + unsigned long count, void *data)
2312 + if (count >= sizeof(str)) {
2313 + printk(KERN_ERR "EXT3: %s string to long, max %u bytes\n",
2314 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2315 + return -EOVERFLOW;
2318 + if (copy_from_user(str, buffer, count))
2321 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2322 + value = simple_strtol(str, NULL, 0);
2326 + ext3_mb_max_to_scan = value;
2331 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2332 + int count, int *eof, void *data)
2340 + len = sprintf(page, "%ld\n", ext3_mb_min_to_scan);
2345 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2346 + unsigned long count, void *data)
2351 + if (count >= sizeof(str)) {
2352 + printk(KERN_ERR "EXT3: %s string to long, max %u bytes\n",
2353 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2354 + return -EOVERFLOW;
2357 + if (copy_from_user(str, buffer, count))
2360 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2361 + value = simple_strtol(str, NULL, 0);
2365 + ext3_mb_min_to_scan = value;
2370 +int __init init_ext3_proc(void)
2372 + struct proc_dir_entry *proc_ext3_mb_stats;
2373 + struct proc_dir_entry *proc_ext3_mb_max_to_scan;
2374 + struct proc_dir_entry *proc_ext3_mb_min_to_scan;
2376 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
2377 + if (proc_root_ext3 == NULL) {
2378 + printk(KERN_ERR "EXT3: Unable to create %s\n", EXT3_ROOT);
2382 + /* Initialize EXT3_MB_STATS_NAME */
2383 + proc_ext3_mb_stats = create_proc_entry(EXT3_MB_STATS_NAME,
2384 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
2385 + if (proc_ext3_mb_stats == NULL) {
2386 + printk(KERN_ERR "EXT3: Unable to create %s\n",
2387 + EXT3_MB_STATS_NAME);
2388 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
2392 + proc_ext3_mb_stats->data = NULL;
2393 + proc_ext3_mb_stats->read_proc = ext3_mb_stats_read;
2394 + proc_ext3_mb_stats->write_proc = ext3_mb_stats_write;
2396 + /* Initialize EXT3_MAX_TO_SCAN_NAME */
2397 + proc_ext3_mb_max_to_scan = create_proc_entry(
2398 + EXT3_MB_MAX_TO_SCAN_NAME,
2399 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
2400 + if (proc_ext3_mb_max_to_scan == NULL) {
2401 + printk(KERN_ERR "EXT3: Unable to create %s\n",
2402 + EXT3_MB_MAX_TO_SCAN_NAME);
2403 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
2404 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
2408 + proc_ext3_mb_max_to_scan->data = NULL;
2409 + proc_ext3_mb_max_to_scan->read_proc = ext3_mb_max_to_scan_read;
2410 + proc_ext3_mb_max_to_scan->write_proc = ext3_mb_max_to_scan_write;
2412 + /* Initialize EXT3_MIN_TO_SCAN_NAME */
2413 + proc_ext3_mb_min_to_scan = create_proc_entry(
2414 + EXT3_MB_MIN_TO_SCAN_NAME,
2415 + S_IFREG | S_IRUGO | S_IWUSR, proc_root_ext3);
2416 + if (proc_ext3_mb_min_to_scan == NULL) {
2417 + printk(KERN_ERR "EXT3: Unable to create %s\n",
2418 + EXT3_MB_MIN_TO_SCAN_NAME);
2419 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
2420 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
2421 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
2425 + proc_ext3_mb_min_to_scan->data = NULL;
2426 + proc_ext3_mb_min_to_scan->read_proc = ext3_mb_min_to_scan_read;
2427 + proc_ext3_mb_min_to_scan->write_proc = ext3_mb_min_to_scan_write;
2432 +void exit_ext3_proc(void)
2434 + remove_proc_entry(EXT3_MB_STATS_NAME, proc_root_ext3);
2435 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, proc_root_ext3);
2436 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, proc_root_ext3);
2437 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
2440 Index: linux-stage/fs/ext3/extents.c
2441 ===================================================================
2442 --- linux-stage.orig/fs/ext3/extents.c 2006-07-16 02:29:43.000000000 +0800
2443 +++ linux-stage/fs/ext3/extents.c 2006-07-16 02:29:49.000000000 +0800
2444 @@ -771,7 +771,7 @@ cleanup:
2445 for (i = 0; i < depth; i++) {
2448 - ext3_free_blocks(handle, tree->inode, ablocks[i], 1);
2449 + ext3_free_blocks(handle, tree->inode, ablocks[i], 1, 1);
2453 @@ -1428,7 +1428,7 @@ int ext3_ext_rm_idx(handle_t *handle, st
2454 path->p_idx->ei_leaf);
2455 bh = sb_find_get_block(tree->inode->i_sb, path->p_idx->ei_leaf);
2456 ext3_forget(handle, 1, tree->inode, bh, path->p_idx->ei_leaf);
2457 - ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1);
2458 + ext3_free_blocks(handle, tree->inode, path->p_idx->ei_leaf, 1, 1);
2462 @@ -1913,10 +1913,12 @@ ext3_remove_blocks(struct ext3_extents_t
2463 int needed = ext3_remove_blocks_credits(tree, ex, from, to);
2464 handle_t *handle = ext3_journal_start(tree->inode, needed);
2465 struct buffer_head *bh;
2467 + int i, metadata = 0;
2470 return PTR_ERR(handle);
2471 + if (S_ISDIR(tree->inode->i_mode) || S_ISLNK(tree->inode->i_mode))
2473 if (from >= ex->ee_block && to == ex->ee_block + ex->ee_len - 1) {
2475 unsigned long num, start;
2476 @@ -1928,7 +1930,7 @@ ext3_remove_blocks(struct ext3_extents_t
2477 bh = sb_find_get_block(tree->inode->i_sb, start + i);
2478 ext3_forget(handle, 0, tree->inode, bh, start + i);
2480 - ext3_free_blocks(handle, tree->inode, start, num);
2481 + ext3_free_blocks(handle, tree->inode, start, num, metadata);
2482 } else if (from == ex->ee_block && to <= ex->ee_block + ex->ee_len - 1) {
2483 printk("strange request: removal %lu-%lu from %u:%u\n",
2484 from, to, ex->ee_block, ex->ee_len);
2485 Index: linux-stage/fs/ext3/xattr.c
2486 ===================================================================
2487 --- linux-stage.orig/fs/ext3/xattr.c 2006-07-16 02:29:43.000000000 +0800
2488 +++ linux-stage/fs/ext3/xattr.c 2006-07-16 02:29:49.000000000 +0800
2489 @@ -484,7 +484,7 @@ ext3_xattr_release_block(handle_t *handl
2490 ea_bdebug(bh, "refcount now=0; freeing");
2492 mb_cache_entry_free(ce);
2493 - ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
2494 + ext3_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
2496 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
2498 @@ -805,7 +805,7 @@ inserted:
2499 new_bh = sb_getblk(sb, block);
2502 - ext3_free_blocks(handle, inode, block, 1);
2503 + ext3_free_blocks(handle, inode, block, 1, 1);
2507 Index: linux-stage/fs/ext3/balloc.c
2508 ===================================================================
2509 --- linux-stage.orig/fs/ext3/balloc.c 2006-07-16 02:29:43.000000000 +0800
2510 +++ linux-stage/fs/ext3/balloc.c 2006-07-16 02:33:13.000000000 +0800
2511 @@ -79,7 +79,7 @@ struct ext3_group_desc * ext3_get_group_
2513 * Return buffer_head on success or NULL in case of failure.
2515 -static struct buffer_head *
2516 +struct buffer_head *
2517 read_block_bitmap(struct super_block *sb, unsigned int block_group)
2519 struct ext3_group_desc * desc;
2520 @@ -490,24 +490,6 @@ error_return:
2524 -/* Free given blocks, update quota and i_blocks field */
2525 -void ext3_free_blocks(handle_t *handle, struct inode *inode,
2526 - ext3_fsblk_t block, unsigned long count)
2528 - struct super_block * sb;
2529 - unsigned long dquot_freed_blocks;
2533 - printk ("ext3_free_blocks: nonexistent device");
2536 - ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
2537 - if (dquot_freed_blocks)
2538 - DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
2543 * For ext3 allocations, we must not reuse any blocks which are
2544 * allocated in the bitmap buffer's "last committed data" copy. This
2545 @@ -1463,7 +1445,7 @@ out:
2549 -ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
2550 +ext3_fsblk_t ext3_new_block_old(handle_t *handle, struct inode *inode,
2551 ext3_fsblk_t goal, int *errp)
2553 unsigned long count = 1;
2554 Index: linux-stage/fs/ext3/super.c
2555 ===================================================================
2556 --- linux-stage.orig/fs/ext3/super.c 2006-07-16 02:29:43.000000000 +0800
2557 +++ linux-stage/fs/ext3/super.c 2006-07-16 02:29:49.000000000 +0800
2558 @@ -391,6 +391,7 @@ static void ext3_put_super (struct super
2559 struct ext3_super_block *es = sbi->s_es;
2562 + ext3_mb_release(sb);
2563 ext3_ext_release(sb);
2564 ext3_xattr_put_super(sb);
2565 journal_destroy(sbi->s_journal);
2566 @@ -641,7 +642,7 @@ enum {
2567 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
2568 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
2569 Opt_iopen, Opt_noiopen, Opt_iopen_nopriv,
2570 - Opt_extents, Opt_extdebug,
2571 + Opt_extents, Opt_extdebug, Opt_mballoc,
2575 @@ -696,6 +697,7 @@ static match_table_t tokens = {
2576 {Opt_iopen_nopriv, "iopen_nopriv"},
2577 {Opt_extents, "extents"},
2578 {Opt_extdebug, "extdebug"},
2579 + {Opt_mballoc, "mballoc"},
2580 {Opt_barrier, "barrier=%u"},
2582 {Opt_resize, "resize"},
2583 @@ -1047,6 +1049,9 @@ clear_qf_name:
2585 set_opt (sbi->s_mount_opt, EXTDEBUG);
2588 + set_opt (sbi->s_mount_opt, MBALLOC);
2592 "EXT3-fs: Unrecognized mount option \"%s\" "
2593 @@ -1773,6 +1778,7 @@ static int ext3_fill_super (struct super
2597 + ext3_mb_init(sb, needs_recovery);
2601 @@ -2712,7 +2718,13 @@ static struct file_system_type ext3_fs_t
2603 static int __init init_ext3_fs(void)
2605 - int err = init_ext3_xattr();
2608 + err = init_ext3_proc();
2612 + err = init_ext3_xattr();
2615 err = init_inodecache();
2616 @@ -2734,6 +2746,7 @@ static void __exit exit_ext3_fs(void)
2617 unregister_filesystem(&ext3_fs_type);
2618 destroy_inodecache();
2623 int ext3_prep_san_write(struct inode *inode, long *blocks,
2624 Index: linux-stage/fs/ext3/Makefile
2625 ===================================================================
2626 --- linux-stage.orig/fs/ext3/Makefile 2006-07-16 02:29:43.000000000 +0800
2627 +++ linux-stage/fs/ext3/Makefile 2006-07-16 02:29:49.000000000 +0800
2628 @@ -6,7 +6,7 @@ obj-$(CONFIG_EXT3_FS) += ext3.o
2630 ext3-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o iopen.o \
2631 ioctl.o namei.o super.o symlink.o hash.o resize.o \
2633 + extents.o mballoc.o
2635 ext3-$(CONFIG_EXT3_FS_XATTR) += xattr.o xattr_user.o xattr_trusted.o
2636 ext3-$(CONFIG_EXT3_FS_POSIX_ACL) += acl.o
2637 Index: linux-stage/include/linux/ext3_fs.h
2638 ===================================================================
2639 --- linux-stage.orig/include/linux/ext3_fs.h 2006-07-16 02:29:43.000000000 +0800
2640 +++ linux-stage/include/linux/ext3_fs.h 2006-07-16 02:29:49.000000000 +0800
2642 #define ext3_debug(f, a...) do {} while (0)
2645 +#define EXT3_MULTIBLOCK_ALLOCATOR 1
2647 +#define EXT3_MB_HINT_MERGE 1
2648 +#define EXT3_MB_HINT_RESERVED 2
2649 +#define EXT3_MB_HINT_METADATA 4
2650 +#define EXT3_MB_HINT_FIRST 8
2651 +#define EXT3_MB_HINT_BEST 16
2654 * Special inodes numbers
2656 @@ -379,6 +387,7 @@ struct ext3_inode {
2657 #define EXT3_MOUNT_IOPEN_NOPRIV 0x800000/* Make iopen world-readable */
2658 #define EXT3_MOUNT_EXTENTS 0x1000000/* Extents support */
2659 #define EXT3_MOUNT_EXTDEBUG 0x2000000/* Extents debug */
2660 +#define EXT3_MOUNT_MBALLOC 0x4000000/* Buddy allocation support */
2662 /* Compatibility, for having both ext2_fs.h and ext3_fs.h included at once */
2664 @@ -749,12 +758,12 @@ ext3_group_first_block_no(struct super_b
2666 extern int ext3_bg_has_super(struct super_block *sb, int group);
2667 extern unsigned long ext3_bg_num_gdb(struct super_block *sb, int group);
2668 -extern ext3_fsblk_t ext3_new_block (handle_t *handle, struct inode *inode,
2669 - ext3_fsblk_t goal, int *errp);
2670 +//extern ext3_fsblk_t ext3_new_block (handle_t *handle, struct inode *inode,
2671 +// ext3_fsblk_t goal, int *errp);
2672 extern ext3_fsblk_t ext3_new_blocks (handle_t *handle, struct inode *inode,
2673 ext3_fsblk_t goal, unsigned long *count, int *errp);
2674 extern void ext3_free_blocks (handle_t *handle, struct inode *inode,
2675 - ext3_fsblk_t block, unsigned long count);
2676 + ext3_fsblk_t block, unsigned long count, int metadata);
2677 extern void ext3_free_blocks_sb (handle_t *handle, struct super_block *sb,
2678 ext3_fsblk_t block, unsigned long count,
2679 unsigned long *pdquot_freed_blocks);
2680 @@ -881,6 +890,17 @@ extern void ext3_extents_initialize_bloc
2681 extern int ext3_ext_ioctl(struct inode *inode, struct file *filp,
2682 unsigned int cmd, unsigned long arg);
2685 +extern long ext3_mb_stats;
2686 +extern long ext3_mb_max_to_scan;
2687 +extern int ext3_mb_init(struct super_block *, int);
2688 +extern int ext3_mb_release(struct super_block *);
2689 +extern int ext3_mb_new_blocks(handle_t *, struct inode *, unsigned long, int *, int, int *);
2690 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
2691 +extern void ext3_mb_release_blocks(struct super_block *, int);
2692 +int __init init_ext3_proc(void);
2693 +void exit_ext3_proc(void);
2695 #endif /* __KERNEL__ */
2697 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
2698 Index: linux-stage/include/linux/ext3_fs_sb.h
2699 ===================================================================
2700 --- linux-stage.orig/include/linux/ext3_fs_sb.h 2006-07-16 02:29:43.000000000 +0800
2701 +++ linux-stage/include/linux/ext3_fs_sb.h 2006-07-16 02:29:49.000000000 +0800
2703 #include <linux/wait.h>
2704 #include <linux/blockgroup_lock.h>
2705 #include <linux/percpu_counter.h>
2706 +#include <linux/list.h>
2708 #include <linux/rbtree.h>
2709 +#include <linux/proc_fs.h>
2711 +struct ext3_buddy_group_blocks;
2712 +struct ext3_mb_history;
2713 +#define EXT3_BB_MAX_BLOCKS
2716 * third extended-fs super-block data in memory
2717 @@ -78,6 +84,38 @@ struct ext3_sb_info {
2718 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
2719 int s_jquota_fmt; /* Format of quota to use */
2722 + /* for buddy allocator */
2723 + struct ext3_group_info **s_group_info;
2724 + struct inode *s_buddy_cache;
2725 + long s_blocks_reserved;
2726 + spinlock_t s_reserve_lock;
2727 + struct list_head s_active_transaction;
2728 + struct list_head s_closed_transaction;
2729 + struct list_head s_committed_transaction;
2730 + spinlock_t s_md_lock;
2731 + tid_t s_last_transaction;
2733 + unsigned short *s_mb_offsets, *s_mb_maxs;
2735 + /* history to debug policy */
2736 + struct ext3_mb_history *s_mb_history;
2737 + int s_mb_history_cur;
2738 + int s_mb_history_max;
2739 + struct proc_dir_entry *s_mb_proc;
2740 + spinlock_t s_mb_history_lock;
2742 + /* stats for buddy allocator */
2743 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
2744 + atomic_t s_bal_success; /* we found long enough chunks */
2745 + atomic_t s_bal_allocated; /* in blocks */
2746 + atomic_t s_bal_ex_scanned; /* total extents scanned */
2747 + atomic_t s_bal_goals; /* goal hits */
2748 + atomic_t s_bal_breaks; /* too long searches */
2749 + atomic_t s_bal_2orders; /* 2^order hits */
2750 + spinlock_t s_bal_lock;
2751 + unsigned long s_mb_buddies_generated;
2752 + unsigned long long s_mb_generation_time;
2755 #endif /* _LINUX_EXT3_FS_SB */
2756 Index: linux-stage/fs/ext3/inode.c
2757 ===================================================================
2758 --- linux-stage.orig/fs/ext3/inode.c 2006-07-16 02:29:43.000000000 +0800
2759 +++ linux-stage/fs/ext3/inode.c 2006-07-16 02:29:49.000000000 +0800
2760 @@ -562,7 +562,7 @@ static int ext3_alloc_blocks(handle_t *h
2763 for (i = 0; i <index; i++)
2764 - ext3_free_blocks(handle, inode, new_blocks[i], 1);
2765 + ext3_free_blocks(handle, inode, new_blocks[i], 1, 1);
2769 @@ -661,9 +661,9 @@ failed:
2770 ext3_journal_forget(handle, branch[i].bh);
2772 for (i = 0; i <indirect_blks; i++)
2773 - ext3_free_blocks(handle, inode, new_blocks[i], 1);
2774 + ext3_free_blocks(handle, inode, new_blocks[i], 1, 1);
2776 - ext3_free_blocks(handle, inode, new_blocks[i], num);
2777 + ext3_free_blocks(handle, inode, new_blocks[i], num, 1);
2781 @@ -760,9 +760,9 @@ err_out:
2782 for (i = 1; i <= num; i++) {
2783 BUFFER_TRACE(where[i].bh, "call journal_forget");
2784 ext3_journal_forget(handle, where[i].bh);
2785 - ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
2786 + ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1,1);
2788 - ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
2789 + ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 1);
2793 @@ -2007,7 +2007,7 @@ static void ext3_clear_blocks(handle_t *
2797 - ext3_free_blocks(handle, inode, block_to_free, count);
2798 + ext3_free_blocks(handle, inode, block_to_free, count, 1);
2802 @@ -2180,7 +2180,7 @@ static void ext3_free_branches(handle_t
2803 ext3_journal_test_restart(handle, inode);
2806 - ext3_free_blocks(handle, inode, nr, 1);
2807 + ext3_free_blocks(handle, inode, nr, 1, 1);