1 // SPDX-License-Identifier: GPL-2.0
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>
12 #include "rcu-string.h"
14 #include "block-group.h"
15 #include "transaction.h"
16 #include "dev-replace.h"
17 #include "space-info.h"
19 /* Maximum number of zones to report per blkdev_report_zones() call */
20 #define BTRFS_REPORT_NR_ZONES 4096
21 /* Invalid allocation pointer value for missing devices */
22 #define WP_MISSING_DEV ((u64)-1)
23 /* Pseudo write pointer value for conventional zone */
24 #define WP_CONVENTIONAL ((u64)-2)
27 * Location of the first zone of superblock logging zone pairs.
29 * - primary superblock: 0B (zone 0)
30 * - first copy: 512G (zone starting at that offset)
31 * - second copy: 4T (zone starting at that offset)
33 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
34 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
35 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
37 #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
38 #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
40 /* Number of superblock log zones */
41 #define BTRFS_NR_SB_LOG_ZONES 2
44 * Minimum of active zones we need:
46 * - BTRFS_SUPER_MIRROR_MAX zones for superblock mirrors
47 * - 3 zones to ensure at least one zone per SYSTEM, META and DATA block group
48 * - 1 zone for tree-log dedicated block group
49 * - 1 zone for relocation
51 #define BTRFS_MIN_ACTIVE_ZONES (BTRFS_SUPER_MIRROR_MAX + 5)
54 * Maximum supported zone size. Currently, SMR disks have a zone size of
55 * 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range. We do not
56 * expect the zone size to become larger than 8GiB in the near future.
58 #define BTRFS_MAX_ZONE_SIZE SZ_8G
60 #define SUPER_INFO_SECTORS ((u64)BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT)
62 static inline bool sb_zone_is_full(const struct blk_zone *zone)
64 return (zone->cond == BLK_ZONE_COND_FULL) ||
65 (zone->wp + SUPER_INFO_SECTORS > zone->start + zone->capacity);
68 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
70 struct blk_zone *zones = data;
72 memcpy(&zones[idx], zone, sizeof(*zone));
77 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
80 bool empty[BTRFS_NR_SB_LOG_ZONES];
81 bool full[BTRFS_NR_SB_LOG_ZONES];
85 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
86 ASSERT(zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL);
87 empty[i] = (zones[i].cond == BLK_ZONE_COND_EMPTY);
88 full[i] = sb_zone_is_full(&zones[i]);
92 * Possible states of log buffer zones
94 * Empty[0] In use[0] Full[0]
100 * *: Special case, no superblock is written
101 * 0: Use write pointer of zones[0]
102 * 1: Use write pointer of zones[1]
103 * C: Compare super blocks from zones[0] and zones[1], use the latest
104 * one determined by generation
108 if (empty[0] && empty[1]) {
109 /* Special case to distinguish no superblock to read */
110 *wp_ret = zones[0].start << SECTOR_SHIFT;
112 } else if (full[0] && full[1]) {
113 /* Compare two super blocks */
114 struct address_space *mapping = bdev->bd_inode->i_mapping;
115 struct page *page[BTRFS_NR_SB_LOG_ZONES];
116 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
119 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
122 bytenr = ((zones[i].start + zones[i].len)
123 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
125 page[i] = read_cache_page_gfp(mapping,
126 bytenr >> PAGE_SHIFT, GFP_NOFS);
127 if (IS_ERR(page[i])) {
129 btrfs_release_disk_super(super[0]);
130 return PTR_ERR(page[i]);
132 super[i] = page_address(page[i]);
135 if (super[0]->generation > super[1]->generation)
136 sector = zones[1].start;
138 sector = zones[0].start;
140 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
141 btrfs_release_disk_super(super[i]);
142 } else if (!full[0] && (empty[1] || full[1])) {
143 sector = zones[0].wp;
144 } else if (full[0]) {
145 sector = zones[1].wp;
149 *wp_ret = sector << SECTOR_SHIFT;
154 * Get the first zone number of the superblock mirror
156 static inline u32 sb_zone_number(int shift, int mirror)
160 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
162 case 0: zone = 0; break;
163 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
164 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
167 ASSERT(zone <= U32_MAX);
172 static inline sector_t zone_start_sector(u32 zone_number,
173 struct block_device *bdev)
175 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
178 static inline u64 zone_start_physical(u32 zone_number,
179 struct btrfs_zoned_device_info *zone_info)
181 return (u64)zone_number << zone_info->zone_size_shift;
185 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
186 * device into static sized chunks and fake a conventional zone on each of
189 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
190 struct blk_zone *zones, unsigned int nr_zones)
192 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
193 sector_t bdev_size = bdev_nr_sectors(device->bdev);
196 pos >>= SECTOR_SHIFT;
197 for (i = 0; i < nr_zones; i++) {
198 zones[i].start = i * zone_sectors + pos;
199 zones[i].len = zone_sectors;
200 zones[i].capacity = zone_sectors;
201 zones[i].wp = zones[i].start + zone_sectors;
202 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
203 zones[i].cond = BLK_ZONE_COND_NOT_WP;
205 if (zones[i].wp >= bdev_size) {
214 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
215 struct blk_zone *zones, unsigned int *nr_zones)
217 struct btrfs_zoned_device_info *zinfo = device->zone_info;
224 if (!bdev_is_zoned(device->bdev)) {
225 ret = emulate_report_zones(device, pos, zones, *nr_zones);
231 if (zinfo->zone_cache) {
234 ASSERT(IS_ALIGNED(pos, zinfo->zone_size));
235 zno = pos >> zinfo->zone_size_shift;
237 * We cannot report zones beyond the zone end. So, it is OK to
238 * cap *nr_zones to at the end.
240 *nr_zones = min_t(u32, *nr_zones, zinfo->nr_zones - zno);
242 for (i = 0; i < *nr_zones; i++) {
243 struct blk_zone *zone_info;
245 zone_info = &zinfo->zone_cache[zno + i];
250 if (i == *nr_zones) {
251 /* Cache hit on all the zones */
252 memcpy(zones, zinfo->zone_cache + zno,
253 sizeof(*zinfo->zone_cache) * *nr_zones);
258 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
259 copy_zone_info_cb, zones);
261 btrfs_err_in_rcu(device->fs_info,
262 "zoned: failed to read zone %llu on %s (devid %llu)",
263 pos, rcu_str_deref(device->name),
272 if (zinfo->zone_cache)
273 memcpy(zinfo->zone_cache + zno, zones,
274 sizeof(*zinfo->zone_cache) * *nr_zones);
279 /* The emulated zone size is determined from the size of device extent */
280 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
282 struct btrfs_path *path;
283 struct btrfs_root *root = fs_info->dev_root;
284 struct btrfs_key key;
285 struct extent_buffer *leaf;
286 struct btrfs_dev_extent *dext;
290 key.type = BTRFS_DEV_EXTENT_KEY;
293 path = btrfs_alloc_path();
297 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
301 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
302 ret = btrfs_next_leaf(root, path);
305 /* No dev extents at all? Not good */
312 leaf = path->nodes[0];
313 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
314 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
318 btrfs_free_path(path);
323 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
325 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
326 struct btrfs_device *device;
329 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
330 if (!btrfs_fs_incompat(fs_info, ZONED))
333 mutex_lock(&fs_devices->device_list_mutex);
334 list_for_each_entry(device, &fs_devices->devices, dev_list) {
335 /* We can skip reading of zone info for missing devices */
339 ret = btrfs_get_dev_zone_info(device, true);
343 mutex_unlock(&fs_devices->device_list_mutex);
348 int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
350 struct btrfs_fs_info *fs_info = device->fs_info;
351 struct btrfs_zoned_device_info *zone_info = NULL;
352 struct block_device *bdev = device->bdev;
353 struct request_queue *queue = bdev_get_queue(bdev);
354 unsigned int max_active_zones;
355 unsigned int nactive;
358 struct blk_zone *zones = NULL;
359 unsigned int i, nreported = 0, nr_zones;
360 sector_t zone_sectors;
361 char *model, *emulated;
365 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
368 if (!btrfs_fs_incompat(fs_info, ZONED))
371 if (device->zone_info)
374 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
378 device->zone_info = zone_info;
380 if (!bdev_is_zoned(bdev)) {
381 if (!fs_info->zone_size) {
382 ret = calculate_emulated_zone_size(fs_info);
387 ASSERT(fs_info->zone_size);
388 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
390 zone_sectors = bdev_zone_sectors(bdev);
393 /* Check if it's power of 2 (see is_power_of_2) */
394 ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
395 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
397 /* We reject devices with a zone size larger than 8GB */
398 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
399 btrfs_err_in_rcu(fs_info,
400 "zoned: %s: zone size %llu larger than supported maximum %llu",
401 rcu_str_deref(device->name),
402 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
407 nr_sectors = bdev_nr_sectors(bdev);
408 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
409 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
410 if (!IS_ALIGNED(nr_sectors, zone_sectors))
411 zone_info->nr_zones++;
413 max_active_zones = queue_max_active_zones(queue);
414 if (max_active_zones && max_active_zones < BTRFS_MIN_ACTIVE_ZONES) {
415 btrfs_err_in_rcu(fs_info,
416 "zoned: %s: max active zones %u is too small, need at least %u active zones",
417 rcu_str_deref(device->name), max_active_zones,
418 BTRFS_MIN_ACTIVE_ZONES);
422 zone_info->max_active_zones = max_active_zones;
424 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
425 if (!zone_info->seq_zones) {
430 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
431 if (!zone_info->empty_zones) {
436 zone_info->active_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
437 if (!zone_info->active_zones) {
442 zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
449 * Enable zone cache only for a zoned device. On a non-zoned device, we
450 * fill the zone info with emulated CONVENTIONAL zones, so no need to
453 if (populate_cache && bdev_is_zoned(device->bdev)) {
454 zone_info->zone_cache = vzalloc(sizeof(struct blk_zone) *
455 zone_info->nr_zones);
456 if (!zone_info->zone_cache) {
457 btrfs_err_in_rcu(device->fs_info,
458 "zoned: failed to allocate zone cache for %s",
459 rcu_str_deref(device->name));
467 while (sector < nr_sectors) {
468 nr_zones = BTRFS_REPORT_NR_ZONES;
469 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
474 for (i = 0; i < nr_zones; i++) {
475 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
476 __set_bit(nreported, zone_info->seq_zones);
477 switch (zones[i].cond) {
478 case BLK_ZONE_COND_EMPTY:
479 __set_bit(nreported, zone_info->empty_zones);
481 case BLK_ZONE_COND_IMP_OPEN:
482 case BLK_ZONE_COND_EXP_OPEN:
483 case BLK_ZONE_COND_CLOSED:
484 __set_bit(nreported, zone_info->active_zones);
490 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
493 if (nreported != zone_info->nr_zones) {
494 btrfs_err_in_rcu(device->fs_info,
495 "inconsistent number of zones on %s (%u/%u)",
496 rcu_str_deref(device->name), nreported,
497 zone_info->nr_zones);
502 if (max_active_zones) {
503 if (nactive > max_active_zones) {
504 btrfs_err_in_rcu(device->fs_info,
505 "zoned: %u active zones on %s exceeds max_active_zones %u",
506 nactive, rcu_str_deref(device->name),
511 atomic_set(&zone_info->active_zones_left,
512 max_active_zones - nactive);
515 /* Validate superblock log */
516 nr_zones = BTRFS_NR_SB_LOG_ZONES;
517 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
520 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
522 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
523 if (sb_zone + 1 >= zone_info->nr_zones)
526 ret = btrfs_get_dev_zones(device,
527 zone_start_physical(sb_zone, zone_info),
528 &zone_info->sb_zones[sb_pos],
533 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
534 btrfs_err_in_rcu(device->fs_info,
535 "zoned: failed to read super block log zone info at devid %llu zone %u",
536 device->devid, sb_zone);
542 * If zones[0] is conventional, always use the beginning of the
543 * zone to record superblock. No need to validate in that case.
545 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
546 BLK_ZONE_TYPE_CONVENTIONAL)
549 ret = sb_write_pointer(device->bdev,
550 &zone_info->sb_zones[sb_pos], &sb_wp);
551 if (ret != -ENOENT && ret) {
552 btrfs_err_in_rcu(device->fs_info,
553 "zoned: super block log zone corrupted devid %llu zone %u",
554 device->devid, sb_zone);
563 switch (bdev_zoned_model(bdev)) {
565 model = "host-managed zoned";
569 model = "host-aware zoned";
574 emulated = "emulated ";
578 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
579 bdev_zoned_model(bdev),
580 rcu_str_deref(device->name));
582 goto out_free_zone_info;
585 btrfs_info_in_rcu(fs_info,
586 "%s block device %s, %u %szones of %llu bytes",
587 model, rcu_str_deref(device->name), zone_info->nr_zones,
588 emulated, zone_info->zone_size);
595 btrfs_destroy_dev_zone_info(device);
600 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
602 struct btrfs_zoned_device_info *zone_info = device->zone_info;
607 bitmap_free(zone_info->active_zones);
608 bitmap_free(zone_info->seq_zones);
609 bitmap_free(zone_info->empty_zones);
610 vfree(zone_info->zone_cache);
612 device->zone_info = NULL;
615 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
616 struct blk_zone *zone)
618 unsigned int nr_zones = 1;
621 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
622 if (ret != 0 || !nr_zones)
623 return ret ? ret : -EIO;
628 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
630 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
631 struct btrfs_device *device;
632 u64 zoned_devices = 0;
635 const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED);
638 /* Count zoned devices */
639 list_for_each_entry(device, &fs_devices->devices, dev_list) {
640 enum blk_zoned_model model;
645 model = bdev_zoned_model(device->bdev);
647 * A Host-Managed zoned device must be used as a zoned device.
648 * A Host-Aware zoned device and a non-zoned devices can be
649 * treated as a zoned device, if ZONED flag is enabled in the
652 if (model == BLK_ZONED_HM ||
653 (model == BLK_ZONED_HA && incompat_zoned) ||
654 (model == BLK_ZONED_NONE && incompat_zoned)) {
655 struct btrfs_zoned_device_info *zone_info =
658 zone_info = device->zone_info;
661 zone_size = zone_info->zone_size;
662 } else if (zone_info->zone_size != zone_size) {
664 "zoned: unequal block device zone sizes: have %llu found %llu",
665 device->zone_info->zone_size,
674 if (!zoned_devices && !incompat_zoned)
677 if (!zoned_devices && incompat_zoned) {
678 /* No zoned block device found on ZONED filesystem */
680 "zoned: no zoned devices found on a zoned filesystem");
685 if (zoned_devices && !incompat_zoned) {
687 "zoned: mode not enabled but zoned device found");
692 if (zoned_devices != nr_devices) {
694 "zoned: cannot mix zoned and regular devices");
700 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
701 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
702 * check the alignment here.
704 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
706 "zoned: zone size %llu not aligned to stripe %u",
707 zone_size, BTRFS_STRIPE_LEN);
712 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
713 btrfs_err(fs_info, "zoned: mixed block groups not supported");
718 fs_info->zone_size = zone_size;
719 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
722 * Check mount options here, because we might change fs_info->zoned
723 * from fs_info->zone_size.
725 ret = btrfs_check_mountopts_zoned(fs_info);
729 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
734 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
736 if (!btrfs_is_zoned(info))
740 * Space cache writing is not COWed. Disable that to avoid write errors
741 * in sequential zones.
743 if (btrfs_test_opt(info, SPACE_CACHE)) {
744 btrfs_err(info, "zoned: space cache v1 is not supported");
748 if (btrfs_test_opt(info, NODATACOW)) {
749 btrfs_err(info, "zoned: NODATACOW not supported");
756 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
757 int rw, u64 *bytenr_ret)
762 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
763 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
767 ret = sb_write_pointer(bdev, zones, &wp);
768 if (ret != -ENOENT && ret < 0)
772 struct blk_zone *reset = NULL;
774 if (wp == zones[0].start << SECTOR_SHIFT)
776 else if (wp == zones[1].start << SECTOR_SHIFT)
779 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
780 ASSERT(sb_zone_is_full(reset));
782 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
783 reset->start, reset->len,
788 reset->cond = BLK_ZONE_COND_EMPTY;
789 reset->wp = reset->start;
791 } else if (ret != -ENOENT) {
793 * For READ, we want the previous one. Move write pointer to
794 * the end of a zone, if it is at the head of a zone.
798 if (wp == zones[0].start << SECTOR_SHIFT)
799 zone_end = zones[1].start + zones[1].capacity;
800 else if (wp == zones[1].start << SECTOR_SHIFT)
801 zone_end = zones[0].start + zones[0].capacity;
803 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
804 BTRFS_SUPER_INFO_SIZE);
806 wp -= BTRFS_SUPER_INFO_SIZE;
814 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
817 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
818 sector_t zone_sectors;
821 u8 zone_sectors_shift;
825 if (!bdev_is_zoned(bdev)) {
826 *bytenr_ret = btrfs_sb_offset(mirror);
830 ASSERT(rw == READ || rw == WRITE);
832 zone_sectors = bdev_zone_sectors(bdev);
833 if (!is_power_of_2(zone_sectors))
835 zone_sectors_shift = ilog2(zone_sectors);
836 nr_sectors = bdev_nr_sectors(bdev);
837 nr_zones = nr_sectors >> zone_sectors_shift;
839 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
840 if (sb_zone + 1 >= nr_zones)
843 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
844 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
848 if (ret != BTRFS_NR_SB_LOG_ZONES)
851 return sb_log_location(bdev, zones, rw, bytenr_ret);
854 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
857 struct btrfs_zoned_device_info *zinfo = device->zone_info;
861 * For a zoned filesystem on a non-zoned block device, use the same
862 * super block locations as regular filesystem. Doing so, the super
863 * block can always be retrieved and the zoned flag of the volume
864 * detected from the super block information.
866 if (!bdev_is_zoned(device->bdev)) {
867 *bytenr_ret = btrfs_sb_offset(mirror);
871 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
872 if (zone_num + 1 >= zinfo->nr_zones)
875 return sb_log_location(device->bdev,
876 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
880 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
888 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
889 if (zone_num + 1 >= zinfo->nr_zones)
892 if (!test_bit(zone_num, zinfo->seq_zones))
898 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
900 struct btrfs_zoned_device_info *zinfo = device->zone_info;
901 struct blk_zone *zone;
904 if (!is_sb_log_zone(zinfo, mirror))
907 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
908 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
909 /* Advance the next zone */
910 if (zone->cond == BLK_ZONE_COND_FULL) {
915 if (zone->cond == BLK_ZONE_COND_EMPTY)
916 zone->cond = BLK_ZONE_COND_IMP_OPEN;
918 zone->wp += SUPER_INFO_SECTORS;
920 if (sb_zone_is_full(zone)) {
922 * No room left to write new superblock. Since
923 * superblock is written with REQ_SYNC, it is safe to
924 * finish the zone now.
926 * If the write pointer is exactly at the capacity,
927 * explicit ZONE_FINISH is not necessary.
929 if (zone->wp != zone->start + zone->capacity) {
932 ret = blkdev_zone_mgmt(device->bdev,
933 REQ_OP_ZONE_FINISH, zone->start,
934 zone->len, GFP_NOFS);
939 zone->wp = zone->start + zone->len;
940 zone->cond = BLK_ZONE_COND_FULL;
945 /* All the zones are FULL. Should not reach here. */
950 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
952 sector_t zone_sectors;
954 u8 zone_sectors_shift;
958 zone_sectors = bdev_zone_sectors(bdev);
959 zone_sectors_shift = ilog2(zone_sectors);
960 nr_sectors = bdev_nr_sectors(bdev);
961 nr_zones = nr_sectors >> zone_sectors_shift;
963 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
964 if (sb_zone + 1 >= nr_zones)
967 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
968 zone_start_sector(sb_zone, bdev),
969 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
973 * btrfs_find_allocatable_zones - find allocatable zones within a given region
975 * @device: the device to allocate a region on
976 * @hole_start: the position of the hole to allocate the region
977 * @num_bytes: size of wanted region
978 * @hole_end: the end of the hole
979 * @return: position of allocatable zones
981 * Allocatable region should not contain any superblock locations.
983 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
984 u64 hole_end, u64 num_bytes)
986 struct btrfs_zoned_device_info *zinfo = device->zone_info;
987 const u8 shift = zinfo->zone_size_shift;
988 u64 nzones = num_bytes >> shift;
989 u64 pos = hole_start;
994 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
995 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
997 while (pos < hole_end) {
998 begin = pos >> shift;
999 end = begin + nzones;
1001 if (end > zinfo->nr_zones)
1004 /* Check if zones in the region are all empty */
1005 if (btrfs_dev_is_sequential(device, pos) &&
1006 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1007 pos += zinfo->zone_size;
1012 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1016 sb_zone = sb_zone_number(shift, i);
1017 if (!(end <= sb_zone ||
1018 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1020 pos = zone_start_physical(
1021 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1025 /* We also need to exclude regular superblock positions */
1026 sb_pos = btrfs_sb_offset(i);
1027 if (!(pos + num_bytes <= sb_pos ||
1028 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1030 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1042 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1044 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1045 unsigned int zno = (pos >> zone_info->zone_size_shift);
1047 /* We can use any number of zones */
1048 if (zone_info->max_active_zones == 0)
1051 if (!test_bit(zno, zone_info->active_zones)) {
1052 /* Active zone left? */
1053 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1055 if (test_and_set_bit(zno, zone_info->active_zones)) {
1056 /* Someone already set the bit */
1057 atomic_inc(&zone_info->active_zones_left);
1064 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1066 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1067 unsigned int zno = (pos >> zone_info->zone_size_shift);
1069 /* We can use any number of zones */
1070 if (zone_info->max_active_zones == 0)
1073 if (test_and_clear_bit(zno, zone_info->active_zones))
1074 atomic_inc(&zone_info->active_zones_left);
1077 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1078 u64 length, u64 *bytes)
1083 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1084 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1091 btrfs_dev_set_zone_empty(device, physical);
1092 btrfs_dev_clear_active_zone(device, physical);
1093 physical += device->zone_info->zone_size;
1094 length -= device->zone_info->zone_size;
1100 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1102 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1103 const u8 shift = zinfo->zone_size_shift;
1104 unsigned long begin = start >> shift;
1105 unsigned long end = (start + size) >> shift;
1109 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1110 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1112 if (end > zinfo->nr_zones)
1115 /* All the zones are conventional */
1116 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1119 /* All the zones are sequential and empty */
1120 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1121 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1124 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1127 if (!btrfs_dev_is_sequential(device, pos) ||
1128 btrfs_dev_is_empty_zone(device, pos))
1131 /* Free regions should be empty */
1134 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1135 rcu_str_deref(device->name), device->devid, pos >> shift);
1138 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1148 * Calculate an allocation pointer from the extent allocation information
1149 * for a block group consist of conventional zones. It is pointed to the
1150 * end of the highest addressed extent in the block group as an allocation
1153 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1156 struct btrfs_fs_info *fs_info = cache->fs_info;
1157 struct btrfs_root *root;
1158 struct btrfs_path *path;
1159 struct btrfs_key key;
1160 struct btrfs_key found_key;
1164 path = btrfs_alloc_path();
1168 key.objectid = cache->start + cache->length;
1172 root = btrfs_extent_root(fs_info, key.objectid);
1173 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1174 /* We should not find the exact match */
1180 ret = btrfs_previous_extent_item(root, path, cache->start);
1189 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1191 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1192 length = found_key.offset;
1194 length = fs_info->nodesize;
1196 if (!(found_key.objectid >= cache->start &&
1197 found_key.objectid + length <= cache->start + cache->length)) {
1201 *offset_ret = found_key.objectid + length - cache->start;
1205 btrfs_free_path(path);
1209 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1211 struct btrfs_fs_info *fs_info = cache->fs_info;
1212 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1213 struct extent_map *em;
1214 struct map_lookup *map;
1215 struct btrfs_device *device;
1216 u64 logical = cache->start;
1217 u64 length = cache->length;
1221 unsigned int nofs_flag;
1222 u64 *alloc_offsets = NULL;
1224 unsigned long *active = NULL;
1226 u32 num_sequential = 0, num_conventional = 0;
1228 if (!btrfs_is_zoned(fs_info))
1232 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1234 "zoned: block group %llu len %llu unaligned to zone size %llu",
1235 logical, length, fs_info->zone_size);
1239 /* Get the chunk mapping */
1240 read_lock(&em_tree->lock);
1241 em = lookup_extent_mapping(em_tree, logical, length);
1242 read_unlock(&em_tree->lock);
1247 map = em->map_lookup;
1249 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1250 if (!cache->physical_map) {
1255 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1256 if (!alloc_offsets) {
1261 caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1267 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1273 for (i = 0; i < map->num_stripes; i++) {
1275 struct blk_zone zone;
1276 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1277 int dev_replace_is_ongoing = 0;
1279 device = map->stripes[i].dev;
1280 physical = map->stripes[i].physical;
1282 if (device->bdev == NULL) {
1283 alloc_offsets[i] = WP_MISSING_DEV;
1287 is_sequential = btrfs_dev_is_sequential(device, physical);
1293 if (!is_sequential) {
1294 alloc_offsets[i] = WP_CONVENTIONAL;
1299 * This zone will be used for allocation, so mark this zone
1302 btrfs_dev_clear_zone_empty(device, physical);
1304 down_read(&dev_replace->rwsem);
1305 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1306 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1307 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical);
1308 up_read(&dev_replace->rwsem);
1311 * The group is mapped to a sequential zone. Get the zone write
1312 * pointer to determine the allocation offset within the zone.
1314 WARN_ON(!IS_ALIGNED(physical, fs_info->zone_size));
1315 nofs_flag = memalloc_nofs_save();
1316 ret = btrfs_get_dev_zone(device, physical, &zone);
1317 memalloc_nofs_restore(nofs_flag);
1318 if (ret == -EIO || ret == -EOPNOTSUPP) {
1320 alloc_offsets[i] = WP_MISSING_DEV;
1326 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1327 btrfs_err_in_rcu(fs_info,
1328 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1329 zone.start << SECTOR_SHIFT,
1330 rcu_str_deref(device->name), device->devid);
1335 caps[i] = (zone.capacity << SECTOR_SHIFT);
1337 switch (zone.cond) {
1338 case BLK_ZONE_COND_OFFLINE:
1339 case BLK_ZONE_COND_READONLY:
1341 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1342 physical >> device->zone_info->zone_size_shift,
1343 rcu_str_deref(device->name), device->devid);
1344 alloc_offsets[i] = WP_MISSING_DEV;
1346 case BLK_ZONE_COND_EMPTY:
1347 alloc_offsets[i] = 0;
1349 case BLK_ZONE_COND_FULL:
1350 alloc_offsets[i] = caps[i];
1353 /* Partially used zone */
1355 ((zone.wp - zone.start) << SECTOR_SHIFT);
1356 __set_bit(i, active);
1361 * Consider a zone as active if we can allow any number of
1364 if (!device->zone_info->max_active_zones)
1365 __set_bit(i, active);
1368 if (num_sequential > 0)
1369 cache->seq_zone = true;
1371 if (num_conventional > 0) {
1373 * Avoid calling calculate_alloc_pointer() for new BG. It
1374 * is no use for new BG. It must be always 0.
1376 * Also, we have a lock chain of extent buffer lock ->
1377 * chunk mutex. For new BG, this function is called from
1378 * btrfs_make_block_group() which is already taking the
1379 * chunk mutex. Thus, we cannot call
1380 * calculate_alloc_pointer() which takes extent buffer
1381 * locks to avoid deadlock.
1384 /* Zone capacity is always zone size in emulation */
1385 cache->zone_capacity = cache->length;
1387 cache->alloc_offset = 0;
1390 ret = calculate_alloc_pointer(cache, &last_alloc);
1391 if (ret || map->num_stripes == num_conventional) {
1393 cache->alloc_offset = last_alloc;
1396 "zoned: failed to determine allocation offset of bg %llu",
1402 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1403 case 0: /* single */
1404 if (alloc_offsets[0] == WP_MISSING_DEV) {
1406 "zoned: cannot recover write pointer for zone %llu",
1411 cache->alloc_offset = alloc_offsets[0];
1412 cache->zone_capacity = caps[0];
1413 cache->zone_is_active = test_bit(0, active);
1415 case BTRFS_BLOCK_GROUP_DUP:
1416 case BTRFS_BLOCK_GROUP_RAID1:
1417 case BTRFS_BLOCK_GROUP_RAID0:
1418 case BTRFS_BLOCK_GROUP_RAID10:
1419 case BTRFS_BLOCK_GROUP_RAID5:
1420 case BTRFS_BLOCK_GROUP_RAID6:
1421 /* non-single profiles are not supported yet */
1423 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1424 btrfs_bg_type_to_raid_name(map->type));
1429 if (cache->zone_is_active) {
1430 btrfs_get_block_group(cache);
1431 spin_lock(&fs_info->zone_active_bgs_lock);
1432 list_add_tail(&cache->active_bg_list, &fs_info->zone_active_bgs);
1433 spin_unlock(&fs_info->zone_active_bgs_lock);
1437 if (cache->alloc_offset > fs_info->zone_size) {
1439 "zoned: invalid write pointer %llu in block group %llu",
1440 cache->alloc_offset, cache->start);
1444 if (cache->alloc_offset > cache->zone_capacity) {
1446 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1447 cache->alloc_offset, cache->zone_capacity,
1452 /* An extent is allocated after the write pointer */
1453 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1455 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1456 logical, last_alloc, cache->alloc_offset);
1461 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1464 kfree(cache->physical_map);
1465 cache->physical_map = NULL;
1467 bitmap_free(active);
1469 kfree(alloc_offsets);
1470 free_extent_map(em);
1475 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1479 if (!btrfs_is_zoned(cache->fs_info))
1482 WARN_ON(cache->bytes_super != 0);
1483 unusable = (cache->alloc_offset - cache->used) +
1484 (cache->length - cache->zone_capacity);
1485 free = cache->zone_capacity - cache->alloc_offset;
1487 /* We only need ->free_space in ALLOC_SEQ block groups */
1488 cache->last_byte_to_unpin = (u64)-1;
1489 cache->cached = BTRFS_CACHE_FINISHED;
1490 cache->free_space_ctl->free_space = free;
1491 cache->zone_unusable = unusable;
1494 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1495 struct extent_buffer *eb)
1497 struct btrfs_fs_info *fs_info = eb->fs_info;
1499 if (!btrfs_is_zoned(fs_info) ||
1500 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1501 !list_empty(&eb->release_list))
1504 set_extent_buffer_dirty(eb);
1505 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1506 eb->start + eb->len - 1, EXTENT_DIRTY);
1507 memzero_extent_buffer(eb, 0, eb->len);
1508 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1510 spin_lock(&trans->releasing_ebs_lock);
1511 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1512 spin_unlock(&trans->releasing_ebs_lock);
1513 atomic_inc(&eb->refs);
1516 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1518 spin_lock(&trans->releasing_ebs_lock);
1519 while (!list_empty(&trans->releasing_ebs)) {
1520 struct extent_buffer *eb;
1522 eb = list_first_entry(&trans->releasing_ebs,
1523 struct extent_buffer, release_list);
1524 list_del_init(&eb->release_list);
1525 free_extent_buffer(eb);
1527 spin_unlock(&trans->releasing_ebs_lock);
1530 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1532 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1533 struct btrfs_block_group *cache;
1536 if (!btrfs_is_zoned(fs_info))
1539 if (!is_data_inode(&inode->vfs_inode))
1543 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1544 * extent layout the relocation code has.
1545 * Furthermore we have set aside own block-group from which only the
1546 * relocation "process" can allocate and make sure only one process at a
1547 * time can add pages to an extent that gets relocated, so it's safe to
1548 * use regular REQ_OP_WRITE for this special case.
1550 if (btrfs_is_data_reloc_root(inode->root))
1553 cache = btrfs_lookup_block_group(fs_info, start);
1558 ret = cache->seq_zone;
1559 btrfs_put_block_group(cache);
1564 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1567 struct btrfs_ordered_extent *ordered;
1568 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1570 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1573 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1574 if (WARN_ON(!ordered))
1577 ordered->physical = physical;
1578 ordered->bdev = bio->bi_bdev;
1580 btrfs_put_ordered_extent(ordered);
1583 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1585 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1586 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1587 struct extent_map_tree *em_tree;
1588 struct extent_map *em;
1589 struct btrfs_ordered_sum *sum;
1590 u64 orig_logical = ordered->disk_bytenr;
1591 u64 *logical = NULL;
1594 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1595 ASSERT(!bdev_is_partition(ordered->bdev));
1596 if (WARN_ON(!ordered->bdev))
1599 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev,
1600 ordered->physical, &logical, &nr,
1606 if (orig_logical == *logical)
1609 ordered->disk_bytenr = *logical;
1611 em_tree = &inode->extent_tree;
1612 write_lock(&em_tree->lock);
1613 em = search_extent_mapping(em_tree, ordered->file_offset,
1614 ordered->num_bytes);
1615 em->block_start = *logical;
1616 free_extent_map(em);
1617 write_unlock(&em_tree->lock);
1619 list_for_each_entry(sum, &ordered->list, list) {
1620 if (*logical < orig_logical)
1621 sum->bytenr -= orig_logical - *logical;
1623 sum->bytenr += *logical - orig_logical;
1630 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1631 struct extent_buffer *eb,
1632 struct btrfs_block_group **cache_ret)
1634 struct btrfs_block_group *cache;
1637 if (!btrfs_is_zoned(fs_info))
1640 cache = btrfs_lookup_block_group(fs_info, eb->start);
1644 if (cache->meta_write_pointer != eb->start) {
1645 btrfs_put_block_group(cache);
1649 cache->meta_write_pointer = eb->start + eb->len;
1657 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1658 struct extent_buffer *eb)
1660 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1663 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1664 cache->meta_write_pointer = eb->start;
1667 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1669 if (!btrfs_dev_is_sequential(device, physical))
1672 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1673 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1676 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1677 struct blk_zone *zone)
1679 struct btrfs_io_context *bioc = NULL;
1680 u64 mapped_length = PAGE_SIZE;
1681 unsigned int nofs_flag;
1685 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1686 &mapped_length, &bioc);
1687 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1688 btrfs_put_bioc(bioc);
1692 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK)
1695 nofs_flag = memalloc_nofs_save();
1696 nmirrors = (int)bioc->num_stripes;
1697 for (i = 0; i < nmirrors; i++) {
1698 u64 physical = bioc->stripes[i].physical;
1699 struct btrfs_device *dev = bioc->stripes[i].dev;
1701 /* Missing device */
1705 ret = btrfs_get_dev_zone(dev, physical, zone);
1706 /* Failing device */
1707 if (ret == -EIO || ret == -EOPNOTSUPP)
1711 memalloc_nofs_restore(nofs_flag);
1717 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1718 * filling zeros between @physical_pos to a write pointer of dev-replace
1721 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1722 u64 physical_start, u64 physical_pos)
1724 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1725 struct blk_zone zone;
1730 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1733 ret = read_zone_info(fs_info, logical, &zone);
1737 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1739 if (physical_pos == wp)
1742 if (physical_pos > wp)
1745 length = wp - physical_pos;
1746 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1749 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1750 u64 logical, u64 length)
1752 struct btrfs_device *device;
1753 struct extent_map *em;
1754 struct map_lookup *map;
1756 em = btrfs_get_chunk_map(fs_info, logical, length);
1758 return ERR_CAST(em);
1760 map = em->map_lookup;
1761 /* We only support single profile for now */
1762 ASSERT(map->num_stripes == 1);
1763 device = map->stripes[0].dev;
1765 free_extent_map(em);
1771 * Activate block group and underlying device zones
1773 * @block_group: the block group to activate
1775 * Return: true on success, false otherwise
1777 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1779 struct btrfs_fs_info *fs_info = block_group->fs_info;
1780 struct map_lookup *map;
1781 struct btrfs_device *device;
1785 if (!btrfs_is_zoned(block_group->fs_info))
1788 map = block_group->physical_map;
1789 /* Currently support SINGLE profile only */
1790 ASSERT(map->num_stripes == 1);
1791 device = map->stripes[0].dev;
1792 physical = map->stripes[0].physical;
1794 if (device->zone_info->max_active_zones == 0)
1797 spin_lock(&block_group->lock);
1799 if (block_group->zone_is_active) {
1805 if (block_group->alloc_offset == block_group->zone_capacity) {
1810 if (!btrfs_dev_set_active_zone(device, physical)) {
1811 /* Cannot activate the zone */
1816 /* Successfully activated all the zones */
1817 block_group->zone_is_active = 1;
1819 spin_unlock(&block_group->lock);
1821 /* For the active block group list */
1822 btrfs_get_block_group(block_group);
1824 spin_lock(&fs_info->zone_active_bgs_lock);
1825 ASSERT(list_empty(&block_group->active_bg_list));
1826 list_add_tail(&block_group->active_bg_list, &fs_info->zone_active_bgs);
1827 spin_unlock(&fs_info->zone_active_bgs_lock);
1832 spin_unlock(&block_group->lock);
1836 int btrfs_zone_finish(struct btrfs_block_group *block_group)
1838 struct btrfs_fs_info *fs_info = block_group->fs_info;
1839 struct map_lookup *map;
1840 struct btrfs_device *device;
1844 if (!btrfs_is_zoned(fs_info))
1847 map = block_group->physical_map;
1848 /* Currently support SINGLE profile only */
1849 ASSERT(map->num_stripes == 1);
1851 device = map->stripes[0].dev;
1852 physical = map->stripes[0].physical;
1854 if (device->zone_info->max_active_zones == 0)
1857 spin_lock(&block_group->lock);
1858 if (!block_group->zone_is_active) {
1859 spin_unlock(&block_group->lock);
1863 /* Check if we have unwritten allocated space */
1864 if ((block_group->flags &
1865 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)) &&
1866 block_group->alloc_offset > block_group->meta_write_pointer) {
1867 spin_unlock(&block_group->lock);
1870 spin_unlock(&block_group->lock);
1872 ret = btrfs_inc_block_group_ro(block_group, false);
1876 /* Ensure all writes in this block group finish */
1877 btrfs_wait_block_group_reservations(block_group);
1878 /* No need to wait for NOCOW writers. Zoned mode does not allow that. */
1879 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
1880 block_group->length);
1882 spin_lock(&block_group->lock);
1885 * Bail out if someone already deactivated the block group, or
1886 * allocated space is left in the block group.
1888 if (!block_group->zone_is_active) {
1889 spin_unlock(&block_group->lock);
1890 btrfs_dec_block_group_ro(block_group);
1894 if (block_group->reserved) {
1895 spin_unlock(&block_group->lock);
1896 btrfs_dec_block_group_ro(block_group);
1900 block_group->zone_is_active = 0;
1901 block_group->alloc_offset = block_group->zone_capacity;
1902 block_group->free_space_ctl->free_space = 0;
1903 btrfs_clear_treelog_bg(block_group);
1904 btrfs_clear_data_reloc_bg(block_group);
1905 spin_unlock(&block_group->lock);
1907 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
1908 physical >> SECTOR_SHIFT,
1909 device->zone_info->zone_size >> SECTOR_SHIFT,
1911 btrfs_dec_block_group_ro(block_group);
1914 btrfs_dev_clear_active_zone(device, physical);
1916 spin_lock(&fs_info->zone_active_bgs_lock);
1917 ASSERT(!list_empty(&block_group->active_bg_list));
1918 list_del_init(&block_group->active_bg_list);
1919 spin_unlock(&fs_info->zone_active_bgs_lock);
1921 /* For active_bg_list */
1922 btrfs_put_block_group(block_group);
1928 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
1930 struct btrfs_device *device;
1933 if (!btrfs_is_zoned(fs_devices->fs_info))
1936 /* Non-single profiles are not supported yet */
1937 ASSERT((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0);
1939 /* Check if there is a device with active zones left */
1940 mutex_lock(&fs_devices->device_list_mutex);
1941 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1942 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1947 if (!zinfo->max_active_zones ||
1948 atomic_read(&zinfo->active_zones_left)) {
1953 mutex_unlock(&fs_devices->device_list_mutex);
1958 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
1960 struct btrfs_block_group *block_group;
1961 struct map_lookup *map;
1962 struct btrfs_device *device;
1965 if (!btrfs_is_zoned(fs_info))
1968 block_group = btrfs_lookup_block_group(fs_info, logical);
1969 ASSERT(block_group);
1971 if (logical + length < block_group->start + block_group->zone_capacity)
1974 spin_lock(&block_group->lock);
1976 if (!block_group->zone_is_active) {
1977 spin_unlock(&block_group->lock);
1981 block_group->zone_is_active = 0;
1982 /* We should have consumed all the free space */
1983 ASSERT(block_group->alloc_offset == block_group->zone_capacity);
1984 ASSERT(block_group->free_space_ctl->free_space == 0);
1985 btrfs_clear_treelog_bg(block_group);
1986 btrfs_clear_data_reloc_bg(block_group);
1987 spin_unlock(&block_group->lock);
1989 map = block_group->physical_map;
1990 device = map->stripes[0].dev;
1991 physical = map->stripes[0].physical;
1993 if (!device->zone_info->max_active_zones)
1996 btrfs_dev_clear_active_zone(device, physical);
1998 spin_lock(&fs_info->zone_active_bgs_lock);
1999 ASSERT(!list_empty(&block_group->active_bg_list));
2000 list_del_init(&block_group->active_bg_list);
2001 spin_unlock(&fs_info->zone_active_bgs_lock);
2003 btrfs_put_block_group(block_group);
2006 btrfs_put_block_group(block_group);
2009 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2011 struct btrfs_fs_info *fs_info = bg->fs_info;
2013 spin_lock(&fs_info->relocation_bg_lock);
2014 if (fs_info->data_reloc_bg == bg->start)
2015 fs_info->data_reloc_bg = 0;
2016 spin_unlock(&fs_info->relocation_bg_lock);
2019 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2021 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2022 struct btrfs_device *device;
2024 if (!btrfs_is_zoned(fs_info))
2027 mutex_lock(&fs_devices->device_list_mutex);
2028 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2029 if (device->zone_info) {
2030 vfree(device->zone_info->zone_cache);
2031 device->zone_info->zone_cache = NULL;
2034 mutex_unlock(&fs_devices->device_list_mutex);