]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
Grow: Split Grow_reshape into helper function
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
index 868bdc3a3be4fe8134a342d0dbcd43b52136015c..0f07a89435af4d54c3293da19b6707edbded8790 100644 (file)
--- a/Grow.c
+++ b/Grow.c
@@ -1773,6 +1773,65 @@ static int reshape_container(char *container, char *devname,
                             char *backup_file, int verbose,
                             int forked, int restart, int freeze_reshape);
 
+/**
+ * prepare_external_reshape() - prepares update on external metadata if supported.
+ * @devname: Device name.
+ * @subarray: Subarray.
+ * @st: Supertype.
+ * @container: Container.
+ * @cfd: Container file descriptor.
+ *
+ * Function checks that the requested reshape is supported on external metadata,
+ * and performs an initial check that the container holds the pre-requisite
+ * spare devices (mdmon owns final validation).
+ *
+ * Return: 0 on success, else 1
+ */
+static int prepare_external_reshape(char *devname, char *subarray,
+                                   struct supertype *st, char *container,
+                                   const int cfd)
+{
+       struct mdinfo *cc = NULL;
+       struct mdinfo *content = NULL;
+
+       if (st->ss->load_container(st, cfd, NULL)) {
+               pr_err("Cannot read superblock for %s\n", devname);
+               return 1;
+       }
+
+       if (!st->ss->container_content)
+               return 1;
+
+       cc = st->ss->container_content(st, subarray);
+       for (content = cc; content ; content = content->next) {
+               /*
+                * check if reshape is allowed based on metadata
+                * indications stored in content.array.status
+                */
+               if (is_bit_set(&content->array.state, MD_SB_BLOCK_VOLUME) ||
+                   is_bit_set(&content->array.state, MD_SB_BLOCK_CONTAINER_RESHAPE)) {
+                       pr_err("Cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
+                              devname, container);
+                       goto error;
+               }
+               if (content->consistency_policy == CONSISTENCY_POLICY_PPL) {
+                       pr_err("Operation not supported when ppl consistency policy is enabled\n");
+                       goto error;
+               }
+               if (content->consistency_policy == CONSISTENCY_POLICY_BITMAP) {
+                       pr_err("Operation not supported when write-intent bitmap consistency policy is enabled\n");
+                       goto error;
+               }
+       }
+       sysfs_free(cc);
+       if (mdmon_running(container))
+               st->update_tail = &st->updates;
+       return 0;
+error:
+       sysfs_free(cc);
+       return 1;
+}
+
 int Grow_reshape(char *devname, int fd,
                 struct mddev_dev *devlist,
                 struct context *c, struct shape *s)
@@ -1799,7 +1858,7 @@ int Grow_reshape(char *devname, int fd,
        struct supertype *st;
        char *subarray = NULL;
 
-       int frozen;
+       int frozen = 0;
        int changed = 0;
        char *container = NULL;
        int cfd = -1;
@@ -1808,7 +1867,7 @@ int Grow_reshape(char *devname, int fd,
        int added_disks;
 
        struct mdinfo info;
-       struct mdinfo *sra;
+       struct mdinfo *sra = NULL;
 
        if (md_get_array_info(fd, &array) < 0) {
                pr_err("%s is not an active md array - aborting\n",
@@ -1865,13 +1924,7 @@ int Grow_reshape(char *devname, int fd,
                }
        }
 
-       /* in the external case we need to check that the requested reshape is
-        * supported, and perform an initial check that the container holds the
-        * pre-requisite spare devices (mdmon owns final validation)
-        */
        if (st->ss->external) {
-               int retval;
-
                if (subarray) {
                        container = st->container_devnm;
                        cfd = open_dev_excl(st->container_devnm);
@@ -1887,13 +1940,12 @@ int Grow_reshape(char *devname, int fd,
                        return 1;
                }
 
-               retval = st->ss->load_container(st, cfd, NULL);
-
-               if (retval) {
-                       pr_err("Cannot read superblock for %s\n", devname);
-                       close(cfd);
+               rv = prepare_external_reshape(devname, subarray, st,
+                                             container, cfd);
+               if (rv > 0) {
                        free(subarray);
-                       return 1;
+                       close(cfd);
+                       goto release;
                }
 
                if (s->raiddisks && subarray) {
@@ -1902,51 +1954,6 @@ int Grow_reshape(char *devname, int fd,
                        free(subarray);
                        return 1;
                }
-
-               /* check if operation is supported for metadata handler */
-               if (st->ss->container_content) {
-                       struct mdinfo *cc = NULL;
-                       struct mdinfo *content = NULL;
-
-                       cc = st->ss->container_content(st, subarray);
-                       for (content = cc; content ; content = content->next) {
-                               int allow_reshape = 1;
-
-                               /* check if reshape is allowed based on metadata
-                                * indications stored in content.array.status
-                                */
-                               if (content->array.state &
-                                   (1 << MD_SB_BLOCK_VOLUME))
-                                       allow_reshape = 0;
-                               if (content->array.state &
-                                   (1 << MD_SB_BLOCK_CONTAINER_RESHAPE))
-                                       allow_reshape = 0;
-                               if (!allow_reshape) {
-                                       pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
-                                              devname, container);
-                                       sysfs_free(cc);
-                                       free(subarray);
-                                       return 1;
-                               }
-                               if (content->consistency_policy ==
-                                   CONSISTENCY_POLICY_PPL) {
-                                       pr_err("Operation not supported when ppl consistency policy is enabled\n");
-                                       sysfs_free(cc);
-                                       free(subarray);
-                                       return 1;
-                               }
-                               if (content->consistency_policy ==
-                                   CONSISTENCY_POLICY_BITMAP) {
-                                       pr_err("Operation not supported when write-intent bitmap is enabled\n");
-                                       sysfs_free(cc);
-                                       free(subarray);
-                                       return 1;
-                               }
-                       }
-                       sysfs_free(cc);
-               }
-               if (mdmon_running(container))
-                       st->update_tail = &st->updates;
        }
 
        added_disks = 0;