]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Incremental.c
Can now mostly assemble DDF arrays
[thirdparty/mdadm.git] / Incremental.c
index d3ab72f781160f2bc0bd273f36c4ba480adc8263..2e23677dc88820c4229b0ee561ce9f385a18c051 100644 (file)
@@ -32,7 +32,7 @@
 
 static int count_active(struct supertype *st, int mdfd, char **availp,
                        struct mdinfo *info);
-static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra,
+static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
                        int number, __u64 events, int verbose,
                        char *array_name);
 
@@ -40,7 +40,7 @@ int Incremental(char *devname, int verbose, int runstop,
                struct supertype *st, char *homehost, int autof)
 {
        /* Add this device to an array, creating the array if necessary
-        * and starting the array if sensibe or - if runstop>0 - if possible.
+        * and starting the array if sensible or - if runstop>0 - if possible.
         *
         * This has several steps:
         *
@@ -140,9 +140,17 @@ int Incremental(char *devname, int verbose, int runstop,
                close(dfd);
                return 1;
        }
-       st->ss->getinfo_super(st, &info);
        close (dfd);
 
+       if (st->ss->container_content) {
+               /* This is a pre-built container array, so we do something
+                * rather different.
+                */
+               return Incremental_container(st, devname, verbose, runstop,
+                                            autof);
+       }
+
+       st->ss->getinfo_super(st, &info);
        /* 3/ Check if there is a match in mdadm.conf */
 
        array_list = conf_get_ident(NULL);
@@ -246,25 +254,20 @@ int Incremental(char *devname, int verbose, int runstop,
 
                if (devnum < 0) {
                        /* Haven't found anything yet, choose something free */
-                       /* There is similar code in mdopen.c - should unify */
-                       for (devnum = 127 ; devnum != 128 ;
-                            devnum = devnum ? devnum-1 : (1<<22)-1) {
-                               if (mddev_busy(use_partitions ?
-                                              (-1-devnum) : devnum))
-                                       break;
-                       }
-                       if (devnum == 128) {
+                       devnum = find_free_devnum(use_partitions);
+
+                       if (devnum == NoMdDev) {
                                fprintf(stderr, Name
                                        ": No spare md devices!!\n");
                                return 2;
                        }
-               }
-               devnum = use_partitions ? (-1-devnum) : devnum;
+               } else
+                       devnum = use_partitions ? (-1-devnum) : devnum;
        }
        mdfd = open_mddev_devnum(match ? match->devname : NULL,
                                 devnum,
                                 info.name,
-                                chosen_name);
+                                chosen_name, autof >> 3);
        if (mdfd < 0) {
                fprintf(stderr, Name ": failed to open %s: %s.\n",
                        chosen_name, strerror(errno));
@@ -279,7 +282,7 @@ int Incremental(char *devname, int verbose, int runstop,
                mdu_array_info_t ainf;
                mdu_disk_info_t disk;
                char md[20];
-               struct sysarray *sra;
+               struct mdinfo *sra;
 
                memset(&ainf, 0, sizeof(ainf));
                ainf.major_version = st->ss->major;
@@ -306,7 +309,7 @@ int Incremental(char *devname, int verbose, int runstop,
                        return 2;
                }
                sra = sysfs_read(mdfd, devnum, GET_DEVS);
-               if (!sra || !sra->devs || sra->devs->role >= 0) {
+               if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
                        /* It really should be 'none' - must be old buggy
                         * kernel, and mdadm -I may not be able to complete.
                         * So reject it.
@@ -328,22 +331,25 @@ int Incremental(char *devname, int verbose, int runstop,
                int dfd2;
                mdu_disk_info_t disk;
                int err;
-               struct sysarray *sra;
+               struct mdinfo *sra;
                struct supertype *st2;
                sra = sysfs_read(mdfd, devnum, (GET_VERSION | GET_DEVS |
                                                GET_STATE));
-               if (sra->major_version != st->ss->major ||
-                   sra->minor_version != st->minor_version) {
+
+               if (sra->array.major_version != st->ss->major ||
+                   sra->array.minor_version != st->minor_version) {
                        if (verbose >= 0)
                                fprintf(stderr, Name
              ": %s has different metadata to chosen array %s %d.%d %d.%d.\n",
                                        devname, chosen_name,
-                                       sra->major_version, sra->minor_version,
+                                       sra->array.major_version,
+                                       sra->array.minor_version,
                                        st->ss->major, st->minor_version);
                        close(mdfd);
                        return 1;
                }
-               sprintf(dn, "%d:%d", sra->devs->major, sra->devs->minor);
+               sprintf(dn, "%d:%d", sra->devs->disk.major,
+                       sra->devs->disk.minor);
                dfd2 = dev_open(dn, O_RDONLY);
                st2 = dup_super(st);
                if (st2->ss->load_super(st2, dfd2, NULL)) {
@@ -426,7 +432,7 @@ int Incremental(char *devname, int verbose, int runstop,
        }
 }
        if (runstop > 0 || active_disks >= info.array.working_disks) {
-               struct sysarray *sra;
+               struct mdinfo *sra;
                /* Let's try to start it */
                if (match && match->bitmap_file) {
                        int bmfd = open(match->bitmap_file, O_RDWR);
@@ -476,14 +482,14 @@ int Incremental(char *devname, int verbose, int runstop,
        return rv;
 }
 
-static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra,
+static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
                        int number, __u64 events, int verbose,
                        char *array_name)
 {
        /* Find a device attached to this array with a disk.number of number
         * and events less than the passed events, and remove the device.
         */
-       struct sysdev *d;
+       struct mdinfo *d;
        mdu_array_info_t ra;
 
        if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
@@ -494,7 +500,7 @@ static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra,
                char dn[10];
                int dfd;
                struct mdinfo info;
-               sprintf(dn, "%d:%d", d->major, d->minor);
+               sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
                dfd = dev_open(dn, O_RDONLY);
                if (dfd < 0)
                        continue;
@@ -510,13 +516,13 @@ static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra,
                    info.events >= events)
                        continue;
 
-               if (d->role > -1)
+               if (d->disk.raid_disk > -1)
                        sysfs_set_str(sra, d, "slot", "none");
                if (sysfs_set_str(sra, d, "state", "remove") == 0)
                        if (verbose >= 0)
                                fprintf(stderr, Name
                                        ": removing old device %s from %s\n",
-                                       d->name+4, array_name);
+                                       d->sys_name+4, array_name);
        }
 }
 
@@ -524,10 +530,10 @@ static int count_active(struct supertype *st, int mdfd, char **availp,
                        struct mdinfo *bestinfo)
 {
        /* count how many devices in sra think they are active */
-       struct sysdev *d;
+       struct mdinfo *d;
        int cnt = 0, cnt1 = 0;
        __u64 max_events = 0;
-       struct sysarray *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
+       struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
        char *avail = NULL;
 
        for (d = sra->devs ; d ; d = d->next) {
@@ -536,7 +542,7 @@ static int count_active(struct supertype *st, int mdfd, char **availp,
                int ok;
                struct mdinfo info;
 
-               sprintf(dn, "%d:%d", d->major, d->minor);
+               sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
                dfd = dev_open(dn, O_RDONLY);
                if (dfd < 0)
                        continue;
@@ -594,8 +600,8 @@ void RebuildMap(void)
        int mdp = get_mdp_major();
 
        for (md = mdstat ; md ; md = md->next) {
-               struct sysarray *sra = sysfs_read(-1, md->devnum, GET_DEVS);
-               struct sysdev *sd;
+               struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
+               struct mdinfo *sd;
 
                for (sd = sra->devs ; sd ; sd = sd->next) {
                        char dn[30];
@@ -605,7 +611,7 @@ void RebuildMap(void)
                        char *path;
                        struct mdinfo info;
 
-                       sprintf(dn, "%d:%d", sd->major, sd->minor);
+                       sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
                        dfd = dev_open(dn, O_RDONLY);
                        if (dfd < 0)
                                continue;
@@ -653,8 +659,9 @@ int IncrementalScan(int verbose)
                char path[1024];
                mdu_array_info_t array;
                mdu_bitmap_file_t bmf;
-               struct sysarray *sra;
-               int mdfd = open_mddev_devnum(me->path, me->devnum, NULL, path);
+               struct mdinfo *sra;
+               int mdfd = open_mddev_devnum(me->path, me->devnum,
+                                            NULL, path, 0);
                if (mdfd < 0)
                        continue;
                if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
@@ -709,3 +716,90 @@ int IncrementalScan(int verbose)
        }
        return rv;
 }
+
+int Incremental_container(struct supertype *st, char *devname, int verbose,
+                         int runstop, int autof)
+{
+       /* Collect the contents of this container and for each
+        * array, choose a device name and assemble the array.
+        */
+
+       struct mdinfo *list = st->ss->container_content(st);
+       struct mdinfo *ra;
+
+       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;
+
+               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;
+               }
+
+               if (devnum >= 0)
+                       devnum = usepart ? (-1-devnum) : devnum;
+               else
+                       devnum = find_free_devnum(usepart);
+               mdfd = open_mddev_devnum(NULL, devnum, ra->name,
+                                        chosen_name, autof>>3);
+
+               if (mdfd < 0) {
+                       fprintf(stderr, Name ": failed to open %s: %s.\n",
+                               chosen_name, strerror(errno));
+                       return 2;
+               }
+
+               sra = sysfs_read(mdfd, 0, 0);
+
+               sysfs_set_array(sra, ra);
+               for (dev = ra->devs; dev; dev = dev->next) {
+                       char buf[20];
+                       int dfd;
+                       sprintf(buf, "%d:%d", dev->disk.major, dev->disk.minor);
+                       dfd = dev_open(buf, O_RDONLY);
+                       if (sysfs_add_disk(sra, dfd, dev) == 0)
+                               working++;
+               }
+               if (runstop > 0 || working >= ra->array.working_disks) {
+                       switch(ra->array.level) {
+                       case LEVEL_LINEAR:
+                       case LEVEL_MULTIPATH:
+                       case 0:
+                               sysfs_set_str(sra, NULL, "array_state",
+                                             "active");
+                               break;
+                       default:
+                               sysfs_set_str(sra, NULL, "array_state",
+                                             "readonly");
+                               break;
+                       }
+                       if (verbose >= 0)
+                               printf("Started %s with %d devices\n",
+                                      chosen_name, working);
+               } else
+                       if (verbose >= 0)
+                               printf("%s assembled with %d devices but "
+                                      "not started\n",
+                                      chosen_name, working);
+               close(mdfd);
+       }
+       return 0;
+}