void lvmcache_seed_infos_from_lvmetad(struct cmd_context *cmd)
{
- if (!lvmetad_active() || _has_scanned)
+ if (!lvmetad_used() || _has_scanned)
return;
if (!lvmetad_pv_list_to_lvmcache(cmd)) {
char vgid_found[ID_LEN + 1] __attribute__((aligned(8)));
if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, vgid))) {
- if (!lvmetad_active())
+ if (!lvmetad_used())
return NULL; /* too bad */
/* If we don't have the info but we have lvmetad, we can ask
* there before failing. */
int r = 0;
- if (lvmetad_active())
+ if (lvmetad_used())
return 1;
/* Avoid recursion when a PVID can't be found! */
* using the classic scanning mechanics, and read from disk or from
* lvmcache.
*/
- if (lvmetad_active() && !precommitted) {
+ if (lvmetad_used() && !precommitted) {
/* Still serve the locally cached VG if available */
if (vgid && (vginfo = lvmcache_vginfo_from_vgid(vgid)) &&
vginfo->vgmetadata && (vg = vginfo->cached_vg))
struct label *label;
if ((info = lvmcache_info_from_pvid((const char *) pvid, 0))) {
- if (lvmetad_active()) {
+ if (lvmetad_used()) {
if (info->label && label_sector)
*label_sector = info->label->sector;
return info->dev;
int lvmcache_vgid_is_cached(const char *vgid) {
struct lvmcache_vginfo *vginfo;
- if (lvmetad_active())
+ if (lvmetad_used())
return 1;
vginfo = lvmcache_vginfo_from_vgid(vgid);
struct lvmcache_vginfo *vginfo;
int ret = 0;
- if (lvmetad_active())
+ if (lvmetad_used())
return lvmetad_vg_is_foreign(cmd, vgname, vgid);
if ((vginfo = lvmcache_vginfo_from_vgid(vgid)))
daemon_close(_lvmetad);
_lvmetad_connected = 0;
+ _lvmetad_use = 0;
+ _lvmetad_cmd = NULL;
}
-void lvmetad_init(struct cmd_context *cmd)
+int lvmetad_connect(struct cmd_context *cmd)
{
- if (!_lvmetad_use && !access(getenv("LVM_LVMETAD_PIDFILE") ? : LVMETAD_PIDFILE, F_OK))
- log_warn("WARNING: lvmetad is running but disabled."
- " Restart lvmetad before enabling it!");
-
- if (_lvmetad_connected)
- log_debug(INTERNAL_ERROR "Refreshing lvmetad global handle while connection with the daemon is active");
-
- _lvmetad_cmd = cmd;
-}
-
-static void _lvmetad_connect(void)
-{
- if (!_lvmetad_use || !_lvmetad_socket || _lvmetad_connected)
- return;
+ if (access(getenv("LVM_LVMETAD_PIDFILE") ? : LVMETAD_PIDFILE, F_OK)) {
+ log_debug_lvmetad("Failed to connect to lvmetad: not running.");
+ _lvmetad_connected = 0;
+ _lvmetad_use = 0;
+ _lvmetad_cmd = NULL;
+ return 0;
+ }
_lvmetad = lvmetad_open(_lvmetad_socket);
+
if (_lvmetad.socket_fd >= 0 && !_lvmetad.error) {
log_debug_lvmetad("Successfully connected to lvmetad on fd %d.",
_lvmetad.socket_fd);
_lvmetad_connected = 1;
- }
-
- if (!_lvmetad_connected)
+ _lvmetad_use = 1;
+ _lvmetad_cmd = cmd;
+ return 1;
+ } else {
+ log_debug_lvmetad("Failed to connect to lvmetad: %s", strerror(_lvmetad.error));
+ _lvmetad_connected = 0;
_lvmetad_use = 0;
-}
-
-void lvmetad_connect_or_warn(void)
-{
- if (!_lvmetad_use)
- return;
-
- if (!_lvmetad_connected && !_lvmetad.error) {
- _lvmetad_connect();
-
- if ((_lvmetad.socket_fd < 0 || _lvmetad.error))
- log_warn("WARNING: Failed to connect to lvmetad. Falling back to internal scanning.");
+ _lvmetad_cmd = NULL;
+ return 0;
}
-
- if (!_lvmetad_connected)
- _lvmetad_use = 0;
}
int lvmetad_used(void)
return _lvmetad_use;
}
+void lvmetad_make_unused(struct cmd_context *cmd)
+{
+ lvmetad_disconnect();
+
+ if (cmd && !refresh_filters(cmd))
+ stack;
+}
+
int lvmetad_socket_present(void)
{
const char *socket = _lvmetad_socket ?: LVMETAD_SOCKET;
return !r;
}
-int lvmetad_active(void)
-{
- lvmetad_connect_or_warn();
-
- return _lvmetad_connected;
-}
-
-void lvmetad_set_active(struct cmd_context *cmd, int active)
+void lvmetad_set_socket(const char *sock)
{
- _lvmetad_use = active;
-
- if (!active && lvmetad_active())
- lvmetad_disconnect();
-
- if (cmd && !refresh_filters(cmd))
- stack;
+ _lvmetad_socket = sock;
}
/*
_lvmetad_token = NULL;
}
-void lvmetad_set_socket(const char *sock)
-{
- _lvmetad_socket = sock;
-}
-
/*
* Check if lvmetad's token matches our token. The token is a hash of the
* global filter used to populate lvmetad. The lvmetad token was set by the
fail:
daemon_reply_destroy(reply);
/* The command will not use lvmetad and will revert to scanning. */
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
return 0;
}
unsigned int wait_sec = 0;
uint64_t now = 0, wait_start = 0;
+ if (!_lvmetad_connected || !_lvmetad_use) {
+ reply.error = ECONNRESET;
+ return reply;
+ }
+
if (cmd)
wait_sec = (unsigned int)find_config_tree_int(cmd, global_lvmetad_update_wait_time_CFG, NULL);
retry:
daemon_request_destroy(req);
+ if (reply.error == ECONNRESET)
+ log_warn("WARNING: lvmetad connection failed, cannot reconnect.");
+
if (reply.error)
goto out;
struct pv_list *pvl;
int rescan = 0;
- if (!lvmetad_active())
+ if (!lvmetad_used())
return NULL;
if (vgid) {
if (!vg)
return 0;
- if (!lvmetad_active() || test_mode())
+ if (!lvmetad_used() || test_mode())
return 1; /* fake it */
if (!vg->cft_precommitted) {
daemon_reply reply;
int result;
- if (!lvmetad_active() || test_mode())
+ if (!lvmetad_used() || test_mode())
return 1; /* just fake it */
if (!id_write_format(&vg->id, uuid, sizeof(uuid)))
int result = 0;
struct dm_config_node *cn;
- if (!lvmetad_active())
+ if (!lvmetad_used())
return_0;
if (!id_write_format(&pvid, uuid, sizeof(uuid)))
daemon_reply reply;
struct dm_config_node *cn;
- if (!lvmetad_active())
+ if (!lvmetad_used())
return_0;
log_debug_lvmetad("Asking lvmetad for PV on %s", dev_name(dev));
daemon_reply reply;
struct dm_config_node *cn;
- if (!lvmetad_active())
+ if (!lvmetad_used())
return 1;
log_debug_lvmetad("Asking lvmetad for complete list of known PVs");
daemon_reply reply;
struct dm_config_node *cn;
- if (!lvmetad_active())
+ if (!lvmetad_used())
return 1;
log_debug_lvmetad("Asking lvmetad for complete list of known VGs");
int64_t changed;
int result;
- if (!lvmetad_active() || test_mode())
+ if (!lvmetad_used() || test_mode())
return 1;
if (!id_write_format(pvid, uuid, sizeof(uuid)))
int result;
int found;
- if (!lvmetad_active() || test_mode())
+ if (!lvmetad_used() || test_mode())
return 1;
/*
struct format_instance_ctx fic = { .type = 0 };
struct metadata_area *mda;
- if (!lvmetad_active()) {
+ if (!lvmetad_used()) {
log_error("Cannot proceed since lvmetad is not active.");
return 0;
}
int replaced_update = 0;
int retries = 0;
- if (!lvmetad_active()) {
+ if (!lvmetad_used()) {
log_error("Cannot proceed since lvmetad is not active.");
return 0;
}
const char *pvid_txt;
const char *vgid;
- if (!lvmetad_active())
+ if (!lvmetad_used())
return 1;
log_debug_lvmetad("Asking lvmetad for complete list of known PVs");
return;
}
- if (!lvmetad_active())
+ if (!lvmetad_used())
return;
log_debug_lvmetad("Validating global lvmetad cache");
*/
if (!lvmetad_pvscan_all_devs(cmd, NULL, 1)) {
log_warn("WARNING: Not using lvmetad because cache update failed.");
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
return;
}
if (lvmetad_is_disabled(cmd, &reason)) {
log_warn("WARNING: Not using lvmetad because %s.", reason);
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
return;
}
enum activation_change activate);
#ifdef LVMETAD_SUPPORT
-/*
- * Sets up a global handle for our process.
- */
-void lvmetad_init(struct cmd_context *);
/*
- * Override the use of lvmetad for retrieving scan results and metadata.
+ * lvmetad_connect: connect to lvmetad
+ * lvmetad_disconnect: disconnect from lvmetad
+ * lvmetad_make_unused: disconnect from lvmetad and refresh cmd filter
+ * lvmetad_used: check if lvmetad is being used (i.e. is connected)
*/
-void lvmetad_set_active(struct cmd_context *, int);
+int lvmetad_connect(struct cmd_context *cmd);
+void lvmetad_disconnect(void);
+void lvmetad_make_unused(struct cmd_context *cmd);
+int lvmetad_used(void);
+
/*
* Configure the socket that lvmetad_init will use to connect to the daemon.
*/
void lvmetad_set_socket(const char *);
-/*
- * Check whether lvmetad is used.
- */
-int lvmetad_used(void);
-
/*
* Check if lvmetad socket is present (either the one set by lvmetad_set_socket
- * or the default one if not set). For example, this may be used before calling
- * lvmetad_active() check that does connect to the socket - this would produce
- * various connection errors if the socket is not present.
+ * or the default one if not set).
*/
int lvmetad_socket_present(void);
-/*
- * Check whether lvmetad is active (where active means both that it is running
- * and that we have a working connection with it). It opens new connection
- * with lvmetad in the process when lvmetad is supposed to be used and the
- * connection is not open yet.
- */
-int lvmetad_active(void);
-
-/*
- * Connect to lvmetad unless the connection is already open or lvmetad is
- * not configured to be used. If we fail to connect, print a warning.
- */
-void lvmetad_connect_or_warn(void);
-
-/*
- * Drop connection to lvmetad. A subsequent lvmetad_connect_or_warn or
- * lvmetad_active will re-establish the connection (possibly at a
- * different socket path).
- */
-void lvmetad_disconnect(void);
-
/*
* Set the "lvmetad validity token" (currently only consists of the lvmetad
* filter. See lvm.conf.
# else /* LVMETAD_SUPPORT */
-# define lvmetad_init(cmd) do { } while (0)
# define lvmetad_disconnect() do { } while (0)
-# define lvmetad_set_active(cmd, a) do { } while (0)
+# define lvmetad_connect(cmd) do { } while (0)
+# define lvmetad_make_unused() do { } while (0)
+# define lvmetad_used() (0)
# define lvmetad_set_socket(a) do { } while (0)
-# define lvmetad_used() (0)
# define lvmetad_socket_present() (0)
-# define lvmetad_active() (0)
-# define lvmetad_connect_or_warn() do { } while (0)
# define lvmetad_set_token(a) do { } while (0)
# define lvmetad_release_token() do { } while (0)
# define lvmetad_vg_update(vg) (1)
return 1;
}
+/*
+ * init_connections();
+ * _init_lvmetad();
+ * lvmetad_disconnect(); (close previous connection)
+ * lvmetad_set_socket(); (set path from config)
+ * lvmetad_set_token(); (set token from filter config)
+ * if (find_config(use_lvmetad))
+ * lvmetad_connect();
+ *
+ * If lvmetad_connect() is successful, lvmetad_used() will
+ * return 1.
+ *
+ * If the config has use_lvmetad=0, then lvmetad_connect()
+ * will not be called, and lvmetad_used() will return 0.
+ *
+ * Other code should use lvmetad_used() to check if the
+ * command is using lvmetad.
+ *
+ */
+
static int _init_lvmetad(struct cmd_context *cmd)
{
const struct dm_config_node *cn;
if (find_config_tree_int(cmd, global_locking_type_CFG, NULL) == 3 &&
find_config_tree_bool(cmd, global_use_lvmetad_CFG, NULL)) {
- log_warn("WARNING: configuration setting use_lvmetad overridden to 0 due to locking_type 3. "
- "Clustered environment not supported by lvmetad yet.");
- lvmetad_set_active(NULL, 0);
- } else
- lvmetad_set_active(NULL, find_config_tree_bool(cmd, global_use_lvmetad_CFG, NULL));
+ log_warn("WARNING: Not using lvmetad because locking_type is 3 (clustered).");
+ return 1;
+ }
+
+ if (!find_config_tree_bool(cmd, global_use_lvmetad_CFG, NULL)) {
+ if (lvmetad_socket_present())
+ log_warn("WARNING: Not using lvmetad because config setting use_lvmetad=0.");
+ return 1;
+ }
+
+ if (!lvmetad_connect(cmd)) {
+ log_warn("WARNING: Failed to connect to lvmetad. Falling back to device scanning.");
+ return 1;
+ }
+
+ if (!lvmetad_used()) {
+ /* This should never happen. */
+ log_error(INTERNAL_ERROR "lvmetad setup incorrect");
+ return 0;
+ }
- lvmetad_init(cmd);
return 1;
}
if (!vg_write(vg))
return_0;
- if (drop_lvmetad && lvmetad_active()) {
+ if (drop_lvmetad && lvmetad_used()) {
struct volume_group *vg_lvmetad = lvmetad_vg_lookup(cmd, vg->name, NULL);
if (vg_lvmetad) {
/* FIXME Cope with failure to update lvmetad */
if (!(dev = dev_cache_get(pv_name, fmt->cmd->filter)))
return_0;
- if (lvmetad_active()) {
+ if (lvmetad_used()) {
info = lvmcache_info_from_pvid(dev->pvid, 0);
if (!info && !lvmetad_pv_lookup_by_dev(fmt->cmd, dev, NULL))
return 0;
const struct dm_config_value *cv;
// TODO if this is pvscan --cache, we want this check back.
- if (lvmetad_active())
+ if (lvmetad_used())
return 1;
/*
return 0;
}
- /* TODO is the !lvmetad_active() too coarse here? */
- if (!pv->dev && !lvmetad_active())
+ /* TODO is the !lvmetad_used() too coarse here? */
+ if (!pv->dev && !lvmetad_used())
pv->status |= MISSING_PV;
if ((pv->status & MISSING_PV) && pv->dev && pv_mda_used_count(pv) == 0) {
return _vg_read_orphans(cmd, warn_flags, vgname, consistent);
}
- if (lvmetad_active() && !use_precommitted) {
+ if (lvmetad_used() && !use_precommitted) {
if ((correct_vg = lvmcache_get_vg(cmd, vgname, vgid, precommitted))) {
dm_list_iterate_items(pvl, &correct_vg->pvs)
if (pvl->pv->dev)
if (!(dev = dev_cache_get(pv_name, cmd->filter)))
return_NULL;
- if (lvmetad_active()) {
+ if (lvmetad_used()) {
info = lvmcache_info_from_pvid(dev->pvid, 0);
if (!info) {
if (!lvmetad_pv_lookup_by_dev(cmd, dev, &found))
return 1;
}
- if (lvmetad_active()) {
+ if (lvmetad_used()) {
/*
* This just gets the list of names/ids from lvmetad
* and does not populate lvmcache.
* need to take special care here as lvmetad service does
* not neet to be running at this moment yet - it could be
* just too early during system initialization time.
- */
- if (arg_count(cmd, sysinit_ARG) && lvmetad_used() &&
- arg_uint_value(cmd, activate_ARG, 0) == CHANGE_AAY) {
- if (!lvmetad_socket_present()) {
- /*
- * If lvmetad socket is not present yet,
- * the service is just not started. It'll
- * be started a bit later so we need to do
- * the activation without lvmetad which means
- * direct activation instead of autoactivation.
- */
- log_warn("lvmetad is not active yet, using direct activation during sysinit");
- lvmetad_set_active(cmd, 0);
- } else if (lvmetad_active()) {
- /*
- * If lvmetad is active already, we want
- * to make use of the autoactivation.
- */
- log_warn("lvmetad is active, skipping direct activation during sysinit");
+ */
+ if (arg_count(cmd, sysinit_ARG) && (arg_uint_value(cmd, activate_ARG, 0) == CHANGE_AAY)) {
+ if (lvmetad_used()) {
+ log_warn("WARNING: lvmetad is active, skipping direct activation during sysinit");
return ECMD_PROCESSED;
}
}
*/
cmd->vg_read_print_access_error = 1;
- if (!arg_count(cmd, sysinit_ARG))
- lvmetad_connect_or_warn();
-
if (arg_count(cmd, nosuffix_ARG))
cmd->current_settings.suffix = 0;
int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
{
- struct dm_config_tree *config_string_cft;
- struct dm_config_tree *config_profile_command_cft, *config_profile_metadata_cft;
+ struct dm_config_tree *config_string_cft, *config_profile_command_cft, *config_profile_metadata_cft;
const char *reason = NULL;
int ret = 0;
int locking_type;
log_warn("WARNING: Disabling lvmetad cache which does not support obsolete metadata.");
lvmetad_set_disabled(cmd, "LVM1");
log_warn("WARNING: Not using lvmetad because lvm1 format is used.");
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
if (cmd->metadata_read_only &&
}
if (lvmetad_used()) {
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
log_verbose("Not using lvmetad because read-only is set.");
}
} else if (arg_count(cmd, nolocking_ARG))
if (cmd->include_foreign_vgs || !lvmetad_token_matches(cmd)) {
if (lvmetad_used() && !lvmetad_pvscan_all_devs(cmd, NULL, cmd->include_foreign_vgs ? 1 : 0)) {
log_warn("WARNING: Not using lvmetad because cache update failed.");
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
}
if (lvmetad_used() && lvmetad_is_disabled(cmd, &reason)) {
log_warn("WARNING: Not using lvmetad because %s.", reason);
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
}
if (lvmetad_used() && (!lvmetad_token_matches(cmd) || lvmetad_is_disabled(cmd, &reason))) {
if (lvmetad_used() && !lvmetad_pvscan_all_devs(cmd, NULL, 0)) {
log_warn("WARNING: Not using lvmetad because cache update failed.");
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
if (lvmetad_used() && lvmetad_is_disabled(cmd, &reason)) {
log_warn("WARNING: Not using lvmetad because %s.", reason);
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
/*
* we lock VG_GLOBAL to enable use of metadata cache.
* This can pause alongide pvscan or vgscan process for a while.
*/
- if (!lvmetad_active()) {
+ if (!lvmetad_used()) {
lock_global = 1;
if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_READ, NULL)) {
log_error("Unable to obtain global lock.");
if (lvmetad_used() && (!lvmetad_token_matches(cmd) || lvmetad_is_disabled(cmd, &reason))) {
if (lvmetad_used() && !lvmetad_pvscan_all_devs(cmd, NULL, 0)) {
log_warn("WARNING: Not using lvmetad because cache update failed.");
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
if (lvmetad_used() && lvmetad_is_disabled(cmd, &reason)) {
log_warn("WARNING: Not using lvmetad because %s.", reason);
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
}
* This can pause alongide pvscan or vgscan process for a while.
*/
if (args_are_pvs && (report_type == PVS || report_type == PVSEGS) &&
- !lvmetad_active()) {
+ !lvmetad_used()) {
lock_global = 1;
if (!lock_vol(cmd, VG_GLOBAL, LCK_VG_READ, NULL)) {
log_error("Unable to obtain global lock.");
* not neet to be running at this moment yet - it could be
* just too early during system initialization time.
*/
- if (arg_count(cmd, sysinit_ARG) && lvmetad_used() &&
- arg_uint_value(cmd, activate_ARG, 0) == CHANGE_AAY) {
- if (!lvmetad_socket_present()) {
- /*
- * If lvmetad socket is not present yet,
- * the service is just not started. It'll
- * be started a bit later so we need to do
- * the activation without lvmetad which means
- * direct activation instead of autoactivation.
- */
- log_warn("lvmetad is not active yet, using direct activation during sysinit");
- lvmetad_set_active(cmd, 0);
- } else if (lvmetad_active()) {
- /*
- * If lvmetad is active already, we want
- * to make use of the autoactivation.
- */
- log_warn("lvmetad is active, skipping direct activation during sysinit");
+ if (arg_count(cmd, sysinit_ARG) && (arg_uint_value(cmd, activate_ARG, 0) == CHANGE_AAY)) {
+ if (lvmetad_used()) {
+ log_warn("WARNING: lvmetad is active, skipping direct activation during sysinit");
return ECMD_PROCESSED;
}
}
int vgck(struct cmd_context *cmd, int argc, char **argv)
{
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
return process_each_vg(cmd, argc, argv, NULL, 0, NULL,
&vgck_single);
}
if (lvmetad_used()) {
if (!lvmetad_pvscan_all_devs(cmd, NULL, 1)) {
log_warn("WARNING: Not using lvmetad because cache update failed.");
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
if (lvmetad_used() && lvmetad_is_disabled(cmd, &reason)) {
log_warn("WARNING: Not using lvmetad because %s.", reason);
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
}
if (lvmetad_used() && (arg_is_set(cmd, cache_long_ARG) || !lvmetad_token_matches(cmd) || lvmetad_is_disabled(cmd, &reason))) {
if (lvmetad_used() && !lvmetad_pvscan_all_devs(cmd, NULL, arg_is_set(cmd, cache_long_ARG))) {
log_warn("WARNING: Not using lvmetad because cache update failed.");
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
if (lvmetad_used() && lvmetad_is_disabled(cmd, &reason)) {
log_warn("WARNING: Not using lvmetad because %s.", reason);
- lvmetad_set_active(cmd, 0);
+ lvmetad_make_unused(cmd);
}
}