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/backing-dev.h>
18 #include <trace/events/ext4.h>
20 #ifdef CONFIG_EXT4_DEBUG
21 ushort ext4_mballoc_debug __read_mostly
;
23 module_param_named(mballoc_debug
, ext4_mballoc_debug
, ushort
, 0644);
24 MODULE_PARM_DESC(mballoc_debug
, "Debugging level for ext4's mballoc");
29 * - test ext4_ext_search_left() and ext4_ext_search_right()
30 * - search for metadata in few groups
33 * - normalization should take into account whether file is still open
34 * - discard preallocations if no free space left (policy?)
35 * - don't normalize tails
37 * - reservation for superuser
40 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
41 * - track min/max extents in each group for better group selection
42 * - mb_mark_used() may allocate chunk right after splitting buddy
43 * - tree of groups sorted by number of free blocks
48 * The allocation request involve request for multiple number of blocks
49 * near to the goal(block) value specified.
51 * During initialization phase of the allocator we decide to use the
52 * group preallocation or inode preallocation depending on the size of
53 * the file. The size of the file could be the resulting file size we
54 * would have after allocation, or the current file size, which ever
55 * is larger. If the size is less than sbi->s_mb_stream_request we
56 * select to use the group preallocation. The default value of
57 * s_mb_stream_request is 16 blocks. This can also be tuned via
58 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
59 * terms of number of blocks.
61 * The main motivation for having small file use group preallocation is to
62 * ensure that we have small files closer together on the disk.
64 * First stage the allocator looks at the inode prealloc list,
65 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
66 * spaces for this particular inode. The inode prealloc space is
69 * pa_lstart -> the logical start block for this prealloc space
70 * pa_pstart -> the physical start block for this prealloc space
71 * pa_len -> length for this prealloc space (in clusters)
72 * pa_free -> free space available in this prealloc space (in clusters)
74 * The inode preallocation space is used looking at the _logical_ start
75 * block. If only the logical file block falls within the range of prealloc
76 * space we will consume the particular prealloc space. This makes sure that
77 * we have contiguous physical blocks representing the file blocks
79 * The important thing to be noted in case of inode prealloc space is that
80 * we don't modify the values associated to inode prealloc space except
83 * If we are not able to find blocks in the inode prealloc space and if we
84 * have the group allocation flag set then we look at the locality group
85 * prealloc space. These are per CPU prealloc list represented as
87 * ext4_sb_info.s_locality_groups[smp_processor_id()]
89 * The reason for having a per cpu locality group is to reduce the contention
90 * between CPUs. It is possible to get scheduled at this point.
92 * The locality group prealloc space is used looking at whether we have
93 * enough free space (pa_free) within the prealloc space.
95 * If we can't allocate blocks via inode prealloc or/and locality group
96 * prealloc then we look at the buddy cache. The buddy cache is represented
97 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
98 * mapped to the buddy and bitmap information regarding different
99 * groups. The buddy information is attached to buddy cache inode so that
100 * we can access them through the page cache. The information regarding
101 * each group is loaded via ext4_mb_load_buddy. The information involve
102 * block bitmap and buddy information. The information are stored in the
106 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
109 * one block each for bitmap and buddy information. So for each group we
110 * take up 2 blocks. A page can contain blocks_per_page (PAGE_SIZE /
111 * blocksize) blocks. So it can have information regarding groups_per_page
112 * which is blocks_per_page/2
114 * The buddy cache inode is not stored on disk. The inode is thrown
115 * away when the filesystem is unmounted.
117 * We look for count number of blocks in the buddy cache. If we were able
118 * to locate that many free blocks we return with additional information
119 * regarding rest of the contiguous physical block available
121 * Before allocating blocks via buddy cache we normalize the request
122 * blocks. This ensure we ask for more blocks that we needed. The extra
123 * blocks that we get after allocation is added to the respective prealloc
124 * list. In case of inode preallocation we follow a list of heuristics
125 * based on file size. This can be found in ext4_mb_normalize_request. If
126 * we are doing a group prealloc we try to normalize the request to
127 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
128 * dependent on the cluster size; for non-bigalloc file systems, it is
129 * 512 blocks. This can be tuned via
130 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
131 * terms of number of blocks. If we have mounted the file system with -O
132 * stripe=<value> option the group prealloc request is normalized to the
133 * the smallest multiple of the stripe value (sbi->s_stripe) which is
134 * greater than the default mb_group_prealloc.
136 * The regular allocator (using the buddy cache) supports a few tunables.
138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
142 * The regular allocator uses buddy scan only if the request len is power of
143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
150 * min_to_scan indicate how long the mballoc __must__ look for a best
151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
155 * can be used for allocation. ext4_mb_good_group explains how the groups are
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
165 * mballoc operates on the following data:
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
170 * there are two types of preallocations:
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
223 * so, now we're building a concurrency table:
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
239 * i_data_sem serializes them
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
248 * i_data_sem or another mutex should serializes them
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
270 * Logic in few words:
275 * mark bits in on-disk bitmap
278 * - use preallocation:
279 * find proper PA (per-inode or group)
281 * mark bits in on-disk bitmap
287 * mark bits in on-disk bitmap
290 * - discard preallocations in group:
292 * move them onto local list
293 * load on-disk bitmap
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
298 * - discard inode's preallocations:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
317 * - release consumed pa:
322 * - generate in-core bitmap:
326 * - discard all for given object (inode, locality group):
331 * - discard all for given group:
338 static struct kmem_cache
*ext4_pspace_cachep
;
339 static struct kmem_cache
*ext4_ac_cachep
;
340 static struct kmem_cache
*ext4_free_data_cachep
;
342 /* We create slab caches for groupinfo data structures based on the
343 * superblock block size. There will be one per mounted filesystem for
344 * each unique s_blocksize_bits */
345 #define NR_GRPINFO_CACHES 8
346 static struct kmem_cache
*ext4_groupinfo_caches
[NR_GRPINFO_CACHES
];
348 static const char * const ext4_groupinfo_slab_names
[NR_GRPINFO_CACHES
] = {
349 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
350 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
351 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
354 static void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
356 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
359 static inline void *mb_correct_addr_and_bit(int *bit
, void *addr
)
361 #if BITS_PER_LONG == 64
362 *bit
+= ((unsigned long) addr
& 7UL) << 3;
363 addr
= (void *) ((unsigned long) addr
& ~7UL);
364 #elif BITS_PER_LONG == 32
365 *bit
+= ((unsigned long) addr
& 3UL) << 3;
366 addr
= (void *) ((unsigned long) addr
& ~3UL);
368 #error "how many bits you are?!"
373 static inline int mb_test_bit(int bit
, void *addr
)
376 * ext4_test_bit on architecture like powerpc
377 * needs unsigned long aligned address
379 addr
= mb_correct_addr_and_bit(&bit
, addr
);
380 return ext4_test_bit(bit
, addr
);
383 static inline void mb_set_bit(int bit
, void *addr
)
385 addr
= mb_correct_addr_and_bit(&bit
, addr
);
386 ext4_set_bit(bit
, addr
);
389 static inline void mb_clear_bit(int bit
, void *addr
)
391 addr
= mb_correct_addr_and_bit(&bit
, addr
);
392 ext4_clear_bit(bit
, addr
);
395 static inline int mb_test_and_clear_bit(int bit
, void *addr
)
397 addr
= mb_correct_addr_and_bit(&bit
, addr
);
398 return ext4_test_and_clear_bit(bit
, addr
);
401 static inline int mb_find_next_zero_bit(void *addr
, int max
, int start
)
403 int fix
= 0, ret
, tmpmax
;
404 addr
= mb_correct_addr_and_bit(&fix
, addr
);
408 ret
= ext4_find_next_zero_bit(addr
, tmpmax
, start
) - fix
;
414 static inline int mb_find_next_bit(void *addr
, int max
, int start
)
416 int fix
= 0, ret
, tmpmax
;
417 addr
= mb_correct_addr_and_bit(&fix
, addr
);
421 ret
= ext4_find_next_bit(addr
, tmpmax
, start
) - fix
;
427 static void *mb_find_buddy(struct ext4_buddy
*e4b
, int order
, int *max
)
431 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
434 if (order
> e4b
->bd_blkbits
+ 1) {
439 /* at order 0 we see each particular block */
441 *max
= 1 << (e4b
->bd_blkbits
+ 3);
442 return e4b
->bd_bitmap
;
445 bb
= e4b
->bd_buddy
+ EXT4_SB(e4b
->bd_sb
)->s_mb_offsets
[order
];
446 *max
= EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[order
];
452 static void mb_free_blocks_double(struct inode
*inode
, struct ext4_buddy
*e4b
,
453 int first
, int count
)
456 struct super_block
*sb
= e4b
->bd_sb
;
458 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
460 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
461 for (i
= 0; i
< count
; i
++) {
462 if (!mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
)) {
463 ext4_fsblk_t blocknr
;
465 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
466 blocknr
+= EXT4_C2B(EXT4_SB(sb
), first
+ i
);
467 ext4_grp_locked_error(sb
, e4b
->bd_group
,
468 inode
? inode
->i_ino
: 0,
470 "freeing block already freed "
473 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
474 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
476 mb_clear_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
480 static void mb_mark_used_double(struct ext4_buddy
*e4b
, int first
, int count
)
484 if (unlikely(e4b
->bd_info
->bb_bitmap
== NULL
))
486 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
487 for (i
= 0; i
< count
; i
++) {
488 BUG_ON(mb_test_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
));
489 mb_set_bit(first
+ i
, e4b
->bd_info
->bb_bitmap
);
493 static void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
495 if (memcmp(e4b
->bd_info
->bb_bitmap
, bitmap
, e4b
->bd_sb
->s_blocksize
)) {
496 unsigned char *b1
, *b2
;
498 b1
= (unsigned char *) e4b
->bd_info
->bb_bitmap
;
499 b2
= (unsigned char *) bitmap
;
500 for (i
= 0; i
< e4b
->bd_sb
->s_blocksize
; i
++) {
501 if (b1
[i
] != b2
[i
]) {
502 ext4_msg(e4b
->bd_sb
, KERN_ERR
,
503 "corruption in group %u "
504 "at byte %u(%u): %x in copy != %x "
506 e4b
->bd_group
, i
, i
* 8, b1
[i
], b2
[i
]);
514 static inline void mb_free_blocks_double(struct inode
*inode
,
515 struct ext4_buddy
*e4b
, int first
, int count
)
519 static inline void mb_mark_used_double(struct ext4_buddy
*e4b
,
520 int first
, int count
)
524 static inline void mb_cmp_bitmaps(struct ext4_buddy
*e4b
, void *bitmap
)
530 #ifdef AGGRESSIVE_CHECK
532 #define MB_CHECK_ASSERT(assert) \
536 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
537 function, file, line, # assert); \
542 static int __mb_check_buddy(struct ext4_buddy
*e4b
, char *file
,
543 const char *function
, int line
)
545 struct super_block
*sb
= e4b
->bd_sb
;
546 int order
= e4b
->bd_blkbits
+ 1;
553 struct ext4_group_info
*grp
;
556 struct list_head
*cur
;
561 static int mb_check_counter
;
562 if (mb_check_counter
++ % 100 != 0)
567 buddy
= mb_find_buddy(e4b
, order
, &max
);
568 MB_CHECK_ASSERT(buddy
);
569 buddy2
= mb_find_buddy(e4b
, order
- 1, &max2
);
570 MB_CHECK_ASSERT(buddy2
);
571 MB_CHECK_ASSERT(buddy
!= buddy2
);
572 MB_CHECK_ASSERT(max
* 2 == max2
);
575 for (i
= 0; i
< max
; i
++) {
577 if (mb_test_bit(i
, buddy
)) {
578 /* only single bit in buddy2 may be 1 */
579 if (!mb_test_bit(i
<< 1, buddy2
)) {
581 mb_test_bit((i
<<1)+1, buddy2
));
582 } else if (!mb_test_bit((i
<< 1) + 1, buddy2
)) {
584 mb_test_bit(i
<< 1, buddy2
));
589 /* both bits in buddy2 must be 1 */
590 MB_CHECK_ASSERT(mb_test_bit(i
<< 1, buddy2
));
591 MB_CHECK_ASSERT(mb_test_bit((i
<< 1) + 1, buddy2
));
593 for (j
= 0; j
< (1 << order
); j
++) {
594 k
= (i
* (1 << order
)) + j
;
596 !mb_test_bit(k
, e4b
->bd_bitmap
));
600 MB_CHECK_ASSERT(e4b
->bd_info
->bb_counters
[order
] == count
);
605 buddy
= mb_find_buddy(e4b
, 0, &max
);
606 for (i
= 0; i
< max
; i
++) {
607 if (!mb_test_bit(i
, buddy
)) {
608 MB_CHECK_ASSERT(i
>= e4b
->bd_info
->bb_first_free
);
616 /* check used bits only */
617 for (j
= 0; j
< e4b
->bd_blkbits
+ 1; j
++) {
618 buddy2
= mb_find_buddy(e4b
, j
, &max2
);
620 MB_CHECK_ASSERT(k
< max2
);
621 MB_CHECK_ASSERT(mb_test_bit(k
, buddy2
));
624 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b
->bd_info
));
625 MB_CHECK_ASSERT(e4b
->bd_info
->bb_fragments
== fragments
);
627 grp
= ext4_get_group_info(sb
, e4b
->bd_group
);
628 list_for_each(cur
, &grp
->bb_prealloc_list
) {
629 ext4_group_t groupnr
;
630 struct ext4_prealloc_space
*pa
;
631 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
632 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &groupnr
, &k
);
633 MB_CHECK_ASSERT(groupnr
== e4b
->bd_group
);
634 for (i
= 0; i
< pa
->pa_len
; i
++)
635 MB_CHECK_ASSERT(mb_test_bit(k
+ i
, buddy
));
639 #undef MB_CHECK_ASSERT
640 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
641 __FILE__, __func__, __LINE__)
643 #define mb_check_buddy(e4b)
647 * Divide blocks started from @first with length @len into
648 * smaller chunks with power of 2 blocks.
649 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
650 * then increase bb_counters[] for corresponded chunk size.
652 static void ext4_mb_mark_free_simple(struct super_block
*sb
,
653 void *buddy
, ext4_grpblk_t first
, ext4_grpblk_t len
,
654 struct ext4_group_info
*grp
)
656 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
662 BUG_ON(len
> EXT4_CLUSTERS_PER_GROUP(sb
));
664 border
= 2 << sb
->s_blocksize_bits
;
667 /* find how many blocks can be covered since this position */
668 max
= ffs(first
| border
) - 1;
670 /* find how many blocks of power 2 we need to mark */
677 /* mark multiblock chunks only */
678 grp
->bb_counters
[min
]++;
680 mb_clear_bit(first
>> min
,
681 buddy
+ sbi
->s_mb_offsets
[min
]);
689 * Cache the order of the largest free extent we have available in this block
693 mb_set_largest_free_order(struct super_block
*sb
, struct ext4_group_info
*grp
)
698 grp
->bb_largest_free_order
= -1; /* uninit */
700 bits
= sb
->s_blocksize_bits
+ 1;
701 for (i
= bits
; i
>= 0; i
--) {
702 if (grp
->bb_counters
[i
] > 0) {
703 grp
->bb_largest_free_order
= i
;
709 static noinline_for_stack
710 void ext4_mb_generate_buddy(struct super_block
*sb
,
711 void *buddy
, void *bitmap
, ext4_group_t group
)
713 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
714 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
715 ext4_grpblk_t max
= EXT4_CLUSTERS_PER_GROUP(sb
);
720 unsigned fragments
= 0;
721 unsigned long long period
= get_cycles();
723 /* initialize buddy from bitmap which is aggregation
724 * of on-disk bitmap and preallocations */
725 i
= mb_find_next_zero_bit(bitmap
, max
, 0);
726 grp
->bb_first_free
= i
;
730 i
= mb_find_next_bit(bitmap
, max
, i
);
734 ext4_mb_mark_free_simple(sb
, buddy
, first
, len
, grp
);
736 grp
->bb_counters
[0]++;
738 i
= mb_find_next_zero_bit(bitmap
, max
, i
);
740 grp
->bb_fragments
= fragments
;
742 if (free
!= grp
->bb_free
) {
743 ext4_grp_locked_error(sb
, group
, 0, 0,
744 "block bitmap and bg descriptor "
745 "inconsistent: %u vs %u free clusters",
748 * If we intend to continue, we consider group descriptor
749 * corrupt and update bb_free using bitmap value
752 ext4_mark_group_bitmap_corrupted(sb
, group
,
753 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
755 mb_set_largest_free_order(sb
, grp
);
757 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
, &(grp
->bb_state
));
759 period
= get_cycles() - period
;
760 spin_lock(&sbi
->s_bal_lock
);
761 sbi
->s_mb_buddies_generated
++;
762 sbi
->s_mb_generation_time
+= period
;
763 spin_unlock(&sbi
->s_bal_lock
);
766 static void mb_regenerate_buddy(struct ext4_buddy
*e4b
)
772 while ((buddy
= mb_find_buddy(e4b
, order
++, &count
))) {
773 ext4_set_bits(buddy
, 0, count
);
775 e4b
->bd_info
->bb_fragments
= 0;
776 memset(e4b
->bd_info
->bb_counters
, 0,
777 sizeof(*e4b
->bd_info
->bb_counters
) *
778 (e4b
->bd_sb
->s_blocksize_bits
+ 2));
780 ext4_mb_generate_buddy(e4b
->bd_sb
, e4b
->bd_buddy
,
781 e4b
->bd_bitmap
, e4b
->bd_group
);
784 /* The buddy information is attached the buddy cache inode
785 * for convenience. The information regarding each group
786 * is loaded via ext4_mb_load_buddy. The information involve
787 * block bitmap and buddy information. The information are
788 * stored in the inode as
791 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
794 * one block each for bitmap and buddy information.
795 * So for each group we take up 2 blocks. A page can
796 * contain blocks_per_page (PAGE_SIZE / blocksize) blocks.
797 * So it can have information regarding groups_per_page which
798 * is blocks_per_page/2
800 * Locking note: This routine takes the block group lock of all groups
801 * for this page; do not hold this lock when calling this routine!
804 static int ext4_mb_init_cache(struct page
*page
, char *incore
, gfp_t gfp
)
806 ext4_group_t ngroups
;
812 ext4_group_t first_group
, group
;
814 struct super_block
*sb
;
815 struct buffer_head
*bhs
;
816 struct buffer_head
**bh
= NULL
;
820 struct ext4_group_info
*grinfo
;
822 mb_debug(1, "init page %lu\n", page
->index
);
824 inode
= page
->mapping
->host
;
826 ngroups
= ext4_get_groups_count(sb
);
827 blocksize
= i_blocksize(inode
);
828 blocks_per_page
= PAGE_SIZE
/ blocksize
;
830 groups_per_page
= blocks_per_page
>> 1;
831 if (groups_per_page
== 0)
834 /* allocate buffer_heads to read bitmaps */
835 if (groups_per_page
> 1) {
836 i
= sizeof(struct buffer_head
*) * groups_per_page
;
837 bh
= kzalloc(i
, gfp
);
845 first_group
= page
->index
* blocks_per_page
/ 2;
847 /* read all groups the page covers into the cache */
848 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
849 if (group
>= ngroups
)
852 grinfo
= ext4_get_group_info(sb
, group
);
854 * If page is uptodate then we came here after online resize
855 * which added some new uninitialized group info structs, so
856 * we must skip all initialized uptodate buddies on the page,
857 * which may be currently in use by an allocating task.
859 if (PageUptodate(page
) && !EXT4_MB_GRP_NEED_INIT(grinfo
)) {
863 bh
[i
] = ext4_read_block_bitmap_nowait(sb
, group
);
865 err
= PTR_ERR(bh
[i
]);
869 mb_debug(1, "read bitmap for group %u\n", group
);
872 /* wait for I/O completion */
873 for (i
= 0, group
= first_group
; i
< groups_per_page
; i
++, group
++) {
878 err2
= ext4_wait_block_bitmap(sb
, group
, bh
[i
]);
883 first_block
= page
->index
* blocks_per_page
;
884 for (i
= 0; i
< blocks_per_page
; i
++) {
885 group
= (first_block
+ i
) >> 1;
886 if (group
>= ngroups
)
889 if (!bh
[group
- first_group
])
890 /* skip initialized uptodate buddy */
893 if (!buffer_verified(bh
[group
- first_group
]))
894 /* Skip faulty bitmaps */
899 * data carry information regarding this
900 * particular group in the format specified
904 data
= page_address(page
) + (i
* blocksize
);
905 bitmap
= bh
[group
- first_group
]->b_data
;
908 * We place the buddy block and bitmap block
911 if ((first_block
+ i
) & 1) {
912 /* this is block of buddy */
913 BUG_ON(incore
== NULL
);
914 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
915 group
, page
->index
, i
* blocksize
);
916 trace_ext4_mb_buddy_bitmap_load(sb
, group
);
917 grinfo
= ext4_get_group_info(sb
, group
);
918 grinfo
->bb_fragments
= 0;
919 memset(grinfo
->bb_counters
, 0,
920 sizeof(*grinfo
->bb_counters
) *
921 (sb
->s_blocksize_bits
+2));
923 * incore got set to the group block bitmap below
925 ext4_lock_group(sb
, group
);
927 memset(data
, 0xff, blocksize
);
928 ext4_mb_generate_buddy(sb
, data
, incore
, group
);
929 ext4_unlock_group(sb
, group
);
932 /* this is block of bitmap */
933 BUG_ON(incore
!= NULL
);
934 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
935 group
, page
->index
, i
* blocksize
);
936 trace_ext4_mb_bitmap_load(sb
, group
);
938 /* see comments in ext4_mb_put_pa() */
939 ext4_lock_group(sb
, group
);
940 memcpy(data
, bitmap
, blocksize
);
942 /* mark all preallocated blks used in in-core bitmap */
943 ext4_mb_generate_from_pa(sb
, data
, group
);
944 ext4_mb_generate_from_freelist(sb
, data
, group
);
945 ext4_unlock_group(sb
, group
);
947 /* set incore so that the buddy information can be
948 * generated using this
953 SetPageUptodate(page
);
957 for (i
= 0; i
< groups_per_page
; i
++)
966 * Lock the buddy and bitmap pages. This make sure other parallel init_group
967 * on the same buddy page doesn't happen whild holding the buddy page lock.
968 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
969 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
971 static int ext4_mb_get_buddy_page_lock(struct super_block
*sb
,
972 ext4_group_t group
, struct ext4_buddy
*e4b
, gfp_t gfp
)
974 struct inode
*inode
= EXT4_SB(sb
)->s_buddy_cache
;
975 int block
, pnum
, poff
;
979 e4b
->bd_buddy_page
= NULL
;
980 e4b
->bd_bitmap_page
= NULL
;
982 blocks_per_page
= PAGE_SIZE
/ sb
->s_blocksize
;
984 * the buddy cache inode stores the block bitmap
985 * and buddy information in consecutive blocks.
986 * So for each group we need two blocks.
989 pnum
= block
/ blocks_per_page
;
990 poff
= block
% blocks_per_page
;
991 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
994 BUG_ON(page
->mapping
!= inode
->i_mapping
);
995 e4b
->bd_bitmap_page
= page
;
996 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
998 if (blocks_per_page
>= 2) {
999 /* buddy and bitmap are on the same page */
1004 pnum
= block
/ blocks_per_page
;
1005 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1008 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1009 e4b
->bd_buddy_page
= page
;
1013 static void ext4_mb_put_buddy_page_lock(struct ext4_buddy
*e4b
)
1015 if (e4b
->bd_bitmap_page
) {
1016 unlock_page(e4b
->bd_bitmap_page
);
1017 put_page(e4b
->bd_bitmap_page
);
1019 if (e4b
->bd_buddy_page
) {
1020 unlock_page(e4b
->bd_buddy_page
);
1021 put_page(e4b
->bd_buddy_page
);
1026 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1027 * block group lock of all groups for this page; do not hold the BG lock when
1028 * calling this routine!
1030 static noinline_for_stack
1031 int ext4_mb_init_group(struct super_block
*sb
, ext4_group_t group
, gfp_t gfp
)
1034 struct ext4_group_info
*this_grp
;
1035 struct ext4_buddy e4b
;
1040 mb_debug(1, "init group %u\n", group
);
1041 this_grp
= ext4_get_group_info(sb
, group
);
1043 * This ensures that we don't reinit the buddy cache
1044 * page which map to the group from which we are already
1045 * allocating. If we are looking at the buddy cache we would
1046 * have taken a reference using ext4_mb_load_buddy and that
1047 * would have pinned buddy page to page cache.
1048 * The call to ext4_mb_get_buddy_page_lock will mark the
1051 ret
= ext4_mb_get_buddy_page_lock(sb
, group
, &e4b
, gfp
);
1052 if (ret
|| !EXT4_MB_GRP_NEED_INIT(this_grp
)) {
1054 * somebody initialized the group
1055 * return without doing anything
1060 page
= e4b
.bd_bitmap_page
;
1061 ret
= ext4_mb_init_cache(page
, NULL
, gfp
);
1064 if (!PageUptodate(page
)) {
1069 if (e4b
.bd_buddy_page
== NULL
) {
1071 * If both the bitmap and buddy are in
1072 * the same page we don't need to force
1078 /* init buddy cache */
1079 page
= e4b
.bd_buddy_page
;
1080 ret
= ext4_mb_init_cache(page
, e4b
.bd_bitmap
, gfp
);
1083 if (!PageUptodate(page
)) {
1088 ext4_mb_put_buddy_page_lock(&e4b
);
1093 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1094 * block group lock of all groups for this page; do not hold the BG lock when
1095 * calling this routine!
1097 static noinline_for_stack
int
1098 ext4_mb_load_buddy_gfp(struct super_block
*sb
, ext4_group_t group
,
1099 struct ext4_buddy
*e4b
, gfp_t gfp
)
1101 int blocks_per_page
;
1107 struct ext4_group_info
*grp
;
1108 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1109 struct inode
*inode
= sbi
->s_buddy_cache
;
1112 mb_debug(1, "load group %u\n", group
);
1114 blocks_per_page
= PAGE_SIZE
/ sb
->s_blocksize
;
1115 grp
= ext4_get_group_info(sb
, group
);
1117 e4b
->bd_blkbits
= sb
->s_blocksize_bits
;
1120 e4b
->bd_group
= group
;
1121 e4b
->bd_buddy_page
= NULL
;
1122 e4b
->bd_bitmap_page
= NULL
;
1124 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
1126 * we need full data about the group
1127 * to make a good selection
1129 ret
= ext4_mb_init_group(sb
, group
, gfp
);
1135 * the buddy cache inode stores the block bitmap
1136 * and buddy information in consecutive blocks.
1137 * So for each group we need two blocks.
1140 pnum
= block
/ blocks_per_page
;
1141 poff
= block
% blocks_per_page
;
1143 /* we could use find_or_create_page(), but it locks page
1144 * what we'd like to avoid in fast path ... */
1145 page
= find_get_page_flags(inode
->i_mapping
, pnum
, FGP_ACCESSED
);
1146 if (page
== NULL
|| !PageUptodate(page
)) {
1149 * drop the page reference and try
1150 * to get the page with lock. If we
1151 * are not uptodate that implies
1152 * somebody just created the page but
1153 * is yet to initialize the same. So
1154 * wait for it to initialize.
1157 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1159 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1160 if (!PageUptodate(page
)) {
1161 ret
= ext4_mb_init_cache(page
, NULL
, gfp
);
1166 mb_cmp_bitmaps(e4b
, page_address(page
) +
1167 (poff
* sb
->s_blocksize
));
1176 if (!PageUptodate(page
)) {
1181 /* Pages marked accessed already */
1182 e4b
->bd_bitmap_page
= page
;
1183 e4b
->bd_bitmap
= page_address(page
) + (poff
* sb
->s_blocksize
);
1186 pnum
= block
/ blocks_per_page
;
1187 poff
= block
% blocks_per_page
;
1189 page
= find_get_page_flags(inode
->i_mapping
, pnum
, FGP_ACCESSED
);
1190 if (page
== NULL
|| !PageUptodate(page
)) {
1193 page
= find_or_create_page(inode
->i_mapping
, pnum
, gfp
);
1195 BUG_ON(page
->mapping
!= inode
->i_mapping
);
1196 if (!PageUptodate(page
)) {
1197 ret
= ext4_mb_init_cache(page
, e4b
->bd_bitmap
,
1211 if (!PageUptodate(page
)) {
1216 /* Pages marked accessed already */
1217 e4b
->bd_buddy_page
= page
;
1218 e4b
->bd_buddy
= page_address(page
) + (poff
* sb
->s_blocksize
);
1220 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
1221 BUG_ON(e4b
->bd_buddy_page
== NULL
);
1228 if (e4b
->bd_bitmap_page
)
1229 put_page(e4b
->bd_bitmap_page
);
1230 if (e4b
->bd_buddy_page
)
1231 put_page(e4b
->bd_buddy_page
);
1232 e4b
->bd_buddy
= NULL
;
1233 e4b
->bd_bitmap
= NULL
;
1237 static int ext4_mb_load_buddy(struct super_block
*sb
, ext4_group_t group
,
1238 struct ext4_buddy
*e4b
)
1240 return ext4_mb_load_buddy_gfp(sb
, group
, e4b
, GFP_NOFS
);
1243 static void ext4_mb_unload_buddy(struct ext4_buddy
*e4b
)
1245 if (e4b
->bd_bitmap_page
)
1246 put_page(e4b
->bd_bitmap_page
);
1247 if (e4b
->bd_buddy_page
)
1248 put_page(e4b
->bd_buddy_page
);
1252 static int mb_find_order_for_block(struct ext4_buddy
*e4b
, int block
)
1255 int bb_incr
= 1 << (e4b
->bd_blkbits
- 1);
1258 BUG_ON(e4b
->bd_bitmap
== e4b
->bd_buddy
);
1259 BUG_ON(block
>= (1 << (e4b
->bd_blkbits
+ 3)));
1262 while (order
<= e4b
->bd_blkbits
+ 1) {
1264 if (!mb_test_bit(block
, bb
)) {
1265 /* this block is part of buddy of order 'order' */
1275 static void mb_clear_bits(void *bm
, int cur
, int len
)
1281 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1282 /* fast path: clear whole word at once */
1283 addr
= bm
+ (cur
>> 3);
1288 mb_clear_bit(cur
, bm
);
1293 /* clear bits in given range
1294 * will return first found zero bit if any, -1 otherwise
1296 static int mb_test_and_clear_bits(void *bm
, int cur
, int len
)
1303 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1304 /* fast path: clear whole word at once */
1305 addr
= bm
+ (cur
>> 3);
1306 if (*addr
!= (__u32
)(-1) && zero_bit
== -1)
1307 zero_bit
= cur
+ mb_find_next_zero_bit(addr
, 32, 0);
1312 if (!mb_test_and_clear_bit(cur
, bm
) && zero_bit
== -1)
1320 void ext4_set_bits(void *bm
, int cur
, int len
)
1326 if ((cur
& 31) == 0 && (len
- cur
) >= 32) {
1327 /* fast path: set whole word at once */
1328 addr
= bm
+ (cur
>> 3);
1333 mb_set_bit(cur
, bm
);
1339 * _________________________________________________________________ */
1341 static inline int mb_buddy_adjust_border(int* bit
, void* bitmap
, int side
)
1343 if (mb_test_bit(*bit
+ side
, bitmap
)) {
1344 mb_clear_bit(*bit
, bitmap
);
1350 mb_set_bit(*bit
, bitmap
);
1355 static void mb_buddy_mark_free(struct ext4_buddy
*e4b
, int first
, int last
)
1359 void *buddy
= mb_find_buddy(e4b
, order
, &max
);
1364 /* Bits in range [first; last] are known to be set since
1365 * corresponding blocks were allocated. Bits in range
1366 * (first; last) will stay set because they form buddies on
1367 * upper layer. We just deal with borders if they don't
1368 * align with upper layer and then go up.
1369 * Releasing entire group is all about clearing
1370 * single bit of highest order buddy.
1374 * ---------------------------------
1376 * ---------------------------------
1377 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1378 * ---------------------------------
1380 * \_____________________/
1382 * Neither [1] nor [6] is aligned to above layer.
1383 * Left neighbour [0] is free, so mark it busy,
1384 * decrease bb_counters and extend range to
1386 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1387 * mark [6] free, increase bb_counters and shrink range to
1389 * Then shift range to [0; 2], go up and do the same.
1394 e4b
->bd_info
->bb_counters
[order
] += mb_buddy_adjust_border(&first
, buddy
, -1);
1396 e4b
->bd_info
->bb_counters
[order
] += mb_buddy_adjust_border(&last
, buddy
, 1);
1401 if (first
== last
|| !(buddy2
= mb_find_buddy(e4b
, order
, &max
))) {
1402 mb_clear_bits(buddy
, first
, last
- first
+ 1);
1403 e4b
->bd_info
->bb_counters
[order
- 1] += last
- first
+ 1;
1412 static void mb_free_blocks(struct inode
*inode
, struct ext4_buddy
*e4b
,
1413 int first
, int count
)
1415 int left_is_free
= 0;
1416 int right_is_free
= 0;
1418 int last
= first
+ count
- 1;
1419 struct super_block
*sb
= e4b
->bd_sb
;
1421 if (WARN_ON(count
== 0))
1423 BUG_ON(last
>= (sb
->s_blocksize
<< 3));
1424 assert_spin_locked(ext4_group_lock_ptr(sb
, e4b
->bd_group
));
1425 /* Don't bother if the block group is corrupt. */
1426 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b
->bd_info
)))
1429 mb_check_buddy(e4b
);
1430 mb_free_blocks_double(inode
, e4b
, first
, count
);
1432 e4b
->bd_info
->bb_free
+= count
;
1433 if (first
< e4b
->bd_info
->bb_first_free
)
1434 e4b
->bd_info
->bb_first_free
= first
;
1436 /* access memory sequentially: check left neighbour,
1437 * clear range and then check right neighbour
1440 left_is_free
= !mb_test_bit(first
- 1, e4b
->bd_bitmap
);
1441 block
= mb_test_and_clear_bits(e4b
->bd_bitmap
, first
, count
);
1442 if (last
+ 1 < EXT4_SB(sb
)->s_mb_maxs
[0])
1443 right_is_free
= !mb_test_bit(last
+ 1, e4b
->bd_bitmap
);
1445 if (unlikely(block
!= -1)) {
1446 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1447 ext4_fsblk_t blocknr
;
1449 blocknr
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
1450 blocknr
+= EXT4_C2B(sbi
, block
);
1451 ext4_grp_locked_error(sb
, e4b
->bd_group
,
1452 inode
? inode
->i_ino
: 0,
1454 "freeing already freed block "
1455 "(bit %u); block bitmap corrupt.",
1457 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
1458 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
1459 mb_regenerate_buddy(e4b
);
1463 /* let's maintain fragments counter */
1464 if (left_is_free
&& right_is_free
)
1465 e4b
->bd_info
->bb_fragments
--;
1466 else if (!left_is_free
&& !right_is_free
)
1467 e4b
->bd_info
->bb_fragments
++;
1469 /* buddy[0] == bd_bitmap is a special case, so handle
1470 * it right away and let mb_buddy_mark_free stay free of
1471 * zero order checks.
1472 * Check if neighbours are to be coaleasced,
1473 * adjust bitmap bb_counters and borders appropriately.
1476 first
+= !left_is_free
;
1477 e4b
->bd_info
->bb_counters
[0] += left_is_free
? -1 : 1;
1480 last
-= !right_is_free
;
1481 e4b
->bd_info
->bb_counters
[0] += right_is_free
? -1 : 1;
1485 mb_buddy_mark_free(e4b
, first
>> 1, last
>> 1);
1488 mb_set_largest_free_order(sb
, e4b
->bd_info
);
1489 mb_check_buddy(e4b
);
1492 static int mb_find_extent(struct ext4_buddy
*e4b
, int block
,
1493 int needed
, struct ext4_free_extent
*ex
)
1499 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1502 buddy
= mb_find_buddy(e4b
, 0, &max
);
1503 BUG_ON(buddy
== NULL
);
1504 BUG_ON(block
>= max
);
1505 if (mb_test_bit(block
, buddy
)) {
1512 /* find actual order */
1513 order
= mb_find_order_for_block(e4b
, block
);
1514 block
= block
>> order
;
1516 ex
->fe_len
= 1 << order
;
1517 ex
->fe_start
= block
<< order
;
1518 ex
->fe_group
= e4b
->bd_group
;
1520 /* calc difference from given start */
1521 next
= next
- ex
->fe_start
;
1523 ex
->fe_start
+= next
;
1525 while (needed
> ex
->fe_len
&&
1526 mb_find_buddy(e4b
, order
, &max
)) {
1528 if (block
+ 1 >= max
)
1531 next
= (block
+ 1) * (1 << order
);
1532 if (mb_test_bit(next
, e4b
->bd_bitmap
))
1535 order
= mb_find_order_for_block(e4b
, next
);
1537 block
= next
>> order
;
1538 ex
->fe_len
+= 1 << order
;
1541 if (ex
->fe_start
+ ex
->fe_len
> (1 << (e4b
->bd_blkbits
+ 3))) {
1542 /* Should never happen! (but apparently sometimes does?!?) */
1544 ext4_error(e4b
->bd_sb
, "corruption or bug in mb_find_extent "
1545 "block=%d, order=%d needed=%d ex=%u/%d/%d@%u",
1546 block
, order
, needed
, ex
->fe_group
, ex
->fe_start
,
1547 ex
->fe_len
, ex
->fe_logical
);
1555 static int mb_mark_used(struct ext4_buddy
*e4b
, struct ext4_free_extent
*ex
)
1561 int start
= ex
->fe_start
;
1562 int len
= ex
->fe_len
;
1567 BUG_ON(start
+ len
> (e4b
->bd_sb
->s_blocksize
<< 3));
1568 BUG_ON(e4b
->bd_group
!= ex
->fe_group
);
1569 assert_spin_locked(ext4_group_lock_ptr(e4b
->bd_sb
, e4b
->bd_group
));
1570 mb_check_buddy(e4b
);
1571 mb_mark_used_double(e4b
, start
, len
);
1573 e4b
->bd_info
->bb_free
-= len
;
1574 if (e4b
->bd_info
->bb_first_free
== start
)
1575 e4b
->bd_info
->bb_first_free
+= len
;
1577 /* let's maintain fragments counter */
1579 mlen
= !mb_test_bit(start
- 1, e4b
->bd_bitmap
);
1580 if (start
+ len
< EXT4_SB(e4b
->bd_sb
)->s_mb_maxs
[0])
1581 max
= !mb_test_bit(start
+ len
, e4b
->bd_bitmap
);
1583 e4b
->bd_info
->bb_fragments
++;
1584 else if (!mlen
&& !max
)
1585 e4b
->bd_info
->bb_fragments
--;
1587 /* let's maintain buddy itself */
1589 ord
= mb_find_order_for_block(e4b
, start
);
1591 if (((start
>> ord
) << ord
) == start
&& len
>= (1 << ord
)) {
1592 /* the whole chunk may be allocated at once! */
1594 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1595 BUG_ON((start
>> ord
) >= max
);
1596 mb_set_bit(start
>> ord
, buddy
);
1597 e4b
->bd_info
->bb_counters
[ord
]--;
1604 /* store for history */
1606 ret
= len
| (ord
<< 16);
1608 /* we have to split large buddy */
1610 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1611 mb_set_bit(start
>> ord
, buddy
);
1612 e4b
->bd_info
->bb_counters
[ord
]--;
1615 cur
= (start
>> ord
) & ~1U;
1616 buddy
= mb_find_buddy(e4b
, ord
, &max
);
1617 mb_clear_bit(cur
, buddy
);
1618 mb_clear_bit(cur
+ 1, buddy
);
1619 e4b
->bd_info
->bb_counters
[ord
]++;
1620 e4b
->bd_info
->bb_counters
[ord
]++;
1622 mb_set_largest_free_order(e4b
->bd_sb
, e4b
->bd_info
);
1624 ext4_set_bits(e4b
->bd_bitmap
, ex
->fe_start
, len0
);
1625 mb_check_buddy(e4b
);
1631 * Must be called under group lock!
1633 static void ext4_mb_use_best_found(struct ext4_allocation_context
*ac
,
1634 struct ext4_buddy
*e4b
)
1636 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1639 BUG_ON(ac
->ac_b_ex
.fe_group
!= e4b
->bd_group
);
1640 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
1642 ac
->ac_b_ex
.fe_len
= min(ac
->ac_b_ex
.fe_len
, ac
->ac_g_ex
.fe_len
);
1643 ac
->ac_b_ex
.fe_logical
= ac
->ac_g_ex
.fe_logical
;
1644 ret
= mb_mark_used(e4b
, &ac
->ac_b_ex
);
1646 /* preallocation can change ac_b_ex, thus we store actually
1647 * allocated blocks for history */
1648 ac
->ac_f_ex
= ac
->ac_b_ex
;
1650 ac
->ac_status
= AC_STATUS_FOUND
;
1651 ac
->ac_tail
= ret
& 0xffff;
1652 ac
->ac_buddy
= ret
>> 16;
1655 * take the page reference. We want the page to be pinned
1656 * so that we don't get a ext4_mb_init_cache_call for this
1657 * group until we update the bitmap. That would mean we
1658 * double allocate blocks. The reference is dropped
1659 * in ext4_mb_release_context
1661 ac
->ac_bitmap_page
= e4b
->bd_bitmap_page
;
1662 get_page(ac
->ac_bitmap_page
);
1663 ac
->ac_buddy_page
= e4b
->bd_buddy_page
;
1664 get_page(ac
->ac_buddy_page
);
1665 /* store last allocated for subsequent stream allocation */
1666 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
1667 spin_lock(&sbi
->s_md_lock
);
1668 sbi
->s_mb_last_group
= ac
->ac_f_ex
.fe_group
;
1669 sbi
->s_mb_last_start
= ac
->ac_f_ex
.fe_start
;
1670 spin_unlock(&sbi
->s_md_lock
);
1675 * regular allocator, for general purposes allocation
1678 static void ext4_mb_check_limits(struct ext4_allocation_context
*ac
,
1679 struct ext4_buddy
*e4b
,
1682 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1683 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1684 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1685 struct ext4_free_extent ex
;
1688 if (ac
->ac_status
== AC_STATUS_FOUND
)
1691 * We don't want to scan for a whole year
1693 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
&&
1694 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1695 ac
->ac_status
= AC_STATUS_BREAK
;
1700 * Haven't found good chunk so far, let's continue
1702 if (bex
->fe_len
< gex
->fe_len
)
1705 if ((finish_group
|| ac
->ac_found
> sbi
->s_mb_min_to_scan
)
1706 && bex
->fe_group
== e4b
->bd_group
) {
1707 /* recheck chunk's availability - we don't know
1708 * when it was found (within this lock-unlock
1710 max
= mb_find_extent(e4b
, bex
->fe_start
, gex
->fe_len
, &ex
);
1711 if (max
>= gex
->fe_len
) {
1712 ext4_mb_use_best_found(ac
, e4b
);
1719 * The routine checks whether found extent is good enough. If it is,
1720 * then the extent gets marked used and flag is set to the context
1721 * to stop scanning. Otherwise, the extent is compared with the
1722 * previous found extent and if new one is better, then it's stored
1723 * in the context. Later, the best found extent will be used, if
1724 * mballoc can't find good enough extent.
1726 * FIXME: real allocation policy is to be designed yet!
1728 static void ext4_mb_measure_extent(struct ext4_allocation_context
*ac
,
1729 struct ext4_free_extent
*ex
,
1730 struct ext4_buddy
*e4b
)
1732 struct ext4_free_extent
*bex
= &ac
->ac_b_ex
;
1733 struct ext4_free_extent
*gex
= &ac
->ac_g_ex
;
1735 BUG_ON(ex
->fe_len
<= 0);
1736 BUG_ON(ex
->fe_len
> EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1737 BUG_ON(ex
->fe_start
>= EXT4_CLUSTERS_PER_GROUP(ac
->ac_sb
));
1738 BUG_ON(ac
->ac_status
!= AC_STATUS_CONTINUE
);
1743 * The special case - take what you catch first
1745 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
1747 ext4_mb_use_best_found(ac
, e4b
);
1752 * Let's check whether the chuck is good enough
1754 if (ex
->fe_len
== gex
->fe_len
) {
1756 ext4_mb_use_best_found(ac
, e4b
);
1761 * If this is first found extent, just store it in the context
1763 if (bex
->fe_len
== 0) {
1769 * If new found extent is better, store it in the context
1771 if (bex
->fe_len
< gex
->fe_len
) {
1772 /* if the request isn't satisfied, any found extent
1773 * larger than previous best one is better */
1774 if (ex
->fe_len
> bex
->fe_len
)
1776 } else if (ex
->fe_len
> gex
->fe_len
) {
1777 /* if the request is satisfied, then we try to find
1778 * an extent that still satisfy the request, but is
1779 * smaller than previous one */
1780 if (ex
->fe_len
< bex
->fe_len
)
1784 ext4_mb_check_limits(ac
, e4b
, 0);
1787 static noinline_for_stack
1788 int ext4_mb_try_best_found(struct ext4_allocation_context
*ac
,
1789 struct ext4_buddy
*e4b
)
1791 struct ext4_free_extent ex
= ac
->ac_b_ex
;
1792 ext4_group_t group
= ex
.fe_group
;
1796 BUG_ON(ex
.fe_len
<= 0);
1797 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1801 ext4_lock_group(ac
->ac_sb
, group
);
1802 max
= mb_find_extent(e4b
, ex
.fe_start
, ex
.fe_len
, &ex
);
1806 ext4_mb_use_best_found(ac
, e4b
);
1809 ext4_unlock_group(ac
->ac_sb
, group
);
1810 ext4_mb_unload_buddy(e4b
);
1815 static noinline_for_stack
1816 int ext4_mb_find_by_goal(struct ext4_allocation_context
*ac
,
1817 struct ext4_buddy
*e4b
)
1819 ext4_group_t group
= ac
->ac_g_ex
.fe_group
;
1822 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
1823 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
1824 struct ext4_free_extent ex
;
1826 if (!(ac
->ac_flags
& EXT4_MB_HINT_TRY_GOAL
))
1828 if (grp
->bb_free
== 0)
1831 err
= ext4_mb_load_buddy(ac
->ac_sb
, group
, e4b
);
1835 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b
->bd_info
))) {
1836 ext4_mb_unload_buddy(e4b
);
1840 ext4_lock_group(ac
->ac_sb
, group
);
1841 max
= mb_find_extent(e4b
, ac
->ac_g_ex
.fe_start
,
1842 ac
->ac_g_ex
.fe_len
, &ex
);
1843 ex
.fe_logical
= 0xDEADFA11; /* debug value */
1845 if (max
>= ac
->ac_g_ex
.fe_len
&& ac
->ac_g_ex
.fe_len
== sbi
->s_stripe
) {
1848 start
= ext4_group_first_block_no(ac
->ac_sb
, e4b
->bd_group
) +
1850 /* use do_div to get remainder (would be 64-bit modulo) */
1851 if (do_div(start
, sbi
->s_stripe
) == 0) {
1854 ext4_mb_use_best_found(ac
, e4b
);
1856 } else if (max
>= ac
->ac_g_ex
.fe_len
) {
1857 BUG_ON(ex
.fe_len
<= 0);
1858 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1859 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1862 ext4_mb_use_best_found(ac
, e4b
);
1863 } else if (max
> 0 && (ac
->ac_flags
& EXT4_MB_HINT_MERGE
)) {
1864 /* Sometimes, caller may want to merge even small
1865 * number of blocks to an existing extent */
1866 BUG_ON(ex
.fe_len
<= 0);
1867 BUG_ON(ex
.fe_group
!= ac
->ac_g_ex
.fe_group
);
1868 BUG_ON(ex
.fe_start
!= ac
->ac_g_ex
.fe_start
);
1871 ext4_mb_use_best_found(ac
, e4b
);
1873 ext4_unlock_group(ac
->ac_sb
, group
);
1874 ext4_mb_unload_buddy(e4b
);
1880 * The routine scans buddy structures (not bitmap!) from given order
1881 * to max order and tries to find big enough chunk to satisfy the req
1883 static noinline_for_stack
1884 void ext4_mb_simple_scan_group(struct ext4_allocation_context
*ac
,
1885 struct ext4_buddy
*e4b
)
1887 struct super_block
*sb
= ac
->ac_sb
;
1888 struct ext4_group_info
*grp
= e4b
->bd_info
;
1894 BUG_ON(ac
->ac_2order
<= 0);
1895 for (i
= ac
->ac_2order
; i
<= sb
->s_blocksize_bits
+ 1; i
++) {
1896 if (grp
->bb_counters
[i
] == 0)
1899 buddy
= mb_find_buddy(e4b
, i
, &max
);
1900 BUG_ON(buddy
== NULL
);
1902 k
= mb_find_next_zero_bit(buddy
, max
, 0);
1907 ac
->ac_b_ex
.fe_len
= 1 << i
;
1908 ac
->ac_b_ex
.fe_start
= k
<< i
;
1909 ac
->ac_b_ex
.fe_group
= e4b
->bd_group
;
1911 ext4_mb_use_best_found(ac
, e4b
);
1913 BUG_ON(ac
->ac_b_ex
.fe_len
!= ac
->ac_g_ex
.fe_len
);
1915 if (EXT4_SB(sb
)->s_mb_stats
)
1916 atomic_inc(&EXT4_SB(sb
)->s_bal_2orders
);
1923 * The routine scans the group and measures all found extents.
1924 * In order to optimize scanning, caller must pass number of
1925 * free blocks in the group, so the routine can know upper limit.
1927 static noinline_for_stack
1928 void ext4_mb_complex_scan_group(struct ext4_allocation_context
*ac
,
1929 struct ext4_buddy
*e4b
)
1931 struct super_block
*sb
= ac
->ac_sb
;
1932 void *bitmap
= e4b
->bd_bitmap
;
1933 struct ext4_free_extent ex
;
1937 free
= e4b
->bd_info
->bb_free
;
1940 i
= e4b
->bd_info
->bb_first_free
;
1942 while (free
&& ac
->ac_status
== AC_STATUS_CONTINUE
) {
1943 i
= mb_find_next_zero_bit(bitmap
,
1944 EXT4_CLUSTERS_PER_GROUP(sb
), i
);
1945 if (i
>= EXT4_CLUSTERS_PER_GROUP(sb
)) {
1947 * IF we have corrupt bitmap, we won't find any
1948 * free blocks even though group info says we
1949 * we have free blocks
1951 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1952 "%d free clusters as per "
1953 "group info. But bitmap says 0",
1955 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
1956 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
1960 mb_find_extent(e4b
, i
, ac
->ac_g_ex
.fe_len
, &ex
);
1961 BUG_ON(ex
.fe_len
<= 0);
1962 if (free
< ex
.fe_len
) {
1963 ext4_grp_locked_error(sb
, e4b
->bd_group
, 0, 0,
1964 "%d free clusters as per "
1965 "group info. But got %d blocks",
1967 ext4_mark_group_bitmap_corrupted(sb
, e4b
->bd_group
,
1968 EXT4_GROUP_INFO_BBITMAP_CORRUPT
);
1970 * The number of free blocks differs. This mostly
1971 * indicate that the bitmap is corrupt. So exit
1972 * without claiming the space.
1976 ex
.fe_logical
= 0xDEADC0DE; /* debug value */
1977 ext4_mb_measure_extent(ac
, &ex
, e4b
);
1983 ext4_mb_check_limits(ac
, e4b
, 1);
1987 * This is a special case for storages like raid5
1988 * we try to find stripe-aligned chunks for stripe-size-multiple requests
1990 static noinline_for_stack
1991 void ext4_mb_scan_aligned(struct ext4_allocation_context
*ac
,
1992 struct ext4_buddy
*e4b
)
1994 struct super_block
*sb
= ac
->ac_sb
;
1995 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
1996 void *bitmap
= e4b
->bd_bitmap
;
1997 struct ext4_free_extent ex
;
1998 ext4_fsblk_t first_group_block
;
2003 BUG_ON(sbi
->s_stripe
== 0);
2005 /* find first stripe-aligned block in group */
2006 first_group_block
= ext4_group_first_block_no(sb
, e4b
->bd_group
);
2008 a
= first_group_block
+ sbi
->s_stripe
- 1;
2009 do_div(a
, sbi
->s_stripe
);
2010 i
= (a
* sbi
->s_stripe
) - first_group_block
;
2012 while (i
< EXT4_CLUSTERS_PER_GROUP(sb
)) {
2013 if (!mb_test_bit(i
, bitmap
)) {
2014 max
= mb_find_extent(e4b
, i
, sbi
->s_stripe
, &ex
);
2015 if (max
>= sbi
->s_stripe
) {
2017 ex
.fe_logical
= 0xDEADF00D; /* debug value */
2019 ext4_mb_use_best_found(ac
, e4b
);
2028 * This is now called BEFORE we load the buddy bitmap.
2029 * Returns either 1 or 0 indicating that the group is either suitable
2030 * for the allocation or not. In addition it can also return negative
2031 * error code when something goes wrong.
2033 static int ext4_mb_good_group(struct ext4_allocation_context
*ac
,
2034 ext4_group_t group
, int cr
)
2036 unsigned free
, fragments
;
2037 int flex_size
= ext4_flex_bg_size(EXT4_SB(ac
->ac_sb
));
2038 struct ext4_group_info
*grp
= ext4_get_group_info(ac
->ac_sb
, group
);
2040 BUG_ON(cr
< 0 || cr
>= 4);
2042 free
= grp
->bb_free
;
2045 if (cr
<= 2 && free
< ac
->ac_g_ex
.fe_len
)
2048 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp
)))
2051 /* We only do this if the grp has never been initialized */
2052 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
2053 int ret
= ext4_mb_init_group(ac
->ac_sb
, group
, GFP_NOFS
);
2058 fragments
= grp
->bb_fragments
;
2064 BUG_ON(ac
->ac_2order
== 0);
2066 /* Avoid using the first bg of a flexgroup for data files */
2067 if ((ac
->ac_flags
& EXT4_MB_HINT_DATA
) &&
2068 (flex_size
>= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
) &&
2069 ((group
% flex_size
) == 0))
2072 if ((ac
->ac_2order
> ac
->ac_sb
->s_blocksize_bits
+1) ||
2073 (free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
2076 if (grp
->bb_largest_free_order
< ac
->ac_2order
)
2081 if ((free
/ fragments
) >= ac
->ac_g_ex
.fe_len
)
2085 if (free
>= ac
->ac_g_ex
.fe_len
)
2097 static noinline_for_stack
int
2098 ext4_mb_regular_allocator(struct ext4_allocation_context
*ac
)
2100 ext4_group_t ngroups
, group
, i
;
2102 int err
= 0, first_err
= 0;
2103 struct ext4_sb_info
*sbi
;
2104 struct super_block
*sb
;
2105 struct ext4_buddy e4b
;
2109 ngroups
= ext4_get_groups_count(sb
);
2110 /* non-extent files are limited to low blocks/groups */
2111 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)))
2112 ngroups
= sbi
->s_blockfile_groups
;
2114 BUG_ON(ac
->ac_status
== AC_STATUS_FOUND
);
2116 /* first, try the goal */
2117 err
= ext4_mb_find_by_goal(ac
, &e4b
);
2118 if (err
|| ac
->ac_status
== AC_STATUS_FOUND
)
2121 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
2125 * ac->ac2_order is set only if the fe_len is a power of 2
2126 * if ac2_order is set we also set criteria to 0 so that we
2127 * try exact allocation using buddy.
2129 i
= fls(ac
->ac_g_ex
.fe_len
);
2132 * We search using buddy data only if the order of the request
2133 * is greater than equal to the sbi_s_mb_order2_reqs
2134 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
2135 * We also support searching for power-of-two requests only for
2136 * requests upto maximum buddy size we have constructed.
2138 if (i
>= sbi
->s_mb_order2_reqs
&& i
<= sb
->s_blocksize_bits
+ 2) {
2140 * This should tell if fe_len is exactly power of 2
2142 if ((ac
->ac_g_ex
.fe_len
& (~(1 << (i
- 1)))) == 0)
2143 ac
->ac_2order
= i
- 1;
2146 /* if stream allocation is enabled, use global goal */
2147 if (ac
->ac_flags
& EXT4_MB_STREAM_ALLOC
) {
2148 /* TBD: may be hot point */
2149 spin_lock(&sbi
->s_md_lock
);
2150 ac
->ac_g_ex
.fe_group
= sbi
->s_mb_last_group
;
2151 ac
->ac_g_ex
.fe_start
= sbi
->s_mb_last_start
;
2152 spin_unlock(&sbi
->s_md_lock
);
2155 /* Let's just scan groups to find more-less suitable blocks */
2156 cr
= ac
->ac_2order
? 0 : 1;
2158 * cr == 0 try to get exact allocation,
2159 * cr == 3 try to get anything
2162 for (; cr
< 4 && ac
->ac_status
== AC_STATUS_CONTINUE
; cr
++) {
2163 ac
->ac_criteria
= cr
;
2165 * searching for the right group start
2166 * from the goal value specified
2168 group
= ac
->ac_g_ex
.fe_group
;
2170 for (i
= 0; i
< ngroups
; group
++, i
++) {
2174 * Artificially restricted ngroups for non-extent
2175 * files makes group > ngroups possible on first loop.
2177 if (group
>= ngroups
)
2180 /* This now checks without needing the buddy page */
2181 ret
= ext4_mb_good_group(ac
, group
, cr
);
2188 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2192 ext4_lock_group(sb
, group
);
2195 * We need to check again after locking the
2198 ret
= ext4_mb_good_group(ac
, group
, cr
);
2200 ext4_unlock_group(sb
, group
);
2201 ext4_mb_unload_buddy(&e4b
);
2207 ac
->ac_groups_scanned
++;
2209 ext4_mb_simple_scan_group(ac
, &e4b
);
2210 else if (cr
== 1 && sbi
->s_stripe
&&
2211 !(ac
->ac_g_ex
.fe_len
% sbi
->s_stripe
))
2212 ext4_mb_scan_aligned(ac
, &e4b
);
2214 ext4_mb_complex_scan_group(ac
, &e4b
);
2216 ext4_unlock_group(sb
, group
);
2217 ext4_mb_unload_buddy(&e4b
);
2219 if (ac
->ac_status
!= AC_STATUS_CONTINUE
)
2224 if (ac
->ac_b_ex
.fe_len
> 0 && ac
->ac_status
!= AC_STATUS_FOUND
&&
2225 !(ac
->ac_flags
& EXT4_MB_HINT_FIRST
)) {
2227 * We've been searching too long. Let's try to allocate
2228 * the best chunk we've found so far
2231 ext4_mb_try_best_found(ac
, &e4b
);
2232 if (ac
->ac_status
!= AC_STATUS_FOUND
) {
2234 * Someone more lucky has already allocated it.
2235 * The only thing we can do is just take first
2237 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2239 ac
->ac_b_ex
.fe_group
= 0;
2240 ac
->ac_b_ex
.fe_start
= 0;
2241 ac
->ac_b_ex
.fe_len
= 0;
2242 ac
->ac_status
= AC_STATUS_CONTINUE
;
2243 ac
->ac_flags
|= EXT4_MB_HINT_FIRST
;
2245 atomic_inc(&sbi
->s_mb_lost_chunks
);
2250 if (!err
&& ac
->ac_status
!= AC_STATUS_FOUND
&& first_err
)
2255 static void *ext4_mb_seq_groups_start(struct seq_file
*seq
, loff_t
*pos
)
2257 struct super_block
*sb
= PDE_DATA(file_inode(seq
->file
));
2260 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2263 return (void *) ((unsigned long) group
);
2266 static void *ext4_mb_seq_groups_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2268 struct super_block
*sb
= PDE_DATA(file_inode(seq
->file
));
2272 if (*pos
< 0 || *pos
>= ext4_get_groups_count(sb
))
2275 return (void *) ((unsigned long) group
);
2278 static int ext4_mb_seq_groups_show(struct seq_file
*seq
, void *v
)
2280 struct super_block
*sb
= PDE_DATA(file_inode(seq
->file
));
2281 ext4_group_t group
= (ext4_group_t
) ((unsigned long) v
);
2283 int err
, buddy_loaded
= 0;
2284 struct ext4_buddy e4b
;
2285 struct ext4_group_info
*grinfo
;
2286 unsigned char blocksize_bits
= min_t(unsigned char,
2287 sb
->s_blocksize_bits
,
2288 EXT4_MAX_BLOCK_LOG_SIZE
);
2290 struct ext4_group_info info
;
2291 ext4_grpblk_t counters
[EXT4_MAX_BLOCK_LOG_SIZE
+ 2];
2296 seq_puts(seq
, "#group: free frags first ["
2297 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
2298 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
2300 i
= (blocksize_bits
+ 2) * sizeof(sg
.info
.bb_counters
[0]) +
2301 sizeof(struct ext4_group_info
);
2303 grinfo
= ext4_get_group_info(sb
, group
);
2304 /* Load the group info in memory only if not already loaded. */
2305 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo
))) {
2306 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
2308 seq_printf(seq
, "#%-5u: I/O error\n", group
);
2314 memcpy(&sg
, ext4_get_group_info(sb
, group
), i
);
2317 ext4_mb_unload_buddy(&e4b
);
2319 seq_printf(seq
, "#%-5u: %-5u %-5u %-5u [", group
, sg
.info
.bb_free
,
2320 sg
.info
.bb_fragments
, sg
.info
.bb_first_free
);
2321 for (i
= 0; i
<= 13; i
++)
2322 seq_printf(seq
, " %-5u", i
<= blocksize_bits
+ 1 ?
2323 sg
.info
.bb_counters
[i
] : 0);
2324 seq_printf(seq
, " ]\n");
2329 static void ext4_mb_seq_groups_stop(struct seq_file
*seq
, void *v
)
2333 const struct seq_operations ext4_mb_seq_groups_ops
= {
2334 .start
= ext4_mb_seq_groups_start
,
2335 .next
= ext4_mb_seq_groups_next
,
2336 .stop
= ext4_mb_seq_groups_stop
,
2337 .show
= ext4_mb_seq_groups_show
,
2340 static struct kmem_cache
*get_groupinfo_cache(int blocksize_bits
)
2342 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2343 struct kmem_cache
*cachep
= ext4_groupinfo_caches
[cache_index
];
2350 * Allocate the top-level s_group_info array for the specified number
2353 int ext4_mb_alloc_groupinfo(struct super_block
*sb
, ext4_group_t ngroups
)
2355 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2357 struct ext4_group_info
***new_groupinfo
;
2359 size
= (ngroups
+ EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2360 EXT4_DESC_PER_BLOCK_BITS(sb
);
2361 if (size
<= sbi
->s_group_info_size
)
2364 size
= roundup_pow_of_two(sizeof(*sbi
->s_group_info
) * size
);
2365 new_groupinfo
= kvzalloc(size
, GFP_KERNEL
);
2366 if (!new_groupinfo
) {
2367 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy meta group");
2370 if (sbi
->s_group_info
) {
2371 memcpy(new_groupinfo
, sbi
->s_group_info
,
2372 sbi
->s_group_info_size
* sizeof(*sbi
->s_group_info
));
2373 kvfree(sbi
->s_group_info
);
2375 sbi
->s_group_info
= new_groupinfo
;
2376 sbi
->s_group_info_size
= size
/ sizeof(*sbi
->s_group_info
);
2377 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2378 sbi
->s_group_info_size
);
2382 /* Create and initialize ext4_group_info data for the given group. */
2383 int ext4_mb_add_groupinfo(struct super_block
*sb
, ext4_group_t group
,
2384 struct ext4_group_desc
*desc
)
2388 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2389 struct ext4_group_info
**meta_group_info
;
2390 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2393 * First check if this group is the first of a reserved block.
2394 * If it's true, we have to allocate a new table of pointers
2395 * to ext4_group_info structures
2397 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2398 metalen
= sizeof(*meta_group_info
) <<
2399 EXT4_DESC_PER_BLOCK_BITS(sb
);
2400 meta_group_info
= kmalloc(metalen
, GFP_NOFS
);
2401 if (meta_group_info
== NULL
) {
2402 ext4_msg(sb
, KERN_ERR
, "can't allocate mem "
2403 "for a buddy group");
2404 goto exit_meta_group_info
;
2406 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] =
2411 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)];
2412 i
= group
& (EXT4_DESC_PER_BLOCK(sb
) - 1);
2414 meta_group_info
[i
] = kmem_cache_zalloc(cachep
, GFP_NOFS
);
2415 if (meta_group_info
[i
] == NULL
) {
2416 ext4_msg(sb
, KERN_ERR
, "can't allocate buddy mem");
2417 goto exit_group_info
;
2419 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT
,
2420 &(meta_group_info
[i
]->bb_state
));
2423 * initialize bb_free to be able to skip
2424 * empty groups without initialization
2426 if (desc
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2427 meta_group_info
[i
]->bb_free
=
2428 ext4_free_clusters_after_init(sb
, group
, desc
);
2430 meta_group_info
[i
]->bb_free
=
2431 ext4_free_group_clusters(sb
, desc
);
2434 INIT_LIST_HEAD(&meta_group_info
[i
]->bb_prealloc_list
);
2435 init_rwsem(&meta_group_info
[i
]->alloc_sem
);
2436 meta_group_info
[i
]->bb_free_root
= RB_ROOT
;
2437 meta_group_info
[i
]->bb_largest_free_order
= -1; /* uninit */
2441 struct buffer_head
*bh
;
2442 meta_group_info
[i
]->bb_bitmap
=
2443 kmalloc(sb
->s_blocksize
, GFP_NOFS
);
2444 BUG_ON(meta_group_info
[i
]->bb_bitmap
== NULL
);
2445 bh
= ext4_read_block_bitmap(sb
, group
);
2446 BUG_ON(IS_ERR_OR_NULL(bh
));
2447 memcpy(meta_group_info
[i
]->bb_bitmap
, bh
->b_data
,
2456 /* If a meta_group_info table has been allocated, release it now */
2457 if (group
% EXT4_DESC_PER_BLOCK(sb
) == 0) {
2458 kfree(sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)]);
2459 sbi
->s_group_info
[group
>> EXT4_DESC_PER_BLOCK_BITS(sb
)] = NULL
;
2461 exit_meta_group_info
:
2463 } /* ext4_mb_add_groupinfo */
2465 static int ext4_mb_init_backend(struct super_block
*sb
)
2467 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2469 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2471 struct ext4_group_desc
*desc
;
2472 struct kmem_cache
*cachep
;
2474 err
= ext4_mb_alloc_groupinfo(sb
, ngroups
);
2478 sbi
->s_buddy_cache
= new_inode(sb
);
2479 if (sbi
->s_buddy_cache
== NULL
) {
2480 ext4_msg(sb
, KERN_ERR
, "can't get new inode");
2483 /* To avoid potentially colliding with an valid on-disk inode number,
2484 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2485 * not in the inode hash, so it should never be found by iget(), but
2486 * this will avoid confusion if it ever shows up during debugging. */
2487 sbi
->s_buddy_cache
->i_ino
= EXT4_BAD_INO
;
2488 EXT4_I(sbi
->s_buddy_cache
)->i_disksize
= 0;
2489 for (i
= 0; i
< ngroups
; i
++) {
2490 desc
= ext4_get_group_desc(sb
, i
, NULL
);
2492 ext4_msg(sb
, KERN_ERR
, "can't read descriptor %u", i
);
2495 if (ext4_mb_add_groupinfo(sb
, i
, desc
) != 0)
2502 cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2504 kmem_cache_free(cachep
, ext4_get_group_info(sb
, i
));
2505 i
= sbi
->s_group_info_size
;
2507 kfree(sbi
->s_group_info
[i
]);
2508 iput(sbi
->s_buddy_cache
);
2510 kvfree(sbi
->s_group_info
);
2514 static void ext4_groupinfo_destroy_slabs(void)
2518 for (i
= 0; i
< NR_GRPINFO_CACHES
; i
++) {
2519 kmem_cache_destroy(ext4_groupinfo_caches
[i
]);
2520 ext4_groupinfo_caches
[i
] = NULL
;
2524 static int ext4_groupinfo_create_slab(size_t size
)
2526 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex
);
2528 int blocksize_bits
= order_base_2(size
);
2529 int cache_index
= blocksize_bits
- EXT4_MIN_BLOCK_LOG_SIZE
;
2530 struct kmem_cache
*cachep
;
2532 if (cache_index
>= NR_GRPINFO_CACHES
)
2535 if (unlikely(cache_index
< 0))
2538 mutex_lock(&ext4_grpinfo_slab_create_mutex
);
2539 if (ext4_groupinfo_caches
[cache_index
]) {
2540 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2541 return 0; /* Already created */
2544 slab_size
= offsetof(struct ext4_group_info
,
2545 bb_counters
[blocksize_bits
+ 2]);
2547 cachep
= kmem_cache_create(ext4_groupinfo_slab_names
[cache_index
],
2548 slab_size
, 0, SLAB_RECLAIM_ACCOUNT
,
2551 ext4_groupinfo_caches
[cache_index
] = cachep
;
2553 mutex_unlock(&ext4_grpinfo_slab_create_mutex
);
2556 "EXT4-fs: no memory for groupinfo slab cache\n");
2563 int ext4_mb_init(struct super_block
*sb
)
2565 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2567 unsigned offset
, offset_incr
;
2571 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_offsets
);
2573 sbi
->s_mb_offsets
= kmalloc(i
, GFP_KERNEL
);
2574 if (sbi
->s_mb_offsets
== NULL
) {
2579 i
= (sb
->s_blocksize_bits
+ 2) * sizeof(*sbi
->s_mb_maxs
);
2580 sbi
->s_mb_maxs
= kmalloc(i
, GFP_KERNEL
);
2581 if (sbi
->s_mb_maxs
== NULL
) {
2586 ret
= ext4_groupinfo_create_slab(sb
->s_blocksize
);
2590 /* order 0 is regular bitmap */
2591 sbi
->s_mb_maxs
[0] = sb
->s_blocksize
<< 3;
2592 sbi
->s_mb_offsets
[0] = 0;
2596 offset_incr
= 1 << (sb
->s_blocksize_bits
- 1);
2597 max
= sb
->s_blocksize
<< 2;
2599 sbi
->s_mb_offsets
[i
] = offset
;
2600 sbi
->s_mb_maxs
[i
] = max
;
2601 offset
+= offset_incr
;
2602 offset_incr
= offset_incr
>> 1;
2605 } while (i
<= sb
->s_blocksize_bits
+ 1);
2607 spin_lock_init(&sbi
->s_md_lock
);
2608 spin_lock_init(&sbi
->s_bal_lock
);
2609 sbi
->s_mb_free_pending
= 0;
2610 INIT_LIST_HEAD(&sbi
->s_freed_data_list
);
2612 sbi
->s_mb_max_to_scan
= MB_DEFAULT_MAX_TO_SCAN
;
2613 sbi
->s_mb_min_to_scan
= MB_DEFAULT_MIN_TO_SCAN
;
2614 sbi
->s_mb_stats
= MB_DEFAULT_STATS
;
2615 sbi
->s_mb_stream_request
= MB_DEFAULT_STREAM_THRESHOLD
;
2616 sbi
->s_mb_order2_reqs
= MB_DEFAULT_ORDER2_REQS
;
2618 * The default group preallocation is 512, which for 4k block
2619 * sizes translates to 2 megabytes. However for bigalloc file
2620 * systems, this is probably too big (i.e, if the cluster size
2621 * is 1 megabyte, then group preallocation size becomes half a
2622 * gigabyte!). As a default, we will keep a two megabyte
2623 * group pralloc size for cluster sizes up to 64k, and after
2624 * that, we will force a minimum group preallocation size of
2625 * 32 clusters. This translates to 8 megs when the cluster
2626 * size is 256k, and 32 megs when the cluster size is 1 meg,
2627 * which seems reasonable as a default.
2629 sbi
->s_mb_group_prealloc
= max(MB_DEFAULT_GROUP_PREALLOC
>>
2630 sbi
->s_cluster_bits
, 32);
2632 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2633 * to the lowest multiple of s_stripe which is bigger than
2634 * the s_mb_group_prealloc as determined above. We want
2635 * the preallocation size to be an exact multiple of the
2636 * RAID stripe size so that preallocations don't fragment
2639 if (sbi
->s_stripe
> 1) {
2640 sbi
->s_mb_group_prealloc
= roundup(
2641 sbi
->s_mb_group_prealloc
, sbi
->s_stripe
);
2644 sbi
->s_locality_groups
= alloc_percpu(struct ext4_locality_group
);
2645 if (sbi
->s_locality_groups
== NULL
) {
2649 for_each_possible_cpu(i
) {
2650 struct ext4_locality_group
*lg
;
2651 lg
= per_cpu_ptr(sbi
->s_locality_groups
, i
);
2652 mutex_init(&lg
->lg_mutex
);
2653 for (j
= 0; j
< PREALLOC_TB_SIZE
; j
++)
2654 INIT_LIST_HEAD(&lg
->lg_prealloc_list
[j
]);
2655 spin_lock_init(&lg
->lg_prealloc_lock
);
2658 /* init file for buddy data */
2659 ret
= ext4_mb_init_backend(sb
);
2661 goto out_free_locality_groups
;
2665 out_free_locality_groups
:
2666 free_percpu(sbi
->s_locality_groups
);
2667 sbi
->s_locality_groups
= NULL
;
2669 kfree(sbi
->s_mb_offsets
);
2670 sbi
->s_mb_offsets
= NULL
;
2671 kfree(sbi
->s_mb_maxs
);
2672 sbi
->s_mb_maxs
= NULL
;
2676 /* need to called with the ext4 group lock held */
2677 static void ext4_mb_cleanup_pa(struct ext4_group_info
*grp
)
2679 struct ext4_prealloc_space
*pa
;
2680 struct list_head
*cur
, *tmp
;
2683 list_for_each_safe(cur
, tmp
, &grp
->bb_prealloc_list
) {
2684 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
2685 list_del(&pa
->pa_group_list
);
2687 kmem_cache_free(ext4_pspace_cachep
, pa
);
2690 mb_debug(1, "mballoc: %u PAs left\n", count
);
2694 int ext4_mb_release(struct super_block
*sb
)
2696 ext4_group_t ngroups
= ext4_get_groups_count(sb
);
2698 int num_meta_group_infos
;
2699 struct ext4_group_info
*grinfo
;
2700 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2701 struct kmem_cache
*cachep
= get_groupinfo_cache(sb
->s_blocksize_bits
);
2703 if (sbi
->s_group_info
) {
2704 for (i
= 0; i
< ngroups
; i
++) {
2705 grinfo
= ext4_get_group_info(sb
, i
);
2707 kfree(grinfo
->bb_bitmap
);
2709 ext4_lock_group(sb
, i
);
2710 ext4_mb_cleanup_pa(grinfo
);
2711 ext4_unlock_group(sb
, i
);
2712 kmem_cache_free(cachep
, grinfo
);
2714 num_meta_group_infos
= (ngroups
+
2715 EXT4_DESC_PER_BLOCK(sb
) - 1) >>
2716 EXT4_DESC_PER_BLOCK_BITS(sb
);
2717 for (i
= 0; i
< num_meta_group_infos
; i
++)
2718 kfree(sbi
->s_group_info
[i
]);
2719 kvfree(sbi
->s_group_info
);
2721 kfree(sbi
->s_mb_offsets
);
2722 kfree(sbi
->s_mb_maxs
);
2723 iput(sbi
->s_buddy_cache
);
2724 if (sbi
->s_mb_stats
) {
2725 ext4_msg(sb
, KERN_INFO
,
2726 "mballoc: %u blocks %u reqs (%u success)",
2727 atomic_read(&sbi
->s_bal_allocated
),
2728 atomic_read(&sbi
->s_bal_reqs
),
2729 atomic_read(&sbi
->s_bal_success
));
2730 ext4_msg(sb
, KERN_INFO
,
2731 "mballoc: %u extents scanned, %u goal hits, "
2732 "%u 2^N hits, %u breaks, %u lost",
2733 atomic_read(&sbi
->s_bal_ex_scanned
),
2734 atomic_read(&sbi
->s_bal_goals
),
2735 atomic_read(&sbi
->s_bal_2orders
),
2736 atomic_read(&sbi
->s_bal_breaks
),
2737 atomic_read(&sbi
->s_mb_lost_chunks
));
2738 ext4_msg(sb
, KERN_INFO
,
2739 "mballoc: %lu generated and it took %Lu",
2740 sbi
->s_mb_buddies_generated
,
2741 sbi
->s_mb_generation_time
);
2742 ext4_msg(sb
, KERN_INFO
,
2743 "mballoc: %u preallocated, %u discarded",
2744 atomic_read(&sbi
->s_mb_preallocated
),
2745 atomic_read(&sbi
->s_mb_discarded
));
2748 free_percpu(sbi
->s_locality_groups
);
2753 static inline int ext4_issue_discard(struct super_block
*sb
,
2754 ext4_group_t block_group
, ext4_grpblk_t cluster
, int count
,
2757 ext4_fsblk_t discard_block
;
2759 discard_block
= (EXT4_C2B(EXT4_SB(sb
), cluster
) +
2760 ext4_group_first_block_no(sb
, block_group
));
2761 count
= EXT4_C2B(EXT4_SB(sb
), count
);
2762 trace_ext4_discard_blocks(sb
,
2763 (unsigned long long) discard_block
, count
);
2765 return __blkdev_issue_discard(sb
->s_bdev
,
2766 (sector_t
)discard_block
<< (sb
->s_blocksize_bits
- 9),
2767 (sector_t
)count
<< (sb
->s_blocksize_bits
- 9),
2770 return sb_issue_discard(sb
, discard_block
, count
, GFP_NOFS
, 0);
2773 static void ext4_free_data_in_buddy(struct super_block
*sb
,
2774 struct ext4_free_data
*entry
)
2776 struct ext4_buddy e4b
;
2777 struct ext4_group_info
*db
;
2778 int err
, count
= 0, count2
= 0;
2780 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2781 entry
->efd_count
, entry
->efd_group
, entry
);
2783 err
= ext4_mb_load_buddy(sb
, entry
->efd_group
, &e4b
);
2784 /* we expect to find existing buddy because it's pinned */
2787 spin_lock(&EXT4_SB(sb
)->s_md_lock
);
2788 EXT4_SB(sb
)->s_mb_free_pending
-= entry
->efd_count
;
2789 spin_unlock(&EXT4_SB(sb
)->s_md_lock
);
2792 /* there are blocks to put in buddy to make them really free */
2793 count
+= entry
->efd_count
;
2795 ext4_lock_group(sb
, entry
->efd_group
);
2796 /* Take it out of per group rb tree */
2797 rb_erase(&entry
->efd_node
, &(db
->bb_free_root
));
2798 mb_free_blocks(NULL
, &e4b
, entry
->efd_start_cluster
, entry
->efd_count
);
2801 * Clear the trimmed flag for the group so that the next
2802 * ext4_trim_fs can trim it.
2803 * If the volume is mounted with -o discard, online discard
2804 * is supported and the free blocks will be trimmed online.
2806 if (!test_opt(sb
, DISCARD
))
2807 EXT4_MB_GRP_CLEAR_TRIMMED(db
);
2809 if (!db
->bb_free_root
.rb_node
) {
2810 /* No more items in the per group rb tree
2811 * balance refcounts from ext4_mb_free_metadata()
2813 put_page(e4b
.bd_buddy_page
);
2814 put_page(e4b
.bd_bitmap_page
);
2816 ext4_unlock_group(sb
, entry
->efd_group
);
2817 kmem_cache_free(ext4_free_data_cachep
, entry
);
2818 ext4_mb_unload_buddy(&e4b
);
2820 mb_debug(1, "freed %u blocks in %u structures\n", count
, count2
);
2824 * This function is called by the jbd2 layer once the commit has finished,
2825 * so we know we can free the blocks that were released with that commit.
2827 void ext4_process_freed_data(struct super_block
*sb
, tid_t commit_tid
)
2829 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
2830 struct ext4_free_data
*entry
, *tmp
;
2831 struct bio
*discard_bio
= NULL
;
2832 struct list_head freed_data_list
;
2833 struct list_head
*cut_pos
= NULL
;
2836 INIT_LIST_HEAD(&freed_data_list
);
2838 spin_lock(&sbi
->s_md_lock
);
2839 list_for_each_entry(entry
, &sbi
->s_freed_data_list
, efd_list
) {
2840 if (entry
->efd_tid
!= commit_tid
)
2842 cut_pos
= &entry
->efd_list
;
2845 list_cut_position(&freed_data_list
, &sbi
->s_freed_data_list
,
2847 spin_unlock(&sbi
->s_md_lock
);
2849 if (test_opt(sb
, DISCARD
)) {
2850 list_for_each_entry(entry
, &freed_data_list
, efd_list
) {
2851 err
= ext4_issue_discard(sb
, entry
->efd_group
,
2852 entry
->efd_start_cluster
,
2855 if (err
&& err
!= -EOPNOTSUPP
) {
2856 ext4_msg(sb
, KERN_WARNING
, "discard request in"
2857 " group:%d block:%d count:%d failed"
2858 " with %d", entry
->efd_group
,
2859 entry
->efd_start_cluster
,
2860 entry
->efd_count
, err
);
2861 } else if (err
== -EOPNOTSUPP
)
2866 submit_bio_wait(discard_bio
);
2867 bio_put(discard_bio
);
2871 list_for_each_entry_safe(entry
, tmp
, &freed_data_list
, efd_list
)
2872 ext4_free_data_in_buddy(sb
, entry
);
2875 int __init
ext4_init_mballoc(void)
2877 ext4_pspace_cachep
= KMEM_CACHE(ext4_prealloc_space
,
2878 SLAB_RECLAIM_ACCOUNT
);
2879 if (ext4_pspace_cachep
== NULL
)
2882 ext4_ac_cachep
= KMEM_CACHE(ext4_allocation_context
,
2883 SLAB_RECLAIM_ACCOUNT
);
2884 if (ext4_ac_cachep
== NULL
) {
2885 kmem_cache_destroy(ext4_pspace_cachep
);
2889 ext4_free_data_cachep
= KMEM_CACHE(ext4_free_data
,
2890 SLAB_RECLAIM_ACCOUNT
);
2891 if (ext4_free_data_cachep
== NULL
) {
2892 kmem_cache_destroy(ext4_pspace_cachep
);
2893 kmem_cache_destroy(ext4_ac_cachep
);
2899 void ext4_exit_mballoc(void)
2902 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2903 * before destroying the slab cache.
2906 kmem_cache_destroy(ext4_pspace_cachep
);
2907 kmem_cache_destroy(ext4_ac_cachep
);
2908 kmem_cache_destroy(ext4_free_data_cachep
);
2909 ext4_groupinfo_destroy_slabs();
2914 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
2915 * Returns 0 if success or error code
2917 static noinline_for_stack
int
2918 ext4_mb_mark_diskspace_used(struct ext4_allocation_context
*ac
,
2919 handle_t
*handle
, unsigned int reserv_clstrs
)
2921 struct buffer_head
*bitmap_bh
= NULL
;
2922 struct ext4_group_desc
*gdp
;
2923 struct buffer_head
*gdp_bh
;
2924 struct ext4_sb_info
*sbi
;
2925 struct super_block
*sb
;
2929 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
2930 BUG_ON(ac
->ac_b_ex
.fe_len
<= 0);
2935 bitmap_bh
= ext4_read_block_bitmap(sb
, ac
->ac_b_ex
.fe_group
);
2936 if (IS_ERR(bitmap_bh
)) {
2937 err
= PTR_ERR(bitmap_bh
);
2942 BUFFER_TRACE(bitmap_bh
, "getting write access");
2943 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
2948 gdp
= ext4_get_group_desc(sb
, ac
->ac_b_ex
.fe_group
, &gdp_bh
);
2952 ext4_debug("using block group %u(%d)\n", ac
->ac_b_ex
.fe_group
,
2953 ext4_free_group_clusters(sb
, gdp
));
2955 BUFFER_TRACE(gdp_bh
, "get_write_access");
2956 err
= ext4_journal_get_write_access(handle
, gdp_bh
);
2960 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
2962 len
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
2963 if (!ext4_data_block_valid(sbi
, block
, len
)) {
2964 ext4_error(sb
, "Allocating blocks %llu-%llu which overlap "
2965 "fs metadata", block
, block
+len
);
2966 /* File system mounted not to panic on error
2967 * Fix the bitmap and return EFSCORRUPTED
2968 * We leak some of the blocks here.
2970 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2971 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2972 ac
->ac_b_ex
.fe_len
);
2973 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
2974 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
2976 err
= -EFSCORRUPTED
;
2980 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
2981 #ifdef AGGRESSIVE_CHECK
2984 for (i
= 0; i
< ac
->ac_b_ex
.fe_len
; i
++) {
2985 BUG_ON(mb_test_bit(ac
->ac_b_ex
.fe_start
+ i
,
2986 bitmap_bh
->b_data
));
2990 ext4_set_bits(bitmap_bh
->b_data
, ac
->ac_b_ex
.fe_start
,
2991 ac
->ac_b_ex
.fe_len
);
2992 if (gdp
->bg_flags
& cpu_to_le16(EXT4_BG_BLOCK_UNINIT
)) {
2993 gdp
->bg_flags
&= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT
);
2994 ext4_free_group_clusters_set(sb
, gdp
,
2995 ext4_free_clusters_after_init(sb
,
2996 ac
->ac_b_ex
.fe_group
, gdp
));
2998 len
= ext4_free_group_clusters(sb
, gdp
) - ac
->ac_b_ex
.fe_len
;
2999 ext4_free_group_clusters_set(sb
, gdp
, len
);
3000 ext4_block_bitmap_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
, bitmap_bh
);
3001 ext4_group_desc_csum_set(sb
, ac
->ac_b_ex
.fe_group
, gdp
);
3003 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3004 percpu_counter_sub(&sbi
->s_freeclusters_counter
, ac
->ac_b_ex
.fe_len
);
3006 * Now reduce the dirty block count also. Should not go negative
3008 if (!(ac
->ac_flags
& EXT4_MB_DELALLOC_RESERVED
))
3009 /* release all the reserved blocks if non delalloc */
3010 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
3013 if (sbi
->s_log_groups_per_flex
) {
3014 ext4_group_t flex_group
= ext4_flex_group(sbi
,
3015 ac
->ac_b_ex
.fe_group
);
3016 atomic64_sub(ac
->ac_b_ex
.fe_len
,
3017 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
3020 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
3023 err
= ext4_handle_dirty_metadata(handle
, NULL
, gdp_bh
);
3031 * here we normalize request for locality group
3032 * Group request are normalized to s_mb_group_prealloc, which goes to
3033 * s_strip if we set the same via mount option.
3034 * s_mb_group_prealloc can be configured via
3035 * /sys/fs/ext4/<partition>/mb_group_prealloc
3037 * XXX: should we try to preallocate more than the group has now?
3039 static void ext4_mb_normalize_group_request(struct ext4_allocation_context
*ac
)
3041 struct super_block
*sb
= ac
->ac_sb
;
3042 struct ext4_locality_group
*lg
= ac
->ac_lg
;
3045 ac
->ac_g_ex
.fe_len
= EXT4_SB(sb
)->s_mb_group_prealloc
;
3046 mb_debug(1, "#%u: goal %u blocks for locality group\n",
3047 current
->pid
, ac
->ac_g_ex
.fe_len
);
3051 * Normalization means making request better in terms of
3052 * size and alignment
3054 static noinline_for_stack
void
3055 ext4_mb_normalize_request(struct ext4_allocation_context
*ac
,
3056 struct ext4_allocation_request
*ar
)
3058 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3061 loff_t size
, start_off
;
3062 loff_t orig_size __maybe_unused
;
3064 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3065 struct ext4_prealloc_space
*pa
;
3067 /* do normalize only data requests, metadata requests
3068 do not need preallocation */
3069 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3072 /* sometime caller may want exact blocks */
3073 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
3076 /* caller may indicate that preallocation isn't
3077 * required (it's a tail, for example) */
3078 if (ac
->ac_flags
& EXT4_MB_HINT_NOPREALLOC
)
3081 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
) {
3082 ext4_mb_normalize_group_request(ac
);
3086 bsbits
= ac
->ac_sb
->s_blocksize_bits
;
3088 /* first, let's learn actual file size
3089 * given current request is allocated */
3090 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
3091 size
= size
<< bsbits
;
3092 if (size
< i_size_read(ac
->ac_inode
))
3093 size
= i_size_read(ac
->ac_inode
);
3096 /* max size of free chunks */
3099 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3100 (req <= (size) || max <= (chunk_size))
3102 /* first, try to predict filesize */
3103 /* XXX: should this table be tunable? */
3105 if (size
<= 16 * 1024) {
3107 } else if (size
<= 32 * 1024) {
3109 } else if (size
<= 64 * 1024) {
3111 } else if (size
<= 128 * 1024) {
3113 } else if (size
<= 256 * 1024) {
3115 } else if (size
<= 512 * 1024) {
3117 } else if (size
<= 1024 * 1024) {
3119 } else if (NRL_CHECK_SIZE(size
, 4 * 1024 * 1024, max
, 2 * 1024)) {
3120 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3121 (21 - bsbits
)) << 21;
3122 size
= 2 * 1024 * 1024;
3123 } else if (NRL_CHECK_SIZE(size
, 8 * 1024 * 1024, max
, 4 * 1024)) {
3124 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3125 (22 - bsbits
)) << 22;
3126 size
= 4 * 1024 * 1024;
3127 } else if (NRL_CHECK_SIZE(ac
->ac_o_ex
.fe_len
,
3128 (8<<20)>>bsbits
, max
, 8 * 1024)) {
3129 start_off
= ((loff_t
)ac
->ac_o_ex
.fe_logical
>>
3130 (23 - bsbits
)) << 23;
3131 size
= 8 * 1024 * 1024;
3133 start_off
= (loff_t
) ac
->ac_o_ex
.fe_logical
<< bsbits
;
3134 size
= (loff_t
) EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3135 ac
->ac_o_ex
.fe_len
) << bsbits
;
3137 size
= size
>> bsbits
;
3138 start
= start_off
>> bsbits
;
3140 /* don't cover already allocated blocks in selected range */
3141 if (ar
->pleft
&& start
<= ar
->lleft
) {
3142 size
-= ar
->lleft
+ 1 - start
;
3143 start
= ar
->lleft
+ 1;
3145 if (ar
->pright
&& start
+ size
- 1 >= ar
->lright
)
3146 size
-= start
+ size
- ar
->lright
;
3149 * Trim allocation request for filesystems with artificially small
3152 if (size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
))
3153 size
= EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
);
3157 /* check we don't cross already preallocated blocks */
3159 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3164 spin_lock(&pa
->pa_lock
);
3165 if (pa
->pa_deleted
) {
3166 spin_unlock(&pa
->pa_lock
);
3170 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3173 /* PA must not overlap original request */
3174 BUG_ON(!(ac
->ac_o_ex
.fe_logical
>= pa_end
||
3175 ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
));
3177 /* skip PAs this normalized request doesn't overlap with */
3178 if (pa
->pa_lstart
>= end
|| pa_end
<= start
) {
3179 spin_unlock(&pa
->pa_lock
);
3182 BUG_ON(pa
->pa_lstart
<= start
&& pa_end
>= end
);
3184 /* adjust start or end to be adjacent to this pa */
3185 if (pa_end
<= ac
->ac_o_ex
.fe_logical
) {
3186 BUG_ON(pa_end
< start
);
3188 } else if (pa
->pa_lstart
> ac
->ac_o_ex
.fe_logical
) {
3189 BUG_ON(pa
->pa_lstart
> end
);
3190 end
= pa
->pa_lstart
;
3192 spin_unlock(&pa
->pa_lock
);
3197 /* XXX: extra loop to check we really don't overlap preallocations */
3199 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3202 spin_lock(&pa
->pa_lock
);
3203 if (pa
->pa_deleted
== 0) {
3204 pa_end
= pa
->pa_lstart
+ EXT4_C2B(EXT4_SB(ac
->ac_sb
),
3206 BUG_ON(!(start
>= pa_end
|| end
<= pa
->pa_lstart
));
3208 spin_unlock(&pa
->pa_lock
);
3212 if (start
+ size
<= ac
->ac_o_ex
.fe_logical
&&
3213 start
> ac
->ac_o_ex
.fe_logical
) {
3214 ext4_msg(ac
->ac_sb
, KERN_ERR
,
3215 "start %lu, size %lu, fe_logical %lu",
3216 (unsigned long) start
, (unsigned long) size
,
3217 (unsigned long) ac
->ac_o_ex
.fe_logical
);
3220 BUG_ON(size
<= 0 || size
> EXT4_BLOCKS_PER_GROUP(ac
->ac_sb
));
3222 /* now prepare goal request */
3224 /* XXX: is it better to align blocks WRT to logical
3225 * placement or satisfy big request as is */
3226 ac
->ac_g_ex
.fe_logical
= start
;
3227 ac
->ac_g_ex
.fe_len
= EXT4_NUM_B2C(sbi
, size
);
3229 /* define goal start in order to merge */
3230 if (ar
->pright
&& (ar
->lright
== (start
+ size
))) {
3231 /* merge to the right */
3232 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pright
- size
,
3233 &ac
->ac_f_ex
.fe_group
,
3234 &ac
->ac_f_ex
.fe_start
);
3235 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3237 if (ar
->pleft
&& (ar
->lleft
+ 1 == start
)) {
3238 /* merge to the left */
3239 ext4_get_group_no_and_offset(ac
->ac_sb
, ar
->pleft
+ 1,
3240 &ac
->ac_f_ex
.fe_group
,
3241 &ac
->ac_f_ex
.fe_start
);
3242 ac
->ac_flags
|= EXT4_MB_HINT_TRY_GOAL
;
3245 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size
,
3246 (unsigned) orig_size
, (unsigned) start
);
3249 static void ext4_mb_collect_stats(struct ext4_allocation_context
*ac
)
3251 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3253 if (sbi
->s_mb_stats
&& ac
->ac_g_ex
.fe_len
> 1) {
3254 atomic_inc(&sbi
->s_bal_reqs
);
3255 atomic_add(ac
->ac_b_ex
.fe_len
, &sbi
->s_bal_allocated
);
3256 if (ac
->ac_b_ex
.fe_len
>= ac
->ac_o_ex
.fe_len
)
3257 atomic_inc(&sbi
->s_bal_success
);
3258 atomic_add(ac
->ac_found
, &sbi
->s_bal_ex_scanned
);
3259 if (ac
->ac_g_ex
.fe_start
== ac
->ac_b_ex
.fe_start
&&
3260 ac
->ac_g_ex
.fe_group
== ac
->ac_b_ex
.fe_group
)
3261 atomic_inc(&sbi
->s_bal_goals
);
3262 if (ac
->ac_found
> sbi
->s_mb_max_to_scan
)
3263 atomic_inc(&sbi
->s_bal_breaks
);
3266 if (ac
->ac_op
== EXT4_MB_HISTORY_ALLOC
)
3267 trace_ext4_mballoc_alloc(ac
);
3269 trace_ext4_mballoc_prealloc(ac
);
3273 * Called on failure; free up any blocks from the inode PA for this
3274 * context. We don't need this for MB_GROUP_PA because we only change
3275 * pa_free in ext4_mb_release_context(), but on failure, we've already
3276 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3278 static void ext4_discard_allocated_blocks(struct ext4_allocation_context
*ac
)
3280 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
3281 struct ext4_buddy e4b
;
3285 if (ac
->ac_f_ex
.fe_len
== 0)
3287 err
= ext4_mb_load_buddy(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
, &e4b
);
3290 * This should never happen since we pin the
3291 * pages in the ext4_allocation_context so
3292 * ext4_mb_load_buddy() should never fail.
3294 WARN(1, "mb_load_buddy failed (%d)", err
);
3297 ext4_lock_group(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
);
3298 mb_free_blocks(ac
->ac_inode
, &e4b
, ac
->ac_f_ex
.fe_start
,
3299 ac
->ac_f_ex
.fe_len
);
3300 ext4_unlock_group(ac
->ac_sb
, ac
->ac_f_ex
.fe_group
);
3301 ext4_mb_unload_buddy(&e4b
);
3304 if (pa
->pa_type
== MB_INODE_PA
)
3305 pa
->pa_free
+= ac
->ac_b_ex
.fe_len
;
3309 * use blocks preallocated to inode
3311 static void ext4_mb_use_inode_pa(struct ext4_allocation_context
*ac
,
3312 struct ext4_prealloc_space
*pa
)
3314 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3319 /* found preallocated blocks, use them */
3320 start
= pa
->pa_pstart
+ (ac
->ac_o_ex
.fe_logical
- pa
->pa_lstart
);
3321 end
= min(pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
),
3322 start
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
));
3323 len
= EXT4_NUM_B2C(sbi
, end
- start
);
3324 ext4_get_group_no_and_offset(ac
->ac_sb
, start
, &ac
->ac_b_ex
.fe_group
,
3325 &ac
->ac_b_ex
.fe_start
);
3326 ac
->ac_b_ex
.fe_len
= len
;
3327 ac
->ac_status
= AC_STATUS_FOUND
;
3330 BUG_ON(start
< pa
->pa_pstart
);
3331 BUG_ON(end
> pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
));
3332 BUG_ON(pa
->pa_free
< len
);
3335 mb_debug(1, "use %llu/%u from inode pa %p\n", start
, len
, pa
);
3339 * use blocks preallocated to locality group
3341 static void ext4_mb_use_group_pa(struct ext4_allocation_context
*ac
,
3342 struct ext4_prealloc_space
*pa
)
3344 unsigned int len
= ac
->ac_o_ex
.fe_len
;
3346 ext4_get_group_no_and_offset(ac
->ac_sb
, pa
->pa_pstart
,
3347 &ac
->ac_b_ex
.fe_group
,
3348 &ac
->ac_b_ex
.fe_start
);
3349 ac
->ac_b_ex
.fe_len
= len
;
3350 ac
->ac_status
= AC_STATUS_FOUND
;
3353 /* we don't correct pa_pstart or pa_plen here to avoid
3354 * possible race when the group is being loaded concurrently
3355 * instead we correct pa later, after blocks are marked
3356 * in on-disk bitmap -- see ext4_mb_release_context()
3357 * Other CPUs are prevented from allocating from this pa by lg_mutex
3359 mb_debug(1, "use %u/%u from group pa %p\n", pa
->pa_lstart
-len
, len
, pa
);
3363 * Return the prealloc space that have minimal distance
3364 * from the goal block. @cpa is the prealloc
3365 * space that is having currently known minimal distance
3366 * from the goal block.
3368 static struct ext4_prealloc_space
*
3369 ext4_mb_check_group_pa(ext4_fsblk_t goal_block
,
3370 struct ext4_prealloc_space
*pa
,
3371 struct ext4_prealloc_space
*cpa
)
3373 ext4_fsblk_t cur_distance
, new_distance
;
3376 atomic_inc(&pa
->pa_count
);
3379 cur_distance
= abs(goal_block
- cpa
->pa_pstart
);
3380 new_distance
= abs(goal_block
- pa
->pa_pstart
);
3382 if (cur_distance
<= new_distance
)
3385 /* drop the previous reference */
3386 atomic_dec(&cpa
->pa_count
);
3387 atomic_inc(&pa
->pa_count
);
3392 * search goal blocks in preallocated space
3394 static noinline_for_stack
int
3395 ext4_mb_use_preallocated(struct ext4_allocation_context
*ac
)
3397 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
3399 struct ext4_inode_info
*ei
= EXT4_I(ac
->ac_inode
);
3400 struct ext4_locality_group
*lg
;
3401 struct ext4_prealloc_space
*pa
, *cpa
= NULL
;
3402 ext4_fsblk_t goal_block
;
3404 /* only data can be preallocated */
3405 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
3408 /* first, try per-file preallocation */
3410 list_for_each_entry_rcu(pa
, &ei
->i_prealloc_list
, pa_inode_list
) {
3412 /* all fields in this condition don't change,
3413 * so we can skip locking for them */
3414 if (ac
->ac_o_ex
.fe_logical
< pa
->pa_lstart
||
3415 ac
->ac_o_ex
.fe_logical
>= (pa
->pa_lstart
+
3416 EXT4_C2B(sbi
, pa
->pa_len
)))
3419 /* non-extent files can't have physical blocks past 2^32 */
3420 if (!(ext4_test_inode_flag(ac
->ac_inode
, EXT4_INODE_EXTENTS
)) &&
3421 (pa
->pa_pstart
+ EXT4_C2B(sbi
, pa
->pa_len
) >
3422 EXT4_MAX_BLOCK_FILE_PHYS
))
3425 /* found preallocated blocks, use them */
3426 spin_lock(&pa
->pa_lock
);
3427 if (pa
->pa_deleted
== 0 && pa
->pa_free
) {
3428 atomic_inc(&pa
->pa_count
);
3429 ext4_mb_use_inode_pa(ac
, pa
);
3430 spin_unlock(&pa
->pa_lock
);
3431 ac
->ac_criteria
= 10;
3435 spin_unlock(&pa
->pa_lock
);
3439 /* can we use group allocation? */
3440 if (!(ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
))
3443 /* inode may have no locality group for some reason */
3447 order
= fls(ac
->ac_o_ex
.fe_len
) - 1;
3448 if (order
> PREALLOC_TB_SIZE
- 1)
3449 /* The max size of hash table is PREALLOC_TB_SIZE */
3450 order
= PREALLOC_TB_SIZE
- 1;
3452 goal_block
= ext4_grp_offs_to_block(ac
->ac_sb
, &ac
->ac_g_ex
);
3454 * search for the prealloc space that is having
3455 * minimal distance from the goal block.
3457 for (i
= order
; i
< PREALLOC_TB_SIZE
; i
++) {
3459 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[i
],
3461 spin_lock(&pa
->pa_lock
);
3462 if (pa
->pa_deleted
== 0 &&
3463 pa
->pa_free
>= ac
->ac_o_ex
.fe_len
) {
3465 cpa
= ext4_mb_check_group_pa(goal_block
,
3468 spin_unlock(&pa
->pa_lock
);
3473 ext4_mb_use_group_pa(ac
, cpa
);
3474 ac
->ac_criteria
= 20;
3481 * the function goes through all block freed in the group
3482 * but not yet committed and marks them used in in-core bitmap.
3483 * buddy must be generated from this bitmap
3484 * Need to be called with the ext4 group lock held
3486 static void ext4_mb_generate_from_freelist(struct super_block
*sb
, void *bitmap
,
3490 struct ext4_group_info
*grp
;
3491 struct ext4_free_data
*entry
;
3493 grp
= ext4_get_group_info(sb
, group
);
3494 n
= rb_first(&(grp
->bb_free_root
));
3497 entry
= rb_entry(n
, struct ext4_free_data
, efd_node
);
3498 ext4_set_bits(bitmap
, entry
->efd_start_cluster
, entry
->efd_count
);
3505 * the function goes through all preallocation in this group and marks them
3506 * used in in-core bitmap. buddy must be generated from this bitmap
3507 * Need to be called with ext4 group lock held
3509 static noinline_for_stack
3510 void ext4_mb_generate_from_pa(struct super_block
*sb
, void *bitmap
,
3513 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3514 struct ext4_prealloc_space
*pa
;
3515 struct list_head
*cur
;
3516 ext4_group_t groupnr
;
3517 ext4_grpblk_t start
;
3518 int preallocated
= 0;
3521 /* all form of preallocation discards first load group,
3522 * so the only competing code is preallocation use.
3523 * we don't need any locking here
3524 * notice we do NOT ignore preallocations with pa_deleted
3525 * otherwise we could leave used blocks available for
3526 * allocation in buddy when concurrent ext4_mb_put_pa()
3527 * is dropping preallocation
3529 list_for_each(cur
, &grp
->bb_prealloc_list
) {
3530 pa
= list_entry(cur
, struct ext4_prealloc_space
, pa_group_list
);
3531 spin_lock(&pa
->pa_lock
);
3532 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
3535 spin_unlock(&pa
->pa_lock
);
3536 if (unlikely(len
== 0))
3538 BUG_ON(groupnr
!= group
);
3539 ext4_set_bits(bitmap
, start
, len
);
3540 preallocated
+= len
;
3542 mb_debug(1, "preallocated %u for group %u\n", preallocated
, group
);
3545 static void ext4_mb_pa_callback(struct rcu_head
*head
)
3547 struct ext4_prealloc_space
*pa
;
3548 pa
= container_of(head
, struct ext4_prealloc_space
, u
.pa_rcu
);
3550 BUG_ON(atomic_read(&pa
->pa_count
));
3551 BUG_ON(pa
->pa_deleted
== 0);
3552 kmem_cache_free(ext4_pspace_cachep
, pa
);
3556 * drops a reference to preallocated space descriptor
3557 * if this was the last reference and the space is consumed
3559 static void ext4_mb_put_pa(struct ext4_allocation_context
*ac
,
3560 struct super_block
*sb
, struct ext4_prealloc_space
*pa
)
3563 ext4_fsblk_t grp_blk
;
3565 /* in this short window concurrent discard can set pa_deleted */
3566 spin_lock(&pa
->pa_lock
);
3567 if (!atomic_dec_and_test(&pa
->pa_count
) || pa
->pa_free
!= 0) {
3568 spin_unlock(&pa
->pa_lock
);
3572 if (pa
->pa_deleted
== 1) {
3573 spin_unlock(&pa
->pa_lock
);
3578 spin_unlock(&pa
->pa_lock
);
3580 grp_blk
= pa
->pa_pstart
;
3582 * If doing group-based preallocation, pa_pstart may be in the
3583 * next group when pa is used up
3585 if (pa
->pa_type
== MB_GROUP_PA
)
3588 grp
= ext4_get_group_number(sb
, grp_blk
);
3593 * P1 (buddy init) P2 (regular allocation)
3594 * find block B in PA
3595 * copy on-disk bitmap to buddy
3596 * mark B in on-disk bitmap
3597 * drop PA from group
3598 * mark all PAs in buddy
3600 * thus, P1 initializes buddy with B available. to prevent this
3601 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3604 ext4_lock_group(sb
, grp
);
3605 list_del(&pa
->pa_group_list
);
3606 ext4_unlock_group(sb
, grp
);
3608 spin_lock(pa
->pa_obj_lock
);
3609 list_del_rcu(&pa
->pa_inode_list
);
3610 spin_unlock(pa
->pa_obj_lock
);
3612 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3616 * creates new preallocated space for given inode
3618 static noinline_for_stack
int
3619 ext4_mb_new_inode_pa(struct ext4_allocation_context
*ac
)
3621 struct super_block
*sb
= ac
->ac_sb
;
3622 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3623 struct ext4_prealloc_space
*pa
;
3624 struct ext4_group_info
*grp
;
3625 struct ext4_inode_info
*ei
;
3627 /* preallocate only when found space is larger then requested */
3628 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3629 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3630 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3632 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3636 if (ac
->ac_b_ex
.fe_len
< ac
->ac_g_ex
.fe_len
) {
3642 /* we can't allocate as much as normalizer wants.
3643 * so, found space must get proper lstart
3644 * to cover original request */
3645 BUG_ON(ac
->ac_g_ex
.fe_logical
> ac
->ac_o_ex
.fe_logical
);
3646 BUG_ON(ac
->ac_g_ex
.fe_len
< ac
->ac_o_ex
.fe_len
);
3648 /* we're limited by original request in that
3649 * logical block must be covered any way
3650 * winl is window we can move our chunk within */
3651 winl
= ac
->ac_o_ex
.fe_logical
- ac
->ac_g_ex
.fe_logical
;
3653 /* also, we should cover whole original request */
3654 wins
= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
- ac
->ac_o_ex
.fe_len
);
3656 /* the smallest one defines real window */
3657 win
= min(winl
, wins
);
3659 offs
= ac
->ac_o_ex
.fe_logical
%
3660 EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
3661 if (offs
&& offs
< win
)
3664 ac
->ac_b_ex
.fe_logical
= ac
->ac_o_ex
.fe_logical
-
3665 EXT4_NUM_B2C(sbi
, win
);
3666 BUG_ON(ac
->ac_o_ex
.fe_logical
< ac
->ac_b_ex
.fe_logical
);
3667 BUG_ON(ac
->ac_o_ex
.fe_len
> ac
->ac_b_ex
.fe_len
);
3670 /* preallocation can change ac_b_ex, thus we store actually
3671 * allocated blocks for history */
3672 ac
->ac_f_ex
= ac
->ac_b_ex
;
3674 pa
->pa_lstart
= ac
->ac_b_ex
.fe_logical
;
3675 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3676 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3677 pa
->pa_free
= pa
->pa_len
;
3678 atomic_set(&pa
->pa_count
, 1);
3679 spin_lock_init(&pa
->pa_lock
);
3680 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3681 INIT_LIST_HEAD(&pa
->pa_group_list
);
3683 pa
->pa_type
= MB_INODE_PA
;
3685 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa
,
3686 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3687 trace_ext4_mb_new_inode_pa(ac
, pa
);
3689 ext4_mb_use_inode_pa(ac
, pa
);
3690 atomic_add(pa
->pa_free
, &sbi
->s_mb_preallocated
);
3692 ei
= EXT4_I(ac
->ac_inode
);
3693 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3695 pa
->pa_obj_lock
= &ei
->i_prealloc_lock
;
3696 pa
->pa_inode
= ac
->ac_inode
;
3698 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3699 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3700 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3702 spin_lock(pa
->pa_obj_lock
);
3703 list_add_rcu(&pa
->pa_inode_list
, &ei
->i_prealloc_list
);
3704 spin_unlock(pa
->pa_obj_lock
);
3710 * creates new preallocated space for locality group inodes belongs to
3712 static noinline_for_stack
int
3713 ext4_mb_new_group_pa(struct ext4_allocation_context
*ac
)
3715 struct super_block
*sb
= ac
->ac_sb
;
3716 struct ext4_locality_group
*lg
;
3717 struct ext4_prealloc_space
*pa
;
3718 struct ext4_group_info
*grp
;
3720 /* preallocate only when found space is larger then requested */
3721 BUG_ON(ac
->ac_o_ex
.fe_len
>= ac
->ac_b_ex
.fe_len
);
3722 BUG_ON(ac
->ac_status
!= AC_STATUS_FOUND
);
3723 BUG_ON(!S_ISREG(ac
->ac_inode
->i_mode
));
3725 BUG_ON(ext4_pspace_cachep
== NULL
);
3726 pa
= kmem_cache_alloc(ext4_pspace_cachep
, GFP_NOFS
);
3730 /* preallocation can change ac_b_ex, thus we store actually
3731 * allocated blocks for history */
3732 ac
->ac_f_ex
= ac
->ac_b_ex
;
3734 pa
->pa_pstart
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
3735 pa
->pa_lstart
= pa
->pa_pstart
;
3736 pa
->pa_len
= ac
->ac_b_ex
.fe_len
;
3737 pa
->pa_free
= pa
->pa_len
;
3738 atomic_set(&pa
->pa_count
, 1);
3739 spin_lock_init(&pa
->pa_lock
);
3740 INIT_LIST_HEAD(&pa
->pa_inode_list
);
3741 INIT_LIST_HEAD(&pa
->pa_group_list
);
3743 pa
->pa_type
= MB_GROUP_PA
;
3745 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa
,
3746 pa
->pa_pstart
, pa
->pa_len
, pa
->pa_lstart
);
3747 trace_ext4_mb_new_group_pa(ac
, pa
);
3749 ext4_mb_use_group_pa(ac
, pa
);
3750 atomic_add(pa
->pa_free
, &EXT4_SB(sb
)->s_mb_preallocated
);
3752 grp
= ext4_get_group_info(sb
, ac
->ac_b_ex
.fe_group
);
3756 pa
->pa_obj_lock
= &lg
->lg_prealloc_lock
;
3757 pa
->pa_inode
= NULL
;
3759 ext4_lock_group(sb
, ac
->ac_b_ex
.fe_group
);
3760 list_add(&pa
->pa_group_list
, &grp
->bb_prealloc_list
);
3761 ext4_unlock_group(sb
, ac
->ac_b_ex
.fe_group
);
3764 * We will later add the new pa to the right bucket
3765 * after updating the pa_free in ext4_mb_release_context
3770 static int ext4_mb_new_preallocation(struct ext4_allocation_context
*ac
)
3774 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
3775 err
= ext4_mb_new_group_pa(ac
);
3777 err
= ext4_mb_new_inode_pa(ac
);
3782 * finds all unused blocks in on-disk bitmap, frees them in
3783 * in-core bitmap and buddy.
3784 * @pa must be unlinked from inode and group lists, so that
3785 * nobody else can find/use it.
3786 * the caller MUST hold group/inode locks.
3787 * TODO: optimize the case when there are no in-core structures yet
3789 static noinline_for_stack
int
3790 ext4_mb_release_inode_pa(struct ext4_buddy
*e4b
, struct buffer_head
*bitmap_bh
,
3791 struct ext4_prealloc_space
*pa
)
3793 struct super_block
*sb
= e4b
->bd_sb
;
3794 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
3799 unsigned long long grp_blk_start
;
3803 BUG_ON(pa
->pa_deleted
== 0);
3804 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3805 grp_blk_start
= pa
->pa_pstart
- EXT4_C2B(sbi
, bit
);
3806 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3807 end
= bit
+ pa
->pa_len
;
3810 bit
= mb_find_next_zero_bit(bitmap_bh
->b_data
, end
, bit
);
3813 next
= mb_find_next_bit(bitmap_bh
->b_data
, end
, bit
);
3814 mb_debug(1, " free preallocated %u/%u in group %u\n",
3815 (unsigned) ext4_group_first_block_no(sb
, group
) + bit
,
3816 (unsigned) next
- bit
, (unsigned) group
);
3819 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, next
- bit
);
3820 trace_ext4_mb_release_inode_pa(pa
, (grp_blk_start
+
3821 EXT4_C2B(sbi
, bit
)),
3823 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, next
- bit
);
3826 if (free
!= pa
->pa_free
) {
3827 ext4_msg(e4b
->bd_sb
, KERN_CRIT
,
3828 "pa %p: logic %lu, phys. %lu, len %lu",
3829 pa
, (unsigned long) pa
->pa_lstart
,
3830 (unsigned long) pa
->pa_pstart
,
3831 (unsigned long) pa
->pa_len
);
3832 ext4_grp_locked_error(sb
, group
, 0, 0, "free %u, pa_free %u",
3835 * pa is already deleted so we use the value obtained
3836 * from the bitmap and continue.
3839 atomic_add(free
, &sbi
->s_mb_discarded
);
3844 static noinline_for_stack
int
3845 ext4_mb_release_group_pa(struct ext4_buddy
*e4b
,
3846 struct ext4_prealloc_space
*pa
)
3848 struct super_block
*sb
= e4b
->bd_sb
;
3852 trace_ext4_mb_release_group_pa(sb
, pa
);
3853 BUG_ON(pa
->pa_deleted
== 0);
3854 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
, &group
, &bit
);
3855 BUG_ON(group
!= e4b
->bd_group
&& pa
->pa_len
!= 0);
3856 mb_free_blocks(pa
->pa_inode
, e4b
, bit
, pa
->pa_len
);
3857 atomic_add(pa
->pa_len
, &EXT4_SB(sb
)->s_mb_discarded
);
3858 trace_ext4_mballoc_discard(sb
, NULL
, group
, bit
, pa
->pa_len
);
3864 * releases all preallocations in given group
3866 * first, we need to decide discard policy:
3867 * - when do we discard
3869 * - how many do we discard
3870 * 1) how many requested
3872 static noinline_for_stack
int
3873 ext4_mb_discard_group_preallocations(struct super_block
*sb
,
3874 ext4_group_t group
, int needed
)
3876 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, group
);
3877 struct buffer_head
*bitmap_bh
= NULL
;
3878 struct ext4_prealloc_space
*pa
, *tmp
;
3879 struct list_head list
;
3880 struct ext4_buddy e4b
;
3885 mb_debug(1, "discard preallocation for group %u\n", group
);
3887 if (list_empty(&grp
->bb_prealloc_list
))
3890 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
3891 if (IS_ERR(bitmap_bh
)) {
3892 err
= PTR_ERR(bitmap_bh
);
3893 ext4_error(sb
, "Error %d reading block bitmap for %u",
3898 err
= ext4_mb_load_buddy(sb
, group
, &e4b
);
3900 ext4_warning(sb
, "Error %d loading buddy information for %u",
3907 needed
= EXT4_CLUSTERS_PER_GROUP(sb
) + 1;
3909 INIT_LIST_HEAD(&list
);
3911 ext4_lock_group(sb
, group
);
3912 list_for_each_entry_safe(pa
, tmp
,
3913 &grp
->bb_prealloc_list
, pa_group_list
) {
3914 spin_lock(&pa
->pa_lock
);
3915 if (atomic_read(&pa
->pa_count
)) {
3916 spin_unlock(&pa
->pa_lock
);
3920 if (pa
->pa_deleted
) {
3921 spin_unlock(&pa
->pa_lock
);
3925 /* seems this one can be freed ... */
3928 /* we can trust pa_free ... */
3929 free
+= pa
->pa_free
;
3931 spin_unlock(&pa
->pa_lock
);
3933 list_del(&pa
->pa_group_list
);
3934 list_add(&pa
->u
.pa_tmp_list
, &list
);
3937 /* if we still need more blocks and some PAs were used, try again */
3938 if (free
< needed
&& busy
) {
3940 ext4_unlock_group(sb
, group
);
3945 /* found anything to free? */
3946 if (list_empty(&list
)) {
3951 /* now free all selected PAs */
3952 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
3954 /* remove from object (inode or locality group) */
3955 spin_lock(pa
->pa_obj_lock
);
3956 list_del_rcu(&pa
->pa_inode_list
);
3957 spin_unlock(pa
->pa_obj_lock
);
3959 if (pa
->pa_type
== MB_GROUP_PA
)
3960 ext4_mb_release_group_pa(&e4b
, pa
);
3962 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
3964 list_del(&pa
->u
.pa_tmp_list
);
3965 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
3969 ext4_unlock_group(sb
, group
);
3970 ext4_mb_unload_buddy(&e4b
);
3976 * releases all non-used preallocated blocks for given inode
3978 * It's important to discard preallocations under i_data_sem
3979 * We don't want another block to be served from the prealloc
3980 * space when we are discarding the inode prealloc space.
3982 * FIXME!! Make sure it is valid at all the call sites
3984 void ext4_discard_preallocations(struct inode
*inode
)
3986 struct ext4_inode_info
*ei
= EXT4_I(inode
);
3987 struct super_block
*sb
= inode
->i_sb
;
3988 struct buffer_head
*bitmap_bh
= NULL
;
3989 struct ext4_prealloc_space
*pa
, *tmp
;
3990 ext4_group_t group
= 0;
3991 struct list_head list
;
3992 struct ext4_buddy e4b
;
3995 if (!S_ISREG(inode
->i_mode
)) {
3996 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4000 mb_debug(1, "discard preallocation for inode %lu\n", inode
->i_ino
);
4001 trace_ext4_discard_preallocations(inode
);
4003 INIT_LIST_HEAD(&list
);
4006 /* first, collect all pa's in the inode */
4007 spin_lock(&ei
->i_prealloc_lock
);
4008 while (!list_empty(&ei
->i_prealloc_list
)) {
4009 pa
= list_entry(ei
->i_prealloc_list
.next
,
4010 struct ext4_prealloc_space
, pa_inode_list
);
4011 BUG_ON(pa
->pa_obj_lock
!= &ei
->i_prealloc_lock
);
4012 spin_lock(&pa
->pa_lock
);
4013 if (atomic_read(&pa
->pa_count
)) {
4014 /* this shouldn't happen often - nobody should
4015 * use preallocation while we're discarding it */
4016 spin_unlock(&pa
->pa_lock
);
4017 spin_unlock(&ei
->i_prealloc_lock
);
4018 ext4_msg(sb
, KERN_ERR
,
4019 "uh-oh! used pa while discarding");
4021 schedule_timeout_uninterruptible(HZ
);
4025 if (pa
->pa_deleted
== 0) {
4027 spin_unlock(&pa
->pa_lock
);
4028 list_del_rcu(&pa
->pa_inode_list
);
4029 list_add(&pa
->u
.pa_tmp_list
, &list
);
4033 /* someone is deleting pa right now */
4034 spin_unlock(&pa
->pa_lock
);
4035 spin_unlock(&ei
->i_prealloc_lock
);
4037 /* we have to wait here because pa_deleted
4038 * doesn't mean pa is already unlinked from
4039 * the list. as we might be called from
4040 * ->clear_inode() the inode will get freed
4041 * and concurrent thread which is unlinking
4042 * pa from inode's list may access already
4043 * freed memory, bad-bad-bad */
4045 /* XXX: if this happens too often, we can
4046 * add a flag to force wait only in case
4047 * of ->clear_inode(), but not in case of
4048 * regular truncate */
4049 schedule_timeout_uninterruptible(HZ
);
4052 spin_unlock(&ei
->i_prealloc_lock
);
4054 list_for_each_entry_safe(pa
, tmp
, &list
, u
.pa_tmp_list
) {
4055 BUG_ON(pa
->pa_type
!= MB_INODE_PA
);
4056 group
= ext4_get_group_number(sb
, pa
->pa_pstart
);
4058 err
= ext4_mb_load_buddy_gfp(sb
, group
, &e4b
,
4059 GFP_NOFS
|__GFP_NOFAIL
);
4061 ext4_error(sb
, "Error %d loading buddy information for %u",
4066 bitmap_bh
= ext4_read_block_bitmap(sb
, group
);
4067 if (IS_ERR(bitmap_bh
)) {
4068 err
= PTR_ERR(bitmap_bh
);
4069 ext4_error(sb
, "Error %d reading block bitmap for %u",
4071 ext4_mb_unload_buddy(&e4b
);
4075 ext4_lock_group(sb
, group
);
4076 list_del(&pa
->pa_group_list
);
4077 ext4_mb_release_inode_pa(&e4b
, bitmap_bh
, pa
);
4078 ext4_unlock_group(sb
, group
);
4080 ext4_mb_unload_buddy(&e4b
);
4083 list_del(&pa
->u
.pa_tmp_list
);
4084 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4088 #ifdef CONFIG_EXT4_DEBUG
4089 static void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4091 struct super_block
*sb
= ac
->ac_sb
;
4092 ext4_group_t ngroups
, i
;
4094 if (!ext4_mballoc_debug
||
4095 (EXT4_SB(sb
)->s_mount_flags
& EXT4_MF_FS_ABORTED
))
4098 ext4_msg(ac
->ac_sb
, KERN_ERR
, "Can't allocate:"
4099 " Allocation context details:");
4100 ext4_msg(ac
->ac_sb
, KERN_ERR
, "status %d flags %d",
4101 ac
->ac_status
, ac
->ac_flags
);
4102 ext4_msg(ac
->ac_sb
, KERN_ERR
, "orig %lu/%lu/%lu@%lu, "
4103 "goal %lu/%lu/%lu@%lu, "
4104 "best %lu/%lu/%lu@%lu cr %d",
4105 (unsigned long)ac
->ac_o_ex
.fe_group
,
4106 (unsigned long)ac
->ac_o_ex
.fe_start
,
4107 (unsigned long)ac
->ac_o_ex
.fe_len
,
4108 (unsigned long)ac
->ac_o_ex
.fe_logical
,
4109 (unsigned long)ac
->ac_g_ex
.fe_group
,
4110 (unsigned long)ac
->ac_g_ex
.fe_start
,
4111 (unsigned long)ac
->ac_g_ex
.fe_len
,
4112 (unsigned long)ac
->ac_g_ex
.fe_logical
,
4113 (unsigned long)ac
->ac_b_ex
.fe_group
,
4114 (unsigned long)ac
->ac_b_ex
.fe_start
,
4115 (unsigned long)ac
->ac_b_ex
.fe_len
,
4116 (unsigned long)ac
->ac_b_ex
.fe_logical
,
4117 (int)ac
->ac_criteria
);
4118 ext4_msg(ac
->ac_sb
, KERN_ERR
, "%d found", ac
->ac_found
);
4119 ext4_msg(ac
->ac_sb
, KERN_ERR
, "groups: ");
4120 ngroups
= ext4_get_groups_count(sb
);
4121 for (i
= 0; i
< ngroups
; i
++) {
4122 struct ext4_group_info
*grp
= ext4_get_group_info(sb
, i
);
4123 struct ext4_prealloc_space
*pa
;
4124 ext4_grpblk_t start
;
4125 struct list_head
*cur
;
4126 ext4_lock_group(sb
, i
);
4127 list_for_each(cur
, &grp
->bb_prealloc_list
) {
4128 pa
= list_entry(cur
, struct ext4_prealloc_space
,
4130 spin_lock(&pa
->pa_lock
);
4131 ext4_get_group_no_and_offset(sb
, pa
->pa_pstart
,
4133 spin_unlock(&pa
->pa_lock
);
4134 printk(KERN_ERR
"PA:%u:%d:%u \n", i
,
4137 ext4_unlock_group(sb
, i
);
4139 if (grp
->bb_free
== 0)
4141 printk(KERN_ERR
"%u: %d/%d \n",
4142 i
, grp
->bb_free
, grp
->bb_fragments
);
4144 printk(KERN_ERR
"\n");
4147 static inline void ext4_mb_show_ac(struct ext4_allocation_context
*ac
)
4154 * We use locality group preallocation for small size file. The size of the
4155 * file is determined by the current size or the resulting size after
4156 * allocation which ever is larger
4158 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4160 static void ext4_mb_group_or_file(struct ext4_allocation_context
*ac
)
4162 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4163 int bsbits
= ac
->ac_sb
->s_blocksize_bits
;
4166 if (!(ac
->ac_flags
& EXT4_MB_HINT_DATA
))
4169 if (unlikely(ac
->ac_flags
& EXT4_MB_HINT_GOAL_ONLY
))
4172 size
= ac
->ac_o_ex
.fe_logical
+ EXT4_C2B(sbi
, ac
->ac_o_ex
.fe_len
);
4173 isize
= (i_size_read(ac
->ac_inode
) + ac
->ac_sb
->s_blocksize
- 1)
4176 if ((size
== isize
) &&
4177 !ext4_fs_is_busy(sbi
) &&
4178 (atomic_read(&ac
->ac_inode
->i_writecount
) == 0)) {
4179 ac
->ac_flags
|= EXT4_MB_HINT_NOPREALLOC
;
4183 if (sbi
->s_mb_group_prealloc
<= 0) {
4184 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4188 /* don't use group allocation for large files */
4189 size
= max(size
, isize
);
4190 if (size
> sbi
->s_mb_stream_request
) {
4191 ac
->ac_flags
|= EXT4_MB_STREAM_ALLOC
;
4195 BUG_ON(ac
->ac_lg
!= NULL
);
4197 * locality group prealloc space are per cpu. The reason for having
4198 * per cpu locality group is to reduce the contention between block
4199 * request from multiple CPUs.
4201 ac
->ac_lg
= raw_cpu_ptr(sbi
->s_locality_groups
);
4203 /* we're going to use group allocation */
4204 ac
->ac_flags
|= EXT4_MB_HINT_GROUP_ALLOC
;
4206 /* serialize all allocations in the group */
4207 mutex_lock(&ac
->ac_lg
->lg_mutex
);
4210 static noinline_for_stack
int
4211 ext4_mb_initialize_context(struct ext4_allocation_context
*ac
,
4212 struct ext4_allocation_request
*ar
)
4214 struct super_block
*sb
= ar
->inode
->i_sb
;
4215 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4216 struct ext4_super_block
*es
= sbi
->s_es
;
4220 ext4_grpblk_t block
;
4222 /* we can't allocate > group size */
4225 /* just a dirty hack to filter too big requests */
4226 if (len
>= EXT4_CLUSTERS_PER_GROUP(sb
))
4227 len
= EXT4_CLUSTERS_PER_GROUP(sb
);
4229 /* start searching from the goal */
4231 if (goal
< le32_to_cpu(es
->s_first_data_block
) ||
4232 goal
>= ext4_blocks_count(es
))
4233 goal
= le32_to_cpu(es
->s_first_data_block
);
4234 ext4_get_group_no_and_offset(sb
, goal
, &group
, &block
);
4236 /* set up allocation goals */
4237 ac
->ac_b_ex
.fe_logical
= EXT4_LBLK_CMASK(sbi
, ar
->logical
);
4238 ac
->ac_status
= AC_STATUS_CONTINUE
;
4240 ac
->ac_inode
= ar
->inode
;
4241 ac
->ac_o_ex
.fe_logical
= ac
->ac_b_ex
.fe_logical
;
4242 ac
->ac_o_ex
.fe_group
= group
;
4243 ac
->ac_o_ex
.fe_start
= block
;
4244 ac
->ac_o_ex
.fe_len
= len
;
4245 ac
->ac_g_ex
= ac
->ac_o_ex
;
4246 ac
->ac_flags
= ar
->flags
;
4248 /* we have to define context: we'll we work with a file or
4249 * locality group. this is a policy, actually */
4250 ext4_mb_group_or_file(ac
);
4252 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4253 "left: %u/%u, right %u/%u to %swritable\n",
4254 (unsigned) ar
->len
, (unsigned) ar
->logical
,
4255 (unsigned) ar
->goal
, ac
->ac_flags
, ac
->ac_2order
,
4256 (unsigned) ar
->lleft
, (unsigned) ar
->pleft
,
4257 (unsigned) ar
->lright
, (unsigned) ar
->pright
,
4258 atomic_read(&ar
->inode
->i_writecount
) ? "" : "non-");
4263 static noinline_for_stack
void
4264 ext4_mb_discard_lg_preallocations(struct super_block
*sb
,
4265 struct ext4_locality_group
*lg
,
4266 int order
, int total_entries
)
4268 ext4_group_t group
= 0;
4269 struct ext4_buddy e4b
;
4270 struct list_head discard_list
;
4271 struct ext4_prealloc_space
*pa
, *tmp
;
4273 mb_debug(1, "discard locality group preallocation\n");
4275 INIT_LIST_HEAD(&discard_list
);
4277 spin_lock(&lg
->lg_prealloc_lock
);
4278 list_for_each_entry_rcu(pa
, &lg
->lg_prealloc_list
[order
],
4280 spin_lock(&pa
->pa_lock
);
4281 if (atomic_read(&pa
->pa_count
)) {
4283 * This is the pa that we just used
4284 * for block allocation. So don't
4287 spin_unlock(&pa
->pa_lock
);
4290 if (pa
->pa_deleted
) {
4291 spin_unlock(&pa
->pa_lock
);
4294 /* only lg prealloc space */
4295 BUG_ON(pa
->pa_type
!= MB_GROUP_PA
);
4297 /* seems this one can be freed ... */
4299 spin_unlock(&pa
->pa_lock
);
4301 list_del_rcu(&pa
->pa_inode_list
);
4302 list_add(&pa
->u
.pa_tmp_list
, &discard_list
);
4305 if (total_entries
<= 5) {
4307 * we want to keep only 5 entries
4308 * allowing it to grow to 8. This
4309 * mak sure we don't call discard
4310 * soon for this list.
4315 spin_unlock(&lg
->lg_prealloc_lock
);
4317 list_for_each_entry_safe(pa
, tmp
, &discard_list
, u
.pa_tmp_list
) {
4320 group
= ext4_get_group_number(sb
, pa
->pa_pstart
);
4321 err
= ext4_mb_load_buddy_gfp(sb
, group
, &e4b
,
4322 GFP_NOFS
|__GFP_NOFAIL
);
4324 ext4_error(sb
, "Error %d loading buddy information for %u",
4328 ext4_lock_group(sb
, group
);
4329 list_del(&pa
->pa_group_list
);
4330 ext4_mb_release_group_pa(&e4b
, pa
);
4331 ext4_unlock_group(sb
, group
);
4333 ext4_mb_unload_buddy(&e4b
);
4334 list_del(&pa
->u
.pa_tmp_list
);
4335 call_rcu(&(pa
)->u
.pa_rcu
, ext4_mb_pa_callback
);
4340 * We have incremented pa_count. So it cannot be freed at this
4341 * point. Also we hold lg_mutex. So no parallel allocation is
4342 * possible from this lg. That means pa_free cannot be updated.
4344 * A parallel ext4_mb_discard_group_preallocations is possible.
4345 * which can cause the lg_prealloc_list to be updated.
4348 static void ext4_mb_add_n_trim(struct ext4_allocation_context
*ac
)
4350 int order
, added
= 0, lg_prealloc_count
= 1;
4351 struct super_block
*sb
= ac
->ac_sb
;
4352 struct ext4_locality_group
*lg
= ac
->ac_lg
;
4353 struct ext4_prealloc_space
*tmp_pa
, *pa
= ac
->ac_pa
;
4355 order
= fls(pa
->pa_free
) - 1;
4356 if (order
> PREALLOC_TB_SIZE
- 1)
4357 /* The max size of hash table is PREALLOC_TB_SIZE */
4358 order
= PREALLOC_TB_SIZE
- 1;
4359 /* Add the prealloc space to lg */
4360 spin_lock(&lg
->lg_prealloc_lock
);
4361 list_for_each_entry_rcu(tmp_pa
, &lg
->lg_prealloc_list
[order
],
4363 spin_lock(&tmp_pa
->pa_lock
);
4364 if (tmp_pa
->pa_deleted
) {
4365 spin_unlock(&tmp_pa
->pa_lock
);
4368 if (!added
&& pa
->pa_free
< tmp_pa
->pa_free
) {
4369 /* Add to the tail of the previous entry */
4370 list_add_tail_rcu(&pa
->pa_inode_list
,
4371 &tmp_pa
->pa_inode_list
);
4374 * we want to count the total
4375 * number of entries in the list
4378 spin_unlock(&tmp_pa
->pa_lock
);
4379 lg_prealloc_count
++;
4382 list_add_tail_rcu(&pa
->pa_inode_list
,
4383 &lg
->lg_prealloc_list
[order
]);
4384 spin_unlock(&lg
->lg_prealloc_lock
);
4386 /* Now trim the list to be not more than 8 elements */
4387 if (lg_prealloc_count
> 8) {
4388 ext4_mb_discard_lg_preallocations(sb
, lg
,
4389 order
, lg_prealloc_count
);
4396 * release all resource we used in allocation
4398 static int ext4_mb_release_context(struct ext4_allocation_context
*ac
)
4400 struct ext4_sb_info
*sbi
= EXT4_SB(ac
->ac_sb
);
4401 struct ext4_prealloc_space
*pa
= ac
->ac_pa
;
4403 if (pa
->pa_type
== MB_GROUP_PA
) {
4404 /* see comment in ext4_mb_use_group_pa() */
4405 spin_lock(&pa
->pa_lock
);
4406 pa
->pa_pstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4407 pa
->pa_lstart
+= EXT4_C2B(sbi
, ac
->ac_b_ex
.fe_len
);
4408 pa
->pa_free
-= ac
->ac_b_ex
.fe_len
;
4409 pa
->pa_len
-= ac
->ac_b_ex
.fe_len
;
4410 spin_unlock(&pa
->pa_lock
);
4415 * We want to add the pa to the right bucket.
4416 * Remove it from the list and while adding
4417 * make sure the list to which we are adding
4420 if ((pa
->pa_type
== MB_GROUP_PA
) && likely(pa
->pa_free
)) {
4421 spin_lock(pa
->pa_obj_lock
);
4422 list_del_rcu(&pa
->pa_inode_list
);
4423 spin_unlock(pa
->pa_obj_lock
);
4424 ext4_mb_add_n_trim(ac
);
4426 ext4_mb_put_pa(ac
, ac
->ac_sb
, pa
);
4428 if (ac
->ac_bitmap_page
)
4429 put_page(ac
->ac_bitmap_page
);
4430 if (ac
->ac_buddy_page
)
4431 put_page(ac
->ac_buddy_page
);
4432 if (ac
->ac_flags
& EXT4_MB_HINT_GROUP_ALLOC
)
4433 mutex_unlock(&ac
->ac_lg
->lg_mutex
);
4434 ext4_mb_collect_stats(ac
);
4438 static int ext4_mb_discard_preallocations(struct super_block
*sb
, int needed
)
4440 ext4_group_t i
, ngroups
= ext4_get_groups_count(sb
);
4444 trace_ext4_mb_discard_preallocations(sb
, needed
);
4445 for (i
= 0; i
< ngroups
&& needed
> 0; i
++) {
4446 ret
= ext4_mb_discard_group_preallocations(sb
, i
, needed
);
4455 * Main entry point into mballoc to allocate blocks
4456 * it tries to use preallocation first, then falls back
4457 * to usual allocation
4459 ext4_fsblk_t
ext4_mb_new_blocks(handle_t
*handle
,
4460 struct ext4_allocation_request
*ar
, int *errp
)
4463 struct ext4_allocation_context
*ac
= NULL
;
4464 struct ext4_sb_info
*sbi
;
4465 struct super_block
*sb
;
4466 ext4_fsblk_t block
= 0;
4467 unsigned int inquota
= 0;
4468 unsigned int reserv_clstrs
= 0;
4471 sb
= ar
->inode
->i_sb
;
4474 trace_ext4_request_blocks(ar
);
4476 /* Allow to use superuser reservation for quota file */
4477 if (ext4_is_quota_file(ar
->inode
))
4478 ar
->flags
|= EXT4_MB_USE_ROOT_BLOCKS
;
4480 if ((ar
->flags
& EXT4_MB_DELALLOC_RESERVED
) == 0) {
4481 /* Without delayed allocation we need to verify
4482 * there is enough free blocks to do block allocation
4483 * and verify allocation doesn't exceed the quota limits.
4486 ext4_claim_free_clusters(sbi
, ar
->len
, ar
->flags
)) {
4488 /* let others to free the space */
4490 ar
->len
= ar
->len
>> 1;
4496 reserv_clstrs
= ar
->len
;
4497 if (ar
->flags
& EXT4_MB_USE_ROOT_BLOCKS
) {
4498 dquot_alloc_block_nofail(ar
->inode
,
4499 EXT4_C2B(sbi
, ar
->len
));
4502 dquot_alloc_block(ar
->inode
,
4503 EXT4_C2B(sbi
, ar
->len
))) {
4505 ar
->flags
|= EXT4_MB_HINT_NOPREALLOC
;
4516 ac
= kmem_cache_zalloc(ext4_ac_cachep
, GFP_NOFS
);
4523 *errp
= ext4_mb_initialize_context(ac
, ar
);
4529 ac
->ac_op
= EXT4_MB_HISTORY_PREALLOC
;
4530 if (!ext4_mb_use_preallocated(ac
)) {
4531 ac
->ac_op
= EXT4_MB_HISTORY_ALLOC
;
4532 ext4_mb_normalize_request(ac
, ar
);
4534 /* allocate space in core */
4535 *errp
= ext4_mb_regular_allocator(ac
);
4537 goto discard_and_exit
;
4539 /* as we've just preallocated more space than
4540 * user requested originally, we store allocated
4541 * space in a special descriptor */
4542 if (ac
->ac_status
== AC_STATUS_FOUND
&&
4543 ac
->ac_o_ex
.fe_len
< ac
->ac_b_ex
.fe_len
)
4544 *errp
= ext4_mb_new_preallocation(ac
);
4547 ext4_discard_allocated_blocks(ac
);
4551 if (likely(ac
->ac_status
== AC_STATUS_FOUND
)) {
4552 *errp
= ext4_mb_mark_diskspace_used(ac
, handle
, reserv_clstrs
);
4554 ext4_discard_allocated_blocks(ac
);
4557 block
= ext4_grp_offs_to_block(sb
, &ac
->ac_b_ex
);
4558 ar
->len
= ac
->ac_b_ex
.fe_len
;
4561 freed
= ext4_mb_discard_preallocations(sb
, ac
->ac_o_ex
.fe_len
);
4569 ac
->ac_b_ex
.fe_len
= 0;
4571 ext4_mb_show_ac(ac
);
4573 ext4_mb_release_context(ac
);
4576 kmem_cache_free(ext4_ac_cachep
, ac
);
4577 if (inquota
&& ar
->len
< inquota
)
4578 dquot_free_block(ar
->inode
, EXT4_C2B(sbi
, inquota
- ar
->len
));
4580 if ((ar
->flags
& EXT4_MB_DELALLOC_RESERVED
) == 0)
4581 /* release all the reserved blocks if non delalloc */
4582 percpu_counter_sub(&sbi
->s_dirtyclusters_counter
,
4586 trace_ext4_allocate_blocks(ar
, (unsigned long long)block
);
4592 * We can merge two free data extents only if the physical blocks
4593 * are contiguous, AND the extents were freed by the same transaction,
4594 * AND the blocks are associated with the same group.
4596 static void ext4_try_merge_freed_extent(struct ext4_sb_info
*sbi
,
4597 struct ext4_free_data
*entry
,
4598 struct ext4_free_data
*new_entry
,
4599 struct rb_root
*entry_rb_root
)
4601 if ((entry
->efd_tid
!= new_entry
->efd_tid
) ||
4602 (entry
->efd_group
!= new_entry
->efd_group
))
4604 if (entry
->efd_start_cluster
+ entry
->efd_count
==
4605 new_entry
->efd_start_cluster
) {
4606 new_entry
->efd_start_cluster
= entry
->efd_start_cluster
;
4607 new_entry
->efd_count
+= entry
->efd_count
;
4608 } else if (new_entry
->efd_start_cluster
+ new_entry
->efd_count
==
4609 entry
->efd_start_cluster
) {
4610 new_entry
->efd_count
+= entry
->efd_count
;
4613 spin_lock(&sbi
->s_md_lock
);
4614 list_del(&entry
->efd_list
);
4615 spin_unlock(&sbi
->s_md_lock
);
4616 rb_erase(&entry
->efd_node
, entry_rb_root
);
4617 kmem_cache_free(ext4_free_data_cachep
, entry
);
4620 static noinline_for_stack
int
4621 ext4_mb_free_metadata(handle_t
*handle
, struct ext4_buddy
*e4b
,
4622 struct ext4_free_data
*new_entry
)
4624 ext4_group_t group
= e4b
->bd_group
;
4625 ext4_grpblk_t cluster
;
4626 ext4_grpblk_t clusters
= new_entry
->efd_count
;
4627 struct ext4_free_data
*entry
;
4628 struct ext4_group_info
*db
= e4b
->bd_info
;
4629 struct super_block
*sb
= e4b
->bd_sb
;
4630 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4631 struct rb_node
**n
= &db
->bb_free_root
.rb_node
, *node
;
4632 struct rb_node
*parent
= NULL
, *new_node
;
4634 BUG_ON(!ext4_handle_valid(handle
));
4635 BUG_ON(e4b
->bd_bitmap_page
== NULL
);
4636 BUG_ON(e4b
->bd_buddy_page
== NULL
);
4638 new_node
= &new_entry
->efd_node
;
4639 cluster
= new_entry
->efd_start_cluster
;
4642 /* first free block exent. We need to
4643 protect buddy cache from being freed,
4644 * otherwise we'll refresh it from
4645 * on-disk bitmap and lose not-yet-available
4647 get_page(e4b
->bd_buddy_page
);
4648 get_page(e4b
->bd_bitmap_page
);
4652 entry
= rb_entry(parent
, struct ext4_free_data
, efd_node
);
4653 if (cluster
< entry
->efd_start_cluster
)
4655 else if (cluster
>= (entry
->efd_start_cluster
+ entry
->efd_count
))
4656 n
= &(*n
)->rb_right
;
4658 ext4_grp_locked_error(sb
, group
, 0,
4659 ext4_group_first_block_no(sb
, group
) +
4660 EXT4_C2B(sbi
, cluster
),
4661 "Block already on to-be-freed list");
4666 rb_link_node(new_node
, parent
, n
);
4667 rb_insert_color(new_node
, &db
->bb_free_root
);
4669 /* Now try to see the extent can be merged to left and right */
4670 node
= rb_prev(new_node
);
4672 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4673 ext4_try_merge_freed_extent(sbi
, entry
, new_entry
,
4674 &(db
->bb_free_root
));
4677 node
= rb_next(new_node
);
4679 entry
= rb_entry(node
, struct ext4_free_data
, efd_node
);
4680 ext4_try_merge_freed_extent(sbi
, entry
, new_entry
,
4681 &(db
->bb_free_root
));
4684 spin_lock(&sbi
->s_md_lock
);
4685 list_add_tail(&new_entry
->efd_list
, &sbi
->s_freed_data_list
);
4686 sbi
->s_mb_free_pending
+= clusters
;
4687 spin_unlock(&sbi
->s_md_lock
);
4692 * ext4_free_blocks() -- Free given blocks and update quota
4693 * @handle: handle for this transaction
4695 * @block: start physical block to free
4696 * @count: number of blocks to count
4697 * @flags: flags used by ext4_free_blocks
4699 void ext4_free_blocks(handle_t
*handle
, struct inode
*inode
,
4700 struct buffer_head
*bh
, ext4_fsblk_t block
,
4701 unsigned long count
, int flags
)
4703 struct buffer_head
*bitmap_bh
= NULL
;
4704 struct super_block
*sb
= inode
->i_sb
;
4705 struct ext4_group_desc
*gdp
;
4706 unsigned int overflow
;
4708 struct buffer_head
*gd_bh
;
4709 ext4_group_t block_group
;
4710 struct ext4_sb_info
*sbi
;
4711 struct ext4_buddy e4b
;
4712 unsigned int count_clusters
;
4719 BUG_ON(block
!= bh
->b_blocknr
);
4721 block
= bh
->b_blocknr
;
4725 if (!(flags
& EXT4_FREE_BLOCKS_VALIDATED
) &&
4726 !ext4_data_block_valid(sbi
, block
, count
)) {
4727 ext4_error(sb
, "Freeing blocks not in datazone - "
4728 "block = %llu, count = %lu", block
, count
);
4732 ext4_debug("freeing block %llu\n", block
);
4733 trace_ext4_free_blocks(inode
, block
, count
, flags
);
4735 if (bh
&& (flags
& EXT4_FREE_BLOCKS_FORGET
)) {
4738 ext4_forget(handle
, flags
& EXT4_FREE_BLOCKS_METADATA
,
4743 * If the extent to be freed does not begin on a cluster
4744 * boundary, we need to deal with partial clusters at the
4745 * beginning and end of the extent. Normally we will free
4746 * blocks at the beginning or the end unless we are explicitly
4747 * requested to avoid doing so.
4749 overflow
= EXT4_PBLK_COFF(sbi
, block
);
4751 if (flags
& EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER
) {
4752 overflow
= sbi
->s_cluster_ratio
- overflow
;
4754 if (count
> overflow
)
4763 overflow
= EXT4_LBLK_COFF(sbi
, count
);
4765 if (flags
& EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER
) {
4766 if (count
> overflow
)
4771 count
+= sbi
->s_cluster_ratio
- overflow
;
4774 if (!bh
&& (flags
& EXT4_FREE_BLOCKS_FORGET
)) {
4776 int is_metadata
= flags
& EXT4_FREE_BLOCKS_METADATA
;
4778 for (i
= 0; i
< count
; i
++) {
4781 bh
= sb_find_get_block(inode
->i_sb
, block
+ i
);
4782 ext4_forget(handle
, is_metadata
, inode
, bh
, block
+ i
);
4788 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4790 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4791 ext4_get_group_info(sb
, block_group
))))
4795 * Check to see if we are freeing blocks across a group
4798 if (EXT4_C2B(sbi
, bit
) + count
> EXT4_BLOCKS_PER_GROUP(sb
)) {
4799 overflow
= EXT4_C2B(sbi
, bit
) + count
-
4800 EXT4_BLOCKS_PER_GROUP(sb
);
4803 count_clusters
= EXT4_NUM_B2C(sbi
, count
);
4804 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4805 if (IS_ERR(bitmap_bh
)) {
4806 err
= PTR_ERR(bitmap_bh
);
4810 gdp
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4816 if (in_range(ext4_block_bitmap(sb
, gdp
), block
, count
) ||
4817 in_range(ext4_inode_bitmap(sb
, gdp
), block
, count
) ||
4818 in_range(block
, ext4_inode_table(sb
, gdp
),
4819 sbi
->s_itb_per_group
) ||
4820 in_range(block
+ count
- 1, ext4_inode_table(sb
, gdp
),
4821 sbi
->s_itb_per_group
)) {
4823 ext4_error(sb
, "Freeing blocks in system zone - "
4824 "Block = %llu, count = %lu", block
, count
);
4825 /* err = 0. ext4_std_error should be a no op */
4829 BUFFER_TRACE(bitmap_bh
, "getting write access");
4830 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
4835 * We are about to modify some metadata. Call the journal APIs
4836 * to unshare ->b_data if a currently-committing transaction is
4839 BUFFER_TRACE(gd_bh
, "get_write_access");
4840 err
= ext4_journal_get_write_access(handle
, gd_bh
);
4843 #ifdef AGGRESSIVE_CHECK
4846 for (i
= 0; i
< count_clusters
; i
++)
4847 BUG_ON(!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
));
4850 trace_ext4_mballoc_free(sb
, inode
, block_group
, bit
, count_clusters
);
4852 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
4853 err
= ext4_mb_load_buddy_gfp(sb
, block_group
, &e4b
,
4854 GFP_NOFS
|__GFP_NOFAIL
);
4859 * We need to make sure we don't reuse the freed block until after the
4860 * transaction is committed. We make an exception if the inode is to be
4861 * written in writeback mode since writeback mode has weak data
4862 * consistency guarantees.
4864 if (ext4_handle_valid(handle
) &&
4865 ((flags
& EXT4_FREE_BLOCKS_METADATA
) ||
4866 !ext4_should_writeback_data(inode
))) {
4867 struct ext4_free_data
*new_entry
;
4869 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4872 new_entry
= kmem_cache_alloc(ext4_free_data_cachep
,
4873 GFP_NOFS
|__GFP_NOFAIL
);
4874 new_entry
->efd_start_cluster
= bit
;
4875 new_entry
->efd_group
= block_group
;
4876 new_entry
->efd_count
= count_clusters
;
4877 new_entry
->efd_tid
= handle
->h_transaction
->t_tid
;
4879 ext4_lock_group(sb
, block_group
);
4880 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4881 ext4_mb_free_metadata(handle
, &e4b
, new_entry
);
4883 /* need to update group_info->bb_free and bitmap
4884 * with group lock held. generate_buddy look at
4885 * them with group lock_held
4887 if (test_opt(sb
, DISCARD
)) {
4888 err
= ext4_issue_discard(sb
, block_group
, bit
, count
,
4890 if (err
&& err
!= -EOPNOTSUPP
)
4891 ext4_msg(sb
, KERN_WARNING
, "discard request in"
4892 " group:%d block:%d count:%lu failed"
4893 " with %d", block_group
, bit
, count
,
4896 EXT4_MB_GRP_CLEAR_TRIMMED(e4b
.bd_info
);
4898 ext4_lock_group(sb
, block_group
);
4899 mb_clear_bits(bitmap_bh
->b_data
, bit
, count_clusters
);
4900 mb_free_blocks(inode
, &e4b
, bit
, count_clusters
);
4903 ret
= ext4_free_group_clusters(sb
, gdp
) + count_clusters
;
4904 ext4_free_group_clusters_set(sb
, gdp
, ret
);
4905 ext4_block_bitmap_csum_set(sb
, block_group
, gdp
, bitmap_bh
);
4906 ext4_group_desc_csum_set(sb
, block_group
, gdp
);
4907 ext4_unlock_group(sb
, block_group
);
4909 if (sbi
->s_log_groups_per_flex
) {
4910 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
4911 atomic64_add(count_clusters
,
4912 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
4915 if (!(flags
& EXT4_FREE_BLOCKS_NO_QUOT_UPDATE
))
4916 dquot_free_block(inode
, EXT4_C2B(sbi
, count_clusters
));
4917 percpu_counter_add(&sbi
->s_freeclusters_counter
, count_clusters
);
4919 ext4_mb_unload_buddy(&e4b
);
4921 /* We dirtied the bitmap block */
4922 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
4923 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
4925 /* And the group descriptor block */
4926 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
4927 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
4931 if (overflow
&& !err
) {
4939 ext4_std_error(sb
, err
);
4944 * ext4_group_add_blocks() -- Add given blocks to an existing group
4945 * @handle: handle to this transaction
4947 * @block: start physical block to add to the block group
4948 * @count: number of blocks to free
4950 * This marks the blocks as free in the bitmap and buddy.
4952 int ext4_group_add_blocks(handle_t
*handle
, struct super_block
*sb
,
4953 ext4_fsblk_t block
, unsigned long count
)
4955 struct buffer_head
*bitmap_bh
= NULL
;
4956 struct buffer_head
*gd_bh
;
4957 ext4_group_t block_group
;
4960 struct ext4_group_desc
*desc
;
4961 struct ext4_sb_info
*sbi
= EXT4_SB(sb
);
4962 struct ext4_buddy e4b
;
4963 int err
= 0, ret
, free_clusters_count
;
4964 ext4_grpblk_t clusters_freed
;
4965 ext4_fsblk_t first_cluster
= EXT4_B2C(sbi
, block
);
4966 ext4_fsblk_t last_cluster
= EXT4_B2C(sbi
, block
+ count
- 1);
4967 unsigned long cluster_count
= last_cluster
- first_cluster
+ 1;
4969 ext4_debug("Adding block(s) %llu-%llu\n", block
, block
+ count
- 1);
4974 ext4_get_group_no_and_offset(sb
, block
, &block_group
, &bit
);
4976 * Check to see if we are freeing blocks across a group
4979 if (bit
+ cluster_count
> EXT4_CLUSTERS_PER_GROUP(sb
)) {
4980 ext4_warning(sb
, "too many blocks added to group %u",
4986 bitmap_bh
= ext4_read_block_bitmap(sb
, block_group
);
4987 if (IS_ERR(bitmap_bh
)) {
4988 err
= PTR_ERR(bitmap_bh
);
4993 desc
= ext4_get_group_desc(sb
, block_group
, &gd_bh
);
4999 if (in_range(ext4_block_bitmap(sb
, desc
), block
, count
) ||
5000 in_range(ext4_inode_bitmap(sb
, desc
), block
, count
) ||
5001 in_range(block
, ext4_inode_table(sb
, desc
), sbi
->s_itb_per_group
) ||
5002 in_range(block
+ count
- 1, ext4_inode_table(sb
, desc
),
5003 sbi
->s_itb_per_group
)) {
5004 ext4_error(sb
, "Adding blocks in system zones - "
5005 "Block = %llu, count = %lu",
5011 BUFFER_TRACE(bitmap_bh
, "getting write access");
5012 err
= ext4_journal_get_write_access(handle
, bitmap_bh
);
5017 * We are about to modify some metadata. Call the journal APIs
5018 * to unshare ->b_data if a currently-committing transaction is
5021 BUFFER_TRACE(gd_bh
, "get_write_access");
5022 err
= ext4_journal_get_write_access(handle
, gd_bh
);
5026 for (i
= 0, clusters_freed
= 0; i
< cluster_count
; i
++) {
5027 BUFFER_TRACE(bitmap_bh
, "clear bit");
5028 if (!mb_test_bit(bit
+ i
, bitmap_bh
->b_data
)) {
5029 ext4_error(sb
, "bit already cleared for block %llu",
5030 (ext4_fsblk_t
)(block
+ i
));
5031 BUFFER_TRACE(bitmap_bh
, "bit already cleared");
5037 err
= ext4_mb_load_buddy(sb
, block_group
, &e4b
);
5042 * need to update group_info->bb_free and bitmap
5043 * with group lock held. generate_buddy look at
5044 * them with group lock_held
5046 ext4_lock_group(sb
, block_group
);
5047 mb_clear_bits(bitmap_bh
->b_data
, bit
, cluster_count
);
5048 mb_free_blocks(NULL
, &e4b
, bit
, cluster_count
);
5049 free_clusters_count
= clusters_freed
+
5050 ext4_free_group_clusters(sb
, desc
);
5051 ext4_free_group_clusters_set(sb
, desc
, free_clusters_count
);
5052 ext4_block_bitmap_csum_set(sb
, block_group
, desc
, bitmap_bh
);
5053 ext4_group_desc_csum_set(sb
, block_group
, desc
);
5054 ext4_unlock_group(sb
, block_group
);
5055 percpu_counter_add(&sbi
->s_freeclusters_counter
,
5058 if (sbi
->s_log_groups_per_flex
) {
5059 ext4_group_t flex_group
= ext4_flex_group(sbi
, block_group
);
5060 atomic64_add(clusters_freed
,
5061 &sbi
->s_flex_groups
[flex_group
].free_clusters
);
5064 ext4_mb_unload_buddy(&e4b
);
5066 /* We dirtied the bitmap block */
5067 BUFFER_TRACE(bitmap_bh
, "dirtied bitmap block");
5068 err
= ext4_handle_dirty_metadata(handle
, NULL
, bitmap_bh
);
5070 /* And the group descriptor block */
5071 BUFFER_TRACE(gd_bh
, "dirtied group descriptor block");
5072 ret
= ext4_handle_dirty_metadata(handle
, NULL
, gd_bh
);
5078 ext4_std_error(sb
, err
);
5083 * ext4_trim_extent -- function to TRIM one single free extent in the group
5084 * @sb: super block for the file system
5085 * @start: starting block of the free extent in the alloc. group
5086 * @count: number of blocks to TRIM
5087 * @group: alloc. group we are working with
5088 * @e4b: ext4 buddy for the group
5090 * Trim "count" blocks starting at "start" in the "group". To assure that no
5091 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5092 * be called with under the group lock.
5094 static int ext4_trim_extent(struct super_block
*sb
, int start
, int count
,
5095 ext4_group_t group
, struct ext4_buddy
*e4b
)
5099 struct ext4_free_extent ex
;
5102 trace_ext4_trim_extent(sb
, group
, start
, count
);
5104 assert_spin_locked(ext4_group_lock_ptr(sb
, group
));
5106 ex
.fe_start
= start
;
5107 ex
.fe_group
= group
;
5111 * Mark blocks used, so no one can reuse them while
5114 mb_mark_used(e4b
, &ex
);
5115 ext4_unlock_group(sb
, group
);
5116 ret
= ext4_issue_discard(sb
, group
, start
, count
, NULL
);
5117 ext4_lock_group(sb
, group
);
5118 mb_free_blocks(NULL
, e4b
, start
, ex
.fe_len
);
5123 * ext4_trim_all_free -- function to trim all free space in alloc. group
5124 * @sb: super block for file system
5125 * @group: group to be trimmed
5126 * @start: first group block to examine
5127 * @max: last group block to examine
5128 * @minblocks: minimum extent block count
5130 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5131 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5135 * ext4_trim_all_free walks through group's block bitmap searching for free
5136 * extents. When the free extent is found, mark it as used in group buddy
5137 * bitmap. Then issue a TRIM command on this extent and free the extent in
5138 * the group buddy bitmap. This is done until whole group is scanned.
5140 static ext4_grpblk_t
5141 ext4_trim_all_free(struct super_block
*sb
, ext4_group_t group
,
5142 ext4_grpblk_t start
, ext4_grpblk_t max
,
5143 ext4_grpblk_t minblocks
)
5146 ext4_grpblk_t next
, count
= 0, free_count
= 0;
5147 struct ext4_buddy e4b
;
5150 trace_ext4_trim_all_free(sb
, group
, start
, max
);
5152 ret
= ext4_mb_load_buddy(sb
, group
, &e4b
);
5154 ext4_warning(sb
, "Error %d loading buddy information for %u",
5158 bitmap
= e4b
.bd_bitmap
;
5160 ext4_lock_group(sb
, group
);
5161 if (EXT4_MB_GRP_WAS_TRIMMED(e4b
.bd_info
) &&
5162 minblocks
>= atomic_read(&EXT4_SB(sb
)->s_last_trim_minblks
))
5165 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
5166 e4b
.bd_info
->bb_first_free
: start
;
5168 while (start
<= max
) {
5169 start
= mb_find_next_zero_bit(bitmap
, max
+ 1, start
);
5172 next
= mb_find_next_bit(bitmap
, max
+ 1, start
);
5174 if ((next
- start
) >= minblocks
) {
5175 ret
= ext4_trim_extent(sb
, start
,
5176 next
- start
, group
, &e4b
);
5177 if (ret
&& ret
!= -EOPNOTSUPP
)
5180 count
+= next
- start
;
5182 free_count
+= next
- start
;
5185 if (fatal_signal_pending(current
)) {
5186 count
= -ERESTARTSYS
;
5190 if (need_resched()) {
5191 ext4_unlock_group(sb
, group
);
5193 ext4_lock_group(sb
, group
);
5196 if ((e4b
.bd_info
->bb_free
- free_count
) < minblocks
)
5202 EXT4_MB_GRP_SET_TRIMMED(e4b
.bd_info
);
5205 ext4_unlock_group(sb
, group
);
5206 ext4_mb_unload_buddy(&e4b
);
5208 ext4_debug("trimmed %d blocks in the group %d\n",
5215 * ext4_trim_fs() -- trim ioctl handle function
5216 * @sb: superblock for filesystem
5217 * @range: fstrim_range structure
5219 * start: First Byte to trim
5220 * len: number of Bytes to trim from start
5221 * minlen: minimum extent length in Bytes
5222 * ext4_trim_fs goes through all allocation groups containing Bytes from
5223 * start to start+len. For each such a group ext4_trim_all_free function
5224 * is invoked to trim all free space.
5226 int ext4_trim_fs(struct super_block
*sb
, struct fstrim_range
*range
)
5228 struct ext4_group_info
*grp
;
5229 ext4_group_t group
, first_group
, last_group
;
5230 ext4_grpblk_t cnt
= 0, first_cluster
, last_cluster
;
5231 uint64_t start
, end
, minlen
, trimmed
= 0;
5232 ext4_fsblk_t first_data_blk
=
5233 le32_to_cpu(EXT4_SB(sb
)->s_es
->s_first_data_block
);
5234 ext4_fsblk_t max_blks
= ext4_blocks_count(EXT4_SB(sb
)->s_es
);
5237 start
= range
->start
>> sb
->s_blocksize_bits
;
5238 end
= start
+ (range
->len
>> sb
->s_blocksize_bits
) - 1;
5239 minlen
= EXT4_NUM_B2C(EXT4_SB(sb
),
5240 range
->minlen
>> sb
->s_blocksize_bits
);
5242 if (minlen
> EXT4_CLUSTERS_PER_GROUP(sb
) ||
5243 start
>= max_blks
||
5244 range
->len
< sb
->s_blocksize
)
5246 if (end
>= max_blks
)
5248 if (end
<= first_data_blk
)
5250 if (start
< first_data_blk
)
5251 start
= first_data_blk
;
5253 /* Determine first and last group to examine based on start and end */
5254 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) start
,
5255 &first_group
, &first_cluster
);
5256 ext4_get_group_no_and_offset(sb
, (ext4_fsblk_t
) end
,
5257 &last_group
, &last_cluster
);
5259 /* end now represents the last cluster to discard in this group */
5260 end
= EXT4_CLUSTERS_PER_GROUP(sb
) - 1;
5262 for (group
= first_group
; group
<= last_group
; group
++) {
5263 grp
= ext4_get_group_info(sb
, group
);
5264 /* We only do this if the grp has never been initialized */
5265 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp
))) {
5266 ret
= ext4_mb_init_group(sb
, group
, GFP_NOFS
);
5272 * For all the groups except the last one, last cluster will
5273 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5274 * change it for the last group, note that last_cluster is
5275 * already computed earlier by ext4_get_group_no_and_offset()
5277 if (group
== last_group
)
5280 if (grp
->bb_free
>= minlen
) {
5281 cnt
= ext4_trim_all_free(sb
, group
, first_cluster
,
5291 * For every group except the first one, we are sure
5292 * that the first cluster to discard will be cluster #0.
5298 atomic_set(&EXT4_SB(sb
)->s_last_trim_minblks
, minlen
);
5301 range
->len
= EXT4_C2B(EXT4_SB(sb
), trimmed
) << sb
->s_blocksize_bits
;
5305 /* Iterate all the free extents in the group. */
5307 ext4_mballoc_query_range(
5308 struct super_block
*sb
,
5310 ext4_grpblk_t start
,
5312 ext4_mballoc_query_range_fn formatter
,
5317 struct ext4_buddy e4b
;
5320 error
= ext4_mb_load_buddy(sb
, group
, &e4b
);
5323 bitmap
= e4b
.bd_bitmap
;
5325 ext4_lock_group(sb
, group
);
5327 start
= (e4b
.bd_info
->bb_first_free
> start
) ?
5328 e4b
.bd_info
->bb_first_free
: start
;
5329 if (end
>= EXT4_CLUSTERS_PER_GROUP(sb
))
5330 end
= EXT4_CLUSTERS_PER_GROUP(sb
) - 1;
5332 while (start
<= end
) {
5333 start
= mb_find_next_zero_bit(bitmap
, end
+ 1, start
);
5336 next
= mb_find_next_bit(bitmap
, end
+ 1, start
);
5338 ext4_unlock_group(sb
, group
);
5339 error
= formatter(sb
, group
, start
, next
- start
, priv
);
5342 ext4_lock_group(sb
, group
);
5347 ext4_unlock_group(sb
, group
);
5349 ext4_mb_unload_buddy(&e4b
);