]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Assemble.c
Generate 'change' uevents when arrays change in non-obvious ways.
[thirdparty/mdadm.git] / Assemble.c
index 7efa2b8c8bfc4d0526e819f49b207266b9e49f79..f801a73e19c53572941d82604ec1b248bae98a9a 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,23 @@ 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;
+       char *name;
+       int trustworthy;
+       char chosen_name[1024];
+
+       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 +167,63 @@ 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,
+                                            LOCAL);
+       }
+#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 +243,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,19 +331,9 @@ 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) {
-                               if ((inargv && verbose >= 0) || verbose > 0)
-                                       fprintf(stderr, Name ": %s is not built for host %s.\n",
-                                               devname, homehost);
-                               /* Auto-assemble, and this is not a usable host */
-                               /* if update != NULL, we are updating the host
-                                * name... */
-                               goto loop;
-                       }
                }
                /* If we are this far, then we are nearly commited to this device.
                 * If the super_block doesn't exist, or doesn't match others,
@@ -332,12 +362,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 +402,70 @@ 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;
-               }
-               st->ss->getinfo_super(st, &info);
-               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);
+       if (!st || !st->sb)
+               return 2;
+
+       /* Now need to open array the device.  Use create_mddev */
+       st->ss->getinfo_super(st, &info);
+
+       trustworthy = FOREIGN;
+       switch (st->ss->match_home(st, homehost)) {
+       case 0:
+               trustworthy = FOREIGN;
+               name = info.name;
+               break;
+       case 1:
+               trustworthy = LOCAL;
+               name = strchr(info.name, ':');
+               if (name)
+                       name++;
                else
-                       asprintf(&mddev, "/dev/md/%s", c);
-               mdfd = open_mddev(mddev, ident->autof);
-               if (mdfd < 0) {
-                       st->ss->free_super(st);
-                       free(devices);
+                       name = info.name;
+               break;
+       case -1:
+               if (info.name[0] == 0 && info.array.level == LEVEL_CONTAINER) {
+                       name = info.text_version;
+                       trustworthy = METADATA;
+               } else
+                       trustworthy = FOREIGN;
+               break;
+       }
+       mdfd = create_mddev(mddev, name, ident->autof, trustworthy,
+                           chosen_name);
+       if (mdfd < 0) {
+               st->ss->free_super(st);
+               free(devices);
+               if (auto_assem)
                        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);
+               return 1;
+       }
+       mddev = chosen_name;
+       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;
-               }
-               must_close = 1;
+               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;
@@ -558,7 +611,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 +627,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;
        }
 
@@ -718,24 +771,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 +846,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 +888,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;
                }
        }
@@ -846,42 +902,23 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
        if (!old_linux) {
                int rv;
 
-#ifndef MDASSEMBLE
-               struct mdinfo *sra;
-               if (st->ss->external) {
-                       char ver[100];
-                       strcat(strcpy(ver, "external:"), info.text_version);
-                       sra = sysfs_read(mdfd, 0, 0);
-                       if ((vers % 100) < 2 ||
-                           sra == NULL ||
-                           sysfs_set_str(sra, NULL, "metadata_version",
-                                         ver) < 0) {
-                               fprintf(stderr, Name ": This kernel does not "
-                                       "support external metadata.\n");
-                               return 1;
-                       }
-                       rv = sysfs_set_array(sra, &info);
-               } else
-#endif
-               if ((vers % 100) >= 1) { /* can use different versions */
-                       mdu_array_info_t inf;
-                       memset(&inf, 0, sizeof(inf));
-                       inf.major_version = info.array.major_version;
-                       inf.minor_version = info.array.minor_version;
-                       rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
-               } else
-                       rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
+               /* First, fill in the map, so that udev can find our name
+                * as soon as we become active.
+                */
+               map_update(NULL, fd2devnum(mdfd), info.text_version,
+                          info.uuid, chosen_name);
 
+               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) {
@@ -890,13 +927,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);
@@ -913,13 +950,8 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                                j = chosen_drive;
 
                        if (j >= 0 /* && devices[j].uptodate */) {
-#ifndef MDASSEMBLE
-                               if (st->ss->external)
-                                       rv = sysfs_add_disk(sra, &devices[j].i);
-                               else
-#endif
-                                       rv = ioctl(mdfd, ADD_NEW_DISK,
-                                         &devices[j].i.disk);
+                               rv = add_disk(mdfd, st, &info, &devices[j].i);
+
                                if (rv) {
                                        fprintf(stderr, Name ": failed to add "
                                                        "%s to %s: %s\n",
@@ -946,14 +978,14 @@ 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);
+                       sysfs_uevent(&info, "change");
+                       close(mdfd);
                        return 0;
                }
 
@@ -973,9 +1005,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
@@ -1017,10 +1049,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) {
@@ -1029,7 +1060,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) {
@@ -1054,10 +1085,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
@@ -1073,6 +1102,6 @@ int Assemble(struct supertype *st, char *mddev, int mdfd,
                }
 
        }
-       if (must_close) close(mdfd);
+       close(mdfd);
        return 0;
 }