Blame SOURCES/0011-mdadm-add-map_num_s.patch

b33395
From 5f21d67472ad08c1e96b4385254adba79aa1c467 Mon Sep 17 00:00:00 2001
b33395
From: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
b33395
Date: Thu, 20 Jan 2022 13:18:33 +0100
2ad819
Subject: [PATCH 11/83] mdadm: add map_num_s()
b33395
b33395
map_num() returns NULL if key is not defined. This patch adds
b33395
alternative, non NULL version for cases where NULL is not expected.
b33395
b33395
There are many printf() calls where map_num() is called on variable
b33395
without NULL verification. It works, even if NULL is passed because
b33395
gcc is able to ignore NULL argument quietly but the behavior is
b33395
undefined. For safety reasons such usages will use map_num_s() now.
b33395
It is a potential point of regression.
b33395
b33395
Signed-off-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
b33395
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
b33395
---
b33395
 Assemble.c    |  6 ++----
b33395
 Create.c      |  2 +-
b33395
 Detail.c      |  4 ++--
b33395
 Grow.c        | 16 ++++++++--------
b33395
 Query.c       |  4 ++--
b33395
 maps.c        | 24 ++++++++++++++++++++++++
b33395
 mdadm.c       | 20 ++++++++++----------
b33395
 mdadm.h       |  2 +-
b33395
 super-ddf.c   |  6 +++---
b33395
 super-intel.c |  2 +-
b33395
 super0.c      |  2 +-
b33395
 super1.c      |  2 +-
b33395
 sysfs.c       |  9 +++++----
b33395
 13 files changed, 61 insertions(+), 38 deletions(-)
b33395
b33395
diff --git a/Assemble.c b/Assemble.c
b33395
index 704b8293..9eac9ce0 100644
b33395
--- a/Assemble.c
b33395
+++ b/Assemble.c
b33395
@@ -63,7 +63,7 @@ static void set_array_assembly_status(struct context *c,
b33395
 				   struct assembly_array_info *arr)
b33395
 {
b33395
 	int raid_disks = arr->preexist_cnt + arr->new_cnt;
b33395
-	char *status_msg = map_num(assemble_statuses, status);
b33395
+	char *status_msg = map_num_s(assemble_statuses, status);
b33395
 
b33395
 	if (c->export && result)
b33395
 		*result |= status;
b33395
@@ -77,9 +77,7 @@ static void set_array_assembly_status(struct context *c,
b33395
 		fprintf(stderr, " (%d new)", arr->new_cnt);
b33395
 	if (arr->exp_cnt)
b33395
 		fprintf(stderr, " ( + %d for expansion)", arr->exp_cnt);
b33395
-	if (status_msg)
b33395
-		fprintf(stderr, " %s", status_msg);
b33395
-	fprintf(stderr, ".\n");
b33395
+	fprintf(stderr, " %s.\n", status_msg);
b33395
 }
b33395
 
b33395
 static int name_matches(char *found, char *required, char *homehost, int require_homehost)
b33395
diff --git a/Create.c b/Create.c
b33395
index 9ea19de0..c84c1ac8 100644
b33395
--- a/Create.c
b33395
+++ b/Create.c
b33395
@@ -83,7 +83,7 @@ int default_layout(struct supertype *st, int level, int verbose)
b33395
 
b33395
 	if (layout_map) {
b33395
 		layout = map_name(layout_map, "default");
b33395
-		layout_name = map_num(layout_map, layout);
b33395
+		layout_name = map_num_s(layout_map, layout);
b33395
 	}
b33395
 	if (layout_name && verbose > 0)
b33395
 		pr_err("layout defaults to %s\n", layout_name);
b33395
diff --git a/Detail.c b/Detail.c
b33395
index 95d4cc70..ce7a8445 100644
b33395
--- a/Detail.c
b33395
+++ b/Detail.c
b33395
@@ -495,8 +495,8 @@ int Detail(char *dev, struct context *c)
b33395
 			if (array.state & (1 << MD_SB_CLEAN)) {
b33395
 				if ((array.level == 0) ||
b33395
 				    (array.level == LEVEL_LINEAR))
b33395
-					arrayst = map_num(sysfs_array_states,
b33395
-							  sra->array_state);
b33395
+					arrayst = map_num_s(sysfs_array_states,
b33395
+							       sra->array_state);
b33395
 				else
b33395
 					arrayst = "clean";
b33395
 			} else {
b33395
diff --git a/Grow.c b/Grow.c
b33395
index 18c5719b..8a242b0f 100644
b33395
--- a/Grow.c
b33395
+++ b/Grow.c
b33395
@@ -547,7 +547,7 @@ int Grow_consistency_policy(char *devname, int fd, struct context *c, struct sha
b33395
 	if (s->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
b33395
 	    s->consistency_policy != CONSISTENCY_POLICY_PPL) {
b33395
 		pr_err("Operation not supported for consistency policy %s\n",
b33395
-		       map_num(consistency_policies, s->consistency_policy));
b33395
+		       map_num_s(consistency_policies, s->consistency_policy));
b33395
 		return 1;
b33395
 	}
b33395
 
b33395
@@ -578,14 +578,14 @@ int Grow_consistency_policy(char *devname, int fd, struct context *c, struct sha
b33395
 
b33395
 	if (sra->consistency_policy == (unsigned)s->consistency_policy) {
b33395
 		pr_err("Consistency policy is already %s\n",
b33395
-		       map_num(consistency_policies, s->consistency_policy));
b33395
+		       map_num_s(consistency_policies, s->consistency_policy));
b33395
 		ret = 1;
b33395
 		goto free_info;
b33395
 	} else if (sra->consistency_policy != CONSISTENCY_POLICY_RESYNC &&
b33395
 		   sra->consistency_policy != CONSISTENCY_POLICY_PPL) {
b33395
 		pr_err("Current consistency policy is %s, cannot change to %s\n",
b33395
-		       map_num(consistency_policies, sra->consistency_policy),
b33395
-		       map_num(consistency_policies, s->consistency_policy));
b33395
+		       map_num_s(consistency_policies, sra->consistency_policy),
b33395
+		       map_num_s(consistency_policies, s->consistency_policy));
b33395
 		ret = 1;
b33395
 		goto free_info;
b33395
 	}
b33395
@@ -704,8 +704,8 @@ int Grow_consistency_policy(char *devname, int fd, struct context *c, struct sha
b33395
 	}
b33395
 
b33395
 	ret = sysfs_set_str(sra, NULL, "consistency_policy",
b33395
-			    map_num(consistency_policies,
b33395
-				    s->consistency_policy));
b33395
+			    map_num_s(consistency_policies,
b33395
+					 s->consistency_policy));
b33395
 	if (ret)
b33395
 		pr_err("Failed to change array consistency policy\n");
b33395
 
b33395
@@ -2241,7 +2241,7 @@ size_change_error:
b33395
 		info.new_layout = UnSet;
b33395
 		if (info.array.level == 6 && info.new_level == UnSet) {
b33395
 			char l[40], *h;
b33395
-			strcpy(l, map_num(r6layout, info.array.layout));
b33395
+			strcpy(l, map_num_s(r6layout, info.array.layout));
b33395
 			h = strrchr(l, '-');
b33395
 			if (h && strcmp(h, "-6") == 0) {
b33395
 				*h = 0;
b33395
@@ -2266,7 +2266,7 @@ size_change_error:
b33395
 			info.new_layout = info.array.layout;
b33395
 		else if (info.array.level == 5 && info.new_level == 6) {
b33395
 			char l[40];
b33395
-			strcpy(l, map_num(r5layout, info.array.layout));
b33395
+			strcpy(l, map_num_s(r5layout, info.array.layout));
b33395
 			strcat(l, "-6");
b33395
 			info.new_layout = map_name(r6layout, l);
b33395
 		} else {
b33395
diff --git a/Query.c b/Query.c
b33395
index 23fbf8aa..adcd231e 100644
b33395
--- a/Query.c
b33395
+++ b/Query.c
b33395
@@ -93,7 +93,7 @@ int Query(char *dev)
b33395
 	else {
b33395
 		printf("%s: %s %s %d devices, %d spare%s. Use mdadm --detail for more detail.\n",
b33395
 		       dev, human_size_brief(larray_size,IEC),
b33395
-		       map_num(pers, level), raid_disks,
b33395
+		       map_num_s(pers, level), raid_disks,
b33395
 		       spare_disks, spare_disks == 1 ? "" : "s");
b33395
 	}
b33395
 	st = guess_super(fd);
b33395
@@ -131,7 +131,7 @@ int Query(char *dev)
b33395
 		       dev,
b33395
 		       info.disk.number, info.array.raid_disks,
b33395
 		       activity,
b33395
-		       map_num(pers, info.array.level),
b33395
+		       map_num_s(pers, info.array.level),
b33395
 		       mddev);
b33395
 		if (st->ss == &super0)
b33395
 			put_md_name(mddev);
b33395
diff --git a/maps.c b/maps.c
b33395
index a4fd2797..20fcf719 100644
b33395
--- a/maps.c
b33395
+++ b/maps.c
b33395
@@ -166,6 +166,30 @@ mapping_t sysfs_array_states[] = {
b33395
 	{ NULL, ARRAY_UNKNOWN_STATE }
b33395
 };
b33395
 
b33395
+/**
b33395
+ * map_num_s() - Safer alternative of map_num() function.
b33395
+ * @map: map to search.
b33395
+ * @num: key to match.
b33395
+ *
b33395
+ * Shall be used only if key existence is quaranted.
b33395
+ *
b33395
+ * Return: Pointer to name of the element.
b33395
+ */
b33395
+char *map_num_s(mapping_t *map, int num)
b33395
+{
b33395
+	char *ret = map_num(map, num);
b33395
+
b33395
+	assert(ret);
b33395
+	return ret;
b33395
+}
b33395
+
b33395
+/**
b33395
+ * map_num() - get element name by key.
b33395
+ * @map: map to search.
b33395
+ * @num: key to match.
b33395
+ *
b33395
+ * Return: Pointer to name of the element or NULL.
b33395
+ */
b33395
 char *map_num(mapping_t *map, int num)
b33395
 {
b33395
 	while (map->name) {
b33395
diff --git a/mdadm.c b/mdadm.c
b33395
index 26299b2e..be40686c 100644
b33395
--- a/mdadm.c
b33395
+++ b/mdadm.c
b33395
@@ -280,8 +280,8 @@ int main(int argc, char *argv[])
b33395
 			else
b33395
 				fprintf(stderr, "-%c", opt);
b33395
 			fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
b33395
-				map_num(modes, newmode),
b33395
-				map_num(modes, mode));
b33395
+				map_num_s(modes, newmode),
b33395
+				map_num_s(modes, mode));
b33395
 			exit(2);
b33395
 		} else if (!mode && newmode) {
b33395
 			mode = newmode;
b33395
@@ -544,7 +544,7 @@ int main(int argc, char *argv[])
b33395
 			switch(s.level) {
b33395
 			default:
b33395
 				pr_err("layout not meaningful for %s arrays.\n",
b33395
-					map_num(pers, s.level));
b33395
+					map_num_s(pers, s.level));
b33395
 				exit(2);
b33395
 			case UnSet:
b33395
 				pr_err("raid level must be given before layout.\n");
b33395
@@ -1248,10 +1248,10 @@ int main(int argc, char *argv[])
b33395
 		if (option_index > 0)
b33395
 			pr_err(":option --%s not valid in %s mode\n",
b33395
 				long_options[option_index].name,
b33395
-				map_num(modes, mode));
b33395
+				map_num_s(modes, mode));
b33395
 		else
b33395
 			pr_err("option -%c not valid in %s mode\n",
b33395
-				opt, map_num(modes, mode));
b33395
+				opt, map_num_s(modes, mode));
b33395
 		exit(2);
b33395
 
b33395
 	}
b33395
@@ -1276,7 +1276,7 @@ int main(int argc, char *argv[])
b33395
 		if (s.consistency_policy != CONSISTENCY_POLICY_UNKNOWN &&
b33395
 		    s.consistency_policy != CONSISTENCY_POLICY_JOURNAL) {
b33395
 			pr_err("--write-journal is not supported with consistency policy: %s\n",
b33395
-			       map_num(consistency_policies, s.consistency_policy));
b33395
+			       map_num_s(consistency_policies, s.consistency_policy));
b33395
 			exit(2);
b33395
 		}
b33395
 	}
b33395
@@ -1285,12 +1285,12 @@ int main(int argc, char *argv[])
b33395
 	    s.consistency_policy != CONSISTENCY_POLICY_UNKNOWN) {
b33395
 		if (s.level <= 0) {
b33395
 			pr_err("--consistency-policy not meaningful with level %s.\n",
b33395
-			       map_num(pers, s.level));
b33395
+			       map_num_s(pers, s.level));
b33395
 			exit(2);
b33395
 		} else if (s.consistency_policy == CONSISTENCY_POLICY_JOURNAL &&
b33395
 			   !s.journaldisks) {
b33395
 			pr_err("--write-journal is required for consistency policy: %s\n",
b33395
-			       map_num(consistency_policies, s.consistency_policy));
b33395
+			       map_num_s(consistency_policies, s.consistency_policy));
b33395
 			exit(2);
b33395
 		} else if (s.consistency_policy == CONSISTENCY_POLICY_PPL &&
b33395
 			   s.level != 5) {
b33395
@@ -1300,14 +1300,14 @@ int main(int argc, char *argv[])
b33395
 			   (!s.bitmap_file ||
b33395
 			    strcmp(s.bitmap_file, "none") == 0)) {
b33395
 			pr_err("--bitmap is required for consistency policy: %s\n",
b33395
-			       map_num(consistency_policies, s.consistency_policy));
b33395
+			       map_num_s(consistency_policies, s.consistency_policy));
b33395
 			exit(2);
b33395
 		} else if (s.bitmap_file &&
b33395
 			   strcmp(s.bitmap_file, "none") != 0 &&
b33395
 			   s.consistency_policy != CONSISTENCY_POLICY_BITMAP &&
b33395
 			   s.consistency_policy != CONSISTENCY_POLICY_JOURNAL) {
b33395
 			pr_err("--bitmap is not compatible with consistency policy: %s\n",
b33395
-			       map_num(consistency_policies, s.consistency_policy));
b33395
+			       map_num_s(consistency_policies, s.consistency_policy));
b33395
 			exit(2);
b33395
 		}
b33395
 	}
b33395
diff --git a/mdadm.h b/mdadm.h
b33395
index cd72e711..09915a00 100644
b33395
--- a/mdadm.h
b33395
+++ b/mdadm.h
b33395
@@ -770,7 +770,7 @@ extern int restore_stripes(int *dest, unsigned long long *offsets,
b33395
 #endif
b33395
 
b33395
 #define SYSLOG_FACILITY LOG_DAEMON
b33395
-
b33395
+extern char *map_num_s(mapping_t *map, int num);
b33395
 extern char *map_num(mapping_t *map, int num);
b33395
 extern int map_name(mapping_t *map, char *name);
b33395
 extern mapping_t r0layout[], r5layout[], r6layout[],
b33395
diff --git a/super-ddf.c b/super-ddf.c
b33395
index 3f304cdc..8cda23a7 100644
b33395
--- a/super-ddf.c
b33395
+++ b/super-ddf.c
b33395
@@ -1477,13 +1477,13 @@ static void examine_vds(struct ddf_super *sb)
b33395
 		printf("\n");
b33395
 		printf("         unit[%d] : %d\n", i, be16_to_cpu(ve->unit));
b33395
 		printf("        state[%d] : %s, %s%s\n", i,
b33395
-		       map_num(ddf_state, ve->state & 7),
b33395
+		       map_num_s(ddf_state, ve->state & 7),
b33395
 		       (ve->state & DDF_state_morphing) ? "Morphing, ": "",
b33395
 		       (ve->state & DDF_state_inconsistent)? "Not Consistent" : "Consistent");
b33395
 		printf("   init state[%d] : %s\n", i,
b33395
-		       map_num(ddf_init_state, ve->init_state&DDF_initstate_mask));
b33395
+		       map_num_s(ddf_init_state, ve->init_state & DDF_initstate_mask));
b33395
 		printf("       access[%d] : %s\n", i,
b33395
-		       map_num(ddf_access, (ve->init_state & DDF_access_mask) >> 6));
b33395
+		       map_num_s(ddf_access, (ve->init_state & DDF_access_mask) >> 6));
b33395
 		printf("         Name[%d] : %.16s\n", i, ve->name);
b33395
 		examine_vd(i, sb, ve->guid);
b33395
 	}
b33395
diff --git a/super-intel.c b/super-intel.c
b33395
index 6ff336ee..ba3bd41f 100644
b33395
--- a/super-intel.c
b33395
+++ b/super-intel.c
b33395
@@ -5625,7 +5625,7 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
b33395
 		free(dev);
b33395
 		free(dv);
b33395
 		pr_err("imsm does not support consistency policy %s\n",
b33395
-		       map_num(consistency_policies, s->consistency_policy));
b33395
+		       map_num_s(consistency_policies, s->consistency_policy));
b33395
 		return 0;
b33395
 	}
b33395
 
b33395
diff --git a/super0.c b/super0.c
b33395
index b79b97a9..61c9ec1d 100644
b33395
--- a/super0.c
b33395
+++ b/super0.c
b33395
@@ -288,7 +288,7 @@ static void export_examine_super0(struct supertype *st)
b33395
 {
b33395
 	mdp_super_t *sb = st->sb;
b33395
 
b33395
-	printf("MD_LEVEL=%s\n", map_num(pers, sb->level));
b33395
+	printf("MD_LEVEL=%s\n", map_num_s(pers, sb->level));
b33395
 	printf("MD_DEVICES=%d\n", sb->raid_disks);
b33395
 	if (sb->minor_version >= 90)
b33395
 		printf("MD_UUID=%08x:%08x:%08x:%08x\n",
b33395
diff --git a/super1.c b/super1.c
b33395
index a12a5bc8..e3e2f954 100644
b33395
--- a/super1.c
b33395
+++ b/super1.c
b33395
@@ -671,7 +671,7 @@ static void export_examine_super1(struct supertype *st)
b33395
 	int len = 32;
b33395
 	int layout;
b33395
 
b33395
-	printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
b33395
+	printf("MD_LEVEL=%s\n", map_num_s(pers, __le32_to_cpu(sb->level)));
b33395
 	printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
b33395
 	for (i = 0; i < 32; i++)
b33395
 		if (sb->set_name[i] == '\n' || sb->set_name[i] == '\0') {
b33395
diff --git a/sysfs.c b/sysfs.c
b33395
index 2995713d..0d98a65f 100644
b33395
--- a/sysfs.c
b33395
+++ b/sysfs.c
b33395
@@ -689,7 +689,7 @@ int sysfs_set_array(struct mdinfo *info, int vers)
b33395
 	if (info->array.level < 0)
b33395
 		return 0; /* FIXME */
b33395
 	rv |= sysfs_set_str(info, NULL, "level",
b33395
-			    map_num(pers, info->array.level));
b33395
+			    map_num_s(pers, info->array.level));
b33395
 	if (info->reshape_active && info->delta_disks != UnSet)
b33395
 		raid_disks -= info->delta_disks;
b33395
 	rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks);
b33395
@@ -724,9 +724,10 @@ int sysfs_set_array(struct mdinfo *info, int vers)
b33395
 	}
b33395
 
b33395
 	if (info->consistency_policy == CONSISTENCY_POLICY_PPL) {
b33395
-		if (sysfs_set_str(info, NULL, "consistency_policy",
b33395
-				  map_num(consistency_policies,
b33395
-					  info->consistency_policy))) {
b33395
+		char *policy = map_num_s(consistency_policies,
b33395
+					    info->consistency_policy);
b33395
+
b33395
+		if (sysfs_set_str(info, NULL, "consistency_policy", policy)) {
b33395
 			pr_err("This kernel does not support PPL. Falling back to consistency-policy=resync.\n");
b33395
 			info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
b33395
 		}
b33395
-- 
2ad819
2.38.1
b33395