Blame SOURCES/0071-super0-refactor-the-code-for-enum.patch

01ff50
From 0a9e39383d3bf63e1f5cf10f64200083a1af8091 Mon Sep 17 00:00:00 2001
01ff50
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
01ff50
Date: Mon, 2 Jan 2023 09:35:19 +0100
01ff50
Subject: [PATCH 71/83] super0: refactor the code for enum
01ff50
01ff50
It prepares update_super0 for change context->update to enum.
01ff50
Change if else statements to switch.
01ff50
01ff50
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
01ff50
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
01ff50
---
01ff50
 super0.c | 102 ++++++++++++++++++++++++++++++++++---------------------
01ff50
 1 file changed, 63 insertions(+), 39 deletions(-)
01ff50
01ff50
diff --git a/super0.c b/super0.c
01ff50
index 93876e2e..d9f5bff4 100644
01ff50
--- a/super0.c
01ff50
+++ b/super0.c
01ff50
@@ -502,19 +502,39 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 	int rv = 0;
01ff50
 	int uuid[4];
01ff50
 	mdp_super_t *sb = st->sb;
01ff50
+	enum update_opt update_enum = map_name(update_options, update);
01ff50
 
01ff50
-	if (strcmp(update, "homehost") == 0 &&
01ff50
-	    homehost) {
01ff50
-		/* note that 'homehost' is special as it is really
01ff50
+	if (update_enum == UOPT_HOMEHOST && homehost) {
01ff50
+		/*
01ff50
+		 * note that 'homehost' is special as it is really
01ff50
 		 * a "uuid" update.
01ff50
 		 */
01ff50
 		uuid_set = 0;
01ff50
-		update = "uuid";
01ff50
+		update_enum = UOPT_UUID;
01ff50
 		info->uuid[0] = sb->set_uuid0;
01ff50
 		info->uuid[1] = sb->set_uuid1;
01ff50
 	}
01ff50
 
01ff50
-	if (strcmp(update, "sparc2.2")==0 ) {
01ff50
+	switch (update_enum) {
01ff50
+	case UOPT_UUID:
01ff50
+		if (!uuid_set && homehost) {
01ff50
+			char buf[20];
01ff50
+			memcpy(info->uuid+2,
01ff50
+			       sha1_buffer(homehost, strlen(homehost), buf),
01ff50
+			       8);
01ff50
+		}
01ff50
+		sb->set_uuid0 = info->uuid[0];
01ff50
+		sb->set_uuid1 = info->uuid[1];
01ff50
+		sb->set_uuid2 = info->uuid[2];
01ff50
+		sb->set_uuid3 = info->uuid[3];
01ff50
+		if (sb->state & (1<
01ff50
+			struct bitmap_super_s *bm;
01ff50
+			bm = (struct bitmap_super_s *)(sb+1);
01ff50
+			uuid_from_super0(st, uuid);
01ff50
+			memcpy(bm->uuid, uuid, 16);
01ff50
+		}
01ff50
+		break;
01ff50
+	case UOPT_SPARC22: {
01ff50
 		/* 2.2 sparc put the events in the wrong place
01ff50
 		 * So we copy the tail of the superblock
01ff50
 		 * up 4 bytes before continuing
01ff50
@@ -527,12 +547,15 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 		if (verbose >= 0)
01ff50
 			pr_err("adjusting superblock of %s for 2.2/sparc compatibility.\n",
01ff50
 			       devname);
01ff50
-	} else if (strcmp(update, "super-minor") ==0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_SUPER_MINOR:
01ff50
 		sb->md_minor = info->array.md_minor;
01ff50
 		if (verbose > 0)
01ff50
 			pr_err("updating superblock of %s with minor number %d\n",
01ff50
 				devname, info->array.md_minor);
01ff50
-	} else if (strcmp(update, "summaries") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_SUMMARIES: {
01ff50
 		unsigned int i;
01ff50
 		/* set nr_disks, active_disks, working_disks,
01ff50
 		 * failed_disks, spare_disks based on disks[]
01ff50
@@ -559,7 +582,9 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 					sb->spare_disks++;
01ff50
 			} else if (i >= sb->raid_disks && sb->disks[i].number == 0)
01ff50
 				sb->disks[i].state = 0;
01ff50
-	} else if (strcmp(update, "force-one")==0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_SPEC_FORCE_ONE: {
01ff50
 		/* Not enough devices for a working array, so
01ff50
 		 * bring this one up-to-date.
01ff50
 		 */
01ff50
@@ -569,7 +594,9 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 		if (sb->events_hi != ehi ||
01ff50
 		    sb->events_lo != elo)
01ff50
 			rv = 1;
01ff50
-	} else if (strcmp(update, "force-array")==0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_SPEC_FORCE_ARRAY:
01ff50
 		/* degraded array and 'force' requested, so
01ff50
 		 * maybe need to mark it 'clean'
01ff50
 		 */
01ff50
@@ -579,7 +606,8 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 			sb->state |= (1 << MD_SB_CLEAN);
01ff50
 			rv = 1;
01ff50
 		}
01ff50
-	} else if (strcmp(update, "assemble")==0) {
01ff50
+		break;
01ff50
+	case UOPT_SPEC_ASSEMBLE: {
01ff50
 		int d = info->disk.number;
01ff50
 		int wonly = sb->disks[d].state & (1<
01ff50
 		int failfast = sb->disks[d].state & (1<
01ff50
@@ -609,7 +637,9 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 			sb->reshape_position = info->reshape_progress;
01ff50
 			rv = 1;
01ff50
 		}
01ff50
-	} else if (strcmp(update, "linear-grow-new") == 0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_SPEC_LINEAR_GROW_NEW:
01ff50
 		memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
01ff50
 		sb->disks[info->disk.number].number = info->disk.number;
01ff50
 		sb->disks[info->disk.number].major = info->disk.major;
01ff50
@@ -617,7 +647,8 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 		sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
01ff50
 		sb->disks[info->disk.number].state = info->disk.state;
01ff50
 		sb->this_disk = sb->disks[info->disk.number];
01ff50
-	} else if (strcmp(update, "linear-grow-update") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_SPEC_LINEAR_GROW_UPDATE:
01ff50
 		sb->raid_disks = info->array.raid_disks;
01ff50
 		sb->nr_disks = info->array.nr_disks;
01ff50
 		sb->active_disks = info->array.active_disks;
01ff50
@@ -628,29 +659,15 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 		sb->disks[info->disk.number].minor = info->disk.minor;
01ff50
 		sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
01ff50
 		sb->disks[info->disk.number].state = info->disk.state;
01ff50
-	} else if (strcmp(update, "resync") == 0) {
01ff50
-		/* make sure resync happens */
01ff50
+		break;
01ff50
+	case UOPT_RESYNC:
01ff50
+		/*
01ff50
+		 * make sure resync happens
01ff50
+		 */
01ff50
 		sb->state &= ~(1<
01ff50
 		sb->recovery_cp = 0;
01ff50
-	} else if (strcmp(update, "uuid") == 0) {
01ff50
-		if (!uuid_set && homehost) {
01ff50
-			char buf[20];
01ff50
-			char *hash = sha1_buffer(homehost,
01ff50
-						 strlen(homehost),
01ff50
-						 buf);
01ff50
-			memcpy(info->uuid+2, hash, 8);
01ff50
-		}
01ff50
-		sb->set_uuid0 = info->uuid[0];
01ff50
-		sb->set_uuid1 = info->uuid[1];
01ff50
-		sb->set_uuid2 = info->uuid[2];
01ff50
-		sb->set_uuid3 = info->uuid[3];
01ff50
-		if (sb->state & (1<
01ff50
-			struct bitmap_super_s *bm;
01ff50
-			bm = (struct bitmap_super_s*)(sb+1);
01ff50
-			uuid_from_super0(st, uuid);
01ff50
-			memcpy(bm->uuid, uuid, 16);
01ff50
-		}
01ff50
-	} else if (strcmp(update, "metadata") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_METADATA:
01ff50
 		/* Create some v1.0 metadata to match ours but make the
01ff50
 		 * ctime bigger.  Also update info->array.*_version.
01ff50
 		 * We need to arrange that store_super writes out
01ff50
@@ -670,7 +687,8 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 			uuid_from_super0(st, info->uuid);
01ff50
 			st->other = super1_make_v0(st, info, st->sb);
01ff50
 		}
01ff50
-	} else if (strcmp(update, "revert-reshape") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_REVERT_RESHAPE:
01ff50
 		rv = -2;
01ff50
 		if (sb->minor_version <= 90)
01ff50
 			pr_err("No active reshape to revert on %s\n",
01ff50
@@ -702,16 +720,22 @@ static int update_super0(struct supertype *st, struct mdinfo *info,
01ff50
 			sb->new_chunk = sb->chunk_size;
01ff50
 			sb->chunk_size = tmp;
01ff50
 		}
01ff50
-	} else if (strcmp(update, "no-bitmap") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_NO_BITMAP:
01ff50
 		sb->state &= ~(1<
01ff50
-	} else if (strcmp(update, "_reshape_progress")==0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC__RESHAPE_PROGRESS:
01ff50
 		sb->reshape_position = info->reshape_progress;
01ff50
-	else if (strcmp(update, "writemostly")==0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC_WRITEMOSTLY:
01ff50
 		sb->state |= (1<
01ff50
-	else if (strcmp(update, "readwrite")==0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC_READWRITE:
01ff50
 		sb->state &= ~(1<
01ff50
-	else
01ff50
+		break;
01ff50
+	default:
01ff50
 		rv = -1;
01ff50
+	}
01ff50
 
01ff50
 	sb->sb_csum = calc_sb0_csum(sb);
01ff50
 	return rv;
01ff50
-- 
01ff50
2.38.1
01ff50