summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Teigland <teigland@redhat.com>2015-05-28 14:32:41 -0500
committerDavid Teigland <teigland@redhat.com>2015-06-15 16:50:54 -0500
commite29c4811025dbcf89534f97f28d00001c74d8f08 (patch)
tree3faaefa416f09a9a2410554655bdcdbd0cfd03c1
parente8badb6ba63a5bcee45b196fa4503c7062c7f269 (diff)
downloadlvm2-e29c4811025dbcf89534f97f28d00001c74d8f08.tar.gz
lvmlockd: clean up the use/connect/connected logic
Get rid of the round about logic that was copied from lvmetad. Make it simple and clear.
-rw-r--r--lib/commands/toolcontext.c7
-rw-r--r--lib/locking/lvmlockd.c164
-rw-r--r--lib/locking/lvmlockd.h32
-rw-r--r--lib/metadata/metadata.c4
-rw-r--r--tools/lvmcmdline.c4
-rw-r--r--tools/pvmove.c4
6 files changed, 105 insertions, 110 deletions
diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c
index d84588600..2a1e0335d 100644
--- a/lib/commands/toolcontext.c
+++ b/lib/commands/toolcontext.c
@@ -677,14 +677,15 @@ static int _process_config(struct cmd_context *cmd)
return 0;
}
- lvmlockd_disconnect();
+ lvmlockd_disconnect(); /* start over when tool context is refreshed */
lvmlockd_socket = getenv("LVM_LVMLOCKD_SOCKET");
if (!lvmlockd_socket)
lvmlockd_socket = DEFAULT_RUN_DIR "/lvmlockd.socket";
lvmlockd_set_socket(lvmlockd_socket);
- lvmlockd_set_active(!!use_lvmlockd);
- lvmlockd_init(cmd);
+ lvmlockd_set_use(use_lvmlockd);
+ if (use_lvmlockd)
+ lvmlockd_init(cmd);
return 1;
}
diff --git a/lib/locking/lvmlockd.c b/lib/locking/lvmlockd.c
index 3c254a709..600280d3c 100644
--- a/lib/locking/lvmlockd.c
+++ b/lib/locking/lvmlockd.c
@@ -19,96 +19,84 @@
#include "lvmlockd-client.h"
static daemon_handle _lvmlockd;
-static int _lvmlockd_active;
-static int _lvmlockd_connected;
-
static const char *_lvmlockd_socket = NULL;
static struct cmd_context *_lvmlockd_cmd = NULL;
+static int _use_lvmlockd; /* is 1 if command is configured to use lvmlockd */
+static int _lvmlockd_connected; /* is 1 if command is connected to lvmlockd */
-void lvmlockd_disconnect(void)
+void lvmlockd_set_socket(const char *sock)
{
- if (_lvmlockd_connected)
- daemon_close(_lvmlockd);
- _lvmlockd_connected = 0;
- _lvmlockd_cmd = NULL;
+ _lvmlockd_socket = sock;
}
-void lvmlockd_init(struct cmd_context *cmd)
+/*
+ * Set directly from global/use_lvmlockd
+ */
+void lvmlockd_set_use(int use)
{
- if (!_lvmlockd_active && !access(LVMLOCKD_PIDFILE, F_OK))
- log_warn("lvmlockd is not running.");
- if (!_lvmlockd_active)
- return;
- _lvmlockd_cmd = cmd;
+ _use_lvmlockd = use;
}
-static void _lvmlockd_connect(void)
+/*
+ * Returns the value of global/use_lvmlockd being used by the command.
+ */
+int lvmlockd_use(void)
{
- if (!_lvmlockd_active || !_lvmlockd_socket || _lvmlockd_connected)
- return;
-
- _lvmlockd = lvmlockd_open(_lvmlockd_socket);
-
- if (_lvmlockd.socket_fd >= 0 && !_lvmlockd.error) {
- log_debug("Successfully connected to lvmlockd on fd %d.",
- _lvmlockd.socket_fd);
- _lvmlockd_connected = 1;
- }
+ return _use_lvmlockd;
}
-void lvmlockd_connect_or_warn(void)
+/*
+ * The command continues even if init and/or connect fail,
+ * because the command is allowed to use local VGs without lvmlockd,
+ * and is allowed to read lockd VGs without locks from lvmlockd.
+ */
+void lvmlockd_init(struct cmd_context *cmd)
{
- if (!_lvmlockd_active || _lvmlockd_connected)
- return;
+ if (!_use_lvmlockd) {
+ /* Should never happen, don't call init when not using lvmlockd. */
+ log_error("Should not initialize lvmlockd with use_lvmlockd=0.");
+ }
- _lvmlockd_connect();
+ if (!!access(LVMLOCKD_PIDFILE, F_OK))
+ log_warn("lvmlockd process not found using %s.", LVMLOCKD_PIDFILE);
- if (!_lvmlockd_connected) {
- log_warn("Failed to connect to lvmlockd: %s.",
- strerror(_lvmlockd.error));
- }
+ _lvmlockd_cmd = cmd;
}
-/*
- * in command setup:
- *
- * 1. if use_lvmlockd is set in config,
- * lvmlockd_set_active() sets _lvmlockd_active = 1
- *
- * 2. lvmlockd_init() sees _lvmlockd_active, and sets _lvmlockd_cmd
- *
- * 3. lvmlockd_connect_or_warn()/_lvmlockd_connect() see _lvmlockd_active,
- * create connection and if successful set _lvmlockd_connected = 1
- *
- * in command processing:
- *
- * 1. lock function calls lvmlockd_connected() which returns
- * _lvmlockd_connected
- *
- * 2. if lvmlockd_connected() returns 0, lock function fails
- */
-
-int lvmlockd_connected(void)
+void lvmlockd_connect(void)
{
- if (_lvmlockd_connected)
- return 1;
+ if (!_use_lvmlockd) {
+ /* Should never happen, don't call connect when not using lvmlockd. */
+ log_error("Should not connect to lvmlockd with use_lvmlockd=0.");
+ }
- return 0;
-}
+ if (_lvmlockd_connected) {
+ /* Should never happen, only call connect once. */
+ log_error("lvmlockd is already connected.");
+ }
-int lvmlockd_active(void)
-{
- return _lvmlockd_active;
-}
+ if (!_lvmlockd_socket) {
+ log_error("lvmlockd connect failed: no path to socket.");
+ return;
+ }
-void lvmlockd_set_active(int active)
-{
- _lvmlockd_active = active;
+ _lvmlockd = lvmlockd_open(_lvmlockd_socket);
+
+ if (_lvmlockd.socket_fd >= 0 && !_lvmlockd.error) {
+ log_debug("Successfully connected to lvmlockd on fd %d.", _lvmlockd.socket_fd);
+ _lvmlockd_connected = 1;
+ } else {
+ log_error("lvmlockd connect failed: fd %d error %d.",
+ _lvmlockd.socket_fd, _lvmlockd.error);
+ }
}
-void lvmlockd_set_socket(const char *sock)
+void lvmlockd_disconnect(void)
{
- _lvmlockd_socket = sock;
+ if (_lvmlockd_connected)
+ daemon_close(_lvmlockd);
+ _lvmlockd_connected = 0;
+ _lvmlockd_cmd = NULL;
}
/* Translate the result strings from lvmlockd to bit flags. */
@@ -247,9 +235,9 @@ static int _lockd_request(struct cmd_context *cmd,
if (!strcmp(mode, "na"))
return 1;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
/* cmd and pid are passed for informational and debugging purposes */
@@ -498,9 +486,9 @@ static int _init_vg_dlm(struct cmd_context *cmd, struct volume_group *vg)
int result;
int ret;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
reply = _lockd_send("init_vg",
@@ -572,9 +560,9 @@ static int _init_vg_sanlock(struct cmd_context *cmd, struct volume_group *vg)
int result;
int ret;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
if (!_create_sanlock_lv(cmd, vg, lock_lv_name)) {
@@ -703,9 +691,9 @@ static int _free_vg_sanlock(struct cmd_context *cmd, struct volume_group *vg)
int result;
int ret;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
if (!vg->lock_args || !strlen(vg->lock_args)) {
@@ -842,9 +830,9 @@ int lockd_start_vg(struct cmd_context *cmd, struct volume_group *vg)
memset(uuid, 0, sizeof(uuid));
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
/* Skip starting the vg lockspace when the vg lock is skipped. */
@@ -929,9 +917,9 @@ int lockd_stop_vg(struct cmd_context *cmd, struct volume_group *vg)
if (!is_lockd_type(vg->lock_type))
return 1;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
log_debug("lockd_stop_vg %s lock_type %s", vg->name,
@@ -974,9 +962,9 @@ int lockd_start_wait(struct cmd_context *cmd)
int result;
int ret;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
reply = _lockd_send("start_wait",
@@ -1580,9 +1568,9 @@ int lockd_vg_update(struct volume_group *vg)
if (!is_lockd_type(vg->lock_type))
return 1;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
reply = _lockd_send("vg_update",
@@ -1789,9 +1777,9 @@ static int _init_lv_sanlock(struct cmd_context *cmd, struct volume_group *vg,
int result;
int ret;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
retry:
@@ -1864,9 +1852,9 @@ static int _free_lv_sanlock(struct cmd_context *cmd, struct volume_group *vg,
int result;
int ret;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return 1;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return 0;
reply = _lockd_send("free_lv",
@@ -2203,9 +2191,9 @@ const char *lockd_running_lock_type(struct cmd_context *cmd)
const char *lock_type = NULL;
int result;
- if (!_lvmlockd_active)
+ if (!_use_lvmlockd)
return NULL;
- if (!lvmlockd_connected())
+ if (!_lvmlockd_connected)
return NULL;
reply = _lockd_send("running_lm",
diff --git a/lib/locking/lvmlockd.h b/lib/locking/lvmlockd.h
index 15bc5765f..c2635fc10 100644
--- a/lib/locking/lvmlockd.h
+++ b/lib/locking/lvmlockd.h
@@ -95,13 +95,12 @@ static inline int is_lockd_type(const char *lock_type)
/* lvmlockd connection and communication */
-void lvmlockd_init(struct cmd_context *);
-void lvmlockd_set_active(int);
-void lvmlockd_set_socket(const char *);
+void lvmlockd_set_socket(const char *sock);
+void lvmlockd_set_use(int use);
+int lvmlockd_use(void);
+void lvmlockd_init(struct cmd_context *cmd);
+void lvmlockd_connect(void);
void lvmlockd_disconnect(void);
-void lvmlockd_connect_or_warn(void);
-int lvmlockd_connected(void);
-int lvmlockd_active(void);
/* vgcreate/vgremove use init/free */
@@ -148,18 +147,23 @@ const char *lockd_running_lock_type(struct cmd_context *cmd);
#else /* LVMLOCKD_SUPPORT */
-#define lvmlockd_init(cmd) do { } while (0)
-#define lvmlockd_set_active(int) do { } while (0)
-#define lvmlockd_set_socket(str) do { } while (0)
-#define lvmlockd_disconnect() do { } while (0)
-#define lvmlockd_connect_or_warn() do { } while (0)
+static inline void lvmlockd_set_socket(const char *sock)
+{
+}
-static inline int lvmlockd_connected(void)
+static inline void lvmlockd_init(struct cmd_context *cmd)
+{
+}
+
+static inline void lvmlockd_disconnect(void)
+{
+}
+
+static inline void lvmlockd_connect(void)
{
- return 0;
}
-static inline int lvmlockd_active(void)
+static inline int lvmlockd_use(void)
{
return 0;
}
diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c
index bb1fb3652..fb7fb53c6 100644
--- a/lib/metadata/metadata.c
+++ b/lib/metadata/metadata.c
@@ -4535,9 +4535,9 @@ static int _access_vg_lock_type(struct cmd_context *cmd, struct volume_group *vg
return 1;
/*
- * When lvmlockd is not running, only allow read access to the VG.
+ * When lvmlockd is not used, only allow read access to the VG.
*/
- if (!lvmlockd_active()) {
+ if (!lvmlockd_use()) {
if (lockd_state & LDST_EX) {
log_error("Cannot access VG %s which requires lvmlockd for lock_type %s.",
vg->name, vg->lock_type);
diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c
index 92cc933c3..035fe5820 100644
--- a/tools/lvmcmdline.c
+++ b/tools/lvmcmdline.c
@@ -1111,7 +1111,9 @@ static int _get_settings(struct cmd_context *cmd)
if (!arg_count(cmd, sysinit_ARG)) {
lvmetad_connect_or_warn();
- lvmlockd_connect_or_warn();
+
+ if (lvmlockd_use())
+ lvmlockd_connect();
}
if (arg_count(cmd, nosuffix_ARG))
diff --git a/tools/pvmove.c b/tools/pvmove.c
index cc9aeca84..03ad656b4 100644
--- a/tools/pvmove.c
+++ b/tools/pvmove.c
@@ -847,12 +847,12 @@ int pvmove(struct cmd_context *cmd, int argc, char **argv)
return ECMD_FAILED;
}
- if (lvmlockd_active() && !lvmpolld_use()) {
+ if (lvmlockd_use() && !lvmpolld_use()) {
log_error("Enable lvmpolld when using lvmlockd.");
return ECMD_FAILED;
}
- if (lvmlockd_active() && !argc) {
+ if (lvmlockd_use() && !argc) {
/*
* TODO: move process_each_vg from polldaemon up to here,
* then we can remove this limitation.