]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Incremental.c
sysfs: Use the presence of /sys/block/<dev>/md as indicator of valid device
[thirdparty/mdadm.git] / Incremental.c
index 5b2974c384f66eaad17a18f8f34ce9e291a4c674..802e5255d3389c74f00aef55a00c549ee31db926 100644 (file)
@@ -35,7 +35,7 @@
 
 static int count_active(struct supertype *st, struct mdinfo *sra,
                        int mdfd, char **availp,
-                       struct mdinfo *info, int *journal_device_missing);
+                       struct mdinfo *info);
 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
                        int number, __u64 events, int verbose,
                        char *array_name);
@@ -398,7 +398,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
                    && ! policy_action_allows(policy, st->ss->name,
                                              act_re_add)
                    && c->runstop < 1) {
-                       if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
+                       if (md_get_array_info(mdfd, &ainf) == 0) {
                                pr_err("not adding %s to active array (without --run) %s\n",
                                       devname, chosen_name);
                                rv = 2;
@@ -444,6 +444,10 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
                /* add disk needs to know about containers */
                if (st->ss->external)
                        sra->array.level = LEVEL_CONTAINER;
+
+               if (info.array.state & (1 << MD_SB_CLUSTERED))
+                       info.disk.state |= (1 << MD_DISK_CLUSTER_ADD);
+
                err = add_disk(mdfd, st, sra, &info);
                if (err < 0 && errno == EBUSY) {
                        /* could be another device present with the same
@@ -520,7 +524,13 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
        sysfs_free(sra);
        sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
                                    GET_OFFSET | GET_SIZE));
-       active_disks = count_active(st, sra, mdfd, &avail, &info, &journal_device_missing);
+       active_disks = count_active(st, sra, mdfd, &avail, &info);
+
+       journal_device_missing = (info.journal_device_required) && (info.journal_clean == 0);
+
+       if (info.consistency_policy == CONSISTENCY_POLICY_PPL)
+               info.array.state |= 1;
+
        if (enough(info.array.level, info.array.raid_disks,
                   info.array.layout, info.array.state & 1,
                   avail) == 0) {
@@ -539,7 +549,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
        /*   + add any bitmap file  */
        /*   + start the array (auto-readonly). */
 
-       if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
+       if (md_get_array_info(mdfd, &ainf) == 0) {
                if (c->export) {
                        printf("MD_STARTED=already\n");
                } else if (c->verbose >= 0)
@@ -637,8 +647,7 @@ out:
                close(mdfd);
        if (policy)
                dev_policy_free(policy);
-       if (sra)
-               sysfs_free(sra);
+       sysfs_free(sra);
        return rv;
 out_unlock:
        map_unlock(&map);
@@ -655,12 +664,12 @@ static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
        struct mdinfo *d;
        mdu_array_info_t ra;
 
-       if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
+       if (md_get_array_info(mdfd, &ra) == 0)
                return; /* not safe to remove from active arrays
                         * without thinking more */
 
        for (d = sra->devs; d ; d = d->next) {
-               char dn[10];
+               char dn[24]; // 2*11 bytes for ints (including sign) + colon + null byte
                int dfd;
                struct mdinfo info;
                sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
@@ -690,8 +699,7 @@ static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
 
 static int count_active(struct supertype *st, struct mdinfo *sra,
                        int mdfd, char **availp,
-                       struct mdinfo *bestinfo,
-                       int *journal_device_missing)
+                       struct mdinfo *bestinfo)
 {
        /* count how many devices in sra think they are active */
        struct mdinfo *d;
@@ -705,8 +713,6 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
        int devnum;
        int b, i;
        int raid_disks = 0;
-       int require_journal_dev = 0;
-       int has_journal_dev = 0;
 
        if (!sra)
                return 0;
@@ -728,18 +734,10 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
                if (ok != 0)
                        continue;
 
-               if (st->ss->require_journal) {
-                       require_journal_dev = st->ss->require_journal(st);
-                       if (require_journal_dev == 2) {
-                               pr_err("BUG: Superblock not loaded in Incremental.c:count_active\n");
-                               return 0;
-                       }
-               }
-
                info.array.raid_disks = raid_disks;
                st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
                if (info.disk.raid_disk == MD_DISK_ROLE_JOURNAL)
-                       has_journal_dev = 1;
+                       bestinfo->journal_clean = 1;
                if (!avail) {
                        raid_disks = info.array.raid_disks;
                        avail = xcalloc(raid_disks, 1);
@@ -790,9 +788,6 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
                st->ss->free_super(st);
        }
 
-       if (require_journal_dev && !has_journal_dev)
-               *journal_device_missing = 1;
-
        if (!avail)
                return 0;
        /* We need to reject any device that thinks the best device is
@@ -842,7 +837,7 @@ static int container_members_max_degradation(struct map_ent *map, struct map_ent
                if (afd < 0)
                        continue;
                /* most accurate information regarding array degradation */
-               if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
+               if (md_get_array_info(afd, &array) >= 0) {
                        int degraded = array.raid_disks - array.active_disks -
                                       array.spare_disks;
                        if (degraded > max_degraded)
@@ -996,8 +991,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                                   sizeof(target->uuid)) == 0 &&
                            sra->array.failed_disks > 0) {
                                /* This is our target!! */
-                               if (chosen)
-                                       sysfs_free(chosen);
+                               sysfs_free(chosen);
                                chosen = sra;
                                sra = NULL;
                                /* skip to end so we don't check any more */
@@ -1029,8 +1023,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                        sra = NULL;
                }
        next:
-               if (sra)
-                       sysfs_free(sra);
+               sysfs_free(sra);
                if (st != st2)
                        free(st2);
                if (dl)
@@ -1041,12 +1034,13 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                int mdfd = open_dev(chosen->sys_name);
                if (mdfd >= 0) {
                        struct mddev_dev devlist;
-                       char devname[20];
+                       char chosen_devname[24]; // 2*11 for int (including signs) + colon + null
                        devlist.next = NULL;
                        devlist.used = 0;
-                       devlist.writemostly = 0;
-                       devlist.devname = devname;
-                       sprintf(devname, "%d:%d", major(stb.st_rdev),
+                       devlist.writemostly = FlagDefault;
+                       devlist.failfast = FlagDefault;
+                       devlist.devname = chosen_devname;
+                       sprintf(chosen_devname, "%d:%d", major(stb.st_rdev),
                                minor(stb.st_rdev));
                        devlist.disposition = 'a';
                        close(dfd);
@@ -1351,14 +1345,17 @@ int IncrementalScan(struct context *c, char *devnm)
 restart:
        for (me = mapl ; me ; me = me->next) {
                mdu_array_info_t array;
-               mdu_bitmap_file_t bmf;
                struct mdinfo *sra;
                int mdfd;
 
                if (devnm && strcmp(devnm, me->devnm) != 0)
                        continue;
-               if (devnm && me->metadata[0] == '/') {
+               if (me->metadata[0] == '/') {
                        char *sl;
+
+                       if (!devnm)
+                               continue;
+
                        /* member array, need to work on container */
                        strncpy(container, me->metadata+1, 32);
                        container[31] = 0;
@@ -1392,8 +1389,7 @@ restart:
                                rv = 1;
                        continue;
                }
-               if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
-                   errno != ENODEV) {
+               if (md_get_array_info(mdfd, &array) == 0 || errno != ENODEV) {
                        close(mdfd);
                        continue;
                }
@@ -1408,13 +1404,12 @@ restart:
                         * is a hint only
                         */
                        int added = -1;
-                       if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
-                               int bmfd = open(mddev->bitmap_file, O_RDWR);
-                               if (bmfd >= 0) {
-                                       added = ioctl(mdfd, SET_BITMAP_FILE,
-                                                     bmfd);
-                                       close(bmfd);
-                               }
+                       int bmfd;
+
+                       bmfd = open(mddev->bitmap_file, O_RDWR);
+                       if (bmfd >= 0) {
+                               added = ioctl(mdfd, SET_BITMAP_FILE, bmfd);
+                               close(bmfd);
                        }
                        if (c->verbose >= 0) {
                                if (added == 0)