dcavalca / rpms / mdadm

Forked from rpms/mdadm 3 years ago
Clone

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

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