From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=56951 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PgZnD-00036I-W9 for qemu-devel@nongnu.org; Sat, 22 Jan 2011 04:30:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PgZnA-0001my-MY for qemu-devel@nongnu.org; Sat, 22 Jan 2011 04:29:59 -0500 Received: from mtagate3.uk.ibm.com ([194.196.100.163]:47069) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PgZnA-0001mc-FE for qemu-devel@nongnu.org; Sat, 22 Jan 2011 04:29:56 -0500 Received: from d06nrmr1307.portsmouth.uk.ibm.com (d06nrmr1307.portsmouth.uk.ibm.com [9.149.38.129]) by mtagate3.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p0M9Ttnq009778 for ; Sat, 22 Jan 2011 09:29:55 GMT Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p0M9TwDJ1429542 for ; Sat, 22 Jan 2011 09:29:58 GMT Received: from d06av07.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p0M9Tt0w028240 for ; Sat, 22 Jan 2011 02:29:55 -0700 From: Stefan Hajnoczi Date: Sat, 22 Jan 2011 09:29:25 +0000 Message-Id: <1295688567-25496-11-git-send-email-stefanha@linux.vnet.ibm.com> In-Reply-To: <1295688567-25496-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1295688567-25496-1-git-send-email-stefanha@linux.vnet.ibm.com> Subject: [Qemu-devel] [RFC][PATCH 10/12] block: Add coroutine support to synchronous I/O functions List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi Implement transparent support for the following synchronous I/O functions to be called from coroutines: bdrv_read() bdrv_write() bdrv_pread() bdrv_pwrite() bdrv_pwrite_sync() bdrv_write_sync() bdrv_flush() These functions will use asynchronous I/O behind the scenes but otherwise behave the same as the truly synchronous implementations. This change allows synchronous I/O code in QEMU to just work in a coroutine. Signed-off-by: Stefan Hajnoczi --- block.c | 42 ++++++++++++++++++++++++++++++++++++++++++ block.h | 1 + 2 files changed, 43 insertions(+), 0 deletions(-) diff --git a/block.c b/block.c index ecb6538..f955ded 100644 --- a/block.c +++ b/block.c @@ -924,6 +924,17 @@ int bdrv_read(BlockDriverState *bs, int64_t sector_num, { BlockDriver *drv = bs->drv; + if (qemu_in_coroutine()) { + QEMUIOVector qiov; + struct iovec iov = { + .iov_base = buf, + .iov_len = nb_sectors * BDRV_SECTOR_SIZE, + }; + + qemu_iovec_init_external(&qiov, &iov, 1); + return bdrv_co_readv(bs, sector_num, &qiov, nb_sectors); + } + if (!drv) return -ENOMEDIUM; if (bdrv_check_request(bs, sector_num, nb_sectors)) @@ -970,6 +981,18 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors) { BlockDriver *drv = bs->drv; + + if (qemu_in_coroutine()) { + QEMUIOVector qiov; + struct iovec iov = { + .iov_base = (void *)buf, + .iov_len = nb_sectors * BDRV_SECTOR_SIZE, + }; + + qemu_iovec_init_external(&qiov, &iov, 1); + return bdrv_co_writev(bs, sector_num, &qiov, nb_sectors); + } + if (!bs->drv) return -ENOMEDIUM; if (bs->read_only) @@ -1471,6 +1494,10 @@ const char *bdrv_get_device_name(BlockDriverState *bs) int bdrv_flush(BlockDriverState *bs) { + if (qemu_in_coroutine()) { + return bdrv_co_flush(bs); + } + if (bs->open_flags & BDRV_O_NO_FLUSH) { return 0; } @@ -2664,6 +2691,21 @@ int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, return bdrv_co_io(bs, sector_num, iov, nb_sectors, 1); } +int coroutine_fn bdrv_co_flush(BlockDriverState *bs) +{ + CoroutineIOCompletion co = { + .coroutine = qemu_coroutine_self(), + }; + BlockDriverAIOCB *acb; + + acb = bdrv_aio_flush(bs, bdrv_co_complete, &co); + if (!acb) { + return -EIO; + } + qemu_coroutine_yield(NULL); + return co.ret; +} + /**************************************************************/ /* removable device support */ diff --git a/block.h b/block.h index 472e3d4..bced0c5 100644 --- a/block.h +++ b/block.h @@ -126,6 +126,7 @@ int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *iov, int nb_sectors); int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *iov, int nb_sectors); +int coroutine_fn bdrv_co_flush(BlockDriverState *bs); typedef struct BlockRequest { /* Fields to be filled by multiwrite caller */ -- 1.7.2.3