]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Incremental.c
Use uuid as /dev name when assembling array of uncertain origin.
[thirdparty/mdadm.git] / Incremental.c
index 9b22220604fc463f9fbba2ebc7fb493ac7d41ac7..869ae8396802f81a729e3678d60de55362f38b03 100644 (file)
@@ -56,6 +56,7 @@ int Incremental(char *devname, int verbose, int runstop,
         * - Choose a free, high number.
         * - Use a partitioned device unless strong suggestion not to.
         *         e.g. auto=md
+        *   Don't choose partitioned for containers.
         * 5/ Find out if array already exists
         * 5a/ if it does not
         * - choose a name, from mdadm.conf or 'name' field in array.
@@ -67,6 +68,7 @@ int Incremental(char *devname, int verbose, int runstop,
         * - add the device
         * 6/ Make sure /var/run/mdadm.map contains this array.
         * 7/ Is there enough devices to possibly start the array?
+        *     For a container, this means running Incremental_container.
         * 7a/ if not, finish with success.
         * 7b/ if yes,
         * - read all metadata and arrange devices like -A does
@@ -74,7 +76,7 @@ int Incremental(char *devname, int verbose, int runstop,
         *   start the array (auto-readonly).
         */
        struct stat stb;
-       struct mdinfo info, info2;
+       struct mdinfo info;
        struct mddev_ident_s *array_list, *match;
        char chosen_name[1024];
        int rv;
@@ -83,14 +85,16 @@ int Incremental(char *devname, int verbose, int runstop,
        int dfd, mdfd;
        char *avail;
        int active_disks;
-
+       int uuid_for_name = 0;
+       char *name_to_use;
+       char nbuf[64];
 
        struct createinfo *ci = conf_get_create_info();
 
        if (autof == 0)
                autof = ci->autof;
 
-       /* 1/ Check if devices is permitted by mdadm.conf */
+       /* 1/ Check if device is permitted by mdadm.conf */
 
        if (!conf_test_dev(devname)) {
                if (verbose >= 0)
@@ -142,7 +146,7 @@ int Incremental(char *devname, int verbose, int runstop,
        }
        close (dfd);
 
-       if (st->ss->container_content) {
+       if (st->ss->container_content && st->loaded_container) {
                /* This is a pre-built container array, so we do something
                 * rather different.
                 */
@@ -150,6 +154,7 @@ int Incremental(char *devname, int verbose, int runstop,
                                             autof);
        }
 
+       memset(&info, 0, sizeof(info));
        st->ss->getinfo_super(st, &info);
        /* 3/ Check if there is a match in mdadm.conf */
 
@@ -214,12 +219,8 @@ int Incremental(char *devname, int verbose, int runstop,
        /* 3a/ if not, check for homehost match.  If no match, reject. */
        if (!match) {
                if (homehost == NULL ||
-                   st->ss->match_home(st, homehost) == 0) {
-                       if (verbose >= 0)
-                               fprintf(stderr, Name
-             ": not found in mdadm.conf and not identified by homehost.\n");
-                       return 2;
-               }
+                      st->ss->match_home(st, homehost) != 1)
+                       uuid_for_name = 1;
        }
        /* 4/ Determine device number. */
        /* - If in mdadm.conf with std name, use that */
@@ -229,22 +230,37 @@ int Incremental(char *devname, int verbose, int runstop,
        /* - Choose a free, high number. */
        /* - Use a partitioned device unless strong suggestion not to. */
        /*         e.g. auto=md */
+       mp = map_by_uuid(&map, info.uuid);
+
+       if (uuid_for_name && ! mp) {
+               name_to_use = fname_from_uuid(st, &info, nbuf);
+               if (verbose >= 0)
+                       fprintf(stderr, Name
+               ": not found in mdadm.conf and not identified by homehost"
+                               " - using uuid based name\n");
+       } else
+               name_to_use = info.name;
+
        if (match && is_standard(match->devname, &devnum))
                /* We have devnum now */;
-       else if ((mp = map_by_uuid(&map, info.uuid)) != NULL)
+       else if (mp != NULL)
                devnum = mp->devnum;
        else {
                /* Have to guess a bit. */
                int use_partitions = 1;
                char *np, *ep;
                char *nm, nbuf[1024];
+               struct stat stb2;
+
                if ((autof&7) == 3 || (autof&7) == 5)
                        use_partitions = 0;
-               np = strchr(info.name, ':');
+               if (st->ss->external)
+                       use_partitions = 0;
+               np = strchr(name_to_use, ':');
                if (np)
                        np++;
                else
-                       np = info.name;
+                       np = name_to_use;
                devnum = strtoul(np, &ep, 10);
                if (ep > np && *ep == 0) {
                        /* This is a number.  Let check that it is unused. */
@@ -259,14 +275,14 @@ int Incremental(char *devname, int verbose, int runstop,
                        sprintf(nbuf, "/dev/md/%s", np);
                        nm = nbuf;
                }
-               if (stat(nm, &stb) == 0 &&
-                   S_ISBLK(stb.st_mode) &&
-                   major(stb.st_rdev) == (use_partitions ?
-                                          get_mdp_major() : MD_MAJOR)) {
+               if (stat(nm, &stb2) == 0 &&
+                   S_ISBLK(stb2.st_mode) &&
+                   major(stb2.st_rdev) == (use_partitions ?
+                                           get_mdp_major() : MD_MAJOR)) {
                        if (use_partitions)
-                               devnum = minor(stb.st_rdev) >> MdpMinorShift;
+                               devnum = minor(stb2.st_rdev) >> MdpMinorShift;
                        else
-                               devnum = minor(stb.st_rdev);
+                               devnum = minor(stb2.st_rdev);
                        if (mddev_busy(use_partitions ? (-1-devnum) : devnum))
                                devnum = -1;
                }
@@ -283,42 +299,38 @@ int Incremental(char *devname, int verbose, int runstop,
                } else
                        devnum = use_partitions ? (-1-devnum) : devnum;
        }
-       mdfd = open_mddev_devnum(match ? match->devname : NULL,
+
+       mdfd = open_mddev_devnum(match ? match->devname : mp ? mp->path : NULL,
                                 devnum,
-                                info.name,
+                                name_to_use,
                                 chosen_name, autof >> 3);
        if (mdfd < 0) {
                fprintf(stderr, Name ": failed to open %s: %s.\n",
                        chosen_name, strerror(errno));
                return 2;
        }
+       sysfs_init(&info, mdfd, 0);
+
        /* 5/ Find out if array already exists */
        if (! mddev_busy(devnum)) {
        /* 5a/ if it does not */
        /* - choose a name, from mdadm.conf or 'name' field in array. */
        /* - create the array */
        /* - add the device */
-               mdu_array_info_t ainf;
-               mdu_disk_info_t disk;
                struct mdinfo *sra;
+               struct mdinfo dinfo;
 
-               memset(&ainf, 0, sizeof(ainf));
-               ainf.major_version = info.array.major_version;
-               ainf.minor_version = info.array.minor_version;
-               if (ioctl(mdfd, SET_ARRAY_INFO, &ainf) != 0) {
-                       fprintf(stderr, Name
-                               ": SET_ARRAY_INFO failed for %s: %s\b",
+               if (set_array_info(mdfd, st, &info) != 0) {
+                       fprintf(stderr, Name ": failed to set array info for %s: %s\n",
                                chosen_name, strerror(errno));
                        close(mdfd);
                        return 2;
                }
-               sra = sysfs_read(mdfd, devnum, GET_VERSION);
-               sysfs_set_str(sra, NULL, "metadata_version", info.text_version);
-               memset(&disk, 0, sizeof(disk));
-               disk.major = major(stb.st_rdev);
-               disk.minor = minor(stb.st_rdev);
-               sysfs_free(sra);
-               if (ioctl(mdfd, ADD_NEW_DISK, &disk) != 0) {
+
+               dinfo = info;
+               dinfo.disk.major = major(stb.st_rdev);
+               dinfo.disk.minor = minor(stb.st_rdev);
+               if (add_disk(mdfd, st, &info, &dinfo) != 0) {
                        fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
                                devname, chosen_name, strerror(errno));
                        ioctl(mdfd, STOP_ARRAY, 0);
@@ -339,6 +351,8 @@ int Incremental(char *devname, int verbose, int runstop,
                        sysfs_free(sra);
                        return 2;
                }
+               info.array.working_disks = 1;
+               sysfs_free(sra);
        } else {
        /* 5b/ if it does */
        /* - check one drive in array to make sure metadata is a reasonably */
@@ -346,10 +360,10 @@ int Incremental(char *devname, int verbose, int runstop,
        /* - add the device */
                char dn[20];
                int dfd2;
-               mdu_disk_info_t disk;
                int err;
                struct mdinfo *sra;
                struct supertype *st2;
+               struct mdinfo info2, *d;
                sra = sysfs_read(mdfd, devnum, (GET_DEVS | GET_STATE));
 
                sprintf(dn, "%d:%d", sra->devs->disk.major,
@@ -367,6 +381,7 @@ int Incremental(char *devname, int verbose, int runstop,
                        return 2;
                }
                close(dfd2);
+               memset(&info2, 0, sizeof(info2));
                st2->ss->getinfo_super(st2, &info2);
                st2->ss->free_super(st2);
                if (info.array.level != info2.array.level ||
@@ -378,17 +393,19 @@ int Incremental(char *devname, int verbose, int runstop,
                        close(mdfd);
                        return 2;
                }
-               memset(&disk, 0, sizeof(disk));
-               disk.major = major(stb.st_rdev);
-               disk.minor = minor(stb.st_rdev);
-               err = ioctl(mdfd, ADD_NEW_DISK, &disk);
+               info2.disk.major = major(stb.st_rdev);
+               info2.disk.minor = minor(stb.st_rdev);
+               /* add disk needs to know about containers */
+               if (st->ss->external)
+                       sra->array.level = LEVEL_CONTAINER;
+               err = add_disk(mdfd, st2, sra, &info2);
                if (err < 0 && errno == EBUSY) {
                        /* could be another device present with the same
                         * disk.number. Find and reject any such
                         */
                        find_reject(mdfd, st, sra, info.disk.number,
                                    info.events, verbose, chosen_name);
-                       err = ioctl(mdfd, ADD_NEW_DISK, &disk);
+                       err = add_disk(mdfd, st2, sra, &info2);
                }
                if (err < 0) {
                        fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
@@ -396,6 +413,10 @@ int Incremental(char *devname, int verbose, int runstop,
                        close(mdfd);
                        return 2;
                }
+               info.array.working_disks = 0;
+               for (d = sra->devs; d; d=d->next)
+                       info.array.working_disks ++;
+                       
        }
        /* 6/ Make sure /var/run/mdadm.map contains this array. */
        map_update(&map, devnum,
@@ -404,6 +425,16 @@ int Incremental(char *devname, int verbose, int runstop,
 
        /* 7/ Is there enough devices to possibly start the array? */
        /* 7a/ if not, finish with success. */
+       if (info.array.level == LEVEL_CONTAINER) {
+               /* Try to assemble within the container */
+               close(mdfd);
+               if (verbose >= 0)
+                       fprintf(stderr, Name
+                               ": container %s now has %d devices\n",
+                               chosen_name, info.array.working_disks);
+               return Incremental(chosen_name, verbose, runstop,
+                                  NULL, homehost, autof);
+       }
        avail = NULL;
        active_disks = count_active(st, mdfd, &avail, &info);
        if (enough(info.array.level, info.array.raid_disks,
@@ -752,60 +783,75 @@ int Incremental_container(struct supertype *st, char *devname, int verbose,
        }
 
        for (ra = list ; ra ; ra = ra->next) {
-               struct mdinfo *sra;
                struct mdinfo *dev;
                int devnum = -1;
                int mdfd;
                char chosen_name[1024];
                int usepart = 1;
                char *n;
-               int working = 0;
-               char ver[100];
+               int working = 0, preexist = 0;
+               struct map_ent *mp, *map = NULL;
+               char nbuf[64];
+               char *name_to_use;
 
                if ((autof&7) == 3 || (autof&7) == 5)
                        usepart = 0;
 
-               n = ra->name;
-               if (*n == 'd')
-                       n++;
-               if (*n) {
-                       devnum = strtoul(n, &n, 10);
-                       if (devnum >= 0 && (*n == 0 || *n == ' ')) {
-                               /* Use this devnum */
-                               usepart = (ra->name[0] == 'd');
-                               if (mddev_busy(usepart ? (-1-devnum) : devnum))
-                                       devnum = -1;
-                       } else
-                               devnum = -1;
-               }
+               mp = map_by_uuid(&map, ra->uuid);
+
+               name_to_use = ra->name;
+               if (! name_to_use ||
+                   ! *name_to_use ||
+                   (*devname != '/' || strncmp("UUID-", strrchr(devname,'/')+1,5) == 0)
+                       )
+                       name_to_use = fname_from_uuid(st, ra, nbuf);
+                   
+               if (mp)
+                       devnum = mp->devnum;
+               else {
 
-               if (devnum < 0) {
-                       char *nm = ra->name;
-                       char nbuf[1024];
-                       struct stat stb;
-                       if (strchr(nm, ':'))
-                               nm = strchr(nm, ':')+1;
-                       sprintf(nbuf, "/dev/md/%s", nm);
-
-                       if (stat(nbuf, &stb) == 0 &&
-                           S_ISBLK(stb.st_mode) &&
-                           major(stb.st_rdev) == (usepart ?
-                                                  get_mdp_major() : MD_MAJOR)){
-                               if (usepart)
-                                       devnum = minor(stb.st_rdev)
-                                               >> MdpMinorShift;
-                               else
-                                       devnum = minor(stb.st_rdev);
-                               if (mddev_busy(usepart ? (-1-devnum) : devnum))
+                       n = name_to_use;
+                       if (*n == 'd')
+                               n++;
+                       if (*n && devnum < 0) {
+                               devnum = strtoul(n, &n, 10);
+                               if (devnum >= 0 && (*n == 0 || *n == ' ')) {
+                                       /* Use this devnum */
+                                       usepart = (name_to_use[0] == 'd');
+                                       if (mddev_busy(usepart ? (-1-devnum) : devnum))
+                                               devnum = -1;
+                               } else
                                        devnum = -1;
                        }
-               }
 
-               if (devnum >= 0)
-                       devnum = usepart ? (-1-devnum) : devnum;
-               else
-                       devnum = find_free_devnum(usepart);
-               mdfd = open_mddev_devnum(NULL, devnum, ra->name,
+                       if (devnum < 0) {
+                               char *nm = name_to_use;
+                               char nbuf[1024];
+                               struct stat stb;
+                               if (strchr(nm, ':'))
+                                       nm = strchr(nm, ':')+1;
+                               sprintf(nbuf, "/dev/md/%s", nm);
+
+                               if (stat(nbuf, &stb) == 0 &&
+                                   S_ISBLK(stb.st_mode) &&
+                                   major(stb.st_rdev) == (usepart ?
+                                                          get_mdp_major() : MD_MAJOR)){
+                                       if (usepart)
+                                               devnum = minor(stb.st_rdev)
+                                                       >> MdpMinorShift;
+                                       else
+                                               devnum = minor(stb.st_rdev);
+                                       if (mddev_busy(usepart ? (-1-devnum) : devnum))
+                                               devnum = -1;
+                               }
+                       }
+
+                       if (devnum >= 0)
+                               devnum = usepart ? (-1-devnum) : devnum;
+                       else
+                               devnum = find_free_devnum(usepart);
+               }
+               mdfd = open_mddev_devnum(mp ? mp->path : NULL, devnum, name_to_use,
                                         chosen_name, autof>>3);
 
                if (mdfd < 0) {
@@ -814,26 +860,26 @@ int Incremental_container(struct supertype *st, char *devname, int verbose,
                        return 2;
                }
 
-               sra = sysfs_read(mdfd, 0, 0);
-
-               sprintf(ver, "external:%s", ra->text_version);
-               sysfs_set_str(sra, NULL, "metadata_version", ver);
-
-               sysfs_set_array(sra, ra);
+               sysfs_init(ra, mdfd, 0);
+               sysfs_set_array(ra, md_get_version(mdfd));
                for (dev = ra->devs; dev; dev = dev->next)
-                       if (sysfs_add_disk(sra, dev) == 0)
+                       if (sysfs_add_disk(ra, dev) == 0)
                                working++;
-
-               if (runstop > 0 || working >= ra->array.working_disks) {
+                       else if (errno == EEXIST)
+                               preexist++;
+               if (working == 0)
+                       /* Nothing new, don't try to start */ ;
+               else if (runstop > 0 ||
+                        (working + preexist) >= ra->array.working_disks) {
                        switch(ra->array.level) {
                        case LEVEL_LINEAR:
                        case LEVEL_MULTIPATH:
                        case 0:
-                               sysfs_set_str(sra, NULL, "array_state",
+                               sysfs_set_str(ra, NULL, "array_state",
                                              "active");
                                break;
                        default:
-                               sysfs_set_str(sra, NULL, "array_state",
+                               sysfs_set_str(ra, NULL, "array_state",
                                              "readonly");
                                /* start mdmon if needed. */
                                if (!mdmon_running(st->container_dev))
@@ -841,16 +887,26 @@ int Incremental_container(struct supertype *st, char *devname, int verbose,
                                ping_monitor(devnum2devname(st->container_dev));
                                break;
                        }
-                       if (verbose >= 0)
-                               printf("Started %s with %d devices\n",
-                                      chosen_name, working);
+                       sysfs_set_safemode(ra, ra->safe_mode_delay);
+                       if (verbose >= 0) {
+                               fprintf(stderr, Name
+                                       ": Started %s with %d devices",
+                                       chosen_name, working + preexist);
+                               if (preexist)
+                                       fprintf(stderr, " (%d new)", working);
+                               fprintf(stderr, "\n");
+                       }
                        /* FIXME should have an O_EXCL and wait for read-auto */
                } else
                        if (verbose >= 0)
-                               printf("%s assembled with %d devices but "
-                                      "not started\n",
-                                      chosen_name, working);
+                               fprintf(stderr, Name
+                                       ": %s assembled with %d devices but "
+                                       "not started\n",
+                                       chosen_name, working);
                close(mdfd);
+               map_update(&map, devnum,
+                          ra->text_version,
+                          ra->uuid, chosen_name);
        }
        return 0;
 }