diff --git a/block/blkio.c b/block/blkio.c index 8e7ce42c79..1dd495617c 100644 --- a/block/blkio.c +++ b/block/blkio.c @@ -678,7 +678,7 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options, const char *path = qdict_get_try_str(options, "path"); BDRVBlkioState *s = bs->opaque; bool fd_supported = false; - int fd, ret; + int fd = -1, ret; if (!path) { error_setg(errp, "missing 'path' option"); @@ -713,12 +713,19 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options, */ fd = qemu_open(path, O_RDWR, NULL); if (fd < 0) { + /* + * qemu_open() can fail if the user specifies a path that is not + * a file or device, for example in the case of Unix Domain Socket + * for the virtio-blk-vhost-user driver. In such cases let's have + * libblkio open the path directly. + */ fd_supported = false; } else { ret = blkio_set_int(s->blkio, "fd", fd); if (ret < 0) { fd_supported = false; qemu_close(fd); + fd = -1; } } } @@ -733,14 +740,21 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options, } ret = blkio_connect(s->blkio); + if (ret < 0 && fd >= 0) { + /* Failed to give the FD to libblkio, close it */ + qemu_close(fd); + fd = -1; + } + /* - * If the libblkio driver doesn't support the `fd` property, blkio_connect() - * will fail with -EINVAL. So let's try calling blkio_connect() again by - * directly setting `path`. + * Before https://gitlab.com/libblkio/libblkio/-/merge_requests/208 + * (libblkio <= v1.3.0), setting the `fd` property is not enough to check + * whether the driver supports the `fd` property or not. In that case, + * blkio_connect() will fail with -EINVAL. + * So let's try calling blkio_connect() again by directly setting `path` + * to cover this scenario. */ if (fd_supported && ret == -EINVAL) { - qemu_close(fd); - /* * We need to clear the `fd` property we set previously by setting * it to -1.