Blame SOURCES/0023-libmultipath-invert-regexes-that-start-with-exclamat.patch

785c99
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
785c99
From: Benjamin Marzinski <bmarzins@redhat.com>
785c99
Date: Mon, 8 Jun 2020 13:40:16 -0500
785c99
Subject: [PATCH] libmultipath: invert regexes that start with exclamation
785c99
 point
785c99
785c99
The number of devices that multipath needs to blacklist keeps growing,
785c99
and the udev rules already have
785c99
785c99
KERNEL!="sd*|dasd*|nvme*", GOTO="end_mpath"
785c99
785c99
so they only work correctly with these device types.  Instead of
785c99
individually blacklisting every type of device that can't be
785c99
multipathed, multipath's default blacklist should work like the udev
785c99
rule, and blacklist all devices that aren't scsi, dasd, or nvme.
785c99
Unfortunately, the c regex library doesn't support negative lookahead.
785c99
Instead, multipath should treat "!" at the beginning of
785c99
blacklist/exceptions regexes as inverse matching the rest of the regex.
785c99
If users need to match a literal '!' as the first character of their
785c99
regex, they can use "\!" instead. This allows multipath to change the
785c99
default devnode blacklist regex to "!^(sd[a-z]|dasd[a-z]|nvme[0-9])".
785c99
785c99
Extra tests have been added to the blacklist unit tests to verify the
785c99
inverse matching code and the new default blacklist.
785c99
785c99
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
785c99
---
785c99
 libmultipath/blacklist.c   |  41 +++++++++-----
785c99
 libmultipath/blacklist.h   |   3 +
785c99
 multipath/multipath.conf.5 |  17 ++++--
785c99
 tests/blacklist.c          | 110 +++++++++++++++++++++++++++++++++++++
785c99
 tests/test-lib.c           |   2 +-
785c99
 5 files changed, 155 insertions(+), 18 deletions(-)
785c99
785c99
diff --git a/libmultipath/blacklist.c b/libmultipath/blacklist.c
785c99
index 04d3adb9..0c58aa32 100644
785c99
--- a/libmultipath/blacklist.c
785c99
+++ b/libmultipath/blacklist.c
785c99
@@ -15,9 +15,24 @@
785c99
 #include "structs_vec.h"
785c99
 #include "print.h"
785c99
 
785c99
+char *check_invert(char *str, bool *invert)
785c99
+{
785c99
+	if (str[0] == '!') {
785c99
+		*invert = true;
785c99
+		return str + 1;
785c99
+	}
785c99
+	if (str[0] == '\\' && str[1] == '!') {
785c99
+		*invert = false;
785c99
+		return str + 1;
785c99
+	}
785c99
+	*invert = false;
785c99
+	return str;
785c99
+}
785c99
+
785c99
 int store_ble(vector blist, char * str, int origin)
785c99
 {
785c99
 	struct blentry * ble;
785c99
+	char *regex_str;
785c99
 
785c99
 	if (!str)
785c99
 		return 0;
785c99
@@ -30,7 +45,8 @@ int store_ble(vector blist, char * str, int origin)
785c99
 	if (!ble)
785c99
 		goto out;
785c99
 
785c99
-	if (regcomp(&ble->regex, str, REG_EXTENDED|REG_NOSUB))
785c99
+	regex_str = check_invert(str, &ble->invert);
785c99
+	if (regcomp(&ble->regex, regex_str, REG_EXTENDED|REG_NOSUB))
785c99
 		goto out1;
785c99
 
785c99
 	if (!vector_alloc_slot(blist))
785c99
@@ -66,6 +82,7 @@ int alloc_ble_device(vector blist)
785c99
 int set_ble_device(vector blist, char * vendor, char * product, int origin)
785c99
 {
785c99
 	struct blentry_device * ble;
785c99
+	char *regex_str;
785c99
 
785c99
 	if (!blist)
785c99
 		return 1;
785c99
@@ -76,7 +93,8 @@ int set_ble_device(vector blist, char * vendor, char * product, int origin)
785c99
 		return 1;
785c99
 
785c99
 	if (vendor) {
785c99
-		if (regcomp(&ble->vendor_reg, vendor,
785c99
+		regex_str = check_invert(vendor, &ble->vendor_invert);
785c99
+		if (regcomp(&ble->vendor_reg, regex_str,
785c99
 			    REG_EXTENDED|REG_NOSUB)) {
785c99
 			FREE(vendor);
785c99
 			if (product)
785c99
@@ -86,7 +104,8 @@ int set_ble_device(vector blist, char * vendor, char * product, int origin)
785c99
 		ble->vendor = vendor;
785c99
 	}
785c99
 	if (product) {
785c99
-		if (regcomp(&ble->product_reg, product,
785c99
+		regex_str = check_invert(product, &ble->product_invert);
785c99
+		if (regcomp(&ble->product_reg, regex_str,
785c99
 			    REG_EXTENDED|REG_NOSUB)) {
785c99
 			FREE(product);
785c99
 			if (vendor) {
785c99
@@ -108,7 +127,7 @@ match_reglist (vector blist, const char * str)
785c99
 	struct blentry * ble;
785c99
 
785c99
 	vector_foreach_slot (blist, ble, i) {
785c99
-		if (!regexec(&ble->regex, str, 0, NULL, 0))
785c99
+		if (!!regexec(&ble->regex, str, 0, NULL, 0) == ble->invert)
785c99
 			return 1;
785c99
 	}
785c99
 	return 0;
785c99
@@ -125,9 +144,11 @@ match_reglist_device (const struct _vector *blist, const char * vendor,
785c99
 		if (!ble->vendor && !ble->product)
785c99
 			continue;
785c99
 		if ((!ble->vendor ||
785c99
-		     !regexec(&ble->vendor_reg, vendor, 0, NULL, 0)) &&
785c99
+		     !!regexec(&ble->vendor_reg, vendor, 0, NULL, 0) ==
785c99
+		     ble->vendor_invert) &&
785c99
 		    (!ble->product ||
785c99
-		     !regexec(&ble->product_reg, product, 0, NULL, 0)))
785c99
+		     !!regexec(&ble->product_reg, product, 0, NULL, 0) ==
785c99
+		     ble->product_invert))
785c99
 			return 1;
785c99
 	}
785c99
 	return 0;
785c99
@@ -160,13 +181,7 @@ setup_default_blist (struct config * conf)
785c99
 	char * str;
785c99
 	int i;
785c99
 
785c99
-	str = STRDUP("^(ram|zram|raw|loop|fd|md|dm-|sr|scd|st|dcssblk)[0-9]");
785c99
-	if (!str)
785c99
-		return 1;
785c99
-	if (store_ble(conf->blist_devnode, str, ORIGIN_DEFAULT))
785c99
-		return 1;
785c99
-
785c99
-	str = STRDUP("^(td|hd|vd)[a-z]");
785c99
+	str = STRDUP("!^(sd[a-z]|dasd[a-z]|nvme[0-9])");
785c99
 	if (!str)
785c99
 		return 1;
785c99
 	if (store_ble(conf->blist_devnode, str, ORIGIN_DEFAULT))
785c99
diff --git a/libmultipath/blacklist.h b/libmultipath/blacklist.h
785c99
index 2d721f60..4305857d 100644
785c99
--- a/libmultipath/blacklist.h
785c99
+++ b/libmultipath/blacklist.h
785c99
@@ -20,6 +20,7 @@
785c99
 struct blentry {
785c99
 	char * str;
785c99
 	regex_t regex;
785c99
+	bool invert;
785c99
 	int origin;
785c99
 };
785c99
 
785c99
@@ -28,6 +29,8 @@ struct blentry_device {
785c99
 	char * product;
785c99
 	regex_t vendor_reg;
785c99
 	regex_t product_reg;
785c99
+	bool vendor_invert;
785c99
+	bool product_invert;
785c99
 	int origin;
785c99
 };
785c99
 
785c99
diff --git a/multipath/multipath.conf.5 b/multipath/multipath.conf.5
785c99
index 3455b1cc..6dc26f10 100644
785c99
--- a/multipath/multipath.conf.5
785c99
+++ b/multipath/multipath.conf.5
785c99
@@ -1248,6 +1248,16 @@ being handled by multipath-tools.
785c99
 .LP
785c99
 .
785c99
 .
785c99
+In the \fIblacklist\fR and \fIblacklist_exceptions\fR sections, starting a
785c99
+quoted value with an exclamation mark \fB"!"\fR will invert the matching
785c99
+of the rest of the regular expression. For instance, \fB"!^sd[a-z]"\fR will
785c99
+match all values that do not start with \fB"sd[a-z]"\fR. The exclamation mark
785c99
+can be escaped \fB"\\!"\fR to match a literal \fB!\fR at the start of a
785c99
+regular expression. \fBNote:\fR The exclamation mark must be inside quotes,
785c99
+otherwise it will be treated as starting a comment.
785c99
+.LP
785c99
+.
785c99
+.
785c99
 The \fIblacklist_exceptions\fR section is used to revert the actions of the
785c99
 \fIblacklist\fR section. This allows one to selectively include ("whitelist") devices which
785c99
 would normally be excluded via the \fIblacklist\fR section. A common usage is
785c99
@@ -1264,10 +1274,9 @@ unless explicitly stated.
785c99
 Regular expression matching the device nodes to be excluded/included.
785c99
 .RS
785c99
 .PP
785c99
-The default \fIblacklist\fR consists of the regular expressions
785c99
-"^(ram|zram|raw|loop|fd|md|dm-|sr|scd|st|dcssblk)[0-9]" and
785c99
-"^(td|hd|vd)[a-z]". This causes virtual devices, non-disk devices, and some other
785c99
-device types to be excluded from multipath handling by default.
785c99
+The default \fIblacklist\fR consists of the regular expression
785c99
+\fB"!^(sd[a-z]|dasd[a-z]|nvme[0-9])"\fR. This causes all device types other
785c99
+than scsi, dasd, and nvme to be excluded from multipath handling by default.
785c99
 .RE
785c99
 .TP
785c99
 .B wwid
785c99
diff --git a/tests/blacklist.c b/tests/blacklist.c
785c99
index cc8a9a4a..d20e97af 100644
785c99
--- a/tests/blacklist.c
785c99
+++ b/tests/blacklist.c
785c99
@@ -60,20 +60,46 @@ __wrap_udev_list_entry_get_name(struct udev_list_entry *list_entry)
785c99
 	return *(const char **)list_entry;
785c99
 }
785c99
 
785c99
+vector elist_property_default;
785c99
+vector blist_devnode_default;
785c99
 vector blist_devnode_sdb;
785c99
+vector blist_devnode_sdb_inv;
785c99
 vector blist_all;
785c99
 vector blist_device_foo_bar;
785c99
+vector blist_device_foo_inv_bar;
785c99
+vector blist_device_foo_bar_inv;
785c99
 vector blist_device_all;
785c99
 vector blist_wwid_xyzzy;
785c99
+vector blist_wwid_xyzzy_inv;
785c99
 vector blist_protocol_fcp;
785c99
+vector blist_protocol_fcp_inv;
785c99
 vector blist_property_wwn;
785c99
+vector blist_property_wwn_inv;
785c99
 
785c99
 static int setup(void **state)
785c99
 {
785c99
+	struct config conf;
785c99
+
785c99
+	memset(&conf, 0, sizeof(conf));
785c99
+	conf.blist_devnode = vector_alloc();
785c99
+	if (!conf.blist_devnode)
785c99
+		return -1;
785c99
+	conf.elist_property = vector_alloc();
785c99
+	if (!conf.elist_property)
785c99
+		return -1;
785c99
+	if (setup_default_blist(&conf) != 0)
785c99
+		return -1;
785c99
+	elist_property_default = conf.elist_property;
785c99
+	blist_devnode_default = conf.blist_devnode;
785c99
+
785c99
 	blist_devnode_sdb = vector_alloc();
785c99
 	if (!blist_devnode_sdb ||
785c99
 	    store_ble(blist_devnode_sdb, strdup("sdb"), ORIGIN_CONFIG))
785c99
 		return -1;
785c99
+	blist_devnode_sdb_inv = vector_alloc();
785c99
+	if (!blist_devnode_sdb_inv ||
785c99
+	    store_ble(blist_devnode_sdb_inv, strdup("!sdb"), ORIGIN_CONFIG))
785c99
+		return -1;
785c99
 
785c99
 	blist_all = vector_alloc();
785c99
 	if (!blist_all || store_ble(blist_all, strdup(".*"), ORIGIN_CONFIG))
785c99
@@ -84,6 +110,18 @@ static int setup(void **state)
785c99
 	    set_ble_device(blist_device_foo_bar, strdup("foo"), strdup("bar"),
785c99
 			   ORIGIN_CONFIG))
785c99
 		return -1;
785c99
+	blist_device_foo_inv_bar = vector_alloc();
785c99
+	if (!blist_device_foo_inv_bar ||
785c99
+	    alloc_ble_device(blist_device_foo_inv_bar) ||
785c99
+	    set_ble_device(blist_device_foo_inv_bar, strdup("!foo"),
785c99
+			   strdup("bar"), ORIGIN_CONFIG))
785c99
+		return -1;
785c99
+	blist_device_foo_bar_inv = vector_alloc();
785c99
+	if (!blist_device_foo_bar_inv ||
785c99
+	    alloc_ble_device(blist_device_foo_bar_inv) ||
785c99
+	    set_ble_device(blist_device_foo_bar_inv, strdup("foo"),
785c99
+			   strdup("!bar"), ORIGIN_CONFIG))
785c99
+		return -1;
785c99
 
785c99
 	blist_device_all = vector_alloc();
785c99
 	if (!blist_device_all || alloc_ble_device(blist_device_all) ||
785c99
@@ -95,29 +133,50 @@ static int setup(void **state)
785c99
 	if (!blist_wwid_xyzzy ||
785c99
 	    store_ble(blist_wwid_xyzzy, strdup("xyzzy"), ORIGIN_CONFIG))
785c99
 		return -1;
785c99
+	blist_wwid_xyzzy_inv = vector_alloc();
785c99
+	if (!blist_wwid_xyzzy_inv ||
785c99
+	    store_ble(blist_wwid_xyzzy_inv, strdup("!xyzzy"), ORIGIN_CONFIG))
785c99
+		return -1;
785c99
 
785c99
 	blist_protocol_fcp = vector_alloc();
785c99
 	if (!blist_protocol_fcp ||
785c99
 	    store_ble(blist_protocol_fcp, strdup("scsi:fcp"), ORIGIN_CONFIG))
785c99
 		return -1;
785c99
+	blist_protocol_fcp_inv = vector_alloc();
785c99
+	if (!blist_protocol_fcp_inv ||
785c99
+	    store_ble(blist_protocol_fcp_inv, strdup("!scsi:fcp"),
785c99
+		      ORIGIN_CONFIG))
785c99
+		return -1;
785c99
 
785c99
 	blist_property_wwn = vector_alloc();
785c99
 	if (!blist_property_wwn ||
785c99
 	    store_ble(blist_property_wwn, strdup("ID_WWN"), ORIGIN_CONFIG))
785c99
 		return -1;
785c99
+	blist_property_wwn_inv = vector_alloc();
785c99
+	if (!blist_property_wwn_inv ||
785c99
+	    store_ble(blist_property_wwn_inv, strdup("!ID_WWN"), ORIGIN_CONFIG))
785c99
+		return -1;
785c99
 
785c99
 	return 0;
785c99
 }
785c99
 
785c99
 static int teardown(void **state)
785c99
 {
785c99
+	free_blacklist(elist_property_default);
785c99
+	free_blacklist(blist_devnode_default);
785c99
 	free_blacklist(blist_devnode_sdb);
785c99
+	free_blacklist(blist_devnode_sdb_inv);
785c99
 	free_blacklist(blist_all);
785c99
 	free_blacklist_device(blist_device_foo_bar);
785c99
+	free_blacklist_device(blist_device_foo_inv_bar);
785c99
+	free_blacklist_device(blist_device_foo_bar_inv);
785c99
 	free_blacklist_device(blist_device_all);
785c99
 	free_blacklist(blist_wwid_xyzzy);
785c99
+	free_blacklist(blist_wwid_xyzzy_inv);
785c99
 	free_blacklist(blist_protocol_fcp);
785c99
+	free_blacklist(blist_protocol_fcp_inv);
785c99
 	free_blacklist(blist_property_wwn);
785c99
+	free_blacklist(blist_property_wwn_inv);
785c99
 	return 0;
785c99
 }
785c99
 
785c99
@@ -141,6 +200,11 @@ static void test_devnode_blacklist(void **state)
785c99
 	expect_condlog(3, "sdb: device node name blacklisted\n");
785c99
 	assert_int_equal(filter_devnode(blist_devnode_sdb, NULL, "sdb"),
785c99
 			 MATCH_DEVNODE_BLIST);
785c99
+	assert_int_equal(filter_devnode(blist_devnode_sdb_inv, NULL, "sdb"),
785c99
+			 MATCH_NOTHING);
785c99
+	expect_condlog(3, "sdc: device node name blacklisted\n");
785c99
+	assert_int_equal(filter_devnode(blist_devnode_sdb_inv, NULL, "sdc"),
785c99
+			 MATCH_DEVNODE_BLIST);
785c99
 }
785c99
 
785c99
 static void test_devnode_whitelist(void **state)
785c99
@@ -159,12 +223,39 @@ static void test_devnode_missing(void **state)
785c99
 			 MATCH_NOTHING);
785c99
 }
785c99
 
785c99
+static void test_devnode_default(void **state)
785c99
+{
785c99
+	assert_int_equal(filter_devnode(blist_devnode_default, NULL, "sdaa"),
785c99
+			 MATCH_NOTHING);
785c99
+	assert_int_equal(filter_devnode(blist_devnode_default, NULL, "nvme0n1"),
785c99
+			 MATCH_NOTHING);
785c99
+	assert_int_equal(filter_devnode(blist_devnode_default, NULL, "dasda"),
785c99
+			 MATCH_NOTHING);
785c99
+	expect_condlog(3, "hda: device node name blacklisted\n");
785c99
+	assert_int_equal(filter_devnode(blist_devnode_default, NULL, "hda"),
785c99
+			 MATCH_DEVNODE_BLIST);
785c99
+}
785c99
+
785c99
 static void test_device_blacklist(void **state)
785c99
 {
785c99
 	expect_condlog(3, "sdb: (foo:bar) vendor/product blacklisted\n");
785c99
 	assert_int_equal(filter_device(blist_device_foo_bar, NULL, "foo",
785c99
 				       "bar", "sdb"),
785c99
 			 MATCH_DEVICE_BLIST);
785c99
+	assert_int_equal(filter_device(blist_device_foo_inv_bar, NULL, "foo",
785c99
+				        "bar", "sdb"),
785c99
+			 MATCH_NOTHING);
785c99
+	assert_int_equal(filter_device(blist_device_foo_bar_inv, NULL, "foo",
785c99
+				        "bar", "sdb"),
785c99
+			 MATCH_NOTHING);
785c99
+	expect_condlog(3, "sdb: (baz:bar) vendor/product blacklisted\n");
785c99
+	assert_int_equal(filter_device(blist_device_foo_inv_bar, NULL, "baz",
785c99
+				        "bar", "sdb"),
785c99
+			 MATCH_DEVICE_BLIST);
785c99
+	expect_condlog(3, "sdb: (foo:baz) vendor/product blacklisted\n");
785c99
+	assert_int_equal(filter_device(blist_device_foo_bar_inv, NULL, "foo",
785c99
+				        "baz", "sdb"),
785c99
+			 MATCH_DEVICE_BLIST);
785c99
 }
785c99
 
785c99
 static void test_device_whitelist(void **state)
785c99
@@ -191,6 +282,11 @@ static void test_wwid_blacklist(void **state)
785c99
 	expect_condlog(3, "sdb: wwid xyzzy blacklisted\n");
785c99
 	assert_int_equal(filter_wwid(blist_wwid_xyzzy, NULL, "xyzzy", "sdb"),
785c99
 			 MATCH_WWID_BLIST);
785c99
+	assert_int_equal(filter_wwid(blist_wwid_xyzzy_inv, NULL, "xyzzy",
785c99
+				     "sdb"), MATCH_NOTHING);
785c99
+	expect_condlog(3, "sdb: wwid plugh blacklisted\n");
785c99
+	assert_int_equal(filter_wwid(blist_wwid_xyzzy_inv, NULL, "plugh",
785c99
+				     "sdb"), MATCH_WWID_BLIST);
785c99
 }
785c99
 
785c99
 static void test_wwid_whitelist(void **state)
785c99
@@ -218,6 +314,12 @@ static void test_protocol_blacklist(void **state)
785c99
 	expect_condlog(3, "sdb: protocol scsi:fcp blacklisted\n");
785c99
 	assert_int_equal(filter_protocol(blist_protocol_fcp, NULL, &pp),
785c99
 			 MATCH_PROTOCOL_BLIST);
785c99
+	assert_int_equal(filter_protocol(blist_protocol_fcp_inv, NULL, &pp),
785c99
+			 MATCH_NOTHING);
785c99
+	pp.sg_id.proto_id = SCSI_PROTOCOL_ATA;
785c99
+	expect_condlog(3, "sdb: protocol scsi:ata blacklisted\n");
785c99
+	assert_int_equal(filter_protocol(blist_protocol_fcp_inv, NULL, &pp),
785c99
+			 MATCH_PROTOCOL_BLIST);
785c99
 }
785c99
 
785c99
 static void test_protocol_whitelist(void **state)
785c99
@@ -245,10 +347,17 @@ static void test_protocol_missing(void **state)
785c99
 static void test_property_blacklist(void **state)
785c99
 {
785c99
 	static struct udev_device udev = { "sdb", { "ID_FOO", "ID_WWN", "ID_BAR", NULL } };
785c99
+	static struct udev_device udev_inv = { "sdb", { "ID_WWN", NULL } };
785c99
 	conf.blist_property = blist_property_wwn;
785c99
 	expect_condlog(3, "sdb: udev property ID_WWN blacklisted\n");
785c99
 	assert_int_equal(filter_property(&conf, &udev, 3, "ID_SERIAL"),
785c99
 			 MATCH_PROPERTY_BLIST);
785c99
+	conf.blist_property = blist_property_wwn_inv;
785c99
+	expect_condlog(3, "sdb: udev property ID_FOO blacklisted\n");
785c99
+	assert_int_equal(filter_property(&conf, &udev, 3, "ID_SERIAL"),
785c99
+			 MATCH_PROPERTY_BLIST);
785c99
+	assert_int_equal(filter_property(&conf, &udev_inv, 3, "ID_SERIAL"),
785c99
+			 MATCH_NOTHING);
785c99
 }
785c99
 
785c99
 /* the property check works different in that you check all the property
785c99
@@ -482,6 +591,7 @@ int test_blacklist(void)
785c99
 		cmocka_unit_test(test_devnode_blacklist),
785c99
 		cmocka_unit_test(test_devnode_whitelist),
785c99
 		cmocka_unit_test(test_devnode_missing),
785c99
+		cmocka_unit_test(test_devnode_default),
785c99
 		cmocka_unit_test(test_device_blacklist),
785c99
 		cmocka_unit_test(test_device_whitelist),
785c99
 		cmocka_unit_test(test_device_missing),
785c99
diff --git a/tests/test-lib.c b/tests/test-lib.c
785c99
index 59275163..08ff2d8d 100644
785c99
--- a/tests/test-lib.c
785c99
+++ b/tests/test-lib.c
785c99
@@ -15,7 +15,7 @@
785c99
 #include "test-lib.h"
785c99
 
785c99
 const int default_mask = (DI_SYSFS|DI_BLACKLIST|DI_WWID|DI_CHECKER|DI_PRIO);
785c99
-const char default_devnode[] = "sdTEST";
785c99
+const char default_devnode[] = "sdxTEST";
785c99
 const char default_wwid[] = "TEST-WWID";
785c99
 /* default_wwid should be a substring of default_wwid_1! */
785c99
 const char default_wwid_1[] = "TEST-WWID-1";
785c99
-- 
785c99
2.17.2
785c99