[PATCH] scsi_debug: Fix shared UUID issue when creating multiple hosts

Luis Chamberlain posted 1 patch 3 months, 1 week ago
drivers/scsi/scsi_debug.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
[PATCH] scsi_debug: Fix shared UUID issue when creating multiple hosts
Posted by Luis Chamberlain 3 months, 1 week ago
When creating multiple scsi_debug hosts (e.g., modprobe scsi_debug
add_host=2), all devices share the same backing storage by default.
This causes filesystem UUIDs to be shared between devices like /dev/sda
and /dev/sdb, leading to confusion and potential mount issues.

For example:
  # modprobe scsi_debug add_host=2 dev_size_mb=1024
  # mkfs.xfs -f /dev/sda
  # wipefs /dev/sda /dev/sdb
Both devices show the same UUID, which is incorrect behavior.

Fix this by automatically enabling separate per-host stores when
multiple hosts are created, while preserving backward compatibility
for single-host scenarios. Users can still explicitly control this
behavior via the per_host_store parameter.

After this fix:
- Single host: Uses shared store (unchanged behavior)
- Multiple hosts: Automatically uses separate stores (fixes UUID issue)
- Explicit per_host_store=1: Always separate stores (unchanged behavior)

Reported-by: Swarna Prabhu <s.prabhu@samsung.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
---
 drivers/scsi/scsi_debug.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index f0eec4708ddd..4d0b17861adb 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -8034,12 +8034,18 @@ static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
 	bool found;
 	unsigned long idx;
 	struct sdeb_store_info *sip;
+	bool uniq_uuid_required = false;
 	bool want_phs = (sdebug_fake_rw == 0) && sdebug_per_host_store;
 	int delta_hosts;
 
 	if (sscanf(buf, "%d", &delta_hosts) != 1)
 		return -EINVAL;
 	if (delta_hosts > 0) {
+		if (delta_hosts > 1) {
+			uniq_uuid_required = true;
+			if (sdebug_fake_rw == 0)
+				want_phs = true;
+		}
 		do {
 			found = false;
 			if (want_phs) {
@@ -8054,7 +8060,7 @@ static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
 				else
 					sdebug_do_add_host(true);
 			} else {
-				sdebug_do_add_host(false);
+				sdebug_do_add_host(uniq_uuid_required);
 			}
 		} while (--delta_hosts);
 	} else if (delta_hosts < 0) {
@@ -8383,6 +8389,7 @@ static struct device *pseudo_primary;
 static int __init scsi_debug_init(void)
 {
 	bool want_store = (sdebug_fake_rw == 0);
+	bool uniq_uuid_required = false;
 	unsigned long sz;
 	int k, ret, hosts_to_add;
 	int idx = -1;
@@ -8578,6 +8585,9 @@ static int __init scsi_debug_init(void)
 	}
 
 	hosts_to_add = sdebug_add_host;
+	if (hosts_to_add > 1)
+		uniq_uuid_required = true;
+
 	sdebug_add_host = 0;
 
 	sdebug_debugfs_root = debugfs_create_dir("scsi_debug", NULL);
@@ -8593,8 +8603,9 @@ static int __init scsi_debug_init(void)
 				break;
 			}
 		} else {
-			ret = sdebug_do_add_host(want_store &&
-						 sdebug_per_host_store);
+			bool use_store = want_store &&
+				(sdebug_per_host_store || uniq_uuid_required);
+			ret = sdebug_do_add_host(use_store);
 			if (ret < 0) {
 				pr_err("add_host k=%d error=%d\n", k, -ret);
 				break;
-- 
2.45.2
Re: [PATCH] scsi_debug: Fix shared UUID issue when creating multiple hosts
Posted by John Garry 3 months, 1 week ago
On 01/07/2025 23:02, Luis Chamberlain wrote:
> When creating multiple scsi_debug hosts (e.g., modprobe scsi_debug
> add_host=2), all devices share the same backing storage by default.
> This causes filesystem UUIDs to be shared between devices like /dev/sda
> and /dev/sdb, leading to confusion and potential mount issues.
> 
> For example:
>    # modprobe scsi_debug add_host=2 dev_size_mb=1024
>    # mkfs.xfs -f /dev/sda
>    # wipefs /dev/sda /dev/sdb
> Both devices show the same UUID, which is incorrect behavior.
> 
> Fix this by automatically enabling separate per-host stores when
> multiple hosts are created, while preserving backward compatibility
> for single-host scenarios. Users can still explicitly control this
> behavior via the per_host_store parameter.
> 
> After this fix:
> - Single host: Uses shared store (unchanged behavior)

Does phs even have any affect for only a single scsi_debug host?

I thought that phs means that each host has its own backing storage 
which is shared among all devices for that host.

> - Multiple hosts: Automatically uses separate stores (fixes UUID issue)
> - Explicit per_host_store=1: Always separate stores (unchanged behavior)
 > > Reported-by: Swarna Prabhu <s.prabhu@samsung.com>
> Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
> ---
>   drivers/scsi/scsi_debug.c | 17 ++++++++++++++---
>   1 file changed, 14 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
> index f0eec4708ddd..4d0b17861adb 100644
> --- a/drivers/scsi/scsi_debug.c
> +++ b/drivers/scsi/scsi_debug.c
> @@ -8034,12 +8034,18 @@ static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
>   	bool found;
>   	unsigned long idx;
>   	struct sdeb_store_info *sip;
> +	bool uniq_uuid_required = false;
>   	bool want_phs = (sdebug_fake_rw == 0) && sdebug_per_host_store;
>   	int delta_hosts;
>   
>   	if (sscanf(buf, "%d", &delta_hosts) != 1)
>   		return -EINVAL;
>   	if (delta_hosts > 0) {
> +		if (delta_hosts > 1) {
> +			uniq_uuid_required = true;
> +			if (sdebug_fake_rw == 0)
> +				want_phs = true;
> +		}
>   		do {
>   			found = false;
>   			if (want_phs) {
> @@ -8054,7 +8060,7 @@ static ssize_t add_host_store(struct device_driver *ddp, const char *buf,
>   				else
>   					sdebug_do_add_host(true);
>   			} else {
> -				sdebug_do_add_host(false);
> +				sdebug_do_add_host(uniq_uuid_required);
>   			}
>   		} while (--delta_hosts);
>   	} else if (delta_hosts < 0) {
> @@ -8383,6 +8389,7 @@ static struct device *pseudo_primary;
>   static int __init scsi_debug_init(void)
>   {
>   	bool want_store = (sdebug_fake_rw == 0);
> +	bool uniq_uuid_required = false;
>   	unsigned long sz;
>   	int k, ret, hosts_to_add;
>   	int idx = -1;
> @@ -8578,6 +8585,9 @@ static int __init scsi_debug_init(void)
>   	}
>   
>   	hosts_to_add = sdebug_add_host;
> +	if (hosts_to_add > 1)
> +		uniq_uuid_required = true;
> +
>   	sdebug_add_host = 0;
>   
>   	sdebug_debugfs_root = debugfs_create_dir("scsi_debug", NULL);
> @@ -8593,8 +8603,9 @@ static int __init scsi_debug_init(void)
>   				break;
>   			}
>   		} else {
> -			ret = sdebug_do_add_host(want_store &&
> -						 sdebug_per_host_store);
> +			bool use_store = want_store &&
> +				(sdebug_per_host_store || uniq_uuid_required);
> +			ret = sdebug_do_add_host(use_store);
>   			if (ret < 0) {
>   				pr_err("add_host k=%d error=%d\n", k, -ret);
>   				break;