Thread (2 messages) 2 messages, 1 author, 2021-05-27

Re: [PATCH] btrfs: zoned: fix zone number to sector/physical calculation

From: Naohiro Aota <naohiro.aota@wdc.com>
Date: 2021-05-27 05:42:42
Also in: linux-btrfs

On Thu, May 27, 2021 at 10:46:59AM +0900, Naohiro Aota wrote:
quoted hunk ↗ jump to hunk
In btrfs_get_dev_zone_info(), we have "u32 sb_zone" and calculate "sector_t
sector" by shifting it. But, this "sector" is calculated in 32bit, leading
it to be 0 for the 2nd superblock copy.

Since zone number is u32, shifting it to sector (sector_t) or physical
address (u64) can easily trigger a missing cast bug like this.

This commit introduces helpers to convert zone number to sector/LBA, so we
won't fall into the same pitfall again.

Fixes: 12659251ca5d ("btrfs: implement log-structured superblock for ZONED mode")
Cc: stable@vger.kernel.org # 5.11+
Reported-by: Dmitry Fomichev <redacted>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
 fs/btrfs/zoned.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 5feb76bdfc06..a7f77f0a5a86 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -153,6 +153,19 @@ static inline u32 sb_zone_number(int shift, int mirror)
 	return (u32)zone;
 }
 
+static inline sector_t zone_start_sector(u32 zone_number, struct block_device *bdev)
+{
+	sector_t zone_sectors = bdev_zone_sectors(bdev);
+
+	return (sector_t)zone_number << ilog2(zone_sectors);
+}
+
+static inline u64 zone_start_physical(u32 zone_number,
+			   struct btrfs_zoned_device_info *zone_info)
+{
+	return (u64)zone_number << zone_info->zone_size_shift;
+}
+
I just noticed an indentation fix missed the patch. I will resend
revised version.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help