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

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