1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/sizes.h> 4 #include <linux/list_sort.h> 5 #include "misc.h" 6 #include "ctree.h" 7 #include "block-group.h" 8 #include "space-info.h" 9 #include "disk-io.h" 10 #include "free-space-cache.h" 11 #include "free-space-tree.h" 12 #include "volumes.h" 13 #include "transaction.h" 14 #include "ref-verify.h" 15 #include "sysfs.h" 16 #include "tree-log.h" 17 #include "delalloc-space.h" 18 #include "discard.h" 19 #include "raid56.h" 20 #include "zoned.h" 21 #include "fs.h" 22 #include "accessors.h" 23 #include "extent-tree.h" 24 25 #ifdef CONFIG_BTRFS_DEBUG 26 int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group) 27 { 28 struct btrfs_fs_info *fs_info = block_group->fs_info; 29 30 return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) && 31 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) || 32 (btrfs_test_opt(fs_info, FRAGMENT_DATA) && 33 block_group->flags & BTRFS_BLOCK_GROUP_DATA); 34 } 35 #endif 36 37 static inline bool has_unwritten_metadata(struct btrfs_block_group *block_group) 38 { 39 /* The meta_write_pointer is available only on the zoned setup. */ 40 if (!btrfs_is_zoned(block_group->fs_info)) 41 return false; 42 43 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA) 44 return false; 45 46 return block_group->start + block_group->alloc_offset > 47 block_group->meta_write_pointer; 48 } 49 50 /* 51 * Return target flags in extended format or 0 if restripe for this chunk_type 52 * is not in progress 53 * 54 * Should be called with balance_lock held 55 */ 56 static u64 get_restripe_target(const struct btrfs_fs_info *fs_info, u64 flags) 57 { 58 const struct btrfs_balance_control *bctl = fs_info->balance_ctl; 59 u64 target = 0; 60 61 if (!bctl) 62 return 0; 63 64 if (flags & BTRFS_BLOCK_GROUP_DATA && 65 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { 66 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; 67 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && 68 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { 69 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; 70 } else if (flags & BTRFS_BLOCK_GROUP_METADATA && 71 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { 72 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; 73 } 74 75 return target; 76 } 77 78 /* 79 * @flags: available profiles in extended format (see ctree.h) 80 * 81 * Return reduced profile in chunk format. If profile changing is in progress 82 * (either running or paused) picks the target profile (if it's already 83 * available), otherwise falls back to plain reducing. 84 */ 85 static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags) 86 { 87 u64 num_devices = fs_info->fs_devices->rw_devices; 88 u64 target; 89 u64 raid_type; 90 u64 allowed = 0; 91 92 /* 93 * See if restripe for this chunk_type is in progress, if so try to 94 * reduce to the target profile 95 */ 96 spin_lock(&fs_info->balance_lock); 97 target = get_restripe_target(fs_info, flags); 98 if (target) { 99 spin_unlock(&fs_info->balance_lock); 100 return extended_to_chunk(target); 101 } 102 spin_unlock(&fs_info->balance_lock); 103 104 /* First, mask out the RAID levels which aren't possible */ 105 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 106 if (num_devices >= btrfs_raid_array[raid_type].devs_min) 107 allowed |= btrfs_raid_array[raid_type].bg_flag; 108 } 109 allowed &= flags; 110 111 /* Select the highest-redundancy RAID level. */ 112 if (allowed & BTRFS_BLOCK_GROUP_RAID1C4) 113 allowed = BTRFS_BLOCK_GROUP_RAID1C4; 114 else if (allowed & BTRFS_BLOCK_GROUP_RAID6) 115 allowed = BTRFS_BLOCK_GROUP_RAID6; 116 else if (allowed & BTRFS_BLOCK_GROUP_RAID1C3) 117 allowed = BTRFS_BLOCK_GROUP_RAID1C3; 118 else if (allowed & BTRFS_BLOCK_GROUP_RAID5) 119 allowed = BTRFS_BLOCK_GROUP_RAID5; 120 else if (allowed & BTRFS_BLOCK_GROUP_RAID10) 121 allowed = BTRFS_BLOCK_GROUP_RAID10; 122 else if (allowed & BTRFS_BLOCK_GROUP_RAID1) 123 allowed = BTRFS_BLOCK_GROUP_RAID1; 124 else if (allowed & BTRFS_BLOCK_GROUP_DUP) 125 allowed = BTRFS_BLOCK_GROUP_DUP; 126 else if (allowed & BTRFS_BLOCK_GROUP_RAID0) 127 allowed = BTRFS_BLOCK_GROUP_RAID0; 128 129 flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; 130 131 return extended_to_chunk(flags | allowed); 132 } 133 134 u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) 135 { 136 unsigned seq; 137 u64 flags; 138 139 do { 140 flags = orig_flags; 141 seq = read_seqbegin(&fs_info->profiles_lock); 142 143 if (flags & BTRFS_BLOCK_GROUP_DATA) 144 flags |= fs_info->avail_data_alloc_bits; 145 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 146 flags |= fs_info->avail_system_alloc_bits; 147 else if (flags & BTRFS_BLOCK_GROUP_METADATA) 148 flags |= fs_info->avail_metadata_alloc_bits; 149 } while (read_seqretry(&fs_info->profiles_lock, seq)); 150 151 return btrfs_reduce_alloc_profile(fs_info, flags); 152 } 153 154 void btrfs_get_block_group(struct btrfs_block_group *cache) 155 { 156 refcount_inc(&cache->refs); 157 } 158 159 void btrfs_put_block_group(struct btrfs_block_group *cache) 160 { 161 if (refcount_dec_and_test(&cache->refs)) { 162 WARN_ON(cache->pinned > 0); 163 /* 164 * If there was a failure to cleanup a log tree, very likely due 165 * to an IO failure on a writeback attempt of one or more of its 166 * extent buffers, we could not do proper (and cheap) unaccounting 167 * of their reserved space, so don't warn on reserved > 0 in that 168 * case. 169 */ 170 if (!(cache->flags & BTRFS_BLOCK_GROUP_METADATA) || 171 !BTRFS_FS_LOG_CLEANUP_ERROR(cache->fs_info)) 172 WARN_ON(cache->reserved > 0); 173 174 /* 175 * A block_group shouldn't be on the discard_list anymore. 176 * Remove the block_group from the discard_list to prevent us 177 * from causing a panic due to NULL pointer dereference. 178 */ 179 if (WARN_ON(!list_empty(&cache->discard_list))) 180 btrfs_discard_cancel_work(&cache->fs_info->discard_ctl, 181 cache); 182 183 kfree(cache->free_space_ctl); 184 btrfs_free_chunk_map(cache->physical_map); 185 kfree(cache); 186 } 187 } 188 189 static int btrfs_bg_start_cmp(const struct rb_node *new, 190 const struct rb_node *exist) 191 { 192 const struct btrfs_block_group *new_bg = 193 rb_entry(new, struct btrfs_block_group, cache_node); 194 const struct btrfs_block_group *exist_bg = 195 rb_entry(exist, struct btrfs_block_group, cache_node); 196 197 if (new_bg->start < exist_bg->start) 198 return -1; 199 if (new_bg->start > exist_bg->start) 200 return 1; 201 return 0; 202 } 203 204 /* 205 * This adds the block group to the fs_info rb tree for the block group cache 206 */ 207 static int btrfs_add_block_group_cache(struct btrfs_block_group *block_group) 208 { 209 struct btrfs_fs_info *fs_info = block_group->fs_info; 210 struct rb_node *exist; 211 int ret = 0; 212 213 ASSERT(block_group->length != 0); 214 215 write_lock(&fs_info->block_group_cache_lock); 216 217 exist = rb_find_add_cached(&block_group->cache_node, 218 &fs_info->block_group_cache_tree, btrfs_bg_start_cmp); 219 if (exist) 220 ret = -EEXIST; 221 write_unlock(&fs_info->block_group_cache_lock); 222 223 return ret; 224 } 225 226 /* 227 * This will return the block group at or after bytenr if contains is 0, else 228 * it will return the block group that contains the bytenr 229 */ 230 static struct btrfs_block_group *block_group_cache_tree_search( 231 struct btrfs_fs_info *info, u64 bytenr, int contains) 232 { 233 struct btrfs_block_group *cache, *ret = NULL; 234 struct rb_node *n; 235 u64 end, start; 236 237 read_lock(&info->block_group_cache_lock); 238 n = info->block_group_cache_tree.rb_root.rb_node; 239 240 while (n) { 241 cache = rb_entry(n, struct btrfs_block_group, cache_node); 242 end = btrfs_block_group_end(cache) - 1; 243 start = cache->start; 244 245 if (bytenr < start) { 246 if (!contains && (!ret || start < ret->start)) 247 ret = cache; 248 n = n->rb_left; 249 } else if (bytenr > start) { 250 if (contains && bytenr <= end) { 251 ret = cache; 252 break; 253 } 254 n = n->rb_right; 255 } else { 256 ret = cache; 257 break; 258 } 259 } 260 if (ret) 261 btrfs_get_block_group(ret); 262 read_unlock(&info->block_group_cache_lock); 263 264 return ret; 265 } 266 267 /* 268 * Return the block group that starts at or after bytenr 269 */ 270 struct btrfs_block_group *btrfs_lookup_first_block_group( 271 struct btrfs_fs_info *info, u64 bytenr) 272 { 273 return block_group_cache_tree_search(info, bytenr, 0); 274 } 275 276 /* 277 * Return the block group that contains the given bytenr 278 */ 279 struct btrfs_block_group *btrfs_lookup_block_group( 280 struct btrfs_fs_info *info, u64 bytenr) 281 { 282 return block_group_cache_tree_search(info, bytenr, 1); 283 } 284 285 struct btrfs_block_group *btrfs_next_block_group( 286 struct btrfs_block_group *cache) 287 { 288 struct btrfs_fs_info *fs_info = cache->fs_info; 289 struct rb_node *node; 290 291 read_lock(&fs_info->block_group_cache_lock); 292 293 /* If our block group was removed, we need a full search. */ 294 if (RB_EMPTY_NODE(&cache->cache_node)) { 295 const u64 next_bytenr = btrfs_block_group_end(cache); 296 297 read_unlock(&fs_info->block_group_cache_lock); 298 btrfs_put_block_group(cache); 299 return btrfs_lookup_first_block_group(fs_info, next_bytenr); 300 } 301 node = rb_next(&cache->cache_node); 302 btrfs_put_block_group(cache); 303 if (node) { 304 cache = rb_entry(node, struct btrfs_block_group, cache_node); 305 btrfs_get_block_group(cache); 306 } else 307 cache = NULL; 308 read_unlock(&fs_info->block_group_cache_lock); 309 return cache; 310 } 311 312 /* 313 * Check if we can do a NOCOW write for a given extent. 314 * 315 * @fs_info: The filesystem information object. 316 * @bytenr: Logical start address of the extent. 317 * 318 * Check if we can do a NOCOW write for the given extent, and increments the 319 * number of NOCOW writers in the block group that contains the extent, as long 320 * as the block group exists and it's currently not in read-only mode. 321 * 322 * Returns: A non-NULL block group pointer if we can do a NOCOW write, the caller 323 * is responsible for calling btrfs_dec_nocow_writers() later. 324 * 325 * Or NULL if we can not do a NOCOW write 326 */ 327 struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, 328 u64 bytenr) 329 { 330 struct btrfs_block_group *bg; 331 bool can_nocow = true; 332 333 bg = btrfs_lookup_block_group(fs_info, bytenr); 334 if (!bg) 335 return NULL; 336 337 spin_lock(&bg->lock); 338 if (bg->ro) 339 can_nocow = false; 340 else 341 atomic_inc(&bg->nocow_writers); 342 spin_unlock(&bg->lock); 343 344 if (!can_nocow) { 345 btrfs_put_block_group(bg); 346 return NULL; 347 } 348 349 /* No put on block group, done by btrfs_dec_nocow_writers(). */ 350 return bg; 351 } 352 353 /* 354 * Decrement the number of NOCOW writers in a block group. 355 * 356 * This is meant to be called after a previous call to btrfs_inc_nocow_writers(), 357 * and on the block group returned by that call. Typically this is called after 358 * creating an ordered extent for a NOCOW write, to prevent races with scrub and 359 * relocation. 360 * 361 * After this call, the caller should not use the block group anymore. It it wants 362 * to use it, then it should get a reference on it before calling this function. 363 */ 364 void btrfs_dec_nocow_writers(struct btrfs_block_group *bg) 365 { 366 if (atomic_dec_and_test(&bg->nocow_writers)) 367 wake_up_var(&bg->nocow_writers); 368 369 /* For the lookup done by a previous call to btrfs_inc_nocow_writers(). */ 370 btrfs_put_block_group(bg); 371 } 372 373 void btrfs_wait_nocow_writers(struct btrfs_block_group *bg) 374 { 375 wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); 376 } 377 378 void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, 379 const u64 start) 380 { 381 struct btrfs_block_group *bg; 382 383 bg = btrfs_lookup_block_group(fs_info, start); 384 ASSERT(bg); 385 if (atomic_dec_and_test(&bg->reservations)) 386 wake_up_var(&bg->reservations); 387 btrfs_put_block_group(bg); 388 } 389 390 void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg) 391 { 392 struct btrfs_space_info *space_info = bg->space_info; 393 394 ASSERT(bg->ro); 395 396 if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) 397 return; 398 399 /* 400 * Our block group is read only but before we set it to read only, 401 * some task might have had allocated an extent from it already, but it 402 * has not yet created a respective ordered extent (and added it to a 403 * root's list of ordered extents). 404 * Therefore wait for any task currently allocating extents, since the 405 * block group's reservations counter is incremented while a read lock 406 * on the groups' semaphore is held and decremented after releasing 407 * the read access on that semaphore and creating the ordered extent. 408 */ 409 down_write(&space_info->groups_sem); 410 up_write(&space_info->groups_sem); 411 412 wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); 413 } 414 415 struct btrfs_caching_control *btrfs_get_caching_control( 416 struct btrfs_block_group *cache) 417 { 418 struct btrfs_caching_control *ctl; 419 420 spin_lock(&cache->lock); 421 if (!cache->caching_ctl) { 422 spin_unlock(&cache->lock); 423 return NULL; 424 } 425 426 ctl = cache->caching_ctl; 427 refcount_inc(&ctl->count); 428 spin_unlock(&cache->lock); 429 return ctl; 430 } 431 432 static void btrfs_put_caching_control(struct btrfs_caching_control *ctl) 433 { 434 if (refcount_dec_and_test(&ctl->count)) 435 kfree(ctl); 436 } 437 438 /* 439 * When we wait for progress in the block group caching, its because our 440 * allocation attempt failed at least once. So, we must sleep and let some 441 * progress happen before we try again. 442 * 443 * This function will sleep at least once waiting for new free space to show 444 * up, and then it will check the block group free space numbers for our min 445 * num_bytes. Another option is to have it go ahead and look in the rbtree for 446 * a free extent of a given size, but this is a good start. 447 * 448 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using 449 * any of the information in this block group. 450 */ 451 void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, 452 u64 num_bytes) 453 { 454 struct btrfs_caching_control *caching_ctl; 455 int progress; 456 457 caching_ctl = btrfs_get_caching_control(cache); 458 if (!caching_ctl) 459 return; 460 461 /* 462 * We've already failed to allocate from this block group, so even if 463 * there's enough space in the block group it isn't contiguous enough to 464 * allow for an allocation, so wait for at least the next wakeup tick, 465 * or for the thing to be done. 466 */ 467 progress = atomic_read(&caching_ctl->progress); 468 469 wait_event(caching_ctl->wait, btrfs_block_group_done(cache) || 470 (progress != atomic_read(&caching_ctl->progress) && 471 (cache->free_space_ctl->free_space >= num_bytes))); 472 473 btrfs_put_caching_control(caching_ctl); 474 } 475 476 static int btrfs_caching_ctl_wait_done(struct btrfs_block_group *cache, 477 struct btrfs_caching_control *caching_ctl) 478 { 479 wait_event(caching_ctl->wait, btrfs_block_group_done(cache)); 480 return cache->cached == BTRFS_CACHE_ERROR ? -EIO : 0; 481 } 482 483 static int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache) 484 { 485 struct btrfs_caching_control *caching_ctl; 486 int ret; 487 488 caching_ctl = btrfs_get_caching_control(cache); 489 if (!caching_ctl) 490 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; 491 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); 492 btrfs_put_caching_control(caching_ctl); 493 return ret; 494 } 495 496 #ifdef CONFIG_BTRFS_DEBUG 497 static void fragment_free_space(struct btrfs_block_group *block_group) 498 { 499 struct btrfs_fs_info *fs_info = block_group->fs_info; 500 u64 start = block_group->start; 501 u64 len = block_group->length; 502 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? 503 fs_info->nodesize : fs_info->sectorsize; 504 u64 step = chunk << 1; 505 506 while (len > chunk) { 507 btrfs_remove_free_space(block_group, start, chunk); 508 start += step; 509 if (len < step) 510 len = 0; 511 else 512 len -= step; 513 } 514 } 515 #endif 516 517 /* 518 * Add a free space range to the in memory free space cache of a block group. 519 * This checks if the range contains super block locations and any such 520 * locations are not added to the free space cache. 521 * 522 * @block_group: The target block group. 523 * @start: Start offset of the range. 524 * @end: End offset of the range (exclusive). 525 * @total_added_ret: Optional pointer to return the total amount of space 526 * added to the block group's free space cache. 527 * 528 * Returns 0 on success or < 0 on error. 529 */ 530 int btrfs_add_new_free_space(struct btrfs_block_group *block_group, u64 start, 531 u64 end, u64 *total_added_ret) 532 { 533 struct btrfs_fs_info *info = block_group->fs_info; 534 u64 extent_start, extent_end, size; 535 int ret; 536 537 if (total_added_ret) 538 *total_added_ret = 0; 539 540 while (start < end) { 541 if (!btrfs_find_first_extent_bit(&info->excluded_extents, start, 542 &extent_start, &extent_end, 543 EXTENT_DIRTY, NULL)) 544 break; 545 546 if (extent_start <= start) { 547 start = extent_end + 1; 548 } else if (extent_start > start && extent_start < end) { 549 size = extent_start - start; 550 ret = btrfs_add_free_space_async_trimmed(block_group, 551 start, size); 552 if (ret) 553 return ret; 554 if (total_added_ret) 555 *total_added_ret += size; 556 start = extent_end + 1; 557 } else { 558 break; 559 } 560 } 561 562 if (start < end) { 563 size = end - start; 564 ret = btrfs_add_free_space_async_trimmed(block_group, start, 565 size); 566 if (ret) 567 return ret; 568 if (total_added_ret) 569 *total_added_ret += size; 570 } 571 572 return 0; 573 } 574 575 /* 576 * Get an arbitrary extent item index / max_index through the block group 577 * 578 * @caching_ctl the caching control containing the block group to sample from 579 * @index: the integral step through the block group to grab from 580 * @max_index: the granularity of the sampling 581 * @key: return value parameter for the item we find 582 * @path: path to use for searching in the extent tree 583 * 584 * Pre-conditions on indices: 585 * 0 <= index <= max_index 586 * 0 < max_index 587 * 588 * Returns: 0 on success, 1 if the search didn't yield a useful item. 589 */ 590 static int sample_block_group_extent_item(struct btrfs_caching_control *caching_ctl, 591 int index, int max_index, 592 struct btrfs_key *found_key, 593 struct btrfs_path *path) 594 { 595 struct btrfs_block_group *block_group = caching_ctl->block_group; 596 struct btrfs_fs_info *fs_info = block_group->fs_info; 597 struct btrfs_root *extent_root; 598 u64 search_offset; 599 const u64 search_end = btrfs_block_group_end(block_group); 600 struct btrfs_key search_key; 601 int ret = 0; 602 603 ASSERT(index >= 0); 604 ASSERT(index <= max_index); 605 ASSERT(max_index > 0); 606 lockdep_assert_held(&caching_ctl->mutex); 607 lockdep_assert_held_read(&fs_info->commit_root_sem); 608 609 extent_root = btrfs_extent_root(fs_info, block_group->start); 610 if (unlikely(!extent_root)) { 611 btrfs_err(fs_info, 612 "missing extent root for block group at offset %llu", 613 block_group->start); 614 return -EUCLEAN; 615 } 616 617 search_offset = index * div_u64(block_group->length, max_index); 618 search_key.objectid = block_group->start + search_offset; 619 search_key.type = BTRFS_EXTENT_ITEM_KEY; 620 search_key.offset = 0; 621 622 btrfs_for_each_slot(extent_root, &search_key, found_key, path, ret) { 623 /* Success; sampled an extent item in the block group */ 624 if (found_key->type == BTRFS_EXTENT_ITEM_KEY && 625 found_key->objectid >= block_group->start && 626 found_key->objectid + found_key->offset <= search_end) 627 break; 628 629 /* We can't possibly find a valid extent item anymore */ 630 if (found_key->objectid >= search_end) { 631 ret = 1; 632 break; 633 } 634 } 635 636 lockdep_assert_held(&caching_ctl->mutex); 637 lockdep_assert_held_read(&fs_info->commit_root_sem); 638 return ret; 639 } 640 641 /* 642 * Best effort attempt to compute a block group's size class while caching it. 643 * 644 * @block_group: the block group we are caching 645 * 646 * We cannot infer the size class while adding free space extents, because that 647 * logic doesn't care about contiguous file extents (it doesn't differentiate 648 * between a 100M extent and 100 contiguous 1M extents). So we need to read the 649 * file extent items. Reading all of them is quite wasteful, because usually 650 * only a handful are enough to give a good answer. Therefore, we just grab 5 of 651 * them at even steps through the block group and pick the smallest size class 652 * we see. Since size class is best effort, and not guaranteed in general, 653 * inaccuracy is acceptable. 654 * 655 * To be more explicit about why this algorithm makes sense: 656 * 657 * If we are caching in a block group from disk, then there are three major cases 658 * to consider: 659 * 1. the block group is well behaved and all extents in it are the same size 660 * class. 661 * 2. the block group is mostly one size class with rare exceptions for last 662 * ditch allocations 663 * 3. the block group was populated before size classes and can have a totally 664 * arbitrary mix of size classes. 665 * 666 * In case 1, looking at any extent in the block group will yield the correct 667 * result. For the mixed cases, taking the minimum size class seems like a good 668 * approximation, since gaps from frees will be usable to the size class. For 669 * 2., a small handful of file extents is likely to yield the right answer. For 670 * 3, we can either read every file extent, or admit that this is best effort 671 * anyway and try to stay fast. 672 * 673 * No errors are returned since failing to determine the size class is not a 674 * critical error, size classes are just an optimization. 675 */ 676 static void load_block_group_size_class(struct btrfs_caching_control *caching_ctl) 677 { 678 BTRFS_PATH_AUTO_RELEASE(path); 679 struct btrfs_block_group *block_group = caching_ctl->block_group; 680 struct btrfs_fs_info *fs_info = block_group->fs_info; 681 struct btrfs_key key; 682 int i; 683 u64 min_size = block_group->length; 684 enum btrfs_block_group_size_class size_class = BTRFS_BG_SZ_NONE; 685 686 /* 687 * Since we run in workqueue context, we allocate the path on stack to 688 * avoid memory allocation failure, as the stack in a work queue task 689 * is not deep. 690 */ 691 ASSERT(current_work() == &caching_ctl->work.normal_work); 692 693 if (!btrfs_block_group_should_use_size_class(block_group)) 694 return; 695 696 path.skip_locking = true; 697 path.search_commit_root = true; 698 path.reada = READA_FORWARD; 699 700 lockdep_assert_held(&caching_ctl->mutex); 701 lockdep_assert_held_read(&fs_info->commit_root_sem); 702 for (i = 0; i < 5; ++i) { 703 int ret; 704 705 ret = sample_block_group_extent_item(caching_ctl, i, 5, &key, &path); 706 if (ret < 0) 707 return; 708 btrfs_release_path(&path); 709 if (ret > 0) 710 continue; 711 min_size = min_t(u64, min_size, key.offset); 712 size_class = btrfs_calc_block_group_size_class(min_size); 713 } 714 if (size_class != BTRFS_BG_SZ_NONE) { 715 spin_lock(&block_group->lock); 716 block_group->size_class = size_class; 717 spin_unlock(&block_group->lock); 718 } 719 } 720 721 static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) 722 { 723 struct btrfs_block_group *block_group = caching_ctl->block_group; 724 const u64 block_group_end = btrfs_block_group_end(block_group); 725 struct btrfs_fs_info *fs_info = block_group->fs_info; 726 struct btrfs_root *extent_root; 727 BTRFS_PATH_AUTO_FREE(path); 728 struct extent_buffer *leaf; 729 struct btrfs_key key; 730 u64 total_found = 0; 731 u64 last = 0; 732 u32 nritems; 733 int ret; 734 bool wakeup = true; 735 736 path = btrfs_alloc_path(); 737 if (!path) 738 return -ENOMEM; 739 740 last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); 741 extent_root = btrfs_extent_root(fs_info, last); 742 743 #ifdef CONFIG_BTRFS_DEBUG 744 /* 745 * If we're fragmenting we don't want to make anybody think we can 746 * allocate from this block group until we've had a chance to fragment 747 * the free space. 748 */ 749 if (btrfs_should_fragment_free_space(block_group)) 750 wakeup = false; 751 #endif 752 /* 753 * We don't want to deadlock with somebody trying to allocate a new 754 * extent for the extent root while also trying to search the extent 755 * root to add free space. So we skip locking and search the commit 756 * root, since its read-only 757 */ 758 path->skip_locking = true; 759 path->search_commit_root = true; 760 path->reada = READA_FORWARD; 761 762 key.objectid = last; 763 key.type = BTRFS_EXTENT_ITEM_KEY; 764 key.offset = 0; 765 766 next: 767 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 768 if (ret < 0) 769 return ret; 770 771 leaf = path->nodes[0]; 772 nritems = btrfs_header_nritems(leaf); 773 774 while (1) { 775 if (btrfs_fs_closing_done(fs_info)) { 776 last = (u64)-1; 777 break; 778 } 779 780 if (path->slots[0] < nritems) { 781 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 782 } else { 783 ret = btrfs_find_next_key(extent_root, path, &key, 0, 0); 784 if (ret) 785 break; 786 787 if (need_resched() || 788 rwsem_is_contended(&fs_info->commit_root_sem)) { 789 btrfs_release_path(path); 790 up_read(&fs_info->commit_root_sem); 791 mutex_unlock(&caching_ctl->mutex); 792 cond_resched(); 793 mutex_lock(&caching_ctl->mutex); 794 down_read(&fs_info->commit_root_sem); 795 goto next; 796 } 797 798 ret = btrfs_next_leaf(extent_root, path); 799 if (ret < 0) 800 return ret; 801 if (ret) 802 break; 803 leaf = path->nodes[0]; 804 nritems = btrfs_header_nritems(leaf); 805 continue; 806 } 807 808 if (key.objectid < last) { 809 key.objectid = last; 810 key.type = BTRFS_EXTENT_ITEM_KEY; 811 key.offset = 0; 812 btrfs_release_path(path); 813 goto next; 814 } 815 816 if (key.objectid < block_group->start) { 817 path->slots[0]++; 818 continue; 819 } 820 821 if (key.objectid >= block_group_end) 822 break; 823 824 if (key.type == BTRFS_EXTENT_ITEM_KEY || 825 key.type == BTRFS_METADATA_ITEM_KEY) { 826 u64 space_added; 827 828 ret = btrfs_add_new_free_space(block_group, last, 829 key.objectid, &space_added); 830 if (ret) 831 return ret; 832 total_found += space_added; 833 if (key.type == BTRFS_METADATA_ITEM_KEY) 834 last = key.objectid + 835 fs_info->nodesize; 836 else 837 last = key.objectid + key.offset; 838 839 if (total_found > CACHING_CTL_WAKE_UP) { 840 total_found = 0; 841 if (wakeup) { 842 atomic_inc(&caching_ctl->progress); 843 wake_up(&caching_ctl->wait); 844 } 845 } 846 } 847 path->slots[0]++; 848 } 849 850 return btrfs_add_new_free_space(block_group, last, block_group_end, NULL); 851 } 852 853 static inline void btrfs_free_excluded_extents(const struct btrfs_block_group *bg) 854 { 855 btrfs_clear_extent_bit(&bg->fs_info->excluded_extents, bg->start, 856 btrfs_block_group_end(bg) - 1, EXTENT_DIRTY, NULL); 857 } 858 859 static noinline void caching_thread(struct btrfs_work *work) 860 { 861 struct btrfs_block_group *block_group; 862 struct btrfs_fs_info *fs_info; 863 struct btrfs_caching_control *caching_ctl; 864 int ret; 865 866 caching_ctl = container_of(work, struct btrfs_caching_control, work); 867 block_group = caching_ctl->block_group; 868 fs_info = block_group->fs_info; 869 870 mutex_lock(&caching_ctl->mutex); 871 down_read(&fs_info->commit_root_sem); 872 873 load_block_group_size_class(caching_ctl); 874 if (btrfs_test_opt(fs_info, SPACE_CACHE)) { 875 ret = load_free_space_cache(block_group); 876 if (ret == 1) { 877 ret = 0; 878 goto done; 879 } 880 881 /* 882 * We failed to load the space cache, set ourselves to 883 * CACHE_STARTED and carry on. 884 */ 885 spin_lock(&block_group->lock); 886 block_group->cached = BTRFS_CACHE_STARTED; 887 spin_unlock(&block_group->lock); 888 wake_up(&caching_ctl->wait); 889 } 890 891 /* 892 * If we are in the transaction that populated the free space tree we 893 * can't actually cache from the free space tree as our commit root and 894 * real root are the same, so we could change the contents of the blocks 895 * while caching. Instead do the slow caching in this case, and after 896 * the transaction has committed we will be safe. 897 */ 898 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 899 !(test_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags))) 900 ret = btrfs_load_free_space_tree(caching_ctl); 901 else 902 ret = load_extent_tree_free(caching_ctl); 903 done: 904 spin_lock(&block_group->lock); 905 block_group->caching_ctl = NULL; 906 block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; 907 spin_unlock(&block_group->lock); 908 909 #ifdef CONFIG_BTRFS_DEBUG 910 if (btrfs_should_fragment_free_space(block_group)) { 911 u64 bytes_used; 912 913 spin_lock(&block_group->space_info->lock); 914 spin_lock(&block_group->lock); 915 bytes_used = block_group->length - block_group->used; 916 block_group->space_info->bytes_used += bytes_used >> 1; 917 spin_unlock(&block_group->lock); 918 spin_unlock(&block_group->space_info->lock); 919 fragment_free_space(block_group); 920 } 921 #endif 922 923 up_read(&fs_info->commit_root_sem); 924 btrfs_free_excluded_extents(block_group); 925 mutex_unlock(&caching_ctl->mutex); 926 927 wake_up(&caching_ctl->wait); 928 929 btrfs_put_caching_control(caching_ctl); 930 btrfs_put_block_group(block_group); 931 } 932 933 int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait) 934 { 935 struct btrfs_fs_info *fs_info = cache->fs_info; 936 struct btrfs_caching_control *caching_ctl = NULL; 937 int ret = 0; 938 939 /* Allocator for zoned filesystems does not use the cache at all */ 940 if (btrfs_is_zoned(fs_info)) 941 return 0; 942 943 /* 944 * No allocations can be done from remapped block groups, so they have 945 * no entries in the free-space tree. 946 */ 947 if (cache->flags & BTRFS_BLOCK_GROUP_REMAPPED) 948 return 0; 949 950 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); 951 if (!caching_ctl) 952 return -ENOMEM; 953 954 INIT_LIST_HEAD(&caching_ctl->list); 955 mutex_init(&caching_ctl->mutex); 956 init_waitqueue_head(&caching_ctl->wait); 957 caching_ctl->block_group = cache; 958 refcount_set(&caching_ctl->count, 2); 959 atomic_set(&caching_ctl->progress, 0); 960 btrfs_init_work(&caching_ctl->work, caching_thread, NULL); 961 962 spin_lock(&cache->lock); 963 if (cache->cached != BTRFS_CACHE_NO) { 964 kfree(caching_ctl); 965 966 caching_ctl = cache->caching_ctl; 967 if (caching_ctl) 968 refcount_inc(&caching_ctl->count); 969 spin_unlock(&cache->lock); 970 goto out; 971 } 972 WARN_ON(cache->caching_ctl); 973 cache->caching_ctl = caching_ctl; 974 cache->cached = BTRFS_CACHE_STARTED; 975 spin_unlock(&cache->lock); 976 977 write_lock(&fs_info->block_group_cache_lock); 978 refcount_inc(&caching_ctl->count); 979 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); 980 write_unlock(&fs_info->block_group_cache_lock); 981 982 btrfs_get_block_group(cache); 983 984 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); 985 out: 986 if (wait && caching_ctl) 987 ret = btrfs_caching_ctl_wait_done(cache, caching_ctl); 988 if (caching_ctl) 989 btrfs_put_caching_control(caching_ctl); 990 991 return ret; 992 } 993 994 static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 995 { 996 u64 extra_flags = chunk_to_extended(flags) & 997 BTRFS_EXTENDED_PROFILE_MASK; 998 999 write_seqlock(&fs_info->profiles_lock); 1000 if (flags & BTRFS_BLOCK_GROUP_DATA) 1001 fs_info->avail_data_alloc_bits &= ~extra_flags; 1002 if (flags & BTRFS_BLOCK_GROUP_METADATA) 1003 fs_info->avail_metadata_alloc_bits &= ~extra_flags; 1004 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 1005 fs_info->avail_system_alloc_bits &= ~extra_flags; 1006 write_sequnlock(&fs_info->profiles_lock); 1007 } 1008 1009 /* 1010 * Clear incompat bits for the following feature(s): 1011 * 1012 * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group 1013 * in the whole filesystem 1014 * 1015 * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups 1016 */ 1017 static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags) 1018 { 1019 bool found_raid56 = false; 1020 bool found_raid1c34 = false; 1021 1022 if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) || 1023 (flags & BTRFS_BLOCK_GROUP_RAID1C3) || 1024 (flags & BTRFS_BLOCK_GROUP_RAID1C4)) { 1025 struct list_head *head = &fs_info->space_info; 1026 struct btrfs_space_info *sinfo; 1027 1028 list_for_each_entry_rcu(sinfo, head, list) { 1029 down_read(&sinfo->groups_sem); 1030 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) 1031 found_raid56 = true; 1032 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) 1033 found_raid56 = true; 1034 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) 1035 found_raid1c34 = true; 1036 if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) 1037 found_raid1c34 = true; 1038 up_read(&sinfo->groups_sem); 1039 } 1040 if (!found_raid56) 1041 btrfs_clear_fs_incompat(fs_info, RAID56); 1042 if (!found_raid1c34) 1043 btrfs_clear_fs_incompat(fs_info, RAID1C34); 1044 } 1045 } 1046 1047 static struct btrfs_root *btrfs_block_group_root(struct btrfs_fs_info *fs_info) 1048 { 1049 if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) 1050 return fs_info->block_group_root; 1051 return btrfs_extent_root(fs_info, 0); 1052 } 1053 1054 static int remove_block_group_item(struct btrfs_trans_handle *trans, 1055 struct btrfs_path *path, 1056 struct btrfs_block_group *block_group) 1057 { 1058 struct btrfs_fs_info *fs_info = trans->fs_info; 1059 struct btrfs_root *root; 1060 struct btrfs_key key; 1061 int ret; 1062 1063 root = btrfs_block_group_root(fs_info); 1064 key.objectid = block_group->start; 1065 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 1066 key.offset = block_group->length; 1067 1068 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 1069 if (ret > 0) 1070 ret = -ENOENT; 1071 if (ret < 0) 1072 return ret; 1073 1074 return btrfs_del_item(trans, root, path); 1075 } 1076 1077 void btrfs_remove_bg_from_sinfo(struct btrfs_block_group *bg) 1078 { 1079 int factor = btrfs_bg_type_to_factor(bg->flags); 1080 1081 spin_lock(&bg->space_info->lock); 1082 if (btrfs_test_opt(bg->fs_info, ENOSPC_DEBUG)) { 1083 WARN_ON(bg->space_info->total_bytes < bg->length); 1084 WARN_ON(bg->space_info->bytes_readonly < bg->length - bg->zone_unusable); 1085 WARN_ON(bg->space_info->bytes_zone_unusable < bg->zone_unusable); 1086 WARN_ON(bg->space_info->disk_total < bg->length * factor); 1087 } 1088 bg->space_info->total_bytes -= bg->length; 1089 bg->space_info->bytes_readonly -= (bg->length - bg->zone_unusable); 1090 btrfs_space_info_update_bytes_zone_unusable(bg->space_info, -bg->zone_unusable); 1091 bg->space_info->disk_total -= bg->length * factor; 1092 spin_unlock(&bg->space_info->lock); 1093 } 1094 1095 int btrfs_remove_block_group(struct btrfs_trans_handle *trans, 1096 struct btrfs_chunk_map *map) 1097 { 1098 struct btrfs_fs_info *fs_info = trans->fs_info; 1099 BTRFS_PATH_AUTO_FREE(path); 1100 struct btrfs_block_group *block_group; 1101 struct btrfs_free_cluster *cluster; 1102 struct inode *inode; 1103 struct kobject *kobj = NULL; 1104 int ret; 1105 int index; 1106 struct btrfs_caching_control *caching_ctl = NULL; 1107 bool remove_map; 1108 bool remove_rsv = false; 1109 1110 block_group = btrfs_lookup_block_group(fs_info, map->start); 1111 if (unlikely(!block_group)) { 1112 btrfs_abort_transaction(trans, -ENOENT); 1113 return -ENOENT; 1114 } 1115 1116 if (unlikely(!block_group->ro && 1117 !(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED))) { 1118 ret = -EUCLEAN; 1119 btrfs_abort_transaction(trans, ret); 1120 goto out; 1121 } 1122 1123 trace_btrfs_remove_block_group(block_group); 1124 /* 1125 * Free the reserved super bytes from this block group before 1126 * remove it. 1127 */ 1128 btrfs_free_excluded_extents(block_group); 1129 btrfs_free_ref_tree_range(fs_info, block_group->start, 1130 block_group->length); 1131 1132 index = btrfs_bg_flags_to_raid_index(block_group->flags); 1133 1134 /* make sure this block group isn't part of an allocation cluster */ 1135 cluster = &fs_info->data_alloc_cluster; 1136 spin_lock(&cluster->refill_lock); 1137 btrfs_return_cluster_to_free_space(block_group, cluster); 1138 spin_unlock(&cluster->refill_lock); 1139 1140 /* 1141 * make sure this block group isn't part of a metadata 1142 * allocation cluster 1143 */ 1144 cluster = &fs_info->meta_alloc_cluster; 1145 spin_lock(&cluster->refill_lock); 1146 btrfs_return_cluster_to_free_space(block_group, cluster); 1147 spin_unlock(&cluster->refill_lock); 1148 1149 btrfs_clear_treelog_bg(block_group); 1150 btrfs_clear_data_reloc_bg(block_group); 1151 1152 path = btrfs_alloc_path(); 1153 if (unlikely(!path)) { 1154 ret = -ENOMEM; 1155 btrfs_abort_transaction(trans, ret); 1156 goto out; 1157 } 1158 1159 /* 1160 * get the inode first so any iput calls done for the io_list 1161 * aren't the final iput (no unlinks allowed now) 1162 */ 1163 inode = lookup_free_space_inode(block_group, path); 1164 1165 mutex_lock(&trans->transaction->cache_write_mutex); 1166 /* 1167 * Make sure our free space cache IO is done before removing the 1168 * free space inode 1169 */ 1170 spin_lock(&trans->transaction->dirty_bgs_lock); 1171 if (!list_empty(&block_group->io_list)) { 1172 list_del_init(&block_group->io_list); 1173 1174 WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); 1175 1176 spin_unlock(&trans->transaction->dirty_bgs_lock); 1177 btrfs_wait_cache_io(trans, block_group, path); 1178 btrfs_put_block_group(block_group); 1179 spin_lock(&trans->transaction->dirty_bgs_lock); 1180 } 1181 1182 if (!list_empty(&block_group->dirty_list)) { 1183 list_del_init(&block_group->dirty_list); 1184 remove_rsv = true; 1185 btrfs_put_block_group(block_group); 1186 } 1187 spin_unlock(&trans->transaction->dirty_bgs_lock); 1188 mutex_unlock(&trans->transaction->cache_write_mutex); 1189 1190 ret = btrfs_remove_free_space_inode(trans, inode, block_group); 1191 if (unlikely(ret)) { 1192 btrfs_abort_transaction(trans, ret); 1193 goto out; 1194 } 1195 1196 write_lock(&fs_info->block_group_cache_lock); 1197 rb_erase_cached(&block_group->cache_node, 1198 &fs_info->block_group_cache_tree); 1199 RB_CLEAR_NODE(&block_group->cache_node); 1200 1201 /* Once for the block groups rbtree */ 1202 btrfs_put_block_group(block_group); 1203 1204 write_unlock(&fs_info->block_group_cache_lock); 1205 1206 down_write(&block_group->space_info->groups_sem); 1207 /* 1208 * we must use list_del_init so people can check to see if they 1209 * are still on the list after taking the semaphore 1210 */ 1211 list_del_init(&block_group->list); 1212 if (list_empty(&block_group->space_info->block_groups[index])) { 1213 kobj = block_group->space_info->block_group_kobjs[index]; 1214 block_group->space_info->block_group_kobjs[index] = NULL; 1215 clear_avail_alloc_bits(fs_info, block_group->flags); 1216 } 1217 up_write(&block_group->space_info->groups_sem); 1218 clear_incompat_bg_bits(fs_info, block_group->flags); 1219 if (kobj) { 1220 kobject_del(kobj); 1221 kobject_put(kobj); 1222 } 1223 1224 if (block_group->cached == BTRFS_CACHE_STARTED) 1225 btrfs_wait_block_group_cache_done(block_group); 1226 1227 write_lock(&fs_info->block_group_cache_lock); 1228 caching_ctl = btrfs_get_caching_control(block_group); 1229 if (!caching_ctl) { 1230 struct btrfs_caching_control *ctl; 1231 1232 list_for_each_entry(ctl, &fs_info->caching_block_groups, list) { 1233 if (ctl->block_group == block_group) { 1234 caching_ctl = ctl; 1235 refcount_inc(&caching_ctl->count); 1236 break; 1237 } 1238 } 1239 } 1240 if (caching_ctl) 1241 list_del_init(&caching_ctl->list); 1242 write_unlock(&fs_info->block_group_cache_lock); 1243 1244 if (caching_ctl) { 1245 /* Once for the caching bgs list and once for us. */ 1246 btrfs_put_caching_control(caching_ctl); 1247 btrfs_put_caching_control(caching_ctl); 1248 } 1249 1250 spin_lock(&trans->transaction->dirty_bgs_lock); 1251 WARN_ON(!list_empty(&block_group->dirty_list)); 1252 WARN_ON(!list_empty(&block_group->io_list)); 1253 spin_unlock(&trans->transaction->dirty_bgs_lock); 1254 1255 btrfs_remove_free_space_cache(block_group); 1256 1257 spin_lock(&block_group->space_info->lock); 1258 list_del_init(&block_group->ro_list); 1259 spin_unlock(&block_group->space_info->lock); 1260 1261 if (!(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) 1262 btrfs_remove_bg_from_sinfo(block_group); 1263 1264 /* 1265 * Remove the free space for the block group from the free space tree 1266 * and the block group's item from the extent tree before marking the 1267 * block group as removed. This is to prevent races with tasks that 1268 * freeze and unfreeze a block group, this task and another task 1269 * allocating a new block group - the unfreeze task ends up removing 1270 * the block group's extent map before the task calling this function 1271 * deletes the block group item from the extent tree, allowing for 1272 * another task to attempt to create another block group with the same 1273 * item key (and failing with -EEXIST and a transaction abort). 1274 * 1275 * If the REMAPPED flag has been set the block group's free space 1276 * has already been removed, so we can skip the call to 1277 * btrfs_remove_block_group_free_space(). 1278 */ 1279 if (!(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) { 1280 ret = btrfs_remove_block_group_free_space(trans, block_group); 1281 if (unlikely(ret)) { 1282 btrfs_abort_transaction(trans, ret); 1283 goto out; 1284 } 1285 } 1286 1287 ret = remove_block_group_item(trans, path, block_group); 1288 if (unlikely(ret < 0)) { 1289 btrfs_abort_transaction(trans, ret); 1290 goto out; 1291 } 1292 1293 spin_lock(&block_group->lock); 1294 /* 1295 * Hitting this WARN means we removed a block group with an unwritten 1296 * region. It will cause "unable to find chunk map for logical" errors. 1297 */ 1298 if (WARN_ON(has_unwritten_metadata(block_group))) 1299 btrfs_warn(fs_info, 1300 "block group %llu is removed before metadata write out", 1301 block_group->start); 1302 1303 set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags); 1304 1305 /* 1306 * At this point trimming or scrub can't start on this block group, 1307 * because we removed the block group from the rbtree 1308 * fs_info->block_group_cache_tree so no one can't find it anymore and 1309 * even if someone already got this block group before we removed it 1310 * from the rbtree, they have already incremented block_group->frozen - 1311 * if they didn't, for the trimming case they won't find any free space 1312 * entries because we already removed them all when we called 1313 * btrfs_remove_free_space_cache(). 1314 * 1315 * And we must not remove the chunk map from the fs_info->mapping_tree 1316 * to prevent the same logical address range and physical device space 1317 * ranges from being reused for a new block group. This is needed to 1318 * avoid races with trimming and scrub. 1319 * 1320 * An fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is 1321 * completely transactionless, so while it is trimming a range the 1322 * currently running transaction might finish and a new one start, 1323 * allowing for new block groups to be created that can reuse the same 1324 * physical device locations unless we take this special care. 1325 * 1326 * There may also be an implicit trim operation if the file system 1327 * is mounted with -odiscard. The same protections must remain 1328 * in place until the extents have been discarded completely when 1329 * the transaction commit has completed. 1330 */ 1331 remove_map = (atomic_read(&block_group->frozen) == 0); 1332 spin_unlock(&block_group->lock); 1333 1334 if (remove_map) 1335 btrfs_remove_chunk_map(fs_info, map); 1336 1337 out: 1338 /* Once for the lookup reference */ 1339 btrfs_put_block_group(block_group); 1340 if (remove_rsv) 1341 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info); 1342 return ret; 1343 } 1344 1345 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( 1346 struct btrfs_fs_info *fs_info, const u64 chunk_offset) 1347 { 1348 struct btrfs_root *root = btrfs_block_group_root(fs_info); 1349 struct btrfs_chunk_map *map; 1350 unsigned int num_items; 1351 1352 map = btrfs_find_chunk_map(fs_info, chunk_offset, 1); 1353 ASSERT(map != NULL); 1354 ASSERT(map->start == chunk_offset); 1355 1356 /* 1357 * We need to reserve 3 + N units from the metadata space info in order 1358 * to remove a block group (done at btrfs_remove_chunk() and at 1359 * btrfs_remove_block_group()), which are used for: 1360 * 1361 * 1 unit for adding the free space inode's orphan (located in the tree 1362 * of tree roots). 1363 * 1 unit for deleting the block group item (located in the extent 1364 * tree). 1365 * 1 unit for deleting the free space item (located in tree of tree 1366 * roots). 1367 * N units for deleting N device extent items corresponding to each 1368 * stripe (located in the device tree). 1369 * 1370 * In order to remove a block group we also need to reserve units in the 1371 * system space info in order to update the chunk tree (update one or 1372 * more device items and remove one chunk item), but this is done at 1373 * btrfs_remove_chunk() through a call to check_system_chunk(). 1374 */ 1375 num_items = 3 + map->num_stripes; 1376 btrfs_free_chunk_map(map); 1377 1378 return btrfs_start_transaction_fallback_global_rsv(root, num_items); 1379 } 1380 1381 /* 1382 * Mark block group @cache read-only, so later write won't happen to block 1383 * group @cache. 1384 * 1385 * If @force is not set, this function will only mark the block group readonly 1386 * if we have enough free space (1M) in other metadata/system block groups. 1387 * If @force is not set, this function will mark the block group readonly 1388 * without checking free space. 1389 * 1390 * NOTE: This function doesn't care if other block groups can contain all the 1391 * data in this block group. That check should be done by relocation routine, 1392 * not this function. 1393 */ 1394 static int inc_block_group_ro(struct btrfs_block_group *cache, bool force) 1395 { 1396 struct btrfs_space_info *sinfo = cache->space_info; 1397 u64 num_bytes; 1398 int ret = -ENOSPC; 1399 1400 spin_lock(&sinfo->lock); 1401 spin_lock(&cache->lock); 1402 1403 if (cache->swap_extents) { 1404 ret = -ETXTBSY; 1405 goto out; 1406 } 1407 1408 if (cache->ro) { 1409 cache->ro++; 1410 ret = 0; 1411 goto out; 1412 } 1413 1414 num_bytes = btrfs_block_group_available_space(cache); 1415 1416 /* 1417 * Data never overcommits, even in mixed mode, so do just the straight 1418 * check of left over space in how much we have allocated. 1419 */ 1420 if (force) { 1421 ret = 0; 1422 } else if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) { 1423 u64 sinfo_used = btrfs_space_info_used(sinfo, true); 1424 1425 /* 1426 * Here we make sure if we mark this bg RO, we still have enough 1427 * free space as buffer. 1428 */ 1429 if (sinfo_used + num_bytes <= sinfo->total_bytes) 1430 ret = 0; 1431 } else { 1432 /* 1433 * We overcommit metadata, so we need to do the 1434 * btrfs_can_overcommit check here, and we need to pass in 1435 * BTRFS_RESERVE_NO_FLUSH to give ourselves the most amount of 1436 * leeway to allow us to mark this block group as read only. 1437 */ 1438 if (btrfs_can_overcommit(sinfo, num_bytes, BTRFS_RESERVE_NO_FLUSH)) 1439 ret = 0; 1440 } 1441 1442 if (!ret) { 1443 sinfo->bytes_readonly += num_bytes; 1444 if (btrfs_is_zoned(cache->fs_info)) { 1445 /* Migrate zone_unusable bytes to readonly */ 1446 sinfo->bytes_readonly += cache->zone_unusable; 1447 btrfs_space_info_update_bytes_zone_unusable(sinfo, -cache->zone_unusable); 1448 cache->zone_unusable = 0; 1449 } 1450 cache->ro++; 1451 list_add_tail(&cache->ro_list, &sinfo->ro_bgs); 1452 } 1453 out: 1454 spin_unlock(&cache->lock); 1455 spin_unlock(&sinfo->lock); 1456 if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { 1457 btrfs_info(cache->fs_info, 1458 "unable to make block group %llu ro", cache->start); 1459 btrfs_dump_space_info(cache->space_info, 0, false); 1460 } 1461 return ret; 1462 } 1463 1464 static bool clean_pinned_extents(struct btrfs_trans_handle *trans, 1465 const struct btrfs_block_group *bg) 1466 { 1467 struct btrfs_fs_info *fs_info = trans->fs_info; 1468 struct btrfs_transaction *prev_trans = NULL; 1469 const u64 start = bg->start; 1470 const u64 end = start + bg->length - 1; 1471 int ret; 1472 1473 spin_lock(&fs_info->trans_lock); 1474 if (!list_is_first(&trans->transaction->list, &fs_info->trans_list)) { 1475 prev_trans = list_prev_entry(trans->transaction, list); 1476 refcount_inc(&prev_trans->use_count); 1477 } 1478 spin_unlock(&fs_info->trans_lock); 1479 1480 /* 1481 * Hold the unused_bg_unpin_mutex lock to avoid racing with 1482 * btrfs_finish_extent_commit(). If we are at transaction N, another 1483 * task might be running finish_extent_commit() for the previous 1484 * transaction N - 1, and have seen a range belonging to the block 1485 * group in pinned_extents before we were able to clear the whole block 1486 * group range from pinned_extents. This means that task can lookup for 1487 * the block group after we unpinned it from pinned_extents and removed 1488 * it, leading to an error at unpin_extent_range(). 1489 */ 1490 mutex_lock(&fs_info->unused_bg_unpin_mutex); 1491 if (prev_trans) { 1492 ret = btrfs_clear_extent_bit(&prev_trans->pinned_extents, start, end, 1493 EXTENT_DIRTY, NULL); 1494 if (ret) 1495 goto out; 1496 } 1497 1498 ret = btrfs_clear_extent_bit(&trans->transaction->pinned_extents, start, end, 1499 EXTENT_DIRTY, NULL); 1500 out: 1501 mutex_unlock(&fs_info->unused_bg_unpin_mutex); 1502 if (prev_trans) 1503 btrfs_put_transaction(prev_trans); 1504 1505 return ret == 0; 1506 } 1507 1508 /* 1509 * Link the block_group to a list via bg_list. 1510 * 1511 * @bg: The block_group to link to the list. 1512 * @list: The list to link it to. 1513 * 1514 * Use this rather than list_add_tail() directly to ensure proper respect 1515 * to locking and refcounting. 1516 * 1517 * Returns: true if the bg was linked with a refcount bump and false otherwise. 1518 */ 1519 static bool btrfs_link_bg_list(struct btrfs_block_group *bg, struct list_head *list) 1520 { 1521 struct btrfs_fs_info *fs_info = bg->fs_info; 1522 bool added = false; 1523 1524 spin_lock(&fs_info->unused_bgs_lock); 1525 if (list_empty(&bg->bg_list)) { 1526 btrfs_get_block_group(bg); 1527 list_add_tail(&bg->bg_list, list); 1528 added = true; 1529 } 1530 spin_unlock(&fs_info->unused_bgs_lock); 1531 return added; 1532 } 1533 1534 /* 1535 * Process the unused_bgs list and remove any that don't have any allocated 1536 * space inside of them. 1537 */ 1538 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) 1539 { 1540 LIST_HEAD(retry_list); 1541 struct btrfs_block_group *block_group; 1542 struct btrfs_space_info *space_info; 1543 struct btrfs_trans_handle *trans; 1544 const bool async_trim_enabled = btrfs_test_opt(fs_info, DISCARD_ASYNC); 1545 int ret = 0; 1546 1547 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 1548 return; 1549 1550 if (btrfs_fs_closing(fs_info)) 1551 return; 1552 1553 /* 1554 * Long running balances can keep us blocked here for eternity, so 1555 * simply skip deletion if we're unable to get the mutex. 1556 */ 1557 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) 1558 return; 1559 1560 spin_lock(&fs_info->unused_bgs_lock); 1561 while (!list_empty(&fs_info->unused_bgs)) { 1562 u64 used; 1563 int trimming; 1564 1565 block_group = list_first_entry(&fs_info->unused_bgs, 1566 struct btrfs_block_group, 1567 bg_list); 1568 list_del_init(&block_group->bg_list); 1569 1570 space_info = block_group->space_info; 1571 1572 if (ret || btrfs_mixed_space_info(space_info)) { 1573 btrfs_put_block_group(block_group); 1574 continue; 1575 } 1576 spin_unlock(&fs_info->unused_bgs_lock); 1577 1578 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); 1579 1580 /* Don't want to race with allocators so take the groups_sem */ 1581 down_write(&space_info->groups_sem); 1582 1583 /* 1584 * Async discard moves the final block group discard to be prior 1585 * to the unused_bgs code path. Therefore, if it's not fully 1586 * trimmed, punt it back to the async discard lists. 1587 */ 1588 if (btrfs_test_opt(fs_info, DISCARD_ASYNC) && 1589 !btrfs_is_free_space_trimmed(block_group)) { 1590 trace_btrfs_skip_unused_block_group(block_group); 1591 up_write(&space_info->groups_sem); 1592 /* Requeue if we failed because of async discard */ 1593 btrfs_discard_queue_work(&fs_info->discard_ctl, 1594 block_group); 1595 goto next; 1596 } 1597 1598 spin_lock(&space_info->lock); 1599 spin_lock(&block_group->lock); 1600 if (btrfs_is_block_group_used(block_group) || 1601 (block_group->ro && !(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) || 1602 list_is_singular(&block_group->list) || 1603 test_bit(BLOCK_GROUP_FLAG_FULLY_REMAPPED, &block_group->runtime_flags)) { 1604 /* 1605 * We want to bail if we made new allocations or have 1606 * outstanding allocations in this block group. We do 1607 * the ro check in case balance is currently acting on 1608 * this block group. 1609 * 1610 * Also bail out if this is the only block group for its 1611 * type, because otherwise we would lose profile 1612 * information from fs_info->avail_*_alloc_bits and the 1613 * next block group of this type would be created with a 1614 * "single" profile (even if we're in a raid fs) because 1615 * fs_info->avail_*_alloc_bits would be 0. 1616 */ 1617 trace_btrfs_skip_unused_block_group(block_group); 1618 spin_unlock(&block_group->lock); 1619 spin_unlock(&space_info->lock); 1620 up_write(&space_info->groups_sem); 1621 goto next; 1622 } 1623 1624 /* 1625 * The block group may be unused but there may be space reserved 1626 * accounting with the existence of that block group, that is, 1627 * space_info->bytes_may_use was incremented by a task but no 1628 * space was yet allocated from the block group by the task. 1629 * That space may or may not be allocated, as we are generally 1630 * pessimistic about space reservation for metadata as well as 1631 * for data when using compression (as we reserve space based on 1632 * the worst case, when data can't be compressed, and before 1633 * actually attempting compression, before starting writeback). 1634 * 1635 * So check if the total space of the space_info minus the size 1636 * of this block group is less than the used space of the 1637 * space_info - if that's the case, then it means we have tasks 1638 * that might be relying on the block group in order to allocate 1639 * extents, and add back the block group to the unused list when 1640 * we finish, so that we retry later in case no tasks ended up 1641 * needing to allocate extents from the block group. 1642 */ 1643 used = btrfs_space_info_used(space_info, true); 1644 if (((space_info->total_bytes - block_group->length < used && 1645 block_group->zone_unusable < block_group->length) || 1646 has_unwritten_metadata(block_group)) && 1647 !(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) { 1648 /* 1649 * Add a reference for the list, compensate for the ref 1650 * drop under the "next" label for the 1651 * fs_info->unused_bgs list. 1652 */ 1653 btrfs_link_bg_list(block_group, &retry_list); 1654 1655 trace_btrfs_skip_unused_block_group(block_group); 1656 spin_unlock(&block_group->lock); 1657 spin_unlock(&space_info->lock); 1658 up_write(&space_info->groups_sem); 1659 goto next; 1660 } 1661 1662 spin_unlock(&block_group->lock); 1663 spin_unlock(&space_info->lock); 1664 1665 /* We don't want to force the issue, only flip if it's ok. */ 1666 ret = inc_block_group_ro(block_group, 0); 1667 up_write(&space_info->groups_sem); 1668 if (ret < 0) { 1669 ret = 0; 1670 goto next; 1671 } 1672 1673 ret = btrfs_zone_finish(block_group); 1674 if (ret < 0) { 1675 btrfs_dec_block_group_ro(block_group); 1676 if (ret == -EAGAIN) { 1677 btrfs_link_bg_list(block_group, &retry_list); 1678 ret = 0; 1679 } 1680 goto next; 1681 } 1682 1683 /* 1684 * Want to do this before we do anything else so we can recover 1685 * properly if we fail to join the transaction. 1686 */ 1687 trans = btrfs_start_trans_remove_block_group(fs_info, 1688 block_group->start); 1689 if (IS_ERR(trans)) { 1690 btrfs_dec_block_group_ro(block_group); 1691 ret = PTR_ERR(trans); 1692 goto next; 1693 } 1694 1695 /* 1696 * We could have pending pinned extents for this block group, 1697 * just delete them, we don't care about them anymore. 1698 */ 1699 if (!clean_pinned_extents(trans, block_group)) { 1700 btrfs_dec_block_group_ro(block_group); 1701 goto end_trans; 1702 } 1703 1704 /* 1705 * At this point, the block_group is read only and should fail 1706 * new allocations. However, btrfs_finish_extent_commit() can 1707 * cause this block_group to be placed back on the discard 1708 * lists because now the block_group isn't fully discarded. 1709 * Bail here and try again later after discarding everything. 1710 */ 1711 spin_lock(&fs_info->discard_ctl.lock); 1712 if (!list_empty(&block_group->discard_list)) { 1713 spin_unlock(&fs_info->discard_ctl.lock); 1714 btrfs_dec_block_group_ro(block_group); 1715 btrfs_discard_queue_work(&fs_info->discard_ctl, 1716 block_group); 1717 goto end_trans; 1718 } 1719 spin_unlock(&fs_info->discard_ctl.lock); 1720 1721 /* Reset pinned so btrfs_put_block_group doesn't complain */ 1722 spin_lock(&space_info->lock); 1723 spin_lock(&block_group->lock); 1724 1725 btrfs_space_info_update_bytes_pinned(space_info, -block_group->pinned); 1726 space_info->bytes_readonly += block_group->pinned; 1727 block_group->pinned = 0; 1728 1729 spin_unlock(&block_group->lock); 1730 spin_unlock(&space_info->lock); 1731 1732 /* 1733 * The normal path here is an unused block group is passed here, 1734 * then trimming is handled in the transaction commit path. 1735 * Async discard interposes before this to do the trimming 1736 * before coming down the unused block group path as trimming 1737 * will no longer be done later in the transaction commit path. 1738 */ 1739 if (!async_trim_enabled && btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1740 goto flip_async; 1741 1742 /* 1743 * DISCARD can flip during remount. On zoned filesystems, we 1744 * need to reset sequential-required zones. 1745 */ 1746 trimming = btrfs_test_opt(fs_info, DISCARD_SYNC) || 1747 btrfs_is_zoned(fs_info); 1748 1749 /* Implicit trim during transaction commit. */ 1750 if (trimming) 1751 btrfs_freeze_block_group(block_group); 1752 1753 /* 1754 * Btrfs_remove_chunk will abort the transaction if things go 1755 * horribly wrong. 1756 */ 1757 ret = btrfs_remove_chunk(trans, block_group->start); 1758 1759 if (ret) { 1760 if (trimming) 1761 btrfs_unfreeze_block_group(block_group); 1762 goto end_trans; 1763 } 1764 1765 /* 1766 * If we're not mounted with -odiscard, we can just forget 1767 * about this block group. Otherwise we'll need to wait 1768 * until transaction commit to do the actual discard. 1769 */ 1770 if (trimming) { 1771 spin_lock(&fs_info->unused_bgs_lock); 1772 /* 1773 * A concurrent scrub might have added us to the list 1774 * fs_info->unused_bgs, so use a list_move operation 1775 * to add the block group to the deleted_bgs list. 1776 */ 1777 list_move(&block_group->bg_list, 1778 &trans->transaction->deleted_bgs); 1779 spin_unlock(&fs_info->unused_bgs_lock); 1780 btrfs_get_block_group(block_group); 1781 } 1782 end_trans: 1783 btrfs_end_transaction(trans); 1784 next: 1785 btrfs_put_block_group(block_group); 1786 spin_lock(&fs_info->unused_bgs_lock); 1787 } 1788 list_splice_tail(&retry_list, &fs_info->unused_bgs); 1789 spin_unlock(&fs_info->unused_bgs_lock); 1790 mutex_unlock(&fs_info->reclaim_bgs_lock); 1791 return; 1792 1793 flip_async: 1794 btrfs_end_transaction(trans); 1795 spin_lock(&fs_info->unused_bgs_lock); 1796 list_splice_tail(&retry_list, &fs_info->unused_bgs); 1797 spin_unlock(&fs_info->unused_bgs_lock); 1798 mutex_unlock(&fs_info->reclaim_bgs_lock); 1799 btrfs_put_block_group(block_group); 1800 btrfs_discard_punt_unused_bgs_list(fs_info); 1801 } 1802 1803 void btrfs_mark_bg_unused(struct btrfs_block_group *bg) 1804 { 1805 struct btrfs_fs_info *fs_info = bg->fs_info; 1806 1807 spin_lock(&fs_info->unused_bgs_lock); 1808 if (list_empty(&bg->bg_list)) { 1809 btrfs_get_block_group(bg); 1810 trace_btrfs_add_unused_block_group(bg); 1811 list_add_tail(&bg->bg_list, &fs_info->unused_bgs); 1812 } else if (bg->flags & BTRFS_BLOCK_GROUP_REMAPPED && 1813 bg->identity_remap_count == 0) { 1814 /* Leave fully remapped block groups on the fully_remapped_bgs list. */ 1815 } else if (!test_bit(BLOCK_GROUP_FLAG_NEW, &bg->runtime_flags)) { 1816 /* Pull out the block group from the reclaim_bgs list. */ 1817 trace_btrfs_add_unused_block_group(bg); 1818 list_move_tail(&bg->bg_list, &fs_info->unused_bgs); 1819 } 1820 spin_unlock(&fs_info->unused_bgs_lock); 1821 } 1822 1823 /* 1824 * We want block groups with a low number of used bytes to be in the beginning 1825 * of the list, so they will get reclaimed first. 1826 */ 1827 static int reclaim_bgs_cmp(void *unused, const struct list_head *a, 1828 const struct list_head *b) 1829 { 1830 const struct btrfs_block_group *bg1, *bg2; 1831 1832 bg1 = list_entry(a, struct btrfs_block_group, bg_list); 1833 bg2 = list_entry(b, struct btrfs_block_group, bg_list); 1834 1835 /* 1836 * Some other task may be updating the ->used field concurrently, but it 1837 * is not serious if we get a stale value or load/store tearing issues, 1838 * as sorting the list of block groups to reclaim is not critical and an 1839 * occasional imperfect order is ok. So silence KCSAN and avoid the 1840 * overhead of locking or any other synchronization. 1841 */ 1842 return data_race(bg1->used > bg2->used); 1843 } 1844 1845 static inline bool btrfs_should_reclaim(const struct btrfs_fs_info *fs_info) 1846 { 1847 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) 1848 return false; 1849 1850 if (btrfs_fs_closing(fs_info)) 1851 return false; 1852 1853 if (btrfs_is_zoned(fs_info)) 1854 return btrfs_zoned_should_reclaim(fs_info); 1855 return true; 1856 } 1857 1858 static bool should_reclaim_block_group(const struct btrfs_block_group *bg, u64 bytes_freed) 1859 { 1860 const int thresh_pct = btrfs_calc_reclaim_threshold(bg->space_info); 1861 u64 thresh_bytes = mult_perc(bg->length, thresh_pct); 1862 const u64 new_val = bg->used; 1863 const u64 old_val = new_val + bytes_freed; 1864 1865 if (thresh_bytes == 0) 1866 return false; 1867 1868 /* 1869 * If we were below the threshold before don't reclaim, we are likely a 1870 * brand new block group and we don't want to relocate new block groups. 1871 */ 1872 if (old_val < thresh_bytes) 1873 return false; 1874 if (new_val >= thresh_bytes) 1875 return false; 1876 return true; 1877 } 1878 1879 void btrfs_reclaim_bgs_work(struct work_struct *work) 1880 { 1881 struct btrfs_fs_info *fs_info = 1882 container_of(work, struct btrfs_fs_info, reclaim_bgs_work); 1883 struct btrfs_block_group *bg; 1884 struct btrfs_space_info *space_info; 1885 LIST_HEAD(retry_list); 1886 1887 if (!btrfs_should_reclaim(fs_info)) 1888 return; 1889 1890 guard(super_write)(fs_info->sb); 1891 1892 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) 1893 return; 1894 1895 /* 1896 * Long running balances can keep us blocked here for eternity, so 1897 * simply skip reclaim if we're unable to get the mutex. 1898 */ 1899 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) { 1900 btrfs_exclop_finish(fs_info); 1901 return; 1902 } 1903 1904 spin_lock(&fs_info->unused_bgs_lock); 1905 /* 1906 * Sort happens under lock because we can't simply splice it and sort. 1907 * The block groups might still be in use and reachable via bg_list, 1908 * and their presence in the reclaim_bgs list must be preserved. 1909 */ 1910 list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp); 1911 while (!list_empty(&fs_info->reclaim_bgs)) { 1912 u64 used; 1913 u64 reserved; 1914 u64 old_total; 1915 int ret = 0; 1916 1917 bg = list_first_entry(&fs_info->reclaim_bgs, 1918 struct btrfs_block_group, 1919 bg_list); 1920 list_del_init(&bg->bg_list); 1921 1922 space_info = bg->space_info; 1923 spin_unlock(&fs_info->unused_bgs_lock); 1924 1925 /* Don't race with allocators so take the groups_sem */ 1926 down_write(&space_info->groups_sem); 1927 1928 spin_lock(&space_info->lock); 1929 spin_lock(&bg->lock); 1930 if (bg->reserved || bg->pinned || bg->ro) { 1931 /* 1932 * We want to bail if we made new allocations or have 1933 * outstanding allocations in this block group. We do 1934 * the ro check in case balance is currently acting on 1935 * this block group. 1936 */ 1937 spin_unlock(&bg->lock); 1938 spin_unlock(&space_info->lock); 1939 up_write(&space_info->groups_sem); 1940 goto next; 1941 } 1942 if (bg->used == 0) { 1943 /* 1944 * It is possible that we trigger relocation on a block 1945 * group as its extents are deleted and it first goes 1946 * below the threshold, then shortly after goes empty. 1947 * 1948 * In this case, relocating it does delete it, but has 1949 * some overhead in relocation specific metadata, looking 1950 * for the non-existent extents and running some extra 1951 * transactions, which we can avoid by using one of the 1952 * other mechanisms for dealing with empty block groups. 1953 */ 1954 if (!btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1955 btrfs_mark_bg_unused(bg); 1956 spin_unlock(&bg->lock); 1957 spin_unlock(&space_info->lock); 1958 up_write(&space_info->groups_sem); 1959 goto next; 1960 1961 } 1962 /* 1963 * The block group might no longer meet the reclaim condition by 1964 * the time we get around to reclaiming it, so to avoid 1965 * reclaiming overly full block_groups, skip reclaiming them. 1966 * 1967 * Since the decision making process also depends on the amount 1968 * being freed, pass in a fake giant value to skip that extra 1969 * check, which is more meaningful when adding to the list in 1970 * the first place. 1971 */ 1972 if (!should_reclaim_block_group(bg, bg->length)) { 1973 spin_unlock(&bg->lock); 1974 spin_unlock(&space_info->lock); 1975 up_write(&space_info->groups_sem); 1976 goto next; 1977 } 1978 1979 spin_unlock(&bg->lock); 1980 old_total = space_info->total_bytes; 1981 spin_unlock(&space_info->lock); 1982 1983 /* 1984 * Get out fast, in case we're read-only or unmounting the 1985 * filesystem. It is OK to drop block groups from the list even 1986 * for the read-only case. As we did take the super write lock, 1987 * "mount -o remount,ro" won't happen and read-only filesystem 1988 * means it is forced read-only due to a fatal error. So, it 1989 * never gets back to read-write to let us reclaim again. 1990 */ 1991 if (btrfs_need_cleaner_sleep(fs_info)) { 1992 up_write(&space_info->groups_sem); 1993 goto next; 1994 } 1995 1996 ret = inc_block_group_ro(bg, 0); 1997 up_write(&space_info->groups_sem); 1998 if (ret < 0) 1999 goto next; 2000 2001 /* 2002 * The amount of bytes reclaimed corresponds to the sum of the 2003 * "used" and "reserved" counters. We have set the block group 2004 * to RO above, which prevents reservations from happening but 2005 * we may have existing reservations for which allocation has 2006 * not yet been done - btrfs_update_block_group() was not yet 2007 * called, which is where we will transfer a reserved extent's 2008 * size from the "reserved" counter to the "used" counter - this 2009 * happens when running delayed references. When we relocate the 2010 * chunk below, relocation first flushes delalloc, waits for 2011 * ordered extent completion (which is where we create delayed 2012 * references for data extents) and commits the current 2013 * transaction (which runs delayed references), and only after 2014 * it does the actual work to move extents out of the block 2015 * group. So the reported amount of reclaimed bytes is 2016 * effectively the sum of the 'used' and 'reserved' counters. 2017 */ 2018 spin_lock(&bg->lock); 2019 used = bg->used; 2020 reserved = bg->reserved; 2021 spin_unlock(&bg->lock); 2022 2023 trace_btrfs_reclaim_block_group(bg); 2024 ret = btrfs_relocate_chunk(fs_info, bg->start, false); 2025 if (ret) { 2026 btrfs_dec_block_group_ro(bg); 2027 btrfs_err(fs_info, "error relocating chunk %llu", 2028 bg->start); 2029 used = 0; 2030 reserved = 0; 2031 spin_lock(&space_info->lock); 2032 space_info->reclaim_errors++; 2033 spin_unlock(&space_info->lock); 2034 } 2035 spin_lock(&space_info->lock); 2036 space_info->reclaim_count++; 2037 space_info->reclaim_bytes += used; 2038 space_info->reclaim_bytes += reserved; 2039 if (space_info->total_bytes < old_total) 2040 btrfs_set_periodic_reclaim_ready(space_info, true); 2041 spin_unlock(&space_info->lock); 2042 2043 next: 2044 if (ret && !READ_ONCE(space_info->periodic_reclaim)) 2045 btrfs_link_bg_list(bg, &retry_list); 2046 btrfs_put_block_group(bg); 2047 2048 mutex_unlock(&fs_info->reclaim_bgs_lock); 2049 /* 2050 * Reclaiming all the block groups in the list can take really 2051 * long. Prioritize cleaning up unused block groups. 2052 */ 2053 btrfs_delete_unused_bgs(fs_info); 2054 /* 2055 * If we are interrupted by a balance, we can just bail out. The 2056 * cleaner thread restart again if necessary. 2057 */ 2058 if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) 2059 goto end; 2060 spin_lock(&fs_info->unused_bgs_lock); 2061 } 2062 spin_unlock(&fs_info->unused_bgs_lock); 2063 mutex_unlock(&fs_info->reclaim_bgs_lock); 2064 end: 2065 spin_lock(&fs_info->unused_bgs_lock); 2066 list_splice_tail(&retry_list, &fs_info->reclaim_bgs); 2067 spin_unlock(&fs_info->unused_bgs_lock); 2068 btrfs_exclop_finish(fs_info); 2069 } 2070 2071 void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info) 2072 { 2073 btrfs_reclaim_sweep(fs_info); 2074 spin_lock(&fs_info->unused_bgs_lock); 2075 if (!list_empty(&fs_info->reclaim_bgs)) 2076 queue_work(system_dfl_wq, &fs_info->reclaim_bgs_work); 2077 spin_unlock(&fs_info->unused_bgs_lock); 2078 } 2079 2080 void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg) 2081 { 2082 struct btrfs_fs_info *fs_info = bg->fs_info; 2083 2084 if (btrfs_link_bg_list(bg, &fs_info->reclaim_bgs)) 2085 trace_btrfs_add_reclaim_block_group(bg); 2086 } 2087 2088 static int read_bg_from_eb(struct btrfs_fs_info *fs_info, const struct btrfs_key *key, 2089 const struct btrfs_path *path) 2090 { 2091 struct btrfs_chunk_map *map; 2092 struct btrfs_block_group_item bg; 2093 struct extent_buffer *leaf; 2094 int slot; 2095 u64 flags; 2096 int ret = 0; 2097 2098 slot = path->slots[0]; 2099 leaf = path->nodes[0]; 2100 2101 map = btrfs_find_chunk_map(fs_info, key->objectid, key->offset); 2102 if (!map) { 2103 btrfs_err(fs_info, 2104 "logical %llu len %llu found bg but no related chunk", 2105 key->objectid, key->offset); 2106 return -ENOENT; 2107 } 2108 2109 if (unlikely(map->start != key->objectid || map->chunk_len != key->offset)) { 2110 btrfs_err(fs_info, 2111 "block group %llu len %llu mismatch with chunk %llu len %llu", 2112 key->objectid, key->offset, map->start, map->chunk_len); 2113 ret = -EUCLEAN; 2114 goto out_free_map; 2115 } 2116 2117 read_extent_buffer(leaf, &bg, btrfs_item_ptr_offset(leaf, slot), 2118 sizeof(bg)); 2119 flags = btrfs_stack_block_group_flags(&bg) & 2120 BTRFS_BLOCK_GROUP_TYPE_MASK; 2121 2122 if (unlikely(flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK))) { 2123 btrfs_err(fs_info, 2124 "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", 2125 key->objectid, key->offset, flags, 2126 (BTRFS_BLOCK_GROUP_TYPE_MASK & map->type)); 2127 ret = -EUCLEAN; 2128 } 2129 2130 out_free_map: 2131 btrfs_free_chunk_map(map); 2132 return ret; 2133 } 2134 2135 static int find_first_block_group(struct btrfs_fs_info *fs_info, 2136 struct btrfs_path *path, 2137 const struct btrfs_key *key) 2138 { 2139 struct btrfs_root *root = btrfs_block_group_root(fs_info); 2140 int ret; 2141 struct btrfs_key found_key; 2142 2143 btrfs_for_each_slot(root, key, &found_key, path, ret) { 2144 if (found_key.objectid >= key->objectid && 2145 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { 2146 return read_bg_from_eb(fs_info, &found_key, path); 2147 } 2148 } 2149 return ret; 2150 } 2151 2152 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 2153 { 2154 u64 extra_flags = chunk_to_extended(flags) & 2155 BTRFS_EXTENDED_PROFILE_MASK; 2156 2157 write_seqlock(&fs_info->profiles_lock); 2158 if (flags & BTRFS_BLOCK_GROUP_DATA) 2159 fs_info->avail_data_alloc_bits |= extra_flags; 2160 if (flags & BTRFS_BLOCK_GROUP_METADATA) 2161 fs_info->avail_metadata_alloc_bits |= extra_flags; 2162 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 2163 fs_info->avail_system_alloc_bits |= extra_flags; 2164 write_sequnlock(&fs_info->profiles_lock); 2165 } 2166 2167 /* 2168 * Map a physical disk address to a list of logical addresses. 2169 * 2170 * @fs_info: the filesystem 2171 * @chunk_start: logical address of block group 2172 * @physical: physical address to map to logical addresses 2173 * @logical: return array of logical addresses which map to @physical 2174 * @naddrs: length of @logical 2175 * @stripe_len: size of IO stripe for the given block group 2176 * 2177 * Maps a particular @physical disk address to a list of @logical addresses. 2178 * Used primarily to exclude those portions of a block group that contain super 2179 * block copies. 2180 */ 2181 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, 2182 u64 physical, u64 **logical, int *naddrs, int *stripe_len) 2183 { 2184 struct btrfs_chunk_map *map; 2185 u64 *buf; 2186 u64 bytenr; 2187 u64 data_stripe_length; 2188 u64 io_stripe_size; 2189 int i, nr = 0; 2190 int ret = 0; 2191 2192 map = btrfs_get_chunk_map(fs_info, chunk_start, 1); 2193 if (IS_ERR(map)) 2194 return -EIO; 2195 2196 data_stripe_length = map->stripe_size; 2197 io_stripe_size = BTRFS_STRIPE_LEN; 2198 chunk_start = map->start; 2199 2200 /* For RAID5/6 adjust to a full IO stripe length */ 2201 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) 2202 io_stripe_size = btrfs_stripe_nr_to_offset(nr_data_stripes(map)); 2203 2204 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); 2205 if (!buf) { 2206 ret = -ENOMEM; 2207 goto out; 2208 } 2209 2210 for (i = 0; i < map->num_stripes; i++) { 2211 bool already_inserted = false; 2212 u32 stripe_nr; 2213 u32 offset; 2214 int j; 2215 2216 if (!in_range(physical, map->stripes[i].physical, 2217 data_stripe_length)) 2218 continue; 2219 2220 stripe_nr = (physical - map->stripes[i].physical) >> 2221 BTRFS_STRIPE_LEN_SHIFT; 2222 offset = (physical - map->stripes[i].physical) & 2223 BTRFS_STRIPE_LEN_MASK; 2224 2225 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | 2226 BTRFS_BLOCK_GROUP_RAID10)) 2227 stripe_nr = div_u64(stripe_nr * map->num_stripes + i, 2228 map->sub_stripes); 2229 /* 2230 * The remaining case would be for RAID56, multiply by 2231 * nr_data_stripes(). Alternatively, just use rmap_len below 2232 * instead of map->stripe_len 2233 */ 2234 bytenr = chunk_start + stripe_nr * io_stripe_size + offset; 2235 2236 /* Ensure we don't add duplicate addresses */ 2237 for (j = 0; j < nr; j++) { 2238 if (buf[j] == bytenr) { 2239 already_inserted = true; 2240 break; 2241 } 2242 } 2243 2244 if (!already_inserted) 2245 buf[nr++] = bytenr; 2246 } 2247 2248 *logical = buf; 2249 *naddrs = nr; 2250 *stripe_len = io_stripe_size; 2251 out: 2252 btrfs_free_chunk_map(map); 2253 return ret; 2254 } 2255 2256 static int exclude_super_stripes(struct btrfs_block_group *cache) 2257 { 2258 struct btrfs_fs_info *fs_info = cache->fs_info; 2259 const bool zoned = btrfs_is_zoned(fs_info); 2260 u64 bytenr; 2261 u64 *logical; 2262 int stripe_len; 2263 int i, nr, ret; 2264 2265 if (cache->start < BTRFS_SUPER_INFO_OFFSET) { 2266 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; 2267 cache->bytes_super += stripe_len; 2268 ret = btrfs_set_extent_bit(&fs_info->excluded_extents, cache->start, 2269 cache->start + stripe_len - 1, 2270 EXTENT_DIRTY, NULL); 2271 if (ret) 2272 return ret; 2273 } 2274 2275 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 2276 bytenr = btrfs_sb_offset(i); 2277 ret = btrfs_rmap_block(fs_info, cache->start, 2278 bytenr, &logical, &nr, &stripe_len); 2279 if (ret) 2280 return ret; 2281 2282 /* Shouldn't have super stripes in sequential zones */ 2283 if (unlikely(zoned && nr)) { 2284 kfree(logical); 2285 btrfs_err(fs_info, 2286 "zoned: block group %llu must not contain super block", 2287 cache->start); 2288 return -EUCLEAN; 2289 } 2290 2291 while (nr--) { 2292 u64 len = min_t(u64, stripe_len, 2293 btrfs_block_group_end(cache) - logical[nr]); 2294 2295 cache->bytes_super += len; 2296 ret = btrfs_set_extent_bit(&fs_info->excluded_extents, 2297 logical[nr], logical[nr] + len - 1, 2298 EXTENT_DIRTY, NULL); 2299 if (ret) { 2300 kfree(logical); 2301 return ret; 2302 } 2303 } 2304 2305 kfree(logical); 2306 } 2307 return 0; 2308 } 2309 2310 static struct btrfs_block_group *btrfs_create_block_group( 2311 struct btrfs_fs_info *fs_info, u64 start) 2312 { 2313 struct btrfs_block_group *cache; 2314 2315 cache = kzalloc(sizeof(*cache), GFP_NOFS); 2316 if (!cache) 2317 return NULL; 2318 2319 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), 2320 GFP_NOFS); 2321 if (!cache->free_space_ctl) { 2322 kfree(cache); 2323 return NULL; 2324 } 2325 2326 cache->start = start; 2327 2328 cache->fs_info = fs_info; 2329 cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); 2330 2331 cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED; 2332 2333 refcount_set(&cache->refs, 1); 2334 spin_lock_init(&cache->lock); 2335 init_rwsem(&cache->data_rwsem); 2336 INIT_LIST_HEAD(&cache->list); 2337 INIT_LIST_HEAD(&cache->cluster_list); 2338 INIT_LIST_HEAD(&cache->bg_list); 2339 INIT_LIST_HEAD(&cache->ro_list); 2340 INIT_LIST_HEAD(&cache->discard_list); 2341 INIT_LIST_HEAD(&cache->dirty_list); 2342 INIT_LIST_HEAD(&cache->io_list); 2343 INIT_LIST_HEAD(&cache->active_bg_list); 2344 btrfs_init_free_space_ctl(cache, cache->free_space_ctl); 2345 atomic_set(&cache->frozen, 0); 2346 mutex_init(&cache->free_space_lock); 2347 2348 return cache; 2349 } 2350 2351 /* 2352 * Iterate all chunks and verify that each of them has the corresponding block 2353 * group 2354 */ 2355 static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) 2356 { 2357 u64 start = 0; 2358 int ret = 0; 2359 2360 while (1) { 2361 struct btrfs_chunk_map *map; 2362 struct btrfs_block_group *bg; 2363 2364 /* 2365 * btrfs_find_chunk_map() will return the first chunk map 2366 * intersecting the range, so setting @length to 1 is enough to 2367 * get the first chunk. 2368 */ 2369 map = btrfs_find_chunk_map(fs_info, start, 1); 2370 if (!map) 2371 break; 2372 2373 bg = btrfs_lookup_block_group(fs_info, map->start); 2374 if (unlikely(!bg)) { 2375 btrfs_err(fs_info, 2376 "chunk start=%llu len=%llu doesn't have corresponding block group", 2377 map->start, map->chunk_len); 2378 ret = -EUCLEAN; 2379 btrfs_free_chunk_map(map); 2380 break; 2381 } 2382 if (unlikely(bg->start != map->start || bg->length != map->chunk_len || 2383 (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != 2384 (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK))) { 2385 btrfs_err(fs_info, 2386 "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", 2387 map->start, map->chunk_len, 2388 map->type & BTRFS_BLOCK_GROUP_TYPE_MASK, 2389 bg->start, bg->length, 2390 bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); 2391 ret = -EUCLEAN; 2392 btrfs_free_chunk_map(map); 2393 btrfs_put_block_group(bg); 2394 break; 2395 } 2396 start = map->start + map->chunk_len; 2397 btrfs_free_chunk_map(map); 2398 btrfs_put_block_group(bg); 2399 } 2400 return ret; 2401 } 2402 2403 static int read_one_block_group(struct btrfs_fs_info *info, 2404 struct btrfs_block_group_item_v2 *bgi, 2405 const struct btrfs_key *key, 2406 int need_clear) 2407 { 2408 struct btrfs_block_group *cache; 2409 const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS); 2410 int ret; 2411 2412 ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); 2413 2414 cache = btrfs_create_block_group(info, key->objectid); 2415 if (!cache) 2416 return -ENOMEM; 2417 2418 cache->length = key->offset; 2419 cache->used = btrfs_stack_block_group_v2_used(bgi); 2420 cache->last_used = cache->used; 2421 cache->flags = btrfs_stack_block_group_v2_flags(bgi); 2422 cache->last_flags = cache->flags; 2423 cache->global_root_id = btrfs_stack_block_group_v2_chunk_objectid(bgi); 2424 cache->space_info = btrfs_find_space_info(info, cache->flags); 2425 cache->remap_bytes = btrfs_stack_block_group_v2_remap_bytes(bgi); 2426 cache->last_remap_bytes = cache->remap_bytes; 2427 cache->identity_remap_count = btrfs_stack_block_group_v2_identity_remap_count(bgi); 2428 cache->last_identity_remap_count = cache->identity_remap_count; 2429 2430 btrfs_set_free_space_tree_thresholds(cache); 2431 2432 if (need_clear) { 2433 /* 2434 * When we mount with old space cache, we need to 2435 * set BTRFS_DC_CLEAR and set dirty flag. 2436 * 2437 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we 2438 * truncate the old free space cache inode and 2439 * setup a new one. 2440 * b) Setting 'dirty flag' makes sure that we flush 2441 * the new space cache info onto disk. 2442 */ 2443 if (btrfs_test_opt(info, SPACE_CACHE)) 2444 cache->disk_cache_state = BTRFS_DC_CLEAR; 2445 } 2446 if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && 2447 (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { 2448 btrfs_err(info, 2449 "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", 2450 cache->start); 2451 ret = -EINVAL; 2452 goto error; 2453 } 2454 2455 ret = btrfs_load_block_group_zone_info(cache, false); 2456 if (ret) { 2457 btrfs_err(info, "zoned: failed to load zone info of bg %llu", 2458 cache->start); 2459 goto error; 2460 } 2461 2462 /* 2463 * We need to exclude the super stripes now so that the space info has 2464 * super bytes accounted for, otherwise we'll think we have more space 2465 * than we actually do. 2466 */ 2467 ret = exclude_super_stripes(cache); 2468 if (ret) { 2469 /* We may have excluded something, so call this just in case. */ 2470 btrfs_free_excluded_extents(cache); 2471 goto error; 2472 } 2473 2474 /* 2475 * For zoned filesystem, space after the allocation offset is the only 2476 * free space for a block group. So, we don't need any caching work. 2477 * btrfs_calc_zone_unusable() will set the amount of free space and 2478 * zone_unusable space. 2479 * 2480 * For regular filesystem, check for two cases, either we are full, and 2481 * therefore don't need to bother with the caching work since we won't 2482 * find any space, or we are empty, and we can just add all the space 2483 * in and be done with it. This saves us _a_lot_ of time, particularly 2484 * in the full case. 2485 */ 2486 if (btrfs_is_zoned(info)) { 2487 btrfs_calc_zone_unusable(cache); 2488 /* Should not have any excluded extents. Just in case, though. */ 2489 btrfs_free_excluded_extents(cache); 2490 } else if (cache->length == cache->used) { 2491 cache->cached = BTRFS_CACHE_FINISHED; 2492 btrfs_free_excluded_extents(cache); 2493 } else if (cache->used == 0 && cache->remap_bytes == 0) { 2494 cache->cached = BTRFS_CACHE_FINISHED; 2495 ret = btrfs_add_new_free_space(cache, cache->start, 2496 btrfs_block_group_end(cache), NULL); 2497 btrfs_free_excluded_extents(cache); 2498 if (ret) 2499 goto error; 2500 } 2501 2502 ret = btrfs_add_block_group_cache(cache); 2503 if (ret) { 2504 btrfs_remove_free_space_cache(cache); 2505 goto error; 2506 } 2507 2508 trace_btrfs_add_block_group(info, cache, 0); 2509 btrfs_add_bg_to_space_info(info, cache); 2510 2511 set_avail_alloc_bits(info, cache->flags); 2512 if (btrfs_chunk_writeable(info, cache->start)) { 2513 if (cache->used == 0 && cache->remap_bytes == 0) { 2514 ASSERT(list_empty(&cache->bg_list)); 2515 if (btrfs_test_opt(info, DISCARD_ASYNC)) 2516 btrfs_discard_queue_work(&info->discard_ctl, cache); 2517 else 2518 btrfs_mark_bg_unused(cache); 2519 } 2520 } else { 2521 inc_block_group_ro(cache, 1); 2522 } 2523 2524 return 0; 2525 error: 2526 btrfs_put_block_group(cache); 2527 return ret; 2528 } 2529 2530 static int fill_dummy_bgs(struct btrfs_fs_info *fs_info) 2531 { 2532 struct rb_node *node; 2533 int ret = 0; 2534 2535 for (node = rb_first_cached(&fs_info->mapping_tree); node; node = rb_next(node)) { 2536 struct btrfs_chunk_map *map; 2537 struct btrfs_block_group *bg; 2538 2539 map = rb_entry(node, struct btrfs_chunk_map, rb_node); 2540 bg = btrfs_create_block_group(fs_info, map->start); 2541 if (!bg) { 2542 ret = -ENOMEM; 2543 break; 2544 } 2545 2546 /* Fill dummy cache as FULL */ 2547 bg->length = map->chunk_len; 2548 bg->flags = map->type; 2549 bg->cached = BTRFS_CACHE_FINISHED; 2550 bg->used = map->chunk_len; 2551 bg->flags = map->type; 2552 bg->space_info = btrfs_find_space_info(fs_info, bg->flags); 2553 ret = btrfs_add_block_group_cache(bg); 2554 /* 2555 * We may have some valid block group cache added already, in 2556 * that case we skip to the next one. 2557 */ 2558 if (ret == -EEXIST) { 2559 ret = 0; 2560 btrfs_put_block_group(bg); 2561 continue; 2562 } 2563 2564 if (ret) { 2565 btrfs_remove_free_space_cache(bg); 2566 btrfs_put_block_group(bg); 2567 break; 2568 } 2569 2570 btrfs_add_bg_to_space_info(fs_info, bg); 2571 2572 set_avail_alloc_bits(fs_info, bg->flags); 2573 } 2574 if (!ret) 2575 btrfs_init_global_block_rsv(fs_info); 2576 return ret; 2577 } 2578 2579 int btrfs_read_block_groups(struct btrfs_fs_info *info) 2580 { 2581 struct btrfs_root *root = btrfs_block_group_root(info); 2582 struct btrfs_path *path; 2583 int ret; 2584 struct btrfs_block_group *cache; 2585 struct btrfs_space_info *space_info; 2586 struct btrfs_key key; 2587 int need_clear = 0; 2588 u64 cache_gen; 2589 2590 /* 2591 * Either no extent root (with ibadroots rescue option) or we have 2592 * unsupported RO options. The fs can never be mounted read-write, so no 2593 * need to waste time searching block group items. 2594 * 2595 * This also allows new extent tree related changes to be RO compat, 2596 * no need for a full incompat flag. 2597 */ 2598 if (!root || (btrfs_super_compat_ro_flags(info->super_copy) & 2599 ~BTRFS_FEATURE_COMPAT_RO_SUPP)) 2600 return fill_dummy_bgs(info); 2601 2602 key.objectid = 0; 2603 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 2604 key.offset = 0; 2605 path = btrfs_alloc_path(); 2606 if (!path) 2607 return -ENOMEM; 2608 2609 cache_gen = btrfs_super_cache_generation(info->super_copy); 2610 if (btrfs_test_opt(info, SPACE_CACHE) && 2611 btrfs_super_generation(info->super_copy) != cache_gen) 2612 need_clear = 1; 2613 if (btrfs_test_opt(info, CLEAR_CACHE)) 2614 need_clear = 1; 2615 2616 while (1) { 2617 struct btrfs_block_group_item_v2 bgi; 2618 struct extent_buffer *leaf; 2619 int slot; 2620 size_t size; 2621 2622 ret = find_first_block_group(info, path, &key); 2623 if (ret > 0) 2624 break; 2625 if (ret != 0) 2626 goto error; 2627 2628 leaf = path->nodes[0]; 2629 slot = path->slots[0]; 2630 2631 if (btrfs_fs_incompat(info, REMAP_TREE)) { 2632 size = sizeof(struct btrfs_block_group_item_v2); 2633 } else { 2634 size = sizeof(struct btrfs_block_group_item); 2635 btrfs_set_stack_block_group_v2_remap_bytes(&bgi, 0); 2636 btrfs_set_stack_block_group_v2_identity_remap_count(&bgi, 0); 2637 } 2638 2639 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), 2640 size); 2641 2642 btrfs_item_key_to_cpu(leaf, &key, slot); 2643 btrfs_release_path(path); 2644 ret = read_one_block_group(info, &bgi, &key, need_clear); 2645 if (ret < 0) 2646 goto error; 2647 key.objectid += key.offset; 2648 key.offset = 0; 2649 } 2650 btrfs_release_path(path); 2651 2652 list_for_each_entry(space_info, &info->space_info, list) { 2653 int i; 2654 2655 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2656 if (list_empty(&space_info->block_groups[i])) 2657 continue; 2658 cache = list_first_entry(&space_info->block_groups[i], 2659 struct btrfs_block_group, 2660 list); 2661 btrfs_sysfs_add_block_group_type(cache); 2662 } 2663 2664 if (!(btrfs_get_alloc_profile(info, space_info->flags) & 2665 (BTRFS_BLOCK_GROUP_RAID10 | 2666 BTRFS_BLOCK_GROUP_RAID1_MASK | 2667 BTRFS_BLOCK_GROUP_RAID56_MASK | 2668 BTRFS_BLOCK_GROUP_DUP))) 2669 continue; 2670 /* 2671 * Avoid allocating from un-mirrored block group if there are 2672 * mirrored block groups. 2673 */ 2674 list_for_each_entry(cache, 2675 &space_info->block_groups[BTRFS_RAID_RAID0], 2676 list) 2677 inc_block_group_ro(cache, 1); 2678 list_for_each_entry(cache, 2679 &space_info->block_groups[BTRFS_RAID_SINGLE], 2680 list) 2681 inc_block_group_ro(cache, 1); 2682 } 2683 2684 btrfs_init_global_block_rsv(info); 2685 ret = check_chunk_block_group_mappings(info); 2686 error: 2687 btrfs_free_path(path); 2688 /* 2689 * We've hit some error while reading the extent tree, and have 2690 * rescue=ibadroots mount option. 2691 * Try to fill the tree using dummy block groups so that the user can 2692 * continue to mount and grab their data. 2693 */ 2694 if (ret && btrfs_test_opt(info, IGNOREBADROOTS)) 2695 ret = fill_dummy_bgs(info); 2696 return ret; 2697 } 2698 2699 /* 2700 * This function, insert_block_group_item(), belongs to the phase 2 of chunk 2701 * allocation. 2702 * 2703 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation 2704 * phases. 2705 */ 2706 static int insert_block_group_item(struct btrfs_trans_handle *trans, 2707 struct btrfs_block_group *block_group) 2708 { 2709 struct btrfs_fs_info *fs_info = trans->fs_info; 2710 struct btrfs_block_group_item_v2 bgi; 2711 struct btrfs_root *root = btrfs_block_group_root(fs_info); 2712 struct btrfs_key key; 2713 u64 old_last_used; 2714 size_t size; 2715 int ret; 2716 2717 spin_lock(&block_group->lock); 2718 btrfs_set_stack_block_group_v2_used(&bgi, block_group->used); 2719 btrfs_set_stack_block_group_v2_chunk_objectid(&bgi, block_group->global_root_id); 2720 btrfs_set_stack_block_group_v2_flags(&bgi, block_group->flags); 2721 btrfs_set_stack_block_group_v2_remap_bytes(&bgi, block_group->remap_bytes); 2722 btrfs_set_stack_block_group_v2_identity_remap_count(&bgi, block_group->identity_remap_count); 2723 old_last_used = block_group->last_used; 2724 block_group->last_used = block_group->used; 2725 block_group->last_remap_bytes = block_group->remap_bytes; 2726 block_group->last_identity_remap_count = block_group->identity_remap_count; 2727 block_group->last_flags = block_group->flags; 2728 key.objectid = block_group->start; 2729 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 2730 key.offset = block_group->length; 2731 spin_unlock(&block_group->lock); 2732 2733 if (btrfs_fs_incompat(fs_info, REMAP_TREE)) 2734 size = sizeof(struct btrfs_block_group_item_v2); 2735 else 2736 size = sizeof(struct btrfs_block_group_item); 2737 2738 ret = btrfs_insert_item(trans, root, &key, &bgi, size); 2739 if (ret < 0) { 2740 spin_lock(&block_group->lock); 2741 block_group->last_used = old_last_used; 2742 spin_unlock(&block_group->lock); 2743 } 2744 2745 return ret; 2746 } 2747 2748 static int insert_dev_extent(struct btrfs_trans_handle *trans, 2749 const struct btrfs_device *device, u64 chunk_offset, 2750 u64 start, u64 num_bytes) 2751 { 2752 struct btrfs_fs_info *fs_info = device->fs_info; 2753 struct btrfs_root *root = fs_info->dev_root; 2754 BTRFS_PATH_AUTO_FREE(path); 2755 struct btrfs_dev_extent *extent; 2756 struct extent_buffer *leaf; 2757 struct btrfs_key key; 2758 int ret; 2759 2760 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)); 2761 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); 2762 path = btrfs_alloc_path(); 2763 if (!path) 2764 return -ENOMEM; 2765 2766 key.objectid = device->devid; 2767 key.type = BTRFS_DEV_EXTENT_KEY; 2768 key.offset = start; 2769 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent)); 2770 if (ret) 2771 return ret; 2772 2773 leaf = path->nodes[0]; 2774 extent = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); 2775 btrfs_set_dev_extent_chunk_tree(leaf, extent, BTRFS_CHUNK_TREE_OBJECTID); 2776 btrfs_set_dev_extent_chunk_objectid(leaf, extent, 2777 BTRFS_FIRST_CHUNK_TREE_OBJECTID); 2778 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); 2779 btrfs_set_dev_extent_length(leaf, extent, num_bytes); 2780 2781 return ret; 2782 } 2783 2784 /* 2785 * This function belongs to phase 2. 2786 * 2787 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation 2788 * phases. 2789 */ 2790 static int insert_dev_extents(struct btrfs_trans_handle *trans, 2791 u64 chunk_offset, u64 chunk_size) 2792 { 2793 struct btrfs_fs_info *fs_info = trans->fs_info; 2794 struct btrfs_device *device; 2795 struct btrfs_chunk_map *map; 2796 u64 dev_offset; 2797 int i; 2798 int ret = 0; 2799 2800 map = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); 2801 if (IS_ERR(map)) 2802 return PTR_ERR(map); 2803 2804 /* 2805 * Take the device list mutex to prevent races with the final phase of 2806 * a device replace operation that replaces the device object associated 2807 * with the map's stripes, because the device object's id can change 2808 * at any time during that final phase of the device replace operation 2809 * (dev-replace.c:btrfs_dev_replace_finishing()), so we could grab the 2810 * replaced device and then see it with an ID of BTRFS_DEV_REPLACE_DEVID, 2811 * resulting in persisting a device extent item with such ID. 2812 */ 2813 mutex_lock(&fs_info->fs_devices->device_list_mutex); 2814 for (i = 0; i < map->num_stripes; i++) { 2815 device = map->stripes[i].dev; 2816 dev_offset = map->stripes[i].physical; 2817 2818 ret = insert_dev_extent(trans, device, chunk_offset, dev_offset, 2819 map->stripe_size); 2820 if (ret) 2821 break; 2822 } 2823 mutex_unlock(&fs_info->fs_devices->device_list_mutex); 2824 2825 btrfs_free_chunk_map(map); 2826 return ret; 2827 } 2828 2829 /* 2830 * This function, btrfs_create_pending_block_groups(), belongs to the phase 2 of 2831 * chunk allocation. 2832 * 2833 * See the comment at btrfs_chunk_alloc() for details about the chunk allocation 2834 * phases. 2835 */ 2836 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) 2837 { 2838 struct btrfs_fs_info *fs_info = trans->fs_info; 2839 struct btrfs_block_group *block_group; 2840 int ret = 0; 2841 2842 while (!list_empty(&trans->new_bgs)) { 2843 int index; 2844 2845 block_group = list_first_entry(&trans->new_bgs, 2846 struct btrfs_block_group, 2847 bg_list); 2848 if (ret) 2849 goto next; 2850 2851 index = btrfs_bg_flags_to_raid_index(block_group->flags); 2852 2853 ret = insert_block_group_item(trans, block_group); 2854 if (ret) 2855 btrfs_abort_transaction(trans, ret); 2856 if (!test_bit(BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED, 2857 &block_group->runtime_flags)) { 2858 mutex_lock(&fs_info->chunk_mutex); 2859 ret = btrfs_chunk_alloc_add_chunk_item(trans, block_group); 2860 mutex_unlock(&fs_info->chunk_mutex); 2861 if (ret) 2862 btrfs_abort_transaction(trans, ret); 2863 } 2864 ret = insert_dev_extents(trans, block_group->start, 2865 block_group->length); 2866 if (ret) 2867 btrfs_abort_transaction(trans, ret); 2868 btrfs_add_block_group_free_space(trans, block_group); 2869 2870 /* 2871 * If we restriped during balance, we may have added a new raid 2872 * type, so now add the sysfs entries when it is safe to do so. 2873 * We don't have to worry about locking here as it's handled in 2874 * btrfs_sysfs_add_block_group_type. 2875 */ 2876 if (block_group->space_info->block_group_kobjs[index] == NULL) 2877 btrfs_sysfs_add_block_group_type(block_group); 2878 2879 /* Already aborted the transaction if it failed. */ 2880 next: 2881 btrfs_dec_delayed_refs_rsv_bg_inserts(fs_info); 2882 2883 spin_lock(&fs_info->unused_bgs_lock); 2884 list_del_init(&block_group->bg_list); 2885 clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); 2886 btrfs_put_block_group(block_group); 2887 spin_unlock(&fs_info->unused_bgs_lock); 2888 2889 /* 2890 * If the block group is still unused, add it to the list of 2891 * unused block groups. The block group may have been created in 2892 * order to satisfy a space reservation, in which case the 2893 * extent allocation only happens later. But often we don't 2894 * actually need to allocate space that we previously reserved, 2895 * so the block group may become unused for a long time. For 2896 * example for metadata we generally reserve space for a worst 2897 * possible scenario, but then don't end up allocating all that 2898 * space or none at all (due to no need to COW, extent buffers 2899 * were already COWed in the current transaction and still 2900 * unwritten, tree heights lower than the maximum possible 2901 * height, etc). For data we generally reserve the exact amount 2902 * of space we are going to allocate later, the exception is 2903 * when using compression, as we must reserve space based on the 2904 * uncompressed data size, because the compression is only done 2905 * when writeback triggered and we don't know how much space we 2906 * are actually going to need, so we reserve the uncompressed 2907 * size because the data may be incompressible in the worst case. 2908 */ 2909 if (ret == 0) { 2910 bool used; 2911 2912 spin_lock(&block_group->lock); 2913 used = btrfs_is_block_group_used(block_group); 2914 spin_unlock(&block_group->lock); 2915 2916 if (!used) 2917 btrfs_mark_bg_unused(block_group); 2918 } 2919 } 2920 btrfs_trans_release_chunk_metadata(trans); 2921 } 2922 2923 /* 2924 * For extent tree v2 we use the block_group_item->chunk_offset to point at our 2925 * global root id. For v1 it's always set to BTRFS_FIRST_CHUNK_TREE_OBJECTID. 2926 */ 2927 static u64 calculate_global_root_id(const struct btrfs_fs_info *fs_info, u64 offset) 2928 { 2929 u64 div = SZ_1G; 2930 u64 index; 2931 2932 if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 2933 return BTRFS_FIRST_CHUNK_TREE_OBJECTID; 2934 2935 /* If we have a smaller fs index based on 128MiB. */ 2936 if (btrfs_super_total_bytes(fs_info->super_copy) <= (SZ_1G * 10ULL)) 2937 div = SZ_128M; 2938 2939 offset = div64_u64(offset, div); 2940 div64_u64_rem(offset, fs_info->nr_global_roots, &index); 2941 return index; 2942 } 2943 2944 struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans, 2945 struct btrfs_space_info *space_info, 2946 u64 type, u64 chunk_offset, u64 size) 2947 { 2948 struct btrfs_fs_info *fs_info = trans->fs_info; 2949 struct btrfs_block_group *cache; 2950 int ret; 2951 2952 btrfs_set_log_full_commit(trans); 2953 2954 cache = btrfs_create_block_group(fs_info, chunk_offset); 2955 if (!cache) 2956 return ERR_PTR(-ENOMEM); 2957 2958 /* 2959 * Mark it as new before adding it to the rbtree of block groups or any 2960 * list, so that no other task finds it and calls btrfs_mark_bg_unused() 2961 * before the new flag is set. 2962 */ 2963 set_bit(BLOCK_GROUP_FLAG_NEW, &cache->runtime_flags); 2964 2965 cache->length = size; 2966 btrfs_set_free_space_tree_thresholds(cache); 2967 cache->flags = type; 2968 cache->cached = BTRFS_CACHE_FINISHED; 2969 cache->global_root_id = calculate_global_root_id(fs_info, cache->start); 2970 2971 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) 2972 set_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &cache->runtime_flags); 2973 2974 ret = btrfs_load_block_group_zone_info(cache, true); 2975 if (ret) { 2976 btrfs_put_block_group(cache); 2977 return ERR_PTR(ret); 2978 } 2979 2980 ret = exclude_super_stripes(cache); 2981 if (ret) { 2982 /* We may have excluded something, so call this just in case */ 2983 btrfs_free_excluded_extents(cache); 2984 btrfs_put_block_group(cache); 2985 return ERR_PTR(ret); 2986 } 2987 2988 ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL); 2989 btrfs_free_excluded_extents(cache); 2990 if (ret) { 2991 btrfs_put_block_group(cache); 2992 return ERR_PTR(ret); 2993 } 2994 2995 /* 2996 * Ensure the corresponding space_info object is created and 2997 * assigned to our block group. We want our bg to be added to the rbtree 2998 * with its ->space_info set. 2999 */ 3000 cache->space_info = space_info; 3001 ASSERT(cache->space_info); 3002 3003 ret = btrfs_add_block_group_cache(cache); 3004 if (ret) { 3005 btrfs_remove_free_space_cache(cache); 3006 btrfs_put_block_group(cache); 3007 return ERR_PTR(ret); 3008 } 3009 3010 /* 3011 * Now that our block group has its ->space_info set and is inserted in 3012 * the rbtree, update the space info's counters. 3013 */ 3014 trace_btrfs_add_block_group(fs_info, cache, 1); 3015 btrfs_add_bg_to_space_info(fs_info, cache); 3016 btrfs_update_global_block_rsv(fs_info); 3017 3018 #ifdef CONFIG_BTRFS_DEBUG 3019 if (btrfs_should_fragment_free_space(cache)) { 3020 cache->space_info->bytes_used += size >> 1; 3021 fragment_free_space(cache); 3022 } 3023 #endif 3024 3025 btrfs_link_bg_list(cache, &trans->new_bgs); 3026 btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info); 3027 3028 set_avail_alloc_bits(fs_info, type); 3029 return cache; 3030 } 3031 3032 /* 3033 * Mark one block group RO, can be called several times for the same block 3034 * group. 3035 * 3036 * @cache: the destination block group 3037 * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to 3038 * ensure we still have some free space after marking this 3039 * block group RO. 3040 */ 3041 int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, 3042 bool do_chunk_alloc) 3043 { 3044 struct btrfs_fs_info *fs_info = cache->fs_info; 3045 struct btrfs_space_info *space_info = cache->space_info; 3046 struct btrfs_trans_handle *trans; 3047 struct btrfs_root *root = btrfs_block_group_root(fs_info); 3048 u64 alloc_flags; 3049 int ret; 3050 bool dirty_bg_running; 3051 3052 /* 3053 * This can only happen when we are doing read-only scrub on read-only 3054 * mount. 3055 * In that case we should not start a new transaction on read-only fs. 3056 * Thus here we skip all chunk allocations. 3057 */ 3058 if (sb_rdonly(fs_info->sb)) { 3059 mutex_lock(&fs_info->ro_block_group_mutex); 3060 ret = inc_block_group_ro(cache, 0); 3061 mutex_unlock(&fs_info->ro_block_group_mutex); 3062 return ret; 3063 } 3064 3065 do { 3066 trans = btrfs_join_transaction(root); 3067 if (IS_ERR(trans)) 3068 return PTR_ERR(trans); 3069 3070 dirty_bg_running = false; 3071 3072 /* 3073 * We're not allowed to set block groups readonly after the dirty 3074 * block group cache has started writing. If it already started, 3075 * back off and let this transaction commit. 3076 */ 3077 mutex_lock(&fs_info->ro_block_group_mutex); 3078 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { 3079 u64 transid = trans->transid; 3080 3081 mutex_unlock(&fs_info->ro_block_group_mutex); 3082 btrfs_end_transaction(trans); 3083 3084 ret = btrfs_wait_for_commit(fs_info, transid); 3085 if (ret) 3086 return ret; 3087 dirty_bg_running = true; 3088 } 3089 } while (dirty_bg_running); 3090 3091 if (do_chunk_alloc) { 3092 /* 3093 * If we are changing raid levels, try to allocate a 3094 * corresponding block group with the new raid level. 3095 */ 3096 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); 3097 if (alloc_flags != cache->flags) { 3098 ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, 3099 CHUNK_ALLOC_FORCE); 3100 /* 3101 * ENOSPC is allowed here, we may have enough space 3102 * already allocated at the new raid level to carry on 3103 */ 3104 if (ret == -ENOSPC) 3105 ret = 0; 3106 if (ret < 0) 3107 goto out; 3108 } 3109 } 3110 3111 ret = inc_block_group_ro(cache, 0); 3112 if (!ret) 3113 goto out; 3114 if (ret == -ETXTBSY) 3115 goto unlock_out; 3116 3117 /* 3118 * Skip chunk allocation if the bg is SYSTEM, this is to avoid system 3119 * chunk allocation storm to exhaust the system chunk array. Otherwise 3120 * we still want to try our best to mark the block group read-only. 3121 */ 3122 if (!do_chunk_alloc && ret == -ENOSPC && 3123 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM)) 3124 goto unlock_out; 3125 3126 alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags); 3127 ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE); 3128 if (ret < 0) 3129 goto out; 3130 /* 3131 * We have allocated a new chunk. We also need to activate that chunk to 3132 * grant metadata tickets for zoned filesystem. 3133 */ 3134 ret = btrfs_zoned_activate_one_bg(space_info, true); 3135 if (ret < 0) 3136 goto out; 3137 3138 ret = inc_block_group_ro(cache, 0); 3139 if (ret == -ETXTBSY) 3140 goto unlock_out; 3141 out: 3142 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { 3143 alloc_flags = btrfs_get_alloc_profile(fs_info, cache->flags); 3144 mutex_lock(&fs_info->chunk_mutex); 3145 check_system_chunk(trans, alloc_flags); 3146 mutex_unlock(&fs_info->chunk_mutex); 3147 } 3148 unlock_out: 3149 mutex_unlock(&fs_info->ro_block_group_mutex); 3150 3151 btrfs_end_transaction(trans); 3152 return ret; 3153 } 3154 3155 void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) 3156 { 3157 struct btrfs_space_info *sinfo = cache->space_info; 3158 3159 BUG_ON(!cache->ro); 3160 3161 spin_lock(&sinfo->lock); 3162 spin_lock(&cache->lock); 3163 if (!--cache->ro) { 3164 if (btrfs_is_zoned(cache->fs_info)) { 3165 /* Migrate zone_unusable bytes back */ 3166 cache->zone_unusable = 3167 (cache->alloc_offset - cache->used - cache->pinned - 3168 cache->reserved) + 3169 (cache->length - cache->zone_capacity); 3170 btrfs_space_info_update_bytes_zone_unusable(sinfo, cache->zone_unusable); 3171 sinfo->bytes_readonly -= cache->zone_unusable; 3172 } 3173 sinfo->bytes_readonly -= btrfs_block_group_available_space(cache); 3174 list_del_init(&cache->ro_list); 3175 } 3176 spin_unlock(&cache->lock); 3177 spin_unlock(&sinfo->lock); 3178 } 3179 3180 static int update_block_group_item(struct btrfs_trans_handle *trans, 3181 struct btrfs_path *path, 3182 struct btrfs_block_group *cache) 3183 { 3184 struct btrfs_fs_info *fs_info = trans->fs_info; 3185 int ret; 3186 struct btrfs_root *root = btrfs_block_group_root(fs_info); 3187 unsigned long bi; 3188 struct extent_buffer *leaf; 3189 struct btrfs_block_group_item_v2 bgi; 3190 struct btrfs_key key; 3191 u64 old_last_used, old_last_remap_bytes; 3192 u32 old_last_identity_remap_count; 3193 u64 used, remap_bytes; 3194 u32 identity_remap_count; 3195 3196 /* 3197 * Block group items update can be triggered out of commit transaction 3198 * critical section, thus we need a consistent view of used bytes. 3199 * We cannot use cache->used directly outside of the spin lock, as it 3200 * may be changed. 3201 */ 3202 spin_lock(&cache->lock); 3203 old_last_used = cache->last_used; 3204 old_last_remap_bytes = cache->last_remap_bytes; 3205 old_last_identity_remap_count = cache->last_identity_remap_count; 3206 used = cache->used; 3207 remap_bytes = cache->remap_bytes; 3208 identity_remap_count = cache->identity_remap_count; 3209 /* No change in values, can safely skip it. */ 3210 if (cache->last_used == used && 3211 cache->last_remap_bytes == remap_bytes && 3212 cache->last_identity_remap_count == identity_remap_count && 3213 cache->last_flags == cache->flags) { 3214 spin_unlock(&cache->lock); 3215 return 0; 3216 } 3217 cache->last_used = used; 3218 cache->last_remap_bytes = remap_bytes; 3219 cache->last_identity_remap_count = identity_remap_count; 3220 cache->last_flags = cache->flags; 3221 spin_unlock(&cache->lock); 3222 3223 key.objectid = cache->start; 3224 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 3225 key.offset = cache->length; 3226 3227 ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 3228 if (ret) { 3229 if (ret > 0) 3230 ret = -ENOENT; 3231 goto fail; 3232 } 3233 3234 leaf = path->nodes[0]; 3235 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); 3236 btrfs_set_stack_block_group_v2_used(&bgi, used); 3237 btrfs_set_stack_block_group_v2_chunk_objectid(&bgi, cache->global_root_id); 3238 btrfs_set_stack_block_group_v2_flags(&bgi, cache->flags); 3239 3240 if (btrfs_fs_incompat(fs_info, REMAP_TREE)) { 3241 btrfs_set_stack_block_group_v2_remap_bytes(&bgi, cache->remap_bytes); 3242 btrfs_set_stack_block_group_v2_identity_remap_count(&bgi, 3243 cache->identity_remap_count); 3244 write_extent_buffer(leaf, &bgi, bi, 3245 sizeof(struct btrfs_block_group_item_v2)); 3246 } else { 3247 write_extent_buffer(leaf, &bgi, bi, 3248 sizeof(struct btrfs_block_group_item)); 3249 } 3250 3251 fail: 3252 btrfs_release_path(path); 3253 /* 3254 * We didn't update the block group item, need to revert last_used 3255 * unless the block group item didn't exist yet - this is to prevent a 3256 * race with a concurrent insertion of the block group item, with 3257 * insert_block_group_item(), that happened just after we attempted to 3258 * update. In that case we would reset last_used to 0 just after the 3259 * insertion set it to a value greater than 0 - if the block group later 3260 * becomes with 0 used bytes, we would incorrectly skip its update. 3261 */ 3262 if (ret < 0 && ret != -ENOENT) { 3263 spin_lock(&cache->lock); 3264 cache->last_used = old_last_used; 3265 cache->last_remap_bytes = old_last_remap_bytes; 3266 cache->last_identity_remap_count = old_last_identity_remap_count; 3267 spin_unlock(&cache->lock); 3268 } 3269 return ret; 3270 3271 } 3272 3273 static int cache_save_setup(struct btrfs_block_group *block_group, 3274 struct btrfs_trans_handle *trans, 3275 struct btrfs_path *path) 3276 { 3277 struct btrfs_fs_info *fs_info = block_group->fs_info; 3278 struct inode *inode = NULL; 3279 struct extent_changeset *data_reserved = NULL; 3280 u64 alloc_hint = 0; 3281 int dcs = BTRFS_DC_ERROR; 3282 u64 cache_size = 0; 3283 int retries = 0; 3284 int ret = 0; 3285 3286 if (!btrfs_test_opt(fs_info, SPACE_CACHE)) 3287 return 0; 3288 3289 /* 3290 * If this block group is smaller than 100 megs don't bother caching the 3291 * block group. 3292 */ 3293 if (block_group->length < (100 * SZ_1M)) { 3294 spin_lock(&block_group->lock); 3295 block_group->disk_cache_state = BTRFS_DC_WRITTEN; 3296 spin_unlock(&block_group->lock); 3297 return 0; 3298 } 3299 3300 if (TRANS_ABORTED(trans)) 3301 return 0; 3302 again: 3303 inode = lookup_free_space_inode(block_group, path); 3304 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { 3305 ret = PTR_ERR(inode); 3306 btrfs_release_path(path); 3307 goto out; 3308 } 3309 3310 if (IS_ERR(inode)) { 3311 BUG_ON(retries); 3312 retries++; 3313 3314 if (block_group->ro) 3315 goto out_free; 3316 3317 ret = create_free_space_inode(trans, block_group, path); 3318 if (ret) 3319 goto out_free; 3320 goto again; 3321 } 3322 3323 /* 3324 * We want to set the generation to 0, that way if anything goes wrong 3325 * from here on out we know not to trust this cache when we load up next 3326 * time. 3327 */ 3328 BTRFS_I(inode)->generation = 0; 3329 ret = btrfs_update_inode(trans, BTRFS_I(inode)); 3330 if (unlikely(ret)) { 3331 /* 3332 * So theoretically we could recover from this, simply set the 3333 * super cache generation to 0 so we know to invalidate the 3334 * cache, but then we'd have to keep track of the block groups 3335 * that fail this way so we know we _have_ to reset this cache 3336 * before the next commit or risk reading stale cache. So to 3337 * limit our exposure to horrible edge cases lets just abort the 3338 * transaction, this only happens in really bad situations 3339 * anyway. 3340 */ 3341 btrfs_abort_transaction(trans, ret); 3342 goto out_put; 3343 } 3344 WARN_ON(ret); 3345 3346 /* We've already setup this transaction, go ahead and exit */ 3347 if (block_group->cache_generation == trans->transid && 3348 i_size_read(inode)) { 3349 dcs = BTRFS_DC_SETUP; 3350 goto out_put; 3351 } 3352 3353 if (i_size_read(inode) > 0) { 3354 ret = btrfs_check_trunc_cache_free_space(fs_info, 3355 &fs_info->global_block_rsv); 3356 if (ret) 3357 goto out_put; 3358 3359 ret = btrfs_truncate_free_space_cache(trans, NULL, inode); 3360 if (ret) 3361 goto out_put; 3362 } 3363 3364 spin_lock(&block_group->lock); 3365 if (block_group->cached != BTRFS_CACHE_FINISHED || 3366 !btrfs_test_opt(fs_info, SPACE_CACHE)) { 3367 /* 3368 * don't bother trying to write stuff out _if_ 3369 * a) we're not cached, 3370 * b) we're with nospace_cache mount option, 3371 * c) we're with v2 space_cache (FREE_SPACE_TREE). 3372 */ 3373 dcs = BTRFS_DC_WRITTEN; 3374 spin_unlock(&block_group->lock); 3375 goto out_put; 3376 } 3377 spin_unlock(&block_group->lock); 3378 3379 /* 3380 * We hit an ENOSPC when setting up the cache in this transaction, just 3381 * skip doing the setup, we've already cleared the cache so we're safe. 3382 */ 3383 if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { 3384 ret = -ENOSPC; 3385 goto out_put; 3386 } 3387 3388 /* 3389 * Try to preallocate enough space based on how big the block group is. 3390 * Keep in mind this has to include any pinned space which could end up 3391 * taking up quite a bit since it's not folded into the other space 3392 * cache. 3393 */ 3394 cache_size = div_u64(block_group->length, SZ_256M); 3395 if (!cache_size) 3396 cache_size = 1; 3397 3398 cache_size *= 16; 3399 cache_size *= fs_info->sectorsize; 3400 3401 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved, 0, 3402 cache_size, false); 3403 if (ret) 3404 goto out_put; 3405 3406 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, cache_size, 3407 cache_size, cache_size, 3408 &alloc_hint); 3409 /* 3410 * Our cache requires contiguous chunks so that we don't modify a bunch 3411 * of metadata or split extents when writing the cache out, which means 3412 * we can enospc if we are heavily fragmented in addition to just normal 3413 * out of space conditions. So if we hit this just skip setting up any 3414 * other block groups for this transaction, maybe we'll unpin enough 3415 * space the next time around. 3416 */ 3417 if (!ret) 3418 dcs = BTRFS_DC_SETUP; 3419 else if (ret == -ENOSPC) 3420 set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); 3421 3422 out_put: 3423 iput(inode); 3424 out_free: 3425 btrfs_release_path(path); 3426 out: 3427 spin_lock(&block_group->lock); 3428 if (!ret && dcs == BTRFS_DC_SETUP) 3429 block_group->cache_generation = trans->transid; 3430 block_group->disk_cache_state = dcs; 3431 spin_unlock(&block_group->lock); 3432 3433 extent_changeset_free(data_reserved); 3434 return ret; 3435 } 3436 3437 int btrfs_setup_space_cache(struct btrfs_trans_handle *trans) 3438 { 3439 struct btrfs_fs_info *fs_info = trans->fs_info; 3440 struct btrfs_block_group *cache, *tmp; 3441 struct btrfs_transaction *cur_trans = trans->transaction; 3442 BTRFS_PATH_AUTO_FREE(path); 3443 3444 if (list_empty(&cur_trans->dirty_bgs) || 3445 !btrfs_test_opt(fs_info, SPACE_CACHE)) 3446 return 0; 3447 3448 path = btrfs_alloc_path(); 3449 if (!path) 3450 return -ENOMEM; 3451 3452 /* Could add new block groups, use _safe just in case */ 3453 list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, 3454 dirty_list) { 3455 if (cache->disk_cache_state == BTRFS_DC_CLEAR) 3456 cache_save_setup(cache, trans, path); 3457 } 3458 3459 return 0; 3460 } 3461 3462 /* 3463 * Transaction commit does final block group cache writeback during a critical 3464 * section where nothing is allowed to change the FS. This is required in 3465 * order for the cache to actually match the block group, but can introduce a 3466 * lot of latency into the commit. 3467 * 3468 * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO. 3469 * There's a chance we'll have to redo some of it if the block group changes 3470 * again during the commit, but it greatly reduces the commit latency by 3471 * getting rid of the easy block groups while we're still allowing others to 3472 * join the commit. 3473 */ 3474 int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans) 3475 { 3476 struct btrfs_fs_info *fs_info = trans->fs_info; 3477 struct btrfs_block_group *cache; 3478 struct btrfs_transaction *cur_trans = trans->transaction; 3479 int ret = 0; 3480 int should_put; 3481 BTRFS_PATH_AUTO_FREE(path); 3482 LIST_HEAD(dirty); 3483 struct list_head *io = &cur_trans->io_bgs; 3484 int loops = 0; 3485 3486 spin_lock(&cur_trans->dirty_bgs_lock); 3487 if (list_empty(&cur_trans->dirty_bgs)) { 3488 spin_unlock(&cur_trans->dirty_bgs_lock); 3489 return 0; 3490 } 3491 list_splice_init(&cur_trans->dirty_bgs, &dirty); 3492 spin_unlock(&cur_trans->dirty_bgs_lock); 3493 3494 again: 3495 /* Make sure all the block groups on our dirty list actually exist */ 3496 btrfs_create_pending_block_groups(trans); 3497 3498 if (!path) { 3499 path = btrfs_alloc_path(); 3500 if (!path) { 3501 ret = -ENOMEM; 3502 goto out; 3503 } 3504 } 3505 3506 /* 3507 * cache_write_mutex is here only to save us from balance or automatic 3508 * removal of empty block groups deleting this block group while we are 3509 * writing out the cache 3510 */ 3511 mutex_lock(&trans->transaction->cache_write_mutex); 3512 while (!list_empty(&dirty)) { 3513 bool drop_reserve = true; 3514 3515 cache = list_first_entry(&dirty, struct btrfs_block_group, 3516 dirty_list); 3517 /* 3518 * This can happen if something re-dirties a block group that 3519 * is already under IO. Just wait for it to finish and then do 3520 * it all again 3521 */ 3522 if (!list_empty(&cache->io_list)) { 3523 list_del_init(&cache->io_list); 3524 btrfs_wait_cache_io(trans, cache, path); 3525 btrfs_put_block_group(cache); 3526 } 3527 3528 3529 /* 3530 * btrfs_wait_cache_io uses the cache->dirty_list to decide if 3531 * it should update the cache_state. Don't delete until after 3532 * we wait. 3533 * 3534 * Since we're not running in the commit critical section 3535 * we need the dirty_bgs_lock to protect from update_block_group 3536 */ 3537 spin_lock(&cur_trans->dirty_bgs_lock); 3538 list_del_init(&cache->dirty_list); 3539 spin_unlock(&cur_trans->dirty_bgs_lock); 3540 3541 should_put = 1; 3542 3543 cache_save_setup(cache, trans, path); 3544 3545 if (cache->disk_cache_state == BTRFS_DC_SETUP) { 3546 cache->io_ctl.inode = NULL; 3547 ret = btrfs_write_out_cache(trans, cache, path); 3548 if (ret == 0 && cache->io_ctl.inode) { 3549 should_put = 0; 3550 3551 /* 3552 * The cache_write_mutex is protecting the 3553 * io_list, also refer to the definition of 3554 * btrfs_transaction::io_bgs for more details 3555 */ 3556 list_add_tail(&cache->io_list, io); 3557 } else { 3558 /* 3559 * If we failed to write the cache, the 3560 * generation will be bad and life goes on 3561 */ 3562 ret = 0; 3563 } 3564 } 3565 if (!ret) { 3566 ret = update_block_group_item(trans, path, cache); 3567 /* 3568 * Our block group might still be attached to the list 3569 * of new block groups in the transaction handle of some 3570 * other task (struct btrfs_trans_handle->new_bgs). This 3571 * means its block group item isn't yet in the extent 3572 * tree. If this happens ignore the error, as we will 3573 * try again later in the critical section of the 3574 * transaction commit. 3575 */ 3576 if (ret == -ENOENT) { 3577 ret = 0; 3578 spin_lock(&cur_trans->dirty_bgs_lock); 3579 if (list_empty(&cache->dirty_list)) { 3580 list_add_tail(&cache->dirty_list, 3581 &cur_trans->dirty_bgs); 3582 btrfs_get_block_group(cache); 3583 drop_reserve = false; 3584 } 3585 spin_unlock(&cur_trans->dirty_bgs_lock); 3586 } else if (ret) { 3587 btrfs_abort_transaction(trans, ret); 3588 } 3589 } 3590 3591 /* If it's not on the io list, we need to put the block group */ 3592 if (should_put) 3593 btrfs_put_block_group(cache); 3594 if (drop_reserve) 3595 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info); 3596 /* 3597 * Avoid blocking other tasks for too long. It might even save 3598 * us from writing caches for block groups that are going to be 3599 * removed. 3600 */ 3601 mutex_unlock(&trans->transaction->cache_write_mutex); 3602 if (ret) 3603 goto out; 3604 mutex_lock(&trans->transaction->cache_write_mutex); 3605 } 3606 mutex_unlock(&trans->transaction->cache_write_mutex); 3607 3608 /* 3609 * Go through delayed refs for all the stuff we've just kicked off 3610 * and then loop back (just once) 3611 */ 3612 if (!ret) 3613 ret = btrfs_run_delayed_refs(trans, 0); 3614 if (!ret && loops == 0) { 3615 loops++; 3616 spin_lock(&cur_trans->dirty_bgs_lock); 3617 list_splice_init(&cur_trans->dirty_bgs, &dirty); 3618 /* 3619 * dirty_bgs_lock protects us from concurrent block group 3620 * deletes too (not just cache_write_mutex). 3621 */ 3622 if (!list_empty(&dirty)) { 3623 spin_unlock(&cur_trans->dirty_bgs_lock); 3624 goto again; 3625 } 3626 spin_unlock(&cur_trans->dirty_bgs_lock); 3627 } 3628 out: 3629 if (ret < 0) { 3630 spin_lock(&cur_trans->dirty_bgs_lock); 3631 list_splice_init(&dirty, &cur_trans->dirty_bgs); 3632 spin_unlock(&cur_trans->dirty_bgs_lock); 3633 btrfs_cleanup_dirty_bgs(cur_trans, fs_info); 3634 } 3635 3636 return ret; 3637 } 3638 3639 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) 3640 { 3641 struct btrfs_fs_info *fs_info = trans->fs_info; 3642 struct btrfs_block_group *cache; 3643 struct btrfs_transaction *cur_trans = trans->transaction; 3644 int ret = 0; 3645 int should_put; 3646 BTRFS_PATH_AUTO_FREE(path); 3647 struct list_head *io = &cur_trans->io_bgs; 3648 3649 path = btrfs_alloc_path(); 3650 if (!path) 3651 return -ENOMEM; 3652 3653 /* 3654 * Even though we are in the critical section of the transaction commit, 3655 * we can still have concurrent tasks adding elements to this 3656 * transaction's list of dirty block groups. These tasks correspond to 3657 * endio free space workers started when writeback finishes for a 3658 * space cache, which run inode.c:btrfs_finish_ordered_io(), and can 3659 * allocate new block groups as a result of COWing nodes of the root 3660 * tree when updating the free space inode. The writeback for the space 3661 * caches is triggered by an earlier call to 3662 * btrfs_start_dirty_block_groups() and iterations of the following 3663 * loop. 3664 * Also we want to do the cache_save_setup first and then run the 3665 * delayed refs to make sure we have the best chance at doing this all 3666 * in one shot. 3667 */ 3668 spin_lock(&cur_trans->dirty_bgs_lock); 3669 while (!list_empty(&cur_trans->dirty_bgs)) { 3670 cache = list_first_entry(&cur_trans->dirty_bgs, 3671 struct btrfs_block_group, 3672 dirty_list); 3673 3674 /* 3675 * This can happen if cache_save_setup re-dirties a block group 3676 * that is already under IO. Just wait for it to finish and 3677 * then do it all again 3678 */ 3679 if (!list_empty(&cache->io_list)) { 3680 spin_unlock(&cur_trans->dirty_bgs_lock); 3681 list_del_init(&cache->io_list); 3682 btrfs_wait_cache_io(trans, cache, path); 3683 btrfs_put_block_group(cache); 3684 spin_lock(&cur_trans->dirty_bgs_lock); 3685 } 3686 3687 /* 3688 * Don't remove from the dirty list until after we've waited on 3689 * any pending IO 3690 */ 3691 list_del_init(&cache->dirty_list); 3692 spin_unlock(&cur_trans->dirty_bgs_lock); 3693 should_put = 1; 3694 3695 cache_save_setup(cache, trans, path); 3696 3697 if (!ret) 3698 ret = btrfs_run_delayed_refs(trans, U64_MAX); 3699 3700 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { 3701 cache->io_ctl.inode = NULL; 3702 ret = btrfs_write_out_cache(trans, cache, path); 3703 if (ret == 0 && cache->io_ctl.inode) { 3704 should_put = 0; 3705 list_add_tail(&cache->io_list, io); 3706 } else { 3707 /* 3708 * If we failed to write the cache, the 3709 * generation will be bad and life goes on 3710 */ 3711 ret = 0; 3712 } 3713 } 3714 if (!ret) { 3715 ret = update_block_group_item(trans, path, cache); 3716 /* 3717 * One of the free space endio workers might have 3718 * created a new block group while updating a free space 3719 * cache's inode (at inode.c:btrfs_finish_ordered_io()) 3720 * and hasn't released its transaction handle yet, in 3721 * which case the new block group is still attached to 3722 * its transaction handle and its creation has not 3723 * finished yet (no block group item in the extent tree 3724 * yet, etc). If this is the case, wait for all free 3725 * space endio workers to finish and retry. This is a 3726 * very rare case so no need for a more efficient and 3727 * complex approach. 3728 */ 3729 if (ret == -ENOENT) { 3730 wait_event(cur_trans->writer_wait, 3731 atomic_read(&cur_trans->num_writers) == 1); 3732 ret = update_block_group_item(trans, path, cache); 3733 if (ret) 3734 btrfs_abort_transaction(trans, ret); 3735 } else if (ret) { 3736 btrfs_abort_transaction(trans, ret); 3737 } 3738 } 3739 3740 /* If its not on the io list, we need to put the block group */ 3741 if (should_put) 3742 btrfs_put_block_group(cache); 3743 btrfs_dec_delayed_refs_rsv_bg_updates(fs_info); 3744 spin_lock(&cur_trans->dirty_bgs_lock); 3745 } 3746 spin_unlock(&cur_trans->dirty_bgs_lock); 3747 3748 /* 3749 * Refer to the definition of io_bgs member for details why it's safe 3750 * to use it without any locking 3751 */ 3752 while (!list_empty(io)) { 3753 cache = list_first_entry(io, struct btrfs_block_group, 3754 io_list); 3755 list_del_init(&cache->io_list); 3756 btrfs_wait_cache_io(trans, cache, path); 3757 btrfs_put_block_group(cache); 3758 } 3759 3760 return ret; 3761 } 3762 3763 int btrfs_update_block_group(struct btrfs_trans_handle *trans, 3764 u64 bytenr, u64 num_bytes, bool alloc) 3765 { 3766 struct btrfs_fs_info *info = trans->fs_info; 3767 struct btrfs_space_info *space_info; 3768 struct btrfs_block_group *cache; 3769 u64 old_val; 3770 bool reclaim = false; 3771 bool bg_already_dirty = true; 3772 int factor; 3773 3774 /* Block accounting for super block */ 3775 spin_lock(&info->delalloc_root_lock); 3776 old_val = btrfs_super_bytes_used(info->super_copy); 3777 if (alloc) 3778 old_val += num_bytes; 3779 else 3780 old_val -= num_bytes; 3781 btrfs_set_super_bytes_used(info->super_copy, old_val); 3782 spin_unlock(&info->delalloc_root_lock); 3783 3784 cache = btrfs_lookup_block_group(info, bytenr); 3785 if (!cache) 3786 return -ENOENT; 3787 3788 /* An extent can not span multiple block groups. */ 3789 ASSERT(bytenr + num_bytes <= btrfs_block_group_end(cache)); 3790 3791 space_info = cache->space_info; 3792 factor = btrfs_bg_type_to_factor(cache->flags); 3793 3794 /* 3795 * If this block group has free space cache written out, we need to make 3796 * sure to load it if we are removing space. This is because we need 3797 * the unpinning stage to actually add the space back to the block group, 3798 * otherwise we will leak space. 3799 */ 3800 if (!alloc && !btrfs_block_group_done(cache)) 3801 btrfs_cache_block_group(cache, true); 3802 3803 spin_lock(&space_info->lock); 3804 spin_lock(&cache->lock); 3805 3806 if (btrfs_test_opt(info, SPACE_CACHE) && 3807 cache->disk_cache_state < BTRFS_DC_CLEAR) 3808 cache->disk_cache_state = BTRFS_DC_CLEAR; 3809 3810 old_val = cache->used; 3811 if (alloc) { 3812 old_val += num_bytes; 3813 cache->used = old_val; 3814 cache->reserved -= num_bytes; 3815 cache->reclaim_mark = 0; 3816 space_info->bytes_reserved -= num_bytes; 3817 space_info->bytes_used += num_bytes; 3818 space_info->disk_used += num_bytes * factor; 3819 if (READ_ONCE(space_info->periodic_reclaim)) 3820 btrfs_space_info_update_reclaimable(space_info, -num_bytes); 3821 spin_unlock(&cache->lock); 3822 spin_unlock(&space_info->lock); 3823 } else { 3824 old_val -= num_bytes; 3825 cache->used = old_val; 3826 cache->pinned += num_bytes; 3827 btrfs_space_info_update_bytes_pinned(space_info, num_bytes); 3828 space_info->bytes_used -= num_bytes; 3829 space_info->disk_used -= num_bytes * factor; 3830 if (READ_ONCE(space_info->periodic_reclaim)) 3831 btrfs_space_info_update_reclaimable(space_info, num_bytes); 3832 else 3833 reclaim = should_reclaim_block_group(cache, num_bytes); 3834 3835 spin_unlock(&cache->lock); 3836 spin_unlock(&space_info->lock); 3837 3838 btrfs_set_extent_bit(&trans->transaction->pinned_extents, bytenr, 3839 bytenr + num_bytes - 1, EXTENT_DIRTY, NULL); 3840 } 3841 3842 spin_lock(&trans->transaction->dirty_bgs_lock); 3843 if (list_empty(&cache->dirty_list)) { 3844 list_add_tail(&cache->dirty_list, &trans->transaction->dirty_bgs); 3845 bg_already_dirty = false; 3846 btrfs_get_block_group(cache); 3847 } 3848 spin_unlock(&trans->transaction->dirty_bgs_lock); 3849 3850 /* 3851 * No longer have used bytes in this block group, queue it for deletion. 3852 * We do this after adding the block group to the dirty list to avoid 3853 * races between cleaner kthread and space cache writeout. 3854 */ 3855 if (!alloc && old_val == 0) { 3856 if (!btrfs_test_opt(info, DISCARD_ASYNC)) 3857 btrfs_mark_bg_unused(cache); 3858 } else if (!alloc && reclaim) { 3859 btrfs_mark_bg_to_reclaim(cache); 3860 } 3861 3862 btrfs_put_block_group(cache); 3863 3864 /* Modified block groups are accounted for in the delayed_refs_rsv. */ 3865 if (!bg_already_dirty) 3866 btrfs_inc_delayed_refs_rsv_bg_updates(info); 3867 3868 return 0; 3869 } 3870 3871 /* 3872 * Update the block_group and space info counters. 3873 * 3874 * @cache: The cache we are manipulating 3875 * @ram_bytes: The number of bytes of file content, and will be same to 3876 * @num_bytes except for the compress path. 3877 * @num_bytes: The number of bytes in question 3878 * @delalloc: The blocks are allocated for the delalloc write 3879 * 3880 * This is called by the allocator when it reserves space. If this is a 3881 * reservation and the block group has become read only we cannot make the 3882 * reservation and return -EAGAIN, otherwise this function always succeeds. 3883 */ 3884 int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, 3885 u64 ram_bytes, u64 num_bytes, bool delalloc, 3886 bool force_wrong_size_class) 3887 { 3888 struct btrfs_space_info *space_info = cache->space_info; 3889 enum btrfs_block_group_size_class size_class; 3890 int ret = 0; 3891 3892 spin_lock(&space_info->lock); 3893 spin_lock(&cache->lock); 3894 if (cache->ro) { 3895 ret = -EAGAIN; 3896 goto out_error; 3897 } 3898 3899 if (btrfs_block_group_should_use_size_class(cache)) { 3900 size_class = btrfs_calc_block_group_size_class(num_bytes); 3901 ret = btrfs_use_block_group_size_class(cache, size_class, force_wrong_size_class); 3902 if (ret) 3903 goto out_error; 3904 } 3905 3906 cache->reserved += num_bytes; 3907 if (delalloc) 3908 cache->delalloc_bytes += num_bytes; 3909 3910 trace_btrfs_space_reservation(cache->fs_info, "space_info", 3911 space_info->flags, num_bytes, 1); 3912 spin_unlock(&cache->lock); 3913 3914 space_info->bytes_reserved += num_bytes; 3915 btrfs_space_info_update_bytes_may_use(space_info, -ram_bytes); 3916 3917 /* 3918 * Compression can use less space than we reserved, so wake tickets if 3919 * that happens. 3920 */ 3921 if (num_bytes < ram_bytes) 3922 btrfs_try_granting_tickets(space_info); 3923 spin_unlock(&space_info->lock); 3924 3925 return 0; 3926 3927 out_error: 3928 spin_unlock(&cache->lock); 3929 spin_unlock(&space_info->lock); 3930 return ret; 3931 } 3932 3933 /* 3934 * Update the block_group and space info counters. 3935 * 3936 * @cache: The cache we are manipulating. 3937 * @num_bytes: The number of bytes in question. 3938 * @is_delalloc: Whether the blocks are allocated for a delalloc write. 3939 * 3940 * This is called by somebody who is freeing space that was never actually used 3941 * on disk. For example if you reserve some space for a new leaf in transaction 3942 * A and before transaction A commits you free that leaf, you call this with 3943 * reserve set to 0 in order to clear the reservation. 3944 */ 3945 void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes, 3946 bool is_delalloc) 3947 { 3948 struct btrfs_space_info *space_info = cache->space_info; 3949 bool bg_ro; 3950 3951 spin_lock(&space_info->lock); 3952 spin_lock(&cache->lock); 3953 bg_ro = cache->ro; 3954 cache->reserved -= num_bytes; 3955 if (is_delalloc) 3956 cache->delalloc_bytes -= num_bytes; 3957 spin_unlock(&cache->lock); 3958 3959 if (bg_ro) 3960 space_info->bytes_readonly += num_bytes; 3961 else if (btrfs_is_zoned(cache->fs_info)) 3962 space_info->bytes_zone_unusable += num_bytes; 3963 3964 space_info->bytes_reserved -= num_bytes; 3965 space_info->max_extent_size = 0; 3966 3967 btrfs_try_granting_tickets(space_info); 3968 spin_unlock(&space_info->lock); 3969 } 3970 3971 static void force_metadata_allocation(struct btrfs_fs_info *info) 3972 { 3973 struct list_head *head = &info->space_info; 3974 struct btrfs_space_info *found; 3975 3976 list_for_each_entry(found, head, list) { 3977 if (found->flags & BTRFS_BLOCK_GROUP_METADATA) 3978 found->force_alloc = CHUNK_ALLOC_FORCE; 3979 } 3980 } 3981 3982 static bool should_alloc_chunk(const struct btrfs_fs_info *fs_info, 3983 const struct btrfs_space_info *sinfo, int force) 3984 { 3985 u64 bytes_used = btrfs_space_info_used(sinfo, false); 3986 u64 thresh; 3987 3988 if (force == CHUNK_ALLOC_FORCE) 3989 return true; 3990 3991 /* 3992 * in limited mode, we want to have some free space up to 3993 * about 1% of the FS size. 3994 */ 3995 if (force == CHUNK_ALLOC_LIMITED) { 3996 thresh = btrfs_super_total_bytes(fs_info->super_copy); 3997 thresh = max_t(u64, SZ_64M, mult_perc(thresh, 1)); 3998 3999 if (sinfo->total_bytes - bytes_used < thresh) 4000 return true; 4001 } 4002 4003 if (bytes_used + SZ_2M < mult_perc(sinfo->total_bytes, 80)) 4004 return false; 4005 return true; 4006 } 4007 4008 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type) 4009 { 4010 u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); 4011 struct btrfs_space_info *space_info; 4012 4013 space_info = btrfs_find_space_info(trans->fs_info, type); 4014 if (!space_info) { 4015 DEBUG_WARN(); 4016 return -EINVAL; 4017 } 4018 4019 return btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE); 4020 } 4021 4022 static struct btrfs_block_group *do_chunk_alloc(struct btrfs_trans_handle *trans, 4023 struct btrfs_space_info *space_info, 4024 u64 flags) 4025 { 4026 struct btrfs_block_group *bg; 4027 int ret; 4028 4029 /* 4030 * Check if we have enough space in the system space info because we 4031 * will need to update device items in the chunk btree and insert a new 4032 * chunk item in the chunk btree as well. This will allocate a new 4033 * system block group if needed. 4034 */ 4035 check_system_chunk(trans, flags); 4036 4037 bg = btrfs_create_chunk(trans, space_info, flags); 4038 if (IS_ERR(bg)) { 4039 ret = PTR_ERR(bg); 4040 goto out; 4041 } 4042 4043 ret = btrfs_chunk_alloc_add_chunk_item(trans, bg); 4044 /* 4045 * Normally we are not expected to fail with -ENOSPC here, since we have 4046 * previously reserved space in the system space_info and allocated one 4047 * new system chunk if necessary. However there are three exceptions: 4048 * 4049 * 1) We may have enough free space in the system space_info but all the 4050 * existing system block groups have a profile which can not be used 4051 * for extent allocation. 4052 * 4053 * This happens when mounting in degraded mode. For example we have a 4054 * RAID1 filesystem with 2 devices, lose one device and mount the fs 4055 * using the other device in degraded mode. If we then allocate a chunk, 4056 * we may have enough free space in the existing system space_info, but 4057 * none of the block groups can be used for extent allocation since they 4058 * have a RAID1 profile, and because we are in degraded mode with a 4059 * single device, we are forced to allocate a new system chunk with a 4060 * SINGLE profile. Making check_system_chunk() iterate over all system 4061 * block groups and check if they have a usable profile and enough space 4062 * can be slow on very large filesystems, so we tolerate the -ENOSPC and 4063 * try again after forcing allocation of a new system chunk. Like this 4064 * we avoid paying the cost of that search in normal circumstances, when 4065 * we were not mounted in degraded mode; 4066 * 4067 * 2) We had enough free space info the system space_info, and one suitable 4068 * block group to allocate from when we called check_system_chunk() 4069 * above. However right after we called it, the only system block group 4070 * with enough free space got turned into RO mode by a running scrub, 4071 * and in this case we have to allocate a new one and retry. We only 4072 * need do this allocate and retry once, since we have a transaction 4073 * handle and scrub uses the commit root to search for block groups; 4074 * 4075 * 3) We had one system block group with enough free space when we called 4076 * check_system_chunk(), but after that, right before we tried to 4077 * allocate the last extent buffer we needed, a discard operation came 4078 * in and it temporarily removed the last free space entry from the 4079 * block group (discard removes a free space entry, discards it, and 4080 * then adds back the entry to the block group cache). 4081 */ 4082 if (ret == -ENOSPC) { 4083 const u64 sys_flags = btrfs_system_alloc_profile(trans->fs_info); 4084 struct btrfs_block_group *sys_bg; 4085 struct btrfs_space_info *sys_space_info; 4086 4087 sys_space_info = btrfs_find_space_info(trans->fs_info, sys_flags); 4088 if (unlikely(!sys_space_info)) { 4089 ret = -EINVAL; 4090 btrfs_abort_transaction(trans, ret); 4091 goto out; 4092 } 4093 4094 sys_bg = btrfs_create_chunk(trans, sys_space_info, sys_flags); 4095 if (IS_ERR(sys_bg)) { 4096 ret = PTR_ERR(sys_bg); 4097 btrfs_abort_transaction(trans, ret); 4098 goto out; 4099 } 4100 4101 ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg); 4102 if (unlikely(ret)) { 4103 btrfs_abort_transaction(trans, ret); 4104 goto out; 4105 } 4106 4107 ret = btrfs_chunk_alloc_add_chunk_item(trans, bg); 4108 if (unlikely(ret)) { 4109 btrfs_abort_transaction(trans, ret); 4110 goto out; 4111 } 4112 } else if (unlikely(ret)) { 4113 btrfs_abort_transaction(trans, ret); 4114 goto out; 4115 } 4116 out: 4117 btrfs_trans_release_chunk_metadata(trans); 4118 4119 if (ret) 4120 return ERR_PTR(ret); 4121 4122 btrfs_get_block_group(bg); 4123 return bg; 4124 } 4125 4126 /* 4127 * Chunk allocation is done in 2 phases: 4128 * 4129 * 1) Phase 1 - through btrfs_chunk_alloc() we allocate device extents for 4130 * the chunk, the chunk mapping, create its block group and add the items 4131 * that belong in the chunk btree to it - more specifically, we need to 4132 * update device items in the chunk btree and add a new chunk item to it. 4133 * 4134 * 2) Phase 2 - through btrfs_create_pending_block_groups(), we add the block 4135 * group item to the extent btree and the device extent items to the devices 4136 * btree. 4137 * 4138 * This is done to prevent deadlocks. For example when COWing a node from the 4139 * extent btree we are holding a write lock on the node's parent and if we 4140 * trigger chunk allocation and attempted to insert the new block group item 4141 * in the extent btree right way, we could deadlock because the path for the 4142 * insertion can include that parent node. At first glance it seems impossible 4143 * to trigger chunk allocation after starting a transaction since tasks should 4144 * reserve enough transaction units (metadata space), however while that is true 4145 * most of the time, chunk allocation may still be triggered for several reasons: 4146 * 4147 * 1) When reserving metadata, we check if there is enough free space in the 4148 * metadata space_info and therefore don't trigger allocation of a new chunk. 4149 * However later when the task actually tries to COW an extent buffer from 4150 * the extent btree or from the device btree for example, it is forced to 4151 * allocate a new block group (chunk) because the only one that had enough 4152 * free space was just turned to RO mode by a running scrub for example (or 4153 * device replace, block group reclaim thread, etc), so we can not use it 4154 * for allocating an extent and end up being forced to allocate a new one; 4155 * 4156 * 2) Because we only check that the metadata space_info has enough free bytes, 4157 * we end up not allocating a new metadata chunk in that case. However if 4158 * the filesystem was mounted in degraded mode, none of the existing block 4159 * groups might be suitable for extent allocation due to their incompatible 4160 * profile (for e.g. mounting a 2 devices filesystem, where all block groups 4161 * use a RAID1 profile, in degraded mode using a single device). In this case 4162 * when the task attempts to COW some extent buffer of the extent btree for 4163 * example, it will trigger allocation of a new metadata block group with a 4164 * suitable profile (SINGLE profile in the example of the degraded mount of 4165 * the RAID1 filesystem); 4166 * 4167 * 3) The task has reserved enough transaction units / metadata space, but when 4168 * it attempts to COW an extent buffer from the extent or device btree for 4169 * example, it does not find any free extent in any metadata block group, 4170 * therefore forced to try to allocate a new metadata block group. 4171 * This is because some other task allocated all available extents in the 4172 * meanwhile - this typically happens with tasks that don't reserve space 4173 * properly, either intentionally or as a bug. One example where this is 4174 * done intentionally is fsync, as it does not reserve any transaction units 4175 * and ends up allocating a variable number of metadata extents for log 4176 * tree extent buffers; 4177 * 4178 * 4) The task has reserved enough transaction units / metadata space, but right 4179 * before it tries to allocate the last extent buffer it needs, a discard 4180 * operation comes in and, temporarily, removes the last free space entry from 4181 * the only metadata block group that had free space (discard starts by 4182 * removing a free space entry from a block group, then does the discard 4183 * operation and, once it's done, it adds back the free space entry to the 4184 * block group). 4185 * 4186 * We also need this 2 phases setup when adding a device to a filesystem with 4187 * a seed device - we must create new metadata and system chunks without adding 4188 * any of the block group items to the chunk, extent and device btrees. If we 4189 * did not do it this way, we would get ENOSPC when attempting to update those 4190 * btrees, since all the chunks from the seed device are read-only. 4191 * 4192 * Phase 1 does the updates and insertions to the chunk btree because if we had 4193 * it done in phase 2 and have a thundering herd of tasks allocating chunks in 4194 * parallel, we risk having too many system chunks allocated by many tasks if 4195 * many tasks reach phase 1 without the previous ones completing phase 2. In the 4196 * extreme case this leads to exhaustion of the system chunk array in the 4197 * superblock. This is easier to trigger if using a btree node/leaf size of 64K 4198 * and with RAID filesystems (so we have more device items in the chunk btree). 4199 * This has happened before and commit eafa4fd0ad0607 ("btrfs: fix exhaustion of 4200 * the system chunk array due to concurrent allocations") provides more details. 4201 * 4202 * Allocation of system chunks does not happen through this function. A task that 4203 * needs to update the chunk btree (the only btree that uses system chunks), must 4204 * preallocate chunk space by calling either check_system_chunk() or 4205 * btrfs_reserve_chunk_metadata() - the former is used when allocating a data or 4206 * metadata chunk or when removing a chunk, while the later is used before doing 4207 * a modification to the chunk btree - use cases for the later are adding, 4208 * removing and resizing a device as well as relocation of a system chunk. 4209 * See the comment below for more details. 4210 * 4211 * The reservation of system space, done through check_system_chunk(), as well 4212 * as all the updates and insertions into the chunk btree must be done while 4213 * holding fs_info->chunk_mutex. This is important to guarantee that while COWing 4214 * an extent buffer from the chunks btree we never trigger allocation of a new 4215 * system chunk, which would result in a deadlock (trying to lock twice an 4216 * extent buffer of the chunk btree, first time before triggering the chunk 4217 * allocation and the second time during chunk allocation while attempting to 4218 * update the chunks btree). The system chunk array is also updated while holding 4219 * that mutex. The same logic applies to removing chunks - we must reserve system 4220 * space, update the chunk btree and the system chunk array in the superblock 4221 * while holding fs_info->chunk_mutex. 4222 * 4223 * This function, btrfs_chunk_alloc(), belongs to phase 1. 4224 * 4225 * @space_info: specify which space_info the new chunk should belong to. 4226 * 4227 * If @force is CHUNK_ALLOC_FORCE: 4228 * - return 1 if it successfully allocates a chunk, 4229 * - return errors including -ENOSPC otherwise. 4230 * If @force is NOT CHUNK_ALLOC_FORCE: 4231 * - return 0 if it doesn't need to allocate a new chunk, 4232 * - return 1 if it successfully allocates a chunk, 4233 * - return errors including -ENOSPC otherwise. 4234 */ 4235 int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, 4236 struct btrfs_space_info *space_info, u64 flags, 4237 enum btrfs_chunk_alloc_enum force) 4238 { 4239 struct btrfs_fs_info *fs_info = trans->fs_info; 4240 struct btrfs_block_group *ret_bg; 4241 bool wait_for_alloc = false; 4242 bool should_alloc = false; 4243 bool from_extent_allocation = false; 4244 int ret = 0; 4245 4246 if (force == CHUNK_ALLOC_FORCE_FOR_EXTENT) { 4247 from_extent_allocation = true; 4248 force = CHUNK_ALLOC_FORCE; 4249 } 4250 4251 /* Don't re-enter if we're already allocating a chunk */ 4252 if (trans->allocating_chunk) 4253 return -ENOSPC; 4254 /* 4255 * Allocation of system chunks can not happen through this path, as we 4256 * could end up in a deadlock if we are allocating a data or metadata 4257 * chunk and there is another task modifying the chunk btree. 4258 * 4259 * This is because while we are holding the chunk mutex, we will attempt 4260 * to add the new chunk item to the chunk btree or update an existing 4261 * device item in the chunk btree, while the other task that is modifying 4262 * the chunk btree is attempting to COW an extent buffer while holding a 4263 * lock on it and on its parent - if the COW operation triggers a system 4264 * chunk allocation, then we can deadlock because we are holding the 4265 * chunk mutex and we may need to access that extent buffer or its parent 4266 * in order to add the chunk item or update a device item. 4267 * 4268 * Tasks that want to modify the chunk tree should reserve system space 4269 * before updating the chunk btree, by calling either 4270 * btrfs_reserve_chunk_metadata() or check_system_chunk(). 4271 * It's possible that after a task reserves the space, it still ends up 4272 * here - this happens in the cases described above at do_chunk_alloc(). 4273 * The task will have to either retry or fail. 4274 */ 4275 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 4276 return -ENOSPC; 4277 4278 do { 4279 spin_lock(&space_info->lock); 4280 if (force < space_info->force_alloc) 4281 force = space_info->force_alloc; 4282 should_alloc = should_alloc_chunk(fs_info, space_info, force); 4283 if (space_info->full) { 4284 /* No more free physical space */ 4285 spin_unlock(&space_info->lock); 4286 if (should_alloc) 4287 ret = -ENOSPC; 4288 else 4289 ret = 0; 4290 return ret; 4291 } else if (!should_alloc) { 4292 spin_unlock(&space_info->lock); 4293 return 0; 4294 } else if (space_info->chunk_alloc) { 4295 /* 4296 * Someone is already allocating, so we need to block 4297 * until this someone is finished and then loop to 4298 * recheck if we should continue with our allocation 4299 * attempt. 4300 */ 4301 spin_unlock(&space_info->lock); 4302 wait_for_alloc = true; 4303 force = CHUNK_ALLOC_NO_FORCE; 4304 mutex_lock(&fs_info->chunk_mutex); 4305 mutex_unlock(&fs_info->chunk_mutex); 4306 } else { 4307 /* Proceed with allocation */ 4308 space_info->chunk_alloc = true; 4309 spin_unlock(&space_info->lock); 4310 wait_for_alloc = false; 4311 } 4312 4313 cond_resched(); 4314 } while (wait_for_alloc); 4315 4316 mutex_lock(&fs_info->chunk_mutex); 4317 trans->allocating_chunk = true; 4318 4319 /* 4320 * If we have mixed data/metadata chunks we want to make sure we keep 4321 * allocating mixed chunks instead of individual chunks. 4322 */ 4323 if (btrfs_mixed_space_info(space_info)) 4324 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); 4325 4326 /* 4327 * if we're doing a data chunk, go ahead and make sure that 4328 * we keep a reasonable number of metadata chunks allocated in the 4329 * FS as well. 4330 */ 4331 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { 4332 fs_info->data_chunk_allocations++; 4333 if (!(fs_info->data_chunk_allocations % 4334 fs_info->metadata_ratio)) 4335 force_metadata_allocation(fs_info); 4336 } 4337 4338 ret_bg = do_chunk_alloc(trans, space_info, flags); 4339 trans->allocating_chunk = false; 4340 4341 if (IS_ERR(ret_bg)) { 4342 ret = PTR_ERR(ret_bg); 4343 } else if (from_extent_allocation && (flags & BTRFS_BLOCK_GROUP_DATA)) { 4344 /* 4345 * New block group is likely to be used soon. Try to activate 4346 * it now. Failure is OK for now. 4347 */ 4348 btrfs_zone_activate(ret_bg); 4349 } 4350 4351 if (!ret) 4352 btrfs_put_block_group(ret_bg); 4353 4354 spin_lock(&space_info->lock); 4355 if (ret < 0) { 4356 if (ret == -ENOSPC) 4357 space_info->full = true; 4358 else 4359 goto out; 4360 } else { 4361 ret = 1; 4362 space_info->max_extent_size = 0; 4363 } 4364 4365 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; 4366 out: 4367 space_info->chunk_alloc = false; 4368 spin_unlock(&space_info->lock); 4369 mutex_unlock(&fs_info->chunk_mutex); 4370 4371 return ret; 4372 } 4373 4374 static u64 get_profile_num_devs(const struct btrfs_fs_info *fs_info, u64 type) 4375 { 4376 u64 num_dev; 4377 4378 num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max; 4379 if (!num_dev) 4380 num_dev = fs_info->fs_devices->rw_devices; 4381 4382 return num_dev; 4383 } 4384 4385 static void reserve_chunk_space(struct btrfs_trans_handle *trans, 4386 u64 bytes, 4387 u64 type) 4388 { 4389 struct btrfs_fs_info *fs_info = trans->fs_info; 4390 struct btrfs_space_info *info; 4391 u64 left; 4392 int ret = 0; 4393 4394 /* 4395 * Needed because we can end up allocating a system chunk and for an 4396 * atomic and race free space reservation in the chunk block reserve. 4397 */ 4398 lockdep_assert_held(&fs_info->chunk_mutex); 4399 4400 info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); 4401 spin_lock(&info->lock); 4402 left = info->total_bytes - btrfs_space_info_used(info, true); 4403 spin_unlock(&info->lock); 4404 4405 if (left < bytes && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { 4406 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", 4407 left, bytes, type); 4408 btrfs_dump_space_info(info, 0, false); 4409 } 4410 4411 if (left < bytes) { 4412 u64 flags = btrfs_system_alloc_profile(fs_info); 4413 struct btrfs_block_group *bg; 4414 struct btrfs_space_info *space_info; 4415 4416 space_info = btrfs_find_space_info(fs_info, flags); 4417 ASSERT(space_info); 4418 4419 /* 4420 * Ignore failure to create system chunk. We might end up not 4421 * needing it, as we might not need to COW all nodes/leafs from 4422 * the paths we visit in the chunk tree (they were already COWed 4423 * or created in the current transaction for example). 4424 */ 4425 bg = btrfs_create_chunk(trans, space_info, flags); 4426 if (IS_ERR(bg)) { 4427 ret = PTR_ERR(bg); 4428 } else { 4429 /* 4430 * We have a new chunk. We also need to activate it for 4431 * zoned filesystem. 4432 */ 4433 ret = btrfs_zoned_activate_one_bg(info, true); 4434 if (ret < 0) 4435 return; 4436 4437 /* 4438 * If we fail to add the chunk item here, we end up 4439 * trying again at phase 2 of chunk allocation, at 4440 * btrfs_create_pending_block_groups(). So ignore 4441 * any error here. An ENOSPC here could happen, due to 4442 * the cases described at do_chunk_alloc() - the system 4443 * block group we just created was just turned into RO 4444 * mode by a scrub for example, or a running discard 4445 * temporarily removed its free space entries, etc. 4446 */ 4447 btrfs_chunk_alloc_add_chunk_item(trans, bg); 4448 } 4449 } 4450 4451 if (!ret) { 4452 ret = btrfs_block_rsv_add(fs_info, 4453 &fs_info->chunk_block_rsv, 4454 bytes, BTRFS_RESERVE_NO_FLUSH); 4455 if (!ret) 4456 trans->chunk_bytes_reserved += bytes; 4457 } 4458 } 4459 4460 /* 4461 * Reserve space in the system space for allocating or removing a chunk. 4462 * The caller must be holding fs_info->chunk_mutex. 4463 */ 4464 void check_system_chunk(struct btrfs_trans_handle *trans, u64 type) 4465 { 4466 struct btrfs_fs_info *fs_info = trans->fs_info; 4467 const u64 num_devs = get_profile_num_devs(fs_info, type); 4468 u64 bytes; 4469 4470 /* num_devs device items to update and 1 chunk item to add or remove. */ 4471 bytes = btrfs_calc_metadata_size(fs_info, num_devs) + 4472 btrfs_calc_insert_metadata_size(fs_info, 1); 4473 4474 reserve_chunk_space(trans, bytes, type); 4475 } 4476 4477 /* 4478 * Reserve space in the system space, if needed, for doing a modification to the 4479 * chunk btree. 4480 * 4481 * @trans: A transaction handle. 4482 * @is_item_insertion: Indicate if the modification is for inserting a new item 4483 * in the chunk btree or if it's for the deletion or update 4484 * of an existing item. 4485 * 4486 * This is used in a context where we need to update the chunk btree outside 4487 * block group allocation and removal, to avoid a deadlock with a concurrent 4488 * task that is allocating a metadata or data block group and therefore needs to 4489 * update the chunk btree while holding the chunk mutex. After the update to the 4490 * chunk btree is done, btrfs_trans_release_chunk_metadata() should be called. 4491 * 4492 */ 4493 void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans, 4494 bool is_item_insertion) 4495 { 4496 struct btrfs_fs_info *fs_info = trans->fs_info; 4497 u64 bytes; 4498 4499 if (is_item_insertion) 4500 bytes = btrfs_calc_insert_metadata_size(fs_info, 1); 4501 else 4502 bytes = btrfs_calc_metadata_size(fs_info, 1); 4503 4504 mutex_lock(&fs_info->chunk_mutex); 4505 reserve_chunk_space(trans, bytes, BTRFS_BLOCK_GROUP_SYSTEM); 4506 mutex_unlock(&fs_info->chunk_mutex); 4507 } 4508 4509 void btrfs_put_block_group_cache(struct btrfs_fs_info *info) 4510 { 4511 struct btrfs_block_group *block_group; 4512 4513 block_group = btrfs_lookup_first_block_group(info, 0); 4514 while (block_group) { 4515 btrfs_wait_block_group_cache_done(block_group); 4516 spin_lock(&block_group->lock); 4517 if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF, 4518 &block_group->runtime_flags)) { 4519 struct btrfs_inode *inode = block_group->inode; 4520 4521 block_group->inode = NULL; 4522 spin_unlock(&block_group->lock); 4523 4524 ASSERT(block_group->io_ctl.inode == NULL); 4525 iput(&inode->vfs_inode); 4526 } else { 4527 spin_unlock(&block_group->lock); 4528 } 4529 block_group = btrfs_next_block_group(block_group); 4530 } 4531 } 4532 4533 static void check_removing_space_info(struct btrfs_space_info *space_info) 4534 { 4535 struct btrfs_fs_info *info = space_info->fs_info; 4536 4537 if (space_info->subgroup_id == BTRFS_SUB_GROUP_PRIMARY) { 4538 /* This is a top space_info, proceed with its children first. */ 4539 for (int i = 0; i < BTRFS_SPACE_INFO_SUB_GROUP_MAX; i++) { 4540 if (space_info->sub_group[i]) { 4541 check_removing_space_info(space_info->sub_group[i]); 4542 kfree(space_info->sub_group[i]); 4543 space_info->sub_group[i] = NULL; 4544 } 4545 } 4546 } 4547 4548 /* 4549 * Do not hide this behind enospc_debug, this is actually important and 4550 * indicates a real bug if this happens. 4551 */ 4552 if (WARN_ON(space_info->bytes_pinned > 0 || space_info->bytes_may_use > 0)) 4553 btrfs_dump_space_info(space_info, 0, false); 4554 4555 /* 4556 * If there was a failure to cleanup a log tree, very likely due to an 4557 * IO failure on a writeback attempt of one or more of its extent 4558 * buffers, we could not do proper (and cheap) unaccounting of their 4559 * reserved space, so don't warn on bytes_reserved > 0 in that case. 4560 */ 4561 if (!(space_info->flags & BTRFS_BLOCK_GROUP_METADATA) || 4562 !BTRFS_FS_LOG_CLEANUP_ERROR(info)) { 4563 if (WARN_ON(space_info->bytes_reserved > 0)) 4564 btrfs_dump_space_info(space_info, 0, false); 4565 } 4566 4567 WARN_ON(space_info->reclaim_size > 0); 4568 } 4569 4570 /* 4571 * Must be called only after stopping all workers, since we could have block 4572 * group caching kthreads running, and therefore they could race with us if we 4573 * freed the block groups before stopping them. 4574 */ 4575 int btrfs_free_block_groups(struct btrfs_fs_info *info) 4576 { 4577 struct btrfs_block_group *block_group; 4578 struct btrfs_space_info *space_info; 4579 struct btrfs_caching_control *caching_ctl; 4580 struct rb_node *n; 4581 4582 if (btrfs_is_zoned(info)) { 4583 if (info->active_meta_bg) { 4584 btrfs_put_block_group(info->active_meta_bg); 4585 info->active_meta_bg = NULL; 4586 } 4587 if (info->active_system_bg) { 4588 btrfs_put_block_group(info->active_system_bg); 4589 info->active_system_bg = NULL; 4590 } 4591 } 4592 4593 write_lock(&info->block_group_cache_lock); 4594 while (!list_empty(&info->caching_block_groups)) { 4595 caching_ctl = list_first_entry(&info->caching_block_groups, 4596 struct btrfs_caching_control, list); 4597 list_del(&caching_ctl->list); 4598 btrfs_put_caching_control(caching_ctl); 4599 } 4600 write_unlock(&info->block_group_cache_lock); 4601 4602 spin_lock(&info->unused_bgs_lock); 4603 while (!list_empty(&info->unused_bgs)) { 4604 block_group = list_first_entry(&info->unused_bgs, 4605 struct btrfs_block_group, 4606 bg_list); 4607 list_del_init(&block_group->bg_list); 4608 btrfs_put_block_group(block_group); 4609 } 4610 4611 while (!list_empty(&info->reclaim_bgs)) { 4612 block_group = list_first_entry(&info->reclaim_bgs, 4613 struct btrfs_block_group, 4614 bg_list); 4615 list_del_init(&block_group->bg_list); 4616 btrfs_put_block_group(block_group); 4617 } 4618 4619 while (!list_empty(&info->fully_remapped_bgs)) { 4620 block_group = list_first_entry(&info->fully_remapped_bgs, 4621 struct btrfs_block_group, bg_list); 4622 list_del_init(&block_group->bg_list); 4623 btrfs_put_block_group(block_group); 4624 } 4625 spin_unlock(&info->unused_bgs_lock); 4626 4627 spin_lock(&info->zone_active_bgs_lock); 4628 while (!list_empty(&info->zone_active_bgs)) { 4629 block_group = list_first_entry(&info->zone_active_bgs, 4630 struct btrfs_block_group, 4631 active_bg_list); 4632 list_del_init(&block_group->active_bg_list); 4633 btrfs_put_block_group(block_group); 4634 } 4635 spin_unlock(&info->zone_active_bgs_lock); 4636 4637 write_lock(&info->block_group_cache_lock); 4638 while ((n = rb_last(&info->block_group_cache_tree.rb_root)) != NULL) { 4639 block_group = rb_entry(n, struct btrfs_block_group, 4640 cache_node); 4641 rb_erase_cached(&block_group->cache_node, 4642 &info->block_group_cache_tree); 4643 RB_CLEAR_NODE(&block_group->cache_node); 4644 write_unlock(&info->block_group_cache_lock); 4645 4646 down_write(&block_group->space_info->groups_sem); 4647 list_del(&block_group->list); 4648 up_write(&block_group->space_info->groups_sem); 4649 4650 /* 4651 * We haven't cached this block group, which means we could 4652 * possibly have excluded extents on this block group. 4653 */ 4654 if (block_group->cached == BTRFS_CACHE_NO || 4655 block_group->cached == BTRFS_CACHE_ERROR) 4656 btrfs_free_excluded_extents(block_group); 4657 4658 btrfs_remove_free_space_cache(block_group); 4659 ASSERT(block_group->cached != BTRFS_CACHE_STARTED); 4660 ASSERT(list_empty(&block_group->dirty_list)); 4661 ASSERT(list_empty(&block_group->io_list)); 4662 ASSERT(list_empty(&block_group->bg_list)); 4663 ASSERT(refcount_read(&block_group->refs) == 1); 4664 ASSERT(block_group->swap_extents == 0); 4665 btrfs_put_block_group(block_group); 4666 4667 write_lock(&info->block_group_cache_lock); 4668 } 4669 write_unlock(&info->block_group_cache_lock); 4670 4671 btrfs_release_global_block_rsv(info); 4672 4673 while (!list_empty(&info->space_info)) { 4674 space_info = list_first_entry(&info->space_info, 4675 struct btrfs_space_info, list); 4676 4677 check_removing_space_info(space_info); 4678 list_del(&space_info->list); 4679 btrfs_sysfs_remove_space_info(space_info); 4680 } 4681 return 0; 4682 } 4683 4684 void btrfs_freeze_block_group(struct btrfs_block_group *cache) 4685 { 4686 atomic_inc(&cache->frozen); 4687 } 4688 4689 void btrfs_unfreeze_block_group(struct btrfs_block_group *block_group) 4690 { 4691 struct btrfs_fs_info *fs_info = block_group->fs_info; 4692 bool cleanup; 4693 4694 spin_lock(&block_group->lock); 4695 cleanup = (atomic_dec_and_test(&block_group->frozen) && 4696 test_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags)); 4697 spin_unlock(&block_group->lock); 4698 4699 if (cleanup) { 4700 struct btrfs_chunk_map *map; 4701 4702 map = btrfs_find_chunk_map(fs_info, block_group->start, 1); 4703 /* Logic error, can't happen. */ 4704 ASSERT(map); 4705 4706 btrfs_remove_chunk_map(fs_info, map); 4707 4708 /* Once for our lookup reference. */ 4709 btrfs_free_chunk_map(map); 4710 4711 /* 4712 * We may have left one free space entry and other possible 4713 * tasks trimming this block group have left 1 entry each one. 4714 * Free them if any. 4715 */ 4716 btrfs_remove_free_space_cache(block_group); 4717 } 4718 } 4719 4720 bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg) 4721 { 4722 bool ret = true; 4723 4724 spin_lock(&bg->lock); 4725 if (bg->ro) 4726 ret = false; 4727 else 4728 bg->swap_extents++; 4729 spin_unlock(&bg->lock); 4730 4731 return ret; 4732 } 4733 4734 void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount) 4735 { 4736 spin_lock(&bg->lock); 4737 ASSERT(!bg->ro); 4738 ASSERT(bg->swap_extents >= amount); 4739 bg->swap_extents -= amount; 4740 spin_unlock(&bg->lock); 4741 } 4742 4743 enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size) 4744 { 4745 if (size <= SZ_128K) 4746 return BTRFS_BG_SZ_SMALL; 4747 if (size <= SZ_8M) 4748 return BTRFS_BG_SZ_MEDIUM; 4749 return BTRFS_BG_SZ_LARGE; 4750 } 4751 4752 /* 4753 * Handle a block group allocating an extent in a size class 4754 * 4755 * @bg: The block group we allocated in. 4756 * @size_class: The size class of the allocation. 4757 * @force_wrong_size_class: Whether we are desperate enough to allow 4758 * mismatched size classes. 4759 * 4760 * Returns: 0 if the size class was valid for this block_group, -EAGAIN in the 4761 * case of a race that leads to the wrong size class without 4762 * force_wrong_size_class set. 4763 * 4764 * find_free_extent will skip block groups with a mismatched size class until 4765 * it really needs to avoid ENOSPC. In that case it will set 4766 * force_wrong_size_class. However, if a block group is newly allocated and 4767 * doesn't yet have a size class, then it is possible for two allocations of 4768 * different sizes to race and both try to use it. The loser is caught here and 4769 * has to retry. 4770 */ 4771 int btrfs_use_block_group_size_class(struct btrfs_block_group *bg, 4772 enum btrfs_block_group_size_class size_class, 4773 bool force_wrong_size_class) 4774 { 4775 lockdep_assert_held(&bg->lock); 4776 ASSERT(size_class != BTRFS_BG_SZ_NONE); 4777 4778 /* The new allocation is in the right size class, do nothing */ 4779 if (bg->size_class == size_class) 4780 return 0; 4781 /* 4782 * The new allocation is in a mismatched size class. 4783 * This means one of two things: 4784 * 4785 * 1. Two tasks in find_free_extent for different size_classes raced 4786 * and hit the same empty block_group. Make the loser try again. 4787 * 2. A call to find_free_extent got desperate enough to set 4788 * 'force_wrong_slab'. Don't change the size_class, but allow the 4789 * allocation. 4790 */ 4791 if (bg->size_class != BTRFS_BG_SZ_NONE) { 4792 if (force_wrong_size_class) 4793 return 0; 4794 return -EAGAIN; 4795 } 4796 /* 4797 * The happy new block group case: the new allocation is the first 4798 * one in the block_group so we set size_class. 4799 */ 4800 bg->size_class = size_class; 4801 4802 return 0; 4803 } 4804 4805 bool btrfs_block_group_should_use_size_class(const struct btrfs_block_group *bg) 4806 { 4807 if (btrfs_is_zoned(bg->fs_info)) 4808 return false; 4809 if (!btrfs_is_block_group_data_only(bg)) 4810 return false; 4811 return true; 4812 } 4813 4814 void btrfs_mark_bg_fully_remapped(struct btrfs_block_group *bg, 4815 struct btrfs_trans_handle *trans) 4816 { 4817 struct btrfs_fs_info *fs_info = trans->fs_info; 4818 4819 4820 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) { 4821 spin_lock(&bg->lock); 4822 set_bit(BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING, &bg->runtime_flags); 4823 spin_unlock(&bg->lock); 4824 4825 btrfs_discard_queue_work(&fs_info->discard_ctl, bg); 4826 } else { 4827 spin_lock(&fs_info->unused_bgs_lock); 4828 /* 4829 * The block group might already be on the unused_bgs list, 4830 * remove it if it is. It'll get readded after 4831 * btrfs_handle_fully_remapped_bgs() finishes. 4832 */ 4833 if (!list_empty(&bg->bg_list)) 4834 list_del(&bg->bg_list); 4835 else 4836 btrfs_get_block_group(bg); 4837 4838 list_add_tail(&bg->bg_list, &fs_info->fully_remapped_bgs); 4839 spin_unlock(&fs_info->unused_bgs_lock); 4840 } 4841 } 4842 4843 /* 4844 * Compare the block group and chunk trees, and find any fully-remapped block 4845 * groups which haven't yet had their chunk stripes and device extents removed, 4846 * and put them on the fully_remapped_bgs list so this gets done. 4847 * 4848 * This happens when a block group becomes fully remapped, i.e. its last 4849 * identity mapping is removed, and the volume is unmounted before async 4850 * discard has finished. It's important this gets done as until it is the 4851 * chunk's stripes are dead space. 4852 */ 4853 int btrfs_populate_fully_remapped_bgs_list(struct btrfs_fs_info *fs_info) 4854 { 4855 struct rb_node *node_bg, *node_chunk; 4856 4857 node_bg = rb_first_cached(&fs_info->block_group_cache_tree); 4858 node_chunk = rb_first_cached(&fs_info->mapping_tree); 4859 4860 while (node_bg && node_chunk) { 4861 struct btrfs_block_group *bg; 4862 struct btrfs_chunk_map *map; 4863 4864 bg = rb_entry(node_bg, struct btrfs_block_group, cache_node); 4865 map = rb_entry(node_chunk, struct btrfs_chunk_map, rb_node); 4866 4867 ASSERT(bg->start == map->start); 4868 4869 if (!(bg->flags & BTRFS_BLOCK_GROUP_REMAPPED)) 4870 goto next; 4871 4872 if (bg->identity_remap_count != 0) 4873 goto next; 4874 4875 if (map->num_stripes == 0) 4876 goto next; 4877 4878 spin_lock(&fs_info->unused_bgs_lock); 4879 4880 if (list_empty(&bg->bg_list)) { 4881 btrfs_get_block_group(bg); 4882 list_add_tail(&bg->bg_list, &fs_info->fully_remapped_bgs); 4883 } else { 4884 list_move_tail(&bg->bg_list, &fs_info->fully_remapped_bgs); 4885 } 4886 4887 spin_unlock(&fs_info->unused_bgs_lock); 4888 4889 /* 4890 * Ideally we'd want to call btrfs_discard_queue_work() here, 4891 * but it'd do nothing as the discard worker hasn't been 4892 * started yet. 4893 * 4894 * The block group will get added to the discard list when 4895 * btrfs_handle_fully_remapped_bgs() gets called, when we 4896 * commit the first transaction. 4897 */ 4898 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) { 4899 spin_lock(&bg->lock); 4900 set_bit(BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING, &bg->runtime_flags); 4901 spin_unlock(&bg->lock); 4902 } 4903 4904 next: 4905 node_bg = rb_next(node_bg); 4906 node_chunk = rb_next(node_chunk); 4907 } 4908 4909 ASSERT(!node_bg && !node_chunk); 4910 4911 return 0; 4912 } 4913