diff mbox series

[v2,2/2] sr: fix automatic tray loading for data reading

Message ID 20201006094026.1730-3-scdbackup@gmx.net (mailing list archive)
State New, archived
Headers show
Series Fix automatic CD tray loading for data reading via sr | expand

Commit Message

Thomas Schmitt Oct. 6, 2020, 9:40 a.m. UTC
If
  open("/dev/sr0", O_RDONLY);
pulls in the tray of an optical drive, it immediately returns -1 with
errno ENOMEDIUM or the first read(2) fails with EIO. Later, when the drive
has stopped blinking, another open() yields success and read() works.
This affects not only userland reading of the device file but also
mounting the device.
The reason is that medium assessment happens before automatic tray
loading.

Use the new function cdrom_handle_open_tray() for deciding and performing
tray loading before doing medium assessment.

Signed-off-by: Thomas Schmitt <scdbackup@gmx.net>
---
 drivers/scsi/sr.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

--
2.20.1
diff mbox series

Patch

diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
index 2b43c0f97442..1c3806f59865 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -540,12 +540,18 @@  static int sr_block_open(struct block_device *bdev, fmode_t mode)

 	sdev = cd->device;
 	scsi_autopm_get_device(sdev);
-	if (bdev_check_media_change(bdev))
-		sr_revalidate_disk(cd);

 	mutex_lock(&cd->lock);
-	ret = cdrom_open(&cd->cdi, bdev, mode);
+	ret = cdrom_handle_open_tray(&cd->cdi, mode, 0);
 	mutex_unlock(&cd->lock);
+	if (!ret) {
+		if (bdev_check_media_change(bdev))
+			sr_revalidate_disk(cd);
+
+		mutex_lock(&cd->lock);
+		ret = cdrom_open(&cd->cdi, bdev, mode);
+		mutex_unlock(&cd->lock);
+	}

 	scsi_autopm_put_device(sdev);
 	if (ret)