]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Incremental.c
Allow domain_test to report that no domains were found.
[thirdparty/mdadm.git] / Incremental.c
index 9399f5bec27f6f35356c8abe0df009f9d57e9c83..52558e4059d42aa4dd5fd7fb51925055ddaabcfe 100644 (file)
@@ -358,22 +358,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);
@@ -443,6 +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;
                /* Try to assemble within the container */
                map_unlock(&map);
                sysfs_uevent(&info, "change");
@@ -451,6 +447,8 @@ int Incremental(char *devname, int verbose, int runstop,
                                ": container %s now has %d devices\n",
                                chosen_name, info.array.working_disks);
                wait_for(chosen_name, mdfd);
+               if (st->ss->external)
+                       devname = devnum2devname(fd2devnum(mdfd));
                close(mdfd);
                sysfs_free(sra);
                rv = Incremental(chosen_name, verbose, runstop,
@@ -460,6 +458,12 @@ int Incremental(char *devname, int verbose, int runstop,
                         * have enough devices to start yet
                         */
                        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);
+               }
                return rv;
        }
 
@@ -811,6 +815,32 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
        return cnt;
 }
 
+/* test if container has degraded member(s) */
+static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
+{
+       mdu_array_info_t array;
+       int afd;
+       int max_degraded = 0;
+
+       for(; map; map = map->next) {
+               if (!is_subarray(map->metadata) ||
+                   devname2devnum(map->metadata+1) != me->devnum)
+                       continue;
+               afd = open_dev(map->devnum);
+               if (afd < 0)
+                       continue;
+               /* most accurate information regarding array degradation */
+               if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
+                       int degraded = array.raid_disks - array.active_disks -
+                                      array.spare_disks;
+                       if (degraded > max_degraded)
+                               max_degraded = degraded;
+               }
+               close(afd);
+       }
+       return (max_degraded);
+}
+
 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                           struct map_ent *target, int bare,
                           struct supertype *st, int verbose)
@@ -825,7 +855,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
         * If st is set, then only arrays of that type are considered
         * Return 0 on success, or some exit code on failure, probably 1.
         */
-       int rv = -1;
+       int rv = 1;
        struct stat stb;
        struct map_ent *mp, *map = NULL;
        struct mdinfo *chosen = NULL;
@@ -853,6 +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;
 
                if (is_subarray(mp->metadata))
                        continue;
@@ -878,7 +909,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                                         GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
                                         GET_COMPONENT|GET_VERSION);
                        if (sra)
-                               sra->array.failed_disks = 0;
+                               sra->array.failed_disks = -1;
                }
                if (!sra)
                        continue;
@@ -905,20 +936,34 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                                goto next;
                } else
                        st2 = st;
+               /* update number of failed disks for mostly degraded
+                * container member */
+               if (sra->array.failed_disks == -1)
+                       sra->array.failed_disks = container_members_max_degradation(map, mp);
+
                get_dev_size(dfd, NULL, &devsize);
-               if (st2->ss->avail_size(st2, devsize) < sra->component_size) {
-                       if (verbose > 1)
-                               fprintf(stderr, Name ": not adding %s to %s as it is too small\n",
-                                       devname, mp->path);
-                       goto next;
+               if (sra->component_size == 0) {
+                       /* true for containers, here we must read superblock
+                        * to obtain minimum spare size */
+                       struct supertype *st3 = dup_super(st2);
+                       int mdfd = open_dev(mp->devnum);
+                       if (!mdfd)
+                               goto next;
+                       if (st3->ss->load_container &&
+                           !st3->ss->load_container(st3, mdfd, mp->path)) {
+                               component_size = st3->ss->min_acceptable_spare_size(st3);
+                               st3->ss->free_super(st3);
+                       }
+                       free(st3);
+                       close(mdfd);
                }
-               dl = domain_from_array(sra, st2->ss->name);
-               if (!domain_test(dl, pol, st2->ss->name)) {
-                       /* domain test fails */
+               if ((sra->component_size > 0 &&
+                    st2->ss->avail_size(st2, devsize) < sra->component_size)
+                   ||
+                   (sra->component_size == 0 && devsize < component_size)) {
                        if (verbose > 1)
-                               fprintf(stderr, Name ": not adding %s to %s as it is not in a compatible domain\n",
+                               fprintf(stderr, Name ": not adding %s to %s as it is too small\n",
                                        devname, mp->path);
-
                        goto next;
                }
                /* test against target.
@@ -926,11 +971,14 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                 * arrays/containers that match 'target'.
                 * If 'target' is set and 'bare' is true, we prefer the
                 * array which matches 'target'.
+                * target is considered only if we deal with degraded array
                 */
-               if (target) {
+               if (target && policy_action_allows(pol, st2->ss->name,
+                                                  act_spare_same_slot)) {
                        if (strcmp(target->metadata, mp->metadata) == 0 &&
                            memcmp(target->uuid, mp->uuid,
-                                  sizeof(target->uuid)) == 0) {
+                                  sizeof(target->uuid)) == 0 &&
+                           sra->array.failed_disks > 0) {
                                /* This is our target!! */
                                if (chosen)
                                        sysfs_free(chosen);
@@ -946,6 +994,16 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                                goto next;
                }
 
+               dl = domain_from_array(sra, 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"
+                                       " it is not in a compatible domain\n",
+                                       devname, mp->path);
+
+                       goto next;
+               }
                /* all tests passed, OK to add to this array */
                if (!chosen) {
                        chosen = sra;
@@ -980,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);
+                                            -1, 0, NULL);
                        close(mdfd);
                }
                if (verbose > 0) {
@@ -993,7 +1051,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                }
                sysfs_free(chosen);
        }
-       return rv ? 0 : 1;
+       return rv;
 }
 
 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
@@ -1053,7 +1111,7 @@ 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;
 
@@ -1079,7 +1137,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))
@@ -1371,6 +1429,10 @@ static int Incremental_container(struct supertype *st, char *devname,
        int trustworthy;
        struct mddev_ident *match;
        int rv = 0;
+       struct domainlist *domains;
+       struct map_ent *smp;
+       int suuid[4];
+       int sfd;
 
        memset(&info, 0, sizeof(info));
        st->ss->getinfo_super(st, &info, NULL);
@@ -1402,6 +1464,12 @@ static int Incremental_container(struct supertype *st, char *devname,
        if (map_lock(&map))
                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)) {
+               fprintf(stderr, Name ": BBM log found in metadata. "
+                                       "Cannot activate array(s).\n");
+               list = NULL;
+       }
 
        for (ra = list ; ra ; ra = ra->next) {
                int mdfd;
@@ -1484,6 +1552,52 @@ static int Incremental_container(struct supertype *st, char *devname,
                assemble_container_content(st, mdfd, ra, runstop,
                                           chosen_name, verbose);
        }
+
+       /* Now move all suitable spares from spare container */
+       domains = domain_from_array(list, st->ss->name);
+       memcpy(suuid, uuid_zero, sizeof(int[4]));
+       if (domains &&
+           (smp = map_by_uuid(&map, suuid)) != NULL &&
+           (sfd = open(smp->path, O_RDONLY)) >= 0) {
+               /* spare container found */
+               struct supertype *sst =
+                       super_imsm.match_metadata_desc("imsm");
+               struct mdinfo *sinfo;
+               unsigned long long min_size = 0;
+               if (st->ss->min_acceptable_spare_size)
+                       min_size = st->ss->min_acceptable_spare_size(st);
+               if (!sst->ss->load_container(sst, sfd, NULL)) {
+                       close(sfd);
+                       sinfo = container_choose_spares(sst, min_size,
+                                                       domains, NULL,
+                                                       st->ss->name, 0);
+                       sst->ss->free_super(sst);
+                       if (sinfo){
+                               int count = 0;
+                               struct mdinfo *disks = sinfo->devs;
+                               while (disks) {
+                                       /* move spare from spare
+                                        * container to currently
+                                        * assembled one
+                                        */
+                                       if (move_spare(
+                                                   smp->path,
+                                                   devname,
+                                                   makedev(disks->disk.major,
+                                                           disks->disk.minor)))
+                                               count++;
+                                       disks = disks->next;
+                               }
+                               if (count)
+                                       fprintf(stderr, Name
+                                               ": Added %d spare%s to %s\n",
+                                               count, count>1?"s":"", devname);
+                       }
+                       sysfs_free(sinfo);
+               } else
+                       close(sfd);
+       }
+       domain_free(domains);
        map_unlock(&map);
        return 0;
 }
@@ -1549,15 +1663,16 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
                                int subfd = open_dev(memb->devnum);
                                if (subfd >= 0) {
                                        Manage_subdevs(memb->dev, subfd,
-                                                      &devlist, verbose, 0);
+                                                      &devlist, verbose, 0,
+                                                      NULL);
                                        close(subfd);
                                }
                        }
                free_mdstat(mdstat);
        } else
-               Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
+               Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
        devlist.disposition = 'r';
-       rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
+       rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL);
        close(mdfd);
        free_mdstat(ent);
        return rv;