diff mbox series

[-next,RFC,2/3] block: factor out the setting of GD_NEED_PART_SCAN

Message ID 20230212092641.2394146-3-yukuai1@huaweicloud.com (mailing list archive)
State New, archived
Headers show
Series block: fix scan partition for exclusively open device again | expand

Commit Message

Yu Kuai Feb. 12, 2023, 9:26 a.m. UTC
From: Yu Kuai <yukuai3@huawei.com>

In order to prevent scan partition for a device that is opened
exclusively by someone else, new conditions will be added to
disk_scan_partitions() in the next patch. Hence if device is opened
exclusively between bdev_add() and disk_scan_partitions(), the first
partition scan will fail unexpected. This patch factor out the setting
of GD_NEED_PART_SCAN to prevent the problem.

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 block/genhd.c | 2 +-
 block/ioctl.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

Comments

Hannes Reinecke Feb. 12, 2023, 11:36 a.m. UTC | #1
On 2/12/23 10:26, Yu Kuai wrote:
> From: Yu Kuai <yukuai3@huawei.com>
> 
> In order to prevent scan partition for a device that is opened
> exclusively by someone else, new conditions will be added to
> disk_scan_partitions() in the next patch. Hence if device is opened
> exclusively between bdev_add() and disk_scan_partitions(), the first
> partition scan will fail unexpected. This patch factor out the setting
> of GD_NEED_PART_SCAN to prevent the problem.
> 
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> ---
>   block/genhd.c | 2 +-
>   block/ioctl.c | 1 +
>   2 files changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/block/genhd.c b/block/genhd.c
> index 075d8da284f5..c0d1220bd798 100644
> --- a/block/genhd.c
> +++ b/block/genhd.c
> @@ -367,7 +367,6 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
>   	if (disk->open_partitions)
>   		return -EBUSY;
>   
> -	set_bit(GD_NEED_PART_SCAN, &disk->state);
>   	bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
>   	if (IS_ERR(bdev))
>   		return PTR_ERR(bdev);
> @@ -493,6 +492,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
>   		if (ret)
>   			goto out_unregister_bdi;
>   
> +		set_bit(GD_NEED_PART_SCAN, &disk->state);
>   		bdev_add(disk->part0, ddev->devt);
>   		if (get_capacity(disk))
>   			disk_scan_partitions(disk, FMODE_READ);
Usual caveat:
What happens if the flag is already set here?
Wouldn't that imply that another scan is underway?
And wouldn't it be better to use 'test_and_set()'?


> diff --git a/block/ioctl.c b/block/ioctl.c
> index 6dd49d877584..0eefcdb936a0 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -528,6 +528,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
>   			return -EACCES;
>   		if (bdev_is_partition(bdev))
>   			return -EINVAL;
> +		set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
>   		return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
>   	case BLKTRACESTART:
>   	case BLKTRACESTOP:
Similar here.

Cheers,

Hannes
Yu Kuai Feb. 13, 2023, 2:41 a.m. UTC | #2
Hi,

在 2023/02/12 19:36, Hannes Reinecke 写道:
> On 2/12/23 10:26, Yu Kuai wrote:
>> From: Yu Kuai <yukuai3@huawei.com>
>>
>> In order to prevent scan partition for a device that is opened
>> exclusively by someone else, new conditions will be added to
>> disk_scan_partitions() in the next patch. Hence if device is opened
>> exclusively between bdev_add() and disk_scan_partitions(), the first
>> partition scan will fail unexpected. This patch factor out the setting
>> of GD_NEED_PART_SCAN to prevent the problem.
>>
>> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
>> ---
>>   block/genhd.c | 2 +-
>>   block/ioctl.c | 1 +
>>   2 files changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/block/genhd.c b/block/genhd.c
>> index 075d8da284f5..c0d1220bd798 100644
>> --- a/block/genhd.c
>> +++ b/block/genhd.c
>> @@ -367,7 +367,6 @@ int disk_scan_partitions(struct gendisk *disk, 
>> fmode_t mode)
>>       if (disk->open_partitions)
>>           return -EBUSY;
>> -    set_bit(GD_NEED_PART_SCAN, &disk->state);
>>       bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
>>       if (IS_ERR(bdev))
>>           return PTR_ERR(bdev);
>> @@ -493,6 +492,7 @@ int __must_check device_add_disk(struct device 
>> *parent, struct gendisk *disk,
>>           if (ret)
>>               goto out_unregister_bdi;
>> +        set_bit(GD_NEED_PART_SCAN, &disk->state);
>>           bdev_add(disk->part0, ddev->devt);
>>           if (get_capacity(disk))
>>               disk_scan_partitions(disk, FMODE_READ);
> Usual caveat:
> What happens if the flag is already set here?
> Wouldn't that imply that another scan is underway?

I think this is not true, currently set the state just means we're going
to scan, and if some checking failed, the state will be left. I don't
have a good idea how to fix this yet...

Thanks,
Kuai

> And wouldn't it be better to use 'test_and_set()'?
> 
> 
>> diff --git a/block/ioctl.c b/block/ioctl.c
>> index 6dd49d877584..0eefcdb936a0 100644
>> --- a/block/ioctl.c
>> +++ b/block/ioctl.c
>> @@ -528,6 +528,7 @@ static int blkdev_common_ioctl(struct block_device 
>> *bdev, fmode_t mode,
>>               return -EACCES;
>>           if (bdev_is_partition(bdev))
>>               return -EINVAL;
>> +        set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
>>           return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
>>       case BLKTRACESTART:
>>       case BLKTRACESTOP:
> Similar here.
> 
> Cheers,
> 
> Hannes
Jan Kara Feb. 16, 2023, 1:17 p.m. UTC | #3
On Sun 12-02-23 17:26:40, Yu Kuai wrote:
> From: Yu Kuai <yukuai3@huawei.com>
> 
> In order to prevent scan partition for a device that is opened
> exclusively by someone else, new conditions will be added to
> disk_scan_partitions() in the next patch. Hence if device is opened
> exclusively between bdev_add() and disk_scan_partitions(), the first
> partition scan will fail unexpected. This patch factor out the setting
> of GD_NEED_PART_SCAN to prevent the problem.
> 
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>

I'd rather leave the setting of GD_NEED_PART_SCAN in disk_scan_partitions()
to keep it self-contained. On top of that we can set GD_NEED_PART_SCAN in
device_add_disk() to ensure initial partition scan but we should probably
also make sure flags like GD_SUPPRESS_PART_SCAN or GENHD_FL_NO_PART are not
set to avoid unwanted partition scanning.

								Honza

> ---
>  block/genhd.c | 2 +-
>  block/ioctl.c | 1 +
>  2 files changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/block/genhd.c b/block/genhd.c
> index 075d8da284f5..c0d1220bd798 100644
> --- a/block/genhd.c
> +++ b/block/genhd.c
> @@ -367,7 +367,6 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
>  	if (disk->open_partitions)
>  		return -EBUSY;
>  
> -	set_bit(GD_NEED_PART_SCAN, &disk->state);
>  	bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
>  	if (IS_ERR(bdev))
>  		return PTR_ERR(bdev);
> @@ -493,6 +492,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
>  		if (ret)
>  			goto out_unregister_bdi;
>  
> +		set_bit(GD_NEED_PART_SCAN, &disk->state);
>  		bdev_add(disk->part0, ddev->devt);
>  		if (get_capacity(disk))
>  			disk_scan_partitions(disk, FMODE_READ);
> diff --git a/block/ioctl.c b/block/ioctl.c
> index 6dd49d877584..0eefcdb936a0 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -528,6 +528,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
>  			return -EACCES;
>  		if (bdev_is_partition(bdev))
>  			return -EINVAL;
> +		set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
>  		return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
>  	case BLKTRACESTART:
>  	case BLKTRACESTOP:
> -- 
> 2.31.1
>
Yu Kuai Feb. 17, 2023, 1:44 a.m. UTC | #4
Hi, jan!

在 2023/02/16 21:17, Jan Kara 写道:
> On Sun 12-02-23 17:26:40, Yu Kuai wrote:
>> From: Yu Kuai <yukuai3@huawei.com>
>>
>> In order to prevent scan partition for a device that is opened
>> exclusively by someone else, new conditions will be added to
>> disk_scan_partitions() in the next patch. Hence if device is opened
>> exclusively between bdev_add() and disk_scan_partitions(), the first
>> partition scan will fail unexpected. This patch factor out the setting
>> of GD_NEED_PART_SCAN to prevent the problem.
>>
>> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> 
> I'd rather leave the setting of GD_NEED_PART_SCAN in disk_scan_partitions()
> to keep it self-contained. On top of that we can set GD_NEED_PART_SCAN in
> device_add_disk() to ensure initial partition scan but we should probably
> also make sure flags like GD_SUPPRESS_PART_SCAN or GENHD_FL_NO_PART are not
> set to avoid unwanted partition scanning.

Thanks for the suggestion, I'll remove this patch and do that in patch 3
in the next version.

Thanks,
Kuai
> 
> 								Honza
> 
>> ---
>>   block/genhd.c | 2 +-
>>   block/ioctl.c | 1 +
>>   2 files changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/block/genhd.c b/block/genhd.c
>> index 075d8da284f5..c0d1220bd798 100644
>> --- a/block/genhd.c
>> +++ b/block/genhd.c
>> @@ -367,7 +367,6 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
>>   	if (disk->open_partitions)
>>   		return -EBUSY;
>>   
>> -	set_bit(GD_NEED_PART_SCAN, &disk->state);
>>   	bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
>>   	if (IS_ERR(bdev))
>>   		return PTR_ERR(bdev);
>> @@ -493,6 +492,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
>>   		if (ret)
>>   			goto out_unregister_bdi;
>>   
>> +		set_bit(GD_NEED_PART_SCAN, &disk->state);
>>   		bdev_add(disk->part0, ddev->devt);
>>   		if (get_capacity(disk))
>>   			disk_scan_partitions(disk, FMODE_READ);
>> diff --git a/block/ioctl.c b/block/ioctl.c
>> index 6dd49d877584..0eefcdb936a0 100644
>> --- a/block/ioctl.c
>> +++ b/block/ioctl.c
>> @@ -528,6 +528,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
>>   			return -EACCES;
>>   		if (bdev_is_partition(bdev))
>>   			return -EINVAL;
>> +		set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
>>   		return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
>>   	case BLKTRACESTART:
>>   	case BLKTRACESTOP:
>> -- 
>> 2.31.1
>>
diff mbox series

Patch

diff --git a/block/genhd.c b/block/genhd.c
index 075d8da284f5..c0d1220bd798 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -367,7 +367,6 @@  int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
 	if (disk->open_partitions)
 		return -EBUSY;
 
-	set_bit(GD_NEED_PART_SCAN, &disk->state);
 	bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
 	if (IS_ERR(bdev))
 		return PTR_ERR(bdev);
@@ -493,6 +492,7 @@  int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 		if (ret)
 			goto out_unregister_bdi;
 
+		set_bit(GD_NEED_PART_SCAN, &disk->state);
 		bdev_add(disk->part0, ddev->devt);
 		if (get_capacity(disk))
 			disk_scan_partitions(disk, FMODE_READ);
diff --git a/block/ioctl.c b/block/ioctl.c
index 6dd49d877584..0eefcdb936a0 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -528,6 +528,7 @@  static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
 			return -EACCES;
 		if (bdev_is_partition(bdev))
 			return -EINVAL;
+		set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
 		return disk_scan_partitions(bdev->bd_disk, mode & ~FMODE_EXCL);
 	case BLKTRACESTART:
 	case BLKTRACESTOP: