Blame SOURCES/0051-Grow-Split-Grow_reshape-into-helper-function.patch

fdf7c0
From 7211116c295ba1f9e1fcbdc2dd2d3762855062e1 Mon Sep 17 00:00:00 2001
fdf7c0
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
fdf7c0
Date: Thu, 28 Jul 2022 20:20:53 +0800
01ff50
Subject: [PATCH 51/83] Grow: Split Grow_reshape into helper function
fdf7c0
fdf7c0
Grow_reshape should be split into helper functions given its size.
fdf7c0
- Add helper function for preparing reshape on external metadata.
fdf7c0
- Close cfd file descriptor.
fdf7c0
fdf7c0
Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
fdf7c0
Acked-by: Coly Li <colyli@suse.de>
fdf7c0
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
fdf7c0
---
fdf7c0
 Grow.c  | 125 ++++++++++++++++++++++++++++++--------------------------
fdf7c0
 mdadm.h |   1 +
fdf7c0
 util.c  |  14 +++++++
fdf7c0
 3 files changed, 81 insertions(+), 59 deletions(-)
fdf7c0
fdf7c0
diff --git a/Grow.c b/Grow.c
fdf7c0
index 868bdc3a..0f07a894 100644
fdf7c0
--- a/Grow.c
fdf7c0
+++ b/Grow.c
fdf7c0
@@ -1773,6 +1773,65 @@ static int reshape_container(char *container, char *devname,
fdf7c0
 			     char *backup_file, int verbose,
fdf7c0
 			     int forked, int restart, int freeze_reshape);
fdf7c0
 
fdf7c0
+/**
fdf7c0
+ * prepare_external_reshape() - prepares update on external metadata if supported.
fdf7c0
+ * @devname: Device name.
fdf7c0
+ * @subarray: Subarray.
fdf7c0
+ * @st: Supertype.
fdf7c0
+ * @container: Container.
fdf7c0
+ * @cfd: Container file descriptor.
fdf7c0
+ *
fdf7c0
+ * Function checks that the requested reshape is supported on external metadata,
fdf7c0
+ * and performs an initial check that the container holds the pre-requisite
fdf7c0
+ * spare devices (mdmon owns final validation).
fdf7c0
+ *
fdf7c0
+ * Return: 0 on success, else 1
fdf7c0
+ */
fdf7c0
+static int prepare_external_reshape(char *devname, char *subarray,
fdf7c0
+				    struct supertype *st, char *container,
fdf7c0
+				    const int cfd)
fdf7c0
+{
fdf7c0
+	struct mdinfo *cc = NULL;
fdf7c0
+	struct mdinfo *content = NULL;
fdf7c0
+
fdf7c0
+	if (st->ss->load_container(st, cfd, NULL)) {
fdf7c0
+		pr_err("Cannot read superblock for %s\n", devname);
fdf7c0
+		return 1;
fdf7c0
+	}
fdf7c0
+
fdf7c0
+	if (!st->ss->container_content)
fdf7c0
+		return 1;
fdf7c0
+
fdf7c0
+	cc = st->ss->container_content(st, subarray);
fdf7c0
+	for (content = cc; content ; content = content->next) {
fdf7c0
+		/*
fdf7c0
+		 * check if reshape is allowed based on metadata
fdf7c0
+		 * indications stored in content.array.status
fdf7c0
+		 */
fdf7c0
+		if (is_bit_set(&content->array.state, MD_SB_BLOCK_VOLUME) ||
fdf7c0
+		    is_bit_set(&content->array.state, MD_SB_BLOCK_CONTAINER_RESHAPE)) {
fdf7c0
+			pr_err("Cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
fdf7c0
+			       devname, container);
fdf7c0
+			goto error;
fdf7c0
+		}
fdf7c0
+		if (content->consistency_policy == CONSISTENCY_POLICY_PPL) {
fdf7c0
+			pr_err("Operation not supported when ppl consistency policy is enabled\n");
fdf7c0
+			goto error;
fdf7c0
+		}
fdf7c0
+		if (content->consistency_policy == CONSISTENCY_POLICY_BITMAP) {
fdf7c0
+			pr_err("Operation not supported when write-intent bitmap consistency policy is enabled\n");
fdf7c0
+			goto error;
fdf7c0
+		}
fdf7c0
+	}
fdf7c0
+	sysfs_free(cc);
fdf7c0
+	if (mdmon_running(container))
fdf7c0
+		st->update_tail = &st->updates;
fdf7c0
+	return 0;
fdf7c0
+error:
fdf7c0
+	sysfs_free(cc);
fdf7c0
+	return 1;
fdf7c0
+}
fdf7c0
+
fdf7c0
 int Grow_reshape(char *devname, int fd,
fdf7c0
 		 struct mddev_dev *devlist,
fdf7c0
 		 struct context *c, struct shape *s)
fdf7c0
@@ -1799,7 +1858,7 @@ int Grow_reshape(char *devname, int fd,
fdf7c0
 	struct supertype *st;
fdf7c0
 	char *subarray = NULL;
fdf7c0
 
fdf7c0
-	int frozen;
fdf7c0
+	int frozen = 0;
fdf7c0
 	int changed = 0;
fdf7c0
 	char *container = NULL;
fdf7c0
 	int cfd = -1;
fdf7c0
@@ -1808,7 +1867,7 @@ int Grow_reshape(char *devname, int fd,
fdf7c0
 	int added_disks;
fdf7c0
 
fdf7c0
 	struct mdinfo info;
fdf7c0
-	struct mdinfo *sra;
fdf7c0
+	struct mdinfo *sra = NULL;
fdf7c0
 
fdf7c0
 	if (md_get_array_info(fd, &array) < 0) {
fdf7c0
 		pr_err("%s is not an active md array - aborting\n",
fdf7c0
@@ -1865,13 +1924,7 @@ int Grow_reshape(char *devname, int fd,
fdf7c0
 		}
fdf7c0
 	}
fdf7c0
 
fdf7c0
-	/* in the external case we need to check that the requested reshape is
fdf7c0
-	 * supported, and perform an initial check that the container holds the
fdf7c0
-	 * pre-requisite spare devices (mdmon owns final validation)
fdf7c0
-	 */
fdf7c0
 	if (st->ss->external) {
fdf7c0
-		int retval;
fdf7c0
-
fdf7c0
 		if (subarray) {
fdf7c0
 			container = st->container_devnm;
fdf7c0
 			cfd = open_dev_excl(st->container_devnm);
fdf7c0
@@ -1887,13 +1940,12 @@ int Grow_reshape(char *devname, int fd,
fdf7c0
 			return 1;
fdf7c0
 		}
fdf7c0
 
fdf7c0
-		retval = st->ss->load_container(st, cfd, NULL);
fdf7c0
-
fdf7c0
-		if (retval) {
fdf7c0
-			pr_err("Cannot read superblock for %s\n", devname);
fdf7c0
-			close(cfd);
fdf7c0
+		rv = prepare_external_reshape(devname, subarray, st,
fdf7c0
+					      container, cfd);
fdf7c0
+		if (rv > 0) {
fdf7c0
 			free(subarray);
fdf7c0
-			return 1;
fdf7c0
+			close(cfd);
fdf7c0
+			goto release;
fdf7c0
 		}
fdf7c0
 
fdf7c0
 		if (s->raiddisks && subarray) {
fdf7c0
@@ -1902,51 +1954,6 @@ int Grow_reshape(char *devname, int fd,
fdf7c0
 			free(subarray);
fdf7c0
 			return 1;
fdf7c0
 		}
fdf7c0
-
fdf7c0
-		/* check if operation is supported for metadata handler */
fdf7c0
-		if (st->ss->container_content) {
fdf7c0
-			struct mdinfo *cc = NULL;
fdf7c0
-			struct mdinfo *content = NULL;
fdf7c0
-
fdf7c0
-			cc = st->ss->container_content(st, subarray);
fdf7c0
-			for (content = cc; content ; content = content->next) {
fdf7c0
-				int allow_reshape = 1;
fdf7c0
-
fdf7c0
-				/* check if reshape is allowed based on metadata
fdf7c0
-				 * indications stored in content.array.status
fdf7c0
-				 */
fdf7c0
-				if (content->array.state &
fdf7c0
-				    (1 << MD_SB_BLOCK_VOLUME))
fdf7c0
-					allow_reshape = 0;
fdf7c0
-				if (content->array.state &
fdf7c0
-				    (1 << MD_SB_BLOCK_CONTAINER_RESHAPE))
fdf7c0
-					allow_reshape = 0;
fdf7c0
-				if (!allow_reshape) {
fdf7c0
-					pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
fdf7c0
-					       devname, container);
fdf7c0
-					sysfs_free(cc);
fdf7c0
-					free(subarray);
fdf7c0
-					return 1;
fdf7c0
-				}
fdf7c0
-				if (content->consistency_policy ==
fdf7c0
-				    CONSISTENCY_POLICY_PPL) {
fdf7c0
-					pr_err("Operation not supported when ppl consistency policy is enabled\n");
fdf7c0
-					sysfs_free(cc);
fdf7c0
-					free(subarray);
fdf7c0
-					return 1;
fdf7c0
-				}
fdf7c0
-				if (content->consistency_policy ==
fdf7c0
-				    CONSISTENCY_POLICY_BITMAP) {
fdf7c0
-					pr_err("Operation not supported when write-intent bitmap is enabled\n");
fdf7c0
-					sysfs_free(cc);
fdf7c0
-					free(subarray);
fdf7c0
-					return 1;
fdf7c0
-				}
fdf7c0
-			}
fdf7c0
-			sysfs_free(cc);
fdf7c0
-		}
fdf7c0
-		if (mdmon_running(container))
fdf7c0
-			st->update_tail = &st->updates;
fdf7c0
 	}
fdf7c0
 
fdf7c0
 	added_disks = 0;
fdf7c0
diff --git a/mdadm.h b/mdadm.h
fdf7c0
index 8208b81e..941a5f38 100644
fdf7c0
--- a/mdadm.h
fdf7c0
+++ b/mdadm.h
fdf7c0
@@ -1539,6 +1539,7 @@ extern int stat_is_blkdev(char *devname, dev_t *rdev);
fdf7c0
 extern bool is_dev_alive(char *path);
fdf7c0
 extern int get_mdp_major(void);
fdf7c0
 extern int get_maj_min(char *dev, int *major, int *minor);
fdf7c0
+extern bool is_bit_set(int *val, unsigned char index);
fdf7c0
 extern int dev_open(char *dev, int flags);
fdf7c0
 extern int open_dev(char *devnm);
fdf7c0
 extern void reopen_mddev(int mdfd);
fdf7c0
diff --git a/util.c b/util.c
fdf7c0
index ca48d976..26ffdcea 100644
fdf7c0
--- a/util.c
fdf7c0
+++ b/util.c
fdf7c0
@@ -1027,6 +1027,20 @@ int get_maj_min(char *dev, int *major, int *minor)
fdf7c0
 		*e == 0);
fdf7c0
 }
fdf7c0
 
fdf7c0
+/**
fdf7c0
+ * is_bit_set() - get bit value by index.
fdf7c0
+ * @val: value.
fdf7c0
+ * @index: index of the bit (LSB numbering).
fdf7c0
+ *
fdf7c0
+ * Return: bit value.
fdf7c0
+ */
fdf7c0
+bool is_bit_set(int *val, unsigned char index)
fdf7c0
+{
fdf7c0
+	if ((*val) & (1 << index))
fdf7c0
+		return true;
fdf7c0
+	return false;
fdf7c0
+}
fdf7c0
+
fdf7c0
 int dev_open(char *dev, int flags)
fdf7c0
 {
fdf7c0
 	/* like 'open', but if 'dev' matches %d:%d, create a temp
fdf7c0
-- 
01ff50
2.38.1
fdf7c0