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

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