Skip to content

Commit 20af3cf

Browse files
bonzinirustyrussell
authored andcommitted
virtio-blk: use virtqueue_add_sgs on req path
(This is a respin of Paolo Bonzini's patch, but it calls virtqueue_add_sgs() instead of his multi-part API). This is similar to the previous patch, but a bit more radical because the bio and req paths now share the buffer construction code. Because the req path doesn't use vbr->sg, however, we need to add a couple of arguments to __virtblk_add_req. We also need to teach __virtblk_add_req how to build SCSI command requests. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Reviewed-by: Asias He <asias@redhat.com>
1 parent 8f39db9 commit 20af3cf

1 file changed

Lines changed: 33 additions & 36 deletions

File tree

‎drivers/block/virtio_blk.c‎

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,40 @@ static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk,
102102
}
103103

104104
static int __virtblk_add_req(struct virtqueue *vq,
105-
struct virtblk_req *vbr)
105+
struct virtblk_req *vbr,
106+
struct scatterlist *data_sg,
107+
unsigned data_nents)
106108
{
107-
struct scatterlist hdr, status, *sgs[3];
109+
struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
108110
unsigned int num_out = 0, num_in = 0;
111+
int type = vbr->out_hdr.type & ~VIRTIO_BLK_T_OUT;
109112

110113
sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
111114
sgs[num_out++] = &hdr;
112115

113-
if (vbr->nents) {
116+
/*
117+
* If this is a packet command we need a couple of additional headers.
118+
* Behind the normal outhdr we put a segment with the scsi command
119+
* block, and before the normal inhdr we put the sense data and the
120+
* inhdr with additional status information.
121+
*/
122+
if (type == VIRTIO_BLK_T_SCSI_CMD) {
123+
sg_init_one(&cmd, vbr->req->cmd, vbr->req->cmd_len);
124+
sgs[num_out++] = &cmd;
125+
}
126+
127+
if (data_nents) {
114128
if (vbr->out_hdr.type & VIRTIO_BLK_T_OUT)
115-
sgs[num_out++] = vbr->sg;
129+
sgs[num_out++] = data_sg;
116130
else
117-
sgs[num_out + num_in++] = vbr->sg;
131+
sgs[num_out + num_in++] = data_sg;
132+
}
133+
134+
if (type == VIRTIO_BLK_T_SCSI_CMD) {
135+
sg_init_one(&sense, vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
136+
sgs[num_out + num_in++] = &sense;
137+
sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr));
138+
sgs[num_out + num_in++] = &inhdr;
118139
}
119140

120141
sg_init_one(&status, &vbr->status, sizeof(vbr->status));
@@ -130,7 +151,8 @@ static void virtblk_add_req(struct virtblk_req *vbr)
130151
int ret;
131152

132153
spin_lock_irq(vblk->disk->queue->queue_lock);
133-
while (unlikely((ret = __virtblk_add_req(vblk->vq, vbr)) < 0)) {
154+
while (unlikely((ret = __virtblk_add_req(vblk->vq, vbr, vbr->sg,
155+
vbr->nents)) < 0)) {
134156
prepare_to_wait_exclusive(&vblk->queue_wait, &wait,
135157
TASK_UNINTERRUPTIBLE);
136158

@@ -283,7 +305,7 @@ static void virtblk_done(struct virtqueue *vq)
283305
static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
284306
struct request *req)
285307
{
286-
unsigned long num, out = 0, in = 0;
308+
unsigned int num;
287309
struct virtblk_req *vbr;
288310

289311
vbr = virtblk_alloc_req(vblk, GFP_ATOMIC);
@@ -320,40 +342,15 @@ static bool do_req(struct request_queue *q, struct virtio_blk *vblk,
320342
}
321343
}
322344

323-
sg_set_buf(&vblk->sg[out++], &vbr->out_hdr, sizeof(vbr->out_hdr));
324-
325-
/*
326-
* If this is a packet command we need a couple of additional headers.
327-
* Behind the normal outhdr we put a segment with the scsi command
328-
* block, and before the normal inhdr we put the sense data and the
329-
* inhdr with additional status information before the normal inhdr.
330-
*/
331-
if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC)
332-
sg_set_buf(&vblk->sg[out++], vbr->req->cmd, vbr->req->cmd_len);
333-
334-
num = blk_rq_map_sg(q, vbr->req, vblk->sg + out);
335-
336-
if (vbr->req->cmd_type == REQ_TYPE_BLOCK_PC) {
337-
sg_set_buf(&vblk->sg[num + out + in++], vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
338-
sg_set_buf(&vblk->sg[num + out + in++], &vbr->in_hdr,
339-
sizeof(vbr->in_hdr));
340-
}
341-
342-
sg_set_buf(&vblk->sg[num + out + in++], &vbr->status,
343-
sizeof(vbr->status));
344-
345+
num = blk_rq_map_sg(q, vbr->req, vblk->sg);
345346
if (num) {
346-
if (rq_data_dir(vbr->req) == WRITE) {
347+
if (rq_data_dir(vbr->req) == WRITE)
347348
vbr->out_hdr.type |= VIRTIO_BLK_T_OUT;
348-
out += num;
349-
} else {
349+
else
350350
vbr->out_hdr.type |= VIRTIO_BLK_T_IN;
351-
in += num;
352-
}
353351
}
354352

355-
if (virtqueue_add_buf(vblk->vq, vblk->sg, out, in, vbr,
356-
GFP_ATOMIC) < 0) {
353+
if (__virtblk_add_req(vblk->vq, vbr, vblk->sg, num) < 0) {
357354
mempool_free(vbr, vblk->pool);
358355
return false;
359356
}

0 commit comments

Comments
 (0)