diff mbox series

[19/26] btrfs-progs: zoned: support wiping SB on sequential write zone

Message ID f364599e85396d50becbc7179b5dc00e0d9a9cc2.1619416549.git.naohiro.aota@wdc.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: zoned: zoned block device support | expand

Commit Message

Naohiro Aota April 26, 2021, 6:27 a.m. UTC
We cannot overwrite superblock magic in a sequential required zone.
Instead, we can reset the zone to wipe it.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 common/device-utils.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/common/device-utils.c b/common/device-utils.c
index c1006c501555..4230654653aa 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -106,7 +106,7 @@  static int zero_dev_clamped(int fd, struct btrfs_zoned_device_info *zinfo,
 	return zero_blocks(fd, start, end - start);
 }
 
-static int btrfs_wipe_existing_sb(int fd)
+static int btrfs_wipe_existing_sb(int fd, struct btrfs_zoned_device_info *zinfo)
 {
 	const char *off = NULL;
 	size_t len = 0;
@@ -141,14 +141,26 @@  static int btrfs_wipe_existing_sb(int fd)
 	if (len > sizeof(buf))
 		len = sizeof(buf);
 
-	memset(buf, 0, len);
-	ret = pwrite(fd, buf, len, offset);
-	if (ret < 0) {
-		error("cannot wipe existing superblock: %m");
-		ret = -1;
-	} else if (ret != len) {
-		error("cannot wipe existing superblock: wrote %d of %zd", ret, len);
-		ret = -1;
+	if (!zone_is_sequential(zinfo, offset)) {
+		memset(buf, 0, len);
+		ret = pwrite(fd, buf, len, offset);
+		if (ret < 0) {
+			error("cannot wipe existing superblock: %m");
+			ret = -1;
+		} else if (ret != len) {
+			error("cannot wipe existing superblock: wrote %d of %zd",
+			      ret, len);
+			ret = -1;
+		}
+	} else {
+		struct blk_zone *zone = &zinfo->zones[offset / zinfo->zone_size];
+
+		ret = btrfs_reset_dev_zone(fd, zone);
+		if (ret < 0) {
+			error(
+		"zoned: failed to wipe zones containing superblock: %m");
+			ret = -1;
+		}
 	}
 	fsync(fd);
 
@@ -227,7 +239,7 @@  int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
 		goto err;
 	}
 
-	ret = btrfs_wipe_existing_sb(fd);
+	ret = btrfs_wipe_existing_sb(fd, zinfo);
 	if (ret < 0) {
 		error("cannot wipe superblocks on %s", file);
 		goto err;