mrc0mmand / rpms / lvm2

Forked from rpms/lvm2 2 years ago
Clone

Blame SOURCES/lvm2-2_02_182-dmeventd-lvm2-plugin-uses-envvar-registry.patch

5e29a5
From 41d8039e12ebad0727f8c7455ad9392bc61e6414 Mon Sep 17 00:00:00 2001
5e29a5
From: Zdenek Kabelac <zkabelac@redhat.com>
5e29a5
Date: Mon, 27 Aug 2018 10:18:26 +0200
5e29a5
Subject: [PATCH 1/2] dmeventd: lvm2 plugin uses envvar registry
5e29a5
5e29a5
Thin plugin started to use configuble setting to allow to configure
5e29a5
usage of external scripts - however to read this value it needed to
5e29a5
execute internal command as dmeventd itself has no access to lvm.conf
5e29a5
and the API for dmeventd plugin has been kept stable.
5e29a5
5e29a5
The call of command itself was not normally 'a big issue' until users
5e29a5
started to use higher number of monitored LVs and execution of command
5e29a5
got stuck because other monitored resource already started to execute
5e29a5
some other lvm2 command and become blocked waiting on VG lock.
5e29a5
5e29a5
This scenario revealed necesity to somehow avoid calling lvm2 command
5e29a5
during resource registration - but this requires bigger changes - so
5e29a5
meanwhile this patch tries to minimize the possibility to hit this race
5e29a5
by obtaining any configurable setting just once - such patch is small
5e29a5
and covers majority of problem - yet better solution needs to be
5e29a5
introduced likely with bigger rework of dmeventd.
5e29a5
5e29a5
TODO: Avoid blocking registration of resource with execution of lvm2
5e29a5
commands since those can get stuck waiting on mutexes.
5e29a5
5e29a5
(cherry picked from commit a8d59404f7713ae4f9a3b172dd560ed1364d8bee)
5e29a5
5e29a5
Conflicts:
5e29a5
	WHATS_NEW_DM
5e29a5
---
5e29a5
 WHATS_NEW_DM                                 |  4 +++
5e29a5
 daemons/dmeventd/plugins/lvm2/dmeventd_lvm.c | 49 +++++++++++++++++++++-------
5e29a5
 2 files changed, 42 insertions(+), 11 deletions(-)
5e29a5
5e29a5
diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM
5e29a5
index c42ee17..42cf2a8 100644
5e29a5
--- a/WHATS_NEW_DM
5e29a5
+++ b/WHATS_NEW_DM
5e29a5
@@ -1,3 +1,7 @@
5e29a5
+Version 1.02.151 - 
5e29a5
+==============================
5e29a5
+  Add hot fix to avoiding locking collision when monitoring thin-pools.
5e29a5
+
5e29a5
 Version 1.02.150 - 
5e29a5
 =================================
5e29a5
   Add vdo plugin for monitoring VDO devices.
5e29a5
diff --git a/daemons/dmeventd/plugins/lvm2/dmeventd_lvm.c b/daemons/dmeventd/plugins/lvm2/dmeventd_lvm.c
5e29a5
index 930f9fc..5be11f1 100644
5e29a5
--- a/daemons/dmeventd/plugins/lvm2/dmeventd_lvm.c
5e29a5
+++ b/daemons/dmeventd/plugins/lvm2/dmeventd_lvm.c
5e29a5
@@ -31,6 +31,13 @@ static pthread_mutex_t _register_mutex = PTHREAD_MUTEX_INITIALIZER;
5e29a5
 static int _register_count = 0;
5e29a5
 static struct dm_pool *_mem_pool = NULL;
5e29a5
 static void *_lvm_handle = NULL;
5e29a5
+static DM_LIST_INIT(_env_registry);
5e29a5
+
5e29a5
+struct env_data {
5e29a5
+	struct dm_list list;
5e29a5
+	const char *cmd;
5e29a5
+	const char *data;
5e29a5
+};
5e29a5
 
5e29a5
 DM_EVENT_LOG_FN("#lvm")
5e29a5
 
5e29a5
@@ -100,6 +107,7 @@ void dmeventd_lvm2_exit(void)
5e29a5
 		lvm2_run(_lvm_handle, "_memlock_dec");
5e29a5
 		dm_pool_destroy(_mem_pool);
5e29a5
 		_mem_pool = NULL;
5e29a5
+		dm_list_init(&_env_registry);
5e29a5
 		lvm2_exit(_lvm_handle);
5e29a5
 		_lvm_handle = NULL;
5e29a5
 		log_debug("lvm plugin exited.");
5e29a5
@@ -124,6 +132,8 @@ int dmeventd_lvm2_command(struct dm_pool *mem, char *buffer, size_t size,
5e29a5
 	static char _internal_prefix[] =  "_dmeventd_";
5e29a5
 	char *vg = NULL, *lv = NULL, *layer;
5e29a5
 	int r;
5e29a5
+	struct env_data *env_data;
5e29a5
+	const char *env = NULL;
5e29a5
 
5e29a5
 	if (!dm_split_lvm_name(mem, device, &vg, &lv, &layer)) {
5e29a5
 		log_error("Unable to determine VG name from %s.",
5e29a5
@@ -137,18 +147,35 @@ int dmeventd_lvm2_command(struct dm_pool *mem, char *buffer, size_t size,
5e29a5
 		*layer = '\0';
5e29a5
 
5e29a5
 	if (!strncmp(cmd, _internal_prefix, sizeof(_internal_prefix) - 1)) {
5e29a5
-		dmeventd_lvm2_lock();
5e29a5
-		/* output of internal command passed via env var */
5e29a5
-		if (!dmeventd_lvm2_run(cmd))
5e29a5
-			cmd = NULL;
5e29a5
-		else if ((cmd = getenv(cmd)))
5e29a5
-			cmd = dm_pool_strdup(mem, cmd); /* copy with lock */
5e29a5
-		dmeventd_lvm2_unlock();
5e29a5
-
5e29a5
-		if (!cmd) {
5e29a5
-			log_error("Unable to find configured command.");
5e29a5
-			return 0;
5e29a5
+		/* check if ENVVAR wasn't already resolved */
5e29a5
+		dm_list_iterate_items(env_data, &_env_registry)
5e29a5
+			if (!strcmp(cmd, env_data->cmd)) {
5e29a5
+				env = env_data->data;
5e29a5
+				break;
5e29a5
+			}
5e29a5
+
5e29a5
+		if (!env) {
5e29a5
+			/* run lvm2 command to find out setting value */
5e29a5
+			dmeventd_lvm2_lock();
5e29a5
+			if (!dmeventd_lvm2_run(cmd) ||
5e29a5
+			    !(env = getenv(cmd))) {
5e29a5
+				log_error("Unable to find configured command.");
5e29a5
+				return 0;
5e29a5
+			}
5e29a5
+			/* output of internal command passed via env var */
5e29a5
+			env = dm_pool_strdup(_mem_pool, env); /* copy with lock */
5e29a5
+			dmeventd_lvm2_unlock();
5e29a5
+			if (!env ||
5e29a5
+			    !(env_data = dm_pool_zalloc(_mem_pool, sizeof(*env_data))) ||
5e29a5
+			    !(env_data->cmd = dm_pool_strdup(_mem_pool, cmd))) {
5e29a5
+				log_error("Unable to allocate env memory.");
5e29a5
+				return 0;
5e29a5
+			}
5e29a5
+			env_data->data = env;
5e29a5
+			/* add to ENVVAR registry */
5e29a5
+			dm_list_add(&_env_registry, &env_data->list);
5e29a5
 		}
5e29a5
+		cmd = env;
5e29a5
 	}
5e29a5
 
5e29a5
 	r = dm_snprintf(buffer, size, "%s %s/%s", cmd, vg, lv);
5e29a5
-- 
5e29a5
1.8.3.1
5e29a5