Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4071197ybg; Fri, 25 Oct 2019 12:54:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqz28wECyiFb+rC03ld0BJdF8O5qlKV7rOsXhYPLotHlsXI94Sq3Oo+zcrqBK95SqnlQ2a+g X-Received: by 2002:a17:906:8288:: with SMTP id h8mr2297878ejx.251.1572033295076; Fri, 25 Oct 2019 12:54:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572033295; cv=none; d=google.com; s=arc-20160816; b=fpM3L34jqZJVg3pm3oGns3hnabnKQTITVy0VrOXGhuv9fqyUIgVgVWV2I8fWr4OK1g 6kpU1Ibs1NlWevnaDEk1wb6wZn5YBJftyo0NzOAwa858rC0M4ms2rNDOSISDIAV7TEkq hL7LQUfltgQcfoMxVMwpz/qnvFKcxEiDlsYGjumfquCN/ECsgvG8YR0eMWMeFO2awL4j 4Cx4C1FEWIL1EXOMeK3Gr/b7C86ge3MwrAZzWfbYxKlTskJnVcVI26EAKfl6t33Mz8FN 2B6mY7r+Cf1L8VxjYDV9QOpQAcKEusqqGX3DCiCjgHiVzCobLc1gqa1/tWalTRyd508t sYSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=wfSv4Ef83n92WiklwKDRwz1Gx6eTKzg7AlB+j09e5G0=; b=UVUrR8p9oFgTZn3Ruxzg9F8MMbTLYr3f1vAXD2zvPC63l+wtNnLLFwykxGNVvhHuDt 0RTM/gnR3xmaEw16n3bSvU63CmJ/7KU7LT1bQzdYkp6w7WLdzmfcaST/qMTTbkneAsHb OcaHXn2917K0AtsR3mnZyIRtwq5aibg3yQZtP6WJVC61t0QAXLV1daqms3WK95SdECau 4Ec6Br6tz/4GS2U7p2JqHY5xJPYCTpPr5h2P6CzIkW8TJivVhnfNW4q7cLZBTQ/SVqhp UVPtGAtu/UZk774K4M4PDfT9tCt/AX1IGwyIFBINihWzcZQ6xSMbV1ISeKfZEd/C9/uj pcvg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q45si2024606eda.202.2019.10.25.12.54.30; Fri, 25 Oct 2019 12:54:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2410332AbfJYLV7 (ORCPT + 99 others); Fri, 25 Oct 2019 07:21:59 -0400 Received: from mx2.suse.de ([195.135.220.15]:56914 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2410285AbfJYLV4 (ORCPT ); Fri, 25 Oct 2019 07:21:56 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 7F148B2B1; Fri, 25 Oct 2019 11:21:54 +0000 (UTC) From: Michal Suchanek To: linux-scsi@vger.kernel.org Cc: Michal Suchanek , Christoph Hellwig , Matthew Wilcox , "Ewan D. Milne" , Jonathan Corbet , Jens Axboe , "James E.J. Bottomley" , "Martin K. Petersen" , Alexander Viro , "J. Bruce Fields" , Mauro Carvalho Chehab , Eric Biggers , Benjamin Coddington , Hannes Reinecke , Omar Sandoval , Ming Lei , Damien Le Moal , Bart Van Assche , Tejun Heo , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v3 7/7] scsi: sr: wait for the medium to become ready Date: Fri, 25 Oct 2019 13:21:44 +0200 Message-Id: X-Mailer: git-send-email 2.23.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use the autoclose function provided by cdrom driver to wait for drive to close in open_finish, and attempt to open once more after the door closes. Signed-off-by: Michal Suchanek --- v3: use function call rather than IOCTL --- drivers/scsi/sr.c | 54 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c index 07c319494bf4..d144ad085b35 100644 --- a/drivers/scsi/sr.c +++ b/drivers/scsi/sr.c @@ -522,29 +522,58 @@ static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt) return ret; } -static int sr_block_open(struct block_device *bdev, fmode_t mode) +static int __sr_block_open(struct block_device *bdev, fmode_t mode) { - struct scsi_cd *cd; - struct scsi_device *sdev; - int ret = -ENXIO; - - cd = scsi_cd_get(bdev->bd_disk); - if (!cd) - goto out; + struct scsi_cd *cd = scsi_cd(bdev->bd_disk); + int ret; - sdev = cd->device; - scsi_autopm_get_device(sdev); check_disk_change(bdev); mutex_lock(&sr_mutex); ret = cdrom_open(&cd->cdi, bdev, mode); mutex_unlock(&sr_mutex); + return ret; +} + +static int sr_block_open(struct block_device *bdev, fmode_t mode) +{ + struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk); + struct scsi_device *sdev; + int ret; + + if (!cd) + return -ENXIO; + + sdev = cd->device; + scsi_autopm_get_device(sdev); + ret = __sr_block_open(bdev, mode); scsi_autopm_put_device(sdev); - if (ret) + + if (ret == -ERESTARTSYS) scsi_cd_put(cd); -out: + return ret; +} + +static int sr_block_open_finish(struct block_device *bdev, fmode_t mode, + int ret) +{ + struct scsi_cd *cd = scsi_cd(bdev->bd_disk); + + /* wait for drive to get ready */ + if ((ret == -ENOMEDIUM) && !(mode & FMODE_NDELAY)) { + struct scsi_device *sdev = cd->device; + /* + * Cannot use sr_block_ioctl because it locks sr_mutex blocking + * out any processes trying to access the drive + */ + scsi_autopm_get_device(sdev); + cdrom_autoclose(&cd->cdi); + ret = __sr_block_open(bdev, mode); + scsi_autopm_put_device(sdev); + } + return ret; } @@ -640,6 +669,7 @@ static const struct block_device_operations sr_bdops = { .owner = THIS_MODULE, .open = sr_block_open, + .open_finish = sr_block_open_finish, .release = sr_block_release, .ioctl = sr_block_ioctl, .check_events = sr_block_check_events, -- 2.23.0