From 214794c9c7ce1e6486d02ad8cc98a62e00f555aa Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 22 Nov 2023 14:27:19 +0100 Subject: [PATCH] qemu: block: Extract logic from qemuBlockReopenReadWrite/ReadOnly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to preserve the wrappers for clarity but the inner logic can be extracted to a common function qemuBlockReopenAccess. In further patches the code from qemuBlockReopenFormat will be merged into the new wrapper as well as logic for handling scenarios with missing 'format' layers will be added. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/qemu/qemu_block.c | 66 +++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index c03f278aa9..6cc5f891c5 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -3230,32 +3230,53 @@ qemuBlockReopenFormat(virDomainObj *vm, } +/** + * qemuBlockReopenAccess: + * @vm: domain object + * @src: storage source to reopen + * @readonly: requested readonly mode + * @asyncJob: qemu async job type + * + * Reopen @src image to ensure that it is in @readonly. Does nothing if it is + * already in the requested state. + * + * Callers must use qemuBlockReopenReadWrite/qemuBlockReopenReadOnly functions. + */ + +static int +qemuBlockReopenAccess(virDomainObj *vm, + virStorageSource *src, + bool readonly, + virDomainAsyncJob asyncJob) +{ + if (src->readonly == readonly) + return 0; + + src->readonly = readonly; + if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) { + src->readonly = !readonly; + return -1; + } + + return 0; +} + + /** * qemuBlockReopenReadWrite: * @vm: domain object * @src: storage source to reopen * @asyncJob: qemu async job type * - * Wrapper that reopens @src read-write. We currently depend on qemu - * reopening the storage with 'auto-read-only' enabled for us. - * After successful reopen @src's 'readonly' flag is modified. Does nothing - * if @src is already read-write. + * Semantic wrapper that reopens @src read-write. After successful reopen @src's + * 'readonly' flag is modified. Does nothing if @src is already read-write. */ int qemuBlockReopenReadWrite(virDomainObj *vm, virStorageSource *src, virDomainAsyncJob asyncJob) { - if (!src->readonly) - return 0; - - src->readonly = false; - if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) { - src->readonly = true; - return -1; - } - - return 0; + return qemuBlockReopenAccess(vm, src, false, asyncJob); } @@ -3265,26 +3286,15 @@ qemuBlockReopenReadWrite(virDomainObj *vm, * @src: storage source to reopen * @asyncJob: qemu async job type * - * Wrapper that reopens @src read-only. We currently depend on qemu - * reopening the storage with 'auto-read-only' enabled for us. - * After successful reopen @src's 'readonly' flag is modified. Does nothing - * if @src is already read-only. + * Semantic wrapper that reopens @src read-only. After successful reopen @src's + * 'readonly' flag is modified. Does nothing if @src is already read-only. */ int qemuBlockReopenReadOnly(virDomainObj *vm, virStorageSource *src, virDomainAsyncJob asyncJob) { - if (src->readonly) - return 0; - - src->readonly = true; - if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) { - src->readonly = false; - return -1; - } - - return 0; + return qemuBlockReopenAccess(vm, src, true, asyncJob); } /**