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

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