public inbox for linux-erofs@ozlabs.org
 help / color / mirror / Atom feed
* [PATCH] erofs: handle 48-bit blocks/uniaddr for extra devices
@ 2026-04-03  3:34 Zhan Xusheng
  2026-04-03  5:26 ` Gao Xiang
  0 siblings, 1 reply; 4+ messages in thread
From: Zhan Xusheng @ 2026-04-03  3:34 UTC (permalink / raw)
  To: Gao Xiang; +Cc: linux-erofs, linux-kernel, Zhan Xusheng

erofs_init_device() only reads blocks_lo and uniaddr_lo from the
on-disk device slot, ignoring blocks_hi and uniaddr_hi that were
introduced alongside the 48-bit block addressing feature.

For the primary device (dif0), erofs_read_superblock() already handles
this correctly by combining blocks_lo with blocks_hi when 48-bit
layout is enabled.  But the same logic was not applied to extra
devices.

With a 48-bit EROFS image using extra devices whose uniaddr or blocks
exceed 32-bit range, the truncated values cause erofs_map_dev() to
compute wrong physical addresses, leading to silent data corruption.

Fix this by reading blocks_hi and uniaddr_hi in erofs_init_device()
when 48-bit layout is enabled, consistent with the primary device
handling.

Fixes: 61ba89b57905 ("erofs: add 48-bit block addressing on-disk support")
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
Note: erofs-utils also needs corresponding fixes for the write path
(erofs_mkfs_format_devices) and a swapped hi/lo read in
erofs_read_superblock, which will be sent separately.
---
 fs/erofs/super.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 972a0c82198d..a04e70ef4fcc 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -177,6 +177,10 @@ static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
 
 	dif->blocks = le32_to_cpu(dis->blocks_lo);
 	dif->uniaddr = le32_to_cpu(dis->uniaddr_lo);
+	if (erofs_sb_has_48bit(sbi)) {
+		dif->blocks |= (u64)le32_to_cpu(dis->blocks_hi) << 32;
+		dif->uniaddr |= (u64)le16_to_cpu(dis->uniaddr_hi) << 32;
+	}
 	sbi->total_blocks += dif->blocks;
 	*pos += EROFS_DEVT_SLOT_SIZE;
 	return 0;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] erofs: handle 48-bit blocks/uniaddr for extra devices
  2026-04-03  3:34 [PATCH] erofs: handle 48-bit blocks/uniaddr for extra devices Zhan Xusheng
@ 2026-04-03  5:26 ` Gao Xiang
  2026-04-03  6:36   ` [PATCH v2] " Zhan Xusheng
  0 siblings, 1 reply; 4+ messages in thread
From: Gao Xiang @ 2026-04-03  5:26 UTC (permalink / raw)
  To: Zhan Xusheng, Gao Xiang; +Cc: linux-erofs, linux-kernel, Zhan Xusheng



On 2026/4/3 11:34, Zhan Xusheng wrote:
> erofs_init_device() only reads blocks_lo and uniaddr_lo from the
> on-disk device slot, ignoring blocks_hi and uniaddr_hi that were
> introduced alongside the 48-bit block addressing feature.
> 
> For the primary device (dif0), erofs_read_superblock() already handles
> this correctly by combining blocks_lo with blocks_hi when 48-bit
> layout is enabled.  But the same logic was not applied to extra
> devices.
> 
> With a 48-bit EROFS image using extra devices whose uniaddr or blocks
> exceed 32-bit range, the truncated values cause erofs_map_dev() to
> compute wrong physical addresses, leading to silent data corruption.
> 
> Fix this by reading blocks_hi and uniaddr_hi in erofs_init_device()
> when 48-bit layout is enabled, consistent with the primary device
> handling.
> 
> Fixes: 61ba89b57905 ("erofs: add 48-bit block addressing on-disk support")
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>

Yeah, it seems that part was never implemented,

but could you fix

__le32 blocks_hi; in `struct erofs_deviceslot` to `__le16` as well?

`blocks_hi` shouldn't be `__le32`.

> ---
> Note: erofs-utils also needs corresponding fixes for the write path
> (erofs_mkfs_format_devices) and a swapped hi/lo read in
> erofs_read_superblock, which will be sent separately.
> ---
>   fs/erofs/super.c | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/fs/erofs/super.c b/fs/erofs/super.c
> index 972a0c82198d..a04e70ef4fcc 100644
> --- a/fs/erofs/super.c
> +++ b/fs/erofs/super.c
> @@ -177,6 +177,10 @@ static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
>   
>   	dif->blocks = le32_to_cpu(dis->blocks_lo);
>   	dif->uniaddr = le32_to_cpu(dis->uniaddr_lo);
> +	if (erofs_sb_has_48bit(sbi)) {
> +		dif->blocks |= (u64)le32_to_cpu(dis->blocks_hi) << 32;
> +		dif->uniaddr |= (u64)le16_to_cpu(dis->uniaddr_hi) << 32;
> +	}

Maybe just

	bool _48bit = erofs_sb_has_48bit(sbi);

	..

	dif->blocks = le32_to_cpu(dis->blocks_lo) |
		(_48bit ? (u64)le16_to_cpu(dis->blocks_hi) << 32 : 0);
	dif->uniaddr = le32_to_cpu(dis->uniaddr_lo) |
		(_48bit ? (u64)le16_to_cpu(dis->uniaddr_hi) << 32 : 0);

Thanks,
Gao Xiang


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] erofs: handle 48-bit blocks/uniaddr for extra devices
  2026-04-03  5:26 ` Gao Xiang
@ 2026-04-03  6:36   ` Zhan Xusheng
  2026-04-03  8:27     ` Gao Xiang
  0 siblings, 1 reply; 4+ messages in thread
From: Zhan Xusheng @ 2026-04-03  6:36 UTC (permalink / raw)
  To: Gao Xiang; +Cc: linux-erofs, linux-kernel, Zhan Xusheng, Gao Xiang

erofs_init_device() only reads blocks_lo and uniaddr_lo from the
on-disk device slot, ignoring blocks_hi and uniaddr_hi that were
introduced alongside the 48-bit block addressing feature.

For the primary device (dif0), erofs_read_superblock() already handles
this correctly by combining blocks_lo with blocks_hi when 48-bit
layout is enabled.  But the same logic was not applied to extra
devices.

With a 48-bit EROFS image using extra devices whose uniaddr or blocks
exceed 32-bit range, the truncated values cause erofs_map_dev() to
compute wrong physical addresses, leading to silent data corruption.

Fix this by reading blocks_hi and uniaddr_hi in erofs_init_device()
when 48-bit layout is enabled, consistent with the primary device
handling.  Also fix the erofs_deviceslot on-disk definition where
blocks_hi was incorrectly declared as __le32 instead of __le16.

Fixes: 61ba89b57905 ("erofs: add 48-bit block addressing on-disk support")
Suggested-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
 fs/erofs/erofs_fs.h | 4 ++--
 fs/erofs/super.c    | 8 ++++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/fs/erofs/erofs_fs.h b/fs/erofs/erofs_fs.h
index b80c6bb33a58..7871b16c1d33 100644
--- a/fs/erofs/erofs_fs.h
+++ b/fs/erofs/erofs_fs.h
@@ -44,9 +44,9 @@ struct erofs_deviceslot {
 	u8 tag[64];		/* digest(sha256), etc. */
 	__le32 blocks_lo;	/* total blocks count of this device */
 	__le32 uniaddr_lo;	/* unified starting block of this device */
-	__le32 blocks_hi;	/* total blocks count MSB */
+	__le16 blocks_hi;	/* total blocks count MSB */
 	__le16 uniaddr_hi;	/* unified starting block MSB */
-	u8 reserved[50];
+	u8 reserved[52];
 };
 #define EROFS_DEVT_SLOT_SIZE	sizeof(struct erofs_deviceslot)
 
diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 972a0c82198d..802add6652fd 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -129,6 +129,7 @@ static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
 	struct erofs_fscache *fscache;
 	struct erofs_deviceslot *dis;
 	struct file *file;
+	bool _48bit;
 
 	dis = erofs_read_metabuf(buf, sb, *pos, false);
 	if (IS_ERR(dis))
@@ -175,8 +176,11 @@ static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
 		dif->file = file;
 	}
 
-	dif->blocks = le32_to_cpu(dis->blocks_lo);
-	dif->uniaddr = le32_to_cpu(dis->uniaddr_lo);
+	_48bit = erofs_sb_has_48bit(sbi);
+	dif->blocks = le32_to_cpu(dis->blocks_lo) |
+		(_48bit ? (u64)le16_to_cpu(dis->blocks_hi) << 32 : 0);
+	dif->uniaddr = le32_to_cpu(dis->uniaddr_lo) |
+		(_48bit ? (u64)le16_to_cpu(dis->uniaddr_hi) << 32 : 0);
 	sbi->total_blocks += dif->blocks;
 	*pos += EROFS_DEVT_SLOT_SIZE;
 	return 0;
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] erofs: handle 48-bit blocks/uniaddr for extra devices
  2026-04-03  6:36   ` [PATCH v2] " Zhan Xusheng
@ 2026-04-03  8:27     ` Gao Xiang
  0 siblings, 0 replies; 4+ messages in thread
From: Gao Xiang @ 2026-04-03  8:27 UTC (permalink / raw)
  To: Zhan Xusheng, Gao Xiang; +Cc: linux-erofs, linux-kernel, Zhan Xusheng



On 2026/4/3 14:36, Zhan Xusheng wrote:
> erofs_init_device() only reads blocks_lo and uniaddr_lo from the
> on-disk device slot, ignoring blocks_hi and uniaddr_hi that were
> introduced alongside the 48-bit block addressing feature.
> 
> For the primary device (dif0), erofs_read_superblock() already handles
> this correctly by combining blocks_lo with blocks_hi when 48-bit
> layout is enabled.  But the same logic was not applied to extra
> devices.
> 
> With a 48-bit EROFS image using extra devices whose uniaddr or blocks
> exceed 32-bit range, the truncated values cause erofs_map_dev() to
> compute wrong physical addresses, leading to silent data corruption.
> 
> Fix this by reading blocks_hi and uniaddr_hi in erofs_init_device()
> when 48-bit layout is enabled, consistent with the primary device
> handling.  Also fix the erofs_deviceslot on-disk definition where
> blocks_hi was incorrectly declared as __le32 instead of __le16.
> 
> Fixes: 61ba89b57905 ("erofs: add 48-bit block addressing on-disk support")
> Suggested-by: Gao Xiang <hsiangkao@linux.alibaba.com>
> Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>

Thanks,

Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Thanks,
Gao Xiang


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-04-03  8:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-03  3:34 [PATCH] erofs: handle 48-bit blocks/uniaddr for extra devices Zhan Xusheng
2026-04-03  5:26 ` Gao Xiang
2026-04-03  6:36   ` [PATCH v2] " Zhan Xusheng
2026-04-03  8:27     ` Gao Xiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox