Commit 3b003157 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by James Bottomley

[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: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJames Bottomley <James.Bottomley@SteelEye.com>
parent 2dc611de
...@@ -995,25 +995,14 @@ static int scsi_init_io(struct scsi_cmnd *cmd) ...@@ -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) ...@@ -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, ...@@ -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) ...@@ -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) ...@@ -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;
} }
static int scsi_prep_fn(struct request_queue *q, struct request *req) /*
* 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_device *sdev = q->queuedata;
struct scsi_cmnd *cmd; struct scsi_cmnd *cmd;
int specials_only = 0; struct scsi_driver *drv;
int ret;
/* /*
* Just check to see if the device is online. If it isn't, we * Filesystem requests must transfer data.
* refuse to process any commands. The device must be brought
* online before trying any recovery commands
*/ */
if (unlikely(!scsi_device_online(sdev))) { BUG_ON(!req->nr_phys_segments);
sdev_printk(KERN_ERR, sdev,
"rejecting I/O to offline device\n");
goto kill;
}
if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
/* OK, we're not in a running state don't prep
* user commands */
if (sdev->sdev_state == SDEV_DEL) {
/* Device is fully deleted, no commands
* at all allowed down */
sdev_printk(KERN_ERR, sdev,
"rejecting I/O to dead device\n");
goto kill;
}
/* OK, we only allow special commands (i.e. not
* user initiated ones */
specials_only = sdev->sdev_state;
}
/* cmd = scsi_get_cmd_from_req(sdev, req);
* Find the actual device driver associated with this command. if (unlikely(!cmd))
* The SPECIAL requests are things like character device or return BLKPREP_DEFER;
* ioctls, which did not originate from ll_rw_blk. Note that
* the special field is also used to indicate the cmd for
* the remainder of a partially fulfilled request that can
* come up when there is a medium error. We have to treat
* these two cases differently. We differentiate by looking
* at request->cmd, as this tells us the real story.
*/
if (blk_special_request(req) && req->special)
cmd = req->special;
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, ret = scsi_init_io(cmd);
"rejecting I/O to device being removed\n"); if (unlikely(ret))
goto kill; return ret;
}
/* /*
* Now try and find a command block that we can use. * Initialize the actual SCSI command for this request.
*/ */
if (!req->special) { drv = *(struct scsi_driver **)req->rq_disk->private_data;
cmd = scsi_get_command(sdev, GFP_ATOMIC); if (unlikely(!drv->init_command(cmd))) {
if (unlikely(!cmd)) scsi_release_buffers(cmd);
goto defer; scsi_put_command(cmd);
} else return BLKPREP_KILL;
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 return BLKPREP_OK;
* is active it always means cmd. If the tag goes }
* back for re-queueing, it may be reset */
req->special = cmd; static int scsi_prep_fn(struct request_queue *q, struct request *req)
cmd->request = req; {
struct scsi_device *sdev = q->queuedata;
int ret = BLKPREP_OK;
/* /*
* FIXME: drop the lock here because the functions below * If the device is not in running state we will reject some
* expect to be called without the queue lock held. Also, * or all commands.
* 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)) { if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
int ret; switch (sdev->sdev_state) {
case SDEV_OFFLINE:
/* /*
* This will do a couple of things: * If the device is offline we refuse to process any
* 1) Fill in the actual SCSI command. * commands. The device must be brought online
* 2) Fill in any other upper-level specific fields * before trying any recovery commands.
* (timeout).
*
* If this returns 0, it means that the request failed
* (reading past end of disk, reading offline device,
* etc). This won't actually talk to the device, but
* some kinds of consistency checking may cause the
* request to be rejected immediately.
*/ */
sdev_printk(KERN_ERR, sdev,
"rejecting I/O to offline device\n");
ret = BLKPREP_KILL;
break;
case SDEV_DEL:
/* /*
* This sets up the scatter-gather table (allocating if * If the device is fully deleted, we refuse to
* required). * process any commands as well.
*/ */
ret = scsi_init_io(cmd); sdev_printk(KERN_ERR, sdev,
switch(ret) { "rejecting I/O to dead device\n");
/* For BLKPREP_KILL/DEFER the cmd was released */ ret = BLKPREP_KILL;
case BLKPREP_KILL: break;
goto kill; case SDEV_QUIESCE:
case BLKPREP_DEFER: case SDEV_BLOCK:
goto defer;
}
/* /*
* Initialize the actual SCSI command for this request. * If the devices is blocked we defer normal commands.
*/ */
if (blk_pc_request(req)) { if (!(req->cmd_flags & REQ_PREEMPT))
scsi_setup_blk_pc_cmnd(cmd); ret = BLKPREP_DEFER;
} else if (req->rq_disk) { break;
struct scsi_driver *drv; default:
/*
drv = *(struct scsi_driver **)req->rq_disk->private_data; * For any other not fully online state we only allow
if (unlikely(!drv->init_command(cmd))) { * special commands. In particular any user initiated
scsi_release_buffers(cmd); * command is not allowed.
scsi_put_command(cmd); */
goto kill; if (!(req->cmd_flags & REQ_PREEMPT))
} ret = BLKPREP_KILL;
break;
} }
if (ret != BLKPREP_OK)
goto out;
} }
switch (req->cmd_type) {
case REQ_TYPE_BLOCK_PC:
ret = scsi_setup_blk_pc_cmnd(sdev, req);
break;
case REQ_TYPE_FS:
ret = scsi_setup_fs_cmnd(sdev, req);
break;
default:
/* /*
* The request is now prepped, no need to come back here * All other command types are not supported.
*
* Note that these days the SCSI subsystem does not use
* REQ_TYPE_SPECIAL requests anymore. These are only used
* (directly or via blk_insert_request) by non-SCSI drivers.
*/ */
req->cmd_flags |= REQ_DONTPREP; blk_dump_rq_flags(req, "SCSI bad req");
return BLKPREP_OK; ret = BLKPREP_KILL;
break;
}
defer: out:
/* If we defer, the elv_next_request() returns NULL, but the switch (ret) {
case BLKPREP_KILL:
req->errors = DID_NO_CONNECT << 16;
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 * queue must be restarted, so we plug here if no returning
* command will automatically do that. */ * command will automatically do that.
*/
if (sdev->device_busy == 0) if (sdev->device_busy == 0)
blk_plug_device(q); blk_plug_device(q);
return BLKPREP_DEFER; break;
kill: default:
req->errors = DID_NO_CONNECT << 16; req->cmd_flags |= REQ_DONTPREP;
return BLKPREP_KILL; }
return ret;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment