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>
10 #include "rcu-string.h"
12 #include "block-group.h"
13 #include "transaction.h"
14 #include "dev-replace.h"
15 #include "space-info.h"
17 /* Maximum number of zones to report per blkdev_report_zones() call */
18 #define BTRFS_REPORT_NR_ZONES 4096
19 /* Invalid allocation pointer value for missing devices */
20 #define WP_MISSING_DEV ((u64)-1)
21 /* Pseudo write pointer value for conventional zone */
22 #define WP_CONVENTIONAL ((u64)-2)
25 * Location of the first zone of superblock logging zone pairs.
27 * - primary superblock: 0B (zone 0)
28 * - first copy: 512G (zone starting at that offset)
29 * - second copy: 4T (zone starting at that offset)
31 #define BTRFS_SB_LOG_PRIMARY_OFFSET (0ULL)
32 #define BTRFS_SB_LOG_FIRST_OFFSET (512ULL * SZ_1G)
33 #define BTRFS_SB_LOG_SECOND_OFFSET (4096ULL * SZ_1G)
35 #define BTRFS_SB_LOG_FIRST_SHIFT const_ilog2(BTRFS_SB_LOG_FIRST_OFFSET)
36 #define BTRFS_SB_LOG_SECOND_SHIFT const_ilog2(BTRFS_SB_LOG_SECOND_OFFSET)
38 /* Number of superblock log zones */
39 #define BTRFS_NR_SB_LOG_ZONES 2
42 * Maximum supported zone size. Currently, SMR disks have a zone size of
43 * 256MiB, and we are expecting ZNS drives to be in the 1-4GiB range. We do not
44 * expect the zone size to become larger than 8GiB in the near future.
46 #define BTRFS_MAX_ZONE_SIZE SZ_8G
48 static int copy_zone_info_cb(struct blk_zone *zone, unsigned int idx, void *data)
50 struct blk_zone *zones = data;
52 memcpy(&zones[idx], zone, sizeof(*zone));
57 static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
60 bool empty[BTRFS_NR_SB_LOG_ZONES];
61 bool full[BTRFS_NR_SB_LOG_ZONES];
64 ASSERT(zones[0].type != BLK_ZONE_TYPE_CONVENTIONAL &&
65 zones[1].type != BLK_ZONE_TYPE_CONVENTIONAL);
67 empty[0] = (zones[0].cond == BLK_ZONE_COND_EMPTY);
68 empty[1] = (zones[1].cond == BLK_ZONE_COND_EMPTY);
69 full[0] = (zones[0].cond == BLK_ZONE_COND_FULL);
70 full[1] = (zones[1].cond == BLK_ZONE_COND_FULL);
73 * Possible states of log buffer zones
75 * Empty[0] In use[0] Full[0]
81 * *: Special case, no superblock is written
82 * 0: Use write pointer of zones[0]
83 * 1: Use write pointer of zones[1]
84 * C: Compare super blcoks from zones[0] and zones[1], use the latest
85 * one determined by generation
89 if (empty[0] && empty[1]) {
90 /* Special case to distinguish no superblock to read */
91 *wp_ret = zones[0].start << SECTOR_SHIFT;
93 } else if (full[0] && full[1]) {
94 /* Compare two super blocks */
95 struct address_space *mapping = bdev->bd_inode->i_mapping;
96 struct page *page[BTRFS_NR_SB_LOG_ZONES];
97 struct btrfs_super_block *super[BTRFS_NR_SB_LOG_ZONES];
100 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++) {
103 bytenr = ((zones[i].start + zones[i].len)
104 << SECTOR_SHIFT) - BTRFS_SUPER_INFO_SIZE;
106 page[i] = read_cache_page_gfp(mapping,
107 bytenr >> PAGE_SHIFT, GFP_NOFS);
108 if (IS_ERR(page[i])) {
110 btrfs_release_disk_super(super[0]);
111 return PTR_ERR(page[i]);
113 super[i] = page_address(page[i]);
116 if (super[0]->generation > super[1]->generation)
117 sector = zones[1].start;
119 sector = zones[0].start;
121 for (i = 0; i < BTRFS_NR_SB_LOG_ZONES; i++)
122 btrfs_release_disk_super(super[i]);
123 } else if (!full[0] && (empty[1] || full[1])) {
124 sector = zones[0].wp;
125 } else if (full[0]) {
126 sector = zones[1].wp;
130 *wp_ret = sector << SECTOR_SHIFT;
135 * Get the first zone number of the superblock mirror
137 static inline u32 sb_zone_number(int shift, int mirror)
141 ASSERT(mirror < BTRFS_SUPER_MIRROR_MAX);
143 case 0: zone = 0; break;
144 case 1: zone = 1ULL << (BTRFS_SB_LOG_FIRST_SHIFT - shift); break;
145 case 2: zone = 1ULL << (BTRFS_SB_LOG_SECOND_SHIFT - shift); break;
148 ASSERT(zone <= U32_MAX);
153 static inline sector_t zone_start_sector(u32 zone_number,
154 struct block_device *bdev)
156 return (sector_t)zone_number << ilog2(bdev_zone_sectors(bdev));
159 static inline u64 zone_start_physical(u32 zone_number,
160 struct btrfs_zoned_device_info *zone_info)
162 return (u64)zone_number << zone_info->zone_size_shift;
166 * Emulate blkdev_report_zones() for a non-zoned device. It slices up the block
167 * device into static sized chunks and fake a conventional zone on each of
170 static int emulate_report_zones(struct btrfs_device *device, u64 pos,
171 struct blk_zone *zones, unsigned int nr_zones)
173 const sector_t zone_sectors = device->fs_info->zone_size >> SECTOR_SHIFT;
174 sector_t bdev_size = bdev_nr_sectors(device->bdev);
177 pos >>= SECTOR_SHIFT;
178 for (i = 0; i < nr_zones; i++) {
179 zones[i].start = i * zone_sectors + pos;
180 zones[i].len = zone_sectors;
181 zones[i].capacity = zone_sectors;
182 zones[i].wp = zones[i].start + zone_sectors;
183 zones[i].type = BLK_ZONE_TYPE_CONVENTIONAL;
184 zones[i].cond = BLK_ZONE_COND_NOT_WP;
186 if (zones[i].wp >= bdev_size) {
195 static int btrfs_get_dev_zones(struct btrfs_device *device, u64 pos,
196 struct blk_zone *zones, unsigned int *nr_zones)
203 if (!bdev_is_zoned(device->bdev)) {
204 ret = emulate_report_zones(device, pos, zones, *nr_zones);
209 ret = blkdev_report_zones(device->bdev, pos >> SECTOR_SHIFT, *nr_zones,
210 copy_zone_info_cb, zones);
212 btrfs_err_in_rcu(device->fs_info,
213 "zoned: failed to read zone %llu on %s (devid %llu)",
214 pos, rcu_str_deref(device->name),
225 /* The emulated zone size is determined from the size of device extent */
226 static int calculate_emulated_zone_size(struct btrfs_fs_info *fs_info)
228 struct btrfs_path *path;
229 struct btrfs_root *root = fs_info->dev_root;
230 struct btrfs_key key;
231 struct extent_buffer *leaf;
232 struct btrfs_dev_extent *dext;
236 key.type = BTRFS_DEV_EXTENT_KEY;
239 path = btrfs_alloc_path();
243 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
247 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
248 ret = btrfs_next_item(root, path);
251 /* No dev extents at all? Not good */
258 leaf = path->nodes[0];
259 dext = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_extent);
260 fs_info->zone_size = btrfs_dev_extent_length(leaf, dext);
264 btrfs_free_path(path);
269 int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)
271 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
272 struct btrfs_device *device;
275 /* fs_info->zone_size might not set yet. Use the incomapt flag here. */
276 if (!btrfs_fs_incompat(fs_info, ZONED))
279 mutex_lock(&fs_devices->device_list_mutex);
280 list_for_each_entry(device, &fs_devices->devices, dev_list) {
281 /* We can skip reading of zone info for missing devices */
285 ret = btrfs_get_dev_zone_info(device);
289 mutex_unlock(&fs_devices->device_list_mutex);
294 int btrfs_get_dev_zone_info(struct btrfs_device *device)
296 struct btrfs_fs_info *fs_info = device->fs_info;
297 struct btrfs_zoned_device_info *zone_info = NULL;
298 struct block_device *bdev = device->bdev;
299 struct request_queue *queue = bdev_get_queue(bdev);
302 struct blk_zone *zones = NULL;
303 unsigned int i, nreported = 0, nr_zones;
304 sector_t zone_sectors;
305 char *model, *emulated;
309 * Cannot use btrfs_is_zoned here, since fs_info::zone_size might not
312 if (!btrfs_fs_incompat(fs_info, ZONED))
315 if (device->zone_info)
318 zone_info = kzalloc(sizeof(*zone_info), GFP_KERNEL);
322 if (!bdev_is_zoned(bdev)) {
323 if (!fs_info->zone_size) {
324 ret = calculate_emulated_zone_size(fs_info);
329 ASSERT(fs_info->zone_size);
330 zone_sectors = fs_info->zone_size >> SECTOR_SHIFT;
332 zone_sectors = bdev_zone_sectors(bdev);
335 /* Check if it's power of 2 (see is_power_of_2) */
336 ASSERT(zone_sectors != 0 && (zone_sectors & (zone_sectors - 1)) == 0);
337 zone_info->zone_size = zone_sectors << SECTOR_SHIFT;
339 /* We reject devices with a zone size larger than 8GB */
340 if (zone_info->zone_size > BTRFS_MAX_ZONE_SIZE) {
341 btrfs_err_in_rcu(fs_info,
342 "zoned: %s: zone size %llu larger than supported maximum %llu",
343 rcu_str_deref(device->name),
344 zone_info->zone_size, BTRFS_MAX_ZONE_SIZE);
349 nr_sectors = bdev_nr_sectors(bdev);
350 zone_info->zone_size_shift = ilog2(zone_info->zone_size);
351 zone_info->max_zone_append_size =
352 (u64)queue_max_zone_append_sectors(queue) << SECTOR_SHIFT;
353 zone_info->nr_zones = nr_sectors >> ilog2(zone_sectors);
354 if (!IS_ALIGNED(nr_sectors, zone_sectors))
355 zone_info->nr_zones++;
357 if (bdev_is_zoned(bdev) && zone_info->max_zone_append_size == 0) {
358 btrfs_err(fs_info, "zoned: device %pg does not support zone append",
364 zone_info->seq_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
365 if (!zone_info->seq_zones) {
370 zone_info->empty_zones = bitmap_zalloc(zone_info->nr_zones, GFP_KERNEL);
371 if (!zone_info->empty_zones) {
376 zones = kcalloc(BTRFS_REPORT_NR_ZONES, sizeof(struct blk_zone), GFP_KERNEL);
383 while (sector < nr_sectors) {
384 nr_zones = BTRFS_REPORT_NR_ZONES;
385 ret = btrfs_get_dev_zones(device, sector << SECTOR_SHIFT, zones,
390 for (i = 0; i < nr_zones; i++) {
391 if (zones[i].type == BLK_ZONE_TYPE_SEQWRITE_REQ)
392 __set_bit(nreported, zone_info->seq_zones);
393 if (zones[i].cond == BLK_ZONE_COND_EMPTY)
394 __set_bit(nreported, zone_info->empty_zones);
397 sector = zones[nr_zones - 1].start + zones[nr_zones - 1].len;
400 if (nreported != zone_info->nr_zones) {
401 btrfs_err_in_rcu(device->fs_info,
402 "inconsistent number of zones on %s (%u/%u)",
403 rcu_str_deref(device->name), nreported,
404 zone_info->nr_zones);
409 /* Validate superblock log */
410 nr_zones = BTRFS_NR_SB_LOG_ZONES;
411 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
414 int sb_pos = BTRFS_NR_SB_LOG_ZONES * i;
416 sb_zone = sb_zone_number(zone_info->zone_size_shift, i);
417 if (sb_zone + 1 >= zone_info->nr_zones)
420 ret = btrfs_get_dev_zones(device,
421 zone_start_physical(sb_zone, zone_info),
422 &zone_info->sb_zones[sb_pos],
427 if (nr_zones != BTRFS_NR_SB_LOG_ZONES) {
428 btrfs_err_in_rcu(device->fs_info,
429 "zoned: failed to read super block log zone info at devid %llu zone %u",
430 device->devid, sb_zone);
436 * If zones[0] is conventional, always use the beggining of the
437 * zone to record superblock. No need to validate in that case.
439 if (zone_info->sb_zones[BTRFS_NR_SB_LOG_ZONES * i].type ==
440 BLK_ZONE_TYPE_CONVENTIONAL)
443 ret = sb_write_pointer(device->bdev,
444 &zone_info->sb_zones[sb_pos], &sb_wp);
445 if (ret != -ENOENT && ret) {
446 btrfs_err_in_rcu(device->fs_info,
447 "zoned: super block log zone corrupted devid %llu zone %u",
448 device->devid, sb_zone);
457 device->zone_info = zone_info;
459 switch (bdev_zoned_model(bdev)) {
461 model = "host-managed zoned";
465 model = "host-aware zoned";
470 emulated = "emulated ";
474 btrfs_err_in_rcu(fs_info, "zoned: unsupported model %d on %s",
475 bdev_zoned_model(bdev),
476 rcu_str_deref(device->name));
478 goto out_free_zone_info;
481 btrfs_info_in_rcu(fs_info,
482 "%s block device %s, %u %szones of %llu bytes",
483 model, rcu_str_deref(device->name), zone_info->nr_zones,
484 emulated, zone_info->zone_size);
491 bitmap_free(zone_info->empty_zones);
492 bitmap_free(zone_info->seq_zones);
494 device->zone_info = NULL;
499 void btrfs_destroy_dev_zone_info(struct btrfs_device *device)
501 struct btrfs_zoned_device_info *zone_info = device->zone_info;
506 bitmap_free(zone_info->seq_zones);
507 bitmap_free(zone_info->empty_zones);
509 device->zone_info = NULL;
512 int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos,
513 struct blk_zone *zone)
515 unsigned int nr_zones = 1;
518 ret = btrfs_get_dev_zones(device, pos, zone, &nr_zones);
519 if (ret != 0 || !nr_zones)
520 return ret ? ret : -EIO;
525 int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
527 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
528 struct btrfs_device *device;
529 u64 zoned_devices = 0;
532 u64 max_zone_append_size = 0;
533 const bool incompat_zoned = btrfs_fs_incompat(fs_info, ZONED);
536 /* Count zoned devices */
537 list_for_each_entry(device, &fs_devices->devices, dev_list) {
538 enum blk_zoned_model model;
543 model = bdev_zoned_model(device->bdev);
545 * A Host-Managed zoned device must be used as a zoned device.
546 * A Host-Aware zoned device and a non-zoned devices can be
547 * treated as a zoned device, if ZONED flag is enabled in the
550 if (model == BLK_ZONED_HM ||
551 (model == BLK_ZONED_HA && incompat_zoned) ||
552 (model == BLK_ZONED_NONE && incompat_zoned)) {
553 struct btrfs_zoned_device_info *zone_info =
556 zone_info = device->zone_info;
559 zone_size = zone_info->zone_size;
560 } else if (zone_info->zone_size != zone_size) {
562 "zoned: unequal block device zone sizes: have %llu found %llu",
563 device->zone_info->zone_size,
568 if (!max_zone_append_size ||
569 (zone_info->max_zone_append_size &&
570 zone_info->max_zone_append_size < max_zone_append_size))
571 max_zone_append_size =
572 zone_info->max_zone_append_size;
577 if (!zoned_devices && !incompat_zoned)
580 if (!zoned_devices && incompat_zoned) {
581 /* No zoned block device found on ZONED filesystem */
583 "zoned: no zoned devices found on a zoned filesystem");
588 if (zoned_devices && !incompat_zoned) {
590 "zoned: mode not enabled but zoned device found");
595 if (zoned_devices != nr_devices) {
597 "zoned: cannot mix zoned and regular devices");
603 * stripe_size is always aligned to BTRFS_STRIPE_LEN in
604 * __btrfs_alloc_chunk(). Since we want stripe_len == zone_size,
605 * check the alignment here.
607 if (!IS_ALIGNED(zone_size, BTRFS_STRIPE_LEN)) {
609 "zoned: zone size %llu not aligned to stripe %u",
610 zone_size, BTRFS_STRIPE_LEN);
615 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
616 btrfs_err(fs_info, "zoned: mixed block groups not supported");
621 fs_info->zone_size = zone_size;
622 fs_info->max_zone_append_size = max_zone_append_size;
623 fs_info->fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_ZONED;
626 * Check mount options here, because we might change fs_info->zoned
627 * from fs_info->zone_size.
629 ret = btrfs_check_mountopts_zoned(fs_info);
633 btrfs_info(fs_info, "zoned mode enabled with zone size %llu", zone_size);
638 int btrfs_check_mountopts_zoned(struct btrfs_fs_info *info)
640 if (!btrfs_is_zoned(info))
644 * Space cache writing is not COWed. Disable that to avoid write errors
645 * in sequential zones.
647 if (btrfs_test_opt(info, SPACE_CACHE)) {
648 btrfs_err(info, "zoned: space cache v1 is not supported");
652 if (btrfs_test_opt(info, NODATACOW)) {
653 btrfs_err(info, "zoned: NODATACOW not supported");
660 static int sb_log_location(struct block_device *bdev, struct blk_zone *zones,
661 int rw, u64 *bytenr_ret)
666 if (zones[0].type == BLK_ZONE_TYPE_CONVENTIONAL) {
667 *bytenr_ret = zones[0].start << SECTOR_SHIFT;
671 ret = sb_write_pointer(bdev, zones, &wp);
672 if (ret != -ENOENT && ret < 0)
676 struct blk_zone *reset = NULL;
678 if (wp == zones[0].start << SECTOR_SHIFT)
680 else if (wp == zones[1].start << SECTOR_SHIFT)
683 if (reset && reset->cond != BLK_ZONE_COND_EMPTY) {
684 ASSERT(reset->cond == BLK_ZONE_COND_FULL);
686 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
687 reset->start, reset->len,
692 reset->cond = BLK_ZONE_COND_EMPTY;
693 reset->wp = reset->start;
695 } else if (ret != -ENOENT) {
696 /* For READ, we want the precious one */
697 if (wp == zones[0].start << SECTOR_SHIFT)
698 wp = (zones[1].start + zones[1].len) << SECTOR_SHIFT;
699 wp -= BTRFS_SUPER_INFO_SIZE;
707 int btrfs_sb_log_location_bdev(struct block_device *bdev, int mirror, int rw,
710 struct blk_zone zones[BTRFS_NR_SB_LOG_ZONES];
711 sector_t zone_sectors;
714 u8 zone_sectors_shift;
718 if (!bdev_is_zoned(bdev)) {
719 *bytenr_ret = btrfs_sb_offset(mirror);
723 ASSERT(rw == READ || rw == WRITE);
725 zone_sectors = bdev_zone_sectors(bdev);
726 if (!is_power_of_2(zone_sectors))
728 zone_sectors_shift = ilog2(zone_sectors);
729 nr_sectors = bdev_nr_sectors(bdev);
730 nr_zones = nr_sectors >> zone_sectors_shift;
732 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
733 if (sb_zone + 1 >= nr_zones)
736 ret = blkdev_report_zones(bdev, zone_start_sector(sb_zone, bdev),
737 BTRFS_NR_SB_LOG_ZONES, copy_zone_info_cb,
741 if (ret != BTRFS_NR_SB_LOG_ZONES)
744 return sb_log_location(bdev, zones, rw, bytenr_ret);
747 int btrfs_sb_log_location(struct btrfs_device *device, int mirror, int rw,
750 struct btrfs_zoned_device_info *zinfo = device->zone_info;
754 * For a zoned filesystem on a non-zoned block device, use the same
755 * super block locations as regular filesystem. Doing so, the super
756 * block can always be retrieved and the zoned flag of the volume
757 * detected from the super block information.
759 if (!bdev_is_zoned(device->bdev)) {
760 *bytenr_ret = btrfs_sb_offset(mirror);
764 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
765 if (zone_num + 1 >= zinfo->nr_zones)
768 return sb_log_location(device->bdev,
769 &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror],
773 static inline bool is_sb_log_zone(struct btrfs_zoned_device_info *zinfo,
781 zone_num = sb_zone_number(zinfo->zone_size_shift, mirror);
782 if (zone_num + 1 >= zinfo->nr_zones)
785 if (!test_bit(zone_num, zinfo->seq_zones))
791 void btrfs_advance_sb_log(struct btrfs_device *device, int mirror)
793 struct btrfs_zoned_device_info *zinfo = device->zone_info;
794 struct blk_zone *zone;
796 if (!is_sb_log_zone(zinfo, mirror))
799 zone = &zinfo->sb_zones[BTRFS_NR_SB_LOG_ZONES * mirror];
800 if (zone->cond != BLK_ZONE_COND_FULL) {
801 if (zone->cond == BLK_ZONE_COND_EMPTY)
802 zone->cond = BLK_ZONE_COND_IMP_OPEN;
804 zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
806 if (zone->wp == zone->start + zone->len)
807 zone->cond = BLK_ZONE_COND_FULL;
813 ASSERT(zone->cond != BLK_ZONE_COND_FULL);
814 if (zone->cond == BLK_ZONE_COND_EMPTY)
815 zone->cond = BLK_ZONE_COND_IMP_OPEN;
817 zone->wp += (BTRFS_SUPER_INFO_SIZE >> SECTOR_SHIFT);
819 if (zone->wp == zone->start + zone->len)
820 zone->cond = BLK_ZONE_COND_FULL;
823 int btrfs_reset_sb_log_zones(struct block_device *bdev, int mirror)
825 sector_t zone_sectors;
827 u8 zone_sectors_shift;
831 zone_sectors = bdev_zone_sectors(bdev);
832 zone_sectors_shift = ilog2(zone_sectors);
833 nr_sectors = bdev_nr_sectors(bdev);
834 nr_zones = nr_sectors >> zone_sectors_shift;
836 sb_zone = sb_zone_number(zone_sectors_shift + SECTOR_SHIFT, mirror);
837 if (sb_zone + 1 >= nr_zones)
840 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
841 zone_start_sector(sb_zone, bdev),
842 zone_sectors * BTRFS_NR_SB_LOG_ZONES, GFP_NOFS);
846 * btrfs_find_allocatable_zones - find allocatable zones within a given region
848 * @device: the device to allocate a region on
849 * @hole_start: the position of the hole to allocate the region
850 * @num_bytes: size of wanted region
851 * @hole_end: the end of the hole
852 * @return: position of allocatable zones
854 * Allocatable region should not contain any superblock locations.
856 u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
857 u64 hole_end, u64 num_bytes)
859 struct btrfs_zoned_device_info *zinfo = device->zone_info;
860 const u8 shift = zinfo->zone_size_shift;
861 u64 nzones = num_bytes >> shift;
862 u64 pos = hole_start;
867 ASSERT(IS_ALIGNED(hole_start, zinfo->zone_size));
868 ASSERT(IS_ALIGNED(num_bytes, zinfo->zone_size));
870 while (pos < hole_end) {
871 begin = pos >> shift;
872 end = begin + nzones;
874 if (end > zinfo->nr_zones)
877 /* Check if zones in the region are all empty */
878 if (btrfs_dev_is_sequential(device, pos) &&
879 find_next_zero_bit(zinfo->empty_zones, end, begin) != end) {
880 pos += zinfo->zone_size;
885 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
889 sb_zone = sb_zone_number(shift, i);
890 if (!(end <= sb_zone ||
891 sb_zone + BTRFS_NR_SB_LOG_ZONES <= begin)) {
893 pos = zone_start_physical(
894 sb_zone + BTRFS_NR_SB_LOG_ZONES, zinfo);
898 /* We also need to exclude regular superblock positions */
899 sb_pos = btrfs_sb_offset(i);
900 if (!(pos + num_bytes <= sb_pos ||
901 sb_pos + BTRFS_SUPER_INFO_SIZE <= pos)) {
903 pos = ALIGN(sb_pos + BTRFS_SUPER_INFO_SIZE,
915 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
916 u64 length, u64 *bytes)
921 ret = blkdev_zone_mgmt(device->bdev, REQ_OP_ZONE_RESET,
922 physical >> SECTOR_SHIFT, length >> SECTOR_SHIFT,
929 btrfs_dev_set_zone_empty(device, physical);
930 physical += device->zone_info->zone_size;
931 length -= device->zone_info->zone_size;
937 int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
939 struct btrfs_zoned_device_info *zinfo = device->zone_info;
940 const u8 shift = zinfo->zone_size_shift;
941 unsigned long begin = start >> shift;
942 unsigned long end = (start + size) >> shift;
946 ASSERT(IS_ALIGNED(start, zinfo->zone_size));
947 ASSERT(IS_ALIGNED(size, zinfo->zone_size));
949 if (end > zinfo->nr_zones)
952 /* All the zones are conventional */
953 if (find_next_bit(zinfo->seq_zones, begin, end) == end)
956 /* All the zones are sequential and empty */
957 if (find_next_zero_bit(zinfo->seq_zones, begin, end) == end &&
958 find_next_zero_bit(zinfo->empty_zones, begin, end) == end)
961 for (pos = start; pos < start + size; pos += zinfo->zone_size) {
964 if (!btrfs_dev_is_sequential(device, pos) ||
965 btrfs_dev_is_empty_zone(device, pos))
968 /* Free regions should be empty */
971 "zoned: resetting device %s (devid %llu) zone %llu for allocation",
972 rcu_str_deref(device->name), device->devid, pos >> shift);
975 ret = btrfs_reset_device_zone(device, pos, zinfo->zone_size,
985 * Calculate an allocation pointer from the extent allocation information
986 * for a block group consist of conventional zones. It is pointed to the
987 * end of the highest addressed extent in the block group as an allocation
990 static int calculate_alloc_pointer(struct btrfs_block_group *cache,
993 struct btrfs_fs_info *fs_info = cache->fs_info;
994 struct btrfs_root *root = fs_info->extent_root;
995 struct btrfs_path *path;
996 struct btrfs_key key;
997 struct btrfs_key found_key;
1001 path = btrfs_alloc_path();
1005 key.objectid = cache->start + cache->length;
1009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1010 /* We should not find the exact match */
1016 ret = btrfs_previous_extent_item(root, path, cache->start);
1025 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
1027 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
1028 length = found_key.offset;
1030 length = fs_info->nodesize;
1032 if (!(found_key.objectid >= cache->start &&
1033 found_key.objectid + length <= cache->start + cache->length)) {
1037 *offset_ret = found_key.objectid + length - cache->start;
1041 btrfs_free_path(path);
1045 int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1047 struct btrfs_fs_info *fs_info = cache->fs_info;
1048 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
1049 struct extent_map *em;
1050 struct map_lookup *map;
1051 struct btrfs_device *device;
1052 u64 logical = cache->start;
1053 u64 length = cache->length;
1057 unsigned int nofs_flag;
1058 u64 *alloc_offsets = NULL;
1060 u32 num_sequential = 0, num_conventional = 0;
1062 if (!btrfs_is_zoned(fs_info))
1066 if (!IS_ALIGNED(length, fs_info->zone_size)) {
1068 "zoned: block group %llu len %llu unaligned to zone size %llu",
1069 logical, length, fs_info->zone_size);
1073 /* Get the chunk mapping */
1074 read_lock(&em_tree->lock);
1075 em = lookup_extent_mapping(em_tree, logical, length);
1076 read_unlock(&em_tree->lock);
1081 map = em->map_lookup;
1083 alloc_offsets = kcalloc(map->num_stripes, sizeof(*alloc_offsets), GFP_NOFS);
1084 if (!alloc_offsets) {
1085 free_extent_map(em);
1089 for (i = 0; i < map->num_stripes; i++) {
1091 struct blk_zone zone;
1092 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
1093 int dev_replace_is_ongoing = 0;
1095 device = map->stripes[i].dev;
1096 physical = map->stripes[i].physical;
1098 if (device->bdev == NULL) {
1099 alloc_offsets[i] = WP_MISSING_DEV;
1103 is_sequential = btrfs_dev_is_sequential(device, physical);
1109 if (!is_sequential) {
1110 alloc_offsets[i] = WP_CONVENTIONAL;
1115 * This zone will be used for allocation, so mark this zone
1118 btrfs_dev_clear_zone_empty(device, physical);
1120 down_read(&dev_replace->rwsem);
1121 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
1122 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
1123 btrfs_dev_clear_zone_empty(dev_replace->tgtdev, physical);
1124 up_read(&dev_replace->rwsem);
1127 * The group is mapped to a sequential zone. Get the zone write
1128 * pointer to determine the allocation offset within the zone.
1130 WARN_ON(!IS_ALIGNED(physical, fs_info->zone_size));
1131 nofs_flag = memalloc_nofs_save();
1132 ret = btrfs_get_dev_zone(device, physical, &zone);
1133 memalloc_nofs_restore(nofs_flag);
1134 if (ret == -EIO || ret == -EOPNOTSUPP) {
1136 alloc_offsets[i] = WP_MISSING_DEV;
1142 if (zone.type == BLK_ZONE_TYPE_CONVENTIONAL) {
1143 btrfs_err_in_rcu(fs_info,
1144 "zoned: unexpected conventional zone %llu on device %s (devid %llu)",
1145 zone.start << SECTOR_SHIFT,
1146 rcu_str_deref(device->name), device->devid);
1151 switch (zone.cond) {
1152 case BLK_ZONE_COND_OFFLINE:
1153 case BLK_ZONE_COND_READONLY:
1155 "zoned: offline/readonly zone %llu on device %s (devid %llu)",
1156 physical >> device->zone_info->zone_size_shift,
1157 rcu_str_deref(device->name), device->devid);
1158 alloc_offsets[i] = WP_MISSING_DEV;
1160 case BLK_ZONE_COND_EMPTY:
1161 alloc_offsets[i] = 0;
1163 case BLK_ZONE_COND_FULL:
1164 alloc_offsets[i] = fs_info->zone_size;
1167 /* Partially used zone */
1169 ((zone.wp - zone.start) << SECTOR_SHIFT);
1174 if (num_sequential > 0)
1175 cache->seq_zone = true;
1177 if (num_conventional > 0) {
1179 * Avoid calling calculate_alloc_pointer() for new BG. It
1180 * is no use for new BG. It must be always 0.
1182 * Also, we have a lock chain of extent buffer lock ->
1183 * chunk mutex. For new BG, this function is called from
1184 * btrfs_make_block_group() which is already taking the
1185 * chunk mutex. Thus, we cannot call
1186 * calculate_alloc_pointer() which takes extent buffer
1187 * locks to avoid deadlock.
1190 cache->alloc_offset = 0;
1193 ret = calculate_alloc_pointer(cache, &last_alloc);
1194 if (ret || map->num_stripes == num_conventional) {
1196 cache->alloc_offset = last_alloc;
1199 "zoned: failed to determine allocation offset of bg %llu",
1205 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
1206 case 0: /* single */
1207 if (alloc_offsets[0] == WP_MISSING_DEV) {
1209 "zoned: cannot recover write pointer for zone %llu",
1214 cache->alloc_offset = alloc_offsets[0];
1216 case BTRFS_BLOCK_GROUP_DUP:
1217 case BTRFS_BLOCK_GROUP_RAID1:
1218 case BTRFS_BLOCK_GROUP_RAID0:
1219 case BTRFS_BLOCK_GROUP_RAID10:
1220 case BTRFS_BLOCK_GROUP_RAID5:
1221 case BTRFS_BLOCK_GROUP_RAID6:
1222 /* non-single profiles are not supported yet */
1224 btrfs_err(fs_info, "zoned: profile %s not yet supported",
1225 btrfs_bg_type_to_raid_name(map->type));
1231 if (cache->alloc_offset > fs_info->zone_size) {
1233 "zoned: invalid write pointer %llu in block group %llu",
1234 cache->alloc_offset, cache->start);
1238 /* An extent is allocated after the write pointer */
1239 if (!ret && num_conventional && last_alloc > cache->alloc_offset) {
1241 "zoned: got wrong write pointer in BG %llu: %llu > %llu",
1242 logical, last_alloc, cache->alloc_offset);
1247 cache->meta_write_pointer = cache->alloc_offset + cache->start;
1249 kfree(alloc_offsets);
1250 free_extent_map(em);
1255 void btrfs_calc_zone_unusable(struct btrfs_block_group *cache)
1259 if (!btrfs_is_zoned(cache->fs_info))
1262 WARN_ON(cache->bytes_super != 0);
1263 unusable = cache->alloc_offset - cache->used;
1264 free = cache->length - cache->alloc_offset;
1266 /* We only need ->free_space in ALLOC_SEQ block groups */
1267 cache->last_byte_to_unpin = (u64)-1;
1268 cache->cached = BTRFS_CACHE_FINISHED;
1269 cache->free_space_ctl->free_space = free;
1270 cache->zone_unusable = unusable;
1272 /* Should not have any excluded extents. Just in case, though */
1273 btrfs_free_excluded_extents(cache);
1276 void btrfs_redirty_list_add(struct btrfs_transaction *trans,
1277 struct extent_buffer *eb)
1279 struct btrfs_fs_info *fs_info = eb->fs_info;
1281 if (!btrfs_is_zoned(fs_info) ||
1282 btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN) ||
1283 !list_empty(&eb->release_list))
1286 set_extent_buffer_dirty(eb);
1287 set_extent_bits_nowait(&trans->dirty_pages, eb->start,
1288 eb->start + eb->len - 1, EXTENT_DIRTY);
1289 memzero_extent_buffer(eb, 0, eb->len);
1290 set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
1292 spin_lock(&trans->releasing_ebs_lock);
1293 list_add_tail(&eb->release_list, &trans->releasing_ebs);
1294 spin_unlock(&trans->releasing_ebs_lock);
1295 atomic_inc(&eb->refs);
1298 void btrfs_free_redirty_list(struct btrfs_transaction *trans)
1300 spin_lock(&trans->releasing_ebs_lock);
1301 while (!list_empty(&trans->releasing_ebs)) {
1302 struct extent_buffer *eb;
1304 eb = list_first_entry(&trans->releasing_ebs,
1305 struct extent_buffer, release_list);
1306 list_del_init(&eb->release_list);
1307 free_extent_buffer(eb);
1309 spin_unlock(&trans->releasing_ebs_lock);
1312 bool btrfs_use_zone_append(struct btrfs_inode *inode, u64 start)
1314 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1315 struct btrfs_block_group *cache;
1318 if (!btrfs_is_zoned(fs_info))
1321 if (!fs_info->max_zone_append_size)
1324 if (!is_data_inode(&inode->vfs_inode))
1327 cache = btrfs_lookup_block_group(fs_info, start);
1332 ret = cache->seq_zone;
1333 btrfs_put_block_group(cache);
1338 void btrfs_record_physical_zoned(struct inode *inode, u64 file_offset,
1341 struct btrfs_ordered_extent *ordered;
1342 const u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
1344 if (bio_op(bio) != REQ_OP_ZONE_APPEND)
1347 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), file_offset);
1348 if (WARN_ON(!ordered))
1351 ordered->physical = physical;
1352 ordered->disk = bio->bi_bdev->bd_disk;
1353 ordered->partno = bio->bi_bdev->bd_partno;
1355 btrfs_put_ordered_extent(ordered);
1358 void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered)
1360 struct btrfs_inode *inode = BTRFS_I(ordered->inode);
1361 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1362 struct extent_map_tree *em_tree;
1363 struct extent_map *em;
1364 struct btrfs_ordered_sum *sum;
1365 struct block_device *bdev;
1366 u64 orig_logical = ordered->disk_bytenr;
1367 u64 *logical = NULL;
1370 /* Zoned devices should not have partitions. So, we can assume it is 0 */
1371 ASSERT(ordered->partno == 0);
1372 bdev = bdgrab(ordered->disk->part0);
1376 if (WARN_ON(btrfs_rmap_block(fs_info, orig_logical, bdev,
1377 ordered->physical, &logical, &nr,
1383 if (orig_logical == *logical)
1386 ordered->disk_bytenr = *logical;
1388 em_tree = &inode->extent_tree;
1389 write_lock(&em_tree->lock);
1390 em = search_extent_mapping(em_tree, ordered->file_offset,
1391 ordered->num_bytes);
1392 em->block_start = *logical;
1393 free_extent_map(em);
1394 write_unlock(&em_tree->lock);
1396 list_for_each_entry(sum, &ordered->list, list) {
1397 if (*logical < orig_logical)
1398 sum->bytenr -= orig_logical - *logical;
1400 sum->bytenr += *logical - orig_logical;
1408 bool btrfs_check_meta_write_pointer(struct btrfs_fs_info *fs_info,
1409 struct extent_buffer *eb,
1410 struct btrfs_block_group **cache_ret)
1412 struct btrfs_block_group *cache;
1415 if (!btrfs_is_zoned(fs_info))
1420 if (cache && (eb->start < cache->start ||
1421 cache->start + cache->length <= eb->start)) {
1422 btrfs_put_block_group(cache);
1428 cache = btrfs_lookup_block_group(fs_info, eb->start);
1431 if (cache->meta_write_pointer != eb->start) {
1432 btrfs_put_block_group(cache);
1436 cache->meta_write_pointer = eb->start + eb->len;
1445 void btrfs_revert_meta_write_pointer(struct btrfs_block_group *cache,
1446 struct extent_buffer *eb)
1448 if (!btrfs_is_zoned(eb->fs_info) || !cache)
1451 ASSERT(cache->meta_write_pointer == eb->start + eb->len);
1452 cache->meta_write_pointer = eb->start;
1455 int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 length)
1457 if (!btrfs_dev_is_sequential(device, physical))
1460 return blkdev_issue_zeroout(device->bdev, physical >> SECTOR_SHIFT,
1461 length >> SECTOR_SHIFT, GFP_NOFS, 0);
1464 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
1465 struct blk_zone *zone)
1467 struct btrfs_bio *bbio = NULL;
1468 u64 mapped_length = PAGE_SIZE;
1469 unsigned int nofs_flag;
1473 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
1474 &mapped_length, &bbio);
1475 if (ret || !bbio || mapped_length < PAGE_SIZE) {
1476 btrfs_put_bbio(bbio);
1480 if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK)
1483 nofs_flag = memalloc_nofs_save();
1484 nmirrors = (int)bbio->num_stripes;
1485 for (i = 0; i < nmirrors; i++) {
1486 u64 physical = bbio->stripes[i].physical;
1487 struct btrfs_device *dev = bbio->stripes[i].dev;
1489 /* Missing device */
1493 ret = btrfs_get_dev_zone(dev, physical, zone);
1494 /* Failing device */
1495 if (ret == -EIO || ret == -EOPNOTSUPP)
1499 memalloc_nofs_restore(nofs_flag);
1505 * Synchronize write pointer in a zone at @physical_start on @tgt_dev, by
1506 * filling zeros between @physical_pos to a write pointer of dev-replace
1509 int btrfs_sync_zone_write_pointer(struct btrfs_device *tgt_dev, u64 logical,
1510 u64 physical_start, u64 physical_pos)
1512 struct btrfs_fs_info *fs_info = tgt_dev->fs_info;
1513 struct blk_zone zone;
1518 if (!btrfs_dev_is_sequential(tgt_dev, physical_pos))
1521 ret = read_zone_info(fs_info, logical, &zone);
1525 wp = physical_start + ((zone.wp - zone.start) << SECTOR_SHIFT);
1527 if (physical_pos == wp)
1530 if (physical_pos > wp)
1533 length = wp - physical_pos;
1534 return btrfs_zoned_issue_zeroout(tgt_dev, physical_pos, length);
1537 struct btrfs_device *btrfs_zoned_get_device(struct btrfs_fs_info *fs_info,
1538 u64 logical, u64 length)
1540 struct btrfs_device *device;
1541 struct extent_map *em;
1542 struct map_lookup *map;
1544 em = btrfs_get_chunk_map(fs_info, logical, length);
1546 return ERR_CAST(em);
1548 map = em->map_lookup;
1549 /* We only support single profile for now */
1550 ASSERT(map->num_stripes == 1);
1551 device = map->stripes[0].dev;
1553 free_extent_map(em);