]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
Incremental: Remove redundant call for GET_ARRAY_INFO
[thirdparty/mdadm.git] / Manage.c
index 494cca9248e763ab1db475b3b7b7f75de9d7a339..24ed370579472e16f546a3a7ecdd261a40609639 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -95,7 +95,7 @@ int Manage_ro(char *devname, int fd, int readonly)
                goto out;
        }
 #endif
-       if (ioctl(fd, GET_ARRAY_INFO, &array)) {
+       if (md_get_array_info(fd, &array)) {
                pr_err("%s does not appear to be active.\n",
                        devname);
                rv = 1;
@@ -119,8 +119,7 @@ int Manage_ro(char *devname, int fd, int readonly)
        }
 out:
 #ifndef MDASSEMBLE
-       if (mdi)
-               sysfs_free(mdi);
+       sysfs_free(mdi);
 #endif
        return rv;
 }
@@ -493,14 +492,17 @@ done:
                rv = 1;
                goto out;
        }
-       /* 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 (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);
@@ -513,8 +515,7 @@ done:
        map_remove(&map, devnm);
        map_unlock(&map);
 out:
-       if (mdi)
-               sysfs_free(mdi);
+       sysfs_free(mdi);
 
        return rv;
 }
@@ -538,7 +539,7 @@ 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;
@@ -564,7 +565,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;
@@ -601,7 +602,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;
@@ -669,12 +670,26 @@ int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
                disc.number = mdi.disk.number;
                disc.raid_disk = mdi.disk.raid_disk;
                disc.state = mdi.disk.state;
-               if (dv->writemostly == 1)
+               if (array->state & (1 << MD_SB_CLUSTERED)) {
+                       /* extra flags are needed when adding to a cluster as
+                        * there are two cases to distinguish
+                        */
+                       if (dv->disposition == 'c')
+                               disc.state |= (1 << MD_DISK_CANDIDATE);
+                       else
+                               disc.state |= (1 << MD_DISK_CLUSTER_ADD);
+               }
+               if (dv->writemostly == FlagSet)
                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
-               if (dv->writemostly == 2)
+               if (dv->writemostly == FlagClear)
                        disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
+               if (dv->failfast == FlagSet)
+                       disc.state |= 1 << MD_DISK_FAILFAST;
+               if (dv->failfast == FlagClear)
+                       disc.state &= ~(1 << MD_DISK_FAILFAST);
                remove_partitions(tfd);
-               if (update || dv->writemostly > 0) {
+               if (update || dv->writemostly != FlagDefault
+                       || dv->failfast != FlagDefault) {
                        int rv = -1;
                        tfd = dev_open(dv->devname, O_RDWR);
                        if (tfd < 0) {
@@ -682,14 +697,22 @@ int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
                                return -1;
                        }
 
-                       if (dv->writemostly == 1)
+                       if (dv->writemostly == FlagSet)
                                rv = dev_st->ss->update_super(
                                        dev_st, NULL, "writemostly",
                                        devname, verbose, 0, NULL);
-                       if (dv->writemostly == 2)
+                       if (dv->writemostly == FlagClear)
                                rv = dev_st->ss->update_super(
                                        dev_st, NULL, "readwrite",
                                        devname, verbose, 0, NULL);
+                       if (dv->failfast == FlagSet)
+                               rv = dev_st->ss->update_super(
+                                       dev_st, NULL, "failfast",
+                                       devname, verbose, 0, NULL);
+                       if (dv->failfast == FlagClear)
+                               rv = dev_st->ss->update_super(
+                                       dev_st, NULL, "nofailfast",
+                                       devname, verbose, 0, NULL);
                        if (update)
                                rv = dev_st->ss->update_super(
                                        dev_st, NULL, update,
@@ -728,7 +751,7 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
               int raid_slot)
 {
        unsigned long long ldsize;
-       struct supertype *dev_st = NULL;
+       struct supertype *dev_st;
        int j;
        mdu_disk_info_t disc;
 
@@ -816,7 +839,8 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                }
 
                /* Make sure device is large enough */
-               if (tst->sb &&
+               if (dv->disposition != 'j' &&  /* skip size check for Journal */
+                   tst->sb &&
                    tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
                    array_size) {
                        if (dv->disposition == 'M')
@@ -832,20 +856,19 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                 * simply re-add it.
                 */
 
-               if (array->not_persistent==0) {
+               if (array->not_persistent == 0) {
                        dev_st = dup_super(tst);
                        dev_st->ss->load_super(dev_st, tfd, NULL);
-               }
-               if (dev_st && dev_st->sb && dv->disposition != 'S') {
-                       int rv = attempt_re_add(fd, tfd, dv,
-                                               dev_st, tst,
-                                               rdev,
-                                               update, devname,
-                                               verbose,
-                                               array);
-                       dev_st->ss->free_super(dev_st);
-                       if (rv)
-                               return rv;
+                       if (dev_st->sb && dv->disposition != 'S') {
+                               int rv;
+
+                               rv = attempt_re_add(fd, tfd, dv, dev_st, tst,
+                                                   rdev, update, devname,
+                                                   verbose, array);
+                               dev_st->ss->free_super(dev_st);
+                               if (rv)
+                                       return rv;
+                       }
                }
                if (dv->disposition == 'M') {
                        if (verbose > 0)
@@ -869,10 +892,10 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                                        continue;
                                if (disc.major == 0 && disc.minor == 0)
                                        continue;
-                               found++;
                                if (!(disc.state & (1<<MD_DISK_SYNC)))
                                        continue;
                                avail[disc.raid_disk] = 1;
+                               found++;
                        }
                        array_failed = !enough(array->level, array->raid_disks,
                                               array->layout, 1, avail);
@@ -920,10 +943,42 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
        else
                disc.number = raid_slot;
        disc.state = 0;
+
+       /* 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);
+               if (!mdp) {
+                       pr_err("%s unable to read array state.\n", devname);
+                       return -1;
+               }
+
+               if (strncmp(mdp->sysfs_array_state, "readonly", 8) != 0) {
+                       sysfs_free(mdp);
+                       pr_err("%s is not readonly, cannot add journal.\n", devname);
+                       return -1;
+               }
+
+               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;
+       }
+
        if (array->not_persistent==0) {
                int dfd;
-               if (dv->writemostly == 1)
+               if (dv->disposition == 'j')
+                       disc.state |= (1 << MD_DISK_JOURNAL) | (1 << MD_DISK_SYNC);
+               if (dv->writemostly == FlagSet)
                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
+               if (dv->failfast == FlagSet)
+                       disc.state |= 1 << MD_DISK_FAILFAST;
                dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
                if (tst->ss->add_to_super(tst, &disc, dfd,
                                          dv->devname, INVALID_SECTORS))
@@ -967,8 +1022,10 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
                        disc.state |= (1 << MD_DISK_CLUSTER_ADD);
        }
 
-       if (dv->writemostly == 1)
+       if (dv->writemostly == FlagSet)
                disc.state |= (1 << MD_DISK_WRITEMOSTLY);
+       if (dv->failfast == FlagSet)
+               disc.state |= (1 << MD_DISK_FAILFAST);
        if (tst->ss->external) {
                /* add a disk
                 * to an external metadata container */
@@ -1032,10 +1089,20 @@ int Manage_add(int fd, int tfd, struct mddev_dev *dv,
        } else {
                tst->ss->free_super(tst);
                if (ioctl(fd, ADD_NEW_DISK, &disc)) {
-                       pr_err("add new device failed for %s as %d: %s\n",
-                              dv->devname, j, strerror(errno));
+                       if (dv->disposition == 'j')
+                               pr_err("Failed to hot add %s as journal, "
+                                      "please try restart %s.\n", dv->devname, devname);
+                       else
+                               pr_err("add new device failed for %s as %d: %s\n",
+                                      dv->devname, j, strerror(errno));
                        return -1;
                }
+               if (dv->disposition == 'j') {
+                       pr_err("Journal added successfully, making %s read-write\n", devname);
+                       if (Manage_ro(devname, fd, -1))
+                               pr_err("Failed to make %s read-write\n", devname);
+               }
+
        }
        if (verbose >= 0)
                pr_err("added %s\n", dv->devname);
@@ -1043,7 +1110,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;
@@ -1075,19 +1142,34 @@ int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
                 */
                if (rdev == 0)
                        ret = -1;
-               else
-                       ret = sysfs_unique_holder(devnm, rdev);
-               if (ret == 0) {
-                       pr_err("%s is not a member, cannot remove.\n",
-                              dv->devname);
-                       close(lfd);
-                       return -1;
-               }
-               if (ret >= 2) {
-                       pr_err("%s is still in use, cannot remove.\n",
-                              dv->devname);
-                       close(lfd);
-                       return -1;
+               else {
+                       /*
+                        * The drive has already been set to 'faulty', however
+                        * monitor might not have had time to process it and the
+                        * drive might still have an entry in the 'holders'
+                        * directory. Try a few times to avoid a false error
+                        */
+                       int count = 20;
+
+                       do {
+                               ret = sysfs_unique_holder(devnm, rdev);
+                               if (ret < 2)
+                                       break;
+                               usleep(100 * 1000);     /* 100ms */
+                       } while (--count > 0);
+
+                       if (ret == 0) {
+                               pr_err("%s is not a member, cannot remove.\n",
+                                       dv->devname);
+                               close(lfd);
+                               return -1;
+                       }
+                       if (ret >= 2) {
+                               pr_err("%s is still in use, cannot remove.\n",
+                                       dv->devname);
+                               close(lfd);
+                               return -1;
+                       }
                }
        }
        /* FIXME check that it is a current member */
@@ -1095,13 +1177,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 = ioctl(fd, HOT_REMOVE_DISK, rdev);
+               err = hot_remove_disk(fd, rdev, force);
                if (err && errno == ENODEV) {
                        /* Old kernels rejected this if no personality
                         * is registered */
@@ -1118,8 +1196,7 @@ int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
                                                    "state", "remove");
                        else
                                err = -1;
-                       if (sra)
-                               sysfs_free(sra);
+                       sysfs_free(sra);
                }
        }
        if (err) {
@@ -1268,6 +1345,7 @@ int Manage_subdevs(char *devname, int fd,
         *         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
@@ -1300,13 +1378,13 @@ int Manage_subdevs(char *devname, int fd,
        int sysfd = -1;
        int count = 0; /* number of actions taken */
        struct mdinfo info;
+       struct mdinfo devinfo;
        int frozen = 0;
        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 (md_get_array_info(fd, &array)) {
+               pr_err("Cannot get array info for %s\n", devname);
                goto abort;
        }
        sysfs_init(&info, fd, NULL);
@@ -1365,7 +1443,7 @@ int Manage_subdevs(char *devname, int fd,
                }
 
                if (strcmp(dv->devname, "missing") == 0) {
-                       struct mddev_dev *add_devlist = NULL;
+                       struct mddev_dev *add_devlist;
                        struct mddev_dev **dp;
                        if (dv->disposition == 'c') {
                                rv = ioctl(fd, CLUSTERED_DISK_NACK, NULL);
@@ -1459,9 +1537,10 @@ int Manage_subdevs(char *devname, int fd,
                } else {
                        struct stat stb;
                        tfd = dev_open(dv->devname, O_RDONLY);
-                       if (tfd >= 0)
+                       if (tfd >= 0) {
                                fstat(tfd, &stb);
-                       else {
+                               close(tfd);
+                       } else {
                                int open_err = errno;
                                if (stat(dv->devname, &stb) != 0) {
                                        pr_err("Cannot find %s: %s\n",
@@ -1499,6 +1578,7 @@ int Manage_subdevs(char *devname, int fd,
                        goto abort;
                case 'a':
                case 'S': /* --add-spare */
+               case 'j': /* --add-journal */
                case 'A':
                case 'M': /* --re-add missing */
                case 'F': /* --re-add faulty  */
@@ -1508,9 +1588,21 @@ int Manage_subdevs(char *devname, int fd,
                                pr_err("Cannot add disks to a \'member\' array, perform this operation on the parent container\n");
                                goto abort;
                        }
+
+                       /* Let's first try to write re-add to sysfs */
+                       if (rdev != 0 &&
+                           (dv->disposition == 'A' || dv->disposition == 'F')) {
+                               sysfs_init_dev(&devinfo, rdev);
+                               if (sysfs_set_str(&info, &devinfo, "state", "re-add") == 0) {
+                                       pr_err("re-add %s to %s succeed\n",
+                                               dv->devname, info.sys_name);
+                                       break;
+                               }
+                       }
+
                        if (dv->disposition == 'F')
                                /* Need to remove first */
-                               ioctl(fd, HOT_REMOVE_DISK, 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) {
@@ -1552,7 +1644,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);
@@ -1704,7 +1796,8 @@ int move_spare(char *from_devname, char *to_devname, dev_t devid)
 
        devlist.next = NULL;
        devlist.used = 0;
-       devlist.writemostly = 0;
+       devlist.writemostly = FlagDefault;
+       devlist.failfast = FlagDefault;
        devlist.devname = devname;
        sprintf(devname, "%d:%d", major(devid), minor(devid));