]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Assemble: don't use O_EXCL until we have checked device content.
authorNeilBrown <neilb@suse.de>
Tue, 6 Mar 2012 23:41:24 +0000 (10:41 +1100)
committerNeilBrown <neilb@suse.de>
Tue, 6 Mar 2012 23:41:24 +0000 (10:41 +1100)
If we open with O_EXCL before checking that the device is one that
we really want, then that could cause some other process to think
the device is busy when it isn't really.

This particularly affects running "mdadm -A devname" in parallel for
different arrays.  One might be looking at a device that it won't
end up using while another trys and fails to look at a device that
it needs.

So delay the O_EXCL until after all identity checks.

Multiple "mdadm -As" will still have races, but that is fundamentally
racy anyway.

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

index 13adfc3bde5dd92bc75b0837eca76b1535fb891d..0af8ad86f8119b6f5d15e1c7174605814f1231e5 100644 (file)
@@ -310,7 +310,7 @@ int Assemble(struct supertype *st, char *mddev,
 
                tst = dup_super(st);
 
-               dfd = dev_open(devname, O_RDONLY|O_EXCL);
+               dfd = dev_open(devname, O_RDONLY);
                if (dfd < 0) {
                        if (report_missmatch)
                                fprintf(stderr, Name ": cannot open device %s: %s\n",
@@ -408,6 +408,17 @@ int Assemble(struct supertype *st, char *mddev,
                        /* tmpdev is a container.  We need to be either
                         * looking for a member, or auto-assembling
                         */
+                       /* should be safe to try an exclusive open now, we
+                        * have rejected anything that some other mdadm might
+                        * be looking at
+                        */
+                       dfd = dev_open(devname, O_RDONLY | O_EXCL);
+                       if (dfd < 0) {
+                               if (report_missmatch)
+                                       fprintf(stderr, Name ": %s is busy - skipping\n", devname);
+                               goto loop;
+                       }
+                       close(dfd);
 
                        if (ident->container) {
                                if (ident->container[0] == '/' &&
@@ -492,6 +503,18 @@ int Assemble(struct supertype *st, char *mddev,
                                           report_missmatch ? devname : NULL))
                                goto loop;
                                
+                       /* should be safe to try an exclusive open now, we
+                        * have rejected anything that some other mdadm might
+                        * be looking at
+                        */
+                       dfd = dev_open(devname, O_RDONLY | O_EXCL);
+                       if (dfd < 0) {
+                               if (report_missmatch)
+                                       fprintf(stderr, Name ": %s is busy - skipping\n", devname);
+                               goto loop;
+                       }
+                       close(dfd);
+
                        if (st == NULL)
                                st = dup_super(tst);
                        if (st->minor_version == -1)