From 58ec593892638c276cd12d08177711bba1c6d023 Mon Sep 17 00:00:00 2001 From: Naohiro Aota Date: Mon, 26 Apr 2021 15:27:33 +0900 Subject: [PATCH] btrfs-progs: zoned: support resetting zoned device All zones of zoned block devices should be reset before writing. Support this by introducing PREP_DEVICE_ZONED. btrfs_reset_all_zones() walk all the zones on a device, and reset a zone if it is sequential required zone, or discard the zone range otherwise. Signed-off-by: Naohiro Aota Signed-off-by: David Sterba --- common/device-utils.c | 35 +++++++++++++++++++++++++++++++---- common/device-utils.h | 2 ++ kernel-shared/zoned.c | 32 ++++++++++++++++++++++++++++++++ kernel-shared/zoned.h | 7 +++++++ 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/common/device-utils.c b/common/device-utils.c index e3b61c98..bdc653fc 100644 --- a/common/device-utils.c +++ b/common/device-utils.c @@ -26,6 +26,7 @@ #include #include "kernel-lib/sizes.h" #include "kernel-shared/disk-io.h" +#include "kernel-shared/zoned.h" #include "common/device-utils.h" #include "common/internal.h" #include "common/messages.h" @@ -50,7 +51,7 @@ static int discard_range(int fd, u64 start, u64 len) /* * Discard blocks in the given range in 1G chunks, the process is interruptible */ -static int discard_blocks(int fd, u64 start, u64 len) +int discard_blocks(int fd, u64 start, u64 len) { while (len > 0) { /* 1G granularity */ @@ -156,6 +157,7 @@ out: int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret, u64 max_block_count, unsigned opflags) { + struct btrfs_zoned_device_info *zinfo = NULL; u64 block_count; struct stat st; int i, ret; @@ -174,7 +176,27 @@ int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret, if (max_block_count) block_count = min(block_count, max_block_count); - if (opflags & PREP_DEVICE_DISCARD) { + if (opflags & PREP_DEVICE_ZONED) { + ret = btrfs_get_zone_info(fd, file, &zinfo); + if (ret < 0 || !zinfo) { + error("zoned: unable to load zone information of %s", + file); + return 1; + } + if (opflags & PREP_DEVICE_VERBOSE) + printf("Resetting device zones %s (%u zones) ...\n", + file, zinfo->nr_zones); + /* + * We cannot ignore zone reset errors for a zoned block + * device as this could result in the inability to write to + * non-empty sequential zones of the device. + */ + if (btrfs_reset_all_zones(fd, zinfo)) { + error("zoned: failed to reset device '%s' zones: %m", + file); + goto err; + } + } else if (opflags & PREP_DEVICE_DISCARD) { /* * We intentionally ignore errors from the discard ioctl. It * is not necessary for the mkfs functionality but just an @@ -199,17 +221,22 @@ int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret, if (ret < 0) { errno = -ret; error("failed to zero device '%s': %m", file); - return 1; + goto err; } ret = btrfs_wipe_existing_sb(fd); if (ret < 0) { error("cannot wipe superblocks on %s", file); - return 1; + goto err; } + free(zinfo); *block_count_ret = block_count; return 0; + +err: + free(zinfo); + return 1; } u64 btrfs_device_size(int fd, struct stat *st) diff --git a/common/device-utils.h b/common/device-utils.h index d1799323..e7e638a5 100644 --- a/common/device-utils.h +++ b/common/device-utils.h @@ -23,7 +23,9 @@ #define PREP_DEVICE_ZERO_END (1U << 0) #define PREP_DEVICE_DISCARD (1U << 1) #define PREP_DEVICE_VERBOSE (1U << 2) +#define PREP_DEVICE_ZONED (1U << 3) +int discard_blocks(int fd, u64 start, u64 len); u64 get_partition_size(const char *dev); u64 disk_size(const char *path); u64 btrfs_device_size(int fd, struct stat *st); diff --git a/kernel-shared/zoned.c b/kernel-shared/zoned.c index 50d24798..d1e26c35 100644 --- a/kernel-shared/zoned.c +++ b/kernel-shared/zoned.c @@ -357,6 +357,38 @@ static int report_zones(int fd, const char *file, return 0; } +/* + * Discard blocks in the zones of a zoned block device. Process this with zone + * size granularity so that blocks in conventional zones are discarded using + * discard_range and blocks in sequential zones are reset though a zone reset. + */ +int btrfs_reset_all_zones(int fd, struct btrfs_zoned_device_info *zinfo) +{ + unsigned int i; + int ret = 0; + + ASSERT(zinfo); + + /* Zone size granularity */ + for (i = 0; i < zinfo->nr_zones; i++) { + if (zinfo->zones[i].type == BLK_ZONE_TYPE_CONVENTIONAL) { + ret = discard_blocks(fd, + zinfo->zones[i].start << SECTOR_SHIFT, + zinfo->zone_size); + if (ret == EOPNOTSUPP) + ret = 0; + } else if (zinfo->zones[i].cond != BLK_ZONE_COND_EMPTY) { + ret = btrfs_reset_dev_zone(fd, &zinfo->zones[i]); + } else { + ret = 0; + } + + if (ret) + return ret; + } + return fsync(fd); +} + static int sb_log_location(int fd, struct blk_zone *zones, int rw, u64 *bytenr_ret) { u64 wp; diff --git a/kernel-shared/zoned.h b/kernel-shared/zoned.h index f8f841aa..396fa464 100644 --- a/kernel-shared/zoned.h +++ b/kernel-shared/zoned.h @@ -94,6 +94,7 @@ bool btrfs_redirty_extent_buffer_for_zoned(struct btrfs_fs_info *fs_info, u64 start, u64 end); int btrfs_reset_chunk_zones(struct btrfs_fs_info *fs_info, u64 devid, u64 offset, u64 length); +int btrfs_reset_all_zones(int fd, struct btrfs_zoned_device_info *zinfo); #else @@ -143,6 +144,12 @@ static inline int btrfs_reset_chunk_zones(struct btrfs_fs_info *fs_info, return 0; } +static inline int btrfs_reset_all_zones(int fd, + struct btrfs_zoned_device_info *zinfo) +{ + return -EOPNOTSUPP; +} + #endif /* BTRFS_ZONED */ static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)