]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Create.c
Move calls to SET_ARRAY_INFO to common helper.
[thirdparty/mdadm.git] / Create.c
index 71871f68ee4d037dea28e4991fa6c14a2ad102c4..652bc1c761cbdd7e9a30839f33f29000e7d95a32 100644 (file)
--- a/Create.c
+++ b/Create.c
@@ -72,12 +72,12 @@ int Create(struct supertype *st, char *mddev, int mdfd,
        int rv;
        int bitmap_fd;
        int have_container = 0;
-       int container_fd;
+       int container_fd = -1;
        int need_mdmon = 0;
        unsigned long long bitmapsize;
-       struct mdinfo *sra;
        struct mdinfo info, *infos;
        int did_default = 0;
+       unsigned long safe_mode_delay = 0;
 
        int major_num = BITMAP_MAJOR_HI;
 
@@ -139,7 +139,7 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                int fd;
 
                memset(&inf, 0, sizeof(inf));
-               fd = open(devlist->devname, O_RDONLY, 0);
+               fd = open(devlist->devname, O_RDONLY);
                if (fd >= 0 &&
                    ioctl(fd, GET_ARRAY_INFO, &inf) == 0 &&
                    inf.raid_disks == 0) {
@@ -245,7 +245,7 @@ int Create(struct supertype *st, char *mddev, int mdfd,
        }
 
        if (st && ! st->ss->validate_geometry(st, level, layout, raiddisks,
-                                             chunk, size, NULL, NULL))
+                                             chunk, size, NULL, NULL, verbose>=0))
                return 1;
 
        /* now look at the subdevs */
@@ -281,7 +281,8 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                                st = superlist[i]->match_metadata_desc(name);
                                if (st && !st->ss->validate_geometry
                                                (st, level, layout, raiddisks,
-                                                chunk, size, dname, &freesize))
+                                                chunk, size, dname, &freesize,
+                                                verbose > 0))
                                        st = NULL;
                        }
 
@@ -298,7 +299,8 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                        if (!st->ss->validate_geometry(st, level, layout,
                                                       raiddisks,
                                                       chunk, size, dname,
-                                                      &freesize)) {
+                                                      &freesize,
+                                                      verbose > 0)) {
 
                                fprintf(stderr,
                                        Name ": %s is not suitable for "
@@ -331,7 +333,7 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                        minsize = freesize;
                }
                if (runstop != 1 || verbose >= 0) {
-                       int fd = open(dname, O_RDONLY, 0);
+                       int fd = open(dname, O_RDONLY);
                        if (fd <0 ) {
                                fprintf(stderr, Name ": Cannot open %s: %s\n",
                                        dname, strerror(errno));
@@ -360,7 +362,7 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                        if (!st->ss->validate_geometry(st, level, layout,
                                                       raiddisks,
                                                       chunk, minsize,
-                                                      NULL, NULL)) {
+                                                      NULL, NULL, 0)) {
                                fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
                                return 1;
                        }
@@ -446,11 +448,13 @@ int Create(struct supertype *st, char *mddev, int mdfd,
             ( level <= 0 )
             ||
             assume_clean
-               )
+               ) {
                info.array.state = 1; /* clean, but one+ drive will be missing*/
-       else
+               info.resync_start = ~0ULL;
+       } else {
                info.array.state = 0; /* not clean, but no errors */
-
+               info.resync_start = 0;
+       }
        if (level == 10) {
                /* for raid10, the bitmap size is the capacity of the array,
                 * which is array.size * raid_disks / ncopies;
@@ -487,7 +491,6 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                + info.array.failed_disks;
        info.array.layout = layout;
        info.array.chunk_size = chunk*1024;
-       info.array.major_version = 0; /* Flag to say "not zeroing superblock" */
 
        if (name == NULL || *name == 0) {
                /* base name on mddev */
@@ -517,11 +520,30 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                return 1;
 
        total_slots = info.array.nr_disks;
+       sysfs_init(&info, mdfd, 0);
        st->ss->getinfo_super(st, &info);
 
-       if (did_default)
-               fprintf(stderr, Name ": Defaulting to version"
-                       " %s metadata\n", info.text_version);
+       if (did_default && verbose >= 0) {
+               if (is_subarray(info.text_version)) {
+                       int dnum = devname2devnum(info.text_version+1);
+                       char *path;
+                       int mdp = get_mdp_major();
+                       struct mdinfo *mdi;
+                       if (dnum > 0)
+                               path = map_dev(MD_MAJOR, dnum, 1);
+                       else
+                               path = map_dev(mdp, (-1-dnum)<< 6, 1);
+
+                       mdi = sysfs_read(-1, dnum, GET_VERSION);
+
+                       fprintf(stderr, Name ": Creating array inside "
+                               "%s container %s\n", 
+                               mdi?mdi->text_version:"managed", path);
+                       sysfs_free(mdi);
+               } else
+                       fprintf(stderr, Name ": Defaulting to version"
+                               " %s metadata\n", info.text_version);
+       }
 
        if (bitmap_file && vers < 9003) {
                major_num = BITMAP_MAJOR_HOSTENDIAN;
@@ -546,60 +568,41 @@ int Create(struct supertype *st, char *mddev, int mdfd,
        }
 
 
-       sra = sysfs_read(mdfd, 0, 0);
+       sysfs_init(&info, mdfd, 0);
 
-       if (st->ss->external) {
-               char ver[100];
-               strcat(strcpy(ver, "external:"),
-                      info.text_version);
-               if (st->ss->external && st->subarray[0]) {
-                       /* member */
-
-                       /* When creating a member, we need to be careful
-                        * to negotiate with mdmon properly.
-                        * If it is already running, we cannot write to
-                        * the devices and must ask it to do that part.
-                        * If it isn't running, we write to the devices,
-                        * and then start it.
-                        * We hold an exclusive open on the container
-                        * device to make sure mdmon doesn't exit after
-                        * we checked that it is running.
-                        *
-                        * For now, fail if it is already running.
-                        */
-                       container_fd = open_dev_excl(st->container_dev);
-                       if (container_fd < 0) {
-                               fprintf(stderr, Name ": Cannot get exclusive "
-                                       "open on container - weird.\n");
-                               return 1;
-                       }
-                       if (mdmon_running(st->container_dev)) {
-                               fprintf(stderr, Name ": mdmon already running "
-                                       "for %s - sorry\n",
-                                       devnum2devname(st->container_dev));
-                               return 1;
-                       }
-                       need_mdmon = 1;
-               }
-               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");
+       if (st->ss->external && st->subarray[0]) {
+               /* member */
+
+               /* When creating a member, we need to be careful
+                * to negotiate with mdmon properly.
+                * If it is already running, we cannot write to
+                * the devices and must ask it to do that part.
+                * If it isn't running, we write to the devices,
+                * and then start it.
+                * We hold an exclusive open on the container
+                * device to make sure mdmon doesn't exit after
+                * we checked that it is running.
+                *
+                * For now, fail if it is already running.
+                */
+               container_fd = open_dev_excl(st->container_dev);
+               if (container_fd < 0) {
+                       fprintf(stderr, Name ": Cannot get exclusive "
+                               "open on container - weird.\n");
                        return 1;
                }
-               rv = sysfs_set_array(sra, &info);
-       } else  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);
+               if (mdmon_running(st->container_dev)) {
+                       if (verbose)
+                               fprintf(stderr, Name ": reusing mdmon "
+                                       "for %s.\n",
+                                       devnum2devname(st->container_dev));
+                       st->update_tail = &st->updates;
+               } else
+                       need_mdmon = 1;
+       }
+       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));
                return 1;
        }
@@ -663,9 +666,9 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                                        inf->disk.state |= (1<<MD_DISK_WRITEMOSTLY);
 
                                if (st->ss->external && st->subarray[0])
-                                       fd = open(dv->devname, O_RDWR, 0);
+                                       fd = open(dv->devname, O_RDWR);
                                else
-                                       fd = open(dv->devname, O_RDWR|O_EXCL,0);
+                                       fd = open(dv->devname, O_RDWR|O_EXCL);
 
                                if (fd < 0) {
                                        fprintf(stderr, Name ": failed to open %s "
@@ -681,6 +684,7 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                                st->ss->add_to_super(st, &inf->disk,
                                                     fd, dv->devname);
                                st->ss->getinfo_super(st, inf);
+                               safe_mode_delay = inf->safe_mode_delay;
 
                                /* getinfo_super might have lost these ... */
                                inf->disk.major = major(stb.st_rdev);
@@ -690,11 +694,7 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                                inf->errors = 0;
                                rv = 0;
 
-                               if (st->ss->external)
-                                       rv = sysfs_add_disk(sra, inf);
-                               else
-                                       rv = ioctl(mdfd, ADD_NEW_DISK,
-                                                  &inf->disk);
+                               rv = add_disk(mdfd, st, &info, inf);
 
                                if (rv) {
                                        fprintf(stderr,
@@ -708,8 +708,10 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                        }
                        if (dv == moved_disk && dnum != insert_point) break;
                }
-               if (pass == 1)
+               if (pass == 1) {
                        st->ss->write_init_super(st);
+                       flush_metadata_updates(st);
+               }
        }
        free(infos);
        st->ss->free_super(st);
@@ -724,15 +726,16 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                        case LEVEL_LINEAR:
                        case LEVEL_MULTIPATH:
                        case 0:
-                               sysfs_set_str(sra, NULL, "array_state",
+                               sysfs_set_str(&info, NULL, "array_state",
                                              "active");
                                need_mdmon = 0;
                                break;
                        default:
-                               sysfs_set_str(sra, NULL, "array_state",
+                               sysfs_set_str(&info, NULL, "array_state",
                                              "readonly");
                                break;
                        }
+                       sysfs_set_safemode(&info, safe_mode_delay);
                } else {
                        mdu_param_t param;
                        if (ioctl(mdfd, RUN_ARRAY, &param)) {
@@ -745,28 +748,10 @@ int Create(struct supertype *st, char *mddev, int mdfd,
                if (verbose >= 0)
                        fprintf(stderr, Name ": array %s started.\n", mddev);
                if (st->ss->external && st->subarray[0]) {
-                       if (need_mdmon) {
-                               int dn = st->container_dev;
-                               int i;
-                               switch(fork()) {
-                               case 0:
-                                       /* FIXME yuk. CLOSE_EXEC?? */
-                                       for (i=3; i < 100; i++)
-                                               close(i);
-                                       execl("./mdmon", "mdmon",
-                                             map_dev(dev2major(dn),
-                                                     dev2minor(dn),
-                                                     1), NULL);
-                                       exit(1);
-                               case -1: fprintf(stderr, Name ": cannot fork. "
-                                                "Array remains readonly\n");
-                                       return 1;
-                               default: ; /* parent - good */
-                               }
-                       } else
-                               signal_mdmon(st->container_dev);
-                       /* FIXME wait for mdmon to set array to read-auto */
-                       sleep(1);
+                       if (need_mdmon)
+                               start_mdmon(st->container_dev);
+
+                       ping_monitor(devnum2devname(st->container_dev));
                        close(container_fd);
                }
        } else {