From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, hreitz@redhat.com, eesposit@redhat.com,
pbonzini@redhat.com, vsementsov@yandex-team.ru,
qemu-devel@nongnu.org
Subject: [PATCH 10/14] block: Convert bdrv_is_inserted() to co_wrapper
Date: Tue, 13 Dec 2022 09:53:16 +0100 [thread overview]
Message-ID: <20221213085320.95673-11-kwolf@redhat.com> (raw)
In-Reply-To: <20221213085320.95673-1-kwolf@redhat.com>
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
bdrv_is_inserted() is categorized as an I/O function, and it currently
doesn't run in a coroutine. We should let it take a graph rdlock since
it traverses the block nodes graph, which however is only possible in a
coroutine.
Therefore turn it into a co_wrapper to move the actual function into a
coroutine where the lock can be taken.
At the same time, add also blk_is_inserted as co_wrapper_mixed, since it
is called in both coroutine and non-coroutine contexts.
Because now this function creates a new coroutine and polls, we need to
take the AioContext lock where it is missing, for the only reason that
internally c_w_mixed_bdrv_rdlock calls AIO_WAIT_WHILE and it expects to
release the AioContext lock. Once the rwlock is ultimated and placed in
every place it needs to be, we will poll using AIO_WAIT_WHILE_UNLOCKED
and remove the AioContext lock.
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
include/block/block-io.h | 5 ++++-
include/block/block_int-common.h | 2 +-
include/sysemu/block-backend-io.h | 5 ++++-
block.c | 4 ++--
block/block-backend.c | 4 ++--
block/io.c | 12 ++++++------
blockdev.c | 8 +++++++-
7 files changed, 26 insertions(+), 14 deletions(-)
diff --git a/include/block/block-io.h b/include/block/block-io.h
index 1d6748463c..e27dc9787b 100644
--- a/include/block/block-io.h
+++ b/include/block/block-io.h
@@ -139,7 +139,10 @@ bool bdrv_is_read_only(BlockDriverState *bs);
bool bdrv_is_writable(BlockDriverState *bs);
bool bdrv_is_sg(BlockDriverState *bs);
int bdrv_get_flags(BlockDriverState *bs);
-bool bdrv_is_inserted(BlockDriverState *bs);
+
+bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs);
+bool co_wrapper bdrv_is_inserted(BlockDriverState *bs);
+
void bdrv_lock_medium(BlockDriverState *bs, bool locked);
void bdrv_eject(BlockDriverState *bs, bool eject_flag);
const char *bdrv_get_format_name(BlockDriverState *bs);
diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h
index 2c190c6d75..b954b3cb36 100644
--- a/include/block/block_int-common.h
+++ b/include/block/block_int-common.h
@@ -710,7 +710,7 @@ struct BlockDriver {
BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
/* removable device specific */
- bool (*bdrv_is_inserted)(BlockDriverState *bs);
+ bool coroutine_fn (*bdrv_is_inserted)(BlockDriverState *bs);
void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
diff --git a/include/sysemu/block-backend-io.h b/include/sysemu/block-backend-io.h
index 8ad8854ecb..a1eac6c00a 100644
--- a/include/sysemu/block-backend-io.h
+++ b/include/sysemu/block-backend-io.h
@@ -53,7 +53,10 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
void blk_inc_in_flight(BlockBackend *blk);
void blk_dec_in_flight(BlockBackend *blk);
-bool blk_is_inserted(BlockBackend *blk);
+
+bool coroutine_fn blk_co_is_inserted(BlockBackend *blk);
+bool co_wrapper_mixed blk_is_inserted(BlockBackend *blk);
+
bool blk_is_available(BlockBackend *blk);
void blk_lock_medium(BlockBackend *blk, bool locked);
void blk_eject(BlockBackend *blk, bool eject_flag);
diff --git a/block.c b/block.c
index 7694879a54..a7b9da6a7e 100644
--- a/block.c
+++ b/block.c
@@ -6797,7 +6797,7 @@ out:
/**
* Return TRUE if the media is present
*/
-bool bdrv_is_inserted(BlockDriverState *bs)
+bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs)
{
BlockDriver *drv = bs->drv;
BdrvChild *child;
@@ -6810,7 +6810,7 @@ bool bdrv_is_inserted(BlockDriverState *bs)
return drv->bdrv_is_inserted(bs);
}
QLIST_FOREACH(child, &bs->children, next) {
- if (!bdrv_is_inserted(child->bs)) {
+ if (!bdrv_co_is_inserted(child->bs)) {
return false;
}
}
diff --git a/block/block-backend.c b/block/block-backend.c
index 5f6b269a79..e4b54f7b0c 100644
--- a/block/block-backend.c
+++ b/block/block-backend.c
@@ -1985,12 +1985,12 @@ void blk_activate(BlockBackend *blk, Error **errp)
bdrv_activate(bs, errp);
}
-bool blk_is_inserted(BlockBackend *blk)
+bool coroutine_fn blk_co_is_inserted(BlockBackend *blk)
{
BlockDriverState *bs = blk_bs(blk);
IO_CODE();
- return bs && bdrv_is_inserted(bs);
+ return bs && bdrv_co_is_inserted(bs);
}
bool blk_is_available(BlockBackend *blk)
diff --git a/block/io.c b/block/io.c
index f988e42f26..116d6cf10b 100644
--- a/block/io.c
+++ b/block/io.c
@@ -1612,7 +1612,7 @@ int coroutine_fn bdrv_co_preadv_part(BdrvChild *child,
trace_bdrv_co_preadv_part(bs, offset, bytes, flags);
- if (!bdrv_is_inserted(bs)) {
+ if (!bdrv_co_is_inserted(bs)) {
return -ENOMEDIUM;
}
@@ -2057,7 +2057,7 @@ int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child,
trace_bdrv_co_pwritev_part(child->bs, offset, bytes, flags);
- if (!bdrv_is_inserted(bs)) {
+ if (!bdrv_co_is_inserted(bs)) {
return -ENOMEDIUM;
}
@@ -2825,7 +2825,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
bdrv_inc_in_flight(bs);
- if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
+ if (!bdrv_co_is_inserted(bs) || bdrv_is_read_only(bs) ||
bdrv_is_sg(bs)) {
goto early_exit;
}
@@ -2949,7 +2949,7 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
BlockDriverState *bs = child->bs;
IO_CODE();
- if (!bs || !bs->drv || !bdrv_is_inserted(bs)) {
+ if (!bs || !bs->drv || !bdrv_co_is_inserted(bs)) {
return -ENOMEDIUM;
}
@@ -3231,7 +3231,7 @@ static int coroutine_fn bdrv_co_copy_range_internal(
assert(!(read_flags & BDRV_REQ_NO_WAIT));
assert(!(write_flags & BDRV_REQ_NO_WAIT));
- if (!dst || !dst->bs || !bdrv_is_inserted(dst->bs)) {
+ if (!dst || !dst->bs || !bdrv_co_is_inserted(dst->bs)) {
return -ENOMEDIUM;
}
ret = bdrv_check_request32(dst_offset, bytes, NULL, 0);
@@ -3242,7 +3242,7 @@ static int coroutine_fn bdrv_co_copy_range_internal(
return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags);
}
- if (!src || !src->bs || !bdrv_is_inserted(src->bs)) {
+ if (!src || !src->bs || !bdrv_co_is_inserted(src->bs)) {
return -ENOMEDIUM;
}
ret = bdrv_check_request32(src_offset, bytes, NULL, 0);
diff --git a/blockdev.c b/blockdev.c
index 011e48df7b..cef3bc1215 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -1023,6 +1023,7 @@ fail:
static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
{
BlockDriverState *bs;
+ AioContext *aio_context;
bs = bdrv_lookup_bs(name, name, errp);
if (bs == NULL) {
@@ -1034,11 +1035,16 @@ static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
return NULL;
}
+ aio_context = bdrv_get_aio_context(bs);
+ aio_context_acquire(aio_context);
+
if (!bdrv_is_inserted(bs)) {
error_setg(errp, "Device has no medium");
- return NULL;
+ bs = NULL;
}
+ aio_context_release(aio_context);
+
return bs;
}
--
2.38.1
next prev parent reply other threads:[~2022-12-13 8:58 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-13 8:53 [PATCH 00/14] block: Move more functions to coroutines Kevin Wolf
2022-12-13 8:53 ` [PATCH 01/14] block-coroutine-wrapper: support void functions Kevin Wolf
2022-12-16 14:21 ` Vladimir Sementsov-Ogievskiy
2022-12-13 8:53 ` [PATCH 02/14] block: Convert bdrv_io_plug() to co_wrapper Kevin Wolf
2022-12-16 14:26 ` Vladimir Sementsov-Ogievskiy
2022-12-19 12:24 ` Emanuele Giuseppe Esposito
2022-12-16 16:12 ` Vladimir Sementsov-Ogievskiy
2022-12-19 12:26 ` Emanuele Giuseppe Esposito
2023-01-13 15:51 ` Kevin Wolf
2022-12-13 8:53 ` [PATCH 03/14] block: Convert bdrv_io_unplug() " Kevin Wolf
2022-12-13 8:53 ` [PATCH 04/14] block: Rename refresh_total_sectors to bdrv_refresh_total_sectors Kevin Wolf
2022-12-16 16:55 ` Vladimir Sementsov-Ogievskiy
2022-12-13 8:53 ` [PATCH 05/14] block: Convert bdrv_refresh_total_sectors() to co_wrapper_mixed Kevin Wolf
2022-12-16 17:19 ` Vladimir Sementsov-Ogievskiy
2022-12-13 8:53 ` [PATCH 06/14] block-backend: use bdrv_getlength instead of blk_getlength Kevin Wolf
2022-12-16 17:22 ` Vladimir Sementsov-Ogievskiy
2022-12-19 12:28 ` Emanuele Giuseppe Esposito
2022-12-13 8:53 ` [PATCH 07/14] block: use bdrv_co_refresh_total_sectors when possible Kevin Wolf
2022-12-16 17:26 ` Vladimir Sementsov-Ogievskiy
2022-12-13 8:53 ` [PATCH 08/14] block: Convert bdrv_get_allocated_file_size() to co_wrapper Kevin Wolf
2022-12-13 8:53 ` [PATCH 09/14] block: Convert bdrv_get_info() to co_wrapper_mixed Kevin Wolf
2022-12-13 8:53 ` Kevin Wolf [this message]
2022-12-13 8:53 ` [PATCH 11/14] block: Convert bdrv_eject() to co_wrapper Kevin Wolf
2022-12-13 8:53 ` [PATCH 12/14] block: convert bdrv_lock_medium in co_wrapper Kevin Wolf
2022-12-13 8:53 ` [PATCH 13/14] block: Convert bdrv_debug_event to co_wrapper_mixed Kevin Wolf
2022-12-13 8:53 ` [PATCH 14/14] block: Rename newly converted BlockDriver IO coroutine functions Kevin Wolf
2022-12-16 17:30 ` Vladimir Sementsov-Ogievskiy
2022-12-15 12:39 ` [PATCH 00/14] block: Move more functions to coroutines Emanuele Giuseppe Esposito
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=20221213085320.95673-11-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=eesposit@redhat.com \
--cc=hreitz@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@yandex-team.ru \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.