1 Index: linux-2.6.18-92.1.6/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.18-92.1.6.orig/include/linux/ext3_fs.h
4 +++ linux-2.6.18-92.1.6/include/linux/ext3_fs.h
6 #define ext3_debug(f, a...) do {} while (0)
9 +#define EXT3_MULTIBLOCK_ALLOCATOR 1
11 +#define EXT3_MB_HINT_MERGE 1 /* prefer goal again. length */
12 +#define EXT3_MB_HINT_RESERVED 2 /* blocks already reserved */
13 +#define EXT3_MB_HINT_METADATA 4 /* metadata is being allocated */
14 +#define EXT3_MB_HINT_FIRST 8 /* first blocks in the file */
15 +#define EXT3_MB_HINT_BEST 16 /* search for the best chunk */
16 +#define EXT3_MB_HINT_DATA 32 /* data is being allocated */
17 +#define EXT3_MB_HINT_NOPREALLOC 64 /* don't preallocate (for tails) */
18 +#define EXT3_MB_HINT_GROUP_ALLOC 128 /* allocate for locality group */
19 +#define EXT3_MB_HINT_GOAL_ONLY 256 /* allocate goal blocks or none */
20 +#define EXT3_MB_HINT_TRY_GOAL 512 /* goal is meaningful */
22 +struct ext3_allocation_request {
23 + struct inode *inode; /* target inode for block we're allocating */
24 + unsigned long logical; /* logical block in target inode */
25 + unsigned long goal; /* phys. target (a hint) */
26 + unsigned long lleft; /* the closest logical allocated block to the left */
27 + unsigned long pleft; /* phys. block for ^^^ */
28 + unsigned long lright; /* the closest logical allocated block to the right */
29 + unsigned long pright; /* phys. block for ^^^ */
30 + unsigned long len; /* how many blocks we want to allocate */
31 + unsigned long flags; /* flags. see above EXT3_MB_HINT_* */
35 * Special inodes numbers
37 @@ -398,6 +423,14 @@ struct ext3_inode {
38 #define ext3_find_first_zero_bit ext2_find_first_zero_bit
39 #define ext3_find_next_zero_bit ext2_find_next_zero_bit
41 +#ifndef ext2_find_next_le_bit
42 +#ifdef __LITTLE_ENDIAN
43 +#define ext2_find_next_le_bit(addr, size, off) find_next_bit((addr), (size), (off))
45 +#error "mballoc needs a patch for big-endian systems - CFS bug 10634"
46 +#endif /* __LITTLE_ENDIAN */
47 +#endif /* !ext2_find_next_le_bit */
50 * Maximal mount counts between two filesystem checks
52 @@ -799,6 +832,20 @@ extern unsigned long ext3_count_dirs (st
53 extern void ext3_check_inodes_bitmap (struct super_block *);
54 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
57 +extern long ext3_mb_stats;
58 +extern long ext3_mb_max_to_scan;
59 +extern int ext3_mb_init(struct super_block *, int);
60 +extern int ext3_mb_release(struct super_block *);
61 +extern unsigned long ext3_mb_new_blocks(handle_t *, struct ext3_allocation_request *, int *);
62 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
63 +extern void ext3_mb_release_blocks(struct super_block *, int);
64 +extern void ext3_mb_release_blocks(struct super_block *, int);
65 +extern void ext3_mb_discard_inode_preallocations(struct inode *);
66 +extern int __init init_ext3_mb_proc(void);
67 +extern void exit_ext3_mb_proc(void);
68 +extern void ext3_mb_free_blocks(handle_t *, struct inode *, unsigned long, unsigned long, int, int *);
72 int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
73 @@ -843,6 +890,10 @@ extern int ext3_group_extend(struct supe
74 ext3_fsblk_t n_blocks_count);
77 +extern struct proc_dir_entry *proc_root_ext3;
78 +extern int __init init_ext3_proc(void);
79 +extern void exit_ext3_proc(void);
81 extern void ext3_error (struct super_block *, const char *, const char *, ...)
82 __attribute__ ((format (printf, 3, 4)));
83 extern void __ext3_std_error (struct super_block *, const char *, int);
84 Index: linux-2.6.18-92.1.6/include/linux/ext3_fs_sb.h
85 ===================================================================
86 --- linux-2.6.18-92.1.6.orig/include/linux/ext3_fs_sb.h
87 +++ linux-2.6.18-92.1.6/include/linux/ext3_fs_sb.h
88 @@ -88,6 +88,68 @@ struct ext3_sb_info {
89 unsigned long s_ext_blocks;
90 unsigned long s_ext_extents;
93 + /* for buddy allocator */
94 + struct ext3_group_info ***s_group_info;
95 + struct inode *s_buddy_cache;
96 + long s_blocks_reserved;
97 + spinlock_t s_reserve_lock;
98 + struct list_head s_active_transaction;
99 + struct list_head s_closed_transaction;
100 + struct list_head s_committed_transaction;
101 + spinlock_t s_md_lock;
102 + tid_t s_last_transaction;
103 + unsigned short *s_mb_offsets, *s_mb_maxs;
106 + unsigned long s_mb_factor;
107 + unsigned long s_stripe;
108 + unsigned long s_mb_small_req;
109 + unsigned long s_mb_large_req;
110 + unsigned long s_mb_max_to_scan;
111 + unsigned long s_mb_min_to_scan;
112 + unsigned long s_mb_max_groups_to_scan;
113 + unsigned long s_mb_stats;
114 + unsigned long s_mb_order2_reqs;
115 + unsigned long *s_mb_prealloc_table;
116 + unsigned long s_mb_prealloc_table_size;
117 + unsigned long s_mb_group_prealloc;
118 + /* where last allocation was done - for stream allocation */
119 + unsigned long s_mb_last_group;
120 + unsigned long s_mb_last_start;
122 + /* history to debug policy */
123 + struct ext3_mb_history *s_mb_history;
124 + int s_mb_history_cur;
125 + int s_mb_history_max;
126 + int s_mb_history_num;
127 + struct proc_dir_entry *s_dev_proc;
128 + spinlock_t s_mb_history_lock;
129 + int s_mb_history_filter;
131 + /* stats for buddy allocator */
132 + spinlock_t s_mb_pa_lock;
133 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
134 + atomic_t s_bal_success; /* we found long enough chunks */
135 + atomic_t s_bal_allocated; /* in blocks */
136 + atomic_t s_bal_ex_scanned; /* total extents scanned */
137 + atomic_t s_bal_goals; /* goal hits */
138 + atomic_t s_bal_breaks; /* too long searches */
139 + atomic_t s_bal_2orders; /* 2^order hits */
140 + spinlock_t s_bal_lock;
141 + unsigned long s_mb_buddies_generated;
142 + unsigned long long s_mb_generation_time;
143 + atomic_t s_mb_lost_chunks;
144 + atomic_t s_mb_preallocated;
145 + atomic_t s_mb_discarded;
147 + /* locality groups */
148 + struct ext3_locality_group *s_locality_groups;
152 +#define EXT3_GROUP_INFO(sb, group) \
153 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
154 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
156 #endif /* _LINUX_EXT3_FS_SB */
157 Index: linux-2.6.18-92.1.6/fs/ext3/super.c
158 ===================================================================
159 --- linux-2.6.18-92.1.6.orig/fs/ext3/super.c
160 +++ linux-2.6.18-92.1.6/fs/ext3/super.c
161 @@ -391,6 +391,7 @@ static void ext3_put_super (struct super
162 struct ext3_super_block *es = sbi->s_es;
165 + ext3_mb_release(sb);
166 ext3_ext_release(sb);
167 ext3_xattr_put_super(sb);
168 journal_destroy(sbi->s_journal);
169 @@ -433,6 +434,8 @@ static void ext3_put_super (struct super
170 invalidate_bdev(sbi->journal_bdev, 0);
171 ext3_blkdev_remove(sbi);
173 + remove_proc_entry(sb->s_id, proc_root_ext3);
174 + sbi->s_dev_proc = NULL;
175 sb->s_fs_info = NULL;
178 @@ -458,6 +461,8 @@ static struct inode *ext3_alloc_inode(st
179 ei->vfs_inode.i_version = 1;
181 memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
182 + INIT_LIST_HEAD(&ei->i_prealloc_list);
183 + spin_lock_init(&ei->i_prealloc_lock);
184 return &ei->vfs_inode;
187 @@ -1454,6 +1459,13 @@ static int ext3_fill_super (struct super
188 sbi->s_mount_opt = 0;
189 sbi->s_resuid = EXT3_DEF_RESUID;
190 sbi->s_resgid = EXT3_DEF_RESGID;
191 + sbi->s_dev_proc = proc_mkdir(sb->s_id, proc_root_ext3);
192 + if (sbi->s_dev_proc == NULL) {
193 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", sb->s_id);
194 + sb->s_fs_info = NULL;
201 @@ -1857,6 +1869,8 @@ failed_mount:
202 ext3_blkdev_remove(sbi);
205 + remove_proc_entry(sb->s_id, proc_root_ext3);
206 + sbi->s_dev_proc = NULL;
207 sb->s_fs_info = NULL;
210 @@ -2782,9 +2796,46 @@ static struct file_system_type ext3_fs_t
211 .fs_flags = FS_REQUIRES_DEV,
214 +#define EXT3_ROOT "ext3"
215 +struct proc_dir_entry *proc_root_ext3;
217 +int __init init_ext3_proc(void)
221 + if ((ret = init_ext3_mb_proc()))
224 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
225 + if (proc_root_ext3 == NULL) {
226 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
234 + exit_ext3_mb_proc();
239 +void exit_ext3_proc(void)
241 + exit_ext3_mb_proc();
242 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
245 static int __init init_ext3_fs(void)
247 - int err = init_ext3_xattr();
250 + err = init_ext3_proc();
254 + err = init_ext3_xattr();
257 err = init_inodecache();
258 @@ -2806,6 +2857,7 @@ static void __exit exit_ext3_fs(void)
259 unregister_filesystem(&ext3_fs_type);
260 destroy_inodecache();
265 int ext3_map_inode_page(struct inode *inode, struct page *page,
266 Index: linux-2.6.18-92.1.6/fs/ext3/mballoc.c
267 ===================================================================
269 +++ linux-2.6.18-92.1.6/fs/ext3/mballoc.c
272 + * Copyright 2008 Sun Microsystems, Inc.
273 + * Written by Alex Tomas <alex@clusterfs.com>
275 + * This program is free software; you can redistribute it and/or modify
276 + * it under the terms of the GNU General Public License version 2 as
277 + * published by the Free Software Foundation.
279 + * This program is distributed in the hope that it will be useful,
280 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
281 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
282 + * GNU General Public License for more details.
284 + * You should have received a copy of the GNU General Public Licens
285 + * along with this program; if not, write to the Free Software
286 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
291 + * mballoc.c contains the multiblocks allocation routines
294 +#include <linux/time.h>
295 +#include <linux/fs.h>
296 +#include <linux/namei.h>
297 +#include <linux/ext3_jbd.h>
298 +#include <linux/jbd.h>
299 +#include <linux/ext3_fs.h>
300 +#include <linux/quotaops.h>
301 +#include <linux/buffer_head.h>
302 +#include <linux/module.h>
303 +#include <linux/swap.h>
304 +#include <linux/proc_fs.h>
305 +#include <linux/pagemap.h>
306 +#include <linux/seq_file.h>
307 +#include <linux/version.h>
311 + * - test ext3_ext_search_left() and ext3_ext_search_right()
312 + * - search for metadata in few groups
315 + * - normalization should take into account whether file is still open
316 + * - discard preallocations if no free space left (policy?)
317 + * - don't normalize tails
319 + * - reservation for superuser
322 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
323 + * - track min/max extents in each group for better group selection
324 + * - mb_mark_used() may allocate chunk right after splitting buddy
325 + * - tree of groups sorted by number of free blocks
330 + * mballoc operates on the following data:
332 + * - in-core buddy (actually includes buddy and bitmap)
333 + * - preallocation descriptors (PAs)
335 + * there are two types of preallocations:
337 + * assiged to specific inode and can be used for this inode only.
338 + * it describes part of inode's space preallocated to specific
339 + * physical blocks. any block from that preallocated can be used
340 + * independent. the descriptor just tracks number of blocks left
341 + * unused. so, before taking some block from descriptor, one must
342 + * make sure corresponded logical block isn't allocated yet. this
343 + * also means that freeing any block within descriptor's range
344 + * must discard all preallocated blocks.
346 + * assigned to specific locality group which does not translate to
347 + * permanent set of inodes: inode can join and leave group. space
348 + * from this type of preallocation can be used for any inode. thus
349 + * it's consumed from the beginning to the end.
351 + * relation between them can be expressed as:
352 + * in-core buddy = on-disk bitmap + preallocation descriptors
354 + * this mean blocks mballoc considers used are:
355 + * - allocated blocks (persistent)
356 + * - preallocated blocks (non-persistent)
358 + * consistency in mballoc world means that at any time a block is either
359 + * free or used in ALL structures. notice: "any time" should not be read
360 + * literally -- time is discrete and delimited by locks.
362 + * to keep it simple, we don't use block numbers, instead we count number of
363 + * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
365 + * all operations can be expressed as:
366 + * - init buddy: buddy = on-disk + PAs
367 + * - new PA: buddy += N; PA = N
368 + * - use inode PA: on-disk += N; PA -= N
369 + * - discard inode PA buddy -= on-disk - PA; PA = 0
370 + * - use locality group PA on-disk += N; PA -= N
371 + * - discard locality group PA buddy -= PA; PA = 0
372 + * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
373 + * is used in real operation because we can't know actual used
374 + * bits from PA, only from on-disk bitmap
376 + * if we follow this strict logic, then all operations above should be atomic.
377 + * given some of them can block, we'd have to use something like semaphores
378 + * killing performance on high-end SMP hardware. let's try to relax it using
379 + * the following knowledge:
380 + * 1) if buddy is referenced, it's already initialized
381 + * 2) while block is used in buddy and the buddy is referenced,
382 + * nobody can re-allocate that block
383 + * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
384 + * bit set and PA claims same block, it's OK. IOW, one can set bit in
385 + * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
388 + * so, now we're building a concurrency table:
391 + * blocks for PA are allocated in the buddy, buddy must be referenced
392 + * until PA is linked to allocation group to avoid concurrent buddy init
394 + * we need to make sure that either on-disk bitmap or PA has uptodate data
395 + * given (3) we care that PA-=N operation doesn't interfere with init
396 + * - discard inode PA
397 + * the simplest way would be to have buddy initialized by the discard
398 + * - use locality group PA
399 + * again PA-=N must be serialized with init
400 + * - discard locality group PA
401 + * the simplest way would be to have buddy initialized by the discard
404 + * i_truncate_mutex serializes them
405 + * - discard inode PA
406 + * discard process must wait until PA isn't used by another process
407 + * - use locality group PA
408 + * some mutex should serialize them
409 + * - discard locality group PA
410 + * discard process must wait until PA isn't used by another process
413 + * i_truncate_mutex or another mutex should serializes them
414 + * - discard inode PA
415 + * discard process must wait until PA isn't used by another process
416 + * - use locality group PA
417 + * nothing wrong here -- they're different PAs covering different blocks
418 + * - discard locality group PA
419 + * discard process must wait until PA isn't used by another process
421 + * now we're ready to make few consequences:
422 + * - PA is referenced and while it is no discard is possible
423 + * - PA is referenced until block isn't marked in on-disk bitmap
424 + * - PA changes only after on-disk bitmap
425 + * - discard must not compete with init. either init is done before
426 + * any discard or they're serialized somehow
427 + * - buddy init as sum of on-disk bitmap and PAs is done atomically
429 + * a special case when we've used PA to emptiness. no need to modify buddy
430 + * in this case, but we should care about concurrent init
435 + * Logic in few words:
440 + * mark bits in on-disk bitmap
443 + * - use preallocation:
444 + * find proper PA (per-inode or group)
446 + * mark bits in on-disk bitmap
452 + * mark bits in on-disk bitmap
455 + * - discard preallocations in group:
457 + * move them onto local list
458 + * load on-disk bitmap
460 + * remove PA from object (inode or locality group)
461 + * mark free blocks in-core
463 + * - discard inode's preallocations:
470 + * - bitlock on a group (group)
471 + * - object (inode/locality) (object)
472 + * - per-pa lock (pa)
479 + * - find and use pa:
482 + * - release consumed pa:
487 + * - generate in-core bitmap:
491 + * - discard all for given object (inode, locality group):
496 + * - discard all for given group:
505 + * with AGGRESSIVE_CHECK allocator runs consistency checks over
506 + * structures. these checks slow things down a lot
508 +#define AGGRESSIVE_CHECK__
511 + * with DOUBLE_CHECK defined mballoc creates persistent in-core
512 + * bitmaps, maintains and uses them to check for double allocations
514 +#define DOUBLE_CHECK__
520 +#define mb_debug(fmt,a...) printk(fmt, ##a)
522 +#define mb_debug(fmt,a...)
526 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
527 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
529 +#define EXT3_MB_HISTORY
530 +#define EXT3_MB_HISTORY_ALLOC 1 /* allocation */
531 +#define EXT3_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
532 +#define EXT3_MB_HISTORY_DISCARD 4 /* preallocation discarded */
533 +#define EXT3_MB_HISTORY_FREE 8 /* free */
535 +#define EXT3_MB_HISTORY_DEFAULT (EXT3_MB_HISTORY_ALLOC | \
536 + EXT3_MB_HISTORY_PREALLOC | \
537 + EXT3_MB_HISTORY_DISCARD | \
538 + EXT3_MB_HISTORY_FREE)
541 + * How long mballoc can look for a best extent (in found extents)
543 +#define MB_DEFAULT_MAX_TO_SCAN 200
546 + * How long mballoc must look for a best extent
548 +#define MB_DEFAULT_MIN_TO_SCAN 10
551 + * How many groups mballoc will scan looking for the best chunk
553 +#define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
556 + * with 'ext3_mb_stats' allocator will collect stats that will be
557 + * shown at umount. The collecting costs though!
559 +#define MB_DEFAULT_STATS 1
562 + * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
563 + * by the stream allocator, which purpose is to pack requests
564 + * as close each to other as possible to produce smooth I/O traffic
566 +#define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
569 + * for which requests use 2^N search using buddies
571 +#define MB_DEFAULT_ORDER2_REQS 8
574 + * default stripe size = 1MB
576 +#define MB_DEFAULT_STRIPE 256
578 +static kmem_cache_t *ext3_pspace_cachep = NULL;
580 +#ifdef EXT3_BB_MAX_BLOCKS
581 +#undef EXT3_BB_MAX_BLOCKS
583 +#define EXT3_BB_MAX_BLOCKS 30
585 +struct ext3_free_metadata {
587 + unsigned short num;
588 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
589 + struct list_head list;
592 +struct ext3_group_info {
593 + unsigned long bb_state;
594 + unsigned long bb_tid;
595 + struct ext3_free_metadata *bb_md_cur;
596 + unsigned short bb_first_free;
597 + unsigned short bb_free;
598 + unsigned short bb_fragments;
599 + struct list_head bb_prealloc_list;
603 + unsigned short bb_counters[];
606 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
607 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
609 +#define EXT3_MB_GRP_NEED_INIT(grp) \
610 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
613 +struct ext3_prealloc_space {
614 + struct list_head pa_inode_list;
615 + struct list_head pa_group_list;
617 + struct list_head pa_tmp_list;
618 + struct rcu_head pa_rcu;
620 + spinlock_t pa_lock;
622 + unsigned pa_deleted;
623 + unsigned long pa_pstart; /* phys. block */
624 + unsigned long pa_lstart; /* log. block */
625 + unsigned short pa_len; /* len of preallocated chunk */
626 + unsigned short pa_free; /* how many blocks are free */
627 + unsigned short pa_linear; /* consumed in one direction
628 + * strictly, for group prealloc */
629 + spinlock_t *pa_obj_lock;
630 + struct inode *pa_inode; /* hack, for history only */
634 +struct ext3_free_extent {
635 + unsigned long fe_logical;
636 + unsigned long fe_start;
637 + unsigned long fe_group;
638 + unsigned long fe_len;
643 + * we try to group all related changes together
644 + * so that writeback can flush/allocate them together as well
646 +struct ext3_locality_group {
647 + /* for allocator */
648 + struct semaphore lg_sem; /* to serialize allocates */
649 + struct list_head lg_prealloc_list;/* list of preallocations */
650 + spinlock_t lg_prealloc_lock;
653 +struct ext3_allocation_context {
654 + struct inode *ac_inode;
655 + struct super_block *ac_sb;
657 + /* original request */
658 + struct ext3_free_extent ac_o_ex;
660 + /* goal request (after normalization) */
661 + struct ext3_free_extent ac_g_ex;
663 + /* the best found extent */
664 + struct ext3_free_extent ac_b_ex;
666 + /* copy of the bext found extent taken before preallocation efforts */
667 + struct ext3_free_extent ac_f_ex;
669 + /* number of iterations done. we have to track to limit searching */
670 + unsigned long ac_ex_scanned;
671 + __u16 ac_groups_scanned;
675 + __u16 ac_flags; /* allocation hints */
679 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
680 + * N > 0, the field stores N, otherwise 0 */
681 + __u8 ac_op; /* operation, for history only */
682 + struct page *ac_bitmap_page;
683 + struct page *ac_buddy_page;
684 + struct ext3_prealloc_space *ac_pa;
685 + struct ext3_locality_group *ac_lg;
688 +#define AC_STATUS_CONTINUE 1
689 +#define AC_STATUS_FOUND 2
690 +#define AC_STATUS_BREAK 3
692 +struct ext3_mb_history {
693 + struct ext3_free_extent orig; /* orig allocation */
694 + struct ext3_free_extent goal; /* goal allocation */
695 + struct ext3_free_extent result; /* result allocation */
698 + __u16 found; /* how many extents have been found */
699 + __u16 groups; /* how many groups have been scanned */
700 + __u16 tail; /* what tail broke some buddy */
701 + __u16 buddy; /* buddy the tail ^^^ broke */
703 + __u8 cr:3; /* which phase the result extent was found at */
709 + struct page *bd_buddy_page;
711 + struct page *bd_bitmap_page;
713 + struct ext3_group_info *bd_info;
714 + struct super_block *bd_sb;
716 + unsigned bd_blkbits;
718 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
719 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
721 +#ifndef EXT3_MB_HISTORY
722 +#define ext3_mb_store_history(ac)
724 +static void ext3_mb_store_history(struct ext3_allocation_context *ac);
727 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
729 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
730 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
731 +unsigned long ext3_new_blocks_old(handle_t *handle, struct inode *inode,
732 + unsigned long goal, unsigned long *count, int *errp);
733 +void ext3_mb_release_blocks(struct super_block *, int);
734 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
735 +void ext3_mb_free_committed_blocks(struct super_block *);
736 +void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group);
737 +void ext3_mb_free_consumed_preallocations(struct ext3_allocation_context *ac);
738 +void ext3_mb_return_to_preallocation(struct inode *inode, struct ext3_buddy *e3b,
739 + sector_t block, int count);
740 +void ext3_mb_show_ac(struct ext3_allocation_context *ac);
741 +void ext3_mb_check_with_pa(struct ext3_buddy *e3b, int first, int count);
742 +void ext3_mb_put_pa(struct ext3_allocation_context *, struct super_block *, struct ext3_prealloc_space *pa);
743 +int ext3_mb_init_per_dev_proc(struct super_block *sb);
744 +int ext3_mb_destroy_per_dev_proc(struct super_block *sb);
747 + * Calculate the block group number and offset, given a block number
749 +static void ext3_get_group_no_and_offset(struct super_block *sb,
750 + unsigned long blocknr,
751 + unsigned long *blockgrpp,
752 + unsigned long *offsetp)
754 + struct ext3_super_block *es = EXT3_SB(sb)->s_es;
755 + unsigned long offset;
757 + blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
758 + offset = blocknr % EXT3_BLOCKS_PER_GROUP(sb);
759 + blocknr = blocknr / EXT3_BLOCKS_PER_GROUP(sb);
763 + *blockgrpp = blocknr;
768 +ext3_lock_group(struct super_block *sb, int group)
770 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
771 + &EXT3_GROUP_INFO(sb, group)->bb_state);
775 +ext3_unlock_group(struct super_block *sb, int group)
777 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
778 + &EXT3_GROUP_INFO(sb, group)->bb_state);
782 +ext3_is_group_locked(struct super_block *sb, int group)
784 + return bit_spin_is_locked(EXT3_GROUP_INFO_LOCKED_BIT,
785 + &EXT3_GROUP_INFO(sb, group)->bb_state);
788 +unsigned long ext3_grp_offs_to_block(struct super_block *sb,
789 + struct ext3_free_extent *fex)
791 + unsigned long block;
793 + block = (unsigned long) fex->fe_group * EXT3_BLOCKS_PER_GROUP(sb)
795 + + le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
799 +#if BITS_PER_LONG == 64
800 +#define mb_correct_addr_and_bit(bit,addr) \
802 + bit += ((unsigned long) addr & 7UL) << 3; \
803 + addr = (void *) ((unsigned long) addr & ~7UL); \
805 +#elif BITS_PER_LONG == 32
806 +#define mb_correct_addr_and_bit(bit,addr) \
808 + bit += ((unsigned long) addr & 3UL) << 3; \
809 + addr = (void *) ((unsigned long) addr & ~3UL); \
812 +#error "how many bits you are?!"
815 +static inline int mb_test_bit(int bit, void *addr)
817 + mb_correct_addr_and_bit(bit,addr);
818 + return ext2_test_bit(bit, addr);
821 +static inline void mb_set_bit(int bit, void *addr)
823 + mb_correct_addr_and_bit(bit,addr);
824 + ext2_set_bit(bit, addr);
827 +static inline void mb_set_bit_atomic(spinlock_t *lock, int bit, void *addr)
829 + mb_correct_addr_and_bit(bit,addr);
830 + ext2_set_bit_atomic(lock, bit, addr);
833 +static inline void mb_clear_bit(int bit, void *addr)
835 + mb_correct_addr_and_bit(bit,addr);
836 + ext2_clear_bit(bit, addr);
839 +static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
841 + mb_correct_addr_and_bit(bit,addr);
842 + ext2_clear_bit_atomic(lock, bit, addr);
845 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
848 +#if BITS_PER_LONG == 64
849 + fix = ((unsigned long) addr & 7UL) << 3;
850 + addr = (void *) ((unsigned long) addr & ~7UL);
851 +#elif BITS_PER_LONG == 32
852 + fix = ((unsigned long) addr & 3UL) << 3;
853 + addr = (void *) ((unsigned long) addr & ~3UL);
855 +#error "how many bits you are?!"
859 + return ext2_find_next_zero_bit(addr, max, start) - fix;
862 +static inline int mb_find_next_bit(void *addr, int max, int start)
865 +#if BITS_PER_LONG == 64
866 + fix = ((unsigned long) addr & 7UL) << 3;
867 + addr = (void *) ((unsigned long) addr & ~7UL);
868 +#elif BITS_PER_LONG == 32
869 + fix = ((unsigned long) addr & 3UL) << 3;
870 + addr = (void *) ((unsigned long) addr & ~3UL);
872 +#error "how many bits you are?!"
879 + return find_next_bit(addr, max, start) - fix;
883 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
887 + BUG_ON(EXT3_MB_BITMAP(e3b) == EXT3_MB_BUDDY(e3b));
888 + BUG_ON(max == NULL);
890 + if (order > e3b->bd_blkbits + 1) {
895 + /* at order 0 we see each particular block */
896 + *max = 1 << (e3b->bd_blkbits + 3);
898 + return EXT3_MB_BITMAP(e3b);
900 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
901 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
907 +void mb_free_blocks_double(struct inode *inode, struct ext3_buddy *e3b,
908 + int first, int count)
911 + struct super_block *sb = e3b->bd_sb;
913 + if (unlikely(e3b->bd_info->bb_bitmap == NULL))
915 + BUG_ON(!ext3_is_group_locked(sb, e3b->bd_group));
916 + for (i = 0; i < count; i++) {
917 + if (!mb_test_bit(first + i, e3b->bd_info->bb_bitmap)) {
918 + unsigned long blocknr;
919 + blocknr = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb);
920 + blocknr += first + i;
922 + le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
924 + ext3_error(sb, __FUNCTION__, "double-free of inode"
925 + " %lu's block %lu(bit %u in group %u)\n",
926 + inode ? inode->i_ino : 0, blocknr,
927 + first + i, e3b->bd_group);
929 + mb_clear_bit(first + i, e3b->bd_info->bb_bitmap);
933 +void mb_mark_used_double(struct ext3_buddy *e3b, int first, int count)
936 + if (unlikely(e3b->bd_info->bb_bitmap == NULL))
938 + BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
939 + for (i = 0; i < count; i++) {
940 + BUG_ON(mb_test_bit(first + i, e3b->bd_info->bb_bitmap));
941 + mb_set_bit(first + i, e3b->bd_info->bb_bitmap);
945 +void mb_cmp_bitmaps(struct ext3_buddy *e3b, void *bitmap)
947 + if (memcmp(e3b->bd_info->bb_bitmap, bitmap, e3b->bd_sb->s_blocksize)) {
948 + unsigned char *b1, *b2;
950 + b1 = (unsigned char *) e3b->bd_info->bb_bitmap;
951 + b2 = (unsigned char *) bitmap;
952 + for (i = 0; i < e3b->bd_sb->s_blocksize; i++) {
953 + if (b1[i] != b2[i]) {
954 + printk("corruption in group %u at byte %u(%u): "
955 + "%x in copy != %x on disk/prealloc\n",
956 + e3b->bd_group, i, i * 8, b1[i], b2[i]);
964 +#define mb_free_blocks_double(a,b,c,d)
965 +#define mb_mark_used_double(a,b,c)
966 +#define mb_cmp_bitmaps(a,b)
969 +#ifdef AGGRESSIVE_CHECK
971 +#define MB_CHECK_ASSERT(assert) \
974 + printk (KERN_EMERG \
975 + "Assertion failure in %s() at %s:%d: \"%s\"\n", \
976 + function, file, line, # assert); \
981 +static int __mb_check_buddy(struct ext3_buddy *e3b, char *file,
982 + const char *function, int line)
984 + struct super_block *sb = e3b->bd_sb;
985 + int order = e3b->bd_blkbits + 1;
986 + int max, max2, i, j, k, count;
987 + struct ext3_group_info *grp;
988 + int fragments = 0, fstart;
989 + struct list_head *cur;
990 + void *buddy, *buddy2;
992 + if (!test_opt(sb, MBALLOC))
996 + static int mb_check_counter = 0;
997 + if (mb_check_counter++ % 100 != 0)
1001 + while (order > 1) {
1002 + buddy = mb_find_buddy(e3b, order, &max);
1003 + MB_CHECK_ASSERT(buddy);
1004 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
1005 + MB_CHECK_ASSERT(buddy2);
1006 + MB_CHECK_ASSERT(buddy != buddy2);
1007 + MB_CHECK_ASSERT(max * 2 == max2);
1010 + for (i = 0; i < max; i++) {
1012 + if (mb_test_bit(i, buddy)) {
1013 + /* only single bit in buddy2 may be 1 */
1014 + if (!mb_test_bit(i << 1, buddy2))
1015 + MB_CHECK_ASSERT(mb_test_bit((i<<1)+1, buddy2));
1016 + else if (!mb_test_bit((i << 1) + 1, buddy2))
1017 + MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
1021 + /* both bits in buddy2 must be 0 */
1022 + MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
1023 + MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
1025 + for (j = 0; j < (1 << order); j++) {
1026 + k = (i * (1 << order)) + j;
1027 + MB_CHECK_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
1031 + MB_CHECK_ASSERT(e3b->bd_info->bb_counters[order] == count);
1036 + buddy = mb_find_buddy(e3b, 0, &max);
1037 + for (i = 0; i < max; i++) {
1038 + if (!mb_test_bit(i, buddy)) {
1039 + MB_CHECK_ASSERT(i >= e3b->bd_info->bb_first_free);
1040 + if (fstart == -1) {
1047 + /* check used bits only */
1048 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
1049 + buddy2 = mb_find_buddy(e3b, j, &max2);
1051 + MB_CHECK_ASSERT(k < max2);
1052 + MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
1055 + MB_CHECK_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
1056 + MB_CHECK_ASSERT(e3b->bd_info->bb_fragments == fragments);
1058 + grp = EXT3_GROUP_INFO(sb, e3b->bd_group);
1059 + buddy = mb_find_buddy(e3b, 0, &max);
1060 + list_for_each(cur, &grp->bb_prealloc_list) {
1061 + unsigned long groupnr;
1062 + struct ext3_prealloc_space *pa;
1063 + pa = list_entry(cur, struct ext3_prealloc_space, group_list);
1064 + ext3_get_group_no_and_offset(sb, pa->pstart, &groupnr, &k);
1065 + MB_CHECK_ASSERT(groupnr == e3b->bd_group);
1066 + for (i = 0; i < pa->len; i++)
1067 + MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
1071 +#undef MB_CHECK_ASSERT
1072 +#define mb_check_buddy(e3b) __mb_check_buddy(e3b,__FILE__,__FUNCTION__,__LINE__)
1074 +#define mb_check_buddy(e3b)
1077 +/* find most significant bit */
1078 +static int inline fmsb(unsigned short word)
1092 + } while (word != 0);
1098 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
1099 + int len, struct ext3_group_info *grp)
1101 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1102 + unsigned short min, max, chunk, border;
1104 + BUG_ON(len >= EXT3_BLOCKS_PER_GROUP(sb));
1106 + border = 2 << sb->s_blocksize_bits;
1109 + /* find how many blocks can be covered since this position */
1110 + max = ffs(first | border) - 1;
1112 + /* find how many blocks of power 2 we need to mark */
1119 + /* mark multiblock chunks only */
1120 + grp->bb_counters[min]++;
1122 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
1130 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
1133 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
1134 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
1135 + unsigned short i = 0, first, len;
1136 + unsigned free = 0, fragments = 0;
1137 + unsigned long long period = get_cycles();
1139 + /* initialize buddy from bitmap which is aggregation
1140 + * of on-disk bitmap and preallocations */
1141 + i = mb_find_next_zero_bit(bitmap, max, 0);
1142 + grp->bb_first_free = i;
1146 + i = ext2_find_next_le_bit(bitmap, max, i);
1152 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
1154 + grp->bb_counters[0]++;
1156 + i = mb_find_next_zero_bit(bitmap, max, i);
1158 + grp->bb_fragments = fragments;
1160 + if (free != grp->bb_free) {
1161 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
1162 + group, free, grp->bb_free);
1163 + grp->bb_free = free;
1166 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
1168 + period = get_cycles() - period;
1169 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
1170 + EXT3_SB(sb)->s_mb_buddies_generated++;
1171 + EXT3_SB(sb)->s_mb_generation_time += period;
1172 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
1175 +static int ext3_mb_init_cache(struct page *page, char *incore)
1177 + int blocksize, blocks_per_page, groups_per_page;
1178 + int err = 0, i, first_group, first_block;
1179 + struct super_block *sb;
1180 + struct buffer_head *bhs;
1181 + struct buffer_head **bh;
1182 + struct inode *inode;
1183 + char *data, *bitmap;
1185 + mb_debug("init page %lu\n", page->index);
1187 + inode = page->mapping->host;
1189 + blocksize = 1 << inode->i_blkbits;
1190 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
1192 + groups_per_page = blocks_per_page >> 1;
1193 + if (groups_per_page == 0)
1194 + groups_per_page = 1;
1196 + /* allocate buffer_heads to read bitmaps */
1197 + if (groups_per_page > 1) {
1199 + i = sizeof(struct buffer_head *) * groups_per_page;
1200 + bh = kmalloc(i, GFP_NOFS);
1207 + first_group = page->index * blocks_per_page / 2;
1209 + /* read all groups the page covers into the cache */
1210 + for (i = 0; i < groups_per_page; i++) {
1211 + struct ext3_group_desc * desc;
1213 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
1217 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
1222 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
1223 + if (bh[i] == NULL)
1226 + if (buffer_uptodate(bh[i]))
1229 + lock_buffer(bh[i]);
1230 + if (buffer_uptodate(bh[i])) {
1231 + unlock_buffer(bh[i]);
1236 + bh[i]->b_end_io = end_buffer_read_sync;
1237 + submit_bh(READ, bh[i]);
1238 + mb_debug("read bitmap for group %u\n", first_group + i);
1241 + /* wait for I/O completion */
1242 + for (i = 0; i < groups_per_page && bh[i]; i++)
1243 + wait_on_buffer(bh[i]);
1246 + for (i = 0; i < groups_per_page && bh[i]; i++)
1247 + if (!buffer_uptodate(bh[i]))
1250 + first_block = page->index * blocks_per_page;
1251 + for (i = 0; i < blocks_per_page; i++) {
1254 + group = (first_block + i) >> 1;
1255 + if (group >= EXT3_SB(sb)->s_groups_count)
1258 + data = page_address(page) + (i * blocksize);
1259 + bitmap = bh[group - first_group]->b_data;
1261 + if ((first_block + i) & 1) {
1262 + /* this is block of buddy */
1263 + BUG_ON(incore == NULL);
1264 + mb_debug("put buddy for group %u in page %lu/%x\n",
1265 + group, page->index, i * blocksize);
1266 + memset(data, 0xff, blocksize);
1267 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
1268 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
1269 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
1270 + ext3_mb_generate_buddy(sb, data, incore, group);
1273 + /* this is block of bitmap */
1274 + BUG_ON(incore != NULL);
1275 + mb_debug("put bitmap for group %u in page %lu/%x\n",
1276 + group, page->index, i * blocksize);
1278 + /* see comments in ext3_mb_put_pa() */
1279 + ext3_lock_group(sb, group);
1280 + memcpy(data, bitmap, blocksize);
1282 + /* mark all preallocated blocks used in in-core bitmap */
1283 + ext3_mb_generate_from_pa(sb, data, group);
1284 + ext3_unlock_group(sb, group);
1289 + SetPageUptodate(page);
1293 + for (i = 0; i < groups_per_page && bh[i]; i++)
1301 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
1302 + struct ext3_buddy *e3b)
1304 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1305 + struct inode *inode = sbi->s_buddy_cache;
1306 + int blocks_per_page, block, pnum, poff;
1307 + struct page *page;
1309 + mb_debug("load group %u\n", group);
1311 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1313 + e3b->bd_blkbits = sb->s_blocksize_bits;
1314 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
1316 + e3b->bd_group = group;
1317 + e3b->bd_buddy_page = NULL;
1318 + e3b->bd_bitmap_page = NULL;
1320 + block = group * 2;
1321 + pnum = block / blocks_per_page;
1322 + poff = block % blocks_per_page;
1324 + /* we could use find_or_create_page(), but it locks page
1325 + * what we'd like to avoid in fast path ... */
1326 + page = find_get_page(inode->i_mapping, pnum);
1327 + if (page == NULL || !PageUptodate(page)) {
1329 + page_cache_release(page);
1330 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1332 + BUG_ON(page->mapping != inode->i_mapping);
1333 + if (!PageUptodate(page)) {
1334 + ext3_mb_init_cache(page, NULL);
1335 + mb_cmp_bitmaps(e3b, page_address(page) +
1336 + (poff * sb->s_blocksize));
1338 + unlock_page(page);
1341 + if (page == NULL || !PageUptodate(page))
1343 + e3b->bd_bitmap_page = page;
1344 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1345 + mark_page_accessed(page);
1348 + pnum = block / blocks_per_page;
1349 + poff = block % blocks_per_page;
1351 + page = find_get_page(inode->i_mapping, pnum);
1352 + if (page == NULL || !PageUptodate(page)) {
1354 + page_cache_release(page);
1355 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1357 + BUG_ON(page->mapping != inode->i_mapping);
1358 + if (!PageUptodate(page))
1359 + ext3_mb_init_cache(page, e3b->bd_bitmap);
1361 + unlock_page(page);
1364 + if (page == NULL || !PageUptodate(page))
1366 + e3b->bd_buddy_page = page;
1367 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1368 + mark_page_accessed(page);
1370 + BUG_ON(e3b->bd_bitmap_page == NULL);
1371 + BUG_ON(e3b->bd_buddy_page == NULL);
1376 + if (e3b->bd_bitmap_page)
1377 + page_cache_release(e3b->bd_bitmap_page);
1378 + if (e3b->bd_buddy_page)
1379 + page_cache_release(e3b->bd_buddy_page);
1380 + e3b->bd_buddy = NULL;
1381 + e3b->bd_bitmap = NULL;
1385 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1387 + if (e3b->bd_bitmap_page)
1388 + page_cache_release(e3b->bd_bitmap_page);
1389 + if (e3b->bd_buddy_page)
1390 + page_cache_release(e3b->bd_buddy_page);
1394 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1399 + BUG_ON(EXT3_MB_BITMAP(e3b) == EXT3_MB_BUDDY(e3b));
1400 + BUG_ON(block >= (1 << (e3b->bd_blkbits + 3)));
1402 + bb = EXT3_MB_BUDDY(e3b);
1403 + while (order <= e3b->bd_blkbits + 1) {
1404 + block = block >> 1;
1405 + if (!mb_test_bit(block, bb)) {
1406 + /* this block is part of buddy of order 'order' */
1409 + bb += 1 << (e3b->bd_blkbits - order);
1415 +static inline void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
1420 + while (cur < len) {
1421 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1422 + /* fast path: clear whole word at once */
1423 + addr = bm + (cur >> 3);
1428 + mb_clear_bit_atomic(lock, cur, bm);
1433 +static inline void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
1438 + while (cur < len) {
1439 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1440 + /* fast path: clear whole word at once */
1441 + addr = bm + (cur >> 3);
1442 + *addr = 0xffffffff;
1446 + mb_set_bit_atomic(lock, cur, bm);
1451 +static int mb_free_blocks(struct inode *inode, struct ext3_buddy *e3b,
1452 + int first, int count)
1454 + int block = 0, max = 0, order;
1455 + void *buddy, *buddy2;
1456 + struct super_block *sb = e3b->bd_sb;
1458 + BUG_ON(first + count > (sb->s_blocksize << 3));
1459 + BUG_ON(!ext3_is_group_locked(sb, e3b->bd_group));
1460 + mb_check_buddy(e3b);
1461 + mb_free_blocks_double(inode, e3b, first, count);
1463 + e3b->bd_info->bb_free += count;
1464 + if (first < e3b->bd_info->bb_first_free)
1465 + e3b->bd_info->bb_first_free = first;
1467 + /* let's maintain fragments counter */
1469 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1470 + if (first + count < EXT3_SB(sb)->s_mb_maxs[0])
1471 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1473 + e3b->bd_info->bb_fragments--;
1474 + else if (!block && !max)
1475 + e3b->bd_info->bb_fragments++;
1477 + /* let's maintain buddy itself */
1478 + while (count-- > 0) {
1482 + if (!mb_test_bit(block, EXT3_MB_BITMAP(e3b))) {
1483 + unsigned long blocknr;
1484 + blocknr = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb);
1487 + le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
1489 + ext3_error(sb, __FUNCTION__, "double-free of inode"
1490 + " %lu's block %lu(bit %u in group %u)\n",
1491 + inode ? inode->i_ino : 0, blocknr, block,
1494 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1495 + e3b->bd_info->bb_counters[order]++;
1497 + /* start of the buddy */
1498 + buddy = mb_find_buddy(e3b, order, &max);
1502 + if (mb_test_bit(block, buddy) ||
1503 + mb_test_bit(block + 1, buddy))
1506 + /* both the buddies are free, try to coalesce them */
1507 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1513 + /* for special purposes, we don't set
1514 + * free bits in bitmap */
1515 + mb_set_bit(block, buddy);
1516 + mb_set_bit(block + 1, buddy);
1518 + e3b->bd_info->bb_counters[order]--;
1519 + e3b->bd_info->bb_counters[order]--;
1521 + block = block >> 1;
1523 + e3b->bd_info->bb_counters[order]++;
1525 + mb_clear_bit(block, buddy2);
1529 + mb_check_buddy(e3b);
1534 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1535 + int needed, struct ext3_free_extent *ex)
1537 + int next = block, max, ord;
1540 + BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
1541 + BUG_ON(ex == NULL);
1543 + buddy = mb_find_buddy(e3b, order, &max);
1544 + BUG_ON(buddy == NULL);
1545 + BUG_ON(block >= max);
1546 + if (mb_test_bit(block, buddy)) {
1553 + if (likely(order == 0)) {
1554 + /* find actual order */
1555 + order = mb_find_order_for_block(e3b, block);
1556 + block = block >> order;
1559 + ex->fe_len = 1 << order;
1560 + ex->fe_start = block << order;
1561 + ex->fe_group = e3b->bd_group;
1563 + /* calc difference from given start */
1564 + next = next - ex->fe_start;
1565 + ex->fe_len -= next;
1566 + ex->fe_start += next;
1568 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1570 + if (block + 1 >= max)
1573 + next = (block + 1) * (1 << order);
1574 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1577 + ord = mb_find_order_for_block(e3b, next);
1580 + block = next >> order;
1581 + ex->fe_len += 1 << order;
1584 + BUG_ON(ex->fe_start + ex->fe_len > (1 << (e3b->bd_blkbits + 3)));
1585 + return ex->fe_len;
1588 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1590 + int ord, mlen = 0, max = 0, cur;
1591 + int start = ex->fe_start;
1592 + int len = ex->fe_len;
1597 + BUG_ON(start + len > (e3b->bd_sb->s_blocksize << 3));
1598 + BUG_ON(e3b->bd_group != ex->fe_group);
1599 + BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
1600 + mb_check_buddy(e3b);
1601 + mb_mark_used_double(e3b, start, len);
1603 + e3b->bd_info->bb_free -= len;
1604 + if (e3b->bd_info->bb_first_free == start)
1605 + e3b->bd_info->bb_first_free += len;
1607 + /* let's maintain fragments counter */
1609 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1610 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1611 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1613 + e3b->bd_info->bb_fragments++;
1614 + else if (!mlen && !max)
1615 + e3b->bd_info->bb_fragments--;
1617 + /* let's maintain buddy itself */
1619 + ord = mb_find_order_for_block(e3b, start);
1621 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1622 + /* the whole chunk may be allocated at once! */
1624 + buddy = mb_find_buddy(e3b, ord, &max);
1625 + BUG_ON((start >> ord) >= max);
1626 + mb_set_bit(start >> ord, buddy);
1627 + e3b->bd_info->bb_counters[ord]--;
1634 + /* store for history */
1636 + ret = len | (ord << 16);
1638 + /* we have to split large buddy */
1640 + buddy = mb_find_buddy(e3b, ord, &max);
1641 + mb_set_bit(start >> ord, buddy);
1642 + e3b->bd_info->bb_counters[ord]--;
1645 + cur = (start >> ord) & ~1U;
1646 + buddy = mb_find_buddy(e3b, ord, &max);
1647 + mb_clear_bit(cur, buddy);
1648 + mb_clear_bit(cur + 1, buddy);
1649 + e3b->bd_info->bb_counters[ord]++;
1650 + e3b->bd_info->bb_counters[ord]++;
1653 + mb_set_bits(sb_bgl_lock(EXT3_SB(e3b->bd_sb), ex->fe_group),
1654 + EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1655 + mb_check_buddy(e3b);
1661 + * Must be called under group lock!
1663 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1664 + struct ext3_buddy *e3b)
1666 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1667 + unsigned long ret;
1669 + BUG_ON(ac->ac_b_ex.fe_group != e3b->bd_group);
1670 + BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1672 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1673 + ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1674 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1676 + /* preallocation can change ac_b_ex, thus we store actually
1677 + * allocated blocks for history */
1678 + ac->ac_f_ex = ac->ac_b_ex;
1680 + ac->ac_status = AC_STATUS_FOUND;
1681 + ac->ac_tail = ret & 0xffff;
1682 + ac->ac_buddy = ret >> 16;
1684 + /* XXXXXXX: SUCH A HORRIBLE **CK */
1685 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1686 + get_page(ac->ac_bitmap_page);
1687 + ac->ac_buddy_page = e3b->bd_buddy_page;
1688 + get_page(ac->ac_buddy_page);
1690 + /* store last allocated for subsequent stream allocation */
1691 + if ((ac->ac_flags & EXT3_MB_HINT_DATA)) {
1692 + spin_lock(&sbi->s_md_lock);
1693 + sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1694 + sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1695 + spin_unlock(&sbi->s_md_lock);
1700 + * regular allocator, for general purposes allocation
1703 +void ext3_mb_check_limits(struct ext3_allocation_context *ac,
1704 + struct ext3_buddy *e3b,
1707 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1708 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1709 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1710 + struct ext3_free_extent ex;
1714 + * We don't want to scan for a whole year
1716 + if (ac->ac_found > sbi->s_mb_max_to_scan &&
1717 + !(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1718 + ac->ac_status = AC_STATUS_BREAK;
1723 + * Haven't found good chunk so far, let's continue
1725 + if (bex->fe_len < gex->fe_len)
1728 + if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1729 + && bex->fe_group == e3b->bd_group) {
1730 + /* recheck chunk's availability - we don't know
1731 + * when it was found (within this lock-unlock
1732 + * period or not) */
1733 + max = mb_find_extent(e3b, 0, bex->fe_start, gex->fe_len, &ex);
1734 + if (max >= gex->fe_len) {
1735 + ext3_mb_use_best_found(ac, e3b);
1742 + * The routine checks whether found extent is good enough. If it is,
1743 + * then the extent gets marked used and flag is set to the context
1744 + * to stop scanning. Otherwise, the extent is compared with the
1745 + * previous found extent and if new one is better, then it's stored
1746 + * in the context. Later, the best found extent will be used, if
1747 + * mballoc can't find good enough extent.
1749 + * FIXME: real allocation policy is to be designed yet!
1751 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1752 + struct ext3_free_extent *ex,
1753 + struct ext3_buddy *e3b)
1755 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1756 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1758 + BUG_ON(ex->fe_len <= 0);
1759 + BUG_ON(ex->fe_len >= EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1760 + BUG_ON(ex->fe_start >= EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
1761 + BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1766 + * The special case - take what you catch first
1768 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1770 + ext3_mb_use_best_found(ac, e3b);
1775 + * Let's check whether the chuck is good enough
1777 + if (ex->fe_len == gex->fe_len) {
1779 + ext3_mb_use_best_found(ac, e3b);
1784 + * If this is first found extent, just store it in the context
1786 + if (bex->fe_len == 0) {
1792 + * If new found extent is better, store it in the context
1794 + if (bex->fe_len < gex->fe_len) {
1795 + /* if the request isn't satisfied, any found extent
1796 + * larger than previous best one is better */
1797 + if (ex->fe_len > bex->fe_len)
1799 + } else if (ex->fe_len > gex->fe_len) {
1800 + /* if the request is satisfied, then we try to find
1801 + * an extent that still satisfy the request, but is
1802 + * smaller than previous one */
1803 + if (ex->fe_len < bex->fe_len)
1807 + ext3_mb_check_limits(ac, e3b, 0);
1810 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1811 + struct ext3_buddy *e3b)
1813 + struct ext3_free_extent ex = ac->ac_b_ex;
1814 + int group = ex.fe_group, max, err;
1816 + BUG_ON(ex.fe_len <= 0);
1817 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1821 + ext3_lock_group(ac->ac_sb, group);
1822 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1826 + ext3_mb_use_best_found(ac, e3b);
1829 + ext3_unlock_group(ac->ac_sb, group);
1830 + ext3_mb_release_desc(e3b);
1835 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1836 + struct ext3_buddy *e3b)
1838 + int group = ac->ac_g_ex.fe_group, max, err;
1839 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1840 + struct ext3_super_block *es = sbi->s_es;
1841 + struct ext3_free_extent ex;
1843 + if (!(ac->ac_flags & EXT3_MB_HINT_TRY_GOAL))
1846 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1850 + ext3_lock_group(ac->ac_sb, group);
1851 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1852 + ac->ac_g_ex.fe_len, &ex);
1854 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1855 + unsigned long start;
1856 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1857 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1858 + if (start % sbi->s_stripe == 0) {
1861 + ext3_mb_use_best_found(ac, e3b);
1863 + } else if (max >= ac->ac_g_ex.fe_len) {
1864 + BUG_ON(ex.fe_len <= 0);
1865 + BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1866 + BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1869 + ext3_mb_use_best_found(ac, e3b);
1870 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1871 + /* Sometimes, caller may want to merge even small
1872 + * number of blocks to an existing extent */
1873 + BUG_ON(ex.fe_len <= 0);
1874 + BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1875 + BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1878 + ext3_mb_use_best_found(ac, e3b);
1880 + ext3_unlock_group(ac->ac_sb, group);
1881 + ext3_mb_release_desc(e3b);
1887 + * The routine scans buddy structures (not bitmap!) from given order
1888 + * to max order and tries to find big enough chunk to satisfy the req
1890 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1891 + struct ext3_buddy *e3b)
1893 + struct super_block *sb = ac->ac_sb;
1894 + struct ext3_group_info *grp = e3b->bd_info;
1898 + BUG_ON(ac->ac_2order <= 0);
1899 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1900 + if (grp->bb_counters[i] == 0)
1903 + buddy = mb_find_buddy(e3b, i, &max);
1904 + BUG_ON(buddy == NULL);
1906 + k = mb_find_next_zero_bit(buddy, max, 0);
1911 + ac->ac_b_ex.fe_len = 1 << i;
1912 + ac->ac_b_ex.fe_start = k << i;
1913 + ac->ac_b_ex.fe_group = e3b->bd_group;
1915 + ext3_mb_use_best_found(ac, e3b);
1917 + BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1919 + if (EXT3_SB(sb)->s_mb_stats)
1920 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1927 + * The routine scans the group and measures all found extents.
1928 + * In order to optimize scanning, caller must pass number of
1929 + * free blocks in the group, so the routine can know upper limit.
1931 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1932 + struct ext3_buddy *e3b)
1934 + struct super_block *sb = ac->ac_sb;
1935 + void *bitmap = EXT3_MB_BITMAP(e3b);
1936 + struct ext3_free_extent ex;
1939 + free = e3b->bd_info->bb_free;
1940 + BUG_ON(free <= 0);
1942 + i = e3b->bd_info->bb_first_free;
1944 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1945 + i = mb_find_next_zero_bit(bitmap, EXT3_BLOCKS_PER_GROUP(sb), i);
1946 + if (i >= EXT3_BLOCKS_PER_GROUP(sb)) {
1947 + BUG_ON(free != 0);
1951 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1952 + BUG_ON(ex.fe_len <= 0);
1953 + BUG_ON(free < ex.fe_len);
1955 + ext3_mb_measure_extent(ac, &ex, e3b);
1958 + free -= ex.fe_len;
1961 + ext3_mb_check_limits(ac, e3b, 1);
1965 + * This is a special case for storages like raid5
1966 + * we try to find stripe-aligned chunks for stripe-size requests
1968 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1969 + struct ext3_buddy *e3b)
1971 + struct super_block *sb = ac->ac_sb;
1972 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1973 + void *bitmap = EXT3_MB_BITMAP(e3b);
1974 + struct ext3_free_extent ex;
1975 + unsigned long i, max;
1977 + BUG_ON(sbi->s_stripe == 0);
1979 + /* find first stripe-aligned block */
1980 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb)
1981 + + le32_to_cpu(sbi->s_es->s_first_data_block);
1982 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1983 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block))
1984 + % EXT3_BLOCKS_PER_GROUP(sb);
1986 + while (i < EXT3_BLOCKS_PER_GROUP(sb)) {
1987 + if (!mb_test_bit(i, bitmap)) {
1988 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1989 + if (max >= sbi->s_stripe) {
1992 + ext3_mb_use_best_found(ac, e3b);
1996 + i += sbi->s_stripe;
2000 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
2001 + int group, int cr)
2003 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
2004 + unsigned free, fragments, i, bits;
2006 + BUG_ON(cr < 0 || cr >= 4);
2007 + BUG_ON(EXT3_MB_GRP_NEED_INIT(grp));
2009 + free = grp->bb_free;
2010 + fragments = grp->bb_fragments;
2013 + if (fragments == 0)
2018 + BUG_ON(ac->ac_2order == 0);
2019 + bits = ac->ac_sb->s_blocksize_bits + 1;
2020 + for (i = ac->ac_2order; i <= bits; i++)
2021 + if (grp->bb_counters[i] > 0)
2025 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
2029 + if (free >= ac->ac_g_ex.fe_len)
2041 +int ext3_mb_regular_allocator(struct ext3_allocation_context *ac)
2043 + int group, i, cr, err = 0;
2044 + struct ext3_sb_info *sbi;
2045 + struct super_block *sb;
2046 + struct ext3_buddy e3b;
2049 + sbi = EXT3_SB(sb);
2050 + BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2052 + /* first, try the goal */
2053 + err = ext3_mb_find_by_goal(ac, &e3b);
2054 + if (err || ac->ac_status == AC_STATUS_FOUND)
2057 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
2060 + i = ffs(ac->ac_g_ex.fe_len);
2061 + ac->ac_2order = 0;
2062 + if (i >= sbi->s_mb_order2_reqs) {
2064 + if ((ac->ac_g_ex.fe_len & (~(1 << i))) == 0)
2065 + ac->ac_2order = i;
2068 + /* if stream allocation is enabled, use global goal */
2069 + if ((ac->ac_g_ex.fe_len < sbi->s_mb_large_req) &&
2070 + (ac->ac_flags & EXT3_MB_HINT_DATA)) {
2071 + /* TBD: may be hot point */
2072 + spin_lock(&sbi->s_md_lock);
2073 + ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2074 + ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2075 + spin_unlock(&sbi->s_md_lock);
2078 + group = ac->ac_g_ex.fe_group;
2080 + /* Let's just scan groups to find more-less suitable blocks */
2081 + cr = ac->ac_2order ? 0 : 1;
2083 + for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2084 + ac->ac_criteria = cr;
2085 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
2086 + struct ext3_group_info *grp;
2088 + if (group == EXT3_SB(sb)->s_groups_count)
2091 + /* quick check to skip empty groups */
2092 + grp = EXT3_GROUP_INFO(ac->ac_sb, group);
2093 + if (grp->bb_free == 0)
2096 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
2097 + /* we need full data about the group
2098 + * to make a good selection */
2099 + err = ext3_mb_load_buddy(sb, group, &e3b);
2102 + ext3_mb_release_desc(&e3b);
2105 + /* check is group good for our criteries */
2106 + if (!ext3_mb_good_group(ac, group, cr))
2109 + err = ext3_mb_load_buddy(sb, group, &e3b);
2113 + ext3_lock_group(sb, group);
2114 + if (!ext3_mb_good_group(ac, group, cr)) {
2115 + /* someone did allocation from this group */
2116 + ext3_unlock_group(sb, group);
2117 + ext3_mb_release_desc(&e3b);
2121 + ac->ac_groups_scanned++;
2123 + ext3_mb_simple_scan_group(ac, &e3b);
2124 + else if (cr == 1 && ac->ac_g_ex.fe_len == sbi->s_stripe)
2125 + ext3_mb_scan_aligned(ac, &e3b);
2127 + ext3_mb_complex_scan_group(ac, &e3b);
2129 + ext3_unlock_group(sb, group);
2130 + ext3_mb_release_desc(&e3b);
2132 + if (ac->ac_status != AC_STATUS_CONTINUE)
2137 + if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2138 + !(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
2140 + * We've been searching too long. Let's try to allocate
2141 + * the best chunk we've found so far
2144 + ext3_mb_try_best_found(ac, &e3b);
2145 + if (ac->ac_status != AC_STATUS_FOUND) {
2147 + * Someone more lucky has already allocated it.
2148 + * The only thing we can do is just take first
2150 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
2152 + ac->ac_b_ex.fe_group = 0;
2153 + ac->ac_b_ex.fe_start = 0;
2154 + ac->ac_b_ex.fe_len = 0;
2155 + ac->ac_status = AC_STATUS_CONTINUE;
2156 + ac->ac_flags |= EXT3_MB_HINT_FIRST;
2158 + atomic_inc(&sbi->s_mb_lost_chunks);
2166 +#ifdef EXT3_MB_HISTORY
2167 +struct ext3_mb_proc_session {
2168 + struct ext3_mb_history *history;
2169 + struct super_block *sb;
2174 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
2175 + struct ext3_mb_history *hs,
2178 + if (hs == s->history + s->max)
2180 + if (!first && hs == s->history + s->start)
2182 + while (hs->orig.fe_len == 0) {
2184 + if (hs == s->history + s->max)
2186 + if (hs == s->history + s->start)
2192 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2194 + struct ext3_mb_proc_session *s = seq->private;
2195 + struct ext3_mb_history *hs;
2199 + return SEQ_START_TOKEN;
2200 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2203 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2207 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2209 + struct ext3_mb_proc_session *s = seq->private;
2210 + struct ext3_mb_history *hs = v;
2213 + if (v == SEQ_START_TOKEN)
2214 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2216 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2219 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2221 + char buf[25], buf2[25], buf3[25], *fmt;
2222 + struct ext3_mb_history *hs = v;
2224 + if (v == SEQ_START_TOKEN) {
2225 + seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2226 + "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2227 + "pid", "inode", "original", "goal", "result","found",
2228 + "grps", "cr", "flags", "merge", "tail", "broken");
2232 + if (hs->op == EXT3_MB_HISTORY_ALLOC) {
2233 + fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2234 + "%-5u %-5s %-5u %-6u\n";
2235 + sprintf(buf2, "%lu/%lu/%lu@%lu", hs->result.fe_group,
2236 + hs->result.fe_start, hs->result.fe_len,
2237 + hs->result.fe_logical);
2238 + sprintf(buf, "%lu/%lu/%lu@%lu", hs->orig.fe_group,
2239 + hs->orig.fe_start, hs->orig.fe_len,
2240 + hs->orig.fe_logical);
2241 + sprintf(buf3, "%lu/%lu/%lu@%lu", hs->goal.fe_group,
2242 + hs->goal.fe_start, hs->goal.fe_len,
2243 + hs->goal.fe_logical);
2244 + seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2245 + hs->found, hs->groups, hs->cr, hs->flags,
2246 + hs->merged ? "M" : "", hs->tail,
2247 + hs->buddy ? 1 << hs->buddy : 0);
2248 + } else if (hs->op == EXT3_MB_HISTORY_PREALLOC) {
2249 + fmt = "%-5u %-8u %-23s %-23s %-23s\n";
2250 + sprintf(buf2, "%lu/%lu/%lu@%lu", hs->result.fe_group,
2251 + hs->result.fe_start, hs->result.fe_len,
2252 + hs->result.fe_logical);
2253 + sprintf(buf, "%lu/%lu/%lu@%lu", hs->orig.fe_group,
2254 + hs->orig.fe_start, hs->orig.fe_len,
2255 + hs->orig.fe_logical);
2256 + seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2257 + } else if (hs->op == EXT3_MB_HISTORY_DISCARD) {
2258 + sprintf(buf2, "%lu/%lu/%lu", hs->result.fe_group,
2259 + hs->result.fe_start, hs->result.fe_len);
2260 + seq_printf(seq, "%-5u %-8u %-23s discard\n",
2261 + hs->pid, hs->ino, buf2);
2262 + } else if (hs->op == EXT3_MB_HISTORY_FREE) {
2263 + sprintf(buf2, "%lu/%lu/%lu", hs->result.fe_group,
2264 + hs->result.fe_start, hs->result.fe_len);
2265 + seq_printf(seq, "%-5u %-8u %-23s free\n",
2266 + hs->pid, hs->ino, buf2);
2271 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2275 +static struct seq_operations ext3_mb_seq_history_ops = {
2276 + .start = ext3_mb_seq_history_start,
2277 + .next = ext3_mb_seq_history_next,
2278 + .stop = ext3_mb_seq_history_stop,
2279 + .show = ext3_mb_seq_history_show,
2282 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2284 + struct super_block *sb = PDE(inode)->data;
2285 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2286 + struct ext3_mb_proc_session *s;
2289 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2293 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2294 + s->history = kmalloc(size, GFP_KERNEL);
2295 + if (s->history == NULL) {
2300 + spin_lock(&sbi->s_mb_history_lock);
2301 + memcpy(s->history, sbi->s_mb_history, size);
2302 + s->max = sbi->s_mb_history_max;
2303 + s->start = sbi->s_mb_history_cur % s->max;
2304 + spin_unlock(&sbi->s_mb_history_lock);
2306 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2308 + struct seq_file *m = (struct seq_file *)file->private_data;
2311 + kfree(s->history);
2318 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2320 + struct seq_file *seq = (struct seq_file *)file->private_data;
2321 + struct ext3_mb_proc_session *s = seq->private;
2322 + kfree(s->history);
2324 + return seq_release(inode, file);
2327 +static ssize_t ext3_mb_seq_history_write(struct file *file,
2328 + const char __user *buffer,
2329 + size_t count, loff_t *ppos)
2331 + struct seq_file *seq = (struct seq_file *)file->private_data;
2332 + struct ext3_mb_proc_session *s = seq->private;
2333 + struct super_block *sb = s->sb;
2337 + if (count >= sizeof(str)) {
2338 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2339 + "mb_history", (int)sizeof(str));
2340 + return -EOVERFLOW;
2343 + if (copy_from_user(str, buffer, count))
2346 + value = simple_strtol(str, NULL, 0);
2349 + EXT3_SB(sb)->s_mb_history_filter = value;
2354 +static struct file_operations ext3_mb_seq_history_fops = {
2355 + .owner = THIS_MODULE,
2356 + .open = ext3_mb_seq_history_open,
2358 + .write = ext3_mb_seq_history_write,
2359 + .llseek = seq_lseek,
2360 + .release = ext3_mb_seq_history_release,
2363 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2365 + struct super_block *sb = seq->private;
2366 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2369 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2373 + return (void *) group;
2376 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2378 + struct super_block *sb = seq->private;
2379 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2383 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2386 + return (void *) group;;
2389 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2391 + struct super_block *sb = seq->private;
2392 + long group = (long) v;
2394 + struct ext3_buddy e3b;
2396 + struct ext3_group_info info;
2397 + unsigned short counters[16];
2402 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2403 + "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2404 + "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2405 + "group", "free", "frags", "first",
2406 + "2^0", "2^1", "2^2", "2^3", "2^4", "2^5","2^6",
2407 + "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2409 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2410 + sizeof(struct ext3_group_info);
2411 + err = ext3_mb_load_buddy(sb, group, &e3b);
2413 + seq_printf(seq, "#%-5lu: I/O error\n", group);
2416 + ext3_lock_group(sb, group);
2417 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2418 + ext3_unlock_group(sb, group);
2419 + ext3_mb_release_desc(&e3b);
2421 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2422 + sg.info.bb_fragments, sg.info.bb_first_free);
2423 + for (i = 0; i <= 13; i++)
2424 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2425 + sg.info.bb_counters[i] : 0);
2426 + seq_printf(seq, " ]\n");
2431 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2435 +static struct seq_operations ext3_mb_seq_groups_ops = {
2436 + .start = ext3_mb_seq_groups_start,
2437 + .next = ext3_mb_seq_groups_next,
2438 + .stop = ext3_mb_seq_groups_stop,
2439 + .show = ext3_mb_seq_groups_show,
2442 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2444 + struct super_block *sb = PDE(inode)->data;
2447 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2449 + struct seq_file *m = (struct seq_file *)file->private_data;
2456 +static struct file_operations ext3_mb_seq_groups_fops = {
2457 + .owner = THIS_MODULE,
2458 + .open = ext3_mb_seq_groups_open,
2460 + .llseek = seq_lseek,
2461 + .release = seq_release,
2464 +static void ext3_mb_history_release(struct super_block *sb)
2466 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2468 + remove_proc_entry("mb_groups", sbi->s_dev_proc);
2469 + remove_proc_entry("mb_history", sbi->s_dev_proc);
2471 + if (sbi->s_mb_history)
2472 + kfree(sbi->s_mb_history);
2475 +static void ext3_mb_history_init(struct super_block *sb)
2477 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2480 + if (sbi->s_dev_proc != NULL) {
2481 + struct proc_dir_entry *p;
2482 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_dev_proc);
2484 + p->proc_fops = &ext3_mb_seq_history_fops;
2487 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_dev_proc);
2489 + p->proc_fops = &ext3_mb_seq_groups_fops;
2494 + sbi->s_mb_history_max = 1000;
2495 + sbi->s_mb_history_cur = 0;
2496 + spin_lock_init(&sbi->s_mb_history_lock);
2497 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2498 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2499 + if (likely(sbi->s_mb_history != NULL))
2500 + memset(sbi->s_mb_history, 0, i);
2501 + /* if we can't allocate history, then we simple won't use it */
2505 +ext3_mb_store_history(struct ext3_allocation_context *ac)
2507 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
2508 + struct ext3_mb_history h;
2510 + if (unlikely(sbi->s_mb_history == NULL))
2513 + if (!(ac->ac_op & sbi->s_mb_history_filter))
2517 + h.pid = current->pid;
2518 + h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2519 + h.orig = ac->ac_o_ex;
2520 + h.result = ac->ac_b_ex;
2521 + h.flags = ac->ac_flags;
2522 + h.found = ac->ac_found;
2523 + h.groups = ac->ac_groups_scanned;
2524 + h.cr = ac->ac_criteria;
2525 + h.tail = ac->ac_tail;
2526 + h.buddy = ac->ac_buddy;
2528 + if (ac->ac_op == EXT3_MB_HISTORY_ALLOC) {
2529 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2530 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2532 + h.goal = ac->ac_g_ex;
2533 + h.result = ac->ac_f_ex;
2536 + spin_lock(&sbi->s_mb_history_lock);
2537 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2538 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2539 + sbi->s_mb_history_cur = 0;
2540 + spin_unlock(&sbi->s_mb_history_lock);
2544 +#define ext3_mb_history_release(sb)
2545 +#define ext3_mb_history_init(sb)
2548 +int ext3_mb_init_backend(struct super_block *sb)
2550 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2551 + int i, j, len, metalen;
2552 + int num_meta_group_infos =
2553 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2554 + EXT3_DESC_PER_BLOCK_BITS(sb);
2555 + struct ext3_group_info **meta_group_info;
2557 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2558 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2559 + * So a two level scheme suffices for now. */
2560 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2561 + num_meta_group_infos, GFP_KERNEL);
2562 + if (sbi->s_group_info == NULL) {
2563 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2566 + sbi->s_buddy_cache = new_inode(sb);
2567 + if (sbi->s_buddy_cache == NULL) {
2568 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2571 + EXT3_I(sbi->s_buddy_cache)->i_disksize = 0;
2573 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2574 + for (i = 0; i < num_meta_group_infos; i++) {
2575 + if ((i + 1) == num_meta_group_infos)
2576 + metalen = sizeof(*meta_group_info) *
2577 + (sbi->s_groups_count -
2578 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2579 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2580 + if (meta_group_info == NULL) {
2581 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2583 + goto err_freemeta;
2585 + sbi->s_group_info[i] = meta_group_info;
2589 + * calculate needed size. if change bb_counters size,
2590 + * don't forget about ext3_mb_generate_buddy()
2592 + len = sizeof(struct ext3_group_info);
2593 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2594 + for (i = 0; i < sbi->s_groups_count; i++) {
2595 + struct ext3_group_desc * desc;
2598 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2599 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2601 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2602 + if (meta_group_info[j] == NULL) {
2603 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2605 + goto err_freebuddy;
2607 + desc = ext3_get_group_desc(sb, i, NULL);
2608 + if (desc == NULL) {
2609 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2610 + goto err_freebuddy;
2612 + memset(meta_group_info[j], 0, len);
2613 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2614 + &meta_group_info[j]->bb_state);
2616 + /* initialize bb_free to be able to skip
2617 + * empty groups without initialization */
2618 + meta_group_info[j]->bb_free =
2619 + le16_to_cpu(desc->bg_free_blocks_count);
2621 + INIT_LIST_HEAD(&meta_group_info[j]->bb_prealloc_list);
2623 +#ifdef DOUBLE_CHECK
2625 + struct buffer_head *bh;
2626 + meta_group_info[j]->bb_bitmap =
2627 + kmalloc(sb->s_blocksize, GFP_KERNEL);
2628 + BUG_ON(meta_group_info[j]->bb_bitmap == NULL);
2629 + bh = read_block_bitmap(sb, i);
2630 + BUG_ON(bh == NULL);
2631 + memcpy(meta_group_info[j]->bb_bitmap, bh->b_data,
2643 + kfree(EXT3_GROUP_INFO(sb, i));
2646 + i = num_meta_group_infos;
2649 + kfree(sbi->s_group_info[i]);
2650 + iput(sbi->s_buddy_cache);
2652 + kfree(sbi->s_group_info);
2656 +static void ext3_mb_prealloc_table_add(struct ext3_sb_info *sbi, int value)
2660 + if (value > (sbi->s_blocks_per_group - 1 - 1 - sbi->s_itb_per_group))
2663 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
2664 + if (sbi->s_mb_prealloc_table[i] == 0) {
2665 + sbi->s_mb_prealloc_table[i] = value;
2669 + /* they should add values in order */
2670 + if (value <= sbi->s_mb_prealloc_table[i])
2675 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2677 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2678 + unsigned i, offset, max;
2680 + if (!test_opt(sb, MBALLOC))
2683 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2685 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2686 + if (sbi->s_mb_offsets == NULL) {
2687 + clear_opt(sbi->s_mount_opt, MBALLOC);
2690 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2691 + if (sbi->s_mb_maxs == NULL) {
2692 + clear_opt(sbi->s_mount_opt, MBALLOC);
2693 + kfree(sbi->s_mb_maxs);
2697 + /* order 0 is regular bitmap */
2698 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2699 + sbi->s_mb_offsets[0] = 0;
2703 + max = sb->s_blocksize << 2;
2705 + sbi->s_mb_offsets[i] = offset;
2706 + sbi->s_mb_maxs[i] = max;
2707 + offset += 1 << (sb->s_blocksize_bits - i);
2710 + } while (i <= sb->s_blocksize_bits + 1);
2712 + /* init file for buddy data */
2713 + if ((i = ext3_mb_init_backend(sb))) {
2714 + clear_opt(sbi->s_mount_opt, MBALLOC);
2715 + kfree(sbi->s_mb_offsets);
2716 + kfree(sbi->s_mb_maxs);
2720 + spin_lock_init(&sbi->s_md_lock);
2721 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2722 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2723 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2724 + spin_lock_init(&sbi->s_bal_lock);
2726 + sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2727 + sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2728 + sbi->s_mb_max_groups_to_scan = MB_DEFAULT_MAX_GROUPS_TO_SCAN;
2729 + sbi->s_mb_stats = MB_DEFAULT_STATS;
2730 + sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2731 + sbi->s_mb_history_filter = EXT3_MB_HISTORY_DEFAULT;
2733 + if (sbi->s_stripe == 0) {
2734 + sbi->s_mb_prealloc_table_size = 8;
2735 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
2736 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
2737 + if (sbi->s_mb_prealloc_table == NULL) {
2738 + clear_opt(sbi->s_mount_opt, MBALLOC);
2739 + kfree(sbi->s_mb_offsets);
2740 + kfree(sbi->s_mb_maxs);
2743 + memset(sbi->s_mb_prealloc_table, 0, i);
2745 + ext3_mb_prealloc_table_add(sbi, 4);
2746 + ext3_mb_prealloc_table_add(sbi, 8);
2747 + ext3_mb_prealloc_table_add(sbi, 16);
2748 + ext3_mb_prealloc_table_add(sbi, 32);
2749 + ext3_mb_prealloc_table_add(sbi, 64);
2750 + ext3_mb_prealloc_table_add(sbi, 128);
2751 + ext3_mb_prealloc_table_add(sbi, 256);
2752 + ext3_mb_prealloc_table_add(sbi, 512);
2754 + sbi->s_mb_small_req = 256;
2755 + sbi->s_mb_large_req = 1024;
2756 + sbi->s_mb_group_prealloc = 512;
2758 + sbi->s_mb_prealloc_table_size = 3;
2759 + i = sbi->s_mb_prealloc_table_size * sizeof(unsigned long);
2760 + sbi->s_mb_prealloc_table = kmalloc(i, GFP_NOFS);
2761 + if (sbi->s_mb_prealloc_table == NULL) {
2762 + clear_opt(sbi->s_mount_opt, MBALLOC);
2763 + kfree(sbi->s_mb_offsets);
2764 + kfree(sbi->s_mb_maxs);
2767 + memset(sbi->s_mb_prealloc_table, 0, i);
2769 + ext3_mb_prealloc_table_add(sbi, sbi->s_stripe);
2770 + ext3_mb_prealloc_table_add(sbi, sbi->s_stripe * 2);
2771 + ext3_mb_prealloc_table_add(sbi, sbi->s_stripe * 4);
2773 + sbi->s_mb_small_req = sbi->s_stripe;
2774 + sbi->s_mb_large_req = sbi->s_stripe * 8;
2775 + sbi->s_mb_group_prealloc = sbi->s_stripe * 4;
2778 + i = sizeof(struct ext3_locality_group) * num_possible_cpus();
2779 + sbi->s_locality_groups = kmalloc(i, GFP_NOFS);
2780 + if (sbi->s_locality_groups == NULL) {
2781 + clear_opt(sbi->s_mount_opt, MBALLOC);
2782 + kfree(sbi->s_mb_prealloc_table);
2783 + kfree(sbi->s_mb_offsets);
2784 + kfree(sbi->s_mb_maxs);
2787 + for (i = 0; i < num_possible_cpus(); i++) {
2788 + struct ext3_locality_group *lg;
2789 + lg = &sbi->s_locality_groups[i];
2790 + sema_init(&lg->lg_sem, 1);
2791 + INIT_LIST_HEAD(&lg->lg_prealloc_list);
2792 + spin_lock_init(&lg->lg_prealloc_lock);
2795 + ext3_mb_init_per_dev_proc(sb);
2796 + ext3_mb_history_init(sb);
2798 + printk("EXT3-fs: mballoc enabled\n");
2802 +void ext3_mb_cleanup_pa(struct ext3_group_info *grp)
2804 + struct ext3_prealloc_space *pa;
2805 + struct list_head *cur, *tmp;
2808 + list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2809 + pa = list_entry(cur, struct ext3_prealloc_space, pa_group_list);
2810 + list_del_rcu(&pa->pa_group_list);
2815 + mb_debug("mballoc: %u PAs left\n", count);
2819 +int ext3_mb_release(struct super_block *sb)
2821 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2822 + int i, num_meta_group_infos;
2824 + if (!test_opt(sb, MBALLOC))
2827 + /* release freed, non-committed blocks */
2828 + spin_lock(&sbi->s_md_lock);
2829 + list_splice_init(&sbi->s_closed_transaction,
2830 + &sbi->s_committed_transaction);
2831 + list_splice_init(&sbi->s_active_transaction,
2832 + &sbi->s_committed_transaction);
2833 + spin_unlock(&sbi->s_md_lock);
2834 + ext3_mb_free_committed_blocks(sb);
2836 + if (sbi->s_group_info) {
2837 + for (i = 0; i < sbi->s_groups_count; i++) {
2838 +#ifdef DOUBLE_CHECK
2839 + if (EXT3_GROUP_INFO(sb, i)->bb_bitmap)
2840 + kfree(EXT3_GROUP_INFO(sb, i)->bb_bitmap);
2842 + ext3_mb_cleanup_pa(EXT3_GROUP_INFO(sb, i));
2843 + kfree(EXT3_GROUP_INFO(sb, i));
2845 + num_meta_group_infos = (sbi->s_groups_count +
2846 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2847 + EXT3_DESC_PER_BLOCK_BITS(sb);
2848 + for (i = 0; i < num_meta_group_infos; i++)
2849 + kfree(sbi->s_group_info[i]);
2850 + kfree(sbi->s_group_info);
2852 + if (sbi->s_mb_offsets)
2853 + kfree(sbi->s_mb_offsets);
2854 + if (sbi->s_mb_maxs)
2855 + kfree(sbi->s_mb_maxs);
2856 + if (sbi->s_buddy_cache)
2857 + iput(sbi->s_buddy_cache);
2858 + if (sbi->s_mb_stats) {
2859 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2860 + atomic_read(&sbi->s_bal_allocated),
2861 + atomic_read(&sbi->s_bal_reqs),
2862 + atomic_read(&sbi->s_bal_success));
2863 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2864 + "%u 2^N hits, %u breaks, %u lost\n",
2865 + atomic_read(&sbi->s_bal_ex_scanned),
2866 + atomic_read(&sbi->s_bal_goals),
2867 + atomic_read(&sbi->s_bal_2orders),
2868 + atomic_read(&sbi->s_bal_breaks),
2869 + atomic_read(&sbi->s_mb_lost_chunks));
2870 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2871 + sbi->s_mb_buddies_generated++,
2872 + sbi->s_mb_generation_time);
2873 + printk("EXT3-fs: mballoc: %u preallocated, %u discarded\n",
2874 + atomic_read(&sbi->s_mb_preallocated),
2875 + atomic_read(&sbi->s_mb_discarded));
2878 + if (sbi->s_locality_groups)
2879 + kfree(sbi->s_locality_groups);
2881 + ext3_mb_history_release(sb);
2882 + ext3_mb_destroy_per_dev_proc(sb);
2887 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2889 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2890 + int err, i, count = 0, count2 = 0;
2891 + struct ext3_free_metadata *md;
2892 + struct ext3_buddy e3b;
2894 + if (list_empty(&sbi->s_committed_transaction))
2897 + /* there is committed blocks to be freed yet */
2899 + /* get next array of blocks */
2901 + spin_lock(&sbi->s_md_lock);
2902 + if (!list_empty(&sbi->s_committed_transaction)) {
2903 + md = list_entry(sbi->s_committed_transaction.next,
2904 + struct ext3_free_metadata, list);
2905 + list_del(&md->list);
2907 + spin_unlock(&sbi->s_md_lock);
2912 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2913 + md->num, md->group, md);
2915 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2916 + /* we expect to find existing buddy because it's pinned */
2919 + /* there are blocks to put in buddy to make them really free */
2922 + ext3_lock_group(sb, md->group);
2923 + for (i = 0; i < md->num; i++) {
2924 + mb_debug(" %u", md->blocks[i]);
2925 + err = mb_free_blocks(NULL, &e3b, md->blocks[i], 1);
2929 + ext3_unlock_group(sb, md->group);
2931 + /* balance refcounts from ext3_mb_free_metadata() */
2932 + page_cache_release(e3b.bd_buddy_page);
2933 + page_cache_release(e3b.bd_bitmap_page);
2936 + ext3_mb_release_desc(&e3b);
2940 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2943 +#define EXT3_MB_STATS_NAME "stats"
2944 +#define EXT3_MB_MAX_TO_SCAN_NAME "max_to_scan"
2945 +#define EXT3_MB_MIN_TO_SCAN_NAME "min_to_scan"
2946 +#define EXT3_MB_ORDER2_REQ "order2_req"
2947 +#define EXT3_MB_SMALL_REQ "small_req"
2948 +#define EXT3_MB_LARGE_REQ "large_req"
2949 +#define EXT3_MB_PREALLOC_TABLE "prealloc_table"
2950 +#define EXT3_MB_GROUP_PREALLOC "group_prealloc"
2952 +static int ext3_mb_read_prealloc_table(char *page, char **start, off_t off,
2953 + int count, int *eof, void *data)
2955 + struct ext3_sb_info *sbi = data;
2963 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++)
2964 + len += sprintf(page + len, "%ld ",
2965 + sbi->s_mb_prealloc_table[i]);
2966 + len += sprintf(page + len, "\n");
2972 +static int ext3_mb_write_prealloc_table(struct file *file,
2973 + const char __user *buf,
2974 + unsigned long cnt, void *data)
2976 + struct ext3_sb_info *sbi = data;
2977 + unsigned long value;
2978 + unsigned long prev = 0;
2982 + unsigned long *new_table;
2986 + if (cnt >= sizeof(str))
2988 + if (copy_from_user(str, buf, cnt))
2994 + while (cur < end) {
2995 + while ((cur < end) && (*cur == ' ')) cur++;
2996 + value = simple_strtol(cur, &cur, 0);
2999 + if (value <= prev)
3005 + new_table = kmalloc(num * sizeof(*new_table), GFP_KERNEL);
3006 + if (new_table == NULL)
3008 + kfree(sbi->s_mb_prealloc_table);
3009 + memset(new_table, 0, num * sizeof(*new_table));
3010 + sbi->s_mb_prealloc_table = new_table;
3011 + sbi->s_mb_prealloc_table_size = num;
3014 + while (cur < end && i < num) {
3015 + while ((cur < end) && (*cur == ' ')) cur++;
3016 + value = simple_strtol(cur, &cur, 0);
3017 + ext3_mb_prealloc_table_add(sbi, value);
3024 +#define MB_PROC_VALUE_READ(name) \
3025 +static int ext3_mb_read_##name(char *page, char **start, \
3026 + off_t off, int count, int *eof, void *data) \
3028 + struct ext3_sb_info *sbi = data; \
3033 + len = sprintf(page, "%ld\n", sbi->s_mb_##name); \
3038 +#define MB_PROC_VALUE_WRITE(name) \
3039 +static int ext3_mb_write_##name(struct file *file, \
3040 + const char __user *buf, unsigned long cnt, void *data) \
3042 + struct ext3_sb_info *sbi = data; \
3045 + if (cnt >= sizeof(str)) \
3047 + if (copy_from_user(str, buf, cnt)) \
3049 + value = simple_strtol(str, NULL, 0); \
3052 + sbi->s_mb_##name = value; \
3056 +MB_PROC_VALUE_READ(stats);
3057 +MB_PROC_VALUE_WRITE(stats);
3058 +MB_PROC_VALUE_READ(max_to_scan);
3059 +MB_PROC_VALUE_WRITE(max_to_scan);
3060 +MB_PROC_VALUE_READ(min_to_scan);
3061 +MB_PROC_VALUE_WRITE(min_to_scan);
3062 +MB_PROC_VALUE_READ(order2_reqs);
3063 +MB_PROC_VALUE_WRITE(order2_reqs);
3064 +MB_PROC_VALUE_READ(small_req);
3065 +MB_PROC_VALUE_WRITE(small_req);
3066 +MB_PROC_VALUE_READ(large_req);
3067 +MB_PROC_VALUE_WRITE(large_req);
3068 +MB_PROC_VALUE_READ(group_prealloc);
3069 +MB_PROC_VALUE_WRITE(group_prealloc);
3071 +#define MB_PROC_HANDLER(name, var) \
3073 + proc = create_proc_entry(name, mode, sbi->s_dev_proc); \
3074 + if (proc == NULL) { \
3075 + printk(KERN_ERR "EXT3-fs: can't to create %s\n", name); \
3078 + proc->data = sbi; \
3079 + proc->read_proc = ext3_mb_read_##var ; \
3080 + proc->write_proc = ext3_mb_write_##var; \
3083 +int ext3_mb_init_per_dev_proc(struct super_block *sb)
3085 + struct ext3_sb_info *sbi = EXT3_SB(sb);
3086 + mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
3087 + struct proc_dir_entry *proc;
3089 + MB_PROC_HANDLER(EXT3_MB_STATS_NAME, stats);
3090 + MB_PROC_HANDLER(EXT3_MB_MAX_TO_SCAN_NAME, max_to_scan);
3091 + MB_PROC_HANDLER(EXT3_MB_MIN_TO_SCAN_NAME, min_to_scan);
3092 + MB_PROC_HANDLER(EXT3_MB_ORDER2_REQ, order2_reqs);
3093 + MB_PROC_HANDLER(EXT3_MB_SMALL_REQ, small_req);
3094 + MB_PROC_HANDLER(EXT3_MB_LARGE_REQ, large_req);
3095 + MB_PROC_HANDLER(EXT3_MB_PREALLOC_TABLE, prealloc_table);
3096 + MB_PROC_HANDLER(EXT3_MB_GROUP_PREALLOC, group_prealloc);
3101 + remove_proc_entry(EXT3_MB_GROUP_PREALLOC, sbi->s_dev_proc);
3102 + remove_proc_entry(EXT3_MB_PREALLOC_TABLE, sbi->s_dev_proc);
3103 + remove_proc_entry(EXT3_MB_LARGE_REQ, sbi->s_dev_proc);
3104 + remove_proc_entry(EXT3_MB_SMALL_REQ, sbi->s_dev_proc);
3105 + remove_proc_entry(EXT3_MB_ORDER2_REQ, sbi->s_dev_proc);
3106 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, sbi->s_dev_proc);
3107 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, sbi->s_dev_proc);
3108 + remove_proc_entry(EXT3_MB_STATS_NAME, sbi->s_dev_proc);
3113 +int ext3_mb_destroy_per_dev_proc(struct super_block *sb)
3115 + struct ext3_sb_info *sbi = EXT3_SB(sb);
3117 + if (sbi->s_dev_proc == NULL)
3120 + remove_proc_entry(EXT3_MB_GROUP_PREALLOC, sbi->s_dev_proc);
3121 + remove_proc_entry(EXT3_MB_PREALLOC_TABLE, sbi->s_dev_proc);
3122 + remove_proc_entry(EXT3_MB_SMALL_REQ, sbi->s_dev_proc);
3123 + remove_proc_entry(EXT3_MB_LARGE_REQ, sbi->s_dev_proc);
3124 + remove_proc_entry(EXT3_MB_ORDER2_REQ, sbi->s_dev_proc);
3125 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, sbi->s_dev_proc);
3126 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, sbi->s_dev_proc);
3127 + remove_proc_entry(EXT3_MB_STATS_NAME, sbi->s_dev_proc);
3132 +int __init init_ext3_mb_proc(void)
3134 + ext3_pspace_cachep =
3135 + kmem_cache_create("ext3_prealloc_space",
3136 + sizeof(struct ext3_prealloc_space),
3137 + 0, SLAB_RECLAIM_ACCOUNT, NULL, NULL);
3138 + if (ext3_pspace_cachep == NULL)
3144 +void exit_ext3_mb_proc(void)
3146 + /* XXX: synchronize_rcu(); */
3147 + kmem_cache_destroy(ext3_pspace_cachep);
3152 + * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
3153 + * Returns 0 if success or error code
3155 +int ext3_mb_mark_diskspace_used(struct ext3_allocation_context *ac, handle_t *handle)
3157 + struct buffer_head *bitmap_bh = NULL;
3158 + struct ext3_super_block *es;
3159 + struct ext3_group_desc *gdp;
3160 + struct buffer_head *gdp_bh;
3161 + struct ext3_sb_info *sbi;
3162 + struct super_block *sb;
3166 + BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3167 + BUG_ON(ac->ac_b_ex.fe_len <= 0);
3170 + sbi = EXT3_SB(sb);
3173 + ext3_debug("using block group %d(%d)\n", ac->ac_b_group.group,
3174 + gdp->bg_free_blocks_count);
3177 + bitmap_bh = read_block_bitmap(sb, ac->ac_b_ex.fe_group);
3181 + err = ext3_journal_get_write_access(handle, bitmap_bh);
3186 + gdp = ext3_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
3190 + err = ext3_journal_get_write_access(handle, gdp_bh);
3194 + block = ac->ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
3195 + + ac->ac_b_ex.fe_start
3196 + + le32_to_cpu(es->s_first_data_block);
3198 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
3199 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
3200 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
3201 + EXT3_SB(sb)->s_itb_per_group))
3202 + ext3_error(sb, __FUNCTION__,
3203 + "Allocating block in system zone - block = %lu",
3204 + (unsigned long) block);
3205 +#ifdef AGGRESSIVE_CHECK
3208 + for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3209 + BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3210 + bitmap_bh->b_data));
3214 + mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
3215 + ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
3217 + spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3218 + gdp->bg_free_blocks_count =
3219 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
3220 + - ac->ac_b_ex.fe_len);
3221 + spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3222 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac->ac_b_ex.fe_len);
3224 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
3227 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
3231 + brelse(bitmap_bh);
3236 + * here we normalize request for locality group
3237 + * XXX: should we try to preallocate more than the group has now?
3239 +void ext3_mb_normalize_group_request(struct ext3_allocation_context *ac)
3241 + struct super_block *sb = ac->ac_sb;
3242 + struct ext3_locality_group *lg = ac->ac_lg;
3244 + BUG_ON(lg == NULL);
3245 + ac->ac_g_ex.fe_len = EXT3_SB(sb)->s_mb_group_prealloc;
3247 + mb_debug("#%u: goal %u blocks for locality group\n",
3248 + current->pid, ac->ac_g_ex.fe_len);
3252 + * Normalization means making request better in terms of
3253 + * size and alignment
3255 +void ext3_mb_normalize_request(struct ext3_allocation_context *ac,
3256 + struct ext3_allocation_request *ar)
3258 + struct ext3_inode_info *ei = EXT3_I(ac->ac_inode);
3259 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
3260 + loff_t start, end, size, orig_size, orig_start;
3261 + struct list_head *cur;
3262 + int bsbits, i, wind;
3264 + /* do normalize only data requests, metadata requests
3265 + do not need preallocation */
3266 + if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
3269 + /* sometime caller may want exact blocks */
3270 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
3273 + /* caller may indicate that preallocation isn't
3274 + * required (it's a tail, for example) */
3275 + if (ac->ac_flags & EXT3_MB_HINT_NOPREALLOC)
3278 + if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
3279 + return ext3_mb_normalize_group_request(ac);
3281 + bsbits = ac->ac_sb->s_blocksize_bits;
3283 + /* first, let's learn actual file size
3284 + * given current request is allocated */
3285 + size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3286 + size = size << bsbits;
3287 + if (size < i_size_read(ac->ac_inode))
3288 + size = i_size_read(ac->ac_inode);
3289 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
3294 + /* let's choose preallocation window depending on file size */
3295 + for (i = 0; i < sbi->s_mb_prealloc_table_size; i++) {
3296 + if (size <= sbi->s_mb_prealloc_table[i]) {
3297 + wind = sbi->s_mb_prealloc_table[i];
3304 + __u64 tstart, tend;
3305 + /* file is quite large, we now preallocate with
3306 + * the biggest configured window with regart to
3307 + * logical offset */
3308 + wind = sbi->s_mb_prealloc_table[i - 1];
3309 + tstart = ac->ac_o_ex.fe_logical;
3310 + do_div(tstart, wind);
3311 + start = tstart * wind;
3312 + tend = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len - 1;
3313 + do_div(tend, wind);
3314 + tend = tend * wind + wind;
3315 + size = tend - start;
3318 + orig_start = start;
3320 + /* don't cover already allocated blocks in selected range */
3321 + if (ar->pleft && start <= ar->lleft) {
3322 + size -= ar->lleft + 1 - start;
3323 + start = ar->lleft + 1;
3325 + if (ar->pright && start + size - 1 >= ar->lright)
3326 + size -= start + size - ar->lright;
3328 + end = start + size;
3330 + /* check we don't cross already preallocated blocks */
3332 + list_for_each_rcu(cur, &ei->i_prealloc_list) {
3333 + struct ext3_prealloc_space *pa;
3334 + unsigned long pa_end;
3336 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3338 + if (pa->pa_deleted)
3340 + spin_lock(&pa->pa_lock);
3341 + if (pa->pa_deleted) {
3342 + spin_unlock(&pa->pa_lock);
3346 + pa_end = pa->pa_lstart + pa->pa_len;
3348 + /* PA must not overlap original request */
3349 + BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3350 + ac->ac_o_ex.fe_logical < pa->pa_lstart));
3352 + /* skip PA normalized request doesn't overlap with */
3353 + if (pa->pa_lstart >= end) {
3354 + spin_unlock(&pa->pa_lock);
3357 + if (pa_end <= start) {
3358 + spin_unlock(&pa->pa_lock);
3361 + BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3363 + if (pa_end <= ac->ac_o_ex.fe_logical) {
3364 + BUG_ON(pa_end < start);
3368 + if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3369 + BUG_ON(pa->pa_lstart > end);
3370 + end = pa->pa_lstart;
3372 + spin_unlock(&pa->pa_lock);
3374 + rcu_read_unlock();
3375 + size = end - start;
3377 + /* XXX: extra loop to check we really don't overlap preallocations */
3379 + list_for_each_rcu(cur, &ei->i_prealloc_list) {
3380 + struct ext3_prealloc_space *pa;
3381 + unsigned long pa_end;
3382 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3383 + spin_lock(&pa->pa_lock);
3384 + if (pa->pa_deleted == 0) {
3385 + pa_end = pa->pa_lstart + pa->pa_len;
3386 + BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3388 + spin_unlock(&pa->pa_lock);
3390 + rcu_read_unlock();
3392 + if (start + size <= ac->ac_o_ex.fe_logical &&
3393 + start > ac->ac_o_ex.fe_logical) {
3394 + printk("start %lu, size %lu, fe_logical %lu\n",
3395 + (unsigned long) start, (unsigned long) size,
3396 + (unsigned long) ac->ac_o_ex.fe_logical);
3398 + BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3399 + start > ac->ac_o_ex.fe_logical);
3401 + /* now prepare goal request */
3403 + /* XXX: is it better to align blocks WRT to logical
3404 + * placement or satisfy big request as is */
3405 + ac->ac_g_ex.fe_logical = start;
3406 + ac->ac_g_ex.fe_len = size;
3408 + /* define goal start in order to merge */
3409 + if (ar->pright && (ar->lright == (start + size))) {
3410 + /* merge to the right */
3411 + ext3_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3412 + &ac->ac_f_ex.fe_group,
3413 + &ac->ac_f_ex.fe_start);
3414 + ac->ac_flags |= EXT3_MB_HINT_TRY_GOAL;
3416 + if (ar->pleft && (ar->lleft + 1 == start)) {
3417 + /* merge to the left */
3418 + ext3_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3419 + &ac->ac_f_ex.fe_group,
3420 + &ac->ac_f_ex.fe_start);
3421 + ac->ac_flags |= EXT3_MB_HINT_TRY_GOAL;
3424 + mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3425 + (unsigned) orig_size, (unsigned) start);
3428 +void ext3_mb_collect_stats(struct ext3_allocation_context *ac)
3430 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
3432 + if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3433 + atomic_inc(&sbi->s_bal_reqs);
3434 + atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3435 + if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3436 + atomic_inc(&sbi->s_bal_success);
3437 + atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3438 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3439 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3440 + atomic_inc(&sbi->s_bal_goals);
3441 + if (ac->ac_found > sbi->s_mb_max_to_scan)
3442 + atomic_inc(&sbi->s_bal_breaks);
3445 + ext3_mb_store_history(ac);
3449 + * use blocks preallocated to inode
3451 +void ext3_mb_use_inode_pa(struct ext3_allocation_context *ac,
3452 + struct ext3_prealloc_space *pa)
3454 + unsigned long start, len;
3456 + /* found preallocated blocks, use them */
3457 + start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3458 + len = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3459 + len = len - start;
3460 + ext3_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3461 + &ac->ac_b_ex.fe_start);
3462 + ac->ac_b_ex.fe_len = len;
3463 + ac->ac_status = AC_STATUS_FOUND;
3466 + BUG_ON(start < pa->pa_pstart);
3467 + BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3468 + BUG_ON(pa->pa_free < len);
3469 + pa->pa_free -= len;
3471 + mb_debug("use %lu/%lu from inode pa %p\n", start, len, pa);
3475 + * use blocks preallocated to locality group
3477 +void ext3_mb_use_group_pa(struct ext3_allocation_context *ac,
3478 + struct ext3_prealloc_space *pa)
3480 + unsigned len = ac->ac_o_ex.fe_len;
3482 + ext3_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3483 + &ac->ac_b_ex.fe_group,
3484 + &ac->ac_b_ex.fe_start);
3485 + ac->ac_b_ex.fe_len = len;
3486 + ac->ac_status = AC_STATUS_FOUND;
3489 + /* we don't correct pa_pstart or pa_plen here to avoid
3490 + * possible race when tte group is being loaded concurrently
3491 + * instead we correct pa later, after blocks are marked
3492 + * in on-disk bitmap -- see ext3_mb_release_context() */
3493 + mb_debug("use %lu/%lu from group pa %p\n", pa->pa_lstart-len, len, pa);
3497 + * search goal blocks in preallocated space
3499 +int ext3_mb_use_preallocated(struct ext3_allocation_context *ac)
3501 + struct ext3_inode_info *ei = EXT3_I(ac->ac_inode);
3502 + struct ext3_locality_group *lg;
3503 + struct ext3_prealloc_space *pa;
3504 + struct list_head *cur;
3506 + /* only data can be preallocated */
3507 + if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
3510 + /* first, try per-file preallocation */
3512 + list_for_each_rcu(cur, &ei->i_prealloc_list) {
3513 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3515 + /* all fields in this condition don't change,
3516 + * so we can skip locking for them */
3517 + if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3518 + ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3521 + /* found preallocated blocks, use them */
3522 + spin_lock(&pa->pa_lock);
3523 + if (pa->pa_deleted == 0 && pa->pa_free) {
3524 + atomic_inc(&pa->pa_count);
3525 + ext3_mb_use_inode_pa(ac, pa);
3526 + spin_unlock(&pa->pa_lock);
3527 + ac->ac_criteria = 10;
3528 + rcu_read_unlock();
3531 + spin_unlock(&pa->pa_lock);
3533 + rcu_read_unlock();
3535 + /* can we use group allocation? */
3536 + if (!(ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC))
3539 + /* inode may have no locality group for some reason */
3545 + list_for_each_rcu(cur, &lg->lg_prealloc_list) {
3546 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3547 + spin_lock(&pa->pa_lock);
3548 + if (pa->pa_deleted == 0 && pa->pa_free >= ac->ac_o_ex.fe_len) {
3549 + atomic_inc(&pa->pa_count);
3550 + ext3_mb_use_group_pa(ac, pa);
3551 + spin_unlock(&pa->pa_lock);
3552 + ac->ac_criteria = 20;
3553 + rcu_read_unlock();
3556 + spin_unlock(&pa->pa_lock);
3558 + rcu_read_unlock();
3564 + * the function goes through all preallocation in this group and marks them
3565 + * used in in-core bitmap. buddy must be generated from this bitmap
3567 +void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group)
3569 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
3570 + struct ext3_prealloc_space *pa;
3571 + struct list_head *cur;
3572 + unsigned long groupnr;
3573 + unsigned long start;
3574 + int preallocated = 0, count = 0, len;
3576 + /* all form of preallocation discards first load group,
3577 + * so the only competing code is preallocation use.
3578 + * we don't need any locking here
3579 + * notice we do NOT ignore preallocations with pa_deleted
3580 + * otherwise we could leave used blocks available for
3581 + * allocation in buddy when concurrent ext3_mb_put_pa()
3582 + * is dropping preallocation
3584 + list_for_each_rcu(cur, &grp->bb_prealloc_list) {
3585 + pa = list_entry(cur, struct ext3_prealloc_space, pa_group_list);
3586 + spin_lock(&pa->pa_lock);
3587 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &start);
3589 + spin_unlock(&pa->pa_lock);
3590 + if (unlikely(len == 0))
3592 + BUG_ON(groupnr != group && len != 0);
3593 + mb_set_bits(sb_bgl_lock(EXT3_SB(sb), group), bitmap, start,len);
3594 + preallocated += len;
3597 + mb_debug("prellocated %u for group %u\n", preallocated, group);
3600 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,5)
3601 +static void ext3_mb_pa_callback(struct rcu_head *head)
3603 + struct ext3_prealloc_space *pa;
3604 + pa = container_of(head, struct ext3_prealloc_space, u.pa_rcu);
3605 + kmem_cache_free(ext3_pspace_cachep, pa);
3607 +#define mb_call_rcu(__pa) call_rcu(&(__pa)->u.pa_rcu, ext3_mb_pa_callback)
3609 +static void ext3_mb_pa_callback(void *pa)
3611 + kmem_cache_free(ext3_pspace_cachep, pa);
3613 +#define mb_call_rcu(__pa) call_rcu(&(__pa)->u.pa_rcu, ext3_mb_pa_callback, pa)
3617 + * drops a reference to preallocated space descriptor
3618 + * if this was the last reference and the space is consumed
3620 +void ext3_mb_put_pa(struct ext3_allocation_context *ac,
3621 + struct super_block *sb, struct ext3_prealloc_space *pa)
3623 + unsigned long grp;
3625 + if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3628 + /* in this short window concurrent discard can set pa_deleted */
3629 + spin_lock(&pa->pa_lock);
3630 + if (pa->pa_deleted == 1) {
3631 + spin_unlock(&pa->pa_lock);
3635 + pa->pa_deleted = 1;
3636 + spin_unlock(&pa->pa_lock);
3638 + /* -1 is to protect from crossing allocation group */
3639 + ext3_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
3644 + * P1 (buddy init) P2 (regular allocation)
3645 + * find block B in PA
3646 + * copy on-disk bitmap to buddy
3647 + * mark B in on-disk bitmap
3648 + * drop PA from group
3649 + * mark all PAs in buddy
3651 + * thus, P1 initializes buddy with B available. to prevent this
3652 + * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3653 + * against that pair
3655 + ext3_lock_group(sb, grp);
3656 + list_del_rcu(&pa->pa_group_list);
3657 + ext3_unlock_group(sb, grp);
3659 + spin_lock(pa->pa_obj_lock);
3660 + list_del_rcu(&pa->pa_inode_list);
3661 + spin_unlock(pa->pa_obj_lock);
3667 + * creates new preallocated space for given inode
3669 +int ext3_mb_new_inode_pa(struct ext3_allocation_context *ac)
3671 + struct super_block *sb = ac->ac_sb;
3672 + struct ext3_prealloc_space *pa;
3673 + struct ext3_group_info *grp;
3674 + struct ext3_inode_info *ei;
3676 + /* preallocate only when found space is larger then requested */
3677 + BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3678 + BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3679 + BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3681 + pa = kmem_cache_alloc(ext3_pspace_cachep, GFP_NOFS);
3685 + if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3686 + int winl, wins, win, offs;
3688 + /* we can't allocate as much as normalizer wants.
3689 + * so, found space must get proper lstart
3690 + * to cover original request */
3691 + BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3692 + BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3694 + /* we're limited by original request in that
3695 + * logical block must be covered any way
3696 + * winl is window we can move our chunk within */
3697 + winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3699 + /* also, we should cover whole original request */
3700 + wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3702 + /* the smallest one defines real window */
3703 + win = min(winl, wins);
3705 + offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3706 + if (offs && offs < win)
3709 + ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3710 + BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3711 + BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3714 + /* preallocation can change ac_b_ex, thus we store actually
3715 + * allocated blocks for history */
3716 + ac->ac_f_ex = ac->ac_b_ex;
3718 + pa->pa_lstart = ac->ac_b_ex.fe_logical;
3719 + pa->pa_pstart = ext3_grp_offs_to_block(sb, &ac->ac_b_ex);
3720 + pa->pa_len = ac->ac_b_ex.fe_len;
3721 + pa->pa_free = pa->pa_len;
3722 + atomic_set(&pa->pa_count, 1);
3723 + spin_lock_init(&pa->pa_lock);
3724 + pa->pa_deleted = 0;
3725 + pa->pa_linear = 0;
3727 + mb_debug("new inode pa %p: %lu/%lu for %lu\n", pa,
3728 + pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3730 + ext3_mb_use_inode_pa(ac, pa);
3731 + atomic_add(pa->pa_free, &EXT3_SB(sb)->s_mb_preallocated);
3733 + ei = EXT3_I(ac->ac_inode);
3734 + grp = EXT3_GROUP_INFO(sb, ac->ac_b_ex.fe_group);
3736 + pa->pa_obj_lock = &ei->i_prealloc_lock;
3737 + pa->pa_inode = ac->ac_inode;
3739 + ext3_lock_group(sb, ac->ac_b_ex.fe_group);
3740 + list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
3741 + ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
3743 + spin_lock(pa->pa_obj_lock);
3744 + list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3745 + spin_unlock(pa->pa_obj_lock);
3751 + * creates new preallocated space for locality group inodes belongs to
3753 +int ext3_mb_new_group_pa(struct ext3_allocation_context *ac)
3755 + struct super_block *sb = ac->ac_sb;
3756 + struct ext3_locality_group *lg;
3757 + struct ext3_prealloc_space *pa;
3758 + struct ext3_group_info *grp;
3760 + /* preallocate only when found space is larger then requested */
3761 + BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3762 + BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3763 + BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3765 + BUG_ON(ext3_pspace_cachep == NULL);
3766 + pa = kmem_cache_alloc(ext3_pspace_cachep, GFP_NOFS);
3770 + /* preallocation can change ac_b_ex, thus we store actually
3771 + * allocated blocks for history */
3772 + ac->ac_f_ex = ac->ac_b_ex;
3774 + pa->pa_pstart = ext3_grp_offs_to_block(sb, &ac->ac_b_ex);
3775 + pa->pa_lstart = pa->pa_pstart;
3776 + pa->pa_len = ac->ac_b_ex.fe_len;
3777 + pa->pa_free = pa->pa_len;
3778 + atomic_set(&pa->pa_count, 1);
3779 + spin_lock_init(&pa->pa_lock);
3780 + pa->pa_deleted = 0;
3781 + pa->pa_linear = 1;
3783 + mb_debug("new group pa %p: %lu/%lu for %lu\n", pa,
3784 + pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3786 + ext3_mb_use_group_pa(ac, pa);
3787 + atomic_add(pa->pa_free, &EXT3_SB(sb)->s_mb_preallocated);
3789 + grp = EXT3_GROUP_INFO(sb, ac->ac_b_ex.fe_group);
3791 + BUG_ON(lg == NULL);
3793 + pa->pa_obj_lock = &lg->lg_prealloc_lock;
3794 + pa->pa_inode = NULL;
3796 + ext3_lock_group(sb, ac->ac_b_ex.fe_group);
3797 + list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
3798 + ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
3800 + spin_lock(pa->pa_obj_lock);
3801 + list_add_tail_rcu(&pa->pa_inode_list, &lg->lg_prealloc_list);
3802 + spin_unlock(pa->pa_obj_lock);
3807 +int ext3_mb_new_preallocation(struct ext3_allocation_context *ac)
3811 + if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
3812 + err = ext3_mb_new_group_pa(ac);
3814 + err = ext3_mb_new_inode_pa(ac);
3819 + * finds all unused blocks in on-disk bitmap, frees them in
3820 + * in-core bitmap and buddy.
3821 + * @pa must be unlinked from inode and group lists, so that
3822 + * nobody else can find/use it.
3823 + * the caller MUST hold group/inode locks.
3824 + * TODO: optimize the case when there are no in-core structures yet
3826 +int ext3_mb_release_inode_pa(struct ext3_buddy *e3b,
3827 + struct buffer_head *bitmap_bh,
3828 + struct ext3_prealloc_space *pa)
3830 + struct ext3_allocation_context ac;
3831 + struct super_block *sb = e3b->bd_sb;
3832 + struct ext3_sb_info *sbi = EXT3_SB(sb);
3833 + unsigned long bit, end, next, group;
3835 + int err = 0, free = 0;
3837 + BUG_ON(pa->pa_deleted == 0);
3838 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3839 + BUG_ON(group != e3b->bd_group && pa->pa_len != 0);
3840 + end = bit + pa->pa_len;
3843 + ac.ac_inode = pa->pa_inode;
3844 + ac.ac_op = EXT3_MB_HISTORY_DISCARD;
3846 + while (bit < end) {
3847 + bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
3850 + next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
3853 + start = group * EXT3_BLOCKS_PER_GROUP(sb) + bit +
3854 + le32_to_cpu(sbi->s_es->s_first_data_block);
3855 + mb_debug(" free preallocated %u/%u in group %u\n",
3856 + (unsigned) start, (unsigned) next - bit,
3857 + (unsigned) group);
3858 + free += next - bit;
3860 + ac.ac_b_ex.fe_group = group;
3861 + ac.ac_b_ex.fe_start = bit;
3862 + ac.ac_b_ex.fe_len = next - bit;
3863 + ac.ac_b_ex.fe_logical = 0;
3864 + ext3_mb_store_history(&ac);
3866 + mb_free_blocks(pa->pa_inode, e3b, bit, next - bit);
3869 + if (free != pa->pa_free) {
3870 + printk("pa %p: logic %lu, phys. %lu, len %lu\n",
3871 + pa, (unsigned long) pa->pa_lstart,
3872 + (unsigned long) pa->pa_pstart,
3873 + (unsigned long) pa->pa_len);
3874 + printk("free %u, pa_free %u\n", free, pa->pa_free);
3876 + BUG_ON(free != pa->pa_free);
3877 + atomic_add(free, &sbi->s_mb_discarded);
3882 +int ext3_mb_release_group_pa(struct ext3_buddy *e3b,
3883 + struct ext3_prealloc_space *pa)
3885 + struct ext3_allocation_context ac;
3886 + struct super_block *sb = e3b->bd_sb;
3887 + unsigned long bit, group;
3889 + ac.ac_op = EXT3_MB_HISTORY_DISCARD;
3891 + BUG_ON(pa->pa_deleted == 0);
3892 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3893 + BUG_ON(group != e3b->bd_group && pa->pa_len != 0);
3894 + mb_free_blocks(pa->pa_inode, e3b, bit, pa->pa_len);
3895 + atomic_add(pa->pa_len, &EXT3_SB(sb)->s_mb_discarded);
3898 + ac.ac_inode = NULL;
3899 + ac.ac_b_ex.fe_group = group;
3900 + ac.ac_b_ex.fe_start = bit;
3901 + ac.ac_b_ex.fe_len = pa->pa_len;
3902 + ac.ac_b_ex.fe_logical = 0;
3903 + ext3_mb_store_history(&ac);
3909 + * releases all preallocations in given group
3911 + * first, we need to decide discard policy:
3912 + * - when do we discard
3914 + * - how many do we discard
3915 + * 1) how many requested
3917 +int ext3_mb_discard_group_preallocations(struct super_block *sb,
3918 + int group, int needed)
3920 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
3921 + struct buffer_head *bitmap_bh = NULL;
3922 + struct ext3_prealloc_space *pa, *tmp;
3923 + struct list_head list;
3924 + struct ext3_buddy e3b;
3925 + int err, busy, free = 0;
3927 + mb_debug("discard preallocation for group %lu\n", group);
3929 + if (list_empty(&grp->bb_prealloc_list))
3932 + bitmap_bh = read_block_bitmap(sb, group);
3933 + if (bitmap_bh == NULL) {
3934 + /* error handling here */
3935 + ext3_mb_release_desc(&e3b);
3936 + BUG_ON(bitmap_bh == NULL);
3939 + err = ext3_mb_load_buddy(sb, group, &e3b);
3940 + BUG_ON(err != 0); /* error handling here */
3943 + needed = EXT3_BLOCKS_PER_GROUP(sb) + 1;
3945 + grp = EXT3_GROUP_INFO(sb, group);
3946 + INIT_LIST_HEAD(&list);
3950 + ext3_lock_group(sb, group);
3951 + list_for_each_entry_safe (pa, tmp, &grp->bb_prealloc_list, pa_group_list) {
3952 + spin_lock(&pa->pa_lock);
3953 + if (atomic_read(&pa->pa_count)) {
3954 + spin_unlock(&pa->pa_lock);
3958 + if (pa->pa_deleted) {
3959 + spin_unlock(&pa->pa_lock);
3963 + /* seems this one can be freed ... */
3964 + pa->pa_deleted = 1;
3966 + /* we can trust pa_free ... */
3967 + free += pa->pa_free;
3969 + spin_unlock(&pa->pa_lock);
3971 + list_del_rcu(&pa->pa_group_list);
3972 + list_add(&pa->u.pa_tmp_list, &list);
3975 + /* if we still need more blocks and some PAs were used, try again */
3976 + if (free < needed && busy) {
3977 + ext3_unlock_group(sb, group);
3981 + /* found anything to free? */
3982 + if (list_empty(&list)) {
3983 + BUG_ON(free != 0);
3987 + /* now free all selected PAs */
3988 + list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3990 + /* remove from object (inode or locality group) */
3991 + spin_lock(pa->pa_obj_lock);
3992 + list_del_rcu(&pa->pa_inode_list);
3993 + spin_unlock(pa->pa_obj_lock);
3995 + if (pa->pa_linear)
3996 + ext3_mb_release_group_pa(&e3b, pa);
3998 + ext3_mb_release_inode_pa(&e3b, bitmap_bh, pa);
4000 + list_del(&pa->u.pa_tmp_list);
4005 + ext3_unlock_group(sb, group);
4006 + ext3_mb_release_desc(&e3b);
4007 + brelse(bitmap_bh);
4012 + * releases all non-used preallocated blocks for given inode
4014 +void ext3_mb_discard_inode_preallocations(struct inode *inode)
4016 + struct ext3_inode_info *ei = EXT3_I(inode);
4017 + struct super_block *sb = inode->i_sb;
4018 + struct buffer_head *bitmap_bh = NULL;
4019 + struct ext3_prealloc_space *pa, *tmp;
4020 + unsigned long group = 0;
4021 + struct list_head list;
4022 + struct ext3_buddy e3b;
4025 + if (!test_opt(sb, MBALLOC) || !S_ISREG(inode->i_mode)) {
4026 + /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4030 + mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
4032 + INIT_LIST_HEAD(&list);
4035 + /* first, collect all pa's in the inode */
4036 + spin_lock(&ei->i_prealloc_lock);
4037 + while (!list_empty(&ei->i_prealloc_list)) {
4038 + pa = list_entry(ei->i_prealloc_list.next,
4039 + struct ext3_prealloc_space, pa_inode_list);
4040 + BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4041 + spin_lock(&pa->pa_lock);
4042 + if (atomic_read(&pa->pa_count)) {
4043 + /* this shouldn't happen often - nobody should
4044 + * use preallocation while we're discarding it */
4045 + spin_unlock(&pa->pa_lock);
4046 + spin_unlock(&ei->i_prealloc_lock);
4047 + current->state = TASK_UNINTERRUPTIBLE;
4048 + schedule_timeout(HZ);
4052 + if (pa->pa_deleted == 0) {
4053 + pa->pa_deleted = 1;
4054 + spin_unlock(&pa->pa_lock);
4055 + list_del_rcu(&pa->pa_inode_list);
4056 + list_add(&pa->u.pa_tmp_list, &list);
4060 + /* someone is deleting pa right now */
4061 + spin_unlock(&pa->pa_lock);
4062 + spin_unlock(&ei->i_prealloc_lock);
4064 + /* we have to wait here because pa_deleted
4065 + * doesn't mean pa is already unlinked from
4066 + * the list. as we might be called from
4067 + * ->clear_inode() the inode will get freed
4068 + * and concurrent thread which is unlinking
4069 + * pa from inode's list may access already
4070 + * freed memory, bad-bad-bad */
4072 + /* XXX: if this happens too often, we can
4073 + * add a flag to force wait only in case
4074 + * of ->clear_inode(), but not in case of
4075 + * regular truncate */
4076 + current->state = TASK_UNINTERRUPTIBLE;
4077 + schedule_timeout(HZ);
4080 + spin_unlock(&ei->i_prealloc_lock);
4082 + list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4083 + BUG_ON(pa->pa_linear != 0);
4084 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4086 + err = ext3_mb_load_buddy(sb, group, &e3b);
4087 + BUG_ON(err != 0); /* error handling here */
4089 + bitmap_bh = read_block_bitmap(sb, group);
4091 + ext3_lock_group(sb, group);
4092 + list_del_rcu(&pa->pa_group_list);
4094 + /* can be NULL due to IO error, at worst
4095 + * we leave some free blocks unavailable
4096 + * do not go RO - no need for */
4097 + if (bitmap_bh != NULL)
4098 + ext3_mb_release_inode_pa(&e3b, bitmap_bh, pa);
4099 + ext3_unlock_group(sb, group);
4101 + ext3_mb_release_desc(&e3b);
4102 + brelse(bitmap_bh);
4104 + list_del(&pa->u.pa_tmp_list);
4110 + * finds all preallocated spaces and return blocks being freed to them
4111 + * if preallocated space becomes full (no block is used from the space)
4112 + * then the function frees space in buddy
4113 + * XXX: at the moment, truncate (which is the only way to free blocks)
4114 + * discards all preallocations
4116 +void ext3_mb_return_to_preallocation(struct inode *inode, struct ext3_buddy *e3b,
4117 + sector_t block, int count)
4119 + BUG_ON(!list_empty(&EXT3_I(inode)->i_prealloc_list));
4122 +void ext3_mb_show_ac(struct ext3_allocation_context *ac)
4125 + struct super_block *sb = ac->ac_sb;
4128 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
4129 + ac->ac_status, ac->ac_flags);
4130 + printk(KERN_ERR "EXT3-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4131 + "best %lu/%lu/%lu@%lu cr %d\n",
4132 + ac->ac_o_ex.fe_group, ac->ac_o_ex.fe_start,
4133 + ac->ac_o_ex.fe_len, ac->ac_o_ex.fe_logical,
4134 + ac->ac_g_ex.fe_group, ac->ac_g_ex.fe_start,
4135 + ac->ac_g_ex.fe_len, ac->ac_g_ex.fe_logical,
4136 + ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start,
4137 + ac->ac_b_ex.fe_len, ac->ac_b_ex.fe_logical,
4139 + printk(KERN_ERR "EXT3-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4141 + printk("EXT3-fs: groups: ");
4142 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
4143 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, i);
4144 + struct ext3_prealloc_space *pa;
4145 + unsigned long start;
4146 + struct list_head *cur;
4147 + list_for_each_rcu(cur, &grp->bb_prealloc_list) {
4148 + pa = list_entry(cur, struct ext3_prealloc_space,
4150 + spin_lock(&pa->pa_lock);
4151 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, NULL, &start);
4152 + spin_unlock(&pa->pa_lock);
4153 + printk("PA:%u:%lu:%u ", i, start, pa->pa_len);
4156 + if (grp->bb_free == 0)
4158 + printk("%d: %d/%d ", i, grp->bb_free, grp->bb_fragments);
4165 +void ext3_mb_group_or_file(struct ext3_allocation_context *ac)
4167 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
4171 + if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
4174 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_small_req)
4177 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
4180 + /* request is so large that we don't care about
4181 + * streaming - it overweights any possible seek */
4182 + if (ac->ac_o_ex.fe_len >= sbi->s_mb_large_req)
4185 + bsbits = ac->ac_sb->s_blocksize_bits;
4187 + size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4188 + size = size << bsbits;
4189 + if (size < i_size_read(ac->ac_inode))
4190 + size = i_size_read(ac->ac_inode);
4191 + size = (size + ac->ac_sb->s_blocksize - 1) >> bsbits;
4193 + /* don't use group allocation for large files */
4194 + if (size >= sbi->s_mb_large_req)
4197 + BUG_ON(ac->ac_lg != NULL);
4198 + ac->ac_lg = &sbi->s_locality_groups[smp_processor_id()];
4200 + /* we're going to use group allocation */
4201 + ac->ac_flags |= EXT3_MB_HINT_GROUP_ALLOC;
4203 + /* serialize all allocations in the group */
4204 + down(&ac->ac_lg->lg_sem);
4207 +int ext3_mb_initialize_context(struct ext3_allocation_context *ac,
4208 + struct ext3_allocation_request *ar)
4210 + struct super_block *sb = ar->inode->i_sb;
4211 + struct ext3_sb_info *sbi = EXT3_SB(sb);
4212 + struct ext3_super_block *es = sbi->s_es;
4213 + unsigned long group, len, goal;
4214 + unsigned long block;
4216 + /* we can't allocate > group size */
4218 + if (len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
4219 + len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
4221 + /* start searching from the goal */
4223 + if (goal < le32_to_cpu(es->s_first_data_block) ||
4224 + goal >= le32_to_cpu(es->s_blocks_count))
4225 + goal = le32_to_cpu(es->s_first_data_block);
4226 + ext3_get_group_no_and_offset(sb, goal, &group, &block);
4228 + /* set up allocation goals */
4229 + ac->ac_b_ex.fe_logical = ar->logical;
4230 + ac->ac_b_ex.fe_group = 0;
4231 + ac->ac_b_ex.fe_start = 0;
4232 + ac->ac_b_ex.fe_len = 0;
4233 + ac->ac_status = AC_STATUS_CONTINUE;
4234 + ac->ac_groups_scanned = 0;
4235 + ac->ac_ex_scanned = 0;
4238 + ac->ac_inode = ar->inode;
4239 + ac->ac_o_ex.fe_logical = ar->logical;
4240 + ac->ac_o_ex.fe_group = group;
4241 + ac->ac_o_ex.fe_start = block;
4242 + ac->ac_o_ex.fe_len = len;
4243 + ac->ac_g_ex.fe_logical = ar->logical;
4244 + ac->ac_g_ex.fe_group = group;
4245 + ac->ac_g_ex.fe_start = block;
4246 + ac->ac_g_ex.fe_len = len;
4247 + ac->ac_f_ex.fe_len = 0;
4248 + ac->ac_flags = ar->flags;
4249 + ac->ac_2order = 0;
4250 + ac->ac_criteria = 0;
4252 + ac->ac_bitmap_page = NULL;
4253 + ac->ac_buddy_page = NULL;
4256 + /* we have to define context: we'll we work with a file or
4257 + * locality group. this is a policy, actually */
4258 + ext3_mb_group_or_file(ac);
4260 + mb_debug("init ac: %u blocks @ %llu, goal %llu, flags %x, 2^%d, "
4261 + "left: %llu/%llu, right %llu/%llu to %swritable\n",
4262 + (unsigned) ar->len, (unsigned) ar->logical,
4263 + (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4264 + (unsigned) ar->lleft, (unsigned) ar->pleft,
4265 + (unsigned) ar->lright, (unsigned) ar->pright,
4266 + atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4272 + * release all resource we used in allocation
4274 +int ext3_mb_release_context(struct ext3_allocation_context *ac)
4277 + if (ac->ac_pa->pa_linear) {
4278 + /* see comment in ext3_mb_use_group_pa() */
4279 + spin_lock(&ac->ac_pa->pa_lock);
4280 + ac->ac_pa->pa_pstart += ac->ac_b_ex.fe_len;
4281 + ac->ac_pa->pa_lstart += ac->ac_b_ex.fe_len;
4282 + ac->ac_pa->pa_free -= ac->ac_b_ex.fe_len;
4283 + ac->ac_pa->pa_len -= ac->ac_b_ex.fe_len;
4284 + spin_unlock(&ac->ac_pa->pa_lock);
4286 + ext3_mb_put_pa(ac, ac->ac_sb, ac->ac_pa);
4288 + if (ac->ac_bitmap_page)
4289 + page_cache_release(ac->ac_bitmap_page);
4290 + if (ac->ac_buddy_page)
4291 + page_cache_release(ac->ac_buddy_page);
4292 + if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
4293 + up(&ac->ac_lg->lg_sem);
4294 + ext3_mb_collect_stats(ac);
4298 +int ext3_mb_discard_preallocations(struct super_block *sb, int needed)
4300 + int i, ret, freed = 0;
4302 + for (i = 0; i < EXT3_SB(sb)->s_groups_count && needed > 0; i++) {
4303 + ret = ext3_mb_discard_group_preallocations(sb, i, needed);
4312 + * Main entry point into mballoc to allocate blocks
4313 + * it tries to use preallocation first, then falls back
4314 + * to usual allocation
4316 +unsigned long ext3_mb_new_blocks(handle_t *handle,
4317 + struct ext3_allocation_request *ar, int *errp)
4319 + struct ext3_allocation_context ac;
4320 + struct ext3_sb_info *sbi;
4321 + struct super_block *sb;
4322 + unsigned long block = 0;
4323 + int freed, inquota;
4325 + sb = ar->inode->i_sb;
4326 + sbi = EXT3_SB(sb);
4328 + if (!test_opt(sb, MBALLOC)) {
4329 + static int ext3_mballoc_warning = 0;
4330 + if (ext3_mballoc_warning++ == 0)
4331 + printk(KERN_ERR "EXT3-fs: multiblock request with "
4332 + "mballoc disabled!\n");
4334 + block = ext3_new_block_old(handle, ar->inode, ar->goal, errp);
4338 + while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
4339 + ar->flags |= EXT3_MB_HINT_NOPREALLOC;
4342 + if (ar->len == 0) {
4346 + inquota = ar->len;
4348 + ext3_mb_poll_new_transaction(sb, handle);
4350 + if ((*errp = ext3_mb_initialize_context(&ac, ar))) {
4355 + ac.ac_op = EXT3_MB_HISTORY_PREALLOC;
4356 + if (!ext3_mb_use_preallocated(&ac)) {
4358 + ac.ac_op = EXT3_MB_HISTORY_ALLOC;
4359 + ext3_mb_normalize_request(&ac, ar);
4362 + /* allocate space in core */
4363 + ext3_mb_regular_allocator(&ac);
4365 + /* as we've just preallocated more space than
4366 + * user requested orinally, we store allocated
4367 + * space in a special descriptor */
4368 + if (ac.ac_status == AC_STATUS_FOUND &&
4369 + ac.ac_o_ex.fe_len < ac.ac_b_ex.fe_len)
4370 + ext3_mb_new_preallocation(&ac);
4373 + if (likely(ac.ac_status == AC_STATUS_FOUND)) {
4374 + ext3_mb_mark_diskspace_used(&ac, handle);
4376 + block = ext3_grp_offs_to_block(sb, &ac.ac_b_ex);
4377 + ar->len = ac.ac_b_ex.fe_len;
4379 + freed = ext3_mb_discard_preallocations(sb, ac.ac_o_ex.fe_len);
4383 + ac.ac_b_ex.fe_len = 0;
4385 + ext3_mb_show_ac(&ac);
4388 + ext3_mb_release_context(&ac);
4391 + if (ar->len < inquota)
4392 + DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
4396 +EXPORT_SYMBOL(ext3_mb_new_blocks);
4398 +int ext3_new_block(handle_t *handle, struct inode *inode,
4399 + unsigned long goal, int *errp)
4401 + struct ext3_allocation_request ar;
4402 + unsigned long ret;
4404 + if (!test_opt(inode->i_sb, MBALLOC)) {
4405 + ret = ext3_new_block_old(handle, inode, goal, errp);
4418 + ret = ext3_mb_new_blocks(handle, &ar, errp);
4422 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
4424 + struct ext3_sb_info *sbi = EXT3_SB(sb);
4426 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
4429 + /* new transaction! time to close last one and free blocks for
4430 + * committed transaction. we know that only transaction can be
4431 + * active, so previos transaction can be being logged and we
4432 + * know that transaction before previous is known to be already
4433 + * logged. this means that now we may free blocks freed in all
4434 + * transactions before previous one. hope I'm clear enough ... */
4436 + spin_lock(&sbi->s_md_lock);
4437 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
4438 + mb_debug("new transaction %lu, old %lu\n",
4439 + (unsigned long) handle->h_transaction->t_tid,
4440 + (unsigned long) sbi->s_last_transaction);
4441 + list_splice_init(&sbi->s_closed_transaction,
4442 + &sbi->s_committed_transaction);
4443 + list_splice_init(&sbi->s_active_transaction,
4444 + &sbi->s_closed_transaction);
4445 + sbi->s_last_transaction = handle->h_transaction->t_tid;
4447 + spin_unlock(&sbi->s_md_lock);
4449 + ext3_mb_free_committed_blocks(sb);
4452 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
4453 + int group, int block, int count)
4455 + struct ext3_group_info *db = e3b->bd_info;
4456 + struct super_block *sb = e3b->bd_sb;
4457 + struct ext3_sb_info *sbi = EXT3_SB(sb);
4458 + struct ext3_free_metadata *md;
4461 + BUG_ON(e3b->bd_bitmap_page == NULL);
4462 + BUG_ON(e3b->bd_buddy_page == NULL);
4464 + ext3_lock_group(sb, group);
4465 + for (i = 0; i < count; i++) {
4466 + md = db->bb_md_cur;
4467 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
4468 + db->bb_md_cur = NULL;
4473 + ext3_unlock_group(sb, group);
4474 + md = kmalloc(sizeof(*md), GFP_KERNEL);
4478 + md->group = group;
4480 + ext3_lock_group(sb, group);
4481 + if (db->bb_md_cur == NULL) {
4482 + spin_lock(&sbi->s_md_lock);
4483 + list_add(&md->list, &sbi->s_active_transaction);
4484 + spin_unlock(&sbi->s_md_lock);
4485 + /* protect buddy cache from being freed,
4486 + * otherwise we'll refresh it from
4487 + * on-disk bitmap and lose not-yet-available
4489 + page_cache_get(e3b->bd_buddy_page);
4490 + page_cache_get(e3b->bd_bitmap_page);
4491 + db->bb_md_cur = md;
4492 + db->bb_tid = handle->h_transaction->t_tid;
4493 + mb_debug("new md 0x%p for group %u\n",
4497 + md = db->bb_md_cur;
4501 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
4502 + md->blocks[md->num] = block + i;
4504 + if (md->num == EXT3_BB_MAX_BLOCKS) {
4505 + /* no more space, put full container on a sb's list */
4506 + db->bb_md_cur = NULL;
4509 + ext3_unlock_group(sb, group);
4514 + * Main entry point into mballoc to free blocks
4516 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
4517 + unsigned long block, unsigned long count,
4518 + int metadata, int *freed)
4520 + struct buffer_head *bitmap_bh = NULL;
4521 + struct super_block *sb = inode->i_sb;
4522 + struct ext3_allocation_context ac;
4523 + struct ext3_group_desc *gdp;
4524 + struct ext3_super_block *es;
4525 + unsigned long bit, overflow;
4526 + struct buffer_head *gd_bh;
4527 + unsigned long block_group;
4528 + struct ext3_sb_info *sbi;
4529 + struct ext3_buddy e3b;
4534 + ext3_mb_poll_new_transaction(sb, handle);
4536 + sbi = EXT3_SB(sb);
4537 + es = EXT3_SB(sb)->s_es;
4538 + if (block < le32_to_cpu(es->s_first_data_block) ||
4539 + block + count < block ||
4540 + block + count > le32_to_cpu(es->s_blocks_count)) {
4541 + ext3_error (sb, __FUNCTION__,
4542 + "Freeing blocks not in datazone - "
4543 + "block = %lu, count = %lu", block, count);
4544 + goto error_return;
4547 + ext3_debug("freeing block %lu\n", block);
4549 + ac.ac_op = EXT3_MB_HISTORY_FREE;
4550 + ac.ac_inode = inode;
4555 + ext3_get_group_no_and_offset(sb, block, &block_group, &bit);
4558 + * Check to see if we are freeing blocks across a group
4561 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
4562 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
4563 + count -= overflow;
4565 + brelse(bitmap_bh);
4566 + bitmap_bh = read_block_bitmap(sb, block_group);
4568 + goto error_return;
4569 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
4571 + goto error_return;
4573 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
4574 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
4575 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
4576 + EXT3_SB(sb)->s_itb_per_group) ||
4577 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
4578 + EXT3_SB(sb)->s_itb_per_group))
4579 + ext3_error(sb, __FUNCTION__,
4580 + "Freeing blocks in system zone - "
4581 + "Block = %lu, count = %lu", block, count);
4583 + BUFFER_TRACE(bitmap_bh, "getting write access");
4584 + err = ext3_journal_get_write_access(handle, bitmap_bh);
4586 + goto error_return;
4589 + * We are about to modify some metadata. Call the journal APIs
4590 + * to unshare ->b_data if a currently-committing transaction is
4593 + BUFFER_TRACE(gd_bh, "get_write_access");
4594 + err = ext3_journal_get_write_access(handle, gd_bh);
4596 + goto error_return;
4598 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
4600 + goto error_return;
4602 +#ifdef AGGRESSIVE_CHECK
4605 + for (i = 0; i < count; i++)
4606 + BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4609 + mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data, bit,
4612 + /* We dirtied the bitmap block */
4613 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4614 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
4616 + ac.ac_b_ex.fe_group = block_group;
4617 + ac.ac_b_ex.fe_start = bit;
4618 + ac.ac_b_ex.fe_len = count;
4619 + ext3_mb_store_history(&ac);
4622 + /* blocks being freed are metadata. these blocks shouldn't
4623 + * be used until this transaction is committed */
4624 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
4626 + ext3_lock_group(sb, block_group);
4627 + err = mb_free_blocks(inode, &e3b, bit, count);
4628 + ext3_mb_return_to_preallocation(inode, &e3b, block, count);
4629 + ext3_unlock_group(sb, block_group);
4633 + spin_lock(sb_bgl_lock(sbi, block_group));
4634 + gdp->bg_free_blocks_count =
4635 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
4636 + spin_unlock(sb_bgl_lock(sbi, block_group));
4637 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
4639 + ext3_mb_release_desc(&e3b);
4643 + /* And the group descriptor block */
4644 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4645 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
4646 + if (!err) err = ret;
4648 + if (overflow && !err) {
4655 + brelse(bitmap_bh);
4656 + ext3_std_error(sb, err);
4660 +EXPORT_SYMBOL(ext3_free_blocks);
4661 +EXPORT_SYMBOL(ext3_mb_discard_inode_preallocations);