>From 12d2631f96340ebc677d6e93b076fd9be8b00aa7 Mon Sep 17 00:00:00 2001
From: Jim Fehlig <jfehlig@suse.com>
Date: Wed, 30 Jun 2021 15:48:42 -0600
Subject: [PATCH] locking: Add flag to ingore existing lockspace when creating
 lock manager

Signed-off-by: Jim Fehlig <jfehlig@suse.com>
---
 src/libxl/libxl_driver.c           |  2 +-
 src/lock_protocol-structs          |  1 +
 src/locking/lock_daemon_dispatch.c | 17 +++++++++++++----
 src/locking/lock_driver.h          |  4 +++-
 src/locking/lock_driver_lockd.c    | 13 ++++++++-----
 src/locking/lock_driver_lockd.h    |  4 ++++
 src/locking/lock_protocol.x        |  1 +
 src/qemu/qemu_driver.c             |  2 +-
 8 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index c97b2fb485..af0daa0361 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -759,7 +759,7 @@ libxlStateInitialize(bool privileged,
                                   cfg->lockManagerName : "nop",
                                   "libxl",
                                   cfg->configBaseDir,
-                                  0)))
+                                  VIR_LOCK_MANAGER_IGNORE_EXISTING_LOCKSPACE_ERROR)))
         goto error;
 
     /* read the host sysinfo */
diff --git a/src/lock_protocol-structs b/src/lock_protocol-structs
index 41be9ce347..bca752a258 100644
--- a/src/lock_protocol-structs
+++ b/src/lock_protocol-structs
@@ -38,6 +38,7 @@ struct virLockSpaceProtocolReleaseResourceArgs {
 };
 struct virLockSpaceProtocolCreateLockSpaceArgs {
         virLockSpaceProtocolNonNullString path;
+        u_int                      flags;
 };
 enum virLockSpaceProtocolProcedure {
         VIR_LOCK_SPACE_PROTOCOL_PROC_REGISTER = 1,
diff --git a/src/locking/lock_daemon_dispatch.c b/src/locking/lock_daemon_dispatch.c
index e8b9832453..97ae088f61 100644
--- a/src/locking/lock_daemon_dispatch.c
+++ b/src/locking/lock_daemon_dispatch.c
@@ -392,12 +392,16 @@ virLockSpaceProtocolDispatchCreateLockSpace(virNetServer *server G_GNUC_UNUSED,
                                             virLockSpaceProtocolCreateLockSpaceArgs *args)
 {
     int rv = -1;
+    unsigned int flags = args->flags;
     virLockDaemonClient *priv =
         virNetServerClientGetPrivateData(client);
     virLockSpace *lockspace;
 
     g_mutex_lock(&priv->lock);
 
+    virCheckFlagsGoto(VIR_LOCK_SPACE_PROTOCOL_CREATE_LOCKSPACE_IGNORE_EXISTING_RESOURCE,
+                      cleanup);
+
     if (priv->restricted) {
         virReportError(VIR_ERR_OPERATION_DENIED, "%s",
                        _("lock manager connection has been restricted"));
@@ -405,10 +409,15 @@ virLockSpaceProtocolDispatchCreateLockSpace(virNetServer *server G_GNUC_UNUSED,
     }
 
     if (virLockDaemonFindLockSpace(lockDaemon, args->path) != NULL) {
-        virReportError(VIR_ERR_OPERATION_INVALID,
-                       _("Lockspace for path %s already exists"),
-                       args->path);
-        goto cleanup;
+        if (flags & VIR_LOCK_SPACE_PROTOCOL_CREATE_LOCKSPACE_IGNORE_EXISTING_RESOURCE) {
+            rv = 0;
+            goto cleanup;
+        } else {
+            virReportError(VIR_ERR_OPERATION_INVALID,
+                           _("Lockspace for path %s already exists"),
+                           args->path);
+            goto cleanup;
+        }
     }
 
     if (!(lockspace = virLockSpaceNew(args->path)))
diff --git a/src/locking/lock_driver.h b/src/locking/lock_driver.h
index 087a918882..37266c8251 100644
--- a/src/locking/lock_driver.h
+++ b/src/locking/lock_driver.h
@@ -32,7 +32,9 @@ typedef struct _virLockManagerParam virLockManagerParam;
 
 typedef enum {
     /* State passing is used to re-acquire existing leases */
-    VIR_LOCK_MANAGER_USES_STATE = (1 << 0)
+    VIR_LOCK_MANAGER_USES_STATE = (1 << 0),
+    /* Ignore existing lockspace errors on daemon init */
+    VIR_LOCK_MANAGER_IGNORE_EXISTING_LOCKSPACE_ERROR = (1 << 1),
 } virLockManagerFlags;
 
 typedef enum {
diff --git a/src/locking/lock_driver_lockd.c b/src/locking/lock_driver_lockd.c
index 3a7386af30..115dfb2044 100644
--- a/src/locking/lock_driver_lockd.c
+++ b/src/locking/lock_driver_lockd.c
@@ -261,7 +261,8 @@ virLockManagerLockDaemonConnect(virLockManager *lock,
 }
 
 
-static int virLockManagerLockDaemonSetupLockspace(const char *path)
+static int virLockManagerLockDaemonSetupLockspace(const char *path,
+                                                  unsigned int flags)
 {
     virNetClient *client;
     virNetClientProgram *program = NULL;
@@ -271,6 +272,8 @@ static int virLockManagerLockDaemonSetupLockspace(const char *path)
 
     memset(&args, 0, sizeof(args));
     args.path = (char*)path;
+    if (flags & VIR_LOCK_MANAGER_IGNORE_EXISTING_LOCKSPACE_ERROR)
+        args.flags = VIR_LOCK_SPACE_PROTOCOL_CREATE_LOCKSPACE_IGNORE_EXISTING_RESOURCE;
 
     if (!(client = virLockManagerLockDaemonConnectionNew(geteuid() == 0, &program)))
         return -1;
@@ -309,7 +312,7 @@ static int virLockManagerLockDaemonInit(unsigned int version,
 {
     VIR_DEBUG("version=%u configFile=%s flags=0x%x", version, NULLSTR(configFile), flags);
 
-    virCheckFlags(0, -1);
+    virCheckFlags(VIR_LOCK_MANAGER_IGNORE_EXISTING_LOCKSPACE_ERROR, -1);
 
     if (driver)
         return 0;
@@ -324,15 +327,15 @@ static int virLockManagerLockDaemonInit(unsigned int version,
 
     if (driver->autoDiskLease) {
         if (driver->fileLockSpaceDir &&
-            virLockManagerLockDaemonSetupLockspace(driver->fileLockSpaceDir) < 0)
+            virLockManagerLockDaemonSetupLockspace(driver->fileLockSpaceDir, flags) < 0)
             goto error;
 
         if (driver->lvmLockSpaceDir &&
-            virLockManagerLockDaemonSetupLockspace(driver->lvmLockSpaceDir) < 0)
+            virLockManagerLockDaemonSetupLockspace(driver->lvmLockSpaceDir, flags) < 0)
             goto error;
 
         if (driver->scsiLockSpaceDir &&
-            virLockManagerLockDaemonSetupLockspace(driver->scsiLockSpaceDir) < 0)
+            virLockManagerLockDaemonSetupLockspace(driver->scsiLockSpaceDir, flags) < 0)
             goto error;
     }
 
diff --git a/src/locking/lock_driver_lockd.h b/src/locking/lock_driver_lockd.h
index b9eaab4831..9457b95efc 100644
--- a/src/locking/lock_driver_lockd.h
+++ b/src/locking/lock_driver_lockd.h
@@ -25,3 +25,7 @@ enum virLockSpaceProtocolAcquireResourceFlags {
         VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_SHARED = (1 << 0),
         VIR_LOCK_SPACE_PROTOCOL_ACQUIRE_RESOURCE_AUTOCREATE = (1 << 1),
 };
+
+enum virLockSpaceProtocolCreateLockSpaceFlags {
+        VIR_LOCK_SPACE_PROTOCOL_CREATE_LOCKSPACE_IGNORE_EXISTING_RESOURCE = (1 << 0),
+};
diff --git a/src/locking/lock_protocol.x b/src/locking/lock_protocol.x
index 6d4cec39e2..a49080de98 100644
--- a/src/locking/lock_protocol.x
+++ b/src/locking/lock_protocol.x
@@ -65,6 +65,7 @@ struct virLockSpaceProtocolReleaseResourceArgs {
 
 struct virLockSpaceProtocolCreateLockSpaceArgs {
     virLockSpaceProtocolNonNullString path;
+    unsigned int flags;
 };
 
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 235f575901..2ced052abd 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -711,7 +711,7 @@ qemuStateInitialize(bool privileged,
                                   cfg->lockManagerName : "nop",
                                   "qemu",
                                   cfg->configBaseDir,
-                                  0)))
+                                  VIR_LOCK_MANAGER_IGNORE_EXISTING_LOCKSPACE_ERROR)))
         goto error;
 
    if (cfg->macFilter) {
-- 
2.31.1

