Blame SOURCES/0076-Change-char-to-enum-in-context-update-refactor-code.patch

37f2b0
From 3a87fa67112dc2c2c3664aeecd0b49cb4b6ceaa9 Mon Sep 17 00:00:00 2001
37f2b0
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
37f2b0
Date: Mon, 2 Jan 2023 09:35:24 +0100
37f2b0
Subject: [PATCH 76/83] Change char* to enum in context->update & refactor code
37f2b0
37f2b0
Storing update option in string is bad for frequent comparisons and
37f2b0
error prone.
37f2b0
Replace char array with enum so already existing enum is passed around
37f2b0
instead of string.
37f2b0
Adapt code to changes.
37f2b0
37f2b0
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
37f2b0
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
37f2b0
---
37f2b0
 Assemble.c | 40 +++++++++++++++++-----------------------
37f2b0
 mdadm.c    | 52 +++++++++++++++++++---------------------------------
37f2b0
 mdadm.h    |  2 +-
37f2b0
 3 files changed, 37 insertions(+), 57 deletions(-)
37f2b0
37f2b0
diff --git a/Assemble.c b/Assemble.c
37f2b0
index dba910cd..49804941 100644
37f2b0
--- a/Assemble.c
37f2b0
+++ b/Assemble.c
37f2b0
@@ -135,17 +135,17 @@ static int ident_matches(struct mddev_ident *ident,
37f2b0
 			 struct mdinfo *content,
37f2b0
 			 struct supertype *tst,
37f2b0
 			 char *homehost, int require_homehost,
37f2b0
-			 char *update, char *devname)
37f2b0
+			 enum update_opt update, char *devname)
37f2b0
 {
37f2b0
 
37f2b0
-	if (ident->uuid_set && (!update || strcmp(update, "uuid")!= 0) &&
37f2b0
+	if (ident->uuid_set && update != UOPT_UUID &&
37f2b0
 	    same_uuid(content->uuid, ident->uuid, tst->ss->swapuuid)==0 &&
37f2b0
 	    memcmp(content->uuid, uuid_zero, sizeof(int[4])) != 0) {
37f2b0
 		if (devname)
37f2b0
 			pr_err("%s has wrong uuid.\n", devname);
37f2b0
 		return 0;
37f2b0
 	}
37f2b0
-	if (ident->name[0] && (!update || strcmp(update, "name")!= 0) &&
37f2b0
+	if (ident->name[0] && update != UOPT_NAME &&
37f2b0
 	    name_matches(content->name, ident->name, homehost, require_homehost)==0) {
37f2b0
 		if (devname)
37f2b0
 			pr_err("%s has wrong name.\n", devname);
37f2b0
@@ -648,11 +648,10 @@ static int load_devices(struct devs *devices, char *devmap,
37f2b0
 			int err;
37f2b0
 			fstat(mdfd, &stb2);
37f2b0
 
37f2b0
-			if (strcmp(c->update, "uuid") == 0 && !ident->uuid_set)
37f2b0
+			if (c->update == UOPT_UUID && !ident->uuid_set)
37f2b0
 				random_uuid((__u8 *)ident->uuid);
37f2b0
 
37f2b0
-			if (strcmp(c->update, "ppl") == 0 &&
37f2b0
-			    ident->bitmap_fd >= 0) {
37f2b0
+			if (c->update == UOPT_PPL && ident->bitmap_fd >= 0) {
37f2b0
 				pr_err("PPL is not compatible with bitmap\n");
37f2b0
 				close(mdfd);
37f2b0
 				free(devices);
37f2b0
@@ -684,34 +683,30 @@ static int load_devices(struct devs *devices, char *devmap,
37f2b0
 			strcpy(content->name, ident->name);
37f2b0
 			content->array.md_minor = minor(stb2.st_rdev);
37f2b0
 
37f2b0
-			if (strcmp(c->update, "byteorder") == 0)
37f2b0
+			if (c->update == UOPT_BYTEORDER)
37f2b0
 				err = 0;
37f2b0
-			else if (strcmp(c->update, "home-cluster") == 0) {
37f2b0
+			else if (c->update == UOPT_HOME_CLUSTER) {
37f2b0
 				tst->cluster_name = c->homecluster;
37f2b0
 				err = tst->ss->write_bitmap(tst, dfd, NameUpdate);
37f2b0
-			} else if (strcmp(c->update, "nodes") == 0) {
37f2b0
+			} else if (c->update == UOPT_NODES) {
37f2b0
 				tst->nodes = c->nodes;
37f2b0
 				err = tst->ss->write_bitmap(tst, dfd, NodeNumUpdate);
37f2b0
-			} else if (strcmp(c->update, "revert-reshape") == 0 &&
37f2b0
-				   c->invalid_backup)
37f2b0
+			} else if (c->update == UOPT_REVERT_RESHAPE && c->invalid_backup)
37f2b0
 				err = tst->ss->update_super(tst, content,
37f2b0
 							    UOPT_SPEC_REVERT_RESHAPE_NOBACKUP,
37f2b0
 							    devname, c->verbose,
37f2b0
 							    ident->uuid_set,
37f2b0
 							    c->homehost);
37f2b0
 			else
37f2b0
-				/*
37f2b0
-				 * Mapping is temporary, will be removed in this patchset
37f2b0
-				 */
37f2b0
 				err = tst->ss->update_super(tst, content,
37f2b0
-							    map_name(update_options, c->update),
37f2b0
+							    c->update,
37f2b0
 							    devname, c->verbose,
37f2b0
 							    ident->uuid_set,
37f2b0
 							    c->homehost);
37f2b0
 			if (err < 0) {
37f2b0
 				if (err == -1)
37f2b0
 					pr_err("--update=%s not understood for %s metadata\n",
37f2b0
-					       c->update, tst->ss->name);
37f2b0
+					       map_num(update_options, c->update), tst->ss->name);
37f2b0
 				tst->ss->free_super(tst);
37f2b0
 				free(tst);
37f2b0
 				close(mdfd);
37f2b0
@@ -721,7 +716,7 @@ static int load_devices(struct devs *devices, char *devmap,
37f2b0
 				*stp = st;
37f2b0
 				return -1;
37f2b0
 			}
37f2b0
-			if (strcmp(c->update, "uuid")==0 &&
37f2b0
+			if (c->update == UOPT_UUID &&
37f2b0
 			    !ident->uuid_set) {
37f2b0
 				ident->uuid_set = 1;
37f2b0
 				memcpy(ident->uuid, content->uuid, 16);
37f2b0
@@ -730,7 +725,7 @@ static int load_devices(struct devs *devices, char *devmap,
37f2b0
 				pr_err("Could not re-write superblock on %s.\n",
37f2b0
 				       devname);
37f2b0
 
37f2b0
-			if (strcmp(c->update, "uuid")==0 &&
37f2b0
+			if (c->update == UOPT_UUID &&
37f2b0
 			    ident->bitmap_fd >= 0 && !bitmap_done) {
37f2b0
 				if (bitmap_update_uuid(ident->bitmap_fd,
37f2b0
 						       content->uuid,
37f2b0
@@ -1188,8 +1183,7 @@ static int start_array(int mdfd,
37f2b0
 				pr_err("%s: Need a backup file to complete reshape of this array.\n",
37f2b0
 				       mddev);
37f2b0
 				pr_err("Please provided one with \"--backup-file=...\"\n");
37f2b0
-				if (c->update &&
37f2b0
-				    strcmp(c->update, "revert-reshape") == 0)
37f2b0
+				if (c->update == UOPT_REVERT_RESHAPE)
37f2b0
 					pr_err("(Don't specify --update=revert-reshape again, that part succeeded.)\n");
37f2b0
 				return 1;
37f2b0
 			}
37f2b0
@@ -1487,7 +1481,7 @@ try_again:
37f2b0
 	 */
37f2b0
 	if (map_lock(&map))
37f2b0
 		pr_err("failed to get exclusive lock on mapfile - continue anyway...\n");
37f2b0
-	if (c->update && strcmp(c->update,"uuid") == 0)
37f2b0
+	if (c->update == UOPT_UUID)
37f2b0
 		mp = NULL;
37f2b0
 	else
37f2b0
 		mp = map_by_uuid(&map, content->uuid);
37f2b0
@@ -1634,7 +1628,7 @@ try_again:
37f2b0
 		goto out;
37f2b0
 	}
37f2b0
 
37f2b0
-	if (c->update && strcmp(c->update, "byteorder")==0)
37f2b0
+	if (c->update == UOPT_BYTEORDER)
37f2b0
 		st->minor_version = 90;
37f2b0
 
37f2b0
 	st->ss->getinfo_super(st, content, NULL);
37f2b0
@@ -1902,7 +1896,7 @@ try_again:
37f2b0
 	/* First, fill in the map, so that udev can find our name
37f2b0
 	 * as soon as we become active.
37f2b0
 	 */
37f2b0
-	if (c->update && strcmp(c->update, "metadata")==0) {
37f2b0
+	if (c->update == UOPT_METADATA) {
37f2b0
 		content->array.major_version = 1;
37f2b0
 		content->array.minor_version = 0;
37f2b0
 		strcpy(content->text_version, "1.0");
37f2b0
diff --git a/mdadm.c b/mdadm.c
37f2b0
index d06e2820..57e8e6fa 100644
37f2b0
--- a/mdadm.c
37f2b0
+++ b/mdadm.c
37f2b0
@@ -724,13 +724,12 @@ int main(int argc, char *argv[])
37f2b0
 
37f2b0
 		case O(ASSEMBLE,'U'): /* update the superblock */
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
+					map_num(update_options, c.update), optarg);
37f2b0
 				exit(2);
37f2b0
 			}
37f2b0
 			if (mode == MISC && !c.subarray) {
37f2b0
@@ -738,20 +737,20 @@ int main(int argc, char *argv[])
37f2b0
 				exit(2);
37f2b0
 			}
37f2b0
 
37f2b0
-			c.update = optarg;
37f2b0
+			c.update = map_name(update_options, optarg);
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
+				if (c.update > UOPT_SUBARRAY_ONLY && c.update < UOPT_HELP)
37f2b0
+					c.update = UOPT_UNDEFINED;
37f2b0
 			}
37f2b0
 
37f2b0
-			switch (updateopt) {
37f2b0
+			switch (c.update) {
37f2b0
 			case UOPT_UNDEFINED:
37f2b0
 				pr_err("'--update=%s' is invalid %s. ",
37f2b0
-					c.update, error_addon);
37f2b0
+					optarg, error_addon);
37f2b0
 				outf = stderr;
37f2b0
 			case UOPT_HELP:
37f2b0
 				if (!outf)
37f2b0
@@ -776,14 +775,14 @@ int main(int argc, char *argv[])
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
+					map_num(update_options, c.update), optarg);
37f2b0
 				exit(2);
37f2b0
 			}
37f2b0
-			c.update = optarg;
37f2b0
-			if (strcmp(c.update, "devicesize") != 0 &&
37f2b0
-			    strcmp(c.update, "bbl") != 0 &&
37f2b0
-			    strcmp(c.update, "force-no-bbl") != 0 &&
37f2b0
-			    strcmp(c.update, "no-bbl") != 0) {
37f2b0
+			c.update = map_name(update_options, optarg);
37f2b0
+			if (c.update != UOPT_DEVICESIZE &&
37f2b0
+			    c.update != UOPT_BBL &&
37f2b0
+			    c.update != UOPT_NO_BBL &&
37f2b0
+			    c.update != UOPT_FORCE_NO_BBL) {
37f2b0
 				pr_err("only 'devicesize', 'bbl', 'no-bbl', and 'force-no-bbl' can be updated with --re-add\n");
37f2b0
 				exit(2);
37f2b0
 			}
37f2b0
@@ -1357,7 +1356,7 @@ int main(int argc, char *argv[])
37f2b0
 		}
37f2b0
 	}
37f2b0
 
37f2b0
-	if (c.update && strcmp(c.update, "nodes") == 0 && c.nodes == 0) {
37f2b0
+	if (c.update && c.update == UOPT_NODES && c.nodes == 0) {
37f2b0
 		pr_err("Please specify nodes number with --nodes\n");
37f2b0
 		exit(1);
37f2b0
 	}
37f2b0
@@ -1402,22 +1401,10 @@ int main(int argc, char *argv[])
37f2b0
 		/* readonly, add/remove, readwrite, runstop */
37f2b0
 		if (c.readonly > 0)
37f2b0
 			rv = Manage_ro(devlist->devname, mdfd, c.readonly);
37f2b0
-		if (!rv && devs_found > 1) {
37f2b0
-			/*
37f2b0
-			 * This is temporary and will be removed in next patches
37f2b0
-			 * Null c.update will cause segfault
37f2b0
-			 */
37f2b0
-			if (c.update)
37f2b0
-				rv = Manage_subdevs(devlist->devname, mdfd,
37f2b0
-						devlist->next, c.verbose, c.test,
37f2b0
-						map_name(update_options, c.update),
37f2b0
-						c.force);
37f2b0
-			else
37f2b0
-				rv = Manage_subdevs(devlist->devname, mdfd,
37f2b0
-						devlist->next, c.verbose, c.test,
37f2b0
-						UOPT_UNDEFINED,
37f2b0
-						c.force);
37f2b0
-		}
37f2b0
+		if (!rv && devs_found > 1)
37f2b0
+			rv = Manage_subdevs(devlist->devname, mdfd,
37f2b0
+					    devlist->next, c.verbose,
37f2b0
+					    c.test, c.update, c.force);
37f2b0
 		if (!rv && c.readonly < 0)
37f2b0
 			rv = Manage_ro(devlist->devname, mdfd, c.readonly);
37f2b0
 		if (!rv && c.runstop > 0)
37f2b0
@@ -1937,14 +1924,13 @@ static int misc_list(struct mddev_dev *devlist,
37f2b0
 			rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
37f2b0
 			continue;
37f2b0
 		case UpdateSubarray:
37f2b0
-			if (c->update == NULL) {
37f2b0
+			if (!c->update) {
37f2b0
 				pr_err("-U/--update must be specified with --update-subarray\n");
37f2b0
 				rv |= 1;
37f2b0
 				continue;
37f2b0
 			}
37f2b0
 			rv |= Update_subarray(dv->devname, c->subarray,
37f2b0
-					      map_name(update_options, c->update),
37f2b0
-					      ident, c->verbose);
37f2b0
+					      c->update, ident, c->verbose);
37f2b0
 			continue;
37f2b0
 		case Dump:
37f2b0
 			rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
37f2b0
diff --git a/mdadm.h b/mdadm.h
37f2b0
index 924f4b63..13f8b4cb 100644
37f2b0
--- a/mdadm.h
37f2b0
+++ b/mdadm.h
37f2b0
@@ -616,7 +616,7 @@ struct context {
37f2b0
 	int	export;
37f2b0
 	int	test;
37f2b0
 	char	*subarray;
37f2b0
-	char	*update;
37f2b0
+	enum	update_opt update;
37f2b0
 	int	scan;
37f2b0
 	int	SparcAdjust;
37f2b0
 	int	autof;
37f2b0
-- 
37f2b0
2.38.1
37f2b0