From 56d1885944b25cc186ad18827b62e14c49b3c3b5 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Wed, 7 Mar 2012 10:41:24 +1100 Subject: [PATCH] Assemble: don't use O_EXCL until we have checked device content. 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 --- Assemble.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Assemble.c b/Assemble.c index 13adfc3..0af8ad8 100644 --- a/Assemble.c +++ b/Assemble.c @@ -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)