martin f. krafft: mapfile: fix locking.

Martin F. Krafft madduck at alioth.debian.org
Fri Jan 29 10:53:18 UTC 2010


Module: mdadm
Branch: incremental
Commit: 2d2a56d302d1f0f7ea49299f7a89f24c74cab6b7
URL:    http://git.debian.org/?p=pkg-mdadm/mdadm.git;a=commit;h=2d2a56d302d1f0f7ea49299f7a89f24c74cab6b7

Author: martin f. krafft <madduck at debian.org>
Date:   Fri Jan 29 22:55:42 2010 +1300

mapfile: fix locking.

The current locking uses lockf, and is completely broken.
When you hold a lockf lock, any close of any fd on that file will
release the lock.  So map_read() call which is made as soon as we get
the lock, will immediately drop the lock.

So change to flock locking which isn't so badly designed.

(cherry picked from commit fc7e81e54ec37ece4f1a8ec1729933fc22ec25ff)

Signed-off-by: NeilBrown <neilb at suse.de>
Signed-off-by: martin f. krafft <madduck at debian.org>

---

 debian/changelog |    1 +
 mapfile.c        |    7 +++++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index a01038d..3b9b6ba 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -9,6 +9,7 @@ mdadm (3.1.1-2) UNRELEASED; urgency=low
     sanity checks in the initramfs hook.
   * It makes sense to check that all active devices are in the configuration
     even for incremental mode, so move it to an earlier location.
+  * Cherry-pick fc7e81e from upstream to fix map locking problems.
 
  -- martin f. krafft <madduck at debian.org>  Wed, 27 Jan 2010 14:42:16 +1300
 
diff --git a/mapfile.c b/mapfile.c
index ed59db5..5444452 100644
--- a/mapfile.c
+++ b/mapfile.c
@@ -48,6 +48,7 @@
  * one that we can.
  */
 #include	"mdadm.h"
+#include	<sys/file.h>
 #include	<ctype.h>
 
 #define mapnames(base) { #base, #base ".new", #base ".lock"}
@@ -115,7 +116,7 @@ int map_lock(struct map_ent **melp)
 		lf = open_map(2, &lwhich);
 		if (lf == NULL)
 			return -1;
-		if (lockf(fileno(lf), F_LOCK, 0) != 0) {
+		if (flock(fileno(lf), LOCK_EX) != 0) {
 			fclose(lf);
 			lf = NULL;
 			return -1;
@@ -129,8 +130,10 @@ int map_lock(struct map_ent **melp)
 
 void map_unlock(struct map_ent **melp)
 {
-	if (lf)
+	if (lf) {
+		flock(fileno(lf), LOCK_UN);
 		fclose(lf);
+	}
 	unlink(mapname[lwhich][2]);
 	lf = NULL;
 }




More information about the pkg-mdadm-commits mailing list