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
;
423 static struct kmem_cache
*ext4_ac_cachep
;
425 #ifdef EXT4_BB_MAX_BLOCKS
426 #undef EXT4_BB_MAX_BLOCKS
428 #define EXT4_BB_MAX_BLOCKS 30
430 struct ext4_free_metadata
{
433 ext4_grpblk_t blocks
[EXT4_BB_MAX_BLOCKS
];
434 struct list_head list
;
437 struct ext4_group_info
{
438 unsigned long bb_state
;
439 unsigned long bb_tid
;
440 struct ext4_free_metadata
*bb_md_cur
;
441 unsigned short bb_first_free
;
442 unsigned short bb_free
;
443 unsigned short bb_fragments
;
444 struct list_head bb_prealloc_list
;
448 unsigned short bb_counters
[];
451 #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
452 #define EXT4_GROUP_INFO_LOCKED_BIT 1
454 #define EXT4_MB_GRP_NEED_INIT(grp) \
455 (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
458 struct ext4_prealloc_space
{
459 struct list_head pa_inode_list
;
460 struct list_head pa_group_list
;
462 struct list_head pa_tmp_list
;
463 struct rcu_head pa_rcu
;
468 ext4_fsblk_t pa_pstart
; /* phys. block */
469 ext4_lblk_t pa_lstart
; /* log. block */
470 unsigned short pa_len
; /* len of preallocated chunk */
471 unsigned short pa_free
; /* how many blocks are free */
472 unsigned short pa_linear
; /* consumed in one direction
473 * strictly, for grp prealloc */
474 spinlock_t
*pa_obj_lock
;
475 struct inode
*pa_inode
; /* hack, for history only */
479 struct ext4_free_extent
{
480 ext4_lblk_t fe_logical
;
481 ext4_grpblk_t fe_start
;
482 ext4_group_t fe_group
;
488 * we try to group all related changes together
489 * so that writeback can flush/allocate them together as well
491 struct ext4_locality_group
{
493 struct mutex lg_mutex
; /* to serialize allocates */
494 struct list_head lg_prealloc_list
;/* list of preallocations */
495 spinlock_t lg_prealloc_lock
;
498 struct ext4_allocation_context
{
499 struct inode
*ac_inode
;
500 struct super_block
*ac_sb
;
502 /* original request */
503 struct ext4_free_extent ac_o_ex
;
505 /* goal request (after normalization) */
506 struct ext4_free_extent ac_g_ex
;
508 /* the best found extent */
509 struct ext4_free_extent ac_b_ex
;
511 /* copy of the bext found extent taken before preallocation efforts */
512 struct ext4_free_extent ac_f_ex
;
514 /* number of iterations done. we have to track to limit searching */
515 unsigned long ac_ex_scanned
;
516 __u16 ac_groups_scanned
;
520 __u16 ac_flags
; /* allocation hints */
524 __u8 ac_2order
; /* if request is to allocate 2^N blocks and
525 * N > 0, the field stores N, otherwise 0 */
526 __u8 ac_op
; /* operation, for history only */
527 struct page
*ac_bitmap_page
;
528 struct page
*ac_buddy_page
;
529 struct ext4_prealloc_space
*ac_pa
;
530 struct ext4_locality_group
*ac_lg
;
533 #define AC_STATUS_CONTINUE 1
534 #define AC_STATUS_FOUND 2
535 #define AC_STATUS_BREAK 3
537 struct ext4_mb_history
{
538 struct ext4_free_extent orig
; /* orig allocation */
539 struct ext4_free_extent goal
; /* goal allocation */
540 struct ext4_free_extent result
; /* result allocation */
543 __u16 found
; /* how many extents have been found */
544 __u16 groups
; /* how many groups have been scanned */
545 __u16 tail
; /* what tail broke some buddy */
546 __u16 buddy
; /* buddy the tail ^^^ broke */
548 __u8 cr
:3; /* which phase the result extent was found at */
554 struct page
*bd_buddy_page
;
556 struct page
*bd_bitmap_page
;
558 struct ext4_group_info
*bd_info
;
559 struct super_block
*bd_sb
;
561 ext4_group_t bd_group
;
563 #define EXT4_MB_BITMAP(e4b) ((e4b)->bd_bitmap)
564 #define EXT4_MB_BUDDY(e4b) ((e4b)->bd_buddy)
566 #ifndef EXT4_MB_HISTORY
567 static inline void ext4_mb_store_history(struct ext4_allocation_context
*ac
)
572 static void ext4_mb_store_history(struct ext4_allocation_context
*ac
);
575 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
577 static struct proc_dir_entry
*proc_root_ext4
;
578 struct buffer_head
*read_block_bitmap(struct super_block
*, ext4_group_t
);
579 ext4_fsblk_t
ext4_new_blocks_old(handle_t
*handle
, struct inode
*inode
,
580 ext4_fsblk_t goal
, unsigned long *count
, int *errp
);
582 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
584 static void ext4_mb_poll_new_transaction(struct super_block
*, handle_t
*);
585 static void ext4_mb_free_committed_blocks(struct super_block
*);
586 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
587 struct ext4_buddy
*e4b
, sector_t block
,
589 static void ext4_mb_put_pa(struct ext4_allocation_context
*,
590 struct super_block
*, struct ext4_prealloc_space
*pa
);
591 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
);
592 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
);
595 static inline void ext4_lock_group(struct super_block
*sb
, ext4_group_t group
)
597 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
599 bit_spin_lock(EXT4_GROUP_INFO_LOCKED_BIT
, &(grinfo
->bb_state
));
602 static inline void ext4_unlock_group(struct super_block
*sb
,
605 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
607 bit_spin_unlock(EXT4_GROUP_INFO_LOCKED_BIT
, &(grinfo
->bb_state
));
610 static inline int ext4_is_group_locked(struct super_block
*sb
,
613 struct ext4_group_info
*grinfo
= ext4_get_group_info(sb
, group
);
615 return bit_spin_is_locked(EXT4_GROUP_INFO_LOCKED_BIT
,
616 &(grinfo
->bb_state
));
619 static ext4_fsblk_t
ext4_grp_offs_to_block(struct super_block
*sb
,
620 struct ext4_free_extent
*fex
)
624 block
= (ext4_fsblk_t
) fex
->fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
626 + le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
630 #if BITS_PER_LONG == 64
631 #define mb_correct_addr_and_bit(bit, addr) \
633 bit += ((unsigned long) addr & 7UL) << 3; \
634 addr = (void *) ((unsigned long) addr & ~7UL); \
636 #elif BITS_PER_LONG == 32
637 #define mb_correct_addr_and_bit(bit, addr) \
639 bit += ((unsigned long) addr & 3UL) << 3; \
640 addr = (void *) ((unsigned long) addr & ~3UL); \
643 #error "how many bits you are?!"
646 static inline int mb_test_bit(int bit
, void *addr
)
649 * ext4_test_bit on architecture like powerpc
650 * needs unsigned long aligned address
652 mb_correct_addr_and_bit(bit
, addr
);
653 return ext4_test_bit(bit
, addr
);
656 static inline void mb_set_bit(int bit
, void *addr
)
658 mb_correct_addr_and_bit(bit
, addr
);
659 ext4_set_bit(bit
, addr
);
662 static inline void mb_set_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
664 mb_correct_addr_and_bit(bit
, addr
);
665 ext4_set_bit_atomic(lock
, bit
, addr
);
668 static inline void mb_clear_bit(int bit
, void *addr
)
670 mb_correct_addr_and_bit(bit
, addr
);
671 ext4_clear_bit(bit
, addr
);
674 static inline void mb_clear_bit_atomic(spinlock_t
*lock
, int bit
, void *addr
)
676 mb_correct_addr_and_bit(bit
, addr
);
677 ext4_clear_bit_atomic(lock
, bit
, addr
);
680 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
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
) {
967 ext4_error(sb
, __FUNCTION__
,
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
)) {
1825 * IF we corrupt the bitmap we won't find any
1826 * free blocks even though group info says we
1827 * we have free blocks
1829 ext4_error(sb
, __FUNCTION__
, "%d free blocks as per "
1830 "group info. But bitmap says 0\n",
1835 mb_find_extent(e4b
, 0, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1836 BUG_ON(ex
.fe_len
<= 0);
1837 if (free
< ex
.fe_len
) {
1838 ext4_error(sb
, __FUNCTION__
, "%d free blocks as per "
1839 "group info. But got %d blocks\n",
1843 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1849 ext4_mb_check_limits(ac
, e4b
, 1);
1853 * This is a special case for storages like raid5
1854 * we try to find stripe-aligned chunks for stripe-size requests
1855 * XXX should do so at least for multiples of stripe size as well
1857 static void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1858 struct ext4_buddy
*e4b
)
1860 struct super_block
*sb
= ac
->ac_sb
;
1861 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1862 void *bitmap
= EXT4_MB_BITMAP(e4b
);
1863 struct ext4_free_extent ex
;
1864 ext4_fsblk_t first_group_block
;
1869 BUG_ON(sbi
->s_stripe
== 0);
1871 /* find first stripe-aligned block in group */
1872 first_group_block
= e4b
->bd_group
* EXT4_BLOCKS_PER_GROUP(sb
)
1873 + le32_to_cpu(sbi
->s_es
->s_first_data_block
);
1874 a
= first_group_block
+ sbi
->s_stripe
- 1;
1875 do_div(a
, sbi
->s_stripe
);
1876 i
= (a
* sbi
->s_stripe
) - first_group_block
;
1878 while (i
< EXT4_BLOCKS_PER_GROUP(sb
)) {
1879 if (!mb_test_bit(i
, bitmap
)) {
1880 max
= mb_find_extent(e4b
, 0, i
, sbi
->s_stripe
, &ex
);
1881 if (max
>= sbi
->s_stripe
) {
1884 ext4_mb_use_best_found(ac
, e4b
);
1892 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
1893 ext4_group_t group
, int cr
)
1895 unsigned free
, fragments
;
1897 struct ext4_group_desc
*desc
;
1898 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1900 BUG_ON(cr
< 0 || cr
>= 4);
1901 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp
));
1903 free
= grp
->bb_free
;
1904 fragments
= grp
->bb_fragments
;
1912 BUG_ON(ac
->ac_2order
== 0);
1913 /* If this group is uninitialized, skip it initially */
1914 desc
= ext4_get_group_desc(ac
->ac_sb
, group
, NULL
);
1915 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))
1918 bits
= ac
->ac_sb
->s_blocksize_bits
+ 1;
1919 for (i
= ac
->ac_2order
; i
<= bits
; i
++)
1920 if (grp
->bb_counters
[i
] > 0)
1924 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
1928 if (free
>= ac
->ac_g_ex
.fe_len
)
1940 static int ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
1947 struct ext4_sb_info
*sbi
;
1948 struct super_block
*sb
;
1949 struct ext4_buddy e4b
;
1954 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1956 /* first, try the goal */
1957 err
= ext4_mb_find_by_goal(ac
, &e4b
);
1958 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
1961 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
1965 * ac->ac2_order is set only if the fe_len is a power of 2
1966 * if ac2_order is set we also set criteria to 0 so that we
1967 * try exact allocation using buddy.
1969 i
= fls(ac
->ac_g_ex
.fe_len
);
1972 * We search using buddy data only if the order of the request
1973 * is greater than equal to the sbi_s_mb_order2_reqs
1974 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1976 if (i
>= sbi
->s_mb_order2_reqs
) {
1978 * This should tell if fe_len is exactly power of 2
1980 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
1981 ac
->ac_2order
= i
- 1;
1984 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
1985 /* if stream allocation is enabled, use global goal */
1986 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
1987 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
1991 if (size
< sbi
->s_mb_stream_request
&&
1992 (ac
->ac_flags
& EXT4_MB_HINT_DATA
)) {
1993 /* TBD: may be hot point */
1994 spin_lock(&sbi
->s_md_lock
);
1995 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
1996 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
1997 spin_unlock(&sbi
->s_md_lock
);
2000 /* searching for the right group start from the goal value specified */
2001 group
= ac
->ac_g_ex
.fe_group
;
2003 /* Let's just scan groups to find more-less suitable blocks */
2004 cr
= ac
->ac_2order
? 0 : 1;
2006 * cr == 0 try to get exact allocation,
2007 * cr == 3 try to get anything
2010 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2011 ac
->ac_criteria
= cr
;
2012 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; group
++, i
++) {
2013 struct ext4_group_info
*grp
;
2014 struct ext4_group_desc
*desc
;
2016 if (group
== EXT4_SB(sb
)->s_groups_count
)
2019 /* quick check to skip empty groups */
2020 grp
= ext4_get_group_info(ac
->ac_sb
, group
);
2021 if (grp
->bb_free
== 0)
2025 * if the group is already init we check whether it is
2026 * a good group and if not we don't load the buddy
2028 if (EXT4_MB_GRP_NEED_INIT(grp
)) {
2030 * we need full data about the group
2031 * to make a good selection
2033 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2036 ext4_mb_release_desc(&e4b
);
2040 * If the particular group doesn't satisfy our
2041 * criteria we continue with the next group
2043 if (!ext4_mb_good_group(ac
, group
, cr
))
2046 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2050 ext4_lock_group(sb
, group
);
2051 if (!ext4_mb_good_group(ac
, group
, cr
)) {
2052 /* someone did allocation from this group */
2053 ext4_unlock_group(sb
, group
);
2054 ext4_mb_release_desc(&e4b
);
2058 ac
->ac_groups_scanned
++;
2059 desc
= ext4_get_group_desc(sb
, group
, NULL
);
2060 if (cr
== 0 || (desc
->bg_flags
&
2061 cpu_to_le16(EXT4_BG_BLOCK_UNINIT
) &&
2062 ac
->ac_2order
!= 0))
2063 ext4_mb_simple_scan_group(ac
, &e4b
);
2065 ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
)
2066 ext4_mb_scan_aligned(ac
, &e4b
);
2068 ext4_mb_complex_scan_group(ac
, &e4b
);
2070 ext4_unlock_group(sb
, group
);
2071 ext4_mb_release_desc(&e4b
);
2073 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2078 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2079 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2081 * We've been searching too long. Let's try to allocate
2082 * the best chunk we've found so far
2085 ext4_mb_try_best_found(ac
, &e4b
);
2086 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2088 * Someone more lucky has already allocated it.
2089 * The only thing we can do is just take first
2091 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2093 ac
->ac_b_ex
.fe_group
= 0;
2094 ac
->ac_b_ex
.fe_start
= 0;
2095 ac
->ac_b_ex
.fe_len
= 0;
2096 ac
->ac_status
= AC_STATUS_CONTINUE
;
2097 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2099 atomic_inc(&sbi
->s_mb_lost_chunks
);
2107 #ifdef EXT4_MB_HISTORY
2108 struct ext4_mb_proc_session
{
2109 struct ext4_mb_history
*history
;
2110 struct super_block
*sb
;
2115 static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session
*s
,
2116 struct ext4_mb_history
*hs
,
2119 if (hs
== s
->history
+ s
->max
)
2121 if (!first
&& hs
== s
->history
+ s
->start
)
2123 while (hs
->orig
.fe_len
== 0) {
2125 if (hs
== s
->history
+ s
->max
)
2127 if (hs
== s
->history
+ s
->start
)
2133 static void *ext4_mb_seq_history_start(struct seq_file
*seq
, loff_t
*pos
)
2135 struct ext4_mb_proc_session
*s
= seq
->private;
2136 struct ext4_mb_history
*hs
;
2140 return SEQ_START_TOKEN
;
2141 hs
= ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2144 while (--l
&& (hs
= ext4_mb_history_skip_empty(s
, ++hs
, 0)) != NULL
);
2148 static void *ext4_mb_seq_history_next(struct seq_file
*seq
, void *v
,
2151 struct ext4_mb_proc_session
*s
= seq
->private;
2152 struct ext4_mb_history
*hs
= v
;
2155 if (v
== SEQ_START_TOKEN
)
2156 return ext4_mb_history_skip_empty(s
, s
->history
+ s
->start
, 1);
2158 return ext4_mb_history_skip_empty(s
, ++hs
, 0);
2161 static int ext4_mb_seq_history_show(struct seq_file
*seq
, void *v
)
2163 char buf
[25], buf2
[25], buf3
[25], *fmt
;
2164 struct ext4_mb_history
*hs
= v
;
2166 if (v
== SEQ_START_TOKEN
) {
2167 seq_printf(seq
, "%-5s %-8s %-23s %-23s %-23s %-5s "
2168 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2169 "pid", "inode", "original", "goal", "result", "found",
2170 "grps", "cr", "flags", "merge", "tail", "broken");
2174 if (hs
->op
== EXT4_MB_HISTORY_ALLOC
) {
2175 fmt
= "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2176 "%-5u %-5s %-5u %-6u\n";
2177 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
2178 hs
->result
.fe_start
, hs
->result
.fe_len
,
2179 hs
->result
.fe_logical
);
2180 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
2181 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2182 hs
->orig
.fe_logical
);
2183 sprintf(buf3
, "%lu/%d/%u@%u", hs
->goal
.fe_group
,
2184 hs
->goal
.fe_start
, hs
->goal
.fe_len
,
2185 hs
->goal
.fe_logical
);
2186 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, buf3
, buf2
,
2187 hs
->found
, hs
->groups
, hs
->cr
, hs
->flags
,
2188 hs
->merged
? "M" : "", hs
->tail
,
2189 hs
->buddy
? 1 << hs
->buddy
: 0);
2190 } else if (hs
->op
== EXT4_MB_HISTORY_PREALLOC
) {
2191 fmt
= "%-5u %-8u %-23s %-23s %-23s\n";
2192 sprintf(buf2
, "%lu/%d/%u@%u", hs
->result
.fe_group
,
2193 hs
->result
.fe_start
, hs
->result
.fe_len
,
2194 hs
->result
.fe_logical
);
2195 sprintf(buf
, "%lu/%d/%u@%u", hs
->orig
.fe_group
,
2196 hs
->orig
.fe_start
, hs
->orig
.fe_len
,
2197 hs
->orig
.fe_logical
);
2198 seq_printf(seq
, fmt
, hs
->pid
, hs
->ino
, buf
, "", buf2
);
2199 } else if (hs
->op
== EXT4_MB_HISTORY_DISCARD
) {
2200 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
2201 hs
->result
.fe_start
, hs
->result
.fe_len
);
2202 seq_printf(seq
, "%-5u %-8u %-23s discard\n",
2203 hs
->pid
, hs
->ino
, buf2
);
2204 } else if (hs
->op
== EXT4_MB_HISTORY_FREE
) {
2205 sprintf(buf2
, "%lu/%d/%u", hs
->result
.fe_group
,
2206 hs
->result
.fe_start
, hs
->result
.fe_len
);
2207 seq_printf(seq
, "%-5u %-8u %-23s free\n",
2208 hs
->pid
, hs
->ino
, buf2
);
2213 static void ext4_mb_seq_history_stop(struct seq_file
*seq
, void *v
)
2217 static struct seq_operations ext4_mb_seq_history_ops
= {
2218 .start
= ext4_mb_seq_history_start
,
2219 .next
= ext4_mb_seq_history_next
,
2220 .stop
= ext4_mb_seq_history_stop
,
2221 .show
= ext4_mb_seq_history_show
,
2224 static int ext4_mb_seq_history_open(struct inode
*inode
, struct file
*file
)
2226 struct super_block
*sb
= PDE(inode
)->data
;
2227 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2228 struct ext4_mb_proc_session
*s
;
2232 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
2236 size
= sizeof(struct ext4_mb_history
) * sbi
->s_mb_history_max
;
2237 s
->history
= kmalloc(size
, GFP_KERNEL
);
2238 if (s
->history
== NULL
) {
2243 spin_lock(&sbi
->s_mb_history_lock
);
2244 memcpy(s
->history
, sbi
->s_mb_history
, size
);
2245 s
->max
= sbi
->s_mb_history_max
;
2246 s
->start
= sbi
->s_mb_history_cur
% s
->max
;
2247 spin_unlock(&sbi
->s_mb_history_lock
);
2249 rc
= seq_open(file
, &ext4_mb_seq_history_ops
);
2251 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2261 static int ext4_mb_seq_history_release(struct inode
*inode
, struct file
*file
)
2263 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2264 struct ext4_mb_proc_session
*s
= seq
->private;
2267 return seq_release(inode
, file
);
2270 static ssize_t
ext4_mb_seq_history_write(struct file
*file
,
2271 const char __user
*buffer
,
2272 size_t count
, loff_t
*ppos
)
2274 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
2275 struct ext4_mb_proc_session
*s
= seq
->private;
2276 struct super_block
*sb
= s
->sb
;
2280 if (count
>= sizeof(str
)) {
2281 printk(KERN_ERR
"EXT4-fs: %s string too long, max %u bytes\n",
2282 "mb_history", (int)sizeof(str
));
2286 if (copy_from_user(str
, buffer
, count
))
2289 value
= simple_strtol(str
, NULL
, 0);
2292 EXT4_SB(sb
)->s_mb_history_filter
= value
;
2297 static struct file_operations ext4_mb_seq_history_fops
= {
2298 .owner
= THIS_MODULE
,
2299 .open
= ext4_mb_seq_history_open
,
2301 .write
= ext4_mb_seq_history_write
,
2302 .llseek
= seq_lseek
,
2303 .release
= ext4_mb_seq_history_release
,
2306 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2308 struct super_block
*sb
= seq
->private;
2309 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2312 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2316 return (void *) group
;
2319 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2321 struct super_block
*sb
= seq
->private;
2322 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2326 if (*pos
< 0 || *pos
>= sbi
->s_groups_count
)
2329 return (void *) group
;;
2332 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2334 struct super_block
*sb
= seq
->private;
2335 long group
= (long) v
;
2338 struct ext4_buddy e4b
;
2340 struct ext4_group_info info
;
2341 unsigned short counters
[16];
2346 seq_printf(seq
, "#%-5s: %-5s %-5s %-5s "
2347 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2348 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2349 "group", "free", "frags", "first",
2350 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2351 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2353 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2354 sizeof(struct ext4_group_info
);
2355 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2357 seq_printf(seq
, "#%-5lu: I/O error\n", group
);
2360 ext4_lock_group(sb
, group
);
2361 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2362 ext4_unlock_group(sb
, group
);
2363 ext4_mb_release_desc(&e4b
);
2365 seq_printf(seq
, "#%-5lu: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2366 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2367 for (i
= 0; i
<= 13; i
++)
2368 seq_printf(seq
, " %-5u", i
<= sb
->s_blocksize_bits
+ 1 ?
2369 sg
.info
.bb_counters
[i
] : 0);
2370 seq_printf(seq
, " ]\n");
2375 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2379 static struct seq_operations ext4_mb_seq_groups_ops
= {
2380 .start
= ext4_mb_seq_groups_start
,
2381 .next
= ext4_mb_seq_groups_next
,
2382 .stop
= ext4_mb_seq_groups_stop
,
2383 .show
= ext4_mb_seq_groups_show
,
2386 static int ext4_mb_seq_groups_open(struct inode
*inode
, struct file
*file
)
2388 struct super_block
*sb
= PDE(inode
)->data
;
2391 rc
= seq_open(file
, &ext4_mb_seq_groups_ops
);
2393 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2400 static struct file_operations ext4_mb_seq_groups_fops
= {
2401 .owner
= THIS_MODULE
,
2402 .open
= ext4_mb_seq_groups_open
,
2404 .llseek
= seq_lseek
,
2405 .release
= seq_release
,
2408 static void ext4_mb_history_release(struct super_block
*sb
)
2410 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2412 remove_proc_entry("mb_groups", sbi
->s_mb_proc
);
2413 remove_proc_entry("mb_history", sbi
->s_mb_proc
);
2415 kfree(sbi
->s_mb_history
);
2418 static void ext4_mb_history_init(struct super_block
*sb
)
2420 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2423 if (sbi
->s_mb_proc
!= NULL
) {
2424 struct proc_dir_entry
*p
;
2425 p
= create_proc_entry("mb_history", S_IRUGO
, sbi
->s_mb_proc
);
2427 p
->proc_fops
= &ext4_mb_seq_history_fops
;
2430 p
= create_proc_entry("mb_groups", S_IRUGO
, sbi
->s_mb_proc
);
2432 p
->proc_fops
= &ext4_mb_seq_groups_fops
;
2437 sbi
->s_mb_history_max
= 1000;
2438 sbi
->s_mb_history_cur
= 0;
2439 spin_lock_init(&sbi
->s_mb_history_lock
);
2440 i
= sbi
->s_mb_history_max
* sizeof(struct ext4_mb_history
);
2441 sbi
->s_mb_history
= kmalloc(i
, GFP_KERNEL
);
2442 if (likely(sbi
->s_mb_history
!= NULL
))
2443 memset(sbi
->s_mb_history
, 0, i
);
2444 /* if we can't allocate history, then we simple won't use it */
2447 static void ext4_mb_store_history(struct ext4_allocation_context
*ac
)
2449 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
2450 struct ext4_mb_history h
;
2452 if (unlikely(sbi
->s_mb_history
== NULL
))
2455 if (!(ac
->ac_op
& sbi
->s_mb_history_filter
))
2459 h
.pid
= current
->pid
;
2460 h
.ino
= ac
->ac_inode
? ac
->ac_inode
->i_ino
: 0;
2461 h
.orig
= ac
->ac_o_ex
;
2462 h
.result
= ac
->ac_b_ex
;
2463 h
.flags
= ac
->ac_flags
;
2464 h
.found
= ac
->ac_found
;
2465 h
.groups
= ac
->ac_groups_scanned
;
2466 h
.cr
= ac
->ac_criteria
;
2467 h
.tail
= ac
->ac_tail
;
2468 h
.buddy
= ac
->ac_buddy
;
2470 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
) {
2471 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
2472 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
2474 h
.goal
= ac
->ac_g_ex
;
2475 h
.result
= ac
->ac_f_ex
;
2478 spin_lock(&sbi
->s_mb_history_lock
);
2479 memcpy(sbi
->s_mb_history
+ sbi
->s_mb_history_cur
, &h
, sizeof(h
));
2480 if (++sbi
->s_mb_history_cur
>= sbi
->s_mb_history_max
)
2481 sbi
->s_mb_history_cur
= 0;
2482 spin_unlock(&sbi
->s_mb_history_lock
);
2486 #define ext4_mb_history_release(sb)
2487 #define ext4_mb_history_init(sb)
2490 static int ext4_mb_init_backend(struct super_block
*sb
)
2493 int j
, len
, metalen
;
2494 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2495 int num_meta_group_infos
=
2496 (sbi
->s_groups_count
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2497 EXT4_DESC_PER_BLOCK_BITS(sb
);
2498 struct ext4_group_info
**meta_group_info
;
2500 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2501 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2502 * So a two level scheme suffices for now. */
2503 sbi
->s_group_info
= kmalloc(sizeof(*sbi
->s_group_info
) *
2504 num_meta_group_infos
, GFP_KERNEL
);
2505 if (sbi
->s_group_info
== NULL
) {
2506 printk(KERN_ERR
"EXT4-fs: can't allocate buddy meta group\n");
2509 sbi
->s_buddy_cache
= new_inode(sb
);
2510 if (sbi
->s_buddy_cache
== NULL
) {
2511 printk(KERN_ERR
"EXT4-fs: can't get new inode\n");
2514 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2516 metalen
= sizeof(*meta_group_info
) << EXT4_DESC_PER_BLOCK_BITS(sb
);
2517 for (i
= 0; i
< num_meta_group_infos
; i
++) {
2518 if ((i
+ 1) == num_meta_group_infos
)
2519 metalen
= sizeof(*meta_group_info
) *
2520 (sbi
->s_groups_count
-
2521 (i
<< EXT4_DESC_PER_BLOCK_BITS(sb
)));
2522 meta_group_info
= kmalloc(metalen
, GFP_KERNEL
);
2523 if (meta_group_info
== NULL
) {
2524 printk(KERN_ERR
"EXT4-fs: can't allocate mem for a "
2528 sbi
->s_group_info
[i
] = meta_group_info
;
2532 * calculate needed size. if change bb_counters size,
2533 * don't forget about ext4_mb_generate_buddy()
2535 len
= sizeof(struct ext4_group_info
);
2536 len
+= sizeof(unsigned short) * (sb
->s_blocksize_bits
+ 2);
2537 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2538 struct ext4_group_desc
*desc
;
2541 sbi
->s_group_info
[i
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2542 j
= i
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2544 meta_group_info
[j
] = kzalloc(len
, GFP_KERNEL
);
2545 if (meta_group_info
[j
] == NULL
) {
2546 printk(KERN_ERR
"EXT4-fs: can't allocate buddy mem\n");
2550 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2553 "EXT4-fs: can't read descriptor %lu\n", i
);
2556 memset(meta_group_info
[j
], 0, len
);
2557 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2558 &(meta_group_info
[j
]->bb_state
));
2561 * initialize bb_free to be able to skip
2562 * empty groups without initialization
2564 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2565 meta_group_info
[j
]->bb_free
=
2566 ext4_free_blocks_after_init(sb
, i
, desc
);
2568 meta_group_info
[j
]->bb_free
=
2569 le16_to_cpu(desc
->bg_free_blocks_count
);
2572 INIT_LIST_HEAD(&meta_group_info
[j
]->bb_prealloc_list
);
2576 struct buffer_head
*bh
;
2577 meta_group_info
[j
]->bb_bitmap
=
2578 kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
2579 BUG_ON(meta_group_info
[j
]->bb_bitmap
== NULL
);
2580 bh
= read_block_bitmap(sb
, i
);
2582 memcpy(meta_group_info
[j
]->bb_bitmap
, bh
->b_data
,
2594 kfree(ext4_get_group_info(sb
, i
));
2597 i
= num_meta_group_infos
;
2600 kfree(sbi
->s_group_info
[i
]);
2601 iput(sbi
->s_buddy_cache
);
2603 kfree(sbi
->s_group_info
);
2607 int ext4_mb_init(struct super_block
*sb
, int needs_recovery
)
2609 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2614 if (!test_opt(sb
, MBALLOC
))
2617 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(unsigned short);
2619 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2620 if (sbi
->s_mb_offsets
== NULL
) {
2621 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2624 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2625 if (sbi
->s_mb_maxs
== NULL
) {
2626 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2627 kfree(sbi
->s_mb_maxs
);
2631 /* order 0 is regular bitmap */
2632 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2633 sbi
->s_mb_offsets
[0] = 0;
2637 max
= sb
->s_blocksize
<< 2;
2639 sbi
->s_mb_offsets
[i
] = offset
;
2640 sbi
->s_mb_maxs
[i
] = max
;
2641 offset
+= 1 << (sb
->s_blocksize_bits
- i
);
2644 } while (i
<= sb
->s_blocksize_bits
+ 1);
2646 /* init file for buddy data */
2647 i
= ext4_mb_init_backend(sb
);
2649 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2650 kfree(sbi
->s_mb_offsets
);
2651 kfree(sbi
->s_mb_maxs
);
2655 spin_lock_init(&sbi
->s_md_lock
);
2656 INIT_LIST_HEAD(&sbi
->s_active_transaction
);
2657 INIT_LIST_HEAD(&sbi
->s_closed_transaction
);
2658 INIT_LIST_HEAD(&sbi
->s_committed_transaction
);
2659 spin_lock_init(&sbi
->s_bal_lock
);
2661 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2662 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2663 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2664 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2665 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2666 sbi
->s_mb_history_filter
= EXT4_MB_HISTORY_DEFAULT
;
2667 sbi
->s_mb_group_prealloc
= MB_DEFAULT_GROUP_PREALLOC
;
2669 i
= sizeof(struct ext4_locality_group
) * NR_CPUS
;
2670 sbi
->s_locality_groups
= kmalloc(i
, GFP_KERNEL
);
2671 if (sbi
->s_locality_groups
== NULL
) {
2672 clear_opt(sbi
->s_mount_opt
, MBALLOC
);
2673 kfree(sbi
->s_mb_offsets
);
2674 kfree(sbi
->s_mb_maxs
);
2677 for (i
= 0; i
< NR_CPUS
; i
++) {
2678 struct ext4_locality_group
*lg
;
2679 lg
= &sbi
->s_locality_groups
[i
];
2680 mutex_init(&lg
->lg_mutex
);
2681 INIT_LIST_HEAD(&lg
->lg_prealloc_list
);
2682 spin_lock_init(&lg
->lg_prealloc_lock
);
2685 ext4_mb_init_per_dev_proc(sb
);
2686 ext4_mb_history_init(sb
);
2688 printk("EXT4-fs: mballoc enabled\n");
2692 /* need to called with ext4 group lock (ext4_lock_group) */
2693 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2695 struct ext4_prealloc_space
*pa
;
2696 struct list_head
*cur
, *tmp
;
2699 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2700 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2701 list_del(&pa
->pa_group_list
);
2706 mb_debug("mballoc: %u PAs left\n", count
);
2710 int ext4_mb_release(struct super_block
*sb
)
2713 int num_meta_group_infos
;
2714 struct ext4_group_info
*grinfo
;
2715 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2717 if (!test_opt(sb
, MBALLOC
))
2720 /* release freed, non-committed blocks */
2721 spin_lock(&sbi
->s_md_lock
);
2722 list_splice_init(&sbi
->s_closed_transaction
,
2723 &sbi
->s_committed_transaction
);
2724 list_splice_init(&sbi
->s_active_transaction
,
2725 &sbi
->s_committed_transaction
);
2726 spin_unlock(&sbi
->s_md_lock
);
2727 ext4_mb_free_committed_blocks(sb
);
2729 if (sbi
->s_group_info
) {
2730 for (i
= 0; i
< sbi
->s_groups_count
; i
++) {
2731 grinfo
= ext4_get_group_info(sb
, i
);
2733 kfree(grinfo
->bb_bitmap
);
2735 ext4_lock_group(sb
, i
);
2736 ext4_mb_cleanup_pa(grinfo
);
2737 ext4_unlock_group(sb
, i
);
2740 num_meta_group_infos
= (sbi
->s_groups_count
+
2741 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2742 EXT4_DESC_PER_BLOCK_BITS(sb
);
2743 for (i
= 0; i
< num_meta_group_infos
; i
++)
2744 kfree(sbi
->s_group_info
[i
]);
2745 kfree(sbi
->s_group_info
);
2747 kfree(sbi
->s_mb_offsets
);
2748 kfree(sbi
->s_mb_maxs
);
2749 if (sbi
->s_buddy_cache
)
2750 iput(sbi
->s_buddy_cache
);
2751 if (sbi
->s_mb_stats
) {
2753 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2754 atomic_read(&sbi
->s_bal_allocated
),
2755 atomic_read(&sbi
->s_bal_reqs
),
2756 atomic_read(&sbi
->s_bal_success
));
2758 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2759 "%u 2^N hits, %u breaks, %u lost\n",
2760 atomic_read(&sbi
->s_bal_ex_scanned
),
2761 atomic_read(&sbi
->s_bal_goals
),
2762 atomic_read(&sbi
->s_bal_2orders
),
2763 atomic_read(&sbi
->s_bal_breaks
),
2764 atomic_read(&sbi
->s_mb_lost_chunks
));
2766 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2767 sbi
->s_mb_buddies_generated
++,
2768 sbi
->s_mb_generation_time
);
2770 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2771 atomic_read(&sbi
->s_mb_preallocated
),
2772 atomic_read(&sbi
->s_mb_discarded
));
2775 kfree(sbi
->s_locality_groups
);
2777 ext4_mb_history_release(sb
);
2778 ext4_mb_destroy_per_dev_proc(sb
);
2783 static void ext4_mb_free_committed_blocks(struct super_block
*sb
)
2785 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2790 struct ext4_free_metadata
*md
;
2791 struct ext4_buddy e4b
;
2793 if (list_empty(&sbi
->s_committed_transaction
))
2796 /* there is committed blocks to be freed yet */
2798 /* get next array of blocks */
2800 spin_lock(&sbi
->s_md_lock
);
2801 if (!list_empty(&sbi
->s_committed_transaction
)) {
2802 md
= list_entry(sbi
->s_committed_transaction
.next
,
2803 struct ext4_free_metadata
, list
);
2804 list_del(&md
->list
);
2806 spin_unlock(&sbi
->s_md_lock
);
2811 mb_debug("gonna free %u blocks in group %lu (0x%p):",
2812 md
->num
, md
->group
, md
);
2814 err
= ext4_mb_load_buddy(sb
, md
->group
, &e4b
);
2815 /* we expect to find existing buddy because it's pinned */
2818 /* there are blocks to put in buddy to make them really free */
2821 ext4_lock_group(sb
, md
->group
);
2822 for (i
= 0; i
< md
->num
; i
++) {
2823 mb_debug(" %u", md
->blocks
[i
]);
2824 err
= mb_free_blocks(NULL
, &e4b
, md
->blocks
[i
], 1);
2828 ext4_unlock_group(sb
, md
->group
);
2830 /* balance refcounts from ext4_mb_free_metadata() */
2831 page_cache_release(e4b
.bd_buddy_page
);
2832 page_cache_release(e4b
.bd_bitmap_page
);
2835 ext4_mb_release_desc(&e4b
);
2839 mb_debug("freed %u blocks in %u structures\n", count
, count2
);
2842 #define EXT4_ROOT "ext4"
2843 #define EXT4_MB_STATS_NAME "stats"
2844 #define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2845 #define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2846 #define EXT4_MB_ORDER2_REQ "order2_req"
2847 #define EXT4_MB_STREAM_REQ "stream_req"
2848 #define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2852 #define MB_PROC_VALUE_READ(name) \
2853 static int ext4_mb_read_##name(char *page, char **start, \
2854 off_t off, int count, int *eof, void *data) \
2856 struct ext4_sb_info *sbi = data; \
2861 len = sprintf(page, "%ld\n", sbi->s_mb_##name); \
2866 #define MB_PROC_VALUE_WRITE(name) \
2867 static int ext4_mb_write_##name(struct file *file, \
2868 const char __user *buf, unsigned long cnt, void *data) \
2870 struct ext4_sb_info *sbi = data; \
2873 if (cnt >= sizeof(str)) \
2875 if (copy_from_user(str, buf, cnt)) \
2877 value = simple_strtol(str, NULL, 0); \
2880 sbi->s_mb_##name = value; \
2884 MB_PROC_VALUE_READ(stats
);
2885 MB_PROC_VALUE_WRITE(stats
);
2886 MB_PROC_VALUE_READ(max_to_scan
);
2887 MB_PROC_VALUE_WRITE(max_to_scan
);
2888 MB_PROC_VALUE_READ(min_to_scan
);
2889 MB_PROC_VALUE_WRITE(min_to_scan
);
2890 MB_PROC_VALUE_READ(order2_reqs
);
2891 MB_PROC_VALUE_WRITE(order2_reqs
);
2892 MB_PROC_VALUE_READ(stream_request
);
2893 MB_PROC_VALUE_WRITE(stream_request
);
2894 MB_PROC_VALUE_READ(group_prealloc
);
2895 MB_PROC_VALUE_WRITE(group_prealloc
);
2897 #define MB_PROC_HANDLER(name, var) \
2899 proc = create_proc_entry(name, mode, sbi->s_mb_proc); \
2900 if (proc == NULL) { \
2901 printk(KERN_ERR "EXT4-fs: can't to create %s\n", name); \
2905 proc->read_proc = ext4_mb_read_##var ; \
2906 proc->write_proc = ext4_mb_write_##var; \
2909 static int ext4_mb_init_per_dev_proc(struct super_block
*sb
)
2911 mode_t mode
= S_IFREG
| S_IRUGO
| S_IWUSR
;
2912 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2913 struct proc_dir_entry
*proc
;
2916 snprintf(devname
, sizeof(devname
) - 1, "%s",
2917 bdevname(sb
->s_bdev
, devname
));
2918 sbi
->s_mb_proc
= proc_mkdir(devname
, proc_root_ext4
);
2920 MB_PROC_HANDLER(EXT4_MB_STATS_NAME
, stats
);
2921 MB_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME
, max_to_scan
);
2922 MB_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME
, min_to_scan
);
2923 MB_PROC_HANDLER(EXT4_MB_ORDER2_REQ
, order2_reqs
);
2924 MB_PROC_HANDLER(EXT4_MB_STREAM_REQ
, stream_request
);
2925 MB_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC
, group_prealloc
);
2930 printk(KERN_ERR
"EXT4-fs: Unable to create %s\n", devname
);
2931 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2932 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2933 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2934 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2935 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2936 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2937 remove_proc_entry(devname
, proc_root_ext4
);
2938 sbi
->s_mb_proc
= NULL
;
2943 static int ext4_mb_destroy_per_dev_proc(struct super_block
*sb
)
2945 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2948 if (sbi
->s_mb_proc
== NULL
)
2951 snprintf(devname
, sizeof(devname
) - 1, "%s",
2952 bdevname(sb
->s_bdev
, devname
));
2953 remove_proc_entry(EXT4_MB_GROUP_PREALLOC
, sbi
->s_mb_proc
);
2954 remove_proc_entry(EXT4_MB_STREAM_REQ
, sbi
->s_mb_proc
);
2955 remove_proc_entry(EXT4_MB_ORDER2_REQ
, sbi
->s_mb_proc
);
2956 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2957 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME
, sbi
->s_mb_proc
);
2958 remove_proc_entry(EXT4_MB_STATS_NAME
, sbi
->s_mb_proc
);
2959 remove_proc_entry(devname
, proc_root_ext4
);
2964 int __init
init_ext4_mballoc(void)
2966 ext4_pspace_cachep
=
2967 kmem_cache_create("ext4_prealloc_space",
2968 sizeof(struct ext4_prealloc_space
),
2969 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2970 if (ext4_pspace_cachep
== NULL
)
2974 kmem_cache_create("ext4_alloc_context",
2975 sizeof(struct ext4_allocation_context
),
2976 0, SLAB_RECLAIM_ACCOUNT
, NULL
);
2977 if (ext4_ac_cachep
== NULL
) {
2978 kmem_cache_destroy(ext4_pspace_cachep
);
2981 #ifdef CONFIG_PROC_FS
2982 proc_root_ext4
= proc_mkdir(EXT4_ROOT
, proc_root_fs
);
2983 if (proc_root_ext4
== NULL
)
2984 printk(KERN_ERR
"EXT4-fs: Unable to create %s\n", EXT4_ROOT
);
2989 void exit_ext4_mballoc(void)
2991 /* XXX: synchronize_rcu(); */
2992 kmem_cache_destroy(ext4_pspace_cachep
);
2993 kmem_cache_destroy(ext4_ac_cachep
);
2994 #ifdef CONFIG_PROC_FS
2995 remove_proc_entry(EXT4_ROOT
, proc_root_fs
);
3001 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
3002 * Returns 0 if success or error code
3004 static int ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
3007 struct buffer_head
*bitmap_bh
= NULL
;
3008 struct ext4_super_block
*es
;
3009 struct ext4_group_desc
*gdp
;
3010 struct buffer_head
*gdp_bh
;
3011 struct ext4_sb_info
*sbi
;
3012 struct super_block
*sb
;
3016 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3017 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
3023 ext4_debug("using block group %lu(%d)\n", ac
->ac_b_ex
.fe_group
,
3024 gdp
->bg_free_blocks_count
);
3027 bitmap_bh
= read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
3031 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
3036 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
3040 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
3044 block
= ac
->ac_b_ex
.fe_group
* EXT4_BLOCKS_PER_GROUP(sb
)
3045 + ac
->ac_b_ex
.fe_start
3046 + le32_to_cpu(es
->s_first_data_block
);
3048 if (block
== ext4_block_bitmap(sb
, gdp
) ||
3049 block
== ext4_inode_bitmap(sb
, gdp
) ||
3050 in_range(block
, ext4_inode_table(sb
, gdp
),
3051 EXT4_SB(sb
)->s_itb_per_group
)) {
3053 ext4_error(sb
, __FUNCTION__
,
3054 "Allocating block in system zone - block = %llu",
3057 #ifdef AGGRESSIVE_CHECK
3060 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
3061 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
3062 bitmap_bh
->b_data
));
3066 mb_set_bits(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
), bitmap_bh
->b_data
,
3067 ac
->ac_b_ex
.fe_start
, ac
->ac_b_ex
.fe_len
);
3069 spin_lock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3070 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
3071 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
3072 gdp
->bg_free_blocks_count
=
3073 cpu_to_le16(ext4_free_blocks_after_init(sb
,
3074 ac
->ac_b_ex
.fe_group
,
3077 gdp
->bg_free_blocks_count
=
3078 cpu_to_le16(le16_to_cpu(gdp
->bg_free_blocks_count
)
3079 - ac
->ac_b_ex
.fe_len
);
3080 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, ac
->ac_b_ex
.fe_group
, gdp
);
3081 spin_unlock(sb_bgl_lock(sbi
, ac
->ac_b_ex
.fe_group
));
3082 percpu_counter_sub(&sbi
->s_freeblocks_counter
, ac
->ac_b_ex
.fe_len
);
3084 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
3087 err
= ext4_journal_dirty_metadata(handle
, gdp_bh
);
3096 * here we normalize request for locality group
3097 * Group request are normalized to s_strip size if we set the same via mount
3098 * option. If not we set it to s_mb_group_prealloc which can be configured via
3099 * /proc/fs/ext4/<partition>/group_prealloc
3101 * XXX: should we try to preallocate more than the group has now?
3103 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3105 struct super_block
*sb
= ac
->ac_sb
;
3106 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3109 if (EXT4_SB(sb
)->s_stripe
)
3110 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_stripe
;
3112 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3113 mb_debug("#%u: goal %lu blocks for locality group\n",
3114 current
->pid
, ac
->ac_g_ex
.fe_len
);
3118 * Normalization means making request better in terms of
3119 * size and alignment
3121 static void ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3122 struct ext4_allocation_request
*ar
)
3126 struct list_head
*cur
;
3127 loff_t size
, orig_size
, start_off
;
3128 ext4_lblk_t start
, orig_start
;
3129 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3131 /* do normalize only data requests, metadata requests
3132 do not need preallocation */
3133 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3136 /* sometime caller may want exact blocks */
3137 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3140 /* caller may indicate that preallocation isn't
3141 * required (it's a tail, for example) */
3142 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3145 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3146 ext4_mb_normalize_group_request(ac
);
3150 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3152 /* first, let's learn actual file size
3153 * given current request is allocated */
3154 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
3155 size
= size
<< bsbits
;
3156 if (size
< i_size_read(ac
->ac_inode
))
3157 size
= i_size_read(ac
->ac_inode
);
3159 /* max available blocks in a free group */
3160 max
= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
) - 1 - 1 -
3161 EXT4_SB(ac
->ac_sb
)->s_itb_per_group
;
3163 #define NRL_CHECK_SIZE(req, size, max,bits) \
3164 (req <= (size) || max <= ((size) >> bits))
3166 /* first, try to predict filesize */
3167 /* XXX: should this table be tunable? */
3169 if (size
<= 16 * 1024) {
3171 } else if (size
<= 32 * 1024) {
3173 } else if (size
<= 64 * 1024) {
3175 } else if (size
<= 128 * 1024) {
3177 } else if (size
<= 256 * 1024) {
3179 } else if (size
<= 512 * 1024) {
3181 } else if (size
<= 1024 * 1024) {
3183 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, bsbits
)) {
3184 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3185 (20 - bsbits
)) << 20;
3187 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, bsbits
)) {
3188 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3189 (22 - bsbits
)) << 22;
3190 size
= 4 * 1024 * 1024;
3191 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3192 (8<<20)>>bsbits
, max
, bsbits
)) {
3193 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3194 (23 - bsbits
)) << 23;
3195 size
= 8 * 1024 * 1024;
3197 start_off
= (loff_t
)ac
->ac_o_ex
.fe_logical
<< bsbits
;
3198 size
= ac
->ac_o_ex
.fe_len
<< bsbits
;
3200 orig_size
= size
= size
>> bsbits
;
3201 orig_start
= start
= start_off
>> bsbits
;
3203 /* don't cover already allocated blocks in selected range */
3204 if (ar
->pleft
&& start
<= ar
->lleft
) {
3205 size
-= ar
->lleft
+ 1 - start
;
3206 start
= ar
->lleft
+ 1;
3208 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3209 size
-= start
+ size
- ar
->lright
;
3213 /* check we don't cross already preallocated blocks */
3215 list_for_each_rcu(cur
, &ei
->i_prealloc_list
) {
3216 struct ext4_prealloc_space
*pa
;
3217 unsigned long pa_end
;
3219 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3223 spin_lock(&pa
->pa_lock
);
3224 if (pa
->pa_deleted
) {
3225 spin_unlock(&pa
->pa_lock
);
3229 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3231 /* PA must not overlap original request */
3232 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3233 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3235 /* skip PA normalized request doesn't overlap with */
3236 if (pa
->pa_lstart
>= end
) {
3237 spin_unlock(&pa
->pa_lock
);
3240 if (pa_end
<= start
) {
3241 spin_unlock(&pa
->pa_lock
);
3244 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3246 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3247 BUG_ON(pa_end
< start
);
3251 if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3252 BUG_ON(pa
->pa_lstart
> end
);
3253 end
= pa
->pa_lstart
;
3255 spin_unlock(&pa
->pa_lock
);
3260 /* XXX: extra loop to check we really don't overlap preallocations */
3262 list_for_each_rcu(cur
, &ei
->i_prealloc_list
) {
3263 struct ext4_prealloc_space
*pa
;
3264 unsigned long pa_end
;
3265 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3266 spin_lock(&pa
->pa_lock
);
3267 if (pa
->pa_deleted
== 0) {
3268 pa_end
= pa
->pa_lstart
+ pa
->pa_len
;
3269 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3271 spin_unlock(&pa
->pa_lock
);
3275 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3276 start
> ac
->ac_o_ex
.fe_logical
) {
3277 printk(KERN_ERR
"start %lu, size %lu, fe_logical %lu\n",
3278 (unsigned long) start
, (unsigned long) size
,
3279 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3281 BUG_ON(start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3282 start
> ac
->ac_o_ex
.fe_logical
);
3283 BUG_ON(size
<= 0 || size
>= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3285 /* now prepare goal request */
3287 /* XXX: is it better to align blocks WRT to logical
3288 * placement or satisfy big request as is */
3289 ac
->ac_g_ex
.fe_logical
= start
;
3290 ac
->ac_g_ex
.fe_len
= size
;
3292 /* define goal start in order to merge */
3293 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3294 /* merge to the right */
3295 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3296 &ac
->ac_f_ex
.fe_group
,
3297 &ac
->ac_f_ex
.fe_start
);
3298 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3300 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3301 /* merge to the left */
3302 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3303 &ac
->ac_f_ex
.fe_group
,
3304 &ac
->ac_f_ex
.fe_start
);
3305 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3308 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3309 (unsigned) orig_size
, (unsigned) start
);
3312 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3314 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3316 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3317 atomic_inc(&sbi
->s_bal_reqs
);
3318 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3319 if (ac
->ac_o_ex
.fe_len
>= ac
->ac_g_ex
.fe_len
)
3320 atomic_inc(&sbi
->s_bal_success
);
3321 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3322 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3323 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3324 atomic_inc(&sbi
->s_bal_goals
);
3325 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3326 atomic_inc(&sbi
->s_bal_breaks
);
3329 ext4_mb_store_history(ac
);
3333 * use blocks preallocated to inode
3335 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3336 struct ext4_prealloc_space
*pa
)
3342 /* found preallocated blocks, use them */
3343 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3344 end
= min(pa
->pa_pstart
+ pa
->pa_len
, start
+ ac
->ac_o_ex
.fe_len
);
3346 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3347 &ac
->ac_b_ex
.fe_start
);
3348 ac
->ac_b_ex
.fe_len
= len
;
3349 ac
->ac_status
= AC_STATUS_FOUND
;
3352 BUG_ON(start
< pa
->pa_pstart
);
3353 BUG_ON(start
+ len
> pa
->pa_pstart
+ pa
->pa_len
);
3354 BUG_ON(pa
->pa_free
< len
);
3357 mb_debug("use %llu/%lu from inode pa %p\n", start
, len
, pa
);
3361 * use blocks preallocated to locality group
3363 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3364 struct ext4_prealloc_space
*pa
)
3366 unsigned len
= ac
->ac_o_ex
.fe_len
;
3368 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3369 &ac
->ac_b_ex
.fe_group
,
3370 &ac
->ac_b_ex
.fe_start
);
3371 ac
->ac_b_ex
.fe_len
= len
;
3372 ac
->ac_status
= AC_STATUS_FOUND
;
3375 /* we don't correct pa_pstart or pa_plen here to avoid
3376 * possible race when the group is being loaded concurrently
3377 * instead we correct pa later, after blocks are marked
3378 * in on-disk bitmap -- see ext4_mb_release_context()
3379 * Other CPUs are prevented from allocating from this pa by lg_mutex
3381 mb_debug("use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3385 * search goal blocks in preallocated space
3387 static int ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3389 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3390 struct ext4_locality_group
*lg
;
3391 struct ext4_prealloc_space
*pa
;
3392 struct list_head
*cur
;
3394 /* only data can be preallocated */
3395 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3398 /* first, try per-file preallocation */
3400 list_for_each_rcu(cur
, &ei
->i_prealloc_list
) {
3401 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3403 /* all fields in this condition don't change,
3404 * so we can skip locking for them */
3405 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3406 ac
->ac_o_ex
.fe_logical
>= pa
->pa_lstart
+ pa
->pa_len
)
3409 /* found preallocated blocks, use them */
3410 spin_lock(&pa
->pa_lock
);
3411 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3412 atomic_inc(&pa
->pa_count
);
3413 ext4_mb_use_inode_pa(ac
, pa
);
3414 spin_unlock(&pa
->pa_lock
);
3415 ac
->ac_criteria
= 10;
3419 spin_unlock(&pa
->pa_lock
);
3423 /* can we use group allocation? */
3424 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3427 /* inode may have no locality group for some reason */
3433 list_for_each_rcu(cur
, &lg
->lg_prealloc_list
) {
3434 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_inode_list
);
3435 spin_lock(&pa
->pa_lock
);
3436 if (pa
->pa_deleted
== 0 && pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3437 atomic_inc(&pa
->pa_count
);
3438 ext4_mb_use_group_pa(ac
, pa
);
3439 spin_unlock(&pa
->pa_lock
);
3440 ac
->ac_criteria
= 20;
3444 spin_unlock(&pa
->pa_lock
);
3452 * the function goes through all preallocation in this group and marks them
3453 * used in in-core bitmap. buddy must be generated from this bitmap
3454 * Need to be called with ext4 group lock (ext4_lock_group)
3456 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3459 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3460 struct ext4_prealloc_space
*pa
;
3461 struct list_head
*cur
;
3462 ext4_group_t groupnr
;
3463 ext4_grpblk_t start
;
3464 int preallocated
= 0;
3468 /* all form of preallocation discards first load group,
3469 * so the only competing code is preallocation use.
3470 * we don't need any locking here
3471 * notice we do NOT ignore preallocations with pa_deleted
3472 * otherwise we could leave used blocks available for
3473 * allocation in buddy when concurrent ext4_mb_put_pa()
3474 * is dropping preallocation
3476 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3477 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3478 spin_lock(&pa
->pa_lock
);
3479 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3482 spin_unlock(&pa
->pa_lock
);
3483 if (unlikely(len
== 0))
3485 BUG_ON(groupnr
!= group
);
3486 mb_set_bits(sb_bgl_lock(EXT4_SB(sb
), group
),
3487 bitmap
, start
, len
);
3488 preallocated
+= len
;
3491 mb_debug("prellocated %u for group %lu\n", preallocated
, group
);
3494 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3496 struct ext4_prealloc_space
*pa
;
3497 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3498 kmem_cache_free(ext4_pspace_cachep
, pa
);
3502 * drops a reference to preallocated space descriptor
3503 * if this was the last reference and the space is consumed
3505 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3506 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3510 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0)
3513 /* in this short window concurrent discard can set pa_deleted */
3514 spin_lock(&pa
->pa_lock
);
3515 if (pa
->pa_deleted
== 1) {
3516 spin_unlock(&pa
->pa_lock
);
3521 spin_unlock(&pa
->pa_lock
);
3523 /* -1 is to protect from crossing allocation group */
3524 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
- 1, &grp
, NULL
);
3529 * P1 (buddy init) P2 (regular allocation)
3530 * find block B in PA
3531 * copy on-disk bitmap to buddy
3532 * mark B in on-disk bitmap
3533 * drop PA from group
3534 * mark all PAs in buddy
3536 * thus, P1 initializes buddy with B available. to prevent this
3537 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3540 ext4_lock_group(sb
, grp
);
3541 list_del(&pa
->pa_group_list
);
3542 ext4_unlock_group(sb
, grp
);
3544 spin_lock(pa
->pa_obj_lock
);
3545 list_del_rcu(&pa
->pa_inode_list
);
3546 spin_unlock(pa
->pa_obj_lock
);
3548 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3552 * creates new preallocated space for given inode
3554 static int ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3556 struct super_block
*sb
= ac
->ac_sb
;
3557 struct ext4_prealloc_space
*pa
;
3558 struct ext4_group_info
*grp
;
3559 struct ext4_inode_info
*ei
;
3561 /* preallocate only when found space is larger then requested */
3562 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3563 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3564 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3566 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3570 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3576 /* we can't allocate as much as normalizer wants.
3577 * so, found space must get proper lstart
3578 * to cover original request */
3579 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3580 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3582 /* we're limited by original request in that
3583 * logical block must be covered any way
3584 * winl is window we can move our chunk within */
3585 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3587 /* also, we should cover whole original request */
3588 wins
= ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
;
3590 /* the smallest one defines real window */
3591 win
= min(winl
, wins
);
3593 offs
= ac
->ac_o_ex
.fe_logical
% ac
->ac_b_ex
.fe_len
;
3594 if (offs
&& offs
< win
)
3597 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
- win
;
3598 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3599 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3602 /* preallocation can change ac_b_ex, thus we store actually
3603 * allocated blocks for history */
3604 ac
->ac_f_ex
= ac
->ac_b_ex
;
3606 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3607 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3608 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3609 pa
->pa_free
= pa
->pa_len
;
3610 atomic_set(&pa
->pa_count
, 1);
3611 spin_lock_init(&pa
->pa_lock
);
3615 mb_debug("new inode pa %p: %llu/%u for %u\n", pa
,
3616 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3618 ext4_mb_use_inode_pa(ac
, pa
);
3619 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3621 ei
= EXT4_I(ac
->ac_inode
);
3622 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3624 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3625 pa
->pa_inode
= ac
->ac_inode
;
3627 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3628 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3629 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3631 spin_lock(pa
->pa_obj_lock
);
3632 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3633 spin_unlock(pa
->pa_obj_lock
);
3639 * creates new preallocated space for locality group inodes belongs to
3641 static int ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3643 struct super_block
*sb
= ac
->ac_sb
;
3644 struct ext4_locality_group
*lg
;
3645 struct ext4_prealloc_space
*pa
;
3646 struct ext4_group_info
*grp
;
3648 /* preallocate only when found space is larger then requested */
3649 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3650 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3651 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3653 BUG_ON(ext4_pspace_cachep
== NULL
);
3654 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3658 /* preallocation can change ac_b_ex, thus we store actually
3659 * allocated blocks for history */
3660 ac
->ac_f_ex
= ac
->ac_b_ex
;
3662 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3663 pa
->pa_lstart
= pa
->pa_pstart
;
3664 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3665 pa
->pa_free
= pa
->pa_len
;
3666 atomic_set(&pa
->pa_count
, 1);
3667 spin_lock_init(&pa
->pa_lock
);
3671 mb_debug("new group pa %p: %llu/%u for %u\n", pa
,
3672 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3674 ext4_mb_use_group_pa(ac
, pa
);
3675 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3677 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3681 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3682 pa
->pa_inode
= NULL
;
3684 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3685 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3686 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3688 spin_lock(pa
->pa_obj_lock
);
3689 list_add_tail_rcu(&pa
->pa_inode_list
, &lg
->lg_prealloc_list
);
3690 spin_unlock(pa
->pa_obj_lock
);
3695 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3699 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3700 err
= ext4_mb_new_group_pa(ac
);
3702 err
= ext4_mb_new_inode_pa(ac
);
3707 * finds all unused blocks in on-disk bitmap, frees them in
3708 * in-core bitmap and buddy.
3709 * @pa must be unlinked from inode and group lists, so that
3710 * nobody else can find/use it.
3711 * the caller MUST hold group/inode locks.
3712 * TODO: optimize the case when there are no in-core structures yet
3714 static int ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
,
3715 struct buffer_head
*bitmap_bh
,
3716 struct ext4_prealloc_space
*pa
)
3718 struct ext4_allocation_context
*ac
;
3719 struct super_block
*sb
= e4b
->bd_sb
;
3720 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3729 BUG_ON(pa
->pa_deleted
== 0);
3730 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3731 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3732 end
= bit
+ pa
->pa_len
;
3734 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3738 ac
->ac_inode
= pa
->pa_inode
;
3739 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3743 bit
= ext4_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3746 next
= ext4_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3749 start
= group
* EXT4_BLOCKS_PER_GROUP(sb
) + bit
+
3750 le32_to_cpu(sbi
->s_es
->s_first_data_block
);
3751 mb_debug(" free preallocated %u/%u in group %u\n",
3752 (unsigned) start
, (unsigned) next
- bit
,
3757 ac
->ac_b_ex
.fe_group
= group
;
3758 ac
->ac_b_ex
.fe_start
= bit
;
3759 ac
->ac_b_ex
.fe_len
= next
- bit
;
3760 ac
->ac_b_ex
.fe_logical
= 0;
3761 ext4_mb_store_history(ac
);
3764 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3767 if (free
!= pa
->pa_free
) {
3768 printk(KERN_CRIT
"pa %p: logic %lu, phys. %lu, len %lu\n",
3769 pa
, (unsigned long) pa
->pa_lstart
,
3770 (unsigned long) pa
->pa_pstart
,
3771 (unsigned long) pa
->pa_len
);
3772 ext4_error(sb
, __FUNCTION__
, "free %u, pa_free %u\n",
3775 atomic_add(free
, &sbi
->s_mb_discarded
);
3777 kmem_cache_free(ext4_ac_cachep
, ac
);
3782 static int ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3783 struct ext4_prealloc_space
*pa
)
3785 struct ext4_allocation_context
*ac
;
3786 struct super_block
*sb
= e4b
->bd_sb
;
3790 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
3793 ac
->ac_op
= EXT4_MB_HISTORY_DISCARD
;
3795 BUG_ON(pa
->pa_deleted
== 0);
3796 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3797 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3798 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3799 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3803 ac
->ac_inode
= NULL
;
3804 ac
->ac_b_ex
.fe_group
= group
;
3805 ac
->ac_b_ex
.fe_start
= bit
;
3806 ac
->ac_b_ex
.fe_len
= pa
->pa_len
;
3807 ac
->ac_b_ex
.fe_logical
= 0;
3808 ext4_mb_store_history(ac
);
3809 kmem_cache_free(ext4_ac_cachep
, ac
);
3816 * releases all preallocations in given group
3818 * first, we need to decide discard policy:
3819 * - when do we discard
3821 * - how many do we discard
3822 * 1) how many requested
3824 static int ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3825 ext4_group_t group
, int needed
)
3827 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3828 struct buffer_head
*bitmap_bh
= NULL
;
3829 struct ext4_prealloc_space
*pa
, *tmp
;
3830 struct list_head list
;
3831 struct ext4_buddy e4b
;
3836 mb_debug("discard preallocation for group %lu\n", group
);
3838 if (list_empty(&grp
->bb_prealloc_list
))
3841 bitmap_bh
= read_block_bitmap(sb
, group
);
3842 if (bitmap_bh
== NULL
) {
3843 /* error handling here */
3844 ext4_mb_release_desc(&e4b
);
3845 BUG_ON(bitmap_bh
== NULL
);
3848 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3849 BUG_ON(err
!= 0); /* error handling here */
3852 needed
= EXT4_BLOCKS_PER_GROUP(sb
) + 1;
3854 grp
= ext4_get_group_info(sb
, group
);
3855 INIT_LIST_HEAD(&list
);
3858 ext4_lock_group(sb
, group
);
3859 list_for_each_entry_safe(pa
, tmp
,
3860 &grp
->bb_prealloc_list
, pa_group_list
) {
3861 spin_lock(&pa
->pa_lock
);
3862 if (atomic_read(&pa
->pa_count
)) {
3863 spin_unlock(&pa
->pa_lock
);
3867 if (pa
->pa_deleted
) {
3868 spin_unlock(&pa
->pa_lock
);
3872 /* seems this one can be freed ... */
3875 /* we can trust pa_free ... */
3876 free
+= pa
->pa_free
;
3878 spin_unlock(&pa
->pa_lock
);
3880 list_del(&pa
->pa_group_list
);
3881 list_add(&pa
->u
.pa_tmp_list
, &list
);
3884 /* if we still need more blocks and some PAs were used, try again */
3885 if (free
< needed
&& busy
) {
3887 ext4_unlock_group(sb
, group
);
3889 * Yield the CPU here so that we don't get soft lockup
3890 * in non preempt case.
3896 /* found anything to free? */
3897 if (list_empty(&list
)) {
3902 /* now free all selected PAs */
3903 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3905 /* remove from object (inode or locality group) */
3906 spin_lock(pa
->pa_obj_lock
);
3907 list_del_rcu(&pa
->pa_inode_list
);
3908 spin_unlock(pa
->pa_obj_lock
);
3911 ext4_mb_release_group_pa(&e4b
, pa
);
3913 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3915 list_del(&pa
->u
.pa_tmp_list
);
3916 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3920 ext4_unlock_group(sb
, group
);
3921 ext4_mb_release_desc(&e4b
);
3927 * releases all non-used preallocated blocks for given inode
3929 * It's important to discard preallocations under i_data_sem
3930 * We don't want another block to be served from the prealloc
3931 * space when we are discarding the inode prealloc space.
3933 * FIXME!! Make sure it is valid at all the call sites
3935 void ext4_mb_discard_inode_preallocations(struct inode
*inode
)
3937 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3938 struct super_block
*sb
= inode
->i_sb
;
3939 struct buffer_head
*bitmap_bh
= NULL
;
3940 struct ext4_prealloc_space
*pa
, *tmp
;
3941 ext4_group_t group
= 0;
3942 struct list_head list
;
3943 struct ext4_buddy e4b
;
3946 if (!test_opt(sb
, MBALLOC
) || !S_ISREG(inode
->i_mode
)) {
3947 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3951 mb_debug("discard preallocation for inode %lu\n", inode
->i_ino
);
3953 INIT_LIST_HEAD(&list
);
3956 /* first, collect all pa's in the inode */
3957 spin_lock(&ei
->i_prealloc_lock
);
3958 while (!list_empty(&ei
->i_prealloc_list
)) {
3959 pa
= list_entry(ei
->i_prealloc_list
.next
,
3960 struct ext4_prealloc_space
, pa_inode_list
);
3961 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
3962 spin_lock(&pa
->pa_lock
);
3963 if (atomic_read(&pa
->pa_count
)) {
3964 /* this shouldn't happen often - nobody should
3965 * use preallocation while we're discarding it */
3966 spin_unlock(&pa
->pa_lock
);
3967 spin_unlock(&ei
->i_prealloc_lock
);
3968 printk(KERN_ERR
"uh-oh! used pa while discarding\n");
3970 schedule_timeout_uninterruptible(HZ
);
3974 if (pa
->pa_deleted
== 0) {
3976 spin_unlock(&pa
->pa_lock
);
3977 list_del_rcu(&pa
->pa_inode_list
);
3978 list_add(&pa
->u
.pa_tmp_list
, &list
);
3982 /* someone is deleting pa right now */
3983 spin_unlock(&pa
->pa_lock
);
3984 spin_unlock(&ei
->i_prealloc_lock
);
3986 /* we have to wait here because pa_deleted
3987 * doesn't mean pa is already unlinked from
3988 * the list. as we might be called from
3989 * ->clear_inode() the inode will get freed
3990 * and concurrent thread which is unlinking
3991 * pa from inode's list may access already
3992 * freed memory, bad-bad-bad */
3994 /* XXX: if this happens too often, we can
3995 * add a flag to force wait only in case
3996 * of ->clear_inode(), but not in case of
3997 * regular truncate */
3998 schedule_timeout_uninterruptible(HZ
);
4001 spin_unlock(&ei
->i_prealloc_lock
);
4003 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4004 BUG_ON(pa
->pa_linear
!= 0);
4005 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, NULL
);
4007 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4008 BUG_ON(err
!= 0); /* error handling here */
4010 bitmap_bh
= read_block_bitmap(sb
, group
);
4011 if (bitmap_bh
== NULL
) {
4012 /* error handling here */
4013 ext4_mb_release_desc(&e4b
);
4014 BUG_ON(bitmap_bh
== NULL
);
4017 ext4_lock_group(sb
, group
);
4018 list_del(&pa
->pa_group_list
);
4019 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
4020 ext4_unlock_group(sb
, group
);
4022 ext4_mb_release_desc(&e4b
);
4025 list_del(&pa
->u
.pa_tmp_list
);
4026 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4031 * finds all preallocated spaces and return blocks being freed to them
4032 * if preallocated space becomes full (no block is used from the space)
4033 * then the function frees space in buddy
4034 * XXX: at the moment, truncate (which is the only way to free blocks)
4035 * discards all preallocations
4037 static void ext4_mb_return_to_preallocation(struct inode
*inode
,
4038 struct ext4_buddy
*e4b
,
4039 sector_t block
, int count
)
4041 BUG_ON(!list_empty(&EXT4_I(inode
)->i_prealloc_list
));
4044 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4046 struct super_block
*sb
= ac
->ac_sb
;
4049 printk(KERN_ERR
"EXT4-fs: Can't allocate:"
4050 " Allocation context details:\n");
4051 printk(KERN_ERR
"EXT4-fs: status %d flags %d\n",
4052 ac
->ac_status
, ac
->ac_flags
);
4053 printk(KERN_ERR
"EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4054 "best %lu/%lu/%lu@%lu cr %d\n",
4055 (unsigned long)ac
->ac_o_ex
.fe_group
,
4056 (unsigned long)ac
->ac_o_ex
.fe_start
,
4057 (unsigned long)ac
->ac_o_ex
.fe_len
,
4058 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4059 (unsigned long)ac
->ac_g_ex
.fe_group
,
4060 (unsigned long)ac
->ac_g_ex
.fe_start
,
4061 (unsigned long)ac
->ac_g_ex
.fe_len
,
4062 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4063 (unsigned long)ac
->ac_b_ex
.fe_group
,
4064 (unsigned long)ac
->ac_b_ex
.fe_start
,
4065 (unsigned long)ac
->ac_b_ex
.fe_len
,
4066 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4067 (int)ac
->ac_criteria
);
4068 printk(KERN_ERR
"EXT4-fs: %lu scanned, %d found\n", ac
->ac_ex_scanned
,
4070 printk(KERN_ERR
"EXT4-fs: groups: \n");
4071 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
; i
++) {
4072 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4073 struct ext4_prealloc_space
*pa
;
4074 ext4_grpblk_t start
;
4075 struct list_head
*cur
;
4076 ext4_lock_group(sb
, i
);
4077 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4078 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4080 spin_lock(&pa
->pa_lock
);
4081 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4083 spin_unlock(&pa
->pa_lock
);
4084 printk(KERN_ERR
"PA:%lu:%d:%u \n", i
,
4087 ext4_lock_group(sb
, i
);
4089 if (grp
->bb_free
== 0)
4091 printk(KERN_ERR
"%lu: %d/%d \n",
4092 i
, grp
->bb_free
, grp
->bb_fragments
);
4094 printk(KERN_ERR
"\n");
4097 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4104 * We use locality group preallocation for small size file. The size of the
4105 * file is determined by the current size or the resulting size after
4106 * allocation which ever is larger
4108 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4110 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4112 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4113 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4116 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4119 size
= ac
->ac_o_ex
.fe_logical
+ ac
->ac_o_ex
.fe_len
;
4120 isize
= i_size_read(ac
->ac_inode
) >> bsbits
;
4121 size
= max(size
, isize
);
4123 /* don't use group allocation for large files */
4124 if (size
>= sbi
->s_mb_stream_request
)
4127 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4130 BUG_ON(ac
->ac_lg
!= NULL
);
4132 * locality group prealloc space are per cpu. The reason for having
4133 * per cpu locality group is to reduce the contention between block
4134 * request from multiple CPUs.
4136 ac
->ac_lg
= &sbi
->s_locality_groups
[get_cpu()];
4139 /* we're going to use group allocation */
4140 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4142 /* serialize all allocations in the group */
4143 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4146 static int ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4147 struct ext4_allocation_request
*ar
)
4149 struct super_block
*sb
= ar
->inode
->i_sb
;
4150 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4151 struct ext4_super_block
*es
= sbi
->s_es
;
4155 ext4_grpblk_t block
;
4157 /* we can't allocate > group size */
4160 /* just a dirty hack to filter too big requests */
4161 if (len
>= EXT4_BLOCKS_PER_GROUP(sb
) - 10)
4162 len
= EXT4_BLOCKS_PER_GROUP(sb
) - 10;
4164 /* start searching from the goal */
4166 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4167 goal
>= ext4_blocks_count(es
))
4168 goal
= le32_to_cpu(es
->s_first_data_block
);
4169 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4171 /* set up allocation goals */
4172 ac
->ac_b_ex
.fe_logical
= ar
->logical
;
4173 ac
->ac_b_ex
.fe_group
= 0;
4174 ac
->ac_b_ex
.fe_start
= 0;
4175 ac
->ac_b_ex
.fe_len
= 0;
4176 ac
->ac_status
= AC_STATUS_CONTINUE
;
4177 ac
->ac_groups_scanned
= 0;
4178 ac
->ac_ex_scanned
= 0;
4181 ac
->ac_inode
= ar
->inode
;
4182 ac
->ac_o_ex
.fe_logical
= ar
->logical
;
4183 ac
->ac_o_ex
.fe_group
= group
;
4184 ac
->ac_o_ex
.fe_start
= block
;
4185 ac
->ac_o_ex
.fe_len
= len
;
4186 ac
->ac_g_ex
.fe_logical
= ar
->logical
;
4187 ac
->ac_g_ex
.fe_group
= group
;
4188 ac
->ac_g_ex
.fe_start
= block
;
4189 ac
->ac_g_ex
.fe_len
= len
;
4190 ac
->ac_f_ex
.fe_len
= 0;
4191 ac
->ac_flags
= ar
->flags
;
4193 ac
->ac_criteria
= 0;
4195 ac
->ac_bitmap_page
= NULL
;
4196 ac
->ac_buddy_page
= NULL
;
4199 /* we have to define context: we'll we work with a file or
4200 * locality group. this is a policy, actually */
4201 ext4_mb_group_or_file(ac
);
4203 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4204 "left: %u/%u, right %u/%u to %swritable\n",
4205 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4206 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4207 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4208 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4209 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4215 * release all resource we used in allocation
4217 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4220 if (ac
->ac_pa
->pa_linear
) {
4221 /* see comment in ext4_mb_use_group_pa() */
4222 spin_lock(&ac
->ac_pa
->pa_lock
);
4223 ac
->ac_pa
->pa_pstart
+= ac
->ac_b_ex
.fe_len
;
4224 ac
->ac_pa
->pa_lstart
+= ac
->ac_b_ex
.fe_len
;
4225 ac
->ac_pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4226 ac
->ac_pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4227 spin_unlock(&ac
->ac_pa
->pa_lock
);
4229 ext4_mb_put_pa(ac
, ac
->ac_sb
, ac
->ac_pa
);
4231 if (ac
->ac_bitmap_page
)
4232 page_cache_release(ac
->ac_bitmap_page
);
4233 if (ac
->ac_buddy_page
)
4234 page_cache_release(ac
->ac_buddy_page
);
4235 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4236 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4237 ext4_mb_collect_stats(ac
);
4241 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4247 for (i
= 0; i
< EXT4_SB(sb
)->s_groups_count
&& needed
> 0; i
++) {
4248 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4257 * Main entry point into mballoc to allocate blocks
4258 * it tries to use preallocation first, then falls back
4259 * to usual allocation
4261 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4262 struct ext4_allocation_request
*ar
, int *errp
)
4264 struct ext4_allocation_context
*ac
= NULL
;
4265 struct ext4_sb_info
*sbi
;
4266 struct super_block
*sb
;
4267 ext4_fsblk_t block
= 0;
4271 sb
= ar
->inode
->i_sb
;
4274 if (!test_opt(sb
, MBALLOC
)) {
4275 block
= ext4_new_blocks_old(handle
, ar
->inode
, ar
->goal
,
4280 while (ar
->len
&& DQUOT_ALLOC_BLOCK(ar
->inode
, ar
->len
)) {
4281 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4290 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4296 ext4_mb_poll_new_transaction(sb
, handle
);
4298 *errp
= ext4_mb_initialize_context(ac
, ar
);
4304 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4305 if (!ext4_mb_use_preallocated(ac
)) {
4307 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4308 ext4_mb_normalize_request(ac
, ar
);
4311 /* allocate space in core */
4312 ext4_mb_regular_allocator(ac
);
4314 /* as we've just preallocated more space than
4315 * user requested orinally, we store allocated
4316 * space in a special descriptor */
4317 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4318 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4319 ext4_mb_new_preallocation(ac
);
4322 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4323 ext4_mb_mark_diskspace_used(ac
, handle
);
4325 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4326 ar
->len
= ac
->ac_b_ex
.fe_len
;
4328 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4332 ac
->ac_b_ex
.fe_len
= 0;
4334 ext4_mb_show_ac(ac
);
4337 ext4_mb_release_context(ac
);
4340 if (ar
->len
< inquota
)
4341 DQUOT_FREE_BLOCK(ar
->inode
, inquota
- ar
->len
);
4343 kmem_cache_free(ext4_ac_cachep
, ac
);
4346 static void ext4_mb_poll_new_transaction(struct super_block
*sb
,
4349 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4351 if (sbi
->s_last_transaction
== handle
->h_transaction
->t_tid
)
4354 /* new transaction! time to close last one and free blocks for
4355 * committed transaction. we know that only transaction can be
4356 * active, so previos transaction can be being logged and we
4357 * know that transaction before previous is known to be already
4358 * logged. this means that now we may free blocks freed in all
4359 * transactions before previous one. hope I'm clear enough ... */
4361 spin_lock(&sbi
->s_md_lock
);
4362 if (sbi
->s_last_transaction
!= handle
->h_transaction
->t_tid
) {
4363 mb_debug("new transaction %lu, old %lu\n",
4364 (unsigned long) handle
->h_transaction
->t_tid
,
4365 (unsigned long) sbi
->s_last_transaction
);
4366 list_splice_init(&sbi
->s_closed_transaction
,
4367 &sbi
->s_committed_transaction
);
4368 list_splice_init(&sbi
->s_active_transaction
,
4369 &sbi
->s_closed_transaction
);
4370 sbi
->s_last_transaction
= handle
->h_transaction
->t_tid
;
4372 spin_unlock(&sbi
->s_md_lock
);
4374 ext4_mb_free_committed_blocks(sb
);
4377 static int ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4378 ext4_group_t group
, ext4_grpblk_t block
, int count
)
4380 struct ext4_group_info
*db
= e4b
->bd_info
;
4381 struct super_block
*sb
= e4b
->bd_sb
;
4382 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4383 struct ext4_free_metadata
*md
;
4386 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4387 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4389 ext4_lock_group(sb
, group
);
4390 for (i
= 0; i
< count
; i
++) {
4392 if (md
&& db
->bb_tid
!= handle
->h_transaction
->t_tid
) {
4393 db
->bb_md_cur
= NULL
;
4398 ext4_unlock_group(sb
, group
);
4399 md
= kmalloc(sizeof(*md
), GFP_NOFS
);
4405 ext4_lock_group(sb
, group
);
4406 if (db
->bb_md_cur
== NULL
) {
4407 spin_lock(&sbi
->s_md_lock
);
4408 list_add(&md
->list
, &sbi
->s_active_transaction
);
4409 spin_unlock(&sbi
->s_md_lock
);
4410 /* protect buddy cache from being freed,
4411 * otherwise we'll refresh it from
4412 * on-disk bitmap and lose not-yet-available
4414 page_cache_get(e4b
->bd_buddy_page
);
4415 page_cache_get(e4b
->bd_bitmap_page
);
4417 db
->bb_tid
= handle
->h_transaction
->t_tid
;
4418 mb_debug("new md 0x%p for group %lu\n",
4426 BUG_ON(md
->num
>= EXT4_BB_MAX_BLOCKS
);
4427 md
->blocks
[md
->num
] = block
+ i
;
4429 if (md
->num
== EXT4_BB_MAX_BLOCKS
) {
4430 /* no more space, put full container on a sb's list */
4431 db
->bb_md_cur
= NULL
;
4434 ext4_unlock_group(sb
, group
);
4439 * Main entry point into mballoc to free blocks
4441 void ext4_mb_free_blocks(handle_t
*handle
, struct inode
*inode
,
4442 unsigned long block
, unsigned long count
,
4443 int metadata
, unsigned long *freed
)
4445 struct buffer_head
*bitmap_bh
= NULL
;
4446 struct super_block
*sb
= inode
->i_sb
;
4447 struct ext4_allocation_context
*ac
= NULL
;
4448 struct ext4_group_desc
*gdp
;
4449 struct ext4_super_block
*es
;
4450 unsigned long overflow
;
4452 struct buffer_head
*gd_bh
;
4453 ext4_group_t block_group
;
4454 struct ext4_sb_info
*sbi
;
4455 struct ext4_buddy e4b
;
4461 ext4_mb_poll_new_transaction(sb
, handle
);
4464 es
= EXT4_SB(sb
)->s_es
;
4465 if (block
< le32_to_cpu(es
->s_first_data_block
) ||
4466 block
+ count
< block
||
4467 block
+ count
> ext4_blocks_count(es
)) {
4468 ext4_error(sb
, __FUNCTION__
,
4469 "Freeing blocks not in datazone - "
4470 "block = %lu, count = %lu", block
, count
);
4474 ext4_debug("freeing block %lu\n", block
);
4476 ac
= kmem_cache_alloc(ext4_ac_cachep
, GFP_NOFS
);
4478 ac
->ac_op
= EXT4_MB_HISTORY_FREE
;
4479 ac
->ac_inode
= inode
;
4485 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4488 * Check to see if we are freeing blocks across a group
4491 if (bit
+ count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4492 overflow
= bit
+ count
- EXT4_BLOCKS_PER_GROUP(sb
);
4495 bitmap_bh
= read_block_bitmap(sb
, block_group
);
4498 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4502 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4503 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4504 in_range(block
, ext4_inode_table(sb
, gdp
),
4505 EXT4_SB(sb
)->s_itb_per_group
) ||
4506 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4507 EXT4_SB(sb
)->s_itb_per_group
)) {
4509 ext4_error(sb
, __FUNCTION__
,
4510 "Freeing blocks in system zone - "
4511 "Block = %lu, count = %lu", block
, count
);
4514 BUFFER_TRACE(bitmap_bh
, "getting write access");
4515 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4520 * We are about to modify some metadata. Call the journal APIs
4521 * to unshare ->b_data if a currently-committing transaction is
4524 BUFFER_TRACE(gd_bh
, "get_write_access");
4525 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4529 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
4533 #ifdef AGGRESSIVE_CHECK
4536 for (i
= 0; i
< count
; i
++)
4537 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4540 mb_clear_bits(sb_bgl_lock(sbi
, block_group
), bitmap_bh
->b_data
,
4543 /* We dirtied the bitmap block */
4544 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4545 err
= ext4_journal_dirty_metadata(handle
, bitmap_bh
);
4548 ac
->ac_b_ex
.fe_group
= block_group
;
4549 ac
->ac_b_ex
.fe_start
= bit
;
4550 ac
->ac_b_ex
.fe_len
= count
;
4551 ext4_mb_store_history(ac
);
4555 /* blocks being freed are metadata. these blocks shouldn't
4556 * be used until this transaction is committed */
4557 ext4_mb_free_metadata(handle
, &e4b
, block_group
, bit
, count
);
4559 ext4_lock_group(sb
, block_group
);
4560 err
= mb_free_blocks(inode
, &e4b
, bit
, count
);
4561 ext4_mb_return_to_preallocation(inode
, &e4b
, block
, count
);
4562 ext4_unlock_group(sb
, block_group
);
4566 spin_lock(sb_bgl_lock(sbi
, block_group
));
4567 gdp
->bg_free_blocks_count
=
4568 cpu_to_le16(le16_to_cpu(gdp
->bg_free_blocks_count
) + count
);
4569 gdp
->bg_checksum
= ext4_group_desc_csum(sbi
, block_group
, gdp
);
4570 spin_unlock(sb_bgl_lock(sbi
, block_group
));
4571 percpu_counter_add(&sbi
->s_freeblocks_counter
, count
);
4573 ext4_mb_release_desc(&e4b
);
4577 /* And the group descriptor block */
4578 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4579 ret
= ext4_journal_dirty_metadata(handle
, gd_bh
);
4583 if (overflow
&& !err
) {
4592 ext4_std_error(sb
, err
);
4594 kmem_cache_free(ext4_ac_cachep
, ac
);