From: Babu Moger <babu.moger@netapp.com>
Date: Fri, 21 Oct 2011 14:43:02 +0200
Subject: Check for invalid sdev in scsi_prep_fn()
Patch-Mainline: n/a
References: bnc#722569

When a device is torn down in the middle of I/O processing
the sdev might become invalid. So we need to check for this
in scsi_prep_fn().
This issue has been discussed at
https://lkml.org/lkml/2011/7/20/60
but no real resolution has been found.

Signed-off-by: Babu Moger <babu.moger@netapp.com>
Signed-off-by: Hannes Reinecke <hare@suse.de>

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index a96fe54..a65575d 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -1250,6 +1250,9 @@ int scsi_prep_fn(struct request_queue *q, struct request *req)
 	struct scsi_device *sdev = q->queuedata;
 	int ret = BLKPREP_KILL;
 
+	if (!sdev)
+		return ret;
+
 	if (req->cmd_type == REQ_TYPE_BLOCK_PC)
 		ret = scsi_setup_blk_pc_cmnd(sdev, req);
 	return scsi_prep_return(q, req, ret);
