From patchwork Thu Sep 7 16:16:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Le Moal X-Patchwork-Id: 9942539 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2874B6038C for ; Thu, 7 Sep 2017 16:19:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 047DE286F2 for ; Thu, 7 Sep 2017 16:19:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED9F628722; Thu, 7 Sep 2017 16:19:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3CBBC2870D for ; Thu, 7 Sep 2017 16:19:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932349AbdIGQTS (ORCPT ); Thu, 7 Sep 2017 12:19:18 -0400 Received: from esa2.hgst.iphmx.com ([68.232.143.124]:20811 "EHLO esa2.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932303AbdIGQTR (ORCPT ); Thu, 7 Sep 2017 12:19:17 -0400 X-IronPort-AV: E=Sophos;i="5.42,359,1500912000"; d="scan'208";a="145340052" Received: from sjappemgw12.hgst.com (HELO sjappemgw11.hgst.com) ([199.255.44.66]) by ob1.hgst.iphmx.com with ESMTP; 08 Sep 2017 00:22:13 +0800 Received: from washi.fujisawa.hgst.com ([10.149.53.254]) by sjappemgw11.hgst.com with ESMTP; 07 Sep 2017 09:17:01 -0700 From: Damien Le Moal To: linux-scsi@vger.kernel.org, "Martin K . Petersen" , linux-block@vger.kernel.org, Jens Axboe Cc: Christoph Hellwig , Bart Van Assche Subject: [PATCH V2 11/12] scsi: sd: Introduce scsi_disk_from_queue() Date: Fri, 8 Sep 2017 01:16:39 +0900 Message-Id: <20170907161640.30465-12-damien.lemoal@wdc.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20170907161640.30465-1-damien.lemoal@wdc.com> References: <20170907161640.30465-1-damien.lemoal@wdc.com> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Using scsi_device_from_queue(), return the scsi_disk structure associated with a request queue if the device is a disk. Export this function to make it available to modules. Signed-off-by: Damien Le Moal --- drivers/scsi/sd.c | 32 ++++++++++++++++++++++++++++++++ drivers/scsi/sd.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 1cd113df2d3a..ff9fff4de3e6 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -631,6 +631,38 @@ static void scsi_disk_put(struct scsi_disk *sdkp) mutex_unlock(&sd_ref_mutex); } +static int scsi_disk_lookup(struct device *dev, void *data) +{ + struct scsi_disk **sdkp = data; + + if (!*sdkp && dev->class == &sd_disk_class) + *sdkp = to_scsi_disk(dev); + + return 0; +} + +/** + * scsi_disk_from_queue - return scsi disk associated with a request_queue + * @q: The request queue to return the scsi disk from + * + * Return the struct scsi_disk associated with a request queue or NULL if the + * request_queue does not reference a SCSI device or a if the device is + * not a disk. + */ +struct scsi_disk *scsi_disk_from_queue(struct request_queue *q) +{ + struct scsi_device *sdev = scsi_device_from_queue(q); + struct scsi_disk *sdkp = NULL; + + if (!sdev) + return NULL; + + device_for_each_child(&sdev->sdev_gendev, &sdkp, scsi_disk_lookup); + + return sdkp; +} +EXPORT_SYMBOL_GPL(scsi_disk_from_queue); + #ifdef CONFIG_BLK_SED_OPAL static int sd_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len, bool send) diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h index 3ea82c8cecd5..92113a9e2b20 100644 --- a/drivers/scsi/sd.h +++ b/drivers/scsi/sd.h @@ -126,6 +126,8 @@ static inline struct scsi_disk *scsi_disk(struct gendisk *disk) return container_of(disk->private_data, struct scsi_disk, driver); } +struct scsi_disk *scsi_disk_from_queue(struct request_queue *q); + #define sd_printk(prefix, sdsk, fmt, a...) \ (sdsk)->disk ? \ sdev_prefix_printk(prefix, (sdsk)->device, \