]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
Factor out add-disk code
[thirdparty/mdadm.git] / Manage.c
index 09d397b7c9fc98535aedbbb9ed8fca28bc116954..a6e1072502c4f7e2c998094605fd1492faecf2e9 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -45,11 +45,57 @@ int Manage_ro(char *devname, int fd, int readonly)
         *
         */
        mdu_array_info_t array;
+#ifndef MDASSEMBLE
+       struct mdinfo *mdi;
+#endif
 
        if (md_get_version(fd) < 9000) {
                fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
                return 1;
        }
+#ifndef MDASSEMBLE
+       /* If this is an externally-manage array, we need to modify the
+        * metadata_version so that mdmon doesn't undo our change.
+        */
+       mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
+       if (mdi &&
+           mdi->array.major_version == -1 &&
+           mdi->array.level > 0 &&
+           is_subarray(mdi->text_version)) {
+               char vers[64];
+               strcpy(vers, "external:");
+               strcat(vers, mdi->text_version);
+               if (readonly > 0) {
+                       int rv;
+                       /* We set readonly ourselves. */
+                       vers[9] = '-';
+                       sysfs_set_str(mdi, NULL, "metadata_version", vers);
+
+                       close(fd);
+                       rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
+
+                       if (rv < 0) {
+                               fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
+                                       devname, strerror(errno));
+
+                               vers[9] = mdi->text_version[0];
+                               sysfs_set_str(mdi, NULL, "metadata_version", vers);
+                               return 1;
+                       }
+               } else {
+                       char *cp;
+                       /* We cannot set read/write - must signal mdmon */
+                       vers[9] = '/';
+                       sysfs_set_str(mdi, NULL, "metadata_version", vers);
+
+                       cp = strchr(vers+10, '/');
+                       if (*cp)
+                               *cp = 0;
+                       ping_monitor(vers+10);
+               }
+               return 0;
+       }
+#endif
        if (ioctl(fd, GET_ARRAY_INFO, &array)) {
                fprintf(stderr, Name ": %s does not appear to be active.\n",
                        devname);
@@ -78,13 +124,18 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
 {
        /* Run or stop the array. array must already be configured
         * required >= 0.90.0
+        * Only print failure messages if quiet == 0;
+        * quiet > 0 means really be quiet
+        * quiet < 0 means we will try again if it fails.
         */
        mdu_param_t param; /* unused */
 
        if (runstop == -1 && md_get_version(fd) < 9000) {
                if (ioctl(fd, STOP_MD, 0)) {
-                       if (!quiet) fprintf(stderr, Name ": stopping device %s failed: %s\n",
-                                           devname, strerror(errno));
+                       if (quiet == 0) fprintf(stderr,
+                                               Name ": stopping device %s "
+                                               "failed: %s\n",
+                                               devname, strerror(errno));
                        return 1;
                }
        }
@@ -111,15 +162,58 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
        } else if (runstop < 0){
                struct map_ent *map = NULL;
                struct stat stb;
-               if (ioctl(fd, STOP_ARRAY, NULL)) {
-                       if (quiet==0)
-                               fprintf(stderr, Name ": fail to stop array %s: %s\n",
+               struct mdinfo *mdi;
+               /* If this is an mdmon managed array, just write 'inactive'
+                * to the array state and let mdmon clear up.
+                */
+               mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
+               if (mdi &&
+                   mdi->array.level > 0 &&
+                   is_subarray(mdi->text_version)) {
+                       /* This is mdmon managed. */
+                       close(fd);
+                       if (sysfs_set_str(mdi, NULL,
+                                         "array_state", "inactive") < 0) {
+                               if (quiet == 0)
+                                       fprintf(stderr, Name
+                                               ": failed to stop array %s: %s\n",
+                                               devname, strerror(errno));
+                               return 1;
+                       }
+
+                       /* Give monitor a chance to act */
+                       ping_monitor(mdi->text_version);
+
+                       fd = open(devname, O_RDONLY);
+               } else if (mdi &&
+                          mdi->array.major_version == -1 &&
+                          mdi->array.minor_version == -2 &&
+                          !is_subarray(mdi->text_version)) {
+                       /* container, possibly mdmon-managed.
+                        * Make sure mdmon isn't opening it, which
+                        * would interfere with the 'stop'
+                        */
+                       ping_monitor(mdi->sys_name);
+               }
+               if (mdi)
+                       sysfs_free(mdi);
+
+               if (fd >= 0 && ioctl(fd, STOP_ARRAY, NULL)) {
+                       if (quiet == 0) {
+                               fprintf(stderr, Name
+                                       ": failed to stop array %s: %s\n",
                                        devname, strerror(errno));
+                               if (errno == EBUSY)
+                                       fprintf(stderr, "Perhaps a running "
+                                               "process, mounted filesystem "
+                                               "or active volume group?\n");
+                       }
                        return 1;
                }
+
                if (quiet <= 0)
                        fprintf(stderr, Name ": stopped %s\n", devname);
-               if (fstat(fd, &stb) == 0) {
+               if (fd >= 0 && fstat(fd, &stb) == 0) {
                        int devnum;
                        if (major(stb.st_rdev) == MD_MAJOR)
                                devnum = minor(stb.st_rdev);
@@ -188,6 +282,7 @@ int Manage_subdevs(char *devname, int fd,
         */
        mdu_array_info_t array;
        mdu_disk_info_t disc;
+       unsigned long long array_size;
        mddev_dev_t dv, next = NULL;
        struct stat stb;
        int j, jnext = 0;
@@ -195,6 +290,7 @@ int Manage_subdevs(char *devname, int fd,
        struct supertype *st, *tst;
        int duuid[4];
        int ouuid[4];
+       int lfd = -1;
 
        if (ioctl(fd, GET_ARRAY_INFO, &array)) {
                fprintf(stderr, Name ": cannot get array info for %s\n",
@@ -202,8 +298,15 @@ int Manage_subdevs(char *devname, int fd,
                return 1;
        }
 
-       tst = super_by_version(array.major_version,
-                              array.minor_version);
+       /* array.size is only 32 bit and may be truncated.
+        * So read from sysfs if possible, and record number of sectors
+        */
+
+       array_size = get_component_size(fd);
+       if (array_size <= 0)
+               array_size = array.size * 2;
+
+       tst = super_by_fd(fd);
        if (!tst) {
                fprintf(stderr, Name ": unsupport array - version %d.%d\n",
                        array.major_version, array.minor_version);
@@ -214,6 +317,7 @@ int Manage_subdevs(char *devname, int fd,
                unsigned long long ldsize;
                char dvname[20];
                char *dnprintable = dv->devname;
+               int err;
 
                next = dv->next;
                jnext = 0;
@@ -298,9 +402,14 @@ int Manage_subdevs(char *devname, int fd,
                        return 1;
                case 'a':
                        /* add the device */
-
+                       if (tst->subarray[0]) {
+                               fprintf(stderr, Name ": Cannot add disks to a"
+                                       " \'member\' array, perform this"
+                                       " operation on the parent container\n");
+                               return 1;
+                       }
                        /* Make sure it isn't in use (in 2.6 or later) */
-                       tfd = open(dv->devname, O_RDONLY|O_EXCL);
+                       tfd = open(dv->devname, O_RDONLY|O_EXCL|O_DIRECT);
                        if (tfd < 0) {
                                fprintf(stderr, Name ": Cannot open %s: %s\n",
                                        dv->devname, strerror(errno));
@@ -308,8 +417,7 @@ int Manage_subdevs(char *devname, int fd,
                        }
                        remove_partitions(tfd);
 
-                       st = super_by_version(array.major_version,
-                                             array.minor_version);
+                       st = dup_super(tst);
 
                        if (array.not_persistent==0)
                                st->ss->load_super(st, tfd, NULL);
@@ -320,7 +428,9 @@ int Manage_subdevs(char *devname, int fd,
                        }
                        close(tfd);
 
-                       if (array.major_version == 0 &&
+
+                       if (!tst->ss->external &&
+                           array.major_version == 0 &&
                            md_get_version(fd)%100 < 2) {
                                if (ioctl(fd, HOT_ADD_DISK,
                                          (unsigned long)stb.st_rdev)==0) {
@@ -335,20 +445,23 @@ int Manage_subdevs(char *devname, int fd,
                                return 1;
                        }
 
-                       if (array.not_persistent == 0) {
-
+                       if (array.not_persistent == 0 || tst->ss->external) {
                                /* Make sure device is large enough */
                                if (tst->ss->avail_size(tst, ldsize/512) <
-                                   array.size) {
+                                   array_size) {
                                        fprintf(stderr, Name ": %s not large enough to join array\n",
                                                dv->devname);
                                        return 1;
                                }
 
                                /* need to find a sample superblock to copy, and
-                                * a spare slot to use
+                                * a spare slot to use.
+                                * For 'external' array (well, container based),
+                                * We can just load the metadata for the array.
                                 */
-                               for (j = 0; j < tst->max_devs; j++) {
+                               if (tst->ss->external) {
+                                       tst->ss->load_super(tst, fd, NULL);
+                               } else for (j = 0; j < tst->max_devs; j++) {
                                        char *dev;
                                        int dfd;
                                        disc.number = j;
@@ -370,6 +483,7 @@ int Manage_subdevs(char *devname, int fd,
                                        close(dfd);
                                        break;
                                }
+                               /* FIXME this is a bad test to be using */
                                if (!tst->sb) {
                                        fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
                                        return 1;
@@ -414,7 +528,7 @@ int Manage_subdevs(char *devname, int fd,
                                /* non-persistent. Must ensure that new drive
                                 * is at least array.size big.
                                 */
-                               if (ldsize/512 < array.size) {
+                               if (ldsize/512 < array_size) {
                                        fprintf(stderr, Name ": %s not large enough to join array\n",
                                                dv->devname);
                                        return 1;
@@ -438,12 +552,18 @@ int Manage_subdevs(char *devname, int fd,
                        disc.minor = minor(stb.st_rdev);
                        disc.number =j;
                        disc.state = 0;
-                       if (array.not_persistent==0) {
+                       if (array.not_persistent==0 || tst->ss->external) {
+                               int dfd;
                                if (dv->writemostly)
                                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
-                               tst->ss->add_to_super(tst, &disc);
-                               if (tst->ss->write_init_super(tst, &disc,
-                                                             dv->devname))
+                               dfd = open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
+                               tst->ss->add_to_super(tst, &disc, dfd,
+                                                     dv->devname);
+                               /* write_init_super will close 'dfd' */
+                               if (tst->ss->external)
+                                       /* mdmon will write the metadata */
+                                       close(dfd);
+                               else if (tst->ss->write_init_super(tst))
                                        return 1;
                        } else if (dv->re_add) {
                                /*  this had better be raid1.
@@ -476,7 +596,52 @@ int Manage_subdevs(char *devname, int fd,
                        }
                        if (dv->writemostly)
                                disc.state |= (1 << MD_DISK_WRITEMOSTLY);
-                       if (ioctl(fd,ADD_NEW_DISK, &disc)) {
+                       if (tst->ss->external) {
+                               /* add a disk to an external metadata container
+                                * only if mdmon is around to see it
+                                */
+                               struct mdinfo new_mdi;
+                               struct mdinfo *sra;
+                               int container_fd;
+                               int devnum = fd2devnum(fd);
+
+                               container_fd = open_dev_excl(devnum);
+                               if (container_fd < 0) {
+                                       fprintf(stderr, Name ": add failed for %s:"
+                                               " could not get exclusive access to container\n",
+                                               dv->devname);
+                                       return 1;
+                               }
+
+                               if (!mdmon_running(devnum)) {
+                                       fprintf(stderr, Name ": add failed for %s: mdmon not running\n",
+                                               dv->devname);
+                                       close(container_fd);
+                                       return 1;
+                               }
+
+                               sra = sysfs_read(container_fd, -1, 0);
+                               if (!sra) {
+                                       fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
+                                               dv->devname);
+                                       close(container_fd);
+                                       return 1;
+                               }
+                               sra->array.level = LEVEL_CONTAINER;
+                               /* Need to set data_offset and component_size */
+                               tst->ss->getinfo_super(tst, &new_mdi);
+                               new_mdi.disk.major = disc.major;
+                               new_mdi.disk.minor = disc.minor;
+                               if (sysfs_add_disk(sra, &new_mdi) != 0) {
+                                       fprintf(stderr, Name ": add new device to external metadata"
+                                               " failed for %s\n", dv->devname);
+                                       close(container_fd);
+                                       return 1;
+                               }
+                               ping_monitor(devnum2devname(devnum));
+                               sysfs_free(sra);
+                               close(container_fd);
+                       } else if (ioctl(fd, ADD_NEW_DISK, &disc)) {
                                fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
                                        dv->devname, j, strerror(errno));
                                return 1;
@@ -487,13 +652,87 @@ int Manage_subdevs(char *devname, int fd,
 
                case 'r':
                        /* hot remove */
+                       if (tst->subarray[0]) {
+                               fprintf(stderr, Name ": Cannot remove disks from a"
+                                       " \'member\' array, perform this"
+                                       " operation on the parent container\n");
+                               return 1;
+                       }
+                       if (tst->ss->external) {
+                               /* To remove a device from a container, we must
+                                * check that it isn't in use in an array.
+                                * This involves looking in the 'holders'
+                                * directory - there must be just one entry,
+                                * the container.
+                                * To ensure that it doesn't get used as a
+                                * hold spare while we are checking, we
+                                * get an O_EXCL open on the container
+                                */
+                               int dnum = fd2devnum(fd);
+                               lfd = open_dev_excl(dnum);
+                               if (lfd < 0) {
+                                       fprintf(stderr, Name
+                                               ": Cannot get exclusive access "
+                                               " to container - odd\n");
+                                       return 1;
+                               }
+                               if (!sysfs_unique_holder(dnum, stb.st_rdev)) {
+                                       fprintf(stderr, Name
+                                               ": %s is %s, cannot remove.\n",
+                                               dnprintable,
+                                               errno == EEXIST ? "still in use":
+                                               "not a member");
+                                       close(lfd);
+                                       return 1;
+                               }
+                       }
                        /* FIXME check that it is a current member */
-                       if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
+                       err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
+                       if (err && errno == ENODEV) {
+                               /* Old kernels rejected this if no personality
+                                * registered */
+                               struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
+                               struct mdinfo *dv = NULL;
+                               if (sra)
+                                       dv = sra->devs;
+                               for ( ; dv ; dv=dv->next)
+                                       if (dv->disk.major == major(stb.st_rdev) &&
+                                           dv->disk.minor == minor(stb.st_rdev))
+                                               break;
+                               if (dv)
+                                       err = sysfs_set_str(sra, dv,
+                                                           "state", "remove");
+                               else
+                                       err = -1;
+                               if (sra)
+                                       sysfs_free(sra);
+                       }
+                       if (err) {
                                fprintf(stderr, Name ": hot remove failed "
                                        "for %s: %s\n", dnprintable,
                                        strerror(errno));
+                               if (lfd >= 0)
+                                       close(lfd);
                                return 1;
                        }
+                       if (tst->ss->external) {
+                               /*
+                                * Before dropping our exclusive open we make an
+                                * attempt at preventing mdmon from seeing an
+                                * 'add' event before reconciling this 'remove'
+                                * event.
+                                */
+                               char *name = devnum2devname(fd2devnum(fd));
+
+                               if (!name) {
+                                       fprintf(stderr, Name ": unable to get container name\n");
+                                       return 1;
+                               }
+
+                               ping_manager(name);
+                               free(name);
+                       }
+                       close(lfd);
                        if (verbose >= 0)
                                fprintf(stderr, Name ": hot removed %s\n",
                                        dnprintable);