]> 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 36b230435851b9449d78f02fa4952736717007cd..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,35 +134,21 @@ 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;
+
+       memset(&info, 0, sizeof(info));
 
        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
@@ -178,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) {
@@ -203,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;
@@ -291,18 +328,31 @@ 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;
-                       if (update == NULL &&
-                           tst->ss->match_home(tst, homehost)==0) {
+                       switch(tst->ss->match_home(tst, homehost))
+                       {
+                       case 1: /* happy with match. */
+                               break;
+                       case -1: /* cannot match */
+                               uuid_for_name = 1;
+                               break;
+                       case 0: /* Doesn't match */
+                               if (update)
+                                       /* We are changing the name*/
+                                       break;
                                if ((inargv && verbose >= 0) || verbose > 0)
-                                       fprintf(stderr, Name ": %s is not built for host %s.\n",
+                                       fprintf(stderr, Name ": %s is not built for "
+                                               "host %s - using UUID for "
+                                               "device name.\n",
                                                devname, homehost);
+                               
                                /* Auto-assemble, and this is not a usable host */
                                /* if update != NULL, we are updating the host
                                 * name... */
-                               goto loop;
+                               uuid_for_name = 1;
+                               break;
                        }
                }
                /* If we are this far, then we are nearly commited to this device.
@@ -332,12 +382,13 @@ 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);
                                int last = tst->ss->match_home(tst, homehost);
-                               if (first+last == 1) {
+                               if (first != last &&
+                                   (first == 1 || last == 1)) {
                                        /* We can do something */
                                        if (first) {/* just ignore this one */
                                                if ((inargv && verbose >= 0) || verbose > 0)
@@ -371,48 +422,72 @@ 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;
+       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];
                char *c;
-               if (!st || !st->sb) {
-                       return 2;
-               }
+               int rc;
+
                st->ss->getinfo_super(st, &info);
-               c = strchr(info.name, ':');
-               if (c) c++; else c= info.name;
+               if (uuid_for_name)
+                       c = fname_from_uuid(st, &info, nbuf, '-');
+               else {
+                       c = strchr(info.name, ':');
+                       if (c) c++; else c= info.name;
+               }
                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;
@@ -542,8 +617,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                            == devices[devcnt].i.events
                            && (devices[best[i]].i.disk.minor
                                != devices[devcnt].i.disk.minor)
-                           && st->ss->major == 0
-                           && info.array.level != -4) {
+                           && st->ss == &super0
+                           && info.array.level != LEVEL_MULTIPATH) {
                                /* two different devices with identical superblock.
                                 * Could be a mis-detection caused by overlapping
                                 * partitions.  fail-safe.
@@ -558,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
@@ -574,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;
        }
 
@@ -656,7 +731,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        continue;
                }
                tst = dup_super(st);
-               if (tst->ss->load_super(st,fd, NULL)) {
+               if (tst->ss->load_super(tst,fd, NULL)) {
                        close(fd);
                        fprintf(stderr, Name ": RAID superblock disappeared from %s - not updating.\n",
                                devices[chosen_drive].devname);
@@ -718,24 +793,27 @@ 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);
+#ifndef MDASSEMBLE
+       sysfs_init(&info, mdfd, 0);
+#endif
        for (i=0; i<bestcnt; i++) {
                int j = best[i];
                unsigned int desired_state;
@@ -790,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);
@@ -832,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;
                }
        }
@@ -845,25 +923,18 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
        /* Almost ready to actually *do* something */
        if (!old_linux) {
                int rv;
-               if ((vers % 100) >= 1) { /* can use different versions */
-                       mdu_array_info_t inf;
-                       memset(&inf, 0, sizeof(inf));
-                       inf.major_version = st->ss->major;
-                       inf.minor_version = st->minor_version;
-                       rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
-               } else
-                       rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
 
+               rv = set_array_info(mdfd, st, &info);
                if (rv) {
-                       fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
+                       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) {
@@ -872,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);
@@ -895,8 +966,9 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                j = chosen_drive;
 
                        if (j >= 0 /* && devices[j].uptodate */) {
-                               if (ioctl(mdfd, ADD_NEW_DISK,
-                                         &devices[j].i.disk)!=0) {
+                               rv = add_disk(mdfd, st, &info, &devices[j].i);
+
+                               if (rv) {
                                        fprintf(stderr, Name ": failed to add "
                                                        "%s to %s: %s\n",
                                                devices[j].devname,
@@ -918,6 +990,20 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                        i, mddev);
                }
 
+               if (info.array.level == LEVEL_CONTAINER) {
+                       if (verbose >= 0) {
+                               fprintf(stderr, Name ": Container %s has been "
+                                       "assembled with %d drive%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");
+                       }
+                       close(mdfd);
+                       return 0;
+               }
+
                if (runstop == 1 ||
                    (runstop <= 0 &&
                     ( enough(info.array.level, info.array.raid_disks,
@@ -934,13 +1020,14 @@ 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
-                                        * doesn't work and the capacity if wrong.
+                                        * doesn't work and the capacity is
+                                        * wrong.
                                         * If we reopen (to check for layered devices)
                                         * before --monitor closes, we loose.
                                         *
@@ -977,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) {
@@ -989,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) {
@@ -1014,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
@@ -1033,6 +1117,6 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                }
 
        }
-       if (must_close) close(mdfd);
+       close(mdfd);
        return 0;
 }