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

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