From b2e12bc6e304c17da0bee970fb4776d0731422e6 Mon Sep 17 00:00:00 2001 From: Christoph Hellwig Date: Fri, 4 Sep 2009 19:01:49 +0200 Subject: [PATCH] block: add aio_flush operation Instead stalling the VCPU while serving a cache flush try to do it asynchronously. Use our good old helper thread pool to issue an asynchronous fdatasync for raw-posix. Note that while Linux AIO implements a fdatasync operation it is not useful for us because it isn't actually implement in asynchronous fashion. Signed-off-by: Christoph Hellwig Signed-off-by: Anthony Liguori --- block.c | 40 ++++++++++++++++++++++++++++++++++++++++ block.h | 2 ++ block/raw-posix-aio.h | 3 ++- block/raw-posix.c | 17 +++++++++++++++++ block_int.h | 2 ++ posix-aio-compat.c | 19 +++++++++++++++++-- 6 files changed, 80 insertions(+), 3 deletions(-) diff --git a/block.c b/block.c index da0930cbba..0c6a97bc9c 100644 --- a/block.c +++ b/block.c @@ -54,6 +54,8 @@ static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque); +static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, + BlockDriverCompletionFunc *cb, void *opaque); static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors); static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, @@ -138,6 +140,10 @@ void bdrv_register(BlockDriver *bdrv) bdrv->bdrv_read = bdrv_read_em; bdrv->bdrv_write = bdrv_write_em; } + + if (!bdrv->bdrv_aio_flush) + bdrv->bdrv_aio_flush = bdrv_aio_flush_em; + bdrv->next = first_drv; first_drv = bdrv; } @@ -1552,6 +1558,21 @@ fail: return -1; } +BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, + BlockDriverCompletionFunc *cb, void *opaque) +{ + BlockDriver *drv = bs->drv; + + if (!drv) + return NULL; + + /* + * Note that unlike bdrv_flush the driver is reponsible for flushing a + * backing image if it exists. + */ + return drv->bdrv_aio_flush(bs, cb, opaque); +} + void bdrv_aio_cancel(BlockDriverAIOCB *acb) { acb->pool->cancel(acb); @@ -1642,6 +1663,25 @@ static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); } +static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, + BlockDriverCompletionFunc *cb, void *opaque) +{ + BlockDriverAIOCBSync *acb; + + acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); + acb->is_write = 1; /* don't bounce in the completion hadler */ + acb->qiov = NULL; + acb->bounce = NULL; + acb->ret = 0; + + if (!acb->bh) + acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); + + bdrv_flush(bs); + qemu_bh_schedule(acb->bh); + return &acb->common; +} + /**************************************************************/ /* sync block device emulation */ diff --git a/block.h b/block.h index 0ae2526531..a966afb090 100644 --- a/block.h +++ b/block.h @@ -85,6 +85,8 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *iov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque); +BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, + BlockDriverCompletionFunc *cb, void *opaque); void bdrv_aio_cancel(BlockDriverAIOCB *acb); typedef struct BlockRequest { diff --git a/block/raw-posix-aio.h b/block/raw-posix-aio.h index 244bc8b798..a2d4348ee2 100644 --- a/block/raw-posix-aio.h +++ b/block/raw-posix-aio.h @@ -17,8 +17,9 @@ #define QEMU_AIO_READ 0x0001 #define QEMU_AIO_WRITE 0x0002 #define QEMU_AIO_IOCTL 0x0004 +#define QEMU_AIO_FLUSH 0x0008 #define QEMU_AIO_TYPE_MASK \ - (QEMU_AIO_READ|QEMU_AIO_WRITE|QEMU_AIO_IOCTL) + (QEMU_AIO_READ|QEMU_AIO_WRITE|QEMU_AIO_IOCTL|QEMU_AIO_FLUSH) /* AIO flags */ #define QEMU_AIO_MISALIGNED 0x1000 diff --git a/block/raw-posix.c b/block/raw-posix.c index 2ebc88f004..f612340fa4 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -574,6 +574,18 @@ static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs, cb, opaque, QEMU_AIO_WRITE); } +static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs, + BlockDriverCompletionFunc *cb, void *opaque) +{ + BDRVRawState *s = bs->opaque; + + if (fd_open(bs) < 0) + return NULL; + + return paio_submit(bs, s->aio_ctx, s->fd, 0, NULL, 0, + cb, opaque, QEMU_AIO_FLUSH); +} + static void raw_close(BlockDriverState *bs) { BDRVRawState *s = bs->opaque; @@ -749,6 +761,7 @@ static BlockDriver bdrv_raw = { .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev, + .bdrv_aio_flush = raw_aio_flush, .bdrv_truncate = raw_truncate, .bdrv_getlength = raw_getlength, @@ -1002,6 +1015,7 @@ static BlockDriver bdrv_host_device = { .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev, + .bdrv_aio_flush = raw_aio_flush, .bdrv_read = raw_read, .bdrv_write = raw_write, @@ -1096,6 +1110,7 @@ static BlockDriver bdrv_host_floppy = { .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev, + .bdrv_aio_flush = raw_aio_flush, .bdrv_read = raw_read, .bdrv_write = raw_write, @@ -1176,6 +1191,7 @@ static BlockDriver bdrv_host_cdrom = { .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev, + .bdrv_aio_flush = raw_aio_flush, .bdrv_read = raw_read, .bdrv_write = raw_write, @@ -1295,6 +1311,7 @@ static BlockDriver bdrv_host_cdrom = { .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev, + .bdrv_aio_flush = raw_aio_flush, .bdrv_read = raw_read, .bdrv_write = raw_write, diff --git a/block_int.h b/block_int.h index f95c80b906..8e72abee0c 100644 --- a/block_int.h +++ b/block_int.h @@ -69,6 +69,8 @@ struct BlockDriver { BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, BlockDriverCompletionFunc *cb, void *opaque); + BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs, + BlockDriverCompletionFunc *cb, void *opaque); int (*bdrv_aio_multiwrite)(BlockDriverState *bs, BlockRequest *reqs, int num_reqs); diff --git a/posix-aio-compat.c b/posix-aio-compat.c index 5ea197f668..498cc1f4cb 100644 --- a/posix-aio-compat.c +++ b/posix-aio-compat.c @@ -134,6 +134,16 @@ static size_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb) return aiocb->aio_nbytes; } +static size_t handle_aiocb_flush(struct qemu_paiocb *aiocb) +{ + int ret; + + ret = fdatasync(aiocb->aio_fildes); + if (ret == -1) + return -errno; + return 0; +} + #ifdef CONFIG_PREADV static ssize_t @@ -330,6 +340,9 @@ static void *aio_thread(void *unused) case QEMU_AIO_WRITE: ret = handle_aiocb_rw(aiocb); break; + case QEMU_AIO_FLUSH: + ret = handle_aiocb_flush(aiocb); + break; case QEMU_AIO_IOCTL: ret = handle_aiocb_ioctl(aiocb); break; @@ -530,8 +543,10 @@ BlockDriverAIOCB *paio_submit(BlockDriverState *bs, void *aio_ctx, int fd, acb->aio_type = type; acb->aio_fildes = fd; acb->ev_signo = SIGUSR2; - acb->aio_iov = qiov->iov; - acb->aio_niov = qiov->niov; + if (qiov) { + acb->aio_iov = qiov->iov; + acb->aio_niov = qiov->niov; + } acb->aio_nbytes = nb_sectors * 512; acb->aio_offset = sector_num * 512; -- GitLab