File 0123-Make-cmap_-also-has-same-policy-as-dlm_.patch of Package mdadm.5365

From e80357f8254d8102fcfe2db9252922fbb054640a Mon Sep 17 00:00:00 2001
From: Guoqing Jiang <gqjiang@suse.com>
Date: Mon, 19 Oct 2015 16:03:20 +0800
Subject: [PATCH 177/359] Make cmap_* also has same policy as dlm_*
References: bsc#1081910

Let libcmap lib and related funs also only need one-time
setup during mdadm running period.

Signed-off-by: Guoqing Jiang <gqjiang@suse.com>
Signed-off-by: NeilBrown <neilb@suse.com>
Signed-off-by: Coly Li <colyli@suse.de>

---
 mdadm.c |  5 +++--
 mdadm.h | 20 ++++++++++++++++++++
 util.c  | 67 ++++++++++++++++++++++++++++++++---------------------------------
 3 files changed, 56 insertions(+), 36 deletions(-)

diff --git a/mdadm.c b/mdadm.c
index 8a758ea..f56a8cf 100644
--- a/mdadm.c
+++ b/mdadm.c
@@ -1317,6 +1317,9 @@ int main(int argc, char *argv[])
 	}
 
 	rv = 0;
+
+	set_hooks(); /* set hooks from libs */
+
 	if (c.homecluster == NULL && (c.nodes > 0)) {
 		c.homecluster = conf_get_homecluster();
 		if (c.homecluster == NULL)
@@ -1346,8 +1349,6 @@ int main(int argc, char *argv[])
 		/* --scan implied --brief unless -vv */
 		c.brief = 1;
 
-	set_dlm_hooks(); /* get dlm funcs from libdlm_lt.so.3 */
-
 	switch(mode) {
 	case MANAGE:
 		/* readonly, add/remove, readwrite, runstop */
diff --git a/mdadm.h b/mdadm.h
index b95697d..1f3c4bd 100644
--- a/mdadm.h
+++ b/mdadm.h
@@ -52,6 +52,13 @@ extern __off64_t lseek64 __P ((int __fd, __off64_t __offset, int __whence));
 #define srandom srand
 #endif
 
+#ifdef NO_COROSYNC
+#define CS_OK 1
+typedef uint64_t cmap_handle_t;
+#else
+#include	<corosync/cmap.h>
+#endif
+
 #ifndef NO_DLM
 #include	<libdlm.h>
 #include	<errno.h>
@@ -1444,6 +1451,19 @@ extern char *fd2devnm(int fd);
 
 extern int in_initrd(void);
 
+struct cmap_hooks {
+	void *cmap_handle;      /* corosync lib related */
+
+	int (*initialize)(cmap_handle_t *handle);
+	int (*get_string)(cmap_handle_t handle,
+			  const char *string,
+			  char **name);
+	int (*finalize)(cmap_handle_t handle);
+};
+
+extern void set_cmap_hooks(void);
+extern void set_hooks(void);
+
 struct dlm_hooks {
 	void *dlm_handle;	/* dlm lib related */
 
diff --git a/util.c b/util.c
index 1d2f1b3..8217e11 100644
--- a/util.c
+++ b/util.c
@@ -36,13 +36,6 @@
 #include	<dirent.h>
 #include	<signal.h>
 #include	<dlfcn.h>
-#include	<stdint.h>
-#ifdef NO_COROSYNC
- typedef uint64_t cmap_handle_t;
- #define CS_OK 1
-#else
- #include	<corosync/cmap.h>
-#endif
 
 
 /*
@@ -2121,41 +2114,42 @@ void reopen_mddev(int mdfd)
 	if (fd >= 0 && fd != mdfd)
 		dup2(fd, mdfd);
 }
-#ifndef MDASSEMBLE
-int get_cluster_name(char **cluster_name)
-{
-        void *lib_handle = NULL;
-        int rv = -1;
 
-        cmap_handle_t handle;
-        static int (*initialize)(cmap_handle_t *handle);
-        static int (*get_string)(cmap_handle_t handle,
-				 const char *string,
-				 char **name);
-        static int (*finalize)(cmap_handle_t handle);
+static struct cmap_hooks *cmap_hooks = NULL;
+static int is_cmap_hooks_ready = 0;
 
+#ifndef MDASSEMBLE
+void set_cmap_hooks(void)
+{
+	cmap_hooks = xmalloc(sizeof(struct cmap_hooks));
+	cmap_hooks->cmap_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL);
+	if (!cmap_hooks->cmap_handle)
+		return;
 
-        lib_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL);
-        if (!lib_handle)
-                return rv;
+	cmap_hooks->initialize = dlsym(cmap_hooks->cmap_handle, "cmap_initialize");
+	cmap_hooks->get_string = dlsym(cmap_hooks->cmap_handle, "cmap_get_string");
+	cmap_hooks->finalize = dlsym(cmap_hooks->cmap_handle, "cmap_finalize");
 
-        initialize = dlsym(lib_handle, "cmap_initialize");
-        if (!initialize)
-                goto out;
+	if (!cmap_hooks->initialize || !cmap_hooks->get_string ||
+	    !cmap_hooks->finalize)
+		dlclose(cmap_hooks->cmap_handle);
+	else
+		is_cmap_hooks_ready = 1;
+}
 
-        get_string = dlsym(lib_handle, "cmap_get_string");
-        if (!get_string)
-                goto out;
+int get_cluster_name(char **cluster_name)
+{
+        int rv = -1;
+	cmap_handle_t handle;
 
-        finalize = dlsym(lib_handle, "cmap_finalize");
-        if (!finalize)
-                goto out;
+	if (!is_cmap_hooks_ready)
+		return rv;
 
-        rv = initialize(&handle);
+        rv = cmap_hooks->initialize(&handle);
         if (rv != CS_OK)
                 goto out;
 
-        rv = get_string(handle, "totem.cluster_name", cluster_name);
+        rv = cmap_hooks->get_string(handle, "totem.cluster_name", cluster_name);
         if (rv != CS_OK) {
                 free(*cluster_name);
                 rv = -1;
@@ -2164,9 +2158,8 @@ int get_cluster_name(char **cluster_name)
 
         rv = 0;
 name_err:
-        finalize(handle);
+        cmap_hooks->finalize(handle);
 out:
-        dlclose(lib_handle);
         return rv;
 }
 
@@ -2191,4 +2184,10 @@ void set_dlm_hooks(void)
 	else
 		is_dlm_hooks_ready = 1;
 }
+
+void set_hooks(void)
+{
+	set_dlm_hooks();
+	set_cmap_hooks();
+}
 #endif
-- 
2.16.1

openSUSE Build Service is sponsored by