Skip to content

Commit 1e97be9

Browse files
esposemkevmw
authored andcommitted
block: Convert bdrv_is_inserted() to co_wrapper
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]>
1 parent 09d9fc9 commit 1e97be9

File tree

8 files changed

+32
-20
lines changed

8 files changed

+32
-20
lines changed

block.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -6782,7 +6782,7 @@ int bdrv_inactivate_all(void)
67826782
/**
67836783
* Return TRUE if the media is present
67846784
*/
6785-
bool bdrv_is_inserted(BlockDriverState *bs)
6785+
bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs)
67866786
{
67876787
BlockDriver *drv = bs->drv;
67886788
BdrvChild *child;
@@ -6791,11 +6791,11 @@ bool bdrv_is_inserted(BlockDriverState *bs)
67916791
if (!drv) {
67926792
return false;
67936793
}
6794-
if (drv->bdrv_is_inserted) {
6795-
return drv->bdrv_is_inserted(bs);
6794+
if (drv->bdrv_co_is_inserted) {
6795+
return drv->bdrv_co_is_inserted(bs);
67966796
}
67976797
QLIST_FOREACH(child, &bs->children, next) {
6798-
if (!bdrv_is_inserted(child->bs)) {
6798+
if (!bdrv_co_is_inserted(child->bs)) {
67996799
return false;
68006800
}
68016801
}

block/block-backend.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1983,12 +1983,12 @@ void blk_activate(BlockBackend *blk, Error **errp)
19831983
bdrv_activate(bs, errp);
19841984
}
19851985

1986-
bool blk_is_inserted(BlockBackend *blk)
1986+
bool coroutine_fn blk_co_is_inserted(BlockBackend *blk)
19871987
{
19881988
BlockDriverState *bs = blk_bs(blk);
19891989
IO_CODE();
19901990

1991-
return bs && bdrv_is_inserted(bs);
1991+
return bs && bdrv_co_is_inserted(bs);
19921992
}
19931993

19941994
bool blk_is_available(BlockBackend *blk)

block/file-posix.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -3757,7 +3757,7 @@ static int cdrom_probe_device(const char *filename)
37573757
return prio;
37583758
}
37593759

3760-
static bool cdrom_is_inserted(BlockDriverState *bs)
3760+
static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
37613761
{
37623762
BDRVRawState *s = bs->opaque;
37633763
int ret;
@@ -3824,7 +3824,7 @@ static BlockDriver bdrv_host_cdrom = {
38243824
= raw_get_allocated_file_size,
38253825

38263826
/* removable device support */
3827-
.bdrv_is_inserted = cdrom_is_inserted,
3827+
.bdrv_co_is_inserted = cdrom_co_is_inserted,
38283828
.bdrv_eject = cdrom_eject,
38293829
.bdrv_lock_medium = cdrom_lock_medium,
38303830

@@ -3883,7 +3883,7 @@ static int cdrom_reopen(BlockDriverState *bs)
38833883
return 0;
38843884
}
38853885

3886-
static bool cdrom_is_inserted(BlockDriverState *bs)
3886+
static bool coroutine_fn cdrom_co_is_inserted(BlockDriverState *bs)
38873887
{
38883888
return raw_getlength(bs) > 0;
38893889
}
@@ -3954,7 +3954,7 @@ static BlockDriver bdrv_host_cdrom = {
39543954
= raw_get_allocated_file_size,
39553955

39563956
/* removable device support */
3957-
.bdrv_is_inserted = cdrom_is_inserted,
3957+
.bdrv_co_is_inserted = cdrom_co_is_inserted,
39583958
.bdrv_eject = cdrom_eject,
39593959
.bdrv_lock_medium = cdrom_lock_medium,
39603960
};

block/io.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ int coroutine_fn bdrv_co_preadv_part(BdrvChild *child,
16221622

16231623
trace_bdrv_co_preadv_part(bs, offset, bytes, flags);
16241624

1625-
if (!bdrv_is_inserted(bs)) {
1625+
if (!bdrv_co_is_inserted(bs)) {
16261626
return -ENOMEDIUM;
16271627
}
16281628

@@ -2067,7 +2067,7 @@ int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child,
20672067

20682068
trace_bdrv_co_pwritev_part(child->bs, offset, bytes, flags);
20692069

2070-
if (!bdrv_is_inserted(bs)) {
2070+
if (!bdrv_co_is_inserted(bs)) {
20712071
return -ENOMEDIUM;
20722072
}
20732073

@@ -2835,7 +2835,7 @@ int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
28352835

28362836
bdrv_inc_in_flight(bs);
28372837

2838-
if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs) ||
2838+
if (!bdrv_co_is_inserted(bs) || bdrv_is_read_only(bs) ||
28392839
bdrv_is_sg(bs)) {
28402840
goto early_exit;
28412841
}
@@ -2959,7 +2959,7 @@ int coroutine_fn bdrv_co_pdiscard(BdrvChild *child, int64_t offset,
29592959
BlockDriverState *bs = child->bs;
29602960
IO_CODE();
29612961

2962-
if (!bs || !bs->drv || !bdrv_is_inserted(bs)) {
2962+
if (!bs || !bs->drv || !bdrv_co_is_inserted(bs)) {
29632963
return -ENOMEDIUM;
29642964
}
29652965

@@ -3241,7 +3241,7 @@ static int coroutine_fn bdrv_co_copy_range_internal(
32413241
assert(!(read_flags & BDRV_REQ_NO_WAIT));
32423242
assert(!(write_flags & BDRV_REQ_NO_WAIT));
32433243

3244-
if (!dst || !dst->bs || !bdrv_is_inserted(dst->bs)) {
3244+
if (!dst || !dst->bs || !bdrv_co_is_inserted(dst->bs)) {
32453245
return -ENOMEDIUM;
32463246
}
32473247
ret = bdrv_check_request32(dst_offset, bytes, NULL, 0);
@@ -3252,7 +3252,7 @@ static int coroutine_fn bdrv_co_copy_range_internal(
32523252
return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, write_flags);
32533253
}
32543254

3255-
if (!src || !src->bs || !bdrv_is_inserted(src->bs)) {
3255+
if (!src || !src->bs || !bdrv_co_is_inserted(src->bs)) {
32563256
return -ENOMEDIUM;
32573257
}
32583258
ret = bdrv_check_request32(src_offset, bytes, NULL, 0);

blockdev.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type,
10241024
static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
10251025
{
10261026
BlockDriverState *bs;
1027+
AioContext *aio_context;
10271028

10281029
bs = bdrv_lookup_bs(name, name, errp);
10291030
if (bs == NULL) {
@@ -1035,11 +1036,16 @@ static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp)
10351036
return NULL;
10361037
}
10371038

1039+
aio_context = bdrv_get_aio_context(bs);
1040+
aio_context_acquire(aio_context);
1041+
10381042
if (!bdrv_is_inserted(bs)) {
10391043
error_setg(errp, "Device has no medium");
1040-
return NULL;
1044+
bs = NULL;
10411045
}
10421046

1047+
aio_context_release(aio_context);
1048+
10431049
return bs;
10441050
}
10451051

include/block/block-io.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ bool bdrv_is_read_only(BlockDriverState *bs);
136136
bool bdrv_is_writable(BlockDriverState *bs);
137137
bool bdrv_is_sg(BlockDriverState *bs);
138138
int bdrv_get_flags(BlockDriverState *bs);
139-
bool bdrv_is_inserted(BlockDriverState *bs);
139+
140+
bool coroutine_fn bdrv_co_is_inserted(BlockDriverState *bs);
141+
bool co_wrapper bdrv_is_inserted(BlockDriverState *bs);
142+
140143
void bdrv_lock_medium(BlockDriverState *bs, bool locked);
141144
void bdrv_eject(BlockDriverState *bs, bool eject_flag);
142145
const char *bdrv_get_format_name(BlockDriverState *bs);

include/block/block_int-common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ struct BlockDriver {
704704
BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
705705

706706
/* removable device specific */
707-
bool (*bdrv_is_inserted)(BlockDriverState *bs);
707+
bool coroutine_fn (*bdrv_co_is_inserted)(BlockDriverState *bs);
708708
void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag);
709709
void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
710710

include/sysemu/block-backend-io.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
5454

5555
void blk_inc_in_flight(BlockBackend *blk);
5656
void blk_dec_in_flight(BlockBackend *blk);
57-
bool blk_is_inserted(BlockBackend *blk);
57+
58+
bool coroutine_fn blk_co_is_inserted(BlockBackend *blk);
59+
bool co_wrapper_mixed blk_is_inserted(BlockBackend *blk);
60+
5861
bool blk_is_available(BlockBackend *blk);
5962
void blk_lock_medium(BlockBackend *blk, bool locked);
6063
void blk_eject(BlockBackend *blk, bool eject_flag);

0 commit comments

Comments
 (0)