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;
657 zone_info = device->zone_info;
660 zone_size = zone_info->zone_size;
661 } else if (zone_info->zone_size != zone_size) {
663 "zoned: unequal block device zone sizes: have %llu found %llu",
664 device->zone_info->zone_size,
673 if (!zoned_devices && !incompat_zoned)
676 if (!zoned_devices && incompat_zoned) {
677 /* No zoned block device found on ZONED filesystem */
679 "zoned: no zoned devices found on a zoned filesystem");
684 if (zoned_devices && !incompat_zoned) {
686 "zoned: mode not enabled but zoned device found");
691 if (zoned_devices != nr_devices) {
693 "zoned: cannot mix zoned and regular devices");
699 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
700 * btrfs_create_chunk(). Since we want stripe_len == zone_size,
701 * check the alignment here.
703 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
705 "zoned: zone size %llu not aligned to stripe %u",
706 zone_size, BTRFS_STRIPE_LEN);
711 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
712 btrfs_err(fs_info, "zoned: mixed block groups not supported");
717 fs_info->zone_size = zone_size;
718 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
721 * Check mount options here, because we might change fs_info->zoned
722 * from fs_info->zone_size.
724 ret = btrfs_check_mountopts_zoned(fs_info);
728 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
733 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
735 if (!btrfs_is_zoned(info))
739 * Space cache writing is not COWed. Disable that to avoid write errors
740 * in sequential zones.
742 if (btrfs_test_opt(info, SPACE_CACHE)) {
743 btrfs_err(info, "zoned: space cache v1 is not supported");
747 if (btrfs_test_opt(info, NODATACOW)) {
748 btrfs_err(info, "zoned: NODATACOW not supported");
755 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
756 int rw, u64 *bytenr_ret)
761 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
762 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
766 ret = sb_write_pointer(bdev, zones, &wp);
767 if (ret != -ENOENT && ret < 0)
771 struct blk_zone *reset = NULL;
773 if (wp == zones[0].start << SECTOR_SHIFT)
775 else if (wp == zones[1].start << SECTOR_SHIFT)
778 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
779 ASSERT(sb_zone_is_full(reset));
781 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
782 reset->start, reset->len,
787 reset->cond = BLK_ZONE_COND_EMPTY;
788 reset->wp = reset->start;
790 } else if (ret != -ENOENT) {
792 * For READ, we want the previous one. Move write pointer to
793 * the end of a zone, if it is at the head of a zone.
797 if (wp == zones[0].start << SECTOR_SHIFT)
798 zone_end = zones[1].start + zones[1].capacity;
799 else if (wp == zones[1].start << SECTOR_SHIFT)
800 zone_end = zones[0].start + zones[0].capacity;
802 wp = ALIGN_DOWN(zone_end << SECTOR_SHIFT,
803 BTRFS_SUPER_INFO_SIZE);
805 wp -= BTRFS_SUPER_INFO_SIZE;
813 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
816 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
817 sector_t zone_sectors;
820 u8 zone_sectors_shift;
824 if (!bdev_is_zoned(bdev)) {
825 *bytenr_ret = btrfs_sb_offset(mirror);
829 ASSERT(rw == READ || rw == WRITE);
831 zone_sectors = bdev_zone_sectors(bdev);
832 if (!is_power_of_2(zone_sectors))
834 zone_sectors_shift = ilog2(zone_sectors);
835 nr_sectors = bdev_nr_sectors(bdev);
836 nr_zones = nr_sectors >> zone_sectors_shift;
838 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
839 if (sb_zone + 1 >= nr_zones)
842 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
843 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
847 if (ret != BTRFS_NR_SB_LOG_ZONES)
850 return sb_log_location(bdev, zones, rw, bytenr_ret);
853 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
856 struct btrfs_zoned_device_info *zinfo = device->zone_info;
860 * For a zoned filesystem on a non-zoned block device, use the same
861 * super block locations as regular filesystem. Doing so, the super
862 * block can always be retrieved and the zoned flag of the volume
863 * detected from the super block information.
865 if (!bdev_is_zoned(device->bdev)) {
866 *bytenr_ret = btrfs_sb_offset(mirror);
870 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
871 if (zone_num + 1 >= zinfo->nr_zones)
874 return sb_log_location(device->bdev,
875 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
879 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
887 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
888 if (zone_num + 1 >= zinfo->nr_zones)
891 if (!test_bit(zone_num, zinfo->seq_zones))
897 int btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
899 struct btrfs_zoned_device_info *zinfo = device->zone_info;
900 struct blk_zone *zone;
903 if (!is_sb_log_zone(zinfo, mirror))
906 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
907 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
908 /* Advance the next zone */
909 if (zone->cond == BLK_ZONE_COND_FULL) {
914 if (zone->cond == BLK_ZONE_COND_EMPTY)
915 zone->cond = BLK_ZONE_COND_IMP_OPEN;
917 zone->wp += SUPER_INFO_SECTORS;
919 if (sb_zone_is_full(zone)) {
921 * No room left to write new superblock. Since
922 * superblock is written with REQ_SYNC, it is safe to
923 * finish the zone now.
925 * If the write pointer is exactly at the capacity,
926 * explicit ZONE_FINISH is not necessary.
928 if (zone->wp != zone->start + zone->capacity) {
931 ret = blkdev_zone_mgmt(device->bdev,
932 REQ_OP_ZONE_FINISH, zone->start,
933 zone->len, GFP_NOFS);
938 zone->wp = zone->start + zone->len;
939 zone->cond = BLK_ZONE_COND_FULL;
944 /* All the zones are FULL. Should not reach here. */
949 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
951 sector_t zone_sectors;
953 u8 zone_sectors_shift;
957 zone_sectors = bdev_zone_sectors(bdev);
958 zone_sectors_shift = ilog2(zone_sectors);
959 nr_sectors = bdev_nr_sectors(bdev);
960 nr_zones = nr_sectors >> zone_sectors_shift;
962 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
963 if (sb_zone + 1 >= nr_zones)
966 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
967 zone_start_sector(sb_zone, bdev),
968 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
972 * btrfs_find_allocatable_zones - find allocatable zones within a given region
974 * @device: the device to allocate a region on
975 * @hole_start: the position of the hole to allocate the region
976 * @num_bytes: size of wanted region
977 * @hole_end: the end of the hole
978 * @return: position of allocatable zones
980 * Allocatable region should not contain any superblock locations.
982 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
983 u64 hole_end, u64 num_bytes)
985 struct btrfs_zoned_device_info *zinfo = device->zone_info;
986 const u8 shift = zinfo->zone_size_shift;
987 u64 nzones = num_bytes >> shift;
988 u64 pos = hole_start;
993 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
994 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
996 while (pos < hole_end) {
997 begin = pos >> shift;
998 end = begin + nzones;
1000 if (end > zinfo->nr_zones)
1003 /* Check if zones in the region are all empty */
1004 if (btrfs_dev_is_sequential(device, pos) &&
1005 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
1006 pos += zinfo->zone_size;
1011 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1015 sb_zone = sb_zone_number(shift, i);
1016 if (!(end <= sb_zone ||
1017 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
1019 pos = zone_start_physical(
1020 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
1024 /* We also need to exclude regular superblock positions */
1025 sb_pos = btrfs_sb_offset(i);
1026 if (!(pos + num_bytes <= sb_pos ||
1027 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
1029 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
1041 static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
1043 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1044 unsigned int zno = (pos >> zone_info->zone_size_shift);
1046 /* We can use any number of zones */
1047 if (zone_info->max_active_zones == 0)
1050 if (!test_bit(zno, zone_info->active_zones)) {
1051 /* Active zone left? */
1052 if (atomic_dec_if_positive(&zone_info->active_zones_left) < 0)
1054 if (test_and_set_bit(zno, zone_info->active_zones)) {
1055 /* Someone already set the bit */
1056 atomic_inc(&zone_info->active_zones_left);
1063 static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
1065 struct btrfs_zoned_device_info *zone_info = device->zone_info;
1066 unsigned int zno = (pos >> zone_info->zone_size_shift);
1068 /* We can use any number of zones */
1069 if (zone_info->max_active_zones == 0)
1072 if (test_and_clear_bit(zno, zone_info->active_zones))
1073 atomic_inc(&zone_info->active_zones_left);
1076 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
1077 u64 length, u64 *bytes)
1082 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
1083 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
1090 btrfs_dev_set_zone_empty(device, physical);
1091 btrfs_dev_clear_active_zone(device, physical);
1092 physical += device->zone_info->zone_size;
1093 length -= device->zone_info->zone_size;
1099 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1101 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1102 const u8 shift = zinfo->zone_size_shift;
1103 unsigned long begin = start >> shift;
1104 unsigned long end = (start + size) >> shift;
1108 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
1109 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
1111 if (end > zinfo->nr_zones)
1114 /* All the zones are conventional */
1115 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
1118 /* All the zones are sequential and empty */
1119 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
1120 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
1123 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
1126 if (!btrfs_dev_is_sequential(device, pos) ||
1127 btrfs_dev_is_empty_zone(device, pos))
1130 /* Free regions should be empty */
1133 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
1134 rcu_str_deref(device->name), device->devid, pos >> shift);
1137 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
1147 * Calculate an allocation pointer from the extent allocation information
1148 * for a block group consist of conventional zones. It is pointed to the
1149 * end of the highest addressed extent in the block group as an allocation
1152 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1155 struct btrfs_fs_info *fs_info = cache->fs_info;
1156 struct btrfs_root *root;
1157 struct btrfs_path *path;
1158 struct btrfs_key key;
1159 struct btrfs_key found_key;
1163 path = btrfs_alloc_path();
1167 key.objectid = cache->start + cache->length;
1171 root = btrfs_extent_root(fs_info, key.objectid);
1172 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1173 /* We should not find the exact match */
1179 ret = btrfs_previous_extent_item(root, path, cache->start);
1188 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1190 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1191 length = found_key.offset;
1193 length = fs_info->nodesize;
1195 if (!(found_key.objectid >= cache->start &&
1196 found_key.objectid + length <= cache->start + cache->length)) {
1200 *offset_ret = found_key.objectid + length - cache->start;
1204 btrfs_free_path(path);
1208 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1210 struct btrfs_fs_info *fs_info = cache->fs_info;
1211 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1212 struct extent_map *em;
1213 struct map_lookup *map;
1214 struct btrfs_device *device;
1215 u64 logical = cache->start;
1216 u64 length = cache->length;
1219 unsigned int nofs_flag;
1220 u64 *alloc_offsets = NULL;
1222 u64 *physical = NULL;
1223 unsigned long *active = NULL;
1225 u32 num_sequential = 0, num_conventional = 0;
1227 if (!btrfs_is_zoned(fs_info))
1231 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1233 "zoned: block group %llu len %llu unaligned to zone size %llu",
1234 logical, length, fs_info->zone_size);
1238 /* Get the chunk mapping */
1239 read_lock(&em_tree->lock);
1240 em = lookup_extent_mapping(em_tree, logical, length);
1241 read_unlock(&em_tree->lock);
1246 map = em->map_lookup;
1248 cache->physical_map = kmemdup(map, map_lookup_size(map->num_stripes), GFP_NOFS);
1249 if (!cache->physical_map) {
1254 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1255 if (!alloc_offsets) {
1260 caps = kcalloc(map->num_stripes, sizeof(*caps), GFP_NOFS);
1266 physical = kcalloc(map->num_stripes, sizeof(*physical), GFP_NOFS);
1272 active = bitmap_zalloc(map->num_stripes, GFP_NOFS);
1278 for (i = 0; i < map->num_stripes; i++) {
1280 struct blk_zone zone;
1281 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1282 int dev_replace_is_ongoing = 0;
1284 device = map->stripes[i].dev;
1285 physical[i] = map->stripes[i].physical;
1287 if (device->bdev == NULL) {
1288 alloc_offsets[i] = WP_MISSING_DEV;
1292 is_sequential = btrfs_dev_is_sequential(device, physical[i]);
1298 if (!is_sequential) {
1299 alloc_offsets[i] = WP_CONVENTIONAL;
1304 * This zone will be used for allocation, so mark this zone
1307 btrfs_dev_clear_zone_empty(device, physical[i]);
1309 down_read(&dev_replace->rwsem);
1310 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1311 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1312 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical[i]);
1313 up_read(&dev_replace->rwsem);
1316 * The group is mapped to a sequential zone. Get the zone write
1317 * pointer to determine the allocation offset within the zone.
1319 WARN_ON(!IS_ALIGNED(physical[i], fs_info->zone_size));
1320 nofs_flag = memalloc_nofs_save();
1321 ret = btrfs_get_dev_zone(device, physical[i], &zone);
1322 memalloc_nofs_restore(nofs_flag);
1323 if (ret == -EIO || ret == -EOPNOTSUPP) {
1325 alloc_offsets[i] = WP_MISSING_DEV;
1331 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1332 btrfs_err_in_rcu(fs_info,
1333 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1334 zone.start << SECTOR_SHIFT,
1335 rcu_str_deref(device->name), device->devid);
1340 caps[i] = (zone.capacity << SECTOR_SHIFT);
1342 switch (zone.cond) {
1343 case BLK_ZONE_COND_OFFLINE:
1344 case BLK_ZONE_COND_READONLY:
1346 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1347 physical[i] >> device->zone_info->zone_size_shift,
1348 rcu_str_deref(device->name), device->devid);
1349 alloc_offsets[i] = WP_MISSING_DEV;
1351 case BLK_ZONE_COND_EMPTY:
1352 alloc_offsets[i] = 0;
1354 case BLK_ZONE_COND_FULL:
1355 alloc_offsets[i] = caps[i];
1358 /* Partially used zone */
1360 ((zone.wp - zone.start) << SECTOR_SHIFT);
1361 __set_bit(i, active);
1366 * Consider a zone as active if we can allow any number of
1369 if (!device->zone_info->max_active_zones)
1370 __set_bit(i, active);
1373 if (num_sequential > 0)
1374 cache->seq_zone = true;
1376 if (num_conventional > 0) {
1378 * Avoid calling calculate_alloc_pointer() for new BG. It
1379 * is no use for new BG. It must be always 0.
1381 * Also, we have a lock chain of extent buffer lock ->
1382 * chunk mutex. For new BG, this function is called from
1383 * btrfs_make_block_group() which is already taking the
1384 * chunk mutex. Thus, we cannot call
1385 * calculate_alloc_pointer() which takes extent buffer
1386 * locks to avoid deadlock.
1389 /* Zone capacity is always zone size in emulation */
1390 cache->zone_capacity = cache->length;
1392 cache->alloc_offset = 0;
1395 ret = calculate_alloc_pointer(cache, &last_alloc);
1396 if (ret || map->num_stripes == num_conventional) {
1398 cache->alloc_offset = last_alloc;
1401 "zoned: failed to determine allocation offset of bg %llu",
1407 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1408 case 0: /* single */
1409 if (alloc_offsets[0] == WP_MISSING_DEV) {
1411 "zoned: cannot recover write pointer for zone %llu",
1416 cache->alloc_offset = alloc_offsets[0];
1417 cache->zone_capacity = caps[0];
1418 cache->zone_is_active = test_bit(0, active);
1420 case BTRFS_BLOCK_GROUP_DUP:
1421 if (map->type & BTRFS_BLOCK_GROUP_DATA) {
1422 btrfs_err(fs_info, "zoned: profile DUP not yet supported on data bg");
1426 if (alloc_offsets[0] == WP_MISSING_DEV) {
1428 "zoned: cannot recover write pointer for zone %llu",
1433 if (alloc_offsets[1] == WP_MISSING_DEV) {
1435 "zoned: cannot recover write pointer for zone %llu",
1440 if (alloc_offsets[0] != alloc_offsets[1]) {
1442 "zoned: write pointer offset mismatch of zones in DUP profile");
1446 if (test_bit(0, active) != test_bit(1, active)) {
1447 if (!btrfs_zone_activate(cache)) {
1452 cache->zone_is_active = test_bit(0, active);
1454 cache->alloc_offset = alloc_offsets[0];
1455 cache->zone_capacity = min(caps[0], caps[1]);
1457 case BTRFS_BLOCK_GROUP_RAID1:
1458 case BTRFS_BLOCK_GROUP_RAID0:
1459 case BTRFS_BLOCK_GROUP_RAID10:
1460 case BTRFS_BLOCK_GROUP_RAID5:
1461 case BTRFS_BLOCK_GROUP_RAID6:
1462 /* non-single profiles are not supported yet */
1464 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1465 btrfs_bg_type_to_raid_name(map->type));
1470 if (cache->zone_is_active) {
1471 btrfs_get_block_group(cache);
1472 spin_lock(&fs_info->zone_active_bgs_lock);
1473 list_add_tail(&cache->active_bg_list, &fs_info->zone_active_bgs);
1474 spin_unlock(&fs_info->zone_active_bgs_lock);
1478 if (cache->alloc_offset > fs_info->zone_size) {
1480 "zoned: invalid write pointer %llu in block group %llu",
1481 cache->alloc_offset, cache->start);
1485 if (cache->alloc_offset > cache->zone_capacity) {
1487 "zoned: invalid write pointer %llu (larger than zone capacity %llu) in block group %llu",
1488 cache->alloc_offset, cache->zone_capacity,
1493 /* An extent is allocated after the write pointer */
1494 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1496 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1497 logical, last_alloc, cache->alloc_offset);
1502 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1505 kfree(cache->physical_map);
1506 cache->physical_map = NULL;
1508 bitmap_free(active);
1511 kfree(alloc_offsets);
1512 free_extent_map(em);
1517 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1521 if (!btrfs_is_zoned(cache->fs_info))
1524 WARN_ON(cache->bytes_super != 0);
1525 unusable = (cache->alloc_offset - cache->used) +
1526 (cache->length - cache->zone_capacity);
1527 free = cache->zone_capacity - cache->alloc_offset;
1529 /* We only need ->free_space in ALLOC_SEQ block groups */
1530 cache->last_byte_to_unpin = (u64)-1;
1531 cache->cached = BTRFS_CACHE_FINISHED;
1532 cache->free_space_ctl->free_space = free;
1533 cache->zone_unusable = unusable;
1536 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1537 struct extent_buffer *eb)
1539 struct btrfs_fs_info *fs_info = eb->fs_info;
1541 if (!btrfs_is_zoned(fs_info) ||
1542 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1543 !list_empty(&eb->release_list))
1546 set_extent_buffer_dirty(eb);
1547 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1548 eb->start + eb->len - 1, EXTENT_DIRTY);
1549 memzero_extent_buffer(eb, 0, eb->len);
1550 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1552 spin_lock(&trans->releasing_ebs_lock);
1553 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1554 spin_unlock(&trans->releasing_ebs_lock);
1555 atomic_inc(&eb->refs);
1558 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1560 spin_lock(&trans->releasing_ebs_lock);
1561 while (!list_empty(&trans->releasing_ebs)) {
1562 struct extent_buffer *eb;
1564 eb = list_first_entry(&trans->releasing_ebs,
1565 struct extent_buffer, release_list);
1566 list_del_init(&eb->release_list);
1567 free_extent_buffer(eb);
1569 spin_unlock(&trans->releasing_ebs_lock);
1572 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1574 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1575 struct btrfs_block_group *cache;
1578 if (!btrfs_is_zoned(fs_info))
1581 if (!is_data_inode(&inode->vfs_inode))
1585 * Using REQ_OP_ZONE_APPNED for relocation can break assumptions on the
1586 * extent layout the relocation code has.
1587 * Furthermore we have set aside own block-group from which only the
1588 * relocation "process" can allocate and make sure only one process at a
1589 * time can add pages to an extent that gets relocated, so it's safe to
1590 * use regular REQ_OP_WRITE for this special case.
1592 if (btrfs_is_data_reloc_root(inode->root))
1595 cache = btrfs_lookup_block_group(fs_info, start);
1600 ret = cache->seq_zone;
1601 btrfs_put_block_group(cache);
1606 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1609 struct btrfs_ordered_extent *ordered;
1610 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1612 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1615 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1616 if (WARN_ON(!ordered))
1619 ordered->physical = physical;
1620 ordered->bdev = bio->bi_bdev;
1622 btrfs_put_ordered_extent(ordered);
1625 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1627 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1628 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1629 struct extent_map_tree *em_tree;
1630 struct extent_map *em;
1631 struct btrfs_ordered_sum *sum;
1632 u64 orig_logical = ordered->disk_bytenr;
1633 u64 *logical = NULL;
1636 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1637 ASSERT(!bdev_is_partition(ordered->bdev));
1638 if (WARN_ON(!ordered->bdev))
1641 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, ordered->bdev,
1642 ordered->physical, &logical, &nr,
1648 if (orig_logical == *logical)
1651 ordered->disk_bytenr = *logical;
1653 em_tree = &inode->extent_tree;
1654 write_lock(&em_tree->lock);
1655 em = search_extent_mapping(em_tree, ordered->file_offset,
1656 ordered->num_bytes);
1657 em->block_start = *logical;
1658 free_extent_map(em);
1659 write_unlock(&em_tree->lock);
1661 list_for_each_entry(sum, &ordered->list, list) {
1662 if (*logical < orig_logical)
1663 sum->bytenr -= orig_logical - *logical;
1665 sum->bytenr += *logical - orig_logical;
1672 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1673 struct extent_buffer *eb,
1674 struct btrfs_block_group **cache_ret)
1676 struct btrfs_block_group *cache;
1679 if (!btrfs_is_zoned(fs_info))
1682 cache = btrfs_lookup_block_group(fs_info, eb->start);
1686 if (cache->meta_write_pointer != eb->start) {
1687 btrfs_put_block_group(cache);
1691 cache->meta_write_pointer = eb->start + eb->len;
1699 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1700 struct extent_buffer *eb)
1702 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1705 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1706 cache->meta_write_pointer = eb->start;
1709 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1711 if (!btrfs_dev_is_sequential(device, physical))
1714 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1715 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1718 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1719 struct blk_zone *zone)
1721 struct btrfs_io_context *bioc = NULL;
1722 u64 mapped_length = PAGE_SIZE;
1723 unsigned int nofs_flag;
1727 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1728 &mapped_length, &bioc);
1729 if (ret || !bioc || mapped_length < PAGE_SIZE) {
1730 btrfs_put_bioc(bioc);
1734 if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK)
1737 nofs_flag = memalloc_nofs_save();
1738 nmirrors = (int)bioc->num_stripes;
1739 for (i = 0; i < nmirrors; i++) {
1740 u64 physical = bioc->stripes[i].physical;
1741 struct btrfs_device *dev = bioc->stripes[i].dev;
1743 /* Missing device */
1747 ret = btrfs_get_dev_zone(dev, physical, zone);
1748 /* Failing device */
1749 if (ret == -EIO || ret == -EOPNOTSUPP)
1753 memalloc_nofs_restore(nofs_flag);
1759 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1760 * filling zeros between @physical_pos to a write pointer of dev-replace
1763 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1764 u64 physical_start, u64 physical_pos)
1766 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1767 struct blk_zone zone;
1772 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1775 ret = read_zone_info(fs_info, logical, &zone);
1779 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1781 if (physical_pos == wp)
1784 if (physical_pos > wp)
1787 length = wp - physical_pos;
1788 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1791 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1792 u64 logical, u64 length)
1794 struct btrfs_device *device;
1795 struct extent_map *em;
1796 struct map_lookup *map;
1798 em = btrfs_get_chunk_map(fs_info, logical, length);
1800 return ERR_CAST(em);
1802 map = em->map_lookup;
1803 /* We only support single profile for now */
1804 device = map->stripes[0].dev;
1806 free_extent_map(em);
1812 * Activate block group and underlying device zones
1814 * @block_group: the block group to activate
1816 * Return: true on success, false otherwise
1818 bool btrfs_zone_activate(struct btrfs_block_group *block_group)
1820 struct btrfs_fs_info *fs_info = block_group->fs_info;
1821 struct map_lookup *map;
1822 struct btrfs_device *device;
1827 if (!btrfs_is_zoned(block_group->fs_info))
1830 map = block_group->physical_map;
1832 spin_lock(&block_group->lock);
1833 if (block_group->zone_is_active) {
1838 for (i = 0; i < map->num_stripes; i++) {
1839 device = map->stripes[i].dev;
1840 physical = map->stripes[i].physical;
1842 if (device->zone_info->max_active_zones == 0)
1846 if (block_group->alloc_offset == block_group->zone_capacity) {
1851 if (!btrfs_dev_set_active_zone(device, physical)) {
1852 /* Cannot activate the zone */
1857 /* Successfully activated all the zones */
1858 if (i == map->num_stripes - 1)
1859 block_group->zone_is_active = 1;
1863 spin_unlock(&block_group->lock);
1865 if (block_group->zone_is_active) {
1866 /* For the active block group list */
1867 btrfs_get_block_group(block_group);
1869 spin_lock(&fs_info->zone_active_bgs_lock);
1870 list_add_tail(&block_group->active_bg_list,
1871 &fs_info->zone_active_bgs);
1872 spin_unlock(&fs_info->zone_active_bgs_lock);
1878 spin_unlock(&block_group->lock);
1882 int btrfs_zone_finish(struct btrfs_block_group *block_group)
1884 struct btrfs_fs_info *fs_info = block_group->fs_info;
1885 struct map_lookup *map;
1886 struct btrfs_device *device;
1891 if (!btrfs_is_zoned(fs_info))
1894 map = block_group->physical_map;
1896 spin_lock(&block_group->lock);
1897 if (!block_group->zone_is_active) {
1898 spin_unlock(&block_group->lock);
1902 /* Check if we have unwritten allocated space */
1903 if ((block_group->flags &
1904 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)) &&
1905 block_group->alloc_offset > block_group->meta_write_pointer) {
1906 spin_unlock(&block_group->lock);
1909 spin_unlock(&block_group->lock);
1911 ret = btrfs_inc_block_group_ro(block_group, false);
1915 /* Ensure all writes in this block group finish */
1916 btrfs_wait_block_group_reservations(block_group);
1917 /* No need to wait for NOCOW writers. Zoned mode does not allow that. */
1918 btrfs_wait_ordered_roots(fs_info, U64_MAX, block_group->start,
1919 block_group->length);
1921 spin_lock(&block_group->lock);
1924 * Bail out if someone already deactivated the block group, or
1925 * allocated space is left in the block group.
1927 if (!block_group->zone_is_active) {
1928 spin_unlock(&block_group->lock);
1929 btrfs_dec_block_group_ro(block_group);
1933 if (block_group->reserved) {
1934 spin_unlock(&block_group->lock);
1935 btrfs_dec_block_group_ro(block_group);
1939 block_group->zone_is_active = 0;
1940 block_group->alloc_offset = block_group->zone_capacity;
1941 block_group->free_space_ctl->free_space = 0;
1942 btrfs_clear_treelog_bg(block_group);
1943 btrfs_clear_data_reloc_bg(block_group);
1944 spin_unlock(&block_group->lock);
1946 for (i = 0; i < map->num_stripes; i++) {
1947 device = map->stripes[i].dev;
1948 physical = map->stripes[i].physical;
1950 if (device->zone_info->max_active_zones == 0)
1953 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_FINISH,
1954 physical >> SECTOR_SHIFT,
1955 device->zone_info->zone_size >> SECTOR_SHIFT,
1961 btrfs_dev_clear_active_zone(device, physical);
1963 btrfs_dec_block_group_ro(block_group);
1965 spin_lock(&fs_info->zone_active_bgs_lock);
1966 ASSERT(!list_empty(&block_group->active_bg_list));
1967 list_del_init(&block_group->active_bg_list);
1968 spin_unlock(&fs_info->zone_active_bgs_lock);
1970 /* For active_bg_list */
1971 btrfs_put_block_group(block_group);
1976 bool btrfs_can_activate_zone(struct btrfs_fs_devices *fs_devices, u64 flags)
1978 struct btrfs_fs_info *fs_info = fs_devices->fs_info;
1979 struct btrfs_device *device;
1982 if (!btrfs_is_zoned(fs_info))
1985 /* Check if there is a device with active zones left */
1986 mutex_lock(&fs_info->chunk_mutex);
1987 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
1988 struct btrfs_zoned_device_info *zinfo = device->zone_info;
1993 if (!zinfo->max_active_zones ||
1994 atomic_read(&zinfo->active_zones_left)) {
1999 mutex_unlock(&fs_info->chunk_mutex);
2004 void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length)
2006 struct btrfs_block_group *block_group;
2007 struct map_lookup *map;
2008 struct btrfs_device *device;
2011 if (!btrfs_is_zoned(fs_info))
2014 block_group = btrfs_lookup_block_group(fs_info, logical);
2015 ASSERT(block_group);
2017 if (logical + length < block_group->start + block_group->zone_capacity)
2020 spin_lock(&block_group->lock);
2022 if (!block_group->zone_is_active) {
2023 spin_unlock(&block_group->lock);
2027 block_group->zone_is_active = 0;
2028 /* We should have consumed all the free space */
2029 ASSERT(block_group->alloc_offset == block_group->zone_capacity);
2030 ASSERT(block_group->free_space_ctl->free_space == 0);
2031 btrfs_clear_treelog_bg(block_group);
2032 btrfs_clear_data_reloc_bg(block_group);
2033 spin_unlock(&block_group->lock);
2035 map = block_group->physical_map;
2036 device = map->stripes[0].dev;
2037 physical = map->stripes[0].physical;
2039 if (!device->zone_info->max_active_zones)
2042 btrfs_dev_clear_active_zone(device, physical);
2044 spin_lock(&fs_info->zone_active_bgs_lock);
2045 ASSERT(!list_empty(&block_group->active_bg_list));
2046 list_del_init(&block_group->active_bg_list);
2047 spin_unlock(&fs_info->zone_active_bgs_lock);
2049 btrfs_put_block_group(block_group);
2052 btrfs_put_block_group(block_group);
2055 void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
2057 struct btrfs_fs_info *fs_info = bg->fs_info;
2059 spin_lock(&fs_info->relocation_bg_lock);
2060 if (fs_info->data_reloc_bg == bg->start)
2061 fs_info->data_reloc_bg = 0;
2062 spin_unlock(&fs_info->relocation_bg_lock);
2065 void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info)
2067 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2068 struct btrfs_device *device;
2070 if (!btrfs_is_zoned(fs_info))
2073 mutex_lock(&fs_devices->device_list_mutex);
2074 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2075 if (device->zone_info) {
2076 vfree(device->zone_info->zone_cache);
2077 device->zone_info->zone_cache = NULL;
2080 mutex_unlock(&fs_devices->device_list_mutex);