1 Index: linux-2.6.9-full/include/linux/ext3_fs.h
2 ===================================================================
3 --- linux-2.6.9-full.orig/include/linux/ext3_fs.h 2007-06-08 23:44:08.000000000 +0400
4 +++ linux-2.6.9-full/include/linux/ext3_fs.h 2007-07-19 09:39:07.000000000 +0400
5 @@ -57,6 +57,30 @@ struct statfs;
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 */
21 +struct ext3_allocation_request {
22 + struct inode *inode; /* target inode for block we're allocating */
23 + unsigned long logical; /* logical block in target inode */
24 + unsigned long goal; /* phys. target (a hint) */
25 + unsigned long lleft; /* the closest logical allocated block to the left */
26 + unsigned long pleft; /* phys. block for ^^^ */
27 + unsigned long lright; /* the closest logical allocated block to the right */
28 + unsigned long pright; /* phys. block for ^^^ */
29 + unsigned long len; /* how many blocks we want to allocate */
30 + unsigned long flags; /* flags. see above EXT3_MB_HINT_* */
34 * Special inodes numbers
36 @@ -387,6 +411,14 @@ struct ext3_inode {
37 #define ext3_find_first_zero_bit ext2_find_first_zero_bit
38 #define ext3_find_next_zero_bit ext2_find_next_zero_bit
40 +#ifndef ext2_find_next_le_bit
41 +#ifdef __LITTLE_ENDIAN
42 +#define ext2_find_next_le_bit(addr, size, off) find_next_bit((addr), (size), (off))
44 +#error "mballoc needs a patch for big-endian systems - CFS bug 10634"
45 +#endif /* __LITTLE_ENDIAN */
46 +#endif /* !ext2_find_next_le_bit */
49 * Maximal mount counts between two filesystem checks
51 @@ -763,6 +795,20 @@ extern unsigned long ext3_count_dirs (st
52 extern void ext3_check_inodes_bitmap (struct super_block *);
53 extern unsigned long ext3_count_free (struct buffer_head *, unsigned);
56 +extern long ext3_mb_stats;
57 +extern long ext3_mb_max_to_scan;
58 +extern int ext3_mb_init(struct super_block *, int);
59 +extern int ext3_mb_release(struct super_block *);
60 +extern unsigned long ext3_mb_new_blocks(handle_t *, struct ext3_allocation_request *, int *);
61 +extern int ext3_mb_reserve_blocks(struct super_block *, int);
62 +extern void ext3_mb_release_blocks(struct super_block *, int);
63 +extern void ext3_mb_release_blocks(struct super_block *, int);
64 +extern void ext3_mb_discard_inode_preallocations(struct inode *);
65 +extern int __init init_ext3_proc(void);
66 +extern void exit_ext3_proc(void);
67 +extern void ext3_mb_free_blocks(handle_t *, struct inode *, unsigned long, unsigned long, int, int *);
71 extern int ext3_block_truncate_page(handle_t *, struct page *,
72 Index: linux-2.6.9-full/include/linux/ext3_fs_sb.h
73 ===================================================================
74 --- linux-2.6.9-full.orig/include/linux/ext3_fs_sb.h 2007-06-08 23:44:07.000000000 +0400
75 +++ linux-2.6.9-full/include/linux/ext3_fs_sb.h 2007-07-19 09:39:07.000000000 +0400
76 @@ -81,6 +81,61 @@ struct ext3_sb_info {
77 char *s_qf_names[MAXQUOTAS]; /* Names of quota files with journalled quota */
78 int s_jquota_fmt; /* Format of quota to use */
81 + /* for buddy allocator */
82 + struct ext3_group_info ***s_group_info;
83 + struct inode *s_buddy_cache;
84 + long s_blocks_reserved;
85 + spinlock_t s_reserve_lock;
86 + struct list_head s_active_transaction;
87 + struct list_head s_closed_transaction;
88 + struct list_head s_committed_transaction;
89 + spinlock_t s_md_lock;
90 + tid_t s_last_transaction;
91 + unsigned short *s_mb_offsets, *s_mb_maxs;
94 + unsigned long s_mb_factor;
95 + unsigned long s_stripe;
96 + unsigned long s_mb_stream_request;
97 + unsigned long s_mb_max_to_scan;
98 + unsigned long s_mb_min_to_scan;
99 + unsigned long s_mb_max_groups_to_scan;
100 + unsigned long s_mb_stats;
101 + unsigned long s_mb_order2_reqs;
103 + /* history to debug policy */
104 + struct ext3_mb_history *s_mb_history;
105 + int s_mb_history_cur;
106 + int s_mb_history_max;
107 + int s_mb_history_num;
108 + struct proc_dir_entry *s_mb_proc;
109 + spinlock_t s_mb_history_lock;
110 + int s_mb_history_filter;
112 + /* stats for buddy allocator */
113 + spinlock_t s_mb_pa_lock;
114 + atomic_t s_bal_reqs; /* number of reqs with len > 1 */
115 + atomic_t s_bal_success; /* we found long enough chunks */
116 + atomic_t s_bal_allocated; /* in blocks */
117 + atomic_t s_bal_ex_scanned; /* total extents scanned */
118 + atomic_t s_bal_goals; /* goal hits */
119 + atomic_t s_bal_breaks; /* too long searches */
120 + atomic_t s_bal_2orders; /* 2^order hits */
121 + spinlock_t s_bal_lock;
122 + unsigned long s_mb_buddies_generated;
123 + unsigned long long s_mb_generation_time;
124 + atomic_t s_mb_lost_chunks;
125 + atomic_t s_mb_preallocated;
126 + atomic_t s_mb_discarded;
128 + /* locality groups */
129 + struct ext3_locality_group *s_locality_groups;
133 +#define EXT3_GROUP_INFO(sb, group) \
134 + EXT3_SB(sb)->s_group_info[(group) >> EXT3_DESC_PER_BLOCK_BITS(sb)] \
135 + [(group) & (EXT3_DESC_PER_BLOCK(sb) - 1)]
137 #endif /* _LINUX_EXT3_FS_SB */
138 Index: linux-2.6.9-full/fs/ext3/super.c
139 ===================================================================
140 --- linux-2.6.9-full.orig/fs/ext3/super.c 2007-06-08 23:44:08.000000000 +0400
141 +++ linux-2.6.9-full/fs/ext3/super.c 2007-07-19 09:39:07.000000000 +0400
142 @@ -394,6 +394,7 @@ void ext3_put_super (struct super_block
143 struct ext3_super_block *es = sbi->s_es;
146 + ext3_mb_release(sb);
147 ext3_ext_release(sb);
148 ext3_xattr_put_super(sb);
149 journal_destroy(sbi->s_journal);
150 @@ -463,6 +464,8 @@ static struct inode *ext3_alloc_inode(st
151 ei->vfs_inode.i_version = 1;
153 memset(&ei->i_cached_extent, 0, sizeof(ei->i_cached_extent));
154 + INIT_LIST_HEAD(&ei->i_prealloc_list);
155 + spin_lock_init(&ei->i_prealloc_lock);
156 return &ei->vfs_inode;
159 @@ -2576,7 +2579,13 @@ static struct file_system_type ext3_fs_t
161 static int __init init_ext3_fs(void)
163 - int err = init_ext3_xattr();
166 + err = init_ext3_proc();
170 + err = init_ext3_xattr();
173 err = init_inodecache();
174 @@ -2598,6 +2607,7 @@ static void __exit exit_ext3_fs(void)
175 unregister_filesystem(&ext3_fs_type);
176 destroy_inodecache();
181 int ext3_prep_san_write(struct inode *inode, long *blocks,
182 Index: linux-2.6.9-full/fs/ext3/mballoc.c
183 ===================================================================
184 --- linux-2.6.9-full.orig/fs/ext3/mballoc.c 2007-07-14 04:24:39.138985848 +0400
185 +++ linux-2.6.9-full/fs/ext3/mballoc.c 2007-07-20 11:31:03.000000000 +0400
188 + * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
189 + * Written by Alex Tomas <alex@clusterfs.com>
191 + * This program is free software; you can redistribute it and/or modify
192 + * it under the terms of the GNU General Public License version 2 as
193 + * published by the Free Software Foundation.
195 + * This program is distributed in the hope that it will be useful,
196 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
197 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
198 + * GNU General Public License for more details.
200 + * You should have received a copy of the GNU General Public Licens
201 + * along with this program; if not, write to the Free Software
202 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
207 + * mballoc.c contains the multiblocks allocation routines
210 +#include <linux/time.h>
211 +#include <linux/fs.h>
212 +#include <linux/namei.h>
213 +#include <linux/ext3_jbd.h>
214 +#include <linux/jbd.h>
215 +#include <linux/ext3_fs.h>
216 +#include <linux/quotaops.h>
217 +#include <linux/buffer_head.h>
218 +#include <linux/module.h>
219 +#include <linux/swap.h>
220 +#include <linux/proc_fs.h>
221 +#include <linux/pagemap.h>
222 +#include <linux/seq_file.h>
223 +#include <linux/version.h>
227 + * - test ext3_ext_search_left() and ext3_ext_search_right()
228 + * - search for metadata in few groups
231 + * - normalization should take into account whether file is still open
232 + * - discard preallocations if no free space left (policy?)
233 + * - don't normalize tails
235 + * - reservation for superuser
238 + * - bitmap read-ahead (proposed by Oleg Drokin aka green)
239 + * - track min/max extents in each group for better group selection
240 + * - mb_mark_used() may allocate chunk right after splitting buddy
241 + * - tree of groups sorted by number of free blocks
246 + * mballoc operates on the following data:
248 + * - in-core buddy (actually includes buddy and bitmap)
249 + * - preallocation descriptors (PAs)
251 + * there are two types of preallocations:
253 + * assiged to specific inode and can be used for this inode only.
254 + * it describes part of inode's space preallocated to specific
255 + * physical blocks. any block from that preallocated can be used
256 + * independent. the descriptor just tracks number of blocks left
257 + * unused. so, before taking some block from descriptor, one must
258 + * make sure corresponded logical block isn't allocated yet. this
259 + * also means that freeing any block within descriptor's range
260 + * must discard all preallocated blocks.
262 + * assigned to specific locality group which does not translate to
263 + * permanent set of inodes: inode can join and leave group. space
264 + * from this type of preallocation can be used for any inode. thus
265 + * it's consumed from the beginning to the end.
267 + * relation between them can be expressed as:
268 + * in-core buddy = on-disk bitmap + preallocation descriptors
270 + * this mean blocks mballoc considers used are:
271 + * - allocated blocks (persistent)
272 + * - preallocated blocks (non-persistent)
274 + * consistency in mballoc world means that at any time a block is either
275 + * free or used in ALL structures. notice: "any time" should not be read
276 + * literally -- time is discrete and delimited by locks.
278 + * to keep it simple, we don't use block numbers, instead we count number of
279 + * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
281 + * all operations can be expressed as:
282 + * - init buddy: buddy = on-disk + PAs
283 + * - new PA: buddy += N; PA = N
284 + * - use inode PA: on-disk += N; PA -= N
285 + * - discard inode PA buddy -= on-disk - PA; PA = 0
286 + * - use locality group PA on-disk += N; PA -= N
287 + * - discard locality group PA buddy -= PA; PA = 0
288 + * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
289 + * is used in real operation because we can't know actual used
290 + * bits from PA, only from on-disk bitmap
292 + * if we follow this strict logic, then all operations above should be atomic.
293 + * given some of them can block, we'd have to use something like semaphores
294 + * killing performance on high-end SMP hardware. let's try to relax it using
295 + * the following knowledge:
296 + * 1) if buddy is referenced, it's already initialized
297 + * 2) while block is used in buddy and the buddy is referenced,
298 + * nobody can re-allocate that block
299 + * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
300 + * bit set and PA claims same block, it's OK. IOW, one can set bit in
301 + * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
304 + * so, now we're building a concurrency table:
307 + * blocks for PA are allocated in the buddy, buddy must be referenced
308 + * until PA is linked to allocation group to avoid concurrent buddy init
310 + * we need to make sure that either on-disk bitmap or PA has uptodate data
311 + * given (3) we care that PA-=N operation doesn't interfere with init
312 + * - discard inode PA
313 + * the simplest way would be to have buddy initialized by the discard
314 + * - use locality group PA
315 + * again PA-=N must be serialized with init
316 + * - discard locality group PA
317 + * the simplest way would be to have buddy initialized by the discard
320 + * i_truncate_mutex serializes them
321 + * - discard inode PA
322 + * discard process must wait until PA isn't used by another process
323 + * - use locality group PA
324 + * some mutex should serialize them
325 + * - discard locality group PA
326 + * discard process must wait until PA isn't used by another process
329 + * i_truncate_mutex or another mutex should serializes them
330 + * - discard inode PA
331 + * discard process must wait until PA isn't used by another process
332 + * - use locality group PA
333 + * nothing wrong here -- they're different PAs covering different blocks
334 + * - discard locality group PA
335 + * discard process must wait until PA isn't used by another process
337 + * now we're ready to make few consequences:
338 + * - PA is referenced and while it is no discard is possible
339 + * - PA is referenced until block isn't marked in on-disk bitmap
340 + * - PA changes only after on-disk bitmap
341 + * - discard must not compete with init. either init is done before
342 + * any discard or they're serialized somehow
343 + * - buddy init as sum of on-disk bitmap and PAs is done atomically
345 + * a special case when we've used PA to emptiness. no need to modify buddy
346 + * in this case, but we should care about concurrent init
351 + * Logic in few words:
356 + * mark bits in on-disk bitmap
359 + * - use preallocation:
360 + * find proper PA (per-inode or group)
362 + * mark bits in on-disk bitmap
368 + * mark bits in on-disk bitmap
371 + * - discard preallocations in group:
373 + * move them onto local list
374 + * load on-disk bitmap
376 + * remove PA from object (inode or locality group)
377 + * mark free blocks in-core
379 + * - discard inode's preallocations:
386 + * - bitlock on a group (group)
387 + * - object (inode/locality) (object)
388 + * - per-pa lock (pa)
395 + * - find and use pa:
398 + * - release consumed pa:
403 + * - generate in-core bitmap:
407 + * - discard all for given object (inode, locality group):
412 + * - discard all for given group:
421 + * with AGGRESSIVE_CHECK allocator runs consistency checks over
422 + * structures. these checks slow things down a lot
424 +#define AGGRESSIVE_CHECK__
427 + * with DOUBLE_CHECK defined mballoc creates persistent in-core
428 + * bitmaps, maintains and uses them to check for double allocations
430 +#define DOUBLE_CHECK__
436 +#define mb_debug(fmt,a...) printk(fmt, ##a)
438 +#define mb_debug(fmt,a...)
442 + * with EXT3_MB_HISTORY mballoc stores last N allocations in memory
443 + * and you can monitor it in /proc/fs/ext3/<dev>/mb_history
445 +#define EXT3_MB_HISTORY
446 +#define EXT3_MB_HISTORY_ALLOC 1 /* allocation */
447 +#define EXT3_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
448 +#define EXT3_MB_HISTORY_DISCARD 4 /* preallocation discarded */
449 +#define EXT3_MB_HISTORY_FREE 8 /* free */
451 +#define EXT3_MB_HISTORY_DEFAULT (EXT3_MB_HISTORY_ALLOC | \
452 + EXT3_MB_HISTORY_PREALLOC | \
453 + EXT3_MB_HISTORY_DISCARD | \
454 + EXT3_MB_HISTORY_FREE)
457 + * How long mballoc can look for a best extent (in found extents)
459 +#define MB_DEFAULT_MAX_TO_SCAN 200
462 + * How long mballoc must look for a best extent
464 +#define MB_DEFAULT_MIN_TO_SCAN 10
467 + * How many groups mballoc will scan looking for the best chunk
469 +#define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
472 + * with 'ext3_mb_stats' allocator will collect stats that will be
473 + * shown at umount. The collecting costs though!
475 +#define MB_DEFAULT_STATS 1
478 + * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
479 + * by the stream allocator, which purpose is to pack requests
480 + * as close each to other as possible to produce smooth I/O traffic
482 +#define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
485 + * for which requests use 2^N search using buddies
487 +#define MB_DEFAULT_ORDER2_REQS 8
490 + * default stripe size = 1MB
492 +#define MB_DEFAULT_STRIPE 256
494 +static kmem_cache_t *ext3_pspace_cachep = NULL;
496 +#ifdef EXT3_BB_MAX_BLOCKS
497 +#undef EXT3_BB_MAX_BLOCKS
499 +#define EXT3_BB_MAX_BLOCKS 30
501 +struct ext3_free_metadata {
502 + unsigned short group;
503 + unsigned short num;
504 + unsigned short blocks[EXT3_BB_MAX_BLOCKS];
505 + struct list_head list;
508 +struct ext3_group_info {
509 + unsigned long bb_state;
510 + unsigned long bb_tid;
511 + struct ext3_free_metadata *bb_md_cur;
512 + unsigned short bb_first_free;
513 + unsigned short bb_free;
514 + unsigned short bb_fragments;
515 + struct list_head bb_prealloc_list;
519 + unsigned short bb_counters[];
522 +#define EXT3_GROUP_INFO_NEED_INIT_BIT 0
523 +#define EXT3_GROUP_INFO_LOCKED_BIT 1
525 +#define EXT3_MB_GRP_NEED_INIT(grp) \
526 + (test_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &(grp)->bb_state))
529 +struct ext3_prealloc_space {
530 + struct list_head pa_inode_list;
531 + struct list_head pa_group_list;
533 + struct list_head pa_tmp_list;
534 + struct rcu_head pa_rcu;
536 + spinlock_t pa_lock;
538 + unsigned pa_deleted;
539 + unsigned long pa_pstart; /* phys. block */
540 + unsigned long pa_lstart; /* log. block */
541 + unsigned short pa_len; /* len of preallocated chunk */
542 + unsigned short pa_free; /* how many blocks are free */
543 + unsigned short pa_linear; /* consumed in one direction
544 + * strictly, for group prealloc */
545 + spinlock_t *pa_obj_lock;
546 + struct inode *pa_inode; /* hack, for history only */
550 +struct ext3_free_extent {
551 + unsigned long fe_logical;
552 + unsigned long fe_start;
553 + unsigned long fe_group;
554 + unsigned long fe_len;
559 + * we try to group all related changes together
560 + * so that writeback can flush/allocate them together as well
562 +struct ext3_locality_group {
563 + /* for allocator */
564 + struct semaphore lg_sem; /* to serialize allocates */
565 + struct list_head lg_prealloc_list;/* list of preallocations */
566 + spinlock_t lg_prealloc_lock;
569 +struct ext3_allocation_context {
570 + struct inode *ac_inode;
571 + struct super_block *ac_sb;
573 + /* original request */
574 + struct ext3_free_extent ac_o_ex;
576 + /* goal request (after normalization) */
577 + struct ext3_free_extent ac_g_ex;
579 + /* the best found extent */
580 + struct ext3_free_extent ac_b_ex;
582 + /* copy of the bext found extent taken before preallocation efforts */
583 + struct ext3_free_extent ac_f_ex;
585 + /* number of iterations done. we have to track to limit searching */
586 + unsigned long ac_ex_scanned;
587 + __u16 ac_groups_scanned;
591 + __u16 ac_flags; /* allocation hints */
595 + __u8 ac_2order; /* if request is to allocate 2^N blocks and
596 + * N > 0, the field stores N, otherwise 0 */
597 + __u8 ac_op; /* operation, for history only */
598 + struct page *ac_bitmap_page;
599 + struct page *ac_buddy_page;
600 + struct ext3_prealloc_space *ac_pa;
601 + struct ext3_locality_group *ac_lg;
604 +#define AC_STATUS_CONTINUE 1
605 +#define AC_STATUS_FOUND 2
606 +#define AC_STATUS_BREAK 3
608 +struct ext3_mb_history {
609 + struct ext3_free_extent orig; /* orig allocation */
610 + struct ext3_free_extent goal; /* goal allocation */
611 + struct ext3_free_extent result; /* result allocation */
614 + __u16 found; /* how many extents have been found */
615 + __u16 groups; /* how many groups have been scanned */
616 + __u16 tail; /* what tail broke some buddy */
617 + __u16 buddy; /* buddy the tail ^^^ broke */
619 + __u8 cr:3; /* which phase the result extent was found at */
625 + struct page *bd_buddy_page;
627 + struct page *bd_bitmap_page;
629 + struct ext3_group_info *bd_info;
630 + struct super_block *bd_sb;
634 +#define EXT3_MB_BITMAP(e3b) ((e3b)->bd_bitmap)
635 +#define EXT3_MB_BUDDY(e3b) ((e3b)->bd_buddy)
637 +#ifndef EXT3_MB_HISTORY
638 +#define ext3_mb_store_history(ac)
640 +static void ext3_mb_store_history(struct ext3_allocation_context *ac);
643 +#define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
645 +static struct proc_dir_entry *proc_root_ext3;
647 +int ext3_create (struct inode *, struct dentry *, int, struct nameidata *);
648 +struct buffer_head * read_block_bitmap(struct super_block *, unsigned int);
649 +unsigned long ext3_new_blocks_old(handle_t *handle, struct inode *inode,
650 + unsigned long goal, unsigned long *count, int *errp);
651 +void ext3_mb_release_blocks(struct super_block *, int);
652 +void ext3_mb_poll_new_transaction(struct super_block *, handle_t *);
653 +void ext3_mb_free_committed_blocks(struct super_block *);
654 +void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group);
655 +void ext3_mb_free_consumed_preallocations(struct ext3_allocation_context *ac);
656 +void ext3_mb_return_to_preallocation(struct inode *inode, struct ext3_buddy *e3b,
657 + sector_t block, int count);
658 +void ext3_mb_show_ac(struct ext3_allocation_context *ac);
659 +void ext3_mb_check_with_pa(struct ext3_buddy *e3b, int first, int count);
660 +void ext3_mb_put_pa(struct ext3_allocation_context *, struct super_block *, struct ext3_prealloc_space *pa);
661 +int ext3_mb_init_per_dev_proc(struct super_block *sb);
662 +int ext3_mb_destroy_per_dev_proc(struct super_block *sb);
665 + * Calculate the block group number and offset, given a block number
667 +static void ext3_get_group_no_and_offset(struct super_block *sb,
668 + unsigned long blocknr,
669 + unsigned long *blockgrpp,
670 + unsigned long *offsetp)
672 + struct ext3_super_block *es = EXT3_SB(sb)->s_es;
673 + unsigned long offset;
675 + blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
676 + offset = blocknr % EXT3_BLOCKS_PER_GROUP(sb);
677 + blocknr = blocknr / EXT3_BLOCKS_PER_GROUP(sb);
681 + *blockgrpp = blocknr;
686 +ext3_lock_group(struct super_block *sb, int group)
688 + bit_spin_lock(EXT3_GROUP_INFO_LOCKED_BIT,
689 + &EXT3_GROUP_INFO(sb, group)->bb_state);
693 +ext3_unlock_group(struct super_block *sb, int group)
695 + bit_spin_unlock(EXT3_GROUP_INFO_LOCKED_BIT,
696 + &EXT3_GROUP_INFO(sb, group)->bb_state);
700 +ext3_is_group_locked(struct super_block *sb, int group)
702 + return bit_spin_is_locked(EXT3_GROUP_INFO_LOCKED_BIT,
703 + &EXT3_GROUP_INFO(sb, group)->bb_state);
706 +unsigned long ext3_grp_offs_to_block(struct super_block *sb,
707 + struct ext3_free_extent *fex)
709 + unsigned long block;
711 + block = (unsigned long) fex->fe_group * EXT3_BLOCKS_PER_GROUP(sb)
713 + + le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
717 +#if BITS_PER_LONG == 64
718 +#define mb_correct_addr_and_bit(bit,addr) \
720 + bit += ((unsigned long) addr & 7UL) << 3; \
721 + addr = (void *) ((unsigned long) addr & ~7UL); \
723 +#elif BITS_PER_LONG == 32
724 +#define mb_correct_addr_and_bit(bit,addr) \
726 + bit += ((unsigned long) addr & 3UL) << 3; \
727 + addr = (void *) ((unsigned long) addr & ~3UL); \
730 +#error "how many bits you are?!"
733 +static inline int mb_test_bit(int bit, void *addr)
735 + mb_correct_addr_and_bit(bit,addr);
736 + return ext2_test_bit(bit, addr);
739 +static inline void mb_set_bit(int bit, void *addr)
741 + mb_correct_addr_and_bit(bit,addr);
742 + ext2_set_bit(bit, addr);
745 +static inline void mb_set_bit_atomic(int bit, void *addr)
747 + mb_correct_addr_and_bit(bit,addr);
748 + ext2_set_bit_atomic(NULL, bit, addr);
751 +static inline void mb_clear_bit(int bit, void *addr)
753 + mb_correct_addr_and_bit(bit,addr);
754 + ext2_clear_bit(bit, addr);
757 +static inline void mb_clear_bit_atomic(int bit, void *addr)
759 + mb_correct_addr_and_bit(bit,addr);
760 + ext2_clear_bit_atomic(NULL, bit, addr);
763 +static inline int mb_find_next_zero_bit(void *addr, int max, int start)
766 +#if BITS_PER_LONG == 64
767 + fix = ((unsigned long) addr & 7UL) << 3;
768 + addr = (void *) ((unsigned long) addr & ~7UL);
769 +#elif BITS_PER_LONG == 32
770 + fix = ((unsigned long) addr & 3UL) << 3;
771 + addr = (void *) ((unsigned long) addr & ~3UL);
773 +#error "how many bits you are?!"
777 + return ext2_find_next_zero_bit(addr, max, start) - fix;
780 +static inline int mb_find_next_bit(void *addr, int max, int start)
783 +#if BITS_PER_LONG == 64
784 + fix = ((unsigned long) addr & 7UL) << 3;
785 + addr = (void *) ((unsigned long) addr & ~7UL);
786 +#elif BITS_PER_LONG == 32
787 + fix = ((unsigned long) addr & 3UL) << 3;
788 + addr = (void *) ((unsigned long) addr & ~3UL);
790 +#error "how many bits you are?!"
797 + return find_next_bit(addr, max, start) - fix;
801 +static inline void *mb_find_buddy(struct ext3_buddy *e3b, int order, int *max)
805 + BUG_ON(EXT3_MB_BITMAP(e3b) == EXT3_MB_BUDDY(e3b));
806 + BUG_ON(max == NULL);
808 + if (order > e3b->bd_blkbits + 1) {
813 + /* at order 0 we see each particular block */
814 + *max = 1 << (e3b->bd_blkbits + 3);
816 + return EXT3_MB_BITMAP(e3b);
818 + bb = EXT3_MB_BUDDY(e3b) + EXT3_SB(e3b->bd_sb)->s_mb_offsets[order];
819 + *max = EXT3_SB(e3b->bd_sb)->s_mb_maxs[order];
825 +void mb_free_blocks_double(struct inode *inode, struct ext3_buddy *e3b,
826 + int first, int count)
829 + struct super_block *sb = e3b->bd_sb;
831 + if (unlikely(e3b->bd_info->bb_bitmap == NULL))
833 + BUG_ON(!ext3_is_group_locked(sb, e3b->bd_group));
834 + for (i = 0; i < count; i++) {
835 + if (!mb_test_bit(first + i, e3b->bd_info->bb_bitmap)) {
836 + unsigned long blocknr;
837 + blocknr = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb);
838 + blocknr += first + i;
840 + le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
842 + ext3_error(sb, __FUNCTION__, "double-free of inode"
843 + " %lu's block %lu(bit %u in group %u)\n",
844 + inode ? inode->i_ino : 0, blocknr,
845 + first + i, e3b->bd_group);
847 + mb_clear_bit(first + i, e3b->bd_info->bb_bitmap);
851 +void mb_mark_used_double(struct ext3_buddy *e3b, int first, int count)
854 + if (unlikely(e3b->bd_info->bb_bitmap == NULL))
856 + BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
857 + for (i = 0; i < count; i++) {
858 + BUG_ON(mb_test_bit(first + i, e3b->bd_info->bb_bitmap));
859 + mb_set_bit(first + i, e3b->bd_info->bb_bitmap);
863 +void mb_cmp_bitmaps(struct ext3_buddy *e3b, void *bitmap)
865 + if (memcmp(e3b->bd_info->bb_bitmap, bitmap, e3b->bd_sb->s_blocksize)) {
866 + unsigned char *b1, *b2;
868 + b1 = (unsigned char *) e3b->bd_info->bb_bitmap;
869 + b2 = (unsigned char *) bitmap;
870 + for (i = 0; i < e3b->bd_sb->s_blocksize; i++) {
871 + if (b1[i] != b2[i]) {
872 + printk("corruption in group %u at byte %u(%u): "
873 + "%x in copy != %x on disk/prealloc\n",
874 + e3b->bd_group, i, i * 8, b1[i], b2[i]);
882 +#define mb_free_blocks_double(a,b,c,d)
883 +#define mb_mark_used_double(a,b,c)
884 +#define mb_cmp_bitmaps(a,b)
887 +#ifdef AGGRESSIVE_CHECK
889 +#define MB_CHECK_ASSERT(assert) \
892 + printk (KERN_EMERG \
893 + "Assertion failure in %s() at %s:%d: \"%s\"\n", \
894 + function, file, line, # assert); \
899 +static int __mb_check_buddy(struct ext3_buddy *e3b, char *file,
900 + const char *function, int line)
902 + struct super_block *sb = e3b->bd_sb;
903 + int order = e3b->bd_blkbits + 1;
904 + int max, max2, i, j, k, count;
905 + struct ext3_group_info *grp;
906 + int fragments = 0, fstart;
907 + struct list_head *cur;
908 + void *buddy, *buddy2;
910 + if (!test_opt(sb, MBALLOC))
914 + static int mb_check_counter = 0;
915 + if (mb_check_counter++ % 100 != 0)
919 + while (order > 1) {
920 + buddy = mb_find_buddy(e3b, order, &max);
921 + MB_CHECK_ASSERT(buddy);
922 + buddy2 = mb_find_buddy(e3b, order - 1, &max2);
923 + MB_CHECK_ASSERT(buddy2);
924 + MB_CHECK_ASSERT(buddy != buddy2);
925 + MB_CHECK_ASSERT(max * 2 == max2);
928 + for (i = 0; i < max; i++) {
930 + if (mb_test_bit(i, buddy)) {
931 + /* only single bit in buddy2 may be 1 */
932 + if (!mb_test_bit(i << 1, buddy2))
933 + MB_CHECK_ASSERT(mb_test_bit((i<<1)+1, buddy2));
934 + else if (!mb_test_bit((i << 1) + 1, buddy2))
935 + MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
939 + /* both bits in buddy2 must be 0 */
940 + MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
941 + MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
943 + for (j = 0; j < (1 << order); j++) {
944 + k = (i * (1 << order)) + j;
945 + MB_CHECK_ASSERT(!mb_test_bit(k, EXT3_MB_BITMAP(e3b)));
949 + MB_CHECK_ASSERT(e3b->bd_info->bb_counters[order] == count);
954 + buddy = mb_find_buddy(e3b, 0, &max);
955 + for (i = 0; i < max; i++) {
956 + if (!mb_test_bit(i, buddy)) {
957 + MB_CHECK_ASSERT(i >= e3b->bd_info->bb_first_free);
958 + if (fstart == -1) {
965 + /* check used bits only */
966 + for (j = 0; j < e3b->bd_blkbits + 1; j++) {
967 + buddy2 = mb_find_buddy(e3b, j, &max2);
969 + MB_CHECK_ASSERT(k < max2);
970 + MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
973 + MB_CHECK_ASSERT(!EXT3_MB_GRP_NEED_INIT(e3b->bd_info));
974 + MB_CHECK_ASSERT(e3b->bd_info->bb_fragments == fragments);
976 + grp = EXT3_GROUP_INFO(sb, e3b->bd_group);
977 + buddy = mb_find_buddy(e3b, 0, &max);
978 + list_for_each(cur, &grp->bb_prealloc_list) {
979 + unsigned long groupnr;
980 + struct ext3_prealloc_space *pa;
981 + pa = list_entry(cur, struct ext3_prealloc_space, group_list);
982 + ext3_get_group_no_and_offset(sb, pa->pstart, &groupnr, &k);
983 + MB_CHECK_ASSERT(groupnr == e3b->bd_group);
984 + for (i = 0; i < pa->len; i++)
985 + MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
989 +#undef MB_CHECK_ASSERT
990 +#define mb_check_buddy(e3b) __mb_check_buddy(e3b,__FILE__,__FUNCTION__,__LINE__)
992 +#define mb_check_buddy(e3b)
995 +/* find most significant bit */
996 +static int inline fmsb(unsigned short word)
1010 + } while (word != 0);
1016 +ext3_mb_mark_free_simple(struct super_block *sb, void *buddy, unsigned first,
1017 + int len, struct ext3_group_info *grp)
1019 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1020 + unsigned short min, max, chunk, border;
1022 + BUG_ON(len >= EXT3_BLOCKS_PER_GROUP(sb));
1024 + border = 2 << sb->s_blocksize_bits;
1027 + /* find how many blocks can be covered since this position */
1028 + max = ffs(first | border) - 1;
1030 + /* find how many blocks of power 2 we need to mark */
1037 + /* mark multiblock chunks only */
1038 + grp->bb_counters[min]++;
1040 + mb_clear_bit(first >> min, buddy + sbi->s_mb_offsets[min]);
1048 +ext3_mb_generate_buddy(struct super_block *sb, void *buddy, void *bitmap,
1051 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
1052 + unsigned short max = EXT3_BLOCKS_PER_GROUP(sb);
1053 + unsigned short i = 0, first, len;
1054 + unsigned free = 0, fragments = 0;
1055 + unsigned long long period = get_cycles();
1057 + /* initialize buddy from bitmap which is aggregation
1058 + * of on-disk bitmap and preallocations */
1059 + i = mb_find_next_zero_bit(bitmap, max, 0);
1060 + grp->bb_first_free = i;
1064 + i = ext2_find_next_le_bit(bitmap, max, i);
1068 + ext3_mb_mark_free_simple(sb, buddy, first, len, grp);
1070 + grp->bb_counters[0]++;
1072 + i = mb_find_next_zero_bit(bitmap, max, i);
1074 + grp->bb_fragments = fragments;
1076 + if (free != grp->bb_free) {
1077 + printk("EXT3-fs: group %u: %u blocks in bitmap, %u in gd\n",
1078 + group, free, grp->bb_free);
1079 + grp->bb_free = free;
1082 + clear_bit(EXT3_GROUP_INFO_NEED_INIT_BIT, &grp->bb_state);
1084 + period = get_cycles() - period;
1085 + spin_lock(&EXT3_SB(sb)->s_bal_lock);
1086 + EXT3_SB(sb)->s_mb_buddies_generated++;
1087 + EXT3_SB(sb)->s_mb_generation_time += period;
1088 + spin_unlock(&EXT3_SB(sb)->s_bal_lock);
1091 +static int ext3_mb_init_cache(struct page *page, char *incore)
1093 + int blocksize, blocks_per_page, groups_per_page;
1094 + int err = 0, i, first_group, first_block;
1095 + struct super_block *sb;
1096 + struct buffer_head *bhs;
1097 + struct buffer_head **bh;
1098 + struct inode *inode;
1099 + char *data, *bitmap;
1101 + mb_debug("init page %lu\n", page->index);
1103 + inode = page->mapping->host;
1105 + blocksize = 1 << inode->i_blkbits;
1106 + blocks_per_page = PAGE_CACHE_SIZE / blocksize;
1108 + groups_per_page = blocks_per_page >> 1;
1109 + if (groups_per_page == 0)
1110 + groups_per_page = 1;
1112 + /* allocate buffer_heads to read bitmaps */
1113 + if (groups_per_page > 1) {
1115 + i = sizeof(struct buffer_head *) * groups_per_page;
1116 + bh = kmalloc(i, GFP_NOFS);
1123 + first_group = page->index * blocks_per_page / 2;
1125 + /* read all groups the page covers into the cache */
1126 + for (i = 0; i < groups_per_page; i++) {
1127 + struct ext3_group_desc * desc;
1129 + if (first_group + i >= EXT3_SB(sb)->s_groups_count)
1133 + desc = ext3_get_group_desc(sb, first_group + i, NULL);
1138 + bh[i] = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
1139 + if (bh[i] == NULL)
1142 + if (buffer_uptodate(bh[i]))
1145 + lock_buffer(bh[i]);
1146 + if (buffer_uptodate(bh[i])) {
1147 + unlock_buffer(bh[i]);
1152 + bh[i]->b_end_io = end_buffer_read_sync;
1153 + submit_bh(READ, bh[i]);
1154 + mb_debug("read bitmap for group %u\n", first_group + i);
1157 + /* wait for I/O completion */
1158 + for (i = 0; i < groups_per_page && bh[i]; i++)
1159 + wait_on_buffer(bh[i]);
1162 + for (i = 0; i < groups_per_page && bh[i]; i++)
1163 + if (!buffer_uptodate(bh[i]))
1166 + first_block = page->index * blocks_per_page;
1167 + for (i = 0; i < blocks_per_page; i++) {
1170 + group = (first_block + i) >> 1;
1171 + if (group >= EXT3_SB(sb)->s_groups_count)
1174 + data = page_address(page) + (i * blocksize);
1175 + bitmap = bh[group - first_group]->b_data;
1177 + if ((first_block + i) & 1) {
1178 + /* this is block of buddy */
1179 + BUG_ON(incore == NULL);
1180 + mb_debug("put buddy for group %u in page %lu/%x\n",
1181 + group, page->index, i * blocksize);
1182 + memset(data, 0xff, blocksize);
1183 + EXT3_GROUP_INFO(sb, group)->bb_fragments = 0;
1184 + memset(EXT3_GROUP_INFO(sb, group)->bb_counters, 0,
1185 + sizeof(unsigned short)*(sb->s_blocksize_bits+2));
1186 + ext3_mb_generate_buddy(sb, data, incore, group);
1189 + /* this is block of bitmap */
1190 + BUG_ON(incore != NULL);
1191 + mb_debug("put bitmap for group %u in page %lu/%x\n",
1192 + group, page->index, i * blocksize);
1194 + /* see comments in ext3_mb_put_pa() */
1195 + ext3_lock_group(sb, group);
1196 + memcpy(data, bitmap, blocksize);
1198 + /* mark all preallocated blocks used in in-core bitmap */
1199 + ext3_mb_generate_from_pa(sb, data, group);
1200 + ext3_unlock_group(sb, group);
1205 + SetPageUptodate(page);
1209 + for (i = 0; i < groups_per_page && bh[i]; i++)
1217 +static int ext3_mb_load_buddy(struct super_block *sb, int group,
1218 + struct ext3_buddy *e3b)
1220 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1221 + struct inode *inode = sbi->s_buddy_cache;
1222 + int blocks_per_page, block, pnum, poff;
1223 + struct page *page;
1225 + mb_debug("load group %u\n", group);
1227 + blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1229 + e3b->bd_blkbits = sb->s_blocksize_bits;
1230 + e3b->bd_info = EXT3_GROUP_INFO(sb, group);
1232 + e3b->bd_group = group;
1233 + e3b->bd_buddy_page = NULL;
1234 + e3b->bd_bitmap_page = NULL;
1236 + block = group * 2;
1237 + pnum = block / blocks_per_page;
1238 + poff = block % blocks_per_page;
1240 + /* we could use find_or_create_page(), but it locks page
1241 + * what we'd like to avoid in fast path ... */
1242 + page = find_get_page(inode->i_mapping, pnum);
1243 + if (page == NULL || !PageUptodate(page)) {
1245 + page_cache_release(page);
1246 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1248 + BUG_ON(page->mapping != inode->i_mapping);
1249 + if (!PageUptodate(page)) {
1250 + ext3_mb_init_cache(page, NULL);
1251 + mb_cmp_bitmaps(e3b, page_address(page) +
1252 + (poff * sb->s_blocksize));
1254 + unlock_page(page);
1257 + if (page == NULL || !PageUptodate(page))
1259 + e3b->bd_bitmap_page = page;
1260 + e3b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1261 + mark_page_accessed(page);
1264 + pnum = block / blocks_per_page;
1265 + poff = block % blocks_per_page;
1267 + page = find_get_page(inode->i_mapping, pnum);
1268 + if (page == NULL || !PageUptodate(page)) {
1270 + page_cache_release(page);
1271 + page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1273 + BUG_ON(page->mapping != inode->i_mapping);
1274 + if (!PageUptodate(page))
1275 + ext3_mb_init_cache(page, e3b->bd_bitmap);
1277 + unlock_page(page);
1280 + if (page == NULL || !PageUptodate(page))
1282 + e3b->bd_buddy_page = page;
1283 + e3b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1284 + mark_page_accessed(page);
1286 + BUG_ON(e3b->bd_bitmap_page == NULL);
1287 + BUG_ON(e3b->bd_buddy_page == NULL);
1292 + if (e3b->bd_bitmap_page)
1293 + page_cache_release(e3b->bd_bitmap_page);
1294 + if (e3b->bd_buddy_page)
1295 + page_cache_release(e3b->bd_buddy_page);
1296 + e3b->bd_buddy = NULL;
1297 + e3b->bd_bitmap = NULL;
1301 +static void ext3_mb_release_desc(struct ext3_buddy *e3b)
1303 + if (e3b->bd_bitmap_page)
1304 + page_cache_release(e3b->bd_bitmap_page);
1305 + if (e3b->bd_buddy_page)
1306 + page_cache_release(e3b->bd_buddy_page);
1310 +static int mb_find_order_for_block(struct ext3_buddy *e3b, int block)
1315 + BUG_ON(EXT3_MB_BITMAP(e3b) == EXT3_MB_BUDDY(e3b));
1316 + BUG_ON(block >= (1 << (e3b->bd_blkbits + 3)));
1318 + bb = EXT3_MB_BUDDY(e3b);
1319 + while (order <= e3b->bd_blkbits + 1) {
1320 + block = block >> 1;
1321 + if (!mb_test_bit(block, bb)) {
1322 + /* this block is part of buddy of order 'order' */
1325 + bb += 1 << (e3b->bd_blkbits - order);
1331 +static inline void mb_clear_bits(void *bm, int cur, int len)
1336 + while (cur < len) {
1337 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1338 + /* fast path: clear whole word at once */
1339 + addr = bm + (cur >> 3);
1344 + mb_clear_bit_atomic(cur, bm);
1349 +static inline void mb_set_bits(void *bm, int cur, int len)
1354 + while (cur < len) {
1355 + if ((cur & 31) == 0 && (len - cur) >= 32) {
1356 + /* fast path: clear whole word at once */
1357 + addr = bm + (cur >> 3);
1358 + *addr = 0xffffffff;
1362 + mb_set_bit_atomic(cur, bm);
1367 +static int mb_free_blocks(struct inode *inode, struct ext3_buddy *e3b,
1368 + int first, int count)
1370 + int block = 0, max = 0, order;
1371 + void *buddy, *buddy2;
1372 + struct super_block *sb = e3b->bd_sb;
1374 + BUG_ON(first + count > (sb->s_blocksize << 3));
1375 + BUG_ON(!ext3_is_group_locked(sb, e3b->bd_group));
1376 + mb_check_buddy(e3b);
1377 + mb_free_blocks_double(inode, e3b, first, count);
1379 + e3b->bd_info->bb_free += count;
1380 + if (first < e3b->bd_info->bb_first_free)
1381 + e3b->bd_info->bb_first_free = first;
1383 + /* let's maintain fragments counter */
1385 + block = !mb_test_bit(first - 1, EXT3_MB_BITMAP(e3b));
1386 + if (first + count < EXT3_SB(sb)->s_mb_maxs[0])
1387 + max = !mb_test_bit(first + count, EXT3_MB_BITMAP(e3b));
1389 + e3b->bd_info->bb_fragments--;
1390 + else if (!block && !max)
1391 + e3b->bd_info->bb_fragments++;
1393 + /* let's maintain buddy itself */
1394 + while (count-- > 0) {
1398 + if (!mb_test_bit(block, EXT3_MB_BITMAP(e3b))) {
1399 + unsigned long blocknr;
1400 + blocknr = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb);
1403 + le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block);
1405 + ext3_error(sb, __FUNCTION__, "double-free of inode"
1406 + " %lu's block %lu(bit %u in group %u)\n",
1407 + inode ? inode->i_ino : 0, blocknr, block,
1410 + mb_clear_bit(block, EXT3_MB_BITMAP(e3b));
1411 + e3b->bd_info->bb_counters[order]++;
1413 + /* start of the buddy */
1414 + buddy = mb_find_buddy(e3b, order, &max);
1418 + if (mb_test_bit(block, buddy) ||
1419 + mb_test_bit(block + 1, buddy))
1422 + /* both the buddies are free, try to coalesce them */
1423 + buddy2 = mb_find_buddy(e3b, order + 1, &max);
1429 + /* for special purposes, we don't set
1430 + * free bits in bitmap */
1431 + mb_set_bit(block, buddy);
1432 + mb_set_bit(block + 1, buddy);
1434 + e3b->bd_info->bb_counters[order]--;
1435 + e3b->bd_info->bb_counters[order]--;
1437 + block = block >> 1;
1439 + e3b->bd_info->bb_counters[order]++;
1441 + mb_clear_bit(block, buddy2);
1445 + mb_check_buddy(e3b);
1450 +static int mb_find_extent(struct ext3_buddy *e3b, int order, int block,
1451 + int needed, struct ext3_free_extent *ex)
1453 + int next = block, max, ord;
1456 + BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
1457 + BUG_ON(ex == NULL);
1459 + buddy = mb_find_buddy(e3b, order, &max);
1460 + BUG_ON(buddy == NULL);
1461 + BUG_ON(block >= max);
1462 + if (mb_test_bit(block, buddy)) {
1469 + if (likely(order == 0)) {
1470 + /* find actual order */
1471 + order = mb_find_order_for_block(e3b, block);
1472 + block = block >> order;
1475 + ex->fe_len = 1 << order;
1476 + ex->fe_start = block << order;
1477 + ex->fe_group = e3b->bd_group;
1479 + /* calc difference from given start */
1480 + next = next - ex->fe_start;
1481 + ex->fe_len -= next;
1482 + ex->fe_start += next;
1484 + while (needed > ex->fe_len && (buddy = mb_find_buddy(e3b, order, &max))) {
1486 + if (block + 1 >= max)
1489 + next = (block + 1) * (1 << order);
1490 + if (mb_test_bit(next, EXT3_MB_BITMAP(e3b)))
1493 + ord = mb_find_order_for_block(e3b, next);
1496 + block = next >> order;
1497 + ex->fe_len += 1 << order;
1500 + BUG_ON(ex->fe_start + ex->fe_len > (1 << (e3b->bd_blkbits + 3)));
1501 + return ex->fe_len;
1504 +static int mb_mark_used(struct ext3_buddy *e3b, struct ext3_free_extent *ex)
1506 + int ord, mlen = 0, max = 0, cur;
1507 + int start = ex->fe_start;
1508 + int len = ex->fe_len;
1513 + BUG_ON(start + len > (e3b->bd_sb->s_blocksize << 3));
1514 + BUG_ON(e3b->bd_group != ex->fe_group);
1515 + BUG_ON(!ext3_is_group_locked(e3b->bd_sb, e3b->bd_group));
1516 + mb_check_buddy(e3b);
1517 + mb_mark_used_double(e3b, start, len);
1519 + e3b->bd_info->bb_free -= len;
1520 + if (e3b->bd_info->bb_first_free == start)
1521 + e3b->bd_info->bb_first_free += len;
1523 + /* let's maintain fragments counter */
1525 + mlen = !mb_test_bit(start - 1, EXT3_MB_BITMAP(e3b));
1526 + if (start + len < EXT3_SB(e3b->bd_sb)->s_mb_maxs[0])
1527 + max = !mb_test_bit(start + len, EXT3_MB_BITMAP(e3b));
1529 + e3b->bd_info->bb_fragments++;
1530 + else if (!mlen && !max)
1531 + e3b->bd_info->bb_fragments--;
1533 + /* let's maintain buddy itself */
1535 + ord = mb_find_order_for_block(e3b, start);
1537 + if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1538 + /* the whole chunk may be allocated at once! */
1540 + buddy = mb_find_buddy(e3b, ord, &max);
1541 + BUG_ON((start >> ord) >= max);
1542 + mb_set_bit(start >> ord, buddy);
1543 + e3b->bd_info->bb_counters[ord]--;
1550 + /* store for history */
1552 + ret = len | (ord << 16);
1554 + /* we have to split large buddy */
1556 + buddy = mb_find_buddy(e3b, ord, &max);
1557 + mb_set_bit(start >> ord, buddy);
1558 + e3b->bd_info->bb_counters[ord]--;
1561 + cur = (start >> ord) & ~1U;
1562 + buddy = mb_find_buddy(e3b, ord, &max);
1563 + mb_clear_bit(cur, buddy);
1564 + mb_clear_bit(cur + 1, buddy);
1565 + e3b->bd_info->bb_counters[ord]++;
1566 + e3b->bd_info->bb_counters[ord]++;
1569 + mb_set_bits(EXT3_MB_BITMAP(e3b), ex->fe_start, len0);
1570 + mb_check_buddy(e3b);
1576 + * Must be called under group lock!
1578 +static void ext3_mb_use_best_found(struct ext3_allocation_context *ac,
1579 + struct ext3_buddy *e3b)
1581 + unsigned long ret;
1583 + BUG_ON(ac->ac_b_ex.fe_group != e3b->bd_group);
1584 + BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1586 + ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1587 + ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1588 + ret = mb_mark_used(e3b, &ac->ac_b_ex);
1590 + /* preallocation can change ac_b_ex, thus we store actually
1591 + * allocated blocks for history */
1592 + ac->ac_f_ex = ac->ac_b_ex;
1594 + ac->ac_status = AC_STATUS_FOUND;
1595 + ac->ac_tail = ret & 0xffff;
1596 + ac->ac_buddy = ret >> 16;
1598 + /* XXXXXXX: SUCH A HORRIBLE **CK */
1599 + ac->ac_bitmap_page = e3b->bd_bitmap_page;
1600 + get_page(ac->ac_bitmap_page);
1601 + ac->ac_buddy_page = e3b->bd_buddy_page;
1602 + get_page(ac->ac_buddy_page);
1606 + * regular allocator, for general purposes allocation
1609 +void ext3_mb_check_limits(struct ext3_allocation_context *ac,
1610 + struct ext3_buddy *e3b,
1613 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1614 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1615 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1616 + struct ext3_free_extent ex;
1620 + * We don't want to scan for a whole year
1622 + if (ac->ac_found > sbi->s_mb_max_to_scan &&
1623 + !(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1624 + ac->ac_status = AC_STATUS_BREAK;
1629 + * Haven't found good chunk so far, let's continue
1631 + if (bex->fe_len < gex->fe_len)
1634 + if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1635 + && bex->fe_group == e3b->bd_group) {
1636 + /* recheck chunk's availability - we don't know
1637 + * when it was found (within this lock-unlock
1638 + * period or not) */
1639 + max = mb_find_extent(e3b, 0, bex->fe_start, gex->fe_len, &ex);
1640 + if (max >= gex->fe_len) {
1641 + ext3_mb_use_best_found(ac, e3b);
1648 + * The routine checks whether found extent is good enough. If it is,
1649 + * then the extent gets marked used and flag is set to the context
1650 + * to stop scanning. Otherwise, the extent is compared with the
1651 + * previous found extent and if new one is better, then it's stored
1652 + * in the context. Later, the best found extent will be used, if
1653 + * mballoc can't find good enough extent.
1655 + * FIXME: real allocation policy is to be designed yet!
1657 +static void ext3_mb_measure_extent(struct ext3_allocation_context *ac,
1658 + struct ext3_free_extent *ex,
1659 + struct ext3_buddy *e3b)
1661 + struct ext3_free_extent *bex = &ac->ac_b_ex;
1662 + struct ext3_free_extent *gex = &ac->ac_g_ex;
1664 + BUG_ON(ex->fe_len <= 0);
1665 + BUG_ON(ex->fe_len >= (1 << ac->ac_sb->s_blocksize_bits) * 8);
1666 + BUG_ON(ex->fe_start >= (1 << ac->ac_sb->s_blocksize_bits) * 8);
1667 + BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1672 + * The special case - take what you catch first
1674 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
1676 + ext3_mb_use_best_found(ac, e3b);
1681 + * Let's check whether the chuck is good enough
1683 + if (ex->fe_len == gex->fe_len) {
1685 + ext3_mb_use_best_found(ac, e3b);
1690 + * If this is first found extent, just store it in the context
1692 + if (bex->fe_len == 0) {
1698 + * If new found extent is better, store it in the context
1700 + if (bex->fe_len < gex->fe_len) {
1701 + /* if the request isn't satisfied, any found extent
1702 + * larger than previous best one is better */
1703 + if (ex->fe_len > bex->fe_len)
1705 + } else if (ex->fe_len > gex->fe_len) {
1706 + /* if the request is satisfied, then we try to find
1707 + * an extent that still satisfy the request, but is
1708 + * smaller than previous one */
1712 + ext3_mb_check_limits(ac, e3b, 0);
1715 +static int ext3_mb_try_best_found(struct ext3_allocation_context *ac,
1716 + struct ext3_buddy *e3b)
1718 + struct ext3_free_extent ex = ac->ac_b_ex;
1719 + int group = ex.fe_group, max, err;
1721 + BUG_ON(ex.fe_len <= 0);
1722 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1726 + ext3_lock_group(ac->ac_sb, group);
1727 + max = mb_find_extent(e3b, 0, ex.fe_start, ex.fe_len, &ex);
1731 + ext3_mb_use_best_found(ac, e3b);
1734 + ext3_unlock_group(ac->ac_sb, group);
1735 + ext3_mb_release_desc(e3b);
1740 +static int ext3_mb_find_by_goal(struct ext3_allocation_context *ac,
1741 + struct ext3_buddy *e3b)
1743 + int group = ac->ac_g_ex.fe_group, max, err;
1744 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
1745 + struct ext3_super_block *es = sbi->s_es;
1746 + struct ext3_free_extent ex;
1748 + err = ext3_mb_load_buddy(ac->ac_sb, group, e3b);
1752 + ext3_lock_group(ac->ac_sb, group);
1753 + max = mb_find_extent(e3b, 0, ac->ac_g_ex.fe_start,
1754 + ac->ac_g_ex.fe_len, &ex);
1756 + if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1757 + unsigned long start;
1758 + start = (e3b->bd_group * EXT3_BLOCKS_PER_GROUP(ac->ac_sb) +
1759 + ex.fe_start + le32_to_cpu(es->s_first_data_block));
1760 + if (start % sbi->s_stripe == 0) {
1763 + ext3_mb_use_best_found(ac, e3b);
1765 + } else if (max >= ac->ac_g_ex.fe_len) {
1766 + BUG_ON(ex.fe_len <= 0);
1767 + BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1768 + BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1771 + ext3_mb_use_best_found(ac, e3b);
1772 + } else if (max > 0 && (ac->ac_flags & EXT3_MB_HINT_MERGE)) {
1773 + /* Sometimes, caller may want to merge even small
1774 + * number of blocks to an existing extent */
1775 + BUG_ON(ex.fe_len <= 0);
1776 + BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1777 + BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1780 + ext3_mb_use_best_found(ac, e3b);
1782 + ext3_unlock_group(ac->ac_sb, group);
1783 + ext3_mb_release_desc(e3b);
1789 + * The routine scans buddy structures (not bitmap!) from given order
1790 + * to max order and tries to find big enough chunk to satisfy the req
1792 +static void ext3_mb_simple_scan_group(struct ext3_allocation_context *ac,
1793 + struct ext3_buddy *e3b)
1795 + struct super_block *sb = ac->ac_sb;
1796 + struct ext3_group_info *grp = e3b->bd_info;
1800 + BUG_ON(ac->ac_2order <= 0);
1801 + for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1802 + if (grp->bb_counters[i] == 0)
1805 + buddy = mb_find_buddy(e3b, i, &max);
1806 + BUG_ON(buddy == NULL);
1808 + k = mb_find_next_zero_bit(buddy, max, 0);
1813 + ac->ac_b_ex.fe_len = 1 << i;
1814 + ac->ac_b_ex.fe_start = k << i;
1815 + ac->ac_b_ex.fe_group = e3b->bd_group;
1817 + ext3_mb_use_best_found(ac, e3b);
1819 + BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1821 + if (EXT3_SB(sb)->s_mb_stats)
1822 + atomic_inc(&EXT3_SB(sb)->s_bal_2orders);
1829 + * The routine scans the group and measures all found extents.
1830 + * In order to optimize scanning, caller must pass number of
1831 + * free blocks in the group, so the routine can know upper limit.
1833 +static void ext3_mb_complex_scan_group(struct ext3_allocation_context *ac,
1834 + struct ext3_buddy *e3b)
1836 + struct super_block *sb = ac->ac_sb;
1837 + void *bitmap = EXT3_MB_BITMAP(e3b);
1838 + struct ext3_free_extent ex;
1841 + free = e3b->bd_info->bb_free;
1842 + BUG_ON(free <= 0);
1844 + i = e3b->bd_info->bb_first_free;
1846 + while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1847 + i = mb_find_next_zero_bit(bitmap, sb->s_blocksize * 8, i);
1848 + if (i >= sb->s_blocksize * 8) {
1849 + BUG_ON(free != 0);
1853 + mb_find_extent(e3b, 0, i, ac->ac_g_ex.fe_len, &ex);
1854 + BUG_ON(ex.fe_len <= 0);
1855 + BUG_ON(free < ex.fe_len);
1857 + ext3_mb_measure_extent(ac, &ex, e3b);
1860 + free -= ex.fe_len;
1863 + ext3_mb_check_limits(ac, e3b, 1);
1867 + * This is a special case for storages like raid5
1868 + * we try to find stripe-aligned chunks for stripe-size requests
1870 +static void ext3_mb_scan_aligned(struct ext3_allocation_context *ac,
1871 + struct ext3_buddy *e3b)
1873 + struct super_block *sb = ac->ac_sb;
1874 + struct ext3_sb_info *sbi = EXT3_SB(sb);
1875 + void *bitmap = EXT3_MB_BITMAP(e3b);
1876 + struct ext3_free_extent ex;
1877 + unsigned long i, max;
1879 + BUG_ON(sbi->s_stripe == 0);
1881 + /* find first stripe-aligned block */
1882 + i = e3b->bd_group * EXT3_BLOCKS_PER_GROUP(sb)
1883 + + le32_to_cpu(sbi->s_es->s_first_data_block);
1884 + i = ((i + sbi->s_stripe - 1) / sbi->s_stripe) * sbi->s_stripe;
1885 + i = (i - le32_to_cpu(sbi->s_es->s_first_data_block))
1886 + % EXT3_BLOCKS_PER_GROUP(sb);
1888 + while (i < sb->s_blocksize * 8) {
1889 + if (!mb_test_bit(i, bitmap)) {
1890 + max = mb_find_extent(e3b, 0, i, sbi->s_stripe, &ex);
1891 + if (max >= sbi->s_stripe) {
1894 + ext3_mb_use_best_found(ac, e3b);
1898 + i += sbi->s_stripe;
1902 +static int ext3_mb_good_group(struct ext3_allocation_context *ac,
1903 + int group, int cr)
1905 + struct ext3_group_info *grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1906 + unsigned free, fragments, i, bits;
1908 + BUG_ON(cr < 0 || cr >= 4);
1909 + BUG_ON(EXT3_MB_GRP_NEED_INIT(grp));
1911 + free = grp->bb_free;
1912 + fragments = grp->bb_fragments;
1915 + if (fragments == 0)
1920 + BUG_ON(ac->ac_2order == 0);
1921 + bits = ac->ac_sb->s_blocksize_bits + 1;
1922 + for (i = ac->ac_2order; i <= bits; i++)
1923 + if (grp->bb_counters[i] > 0)
1927 + if ((free / fragments) >= ac->ac_g_ex.fe_len)
1931 + if (free >= ac->ac_g_ex.fe_len)
1943 +int ext3_mb_regular_allocator(struct ext3_allocation_context *ac)
1945 + int group, i, cr, err = 0;
1946 + struct ext3_sb_info *sbi;
1947 + struct super_block *sb;
1948 + struct ext3_buddy e3b;
1951 + sbi = EXT3_SB(sb);
1952 + BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1954 + /* first, try the goal */
1955 + err = ext3_mb_find_by_goal(ac, &e3b);
1956 + if (err || ac->ac_status == AC_STATUS_FOUND)
1959 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
1962 + i = ffs(ac->ac_g_ex.fe_len);
1963 + ac->ac_2order = 0;
1964 + if (i >= sbi->s_mb_order2_reqs) {
1966 + if ((ac->ac_g_ex.fe_len & (~(1 << i))) == 0)
1967 + ac->ac_2order = i;
1970 + group = ac->ac_g_ex.fe_group;
1972 + /* Let's just scan groups to find more-less suitable blocks */
1973 + cr = ac->ac_2order ? 0 : 1;
1975 + for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
1976 + ac->ac_criteria = cr;
1977 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; group++, i++) {
1978 + struct ext3_group_info *grp;
1980 + if (group == EXT3_SB(sb)->s_groups_count)
1983 + /* quick check to skip empty groups */
1984 + grp = EXT3_GROUP_INFO(ac->ac_sb, group);
1985 + if (grp->bb_free == 0)
1988 + if (EXT3_MB_GRP_NEED_INIT(EXT3_GROUP_INFO(sb, group))) {
1989 + /* we need full data about the group
1990 + * to make a good selection */
1991 + err = ext3_mb_load_buddy(sb, group, &e3b);
1994 + ext3_mb_release_desc(&e3b);
1997 + /* check is group good for our criteries */
1998 + if (!ext3_mb_good_group(ac, group, cr))
2001 + err = ext3_mb_load_buddy(sb, group, &e3b);
2005 + ext3_lock_group(sb, group);
2006 + if (!ext3_mb_good_group(ac, group, cr)) {
2007 + /* someone did allocation from this group */
2008 + ext3_unlock_group(sb, group);
2009 + ext3_mb_release_desc(&e3b);
2013 + ac->ac_groups_scanned++;
2015 + ext3_mb_simple_scan_group(ac, &e3b);
2016 + else if (cr == 1 && ac->ac_g_ex.fe_len == sbi->s_stripe)
2017 + ext3_mb_scan_aligned(ac, &e3b);
2019 + ext3_mb_complex_scan_group(ac, &e3b);
2021 + ext3_unlock_group(sb, group);
2022 + ext3_mb_release_desc(&e3b);
2024 + if (ac->ac_status != AC_STATUS_CONTINUE)
2029 + if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2030 + !(ac->ac_flags & EXT3_MB_HINT_FIRST)) {
2032 + * We've been searching too long. Let's try to allocate
2033 + * the best chunk we've found so far
2036 + ext3_mb_try_best_found(ac, &e3b);
2037 + if (ac->ac_status != AC_STATUS_FOUND) {
2039 + * Someone more lucky has already allocated it.
2040 + * The only thing we can do is just take first
2042 + printk(KERN_DEBUG "EXT3-fs: someone won our chunk\n");
2044 + ac->ac_b_ex.fe_group = 0;
2045 + ac->ac_b_ex.fe_start = 0;
2046 + ac->ac_b_ex.fe_len = 0;
2047 + ac->ac_status = AC_STATUS_CONTINUE;
2048 + ac->ac_flags |= EXT3_MB_HINT_FIRST;
2050 + atomic_inc(&sbi->s_mb_lost_chunks);
2058 +#ifdef EXT3_MB_HISTORY
2059 +struct ext3_mb_proc_session {
2060 + struct ext3_mb_history *history;
2061 + struct super_block *sb;
2066 +static void *ext3_mb_history_skip_empty(struct ext3_mb_proc_session *s,
2067 + struct ext3_mb_history *hs,
2070 + if (hs == s->history + s->max)
2072 + if (!first && hs == s->history + s->start)
2074 + while (hs->orig.fe_len == 0) {
2076 + if (hs == s->history + s->max)
2078 + if (hs == s->history + s->start)
2084 +static void *ext3_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2086 + struct ext3_mb_proc_session *s = seq->private;
2087 + struct ext3_mb_history *hs;
2091 + return SEQ_START_TOKEN;
2092 + hs = ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2095 + while (--l && (hs = ext3_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2099 +static void *ext3_mb_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
2101 + struct ext3_mb_proc_session *s = seq->private;
2102 + struct ext3_mb_history *hs = v;
2105 + if (v == SEQ_START_TOKEN)
2106 + return ext3_mb_history_skip_empty(s, s->history + s->start, 1);
2108 + return ext3_mb_history_skip_empty(s, ++hs, 0);
2111 +static int ext3_mb_seq_history_show(struct seq_file *seq, void *v)
2113 + char buf[25], buf2[25], buf3[25], *fmt;
2114 + struct ext3_mb_history *hs = v;
2116 + if (v == SEQ_START_TOKEN) {
2117 + seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2118 + "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2119 + "pid", "inode", "original", "goal", "result","found",
2120 + "grps", "cr", "flags", "merge", "tail", "broken");
2124 + if (hs->op == EXT3_MB_HISTORY_ALLOC) {
2125 + fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2126 + "%-5u %-5s %-5u %-6u\n";
2127 + sprintf(buf2, "%lu/%lu/%lu@%lu", hs->result.fe_group,
2128 + hs->result.fe_start, hs->result.fe_len,
2129 + hs->result.fe_logical);
2130 + sprintf(buf, "%lu/%lu/%lu@%lu", hs->orig.fe_group,
2131 + hs->orig.fe_start, hs->orig.fe_len,
2132 + hs->orig.fe_logical);
2133 + sprintf(buf3, "%lu/%lu/%lu@%lu", hs->goal.fe_group,
2134 + hs->goal.fe_start, hs->goal.fe_len,
2135 + hs->goal.fe_logical);
2136 + seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2137 + hs->found, hs->groups, hs->cr, hs->flags,
2138 + hs->merged ? "M" : "", hs->tail,
2139 + hs->buddy ? 1 << hs->buddy : 0);
2140 + } else if (hs->op == EXT3_MB_HISTORY_PREALLOC) {
2141 + fmt = "%-5u %-8u %-23s %-23s %-23s\n";
2142 + sprintf(buf2, "%lu/%lu/%lu@%lu", hs->result.fe_group,
2143 + hs->result.fe_start, hs->result.fe_len,
2144 + hs->result.fe_logical);
2145 + sprintf(buf, "%lu/%lu/%lu@%lu", hs->orig.fe_group,
2146 + hs->orig.fe_start, hs->orig.fe_len,
2147 + hs->orig.fe_logical);
2148 + seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2149 + } else if (hs->op == EXT3_MB_HISTORY_DISCARD) {
2150 + sprintf(buf2, "%lu/%lu/%lu", hs->result.fe_group,
2151 + hs->result.fe_start, hs->result.fe_len);
2152 + seq_printf(seq, "%-5u %-8u %-23s discard\n",
2153 + hs->pid, hs->ino, buf2);
2154 + } else if (hs->op == EXT3_MB_HISTORY_FREE) {
2155 + sprintf(buf2, "%lu/%lu/%lu", hs->result.fe_group,
2156 + hs->result.fe_start, hs->result.fe_len);
2157 + seq_printf(seq, "%-5u %-8u %-23s free\n",
2158 + hs->pid, hs->ino, buf2);
2163 +static void ext3_mb_seq_history_stop(struct seq_file *seq, void *v)
2167 +static struct seq_operations ext3_mb_seq_history_ops = {
2168 + .start = ext3_mb_seq_history_start,
2169 + .next = ext3_mb_seq_history_next,
2170 + .stop = ext3_mb_seq_history_stop,
2171 + .show = ext3_mb_seq_history_show,
2174 +static int ext3_mb_seq_history_open(struct inode *inode, struct file *file)
2176 + struct super_block *sb = PDE(inode)->data;
2177 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2178 + struct ext3_mb_proc_session *s;
2181 + s = kmalloc(sizeof(*s), GFP_KERNEL);
2185 + size = sizeof(struct ext3_mb_history) * sbi->s_mb_history_max;
2186 + s->history = kmalloc(size, GFP_KERNEL);
2187 + if (s->history == NULL) {
2192 + spin_lock(&sbi->s_mb_history_lock);
2193 + memcpy(s->history, sbi->s_mb_history, size);
2194 + s->max = sbi->s_mb_history_max;
2195 + s->start = sbi->s_mb_history_cur % s->max;
2196 + spin_unlock(&sbi->s_mb_history_lock);
2198 + rc = seq_open(file, &ext3_mb_seq_history_ops);
2200 + struct seq_file *m = (struct seq_file *)file->private_data;
2203 + kfree(s->history);
2210 +static int ext3_mb_seq_history_release(struct inode *inode, struct file *file)
2212 + struct seq_file *seq = (struct seq_file *)file->private_data;
2213 + struct ext3_mb_proc_session *s = seq->private;
2214 + kfree(s->history);
2216 + return seq_release(inode, file);
2219 +static ssize_t ext3_mb_seq_history_write(struct file *file,
2220 + const char __user *buffer,
2221 + size_t count, loff_t *ppos)
2223 + struct seq_file *seq = (struct seq_file *)file->private_data;
2224 + struct ext3_mb_proc_session *s = seq->private;
2225 + struct super_block *sb = s->sb;
2229 + if (count >= sizeof(str)) {
2230 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2231 + "mb_history", (int)sizeof(str));
2232 + return -EOVERFLOW;
2235 + if (copy_from_user(str, buffer, count))
2238 + value = simple_strtol(str, NULL, 0);
2241 + EXT3_SB(sb)->s_mb_history_filter = value;
2246 +static struct file_operations ext3_mb_seq_history_fops = {
2247 + .owner = THIS_MODULE,
2248 + .open = ext3_mb_seq_history_open,
2250 + .write = ext3_mb_seq_history_write,
2251 + .llseek = seq_lseek,
2252 + .release = ext3_mb_seq_history_release,
2255 +static void *ext3_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2257 + struct super_block *sb = seq->private;
2258 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2261 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2265 + return (void *) group;
2268 +static void *ext3_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2270 + struct super_block *sb = seq->private;
2271 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2275 + if (*pos < 0 || *pos >= sbi->s_groups_count)
2278 + return (void *) group;;
2281 +static int ext3_mb_seq_groups_show(struct seq_file *seq, void *v)
2283 + struct super_block *sb = seq->private;
2284 + long group = (long) v;
2286 + struct ext3_buddy e3b;
2288 + struct ext3_group_info info;
2289 + unsigned short counters[16];
2294 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2295 + "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2296 + "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2297 + "group", "free", "frags", "first",
2298 + "2^0", "2^1", "2^2", "2^3", "2^4", "2^5","2^6",
2299 + "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2301 + i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2302 + sizeof(struct ext3_group_info);
2303 + err = ext3_mb_load_buddy(sb, group, &e3b);
2305 + seq_printf(seq, "#%-5lu: I/O error\n", group);
2308 + ext3_lock_group(sb, group);
2309 + memcpy(&sg, EXT3_GROUP_INFO(sb, group), i);
2310 + ext3_unlock_group(sb, group);
2311 + ext3_mb_release_desc(&e3b);
2313 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
2314 + sg.info.bb_fragments, sg.info.bb_first_free);
2315 + for (i = 0; i <= 13; i++)
2316 + seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2317 + sg.info.bb_counters[i] : 0);
2318 + seq_printf(seq, " ]\n");
2323 +static void ext3_mb_seq_groups_stop(struct seq_file *seq, void *v)
2327 +static struct seq_operations ext3_mb_seq_groups_ops = {
2328 + .start = ext3_mb_seq_groups_start,
2329 + .next = ext3_mb_seq_groups_next,
2330 + .stop = ext3_mb_seq_groups_stop,
2331 + .show = ext3_mb_seq_groups_show,
2334 +static int ext3_mb_seq_groups_open(struct inode *inode, struct file *file)
2336 + struct super_block *sb = PDE(inode)->data;
2339 + rc = seq_open(file, &ext3_mb_seq_groups_ops);
2341 + struct seq_file *m = (struct seq_file *)file->private_data;
2348 +static struct file_operations ext3_mb_seq_groups_fops = {
2349 + .owner = THIS_MODULE,
2350 + .open = ext3_mb_seq_groups_open,
2352 + .llseek = seq_lseek,
2353 + .release = seq_release,
2356 +static void ext3_mb_history_release(struct super_block *sb)
2358 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2360 + remove_proc_entry("mb_groups", sbi->s_mb_proc);
2361 + remove_proc_entry("mb_history", sbi->s_mb_proc);
2363 + if (sbi->s_mb_history)
2364 + kfree(sbi->s_mb_history);
2367 +static void ext3_mb_history_init(struct super_block *sb)
2369 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2372 + if (sbi->s_mb_proc != NULL) {
2373 + struct proc_dir_entry *p;
2374 + p = create_proc_entry("mb_history", S_IRUGO, sbi->s_mb_proc);
2376 + p->proc_fops = &ext3_mb_seq_history_fops;
2379 + p = create_proc_entry("mb_groups", S_IRUGO, sbi->s_mb_proc);
2381 + p->proc_fops = &ext3_mb_seq_groups_fops;
2386 + sbi->s_mb_history_max = 1000;
2387 + sbi->s_mb_history_cur = 0;
2388 + spin_lock_init(&sbi->s_mb_history_lock);
2389 + i = sbi->s_mb_history_max * sizeof(struct ext3_mb_history);
2390 + sbi->s_mb_history = kmalloc(i, GFP_KERNEL);
2391 + if (likely(sbi->s_mb_history != NULL))
2392 + memset(sbi->s_mb_history, 0, i);
2393 + /* if we can't allocate history, then we simple won't use it */
2397 +ext3_mb_store_history(struct ext3_allocation_context *ac)
2399 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
2400 + struct ext3_mb_history h;
2402 + if (unlikely(sbi->s_mb_history == NULL))
2405 + if (!(ac->ac_op & sbi->s_mb_history_filter))
2409 + h.pid = current->pid;
2410 + h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2411 + h.orig = ac->ac_o_ex;
2412 + h.result = ac->ac_b_ex;
2413 + h.flags = ac->ac_flags;
2415 + if (ac->ac_op == EXT3_MB_HISTORY_ALLOC) {
2416 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2417 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2419 + h.goal = ac->ac_g_ex;
2420 + h.result = ac->ac_f_ex;
2423 + spin_lock(&sbi->s_mb_history_lock);
2424 + memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2425 + if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2426 + sbi->s_mb_history_cur = 0;
2427 + spin_unlock(&sbi->s_mb_history_lock);
2431 +#define ext3_mb_history_release(sb)
2432 +#define ext3_mb_history_init(sb)
2435 +int ext3_mb_init_backend(struct super_block *sb)
2437 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2438 + int i, j, len, metalen;
2439 + int num_meta_group_infos =
2440 + (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2441 + EXT3_DESC_PER_BLOCK_BITS(sb);
2442 + struct ext3_group_info **meta_group_info;
2444 + /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2445 + * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2446 + * So a two level scheme suffices for now. */
2447 + sbi->s_group_info = kmalloc(sizeof(*sbi->s_group_info) *
2448 + num_meta_group_infos, GFP_KERNEL);
2449 + if (sbi->s_group_info == NULL) {
2450 + printk(KERN_ERR "EXT3-fs: can't allocate buddy meta group\n");
2453 + sbi->s_buddy_cache = new_inode(sb);
2454 + if (sbi->s_buddy_cache == NULL) {
2455 + printk(KERN_ERR "EXT3-fs: can't get new inode\n");
2458 + EXT3_I(sbi->s_buddy_cache)->i_disksize = 0;
2460 + metalen = sizeof(*meta_group_info) << EXT3_DESC_PER_BLOCK_BITS(sb);
2461 + for (i = 0; i < num_meta_group_infos; i++) {
2462 + if ((i + 1) == num_meta_group_infos)
2463 + metalen = sizeof(*meta_group_info) *
2464 + (sbi->s_groups_count -
2465 + (i << EXT3_DESC_PER_BLOCK_BITS(sb)));
2466 + meta_group_info = kmalloc(metalen, GFP_KERNEL);
2467 + if (meta_group_info == NULL) {
2468 + printk(KERN_ERR "EXT3-fs: can't allocate mem for a "
2470 + goto err_freemeta;
2472 + sbi->s_group_info[i] = meta_group_info;
2476 + * calculate needed size. if change bb_counters size,
2477 + * don't forget about ext3_mb_generate_buddy()
2479 + len = sizeof(struct ext3_group_info);
2480 + len += sizeof(unsigned short) * (sb->s_blocksize_bits + 2);
2481 + for (i = 0; i < sbi->s_groups_count; i++) {
2482 + struct ext3_group_desc * desc;
2485 + sbi->s_group_info[i >> EXT3_DESC_PER_BLOCK_BITS(sb)];
2486 + j = i & (EXT3_DESC_PER_BLOCK(sb) - 1);
2488 + meta_group_info[j] = kmalloc(len, GFP_KERNEL);
2489 + if (meta_group_info[j] == NULL) {
2490 + printk(KERN_ERR "EXT3-fs: can't allocate buddy mem\n");
2492 + goto err_freebuddy;
2494 + desc = ext3_get_group_desc(sb, i, NULL);
2495 + if (desc == NULL) {
2496 + printk(KERN_ERR"EXT3-fs: can't read descriptor %u\n",i);
2497 + goto err_freebuddy;
2499 + memset(meta_group_info[j], 0, len);
2500 + set_bit(EXT3_GROUP_INFO_NEED_INIT_BIT,
2501 + &meta_group_info[j]->bb_state);
2503 + /* initialize bb_free to be able to skip
2504 + * empty groups without initialization */
2505 + meta_group_info[j]->bb_free =
2506 + le16_to_cpu(desc->bg_free_blocks_count);
2508 + INIT_LIST_HEAD(&meta_group_info[j]->bb_prealloc_list);
2510 +#ifdef DOUBLE_CHECK
2512 + struct buffer_head *bh;
2513 + meta_group_info[j]->bb_bitmap =
2514 + kmalloc(sb->s_blocksize, GFP_KERNEL);
2515 + BUG_ON(meta_group_info[j]->bb_bitmap == NULL);
2516 + bh = read_block_bitmap(sb, i);
2517 + BUG_ON(bh == NULL);
2518 + memcpy(meta_group_info[j]->bb_bitmap, bh->b_data,
2530 + kfree(EXT3_GROUP_INFO(sb, i));
2533 + i = num_meta_group_infos;
2536 + kfree(sbi->s_group_info[i]);
2537 + iput(sbi->s_buddy_cache);
2539 + kfree(sbi->s_group_info);
2543 +int ext3_mb_init(struct super_block *sb, int needs_recovery)
2545 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2546 + unsigned i, offset, max;
2548 + if (!test_opt(sb, MBALLOC))
2551 + i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2553 + sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2554 + if (sbi->s_mb_offsets == NULL) {
2555 + clear_opt(sbi->s_mount_opt, MBALLOC);
2558 + sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2559 + if (sbi->s_mb_maxs == NULL) {
2560 + clear_opt(sbi->s_mount_opt, MBALLOC);
2561 + kfree(sbi->s_mb_maxs);
2565 + /* order 0 is regular bitmap */
2566 + sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2567 + sbi->s_mb_offsets[0] = 0;
2571 + max = sb->s_blocksize << 2;
2573 + sbi->s_mb_offsets[i] = offset;
2574 + sbi->s_mb_maxs[i] = max;
2575 + offset += 1 << (sb->s_blocksize_bits - i);
2578 + } while (i <= sb->s_blocksize_bits + 1);
2580 + /* init file for buddy data */
2581 + if ((i = ext3_mb_init_backend(sb))) {
2582 + clear_opt(sbi->s_mount_opt, MBALLOC);
2583 + kfree(sbi->s_mb_offsets);
2584 + kfree(sbi->s_mb_maxs);
2588 + spin_lock_init(&sbi->s_md_lock);
2589 + INIT_LIST_HEAD(&sbi->s_active_transaction);
2590 + INIT_LIST_HEAD(&sbi->s_closed_transaction);
2591 + INIT_LIST_HEAD(&sbi->s_committed_transaction);
2592 + spin_lock_init(&sbi->s_bal_lock);
2594 + sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2595 + sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2596 + sbi->s_mb_max_groups_to_scan = MB_DEFAULT_MAX_GROUPS_TO_SCAN;
2597 + sbi->s_mb_stats = MB_DEFAULT_STATS;
2598 + sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2599 + sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2600 + sbi->s_mb_history_filter = EXT3_MB_HISTORY_DEFAULT;
2602 + i = sizeof(struct ext3_locality_group) * NR_CPUS;
2603 + sbi->s_locality_groups = kmalloc(i, GFP_NOFS);
2604 + if (sbi->s_locality_groups == NULL) {
2605 + clear_opt(sbi->s_mount_opt, MBALLOC);
2606 + kfree(sbi->s_mb_offsets);
2607 + kfree(sbi->s_mb_maxs);
2610 + for (i = 0; i < NR_CPUS; i++) {
2611 + struct ext3_locality_group *lg;
2612 + lg = &sbi->s_locality_groups[i];
2613 + sema_init(&lg->lg_sem, 1);
2614 + INIT_LIST_HEAD(&lg->lg_prealloc_list);
2615 + spin_lock_init(&lg->lg_prealloc_lock);
2618 + ext3_mb_init_per_dev_proc(sb);
2619 + ext3_mb_history_init(sb);
2621 + printk("EXT3-fs: mballoc enabled\n");
2625 +void ext3_mb_cleanup_pa(struct ext3_group_info *grp)
2627 + struct ext3_prealloc_space *pa;
2628 + struct list_head *cur, *tmp;
2631 + list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2632 + pa = list_entry(cur, struct ext3_prealloc_space, pa_group_list);
2633 + list_del_rcu(&pa->pa_group_list);
2638 + mb_debug("mballoc: %u PAs left\n", count);
2642 +int ext3_mb_release(struct super_block *sb)
2644 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2645 + int i, num_meta_group_infos;
2647 + if (!test_opt(sb, MBALLOC))
2650 + /* release freed, non-committed blocks */
2651 + spin_lock(&sbi->s_md_lock);
2652 + list_splice_init(&sbi->s_closed_transaction,
2653 + &sbi->s_committed_transaction);
2654 + list_splice_init(&sbi->s_active_transaction,
2655 + &sbi->s_committed_transaction);
2656 + spin_unlock(&sbi->s_md_lock);
2657 + ext3_mb_free_committed_blocks(sb);
2659 + if (sbi->s_group_info) {
2660 + for (i = 0; i < sbi->s_groups_count; i++) {
2661 +#ifdef DOUBLE_CHECK
2662 + if (EXT3_GROUP_INFO(sb, i)->bb_bitmap)
2663 + kfree(EXT3_GROUP_INFO(sb, i)->bb_bitmap);
2665 + ext3_mb_cleanup_pa(EXT3_GROUP_INFO(sb, i));
2666 + kfree(EXT3_GROUP_INFO(sb, i));
2668 + num_meta_group_infos = (sbi->s_groups_count +
2669 + EXT3_DESC_PER_BLOCK(sb) - 1) >>
2670 + EXT3_DESC_PER_BLOCK_BITS(sb);
2671 + for (i = 0; i < num_meta_group_infos; i++)
2672 + kfree(sbi->s_group_info[i]);
2673 + kfree(sbi->s_group_info);
2675 + if (sbi->s_mb_offsets)
2676 + kfree(sbi->s_mb_offsets);
2677 + if (sbi->s_mb_maxs)
2678 + kfree(sbi->s_mb_maxs);
2679 + if (sbi->s_buddy_cache)
2680 + iput(sbi->s_buddy_cache);
2681 + if (sbi->s_mb_stats) {
2682 + printk("EXT3-fs: mballoc: %u blocks %u reqs (%u success)\n",
2683 + atomic_read(&sbi->s_bal_allocated),
2684 + atomic_read(&sbi->s_bal_reqs),
2685 + atomic_read(&sbi->s_bal_success));
2686 + printk("EXT3-fs: mballoc: %u extents scanned, %u goal hits, "
2687 + "%u 2^N hits, %u breaks, %u lost\n",
2688 + atomic_read(&sbi->s_bal_ex_scanned),
2689 + atomic_read(&sbi->s_bal_goals),
2690 + atomic_read(&sbi->s_bal_2orders),
2691 + atomic_read(&sbi->s_bal_breaks),
2692 + atomic_read(&sbi->s_mb_lost_chunks));
2693 + printk("EXT3-fs: mballoc: %lu generated and it took %Lu\n",
2694 + sbi->s_mb_buddies_generated++,
2695 + sbi->s_mb_generation_time);
2696 + printk("EXT3-fs: mballoc: %u preallocated, %u discarded\n",
2697 + atomic_read(&sbi->s_mb_preallocated),
2698 + atomic_read(&sbi->s_mb_discarded));
2701 + if (sbi->s_locality_groups)
2702 + kfree(sbi->s_locality_groups);
2704 + ext3_mb_history_release(sb);
2705 + ext3_mb_destroy_per_dev_proc(sb);
2710 +void ext3_mb_free_committed_blocks(struct super_block *sb)
2712 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2713 + int err, i, count = 0, count2 = 0;
2714 + struct ext3_free_metadata *md;
2715 + struct ext3_buddy e3b;
2717 + if (list_empty(&sbi->s_committed_transaction))
2720 + /* there is committed blocks to be freed yet */
2722 + /* get next array of blocks */
2724 + spin_lock(&sbi->s_md_lock);
2725 + if (!list_empty(&sbi->s_committed_transaction)) {
2726 + md = list_entry(sbi->s_committed_transaction.next,
2727 + struct ext3_free_metadata, list);
2728 + list_del(&md->list);
2730 + spin_unlock(&sbi->s_md_lock);
2735 + mb_debug("gonna free %u blocks in group %u (0x%p):",
2736 + md->num, md->group, md);
2738 + err = ext3_mb_load_buddy(sb, md->group, &e3b);
2739 + /* we expect to find existing buddy because it's pinned */
2742 + /* there are blocks to put in buddy to make them really free */
2745 + ext3_lock_group(sb, md->group);
2746 + for (i = 0; i < md->num; i++) {
2747 + mb_debug(" %u", md->blocks[i]);
2748 + err = mb_free_blocks(NULL, &e3b, md->blocks[i], 1);
2752 + ext3_unlock_group(sb, md->group);
2754 + /* balance refcounts from ext3_mb_free_metadata() */
2755 + page_cache_release(e3b.bd_buddy_page);
2756 + page_cache_release(e3b.bd_bitmap_page);
2759 + ext3_mb_release_desc(&e3b);
2763 + mb_debug("freed %u blocks in %u structures\n", count, count2);
2766 +#define EXT3_ROOT "ext3"
2767 +#define EXT3_MB_STATS_NAME "stats"
2768 +#define EXT3_MB_MAX_TO_SCAN_NAME "max_to_scan"
2769 +#define EXT3_MB_MIN_TO_SCAN_NAME "min_to_scan"
2770 +#define EXT3_MB_ORDER2_REQ "order2_req"
2771 +#define EXT3_MB_STREAM_REQ "stream_req"
2773 +static int ext3_mb_stats_read(char *page, char **start, off_t off,
2774 + int count, int *eof, void *data)
2776 + struct ext3_sb_info *sbi = data;
2783 + len = sprintf(page, "%ld\n", sbi->s_mb_stats);
2788 +static int ext3_mb_stats_write(struct file *file, const char *buffer,
2789 + unsigned long count, void *data)
2791 + struct ext3_sb_info *sbi = data;
2794 + if (count >= sizeof(str)) {
2795 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2796 + EXT3_MB_STATS_NAME, (int)sizeof(str));
2797 + return -EOVERFLOW;
2800 + if (copy_from_user(str, buffer, count))
2803 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2804 + sbi->s_mb_stats = (simple_strtol(str, NULL, 0) != 0);
2808 +static int ext3_mb_max_to_scan_read(char *page, char **start, off_t off,
2809 + int count, int *eof, void *data)
2811 + struct ext3_sb_info *sbi = data;
2818 + len = sprintf(page, "%ld\n", sbi->s_mb_max_to_scan);
2823 +static int ext3_mb_max_to_scan_write(struct file *file, const char *buffer,
2824 + unsigned long count, void *data)
2826 + struct ext3_sb_info *sbi = data;
2830 + if (count >= sizeof(str)) {
2831 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2832 + EXT3_MB_MAX_TO_SCAN_NAME, (int)sizeof(str));
2833 + return -EOVERFLOW;
2836 + if (copy_from_user(str, buffer, count))
2839 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2840 + value = simple_strtol(str, NULL, 0);
2844 + sbi->s_mb_max_to_scan = value;
2849 +static int ext3_mb_min_to_scan_read(char *page, char **start, off_t off,
2850 + int count, int *eof, void *data)
2852 + struct ext3_sb_info *sbi = data;
2859 + len = sprintf(page, "%ld\n", sbi->s_mb_min_to_scan);
2864 +static int ext3_mb_order2_req_write(struct file *file, const char *buffer,
2865 + unsigned long count, void *data)
2867 + struct ext3_sb_info *sbi = data;
2871 + if (count >= sizeof(str)) {
2872 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2873 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2874 + return -EOVERFLOW;
2877 + if (copy_from_user(str, buffer, count))
2880 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2881 + value = simple_strtol(str, NULL, 0);
2885 + sbi->s_mb_order2_reqs = value;
2890 +static int ext3_mb_order2_req_read(char *page, char **start, off_t off,
2891 + int count, int *eof, void *data)
2893 + struct ext3_sb_info *sbi = data;
2900 + len = sprintf(page, "%ld\n", sbi->s_mb_order2_reqs);
2905 +static int ext3_mb_min_to_scan_write(struct file *file, const char *buffer,
2906 + unsigned long count, void *data)
2908 + struct ext3_sb_info *sbi = data;
2912 + if (count >= sizeof(str)) {
2913 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2914 + EXT3_MB_MIN_TO_SCAN_NAME, (int)sizeof(str));
2915 + return -EOVERFLOW;
2918 + if (copy_from_user(str, buffer, count))
2921 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2922 + value = simple_strtol(str, NULL, 0);
2926 + sbi->s_mb_min_to_scan = value;
2931 +static int ext3_mb_stream_req_read(char *page, char **start, off_t off,
2932 + int count, int *eof, void *data)
2934 + struct ext3_sb_info *sbi = data;
2941 + len = sprintf(page, "%ld\n", sbi->s_mb_stream_request);
2946 +static int ext3_mb_stream_req_write(struct file *file, const char *buffer,
2947 + unsigned long count, void *data)
2949 + struct ext3_sb_info *sbi = data;
2953 + if (count >= sizeof(str)) {
2954 + printk(KERN_ERR "EXT3-fs: %s string too long, max %u bytes\n",
2955 + EXT3_MB_STREAM_REQ, (int)sizeof(str));
2956 + return -EOVERFLOW;
2959 + if (copy_from_user(str, buffer, count))
2962 + /* Only set to 0 or 1 respectively; zero->0; non-zero->1 */
2963 + value = simple_strtol(str, NULL, 0);
2967 + sbi->s_mb_stream_request = value;
2972 +int ext3_mb_init_per_dev_proc(struct super_block *sb)
2974 + mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
2975 + struct ext3_sb_info *sbi = EXT3_SB(sb);
2976 + struct proc_dir_entry *proc;
2977 + char devname[64], *name;
2979 + snprintf(devname, sizeof(devname) - 1, "%s",
2980 + bdevname(sb->s_bdev, devname));
2981 + sbi->s_mb_proc = proc_mkdir(devname, proc_root_ext3);
2983 + name = EXT3_MB_STATS_NAME;
2984 + proc = create_proc_entry(name, mode, sbi->s_mb_proc);
2988 + proc->read_proc = ext3_mb_stats_read;
2989 + proc->write_proc = ext3_mb_stats_write;
2991 + name = EXT3_MB_MAX_TO_SCAN_NAME;
2992 + proc = create_proc_entry(name, mode, sbi->s_mb_proc);
2996 + proc->read_proc = ext3_mb_max_to_scan_read;
2997 + proc->write_proc = ext3_mb_max_to_scan_write;
2999 + name = EXT3_MB_MIN_TO_SCAN_NAME;
3000 + proc = create_proc_entry(name, mode, sbi->s_mb_proc);
3004 + proc->read_proc = ext3_mb_min_to_scan_read;
3005 + proc->write_proc = ext3_mb_min_to_scan_write;
3007 + name = EXT3_MB_ORDER2_REQ;
3008 + proc = create_proc_entry(name, mode, sbi->s_mb_proc);
3012 + proc->read_proc = ext3_mb_order2_req_read;
3013 + proc->write_proc = ext3_mb_order2_req_write;
3015 + name = EXT3_MB_STREAM_REQ;
3016 + proc = create_proc_entry(name, mode, sbi->s_mb_proc);
3020 + proc->read_proc = ext3_mb_stream_req_read;
3021 + proc->write_proc = ext3_mb_stream_req_write;
3026 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", name);
3027 + remove_proc_entry(EXT3_MB_STREAM_REQ, sbi->s_mb_proc);
3028 + remove_proc_entry(EXT3_MB_ORDER2_REQ, sbi->s_mb_proc);
3029 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
3030 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
3031 + remove_proc_entry(EXT3_MB_STATS_NAME, sbi->s_mb_proc);
3032 + remove_proc_entry(devname, proc_root_ext3);
3033 + sbi->s_mb_proc = NULL;
3038 +int ext3_mb_destroy_per_dev_proc(struct super_block *sb)
3040 + struct ext3_sb_info *sbi = EXT3_SB(sb);
3043 + if (sbi->s_mb_proc == NULL)
3046 + snprintf(devname, sizeof(devname) - 1, "%s",
3047 + bdevname(sb->s_bdev, devname));
3048 + remove_proc_entry(EXT3_MB_STREAM_REQ, sbi->s_mb_proc);
3049 + remove_proc_entry(EXT3_MB_ORDER2_REQ, sbi->s_mb_proc);
3050 + remove_proc_entry(EXT3_MB_MIN_TO_SCAN_NAME, sbi->s_mb_proc);
3051 + remove_proc_entry(EXT3_MB_MAX_TO_SCAN_NAME, sbi->s_mb_proc);
3052 + remove_proc_entry(EXT3_MB_STATS_NAME, sbi->s_mb_proc);
3053 + remove_proc_entry(devname, proc_root_ext3);
3058 +int __init init_ext3_proc(void)
3060 + ext3_pspace_cachep =
3061 + kmem_cache_create("ext3_prealloc_space",
3062 + sizeof(struct ext3_prealloc_space),
3063 + 0, SLAB_RECLAIM_ACCOUNT, NULL, NULL);
3064 + if (ext3_pspace_cachep == NULL)
3067 + proc_root_ext3 = proc_mkdir(EXT3_ROOT, proc_root_fs);
3068 + if (proc_root_ext3 == NULL)
3069 + printk(KERN_ERR "EXT3-fs: Unable to create %s\n", EXT3_ROOT);
3074 +void exit_ext3_proc(void)
3076 + /* XXX: synchronize_rcu(); */
3077 + kmem_cache_destroy(ext3_pspace_cachep);
3078 + remove_proc_entry(EXT3_ROOT, proc_root_fs);
3083 + * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
3084 + * Returns 0 if success or error code
3086 +int ext3_mb_mark_diskspace_used(struct ext3_allocation_context *ac, handle_t *handle)
3088 + struct buffer_head *bitmap_bh = NULL;
3089 + struct ext3_super_block *es;
3090 + struct ext3_group_desc *gdp;
3091 + struct buffer_head *gdp_bh;
3092 + struct ext3_sb_info *sbi;
3093 + struct super_block *sb;
3097 + BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3098 + BUG_ON(ac->ac_b_ex.fe_len <= 0);
3101 + sbi = EXT3_SB(sb);
3104 + ext3_debug("using block group %d(%d)\n", ac->ac_b_group.group,
3105 + gdp->bg_free_blocks_count);
3108 + bitmap_bh = read_block_bitmap(sb, ac->ac_b_ex.fe_group);
3112 + err = ext3_journal_get_write_access(handle, bitmap_bh);
3117 + gdp = ext3_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
3121 + err = ext3_journal_get_write_access(handle, gdp_bh);
3125 + block = ac->ac_b_ex.fe_group * EXT3_BLOCKS_PER_GROUP(sb)
3126 + + ac->ac_b_ex.fe_start
3127 + + le32_to_cpu(es->s_first_data_block);
3129 + if (block == le32_to_cpu(gdp->bg_block_bitmap) ||
3130 + block == le32_to_cpu(gdp->bg_inode_bitmap) ||
3131 + in_range(block, le32_to_cpu(gdp->bg_inode_table),
3132 + EXT3_SB(sb)->s_itb_per_group))
3133 + ext3_error(sb, __FUNCTION__,
3134 + "Allocating block in system zone - block = %lu",
3135 + (unsigned long) block);
3136 +#ifdef AGGRESSIVE_CHECK
3139 + for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3140 + BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3141 + bitmap_bh->b_data));
3145 + mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
3147 + spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3148 + gdp->bg_free_blocks_count =
3149 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)
3150 + - ac->ac_b_ex.fe_len);
3151 + spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3152 + percpu_counter_mod(&sbi->s_freeblocks_counter, - ac->ac_b_ex.fe_len);
3154 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
3157 + err = ext3_journal_dirty_metadata(handle, gdp_bh);
3161 + brelse(bitmap_bh);
3166 + * here we normalize request for locality group
3167 + * XXX: should we try to preallocate more than the group has now?
3169 +void ext3_mb_normalize_group_request(struct ext3_allocation_context *ac)
3171 + struct super_block *sb = ac->ac_sb;
3172 + struct ext3_locality_group *lg = ac->ac_lg;
3174 + BUG_ON(lg == NULL);
3175 + if (EXT3_SB(sb)->s_stripe)
3176 + ac->ac_g_ex.fe_len = EXT3_SB(sb)->s_stripe;
3178 + ac->ac_g_ex.fe_len = (1024 * 1024) >> sb->s_blocksize_bits;
3180 + mb_debug("#%u: goal %u blocks for locality group\n",
3181 + current->pid, ac->ac_g_ex.fe_len);
3185 + * Normalization means making request better in terms of
3186 + * size and alignment
3188 +void ext3_mb_normalize_request(struct ext3_allocation_context *ac,
3189 + struct ext3_allocation_request *ar)
3191 + struct ext3_inode_info *ei = EXT3_I(ac->ac_inode);
3192 + loff_t start, end, size, orig_size, orig_start;
3193 + struct list_head *cur;
3196 + /* do normalize only data requests, metadata requests
3197 + do not need preallocation */
3198 + if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
3201 + /* sometime caller may want exact blocks */
3202 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
3205 + /* caller may indicate that preallocation isn't
3206 + * required (it's a tail, for example) */
3207 + if (ac->ac_flags & EXT3_MB_HINT_NOPREALLOC)
3210 + if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
3211 + return ext3_mb_normalize_group_request(ac);
3213 + bsbits = ac->ac_sb->s_blocksize_bits;
3215 + /* first, let's learn actual file size
3216 + * given current request is allocated */
3217 + size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3218 + size = size << bsbits;
3219 + if (size < i_size_read(ac->ac_inode))
3220 + size = i_size_read(ac->ac_inode);
3222 + /* first, try to predict filesize */
3223 + /* XXX: should this table be tunable? */
3225 + if (size <= 16 * 1024) {
3227 + } else if (size <= 32 * 1024) {
3229 + } else if (size <= 64 * 1024) {
3231 + } else if (size <= 128 * 1024) {
3232 + size = 128 * 1024;
3233 + } else if (size <= 256 * 1024) {
3234 + size = 256 * 1024;
3235 + } else if (size <= 512 * 1024) {
3236 + size = 512 * 1024;
3237 + } else if (size <= 1024 * 1024) {
3238 + size = 1024 * 1024;
3239 + } else if (size < 4 * 1024 * 1024) {
3240 + start = ac->ac_o_ex.fe_logical << bsbits;
3241 + start = (start / (1024 * 1024)) * (1024 * 1024);
3242 + size = 1024 * 1024;
3243 + } else if (size < 8 * 1024 * 1024) {
3244 + start = ac->ac_o_ex.fe_logical << bsbits;
3245 + start = (start / (4 * (1024 * 1024))) * 4 * (1024 * 1024);
3246 + size = 4 * 1024 * 1024;
3247 + } else if (ac->ac_o_ex.fe_len < ((8 << 20) >> bsbits)) {
3248 + start = ac->ac_o_ex.fe_logical;
3249 + start = start << bsbits;
3250 + start = (start / (8 * (1024 * 1024))) * 8 * (1024 * 1024);
3251 + size = 8 * 1024 * 1024;
3253 + start = ac->ac_o_ex.fe_logical;
3254 + start = start << bsbits;
3255 + size = ac->ac_o_ex.fe_len << bsbits;
3257 + orig_size = size = size >> bsbits;
3258 + orig_start = start = start >> bsbits;
3260 + /* don't cover already allocated blocks in selected range */
3261 + if (ar->pleft && start <= ar->lleft) {
3262 + size -= ar->lleft + 1 - start;
3263 + start = ar->lleft + 1;
3265 + if (ar->pright && start + size - 1 >= ar->lright)
3266 + size -= start + size - ar->lright;
3268 + end = start + size;
3270 + /* check we don't cross already preallocated blocks */
3272 + list_for_each_rcu(cur, &ei->i_prealloc_list) {
3273 + struct ext3_prealloc_space *pa;
3274 + unsigned long pa_end;
3276 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3278 + if (pa->pa_deleted)
3280 + spin_lock(&pa->pa_lock);
3281 + if (pa->pa_deleted) {
3282 + spin_unlock(&pa->pa_lock);
3286 + pa_end = pa->pa_lstart + pa->pa_len;
3288 + /* PA must not overlap original request */
3289 + BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3290 + ac->ac_o_ex.fe_logical < pa->pa_lstart));
3292 + /* skip PA normalized request doesn't overlap with */
3293 + if (pa->pa_lstart >= end) {
3294 + spin_unlock(&pa->pa_lock);
3297 + if (pa_end <= start) {
3298 + spin_unlock(&pa->pa_lock);
3301 + BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3303 + if (pa_end <= ac->ac_o_ex.fe_logical) {
3304 + BUG_ON(pa_end < start);
3308 + if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3309 + BUG_ON(pa->pa_lstart > end);
3310 + end = pa->pa_lstart;
3312 + spin_unlock(&pa->pa_lock);
3314 + rcu_read_unlock();
3315 + size = end - start;
3317 + /* XXX: extra loop to check we really don't overlap preallocations */
3319 + list_for_each_rcu(cur, &ei->i_prealloc_list) {
3320 + struct ext3_prealloc_space *pa;
3321 + unsigned long pa_end;
3322 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3323 + spin_lock(&pa->pa_lock);
3324 + if (pa->pa_deleted == 0) {
3325 + pa_end = pa->pa_lstart + pa->pa_len;
3326 + BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3328 + spin_unlock(&pa->pa_lock);
3330 + rcu_read_unlock();
3332 + if (start + size <= ac->ac_o_ex.fe_logical &&
3333 + start > ac->ac_o_ex.fe_logical) {
3334 + printk("start %lu, size %lu, fe_logical %lu\n",
3335 + (unsigned long) start, (unsigned long) size,
3336 + (unsigned long) ac->ac_o_ex.fe_logical);
3338 + BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3339 + start > ac->ac_o_ex.fe_logical);
3341 + /* now prepare goal request */
3342 + BUG_ON(size <= 0 || size >= EXT3_BLOCKS_PER_GROUP(ac->ac_sb));
3343 + if (size < ac->ac_o_ex.fe_len) {
3344 + /* XXX: don't normalize tails? */
3347 + /* XXX: is it better to align blocks WRT to logical placement
3348 + * or satisfy big request as is */
3349 + ac->ac_g_ex.fe_logical = start;
3350 + ac->ac_g_ex.fe_len = size;
3352 + mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3353 + (unsigned) orig_size, (unsigned) start);
3356 +void ext3_mb_collect_stats(struct ext3_allocation_context *ac)
3358 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
3360 + if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3361 + atomic_inc(&sbi->s_bal_reqs);
3362 + atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3363 + if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3364 + atomic_inc(&sbi->s_bal_success);
3365 + atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3366 + if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3367 + ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3368 + atomic_inc(&sbi->s_bal_goals);
3369 + if (ac->ac_found > sbi->s_mb_max_to_scan)
3370 + atomic_inc(&sbi->s_bal_breaks);
3373 + ext3_mb_store_history(ac);
3377 + * use blocks preallocated to inode
3379 +void ext3_mb_use_inode_pa(struct ext3_allocation_context *ac,
3380 + struct ext3_prealloc_space *pa)
3382 + unsigned long start, len;
3384 + /* found preallocated blocks, use them */
3385 + start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3386 + len = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3387 + len = len - start;
3388 + ext3_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3389 + &ac->ac_b_ex.fe_start);
3390 + ac->ac_b_ex.fe_len = len;
3391 + ac->ac_status = AC_STATUS_FOUND;
3394 + BUG_ON(start < pa->pa_pstart);
3395 + BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3396 + BUG_ON(pa->pa_free < len);
3397 + pa->pa_free -= len;
3399 + mb_debug("use %lu/%lu from inode pa %p\n", start, len, pa);
3403 + * use blocks preallocated to locality group
3405 +void ext3_mb_use_group_pa(struct ext3_allocation_context *ac,
3406 + struct ext3_prealloc_space *pa)
3408 + unsigned len = ac->ac_o_ex.fe_len;
3410 + ext3_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3411 + &ac->ac_b_ex.fe_group,
3412 + &ac->ac_b_ex.fe_start);
3413 + ac->ac_b_ex.fe_len = len;
3414 + ac->ac_status = AC_STATUS_FOUND;
3417 + /* we don't correct pa_pstart or pa_plen here to avoid
3418 + * possible race when tte group is being loaded concurrently
3419 + * instead we correct pa later, after blocks are marked
3420 + * in on-disk bitmap -- see ext3_mb_release_context() */
3421 + mb_debug("use %lu/%lu from group pa %p\n", pa->pa_lstart-len, len, pa);
3425 + * search goal blocks in preallocated space
3427 +int ext3_mb_use_preallocated(struct ext3_allocation_context *ac)
3429 + struct ext3_inode_info *ei = EXT3_I(ac->ac_inode);
3430 + struct ext3_locality_group *lg;
3431 + struct ext3_prealloc_space *pa;
3432 + struct list_head *cur;
3434 + /* only data can be preallocated */
3435 + if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
3438 + /* first, try per-file preallocation */
3440 + list_for_each_rcu(cur, &ei->i_prealloc_list) {
3441 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3443 + /* all fields in this condition don't change,
3444 + * so we can skip locking for them */
3445 + if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3446 + ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3449 + /* found preallocated blocks, use them */
3450 + spin_lock(&pa->pa_lock);
3451 + if (pa->pa_deleted == 0 && pa->pa_free) {
3452 + atomic_inc(&pa->pa_count);
3453 + ext3_mb_use_inode_pa(ac, pa);
3454 + spin_unlock(&pa->pa_lock);
3455 + ac->ac_criteria = 10;
3456 + rcu_read_unlock();
3459 + spin_unlock(&pa->pa_lock);
3461 + rcu_read_unlock();
3463 + /* can we use group allocation? */
3464 + if (!(ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC))
3467 + /* inode may have no locality group for some reason */
3473 + list_for_each_rcu(cur, &lg->lg_prealloc_list) {
3474 + pa = list_entry(cur, struct ext3_prealloc_space, pa_inode_list);
3475 + spin_lock(&pa->pa_lock);
3476 + if (pa->pa_deleted == 0 && pa->pa_free >= ac->ac_o_ex.fe_len) {
3477 + atomic_inc(&pa->pa_count);
3478 + ext3_mb_use_group_pa(ac, pa);
3479 + spin_unlock(&pa->pa_lock);
3480 + ac->ac_criteria = 20;
3481 + rcu_read_unlock();
3484 + spin_unlock(&pa->pa_lock);
3486 + rcu_read_unlock();
3492 + * the function goes through all preallocation in this group and marks them
3493 + * used in in-core bitmap. buddy must be generated from this bitmap
3495 +void ext3_mb_generate_from_pa(struct super_block *sb, void *bitmap, int group)
3497 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
3498 + struct ext3_prealloc_space *pa;
3499 + struct list_head *cur;
3500 + unsigned long groupnr;
3501 + unsigned long start;
3502 + int preallocated = 0, count = 0, len;
3504 + /* all form of preallocation discards first load group,
3505 + * so the only competing code is preallocation use.
3506 + * we don't need any locking here
3507 + * notice we do NOT ignore preallocations with pa_deleted
3508 + * otherwise we could leave used blocks available for
3509 + * allocation in buddy when concurrent ext3_mb_put_pa()
3510 + * is dropping preallocation
3512 + list_for_each_rcu(cur, &grp->bb_prealloc_list) {
3513 + pa = list_entry(cur, struct ext3_prealloc_space, pa_group_list);
3514 + spin_lock(&pa->pa_lock);
3515 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &start);
3517 + spin_unlock(&pa->pa_lock);
3518 + BUG_ON(groupnr != group);
3519 + mb_set_bits(bitmap, start, len);
3520 + preallocated += len;
3523 + mb_debug("prellocated %u for group %u\n", preallocated, group);
3526 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,5)
3527 +static void ext3_mb_pa_callback(struct rcu_head *head)
3529 + struct ext3_prealloc_space *pa;
3530 + pa = container_of(head, struct ext3_prealloc_space, u.pa_rcu);
3531 + kmem_cache_free(ext3_pspace_cachep, pa);
3533 +#define mb_call_rcu(__pa) call_rcu(&(__pa)->u.pa_rcu, ext3_mb_pa_callback)
3535 +static void ext3_mb_pa_callback(void *pa)
3537 + kmem_cache_free(ext3_pspace_cachep, pa);
3539 +#define mb_call_rcu(__pa) call_rcu(&(__pa)->u.pa_rcu, ext3_mb_pa_callback, pa)
3543 + * drops a reference to preallocated space descriptor
3544 + * if this was the last reference and the space is consumed
3546 +void ext3_mb_put_pa(struct ext3_allocation_context *ac,
3547 + struct super_block *sb, struct ext3_prealloc_space *pa)
3549 + unsigned long grp;
3551 + if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3554 + /* in this short window concurrent discard can set pa_deleted */
3555 + spin_lock(&pa->pa_lock);
3556 + if (pa->pa_deleted == 0) {
3557 + spin_unlock(&pa->pa_lock);
3561 + pa->pa_deleted = 1;
3562 + spin_unlock(&pa->pa_lock);
3564 + /* -1 is to protect from crossing allocation group */
3565 + ext3_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
3570 + * P1 (buddy init) P2 (regular allocation)
3571 + * find block B in PA
3572 + * copy on-disk bitmap to buddy
3573 + * mark B in on-disk bitmap
3574 + * drop PA from group
3575 + * mark all PAs in buddy
3577 + * thus, P1 initializes buddy with B available. to prevent this
3578 + * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3579 + * against that pair
3581 + ext3_lock_group(sb, grp);
3582 + list_del_rcu(&pa->pa_group_list);
3583 + ext3_unlock_group(sb, grp);
3585 + spin_lock(pa->pa_obj_lock);
3586 + list_del_rcu(&pa->pa_inode_list);
3587 + spin_unlock(pa->pa_obj_lock);
3593 + * creates new preallocated space for given inode
3595 +int ext3_mb_new_inode_pa(struct ext3_allocation_context *ac)
3597 + struct super_block *sb = ac->ac_sb;
3598 + struct ext3_prealloc_space *pa;
3599 + struct ext3_group_info *grp;
3600 + struct ext3_inode_info *ei;
3602 + /* preallocate only when found space is larger then requested */
3603 + BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3604 + BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3605 + BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3607 + pa = kmem_cache_alloc(ext3_pspace_cachep, SLAB_NOFS);
3611 + if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3612 + int winl, wins, win, offs;
3614 + /* we can't allocate as much as normalizer wants.
3615 + * so, found space must get proper lstart
3616 + * to cover original request */
3617 + BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3618 + BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3620 + /* we're limited by original request in that
3621 + * logical block must be covered any way
3622 + * winl is window we can move our chunk within */
3623 + winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3625 + /* also, we should cover whole original request */
3626 + wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3628 + /* the smallest one defines real window */
3629 + win = min(winl, wins);
3631 + offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3632 + if (offs && offs < win)
3635 + ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3636 + BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3637 + BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3640 + /* preallocation can change ac_b_ex, thus we store actually
3641 + * allocated blocks for history */
3642 + ac->ac_f_ex = ac->ac_b_ex;
3644 + pa->pa_lstart = ac->ac_b_ex.fe_logical;
3645 + pa->pa_pstart = ext3_grp_offs_to_block(sb, &ac->ac_b_ex);
3646 + pa->pa_len = ac->ac_b_ex.fe_len;
3647 + pa->pa_free = pa->pa_len;
3648 + atomic_set(&pa->pa_count, 1);
3649 + spin_lock_init(&pa->pa_lock);
3650 + pa->pa_deleted = 0;
3651 + pa->pa_linear = 0;
3653 + mb_debug("new inode pa %p: %lu/%lu for %lu\n", pa,
3654 + pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3656 + ext3_mb_use_inode_pa(ac, pa);
3657 + atomic_add(pa->pa_free, &EXT3_SB(sb)->s_mb_preallocated);
3659 + ei = EXT3_I(ac->ac_inode);
3660 + grp = EXT3_GROUP_INFO(sb, ac->ac_b_ex.fe_group);
3662 + pa->pa_obj_lock = &ei->i_prealloc_lock;
3663 + pa->pa_inode = ac->ac_inode;
3665 + ext3_lock_group(sb, ac->ac_b_ex.fe_group);
3666 + list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
3667 + ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
3669 + spin_lock(pa->pa_obj_lock);
3670 + list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3671 + spin_unlock(pa->pa_obj_lock);
3677 + * creates new preallocated space for locality group inodes belongs to
3679 +int ext3_mb_new_group_pa(struct ext3_allocation_context *ac)
3681 + struct super_block *sb = ac->ac_sb;
3682 + struct ext3_locality_group *lg;
3683 + struct ext3_prealloc_space *pa;
3684 + struct ext3_group_info *grp;
3686 + /* preallocate only when found space is larger then requested */
3687 + BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3688 + BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3689 + BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3691 + BUG_ON(ext3_pspace_cachep == NULL);
3692 + pa = kmem_cache_alloc(ext3_pspace_cachep, SLAB_NOFS);
3696 + /* preallocation can change ac_b_ex, thus we store actually
3697 + * allocated blocks for history */
3698 + ac->ac_f_ex = ac->ac_b_ex;
3700 + pa->pa_pstart = ext3_grp_offs_to_block(sb, &ac->ac_b_ex);
3701 + pa->pa_lstart = pa->pa_pstart;
3702 + pa->pa_len = ac->ac_b_ex.fe_len;
3703 + pa->pa_free = pa->pa_len;
3704 + atomic_set(&pa->pa_count, 1);
3705 + spin_lock_init(&pa->pa_lock);
3706 + pa->pa_deleted = 0;
3707 + pa->pa_linear = 1;
3709 + mb_debug("new group pa %p: %lu/%lu for %lu\n", pa,
3710 + pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3712 + ext3_mb_use_group_pa(ac, pa);
3713 + atomic_add(pa->pa_free, &EXT3_SB(sb)->s_mb_preallocated);
3715 + grp = EXT3_GROUP_INFO(sb, ac->ac_b_ex.fe_group);
3717 + BUG_ON(lg == NULL);
3719 + pa->pa_obj_lock = &lg->lg_prealloc_lock;
3720 + pa->pa_inode = NULL;
3722 + ext3_lock_group(sb, ac->ac_b_ex.fe_group);
3723 + list_add_rcu(&pa->pa_group_list, &grp->bb_prealloc_list);
3724 + ext3_unlock_group(sb, ac->ac_b_ex.fe_group);
3726 + spin_lock(pa->pa_obj_lock);
3727 + list_add_tail_rcu(&pa->pa_inode_list, &lg->lg_prealloc_list);
3728 + spin_unlock(pa->pa_obj_lock);
3733 +int ext3_mb_new_preallocation(struct ext3_allocation_context *ac)
3737 + if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
3738 + err = ext3_mb_new_group_pa(ac);
3740 + err = ext3_mb_new_inode_pa(ac);
3745 + * finds all unused blocks in on-disk bitmap, frees them in
3746 + * in-core bitmap and buddy.
3747 + * @pa must be unlinked from inode and group lists, so that
3748 + * nobody else can find/use it.
3749 + * the caller MUST hold group/inode locks.
3750 + * TODO: optimize the case when there are no in-core structures yet
3752 +int ext3_mb_release_inode_pa(struct ext3_buddy *e3b,
3753 + struct buffer_head *bitmap_bh,
3754 + struct ext3_prealloc_space *pa)
3756 + struct ext3_allocation_context ac;
3757 + struct super_block *sb = e3b->bd_sb;
3758 + struct ext3_sb_info *sbi = EXT3_SB(sb);
3759 + unsigned long bit, end, next, group;
3761 + int err = 0, free = 0;
3763 + BUG_ON(pa->pa_deleted == 0);
3764 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3765 + BUG_ON(group != e3b->bd_group);
3766 + end = bit + pa->pa_len;
3769 + ac.ac_inode = pa->pa_inode;
3770 + ac.ac_op = EXT3_MB_HISTORY_DISCARD;
3772 + while (bit < end) {
3773 + bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
3776 + next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
3779 + start = group * EXT3_BLOCKS_PER_GROUP(sb) + bit +
3780 + le32_to_cpu(sbi->s_es->s_first_data_block);
3781 + mb_debug(" free preallocated %u/%u in group %u\n",
3782 + (unsigned) start, (unsigned) next - bit,
3783 + (unsigned) group);
3784 + free += next - bit;
3786 + ac.ac_b_ex.fe_group = group;
3787 + ac.ac_b_ex.fe_start = bit;
3788 + ac.ac_b_ex.fe_len = next - bit;
3789 + ac.ac_b_ex.fe_logical = 0;
3790 + ext3_mb_store_history(&ac);
3792 + mb_free_blocks(pa->pa_inode, e3b, bit, next - bit);
3795 + if (free != pa->pa_free) {
3796 + printk("pa %p: logic %lu, phys. %lu, len %lu\n",
3797 + pa, (unsigned long) pa->pa_lstart,
3798 + (unsigned long) pa->pa_pstart,
3799 + (unsigned long) pa->pa_len);
3800 + printk("free %u, pa_free %u\n", free, pa->pa_free);
3802 + BUG_ON(free != pa->pa_free);
3803 + atomic_add(free, &sbi->s_mb_discarded);
3808 +int ext3_mb_release_group_pa(struct ext3_buddy *e3b,
3809 + struct ext3_prealloc_space *pa)
3811 + struct ext3_allocation_context ac;
3812 + struct super_block *sb = e3b->bd_sb;
3813 + unsigned long bit, group;
3815 + ac.ac_op = EXT3_MB_HISTORY_DISCARD;
3817 + BUG_ON(pa->pa_deleted == 0);
3818 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3819 + BUG_ON(group != e3b->bd_group);
3820 + mb_free_blocks(pa->pa_inode, e3b, bit, pa->pa_len);
3821 + atomic_add(pa->pa_len, &EXT3_SB(sb)->s_mb_discarded);
3824 + ac.ac_inode = NULL;
3825 + ac.ac_b_ex.fe_group = group;
3826 + ac.ac_b_ex.fe_start = bit;
3827 + ac.ac_b_ex.fe_len = pa->pa_len;
3828 + ac.ac_b_ex.fe_logical = 0;
3829 + ext3_mb_store_history(&ac);
3835 + * releases all preallocations in given group
3837 + * first, we need to decide discard policy:
3838 + * - when do we discard
3840 + * - how many do we discard
3841 + * 1) how many requested
3843 +int ext3_mb_discard_group_preallocations(struct super_block *sb,
3844 + int group, int needed)
3846 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, group);
3847 + struct buffer_head *bitmap_bh = NULL;
3848 + struct ext3_prealloc_space *pa, *tmp;
3849 + struct list_head list;
3850 + struct ext3_buddy e3b;
3851 + int err, busy, free = 0;
3853 + mb_debug("discard preallocation for group %lu\n", group);
3855 + if (list_empty(&grp->bb_prealloc_list))
3858 + bitmap_bh = read_block_bitmap(sb, group);
3859 + if (bitmap_bh == NULL) {
3860 + /* error handling here */
3861 + ext3_mb_release_desc(&e3b);
3862 + BUG_ON(bitmap_bh == NULL);
3865 + err = ext3_mb_load_buddy(sb, group, &e3b);
3866 + BUG_ON(err != 0); /* error handling here */
3869 + needed = EXT3_BLOCKS_PER_GROUP(sb) + 1;
3871 + grp = EXT3_GROUP_INFO(sb, group);
3872 + INIT_LIST_HEAD(&list);
3876 + ext3_lock_group(sb, group);
3877 + list_for_each_entry_safe (pa, tmp, &grp->bb_prealloc_list, pa_group_list) {
3878 + spin_lock(&pa->pa_lock);
3879 + if (atomic_read(&pa->pa_count)) {
3880 + spin_unlock(&pa->pa_lock);
3881 + printk("uh! busy PA\n");
3886 + if (pa->pa_deleted) {
3887 + spin_unlock(&pa->pa_lock);
3891 + /* seems this one can be freed ... */
3892 + pa->pa_deleted = 1;
3894 + /* we can trust pa_free ... */
3895 + free += pa->pa_free;
3897 + spin_unlock(&pa->pa_lock);
3899 + list_del_rcu(&pa->pa_group_list);
3900 + list_add(&pa->u.pa_tmp_list, &list);
3903 + /* if we still need more blocks and some PAs were used, try again */
3904 + if (free < needed && busy) {
3905 + ext3_unlock_group(sb, group);
3909 + /* found anything to free? */
3910 + if (list_empty(&list)) {
3911 + BUG_ON(free != 0);
3915 + /* now free all selected PAs */
3916 + list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3918 + /* remove from object (inode or locality group) */
3919 + spin_lock(pa->pa_obj_lock);
3920 + list_del_rcu(&pa->pa_inode_list);
3921 + spin_unlock(pa->pa_obj_lock);
3923 + if (pa->pa_linear)
3924 + ext3_mb_release_group_pa(&e3b, pa);
3926 + ext3_mb_release_inode_pa(&e3b, bitmap_bh, pa);
3928 + list_del(&pa->u.pa_tmp_list);
3933 + ext3_unlock_group(sb, group);
3934 + ext3_mb_release_desc(&e3b);
3935 + brelse(bitmap_bh);
3940 + * releases all non-used preallocated blocks for given inode
3942 +void ext3_mb_discard_inode_preallocations(struct inode *inode)
3944 + struct ext3_inode_info *ei = EXT3_I(inode);
3945 + struct super_block *sb = inode->i_sb;
3946 + struct buffer_head *bitmap_bh = NULL;
3947 + struct ext3_prealloc_space *pa, *tmp;
3948 + unsigned long group = 0;
3949 + struct list_head list;
3950 + struct ext3_buddy e3b;
3953 + if (!test_opt(sb, MBALLOC) || !S_ISREG(inode->i_mode)) {
3954 + /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3958 + mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
3960 + INIT_LIST_HEAD(&list);
3963 + /* first, collect all pa's in the inode */
3964 + spin_lock(&ei->i_prealloc_lock);
3965 + while (!list_empty(&ei->i_prealloc_list)) {
3966 + pa = list_entry(ei->i_prealloc_list.next,
3967 + struct ext3_prealloc_space, pa_inode_list);
3968 + BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3969 + spin_lock(&pa->pa_lock);
3970 + if (atomic_read(&pa->pa_count)) {
3971 + /* this shouldn't happen often - nobody should
3972 + * use preallocation while we're discarding it */
3973 + spin_unlock(&pa->pa_lock);
3974 + spin_unlock(&ei->i_prealloc_lock);
3975 + printk("uh-oh! used pa while discarding\n");
3977 + current->state = TASK_UNINTERRUPTIBLE;
3978 + schedule_timeout(HZ);
3982 + if (pa->pa_deleted == 0) {
3983 + pa->pa_deleted = 1;
3984 + spin_unlock(&pa->pa_lock);
3985 + list_del_rcu(&pa->pa_inode_list);
3986 + list_add(&pa->u.pa_tmp_list, &list);
3990 + /* someone is deleting pa right now */
3991 + spin_unlock(&pa->pa_lock);
3992 + spin_unlock(&ei->i_prealloc_lock);
3994 + /* we have to wait here because pa_deleted
3995 + * doesn't mean pa is already unlinked from
3996 + * the list. as we might be called from
3997 + * ->clear_inode() the inode will get freed
3998 + * and concurrent thread which is unlinking
3999 + * pa from inode's list may access already
4000 + * freed memory, bad-bad-bad */
4002 + /* XXX: if this happens too often, we can
4003 + * add a flag to force wait only in case
4004 + * of ->clear_inode(), but not in case of
4005 + * regular truncate */
4006 + printk("uh-oh! some one just deleted it\n");
4008 + current->state = TASK_UNINTERRUPTIBLE;
4009 + schedule_timeout(HZ);
4012 + spin_unlock(&ei->i_prealloc_lock);
4014 + list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4015 + BUG_ON(pa->pa_linear != 0);
4016 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4018 + err = ext3_mb_load_buddy(sb, group, &e3b);
4019 + BUG_ON(err != 0); /* error handling here */
4021 + bitmap_bh = read_block_bitmap(sb, group);
4022 + if (bitmap_bh == NULL) {
4023 + /* error handling here */
4024 + ext3_mb_release_desc(&e3b);
4025 + BUG_ON(bitmap_bh == NULL);
4028 + ext3_lock_group(sb, group);
4029 + list_del_rcu(&pa->pa_group_list);
4030 + ext3_mb_release_inode_pa(&e3b, bitmap_bh, pa);
4031 + ext3_unlock_group(sb, group);
4033 + ext3_mb_release_desc(&e3b);
4034 + brelse(bitmap_bh);
4036 + list_del(&pa->u.pa_tmp_list);
4042 + * finds all preallocated spaces and return blocks being freed to them
4043 + * if preallocated space becomes full (no block is used from the space)
4044 + * then the function frees space in buddy
4045 + * XXX: at the moment, truncate (which is the only way to free blocks)
4046 + * discards all preallocations
4048 +void ext3_mb_return_to_preallocation(struct inode *inode, struct ext3_buddy *e3b,
4049 + sector_t block, int count)
4051 + BUG_ON(!list_empty(&EXT3_I(inode)->i_prealloc_list));
4054 +void ext3_mb_show_ac(struct ext3_allocation_context *ac)
4057 + struct super_block *sb = ac->ac_sb;
4060 + printk(KERN_ERR "EXT3-fs: can't allocate: status %d flags %d\n",
4061 + ac->ac_status, ac->ac_flags);
4062 + printk(KERN_ERR "EXT3-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4063 + "best %lu/%lu/%lu@%lu cr %d\n",
4064 + ac->ac_o_ex.fe_group, ac->ac_o_ex.fe_start,
4065 + ac->ac_o_ex.fe_len, ac->ac_o_ex.fe_logical,
4066 + ac->ac_g_ex.fe_group, ac->ac_g_ex.fe_start,
4067 + ac->ac_g_ex.fe_len, ac->ac_g_ex.fe_logical,
4068 + ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start,
4069 + ac->ac_b_ex.fe_len, ac->ac_b_ex.fe_logical,
4071 + printk(KERN_ERR "EXT3-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4073 + printk("EXT3-fs: groups: ");
4074 + for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
4075 + struct ext3_group_info *grp = EXT3_GROUP_INFO(sb, i);
4076 + struct ext3_prealloc_space *pa;
4077 + unsigned long start;
4078 + struct list_head *cur;
4079 + list_for_each_rcu(cur, &grp->bb_prealloc_list) {
4080 + pa = list_entry(cur, struct ext3_prealloc_space,
4082 + spin_lock(&pa->pa_lock);
4083 + ext3_get_group_no_and_offset(sb, pa->pa_pstart, NULL, &start);
4084 + spin_unlock(&pa->pa_lock);
4085 + printk("PA:%u:%lu:%u ", i, start, pa->pa_len);
4088 + if (grp->bb_free == 0)
4090 + printk("%d: %d/%d ", i, grp->bb_free, grp->bb_fragments);
4097 +void ext3_mb_group_or_file(struct ext3_allocation_context *ac)
4099 + struct ext3_sb_info *sbi = EXT3_SB(ac->ac_sb);
4100 + int bsbits = ac->ac_sb->s_blocksize_bits;
4101 + loff_t size, isize;
4103 + if (!(ac->ac_flags & EXT3_MB_HINT_DATA))
4106 + size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4107 + isize = i_size_read(ac->ac_inode) >> bsbits;
4111 + /* don't use group allocation for large files */
4112 + if (size >= sbi->s_mb_stream_request)
4115 + if (unlikely(ac->ac_flags & EXT3_MB_HINT_GOAL_ONLY))
4118 + BUG_ON(ac->ac_lg != NULL);
4119 + ac->ac_lg = &sbi->s_locality_groups[smp_processor_id()];
4121 + /* we're going to use group allocation */
4122 + ac->ac_flags |= EXT3_MB_HINT_GROUP_ALLOC;
4124 + /* serialize all allocations in the group */
4125 + down(&ac->ac_lg->lg_sem);
4128 +int ext3_mb_initialize_context(struct ext3_allocation_context *ac,
4129 + struct ext3_allocation_request *ar)
4131 + struct super_block *sb = ar->inode->i_sb;
4132 + struct ext3_sb_info *sbi = EXT3_SB(sb);
4133 + struct ext3_super_block *es = sbi->s_es;
4134 + unsigned long group, len, goal;
4135 + unsigned long block;
4137 + /* we can't allocate > group size */
4139 + if (len >= EXT3_BLOCKS_PER_GROUP(sb) - 10)
4140 + len = EXT3_BLOCKS_PER_GROUP(sb) - 10;
4142 + /* start searching from the goal */
4144 + if (goal < le32_to_cpu(es->s_first_data_block) ||
4145 + goal >= le32_to_cpu(es->s_blocks_count))
4146 + goal = le32_to_cpu(es->s_first_data_block);
4147 + ext3_get_group_no_and_offset(sb, goal, &group, &block);
4149 + /* set up allocation goals */
4150 + ac->ac_b_ex.fe_logical = ar->logical;
4151 + ac->ac_b_ex.fe_group = 0;
4152 + ac->ac_b_ex.fe_start = 0;
4153 + ac->ac_b_ex.fe_len = 0;
4154 + ac->ac_status = AC_STATUS_CONTINUE;
4155 + ac->ac_groups_scanned = 0;
4156 + ac->ac_ex_scanned = 0;
4159 + ac->ac_inode = ar->inode;
4160 + ac->ac_o_ex.fe_logical = ar->logical;
4161 + ac->ac_o_ex.fe_group = group;
4162 + ac->ac_o_ex.fe_start = block;
4163 + ac->ac_o_ex.fe_len = len;
4164 + ac->ac_g_ex.fe_logical = ar->logical;
4165 + ac->ac_g_ex.fe_group = group;
4166 + ac->ac_g_ex.fe_start = block;
4167 + ac->ac_g_ex.fe_len = len;
4168 + ac->ac_f_ex.fe_len = 0;
4169 + ac->ac_flags = ar->flags;
4170 + ac->ac_2order = 0;
4171 + ac->ac_criteria = 0;
4173 + ac->ac_bitmap_page = NULL;
4174 + ac->ac_buddy_page = NULL;
4177 + /* we have to define context: we'll we work with a file or
4178 + * locality group. this is a policy, actually */
4179 + ext3_mb_group_or_file(ac);
4181 + mb_debug("init ac: %u blocks @ %llu, goal %llu, flags %x, 2^%d, "
4182 + "left: %llu/%llu, right %llu/%llu to %swritable\n",
4183 + (unsigned) ar->len, (unsigned) ar->logical,
4184 + (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4185 + (unsigned) ar->lleft, (unsigned) ar->pleft,
4186 + (unsigned) ar->lright, (unsigned) ar->pright,
4187 + atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4193 + * release all resource we used in allocation
4195 +int ext3_mb_release_context(struct ext3_allocation_context *ac)
4198 + if (ac->ac_pa->pa_linear) {
4199 + /* see comment in ext3_mb_use_group_pa() */
4200 + spin_lock(&ac->ac_pa->pa_lock);
4201 + ac->ac_pa->pa_pstart += ac->ac_b_ex.fe_len;
4202 + ac->ac_pa->pa_lstart += ac->ac_b_ex.fe_len;
4203 + ac->ac_pa->pa_free -= ac->ac_b_ex.fe_len;
4204 + ac->ac_pa->pa_len -= ac->ac_b_ex.fe_len;
4205 + spin_unlock(&ac->ac_pa->pa_lock);
4207 + ext3_mb_put_pa(ac, ac->ac_sb, ac->ac_pa);
4209 + if (ac->ac_bitmap_page)
4210 + page_cache_release(ac->ac_bitmap_page);
4211 + if (ac->ac_buddy_page)
4212 + page_cache_release(ac->ac_buddy_page);
4213 + if (ac->ac_flags & EXT3_MB_HINT_GROUP_ALLOC)
4214 + up(&ac->ac_lg->lg_sem);
4215 + ext3_mb_collect_stats(ac);
4219 +int ext3_mb_discard_preallocations(struct super_block *sb, int needed)
4221 + int i, ret, freed = 0;
4223 + for (i = 0; i < EXT3_SB(sb)->s_groups_count && needed > 0; i++) {
4224 + ret = ext3_mb_discard_group_preallocations(sb, i, needed);
4233 + * Main entry point into mballoc to allocate blocks
4234 + * it tries to use preallocation first, then falls back
4235 + * to usual allocation
4237 +unsigned long ext3_mb_new_blocks(handle_t *handle,
4238 + struct ext3_allocation_request *ar, int *errp)
4240 + struct ext3_allocation_context ac;
4241 + struct ext3_sb_info *sbi;
4242 + struct super_block *sb;
4243 + unsigned long block = 0;
4244 + int freed, inquota;
4246 + sb = ar->inode->i_sb;
4247 + sbi = EXT3_SB(sb);
4249 + if (!test_opt(sb, MBALLOC)) {
4250 + static int ext3_mballoc_warning = 0;
4251 + if (ext3_mballoc_warning++ == 0)
4252 + printk(KERN_ERR "EXT3-fs: multiblock request with "
4253 + "mballoc disabled!\n");
4255 + block = ext3_new_block_old(handle, ar->inode, ar->goal, errp);
4259 + while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
4260 + ar->flags |= EXT3_MB_HINT_NOPREALLOC;
4263 + if (ar->len == 0) {
4267 + inquota = ar->len;
4269 + ext3_mb_poll_new_transaction(sb, handle);
4271 + if ((*errp = ext3_mb_initialize_context(&ac, ar))) {
4276 + ac.ac_op = EXT3_MB_HISTORY_PREALLOC;
4277 + if (!ext3_mb_use_preallocated(&ac)) {
4279 + ac.ac_op = EXT3_MB_HISTORY_ALLOC;
4280 + ext3_mb_normalize_request(&ac, ar);
4283 + /* allocate space in core */
4284 + ext3_mb_regular_allocator(&ac);
4286 + /* as we've just preallocated more space than
4287 + * user requested orinally, we store allocated
4288 + * space in a special descriptor */
4289 + if (ac.ac_status == AC_STATUS_FOUND &&
4290 + ac.ac_o_ex.fe_len < ac.ac_b_ex.fe_len)
4291 + ext3_mb_new_preallocation(&ac);
4294 + if (likely(ac.ac_status == AC_STATUS_FOUND)) {
4295 + ext3_mb_mark_diskspace_used(&ac, handle);
4297 + block = ext3_grp_offs_to_block(sb, &ac.ac_b_ex);
4298 + ar->len = ac.ac_b_ex.fe_len;
4300 + freed = ext3_mb_discard_preallocations(sb, ac.ac_o_ex.fe_len);
4304 + ac.ac_b_ex.fe_len = 0;
4306 + ext3_mb_show_ac(&ac);
4309 + ext3_mb_release_context(&ac);
4312 + if (ar->len < inquota)
4313 + DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
4317 +EXPORT_SYMBOL(ext3_mb_new_blocks);
4319 +int ext3_new_block(handle_t *handle, struct inode *inode,
4320 + unsigned long goal, int *errp)
4322 + struct ext3_allocation_request ar;
4323 + unsigned long ret;
4325 + if (!test_opt(inode->i_sb, MBALLOC)) {
4326 + ret = ext3_new_block_old(handle, inode, goal, errp);
4339 + ret = ext3_mb_new_blocks(handle, &ar, errp);
4343 +void ext3_mb_poll_new_transaction(struct super_block *sb, handle_t *handle)
4345 + struct ext3_sb_info *sbi = EXT3_SB(sb);
4347 + if (sbi->s_last_transaction == handle->h_transaction->t_tid)
4350 + /* new transaction! time to close last one and free blocks for
4351 + * committed transaction. we know that only transaction can be
4352 + * active, so previos transaction can be being logged and we
4353 + * know that transaction before previous is known to be already
4354 + * logged. this means that now we may free blocks freed in all
4355 + * transactions before previous one. hope I'm clear enough ... */
4357 + spin_lock(&sbi->s_md_lock);
4358 + if (sbi->s_last_transaction != handle->h_transaction->t_tid) {
4359 + mb_debug("new transaction %lu, old %lu\n",
4360 + (unsigned long) handle->h_transaction->t_tid,
4361 + (unsigned long) sbi->s_last_transaction);
4362 + list_splice_init(&sbi->s_closed_transaction,
4363 + &sbi->s_committed_transaction);
4364 + list_splice_init(&sbi->s_active_transaction,
4365 + &sbi->s_closed_transaction);
4366 + sbi->s_last_transaction = handle->h_transaction->t_tid;
4368 + spin_unlock(&sbi->s_md_lock);
4370 + ext3_mb_free_committed_blocks(sb);
4373 +int ext3_mb_free_metadata(handle_t *handle, struct ext3_buddy *e3b,
4374 + int group, int block, int count)
4376 + struct ext3_group_info *db = e3b->bd_info;
4377 + struct super_block *sb = e3b->bd_sb;
4378 + struct ext3_sb_info *sbi = EXT3_SB(sb);
4379 + struct ext3_free_metadata *md;
4382 + BUG_ON(e3b->bd_bitmap_page == NULL);
4383 + BUG_ON(e3b->bd_buddy_page == NULL);
4385 + ext3_lock_group(sb, group);
4386 + for (i = 0; i < count; i++) {
4387 + md = db->bb_md_cur;
4388 + if (md && db->bb_tid != handle->h_transaction->t_tid) {
4389 + db->bb_md_cur = NULL;
4394 + ext3_unlock_group(sb, group);
4395 + md = kmalloc(sizeof(*md), GFP_KERNEL);
4399 + md->group = group;
4401 + ext3_lock_group(sb, group);
4402 + if (db->bb_md_cur == NULL) {
4403 + spin_lock(&sbi->s_md_lock);
4404 + list_add(&md->list, &sbi->s_active_transaction);
4405 + spin_unlock(&sbi->s_md_lock);
4406 + /* protect buddy cache from being freed,
4407 + * otherwise we'll refresh it from
4408 + * on-disk bitmap and lose not-yet-available
4410 + page_cache_get(e3b->bd_buddy_page);
4411 + page_cache_get(e3b->bd_bitmap_page);
4412 + db->bb_md_cur = md;
4413 + db->bb_tid = handle->h_transaction->t_tid;
4414 + mb_debug("new md 0x%p for group %u\n",
4418 + md = db->bb_md_cur;
4422 + BUG_ON(md->num >= EXT3_BB_MAX_BLOCKS);
4423 + md->blocks[md->num] = block + i;
4425 + if (md->num == EXT3_BB_MAX_BLOCKS) {
4426 + /* no more space, put full container on a sb's list */
4427 + db->bb_md_cur = NULL;
4430 + ext3_unlock_group(sb, group);
4435 + * Main entry point into mballoc to free blocks
4437 +void ext3_mb_free_blocks(handle_t *handle, struct inode *inode,
4438 + unsigned long block, unsigned long count,
4439 + int metadata, int *freed)
4441 + struct buffer_head *bitmap_bh = NULL;
4442 + struct super_block *sb = inode->i_sb;
4443 + struct ext3_allocation_context ac;
4444 + struct ext3_group_desc *gdp;
4445 + struct ext3_super_block *es;
4446 + unsigned long bit, overflow;
4447 + struct buffer_head *gd_bh;
4448 + unsigned long block_group;
4449 + struct ext3_sb_info *sbi;
4450 + struct ext3_buddy e3b;
4455 + ext3_mb_poll_new_transaction(sb, handle);
4457 + sbi = EXT3_SB(sb);
4458 + es = EXT3_SB(sb)->s_es;
4459 + if (block < le32_to_cpu(es->s_first_data_block) ||
4460 + block + count < block ||
4461 + block + count > le32_to_cpu(es->s_blocks_count)) {
4462 + ext3_error (sb, __FUNCTION__,
4463 + "Freeing blocks not in datazone - "
4464 + "block = %lu, count = %lu", block, count);
4465 + goto error_return;
4468 + ext3_debug("freeing block %lu\n", block);
4470 + ac.ac_op = EXT3_MB_HISTORY_FREE;
4471 + ac.ac_inode = inode;
4476 + ext3_get_group_no_and_offset(sb, block, &block_group, &bit);
4479 + * Check to see if we are freeing blocks across a group
4482 + if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
4483 + overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
4484 + count -= overflow;
4486 + brelse(bitmap_bh);
4487 + bitmap_bh = read_block_bitmap(sb, block_group);
4489 + goto error_return;
4490 + gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
4492 + goto error_return;
4494 + if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
4495 + in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
4496 + in_range (block, le32_to_cpu(gdp->bg_inode_table),
4497 + EXT3_SB(sb)->s_itb_per_group) ||
4498 + in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
4499 + EXT3_SB(sb)->s_itb_per_group))
4500 + ext3_error(sb, __FUNCTION__,
4501 + "Freeing blocks in system zone - "
4502 + "Block = %lu, count = %lu", block, count);
4504 + BUFFER_TRACE(bitmap_bh, "getting write access");
4505 + err = ext3_journal_get_write_access(handle, bitmap_bh);
4507 + goto error_return;
4510 + * We are about to modify some metadata. Call the journal APIs
4511 + * to unshare ->b_data if a currently-committing transaction is
4514 + BUFFER_TRACE(gd_bh, "get_write_access");
4515 + err = ext3_journal_get_write_access(handle, gd_bh);
4517 + goto error_return;
4519 + err = ext3_mb_load_buddy(sb, block_group, &e3b);
4521 + goto error_return;
4523 +#ifdef AGGRESSIVE_CHECK
4526 + for (i = 0; i < count; i++)
4527 + BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4530 + mb_clear_bits(bitmap_bh->b_data, bit, count);
4532 + /* We dirtied the bitmap block */
4533 + BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4534 + err = ext3_journal_dirty_metadata(handle, bitmap_bh);
4536 + ac.ac_b_ex.fe_group = block_group;
4537 + ac.ac_b_ex.fe_start = bit;
4538 + ac.ac_b_ex.fe_len = count;
4539 + ext3_mb_store_history(&ac);
4542 + /* blocks being freed are metadata. these blocks shouldn't
4543 + * be used until this transaction is committed */
4544 + ext3_mb_free_metadata(handle, &e3b, block_group, bit, count);
4546 + ext3_lock_group(sb, block_group);
4547 + err = mb_free_blocks(inode, &e3b, bit, count);
4548 + ext3_mb_return_to_preallocation(inode, &e3b, block, count);
4549 + ext3_unlock_group(sb, block_group);
4553 + spin_lock(sb_bgl_lock(sbi, block_group));
4554 + gdp->bg_free_blocks_count =
4555 + cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) + count);
4556 + spin_unlock(sb_bgl_lock(sbi, block_group));
4557 + percpu_counter_mod(&sbi->s_freeblocks_counter, count);
4559 + ext3_mb_release_desc(&e3b);
4563 + /* And the group descriptor block */
4564 + BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4565 + ret = ext3_journal_dirty_metadata(handle, gd_bh);
4566 + if (!err) err = ret;
4568 + if (overflow && !err) {
4575 + brelse(bitmap_bh);
4576 + ext3_std_error(sb, err);