[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC][PATCH 10/12] block: Add coroutine support to synchron
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [RFC][PATCH 10/12] block: Add coroutine support to synchronous I/O functions |
Date: |
Sat, 22 Jan 2011 09:29:25 +0000 |
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 <address@hidden>
---
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
- [Qemu-devel] [RFC][PATCH 00/12] qcow2: Convert qcow2 to use coroutines for async I/O, Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 07/12] coroutine: Add coroutine_is_leader(), Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 08/12] coroutine: Add qemu_in_coroutine(), Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 04/12] coroutine: Use thread-local leader and current variables, Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 09/12] block: Add bdrv_co_readv() and bdrv_co_writev(), Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 10/12] block: Add coroutine support to synchronous I/O functions,
Stefan Hajnoczi <=
- [Qemu-devel] [RFC][PATCH 06/12] coroutine: Add qemu_coroutine_self(), Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 12/12] qcow2: Serialize all requests, Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 05/12] coroutine: Add coroutines, Stefan Hajnoczi, 2011/01/22
- [Qemu-devel] [RFC][PATCH 11/12] qcow2: Convert qcow2 to use coroutines for async I/O, Stefan Hajnoczi, 2011/01/22