]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Incremental.c
Change some "fprintf(stderr,"s to pr_err.
[thirdparty/mdadm.git] / Incremental.c
index a94d5fa5eb3b29d78061736eb5325d149434c73a..0a75e3431a08ab5f3465099169b1ef3a4e3e2b8d 100644 (file)
@@ -2,7 +2,7 @@
  * Incremental.c - support --incremental.  Part of:
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2006-2012 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
@@ -29,6 +29,7 @@
  */
 
 #include       "mdadm.h"
+#include       <sys/wait.h>
 #include       <dirent.h>
 #include       <ctype.h>
 
@@ -43,13 +44,10 @@ static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                     struct supertype *st, int verbose);
 
 static int Incremental_container(struct supertype *st, char *devname,
-                                char *homehost,
-                                int verbose, int runstop, int autof,
-                                int freeze_reshape);
+                                struct context *c);
 
-int Incremental(char *devname, int verbose, int runstop,
-               struct supertype *st, char *homehost, int require_homehost,
-               int autof, int freeze_reshape)
+int Incremental(char *devname, struct context *c,
+               struct supertype *st)
 {
        /* Add this device to an array, creating the array if necessary
         * and starting the array if sensible or - if runstop>0 - if possible.
@@ -108,20 +106,20 @@ int Incremental(char *devname, int verbose, int runstop,
        struct createinfo *ci = conf_get_create_info();
 
        if (stat(devname, &stb) < 0) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("stat failed for %s: %s.\n",
                                devname, strerror(errno));
                return rv;
        }
        if ((stb.st_mode & S_IFMT) != S_IFBLK) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("%s is not a block device.\n",
                                devname);
                return rv;
        }
-       dfd = dev_open(devname, O_RDONLY|O_EXCL);
+       dfd = dev_open(devname, O_RDONLY);
        if (dfd < 0) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("cannot open %s: %s.\n",
                                devname, strerror(errno));
                return rv;
@@ -130,6 +128,8 @@ int Incremental(char *devname, int verbose, int runstop,
        if (must_be_container(dfd)) {
                if (!st)
                        st = super_by_fd(dfd, NULL);
+               if (st)
+                       st->ignore_hw_compat = 1;
                if (st && st->ss->load_container)
                        rv = st->ss->load_container(st, dfd, NULL);
 
@@ -138,9 +138,7 @@ int Incremental(char *devname, int verbose, int runstop,
                        if (map_lock(&map))
                                pr_err("failed to get "
                                       "exclusive lock on mapfile\n");
-                       rv = Incremental_container(st, devname, homehost,
-                                                  verbose, runstop, autof,
-                                                  freeze_reshape);
+                       rv = Incremental_container(st, devname, c);
                        map_unlock(&map);
                        return rv;
                }
@@ -153,7 +151,7 @@ int Incremental(char *devname, int verbose, int runstop,
        /* 1/ Check if device is permitted by mdadm.conf */
 
        if (!conf_test_dev(devname)) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("%s not permitted by mdadm.conf.\n",
                               devname);
                goto out;
@@ -163,13 +161,13 @@ int Incremental(char *devname, int verbose, int runstop,
         *            version/name from args) */
 
        if (fstat(dfd, &stb) < 0) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("fstat failed for %s: %s.\n",
                                devname, strerror(errno));
                goto out;
        }
        if ((stb.st_mode & S_IFMT) != S_IFBLK) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("%s is not a block device.\n",
                                devname);
                goto out;
@@ -182,22 +180,23 @@ int Incremental(char *devname, int verbose, int runstop,
        have_target = policy_check_path(&dinfo, &target_array);
 
        if (st == NULL && (st = guess_super(dfd)) == NULL) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("no recognisable superblock on %s.\n",
                               devname);
                rv = try_spare(devname, &dfd, policy,
                               have_target ? &target_array : NULL,
-                              st, verbose);
+                              st, c->verbose);
                goto out;
        }
+       st->ignore_hw_compat = 1;
        if (st->ss->compare_super == NULL ||
            st->ss->load_super(st, dfd, NULL)) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("no RAID superblock on %s.\n",
                                devname);
                rv = try_spare(devname, &dfd, policy,
                               have_target ? &target_array : NULL,
-                              st, verbose);
+                              st, c->verbose);
                free(st);
                goto out;
        }
@@ -206,13 +205,13 @@ int Incremental(char *devname, int verbose, int runstop,
        st->ss->getinfo_super(st, &info, NULL);
 
        /* 3/ Check if there is a match in mdadm.conf */
-       match = conf_match(st, &info, devname, verbose, &rv);
+       match = conf_match(st, &info, devname, c->verbose, &rv);
        if (!match && rv == 2)
                goto out;
 
        if (match && match->devname
            && strcasecmp(match->devname, "<ignore>") == 0) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("array containing %s is explicitly"
                                " ignored by mdadm.conf\n",
                                devname);
@@ -225,17 +224,16 @@ int Incremental(char *devname, int verbose, int runstop,
         * on that. */
        if (match)
                trustworthy = LOCAL;
-       else if (st->ss->match_home(st, homehost) == 1)
+       else if (st->ss->match_home(st, c->homehost) == 1)
                trustworthy = LOCAL;
        else if (st->ss->match_home(st, "any") == 1)
                trustworthy = LOCAL_ANY;
        else
                trustworthy = FOREIGN;
 
-
        if (!match && !conf_test_metadata(st->ss->name, policy,
                                          (trustworthy == LOCAL))) {
-               if (verbose >= 1)
+               if (c->verbose >= 1)
                        pr_err("%s has metadata type %s for which "
                               "auto-assembly is disabled\n",
                               devname, st->ss->name);
@@ -250,9 +248,9 @@ int Incremental(char *devname, int verbose, int runstop,
         * CREATE.
         */
        if (match && match->autof)
-               autof = match->autof;
-       if (autof == 0)
-               autof = ci->autof;
+               c->autof = match->autof;
+       if (c->autof == 0)
+               c->autof = ci->autof;
 
        name_to_use = info.name;
        if (name_to_use[0] == 0 &&
@@ -261,7 +259,7 @@ int Incremental(char *devname, int verbose, int runstop,
                trustworthy = METADATA;
        }
        if (name_to_use[0] && trustworthy != LOCAL &&
-           ! require_homehost &&
+           ! c->require_homehost &&
            conf_name_is_free(name_to_use))
                trustworthy = LOCAL;
 
@@ -276,9 +274,27 @@ int Incremental(char *devname, int verbose, int runstop,
        if (map_lock(&map))
                pr_err("failed to get exclusive lock on "
                        "mapfile\n");
+       /* Now check we can get O_EXCL.  If not, probably "mdadm -A" has
+        * taken over
+        */
+       dfd = dev_open(devname, O_RDONLY|O_EXCL);
+       if (dfd < 0) {
+               if (c->verbose >= 0)
+                       pr_err("cannot reopen %s: %s.\n",
+                               devname, strerror(errno));
+               goto out_unlock;
+       }
+       /* Cannot hold it open while we add the device to the array,
+        * so we must release the O_EXCL and depend on the map_lock()
+        * So now is the best time to remove any partitions.
+        */
+       remove_partitions(dfd);
+       close(dfd);
+       dfd = -1;
+
        mp = map_by_uuid(&map, info.uuid);
        if (mp)
-               mdfd = open_dev(mp->devnum);
+               mdfd = open_dev(mp->devnm);
        else
                mdfd = -1;
 
@@ -286,12 +302,12 @@ int Incremental(char *devname, int verbose, int runstop,
 
                /* Couldn't find an existing array, maybe make a new one */
                mdfd = create_mddev(match ? match->devname : NULL,
-                                   name_to_use, autof, trustworthy, chosen_name);
+                                   name_to_use, c->autof, trustworthy, chosen_name);
 
                if (mdfd < 0)
                        goto out_unlock;
 
-               sysfs_init(&info, mdfd, 0);
+               sysfs_init(&info, mdfd, NULL);
 
                if (set_array_info(mdfd, st, &info) != 0) {
                        pr_err("failed to set array info for %s: %s\n",
@@ -304,15 +320,15 @@ int Incremental(char *devname, int verbose, int runstop,
                dinfo.disk.major = major(stb.st_rdev);
                dinfo.disk.minor = minor(stb.st_rdev);
                if (add_disk(mdfd, st, &info, &dinfo) != 0) {
-                       pr_err("failed to add %s to %s: %s.\n",
+                       pr_err("failed to add %s to new array %s: %s.\n",
                                devname, chosen_name, strerror(errno));
                        ioctl(mdfd, STOP_ARRAY, 0);
                        rv = 2;
                        goto out_unlock;
                }
-               sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
-                                           GET_OFFSET | GET_SIZE));
-       
+               sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
+                                             GET_OFFSET | GET_SIZE));
+
                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.
@@ -326,7 +342,7 @@ int Incremental(char *devname, int verbose, int runstop,
                }
                info.array.working_disks = 1;
                /* 6/ Make sure /var/run/mdadm.map contains this array. */
-               map_update(&map, fd2devnum(mdfd),
+               map_update(&map, fd2devnm(mdfd),
                           info.text_version,
                           info.uuid, chosen_name);
        } else {
@@ -340,13 +356,13 @@ int Incremental(char *devname, int verbose, int runstop,
                struct supertype *st2;
                struct mdinfo info2, *d;
 
-               sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
+               sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
                                            GET_OFFSET | GET_SIZE));
-       
+
                if (mp->path)
                        strcpy(chosen_name, mp->path);
                else
-                       strcpy(chosen_name, devnum2devname(mp->devnum));
+                       strcpy(chosen_name, mp->devnm);
 
                /* It is generally not OK to add non-spare drives to a
                 * running array as they are probably missing because
@@ -363,7 +379,7 @@ int Incremental(char *devname, int verbose, int runstop,
                    && (info.disk.state & (1<<MD_DISK_SYNC)) != 0
                    && ! policy_action_allows(policy, st->ss->name,
                                              act_re_add)
-                   && runstop < 1) {
+                   && c->runstop < 1) {
                        if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
                                pr_err("not adding %s to active array (without --run) %s\n",
                                       devname, chosen_name);
@@ -417,11 +433,23 @@ int Incremental(char *devname, int verbose, int runstop,
                         * disk.number. Find and reject any such
                         */
                        find_reject(mdfd, st, sra, info.disk.number,
-                                   info.events, verbose, chosen_name);
+                                   info.events, c->verbose, chosen_name);
                        err = add_disk(mdfd, st, sra, &info);
                }
+               if (err < 0 && errno == EINVAL &&
+                   info.disk.state & (1<<MD_DISK_SYNC)) {
+                       /* Maybe it needs to be added as a spare */
+                       if (policy_action_allows(policy, st->ss->name,
+                                                act_force_spare)) {
+                               info.disk.state &= ~(1<<MD_DISK_SYNC);
+                               err = add_disk(mdfd, st, sra, &info);
+                       } else
+                               if (c->verbose >= 0)
+                                       pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
+                                              devname, chosen_name);
+               }
                if (err < 0) {
-                       pr_err("failed to add %s to %s: %s.\n",
+                       pr_err("failed to add %s to existing array %s: %s.\n",
                                devname, chosen_name, strerror(errno));
                        rv = 2;
                        goto out_unlock;
@@ -429,30 +457,28 @@ int Incremental(char *devname, int verbose, int runstop,
                info.array.working_disks = 0;
                for (d = sra->devs; d; d=d->next)
                        info.array.working_disks ++;
-                       
+
        }
 
        /* 7/ Is there enough devices to possibly start the array? */
        /* 7a/ if not, finish with success. */
        if (info.array.level == LEVEL_CONTAINER) {
-               int devnum = devnum; /* defined and used iff ->external */
+               char devnm[32];
                /* Try to assemble within the container */
                sysfs_uevent(sra, "change");
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("container %s now has %d device%s\n",
                               chosen_name, info.array.working_disks,
-                              info.array.working_disks==1?"":"s");
+                              info.array.working_disks == 1?"":"s");
                wait_for(chosen_name, mdfd);
                if (st->ss->external)
-                       devnum = fd2devnum(mdfd);
+                       strcpy(devnm, fd2devnm(mdfd));
                if (st->ss->load_container)
                        rv = st->ss->load_container(st, mdfd, NULL);
                close(mdfd);
                sysfs_free(sra);
                if (!rv)
-                       rv = Incremental_container(st, chosen_name, homehost,
-                                                  verbose, runstop, autof,
-                                                  freeze_reshape);
+                       rv = Incremental_container(st, chosen_name, c);
                map_unlock(&map);
                if (rv == 1)
                        /* Don't fail the whole -I if a subarray didn't
@@ -462,7 +488,7 @@ int Incremental(char *devname, int verbose, int runstop,
                /* 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_by_id(devnum);
+                       ping_monitor(devnm);
                return rv;
        }
 
@@ -471,13 +497,13 @@ int Incremental(char *devname, int verbose, int runstop,
         * things change.
         */
        sysfs_free(sra);
-       sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
+       sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
                                    GET_OFFSET | GET_SIZE));
        active_disks = count_active(st, sra, mdfd, &avail, &info);
        if (enough(info.array.level, info.array.raid_disks,
                   info.array.layout, info.array.state & 1,
                   avail) == 0) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("%s attached to %s, not enough to start (%d).\n",
                               devname, chosen_name, active_disks);
                rv = 0;
@@ -491,7 +517,7 @@ int Incremental(char *devname, int verbose, int runstop,
        /*   + start the array (auto-readonly). */
 
        if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("%s attached to %s which is already active.\n",
                               devname, chosen_name);
                rv = 0;
@@ -499,9 +525,16 @@ int Incremental(char *devname, int verbose, int runstop,
        }
 
        map_unlock(&map);
-       if (runstop > 0 || active_disks >= info.array.working_disks) {
+       if (c->runstop > 0 || active_disks >= info.array.working_disks) {
                struct mdinfo *dsk;
                /* Let's try to start it */
+
+               if (info.reshape_active && !(info.reshape_active & RESHAPE_NO_BACKUP)) {
+                       pr_err("%s: This array is being reshaped and cannot be started\n",
+                              chosen_name);
+                       cont_err("by --incremental.  Please use --assemble\n");
+                       goto out;
+               }
                if (match && match->bitmap_file) {
                        int bmfd = open(match->bitmap_file, O_RDWR);
                        if (bmfd < 0) {
@@ -531,7 +564,7 @@ int Incremental(char *devname, int verbose, int runstop,
                        rv = sysfs_set_str(sra, NULL,
                                           "array_state", "read-auto");
                if (rv == 0) {
-                       if (verbose >= 0)
+                       if (c->verbose >= 0)
                                pr_err("%s attached to %s, which has been started.\n",
                                       devname, chosen_name);
                        rv = 0;
@@ -554,7 +587,7 @@ int Incremental(char *devname, int verbose, int runstop,
                        rv = 1;
                }
        } else {
-               if (verbose >= 0)
+               if (c->verbose >= 0)
                        pr_err("%s attached to %s, not enough to start safely.\n",
                               devname, chosen_name);
                rv = 0;
@@ -625,6 +658,7 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
        /* count how many devices in sra think they are active */
        struct mdinfo *d;
        int cnt = 0;
+       int replcnt = 0;
        __u64 max_events = 0;
        char *avail = NULL;
        int *best = NULL;
@@ -639,7 +673,7 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
 
        for (d = sra->devs ; d ; d = d->next)
                numdevs++;
-       for (d = sra->devs, devnum=0 ; d ; d = d->next, devnum++) {
+       for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) {
                char dn[30];
                int dfd;
                int ok;
@@ -657,15 +691,11 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
                st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
                if (!avail) {
                        raid_disks = info.array.raid_disks;
-                       avail = calloc(raid_disks, 1);
-                       if (!avail) {
-                               pr_err("out of memory.\n");
-                               exit(1);
-                       }
+                       avail = xcalloc(raid_disks, 1);
                        *availp = avail;
 
-                       best = calloc(raid_disks, sizeof(int));
-                       devmap = calloc(raid_disks * numdevs, 1);
+                       best = xcalloc(raid_disks, sizeof(int));
+                       devmap = xcalloc(raid_disks, numdevs);
 
                        st->ss->getinfo_super(st, &info, devmap);
                }
@@ -691,7 +721,7 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
                        else if (info.events == max_events+1) {
                                int i;
                                max_events = info.events;
-                               for (i=0; i < raid_disks; i++)
+                               for (i = 0; i < raid_disks; i++)
                                        if (avail[i])
                                                avail[i]--;
                                avail[info.disk.raid_disk] = 2;
@@ -704,7 +734,8 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
                                best[info.disk.raid_disk] = devnum;
                                st->ss->getinfo_super(st, bestinfo, NULL);
                        }
-               }
+               } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT))
+                       replcnt++;
                st->ss->free_super(st);
        }
        if (!avail)
@@ -731,7 +762,7 @@ static int count_active(struct supertype *st, struct mdinfo *sra,
        }
        free(best);
        free(devmap);
-       return cnt;
+       return cnt + replcnt;
 }
 
 /* test if container has degraded member(s) */
@@ -742,10 +773,9 @@ static int container_members_max_degradation(struct map_ent *map, struct map_ent
        int max_degraded = 0;
 
        for(; map; map = map->next) {
-               if (!is_subarray(map->metadata) ||
-                   devname2devnum(map->metadata+1) != me->devnum)
+               if (!metadata_container_matches(map->metadata, me->devnm))
                        continue;
-               afd = open_dev(map->devnum);
+               afd = open_dev(map->devnm);
                if (afd < 0)
                        continue;
                /* most accurate information regarding array degradation */
@@ -819,12 +849,12 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                        }
                        free(st2);
                }
-               sra = sysfs_read(-1, mp->devnum,
+               sra = sysfs_read(-1, mp->devnm,
                                 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
                                 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
                if (!sra) {
                        /* Probably a container - no degraded info */
-                       sra = sysfs_read(-1, mp->devnum,
+                       sra = sysfs_read(-1, mp->devnm,
                                         GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
                                         GET_COMPONENT|GET_VERSION);
                        if (sra)
@@ -835,7 +865,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                if (st == NULL) {
                        int i;
                        st2 = NULL;
-                       for(i=0; !st2 && superlist[i]; i++)
+                       for(i = 0; !st2 && superlist[i]; i++)
                                st2 = superlist[i]->match_metadata_desc(
                                        sra->text_version);
                        if (!st2) {
@@ -865,7 +895,7 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                        /* 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);
+                       int mdfd = open_dev(mp->devnm);
                        if (mdfd < 0) {
                                free(st3);
                                goto next;
@@ -879,7 +909,11 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
                        close(mdfd);
                }
                if ((sra->component_size > 0 &&
-                    st2->ss->avail_size(st2, devsize) < sra->component_size)
+                    st2->ss->avail_size(st2, devsize,
+                                        sra->devs
+                                        ? sra->devs->data_offset
+                                        : INVALID_SECTORS)
+                    < sra->component_size)
                    ||
                    (sra->component_size == 0 && devsize < component_size)) {
                        if (verbose > 1)
@@ -944,13 +978,12 @@ static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
        }
        if (chosen) {
                /* add current device to chosen array as a spare */
-               int mdfd = open_dev(devname2devnum(chosen->sys_name));
+               int mdfd = open_dev(chosen->sys_name);
                if (mdfd >= 0) {
                        struct mddev_dev devlist;
                        char devname[20];
                        devlist.next = NULL;
                        devlist.used = 0;
-                       devlist.re_add = 0;
                        devlist.writemostly = 0;
                        devlist.devname = devname;
                        sprintf(devname, "%d:%d", major(stb.st_rdev),
@@ -1257,7 +1290,7 @@ int IncrementalScan(int verbose)
                mdu_array_info_t array;
                mdu_bitmap_file_t bmf;
                struct mdinfo *sra;
-               int mdfd = open_dev(me->devnum);
+               int mdfd = open_dev(me->devnm);
 
                if (mdfd < 0)
                        continue;
@@ -1294,16 +1327,19 @@ int IncrementalScan(int verbose)
                                               me->path, strerror(errno));
                        }
                }
-               sra = sysfs_read(mdfd, 0, 0);
+               /* FIXME check for reshape_active and consider not
+                * starting array.
+                */
+               sra = sysfs_read(mdfd, NULL, 0);
                if (sra) {
                        if (sysfs_set_str(sra, NULL,
                                          "array_state", "read-auto") == 0) {
                                if (verbose >= 0)
                                        pr_err("started array %s\n",
-                                              me->path ?: devnum2devname(me->devnum));
+                                              me->path ?: me->devnm);
                        } else {
                                pr_err("failed to start array %s: %s\n",
-                                      me->path ?: devnum2devname(me->devnum),
+                                      me->path ?: me->devnm,
                                       strerror(errno));
                                rv = 1;
                        }
@@ -1320,18 +1356,18 @@ static char *container2devname(char *devname)
        if (devname[0] == '/') {
                int fd = open(devname, O_RDONLY);
                if (fd >= 0) {
-                       mdname = devnum2devname(fd2devnum(fd));
+                       mdname = xstrdup(fd2devnm(fd));
                        close(fd);
                }
        } else {
                int uuid[4];
                struct map_ent *mp, *map = NULL;
-                                       
+
                if (!parse_uuid(devname, uuid))
                        return mdname;
                mp = map_by_uuid(&map, uuid);
                if (mp)
-                       mdname = devnum2devname(mp->devnum);
+                       mdname = xstrdup(mp->devnm);
                map_free(map);
        }
 
@@ -1339,8 +1375,7 @@ static char *container2devname(char *devname)
 }
 
 static int Incremental_container(struct supertype *st, char *devname,
-                                char *homehost, int verbose,
-                                int runstop, int autof, int freeze_reshape)
+                                struct context *c)
 {
        /* Collect the contents of this container and for each
         * array, choose a device name and assemble the array.
@@ -1362,23 +1397,23 @@ static int Incremental_container(struct supertype *st, char *devname,
 
        st->ss->getinfo_super(st, &info, NULL);
 
-       if ((runstop > 0 && info.container_enough >= 0) ||
+       if ((c->runstop > 0 && info.container_enough >= 0) ||
            info.container_enough > 0)
                /* pass */;
        else {
-               if (verbose)
+               if (c->verbose)
                        pr_err("not enough devices to start the container\n");
                return 0;
        }
 
-       match = conf_match(st, &info, devname, verbose, &rv);
+       match = conf_match(st, &info, devname, c->verbose, &rv);
        if (match == NULL && rv == 2)
                return rv;
 
        /* Need to compute 'trustworthy' */
        if (match)
                trustworthy = LOCAL;
-       else if (st->ss->match_home(st, homehost) == 1)
+       else if (st->ss->match_home(st, c->homehost) == 1)
                trustworthy = LOCAL;
        else if (st->ss->match_home(st, "any") == 1)
                trustworthy = LOCAL;
@@ -1406,11 +1441,11 @@ static int Incremental_container(struct supertype *st, char *devname,
                mp = map_by_uuid(&map, ra->uuid);
 
                if (mp) {
-                       mdfd = open_dev(mp->devnum);
+                       mdfd = open_dev(mp->devnm);
                        if (mp->path)
                                strcpy(chosen_name, mp->path);
                        else
-                               strcpy(chosen_name, devnum2devname(mp->devnum));
+                               strcpy(chosen_name, mp->devnm);
                } else {
 
                        /* Check in mdadm.conf for container == devname and
@@ -1445,7 +1480,7 @@ static int Incremental_container(struct supertype *st, char *devname,
                                free(dn);
                                /* we have a match */
                                match = array_list;
-                               if (verbose>0)
+                               if (c->verbose>0)
                                        pr_err("match found for member %s\n",
                                                array_list->member);
                                break;
@@ -1453,7 +1488,7 @@ static int Incremental_container(struct supertype *st, char *devname,
 
                        if (match && match->devname &&
                            strcasecmp(match->devname, "<ignore>") == 0) {
-                               if (verbose > 0)
+                               if (c->verbose > 0)
                                        pr_err("array %s/%s is "
                                               "explicitly ignored by mdadm.conf\n",
                                               match->container, match->member);
@@ -1464,7 +1499,7 @@ static int Incremental_container(struct supertype *st, char *devname,
 
                        mdfd = create_mddev(match ? match->devname : NULL,
                                            ra->name,
-                                           autof,
+                                           c->autof,
                                            trustworthy,
                                            chosen_name);
                }
@@ -1475,9 +1510,8 @@ static int Incremental_container(struct supertype *st, char *devname,
                        return 2;
                }
 
-               assemble_container_content(st, mdfd, ra, runstop,
-                                          chosen_name, verbose, NULL,
-                                          freeze_reshape);
+               assemble_container_content(st, mdfd, ra, c,
+                                          chosen_name);
                close(mdfd);
        }
 
@@ -1533,6 +1567,19 @@ static int Incremental_container(struct supertype *st, char *devname,
        return 0;
 }
 
+static void run_udisks(char *arg1, char *arg2)
+{
+       int pid = fork();
+       int status;
+       if (pid == 0) {
+               execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
+               execl("/bin/udisks", "udisks", arg1, arg2, NULL);
+               exit(1);
+       }
+       while (pid > 0 && wait(&status) != pid)
+               ;
+}
+
 /*
  * IncrementalRemove - Attempt to see if the passed in device belongs to any
  * raid arrays, and if so first fail (if needed) and then remove the device.
@@ -1546,9 +1593,11 @@ static int Incremental_container(struct supertype *st, char *devname,
 int IncrementalRemove(char *devname, char *id_path, int verbose)
 {
        int mdfd;
-       int rv;
+       int rv = 0;
        struct mdstat_ent *ent;
        struct mddev_dev devlist;
+       struct mdinfo mdi;
+       char buf[32];
 
        if (!id_path)
                dprintf(Name ": incremental removal without --path <id_path> "
@@ -1566,7 +1615,15 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
                        "of any array\n", devname);
                return 1;
        }
-       mdfd = open_dev(ent->devnum);
+       sysfs_init(&mdi, -1, ent->devnm);
+       if (sysfs_get_str(&mdi, NULL, "array_state",
+                         buf, sizeof(buf)) > 0) {
+               if (strncmp(buf, "active", 6) == 0 ||
+                   strncmp(buf, "clean", 5) == 0)
+                       sysfs_set_str(&mdi, NULL,
+                                     "array_state", "read-auto");
+       }
+       mdfd = open_dev(ent->devnm);
        if (mdfd < 0) {
                pr_err("Cannot open array %s!!\n", ent->dev);
                free_mdstat(ent);
@@ -1575,7 +1632,7 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
 
        if (id_path) {
                struct map_ent *map = NULL, *me;
-               me = map_by_devnum(&map, ent->devnum);
+               me = map_by_devnm(&map, ent->devnm);
                if (me)
                        policy_save_path(id_path, me);
                map_free(map);
@@ -1591,19 +1648,39 @@ int IncrementalRemove(char *devname, char *id_path, int verbose)
                struct mdstat_ent *memb;
                for (memb = mdstat ; memb ; memb = memb->next)
                        if (is_container_member(memb, ent->dev)) {
-                               int subfd = open_dev(memb->devnum);
+                               int subfd = open_dev(memb->devnm);
                                if (subfd >= 0) {
-                                       Manage_subdevs(memb->dev, subfd,
-                                                      &devlist, verbose, 0,
-                                                      NULL, 0);
+                                       rv |= Manage_subdevs(
+                                               memb->dev, subfd,
+                                               &devlist, verbose, 0,
+                                               NULL, 0);
                                        close(subfd);
                                }
                        }
                free_mdstat(mdstat);
        } else
-               Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL, 0);
-       devlist.disposition = 'r';
-       rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL, 0);
+               rv |= Manage_subdevs(ent->dev, mdfd, &devlist,
+                                   verbose, 0, NULL, 0);
+       if (rv & 2) {
+               /* Failed due to EBUSY, try to stop the array.
+                * Give udisks a chance to unmount it first.
+                */
+               int devid = devnm2devid(ent->devnm);
+               run_udisks("--unmount", map_dev(major(devid),minor(devid), 0));
+               rv = Manage_runstop(ent->dev, mdfd, -1,
+                                   verbose, 1);
+               if (rv)
+                       /* At least we can try to trigger a 'remove' */
+                       sysfs_uevent(&mdi, "remove");
+               if (verbose) {
+                       if (rv)
+                               pr_err("Fail to stop %s too.\n", ent->devnm);
+               }
+       } else {
+               devlist.disposition = 'r';
+               rv = Manage_subdevs(ent->dev, mdfd, &devlist,
+                                   verbose, 0, NULL, 0);
+       }
        close(mdfd);
        free_mdstat(ent);
        return rv;