]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Incremental.c
Create.c: fix uclibc build
[thirdparty/mdadm.git] / Incremental.c
index ff3548c0f20f055edc6370c8c58ac82319e0fbf5..83db071214ee57ba507e5cfed73f85494cb9950e 100644 (file)
@@ -202,8 +202,7 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
        if (!match && rv == 2)
                goto out;
 
-       if (match && match->devname &&
-           strcasecmp(match->devname, "<ignore>") == 0) {
+       if (match && match->devname && is_devname_ignore(match->devname) == true) {
                if (c->verbose >= 0)
                        pr_err("array containing %s is explicitly ignored by mdadm.conf\n",
                                devname);
@@ -460,8 +459,8 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
                        info.array.working_disks ++;
 
        }
-       if (strncmp(chosen_name, "/dev/md/", 8) == 0)
-               md_devname = chosen_name+8;
+       if (strncmp(chosen_name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0)
+               md_devname = chosen_name + DEV_MD_DIR_LEN;
        else
                md_devname = chosen_name;
        if (c->export) {
@@ -507,6 +506,9 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
                                    GET_OFFSET | GET_SIZE));
        active_disks = count_active(st, sra, mdfd, &avail, &info);
 
+       if (!avail)
+               goto out_unlock;
+
        journal_device_missing = (info.journal_device_required) && (info.journal_clean == 0);
 
        if (info.consistency_policy == CONSISTENCY_POLICY_PPL)
@@ -668,7 +670,7 @@ static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
                        continue;
 
                if (d->disk.raid_disk > -1)
-                       sysfs_set_str(sra, d, "slot", "none");
+                       sysfs_set_str(sra, d, "slot", STR_COMMON_NONE);
                if (sysfs_set_str(sra, d, "state", "remove") == 0)
                        if (verbose >= 0)
                                pr_err("removing old device %s from %s\n",
@@ -831,6 +833,54 @@ container_members_max_degradation(struct map_ent *map, struct map_ent *me)
        return max_degraded;
 }
 
+/**
+ * incremental_external_test_spare_criteria() - helper to test spare criteria.
+ * @st: supertype, must be not NULL, it is duplicated here.
+ * @container_devnm: devnm of the container.
+ * @disk_fd: file descriptor of device to tested.
+ * @verbose: verbose flag.
+ *
+ * The function is used on new drive verification path to check if it can be added to external
+ * container. To test spare criteria, metadata must be loaded. It duplicates super to not mess in
+ * original one.
+ * Function is executed if superblock supports get_spare_criteria(), otherwise success is returned.
+ */
+mdadm_status_t incremental_external_test_spare_criteria(struct supertype *st, char *container_devnm,
+                                                       int disk_fd, int verbose)
+{
+       mdadm_status_t rv = MDADM_STATUS_ERROR;
+       char container_devname[PATH_MAX];
+       struct spare_criteria sc = {0};
+       struct supertype *dup;
+
+       if (!st->ss->get_spare_criteria)
+               return MDADM_STATUS_SUCCESS;
+
+       dup = dup_super(st);
+       snprintf(container_devname, PATH_MAX, "/dev/%s", container_devnm);
+
+       if (dup->ss->get_spare_criteria(dup, container_devname, &sc) != 0) {
+               if (verbose > 1)
+                       pr_err("Failed to get spare criteria for %s\n", container_devname);
+               goto out;
+       }
+
+       if (!disk_fd_matches_criteria(dup, disk_fd, &sc)) {
+               if (verbose > 1)
+                       pr_err("Disk does not match spare criteria for %s\n", container_devname);
+               goto out;
+       }
+
+       rv = MDADM_STATUS_SUCCESS;
+
+out:
+       dev_policy_free(sc.pols);
+       dup->ss->free_super(dup);
+       free(dup);
+
+       return rv;
+}
+
 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                           struct map_ent *target, int bare,
                           struct supertype *st, int verbose)
@@ -871,8 +921,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                struct supertype *st2;
                struct domainlist *dl = NULL;
                struct mdinfo *sra;
-               unsigned long long devsize, freesize = 0;
-               struct spare_criteria sc = {0, 0};
+               unsigned long long freesize = 0;
 
                if (is_subarray(mp->metadata))
                        continue;
@@ -923,34 +972,19 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                if (sra->array.failed_disks == -1)
                        sra->array.failed_disks = container_members_max_degradation(map, mp);
 
-               get_dev_size(dfd, NULL, &devsize);
                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->devnm);
-                       if (mdfd < 0) {
-                               free(st3);
+                       /* true for containers */
+                       if (incremental_external_test_spare_criteria(st2, mp->devnm, dfd, verbose))
                                goto next;
-                       }
-                       if (st3->ss->load_container &&
-                           !st3->ss->load_container(st3, mdfd, mp->path)) {
-                               if (st3->ss->get_spare_criteria)
-                                       st3->ss->get_spare_criteria(st3, &sc);
-                               st3->ss->free_super(st3);
-                       }
-                       free(st3);
-                       close(mdfd);
                }
-               if ((sra->component_size > 0 &&
-                    st2->ss->validate_geometry(st2, sra->array.level, sra->array.layout,
+
+               if (sra->component_size > 0 &&
+                   st2->ss->validate_geometry(st2, sra->array.level, sra->array.layout,
                                                sra->array.raid_disks, &sra->array.chunk_size,
                                                sra->component_size,
                                                sra->devs ? sra->devs->data_offset : INVALID_SECTORS,
                                                devname, &freesize, sra->consistency_policy,
-                                               0) &&
-                    freesize < sra->component_size) ||
-                   (sra->component_size == 0 && devsize < sc.min_size)) {
+                                               0) && freesize < sra->component_size) {
                        if (verbose > 1)
                                pr_err("not adding %s to %s as it is too small\n",
                                        devname, mp->path);
@@ -1465,14 +1499,12 @@ static int Incremental_container(struct supertype *st, char *devname,
 
        st->ss->getinfo_super(st, &info, NULL);
 
-       if ((c->runstop > 0 && info.container_enough >= 0) ||
-           info.container_enough > 0)
-               /* pass */;
-       else {
-               if (c->export) {
+       if (info.container_enough < 0 || (info.container_enough == 0 && c->runstop < 1)) {
+               if (c->export)
                        printf("MD_STARTED=no\n");
-               } else if (c->verbose)
-                       pr_err("not enough devices to start the container\n");
+               else if (c->verbose)
+                       pr_err("Not enough devices to start the container.\n");
+
                return 0;
        }
 
@@ -1564,8 +1596,7 @@ static int Incremental_container(struct supertype *st, char *devname,
                                break;
                        }
 
-                       if (match && match->devname &&
-                           strcasecmp(match->devname, "<ignore>") == 0) {
+                       if (match && match->devname && is_devname_ignore(match->devname) == true) {
                                if (c->verbose > 0)
                                        pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
                                               match->container, match->member);
@@ -1627,54 +1658,18 @@ release:
        return rv;
 }
 
-static void run_udisks(char *arg1, char *arg2)
-{
-       int pid = fork();
-       int status;
-       if (pid == 0) {
-               manage_fork_fds(1);
-               execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
-               execl("/bin/udisks", "udisks", arg1, arg2, NULL);
-               exit(1);
-       }
-       while (pid > 0 && wait(&status) != pid)
-               ;
-}
-
-static int force_remove(char *devnm, int fd, struct mdinfo *mdi, int verbose)
-{
-       int rv;
-       int devid = devnm2devid(devnm);
-
-       run_udisks("--unmount", map_dev(major(devid), minor(devid), 0));
-       rv = Manage_stop(devnm, fd, verbose, 1);
-       if (rv) {
-               /* At least we can try to trigger a 'remove' */
-               sysfs_uevent(mdi, "remove");
-               if (verbose)
-                       pr_err("Fail to stop %s too.\n", devnm);
-       }
-       return rv;
-}
-
 static void remove_from_member_array(struct mdstat_ent *memb,
                                    struct mddev_dev *devlist, int verbose)
 {
-       int rv;
-       struct mdinfo mmdi;
        int subfd = open_dev(memb->devnm);
 
        if (subfd >= 0) {
-               rv = Manage_subdevs(memb->devnm, subfd, devlist, verbose,
-                                   0, UOPT_UNDEFINED, 0);
-               if (rv & 2) {
-                       if (sysfs_init(&mmdi, -1, memb->devnm))
-                               pr_err("unable to initialize sysfs for: %s\n",
-                                      memb->devnm);
-                       else
-                               force_remove(memb->devnm, subfd, &mmdi,
-                                            verbose);
-               }
+               /*
+                * Ignore the return value because it's necessary
+                * to handle failure condition here.
+                */
+               Manage_subdevs(memb->devnm, subfd, devlist, verbose,
+                              0, UOPT_UNDEFINED, 0);
                close(subfd);
        }
 }
@@ -1696,7 +1691,7 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
        struct mdstat_ent *ent;
        struct mddev_dev devlist;
        struct mdinfo mdi;
-       char buf[32];
+       char buf[SYSFS_MAX_BUF_SIZE];
 
        if (!id_path)
                dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
@@ -1744,7 +1739,7 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
 
        memset(&devlist, 0, sizeof(devlist));
        devlist.devname = devname;
-       devlist.disposition = 'f';
+       devlist.disposition = 'I';
        /* for a container, we must fail each member array */
        if (ent->metadata_version &&
            strncmp(ent->metadata_version, "external:", 9) == 0) {
@@ -1757,21 +1752,19 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
                }
                free_mdstat(mdstat);
        } else {
-               rv |= Manage_subdevs(ent->devnm, mdfd, &devlist,
-                                   verbose, 0, UOPT_UNDEFINED, 0);
-               if (rv & 2) {
-               /* Failed due to EBUSY, try to stop the array.
-                * Give udisks a chance to unmount it first.
+               /*
+                * This 'I' incremental remove is a try-best effort,
+                * the failure condition can be safely ignored
+                * because of the following up 'r' remove.
                 */
-                       rv = force_remove(ent->devnm, mdfd, &mdi, verbose);
-                       goto end;
-               }
+               Manage_subdevs(ent->devnm, mdfd, &devlist,
+                              verbose, 0, UOPT_UNDEFINED, 0);
        }
 
        devlist.disposition = 'r';
        rv = Manage_subdevs(ent->devnm, mdfd, &devlist,
                            verbose, 0, UOPT_UNDEFINED, 0);
-end:
+
        close(mdfd);
        free_mdstat(ent);
        return rv;