aboutsummaryrefslogtreecommitdiff
path: root/hw/block/dataplane/virtio-blk.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw/block/dataplane/virtio-blk.c')
-rw-r--r--hw/block/dataplane/virtio-blk.c246
1 files changed, 23 insertions, 223 deletions
diff --git a/hw/block/dataplane/virtio-blk.c b/hw/block/dataplane/virtio-blk.c
index c10b7b70fb..09bd2c70ab 100644
--- a/hw/block/dataplane/virtio-blk.c
+++ b/hw/block/dataplane/virtio-blk.c
@@ -24,16 +24,6 @@
#include "hw/virtio/virtio-bus.h"
#include "qom/object_interfaces.h"
-typedef struct {
- VirtIOBlockDataPlane *s;
- QEMUIOVector *inhdr; /* iovecs for virtio_blk_inhdr */
- VirtQueueElement *elem; /* saved data from the virtqueue */
- QEMUIOVector qiov; /* original request iovecs */
- struct iovec bounce_iov; /* used if guest buffers are unaligned */
- QEMUIOVector bounce_qiov; /* bounce buffer iovecs */
- bool read; /* read or write? */
-} VirtIOBlockRequest;
-
struct VirtIOBlockDataPlane {
bool started;
bool starting;
@@ -57,6 +47,8 @@ struct VirtIOBlockDataPlane {
/* Operation blocker on BDS */
Error *blocker;
+ void (*saved_complete_request)(struct VirtIOBlockReq *req,
+ unsigned char status);
};
/* Raise an interrupt to signal guest, if necessary */
@@ -69,215 +61,14 @@ static void notify_guest(VirtIOBlockDataPlane *s)
event_notifier_set(s->guest_notifier);
}
-static void complete_rdwr(void *opaque, int ret)
-{
- VirtIOBlockRequest *req = opaque;
- struct virtio_blk_inhdr hdr;
- int len;
-
- if (likely(ret == 0)) {
- hdr.status = VIRTIO_BLK_S_OK;
- len = req->qiov.size;
- } else {
- hdr.status = VIRTIO_BLK_S_IOERR;
- len = 0;
- }
-
- trace_virtio_blk_data_plane_complete_request(req->s, req->elem->index, ret);
-
- if (req->read && req->bounce_iov.iov_base) {
- qemu_iovec_from_buf(&req->qiov, 0, req->bounce_iov.iov_base, len);
- }
-
- if (req->bounce_iov.iov_base) {
- qemu_vfree(req->bounce_iov.iov_base);
- }
-
- qemu_iovec_from_buf(req->inhdr, 0, &hdr, sizeof(hdr));
- qemu_iovec_destroy(req->inhdr);
- g_slice_free(QEMUIOVector, req->inhdr);
-
- /* According to the virtio specification len should be the number of bytes
- * written to, but for virtio-blk it seems to be the number of bytes
- * transferred plus the status bytes.
- */
- vring_push(&req->s->vring, req->elem, len + sizeof(hdr));
- notify_guest(req->s);
- g_slice_free(VirtIOBlockRequest, req);
-}
-
-static void complete_request_early(VirtIOBlockDataPlane *s, VirtQueueElement *elem,
- QEMUIOVector *inhdr, unsigned char status)
-{
- struct virtio_blk_inhdr hdr = {
- .status = status,
- };
-
- qemu_iovec_from_buf(inhdr, 0, &hdr, sizeof(hdr));
- qemu_iovec_destroy(inhdr);
- g_slice_free(QEMUIOVector, inhdr);
-
- vring_push(&s->vring, elem, sizeof(hdr));
- notify_guest(s);
-}
-
-/* Get disk serial number */
-static void do_get_id_cmd(VirtIOBlockDataPlane *s,
- struct iovec *iov, unsigned int iov_cnt,
- VirtQueueElement *elem, QEMUIOVector *inhdr)
-{
- char id[VIRTIO_BLK_ID_BYTES];
-
- /* Serial number not NUL-terminated when longer than buffer */
- strncpy(id, s->blk->serial ? s->blk->serial : "", sizeof(id));
- iov_from_buf(iov, iov_cnt, 0, id, sizeof(id));
- complete_request_early(s, elem, inhdr, VIRTIO_BLK_S_OK);
-}
-
-static void do_rdwr_cmd(VirtIOBlockDataPlane *s, bool read,
- struct iovec *iov, unsigned iov_cnt,
- int64_t sector_num, VirtQueueElement *elem,
- QEMUIOVector *inhdr)
-{
- VirtIOBlockRequest *req = g_slice_new0(VirtIOBlockRequest);
- QEMUIOVector *qiov;
- int nb_sectors;
-
- /* Fill in virtio block metadata needed for completion */
- req->s = s;
- req->elem = elem;
- req->inhdr = inhdr;
- req->read = read;
- qemu_iovec_init_external(&req->qiov, iov, iov_cnt);
-
- qiov = &req->qiov;
-
- if (!bdrv_qiov_is_aligned(s->blk->conf.bs, qiov)) {
- void *bounce_buffer = qemu_blockalign(s->blk->conf.bs, qiov->size);
-
- /* Populate bounce buffer with data for writes */
- if (!read) {
- qemu_iovec_to_buf(qiov, 0, bounce_buffer, qiov->size);
- }
-
- /* Redirect I/O to aligned bounce buffer */
- req->bounce_iov.iov_base = bounce_buffer;
- req->bounce_iov.iov_len = qiov->size;
- qemu_iovec_init_external(&req->bounce_qiov, &req->bounce_iov, 1);
- qiov = &req->bounce_qiov;
- }
-
- nb_sectors = qiov->size / BDRV_SECTOR_SIZE;
-
- if (read) {
- bdrv_aio_readv(s->blk->conf.bs, sector_num, qiov, nb_sectors,
- complete_rdwr, req);
- } else {
- bdrv_aio_writev(s->blk->conf.bs, sector_num, qiov, nb_sectors,
- complete_rdwr, req);
- }
-}
-
-static void complete_flush(void *opaque, int ret)
-{
- VirtIOBlockRequest *req = opaque;
- unsigned char status;
-
- if (ret == 0) {
- status = VIRTIO_BLK_S_OK;
- } else {
- status = VIRTIO_BLK_S_IOERR;
- }
-
- complete_request_early(req->s, req->elem, req->inhdr, status);
- g_slice_free(VirtIOBlockRequest, req);
-}
-
-static void do_flush_cmd(VirtIOBlockDataPlane *s, VirtQueueElement *elem,
- QEMUIOVector *inhdr)
+static void complete_request_vring(VirtIOBlockReq *req, unsigned char status)
{
- VirtIOBlockRequest *req = g_slice_new(VirtIOBlockRequest);
- req->s = s;
- req->elem = elem;
- req->inhdr = inhdr;
+ stb_p(&req->in->status, status);
- bdrv_aio_flush(s->blk->conf.bs, complete_flush, req);
-}
-
-static void do_scsi_cmd(VirtIOBlockDataPlane *s, VirtQueueElement *elem,
- QEMUIOVector *inhdr)
-{
- int status;
-
- status = virtio_blk_handle_scsi_req(VIRTIO_BLK(s->vdev), elem);
- complete_request_early(s, elem, inhdr, status);
-}
-
-static int process_request(VirtIOBlockDataPlane *s, VirtQueueElement *elem)
-{
- struct iovec *iov = elem->out_sg;
- struct iovec *in_iov = elem->in_sg;
- unsigned out_num = elem->out_num;
- unsigned in_num = elem->in_num;
- struct virtio_blk_outhdr outhdr;
- QEMUIOVector *inhdr;
- size_t in_size;
-
- /* Copy in outhdr */
- if (unlikely(iov_to_buf(iov, out_num, 0, &outhdr,
- sizeof(outhdr)) != sizeof(outhdr))) {
- error_report("virtio-blk request outhdr too short");
- return -EFAULT;
- }
- iov_discard_front(&iov, &out_num, sizeof(outhdr));
-
- /* Grab inhdr for later */
- in_size = iov_size(in_iov, in_num);
- if (in_size < sizeof(struct virtio_blk_inhdr)) {
- error_report("virtio_blk request inhdr too short");
- return -EFAULT;
- }
- inhdr = g_slice_new(QEMUIOVector);
- qemu_iovec_init(inhdr, 1);
- qemu_iovec_concat_iov(inhdr, in_iov, in_num,
- in_size - sizeof(struct virtio_blk_inhdr),
- sizeof(struct virtio_blk_inhdr));
- iov_discard_back(in_iov, &in_num, sizeof(struct virtio_blk_inhdr));
-
- /* TODO Linux sets the barrier bit even when not advertised! */
- outhdr.type &= ~VIRTIO_BLK_T_BARRIER;
-
- switch (outhdr.type) {
- case VIRTIO_BLK_T_IN:
- do_rdwr_cmd(s, true, in_iov, in_num,
- outhdr.sector * 512 / BDRV_SECTOR_SIZE,
- elem, inhdr);
- return 0;
-
- case VIRTIO_BLK_T_OUT:
- do_rdwr_cmd(s, false, iov, out_num,
- outhdr.sector * 512 / BDRV_SECTOR_SIZE,
- elem, inhdr);
- return 0;
-
- case VIRTIO_BLK_T_SCSI_CMD:
- do_scsi_cmd(s, elem, inhdr);
- return 0;
-
- case VIRTIO_BLK_T_FLUSH:
- do_flush_cmd(s, elem, inhdr);
- return 0;
-
- case VIRTIO_BLK_T_GET_ID:
- do_get_id_cmd(s, in_iov, in_num, elem, inhdr);
- return 0;
-
- default:
- error_report("virtio-blk unsupported request type %#x", outhdr.type);
- qemu_iovec_destroy(inhdr);
- g_slice_free(QEMUIOVector, inhdr);
- return -EFAULT;
- }
+ vring_push(&req->dev->dataplane->vring, req->elem,
+ req->qiov.size + sizeof(*req->in));
+ notify_guest(req->dev->dataplane);
+ g_slice_free(VirtIOBlockReq, req);
}
static void handle_notify(EventNotifier *e)
@@ -286,7 +77,11 @@ static void handle_notify(EventNotifier *e)
host_notifier);
VirtQueueElement *elem;
+ VirtIOBlockReq *req;
int ret;
+ MultiReqBuffer mrb = {
+ .num_writes = 0,
+ };
event_notifier_test_and_clear(&s->host_notifier);
for (;;) {
@@ -303,14 +98,14 @@ static void handle_notify(EventNotifier *e)
trace_virtio_blk_data_plane_process_request(s, elem->out_num,
elem->in_num, elem->index);
- if (process_request(s, elem) < 0) {
- vring_set_broken(&s->vring);
- vring_free_element(elem);
- ret = -EFAULT;
- break;
- }
+ req = g_slice_new(VirtIOBlockReq);
+ req->dev = VIRTIO_BLK(s->vdev);
+ req->elem = elem;
+ virtio_blk_handle_request(req, &mrb);
}
+ virtio_submit_multiwrite(s->blk->conf.bs, &mrb);
+
if (likely(ret == -EAGAIN)) { /* vring emptied */
/* Re-enable guest->host notifies and stop processing the vring.
* But if the guest has snuck in more descriptors, keep processing.
@@ -330,6 +125,7 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
Error **errp)
{
VirtIOBlockDataPlane *s;
+ VirtIOBlock *vblk = VIRTIO_BLK(vdev);
Error *local_err = NULL;
*dataplane = NULL;
@@ -372,6 +168,8 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *blk,
bdrv_op_block_all(blk->conf.bs, s->blocker);
*dataplane = s;
+ s->saved_complete_request = vblk->complete_request;
+ vblk->complete_request = complete_request_vring;
}
/* Context: QEMU global mutex held */
@@ -446,10 +244,12 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
{
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+ VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
if (!s->started || s->stopping) {
return;
}
s->stopping = true;
+ vblk->complete_request = s->saved_complete_request;
trace_virtio_blk_data_plane_stop(s);
aio_context_acquire(s->ctx);