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

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