[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 4/9] block/io: move bdrv_rw_co_entry and friends down
From: |
Vladimir Sementsov-Ogievskiy |
Subject: |
[PATCH v2 4/9] block/io: move bdrv_rw_co_entry and friends down |
Date: |
Mon, 27 Apr 2020 17:39:02 +0300 |
We are going to use bdrv_co_pwritev_part and bdrv_co_preadv_part in
bdrv_rw_co_entry, so move it down.
Note: Comment formatting was changed to conform to coding style and
function order was changed. Otherwise the code is unmodified.
Signed-off-by: Vladimir Sementsov-Ogievskiy <address@hidden>
Reviewed-by: Stefan Hajnoczi <address@hidden>
---
block/io.c | 361 +++++++++++++++++++++++++++--------------------------
1 file changed, 181 insertions(+), 180 deletions(-)
diff --git a/block/io.c b/block/io.c
index 1134f8144a..061f3f2590 100644
--- a/block/io.c
+++ b/block/io.c
@@ -891,186 +891,6 @@ static int bdrv_check_byte_request(BlockDriverState *bs,
int64_t offset,
return 0;
}
-typedef struct RwCo {
- BdrvChild *child;
- int64_t offset;
- QEMUIOVector *qiov;
- bool is_write;
- int ret;
- BdrvRequestFlags flags;
-} RwCo;
-
-static void coroutine_fn bdrv_rw_co_entry(void *opaque)
-{
- RwCo *rwco = opaque;
-
- if (!rwco->is_write) {
- rwco->ret = bdrv_co_preadv(rwco->child, rwco->offset,
- rwco->qiov->size, rwco->qiov,
- rwco->flags);
- } else {
- rwco->ret = bdrv_co_pwritev(rwco->child, rwco->offset,
- rwco->qiov->size, rwco->qiov,
- rwco->flags);
- }
- aio_wait_kick();
-}
-
-/*
- * Process a vectored synchronous request using coroutines
- */
-static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
- QEMUIOVector *qiov, bool is_write,
- BdrvRequestFlags flags)
-{
- Coroutine *co;
- RwCo rwco = {
- .child = child,
- .offset = offset,
- .qiov = qiov,
- .is_write = is_write,
- .ret = NOT_DONE,
- .flags = flags,
- };
-
- if (qemu_in_coroutine()) {
- /* Fast-path if already in coroutine context */
- bdrv_rw_co_entry(&rwco);
- } else {
- co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco);
- bdrv_coroutine_enter(child->bs, co);
- BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
- }
- return rwco.ret;
-}
-
-int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
- int bytes, BdrvRequestFlags flags)
-{
- QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
-
- return bdrv_prwv_co(child, offset, &qiov, true,
- BDRV_REQ_ZERO_WRITE | flags);
-}
-
-/*
- * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
- * The operation is sped up by checking the block status and only writing
- * zeroes to the device if they currently do not return zeroes. Optional
- * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
- * BDRV_REQ_FUA).
- *
- * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
- */
-int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
-{
- int ret;
- int64_t target_size, bytes, offset = 0;
- BlockDriverState *bs = child->bs;
-
- target_size = bdrv_getlength(bs);
- if (target_size < 0) {
- return target_size;
- }
-
- for (;;) {
- bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES);
- if (bytes <= 0) {
- return 0;
- }
- ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
- if (ret < 0) {
- return ret;
- }
- if (ret & BDRV_BLOCK_ZERO) {
- offset += bytes;
- continue;
- }
- ret = bdrv_pwrite_zeroes(child, offset, bytes, flags);
- if (ret < 0) {
- return ret;
- }
- offset += bytes;
- }
-}
-
-int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
-{
- int ret;
-
- ret = bdrv_prwv_co(child, offset, qiov, false, 0);
- if (ret < 0) {
- return ret;
- }
-
- return qiov->size;
-}
-
-/* See bdrv_pwrite() for the return codes */
-int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
-{
- QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
-
- if (bytes < 0) {
- return -EINVAL;
- }
-
- return bdrv_preadv(child, offset, &qiov);
-}
-
-int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
-{
- int ret;
-
- ret = bdrv_prwv_co(child, offset, qiov, true, 0);
- if (ret < 0) {
- return ret;
- }
-
- return qiov->size;
-}
-
-/* Return no. of bytes on success or < 0 on error. Important errors are:
- -EIO generic I/O error (may happen for all errors)
- -ENOMEDIUM No media inserted.
- -EINVAL Invalid offset or number of bytes
- -EACCES Trying to write a read-only device
-*/
-int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes)
-{
- QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
-
- if (bytes < 0) {
- return -EINVAL;
- }
-
- return bdrv_pwritev(child, offset, &qiov);
-}
-
-/*
- * Writes to the file and ensures that no writes are reordered across this
- * request (acts as a barrier)
- *
- * Returns 0 on success, -errno in error cases.
- */
-int bdrv_pwrite_sync(BdrvChild *child, int64_t offset,
- const void *buf, int count)
-{
- int ret;
-
- ret = bdrv_pwrite(child, offset, buf, count);
- if (ret < 0) {
- return ret;
- }
-
- ret = bdrv_flush(child->bs);
- if (ret < 0) {
- return ret;
- }
-
- return 0;
-}
-
typedef struct CoroutineIOCompletion {
Coroutine *coroutine;
int ret;
@@ -2185,6 +2005,187 @@ int coroutine_fn bdrv_co_pwrite_zeroes(BdrvChild
*child, int64_t offset,
BDRV_REQ_ZERO_WRITE | flags);
}
+typedef struct RwCo {
+ BdrvChild *child;
+ int64_t offset;
+ QEMUIOVector *qiov;
+ bool is_write;
+ int ret;
+ BdrvRequestFlags flags;
+} RwCo;
+
+static void coroutine_fn bdrv_rw_co_entry(void *opaque)
+{
+ RwCo *rwco = opaque;
+
+ if (!rwco->is_write) {
+ rwco->ret = bdrv_co_preadv(rwco->child, rwco->offset,
+ rwco->qiov->size, rwco->qiov,
+ rwco->flags);
+ } else {
+ rwco->ret = bdrv_co_pwritev(rwco->child, rwco->offset,
+ rwco->qiov->size, rwco->qiov,
+ rwco->flags);
+ }
+ aio_wait_kick();
+}
+
+/*
+ * Process a vectored synchronous request using coroutines
+ */
+static int bdrv_prwv_co(BdrvChild *child, int64_t offset,
+ QEMUIOVector *qiov, bool is_write,
+ BdrvRequestFlags flags)
+{
+ Coroutine *co;
+ RwCo rwco = {
+ .child = child,
+ .offset = offset,
+ .qiov = qiov,
+ .is_write = is_write,
+ .ret = NOT_DONE,
+ .flags = flags,
+ };
+
+ if (qemu_in_coroutine()) {
+ /* Fast-path if already in coroutine context */
+ bdrv_rw_co_entry(&rwco);
+ } else {
+ co = qemu_coroutine_create(bdrv_rw_co_entry, &rwco);
+ bdrv_coroutine_enter(child->bs, co);
+ BDRV_POLL_WHILE(child->bs, rwco.ret == NOT_DONE);
+ }
+ return rwco.ret;
+}
+
+int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,
+ int bytes, BdrvRequestFlags flags)
+{
+ QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, NULL, bytes);
+
+ return bdrv_prwv_co(child, offset, &qiov, true,
+ BDRV_REQ_ZERO_WRITE | flags);
+}
+
+/* See bdrv_pwrite() for the return codes */
+int bdrv_pread(BdrvChild *child, int64_t offset, void *buf, int bytes)
+{
+ QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
+
+ if (bytes < 0) {
+ return -EINVAL;
+ }
+
+ return bdrv_preadv(child, offset, &qiov);
+}
+
+int bdrv_pwritev(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
+{
+ int ret;
+
+ ret = bdrv_prwv_co(child, offset, qiov, true, 0);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return qiov->size;
+}
+
+/*
+ * Return no. of bytes on success or < 0 on error. Important errors are:
+ * -EIO generic I/O error (may happen for all errors)
+ * -ENOMEDIUM No media inserted.
+ * -EINVAL Invalid offset or number of bytes
+ * -EACCES Trying to write a read-only device
+ */
+int bdrv_pwrite(BdrvChild *child, int64_t offset, const void *buf, int bytes)
+{
+ QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes);
+
+ if (bytes < 0) {
+ return -EINVAL;
+ }
+
+ return bdrv_pwritev(child, offset, &qiov);
+}
+
+/*
+ * Writes to the file and ensures that no writes are reordered across this
+ * request (acts as a barrier)
+ *
+ * Returns 0 on success, -errno in error cases.
+ */
+int bdrv_pwrite_sync(BdrvChild *child, int64_t offset,
+ const void *buf, int count)
+{
+ int ret;
+
+ ret = bdrv_pwrite(child, offset, buf, count);
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = bdrv_flush(child->bs);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return 0;
+}
+
+/*
+ * Completely zero out a block device with the help of bdrv_pwrite_zeroes.
+ * The operation is sped up by checking the block status and only writing
+ * zeroes to the device if they currently do not return zeroes. Optional
+ * flags are passed through to bdrv_pwrite_zeroes (e.g. BDRV_REQ_MAY_UNMAP,
+ * BDRV_REQ_FUA).
+ *
+ * Returns < 0 on error, 0 on success. For error codes see bdrv_write().
+ */
+int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
+{
+ int ret;
+ int64_t target_size, bytes, offset = 0;
+ BlockDriverState *bs = child->bs;
+
+ target_size = bdrv_getlength(bs);
+ if (target_size < 0) {
+ return target_size;
+ }
+
+ for (;;) {
+ bytes = MIN(target_size - offset, BDRV_REQUEST_MAX_BYTES);
+ if (bytes <= 0) {
+ return 0;
+ }
+ ret = bdrv_block_status(bs, offset, bytes, &bytes, NULL, NULL);
+ if (ret < 0) {
+ return ret;
+ }
+ if (ret & BDRV_BLOCK_ZERO) {
+ offset += bytes;
+ continue;
+ }
+ ret = bdrv_pwrite_zeroes(child, offset, bytes, flags);
+ if (ret < 0) {
+ return ret;
+ }
+ offset += bytes;
+ }
+}
+
+int bdrv_preadv(BdrvChild *child, int64_t offset, QEMUIOVector *qiov)
+{
+ int ret;
+
+ ret = bdrv_prwv_co(child, offset, qiov, false, 0);
+ if (ret < 0) {
+ return ret;
+ }
+
+ return qiov->size;
+}
+
/*
* Flush ALL BDSes regardless of if they are reachable via a BlkBackend or not.
*/
--
2.21.0
- [PATCH v2 0/9] block/io: safer inc/dec in_flight sections, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 1/9] block/io: refactor bdrv_is_allocated_above to run only one coroutine, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 3/9] block/io: move flush and pdiscard stuff down, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 4/9] block/io: move bdrv_rw_co_entry and friends down,
Vladimir Sementsov-Ogievskiy <=
- [PATCH v2 6/9] block/io: expand in_flight inc/dec section: block-status, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 7/9] block/io: add bdrv_do_pwrite_zeroes, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 8/9] block/io: move bdrv_make_zero under block-status, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 2/9] block/io: refactor bdrv_co_ioctl: move aio stuff to corresponding block, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 5/9] block/io: expand in_flight inc/dec section: simple cases, Vladimir Sementsov-Ogievskiy, 2020/04/27
- [PATCH v2 9/9] block/io: expand in_flight inc/dec section: bdrv_make_zero, Vladimir Sementsov-Ogievskiy, 2020/04/27