mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] An option to make fcntl & flock locks fair
@ 2002-08-22 14:18 Matthew Wilcox
  0 siblings, 0 replies; only message in thread
From: Matthew Wilcox @ 2002-08-22 14:18 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: linux-kernel


Shlomi Fish asked about including first-come, first-served style locking
for posix and flock locks.  After some back-and-forth, we came up with
the following patch which seems unintrusive enough to bother including.
Personally, I doubt the utility of this, but someone might have an
application for it, and the code's already written.

diff -urpNX dontdiff linux-2.5.31/fs/locks.c linux-2.5.31-willy/fs/locks.c
--- linux-2.5.31/fs/locks.c	2002-08-01 14:16:39.000000000 -0700
+++ linux-2.5.31-willy/fs/locks.c	2002-08-21 08:29:29.000000000 -0700
@@ -126,6 +126,14 @@
 #include <asm/semaphore.h>
 #include <asm/uaccess.h>
 
+/* Defining FAIR_LOCKS prevents readers from starving writers.  I'm not
+ * sure anyone has a usage pattern where this is actually a problem, but
+ * it's a trivial change to allow you to enable this, so I added it.
+ * Note that we record the owner and check it first, so you can't deadlock
+ * on an attempt to modify your own lock.
+ */
+#undef FAIR_LOCKS
+
 #define IS_POSIX(fl)	(fl->fl_flags & FL_POSIX)
 #define IS_FLOCK(fl)	(fl->fl_flags & FL_FLOCK)
 #define IS_LEASE(fl)	(fl->fl_flags & FL_LEASE)
@@ -503,24 +508,22 @@ static void locks_delete_lock(struct fil
 	locks_free_lock(fl);
 }
 
-/* Determine if lock sys_fl blocks lock caller_fl. Common functionality
- * checks for shared/exclusive status of overlapping locks.
+/*
+ * Determine if lock sys_fl blocks lock caller_fl.  Write locks require
+ * exclusive access, read locks can share.  If you define FAIR_LOCKS,
+ * readers can only share until a writer comes along and blocks.
  */
 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
 {
-	switch (caller_fl->fl_type) {
-	case F_RDLCK:
-		return (sys_fl->fl_type == F_WRLCK);
-
-	case F_WRLCK:
-		return (1);
-
-	default:
-		printk(KERN_ERR "locks_conflict(): impossible lock type - %d\n",
-		       caller_fl->fl_type);
-		break;
-	}
-	return (0);	/* This should never happen */
+	if (caller_fl->fl_type == F_WRLCK)
+		return 1;
+	if (sys_fl->fl_type == F_WRLCK)
+		return 1;
+#ifdef FAIR_LOCKS
+	if (!list_empty(&sys_fl->fl_block))
+		return 1;
+#endif
+	return 0;
 }
 
 /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific

-- 
Revolutions do not require corporate support.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2002-08-22 14:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-22 14:18 [PATCH] An option to make fcntl & flock locks fair Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®