From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 15/46] vdi: basic conversion to coroutines
Date: Thu, 5 Apr 2012 17:51:53 +0200 [thread overview]
Message-ID: <1333641144-13612-16-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1333641144-13612-1-git-send-email-kwolf@redhat.com>
From: Paolo Bonzini <pbonzini@redhat.com>
Even a basic conversion changing the bdrv_aio_readv/bdrv_aio_writev calls
to bdrv_co_readv/bdrv_co_writev, and callbacks to goto statements can
eliminate a lot of code. This is because error handling is simplified
and indirections through bottom halves can go away.
After this patch, I/O to the underlying file already happens via
coroutines, but the code still looks a lot like if asynchronous I/O was
being used.
Acked-by: Stefan Weil <sw@weilnetz.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/vdi.c | 158 ++++++++++++++---------------------------------------------
1 files changed, 37 insertions(+), 121 deletions(-)
diff --git a/block/vdi.c b/block/vdi.c
index f8fa865..5fd8700 100644
--- a/block/vdi.c
+++ b/block/vdi.c
@@ -160,7 +160,6 @@ typedef struct {
void *orig_buf;
bool is_write;
int header_modified;
- BlockDriverAIOCB *hd_aiocb;
struct iovec hd_iov;
QEMUIOVector hd_qiov;
QEMUBH *bh;
@@ -489,33 +488,19 @@ static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs,
return VDI_IS_ALLOCATED(bmap_entry);
}
-static void vdi_aio_cancel(BlockDriverAIOCB *blockacb)
-{
- /* TODO: This code is untested. How can I get it executed? */
- VdiAIOCB *acb = container_of(blockacb, VdiAIOCB, common);
- logout("\n");
- if (acb->hd_aiocb) {
- bdrv_aio_cancel(acb->hd_aiocb);
- }
- qemu_aio_release(acb);
-}
-
static AIOPool vdi_aio_pool = {
.aiocb_size = sizeof(VdiAIOCB),
- .cancel = vdi_aio_cancel,
};
static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
- QEMUIOVector *qiov, int nb_sectors,
- BlockDriverCompletionFunc *cb, void *opaque, int is_write)
+ QEMUIOVector *qiov, int nb_sectors, int is_write)
{
VdiAIOCB *acb;
logout("%p, %" PRId64 ", %p, %d, %p, %p, %d\n",
- bs, sector_num, qiov, nb_sectors, cb, opaque, is_write);
+ bs, sector_num, qiov, nb_sectors, is_write);
- acb = qemu_aio_get(&vdi_aio_pool, bs, cb, opaque);
- acb->hd_aiocb = NULL;
+ acb = qemu_aio_get(&vdi_aio_pool, bs, NULL, NULL);
acb->sector_num = sector_num;
acb->qiov = qiov;
acb->is_write = is_write;
@@ -538,42 +523,7 @@ static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
return acb;
}
-static int vdi_schedule_bh(QEMUBHFunc *cb, VdiAIOCB *acb)
-{
- logout("\n");
-
- if (acb->bh) {
- return -EIO;
- }
-
- acb->bh = qemu_bh_new(cb, acb);
- if (!acb->bh) {
- return -EIO;
- }
-
- qemu_bh_schedule(acb->bh);
-
- return 0;
-}
-
-static void vdi_aio_read_cb(void *opaque, int ret);
-static void vdi_aio_write_cb(void *opaque, int ret);
-
-static void vdi_aio_rw_bh(void *opaque)
-{
- VdiAIOCB *acb = opaque;
- logout("\n");
- qemu_bh_delete(acb->bh);
- acb->bh = NULL;
-
- if (acb->is_write) {
- vdi_aio_write_cb(opaque, 0);
- } else {
- vdi_aio_read_cb(opaque, 0);
- }
-}
-
-static void vdi_aio_read_cb(void *opaque, int ret)
+static int vdi_aio_read_cb(void *opaque, int ret)
{
VdiAIOCB *acb = opaque;
BlockDriverState *bs = acb->common.bs;
@@ -585,12 +535,7 @@ static void vdi_aio_read_cb(void *opaque, int ret)
logout("%u sectors read\n", acb->n_sectors);
- acb->hd_aiocb = NULL;
-
- if (ret < 0) {
- goto done;
- }
-
+restart:
acb->nb_sectors -= acb->n_sectors;
if (acb->nb_sectors == 0) {
@@ -618,10 +563,7 @@ static void vdi_aio_read_cb(void *opaque, int ret)
if (!VDI_IS_ALLOCATED(bmap_entry)) {
/* Block not allocated, return zeros, no need to wait. */
memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
- ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
- if (ret < 0) {
- goto done;
- }
+ ret = 0;
} else {
uint64_t offset = s->header.offset_data / SECTOR_SIZE +
(uint64_t)bmap_entry * s->block_sectors +
@@ -629,41 +571,34 @@ static void vdi_aio_read_cb(void *opaque, int ret)
acb->hd_iov.iov_base = (void *)acb->buf;
acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
- acb->hd_aiocb = bdrv_aio_readv(bs->file, offset, &acb->hd_qiov,
- n_sectors, vdi_aio_read_cb, acb);
+ ret = bdrv_co_readv(bs->file, offset, n_sectors, &acb->hd_qiov);
+ }
+ if (ret >= 0) {
+ goto restart;
}
- return;
+
done:
if (acb->qiov->niov > 1) {
qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
qemu_vfree(acb->orig_buf);
}
- acb->common.cb(acb->common.opaque, ret);
qemu_aio_release(acb);
+ return ret;
}
-static BlockDriverAIOCB *vdi_aio_readv(BlockDriverState *bs,
- int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
- BlockDriverCompletionFunc *cb, void *opaque)
+static int vdi_co_readv(BlockDriverState *bs,
+ int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
{
VdiAIOCB *acb;
int ret;
logout("\n");
- acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
- ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
- if (ret < 0) {
- if (acb->qiov->niov > 1) {
- qemu_vfree(acb->orig_buf);
- }
- qemu_aio_release(acb);
- return NULL;
- }
-
- return &acb->common;
+ acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 0);
+ ret = vdi_aio_read_cb(acb, 0);
+ return ret;
}
-static void vdi_aio_write_cb(void *opaque, int ret)
+static int vdi_aio_write_cb(void *opaque, int ret)
{
VdiAIOCB *acb = opaque;
BlockDriverState *bs = acb->common.bs;
@@ -673,12 +608,7 @@ static void vdi_aio_write_cb(void *opaque, int ret)
uint32_t sector_in_block;
uint32_t n_sectors;
- acb->hd_aiocb = NULL;
-
- if (ret < 0) {
- goto done;
- }
-
+restart:
acb->nb_sectors -= acb->n_sectors;
acb->sector_num += acb->n_sectors;
acb->buf += acb->n_sectors * SECTOR_SIZE;
@@ -686,6 +616,7 @@ static void vdi_aio_write_cb(void *opaque, int ret)
if (acb->nb_sectors == 0) {
logout("finished data write\n");
acb->n_sectors = 0;
+ ret = 0;
if (acb->header_modified) {
VdiHeader *header = acb->block_buffer;
logout("now writing modified header\n");
@@ -696,10 +627,9 @@ static void vdi_aio_write_cb(void *opaque, int ret)
acb->hd_iov.iov_base = acb->block_buffer;
acb->hd_iov.iov_len = SECTOR_SIZE;
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
- acb->hd_aiocb = bdrv_aio_writev(bs->file, 0, &acb->hd_qiov, 1,
- vdi_aio_write_cb, acb);
- return;
- } else if (VDI_IS_ALLOCATED(acb->bmap_first)) {
+ ret = bdrv_co_writev(bs->file, 0, 1, &acb->hd_qiov);
+ }
+ if (ret >= 0 && VDI_IS_ALLOCATED(acb->bmap_first)) {
/* One or more new blocks were allocated. */
uint64_t offset;
uint32_t bmap_first;
@@ -722,11 +652,8 @@ static void vdi_aio_write_cb(void *opaque, int ret)
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
logout("will write %u block map sectors starting from entry %u\n",
n_sectors, bmap_first);
- acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
- n_sectors, vdi_aio_write_cb, acb);
- return;
+ ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
}
- ret = 0;
goto done;
}
@@ -772,9 +699,7 @@ static void vdi_aio_write_cb(void *opaque, int ret)
acb->hd_iov.iov_base = (void *)block;
acb->hd_iov.iov_len = s->block_size;
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
- acb->hd_aiocb = bdrv_aio_writev(bs->file, offset,
- &acb->hd_qiov, s->block_sectors,
- vdi_aio_write_cb, acb);
+ ret = bdrv_co_writev(bs->file, offset, s->block_sectors, &acb->hd_qiov);
} else {
uint64_t offset = s->header.offset_data / SECTOR_SIZE +
(uint64_t)bmap_entry * s->block_sectors +
@@ -782,39 +707,30 @@ static void vdi_aio_write_cb(void *opaque, int ret)
acb->hd_iov.iov_base = (void *)acb->buf;
acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
- acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
- n_sectors, vdi_aio_write_cb, acb);
+ ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
+ }
+ if (ret >= 0) {
+ goto restart;
}
-
- return;
done:
if (acb->qiov->niov > 1) {
qemu_vfree(acb->orig_buf);
}
- acb->common.cb(acb->common.opaque, ret);
qemu_aio_release(acb);
+ return ret;
}
-static BlockDriverAIOCB *vdi_aio_writev(BlockDriverState *bs,
- int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
- BlockDriverCompletionFunc *cb, void *opaque)
+static int vdi_co_writev(BlockDriverState *bs,
+ int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
{
VdiAIOCB *acb;
int ret;
logout("\n");
- acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
- ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
- if (ret < 0) {
- if (acb->qiov->niov > 1) {
- qemu_vfree(acb->orig_buf);
- }
- qemu_aio_release(acb);
- return NULL;
- }
-
- return &acb->common;
+ acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 1);
+ ret = vdi_aio_write_cb(acb, 0);
+ return ret;
}
static int vdi_create(const char *filename, QEMUOptionParameter *options)
@@ -965,9 +881,9 @@ static BlockDriver bdrv_vdi = {
.bdrv_co_is_allocated = vdi_co_is_allocated,
.bdrv_make_empty = vdi_make_empty,
- .bdrv_aio_readv = vdi_aio_readv,
+ .bdrv_co_readv = vdi_co_readv,
#if defined(CONFIG_VDI_WRITE)
- .bdrv_aio_writev = vdi_aio_writev,
+ .bdrv_co_writev = vdi_co_writev,
#endif
.bdrv_get_info = vdi_get_info,
--
1.7.6.5
next prev parent reply other threads:[~2012-04-05 16:35 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-05 15:51 [Qemu-devel] [PULL 00/46] Block patches Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 01/46] trace-events: Rename 'next' argument Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 02/46] tracetool: Forbid argument name 'next' Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 03/46] qcow2: Remove unused parameter in get_cluster_table() Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 04/46] qemu-io: add option to enable tracing Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 05/46] block: push recursive flushing up from drivers Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 07/46] ide: IDENTIFY word 86 bit 14 is reserved Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 08/46] ide: Add "model=s" qdev option Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 09/46] ide: Change serial number strncpy() to pstrcpy() Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 10/46] ide: Adds wwn=hex qdev option Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 11/46] block/vpc: write checksum back to footer after check Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 12/46] qerror: fix QERR_PROPERTY_VALUE_OUT_OF_RANGE description Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 13/46] qdev: add blocksize property type Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 14/46] block: enforce constraints on block size properties Kevin Wolf
2012-04-05 15:51 ` Kevin Wolf [this message]
2012-04-05 15:51 ` [Qemu-devel] [PATCH 16/46] vdi: move end-of-I/O handling at the end Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 17/46] vdi: merge aio_read_cb and aio_write_cb into callers Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 18/46] vdi: move aiocb fields to locals Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 19/46] vdi: leave bounce buffering to block layer Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 20/46] vdi: do not create useless iovecs Kevin Wolf
2012-04-05 15:51 ` [Qemu-devel] [PATCH 21/46] vdi: change goto to loop Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 22/46] Use DMADirection type for dma_bdrv_io Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 23/46] block: disable I/O throttling on sync api Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 25/46] block: fix streaming/closing race Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 27/46] block: document job API Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 28/46] qemu-img: add image fragmentation statistics Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 29/46] qed: " Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 30/46] qemu-img: add dirty flag status Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 31/46] qed: track " Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 32/46] block: bdrv_append() fixes Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 33/46] sheepdog: implement SD_OP_FLUSH_VDI operation Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 34/46] sheepdog: fix send req helpers Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 36/46] qemu-iotests: Test unknown qcow2 header extensions Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 37/46] qemu-iotests: Fix call syntax for qemu-img Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 38/46] qemu-iotests: Fix call syntax for qemu-io Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 40/46] block: Add new BDRV_O_INCOMING flag to notice incoming live migration Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 42/46] blockdev: open images with BDRV_O_INCOMING on " Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 43/46] qed: add bdrv_invalidate_cache to be called after " Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 44/46] migration: clear BDRV_O_INCOMING flags on end of " Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 45/46] qed: honor BDRV_O_INCOMING for " Kevin Wolf
2012-04-05 15:52 ` [Qemu-devel] [PATCH 46/46] qed: remove incoming live migration blocker Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1333641144-13612-16-git-send-email-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=anthony@codemonkey.ws \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).