diff mbox series

[-next,RFC,3/3] block: fix scan partition for exclusively open device again

Message ID 20230212092641.2394146-4-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>

As explained in commit 36369f46e917 ("block: Do not reread partition table
on exclusively open device"), reread partition on the device that is
exclusively opened by someone else is problematic.

This patch will make sure partition scan will only be proceed if current
thread open the device exclusively, or the device is not opened
exclusively, and in the later case, other scanners and exclusive openers
will be blocked temporarily until partition scan is done.

Fixes: 10c70d95c0f2 ("block: remove the bd_openers checks in blk_drop_partitions")
Cc: <stable@vger.kernel.org>
Suggested-by: Jan Kara <jack@suse.cz>
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 block/genhd.c | 27 +++++++++++++++++++++++----
 block/ioctl.c |  2 +-
 2 files changed, 24 insertions(+), 5 deletions(-)

Comments

Jan Kara Feb. 16, 2023, 1:19 p.m. UTC | #1
On Sun 12-02-23 17:26:41, Yu Kuai wrote:
> From: Yu Kuai <yukuai3@huawei.com>
> 
> As explained in commit 36369f46e917 ("block: Do not reread partition table
> on exclusively open device"), reread partition on the device that is
> exclusively opened by someone else is problematic.
> 
> This patch will make sure partition scan will only be proceed if current
> thread open the device exclusively, or the device is not opened
> exclusively, and in the later case, other scanners and exclusive openers
> will be blocked temporarily until partition scan is done.
> 
> Fixes: 10c70d95c0f2 ("block: remove the bd_openers checks in blk_drop_partitions")
> Cc: <stable@vger.kernel.org>
> Suggested-by: Jan Kara <jack@suse.cz>
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>

Just one nit below.

> diff --git a/block/genhd.c b/block/genhd.c
> index c0d1220bd798..6ec10ffeb9cc 100644
> --- a/block/genhd.c
> +++ b/block/genhd.c
> @@ -359,6 +359,7 @@ EXPORT_SYMBOL_GPL(disk_uevent);
>  int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
>  {
>  	struct block_device *bdev;
> +	int ret = 0;
>  
>  	if (disk->flags & (GENHD_FL_NO_PART | GENHD_FL_HIDDEN))
>  		return -EINVAL;
> @@ -367,11 +368,29 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
>  	if (disk->open_partitions)
>  		return -EBUSY;
>  
> -	bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
> -	if (IS_ERR(bdev))
> -		return PTR_ERR(bdev);
> +	/*
> +	 * If the device is opened exclusively by current thread already, it's
> +	 * safe to scan partitons, otherwise, use bd_prepare_to_claim() to
> +	 * synchronize with other exclusivet openers and other partition
				  ^^^ typo here

								Honza
diff mbox series

Patch

diff --git a/block/genhd.c b/block/genhd.c
index c0d1220bd798..6ec10ffeb9cc 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -359,6 +359,7 @@  EXPORT_SYMBOL_GPL(disk_uevent);
 int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
 {
 	struct block_device *bdev;
+	int ret = 0;
 
 	if (disk->flags & (GENHD_FL_NO_PART | GENHD_FL_HIDDEN))
 		return -EINVAL;
@@ -367,11 +368,29 @@  int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
 	if (disk->open_partitions)
 		return -EBUSY;
 
-	bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
-	if (IS_ERR(bdev))
-		return PTR_ERR(bdev);
+	/*
+	 * If the device is opened exclusively by current thread already, it's
+	 * safe to scan partitons, otherwise, use bd_prepare_to_claim() to
+	 * synchronize with other exclusivet openers and other partition
+	 * scanners.
+	 */
+	if (!(mode & FMODE_EXCL)) {
+		ret = bd_prepare_to_claim(disk->part0, disk_scan_partitions);
+		if (ret)
+			return ret;
+	}
+
+	bdev = blkdev_get_by_dev(disk_devt(disk), mode & ~FMODE_EXCL, NULL);
+	if (IS_ERR(bdev)) {
+		ret =  PTR_ERR(bdev);
+		goto out;
+	}
 	blkdev_put(bdev, mode);
-	return 0;
+
+out:
+	if (!(mode & FMODE_EXCL))
+		bd_abort_claiming(disk->part0, disk_scan_partitions);
+	return ret;
 }
 
 /**
diff --git a/block/ioctl.c b/block/ioctl.c
index 0eefcdb936a0..3adfdb904dd0 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -529,7 +529,7 @@  static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
 		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);
+		return disk_scan_partitions(bdev->bd_disk, mode);
 	case BLKTRACESTART:
 	case BLKTRACESTOP:
 	case BLKTRACETEARDOWN: