From ff68ebef3b6e8c9b48e8ceafee269beb6d57c81e Mon Sep 17 00:00:00 2001 From: guping Date: Thu, 13 Nov 2025 10:40:36 +0800 Subject: [PATCH] block: Allow drivers to control protocol prefix at creation This patch is pure refactoring: instead of hard-coding permission to use a protocol prefix when creating an image, the drivers can now pass in a parameter, comparable to what they could already do for opening a pre-existing image. This patch is purely mechanical (all drivers pass in true for now), but it will enable the next patch to cater to drivers that want to differ in behavior for the primary image vs. any secondary images that are opened at the same time as creating the primary image. Signed-off-by: Eric Blake Message-ID: <20250915213919.3121401-5-eblake@redhat.com> Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf Signed-off-by: guping --- block.c | 4 ++-- block/crypto.c | 2 +- block/parallels.c | 2 +- block/qcow.c | 2 +- block/qcow2.c | 4 ++-- block/qed.c | 2 +- block/raw-format.c | 2 +- block/vdi.c | 2 +- block/vhdx.c | 2 +- block/vmdk.c | 2 +- block/vpc.c | 2 +- include/block/block-global-state.h | 3 ++- 12 files changed, 15 insertions(+), 14 deletions(-) diff --git a/block.c b/block.c index 6a2abfabcb..3a24507d51 100644 --- a/block.c +++ b/block.c @@ -695,7 +695,7 @@ out: } int coroutine_fn bdrv_co_create_file(const char *filename, QemuOpts *opts, - Error **errp) + bool allow_protocol_prefix, Error **errp) { QemuOpts *protocol_opts; BlockDriver *drv; @@ -704,7 +704,7 @@ int coroutine_fn bdrv_co_create_file(const char *filename, QemuOpts *opts, GLOBAL_STATE_CODE(); - drv = bdrv_find_protocol(filename, true, errp); + drv = bdrv_find_protocol(filename, allow_protocol_prefix, errp); if (drv == NULL) { return -ENOENT; } diff --git a/block/crypto.c b/block/crypto.c index 921933a5e5..4dc3f6b5ff 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -709,7 +709,7 @@ block_crypto_co_create_opts_luks(BlockDriver *drv, const char *filename, } /* Create protocol layer */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto fail; } diff --git a/block/parallels.c b/block/parallels.c index 8f2b58e1c9..5e67eb9691 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -1117,7 +1117,7 @@ parallels_co_create_opts(BlockDriver *drv, const char *filename, } /* Create and open the file (protocol layer) */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto done; } diff --git a/block/qcow.c b/block/qcow.c index c6d0e15f1e..cd19a1e529 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -978,7 +978,7 @@ qcow_co_create_opts(BlockDriver *drv, const char *filename, } /* Create and open the file (protocol layer) */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto fail; } diff --git a/block/qcow2.c b/block/qcow2.c index 7af7c0bee4..f2b5ba6f1c 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3954,7 +3954,7 @@ qcow2_co_create_opts(BlockDriver *drv, const char *filename, QemuOpts *opts, } /* Create and open the file (protocol layer) */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto finish; } @@ -3969,7 +3969,7 @@ qcow2_co_create_opts(BlockDriver *drv, const char *filename, QemuOpts *opts, /* Create and open an external data file (protocol layer) */ val = qdict_get_try_str(qdict, BLOCK_OPT_DATA_FILE); if (val) { - ret = bdrv_co_create_file(val, opts, errp); + ret = bdrv_co_create_file(val, opts, true, errp); if (ret < 0) { goto finish; } diff --git a/block/qed.c b/block/qed.c index b986353979..26793e24c8 100644 --- a/block/qed.c +++ b/block/qed.c @@ -788,7 +788,7 @@ bdrv_qed_co_create_opts(BlockDriver *drv, const char *filename, } /* Create and open the file (protocol layer) */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto fail; } diff --git a/block/raw-format.c b/block/raw-format.c index 8195ed87cc..14328ea64a 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -463,7 +463,7 @@ static int coroutine_fn GRAPH_UNLOCKED raw_co_create_opts(BlockDriver *drv, const char *filename, QemuOpts *opts, Error **errp) { - return bdrv_co_create_file(filename, opts, errp); + return bdrv_co_create_file(filename, opts, true, errp); } static int raw_open(BlockDriverState *bs, QDict *options, int flags, diff --git a/block/vdi.c b/block/vdi.c index 3b57becb9f..b28eeefa3d 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -935,7 +935,7 @@ vdi_co_create_opts(BlockDriver *drv, const char *filename, qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, true); /* Create and open the file (protocol layer) */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto done; } diff --git a/block/vhdx.c b/block/vhdx.c index 5aa1a13506..b538ff8374 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -2096,7 +2096,7 @@ vhdx_co_create_opts(BlockDriver *drv, const char *filename, } /* Create and open the file (protocol layer) */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto fail; } diff --git a/block/vmdk.c b/block/vmdk.c index d6971c7067..2c5086a511 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -2331,7 +2331,7 @@ vmdk_create_extent(const char *filename, int64_t filesize, bool flat, int ret; BlockBackend *blk = NULL; - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto exit; } diff --git a/block/vpc.c b/block/vpc.c index d95a204612..00006e5310 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -1113,7 +1113,7 @@ vpc_co_create_opts(BlockDriver *drv, const char *filename, } /* Create and open the file (protocol layer) */ - ret = bdrv_co_create_file(filename, opts, errp); + ret = bdrv_co_create_file(filename, opts, true, errp); if (ret < 0) { goto fail; } diff --git a/include/block/block-global-state.h b/include/block/block-global-state.h index 6b21fbc73f..f4b0f84875 100644 --- a/include/block/block-global-state.h +++ b/include/block/block-global-state.h @@ -66,7 +66,8 @@ int co_wrapper bdrv_create(BlockDriver *drv, const char *filename, QemuOpts *opts, Error **errp); int coroutine_fn GRAPH_UNLOCKED -bdrv_co_create_file(const char *filename, QemuOpts *opts, Error **errp); +bdrv_co_create_file(const char *filename, QemuOpts *opts, + bool allow_protocol_prefix, Error **errp); BlockDriverState *bdrv_new(void); int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top, -- Gitee