[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [lvm-devel] [PATCH] DRAFT! write lock priority



Hi,

Alasdair G Kergon <agk redhat com> writes:
> Updated version of the second patch.
> Handing back over to you for further cleanup, review & testing.
Another iteration of second patch attached. This should mostly address the
raised concerns, and also adds documentation to example.conf. It still uses the
aux_ prefix, see also my other mail.

Index: doc/example.conf
===================================================================
RCS file: /cvs/lvm2/LVM2/doc/example.conf,v
retrieving revision 1.45
diff -u -p -r1.45 example.conf
--- doc/example.conf	4 Aug 2009 15:36:14 -0000	1.45
+++ doc/example.conf	13 Aug 2009 14:54:19 -0000
@@ -288,6 +288,13 @@ global {
     # in progress.  A directory like /tmp that may get wiped on reboot is OK.
     locking_dir = "/var/lock/lvm"
 
+    # Whenever a read-only and read-write access compete on a single volume
+    # group, ensure that the write lock gets priority over the read
+    # lock. Without this setting, write access may be by high volume of
+    # read-only traffic on LVM metadata. NB. This option only affects
+    # locking_type = 1, i.e. local file-based locking.
+    prioritise_write_locks = 1
+
     # Other entries can go here to allow you to load shared libraries
     # e.g. if support for LVM1 metadata was compiled as a shared library use
     #   format_libraries = "liblvm2format1.so" 
Index: lib/config/defaults.h
===================================================================
RCS file: /cvs/lvm2/LVM2/lib/config/defaults.h,v
retrieving revision 1.50
diff -u -p -r1.50 defaults.h
--- lib/config/defaults.h	4 Aug 2009 15:36:14 -0000	1.50
+++ lib/config/defaults.h	13 Aug 2009 14:54:19 -0000
@@ -42,6 +42,7 @@
 #define DEFAULT_FALLBACK_TO_LOCAL_LOCKING 1
 #define DEFAULT_FALLBACK_TO_CLUSTERED_LOCKING 1
 #define DEFAULT_WAIT_FOR_LOCKS 1
+#define DEFAULT_PRIORITISE_WRITE_LOCKS 1
 
 #define DEFAULT_MIRRORLOG "disk"
 #define DEFAULT_MIRROR_LOG_FAULT_POLICY "allocate"
Index: lib/locking/file_locking.c
===================================================================
RCS file: /cvs/lvm2/LVM2/lib/locking/file_locking.c,v
retrieving revision 1.42
diff -u -p -r1.42 file_locking.c
--- lib/locking/file_locking.c	13 Aug 2009 13:23:52 -0000	1.42
+++ lib/locking/file_locking.c	13 Aug 2009 14:54:19 -0000
@@ -38,6 +38,7 @@ struct lock_list {
 
 static struct dm_list _lock_list;
 static char _lock_dir[NAME_LEN];
+static int _prioritise_write_locks;
 
 static sig_t _oldhandler;
 static sigset_t _fullsigset, _intsigset;
@@ -47,6 +48,7 @@ static void _undo_flock(const char *file
 {
 	struct stat buf1, buf2;
 
+	log_debug("_undo_flock %s", file);
 	if (!flock(fd, LOCK_NB | LOCK_EX) &&
 	    !stat(file, &buf1) &&
 	    !fstat(fd, &buf2) &&
@@ -135,6 +137,8 @@ static int _do_flock(const char *file, i
 	int old_errno;
 	struct stat buf1, buf2;
 
+	log_debug("_do_flock %s %c%c",
+		  file, operation == LOCK_EX ? 'W' : 'R', nonblock ? ' ' : 'B');
 	do {
 		if ((*fd > -1) && close(*fd))
 			log_sys_error("close", file);
@@ -169,6 +173,33 @@ static int _do_flock(const char *file, i
 	return_0;
 }
 
+#define AUX_LOCK_PREFIX "aux_"
+
+static int _do_write_priority_flock(const char *file, int *fd, int operation, uint32_t nonblock)
+{
+	int r, fd_aux = -1;
+	char *file_aux = alloca(strlen(file) + sizeof(AUX_LOCK_PREFIX) + 1);
+
+	strcpy(file_aux, file);
+	/* There is always a slash before the basename part of the filename. */
+	*(strrchr(file_aux, '/') + 1) = 0;
+	/* For file /path/to/file, file_aux ends up being /path/to/aux_file */
+	strcat(file_aux, AUX_LOCK_PREFIX);
+	strcat(file_aux, strrchr(file, '/') + 1);
+
+	if ((r = _do_flock(file_aux, &fd_aux, LOCK_EX, 0))) {
+		if (operation == LOCK_EX) {
+			r = _do_flock(file, fd, operation, nonblock);
+			_undo_flock(file_aux, fd_aux);
+		} else {
+			_undo_flock(file_aux, fd_aux);
+			r = _do_flock(file, fd, operation, nonblock);
+		}
+	}
+
+	return r;
+}
+
 static int _lock_file(const char *file, uint32_t flags)
 {
 	int operation;
@@ -207,7 +238,11 @@ static int _lock_file(const char *file, 
 	log_very_verbose("Locking %s %c%c", ll->res, state,
 			 nonblock ? ' ' : 'B');
 
-	r = _do_flock(file, &ll->lf, operation, nonblock);
+	if (_prioritise_write_locks)
+		r = _do_write_priority_flock(file, &ll->lf, operation, nonblock);
+	else 
+		r = _do_flock(file, &ll->lf, operation, nonblock);
+
 	if (r)
 		dm_list_add(&_lock_list, &ll->list);
 	else {
@@ -299,6 +334,10 @@ int init_file_locking(struct locking_typ
 						DEFAULT_LOCK_DIR),
 		sizeof(_lock_dir));
 
+	_prioritise_write_locks =
+	    find_config_tree_bool(cmd, "global/prioritise_write_locks",
+				  DEFAULT_PRIORITISE_WRITE_LOCKS);
+
 	if (!dm_create_dir(_lock_dir))
 		return 0;
 
Yours,
   Petr.

PS: Sorry for the file_aux vs LOCK_EX screwup on my part, good you noticed and
fixed that part. (At least we know why we are reviewing patches...)

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]