1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/bitops.h> 4 #include <linux/slab.h> 5 #include <linux/blkdev.h> 6 #include <linux/sched/mm.h> 7 #include <linux/atomic.h> 8 #include <linux/vmalloc.h> 9 #include "ctree.h" 10 #include "volumes.h" 11 #include "zoned.h" 12 #include "disk-io.h" 13 #include "block-group.h" 14 #include "dev-replace.h" 15 #include "space-info.h" 16 #include "fs.h" 17 #include "accessors.h" 18 #include "bio.h" 19 #include "transaction.h" 20 #include "sysfs.h" 21 22 /* Maximum number of zones to report per blkdev_report_zones() call */ 23 #define BTRFS_REPORT_NR_ZONES 4096 24 /* Invalid allocation pointer value for missing devices */ 25 #define WP_MISSING_DEV ((u64)-1) 26 /* Pseudo write pointer value for conventional zone */ 27 #define WP_CONVENTIONAL ((u64)-2) 28 29 /* 30 * Location of the first zone of superblock logging zone pairs. 31 * 32 * - primary superblock: 0B (zone 0) 33 * - first copy: 512G (zone starting at that offset) 34 * - second copy: 4T (zone starting at that offset) 35 */ 36 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL) 37 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G) 38 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G) 39 40 #define BTRFS_SB_LOG_FIRST_SHIFT ilog2(BTRFS_SB_LOG_FIRST_OFFSET) 41 #define BTRFS_SB_LOG_SECOND_SHIFT ilog2(BTRFS_SB_LOG_SECOND_OFFSET) 42 43 /* Number of superblock log zones */ 44 #define BTRFS_NR_SB_LOG_ZONES 2 45 46 /* Default number of max active zones when the device has no limits. */ 47 #define BTRFS_DEFAULT_MAX_ACTIVE_ZONES 128 48 49 /* 50 * Minimum of active zones we need: 51 * 52 * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors 53 * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group 54 * - 1 zone for tree-log dedicated block group 55 * - 1 zone for relocation 56 */ 57 #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5) 58 59 /* 60 * Minimum / maximum supported zone size. Currently, SMR disks have a zone 61 * size of 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range. 62 * We do not expect the zone size to become larger than 8GiB or smaller than 63 * 4MiB in the near future. 64 */ 65 #define BTRFS_MAX_ZONE_SIZE SZ_8G 66 #define BTRFS_MIN_ZONE_SIZE SZ_4M 67 68 #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT) 69 70 static void wait_eb_writebacks(struct btrfs_block_group *block_group); 71 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written); 72 73 static inline bool sb_zone_is_full(const struct blk_zone *zone) 74 { 75 return (zone->cond == BLK_ZONE_COND_FULL) || 76 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity); 77 } 78 79 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data) 80 { 81 struct blk_zone *zones = data; 82 83 memcpy(&zones[idx], zone, sizeof(*zone)); 84 85 return 0; 86 } 87 88 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones, 89 u64 *wp_ret) 90 { 91 bool empty[BTRFS_NR_SB_LOG_ZONES]; 92 bool full[BTRFS_NR_SB_LOG_ZONES]; 93 sector_t sector; 94 95 for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) { 96 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL, 97 "zones[%d].type=%d", i, zones[i].type); 98 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY); 99 full[i] = sb_zone_is_full(&zones[i]); 100 } 101 102 /* 103 * Possible states of log buffer zones 104 * 105 * Empty[0] In use[0] Full[0] 106 * Empty[1] * 0 1 107 * In use[1] x x 1 108 * Full[1] 0 0 C 109 * 110 * Log position: 111 * *: Special case, no superblock is written 112 * 0: Use write pointer of zones[0] 113 * 1: Use write pointer of zones[1] 114 * C: Compare super blocks from zones[0] and zones[1], use the latest 115 * one determined by generation 116 * x: Invalid state 117 */ 118 119 if (empty[0] && empty[1]) { 120 /* Special case to distinguish no superblock to read */ 121 *wp_ret = zones[0].start << SECTOR_SHIFT; 122 return -ENOENT; 123 } else if (full[0] && full[1]) { 124 /* Compare two super blocks */ 125 struct address_space *mapping = bdev->bd_mapping; 126 struct page *page[BTRFS_NR_SB_LOG_ZONES]; 127 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES]; 128 129 for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) { 130 u64 zone_end = (zones[i].start + zones[i].capacity) << SECTOR_SHIFT; 131 u64 bytenr = ALIGN_DOWN(zone_end, BTRFS_SUPER_INFO_SIZE) - 132 BTRFS_SUPER_INFO_SIZE; 133 134 page[i] = read_cache_page_gfp(mapping, 135 bytenr >> PAGE_SHIFT, GFP_NOFS); 136 if (IS_ERR(page[i])) { 137 if (i == 1) 138 btrfs_release_disk_super(super[0]); 139 return PTR_ERR(page[i]); 140 } 141 super[i] = page_address(page[i]); 142 } 143 144 if (btrfs_super_generation(super[0]) > 145 btrfs_super_generation(super[1])) 146 sector = zones[1].start; 147 else 148 sector = zones[0].start; 149 150 for (int i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) 151 btrfs_release_disk_super(super[i]); 152 } else if (!full[0] && (empty[1] || full[1])) { 153 sector = zones[0].wp; 154 } else if (full[0]) { 155 sector = zones[1].wp; 156 } else { 157 return -EUCLEAN; 158 } 159 *wp_ret = sector << SECTOR_SHIFT; 160 return 0; 161 } 162 163 /* 164 * Get the first zone number of the superblock mirror 165 */ 166 static inline u32 sb_zone_number(int shift, int mirror) 167 { 168 u64 zone = U64_MAX; 169 170 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX, "mirror=%d", mirror); 171 switch (mirror) { 172 case 0: zone = 0; break; 173 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break; 174 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break; 175 } 176 177 ASSERT(zone <= U32_MAX, "zone=%llu", zone); 178 179 return (u32)zone; 180 } 181 182 static inline sector_t zone_start_sector(u32 zone_number, 183 struct block_device *bdev) 184 { 185 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev)); 186 } 187 188 static inline u64 zone_start_physical(u32 zone_number, 189 struct btrfs_zoned_device_info *zone_info) 190 { 191 return (u64)zone_number << zone_info->zone_size_shift; 192 } 193 194 /* 195 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block 196 * device into static sized chunks and fake a conventional zone on each of 197 * them. 198 */ 199 static int emulate_report_zones(struct btrfs_device *device, u64 pos, 200 struct blk_zone *zones, unsigned int nr_zones) 201 { 202 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT; 203 sector_t bdev_size = bdev_nr_sectors(device->bdev); 204 unsigned int i; 205 206 pos >>= SECTOR_SHIFT; 207 for (i = 0; i < nr_zones; i++) { 208 zones[i].start = i * zone_sectors + pos; 209 zones[i].len = zone_sectors; 210 zones[i].capacity = zone_sectors; 211 zones[i].wp = zones[i].start + zone_sectors; 212 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL; 213 zones[i].cond = BLK_ZONE_COND_NOT_WP; 214 215 if (zones[i].wp >= bdev_size) { 216 i++; 217 break; 218 } 219 } 220 221 return i; 222 } 223 224 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos, 225 struct blk_zone *zones, unsigned int *nr_zones) 226 { 227 struct btrfs_zoned_device_info *zinfo = device->zone_info; 228 int ret; 229 230 if (!*nr_zones) 231 return 0; 232 233 if (!bdev_is_zoned(device->bdev)) { 234 ret = emulate_report_zones(device, pos, zones, *nr_zones); 235 *nr_zones = ret; 236 return 0; 237 } 238 239 /* Check cache */ 240 if (zinfo->zone_cache) { 241 unsigned int i; 242 u32 zno; 243 244 ASSERT(IS_ALIGNED(pos, zinfo->zone_size), 245 "pos=%llu zinfo->zone_size=%llu", pos, zinfo->zone_size); 246 zno = pos >> zinfo->zone_size_shift; 247 /* 248 * We cannot report zones beyond the zone end. So, it is OK to 249 * cap *nr_zones to at the end. 250 */ 251 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno); 252 253 for (i = 0; i < *nr_zones; i++) { 254 struct blk_zone *zone_info; 255 256 zone_info = &zinfo->zone_cache[zno + i]; 257 if (!zone_info->len) 258 break; 259 } 260 261 if (i == *nr_zones) { 262 /* Cache hit on all the zones */ 263 memcpy(zones, zinfo->zone_cache + zno, 264 sizeof(*zinfo->zone_cache) * *nr_zones); 265 return 0; 266 } 267 } 268 269 ret = blkdev_report_zones_cached(device->bdev, pos >> SECTOR_SHIFT, 270 *nr_zones, copy_zone_info_cb, zones); 271 if (ret < 0) { 272 btrfs_err(device->fs_info, 273 "zoned: failed to read zone %llu on %s (devid %llu)", 274 pos, rcu_dereference(device->name), 275 device->devid); 276 return ret; 277 } 278 *nr_zones = ret; 279 if (unlikely(!ret)) 280 return -EIO; 281 282 /* Populate cache */ 283 if (zinfo->zone_cache) { 284 u32 zno = pos >> zinfo->zone_size_shift; 285 286 memcpy(zinfo->zone_cache + zno, zones, 287 sizeof(*zinfo->zone_cache) * *nr_zones); 288 } 289 290 return 0; 291 } 292 293 /* The emulated zone size is determined from the size of device extent */ 294 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info) 295 { 296 BTRFS_PATH_AUTO_FREE(path); 297 struct btrfs_root *root = fs_info->dev_root; 298 struct btrfs_key key; 299 struct extent_buffer *leaf; 300 struct btrfs_dev_extent *dext; 301 int ret = 0; 302 303 key.objectid = 1; 304 key.type = BTRFS_DEV_EXTENT_KEY; 305 key.offset = 0; 306 307 path = btrfs_alloc_path(); 308 if (!path) 309 return -ENOMEM; 310 311 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 312 if (ret < 0) 313 return ret; 314 315 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 316 ret = btrfs_next_leaf(root, path); 317 if (ret < 0) 318 return ret; 319 /* No dev extents at all? Not good */ 320 if (unlikely(ret > 0)) 321 return -EUCLEAN; 322 } 323 324 leaf = path->nodes[0]; 325 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent); 326 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext); 327 return 0; 328 } 329 330 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info) 331 { 332 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 333 struct btrfs_device *device; 334 int ret = 0; 335 336 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */ 337 if (!btrfs_fs_incompat(fs_info, ZONED)) 338 return 0; 339 340 /* 341 * No need to take the device_list mutex here, we're still in the mount 342 * path and devices cannot be added to or removed from the list yet. 343 */ 344 list_for_each_entry(device, &fs_devices->devices, dev_list) { 345 /* We can skip reading of zone info for missing devices */ 346 if (!device->bdev) 347 continue; 348 349 ret = btrfs_get_dev_zone_info(device, true); 350 if (ret) 351 break; 352 } 353 354 return ret; 355 } 356 357 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache) 358 { 359 struct btrfs_fs_info *fs_info = device->fs_info; 360 struct btrfs_zoned_device_info *zone_info = NULL; 361 struct block_device *bdev = device->bdev; 362 unsigned int max_active_zones; 363 unsigned int nactive; 364 sector_t nr_sectors; 365 sector_t sector = 0; 366 struct blk_zone *zones = NULL; 367 unsigned int i, nreported = 0, nr_zones; 368 sector_t zone_sectors; 369 char *model, *emulated; 370 int ret; 371 372 /* 373 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not 374 * yet be set. 375 */ 376 if (!btrfs_fs_incompat(fs_info, ZONED)) 377 return 0; 378 379 if (device->zone_info) 380 return 0; 381 382 zone_info = kzalloc_obj(*zone_info); 383 if (!zone_info) 384 return -ENOMEM; 385 386 device->zone_info = zone_info; 387 388 if (!bdev_is_zoned(bdev)) { 389 if (!fs_info->zone_size) { 390 ret = calculate_emulated_zone_size(fs_info); 391 if (ret) 392 goto out; 393 } 394 395 ASSERT(fs_info->zone_size); 396 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT; 397 } else { 398 zone_sectors = bdev_zone_sectors(bdev); 399 } 400 401 ASSERT(is_power_of_two_u64(zone_sectors)); 402 zone_info->zone_size = zone_sectors << SECTOR_SHIFT; 403 404 /* We reject devices with a zone size larger than 8GB */ 405 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) { 406 btrfs_err(fs_info, 407 "zoned: %s: zone size %llu larger than supported maximum %llu", 408 rcu_dereference(device->name), 409 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE); 410 ret = -EINVAL; 411 goto out; 412 } else if (zone_info->zone_size < BTRFS_MIN_ZONE_SIZE) { 413 btrfs_err(fs_info, 414 "zoned: %s: zone size %llu smaller than supported minimum %u", 415 rcu_dereference(device->name), 416 zone_info->zone_size, BTRFS_MIN_ZONE_SIZE); 417 ret = -EINVAL; 418 goto out; 419 } 420 421 nr_sectors = bdev_nr_sectors(bdev); 422 zone_info->zone_size_shift = ilog2(zone_info->zone_size); 423 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors); 424 if (!IS_ALIGNED(nr_sectors, zone_sectors)) 425 zone_info->nr_zones++; 426 427 max_active_zones = min_not_zero(bdev_max_active_zones(bdev), 428 bdev_max_open_zones(bdev)); 429 if (!max_active_zones && zone_info->nr_zones > BTRFS_DEFAULT_MAX_ACTIVE_ZONES) 430 max_active_zones = BTRFS_DEFAULT_MAX_ACTIVE_ZONES; 431 if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) { 432 btrfs_err(fs_info, 433 "zoned: %s: max active zones %u is too small, need at least %u active zones", 434 rcu_dereference(device->name), max_active_zones, 435 BTRFS_MIN_ACTIVE_ZONES); 436 ret = -EINVAL; 437 goto out; 438 } 439 zone_info->max_active_zones = max_active_zones; 440 441 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); 442 if (!zone_info->seq_zones) { 443 ret = -ENOMEM; 444 goto out; 445 } 446 447 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); 448 if (!zone_info->empty_zones) { 449 ret = -ENOMEM; 450 goto out; 451 } 452 453 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); 454 if (!zone_info->active_zones) { 455 ret = -ENOMEM; 456 goto out; 457 } 458 459 zones = kvzalloc_objs(struct blk_zone, BTRFS_REPORT_NR_ZONES); 460 if (!zones) { 461 ret = -ENOMEM; 462 goto out; 463 } 464 465 /* 466 * Enable zone cache only for a zoned device. On a non-zoned device, we 467 * fill the zone info with emulated CONVENTIONAL zones, so no need to 468 * use the cache. 469 */ 470 if (populate_cache && bdev_is_zoned(device->bdev)) { 471 zone_info->zone_cache = vcalloc(zone_info->nr_zones, 472 sizeof(struct blk_zone)); 473 if (!zone_info->zone_cache) { 474 btrfs_err(device->fs_info, 475 "zoned: failed to allocate zone cache for %s", 476 rcu_dereference(device->name)); 477 ret = -ENOMEM; 478 goto out; 479 } 480 } 481 482 /* Get zones type */ 483 nactive = 0; 484 while (sector < nr_sectors) { 485 nr_zones = BTRFS_REPORT_NR_ZONES; 486 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones, 487 &nr_zones); 488 if (ret) 489 goto out; 490 491 for (i = 0; i < nr_zones; i++) { 492 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ) 493 __set_bit(nreported, zone_info->seq_zones); 494 switch (zones[i].cond) { 495 case BLK_ZONE_COND_EMPTY: 496 __set_bit(nreported, zone_info->empty_zones); 497 break; 498 case BLK_ZONE_COND_IMP_OPEN: 499 case BLK_ZONE_COND_EXP_OPEN: 500 case BLK_ZONE_COND_CLOSED: 501 case BLK_ZONE_COND_ACTIVE: 502 __set_bit(nreported, zone_info->active_zones); 503 nactive++; 504 break; 505 } 506 nreported++; 507 } 508 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len; 509 } 510 511 if (unlikely(nreported != zone_info->nr_zones)) { 512 btrfs_err(device->fs_info, 513 "inconsistent number of zones on %s (%u/%u)", 514 rcu_dereference(device->name), nreported, 515 zone_info->nr_zones); 516 ret = -EIO; 517 goto out; 518 } 519 520 if (max_active_zones) { 521 if (unlikely(nactive > max_active_zones)) { 522 if (bdev_max_active_zones(bdev) == 0) { 523 max_active_zones = 0; 524 zone_info->max_active_zones = 0; 525 goto validate; 526 } 527 btrfs_err(device->fs_info, 528 "zoned: %u active zones on %s exceeds max_active_zones %u", 529 nactive, rcu_dereference(device->name), 530 max_active_zones); 531 ret = -EIO; 532 goto out; 533 } 534 atomic_set(&zone_info->active_zones_left, 535 max_active_zones - nactive); 536 set_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags); 537 } 538 539 validate: 540 /* Validate superblock log */ 541 nr_zones = BTRFS_NR_SB_LOG_ZONES; 542 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 543 u32 sb_zone; 544 u64 sb_wp; 545 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i; 546 547 sb_zone = sb_zone_number(zone_info->zone_size_shift, i); 548 if (sb_zone + 1 >= zone_info->nr_zones) 549 continue; 550 551 ret = btrfs_get_dev_zones(device, 552 zone_start_physical(sb_zone, zone_info), 553 &zone_info->sb_zones[sb_pos], 554 &nr_zones); 555 if (ret) 556 goto out; 557 558 if (unlikely(nr_zones != BTRFS_NR_SB_LOG_ZONES)) { 559 btrfs_err(device->fs_info, 560 "zoned: failed to read super block log zone info at devid %llu zone %u", 561 device->devid, sb_zone); 562 ret = -EUCLEAN; 563 goto out; 564 } 565 566 /* 567 * If zones[0] is conventional, always use the beginning of the 568 * zone to record superblock. No need to validate in that case. 569 */ 570 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type == 571 BLK_ZONE_TYPE_CONVENTIONAL) 572 continue; 573 574 ret = sb_write_pointer(device->bdev, 575 &zone_info->sb_zones[sb_pos], &sb_wp); 576 if (unlikely(ret != -ENOENT && ret)) { 577 btrfs_err(device->fs_info, 578 "zoned: super block log zone corrupted devid %llu zone %u", 579 device->devid, sb_zone); 580 ret = -EUCLEAN; 581 goto out; 582 } 583 } 584 585 586 kvfree(zones); 587 588 if (bdev_is_zoned(bdev)) { 589 model = "host-managed zoned"; 590 emulated = ""; 591 } else { 592 model = "regular"; 593 emulated = "emulated "; 594 } 595 596 btrfs_info(fs_info, 597 "%s block device %s, %u %szones of %llu bytes", 598 model, rcu_dereference(device->name), zone_info->nr_zones, 599 emulated, zone_info->zone_size); 600 601 return 0; 602 603 out: 604 kvfree(zones); 605 btrfs_destroy_dev_zone_info(device); 606 return ret; 607 } 608 609 void btrfs_destroy_dev_zone_info(struct btrfs_device *device) 610 { 611 struct btrfs_zoned_device_info *zone_info = device->zone_info; 612 613 if (!zone_info) 614 return; 615 616 bitmap_free(zone_info->active_zones); 617 bitmap_free(zone_info->seq_zones); 618 bitmap_free(zone_info->empty_zones); 619 vfree(zone_info->zone_cache); 620 kfree(zone_info); 621 device->zone_info = NULL; 622 } 623 624 struct btrfs_zoned_device_info *btrfs_clone_dev_zone_info(struct btrfs_device *orig_dev) 625 { 626 struct btrfs_zoned_device_info *zone_info; 627 628 zone_info = kmemdup(orig_dev->zone_info, sizeof(*zone_info), GFP_KERNEL); 629 if (!zone_info) 630 return NULL; 631 632 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); 633 if (!zone_info->seq_zones) 634 goto out; 635 636 bitmap_copy(zone_info->seq_zones, orig_dev->zone_info->seq_zones, 637 zone_info->nr_zones); 638 639 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); 640 if (!zone_info->empty_zones) 641 goto out; 642 643 bitmap_copy(zone_info->empty_zones, orig_dev->zone_info->empty_zones, 644 zone_info->nr_zones); 645 646 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL); 647 if (!zone_info->active_zones) 648 goto out; 649 650 bitmap_copy(zone_info->active_zones, orig_dev->zone_info->active_zones, 651 zone_info->nr_zones); 652 zone_info->zone_cache = NULL; 653 654 return zone_info; 655 656 out: 657 bitmap_free(zone_info->seq_zones); 658 bitmap_free(zone_info->empty_zones); 659 bitmap_free(zone_info->active_zones); 660 kfree(zone_info); 661 return NULL; 662 } 663 664 static int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, struct blk_zone *zone) 665 { 666 unsigned int nr_zones = 1; 667 int ret; 668 669 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones); 670 if (ret != 0 || !nr_zones) 671 return ret ? ret : -EIO; 672 673 return 0; 674 } 675 676 static int btrfs_check_for_zoned_device(struct btrfs_fs_info *fs_info) 677 { 678 struct btrfs_device *device; 679 680 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { 681 if (device->bdev && bdev_is_zoned(device->bdev)) { 682 btrfs_err(fs_info, 683 "zoned: mode not enabled but zoned device found: %pg", 684 device->bdev); 685 return -EINVAL; 686 } 687 } 688 689 return 0; 690 } 691 692 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info) 693 { 694 struct queue_limits *lim = &fs_info->limits; 695 struct btrfs_device *device; 696 u64 zone_size = 0; 697 int ret; 698 699 /* 700 * Host-Managed devices can't be used without the ZONED flag. With the 701 * ZONED all devices can be used, using zone emulation if required. 702 */ 703 if (!btrfs_fs_incompat(fs_info, ZONED)) 704 return btrfs_check_for_zoned_device(fs_info); 705 706 blk_set_stacking_limits(lim); 707 708 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { 709 struct btrfs_zoned_device_info *zone_info = device->zone_info; 710 711 if (!device->bdev) 712 continue; 713 714 if (!zone_size) { 715 zone_size = zone_info->zone_size; 716 } else if (zone_info->zone_size != zone_size) { 717 btrfs_err(fs_info, 718 "zoned: unequal block device zone sizes: have %llu found %llu", 719 zone_info->zone_size, zone_size); 720 return -EINVAL; 721 } 722 723 /* 724 * With the zoned emulation, we can have non-zoned device on the 725 * zoned mode. In this case, we don't have a valid max zone 726 * append size. 727 */ 728 if (bdev_is_zoned(device->bdev)) 729 blk_stack_limits(lim, bdev_limits(device->bdev), 0); 730 } 731 732 ret = blk_validate_limits(lim); 733 if (ret) { 734 btrfs_err(fs_info, "zoned: failed to validate queue limits"); 735 return ret; 736 } 737 738 /* 739 * stripe_size is always aligned to BTRFS_STRIPE_LEN in 740 * btrfs_create_chunk(). Since we want stripe_len == zone_size, 741 * check the alignment here. 742 */ 743 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) { 744 btrfs_err(fs_info, 745 "zoned: zone size %llu not aligned to stripe %u", 746 zone_size, BTRFS_STRIPE_LEN); 747 return -EINVAL; 748 } 749 750 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { 751 btrfs_err(fs_info, "zoned: mixed block groups not supported"); 752 return -EINVAL; 753 } 754 755 fs_info->zone_size = zone_size; 756 /* 757 * Also limit max_zone_append_size by max_segments * PAGE_SIZE. 758 * Technically, we can have multiple pages per segment. But, since 759 * we add the pages one by one to a bio, and cannot increase the 760 * metadata reservation even if it increases the number of extents, it 761 * is safe to stick with the limit. 762 */ 763 fs_info->max_zone_append_size = ALIGN_DOWN( 764 min3((u64)lim->max_zone_append_sectors << SECTOR_SHIFT, 765 (u64)lim->max_sectors << SECTOR_SHIFT, 766 (u64)lim->max_segments << PAGE_SHIFT), 767 fs_info->sectorsize); 768 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED; 769 770 fs_info->max_extent_size = min_not_zero(fs_info->max_extent_size, 771 fs_info->max_zone_append_size); 772 773 /* 774 * Check mount options here, because we might change fs_info->zoned 775 * from fs_info->zone_size. 776 */ 777 ret = btrfs_check_mountopts_zoned(fs_info, &fs_info->mount_opt); 778 if (ret) 779 return ret; 780 781 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size); 782 return 0; 783 } 784 785 int btrfs_check_mountopts_zoned(const struct btrfs_fs_info *info, 786 unsigned long long *mount_opt) 787 { 788 if (!btrfs_is_zoned(info)) 789 return 0; 790 791 /* 792 * Space cache writing is not COWed. Disable that to avoid write errors 793 * in sequential zones. 794 */ 795 if (btrfs_raw_test_opt(*mount_opt, SPACE_CACHE)) { 796 btrfs_err(info, "zoned: space cache v1 is not supported"); 797 return -EINVAL; 798 } 799 800 if (btrfs_raw_test_opt(*mount_opt, NODATACOW)) { 801 btrfs_err(info, "zoned: NODATACOW not supported"); 802 return -EINVAL; 803 } 804 805 if (btrfs_raw_test_opt(*mount_opt, DISCARD_ASYNC)) { 806 btrfs_info(info, 807 "zoned: async discard ignored and disabled for zoned mode"); 808 btrfs_clear_opt(*mount_opt, DISCARD_ASYNC); 809 } 810 811 return 0; 812 } 813 814 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones, 815 int rw, u64 *bytenr_ret) 816 { 817 u64 wp; 818 int ret; 819 820 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) { 821 *bytenr_ret = zones[0].start << SECTOR_SHIFT; 822 return 0; 823 } 824 825 ret = sb_write_pointer(bdev, zones, &wp); 826 if (ret != -ENOENT && ret < 0) 827 return ret; 828 829 if (rw == WRITE) { 830 struct blk_zone *reset = NULL; 831 832 if (wp == zones[0].start << SECTOR_SHIFT) 833 reset = &zones[0]; 834 else if (wp == zones[1].start << SECTOR_SHIFT) 835 reset = &zones[1]; 836 837 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) { 838 unsigned int nofs_flags; 839 840 ASSERT(sb_zone_is_full(reset)); 841 842 nofs_flags = memalloc_nofs_save(); 843 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET, 844 reset->start, reset->len); 845 memalloc_nofs_restore(nofs_flags); 846 if (ret) 847 return ret; 848 849 reset->cond = BLK_ZONE_COND_EMPTY; 850 reset->wp = reset->start; 851 } 852 } else if (ret != -ENOENT) { 853 /* 854 * For READ, we want the previous one. Move write pointer to 855 * the end of a zone, if it is at the head of a zone. 856 */ 857 u64 zone_end = 0; 858 859 if (wp == zones[0].start << SECTOR_SHIFT) 860 zone_end = zones[1].start + zones[1].capacity; 861 else if (wp == zones[1].start << SECTOR_SHIFT) 862 zone_end = zones[0].start + zones[0].capacity; 863 if (zone_end) 864 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT, 865 BTRFS_SUPER_INFO_SIZE); 866 867 wp -= BTRFS_SUPER_INFO_SIZE; 868 } 869 870 *bytenr_ret = wp; 871 return 0; 872 873 } 874 875 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw, 876 u64 *bytenr_ret) 877 { 878 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES]; 879 sector_t zone_sectors; 880 u32 sb_zone; 881 int ret; 882 u8 zone_sectors_shift; 883 sector_t nr_sectors; 884 u32 nr_zones; 885 886 if (!bdev_is_zoned(bdev)) { 887 *bytenr_ret = btrfs_sb_offset(mirror); 888 return 0; 889 } 890 891 ASSERT(rw == READ || rw == WRITE); 892 893 zone_sectors = bdev_zone_sectors(bdev); 894 if (!is_power_of_2(zone_sectors)) 895 return -EINVAL; 896 zone_sectors_shift = ilog2(zone_sectors); 897 nr_sectors = bdev_nr_sectors(bdev); 898 nr_zones = nr_sectors >> zone_sectors_shift; 899 900 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror); 901 if (sb_zone + 1 >= nr_zones) 902 return -ENOENT; 903 904 ret = blkdev_report_zones_cached(bdev, zone_start_sector(sb_zone, bdev), 905 BTRFS_NR_SB_LOG_ZONES, 906 copy_zone_info_cb, zones); 907 if (ret < 0) 908 return ret; 909 if (unlikely(ret != BTRFS_NR_SB_LOG_ZONES)) 910 return -EIO; 911 912 return sb_log_location(bdev, zones, rw, bytenr_ret); 913 } 914 915 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw, 916 u64 *bytenr_ret) 917 { 918 struct btrfs_zoned_device_info *zinfo = device->zone_info; 919 u32 zone_num; 920 921 /* 922 * For a zoned filesystem on a non-zoned block device, use the same 923 * super block locations as regular filesystem. Doing so, the super 924 * block can always be retrieved and the zoned flag of the volume 925 * detected from the super block information. 926 */ 927 if (!bdev_is_zoned(device->bdev)) { 928 *bytenr_ret = btrfs_sb_offset(mirror); 929 return 0; 930 } 931 932 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror); 933 if (zone_num + 1 >= zinfo->nr_zones) 934 return -ENOENT; 935 936 return sb_log_location(device->bdev, 937 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror], 938 rw, bytenr_ret); 939 } 940 941 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo, 942 int mirror) 943 { 944 u32 zone_num; 945 946 if (!zinfo) 947 return false; 948 949 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror); 950 if (zone_num + 1 >= zinfo->nr_zones) 951 return false; 952 953 if (!test_bit(zone_num, zinfo->seq_zones)) 954 return false; 955 956 return true; 957 } 958 959 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror) 960 { 961 struct btrfs_zoned_device_info *zinfo = device->zone_info; 962 struct blk_zone *zone; 963 int i; 964 965 if (!is_sb_log_zone(zinfo, mirror)) 966 return 0; 967 968 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror]; 969 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) { 970 /* Advance the next zone */ 971 if (zone->cond == BLK_ZONE_COND_FULL) { 972 zone++; 973 continue; 974 } 975 976 if (zone->cond == BLK_ZONE_COND_EMPTY) 977 zone->cond = BLK_ZONE_COND_IMP_OPEN; 978 979 zone->wp += SUPER_INFO_SECTORS; 980 981 if (sb_zone_is_full(zone)) { 982 /* 983 * No room left to write new superblock. Since 984 * superblock is written with REQ_SYNC, it is safe to 985 * finish the zone now. 986 * 987 * If the write pointer is exactly at the capacity, 988 * explicit ZONE_FINISH is not necessary. 989 */ 990 if (zone->wp != zone->start + zone->capacity) { 991 unsigned int nofs_flags; 992 int ret; 993 994 nofs_flags = memalloc_nofs_save(); 995 ret = blkdev_zone_mgmt(device->bdev, 996 REQ_OP_ZONE_FINISH, zone->start, 997 zone->len); 998 memalloc_nofs_restore(nofs_flags); 999 if (ret) 1000 return ret; 1001 } 1002 1003 zone->wp = zone->start + zone->len; 1004 zone->cond = BLK_ZONE_COND_FULL; 1005 } 1006 return 0; 1007 } 1008 1009 /* All the zones are FULL. Should not reach here. */ 1010 DEBUG_WARN("unexpected state, all zones full"); 1011 return -EIO; 1012 } 1013 1014 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror) 1015 { 1016 unsigned int nofs_flags; 1017 sector_t zone_sectors; 1018 sector_t nr_sectors; 1019 u8 zone_sectors_shift; 1020 u32 sb_zone; 1021 u32 nr_zones; 1022 int ret; 1023 1024 zone_sectors = bdev_zone_sectors(bdev); 1025 zone_sectors_shift = ilog2(zone_sectors); 1026 nr_sectors = bdev_nr_sectors(bdev); 1027 nr_zones = nr_sectors >> zone_sectors_shift; 1028 1029 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror); 1030 if (sb_zone + 1 >= nr_zones) 1031 return -ENOENT; 1032 1033 nofs_flags = memalloc_nofs_save(); 1034 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET, 1035 zone_start_sector(sb_zone, bdev), 1036 zone_sectors * BTRFS_NR_SB_LOG_ZONES); 1037 memalloc_nofs_restore(nofs_flags); 1038 return ret; 1039 } 1040 1041 /* 1042 * Find allocatable zones within a given region. 1043 * 1044 * @device: the device to allocate a region on 1045 * @hole_start: the position of the hole to allocate the region 1046 * @num_bytes: size of wanted region 1047 * @hole_end: the end of the hole 1048 * @return: position of allocatable zones 1049 * 1050 * Allocatable region should not contain any superblock locations. 1051 */ 1052 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start, 1053 u64 hole_end, u64 num_bytes) 1054 { 1055 struct btrfs_zoned_device_info *zinfo = device->zone_info; 1056 const u8 shift = zinfo->zone_size_shift; 1057 u64 nzones = num_bytes >> shift; 1058 u64 pos = hole_start; 1059 u64 begin, end; 1060 bool have_sb; 1061 int i; 1062 1063 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size), 1064 "hole_start=%llu zinfo->zone_size=%llu", hole_start, zinfo->zone_size); 1065 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size), 1066 "num_bytes=%llu zinfo->zone_size=%llu", num_bytes, zinfo->zone_size); 1067 1068 while (pos < hole_end) { 1069 begin = pos >> shift; 1070 end = begin + nzones; 1071 1072 if (end > zinfo->nr_zones) 1073 return hole_end; 1074 1075 /* Check if zones in the region are all empty */ 1076 if (btrfs_dev_is_sequential(device, pos) && 1077 !bitmap_test_range_all_set(zinfo->empty_zones, begin, nzones)) { 1078 pos += zinfo->zone_size; 1079 continue; 1080 } 1081 1082 have_sb = false; 1083 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 1084 u32 sb_zone; 1085 u64 sb_pos; 1086 1087 sb_zone = sb_zone_number(shift, i); 1088 if (!(end <= sb_zone || 1089 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) { 1090 have_sb = true; 1091 pos = zone_start_physical( 1092 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo); 1093 break; 1094 } 1095 1096 /* We also need to exclude regular superblock positions */ 1097 sb_pos = btrfs_sb_offset(i); 1098 if (!(pos + num_bytes <= sb_pos || 1099 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) { 1100 have_sb = true; 1101 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE, 1102 zinfo->zone_size); 1103 break; 1104 } 1105 } 1106 if (!have_sb) 1107 break; 1108 } 1109 1110 return pos; 1111 } 1112 1113 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos) 1114 { 1115 struct btrfs_zoned_device_info *zone_info = device->zone_info; 1116 unsigned int zno = (pos >> zone_info->zone_size_shift); 1117 1118 /* We can use any number of zones */ 1119 if (zone_info->max_active_zones == 0) 1120 return true; 1121 1122 if (!test_bit(zno, zone_info->active_zones)) { 1123 /* Active zone left? */ 1124 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0) 1125 return false; 1126 if (test_and_set_bit(zno, zone_info->active_zones)) { 1127 /* Someone already set the bit */ 1128 atomic_inc(&zone_info->active_zones_left); 1129 } 1130 } 1131 1132 return true; 1133 } 1134 1135 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos) 1136 { 1137 struct btrfs_zoned_device_info *zone_info = device->zone_info; 1138 unsigned int zno = (pos >> zone_info->zone_size_shift); 1139 1140 /* We can use any number of zones */ 1141 if (zone_info->max_active_zones == 0) 1142 return; 1143 1144 if (test_and_clear_bit(zno, zone_info->active_zones)) 1145 atomic_inc(&zone_info->active_zones_left); 1146 } 1147 1148 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical, 1149 u64 length, u64 *bytes) 1150 { 1151 unsigned int nofs_flags; 1152 int ret; 1153 1154 *bytes = 0; 1155 nofs_flags = memalloc_nofs_save(); 1156 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET, 1157 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT); 1158 memalloc_nofs_restore(nofs_flags); 1159 if (ret) 1160 return ret; 1161 1162 *bytes = length; 1163 while (length) { 1164 btrfs_dev_set_zone_empty(device, physical); 1165 btrfs_dev_clear_active_zone(device, physical); 1166 physical += device->zone_info->zone_size; 1167 length -= device->zone_info->zone_size; 1168 } 1169 1170 return 0; 1171 } 1172 1173 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size) 1174 { 1175 struct btrfs_zoned_device_info *zinfo = device->zone_info; 1176 const u8 shift = zinfo->zone_size_shift; 1177 unsigned long begin = start >> shift; 1178 unsigned long nbits = size >> shift; 1179 u64 pos; 1180 int ret; 1181 1182 ASSERT(IS_ALIGNED(start, zinfo->zone_size), 1183 "start=%llu, zinfo->zone_size=%llu", start, zinfo->zone_size); 1184 ASSERT(IS_ALIGNED(size, zinfo->zone_size), 1185 "size=%llu, zinfo->zone_size=%llu", size, zinfo->zone_size); 1186 1187 if (begin + nbits > zinfo->nr_zones) 1188 return -ERANGE; 1189 1190 /* All the zones are conventional */ 1191 if (bitmap_test_range_all_zero(zinfo->seq_zones, begin, nbits)) 1192 return 0; 1193 1194 /* All the zones are sequential and empty */ 1195 if (bitmap_test_range_all_set(zinfo->seq_zones, begin, nbits) && 1196 bitmap_test_range_all_set(zinfo->empty_zones, begin, nbits)) 1197 return 0; 1198 1199 for (pos = start; pos < start + size; pos += zinfo->zone_size) { 1200 u64 reset_bytes; 1201 1202 if (!btrfs_dev_is_sequential(device, pos) || 1203 btrfs_dev_is_empty_zone(device, pos)) 1204 continue; 1205 1206 /* Free regions should be empty */ 1207 btrfs_warn( 1208 device->fs_info, 1209 "zoned: resetting device %s (devid %llu) zone %llu for allocation", 1210 rcu_dereference(device->name), device->devid, pos >> shift); 1211 WARN_ON_ONCE(1); 1212 1213 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size, 1214 &reset_bytes); 1215 if (ret) 1216 return ret; 1217 } 1218 1219 return 0; 1220 } 1221 1222 /* 1223 * Calculate an allocation pointer from the extent allocation information 1224 * for a block group consist of conventional zones. It is pointed to the 1225 * end of the highest addressed extent in the block group as an allocation 1226 * offset. 1227 */ 1228 static int calculate_alloc_pointer(struct btrfs_block_group *cache, 1229 u64 *offset_ret, bool new) 1230 { 1231 struct btrfs_fs_info *fs_info = cache->fs_info; 1232 struct btrfs_root *root; 1233 BTRFS_PATH_AUTO_FREE(path); 1234 struct btrfs_key key; 1235 struct btrfs_key found_key; 1236 const u64 bg_end = btrfs_block_group_end(cache); 1237 int ret; 1238 u64 length; 1239 1240 /* 1241 * Avoid tree lookups for a new block group, there's no use for it. 1242 * It must always be 0. 1243 * 1244 * Also, we have a lock chain of extent buffer lock -> chunk mutex. 1245 * For new a block group, this function is called from 1246 * btrfs_make_block_group() which is already taking the chunk mutex. 1247 * Thus, we cannot call calculate_alloc_pointer() which takes extent 1248 * buffer locks to avoid deadlock. 1249 */ 1250 if (new) { 1251 *offset_ret = 0; 1252 return 0; 1253 } 1254 1255 path = btrfs_alloc_path(); 1256 if (!path) 1257 return -ENOMEM; 1258 1259 key.objectid = bg_end; 1260 key.type = 0; 1261 key.offset = 0; 1262 1263 root = btrfs_extent_root(fs_info, key.objectid); 1264 if (unlikely(!root)) { 1265 btrfs_err(fs_info, 1266 "missing extent root for extent at bytenr %llu", 1267 key.objectid); 1268 return -EUCLEAN; 1269 } 1270 1271 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1272 /* We should not find the exact match */ 1273 if (unlikely(!ret)) 1274 ret = -EUCLEAN; 1275 if (ret < 0) 1276 return ret; 1277 1278 ret = btrfs_previous_extent_item(root, path, cache->start); 1279 if (ret) { 1280 if (ret == 1) { 1281 ret = 0; 1282 *offset_ret = 0; 1283 } 1284 return ret; 1285 } 1286 1287 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); 1288 1289 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) 1290 length = found_key.offset; 1291 else 1292 length = fs_info->nodesize; 1293 1294 if (unlikely(!(found_key.objectid >= cache->start && 1295 found_key.objectid + length <= bg_end))) { 1296 return -EUCLEAN; 1297 } 1298 *offset_ret = found_key.objectid + length - cache->start; 1299 return 0; 1300 } 1301 1302 struct zone_info { 1303 u64 physical; 1304 u64 capacity; 1305 u64 alloc_offset; 1306 }; 1307 1308 static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx, 1309 struct zone_info *info, unsigned long *active, 1310 struct btrfs_chunk_map *map, bool new) 1311 { 1312 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; 1313 struct btrfs_device *device; 1314 int dev_replace_is_ongoing = 0; 1315 unsigned int nofs_flag; 1316 struct blk_zone zone; 1317 int ret; 1318 1319 info->physical = map->stripes[zone_idx].physical; 1320 1321 down_read(&dev_replace->rwsem); 1322 device = map->stripes[zone_idx].dev; 1323 1324 if (!device->bdev) { 1325 up_read(&dev_replace->rwsem); 1326 info->alloc_offset = WP_MISSING_DEV; 1327 return 0; 1328 } 1329 1330 /* Consider a zone as active if we can allow any number of active zones. */ 1331 if (!device->zone_info->max_active_zones) 1332 __set_bit(zone_idx, active); 1333 1334 if (!btrfs_dev_is_sequential(device, info->physical)) { 1335 up_read(&dev_replace->rwsem); 1336 info->alloc_offset = WP_CONVENTIONAL; 1337 info->capacity = device->zone_info->zone_size; 1338 return 0; 1339 } 1340 1341 ASSERT(!new || btrfs_dev_is_empty_zone(device, info->physical)); 1342 1343 /* This zone will be used for allocation, so mark this zone non-empty. */ 1344 btrfs_dev_clear_zone_empty(device, info->physical); 1345 1346 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); 1347 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) 1348 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, info->physical); 1349 1350 /* 1351 * The group is mapped to a sequential zone. Get the zone write pointer 1352 * to determine the allocation offset within the zone. 1353 */ 1354 WARN_ON(!IS_ALIGNED(info->physical, fs_info->zone_size)); 1355 1356 if (new) { 1357 sector_t capacity; 1358 1359 capacity = bdev_zone_capacity(device->bdev, info->physical >> SECTOR_SHIFT); 1360 up_read(&dev_replace->rwsem); 1361 info->alloc_offset = 0; 1362 info->capacity = capacity << SECTOR_SHIFT; 1363 1364 return 0; 1365 } 1366 1367 nofs_flag = memalloc_nofs_save(); 1368 ret = btrfs_get_dev_zone(device, info->physical, &zone); 1369 memalloc_nofs_restore(nofs_flag); 1370 if (ret) { 1371 up_read(&dev_replace->rwsem); 1372 if (ret != -EIO && ret != -EOPNOTSUPP) 1373 return ret; 1374 info->alloc_offset = WP_MISSING_DEV; 1375 return 0; 1376 } 1377 1378 if (unlikely(zone.type == BLK_ZONE_TYPE_CONVENTIONAL)) { 1379 btrfs_err(fs_info, 1380 "zoned: unexpected conventional zone %llu on device %s (devid %llu)", 1381 zone.start << SECTOR_SHIFT, rcu_dereference(device->name), 1382 device->devid); 1383 up_read(&dev_replace->rwsem); 1384 return -EIO; 1385 } 1386 1387 info->capacity = (zone.capacity << SECTOR_SHIFT); 1388 1389 switch (zone.cond) { 1390 case BLK_ZONE_COND_OFFLINE: 1391 case BLK_ZONE_COND_READONLY: 1392 btrfs_err(fs_info, 1393 "zoned: offline/readonly zone %llu on device %s (devid %llu)", 1394 (info->physical >> device->zone_info->zone_size_shift), 1395 rcu_dereference(device->name), device->devid); 1396 info->alloc_offset = WP_MISSING_DEV; 1397 break; 1398 case BLK_ZONE_COND_EMPTY: 1399 info->alloc_offset = 0; 1400 break; 1401 case BLK_ZONE_COND_FULL: 1402 info->alloc_offset = info->capacity; 1403 break; 1404 default: 1405 /* Partially used zone. */ 1406 info->alloc_offset = ((zone.wp - zone.start) << SECTOR_SHIFT); 1407 __set_bit(zone_idx, active); 1408 break; 1409 } 1410 1411 up_read(&dev_replace->rwsem); 1412 1413 return 0; 1414 } 1415 1416 static int btrfs_load_block_group_single(struct btrfs_block_group *bg, 1417 struct zone_info *info, 1418 unsigned long *active) 1419 { 1420 if (unlikely(info->alloc_offset == WP_MISSING_DEV)) { 1421 btrfs_err(bg->fs_info, 1422 "zoned: cannot recover write pointer for zone %llu", 1423 info->physical); 1424 return -EIO; 1425 } 1426 1427 bg->alloc_offset = info->alloc_offset; 1428 bg->zone_capacity = info->capacity; 1429 if (test_bit(0, active)) 1430 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags); 1431 return 0; 1432 } 1433 1434 static int btrfs_load_block_group_dup(struct btrfs_block_group *bg, 1435 struct btrfs_chunk_map *map, 1436 struct zone_info *zone_info, 1437 unsigned long *active, 1438 u64 last_alloc) 1439 { 1440 struct btrfs_fs_info *fs_info = bg->fs_info; 1441 1442 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) { 1443 btrfs_err(fs_info, "zoned: data DUP profile needs raid-stripe-tree"); 1444 return -EINVAL; 1445 } 1446 1447 bg->zone_capacity = min_not_zero(zone_info[0].capacity, zone_info[1].capacity); 1448 1449 if (unlikely(zone_info[0].alloc_offset == WP_MISSING_DEV)) { 1450 btrfs_err(fs_info, 1451 "zoned: cannot recover write pointer for zone %llu", 1452 zone_info[0].physical); 1453 return -EIO; 1454 } 1455 if (unlikely(zone_info[1].alloc_offset == WP_MISSING_DEV)) { 1456 btrfs_err(fs_info, 1457 "zoned: cannot recover write pointer for zone %llu", 1458 zone_info[1].physical); 1459 return -EIO; 1460 } 1461 1462 /* 1463 * When the last extent is removed, last_alloc can be smaller than the other write 1464 * pointer. In that case, last_alloc should be moved to the corresponding write 1465 * pointer position. 1466 */ 1467 for (int i = 0; i < map->num_stripes; i++) { 1468 if (zone_info[i].alloc_offset == WP_CONVENTIONAL) 1469 continue; 1470 if (last_alloc <= zone_info[i].alloc_offset) { 1471 last_alloc = zone_info[i].alloc_offset; 1472 break; 1473 } 1474 } 1475 1476 if (zone_info[0].alloc_offset == WP_CONVENTIONAL) 1477 zone_info[0].alloc_offset = last_alloc; 1478 1479 if (zone_info[1].alloc_offset == WP_CONVENTIONAL) 1480 zone_info[1].alloc_offset = last_alloc; 1481 1482 if (unlikely(zone_info[0].alloc_offset != zone_info[1].alloc_offset)) { 1483 btrfs_err(fs_info, 1484 "zoned: write pointer offset mismatch of zones in DUP profile"); 1485 return -EIO; 1486 } 1487 1488 if (test_bit(0, active) != test_bit(1, active)) { 1489 if (unlikely(!btrfs_zone_activate(bg))) 1490 return -EIO; 1491 } else if (test_bit(0, active)) { 1492 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags); 1493 } 1494 1495 bg->alloc_offset = zone_info[0].alloc_offset; 1496 return 0; 1497 } 1498 1499 static int btrfs_load_block_group_raid1(struct btrfs_block_group *bg, 1500 struct btrfs_chunk_map *map, 1501 struct zone_info *zone_info, 1502 unsigned long *active, 1503 u64 last_alloc) 1504 { 1505 struct btrfs_fs_info *fs_info = bg->fs_info; 1506 int i; 1507 1508 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) { 1509 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree", 1510 btrfs_bg_type_to_raid_name(map->type)); 1511 return -EINVAL; 1512 } 1513 1514 /* In case a device is missing we have a cap of 0, so don't use it. */ 1515 bg->zone_capacity = min_not_zero(zone_info[0].capacity, zone_info[1].capacity); 1516 1517 /* 1518 * When the last extent is removed, last_alloc can be smaller than the other write 1519 * pointer. In that case, last_alloc should be moved to the corresponding write 1520 * pointer position. 1521 */ 1522 for (i = 0; i < map->num_stripes; i++) { 1523 if (zone_info[i].alloc_offset == WP_MISSING_DEV || 1524 zone_info[i].alloc_offset == WP_CONVENTIONAL) 1525 continue; 1526 if (last_alloc <= zone_info[i].alloc_offset) { 1527 last_alloc = zone_info[i].alloc_offset; 1528 break; 1529 } 1530 } 1531 1532 for (i = 0; i < map->num_stripes; i++) { 1533 if (zone_info[i].alloc_offset == WP_MISSING_DEV) 1534 continue; 1535 1536 if (zone_info[i].alloc_offset == WP_CONVENTIONAL) 1537 zone_info[i].alloc_offset = last_alloc; 1538 1539 if (unlikely((zone_info[0].alloc_offset != zone_info[i].alloc_offset) && 1540 !btrfs_test_opt(fs_info, DEGRADED))) { 1541 btrfs_err(fs_info, 1542 "zoned: write pointer offset mismatch of zones in %s profile", 1543 btrfs_bg_type_to_raid_name(map->type)); 1544 return -EIO; 1545 } 1546 if (test_bit(0, active) != test_bit(i, active)) { 1547 if (unlikely(!btrfs_test_opt(fs_info, DEGRADED) && 1548 !btrfs_zone_activate(bg))) { 1549 return -EIO; 1550 } 1551 } else { 1552 if (test_bit(0, active)) 1553 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags); 1554 } 1555 } 1556 1557 if (zone_info[0].alloc_offset != WP_MISSING_DEV) 1558 bg->alloc_offset = zone_info[0].alloc_offset; 1559 else 1560 bg->alloc_offset = zone_info[i - 1].alloc_offset; 1561 1562 return 0; 1563 } 1564 1565 static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg, 1566 struct btrfs_chunk_map *map, 1567 struct zone_info *zone_info, 1568 unsigned long *active, 1569 u64 last_alloc) 1570 { 1571 struct btrfs_fs_info *fs_info = bg->fs_info; 1572 u64 stripe_nr = 0, stripe_offset = 0; 1573 u64 prev_offset = 0; 1574 u32 stripe_index = 0; 1575 bool has_partial = false, has_conventional = false; 1576 1577 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) { 1578 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree", 1579 btrfs_bg_type_to_raid_name(map->type)); 1580 return -EINVAL; 1581 } 1582 1583 /* 1584 * When the last extent is removed, last_alloc can be smaller than the other write 1585 * pointer. In that case, last_alloc should be moved to the corresponding write 1586 * pointer position. 1587 */ 1588 for (int i = 0; i < map->num_stripes; i++) { 1589 u64 alloc; 1590 1591 if (zone_info[i].alloc_offset == WP_MISSING_DEV || 1592 zone_info[i].alloc_offset == WP_CONVENTIONAL) 1593 continue; 1594 1595 stripe_nr = zone_info[i].alloc_offset >> BTRFS_STRIPE_LEN_SHIFT; 1596 stripe_offset = zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK; 1597 if (stripe_offset == 0 && stripe_nr > 0) { 1598 stripe_nr--; 1599 stripe_offset = BTRFS_STRIPE_LEN; 1600 } 1601 alloc = ((stripe_nr * map->num_stripes + i) << BTRFS_STRIPE_LEN_SHIFT) + 1602 stripe_offset; 1603 last_alloc = max(last_alloc, alloc); 1604 1605 /* Partially written stripe found. It should be last. */ 1606 if (zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK) 1607 break; 1608 } 1609 stripe_nr = 0; 1610 stripe_offset = 0; 1611 1612 if (last_alloc) { 1613 u32 factor = map->num_stripes; 1614 1615 stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT; 1616 stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK; 1617 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); 1618 } 1619 1620 for (int i = 0; i < map->num_stripes; i++) { 1621 if (zone_info[i].alloc_offset == WP_MISSING_DEV) 1622 continue; 1623 1624 if (zone_info[i].alloc_offset == WP_CONVENTIONAL) { 1625 has_conventional = true; 1626 zone_info[i].alloc_offset = btrfs_stripe_nr_to_offset(stripe_nr); 1627 1628 if (stripe_index > i) 1629 zone_info[i].alloc_offset += BTRFS_STRIPE_LEN; 1630 else if (stripe_index == i) 1631 zone_info[i].alloc_offset += stripe_offset; 1632 } 1633 1634 /* Verification */ 1635 if (i != 0) { 1636 if (unlikely(prev_offset < zone_info[i].alloc_offset)) { 1637 btrfs_err(fs_info, 1638 "zoned: stripe position disorder found in block group %llu", 1639 bg->start); 1640 return -EIO; 1641 } 1642 1643 if (unlikely(has_partial && 1644 (zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK))) { 1645 btrfs_err(fs_info, 1646 "zoned: multiple partial written stripe found in block group %llu", 1647 bg->start); 1648 return -EIO; 1649 } 1650 } 1651 prev_offset = zone_info[i].alloc_offset; 1652 1653 if ((zone_info[i].alloc_offset & BTRFS_STRIPE_LEN_MASK) != 0) 1654 has_partial = true; 1655 1656 if (test_bit(0, active) != test_bit(i, active)) { 1657 if (unlikely(!btrfs_zone_activate(bg))) 1658 return -EIO; 1659 } else { 1660 if (test_bit(0, active)) 1661 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags); 1662 } 1663 bg->zone_capacity += zone_info[i].capacity; 1664 bg->alloc_offset += zone_info[i].alloc_offset; 1665 } 1666 1667 /* Check if all devices stay in the same stripe row. */ 1668 if (unlikely(zone_info[0].alloc_offset - 1669 zone_info[map->num_stripes - 1].alloc_offset > BTRFS_STRIPE_LEN)) { 1670 btrfs_err(fs_info, "zoned: stripe gap too large in block group %llu", bg->start); 1671 return -EIO; 1672 } 1673 1674 if (unlikely(has_conventional && bg->alloc_offset < last_alloc)) { 1675 btrfs_err(fs_info, "zoned: allocated extent stays beyond write pointers %llu %llu", 1676 bg->alloc_offset, last_alloc); 1677 return -EIO; 1678 } 1679 1680 return 0; 1681 } 1682 1683 static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg, 1684 struct btrfs_chunk_map *map, 1685 struct zone_info *zone_info, 1686 unsigned long *active, 1687 u64 last_alloc) 1688 { 1689 struct btrfs_fs_info *fs_info = bg->fs_info; 1690 u64 AUTO_KFREE(raid0_allocs); 1691 u64 stripe_nr = 0, stripe_offset = 0; 1692 u32 stripe_index = 0; 1693 bool has_partial = false, has_conventional = false; 1694 u64 prev_offset = 0; 1695 1696 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && !fs_info->stripe_root) { 1697 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree", 1698 btrfs_bg_type_to_raid_name(map->type)); 1699 return -EINVAL; 1700 } 1701 1702 raid0_allocs = kcalloc(map->num_stripes / map->sub_stripes, sizeof(*raid0_allocs), 1703 GFP_NOFS); 1704 if (!raid0_allocs) 1705 return -ENOMEM; 1706 1707 /* 1708 * When the last extent is removed, last_alloc can be smaller than the other write 1709 * pointer. In that case, last_alloc should be moved to the corresponding write 1710 * pointer position. 1711 */ 1712 for (int i = 0; i < map->num_stripes; i += map->sub_stripes) { 1713 u64 alloc = zone_info[i].alloc_offset; 1714 1715 for (int j = 1; j < map->sub_stripes; j++) { 1716 int idx = i + j; 1717 1718 if (zone_info[idx].alloc_offset == WP_MISSING_DEV || 1719 zone_info[idx].alloc_offset == WP_CONVENTIONAL) 1720 continue; 1721 if (alloc == WP_MISSING_DEV || alloc == WP_CONVENTIONAL) { 1722 alloc = zone_info[idx].alloc_offset; 1723 } else if (unlikely(zone_info[idx].alloc_offset != alloc)) { 1724 btrfs_err(fs_info, 1725 "zoned: write pointer mismatch found in block group %llu", 1726 bg->start); 1727 return -EIO; 1728 } 1729 } 1730 1731 raid0_allocs[i / map->sub_stripes] = alloc; 1732 if (alloc == WP_CONVENTIONAL) 1733 continue; 1734 if (unlikely(alloc == WP_MISSING_DEV)) { 1735 btrfs_err(fs_info, 1736 "zoned: cannot recover write pointer of block group %llu due to missing device", 1737 bg->start); 1738 return -EIO; 1739 } 1740 1741 stripe_nr = alloc >> BTRFS_STRIPE_LEN_SHIFT; 1742 stripe_offset = alloc & BTRFS_STRIPE_LEN_MASK; 1743 if (stripe_offset == 0 && stripe_nr > 0) { 1744 stripe_nr--; 1745 stripe_offset = BTRFS_STRIPE_LEN; 1746 } 1747 1748 alloc = ((stripe_nr * (map->num_stripes / map->sub_stripes) + 1749 (i / map->sub_stripes)) << 1750 BTRFS_STRIPE_LEN_SHIFT) + stripe_offset; 1751 last_alloc = max(last_alloc, alloc); 1752 } 1753 stripe_nr = 0; 1754 stripe_offset = 0; 1755 1756 if (last_alloc) { 1757 u32 factor = map->num_stripes / map->sub_stripes; 1758 1759 stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT; 1760 stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK; 1761 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); 1762 } 1763 1764 for (int i = 0; i < map->num_stripes; i++) { 1765 int idx = i / map->sub_stripes; 1766 1767 if (raid0_allocs[idx] == WP_CONVENTIONAL) { 1768 has_conventional = true; 1769 raid0_allocs[idx] = btrfs_stripe_nr_to_offset(stripe_nr); 1770 1771 if (stripe_index > idx) 1772 raid0_allocs[idx] += BTRFS_STRIPE_LEN; 1773 else if (stripe_index == idx) 1774 raid0_allocs[idx] += stripe_offset; 1775 } 1776 1777 if ((i % map->sub_stripes) == 0) { 1778 /* Verification */ 1779 if (i != 0) { 1780 if (unlikely(prev_offset < raid0_allocs[idx])) { 1781 btrfs_err(fs_info, 1782 "zoned: stripe position disorder found in block group %llu", 1783 bg->start); 1784 return -EIO; 1785 } 1786 1787 if (unlikely(has_partial && 1788 (raid0_allocs[idx] & BTRFS_STRIPE_LEN_MASK))) { 1789 btrfs_err(fs_info, 1790 "zoned: multiple partial written stripe found in block group %llu", 1791 bg->start); 1792 return -EIO; 1793 } 1794 } 1795 prev_offset = raid0_allocs[idx]; 1796 1797 if ((raid0_allocs[idx] & BTRFS_STRIPE_LEN_MASK) != 0) 1798 has_partial = true; 1799 } 1800 1801 if (zone_info[i].alloc_offset == WP_MISSING_DEV || 1802 zone_info[i].alloc_offset == WP_CONVENTIONAL) 1803 zone_info[i].alloc_offset = raid0_allocs[idx]; 1804 1805 if (test_bit(0, active) != test_bit(i, active)) { 1806 if (unlikely(!btrfs_zone_activate(bg))) 1807 return -EIO; 1808 } else if (test_bit(0, active)) { 1809 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &bg->runtime_flags); 1810 } 1811 1812 if ((i % map->sub_stripes) == 0) { 1813 bg->zone_capacity += zone_info[i].capacity; 1814 bg->alloc_offset += zone_info[i].alloc_offset; 1815 } 1816 } 1817 1818 /* Check if all devices stay in the same stripe row. */ 1819 if (unlikely(zone_info[0].alloc_offset - 1820 zone_info[map->num_stripes - 1].alloc_offset > BTRFS_STRIPE_LEN)) { 1821 btrfs_err(fs_info, "zoned: stripe gap too large in block group %llu", 1822 bg->start); 1823 return -EIO; 1824 } 1825 1826 if (unlikely(has_conventional && bg->alloc_offset < last_alloc)) { 1827 btrfs_err(fs_info, "zoned: allocated extent stays beyond write pointers %llu %llu", 1828 bg->alloc_offset, last_alloc); 1829 return -EIO; 1830 } 1831 1832 return 0; 1833 } 1834 1835 EXPORT_FOR_TESTS 1836 int btrfs_load_block_group_by_raid_type(struct btrfs_block_group *bg, 1837 struct btrfs_chunk_map *map, 1838 struct zone_info *zone_info, 1839 unsigned long *active, u64 last_alloc) 1840 { 1841 struct btrfs_fs_info *fs_info = bg->fs_info; 1842 u64 profile; 1843 int ret; 1844 1845 profile = map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK; 1846 switch (profile) { 1847 case 0: /* single */ 1848 ret = btrfs_load_block_group_single(bg, &zone_info[0], active); 1849 break; 1850 case BTRFS_BLOCK_GROUP_DUP: 1851 ret = btrfs_load_block_group_dup(bg, map, zone_info, active, last_alloc); 1852 break; 1853 case BTRFS_BLOCK_GROUP_RAID1: 1854 case BTRFS_BLOCK_GROUP_RAID1C3: 1855 case BTRFS_BLOCK_GROUP_RAID1C4: 1856 ret = btrfs_load_block_group_raid1(bg, map, zone_info, active, last_alloc); 1857 break; 1858 case BTRFS_BLOCK_GROUP_RAID0: 1859 ret = btrfs_load_block_group_raid0(bg, map, zone_info, active, last_alloc); 1860 break; 1861 case BTRFS_BLOCK_GROUP_RAID10: 1862 ret = btrfs_load_block_group_raid10(bg, map, zone_info, active, last_alloc); 1863 break; 1864 case BTRFS_BLOCK_GROUP_RAID5: 1865 case BTRFS_BLOCK_GROUP_RAID6: 1866 default: 1867 btrfs_err(fs_info, "zoned: profile %s not yet supported", 1868 btrfs_bg_type_to_raid_name(map->type)); 1869 return -EINVAL; 1870 } 1871 1872 if (ret == -EIO && profile != 0 && profile != BTRFS_BLOCK_GROUP_RAID0 && 1873 profile != BTRFS_BLOCK_GROUP_RAID10) { 1874 /* 1875 * Detected broken write pointer. Make this block group 1876 * unallocatable by setting the allocation pointer at the end of 1877 * allocatable region. Relocating this block group will fix the 1878 * mismatch. 1879 * 1880 * Currently, we cannot handle RAID0 or RAID10 case like this 1881 * because we don't have a proper zone_capacity value. But, 1882 * reading from this block group won't work anyway by a missing 1883 * stripe. 1884 */ 1885 bg->alloc_offset = bg->zone_capacity; 1886 } 1887 1888 return ret; 1889 } 1890 1891 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new) 1892 { 1893 struct btrfs_fs_info *fs_info = cache->fs_info; 1894 struct btrfs_chunk_map *map; 1895 u64 logical = cache->start; 1896 u64 length = cache->length; 1897 struct zone_info AUTO_KFREE(zone_info); 1898 int ret; 1899 int i; 1900 unsigned long *active = NULL; 1901 u64 last_alloc = 0; 1902 u32 num_sequential = 0, num_conventional = 0; 1903 1904 if (!btrfs_is_zoned(fs_info)) 1905 return 0; 1906 1907 /* Sanity check */ 1908 if (unlikely(!IS_ALIGNED(length, fs_info->zone_size))) { 1909 btrfs_err(fs_info, 1910 "zoned: block group %llu len %llu unaligned to zone size %llu", 1911 logical, length, fs_info->zone_size); 1912 return -EIO; 1913 } 1914 1915 map = btrfs_find_chunk_map(fs_info, logical, length); 1916 if (!map) 1917 return -EINVAL; 1918 1919 cache->physical_map = map; 1920 1921 zone_info = kcalloc(map->num_stripes, sizeof(*zone_info), GFP_NOFS); 1922 if (!zone_info) { 1923 ret = -ENOMEM; 1924 goto out; 1925 } 1926 1927 active = bitmap_zalloc(map->num_stripes, GFP_NOFS); 1928 if (!active) { 1929 ret = -ENOMEM; 1930 goto out; 1931 } 1932 1933 for (i = 0; i < map->num_stripes; i++) { 1934 ret = btrfs_load_zone_info(fs_info, i, &zone_info[i], active, map, new); 1935 if (ret) 1936 goto out; 1937 1938 if (zone_info[i].alloc_offset == WP_CONVENTIONAL) 1939 num_conventional++; 1940 else 1941 num_sequential++; 1942 } 1943 1944 if (num_sequential > 0) 1945 set_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags); 1946 1947 if (num_conventional > 0) { 1948 ret = calculate_alloc_pointer(cache, &last_alloc, new); 1949 if (ret) { 1950 btrfs_err(fs_info, 1951 "zoned: failed to determine allocation offset of bg %llu", 1952 cache->start); 1953 goto out; 1954 } else if (map->num_stripes == num_conventional) { 1955 cache->alloc_offset = last_alloc; 1956 cache->zone_capacity = cache->length; 1957 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags); 1958 goto out; 1959 } 1960 } 1961 1962 ret = btrfs_load_block_group_by_raid_type(cache, map, zone_info, active, last_alloc); 1963 1964 out: 1965 /* Reject non SINGLE data profiles without RST */ 1966 if ((map->type & BTRFS_BLOCK_GROUP_DATA) && 1967 (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) && 1968 !fs_info->stripe_root) { 1969 btrfs_err(fs_info, "zoned: data %s needs raid-stripe-tree", 1970 btrfs_bg_type_to_raid_name(map->type)); 1971 ret = -EINVAL; 1972 } 1973 1974 if (unlikely(cache->alloc_offset > cache->zone_capacity)) { 1975 btrfs_err(fs_info, 1976 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu", 1977 cache->alloc_offset, cache->zone_capacity, 1978 cache->start); 1979 ret = -EIO; 1980 } 1981 1982 /* An extent is allocated after the write pointer */ 1983 if (!ret && num_conventional && last_alloc > cache->alloc_offset) { 1984 btrfs_err(fs_info, 1985 "zoned: got wrong write pointer in BG %llu: %llu > %llu", 1986 logical, last_alloc, cache->alloc_offset); 1987 ret = -EIO; 1988 } 1989 1990 if (!ret) { 1991 cache->meta_write_pointer = cache->alloc_offset + cache->start; 1992 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &cache->runtime_flags)) { 1993 btrfs_get_block_group(cache); 1994 spin_lock(&fs_info->zone_active_bgs_lock); 1995 list_add_tail(&cache->active_bg_list, 1996 &fs_info->zone_active_bgs); 1997 spin_unlock(&fs_info->zone_active_bgs_lock); 1998 } 1999 } else { 2000 btrfs_free_chunk_map(cache->physical_map); 2001 cache->physical_map = NULL; 2002 } 2003 bitmap_free(active); 2004 2005 return ret; 2006 } 2007 2008 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache) 2009 { 2010 u64 unusable, free; 2011 2012 if (!btrfs_is_zoned(cache->fs_info)) 2013 return; 2014 2015 WARN_ON(cache->bytes_super != 0); 2016 unusable = (cache->alloc_offset - cache->used) + 2017 (cache->length - cache->zone_capacity); 2018 free = cache->zone_capacity - cache->alloc_offset; 2019 2020 /* We only need ->free_space in ALLOC_SEQ block groups */ 2021 cache->cached = BTRFS_CACHE_FINISHED; 2022 cache->free_space_ctl->free_space = free; 2023 cache->zone_unusable = unusable; 2024 } 2025 2026 bool btrfs_use_zone_append(struct btrfs_bio *bbio) 2027 { 2028 u64 start = (bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT); 2029 struct btrfs_inode *inode = bbio->inode; 2030 struct btrfs_fs_info *fs_info = inode->root->fs_info; 2031 struct btrfs_block_group *cache; 2032 bool ret = false; 2033 2034 if (!btrfs_is_zoned(fs_info)) 2035 return false; 2036 2037 if (!is_data_inode(inode)) 2038 return false; 2039 2040 if (btrfs_op(&bbio->bio) != BTRFS_MAP_WRITE) 2041 return false; 2042 2043 /* 2044 * Using REQ_OP_ZONE_APPEND for relocation can break assumptions on the 2045 * extent layout the relocation code has. 2046 * Furthermore we have set aside own block-group from which only the 2047 * relocation "process" can allocate and make sure only one process at a 2048 * time can add pages to an extent that gets relocated, so it's safe to 2049 * use regular REQ_OP_WRITE for this special case. 2050 */ 2051 if (btrfs_is_data_reloc_root(inode->root)) 2052 return false; 2053 2054 cache = btrfs_lookup_block_group(fs_info, start); 2055 ASSERT(cache); 2056 if (!cache) 2057 return false; 2058 2059 ret = !!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &cache->runtime_flags); 2060 btrfs_put_block_group(cache); 2061 2062 return ret; 2063 } 2064 2065 void btrfs_record_physical_zoned(struct btrfs_bio *bbio) 2066 { 2067 const u64 physical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT; 2068 struct btrfs_ordered_sum *sum = bbio->sums; 2069 2070 if (physical < bbio->orig_physical) 2071 sum->logical -= bbio->orig_physical - physical; 2072 else 2073 sum->logical += physical - bbio->orig_physical; 2074 } 2075 2076 static void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered, 2077 u64 logical) 2078 { 2079 struct extent_map_tree *em_tree = &ordered->inode->extent_tree; 2080 struct extent_map *em; 2081 2082 ordered->disk_bytenr = logical; 2083 2084 write_lock(&em_tree->lock); 2085 em = btrfs_search_extent_mapping(em_tree, ordered->file_offset, 2086 ordered->num_bytes); 2087 /* The em should be a new COW extent, thus it should not have an offset. */ 2088 ASSERT(em->offset == 0, "em->offset=%llu", em->offset); 2089 em->disk_bytenr = logical; 2090 btrfs_free_extent_map(em); 2091 write_unlock(&em_tree->lock); 2092 } 2093 2094 static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered, 2095 u64 logical, u64 len) 2096 { 2097 struct btrfs_ordered_extent *new; 2098 2099 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags) && 2100 btrfs_split_extent_map(ordered->inode, ordered->file_offset, 2101 ordered->num_bytes, len, logical)) 2102 return false; 2103 2104 new = btrfs_split_ordered_extent(ordered, len); 2105 if (IS_ERR(new)) 2106 return false; 2107 new->disk_bytenr = logical; 2108 btrfs_finish_one_ordered(new); 2109 return true; 2110 } 2111 2112 void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered) 2113 { 2114 struct btrfs_inode *inode = ordered->inode; 2115 struct btrfs_fs_info *fs_info = inode->root->fs_info; 2116 struct btrfs_ordered_sum *sum; 2117 u64 logical, len; 2118 2119 /* 2120 * Write to pre-allocated region is for the data relocation, and so 2121 * it should use WRITE operation. No split/rewrite are necessary. 2122 */ 2123 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) 2124 return; 2125 2126 ASSERT(!list_empty(&ordered->list)); 2127 /* The ordered->list can be empty in the above pre-alloc case. */ 2128 sum = list_first_entry(&ordered->list, struct btrfs_ordered_sum, list); 2129 logical = sum->logical; 2130 len = sum->len; 2131 2132 while (len < ordered->disk_num_bytes) { 2133 sum = list_next_entry(sum, list); 2134 if (sum->logical == logical + len) { 2135 len += sum->len; 2136 continue; 2137 } 2138 if (!btrfs_zoned_split_ordered(ordered, logical, len)) { 2139 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); 2140 btrfs_err(fs_info, "failed to split ordered extent"); 2141 goto out; 2142 } 2143 logical = sum->logical; 2144 len = sum->len; 2145 } 2146 2147 if (ordered->disk_bytenr != logical) 2148 btrfs_rewrite_logical_zoned(ordered, logical); 2149 2150 out: 2151 /* 2152 * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures 2153 * were allocated by btrfs_alloc_dummy_sum only to record the logical 2154 * addresses and don't contain actual checksums. We thus must free them 2155 * here so that we don't attempt to log the csums later. 2156 */ 2157 if ((inode->flags & BTRFS_INODE_NODATASUM) || 2158 test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state)) { 2159 while ((sum = list_first_entry_or_null(&ordered->list, 2160 typeof(*sum), list))) { 2161 list_del(&sum->list); 2162 kfree(sum); 2163 } 2164 } 2165 } 2166 2167 static bool check_bg_is_active(struct btrfs_eb_write_context *ctx, 2168 struct btrfs_block_group **active_bg) 2169 { 2170 const struct writeback_control *wbc = ctx->wbc; 2171 struct btrfs_block_group *block_group = ctx->zoned_bg; 2172 struct btrfs_fs_info *fs_info = block_group->fs_info; 2173 2174 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) 2175 return true; 2176 2177 if (fs_info->treelog_bg == block_group->start) { 2178 if (!btrfs_zone_activate(block_group)) { 2179 int ret_fin = btrfs_zone_finish_one_bg(fs_info); 2180 2181 if (ret_fin != 1 || !btrfs_zone_activate(block_group)) 2182 return false; 2183 } 2184 } else if (*active_bg != block_group) { 2185 struct btrfs_block_group *tgt = *active_bg; 2186 2187 /* zoned_meta_io_lock protects fs_info->active_{meta,system}_bg. */ 2188 lockdep_assert_held(&fs_info->zoned_meta_io_lock); 2189 2190 if (tgt) { 2191 /* 2192 * If there is an unsent IO left in the allocated area, 2193 * we cannot wait for them as it may cause a deadlock. 2194 */ 2195 if (tgt->meta_write_pointer < tgt->start + tgt->alloc_offset) { 2196 if (wbc->sync_mode == WB_SYNC_NONE || 2197 (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)) 2198 return false; 2199 } 2200 2201 /* Pivot active metadata/system block group. */ 2202 btrfs_zoned_meta_io_unlock(fs_info); 2203 wait_eb_writebacks(tgt); 2204 do_zone_finish(tgt, true); 2205 btrfs_zoned_meta_io_lock(fs_info); 2206 if (*active_bg == tgt) { 2207 btrfs_put_block_group(tgt); 2208 *active_bg = NULL; 2209 } 2210 } 2211 if (!btrfs_zone_activate(block_group)) 2212 return false; 2213 if (*active_bg != block_group) { 2214 ASSERT(*active_bg == NULL); 2215 *active_bg = block_group; 2216 btrfs_get_block_group(block_group); 2217 } 2218 } 2219 2220 return true; 2221 } 2222 2223 /* 2224 * Check if @ctx->eb is aligned to the write pointer. 2225 * 2226 * Return: 2227 * 0: @ctx->eb is at the write pointer. You can write it. 2228 * -EAGAIN: There is a hole. The caller should handle the case. 2229 * -EBUSY: There is a hole, but the caller can just bail out. 2230 */ 2231 int btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info, 2232 struct btrfs_eb_write_context *ctx) 2233 { 2234 const struct writeback_control *wbc = ctx->wbc; 2235 const struct extent_buffer *eb = ctx->eb; 2236 struct btrfs_block_group *block_group = ctx->zoned_bg; 2237 2238 if (!btrfs_is_zoned(fs_info)) 2239 return 0; 2240 2241 if (block_group) { 2242 if (block_group->start > eb->start || 2243 btrfs_block_group_end(block_group) <= eb->start) { 2244 btrfs_put_block_group(block_group); 2245 block_group = NULL; 2246 ctx->zoned_bg = NULL; 2247 } 2248 } 2249 2250 if (!block_group) { 2251 block_group = btrfs_lookup_block_group(fs_info, eb->start); 2252 if (!block_group) 2253 return 0; 2254 ctx->zoned_bg = block_group; 2255 } 2256 2257 if (block_group->meta_write_pointer == eb->start) { 2258 struct btrfs_block_group **tgt; 2259 2260 if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags)) 2261 return 0; 2262 2263 if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM) 2264 tgt = &fs_info->active_system_bg; 2265 else 2266 tgt = &fs_info->active_meta_bg; 2267 if (check_bg_is_active(ctx, tgt)) 2268 return 0; 2269 } 2270 2271 /* 2272 * Since we may release fs_info->zoned_meta_io_lock, someone can already 2273 * start writing this eb. In that case, we can just bail out. 2274 */ 2275 if (block_group->meta_write_pointer > eb->start) 2276 return -EBUSY; 2277 2278 /* If for_sync, this hole will be filled with transaction commit. */ 2279 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) 2280 return -EAGAIN; 2281 return -EBUSY; 2282 } 2283 2284 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length) 2285 { 2286 if (!btrfs_dev_is_sequential(device, physical)) 2287 return -EOPNOTSUPP; 2288 2289 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT, 2290 length >> SECTOR_SHIFT, GFP_NOFS, 0); 2291 } 2292 2293 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical, 2294 struct blk_zone *zone) 2295 { 2296 struct btrfs_io_context *bioc = NULL; 2297 u64 mapped_length = PAGE_SIZE; 2298 unsigned int nofs_flag; 2299 int nmirrors; 2300 int i, ret; 2301 2302 ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical, 2303 &mapped_length, &bioc, NULL, NULL); 2304 if (unlikely(ret || !bioc || mapped_length < PAGE_SIZE)) { 2305 ret = -EIO; 2306 goto out_put_bioc; 2307 } 2308 2309 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { 2310 ret = -EINVAL; 2311 goto out_put_bioc; 2312 } 2313 2314 nofs_flag = memalloc_nofs_save(); 2315 nmirrors = (int)bioc->num_stripes; 2316 for (i = 0; i < nmirrors; i++) { 2317 u64 physical = bioc->stripes[i].physical; 2318 struct btrfs_device *dev = bioc->stripes[i].dev; 2319 2320 /* Missing device */ 2321 if (!dev->bdev) 2322 continue; 2323 2324 ret = btrfs_get_dev_zone(dev, physical, zone); 2325 /* Failing device */ 2326 if (ret == -EIO || ret == -EOPNOTSUPP) 2327 continue; 2328 break; 2329 } 2330 memalloc_nofs_restore(nofs_flag); 2331 out_put_bioc: 2332 btrfs_put_bioc(bioc); 2333 return ret; 2334 } 2335 2336 /* 2337 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by 2338 * filling zeros between @physical_pos to a write pointer of dev-replace 2339 * source device. 2340 */ 2341 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical, 2342 u64 physical_start, u64 physical_pos) 2343 { 2344 struct btrfs_fs_info *fs_info = tgt_dev->fs_info; 2345 struct blk_zone zone; 2346 u64 length; 2347 u64 wp; 2348 int ret; 2349 2350 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos)) 2351 return 0; 2352 2353 ret = read_zone_info(fs_info, logical, &zone); 2354 if (ret) 2355 return ret; 2356 2357 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT); 2358 2359 if (physical_pos == wp) 2360 return 0; 2361 2362 if (unlikely(physical_pos > wp)) 2363 return -EUCLEAN; 2364 2365 length = wp - physical_pos; 2366 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length); 2367 } 2368 2369 /* 2370 * Activate block group and underlying device zones 2371 * 2372 * @block_group: the block group to activate 2373 * 2374 * Return: true on success, false otherwise 2375 */ 2376 bool btrfs_zone_activate(struct btrfs_block_group *block_group) 2377 { 2378 struct btrfs_fs_info *fs_info = block_group->fs_info; 2379 struct btrfs_chunk_map *map; 2380 struct btrfs_device *device; 2381 u64 physical; 2382 const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA); 2383 bool ret; 2384 int i; 2385 2386 if (!btrfs_is_zoned(block_group->fs_info)) 2387 return true; 2388 2389 map = block_group->physical_map; 2390 2391 spin_lock(&fs_info->zone_active_bgs_lock); 2392 spin_lock(&block_group->lock); 2393 if (test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) { 2394 ret = true; 2395 goto out_unlock; 2396 } 2397 2398 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA) { 2399 /* The caller should check if the block group is full. */ 2400 if (WARN_ON_ONCE(btrfs_zoned_bg_is_full(block_group))) { 2401 ret = false; 2402 goto out_unlock; 2403 } 2404 } else { 2405 /* Since it is already written, it should have been active. */ 2406 WARN_ON_ONCE(block_group->meta_write_pointer != block_group->start); 2407 } 2408 2409 for (i = 0; i < map->num_stripes; i++) { 2410 struct btrfs_zoned_device_info *zinfo; 2411 int reserved = 0; 2412 2413 device = map->stripes[i].dev; 2414 physical = map->stripes[i].physical; 2415 zinfo = device->zone_info; 2416 2417 if (!device->bdev) 2418 continue; 2419 2420 if (zinfo->max_active_zones == 0) 2421 continue; 2422 2423 if (is_data) 2424 reserved = zinfo->reserved_active_zones; 2425 /* 2426 * For the data block group, leave active zones for one 2427 * metadata block group and one system block group. 2428 */ 2429 if (atomic_read(&zinfo->active_zones_left) <= reserved) { 2430 ret = false; 2431 goto out_unlock; 2432 } 2433 2434 if (!btrfs_dev_set_active_zone(device, physical)) { 2435 /* Cannot activate the zone */ 2436 ret = false; 2437 goto out_unlock; 2438 } 2439 if (!is_data) 2440 zinfo->reserved_active_zones--; 2441 } 2442 2443 /* Successfully activated all the zones */ 2444 set_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags); 2445 spin_unlock(&block_group->lock); 2446 2447 /* For the active block group list */ 2448 btrfs_get_block_group(block_group); 2449 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs); 2450 spin_unlock(&fs_info->zone_active_bgs_lock); 2451 2452 return true; 2453 2454 out_unlock: 2455 spin_unlock(&block_group->lock); 2456 spin_unlock(&fs_info->zone_active_bgs_lock); 2457 return ret; 2458 } 2459 2460 static void wait_eb_writebacks(struct btrfs_block_group *block_group) 2461 { 2462 struct btrfs_fs_info *fs_info = block_group->fs_info; 2463 const u64 end = btrfs_block_group_end(block_group); 2464 struct extent_buffer *eb; 2465 unsigned long index, start = (block_group->start >> fs_info->nodesize_bits); 2466 2467 rcu_read_lock(); 2468 xa_for_each_start(&fs_info->buffer_tree, index, eb, start) { 2469 if (eb->start < block_group->start) 2470 continue; 2471 if (eb->start >= end) 2472 break; 2473 rcu_read_unlock(); 2474 wait_on_extent_buffer_writeback(eb); 2475 rcu_read_lock(); 2476 } 2477 rcu_read_unlock(); 2478 } 2479 2480 static int call_zone_finish(struct btrfs_block_group *block_group, 2481 struct btrfs_io_stripe *stripe) 2482 { 2483 struct btrfs_device *device = stripe->dev; 2484 const u64 physical = stripe->physical; 2485 struct btrfs_zoned_device_info *zinfo = device->zone_info; 2486 int ret; 2487 2488 if (!device->bdev) 2489 return 0; 2490 2491 if (zinfo->max_active_zones == 0) 2492 return 0; 2493 2494 if (btrfs_dev_is_sequential(device, physical)) { 2495 unsigned int nofs_flags; 2496 2497 nofs_flags = memalloc_nofs_save(); 2498 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH, 2499 physical >> SECTOR_SHIFT, 2500 zinfo->zone_size >> SECTOR_SHIFT); 2501 memalloc_nofs_restore(nofs_flags); 2502 2503 if (ret) 2504 return ret; 2505 } 2506 2507 if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA)) 2508 zinfo->reserved_active_zones++; 2509 btrfs_dev_clear_active_zone(device, physical); 2510 2511 return 0; 2512 } 2513 2514 static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_written) 2515 { 2516 struct btrfs_fs_info *fs_info = block_group->fs_info; 2517 struct btrfs_chunk_map *map; 2518 const bool is_metadata = (block_group->flags & 2519 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)); 2520 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; 2521 int ret = 0; 2522 int i; 2523 2524 spin_lock(&block_group->lock); 2525 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags)) { 2526 spin_unlock(&block_group->lock); 2527 return 0; 2528 } 2529 2530 /* Check if we have unwritten allocated space */ 2531 if (is_metadata && 2532 block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) { 2533 spin_unlock(&block_group->lock); 2534 return -EAGAIN; 2535 } 2536 2537 /* 2538 * If we are sure that the block group is full (= no more room left for 2539 * new allocation) and the IO for the last usable block is completed, we 2540 * don't need to wait for the other IOs. This holds because we ensure 2541 * the sequential IO submissions using the ZONE_APPEND command for data 2542 * and block_group->meta_write_pointer for metadata. 2543 */ 2544 if (!fully_written) { 2545 if (test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) { 2546 spin_unlock(&block_group->lock); 2547 return -EAGAIN; 2548 } 2549 spin_unlock(&block_group->lock); 2550 2551 ret = btrfs_inc_block_group_ro(block_group, false); 2552 if (ret) 2553 return ret; 2554 2555 /* Ensure all writes in this block group finish */ 2556 btrfs_wait_block_group_reservations(block_group); 2557 /* No need to wait for NOCOW writers. Zoned mode does not allow that */ 2558 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group); 2559 /* Wait for extent buffers to be written. */ 2560 if (is_metadata) 2561 wait_eb_writebacks(block_group); 2562 2563 spin_lock(&block_group->lock); 2564 2565 /* 2566 * Bail out if someone already deactivated the block group, or 2567 * allocated space is left in the block group. 2568 */ 2569 if (!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, 2570 &block_group->runtime_flags)) { 2571 spin_unlock(&block_group->lock); 2572 btrfs_dec_block_group_ro(block_group); 2573 return 0; 2574 } 2575 2576 if (block_group->reserved || 2577 test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, 2578 &block_group->runtime_flags)) { 2579 spin_unlock(&block_group->lock); 2580 btrfs_dec_block_group_ro(block_group); 2581 return -EAGAIN; 2582 } 2583 } 2584 2585 clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags); 2586 block_group->alloc_offset = block_group->zone_capacity; 2587 if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)) 2588 block_group->meta_write_pointer = block_group->start + 2589 block_group->zone_capacity; 2590 block_group->free_space_ctl->free_space = 0; 2591 btrfs_clear_treelog_bg(block_group); 2592 btrfs_clear_data_reloc_bg(block_group); 2593 spin_unlock(&block_group->lock); 2594 2595 down_read(&dev_replace->rwsem); 2596 map = block_group->physical_map; 2597 for (i = 0; i < map->num_stripes; i++) { 2598 2599 ret = call_zone_finish(block_group, &map->stripes[i]); 2600 if (ret) { 2601 up_read(&dev_replace->rwsem); 2602 return ret; 2603 } 2604 } 2605 up_read(&dev_replace->rwsem); 2606 2607 if (!fully_written) 2608 btrfs_dec_block_group_ro(block_group); 2609 2610 spin_lock(&fs_info->zone_active_bgs_lock); 2611 ASSERT(!list_empty(&block_group->active_bg_list)); 2612 list_del_init(&block_group->active_bg_list); 2613 spin_unlock(&fs_info->zone_active_bgs_lock); 2614 2615 /* For active_bg_list */ 2616 btrfs_put_block_group(block_group); 2617 2618 clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags); 2619 2620 return 0; 2621 } 2622 2623 int btrfs_zone_finish(struct btrfs_block_group *block_group) 2624 { 2625 if (!btrfs_is_zoned(block_group->fs_info)) 2626 return 0; 2627 2628 return do_zone_finish(block_group, false); 2629 } 2630 2631 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags) 2632 { 2633 struct btrfs_fs_info *fs_info = fs_devices->fs_info; 2634 struct btrfs_device *device; 2635 bool ret = false; 2636 2637 if (!btrfs_is_zoned(fs_info)) 2638 return true; 2639 2640 if (test_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags)) 2641 return false; 2642 2643 /* Check if there is a device with active zones left */ 2644 mutex_lock(&fs_info->chunk_mutex); 2645 spin_lock(&fs_info->zone_active_bgs_lock); 2646 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { 2647 struct btrfs_zoned_device_info *zinfo = device->zone_info; 2648 int reserved = 0; 2649 2650 if (!device->bdev) 2651 continue; 2652 2653 if (!zinfo->max_active_zones) { 2654 ret = true; 2655 break; 2656 } 2657 2658 if (flags & BTRFS_BLOCK_GROUP_DATA) 2659 reserved = zinfo->reserved_active_zones; 2660 2661 switch (flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) { 2662 case 0: /* single */ 2663 ret = (atomic_read(&zinfo->active_zones_left) >= (1 + reserved)); 2664 break; 2665 case BTRFS_BLOCK_GROUP_DUP: 2666 ret = (atomic_read(&zinfo->active_zones_left) >= (2 + reserved)); 2667 break; 2668 } 2669 if (ret) 2670 break; 2671 } 2672 spin_unlock(&fs_info->zone_active_bgs_lock); 2673 mutex_unlock(&fs_info->chunk_mutex); 2674 2675 if (!ret) 2676 set_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags); 2677 2678 return ret; 2679 } 2680 2681 int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length) 2682 { 2683 struct btrfs_block_group *block_group; 2684 u64 min_alloc_bytes; 2685 2686 if (!btrfs_is_zoned(fs_info)) 2687 return 0; 2688 2689 block_group = btrfs_lookup_block_group(fs_info, logical); 2690 if (WARN_ON_ONCE(!block_group)) 2691 return -ENOENT; 2692 2693 /* No MIXED_BG on zoned btrfs. */ 2694 if (block_group->flags & BTRFS_BLOCK_GROUP_DATA) 2695 min_alloc_bytes = fs_info->sectorsize; 2696 else 2697 min_alloc_bytes = fs_info->nodesize; 2698 2699 /* Bail out if we can allocate more data from this block group. */ 2700 if (logical + length + min_alloc_bytes <= 2701 block_group->start + block_group->zone_capacity) 2702 goto out; 2703 2704 do_zone_finish(block_group, true); 2705 2706 out: 2707 btrfs_put_block_group(block_group); 2708 return 0; 2709 } 2710 2711 static void btrfs_zone_finish_endio_workfn(struct work_struct *work) 2712 { 2713 int ret; 2714 struct btrfs_block_group *bg = 2715 container_of(work, struct btrfs_block_group, zone_finish_work); 2716 2717 wait_on_extent_buffer_writeback(bg->last_eb); 2718 free_extent_buffer(bg->last_eb); 2719 ret = do_zone_finish(bg, true); 2720 if (ret) 2721 btrfs_handle_fs_error(bg->fs_info, ret, 2722 "Failed to finish block-group's zone"); 2723 btrfs_put_block_group(bg); 2724 } 2725 2726 void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg, 2727 struct extent_buffer *eb) 2728 { 2729 if (!test_bit(BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE, &bg->runtime_flags) || 2730 eb->start + eb->len * 2 <= bg->start + bg->zone_capacity) 2731 return; 2732 2733 if (WARN_ON(bg->zone_finish_work.func == btrfs_zone_finish_endio_workfn)) { 2734 btrfs_err(bg->fs_info, "double scheduling of bg %llu zone finishing", 2735 bg->start); 2736 return; 2737 } 2738 2739 /* For the work */ 2740 btrfs_get_block_group(bg); 2741 refcount_inc(&eb->refs); 2742 bg->last_eb = eb; 2743 INIT_WORK(&bg->zone_finish_work, btrfs_zone_finish_endio_workfn); 2744 queue_work(system_dfl_wq, &bg->zone_finish_work); 2745 } 2746 2747 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) 2748 { 2749 struct btrfs_fs_info *fs_info = bg->fs_info; 2750 2751 spin_lock(&fs_info->relocation_bg_lock); 2752 if (fs_info->data_reloc_bg == bg->start) 2753 fs_info->data_reloc_bg = 0; 2754 spin_unlock(&fs_info->relocation_bg_lock); 2755 } 2756 2757 void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info) 2758 { 2759 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo; 2760 struct btrfs_space_info *space_info = data_sinfo; 2761 struct btrfs_trans_handle *trans; 2762 struct btrfs_block_group *bg; 2763 struct list_head *bg_list; 2764 u64 alloc_flags; 2765 bool first = true; 2766 bool did_chunk_alloc = false; 2767 int index; 2768 int ret; 2769 2770 if (!btrfs_is_zoned(fs_info)) 2771 return; 2772 2773 if (fs_info->data_reloc_bg) 2774 return; 2775 2776 if (sb_rdonly(fs_info->sb)) 2777 return; 2778 2779 alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags); 2780 index = btrfs_bg_flags_to_raid_index(alloc_flags); 2781 2782 /* Scan the data space_info to find empty block groups. Take the second one. */ 2783 again: 2784 bg_list = &space_info->block_groups[index]; 2785 list_for_each_entry(bg, bg_list, list) { 2786 if (bg->alloc_offset != 0) 2787 continue; 2788 2789 if (first) { 2790 first = false; 2791 continue; 2792 } 2793 2794 if (space_info == data_sinfo) { 2795 /* Migrate the block group to the data relocation space_info. */ 2796 struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0]; 2797 int factor; 2798 2799 ASSERT(reloc_sinfo->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC, 2800 "reloc_sinfo->subgroup_id=%d", reloc_sinfo->subgroup_id); 2801 factor = btrfs_bg_type_to_factor(bg->flags); 2802 2803 down_write(&space_info->groups_sem); 2804 list_del_init(&bg->list); 2805 /* We can assume this as we choose the second empty one. */ 2806 ASSERT(!list_empty(&space_info->block_groups[index])); 2807 up_write(&space_info->groups_sem); 2808 2809 spin_lock(&space_info->lock); 2810 space_info->total_bytes -= bg->length; 2811 space_info->disk_total -= bg->length * factor; 2812 space_info->disk_total -= bg->zone_unusable; 2813 /* There is no allocation ever happened. */ 2814 ASSERT(bg->used == 0, "bg->used=%llu", bg->used); 2815 /* No super block in a block group on the zoned setup. */ 2816 ASSERT(bg->bytes_super == 0, "bg->bytes_super=%llu", bg->bytes_super); 2817 spin_unlock(&space_info->lock); 2818 2819 bg->space_info = reloc_sinfo; 2820 if (reloc_sinfo->block_group_kobjs[index] == NULL) 2821 btrfs_sysfs_add_block_group_type(bg); 2822 2823 btrfs_add_bg_to_space_info(fs_info, bg); 2824 } 2825 2826 fs_info->data_reloc_bg = bg->start; 2827 set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags); 2828 btrfs_zone_activate(bg); 2829 2830 return; 2831 } 2832 2833 if (did_chunk_alloc) 2834 return; 2835 2836 trans = btrfs_join_transaction(fs_info->tree_root); 2837 if (IS_ERR(trans)) 2838 return; 2839 2840 /* Allocate new BG in the data relocation space_info. */ 2841 space_info = data_sinfo->sub_group[0]; 2842 ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC, 2843 "space_info->subgroup_id=%d", space_info->subgroup_id); 2844 ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE); 2845 btrfs_end_transaction(trans); 2846 if (ret == 1) { 2847 /* 2848 * We allocated a new block group in the data relocation space_info. We 2849 * can take that one. 2850 */ 2851 first = false; 2852 did_chunk_alloc = true; 2853 goto again; 2854 } 2855 } 2856 2857 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) 2858 { 2859 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2860 struct btrfs_device *device; 2861 2862 if (!btrfs_is_zoned(fs_info)) 2863 return; 2864 2865 mutex_lock(&fs_devices->device_list_mutex); 2866 list_for_each_entry(device, &fs_devices->devices, dev_list) { 2867 if (device->zone_info) { 2868 vfree(device->zone_info->zone_cache); 2869 device->zone_info->zone_cache = NULL; 2870 } 2871 } 2872 mutex_unlock(&fs_devices->device_list_mutex); 2873 } 2874 2875 bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info) 2876 { 2877 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2878 struct btrfs_device *device; 2879 u64 total = btrfs_super_total_bytes(fs_info->super_copy); 2880 u64 used = 0; 2881 u64 factor; 2882 2883 ASSERT(btrfs_is_zoned(fs_info)); 2884 2885 if (fs_info->bg_reclaim_threshold == 0) 2886 return false; 2887 2888 mutex_lock(&fs_devices->device_list_mutex); 2889 list_for_each_entry(device, &fs_devices->devices, dev_list) { 2890 if (!device->bdev) 2891 continue; 2892 2893 used += device->bytes_used; 2894 } 2895 mutex_unlock(&fs_devices->device_list_mutex); 2896 2897 factor = div64_u64(used * 100, total); 2898 return factor >= fs_info->bg_reclaim_threshold; 2899 } 2900 2901 void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical, 2902 u64 length) 2903 { 2904 struct btrfs_block_group *block_group; 2905 2906 if (!btrfs_is_zoned(fs_info)) 2907 return; 2908 2909 block_group = btrfs_lookup_block_group(fs_info, logical); 2910 /* It should be called on a previous data relocation block group. */ 2911 ASSERT(block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)); 2912 2913 spin_lock(&block_group->lock); 2914 if (!test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) 2915 goto out; 2916 2917 /* All relocation extents are written. */ 2918 if (block_group->start + block_group->alloc_offset == logical + length) { 2919 /* 2920 * Now, release this block group for further allocations and 2921 * zone finish. 2922 */ 2923 clear_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, 2924 &block_group->runtime_flags); 2925 } 2926 2927 out: 2928 spin_unlock(&block_group->lock); 2929 btrfs_put_block_group(block_group); 2930 } 2931 2932 int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info) 2933 { 2934 struct btrfs_block_group *block_group; 2935 struct btrfs_block_group *min_bg = NULL; 2936 u64 min_avail = U64_MAX; 2937 int ret; 2938 2939 spin_lock(&fs_info->zone_active_bgs_lock); 2940 list_for_each_entry(block_group, &fs_info->zone_active_bgs, 2941 active_bg_list) { 2942 u64 avail; 2943 2944 spin_lock(&block_group->lock); 2945 if (block_group->reserved || block_group->alloc_offset == 0 || 2946 !(block_group->flags & BTRFS_BLOCK_GROUP_DATA) || 2947 test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &block_group->runtime_flags)) { 2948 spin_unlock(&block_group->lock); 2949 continue; 2950 } 2951 2952 avail = block_group->zone_capacity - block_group->alloc_offset; 2953 if (min_avail > avail) { 2954 if (min_bg) 2955 btrfs_put_block_group(min_bg); 2956 min_bg = block_group; 2957 min_avail = avail; 2958 btrfs_get_block_group(min_bg); 2959 } 2960 spin_unlock(&block_group->lock); 2961 } 2962 spin_unlock(&fs_info->zone_active_bgs_lock); 2963 2964 if (!min_bg) 2965 return 0; 2966 2967 ret = btrfs_zone_finish(min_bg); 2968 btrfs_put_block_group(min_bg); 2969 2970 return ret < 0 ? ret : 1; 2971 } 2972 2973 int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish) 2974 { 2975 struct btrfs_fs_info *fs_info = space_info->fs_info; 2976 struct btrfs_block_group *bg; 2977 int index; 2978 2979 if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA)) 2980 return 0; 2981 2982 for (;;) { 2983 int ret; 2984 bool need_finish = false; 2985 2986 down_read(&space_info->groups_sem); 2987 for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) { 2988 list_for_each_entry(bg, &space_info->block_groups[index], 2989 list) { 2990 if (!spin_trylock(&bg->lock)) 2991 continue; 2992 if (btrfs_zoned_bg_is_full(bg) || 2993 test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, 2994 &bg->runtime_flags)) { 2995 spin_unlock(&bg->lock); 2996 continue; 2997 } 2998 spin_unlock(&bg->lock); 2999 3000 if (btrfs_zone_activate(bg)) { 3001 up_read(&space_info->groups_sem); 3002 return 1; 3003 } 3004 3005 need_finish = true; 3006 } 3007 } 3008 up_read(&space_info->groups_sem); 3009 3010 if (!do_finish || !need_finish) 3011 break; 3012 3013 ret = btrfs_zone_finish_one_bg(fs_info); 3014 if (ret == 0) 3015 break; 3016 if (ret < 0) 3017 return ret; 3018 } 3019 3020 return 0; 3021 } 3022 3023 /* 3024 * Reserve zones for one metadata block group, one tree-log block group, and one 3025 * system block group. 3026 */ 3027 void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info) 3028 { 3029 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 3030 struct btrfs_block_group *block_group; 3031 struct btrfs_device *device; 3032 /* Reserve zones for normal SINGLE metadata and tree-log block group. */ 3033 unsigned int metadata_reserve = 2; 3034 /* Reserve a zone for SINGLE system block group. */ 3035 unsigned int system_reserve = 1; 3036 3037 if (!test_bit(BTRFS_FS_ACTIVE_ZONE_TRACKING, &fs_info->flags)) 3038 return; 3039 3040 /* 3041 * This function is called from the mount context. So, there is no 3042 * parallel process touching the bits. No need for read_seqretry(). 3043 */ 3044 if (fs_info->avail_metadata_alloc_bits & BTRFS_BLOCK_GROUP_DUP) 3045 metadata_reserve = 4; 3046 if (fs_info->avail_system_alloc_bits & BTRFS_BLOCK_GROUP_DUP) 3047 system_reserve = 2; 3048 3049 /* Apply the reservation on all the devices. */ 3050 mutex_lock(&fs_devices->device_list_mutex); 3051 list_for_each_entry(device, &fs_devices->devices, dev_list) { 3052 if (!device->bdev) 3053 continue; 3054 3055 device->zone_info->reserved_active_zones = 3056 metadata_reserve + system_reserve; 3057 } 3058 mutex_unlock(&fs_devices->device_list_mutex); 3059 3060 /* Release reservation for currently active block groups. */ 3061 spin_lock(&fs_info->zone_active_bgs_lock); 3062 list_for_each_entry(block_group, &fs_info->zone_active_bgs, active_bg_list) { 3063 struct btrfs_chunk_map *map = block_group->physical_map; 3064 3065 if (!(block_group->flags & 3066 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))) 3067 continue; 3068 3069 for (int i = 0; i < map->num_stripes; i++) 3070 map->stripes[i].dev->zone_info->reserved_active_zones--; 3071 } 3072 spin_unlock(&fs_info->zone_active_bgs_lock); 3073 } 3074 3075 /* 3076 * Reset the zones of unused block groups from @space_info->bytes_zone_unusable. 3077 * 3078 * @space_info: the space to work on 3079 * @num_bytes: targeting reclaim bytes 3080 * 3081 * This one resets the zones of a block group, so we can reuse the region 3082 * without removing the block group. On the other hand, btrfs_delete_unused_bgs() 3083 * just removes a block group and frees up the underlying zones. So, we still 3084 * need to allocate a new block group to reuse the zones. 3085 * 3086 * Resetting is faster than deleting/recreating a block group. It is similar 3087 * to freeing the logical space on the regular mode. However, we cannot change 3088 * the block group's profile with this operation. 3089 */ 3090 int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes) 3091 { 3092 struct btrfs_fs_info *fs_info = space_info->fs_info; 3093 const sector_t zone_size_sectors = fs_info->zone_size >> SECTOR_SHIFT; 3094 3095 if (!btrfs_is_zoned(fs_info)) 3096 return 0; 3097 3098 while (num_bytes > 0) { 3099 struct btrfs_chunk_map *map; 3100 struct btrfs_block_group *bg = NULL; 3101 bool found = false; 3102 u64 reclaimed = 0; 3103 3104 /* 3105 * Here, we choose a fully zone_unusable block group. It's 3106 * technically possible to reset a partly zone_unusable block 3107 * group, which still has some free space left. However, 3108 * handling that needs to cope with the allocation side, which 3109 * makes the logic more complex. So, let's handle the easy case 3110 * for now. 3111 */ 3112 spin_lock(&fs_info->unused_bgs_lock); 3113 list_for_each_entry(bg, &fs_info->unused_bgs, bg_list) { 3114 if ((bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != space_info->flags) 3115 continue; 3116 3117 /* 3118 * Use trylock to avoid locking order violation. In 3119 * btrfs_reclaim_bgs_work(), the lock order is 3120 * &bg->lock -> &fs_info->unused_bgs_lock. We skip a 3121 * block group if we cannot take its lock. 3122 */ 3123 if (!spin_trylock(&bg->lock)) 3124 continue; 3125 if (btrfs_is_block_group_used(bg) || bg->zone_unusable < bg->length) { 3126 spin_unlock(&bg->lock); 3127 continue; 3128 } 3129 spin_unlock(&bg->lock); 3130 found = true; 3131 break; 3132 } 3133 if (!found) { 3134 spin_unlock(&fs_info->unused_bgs_lock); 3135 return 0; 3136 } 3137 3138 list_del_init(&bg->bg_list); 3139 btrfs_put_block_group(bg); 3140 spin_unlock(&fs_info->unused_bgs_lock); 3141 3142 /* 3143 * Since the block group is fully zone_unusable and we cannot 3144 * allocate from this block group anymore, we don't need to set 3145 * this block group read-only. 3146 */ 3147 3148 down_read(&fs_info->dev_replace.rwsem); 3149 map = bg->physical_map; 3150 for (int i = 0; i < map->num_stripes; i++) { 3151 struct btrfs_io_stripe *stripe = &map->stripes[i]; 3152 unsigned int nofs_flags; 3153 int ret; 3154 3155 nofs_flags = memalloc_nofs_save(); 3156 ret = blkdev_zone_mgmt(stripe->dev->bdev, REQ_OP_ZONE_RESET, 3157 stripe->physical >> SECTOR_SHIFT, 3158 zone_size_sectors); 3159 memalloc_nofs_restore(nofs_flags); 3160 3161 if (ret) { 3162 up_read(&fs_info->dev_replace.rwsem); 3163 return ret; 3164 } 3165 } 3166 up_read(&fs_info->dev_replace.rwsem); 3167 3168 spin_lock(&space_info->lock); 3169 spin_lock(&bg->lock); 3170 ASSERT(!btrfs_is_block_group_used(bg)); 3171 if (bg->ro) { 3172 spin_unlock(&bg->lock); 3173 spin_unlock(&space_info->lock); 3174 continue; 3175 } 3176 3177 reclaimed = bg->alloc_offset; 3178 bg->zone_unusable = bg->length - bg->zone_capacity; 3179 bg->alloc_offset = 0; 3180 /* 3181 * This holds because we currently reset fully used then freed 3182 * block group. 3183 */ 3184 ASSERT(reclaimed == bg->zone_capacity, 3185 "reclaimed=%llu bg->zone_capacity=%llu", reclaimed, bg->zone_capacity); 3186 bg->free_space_ctl->free_space += reclaimed; 3187 space_info->bytes_zone_unusable -= reclaimed; 3188 spin_unlock(&bg->lock); 3189 btrfs_return_free_space(space_info, reclaimed); 3190 spin_unlock(&space_info->lock); 3191 3192 if (num_bytes <= reclaimed) 3193 break; 3194 num_bytes -= reclaimed; 3195 } 3196 3197 return 0; 3198 } 3199 3200 void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq) 3201 { 3202 struct btrfs_block_group *bg; 3203 u64 data_reloc_bg; 3204 u64 treelog_bg; 3205 3206 seq_puts(seq, "\n zoned statistics:\n"); 3207 3208 spin_lock(&fs_info->zone_active_bgs_lock); 3209 seq_printf(seq, "\tactive block-groups: %zu\n", 3210 list_count_nodes(&fs_info->zone_active_bgs)); 3211 spin_unlock(&fs_info->zone_active_bgs_lock); 3212 3213 spin_lock(&fs_info->unused_bgs_lock); 3214 seq_printf(seq, "\t reclaimable: %zu\n", 3215 list_count_nodes(&fs_info->reclaim_bgs)); 3216 seq_printf(seq, "\t unused: %zu\n", list_count_nodes(&fs_info->unused_bgs)); 3217 spin_unlock(&fs_info->unused_bgs_lock); 3218 3219 seq_printf(seq,"\t need reclaim: %s\n", 3220 str_true_false(btrfs_zoned_should_reclaim(fs_info))); 3221 3222 data_reloc_bg = data_race(fs_info->data_reloc_bg); 3223 if (data_reloc_bg) 3224 seq_printf(seq, "\tdata relocation block-group: %llu\n", 3225 data_reloc_bg); 3226 treelog_bg = data_race(fs_info->treelog_bg); 3227 if (treelog_bg) 3228 seq_printf(seq, "\ttree-log block-group: %llu\n", treelog_bg); 3229 3230 spin_lock(&fs_info->zone_active_bgs_lock); 3231 seq_puts(seq, "\tactive zones:\n"); 3232 list_for_each_entry(bg, &fs_info->zone_active_bgs, active_bg_list) { 3233 u64 start; 3234 u64 alloc_offset; 3235 u64 used; 3236 u64 reserved; 3237 u64 zone_unusable; 3238 const char *typestr = btrfs_space_info_type_str(bg->space_info); 3239 3240 spin_lock(&bg->lock); 3241 start = bg->start; 3242 alloc_offset = bg->alloc_offset; 3243 used = bg->used; 3244 reserved = bg->reserved; 3245 zone_unusable = bg->zone_unusable; 3246 spin_unlock(&bg->lock); 3247 3248 seq_printf(seq, 3249 "\t start: %llu, wp: %llu used: %llu, reserved: %llu, unusable: %llu (%s)\n", 3250 start, alloc_offset, used, reserved, zone_unusable, typestr); 3251 } 3252 spin_unlock(&fs_info->zone_active_bgs_lock); 3253 } 3254