]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
--stop: separate 'is busy' test for 'did it stop properly'.
authorNeilBrown <neilb@suse.de>
Wed, 23 Mar 2011 04:42:24 +0000 (15:42 +1100)
committerNeilBrown <neilb@suse.de>
Wed, 23 Mar 2011 04:42:24 +0000 (15:42 +1100)
Stopping an md array requires that there is no other user of it.
However with udev and udisks and such there can be transient other
users of md devices which can interfere with stopping the array.

If there is a transient users, we really want "mdadm --stop" to wait a
little while and retry.
However if the array is genuinely in-use (e.g. mounted), then we
don't want to wait at all - we want to fail immediately.

So before trying to stop, re-open device with O_EXCL.  If this fails
then the device is probably in use, so give up.

If it succeeds, but a subsequent STOP_ARRAY fails, then it is possibly
a transient failure, so try again for a few seconds.

Signed-off-by: NeilBrown <neilb@suse.de>
Manage.c

index 5932c9031b573c1531882ad925cf96be7e840fbc..3502175dad27d8328eea83d574b4878a9b18a8aa 100644 (file)
--- a/Manage.c
+++ b/Manage.c
@@ -207,10 +207,26 @@ 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:"
+                               " possibly it is still in use.\n",
+                               devname);
+                       return 1;
+               }
                mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
                if (mdi &&
                    mdi->array.level > 0 &&
@@ -229,7 +245,14 @@ int Manage_runstop(char *devname, int fd, int runstop, int quiet)
                        /* 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 &&
@@ -262,7 +285,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; err = 0;
+               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",