linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] block: check disk exist before trying to add partition
@ 2021-06-10  2:32 Yufen Yu
  2021-06-29  8:47 ` Yufen Yu
  2021-07-01  1:39 ` Jens Axboe
  0 siblings, 2 replies; 3+ messages in thread
From: Yufen Yu @ 2021-06-10  2:32 UTC (permalink / raw)
  To: axboe; +Cc: linux-block, Yufen Yu, Jan Kara, Christoph Hellwig

If disk have been deleted, we should return fail for ioctl
BLKPG_DEL_PARTITION. Otherwise, the directory /sys/class/block
may remain invalid symlinks file. The race as following:

blkdev_open
				del_gendisk
				    disk->flags &= ~GENHD_FL_UP;
				    blk_drop_partitions
blkpg_ioctl
    bdev_add_partition
    add_partition
        device_add
	    device_add_class_symlinks

ioctl may add_partition after del_gendisk() have tried to delete
partitions. Then, symlinks file will be created.

Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Yufen Yu <yuyufen@huawei.com>
---
 block/partitions/core.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/block/partitions/core.c b/block/partitions/core.c
index 186d4fbd9f09..b123ccb2cf64 100644
--- a/block/partitions/core.c
+++ b/block/partitions/core.c
@@ -454,17 +454,26 @@ int bdev_add_partition(struct block_device *bdev, int partno,
 		sector_t start, sector_t length)
 {
 	struct block_device *part;
+	struct gendisk *disk = bdev->bd_disk;
+	int ret;
 
-	mutex_lock(&bdev->bd_disk->open_mutex);
-	if (partition_overlaps(bdev->bd_disk, start, length, -1)) {
-		mutex_unlock(&bdev->bd_disk->open_mutex);
-		return -EBUSY;
+	mutex_lock(&disk->open_mutex);
+	if (!(disk->flags & GENHD_FL_UP)) {
+		ret = -ENXIO;
+		goto out;
+	}
+
+	if (partition_overlaps(disk, start, length, -1)) {
+		ret = -EBUSY;
+		goto out;
 	}
 
-	part = add_partition(bdev->bd_disk, partno, start, length,
+	part = add_partition(disk, partno, start, length,
 			ADDPART_FLAG_NONE, NULL);
-	mutex_unlock(&bdev->bd_disk->open_mutex);
-	return PTR_ERR_OR_ZERO(part);
+	ret = PTR_ERR_OR_ZERO(part);
+out:
+	mutex_unlock(&disk->open_mutex);
+	return ret;
 }
 
 int bdev_del_partition(struct block_device *bdev, int partno)
-- 
2.25.4


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

* Re: [PATCH v2] block: check disk exist before trying to add partition
  2021-06-10  2:32 [PATCH v2] block: check disk exist before trying to add partition Yufen Yu
@ 2021-06-29  8:47 ` Yufen Yu
  2021-07-01  1:39 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Yufen Yu @ 2021-06-29  8:47 UTC (permalink / raw)
  To: axboe; +Cc: linux-block, Jan Kara, Christoph Hellwig

Hi, Jens

This patch is based on for-5.14/block branch. Please consider to
apply it.

Thansk,
Yufen

On 2021/6/10 10:32, Yufen Yu wrote:
> If disk have been deleted, we should return fail for ioctl
> BLKPG_DEL_PARTITION. Otherwise, the directory /sys/class/block
> may remain invalid symlinks file. The race as following:
> 
> blkdev_open
> 				del_gendisk
> 				    disk->flags &= ~GENHD_FL_UP;
> 				    blk_drop_partitions
> blkpg_ioctl
>      bdev_add_partition
>      add_partition
>          device_add
> 	    device_add_class_symlinks
> 
> ioctl may add_partition after del_gendisk() have tried to delete
> partitions. Then, symlinks file will be created.
> 
> Reviewed-by: Jan Kara <jack@suse.cz>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> Signed-off-by: Yufen Yu <yuyufen@huawei.com>
> ---
>   block/partitions/core.c | 23 ++++++++++++++++-------
>   1 file changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/block/partitions/core.c b/block/partitions/core.c
> index 186d4fbd9f09..b123ccb2cf64 100644
> --- a/block/partitions/core.c
> +++ b/block/partitions/core.c
> @@ -454,17 +454,26 @@ int bdev_add_partition(struct block_device *bdev, int partno,
>   		sector_t start, sector_t length)
>   {
>   	struct block_device *part;
> +	struct gendisk *disk = bdev->bd_disk;
> +	int ret;
>   
> -	mutex_lock(&bdev->bd_disk->open_mutex);
> -	if (partition_overlaps(bdev->bd_disk, start, length, -1)) {
> -		mutex_unlock(&bdev->bd_disk->open_mutex);
> -		return -EBUSY;
> +	mutex_lock(&disk->open_mutex);
> +	if (!(disk->flags & GENHD_FL_UP)) {
> +		ret = -ENXIO;
> +		goto out;
> +	}
> +
> +	if (partition_overlaps(disk, start, length, -1)) {
> +		ret = -EBUSY;
> +		goto out;
>   	}
>   
> -	part = add_partition(bdev->bd_disk, partno, start, length,
> +	part = add_partition(disk, partno, start, length,
>   			ADDPART_FLAG_NONE, NULL);
> -	mutex_unlock(&bdev->bd_disk->open_mutex);
> -	return PTR_ERR_OR_ZERO(part);
> +	ret = PTR_ERR_OR_ZERO(part);
> +out:
> +	mutex_unlock(&disk->open_mutex);
> +	return ret;
>   }
>   
>   int bdev_del_partition(struct block_device *bdev, int partno)
> 

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

* Re: [PATCH v2] block: check disk exist before trying to add partition
  2021-06-10  2:32 [PATCH v2] block: check disk exist before trying to add partition Yufen Yu
  2021-06-29  8:47 ` Yufen Yu
@ 2021-07-01  1:39 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2021-07-01  1:39 UTC (permalink / raw)
  To: Yufen Yu; +Cc: linux-block, Jan Kara, Christoph Hellwig

On 6/9/21 8:32 PM, Yufen Yu wrote:
> If disk have been deleted, we should return fail for ioctl
> BLKPG_DEL_PARTITION. Otherwise, the directory /sys/class/block
> may remain invalid symlinks file. The race as following:
> 
> blkdev_open
> 				del_gendisk
> 				    disk->flags &= ~GENHD_FL_UP;
> 				    blk_drop_partitions
> blkpg_ioctl
>     bdev_add_partition
>     add_partition
>         device_add
> 	    device_add_class_symlinks
> 
> ioctl may add_partition after del_gendisk() have tried to delete
> partitions. Then, symlinks file will be created.

Applied, thanks.

-- 
Jens Axboe


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

end of thread, other threads:[~2021-07-01  1:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-10  2:32 [PATCH v2] block: check disk exist before trying to add partition Yufen Yu
2021-06-29  8:47 ` Yufen Yu
2021-07-01  1:39 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).