Blame SOURCES/0003-policy-support-devices-with-multiple-paths.patch

c025cf
From cd72f9d114da206baa01fd56ff2d8ffcc08f3239 Mon Sep 17 00:00:00 2001
c025cf
From: NeilBrown <neilb@suse.com>
c025cf
Date: Fri, 9 Nov 2018 17:12:33 +1100
c025cf
Subject: [RHEL7.7 PATCH 03/21] policy: support devices with multiple paths.
c025cf
c025cf
As new releases of Linux some time change the name of
c025cf
a path, some distros keep "legacy" names as well.  This
c025cf
is useful, but confuses mdadm which assumes each device has
c025cf
precisely one path.
c025cf
c025cf
So change this assumption:  allow a disk to have several
c025cf
paths, and allow any to match when looking for a policy
c025cf
which matches a disk.
c025cf
c025cf
Reported-and-tested-by: Mariusz Tkaczyk <mariusz.tkaczyk@intel.com>
c025cf
Signed-off-by: NeilBrown <neilb@suse.com>
c025cf
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
c025cf
---
c025cf
 Incremental.c |   5 +-
c025cf
 mdadm.h       |   2 +-
c025cf
 policy.c      | 163 ++++++++++++++++++++++++++++++++--------------------------
c025cf
 3 files changed, 95 insertions(+), 75 deletions(-)
c025cf
c025cf
diff --git a/Incremental.c b/Incremental.c
c025cf
index a4ff7d4..d4d3c35 100644
c025cf
--- a/Incremental.c
c025cf
+++ b/Incremental.c
c025cf
@@ -1080,6 +1080,7 @@ static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
c025cf
 		struct supertype *st2 = NULL;
c025cf
 		char *devname = NULL;
c025cf
 		unsigned long long devsectors;
c025cf
+		char *pathlist[2];
c025cf
 
c025cf
 		if (de->d_ino == 0 || de->d_name[0] == '.' ||
c025cf
 		    (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
c025cf
@@ -1094,7 +1095,9 @@ static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
c025cf
 			/* This is a partition - skip it */
c025cf
 			goto next;
c025cf
 
c025cf
-		pol2 = path_policy(de->d_name, type_disk);
c025cf
+		pathlist[0] = de->d_name;
c025cf
+		pathlist[1] = NULL;
c025cf
+		pol2 = path_policy(pathlist, type_disk);
c025cf
 
c025cf
 		domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
c025cf
 		if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
c025cf
diff --git a/mdadm.h b/mdadm.h
c025cf
index 387e681..705bd9b 100644
c025cf
--- a/mdadm.h
c025cf
+++ b/mdadm.h
c025cf
@@ -1247,7 +1247,7 @@ extern void policyline(char *line, char *type);
c025cf
 extern void policy_add(char *type, ...);
c025cf
 extern void policy_free(void);
c025cf
 
c025cf
-extern struct dev_policy *path_policy(char *path, char *type);
c025cf
+extern struct dev_policy *path_policy(char **paths, char *type);
c025cf
 extern struct dev_policy *disk_policy(struct mdinfo *disk);
c025cf
 extern struct dev_policy *devid_policy(int devid);
c025cf
 extern void dev_policy_free(struct dev_policy *p);
c025cf
diff --git a/policy.c b/policy.c
c025cf
index 258f393..fa67d55 100644
c025cf
--- a/policy.c
c025cf
+++ b/policy.c
c025cf
@@ -189,15 +189,17 @@ struct dev_policy *pol_find(struct dev_policy *pol, char *name)
c025cf
 	return pol;
c025cf
 }
c025cf
 
c025cf
-static char *disk_path(struct mdinfo *disk)
c025cf
+static char **disk_paths(struct mdinfo *disk)
c025cf
 {
c025cf
 	struct stat stb;
c025cf
 	int prefix_len;
c025cf
 	DIR *by_path;
c025cf
 	char symlink[PATH_MAX] = "/dev/disk/by-path/";
c025cf
-	char nm[PATH_MAX];
c025cf
+	char **paths;
c025cf
+	int cnt = 0;
c025cf
 	struct dirent *ent;
c025cf
-	int rv;
c025cf
+
c025cf
+	paths = xmalloc(sizeof(*paths) * (cnt+1));
c025cf
 
c025cf
 	by_path = opendir(symlink);
c025cf
 	if (by_path) {
c025cf
@@ -214,22 +216,13 @@ static char *disk_path(struct mdinfo *disk)
c025cf
 				continue;
c025cf
 			if (stb.st_rdev != makedev(disk->disk.major, disk->disk.minor))
c025cf
 				continue;
c025cf
-			closedir(by_path);
c025cf
-			return xstrdup(ent->d_name);
c025cf
+			paths[cnt++] = xstrdup(ent->d_name);
c025cf
+			paths = xrealloc(paths, sizeof(*paths) * (cnt+1));
c025cf
 		}
c025cf
 		closedir(by_path);
c025cf
 	}
c025cf
-	/* A NULL path isn't really acceptable - use the devname.. */
c025cf
-	sprintf(symlink, "/sys/dev/block/%d:%d", disk->disk.major, disk->disk.minor);
c025cf
-	rv = readlink(symlink, nm, sizeof(nm)-1);
c025cf
-	if (rv > 0) {
c025cf
-		char *dname;
c025cf
-		nm[rv] = 0;
c025cf
-		dname = strrchr(nm, '/');
c025cf
-		if (dname)
c025cf
-			return xstrdup(dname + 1);
c025cf
-	}
c025cf
-	return xstrdup("unknown");
c025cf
+	paths[cnt] = NULL;
c025cf
+	return paths;
c025cf
 }
c025cf
 
c025cf
 char type_part[] = "part";
c025cf
@@ -246,18 +239,53 @@ static char *disk_type(struct mdinfo *disk)
c025cf
 		return type_disk;
c025cf
 }
c025cf
 
c025cf
-static int pol_match(struct rule *rule, char *path, char *type)
c025cf
+static int path_has_part(char *path, char **part)
c025cf
+{
c025cf
+	/* check if path ends with "-partNN" and
c025cf
+	 * if it does, place a pointer to "-pathNN"
c025cf
+	 * in 'part'.
c025cf
+	 */
c025cf
+	int l;
c025cf
+	if (!path)
c025cf
+		return 0;
c025cf
+	l = strlen(path);
c025cf
+	while (l > 1 && isdigit(path[l-1]))
c025cf
+		l--;
c025cf
+	if (l < 5 || strncmp(path+l-5, "-part", 5) != 0)
c025cf
+		return 0;
c025cf
+	*part = path+l-5;
c025cf
+	return 1;
c025cf
+}
c025cf
+
c025cf
+static int pol_match(struct rule *rule, char **paths, char *type, char **part)
c025cf
 {
c025cf
-	/* check if this rule matches on path and type */
c025cf
+	/* Check if this rule matches on any path and type.
c025cf
+	 * If 'part' is not NULL, then 'path' must end in -partN, which
c025cf
+	 * we ignore for matching, and return in *part on success.
c025cf
+	 */
c025cf
 	int pathok = 0; /* 0 == no path, 1 == match, -1 == no match yet */
c025cf
 	int typeok = 0;
c025cf
 
c025cf
-	while (rule) {
c025cf
+	for (; rule; rule = rule->next) {
c025cf
 		if (rule->name == rule_path) {
c025cf
+			char *p;
c025cf
+			int i;
c025cf
 			if (pathok == 0)
c025cf
 				pathok = -1;
c025cf
-			if (path && fnmatch(rule->value, path, 0) == 0)
c025cf
-				pathok = 1;
c025cf
+			if (!paths)
c025cf
+				continue;
c025cf
+			for (i = 0; paths[i]; i++) {
c025cf
+				if (part) {
c025cf
+					if (!path_has_part(paths[i], &p))
c025cf
+						continue;
c025cf
+					*p = '\0';
c025cf
+					*part = p+1;
c025cf
+				}
c025cf
+				if (fnmatch(rule->value, paths[i], 0) == 0)
c025cf
+					pathok = 1;
c025cf
+				if (part)
c025cf
+					*p = '-';
c025cf
+			}
c025cf
 		}
c025cf
 		if (rule->name == rule_type) {
c025cf
 			if (typeok == 0)
c025cf
@@ -265,7 +293,6 @@ static int pol_match(struct rule *rule, char *path, char *type)
c025cf
 			if (type && strcmp(rule->value, type) == 0)
c025cf
 				typeok = 1;
c025cf
 		}
c025cf
-		rule = rule->next;
c025cf
 	}
c025cf
 	return pathok >= 0 && typeok >= 0;
c025cf
 }
c025cf
@@ -286,24 +313,6 @@ static void pol_merge(struct dev_policy **pol, struct rule *rule)
c025cf
 			pol_new(pol, r->name, r->value, metadata);
c025cf
 }
c025cf
 
c025cf
-static int path_has_part(char *path, char **part)
c025cf
-{
c025cf
-	/* check if path ends with "-partNN" and
c025cf
-	 * if it does, place a pointer to "-pathNN"
c025cf
-	 * in 'part'.
c025cf
-	 */
c025cf
-	int l;
c025cf
-	if (!path)
c025cf
-		return 0;
c025cf
-	l = strlen(path);
c025cf
-	while (l > 1 && isdigit(path[l-1]))
c025cf
-		l--;
c025cf
-	if (l < 5 || strncmp(path+l-5, "-part", 5) != 0)
c025cf
-		return 0;
c025cf
-	*part = path+l-5;
c025cf
-	return 1;
c025cf
-}
c025cf
-
c025cf
 static void pol_merge_part(struct dev_policy **pol, struct rule *rule, char *part)
c025cf
 {
c025cf
 	/* copy any name assignments from rule into pol, appending
c025cf
@@ -352,7 +361,7 @@ static int config_rules_has_path = 0;
c025cf
  * path_policy() gathers policy information for the
c025cf
  * disk described in the given a 'path' and a 'type'.
c025cf
  */
c025cf
-struct dev_policy *path_policy(char *path, char *type)
c025cf
+struct dev_policy *path_policy(char **paths, char *type)
c025cf
 {
c025cf
 	struct pol_rule *rules;
c025cf
 	struct dev_policy *pol = NULL;
c025cf
@@ -361,27 +370,24 @@ struct dev_policy *path_policy(char *path, char *type)
c025cf
 	rules = config_rules;
c025cf
 
c025cf
 	while (rules) {
c025cf
-		char *part;
c025cf
+		char *part = NULL;
c025cf
 		if (rules->type == rule_policy)
c025cf
-			if (pol_match(rules->rule, path, type))
c025cf
+			if (pol_match(rules->rule, paths, type, NULL))
c025cf
 				pol_merge(&pol, rules->rule);
c025cf
 		if (rules->type == rule_part && strcmp(type, type_part) == 0)
c025cf
-			if (path_has_part(path, &part)) {
c025cf
-				*part = 0;
c025cf
-				if (pol_match(rules->rule, path, type_disk))
c025cf
-					pol_merge_part(&pol, rules->rule, part+1);
c025cf
-				*part = '-';
c025cf
-			}
c025cf
+			if (pol_match(rules->rule, paths, type_disk, &part))
c025cf
+					pol_merge_part(&pol, rules->rule, part);
c025cf
 		rules = rules->next;
c025cf
 	}
c025cf
 
c025cf
 	/* Now add any metadata-specific internal knowledge
c025cf
 	 * about this path
c025cf
 	 */
c025cf
-	for (i=0; path && superlist[i]; i++)
c025cf
+	for (i=0; paths[0] && superlist[i]; i++)
c025cf
 		if (superlist[i]->get_disk_controller_domain) {
c025cf
 			const char *d =
c025cf
-				superlist[i]->get_disk_controller_domain(path);
c025cf
+				superlist[i]->get_disk_controller_domain(
c025cf
+					paths[0]);
c025cf
 			if (d)
c025cf
 				pol_new(&pol, pol_domain, d, superlist[i]->name);
c025cf
 		}
c025cf
@@ -400,22 +406,34 @@ void pol_add(struct dev_policy **pol,
c025cf
 	pol_dedup(*pol);
c025cf
 }
c025cf
 
c025cf
+static void free_paths(char **paths)
c025cf
+{
c025cf
+	int i;
c025cf
+
c025cf
+	if (!paths)
c025cf
+		return;
c025cf
+
c025cf
+	for (i = 0; paths[i]; i++)
c025cf
+		free(paths[i]);
c025cf
+	free(paths);
c025cf
+}
c025cf
+
c025cf
 /*
c025cf
  * disk_policy() gathers policy information for the
c025cf
  * disk described in the given mdinfo (disk.{major,minor}).
c025cf
  */
c025cf
 struct dev_policy *disk_policy(struct mdinfo *disk)
c025cf
 {
c025cf
-	char *path = NULL;
c025cf
+	char **paths = NULL;
c025cf
 	char *type = disk_type(disk);
c025cf
 	struct dev_policy *pol = NULL;
c025cf
 
c025cf
 	if (config_rules_has_path)
c025cf
-		path = disk_path(disk);
c025cf
+		paths = disk_paths(disk);
c025cf
 
c025cf
-	pol = path_policy(path, type);
c025cf
+	pol = path_policy(paths, type);
c025cf
 
c025cf
-	free(path);
c025cf
+	free_paths(paths);
c025cf
 	return pol;
c025cf
 }
c025cf
 
c025cf
@@ -756,27 +774,26 @@ int policy_check_path(struct mdinfo *disk, struct map_ent *array)
c025cf
 {
c025cf
 	char path[PATH_MAX];
c025cf
 	FILE *f = NULL;
c025cf
-	char *id_path = disk_path(disk);
c025cf
-	int rv;
c025cf
+	char **id_paths = disk_paths(disk);
c025cf
+	int i;
c025cf
+	int rv = 0;
c025cf
 
c025cf
-	if (!id_path)
c025cf
-		return 0;
c025cf
+	for (i = 0; id_paths[i]; i++) {
c025cf
+		snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_paths[i]);
c025cf
+		f = fopen(path, "r");
c025cf
+		if (!f)
c025cf
+			continue;
c025cf
 
c025cf
-	snprintf(path, PATH_MAX, FAILED_SLOTS_DIR "/%s", id_path);
c025cf
-	f = fopen(path, "r");
c025cf
-	if (!f) {
c025cf
-		free(id_path);
c025cf
-		return 0;
c025cf
+		rv = fscanf(f, " %s %x:%x:%x:%x\n",
c025cf
+			    array->metadata,
c025cf
+			    array->uuid,
c025cf
+			    array->uuid+1,
c025cf
+			    array->uuid+2,
c025cf
+			    array->uuid+3);
c025cf
+		fclose(f);
c025cf
+		break;
c025cf
 	}
c025cf
-
c025cf
-	rv = fscanf(f, " %s %x:%x:%x:%x\n",
c025cf
-		    array->metadata,
c025cf
-		    array->uuid,
c025cf
-		    array->uuid+1,
c025cf
-		    array->uuid+2,
c025cf
-		    array->uuid+3);
c025cf
-	fclose(f);
c025cf
-	free(id_path);
c025cf
+	free_paths(id_paths);
c025cf
 	return rv == 5;
c025cf
 }
c025cf
 
c025cf
-- 
c025cf
2.7.5
c025cf