Skip to content

Commit a1b73fc

Browse files
author
Christoph Hellwig
committed
scsi: reintroduce scsi_driver.init_command
Instead of letting the ULD play games with the prep_fn move back to the model of a central prep_fn with a callback to the ULD. This already cleans up and shortens the code by itself, and will be required to properly support blk-mq in the SCSI midlayer. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Nicholas Bellinger <nab@linux-iscsi.org> Reviewed-by: Mike Christie <michaelc@cs.wisc.edu> Reviewed-by: Hannes Reinecke <hare@suse.de>
1 parent bc85dc5 commit a1b73fc

File tree

4 files changed

+62
-76
lines changed

4 files changed

+62
-76
lines changed

drivers/scsi/scsi_lib.c

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,15 +1073,7 @@ static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
10731073

10741074
int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
10751075
{
1076-
struct scsi_cmnd *cmd;
1077-
int ret = scsi_prep_state_check(sdev, req);
1078-
1079-
if (ret != BLKPREP_OK)
1080-
return ret;
1081-
1082-
cmd = scsi_get_cmd_from_req(sdev, req);
1083-
if (unlikely(!cmd))
1084-
return BLKPREP_DEFER;
1076+
struct scsi_cmnd *cmd = req->special;
10851077

10861078
/*
10871079
* BLOCK_PC requests may transfer data, in which case they must
@@ -1125,15 +1117,11 @@ EXPORT_SYMBOL(scsi_setup_blk_pc_cmnd);
11251117
*/
11261118
int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
11271119
{
1128-
struct scsi_cmnd *cmd;
1129-
int ret = scsi_prep_state_check(sdev, req);
1130-
1131-
if (ret != BLKPREP_OK)
1132-
return ret;
1120+
struct scsi_cmnd *cmd = req->special;
11331121

11341122
if (unlikely(sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh
11351123
&& sdev->scsi_dh_data->scsi_dh->prep_fn)) {
1136-
ret = sdev->scsi_dh_data->scsi_dh->prep_fn(sdev, req);
1124+
int ret = sdev->scsi_dh_data->scsi_dh->prep_fn(sdev, req);
11371125
if (ret != BLKPREP_OK)
11381126
return ret;
11391127
}
@@ -1143,16 +1131,13 @@ int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
11431131
*/
11441132
BUG_ON(!req->nr_phys_segments);
11451133

1146-
cmd = scsi_get_cmd_from_req(sdev, req);
1147-
if (unlikely(!cmd))
1148-
return BLKPREP_DEFER;
1149-
11501134
memset(cmd->cmnd, 0, BLK_MAX_CDB);
11511135
return scsi_init_io(cmd, GFP_ATOMIC);
11521136
}
11531137
EXPORT_SYMBOL(scsi_setup_fs_cmnd);
11541138

1155-
int scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
1139+
static int
1140+
scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
11561141
{
11571142
int ret = BLKPREP_OK;
11581143

@@ -1204,9 +1189,9 @@ int scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
12041189
}
12051190
return ret;
12061191
}
1207-
EXPORT_SYMBOL(scsi_prep_state_check);
12081192

1209-
int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
1193+
static int
1194+
scsi_prep_return(struct request_queue *q, struct request *req, int ret)
12101195
{
12111196
struct scsi_device *sdev = q->queuedata;
12121197

@@ -1237,18 +1222,44 @@ int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
12371222

12381223
return ret;
12391224
}
1240-
EXPORT_SYMBOL(scsi_prep_return);
12411225

1242-
int scsi_prep_fn(struct request_queue *q, struct request *req)
1226+
static int scsi_prep_fn(struct request_queue *q, struct request *req)
12431227
{
12441228
struct scsi_device *sdev = q->queuedata;
1245-
int ret = BLKPREP_KILL;
1229+
struct scsi_cmnd *cmd;
1230+
int ret;
12461231

1247-
if (req->cmd_type == REQ_TYPE_BLOCK_PC)
1232+
ret = scsi_prep_state_check(sdev, req);
1233+
if (ret != BLKPREP_OK)
1234+
goto out;
1235+
1236+
cmd = scsi_get_cmd_from_req(sdev, req);
1237+
if (unlikely(!cmd)) {
1238+
ret = BLKPREP_DEFER;
1239+
goto out;
1240+
}
1241+
1242+
if (req->cmd_type == REQ_TYPE_FS)
1243+
ret = scsi_cmd_to_driver(cmd)->init_command(cmd);
1244+
else if (req->cmd_type == REQ_TYPE_BLOCK_PC)
12481245
ret = scsi_setup_blk_pc_cmnd(sdev, req);
1246+
else
1247+
ret = BLKPREP_KILL;
1248+
1249+
out:
12491250
return scsi_prep_return(q, req, ret);
12501251
}
1251-
EXPORT_SYMBOL(scsi_prep_fn);
1252+
1253+
static void scsi_unprep_fn(struct request_queue *q, struct request *req)
1254+
{
1255+
if (req->cmd_type == REQ_TYPE_FS) {
1256+
struct scsi_cmnd *cmd = req->special;
1257+
struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
1258+
1259+
if (drv->uninit_command)
1260+
drv->uninit_command(cmd);
1261+
}
1262+
}
12521263

12531264
/*
12541265
* scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
@@ -1669,6 +1680,7 @@ struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
16691680
return NULL;
16701681

16711682
blk_queue_prep_rq(q, scsi_prep_fn);
1683+
blk_queue_unprep_rq(q, scsi_unprep_fn);
16721684
blk_queue_softirq_done(q, scsi_softirq_done);
16731685
blk_queue_rq_timed_out(q, scsi_times_out);
16741686
blk_queue_lld_busy(q, scsi_lld_busy);

drivers/scsi/sd.c

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static int sd_suspend_system(struct device *);
109109
static int sd_suspend_runtime(struct device *);
110110
static int sd_resume(struct device *);
111111
static void sd_rescan(struct device *);
112+
static int sd_init_command(struct scsi_cmnd *SCpnt);
113+
static void sd_uninit_command(struct scsi_cmnd *SCpnt);
112114
static int sd_done(struct scsi_cmnd *);
113115
static int sd_eh_action(struct scsi_cmnd *, int);
114116
static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
@@ -503,6 +505,8 @@ static struct scsi_driver sd_template = {
503505
.pm = &sd_pm_ops,
504506
},
505507
.rescan = sd_rescan,
508+
.init_command = sd_init_command,
509+
.uninit_command = sd_uninit_command,
506510
.done = sd_done,
507511
.eh_action = sd_eh_action,
508512
};
@@ -838,9 +842,9 @@ static int scsi_setup_flush_cmnd(struct scsi_device *sdp, struct request *rq)
838842
return scsi_setup_blk_pc_cmnd(sdp, rq);
839843
}
840844

841-
static void sd_unprep_fn(struct request_queue *q, struct request *rq)
845+
static void sd_uninit_command(struct scsi_cmnd *SCpnt)
842846
{
843-
struct scsi_cmnd *SCpnt = rq->special;
847+
struct request *rq = SCpnt->request;
844848

845849
if (rq->cmd_flags & REQ_DISCARD) {
846850
free_page((unsigned long)rq->buffer);
@@ -853,18 +857,10 @@ static void sd_unprep_fn(struct request_queue *q, struct request *rq)
853857
}
854858
}
855859

856-
/**
857-
* sd_prep_fn - build a scsi (read or write) command from
858-
* information in the request structure.
859-
* @SCpnt: pointer to mid-level's per scsi command structure that
860-
* contains request and into which the scsi command is written
861-
*
862-
* Returns 1 if successful and 0 if error (or cannot be done now).
863-
**/
864-
static int sd_prep_fn(struct request_queue *q, struct request *rq)
860+
static int sd_init_command(struct scsi_cmnd *SCpnt)
865861
{
866-
struct scsi_cmnd *SCpnt;
867-
struct scsi_device *sdp = q->queuedata;
862+
struct request *rq = SCpnt->request;
863+
struct scsi_device *sdp = SCpnt->device;
868864
struct gendisk *disk = rq->rq_disk;
869865
struct scsi_disk *sdkp;
870866
sector_t block = blk_rq_pos(rq);
@@ -886,12 +882,6 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
886882
} else if (rq->cmd_flags & REQ_FLUSH) {
887883
ret = scsi_setup_flush_cmnd(sdp, rq);
888884
goto out;
889-
} else if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
890-
ret = scsi_setup_blk_pc_cmnd(sdp, rq);
891-
goto out;
892-
} else if (rq->cmd_type != REQ_TYPE_FS) {
893-
ret = BLKPREP_KILL;
894-
goto out;
895885
}
896886
ret = scsi_setup_fs_cmnd(sdp, rq);
897887
if (ret != BLKPREP_OK)
@@ -903,11 +893,10 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
903893
* is used for a killable error condition */
904894
ret = BLKPREP_KILL;
905895

906-
SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
907-
"sd_prep_fn: block=%llu, "
908-
"count=%d\n",
909-
(unsigned long long)block,
910-
this_count));
896+
SCSI_LOG_HLQUEUE(1,
897+
scmd_printk(KERN_INFO, SCpnt,
898+
"%s: block=%llu, count=%d\n",
899+
__func__, (unsigned long long)block, this_count));
911900

912901
if (!sdp || !scsi_device_online(sdp) ||
913902
block + blk_rq_sectors(rq) > get_capacity(disk)) {
@@ -1127,7 +1116,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
11271116
*/
11281117
ret = BLKPREP_OK;
11291118
out:
1130-
return scsi_prep_return(q, rq, ret);
1119+
return ret;
11311120
}
11321121

11331122
/**
@@ -2878,9 +2867,6 @@ static void sd_probe_async(void *data, async_cookie_t cookie)
28782867

28792868
sd_revalidate_disk(gd);
28802869

2881-
blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
2882-
blk_queue_unprep_rq(sdp->request_queue, sd_unprep_fn);
2883-
28842870
gd->driverfs_dev = &sdp->sdev_gendev;
28852871
gd->flags = GENHD_FL_EXT_DEVT;
28862872
if (sdp->removable) {
@@ -3028,8 +3014,6 @@ static int sd_remove(struct device *dev)
30283014

30293015
async_synchronize_full_domain(&scsi_sd_pm_domain);
30303016
async_synchronize_full_domain(&scsi_sd_probe_domain);
3031-
blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
3032-
blk_queue_unprep_rq(sdkp->device->request_queue, NULL);
30333017
device_del(&sdkp->dev);
30343018
del_gendisk(sdkp->disk);
30353019
sd_shutdown(dev);

drivers/scsi/sr.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
7979
static DEFINE_MUTEX(sr_mutex);
8080
static int sr_probe(struct device *);
8181
static int sr_remove(struct device *);
82+
static int sr_init_command(struct scsi_cmnd *SCpnt);
8283
static int sr_done(struct scsi_cmnd *);
8384
static int sr_runtime_suspend(struct device *dev);
8485

@@ -94,6 +95,7 @@ static struct scsi_driver sr_template = {
9495
.remove = sr_remove,
9596
.pm = &sr_pm_ops,
9697
},
98+
.init_command = sr_init_command,
9799
.done = sr_done,
98100
};
99101

@@ -378,21 +380,14 @@ static int sr_done(struct scsi_cmnd *SCpnt)
378380
return good_bytes;
379381
}
380382

381-
static int sr_prep_fn(struct request_queue *q, struct request *rq)
383+
static int sr_init_command(struct scsi_cmnd *SCpnt)
382384
{
383385
int block = 0, this_count, s_size;
384386
struct scsi_cd *cd;
385-
struct scsi_cmnd *SCpnt;
386-
struct scsi_device *sdp = q->queuedata;
387+
struct request *rq = SCpnt->request;
388+
struct scsi_device *sdp = SCpnt->device;
387389
int ret;
388390

389-
if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
390-
ret = scsi_setup_blk_pc_cmnd(sdp, rq);
391-
goto out;
392-
} else if (rq->cmd_type != REQ_TYPE_FS) {
393-
ret = BLKPREP_KILL;
394-
goto out;
395-
}
396391
ret = scsi_setup_fs_cmnd(sdp, rq);
397392
if (ret != BLKPREP_OK)
398393
goto out;
@@ -517,7 +512,7 @@ static int sr_prep_fn(struct request_queue *q, struct request *rq)
517512
*/
518513
ret = BLKPREP_OK;
519514
out:
520-
return scsi_prep_return(q, rq, ret);
515+
return ret;
521516
}
522517

523518
static int sr_block_open(struct block_device *bdev, fmode_t mode)
@@ -718,7 +713,6 @@ static int sr_probe(struct device *dev)
718713

719714
/* FIXME: need to handle a get_capabilities failure properly ?? */
720715
get_capabilities(cd);
721-
blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
722716
sr_vendor_init(cd);
723717

724718
disk->driverfs_dev = &sdev->sdev_gendev;
@@ -993,7 +987,6 @@ static int sr_remove(struct device *dev)
993987

994988
scsi_autopm_get_device(cd->device);
995989

996-
blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
997990
del_gendisk(cd->disk);
998991

999992
mutex_lock(&sr_ref_mutex);

include/scsi/scsi_driver.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
#include <linux/device.h>
55

66
struct module;
7+
struct request;
78
struct scsi_cmnd;
89
struct scsi_device;
9-
struct request;
10-
struct request_queue;
11-
1210

1311
struct scsi_driver {
1412
struct module *owner;
1513
struct device_driver gendrv;
1614

1715
void (*rescan)(struct device *);
16+
int (*init_command)(struct scsi_cmnd *);
17+
void (*uninit_command)(struct scsi_cmnd *);
1818
int (*done)(struct scsi_cmnd *);
1919
int (*eh_action)(struct scsi_cmnd *, int);
2020
};
@@ -31,8 +31,5 @@ extern int scsi_register_interface(struct class_interface *);
3131

3232
int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req);
3333
int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req);
34-
int scsi_prep_state_check(struct scsi_device *sdev, struct request *req);
35-
int scsi_prep_return(struct request_queue *q, struct request *req, int ret);
36-
int scsi_prep_fn(struct request_queue *, struct request *);
3734

3835
#endif /* _SCSI_SCSI_DRIVER_H */

0 commit comments

Comments
 (0)