Skip to contentSkip to conversation

block: reopen the device in blkdev_reread_part

PATCH Applied Christoph Hellwig 4 participants 7 messages Feb 23, 2021 – Feb 24, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: reopen the device in blkdev_reread_part

Historically the BLKRRPART ioctls called into the now defunct ->revalidate method, which caused the sd driver to check if any media is present. When the ->revalidate method was removed this revalidation was lost, leading to lots of I/O errors when using the eject command. Fix this by reopening the device to rescan the partitions, and thus calling the revalidation logic in the sd driver.

Fixes 471bd0af544b ("sd: use bdev_check_media_change")
---
 block/ioctl.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
block/ioctl.c +14 -7
index d61d652078f41c..ff241e663c018f 100644
@@ -81,20 +81,27 @@ static int compat_blkpg_ioctl(struct block_device *bdev,
 }
 #endif
 
-static int blkdev_reread_part(struct block_device *bdev)
+static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
 {
-	int ret;
+	struct block_device *tmp;
 
 	if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
 		return -EINVAL;
 	if (!capable(CAP_SYS_ADMIN))
 		return -EACCES;
 
-	mutex_lock(&bdev->bd_mutex);
-	ret = bdev_disk_changed(bdev, false);
-	mutex_unlock(&bdev->bd_mutex);
+	/*
+	 * Reopen the device to revalidate the driver state and force a
+	 * partition rescan.
+	 */
+	mode &= ~FMODE_EXCL;
+	set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
 
-	return ret;
+	tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
+	if (IS_ERR(tmp))
+		return PTR_ERR(tmp);
+	blkdev_put(tmp, mode);
+	return 0;
 }
 
 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
@@ -498,7 +505,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
 		bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
 		return 0;
 	case BLKRRPART:
-		return blkdev_reread_part(bdev);
+		return blkdev_reread_part(bdev, mode);
 	case BLKTRACESTART:
 	case BLKTRACESTOP:
 	case BLKTRACETEARDOWN:
Signature · 1 line
2.29.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 65 lines · Christoph Hellwig

Historically the BLKRRPART ioctls called into the now defunct ->revalidate method, which caused the sd driver to check if any media is present. When the ->revalidate method was removed this revalidation was lost, leading to lots of I/O errors when using the eject command. Fix this by reopening the device to rescan the partitions, and thus calling the revalidation logic in the sd driver.

Fixes 471bd0af544b ("sd: use bdev_check_media_change")
---
 block/ioctl.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
block/ioctl.c +14 -7
index d61d652078f41c..ff241e663c018f 100644
@@ -81,20 +81,27 @@ static int compat_blkpg_ioctl(struct block_device *bdev,
 }
 #endif
 
-static int blkdev_reread_part(struct block_device *bdev)
+static int blkdev_reread_part(struct block_device *bdev, fmode_t mode)
 {
-	int ret;
+	struct block_device *tmp;
 
 	if (!disk_part_scan_enabled(bdev->bd_disk) || bdev_is_partition(bdev))
 		return -EINVAL;
 	if (!capable(CAP_SYS_ADMIN))
 		return -EACCES;
 
-	mutex_lock(&bdev->bd_mutex);
-	ret = bdev_disk_changed(bdev, false);
-	mutex_unlock(&bdev->bd_mutex);
+	/*
+	 * Reopen the device to revalidate the driver state and force a
+	 * partition rescan.
+	 */
+	mode &= ~FMODE_EXCL;
+	set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
 
-	return ret;
+	tmp = blkdev_get_by_dev(bdev->bd_dev, mode, NULL);
+	if (IS_ERR(tmp))
+		return PTR_ERR(tmp);
+	blkdev_put(tmp, mode);
+	return 0;
 }
 
 static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
@@ -498,7 +505,7 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode,
 		bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
 		return 0;
 	case BLKRRPART:
-		return blkdev_reread_part(bdev);
+		return blkdev_reread_part(bdev, mode);
 	case BLKTRACESTART:
 	case BLKTRACESTOP:
 	case BLKTRACETEARDOWN:
Signature · 1 line
2.29.2

Looks fine,

Signature · 1 line
Ming
Reviewed-by on v1 1/1
#2 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Historically the BLKRRPART ioctls called into the now defunct ->revalidate method, which caused the sd driver to check if any media is present. When the ->revalidate method was removed this revalidation was lost, leading to lots of I/O errors when using the eject command. Fix this by reopening the device to rescan the partitions, and thus calling the revalidation logic in the sd driver.

It looks like a related issue that I've reported in [1]. And this looks much better!

[1] https://lore.kernel.org/linux-block/[email protected]/

#3 lore raw
Quoted 10 lines · Minwoo Im
Quoted 6 lines · Christoph Hellwig

Historically the BLKRRPART ioctls called into the now defunct ->revalidate method, which caused the sd driver to check if any media is present. When the ->revalidate method was removed this revalidation was lost, leading to lots of I/O errors when using the eject command. Fix this by reopening the device to rescan the partitions, and thus calling the revalidation logic in the sd driver.

It looks like a related issue that I've reported in [1]. And this looks much better!

I don't think it fixes the block size issue, does it?

#4 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 8 lines · Minwoo Im
Quoted 6 lines · Christoph Hellwig

Historically the BLKRRPART ioctls called into the now defunct ->revalidate method, which caused the sd driver to check if any media is present. When the ->revalidate method was removed this revalidation was lost, leading to lots of I/O errors when using the eject command. Fix this by reopening the device to rescan the partitions, and thus calling the revalidation logic in the sd driver.

Quoted 4 lines
Quoted 2 lines

It looks like a related issue that I've reported in [1]. And this looks much better!

I don't think it fixes the block size issue, does it?

Uh... Sorry for the noise. This reopen is not the first shot so that the block size will not be updated because bd_openers is not 0. set_init_blocksize is not being invoked.

Sorry please ignore this noise.

#5 lore raw
Quoted 13 lines · Minwoo Im
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Minwoo Im
Quoted 6 lines · Christoph Hellwig

Historically the BLKRRPART ioctls called into the now defunct ->revalidate method, which caused the sd driver to check if any media is present. When the ->revalidate method was removed this revalidation was lost, leading to lots of I/O errors when using the eject command. Fix this by reopening the device to rescan the partitions, and thus calling the revalidation logic in the sd driver.

It looks like a related issue that I've reported in [1]. And this looks much better!

Quoted 7 lines
Quoted 1 line

I don't think it fixes the block size issue, does it?

Uh... Sorry for the noise. This reopen is not the first shot so that the block size will not be updated because bd_openers is not 0. set_init_blocksize is not being invoked.

Sorry please ignore this noise.

FYI,

the block size issue has no issue based on the current tree with 8dc932d3e8af ("Revert "block: simplify set_init_blocksize" to regain lost performance")

maintainer Applied
#6 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Historically the BLKRRPART ioctls called into the now defunct ->revalidate method, which caused the sd driver to check if any media is present. When the ->revalidate method was removed this revalidation was lost, leading to lots of I/O errors when using the eject command. Fix this by reopening the device to rescan the partitions, and thus calling the revalidation logic in the sd driver.

Applied, thanks.

Signature · 1 line
Jens Axboe