diff mbox series

[2/2] btrfs-progs: zoned: check SB zone existence properly

Message ID 5162f065fa9b020c2a3cf1906327fae7c0a16913.1697104952.git.naohiro.aota@wdc.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: zoned: check existence of SB zone, not LBA | expand

Commit Message

Naohiro Aota Oct. 12, 2023, 2:19 p.m. UTC
Currently, write_dev_supers() compares the superblock location vs the size
of the device to check if it can write the superblock. This is not correct
for a zoned device, whose superblock location is different than a regular
device.

Introduce check_sb_location() to check if the superblock zone exists for
the zoned case.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 kernel-shared/disk-io.c | 9 ++++++++-
 kernel-shared/zoned.c   | 8 ++++++++
 kernel-shared/zoned.h   | 6 ++++++
 3 files changed, 22 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c
index 1b1531c52ad7..a056f2ca0560 100644
--- a/kernel-shared/disk-io.c
+++ b/kernel-shared/disk-io.c
@@ -1998,6 +1998,13 @@  int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
 	return transid > 0 ? 0 : -1;
 }
 
+static bool check_sb_location(struct btrfs_device *device, u64 bytenr)
+{
+	if (!device->zone_info)
+		return bytenr + BTRFS_SUPER_INFO_SIZE <= device->total_bytes;
+	return btrfs_sb_zone_exists(device, bytenr);
+}
+
 static int write_dev_supers(struct btrfs_fs_info *fs_info,
 			    struct btrfs_super_block *sb,
 			    struct btrfs_device *device)
@@ -2048,7 +2055,7 @@  static int write_dev_supers(struct btrfs_fs_info *fs_info,
 
 	for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
 		bytenr = btrfs_sb_offset(i);
-		if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
+		if (!check_sb_location(device, bytenr))
 			break;
 
 		btrfs_set_super_bytenr(sb, bytenr);
diff --git a/kernel-shared/zoned.c b/kernel-shared/zoned.c
index 206ad906c985..491d52eab942 100644
--- a/kernel-shared/zoned.c
+++ b/kernel-shared/zoned.c
@@ -1175,3 +1175,11 @@  int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info)
 out:
 	return ret;
 }
+
+bool btrfs_sb_zone_exists(struct btrfs_device *device, u64 bytenr)
+{
+	u32 zone_num = sb_bytenr_to_sb_zone(bytenr,
+					    ilog2(device->zone_info->zone_size));
+
+	return zone_num + 1 <= device->zone_info->nr_zones - 1;
+}
diff --git a/kernel-shared/zoned.h b/kernel-shared/zoned.h
index f4b0d5d06301..6eba86d266bf 100644
--- a/kernel-shared/zoned.h
+++ b/kernel-shared/zoned.h
@@ -153,6 +153,7 @@  int btrfs_reset_all_zones(int fd, struct btrfs_zoned_device_info *zinfo);
 int zero_zone_blocks(int fd, struct btrfs_zoned_device_info *zinfo, off_t start,
 		     size_t len);
 int btrfs_wipe_temporary_sb(struct btrfs_fs_devices *fs_devices);
+bool btrfs_sb_zone_exists(struct btrfs_device *device, u64 bytenr);
 
 #else
 
@@ -225,6 +226,11 @@  static inline bool zoned_profile_supported(u64 map_type, bool rst)
 	return false;
 }
 
+static inline bool btrfs_sb_zone_exists(struct btrfs_device *device, u64 bytenr)
+{
+	return true;
+}
+
 #endif /* BTRFS_ZONED */
 
 /*