[SCSI] untangle scsi_prep_fn
I wanted to add some BUG checks to scsi_prep_fn to make sure no one sends us a non-sg command, but this function is a horrible mess. So I decided to detangle the function and document what the valid cases are. While doing that I found that REQ_TYPE_SPECIAL commands aren't used by the SCSI layer anymore and we can get rid of the code handling them. The new structure of scsi_prep_fn is: (1) check if we're allowed to send this command (2) big switch on cmd_type. For the two valid types call into a function to set the command up, else error (3) code to handle error cases Because FS and BLOCK_PC commands are handled entirely separate after the patch this introduces a tiny amount of code duplication. This improves readabiulity though and will help to avoid the bidi command overhead for FS commands so it's a good thing. I've tested this on both sata and mptsas. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>
This commit is contained in:
committed by
James Bottomley
parent
2dc611de5a
commit
3b00315799
@@ -995,25 +995,14 @@ static int scsi_init_io(struct scsi_cmnd *cmd)
|
|||||||
int count;
|
int count;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
|
* We used to not use scatter-gather for single segment request,
|
||||||
*/
|
|
||||||
if (blk_pc_request(req) && !req->bio) {
|
|
||||||
cmd->request_bufflen = req->data_len;
|
|
||||||
cmd->request_buffer = req->data;
|
|
||||||
req->buffer = req->data;
|
|
||||||
cmd->use_sg = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* we used to not use scatter-gather for single segment request,
|
|
||||||
* but now we do (it makes highmem I/O easier to support without
|
* but now we do (it makes highmem I/O easier to support without
|
||||||
* kmapping pages)
|
* kmapping pages)
|
||||||
*/
|
*/
|
||||||
cmd->use_sg = req->nr_phys_segments;
|
cmd->use_sg = req->nr_phys_segments;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* if sg table allocation fails, requeue request later.
|
* If sg table allocation fails, requeue request later.
|
||||||
*/
|
*/
|
||||||
sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
|
sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
|
||||||
if (unlikely(!sgpnt)) {
|
if (unlikely(!sgpnt)) {
|
||||||
@@ -1021,24 +1010,21 @@ static int scsi_init_io(struct scsi_cmnd *cmd)
|
|||||||
return BLKPREP_DEFER;
|
return BLKPREP_DEFER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req->buffer = NULL;
|
||||||
cmd->request_buffer = (char *) sgpnt;
|
cmd->request_buffer = (char *) sgpnt;
|
||||||
cmd->request_bufflen = req->nr_sectors << 9;
|
|
||||||
if (blk_pc_request(req))
|
if (blk_pc_request(req))
|
||||||
cmd->request_bufflen = req->data_len;
|
cmd->request_bufflen = req->data_len;
|
||||||
req->buffer = NULL;
|
else
|
||||||
|
cmd->request_bufflen = req->nr_sectors << 9;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Next, walk the list, and fill in the addresses and sizes of
|
* Next, walk the list, and fill in the addresses and sizes of
|
||||||
* each segment.
|
* each segment.
|
||||||
*/
|
*/
|
||||||
count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
|
count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
|
||||||
|
|
||||||
/*
|
|
||||||
* mapped well, send it off
|
|
||||||
*/
|
|
||||||
if (likely(count <= cmd->use_sg)) {
|
if (likely(count <= cmd->use_sg)) {
|
||||||
cmd->use_sg = count;
|
cmd->use_sg = count;
|
||||||
return 0;
|
return BLKPREP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
printk(KERN_ERR "Incorrect number of segments after building list\n");
|
printk(KERN_ERR "Incorrect number of segments after building list\n");
|
||||||
@@ -1068,6 +1054,27 @@ static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
|
|||||||
return -EOPNOTSUPP;
|
return -EOPNOTSUPP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
|
||||||
|
struct request *req)
|
||||||
|
{
|
||||||
|
struct scsi_cmnd *cmd;
|
||||||
|
|
||||||
|
if (!req->special) {
|
||||||
|
cmd = scsi_get_command(sdev, GFP_ATOMIC);
|
||||||
|
if (unlikely(!cmd))
|
||||||
|
return NULL;
|
||||||
|
req->special = cmd;
|
||||||
|
} else {
|
||||||
|
cmd = req->special;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* pull a tag out of the request if we have one */
|
||||||
|
cmd->tag = req->tag;
|
||||||
|
cmd->request = req;
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
static void scsi_blk_pc_done(struct scsi_cmnd *cmd)
|
static void scsi_blk_pc_done(struct scsi_cmnd *cmd)
|
||||||
{
|
{
|
||||||
BUG_ON(!blk_pc_request(cmd->request));
|
BUG_ON(!blk_pc_request(cmd->request));
|
||||||
@@ -1080,9 +1087,37 @@ static void scsi_blk_pc_done(struct scsi_cmnd *cmd)
|
|||||||
scsi_io_completion(cmd, cmd->request_bufflen);
|
scsi_io_completion(cmd, cmd->request_bufflen);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void scsi_setup_blk_pc_cmnd(struct scsi_cmnd *cmd)
|
static int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
|
||||||
{
|
{
|
||||||
struct request *req = cmd->request;
|
struct scsi_cmnd *cmd;
|
||||||
|
|
||||||
|
cmd = scsi_get_cmd_from_req(sdev, req);
|
||||||
|
if (unlikely(!cmd))
|
||||||
|
return BLKPREP_DEFER;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* BLOCK_PC requests may transfer data, in which case they must
|
||||||
|
* a bio attached to them. Or they might contain a SCSI command
|
||||||
|
* that does not transfer data, in which case they may optionally
|
||||||
|
* submit a request without an attached bio.
|
||||||
|
*/
|
||||||
|
if (req->bio) {
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
BUG_ON(!req->nr_phys_segments);
|
||||||
|
|
||||||
|
ret = scsi_init_io(cmd);
|
||||||
|
if (unlikely(ret))
|
||||||
|
return ret;
|
||||||
|
} else {
|
||||||
|
BUG_ON(req->data_len);
|
||||||
|
BUG_ON(req->data);
|
||||||
|
|
||||||
|
cmd->request_bufflen = 0;
|
||||||
|
cmd->request_buffer = NULL;
|
||||||
|
cmd->use_sg = 0;
|
||||||
|
req->buffer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
BUG_ON(sizeof(req->cmd) > sizeof(cmd->cmnd));
|
BUG_ON(sizeof(req->cmd) > sizeof(cmd->cmnd));
|
||||||
memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
|
memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
|
||||||
@@ -1098,154 +1133,138 @@ static void scsi_setup_blk_pc_cmnd(struct scsi_cmnd *cmd)
|
|||||||
cmd->allowed = req->retries;
|
cmd->allowed = req->retries;
|
||||||
cmd->timeout_per_command = req->timeout;
|
cmd->timeout_per_command = req->timeout;
|
||||||
cmd->done = scsi_blk_pc_done;
|
cmd->done = scsi_blk_pc_done;
|
||||||
|
return BLKPREP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Setup a REQ_TYPE_FS command. These are simple read/write request
|
||||||
|
* from filesystems that still need to be translated to SCSI CDBs from
|
||||||
|
* the ULD.
|
||||||
|
*/
|
||||||
|
static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
|
||||||
|
{
|
||||||
|
struct scsi_cmnd *cmd;
|
||||||
|
struct scsi_driver *drv;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Filesystem requests must transfer data.
|
||||||
|
*/
|
||||||
|
BUG_ON(!req->nr_phys_segments);
|
||||||
|
|
||||||
|
cmd = scsi_get_cmd_from_req(sdev, req);
|
||||||
|
if (unlikely(!cmd))
|
||||||
|
return BLKPREP_DEFER;
|
||||||
|
|
||||||
|
ret = scsi_init_io(cmd);
|
||||||
|
if (unlikely(ret))
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize the actual SCSI command for this request.
|
||||||
|
*/
|
||||||
|
drv = *(struct scsi_driver **)req->rq_disk->private_data;
|
||||||
|
if (unlikely(!drv->init_command(cmd))) {
|
||||||
|
scsi_release_buffers(cmd);
|
||||||
|
scsi_put_command(cmd);
|
||||||
|
return BLKPREP_KILL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return BLKPREP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int scsi_prep_fn(struct request_queue *q, struct request *req)
|
static int scsi_prep_fn(struct request_queue *q, struct request *req)
|
||||||
{
|
{
|
||||||
struct scsi_device *sdev = q->queuedata;
|
struct scsi_device *sdev = q->queuedata;
|
||||||
struct scsi_cmnd *cmd;
|
int ret = BLKPREP_OK;
|
||||||
int specials_only = 0;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Just check to see if the device is online. If it isn't, we
|
* If the device is not in running state we will reject some
|
||||||
* refuse to process any commands. The device must be brought
|
* or all commands.
|
||||||
* online before trying any recovery commands
|
|
||||||
*/
|
*/
|
||||||
if (unlikely(!scsi_device_online(sdev))) {
|
|
||||||
sdev_printk(KERN_ERR, sdev,
|
|
||||||
"rejecting I/O to offline device\n");
|
|
||||||
goto kill;
|
|
||||||
}
|
|
||||||
if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
|
if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
|
||||||
/* OK, we're not in a running state don't prep
|
switch (sdev->sdev_state) {
|
||||||
* user commands */
|
case SDEV_OFFLINE:
|
||||||
if (sdev->sdev_state == SDEV_DEL) {
|
/*
|
||||||
/* Device is fully deleted, no commands
|
* If the device is offline we refuse to process any
|
||||||
* at all allowed down */
|
* commands. The device must be brought online
|
||||||
|
* before trying any recovery commands.
|
||||||
|
*/
|
||||||
|
sdev_printk(KERN_ERR, sdev,
|
||||||
|
"rejecting I/O to offline device\n");
|
||||||
|
ret = BLKPREP_KILL;
|
||||||
|
break;
|
||||||
|
case SDEV_DEL:
|
||||||
|
/*
|
||||||
|
* If the device is fully deleted, we refuse to
|
||||||
|
* process any commands as well.
|
||||||
|
*/
|
||||||
sdev_printk(KERN_ERR, sdev,
|
sdev_printk(KERN_ERR, sdev,
|
||||||
"rejecting I/O to dead device\n");
|
"rejecting I/O to dead device\n");
|
||||||
goto kill;
|
ret = BLKPREP_KILL;
|
||||||
}
|
break;
|
||||||
/* OK, we only allow special commands (i.e. not
|
case SDEV_QUIESCE:
|
||||||
* user initiated ones */
|
case SDEV_BLOCK:
|
||||||
specials_only = sdev->sdev_state;
|
/*
|
||||||
}
|
* If the devices is blocked we defer normal commands.
|
||||||
|
*/
|
||||||
/*
|
if (!(req->cmd_flags & REQ_PREEMPT))
|
||||||
* Find the actual device driver associated with this command.
|
ret = BLKPREP_DEFER;
|
||||||
* The SPECIAL requests are things like character device or
|
break;
|
||||||
* ioctls, which did not originate from ll_rw_blk. Note that
|
default:
|
||||||
* the special field is also used to indicate the cmd for
|
/*
|
||||||
* the remainder of a partially fulfilled request that can
|
* For any other not fully online state we only allow
|
||||||
* come up when there is a medium error. We have to treat
|
* special commands. In particular any user initiated
|
||||||
* these two cases differently. We differentiate by looking
|
* command is not allowed.
|
||||||
* at request->cmd, as this tells us the real story.
|
*/
|
||||||
*/
|
if (!(req->cmd_flags & REQ_PREEMPT))
|
||||||
if (blk_special_request(req) && req->special)
|
ret = BLKPREP_KILL;
|
||||||
cmd = req->special;
|
break;
|
||||||
else if (blk_pc_request(req) || blk_fs_request(req)) {
|
|
||||||
if (unlikely(specials_only) && !(req->cmd_flags & REQ_PREEMPT)){
|
|
||||||
if (specials_only == SDEV_QUIESCE ||
|
|
||||||
specials_only == SDEV_BLOCK)
|
|
||||||
goto defer;
|
|
||||||
|
|
||||||
sdev_printk(KERN_ERR, sdev,
|
|
||||||
"rejecting I/O to device being removed\n");
|
|
||||||
goto kill;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (ret != BLKPREP_OK)
|
||||||
* Now try and find a command block that we can use.
|
goto out;
|
||||||
*/
|
|
||||||
if (!req->special) {
|
|
||||||
cmd = scsi_get_command(sdev, GFP_ATOMIC);
|
|
||||||
if (unlikely(!cmd))
|
|
||||||
goto defer;
|
|
||||||
} else
|
|
||||||
cmd = req->special;
|
|
||||||
|
|
||||||
/* pull a tag out of the request if we have one */
|
|
||||||
cmd->tag = req->tag;
|
|
||||||
} else {
|
|
||||||
blk_dump_rq_flags(req, "SCSI bad req");
|
|
||||||
goto kill;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* note the overloading of req->special. When the tag
|
switch (req->cmd_type) {
|
||||||
* is active it always means cmd. If the tag goes
|
case REQ_TYPE_BLOCK_PC:
|
||||||
* back for re-queueing, it may be reset */
|
ret = scsi_setup_blk_pc_cmnd(sdev, req);
|
||||||
req->special = cmd;
|
break;
|
||||||
cmd->request = req;
|
case REQ_TYPE_FS:
|
||||||
|
ret = scsi_setup_fs_cmnd(sdev, req);
|
||||||
/*
|
break;
|
||||||
* FIXME: drop the lock here because the functions below
|
default:
|
||||||
* expect to be called without the queue lock held. Also,
|
|
||||||
* previously, we dequeued the request before dropping the
|
|
||||||
* lock. We hope REQ_STARTED prevents anything untoward from
|
|
||||||
* happening now.
|
|
||||||
*/
|
|
||||||
if (blk_fs_request(req) || blk_pc_request(req)) {
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This will do a couple of things:
|
* All other command types are not supported.
|
||||||
* 1) Fill in the actual SCSI command.
|
|
||||||
* 2) Fill in any other upper-level specific fields
|
|
||||||
* (timeout).
|
|
||||||
*
|
*
|
||||||
* If this returns 0, it means that the request failed
|
* Note that these days the SCSI subsystem does not use
|
||||||
* (reading past end of disk, reading offline device,
|
* REQ_TYPE_SPECIAL requests anymore. These are only used
|
||||||
* etc). This won't actually talk to the device, but
|
* (directly or via blk_insert_request) by non-SCSI drivers.
|
||||||
* some kinds of consistency checking may cause the
|
|
||||||
* request to be rejected immediately.
|
|
||||||
*/
|
*/
|
||||||
|
blk_dump_rq_flags(req, "SCSI bad req");
|
||||||
/*
|
ret = BLKPREP_KILL;
|
||||||
* This sets up the scatter-gather table (allocating if
|
break;
|
||||||
* required).
|
|
||||||
*/
|
|
||||||
ret = scsi_init_io(cmd);
|
|
||||||
switch(ret) {
|
|
||||||
/* For BLKPREP_KILL/DEFER the cmd was released */
|
|
||||||
case BLKPREP_KILL:
|
|
||||||
goto kill;
|
|
||||||
case BLKPREP_DEFER:
|
|
||||||
goto defer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Initialize the actual SCSI command for this request.
|
|
||||||
*/
|
|
||||||
if (blk_pc_request(req)) {
|
|
||||||
scsi_setup_blk_pc_cmnd(cmd);
|
|
||||||
} else if (req->rq_disk) {
|
|
||||||
struct scsi_driver *drv;
|
|
||||||
|
|
||||||
drv = *(struct scsi_driver **)req->rq_disk->private_data;
|
|
||||||
if (unlikely(!drv->init_command(cmd))) {
|
|
||||||
scsi_release_buffers(cmd);
|
|
||||||
scsi_put_command(cmd);
|
|
||||||
goto kill;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
out:
|
||||||
* The request is now prepped, no need to come back here
|
switch (ret) {
|
||||||
*/
|
case BLKPREP_KILL:
|
||||||
req->cmd_flags |= REQ_DONTPREP;
|
req->errors = DID_NO_CONNECT << 16;
|
||||||
return BLKPREP_OK;
|
break;
|
||||||
|
case BLKPREP_DEFER:
|
||||||
|
/*
|
||||||
|
* If we defer, the elv_next_request() returns NULL, but the
|
||||||
|
* queue must be restarted, so we plug here if no returning
|
||||||
|
* command will automatically do that.
|
||||||
|
*/
|
||||||
|
if (sdev->device_busy == 0)
|
||||||
|
blk_plug_device(q);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
req->cmd_flags |= REQ_DONTPREP;
|
||||||
|
}
|
||||||
|
|
||||||
defer:
|
return ret;
|
||||||
/* If we defer, the elv_next_request() returns NULL, but the
|
|
||||||
* queue must be restarted, so we plug here if no returning
|
|
||||||
* command will automatically do that. */
|
|
||||||
if (sdev->device_busy == 0)
|
|
||||||
blk_plug_device(q);
|
|
||||||
return BLKPREP_DEFER;
|
|
||||||
kill:
|
|
||||||
req->errors = DID_NO_CONNECT << 16;
|
|
||||||
return BLKPREP_KILL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user