]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Assemble.c
Report uuid in --detail --brief for ddf and intel
[thirdparty/mdadm.git] / Assemble.c
index ebbe18c976210979c5dac4cefb6364beb39cd0c2..a73e101ed047e75c139ab48b84a70f0c15270df2 100644 (file)
@@ -117,12 +117,10 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
        int vers = 0; /* Keep gcc quite - it really is initialised */
        struct {
                char *devname;
-               unsigned int major, minor;
-               long long events;
-               int uptodate;
-               int state;
-               int raid_disk;
-               int disk_nr;
+               int uptodate; /* set once we decide that this device is as
+                              * recent as everything else in the array.
+                              */
+               struct mdinfo i;
        } *devices;
        int *best = NULL; /* indexed by raid_disk */
        unsigned int bestcnt = 0;
@@ -141,6 +139,9 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
        struct mdinfo info;
        char *avail;
        int nextspare = 0;
+       int uuid_for_name = 0;
+
+       memset(&info, 0, sizeof(info));
 
        if (get_linux_version() < 2004000)
                old_linux = 1;
@@ -296,15 +297,28 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                if (mdfd < 0) {
                        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.
@@ -317,7 +331,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                if (!tst || !tst->sb) {
                        fprintf(stderr, Name ": %s has no superblock - assembly aborted\n",
                                devname);
-                       st->ss->free_super(st);
+                       if (st)
+                               st->ss->free_super(st);
                        return 1;
                }
 
@@ -338,7 +353,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        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)
@@ -368,7 +384,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                tmpdev->used = 1;
 
        loop:
-               tst->ss->free_super(tst);
+               if (tst)
+                       tst->ss->free_super(tst);
        }
 
        if (mdfd < 0) {
@@ -378,12 +395,17 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                 */
                mdu_array_info_t inf;
                char *c;
-               if (!st->sb) {
+               char nbuf[64];
+               if (!st || !st->sb) {
                        return 2;
                }
                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);
@@ -482,7 +504,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                } else
 #endif
                {
-                       struct supertype *tst = dup_super(st);;
+                       struct supertype *tst = dup_super(st);
                        int dfd;
                        dfd = dev_open(devname, O_RDWR|O_EXCL);
 
@@ -500,23 +522,20 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        fprintf(stderr, Name ": %s is identified as a member of %s, slot %d.\n",
                                devname, mddev, info.disk.raid_disk);
                devices[devcnt].devname = devname;
-               devices[devcnt].major = major(stb.st_rdev);
-               devices[devcnt].minor = minor(stb.st_rdev);
-               devices[devcnt].events = info.events;
-               devices[devcnt].raid_disk = info.disk.raid_disk;
-               devices[devcnt].disk_nr = info.disk.number;
                devices[devcnt].uptodate = 0;
-               devices[devcnt].state = info.disk.state;
+               devices[devcnt].i = info;
+               devices[devcnt].i.disk.major = major(stb.st_rdev);
+               devices[devcnt].i.disk.minor = minor(stb.st_rdev);
                if (most_recent < devcnt) {
-                       if (devices[devcnt].events
-                           > devices[most_recent].events)
+                       if (devices[devcnt].i.events
+                           > devices[most_recent].i.events)
                                most_recent = devcnt;
                }
                if (info.array.level == -4)
                        /* with multipath, the raid_disk from the superblock is meaningless */
                        i = devcnt;
                else
-                       i = devices[devcnt].raid_disk;
+                       i = devices[devcnt].i.disk.raid_disk;
                if (i+1 == 0) {
                        if (nextspare < info.array.raid_disks)
                                nextspare = info.array.raid_disks;
@@ -541,10 +560,12 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                bestcnt = newbestcnt;
                        }
                        if (best[i] >=0 &&
-                           devices[best[i]].events == devices[devcnt].events &&
-                           devices[best[i]].minor != devices[devcnt].minor &&
-                           st->ss->major == 0 &&
-                           info.array.level != -4) {
+                           devices[best[i]].i.events
+                           == devices[devcnt].i.events
+                           && (devices[best[i]].i.disk.minor
+                               != devices[devcnt].i.disk.minor)
+                           && 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.
@@ -563,23 +584,25 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                return 1;
                        }
                        if (best[i] == -1
-                           || devices[best[i]].events < devices[devcnt].events)
+                           || (devices[best[i]].i.events
+                               < devices[devcnt].i.events))
                                best[i] = devcnt;
                }
                devcnt++;
        }
 
-       if (update && strcmp(update, "byteorder")==0)
-               st->minor_version = 90;
-
        if (devcnt == 0) {
                fprintf(stderr, Name ": no devices found for %s\n",
                        mddev);
-               st->ss->free_super(st);
+               if (st)
+                       st->ss->free_super(st);
                if (must_close) close(mdfd);
                return 1;
        }
 
+       if (update && strcmp(update, "byteorder")==0)
+               st->minor_version = 90;
+
        st->ss->getinfo_super(st, &info);
        clean = info.array.state & 1;
 
@@ -600,13 +623,14 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                 * as they don't make sense
                 */
                if (info.array.level != -4)
-                       if (!(devices[j].state & (1<<MD_DISK_SYNC))) {
-                               if (!(devices[j].state & (1<<MD_DISK_FAULTY)))
+                       if (!(devices[j].i.disk.state & (1<<MD_DISK_SYNC))) {
+                               if (!(devices[j].i.disk.state
+                                     & (1<<MD_DISK_FAULTY)))
                                        sparecnt++;
                                continue;
                        }
-               if (devices[j].events+event_margin >=
-                   devices[most_recent].events) {
+               if (devices[j].i.events+event_margin >=
+                   devices[most_recent].i.events) {
                        devices[j].uptodate = 1;
                        if (i < info.array.raid_disks) {
                                okcnt++;
@@ -630,36 +654,38 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        int j = best[i];
                        if (j>=0 &&
                            !devices[j].uptodate &&
-                           devices[j].events > 0 &&
+                           devices[j].i.events > 0 &&
                            (chosen_drive < 0 ||
-                            devices[j].events > devices[chosen_drive].events))
+                            devices[j].i.events
+                            > devices[chosen_drive].i.events))
                                chosen_drive = j;
                }
                if (chosen_drive < 0)
                        break;
-               current_events = devices[chosen_drive].events;
+               current_events = devices[chosen_drive].i.events;
        add_another:
                if (verbose >= 0)
                        fprintf(stderr, Name ": forcing event count in %s(%d) from %d upto %d\n",
-                               devices[chosen_drive].devname, devices[chosen_drive].raid_disk,
-                               (int)(devices[chosen_drive].events),
-                               (int)(devices[most_recent].events));
+                               devices[chosen_drive].devname,
+                               devices[chosen_drive].i.disk.raid_disk,
+                               (int)(devices[chosen_drive].i.events),
+                               (int)(devices[most_recent].i.events));
                fd = dev_open(devices[chosen_drive].devname, O_RDWR|O_EXCL);
                if (fd < 0) {
                        fprintf(stderr, Name ": Couldn't open %s for write - not updating\n",
                                devices[chosen_drive].devname);
-                       devices[chosen_drive].events = 0;
+                       devices[chosen_drive].i.events = 0;
                        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);
-                       devices[chosen_drive].events = 0;
+                       devices[chosen_drive].i.events = 0;
                        continue;
                }
-               info.events = devices[most_recent].events;
+               info.events = devices[most_recent].i.events;
                tst->ss->update_super(tst, &info, "force-one",
                                     devices[chosen_drive].devname, verbose,
                                     0, NULL);
@@ -668,12 +694,12 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        close(fd);
                        fprintf(stderr, Name ": Could not re-write superblock on %s\n",
                                devices[chosen_drive].devname);
-                       devices[chosen_drive].events = 0;
+                       devices[chosen_drive].i.events = 0;
                        tst->ss->free_super(tst);
                        continue;
                }
                close(fd);
-               devices[chosen_drive].events = devices[most_recent].events;
+               devices[chosen_drive].i.events = devices[most_recent].i.events;
                devices[chosen_drive].uptodate = 1;
                avail[chosen_drive] = 1;
                okcnt++;
@@ -686,8 +712,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        int j = best[i];
                        if (j >= 0 &&
                            !devices[j].uptodate &&
-                           devices[j].events > 0 &&
-                           devices[j].events == current_events) {
+                           devices[j].i.events > 0 &&
+                           devices[j].i.events == current_events) {
                                chosen_drive = j;
                                goto add_another;
                        }
@@ -732,6 +758,9 @@ int Assemble(struct supertype *st, char *mddev, int 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;
@@ -745,12 +774,10 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        continue;
                if (!devices[j].uptodate)
                        continue;
-               info.disk.number = devices[j].disk_nr;
-               info.disk.raid_disk = i;
-               info.disk.state = desired_state;
 
-               if (devices[j].uptodate &&
-                   st->ss->update_super(st, &info, "assemble", NULL,
+               devices[j].i.disk.state = desired_state;
+
+               if (st->ss->update_super(st, &devices[j].i, "assemble", NULL,
                                         verbose, 0, NULL)) {
                        if (force) {
                                if (verbose >= 0)
@@ -766,8 +793,7 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                        }
                }
 #if 0
-               if (!devices[j].uptodate &&
-                   !(super.disks[i].state & (1 << MD_DISK_FAULTY))) {
+               if (!(super.disks[i].i.disk.state & (1 << MD_DISK_FAULTY))) {
                        fprintf(stderr, Name ": devices %d of %s is not marked FAULTY in superblock, but cannot be found\n",
                                i, mddev);
                }
@@ -844,17 +870,10 @@ 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);
                        return 1;
@@ -894,27 +913,45 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                j = chosen_drive;
 
                        if (j >= 0 /* && devices[j].uptodate */) {
-                               mdu_disk_info_t disk;
-                               memset(&disk, 0, sizeof(disk));
-                               disk.major = devices[j].major;
-                               disk.minor = devices[j].minor;
-                               if (ioctl(mdfd, ADD_NEW_DISK, &disk)!=0) {
-                                       fprintf(stderr, Name ": failed to add %s to %s: %s\n",
+                               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,
                                                mddev,
                                                strerror(errno));
-                                       if (i < info.array.raid_disks || i == bestcnt)
+                                       if (i < info.array.raid_disks
+                                           || i == bestcnt)
                                                okcnt--;
                                        else
                                                sparecnt--;
                                } else if (verbose > 0)
-                                       fprintf(stderr, Name ": added %s to %s as %d\n",
-                                               devices[j].devname, mddev, devices[j].raid_disk);
+                                       fprintf(stderr, Name ": added %s "
+                                                       "to %s as %d\n",
+                                               devices[j].devname, mddev,
+                                               devices[j].i.disk.raid_disk);
                        } else if (verbose > 0 && i < info.array.raid_disks)
-                               fprintf(stderr, Name ": no uptodate device for slot %d of %s\n",
+                               fprintf(stderr, Name ": no uptodate device for "
+                                               "slot %d of %s\n",
                                        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, okcnt==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);
+                       return 0;
+               }
+
                if (runstop == 1 ||
                    (runstop <= 0 &&
                     ( enough(info.array.level, info.array.raid_disks,
@@ -937,7 +974,8 @@ int Assemble(struct supertype *st, char *mddev, int 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.
                                         *
@@ -974,7 +1012,10 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                        "start the array while not clean "
                                        "- consider --force.\n");
 
-                       if (must_close) close(mdfd);
+                       if (must_close) {
+                               ioctl(mdfd, STOP_ARRAY, NULL);
+                               close(mdfd);
+                       }
                        return 1;
                }
                if (runstop == -1) {
@@ -1008,7 +1049,10 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                fprintf(stderr, " (use --run to insist).\n");
                        }
                }
-               if (must_close) close(mdfd);
+               if (must_close) {
+                       ioctl(mdfd, STOP_ARRAY, NULL);
+                       close(mdfd);
+               }
                return 1;
        } else {
                /* The "chosen_drive" is a good choice, and if necessary, the superblock has
@@ -1016,8 +1060,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                 * so we can just start the array
                 */
                unsigned long dev;
-               dev = makedev(devices[chosen_drive].major,
-                           devices[chosen_drive].minor);
+               dev = makedev(devices[chosen_drive].i.disk.major,
+                           devices[chosen_drive].i.disk.minor);
                if (ioctl(mdfd, START_ARRAY, dev)) {
                    fprintf(stderr, Name ": Cannot start array: %s\n",
                            strerror(errno));