1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com 4 * Written by Alex Tomas <alex@clusterfs.com> 5 */ 6 7 8 /* 9 * mballoc.c contains the multiblocks allocation routines 10 */ 11 12 #include "ext4_jbd2.h" 13 #include "mballoc.h" 14 #include <linux/log2.h> 15 #include <linux/module.h> 16 #include <linux/slab.h> 17 #include <linux/nospec.h> 18 #include <linux/backing-dev.h> 19 #include <linux/freezer.h> 20 #include <trace/events/ext4.h> 21 #include <kunit/static_stub.h> 22 23 /* 24 * MUSTDO: 25 * - test ext4_ext_search_left() and ext4_ext_search_right() 26 * - search for metadata in few groups 27 * 28 * TODO v4: 29 * - normalization should take into account whether file is still open 30 * - discard preallocations if no free space left (policy?) 31 * - don't normalize tails 32 * - quota 33 * - reservation for superuser 34 * 35 * TODO v3: 36 * - bitmap read-ahead (proposed by Oleg Drokin aka green) 37 * - track min/max extents in each group for better group selection 38 * - mb_mark_used() may allocate chunk right after splitting buddy 39 * - tree of groups sorted by number of free blocks 40 * - error handling 41 */ 42 43 /* 44 * The allocation request involve request for multiple number of blocks 45 * near to the goal(block) value specified. 46 * 47 * During initialization phase of the allocator we decide to use the 48 * group preallocation or inode preallocation depending on the size of 49 * the file. The size of the file could be the resulting file size we 50 * would have after allocation, or the current file size, which ever 51 * is larger. If the size is less than sbi->s_mb_stream_request we 52 * select to use the group preallocation. The default value of 53 * s_mb_stream_request is 16 blocks. This can also be tuned via 54 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in 55 * terms of number of blocks. 56 * 57 * The main motivation for having small file use group preallocation is to 58 * ensure that we have small files closer together on the disk. 59 * 60 * First stage the allocator looks at the inode prealloc list, 61 * ext4_inode_info->i_prealloc_list, which contains list of prealloc 62 * spaces for this particular inode. The inode prealloc space is 63 * represented as: 64 * 65 * pa_lstart -> the logical start block for this prealloc space 66 * pa_pstart -> the physical start block for this prealloc space 67 * pa_len -> length for this prealloc space (in clusters) 68 * pa_free -> free space available in this prealloc space (in clusters) 69 * 70 * The inode preallocation space is used looking at the _logical_ start 71 * block. If only the logical file block falls within the range of prealloc 72 * space we will consume the particular prealloc space. This makes sure that 73 * we have contiguous physical blocks representing the file blocks 74 * 75 * The important thing to be noted in case of inode prealloc space is that 76 * we don't modify the values associated to inode prealloc space except 77 * pa_free. 78 * 79 * If we are not able to find blocks in the inode prealloc space and if we 80 * have the group allocation flag set then we look at the locality group 81 * prealloc space. These are per CPU prealloc list represented as 82 * 83 * ext4_sb_info.s_locality_groups[smp_processor_id()] 84 * 85 * The reason for having a per cpu locality group is to reduce the contention 86 * between CPUs. It is possible to get scheduled at this point. 87 * 88 * The locality group prealloc space is used looking at whether we have 89 * enough free space (pa_free) within the prealloc space. 90 * 91 * If we can't allocate blocks via inode prealloc or/and locality group 92 * prealloc then we look at the buddy cache. The buddy cache is represented 93 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets 94 * mapped to the buddy and bitmap information regarding different 95 * groups. The buddy information is attached to buddy cache inode so that 96 * we can access them through the page cache. The information regarding 97 * each group is loaded via ext4_mb_load_buddy. The information involve 98 * block bitmap and buddy information. The information are stored in the 99 * inode as: 100 * 101 * { folio } 102 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... 103 * 104 * 105 * one block each for bitmap and buddy information. So for each group we 106 * take up 2 blocks. A folio can contain blocks_per_folio (folio_size / 107 * blocksize) blocks. So it can have information regarding groups_per_folio 108 * which is blocks_per_folio/2 109 * 110 * The buddy cache inode is not stored on disk. The inode is thrown 111 * away when the filesystem is unmounted. 112 * 113 * We look for count number of blocks in the buddy cache. If we were able 114 * to locate that many free blocks we return with additional information 115 * regarding rest of the contiguous physical block available 116 * 117 * Before allocating blocks via buddy cache we normalize the request 118 * blocks. This ensure we ask for more blocks that we needed. The extra 119 * blocks that we get after allocation is added to the respective prealloc 120 * list. In case of inode preallocation we follow a list of heuristics 121 * based on file size. This can be found in ext4_mb_normalize_request. If 122 * we are doing a group prealloc we try to normalize the request to 123 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is 124 * dependent on the cluster size; for non-bigalloc file systems, it is 125 * 512 blocks. This can be tuned via 126 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in 127 * terms of number of blocks. If we have mounted the file system with -O 128 * stripe=<value> option the group prealloc request is normalized to the 129 * smallest multiple of the stripe value (sbi->s_stripe) which is 130 * greater than the default mb_group_prealloc. 131 * 132 * If "mb_optimize_scan" mount option is set, we maintain in memory group info 133 * structures in two data structures: 134 * 135 * 1) Array of largest free order xarrays (sbi->s_mb_largest_free_orders) 136 * 137 * Locking: Writers use xa_lock, readers use rcu_read_lock. 138 * 139 * This is an array of xarrays where the index in the array represents the 140 * largest free order in the buddy bitmap of the participating group infos of 141 * that xarray. So, there are exactly MB_NUM_ORDERS(sb) (which means total 142 * number of buddy bitmap orders possible) number of xarrays. Group-infos are 143 * placed in appropriate xarrays. 144 * 145 * 2) Average fragment size xarrays (sbi->s_mb_avg_fragment_size) 146 * 147 * Locking: Writers use xa_lock, readers use rcu_read_lock. 148 * 149 * This is an array of xarrays where in the i-th xarray there are groups with 150 * average fragment size >= 2^i and < 2^(i+1). The average fragment size 151 * is computed as ext4_group_info->bb_free / ext4_group_info->bb_fragments. 152 * Note that we don't bother with a special xarray for completely empty 153 * groups so we only have MB_NUM_ORDERS(sb) xarrays. Group-infos are placed 154 * in appropriate xarrays. 155 * 156 * In xarray, the index is the block group number, the value is the block group 157 * information, and a non-empty value indicates the block group is present in 158 * the current xarray. 159 * 160 * When "mb_optimize_scan" mount option is set, mballoc consults the above data 161 * structures to decide the order in which groups are to be traversed for 162 * fulfilling an allocation request. 163 * 164 * At CR_POWER2_ALIGNED , we look for groups which have the largest_free_order 165 * >= the order of the request. We directly look at the largest free order list 166 * in the data structure (1) above where largest_free_order = order of the 167 * request. If that list is empty, we look at remaining list in the increasing 168 * order of largest_free_order. This allows us to perform CR_POWER2_ALIGNED 169 * lookup in O(1) time. 170 * 171 * At CR_GOAL_LEN_FAST, we only consider groups where 172 * average fragment size > request size. So, we lookup a group which has average 173 * fragment size just above or equal to request size using our average fragment 174 * size group lists (data structure 2) in O(1) time. 175 * 176 * At CR_BEST_AVAIL_LEN, we aim to optimize allocations which can't be satisfied 177 * in CR_GOAL_LEN_FAST. The fact that we couldn't find a group in 178 * CR_GOAL_LEN_FAST suggests that there is no BG that has avg 179 * fragment size > goal length. So before falling to the slower 180 * CR_GOAL_LEN_SLOW, in CR_BEST_AVAIL_LEN we proactively trim goal length and 181 * then use the same fragment lists as CR_GOAL_LEN_FAST to find a BG with a big 182 * enough average fragment size. This increases the chances of finding a 183 * suitable block group in O(1) time and results in faster allocation at the 184 * cost of reduced size of allocation. 185 * 186 * If "mb_optimize_scan" mount option is not set, mballoc traverses groups in 187 * linear order which requires O(N) search time for each CR_POWER2_ALIGNED and 188 * CR_GOAL_LEN_FAST phase. 189 * 190 * The regular allocator (using the buddy cache) supports a few tunables. 191 * 192 * /sys/fs/ext4/<partition>/mb_min_to_scan 193 * /sys/fs/ext4/<partition>/mb_max_to_scan 194 * /sys/fs/ext4/<partition>/mb_order2_req 195 * /sys/fs/ext4/<partition>/mb_max_linear_groups 196 * 197 * The regular allocator uses buddy scan only if the request len is power of 198 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The 199 * value of s_mb_order2_reqs can be tuned via 200 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to 201 * stripe size (sbi->s_stripe), we try to search for contiguous block in 202 * stripe size. This should result in better allocation on RAID setups. If 203 * not, we search in the specific group using bitmap for best extents. The 204 * tunable min_to_scan and max_to_scan control the behaviour here. 205 * min_to_scan indicate how long the mballoc __must__ look for a best 206 * extent and max_to_scan indicates how long the mballoc __can__ look for a 207 * best extent in the found extents. Searching for the blocks starts with 208 * the group specified as the goal value in allocation context via 209 * ac_g_ex. Each group is first checked based on the criteria whether it 210 * can be used for allocation. ext4_mb_good_group explains how the groups are 211 * checked. 212 * 213 * When "mb_optimize_scan" is turned on, as mentioned above, the groups may not 214 * get traversed linearly. That may result in subsequent allocations being not 215 * close to each other. And so, the underlying device may get filled up in a 216 * non-linear fashion. While that may not matter on non-rotational devices, for 217 * rotational devices that may result in higher seek times. "mb_max_linear_groups" 218 * tells mballoc how many groups mballoc should search linearly before 219 * performing consulting above data structures for more efficient lookups. For 220 * non rotational devices, this value defaults to 0 and for rotational devices 221 * this is set to MB_DEFAULT_LINEAR_LIMIT. 222 * 223 * Both the prealloc space are getting populated as above. So for the first 224 * request we will hit the buddy cache which will result in this prealloc 225 * space getting filled. The prealloc space is then later used for the 226 * subsequent request. 227 */ 228 229 /* 230 * mballoc operates on the following data: 231 * - on-disk bitmap 232 * - in-core buddy (actually includes buddy and bitmap) 233 * - preallocation descriptors (PAs) 234 * 235 * there are two types of preallocations: 236 * - inode 237 * assiged to specific inode and can be used for this inode only. 238 * it describes part of inode's space preallocated to specific 239 * physical blocks. any block from that preallocated can be used 240 * independent. the descriptor just tracks number of blocks left 241 * unused. so, before taking some block from descriptor, one must 242 * make sure corresponded logical block isn't allocated yet. this 243 * also means that freeing any block within descriptor's range 244 * must discard all preallocated blocks. 245 * - locality group 246 * assigned to specific locality group which does not translate to 247 * permanent set of inodes: inode can join and leave group. space 248 * from this type of preallocation can be used for any inode. thus 249 * it's consumed from the beginning to the end. 250 * 251 * relation between them can be expressed as: 252 * in-core buddy = on-disk bitmap + preallocation descriptors 253 * 254 * this mean blocks mballoc considers used are: 255 * - allocated blocks (persistent) 256 * - preallocated blocks (non-persistent) 257 * 258 * consistency in mballoc world means that at any time a block is either 259 * free or used in ALL structures. notice: "any time" should not be read 260 * literally -- time is discrete and delimited by locks. 261 * 262 * to keep it simple, we don't use block numbers, instead we count number of 263 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA. 264 * 265 * all operations can be expressed as: 266 * - init buddy: buddy = on-disk + PAs 267 * - new PA: buddy += N; PA = N 268 * - use inode PA: on-disk += N; PA -= N 269 * - discard inode PA buddy -= on-disk - PA; PA = 0 270 * - use locality group PA on-disk += N; PA -= N 271 * - discard locality group PA buddy -= PA; PA = 0 272 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap 273 * is used in real operation because we can't know actual used 274 * bits from PA, only from on-disk bitmap 275 * 276 * if we follow this strict logic, then all operations above should be atomic. 277 * given some of them can block, we'd have to use something like semaphores 278 * killing performance on high-end SMP hardware. let's try to relax it using 279 * the following knowledge: 280 * 1) if buddy is referenced, it's already initialized 281 * 2) while block is used in buddy and the buddy is referenced, 282 * nobody can re-allocate that block 283 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has 284 * bit set and PA claims same block, it's OK. IOW, one can set bit in 285 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded 286 * block 287 * 288 * so, now we're building a concurrency table: 289 * - init buddy vs. 290 * - new PA 291 * blocks for PA are allocated in the buddy, buddy must be referenced 292 * until PA is linked to allocation group to avoid concurrent buddy init 293 * - use inode PA 294 * we need to make sure that either on-disk bitmap or PA has uptodate data 295 * given (3) we care that PA-=N operation doesn't interfere with init 296 * - discard inode PA 297 * the simplest way would be to have buddy initialized by the discard 298 * - use locality group PA 299 * again PA-=N must be serialized with init 300 * - discard locality group PA 301 * the simplest way would be to have buddy initialized by the discard 302 * - new PA vs. 303 * - use inode PA 304 * i_data_sem serializes them 305 * - discard inode PA 306 * discard process must wait until PA isn't used by another process 307 * - use locality group PA 308 * some mutex should serialize them 309 * - discard locality group PA 310 * discard process must wait until PA isn't used by another process 311 * - use inode PA 312 * - use inode PA 313 * i_data_sem or another mutex should serializes them 314 * - discard inode PA 315 * discard process must wait until PA isn't used by another process 316 * - use locality group PA 317 * nothing wrong here -- they're different PAs covering different blocks 318 * - discard locality group PA 319 * discard process must wait until PA isn't used by another process 320 * 321 * now we're ready to make few consequences: 322 * - PA is referenced and while it is no discard is possible 323 * - PA is referenced until block isn't marked in on-disk bitmap 324 * - PA changes only after on-disk bitmap 325 * - discard must not compete with init. either init is done before 326 * any discard or they're serialized somehow 327 * - buddy init as sum of on-disk bitmap and PAs is done atomically 328 * 329 * a special case when we've used PA to emptiness. no need to modify buddy 330 * in this case, but we should care about concurrent init 331 * 332 */ 333 334 /* 335 * Logic in few words: 336 * 337 * - allocation: 338 * load group 339 * find blocks 340 * mark bits in on-disk bitmap 341 * release group 342 * 343 * - use preallocation: 344 * find proper PA (per-inode or group) 345 * load group 346 * mark bits in on-disk bitmap 347 * release group 348 * release PA 349 * 350 * - free: 351 * load group 352 * mark bits in on-disk bitmap 353 * release group 354 * 355 * - discard preallocations in group: 356 * mark PAs deleted 357 * move them onto local list 358 * load on-disk bitmap 359 * load group 360 * remove PA from object (inode or locality group) 361 * mark free blocks in-core 362 * 363 * - discard inode's preallocations: 364 */ 365 366 /* 367 * Locking rules 368 * 369 * Locks: 370 * - bitlock on a group (group) 371 * - object (inode/locality) (object) 372 * - per-pa lock (pa) 373 * - cr_power2_aligned lists lock (cr_power2_aligned) 374 * - cr_goal_len_fast lists lock (cr_goal_len_fast) 375 * 376 * Paths: 377 * - new pa 378 * object 379 * group 380 * 381 * - find and use pa: 382 * pa 383 * 384 * - release consumed pa: 385 * pa 386 * group 387 * object 388 * 389 * - generate in-core bitmap: 390 * group 391 * pa 392 * 393 * - discard all for given object (inode, locality group): 394 * object 395 * pa 396 * group 397 * 398 * - discard all for given group: 399 * group 400 * pa 401 * group 402 * object 403 * 404 * - allocation path (ext4_mb_regular_allocator) 405 * group 406 * cr_power2_aligned/cr_goal_len_fast 407 */ 408 static struct kmem_cache *ext4_pspace_cachep; 409 static struct kmem_cache *ext4_ac_cachep; 410 static struct kmem_cache *ext4_free_data_cachep; 411 412 /* We create slab caches for groupinfo data structures based on the 413 * superblock block size. There will be one per mounted filesystem for 414 * each unique s_blocksize_bits */ 415 #define NR_GRPINFO_CACHES 8 416 static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES]; 417 418 static const char * const ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = { 419 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k", 420 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k", 421 "ext4_groupinfo_64k", "ext4_groupinfo_128k" 422 }; 423 424 static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, 425 ext4_group_t group); 426 static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac); 427 428 static int ext4_mb_scan_group(struct ext4_allocation_context *ac, 429 ext4_group_t group); 430 431 static int ext4_try_to_trim_range(struct super_block *sb, 432 struct ext4_buddy *e4b, ext4_grpblk_t start, 433 ext4_grpblk_t max, ext4_grpblk_t minblocks); 434 435 /* 436 * The algorithm using this percpu seq counter goes below: 437 * 1. We sample the percpu discard_pa_seq counter before trying for block 438 * allocation in ext4_mb_new_blocks(). 439 * 2. We increment this percpu discard_pa_seq counter when we either allocate 440 * or free these blocks i.e. while marking those blocks as used/free in 441 * mb_mark_used()/mb_free_blocks(). 442 * 3. We also increment this percpu seq counter when we successfully identify 443 * that the bb_prealloc_list is not empty and hence proceed for discarding 444 * of those PAs inside ext4_mb_discard_group_preallocations(). 445 * 446 * Now to make sure that the regular fast path of block allocation is not 447 * affected, as a small optimization we only sample the percpu seq counter 448 * on that cpu. Only when the block allocation fails and when freed blocks 449 * found were 0, that is when we sample percpu seq counter for all cpus using 450 * below function ext4_get_discard_pa_seq_sum(). This happens after making 451 * sure that all the PAs on grp->bb_prealloc_list got freed or if it's empty. 452 */ 453 static DEFINE_PER_CPU(u64, discard_pa_seq); 454 static inline u64 ext4_get_discard_pa_seq_sum(void) 455 { 456 int __cpu; 457 u64 __seq = 0; 458 459 for_each_possible_cpu(__cpu) 460 __seq += per_cpu(discard_pa_seq, __cpu); 461 return __seq; 462 } 463 464 static inline void *mb_correct_addr_and_bit(int *bit, void *addr) 465 { 466 #if BITS_PER_LONG == 64 467 *bit += ((unsigned long) addr & 7UL) << 3; 468 addr = (void *) ((unsigned long) addr & ~7UL); 469 #elif BITS_PER_LONG == 32 470 *bit += ((unsigned long) addr & 3UL) << 3; 471 addr = (void *) ((unsigned long) addr & ~3UL); 472 #else 473 #error "how many bits you are?!" 474 #endif 475 return addr; 476 } 477 478 static inline int mb_test_bit(int bit, void *addr) 479 { 480 /* 481 * ext4_test_bit on architecture like powerpc 482 * needs unsigned long aligned address 483 */ 484 addr = mb_correct_addr_and_bit(&bit, addr); 485 return ext4_test_bit(bit, addr); 486 } 487 488 static inline void mb_set_bit(int bit, void *addr) 489 { 490 addr = mb_correct_addr_and_bit(&bit, addr); 491 ext4_set_bit(bit, addr); 492 } 493 494 static inline void mb_clear_bit(int bit, void *addr) 495 { 496 addr = mb_correct_addr_and_bit(&bit, addr); 497 ext4_clear_bit(bit, addr); 498 } 499 500 static inline int mb_test_and_clear_bit(int bit, void *addr) 501 { 502 addr = mb_correct_addr_and_bit(&bit, addr); 503 return ext4_test_and_clear_bit(bit, addr); 504 } 505 506 static inline int mb_find_next_zero_bit(void *addr, int max, int start) 507 { 508 int fix = 0, ret, tmpmax; 509 addr = mb_correct_addr_and_bit(&fix, addr); 510 tmpmax = max + fix; 511 start += fix; 512 513 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix; 514 if (ret > max) 515 return max; 516 return ret; 517 } 518 519 static inline int mb_find_next_bit(void *addr, int max, int start) 520 { 521 int fix = 0, ret, tmpmax; 522 addr = mb_correct_addr_and_bit(&fix, addr); 523 tmpmax = max + fix; 524 start += fix; 525 526 ret = ext4_find_next_bit(addr, tmpmax, start) - fix; 527 if (ret > max) 528 return max; 529 return ret; 530 } 531 532 static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max) 533 { 534 char *bb; 535 536 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); 537 BUG_ON(max == NULL); 538 539 if (order > e4b->bd_blkbits + 1) { 540 *max = 0; 541 return NULL; 542 } 543 544 /* at order 0 we see each particular block */ 545 if (order == 0) { 546 *max = 1 << (e4b->bd_blkbits + 3); 547 return e4b->bd_bitmap; 548 } 549 550 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order]; 551 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order]; 552 553 return bb; 554 } 555 556 #ifdef DOUBLE_CHECK 557 static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b, 558 int first, int count) 559 { 560 int i; 561 struct super_block *sb = e4b->bd_sb; 562 563 if (unlikely(e4b->bd_info->bb_bitmap == NULL)) 564 return; 565 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); 566 for (i = 0; i < count; i++) { 567 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) { 568 ext4_fsblk_t blocknr; 569 570 blocknr = ext4_group_first_block_no(sb, e4b->bd_group); 571 blocknr += EXT4_C2B(EXT4_SB(sb), first + i); 572 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, 573 EXT4_GROUP_INFO_BBITMAP_CORRUPT); 574 ext4_grp_locked_error(sb, e4b->bd_group, 575 inode ? inode->i_ino : 0, 576 blocknr, 577 "freeing block already freed " 578 "(bit %u)", 579 first + i); 580 } 581 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap); 582 } 583 } 584 585 static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count) 586 { 587 int i; 588 589 if (unlikely(e4b->bd_info->bb_bitmap == NULL)) 590 return; 591 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); 592 for (i = 0; i < count; i++) { 593 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap)); 594 mb_set_bit(first + i, e4b->bd_info->bb_bitmap); 595 } 596 } 597 598 static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) 599 { 600 if (unlikely(e4b->bd_info->bb_bitmap == NULL)) 601 return; 602 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) { 603 unsigned char *b1, *b2; 604 int i; 605 b1 = (unsigned char *) e4b->bd_info->bb_bitmap; 606 b2 = (unsigned char *) bitmap; 607 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) { 608 if (b1[i] != b2[i]) { 609 ext4_msg(e4b->bd_sb, KERN_ERR, 610 "corruption in group %u " 611 "at byte %u(%u): %x in copy != %x " 612 "on disk/prealloc", 613 e4b->bd_group, i, i * 8, b1[i], b2[i]); 614 BUG(); 615 } 616 } 617 } 618 } 619 620 static void mb_group_bb_bitmap_alloc(struct super_block *sb, 621 struct ext4_group_info *grp, ext4_group_t group) 622 { 623 struct buffer_head *bh; 624 625 grp->bb_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS); 626 if (!grp->bb_bitmap) 627 return; 628 629 bh = ext4_read_block_bitmap(sb, group); 630 if (IS_ERR_OR_NULL(bh)) { 631 kfree(grp->bb_bitmap); 632 grp->bb_bitmap = NULL; 633 return; 634 } 635 636 memcpy(grp->bb_bitmap, bh->b_data, sb->s_blocksize); 637 put_bh(bh); 638 } 639 640 static void mb_group_bb_bitmap_free(struct ext4_group_info *grp) 641 { 642 kfree(grp->bb_bitmap); 643 } 644 645 #else 646 static inline void mb_free_blocks_double(struct inode *inode, 647 struct ext4_buddy *e4b, int first, int count) 648 { 649 return; 650 } 651 static inline void mb_mark_used_double(struct ext4_buddy *e4b, 652 int first, int count) 653 { 654 return; 655 } 656 static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap) 657 { 658 return; 659 } 660 661 static inline void mb_group_bb_bitmap_alloc(struct super_block *sb, 662 struct ext4_group_info *grp, ext4_group_t group) 663 { 664 return; 665 } 666 667 static inline void mb_group_bb_bitmap_free(struct ext4_group_info *grp) 668 { 669 return; 670 } 671 #endif 672 673 #ifdef AGGRESSIVE_CHECK 674 675 #define MB_CHECK_ASSERT(assert) \ 676 do { \ 677 if (!(assert)) { \ 678 printk(KERN_EMERG \ 679 "Assertion failure in %s() at %s:%d: \"%s\"\n", \ 680 function, file, line, # assert); \ 681 BUG(); \ 682 } \ 683 } while (0) 684 685 /* 686 * Perform buddy integrity check with the following steps: 687 * 688 * 1. Top-down validation (from highest order down to order 1, excluding order-0 bitmap): 689 * For each pair of adjacent orders, if a higher-order bit is set (indicating a free block), 690 * at most one of the two corresponding lower-order bits may be clear (free). 691 * 692 * 2. Order-0 (bitmap) validation, performed on bit pairs: 693 * - If either bit in a pair is set (1, allocated), then all corresponding higher-order bits 694 * must not be free (0). 695 * - If both bits in a pair are clear (0, free), then exactly one of the corresponding 696 * higher-order bits must be free (0). 697 * 698 * 3. Preallocation (pa) list validation: 699 * For each preallocated block (pa) in the group: 700 * - Verify that pa_pstart falls within the bounds of this block group. 701 * - Ensure the corresponding bit(s) in the order-0 bitmap are marked as allocated (1). 702 */ 703 static void __mb_check_buddy(struct ext4_buddy *e4b, char *file, 704 const char *function, int line) 705 { 706 struct super_block *sb = e4b->bd_sb; 707 int order = e4b->bd_blkbits + 1; 708 int max; 709 int max2; 710 int i; 711 int j; 712 int k; 713 int count; 714 struct ext4_group_info *grp; 715 int fragments = 0; 716 int fstart; 717 struct list_head *cur; 718 void *buddy; 719 void *buddy2; 720 721 if (e4b->bd_info->bb_check_counter++ % 10) 722 return; 723 724 while (order > 1) { 725 buddy = mb_find_buddy(e4b, order, &max); 726 MB_CHECK_ASSERT(buddy); 727 buddy2 = mb_find_buddy(e4b, order - 1, &max2); 728 MB_CHECK_ASSERT(buddy2); 729 MB_CHECK_ASSERT(buddy != buddy2); 730 MB_CHECK_ASSERT(max * 2 == max2); 731 732 count = 0; 733 for (i = 0; i < max; i++) { 734 735 if (mb_test_bit(i, buddy)) { 736 /* only single bit in buddy2 may be 0 */ 737 if (!mb_test_bit(i << 1, buddy2)) { 738 MB_CHECK_ASSERT( 739 mb_test_bit((i<<1)+1, buddy2)); 740 } 741 continue; 742 } 743 744 count++; 745 } 746 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count); 747 order--; 748 } 749 750 fstart = -1; 751 buddy = mb_find_buddy(e4b, 0, &max); 752 for (i = 0; i < max; i++) { 753 if (!mb_test_bit(i, buddy)) { 754 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free); 755 if (fstart == -1) { 756 fragments++; 757 fstart = i; 758 } 759 } else { 760 fstart = -1; 761 } 762 if (!(i & 1)) { 763 int in_use, zero_bit_count = 0; 764 765 in_use = mb_test_bit(i, buddy) || mb_test_bit(i + 1, buddy); 766 for (j = 1; j < e4b->bd_blkbits + 2; j++) { 767 buddy2 = mb_find_buddy(e4b, j, &max2); 768 k = i >> j; 769 MB_CHECK_ASSERT(k < max2); 770 if (!mb_test_bit(k, buddy2)) 771 zero_bit_count++; 772 } 773 MB_CHECK_ASSERT(zero_bit_count == !in_use); 774 } 775 } 776 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info)); 777 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments); 778 779 grp = ext4_get_group_info(sb, e4b->bd_group); 780 if (!grp) 781 return; 782 list_for_each(cur, &grp->bb_prealloc_list) { 783 ext4_group_t groupnr; 784 struct ext4_prealloc_space *pa; 785 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); 786 if (!pa->pa_len) 787 continue; 788 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k); 789 MB_CHECK_ASSERT(groupnr == e4b->bd_group); 790 for (i = 0; i < pa->pa_len; i++) 791 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy)); 792 } 793 } 794 #undef MB_CHECK_ASSERT 795 #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \ 796 __FILE__, __func__, __LINE__) 797 #else 798 #define mb_check_buddy(e4b) 799 #endif 800 801 /* 802 * Divide blocks started from @first with length @len into 803 * smaller chunks with power of 2 blocks. 804 * Clear the bits in bitmap which the blocks of the chunk(s) covered, 805 * then increase bb_counters[] for corresponded chunk size. 806 */ 807 static void ext4_mb_mark_free_simple(struct super_block *sb, 808 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len, 809 struct ext4_group_info *grp) 810 { 811 struct ext4_sb_info *sbi = EXT4_SB(sb); 812 ext4_grpblk_t min; 813 ext4_grpblk_t max; 814 ext4_grpblk_t chunk; 815 unsigned int border; 816 817 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb)); 818 819 border = 2 << sb->s_blocksize_bits; 820 821 while (len > 0) { 822 /* find how many blocks can be covered since this position */ 823 max = ffs(first | border) - 1; 824 825 /* find how many blocks of power 2 we need to mark */ 826 min = fls(len) - 1; 827 828 if (max < min) 829 min = max; 830 chunk = 1 << min; 831 832 /* mark multiblock chunks only */ 833 grp->bb_counters[min]++; 834 if (min > 0) 835 mb_clear_bit(first >> min, 836 buddy + sbi->s_mb_offsets[min]); 837 838 len -= chunk; 839 first += chunk; 840 } 841 } 842 843 static int mb_avg_fragment_size_order(struct super_block *sb, ext4_grpblk_t len) 844 { 845 int order; 846 847 /* 848 * We don't bother with a special lists groups with only 1 block free 849 * extents and for completely empty groups. 850 */ 851 order = fls(len) - 2; 852 if (order < 0) 853 return 0; 854 if (order == MB_NUM_ORDERS(sb)) 855 order--; 856 if (WARN_ON_ONCE(order > MB_NUM_ORDERS(sb))) 857 order = MB_NUM_ORDERS(sb) - 1; 858 return order; 859 } 860 861 /* Move group to appropriate avg_fragment_size list */ 862 static void 863 mb_update_avg_fragment_size(struct super_block *sb, struct ext4_group_info *grp) 864 { 865 struct ext4_sb_info *sbi = EXT4_SB(sb); 866 int new, old; 867 868 if (!test_opt2(sb, MB_OPTIMIZE_SCAN)) 869 return; 870 871 old = grp->bb_avg_fragment_size_order; 872 new = grp->bb_fragments == 0 ? -1 : 873 mb_avg_fragment_size_order(sb, grp->bb_free / grp->bb_fragments); 874 if (new == old) 875 return; 876 877 if (old >= 0) 878 xa_erase(&sbi->s_mb_avg_fragment_size[old], grp->bb_group); 879 880 grp->bb_avg_fragment_size_order = new; 881 if (new >= 0) { 882 /* 883 * Cannot use __GFP_NOFAIL because we hold the group lock. 884 * Although allocation for insertion may fails, it's not fatal 885 * as we have linear traversal to fall back on. 886 */ 887 int err = xa_insert(&sbi->s_mb_avg_fragment_size[new], 888 grp->bb_group, grp, GFP_ATOMIC); 889 if (err) 890 mb_debug(sb, "insert group: %u to s_mb_avg_fragment_size[%d] failed, err %d", 891 grp->bb_group, new, err); 892 } 893 } 894 895 static ext4_group_t ext4_get_allocation_groups_count( 896 struct ext4_allocation_context *ac) 897 { 898 ext4_group_t ngroups = ext4_get_groups_count(ac->ac_sb); 899 900 /* non-extent files are limited to low blocks/groups */ 901 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS))) 902 /* Pairs with smp_store_release() in ext4_update_super() */ 903 ngroups = smp_load_acquire(&EXT4_SB(ac->ac_sb)->s_blockfile_groups); 904 905 return ngroups; 906 } 907 908 static int ext4_mb_scan_groups_xa_range(struct ext4_allocation_context *ac, 909 struct xarray *xa, 910 ext4_group_t start, ext4_group_t end) 911 { 912 struct super_block *sb = ac->ac_sb; 913 struct ext4_sb_info *sbi = EXT4_SB(sb); 914 enum criteria cr = ac->ac_criteria; 915 ext4_group_t ngroups = ext4_get_allocation_groups_count(ac); 916 unsigned long group = start; 917 struct ext4_group_info *grp; 918 919 if (WARN_ON_ONCE(end > ngroups || start >= end)) 920 return 0; 921 922 xa_for_each_range(xa, group, grp, start, end - 1) { 923 int err; 924 925 if (sbi->s_mb_stats) 926 atomic64_inc(&sbi->s_bal_cX_groups_considered[cr]); 927 928 err = ext4_mb_scan_group(ac, grp->bb_group); 929 if (err || ac->ac_status != AC_STATUS_CONTINUE) 930 return err; 931 932 cond_resched(); 933 } 934 935 return 0; 936 } 937 938 /* 939 * Find a suitable group of given order from the largest free orders xarray. 940 */ 941 static inline int 942 ext4_mb_scan_groups_largest_free_order_range(struct ext4_allocation_context *ac, 943 int order, ext4_group_t start, 944 ext4_group_t end) 945 { 946 struct xarray *xa = &EXT4_SB(ac->ac_sb)->s_mb_largest_free_orders[order]; 947 948 if (xa_empty(xa)) 949 return 0; 950 951 return ext4_mb_scan_groups_xa_range(ac, xa, start, end); 952 } 953 954 /* 955 * Choose next group by traversing largest_free_order lists. Updates *new_cr if 956 * cr level needs an update. 957 */ 958 static int ext4_mb_scan_groups_p2_aligned(struct ext4_allocation_context *ac, 959 ext4_group_t group) 960 { 961 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 962 int i; 963 int ret = 0; 964 ext4_group_t start, end; 965 966 start = group; 967 end = ext4_get_allocation_groups_count(ac); 968 wrap_around: 969 for (i = ac->ac_2order; i < MB_NUM_ORDERS(ac->ac_sb); i++) { 970 ret = ext4_mb_scan_groups_largest_free_order_range(ac, i, 971 start, end); 972 if (ret || ac->ac_status != AC_STATUS_CONTINUE) 973 return ret; 974 } 975 if (start) { 976 end = start; 977 start = 0; 978 goto wrap_around; 979 } 980 981 if (sbi->s_mb_stats) 982 atomic64_inc(&sbi->s_bal_cX_failed[ac->ac_criteria]); 983 984 /* Increment cr and search again if no group is found */ 985 ac->ac_criteria = CR_GOAL_LEN_FAST; 986 return ret; 987 } 988 989 /* 990 * Find a suitable group of given order from the average fragments xarray. 991 */ 992 static int 993 ext4_mb_scan_groups_avg_frag_order_range(struct ext4_allocation_context *ac, 994 int order, ext4_group_t start, 995 ext4_group_t end) 996 { 997 struct xarray *xa = &EXT4_SB(ac->ac_sb)->s_mb_avg_fragment_size[order]; 998 999 if (xa_empty(xa)) 1000 return 0; 1001 1002 return ext4_mb_scan_groups_xa_range(ac, xa, start, end); 1003 } 1004 1005 /* 1006 * Choose next group by traversing average fragment size list of suitable 1007 * order. Updates *new_cr if cr level needs an update. 1008 */ 1009 static int ext4_mb_scan_groups_goal_fast(struct ext4_allocation_context *ac, 1010 ext4_group_t group) 1011 { 1012 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 1013 int i, ret = 0; 1014 ext4_group_t start, end; 1015 1016 start = group; 1017 end = ext4_get_allocation_groups_count(ac); 1018 wrap_around: 1019 i = mb_avg_fragment_size_order(ac->ac_sb, ac->ac_g_ex.fe_len); 1020 for (; i < MB_NUM_ORDERS(ac->ac_sb); i++) { 1021 ret = ext4_mb_scan_groups_avg_frag_order_range(ac, i, 1022 start, end); 1023 if (ret || ac->ac_status != AC_STATUS_CONTINUE) 1024 return ret; 1025 } 1026 if (start) { 1027 end = start; 1028 start = 0; 1029 goto wrap_around; 1030 } 1031 1032 if (sbi->s_mb_stats) 1033 atomic64_inc(&sbi->s_bal_cX_failed[ac->ac_criteria]); 1034 /* 1035 * CR_BEST_AVAIL_LEN works based on the concept that we have 1036 * a larger normalized goal len request which can be trimmed to 1037 * a smaller goal len such that it can still satisfy original 1038 * request len. However, allocation request for non-regular 1039 * files never gets normalized. 1040 * See function ext4_mb_normalize_request() (EXT4_MB_HINT_DATA). 1041 */ 1042 if (ac->ac_flags & EXT4_MB_HINT_DATA) 1043 ac->ac_criteria = CR_BEST_AVAIL_LEN; 1044 else 1045 ac->ac_criteria = CR_GOAL_LEN_SLOW; 1046 1047 return ret; 1048 } 1049 1050 /* 1051 * We couldn't find a group in CR_GOAL_LEN_FAST so try to find the highest free fragment 1052 * order we have and proactively trim the goal request length to that order to 1053 * find a suitable group faster. 1054 * 1055 * This optimizes allocation speed at the cost of slightly reduced 1056 * preallocations. However, we make sure that we don't trim the request too 1057 * much and fall to CR_GOAL_LEN_SLOW in that case. 1058 */ 1059 static int ext4_mb_scan_groups_best_avail(struct ext4_allocation_context *ac, 1060 ext4_group_t group) 1061 { 1062 int ret = 0; 1063 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 1064 int i, order, min_order; 1065 unsigned long num_stripe_clusters = 0; 1066 ext4_group_t start, end; 1067 1068 /* 1069 * mb_avg_fragment_size_order() returns order in a way that makes 1070 * retrieving back the length using (1 << order) inaccurate. Hence, use 1071 * fls() instead since we need to know the actual length while modifying 1072 * goal length. 1073 */ 1074 order = fls(ac->ac_g_ex.fe_len) - 1; 1075 if (WARN_ON_ONCE(order - 1 > MB_NUM_ORDERS(ac->ac_sb))) 1076 order = MB_NUM_ORDERS(ac->ac_sb); 1077 min_order = order - sbi->s_mb_best_avail_max_trim_order; 1078 if (min_order < 0) 1079 min_order = 0; 1080 1081 if (sbi->s_stripe > 0) { 1082 /* 1083 * We are assuming that stripe size is always a multiple of 1084 * cluster ratio otherwise __ext4_fill_super exists early. 1085 */ 1086 num_stripe_clusters = EXT4_NUM_B2C(sbi, sbi->s_stripe); 1087 if (1 << min_order < num_stripe_clusters) 1088 /* 1089 * We consider 1 order less because later we round 1090 * up the goal len to num_stripe_clusters 1091 */ 1092 min_order = fls(num_stripe_clusters) - 1; 1093 } 1094 1095 if (1 << min_order < ac->ac_o_ex.fe_len) 1096 min_order = fls(ac->ac_o_ex.fe_len); 1097 1098 start = group; 1099 end = ext4_get_allocation_groups_count(ac); 1100 wrap_around: 1101 for (i = order; i >= min_order; i--) { 1102 int frag_order; 1103 /* 1104 * Scale down goal len to make sure we find something 1105 * in the free fragments list. Basically, reduce 1106 * preallocations. 1107 */ 1108 ac->ac_g_ex.fe_len = 1 << i; 1109 1110 if (num_stripe_clusters > 0) { 1111 /* 1112 * Try to round up the adjusted goal length to 1113 * stripe size (in cluster units) multiple for 1114 * efficiency. 1115 */ 1116 ac->ac_g_ex.fe_len = roundup(ac->ac_g_ex.fe_len, 1117 num_stripe_clusters); 1118 } 1119 1120 frag_order = mb_avg_fragment_size_order(ac->ac_sb, 1121 ac->ac_g_ex.fe_len); 1122 1123 ret = ext4_mb_scan_groups_avg_frag_order_range(ac, frag_order, 1124 start, end); 1125 if (ret || ac->ac_status != AC_STATUS_CONTINUE) 1126 return ret; 1127 } 1128 if (start) { 1129 end = start; 1130 start = 0; 1131 goto wrap_around; 1132 } 1133 1134 /* Reset goal length to original goal length before falling into CR_GOAL_LEN_SLOW */ 1135 ac->ac_g_ex.fe_len = ac->ac_orig_goal_len; 1136 if (sbi->s_mb_stats) 1137 atomic64_inc(&sbi->s_bal_cX_failed[ac->ac_criteria]); 1138 ac->ac_criteria = CR_GOAL_LEN_SLOW; 1139 1140 return ret; 1141 } 1142 1143 static inline int should_optimize_scan(struct ext4_allocation_context *ac) 1144 { 1145 if (unlikely(!test_opt2(ac->ac_sb, MB_OPTIMIZE_SCAN))) 1146 return 0; 1147 if (ac->ac_criteria >= CR_GOAL_LEN_SLOW) 1148 return 0; 1149 return 1; 1150 } 1151 1152 /* 1153 * next linear group for allocation. 1154 */ 1155 static void next_linear_group(ext4_group_t *group, ext4_group_t ngroups) 1156 { 1157 /* 1158 * Artificially restricted ngroups for non-extent 1159 * files makes group > ngroups possible on first loop. 1160 */ 1161 *group = *group + 1 >= ngroups ? 0 : *group + 1; 1162 } 1163 1164 static int ext4_mb_scan_groups_linear(struct ext4_allocation_context *ac, 1165 ext4_group_t ngroups, ext4_group_t *start, ext4_group_t count) 1166 { 1167 int ret, i; 1168 enum criteria cr = ac->ac_criteria; 1169 struct super_block *sb = ac->ac_sb; 1170 struct ext4_sb_info *sbi = EXT4_SB(sb); 1171 ext4_group_t group = *start; 1172 1173 for (i = 0; i < count; i++, next_linear_group(&group, ngroups)) { 1174 ret = ext4_mb_scan_group(ac, group); 1175 if (ret || ac->ac_status != AC_STATUS_CONTINUE) 1176 return ret; 1177 cond_resched(); 1178 } 1179 1180 *start = group; 1181 if (count == ngroups) 1182 ac->ac_criteria++; 1183 1184 /* Processed all groups and haven't found blocks */ 1185 if (sbi->s_mb_stats && i == ngroups) 1186 atomic64_inc(&sbi->s_bal_cX_failed[cr]); 1187 1188 return 0; 1189 } 1190 1191 static int ext4_mb_scan_groups(struct ext4_allocation_context *ac) 1192 { 1193 int ret = 0; 1194 ext4_group_t start; 1195 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 1196 ext4_group_t ngroups = ext4_get_allocation_groups_count(ac); 1197 1198 /* searching for the right group start from the goal value specified */ 1199 start = ac->ac_g_ex.fe_group; 1200 if (start >= ngroups) 1201 start = 0; 1202 ac->ac_prefetch_grp = start; 1203 ac->ac_prefetch_nr = 0; 1204 1205 if (!should_optimize_scan(ac)) 1206 return ext4_mb_scan_groups_linear(ac, ngroups, &start, ngroups); 1207 1208 /* 1209 * Optimized scanning can return non adjacent groups which can cause 1210 * seek overhead for rotational disks. So try few linear groups before 1211 * trying optimized scan. 1212 */ 1213 if (sbi->s_mb_max_linear_groups) 1214 ret = ext4_mb_scan_groups_linear(ac, ngroups, &start, 1215 sbi->s_mb_max_linear_groups); 1216 if (ret || ac->ac_status != AC_STATUS_CONTINUE) 1217 return ret; 1218 1219 switch (ac->ac_criteria) { 1220 case CR_POWER2_ALIGNED: 1221 return ext4_mb_scan_groups_p2_aligned(ac, start); 1222 case CR_GOAL_LEN_FAST: 1223 return ext4_mb_scan_groups_goal_fast(ac, start); 1224 case CR_BEST_AVAIL_LEN: 1225 return ext4_mb_scan_groups_best_avail(ac, start); 1226 default: 1227 /* 1228 * TODO: For CR_GOAL_LEN_SLOW, we can arrange groups in an 1229 * rb tree sorted by bb_free. But until that happens, we should 1230 * never come here. 1231 */ 1232 WARN_ON(1); 1233 } 1234 1235 return 0; 1236 } 1237 1238 /* 1239 * Cache the order of the largest free extent we have available in this block 1240 * group. 1241 */ 1242 static void 1243 mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp) 1244 { 1245 struct ext4_sb_info *sbi = EXT4_SB(sb); 1246 int new, old = grp->bb_largest_free_order; 1247 1248 for (new = MB_NUM_ORDERS(sb) - 1; new >= 0; new--) 1249 if (grp->bb_counters[new] > 0) 1250 break; 1251 1252 /* No need to move between order lists? */ 1253 if (new == old) 1254 return; 1255 1256 if (old >= 0) { 1257 struct xarray *xa = &sbi->s_mb_largest_free_orders[old]; 1258 1259 if (!xa_empty(xa) && xa_load(xa, grp->bb_group)) 1260 xa_erase(xa, grp->bb_group); 1261 } 1262 1263 grp->bb_largest_free_order = new; 1264 if (test_opt2(sb, MB_OPTIMIZE_SCAN) && new >= 0 && grp->bb_free) { 1265 /* 1266 * Cannot use __GFP_NOFAIL because we hold the group lock. 1267 * Although allocation for insertion may fails, it's not fatal 1268 * as we have linear traversal to fall back on. 1269 */ 1270 int err = xa_insert(&sbi->s_mb_largest_free_orders[new], 1271 grp->bb_group, grp, GFP_ATOMIC); 1272 if (err) 1273 mb_debug(sb, "insert group: %u to s_mb_largest_free_orders[%d] failed, err %d", 1274 grp->bb_group, new, err); 1275 } 1276 } 1277 1278 static noinline_for_stack 1279 void ext4_mb_generate_buddy(struct super_block *sb, 1280 void *buddy, void *bitmap, ext4_group_t group, 1281 struct ext4_group_info *grp) 1282 { 1283 struct ext4_sb_info *sbi = EXT4_SB(sb); 1284 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb); 1285 ext4_grpblk_t i = 0; 1286 ext4_grpblk_t first; 1287 ext4_grpblk_t len; 1288 unsigned free = 0; 1289 unsigned fragments = 0; 1290 unsigned long long period = get_cycles(); 1291 1292 /* initialize buddy from bitmap which is aggregation 1293 * of on-disk bitmap and preallocations */ 1294 i = mb_find_next_zero_bit(bitmap, max, 0); 1295 grp->bb_first_free = i; 1296 while (i < max) { 1297 fragments++; 1298 first = i; 1299 i = mb_find_next_bit(bitmap, max, i); 1300 len = i - first; 1301 free += len; 1302 if (len > 1) 1303 ext4_mb_mark_free_simple(sb, buddy, first, len, grp); 1304 else 1305 grp->bb_counters[0]++; 1306 if (i < max) 1307 i = mb_find_next_zero_bit(bitmap, max, i); 1308 } 1309 grp->bb_fragments = fragments; 1310 1311 if (free != grp->bb_free) { 1312 ext4_grp_locked_error(sb, group, 0, 0, 1313 "block bitmap and bg descriptor " 1314 "inconsistent: %u vs %u free clusters", 1315 free, grp->bb_free); 1316 /* 1317 * If we intend to continue, we consider group descriptor 1318 * corrupt and update bb_free using bitmap value 1319 */ 1320 grp->bb_free = free; 1321 ext4_mark_group_bitmap_corrupted(sb, group, 1322 EXT4_GROUP_INFO_BBITMAP_CORRUPT); 1323 } 1324 mb_set_largest_free_order(sb, grp); 1325 mb_update_avg_fragment_size(sb, grp); 1326 1327 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state)); 1328 1329 period = get_cycles() - period; 1330 atomic_inc(&sbi->s_mb_buddies_generated); 1331 atomic64_add(period, &sbi->s_mb_generation_time); 1332 } 1333 1334 static void mb_regenerate_buddy(struct ext4_buddy *e4b) 1335 { 1336 int count; 1337 int order = 1; 1338 void *buddy; 1339 1340 while ((buddy = mb_find_buddy(e4b, order++, &count))) 1341 mb_set_bits(buddy, 0, count); 1342 1343 e4b->bd_info->bb_fragments = 0; 1344 memset(e4b->bd_info->bb_counters, 0, 1345 sizeof(*e4b->bd_info->bb_counters) * 1346 (e4b->bd_sb->s_blocksize_bits + 2)); 1347 1348 ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy, 1349 e4b->bd_bitmap, e4b->bd_group, e4b->bd_info); 1350 } 1351 1352 /* The buddy information is attached the buddy cache inode 1353 * for convenience. The information regarding each group 1354 * is loaded via ext4_mb_load_buddy. The information involve 1355 * block bitmap and buddy information. The information are 1356 * stored in the inode as 1357 * 1358 * { folio } 1359 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]... 1360 * 1361 * 1362 * one block each for bitmap and buddy information. 1363 * So for each group we take up 2 blocks. A folio can 1364 * contain blocks_per_folio (folio_size / blocksize) blocks. 1365 * So it can have information regarding groups_per_folio which 1366 * is blocks_per_folio/2 1367 * 1368 * Locking note: This routine takes the block group lock of all groups 1369 * for this folio; do not hold this lock when calling this routine! 1370 */ 1371 static int ext4_mb_init_cache(struct folio *folio, char *incore, gfp_t gfp) 1372 { 1373 ext4_group_t ngroups; 1374 unsigned int blocksize; 1375 int blocks_per_folio; 1376 int groups_per_folio; 1377 int err = 0; 1378 int i; 1379 ext4_group_t first_group, group; 1380 int first_block; 1381 struct super_block *sb; 1382 struct buffer_head *bhs; 1383 struct buffer_head **bh = NULL; 1384 struct inode *inode; 1385 char *data; 1386 char *bitmap; 1387 struct ext4_group_info *grinfo; 1388 1389 inode = folio->mapping->host; 1390 sb = inode->i_sb; 1391 ngroups = ext4_get_groups_count(sb); 1392 blocksize = i_blocksize(inode); 1393 blocks_per_folio = folio_size(folio) / blocksize; 1394 WARN_ON_ONCE(!blocks_per_folio); 1395 groups_per_folio = DIV_ROUND_UP(blocks_per_folio, 2); 1396 1397 mb_debug(sb, "init folio %lu\n", folio->index); 1398 1399 /* allocate buffer_heads to read bitmaps */ 1400 if (groups_per_folio > 1) { 1401 i = sizeof(struct buffer_head *) * groups_per_folio; 1402 bh = kzalloc(i, gfp); 1403 if (bh == NULL) 1404 return -ENOMEM; 1405 } else 1406 bh = &bhs; 1407 1408 /* read all groups the folio covers into the cache */ 1409 first_group = EXT4_PG_TO_LBLK(inode, folio->index) / 2; 1410 for (i = 0, group = first_group; i < groups_per_folio; i++, group++) { 1411 if (group >= ngroups) 1412 break; 1413 1414 grinfo = ext4_get_group_info(sb, group); 1415 if (!grinfo) 1416 continue; 1417 /* 1418 * If folio is uptodate then we came here after online resize 1419 * which added some new uninitialized group info structs, so 1420 * we must skip all initialized uptodate buddies on the folio, 1421 * which may be currently in use by an allocating task. 1422 */ 1423 if (folio_test_uptodate(folio) && 1424 !EXT4_MB_GRP_NEED_INIT(grinfo)) { 1425 bh[i] = NULL; 1426 continue; 1427 } 1428 bh[i] = ext4_read_block_bitmap_nowait(sb, group, false); 1429 if (IS_ERR(bh[i])) { 1430 err = PTR_ERR(bh[i]); 1431 bh[i] = NULL; 1432 goto out; 1433 } 1434 mb_debug(sb, "read bitmap for group %u\n", group); 1435 } 1436 1437 /* wait for I/O completion */ 1438 for (i = 0, group = first_group; i < groups_per_folio; i++, group++) { 1439 int err2; 1440 1441 if (!bh[i]) 1442 continue; 1443 err2 = ext4_wait_block_bitmap(sb, group, bh[i]); 1444 if (!err) 1445 err = err2; 1446 } 1447 1448 first_block = EXT4_PG_TO_LBLK(inode, folio->index); 1449 for (i = 0; i < blocks_per_folio; i++) { 1450 group = (first_block + i) >> 1; 1451 if (group >= ngroups) 1452 break; 1453 1454 if (!bh[group - first_group]) 1455 /* skip initialized uptodate buddy */ 1456 continue; 1457 1458 if (!buffer_verified(bh[group - first_group])) 1459 /* Skip faulty bitmaps */ 1460 continue; 1461 err = 0; 1462 1463 /* 1464 * data carry information regarding this 1465 * particular group in the format specified 1466 * above 1467 * 1468 */ 1469 data = folio_address(folio) + (i * blocksize); 1470 bitmap = bh[group - first_group]->b_data; 1471 1472 /* 1473 * We place the buddy block and bitmap block 1474 * close together 1475 */ 1476 grinfo = ext4_get_group_info(sb, group); 1477 if (!grinfo) { 1478 err = -EFSCORRUPTED; 1479 goto out; 1480 } 1481 if ((first_block + i) & 1) { 1482 /* this is block of buddy */ 1483 BUG_ON(incore == NULL); 1484 mb_debug(sb, "put buddy for group %u in folio %lu/%x\n", 1485 group, folio->index, i * blocksize); 1486 trace_ext4_mb_buddy_bitmap_load(sb, group); 1487 grinfo->bb_fragments = 0; 1488 memset(grinfo->bb_counters, 0, 1489 sizeof(*grinfo->bb_counters) * 1490 (MB_NUM_ORDERS(sb))); 1491 /* 1492 * incore got set to the group block bitmap below 1493 */ 1494 ext4_lock_group(sb, group); 1495 /* init the buddy */ 1496 memset(data, 0xff, blocksize); 1497 ext4_mb_generate_buddy(sb, data, incore, group, grinfo); 1498 ext4_unlock_group(sb, group); 1499 incore = NULL; 1500 } else { 1501 /* this is block of bitmap */ 1502 BUG_ON(incore != NULL); 1503 mb_debug(sb, "put bitmap for group %u in folio %lu/%x\n", 1504 group, folio->index, i * blocksize); 1505 trace_ext4_mb_bitmap_load(sb, group); 1506 1507 /* see comments in ext4_mb_put_pa() */ 1508 ext4_lock_group(sb, group); 1509 memcpy(data, bitmap, blocksize); 1510 1511 /* mark all preallocated blks used in in-core bitmap */ 1512 ext4_mb_generate_from_pa(sb, data, group); 1513 WARN_ON_ONCE(!RB_EMPTY_ROOT(&grinfo->bb_free_root)); 1514 ext4_unlock_group(sb, group); 1515 1516 /* set incore so that the buddy information can be 1517 * generated using this 1518 */ 1519 incore = data; 1520 } 1521 } 1522 folio_mark_uptodate(folio); 1523 1524 out: 1525 if (bh) { 1526 for (i = 0; i < groups_per_folio; i++) 1527 brelse(bh[i]); 1528 if (bh != &bhs) 1529 kfree(bh); 1530 } 1531 return err; 1532 } 1533 1534 /* 1535 * Lock the buddy and bitmap folios. This makes sure other parallel init_group 1536 * on the same buddy folio doesn't happen while holding the buddy folio lock. 1537 * Return locked buddy and bitmap folios on e4b struct. If buddy and bitmap 1538 * are on the same folio e4b->bd_buddy_folio is NULL and return value is 0. 1539 */ 1540 static int ext4_mb_get_buddy_folio_lock(struct super_block *sb, 1541 ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp) 1542 { 1543 struct inode *inode = EXT4_SB(sb)->s_buddy_cache; 1544 int block, pnum; 1545 struct folio *folio; 1546 1547 e4b->bd_buddy_folio = NULL; 1548 e4b->bd_bitmap_folio = NULL; 1549 1550 /* 1551 * the buddy cache inode stores the block bitmap 1552 * and buddy information in consecutive blocks. 1553 * So for each group we need two blocks. 1554 */ 1555 block = group * 2; 1556 pnum = EXT4_LBLK_TO_PG(inode, block); 1557 folio = __filemap_get_folio(inode->i_mapping, pnum, 1558 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp); 1559 if (IS_ERR(folio)) 1560 return PTR_ERR(folio); 1561 BUG_ON(folio->mapping != inode->i_mapping); 1562 WARN_ON_ONCE(folio_size(folio) < sb->s_blocksize); 1563 e4b->bd_bitmap_folio = folio; 1564 e4b->bd_bitmap = folio_address(folio) + 1565 offset_in_folio(folio, EXT4_LBLK_TO_B(inode, block)); 1566 1567 block++; 1568 pnum = EXT4_LBLK_TO_PG(inode, block); 1569 if (folio_contains(folio, pnum)) { 1570 /* buddy and bitmap are on the same folio */ 1571 return 0; 1572 } 1573 1574 /* we need another folio for the buddy */ 1575 folio = __filemap_get_folio(inode->i_mapping, pnum, 1576 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp); 1577 if (IS_ERR(folio)) 1578 return PTR_ERR(folio); 1579 BUG_ON(folio->mapping != inode->i_mapping); 1580 WARN_ON_ONCE(folio_size(folio) < sb->s_blocksize); 1581 e4b->bd_buddy_folio = folio; 1582 return 0; 1583 } 1584 1585 static void ext4_mb_put_buddy_folio_lock(struct ext4_buddy *e4b) 1586 { 1587 if (e4b->bd_bitmap_folio) { 1588 folio_unlock(e4b->bd_bitmap_folio); 1589 folio_put(e4b->bd_bitmap_folio); 1590 } 1591 if (e4b->bd_buddy_folio) { 1592 folio_unlock(e4b->bd_buddy_folio); 1593 folio_put(e4b->bd_buddy_folio); 1594 } 1595 } 1596 1597 /* 1598 * Locking note: This routine calls ext4_mb_init_cache(), which takes the 1599 * block group lock of all groups for this folio; do not hold the BG lock when 1600 * calling this routine! 1601 */ 1602 static noinline_for_stack 1603 int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp) 1604 { 1605 1606 struct ext4_group_info *this_grp; 1607 struct ext4_buddy e4b; 1608 struct folio *folio; 1609 int ret = 0; 1610 1611 might_sleep(); 1612 mb_debug(sb, "init group %u\n", group); 1613 this_grp = ext4_get_group_info(sb, group); 1614 if (!this_grp) 1615 return -EFSCORRUPTED; 1616 1617 /* 1618 * This ensures that we don't reinit the buddy cache 1619 * folio which map to the group from which we are already 1620 * allocating. If we are looking at the buddy cache we would 1621 * have taken a reference using ext4_mb_load_buddy and that 1622 * would have pinned buddy folio to page cache. 1623 * The call to ext4_mb_get_buddy_folio_lock will mark the 1624 * folio accessed. 1625 */ 1626 ret = ext4_mb_get_buddy_folio_lock(sb, group, &e4b, gfp); 1627 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) { 1628 /* 1629 * somebody initialized the group 1630 * return without doing anything 1631 */ 1632 goto err; 1633 } 1634 1635 folio = e4b.bd_bitmap_folio; 1636 ret = ext4_mb_init_cache(folio, NULL, gfp); 1637 if (ret) 1638 goto err; 1639 if (!folio_test_uptodate(folio)) { 1640 ret = -EIO; 1641 goto err; 1642 } 1643 1644 if (e4b.bd_buddy_folio == NULL) { 1645 /* 1646 * If both the bitmap and buddy are in 1647 * the same folio we don't need to force 1648 * init the buddy 1649 */ 1650 ret = 0; 1651 goto err; 1652 } 1653 /* init buddy cache */ 1654 folio = e4b.bd_buddy_folio; 1655 ret = ext4_mb_init_cache(folio, e4b.bd_bitmap, gfp); 1656 if (ret) 1657 goto err; 1658 if (!folio_test_uptodate(folio)) { 1659 ret = -EIO; 1660 goto err; 1661 } 1662 err: 1663 ext4_mb_put_buddy_folio_lock(&e4b); 1664 return ret; 1665 } 1666 1667 /* 1668 * Locking note: This routine calls ext4_mb_init_cache(), which takes the 1669 * block group lock of all groups for this folio; do not hold the BG lock when 1670 * calling this routine! 1671 */ 1672 static noinline_for_stack int 1673 ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group, 1674 struct ext4_buddy *e4b, gfp_t gfp) 1675 { 1676 int block; 1677 int pnum; 1678 struct folio *folio; 1679 int ret; 1680 struct ext4_group_info *grp; 1681 struct ext4_sb_info *sbi = EXT4_SB(sb); 1682 struct inode *inode = sbi->s_buddy_cache; 1683 1684 might_sleep(); 1685 mb_debug(sb, "load group %u\n", group); 1686 1687 grp = ext4_get_group_info(sb, group); 1688 if (!grp) 1689 return -EFSCORRUPTED; 1690 1691 e4b->bd_blkbits = sb->s_blocksize_bits; 1692 e4b->bd_info = grp; 1693 e4b->bd_sb = sb; 1694 e4b->bd_group = group; 1695 e4b->bd_buddy_folio = NULL; 1696 e4b->bd_bitmap_folio = NULL; 1697 1698 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { 1699 /* 1700 * we need full data about the group 1701 * to make a good selection 1702 */ 1703 ret = ext4_mb_init_group(sb, group, gfp); 1704 if (ret) 1705 return ret; 1706 } 1707 1708 /* 1709 * the buddy cache inode stores the block bitmap 1710 * and buddy information in consecutive blocks. 1711 * So for each group we need two blocks. 1712 */ 1713 block = group * 2; 1714 pnum = EXT4_LBLK_TO_PG(inode, block); 1715 1716 /* Avoid locking the folio in the fast path ... */ 1717 folio = __filemap_get_folio(inode->i_mapping, pnum, FGP_ACCESSED, 0); 1718 if (IS_ERR(folio) || !folio_test_uptodate(folio) || folio_test_locked(folio)) { 1719 /* 1720 * folio_test_locked is employed to detect ongoing folio 1721 * migrations, since concurrent migrations can lead to 1722 * bitmap inconsistency. And if we are not uptodate that 1723 * implies somebody just created the folio but is yet to 1724 * initialize it. We can drop the folio reference and 1725 * try to get the folio with lock in both cases to avoid 1726 * concurrency. 1727 */ 1728 if (!IS_ERR(folio)) 1729 folio_put(folio); 1730 folio = __filemap_get_folio(inode->i_mapping, pnum, 1731 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp); 1732 if (!IS_ERR(folio)) { 1733 if (WARN_RATELIMIT(folio->mapping != inode->i_mapping, 1734 "ext4: bitmap's mapping != inode->i_mapping\n")) { 1735 /* should never happen */ 1736 folio_unlock(folio); 1737 ret = -EINVAL; 1738 goto err; 1739 } 1740 if (!folio_test_uptodate(folio)) { 1741 ret = ext4_mb_init_cache(folio, NULL, gfp); 1742 if (ret) { 1743 folio_unlock(folio); 1744 goto err; 1745 } 1746 mb_cmp_bitmaps(e4b, folio_address(folio) + 1747 offset_in_folio(folio, 1748 EXT4_LBLK_TO_B(inode, block))); 1749 } 1750 folio_unlock(folio); 1751 } 1752 } 1753 if (IS_ERR(folio)) { 1754 ret = PTR_ERR(folio); 1755 goto err; 1756 } 1757 if (!folio_test_uptodate(folio)) { 1758 ret = -EIO; 1759 goto err; 1760 } 1761 1762 /* Folios marked accessed already */ 1763 e4b->bd_bitmap_folio = folio; 1764 e4b->bd_bitmap = folio_address(folio) + 1765 offset_in_folio(folio, EXT4_LBLK_TO_B(inode, block)); 1766 1767 block++; 1768 pnum = EXT4_LBLK_TO_PG(inode, block); 1769 /* buddy and bitmap are on the same folio? */ 1770 if (folio_contains(folio, pnum)) { 1771 folio_get(folio); 1772 goto update_buddy; 1773 } 1774 1775 /* we need another folio for the buddy */ 1776 folio = __filemap_get_folio(inode->i_mapping, pnum, FGP_ACCESSED, 0); 1777 if (IS_ERR(folio) || !folio_test_uptodate(folio) || folio_test_locked(folio)) { 1778 if (!IS_ERR(folio)) 1779 folio_put(folio); 1780 folio = __filemap_get_folio(inode->i_mapping, pnum, 1781 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp); 1782 if (!IS_ERR(folio)) { 1783 if (WARN_RATELIMIT(folio->mapping != inode->i_mapping, 1784 "ext4: buddy bitmap's mapping != inode->i_mapping\n")) { 1785 /* should never happen */ 1786 folio_unlock(folio); 1787 ret = -EINVAL; 1788 goto err; 1789 } 1790 if (!folio_test_uptodate(folio)) { 1791 ret = ext4_mb_init_cache(folio, e4b->bd_bitmap, 1792 gfp); 1793 if (ret) { 1794 folio_unlock(folio); 1795 goto err; 1796 } 1797 } 1798 folio_unlock(folio); 1799 } 1800 } 1801 if (IS_ERR(folio)) { 1802 ret = PTR_ERR(folio); 1803 goto err; 1804 } 1805 if (!folio_test_uptodate(folio)) { 1806 ret = -EIO; 1807 goto err; 1808 } 1809 1810 update_buddy: 1811 /* Folios marked accessed already */ 1812 e4b->bd_buddy_folio = folio; 1813 e4b->bd_buddy = folio_address(folio) + 1814 offset_in_folio(folio, EXT4_LBLK_TO_B(inode, block)); 1815 1816 return 0; 1817 1818 err: 1819 if (!IS_ERR_OR_NULL(folio)) 1820 folio_put(folio); 1821 if (e4b->bd_bitmap_folio) 1822 folio_put(e4b->bd_bitmap_folio); 1823 1824 e4b->bd_buddy = NULL; 1825 e4b->bd_bitmap = NULL; 1826 return ret; 1827 } 1828 1829 static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group, 1830 struct ext4_buddy *e4b) 1831 { 1832 return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS); 1833 } 1834 1835 static void ext4_mb_unload_buddy(struct ext4_buddy *e4b) 1836 { 1837 if (e4b->bd_bitmap_folio) 1838 folio_put(e4b->bd_bitmap_folio); 1839 if (e4b->bd_buddy_folio) 1840 folio_put(e4b->bd_buddy_folio); 1841 } 1842 1843 1844 static int mb_find_order_for_block(struct ext4_buddy *e4b, int block) 1845 { 1846 int order = 1, max; 1847 void *bb; 1848 1849 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy); 1850 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3))); 1851 1852 while (order <= e4b->bd_blkbits + 1) { 1853 bb = mb_find_buddy(e4b, order, &max); 1854 if (!mb_test_bit(block >> order, bb)) { 1855 /* this block is part of buddy of order 'order' */ 1856 return order; 1857 } 1858 order++; 1859 } 1860 return 0; 1861 } 1862 1863 static void mb_clear_bits(void *bm, int cur, int len) 1864 { 1865 __u32 *addr; 1866 1867 len = cur + len; 1868 while (cur < len) { 1869 if ((cur & 31) == 0 && (len - cur) >= 32) { 1870 /* fast path: clear whole word at once */ 1871 addr = bm + (cur >> 3); 1872 *addr = 0; 1873 cur += 32; 1874 continue; 1875 } 1876 mb_clear_bit(cur, bm); 1877 cur++; 1878 } 1879 } 1880 1881 /* clear bits in given range 1882 * will return first found zero bit if any, -1 otherwise 1883 */ 1884 static int mb_test_and_clear_bits(void *bm, int cur, int len) 1885 { 1886 __u32 *addr; 1887 int zero_bit = -1; 1888 1889 len = cur + len; 1890 while (cur < len) { 1891 if ((cur & 31) == 0 && (len - cur) >= 32) { 1892 /* fast path: clear whole word at once */ 1893 addr = bm + (cur >> 3); 1894 if (*addr != (__u32)(-1) && zero_bit == -1) 1895 zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0); 1896 *addr = 0; 1897 cur += 32; 1898 continue; 1899 } 1900 if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1) 1901 zero_bit = cur; 1902 cur++; 1903 } 1904 1905 return zero_bit; 1906 } 1907 1908 void mb_set_bits(void *bm, int cur, int len) 1909 { 1910 __u32 *addr; 1911 1912 len = cur + len; 1913 while (cur < len) { 1914 if ((cur & 31) == 0 && (len - cur) >= 32) { 1915 /* fast path: set whole word at once */ 1916 addr = bm + (cur >> 3); 1917 *addr = 0xffffffff; 1918 cur += 32; 1919 continue; 1920 } 1921 mb_set_bit(cur, bm); 1922 cur++; 1923 } 1924 } 1925 1926 static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side) 1927 { 1928 if (mb_test_bit(*bit + side, bitmap)) { 1929 mb_clear_bit(*bit, bitmap); 1930 (*bit) -= side; 1931 return 1; 1932 } 1933 else { 1934 (*bit) += side; 1935 mb_set_bit(*bit, bitmap); 1936 return -1; 1937 } 1938 } 1939 1940 static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last) 1941 { 1942 int max; 1943 int order = 1; 1944 void *buddy = mb_find_buddy(e4b, order, &max); 1945 1946 while (buddy) { 1947 void *buddy2; 1948 1949 /* Bits in range [first; last] are known to be set since 1950 * corresponding blocks were allocated. Bits in range 1951 * (first; last) will stay set because they form buddies on 1952 * upper layer. We just deal with borders if they don't 1953 * align with upper layer and then go up. 1954 * Releasing entire group is all about clearing 1955 * single bit of highest order buddy. 1956 */ 1957 1958 /* Example: 1959 * --------------------------------- 1960 * | 1 | 1 | 1 | 1 | 1961 * --------------------------------- 1962 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1963 * --------------------------------- 1964 * 0 1 2 3 4 5 6 7 1965 * \_____________________/ 1966 * 1967 * Neither [1] nor [6] is aligned to above layer. 1968 * Left neighbour [0] is free, so mark it busy, 1969 * decrease bb_counters and extend range to 1970 * [0; 6] 1971 * Right neighbour [7] is busy. It can't be coaleasced with [6], so 1972 * mark [6] free, increase bb_counters and shrink range to 1973 * [0; 5]. 1974 * Then shift range to [0; 2], go up and do the same. 1975 */ 1976 1977 1978 if (first & 1) 1979 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1); 1980 if (!(last & 1)) 1981 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1); 1982 if (first > last) 1983 break; 1984 order++; 1985 1986 buddy2 = mb_find_buddy(e4b, order, &max); 1987 if (!buddy2) { 1988 mb_clear_bits(buddy, first, last - first + 1); 1989 e4b->bd_info->bb_counters[order - 1] += last - first + 1; 1990 break; 1991 } 1992 first >>= 1; 1993 last >>= 1; 1994 buddy = buddy2; 1995 } 1996 } 1997 1998 static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b, 1999 int first, int count) 2000 { 2001 int left_is_free = 0; 2002 int right_is_free = 0; 2003 int block; 2004 int last = first + count - 1; 2005 struct super_block *sb = e4b->bd_sb; 2006 2007 if (WARN_ON(count == 0)) 2008 return; 2009 BUG_ON(last >= (sb->s_blocksize << 3)); 2010 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group)); 2011 /* Don't bother if the block group is corrupt. */ 2012 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) 2013 return; 2014 2015 mb_check_buddy(e4b); 2016 mb_free_blocks_double(inode, e4b, first, count); 2017 2018 /* access memory sequentially: check left neighbour, 2019 * clear range and then check right neighbour 2020 */ 2021 if (first != 0) 2022 left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap); 2023 block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count); 2024 if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0]) 2025 right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap); 2026 2027 if (unlikely(block != -1)) { 2028 struct ext4_sb_info *sbi = EXT4_SB(sb); 2029 ext4_fsblk_t blocknr; 2030 2031 /* 2032 * Fastcommit replay can free already freed blocks which 2033 * corrupts allocation info. Regenerate it. 2034 */ 2035 if (sbi->s_mount_state & EXT4_FC_REPLAY) { 2036 mb_regenerate_buddy(e4b); 2037 goto check; 2038 } 2039 2040 blocknr = ext4_group_first_block_no(sb, e4b->bd_group); 2041 blocknr += EXT4_C2B(sbi, block); 2042 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, 2043 EXT4_GROUP_INFO_BBITMAP_CORRUPT); 2044 ext4_grp_locked_error(sb, e4b->bd_group, 2045 inode ? inode->i_ino : 0, blocknr, 2046 "freeing already freed block (bit %u); block bitmap corrupt.", 2047 block); 2048 return; 2049 } 2050 2051 this_cpu_inc(discard_pa_seq); 2052 e4b->bd_info->bb_free += count; 2053 if (first < e4b->bd_info->bb_first_free) 2054 e4b->bd_info->bb_first_free = first; 2055 2056 /* let's maintain fragments counter */ 2057 if (left_is_free && right_is_free) 2058 e4b->bd_info->bb_fragments--; 2059 else if (!left_is_free && !right_is_free) 2060 e4b->bd_info->bb_fragments++; 2061 2062 /* buddy[0] == bd_bitmap is a special case, so handle 2063 * it right away and let mb_buddy_mark_free stay free of 2064 * zero order checks. 2065 * Check if neighbours are to be coaleasced, 2066 * adjust bitmap bb_counters and borders appropriately. 2067 */ 2068 if (first & 1) { 2069 first += !left_is_free; 2070 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1; 2071 } 2072 if (!(last & 1)) { 2073 last -= !right_is_free; 2074 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1; 2075 } 2076 2077 if (first <= last) 2078 mb_buddy_mark_free(e4b, first >> 1, last >> 1); 2079 2080 mb_set_largest_free_order(sb, e4b->bd_info); 2081 mb_update_avg_fragment_size(sb, e4b->bd_info); 2082 check: 2083 mb_check_buddy(e4b); 2084 } 2085 2086 static int mb_find_extent(struct ext4_buddy *e4b, int block, 2087 int needed, struct ext4_free_extent *ex) 2088 { 2089 int max, order, next; 2090 void *buddy; 2091 2092 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); 2093 BUG_ON(ex == NULL); 2094 2095 buddy = mb_find_buddy(e4b, 0, &max); 2096 BUG_ON(buddy == NULL); 2097 BUG_ON(block >= max); 2098 if (mb_test_bit(block, buddy)) { 2099 ex->fe_len = 0; 2100 ex->fe_start = 0; 2101 ex->fe_group = 0; 2102 return 0; 2103 } 2104 2105 /* find actual order */ 2106 order = mb_find_order_for_block(e4b, block); 2107 2108 ex->fe_len = (1 << order) - (block & ((1 << order) - 1)); 2109 ex->fe_start = block; 2110 ex->fe_group = e4b->bd_group; 2111 2112 block = block >> order; 2113 2114 while (needed > ex->fe_len && 2115 mb_find_buddy(e4b, order, &max)) { 2116 2117 if (block + 1 >= max) 2118 break; 2119 2120 next = (block + 1) * (1 << order); 2121 if (mb_test_bit(next, e4b->bd_bitmap)) 2122 break; 2123 2124 order = mb_find_order_for_block(e4b, next); 2125 2126 block = next >> order; 2127 ex->fe_len += 1 << order; 2128 } 2129 2130 if (ex->fe_start + ex->fe_len > EXT4_CLUSTERS_PER_GROUP(e4b->bd_sb)) { 2131 /* Should never happen! (but apparently sometimes does?!?) */ 2132 WARN_ON(1); 2133 ext4_grp_locked_error(e4b->bd_sb, e4b->bd_group, 0, 0, 2134 "corruption or bug in mb_find_extent " 2135 "block=%d, order=%d needed=%d ex=%u/%d/%d@%u", 2136 block, order, needed, ex->fe_group, ex->fe_start, 2137 ex->fe_len, ex->fe_logical); 2138 ex->fe_len = 0; 2139 ex->fe_start = 0; 2140 ex->fe_group = 0; 2141 } 2142 return ex->fe_len; 2143 } 2144 2145 static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex) 2146 { 2147 int ord; 2148 int mlen = 0; 2149 int max = 0; 2150 int start = ex->fe_start; 2151 int len = ex->fe_len; 2152 unsigned ret = 0; 2153 int len0 = len; 2154 void *buddy; 2155 int ord_start, ord_end; 2156 2157 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3)); 2158 BUG_ON(e4b->bd_group != ex->fe_group); 2159 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group)); 2160 mb_check_buddy(e4b); 2161 mb_mark_used_double(e4b, start, len); 2162 2163 this_cpu_inc(discard_pa_seq); 2164 e4b->bd_info->bb_free -= len; 2165 if (e4b->bd_info->bb_first_free == start) 2166 e4b->bd_info->bb_first_free += len; 2167 2168 /* let's maintain fragments counter */ 2169 if (start != 0) 2170 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap); 2171 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0]) 2172 max = !mb_test_bit(start + len, e4b->bd_bitmap); 2173 if (mlen && max) 2174 e4b->bd_info->bb_fragments++; 2175 else if (!mlen && !max) 2176 e4b->bd_info->bb_fragments--; 2177 2178 /* let's maintain buddy itself */ 2179 while (len) { 2180 ord = mb_find_order_for_block(e4b, start); 2181 2182 if (((start >> ord) << ord) == start && len >= (1 << ord)) { 2183 /* the whole chunk may be allocated at once! */ 2184 mlen = 1 << ord; 2185 buddy = mb_find_buddy(e4b, ord, &max); 2186 BUG_ON((start >> ord) >= max); 2187 mb_set_bit(start >> ord, buddy); 2188 e4b->bd_info->bb_counters[ord]--; 2189 start += mlen; 2190 len -= mlen; 2191 BUG_ON(len < 0); 2192 continue; 2193 } 2194 2195 /* store for history */ 2196 if (ret == 0) 2197 ret = len | (ord << 16); 2198 2199 BUG_ON(ord <= 0); 2200 buddy = mb_find_buddy(e4b, ord, &max); 2201 mb_set_bit(start >> ord, buddy); 2202 e4b->bd_info->bb_counters[ord]--; 2203 2204 ord_start = (start >> ord) << ord; 2205 ord_end = ord_start + (1 << ord); 2206 /* first chunk */ 2207 if (start > ord_start) 2208 ext4_mb_mark_free_simple(e4b->bd_sb, e4b->bd_buddy, 2209 ord_start, start - ord_start, 2210 e4b->bd_info); 2211 2212 /* last chunk */ 2213 if (start + len < ord_end) { 2214 ext4_mb_mark_free_simple(e4b->bd_sb, e4b->bd_buddy, 2215 start + len, 2216 ord_end - (start + len), 2217 e4b->bd_info); 2218 break; 2219 } 2220 len = start + len - ord_end; 2221 start = ord_end; 2222 } 2223 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info); 2224 2225 mb_update_avg_fragment_size(e4b->bd_sb, e4b->bd_info); 2226 mb_set_bits(e4b->bd_bitmap, ex->fe_start, len0); 2227 mb_check_buddy(e4b); 2228 2229 return ret; 2230 } 2231 2232 /* 2233 * Must be called under group lock! 2234 */ 2235 static void ext4_mb_use_best_found(struct ext4_allocation_context *ac, 2236 struct ext4_buddy *e4b) 2237 { 2238 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 2239 int ret; 2240 2241 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group); 2242 BUG_ON(ac->ac_status == AC_STATUS_FOUND); 2243 2244 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len); 2245 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical; 2246 ret = mb_mark_used(e4b, &ac->ac_b_ex); 2247 2248 /* preallocation can change ac_b_ex, thus we store actually 2249 * allocated blocks for history */ 2250 ac->ac_f_ex = ac->ac_b_ex; 2251 2252 ac->ac_status = AC_STATUS_FOUND; 2253 ac->ac_tail = ret & 0xffff; 2254 ac->ac_buddy = ret >> 16; 2255 2256 /* 2257 * take the folio reference. We want the folio to be pinned 2258 * so that we don't get a ext4_mb_init_cache_call for this 2259 * group until we update the bitmap. That would mean we 2260 * double allocate blocks. The reference is dropped 2261 * in ext4_mb_release_context 2262 */ 2263 ac->ac_bitmap_folio = e4b->bd_bitmap_folio; 2264 folio_get(ac->ac_bitmap_folio); 2265 ac->ac_buddy_folio = e4b->bd_buddy_folio; 2266 folio_get(ac->ac_buddy_folio); 2267 /* store last allocated for subsequent stream allocation */ 2268 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { 2269 int hash = (unsigned int)ac->ac_inode->i_ino % sbi->s_mb_nr_global_goals; 2270 2271 WRITE_ONCE(sbi->s_mb_last_groups[hash], ac->ac_f_ex.fe_group); 2272 } 2273 2274 /* 2275 * As we've just preallocated more space than 2276 * user requested originally, we store allocated 2277 * space in a special descriptor. 2278 */ 2279 if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len) 2280 ext4_mb_new_preallocation(ac); 2281 2282 } 2283 2284 static void ext4_mb_check_limits(struct ext4_allocation_context *ac, 2285 struct ext4_buddy *e4b, 2286 int finish_group) 2287 { 2288 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 2289 struct ext4_free_extent *bex = &ac->ac_b_ex; 2290 struct ext4_free_extent *gex = &ac->ac_g_ex; 2291 2292 if (ac->ac_status == AC_STATUS_FOUND) 2293 return; 2294 /* 2295 * We don't want to scan for a whole year 2296 */ 2297 if (ac->ac_found > sbi->s_mb_max_to_scan && 2298 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { 2299 ac->ac_status = AC_STATUS_BREAK; 2300 return; 2301 } 2302 2303 /* 2304 * Haven't found good chunk so far, let's continue 2305 */ 2306 if (bex->fe_len < gex->fe_len) 2307 return; 2308 2309 if (finish_group || ac->ac_found > sbi->s_mb_min_to_scan) 2310 ext4_mb_use_best_found(ac, e4b); 2311 } 2312 2313 /* 2314 * The routine checks whether found extent is good enough. If it is, 2315 * then the extent gets marked used and flag is set to the context 2316 * to stop scanning. Otherwise, the extent is compared with the 2317 * previous found extent and if new one is better, then it's stored 2318 * in the context. Later, the best found extent will be used, if 2319 * mballoc can't find good enough extent. 2320 * 2321 * The algorithm used is roughly as follows: 2322 * 2323 * * If free extent found is exactly as big as goal, then 2324 * stop the scan and use it immediately 2325 * 2326 * * If free extent found is smaller than goal, then keep retrying 2327 * upto a max of sbi->s_mb_max_to_scan times (default 200). After 2328 * that stop scanning and use whatever we have. 2329 * 2330 * * If free extent found is bigger than goal, then keep retrying 2331 * upto a max of sbi->s_mb_min_to_scan times (default 10) before 2332 * stopping the scan and using the extent. 2333 * 2334 * 2335 * FIXME: real allocation policy is to be designed yet! 2336 */ 2337 static void ext4_mb_measure_extent(struct ext4_allocation_context *ac, 2338 struct ext4_free_extent *ex, 2339 struct ext4_buddy *e4b) 2340 { 2341 struct ext4_free_extent *bex = &ac->ac_b_ex; 2342 struct ext4_free_extent *gex = &ac->ac_g_ex; 2343 2344 BUG_ON(ex->fe_len <= 0); 2345 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); 2346 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb)); 2347 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE); 2348 2349 ac->ac_found++; 2350 ac->ac_cX_found[ac->ac_criteria]++; 2351 2352 /* 2353 * The special case - take what you catch first 2354 */ 2355 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) { 2356 *bex = *ex; 2357 ext4_mb_use_best_found(ac, e4b); 2358 return; 2359 } 2360 2361 /* 2362 * Let's check whether the chuck is good enough 2363 */ 2364 if (ex->fe_len == gex->fe_len) { 2365 *bex = *ex; 2366 ext4_mb_use_best_found(ac, e4b); 2367 return; 2368 } 2369 2370 /* 2371 * If this is first found extent, just store it in the context 2372 */ 2373 if (bex->fe_len == 0) { 2374 *bex = *ex; 2375 return; 2376 } 2377 2378 /* 2379 * If new found extent is better, store it in the context 2380 */ 2381 if (bex->fe_len < gex->fe_len) { 2382 /* if the request isn't satisfied, any found extent 2383 * larger than previous best one is better */ 2384 if (ex->fe_len > bex->fe_len) 2385 *bex = *ex; 2386 } else if (ex->fe_len > gex->fe_len) { 2387 /* if the request is satisfied, then we try to find 2388 * an extent that still satisfy the request, but is 2389 * smaller than previous one */ 2390 if (ex->fe_len < bex->fe_len) 2391 *bex = *ex; 2392 } 2393 2394 ext4_mb_check_limits(ac, e4b, 0); 2395 } 2396 2397 static noinline_for_stack 2398 void ext4_mb_try_best_found(struct ext4_allocation_context *ac, 2399 struct ext4_buddy *e4b) 2400 { 2401 struct ext4_free_extent ex = ac->ac_b_ex; 2402 ext4_group_t group = ex.fe_group; 2403 int max; 2404 int err; 2405 2406 BUG_ON(ex.fe_len <= 0); 2407 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); 2408 if (err) 2409 return; 2410 2411 ext4_lock_group(ac->ac_sb, group); 2412 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) 2413 goto out; 2414 2415 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex); 2416 2417 if (max > 0) { 2418 ac->ac_b_ex = ex; 2419 ext4_mb_use_best_found(ac, e4b); 2420 } 2421 2422 out: 2423 ext4_unlock_group(ac->ac_sb, group); 2424 ext4_mb_unload_buddy(e4b); 2425 } 2426 2427 static noinline_for_stack 2428 int ext4_mb_find_by_goal(struct ext4_allocation_context *ac, 2429 struct ext4_buddy *e4b) 2430 { 2431 ext4_group_t group = ac->ac_g_ex.fe_group; 2432 int max; 2433 int err; 2434 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 2435 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); 2436 struct ext4_free_extent ex; 2437 2438 if (!grp) 2439 return -EFSCORRUPTED; 2440 if (!(ac->ac_flags & (EXT4_MB_HINT_TRY_GOAL | EXT4_MB_HINT_GOAL_ONLY))) 2441 return 0; 2442 if (grp->bb_free == 0) 2443 return 0; 2444 2445 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b); 2446 if (err) { 2447 if (EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info) && 2448 !(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) 2449 return 0; 2450 return err; 2451 } 2452 2453 ext4_lock_group(ac->ac_sb, group); 2454 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) 2455 goto out; 2456 2457 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start, 2458 ac->ac_g_ex.fe_len, &ex); 2459 ex.fe_logical = 0xDEADFA11; /* debug value */ 2460 2461 if (max >= ac->ac_g_ex.fe_len && 2462 ac->ac_g_ex.fe_len == EXT4_NUM_B2C(sbi, sbi->s_stripe)) { 2463 ext4_fsblk_t start; 2464 2465 start = ext4_grp_offs_to_block(ac->ac_sb, &ex); 2466 /* use do_div to get remainder (would be 64-bit modulo) */ 2467 if (do_div(start, sbi->s_stripe) == 0) { 2468 ac->ac_found++; 2469 ac->ac_b_ex = ex; 2470 ext4_mb_use_best_found(ac, e4b); 2471 } 2472 } else if (max >= ac->ac_g_ex.fe_len) { 2473 BUG_ON(ex.fe_len <= 0); 2474 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); 2475 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); 2476 ac->ac_found++; 2477 ac->ac_b_ex = ex; 2478 ext4_mb_use_best_found(ac, e4b); 2479 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) { 2480 /* Sometimes, caller may want to merge even small 2481 * number of blocks to an existing extent */ 2482 BUG_ON(ex.fe_len <= 0); 2483 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group); 2484 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start); 2485 ac->ac_found++; 2486 ac->ac_b_ex = ex; 2487 ext4_mb_use_best_found(ac, e4b); 2488 } 2489 out: 2490 ext4_unlock_group(ac->ac_sb, group); 2491 ext4_mb_unload_buddy(e4b); 2492 2493 return 0; 2494 } 2495 2496 /* 2497 * The routine scans buddy structures (not bitmap!) from given order 2498 * to max order and tries to find big enough chunk to satisfy the req 2499 */ 2500 static noinline_for_stack 2501 void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac, 2502 struct ext4_buddy *e4b) 2503 { 2504 struct super_block *sb = ac->ac_sb; 2505 struct ext4_group_info *grp = e4b->bd_info; 2506 void *buddy; 2507 int i; 2508 int k; 2509 int max; 2510 2511 BUG_ON(ac->ac_2order <= 0); 2512 for (i = ac->ac_2order; i < MB_NUM_ORDERS(sb); i++) { 2513 if (grp->bb_counters[i] == 0) 2514 continue; 2515 2516 buddy = mb_find_buddy(e4b, i, &max); 2517 if (WARN_RATELIMIT(buddy == NULL, 2518 "ext4: mb_simple_scan_group: mb_find_buddy failed, (%d)\n", i)) 2519 continue; 2520 2521 k = mb_find_next_zero_bit(buddy, max, 0); 2522 if (k >= max) { 2523 ext4_mark_group_bitmap_corrupted(ac->ac_sb, 2524 e4b->bd_group, 2525 EXT4_GROUP_INFO_BBITMAP_CORRUPT); 2526 ext4_grp_locked_error(ac->ac_sb, e4b->bd_group, 0, 0, 2527 "%d free clusters of order %d. But found 0", 2528 grp->bb_counters[i], i); 2529 break; 2530 } 2531 ac->ac_found++; 2532 ac->ac_cX_found[ac->ac_criteria]++; 2533 2534 ac->ac_b_ex.fe_len = 1 << i; 2535 ac->ac_b_ex.fe_start = k << i; 2536 ac->ac_b_ex.fe_group = e4b->bd_group; 2537 2538 ext4_mb_use_best_found(ac, e4b); 2539 2540 BUG_ON(ac->ac_f_ex.fe_len != ac->ac_g_ex.fe_len); 2541 2542 if (EXT4_SB(sb)->s_mb_stats) 2543 atomic_inc(&EXT4_SB(sb)->s_bal_2orders); 2544 2545 break; 2546 } 2547 } 2548 2549 /* 2550 * The routine scans the group and measures all found extents. 2551 * In order to optimize scanning, caller must pass number of 2552 * free blocks in the group, so the routine can know upper limit. 2553 */ 2554 static noinline_for_stack 2555 void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac, 2556 struct ext4_buddy *e4b) 2557 { 2558 struct super_block *sb = ac->ac_sb; 2559 void *bitmap = e4b->bd_bitmap; 2560 struct ext4_free_extent ex; 2561 int i, j, freelen; 2562 int free; 2563 2564 free = e4b->bd_info->bb_free; 2565 if (WARN_ON(free <= 0)) 2566 return; 2567 2568 i = e4b->bd_info->bb_first_free; 2569 2570 while (free && ac->ac_status == AC_STATUS_CONTINUE) { 2571 i = mb_find_next_zero_bit(bitmap, 2572 EXT4_CLUSTERS_PER_GROUP(sb), i); 2573 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) { 2574 /* 2575 * IF we have corrupt bitmap, we won't find any 2576 * free blocks even though group info says we 2577 * have free blocks 2578 */ 2579 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, 2580 EXT4_GROUP_INFO_BBITMAP_CORRUPT); 2581 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, 2582 "%d free clusters as per " 2583 "group info. But bitmap says 0", 2584 free); 2585 break; 2586 } 2587 2588 if (!ext4_mb_cr_expensive(ac->ac_criteria)) { 2589 /* 2590 * In CR_GOAL_LEN_FAST and CR_BEST_AVAIL_LEN, we are 2591 * sure that this group will have a large enough 2592 * continuous free extent, so skip over the smaller free 2593 * extents 2594 */ 2595 j = mb_find_next_bit(bitmap, 2596 EXT4_CLUSTERS_PER_GROUP(sb), i); 2597 freelen = j - i; 2598 2599 if (freelen < ac->ac_g_ex.fe_len) { 2600 i = j; 2601 free -= freelen; 2602 continue; 2603 } 2604 } 2605 2606 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex); 2607 if (WARN_ON(ex.fe_len <= 0)) 2608 break; 2609 if (free < ex.fe_len) { 2610 ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group, 2611 EXT4_GROUP_INFO_BBITMAP_CORRUPT); 2612 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0, 2613 "%d free clusters as per " 2614 "group info. But got %d blocks", 2615 free, ex.fe_len); 2616 /* 2617 * The number of free blocks differs. This mostly 2618 * indicate that the bitmap is corrupt. So exit 2619 * without claiming the space. 2620 */ 2621 break; 2622 } 2623 ex.fe_logical = 0xDEADC0DE; /* debug value */ 2624 ext4_mb_measure_extent(ac, &ex, e4b); 2625 2626 i += ex.fe_len; 2627 free -= ex.fe_len; 2628 } 2629 2630 ext4_mb_check_limits(ac, e4b, 1); 2631 } 2632 2633 /* 2634 * This is a special case for storages like raid5 2635 * we try to find stripe-aligned chunks for stripe-size-multiple requests 2636 */ 2637 static noinline_for_stack 2638 void ext4_mb_scan_aligned(struct ext4_allocation_context *ac, 2639 struct ext4_buddy *e4b) 2640 { 2641 struct super_block *sb = ac->ac_sb; 2642 struct ext4_sb_info *sbi = EXT4_SB(sb); 2643 void *bitmap = e4b->bd_bitmap; 2644 struct ext4_free_extent ex; 2645 ext4_fsblk_t first_group_block; 2646 ext4_fsblk_t a; 2647 ext4_grpblk_t i, stripe; 2648 int max; 2649 2650 BUG_ON(sbi->s_stripe == 0); 2651 2652 /* find first stripe-aligned block in group */ 2653 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group); 2654 2655 a = first_group_block + sbi->s_stripe - 1; 2656 do_div(a, sbi->s_stripe); 2657 i = (a * sbi->s_stripe) - first_group_block; 2658 2659 stripe = EXT4_NUM_B2C(sbi, sbi->s_stripe); 2660 i = EXT4_B2C(sbi, i); 2661 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) { 2662 if (!mb_test_bit(i, bitmap)) { 2663 max = mb_find_extent(e4b, i, stripe, &ex); 2664 if (max >= stripe) { 2665 ac->ac_found++; 2666 ac->ac_cX_found[ac->ac_criteria]++; 2667 ex.fe_logical = 0xDEADF00D; /* debug value */ 2668 ac->ac_b_ex = ex; 2669 ext4_mb_use_best_found(ac, e4b); 2670 break; 2671 } 2672 } 2673 i += stripe; 2674 } 2675 } 2676 2677 static void __ext4_mb_scan_group(struct ext4_allocation_context *ac) 2678 { 2679 bool is_stripe_aligned; 2680 struct ext4_sb_info *sbi; 2681 enum criteria cr = ac->ac_criteria; 2682 2683 ac->ac_groups_scanned++; 2684 if (cr == CR_POWER2_ALIGNED) 2685 return ext4_mb_simple_scan_group(ac, ac->ac_e4b); 2686 2687 sbi = EXT4_SB(ac->ac_sb); 2688 is_stripe_aligned = false; 2689 if ((sbi->s_stripe >= sbi->s_cluster_ratio) && 2690 !(ac->ac_g_ex.fe_len % EXT4_NUM_B2C(sbi, sbi->s_stripe))) 2691 is_stripe_aligned = true; 2692 2693 if ((cr == CR_GOAL_LEN_FAST || cr == CR_BEST_AVAIL_LEN) && 2694 is_stripe_aligned) 2695 ext4_mb_scan_aligned(ac, ac->ac_e4b); 2696 2697 if (ac->ac_status == AC_STATUS_CONTINUE) 2698 ext4_mb_complex_scan_group(ac, ac->ac_e4b); 2699 } 2700 2701 /* 2702 * This is also called BEFORE we load the buddy bitmap. 2703 * Returns either 1 or 0 indicating that the group is either suitable 2704 * for the allocation or not. 2705 */ 2706 static bool ext4_mb_good_group(struct ext4_allocation_context *ac, 2707 ext4_group_t group, enum criteria cr) 2708 { 2709 ext4_grpblk_t free, fragments; 2710 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb)); 2711 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); 2712 2713 BUG_ON(cr < CR_POWER2_ALIGNED || cr >= EXT4_MB_NUM_CRS); 2714 2715 if (unlikely(!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) 2716 return false; 2717 2718 free = grp->bb_free; 2719 if (free == 0) 2720 return false; 2721 2722 fragments = grp->bb_fragments; 2723 if (fragments == 0) 2724 return false; 2725 2726 switch (cr) { 2727 case CR_POWER2_ALIGNED: 2728 BUG_ON(ac->ac_2order == 0); 2729 2730 /* Avoid using the first bg of a flexgroup for data files */ 2731 if ((ac->ac_flags & EXT4_MB_HINT_DATA) && 2732 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) && 2733 ((group % flex_size) == 0)) 2734 return false; 2735 2736 if (free < ac->ac_g_ex.fe_len) 2737 return false; 2738 2739 if (ac->ac_2order >= MB_NUM_ORDERS(ac->ac_sb)) 2740 return true; 2741 2742 if (grp->bb_largest_free_order < ac->ac_2order) 2743 return false; 2744 2745 return true; 2746 case CR_GOAL_LEN_FAST: 2747 case CR_BEST_AVAIL_LEN: 2748 if ((free / fragments) >= ac->ac_g_ex.fe_len) 2749 return true; 2750 break; 2751 case CR_GOAL_LEN_SLOW: 2752 if (free >= ac->ac_g_ex.fe_len) 2753 return true; 2754 break; 2755 case CR_ANY_FREE: 2756 return true; 2757 default: 2758 BUG(); 2759 } 2760 2761 return false; 2762 } 2763 2764 /* 2765 * This could return negative error code if something goes wrong 2766 * during ext4_mb_init_group(). This should not be called with 2767 * ext4_lock_group() held. 2768 * 2769 * Note: because we are conditionally operating with the group lock in 2770 * the EXT4_MB_STRICT_CHECK case, we need to fake out sparse in this 2771 * function using __acquire and __release. This means we need to be 2772 * super careful before messing with the error path handling via "goto 2773 * out"! 2774 */ 2775 static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac, 2776 ext4_group_t group, enum criteria cr) 2777 { 2778 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group); 2779 struct super_block *sb = ac->ac_sb; 2780 struct ext4_sb_info *sbi = EXT4_SB(sb); 2781 bool should_lock = ac->ac_flags & EXT4_MB_STRICT_CHECK; 2782 ext4_grpblk_t free; 2783 int ret = 0; 2784 2785 if (!grp) 2786 return -EFSCORRUPTED; 2787 if (sbi->s_mb_stats) 2788 atomic64_inc(&sbi->s_bal_cX_groups_considered[ac->ac_criteria]); 2789 if (should_lock) { 2790 ext4_lock_group(sb, group); 2791 __release(ext4_group_lock_ptr(sb, group)); 2792 } 2793 free = grp->bb_free; 2794 if (free == 0) 2795 goto out; 2796 /* 2797 * In all criterias except CR_ANY_FREE we try to avoid groups that 2798 * can't possibly satisfy the full goal request due to insufficient 2799 * free blocks. 2800 */ 2801 if (cr < CR_ANY_FREE && free < ac->ac_g_ex.fe_len) 2802 goto out; 2803 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) 2804 goto out; 2805 if (should_lock) { 2806 __acquire(ext4_group_lock_ptr(sb, group)); 2807 ext4_unlock_group(sb, group); 2808 } 2809 2810 /* We only do this if the grp has never been initialized */ 2811 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { 2812 struct ext4_group_desc *gdp = 2813 ext4_get_group_desc(sb, group, NULL); 2814 int ret; 2815 2816 /* 2817 * CR_POWER2_ALIGNED/CR_GOAL_LEN_FAST is a very optimistic 2818 * search to find large good chunks almost for free. If buddy 2819 * data is not ready, then this optimization makes no sense. But 2820 * we never skip the first block group in a flex_bg, since this 2821 * gets used for metadata block allocation, and we want to make 2822 * sure we locate metadata blocks in the first block group in 2823 * the flex_bg if possible. 2824 */ 2825 if (!ext4_mb_cr_expensive(cr) && 2826 (!sbi->s_log_groups_per_flex || 2827 ((group & ((1 << sbi->s_log_groups_per_flex) - 1)) != 0)) && 2828 !(ext4_has_group_desc_csum(sb) && 2829 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))) 2830 return 0; 2831 ret = ext4_mb_init_group(sb, group, GFP_NOFS); 2832 if (ret) 2833 return ret; 2834 } 2835 2836 if (should_lock) { 2837 ext4_lock_group(sb, group); 2838 __release(ext4_group_lock_ptr(sb, group)); 2839 } 2840 ret = ext4_mb_good_group(ac, group, cr); 2841 out: 2842 if (should_lock) { 2843 __acquire(ext4_group_lock_ptr(sb, group)); 2844 ext4_unlock_group(sb, group); 2845 } 2846 return ret; 2847 } 2848 2849 /* 2850 * Start prefetching @nr block bitmaps starting at @group. 2851 * Return the next group which needs to be prefetched. 2852 */ 2853 ext4_group_t ext4_mb_prefetch(struct super_block *sb, ext4_group_t group, 2854 unsigned int nr, int *cnt) 2855 { 2856 ext4_group_t ngroups = ext4_get_groups_count(sb); 2857 struct buffer_head *bh; 2858 struct blk_plug plug; 2859 2860 blk_start_plug(&plug); 2861 while (nr-- > 0) { 2862 struct ext4_group_info *grp = ext4_get_group_info(sb, group); 2863 2864 /* 2865 * Prefetch block groups with free blocks; but don't 2866 * bother if it is marked uninitialized on disk, since 2867 * it won't require I/O to read. Also only try to 2868 * prefetch once, so we avoid getblk() call, which can 2869 * be expensive. 2870 */ 2871 if (grp && !EXT4_MB_GRP_TEST_AND_SET_READ(grp) && 2872 EXT4_MB_GRP_NEED_INIT(grp)) { 2873 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); 2874 2875 if (gdp && ext4_free_group_clusters(sb, gdp) > 0) { 2876 bh = ext4_read_block_bitmap_nowait(sb, group, true); 2877 if (!IS_ERR_OR_NULL(bh)) { 2878 if (!buffer_uptodate(bh) && cnt) 2879 (*cnt)++; 2880 brelse(bh); 2881 } 2882 } 2883 } 2884 if (++group >= ngroups) 2885 group = 0; 2886 } 2887 blk_finish_plug(&plug); 2888 return group; 2889 } 2890 2891 /* 2892 * Batch reads of the block allocation bitmaps to get 2893 * multiple READs in flight; limit prefetching at inexpensive 2894 * CR, otherwise mballoc can spend a lot of time loading 2895 * imperfect groups 2896 */ 2897 static void ext4_mb_might_prefetch(struct ext4_allocation_context *ac, 2898 ext4_group_t group) 2899 { 2900 struct ext4_sb_info *sbi; 2901 2902 if (ac->ac_prefetch_grp != group) 2903 return; 2904 2905 sbi = EXT4_SB(ac->ac_sb); 2906 if (ext4_mb_cr_expensive(ac->ac_criteria) || 2907 ac->ac_prefetch_ios < sbi->s_mb_prefetch_limit) { 2908 unsigned int nr = sbi->s_mb_prefetch; 2909 2910 if (ext4_has_feature_flex_bg(ac->ac_sb)) { 2911 nr = 1 << sbi->s_log_groups_per_flex; 2912 nr -= group & (nr - 1); 2913 nr = umin(nr, sbi->s_mb_prefetch); 2914 } 2915 2916 ac->ac_prefetch_nr = nr; 2917 ac->ac_prefetch_grp = ext4_mb_prefetch(ac->ac_sb, group, nr, 2918 &ac->ac_prefetch_ios); 2919 } 2920 } 2921 2922 /* 2923 * Prefetching reads the block bitmap into the buffer cache; but we 2924 * need to make sure that the buddy bitmap in the page cache has been 2925 * initialized. Note that ext4_mb_init_group() will block if the I/O 2926 * is not yet completed, or indeed if it was not initiated by 2927 * ext4_mb_prefetch did not start the I/O. 2928 * 2929 * TODO: We should actually kick off the buddy bitmap setup in a work 2930 * queue when the buffer I/O is completed, so that we don't block 2931 * waiting for the block allocation bitmap read to finish when 2932 * ext4_mb_prefetch_fini is called from ext4_mb_regular_allocator(). 2933 */ 2934 void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group, 2935 unsigned int nr) 2936 { 2937 struct ext4_group_desc *gdp; 2938 struct ext4_group_info *grp; 2939 2940 while (nr-- > 0) { 2941 if (!group) 2942 group = ext4_get_groups_count(sb); 2943 group--; 2944 gdp = ext4_get_group_desc(sb, group, NULL); 2945 grp = ext4_get_group_info(sb, group); 2946 2947 if (grp && gdp && EXT4_MB_GRP_NEED_INIT(grp) && 2948 ext4_free_group_clusters(sb, gdp) > 0) { 2949 if (ext4_mb_init_group(sb, group, GFP_NOFS)) 2950 break; 2951 } 2952 } 2953 } 2954 2955 static int ext4_mb_scan_group(struct ext4_allocation_context *ac, 2956 ext4_group_t group) 2957 { 2958 int ret; 2959 struct super_block *sb = ac->ac_sb; 2960 enum criteria cr = ac->ac_criteria; 2961 2962 ext4_mb_might_prefetch(ac, group); 2963 2964 /* prevent unnecessary buddy loading. */ 2965 if (cr < CR_ANY_FREE && spin_is_locked(ext4_group_lock_ptr(sb, group))) 2966 return 0; 2967 2968 /* This now checks without needing the buddy folio */ 2969 ret = ext4_mb_good_group_nolock(ac, group, cr); 2970 if (ret <= 0) { 2971 if (!ac->ac_first_err) 2972 ac->ac_first_err = ret; 2973 return 0; 2974 } 2975 2976 ret = ext4_mb_load_buddy(sb, group, ac->ac_e4b); 2977 if (ret) 2978 return ret; 2979 2980 /* skip busy group */ 2981 if (cr >= CR_ANY_FREE) 2982 ext4_lock_group(sb, group); 2983 else if (!ext4_try_lock_group(sb, group)) 2984 goto out_unload; 2985 2986 /* We need to check again after locking the block group. */ 2987 if (unlikely(!ext4_mb_good_group(ac, group, cr))) 2988 goto out_unlock; 2989 2990 __ext4_mb_scan_group(ac); 2991 2992 out_unlock: 2993 ext4_unlock_group(sb, group); 2994 out_unload: 2995 ext4_mb_unload_buddy(ac->ac_e4b); 2996 return ret; 2997 } 2998 2999 static noinline_for_stack int 3000 ext4_mb_regular_allocator(struct ext4_allocation_context *ac) 3001 { 3002 ext4_group_t i; 3003 int err = 0; 3004 struct super_block *sb = ac->ac_sb; 3005 struct ext4_sb_info *sbi = EXT4_SB(sb); 3006 struct ext4_buddy e4b; 3007 3008 BUG_ON(ac->ac_status == AC_STATUS_FOUND); 3009 3010 /* first, try the goal */ 3011 err = ext4_mb_find_by_goal(ac, &e4b); 3012 if (err || ac->ac_status == AC_STATUS_FOUND) 3013 goto out; 3014 3015 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) 3016 goto out; 3017 3018 /* 3019 * ac->ac_2order is set only if the fe_len is a power of 2 3020 * if ac->ac_2order is set we also set criteria to CR_POWER2_ALIGNED 3021 * so that we try exact allocation using buddy. 3022 */ 3023 i = fls(ac->ac_g_ex.fe_len); 3024 ac->ac_2order = 0; 3025 /* 3026 * We search using buddy data only if the order of the request 3027 * is greater than equal to the sbi_s_mb_order2_reqs 3028 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req 3029 * We also support searching for power-of-two requests only for 3030 * requests upto maximum buddy size we have constructed. 3031 */ 3032 if (i >= sbi->s_mb_order2_reqs && i <= MB_NUM_ORDERS(sb)) { 3033 if (is_power_of_2(ac->ac_g_ex.fe_len)) 3034 ac->ac_2order = array_index_nospec(i - 1, 3035 MB_NUM_ORDERS(sb)); 3036 } 3037 3038 /* if stream allocation is enabled, use global goal */ 3039 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) { 3040 int hash = (unsigned int)ac->ac_inode->i_ino % sbi->s_mb_nr_global_goals; 3041 3042 ac->ac_g_ex.fe_group = READ_ONCE(sbi->s_mb_last_groups[hash]); 3043 ac->ac_g_ex.fe_start = -1; 3044 ac->ac_flags &= ~EXT4_MB_HINT_TRY_GOAL; 3045 } 3046 3047 /* 3048 * Let's just scan groups to find more-less suitable blocks We 3049 * start with CR_GOAL_LEN_FAST, unless it is power of 2 3050 * aligned, in which case let's do that faster approach first. 3051 */ 3052 ac->ac_criteria = CR_GOAL_LEN_FAST; 3053 if (ac->ac_2order) 3054 ac->ac_criteria = CR_POWER2_ALIGNED; 3055 3056 ac->ac_e4b = &e4b; 3057 ac->ac_prefetch_ios = 0; 3058 ac->ac_first_err = 0; 3059 repeat: 3060 while (ac->ac_criteria < EXT4_MB_NUM_CRS) { 3061 err = ext4_mb_scan_groups(ac); 3062 if (err) 3063 goto out; 3064 3065 if (ac->ac_status != AC_STATUS_CONTINUE) 3066 break; 3067 } 3068 3069 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND && 3070 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) { 3071 /* 3072 * We've been searching too long. Let's try to allocate 3073 * the best chunk we've found so far 3074 */ 3075 ext4_mb_try_best_found(ac, &e4b); 3076 if (ac->ac_status != AC_STATUS_FOUND) { 3077 int lost; 3078 3079 /* 3080 * Someone more lucky has already allocated it. 3081 * The only thing we can do is just take first 3082 * found block(s) 3083 */ 3084 lost = atomic_inc_return(&sbi->s_mb_lost_chunks); 3085 mb_debug(sb, "lost chunk, group: %u, start: %d, len: %d, lost: %d\n", 3086 ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start, 3087 ac->ac_b_ex.fe_len, lost); 3088 3089 ac->ac_b_ex.fe_group = 0; 3090 ac->ac_b_ex.fe_start = 0; 3091 ac->ac_b_ex.fe_len = 0; 3092 ac->ac_status = AC_STATUS_CONTINUE; 3093 ac->ac_flags |= EXT4_MB_HINT_FIRST; 3094 ac->ac_criteria = CR_ANY_FREE; 3095 goto repeat; 3096 } 3097 } 3098 3099 if (sbi->s_mb_stats && ac->ac_status == AC_STATUS_FOUND) { 3100 atomic64_inc(&sbi->s_bal_cX_hits[ac->ac_criteria]); 3101 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC && 3102 ac->ac_b_ex.fe_group == ac->ac_g_ex.fe_group) 3103 atomic_inc(&sbi->s_bal_stream_goals); 3104 } 3105 out: 3106 if (!err && ac->ac_status != AC_STATUS_FOUND && ac->ac_first_err) 3107 err = ac->ac_first_err; 3108 3109 mb_debug(sb, "Best len %d, origin len %d, ac_status %u, ac_flags 0x%x, cr %d ret %d\n", 3110 ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status, 3111 ac->ac_flags, ac->ac_criteria, err); 3112 3113 if (ac->ac_prefetch_nr) 3114 ext4_mb_prefetch_fini(sb, ac->ac_prefetch_grp, ac->ac_prefetch_nr); 3115 3116 return err; 3117 } 3118 3119 static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos) 3120 { 3121 struct super_block *sb = pde_data(file_inode(seq->file)); 3122 ext4_group_t group; 3123 3124 if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) 3125 return NULL; 3126 group = *pos + 1; 3127 return (void *) ((unsigned long) group); 3128 } 3129 3130 static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos) 3131 { 3132 struct super_block *sb = pde_data(file_inode(seq->file)); 3133 ext4_group_t group; 3134 3135 ++*pos; 3136 if (*pos < 0 || *pos >= ext4_get_groups_count(sb)) 3137 return NULL; 3138 group = *pos + 1; 3139 return (void *) ((unsigned long) group); 3140 } 3141 3142 static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v) 3143 { 3144 struct super_block *sb = pde_data(file_inode(seq->file)); 3145 ext4_group_t group = (ext4_group_t) ((unsigned long) v); 3146 int i, err; 3147 char nbuf[16]; 3148 struct ext4_buddy e4b; 3149 struct ext4_group_info *grinfo; 3150 unsigned char blocksize_bits = min_t(unsigned char, 3151 sb->s_blocksize_bits, 3152 EXT4_MAX_BLOCK_LOG_SIZE); 3153 DEFINE_RAW_FLEX(struct ext4_group_info, sg, bb_counters, 3154 EXT4_MAX_BLOCK_LOG_SIZE + 2); 3155 3156 group--; 3157 if (group == 0) 3158 seq_puts(seq, "#group: free frags first [" 3159 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 " 3160 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n"); 3161 3162 i = (blocksize_bits + 2) * sizeof(sg->bb_counters[0]) + 3163 sizeof(struct ext4_group_info); 3164 3165 grinfo = ext4_get_group_info(sb, group); 3166 if (!grinfo) 3167 return 0; 3168 /* Load the group info in memory only if not already loaded. */ 3169 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) { 3170 err = ext4_mb_load_buddy(sb, group, &e4b); 3171 if (err) { 3172 seq_printf(seq, "#%-5u: %s\n", group, ext4_decode_error(NULL, err, nbuf)); 3173 return 0; 3174 } 3175 ext4_mb_unload_buddy(&e4b); 3176 } 3177 3178 /* 3179 * We care only about free space counters in the group info and 3180 * these are safe to access even after the buddy has been unloaded 3181 */ 3182 memcpy(sg, grinfo, i); 3183 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg->bb_free, 3184 sg->bb_fragments, sg->bb_first_free); 3185 for (i = 0; i <= 13; i++) 3186 seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ? 3187 sg->bb_counters[i] : 0); 3188 seq_puts(seq, " ]"); 3189 if (EXT4_MB_GRP_BBITMAP_CORRUPT(sg)) 3190 seq_puts(seq, " Block bitmap corrupted!"); 3191 seq_putc(seq, '\n'); 3192 return 0; 3193 } 3194 3195 static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v) 3196 { 3197 } 3198 3199 const struct seq_operations ext4_mb_seq_groups_ops = { 3200 .start = ext4_mb_seq_groups_start, 3201 .next = ext4_mb_seq_groups_next, 3202 .stop = ext4_mb_seq_groups_stop, 3203 .show = ext4_mb_seq_groups_show, 3204 }; 3205 3206 int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset) 3207 { 3208 struct super_block *sb = seq->private; 3209 struct ext4_sb_info *sbi = EXT4_SB(sb); 3210 3211 seq_puts(seq, "mballoc:\n"); 3212 if (!sbi->s_mb_stats) { 3213 seq_puts(seq, "\tmb stats collection turned off.\n"); 3214 seq_puts( 3215 seq, 3216 "\tTo enable, please write \"1\" to sysfs file mb_stats.\n"); 3217 return 0; 3218 } 3219 seq_printf(seq, "\treqs: %u\n", atomic_read(&sbi->s_bal_reqs)); 3220 seq_printf(seq, "\tsuccess: %u\n", atomic_read(&sbi->s_bal_success)); 3221 3222 seq_printf(seq, "\tgroups_scanned: %u\n", 3223 atomic_read(&sbi->s_bal_groups_scanned)); 3224 3225 /* CR_POWER2_ALIGNED stats */ 3226 seq_puts(seq, "\tcr_p2_aligned_stats:\n"); 3227 seq_printf(seq, "\t\thits: %llu\n", 3228 atomic64_read(&sbi->s_bal_cX_hits[CR_POWER2_ALIGNED])); 3229 seq_printf( 3230 seq, "\t\tgroups_considered: %llu\n", 3231 atomic64_read( 3232 &sbi->s_bal_cX_groups_considered[CR_POWER2_ALIGNED])); 3233 seq_printf(seq, "\t\textents_scanned: %u\n", 3234 atomic_read(&sbi->s_bal_cX_ex_scanned[CR_POWER2_ALIGNED])); 3235 seq_printf(seq, "\t\tuseless_loops: %llu\n", 3236 atomic64_read(&sbi->s_bal_cX_failed[CR_POWER2_ALIGNED])); 3237 3238 /* CR_GOAL_LEN_FAST stats */ 3239 seq_puts(seq, "\tcr_goal_fast_stats:\n"); 3240 seq_printf(seq, "\t\thits: %llu\n", 3241 atomic64_read(&sbi->s_bal_cX_hits[CR_GOAL_LEN_FAST])); 3242 seq_printf(seq, "\t\tgroups_considered: %llu\n", 3243 atomic64_read( 3244 &sbi->s_bal_cX_groups_considered[CR_GOAL_LEN_FAST])); 3245 seq_printf(seq, "\t\textents_scanned: %u\n", 3246 atomic_read(&sbi->s_bal_cX_ex_scanned[CR_GOAL_LEN_FAST])); 3247 seq_printf(seq, "\t\tuseless_loops: %llu\n", 3248 atomic64_read(&sbi->s_bal_cX_failed[CR_GOAL_LEN_FAST])); 3249 3250 /* CR_BEST_AVAIL_LEN stats */ 3251 seq_puts(seq, "\tcr_best_avail_stats:\n"); 3252 seq_printf(seq, "\t\thits: %llu\n", 3253 atomic64_read(&sbi->s_bal_cX_hits[CR_BEST_AVAIL_LEN])); 3254 seq_printf( 3255 seq, "\t\tgroups_considered: %llu\n", 3256 atomic64_read( 3257 &sbi->s_bal_cX_groups_considered[CR_BEST_AVAIL_LEN])); 3258 seq_printf(seq, "\t\textents_scanned: %u\n", 3259 atomic_read(&sbi->s_bal_cX_ex_scanned[CR_BEST_AVAIL_LEN])); 3260 seq_printf(seq, "\t\tuseless_loops: %llu\n", 3261 atomic64_read(&sbi->s_bal_cX_failed[CR_BEST_AVAIL_LEN])); 3262 3263 /* CR_GOAL_LEN_SLOW stats */ 3264 seq_puts(seq, "\tcr_goal_slow_stats:\n"); 3265 seq_printf(seq, "\t\thits: %llu\n", 3266 atomic64_read(&sbi->s_bal_cX_hits[CR_GOAL_LEN_SLOW])); 3267 seq_printf(seq, "\t\tgroups_considered: %llu\n", 3268 atomic64_read( 3269 &sbi->s_bal_cX_groups_considered[CR_GOAL_LEN_SLOW])); 3270 seq_printf(seq, "\t\textents_scanned: %u\n", 3271 atomic_read(&sbi->s_bal_cX_ex_scanned[CR_GOAL_LEN_SLOW])); 3272 seq_printf(seq, "\t\tuseless_loops: %llu\n", 3273 atomic64_read(&sbi->s_bal_cX_failed[CR_GOAL_LEN_SLOW])); 3274 3275 /* CR_ANY_FREE stats */ 3276 seq_puts(seq, "\tcr_any_free_stats:\n"); 3277 seq_printf(seq, "\t\thits: %llu\n", 3278 atomic64_read(&sbi->s_bal_cX_hits[CR_ANY_FREE])); 3279 seq_printf( 3280 seq, "\t\tgroups_considered: %llu\n", 3281 atomic64_read(&sbi->s_bal_cX_groups_considered[CR_ANY_FREE])); 3282 seq_printf(seq, "\t\textents_scanned: %u\n", 3283 atomic_read(&sbi->s_bal_cX_ex_scanned[CR_ANY_FREE])); 3284 seq_printf(seq, "\t\tuseless_loops: %llu\n", 3285 atomic64_read(&sbi->s_bal_cX_failed[CR_ANY_FREE])); 3286 3287 /* Aggregates */ 3288 seq_printf(seq, "\textents_scanned: %u\n", 3289 atomic_read(&sbi->s_bal_ex_scanned)); 3290 seq_printf(seq, "\t\tgoal_hits: %u\n", atomic_read(&sbi->s_bal_goals)); 3291 seq_printf(seq, "\t\tstream_goal_hits: %u\n", 3292 atomic_read(&sbi->s_bal_stream_goals)); 3293 seq_printf(seq, "\t\tlen_goal_hits: %u\n", 3294 atomic_read(&sbi->s_bal_len_goals)); 3295 seq_printf(seq, "\t\t2^n_hits: %u\n", atomic_read(&sbi->s_bal_2orders)); 3296 seq_printf(seq, "\t\tbreaks: %u\n", atomic_read(&sbi->s_bal_breaks)); 3297 seq_printf(seq, "\t\tlost: %u\n", atomic_read(&sbi->s_mb_lost_chunks)); 3298 seq_printf(seq, "\tbuddies_generated: %u/%u\n", 3299 atomic_read(&sbi->s_mb_buddies_generated), 3300 ext4_get_groups_count(sb)); 3301 seq_printf(seq, "\tbuddies_time_used: %llu\n", 3302 atomic64_read(&sbi->s_mb_generation_time)); 3303 seq_printf(seq, "\tpreallocated: %u\n", 3304 atomic_read(&sbi->s_mb_preallocated)); 3305 seq_printf(seq, "\tdiscarded: %u\n", atomic_read(&sbi->s_mb_discarded)); 3306 return 0; 3307 } 3308 3309 static void *ext4_mb_seq_structs_summary_start(struct seq_file *seq, loff_t *pos) 3310 { 3311 struct super_block *sb = pde_data(file_inode(seq->file)); 3312 unsigned long position; 3313 3314 if (*pos < 0 || *pos >= 2*MB_NUM_ORDERS(sb)) 3315 return NULL; 3316 position = *pos + 1; 3317 return (void *) ((unsigned long) position); 3318 } 3319 3320 static void *ext4_mb_seq_structs_summary_next(struct seq_file *seq, void *v, loff_t *pos) 3321 { 3322 struct super_block *sb = pde_data(file_inode(seq->file)); 3323 unsigned long position; 3324 3325 ++*pos; 3326 if (*pos < 0 || *pos >= 2*MB_NUM_ORDERS(sb)) 3327 return NULL; 3328 position = *pos + 1; 3329 return (void *) ((unsigned long) position); 3330 } 3331 3332 static int ext4_mb_seq_structs_summary_show(struct seq_file *seq, void *v) 3333 { 3334 struct super_block *sb = pde_data(file_inode(seq->file)); 3335 struct ext4_sb_info *sbi = EXT4_SB(sb); 3336 unsigned long position = ((unsigned long) v); 3337 struct ext4_group_info *grp; 3338 unsigned int count; 3339 unsigned long idx; 3340 3341 position--; 3342 if (position >= MB_NUM_ORDERS(sb)) { 3343 position -= MB_NUM_ORDERS(sb); 3344 if (position == 0) 3345 seq_puts(seq, "avg_fragment_size_lists:\n"); 3346 3347 count = 0; 3348 xa_for_each(&sbi->s_mb_avg_fragment_size[position], idx, grp) 3349 count++; 3350 seq_printf(seq, "\tlist_order_%u_groups: %u\n", 3351 (unsigned int)position, count); 3352 return 0; 3353 } 3354 3355 if (position == 0) { 3356 seq_printf(seq, "optimize_scan: %d\n", 3357 test_opt2(sb, MB_OPTIMIZE_SCAN) ? 1 : 0); 3358 seq_puts(seq, "max_free_order_lists:\n"); 3359 } 3360 count = 0; 3361 xa_for_each(&sbi->s_mb_largest_free_orders[position], idx, grp) 3362 count++; 3363 seq_printf(seq, "\tlist_order_%u_groups: %u\n", 3364 (unsigned int)position, count); 3365 3366 return 0; 3367 } 3368 3369 static void ext4_mb_seq_structs_summary_stop(struct seq_file *seq, void *v) 3370 { 3371 } 3372 3373 const struct seq_operations ext4_mb_seq_structs_summary_ops = { 3374 .start = ext4_mb_seq_structs_summary_start, 3375 .next = ext4_mb_seq_structs_summary_next, 3376 .stop = ext4_mb_seq_structs_summary_stop, 3377 .show = ext4_mb_seq_structs_summary_show, 3378 }; 3379 3380 static struct kmem_cache *get_groupinfo_cache(int blocksize_bits) 3381 { 3382 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; 3383 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index]; 3384 3385 BUG_ON(!cachep); 3386 return cachep; 3387 } 3388 3389 /* 3390 * Allocate the top-level s_group_info array for the specified number 3391 * of groups 3392 */ 3393 int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups) 3394 { 3395 struct ext4_sb_info *sbi = EXT4_SB(sb); 3396 unsigned size; 3397 struct ext4_group_info ***old_groupinfo, ***new_groupinfo; 3398 3399 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >> 3400 EXT4_DESC_PER_BLOCK_BITS(sb); 3401 if (size <= sbi->s_group_info_size) 3402 return 0; 3403 3404 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size); 3405 new_groupinfo = kvzalloc(size, GFP_KERNEL); 3406 if (!new_groupinfo) { 3407 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group"); 3408 return -ENOMEM; 3409 } 3410 rcu_read_lock(); 3411 old_groupinfo = rcu_dereference(sbi->s_group_info); 3412 if (old_groupinfo) 3413 memcpy(new_groupinfo, old_groupinfo, 3414 sbi->s_group_info_size * sizeof(*sbi->s_group_info)); 3415 rcu_read_unlock(); 3416 rcu_assign_pointer(sbi->s_group_info, new_groupinfo); 3417 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info); 3418 if (old_groupinfo) 3419 ext4_kvfree_array_rcu(old_groupinfo); 3420 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n", 3421 sbi->s_group_info_size); 3422 return 0; 3423 } 3424 3425 /* Create and initialize ext4_group_info data for the given group. */ 3426 int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group, 3427 struct ext4_group_desc *desc) 3428 { 3429 int i; 3430 int metalen = 0; 3431 int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb); 3432 struct ext4_sb_info *sbi = EXT4_SB(sb); 3433 struct ext4_group_info **meta_group_info; 3434 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); 3435 3436 /* 3437 * First check if this group is the first of a reserved block. 3438 * If it's true, we have to allocate a new table of pointers 3439 * to ext4_group_info structures 3440 */ 3441 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { 3442 metalen = sizeof(*meta_group_info) << 3443 EXT4_DESC_PER_BLOCK_BITS(sb); 3444 meta_group_info = kmalloc(metalen, GFP_NOFS); 3445 if (meta_group_info == NULL) { 3446 ext4_msg(sb, KERN_ERR, "can't allocate mem " 3447 "for a buddy group"); 3448 return -ENOMEM; 3449 } 3450 rcu_read_lock(); 3451 rcu_dereference(sbi->s_group_info)[idx] = meta_group_info; 3452 rcu_read_unlock(); 3453 } 3454 3455 meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx); 3456 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1); 3457 3458 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS); 3459 if (meta_group_info[i] == NULL) { 3460 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem"); 3461 goto exit_group_info; 3462 } 3463 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, 3464 &(meta_group_info[i]->bb_state)); 3465 3466 /* 3467 * initialize bb_free to be able to skip 3468 * empty groups without initialization 3469 */ 3470 if (ext4_has_group_desc_csum(sb) && 3471 (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { 3472 meta_group_info[i]->bb_free = 3473 ext4_free_clusters_after_init(sb, group, desc); 3474 } else { 3475 meta_group_info[i]->bb_free = 3476 ext4_free_group_clusters(sb, desc); 3477 } 3478 3479 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list); 3480 init_rwsem(&meta_group_info[i]->alloc_sem); 3481 meta_group_info[i]->bb_free_root = RB_ROOT; 3482 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */ 3483 meta_group_info[i]->bb_avg_fragment_size_order = -1; /* uninit */ 3484 meta_group_info[i]->bb_group = group; 3485 3486 mb_group_bb_bitmap_alloc(sb, meta_group_info[i], group); 3487 return 0; 3488 3489 exit_group_info: 3490 /* If a meta_group_info table has been allocated, release it now */ 3491 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) { 3492 struct ext4_group_info ***group_info; 3493 3494 rcu_read_lock(); 3495 group_info = rcu_dereference(sbi->s_group_info); 3496 kfree(group_info[idx]); 3497 group_info[idx] = NULL; 3498 rcu_read_unlock(); 3499 } 3500 return -ENOMEM; 3501 } /* ext4_mb_add_groupinfo */ 3502 3503 static int ext4_mb_init_backend(struct super_block *sb) 3504 { 3505 ext4_group_t ngroups = ext4_get_groups_count(sb); 3506 ext4_group_t i; 3507 struct ext4_sb_info *sbi = EXT4_SB(sb); 3508 int err; 3509 struct ext4_group_desc *desc; 3510 struct ext4_group_info ***group_info; 3511 struct kmem_cache *cachep; 3512 3513 err = ext4_mb_alloc_groupinfo(sb, ngroups); 3514 if (err) 3515 return err; 3516 3517 sbi->s_buddy_cache = new_inode(sb); 3518 if (sbi->s_buddy_cache == NULL) { 3519 ext4_msg(sb, KERN_ERR, "can't get new inode"); 3520 goto err_freesgi; 3521 } 3522 /* To avoid potentially colliding with an valid on-disk inode number, 3523 * use EXT4_BAD_INO for the buddy cache inode number. This inode is 3524 * not in the inode hash, so it should never be found by iget(), but 3525 * this will avoid confusion if it ever shows up during debugging. */ 3526 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO; 3527 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0; 3528 ext4_set_inode_mapping_order(sbi->s_buddy_cache); 3529 3530 for (i = 0; i < ngroups; i++) { 3531 cond_resched(); 3532 desc = ext4_get_group_desc(sb, i, NULL); 3533 if (desc == NULL) { 3534 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i); 3535 goto err_freebuddy; 3536 } 3537 if (ext4_mb_add_groupinfo(sb, i, desc) != 0) 3538 goto err_freebuddy; 3539 } 3540 3541 if (ext4_has_feature_flex_bg(sb)) { 3542 /* a single flex group is supposed to be read by a single IO. 3543 * 2 ^ s_log_groups_per_flex != UINT_MAX as s_mb_prefetch is 3544 * unsigned integer, so the maximum shift is 32. 3545 */ 3546 if (sbi->s_es->s_log_groups_per_flex >= 32) { 3547 ext4_msg(sb, KERN_ERR, "too many log groups per flexible block group"); 3548 goto err_freebuddy; 3549 } 3550 sbi->s_mb_prefetch = min_t(uint, 1 << sbi->s_es->s_log_groups_per_flex, 3551 BLK_MAX_SEGMENT_SIZE >> (sb->s_blocksize_bits - 9)); 3552 sbi->s_mb_prefetch *= 8; /* 8 prefetch IOs in flight at most */ 3553 } else { 3554 sbi->s_mb_prefetch = 32; 3555 } 3556 if (sbi->s_mb_prefetch > ext4_get_groups_count(sb)) 3557 sbi->s_mb_prefetch = ext4_get_groups_count(sb); 3558 /* 3559 * now many real IOs to prefetch within a single allocation at 3560 * CR_POWER2_ALIGNED. Given CR_POWER2_ALIGNED is an CPU-related 3561 * optimization we shouldn't try to load too many groups, at some point 3562 * we should start to use what we've got in memory. 3563 * with an average random access time 5ms, it'd take a second to get 3564 * 200 groups (* N with flex_bg), so let's make this limit 4 3565 */ 3566 sbi->s_mb_prefetch_limit = sbi->s_mb_prefetch * 4; 3567 if (sbi->s_mb_prefetch_limit > ext4_get_groups_count(sb)) 3568 sbi->s_mb_prefetch_limit = ext4_get_groups_count(sb); 3569 3570 return 0; 3571 3572 err_freebuddy: 3573 cachep = get_groupinfo_cache(sb->s_blocksize_bits); 3574 while (i-- > 0) { 3575 struct ext4_group_info *grp = ext4_get_group_info(sb, i); 3576 3577 if (grp) 3578 kmem_cache_free(cachep, grp); 3579 } 3580 i = sbi->s_group_info_size; 3581 rcu_read_lock(); 3582 group_info = rcu_dereference(sbi->s_group_info); 3583 while (i-- > 0) 3584 kfree(group_info[i]); 3585 rcu_read_unlock(); 3586 iput(sbi->s_buddy_cache); 3587 err_freesgi: 3588 kvfree(rcu_access_pointer(sbi->s_group_info)); 3589 return -ENOMEM; 3590 } 3591 3592 static void ext4_groupinfo_destroy_slabs(void) 3593 { 3594 int i; 3595 3596 for (i = 0; i < NR_GRPINFO_CACHES; i++) { 3597 kmem_cache_destroy(ext4_groupinfo_caches[i]); 3598 ext4_groupinfo_caches[i] = NULL; 3599 } 3600 } 3601 3602 static int ext4_groupinfo_create_slab(size_t size) 3603 { 3604 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex); 3605 int slab_size; 3606 int blocksize_bits = order_base_2(size); 3607 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE; 3608 struct kmem_cache *cachep; 3609 3610 if (cache_index >= NR_GRPINFO_CACHES) 3611 return -EINVAL; 3612 3613 if (unlikely(cache_index < 0)) 3614 cache_index = 0; 3615 3616 mutex_lock(&ext4_grpinfo_slab_create_mutex); 3617 if (ext4_groupinfo_caches[cache_index]) { 3618 mutex_unlock(&ext4_grpinfo_slab_create_mutex); 3619 return 0; /* Already created */ 3620 } 3621 3622 slab_size = offsetof(struct ext4_group_info, 3623 bb_counters[blocksize_bits + 2]); 3624 3625 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index], 3626 slab_size, 0, SLAB_RECLAIM_ACCOUNT, 3627 NULL); 3628 3629 ext4_groupinfo_caches[cache_index] = cachep; 3630 3631 mutex_unlock(&ext4_grpinfo_slab_create_mutex); 3632 if (!cachep) { 3633 printk(KERN_EMERG 3634 "EXT4-fs: no memory for groupinfo slab cache\n"); 3635 return -ENOMEM; 3636 } 3637 3638 return 0; 3639 } 3640 3641 static void ext4_discard_work(struct work_struct *work) 3642 { 3643 struct ext4_sb_info *sbi = container_of(work, 3644 struct ext4_sb_info, s_discard_work); 3645 struct super_block *sb = sbi->s_sb; 3646 struct ext4_free_data *fd, *nfd; 3647 struct ext4_buddy e4b; 3648 LIST_HEAD(discard_list); 3649 ext4_group_t grp, load_grp; 3650 int err = 0; 3651 3652 spin_lock(&sbi->s_md_lock); 3653 list_splice_init(&sbi->s_discard_list, &discard_list); 3654 spin_unlock(&sbi->s_md_lock); 3655 3656 load_grp = UINT_MAX; 3657 list_for_each_entry_safe(fd, nfd, &discard_list, efd_list) { 3658 /* 3659 * If filesystem is umounting or no memory or suffering 3660 * from no space, give up the discard 3661 */ 3662 if ((sb->s_flags & SB_ACTIVE) && !err && 3663 !atomic_read(&sbi->s_retry_alloc_pending)) { 3664 grp = fd->efd_group; 3665 if (grp != load_grp) { 3666 if (load_grp != UINT_MAX) 3667 ext4_mb_unload_buddy(&e4b); 3668 3669 err = ext4_mb_load_buddy(sb, grp, &e4b); 3670 if (err) { 3671 kmem_cache_free(ext4_free_data_cachep, fd); 3672 load_grp = UINT_MAX; 3673 continue; 3674 } else { 3675 load_grp = grp; 3676 } 3677 } 3678 3679 ext4_lock_group(sb, grp); 3680 ext4_try_to_trim_range(sb, &e4b, fd->efd_start_cluster, 3681 fd->efd_start_cluster + fd->efd_count - 1, 1); 3682 ext4_unlock_group(sb, grp); 3683 } 3684 kmem_cache_free(ext4_free_data_cachep, fd); 3685 } 3686 3687 if (load_grp != UINT_MAX) 3688 ext4_mb_unload_buddy(&e4b); 3689 } 3690 3691 static inline void ext4_mb_avg_fragment_size_destroy(struct ext4_sb_info *sbi) 3692 { 3693 if (!sbi->s_mb_avg_fragment_size) 3694 return; 3695 3696 for (int i = 0; i < MB_NUM_ORDERS(sbi->s_sb); i++) 3697 xa_destroy(&sbi->s_mb_avg_fragment_size[i]); 3698 3699 kfree(sbi->s_mb_avg_fragment_size); 3700 sbi->s_mb_avg_fragment_size = NULL; 3701 } 3702 3703 static inline void ext4_mb_largest_free_orders_destroy(struct ext4_sb_info *sbi) 3704 { 3705 if (!sbi->s_mb_largest_free_orders) 3706 return; 3707 3708 for (int i = 0; i < MB_NUM_ORDERS(sbi->s_sb); i++) 3709 xa_destroy(&sbi->s_mb_largest_free_orders[i]); 3710 3711 kfree(sbi->s_mb_largest_free_orders); 3712 sbi->s_mb_largest_free_orders = NULL; 3713 } 3714 3715 int ext4_mb_init(struct super_block *sb) 3716 { 3717 struct ext4_sb_info *sbi = EXT4_SB(sb); 3718 unsigned i, j; 3719 unsigned offset, offset_incr; 3720 unsigned max; 3721 int ret; 3722 3723 i = MB_NUM_ORDERS(sb) * sizeof(*sbi->s_mb_offsets); 3724 3725 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL); 3726 if (sbi->s_mb_offsets == NULL) { 3727 ret = -ENOMEM; 3728 goto out; 3729 } 3730 3731 i = MB_NUM_ORDERS(sb) * sizeof(*sbi->s_mb_maxs); 3732 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL); 3733 if (sbi->s_mb_maxs == NULL) { 3734 ret = -ENOMEM; 3735 goto out; 3736 } 3737 3738 ret = ext4_groupinfo_create_slab(sb->s_blocksize); 3739 if (ret < 0) 3740 goto out; 3741 3742 /* order 0 is regular bitmap */ 3743 sbi->s_mb_maxs[0] = sb->s_blocksize << 3; 3744 sbi->s_mb_offsets[0] = 0; 3745 3746 i = 1; 3747 offset = 0; 3748 offset_incr = 1 << (sb->s_blocksize_bits - 1); 3749 max = sb->s_blocksize << 2; 3750 do { 3751 sbi->s_mb_offsets[i] = offset; 3752 sbi->s_mb_maxs[i] = max; 3753 offset += offset_incr; 3754 offset_incr = offset_incr >> 1; 3755 max = max >> 1; 3756 i++; 3757 } while (i < MB_NUM_ORDERS(sb)); 3758 3759 sbi->s_mb_avg_fragment_size = 3760 kmalloc_objs(struct xarray, MB_NUM_ORDERS(sb)); 3761 if (!sbi->s_mb_avg_fragment_size) { 3762 ret = -ENOMEM; 3763 goto out; 3764 } 3765 for (i = 0; i < MB_NUM_ORDERS(sb); i++) 3766 xa_init(&sbi->s_mb_avg_fragment_size[i]); 3767 3768 sbi->s_mb_largest_free_orders = 3769 kmalloc_objs(struct xarray, MB_NUM_ORDERS(sb)); 3770 if (!sbi->s_mb_largest_free_orders) { 3771 ret = -ENOMEM; 3772 goto out; 3773 } 3774 for (i = 0; i < MB_NUM_ORDERS(sb); i++) 3775 xa_init(&sbi->s_mb_largest_free_orders[i]); 3776 3777 spin_lock_init(&sbi->s_md_lock); 3778 atomic_set(&sbi->s_mb_free_pending, 0); 3779 INIT_LIST_HEAD(&sbi->s_freed_data_list[0]); 3780 INIT_LIST_HEAD(&sbi->s_freed_data_list[1]); 3781 INIT_LIST_HEAD(&sbi->s_discard_list); 3782 INIT_WORK(&sbi->s_discard_work, ext4_discard_work); 3783 atomic_set(&sbi->s_retry_alloc_pending, 0); 3784 3785 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN; 3786 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN; 3787 sbi->s_mb_stats = MB_DEFAULT_STATS; 3788 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD; 3789 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS; 3790 sbi->s_mb_best_avail_max_trim_order = MB_DEFAULT_BEST_AVAIL_TRIM_ORDER; 3791 3792 /* 3793 * The default group preallocation is 512, which for 4k block 3794 * sizes translates to 2 megabytes. However for bigalloc file 3795 * systems, this is probably too big (i.e, if the cluster size 3796 * is 1 megabyte, then group preallocation size becomes half a 3797 * gigabyte!). As a default, we will keep a two megabyte 3798 * group pralloc size for cluster sizes up to 64k, and after 3799 * that, we will force a minimum group preallocation size of 3800 * 32 clusters. This translates to 8 megs when the cluster 3801 * size is 256k, and 32 megs when the cluster size is 1 meg, 3802 * which seems reasonable as a default. 3803 */ 3804 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >> 3805 sbi->s_cluster_bits, 32); 3806 /* 3807 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc 3808 * to the lowest multiple of s_stripe which is bigger than 3809 * the s_mb_group_prealloc as determined above. We want 3810 * the preallocation size to be an exact multiple of the 3811 * RAID stripe size so that preallocations don't fragment 3812 * the stripes. 3813 */ 3814 if (sbi->s_stripe > 1) { 3815 sbi->s_mb_group_prealloc = roundup( 3816 sbi->s_mb_group_prealloc, EXT4_NUM_B2C(sbi, sbi->s_stripe)); 3817 } 3818 3819 sbi->s_mb_nr_global_goals = umin(num_possible_cpus(), 3820 DIV_ROUND_UP(sbi->s_groups_count, 4)); 3821 sbi->s_mb_last_groups = kzalloc_objs(ext4_group_t, 3822 sbi->s_mb_nr_global_goals); 3823 if (sbi->s_mb_last_groups == NULL) { 3824 ret = -ENOMEM; 3825 goto out; 3826 } 3827 3828 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group); 3829 if (sbi->s_locality_groups == NULL) { 3830 ret = -ENOMEM; 3831 goto out_free_last_groups; 3832 } 3833 for_each_possible_cpu(i) { 3834 struct ext4_locality_group *lg; 3835 lg = per_cpu_ptr(sbi->s_locality_groups, i); 3836 mutex_init(&lg->lg_mutex); 3837 for (j = 0; j < PREALLOC_TB_SIZE; j++) 3838 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]); 3839 spin_lock_init(&lg->lg_prealloc_lock); 3840 } 3841 3842 if (!bdev_rot(sb->s_bdev)) 3843 sbi->s_mb_max_linear_groups = 0; 3844 else 3845 sbi->s_mb_max_linear_groups = MB_DEFAULT_LINEAR_LIMIT; 3846 /* init file for buddy data */ 3847 ret = ext4_mb_init_backend(sb); 3848 if (ret != 0) 3849 goto out_free_locality_groups; 3850 3851 return 0; 3852 3853 out_free_locality_groups: 3854 free_percpu(sbi->s_locality_groups); 3855 sbi->s_locality_groups = NULL; 3856 out_free_last_groups: 3857 kfree(sbi->s_mb_last_groups); 3858 sbi->s_mb_last_groups = NULL; 3859 out: 3860 ext4_mb_avg_fragment_size_destroy(sbi); 3861 ext4_mb_largest_free_orders_destroy(sbi); 3862 kfree(sbi->s_mb_offsets); 3863 sbi->s_mb_offsets = NULL; 3864 kfree(sbi->s_mb_maxs); 3865 sbi->s_mb_maxs = NULL; 3866 return ret; 3867 } 3868 3869 /* need to called with the ext4 group lock held */ 3870 static int ext4_mb_cleanup_pa(struct ext4_group_info *grp) 3871 { 3872 struct ext4_prealloc_space *pa; 3873 struct list_head *cur, *tmp; 3874 int count = 0; 3875 3876 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) { 3877 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); 3878 list_del(&pa->pa_group_list); 3879 count++; 3880 kmem_cache_free(ext4_pspace_cachep, pa); 3881 } 3882 return count; 3883 } 3884 3885 void ext4_mb_release(struct super_block *sb) 3886 { 3887 ext4_group_t ngroups = ext4_get_groups_count(sb); 3888 ext4_group_t i; 3889 int num_meta_group_infos; 3890 struct ext4_group_info *grinfo, ***group_info; 3891 struct ext4_sb_info *sbi = EXT4_SB(sb); 3892 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits); 3893 int count; 3894 3895 /* 3896 * wait the discard work to drain all of ext4_free_data 3897 */ 3898 flush_work(&sbi->s_discard_work); 3899 WARN_ON_ONCE(!list_empty(&sbi->s_discard_list)); 3900 3901 group_info = rcu_access_pointer(sbi->s_group_info); 3902 if (group_info) { 3903 for (i = 0; i < ngroups; i++) { 3904 cond_resched(); 3905 grinfo = ext4_get_group_info(sb, i); 3906 if (!grinfo) 3907 continue; 3908 mb_group_bb_bitmap_free(grinfo); 3909 ext4_lock_group(sb, i); 3910 count = ext4_mb_cleanup_pa(grinfo); 3911 if (count) 3912 mb_debug(sb, "mballoc: %d PAs left\n", 3913 count); 3914 ext4_unlock_group(sb, i); 3915 kmem_cache_free(cachep, grinfo); 3916 } 3917 num_meta_group_infos = (ngroups + 3918 EXT4_DESC_PER_BLOCK(sb) - 1) >> 3919 EXT4_DESC_PER_BLOCK_BITS(sb); 3920 for (i = 0; i < num_meta_group_infos; i++) 3921 kfree(group_info[i]); 3922 kvfree(group_info); 3923 } 3924 ext4_mb_avg_fragment_size_destroy(sbi); 3925 ext4_mb_largest_free_orders_destroy(sbi); 3926 kfree(sbi->s_mb_offsets); 3927 kfree(sbi->s_mb_maxs); 3928 iput(sbi->s_buddy_cache); 3929 if (sbi->s_mb_stats) { 3930 ext4_msg(sb, KERN_INFO, 3931 "mballoc: %u blocks %u reqs (%u success)", 3932 atomic_read(&sbi->s_bal_allocated), 3933 atomic_read(&sbi->s_bal_reqs), 3934 atomic_read(&sbi->s_bal_success)); 3935 ext4_msg(sb, KERN_INFO, 3936 "mballoc: %u extents scanned, %u groups scanned, %u goal hits, " 3937 "%u 2^N hits, %u breaks, %u lost", 3938 atomic_read(&sbi->s_bal_ex_scanned), 3939 atomic_read(&sbi->s_bal_groups_scanned), 3940 atomic_read(&sbi->s_bal_goals), 3941 atomic_read(&sbi->s_bal_2orders), 3942 atomic_read(&sbi->s_bal_breaks), 3943 atomic_read(&sbi->s_mb_lost_chunks)); 3944 ext4_msg(sb, KERN_INFO, 3945 "mballoc: %u generated and it took %llu", 3946 atomic_read(&sbi->s_mb_buddies_generated), 3947 atomic64_read(&sbi->s_mb_generation_time)); 3948 ext4_msg(sb, KERN_INFO, 3949 "mballoc: %u preallocated, %u discarded", 3950 atomic_read(&sbi->s_mb_preallocated), 3951 atomic_read(&sbi->s_mb_discarded)); 3952 } 3953 3954 free_percpu(sbi->s_locality_groups); 3955 kfree(sbi->s_mb_last_groups); 3956 } 3957 3958 static inline int ext4_issue_discard(struct super_block *sb, 3959 ext4_group_t block_group, ext4_grpblk_t cluster, int count) 3960 { 3961 ext4_fsblk_t discard_block; 3962 3963 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) + 3964 ext4_group_first_block_no(sb, block_group)); 3965 count = EXT4_C2B(EXT4_SB(sb), count); 3966 trace_ext4_discard_blocks(sb, 3967 (unsigned long long) discard_block, count); 3968 3969 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0); 3970 } 3971 3972 static void ext4_free_data_in_buddy(struct super_block *sb, 3973 struct ext4_free_data *entry) 3974 { 3975 struct ext4_buddy e4b; 3976 struct ext4_group_info *db; 3977 int err, count = 0; 3978 3979 mb_debug(sb, "gonna free %u blocks in group %u (0x%p):", 3980 entry->efd_count, entry->efd_group, entry); 3981 3982 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b); 3983 /* we expect to find existing buddy because it's pinned */ 3984 BUG_ON(err != 0); 3985 3986 atomic_sub(entry->efd_count, &EXT4_SB(sb)->s_mb_free_pending); 3987 db = e4b.bd_info; 3988 /* there are blocks to put in buddy to make them really free */ 3989 count += entry->efd_count; 3990 ext4_lock_group(sb, entry->efd_group); 3991 /* Take it out of per group rb tree */ 3992 rb_erase(&entry->efd_node, &(db->bb_free_root)); 3993 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count); 3994 3995 /* 3996 * Clear the trimmed flag for the group so that the next 3997 * ext4_trim_fs can trim it. 3998 */ 3999 EXT4_MB_GRP_CLEAR_TRIMMED(db); 4000 4001 if (!db->bb_free_root.rb_node) { 4002 /* No more items in the per group rb tree 4003 * balance refcounts from ext4_mb_free_metadata() 4004 */ 4005 folio_put(e4b.bd_buddy_folio); 4006 folio_put(e4b.bd_bitmap_folio); 4007 } 4008 ext4_unlock_group(sb, entry->efd_group); 4009 ext4_mb_unload_buddy(&e4b); 4010 4011 mb_debug(sb, "freed %d blocks in 1 structures\n", count); 4012 } 4013 4014 /* 4015 * This function is called by the jbd2 layer once the commit has finished, 4016 * so we know we can free the blocks that were released with that commit. 4017 */ 4018 void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid) 4019 { 4020 struct ext4_sb_info *sbi = EXT4_SB(sb); 4021 struct ext4_free_data *entry, *tmp; 4022 LIST_HEAD(freed_data_list); 4023 struct list_head *s_freed_head = &sbi->s_freed_data_list[commit_tid & 1]; 4024 bool wake; 4025 4026 list_replace_init(s_freed_head, &freed_data_list); 4027 4028 list_for_each_entry(entry, &freed_data_list, efd_list) 4029 ext4_free_data_in_buddy(sb, entry); 4030 4031 if (test_opt(sb, DISCARD)) { 4032 spin_lock(&sbi->s_md_lock); 4033 wake = list_empty(&sbi->s_discard_list); 4034 list_splice_tail(&freed_data_list, &sbi->s_discard_list); 4035 spin_unlock(&sbi->s_md_lock); 4036 if (wake) 4037 queue_work(system_dfl_wq, &sbi->s_discard_work); 4038 } else { 4039 list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list) 4040 kmem_cache_free(ext4_free_data_cachep, entry); 4041 } 4042 } 4043 4044 int __init ext4_init_mballoc(void) 4045 { 4046 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space, 4047 SLAB_RECLAIM_ACCOUNT); 4048 if (ext4_pspace_cachep == NULL) 4049 goto out; 4050 4051 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context, 4052 SLAB_RECLAIM_ACCOUNT); 4053 if (ext4_ac_cachep == NULL) 4054 goto out_pa_free; 4055 4056 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data, 4057 SLAB_RECLAIM_ACCOUNT); 4058 if (ext4_free_data_cachep == NULL) 4059 goto out_ac_free; 4060 4061 return 0; 4062 4063 out_ac_free: 4064 kmem_cache_destroy(ext4_ac_cachep); 4065 out_pa_free: 4066 kmem_cache_destroy(ext4_pspace_cachep); 4067 out: 4068 return -ENOMEM; 4069 } 4070 4071 void ext4_exit_mballoc(void) 4072 { 4073 /* 4074 * Wait for completion of call_rcu()'s on ext4_pspace_cachep 4075 * before destroying the slab cache. 4076 */ 4077 rcu_barrier(); 4078 kmem_cache_destroy(ext4_pspace_cachep); 4079 kmem_cache_destroy(ext4_ac_cachep); 4080 kmem_cache_destroy(ext4_free_data_cachep); 4081 ext4_groupinfo_destroy_slabs(); 4082 } 4083 4084 #define EXT4_MB_BITMAP_MARKED_CHECK 0x0001 4085 #define EXT4_MB_SYNC_UPDATE 0x0002 4086 int 4087 ext4_mb_mark_context(handle_t *handle, struct super_block *sb, bool state, 4088 ext4_group_t group, ext4_grpblk_t blkoff, 4089 ext4_grpblk_t len, int flags, ext4_grpblk_t *ret_changed) 4090 { 4091 struct ext4_sb_info *sbi = EXT4_SB(sb); 4092 struct buffer_head *bitmap_bh = NULL; 4093 struct ext4_group_desc *gdp; 4094 struct buffer_head *gdp_bh; 4095 int err; 4096 unsigned int i, already, changed = len; 4097 4098 KUNIT_STATIC_STUB_REDIRECT(ext4_mb_mark_context, 4099 handle, sb, state, group, blkoff, len, 4100 flags, ret_changed); 4101 4102 if (ret_changed) 4103 *ret_changed = 0; 4104 bitmap_bh = ext4_read_block_bitmap(sb, group); 4105 if (IS_ERR(bitmap_bh)) 4106 return PTR_ERR(bitmap_bh); 4107 4108 if (handle) { 4109 BUFFER_TRACE(bitmap_bh, "getting write access"); 4110 err = ext4_journal_get_write_access(handle, sb, bitmap_bh, 4111 EXT4_JTR_NONE); 4112 if (err) 4113 goto out_err; 4114 } 4115 4116 err = -EIO; 4117 gdp = ext4_get_group_desc(sb, group, &gdp_bh); 4118 if (!gdp) 4119 goto out_err; 4120 4121 if (handle) { 4122 BUFFER_TRACE(gdp_bh, "get_write_access"); 4123 err = ext4_journal_get_write_access(handle, sb, gdp_bh, 4124 EXT4_JTR_NONE); 4125 if (err) 4126 goto out_err; 4127 } 4128 4129 ext4_lock_group(sb, group); 4130 if (ext4_has_group_desc_csum(sb) && 4131 (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { 4132 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); 4133 ext4_free_group_clusters_set(sb, gdp, 4134 ext4_free_clusters_after_init(sb, group, gdp)); 4135 } 4136 4137 if (flags & EXT4_MB_BITMAP_MARKED_CHECK) { 4138 already = 0; 4139 for (i = 0; i < len; i++) 4140 if (mb_test_bit(blkoff + i, bitmap_bh->b_data) == 4141 state) 4142 already++; 4143 changed = len - already; 4144 } 4145 4146 if (state) { 4147 mb_set_bits(bitmap_bh->b_data, blkoff, len); 4148 ext4_free_group_clusters_set(sb, gdp, 4149 ext4_free_group_clusters(sb, gdp) - changed); 4150 } else { 4151 mb_clear_bits(bitmap_bh->b_data, blkoff, len); 4152 ext4_free_group_clusters_set(sb, gdp, 4153 ext4_free_group_clusters(sb, gdp) + changed); 4154 } 4155 4156 ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh); 4157 ext4_group_desc_csum_set(sb, group, gdp); 4158 ext4_unlock_group(sb, group); 4159 if (ret_changed) 4160 *ret_changed = changed; 4161 4162 if (sbi->s_log_groups_per_flex) { 4163 ext4_group_t flex_group = ext4_flex_group(sbi, group); 4164 struct flex_groups *fg = sbi_array_rcu_deref(sbi, 4165 s_flex_groups, flex_group); 4166 4167 if (state) 4168 atomic64_sub(changed, &fg->free_clusters); 4169 else 4170 atomic64_add(changed, &fg->free_clusters); 4171 } 4172 4173 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh); 4174 if (err) 4175 goto out_err; 4176 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh); 4177 if (err) 4178 goto out_err; 4179 4180 if (flags & EXT4_MB_SYNC_UPDATE) { 4181 sync_dirty_buffer(bitmap_bh); 4182 sync_dirty_buffer(gdp_bh); 4183 } 4184 4185 out_err: 4186 brelse(bitmap_bh); 4187 return err; 4188 } 4189 4190 /* 4191 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps 4192 * Returns 0 if success or error code 4193 */ 4194 static noinline_for_stack int 4195 ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, handle_t *handle) 4196 { 4197 struct ext4_group_desc *gdp; 4198 struct ext4_sb_info *sbi; 4199 struct super_block *sb; 4200 ext4_fsblk_t block; 4201 int err, len; 4202 int flags = 0; 4203 ext4_grpblk_t changed; 4204 4205 BUG_ON(ac->ac_status != AC_STATUS_FOUND); 4206 BUG_ON(ac->ac_b_ex.fe_len <= 0); 4207 4208 sb = ac->ac_sb; 4209 sbi = EXT4_SB(sb); 4210 4211 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, NULL); 4212 if (!gdp) 4213 return -EIO; 4214 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group, 4215 ext4_free_group_clusters(sb, gdp)); 4216 4217 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); 4218 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len); 4219 if (!ext4_inode_block_valid(ac->ac_inode, block, len)) { 4220 ext4_error(sb, "Allocating blocks %llu-%llu which overlap " 4221 "fs metadata", block, block+len); 4222 /* File system mounted not to panic on error 4223 * Fix the bitmap and return EFSCORRUPTED 4224 * We leak some of the blocks here. 4225 */ 4226 err = ext4_mb_mark_context(handle, sb, true, 4227 ac->ac_b_ex.fe_group, 4228 ac->ac_b_ex.fe_start, 4229 ac->ac_b_ex.fe_len, 4230 0, NULL); 4231 if (!err) 4232 err = -EFSCORRUPTED; 4233 return err; 4234 } 4235 4236 #ifdef AGGRESSIVE_CHECK 4237 flags |= EXT4_MB_BITMAP_MARKED_CHECK; 4238 #endif 4239 err = ext4_mb_mark_context(handle, sb, true, ac->ac_b_ex.fe_group, 4240 ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len, 4241 flags, &changed); 4242 4243 if (err && changed == 0) 4244 return err; 4245 4246 #ifdef AGGRESSIVE_CHECK 4247 BUG_ON(changed != ac->ac_b_ex.fe_len); 4248 #endif 4249 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len); 4250 4251 return err; 4252 } 4253 4254 /* 4255 * Idempotent helper for Ext4 fast commit replay path to set the state of 4256 * blocks in bitmaps and update counters. 4257 */ 4258 void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block, 4259 int len, bool state) 4260 { 4261 struct ext4_sb_info *sbi = EXT4_SB(sb); 4262 ext4_group_t group; 4263 ext4_grpblk_t blkoff; 4264 int err = 0; 4265 unsigned int clen, thisgrp_len; 4266 4267 while (len > 0) { 4268 ext4_get_group_no_and_offset(sb, block, &group, &blkoff); 4269 4270 /* 4271 * Check to see if we are freeing blocks across a group 4272 * boundary. 4273 * In case of flex_bg, this can happen that (block, len) may 4274 * span across more than one group. In that case we need to 4275 * get the corresponding group metadata to work with. 4276 * For this we have goto again loop. 4277 */ 4278 thisgrp_len = min(len, EXT4_BLOCKS_PER_GROUP(sb) - EXT4_C2B(sbi, blkoff)); 4279 clen = EXT4_NUM_B2C(sbi, thisgrp_len); 4280 4281 if (!ext4_sb_block_valid(sb, NULL, block, thisgrp_len)) { 4282 ext4_error(sb, "Marking blocks in system zone - " 4283 "Block = %llu, len = %u", 4284 block, thisgrp_len); 4285 break; 4286 } 4287 4288 err = ext4_mb_mark_context(NULL, sb, state, 4289 group, blkoff, clen, 4290 EXT4_MB_BITMAP_MARKED_CHECK | 4291 EXT4_MB_SYNC_UPDATE, 4292 NULL); 4293 if (err) 4294 break; 4295 4296 block += thisgrp_len; 4297 len -= thisgrp_len; 4298 BUG_ON(len < 0); 4299 } 4300 } 4301 4302 /* 4303 * here we normalize request for locality group 4304 * Group request are normalized to s_mb_group_prealloc, which goes to 4305 * s_strip if we set the same via mount option. 4306 * s_mb_group_prealloc can be configured via 4307 * /sys/fs/ext4/<partition>/mb_group_prealloc 4308 * 4309 * XXX: should we try to preallocate more than the group has now? 4310 */ 4311 static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac) 4312 { 4313 struct super_block *sb = ac->ac_sb; 4314 struct ext4_locality_group *lg = ac->ac_lg; 4315 4316 BUG_ON(lg == NULL); 4317 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc; 4318 mb_debug(sb, "goal %u blocks for locality group\n", ac->ac_g_ex.fe_len); 4319 } 4320 4321 /* 4322 * This function returns the next element to look at during inode 4323 * PA rbtree walk. We assume that we have held the inode PA rbtree lock 4324 * (ei->i_prealloc_lock) 4325 * 4326 * new_start The start of the range we want to compare 4327 * cur_start The existing start that we are comparing against 4328 * node The node of the rb_tree 4329 */ 4330 static inline struct rb_node* 4331 ext4_mb_pa_rb_next_iter(ext4_lblk_t new_start, ext4_lblk_t cur_start, struct rb_node *node) 4332 { 4333 if (new_start < cur_start) 4334 return node->rb_left; 4335 else 4336 return node->rb_right; 4337 } 4338 4339 static inline void 4340 ext4_mb_pa_assert_overlap(struct ext4_allocation_context *ac, 4341 ext4_lblk_t start, loff_t end) 4342 { 4343 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 4344 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); 4345 struct ext4_prealloc_space *tmp_pa; 4346 ext4_lblk_t tmp_pa_start; 4347 loff_t tmp_pa_end; 4348 struct rb_node *iter; 4349 4350 read_lock(&ei->i_prealloc_lock); 4351 for (iter = ei->i_prealloc_node.rb_node; iter; 4352 iter = ext4_mb_pa_rb_next_iter(start, tmp_pa_start, iter)) { 4353 tmp_pa = rb_entry(iter, struct ext4_prealloc_space, 4354 pa_node.inode_node); 4355 tmp_pa_start = tmp_pa->pa_lstart; 4356 tmp_pa_end = pa_logical_end(sbi, tmp_pa); 4357 4358 spin_lock(&tmp_pa->pa_lock); 4359 if (tmp_pa->pa_deleted == 0) 4360 BUG_ON(!(start >= tmp_pa_end || end <= tmp_pa_start)); 4361 spin_unlock(&tmp_pa->pa_lock); 4362 } 4363 read_unlock(&ei->i_prealloc_lock); 4364 } 4365 4366 /* 4367 * Given an allocation context "ac" and a range "start", "end", check 4368 * and adjust boundaries if the range overlaps with any of the existing 4369 * preallocatoins stored in the corresponding inode of the allocation context. 4370 * 4371 * Parameters: 4372 * ac allocation context 4373 * start start of the new range 4374 * end end of the new range 4375 */ 4376 static inline void 4377 ext4_mb_pa_adjust_overlap(struct ext4_allocation_context *ac, 4378 ext4_lblk_t *start, loff_t *end) 4379 { 4380 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); 4381 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 4382 struct ext4_prealloc_space *tmp_pa = NULL, *left_pa = NULL, *right_pa = NULL; 4383 struct rb_node *iter; 4384 ext4_lblk_t new_start, tmp_pa_start, right_pa_start = -1; 4385 loff_t new_end, tmp_pa_end, left_pa_end = -1; 4386 4387 new_start = *start; 4388 new_end = *end; 4389 4390 /* 4391 * Adjust the normalized range so that it doesn't overlap with any 4392 * existing preallocated blocks(PAs). Make sure to hold the rbtree lock 4393 * so it doesn't change underneath us. 4394 */ 4395 read_lock(&ei->i_prealloc_lock); 4396 4397 /* Step 1: find any one immediate neighboring PA of the normalized range */ 4398 for (iter = ei->i_prealloc_node.rb_node; iter; 4399 iter = ext4_mb_pa_rb_next_iter(ac->ac_o_ex.fe_logical, 4400 tmp_pa_start, iter)) { 4401 tmp_pa = rb_entry(iter, struct ext4_prealloc_space, 4402 pa_node.inode_node); 4403 tmp_pa_start = tmp_pa->pa_lstart; 4404 tmp_pa_end = pa_logical_end(sbi, tmp_pa); 4405 4406 /* PA must not overlap original request */ 4407 spin_lock(&tmp_pa->pa_lock); 4408 if (tmp_pa->pa_deleted == 0) 4409 BUG_ON(!(ac->ac_o_ex.fe_logical >= tmp_pa_end || 4410 ac->ac_o_ex.fe_logical < tmp_pa_start)); 4411 spin_unlock(&tmp_pa->pa_lock); 4412 } 4413 4414 /* 4415 * Step 2: check if the found PA is left or right neighbor and 4416 * get the other neighbor 4417 */ 4418 if (tmp_pa) { 4419 if (tmp_pa->pa_lstart < ac->ac_o_ex.fe_logical) { 4420 struct rb_node *tmp; 4421 4422 left_pa = tmp_pa; 4423 tmp = rb_next(&left_pa->pa_node.inode_node); 4424 if (tmp) { 4425 right_pa = rb_entry(tmp, 4426 struct ext4_prealloc_space, 4427 pa_node.inode_node); 4428 } 4429 } else { 4430 struct rb_node *tmp; 4431 4432 right_pa = tmp_pa; 4433 tmp = rb_prev(&right_pa->pa_node.inode_node); 4434 if (tmp) { 4435 left_pa = rb_entry(tmp, 4436 struct ext4_prealloc_space, 4437 pa_node.inode_node); 4438 } 4439 } 4440 } 4441 4442 /* Step 3: get the non deleted neighbors */ 4443 if (left_pa) { 4444 for (iter = &left_pa->pa_node.inode_node;; 4445 iter = rb_prev(iter)) { 4446 if (!iter) { 4447 left_pa = NULL; 4448 break; 4449 } 4450 4451 tmp_pa = rb_entry(iter, struct ext4_prealloc_space, 4452 pa_node.inode_node); 4453 left_pa = tmp_pa; 4454 spin_lock(&tmp_pa->pa_lock); 4455 if (tmp_pa->pa_deleted == 0) { 4456 spin_unlock(&tmp_pa->pa_lock); 4457 break; 4458 } 4459 spin_unlock(&tmp_pa->pa_lock); 4460 } 4461 } 4462 4463 if (right_pa) { 4464 for (iter = &right_pa->pa_node.inode_node;; 4465 iter = rb_next(iter)) { 4466 if (!iter) { 4467 right_pa = NULL; 4468 break; 4469 } 4470 4471 tmp_pa = rb_entry(iter, struct ext4_prealloc_space, 4472 pa_node.inode_node); 4473 right_pa = tmp_pa; 4474 spin_lock(&tmp_pa->pa_lock); 4475 if (tmp_pa->pa_deleted == 0) { 4476 spin_unlock(&tmp_pa->pa_lock); 4477 break; 4478 } 4479 spin_unlock(&tmp_pa->pa_lock); 4480 } 4481 } 4482 4483 if (left_pa) { 4484 left_pa_end = pa_logical_end(sbi, left_pa); 4485 BUG_ON(left_pa_end > ac->ac_o_ex.fe_logical); 4486 } 4487 4488 if (right_pa) { 4489 right_pa_start = right_pa->pa_lstart; 4490 BUG_ON(right_pa_start <= ac->ac_o_ex.fe_logical); 4491 } 4492 4493 /* Step 4: trim our normalized range to not overlap with the neighbors */ 4494 if (left_pa) { 4495 if (left_pa_end > new_start) 4496 new_start = left_pa_end; 4497 } 4498 4499 if (right_pa) { 4500 if (right_pa_start < new_end) 4501 new_end = right_pa_start; 4502 } 4503 read_unlock(&ei->i_prealloc_lock); 4504 4505 /* XXX: extra loop to check we really don't overlap preallocations */ 4506 ext4_mb_pa_assert_overlap(ac, new_start, new_end); 4507 4508 *start = new_start; 4509 *end = new_end; 4510 } 4511 4512 /* 4513 * Normalization means making request better in terms of 4514 * size and alignment 4515 */ 4516 static noinline_for_stack void 4517 ext4_mb_normalize_request(struct ext4_allocation_context *ac, 4518 struct ext4_allocation_request *ar) 4519 { 4520 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 4521 struct ext4_super_block *es = sbi->s_es; 4522 int bsbits, max; 4523 loff_t size, start_off, end; 4524 loff_t orig_size __maybe_unused; 4525 ext4_lblk_t start; 4526 4527 /* do normalize only data requests, metadata requests 4528 do not need preallocation */ 4529 if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) 4530 return; 4531 4532 /* sometime caller may want exact blocks */ 4533 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) 4534 return; 4535 4536 /* caller may indicate that preallocation isn't 4537 * required (it's a tail, for example) */ 4538 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC) 4539 return; 4540 4541 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) { 4542 ext4_mb_normalize_group_request(ac); 4543 return ; 4544 } 4545 4546 bsbits = ac->ac_sb->s_blocksize_bits; 4547 4548 /* first, let's learn actual file size 4549 * given current request is allocated */ 4550 size = extent_logical_end(sbi, &ac->ac_o_ex); 4551 size = size << bsbits; 4552 if (size < i_size_read(ac->ac_inode)) 4553 size = i_size_read(ac->ac_inode); 4554 orig_size = size; 4555 4556 /* max size of free chunks */ 4557 max = 2 << bsbits; 4558 4559 #define NRL_CHECK_SIZE(req, size, max, chunk_size) \ 4560 (req <= (size) || max <= (chunk_size)) 4561 4562 /* first, try to predict filesize */ 4563 start_off = 0; 4564 if (size <= SZ_1M) { 4565 /* 4566 * For files up to 1MB, round up the preallocation size to 4567 * the next power of two, with a minimum of 16KB. 4568 */ 4569 if (size <= (unsigned long)SZ_16K) 4570 size = SZ_16K; 4571 else 4572 size = roundup_pow_of_two(size); 4573 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) { 4574 start_off = ((loff_t)ac->ac_o_ex.fe_logical >> 4575 (21 - bsbits)) << 21; 4576 size = 2 * 1024 * 1024; 4577 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) { 4578 start_off = ((loff_t)ac->ac_o_ex.fe_logical >> 4579 (22 - bsbits)) << 22; 4580 size = 4 * 1024 * 1024; 4581 } else if (NRL_CHECK_SIZE(EXT4_C2B(sbi, ac->ac_o_ex.fe_len), 4582 (8<<20)>>bsbits, max, 8 * 1024)) { 4583 start_off = ((loff_t)ac->ac_o_ex.fe_logical >> 4584 (23 - bsbits)) << 23; 4585 size = 8 * 1024 * 1024; 4586 } else { 4587 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits; 4588 size = (loff_t) EXT4_C2B(sbi, 4589 ac->ac_o_ex.fe_len) << bsbits; 4590 } 4591 size = size >> bsbits; 4592 start = start_off >> bsbits; 4593 4594 /* 4595 * For tiny groups (smaller than 8MB) the chosen allocation 4596 * alignment may be larger than group size. Make sure the 4597 * alignment does not move allocation to a different group which 4598 * makes mballoc fail assertions later. 4599 */ 4600 start = max(start, rounddown(ac->ac_o_ex.fe_logical, 4601 (ext4_lblk_t)EXT4_BLOCKS_PER_GROUP(ac->ac_sb))); 4602 4603 /* avoid unnecessary preallocation that may trigger assertions */ 4604 if (start + size > EXT_MAX_BLOCKS) 4605 size = EXT_MAX_BLOCKS - start; 4606 4607 /* don't cover already allocated blocks in selected range */ 4608 if (ar->pleft && start <= ar->lleft) { 4609 size -= ar->lleft + 1 - start; 4610 start = ar->lleft + 1; 4611 } 4612 if (ar->pright && start + size - 1 >= ar->lright) 4613 size -= start + size - ar->lright; 4614 4615 /* 4616 * Trim allocation request for filesystems with artificially small 4617 * groups. 4618 */ 4619 if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) 4620 size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb); 4621 4622 end = start + size; 4623 4624 ext4_mb_pa_adjust_overlap(ac, &start, &end); 4625 4626 size = end - start; 4627 4628 /* 4629 * In this function "start" and "size" are normalized for better 4630 * alignment and length such that we could preallocate more blocks. 4631 * This normalization is done such that original request of 4632 * ac->ac_o_ex.fe_logical & fe_len should always lie within "start" and 4633 * "size" boundaries. 4634 * (Note fe_len can be relaxed since FS block allocation API does not 4635 * provide gurantee on number of contiguous blocks allocation since that 4636 * depends upon free space left, etc). 4637 * In case of inode pa, later we use the allocated blocks 4638 * [pa_pstart + fe_logical - pa_lstart, fe_len/size] from the preallocated 4639 * range of goal/best blocks [start, size] to put it at the 4640 * ac_o_ex.fe_logical extent of this inode. 4641 * (See ext4_mb_use_inode_pa() for more details) 4642 */ 4643 if (start + size <= ac->ac_o_ex.fe_logical || 4644 start > ac->ac_o_ex.fe_logical) { 4645 ext4_msg(ac->ac_sb, KERN_ERR, 4646 "start %lu, size %lu, fe_logical %lu", 4647 (unsigned long) start, (unsigned long) size, 4648 (unsigned long) ac->ac_o_ex.fe_logical); 4649 BUG(); 4650 } 4651 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb)); 4652 4653 /* now prepare goal request */ 4654 4655 /* XXX: is it better to align blocks WRT to logical 4656 * placement or satisfy big request as is */ 4657 ac->ac_g_ex.fe_logical = start; 4658 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size); 4659 ac->ac_orig_goal_len = ac->ac_g_ex.fe_len; 4660 4661 /* define goal start in order to merge */ 4662 if (ar->pright && (ar->lright == (start + size)) && 4663 ar->pright >= size && 4664 ar->pright - size >= le32_to_cpu(es->s_first_data_block)) { 4665 /* merge to the right */ 4666 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size, 4667 &ac->ac_g_ex.fe_group, 4668 &ac->ac_g_ex.fe_start); 4669 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; 4670 } 4671 if (ar->pleft && (ar->lleft + 1 == start) && 4672 ar->pleft + 1 < ext4_blocks_count(es)) { 4673 /* merge to the left */ 4674 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1, 4675 &ac->ac_g_ex.fe_group, 4676 &ac->ac_g_ex.fe_start); 4677 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL; 4678 } 4679 4680 mb_debug(ac->ac_sb, "goal: %lld(was %lld) blocks at %u\n", size, 4681 orig_size, start); 4682 } 4683 4684 static void ext4_mb_collect_stats(struct ext4_allocation_context *ac) 4685 { 4686 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 4687 4688 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len >= 1) { 4689 atomic_inc(&sbi->s_bal_reqs); 4690 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated); 4691 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len) 4692 atomic_inc(&sbi->s_bal_success); 4693 4694 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned); 4695 for (int i=0; i<EXT4_MB_NUM_CRS; i++) { 4696 atomic_add(ac->ac_cX_found[i], &sbi->s_bal_cX_ex_scanned[i]); 4697 } 4698 4699 atomic_add(ac->ac_groups_scanned, &sbi->s_bal_groups_scanned); 4700 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start && 4701 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group) 4702 atomic_inc(&sbi->s_bal_goals); 4703 /* did we allocate as much as normalizer originally wanted? */ 4704 if (ac->ac_f_ex.fe_len == ac->ac_orig_goal_len) 4705 atomic_inc(&sbi->s_bal_len_goals); 4706 4707 if (ac->ac_found > sbi->s_mb_max_to_scan) 4708 atomic_inc(&sbi->s_bal_breaks); 4709 } 4710 4711 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) 4712 trace_ext4_mballoc_alloc(ac); 4713 else 4714 trace_ext4_mballoc_prealloc(ac); 4715 } 4716 4717 /* 4718 * Called on failure; free up any blocks from the inode PA for this 4719 * context. We don't need this for MB_GROUP_PA because we only change 4720 * pa_free in ext4_mb_release_context(), but on failure, we've already 4721 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed. 4722 */ 4723 static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac) 4724 { 4725 struct ext4_prealloc_space *pa = ac->ac_pa; 4726 struct ext4_buddy e4b; 4727 int err; 4728 4729 if (pa == NULL) { 4730 if (ac->ac_f_ex.fe_len == 0) 4731 return; 4732 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b); 4733 if (WARN_RATELIMIT(err, 4734 "ext4: mb_load_buddy failed (%d)", err)) 4735 /* 4736 * This should never happen since we pin the 4737 * folios in the ext4_allocation_context so 4738 * ext4_mb_load_buddy() should never fail. 4739 */ 4740 return; 4741 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group); 4742 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start, 4743 ac->ac_f_ex.fe_len); 4744 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group); 4745 ext4_mb_unload_buddy(&e4b); 4746 return; 4747 } 4748 if (pa->pa_type == MB_INODE_PA) { 4749 spin_lock(&pa->pa_lock); 4750 pa->pa_free += ac->ac_b_ex.fe_len; 4751 spin_unlock(&pa->pa_lock); 4752 } 4753 } 4754 4755 /* 4756 * use blocks preallocated to inode 4757 */ 4758 static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac, 4759 struct ext4_prealloc_space *pa) 4760 { 4761 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 4762 ext4_fsblk_t start; 4763 ext4_fsblk_t end; 4764 int len; 4765 4766 /* found preallocated blocks, use them */ 4767 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart); 4768 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len), 4769 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len)); 4770 len = EXT4_NUM_B2C(sbi, end - start); 4771 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group, 4772 &ac->ac_b_ex.fe_start); 4773 ac->ac_b_ex.fe_len = len; 4774 ac->ac_status = AC_STATUS_FOUND; 4775 ac->ac_pa = pa; 4776 4777 BUG_ON(start < pa->pa_pstart); 4778 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len)); 4779 BUG_ON(pa->pa_free < len); 4780 BUG_ON(ac->ac_b_ex.fe_len <= 0); 4781 pa->pa_free -= len; 4782 4783 mb_debug(ac->ac_sb, "use %llu/%d from inode pa %p\n", start, len, pa); 4784 } 4785 4786 /* 4787 * use blocks preallocated to locality group 4788 */ 4789 static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac, 4790 struct ext4_prealloc_space *pa) 4791 { 4792 unsigned int len = ac->ac_o_ex.fe_len; 4793 4794 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart, 4795 &ac->ac_b_ex.fe_group, 4796 &ac->ac_b_ex.fe_start); 4797 ac->ac_b_ex.fe_len = len; 4798 ac->ac_status = AC_STATUS_FOUND; 4799 ac->ac_pa = pa; 4800 4801 /* we don't correct pa_pstart or pa_len here to avoid 4802 * possible race when the group is being loaded concurrently 4803 * instead we correct pa later, after blocks are marked 4804 * in on-disk bitmap -- see ext4_mb_release_context() 4805 * Other CPUs are prevented from allocating from this pa by lg_mutex 4806 */ 4807 mb_debug(ac->ac_sb, "use %u/%u from group pa %p\n", 4808 pa->pa_lstart, len, pa); 4809 } 4810 4811 /* 4812 * Return the prealloc space that have minimal distance 4813 * from the goal block. @cpa is the prealloc 4814 * space that is having currently known minimal distance 4815 * from the goal block. 4816 */ 4817 static struct ext4_prealloc_space * 4818 ext4_mb_check_group_pa(ext4_fsblk_t goal_block, 4819 struct ext4_prealloc_space *pa, 4820 struct ext4_prealloc_space *cpa) 4821 { 4822 ext4_fsblk_t cur_distance, new_distance; 4823 4824 if (cpa == NULL) { 4825 atomic_inc(&pa->pa_count); 4826 return pa; 4827 } 4828 cur_distance = abs(goal_block - cpa->pa_pstart); 4829 new_distance = abs(goal_block - pa->pa_pstart); 4830 4831 if (cur_distance <= new_distance) 4832 return cpa; 4833 4834 /* drop the previous reference */ 4835 atomic_dec(&cpa->pa_count); 4836 atomic_inc(&pa->pa_count); 4837 return pa; 4838 } 4839 4840 /* 4841 * check if found pa meets EXT4_MB_HINT_GOAL_ONLY 4842 */ 4843 static bool 4844 ext4_mb_pa_goal_check(struct ext4_allocation_context *ac, 4845 struct ext4_prealloc_space *pa) 4846 { 4847 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 4848 ext4_fsblk_t start; 4849 4850 if (likely(!(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))) 4851 return true; 4852 4853 /* 4854 * If EXT4_MB_HINT_GOAL_ONLY is set, ac_g_ex will not be adjusted 4855 * in ext4_mb_normalize_request and will keep same with ac_o_ex 4856 * from ext4_mb_initialize_context. Choose ac_g_ex here to keep 4857 * consistent with ext4_mb_find_by_goal. 4858 */ 4859 start = pa->pa_pstart + 4860 (ac->ac_g_ex.fe_logical - pa->pa_lstart); 4861 if (ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex) != start) 4862 return false; 4863 4864 if (ac->ac_g_ex.fe_len > pa->pa_len - 4865 EXT4_B2C(sbi, ac->ac_g_ex.fe_logical - pa->pa_lstart)) 4866 return false; 4867 4868 return true; 4869 } 4870 4871 /* 4872 * search goal blocks in preallocated space 4873 */ 4874 static noinline_for_stack bool 4875 ext4_mb_use_preallocated(struct ext4_allocation_context *ac) 4876 { 4877 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 4878 int order, i; 4879 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); 4880 struct ext4_locality_group *lg; 4881 struct ext4_prealloc_space *tmp_pa = NULL, *cpa = NULL; 4882 struct rb_node *iter; 4883 ext4_fsblk_t goal_block; 4884 4885 /* only data can be preallocated */ 4886 if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) 4887 return false; 4888 4889 /* 4890 * first, try per-file preallocation by searching the inode pa rbtree. 4891 * 4892 * Here, we can't do a direct traversal of the tree because 4893 * ext4_mb_discard_group_preallocation() can paralelly mark the pa 4894 * deleted and that can cause direct traversal to skip some entries. 4895 */ 4896 read_lock(&ei->i_prealloc_lock); 4897 4898 if (RB_EMPTY_ROOT(&ei->i_prealloc_node)) { 4899 goto try_group_pa; 4900 } 4901 4902 /* 4903 * Step 1: Find a pa with logical start immediately adjacent to the 4904 * original logical start. This could be on the left or right. 4905 * 4906 * (tmp_pa->pa_lstart never changes so we can skip locking for it). 4907 */ 4908 for (iter = ei->i_prealloc_node.rb_node; iter; 4909 iter = ext4_mb_pa_rb_next_iter(ac->ac_o_ex.fe_logical, 4910 tmp_pa->pa_lstart, iter)) { 4911 tmp_pa = rb_entry(iter, struct ext4_prealloc_space, 4912 pa_node.inode_node); 4913 } 4914 4915 /* 4916 * Step 2: The adjacent pa might be to the right of logical start, find 4917 * the left adjacent pa. After this step we'd have a valid tmp_pa whose 4918 * logical start is towards the left of original request's logical start 4919 */ 4920 if (tmp_pa->pa_lstart > ac->ac_o_ex.fe_logical) { 4921 struct rb_node *tmp; 4922 tmp = rb_prev(&tmp_pa->pa_node.inode_node); 4923 4924 if (tmp) { 4925 tmp_pa = rb_entry(tmp, struct ext4_prealloc_space, 4926 pa_node.inode_node); 4927 } else { 4928 /* 4929 * If there is no adjacent pa to the left then finding 4930 * an overlapping pa is not possible hence stop searching 4931 * inode pa tree 4932 */ 4933 goto try_group_pa; 4934 } 4935 } 4936 4937 BUG_ON(!(tmp_pa && tmp_pa->pa_lstart <= ac->ac_o_ex.fe_logical)); 4938 4939 /* 4940 * Step 3: If the left adjacent pa is deleted, keep moving left to find 4941 * the first non deleted adjacent pa. After this step we should have a 4942 * valid tmp_pa which is guaranteed to be non deleted. 4943 */ 4944 for (iter = &tmp_pa->pa_node.inode_node;; iter = rb_prev(iter)) { 4945 if (!iter) { 4946 /* 4947 * no non deleted left adjacent pa, so stop searching 4948 * inode pa tree 4949 */ 4950 goto try_group_pa; 4951 } 4952 tmp_pa = rb_entry(iter, struct ext4_prealloc_space, 4953 pa_node.inode_node); 4954 spin_lock(&tmp_pa->pa_lock); 4955 if (tmp_pa->pa_deleted == 0) { 4956 /* 4957 * We will keep holding the pa_lock from 4958 * this point on because we don't want group discard 4959 * to delete this pa underneath us. Since group 4960 * discard is anyways an ENOSPC operation it 4961 * should be okay for it to wait a few more cycles. 4962 */ 4963 break; 4964 } else { 4965 spin_unlock(&tmp_pa->pa_lock); 4966 } 4967 } 4968 4969 BUG_ON(!(tmp_pa && tmp_pa->pa_lstart <= ac->ac_o_ex.fe_logical)); 4970 BUG_ON(tmp_pa->pa_deleted == 1); 4971 4972 /* 4973 * Step 4: We now have the non deleted left adjacent pa. Only this 4974 * pa can possibly satisfy the request hence check if it overlaps 4975 * original logical start and stop searching if it doesn't. 4976 */ 4977 if (ac->ac_o_ex.fe_logical >= pa_logical_end(sbi, tmp_pa)) { 4978 spin_unlock(&tmp_pa->pa_lock); 4979 goto try_group_pa; 4980 } 4981 4982 /* non-extent files can't have physical blocks past 2^32 */ 4983 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) && 4984 (tmp_pa->pa_pstart + EXT4_C2B(sbi, tmp_pa->pa_len) > 4985 EXT4_MAX_BLOCK_FILE_PHYS)) { 4986 /* 4987 * Since PAs don't overlap, we won't find any other PA to 4988 * satisfy this. 4989 */ 4990 spin_unlock(&tmp_pa->pa_lock); 4991 goto try_group_pa; 4992 } 4993 4994 if (tmp_pa->pa_free && likely(ext4_mb_pa_goal_check(ac, tmp_pa))) { 4995 atomic_inc(&tmp_pa->pa_count); 4996 ext4_mb_use_inode_pa(ac, tmp_pa); 4997 spin_unlock(&tmp_pa->pa_lock); 4998 read_unlock(&ei->i_prealloc_lock); 4999 return true; 5000 } else { 5001 /* 5002 * We found a valid overlapping pa but couldn't use it because 5003 * it had no free blocks. This should ideally never happen 5004 * because: 5005 * 5006 * 1. When a new inode pa is added to rbtree it must have 5007 * pa_free > 0 since otherwise we won't actually need 5008 * preallocation. 5009 * 5010 * 2. An inode pa that is in the rbtree can only have it's 5011 * pa_free become zero when another thread calls: 5012 * ext4_mb_new_blocks 5013 * ext4_mb_use_preallocated 5014 * ext4_mb_use_inode_pa 5015 * 5016 * 3. Further, after the above calls make pa_free == 0, we will 5017 * immediately remove it from the rbtree in: 5018 * ext4_mb_new_blocks 5019 * ext4_mb_release_context 5020 * ext4_mb_put_pa 5021 * 5022 * 4. Since the pa_free becoming 0 and pa_free getting removed 5023 * from tree both happen in ext4_mb_new_blocks, which is always 5024 * called with i_data_sem held for data allocations, we can be 5025 * sure that another process will never see a pa in rbtree with 5026 * pa_free == 0. 5027 */ 5028 WARN_ON_ONCE(tmp_pa->pa_free == 0); 5029 } 5030 spin_unlock(&tmp_pa->pa_lock); 5031 try_group_pa: 5032 read_unlock(&ei->i_prealloc_lock); 5033 5034 /* can we use group allocation? */ 5035 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)) 5036 return false; 5037 5038 /* inode may have no locality group for some reason */ 5039 lg = ac->ac_lg; 5040 if (lg == NULL) 5041 return false; 5042 order = fls(ac->ac_o_ex.fe_len) - 1; 5043 if (order > PREALLOC_TB_SIZE - 1) 5044 /* The max size of hash table is PREALLOC_TB_SIZE */ 5045 order = PREALLOC_TB_SIZE - 1; 5046 5047 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex); 5048 /* 5049 * search for the prealloc space that is having 5050 * minimal distance from the goal block. 5051 */ 5052 for (i = order; i < PREALLOC_TB_SIZE; i++) { 5053 rcu_read_lock(); 5054 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[i], 5055 pa_node.lg_list) { 5056 spin_lock(&tmp_pa->pa_lock); 5057 if (tmp_pa->pa_deleted == 0 && 5058 tmp_pa->pa_free >= ac->ac_o_ex.fe_len) { 5059 5060 cpa = ext4_mb_check_group_pa(goal_block, 5061 tmp_pa, cpa); 5062 } 5063 spin_unlock(&tmp_pa->pa_lock); 5064 } 5065 rcu_read_unlock(); 5066 } 5067 if (cpa) { 5068 ext4_mb_use_group_pa(ac, cpa); 5069 return true; 5070 } 5071 return false; 5072 } 5073 5074 /* 5075 * the function goes through all preallocation in this group and marks them 5076 * used in in-core bitmap. buddy must be generated from this bitmap 5077 * Need to be called with ext4 group lock held 5078 */ 5079 static noinline_for_stack 5080 void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap, 5081 ext4_group_t group) 5082 { 5083 struct ext4_group_info *grp = ext4_get_group_info(sb, group); 5084 struct ext4_prealloc_space *pa; 5085 struct list_head *cur; 5086 ext4_group_t groupnr; 5087 ext4_grpblk_t start; 5088 int preallocated = 0; 5089 int len; 5090 5091 if (!grp) 5092 return; 5093 5094 /* all form of preallocation discards first load group, 5095 * so the only competing code is preallocation use. 5096 * we don't need any locking here 5097 * notice we do NOT ignore preallocations with pa_deleted 5098 * otherwise we could leave used blocks available for 5099 * allocation in buddy when concurrent ext4_mb_put_pa() 5100 * is dropping preallocation 5101 */ 5102 list_for_each(cur, &grp->bb_prealloc_list) { 5103 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list); 5104 spin_lock(&pa->pa_lock); 5105 ext4_get_group_no_and_offset(sb, pa->pa_pstart, 5106 &groupnr, &start); 5107 len = pa->pa_len; 5108 spin_unlock(&pa->pa_lock); 5109 if (unlikely(len == 0)) 5110 continue; 5111 BUG_ON(groupnr != group); 5112 mb_set_bits(bitmap, start, len); 5113 preallocated += len; 5114 } 5115 mb_debug(sb, "preallocated %d for group %u\n", preallocated, group); 5116 } 5117 5118 static void ext4_mb_mark_pa_deleted(struct super_block *sb, 5119 struct ext4_prealloc_space *pa) 5120 { 5121 struct ext4_inode_info *ei; 5122 5123 if (pa->pa_deleted) { 5124 ext4_warning(sb, "deleted pa, type:%d, pblk:%llu, lblk:%u, len:%d\n", 5125 pa->pa_type, pa->pa_pstart, pa->pa_lstart, 5126 pa->pa_len); 5127 return; 5128 } 5129 5130 pa->pa_deleted = 1; 5131 5132 if (pa->pa_type == MB_INODE_PA) { 5133 ei = EXT4_I(pa->pa_inode); 5134 atomic_dec(&ei->i_prealloc_active); 5135 } 5136 } 5137 5138 static inline void ext4_mb_pa_free(struct ext4_prealloc_space *pa) 5139 { 5140 BUG_ON(!pa); 5141 BUG_ON(atomic_read(&pa->pa_count)); 5142 BUG_ON(pa->pa_deleted == 0); 5143 kmem_cache_free(ext4_pspace_cachep, pa); 5144 } 5145 5146 static void ext4_mb_pa_callback(struct rcu_head *head) 5147 { 5148 struct ext4_prealloc_space *pa; 5149 5150 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu); 5151 ext4_mb_pa_free(pa); 5152 } 5153 5154 /* 5155 * drops a reference to preallocated space descriptor 5156 * if this was the last reference and the space is consumed 5157 */ 5158 static void ext4_mb_put_pa(struct ext4_allocation_context *ac, 5159 struct super_block *sb, struct ext4_prealloc_space *pa) 5160 { 5161 ext4_group_t grp; 5162 ext4_fsblk_t grp_blk; 5163 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode); 5164 5165 /* in this short window concurrent discard can set pa_deleted */ 5166 spin_lock(&pa->pa_lock); 5167 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) { 5168 spin_unlock(&pa->pa_lock); 5169 return; 5170 } 5171 5172 if (pa->pa_deleted == 1) { 5173 spin_unlock(&pa->pa_lock); 5174 return; 5175 } 5176 5177 ext4_mb_mark_pa_deleted(sb, pa); 5178 spin_unlock(&pa->pa_lock); 5179 5180 grp_blk = pa->pa_pstart; 5181 /* 5182 * If doing group-based preallocation, pa_pstart may be in the 5183 * next group when pa is used up 5184 */ 5185 if (pa->pa_type == MB_GROUP_PA) 5186 grp_blk--; 5187 5188 grp = ext4_get_group_number(sb, grp_blk); 5189 5190 /* 5191 * possible race: 5192 * 5193 * P1 (buddy init) P2 (regular allocation) 5194 * find block B in PA 5195 * copy on-disk bitmap to buddy 5196 * mark B in on-disk bitmap 5197 * drop PA from group 5198 * mark all PAs in buddy 5199 * 5200 * thus, P1 initializes buddy with B available. to prevent this 5201 * we make "copy" and "mark all PAs" atomic and serialize "drop PA" 5202 * against that pair 5203 */ 5204 ext4_lock_group(sb, grp); 5205 list_del(&pa->pa_group_list); 5206 ext4_unlock_group(sb, grp); 5207 5208 if (pa->pa_type == MB_INODE_PA) { 5209 write_lock(pa->pa_node_lock.inode_lock); 5210 rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node); 5211 write_unlock(pa->pa_node_lock.inode_lock); 5212 ext4_mb_pa_free(pa); 5213 } else { 5214 spin_lock(pa->pa_node_lock.lg_lock); 5215 list_del_rcu(&pa->pa_node.lg_list); 5216 spin_unlock(pa->pa_node_lock.lg_lock); 5217 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); 5218 } 5219 } 5220 5221 static void ext4_mb_pa_rb_insert(struct rb_root *root, struct rb_node *new) 5222 { 5223 struct rb_node **iter = &root->rb_node, *parent = NULL; 5224 struct ext4_prealloc_space *iter_pa, *new_pa; 5225 ext4_lblk_t iter_start, new_start; 5226 5227 while (*iter) { 5228 iter_pa = rb_entry(*iter, struct ext4_prealloc_space, 5229 pa_node.inode_node); 5230 new_pa = rb_entry(new, struct ext4_prealloc_space, 5231 pa_node.inode_node); 5232 iter_start = iter_pa->pa_lstart; 5233 new_start = new_pa->pa_lstart; 5234 5235 parent = *iter; 5236 if (new_start < iter_start) 5237 iter = &((*iter)->rb_left); 5238 else 5239 iter = &((*iter)->rb_right); 5240 } 5241 5242 rb_link_node(new, parent, iter); 5243 rb_insert_color(new, root); 5244 } 5245 5246 /* 5247 * creates new preallocated space for given inode 5248 */ 5249 static noinline_for_stack void 5250 ext4_mb_new_inode_pa(struct ext4_allocation_context *ac) 5251 { 5252 struct super_block *sb = ac->ac_sb; 5253 struct ext4_sb_info *sbi = EXT4_SB(sb); 5254 struct ext4_prealloc_space *pa; 5255 struct ext4_group_info *grp; 5256 struct ext4_inode_info *ei; 5257 5258 /* preallocate only when found space is larger then requested */ 5259 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); 5260 BUG_ON(ac->ac_status != AC_STATUS_FOUND); 5261 BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); 5262 BUG_ON(ac->ac_pa == NULL); 5263 5264 pa = ac->ac_pa; 5265 5266 if (ac->ac_b_ex.fe_len < ac->ac_orig_goal_len) { 5267 struct ext4_free_extent ex = { 5268 .fe_logical = ac->ac_g_ex.fe_logical, 5269 .fe_len = ac->ac_orig_goal_len, 5270 }; 5271 loff_t orig_goal_end = extent_logical_end(sbi, &ex); 5272 loff_t o_ex_end = extent_logical_end(sbi, &ac->ac_o_ex); 5273 5274 /* 5275 * We can't allocate as much as normalizer wants, so we try 5276 * to get proper lstart to cover the original request, except 5277 * when the goal doesn't cover the original request as below: 5278 * 5279 * orig_ex:2045/2055(10), isize:8417280 -> normalized:0/2048 5280 * best_ex:0/200(200) -> adjusted: 1848/2048(200) 5281 */ 5282 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical); 5283 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len); 5284 5285 /* 5286 * Use the below logic for adjusting best extent as it keeps 5287 * fragmentation in check while ensuring logical range of best 5288 * extent doesn't overflow out of goal extent: 5289 * 5290 * 1. Check if best ex can be kept at end of goal (before 5291 * cr_best_avail trimmed it) and still cover original start 5292 * 2. Else, check if best ex can be kept at start of goal and 5293 * still cover original end 5294 * 3. Else, keep the best ex at start of original request. 5295 */ 5296 ex.fe_len = ac->ac_b_ex.fe_len; 5297 5298 ex.fe_logical = orig_goal_end - EXT4_C2B(sbi, ex.fe_len); 5299 if (ac->ac_o_ex.fe_logical >= ex.fe_logical) 5300 goto adjust_bex; 5301 5302 ex.fe_logical = ac->ac_g_ex.fe_logical; 5303 if (o_ex_end <= extent_logical_end(sbi, &ex)) 5304 goto adjust_bex; 5305 5306 ex.fe_logical = ac->ac_o_ex.fe_logical; 5307 adjust_bex: 5308 ac->ac_b_ex.fe_logical = ex.fe_logical; 5309 5310 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical); 5311 BUG_ON(extent_logical_end(sbi, &ex) > orig_goal_end); 5312 } 5313 5314 pa->pa_lstart = ac->ac_b_ex.fe_logical; 5315 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); 5316 pa->pa_len = ac->ac_b_ex.fe_len; 5317 pa->pa_free = pa->pa_len; 5318 spin_lock_init(&pa->pa_lock); 5319 INIT_LIST_HEAD(&pa->pa_group_list); 5320 pa->pa_deleted = 0; 5321 pa->pa_type = MB_INODE_PA; 5322 5323 mb_debug(sb, "new inode pa %p: %llu/%d for %u\n", pa, pa->pa_pstart, 5324 pa->pa_len, pa->pa_lstart); 5325 trace_ext4_mb_new_inode_pa(ac, pa); 5326 5327 atomic_add(pa->pa_free, &sbi->s_mb_preallocated); 5328 ext4_mb_use_inode_pa(ac, pa); 5329 5330 ei = EXT4_I(ac->ac_inode); 5331 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); 5332 if (!grp) 5333 return; 5334 5335 pa->pa_node_lock.inode_lock = &ei->i_prealloc_lock; 5336 pa->pa_inode = ac->ac_inode; 5337 5338 list_add(&pa->pa_group_list, &grp->bb_prealloc_list); 5339 5340 write_lock(pa->pa_node_lock.inode_lock); 5341 ext4_mb_pa_rb_insert(&ei->i_prealloc_node, &pa->pa_node.inode_node); 5342 write_unlock(pa->pa_node_lock.inode_lock); 5343 atomic_inc(&ei->i_prealloc_active); 5344 } 5345 5346 /* 5347 * creates new preallocated space for locality group inodes belongs to 5348 */ 5349 static noinline_for_stack void 5350 ext4_mb_new_group_pa(struct ext4_allocation_context *ac) 5351 { 5352 struct super_block *sb = ac->ac_sb; 5353 struct ext4_locality_group *lg; 5354 struct ext4_prealloc_space *pa; 5355 struct ext4_group_info *grp; 5356 5357 /* preallocate only when found space is larger then requested */ 5358 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len); 5359 BUG_ON(ac->ac_status != AC_STATUS_FOUND); 5360 BUG_ON(!S_ISREG(ac->ac_inode->i_mode)); 5361 BUG_ON(ac->ac_pa == NULL); 5362 5363 pa = ac->ac_pa; 5364 5365 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); 5366 pa->pa_lstart = pa->pa_pstart; 5367 pa->pa_len = ac->ac_b_ex.fe_len; 5368 pa->pa_free = pa->pa_len; 5369 spin_lock_init(&pa->pa_lock); 5370 INIT_LIST_HEAD(&pa->pa_node.lg_list); 5371 INIT_LIST_HEAD(&pa->pa_group_list); 5372 pa->pa_deleted = 0; 5373 pa->pa_type = MB_GROUP_PA; 5374 5375 mb_debug(sb, "new group pa %p: %llu/%d for %u\n", pa, pa->pa_pstart, 5376 pa->pa_len, pa->pa_lstart); 5377 trace_ext4_mb_new_group_pa(ac, pa); 5378 5379 ext4_mb_use_group_pa(ac, pa); 5380 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated); 5381 5382 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group); 5383 if (!grp) 5384 return; 5385 lg = ac->ac_lg; 5386 BUG_ON(lg == NULL); 5387 5388 pa->pa_node_lock.lg_lock = &lg->lg_prealloc_lock; 5389 pa->pa_inode = NULL; 5390 5391 list_add(&pa->pa_group_list, &grp->bb_prealloc_list); 5392 5393 /* 5394 * We will later add the new pa to the right bucket 5395 * after updating the pa_free in ext4_mb_release_context 5396 */ 5397 } 5398 5399 static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac) 5400 { 5401 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) 5402 ext4_mb_new_group_pa(ac); 5403 else 5404 ext4_mb_new_inode_pa(ac); 5405 } 5406 5407 /* 5408 * finds all unused blocks in on-disk bitmap, frees them in 5409 * in-core bitmap and buddy. 5410 * @pa must be unlinked from inode and group lists, so that 5411 * nobody else can find/use it. 5412 * the caller MUST hold group/inode locks. 5413 * TODO: optimize the case when there are no in-core structures yet 5414 */ 5415 static noinline_for_stack void 5416 ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh, 5417 struct ext4_prealloc_space *pa) 5418 { 5419 struct super_block *sb = e4b->bd_sb; 5420 struct ext4_sb_info *sbi = EXT4_SB(sb); 5421 unsigned int end; 5422 unsigned int next; 5423 ext4_group_t group; 5424 ext4_grpblk_t bit; 5425 unsigned long long grp_blk_start; 5426 int free = 0; 5427 5428 BUG_ON(pa->pa_deleted == 0); 5429 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); 5430 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit); 5431 BUG_ON(group != e4b->bd_group && pa->pa_len != 0); 5432 end = bit + pa->pa_len; 5433 5434 while (bit < end) { 5435 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit); 5436 if (bit >= end) 5437 break; 5438 next = mb_find_next_bit(bitmap_bh->b_data, end, bit); 5439 mb_debug(sb, "free preallocated %u/%u in group %u\n", 5440 (unsigned) ext4_group_first_block_no(sb, group) + bit, 5441 (unsigned) next - bit, (unsigned) group); 5442 free += next - bit; 5443 5444 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit); 5445 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start + 5446 EXT4_C2B(sbi, bit)), 5447 next - bit); 5448 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit); 5449 bit = next + 1; 5450 } 5451 if (free != pa->pa_free) { 5452 ext4_msg(e4b->bd_sb, KERN_CRIT, 5453 "pa %p: logic %lu, phys. %lu, len %d", 5454 pa, (unsigned long) pa->pa_lstart, 5455 (unsigned long) pa->pa_pstart, 5456 pa->pa_len); 5457 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u", 5458 free, pa->pa_free); 5459 /* 5460 * pa is already deleted so we use the value obtained 5461 * from the bitmap and continue. 5462 */ 5463 } 5464 atomic_add(free, &sbi->s_mb_discarded); 5465 } 5466 5467 static noinline_for_stack void 5468 ext4_mb_release_group_pa(struct ext4_buddy *e4b, 5469 struct ext4_prealloc_space *pa) 5470 { 5471 struct super_block *sb = e4b->bd_sb; 5472 ext4_group_t group; 5473 ext4_grpblk_t bit; 5474 5475 trace_ext4_mb_release_group_pa(sb, pa); 5476 BUG_ON(pa->pa_deleted == 0); 5477 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit); 5478 if (unlikely(group != e4b->bd_group && pa->pa_len != 0)) { 5479 ext4_warning(sb, "bad group: expected %u, group %u, pa_start %llu", 5480 e4b->bd_group, group, pa->pa_pstart); 5481 return; 5482 } 5483 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len); 5484 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded); 5485 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len); 5486 } 5487 5488 /* 5489 * releases all preallocations in given group 5490 * 5491 * first, we need to decide discard policy: 5492 * - when do we discard 5493 * 1) ENOSPC 5494 * - how many do we discard 5495 * 1) how many requested 5496 */ 5497 static noinline_for_stack int 5498 ext4_mb_discard_group_preallocations(struct super_block *sb, 5499 ext4_group_t group, int *busy) 5500 { 5501 struct ext4_group_info *grp = ext4_get_group_info(sb, group); 5502 struct buffer_head *bitmap_bh = NULL; 5503 struct ext4_prealloc_space *pa, *tmp; 5504 LIST_HEAD(list); 5505 struct ext4_buddy e4b; 5506 struct ext4_inode_info *ei; 5507 int err; 5508 int free = 0; 5509 5510 if (!grp) 5511 return 0; 5512 mb_debug(sb, "discard preallocation for group %u\n", group); 5513 if (list_empty(&grp->bb_prealloc_list)) 5514 goto out_dbg; 5515 5516 bitmap_bh = ext4_read_block_bitmap(sb, group); 5517 if (IS_ERR(bitmap_bh)) { 5518 err = PTR_ERR(bitmap_bh); 5519 ext4_error_err(sb, -err, 5520 "Error %d reading block bitmap for %u", 5521 err, group); 5522 goto out_dbg; 5523 } 5524 5525 err = ext4_mb_load_buddy(sb, group, &e4b); 5526 if (err) { 5527 ext4_warning(sb, "Error %d loading buddy information for %u", 5528 err, group); 5529 put_bh(bitmap_bh); 5530 goto out_dbg; 5531 } 5532 5533 ext4_lock_group(sb, group); 5534 list_for_each_entry_safe(pa, tmp, 5535 &grp->bb_prealloc_list, pa_group_list) { 5536 spin_lock(&pa->pa_lock); 5537 if (atomic_read(&pa->pa_count)) { 5538 spin_unlock(&pa->pa_lock); 5539 *busy = 1; 5540 continue; 5541 } 5542 if (pa->pa_deleted) { 5543 spin_unlock(&pa->pa_lock); 5544 continue; 5545 } 5546 5547 /* seems this one can be freed ... */ 5548 ext4_mb_mark_pa_deleted(sb, pa); 5549 5550 if (!free) 5551 this_cpu_inc(discard_pa_seq); 5552 5553 /* we can trust pa_free ... */ 5554 free += pa->pa_free; 5555 5556 spin_unlock(&pa->pa_lock); 5557 5558 list_del(&pa->pa_group_list); 5559 list_add(&pa->u.pa_tmp_list, &list); 5560 } 5561 5562 /* now free all selected PAs */ 5563 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { 5564 5565 /* remove from object (inode or locality group) */ 5566 if (pa->pa_type == MB_GROUP_PA) { 5567 spin_lock(pa->pa_node_lock.lg_lock); 5568 list_del_rcu(&pa->pa_node.lg_list); 5569 spin_unlock(pa->pa_node_lock.lg_lock); 5570 } else { 5571 write_lock(pa->pa_node_lock.inode_lock); 5572 ei = EXT4_I(pa->pa_inode); 5573 rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node); 5574 write_unlock(pa->pa_node_lock.inode_lock); 5575 } 5576 5577 list_del(&pa->u.pa_tmp_list); 5578 5579 if (pa->pa_type == MB_GROUP_PA) { 5580 ext4_mb_release_group_pa(&e4b, pa); 5581 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); 5582 } else { 5583 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); 5584 ext4_mb_pa_free(pa); 5585 } 5586 } 5587 5588 ext4_unlock_group(sb, group); 5589 ext4_mb_unload_buddy(&e4b); 5590 put_bh(bitmap_bh); 5591 out_dbg: 5592 mb_debug(sb, "discarded (%d) blocks preallocated for group %u bb_free (%d)\n", 5593 free, group, grp->bb_free); 5594 return free; 5595 } 5596 5597 /* 5598 * releases all non-used preallocated blocks for given inode 5599 * 5600 * It's important to discard preallocations under i_data_sem 5601 * We don't want another block to be served from the prealloc 5602 * space when we are discarding the inode prealloc space. 5603 * 5604 * FIXME!! Make sure it is valid at all the call sites 5605 */ 5606 void ext4_discard_preallocations(struct inode *inode) 5607 { 5608 struct ext4_inode_info *ei = EXT4_I(inode); 5609 struct super_block *sb = inode->i_sb; 5610 struct buffer_head *bitmap_bh = NULL; 5611 struct ext4_prealloc_space *pa, *tmp; 5612 ext4_group_t group = 0; 5613 LIST_HEAD(list); 5614 struct ext4_buddy e4b; 5615 struct rb_node *iter; 5616 int err; 5617 5618 if (!S_ISREG(inode->i_mode)) 5619 return; 5620 5621 if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) 5622 return; 5623 5624 mb_debug(sb, "discard preallocation for inode %llu\n", 5625 inode->i_ino); 5626 trace_ext4_discard_preallocations(inode, 5627 atomic_read(&ei->i_prealloc_active)); 5628 5629 repeat: 5630 /* first, collect all pa's in the inode */ 5631 write_lock(&ei->i_prealloc_lock); 5632 for (iter = rb_first(&ei->i_prealloc_node); iter; 5633 iter = rb_next(iter)) { 5634 pa = rb_entry(iter, struct ext4_prealloc_space, 5635 pa_node.inode_node); 5636 BUG_ON(pa->pa_node_lock.inode_lock != &ei->i_prealloc_lock); 5637 5638 spin_lock(&pa->pa_lock); 5639 if (atomic_read(&pa->pa_count)) { 5640 /* this shouldn't happen often - nobody should 5641 * use preallocation while we're discarding it */ 5642 spin_unlock(&pa->pa_lock); 5643 write_unlock(&ei->i_prealloc_lock); 5644 ext4_msg(sb, KERN_ERR, 5645 "uh-oh! used pa while discarding"); 5646 WARN_ON(1); 5647 schedule_timeout_uninterruptible(HZ); 5648 goto repeat; 5649 5650 } 5651 if (pa->pa_deleted == 0) { 5652 ext4_mb_mark_pa_deleted(sb, pa); 5653 spin_unlock(&pa->pa_lock); 5654 rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node); 5655 list_add(&pa->u.pa_tmp_list, &list); 5656 continue; 5657 } 5658 5659 /* someone is deleting pa right now */ 5660 spin_unlock(&pa->pa_lock); 5661 write_unlock(&ei->i_prealloc_lock); 5662 5663 /* we have to wait here because pa_deleted 5664 * doesn't mean pa is already unlinked from 5665 * the list. as we might be called from 5666 * ->clear_inode() the inode will get freed 5667 * and concurrent thread which is unlinking 5668 * pa from inode's list may access already 5669 * freed memory, bad-bad-bad */ 5670 5671 /* XXX: if this happens too often, we can 5672 * add a flag to force wait only in case 5673 * of ->clear_inode(), but not in case of 5674 * regular truncate */ 5675 schedule_timeout_uninterruptible(HZ); 5676 goto repeat; 5677 } 5678 write_unlock(&ei->i_prealloc_lock); 5679 5680 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) { 5681 BUG_ON(pa->pa_type != MB_INODE_PA); 5682 group = ext4_get_group_number(sb, pa->pa_pstart); 5683 5684 err = ext4_mb_load_buddy_gfp(sb, group, &e4b, 5685 GFP_NOFS|__GFP_NOFAIL); 5686 if (err) { 5687 ext4_error_err(sb, -err, "Error %d loading buddy information for %u", 5688 err, group); 5689 continue; 5690 } 5691 5692 bitmap_bh = ext4_read_block_bitmap(sb, group); 5693 if (IS_ERR(bitmap_bh)) { 5694 err = PTR_ERR(bitmap_bh); 5695 ext4_error_err(sb, -err, "Error %d reading block bitmap for %u", 5696 err, group); 5697 ext4_mb_unload_buddy(&e4b); 5698 continue; 5699 } 5700 5701 ext4_lock_group(sb, group); 5702 list_del(&pa->pa_group_list); 5703 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa); 5704 ext4_unlock_group(sb, group); 5705 5706 ext4_mb_unload_buddy(&e4b); 5707 put_bh(bitmap_bh); 5708 5709 list_del(&pa->u.pa_tmp_list); 5710 ext4_mb_pa_free(pa); 5711 } 5712 } 5713 5714 static int ext4_mb_pa_alloc(struct ext4_allocation_context *ac) 5715 { 5716 struct ext4_prealloc_space *pa; 5717 5718 BUG_ON(ext4_pspace_cachep == NULL); 5719 pa = kmem_cache_zalloc(ext4_pspace_cachep, GFP_NOFS); 5720 if (!pa) 5721 return -ENOMEM; 5722 atomic_set(&pa->pa_count, 1); 5723 ac->ac_pa = pa; 5724 return 0; 5725 } 5726 5727 static void ext4_mb_pa_put_free(struct ext4_allocation_context *ac) 5728 { 5729 struct ext4_prealloc_space *pa = ac->ac_pa; 5730 5731 BUG_ON(!pa); 5732 ac->ac_pa = NULL; 5733 WARN_ON(!atomic_dec_and_test(&pa->pa_count)); 5734 /* 5735 * current function is only called due to an error or due to 5736 * len of found blocks < len of requested blocks hence the PA has not 5737 * been added to grp->bb_prealloc_list. So we don't need to lock it 5738 */ 5739 pa->pa_deleted = 1; 5740 ext4_mb_pa_free(pa); 5741 } 5742 5743 #ifdef CONFIG_EXT4_DEBUG 5744 static inline void ext4_mb_show_pa(struct super_block *sb) 5745 { 5746 ext4_group_t i, ngroups; 5747 5748 if (ext4_emergency_state(sb)) 5749 return; 5750 5751 ngroups = ext4_get_groups_count(sb); 5752 mb_debug(sb, "groups: "); 5753 for (i = 0; i < ngroups; i++) { 5754 struct ext4_group_info *grp = ext4_get_group_info(sb, i); 5755 struct ext4_prealloc_space *pa; 5756 ext4_grpblk_t start; 5757 struct list_head *cur; 5758 5759 if (!grp) 5760 continue; 5761 ext4_lock_group(sb, i); 5762 list_for_each(cur, &grp->bb_prealloc_list) { 5763 pa = list_entry(cur, struct ext4_prealloc_space, 5764 pa_group_list); 5765 spin_lock(&pa->pa_lock); 5766 ext4_get_group_no_and_offset(sb, pa->pa_pstart, 5767 NULL, &start); 5768 spin_unlock(&pa->pa_lock); 5769 mb_debug(sb, "PA:%u:%d:%d\n", i, start, 5770 pa->pa_len); 5771 } 5772 ext4_unlock_group(sb, i); 5773 mb_debug(sb, "%u: %d/%d\n", i, grp->bb_free, 5774 grp->bb_fragments); 5775 } 5776 } 5777 5778 static void ext4_mb_show_ac(struct ext4_allocation_context *ac) 5779 { 5780 struct super_block *sb = ac->ac_sb; 5781 5782 if (ext4_emergency_state(sb)) 5783 return; 5784 5785 mb_debug(sb, "Can't allocate:" 5786 " Allocation context details:"); 5787 mb_debug(sb, "status %u flags 0x%x", 5788 ac->ac_status, ac->ac_flags); 5789 mb_debug(sb, "orig %lu/%lu/%lu@%lu, " 5790 "goal %lu/%lu/%lu@%lu, " 5791 "best %lu/%lu/%lu@%lu cr %d", 5792 (unsigned long)ac->ac_o_ex.fe_group, 5793 (unsigned long)ac->ac_o_ex.fe_start, 5794 (unsigned long)ac->ac_o_ex.fe_len, 5795 (unsigned long)ac->ac_o_ex.fe_logical, 5796 (unsigned long)ac->ac_g_ex.fe_group, 5797 (unsigned long)ac->ac_g_ex.fe_start, 5798 (unsigned long)ac->ac_g_ex.fe_len, 5799 (unsigned long)ac->ac_g_ex.fe_logical, 5800 (unsigned long)ac->ac_b_ex.fe_group, 5801 (unsigned long)ac->ac_b_ex.fe_start, 5802 (unsigned long)ac->ac_b_ex.fe_len, 5803 (unsigned long)ac->ac_b_ex.fe_logical, 5804 (int)ac->ac_criteria); 5805 mb_debug(sb, "%u found", ac->ac_found); 5806 mb_debug(sb, "used pa: %s, ", str_yes_no(ac->ac_pa)); 5807 if (ac->ac_pa) 5808 mb_debug(sb, "pa_type %s\n", ac->ac_pa->pa_type == MB_GROUP_PA ? 5809 "group pa" : "inode pa"); 5810 ext4_mb_show_pa(sb); 5811 } 5812 #else 5813 static inline void ext4_mb_show_pa(struct super_block *sb) 5814 { 5815 } 5816 static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac) 5817 { 5818 ext4_mb_show_pa(ac->ac_sb); 5819 } 5820 #endif 5821 5822 /* 5823 * We use locality group preallocation for small size file. The size of the 5824 * file is determined by the current size or the resulting size after 5825 * allocation which ever is larger 5826 * 5827 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req 5828 */ 5829 static void ext4_mb_group_or_file(struct ext4_allocation_context *ac) 5830 { 5831 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 5832 int bsbits = ac->ac_sb->s_blocksize_bits; 5833 loff_t size, isize; 5834 bool inode_pa_eligible, group_pa_eligible; 5835 5836 if (!(ac->ac_flags & EXT4_MB_HINT_DATA)) 5837 return; 5838 5839 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)) 5840 return; 5841 5842 group_pa_eligible = sbi->s_mb_group_prealloc > 0; 5843 inode_pa_eligible = true; 5844 size = extent_logical_end(sbi, &ac->ac_o_ex); 5845 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1) 5846 >> bsbits; 5847 5848 /* No point in using inode preallocation for closed files */ 5849 if ((size == isize) && !ext4_fs_is_busy(sbi) && 5850 !inode_is_open_for_write(ac->ac_inode)) 5851 inode_pa_eligible = false; 5852 5853 size = max(size, isize); 5854 /* Don't use group allocation for large files */ 5855 if (size > sbi->s_mb_stream_request) 5856 group_pa_eligible = false; 5857 5858 if (!group_pa_eligible) { 5859 if (inode_pa_eligible) 5860 ac->ac_flags |= EXT4_MB_STREAM_ALLOC; 5861 else 5862 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC; 5863 return; 5864 } 5865 5866 BUG_ON(ac->ac_lg != NULL); 5867 /* 5868 * locality group prealloc space are per cpu. The reason for having 5869 * per cpu locality group is to reduce the contention between block 5870 * request from multiple CPUs. 5871 */ 5872 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups); 5873 5874 /* we're going to use group allocation */ 5875 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC; 5876 5877 /* serialize all allocations in the group */ 5878 mutex_lock(&ac->ac_lg->lg_mutex); 5879 } 5880 5881 static noinline_for_stack void 5882 ext4_mb_initialize_context(struct ext4_allocation_context *ac, 5883 struct ext4_allocation_request *ar) 5884 { 5885 struct super_block *sb = ar->inode->i_sb; 5886 struct ext4_sb_info *sbi = EXT4_SB(sb); 5887 struct ext4_super_block *es = sbi->s_es; 5888 ext4_group_t group; 5889 unsigned int len; 5890 ext4_fsblk_t goal; 5891 ext4_grpblk_t block; 5892 5893 /* we can't allocate > group size */ 5894 len = ar->len; 5895 5896 /* just a dirty hack to filter too big requests */ 5897 if (len >= EXT4_CLUSTERS_PER_GROUP(sb)) 5898 len = EXT4_CLUSTERS_PER_GROUP(sb); 5899 5900 /* start searching from the goal */ 5901 goal = ar->goal; 5902 if (goal < le32_to_cpu(es->s_first_data_block) || 5903 goal >= ext4_blocks_count(es)) 5904 goal = le32_to_cpu(es->s_first_data_block); 5905 ext4_get_group_no_and_offset(sb, goal, &group, &block); 5906 5907 /* set up allocation goals */ 5908 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical); 5909 ac->ac_status = AC_STATUS_CONTINUE; 5910 ac->ac_sb = sb; 5911 ac->ac_inode = ar->inode; 5912 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical; 5913 ac->ac_o_ex.fe_group = group; 5914 ac->ac_o_ex.fe_start = block; 5915 ac->ac_o_ex.fe_len = len; 5916 ac->ac_g_ex = ac->ac_o_ex; 5917 ac->ac_orig_goal_len = ac->ac_g_ex.fe_len; 5918 ac->ac_flags = ar->flags; 5919 5920 /* we have to define context: we'll work with a file or 5921 * locality group. this is a policy, actually */ 5922 ext4_mb_group_or_file(ac); 5923 5924 mb_debug(sb, "init ac: %u blocks @ %u, goal %u, flags 0x%x, 2^%d, " 5925 "left: %u/%u, right %u/%u to %swritable\n", 5926 (unsigned) ar->len, (unsigned) ar->logical, 5927 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order, 5928 (unsigned) ar->lleft, (unsigned) ar->pleft, 5929 (unsigned) ar->lright, (unsigned) ar->pright, 5930 inode_is_open_for_write(ar->inode) ? "" : "non-"); 5931 } 5932 5933 static noinline_for_stack void 5934 ext4_mb_discard_lg_preallocations(struct super_block *sb, 5935 struct ext4_locality_group *lg, 5936 int order, int total_entries) 5937 { 5938 ext4_group_t group = 0; 5939 struct ext4_buddy e4b; 5940 LIST_HEAD(discard_list); 5941 struct ext4_prealloc_space *pa, *tmp; 5942 5943 mb_debug(sb, "discard locality group preallocation\n"); 5944 5945 spin_lock(&lg->lg_prealloc_lock); 5946 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order], 5947 pa_node.lg_list, 5948 lockdep_is_held(&lg->lg_prealloc_lock)) { 5949 spin_lock(&pa->pa_lock); 5950 if (atomic_read(&pa->pa_count)) { 5951 /* 5952 * This is the pa that we just used 5953 * for block allocation. So don't 5954 * free that 5955 */ 5956 spin_unlock(&pa->pa_lock); 5957 continue; 5958 } 5959 if (pa->pa_deleted) { 5960 spin_unlock(&pa->pa_lock); 5961 continue; 5962 } 5963 /* only lg prealloc space */ 5964 BUG_ON(pa->pa_type != MB_GROUP_PA); 5965 5966 /* seems this one can be freed ... */ 5967 ext4_mb_mark_pa_deleted(sb, pa); 5968 spin_unlock(&pa->pa_lock); 5969 5970 list_del_rcu(&pa->pa_node.lg_list); 5971 list_add(&pa->u.pa_tmp_list, &discard_list); 5972 5973 total_entries--; 5974 if (total_entries <= 5) { 5975 /* 5976 * we want to keep only 5 entries 5977 * allowing it to grow to 8. This 5978 * mak sure we don't call discard 5979 * soon for this list. 5980 */ 5981 break; 5982 } 5983 } 5984 spin_unlock(&lg->lg_prealloc_lock); 5985 5986 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) { 5987 int err; 5988 5989 group = ext4_get_group_number(sb, pa->pa_pstart); 5990 err = ext4_mb_load_buddy_gfp(sb, group, &e4b, 5991 GFP_NOFS|__GFP_NOFAIL); 5992 if (err) { 5993 ext4_error_err(sb, -err, "Error %d loading buddy information for %u", 5994 err, group); 5995 continue; 5996 } 5997 ext4_lock_group(sb, group); 5998 list_del(&pa->pa_group_list); 5999 ext4_mb_release_group_pa(&e4b, pa); 6000 ext4_unlock_group(sb, group); 6001 6002 ext4_mb_unload_buddy(&e4b); 6003 list_del(&pa->u.pa_tmp_list); 6004 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback); 6005 } 6006 } 6007 6008 /* 6009 * We have incremented pa_count. So it cannot be freed at this 6010 * point. Also we hold lg_mutex. So no parallel allocation is 6011 * possible from this lg. That means pa_free cannot be updated. 6012 * 6013 * A parallel ext4_mb_discard_group_preallocations is possible. 6014 * which can cause the lg_prealloc_list to be updated. 6015 */ 6016 6017 static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac) 6018 { 6019 int order, added = 0, lg_prealloc_count = 1; 6020 struct super_block *sb = ac->ac_sb; 6021 struct ext4_locality_group *lg = ac->ac_lg; 6022 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa; 6023 6024 order = fls(pa->pa_free) - 1; 6025 if (order > PREALLOC_TB_SIZE - 1) 6026 /* The max size of hash table is PREALLOC_TB_SIZE */ 6027 order = PREALLOC_TB_SIZE - 1; 6028 /* Add the prealloc space to lg */ 6029 spin_lock(&lg->lg_prealloc_lock); 6030 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order], 6031 pa_node.lg_list, 6032 lockdep_is_held(&lg->lg_prealloc_lock)) { 6033 spin_lock(&tmp_pa->pa_lock); 6034 if (tmp_pa->pa_deleted) { 6035 spin_unlock(&tmp_pa->pa_lock); 6036 continue; 6037 } 6038 if (!added && pa->pa_free < tmp_pa->pa_free) { 6039 /* Add to the tail of the previous entry */ 6040 list_add_tail_rcu(&pa->pa_node.lg_list, 6041 &tmp_pa->pa_node.lg_list); 6042 added = 1; 6043 /* 6044 * we want to count the total 6045 * number of entries in the list 6046 */ 6047 } 6048 spin_unlock(&tmp_pa->pa_lock); 6049 lg_prealloc_count++; 6050 } 6051 if (!added) 6052 list_add_tail_rcu(&pa->pa_node.lg_list, 6053 &lg->lg_prealloc_list[order]); 6054 spin_unlock(&lg->lg_prealloc_lock); 6055 6056 /* Now trim the list to be not more than 8 elements */ 6057 if (lg_prealloc_count > 8) 6058 ext4_mb_discard_lg_preallocations(sb, lg, 6059 order, lg_prealloc_count); 6060 } 6061 6062 /* 6063 * release all resource we used in allocation 6064 */ 6065 static void ext4_mb_release_context(struct ext4_allocation_context *ac) 6066 { 6067 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb); 6068 struct ext4_prealloc_space *pa = ac->ac_pa; 6069 if (pa) { 6070 if (pa->pa_type == MB_GROUP_PA) { 6071 /* see comment in ext4_mb_use_group_pa() */ 6072 spin_lock(&pa->pa_lock); 6073 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len); 6074 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len); 6075 pa->pa_free -= ac->ac_b_ex.fe_len; 6076 pa->pa_len -= ac->ac_b_ex.fe_len; 6077 spin_unlock(&pa->pa_lock); 6078 6079 /* 6080 * We want to add the pa to the right bucket. 6081 * Remove it from the list and while adding 6082 * make sure the list to which we are adding 6083 * doesn't grow big. 6084 */ 6085 if (likely(pa->pa_free)) { 6086 spin_lock(pa->pa_node_lock.lg_lock); 6087 list_del_rcu(&pa->pa_node.lg_list); 6088 spin_unlock(pa->pa_node_lock.lg_lock); 6089 ext4_mb_add_n_trim(ac); 6090 } 6091 } 6092 6093 ext4_mb_put_pa(ac, ac->ac_sb, pa); 6094 } 6095 if (ac->ac_bitmap_folio) 6096 folio_put(ac->ac_bitmap_folio); 6097 if (ac->ac_buddy_folio) 6098 folio_put(ac->ac_buddy_folio); 6099 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) 6100 mutex_unlock(&ac->ac_lg->lg_mutex); 6101 ext4_mb_collect_stats(ac); 6102 } 6103 6104 static int ext4_mb_discard_preallocations(struct super_block *sb, int needed) 6105 { 6106 ext4_group_t i, ngroups = ext4_get_groups_count(sb); 6107 int ret; 6108 int freed = 0, busy = 0; 6109 int retry = 0; 6110 6111 trace_ext4_mb_discard_preallocations(sb, needed); 6112 6113 if (needed == 0) 6114 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1; 6115 repeat: 6116 for (i = 0; i < ngroups && needed > 0; i++) { 6117 ret = ext4_mb_discard_group_preallocations(sb, i, &busy); 6118 freed += ret; 6119 needed -= ret; 6120 cond_resched(); 6121 } 6122 6123 if (needed > 0 && busy && ++retry < 3) { 6124 busy = 0; 6125 goto repeat; 6126 } 6127 6128 return freed; 6129 } 6130 6131 static bool ext4_mb_discard_preallocations_should_retry(struct super_block *sb, 6132 struct ext4_allocation_context *ac, u64 *seq) 6133 { 6134 int freed; 6135 u64 seq_retry = 0; 6136 bool ret = false; 6137 6138 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len); 6139 if (freed) { 6140 ret = true; 6141 goto out_dbg; 6142 } 6143 seq_retry = ext4_get_discard_pa_seq_sum(); 6144 if (!(ac->ac_flags & EXT4_MB_STRICT_CHECK) || seq_retry != *seq) { 6145 ac->ac_flags |= EXT4_MB_STRICT_CHECK; 6146 *seq = seq_retry; 6147 ret = true; 6148 } 6149 6150 out_dbg: 6151 mb_debug(sb, "freed %d, retry ? %s\n", freed, str_yes_no(ret)); 6152 return ret; 6153 } 6154 6155 /* 6156 * Simple allocator for Ext4 fast commit replay path. It searches for blocks 6157 * linearly starting at the goal block and also excludes the blocks which 6158 * are going to be in use after fast commit replay. 6159 */ 6160 static ext4_fsblk_t 6161 ext4_mb_new_blocks_simple(struct ext4_allocation_request *ar, int *errp) 6162 { 6163 struct buffer_head *bitmap_bh; 6164 struct super_block *sb = ar->inode->i_sb; 6165 struct ext4_sb_info *sbi = EXT4_SB(sb); 6166 ext4_group_t group, nr; 6167 ext4_grpblk_t blkoff; 6168 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb); 6169 ext4_grpblk_t i = 0; 6170 ext4_fsblk_t goal, block; 6171 struct ext4_super_block *es = sbi->s_es; 6172 6173 goal = ar->goal; 6174 if (goal < le32_to_cpu(es->s_first_data_block) || 6175 goal >= ext4_blocks_count(es)) 6176 goal = le32_to_cpu(es->s_first_data_block); 6177 6178 ar->len = 0; 6179 ext4_get_group_no_and_offset(sb, goal, &group, &blkoff); 6180 for (nr = ext4_get_groups_count(sb); nr > 0; nr--) { 6181 bitmap_bh = ext4_read_block_bitmap(sb, group); 6182 if (IS_ERR(bitmap_bh)) { 6183 *errp = PTR_ERR(bitmap_bh); 6184 pr_warn("Failed to read block bitmap\n"); 6185 return 0; 6186 } 6187 6188 while (1) { 6189 i = mb_find_next_zero_bit(bitmap_bh->b_data, max, 6190 blkoff); 6191 if (i >= max) 6192 break; 6193 if (ext4_fc_replay_check_excluded(sb, 6194 ext4_group_first_block_no(sb, group) + 6195 EXT4_C2B(sbi, i))) { 6196 blkoff = i + 1; 6197 } else 6198 break; 6199 } 6200 brelse(bitmap_bh); 6201 if (i < max) 6202 break; 6203 6204 if (++group >= ext4_get_groups_count(sb)) 6205 group = 0; 6206 6207 blkoff = 0; 6208 } 6209 6210 if (i >= max) { 6211 *errp = -ENOSPC; 6212 return 0; 6213 } 6214 6215 block = ext4_group_first_block_no(sb, group) + EXT4_C2B(sbi, i); 6216 ext4_mb_mark_bb(sb, block, 1, true); 6217 ar->len = 1; 6218 6219 *errp = 0; 6220 return block; 6221 } 6222 6223 /* 6224 * Main entry point into mballoc to allocate blocks 6225 * it tries to use preallocation first, then falls back 6226 * to usual allocation 6227 */ 6228 ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle, 6229 struct ext4_allocation_request *ar, int *errp) 6230 { 6231 struct ext4_allocation_context *ac = NULL; 6232 struct ext4_sb_info *sbi; 6233 struct super_block *sb; 6234 ext4_fsblk_t block = 0; 6235 unsigned int inquota = 0; 6236 unsigned int reserv_clstrs = 0; 6237 int retries = 0; 6238 u64 seq; 6239 6240 might_sleep(); 6241 sb = ar->inode->i_sb; 6242 sbi = EXT4_SB(sb); 6243 6244 trace_ext4_request_blocks(ar); 6245 if (sbi->s_mount_state & EXT4_FC_REPLAY) 6246 return ext4_mb_new_blocks_simple(ar, errp); 6247 6248 /* Allow to use superuser reservation for quota file */ 6249 if (ext4_is_quota_file(ar->inode)) 6250 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS; 6251 6252 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) { 6253 /* Without delayed allocation we need to verify 6254 * there is enough free blocks to do block allocation 6255 * and verify allocation doesn't exceed the quota limits. 6256 */ 6257 while (ar->len && 6258 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) { 6259 6260 /* let others to free the space */ 6261 cond_resched(); 6262 ar->len = ar->len >> 1; 6263 } 6264 if (!ar->len) { 6265 ext4_mb_show_pa(sb); 6266 *errp = -ENOSPC; 6267 return 0; 6268 } 6269 reserv_clstrs = ar->len; 6270 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) { 6271 dquot_alloc_block_nofail(ar->inode, 6272 EXT4_C2B(sbi, ar->len)); 6273 } else { 6274 while (ar->len && 6275 dquot_alloc_block(ar->inode, 6276 EXT4_C2B(sbi, ar->len))) { 6277 6278 ar->flags |= EXT4_MB_HINT_NOPREALLOC; 6279 ar->len--; 6280 } 6281 } 6282 inquota = ar->len; 6283 if (ar->len == 0) { 6284 *errp = -EDQUOT; 6285 goto out; 6286 } 6287 } 6288 6289 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS); 6290 if (!ac) { 6291 ar->len = 0; 6292 *errp = -ENOMEM; 6293 goto out; 6294 } 6295 6296 ext4_mb_initialize_context(ac, ar); 6297 6298 ac->ac_op = EXT4_MB_HISTORY_PREALLOC; 6299 seq = this_cpu_read(discard_pa_seq); 6300 if (!ext4_mb_use_preallocated(ac)) { 6301 ac->ac_op = EXT4_MB_HISTORY_ALLOC; 6302 ext4_mb_normalize_request(ac, ar); 6303 6304 *errp = ext4_mb_pa_alloc(ac); 6305 if (*errp) 6306 goto errout; 6307 repeat: 6308 /* allocate space in core */ 6309 *errp = ext4_mb_regular_allocator(ac); 6310 /* 6311 * pa allocated above is added to grp->bb_prealloc_list only 6312 * when we were able to allocate some block i.e. when 6313 * ac->ac_status == AC_STATUS_FOUND. 6314 * And error from above mean ac->ac_status != AC_STATUS_FOUND 6315 * So we have to free this pa here itself. 6316 */ 6317 if (*errp) { 6318 ext4_mb_pa_put_free(ac); 6319 ext4_discard_allocated_blocks(ac); 6320 goto errout; 6321 } 6322 if (ac->ac_status == AC_STATUS_FOUND && 6323 ac->ac_o_ex.fe_len >= ac->ac_f_ex.fe_len) 6324 ext4_mb_pa_put_free(ac); 6325 } 6326 if (likely(ac->ac_status == AC_STATUS_FOUND)) { 6327 *errp = ext4_mb_mark_diskspace_used(ac, handle); 6328 if (*errp) { 6329 ext4_discard_allocated_blocks(ac); 6330 goto errout; 6331 } else { 6332 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex); 6333 ar->len = ac->ac_b_ex.fe_len; 6334 } 6335 } else { 6336 if (++retries < 3 && 6337 ext4_mb_discard_preallocations_should_retry(sb, ac, &seq)) 6338 goto repeat; 6339 /* 6340 * If block allocation fails then the pa allocated above 6341 * needs to be freed here itself. 6342 */ 6343 ext4_mb_pa_put_free(ac); 6344 *errp = -ENOSPC; 6345 } 6346 6347 if (*errp) { 6348 errout: 6349 ac->ac_b_ex.fe_len = 0; 6350 ar->len = 0; 6351 ext4_mb_show_ac(ac); 6352 } 6353 ext4_mb_release_context(ac); 6354 kmem_cache_free(ext4_ac_cachep, ac); 6355 out: 6356 if (inquota && ar->len < inquota) 6357 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len)); 6358 /* release any reserved blocks */ 6359 if (reserv_clstrs) 6360 percpu_counter_sub(&sbi->s_dirtyclusters_counter, reserv_clstrs); 6361 6362 trace_ext4_allocate_blocks(ar, (unsigned long long)block); 6363 6364 return block; 6365 } 6366 6367 /* 6368 * We can merge two free data extents only if the physical blocks 6369 * are contiguous, AND the extents were freed by the same transaction, 6370 * AND the blocks are associated with the same group. 6371 */ 6372 static inline bool 6373 ext4_freed_extents_can_be_merged(struct ext4_free_data *entry1, 6374 struct ext4_free_data *entry2) 6375 { 6376 if (entry1->efd_tid != entry2->efd_tid) 6377 return false; 6378 if (entry1->efd_start_cluster + entry1->efd_count != 6379 entry2->efd_start_cluster) 6380 return false; 6381 if (WARN_ON_ONCE(entry1->efd_group != entry2->efd_group)) 6382 return false; 6383 return true; 6384 } 6385 6386 static inline void 6387 ext4_merge_freed_extents(struct ext4_sb_info *sbi, struct rb_root *root, 6388 struct ext4_free_data *entry1, 6389 struct ext4_free_data *entry2) 6390 { 6391 entry1->efd_count += entry2->efd_count; 6392 spin_lock(&sbi->s_md_lock); 6393 list_del(&entry2->efd_list); 6394 spin_unlock(&sbi->s_md_lock); 6395 rb_erase(&entry2->efd_node, root); 6396 kmem_cache_free(ext4_free_data_cachep, entry2); 6397 } 6398 6399 static inline void 6400 ext4_try_merge_freed_extent_prev(struct ext4_sb_info *sbi, struct rb_root *root, 6401 struct ext4_free_data *entry) 6402 { 6403 struct ext4_free_data *prev; 6404 struct rb_node *node; 6405 6406 node = rb_prev(&entry->efd_node); 6407 if (!node) 6408 return; 6409 6410 prev = rb_entry(node, struct ext4_free_data, efd_node); 6411 if (ext4_freed_extents_can_be_merged(prev, entry)) 6412 ext4_merge_freed_extents(sbi, root, prev, entry); 6413 } 6414 6415 static inline void 6416 ext4_try_merge_freed_extent_next(struct ext4_sb_info *sbi, struct rb_root *root, 6417 struct ext4_free_data *entry) 6418 { 6419 struct ext4_free_data *next; 6420 struct rb_node *node; 6421 6422 node = rb_next(&entry->efd_node); 6423 if (!node) 6424 return; 6425 6426 next = rb_entry(node, struct ext4_free_data, efd_node); 6427 if (ext4_freed_extents_can_be_merged(entry, next)) 6428 ext4_merge_freed_extents(sbi, root, entry, next); 6429 } 6430 6431 static noinline_for_stack void 6432 ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b, 6433 struct ext4_free_data *new_entry) 6434 { 6435 ext4_group_t group = e4b->bd_group; 6436 ext4_grpblk_t cluster; 6437 ext4_grpblk_t clusters = new_entry->efd_count; 6438 struct ext4_free_data *entry = NULL; 6439 struct ext4_group_info *db = e4b->bd_info; 6440 struct super_block *sb = e4b->bd_sb; 6441 struct ext4_sb_info *sbi = EXT4_SB(sb); 6442 struct rb_root *root = &db->bb_free_root; 6443 struct rb_node **n = &root->rb_node; 6444 struct rb_node *parent = NULL, *new_node; 6445 6446 BUG_ON(!ext4_handle_valid(handle)); 6447 BUG_ON(e4b->bd_bitmap_folio == NULL); 6448 BUG_ON(e4b->bd_buddy_folio == NULL); 6449 6450 new_node = &new_entry->efd_node; 6451 cluster = new_entry->efd_start_cluster; 6452 6453 if (!*n) { 6454 /* first free block exent. We need to 6455 protect buddy cache from being freed, 6456 * otherwise we'll refresh it from 6457 * on-disk bitmap and lose not-yet-available 6458 * blocks */ 6459 folio_get(e4b->bd_buddy_folio); 6460 folio_get(e4b->bd_bitmap_folio); 6461 } 6462 while (*n) { 6463 parent = *n; 6464 entry = rb_entry(parent, struct ext4_free_data, efd_node); 6465 if (cluster < entry->efd_start_cluster) 6466 n = &(*n)->rb_left; 6467 else if (cluster >= (entry->efd_start_cluster + entry->efd_count)) 6468 n = &(*n)->rb_right; 6469 else { 6470 ext4_grp_locked_error(sb, group, 0, 6471 ext4_group_first_block_no(sb, group) + 6472 EXT4_C2B(sbi, cluster), 6473 "Block already on to-be-freed list"); 6474 kmem_cache_free(ext4_free_data_cachep, new_entry); 6475 return; 6476 } 6477 } 6478 6479 atomic_add(clusters, &sbi->s_mb_free_pending); 6480 if (!entry) 6481 goto insert; 6482 6483 /* Now try to see the extent can be merged to prev and next */ 6484 if (ext4_freed_extents_can_be_merged(new_entry, entry)) { 6485 entry->efd_start_cluster = cluster; 6486 entry->efd_count += new_entry->efd_count; 6487 kmem_cache_free(ext4_free_data_cachep, new_entry); 6488 ext4_try_merge_freed_extent_prev(sbi, root, entry); 6489 return; 6490 } 6491 if (ext4_freed_extents_can_be_merged(entry, new_entry)) { 6492 entry->efd_count += new_entry->efd_count; 6493 kmem_cache_free(ext4_free_data_cachep, new_entry); 6494 ext4_try_merge_freed_extent_next(sbi, root, entry); 6495 return; 6496 } 6497 insert: 6498 rb_link_node(new_node, parent, n); 6499 rb_insert_color(new_node, root); 6500 6501 spin_lock(&sbi->s_md_lock); 6502 list_add_tail(&new_entry->efd_list, &sbi->s_freed_data_list[new_entry->efd_tid & 1]); 6503 spin_unlock(&sbi->s_md_lock); 6504 } 6505 6506 static void ext4_free_blocks_simple(struct inode *inode, ext4_fsblk_t block, 6507 unsigned long count) 6508 { 6509 struct super_block *sb = inode->i_sb; 6510 ext4_group_t group; 6511 ext4_grpblk_t blkoff; 6512 6513 ext4_get_group_no_and_offset(sb, block, &group, &blkoff); 6514 ext4_mb_mark_context(NULL, sb, false, group, blkoff, count, 6515 EXT4_MB_BITMAP_MARKED_CHECK | 6516 EXT4_MB_SYNC_UPDATE, 6517 NULL); 6518 } 6519 6520 /** 6521 * ext4_mb_clear_bb() -- helper function for freeing blocks. 6522 * Used by ext4_free_blocks() 6523 * @handle: handle for this transaction 6524 * @inode: inode 6525 * @block: starting physical block to be freed 6526 * @count: number of blocks to be freed 6527 * @flags: flags used by ext4_free_blocks 6528 */ 6529 static void ext4_mb_clear_bb(handle_t *handle, struct inode *inode, 6530 ext4_fsblk_t block, unsigned long count, 6531 int flags) 6532 { 6533 struct super_block *sb = inode->i_sb; 6534 struct ext4_group_info *grp; 6535 unsigned int overflow; 6536 ext4_grpblk_t bit; 6537 ext4_group_t block_group; 6538 struct ext4_sb_info *sbi; 6539 struct ext4_buddy e4b; 6540 unsigned int count_clusters; 6541 int err = 0; 6542 int mark_flags = 0; 6543 ext4_grpblk_t changed; 6544 6545 sbi = EXT4_SB(sb); 6546 6547 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) && 6548 !ext4_inode_block_valid(inode, block, count)) { 6549 ext4_error(sb, "Freeing blocks in system zone - " 6550 "Block = %llu, count = %lu", block, count); 6551 /* err = 0. ext4_std_error should be a no op */ 6552 goto error_out; 6553 } 6554 flags |= EXT4_FREE_BLOCKS_VALIDATED; 6555 6556 do_more: 6557 overflow = 0; 6558 ext4_get_group_no_and_offset(sb, block, &block_group, &bit); 6559 6560 grp = ext4_get_group_info(sb, block_group); 6561 if (unlikely(!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp))) 6562 return; 6563 6564 /* 6565 * Check to see if we are freeing blocks across a group 6566 * boundary. 6567 */ 6568 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) { 6569 overflow = EXT4_C2B(sbi, bit) + count - 6570 EXT4_BLOCKS_PER_GROUP(sb); 6571 count -= overflow; 6572 /* The range changed so it's no longer validated */ 6573 flags &= ~EXT4_FREE_BLOCKS_VALIDATED; 6574 } 6575 count_clusters = EXT4_NUM_B2C(sbi, count); 6576 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters); 6577 6578 /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */ 6579 err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b, 6580 GFP_NOFS|__GFP_NOFAIL); 6581 if (err) 6582 goto error_out; 6583 6584 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) && 6585 !ext4_inode_block_valid(inode, block, count)) { 6586 ext4_error(sb, "Freeing blocks in system zone - " 6587 "Block = %llu, count = %lu", block, count); 6588 /* err = 0. ext4_std_error should be a no op */ 6589 goto error_clean; 6590 } 6591 6592 #ifdef AGGRESSIVE_CHECK 6593 mark_flags |= EXT4_MB_BITMAP_MARKED_CHECK; 6594 #endif 6595 err = ext4_mb_mark_context(handle, sb, false, block_group, bit, 6596 count_clusters, mark_flags, &changed); 6597 6598 6599 if (err && changed == 0) 6600 goto error_clean; 6601 6602 #ifdef AGGRESSIVE_CHECK 6603 BUG_ON(changed != count_clusters); 6604 #endif 6605 6606 /* 6607 * We need to make sure we don't reuse the freed block until after the 6608 * transaction is committed. We make an exception if the inode is to be 6609 * written in writeback mode since writeback mode has weak data 6610 * consistency guarantees. 6611 */ 6612 if (ext4_handle_valid(handle) && 6613 ((flags & EXT4_FREE_BLOCKS_METADATA) || 6614 !ext4_should_writeback_data(inode))) { 6615 struct ext4_free_data *new_entry; 6616 /* 6617 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed 6618 * to fail. 6619 */ 6620 new_entry = kmem_cache_alloc(ext4_free_data_cachep, 6621 GFP_NOFS|__GFP_NOFAIL); 6622 new_entry->efd_start_cluster = bit; 6623 new_entry->efd_group = block_group; 6624 new_entry->efd_count = count_clusters; 6625 new_entry->efd_tid = handle->h_transaction->t_tid; 6626 6627 ext4_lock_group(sb, block_group); 6628 ext4_mb_free_metadata(handle, &e4b, new_entry); 6629 } else { 6630 if (test_opt(sb, DISCARD)) { 6631 err = ext4_issue_discard(sb, block_group, bit, 6632 count_clusters); 6633 /* 6634 * Ignore EOPNOTSUPP error. This is consistent with 6635 * what happens when using journal. 6636 */ 6637 if (err == -EOPNOTSUPP) 6638 err = 0; 6639 if (err) 6640 ext4_msg(sb, KERN_WARNING, "discard request in" 6641 " group:%u block:%d count:%lu failed" 6642 " with %d", block_group, bit, count, 6643 err); 6644 } 6645 6646 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info); 6647 6648 ext4_lock_group(sb, block_group); 6649 mb_free_blocks(inode, &e4b, bit, count_clusters); 6650 } 6651 6652 ext4_unlock_group(sb, block_group); 6653 6654 /* 6655 * on a bigalloc file system, defer the s_freeclusters_counter 6656 * update to the caller (ext4_remove_space and friends) so they 6657 * can determine if a cluster freed here should be rereserved 6658 */ 6659 if (!(flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)) { 6660 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE)) 6661 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters)); 6662 percpu_counter_add(&sbi->s_freeclusters_counter, 6663 count_clusters); 6664 } 6665 6666 if (overflow && !err) { 6667 block += count; 6668 count = overflow; 6669 ext4_mb_unload_buddy(&e4b); 6670 /* The range changed so it's no longer validated */ 6671 flags &= ~EXT4_FREE_BLOCKS_VALIDATED; 6672 goto do_more; 6673 } 6674 6675 error_clean: 6676 ext4_mb_unload_buddy(&e4b); 6677 error_out: 6678 ext4_std_error(sb, err); 6679 } 6680 6681 /** 6682 * ext4_free_blocks() -- Free given blocks and update quota 6683 * @handle: handle for this transaction 6684 * @inode: inode 6685 * @bh: optional buffer of the block to be freed 6686 * @block: starting physical block to be freed 6687 * @count: number of blocks to be freed 6688 * @flags: flags used by ext4_free_blocks 6689 */ 6690 void ext4_free_blocks(handle_t *handle, struct inode *inode, 6691 struct buffer_head *bh, ext4_fsblk_t block, 6692 unsigned long count, int flags) 6693 { 6694 struct super_block *sb = inode->i_sb; 6695 unsigned int overflow; 6696 struct ext4_sb_info *sbi; 6697 6698 sbi = EXT4_SB(sb); 6699 6700 if (bh) { 6701 if (block) 6702 BUG_ON(block != bh->b_blocknr); 6703 else 6704 block = bh->b_blocknr; 6705 } 6706 6707 if (sbi->s_mount_state & EXT4_FC_REPLAY) { 6708 ext4_free_blocks_simple(inode, block, EXT4_NUM_B2C(sbi, count)); 6709 return; 6710 } 6711 6712 might_sleep(); 6713 6714 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) && 6715 !ext4_inode_block_valid(inode, block, count)) { 6716 ext4_error(sb, "Freeing blocks not in datazone - " 6717 "block = %llu, count = %lu", block, count); 6718 return; 6719 } 6720 flags |= EXT4_FREE_BLOCKS_VALIDATED; 6721 6722 ext4_debug("freeing block %llu\n", block); 6723 trace_ext4_free_blocks(inode, block, count, flags); 6724 6725 if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { 6726 BUG_ON(count > 1); 6727 6728 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA, 6729 inode, bh, block); 6730 } 6731 6732 /* 6733 * If the extent to be freed does not begin on a cluster 6734 * boundary, we need to deal with partial clusters at the 6735 * beginning and end of the extent. Normally we will free 6736 * blocks at the beginning or the end unless we are explicitly 6737 * requested to avoid doing so. 6738 */ 6739 overflow = EXT4_PBLK_COFF(sbi, block); 6740 if (overflow) { 6741 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) { 6742 overflow = sbi->s_cluster_ratio - overflow; 6743 block += overflow; 6744 if (count > overflow) 6745 count -= overflow; 6746 else 6747 return; 6748 } else { 6749 block -= overflow; 6750 count += overflow; 6751 } 6752 /* The range changed so it's no longer validated */ 6753 flags &= ~EXT4_FREE_BLOCKS_VALIDATED; 6754 } 6755 overflow = EXT4_LBLK_COFF(sbi, count); 6756 if (overflow) { 6757 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) { 6758 if (count > overflow) 6759 count -= overflow; 6760 else 6761 return; 6762 } else 6763 count += sbi->s_cluster_ratio - overflow; 6764 /* The range changed so it's no longer validated */ 6765 flags &= ~EXT4_FREE_BLOCKS_VALIDATED; 6766 } 6767 6768 if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) { 6769 int i; 6770 int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA; 6771 6772 for (i = 0; i < count; i++) { 6773 cond_resched(); 6774 if (is_metadata) 6775 bh = sb_find_get_block_nonatomic(inode->i_sb, 6776 block + i); 6777 ext4_forget(handle, is_metadata, inode, bh, block + i); 6778 } 6779 } 6780 6781 ext4_mb_clear_bb(handle, inode, block, count, flags); 6782 } 6783 6784 /** 6785 * ext4_group_add_blocks() -- Add given blocks to an existing group 6786 * @handle: handle to this transaction 6787 * @sb: super block 6788 * @block: start physical block to add to the block group 6789 * @count: number of blocks to free 6790 * 6791 * This marks the blocks as free in the bitmap and buddy. 6792 */ 6793 int ext4_group_add_blocks(handle_t *handle, struct super_block *sb, 6794 ext4_fsblk_t block, unsigned long count) 6795 { 6796 ext4_group_t block_group; 6797 ext4_grpblk_t bit; 6798 struct ext4_sb_info *sbi = EXT4_SB(sb); 6799 struct ext4_buddy e4b; 6800 int err = 0; 6801 ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block); 6802 ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1); 6803 unsigned long cluster_count = last_cluster - first_cluster + 1; 6804 ext4_grpblk_t changed; 6805 6806 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1); 6807 6808 if (cluster_count == 0) 6809 return 0; 6810 6811 ext4_get_group_no_and_offset(sb, block, &block_group, &bit); 6812 /* 6813 * Check to see if we are freeing blocks across a group 6814 * boundary. 6815 */ 6816 if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) { 6817 ext4_warning(sb, "too many blocks added to group %u", 6818 block_group); 6819 err = -EINVAL; 6820 goto error_out; 6821 } 6822 6823 err = ext4_mb_load_buddy(sb, block_group, &e4b); 6824 if (err) 6825 goto error_out; 6826 6827 if (!ext4_sb_block_valid(sb, NULL, block, count)) { 6828 ext4_error(sb, "Adding blocks in system zones - " 6829 "Block = %llu, count = %lu", 6830 block, count); 6831 err = -EINVAL; 6832 goto error_clean; 6833 } 6834 6835 err = ext4_mb_mark_context(handle, sb, false, block_group, bit, 6836 cluster_count, EXT4_MB_BITMAP_MARKED_CHECK, 6837 &changed); 6838 if (err && changed == 0) 6839 goto error_clean; 6840 6841 if (changed != cluster_count) 6842 ext4_error(sb, "bit already cleared in group %u", block_group); 6843 6844 ext4_lock_group(sb, block_group); 6845 mb_free_blocks(NULL, &e4b, bit, cluster_count); 6846 ext4_unlock_group(sb, block_group); 6847 percpu_counter_add(&sbi->s_freeclusters_counter, 6848 changed); 6849 6850 error_clean: 6851 ext4_mb_unload_buddy(&e4b); 6852 error_out: 6853 ext4_std_error(sb, err); 6854 return err; 6855 } 6856 6857 /** 6858 * ext4_trim_extent -- function to TRIM one single free extent in the group 6859 * @sb: super block for the file system 6860 * @start: starting block of the free extent in the alloc. group 6861 * @count: number of blocks to TRIM 6862 * @e4b: ext4 buddy for the group 6863 * 6864 * Trim "count" blocks starting at "start" in the "group". To assure that no 6865 * one will allocate those blocks, mark it as used in buddy bitmap. This must 6866 * be called with under the group lock. 6867 */ 6868 static int ext4_trim_extent(struct super_block *sb, 6869 int start, int count, struct ext4_buddy *e4b) 6870 __releases(bitlock) 6871 __acquires(bitlock) 6872 { 6873 struct ext4_free_extent ex; 6874 ext4_group_t group = e4b->bd_group; 6875 int ret = 0; 6876 6877 trace_ext4_trim_extent(sb, group, start, count); 6878 6879 assert_spin_locked(ext4_group_lock_ptr(sb, group)); 6880 6881 ex.fe_start = start; 6882 ex.fe_group = group; 6883 ex.fe_len = count; 6884 6885 /* 6886 * Mark blocks used, so no one can reuse them while 6887 * being trimmed. 6888 */ 6889 mb_mark_used(e4b, &ex); 6890 ext4_unlock_group(sb, group); 6891 ret = ext4_issue_discard(sb, group, start, count); 6892 ext4_lock_group(sb, group); 6893 mb_free_blocks(NULL, e4b, start, ex.fe_len); 6894 return ret; 6895 } 6896 6897 static ext4_grpblk_t ext4_last_grp_cluster(struct super_block *sb, 6898 ext4_group_t grp) 6899 { 6900 unsigned long nr_clusters_in_group; 6901 6902 if (grp < (ext4_get_groups_count(sb) - 1)) 6903 nr_clusters_in_group = EXT4_CLUSTERS_PER_GROUP(sb); 6904 else 6905 nr_clusters_in_group = (ext4_blocks_count(EXT4_SB(sb)->s_es) - 6906 ext4_group_first_block_no(sb, grp)) 6907 >> EXT4_CLUSTER_BITS(sb); 6908 6909 return nr_clusters_in_group - 1; 6910 } 6911 6912 static bool ext4_trim_interrupted(void) 6913 { 6914 return fatal_signal_pending(current) || freezing(current); 6915 } 6916 6917 static int ext4_try_to_trim_range(struct super_block *sb, 6918 struct ext4_buddy *e4b, ext4_grpblk_t start, 6919 ext4_grpblk_t max, ext4_grpblk_t minblocks) 6920 __acquires(ext4_group_lock_ptr(sb, e4b->bd_group)) 6921 __releases(ext4_group_lock_ptr(sb, e4b->bd_group)) 6922 { 6923 ext4_grpblk_t next, count, free_count, last, origin_start; 6924 bool set_trimmed = false; 6925 void *bitmap; 6926 6927 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) 6928 return 0; 6929 6930 last = ext4_last_grp_cluster(sb, e4b->bd_group); 6931 bitmap = e4b->bd_bitmap; 6932 if (start == 0 && max >= last) 6933 set_trimmed = true; 6934 origin_start = start; 6935 start = max(e4b->bd_info->bb_first_free, start); 6936 count = 0; 6937 free_count = 0; 6938 6939 while (start <= max) { 6940 start = mb_find_next_zero_bit(bitmap, max + 1, start); 6941 if (start > max) 6942 break; 6943 6944 next = mb_find_next_bit(bitmap, last + 1, start); 6945 if (origin_start == 0 && next >= last) 6946 set_trimmed = true; 6947 6948 if ((next - start) >= minblocks) { 6949 int ret = ext4_trim_extent(sb, start, next - start, e4b); 6950 6951 if (ret && ret != -EOPNOTSUPP) 6952 return count; 6953 count += next - start; 6954 } 6955 free_count += next - start; 6956 start = next + 1; 6957 6958 if (ext4_trim_interrupted()) 6959 return count; 6960 6961 if (need_resched()) { 6962 ext4_unlock_group(sb, e4b->bd_group); 6963 cond_resched(); 6964 ext4_lock_group(sb, e4b->bd_group); 6965 } 6966 6967 if ((e4b->bd_info->bb_free - free_count) < minblocks) 6968 break; 6969 } 6970 6971 if (set_trimmed) 6972 EXT4_MB_GRP_SET_TRIMMED(e4b->bd_info); 6973 6974 return count; 6975 } 6976 6977 /** 6978 * ext4_trim_all_free -- function to trim all free space in alloc. group 6979 * @sb: super block for file system 6980 * @group: group to be trimmed 6981 * @start: first group block to examine 6982 * @max: last group block to examine 6983 * @minblocks: minimum extent block count 6984 * 6985 * ext4_trim_all_free walks through group's block bitmap searching for free 6986 * extents. When the free extent is found, mark it as used in group buddy 6987 * bitmap. Then issue a TRIM command on this extent and free the extent in 6988 * the group buddy bitmap. 6989 */ 6990 static ext4_grpblk_t 6991 ext4_trim_all_free(struct super_block *sb, ext4_group_t group, 6992 ext4_grpblk_t start, ext4_grpblk_t max, 6993 ext4_grpblk_t minblocks) 6994 { 6995 struct ext4_buddy e4b; 6996 int ret; 6997 6998 trace_ext4_trim_all_free(sb, group, start, max); 6999 7000 ret = ext4_mb_load_buddy(sb, group, &e4b); 7001 if (ret) { 7002 ext4_warning(sb, "Error %d loading buddy information for %u", 7003 ret, group); 7004 return ret; 7005 } 7006 7007 ext4_lock_group(sb, group); 7008 7009 if (!EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) || 7010 minblocks < EXT4_SB(sb)->s_last_trim_minblks) 7011 ret = ext4_try_to_trim_range(sb, &e4b, start, max, minblocks); 7012 else 7013 ret = 0; 7014 7015 ext4_unlock_group(sb, group); 7016 ext4_mb_unload_buddy(&e4b); 7017 7018 ext4_debug("trimmed %d blocks in the group %d\n", 7019 ret, group); 7020 7021 return ret; 7022 } 7023 7024 /** 7025 * ext4_trim_fs() -- trim ioctl handle function 7026 * @sb: superblock for filesystem 7027 * @range: fstrim_range structure 7028 * 7029 * start: First Byte to trim 7030 * len: number of Bytes to trim from start 7031 * minlen: minimum extent length in Bytes 7032 * ext4_trim_fs goes through all allocation groups containing Bytes from 7033 * start to start+len. For each such a group ext4_trim_all_free function 7034 * is invoked to trim all free space. 7035 */ 7036 int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) 7037 { 7038 unsigned int discard_granularity = bdev_discard_granularity(sb->s_bdev); 7039 struct ext4_group_info *grp; 7040 ext4_group_t group, first_group, last_group; 7041 ext4_grpblk_t cnt = 0, first_cluster, last_cluster; 7042 uint64_t start, end, minlen, trimmed = 0; 7043 ext4_fsblk_t first_data_blk = 7044 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block); 7045 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es); 7046 int ret = 0; 7047 7048 start = range->start >> sb->s_blocksize_bits; 7049 end = start + (range->len >> sb->s_blocksize_bits) - 1; 7050 minlen = EXT4_NUM_B2C(EXT4_SB(sb), 7051 range->minlen >> sb->s_blocksize_bits); 7052 7053 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) || 7054 start >= max_blks || 7055 range->len < sb->s_blocksize) 7056 return -EINVAL; 7057 /* No point to try to trim less than discard granularity */ 7058 if (range->minlen < discard_granularity) { 7059 minlen = EXT4_NUM_B2C(EXT4_SB(sb), 7060 discard_granularity >> sb->s_blocksize_bits); 7061 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb)) 7062 goto out; 7063 } 7064 if (end >= max_blks - 1) 7065 end = max_blks - 1; 7066 if (end <= first_data_blk) 7067 goto out; 7068 if (start < first_data_blk) 7069 start = first_data_blk; 7070 7071 /* Determine first and last group to examine based on start and end */ 7072 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start, 7073 &first_group, &first_cluster); 7074 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end, 7075 &last_group, &last_cluster); 7076 7077 /* end now represents the last cluster to discard in this group */ 7078 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; 7079 7080 for (group = first_group; group <= last_group; group++) { 7081 if (ext4_trim_interrupted()) 7082 break; 7083 grp = ext4_get_group_info(sb, group); 7084 if (!grp) 7085 continue; 7086 /* We only do this if the grp has never been initialized */ 7087 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) { 7088 ret = ext4_mb_init_group(sb, group, GFP_NOFS); 7089 if (ret) 7090 break; 7091 } 7092 7093 /* 7094 * For all the groups except the last one, last cluster will 7095 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to 7096 * change it for the last group, note that last_cluster is 7097 * already computed earlier by ext4_get_group_no_and_offset() 7098 */ 7099 if (group == last_group) 7100 end = last_cluster; 7101 if (grp->bb_free >= minlen) { 7102 cnt = ext4_trim_all_free(sb, group, first_cluster, 7103 end, minlen); 7104 if (cnt < 0) { 7105 ret = cnt; 7106 break; 7107 } 7108 trimmed += cnt; 7109 } 7110 7111 /* 7112 * For every group except the first one, we are sure 7113 * that the first cluster to discard will be cluster #0. 7114 */ 7115 first_cluster = 0; 7116 } 7117 7118 if (!ret) 7119 EXT4_SB(sb)->s_last_trim_minblks = minlen; 7120 7121 out: 7122 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits; 7123 return ret; 7124 } 7125 7126 /* Iterate all the free extents in the group. */ 7127 int 7128 ext4_mballoc_query_range( 7129 struct super_block *sb, 7130 ext4_group_t group, 7131 ext4_grpblk_t first, 7132 ext4_grpblk_t end, 7133 ext4_mballoc_query_range_fn meta_formatter, 7134 ext4_mballoc_query_range_fn formatter, 7135 void *priv) 7136 { 7137 void *bitmap; 7138 ext4_grpblk_t start, next; 7139 struct ext4_buddy e4b; 7140 int error; 7141 7142 error = ext4_mb_load_buddy(sb, group, &e4b); 7143 if (error) 7144 return error; 7145 bitmap = e4b.bd_bitmap; 7146 7147 ext4_lock_group(sb, group); 7148 7149 start = max(e4b.bd_info->bb_first_free, first); 7150 if (end >= EXT4_CLUSTERS_PER_GROUP(sb)) 7151 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1; 7152 if (meta_formatter && start != first) { 7153 if (start > end) 7154 start = end; 7155 ext4_unlock_group(sb, group); 7156 error = meta_formatter(sb, group, first, start - first, 7157 priv); 7158 if (error) 7159 goto out_unload; 7160 ext4_lock_group(sb, group); 7161 } 7162 while (start <= end) { 7163 start = mb_find_next_zero_bit(bitmap, end + 1, start); 7164 if (start > end) 7165 break; 7166 next = mb_find_next_bit(bitmap, end + 1, start); 7167 7168 ext4_unlock_group(sb, group); 7169 error = formatter(sb, group, start, next - start, priv); 7170 if (error) 7171 goto out_unload; 7172 ext4_lock_group(sb, group); 7173 7174 start = next + 1; 7175 } 7176 7177 ext4_unlock_group(sb, group); 7178 out_unload: 7179 ext4_mb_unload_buddy(&e4b); 7180 7181 return error; 7182 } 7183 7184 #if IS_ENABLED(CONFIG_EXT4_KUNIT_TESTS) 7185 void mb_clear_bits_test(void *bm, int cur, int len) 7186 { 7187 mb_clear_bits(bm, cur, len); 7188 } 7189 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_clear_bits_test); 7190 7191 ext4_fsblk_t 7192 ext4_mb_new_blocks_simple_test(struct ext4_allocation_request *ar, 7193 int *errp) 7194 { 7195 return ext4_mb_new_blocks_simple(ar, errp); 7196 } 7197 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_new_blocks_simple_test); 7198 7199 int mb_find_next_zero_bit_test(void *addr, int max, int start) 7200 { 7201 return mb_find_next_zero_bit(addr, max, start); 7202 } 7203 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_find_next_zero_bit_test); 7204 7205 int mb_find_next_bit_test(void *addr, int max, int start) 7206 { 7207 return mb_find_next_bit(addr, max, start); 7208 } 7209 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_find_next_bit_test); 7210 7211 void mb_clear_bit_test(int bit, void *addr) 7212 { 7213 mb_clear_bit(bit, addr); 7214 } 7215 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_clear_bit_test); 7216 7217 int mb_test_bit_test(int bit, void *addr) 7218 { 7219 return mb_test_bit(bit, addr); 7220 } 7221 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_test_bit_test); 7222 7223 int ext4_mb_mark_diskspace_used_test(struct ext4_allocation_context *ac, 7224 handle_t *handle) 7225 { 7226 return ext4_mb_mark_diskspace_used(ac, handle); 7227 } 7228 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_mark_diskspace_used_test); 7229 7230 int mb_mark_used_test(struct ext4_buddy *e4b, struct ext4_free_extent *ex) 7231 { 7232 return mb_mark_used(e4b, ex); 7233 } 7234 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_mark_used_test); 7235 7236 void ext4_mb_generate_buddy_test(struct super_block *sb, void *buddy, 7237 void *bitmap, ext4_group_t group, 7238 struct ext4_group_info *grp) 7239 { 7240 ext4_mb_generate_buddy(sb, buddy, bitmap, group, grp); 7241 } 7242 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_generate_buddy_test); 7243 7244 int ext4_mb_load_buddy_test(struct super_block *sb, ext4_group_t group, 7245 struct ext4_buddy *e4b) 7246 { 7247 return ext4_mb_load_buddy(sb, group, e4b); 7248 } 7249 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_load_buddy_test); 7250 7251 void ext4_mb_unload_buddy_test(struct ext4_buddy *e4b) 7252 { 7253 ext4_mb_unload_buddy(e4b); 7254 } 7255 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_unload_buddy_test); 7256 7257 void mb_free_blocks_test(struct inode *inode, struct ext4_buddy *e4b, 7258 int first, int count) 7259 { 7260 mb_free_blocks(inode, e4b, first, count); 7261 } 7262 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_free_blocks_test); 7263 7264 void ext4_free_blocks_simple_test(struct inode *inode, ext4_fsblk_t block, 7265 unsigned long count) 7266 { 7267 return ext4_free_blocks_simple(inode, block, count); 7268 } 7269 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_free_blocks_simple_test); 7270 7271 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_wait_block_bitmap); 7272 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_init); 7273 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_get_group_desc); 7274 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_count_free_clusters); 7275 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_get_group_info); 7276 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_free_group_clusters_set); 7277 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_release); 7278 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_read_block_bitmap_nowait); 7279 EXPORT_SYMBOL_FOR_EXT4_TEST(mb_set_bits); 7280 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fc_init_inode); 7281 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_mb_mark_context); 7282 #endif 7283