Skip to content

Commit

Permalink
block: Convert bdrv_is_inserted() to co_wrapper
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Kevin Wolf <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Emanuele Giuseppe Esposito <[email protected]>
Signed-off-by: Kevin Wolf <[email protected]>
  • Loading branch information
esposem authored and kevmw committed Feb 1, 2023
1 parent 09d9fc9 commit 1e97be9
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
8 changes: 4 additions & 4 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -6782,7 +6782,7 @@ int bdrv_inactivate_all(void)
/**
* 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;
Expand All @@ -6791,11 +6791,11 @@ bool bdrv_is_inserted(BlockDriverState *bs)
if (!drv) {
return false;
}
if (drv->bdrv_is_inserted) {
return drv->bdrv_is_inserted(bs);
if (drv->bdrv_co_is_inserted) {
return drv->bdrv_co_is_inserted(bs);
}
QLIST_FOREACH(child, &bs->children, next) {
if (!bdrv_is_inserted(child->bs)) {
if (!bdrv_co_is_inserted(child->bs)) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions block/block-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1983,12 +1983,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)
Expand Down
8 changes: 4 additions & 4 deletions block/file-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -3757,7 +3757,7 @@ static int cdrom_probe_device(const char *filename)
return prio;
}

static bool cdrom_is_inserted(BlockDriverState *bs)
static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
int ret;
Expand Down Expand Up @@ -3824,7 +3824,7 @@ static BlockDriver bdrv_host_cdrom = {
= raw_get_allocated_file_size,

/* removable device support */
.bdrv_is_inserted = cdrom_is_inserted,
.bdrv_co_is_inserted = cdrom_co_is_inserted,
.bdrv_eject = cdrom_eject,
.bdrv_lock_medium = cdrom_lock_medium,

Expand Down Expand Up @@ -3883,7 +3883,7 @@ static int cdrom_reopen(BlockDriverState *bs)
return 0;
}

static bool cdrom_is_inserted(BlockDriverState *bs)
static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
{
return raw_getlength(bs) > 0;
}
Expand Down Expand Up @@ -3954,7 +3954,7 @@ static BlockDriver bdrv_host_cdrom = {
= raw_get_allocated_file_size,

/* removable device support */
.bdrv_is_inserted = cdrom_is_inserted,
.bdrv_co_is_inserted = cdrom_co_is_inserted,
.bdrv_eject = cdrom_eject,
.bdrv_lock_medium = cdrom_lock_medium,
};
Expand Down
12 changes: 6 additions & 6 deletions block/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,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;
}

Expand Down Expand Up @@ -2067,7 +2067,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;
}

Expand Down Expand Up @@ -2835,7 +2835,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;
}
Expand Down Expand Up @@ -2959,7 +2959,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;
}

Expand Down Expand Up @@ -3241,7 +3241,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);
Expand All @@ -3252,7 +3252,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);
Expand Down
8 changes: 7 additions & 1 deletion blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
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) {
Expand All @@ -1035,11 +1036,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;
}

Expand Down
5 changes: 4 additions & 1 deletion include/block/block-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,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);
Expand Down
2 changes: 1 addition & 1 deletion include/block/block_int-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ struct BlockDriver {
BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);

/* removable device specific */
bool (*bdrv_is_inserted)(BlockDriverState *bs);
bool coroutine_fn (*bdrv_co_is_inserted)(BlockDriverState *bs);
void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);

Expand Down
5 changes: 4 additions & 1 deletion include/sysemu/block-backend-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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);
Expand Down

0 comments on commit 1e97be9

Please sign in to comment.