]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Manage.c
platform_intel: support EFI SCU OEM variable
[thirdparty/mdadm.git] / Manage.c
index 8c86a5328abf5750b8b3b69422164010f74b1a0a..0a966a861ccb5f323e1d2d10eefdee593f890984 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -208,29 +208,63 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
                struct stat stb;
                struct mdinfo *mdi;
                int devnum;
+               int err;
+               int count;
                /* If this is an mdmon managed array, just write 'inactive'
                 * to the array state and let mdmon clear up.
                 */
                devnum = fd2devnum(fd);
+               /* Get EXCL access first.  If this fails, then attempting
+                * to stop is probably a bad idea.
+                */
+               close(fd);
+               fd = open(devname, O_RDONLY|O_EXCL);
+               if (fd < 0 || fd2devnum(fd) != devnum) {
+                       if (fd >= 0)
+                               close(fd);
+                       fprintf(stderr,
+                               Name ": Cannot get exclusive access to %s:"
+                               "Perhaps a running "
+                               "process, mounted filesystem "
+                               "or active volume group?\n",
+                               devname);
+                       return 1;
+               }
                mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
                if (mdi &&
                    mdi->array.level > 0 &&
                    is_subarray(mdi->text_version)) {
+                       int err;
                        /* 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));
+
+                       count = 25;
+                       while (count &&
+                              (err = sysfs_set_str(mdi, NULL,
+                                                   "array_state",
+                                                   "inactive")) < 0
+                              && errno == EBUSY) {
+                               usleep(200000);
+                               count--;
+                       }
+                       if (err && !quiet) {
+                               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);
+                       fd = open_dev_excl(devnum);
+                       if (fd < 0) {
+                               fprintf(stderr, Name
+                                       ": failed to completely stop %s"
+                                       ": Device is busy\n",
+                                       devname);
+                               return 1;
+                       }
                } else if (mdi &&
                           mdi->array.major_version == -1 &&
                           mdi->array.minor_version == -2 &&
@@ -263,7 +297,18 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
                                }
                }
 
-               if (fd >= 0 && ioctl(fd, STOP_ARRAY, NULL)) {
+               /* As we have an O_EXCL open, any use of the device
+                * which blocks STOP_ARRAY is probably a transient use,
+                * so it is reasonable to retry for a while - 5 seconds.
+                */
+               count = 25;
+               while (count && fd >= 0
+                      && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0
+                      && errno == EBUSY) {
+                       usleep(200000);
+                       count --;
+               }
+               if (fd >= 0 && err) {
                        if (quiet == 0) {
                                fprintf(stderr, Name
                                        ": failed to stop array %s: %s\n",
@@ -637,7 +682,7 @@ int Manage_subdevs(char *devname, int fd,
                                /* FIXME this is a bad test to be using */
                                if (!tst->sb) {
                                        close(tfd);
-                                       fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
+                                       fprintf(stderr, Name ": cannot load array metadata from %s\n", devname);
                                        return 1;
                                }
 
@@ -701,7 +746,7 @@ int Manage_subdevs(char *devname, int fd,
                                                                        st, NULL, update,
                                                                        devname, verbose, 0, NULL);
                                                        if (rv == 0)
-                                                               rv = tst->ss->store_super(st, tfd);
+                                                               rv = st->ss->store_super(st, tfd);
                                                        close(tfd);
                                                        tfd = -1;
                                                        if (rv != 0) {
@@ -796,14 +841,11 @@ int Manage_subdevs(char *devname, int fd,
                        disc.minor = minor(stb.st_rdev);
                        disc.number =j;
                        disc.state = 0;
-                       if (array.not_persistent==0 || tst->ss->external) {
+                       if (array.not_persistent==0) {
                                int dfd;
                                if (dv->writemostly == 1)
                                        disc.state |= 1 << MD_DISK_WRITEMOSTLY;
                                dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
-                               if (tst->ss->external &&
-                                   mdmon_running(tst->container_dev))
-                                       tst->update_tail = &tst->updates;
                                if (tst->ss->add_to_super(tst, &disc, dfd,
                                                          dv->devname)) {
                                        close(dfd);
@@ -852,6 +894,7 @@ int Manage_subdevs(char *devname, int fd,
                                struct mdinfo *sra;
                                int container_fd;
                                int devnum = fd2devnum(fd);
+                               int dfd;
 
                                container_fd = open_dev_excl(devnum);
                                if (container_fd < 0) {
@@ -862,6 +905,20 @@ int Manage_subdevs(char *devname, int fd,
                                        return 1;
                                }
 
+                               dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
+                               if (mdmon_running(tst->container_dev))
+                                       tst->update_tail = &tst->updates;
+                               if (tst->ss->add_to_super(tst, &disc, dfd,
+                                                         dv->devname)) {
+                                       close(dfd);
+                                       close(container_fd);
+                                       return 1;
+                               }
+                               if (tst->update_tail)
+                                       flush_metadata_updates(tst);
+                               else
+                                       tst->ss->sync_metadata(tst);
+
                                sra = sysfs_read(container_fd, -1, 0);
                                if (!sra) {
                                        fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
@@ -886,7 +943,7 @@ int Manage_subdevs(char *devname, int fd,
                                        sysfs_free(sra);
                                        return 1;
                                }
-                               ping_monitor(devnum2devname(devnum));
+                               ping_monitor_by_id(devnum);
                                sysfs_free(sra);
                                close(container_fd);
                        } else {