Blame SOURCES/0072-super1-refactor-the-code-for-enum.patch

01ff50
From 7e8daba8b7937716dce8ea28298a4e2e72cb829e Mon Sep 17 00:00:00 2001
01ff50
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
01ff50
Date: Mon, 2 Jan 2023 09:35:20 +0100
01ff50
Subject: [PATCH 72/83] super1: refactor the code for enum
01ff50
01ff50
It prepares update_super1 for change context->update to enum.
01ff50
Change if else statements into switch.
01ff50
01ff50
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
01ff50
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
01ff50
---
01ff50
 super1.c | 152 +++++++++++++++++++++++++++++++++----------------------
01ff50
 1 file changed, 91 insertions(+), 61 deletions(-)
01ff50
01ff50
diff --git a/super1.c b/super1.c
01ff50
index 0b505a7e..b0a97016 100644
01ff50
--- a/super1.c
01ff50
+++ b/super1.c
01ff50
@@ -1218,30 +1218,55 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 	int rv = 0;
01ff50
 	struct mdp_superblock_1 *sb = st->sb;
01ff50
 	bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
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 "name" update.
01ff50
 		 */
01ff50
 		char *c;
01ff50
-		update = "name";
01ff50
+		update_enum = UOPT_NAME;
01ff50
 		c = strchr(sb->set_name, ':');
01ff50
 		if (c)
01ff50
-			strncpy(info->name, c+1, 31 - (c-sb->set_name));
01ff50
+			snprintf(info->name, sizeof(info->name), "%s", c+1);
01ff50
 		else
01ff50
-			strncpy(info->name, sb->set_name, 32);
01ff50
-		info->name[32] = 0;
01ff50
+			snprintf(info->name, sizeof(info->name), "%s", sb->set_name);
01ff50
 	}
01ff50
 
01ff50
-	if (strcmp(update, "force-one")==0) {
01ff50
+	switch (update_enum) {
01ff50
+	case UOPT_NAME: {
01ff50
+		int namelen;
01ff50
+
01ff50
+		if (!info->name[0])
01ff50
+			snprintf(info->name, sizeof(info->name), "%d", info->array.md_minor);
01ff50
+		memset(sb->set_name, 0, sizeof(sb->set_name));
01ff50
+
01ff50
+		namelen = strnlen(homehost, MD_NAME_MAX) + 1 + strnlen(info->name, MD_NAME_MAX);
01ff50
+		if (homehost &&
01ff50
+		    strchr(info->name, ':') == NULL &&
01ff50
+		    namelen < MD_NAME_MAX) {
01ff50
+			strcpy(sb->set_name, homehost);
01ff50
+			strcat(sb->set_name, ":");
01ff50
+			strcat(sb->set_name, info->name);
01ff50
+		} else {
01ff50
+			namelen = min((int)strnlen(info->name, MD_NAME_MAX),
01ff50
+				      (int)sizeof(sb->set_name) - 1);
01ff50
+			memcpy(sb->set_name, info->name, namelen);
01ff50
+			memset(&sb->set_name[namelen], '\0',
01ff50
+			       sizeof(sb->set_name) - namelen);
01ff50
+		}
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_SPEC_FORCE_ONE:
01ff50
 		/* Not enough devices for a working array,
01ff50
 		 * so bring this one up-to-date
01ff50
 		 */
01ff50
 		if (sb->events != __cpu_to_le64(info->events))
01ff50
 			rv = 1;
01ff50
 		sb->events = __cpu_to_le64(info->events);
01ff50
-	} else if (strcmp(update, "force-array")==0) {
01ff50
+		break;
01ff50
+	case UOPT_SPEC_FORCE_ARRAY:
01ff50
 		/* Degraded array and 'force' requests to
01ff50
 		 * maybe need to mark it 'clean'.
01ff50
 		 */
01ff50
@@ -1254,7 +1279,8 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 				rv = 1;
01ff50
 			sb->resync_offset = MaxSector;
01ff50
 		}
01ff50
-	} else if (strcmp(update, "assemble")==0) {
01ff50
+		break;
01ff50
+	case UOPT_SPEC_ASSEMBLE: {
01ff50
 		int d = info->disk.number;
01ff50
 		int want;
01ff50
 		if (info->disk.state & (1<
01ff50
@@ -1287,7 +1313,9 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 				__cpu_to_le64(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
 		int i;
01ff50
 		int fd;
01ff50
 		int max = __le32_to_cpu(sb->max_dev);
01ff50
@@ -1330,7 +1358,9 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 					ds - __le64_to_cpu(sb->data_offset));
01ff50
 			}
01ff50
 		}
01ff50
-	} else if (strcmp(update, "linear-grow-update") == 0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_SPEC_LINEAR_GROW_UPDATE: {
01ff50
 		int max = __le32_to_cpu(sb->max_dev);
01ff50
 		int i = info->disk.number;
01ff50
 		if (max > MAX_DEVS || i > MAX_DEVS)
01ff50
@@ -1342,19 +1372,20 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 		sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
01ff50
 		sb->dev_roles[info->disk.number] =
01ff50
 			__cpu_to_le16(info->disk.raid_disk);
01ff50
-	} else if (strcmp(update, "resync") == 0) {
01ff50
-		/* make sure resync happens */
01ff50
-		sb->resync_offset = 0ULL;
01ff50
-	} else if (strcmp(update, "uuid") == 0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_UUID:
01ff50
 		copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
01ff50
 
01ff50
 		if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)
01ff50
 			memcpy(bms->uuid, sb->set_uuid, 16);
01ff50
-	} else if (strcmp(update, "no-bitmap") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_NO_BITMAP:
01ff50
 		sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
01ff50
 		if (bms->version == BITMAP_MAJOR_CLUSTERED && !IsBitmapDirty(devname))
01ff50
 			sb->resync_offset = MaxSector;
01ff50
-	} else if (strcmp(update, "bbl") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_BBL: {
01ff50
 		/* only possible if there is room after the bitmap, or if
01ff50
 		 * there is no bitmap
01ff50
 		 */
01ff50
@@ -1383,14 +1414,12 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 				bb_offset = bitmap_offset + bm_sectors;
01ff50
 			while (bb_offset < (long)sb_offset + 8 + 32*2 &&
01ff50
 			       bb_offset + 8+8 <= (long)data_offset)
01ff50
-				/* too close to bitmap, and room to grow */
01ff50
 				bb_offset += 8;
01ff50
 			if (bb_offset + 8 <= (long)data_offset) {
01ff50
 				sb->bblog_size = __cpu_to_le16(8);
01ff50
 				sb->bblog_offset = __cpu_to_le32(bb_offset);
01ff50
 			}
01ff50
 		} else {
01ff50
-			/* 1.0 - Put bbl just before super block */
01ff50
 			if (bm_sectors && bitmap_offset < 0)
01ff50
 				space = -bitmap_offset - bm_sectors;
01ff50
 			else
01ff50
@@ -1401,7 +1430,9 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 				sb->bblog_offset = __cpu_to_le32((unsigned)-8);
01ff50
 			}
01ff50
 		}
01ff50
-	} else if (strcmp(update, "no-bbl") == 0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_NO_BBL:
01ff50
 		if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
01ff50
 			pr_err("Cannot remove active bbl from %s\n",devname);
01ff50
 		else {
01ff50
@@ -1409,12 +1440,14 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 			sb->bblog_shift = 0;
01ff50
 			sb->bblog_offset = 0;
01ff50
 		}
01ff50
-	} else if (strcmp(update, "force-no-bbl") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_FORCE_NO_BBL:
01ff50
 		sb->feature_map &= ~ __cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
01ff50
 		sb->bblog_size = 0;
01ff50
 		sb->bblog_shift = 0;
01ff50
 		sb->bblog_offset = 0;
01ff50
-	} else if (strcmp(update, "ppl") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_PPL: {
01ff50
 		unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
01ff50
 		unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
01ff50
 		unsigned long long data_size = __le64_to_cpu(sb->data_size);
01ff50
@@ -1464,37 +1497,26 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 		sb->ppl.offset = __cpu_to_le16(offset);
01ff50
 		sb->ppl.size = __cpu_to_le16(space);
01ff50
 		sb->feature_map |= __cpu_to_le32(MD_FEATURE_PPL);
01ff50
-	} else if (strcmp(update, "no-ppl") == 0) {
01ff50
+		break;
01ff50
+	}
01ff50
+	case UOPT_NO_PPL:
01ff50
 		sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_PPL |
01ff50
 						   MD_FEATURE_MUTLIPLE_PPLS);
01ff50
-	} else if (strcmp(update, "name") == 0) {
01ff50
-		if (info->name[0] == 0)
01ff50
-			sprintf(info->name, "%d", info->array.md_minor);
01ff50
-		memset(sb->set_name, 0, sizeof(sb->set_name));
01ff50
-		if (homehost &&
01ff50
-		    strchr(info->name, ':') == NULL &&
01ff50
-		    strlen(homehost)+1+strlen(info->name) < 32) {
01ff50
-			strcpy(sb->set_name, homehost);
01ff50
-			strcat(sb->set_name, ":");
01ff50
-			strcat(sb->set_name, info->name);
01ff50
-		} else {
01ff50
-			int namelen;
01ff50
-
01ff50
-			namelen = min((int)strlen(info->name),
01ff50
-				      (int)sizeof(sb->set_name) - 1);
01ff50
-			memcpy(sb->set_name, info->name, namelen);
01ff50
-			memset(&sb->set_name[namelen], '\0',
01ff50
-			       sizeof(sb->set_name) - namelen);
01ff50
-		}
01ff50
-	} else if (strcmp(update, "devicesize") == 0 &&
01ff50
-		   __le64_to_cpu(sb->super_offset) <
01ff50
-		   __le64_to_cpu(sb->data_offset)) {
01ff50
-		/* set data_size to device size less data_offset */
01ff50
+		break;
01ff50
+	case UOPT_DEVICESIZE:
01ff50
+		if (__le64_to_cpu(sb->super_offset) >=
01ff50
+		    __le64_to_cpu(sb->data_offset))
01ff50
+			break;
01ff50
+		/*
01ff50
+		 * set data_size to device size less data_offset
01ff50
+		 */
01ff50
 		struct misc_dev_info *misc = (struct misc_dev_info*)
01ff50
 			(st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
01ff50
 		sb->data_size = __cpu_to_le64(
01ff50
 			misc->device_size - __le64_to_cpu(sb->data_offset));
01ff50
-	} else if (strncmp(update, "revert-reshape", 14) == 0) {
01ff50
+		break;
01ff50
+	case UOPT_SPEC_REVERT_RESHAPE_NOBACKUP:
01ff50
+	case UOPT_REVERT_RESHAPE:
01ff50
 		rv = -2;
01ff50
 		if (!(sb->feature_map &
01ff50
 		      __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)))
01ff50
@@ -1512,7 +1534,7 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 			 * If that couldn't happen, the "-nobackup" version
01ff50
 			 * will be used.
01ff50
 			 */
01ff50
-			if (strcmp(update, "revert-reshape-nobackup") == 0 &&
01ff50
+			if (update_enum == UOPT_SPEC_REVERT_RESHAPE_NOBACKUP &&
01ff50
 			    sb->reshape_position == 0 &&
01ff50
 			    (__le32_to_cpu(sb->delta_disks) > 0 ||
01ff50
 			     (__le32_to_cpu(sb->delta_disks) == 0 &&
01ff50
@@ -1575,32 +1597,40 @@ static int update_super1(struct supertype *st, struct mdinfo *info,
01ff50
 			}
01ff50
 		done:;
01ff50
 		}
01ff50
-	} else if (strcmp(update, "_reshape_progress") == 0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC__RESHAPE_PROGRESS:
01ff50
 		sb->reshape_position = __cpu_to_le64(info->reshape_progress);
01ff50
-	else if (strcmp(update, "writemostly") == 0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC_WRITEMOSTLY:
01ff50
 		sb->devflags |= WriteMostly1;
01ff50
-	else if (strcmp(update, "readwrite") == 0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC_READWRITE:
01ff50
 		sb->devflags &= ~WriteMostly1;
01ff50
-	else if (strcmp(update, "failfast") == 0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC_FAILFAST:
01ff50
 		sb->devflags |= FailFast1;
01ff50
-	else if (strcmp(update, "nofailfast") == 0)
01ff50
+		break;
01ff50
+	case UOPT_SPEC_NOFAILFAST:
01ff50
 		sb->devflags &= ~FailFast1;
01ff50
-	else if (strcmp(update, "layout-original") == 0 ||
01ff50
-		 strcmp(update, "layout-alternate") == 0 ||
01ff50
-		 strcmp(update, "layout-unspecified") == 0) {
01ff50
+		break;
01ff50
+	case UOPT_LAYOUT_ORIGINAL:
01ff50
+	case UOPT_LAYOUT_ALTERNATE:
01ff50
+	case UOPT_LAYOUT_UNSPECIFIED:
01ff50
 		if (__le32_to_cpu(sb->level) != 0) {
01ff50
 			pr_err("%s: %s only supported for RAID0\n",
01ff50
-			       devname?:"", update);
01ff50
+			       devname ?: "", map_num(update_options, update_enum));
01ff50
 			rv = -1;
01ff50
-		} else if (strcmp(update, "layout-unspecified") == 0) {
01ff50
+		} else if (update_enum == UOPT_LAYOUT_UNSPECIFIED) {
01ff50
 			sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_RAID0_LAYOUT);
01ff50
 			sb->layout = 0;
01ff50
 		} else {
01ff50
 			sb->feature_map |= __cpu_to_le32(MD_FEATURE_RAID0_LAYOUT);
01ff50
-			sb->layout = __cpu_to_le32(update[7] == 'o' ? 1 : 2);
01ff50
+			sb->layout = __cpu_to_le32(update_enum == UOPT_LAYOUT_ORIGINAL ? 1 : 2);
01ff50
 		}
01ff50
-	} else
01ff50
+		break;
01ff50
+	default:
01ff50
 		rv = -1;
01ff50
+	}
01ff50
 
01ff50
 	sb->sb_csum = calc_sb_1_csum(sb);
01ff50
 
01ff50
-- 
01ff50
2.38.1
01ff50