Blame SOURCES/0063-libmultipath-check-if-user_friendly_name-is-in-use.patch

05be62
From e714eb26fddc8768a8de279d1de3ffedab35929e Mon Sep 17 00:00:00 2001
05be62
From: Benjamin Marzinski <bmarzins@redhat.com>
05be62
Date: Fri, 5 Mar 2021 21:40:57 -0600
05be62
Subject: [PATCH] libmultipath: check if user_friendly_name is in use
05be62
05be62
If there are multipath devices that have user_friendly_names but do not
05be62
have their bindings in the bindings_file, get_user_friendly_alias() can
05be62
currently give out those names again. This can result in an incorrect
05be62
entry in the bindings file, and a device that gets created with a WWID
05be62
alias instead of a user_friendly_name. This situation can happen after
05be62
the pivot root, if a multipath device is created in the initramfs.  If
05be62
this device doesn't have a binding in the regular filesystem
05be62
bindings_file and a new multipath device is created before it can add
05be62
its binding, the new device can steal that user_friendly_name during
05be62
multipathd's initial configure.
05be62
05be62
To solve this, get_user_friendly_alias() now calls lookup_binding() with
05be62
a new paramter, telling it to check if the id it found is already in use
05be62
by a diffent device. If so, lookup_binding() will continue to check open
05be62
ids, until it finds one that it not currently in use by a dm device.
05be62
---
05be62
 libmultipath/alias.c | 48 +++++++++++++++++++++++++++++++++++++++++---
05be62
 tests/alias.c        | 22 ++++++++++----------
05be62
 2 files changed, 56 insertions(+), 14 deletions(-)
05be62
05be62
diff --git a/libmultipath/alias.c b/libmultipath/alias.c
05be62
index 14401cae..01f737f4 100644
05be62
--- a/libmultipath/alias.c
05be62
+++ b/libmultipath/alias.c
05be62
@@ -17,6 +17,7 @@
05be62
 #include "vector.h"
05be62
 #include "checkers.h"
05be62
 #include "structs.h"
05be62
+#include "devmapper.h"
05be62
 
05be62
 
05be62
 /*
05be62
@@ -104,6 +105,28 @@ scan_devname(const char *alias, const char *prefix)
05be62
 	return n;
05be62
 }
05be62
 
05be62
+static int
05be62
+id_already_taken(int id, const char *prefix, const char *map_wwid)
05be62
+{
05be62
+	char alias[LINE_MAX];
05be62
+
05be62
+	if (format_devname(alias, id, LINE_MAX, prefix) < 0)
05be62
+		return 0;
05be62
+
05be62
+	if (dm_map_present(alias)) {
05be62
+		char wwid[WWID_SIZE];
05be62
+
05be62
+		/* If both the name and the wwid match, then it's fine.*/
05be62
+		if (dm_get_uuid(alias, wwid, sizeof(wwid)) == 0 &&
05be62
+		    strncmp(map_wwid, wwid, sizeof(wwid)) == 0)
05be62
+			return 0;
05be62
+		condlog(3, "%s: alias '%s' already taken, but not in bindings file. reselecting alias", map_wwid, alias);
05be62
+		return 1;
05be62
+	}
05be62
+	return 0;
05be62
+}
05be62
+
05be62
+
05be62
 /*
05be62
  * Returns: 0   if matching entry in WWIDs file found
05be62
  *         -1   if an error occurs
05be62
@@ -113,7 +136,7 @@ scan_devname(const char *alias, const char *prefix)
05be62
  */
05be62
 static int
05be62
 lookup_binding(FILE *f, const char *map_wwid, char **map_alias,
05be62
-	       const char *prefix)
05be62
+	       const char *prefix, int check_if_taken)
05be62
 {
05be62
 	char buf[LINE_MAX];
05be62
 	unsigned int line_nr = 0;
05be62
@@ -168,12 +191,31 @@ lookup_binding(FILE *f, const char *map_wwid, char **map_alias,
05be62
 			return 0;
05be62
 		}
05be62
 	}
05be62
+	if (!prefix && check_if_taken)
05be62
+		id = -1;
05be62
 	if (id >= smallest_bigger_id) {
05be62
 		if (biggest_id < INT_MAX)
05be62
 			id = biggest_id + 1;
05be62
 		else
05be62
 			id = -1;
05be62
 	}
05be62
+	if (id > 0 && check_if_taken) {
05be62
+		while(id_already_taken(id, prefix, map_wwid)) {
05be62
+			if (id == INT_MAX) {
05be62
+				id = -1;
05be62
+				break;
05be62
+			}
05be62
+			id++;
05be62
+			if (id == smallest_bigger_id) {
05be62
+				if (biggest_id == INT_MAX) {
05be62
+					id = -1;
05be62
+					break;
05be62
+				}
05be62
+				if (biggest_id >= smallest_bigger_id)
05be62
+					id = biggest_id + 1;
05be62
+			}
05be62
+		}
05be62
+	}
05be62
 	if (id < 0) {
05be62
 		condlog(0, "no more available user_friendly_names");
05be62
 		return -1;
05be62
@@ -316,7 +358,7 @@ use_existing_alias (const char *wwid, const char *file, const char *alias_old,
05be62
 		goto out;
05be62
 	}
05be62
 
05be62
-	id = lookup_binding(f, wwid, &alias, NULL);
05be62
+	id = lookup_binding(f, wwid, &alias, NULL, 0);
05be62
 	if (alias) {
05be62
 		condlog(3, "Use existing binding [%s] for WWID [%s]",
05be62
 			alias, wwid);
05be62
@@ -373,7 +415,7 @@ get_user_friendly_alias(const char *wwid, const char *file, const char *prefix,
05be62
 		return NULL;
05be62
 	}
05be62
 
05be62
-	id = lookup_binding(f, wwid, &alias, prefix);
05be62
+	id = lookup_binding(f, wwid, &alias, prefix, 1);
05be62
 	if (id < 0) {
05be62
 		fclose(f);
05be62
 		return NULL;
05be62
diff --git a/tests/alias.c b/tests/alias.c
05be62
index 30414db0..ab1a9325 100644
05be62
--- a/tests/alias.c
05be62
+++ b/tests/alias.c
05be62
@@ -356,7 +356,7 @@ static void lb_empty(void **state)
05be62
 
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "No matching wwid [WWID0] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID0", &alias, NULL);
05be62
+	rc = lookup_binding(NULL, "WWID0", &alias, NULL, 0);
05be62
 	assert_int_equal(rc, 1);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -369,7 +369,7 @@ static void lb_match_a(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHa WWID0\n");
05be62
 	expect_condlog(3, "Found matching wwid [WWID0] in bindings file."
05be62
 		       " Setting alias to MPATHa\n");
05be62
-	rc = lookup_binding(NULL, "WWID0", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID0", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 0);
05be62
 	assert_ptr_not_equal(alias, NULL);
05be62
 	assert_string_equal(alias, "MPATHa");
05be62
@@ -384,7 +384,7 @@ static void lb_nomatch_a(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHa WWID0\n");
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "No matching wwid [WWID1] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID1", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID1", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 2);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -398,7 +398,7 @@ static void lb_match_c(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHc WWID1\n");
05be62
 	expect_condlog(3, "Found matching wwid [WWID1] in bindings file."
05be62
 		       " Setting alias to MPATHc\n");
05be62
-	rc = lookup_binding(NULL, "WWID1", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID1", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 0);
05be62
 	assert_ptr_not_equal(alias, NULL);
05be62
 	assert_string_equal(alias, "MPATHc");
05be62
@@ -414,7 +414,7 @@ static void lb_nomatch_a_c(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHc WWID1\n");
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "No matching wwid [WWID2] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 2);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -428,7 +428,7 @@ static void lb_nomatch_c_a(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHa WWID0\n");
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "No matching wwid [WWID2] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 2);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -443,7 +443,7 @@ static void lb_nomatch_a_b(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHb WWID1\n");
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "No matching wwid [WWID2] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 3);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -459,7 +459,7 @@ static void lb_nomatch_a_b_bad(void **state)
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "Ignoring malformed line 3 in bindings file\n");
05be62
 	expect_condlog(3, "No matching wwid [WWID2] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 3);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -474,7 +474,7 @@ static void lb_nomatch_b_a(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHa WWID0\n");
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "No matching wwid [WWID2] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, 27);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -490,7 +490,7 @@ static void lb_nomatch_int_max(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHa WWID0\n");
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(0, "no more available user_friendly_names\n");
05be62
-	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, -1);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
@@ -505,7 +505,7 @@ static void lb_nomatch_int_max_m1(void **state)
05be62
 	will_return(__wrap_fgets, "MPATHa WWID0\n");
05be62
 	will_return(__wrap_fgets, NULL);
05be62
 	expect_condlog(3, "No matching wwid [WWID2] in bindings file.\n");
05be62
-	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH");
05be62
+	rc = lookup_binding(NULL, "WWID2", &alias, "MPATH", 0);
05be62
 	assert_int_equal(rc, INT_MAX);
05be62
 	assert_ptr_equal(alias, NULL);
05be62
 }
05be62
-- 
05be62
2.17.2
05be62