2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 * mballoc.c contains the multiblocks allocation routines
24 #include <linux/time.h>
26 #include <linux/namei.h>
27 #include <linux/ext4_jbd2.h>
28 #include <linux/ext4_fs.h>
29 #include <linux/quotaops.h>
30 #include <linux/buffer_head.h>
31 #include <linux/module.h>
32 #include <linux/swap.h>
33 #include <linux/proc_fs.h>
34 #include <linux/pagemap.h>
35 #include <linux/seq_file.h>
36 #include <linux/version.h>
41 * - test ext4_ext_search_left() and ext4_ext_search_right()
42 * - search for metadata in few groups
45 * - normalization should take into account whether file is still open
46 * - discard preallocations if no free space left (policy?)
47 * - don't normalize tails
49 * - reservation for superuser
52 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
53 * - track min/max extents in each group for better group selection
54 * - mb_mark_used() may allocate chunk right after splitting buddy
55 * - tree of groups sorted by number of free blocks
60 * The allocation request involve request for multiple number of blocks
61 * near to the goal(block) value specified.
63 * During initialization phase of the allocator we decide to use the group
64 * preallocation or inode preallocation depending on the size file. The
65 * size of the file could be the resulting file size we would have after
66 * allocation or the current file size which ever is larger. If the size is
67 * less that sbi->s_mb_stream_request we select the group
68 * preallocation. The default value of s_mb_stream_request is 16
69 * blocks. This can also be tuned via
70 * /proc/fs/ext4/<partition>/stream_req. The value is represented in terms
71 * of number of blocks.
73 * The main motivation for having small file use group preallocation is to
74 * ensure that we have small file closer in the disk.
76 * First stage the allocator looks at the inode prealloc list
77 * ext4_inode_info->i_prealloc_list contain list of prealloc spaces for
78 * this particular inode. The inode prealloc space is represented as:
80 * pa_lstart -> the logical start block for this prealloc space
81 * pa_pstart -> the physical start block for this prealloc space
82 * pa_len -> lenght for this prealloc space
83 * pa_free -> free space available in this prealloc space
85 * The inode preallocation space is used looking at the _logical_ start
86 * block. If only the logical file block falls within the range of prealloc
87 * space we will consume the particular prealloc space. This make sure that
88 * that the we have contiguous physical blocks representing the file blocks
90 * The important thing to be noted in case of inode prealloc space is that
91 * we don't modify the values associated to inode prealloc space except
94 * If we are not able to find blocks in the inode prealloc space and if we
95 * have the group allocation flag set then we look at the locality group
96 * prealloc space. These are per CPU prealloc list repreasented as
98 * ext4_sb_info.s_locality_groups[smp_processor_id()]
100 * The reason for having a per cpu locality group is to reduce the contention
101 * between CPUs. It is possible to get scheduled at this point.
103 * The locality group prealloc space is used looking at whether we have
104 * enough free space (pa_free) withing the prealloc space.
106 * If we can't allocate blocks via inode prealloc or/and locality group
107 * prealloc then we look at the buddy cache. The buddy cache is represented
108 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
109 * mapped to the buddy and bitmap information regarding different
110 * groups. The buddy information is attached to buddy cache inode so that
111 * we can access them through the page cache. The information regarding
112 * each group is loaded via ext4_mb_load_buddy. The information involve
113 * block bitmap and buddy information. The information are stored in the
117 * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
120 * one block each for bitmap and buddy information. So for each group we
121 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
122 * blocksize) blocks. So it can have information regarding groups_per_page
123 * which is blocks_per_page/2
125 * The buddy cache inode is not stored on disk. The inode is thrown
126 * away when the filesystem is unmounted.
128 * We look for count number of blocks in the buddy cache. If we were able
129 * to locate that many free blocks we return with additional information
130 * regarding rest of the contiguous physical block available
132 * Before allocating blocks via buddy cache we normalize the request
133 * blocks. This ensure we ask for more blocks that we needed. The extra
134 * blocks that we get after allocation is added to the respective prealloc
135 * list. In case of inode preallocation we follow a list of heuristics
136 * based on file size. This can be found in ext4_mb_normalize_request. If
137 * we are doing a group prealloc we try to normalize the request to
138 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is set to
139 * 512 blocks. This can be tuned via
140 * /proc/fs/ext4/<partition/group_prealloc. The value is represented in
141 * terms of number of blocks. If we have mounted the file system with -O
142 * stripe=<value> option the group prealloc request is normalized to the
143 * stripe value (sbi->s_stripe)
145 * The regular allocator(using the buddy cache) support few tunables.
147 * /proc/fs/ext4/<partition>/min_to_scan
148 * /proc/fs/ext4/<partition>/max_to_scan
149 * /proc/fs/ext4/<partition>/order2_req
151 * The regular allocator use buddy scan only if the request len is power of
152 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
153 * value of s_mb_order2_reqs can be tuned via
154 * /proc/fs/ext4/<partition>/order2_req. If the request len is equal to
155 * stripe size (sbi->s_stripe), we try to search for contigous block in
156 * stripe size. This should result in better allocation on RAID setup. If
157 * not we search in the specific group using bitmap for best extents. The
158 * tunable min_to_scan and max_to_scan controll the behaviour here.
159 * min_to_scan indicate how long the mballoc __must__ look for a best
160 * extent and max_to_scanindicate how long the mballoc __can__ look for a
161 * best extent in the found extents. Searching for the blocks starts with
162 * the group specified as the goal value in allocation context via
163 * ac_g_ex. Each group is first checked based on the criteria whether it
164 * can used for allocation. ext4_mb_good_group explains how the groups are
167 * Both the prealloc space are getting populated as above. So for the first
168 * request we will hit the buddy cache which will result in this prealloc
169 * space getting filled. The prealloc space is then later used for the
170 * subsequent request.
174 * mballoc operates on the following data:
176 * - in-core buddy (actually includes buddy and bitmap)
177 * - preallocation descriptors (PAs)
179 * there are two types of preallocations:
181 * assiged to specific inode and can be used for this inode only.
182 * it describes part of inode's space preallocated to specific
183 * physical blocks. any block from that preallocated can be used
184 * independent. the descriptor just tracks number of blocks left
185 * unused. so, before taking some block from descriptor, one must
186 * make sure corresponded logical block isn't allocated yet. this
187 * also means that freeing any block within descriptor's range
188 * must discard all preallocated blocks.
190 * assigned to specific locality group which does not translate to
191 * permanent set of inodes: inode can join and leave group. space
192 * from this type of preallocation can be used for any inode. thus
193 * it's consumed from the beginning to the end.
195 * relation between them can be expressed as:
196 * in-core buddy = on-disk bitmap + preallocation descriptors
198 * this mean blocks mballoc considers used are:
199 * - allocated blocks (persistent)
200 * - preallocated blocks (non-persistent)
202 * consistency in mballoc world means that at any time a block is either
203 * free or used in ALL structures. notice: "any time" should not be read
204 * literally -- time is discrete and delimited by locks.
206 * to keep it simple, we don't use block numbers, instead we count number of
207 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
209 * all operations can be expressed as:
210 * - init buddy: buddy = on-disk + PAs
211 * - new PA: buddy += N; PA = N
212 * - use inode PA: on-disk += N; PA -= N
213 * - discard inode PA buddy -= on-disk - PA; PA = 0
214 * - use locality group PA on-disk += N; PA -= N
215 * - discard locality group PA buddy -= PA; PA = 0
216 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
217 * is used in real operation because we can't know actual used
218 * bits from PA, only from on-disk bitmap
220 * if we follow this strict logic, then all operations above should be atomic.
221 * given some of them can block, we'd have to use something like semaphores
222 * killing performance on high-end SMP hardware. let's try to relax it using
223 * the following knowledge:
224 * 1) if buddy is referenced, it's already initialized
225 * 2) while block is used in buddy and the buddy is referenced,
226 * nobody can re-allocate that block
227 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
228 * bit set and PA claims same block, it's OK. IOW, one can set bit in
229 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
232 * so, now we're building a concurrency table:
235 * blocks for PA are allocated in the buddy, buddy must be referenced
236 * until PA is linked to allocation group to avoid concurrent buddy init
238 * we need to make sure that either on-disk bitmap or PA has uptodate data
239 * given (3) we care that PA-=N operation doesn't interfere with init
241 * the simplest way would be to have buddy initialized by the discard
242 * - use locality group PA
243 * again PA-=N must be serialized with init
244 * - discard locality group PA
245 * the simplest way would be to have buddy initialized by the discard
248 * i_data_sem serializes them
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * some mutex should serialize them
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
257 * i_data_sem or another mutex should serializes them
259 * discard process must wait until PA isn't used by another process
260 * - use locality group PA
261 * nothing wrong here -- they're different PAs covering different blocks
262 * - discard locality group PA
263 * discard process must wait until PA isn't used by another process
265 * now we're ready to make few consequences:
266 * - PA is referenced and while it is no discard is possible
267 * - PA is referenced until block isn't marked in on-disk bitmap
268 * - PA changes only after on-disk bitmap
269 * - discard must not compete with init. either init is done before
270 * any discard or they're serialized somehow
271 * - buddy init as sum of on-disk bitmap and PAs is done atomically
273 * a special case when we've used PA to emptiness. no need to modify buddy
274 * in this case, but we should care about concurrent init
279 * Logic in few words:
284 * mark bits in on-disk bitmap
287 * - use preallocation:
288 * find proper PA (per-inode or group)
290 * mark bits in on-disk bitmap
296 * mark bits in on-disk bitmap
299 * - discard preallocations in group:
301 * move them onto local list
302 * load on-disk bitmap
304 * remove PA from object (inode or locality group)
305 * mark free blocks in-core
307 * - discard inode's preallocations:
314 * - bitlock on a group (group)
315 * - object (inode/locality) (object)
326 * - release consumed pa:
331 * - generate in-core bitmap:
335 * - discard all for given object (inode, locality group):
340 * - discard all for given group:
349 * with AGGRESSIVE_CHECK allocator runs consistency checks over
350 * structures. these checks slow things down a lot
352 #define AGGRESSIVE_CHECK__
355 * with DOUBLE_CHECK defined mballoc creates persistent in-core
356 * bitmaps, maintains and uses them to check for double allocations
358 #define DOUBLE_CHECK__
364 #define mb_debug(fmt, a...) printk(fmt, ##a)
366 #define mb_debug(fmt, a...)
370 * with EXT4_MB_HISTORY mballoc stores last N allocations in memory
371 * and you can monitor it in /proc/fs/ext4/<dev>/mb_history
373 #define EXT4_MB_HISTORY
374 #define EXT4_MB_HISTORY_ALLOC 1 /* allocation */
375 #define EXT4_MB_HISTORY_PREALLOC 2 /* preallocated blocks used */
376 #define EXT4_MB_HISTORY_DISCARD 4 /* preallocation discarded */
377 #define EXT4_MB_HISTORY_FREE 8 /* free */
379 #define EXT4_MB_HISTORY_DEFAULT (EXT4_MB_HISTORY_ALLOC | \
380 EXT4_MB_HISTORY_PREALLOC)
383 * How long mballoc can look for a best extent (in found extents)
385 #define MB_DEFAULT_MAX_TO_SCAN 200
388 * How long mballoc must look for a best extent
390 #define MB_DEFAULT_MIN_TO_SCAN 10
393 * How many groups mballoc will scan looking for the best chunk
395 #define MB_DEFAULT_MAX_GROUPS_TO_SCAN 5
398 * with 'ext4_mb_stats' allocator will collect stats that will be
399 * shown at umount. The collecting costs though!
401 #define MB_DEFAULT_STATS 1
404 * files smaller than MB_DEFAULT_STREAM_THRESHOLD are served
405 * by the stream allocator, which purpose is to pack requests
406 * as close each to other as possible to produce smooth I/O traffic
407 * We use locality group prealloc space for stream request.
408 * We can tune the same via /proc/fs/ext4/<parition>/stream_req
410 #define MB_DEFAULT_STREAM_THRESHOLD 16 /* 64K */
413 * for which requests use 2^N search using buddies
415 #define MB_DEFAULT_ORDER2_REQS 2
418 * default group prealloc size 512 blocks
420 #define MB_DEFAULT_GROUP_PREALLOC 512
422 static struct kmem_cache
*ext4_pspace_cachep
;
424 #ifdef EXT4_BB_MAX_BLOCKS
425 #undef EXT4_BB_MAX_BLOCKS
427 #define EXT4_BB_MAX_BLOCKS 30
429 struct ext4_free_metadata
{
432 ext4_grpblk_t blocks
[EXT4_BB_MAX_BLOCKS
];
433 struct list_head list
;
436 struct ext4_group_info
{
437 unsigned long bb_state
;
438 unsigned long bb_tid
;
439 struct ext4_free_metadata
*bb_md_cur
;
440 unsigned short bb_first_free
;
441 unsigned short bb_free
;
442 unsigned short bb_fragments
;
443 struct list_head bb_prealloc_list
;
447 unsigned short bb_counters
[];
450 #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
451 #define EXT4_GROUP_INFO_LOCKED_BIT 1
453 #define EXT4_MB_GRP_NEED_INIT(grp) \
454 (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
457 struct ext4_prealloc_space
{
458 struct list_head pa_inode_list
;
459 struct list_head pa_group_list
;
461 struct list_head pa_tmp_list
;
462 struct rcu_head pa_rcu
;
467 ext4_fsblk_t pa_pstart
; /* phys. block */
468 ext4_lblk_t pa_lstart
; /* log. block */
469 unsigned short pa_len
; /* len of preallocated chunk */
470 unsigned short pa_free
; /* how many blocks are free */
471 unsigned short pa_linear
; /* consumed in one direction
472 * strictly, for grp prealloc */
473 spinlock_t
*pa_obj_lock
;
474 struct inode
*pa_inode
; /* hack, for history only */
478 struct ext4_free_extent
{
479 ext4_lblk_t fe_logical
;
480 ext4_grpblk_t fe_start
;
481 ext4_group_t fe_group
;
487 * we try to group all related changes together
488 * so that writeback can flush/allocate them together as well
490 struct ext4_locality_group
{
492 struct mutex lg_mutex
; /* to serialize allocates */
493 struct list_head lg_prealloc_list
;/* list of preallocations */
494 spinlock_t lg_prealloc_lock
;
497 struct ext4_allocation_context
{
498 struct inode
*ac_inode
;
499 struct super_block
*ac_sb
;
501 /* original request */
502 struct ext4_free_extent ac_o_ex
;
504 /* goal request (after normalization) */
505 struct ext4_free_extent ac_g_ex
;
507 /* the best found extent */
508 struct ext4_free_extent ac_b_ex
;
510 /* copy of the bext found extent taken before preallocation efforts */
511 struct ext4_free_extent ac_f_ex
;
513 /* number of iterations done. we have to track to limit searching */
514 unsigned long ac_ex_scanned
;
515 __u16 ac_groups_scanned
;
519 __u16 ac_flags
; /* allocation hints */
523 __u8 ac_2order
; /* if request is to allocate 2^N blocks and
524 * N > 0, the field stores N, otherwise 0 */
525 __u8 ac_op
; /* operation, for history only */
526 struct page
*ac_bitmap_page
;
527 struct page
*ac_buddy_page
;
528 struct ext4_prealloc_space
*ac_pa
;
529 struct ext4_locality_group
*ac_lg
;
532 #define AC_STATUS_CONTINUE 1
533 #define AC_STATUS_FOUND 2
534 #define AC_STATUS_BREAK 3
536 struct ext4_mb_history
{
537 struct ext4_free_extent orig
; /* orig allocation */
538 struct ext4_free_extent goal
; /* goal allocation */
539 struct ext4_free_extent result
; /* result allocation */
542 __u16 found
; /* how many extents have been found */
543 __u16 groups
; /* how many groups have been scanned */
544 __u16 tail
; /* what tail broke some buddy */
545 __u16 buddy
; /* buddy the tail ^^^ broke */
547 __u8 cr
:3; /* which phase the result extent was found at */
553 struct page
*bd_buddy_page
;
555 struct page
*bd_bitmap_page
;
557 struct ext4_group_info
*bd_info
;
558 struct super_block
*bd_sb
;
560 ext4_group_t bd_group
;
562 #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
563 #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
565 #ifndef EXT4_MB_HISTORY
566 static inline void ext4_mb_store_history(struct ext4_allocation_context
*ac
)
571 static void ext4_mb_store_history(struct ext4_allocation_context
*ac
);
574 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
576 static struct proc_dir_entry
*proc_root_ext4
;
577 struct buffer_head
*read_block_bitmap(struct super_block
*, ext4_group_t
);
578 ext4_fsblk_t
ext4_new_blocks_old(handle_t
*handle
, struct inode
*inode
,
579 ext4_fsblk_t goal
, unsigned long *count
, int *errp
);
581 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
583 static void ext4_mb_poll_new_transaction(struct super_block
*, handle_t
*);
584 static void ext4_mb_free_committed_blocks(struct super_block
*);
585 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
586 struct ext4_buddy
*e4b
, sector_t block
,
588 static void ext4_mb_put_pa(struct ext4_allocation_context
*,
589 struct super_block
*, struct ext4_prealloc_space
*pa
);
590 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
);
591 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
);
594 static inline void ext4_lock_group(struct super_block
*sb
, ext4_group_t group
)
596 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
598 bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT
, &(grinfo
->bb_state
));
601 static inline void ext4_unlock_group(struct super_block
*sb
,
604 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
606 bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT
, &(grinfo
->bb_state
));
609 static inline int ext4_is_group_locked(struct super_block
*sb
,
612 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
614 return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT
,
615 &(grinfo
->bb_state
));
618 static ext4_fsblk_t
ext4_grp_offs_to_block(struct super_block
*sb
,
619 struct ext4_free_extent
*fex
)
623 block
= (ext4_fsblk_t
) fex
->fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
625 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
629 #if BITS_PER_LONG == 64
630 #define mb_correct_addr_and_bit(bit, addr) \
632 bit += ((unsigned long) addr & 7UL) << 3; \
633 addr = (void *) ((unsigned long) addr & ~7UL); \
635 #elif BITS_PER_LONG == 32
636 #define mb_correct_addr_and_bit(bit, addr) \
638 bit += ((unsigned long) addr & 3UL) << 3; \
639 addr = (void *) ((unsigned long) addr & ~3UL); \
642 #error "how many bits you are?!"
645 static inline int mb_test_bit(int bit
, void *addr
)
648 * ext4_test_bit on architecture like powerpc
649 * needs unsigned long aligned address
651 mb_correct_addr_and_bit(bit
, addr
);
652 return ext4_test_bit(bit
, addr
);
655 static inline void mb_set_bit(int bit
, void *addr
)
657 mb_correct_addr_and_bit(bit
, addr
);
658 ext4_set_bit(bit
, addr
);
661 static inline void mb_set_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
663 mb_correct_addr_and_bit(bit
, addr
);
664 ext4_set_bit_atomic(lock
, bit
, addr
);
667 static inline void mb_clear_bit(int bit
, void *addr
)
669 mb_correct_addr_and_bit(bit
, addr
);
670 ext4_clear_bit(bit
, addr
);
673 static inline void mb_clear_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
675 mb_correct_addr_and_bit(bit
, addr
);
676 ext4_clear_bit_atomic(lock
, bit
, addr
);
679 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
683 /* FIXME!! is this needed */
684 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
687 if (order
> e4b
->bd_blkbits
+ 1) {
692 /* at order 0 we see each particular block */
693 *max
= 1 << (e4b
->bd_blkbits
+ 3);
695 return EXT4_MB_BITMAP(e4b
);
697 bb
= EXT4_MB_BUDDY(e4b
) + EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
698 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
704 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
705 int first
, int count
)
708 struct super_block
*sb
= e4b
->bd_sb
;
710 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
712 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
713 for (i
= 0; i
< count
; i
++) {
714 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
715 ext4_fsblk_t blocknr
;
716 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
717 blocknr
+= first
+ i
;
719 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
721 ext4_error(sb
, __FUNCTION__
, "double-free of inode"
722 " %lu's block %llu(bit %u in group %lu)\n",
723 inode
? inode
->i_ino
: 0, blocknr
,
724 first
+ i
, e4b
->bd_group
);
726 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
730 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
734 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
736 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
737 for (i
= 0; i
< count
; i
++) {
738 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
739 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
743 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
745 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
746 unsigned char *b1
, *b2
;
748 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
749 b2
= (unsigned char *) bitmap
;
750 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
751 if (b1
[i
] != b2
[i
]) {
752 printk("corruption in group %lu at byte %u(%u):"
753 " %x in copy != %x on disk/prealloc\n",
754 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
762 static inline void mb_free_blocks_double(struct inode
*inode
,
763 struct ext4_buddy
*e4b
, int first
, int count
)
767 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
768 int first
, int count
)
772 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
778 #ifdef AGGRESSIVE_CHECK
780 #define MB_CHECK_ASSERT(assert) \
784 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
785 function, file, line, # assert); \
790 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
791 const char *function
, int line
)
793 struct super_block
*sb
= e4b
->bd_sb
;
794 int order
= e4b
->bd_blkbits
+ 1;
801 struct ext4_group_info
*grp
;
804 struct list_head
*cur
;
808 if (!test_opt(sb
, MBALLOC
))
812 static int mb_check_counter
;
813 if (mb_check_counter
++ % 100 != 0)
818 buddy
= mb_find_buddy(e4b
, order
, &max
);
819 MB_CHECK_ASSERT(buddy
);
820 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
821 MB_CHECK_ASSERT(buddy2
);
822 MB_CHECK_ASSERT(buddy
!= buddy2
);
823 MB_CHECK_ASSERT(max
* 2 == max2
);
826 for (i
= 0; i
< max
; i
++) {
828 if (mb_test_bit(i
, buddy
)) {
829 /* only single bit in buddy2 may be 1 */
830 if (!mb_test_bit(i
<< 1, buddy2
)) {
832 mb_test_bit((i
<<1)+1, buddy2
));
833 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
835 mb_test_bit(i
<< 1, buddy2
));
840 /* both bits in buddy2 must be 0 */
841 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
842 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
844 for (j
= 0; j
< (1 << order
); j
++) {
845 k
= (i
* (1 << order
)) + j
;
847 !mb_test_bit(k
, EXT4_MB_BITMAP(e4b
)));
851 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
856 buddy
= mb_find_buddy(e4b
, 0, &max
);
857 for (i
= 0; i
< max
; i
++) {
858 if (!mb_test_bit(i
, buddy
)) {
859 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
867 /* check used bits only */
868 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
869 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
871 MB_CHECK_ASSERT(k
< max2
);
872 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
875 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
876 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
878 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
879 buddy
= mb_find_buddy(e4b
, 0, &max
);
880 list_for_each(cur
, &grp
->bb_prealloc_list
) {
881 ext4_group_t groupnr
;
882 struct ext4_prealloc_space
*pa
;
883 pa
= list_entry(cur
, struct ext4_prealloc_space
, group_list
);
884 ext4_get_group_no_and_offset(sb
, pa
->pstart
, &groupnr
, &k
);
885 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
886 for (i
= 0; i
< pa
->len
; i
++)
887 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
891 #undef MB_CHECK_ASSERT
892 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
893 __FILE__, __FUNCTION__, __LINE__)
895 #define mb_check_buddy(e4b)
898 /* FIXME!! need more doc */
899 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
900 void *buddy
, unsigned first
, int len
,
901 struct ext4_group_info
*grp
)
903 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
906 unsigned short chunk
;
907 unsigned short border
;
909 BUG_ON(len
>= EXT4_BLOCKS_PER_GROUP(sb
));
911 border
= 2 << sb
->s_blocksize_bits
;
914 /* find how many blocks can be covered since this position */
915 max
= ffs(first
| border
) - 1;
917 /* find how many blocks of power 2 we need to mark */
924 /* mark multiblock chunks only */
925 grp
->bb_counters
[min
]++;
927 mb_clear_bit(first
>> min
,
928 buddy
+ sbi
->s_mb_offsets
[min
]);
935 static void ext4_mb_generate_buddy(struct super_block
*sb
,
936 void *buddy
, void *bitmap
, ext4_group_t group
)
938 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
939 unsigned short max
= EXT4_BLOCKS_PER_GROUP(sb
);
940 unsigned short i
= 0;
941 unsigned short first
;
944 unsigned fragments
= 0;
945 unsigned long long period
= get_cycles();
947 /* initialize buddy from bitmap which is aggregation
948 * of on-disk bitmap and preallocations */
949 i
= ext4_find_next_zero_bit(bitmap
, max
, 0);
950 grp
->bb_first_free
= i
;
954 i
= ext4_find_next_bit(bitmap
, max
, i
);
958 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
960 grp
->bb_counters
[0]++;
962 i
= ext4_find_next_zero_bit(bitmap
, max
, i
);
964 grp
->bb_fragments
= fragments
;
966 if (free
!= grp
->bb_free
) {
968 "EXT4-fs: group %lu: %u blocks in bitmap, %u in gd\n",
969 group
, free
, grp
->bb_free
);
973 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
975 period
= get_cycles() - period
;
976 spin_lock(&EXT4_SB(sb
)->s_bal_lock
);
977 EXT4_SB(sb
)->s_mb_buddies_generated
++;
978 EXT4_SB(sb
)->s_mb_generation_time
+= period
;
979 spin_unlock(&EXT4_SB(sb
)->s_bal_lock
);
982 /* The buddy information is attached the buddy cache inode
983 * for convenience. The information regarding each group
984 * is loaded via ext4_mb_load_buddy. The information involve
985 * block bitmap and buddy information. The information are
986 * stored in the inode as
989 * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
992 * one block each for bitmap and buddy information.
993 * So for each group we take up 2 blocks. A page can
994 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
995 * So it can have information regarding groups_per_page which
996 * is blocks_per_page/2
999 static int ext4_mb_init_cache(struct page
*page
, char *incore
)
1002 int blocks_per_page
;
1003 int groups_per_page
;
1006 ext4_group_t first_group
;
1008 struct super_block
*sb
;
1009 struct buffer_head
*bhs
;
1010 struct buffer_head
**bh
;
1011 struct inode
*inode
;
1015 mb_debug("init page %lu\n", page
->index
);
1017 inode
= page
->mapping
->host
;
1019 blocksize
= 1 << inode
->i_blkbits
;
1020 blocks_per_page
= PAGE_CACHE_SIZE
/ blocksize
;
1022 groups_per_page
= blocks_per_page
>> 1;
1023 if (groups_per_page
== 0)
1024 groups_per_page
= 1;
1026 /* allocate buffer_heads to read bitmaps */
1027 if (groups_per_page
> 1) {
1029 i
= sizeof(struct buffer_head
*) * groups_per_page
;
1030 bh
= kzalloc(i
, GFP_NOFS
);
1036 first_group
= page
->index
* blocks_per_page
/ 2;
1038 /* read all groups the page covers into the cache */
1039 for (i
= 0; i
< groups_per_page
; i
++) {
1040 struct ext4_group_desc
*desc
;
1042 if (first_group
+ i
>= EXT4_SB(sb
)->s_groups_count
)
1046 desc
= ext4_get_group_desc(sb
, first_group
+ i
, NULL
);
1051 bh
[i
] = sb_getblk(sb
, ext4_block_bitmap(sb
, desc
));
1055 if (bh_uptodate_or_lock(bh
[i
]))
1058 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
1059 ext4_init_block_bitmap(sb
, bh
[i
],
1060 first_group
+ i
, desc
);
1061 set_buffer_uptodate(bh
[i
]);
1062 unlock_buffer(bh
[i
]);
1066 bh
[i
]->b_end_io
= end_buffer_read_sync
;
1067 submit_bh(READ
, bh
[i
]);
1068 mb_debug("read bitmap for group %lu\n", first_group
+ i
);
1071 /* wait for I/O completion */
1072 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
1073 wait_on_buffer(bh
[i
]);
1076 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
1077 if (!buffer_uptodate(bh
[i
]))
1080 first_block
= page
->index
* blocks_per_page
;
1081 for (i
= 0; i
< blocks_per_page
; i
++) {
1083 struct ext4_group_info
*grinfo
;
1085 group
= (first_block
+ i
) >> 1;
1086 if (group
>= EXT4_SB(sb
)->s_groups_count
)
1090 * data carry information regarding this
1091 * particular group in the format specified
1095 data
= page_address(page
) + (i
* blocksize
);
1096 bitmap
= bh
[group
- first_group
]->b_data
;
1099 * We place the buddy block and bitmap block
1102 if ((first_block
+ i
) & 1) {
1103 /* this is block of buddy */
1104 BUG_ON(incore
== NULL
);
1105 mb_debug("put buddy for group %u in page %lu/%x\n",
1106 group
, page
->index
, i
* blocksize
);
1107 memset(data
, 0xff, blocksize
);
1108 grinfo
= ext4_get_group_info(sb
, group
);
1109 grinfo
->bb_fragments
= 0;
1110 memset(grinfo
->bb_counters
, 0,
1111 sizeof(unsigned short)*(sb
->s_blocksize_bits
+2));
1113 * incore got set to the group block bitmap below
1115 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
1118 /* this is block of bitmap */
1119 BUG_ON(incore
!= NULL
);
1120 mb_debug("put bitmap for group %u in page %lu/%x\n",
1121 group
, page
->index
, i
* blocksize
);
1123 /* see comments in ext4_mb_put_pa() */
1124 ext4_lock_group(sb
, group
);
1125 memcpy(data
, bitmap
, blocksize
);
1127 /* mark all preallocated blks used in in-core bitmap */
1128 ext4_mb_generate_from_pa(sb
, data
, group
);
1129 ext4_unlock_group(sb
, group
);
1131 /* set incore so that the buddy information can be
1132 * generated using this
1137 SetPageUptodate(page
);
1141 for (i
= 0; i
< groups_per_page
&& bh
[i
]; i
++)
1149 static int ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1150 struct ext4_buddy
*e4b
)
1152 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1153 struct inode
*inode
= sbi
->s_buddy_cache
;
1154 int blocks_per_page
;
1160 mb_debug("load group %lu\n", group
);
1162 blocks_per_page
= PAGE_CACHE_SIZE
/ sb
->s_blocksize
;
1164 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1165 e4b
->bd_info
= ext4_get_group_info(sb
, group
);
1167 e4b
->bd_group
= group
;
1168 e4b
->bd_buddy_page
= NULL
;
1169 e4b
->bd_bitmap_page
= NULL
;
1172 * the buddy cache inode stores the block bitmap
1173 * and buddy information in consecutive blocks.
1174 * So for each group we need two blocks.
1177 pnum
= block
/ blocks_per_page
;
1178 poff
= block
% blocks_per_page
;
1180 /* we could use find_or_create_page(), but it locks page
1181 * what we'd like to avoid in fast path ... */
1182 page
= find_get_page(inode
->i_mapping
, pnum
);
1183 if (page
== NULL
|| !PageUptodate(page
)) {
1185 page_cache_release(page
);
1186 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1188 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1189 if (!PageUptodate(page
)) {
1190 ext4_mb_init_cache(page
, NULL
);
1191 mb_cmp_bitmaps(e4b
, page_address(page
) +
1192 (poff
* sb
->s_blocksize
));
1197 if (page
== NULL
|| !PageUptodate(page
))
1199 e4b
->bd_bitmap_page
= page
;
1200 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1201 mark_page_accessed(page
);
1204 pnum
= block
/ blocks_per_page
;
1205 poff
= block
% blocks_per_page
;
1207 page
= find_get_page(inode
->i_mapping
, pnum
);
1208 if (page
== NULL
|| !PageUptodate(page
)) {
1210 page_cache_release(page
);
1211 page
= find_or_create_page(inode
->i_mapping
, pnum
, GFP_NOFS
);
1213 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1214 if (!PageUptodate(page
))
1215 ext4_mb_init_cache(page
, e4b
->bd_bitmap
);
1220 if (page
== NULL
|| !PageUptodate(page
))
1222 e4b
->bd_buddy_page
= page
;
1223 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1224 mark_page_accessed(page
);
1226 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1227 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1232 if (e4b
->bd_bitmap_page
)
1233 page_cache_release(e4b
->bd_bitmap_page
);
1234 if (e4b
->bd_buddy_page
)
1235 page_cache_release(e4b
->bd_buddy_page
);
1236 e4b
->bd_buddy
= NULL
;
1237 e4b
->bd_bitmap
= NULL
;
1241 static void ext4_mb_release_desc(struct ext4_buddy
*e4b
)
1243 if (e4b
->bd_bitmap_page
)
1244 page_cache_release(e4b
->bd_bitmap_page
);
1245 if (e4b
->bd_buddy_page
)
1246 page_cache_release(e4b
->bd_buddy_page
);
1250 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1255 BUG_ON(EXT4_MB_BITMAP(e4b
) == EXT4_MB_BUDDY(e4b
));
1256 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1258 bb
= EXT4_MB_BUDDY(e4b
);
1259 while (order
<= e4b
->bd_blkbits
+ 1) {
1261 if (!mb_test_bit(block
, bb
)) {
1262 /* this block is part of buddy of order 'order' */
1265 bb
+= 1 << (e4b
->bd_blkbits
- order
);
1271 static void mb_clear_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1277 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1278 /* fast path: clear whole word at once */
1279 addr
= bm
+ (cur
>> 3);
1284 mb_clear_bit_atomic(lock
, cur
, bm
);
1289 static void mb_set_bits(spinlock_t
*lock
, void *bm
, int cur
, int len
)
1295 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1296 /* fast path: set whole word at once */
1297 addr
= bm
+ (cur
>> 3);
1302 mb_set_bit_atomic(lock
, cur
, bm
);
1307 static int mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1308 int first
, int count
)
1315 struct super_block
*sb
= e4b
->bd_sb
;
1317 BUG_ON(first
+ count
> (sb
->s_blocksize
<< 3));
1318 BUG_ON(!ext4_is_group_locked(sb
, e4b
->bd_group
));
1319 mb_check_buddy(e4b
);
1320 mb_free_blocks_double(inode
, e4b
, first
, count
);
1322 e4b
->bd_info
->bb_free
+= count
;
1323 if (first
< e4b
->bd_info
->bb_first_free
)
1324 e4b
->bd_info
->bb_first_free
= first
;
1326 /* let's maintain fragments counter */
1328 block
= !mb_test_bit(first
- 1, EXT4_MB_BITMAP(e4b
));
1329 if (first
+ count
< EXT4_SB(sb
)->s_mb_maxs
[0])
1330 max
= !mb_test_bit(first
+ count
, EXT4_MB_BITMAP(e4b
));
1332 e4b
->bd_info
->bb_fragments
--;
1333 else if (!block
&& !max
)
1334 e4b
->bd_info
->bb_fragments
++;
1336 /* let's maintain buddy itself */
1337 while (count
-- > 0) {
1341 if (!mb_test_bit(block
, EXT4_MB_BITMAP(e4b
))) {
1342 ext4_fsblk_t blocknr
;
1343 blocknr
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
);
1346 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
1348 ext4_error(sb
, __FUNCTION__
, "double-free of inode"
1349 " %lu's block %llu(bit %u in group %lu)\n",
1350 inode
? inode
->i_ino
: 0, blocknr
, block
,
1353 mb_clear_bit(block
, EXT4_MB_BITMAP(e4b
));
1354 e4b
->bd_info
->bb_counters
[order
]++;
1356 /* start of the buddy */
1357 buddy
= mb_find_buddy(e4b
, order
, &max
);
1361 if (mb_test_bit(block
, buddy
) ||
1362 mb_test_bit(block
+ 1, buddy
))
1365 /* both the buddies are free, try to coalesce them */
1366 buddy2
= mb_find_buddy(e4b
, order
+ 1, &max
);
1372 /* for special purposes, we don't set
1373 * free bits in bitmap */
1374 mb_set_bit(block
, buddy
);
1375 mb_set_bit(block
+ 1, buddy
);
1377 e4b
->bd_info
->bb_counters
[order
]--;
1378 e4b
->bd_info
->bb_counters
[order
]--;
1382 e4b
->bd_info
->bb_counters
[order
]++;
1384 mb_clear_bit(block
, buddy2
);
1388 mb_check_buddy(e4b
);
1393 static int mb_find_extent(struct ext4_buddy
*e4b
, int order
, int block
,
1394 int needed
, struct ext4_free_extent
*ex
)
1401 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1404 buddy
= mb_find_buddy(e4b
, order
, &max
);
1405 BUG_ON(buddy
== NULL
);
1406 BUG_ON(block
>= max
);
1407 if (mb_test_bit(block
, buddy
)) {
1414 /* FIXME dorp order completely ? */
1415 if (likely(order
== 0)) {
1416 /* find actual order */
1417 order
= mb_find_order_for_block(e4b
, block
);
1418 block
= block
>> order
;
1421 ex
->fe_len
= 1 << order
;
1422 ex
->fe_start
= block
<< order
;
1423 ex
->fe_group
= e4b
->bd_group
;
1425 /* calc difference from given start */
1426 next
= next
- ex
->fe_start
;
1428 ex
->fe_start
+= next
;
1430 while (needed
> ex
->fe_len
&&
1431 (buddy
= mb_find_buddy(e4b
, order
, &max
))) {
1433 if (block
+ 1 >= max
)
1436 next
= (block
+ 1) * (1 << order
);
1437 if (mb_test_bit(next
, EXT4_MB_BITMAP(e4b
)))
1440 ord
= mb_find_order_for_block(e4b
, next
);
1443 block
= next
>> order
;
1444 ex
->fe_len
+= 1 << order
;
1447 BUG_ON(ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3)));
1451 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1457 int start
= ex
->fe_start
;
1458 int len
= ex
->fe_len
;
1463 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1464 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1465 BUG_ON(!ext4_is_group_locked(e4b
->bd_sb
, e4b
->bd_group
));
1466 mb_check_buddy(e4b
);
1467 mb_mark_used_double(e4b
, start
, len
);
1469 e4b
->bd_info
->bb_free
-= len
;
1470 if (e4b
->bd_info
->bb_first_free
== start
)
1471 e4b
->bd_info
->bb_first_free
+= len
;
1473 /* let's maintain fragments counter */
1475 mlen
= !mb_test_bit(start
- 1, EXT4_MB_BITMAP(e4b
));
1476 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1477 max
= !mb_test_bit(start
+ len
, EXT4_MB_BITMAP(e4b
));
1479 e4b
->bd_info
->bb_fragments
++;
1480 else if (!mlen
&& !max
)
1481 e4b
->bd_info
->bb_fragments
--;
1483 /* let's maintain buddy itself */
1485 ord
= mb_find_order_for_block(e4b
, start
);
1487 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1488 /* the whole chunk may be allocated at once! */
1490 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1491 BUG_ON((start
>> ord
) >= max
);
1492 mb_set_bit(start
>> ord
, buddy
);
1493 e4b
->bd_info
->bb_counters
[ord
]--;
1500 /* store for history */
1502 ret
= len
| (ord
<< 16);
1504 /* we have to split large buddy */
1506 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1507 mb_set_bit(start
>> ord
, buddy
);
1508 e4b
->bd_info
->bb_counters
[ord
]--;
1511 cur
= (start
>> ord
) & ~1U;
1512 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1513 mb_clear_bit(cur
, buddy
);
1514 mb_clear_bit(cur
+ 1, buddy
);
1515 e4b
->bd_info
->bb_counters
[ord
]++;
1516 e4b
->bd_info
->bb_counters
[ord
]++;
1519 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b
->bd_sb
), ex
->fe_group
),
1520 EXT4_MB_BITMAP(e4b
), ex
->fe_start
, len0
);
1521 mb_check_buddy(e4b
);
1527 * Must be called under group lock!
1529 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1530 struct ext4_buddy
*e4b
)
1532 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1535 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1536 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1538 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1539 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1540 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1542 /* preallocation can change ac_b_ex, thus we store actually
1543 * allocated blocks for history */
1544 ac
->ac_f_ex
= ac
->ac_b_ex
;
1546 ac
->ac_status
= AC_STATUS_FOUND
;
1547 ac
->ac_tail
= ret
& 0xffff;
1548 ac
->ac_buddy
= ret
>> 16;
1550 /* XXXXXXX: SUCH A HORRIBLE **CK */
1552 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1553 get_page(ac
->ac_bitmap_page
);
1554 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1555 get_page(ac
->ac_buddy_page
);
1557 /* store last allocated for subsequent stream allocation */
1558 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1559 spin_lock(&sbi
->s_md_lock
);
1560 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1561 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1562 spin_unlock(&sbi
->s_md_lock
);
1567 * regular allocator, for general purposes allocation
1570 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1571 struct ext4_buddy
*e4b
,
1574 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1575 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1576 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1577 struct ext4_free_extent ex
;
1581 * We don't want to scan for a whole year
1583 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1584 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1585 ac
->ac_status
= AC_STATUS_BREAK
;
1590 * Haven't found good chunk so far, let's continue
1592 if (bex
->fe_len
< gex
->fe_len
)
1595 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1596 && bex
->fe_group
== e4b
->bd_group
) {
1597 /* recheck chunk's availability - we don't know
1598 * when it was found (within this lock-unlock
1600 max
= mb_find_extent(e4b
, 0, bex
->fe_start
, gex
->fe_len
, &ex
);
1601 if (max
>= gex
->fe_len
) {
1602 ext4_mb_use_best_found(ac
, e4b
);
1609 * The routine checks whether found extent is good enough. If it is,
1610 * then the extent gets marked used and flag is set to the context
1611 * to stop scanning. Otherwise, the extent is compared with the
1612 * previous found extent and if new one is better, then it's stored
1613 * in the context. Later, the best found extent will be used, if
1614 * mballoc can't find good enough extent.
1616 * FIXME: real allocation policy is to be designed yet!
1618 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1619 struct ext4_free_extent
*ex
,
1620 struct ext4_buddy
*e4b
)
1622 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1623 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1625 BUG_ON(ex
->fe_len
<= 0);
1626 BUG_ON(ex
->fe_len
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1627 BUG_ON(ex
->fe_start
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
1628 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1633 * The special case - take what you catch first
1635 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1637 ext4_mb_use_best_found(ac
, e4b
);
1642 * Let's check whether the chuck is good enough
1644 if (ex
->fe_len
== gex
->fe_len
) {
1646 ext4_mb_use_best_found(ac
, e4b
);
1651 * If this is first found extent, just store it in the context
1653 if (bex
->fe_len
== 0) {
1659 * If new found extent is better, store it in the context
1661 if (bex
->fe_len
< gex
->fe_len
) {
1662 /* if the request isn't satisfied, any found extent
1663 * larger than previous best one is better */
1664 if (ex
->fe_len
> bex
->fe_len
)
1666 } else if (ex
->fe_len
> gex
->fe_len
) {
1667 /* if the request is satisfied, then we try to find
1668 * an extent that still satisfy the request, but is
1669 * smaller than previous one */
1670 if (ex
->fe_len
< bex
->fe_len
)
1674 ext4_mb_check_limits(ac
, e4b
, 0);
1677 static int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1678 struct ext4_buddy
*e4b
)
1680 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1681 ext4_group_t group
= ex
.fe_group
;
1685 BUG_ON(ex
.fe_len
<= 0);
1686 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1690 ext4_lock_group(ac
->ac_sb
, group
);
1691 max
= mb_find_extent(e4b
, 0, ex
.fe_start
, ex
.fe_len
, &ex
);
1695 ext4_mb_use_best_found(ac
, e4b
);
1698 ext4_unlock_group(ac
->ac_sb
, group
);
1699 ext4_mb_release_desc(e4b
);
1704 static int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1705 struct ext4_buddy
*e4b
)
1707 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1710 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1711 struct ext4_super_block
*es
= sbi
->s_es
;
1712 struct ext4_free_extent ex
;
1714 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1717 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1721 ext4_lock_group(ac
->ac_sb
, group
);
1722 max
= mb_find_extent(e4b
, 0, ac
->ac_g_ex
.fe_start
,
1723 ac
->ac_g_ex
.fe_len
, &ex
);
1725 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1728 start
= (e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
)) +
1729 ex
.fe_start
+ le32_to_cpu(es
->s_first_data_block
);
1730 /* use do_div to get remainder (would be 64-bit modulo) */
1731 if (do_div(start
, sbi
->s_stripe
) == 0) {
1734 ext4_mb_use_best_found(ac
, e4b
);
1736 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1737 BUG_ON(ex
.fe_len
<= 0);
1738 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1739 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1742 ext4_mb_use_best_found(ac
, e4b
);
1743 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1744 /* Sometimes, caller may want to merge even small
1745 * number of blocks to an existing extent */
1746 BUG_ON(ex
.fe_len
<= 0);
1747 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1748 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1751 ext4_mb_use_best_found(ac
, e4b
);
1753 ext4_unlock_group(ac
->ac_sb
, group
);
1754 ext4_mb_release_desc(e4b
);
1760 * The routine scans buddy structures (not bitmap!) from given order
1761 * to max order and tries to find big enough chunk to satisfy the req
1763 static void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1764 struct ext4_buddy
*e4b
)
1766 struct super_block
*sb
= ac
->ac_sb
;
1767 struct ext4_group_info
*grp
= e4b
->bd_info
;
1773 BUG_ON(ac
->ac_2order
<= 0);
1774 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1775 if (grp
->bb_counters
[i
] == 0)
1778 buddy
= mb_find_buddy(e4b
, i
, &max
);
1779 BUG_ON(buddy
== NULL
);
1781 k
= ext4_find_next_zero_bit(buddy
, max
, 0);
1786 ac
->ac_b_ex
.fe_len
= 1 << i
;
1787 ac
->ac_b_ex
.fe_start
= k
<< i
;
1788 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1790 ext4_mb_use_best_found(ac
, e4b
);
1792 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1794 if (EXT4_SB(sb
)->s_mb_stats
)
1795 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1802 * The routine scans the group and measures all found extents.
1803 * In order to optimize scanning, caller must pass number of
1804 * free blocks in the group, so the routine can know upper limit.
1806 static void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1807 struct ext4_buddy
*e4b
)
1809 struct super_block
*sb
= ac
->ac_sb
;
1810 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1811 struct ext4_free_extent ex
;
1815 free
= e4b
->bd_info
->bb_free
;
1818 i
= e4b
->bd_info
->bb_first_free
;
1820 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1821 i
= ext4_find_next_zero_bit(bitmap
,
1822 EXT4_BLOCKS_PER_GROUP(sb
), i
);
1823 if (i
>= EXT4_BLOCKS_PER_GROUP(sb
)) {
1828 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1829 BUG_ON(ex
.fe_len
<= 0);
1830 BUG_ON(free
< ex
.fe_len
);
1832 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1838 ext4_mb_check_limits(ac
, e4b
, 1);
1842 * This is a special case for storages like raid5
1843 * we try to find stripe-aligned chunks for stripe-size requests
1844 * XXX should do so at least for multiples of stripe size as well
1846 static void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1847 struct ext4_buddy
*e4b
)
1849 struct super_block
*sb
= ac
->ac_sb
;
1850 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1851 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1852 struct ext4_free_extent ex
;
1853 ext4_fsblk_t first_group_block
;
1858 BUG_ON(sbi
->s_stripe
== 0);
1860 /* find first stripe-aligned block in group */
1861 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1862 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1863 a
= first_group_block
+ sbi
->s_stripe
- 1;
1864 do_div(a
, sbi
->s_stripe
);
1865 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1867 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1868 if (!mb_test_bit(i
, bitmap
)) {
1869 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1870 if (max
>= sbi
->s_stripe
) {
1873 ext4_mb_use_best_found(ac
, e4b
);
1881 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1882 ext4_group_t group
, int cr
)
1884 unsigned free
, fragments
;
1886 struct ext4_group_desc
*desc
;
1887 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1889 BUG_ON(cr
< 0 || cr
>= 4);
1890 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1892 free
= grp
->bb_free
;
1893 fragments
= grp
->bb_fragments
;
1901 BUG_ON(ac
->ac_2order
== 0);
1902 /* If this group is uninitialized, skip it initially */
1903 desc
= ext4_get_group_desc(ac
->ac_sb
, group
, NULL
);
1904 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))
1907 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1908 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1909 if (grp
->bb_counters
[i
] > 0)
1913 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1917 if (free
>= ac
->ac_g_ex
.fe_len
)
1929 static int ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1936 struct ext4_sb_info
*sbi
;
1937 struct super_block
*sb
;
1938 struct ext4_buddy e4b
;
1943 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1945 /* first, try the goal */
1946 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1947 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1950 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1954 * ac->ac2_order is set only if the fe_len is a power of 2
1955 * if ac2_order is set we also set criteria to 0 so that we
1956 * try exact allocation using buddy.
1958 i
= fls(ac
->ac_g_ex
.fe_len
);
1961 * We search using buddy data only if the order of the request
1962 * is greater than equal to the sbi_s_mb_order2_reqs
1963 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1965 if (i
>= sbi
->s_mb_order2_reqs
) {
1967 * This should tell if fe_len is exactly power of 2
1969 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1970 ac
->ac_2order
= i
- 1;
1973 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1974 /* if stream allocation is enabled, use global goal */
1975 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1976 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1980 if (size
< sbi
->s_mb_stream_request
&&
1981 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1982 /* TBD: may be hot point */
1983 spin_lock(&sbi
->s_md_lock
);
1984 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1985 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1986 spin_unlock(&sbi
->s_md_lock
);
1989 /* searching for the right group start from the goal value specified */
1990 group
= ac
->ac_g_ex
.fe_group
;
1992 /* Let's just scan groups to find more-less suitable blocks */
1993 cr
= ac
->ac_2order
? 0 : 1;
1995 * cr == 0 try to get exact allocation,
1996 * cr == 3 try to get anything
1999 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2000 ac
->ac_criteria
= cr
;
2001 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; group
++, i
++) {
2002 struct ext4_group_info
*grp
;
2003 struct ext4_group_desc
*desc
;
2005 if (group
== EXT4_SB(sb
)->s_groups_count
)
2008 /* quick check to skip empty groups */
2009 grp
= ext4_get_group_info(ac
->ac_sb
, group
);
2010 if (grp
->bb_free
== 0)
2014 * if the group is already init we check whether it is
2015 * a good group and if not we don't load the buddy
2017 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
2019 * we need full data about the group
2020 * to make a good selection
2022 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2025 ext4_mb_release_desc(&e4b
);
2029 * If the particular group doesn't satisfy our
2030 * criteria we continue with the next group
2032 if (!ext4_mb_good_group(ac
, group
, cr
))
2035 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2039 ext4_lock_group(sb
, group
);
2040 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2041 /* someone did allocation from this group */
2042 ext4_unlock_group(sb
, group
);
2043 ext4_mb_release_desc(&e4b
);
2047 ac
->ac_groups_scanned
++;
2048 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2049 if (cr
== 0 || (desc
->bg_flags
&
2050 cpu_to_le16(EXT4_BG_BLOCK_UNINIT
) &&
2051 ac
->ac_2order
!= 0))
2052 ext4_mb_simple_scan_group(ac
, &e4b
);
2054 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2055 ext4_mb_scan_aligned(ac
, &e4b
);
2057 ext4_mb_complex_scan_group(ac
, &e4b
);
2059 ext4_unlock_group(sb
, group
);
2060 ext4_mb_release_desc(&e4b
);
2062 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2067 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2068 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2070 * We've been searching too long. Let's try to allocate
2071 * the best chunk we've found so far
2074 ext4_mb_try_best_found(ac
, &e4b
);
2075 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2077 * Someone more lucky has already allocated it.
2078 * The only thing we can do is just take first
2080 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2082 ac
->ac_b_ex
.fe_group
= 0;
2083 ac
->ac_b_ex
.fe_start
= 0;
2084 ac
->ac_b_ex
.fe_len
= 0;
2085 ac
->ac_status
= AC_STATUS_CONTINUE
;
2086 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2088 atomic_inc(&sbi
->s_mb_lost_chunks
);
2096 #ifdef EXT4_MB_HISTORY
2097 struct ext4_mb_proc_session
{
2098 struct ext4_mb_history
*history
;
2099 struct super_block
*sb
;
2104 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2105 struct ext4_mb_history
*hs
,
2108 if (hs
== s
->history
+ s
->max
)
2110 if (!first
&& hs
== s
->history
+ s
->start
)
2112 while (hs
->orig
.fe_len
== 0) {
2114 if (hs
== s
->history
+ s
->max
)
2116 if (hs
== s
->history
+ s
->start
)
2122 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2124 struct ext4_mb_proc_session
*s
= seq
->private;
2125 struct ext4_mb_history
*hs
;
2129 return SEQ_START_TOKEN
;
2130 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2133 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2137 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2140 struct ext4_mb_proc_session
*s
= seq
->private;
2141 struct ext4_mb_history
*hs
= v
;
2144 if (v
== SEQ_START_TOKEN
)
2145 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2147 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2150 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2152 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2153 struct ext4_mb_history
*hs
= v
;
2155 if (v
== SEQ_START_TOKEN
) {
2156 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2157 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2158 "pid", "inode", "original", "goal", "result", "found",
2159 "grps", "cr", "flags", "merge", "tail", "broken");
2163 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2164 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2165 "%-5u %-5s %-5u %-6u\n";
2166 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
2167 hs
->result
.fe_start
, hs
->result
.fe_len
,
2168 hs
->result
.fe_logical
);
2169 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
2170 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2171 hs
->orig
.fe_logical
);
2172 sprintf(buf3
, "%lu/%d/%u@%u", hs
->goal
.fe_group
,
2173 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2174 hs
->goal
.fe_logical
);
2175 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2176 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2177 hs
->merged
? "M" : "", hs
->tail
,
2178 hs
->buddy
? 1 << hs
->buddy
: 0);
2179 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2180 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2181 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
2182 hs
->result
.fe_start
, hs
->result
.fe_len
,
2183 hs
->result
.fe_logical
);
2184 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
2185 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2186 hs
->orig
.fe_logical
);
2187 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2188 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2189 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
2190 hs
->result
.fe_start
, hs
->result
.fe_len
);
2191 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2192 hs
->pid
, hs
->ino
, buf2
);
2193 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2194 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
2195 hs
->result
.fe_start
, hs
->result
.fe_len
);
2196 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2197 hs
->pid
, hs
->ino
, buf2
);
2202 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2206 static struct seq_operations ext4_mb_seq_history_ops
= {
2207 .start
= ext4_mb_seq_history_start
,
2208 .next
= ext4_mb_seq_history_next
,
2209 .stop
= ext4_mb_seq_history_stop
,
2210 .show
= ext4_mb_seq_history_show
,
2213 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2215 struct super_block
*sb
= PDE(inode
)->data
;
2216 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2217 struct ext4_mb_proc_session
*s
;
2221 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2225 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2226 s
->history
= kmalloc(size
, GFP_KERNEL
);
2227 if (s
->history
== NULL
) {
2232 spin_lock(&sbi
->s_mb_history_lock
);
2233 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2234 s
->max
= sbi
->s_mb_history_max
;
2235 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2236 spin_unlock(&sbi
->s_mb_history_lock
);
2238 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2240 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2250 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2252 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2253 struct ext4_mb_proc_session
*s
= seq
->private;
2256 return seq_release(inode
, file
);
2259 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2260 const char __user
*buffer
,
2261 size_t count
, loff_t
*ppos
)
2263 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2264 struct ext4_mb_proc_session
*s
= seq
->private;
2265 struct super_block
*sb
= s
->sb
;
2269 if (count
>= sizeof(str
)) {
2270 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2271 "mb_history", (int)sizeof(str
));
2275 if (copy_from_user(str
, buffer
, count
))
2278 value
= simple_strtol(str
, NULL
, 0);
2281 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2286 static struct file_operations ext4_mb_seq_history_fops
= {
2287 .owner
= THIS_MODULE
,
2288 .open
= ext4_mb_seq_history_open
,
2290 .write
= ext4_mb_seq_history_write
,
2291 .llseek
= seq_lseek
,
2292 .release
= ext4_mb_seq_history_release
,
2295 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2297 struct super_block
*sb
= seq
->private;
2298 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2301 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2305 return (void *) group
;
2308 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2310 struct super_block
*sb
= seq
->private;
2311 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2315 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2318 return (void *) group
;;
2321 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2323 struct super_block
*sb
= seq
->private;
2324 long group
= (long) v
;
2327 struct ext4_buddy e4b
;
2329 struct ext4_group_info info
;
2330 unsigned short counters
[16];
2335 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2336 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2337 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2338 "group", "free", "frags", "first",
2339 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2340 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2342 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2343 sizeof(struct ext4_group_info
);
2344 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2346 seq_printf(seq
, "#%-5lu: I/O error\n", group
);
2349 ext4_lock_group(sb
, group
);
2350 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2351 ext4_unlock_group(sb
, group
);
2352 ext4_mb_release_desc(&e4b
);
2354 seq_printf(seq
, "#%-5lu: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2355 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2356 for (i
= 0; i
<= 13; i
++)
2357 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2358 sg
.info
.bb_counters
[i
] : 0);
2359 seq_printf(seq
, " ]\n");
2364 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2368 static struct seq_operations ext4_mb_seq_groups_ops
= {
2369 .start
= ext4_mb_seq_groups_start
,
2370 .next
= ext4_mb_seq_groups_next
,
2371 .stop
= ext4_mb_seq_groups_stop
,
2372 .show
= ext4_mb_seq_groups_show
,
2375 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2377 struct super_block
*sb
= PDE(inode
)->data
;
2380 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2382 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2389 static struct file_operations ext4_mb_seq_groups_fops
= {
2390 .owner
= THIS_MODULE
,
2391 .open
= ext4_mb_seq_groups_open
,
2393 .llseek
= seq_lseek
,
2394 .release
= seq_release
,
2397 static void ext4_mb_history_release(struct super_block
*sb
)
2399 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2401 remove_proc_entry("mb_groups", sbi
->s_mb_proc
);
2402 remove_proc_entry("mb_history", sbi
->s_mb_proc
);
2404 kfree(sbi
->s_mb_history
);
2407 static void ext4_mb_history_init(struct super_block
*sb
)
2409 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2412 if (sbi
->s_mb_proc
!= NULL
) {
2413 struct proc_dir_entry
*p
;
2414 p
= create_proc_entry("mb_history", S_IRUGO
, sbi
->s_mb_proc
);
2416 p
->proc_fops
= &ext4_mb_seq_history_fops
;
2419 p
= create_proc_entry("mb_groups", S_IRUGO
, sbi
->s_mb_proc
);
2421 p
->proc_fops
= &ext4_mb_seq_groups_fops
;
2426 sbi
->s_mb_history_max
= 1000;
2427 sbi
->s_mb_history_cur
= 0;
2428 spin_lock_init(&sbi
->s_mb_history_lock
);
2429 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2430 sbi
->s_mb_history
= kmalloc(i
, GFP_KERNEL
);
2431 if (likely(sbi
->s_mb_history
!= NULL
))
2432 memset(sbi
->s_mb_history
, 0, i
);
2433 /* if we can't allocate history, then we simple won't use it */
2436 static void ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2438 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2439 struct ext4_mb_history h
;
2441 if (unlikely(sbi
->s_mb_history
== NULL
))
2444 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2448 h
.pid
= current
->pid
;
2449 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2450 h
.orig
= ac
->ac_o_ex
;
2451 h
.result
= ac
->ac_b_ex
;
2452 h
.flags
= ac
->ac_flags
;
2453 h
.found
= ac
->ac_found
;
2454 h
.groups
= ac
->ac_groups_scanned
;
2455 h
.cr
= ac
->ac_criteria
;
2456 h
.tail
= ac
->ac_tail
;
2457 h
.buddy
= ac
->ac_buddy
;
2459 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2460 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2461 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2463 h
.goal
= ac
->ac_g_ex
;
2464 h
.result
= ac
->ac_f_ex
;
2467 spin_lock(&sbi
->s_mb_history_lock
);
2468 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2469 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2470 sbi
->s_mb_history_cur
= 0;
2471 spin_unlock(&sbi
->s_mb_history_lock
);
2475 #define ext4_mb_history_release(sb)
2476 #define ext4_mb_history_init(sb)
2479 static int ext4_mb_init_backend(struct super_block
*sb
)
2482 int j
, len
, metalen
;
2483 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2484 int num_meta_group_infos
=
2485 (sbi
->s_groups_count
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2486 EXT4_DESC_PER_BLOCK_BITS(sb
);
2487 struct ext4_group_info
**meta_group_info
;
2489 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2490 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2491 * So a two level scheme suffices for now. */
2492 sbi
->s_group_info
= kmalloc(sizeof(*sbi
->s_group_info
) *
2493 num_meta_group_infos
, GFP_KERNEL
);
2494 if (sbi
->s_group_info
== NULL
) {
2495 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2498 sbi
->s_buddy_cache
= new_inode(sb
);
2499 if (sbi
->s_buddy_cache
== NULL
) {
2500 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2503 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2505 metalen
= sizeof(*meta_group_info
) << EXT4_DESC_PER_BLOCK_BITS(sb
);
2506 for (i
= 0; i
< num_meta_group_infos
; i
++) {
2507 if ((i
+ 1) == num_meta_group_infos
)
2508 metalen
= sizeof(*meta_group_info
) *
2509 (sbi
->s_groups_count
-
2510 (i
<< EXT4_DESC_PER_BLOCK_BITS(sb
)));
2511 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2512 if (meta_group_info
== NULL
) {
2513 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2517 sbi
->s_group_info
[i
] = meta_group_info
;
2521 * calculate needed size. if change bb_counters size,
2522 * don't forget about ext4_mb_generate_buddy()
2524 len
= sizeof(struct ext4_group_info
);
2525 len
+= sizeof(unsigned short) * (sb
->s_blocksize_bits
+ 2);
2526 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2527 struct ext4_group_desc
*desc
;
2530 sbi
->s_group_info
[i
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2531 j
= i
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2533 meta_group_info
[j
] = kzalloc(len
, GFP_KERNEL
);
2534 if (meta_group_info
[j
] == NULL
) {
2535 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2539 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2542 "EXT4-fs: can't read descriptor %lu\n", i
);
2545 memset(meta_group_info
[j
], 0, len
);
2546 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2547 &(meta_group_info
[j
]->bb_state
));
2550 * initialize bb_free to be able to skip
2551 * empty groups without initialization
2553 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2554 meta_group_info
[j
]->bb_free
=
2555 ext4_free_blocks_after_init(sb
, i
, desc
);
2557 meta_group_info
[j
]->bb_free
=
2558 le16_to_cpu(desc
->bg_free_blocks_count
);
2561 INIT_LIST_HEAD(&meta_group_info
[j
]->bb_prealloc_list
);
2565 struct buffer_head
*bh
;
2566 meta_group_info
[j
]->bb_bitmap
=
2567 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2568 BUG_ON(meta_group_info
[j
]->bb_bitmap
== NULL
);
2569 bh
= read_block_bitmap(sb
, i
);
2571 memcpy(meta_group_info
[j
]->bb_bitmap
, bh
->b_data
,
2583 kfree(ext4_get_group_info(sb
, i
));
2586 i
= num_meta_group_infos
;
2589 kfree(sbi
->s_group_info
[i
]);
2590 iput(sbi
->s_buddy_cache
);
2592 kfree(sbi
->s_group_info
);
2596 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2598 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2603 if (!test_opt(sb
, MBALLOC
))
2606 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2608 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2609 if (sbi
->s_mb_offsets
== NULL
) {
2610 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2613 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2614 if (sbi
->s_mb_maxs
== NULL
) {
2615 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2616 kfree(sbi
->s_mb_maxs
);
2620 /* order 0 is regular bitmap */
2621 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2622 sbi
->s_mb_offsets
[0] = 0;
2626 max
= sb
->s_blocksize
<< 2;
2628 sbi
->s_mb_offsets
[i
] = offset
;
2629 sbi
->s_mb_maxs
[i
] = max
;
2630 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2633 } while (i
<= sb
->s_blocksize_bits
+ 1);
2635 /* init file for buddy data */
2636 i
= ext4_mb_init_backend(sb
);
2638 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2639 kfree(sbi
->s_mb_offsets
);
2640 kfree(sbi
->s_mb_maxs
);
2644 spin_lock_init(&sbi
->s_md_lock
);
2645 INIT_LIST_HEAD(&sbi
->s_active_transaction
);
2646 INIT_LIST_HEAD(&sbi
->s_closed_transaction
);
2647 INIT_LIST_HEAD(&sbi
->s_committed_transaction
);
2648 spin_lock_init(&sbi
->s_bal_lock
);
2650 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2651 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2652 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2653 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2654 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2655 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2656 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2658 i
= sizeof(struct ext4_locality_group
) * NR_CPUS
;
2659 sbi
->s_locality_groups
= kmalloc(i
, GFP_KERNEL
);
2660 if (sbi
->s_locality_groups
== NULL
) {
2661 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2662 kfree(sbi
->s_mb_offsets
);
2663 kfree(sbi
->s_mb_maxs
);
2666 for (i
= 0; i
< NR_CPUS
; i
++) {
2667 struct ext4_locality_group
*lg
;
2668 lg
= &sbi
->s_locality_groups
[i
];
2669 mutex_init(&lg
->lg_mutex
);
2670 INIT_LIST_HEAD(&lg
->lg_prealloc_list
);
2671 spin_lock_init(&lg
->lg_prealloc_lock
);
2674 ext4_mb_init_per_dev_proc(sb
);
2675 ext4_mb_history_init(sb
);
2677 printk("EXT4-fs: mballoc enabled\n");
2681 /* need to called with ext4 group lock (ext4_lock_group) */
2682 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2684 struct ext4_prealloc_space
*pa
;
2685 struct list_head
*cur
, *tmp
;
2688 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2689 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2690 list_del(&pa
->pa_group_list
);
2695 mb_debug("mballoc: %u PAs left\n", count
);
2699 int ext4_mb_release(struct super_block
*sb
)
2702 int num_meta_group_infos
;
2703 struct ext4_group_info
*grinfo
;
2704 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2706 if (!test_opt(sb
, MBALLOC
))
2709 /* release freed, non-committed blocks */
2710 spin_lock(&sbi
->s_md_lock
);
2711 list_splice_init(&sbi
->s_closed_transaction
,
2712 &sbi
->s_committed_transaction
);
2713 list_splice_init(&sbi
->s_active_transaction
,
2714 &sbi
->s_committed_transaction
);
2715 spin_unlock(&sbi
->s_md_lock
);
2716 ext4_mb_free_committed_blocks(sb
);
2718 if (sbi
->s_group_info
) {
2719 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2720 grinfo
= ext4_get_group_info(sb
, i
);
2722 kfree(grinfo
->bb_bitmap
);
2724 ext4_lock_group(sb
, i
);
2725 ext4_mb_cleanup_pa(grinfo
);
2726 ext4_unlock_group(sb
, i
);
2729 num_meta_group_infos
= (sbi
->s_groups_count
+
2730 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2731 EXT4_DESC_PER_BLOCK_BITS(sb
);
2732 for (i
= 0; i
< num_meta_group_infos
; i
++)
2733 kfree(sbi
->s_group_info
[i
]);
2734 kfree(sbi
->s_group_info
);
2736 kfree(sbi
->s_mb_offsets
);
2737 kfree(sbi
->s_mb_maxs
);
2738 if (sbi
->s_buddy_cache
)
2739 iput(sbi
->s_buddy_cache
);
2740 if (sbi
->s_mb_stats
) {
2742 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2743 atomic_read(&sbi
->s_bal_allocated
),
2744 atomic_read(&sbi
->s_bal_reqs
),
2745 atomic_read(&sbi
->s_bal_success
));
2747 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2748 "%u 2^N hits, %u breaks, %u lost\n",
2749 atomic_read(&sbi
->s_bal_ex_scanned
),
2750 atomic_read(&sbi
->s_bal_goals
),
2751 atomic_read(&sbi
->s_bal_2orders
),
2752 atomic_read(&sbi
->s_bal_breaks
),
2753 atomic_read(&sbi
->s_mb_lost_chunks
));
2755 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2756 sbi
->s_mb_buddies_generated
++,
2757 sbi
->s_mb_generation_time
);
2759 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2760 atomic_read(&sbi
->s_mb_preallocated
),
2761 atomic_read(&sbi
->s_mb_discarded
));
2764 kfree(sbi
->s_locality_groups
);
2766 ext4_mb_history_release(sb
);
2767 ext4_mb_destroy_per_dev_proc(sb
);
2772 static void ext4_mb_free_committed_blocks(struct super_block
*sb
)
2774 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2779 struct ext4_free_metadata
*md
;
2780 struct ext4_buddy e4b
;
2782 if (list_empty(&sbi
->s_committed_transaction
))
2785 /* there is committed blocks to be freed yet */
2787 /* get next array of blocks */
2789 spin_lock(&sbi
->s_md_lock
);
2790 if (!list_empty(&sbi
->s_committed_transaction
)) {
2791 md
= list_entry(sbi
->s_committed_transaction
.next
,
2792 struct ext4_free_metadata
, list
);
2793 list_del(&md
->list
);
2795 spin_unlock(&sbi
->s_md_lock
);
2800 mb_debug("gonna free %u blocks in group %lu (0x%p):",
2801 md
->num
, md
->group
, md
);
2803 err
= ext4_mb_load_buddy(sb
, md
->group
, &e4b
);
2804 /* we expect to find existing buddy because it's pinned */
2807 /* there are blocks to put in buddy to make them really free */
2810 ext4_lock_group(sb
, md
->group
);
2811 for (i
= 0; i
< md
->num
; i
++) {
2812 mb_debug(" %u", md
->blocks
[i
]);
2813 err
= mb_free_blocks(NULL
, &e4b
, md
->blocks
[i
], 1);
2817 ext4_unlock_group(sb
, md
->group
);
2819 /* balance refcounts from ext4_mb_free_metadata() */
2820 page_cache_release(e4b
.bd_buddy_page
);
2821 page_cache_release(e4b
.bd_bitmap_page
);
2824 ext4_mb_release_desc(&e4b
);
2828 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2831 #define EXT4_ROOT "ext4"
2832 #define EXT4_MB_STATS_NAME "stats"
2833 #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2834 #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2835 #define EXT4_MB_ORDER2_REQ "order2_req"
2836 #define EXT4_MB_STREAM_REQ "stream_req"
2837 #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2841 #define MB_PROC_VALUE_READ(name) \
2842 static int ext4_mb_read_##name(char *page, char **start, \
2843 off_t off, int count, int *eof, void *data) \
2845 struct ext4_sb_info *sbi = data; \
2850 len = sprintf(page, "%ld\n", sbi->s_mb_##name); \
2855 #define MB_PROC_VALUE_WRITE(name) \
2856 static int ext4_mb_write_##name(struct file *file, \
2857 const char __user *buf, unsigned long cnt, void *data) \
2859 struct ext4_sb_info *sbi = data; \
2862 if (cnt >= sizeof(str)) \
2864 if (copy_from_user(str, buf, cnt)) \
2866 value = simple_strtol(str, NULL, 0); \
2869 sbi->s_mb_##name = value; \
2873 MB_PROC_VALUE_READ(stats
);
2874 MB_PROC_VALUE_WRITE(stats
);
2875 MB_PROC_VALUE_READ(max_to_scan
);
2876 MB_PROC_VALUE_WRITE(max_to_scan
);
2877 MB_PROC_VALUE_READ(min_to_scan
);
2878 MB_PROC_VALUE_WRITE(min_to_scan
);
2879 MB_PROC_VALUE_READ(order2_reqs
);
2880 MB_PROC_VALUE_WRITE(order2_reqs
);
2881 MB_PROC_VALUE_READ(stream_request
);
2882 MB_PROC_VALUE_WRITE(stream_request
);
2883 MB_PROC_VALUE_READ(group_prealloc
);
2884 MB_PROC_VALUE_WRITE(group_prealloc
);
2886 #define MB_PROC_HANDLER(name, var) \
2888 proc = create_proc_entry(name, mode, sbi->s_mb_proc); \
2889 if (proc == NULL) { \
2890 printk(KERN_ERR "EXT4-fs: can't to create %s\n", name); \
2894 proc->read_proc = ext4_mb_read_##var ; \
2895 proc->write_proc = ext4_mb_write_##var; \
2898 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
)
2900 mode_t mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2901 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2902 struct proc_dir_entry
*proc
;
2905 snprintf(devname
, sizeof(devname
) - 1, "%s",
2906 bdevname(sb
->s_bdev
, devname
));
2907 sbi
->s_mb_proc
= proc_mkdir(devname
, proc_root_ext4
);
2909 MB_PROC_HANDLER(EXT4_MB_STATS_NAME
, stats
);
2910 MB_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME
, max_to_scan
);
2911 MB_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME
, min_to_scan
);
2912 MB_PROC_HANDLER(EXT4_MB_ORDER2_REQ
, order2_reqs
);
2913 MB_PROC_HANDLER(EXT4_MB_STREAM_REQ
, stream_request
);
2914 MB_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC
, group_prealloc
);
2919 printk(KERN_ERR
"EXT4-fs: Unable to create %s\n", devname
);
2920 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2921 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2922 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2923 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2924 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2925 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2926 remove_proc_entry(devname
, proc_root_ext4
);
2927 sbi
->s_mb_proc
= NULL
;
2932 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
)
2934 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2937 if (sbi
->s_mb_proc
== NULL
)
2940 snprintf(devname
, sizeof(devname
) - 1, "%s",
2941 bdevname(sb
->s_bdev
, devname
));
2942 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2943 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2944 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2945 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2946 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2947 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2948 remove_proc_entry(devname
, proc_root_ext4
);
2953 int __init
init_ext4_mballoc(void)
2955 ext4_pspace_cachep
=
2956 kmem_cache_create("ext4_prealloc_space",
2957 sizeof(struct ext4_prealloc_space
),
2958 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2959 if (ext4_pspace_cachep
== NULL
)
2962 #ifdef CONFIG_PROC_FS
2963 proc_root_ext4
= proc_mkdir(EXT4_ROOT
, proc_root_fs
);
2964 if (proc_root_ext4
== NULL
)
2965 printk(KERN_ERR
"EXT4-fs: Unable to create %s\n", EXT4_ROOT
);
2971 void exit_ext4_mballoc(void)
2973 /* XXX: synchronize_rcu(); */
2974 kmem_cache_destroy(ext4_pspace_cachep
);
2975 #ifdef CONFIG_PROC_FS
2976 remove_proc_entry(EXT4_ROOT
, proc_root_fs
);
2982 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2983 * Returns 0 if success or error code
2985 static int ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2988 struct buffer_head
*bitmap_bh
= NULL
;
2989 struct ext4_super_block
*es
;
2990 struct ext4_group_desc
*gdp
;
2991 struct buffer_head
*gdp_bh
;
2992 struct ext4_sb_info
*sbi
;
2993 struct super_block
*sb
;
2997 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2998 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
3004 ext4_debug("using block group %lu(%d)\n", ac
->ac_b_ex
.fe_group
,
3005 gdp
->bg_free_blocks_count
);
3008 bitmap_bh
= read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
3012 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
3017 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
3021 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
3025 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
3026 + ac
->ac_b_ex
.fe_start
3027 + le32_to_cpu(es
->s_first_data_block
);
3029 if (block
== ext4_block_bitmap(sb
, gdp
) ||
3030 block
== ext4_inode_bitmap(sb
, gdp
) ||
3031 in_range(block
, ext4_inode_table(sb
, gdp
),
3032 EXT4_SB(sb
)->s_itb_per_group
)) {
3034 ext4_error(sb
, __FUNCTION__
,
3035 "Allocating block in system zone - block = %llu",
3038 #ifdef AGGRESSIVE_CHECK
3041 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
3042 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
3043 bitmap_bh
->b_data
));
3047 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
), bitmap_bh
->b_data
,
3048 ac
->ac_b_ex
.fe_start
, ac
->ac_b_ex
.fe_len
);
3050 spin_lock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3051 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
3052 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
3053 gdp
->bg_free_blocks_count
=
3054 cpu_to_le16(ext4_free_blocks_after_init(sb
,
3055 ac
->ac_b_ex
.fe_group
,
3058 gdp
->bg_free_blocks_count
=
3059 cpu_to_le16(le16_to_cpu(gdp
->bg_free_blocks_count
)
3060 - ac
->ac_b_ex
.fe_len
);
3061 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
3062 spin_unlock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3063 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
3065 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
3068 err
= ext4_journal_dirty_metadata(handle
, gdp_bh
);
3077 * here we normalize request for locality group
3078 * Group request are normalized to s_strip size if we set the same via mount
3079 * option. If not we set it to s_mb_group_prealloc which can be configured via
3080 * /proc/fs/ext4/<partition>/group_prealloc
3082 * XXX: should we try to preallocate more than the group has now?
3084 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3086 struct super_block
*sb
= ac
->ac_sb
;
3087 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3090 if (EXT4_SB(sb
)->s_stripe
)
3091 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3093 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3094 mb_debug("#%u: goal %lu blocks for locality group\n",
3095 current
->pid
, ac
->ac_g_ex
.fe_len
);
3099 * Normalization means making request better in terms of
3100 * size and alignment
3102 static void ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3103 struct ext4_allocation_request
*ar
)
3107 struct list_head
*cur
;
3108 loff_t size
, orig_size
, start_off
;
3109 ext4_lblk_t start
, orig_start
;
3110 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3112 /* do normalize only data requests, metadata requests
3113 do not need preallocation */
3114 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3117 /* sometime caller may want exact blocks */
3118 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3121 /* caller may indicate that preallocation isn't
3122 * required (it's a tail, for example) */
3123 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3126 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3127 ext4_mb_normalize_group_request(ac
);
3131 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3133 /* first, let's learn actual file size
3134 * given current request is allocated */
3135 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3136 size
= size
<< bsbits
;
3137 if (size
< i_size_read(ac
->ac_inode
))
3138 size
= i_size_read(ac
->ac_inode
);
3140 /* max available blocks in a free group */
3141 max
= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) - 1 - 1 -
3142 EXT4_SB(ac
->ac_sb
)->s_itb_per_group
;
3144 #define NRL_CHECK_SIZE(req, size, max,bits) \
3145 (req <= (size) || max <= ((size) >> bits))
3147 /* first, try to predict filesize */
3148 /* XXX: should this table be tunable? */
3150 if (size
<= 16 * 1024) {
3152 } else if (size
<= 32 * 1024) {
3154 } else if (size
<= 64 * 1024) {
3156 } else if (size
<= 128 * 1024) {
3158 } else if (size
<= 256 * 1024) {
3160 } else if (size
<= 512 * 1024) {
3162 } else if (size
<= 1024 * 1024) {
3164 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, bsbits
)) {
3165 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3166 (20 - bsbits
)) << 20;
3168 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, bsbits
)) {
3169 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3170 (22 - bsbits
)) << 22;
3171 size
= 4 * 1024 * 1024;
3172 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3173 (8<<20)>>bsbits
, max
, bsbits
)) {
3174 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3175 (23 - bsbits
)) << 23;
3176 size
= 8 * 1024 * 1024;
3178 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3179 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3181 orig_size
= size
= size
>> bsbits
;
3182 orig_start
= start
= start_off
>> bsbits
;
3184 /* don't cover already allocated blocks in selected range */
3185 if (ar
->pleft
&& start
<= ar
->lleft
) {
3186 size
-= ar
->lleft
+ 1 - start
;
3187 start
= ar
->lleft
+ 1;
3189 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3190 size
-= start
+ size
- ar
->lright
;
3194 /* check we don't cross already preallocated blocks */
3196 list_for_each_rcu(cur
, &ei
->i_prealloc_list
) {
3197 struct ext4_prealloc_space
*pa
;
3198 unsigned long pa_end
;
3200 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3204 spin_lock(&pa
->pa_lock
);
3205 if (pa
->pa_deleted
) {
3206 spin_unlock(&pa
->pa_lock
);
3210 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3212 /* PA must not overlap original request */
3213 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3214 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3216 /* skip PA normalized request doesn't overlap with */
3217 if (pa
->pa_lstart
>= end
) {
3218 spin_unlock(&pa
->pa_lock
);
3221 if (pa_end
<= start
) {
3222 spin_unlock(&pa
->pa_lock
);
3225 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3227 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3228 BUG_ON(pa_end
< start
);
3232 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3233 BUG_ON(pa
->pa_lstart
> end
);
3234 end
= pa
->pa_lstart
;
3236 spin_unlock(&pa
->pa_lock
);
3241 /* XXX: extra loop to check we really don't overlap preallocations */
3243 list_for_each_rcu(cur
, &ei
->i_prealloc_list
) {
3244 struct ext4_prealloc_space
*pa
;
3245 unsigned long pa_end
;
3246 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3247 spin_lock(&pa
->pa_lock
);
3248 if (pa
->pa_deleted
== 0) {
3249 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3250 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3252 spin_unlock(&pa
->pa_lock
);
3256 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3257 start
> ac
->ac_o_ex
.fe_logical
) {
3258 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3259 (unsigned long) start
, (unsigned long) size
,
3260 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3262 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3263 start
> ac
->ac_o_ex
.fe_logical
);
3264 BUG_ON(size
<= 0 || size
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3266 /* now prepare goal request */
3268 /* XXX: is it better to align blocks WRT to logical
3269 * placement or satisfy big request as is */
3270 ac
->ac_g_ex
.fe_logical
= start
;
3271 ac
->ac_g_ex
.fe_len
= size
;
3273 /* define goal start in order to merge */
3274 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3275 /* merge to the right */
3276 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3277 &ac
->ac_f_ex
.fe_group
,
3278 &ac
->ac_f_ex
.fe_start
);
3279 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3281 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3282 /* merge to the left */
3283 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3284 &ac
->ac_f_ex
.fe_group
,
3285 &ac
->ac_f_ex
.fe_start
);
3286 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3289 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3290 (unsigned) orig_size
, (unsigned) start
);
3293 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3295 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3297 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3298 atomic_inc(&sbi
->s_bal_reqs
);
3299 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3300 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3301 atomic_inc(&sbi
->s_bal_success
);
3302 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3303 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3304 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3305 atomic_inc(&sbi
->s_bal_goals
);
3306 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3307 atomic_inc(&sbi
->s_bal_breaks
);
3310 ext4_mb_store_history(ac
);
3314 * use blocks preallocated to inode
3316 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3317 struct ext4_prealloc_space
*pa
)
3323 /* found preallocated blocks, use them */
3324 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3325 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3327 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3328 &ac
->ac_b_ex
.fe_start
);
3329 ac
->ac_b_ex
.fe_len
= len
;
3330 ac
->ac_status
= AC_STATUS_FOUND
;
3333 BUG_ON(start
< pa
->pa_pstart
);
3334 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3335 BUG_ON(pa
->pa_free
< len
);
3338 mb_debug("use %llu/%lu from inode pa %p\n", start
, len
, pa
);
3342 * use blocks preallocated to locality group
3344 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3345 struct ext4_prealloc_space
*pa
)
3347 unsigned len
= ac
->ac_o_ex
.fe_len
;
3349 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3350 &ac
->ac_b_ex
.fe_group
,
3351 &ac
->ac_b_ex
.fe_start
);
3352 ac
->ac_b_ex
.fe_len
= len
;
3353 ac
->ac_status
= AC_STATUS_FOUND
;
3356 /* we don't correct pa_pstart or pa_plen here to avoid
3357 * possible race when tte group is being loaded concurrently
3358 * instead we correct pa later, after blocks are marked
3359 * in on-disk bitmap -- see ext4_mb_release_context() */
3361 * FIXME!! but the other CPUs can look at this particular
3362 * pa and think that it have enought free blocks if we
3363 * don't update pa_free here right ?
3365 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3369 * search goal blocks in preallocated space
3371 static int ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3373 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3374 struct ext4_locality_group
*lg
;
3375 struct ext4_prealloc_space
*pa
;
3376 struct list_head
*cur
;
3378 /* only data can be preallocated */
3379 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3382 /* first, try per-file preallocation */
3384 list_for_each_rcu(cur
, &ei
->i_prealloc_list
) {
3385 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3387 /* all fields in this condition don't change,
3388 * so we can skip locking for them */
3389 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3390 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3393 /* found preallocated blocks, use them */
3394 spin_lock(&pa
->pa_lock
);
3395 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3396 atomic_inc(&pa
->pa_count
);
3397 ext4_mb_use_inode_pa(ac
, pa
);
3398 spin_unlock(&pa
->pa_lock
);
3399 ac
->ac_criteria
= 10;
3403 spin_unlock(&pa
->pa_lock
);
3407 /* can we use group allocation? */
3408 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3411 /* inode may have no locality group for some reason */
3417 list_for_each_rcu(cur
, &lg
->lg_prealloc_list
) {
3418 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3419 spin_lock(&pa
->pa_lock
);
3420 if (pa
->pa_deleted
== 0 && pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3421 atomic_inc(&pa
->pa_count
);
3422 ext4_mb_use_group_pa(ac
, pa
);
3423 spin_unlock(&pa
->pa_lock
);
3424 ac
->ac_criteria
= 20;
3428 spin_unlock(&pa
->pa_lock
);
3436 * the function goes through all preallocation in this group and marks them
3437 * used in in-core bitmap. buddy must be generated from this bitmap
3438 * Need to be called with ext4 group lock (ext4_lock_group)
3440 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3443 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3444 struct ext4_prealloc_space
*pa
;
3445 struct list_head
*cur
;
3446 ext4_group_t groupnr
;
3447 ext4_grpblk_t start
;
3448 int preallocated
= 0;
3452 /* all form of preallocation discards first load group,
3453 * so the only competing code is preallocation use.
3454 * we don't need any locking here
3455 * notice we do NOT ignore preallocations with pa_deleted
3456 * otherwise we could leave used blocks available for
3457 * allocation in buddy when concurrent ext4_mb_put_pa()
3458 * is dropping preallocation
3460 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3461 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3462 spin_lock(&pa
->pa_lock
);
3463 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3466 spin_unlock(&pa
->pa_lock
);
3467 if (unlikely(len
== 0))
3469 BUG_ON(groupnr
!= group
);
3470 mb_set_bits(sb_bgl_lock(EXT4_SB(sb
), group
),
3471 bitmap
, start
, len
);
3472 preallocated
+= len
;
3475 mb_debug("prellocated %u for group %lu\n", preallocated
, group
);
3478 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3480 struct ext4_prealloc_space
*pa
;
3481 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3482 kmem_cache_free(ext4_pspace_cachep
, pa
);
3486 * drops a reference to preallocated space descriptor
3487 * if this was the last reference and the space is consumed
3489 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3490 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3494 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3497 /* in this short window concurrent discard can set pa_deleted */
3498 spin_lock(&pa
->pa_lock
);
3499 if (pa
->pa_deleted
== 1) {
3500 spin_unlock(&pa
->pa_lock
);
3505 spin_unlock(&pa
->pa_lock
);
3507 /* -1 is to protect from crossing allocation group */
3508 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
- 1, &grp
, NULL
);
3513 * P1 (buddy init) P2 (regular allocation)
3514 * find block B in PA
3515 * copy on-disk bitmap to buddy
3516 * mark B in on-disk bitmap
3517 * drop PA from group
3518 * mark all PAs in buddy
3520 * thus, P1 initializes buddy with B available. to prevent this
3521 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3524 ext4_lock_group(sb
, grp
);
3525 list_del(&pa
->pa_group_list
);
3526 ext4_unlock_group(sb
, grp
);
3528 spin_lock(pa
->pa_obj_lock
);
3529 list_del_rcu(&pa
->pa_inode_list
);
3530 spin_unlock(pa
->pa_obj_lock
);
3532 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3536 * creates new preallocated space for given inode
3538 static int ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3540 struct super_block
*sb
= ac
->ac_sb
;
3541 struct ext4_prealloc_space
*pa
;
3542 struct ext4_group_info
*grp
;
3543 struct ext4_inode_info
*ei
;
3545 /* preallocate only when found space is larger then requested */
3546 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3547 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3548 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3550 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3554 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3560 /* we can't allocate as much as normalizer wants.
3561 * so, found space must get proper lstart
3562 * to cover original request */
3563 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3564 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3566 /* we're limited by original request in that
3567 * logical block must be covered any way
3568 * winl is window we can move our chunk within */
3569 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3571 /* also, we should cover whole original request */
3572 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3574 /* the smallest one defines real window */
3575 win
= min(winl
, wins
);
3577 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3578 if (offs
&& offs
< win
)
3581 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3582 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3583 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3586 /* preallocation can change ac_b_ex, thus we store actually
3587 * allocated blocks for history */
3588 ac
->ac_f_ex
= ac
->ac_b_ex
;
3590 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3591 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3592 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3593 pa
->pa_free
= pa
->pa_len
;
3594 atomic_set(&pa
->pa_count
, 1);
3595 spin_lock_init(&pa
->pa_lock
);
3599 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3600 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3602 ext4_mb_use_inode_pa(ac
, pa
);
3603 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3605 ei
= EXT4_I(ac
->ac_inode
);
3606 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3608 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3609 pa
->pa_inode
= ac
->ac_inode
;
3611 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3612 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3613 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3615 spin_lock(pa
->pa_obj_lock
);
3616 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3617 spin_unlock(pa
->pa_obj_lock
);
3623 * creates new preallocated space for locality group inodes belongs to
3625 static int ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3627 struct super_block
*sb
= ac
->ac_sb
;
3628 struct ext4_locality_group
*lg
;
3629 struct ext4_prealloc_space
*pa
;
3630 struct ext4_group_info
*grp
;
3632 /* preallocate only when found space is larger then requested */
3633 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3634 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3635 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3637 BUG_ON(ext4_pspace_cachep
== NULL
);
3638 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3642 /* preallocation can change ac_b_ex, thus we store actually
3643 * allocated blocks for history */
3644 ac
->ac_f_ex
= ac
->ac_b_ex
;
3646 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3647 pa
->pa_lstart
= pa
->pa_pstart
;
3648 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3649 pa
->pa_free
= pa
->pa_len
;
3650 atomic_set(&pa
->pa_count
, 1);
3651 spin_lock_init(&pa
->pa_lock
);
3655 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3656 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3658 ext4_mb_use_group_pa(ac
, pa
);
3659 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3661 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3665 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3666 pa
->pa_inode
= NULL
;
3668 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3669 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3670 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3672 spin_lock(pa
->pa_obj_lock
);
3673 list_add_tail_rcu(&pa
->pa_inode_list
, &lg
->lg_prealloc_list
);
3674 spin_unlock(pa
->pa_obj_lock
);
3679 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3683 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3684 err
= ext4_mb_new_group_pa(ac
);
3686 err
= ext4_mb_new_inode_pa(ac
);
3691 * finds all unused blocks in on-disk bitmap, frees them in
3692 * in-core bitmap and buddy.
3693 * @pa must be unlinked from inode and group lists, so that
3694 * nobody else can find/use it.
3695 * the caller MUST hold group/inode locks.
3696 * TODO: optimize the case when there are no in-core structures yet
3698 static int ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
,
3699 struct buffer_head
*bitmap_bh
,
3700 struct ext4_prealloc_space
*pa
)
3702 struct ext4_allocation_context ac
;
3703 struct super_block
*sb
= e4b
->bd_sb
;
3704 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3713 BUG_ON(pa
->pa_deleted
== 0);
3714 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3715 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3716 end
= bit
+ pa
->pa_len
;
3719 ac
.ac_inode
= pa
->pa_inode
;
3720 ac
.ac_op
= EXT4_MB_HISTORY_DISCARD
;
3723 bit
= ext4_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3726 next
= ext4_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3729 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3730 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3731 mb_debug(" free preallocated %u/%u in group %u\n",
3732 (unsigned) start
, (unsigned) next
- bit
,
3736 ac
.ac_b_ex
.fe_group
= group
;
3737 ac
.ac_b_ex
.fe_start
= bit
;
3738 ac
.ac_b_ex
.fe_len
= next
- bit
;
3739 ac
.ac_b_ex
.fe_logical
= 0;
3740 ext4_mb_store_history(&ac
);
3742 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3745 if (free
!= pa
->pa_free
) {
3746 printk(KERN_ERR
"pa %p: logic %lu, phys. %lu, len %lu\n",
3747 pa
, (unsigned long) pa
->pa_lstart
,
3748 (unsigned long) pa
->pa_pstart
,
3749 (unsigned long) pa
->pa_len
);
3750 printk(KERN_ERR
"free %u, pa_free %u\n", free
, pa
->pa_free
);
3752 BUG_ON(free
!= pa
->pa_free
);
3753 atomic_add(free
, &sbi
->s_mb_discarded
);
3758 static int ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3759 struct ext4_prealloc_space
*pa
)
3761 struct ext4_allocation_context ac
;
3762 struct super_block
*sb
= e4b
->bd_sb
;
3766 ac
.ac_op
= EXT4_MB_HISTORY_DISCARD
;
3768 BUG_ON(pa
->pa_deleted
== 0);
3769 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3770 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3771 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3772 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3776 ac
.ac_b_ex
.fe_group
= group
;
3777 ac
.ac_b_ex
.fe_start
= bit
;
3778 ac
.ac_b_ex
.fe_len
= pa
->pa_len
;
3779 ac
.ac_b_ex
.fe_logical
= 0;
3780 ext4_mb_store_history(&ac
);
3786 * releases all preallocations in given group
3788 * first, we need to decide discard policy:
3789 * - when do we discard
3791 * - how many do we discard
3792 * 1) how many requested
3794 static int ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3795 ext4_group_t group
, int needed
)
3797 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3798 struct buffer_head
*bitmap_bh
= NULL
;
3799 struct ext4_prealloc_space
*pa
, *tmp
;
3800 struct list_head list
;
3801 struct ext4_buddy e4b
;
3806 mb_debug("discard preallocation for group %lu\n", group
);
3808 if (list_empty(&grp
->bb_prealloc_list
))
3811 bitmap_bh
= read_block_bitmap(sb
, group
);
3812 if (bitmap_bh
== NULL
) {
3813 /* error handling here */
3814 ext4_mb_release_desc(&e4b
);
3815 BUG_ON(bitmap_bh
== NULL
);
3818 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3819 BUG_ON(err
!= 0); /* error handling here */
3822 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3824 grp
= ext4_get_group_info(sb
, group
);
3825 INIT_LIST_HEAD(&list
);
3828 ext4_lock_group(sb
, group
);
3829 list_for_each_entry_safe(pa
, tmp
,
3830 &grp
->bb_prealloc_list
, pa_group_list
) {
3831 spin_lock(&pa
->pa_lock
);
3832 if (atomic_read(&pa
->pa_count
)) {
3833 spin_unlock(&pa
->pa_lock
);
3837 if (pa
->pa_deleted
) {
3838 spin_unlock(&pa
->pa_lock
);
3842 /* seems this one can be freed ... */
3845 /* we can trust pa_free ... */
3846 free
+= pa
->pa_free
;
3848 spin_unlock(&pa
->pa_lock
);
3850 list_del(&pa
->pa_group_list
);
3851 list_add(&pa
->u
.pa_tmp_list
, &list
);
3854 /* if we still need more blocks and some PAs were used, try again */
3855 if (free
< needed
&& busy
) {
3857 ext4_unlock_group(sb
, group
);
3859 * Yield the CPU here so that we don't get soft lockup
3860 * in non preempt case.
3866 /* found anything to free? */
3867 if (list_empty(&list
)) {
3872 /* now free all selected PAs */
3873 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3875 /* remove from object (inode or locality group) */
3876 spin_lock(pa
->pa_obj_lock
);
3877 list_del_rcu(&pa
->pa_inode_list
);
3878 spin_unlock(pa
->pa_obj_lock
);
3881 ext4_mb_release_group_pa(&e4b
, pa
);
3883 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3885 list_del(&pa
->u
.pa_tmp_list
);
3886 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3890 ext4_unlock_group(sb
, group
);
3891 ext4_mb_release_desc(&e4b
);
3897 * releases all non-used preallocated blocks for given inode
3899 * It's important to discard preallocations under i_data_sem
3900 * We don't want another block to be served from the prealloc
3901 * space when we are discarding the inode prealloc space.
3903 * FIXME!! Make sure it is valid at all the call sites
3905 void ext4_mb_discard_inode_preallocations(struct inode
*inode
)
3907 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3908 struct super_block
*sb
= inode
->i_sb
;
3909 struct buffer_head
*bitmap_bh
= NULL
;
3910 struct ext4_prealloc_space
*pa
, *tmp
;
3911 ext4_group_t group
= 0;
3912 struct list_head list
;
3913 struct ext4_buddy e4b
;
3916 if (!test_opt(sb
, MBALLOC
) || !S_ISREG(inode
->i_mode
)) {
3917 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3921 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
3923 INIT_LIST_HEAD(&list
);
3926 /* first, collect all pa's in the inode */
3927 spin_lock(&ei
->i_prealloc_lock
);
3928 while (!list_empty(&ei
->i_prealloc_list
)) {
3929 pa
= list_entry(ei
->i_prealloc_list
.next
,
3930 struct ext4_prealloc_space
, pa_inode_list
);
3931 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3932 spin_lock(&pa
->pa_lock
);
3933 if (atomic_read(&pa
->pa_count
)) {
3934 /* this shouldn't happen often - nobody should
3935 * use preallocation while we're discarding it */
3936 spin_unlock(&pa
->pa_lock
);
3937 spin_unlock(&ei
->i_prealloc_lock
);
3938 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
3940 schedule_timeout_uninterruptible(HZ
);
3944 if (pa
->pa_deleted
== 0) {
3946 spin_unlock(&pa
->pa_lock
);
3947 list_del_rcu(&pa
->pa_inode_list
);
3948 list_add(&pa
->u
.pa_tmp_list
, &list
);
3952 /* someone is deleting pa right now */
3953 spin_unlock(&pa
->pa_lock
);
3954 spin_unlock(&ei
->i_prealloc_lock
);
3956 /* we have to wait here because pa_deleted
3957 * doesn't mean pa is already unlinked from
3958 * the list. as we might be called from
3959 * ->clear_inode() the inode will get freed
3960 * and concurrent thread which is unlinking
3961 * pa from inode's list may access already
3962 * freed memory, bad-bad-bad */
3964 /* XXX: if this happens too often, we can
3965 * add a flag to force wait only in case
3966 * of ->clear_inode(), but not in case of
3967 * regular truncate */
3968 schedule_timeout_uninterruptible(HZ
);
3971 spin_unlock(&ei
->i_prealloc_lock
);
3973 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3974 BUG_ON(pa
->pa_linear
!= 0);
3975 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
3977 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3978 BUG_ON(err
!= 0); /* error handling here */
3980 bitmap_bh
= read_block_bitmap(sb
, group
);
3981 if (bitmap_bh
== NULL
) {
3982 /* error handling here */
3983 ext4_mb_release_desc(&e4b
);
3984 BUG_ON(bitmap_bh
== NULL
);
3987 ext4_lock_group(sb
, group
);
3988 list_del(&pa
->pa_group_list
);
3989 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3990 ext4_unlock_group(sb
, group
);
3992 ext4_mb_release_desc(&e4b
);
3995 list_del(&pa
->u
.pa_tmp_list
);
3996 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4001 * finds all preallocated spaces and return blocks being freed to them
4002 * if preallocated space becomes full (no block is used from the space)
4003 * then the function frees space in buddy
4004 * XXX: at the moment, truncate (which is the only way to free blocks)
4005 * discards all preallocations
4007 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4008 struct ext4_buddy
*e4b
,
4009 sector_t block
, int count
)
4011 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4014 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4016 struct super_block
*sb
= ac
->ac_sb
;
4019 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4020 " Allocation context details:\n");
4021 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4022 ac
->ac_status
, ac
->ac_flags
);
4023 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4024 "best %lu/%lu/%lu@%lu cr %d\n",
4025 (unsigned long)ac
->ac_o_ex
.fe_group
,
4026 (unsigned long)ac
->ac_o_ex
.fe_start
,
4027 (unsigned long)ac
->ac_o_ex
.fe_len
,
4028 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4029 (unsigned long)ac
->ac_g_ex
.fe_group
,
4030 (unsigned long)ac
->ac_g_ex
.fe_start
,
4031 (unsigned long)ac
->ac_g_ex
.fe_len
,
4032 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4033 (unsigned long)ac
->ac_b_ex
.fe_group
,
4034 (unsigned long)ac
->ac_b_ex
.fe_start
,
4035 (unsigned long)ac
->ac_b_ex
.fe_len
,
4036 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4037 (int)ac
->ac_criteria
);
4038 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4040 printk(KERN_ERR
"EXT4-fs: groups: \n");
4041 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
4042 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4043 struct ext4_prealloc_space
*pa
;
4044 ext4_grpblk_t start
;
4045 struct list_head
*cur
;
4046 ext4_lock_group(sb
, i
);
4047 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4048 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4050 spin_lock(&pa
->pa_lock
);
4051 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4053 spin_unlock(&pa
->pa_lock
);
4054 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4057 ext4_lock_group(sb
, i
);
4059 if (grp
->bb_free
== 0)
4061 printk(KERN_ERR
"%lu: %d/%d \n",
4062 i
, grp
->bb_free
, grp
->bb_fragments
);
4064 printk(KERN_ERR
"\n");
4067 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4074 * We use locality group preallocation for small size file. The size of the
4075 * file is determined by the current size or the resulting size after
4076 * allocation which ever is larger
4078 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4080 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4082 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4083 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4086 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4089 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4090 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
4091 size
= max(size
, isize
);
4093 /* don't use group allocation for large files */
4094 if (size
>= sbi
->s_mb_stream_request
)
4097 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4100 BUG_ON(ac
->ac_lg
!= NULL
);
4102 * locality group prealloc space are per cpu. The reason for having
4103 * per cpu locality group is to reduce the contention between block
4104 * request from multiple CPUs.
4106 ac
->ac_lg
= &sbi
->s_locality_groups
[get_cpu()];
4109 /* we're going to use group allocation */
4110 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4112 /* serialize all allocations in the group */
4113 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4116 static int ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4117 struct ext4_allocation_request
*ar
)
4119 struct super_block
*sb
= ar
->inode
->i_sb
;
4120 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4121 struct ext4_super_block
*es
= sbi
->s_es
;
4125 ext4_grpblk_t block
;
4127 /* we can't allocate > group size */
4130 /* just a dirty hack to filter too big requests */
4131 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4132 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4134 /* start searching from the goal */
4136 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4137 goal
>= ext4_blocks_count(es
))
4138 goal
= le32_to_cpu(es
->s_first_data_block
);
4139 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4141 /* set up allocation goals */
4142 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4143 ac
->ac_b_ex
.fe_group
= 0;
4144 ac
->ac_b_ex
.fe_start
= 0;
4145 ac
->ac_b_ex
.fe_len
= 0;
4146 ac
->ac_status
= AC_STATUS_CONTINUE
;
4147 ac
->ac_groups_scanned
= 0;
4148 ac
->ac_ex_scanned
= 0;
4151 ac
->ac_inode
= ar
->inode
;
4152 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4153 ac
->ac_o_ex
.fe_group
= group
;
4154 ac
->ac_o_ex
.fe_start
= block
;
4155 ac
->ac_o_ex
.fe_len
= len
;
4156 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4157 ac
->ac_g_ex
.fe_group
= group
;
4158 ac
->ac_g_ex
.fe_start
= block
;
4159 ac
->ac_g_ex
.fe_len
= len
;
4160 ac
->ac_f_ex
.fe_len
= 0;
4161 ac
->ac_flags
= ar
->flags
;
4163 ac
->ac_criteria
= 0;
4165 ac
->ac_bitmap_page
= NULL
;
4166 ac
->ac_buddy_page
= NULL
;
4169 /* we have to define context: we'll we work with a file or
4170 * locality group. this is a policy, actually */
4171 ext4_mb_group_or_file(ac
);
4173 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4174 "left: %u/%u, right %u/%u to %swritable\n",
4175 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4176 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4177 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4178 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4179 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4185 * release all resource we used in allocation
4187 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4190 if (ac
->ac_pa
->pa_linear
) {
4191 /* see comment in ext4_mb_use_group_pa() */
4192 spin_lock(&ac
->ac_pa
->pa_lock
);
4193 ac
->ac_pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4194 ac
->ac_pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4195 ac
->ac_pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4196 ac
->ac_pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4197 spin_unlock(&ac
->ac_pa
->pa_lock
);
4199 ext4_mb_put_pa(ac
, ac
->ac_sb
, ac
->ac_pa
);
4201 if (ac
->ac_bitmap_page
)
4202 page_cache_release(ac
->ac_bitmap_page
);
4203 if (ac
->ac_buddy_page
)
4204 page_cache_release(ac
->ac_buddy_page
);
4205 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4206 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4207 ext4_mb_collect_stats(ac
);
4211 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4217 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
&& needed
> 0; i
++) {
4218 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4227 * Main entry point into mballoc to allocate blocks
4228 * it tries to use preallocation first, then falls back
4229 * to usual allocation
4231 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4232 struct ext4_allocation_request
*ar
, int *errp
)
4234 struct ext4_allocation_context ac
;
4235 struct ext4_sb_info
*sbi
;
4236 struct super_block
*sb
;
4237 ext4_fsblk_t block
= 0;
4241 sb
= ar
->inode
->i_sb
;
4244 if (!test_opt(sb
, MBALLOC
)) {
4245 block
= ext4_new_blocks_old(handle
, ar
->inode
, ar
->goal
,
4250 while (ar
->len
&& DQUOT_ALLOC_BLOCK(ar
->inode
, ar
->len
)) {
4251 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4260 ext4_mb_poll_new_transaction(sb
, handle
);
4262 *errp
= ext4_mb_initialize_context(&ac
, ar
);
4268 ac
.ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4269 if (!ext4_mb_use_preallocated(&ac
)) {
4271 ac
.ac_op
= EXT4_MB_HISTORY_ALLOC
;
4272 ext4_mb_normalize_request(&ac
, ar
);
4275 /* allocate space in core */
4276 ext4_mb_regular_allocator(&ac
);
4278 /* as we've just preallocated more space than
4279 * user requested orinally, we store allocated
4280 * space in a special descriptor */
4281 if (ac
.ac_status
== AC_STATUS_FOUND
&&
4282 ac
.ac_o_ex
.fe_len
< ac
.ac_b_ex
.fe_len
)
4283 ext4_mb_new_preallocation(&ac
);
4286 if (likely(ac
.ac_status
== AC_STATUS_FOUND
)) {
4287 ext4_mb_mark_diskspace_used(&ac
, handle
);
4289 block
= ext4_grp_offs_to_block(sb
, &ac
.ac_b_ex
);
4290 ar
->len
= ac
.ac_b_ex
.fe_len
;
4292 freed
= ext4_mb_discard_preallocations(sb
, ac
.ac_o_ex
.fe_len
);
4296 ac
.ac_b_ex
.fe_len
= 0;
4298 ext4_mb_show_ac(&ac
);
4301 ext4_mb_release_context(&ac
);
4304 if (ar
->len
< inquota
)
4305 DQUOT_FREE_BLOCK(ar
->inode
, inquota
- ar
->len
);
4309 static void ext4_mb_poll_new_transaction(struct super_block
*sb
,
4312 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4314 if (sbi
->s_last_transaction
== handle
->h_transaction
->t_tid
)
4317 /* new transaction! time to close last one and free blocks for
4318 * committed transaction. we know that only transaction can be
4319 * active, so previos transaction can be being logged and we
4320 * know that transaction before previous is known to be already
4321 * logged. this means that now we may free blocks freed in all
4322 * transactions before previous one. hope I'm clear enough ... */
4324 spin_lock(&sbi
->s_md_lock
);
4325 if (sbi
->s_last_transaction
!= handle
->h_transaction
->t_tid
) {
4326 mb_debug("new transaction %lu, old %lu\n",
4327 (unsigned long) handle
->h_transaction
->t_tid
,
4328 (unsigned long) sbi
->s_last_transaction
);
4329 list_splice_init(&sbi
->s_closed_transaction
,
4330 &sbi
->s_committed_transaction
);
4331 list_splice_init(&sbi
->s_active_transaction
,
4332 &sbi
->s_closed_transaction
);
4333 sbi
->s_last_transaction
= handle
->h_transaction
->t_tid
;
4335 spin_unlock(&sbi
->s_md_lock
);
4337 ext4_mb_free_committed_blocks(sb
);
4340 static int ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4341 ext4_group_t group
, ext4_grpblk_t block
, int count
)
4343 struct ext4_group_info
*db
= e4b
->bd_info
;
4344 struct super_block
*sb
= e4b
->bd_sb
;
4345 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4346 struct ext4_free_metadata
*md
;
4349 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4350 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4352 ext4_lock_group(sb
, group
);
4353 for (i
= 0; i
< count
; i
++) {
4355 if (md
&& db
->bb_tid
!= handle
->h_transaction
->t_tid
) {
4356 db
->bb_md_cur
= NULL
;
4361 ext4_unlock_group(sb
, group
);
4362 md
= kmalloc(sizeof(*md
), GFP_NOFS
);
4368 ext4_lock_group(sb
, group
);
4369 if (db
->bb_md_cur
== NULL
) {
4370 spin_lock(&sbi
->s_md_lock
);
4371 list_add(&md
->list
, &sbi
->s_active_transaction
);
4372 spin_unlock(&sbi
->s_md_lock
);
4373 /* protect buddy cache from being freed,
4374 * otherwise we'll refresh it from
4375 * on-disk bitmap and lose not-yet-available
4377 page_cache_get(e4b
->bd_buddy_page
);
4378 page_cache_get(e4b
->bd_bitmap_page
);
4380 db
->bb_tid
= handle
->h_transaction
->t_tid
;
4381 mb_debug("new md 0x%p for group %lu\n",
4389 BUG_ON(md
->num
>= EXT4_BB_MAX_BLOCKS
);
4390 md
->blocks
[md
->num
] = block
+ i
;
4392 if (md
->num
== EXT4_BB_MAX_BLOCKS
) {
4393 /* no more space, put full container on a sb's list */
4394 db
->bb_md_cur
= NULL
;
4397 ext4_unlock_group(sb
, group
);
4402 * Main entry point into mballoc to free blocks
4404 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4405 unsigned long block
, unsigned long count
,
4406 int metadata
, unsigned long *freed
)
4408 struct buffer_head
*bitmap_bh
= 0;
4409 struct super_block
*sb
= inode
->i_sb
;
4410 struct ext4_allocation_context ac
;
4411 struct ext4_group_desc
*gdp
;
4412 struct ext4_super_block
*es
;
4413 unsigned long overflow
;
4415 struct buffer_head
*gd_bh
;
4416 ext4_group_t block_group
;
4417 struct ext4_sb_info
*sbi
;
4418 struct ext4_buddy e4b
;
4424 ext4_mb_poll_new_transaction(sb
, handle
);
4427 es
= EXT4_SB(sb
)->s_es
;
4428 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4429 block
+ count
< block
||
4430 block
+ count
> ext4_blocks_count(es
)) {
4431 ext4_error(sb
, __FUNCTION__
,
4432 "Freeing blocks not in datazone - "
4433 "block = %lu, count = %lu", block
, count
);
4437 ext4_debug("freeing block %lu\n", block
);
4439 ac
.ac_op
= EXT4_MB_HISTORY_FREE
;
4440 ac
.ac_inode
= inode
;
4445 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4448 * Check to see if we are freeing blocks across a group
4451 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4452 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4455 bitmap_bh
= read_block_bitmap(sb
, block_group
);
4458 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4462 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4463 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4464 in_range(block
, ext4_inode_table(sb
, gdp
),
4465 EXT4_SB(sb
)->s_itb_per_group
) ||
4466 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4467 EXT4_SB(sb
)->s_itb_per_group
)) {
4469 ext4_error(sb
, __FUNCTION__
,
4470 "Freeing blocks in system zone - "
4471 "Block = %lu, count = %lu", block
, count
);
4474 BUFFER_TRACE(bitmap_bh
, "getting write access");
4475 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4480 * We are about to modify some metadata. Call the journal APIs
4481 * to unshare ->b_data if a currently-committing transaction is
4484 BUFFER_TRACE(gd_bh
, "get_write_access");
4485 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4489 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4493 #ifdef AGGRESSIVE_CHECK
4496 for (i
= 0; i
< count
; i
++)
4497 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4500 mb_clear_bits(sb_bgl_lock(sbi
, block_group
), bitmap_bh
->b_data
,
4503 /* We dirtied the bitmap block */
4504 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4505 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
4507 ac
.ac_b_ex
.fe_group
= block_group
;
4508 ac
.ac_b_ex
.fe_start
= bit
;
4509 ac
.ac_b_ex
.fe_len
= count
;
4510 ext4_mb_store_history(&ac
);
4513 /* blocks being freed are metadata. these blocks shouldn't
4514 * be used until this transaction is committed */
4515 ext4_mb_free_metadata(handle
, &e4b
, block_group
, bit
, count
);
4517 ext4_lock_group(sb
, block_group
);
4518 err
= mb_free_blocks(inode
, &e4b
, bit
, count
);
4519 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4520 ext4_unlock_group(sb
, block_group
);
4524 spin_lock(sb_bgl_lock(sbi
, block_group
));
4525 gdp
->bg_free_blocks_count
=
4526 cpu_to_le16(le16_to_cpu(gdp
->bg_free_blocks_count
) + count
);
4527 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4528 spin_unlock(sb_bgl_lock(sbi
, block_group
));
4529 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4531 ext4_mb_release_desc(&e4b
);
4535 /* And the group descriptor block */
4536 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4537 ret
= ext4_journal_dirty_metadata(handle
, gd_bh
);
4541 if (overflow
&& !err
) {
4550 ext4_std_error(sb
, err
);