From 30c04b0f6d717ad27601477eb4b4c47402f46b57 Mon Sep 17 00:00:00 2001
From: Kazunori INOUE <kazunori_inoue@newson.co.jp>
Date: Fri, 29 Jan 2021 11:28:20 +0900
Subject: [PATCH] Fix: fencing: remove any devices that are not installed

---
 fencing/commands.c  |  2 ++
 fencing/main.c | 37 ++++++++++++++++++-------------
 fencing/internal.h |  1 +
 3 files changed, 25 insertions(+), 15 deletions(-)

Index: pacemaker-1.1.18+20180430.b12c320f5/fencing/commands.c
===================================================================
--- pacemaker-1.1.18+20180430.b12c320f5.orig/fencing/commands.c
+++ pacemaker-1.1.18+20180430.b12c320f5/fencing/commands.c
@@ -1216,6 +1216,8 @@ stonith_device_register(xmlNode * msg, c
                    g_hash_table_size(device_list));
         free_device(device);
         device = dup;
+        dup = g_hash_table_lookup(device_list, device->id);
+        dup->dirty = FALSE;
 
     } else {
         stonith_device_t *old = g_hash_table_lookup(device_list, device->id);
Index: pacemaker-1.1.18+20180430.b12c320f5/fencing/main.c
===================================================================
--- pacemaker-1.1.18+20180430.b12c320f5.orig/fencing/main.c
+++ pacemaker-1.1.18+20180430.b12c320f5/fencing/main.c
@@ -573,11 +573,8 @@ static void cib_device_update(resource_t
     const char *value = NULL;
     const char *rclass = NULL;
     node_t *parent = NULL;
-    gboolean remove = TRUE;
 
-    /* If this is a complex resource, check children rather than this resource itself.
-     * TODO: Mark each installed device and remove if untouched when this process finishes.
-     */
+    /* If this is a complex resource, check children rather than this resource itself. */
     if(rsc->children) {
         GListPtr gIter = NULL;
         for (gIter = rsc->children; gIter != NULL; gIter = gIter->next) {
@@ -596,11 +593,11 @@ static void cib_device_update(resource_t
         return;
     }
 
-    /* If this STONITH resource is disabled, just remove it. */
+    /* If this STONITH resource is disabled, remove it. */
     value = g_hash_table_lookup(rsc->meta, XML_RSC_ATTR_TARGET_ROLE);
     if (safe_str_eq(value, RSC_STOPPED)) {
         crm_info("Device %s has been disabled", rsc->id);
-        goto update_done;
+        return;
     }
 
     /* Check whether our node is allowed for this resource (and its parent if in a group) */
@@ -619,7 +616,7 @@ static void cib_device_update(resource_t
             crm_trace("Available: %s = %d", node->details->uname, node->weight);
         }
 
-        goto update_done;
+        return;
 
     } else if(node->weight < 0 || (parent && parent->weight < 0)) {
         /* Our node (or its group) is disallowed by score, so remove the device */
@@ -628,7 +625,7 @@ static void cib_device_update(resource_t
         crm_info("Device %s has been disabled on %s: score=%s", rsc->id, stonith_our_uname, score);
         free(score);
 
-        goto update_done;
+        return;
 
     } else {
         /* Our node is allowed, so update the device information */
@@ -656,19 +653,12 @@ static void cib_device_update(resource_t
             crm_trace(" %s=%s", name, value);
         }
 
-        remove = FALSE;
         data = create_device_registration_xml(rsc_name(rsc), provider, agent, params, rsc_provides);
         stonith_device_register(data, NULL, TRUE);
 
         stonith_key_value_freeall(params, 1, 1);
         free_xml(data);
     }
-
-update_done:
-
-    if(remove && g_hash_table_lookup(device_list, rsc_name(rsc))) {
-        stonith_device_remove(rsc_name(rsc), TRUE);
-    }
 }
 
 extern xmlNode *do_calculations(pe_working_set_t * data_set, xmlNode * xml_input, crm_time_t * now);
@@ -682,6 +672,8 @@ cib_devices_update(void)
 {
     GListPtr gIter = NULL;
     pe_working_set_t data_set;
+    GHashTableIter iter;
+    stonith_device_t *device = NULL;
 
     crm_info("Updating devices to version %s.%s.%s",
              crm_element_value(local_cib, XML_ATTR_GENERATION_ADMIN),
@@ -697,9 +689,24 @@ cib_devices_update(void)
     cluster_status(&data_set);
     do_calculations(&data_set, NULL, NULL);
 
+    g_hash_table_iter_init(&iter, device_list);
+    while (g_hash_table_iter_next(&iter, NULL, (void **)&device)) {
+        if (device->cib_registered) {
+            device->dirty = TRUE;
+        }
+    }
+
     for (gIter = data_set.resources; gIter != NULL; gIter = gIter->next) {
         cib_device_update(gIter->data, &data_set);
     }
+
+    g_hash_table_iter_init(&iter, device_list);
+    while (g_hash_table_iter_next(&iter, NULL, (void **)&device)) {
+        if (device->dirty) {
+            g_hash_table_iter_remove(&iter);
+        }
+    }
+
     data_set.input = NULL; /* Wasn't a copy */
     cleanup_alloc_calculations(&data_set);
 }
Index: pacemaker-1.1.18+20180430.b12c320f5/fencing/internal.h
===================================================================
--- pacemaker-1.1.18+20180430.b12c320f5.orig/fencing/internal.h
+++ pacemaker-1.1.18+20180430.b12c320f5/fencing/internal.h
@@ -49,6 +49,7 @@ typedef struct stonith_device_s {
 
     gboolean cib_registered;
     gboolean api_registered;
+    gboolean dirty;
 } stonith_device_t;
 
 /* These values are used to index certain arrays by "phase". Usually an
