]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Assemble.c
Grow: allow a RAID1 to be reshaped directly to 3-drive RAID5
[thirdparty/mdadm.git] / Assemble.c
index 36fd4b7afb25a5fc344d54bbcaf4f8d7069203f1..20c27eb7e76b2d6e38ff09037035f3812838f117 100644 (file)
@@ -330,11 +330,13 @@ int Assemble(struct supertype *st, char *mddev,
                                        fprintf(stderr, Name ": %s is a container, but we are looking for components\n",
                                                devname);
                                tmpdev->used = 2;
+#if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
                        } if (!tst && (tst = super_by_fd(dfd, NULL)) == NULL) {
                                if (report_missmatch)
                                        fprintf(stderr, Name ": not a recognisable container: %s\n",
                                                devname);
                                tmpdev->used = 2;
+#endif
                        } else if (!tst->ss->load_container
                                   || tst->ss->load_container(tst, dfd, NULL)) {
                                if (report_missmatch)
@@ -379,7 +381,7 @@ int Assemble(struct supertype *st, char *mddev,
                }
                if (dfd >= 0) close(dfd);
                if (tmpdev->used == 2) {
-                       if (auto_assem)
+                       if (auto_assem || !inargv)
                                /* Ignore unrecognised devices during auto-assembly */
                                goto loop;
                        if (ident->uuid_set || ident->name[0] ||
@@ -595,10 +597,15 @@ int Assemble(struct supertype *st, char *mddev,
                                tmpdev->devname, strerror(errno));
                        tmpdev->used = 2;
                } else {
-                       struct dev_policy *pol = NULL;
-                       pol = devnum_policy(stb.st_rdev);
-                       if (domain_test(domains, pol, NULL))
-                               /* take this spare if domains match */
+                       struct dev_policy *pol = devnum_policy(stb.st_rdev);
+                       int dt = domain_test(domains, pol, NULL);
+                       if (inargv && dt != 0)
+                               /* take this spare as domains match
+                                * if there are any */
+                               tmpdev->used = 1;
+                       else if (!inargv && dt == 1)
+                               /* device wasn't explicitly listed, so need
+                                * explicit domain match - which we have */
                                tmpdev->used = 1;
                        else
                                /* if domains don't match mark as unused */
@@ -689,8 +696,12 @@ int Assemble(struct supertype *st, char *mddev,
 #ifndef MDASSEMBLE
        if (content != &info) {
                /* This is a member of a container.  Try starting the array. */
-               return assemble_container_content(st, mdfd, content, runstop,
-                                          chosen_name, verbose);
+               int err;
+               err = assemble_container_content(st, mdfd, content, runstop,
+                                                chosen_name, verbose,
+                                                backup_file);
+               close(mdfd);
+               return err;
        }
 #endif
        /* Ok, no bad inconsistancy, we can try updating etc */
@@ -1304,6 +1315,7 @@ int Assemble(struct supertype *st, char *mddev,
                                                content->array.raid_disks);
                                fprintf(stderr, "\n");
                        }
+                       st->ss->free_super(st);
                        sysfs_uevent(content, "change");
                        wait_for(chosen_name, mdfd);
                        close(mdfd);
@@ -1492,20 +1504,21 @@ int Assemble(struct supertype *st, char *mddev,
 #ifndef MDASSEMBLE
 int assemble_container_content(struct supertype *st, int mdfd,
                               struct mdinfo *content, int runstop,
-                              char *chosen_name, int verbose)
+                              char *chosen_name, int verbose,
+                              char *backup_file)
 {
        struct mdinfo *dev, *sra;
        int working = 0, preexist = 0;
+       int expansion = 0;
        struct map_ent *map = NULL;
 
        sysfs_init(content, mdfd, 0);
 
        sra = sysfs_read(mdfd, 0, GET_VERSION);
        if (sra == NULL || strcmp(sra->text_version, content->text_version) != 0)
-               if (sysfs_set_array(content, md_get_version(mdfd)) != 0) {
-                       close(mdfd);
+               if (sysfs_set_array(content, md_get_version(mdfd)) != 0)
                        return 1;
-               }
+
        if (sra)
                sysfs_free(sra);
 
@@ -1514,20 +1527,55 @@ int assemble_container_content(struct supertype *st, int mdfd,
                        working++;
                else if (errno == EEXIST)
                        preexist++;
-       if (working == 0) {
-               close(mdfd);
+               else if (dev->disk.raid_disk >= content->array.raid_disks &&
+                         content->reshape_active)
+                       expansion++;
+       if (working == 0)
                return 1;/* Nothing new, don't try to start */
-       }
-       
+
        map_update(&map, fd2devnum(mdfd),
                   content->text_version,
                   content->uuid, chosen_name);
 
        if (runstop > 0 ||
-                (working + preexist) >= content->array.working_disks) {
+                (working + preexist + expansion) >=
+                       content->array.working_disks) {
                int err;
 
-               switch(content->array.level) {
+               if (content->reshape_active) {
+                       int spare = content->array.raid_disks + expansion;
+                       int i;
+                       int *fdlist = malloc(sizeof(int) *
+                                            (working + expansion
+                                             + content->array.raid_disks));
+                       for (i=0; i<spare; i++)
+                               fdlist[i] = -1;
+                       for (dev = content->devs; dev; dev = dev->next) {
+                               int fd = open_dev(makedev(dev->disk.major,
+                                                         dev->disk.minor));
+                               if (dev->disk.raid_disk >= 0)
+                                       fdlist[dev->disk.raid_disk] = fd;
+                               else
+                                       fdlist[spare++] = fd;
+                       }
+                       err = Grow_restart(st, content, fdlist, spare,
+                                          backup_file, verbose > 0);
+                       while (spare > 0) {
+                               spare--;
+                               if (fdlist[spare] >= 0)
+                                       close(fdlist[spare]);
+                       }
+                       if (err) {
+                               fprintf(stderr, Name ": Failed to restore critical"
+                                       " section for reshape - sorry.\n");
+                               if (!backup_file)
+                                       fprintf(stderr, Name ":  Possibly you need"
+                                               " to specify a --backup-file\n");
+                               return 1;
+                       }
+
+                       err = Grow_continue(mdfd, st, content, backup_file);
+               } else switch(content->array.level) {
                case LEVEL_LINEAR:
                case LEVEL_MULTIPATH:
                case 0:
@@ -1558,12 +1606,14 @@ int assemble_container_content(struct supertype *st, int mdfd,
                                        chosen_name, working + preexist);
                        if (preexist)
                                fprintf(stderr, " (%d new)", working);
+                       if (expansion)
+                               fprintf(stderr, " ( + %d for expansion)",
+                                       expansion);
                        fprintf(stderr, "\n");
                }
                if (!err)
                        wait_for(chosen_name, mdfd);
-               close(mdfd);
-               return 0;
+               return err;
                /* FIXME should have an O_EXCL and wait for read-auto */
        } else {
                if (verbose >= 0)
@@ -1571,7 +1621,6 @@ int assemble_container_content(struct supertype *st, int mdfd,
                                ": %s assembled with %d devices but "
                                "not started\n",
                                chosen_name, working);
-               close(mdfd);
                return 1;
        }
 }