]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
imsm: move expand verification code into new function
[thirdparty/mdadm.git] / Manage.c
index 9139f96e1431ae43a8247a72144c9a42da428f3f..f54de7c685ef76425eaa86896cb254cd15fec2bf 100644 (file)
--- a/Manage.c
+++ b/Manage.c
 #include "md_p.h"
 #include <ctype.h>
 
-#define REGISTER_DEV           _IO (MD_MAJOR, 1)
-#define START_MD               _IO (MD_MAJOR, 2)
-#define STOP_MD                        _IO (MD_MAJOR, 3)
-
 int Manage_ro(char *devname, int fd, int readonly)
 {
        /* switch to readonly or rw
@@ -40,17 +36,9 @@ int Manage_ro(char *devname, int fd, int readonly)
         * use RESTART_ARRAY_RW or STOP_ARRAY_RO
         *
         */
-       mdu_array_info_t array;
-#ifndef MDASSEMBLE
        struct mdinfo *mdi;
-#endif
        int rv = 0;
 
-       if (md_get_version(fd) < 9000) {
-               pr_err("need md driver version 0.90.0 or later\n");
-               return 1;
-       }
-#ifndef MDASSEMBLE
        /* If this is an externally-managed array, we need to modify the
         * metadata_version so that mdmon doesn't undo our change.
         */
@@ -94,10 +82,9 @@ int Manage_ro(char *devname, int fd, int readonly)
                }
                goto out;
        }
-#endif
-       if (ioctl(fd, GET_ARRAY_INFO, &array)) {
-               pr_err("%s does not appear to be active.\n",
-                       devname);
+
+       if (!md_array_active(fd)) {
+               pr_err("%s does not appear to be active.\n", devname);
                rv = 1;
                goto out;
        }
@@ -118,14 +105,10 @@ int Manage_ro(char *devname, int fd, int readonly)
                }
        }
 out:
-#ifndef MDASSEMBLE
        sysfs_free(mdi);
-#endif
        return rv;
 }
 
-#ifndef MDASSEMBLE
-
 static void remove_devices(char *devnm, char *path)
 {
        /*
@@ -176,10 +159,6 @@ int Manage_run(char *devname, int fd, struct context *c)
         */
        char nm[32], *nmp;
 
-       if (md_get_version(fd) < 9000) {
-               pr_err("need md driver version 0.90.0 or later\n");
-               return 1;
-       }
        nmp = fd2devnm(fd);
        if (!nmp) {
                pr_err("Cannot find %s in sysfs!!\n", devname);
@@ -207,14 +186,6 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
        if (will_retry && verbose == 0)
                verbose = -1;
 
-       if (md_get_version(fd) < 9000) {
-               if (ioctl(fd, STOP_MD, 0) == 0)
-                       return 0;
-               pr_err("stopping device %s failed: %s\n",
-                      devname, strerror(errno));
-               return 1;
-       }
-
        strcpy(devnm, fd2devnm(fd));
        /* Get EXCL access first.  If this fails, then attempting
         * to stop is probably a bad idea.
@@ -233,11 +204,9 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
        count = 5;
        while (((fd = ((devname[0] == '/')
                       ?open(devname, O_RDONLY|O_EXCL)
-                      :open_dev_flags(devnm, O_RDONLY|O_EXCL))) < 0
-               || strcmp(fd2devnm(fd), devnm) != 0)
-              && container[0]
-              && mdmon_running(container)
-              && count) {
+                      :open_dev_flags(devnm, O_RDONLY|O_EXCL))) < 0 ||
+               strcmp(fd2devnm(fd), devnm) != 0) && container[0] &&
+              mdmon_running(container) && count) {
                /* Can't open, so something might be wrong.  However it
                 * is a container, so we might be racing with mdmon, so
                 * retry for a bit.
@@ -273,9 +242,9 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
                while (count &&
                       (err = sysfs_set_str(mdi, NULL,
                                            "array_state",
-                                           "inactive")) < 0
-                      && errno == EBUSY) {
-                       usleep(200000);
+                                           "inactive")) < 0 &&
+                      errno == EBUSY) {
+                       sleep_for(0, MSEC_TO_NSEC(200), true);
                        count--;
                }
                if (err) {
@@ -338,7 +307,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
         *  - unfreeze reshape
         *  - wait on 'sync_completed' for that point to be reached.
         */
-       if (mdi && (mdi->array.level >= 4 && mdi->array.level <= 6) &&
+       if (mdi && is_level456(mdi->array.level) &&
            sysfs_attribute_available(mdi, NULL, "sync_action") &&
            sysfs_attribute_available(mdi, NULL, "reshape_direction") &&
            sysfs_get_str(mdi, NULL, "sync_action", buf, 20) > 0 &&
@@ -359,7 +328,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
                       sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) == 0) {
                        /* must be in the critical section - wait a bit */
                        delay -= 1;
-                       usleep(100000);
+                       sleep_for(0, MSEC_TO_NSEC(100), true);
                }
 
                if (sysfs_set_str(mdi, NULL, "sync_action", "frozen") != 0)
@@ -436,7 +405,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry)
                                 * quite started yet.  Wait a bit and
                                 * check  'sync_action' to see.
                                 */
-                               usleep(10000);
+                               sleep_for(0, MSEC_TO_NSEC(10), true);
                                sysfs_get_str(mdi, NULL, "sync_action", buf, sizeof(buf));
                                if (strncmp(buf, "reshape", 7) != 0)
                                        break;
@@ -476,10 +445,9 @@ done:
         * so it is reasonable to retry for a while - 5 seconds.
         */
        count = 25; err = 0;
-       while (count && fd >= 0
-              && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0
-              && errno == EBUSY) {
-               usleep(200000);
+       while (count && fd >= 0 &&
+              (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 && errno == EBUSY) {
+               sleep_for(0, MSEC_TO_NSEC(200), true);
                count --;
        }
        if (fd >= 0 && err) {
@@ -493,17 +461,6 @@ done:
                goto out;
        }
 
-       if (get_linux_version() < 2006028) {
-               /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
-                * was stopped, so We'll do it here just to be sure.  Drop any
-                * partitions as well...
-                */
-               if (fd >= 0)
-                       ioctl(fd, BLKRRPART, 0);
-               if (mdi)
-                       sysfs_uevent(mdi, "change");
-       }
-
        if (devnm[0] && use_udev()) {
                struct map_ent *mp = map_by_devnm(&map, devnm);
                remove_devices(devnm, mp ? mp->path : NULL);
@@ -539,14 +496,14 @@ static void add_faulty(struct mddev_dev *dv, int fd, char disp)
        int remaining_disks;
        int i;
 
-       if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
+       if (md_get_array_info(fd, &array) != 0)
                return;
 
        remaining_disks = array.nr_disks;
        for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
                char buf[40];
                disk.number = i;
-               if (ioctl(fd, GET_DISK_INFO, &disk) != 0)
+               if (md_get_disk_info(fd, &disk) != 0)
                        continue;
                if (disk.major == 0 && disk.minor == 0)
                        continue;
@@ -565,7 +522,7 @@ static void add_detached(struct mddev_dev *dv, int fd, char disp)
        int remaining_disks;
        int i;
 
-       if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
+       if (md_get_array_info(fd, &array) != 0)
                return;
 
        remaining_disks = array.nr_disks;
@@ -573,7 +530,7 @@ static void add_detached(struct mddev_dev *dv, int fd, char disp)
                char buf[40];
                int sfd;
                disk.number = i;
-               if (ioctl(fd, GET_DISK_INFO, &disk) != 0)
+               if (md_get_disk_info(fd, &disk) != 0)
                        continue;
                if (disk.major == 0 && disk.minor == 0)
                        continue;
@@ -602,7 +559,7 @@ static void add_set(struct mddev_dev *dv, int fd, char set_char)
        int copies, set;
        int i;
 
-       if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
+       if (md_get_array_info(fd, &array) != 0)
                return;
        if (array.level != 10)
                return;
@@ -615,7 +572,7 @@ static void add_set(struct mddev_dev *dv, int fd, char set_char)
        for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
                char buf[40];
                disk.number = i;
-               if (ioctl(fd, GET_DISK_INFO, &disk) != 0)
+               if (md_get_disk_info(fd, &disk) != 0)
                        continue;
                if (disk.major == 0 && disk.minor == 0)
                        continue;
@@ -630,9 +587,8 @@ static void add_set(struct mddev_dev *dv, int fd, char set_char)
 
 int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
                   struct supertype *dev_st, struct supertype *tst,
-                  unsigned long rdev,
-                  char *update, char *devname, int verbose,
-                  mdu_array_info_t *array)
+                  unsigned long rdev, enum update_opt update,
+                  char *devname, int verbose, mdu_array_info_t *array)
 {
        struct mdinfo mdi;
        int duuid[4];
@@ -654,16 +610,9 @@ int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
                 * though.
                 */
                mdu_disk_info_t disc;
-               /* re-add doesn't work for version-1 superblocks
-                * before 2.6.18 :-(
-                */
-               if (array->major_version == 1 &&
-                   get_linux_version() <= 2006018)
-                       goto skip_re_add;
                disc.number = mdi.disk.number;
-               if (ioctl(fd, GET_DISK_INFO, &disc) != 0
-                   || disc.major != 0 || disc.minor != 0
-                       )
+               if (md_get_disk_info(fd, &disc) != 0 ||
+                   disc.major != 0 || disc.minor != 0)
                        goto skip_re_add;
                disc.major = major(rdev);
                disc.minor = minor(rdev);
@@ -688,8 +637,8 @@ int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
                if (dv->failfast == FlagClear)
                        disc.state &= ~(1 << MD_DISK_FAILFAST);
                remove_partitions(tfd);
-               if (update || dv->writemostly != FlagDefault
-                       || dv->failfast != FlagDefault) {
+               if (update || dv->writemostly != FlagDefault ||
+                   dv->failfast != FlagDefault) {
                        int rv = -1;
                        tfd = dev_open(dv->devname, O_RDWR);
                        if (tfd < 0) {
@@ -699,19 +648,19 @@ int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
 
                        if (dv->writemostly == FlagSet)
                                rv = dev_st->ss->update_super(
-                                       dev_st, NULL, "writemostly",
+                                       dev_st, NULL, UOPT_SPEC_WRITEMOSTLY,
                                        devname, verbose, 0, NULL);
                        if (dv->writemostly == FlagClear)
                                rv = dev_st->ss->update_super(
-                                       dev_st, NULL, "readwrite",
+                                       dev_st, NULL, UOPT_SPEC_READWRITE,
                                        devname, verbose, 0, NULL);
                        if (dv->failfast == FlagSet)
                                rv = dev_st->ss->update_super(
-                                       dev_st, NULL, "failfast",
+                                       dev_st, NULL, UOPT_SPEC_FAILFAST,
                                        devname, verbose, 0, NULL);
                        if (dv->failfast == FlagClear)
                                rv = dev_st->ss->update_super(
-                                       dev_st, NULL, "nofailfast",
+                                       dev_st, NULL, UOPT_SPEC_NOFAILFAST,
                                        devname, verbose, 0, NULL);
                        if (update)
                                rv = dev_st->ss->update_super(
@@ -747,8 +696,8 @@ skip_re_add:
 int Manage_add(int fd, int tfd, struct mddev_dev *dv,
               struct supertype *tst, mdu_array_info_t *array,
               int force, int verbose, char *devname,
-              char *update, unsigned long rdev, unsigned long long array_size,
-              int raid_slot)
+              enum update_opt update, unsigned long rdev,
+              unsigned long long array_size, int raid_slot)
 {
        unsigned long long ldsize;
        struct supertype *dev_st;
@@ -774,20 +723,6 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                       "       Adding anyway as --force was given.\n",
                       dv->devname, devname);
        }
-       if (!tst->ss->external &&
-           array->major_version == 0 &&
-           md_get_version(fd)%100 < 2) {
-               if (ioctl(fd, HOT_ADD_DISK, rdev)==0) {
-                       if (verbose >= 0)
-                               pr_err("hot added %s\n",
-                                      dv->devname);
-                       return 1;
-               }
-
-               pr_err("hot add failed for %s: %s\n",
-                      dv->devname, strerror(errno));
-               return -1;
-       }
 
        if (array->not_persistent == 0 || tst->ss->external) {
 
@@ -805,7 +740,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                                char *dev;
                                int dfd;
                                disc.number = j;
-                               if (ioctl(fd, GET_DISK_INFO, &disc))
+                               if (md_get_disk_info(fd, &disc))
                                        continue;
                                if (disc.major==0 && disc.minor==0)
                                        continue;
@@ -827,8 +762,8 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                                break;
                        }
                /* FIXME this is a bad test to be using */
-               if (!tst->sb && (dv->disposition != 'a'
-                                && dv->disposition != 'S')) {
+               if (!tst->sb && (dv->disposition != 'a' &&
+                                dv->disposition != 'S')) {
                        /* we are re-adding a device to a
                         * completely dead array - have to depend
                         * on kernel to check
@@ -888,7 +823,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
 
                        for (d = 0; d < MAX_DISKS && found < array->nr_disks; d++) {
                                disc.number = d;
-                               if (ioctl(fd, GET_DISK_INFO, &disc))
+                               if (md_get_disk_info(fd, &disc))
                                        continue;
                                if (disc.major == 0 && disc.minor == 0)
                                        continue;
@@ -929,7 +864,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
         */
        for (j = array->raid_disks; j < tst->max_devs; j++) {
                disc.number = j;
-               if (ioctl(fd, GET_DISK_INFO, &disc))
+               if (md_get_disk_info(fd, &disc))
                        break;
                if (disc.major==0 && disc.minor==0)
                        break;
@@ -946,7 +881,6 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
 
        /* only add journal to array that supports journaling */
        if (dv->disposition == 'j') {
-               struct mdinfo mdi;
                struct mdinfo *mdp;
 
                mdp = sysfs_read(fd, NULL, GET_ARRAY_STATE);
@@ -955,7 +889,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                        return -1;
                }
 
-               if (strncmp(mdp->sysfs_array_state, "readonly", 8) != 0) {
+               if (mdp->array_state != ARRAY_READONLY) {
                        sysfs_free(mdp);
                        pr_err("%s is not readonly, cannot add journal.\n", devname);
                        return -1;
@@ -963,11 +897,6 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
 
                sysfs_free(mdp);
 
-               tst->ss->getinfo_super(tst, &mdi, NULL);
-               if (mdi.journal_device_required == 0) {
-                       pr_err("%s does not support journal device.\n", devname);
-                       return -1;
-               }
                disc.raid_disk = 0;
        }
 
@@ -994,7 +923,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                for (j = 0; j < tst->max_devs; j++) {
                        mdu_disk_info_t disc2;
                        disc2.number = j;
-                       if (ioctl(fd, GET_DISK_INFO, &disc2))
+                       if (md_get_disk_info(fd, &disc2))
                                continue;
                        if (disc2.major==0 && disc2.minor==0)
                                continue;
@@ -1045,19 +974,22 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                        return -1;
                }
 
+               /* Check if metadata handler is able to accept the drive */
+               if (!tst->ss->validate_geometry(tst, LEVEL_CONTAINER, 0, 1, NULL,
+                   0, 0, dv->devname, NULL, 0, 1)) {
+                       close(container_fd);
+                       return -1;
+               }
+
                Kill(dv->devname, NULL, 0, -1, 0);
                dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
-               if (mdmon_running(tst->container_devnm))
-                       tst->update_tail = &tst->updates;
                if (tst->ss->add_to_super(tst, &disc, dfd,
                                          dv->devname, INVALID_SECTORS)) {
                        close(dfd);
                        close(container_fd);
                        return -1;
                }
-               if (tst->update_tail)
-                       flush_metadata_updates(tst);
-               else
+               if (!mdmon_running(tst->container_devnm))
                        tst->ss->sync_metadata(tst);
 
                sra = sysfs_read(container_fd, NULL, 0);
@@ -1110,7 +1042,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
 }
 
 int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
-                 int sysfd, unsigned long rdev, int verbose, char *devname)
+                 int sysfd, unsigned long rdev, int force, int verbose, char *devname)
 {
        int lfd = -1;
        int err;
@@ -1155,7 +1087,7 @@ int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
                                ret = sysfs_unique_holder(devnm, rdev);
                                if (ret < 2)
                                        break;
-                               usleep(100 * 1000);     /* 100ms */
+                               sleep_for(0, MSEC_TO_NSEC(100), true);
                        } while (--count > 0);
 
                        if (ret == 0) {
@@ -1177,13 +1109,9 @@ int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
                /* device has been removed and we don't know
                 * the major:minor number
                 */
-               int n = write(sysfd, "remove", 6);
-               if (n != 6)
-                       err = -1;
-               else
-                       err = 0;
+               err = sys_hot_remove_disk(sysfd, force);
        } else {
-               err = hot_remove_disk(fd, rdev);
+               err = hot_remove_disk(fd, rdev, force);
                if (err && errno == ENODEV) {
                        /* Old kernels rejected this if no personality
                         * is registered */
@@ -1339,40 +1267,97 @@ int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv,
        return -1;
 }
 
+/**
+ * is_remove_safe() - Check if remove is safe.
+ * @array: Array info.
+ * @fd: Array file descriptor.
+ * @devname: Name of device to remove.
+ * @verbose: Verbose.
+ *
+ * The function determines if array will be operational
+ * after removing &devname.
+ *
+ * Return: True if array will be operational, false otherwise.
+ */
+bool is_remove_safe(mdu_array_info_t *array, const int fd, char *devname, const int verbose)
+{
+       dev_t devid = devnm2devid(devname + 5);
+       struct mdinfo *mdi = sysfs_read(fd, NULL, GET_DEVS | GET_DISKS | GET_STATE);
+
+       if (!mdi) {
+               if (verbose)
+                       pr_err("Failed to read sysfs attributes for %s\n", devname);
+               return false;
+       }
+
+       char *avail = xcalloc(array->raid_disks, sizeof(char));
+
+       for (mdi = mdi->devs; mdi; mdi = mdi->next) {
+               if (mdi->disk.raid_disk < 0)
+                       continue;
+               if (!(mdi->disk.state & (1 << MD_DISK_SYNC)))
+                       continue;
+               if (makedev(mdi->disk.major, mdi->disk.minor) == devid)
+                       continue;
+               avail[mdi->disk.raid_disk] = 1;
+       }
+       sysfs_free(mdi);
+
+       bool is_enough = enough(array->level, array->raid_disks,
+                               array->layout, 1, avail);
+
+       free(avail);
+       return is_enough;
+}
+
+/**
+ * Manage_subdevs() - Execute operation depending on devmode.
+ *
+ * @devname: name of the device.
+ * @fd: file descriptor.
+ * @devlist: list of sub-devices to manage.
+ * @verbose: verbose level.
+ * @test: test flag.
+ * @update: type of update.
+ * @force: force flag.
+ *
+ * This function executes operation defined by devmode
+ * for each dev from devlist.
+ * Devmode can be:
+ * 'a' - add the device
+ * 'S' - add the device as a spare - don't try re-add
+ * 'j' - add the device as a journal device
+ * 'A' - re-add the device
+ * 'r' - remove the device: HOT_REMOVE_DISK
+ *       device can be 'faulty' or 'detached' in which case all
+ *       matching devices are removed.
+ * 'f' - set the device faulty SET_DISK_FAULTY
+ *       device can be 'detached' in which case any device that
+ *       is inaccessible will be marked faulty.
+ * 'I' - remove device by using incremental fail
+ *       which is executed when device is removed surprisingly.
+ * 'R' - mark this device as wanting replacement.
+ * 'W' - this device is added if necessary and activated as
+ *       a replacement for a previous 'R' device.
+ * -----
+ * 'w' - 'W' will be changed to 'w' when it is paired with
+ *       a 'R' device.  If a 'W' is found while walking the list
+ *       it must be unpaired, and is an error.
+ * 'M' - this is created by a 'missing' target.  It is a slight
+ *       variant on 'A'
+ * 'F' - Another variant of 'A', where the device was faulty
+ *       so must be removed from the array first.
+ * 'c' - confirm the device as found (for clustered environments)
+ *
+ * For 'f' and 'r', the device can also be a kernel-internal
+ * name such as 'sdb'.
+ *
+ * Return: 0 on success, otherwise 1 or 2.
+ */
 int Manage_subdevs(char *devname, int fd,
                   struct mddev_dev *devlist, int verbose, int test,
-                  char *update, int force)
+                  enum update_opt update, int force)
 {
-       /* Do something to each dev.
-        * devmode can be
-        *  'a' - add the device
-        *         try HOT_ADD_DISK
-        *         If that fails EINVAL, try ADD_NEW_DISK
-        *  'S' - add the device as a spare - don't try re-add
-        *  'j' - add the device as a journal device
-        *  'A' - re-add the device
-        *  'r' - remove the device: HOT_REMOVE_DISK
-        *        device can be 'faulty' or 'detached' in which case all
-        *        matching devices are removed.
-        *  'f' - set the device faulty SET_DISK_FAULTY
-        *        device can be 'detached' in which case any device that
-        *        is inaccessible will be marked faulty.
-        *  'R' - mark this device as wanting replacement.
-        *  'W' - this device is added if necessary and activated as
-        *        a replacement for a previous 'R' device.
-        * -----
-        *  'w' - 'W' will be changed to 'w' when it is paired with
-        *        a 'R' device.  If a 'W' is found while walking the list
-        *        it must be unpaired, and is an error.
-        *  'M' - this is created by a 'missing' target.  It is a slight
-        *        variant on 'A'
-        *  'F' - Another variant of 'A', where the device was faulty
-        *        so must be removed from the array first.
-        *  'c' - confirm the device as found (for clustered environments)
-        *
-        * For 'f' and 'r', the device can also be a kernel-internal
-        * name such as 'sdb'.
-        */
        mdu_array_info_t array;
        unsigned long long array_size;
        struct mddev_dev *dv;
@@ -1387,13 +1372,15 @@ int Manage_subdevs(char *devname, int fd,
        int busy = 0;
        int raid_slot = -1;
 
-       if (ioctl(fd, GET_ARRAY_INFO, &array)) {
-               pr_err("Cannot get array info for %s\n",
-                       devname);
+       if (sysfs_init(&info, fd, NULL)) {
+               pr_err("sysfs not availabile for %s\n", devname);
                goto abort;
        }
-       sysfs_init(&info, fd, NULL);
 
+       if (md_get_array_info(fd, &array)) {
+               pr_err("Cannot get array info for %s\n", devname);
+               goto abort;
+       }
        /* array.size is only 32 bits and may be truncated.
         * So read from sysfs if possible, and record number of sectors
         */
@@ -1410,7 +1397,7 @@ int Manage_subdevs(char *devname, int fd,
        }
 
        for (dv = devlist; dv; dv = dv->next) {
-               unsigned long rdev = 0; /* device to add/remove etc */
+               dev_t rdev = 0; /* device to add/remove etc */
                int rv;
                int mj,mn;
 
@@ -1427,8 +1414,7 @@ int Manage_subdevs(char *devname, int fd,
 
                if (strcmp(dv->devname, "failed") == 0 ||
                    strcmp(dv->devname, "faulty") == 0) {
-                       if (dv->disposition != 'A'
-                           && dv->disposition != 'r') {
+                       if (dv->disposition != 'A' && dv->disposition != 'r') {
                                pr_err("%s only meaningful with -r or --re-add, not -%c\n",
                                        dv->devname, dv->disposition);
                                goto abort;
@@ -1461,7 +1447,7 @@ int Manage_subdevs(char *devname, int fd,
                        }
                        add_devlist = conf_get_devs();
                        if (add_devlist == NULL) {
-                               pr_err("no devices to scan for missing members.");
+                               pr_err("no devices to scan for missing members.\n");
                                continue;
                        }
                        for (dp = &add_devlist; *dp; dp = & (*dp)->next)
@@ -1507,8 +1493,9 @@ int Manage_subdevs(char *devname, int fd,
                        /* Assume this is a kernel-internal name like 'sda1' */
                        int found = 0;
                        char dname[55];
-                       if (dv->disposition != 'r' && dv->disposition != 'f') {
-                               pr_err("%s only meaningful with -r or -f, not -%c\n",
+                       if (dv->disposition != 'r' && dv->disposition != 'f' &&
+                           dv->disposition != 'I') {
+                               pr_err("%s only meaningful with -r, -f or -I, not -%c\n",
                                        dv->devname, dv->disposition);
                                goto abort;
                        }
@@ -1533,31 +1520,24 @@ int Manage_subdevs(char *devname, int fd,
                                        goto abort;
                                }
                        }
-               } else if ((dv->disposition == 'r' || dv->disposition == 'f')
-                          && get_maj_min(dv->devname, &mj, &mn)) {
+               } else if ((dv->disposition == 'r' ||
+                           dv->disposition == 'f') &&
+                          get_maj_min(dv->devname, &mj, &mn)) {
                        /* for 'fail' and 'remove', the device might
                         * not exist.
                         */
                        rdev = makedev(mj, mn);
                } else {
-                       struct stat stb;
                        tfd = dev_open(dv->devname, O_RDONLY);
                        if (tfd >= 0) {
-                               fstat(tfd, &stb);
+                               fstat_is_blkdev(tfd, dv->devname, &rdev);
                                close(tfd);
                        } else {
                                int open_err = errno;
-                               if (stat(dv->devname, &stb) != 0) {
-                                       pr_err("Cannot find %s: %s\n",
-                                              dv->devname, strerror(errno));
-                                       goto abort;
-                               }
-                               if ((stb.st_mode & S_IFMT) != S_IFBLK) {
+                               if (!stat_is_blkdev(dv->devname, &rdev)) {
                                        if (dv->disposition == 'M')
                                                /* non-fatal. Also improbable */
                                                continue;
-                                       pr_err("%s is not a block device.\n",
-                                              dv->devname);
                                        goto abort;
                                }
                                if (dv->disposition == 'r')
@@ -1574,7 +1554,6 @@ int Manage_subdevs(char *devname, int fd,
                                        goto abort;
                                }
                        }
-                       rdev = stb.st_rdev;
                }
                switch(dv->disposition){
                default:
@@ -1607,7 +1586,7 @@ int Manage_subdevs(char *devname, int fd,
 
                        if (dv->disposition == 'F')
                                /* Need to remove first */
-                               hot_remove_disk(fd, rdev);
+                               hot_remove_disk(fd, rdev, force);
                        /* Make sure it isn't in use (in 2.6 or later) */
                        tfd = dev_open(dv->devname, O_RDONLY|O_EXCL);
                        if (tfd >= 0) {
@@ -1649,7 +1628,7 @@ int Manage_subdevs(char *devname, int fd,
                                rv = -1;
                        } else
                                rv = Manage_remove(tst, fd, dv, sysfd,
-                                                  rdev, verbose,
+                                                  rdev, verbose, force,
                                                   devname);
                        if (sysfd >= 0)
                                close(sysfd);
@@ -1661,7 +1640,14 @@ int Manage_subdevs(char *devname, int fd,
                        break;
 
                case 'f': /* set faulty */
-                       /* FIXME check current member */
+                       if (!is_remove_safe(&array, fd, dv->devname, verbose)) {
+                               pr_err("Cannot remove %s from %s, array will be failed.\n",
+                                      dv->devname, devname);
+                               if (sysfd >= 0)
+                                       close(sysfd);
+                               goto abort;
+                       }
+               case 'I': /* incremental fail */
                        if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
                            (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
                                                rdev))) {
@@ -1738,10 +1724,13 @@ int autodetect(void)
        return rv;
 }
 
-int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int verbose)
+int Update_subarray(char *dev, char *subarray, enum update_opt update,
+                   struct mddev_ident *ident, int verbose)
 {
        struct supertype supertype, *st = &supertype;
        int fd, rv = 2;
+       struct mdinfo *info = NULL;
+       char *update_verb = map_num(update_options, update);
 
        memset(st, 0, sizeof(*st));
 
@@ -1756,25 +1745,41 @@ int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident
                goto free_super;
        }
 
+       if (is_subarray_active(subarray, st->devnm)) {
+               if (verbose >= 0)
+                       pr_err("Subarray %s in %s is active, cannot update %s\n",
+                               subarray, dev, update_verb);
+               goto free_super;
+       }
+
        if (mdmon_running(st->devnm))
                st->update_tail = &st->updates;
 
+       info = st->ss->container_content(st, subarray);
+
+       if (update == UOPT_PPL && !is_level456(info->array.level)) {
+               pr_err("RWH policy ppl is supported only for raid4, raid5 and raid6.\n");
+               goto free_super;
+       }
+
        rv = st->ss->update_subarray(st, subarray, update, ident);
 
        if (rv) {
                if (verbose >= 0)
                        pr_err("Failed to update %s of subarray-%s in %s\n",
-                               update, subarray, dev);
+                               update_verb, subarray, dev);
        } else if (st->update_tail)
                flush_metadata_updates(st);
        else
                st->ss->sync_metadata(st);
 
-       if (rv == 0 && strcmp(update, "name") == 0 && verbose >= 0)
+       if (rv == 0 && update == UOPT_NAME && verbose >= 0)
                pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
                       subarray, dev);
 
- free_super:
+free_super:
+       if (info)
+               free(info);
        st->ss->free_super(st);
        close(fd);
 
@@ -1794,8 +1799,10 @@ int move_spare(char *from_devname, char *to_devname, dev_t devid)
        int fd2 = open(from_devname, O_RDONLY);
 
        if (fd1 < 0 || fd2 < 0) {
-               if (fd1>=0) close(fd1);
-               if (fd2>=0) close(fd2);
+               if (fd1 >= 0)
+                       close(fd1);
+               if (fd2 >= 0)
+                       close(fd2);
                return 0;
        }
 
@@ -1807,9 +1814,10 @@ int move_spare(char *from_devname, char *to_devname, dev_t devid)
        sprintf(devname, "%d:%d", major(devid), minor(devid));
 
        devlist.disposition = 'r';
-       if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
+       if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, UOPT_UNDEFINED, 0) == 0) {
                devlist.disposition = 'a';
-               if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) {
+               if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0,
+                                  UOPT_UNDEFINED, 0) == 0) {
                        /* make sure manager is aware of changes */
                        ping_manager(to_devname);
                        ping_manager(from_devname);
@@ -1817,10 +1825,11 @@ int move_spare(char *from_devname, char *to_devname, dev_t devid)
                        close(fd2);
                        return 1;
                }
-               else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0);
+               else
+                       Manage_subdevs(from_devname, fd2, &devlist,
+                                      -1, 0, UOPT_UNDEFINED, 0);
        }
        close(fd1);
        close(fd2);
        return 0;
 }
-#endif