]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: block: Extract logic from qemuBlockReopenReadWrite/ReadOnly
authorPeter Krempa <pkrempa@redhat.com>
Wed, 22 Nov 2023 13:27:19 +0000 (14:27 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 27 Nov 2023 09:14:20 +0000 (10:14 +0100)
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 <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/qemu/qemu_block.c

index c03f278aa9edfb5647e102e5b59d6e5fa3d73867..6cc5f891c5fd04e4b7ea46f68772225c75f963ba 100644 (file)
@@ -3231,27 +3231,30 @@ qemuBlockReopenFormat(virDomainObj *vm,
 
 
 /**
- * qemuBlockReopenReadWrite:
+ * qemuBlockReopenAccess:
  * @vm: domain object
  * @src: storage source to reopen
+ * @readonly: requested readonly mode
  * @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.
+ * 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.
  */
-int
-qemuBlockReopenReadWrite(virDomainObj *vm,
-                         virStorageSource *src,
-                         virDomainAsyncJob asyncJob)
+
+static int
+qemuBlockReopenAccess(virDomainObj *vm,
+                      virStorageSource *src,
+                      bool readonly,
+                      virDomainAsyncJob asyncJob)
 {
-    if (!src->readonly)
+    if (src->readonly == readonly)
         return 0;
 
-    src->readonly = false;
+    src->readonly = readonly;
     if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) {
-        src->readonly = true;
+        src->readonly = !readonly;
         return -1;
     }
 
@@ -3260,31 +3263,38 @@ qemuBlockReopenReadWrite(virDomainObj *vm,
 
 
 /**
- * qemuBlockReopenReadOnly:
+ * qemuBlockReopenReadWrite:
  * @vm: domain object
  * @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-write. After successful reopen @src's
+ * 'readonly' flag is modified. Does nothing if @src is already read-write.
  */
 int
-qemuBlockReopenReadOnly(virDomainObj *vm,
+qemuBlockReopenReadWrite(virDomainObj *vm,
                          virStorageSource *src,
                          virDomainAsyncJob asyncJob)
 {
-    if (src->readonly)
-        return 0;
+    return qemuBlockReopenAccess(vm, src, false, asyncJob);
+}
 
-    src->readonly = true;
-    if (qemuBlockReopenFormat(vm, src, asyncJob) < 0) {
-        src->readonly = false;
-        return -1;
-    }
 
-    return 0;
+/**
+ * qemuBlockReopenReadOnly:
+ * @vm: domain object
+ * @src: storage source to reopen
+ * @asyncJob: qemu async job type
+ *
+ * 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)
+{
+    return qemuBlockReopenAccess(vm, src, true, asyncJob);
 }
 
 /**