[PATCH v3 1/2] md: prevent adding disks with larger logical_block_size to active arrays

linan666@huaweicloud.com posted 2 patches 1 month, 1 week ago
There is a newer version of this series
[PATCH v3 1/2] md: prevent adding disks with larger logical_block_size to active arrays
Posted by linan666@huaweicloud.com 1 month, 1 week ago
From: Li Nan <linan122@huawei.com>

When adding a disk to a md array, avoid updating the array's
logical_block_size to match the new disk. This prevents accidental
partition table loss that renders the array unusable.

The later patch will introduce a way to configure the array's
logical_block_size.

Signed-off-by: Li Nan <linan122@huawei.com>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
---
 drivers/md/md.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index cea8fc96abd3..206434591b97 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -6064,6 +6064,13 @@ int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev)
 	if (mddev_is_dm(mddev))
 		return 0;
 
+	if (queue_logical_block_size(rdev->bdev->bd_disk->queue) >
+	    queue_logical_block_size(mddev->gendisk->queue)) {
+		pr_err("%s: incompatible logical_block_size, can not add\n",
+		       mdname(mddev));
+		return -EINVAL;
+	}
+
 	lim = queue_limits_start_update(mddev->gendisk->queue);
 	queue_limits_stack_bdev(&lim, rdev->bdev, rdev->data_offset,
 				mddev->gendisk->disk_name);
-- 
2.39.2
Re: [PATCH v3 1/2] md: prevent adding disks with larger logical_block_size to active arrays
Posted by Yu Kuai 1 month, 1 week ago
Hi,

在 2025/08/25 15:59, linan666@huaweicloud.com 写道:
> From: Li Nan <linan122@huawei.com>
> 
> When adding a disk to a md array, avoid updating the array's
> logical_block_size to match the new disk. This prevents accidental
> partition table loss that renders the array unusable.
> 
> The later patch will introduce a way to configure the array's
> logical_block_size.
> 
> Signed-off-by: Li Nan <linan122@huawei.com>
> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
> ---
>   drivers/md/md.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 

Can you add a fix tag, as data loss is really serious.

Otherwise, feel free to add:

Reviewed-by: Yu Kuai <yukuai3@huawei.com>

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index cea8fc96abd3..206434591b97 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -6064,6 +6064,13 @@ int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev)
>   	if (mddev_is_dm(mddev))
>   		return 0;
>   
> +	if (queue_logical_block_size(rdev->bdev->bd_disk->queue) >
> +	    queue_logical_block_size(mddev->gendisk->queue)) {
> +		pr_err("%s: incompatible logical_block_size, can not add\n",
> +		       mdname(mddev));
> +		return -EINVAL;
> +	}
> +
>   	lim = queue_limits_start_update(mddev->gendisk->queue);
>   	queue_limits_stack_bdev(&lim, rdev->bdev, rdev->data_offset,
>   				mddev->gendisk->disk_name);
> 

Re: [PATCH v3 1/2] md: prevent adding disks with larger logical_block_size to active arrays
Posted by Paul Menzel 1 month, 1 week ago
Dear Li,


Thank you for your patch.

Am 25.08.25 um 09:59 schrieb linan666@huaweicloud.com:
> From: Li Nan <linan122@huawei.com>
> 
> When adding a disk to a md array, avoid updating the array's
> logical_block_size to match the new disk. This prevents accidental
> partition table loss that renders the array unusable.

Do you have a reproducer to test this?

> The later patch will introduce a way to configure the array's
> logical_block_size.
> 
> Signed-off-by: Li Nan <linan122@huawei.com>
> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
> ---
>   drivers/md/md.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index cea8fc96abd3..206434591b97 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -6064,6 +6064,13 @@ int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev)
>   	if (mddev_is_dm(mddev))
>   		return 0;
>   
> +	if (queue_logical_block_size(rdev->bdev->bd_disk->queue) >
> +	    queue_logical_block_size(mddev->gendisk->queue)) {
> +		pr_err("%s: incompatible logical_block_size, can not add\n",
> +		       mdname(mddev));
> +		return -EINVAL;
> +	}
> +
>   	lim = queue_limits_start_update(mddev->gendisk->queue);
>   	queue_limits_stack_bdev(&lim, rdev->bdev, rdev->data_offset,
>   				mddev->gendisk->disk_name);


Kind regards,

Paul
Re: [PATCH v3 1/2] md: prevent adding disks with larger logical_block_size to active arrays
Posted by Li Nan 1 month, 1 week ago

在 2025/8/25 16:10, Paul Menzel 写道:
> Dear Li,
> 
> 
> Thank you for your patch.
> 
> Am 25.08.25 um 09:59 schrieb linan666@huaweicloud.com:
>> From: Li Nan <linan122@huawei.com>
>>
>> When adding a disk to a md array, avoid updating the array's
>> logical_block_size to match the new disk. This prevents accidental
>> partition table loss that renders the array unusable.
> 
> Do you have a reproducer to test this?

Please try the following script:

```
#sd[de] lbs is 512, sdf is 4096
mdadm -CR /dev/md0 -l1 -n2 /dev/sd[de] --assume-clean

#512
cat /sys/block/md0/queue/logical_block_size

#create md0p1
printf "g\nn\n\n\n\nw\n" | fdisk /dev/md0
lsblk | grep md0

mdadm --fail /dev/md0 /dev/sdd
mdadm --add /dev/md0 /dev/sdf

#4096
cat /sys/block/md0/queue/logical_block_size

#partition loss
partprobe /dev/md0
lsblk | grep md0
```

> 
>> The later patch will introduce a way to configure the array's
>> logical_block_size.
>>
>> Signed-off-by: Li Nan <linan122@huawei.com>
>> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
>> ---
>>   drivers/md/md.c | 7 +++++++
>>   1 file changed, 7 insertions(+)
>>
>> diff --git a/drivers/md/md.c b/drivers/md/md.c
>> index cea8fc96abd3..206434591b97 100644
>> --- a/drivers/md/md.c
>> +++ b/drivers/md/md.c
>> @@ -6064,6 +6064,13 @@ int mddev_stack_new_rdev(struct mddev *mddev, 
>> struct md_rdev *rdev)
>>       if (mddev_is_dm(mddev))
>>           return 0;
>> +    if (queue_logical_block_size(rdev->bdev->bd_disk->queue) >
>> +        queue_logical_block_size(mddev->gendisk->queue)) {
>> +        pr_err("%s: incompatible logical_block_size, can not add\n",
>> +               mdname(mddev));
>> +        return -EINVAL;
>> +    }
>> +
>>       lim = queue_limits_start_update(mddev->gendisk->queue);
>>       queue_limits_stack_bdev(&lim, rdev->bdev, rdev->data_offset,
>>                   mddev->gendisk->disk_name);
> 
> 
> Kind regards,
> 
> Paul
> 
> 
> .

-- 
Thanks,
Nan