Blame SOURCES/0067-mdadm-Add-option-validation-for-update-subarray.patch

37f2b0
From 2568ce89ea5c26225e8984733adc2ea7559d853a Mon Sep 17 00:00:00 2001
37f2b0
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
37f2b0
Date: Mon, 2 Jan 2023 09:35:15 +0100
37f2b0
Subject: [PATCH 67/83] mdadm: Add option validation for --update-subarray
37f2b0
37f2b0
Subset of options available for "--update" is not same as for "--update-subarray".
37f2b0
Define maps and enum for update options and use them instead of direct comparisons.
37f2b0
Add proper error message.
37f2b0
37f2b0
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
37f2b0
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
37f2b0
---
37f2b0
 ReadMe.c |  31 +++++++++++++++++
37f2b0
 maps.c   |  31 +++++++++++++++++
37f2b0
 mdadm.c  | 104 +++++++++++++++++--------------------------------------
37f2b0
 mdadm.h  |  32 ++++++++++++++++-
37f2b0
 4 files changed, 124 insertions(+), 74 deletions(-)
37f2b0
37f2b0
diff --git a/ReadMe.c b/ReadMe.c
37f2b0
index 50a5e36d..bd8d50d2 100644
37f2b0
--- a/ReadMe.c
37f2b0
+++ b/ReadMe.c
37f2b0
@@ -655,3 +655,34 @@ char *mode_help[mode_count] = {
37f2b0
 	[GROW]		= Help_grow,
37f2b0
 	[INCREMENTAL]	= Help_incr,
37f2b0
 };
37f2b0
+
37f2b0
+/**
37f2b0
+ * fprint_update_options() - Print valid update options depending on the mode.
37f2b0
+ * @outf: File (output stream)
37f2b0
+ * @update_mode: Used to distinguish update and update_subarray
37f2b0
+ */
37f2b0
+void fprint_update_options(FILE *outf, enum update_opt update_mode)
37f2b0
+{
37f2b0
+	int counter = UOPT_NAME, breakpoint = UOPT_HELP;
37f2b0
+	mapping_t *map = update_options;
37f2b0
+
37f2b0
+	if (!outf)
37f2b0
+		return;
37f2b0
+	if (update_mode == UOPT_SUBARRAY_ONLY) {
37f2b0
+		breakpoint = UOPT_SUBARRAY_ONLY;
37f2b0
+		fprintf(outf, "Valid --update options for update-subarray are:\n\t");
37f2b0
+	} else
37f2b0
+		fprintf(outf, "Valid --update options are:\n\t");
37f2b0
+	while (map->num) {
37f2b0
+		if (map->num >= breakpoint)
37f2b0
+			break;
37f2b0
+		fprintf(outf, "'%s', ", map->name);
37f2b0
+		if (counter % 5 == 0)
37f2b0
+			fprintf(outf, "\n\t");
37f2b0
+		counter++;
37f2b0
+		map++;
37f2b0
+	}
37f2b0
+	if ((counter - 1) % 5)
37f2b0
+		fprintf(outf, "\n");
37f2b0
+	fprintf(outf, "\r");
37f2b0
+}
37f2b0
diff --git a/maps.c b/maps.c
37f2b0
index 20fcf719..b586679a 100644
37f2b0
--- a/maps.c
37f2b0
+++ b/maps.c
37f2b0
@@ -165,6 +165,37 @@ mapping_t sysfs_array_states[] = {
37f2b0
 	{ "broken", ARRAY_BROKEN },
37f2b0
 	{ NULL, ARRAY_UNKNOWN_STATE }
37f2b0
 };
37f2b0
+/**
37f2b0
+ * mapping_t update_options - stores supported update options.
37f2b0
+ */
37f2b0
+mapping_t update_options[] = {
37f2b0
+	{ "name", UOPT_NAME },
37f2b0
+	{ "ppl", UOPT_PPL },
37f2b0
+	{ "no-ppl", UOPT_NO_PPL },
37f2b0
+	{ "bitmap", UOPT_BITMAP },
37f2b0
+	{ "no-bitmap", UOPT_NO_BITMAP },
37f2b0
+	{ "sparc2.2", UOPT_SPARC22 },
37f2b0
+	{ "super-minor", UOPT_SUPER_MINOR },
37f2b0
+	{ "summaries", UOPT_SUMMARIES },
37f2b0
+	{ "resync", UOPT_RESYNC },
37f2b0
+	{ "uuid", UOPT_UUID },
37f2b0
+	{ "homehost", UOPT_HOMEHOST },
37f2b0
+	{ "home-cluster", UOPT_HOME_CLUSTER },
37f2b0
+	{ "nodes", UOPT_NODES },
37f2b0
+	{ "devicesize", UOPT_DEVICESIZE },
37f2b0
+	{ "bbl", UOPT_BBL },
37f2b0
+	{ "no-bbl", UOPT_NO_BBL },
37f2b0
+	{ "force-no-bbl", UOPT_FORCE_NO_BBL },
37f2b0
+	{ "metadata", UOPT_METADATA },
37f2b0
+	{ "revert-reshape", UOPT_REVERT_RESHAPE },
37f2b0
+	{ "layout-original", UOPT_LAYOUT_ORIGINAL },
37f2b0
+	{ "layout-alternate", UOPT_LAYOUT_ALTERNATE },
37f2b0
+	{ "layout-unspecified", UOPT_LAYOUT_UNSPECIFIED },
37f2b0
+	{ "byteorder", UOPT_BYTEORDER },
37f2b0
+	{ "help", UOPT_HELP },
37f2b0
+	{ "?", UOPT_HELP },
37f2b0
+	{ NULL, UOPT_UNDEFINED}
37f2b0
+};
37f2b0
 
37f2b0
 /**
37f2b0
  * map_num_s() - Safer alternative of map_num() function.
37f2b0
diff --git a/mdadm.c b/mdadm.c
37f2b0
index 74fdec31..f5f505fe 100644
37f2b0
--- a/mdadm.c
37f2b0
+++ b/mdadm.c
37f2b0
@@ -100,7 +100,7 @@ int main(int argc, char *argv[])
37f2b0
 	char *dump_directory = NULL;
37f2b0
 
37f2b0
 	int print_help = 0;
37f2b0
-	FILE *outf;
37f2b0
+	FILE *outf = NULL;
37f2b0
 
37f2b0
 	int mdfd = -1;
37f2b0
 	int locked = 0;
37f2b0
@@ -723,7 +723,11 @@ int main(int argc, char *argv[])
37f2b0
 			continue;
37f2b0
 
37f2b0
 		case O(ASSEMBLE,'U'): /* update the superblock */
37f2b0
-		case O(MISC,'U'):
37f2b0
+		case O(MISC,'U'): {
37f2b0
+			enum update_opt updateopt = map_name(update_options, c.update);
37f2b0
+			enum update_opt print_mode = UOPT_HELP;
37f2b0
+			const char *error_addon = "update option";
37f2b0
+
37f2b0
 			if (c.update) {
37f2b0
 				pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
37f2b0
 					c.update, optarg);
37f2b0
@@ -733,83 +737,37 @@ int main(int argc, char *argv[])
37f2b0
 				pr_err("Only subarrays can be updated in misc mode\n");
37f2b0
 				exit(2);
37f2b0
 			}
37f2b0
+
37f2b0
 			c.update = optarg;
37f2b0
-			if (strcmp(c.update, "sparc2.2") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "super-minor") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "summaries") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "resync") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "uuid") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "name") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "homehost") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "home-cluster") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "nodes") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "devicesize") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "bitmap") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "no-bitmap") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "bbl") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "no-bbl") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "force-no-bbl") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "ppl") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "no-ppl") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "metadata") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "revert-reshape") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "layout-original") == 0 ||
37f2b0
-			    strcmp(c.update, "layout-alternate") == 0 ||
37f2b0
-			    strcmp(c.update, "layout-unspecified") == 0)
37f2b0
-				continue;
37f2b0
-			if (strcmp(c.update, "byteorder") == 0) {
37f2b0
+
37f2b0
+			if (devmode == UpdateSubarray) {
37f2b0
+				print_mode = UOPT_SUBARRAY_ONLY;
37f2b0
+				error_addon = "update-subarray option";
37f2b0
+
37f2b0
+				if (updateopt > UOPT_SUBARRAY_ONLY && updateopt < UOPT_HELP)
37f2b0
+					updateopt = UOPT_UNDEFINED;
37f2b0
+			}
37f2b0
+
37f2b0
+			switch (updateopt) {
37f2b0
+			case UOPT_UNDEFINED:
37f2b0
+				pr_err("'--update=%s' is invalid %s. ",
37f2b0
+					c.update, error_addon);
37f2b0
+				outf = stderr;
37f2b0
+			case UOPT_HELP:
37f2b0
+				if (!outf)
37f2b0
+					outf = stdout;
37f2b0
+				fprint_update_options(outf, print_mode);
37f2b0
+				exit(outf == stdout ? 0 : 2);
37f2b0
+			case UOPT_BYTEORDER:
37f2b0
 				if (ss) {
37f2b0
 					pr_err("must not set metadata type with --update=byteorder.\n");
37f2b0
 					exit(2);
37f2b0
 				}
37f2b0
-				for(i = 0; !ss && superlist[i]; i++)
37f2b0
-					ss = superlist[i]->match_metadata_desc(
37f2b0
-						"0.swap");
37f2b0
-				if (!ss) {
37f2b0
-					pr_err("INTERNAL ERROR cannot find 0.swap\n");
37f2b0
-					exit(2);
37f2b0
-				}
37f2b0
-
37f2b0
-				continue;
37f2b0
+			default:
37f2b0
+				break;
37f2b0
 			}
37f2b0
-			if (strcmp(c.update,"?") == 0 ||
37f2b0
-			    strcmp(c.update, "help") == 0) {
37f2b0
-				outf = stdout;
37f2b0
-				fprintf(outf, "%s: ", Name);
37f2b0
-			} else {
37f2b0
-				outf = stderr;
37f2b0
-				fprintf(outf,
37f2b0
-					"%s: '--update=%s' is invalid.  ",
37f2b0
-					Name, c.update);
37f2b0
-			}
37f2b0
-			fprintf(outf, "Valid --update options are:\n"
37f2b0
-		"     'sparc2.2', 'super-minor', 'uuid', 'name', 'nodes', 'resync',\n"
37f2b0
-		"     'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
37f2b0
-		"     'bitmap', 'no-bitmap', 'metadata', 'revert-reshape'\n"
37f2b0
-		"     'bbl', 'no-bbl', 'force-no-bbl', 'ppl', 'no-ppl'\n"
37f2b0
-		"     'layout-original', 'layout-alternate', 'layout-unspecified'\n"
37f2b0
-				);
37f2b0
-			exit(outf == stdout ? 0 : 2);
37f2b0
-
37f2b0
+			continue;
37f2b0
+		}
37f2b0
 		case O(MANAGE,'U'):
37f2b0
 			/* update=devicesize is allowed with --re-add */
37f2b0
 			if (devmode != 'A') {
37f2b0
diff --git a/mdadm.h b/mdadm.h
37f2b0
index 23ffe977..51f1db2d 100644
37f2b0
--- a/mdadm.h
37f2b0
+++ b/mdadm.h
37f2b0
@@ -497,6 +497,36 @@ enum special_options {
37f2b0
 	ConsistencyPolicy,
37f2b0
 };
37f2b0
 
37f2b0
+enum update_opt {
37f2b0
+	UOPT_NAME = 1,
37f2b0
+	UOPT_PPL,
37f2b0
+	UOPT_NO_PPL,
37f2b0
+	UOPT_BITMAP,
37f2b0
+	UOPT_NO_BITMAP,
37f2b0
+	UOPT_SUBARRAY_ONLY,
37f2b0
+	UOPT_SPARC22,
37f2b0
+	UOPT_SUPER_MINOR,
37f2b0
+	UOPT_SUMMARIES,
37f2b0
+	UOPT_RESYNC,
37f2b0
+	UOPT_UUID,
37f2b0
+	UOPT_HOMEHOST,
37f2b0
+	UOPT_HOME_CLUSTER,
37f2b0
+	UOPT_NODES,
37f2b0
+	UOPT_DEVICESIZE,
37f2b0
+	UOPT_BBL,
37f2b0
+	UOPT_NO_BBL,
37f2b0
+	UOPT_FORCE_NO_BBL,
37f2b0
+	UOPT_METADATA,
37f2b0
+	UOPT_REVERT_RESHAPE,
37f2b0
+	UOPT_LAYOUT_ORIGINAL,
37f2b0
+	UOPT_LAYOUT_ALTERNATE,
37f2b0
+	UOPT_LAYOUT_UNSPECIFIED,
37f2b0
+	UOPT_BYTEORDER,
37f2b0
+	UOPT_HELP,
37f2b0
+	UOPT_UNDEFINED
37f2b0
+};
37f2b0
+extern void fprint_update_options(FILE *outf, enum update_opt update_mode);
37f2b0
+
37f2b0
 enum prefix_standard {
37f2b0
 	JEDEC,
37f2b0
 	IEC
37f2b0
@@ -777,7 +807,7 @@ extern char *map_num(mapping_t *map, int num);
37f2b0
 extern int map_name(mapping_t *map, char *name);
37f2b0
 extern mapping_t r0layout[], r5layout[], r6layout[],
37f2b0
 	pers[], modes[], faultylayout[];
37f2b0
-extern mapping_t consistency_policies[], sysfs_array_states[];
37f2b0
+extern mapping_t consistency_policies[], sysfs_array_states[], update_options[];
37f2b0
 
37f2b0
 extern char *map_dev_preferred(int major, int minor, int create,
37f2b0
 			       char *prefer);
37f2b0
-- 
37f2b0
2.38.1
37f2b0