1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
4 * Written by Alex Tomas <alex@clusterfs.com>
9 * mballoc.c contains the multiblocks allocation routines
12 #include "ext4_jbd2.h"
14 #include <linux/log2.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/nospec.h>
18 #include <linux/backing-dev.h>
19 #include <trace/events/ext4.h>
23 * - test ext4_ext_search_left() and ext4_ext_search_right()
24 * - search for metadata in few groups
27 * - normalization should take into account whether file is still open
28 * - discard preallocations if no free space left (policy?)
29 * - don't normalize tails
31 * - reservation for superuser
34 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
35 * - track min/max extents in each group for better group selection
36 * - mb_mark_used() may allocate chunk right after splitting buddy
37 * - tree of groups sorted by number of free blocks
42 * The allocation request involve request for multiple number of blocks
43 * near to the goal(block) value specified.
45 * During initialization phase of the allocator we decide to use the
46 * group preallocation or inode preallocation depending on the size of
47 * the file. The size of the file could be the resulting file size we
48 * would have after allocation, or the current file size, which ever
49 * is larger. If the size is less than sbi->s_mb_stream_request we
50 * select to use the group preallocation. The default value of
51 * s_mb_stream_request is 16 blocks. This can also be tuned via
52 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
53 * terms of number of blocks.
55 * The main motivation for having small file use group preallocation is to
56 * ensure that we have small files closer together on the disk.
58 * First stage the allocator looks at the inode prealloc list,
59 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
60 * spaces for this particular inode. The inode prealloc space is
63 * pa_lstart -> the logical start block for this prealloc space
64 * pa_pstart -> the physical start block for this prealloc space
65 * pa_len -> length for this prealloc space (in clusters)
66 * pa_free -> free space available in this prealloc space (in clusters)
68 * The inode preallocation space is used looking at the _logical_ start
69 * block. If only the logical file block falls within the range of prealloc
70 * space we will consume the particular prealloc space. This makes sure that
71 * we have contiguous physical blocks representing the file blocks
73 * The important thing to be noted in case of inode prealloc space is that
74 * we don't modify the values associated to inode prealloc space except
77 * If we are not able to find blocks in the inode prealloc space and if we
78 * have the group allocation flag set then we look at the locality group
79 * prealloc space. These are per CPU prealloc list represented as
81 * ext4_sb_info.s_locality_groups[smp_processor_id()]
83 * The reason for having a per cpu locality group is to reduce the contention
84 * between CPUs. It is possible to get scheduled at this point.
86 * The locality group prealloc space is used looking at whether we have
87 * enough free space (pa_free) within the prealloc space.
89 * If we can't allocate blocks via inode prealloc or/and locality group
90 * prealloc then we look at the buddy cache. The buddy cache is represented
91 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
92 * mapped to the buddy and bitmap information regarding different
93 * groups. The buddy information is attached to buddy cache inode so that
94 * we can access them through the page cache. The information regarding
95 * each group is loaded via ext4_mb_load_buddy. The information involve
96 * block bitmap and buddy information. The information are stored in the
100 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
103 * one block each for bitmap and buddy information. So for each group we
104 * take up 2 blocks. A page can contain blocks_per_page (PAGE_SIZE /
105 * blocksize) blocks. So it can have information regarding groups_per_page
106 * which is blocks_per_page/2
108 * The buddy cache inode is not stored on disk. The inode is thrown
109 * away when the filesystem is unmounted.
111 * We look for count number of blocks in the buddy cache. If we were able
112 * to locate that many free blocks we return with additional information
113 * regarding rest of the contiguous physical block available
115 * Before allocating blocks via buddy cache we normalize the request
116 * blocks. This ensure we ask for more blocks that we needed. The extra
117 * blocks that we get after allocation is added to the respective prealloc
118 * list. In case of inode preallocation we follow a list of heuristics
119 * based on file size. This can be found in ext4_mb_normalize_request. If
120 * we are doing a group prealloc we try to normalize the request to
121 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
122 * dependent on the cluster size; for non-bigalloc file systems, it is
123 * 512 blocks. This can be tuned via
124 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
125 * terms of number of blocks. If we have mounted the file system with -O
126 * stripe=<value> option the group prealloc request is normalized to the
127 * the smallest multiple of the stripe value (sbi->s_stripe) which is
128 * greater than the default mb_group_prealloc.
130 * The regular allocator (using the buddy cache) supports a few tunables.
132 * /sys/fs/ext4/<partition>/mb_min_to_scan
133 * /sys/fs/ext4/<partition>/mb_max_to_scan
134 * /sys/fs/ext4/<partition>/mb_order2_req
136 * The regular allocator uses buddy scan only if the request len is power of
137 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
138 * value of s_mb_order2_reqs can be tuned via
139 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
140 * stripe size (sbi->s_stripe), we try to search for contiguous block in
141 * stripe size. This should result in better allocation on RAID setups. If
142 * not, we search in the specific group using bitmap for best extents. The
143 * tunable min_to_scan and max_to_scan control the behaviour here.
144 * min_to_scan indicate how long the mballoc __must__ look for a best
145 * extent and max_to_scan indicates how long the mballoc __can__ look for a
146 * best extent in the found extents. Searching for the blocks starts with
147 * the group specified as the goal value in allocation context via
148 * ac_g_ex. Each group is first checked based on the criteria whether it
149 * can be used for allocation. ext4_mb_good_group explains how the groups are
152 * Both the prealloc space are getting populated as above. So for the first
153 * request we will hit the buddy cache which will result in this prealloc
154 * space getting filled. The prealloc space is then later used for the
155 * subsequent request.
159 * mballoc operates on the following data:
161 * - in-core buddy (actually includes buddy and bitmap)
162 * - preallocation descriptors (PAs)
164 * there are two types of preallocations:
166 * assiged to specific inode and can be used for this inode only.
167 * it describes part of inode's space preallocated to specific
168 * physical blocks. any block from that preallocated can be used
169 * independent. the descriptor just tracks number of blocks left
170 * unused. so, before taking some block from descriptor, one must
171 * make sure corresponded logical block isn't allocated yet. this
172 * also means that freeing any block within descriptor's range
173 * must discard all preallocated blocks.
175 * assigned to specific locality group which does not translate to
176 * permanent set of inodes: inode can join and leave group. space
177 * from this type of preallocation can be used for any inode. thus
178 * it's consumed from the beginning to the end.
180 * relation between them can be expressed as:
181 * in-core buddy = on-disk bitmap + preallocation descriptors
183 * this mean blocks mballoc considers used are:
184 * - allocated blocks (persistent)
185 * - preallocated blocks (non-persistent)
187 * consistency in mballoc world means that at any time a block is either
188 * free or used in ALL structures. notice: "any time" should not be read
189 * literally -- time is discrete and delimited by locks.
191 * to keep it simple, we don't use block numbers, instead we count number of
192 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
194 * all operations can be expressed as:
195 * - init buddy: buddy = on-disk + PAs
196 * - new PA: buddy += N; PA = N
197 * - use inode PA: on-disk += N; PA -= N
198 * - discard inode PA buddy -= on-disk - PA; PA = 0
199 * - use locality group PA on-disk += N; PA -= N
200 * - discard locality group PA buddy -= PA; PA = 0
201 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
202 * is used in real operation because we can't know actual used
203 * bits from PA, only from on-disk bitmap
205 * if we follow this strict logic, then all operations above should be atomic.
206 * given some of them can block, we'd have to use something like semaphores
207 * killing performance on high-end SMP hardware. let's try to relax it using
208 * the following knowledge:
209 * 1) if buddy is referenced, it's already initialized
210 * 2) while block is used in buddy and the buddy is referenced,
211 * nobody can re-allocate that block
212 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
213 * bit set and PA claims same block, it's OK. IOW, one can set bit in
214 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
217 * so, now we're building a concurrency table:
220 * blocks for PA are allocated in the buddy, buddy must be referenced
221 * until PA is linked to allocation group to avoid concurrent buddy init
223 * we need to make sure that either on-disk bitmap or PA has uptodate data
224 * given (3) we care that PA-=N operation doesn't interfere with init
226 * the simplest way would be to have buddy initialized by the discard
227 * - use locality group PA
228 * again PA-=N must be serialized with init
229 * - discard locality group PA
230 * the simplest way would be to have buddy initialized by the discard
233 * i_data_sem serializes them
235 * discard process must wait until PA isn't used by another process
236 * - use locality group PA
237 * some mutex should serialize them
238 * - discard locality group PA
239 * discard process must wait until PA isn't used by another process
242 * i_data_sem or another mutex should serializes them
244 * discard process must wait until PA isn't used by another process
245 * - use locality group PA
246 * nothing wrong here -- they're different PAs covering different blocks
247 * - discard locality group PA
248 * discard process must wait until PA isn't used by another process
250 * now we're ready to make few consequences:
251 * - PA is referenced and while it is no discard is possible
252 * - PA is referenced until block isn't marked in on-disk bitmap
253 * - PA changes only after on-disk bitmap
254 * - discard must not compete with init. either init is done before
255 * any discard or they're serialized somehow
256 * - buddy init as sum of on-disk bitmap and PAs is done atomically
258 * a special case when we've used PA to emptiness. no need to modify buddy
259 * in this case, but we should care about concurrent init
264 * Logic in few words:
269 * mark bits in on-disk bitmap
272 * - use preallocation:
273 * find proper PA (per-inode or group)
275 * mark bits in on-disk bitmap
281 * mark bits in on-disk bitmap
284 * - discard preallocations in group:
286 * move them onto local list
287 * load on-disk bitmap
289 * remove PA from object (inode or locality group)
290 * mark free blocks in-core
292 * - discard inode's preallocations:
299 * - bitlock on a group (group)
300 * - object (inode/locality) (object)
311 * - release consumed pa:
316 * - generate in-core bitmap:
320 * - discard all for given object (inode, locality group):
325 * - discard all for given group:
332 static struct kmem_cache
*ext4_pspace_cachep
;
333 static struct kmem_cache
*ext4_ac_cachep
;
334 static struct kmem_cache
*ext4_free_data_cachep
;
336 /* We create slab caches for groupinfo data structures based on the
337 * superblock block size. There will be one per mounted filesystem for
338 * each unique s_blocksize_bits */
339 #define NR_GRPINFO_CACHES 8
340 static struct kmem_cache
*ext4_groupinfo_caches
[NR_GRPINFO_CACHES
];
342 static const char * const ext4_groupinfo_slab_names
[NR_GRPINFO_CACHES
] = {
343 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
344 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
345 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
348 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
350 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
352 static void ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
);
355 * The algorithm using this percpu seq counter goes below:
356 * 1. We sample the percpu discard_pa_seq counter before trying for block
357 * allocation in ext4_mb_new_blocks().
358 * 2. We increment this percpu discard_pa_seq counter when we either allocate
359 * or free these blocks i.e. while marking those blocks as used/free in
360 * mb_mark_used()/mb_free_blocks().
361 * 3. We also increment this percpu seq counter when we successfully identify
362 * that the bb_prealloc_list is not empty and hence proceed for discarding
363 * of those PAs inside ext4_mb_discard_group_preallocations().
365 * Now to make sure that the regular fast path of block allocation is not
366 * affected, as a small optimization we only sample the percpu seq counter
367 * on that cpu. Only when the block allocation fails and when freed blocks
368 * found were 0, that is when we sample percpu seq counter for all cpus using
369 * below function ext4_get_discard_pa_seq_sum(). This happens after making
370 * sure that all the PAs on grp->bb_prealloc_list got freed or if it's empty.
372 static DEFINE_PER_CPU(u64
, discard_pa_seq
);
373 static inline u64
ext4_get_discard_pa_seq_sum(void)
378 for_each_possible_cpu(__cpu
)
379 __seq
+= per_cpu(discard_pa_seq
, __cpu
);
383 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
385 #if BITS_PER_LONG == 64
386 *bit
+= ((unsigned long) addr
& 7UL) << 3;
387 addr
= (void *) ((unsigned long) addr
& ~7UL);
388 #elif BITS_PER_LONG == 32
389 *bit
+= ((unsigned long) addr
& 3UL) << 3;
390 addr
= (void *) ((unsigned long) addr
& ~3UL);
392 #error "how many bits you are?!"
397 static inline int mb_test_bit(int bit
, void *addr
)
400 * ext4_test_bit on architecture like powerpc
401 * needs unsigned long aligned address
403 addr
= mb_correct_addr_and_bit(&bit
, addr
);
404 return ext4_test_bit(bit
, addr
);
407 static inline void mb_set_bit(int bit
, void *addr
)
409 addr
= mb_correct_addr_and_bit(&bit
, addr
);
410 ext4_set_bit(bit
, addr
);
413 static inline void mb_clear_bit(int bit
, void *addr
)
415 addr
= mb_correct_addr_and_bit(&bit
, addr
);
416 ext4_clear_bit(bit
, addr
);
419 static inline int mb_test_and_clear_bit(int bit
, void *addr
)
421 addr
= mb_correct_addr_and_bit(&bit
, addr
);
422 return ext4_test_and_clear_bit(bit
, addr
);
425 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
427 int fix
= 0, ret
, tmpmax
;
428 addr
= mb_correct_addr_and_bit(&fix
, addr
);
432 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
438 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
440 int fix
= 0, ret
, tmpmax
;
441 addr
= mb_correct_addr_and_bit(&fix
, addr
);
445 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
451 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
455 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
458 if (order
> e4b
->bd_blkbits
+ 1) {
463 /* at order 0 we see each particular block */
465 *max
= 1 << (e4b
->bd_blkbits
+ 3);
466 return e4b
->bd_bitmap
;
469 bb
= e4b
->bd_buddy
+ EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
470 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
476 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
477 int first
, int count
)
480 struct super_block
*sb
= e4b
->bd_sb
;
482 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
484 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
485 for (i
= 0; i
< count
; i
++) {
486 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
487 ext4_fsblk_t blocknr
;
489 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
490 blocknr
+= EXT4_C2B(EXT4_SB(sb
), first
+ i
);
491 ext4_grp_locked_error(sb
, e4b
->bd_group
,
492 inode
? inode
->i_ino
: 0,
494 "freeing block already freed "
497 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
498 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
500 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
504 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
508 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
510 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
511 for (i
= 0; i
< count
; i
++) {
512 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
513 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
517 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
519 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
521 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
522 unsigned char *b1
, *b2
;
524 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
525 b2
= (unsigned char *) bitmap
;
526 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
527 if (b1
[i
] != b2
[i
]) {
528 ext4_msg(e4b
->bd_sb
, KERN_ERR
,
529 "corruption in group %u "
530 "at byte %u(%u): %x in copy != %x "
532 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
539 static void mb_group_bb_bitmap_alloc(struct super_block
*sb
,
540 struct ext4_group_info
*grp
, ext4_group_t group
)
542 struct buffer_head
*bh
;
544 grp
->bb_bitmap
= kmalloc(sb
->s_blocksize
, GFP_NOFS
);
548 bh
= ext4_read_block_bitmap(sb
, group
);
549 if (IS_ERR_OR_NULL(bh
)) {
550 kfree(grp
->bb_bitmap
);
551 grp
->bb_bitmap
= NULL
;
555 memcpy(grp
->bb_bitmap
, bh
->b_data
, sb
->s_blocksize
);
559 static void mb_group_bb_bitmap_free(struct ext4_group_info
*grp
)
561 kfree(grp
->bb_bitmap
);
565 static inline void mb_free_blocks_double(struct inode
*inode
,
566 struct ext4_buddy
*e4b
, int first
, int count
)
570 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
571 int first
, int count
)
575 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
580 static inline void mb_group_bb_bitmap_alloc(struct super_block
*sb
,
581 struct ext4_group_info
*grp
, ext4_group_t group
)
586 static inline void mb_group_bb_bitmap_free(struct ext4_group_info
*grp
)
592 #ifdef AGGRESSIVE_CHECK
594 #define MB_CHECK_ASSERT(assert) \
598 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
599 function, file, line, # assert); \
604 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
605 const char *function
, int line
)
607 struct super_block
*sb
= e4b
->bd_sb
;
608 int order
= e4b
->bd_blkbits
+ 1;
615 struct ext4_group_info
*grp
;
618 struct list_head
*cur
;
623 static int mb_check_counter
;
624 if (mb_check_counter
++ % 100 != 0)
629 buddy
= mb_find_buddy(e4b
, order
, &max
);
630 MB_CHECK_ASSERT(buddy
);
631 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
632 MB_CHECK_ASSERT(buddy2
);
633 MB_CHECK_ASSERT(buddy
!= buddy2
);
634 MB_CHECK_ASSERT(max
* 2 == max2
);
637 for (i
= 0; i
< max
; i
++) {
639 if (mb_test_bit(i
, buddy
)) {
640 /* only single bit in buddy2 may be 1 */
641 if (!mb_test_bit(i
<< 1, buddy2
)) {
643 mb_test_bit((i
<<1)+1, buddy2
));
644 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
646 mb_test_bit(i
<< 1, buddy2
));
651 /* both bits in buddy2 must be 1 */
652 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
653 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
655 for (j
= 0; j
< (1 << order
); j
++) {
656 k
= (i
* (1 << order
)) + j
;
658 !mb_test_bit(k
, e4b
->bd_bitmap
));
662 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
667 buddy
= mb_find_buddy(e4b
, 0, &max
);
668 for (i
= 0; i
< max
; i
++) {
669 if (!mb_test_bit(i
, buddy
)) {
670 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
678 /* check used bits only */
679 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
680 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
682 MB_CHECK_ASSERT(k
< max2
);
683 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
686 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
687 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
689 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
690 list_for_each(cur
, &grp
->bb_prealloc_list
) {
691 ext4_group_t groupnr
;
692 struct ext4_prealloc_space
*pa
;
693 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
694 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
695 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
696 for (i
= 0; i
< pa
->pa_len
; i
++)
697 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
701 #undef MB_CHECK_ASSERT
702 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
703 __FILE__, __func__, __LINE__)
705 #define mb_check_buddy(e4b)
709 * Divide blocks started from @first with length @len into
710 * smaller chunks with power of 2 blocks.
711 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
712 * then increase bb_counters[] for corresponded chunk size.
714 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
715 void *buddy
, ext4_grpblk_t first
, ext4_grpblk_t len
,
716 struct ext4_group_info
*grp
)
718 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
724 BUG_ON(len
> EXT4_CLUSTERS_PER_GROUP(sb
));
726 border
= 2 << sb
->s_blocksize_bits
;
729 /* find how many blocks can be covered since this position */
730 max
= ffs(first
| border
) - 1;
732 /* find how many blocks of power 2 we need to mark */
739 /* mark multiblock chunks only */
740 grp
->bb_counters
[min
]++;
742 mb_clear_bit(first
>> min
,
743 buddy
+ sbi
->s_mb_offsets
[min
]);
751 * Cache the order of the largest free extent we have available in this block
755 mb_set_largest_free_order(struct super_block
*sb
, struct ext4_group_info
*grp
)
760 grp
->bb_largest_free_order
= -1; /* uninit */
762 bits
= sb
->s_blocksize_bits
+ 1;
763 for (i
= bits
; i
>= 0; i
--) {
764 if (grp
->bb_counters
[i
] > 0) {
765 grp
->bb_largest_free_order
= i
;
771 static noinline_for_stack
772 void ext4_mb_generate_buddy(struct super_block
*sb
,
773 void *buddy
, void *bitmap
, ext4_group_t group
)
775 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
776 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
777 ext4_grpblk_t max
= EXT4_CLUSTERS_PER_GROUP(sb
);
782 unsigned fragments
= 0;
783 unsigned long long period
= get_cycles();
785 /* initialize buddy from bitmap which is aggregation
786 * of on-disk bitmap and preallocations */
787 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
788 grp
->bb_first_free
= i
;
792 i
= mb_find_next_bit(bitmap
, max
, i
);
796 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
798 grp
->bb_counters
[0]++;
800 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
802 grp
->bb_fragments
= fragments
;
804 if (free
!= grp
->bb_free
) {
805 ext4_grp_locked_error(sb
, group
, 0, 0,
806 "block bitmap and bg descriptor "
807 "inconsistent: %u vs %u free clusters",
810 * If we intend to continue, we consider group descriptor
811 * corrupt and update bb_free using bitmap value
814 ext4_mark_group_bitmap_corrupted(sb
, group
,
815 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
817 mb_set_largest_free_order(sb
, grp
);
819 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
821 period
= get_cycles() - period
;
822 spin_lock(&sbi
->s_bal_lock
);
823 sbi
->s_mb_buddies_generated
++;
824 sbi
->s_mb_generation_time
+= period
;
825 spin_unlock(&sbi
->s_bal_lock
);
828 static void mb_regenerate_buddy(struct ext4_buddy
*e4b
)
834 while ((buddy
= mb_find_buddy(e4b
, order
++, &count
))) {
835 ext4_set_bits(buddy
, 0, count
);
837 e4b
->bd_info
->bb_fragments
= 0;
838 memset(e4b
->bd_info
->bb_counters
, 0,
839 sizeof(*e4b
->bd_info
->bb_counters
) *
840 (e4b
->bd_sb
->s_blocksize_bits
+ 2));
842 ext4_mb_generate_buddy(e4b
->bd_sb
, e4b
->bd_buddy
,
843 e4b
->bd_bitmap
, e4b
->bd_group
);
846 /* The buddy information is attached the buddy cache inode
847 * for convenience. The information regarding each group
848 * is loaded via ext4_mb_load_buddy. The information involve
849 * block bitmap and buddy information. The information are
850 * stored in the inode as
853 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
856 * one block each for bitmap and buddy information.
857 * So for each group we take up 2 blocks. A page can
858 * contain blocks_per_page (PAGE_SIZE / blocksize) blocks.
859 * So it can have information regarding groups_per_page which
860 * is blocks_per_page/2
862 * Locking note: This routine takes the block group lock of all groups
863 * for this page; do not hold this lock when calling this routine!
866 static int ext4_mb_init_cache(struct page
*page
, char *incore
, gfp_t gfp
)
868 ext4_group_t ngroups
;
874 ext4_group_t first_group
, group
;
876 struct super_block
*sb
;
877 struct buffer_head
*bhs
;
878 struct buffer_head
**bh
= NULL
;
882 struct ext4_group_info
*grinfo
;
884 inode
= page
->mapping
->host
;
886 ngroups
= ext4_get_groups_count(sb
);
887 blocksize
= i_blocksize(inode
);
888 blocks_per_page
= PAGE_SIZE
/ blocksize
;
890 mb_debug(sb
, "init page %lu\n", page
->index
);
892 groups_per_page
= blocks_per_page
>> 1;
893 if (groups_per_page
== 0)
896 /* allocate buffer_heads to read bitmaps */
897 if (groups_per_page
> 1) {
898 i
= sizeof(struct buffer_head
*) * groups_per_page
;
899 bh
= kzalloc(i
, gfp
);
907 first_group
= page
->index
* blocks_per_page
/ 2;
909 /* read all groups the page covers into the cache */
910 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
911 if (group
>= ngroups
)
914 grinfo
= ext4_get_group_info(sb
, group
);
916 * If page is uptodate then we came here after online resize
917 * which added some new uninitialized group info structs, so
918 * we must skip all initialized uptodate buddies on the page,
919 * which may be currently in use by an allocating task.
921 if (PageUptodate(page
) && !EXT4_MB_GRP_NEED_INIT(grinfo
)) {
925 bh
[i
] = ext4_read_block_bitmap_nowait(sb
, group
);
927 err
= PTR_ERR(bh
[i
]);
931 mb_debug(sb
, "read bitmap for group %u\n", group
);
934 /* wait for I/O completion */
935 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
940 err2
= ext4_wait_block_bitmap(sb
, group
, bh
[i
]);
945 first_block
= page
->index
* blocks_per_page
;
946 for (i
= 0; i
< blocks_per_page
; i
++) {
947 group
= (first_block
+ i
) >> 1;
948 if (group
>= ngroups
)
951 if (!bh
[group
- first_group
])
952 /* skip initialized uptodate buddy */
955 if (!buffer_verified(bh
[group
- first_group
]))
956 /* Skip faulty bitmaps */
961 * data carry information regarding this
962 * particular group in the format specified
966 data
= page_address(page
) + (i
* blocksize
);
967 bitmap
= bh
[group
- first_group
]->b_data
;
970 * We place the buddy block and bitmap block
973 if ((first_block
+ i
) & 1) {
974 /* this is block of buddy */
975 BUG_ON(incore
== NULL
);
976 mb_debug(sb
, "put buddy for group %u in page %lu/%x\n",
977 group
, page
->index
, i
* blocksize
);
978 trace_ext4_mb_buddy_bitmap_load(sb
, group
);
979 grinfo
= ext4_get_group_info(sb
, group
);
980 grinfo
->bb_fragments
= 0;
981 memset(grinfo
->bb_counters
, 0,
982 sizeof(*grinfo
->bb_counters
) *
983 (sb
->s_blocksize_bits
+2));
985 * incore got set to the group block bitmap below
987 ext4_lock_group(sb
, group
);
989 memset(data
, 0xff, blocksize
);
990 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
991 ext4_unlock_group(sb
, group
);
994 /* this is block of bitmap */
995 BUG_ON(incore
!= NULL
);
996 mb_debug(sb
, "put bitmap for group %u in page %lu/%x\n",
997 group
, page
->index
, i
* blocksize
);
998 trace_ext4_mb_bitmap_load(sb
, group
);
1000 /* see comments in ext4_mb_put_pa() */
1001 ext4_lock_group(sb
, group
);
1002 memcpy(data
, bitmap
, blocksize
);
1004 /* mark all preallocated blks used in in-core bitmap */
1005 ext4_mb_generate_from_pa(sb
, data
, group
);
1006 ext4_mb_generate_from_freelist(sb
, data
, group
);
1007 ext4_unlock_group(sb
, group
);
1009 /* set incore so that the buddy information can be
1010 * generated using this
1015 SetPageUptodate(page
);
1019 for (i
= 0; i
< groups_per_page
; i
++)
1028 * Lock the buddy and bitmap pages. This make sure other parallel init_group
1029 * on the same buddy page doesn't happen whild holding the buddy page lock.
1030 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
1031 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
1033 static int ext4_mb_get_buddy_page_lock(struct super_block
*sb
,
1034 ext4_group_t group
, struct ext4_buddy
*e4b
, gfp_t gfp
)
1036 struct inode
*inode
= EXT4_SB(sb
)->s_buddy_cache
;
1037 int block
, pnum
, poff
;
1038 int blocks_per_page
;
1041 e4b
->bd_buddy_page
= NULL
;
1042 e4b
->bd_bitmap_page
= NULL
;
1044 blocks_per_page
= PAGE_SIZE
/ sb
->s_blocksize
;
1046 * the buddy cache inode stores the block bitmap
1047 * and buddy information in consecutive blocks.
1048 * So for each group we need two blocks.
1051 pnum
= block
/ blocks_per_page
;
1052 poff
= block
% blocks_per_page
;
1053 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1056 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1057 e4b
->bd_bitmap_page
= page
;
1058 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1060 if (blocks_per_page
>= 2) {
1061 /* buddy and bitmap are on the same page */
1066 pnum
= block
/ blocks_per_page
;
1067 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1070 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1071 e4b
->bd_buddy_page
= page
;
1075 static void ext4_mb_put_buddy_page_lock(struct ext4_buddy
*e4b
)
1077 if (e4b
->bd_bitmap_page
) {
1078 unlock_page(e4b
->bd_bitmap_page
);
1079 put_page(e4b
->bd_bitmap_page
);
1081 if (e4b
->bd_buddy_page
) {
1082 unlock_page(e4b
->bd_buddy_page
);
1083 put_page(e4b
->bd_buddy_page
);
1088 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1089 * block group lock of all groups for this page; do not hold the BG lock when
1090 * calling this routine!
1092 static noinline_for_stack
1093 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
, gfp_t gfp
)
1096 struct ext4_group_info
*this_grp
;
1097 struct ext4_buddy e4b
;
1102 mb_debug(sb
, "init group %u\n", group
);
1103 this_grp
= ext4_get_group_info(sb
, group
);
1105 * This ensures that we don't reinit the buddy cache
1106 * page which map to the group from which we are already
1107 * allocating. If we are looking at the buddy cache we would
1108 * have taken a reference using ext4_mb_load_buddy and that
1109 * would have pinned buddy page to page cache.
1110 * The call to ext4_mb_get_buddy_page_lock will mark the
1113 ret
= ext4_mb_get_buddy_page_lock(sb
, group
, &e4b
, gfp
);
1114 if (ret
|| !EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1116 * somebody initialized the group
1117 * return without doing anything
1122 page
= e4b
.bd_bitmap_page
;
1123 ret
= ext4_mb_init_cache(page
, NULL
, gfp
);
1126 if (!PageUptodate(page
)) {
1131 if (e4b
.bd_buddy_page
== NULL
) {
1133 * If both the bitmap and buddy are in
1134 * the same page we don't need to force
1140 /* init buddy cache */
1141 page
= e4b
.bd_buddy_page
;
1142 ret
= ext4_mb_init_cache(page
, e4b
.bd_bitmap
, gfp
);
1145 if (!PageUptodate(page
)) {
1150 ext4_mb_put_buddy_page_lock(&e4b
);
1155 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1156 * block group lock of all groups for this page; do not hold the BG lock when
1157 * calling this routine!
1159 static noinline_for_stack
int
1160 ext4_mb_load_buddy_gfp(struct super_block
*sb
, ext4_group_t group
,
1161 struct ext4_buddy
*e4b
, gfp_t gfp
)
1163 int blocks_per_page
;
1169 struct ext4_group_info
*grp
;
1170 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1171 struct inode
*inode
= sbi
->s_buddy_cache
;
1174 mb_debug(sb
, "load group %u\n", group
);
1176 blocks_per_page
= PAGE_SIZE
/ sb
->s_blocksize
;
1177 grp
= ext4_get_group_info(sb
, group
);
1179 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1182 e4b
->bd_group
= group
;
1183 e4b
->bd_buddy_page
= NULL
;
1184 e4b
->bd_bitmap_page
= NULL
;
1186 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1188 * we need full data about the group
1189 * to make a good selection
1191 ret
= ext4_mb_init_group(sb
, group
, gfp
);
1197 * the buddy cache inode stores the block bitmap
1198 * and buddy information in consecutive blocks.
1199 * So for each group we need two blocks.
1202 pnum
= block
/ blocks_per_page
;
1203 poff
= block
% blocks_per_page
;
1205 /* we could use find_or_create_page(), but it locks page
1206 * what we'd like to avoid in fast path ... */
1207 page
= find_get_page_flags(inode
->i_mapping
, pnum
, FGP_ACCESSED
);
1208 if (page
== NULL
|| !PageUptodate(page
)) {
1211 * drop the page reference and try
1212 * to get the page with lock. If we
1213 * are not uptodate that implies
1214 * somebody just created the page but
1215 * is yet to initialize the same. So
1216 * wait for it to initialize.
1219 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1221 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1222 if (!PageUptodate(page
)) {
1223 ret
= ext4_mb_init_cache(page
, NULL
, gfp
);
1228 mb_cmp_bitmaps(e4b
, page_address(page
) +
1229 (poff
* sb
->s_blocksize
));
1238 if (!PageUptodate(page
)) {
1243 /* Pages marked accessed already */
1244 e4b
->bd_bitmap_page
= page
;
1245 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1248 pnum
= block
/ blocks_per_page
;
1249 poff
= block
% blocks_per_page
;
1251 page
= find_get_page_flags(inode
->i_mapping
, pnum
, FGP_ACCESSED
);
1252 if (page
== NULL
|| !PageUptodate(page
)) {
1255 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1257 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1258 if (!PageUptodate(page
)) {
1259 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
,
1273 if (!PageUptodate(page
)) {
1278 /* Pages marked accessed already */
1279 e4b
->bd_buddy_page
= page
;
1280 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1282 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1283 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1290 if (e4b
->bd_bitmap_page
)
1291 put_page(e4b
->bd_bitmap_page
);
1292 if (e4b
->bd_buddy_page
)
1293 put_page(e4b
->bd_buddy_page
);
1294 e4b
->bd_buddy
= NULL
;
1295 e4b
->bd_bitmap
= NULL
;
1299 static int ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1300 struct ext4_buddy
*e4b
)
1302 return ext4_mb_load_buddy_gfp(sb
, group
, e4b
, GFP_NOFS
);
1305 static void ext4_mb_unload_buddy(struct ext4_buddy
*e4b
)
1307 if (e4b
->bd_bitmap_page
)
1308 put_page(e4b
->bd_bitmap_page
);
1309 if (e4b
->bd_buddy_page
)
1310 put_page(e4b
->bd_buddy_page
);
1314 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1317 int bb_incr
= 1 << (e4b
->bd_blkbits
- 1);
1320 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
1321 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1324 while (order
<= e4b
->bd_blkbits
+ 1) {
1326 if (!mb_test_bit(block
, bb
)) {
1327 /* this block is part of buddy of order 'order' */
1337 static void mb_clear_bits(void *bm
, int cur
, int len
)
1343 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1344 /* fast path: clear whole word at once */
1345 addr
= bm
+ (cur
>> 3);
1350 mb_clear_bit(cur
, bm
);
1355 /* clear bits in given range
1356 * will return first found zero bit if any, -1 otherwise
1358 static int mb_test_and_clear_bits(void *bm
, int cur
, int len
)
1365 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1366 /* fast path: clear whole word at once */
1367 addr
= bm
+ (cur
>> 3);
1368 if (*addr
!= (__u32
)(-1) && zero_bit
== -1)
1369 zero_bit
= cur
+ mb_find_next_zero_bit(addr
, 32, 0);
1374 if (!mb_test_and_clear_bit(cur
, bm
) && zero_bit
== -1)
1382 void ext4_set_bits(void *bm
, int cur
, int len
)
1388 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1389 /* fast path: set whole word at once */
1390 addr
= bm
+ (cur
>> 3);
1395 mb_set_bit(cur
, bm
);
1401 * _________________________________________________________________ */
1403 static inline int mb_buddy_adjust_border(int* bit
, void* bitmap
, int side
)
1405 if (mb_test_bit(*bit
+ side
, bitmap
)) {
1406 mb_clear_bit(*bit
, bitmap
);
1412 mb_set_bit(*bit
, bitmap
);
1417 static void mb_buddy_mark_free(struct ext4_buddy
*e4b
, int first
, int last
)
1421 void *buddy
= mb_find_buddy(e4b
, order
, &max
);
1426 /* Bits in range [first; last] are known to be set since
1427 * corresponding blocks were allocated. Bits in range
1428 * (first; last) will stay set because they form buddies on
1429 * upper layer. We just deal with borders if they don't
1430 * align with upper layer and then go up.
1431 * Releasing entire group is all about clearing
1432 * single bit of highest order buddy.
1436 * ---------------------------------
1438 * ---------------------------------
1439 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1440 * ---------------------------------
1442 * \_____________________/
1444 * Neither [1] nor [6] is aligned to above layer.
1445 * Left neighbour [0] is free, so mark it busy,
1446 * decrease bb_counters and extend range to
1448 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1449 * mark [6] free, increase bb_counters and shrink range to
1451 * Then shift range to [0; 2], go up and do the same.
1456 e4b
->bd_info
->bb_counters
[order
] += mb_buddy_adjust_border(&first
, buddy
, -1);
1458 e4b
->bd_info
->bb_counters
[order
] += mb_buddy_adjust_border(&last
, buddy
, 1);
1463 if (first
== last
|| !(buddy2
= mb_find_buddy(e4b
, order
, &max
))) {
1464 mb_clear_bits(buddy
, first
, last
- first
+ 1);
1465 e4b
->bd_info
->bb_counters
[order
- 1] += last
- first
+ 1;
1474 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1475 int first
, int count
)
1477 int left_is_free
= 0;
1478 int right_is_free
= 0;
1480 int last
= first
+ count
- 1;
1481 struct super_block
*sb
= e4b
->bd_sb
;
1483 if (WARN_ON(count
== 0))
1485 BUG_ON(last
>= (sb
->s_blocksize
<< 3));
1486 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1487 /* Don't bother if the block group is corrupt. */
1488 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b
->bd_info
)))
1491 mb_check_buddy(e4b
);
1492 mb_free_blocks_double(inode
, e4b
, first
, count
);
1494 this_cpu_inc(discard_pa_seq
);
1495 e4b
->bd_info
->bb_free
+= count
;
1496 if (first
< e4b
->bd_info
->bb_first_free
)
1497 e4b
->bd_info
->bb_first_free
= first
;
1499 /* access memory sequentially: check left neighbour,
1500 * clear range and then check right neighbour
1503 left_is_free
= !mb_test_bit(first
- 1, e4b
->bd_bitmap
);
1504 block
= mb_test_and_clear_bits(e4b
->bd_bitmap
, first
, count
);
1505 if (last
+ 1 < EXT4_SB(sb
)->s_mb_maxs
[0])
1506 right_is_free
= !mb_test_bit(last
+ 1, e4b
->bd_bitmap
);
1508 if (unlikely(block
!= -1)) {
1509 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1510 ext4_fsblk_t blocknr
;
1512 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1513 blocknr
+= EXT4_C2B(sbi
, block
);
1514 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1515 inode
? inode
->i_ino
: 0,
1517 "freeing already freed block "
1518 "(bit %u); block bitmap corrupt.",
1520 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
1521 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
1522 mb_regenerate_buddy(e4b
);
1526 /* let's maintain fragments counter */
1527 if (left_is_free
&& right_is_free
)
1528 e4b
->bd_info
->bb_fragments
--;
1529 else if (!left_is_free
&& !right_is_free
)
1530 e4b
->bd_info
->bb_fragments
++;
1532 /* buddy[0] == bd_bitmap is a special case, so handle
1533 * it right away and let mb_buddy_mark_free stay free of
1534 * zero order checks.
1535 * Check if neighbours are to be coaleasced,
1536 * adjust bitmap bb_counters and borders appropriately.
1539 first
+= !left_is_free
;
1540 e4b
->bd_info
->bb_counters
[0] += left_is_free
? -1 : 1;
1543 last
-= !right_is_free
;
1544 e4b
->bd_info
->bb_counters
[0] += right_is_free
? -1 : 1;
1548 mb_buddy_mark_free(e4b
, first
>> 1, last
>> 1);
1551 mb_set_largest_free_order(sb
, e4b
->bd_info
);
1552 mb_check_buddy(e4b
);
1555 static int mb_find_extent(struct ext4_buddy
*e4b
, int block
,
1556 int needed
, struct ext4_free_extent
*ex
)
1562 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1565 buddy
= mb_find_buddy(e4b
, 0, &max
);
1566 BUG_ON(buddy
== NULL
);
1567 BUG_ON(block
>= max
);
1568 if (mb_test_bit(block
, buddy
)) {
1575 /* find actual order */
1576 order
= mb_find_order_for_block(e4b
, block
);
1577 block
= block
>> order
;
1579 ex
->fe_len
= 1 << order
;
1580 ex
->fe_start
= block
<< order
;
1581 ex
->fe_group
= e4b
->bd_group
;
1583 /* calc difference from given start */
1584 next
= next
- ex
->fe_start
;
1586 ex
->fe_start
+= next
;
1588 while (needed
> ex
->fe_len
&&
1589 mb_find_buddy(e4b
, order
, &max
)) {
1591 if (block
+ 1 >= max
)
1594 next
= (block
+ 1) * (1 << order
);
1595 if (mb_test_bit(next
, e4b
->bd_bitmap
))
1598 order
= mb_find_order_for_block(e4b
, next
);
1600 block
= next
>> order
;
1601 ex
->fe_len
+= 1 << order
;
1604 if (ex
->fe_start
+ ex
->fe_len
> EXT4_CLUSTERS_PER_GROUP(e4b
->bd_sb
)) {
1605 /* Should never happen! (but apparently sometimes does?!?) */
1607 ext4_error(e4b
->bd_sb
, "corruption or bug in mb_find_extent "
1608 "block=%d, order=%d needed=%d ex=%u/%d/%d@%u",
1609 block
, order
, needed
, ex
->fe_group
, ex
->fe_start
,
1610 ex
->fe_len
, ex
->fe_logical
);
1618 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1624 int start
= ex
->fe_start
;
1625 int len
= ex
->fe_len
;
1630 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1631 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1632 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1633 mb_check_buddy(e4b
);
1634 mb_mark_used_double(e4b
, start
, len
);
1636 this_cpu_inc(discard_pa_seq
);
1637 e4b
->bd_info
->bb_free
-= len
;
1638 if (e4b
->bd_info
->bb_first_free
== start
)
1639 e4b
->bd_info
->bb_first_free
+= len
;
1641 /* let's maintain fragments counter */
1643 mlen
= !mb_test_bit(start
- 1, e4b
->bd_bitmap
);
1644 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1645 max
= !mb_test_bit(start
+ len
, e4b
->bd_bitmap
);
1647 e4b
->bd_info
->bb_fragments
++;
1648 else if (!mlen
&& !max
)
1649 e4b
->bd_info
->bb_fragments
--;
1651 /* let's maintain buddy itself */
1653 ord
= mb_find_order_for_block(e4b
, start
);
1655 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1656 /* the whole chunk may be allocated at once! */
1658 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1659 BUG_ON((start
>> ord
) >= max
);
1660 mb_set_bit(start
>> ord
, buddy
);
1661 e4b
->bd_info
->bb_counters
[ord
]--;
1668 /* store for history */
1670 ret
= len
| (ord
<< 16);
1672 /* we have to split large buddy */
1674 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1675 mb_set_bit(start
>> ord
, buddy
);
1676 e4b
->bd_info
->bb_counters
[ord
]--;
1679 cur
= (start
>> ord
) & ~1U;
1680 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1681 mb_clear_bit(cur
, buddy
);
1682 mb_clear_bit(cur
+ 1, buddy
);
1683 e4b
->bd_info
->bb_counters
[ord
]++;
1684 e4b
->bd_info
->bb_counters
[ord
]++;
1686 mb_set_largest_free_order(e4b
->bd_sb
, e4b
->bd_info
);
1688 ext4_set_bits(e4b
->bd_bitmap
, ex
->fe_start
, len0
);
1689 mb_check_buddy(e4b
);
1695 * Must be called under group lock!
1697 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1698 struct ext4_buddy
*e4b
)
1700 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1703 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1704 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1706 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1707 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1708 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1710 /* preallocation can change ac_b_ex, thus we store actually
1711 * allocated blocks for history */
1712 ac
->ac_f_ex
= ac
->ac_b_ex
;
1714 ac
->ac_status
= AC_STATUS_FOUND
;
1715 ac
->ac_tail
= ret
& 0xffff;
1716 ac
->ac_buddy
= ret
>> 16;
1719 * take the page reference. We want the page to be pinned
1720 * so that we don't get a ext4_mb_init_cache_call for this
1721 * group until we update the bitmap. That would mean we
1722 * double allocate blocks. The reference is dropped
1723 * in ext4_mb_release_context
1725 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1726 get_page(ac
->ac_bitmap_page
);
1727 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1728 get_page(ac
->ac_buddy_page
);
1729 /* store last allocated for subsequent stream allocation */
1730 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1731 spin_lock(&sbi
->s_md_lock
);
1732 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1733 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1734 spin_unlock(&sbi
->s_md_lock
);
1737 * As we've just preallocated more space than
1738 * user requested originally, we store allocated
1739 * space in a special descriptor.
1741 if (ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
1742 ext4_mb_new_preallocation(ac
);
1747 * regular allocator, for general purposes allocation
1750 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1751 struct ext4_buddy
*e4b
,
1754 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1755 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1756 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1757 struct ext4_free_extent ex
;
1760 if (ac
->ac_status
== AC_STATUS_FOUND
)
1763 * We don't want to scan for a whole year
1765 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1766 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1767 ac
->ac_status
= AC_STATUS_BREAK
;
1772 * Haven't found good chunk so far, let's continue
1774 if (bex
->fe_len
< gex
->fe_len
)
1777 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1778 && bex
->fe_group
== e4b
->bd_group
) {
1779 /* recheck chunk's availability - we don't know
1780 * when it was found (within this lock-unlock
1782 max
= mb_find_extent(e4b
, bex
->fe_start
, gex
->fe_len
, &ex
);
1783 if (max
>= gex
->fe_len
) {
1784 ext4_mb_use_best_found(ac
, e4b
);
1791 * The routine checks whether found extent is good enough. If it is,
1792 * then the extent gets marked used and flag is set to the context
1793 * to stop scanning. Otherwise, the extent is compared with the
1794 * previous found extent and if new one is better, then it's stored
1795 * in the context. Later, the best found extent will be used, if
1796 * mballoc can't find good enough extent.
1798 * FIXME: real allocation policy is to be designed yet!
1800 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1801 struct ext4_free_extent
*ex
,
1802 struct ext4_buddy
*e4b
)
1804 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1805 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1807 BUG_ON(ex
->fe_len
<= 0);
1808 BUG_ON(ex
->fe_len
> EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1809 BUG_ON(ex
->fe_start
>= EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1810 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1815 * The special case - take what you catch first
1817 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1819 ext4_mb_use_best_found(ac
, e4b
);
1824 * Let's check whether the chuck is good enough
1826 if (ex
->fe_len
== gex
->fe_len
) {
1828 ext4_mb_use_best_found(ac
, e4b
);
1833 * If this is first found extent, just store it in the context
1835 if (bex
->fe_len
== 0) {
1841 * If new found extent is better, store it in the context
1843 if (bex
->fe_len
< gex
->fe_len
) {
1844 /* if the request isn't satisfied, any found extent
1845 * larger than previous best one is better */
1846 if (ex
->fe_len
> bex
->fe_len
)
1848 } else if (ex
->fe_len
> gex
->fe_len
) {
1849 /* if the request is satisfied, then we try to find
1850 * an extent that still satisfy the request, but is
1851 * smaller than previous one */
1852 if (ex
->fe_len
< bex
->fe_len
)
1856 ext4_mb_check_limits(ac
, e4b
, 0);
1859 static noinline_for_stack
1860 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1861 struct ext4_buddy
*e4b
)
1863 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1864 ext4_group_t group
= ex
.fe_group
;
1868 BUG_ON(ex
.fe_len
<= 0);
1869 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1873 ext4_lock_group(ac
->ac_sb
, group
);
1874 max
= mb_find_extent(e4b
, ex
.fe_start
, ex
.fe_len
, &ex
);
1878 ext4_mb_use_best_found(ac
, e4b
);
1881 ext4_unlock_group(ac
->ac_sb
, group
);
1882 ext4_mb_unload_buddy(e4b
);
1887 static noinline_for_stack
1888 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1889 struct ext4_buddy
*e4b
)
1891 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1894 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1895 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1896 struct ext4_free_extent ex
;
1898 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1900 if (grp
->bb_free
== 0)
1903 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1907 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b
->bd_info
))) {
1908 ext4_mb_unload_buddy(e4b
);
1912 ext4_lock_group(ac
->ac_sb
, group
);
1913 max
= mb_find_extent(e4b
, ac
->ac_g_ex
.fe_start
,
1914 ac
->ac_g_ex
.fe_len
, &ex
);
1915 ex
.fe_logical
= 0xDEADFA11; /* debug value */
1917 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1920 start
= ext4_group_first_block_no(ac
->ac_sb
, e4b
->bd_group
) +
1922 /* use do_div to get remainder (would be 64-bit modulo) */
1923 if (do_div(start
, sbi
->s_stripe
) == 0) {
1926 ext4_mb_use_best_found(ac
, e4b
);
1928 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1929 BUG_ON(ex
.fe_len
<= 0);
1930 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1931 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1934 ext4_mb_use_best_found(ac
, e4b
);
1935 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1936 /* Sometimes, caller may want to merge even small
1937 * number of blocks to an existing extent */
1938 BUG_ON(ex
.fe_len
<= 0);
1939 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1940 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1943 ext4_mb_use_best_found(ac
, e4b
);
1945 ext4_unlock_group(ac
->ac_sb
, group
);
1946 ext4_mb_unload_buddy(e4b
);
1952 * The routine scans buddy structures (not bitmap!) from given order
1953 * to max order and tries to find big enough chunk to satisfy the req
1955 static noinline_for_stack
1956 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1957 struct ext4_buddy
*e4b
)
1959 struct super_block
*sb
= ac
->ac_sb
;
1960 struct ext4_group_info
*grp
= e4b
->bd_info
;
1966 BUG_ON(ac
->ac_2order
<= 0);
1967 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1968 if (grp
->bb_counters
[i
] == 0)
1971 buddy
= mb_find_buddy(e4b
, i
, &max
);
1972 BUG_ON(buddy
== NULL
);
1974 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1976 ext4_grp_locked_error(ac
->ac_sb
, e4b
->bd_group
, 0, 0,
1977 "%d free clusters of order %d. But found 0",
1978 grp
->bb_counters
[i
], i
);
1979 ext4_mark_group_bitmap_corrupted(ac
->ac_sb
,
1981 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
1986 ac
->ac_b_ex
.fe_len
= 1 << i
;
1987 ac
->ac_b_ex
.fe_start
= k
<< i
;
1988 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1990 ext4_mb_use_best_found(ac
, e4b
);
1992 BUG_ON(ac
->ac_f_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1994 if (EXT4_SB(sb
)->s_mb_stats
)
1995 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
2002 * The routine scans the group and measures all found extents.
2003 * In order to optimize scanning, caller must pass number of
2004 * free blocks in the group, so the routine can know upper limit.
2006 static noinline_for_stack
2007 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
2008 struct ext4_buddy
*e4b
)
2010 struct super_block
*sb
= ac
->ac_sb
;
2011 void *bitmap
= e4b
->bd_bitmap
;
2012 struct ext4_free_extent ex
;
2016 free
= e4b
->bd_info
->bb_free
;
2017 if (WARN_ON(free
<= 0))
2020 i
= e4b
->bd_info
->bb_first_free
;
2022 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
2023 i
= mb_find_next_zero_bit(bitmap
,
2024 EXT4_CLUSTERS_PER_GROUP(sb
), i
);
2025 if (i
>= EXT4_CLUSTERS_PER_GROUP(sb
)) {
2027 * IF we have corrupt bitmap, we won't find any
2028 * free blocks even though group info says we
2029 * we have free blocks
2031 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
2032 "%d free clusters as per "
2033 "group info. But bitmap says 0",
2035 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
2036 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
2040 mb_find_extent(e4b
, i
, ac
->ac_g_ex
.fe_len
, &ex
);
2041 if (WARN_ON(ex
.fe_len
<= 0))
2043 if (free
< ex
.fe_len
) {
2044 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
2045 "%d free clusters as per "
2046 "group info. But got %d blocks",
2048 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
2049 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
2051 * The number of free blocks differs. This mostly
2052 * indicate that the bitmap is corrupt. So exit
2053 * without claiming the space.
2057 ex
.fe_logical
= 0xDEADC0DE; /* debug value */
2058 ext4_mb_measure_extent(ac
, &ex
, e4b
);
2064 ext4_mb_check_limits(ac
, e4b
, 1);
2068 * This is a special case for storages like raid5
2069 * we try to find stripe-aligned chunks for stripe-size-multiple requests
2071 static noinline_for_stack
2072 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
2073 struct ext4_buddy
*e4b
)
2075 struct super_block
*sb
= ac
->ac_sb
;
2076 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2077 void *bitmap
= e4b
->bd_bitmap
;
2078 struct ext4_free_extent ex
;
2079 ext4_fsblk_t first_group_block
;
2084 BUG_ON(sbi
->s_stripe
== 0);
2086 /* find first stripe-aligned block in group */
2087 first_group_block
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
2089 a
= first_group_block
+ sbi
->s_stripe
- 1;
2090 do_div(a
, sbi
->s_stripe
);
2091 i
= (a
* sbi
->s_stripe
) - first_group_block
;
2093 while (i
< EXT4_CLUSTERS_PER_GROUP(sb
)) {
2094 if (!mb_test_bit(i
, bitmap
)) {
2095 max
= mb_find_extent(e4b
, i
, sbi
->s_stripe
, &ex
);
2096 if (max
>= sbi
->s_stripe
) {
2098 ex
.fe_logical
= 0xDEADF00D; /* debug value */
2100 ext4_mb_use_best_found(ac
, e4b
);
2109 * This is also called BEFORE we load the buddy bitmap.
2110 * Returns either 1 or 0 indicating that the group is either suitable
2111 * for the allocation or not.
2113 static bool ext4_mb_good_group(struct ext4_allocation_context
*ac
,
2114 ext4_group_t group
, int cr
)
2116 ext4_grpblk_t free
, fragments
;
2117 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
2118 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
2120 BUG_ON(cr
< 0 || cr
>= 4);
2122 free
= grp
->bb_free
;
2125 if (cr
<= 2 && free
< ac
->ac_g_ex
.fe_len
)
2128 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp
)))
2131 fragments
= grp
->bb_fragments
;
2137 BUG_ON(ac
->ac_2order
== 0);
2139 /* Avoid using the first bg of a flexgroup for data files */
2140 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
2141 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
2142 ((group
% flex_size
) == 0))
2145 if ((ac
->ac_2order
> ac
->ac_sb
->s_blocksize_bits
+1) ||
2146 (free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
2149 if (grp
->bb_largest_free_order
< ac
->ac_2order
)
2154 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
2158 if (free
>= ac
->ac_g_ex
.fe_len
)
2171 * This could return negative error code if something goes wrong
2172 * during ext4_mb_init_group(). This should not be called with
2173 * ext4_lock_group() held.
2175 static int ext4_mb_good_group_nolock(struct ext4_allocation_context
*ac
,
2176 ext4_group_t group
, int cr
)
2178 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
2179 struct super_block
*sb
= ac
->ac_sb
;
2180 bool should_lock
= ac
->ac_flags
& EXT4_MB_STRICT_CHECK
;
2185 ext4_lock_group(sb
, group
);
2186 free
= grp
->bb_free
;
2189 if (cr
<= 2 && free
< ac
->ac_g_ex
.fe_len
)
2191 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp
)))
2194 ext4_unlock_group(sb
, group
);
2196 /* We only do this if the grp has never been initialized */
2197 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
2198 ret
= ext4_mb_init_group(ac
->ac_sb
, group
, GFP_NOFS
);
2204 ext4_lock_group(sb
, group
);
2205 ret
= ext4_mb_good_group(ac
, group
, cr
);
2208 ext4_unlock_group(sb
, group
);
2212 static noinline_for_stack
int
2213 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
2215 ext4_group_t ngroups
, group
, i
;
2217 int err
= 0, first_err
= 0;
2218 struct ext4_sb_info
*sbi
;
2219 struct super_block
*sb
;
2220 struct ext4_buddy e4b
;
2224 ngroups
= ext4_get_groups_count(sb
);
2225 /* non-extent files are limited to low blocks/groups */
2226 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)))
2227 ngroups
= sbi
->s_blockfile_groups
;
2229 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
2231 /* first, try the goal */
2232 err
= ext4_mb_find_by_goal(ac
, &e4b
);
2233 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
2236 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2240 * ac->ac2_order is set only if the fe_len is a power of 2
2241 * if ac2_order is set we also set criteria to 0 so that we
2242 * try exact allocation using buddy.
2244 i
= fls(ac
->ac_g_ex
.fe_len
);
2247 * We search using buddy data only if the order of the request
2248 * is greater than equal to the sbi_s_mb_order2_reqs
2249 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
2250 * We also support searching for power-of-two requests only for
2251 * requests upto maximum buddy size we have constructed.
2253 if (i
>= sbi
->s_mb_order2_reqs
&& i
<= sb
->s_blocksize_bits
+ 2) {
2255 * This should tell if fe_len is exactly power of 2
2257 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
2258 ac
->ac_2order
= array_index_nospec(i
- 1,
2259 sb
->s_blocksize_bits
+ 2);
2262 /* if stream allocation is enabled, use global goal */
2263 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
2264 /* TBD: may be hot point */
2265 spin_lock(&sbi
->s_md_lock
);
2266 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
2267 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
2268 spin_unlock(&sbi
->s_md_lock
);
2271 /* Let's just scan groups to find more-less suitable blocks */
2272 cr
= ac
->ac_2order
? 0 : 1;
2274 * cr == 0 try to get exact allocation,
2275 * cr == 3 try to get anything
2278 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2279 ac
->ac_criteria
= cr
;
2281 * searching for the right group start
2282 * from the goal value specified
2284 group
= ac
->ac_g_ex
.fe_group
;
2286 for (i
= 0; i
< ngroups
; group
++, i
++) {
2290 * Artificially restricted ngroups for non-extent
2291 * files makes group > ngroups possible on first loop.
2293 if (group
>= ngroups
)
2296 /* This now checks without needing the buddy page */
2297 ret
= ext4_mb_good_group_nolock(ac
, group
, cr
);
2304 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2308 ext4_lock_group(sb
, group
);
2311 * We need to check again after locking the
2314 ret
= ext4_mb_good_group(ac
, group
, cr
);
2316 ext4_unlock_group(sb
, group
);
2317 ext4_mb_unload_buddy(&e4b
);
2321 ac
->ac_groups_scanned
++;
2323 ext4_mb_simple_scan_group(ac
, &e4b
);
2324 else if (cr
== 1 && sbi
->s_stripe
&&
2325 !(ac
->ac_g_ex
.fe_len
% sbi
->s_stripe
))
2326 ext4_mb_scan_aligned(ac
, &e4b
);
2328 ext4_mb_complex_scan_group(ac
, &e4b
);
2330 ext4_unlock_group(sb
, group
);
2331 ext4_mb_unload_buddy(&e4b
);
2333 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2338 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2339 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2341 * We've been searching too long. Let's try to allocate
2342 * the best chunk we've found so far
2345 ext4_mb_try_best_found(ac
, &e4b
);
2346 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2348 * Someone more lucky has already allocated it.
2349 * The only thing we can do is just take first
2351 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2353 ac
->ac_b_ex
.fe_group
= 0;
2354 ac
->ac_b_ex
.fe_start
= 0;
2355 ac
->ac_b_ex
.fe_len
= 0;
2356 ac
->ac_status
= AC_STATUS_CONTINUE
;
2357 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2359 atomic_inc(&sbi
->s_mb_lost_chunks
);
2364 if (!err
&& ac
->ac_status
!= AC_STATUS_FOUND
&& first_err
)
2367 mb_debug(sb
, "Best len %d, origin len %d, ac_status %u, ac_flags 0x%x, cr %d ret %d\n",
2368 ac
->ac_b_ex
.fe_len
, ac
->ac_o_ex
.fe_len
, ac
->ac_status
,
2369 ac
->ac_flags
, cr
, err
);
2373 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2375 struct super_block
*sb
= PDE_DATA(file_inode(seq
->file
));
2378 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2381 return (void *) ((unsigned long) group
);
2384 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2386 struct super_block
*sb
= PDE_DATA(file_inode(seq
->file
));
2390 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2393 return (void *) ((unsigned long) group
);
2396 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2398 struct super_block
*sb
= PDE_DATA(file_inode(seq
->file
));
2399 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2401 int err
, buddy_loaded
= 0;
2402 struct ext4_buddy e4b
;
2403 struct ext4_group_info
*grinfo
;
2404 unsigned char blocksize_bits
= min_t(unsigned char,
2405 sb
->s_blocksize_bits
,
2406 EXT4_MAX_BLOCK_LOG_SIZE
);
2408 struct ext4_group_info info
;
2409 ext4_grpblk_t counters
[EXT4_MAX_BLOCK_LOG_SIZE
+ 2];
2414 seq_puts(seq
, "#group: free frags first ["
2415 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
2416 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
2418 i
= (blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2419 sizeof(struct ext4_group_info
);
2421 grinfo
= ext4_get_group_info(sb
, group
);
2422 /* Load the group info in memory only if not already loaded. */
2423 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo
))) {
2424 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2426 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2432 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2435 ext4_mb_unload_buddy(&e4b
);
2437 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2438 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2439 for (i
= 0; i
<= 13; i
++)
2440 seq_printf(seq
, " %-5u", i
<= blocksize_bits
+ 1 ?
2441 sg
.info
.bb_counters
[i
] : 0);
2442 seq_printf(seq
, " ]\n");
2447 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2451 const struct seq_operations ext4_mb_seq_groups_ops
= {
2452 .start
= ext4_mb_seq_groups_start
,
2453 .next
= ext4_mb_seq_groups_next
,
2454 .stop
= ext4_mb_seq_groups_stop
,
2455 .show
= ext4_mb_seq_groups_show
,
2458 static struct kmem_cache
*get_groupinfo_cache(int blocksize_bits
)
2460 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2461 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[cache_index
];
2468 * Allocate the top-level s_group_info array for the specified number
2471 int ext4_mb_alloc_groupinfo(struct super_block
*sb
, ext4_group_t ngroups
)
2473 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2475 struct ext4_group_info
***old_groupinfo
, ***new_groupinfo
;
2477 size
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2478 EXT4_DESC_PER_BLOCK_BITS(sb
);
2479 if (size
<= sbi
->s_group_info_size
)
2482 size
= roundup_pow_of_two(sizeof(*sbi
->s_group_info
) * size
);
2483 new_groupinfo
= kvzalloc(size
, GFP_KERNEL
);
2484 if (!new_groupinfo
) {
2485 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy meta group");
2489 old_groupinfo
= rcu_dereference(sbi
->s_group_info
);
2491 memcpy(new_groupinfo
, old_groupinfo
,
2492 sbi
->s_group_info_size
* sizeof(*sbi
->s_group_info
));
2494 rcu_assign_pointer(sbi
->s_group_info
, new_groupinfo
);
2495 sbi
->s_group_info_size
= size
/ sizeof(*sbi
->s_group_info
);
2497 ext4_kvfree_array_rcu(old_groupinfo
);
2498 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2499 sbi
->s_group_info_size
);
2503 /* Create and initialize ext4_group_info data for the given group. */
2504 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2505 struct ext4_group_desc
*desc
)
2509 int idx
= group
>> EXT4_DESC_PER_BLOCK_BITS(sb
);
2510 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2511 struct ext4_group_info
**meta_group_info
;
2512 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2515 * First check if this group is the first of a reserved block.
2516 * If it's true, we have to allocate a new table of pointers
2517 * to ext4_group_info structures
2519 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2520 metalen
= sizeof(*meta_group_info
) <<
2521 EXT4_DESC_PER_BLOCK_BITS(sb
);
2522 meta_group_info
= kmalloc(metalen
, GFP_NOFS
);
2523 if (meta_group_info
== NULL
) {
2524 ext4_msg(sb
, KERN_ERR
, "can't allocate mem "
2525 "for a buddy group");
2526 goto exit_meta_group_info
;
2529 rcu_dereference(sbi
->s_group_info
)[idx
] = meta_group_info
;
2533 meta_group_info
= sbi_array_rcu_deref(sbi
, s_group_info
, idx
);
2534 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2536 meta_group_info
[i
] = kmem_cache_zalloc(cachep
, GFP_NOFS
);
2537 if (meta_group_info
[i
] == NULL
) {
2538 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy mem");
2539 goto exit_group_info
;
2541 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2542 &(meta_group_info
[i
]->bb_state
));
2545 * initialize bb_free to be able to skip
2546 * empty groups without initialization
2548 if (ext4_has_group_desc_csum(sb
) &&
2549 (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))) {
2550 meta_group_info
[i
]->bb_free
=
2551 ext4_free_clusters_after_init(sb
, group
, desc
);
2553 meta_group_info
[i
]->bb_free
=
2554 ext4_free_group_clusters(sb
, desc
);
2557 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2558 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2559 meta_group_info
[i
]->bb_free_root
= RB_ROOT
;
2560 meta_group_info
[i
]->bb_largest_free_order
= -1; /* uninit */
2562 mb_group_bb_bitmap_alloc(sb
, meta_group_info
[i
], group
);
2566 /* If a meta_group_info table has been allocated, release it now */
2567 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2568 struct ext4_group_info
***group_info
;
2571 group_info
= rcu_dereference(sbi
->s_group_info
);
2572 kfree(group_info
[idx
]);
2573 group_info
[idx
] = NULL
;
2576 exit_meta_group_info
:
2578 } /* ext4_mb_add_groupinfo */
2580 static int ext4_mb_init_backend(struct super_block
*sb
)
2582 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2584 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2586 struct ext4_group_desc
*desc
;
2587 struct ext4_group_info
***group_info
;
2588 struct kmem_cache
*cachep
;
2590 err
= ext4_mb_alloc_groupinfo(sb
, ngroups
);
2594 sbi
->s_buddy_cache
= new_inode(sb
);
2595 if (sbi
->s_buddy_cache
== NULL
) {
2596 ext4_msg(sb
, KERN_ERR
, "can't get new inode");
2599 /* To avoid potentially colliding with an valid on-disk inode number,
2600 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2601 * not in the inode hash, so it should never be found by iget(), but
2602 * this will avoid confusion if it ever shows up during debugging. */
2603 sbi
->s_buddy_cache
->i_ino
= EXT4_BAD_INO
;
2604 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2605 for (i
= 0; i
< ngroups
; i
++) {
2607 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2609 ext4_msg(sb
, KERN_ERR
, "can't read descriptor %u", i
);
2612 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2619 cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2621 kmem_cache_free(cachep
, ext4_get_group_info(sb
, i
));
2622 i
= sbi
->s_group_info_size
;
2624 group_info
= rcu_dereference(sbi
->s_group_info
);
2626 kfree(group_info
[i
]);
2628 iput(sbi
->s_buddy_cache
);
2631 kvfree(rcu_dereference(sbi
->s_group_info
));
2636 static void ext4_groupinfo_destroy_slabs(void)
2640 for (i
= 0; i
< NR_GRPINFO_CACHES
; i
++) {
2641 kmem_cache_destroy(ext4_groupinfo_caches
[i
]);
2642 ext4_groupinfo_caches
[i
] = NULL
;
2646 static int ext4_groupinfo_create_slab(size_t size
)
2648 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex
);
2650 int blocksize_bits
= order_base_2(size
);
2651 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2652 struct kmem_cache
*cachep
;
2654 if (cache_index
>= NR_GRPINFO_CACHES
)
2657 if (unlikely(cache_index
< 0))
2660 mutex_lock(&ext4_grpinfo_slab_create_mutex
);
2661 if (ext4_groupinfo_caches
[cache_index
]) {
2662 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2663 return 0; /* Already created */
2666 slab_size
= offsetof(struct ext4_group_info
,
2667 bb_counters
[blocksize_bits
+ 2]);
2669 cachep
= kmem_cache_create(ext4_groupinfo_slab_names
[cache_index
],
2670 slab_size
, 0, SLAB_RECLAIM_ACCOUNT
,
2673 ext4_groupinfo_caches
[cache_index
] = cachep
;
2675 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2678 "EXT4-fs: no memory for groupinfo slab cache\n");
2685 int ext4_mb_init(struct super_block
*sb
)
2687 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2689 unsigned offset
, offset_incr
;
2693 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_offsets
);
2695 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2696 if (sbi
->s_mb_offsets
== NULL
) {
2701 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_maxs
);
2702 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2703 if (sbi
->s_mb_maxs
== NULL
) {
2708 ret
= ext4_groupinfo_create_slab(sb
->s_blocksize
);
2712 /* order 0 is regular bitmap */
2713 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2714 sbi
->s_mb_offsets
[0] = 0;
2718 offset_incr
= 1 << (sb
->s_blocksize_bits
- 1);
2719 max
= sb
->s_blocksize
<< 2;
2721 sbi
->s_mb_offsets
[i
] = offset
;
2722 sbi
->s_mb_maxs
[i
] = max
;
2723 offset
+= offset_incr
;
2724 offset_incr
= offset_incr
>> 1;
2727 } while (i
<= sb
->s_blocksize_bits
+ 1);
2729 spin_lock_init(&sbi
->s_md_lock
);
2730 spin_lock_init(&sbi
->s_bal_lock
);
2731 sbi
->s_mb_free_pending
= 0;
2732 INIT_LIST_HEAD(&sbi
->s_freed_data_list
);
2734 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2735 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2736 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2737 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2738 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2740 * The default group preallocation is 512, which for 4k block
2741 * sizes translates to 2 megabytes. However for bigalloc file
2742 * systems, this is probably too big (i.e, if the cluster size
2743 * is 1 megabyte, then group preallocation size becomes half a
2744 * gigabyte!). As a default, we will keep a two megabyte
2745 * group pralloc size for cluster sizes up to 64k, and after
2746 * that, we will force a minimum group preallocation size of
2747 * 32 clusters. This translates to 8 megs when the cluster
2748 * size is 256k, and 32 megs when the cluster size is 1 meg,
2749 * which seems reasonable as a default.
2751 sbi
->s_mb_group_prealloc
= max(MB_DEFAULT_GROUP_PREALLOC
>>
2752 sbi
->s_cluster_bits
, 32);
2754 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2755 * to the lowest multiple of s_stripe which is bigger than
2756 * the s_mb_group_prealloc as determined above. We want
2757 * the preallocation size to be an exact multiple of the
2758 * RAID stripe size so that preallocations don't fragment
2761 if (sbi
->s_stripe
> 1) {
2762 sbi
->s_mb_group_prealloc
= roundup(
2763 sbi
->s_mb_group_prealloc
, sbi
->s_stripe
);
2766 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2767 if (sbi
->s_locality_groups
== NULL
) {
2771 for_each_possible_cpu(i
) {
2772 struct ext4_locality_group
*lg
;
2773 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2774 mutex_init(&lg
->lg_mutex
);
2775 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2776 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2777 spin_lock_init(&lg
->lg_prealloc_lock
);
2780 /* init file for buddy data */
2781 ret
= ext4_mb_init_backend(sb
);
2783 goto out_free_locality_groups
;
2787 out_free_locality_groups
:
2788 free_percpu(sbi
->s_locality_groups
);
2789 sbi
->s_locality_groups
= NULL
;
2791 kfree(sbi
->s_mb_offsets
);
2792 sbi
->s_mb_offsets
= NULL
;
2793 kfree(sbi
->s_mb_maxs
);
2794 sbi
->s_mb_maxs
= NULL
;
2798 /* need to called with the ext4 group lock held */
2799 static int ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2801 struct ext4_prealloc_space
*pa
;
2802 struct list_head
*cur
, *tmp
;
2805 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2806 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2807 list_del(&pa
->pa_group_list
);
2809 kmem_cache_free(ext4_pspace_cachep
, pa
);
2814 int ext4_mb_release(struct super_block
*sb
)
2816 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2818 int num_meta_group_infos
;
2819 struct ext4_group_info
*grinfo
, ***group_info
;
2820 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2821 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2824 if (sbi
->s_group_info
) {
2825 for (i
= 0; i
< ngroups
; i
++) {
2827 grinfo
= ext4_get_group_info(sb
, i
);
2828 mb_group_bb_bitmap_free(grinfo
);
2829 ext4_lock_group(sb
, i
);
2830 count
= ext4_mb_cleanup_pa(grinfo
);
2832 mb_debug(sb
, "mballoc: %d PAs left\n",
2834 ext4_unlock_group(sb
, i
);
2835 kmem_cache_free(cachep
, grinfo
);
2837 num_meta_group_infos
= (ngroups
+
2838 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2839 EXT4_DESC_PER_BLOCK_BITS(sb
);
2841 group_info
= rcu_dereference(sbi
->s_group_info
);
2842 for (i
= 0; i
< num_meta_group_infos
; i
++)
2843 kfree(group_info
[i
]);
2847 kfree(sbi
->s_mb_offsets
);
2848 kfree(sbi
->s_mb_maxs
);
2849 iput(sbi
->s_buddy_cache
);
2850 if (sbi
->s_mb_stats
) {
2851 ext4_msg(sb
, KERN_INFO
,
2852 "mballoc: %u blocks %u reqs (%u success)",
2853 atomic_read(&sbi
->s_bal_allocated
),
2854 atomic_read(&sbi
->s_bal_reqs
),
2855 atomic_read(&sbi
->s_bal_success
));
2856 ext4_msg(sb
, KERN_INFO
,
2857 "mballoc: %u extents scanned, %u goal hits, "
2858 "%u 2^N hits, %u breaks, %u lost",
2859 atomic_read(&sbi
->s_bal_ex_scanned
),
2860 atomic_read(&sbi
->s_bal_goals
),
2861 atomic_read(&sbi
->s_bal_2orders
),
2862 atomic_read(&sbi
->s_bal_breaks
),
2863 atomic_read(&sbi
->s_mb_lost_chunks
));
2864 ext4_msg(sb
, KERN_INFO
,
2865 "mballoc: %lu generated and it took %Lu",
2866 sbi
->s_mb_buddies_generated
,
2867 sbi
->s_mb_generation_time
);
2868 ext4_msg(sb
, KERN_INFO
,
2869 "mballoc: %u preallocated, %u discarded",
2870 atomic_read(&sbi
->s_mb_preallocated
),
2871 atomic_read(&sbi
->s_mb_discarded
));
2874 free_percpu(sbi
->s_locality_groups
);
2879 static inline int ext4_issue_discard(struct super_block
*sb
,
2880 ext4_group_t block_group
, ext4_grpblk_t cluster
, int count
,
2883 ext4_fsblk_t discard_block
;
2885 discard_block
= (EXT4_C2B(EXT4_SB(sb
), cluster
) +
2886 ext4_group_first_block_no(sb
, block_group
));
2887 count
= EXT4_C2B(EXT4_SB(sb
), count
);
2888 trace_ext4_discard_blocks(sb
,
2889 (unsigned long long) discard_block
, count
);
2891 return __blkdev_issue_discard(sb
->s_bdev
,
2892 (sector_t
)discard_block
<< (sb
->s_blocksize_bits
- 9),
2893 (sector_t
)count
<< (sb
->s_blocksize_bits
- 9),
2896 return sb_issue_discard(sb
, discard_block
, count
, GFP_NOFS
, 0);
2899 static void ext4_free_data_in_buddy(struct super_block
*sb
,
2900 struct ext4_free_data
*entry
)
2902 struct ext4_buddy e4b
;
2903 struct ext4_group_info
*db
;
2904 int err
, count
= 0, count2
= 0;
2906 mb_debug(sb
, "gonna free %u blocks in group %u (0x%p):",
2907 entry
->efd_count
, entry
->efd_group
, entry
);
2909 err
= ext4_mb_load_buddy(sb
, entry
->efd_group
, &e4b
);
2910 /* we expect to find existing buddy because it's pinned */
2913 spin_lock(&EXT4_SB(sb
)->s_md_lock
);
2914 EXT4_SB(sb
)->s_mb_free_pending
-= entry
->efd_count
;
2915 spin_unlock(&EXT4_SB(sb
)->s_md_lock
);
2918 /* there are blocks to put in buddy to make them really free */
2919 count
+= entry
->efd_count
;
2921 ext4_lock_group(sb
, entry
->efd_group
);
2922 /* Take it out of per group rb tree */
2923 rb_erase(&entry
->efd_node
, &(db
->bb_free_root
));
2924 mb_free_blocks(NULL
, &e4b
, entry
->efd_start_cluster
, entry
->efd_count
);
2927 * Clear the trimmed flag for the group so that the next
2928 * ext4_trim_fs can trim it.
2929 * If the volume is mounted with -o discard, online discard
2930 * is supported and the free blocks will be trimmed online.
2932 if (!test_opt(sb
, DISCARD
))
2933 EXT4_MB_GRP_CLEAR_TRIMMED(db
);
2935 if (!db
->bb_free_root
.rb_node
) {
2936 /* No more items in the per group rb tree
2937 * balance refcounts from ext4_mb_free_metadata()
2939 put_page(e4b
.bd_buddy_page
);
2940 put_page(e4b
.bd_bitmap_page
);
2942 ext4_unlock_group(sb
, entry
->efd_group
);
2943 kmem_cache_free(ext4_free_data_cachep
, entry
);
2944 ext4_mb_unload_buddy(&e4b
);
2946 mb_debug(sb
, "freed %d blocks in %d structures\n", count
,
2951 * This function is called by the jbd2 layer once the commit has finished,
2952 * so we know we can free the blocks that were released with that commit.
2954 void ext4_process_freed_data(struct super_block
*sb
, tid_t commit_tid
)
2956 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2957 struct ext4_free_data
*entry
, *tmp
;
2958 struct bio
*discard_bio
= NULL
;
2959 struct list_head freed_data_list
;
2960 struct list_head
*cut_pos
= NULL
;
2963 INIT_LIST_HEAD(&freed_data_list
);
2965 spin_lock(&sbi
->s_md_lock
);
2966 list_for_each_entry(entry
, &sbi
->s_freed_data_list
, efd_list
) {
2967 if (entry
->efd_tid
!= commit_tid
)
2969 cut_pos
= &entry
->efd_list
;
2972 list_cut_position(&freed_data_list
, &sbi
->s_freed_data_list
,
2974 spin_unlock(&sbi
->s_md_lock
);
2976 if (test_opt(sb
, DISCARD
)) {
2977 list_for_each_entry(entry
, &freed_data_list
, efd_list
) {
2978 err
= ext4_issue_discard(sb
, entry
->efd_group
,
2979 entry
->efd_start_cluster
,
2982 if (err
&& err
!= -EOPNOTSUPP
) {
2983 ext4_msg(sb
, KERN_WARNING
, "discard request in"
2984 " group:%d block:%d count:%d failed"
2985 " with %d", entry
->efd_group
,
2986 entry
->efd_start_cluster
,
2987 entry
->efd_count
, err
);
2988 } else if (err
== -EOPNOTSUPP
)
2993 submit_bio_wait(discard_bio
);
2994 bio_put(discard_bio
);
2998 list_for_each_entry_safe(entry
, tmp
, &freed_data_list
, efd_list
)
2999 ext4_free_data_in_buddy(sb
, entry
);
3002 int __init
ext4_init_mballoc(void)
3004 ext4_pspace_cachep
= KMEM_CACHE(ext4_prealloc_space
,
3005 SLAB_RECLAIM_ACCOUNT
);
3006 if (ext4_pspace_cachep
== NULL
)
3009 ext4_ac_cachep
= KMEM_CACHE(ext4_allocation_context
,
3010 SLAB_RECLAIM_ACCOUNT
);
3011 if (ext4_ac_cachep
== NULL
)
3014 ext4_free_data_cachep
= KMEM_CACHE(ext4_free_data
,
3015 SLAB_RECLAIM_ACCOUNT
);
3016 if (ext4_free_data_cachep
== NULL
)
3022 kmem_cache_destroy(ext4_ac_cachep
);
3024 kmem_cache_destroy(ext4_pspace_cachep
);
3029 void ext4_exit_mballoc(void)
3032 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
3033 * before destroying the slab cache.
3036 kmem_cache_destroy(ext4_pspace_cachep
);
3037 kmem_cache_destroy(ext4_ac_cachep
);
3038 kmem_cache_destroy(ext4_free_data_cachep
);
3039 ext4_groupinfo_destroy_slabs();
3044 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
3045 * Returns 0 if success or error code
3047 static noinline_for_stack
int
3048 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
3049 handle_t
*handle
, unsigned int reserv_clstrs
)
3051 struct buffer_head
*bitmap_bh
= NULL
;
3052 struct ext4_group_desc
*gdp
;
3053 struct buffer_head
*gdp_bh
;
3054 struct ext4_sb_info
*sbi
;
3055 struct super_block
*sb
;
3059 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3060 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
3065 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
3066 if (IS_ERR(bitmap_bh
)) {
3067 err
= PTR_ERR(bitmap_bh
);
3072 BUFFER_TRACE(bitmap_bh
, "getting write access");
3073 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
3078 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
3082 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
3083 ext4_free_group_clusters(sb
, gdp
));
3085 BUFFER_TRACE(gdp_bh
, "get_write_access");
3086 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
3090 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3092 len
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
3093 if (!ext4_data_block_valid(sbi
, block
, len
)) {
3094 ext4_error(sb
, "Allocating blocks %llu-%llu which overlap "
3095 "fs metadata", block
, block
+len
);
3096 /* File system mounted not to panic on error
3097 * Fix the bitmap and return EFSCORRUPTED
3098 * We leak some of the blocks here.
3100 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3101 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
3102 ac
->ac_b_ex
.fe_len
);
3103 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3104 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3106 err
= -EFSCORRUPTED
;
3110 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3111 #ifdef AGGRESSIVE_CHECK
3114 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
3115 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
3116 bitmap_bh
->b_data
));
3120 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
3121 ac
->ac_b_ex
.fe_len
);
3122 if (ext4_has_group_desc_csum(sb
) &&
3123 (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
))) {
3124 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
3125 ext4_free_group_clusters_set(sb
, gdp
,
3126 ext4_free_clusters_after_init(sb
,
3127 ac
->ac_b_ex
.fe_group
, gdp
));
3129 len
= ext4_free_group_clusters(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
3130 ext4_free_group_clusters_set(sb
, gdp
, len
);
3131 ext4_block_bitmap_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
, bitmap_bh
);
3132 ext4_group_desc_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
);
3134 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3135 percpu_counter_sub(&sbi
->s_freeclusters_counter
, ac
->ac_b_ex
.fe_len
);
3137 * Now reduce the dirty block count also. Should not go negative
3139 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3140 /* release all the reserved blocks if non delalloc */
3141 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
3144 if (sbi
->s_log_groups_per_flex
) {
3145 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3146 ac
->ac_b_ex
.fe_group
);
3147 atomic64_sub(ac
->ac_b_ex
.fe_len
,
3148 &sbi_array_rcu_deref(sbi
, s_flex_groups
,
3149 flex_group
)->free_clusters
);
3152 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3155 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3163 * here we normalize request for locality group
3164 * Group request are normalized to s_mb_group_prealloc, which goes to
3165 * s_strip if we set the same via mount option.
3166 * s_mb_group_prealloc can be configured via
3167 * /sys/fs/ext4/<partition>/mb_group_prealloc
3169 * XXX: should we try to preallocate more than the group has now?
3171 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3173 struct super_block
*sb
= ac
->ac_sb
;
3174 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3177 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3178 mb_debug(sb
, "goal %u blocks for locality group\n", ac
->ac_g_ex
.fe_len
);
3182 * Normalization means making request better in terms of
3183 * size and alignment
3185 static noinline_for_stack
void
3186 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3187 struct ext4_allocation_request
*ar
)
3189 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3192 loff_t size
, start_off
;
3193 loff_t orig_size __maybe_unused
;
3195 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3196 struct ext4_prealloc_space
*pa
;
3198 /* do normalize only data requests, metadata requests
3199 do not need preallocation */
3200 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3203 /* sometime caller may want exact blocks */
3204 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3207 /* caller may indicate that preallocation isn't
3208 * required (it's a tail, for example) */
3209 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3212 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3213 ext4_mb_normalize_group_request(ac
);
3217 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3219 /* first, let's learn actual file size
3220 * given current request is allocated */
3221 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
3222 size
= size
<< bsbits
;
3223 if (size
< i_size_read(ac
->ac_inode
))
3224 size
= i_size_read(ac
->ac_inode
);
3227 /* max size of free chunks */
3230 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3231 (req <= (size) || max <= (chunk_size))
3233 /* first, try to predict filesize */
3234 /* XXX: should this table be tunable? */
3236 if (size
<= 16 * 1024) {
3238 } else if (size
<= 32 * 1024) {
3240 } else if (size
<= 64 * 1024) {
3242 } else if (size
<= 128 * 1024) {
3244 } else if (size
<= 256 * 1024) {
3246 } else if (size
<= 512 * 1024) {
3248 } else if (size
<= 1024 * 1024) {
3250 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3251 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3252 (21 - bsbits
)) << 21;
3253 size
= 2 * 1024 * 1024;
3254 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3255 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3256 (22 - bsbits
)) << 22;
3257 size
= 4 * 1024 * 1024;
3258 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3259 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3260 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3261 (23 - bsbits
)) << 23;
3262 size
= 8 * 1024 * 1024;
3264 start_off
= (loff_t
) ac
->ac_o_ex
.fe_logical
<< bsbits
;
3265 size
= (loff_t
) EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3266 ac
->ac_o_ex
.fe_len
) << bsbits
;
3268 size
= size
>> bsbits
;
3269 start
= start_off
>> bsbits
;
3271 /* don't cover already allocated blocks in selected range */
3272 if (ar
->pleft
&& start
<= ar
->lleft
) {
3273 size
-= ar
->lleft
+ 1 - start
;
3274 start
= ar
->lleft
+ 1;
3276 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3277 size
-= start
+ size
- ar
->lright
;
3280 * Trim allocation request for filesystems with artificially small
3283 if (size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
))
3284 size
= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
);
3288 /* check we don't cross already preallocated blocks */
3290 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3295 spin_lock(&pa
->pa_lock
);
3296 if (pa
->pa_deleted
) {
3297 spin_unlock(&pa
->pa_lock
);
3301 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3304 /* PA must not overlap original request */
3305 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3306 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3308 /* skip PAs this normalized request doesn't overlap with */
3309 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
3310 spin_unlock(&pa
->pa_lock
);
3313 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3315 /* adjust start or end to be adjacent to this pa */
3316 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3317 BUG_ON(pa_end
< start
);
3319 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3320 BUG_ON(pa
->pa_lstart
> end
);
3321 end
= pa
->pa_lstart
;
3323 spin_unlock(&pa
->pa_lock
);
3328 /* XXX: extra loop to check we really don't overlap preallocations */
3330 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3333 spin_lock(&pa
->pa_lock
);
3334 if (pa
->pa_deleted
== 0) {
3335 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3337 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3339 spin_unlock(&pa
->pa_lock
);
3343 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3344 start
> ac
->ac_o_ex
.fe_logical
) {
3345 ext4_msg(ac
->ac_sb
, KERN_ERR
,
3346 "start %lu, size %lu, fe_logical %lu",
3347 (unsigned long) start
, (unsigned long) size
,
3348 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3351 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3353 /* now prepare goal request */
3355 /* XXX: is it better to align blocks WRT to logical
3356 * placement or satisfy big request as is */
3357 ac
->ac_g_ex
.fe_logical
= start
;
3358 ac
->ac_g_ex
.fe_len
= EXT4_NUM_B2C(sbi
, size
);
3360 /* define goal start in order to merge */
3361 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3362 /* merge to the right */
3363 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3364 &ac
->ac_f_ex
.fe_group
,
3365 &ac
->ac_f_ex
.fe_start
);
3366 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3368 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3369 /* merge to the left */
3370 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3371 &ac
->ac_f_ex
.fe_group
,
3372 &ac
->ac_f_ex
.fe_start
);
3373 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3376 mb_debug(ac
->ac_sb
, "goal: %lld(was %lld) blocks at %u\n", size
,
3380 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3382 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3384 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3385 atomic_inc(&sbi
->s_bal_reqs
);
3386 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3387 if (ac
->ac_b_ex
.fe_len
>= ac
->ac_o_ex
.fe_len
)
3388 atomic_inc(&sbi
->s_bal_success
);
3389 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3390 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3391 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3392 atomic_inc(&sbi
->s_bal_goals
);
3393 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3394 atomic_inc(&sbi
->s_bal_breaks
);
3397 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
)
3398 trace_ext4_mballoc_alloc(ac
);
3400 trace_ext4_mballoc_prealloc(ac
);
3404 * Called on failure; free up any blocks from the inode PA for this
3405 * context. We don't need this for MB_GROUP_PA because we only change
3406 * pa_free in ext4_mb_release_context(), but on failure, we've already
3407 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3409 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3411 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3412 struct ext4_buddy e4b
;
3416 if (ac
->ac_f_ex
.fe_len
== 0)
3418 err
= ext4_mb_load_buddy(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
, &e4b
);
3421 * This should never happen since we pin the
3422 * pages in the ext4_allocation_context so
3423 * ext4_mb_load_buddy() should never fail.
3425 WARN(1, "mb_load_buddy failed (%d)", err
);
3428 ext4_lock_group(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
);
3429 mb_free_blocks(ac
->ac_inode
, &e4b
, ac
->ac_f_ex
.fe_start
,
3430 ac
->ac_f_ex
.fe_len
);
3431 ext4_unlock_group(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
);
3432 ext4_mb_unload_buddy(&e4b
);
3435 if (pa
->pa_type
== MB_INODE_PA
)
3436 pa
->pa_free
+= ac
->ac_b_ex
.fe_len
;
3440 * use blocks preallocated to inode
3442 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3443 struct ext4_prealloc_space
*pa
)
3445 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3450 /* found preallocated blocks, use them */
3451 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3452 end
= min(pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
),
3453 start
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
));
3454 len
= EXT4_NUM_B2C(sbi
, end
- start
);
3455 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3456 &ac
->ac_b_ex
.fe_start
);
3457 ac
->ac_b_ex
.fe_len
= len
;
3458 ac
->ac_status
= AC_STATUS_FOUND
;
3461 BUG_ON(start
< pa
->pa_pstart
);
3462 BUG_ON(end
> pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
));
3463 BUG_ON(pa
->pa_free
< len
);
3466 mb_debug(ac
->ac_sb
, "use %llu/%d from inode pa %p\n", start
, len
, pa
);
3470 * use blocks preallocated to locality group
3472 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3473 struct ext4_prealloc_space
*pa
)
3475 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3477 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3478 &ac
->ac_b_ex
.fe_group
,
3479 &ac
->ac_b_ex
.fe_start
);
3480 ac
->ac_b_ex
.fe_len
= len
;
3481 ac
->ac_status
= AC_STATUS_FOUND
;
3484 /* we don't correct pa_pstart or pa_plen here to avoid
3485 * possible race when the group is being loaded concurrently
3486 * instead we correct pa later, after blocks are marked
3487 * in on-disk bitmap -- see ext4_mb_release_context()
3488 * Other CPUs are prevented from allocating from this pa by lg_mutex
3490 mb_debug(ac
->ac_sb
, "use %u/%u from group pa %p\n",
3491 pa
->pa_lstart
-len
, len
, pa
);
3495 * Return the prealloc space that have minimal distance
3496 * from the goal block. @cpa is the prealloc
3497 * space that is having currently known minimal distance
3498 * from the goal block.
3500 static struct ext4_prealloc_space
*
3501 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3502 struct ext4_prealloc_space
*pa
,
3503 struct ext4_prealloc_space
*cpa
)
3505 ext4_fsblk_t cur_distance
, new_distance
;
3508 atomic_inc(&pa
->pa_count
);
3511 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3512 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3514 if (cur_distance
<= new_distance
)
3517 /* drop the previous reference */
3518 atomic_dec(&cpa
->pa_count
);
3519 atomic_inc(&pa
->pa_count
);
3524 * search goal blocks in preallocated space
3526 static noinline_for_stack
bool
3527 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3529 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3531 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3532 struct ext4_locality_group
*lg
;
3533 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3534 ext4_fsblk_t goal_block
;
3536 /* only data can be preallocated */
3537 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3540 /* first, try per-file preallocation */
3542 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3544 /* all fields in this condition don't change,
3545 * so we can skip locking for them */
3546 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3547 ac
->ac_o_ex
.fe_logical
>= (pa
->pa_lstart
+
3548 EXT4_C2B(sbi
, pa
->pa_len
)))
3551 /* non-extent files can't have physical blocks past 2^32 */
3552 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)) &&
3553 (pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
) >
3554 EXT4_MAX_BLOCK_FILE_PHYS
))
3557 /* found preallocated blocks, use them */
3558 spin_lock(&pa
->pa_lock
);
3559 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3560 atomic_inc(&pa
->pa_count
);
3561 ext4_mb_use_inode_pa(ac
, pa
);
3562 spin_unlock(&pa
->pa_lock
);
3563 ac
->ac_criteria
= 10;
3567 spin_unlock(&pa
->pa_lock
);
3571 /* can we use group allocation? */
3572 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3575 /* inode may have no locality group for some reason */
3579 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3580 if (order
> PREALLOC_TB_SIZE
- 1)
3581 /* The max size of hash table is PREALLOC_TB_SIZE */
3582 order
= PREALLOC_TB_SIZE
- 1;
3584 goal_block
= ext4_grp_offs_to_block(ac
->ac_sb
, &ac
->ac_g_ex
);
3586 * search for the prealloc space that is having
3587 * minimal distance from the goal block.
3589 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3591 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3593 spin_lock(&pa
->pa_lock
);
3594 if (pa
->pa_deleted
== 0 &&
3595 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3597 cpa
= ext4_mb_check_group_pa(goal_block
,
3600 spin_unlock(&pa
->pa_lock
);
3605 ext4_mb_use_group_pa(ac
, cpa
);
3606 ac
->ac_criteria
= 20;
3613 * the function goes through all block freed in the group
3614 * but not yet committed and marks them used in in-core bitmap.
3615 * buddy must be generated from this bitmap
3616 * Need to be called with the ext4 group lock held
3618 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3622 struct ext4_group_info
*grp
;
3623 struct ext4_free_data
*entry
;
3625 grp
= ext4_get_group_info(sb
, group
);
3626 n
= rb_first(&(grp
->bb_free_root
));
3629 entry
= rb_entry(n
, struct ext4_free_data
, efd_node
);
3630 ext4_set_bits(bitmap
, entry
->efd_start_cluster
, entry
->efd_count
);
3637 * the function goes through all preallocation in this group and marks them
3638 * used in in-core bitmap. buddy must be generated from this bitmap
3639 * Need to be called with ext4 group lock held
3641 static noinline_for_stack
3642 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3645 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3646 struct ext4_prealloc_space
*pa
;
3647 struct list_head
*cur
;
3648 ext4_group_t groupnr
;
3649 ext4_grpblk_t start
;
3650 int preallocated
= 0;
3653 /* all form of preallocation discards first load group,
3654 * so the only competing code is preallocation use.
3655 * we don't need any locking here
3656 * notice we do NOT ignore preallocations with pa_deleted
3657 * otherwise we could leave used blocks available for
3658 * allocation in buddy when concurrent ext4_mb_put_pa()
3659 * is dropping preallocation
3661 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3662 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3663 spin_lock(&pa
->pa_lock
);
3664 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3667 spin_unlock(&pa
->pa_lock
);
3668 if (unlikely(len
== 0))
3670 BUG_ON(groupnr
!= group
);
3671 ext4_set_bits(bitmap
, start
, len
);
3672 preallocated
+= len
;
3674 mb_debug(sb
, "preallocated %d for group %u\n", preallocated
, group
);
3677 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3679 struct ext4_prealloc_space
*pa
;
3680 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3682 BUG_ON(atomic_read(&pa
->pa_count
));
3683 BUG_ON(pa
->pa_deleted
== 0);
3684 kmem_cache_free(ext4_pspace_cachep
, pa
);
3688 * drops a reference to preallocated space descriptor
3689 * if this was the last reference and the space is consumed
3691 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3692 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3695 ext4_fsblk_t grp_blk
;
3697 /* in this short window concurrent discard can set pa_deleted */
3698 spin_lock(&pa
->pa_lock
);
3699 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0) {
3700 spin_unlock(&pa
->pa_lock
);
3704 if (pa
->pa_deleted
== 1) {
3705 spin_unlock(&pa
->pa_lock
);
3710 spin_unlock(&pa
->pa_lock
);
3712 grp_blk
= pa
->pa_pstart
;
3714 * If doing group-based preallocation, pa_pstart may be in the
3715 * next group when pa is used up
3717 if (pa
->pa_type
== MB_GROUP_PA
)
3720 grp
= ext4_get_group_number(sb
, grp_blk
);
3725 * P1 (buddy init) P2 (regular allocation)
3726 * find block B in PA
3727 * copy on-disk bitmap to buddy
3728 * mark B in on-disk bitmap
3729 * drop PA from group
3730 * mark all PAs in buddy
3732 * thus, P1 initializes buddy with B available. to prevent this
3733 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3736 ext4_lock_group(sb
, grp
);
3737 list_del(&pa
->pa_group_list
);
3738 ext4_unlock_group(sb
, grp
);
3740 spin_lock(pa
->pa_obj_lock
);
3741 list_del_rcu(&pa
->pa_inode_list
);
3742 spin_unlock(pa
->pa_obj_lock
);
3744 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3748 * creates new preallocated space for given inode
3750 static noinline_for_stack
void
3751 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3753 struct super_block
*sb
= ac
->ac_sb
;
3754 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3755 struct ext4_prealloc_space
*pa
;
3756 struct ext4_group_info
*grp
;
3757 struct ext4_inode_info
*ei
;
3759 /* preallocate only when found space is larger then requested */
3760 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3761 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3762 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3763 BUG_ON(ac
->ac_pa
== NULL
);
3767 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3773 /* we can't allocate as much as normalizer wants.
3774 * so, found space must get proper lstart
3775 * to cover original request */
3776 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3777 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3779 /* we're limited by original request in that
3780 * logical block must be covered any way
3781 * winl is window we can move our chunk within */
3782 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3784 /* also, we should cover whole original request */
3785 wins
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
);
3787 /* the smallest one defines real window */
3788 win
= min(winl
, wins
);
3790 offs
= ac
->ac_o_ex
.fe_logical
%
3791 EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
3792 if (offs
&& offs
< win
)
3795 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
-
3796 EXT4_NUM_B2C(sbi
, win
);
3797 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3798 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3801 /* preallocation can change ac_b_ex, thus we store actually
3802 * allocated blocks for history */
3803 ac
->ac_f_ex
= ac
->ac_b_ex
;
3805 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3806 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3807 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3808 pa
->pa_free
= pa
->pa_len
;
3809 spin_lock_init(&pa
->pa_lock
);
3810 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3811 INIT_LIST_HEAD(&pa
->pa_group_list
);
3813 pa
->pa_type
= MB_INODE_PA
;
3815 mb_debug(sb
, "new inode pa %p: %llu/%d for %u\n", pa
, pa
->pa_pstart
,
3816 pa
->pa_len
, pa
->pa_lstart
);
3817 trace_ext4_mb_new_inode_pa(ac
, pa
);
3819 ext4_mb_use_inode_pa(ac
, pa
);
3820 atomic_add(pa
->pa_free
, &sbi
->s_mb_preallocated
);
3822 ei
= EXT4_I(ac
->ac_inode
);
3823 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3825 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3826 pa
->pa_inode
= ac
->ac_inode
;
3828 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3830 spin_lock(pa
->pa_obj_lock
);
3831 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3832 spin_unlock(pa
->pa_obj_lock
);
3836 * creates new preallocated space for locality group inodes belongs to
3838 static noinline_for_stack
void
3839 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3841 struct super_block
*sb
= ac
->ac_sb
;
3842 struct ext4_locality_group
*lg
;
3843 struct ext4_prealloc_space
*pa
;
3844 struct ext4_group_info
*grp
;
3846 /* preallocate only when found space is larger then requested */
3847 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3848 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3849 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3850 BUG_ON(ac
->ac_pa
== NULL
);
3854 /* preallocation can change ac_b_ex, thus we store actually
3855 * allocated blocks for history */
3856 ac
->ac_f_ex
= ac
->ac_b_ex
;
3858 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3859 pa
->pa_lstart
= pa
->pa_pstart
;
3860 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3861 pa
->pa_free
= pa
->pa_len
;
3862 spin_lock_init(&pa
->pa_lock
);
3863 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3864 INIT_LIST_HEAD(&pa
->pa_group_list
);
3866 pa
->pa_type
= MB_GROUP_PA
;
3868 mb_debug(sb
, "new group pa %p: %llu/%d for %u\n", pa
, pa
->pa_pstart
,
3869 pa
->pa_len
, pa
->pa_lstart
);
3870 trace_ext4_mb_new_group_pa(ac
, pa
);
3872 ext4_mb_use_group_pa(ac
, pa
);
3873 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3875 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3879 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3880 pa
->pa_inode
= NULL
;
3882 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3885 * We will later add the new pa to the right bucket
3886 * after updating the pa_free in ext4_mb_release_context
3890 static void ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3892 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3893 ext4_mb_new_group_pa(ac
);
3895 ext4_mb_new_inode_pa(ac
);
3899 * finds all unused blocks in on-disk bitmap, frees them in
3900 * in-core bitmap and buddy.
3901 * @pa must be unlinked from inode and group lists, so that
3902 * nobody else can find/use it.
3903 * the caller MUST hold group/inode locks.
3904 * TODO: optimize the case when there are no in-core structures yet
3906 static noinline_for_stack
int
3907 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3908 struct ext4_prealloc_space
*pa
)
3910 struct super_block
*sb
= e4b
->bd_sb
;
3911 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3916 unsigned long long grp_blk_start
;
3919 BUG_ON(pa
->pa_deleted
== 0);
3920 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3921 grp_blk_start
= pa
->pa_pstart
- EXT4_C2B(sbi
, bit
);
3922 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3923 end
= bit
+ pa
->pa_len
;
3926 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3929 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3930 mb_debug(sb
, "free preallocated %u/%u in group %u\n",
3931 (unsigned) ext4_group_first_block_no(sb
, group
) + bit
,
3932 (unsigned) next
- bit
, (unsigned) group
);
3935 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, next
- bit
);
3936 trace_ext4_mb_release_inode_pa(pa
, (grp_blk_start
+
3937 EXT4_C2B(sbi
, bit
)),
3939 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3942 if (free
!= pa
->pa_free
) {
3943 ext4_msg(e4b
->bd_sb
, KERN_CRIT
,
3944 "pa %p: logic %lu, phys. %lu, len %d",
3945 pa
, (unsigned long) pa
->pa_lstart
,
3946 (unsigned long) pa
->pa_pstart
,
3948 ext4_grp_locked_error(sb
, group
, 0, 0, "free %u, pa_free %u",
3951 * pa is already deleted so we use the value obtained
3952 * from the bitmap and continue.
3955 atomic_add(free
, &sbi
->s_mb_discarded
);
3960 static noinline_for_stack
int
3961 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3962 struct ext4_prealloc_space
*pa
)
3964 struct super_block
*sb
= e4b
->bd_sb
;
3968 trace_ext4_mb_release_group_pa(sb
, pa
);
3969 BUG_ON(pa
->pa_deleted
== 0);
3970 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3971 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3972 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3973 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3974 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, pa
->pa_len
);
3980 * releases all preallocations in given group
3982 * first, we need to decide discard policy:
3983 * - when do we discard
3985 * - how many do we discard
3986 * 1) how many requested
3988 static noinline_for_stack
int
3989 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3990 ext4_group_t group
, int needed
)
3992 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3993 struct buffer_head
*bitmap_bh
= NULL
;
3994 struct ext4_prealloc_space
*pa
, *tmp
;
3995 struct list_head list
;
3996 struct ext4_buddy e4b
;
4001 mb_debug(sb
, "discard preallocation for group %u\n", group
);
4002 if (list_empty(&grp
->bb_prealloc_list
))
4005 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4006 if (IS_ERR(bitmap_bh
)) {
4007 err
= PTR_ERR(bitmap_bh
);
4008 ext4_error_err(sb
, -err
,
4009 "Error %d reading block bitmap for %u",
4014 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
4016 ext4_warning(sb
, "Error %d loading buddy information for %u",
4023 needed
= EXT4_CLUSTERS_PER_GROUP(sb
) + 1;
4025 INIT_LIST_HEAD(&list
);
4027 ext4_lock_group(sb
, group
);
4028 this_cpu_inc(discard_pa_seq
);
4029 list_for_each_entry_safe(pa
, tmp
,
4030 &grp
->bb_prealloc_list
, pa_group_list
) {
4031 spin_lock(&pa
->pa_lock
);
4032 if (atomic_read(&pa
->pa_count
)) {
4033 spin_unlock(&pa
->pa_lock
);
4037 if (pa
->pa_deleted
) {
4038 spin_unlock(&pa
->pa_lock
);
4042 /* seems this one can be freed ... */
4045 /* we can trust pa_free ... */
4046 free
+= pa
->pa_free
;
4048 spin_unlock(&pa
->pa_lock
);
4050 list_del(&pa
->pa_group_list
);
4051 list_add(&pa
->u
.pa_tmp_list
, &list
);
4054 /* if we still need more blocks and some PAs were used, try again */
4055 if (free
< needed
&& busy
) {
4057 ext4_unlock_group(sb
, group
);
4062 /* found anything to free? */
4063 if (list_empty(&list
)) {
4065 mb_debug(sb
, "Someone else may have freed PA for this group %u\n",
4070 /* now free all selected PAs */
4071 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4073 /* remove from object (inode or locality group) */
4074 spin_lock(pa
->pa_obj_lock
);
4075 list_del_rcu(&pa
->pa_inode_list
);
4076 spin_unlock(pa
->pa_obj_lock
);
4078 if (pa
->pa_type
== MB_GROUP_PA
)
4079 ext4_mb_release_group_pa(&e4b
, pa
);
4081 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
4083 list_del(&pa
->u
.pa_tmp_list
);
4084 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4088 ext4_unlock_group(sb
, group
);
4089 ext4_mb_unload_buddy(&e4b
);
4092 mb_debug(sb
, "discarded (%d) blocks preallocated for group %u bb_free (%d)\n",
4093 free
, group
, grp
->bb_free
);
4098 * releases all non-used preallocated blocks for given inode
4100 * It's important to discard preallocations under i_data_sem
4101 * We don't want another block to be served from the prealloc
4102 * space when we are discarding the inode prealloc space.
4104 * FIXME!! Make sure it is valid at all the call sites
4106 void ext4_discard_preallocations(struct inode
*inode
)
4108 struct ext4_inode_info
*ei
= EXT4_I(inode
);
4109 struct super_block
*sb
= inode
->i_sb
;
4110 struct buffer_head
*bitmap_bh
= NULL
;
4111 struct ext4_prealloc_space
*pa
, *tmp
;
4112 ext4_group_t group
= 0;
4113 struct list_head list
;
4114 struct ext4_buddy e4b
;
4117 if (!S_ISREG(inode
->i_mode
)) {
4118 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4122 mb_debug(sb
, "discard preallocation for inode %lu\n",
4124 trace_ext4_discard_preallocations(inode
);
4126 INIT_LIST_HEAD(&list
);
4129 /* first, collect all pa's in the inode */
4130 spin_lock(&ei
->i_prealloc_lock
);
4131 while (!list_empty(&ei
->i_prealloc_list
)) {
4132 pa
= list_entry(ei
->i_prealloc_list
.next
,
4133 struct ext4_prealloc_space
, pa_inode_list
);
4134 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
4135 spin_lock(&pa
->pa_lock
);
4136 if (atomic_read(&pa
->pa_count
)) {
4137 /* this shouldn't happen often - nobody should
4138 * use preallocation while we're discarding it */
4139 spin_unlock(&pa
->pa_lock
);
4140 spin_unlock(&ei
->i_prealloc_lock
);
4141 ext4_msg(sb
, KERN_ERR
,
4142 "uh-oh! used pa while discarding");
4144 schedule_timeout_uninterruptible(HZ
);
4148 if (pa
->pa_deleted
== 0) {
4150 spin_unlock(&pa
->pa_lock
);
4151 list_del_rcu(&pa
->pa_inode_list
);
4152 list_add(&pa
->u
.pa_tmp_list
, &list
);
4156 /* someone is deleting pa right now */
4157 spin_unlock(&pa
->pa_lock
);
4158 spin_unlock(&ei
->i_prealloc_lock
);
4160 /* we have to wait here because pa_deleted
4161 * doesn't mean pa is already unlinked from
4162 * the list. as we might be called from
4163 * ->clear_inode() the inode will get freed
4164 * and concurrent thread which is unlinking
4165 * pa from inode's list may access already
4166 * freed memory, bad-bad-bad */
4168 /* XXX: if this happens too often, we can
4169 * add a flag to force wait only in case
4170 * of ->clear_inode(), but not in case of
4171 * regular truncate */
4172 schedule_timeout_uninterruptible(HZ
);
4175 spin_unlock(&ei
->i_prealloc_lock
);
4177 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4178 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4179 group
= ext4_get_group_number(sb
, pa
->pa_pstart
);
4181 err
= ext4_mb_load_buddy_gfp(sb
, group
, &e4b
,
4182 GFP_NOFS
|__GFP_NOFAIL
);
4184 ext4_error_err(sb
, -err
, "Error %d loading buddy information for %u",
4189 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4190 if (IS_ERR(bitmap_bh
)) {
4191 err
= PTR_ERR(bitmap_bh
);
4192 ext4_error_err(sb
, -err
, "Error %d reading block bitmap for %u",
4194 ext4_mb_unload_buddy(&e4b
);
4198 ext4_lock_group(sb
, group
);
4199 list_del(&pa
->pa_group_list
);
4200 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
4201 ext4_unlock_group(sb
, group
);
4203 ext4_mb_unload_buddy(&e4b
);
4206 list_del(&pa
->u
.pa_tmp_list
);
4207 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4211 static int ext4_mb_pa_alloc(struct ext4_allocation_context
*ac
)
4213 struct ext4_prealloc_space
*pa
;
4215 BUG_ON(ext4_pspace_cachep
== NULL
);
4216 pa
= kmem_cache_zalloc(ext4_pspace_cachep
, GFP_NOFS
);
4219 atomic_set(&pa
->pa_count
, 1);
4224 static void ext4_mb_pa_free(struct ext4_allocation_context
*ac
)
4226 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4230 WARN_ON(!atomic_dec_and_test(&pa
->pa_count
));
4231 kmem_cache_free(ext4_pspace_cachep
, pa
);
4234 #ifdef CONFIG_EXT4_DEBUG
4235 static inline void ext4_mb_show_pa(struct super_block
*sb
)
4237 ext4_group_t i
, ngroups
;
4239 if (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
)
4242 ngroups
= ext4_get_groups_count(sb
);
4243 mb_debug(sb
, "groups: ");
4244 for (i
= 0; i
< ngroups
; i
++) {
4245 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4246 struct ext4_prealloc_space
*pa
;
4247 ext4_grpblk_t start
;
4248 struct list_head
*cur
;
4249 ext4_lock_group(sb
, i
);
4250 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4251 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4253 spin_lock(&pa
->pa_lock
);
4254 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4256 spin_unlock(&pa
->pa_lock
);
4257 mb_debug(sb
, "PA:%u:%d:%d\n", i
, start
,
4260 ext4_unlock_group(sb
, i
);
4261 mb_debug(sb
, "%u: %d/%d\n", i
, grp
->bb_free
,
4266 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4268 struct super_block
*sb
= ac
->ac_sb
;
4270 if (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
)
4273 mb_debug(sb
, "Can't allocate:"
4274 " Allocation context details:");
4275 mb_debug(sb
, "status %u flags 0x%x",
4276 ac
->ac_status
, ac
->ac_flags
);
4277 mb_debug(sb
, "orig %lu/%lu/%lu@%lu, "
4278 "goal %lu/%lu/%lu@%lu, "
4279 "best %lu/%lu/%lu@%lu cr %d",
4280 (unsigned long)ac
->ac_o_ex
.fe_group
,
4281 (unsigned long)ac
->ac_o_ex
.fe_start
,
4282 (unsigned long)ac
->ac_o_ex
.fe_len
,
4283 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4284 (unsigned long)ac
->ac_g_ex
.fe_group
,
4285 (unsigned long)ac
->ac_g_ex
.fe_start
,
4286 (unsigned long)ac
->ac_g_ex
.fe_len
,
4287 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4288 (unsigned long)ac
->ac_b_ex
.fe_group
,
4289 (unsigned long)ac
->ac_b_ex
.fe_start
,
4290 (unsigned long)ac
->ac_b_ex
.fe_len
,
4291 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4292 (int)ac
->ac_criteria
);
4293 mb_debug(sb
, "%u found", ac
->ac_found
);
4294 ext4_mb_show_pa(sb
);
4297 static inline void ext4_mb_show_pa(struct super_block
*sb
)
4301 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4303 ext4_mb_show_pa(ac
->ac_sb
);
4309 * We use locality group preallocation for small size file. The size of the
4310 * file is determined by the current size or the resulting size after
4311 * allocation which ever is larger
4313 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4315 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4317 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4318 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4321 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4324 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4327 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
4328 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
4331 if ((size
== isize
) && !ext4_fs_is_busy(sbi
) &&
4332 !inode_is_open_for_write(ac
->ac_inode
)) {
4333 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
4337 if (sbi
->s_mb_group_prealloc
<= 0) {
4338 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4342 /* don't use group allocation for large files */
4343 size
= max(size
, isize
);
4344 if (size
> sbi
->s_mb_stream_request
) {
4345 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4349 BUG_ON(ac
->ac_lg
!= NULL
);
4351 * locality group prealloc space are per cpu. The reason for having
4352 * per cpu locality group is to reduce the contention between block
4353 * request from multiple CPUs.
4355 ac
->ac_lg
= raw_cpu_ptr(sbi
->s_locality_groups
);
4357 /* we're going to use group allocation */
4358 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4360 /* serialize all allocations in the group */
4361 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4364 static noinline_for_stack
int
4365 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4366 struct ext4_allocation_request
*ar
)
4368 struct super_block
*sb
= ar
->inode
->i_sb
;
4369 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4370 struct ext4_super_block
*es
= sbi
->s_es
;
4374 ext4_grpblk_t block
;
4376 /* we can't allocate > group size */
4379 /* just a dirty hack to filter too big requests */
4380 if (len
>= EXT4_CLUSTERS_PER_GROUP(sb
))
4381 len
= EXT4_CLUSTERS_PER_GROUP(sb
);
4383 /* start searching from the goal */
4385 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4386 goal
>= ext4_blocks_count(es
))
4387 goal
= le32_to_cpu(es
->s_first_data_block
);
4388 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4390 /* set up allocation goals */
4391 ac
->ac_b_ex
.fe_logical
= EXT4_LBLK_CMASK(sbi
, ar
->logical
);
4392 ac
->ac_status
= AC_STATUS_CONTINUE
;
4394 ac
->ac_inode
= ar
->inode
;
4395 ac
->ac_o_ex
.fe_logical
= ac
->ac_b_ex
.fe_logical
;
4396 ac
->ac_o_ex
.fe_group
= group
;
4397 ac
->ac_o_ex
.fe_start
= block
;
4398 ac
->ac_o_ex
.fe_len
= len
;
4399 ac
->ac_g_ex
= ac
->ac_o_ex
;
4400 ac
->ac_flags
= ar
->flags
;
4402 /* we have to define context: we'll we work with a file or
4403 * locality group. this is a policy, actually */
4404 ext4_mb_group_or_file(ac
);
4406 mb_debug(sb
, "init ac: %u blocks @ %u, goal %u, flags 0x%x, 2^%d, "
4407 "left: %u/%u, right %u/%u to %swritable\n",
4408 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4409 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4410 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4411 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4412 inode_is_open_for_write(ar
->inode
) ? "" : "non-");
4417 static noinline_for_stack
void
4418 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4419 struct ext4_locality_group
*lg
,
4420 int order
, int total_entries
)
4422 ext4_group_t group
= 0;
4423 struct ext4_buddy e4b
;
4424 struct list_head discard_list
;
4425 struct ext4_prealloc_space
*pa
, *tmp
;
4427 mb_debug(sb
, "discard locality group preallocation\n");
4429 INIT_LIST_HEAD(&discard_list
);
4431 spin_lock(&lg
->lg_prealloc_lock
);
4432 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4434 lockdep_is_held(&lg
->lg_prealloc_lock
)) {
4435 spin_lock(&pa
->pa_lock
);
4436 if (atomic_read(&pa
->pa_count
)) {
4438 * This is the pa that we just used
4439 * for block allocation. So don't
4442 spin_unlock(&pa
->pa_lock
);
4445 if (pa
->pa_deleted
) {
4446 spin_unlock(&pa
->pa_lock
);
4449 /* only lg prealloc space */
4450 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4452 /* seems this one can be freed ... */
4454 spin_unlock(&pa
->pa_lock
);
4456 list_del_rcu(&pa
->pa_inode_list
);
4457 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4460 if (total_entries
<= 5) {
4462 * we want to keep only 5 entries
4463 * allowing it to grow to 8. This
4464 * mak sure we don't call discard
4465 * soon for this list.
4470 spin_unlock(&lg
->lg_prealloc_lock
);
4472 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4475 group
= ext4_get_group_number(sb
, pa
->pa_pstart
);
4476 err
= ext4_mb_load_buddy_gfp(sb
, group
, &e4b
,
4477 GFP_NOFS
|__GFP_NOFAIL
);
4479 ext4_error_err(sb
, -err
, "Error %d loading buddy information for %u",
4483 ext4_lock_group(sb
, group
);
4484 list_del(&pa
->pa_group_list
);
4485 ext4_mb_release_group_pa(&e4b
, pa
);
4486 ext4_unlock_group(sb
, group
);
4488 ext4_mb_unload_buddy(&e4b
);
4489 list_del(&pa
->u
.pa_tmp_list
);
4490 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4495 * We have incremented pa_count. So it cannot be freed at this
4496 * point. Also we hold lg_mutex. So no parallel allocation is
4497 * possible from this lg. That means pa_free cannot be updated.
4499 * A parallel ext4_mb_discard_group_preallocations is possible.
4500 * which can cause the lg_prealloc_list to be updated.
4503 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4505 int order
, added
= 0, lg_prealloc_count
= 1;
4506 struct super_block
*sb
= ac
->ac_sb
;
4507 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4508 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4510 order
= fls(pa
->pa_free
) - 1;
4511 if (order
> PREALLOC_TB_SIZE
- 1)
4512 /* The max size of hash table is PREALLOC_TB_SIZE */
4513 order
= PREALLOC_TB_SIZE
- 1;
4514 /* Add the prealloc space to lg */
4515 spin_lock(&lg
->lg_prealloc_lock
);
4516 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4518 lockdep_is_held(&lg
->lg_prealloc_lock
)) {
4519 spin_lock(&tmp_pa
->pa_lock
);
4520 if (tmp_pa
->pa_deleted
) {
4521 spin_unlock(&tmp_pa
->pa_lock
);
4524 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4525 /* Add to the tail of the previous entry */
4526 list_add_tail_rcu(&pa
->pa_inode_list
,
4527 &tmp_pa
->pa_inode_list
);
4530 * we want to count the total
4531 * number of entries in the list
4534 spin_unlock(&tmp_pa
->pa_lock
);
4535 lg_prealloc_count
++;
4538 list_add_tail_rcu(&pa
->pa_inode_list
,
4539 &lg
->lg_prealloc_list
[order
]);
4540 spin_unlock(&lg
->lg_prealloc_lock
);
4542 /* Now trim the list to be not more than 8 elements */
4543 if (lg_prealloc_count
> 8) {
4544 ext4_mb_discard_lg_preallocations(sb
, lg
,
4545 order
, lg_prealloc_count
);
4552 * release all resource we used in allocation
4554 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4556 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4557 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4559 if (pa
->pa_type
== MB_GROUP_PA
) {
4560 /* see comment in ext4_mb_use_group_pa() */
4561 spin_lock(&pa
->pa_lock
);
4562 pa
->pa_pstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4563 pa
->pa_lstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4564 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4565 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4566 spin_unlock(&pa
->pa_lock
);
4571 * We want to add the pa to the right bucket.
4572 * Remove it from the list and while adding
4573 * make sure the list to which we are adding
4576 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4577 spin_lock(pa
->pa_obj_lock
);
4578 list_del_rcu(&pa
->pa_inode_list
);
4579 spin_unlock(pa
->pa_obj_lock
);
4580 ext4_mb_add_n_trim(ac
);
4582 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4584 if (ac
->ac_bitmap_page
)
4585 put_page(ac
->ac_bitmap_page
);
4586 if (ac
->ac_buddy_page
)
4587 put_page(ac
->ac_buddy_page
);
4588 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4589 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4590 ext4_mb_collect_stats(ac
);
4594 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4596 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4600 trace_ext4_mb_discard_preallocations(sb
, needed
);
4601 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4602 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4610 static bool ext4_mb_discard_preallocations_should_retry(struct super_block
*sb
,
4611 struct ext4_allocation_context
*ac
, u64
*seq
)
4617 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4622 seq_retry
= ext4_get_discard_pa_seq_sum();
4623 if (!(ac
->ac_flags
& EXT4_MB_STRICT_CHECK
) || seq_retry
!= *seq
) {
4624 ac
->ac_flags
|= EXT4_MB_STRICT_CHECK
;
4630 mb_debug(sb
, "freed %d, retry ? %s\n", freed
, ret
? "yes" : "no");
4635 * Main entry point into mballoc to allocate blocks
4636 * it tries to use preallocation first, then falls back
4637 * to usual allocation
4639 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4640 struct ext4_allocation_request
*ar
, int *errp
)
4642 struct ext4_allocation_context
*ac
= NULL
;
4643 struct ext4_sb_info
*sbi
;
4644 struct super_block
*sb
;
4645 ext4_fsblk_t block
= 0;
4646 unsigned int inquota
= 0;
4647 unsigned int reserv_clstrs
= 0;
4651 sb
= ar
->inode
->i_sb
;
4654 trace_ext4_request_blocks(ar
);
4656 /* Allow to use superuser reservation for quota file */
4657 if (ext4_is_quota_file(ar
->inode
))
4658 ar
->flags
|= EXT4_MB_USE_ROOT_BLOCKS
;
4660 if ((ar
->flags
& EXT4_MB_DELALLOC_RESERVED
) == 0) {
4661 /* Without delayed allocation we need to verify
4662 * there is enough free blocks to do block allocation
4663 * and verify allocation doesn't exceed the quota limits.
4666 ext4_claim_free_clusters(sbi
, ar
->len
, ar
->flags
)) {
4668 /* let others to free the space */
4670 ar
->len
= ar
->len
>> 1;
4673 ext4_mb_show_pa(sb
);
4677 reserv_clstrs
= ar
->len
;
4678 if (ar
->flags
& EXT4_MB_USE_ROOT_BLOCKS
) {
4679 dquot_alloc_block_nofail(ar
->inode
,
4680 EXT4_C2B(sbi
, ar
->len
));
4683 dquot_alloc_block(ar
->inode
,
4684 EXT4_C2B(sbi
, ar
->len
))) {
4686 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4697 ac
= kmem_cache_zalloc(ext4_ac_cachep
, GFP_NOFS
);
4704 *errp
= ext4_mb_initialize_context(ac
, ar
);
4710 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4711 seq
= this_cpu_read(discard_pa_seq
);
4712 if (!ext4_mb_use_preallocated(ac
)) {
4713 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4714 ext4_mb_normalize_request(ac
, ar
);
4716 *errp
= ext4_mb_pa_alloc(ac
);
4720 /* allocate space in core */
4721 *errp
= ext4_mb_regular_allocator(ac
);
4723 * pa allocated above is added to grp->bb_prealloc_list only
4724 * when we were able to allocate some block i.e. when
4725 * ac->ac_status == AC_STATUS_FOUND.
4726 * And error from above mean ac->ac_status != AC_STATUS_FOUND
4727 * So we have to free this pa here itself.
4730 ext4_mb_pa_free(ac
);
4731 ext4_discard_allocated_blocks(ac
);
4734 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4735 ac
->ac_o_ex
.fe_len
>= ac
->ac_f_ex
.fe_len
)
4736 ext4_mb_pa_free(ac
);
4738 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4739 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_clstrs
);
4741 ext4_discard_allocated_blocks(ac
);
4744 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4745 ar
->len
= ac
->ac_b_ex
.fe_len
;
4748 if (ext4_mb_discard_preallocations_should_retry(sb
, ac
, &seq
))
4751 * If block allocation fails then the pa allocated above
4752 * needs to be freed here itself.
4754 ext4_mb_pa_free(ac
);
4760 ac
->ac_b_ex
.fe_len
= 0;
4762 ext4_mb_show_ac(ac
);
4764 ext4_mb_release_context(ac
);
4767 kmem_cache_free(ext4_ac_cachep
, ac
);
4768 if (inquota
&& ar
->len
< inquota
)
4769 dquot_free_block(ar
->inode
, EXT4_C2B(sbi
, inquota
- ar
->len
));
4771 if ((ar
->flags
& EXT4_MB_DELALLOC_RESERVED
) == 0)
4772 /* release all the reserved blocks if non delalloc */
4773 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
4777 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4783 * We can merge two free data extents only if the physical blocks
4784 * are contiguous, AND the extents were freed by the same transaction,
4785 * AND the blocks are associated with the same group.
4787 static void ext4_try_merge_freed_extent(struct ext4_sb_info
*sbi
,
4788 struct ext4_free_data
*entry
,
4789 struct ext4_free_data
*new_entry
,
4790 struct rb_root
*entry_rb_root
)
4792 if ((entry
->efd_tid
!= new_entry
->efd_tid
) ||
4793 (entry
->efd_group
!= new_entry
->efd_group
))
4795 if (entry
->efd_start_cluster
+ entry
->efd_count
==
4796 new_entry
->efd_start_cluster
) {
4797 new_entry
->efd_start_cluster
= entry
->efd_start_cluster
;
4798 new_entry
->efd_count
+= entry
->efd_count
;
4799 } else if (new_entry
->efd_start_cluster
+ new_entry
->efd_count
==
4800 entry
->efd_start_cluster
) {
4801 new_entry
->efd_count
+= entry
->efd_count
;
4804 spin_lock(&sbi
->s_md_lock
);
4805 list_del(&entry
->efd_list
);
4806 spin_unlock(&sbi
->s_md_lock
);
4807 rb_erase(&entry
->efd_node
, entry_rb_root
);
4808 kmem_cache_free(ext4_free_data_cachep
, entry
);
4811 static noinline_for_stack
int
4812 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4813 struct ext4_free_data
*new_entry
)
4815 ext4_group_t group
= e4b
->bd_group
;
4816 ext4_grpblk_t cluster
;
4817 ext4_grpblk_t clusters
= new_entry
->efd_count
;
4818 struct ext4_free_data
*entry
;
4819 struct ext4_group_info
*db
= e4b
->bd_info
;
4820 struct super_block
*sb
= e4b
->bd_sb
;
4821 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4822 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4823 struct rb_node
*parent
= NULL
, *new_node
;
4825 BUG_ON(!ext4_handle_valid(handle
));
4826 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4827 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4829 new_node
= &new_entry
->efd_node
;
4830 cluster
= new_entry
->efd_start_cluster
;
4833 /* first free block exent. We need to
4834 protect buddy cache from being freed,
4835 * otherwise we'll refresh it from
4836 * on-disk bitmap and lose not-yet-available
4838 get_page(e4b
->bd_buddy_page
);
4839 get_page(e4b
->bd_bitmap_page
);
4843 entry
= rb_entry(parent
, struct ext4_free_data
, efd_node
);
4844 if (cluster
< entry
->efd_start_cluster
)
4846 else if (cluster
>= (entry
->efd_start_cluster
+ entry
->efd_count
))
4847 n
= &(*n
)->rb_right
;
4849 ext4_grp_locked_error(sb
, group
, 0,
4850 ext4_group_first_block_no(sb
, group
) +
4851 EXT4_C2B(sbi
, cluster
),
4852 "Block already on to-be-freed list");
4857 rb_link_node(new_node
, parent
, n
);
4858 rb_insert_color(new_node
, &db
->bb_free_root
);
4860 /* Now try to see the extent can be merged to left and right */
4861 node
= rb_prev(new_node
);
4863 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4864 ext4_try_merge_freed_extent(sbi
, entry
, new_entry
,
4865 &(db
->bb_free_root
));
4868 node
= rb_next(new_node
);
4870 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4871 ext4_try_merge_freed_extent(sbi
, entry
, new_entry
,
4872 &(db
->bb_free_root
));
4875 spin_lock(&sbi
->s_md_lock
);
4876 list_add_tail(&new_entry
->efd_list
, &sbi
->s_freed_data_list
);
4877 sbi
->s_mb_free_pending
+= clusters
;
4878 spin_unlock(&sbi
->s_md_lock
);
4883 * ext4_free_blocks() -- Free given blocks and update quota
4884 * @handle: handle for this transaction
4886 * @bh: optional buffer of the block to be freed
4887 * @block: starting physical block to be freed
4888 * @count: number of blocks to be freed
4889 * @flags: flags used by ext4_free_blocks
4891 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
4892 struct buffer_head
*bh
, ext4_fsblk_t block
,
4893 unsigned long count
, int flags
)
4895 struct buffer_head
*bitmap_bh
= NULL
;
4896 struct super_block
*sb
= inode
->i_sb
;
4897 struct ext4_group_desc
*gdp
;
4898 unsigned int overflow
;
4900 struct buffer_head
*gd_bh
;
4901 ext4_group_t block_group
;
4902 struct ext4_sb_info
*sbi
;
4903 struct ext4_buddy e4b
;
4904 unsigned int count_clusters
;
4911 BUG_ON(block
!= bh
->b_blocknr
);
4913 block
= bh
->b_blocknr
;
4917 if (!(flags
& EXT4_FREE_BLOCKS_VALIDATED
) &&
4918 !ext4_data_block_valid(sbi
, block
, count
)) {
4919 ext4_error(sb
, "Freeing blocks not in datazone - "
4920 "block = %llu, count = %lu", block
, count
);
4924 ext4_debug("freeing block %llu\n", block
);
4925 trace_ext4_free_blocks(inode
, block
, count
, flags
);
4927 if (bh
&& (flags
& EXT4_FREE_BLOCKS_FORGET
)) {
4930 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4935 * If the extent to be freed does not begin on a cluster
4936 * boundary, we need to deal with partial clusters at the
4937 * beginning and end of the extent. Normally we will free
4938 * blocks at the beginning or the end unless we are explicitly
4939 * requested to avoid doing so.
4941 overflow
= EXT4_PBLK_COFF(sbi
, block
);
4943 if (flags
& EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
) {
4944 overflow
= sbi
->s_cluster_ratio
- overflow
;
4946 if (count
> overflow
)
4955 overflow
= EXT4_LBLK_COFF(sbi
, count
);
4957 if (flags
& EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER
) {
4958 if (count
> overflow
)
4963 count
+= sbi
->s_cluster_ratio
- overflow
;
4966 if (!bh
&& (flags
& EXT4_FREE_BLOCKS_FORGET
)) {
4968 int is_metadata
= flags
& EXT4_FREE_BLOCKS_METADATA
;
4970 for (i
= 0; i
< count
; i
++) {
4973 bh
= sb_find_get_block(inode
->i_sb
, block
+ i
);
4974 ext4_forget(handle
, is_metadata
, inode
, bh
, block
+ i
);
4980 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4982 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4983 ext4_get_group_info(sb
, block_group
))))
4987 * Check to see if we are freeing blocks across a group
4990 if (EXT4_C2B(sbi
, bit
) + count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4991 overflow
= EXT4_C2B(sbi
, bit
) + count
-
4992 EXT4_BLOCKS_PER_GROUP(sb
);
4995 count_clusters
= EXT4_NUM_B2C(sbi
, count
);
4996 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4997 if (IS_ERR(bitmap_bh
)) {
4998 err
= PTR_ERR(bitmap_bh
);
5002 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
5008 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
5009 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
5010 in_range(block
, ext4_inode_table(sb
, gdp
),
5011 sbi
->s_itb_per_group
) ||
5012 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
5013 sbi
->s_itb_per_group
)) {
5015 ext4_error(sb
, "Freeing blocks in system zone - "
5016 "Block = %llu, count = %lu", block
, count
);
5017 /* err = 0. ext4_std_error should be a no op */
5021 BUFFER_TRACE(bitmap_bh
, "getting write access");
5022 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
5027 * We are about to modify some metadata. Call the journal APIs
5028 * to unshare ->b_data if a currently-committing transaction is
5031 BUFFER_TRACE(gd_bh
, "get_write_access");
5032 err
= ext4_journal_get_write_access(handle
, gd_bh
);
5035 #ifdef AGGRESSIVE_CHECK
5038 for (i
= 0; i
< count_clusters
; i
++)
5039 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
5042 trace_ext4_mballoc_free(sb
, inode
, block_group
, bit
, count_clusters
);
5044 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
5045 err
= ext4_mb_load_buddy_gfp(sb
, block_group
, &e4b
,
5046 GFP_NOFS
|__GFP_NOFAIL
);
5051 * We need to make sure we don't reuse the freed block until after the
5052 * transaction is committed. We make an exception if the inode is to be
5053 * written in writeback mode since writeback mode has weak data
5054 * consistency guarantees.
5056 if (ext4_handle_valid(handle
) &&
5057 ((flags
& EXT4_FREE_BLOCKS_METADATA
) ||
5058 !ext4_should_writeback_data(inode
))) {
5059 struct ext4_free_data
*new_entry
;
5061 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
5064 new_entry
= kmem_cache_alloc(ext4_free_data_cachep
,
5065 GFP_NOFS
|__GFP_NOFAIL
);
5066 new_entry
->efd_start_cluster
= bit
;
5067 new_entry
->efd_group
= block_group
;
5068 new_entry
->efd_count
= count_clusters
;
5069 new_entry
->efd_tid
= handle
->h_transaction
->t_tid
;
5071 ext4_lock_group(sb
, block_group
);
5072 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
5073 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
5075 /* need to update group_info->bb_free and bitmap
5076 * with group lock held. generate_buddy look at
5077 * them with group lock_held
5079 if (test_opt(sb
, DISCARD
)) {
5080 err
= ext4_issue_discard(sb
, block_group
, bit
, count
,
5082 if (err
&& err
!= -EOPNOTSUPP
)
5083 ext4_msg(sb
, KERN_WARNING
, "discard request in"
5084 " group:%d block:%d count:%lu failed"
5085 " with %d", block_group
, bit
, count
,
5088 EXT4_MB_GRP_CLEAR_TRIMMED(e4b
.bd_info
);
5090 ext4_lock_group(sb
, block_group
);
5091 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
5092 mb_free_blocks(inode
, &e4b
, bit
, count_clusters
);
5095 ret
= ext4_free_group_clusters(sb
, gdp
) + count_clusters
;
5096 ext4_free_group_clusters_set(sb
, gdp
, ret
);
5097 ext4_block_bitmap_csum_set(sb
, block_group
, gdp
, bitmap_bh
);
5098 ext4_group_desc_csum_set(sb
, block_group
, gdp
);
5099 ext4_unlock_group(sb
, block_group
);
5101 if (sbi
->s_log_groups_per_flex
) {
5102 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
5103 atomic64_add(count_clusters
,
5104 &sbi_array_rcu_deref(sbi
, s_flex_groups
,
5105 flex_group
)->free_clusters
);
5109 * on a bigalloc file system, defer the s_freeclusters_counter
5110 * update to the caller (ext4_remove_space and friends) so they
5111 * can determine if a cluster freed here should be rereserved
5113 if (!(flags
& EXT4_FREE_BLOCKS_RERESERVE_CLUSTER
)) {
5114 if (!(flags
& EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
))
5115 dquot_free_block(inode
, EXT4_C2B(sbi
, count_clusters
));
5116 percpu_counter_add(&sbi
->s_freeclusters_counter
,
5120 ext4_mb_unload_buddy(&e4b
);
5122 /* We dirtied the bitmap block */
5123 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
5124 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
5126 /* And the group descriptor block */
5127 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
5128 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
5132 if (overflow
&& !err
) {
5140 ext4_std_error(sb
, err
);
5145 * ext4_group_add_blocks() -- Add given blocks to an existing group
5146 * @handle: handle to this transaction
5148 * @block: start physical block to add to the block group
5149 * @count: number of blocks to free
5151 * This marks the blocks as free in the bitmap and buddy.
5153 int ext4_group_add_blocks(handle_t
*handle
, struct super_block
*sb
,
5154 ext4_fsblk_t block
, unsigned long count
)
5156 struct buffer_head
*bitmap_bh
= NULL
;
5157 struct buffer_head
*gd_bh
;
5158 ext4_group_t block_group
;
5161 struct ext4_group_desc
*desc
;
5162 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
5163 struct ext4_buddy e4b
;
5164 int err
= 0, ret
, free_clusters_count
;
5165 ext4_grpblk_t clusters_freed
;
5166 ext4_fsblk_t first_cluster
= EXT4_B2C(sbi
, block
);
5167 ext4_fsblk_t last_cluster
= EXT4_B2C(sbi
, block
+ count
- 1);
5168 unsigned long cluster_count
= last_cluster
- first_cluster
+ 1;
5170 ext4_debug("Adding block(s) %llu-%llu\n", block
, block
+ count
- 1);
5175 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
5177 * Check to see if we are freeing blocks across a group
5180 if (bit
+ cluster_count
> EXT4_CLUSTERS_PER_GROUP(sb
)) {
5181 ext4_warning(sb
, "too many blocks added to group %u",
5187 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
5188 if (IS_ERR(bitmap_bh
)) {
5189 err
= PTR_ERR(bitmap_bh
);
5194 desc
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
5200 if (in_range(ext4_block_bitmap(sb
, desc
), block
, count
) ||
5201 in_range(ext4_inode_bitmap(sb
, desc
), block
, count
) ||
5202 in_range(block
, ext4_inode_table(sb
, desc
), sbi
->s_itb_per_group
) ||
5203 in_range(block
+ count
- 1, ext4_inode_table(sb
, desc
),
5204 sbi
->s_itb_per_group
)) {
5205 ext4_error(sb
, "Adding blocks in system zones - "
5206 "Block = %llu, count = %lu",
5212 BUFFER_TRACE(bitmap_bh
, "getting write access");
5213 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
5218 * We are about to modify some metadata. Call the journal APIs
5219 * to unshare ->b_data if a currently-committing transaction is
5222 BUFFER_TRACE(gd_bh
, "get_write_access");
5223 err
= ext4_journal_get_write_access(handle
, gd_bh
);
5227 for (i
= 0, clusters_freed
= 0; i
< cluster_count
; i
++) {
5228 BUFFER_TRACE(bitmap_bh
, "clear bit");
5229 if (!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
)) {
5230 ext4_error(sb
, "bit already cleared for block %llu",
5231 (ext4_fsblk_t
)(block
+ i
));
5232 BUFFER_TRACE(bitmap_bh
, "bit already cleared");
5238 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
5243 * need to update group_info->bb_free and bitmap
5244 * with group lock held. generate_buddy look at
5245 * them with group lock_held
5247 ext4_lock_group(sb
, block_group
);
5248 mb_clear_bits(bitmap_bh
->b_data
, bit
, cluster_count
);
5249 mb_free_blocks(NULL
, &e4b
, bit
, cluster_count
);
5250 free_clusters_count
= clusters_freed
+
5251 ext4_free_group_clusters(sb
, desc
);
5252 ext4_free_group_clusters_set(sb
, desc
, free_clusters_count
);
5253 ext4_block_bitmap_csum_set(sb
, block_group
, desc
, bitmap_bh
);
5254 ext4_group_desc_csum_set(sb
, block_group
, desc
);
5255 ext4_unlock_group(sb
, block_group
);
5256 percpu_counter_add(&sbi
->s_freeclusters_counter
,
5259 if (sbi
->s_log_groups_per_flex
) {
5260 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
5261 atomic64_add(clusters_freed
,
5262 &sbi_array_rcu_deref(sbi
, s_flex_groups
,
5263 flex_group
)->free_clusters
);
5266 ext4_mb_unload_buddy(&e4b
);
5268 /* We dirtied the bitmap block */
5269 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
5270 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
5272 /* And the group descriptor block */
5273 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
5274 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
5280 ext4_std_error(sb
, err
);
5285 * ext4_trim_extent -- function to TRIM one single free extent in the group
5286 * @sb: super block for the file system
5287 * @start: starting block of the free extent in the alloc. group
5288 * @count: number of blocks to TRIM
5289 * @group: alloc. group we are working with
5290 * @e4b: ext4 buddy for the group
5292 * Trim "count" blocks starting at "start" in the "group". To assure that no
5293 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5294 * be called with under the group lock.
5296 static int ext4_trim_extent(struct super_block
*sb
, int start
, int count
,
5297 ext4_group_t group
, struct ext4_buddy
*e4b
)
5301 struct ext4_free_extent ex
;
5304 trace_ext4_trim_extent(sb
, group
, start
, count
);
5306 assert_spin_locked(ext4_group_lock_ptr(sb
, group
));
5308 ex
.fe_start
= start
;
5309 ex
.fe_group
= group
;
5313 * Mark blocks used, so no one can reuse them while
5316 mb_mark_used(e4b
, &ex
);
5317 ext4_unlock_group(sb
, group
);
5318 ret
= ext4_issue_discard(sb
, group
, start
, count
, NULL
);
5319 ext4_lock_group(sb
, group
);
5320 mb_free_blocks(NULL
, e4b
, start
, ex
.fe_len
);
5325 * ext4_trim_all_free -- function to trim all free space in alloc. group
5326 * @sb: super block for file system
5327 * @group: group to be trimmed
5328 * @start: first group block to examine
5329 * @max: last group block to examine
5330 * @minblocks: minimum extent block count
5332 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5333 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5337 * ext4_trim_all_free walks through group's block bitmap searching for free
5338 * extents. When the free extent is found, mark it as used in group buddy
5339 * bitmap. Then issue a TRIM command on this extent and free the extent in
5340 * the group buddy bitmap. This is done until whole group is scanned.
5342 static ext4_grpblk_t
5343 ext4_trim_all_free(struct super_block
*sb
, ext4_group_t group
,
5344 ext4_grpblk_t start
, ext4_grpblk_t max
,
5345 ext4_grpblk_t minblocks
)
5348 ext4_grpblk_t next
, count
= 0, free_count
= 0;
5349 struct ext4_buddy e4b
;
5352 trace_ext4_trim_all_free(sb
, group
, start
, max
);
5354 ret
= ext4_mb_load_buddy(sb
, group
, &e4b
);
5356 ext4_warning(sb
, "Error %d loading buddy information for %u",
5360 bitmap
= e4b
.bd_bitmap
;
5362 ext4_lock_group(sb
, group
);
5363 if (EXT4_MB_GRP_WAS_TRIMMED(e4b
.bd_info
) &&
5364 minblocks
>= atomic_read(&EXT4_SB(sb
)->s_last_trim_minblks
))
5367 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
5368 e4b
.bd_info
->bb_first_free
: start
;
5370 while (start
<= max
) {
5371 start
= mb_find_next_zero_bit(bitmap
, max
+ 1, start
);
5374 next
= mb_find_next_bit(bitmap
, max
+ 1, start
);
5376 if ((next
- start
) >= minblocks
) {
5377 ret
= ext4_trim_extent(sb
, start
,
5378 next
- start
, group
, &e4b
);
5379 if (ret
&& ret
!= -EOPNOTSUPP
)
5382 count
+= next
- start
;
5384 free_count
+= next
- start
;
5387 if (fatal_signal_pending(current
)) {
5388 count
= -ERESTARTSYS
;
5392 if (need_resched()) {
5393 ext4_unlock_group(sb
, group
);
5395 ext4_lock_group(sb
, group
);
5398 if ((e4b
.bd_info
->bb_free
- free_count
) < minblocks
)
5404 EXT4_MB_GRP_SET_TRIMMED(e4b
.bd_info
);
5407 ext4_unlock_group(sb
, group
);
5408 ext4_mb_unload_buddy(&e4b
);
5410 ext4_debug("trimmed %d blocks in the group %d\n",
5417 * ext4_trim_fs() -- trim ioctl handle function
5418 * @sb: superblock for filesystem
5419 * @range: fstrim_range structure
5421 * start: First Byte to trim
5422 * len: number of Bytes to trim from start
5423 * minlen: minimum extent length in Bytes
5424 * ext4_trim_fs goes through all allocation groups containing Bytes from
5425 * start to start+len. For each such a group ext4_trim_all_free function
5426 * is invoked to trim all free space.
5428 int ext4_trim_fs(struct super_block
*sb
, struct fstrim_range
*range
)
5430 struct ext4_group_info
*grp
;
5431 ext4_group_t group
, first_group
, last_group
;
5432 ext4_grpblk_t cnt
= 0, first_cluster
, last_cluster
;
5433 uint64_t start
, end
, minlen
, trimmed
= 0;
5434 ext4_fsblk_t first_data_blk
=
5435 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
5436 ext4_fsblk_t max_blks
= ext4_blocks_count(EXT4_SB(sb
)->s_es
);
5439 start
= range
->start
>> sb
->s_blocksize_bits
;
5440 end
= start
+ (range
->len
>> sb
->s_blocksize_bits
) - 1;
5441 minlen
= EXT4_NUM_B2C(EXT4_SB(sb
),
5442 range
->minlen
>> sb
->s_blocksize_bits
);
5444 if (minlen
> EXT4_CLUSTERS_PER_GROUP(sb
) ||
5445 start
>= max_blks
||
5446 range
->len
< sb
->s_blocksize
)
5448 if (end
>= max_blks
)
5450 if (end
<= first_data_blk
)
5452 if (start
< first_data_blk
)
5453 start
= first_data_blk
;
5455 /* Determine first and last group to examine based on start and end */
5456 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) start
,
5457 &first_group
, &first_cluster
);
5458 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) end
,
5459 &last_group
, &last_cluster
);
5461 /* end now represents the last cluster to discard in this group */
5462 end
= EXT4_CLUSTERS_PER_GROUP(sb
) - 1;
5464 for (group
= first_group
; group
<= last_group
; group
++) {
5465 grp
= ext4_get_group_info(sb
, group
);
5466 /* We only do this if the grp has never been initialized */
5467 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
5468 ret
= ext4_mb_init_group(sb
, group
, GFP_NOFS
);
5474 * For all the groups except the last one, last cluster will
5475 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5476 * change it for the last group, note that last_cluster is
5477 * already computed earlier by ext4_get_group_no_and_offset()
5479 if (group
== last_group
)
5482 if (grp
->bb_free
>= minlen
) {
5483 cnt
= ext4_trim_all_free(sb
, group
, first_cluster
,
5493 * For every group except the first one, we are sure
5494 * that the first cluster to discard will be cluster #0.
5500 atomic_set(&EXT4_SB(sb
)->s_last_trim_minblks
, minlen
);
5503 range
->len
= EXT4_C2B(EXT4_SB(sb
), trimmed
) << sb
->s_blocksize_bits
;
5507 /* Iterate all the free extents in the group. */
5509 ext4_mballoc_query_range(
5510 struct super_block
*sb
,
5512 ext4_grpblk_t start
,
5514 ext4_mballoc_query_range_fn formatter
,
5519 struct ext4_buddy e4b
;
5522 error
= ext4_mb_load_buddy(sb
, group
, &e4b
);
5525 bitmap
= e4b
.bd_bitmap
;
5527 ext4_lock_group(sb
, group
);
5529 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
5530 e4b
.bd_info
->bb_first_free
: start
;
5531 if (end
>= EXT4_CLUSTERS_PER_GROUP(sb
))
5532 end
= EXT4_CLUSTERS_PER_GROUP(sb
) - 1;
5534 while (start
<= end
) {
5535 start
= mb_find_next_zero_bit(bitmap
, end
+ 1, start
);
5538 next
= mb_find_next_bit(bitmap
, end
+ 1, start
);
5540 ext4_unlock_group(sb
, group
);
5541 error
= formatter(sb
, group
, start
, next
- start
, priv
);
5544 ext4_lock_group(sb
, group
);
5549 ext4_unlock_group(sb
, group
);
5551 ext4_mb_unload_buddy(&e4b
);