@@ -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_zoned_device_info *zinfo = NULL;
u64 block_count;
struct stat st;
int i, ret;
@@ -173,7 +175,30 @@ 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) {
+ if (!zinfo) {
+ error("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 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);
+ kfree(zinfo);
+ 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
@@ -198,6 +223,7 @@ 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);
+ kfree(zinfo);
return 1;
}
@@ -207,6 +233,8 @@ int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
return 1;
}
+ kfree(zinfo);
+
*block_count_ret = block_count;
return 0;
}
@@ -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);
@@ -16,6 +16,7 @@
*/
#include <sys/ioctl.h>
+#include <unistd.h>
#include "common/utils.h"
#include "common/device-utils.h"
@@ -151,6 +152,34 @@ static int report_zones(int fd, const char *file, u64 block_count,
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_zoned_device_info *zinfo)
+{
+ unsigned int i;
+
+ ASSERT(zinfo);
+
+ /* 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 fsync(fd);
+}
+
#endif
int btrfs_get_zone_info(int fd, const char *file, bool hmzoned,
@@ -54,12 +54,18 @@ int btrfs_get_zone_info(int fd, const char *file, bool hmzoned,
#ifdef BTRFS_ZONED
bool zone_is_sequential(struct btrfs_zoned_device_info *zinfo, u64 bytenr);
+int btrfs_discard_all_zones(int fd, struct btrfs_zoned_device_info *zinfo);
#else
static inline bool zone_is_sequential(struct btrfs_zoned_device_info *zinfo,
u64 bytenr)
{
return true;
}
+static inline int btrfs_discard_all_zones(int fd,
+ struct btrfs_zoned_device_info *zinfo)
+{
+ return -EOPNOTSUPP;
+}
#endif /* BTRFS_ZONED */
#endif /* __BTRFS_HMZONED_H__ */
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.aota@wdc.com> --- common/device-utils.c | 32 ++++++++++++++++++++++++++++++-- common/device-utils.h | 2 ++ common/hmzoned.c | 29 +++++++++++++++++++++++++++++ common/hmzoned.h | 6 ++++++ 4 files changed, 67 insertions(+), 2 deletions(-)