All zones of zoned block devices should be reset before writing. Support
this by introducing PREP_DEVICE_HMZONED.

This commit export discard_blocks() and use it from
btrfs_discard_all_zones().

Signed-off-by: Naohiro Aota <naohiro.a...@wdc.com>
---
 common/device-utils.c | 23 +++++++++++++++++++++--
 common/device-utils.h |  2 ++
 common/hmzoned.c      | 27 +++++++++++++++++++++++++++
 common/hmzoned.h      |  5 +++++
 4 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/common/device-utils.c b/common/device-utils.c
index 7fa9386f4677..c7046c22a9fb 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -29,6 +29,7 @@
 #include "common/internal.h"
 #include "common/messages.h"
 #include "common/utils.h"
+#include "common/hmzoned.h"
 
 #ifndef BLKDISCARD
 #define BLKDISCARD     _IO(0x12,119)
@@ -49,7 +50,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 */
@@ -155,6 +156,7 @@ out:
 int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
                u64 max_block_count, unsigned opflags)
 {
+       struct btrfs_zone_info zinfo;
        u64 block_count;
        struct stat st;
        int i, ret;
@@ -173,7 +175,24 @@ 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) {
+       ret = btrfs_get_zone_info(fd, file, opflags & PREP_DEVICE_HMZONED,
+                                 &zinfo);
+       if (ret < 0)
+               return 1;
+
+       if (opflags & PREP_DEVICE_HMZONED) {
+               printf("Resetting device zones %s (%u zones) ...\n",
+                       file, zinfo.nr_zones);
+               /*
+                * We cannot ignore zone discard (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_discard_all_zones(fd, &zinfo)) {
+                       error("failed to reset device '%s' zones", file);
+                       return 1;
+               }
+       } 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
diff --git a/common/device-utils.h b/common/device-utils.h
index d1799323d002..885a46937e0d 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_HMZONED     (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/common/hmzoned.c b/common/hmzoned.c
index 7114943458ef..70de111f22da 100644
--- a/common/hmzoned.c
+++ b/common/hmzoned.c
@@ -216,3 +216,30 @@ int btrfs_get_zone_info(int fd, const char *file, bool 
hmzoned,
 #endif
        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 discarded though a zone reset.
+ */
+int btrfs_discard_all_zones(int fd, struct btrfs_zone_info *zinfo)
+{
+       unsigned int i;
+
+       /* Zone size granularity */
+       for (i = 0; i < zinfo->nr_zones; i++) {
+               if (zinfo->zones[i].type == BLK_ZONE_TYPE_CONVENTIONAL) {
+                       discard_blocks(fd, zinfo->zones[i].start << 9,
+                                      zinfo->zone_size);
+               } else if (zinfo->zones[i].cond != BLK_ZONE_COND_EMPTY) {
+                       struct blk_zone_range range = {
+                               zinfo->zones[i].start,
+                               zinfo->zone_size >> 9 };
+                       if (ioctl(fd, BLKRESETZONE, &range) < 0)
+                               return errno;
+               }
+       }
+
+       return 0;
+}
diff --git a/common/hmzoned.h b/common/hmzoned.h
index fbcaaf2da20e..c4e20ae71d21 100644
--- a/common/hmzoned.h
+++ b/common/hmzoned.h
@@ -56,12 +56,17 @@ int btrfs_get_zone_info(int fd, const char *file, bool 
hmzoned,
 
 #ifdef BTRFS_ZONED
 bool zone_is_sequential(struct btrfs_zone_info *zinfo, u64 bytenr);
+int btrfs_discard_all_zones(int fd, struct btrfs_zone_info *zinfo);
 #else
 static inline bool zone_is_sequential(struct btrfs_zone_info *zinfo,
                                      u64 bytenr)
 {
        return true;
 }
+static inline int btrfs_discard_all_zones(int fd, struct btrfs_zone_info 
*zinfo)
+{
+       return -EOPNOTSUPP;
+}
 #endif /* BTRFS_ZONED */
 
 #endif /* __BTRFS_HMZONED_H__ */
-- 
2.23.0

Reply via email to