]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Incremental.c
super0: fix overflow when checking max size.
[thirdparty/mdadm.git] / Incremental.c
index 7a4906dd5a2458834cc0636fe60314b95fdab5e8..b90089bb39210d960b27a664dcbaa84623a6b05d 100644 (file)
@@ -44,7 +44,8 @@ static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
 
 static int Incremental_container(struct supertype *st, char *devname,
                                 char *homehost,
-                                int verbose, int runstop, int autof);
+                                int verbose, int runstop, int autof,
+                                int freeze_reshape);
 
 static struct mddev_ident *search_mdstat(struct supertype *st,
                                           struct mdinfo *info,
@@ -53,7 +54,7 @@ static struct mddev_ident *search_mdstat(struct supertype *st,
 
 int Incremental(char *devname, int verbose, int runstop,
                struct supertype *st, char *homehost, int require_homehost,
-               int autof)
+               int autof, int freeze_reshape)
 {
        /* Add this device to an array, creating the array if necessary
         * and starting the array if sensible or - if runstop>0 - if possible.
@@ -140,7 +141,8 @@ int Incremental(char *devname, int verbose, int runstop,
                close(dfd);
                if (!rv && st->ss->container_content)
                        return Incremental_container(st, devname, homehost,
-                                                    verbose, runstop, autof);
+                                                    verbose, runstop, autof,
+                                                    freeze_reshape);
 
                fprintf(stderr, Name ": %s is not part of an md array.\n",
                        devname);
@@ -202,7 +204,6 @@ int Incremental(char *devname, int verbose, int runstop,
        }
        close (dfd); dfd = -1;
 
-       memset(&info, 0, sizeof(info));
        st->ss->getinfo_super(st, &info, NULL);
 
        /* 3/ Check if there is a match in mdadm.conf */
@@ -358,22 +359,17 @@ int Incremental(char *devname, int verbose, int runstop,
                 * array was possibly started early and our best bet is
                 * to add this anyway.
                 * Also if action policy is re-add or better we allow
-                * re-add
+                * re-add.
+                * This doesn't apply to containers as the 'non-spare'
+                * flag has a different meaning.  The test has to happen
+                * at the device level there
                 */
-               if ((info.disk.state & (1<<MD_DISK_SYNC)) != 0
+               if (!st->ss->external
+                   && (info.disk.state & (1<<MD_DISK_SYNC)) != 0
                    && ! policy_action_allows(policy, st->ss->name,
                                              act_re_add)
                    && runstop < 1) {
-                       int active = 0;
-                       
-                       if (st->ss->external) {
-                               char *devname = devnum2devname(fd2devnum(mdfd));
-
-                               active = devname && is_container_active(devname);
-                               free(devname);
-                       } else if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0)
-                               active = 1;
-                       if (active) {
+                       if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
                                fprintf(stderr, Name
                                        ": not adding %s to active array (without --run) %s\n",
                                        devname, chosen_name);
@@ -401,7 +397,6 @@ int Incremental(char *devname, int verbose, int runstop,
                                goto out;
                        }
                        close(dfd2);
-                       memset(&info2, 0, sizeof(info2));
                        st2->ss->getinfo_super(st2, &info2, NULL);
                        st2->ss->free_super(st2);
                        if (info.array.level != info2.array.level ||
@@ -443,7 +438,7 @@ 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) {
-               char *devname = NULL;
+               int devnum = devnum; /* defined and used iff ->external */
                /* Try to assemble within the container */
                map_unlock(&map);
                sysfs_uevent(&info, "change");
@@ -453,11 +448,12 @@ int Incremental(char *devname, int verbose, int runstop,
                                chosen_name, info.array.working_disks);
                wait_for(chosen_name, mdfd);
                if (st->ss->external)
-                       devname = devnum2devname(fd2devnum(mdfd));
+                       devnum = fd2devnum(mdfd);
                close(mdfd);
                sysfs_free(sra);
                rv = Incremental(chosen_name, verbose, runstop,
-                                NULL, homehost, require_homehost, autof);
+                                NULL, homehost, require_homehost, autof,
+                                freeze_reshape);
                if (rv == 1)
                        /* Don't fail the whole -I if a subarray didn't
                         * have enough devices to start yet
@@ -465,10 +461,8 @@ int Incremental(char *devname, int verbose, int runstop,
                        rv = 0;
                /* after spare is added, ping monitor for external metadata
                 * so that it can eg. try to rebuild degraded array */
-               if (st->ss->external) {
-                       ping_monitor(devname);
-                       free(devname);
-               }
+               if (st->ss->external)
+                       ping_monitor_by_id(devnum);
                return rv;
        }
 
@@ -714,7 +708,7 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
        int cnt = 0;
        __u64 max_events = 0;
        char *avail = NULL;
-       int *best;
+       int *best = NULL;
        char *devmap = NULL;
        int numdevs = 0;
        int devnum;
@@ -785,9 +779,10 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
                                best[info.disk.raid_disk] = devnum;
                                st->ss->getinfo_super(st, bestinfo, NULL);
                        } else { /* info.events much bigger */
-                               memset(avail, 0, info.disk.raid_disk);
+                               memset(avail, 0, raid_disks);
                                max_events = info.events;
                                avail[info.disk.raid_disk] = 2;
+                               best[info.disk.raid_disk] = devnum;
                                st->ss->getinfo_super(st, bestinfo, NULL);
                        }
                }
@@ -888,7 +883,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                struct domainlist *dl = NULL;
                struct mdinfo *sra;
                unsigned long long devsize;
-               unsigned long long component_size;
+               unsigned long long component_size = 0;
 
                if (is_subarray(mp->metadata))
                        continue;
@@ -1000,7 +995,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                }
 
                dl = domain_from_array(sra, st2->ss->name);
-               if (!domain_test(dl, pol, st2->ss->name)) {
+               if (domain_test(dl, pol, st2->ss->name) != 1) {
                        /* domain test fails */
                        if (verbose > 1)
                                fprintf(stderr, Name ": not adding %s to %s as"
@@ -1043,7 +1038,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                        close(dfd);
                        *dfdp = -1;
                        rv =  Manage_subdevs(chosen->sys_name, mdfd, &devlist,
-                                            -1, 0, NULL);
+                                            -1, 0, NULL, 0);
                        close(mdfd);
                }
                if (verbose > 0) {
@@ -1082,7 +1077,7 @@ static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
        DIR *dir;
        struct dirent *de;
        char *chosen = NULL;
-       unsigned long long chosen_size;
+       unsigned long long chosen_size = 0;
        struct supertype *chosen_st = NULL;
        int fd;
 
@@ -1116,14 +1111,17 @@ static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                pol2 = path_policy(de->d_name, type_disk);
 
                domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
-               if (domain_test(domlist, pol, st ? st->ss->name : NULL) == 0)
+               if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
                        /* new device is incompatible with this device. */
                        goto next;
 
                domain_free(domlist);
                domlist = NULL;
 
-               asprintf(&devname, "/dev/disk/by-path/%s", de->d_name);
+               if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
+                       devname = NULL;
+                       goto next;
+               }
                fd = open(devname, O_RDONLY);
                if (fd < 0)
                        goto next;
@@ -1142,7 +1140,7 @@ static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                if (!st) {
                        /* Check domain policy again, this time referring to metadata */
                        domain_merge(&domlist, pol2, st2->ss->name);
-                       if (domain_test(domlist, pol, st2->ss->name) == 0)
+                       if (domain_test(domlist, pol, st2->ss->name) != 1)
                                /* Incompatible devices for this metadata type */
                                goto next;
                        if (!policy_action_allows(pol, st2->ss->name, act_spare))
@@ -1421,7 +1419,7 @@ static char *container2devname(char *devname)
 
 static int Incremental_container(struct supertype *st, char *devname,
                                 char *homehost, int verbose,
-                                int runstop, int autof)
+                                int runstop, int autof, int freeze_reshape)
 {
        /* Collect the contents of this container and for each
         * array, choose a device name and assemble the array.
@@ -1439,7 +1437,6 @@ static int Incremental_container(struct supertype *st, char *devname,
        int suuid[4];
        int sfd;
 
-       memset(&info, 0, sizeof(info));
        st->ss->getinfo_super(st, &info, NULL);
 
        if ((runstop > 0 && info.container_enough >= 0) ||
@@ -1470,10 +1467,13 @@ static int Incremental_container(struct supertype *st, char *devname,
                fprintf(stderr, Name ": failed to get exclusive lock on "
                        "mapfile\n");
        /* do not assemble arrays that might have bad blocks */
-       if (list->array.state & (1<<MD_SB_BBM_ERRORS)) {
+       if (list && list->array.state & (1<<MD_SB_BBM_ERRORS)) {
                fprintf(stderr, Name ": BBM log found in metadata. "
                                        "Cannot activate array(s).\n");
-               list = NULL;
+               /* free container data and exit */
+               sysfs_free(list);
+               map_unlock(&map);
+               return 2;
        }
 
        for (ra = list ; ra ; ra = ra->next) {
@@ -1536,6 +1536,7 @@ static int Incremental_container(struct supertype *st, char *devname,
                                        fprintf(stderr, Name ": array %s/%s is "
                                                "explicitly ignored by mdadm.conf\n",
                                                match->container, match->member);
+                               map_unlock(&map);
                                return 2;
                        }
                        if (match)
@@ -1551,11 +1552,14 @@ static int Incremental_container(struct supertype *st, char *devname,
                if (mdfd < 0) {
                        fprintf(stderr, Name ": failed to open %s: %s.\n",
                                chosen_name, strerror(errno));
+                       map_unlock(&map);
                        return 2;
                }
 
                assemble_container_content(st, mdfd, ra, runstop,
-                                          chosen_name, verbose);
+                                          chosen_name, verbose, NULL,
+                                          freeze_reshape);
+               close(mdfd);
        }
 
        /* Now move all suitable spares from spare container */
@@ -1669,15 +1673,15 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
                                if (subfd >= 0) {
                                        Manage_subdevs(memb->dev, subfd,
                                                       &devlist, verbose, 0,
-                                                      NULL);
+                                                      NULL, 0);
                                        close(subfd);
                                }
                        }
                free_mdstat(mdstat);
        } else
-               Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
+               Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL, 0);
        devlist.disposition = 'r';
-       rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
+       rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL, 0);
        close(mdfd);
        free_mdstat(ent);
        return rv;