qed: Simplify request handling

Now that we process a request in the same coroutine from beginning to
end and don't drop out of it any more, we can look like a proper
coroutine-based driver and simply call qed_aio_next_io() and get a
return value from it instead of spawning an additional coroutine that
reenters the parent when it's done.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Kevin Wolf 2016-11-18 14:47:36 +01:00
parent 0806c3b5dd
commit 48cc565e76
2 changed files with 22 additions and 82 deletions

View File

@ -21,10 +21,6 @@
#include "qapi/qmp/qerror.h" #include "qapi/qmp/qerror.h"
#include "sysemu/block-backend.h" #include "sysemu/block-backend.h"
static const AIOCBInfo qed_aiocb_info = {
.aiocb_size = sizeof(QEDAIOCB),
};
static int bdrv_qed_probe(const uint8_t *buf, int buf_size, static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
const char *filename) const char *filename)
{ {
@ -253,13 +249,6 @@ static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
return l2_table; return l2_table;
} }
static void qed_aio_next_io(QEDAIOCB *acb);
static void qed_aio_start_io(QEDAIOCB *acb)
{
qed_aio_next_io(acb);
}
static void qed_plug_allocating_write_reqs(BDRVQEDState *s) static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
{ {
assert(!s->allocating_write_reqs_plugged); assert(!s->allocating_write_reqs_plugged);
@ -751,7 +740,7 @@ static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
static BDRVQEDState *acb_to_s(QEDAIOCB *acb) static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
{ {
return acb->common.bs->opaque; return acb->bs->opaque;
} }
/** /**
@ -888,28 +877,10 @@ static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
} }
} }
static void qed_aio_complete_bh(void *opaque) static void qed_aio_complete(QEDAIOCB *acb)
{
QEDAIOCB *acb = opaque;
BDRVQEDState *s = acb_to_s(acb);
BlockCompletionFunc *cb = acb->common.cb;
void *user_opaque = acb->common.opaque;
int ret = acb->bh_ret;
qemu_aio_unref(acb);
/* Invoke callback */
qed_acquire(s);
cb(user_opaque, ret);
qed_release(s);
}
static void qed_aio_complete(QEDAIOCB *acb, int ret)
{ {
BDRVQEDState *s = acb_to_s(acb); BDRVQEDState *s = acb_to_s(acb);
trace_qed_aio_complete(s, acb, ret);
/* Free resources */ /* Free resources */
qemu_iovec_destroy(&acb->cur_qiov); qemu_iovec_destroy(&acb->cur_qiov);
qed_unref_l2_cache_entry(acb->request.l2_table); qed_unref_l2_cache_entry(acb->request.l2_table);
@ -920,11 +891,6 @@ static void qed_aio_complete(QEDAIOCB *acb, int ret)
acb->qiov->iov[0].iov_base = NULL; acb->qiov->iov[0].iov_base = NULL;
} }
/* Arrange for a bh to invoke the completion function */
acb->bh_ret = ret;
aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
qed_aio_complete_bh, acb);
/* Start next allocating write request waiting behind this one. Note that /* Start next allocating write request waiting behind this one. Note that
* requests enqueue themselves when they first hit an unallocated cluster * requests enqueue themselves when they first hit an unallocated cluster
* but they wait until the entire request is finished before waking up the * but they wait until the entire request is finished before waking up the
@ -1172,7 +1138,7 @@ static int qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
struct iovec *iov = acb->qiov->iov; struct iovec *iov = acb->qiov->iov;
if (!iov->iov_base) { if (!iov->iov_base) {
iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len); iov->iov_base = qemu_try_blockalign(acb->bs, iov->iov_len);
if (iov->iov_base == NULL) { if (iov->iov_base == NULL) {
return -ENOMEM; return -ENOMEM;
} }
@ -1231,7 +1197,7 @@ static int qed_aio_read_data(void *opaque, int ret, uint64_t offset, size_t len)
{ {
QEDAIOCB *acb = opaque; QEDAIOCB *acb = opaque;
BDRVQEDState *s = acb_to_s(acb); BDRVQEDState *s = acb_to_s(acb);
BlockDriverState *bs = acb->common.bs; BlockDriverState *bs = acb->bs;
/* Adjust offset into cluster */ /* Adjust offset into cluster */
offset += qed_offset_into_cluster(s, acb->cur_pos); offset += qed_offset_into_cluster(s, acb->cur_pos);
@ -1260,7 +1226,7 @@ static int qed_aio_read_data(void *opaque, int ret, uint64_t offset, size_t len)
/** /**
* Begin next I/O or complete the request * Begin next I/O or complete the request
*/ */
static void qed_aio_next_io(QEDAIOCB *acb) static int qed_aio_next_io(QEDAIOCB *acb)
{ {
BDRVQEDState *s = acb_to_s(acb); BDRVQEDState *s = acb_to_s(acb);
uint64_t offset; uint64_t offset;
@ -1282,16 +1248,15 @@ static void qed_aio_next_io(QEDAIOCB *acb)
/* Complete request */ /* Complete request */
if (acb->cur_pos >= acb->end_pos) { if (acb->cur_pos >= acb->end_pos) {
qed_aio_complete(acb, 0); ret = 0;
return; break;
} }
/* Find next cluster and start I/O */ /* Find next cluster and start I/O */
len = acb->end_pos - acb->cur_pos; len = acb->end_pos - acb->cur_pos;
ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset); ret = qed_find_cluster(s, &acb->request, acb->cur_pos, &len, &offset);
if (ret < 0) { if (ret < 0) {
qed_aio_complete(acb, ret); break;
return;
} }
if (acb->flags & QED_AIOCB_WRITE) { if (acb->flags & QED_AIOCB_WRITE) {
@ -1301,56 +1266,32 @@ static void qed_aio_next_io(QEDAIOCB *acb)
} }
if (ret < 0 && ret != -EAGAIN) { if (ret < 0 && ret != -EAGAIN) {
qed_aio_complete(acb, ret); break;
return;
} }
} }
}
typedef struct QEDRequestCo { trace_qed_aio_complete(s, acb, ret);
Coroutine *co; qed_aio_complete(acb);
bool done; return ret;
int ret;
} QEDRequestCo;
static void qed_co_request_cb(void *opaque, int ret)
{
QEDRequestCo *co = opaque;
co->done = true;
co->ret = ret;
qemu_coroutine_enter_if_inactive(co->co);
} }
static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num, static int coroutine_fn qed_co_request(BlockDriverState *bs, int64_t sector_num,
QEMUIOVector *qiov, int nb_sectors, QEMUIOVector *qiov, int nb_sectors,
int flags) int flags)
{ {
QEDRequestCo co = { QEDAIOCB acb = {
.co = qemu_coroutine_self(), .bs = bs,
.done = false, .cur_pos = (uint64_t) sector_num * BDRV_SECTOR_SIZE,
.end_pos = (sector_num + nb_sectors) * BDRV_SECTOR_SIZE,
.qiov = qiov,
.flags = flags,
}; };
QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, qed_co_request_cb, &co); qemu_iovec_init(&acb.cur_qiov, qiov->niov);
trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors, &co, flags); trace_qed_aio_setup(bs->opaque, &acb, sector_num, nb_sectors, NULL, flags);
acb->flags = flags;
acb->qiov = qiov;
acb->qiov_offset = 0;
acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
acb->backing_qiov = NULL;
acb->request.l2_table = NULL;
qemu_iovec_init(&acb->cur_qiov, qiov->niov);
/* Start request */ /* Start request */
qed_aio_start_io(acb); return qed_aio_next_io(&acb);
if (!co.done) {
qemu_coroutine_yield();
}
return co.ret;
} }
static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs, static int coroutine_fn bdrv_qed_co_readv(BlockDriverState *bs,

View File

@ -129,8 +129,7 @@ enum {
}; };
typedef struct QEDAIOCB { typedef struct QEDAIOCB {
BlockAIOCB common; BlockDriverState *bs;
int bh_ret; /* final return status for completion bh */
QSIMPLEQ_ENTRY(QEDAIOCB) next; /* next request */ QSIMPLEQ_ENTRY(QEDAIOCB) next; /* next request */
int flags; /* QED_AIOCB_* bits ORed together */ int flags; /* QED_AIOCB_* bits ORed together */
uint64_t end_pos; /* request end on block device, in bytes */ uint64_t end_pos; /* request end on block device, in bytes */