mrc0mmand / rpms / lvm2

Forked from rpms/lvm2 3 years ago
Clone

Blame SOURCES/lvm2-2_02_106-enahnce-lvmetad-protocol-for-pvscan-to-lock-vg-properly-and-run-refresh-only-when-needed.patch

4c7ee1
 WHATS_NEW                      |  5 +++
4c7ee1
 daemons/lvmetad/lvmetad-core.c | 74 +++++++++++++++++++++++-------------------
4c7ee1
 lib/cache/lvmetad.c            |  9 +++--
4c7ee1
 lib/cache/lvmetad.h            |  3 +-
4c7ee1
 lib/locking/file_locking.c     |  2 +-
4c7ee1
 tools/pvscan.c                 | 32 ++++++++++--------
4c7ee1
 6 files changed, 74 insertions(+), 51 deletions(-)
4c7ee1
4c7ee1
diff --git a/WHATS_NEW b/WHATS_NEW
4c7ee1
index a3d48f3..b5afb19 100644
4c7ee1
--- a/WHATS_NEW
4c7ee1
+++ b/WHATS_NEW
4c7ee1
@@ -1,5 +1,10 @@
4c7ee1
 Version 2.02.106 - 
4c7ee1
 ====================================
4c7ee1
+  Use VG read lock during 'pvscan --cache -aay' autoactivation.
4c7ee1
+  Issue a VG refresh before autoactivation only if the PV has changed/is new.
4c7ee1
+  Add flag to lvmetad protocol to indicate the PV scanned has changed/is new.
4c7ee1
+  Also add vgname to lvmetad protocol when referencing VGs for PVs scanned.
4c7ee1
+  Use correct PATH_MAX for locking dir path.
4c7ee1
   Update API for internal function build_dm_uuid().
4c7ee1
   Do not try to check empty pool with scheduled messages.
4c7ee1
   Fix return value in pool_has_message() when quering for any message.
4c7ee1
diff --git a/daemons/lvmetad/lvmetad-core.c b/daemons/lvmetad/lvmetad-core.c
4c7ee1
index f35db89..1858ee2 100644
4c7ee1
--- a/daemons/lvmetad/lvmetad-core.c
4c7ee1
+++ b/daemons/lvmetad/lvmetad-core.c
4c7ee1
@@ -344,8 +344,8 @@ static response pv_lookup(lvmetad_state *s, request r)
4c7ee1
 		pvid = dm_hash_lookup_binary(s->device_to_pvid, &devt, sizeof(devt));
4c7ee1
 
4c7ee1
 	if (!pvid) {
4c7ee1
-		WARN(s, "pv_lookup: could not find device %" PRIu64, devt);
4c7ee1
 		unlock_pvid_to_pvmeta(s);
4c7ee1
+		WARN(s, "pv_lookup: could not find device %" PRIu64, devt);
4c7ee1
 		dm_config_destroy(res.cft);
4c7ee1
 		return reply_unknown("device not found");
4c7ee1
 	}
4c7ee1
@@ -809,30 +809,28 @@ static response pv_gone(lvmetad_state *s, request r)
4c7ee1
 	pvid_old = dm_hash_lookup_binary(s->device_to_pvid, &device, sizeof(device));
4c7ee1
 	vgid = dm_hash_lookup(s->pvid_to_vgid, pvid);
4c7ee1
 
4c7ee1
-	if (vgid && !(vgid = dm_strdup(vgid))) {
4c7ee1
-		unlock_pvid_to_pvmeta(s);
4c7ee1
-		return reply_fail("out of memory");
4c7ee1
-	}
4c7ee1
-
4c7ee1
 	dm_hash_remove_binary(s->device_to_pvid, &device, sizeof(device));
4c7ee1
 	dm_hash_remove(s->pvid_to_pvmeta, pvid);
4c7ee1
 	unlock_pvid_to_pvmeta(s);
4c7ee1
 
4c7ee1
+	dm_free(pvid_old);
4c7ee1
+
4c7ee1
 	if (vgid) {
4c7ee1
+		if (!(vgid = dm_strdup(vgid)))
4c7ee1
+			return reply_fail("out of memory");
4c7ee1
+
4c7ee1
 		lock_vg(s, vgid);
4c7ee1
 		vg_remove_if_missing(s, vgid, 1);
4c7ee1
 		unlock_vg(s, vgid);
4c7ee1
 		dm_free(vgid);
4c7ee1
 	}
4c7ee1
 
4c7ee1
-	if (pvid_old)
4c7ee1
-		dm_free(pvid_old);
4c7ee1
-
4c7ee1
-	if (pvmeta) {
4c7ee1
-		dm_config_destroy(pvmeta);
4c7ee1
-		return daemon_reply_simple("OK", NULL);
4c7ee1
-	} else
4c7ee1
+	if (!pvmeta)
4c7ee1
 		return reply_unknown("PVID does not exist");
4c7ee1
+
4c7ee1
+	dm_config_destroy(pvmeta);
4c7ee1
+
4c7ee1
+	return daemon_reply_simple("OK", NULL);
4c7ee1
 }
4c7ee1
 
4c7ee1
 static response pv_clear_all(lvmetad_state *s, request r)
4c7ee1
@@ -866,7 +864,7 @@ static response pv_found(lvmetad_state *s, request r)
4c7ee1
 	char *old;
4c7ee1
 	char *pvid_dup;
4c7ee1
 	int complete = 0, orphan = 0;
4c7ee1
-	int64_t seqno = -1, seqno_old = -1;
4c7ee1
+	int64_t seqno = -1, seqno_old = -1, changed = 0;
4c7ee1
 
4c7ee1
 	if (!pvid)
4c7ee1
 		return reply_fail("need PV UUID");
4c7ee1
@@ -876,58 +874,60 @@ static response pv_found(lvmetad_state *s, request r)
4c7ee1
 	if (!dm_config_get_uint64(pvmeta, "pvmeta/device", &device))
4c7ee1
 		return reply_fail("need PV device number");
4c7ee1
 
4c7ee1
+	if (!(cft = dm_config_create()) ||
4c7ee1
+	    (!(pvid_dup = dm_strdup(pvid)))) {
4c7ee1
+		if (cft)
4c7ee1
+			dm_config_destroy(cft);
4c7ee1
+		return reply_fail("out of memory");
4c7ee1
+	}
4c7ee1
+
4c7ee1
 	lock_pvid_to_pvmeta(s);
4c7ee1
 
4c7ee1
+	if ((pvmeta_old_pvid = dm_hash_lookup(s->pvid_to_pvmeta, pvid)))
4c7ee1
+		dm_config_get_uint64(pvmeta_old_pvid->root, "pvmeta/device", &device_old_pvid);
4c7ee1
+
4c7ee1
 	if ((old = dm_hash_lookup_binary(s->device_to_pvid, &device, sizeof(device)))) {
4c7ee1
 		pvmeta_old_dev = dm_hash_lookup(s->pvid_to_pvmeta, old);
4c7ee1
 		dm_hash_remove(s->pvid_to_pvmeta, old);
4c7ee1
 		vgid_old = dm_hash_lookup(s->pvid_to_vgid, old);
4c7ee1
 	}
4c7ee1
 
4c7ee1
-	if ((pvmeta_old_pvid = dm_hash_lookup(s->pvid_to_pvmeta, pvid)))
4c7ee1
-		dm_config_get_uint64(pvmeta_old_pvid->root, "pvmeta/device", &device_old_pvid);
4c7ee1
-
4c7ee1
 	DEBUGLOG(s, "pv_found %s, vgid = %s, device = %" PRIu64 " (previously %" PRIu64 "), old = %s",
4c7ee1
 		 pvid, vgid, device, device_old_pvid, old);
4c7ee1
 
4c7ee1
-	dm_free(old);
4c7ee1
-
4c7ee1
-	if (!(cft = dm_config_create()) ||
4c7ee1
-	    !(cft->root = dm_config_clone_node(cft, pvmeta, 0))) {
4c7ee1
-		unlock_pvid_to_pvmeta(s);
4c7ee1
-		if (cft)
4c7ee1
-			dm_config_destroy(cft);
4c7ee1
-		return reply_fail("out of memory");
4c7ee1
-	}
4c7ee1
+	if (!(cft->root = dm_config_clone_node(cft, pvmeta, 0)))
4c7ee1
+                goto out_of_mem;
4c7ee1
 
4c7ee1
-	if (!(pvid_dup = dm_strdup(pvid))) {
4c7ee1
-		unlock_pvid_to_pvmeta(s);
4c7ee1
-		dm_config_destroy(cft);
4c7ee1
-		return reply_fail("out of memory");
4c7ee1
-	}
4c7ee1
+	if (!pvmeta_old_pvid || compare_config(pvmeta_old_pvid->root, cft->root))
4c7ee1
+		changed |= 1;
4c7ee1
 
4c7ee1
 	if (pvmeta_old_pvid && device != device_old_pvid) {
4c7ee1
 		DEBUGLOG(s, "pv %s no longer on device %" PRIu64, pvid, device_old_pvid);
4c7ee1
 		dm_free(dm_hash_lookup_binary(s->device_to_pvid, &device_old_pvid, sizeof(device_old_pvid)));
4c7ee1
 		dm_hash_remove_binary(s->device_to_pvid, &device_old_pvid, sizeof(device_old_pvid));
4c7ee1
+		changed |= 1;
4c7ee1
 	}
4c7ee1
 
4c7ee1
 	if (!dm_hash_insert(s->pvid_to_pvmeta, pvid, cft) ||
4c7ee1
 	    !dm_hash_insert_binary(s->device_to_pvid, &device, sizeof(device), (void*)pvid_dup)) {
4c7ee1
 		dm_hash_remove(s->pvid_to_pvmeta, pvid);
4c7ee1
+out_of_mem:
4c7ee1
 		unlock_pvid_to_pvmeta(s);
4c7ee1
 		dm_config_destroy(cft);
4c7ee1
 		dm_free(pvid_dup);
4c7ee1
+		dm_free(old);
4c7ee1
 		return reply_fail("out of memory");
4c7ee1
 	}
4c7ee1
 
4c7ee1
+	unlock_pvid_to_pvmeta(s);
4c7ee1
+
4c7ee1
+	dm_free(old);
4c7ee1
+
4c7ee1
 	if (pvmeta_old_pvid)
4c7ee1
 		dm_config_destroy(pvmeta_old_pvid);
4c7ee1
 	if (pvmeta_old_dev && pvmeta_old_dev != pvmeta_old_pvid)
4c7ee1
 		dm_config_destroy(pvmeta_old_dev);
4c7ee1
 
4c7ee1
-	unlock_pvid_to_pvmeta(s);
4c7ee1
-
4c7ee1
 	if (metadata) {
4c7ee1
 		if (!vgid)
4c7ee1
 			return reply_fail("need VG UUID");
4c7ee1
@@ -939,6 +939,7 @@ static response pv_found(lvmetad_state *s, request r)
4c7ee1
 
4c7ee1
 		if (!update_metadata(s, vgname, vgid, metadata, &seqno_old))
4c7ee1
 			return reply_fail("metadata update failed");
4c7ee1
+		changed |= (seqno_old != dm_config_find_int(metadata, "metadata/seqno", -1));
4c7ee1
 	} else {
4c7ee1
 		lock_pvid_to_vgid(s);
4c7ee1
 		vgid = dm_hash_lookup(s->pvid_to_vgid, pvid);
4c7ee1
@@ -956,6 +957,11 @@ static response pv_found(lvmetad_state *s, request r)
4c7ee1
 			return reply_fail("non-orphan VG without metadata encountered");
4c7ee1
 		}
4c7ee1
 		unlock_vg(s, vgid);
4c7ee1
+
4c7ee1
+		// TODO: separate vgid->vgname lock
4c7ee1
+		lock_vgid_to_metadata(s);
4c7ee1
+		vgname = dm_hash_lookup(s->vgid_to_vgname, vgid);
4c7ee1
+		unlock_vgid_to_metadata(s);
4c7ee1
 	}
4c7ee1
 
4c7ee1
 	if (vgid_old && (!vgid || strcmp(vgid, vgid_old))) {
4c7ee1
@@ -971,7 +977,9 @@ static response pv_found(lvmetad_state *s, request r)
4c7ee1
 	return daemon_reply_simple("OK",
4c7ee1
 				   "status = %s", orphan ? "orphan" :
4c7ee1
 				                     (complete ? "complete" : "partial"),
4c7ee1
+				   "changed = %d", changed,
4c7ee1
 				   "vgid = %s", vgid ? vgid : "#orphan",
4c7ee1
+				   "vgname = %s", vgname ? vgname : "#orphan",
4c7ee1
 				   "seqno_before = %"PRId64, seqno_old,
4c7ee1
 				   "seqno_after = %"PRId64, seqno,
4c7ee1
 				   NULL);
4c7ee1
diff --git a/lib/cache/lvmetad.c b/lib/cache/lvmetad.c
4c7ee1
index c994874..38d9042 100644
4c7ee1
--- a/lib/cache/lvmetad.c
4c7ee1
+++ b/lib/cache/lvmetad.c
4c7ee1
@@ -749,7 +749,8 @@ int lvmetad_pv_found(const struct id *pvid, struct device *dev, const struct for
4c7ee1
 	daemon_reply reply;
4c7ee1
 	struct lvmcache_info *info;
4c7ee1
 	struct dm_config_tree *pvmeta, *vgmeta;
4c7ee1
-	const char *status, *vgid;
4c7ee1
+	const char *status, *vgname, *vgid;
4c7ee1
+	int64_t changed;
4c7ee1
 	int result;
4c7ee1
 
4c7ee1
 	if (!lvmetad_active() || test_mode())
4c7ee1
@@ -818,11 +819,13 @@ int lvmetad_pv_found(const struct id *pvid, struct device *dev, const struct for
4c7ee1
 
4c7ee1
 	if (result && handler) {
4c7ee1
 		status = daemon_reply_str(reply, "status", "<missing>");
4c7ee1
+		vgname = daemon_reply_str(reply, "vgname", "<missing>");
4c7ee1
 		vgid = daemon_reply_str(reply, "vgid", "<missing>");
4c7ee1
+		changed = daemon_reply_int(reply, "changed", 0);
4c7ee1
 		if (!strcmp(status, "partial"))
4c7ee1
-			handler(_lvmetad_cmd, vgid, 1, CHANGE_AAY);
4c7ee1
+			handler(_lvmetad_cmd, vgname, vgid, 1, changed, CHANGE_AAY);
4c7ee1
 		else if (!strcmp(status, "complete"))
4c7ee1
-			handler(_lvmetad_cmd, vgid, 0, CHANGE_AAY);
4c7ee1
+			handler(_lvmetad_cmd, vgname, vgid, 0, changed, CHANGE_AAY);
4c7ee1
 		else if (!strcmp(status, "orphan"))
4c7ee1
 			;
4c7ee1
 		else
4c7ee1
diff --git a/lib/cache/lvmetad.h b/lib/cache/lvmetad.h
4c7ee1
index 85b71c2..d9aa77f 100644
4c7ee1
--- a/lib/cache/lvmetad.h
4c7ee1
+++ b/lib/cache/lvmetad.h
4c7ee1
@@ -23,7 +23,8 @@ struct dm_config_tree;
4c7ee1
 enum activation_change;
4c7ee1
 
4c7ee1
 typedef int (*activation_handler) (struct cmd_context *cmd,
4c7ee1
-				   const char *vgid, int partial,
4c7ee1
+				   const char *vgname, const char *vgid,
4c7ee1
+				   int partial, int changed,
4c7ee1
 				   enum activation_change activate);
4c7ee1
 
4c7ee1
 #ifdef LVMETAD_SUPPORT
4c7ee1
diff --git a/lib/locking/file_locking.c b/lib/locking/file_locking.c
4c7ee1
index fb84c5b..734e0b4 100644
4c7ee1
--- a/lib/locking/file_locking.c
4c7ee1
+++ b/lib/locking/file_locking.c
4c7ee1
@@ -37,7 +37,7 @@ struct lock_list {
4c7ee1
 };
4c7ee1
 
4c7ee1
 static struct dm_list _lock_list;
4c7ee1
-static char _lock_dir[NAME_LEN];
4c7ee1
+static char _lock_dir[PATH_MAX];
4c7ee1
 static int _prioritise_write_locks;
4c7ee1
 
4c7ee1
 static sig_t _oldhandler;
4c7ee1
diff --git a/tools/pvscan.c b/tools/pvscan.c
4c7ee1
index 4f99f45..5db627a 100644
4c7ee1
--- a/tools/pvscan.c
4c7ee1
+++ b/tools/pvscan.c
4c7ee1
@@ -95,13 +95,13 @@ static void _pvscan_display_single(struct cmd_context *cmd,
4c7ee1
 #define REFRESH_BEFORE_AUTOACTIVATION_RETRY_USLEEP_DELAY 100000
4c7ee1
 
4c7ee1
 static int _auto_activation_handler(struct cmd_context *cmd,
4c7ee1
-				    const char *vgid, int partial,
4c7ee1
+				    const char *vgname, const char *vgid,
4c7ee1
+				    int partial, int changed,
4c7ee1
 				    activation_change_t activate)
4c7ee1
 {
4c7ee1
 	unsigned int refresh_retries = REFRESH_BEFORE_AUTOACTIVATION_RETRIES;
4c7ee1
 	int refresh_done = 0;
4c7ee1
 	struct volume_group *vg;
4c7ee1
-	int consistent = 0;
4c7ee1
 	struct id vgid_raw;
4c7ee1
 	int r = 0;
4c7ee1
 
4c7ee1
@@ -113,8 +113,12 @@ static int _auto_activation_handler(struct cmd_context *cmd,
4c7ee1
 		return_0;
4c7ee1
 
4c7ee1
 	/* NB. This is safe because we know lvmetad is running and we won't hit disk. */
4c7ee1
-	if (!(vg = vg_read_internal(cmd, NULL, (const char *) &vgid_raw, 0, &consistent)))
4c7ee1
-	    return 1;
4c7ee1
+	vg = vg_read(cmd, vgname, (const char *)&vgid_raw, 0);
4c7ee1
+	if (vg_read_error(vg)) {
4c7ee1
+		log_error("Failed to read Volume Group \"%s\" (%s) during autoactivation.", vgname, vgid);
4c7ee1
+		release_vg(vg);
4c7ee1
+		return 0;
4c7ee1
+	}
4c7ee1
 
4c7ee1
 	if (vg_is_clustered(vg)) {
4c7ee1
 		r = 1; goto out;
4c7ee1
@@ -139,16 +143,18 @@ static int _auto_activation_handler(struct cmd_context *cmd,
4c7ee1
 	 *
4c7ee1
 	 * Remove this workaround with "refresh_retries" once we have proper locking in!
4c7ee1
 	 */
4c7ee1
-	while (refresh_retries--) {
4c7ee1
-		if (vg_refresh_visible(vg->cmd, vg)) {
4c7ee1
-			refresh_done = 1;
4c7ee1
-			break;
4c7ee1
+	if (changed) {
4c7ee1
+		while (refresh_retries--) {
4c7ee1
+			if (vg_refresh_visible(vg->cmd, vg)) {
4c7ee1
+				refresh_done = 1;
4c7ee1
+				break;
4c7ee1
+			}
4c7ee1
+			usleep(REFRESH_BEFORE_AUTOACTIVATION_RETRY_USLEEP_DELAY);
4c7ee1
 		}
4c7ee1
-		usleep(REFRESH_BEFORE_AUTOACTIVATION_RETRY_USLEEP_DELAY);
4c7ee1
-	}
4c7ee1
 
4c7ee1
-	if (!refresh_done)
4c7ee1
-		log_warn("%s: refresh before autoactivation failed.", vg->name);
4c7ee1
+		if (!refresh_done)
4c7ee1
+			log_warn("%s: refresh before autoactivation failed.", vg->name);
4c7ee1
+	}
4c7ee1
 
4c7ee1
 	if (!vgchange_activate(vg->cmd, vg, activate)) {
4c7ee1
 		log_error("%s: autoactivation failed.", vg->name);
4c7ee1
@@ -158,7 +164,7 @@ static int _auto_activation_handler(struct cmd_context *cmd,
4c7ee1
 	r = 1;
4c7ee1
 
4c7ee1
 out:
4c7ee1
-	release_vg(vg);
4c7ee1
+	unlock_and_release_vg(cmd, vg, vgname);
4c7ee1
 	return r;
4c7ee1
 }
4c7ee1