linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: linux-scsi@vger.kernel.org
Cc: Hannes Reinecke <hare@suse.de>, Mike Snitzer <snitzer@redhat.com>,
	Mike Christie <michaelc@cs.wisc.edu>,
	"Martin K. Petersen" <martin.petersen@oracle.com>
Subject: [PATCH 8/9] scsi_dh: add a common helper to get a scsi_device from a request_queue
Date: Thu, 30 Apr 2015 19:32:30 +0200	[thread overview]
Message-ID: <1430415151-30948-9-git-send-email-hch@lst.de> (raw)
In-Reply-To: <1430415151-30948-1-git-send-email-hch@lst.de>

And cleanup the various messy opencoded versions of this.  Note that this
moves the sdev_state checks outside the queue_lock coverage, but as
we don't hold the lock over the activation they are only advisory anyway.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/scsi/device_handler/scsi_dh.c | 100 ++++++++++++++++------------------
 1 file changed, 47 insertions(+), 53 deletions(-)

diff --git a/drivers/scsi/device_handler/scsi_dh.c b/drivers/scsi/device_handler/scsi_dh.c
index ddc5b9c..0d6ab33 100644
--- a/drivers/scsi/device_handler/scsi_dh.c
+++ b/drivers/scsi/device_handler/scsi_dh.c
@@ -281,6 +281,20 @@ int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
 }
 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
 
+static struct scsi_device *get_sdev_from_queue(struct request_queue *q)
+{
+	struct scsi_device *sdev;
+	unsigned long flags;
+
+	spin_lock_irqsave(q->queue_lock, flags);
+	sdev = q->queuedata;
+	if (!sdev || !get_device(&sdev->sdev_gendev))
+		sdev = NULL;
+	spin_unlock_irqrestore(q->queue_lock, flags);
+
+	return sdev;
+}
+
 /*
  * scsi_dh_activate - activate the path associated with the scsi_device
  *      corresponding to the given request queue.
@@ -296,41 +310,38 @@ EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  */
 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
 {
-	int err = 0;
-	unsigned long flags;
 	struct scsi_device *sdev;
-	struct device *dev = NULL;
+	int err = SCSI_DH_NOSYS;
 
-	spin_lock_irqsave(q->queue_lock, flags);
-	sdev = q->queuedata;
+	sdev = get_sdev_from_queue(q);
 	if (!sdev) {
-		spin_unlock_irqrestore(q->queue_lock, flags);
-		err = SCSI_DH_NOSYS;
 		if (fn)
 			fn(data, err);
 		return err;
 	}
 
-	dev = get_device(&sdev->sdev_gendev);
-	if (!sdev->handler || !dev ||
-	    sdev->sdev_state == SDEV_CANCEL ||
+	if (!sdev->handler)
+		goto out_fn;
+	if (sdev->sdev_state == SDEV_CANCEL ||
 	    sdev->sdev_state == SDEV_DEL)
-		err = SCSI_DH_NOSYS;
-	if (sdev->sdev_state == SDEV_OFFLINE)
-		err = SCSI_DH_DEV_OFFLINED;
-	spin_unlock_irqrestore(q->queue_lock, flags);
+	    	goto out_fn;
 
-	if (err) {
-		if (fn)
-			fn(data, err);
-		goto out;
-	}
+	err = SCSI_DH_DEV_OFFLINED;
+	if (sdev->sdev_state == SDEV_OFFLINE)
+		goto out_fn;
 
 	if (sdev->handler->activate)
 		err = sdev->handler->activate(sdev, fn, data);
-out:
-	put_device(dev);
+
+out_put_device:
+	put_device(&sdev->sdev_gendev);
 	return err;
+
+out_fn:
+	if (fn)
+		fn(data, err);
+	goto out_put_device;
+	
 }
 EXPORT_SYMBOL_GPL(scsi_dh_activate);
 
@@ -346,21 +357,15 @@ EXPORT_SYMBOL_GPL(scsi_dh_activate);
  */
 int scsi_dh_set_params(struct request_queue *q, const char *params)
 {
-	int err = -SCSI_DH_NOSYS;
-	unsigned long flags;
 	struct scsi_device *sdev;
+	int err = -SCSI_DH_NOSYS;
 
-	spin_lock_irqsave(q->queue_lock, flags);
-	sdev = q->queuedata;
-	if (sdev->handler &&
-	    sdev->handler->set_params &&
-	    get_device(&sdev->sdev_gendev))
-		err = 0;
-	spin_unlock_irqrestore(q->queue_lock, flags);
-
-	if (err)
+	sdev = get_sdev_from_queue(q);
+	if (!sdev)
 		return err;
-	err = sdev->handler->set_params(sdev, params);
+
+	if (sdev->handler && sdev->handler->set_params)
+		err = sdev->handler->set_params(sdev, params);
 	put_device(&sdev->sdev_gendev);
 	return err;
 }
@@ -374,23 +379,19 @@ EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  */
 int scsi_dh_attach(struct request_queue *q, const char *name)
 {
-	unsigned long flags;
 	struct scsi_device *sdev;
 	struct scsi_device_handler *scsi_dh;
 	int err = 0;
 
-	scsi_dh = scsi_dh_lookup(name);
-	if (!scsi_dh)
-		return -EINVAL;
-
-	spin_lock_irqsave(q->queue_lock, flags);
-	sdev = q->queuedata;
-	if (!sdev || !get_device(&sdev->sdev_gendev))
-		err = -ENODEV;
-	spin_unlock_irqrestore(q->queue_lock, flags);
+	sdev = get_sdev_from_queue(q);
+	if (!sdev)
+		return -ENODEV;
 
-	if (err)
-		return err;
+	scsi_dh = scsi_dh_lookup(name);
+	if (!scsi_dh) {
+		err = -EINVAL;
+		goto out_put_device;
+	}
 
 	if (sdev->handler) {
 		if (sdev->handler == scsi_dh)
@@ -422,22 +423,15 @@ EXPORT_SYMBOL_GPL(scsi_dh_attach);
  */
 const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
 {
-	unsigned long flags;
 	struct scsi_device *sdev;
 	const char *handler_name = NULL;
 
-	spin_lock_irqsave(q->queue_lock, flags);
-	sdev = q->queuedata;
-	if (!sdev || !get_device(&sdev->sdev_gendev))
-		sdev = NULL;
-	spin_unlock_irqrestore(q->queue_lock, flags);
-
+	sdev = get_sdev_from_queue(q);
 	if (!sdev)
 		return NULL;
 
 	if (sdev->handler)
 		handler_name = kstrdup(sdev->handler->name, gfp);
-
 	put_device(&sdev->sdev_gendev);
 	return handler_name;
 }
-- 
1.9.1


  parent reply	other threads:[~2015-04-30 17:34 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-30 17:32 integrate scsi_dh better into the scsi core Christoph Hellwig
2015-04-30 17:32 ` [PATCH 1/9] dm-mpath: check kstrdup return value in parse_hw_handler Christoph Hellwig
2015-04-30 18:35   ` Hannes Reinecke
2015-05-01 13:57   ` Mike Snitzer
2015-05-01 15:07   ` Martin K. Petersen
2015-05-01 15:29   ` Martin K. Petersen
2015-04-30 17:32 ` [PATCH 2/9] dm-mpath, scsi_dh: don't let dm detach device handlers Christoph Hellwig
2015-04-30 18:21   ` Mike Snitzer
2015-05-01  9:20     ` Christoph Hellwig
2015-05-01  7:10   ` Hannes Reinecke
2015-05-01 13:58   ` Mike Snitzer
2015-05-01 15:30   ` Martin K. Petersen
2015-04-30 17:32 ` [PATCH 3/9] dm-mpath: don't call scsi_dh_attach when we want to retain the attached handler Christoph Hellwig
2015-04-30 18:25   ` Mike Snitzer
2015-05-01  7:10   ` Hannes Reinecke
2015-05-01 15:34   ` Martin K. Petersen
2015-05-01 16:46     ` Christoph Hellwig
2015-04-30 17:32 ` [PATCH 4/9] dm-mpath, scsi_dh: request scsi_dh modules in scsi_dh, not dm-mpath Christoph Hellwig
2015-04-30 18:28   ` Mike Snitzer
2015-05-01  7:11   ` Hannes Reinecke
2015-05-01 13:59   ` Mike Snitzer
2015-05-01 15:35   ` Martin K. Petersen
2015-04-30 17:32 ` [PATCH 5/9] scsi_dh: integrate into the core SCSI code Christoph Hellwig
2015-05-01  7:13   ` Hannes Reinecke
2015-05-01 15:36   ` Martin K. Petersen
2015-05-04  7:50   ` Hannes Reinecke
2015-05-05 15:25     ` Christoph Hellwig
2015-04-30 17:32 ` [PATCH 6/9] scsi_dh: move device matching to the core code Christoph Hellwig
2015-04-30 18:32   ` Mike Snitzer
2015-05-01  7:15   ` Hannes Reinecke
2015-05-01 15:39   ` Martin K. Petersen
2015-04-30 17:32 ` [PATCH 7/9] scsi_dh: kill struct scsi_dh_data Christoph Hellwig
2015-05-01  7:17   ` Hannes Reinecke
2015-05-01 15:41   ` Martin K. Petersen
2015-04-30 17:32 ` Christoph Hellwig [this message]
2015-05-01  7:18   ` [PATCH 8/9] scsi_dh: add a common helper to get a scsi_device from a request_queue Hannes Reinecke
2015-05-01 15:45   ` Martin K. Petersen
2015-04-30 17:32 ` [PATCH 9/9] scsi_dh: don't allow to detach device handlers at runtime Christoph Hellwig
2015-05-01  7:19   ` Hannes Reinecke
2015-05-01 15:46   ` Martin K. Petersen
2015-04-30 18:31 ` integrate scsi_dh better into the scsi core Mike Snitzer
2015-05-01 15:47 ` Martin K. Petersen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1430415151-30948-9-git-send-email-hch@lst.de \
    --to=hch@lst.de \
    --cc=hare@suse.de \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=michaelc@cs.wisc.edu \
    --cc=snitzer@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).