]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Assemble.c
assemble: combine the two create_mddev calls in to one.
[thirdparty/mdadm.git] / Assemble.c
index a73e101ed047e75c139ab48b84a70f0c15270df2..c23da0d3846dc957bdd87ec41847b8eef439bf93 100644 (file)
@@ -50,7 +50,7 @@ static int name_matches(char *found, char *required, char *homehost)
        return 0;
 }
 
-int Assemble(struct supertype *st, char *mddev, int mdfd,
+int Assemble(struct supertype *st, char *mddev,
             mddev_ident_t ident,
             mddev_dev_t devlist, char *backup_file,
             int readonly, int runstop,
@@ -111,10 +111,11 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
         *    START_ARRAY
         *
         */
-       int clean = 0;
-       int must_close = 0;
+       int mdfd;
+       int clean;
+       int auto_assem = (mddev == NULL);
        int old_linux = 0;
-       int vers = 0; /* Keep gcc quite - it really is initialised */
+       int vers = vers; /* Keep gcc quite - it really is initialised */
        struct {
                char *devname;
                int uptodate; /* set once we decide that this device is as
@@ -133,10 +134,12 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
        int change = 0;
        int inargv = 0;
        int bitmap_done;
-       int start_partial_ok = (runstop >= 0) && (force || devlist==NULL || mdfd < 0);
+       int start_partial_ok = (runstop >= 0) && 
+               (force || devlist==NULL || auto_assem);
        unsigned int num_devs;
        mddev_dev_t tmpdev;
        struct mdinfo info;
+       mdu_array_info_t tmp_inf;
        char *avail;
        int nextspare = 0;
        int uuid_for_name = 0;
@@ -146,25 +149,6 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
        if (get_linux_version() < 2004000)
                old_linux = 1;
 
-       if (mdfd >= 0) {
-               vers = md_get_version(mdfd);
-               if (vers <= 0) {
-                       fprintf(stderr, Name ": %s appears not to be an md device.\n", mddev);
-                       return 1;
-               }
-               if (vers < 9000) {
-                       fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
-                               "    Upgrade your kernel or try --build\n");
-                       return 1;
-               }
-
-               if (ioctl(mdfd, GET_ARRAY_INFO, &info.array)>=0) {
-                       fprintf(stderr, Name ": device %s already active - cannot assemble it\n",
-                               mddev);
-                       return 1;
-               }
-               ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
-       }
        /*
         * If any subdevs are listed, then any that don't
         * match ident are discarded.  Remainder must all match and
@@ -181,12 +165,62 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        mddev ? mddev : "further assembly");
                return 1;
        }
+
+       /* if the configuration specifies a container then we use that to
+        * determine the devices and retrieve the array configuration
+        */
+#ifndef MDASSEMBLE
+       if (ident->container && ident->member) {
+               int cfd = open(ident->container, O_RDWR);
+               struct mdinfo *mdi;
+               struct supertype container;
+
+               if (verbose>0)
+                       fprintf(stderr, Name ": looking to assemble member array %s"
+                               " inside container %s\n", ident->member, ident->container);
+               if (cfd < 0) {
+                       if (verbose>0)
+                               fprintf(stderr, Name ": unable to open container %s: %s\n",
+                                       ident->container, strerror(errno));
+                       return 1;
+               }
+
+               mdi = sysfs_read(cfd, fd2devnum(cfd), GET_VERSION);
+               if (!mdi) {
+                       close(cfd);
+                       if (verbose>0)
+                               fprintf(stderr, Name ": unable to read container %s\n",
+                                       ident->container);
+                       return 1;
+               }
+               container.ss = find_metadata_methods(mdi->text_version);
+               sysfs_free(mdi);
+               if (!container.ss) {
+                       close(cfd);
+                       fprintf(stderr, Name ": %s uses unknown metadata: %s\n",
+                               ident->container, mdi->text_version);
+                       return 1;
+               }
+               if (container.ss->load_super(&container, cfd, ident->container)) {
+                       fprintf(stderr, Name ": Cannot load metadata for container %s\n",
+                               ident->container);
+                       return 1;
+               }
+
+               return Incremental_container(&container, ident->container,
+                                            verbose, runstop, ident->autof);
+       }
+#endif
        if (devlist == NULL)
                devlist = conf_get_devs();
-       else if (mdfd >= 0)
+       else if (mddev)
                inargv = 1;
 
  try_again:
+       /* We come back here when doing auto-assembly and attempting some
+        * set of devices failed.  Those are now marked as ->used==2 and
+        * we ignore them and try again
+        */
 
        tmpdev = devlist; num_devs = 0;
        while (tmpdev) {
@@ -206,7 +240,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
 
        /* first walk the list of devices to find a consistent set
         * that match the criterea, if that is possible.
-        * We flag the one we like with 'used'.
+        * We flag the ones we like with 'used'.
         */
        for (tmpdev = devlist;
             tmpdev;
@@ -294,7 +328,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                        devname);
                        goto loop;
                }
-               if (mdfd < 0) {
+               if (auto_assem) {
                        if (tst == NULL || tst->sb == NULL)
                                continue;
                        switch(tst->ss->match_home(tst, homehost))
@@ -348,7 +382,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                         * Or, if we are auto assembling, we just ignore the second
                         * for now.
                         */
-                       if (mdfd < 0)
+                       if (auto_assem)
                                goto loop;
                        if (homehost) {
                                int first = st->ss->match_home(st, homehost);
@@ -388,17 +422,19 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        tst->ss->free_super(tst);
        }
 
-       if (mdfd < 0) {
-               /* So... it is up to me to open the device.
-                * We create a name '/dev/md/XXX' based on the info in the
-                * superblock, and call open_mddev on that
-                */
-               mdu_array_info_t inf;
-               char *c;
+       if (!st || !st->sb)
+               return 2;
+
+       /* Now need to open array the device.
+        * We create a name '/dev/md/XXX' based on the info in the
+        * superblock, and call create_mddev on that
+        */
+
+       if (mddev == NULL) {
                char nbuf[64];
-               if (!st || !st->sb) {
-                       return 2;
-               }
+               char *c;
+               int rc;
+
                st->ss->getinfo_super(st, &info);
                if (uuid_for_name)
                        c = fname_from_uuid(st, &info, nbuf, '-');
@@ -408,33 +444,50 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                }
                if (isdigit(*c) && ((ident->autof & 7)==4 || (ident->autof&7)==6))
                        /* /dev/md/d0 style for partitionable */
-                       asprintf(&mddev, "/dev/md/d%s", c);
+                       rc = asprintf(&mddev, "/dev/md/d%s", c);
                else
-                       asprintf(&mddev, "/dev/md/%s", c);
-               mdfd = open_mddev(mddev, ident->autof);
-               if (mdfd < 0) {
+                       rc = asprintf(&mddev, "/dev/md/%s", c);
+               if (rc < 0) {
                        st->ss->free_super(st);
                        free(devices);
+                       mddev = NULL;
                        goto try_again;
                }
-               vers = md_get_version(mdfd);
-               if (ioctl(mdfd, GET_ARRAY_INFO, &inf)==0) {
-                       for (tmpdev = devlist ;
-                            tmpdev && tmpdev->used != 1;
-                            tmpdev = tmpdev->next)
-                               ;
-                       fprintf(stderr, Name ": %s already active, cannot restart it!\n", mddev);
-                       if (tmpdev)
-                               fprintf(stderr, Name ":   %s needed for %s...\n",
-                                       mddev, tmpdev->devname);
-                       close(mdfd);
-                       mdfd = -1;
-                       st->ss->free_super(st);
-                       free(devices);
+       }
+       mdfd = create_mddev(mddev, ident->autof);
+       if (mdfd < 0) {
+               st->ss->free_super(st);
+               free(devices);
+               if (auto_assem)
                        goto try_again;
-               }
-               must_close = 1;
+               return 1;
        }
+       vers = md_get_version(mdfd);
+       if (vers < 9000) {
+               fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n"
+                       "    Upgrade your kernel or try --build\n");
+               close(mdfd);
+               return 1;
+       }
+       if (ioctl(mdfd, GET_ARRAY_INFO, &tmp_inf)==0) {
+               fprintf(stderr, Name ": %s already active, cannot restart it!\n",
+                       mddev);
+               for (tmpdev = devlist ;
+                    tmpdev && tmpdev->used != 1;
+                    tmpdev = tmpdev->next)
+                       ;
+               if (tmpdev && auto_assem)
+                       fprintf(stderr, Name ":   %s needed for %s...\n",
+                               mddev, tmpdev->devname);
+               close(mdfd);
+               mdfd = -3;
+               st->ss->free_super(st);
+               free(devices);
+               if (auto_assem)
+                       goto try_again;
+               return 1;
+       }
+       ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */
 
        /* Ok, no bad inconsistancy, we can try updating etc */
        bitmap_done = 0;
@@ -580,7 +633,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                        inargv ? "the list" :
                                           "the\n      DEVICE list in mdadm.conf"
                                        );
-                               if (must_close) close(mdfd);
+                               close(mdfd);
                                return 1;
                        }
                        if (best[i] == -1
@@ -596,7 +649,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        mddev);
                if (st)
                        st->ss->free_super(st);
-               if (must_close) close(mdfd);
+               close(mdfd);
                return 1;
        }
 
@@ -740,21 +793,21 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                if ((fd=dev_open(devices[j].devname, O_RDONLY|O_EXCL))< 0) {
                        fprintf(stderr, Name ": Cannot open %s: %s\n",
                                devices[j].devname, strerror(errno));
-                       if (must_close) close(mdfd);
+                       close(mdfd);
                        return 1;
                }
                if (st->ss->load_super(st,fd, NULL)) {
                        close(fd);
                        fprintf(stderr, Name ": RAID superblock has disappeared from %s\n",
                                devices[j].devname);
-                       if (must_close) close(mdfd);
+                       close(mdfd);
                        return 1;
                }
                close(fd);
        }
        if (st->sb == NULL) {
                fprintf(stderr, Name ": No suitable drives found for %s\n", mddev);
-               if (must_close) close(mdfd);
+               close(mdfd);
                return 1;
        }
        st->ss->getinfo_super(st, &info);
@@ -815,14 +868,14 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                if (fd < 0) {
                        fprintf(stderr, Name ": Could not open %s for write - cannot Assemble array.\n",
                                devices[chosen_drive].devname);
-                       if (must_close) close(mdfd);
+                       close(mdfd);
                        return 1;
                }
                if (st->ss->store_super(st, fd)) {
                        close(fd);
                        fprintf(stderr, Name ": Could not re-write superblock on %s\n",
                                devices[chosen_drive].devname);
-                       if (must_close) close(mdfd);
+                       close(mdfd);
                        return 1;
                }
                close(fd);
@@ -857,7 +910,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                }
                if (err) {
                        fprintf(stderr, Name ": Failed to restore critical section for reshape, sorry.\n");
-                       if (must_close) close(mdfd);
+                       close(mdfd);
                        return err;
                }
        }
@@ -875,13 +928,13 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                if (rv) {
                        fprintf(stderr, Name ": failed to set array info for %s: %s\n",
                                mddev, strerror(errno));
-                       if (must_close) close(mdfd);
+                       close(mdfd);
                        return 1;
                }
                if (ident->bitmap_fd >= 0) {
                        if (ioctl(mdfd, SET_BITMAP_FILE, ident->bitmap_fd) != 0) {
                                fprintf(stderr, Name ": SET_BITMAP_FILE failed.\n");
-                               if (must_close) close(mdfd);
+                               close(mdfd);
                                return 1;
                        }
                } else if (ident->bitmap_file) {
@@ -890,13 +943,13 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        if (bmfd < 0) {
                                fprintf(stderr, Name ": Could not open bitmap file %s\n",
                                        ident->bitmap_file);
-                               if (must_close) close(mdfd);
+                               close(mdfd);
                                return 1;
                        }
                        if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
                                fprintf(stderr, Name ": Failed to set bitmapfile for %s\n", mddev);
                                close(bmfd);
-                               if (must_close) close(mdfd);
+                               close(mdfd);
                                return 1;
                        }
                        close(bmfd);
@@ -941,14 +994,13 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        if (verbose >= 0) {
                                fprintf(stderr, Name ": Container %s has been "
                                        "assembled with %d drive%s",
-                                       mddev, okcnt, okcnt==1?"":"s");
+                                       mddev, okcnt+sparecnt, okcnt+sparecnt==1?"":"s");
                                if (okcnt < info.array.raid_disks)
                                        fprintf(stderr, " (out of %d)",
                                                info.array.raid_disks);
                                fprintf(stderr, "\n");
                        }
-                       if (must_close)
-                               close(mdfd);
+                       close(mdfd);
                        return 0;
                }
 
@@ -968,9 +1020,9 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                                fprintf(stderr, " and %d spare%s", sparecnt, sparecnt==1?"":"s");
                                        fprintf(stderr, ".\n");
                                }
-                               if (must_close) {
+                               close(mdfd);
+                               if (auto_assem) {
                                        int usecs = 1;
-                                       close(mdfd);
                                        /* There is a nasty race with 'mdadm --monitor'.
                                         * If it opens this device before we close it,
                                         * it gets an incomplete open on which IO
@@ -1012,10 +1064,9 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                        "start the array while not clean "
                                        "- consider --force.\n");
 
-                       if (must_close) {
+                       if (auto_assem)
                                ioctl(mdfd, STOP_ARRAY, NULL);
-                               close(mdfd);
-                       }
+                       close(mdfd);
                        return 1;
                }
                if (runstop == -1) {
@@ -1024,7 +1075,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        if (okcnt != info.array.raid_disks)
                                fprintf(stderr, " (out of %d)", info.array.raid_disks);
                        fprintf(stderr, ", but not started.\n");
-                       if (must_close) close(mdfd);
+                       close(mdfd);
                        return 0;
                }
                if (verbose >= -1) {
@@ -1049,10 +1100,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                fprintf(stderr, " (use --run to insist).\n");
                        }
                }
-               if (must_close) {
+               if (auto_assem)
                        ioctl(mdfd, STOP_ARRAY, NULL);
-                       close(mdfd);
-               }
                return 1;
        } else {
                /* The "chosen_drive" is a good choice, and if necessary, the superblock has
@@ -1068,6 +1117,6 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                }
 
        }
-       if (must_close) close(mdfd);
+       close(mdfd);
        return 0;
 }