From 03d9e4c0dba9d1b5d0c532ac3518415466ebdf8f Mon Sep 17 00:00:00 2001 From: Alberto Faria Date: Fri, 28 Oct 2022 14:16:35 +0100 Subject: block/blkio: Add virtio-blk-vfio-pci BlockDriver libblkio 1.1.0 [1] introduces a virtio-blk-vfio-pci driver, which accesses a virtio-blk PCI device using VFIO. Add a corresponding BlockDriver. [1] https://gitlab.com/libblkio/libblkio/-/tree/v1.1.0 Signed-off-by: Alberto Faria Message-id: 20221028131635.710267-1-afaria@redhat.com Signed-off-by: Stefan Hajnoczi --- block/blkio.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'block') diff --git a/block/blkio.c b/block/blkio.c index 82f26eedd2..f55eb774b4 100644 --- a/block/blkio.c +++ b/block/blkio.c @@ -25,6 +25,7 @@ */ #define DRIVER_IO_URING "io_uring" #define DRIVER_NVME_IO_URING "nvme-io_uring" +#define DRIVER_VIRTIO_BLK_VFIO_PCI "virtio-blk-vfio-pci" #define DRIVER_VIRTIO_BLK_VHOST_USER "virtio-blk-vhost-user" #define DRIVER_VIRTIO_BLK_VHOST_VDPA "virtio-blk-vhost-vdpa" @@ -704,6 +705,8 @@ static int blkio_file_open(BlockDriverState *bs, QDict *options, int flags, ret = blkio_io_uring_open(bs, options, flags, errp); } else if (strcmp(blkio_driver, DRIVER_NVME_IO_URING) == 0) { ret = blkio_nvme_io_uring(bs, options, flags, errp); + } else if (strcmp(blkio_driver, DRIVER_VIRTIO_BLK_VFIO_PCI) == 0) { + ret = blkio_virtio_blk_common_open(bs, options, flags, errp); } else if (strcmp(blkio_driver, DRIVER_VIRTIO_BLK_VHOST_USER) == 0) { ret = blkio_virtio_blk_common_open(bs, options, flags, errp); } else if (strcmp(blkio_driver, DRIVER_VIRTIO_BLK_VHOST_VDPA) == 0) { @@ -989,6 +992,10 @@ static BlockDriver bdrv_nvme_io_uring = BLKIO_DRIVER( .bdrv_needs_filename = true, ); +static BlockDriver bdrv_virtio_blk_vfio_pci = BLKIO_DRIVER( + DRIVER_VIRTIO_BLK_VFIO_PCI +); + static BlockDriver bdrv_virtio_blk_vhost_user = BLKIO_DRIVER( DRIVER_VIRTIO_BLK_VHOST_USER ); @@ -1001,6 +1008,7 @@ static void bdrv_blkio_init(void) { bdrv_register(&bdrv_io_uring); bdrv_register(&bdrv_nvme_io_uring); + bdrv_register(&bdrv_virtio_blk_vfio_pci); bdrv_register(&bdrv_virtio_blk_vhost_user); bdrv_register(&bdrv_virtio_blk_vhost_vdpa); } -- cgit v1.2.3-55-g7522 From 4c8f4fda0504564580f5c0a37e2d4b32ff17d2a1 Mon Sep 17 00:00:00 2001 From: Alberto Faria Date: Sat, 29 Oct 2022 13:20:31 +0100 Subject: block/blkio: Tolerate device size changes Some libblkio drivers may be able to work with regular files (e.g., io_uring) or otherwise resizable devices. Conservatively set BlockDriver::has_variable_length to true to ensure bdrv_nb_sectors() always gives up-to-date results. Also implement BlockDriver::bdrv_co_truncate for the case where no preallocation is needed and the device already has a size compatible with what was requested. Signed-off-by: Alberto Faria Message-id: 20221029122031.975273-1-afaria@redhat.com Signed-off-by: Stefan Hajnoczi --- block/blkio.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'block') diff --git a/block/blkio.c b/block/blkio.c index f55eb774b4..d850506acd 100644 --- a/block/blkio.c +++ b/block/blkio.c @@ -848,6 +848,31 @@ static int64_t blkio_getlength(BlockDriverState *bs) return capacity; } +static int coroutine_fn blkio_truncate(BlockDriverState *bs, int64_t offset, + bool exact, PreallocMode prealloc, + BdrvRequestFlags flags, Error **errp) +{ + int64_t current_length; + + if (prealloc != PREALLOC_MODE_OFF) { + error_setg(errp, "Unsupported preallocation mode '%s'", + PreallocMode_str(prealloc)); + return -ENOTSUP; + } + + current_length = blkio_getlength(bs); + + if (offset > current_length) { + error_setg(errp, "Cannot grow device"); + return -EINVAL; + } else if (exact && offset != current_length) { + error_setg(errp, "Cannot resize device"); + return -ENOTSUP; + } + + return 0; +} + static int blkio_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) { return 0; @@ -963,10 +988,12 @@ static void blkio_refresh_limits(BlockDriverState *bs, Error **errp) { \ .format_name = name, \ .protocol_name = name, \ + .has_variable_length = true, \ .instance_size = sizeof(BDRVBlkioState), \ .bdrv_file_open = blkio_file_open, \ .bdrv_close = blkio_close, \ .bdrv_getlength = blkio_getlength, \ + .bdrv_co_truncate = blkio_truncate, \ .bdrv_get_info = blkio_get_info, \ .bdrv_attach_aio_context = blkio_attach_aio_context, \ .bdrv_detach_aio_context = blkio_detach_aio_context, \ -- cgit v1.2.3-55-g7522 From 6c32fc0df9cd901add75618c831fb26a9eb742cb Mon Sep 17 00:00:00 2001 From: Alberto Faria Date: Sat, 29 Oct 2022 00:38:54 +0100 Subject: block/blkio: Make driver nvme-io_uring take a "path" instead of a "filename" The nvme-io_uring driver expects a character special file such as /dev/ng0n1. Follow the convention of having a "filename" option when a regular file is expected, and a "path" option otherwise. This makes io_uring the only libblkio-based driver with a "filename" option, as it accepts a regular file (even though it can also take a block special file). Signed-off-by: Alberto Faria Message-id: 20221028233854.839933-1-afaria@redhat.com Signed-off-by: Stefan Hajnoczi --- block/blkio.c | 12 ++++++++---- qapi/block-core.json | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'block') diff --git a/block/blkio.c b/block/blkio.c index d850506acd..620fab28a7 100644 --- a/block/blkio.c +++ b/block/blkio.c @@ -640,12 +640,17 @@ static int blkio_io_uring_open(BlockDriverState *bs, QDict *options, int flags, static int blkio_nvme_io_uring(BlockDriverState *bs, QDict *options, int flags, Error **errp) { - const char *filename = qdict_get_str(options, "filename"); + const char *path = qdict_get_try_str(options, "path"); BDRVBlkioState *s = bs->opaque; int ret; - ret = blkio_set_str(s->blkio, "path", filename); - qdict_del(options, "filename"); + if (!path) { + error_setg(errp, "missing 'path' option"); + return -EINVAL; + } + + ret = blkio_set_str(s->blkio, "path", path); + qdict_del(options, "path"); if (ret < 0) { error_setg_errno(errp, -ret, "failed to set path: %s", blkio_get_error_msg()); @@ -1016,7 +1021,6 @@ static BlockDriver bdrv_io_uring = BLKIO_DRIVER( static BlockDriver bdrv_nvme_io_uring = BLKIO_DRIVER( DRIVER_NVME_IO_URING, - .bdrv_needs_filename = true, ); static BlockDriver bdrv_virtio_blk_vfio_pci = BLKIO_DRIVER( diff --git a/qapi/block-core.json b/qapi/block-core.json index 81bbb0b893..6d904004f8 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3704,12 +3704,12 @@ # # Driver specific block device options for the nvme-io_uring backend. # -# @filename: path to the image file +# @path: path to the image file # # Since: 7.2 ## { 'struct': 'BlockdevOptionsNvmeIoUring', - 'data': { 'filename': 'str' }, + 'data': { 'path': 'str' }, 'if': 'CONFIG_BLKIO' } ## -- cgit v1.2.3-55-g7522