Make cmap_* also has same policy as dlm_*

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>
This commit is contained in:
Guoqing Jiang 2015-10-19 16:03:20 +08:00 committed by NeilBrown
parent d15a1f72bd
commit e80357f825
3 changed files with 57 additions and 37 deletions

View File

@ -1317,6 +1317,9 @@ int main(int argc, char *argv[])
} }
rv = 0; rv = 0;
set_hooks(); /* set hooks from libs */
if (c.homecluster == NULL && (c.nodes > 0)) { if (c.homecluster == NULL && (c.nodes > 0)) {
c.homecluster = conf_get_homecluster(); c.homecluster = conf_get_homecluster();
if (c.homecluster == NULL) if (c.homecluster == NULL)
@ -1346,8 +1349,6 @@ int main(int argc, char *argv[])
/* --scan implied --brief unless -vv */ /* --scan implied --brief unless -vv */
c.brief = 1; c.brief = 1;
set_dlm_hooks(); /* get dlm funcs from libdlm_lt.so.3 */
switch(mode) { switch(mode) {
case MANAGE: case MANAGE:
/* readonly, add/remove, readwrite, runstop */ /* readonly, add/remove, readwrite, runstop */

20
mdadm.h
View File

@ -52,6 +52,13 @@ extern __off64_t lseek64 __P ((int __fd, __off64_t __offset, int __whence));
#define srandom srand #define srandom srand
#endif #endif
#ifdef NO_COROSYNC
#define CS_OK 1
typedef uint64_t cmap_handle_t;
#else
#include <corosync/cmap.h>
#endif
#ifndef NO_DLM #ifndef NO_DLM
#include <libdlm.h> #include <libdlm.h>
#include <errno.h> #include <errno.h>
@ -1444,6 +1451,19 @@ extern char *fd2devnm(int fd);
extern int in_initrd(void); 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 { struct dlm_hooks {
void *dlm_handle; /* dlm lib related */ void *dlm_handle; /* dlm lib related */

65
util.c
View File

@ -36,13 +36,6 @@
#include <dirent.h> #include <dirent.h>
#include <signal.h> #include <signal.h>
#include <dlfcn.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) if (fd >= 0 && fd != mdfd)
dup2(fd, mdfd); dup2(fd, mdfd);
} }
static struct cmap_hooks *cmap_hooks = NULL;
static int is_cmap_hooks_ready = 0;
#ifndef MDASSEMBLE #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;
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");
if (!cmap_hooks->initialize || !cmap_hooks->get_string ||
!cmap_hooks->finalize)
dlclose(cmap_hooks->cmap_handle);
else
is_cmap_hooks_ready = 1;
}
int get_cluster_name(char **cluster_name) int get_cluster_name(char **cluster_name)
{ {
void *lib_handle = NULL;
int rv = -1; int rv = -1;
cmap_handle_t handle; 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);
if (!is_cmap_hooks_ready)
lib_handle = dlopen("libcmap.so.4", RTLD_NOW | RTLD_LOCAL);
if (!lib_handle)
return rv; return rv;
initialize = dlsym(lib_handle, "cmap_initialize"); rv = cmap_hooks->initialize(&handle);
if (!initialize)
goto out;
get_string = dlsym(lib_handle, "cmap_get_string");
if (!get_string)
goto out;
finalize = dlsym(lib_handle, "cmap_finalize");
if (!finalize)
goto out;
rv = initialize(&handle);
if (rv != CS_OK) if (rv != CS_OK)
goto out; 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) { if (rv != CS_OK) {
free(*cluster_name); free(*cluster_name);
rv = -1; rv = -1;
@ -2164,9 +2158,8 @@ int get_cluster_name(char **cluster_name)
rv = 0; rv = 0;
name_err: name_err:
finalize(handle); cmap_hooks->finalize(handle);
out: out:
dlclose(lib_handle);
return rv; return rv;
} }
@ -2191,4 +2184,10 @@ void set_dlm_hooks(void)
else else
is_dlm_hooks_ready = 1; is_dlm_hooks_ready = 1;
} }
void set_hooks(void)
{
set_dlm_hooks();
set_cmap_hooks();
}
#endif #endif