Bill Nottingham: Simplistig locking for --incremental.

Martin F. Krafft madduck at alioth.debian.org
Wed Jun 25 15:42:29 UTC 2008


Module: mdadm
Branch: build
Commit: 767bd452981d5fae96487b1394f9d1ff535a3127
URL:    http://git.debian.org/?p=pkg-mdadm/mdadm.git;a=commit;h=767bd452981d5fae96487b1394f9d1ff535a3127

Author: Bill Nottingham <notting at redhat.com>
Date:   Mon May  5 19:44:04 2008 +1000

Simplistig locking for --incremental.

From: Bill Nottingham <notting at redhat.com>

mdadm --incremental doesn't really do any locking. If you get multiple
events in parallel for the same device (that has not yet started), they
will all go down the path to create the array. One will succeed, the
rest will have SET_ARRAY_INFO die with -EBUSY (md: array mdX already has disks!)
and will exit without adding the disk.

Original bug report is: https://bugzilla.redhat.com/show_bug.cgi?id=433932

This is solved by adding very very rudimentary locking. Incremental() now
opens the device with O_EXCL to ensure only one invocation is frobbing the
array at once. A simple loop just tries to open 5 times a second for 5
seconds. If the array stays locked that long, you probably have bigger
issues.

---

 mdopen.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/mdopen.c b/mdopen.c
index ec34e91..7f6a2ea 100644
--- a/mdopen.c
+++ b/mdopen.c
@@ -302,6 +302,7 @@ int open_mddev_devnum(char *devname, int devnum, char *name, char *chosen_name)
 	 */
 	int major_num, minor_num;
 	struct stat stb;
+	int i;
 
 	if (devname)
 		strcpy(chosen_name, devname);
@@ -353,5 +354,17 @@ int open_mddev_devnum(char *devname, int devnum, char *name, char *chosen_name)
 		}
 		/* FIXME chown/chmod ?? */
 	}
-	return open(chosen_name, O_RDWR);
+
+	/* Simple locking to avoid --incr being called for the same
+	 * array multiple times in parallel.
+	 */
+	for (i = 0; i < 25 ; i++) {
+		int fd;
+
+		fd = open(chosen_name, O_RDWR|O_EXCL);
+		if (fd >= 0 || errno != EBUSY)
+			return fd;
+		usleep(200000);
+	}
+	return -1;
 }




More information about the pkg-mdadm-commits mailing list