linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* VFS,fs/locks.c,NFSD4: add race_free posix_lock_file_conf() interface
@ 2006-03-21 17:16 Trond Myklebust
  2006-03-21 17:29 ` Trond Myklebust
  0 siblings, 1 reply; 3+ messages in thread
From: Trond Myklebust @ 2006-03-21 17:16 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: linux-kernel, Linux Filesystem Development, Neil Brown

Author: Andy Adamson <andros@citi.umich.edu>

Lockd and the NFSv4 server both exercise a race condition where
posix_test_lock() is called either before or after posix_lock_file()
to deal with a denied lock request due to a conflicting lock. 

Remove the race condition for the NFSv4 server by adding a new conflicting lock 
parameter to __posix_lock_file() , changing the name to 
__posix_lock_file_conf().

Keep posix_lock_file() interface, add posix_lock_conf() interface, both 
call __posix_lock_file_conf().

Signed-off-by: Andy Adamson <andros@citi.umich.edu>
Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
---

 fs/locks.c         |   28 ++++++++++++++++++++++------
 include/linux/fs.h |    1 +
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/fs/locks.c b/fs/locks.c
index 56f996e..1b4b899 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -798,8 +798,9 @@ out:
 }
 
 EXPORT_SYMBOL(posix_lock_file);
+EXPORT_SYMBOL(posix_lock_file_conf);
 
-static int __posix_lock_file(struct inode *inode, struct file_lock *request)
+static int __posix_lock_file_conf(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
 {
 	struct file_lock *fl;
 	struct file_lock *new_fl, *new_fl2;
@@ -823,6 +824,8 @@ static int __posix_lock_file(struct inod
 				continue;
 			if (!posix_locks_conflict(request, fl))
 				continue;
+			if (conflock)
+				locks_copy_lock(conflock, fl);
 			error = -EAGAIN;
 			if (!(request->fl_flags & FL_SLEEP))
 				goto out;
@@ -992,7 +995,20 @@ static int __posix_lock_file(struct inod
  */
 int posix_lock_file(struct file *filp, struct file_lock *fl)
 {
-	return __posix_lock_file(filp->f_dentry->d_inode, fl);
+	return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, NULL);
+}
+
+/**
+ * posix_lock_file_conf - Apply a POSIX-style lock to a file
+ * @filp: The file to apply the lock to
+ * @fl: The lock to be applied
+ * @conflock: Place to return a copy of the conflicting lock, if found.
+ *
+ * Except for the conflock parameter, acts just like posix_lock_file.
+ */
+int posix_lock_file_conf(struct file *filp, struct file_lock *fl, struct file_lock *conflock)
+{
+	return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, conflock);
 }
 
 /**
@@ -1009,7 +1025,7 @@ int posix_lock_file_wait(struct file *fi
 	int error;
 	might_sleep ();
 	for (;;) {
-		error = __posix_lock_file(filp->f_dentry->d_inode, fl);
+		error = posix_lock_file(filp, fl);
 		if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
 			break;
 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
@@ -1081,7 +1097,7 @@ int locks_mandatory_area(int read_write,
 	fl.fl_end = offset + count - 1;
 
 	for (;;) {
-		error = __posix_lock_file(inode, &fl);
+		error = __posix_lock_file_conf(inode, &fl, NULL);
 		if (error != -EAGAIN)
 			break;
 		if (!(fl.fl_flags & FL_SLEEP))
@@ -1694,7 +1710,7 @@ again:
 		error = filp->f_op->lock(filp, cmd, file_lock);
 	else {
 		for (;;) {
-			error = __posix_lock_file(inode, file_lock);
+			error = posix_lock_file(filp, file_lock);
 			if ((error != -EAGAIN) || (cmd == F_SETLK))
 				break;
 			error = wait_event_interruptible(file_lock->fl_wait,
@@ -1837,7 +1853,7 @@ again:
 		error = filp->f_op->lock(filp, cmd, file_lock);
 	else {
 		for (;;) {
-			error = __posix_lock_file(inode, file_lock);
+			error = posix_lock_file(filp, file_lock);
 			if ((error != -EAGAIN) || (cmd == F_SETLK64))
 				break;
 			error = wait_event_interruptible(file_lock->fl_wait,
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5dc0fa2..0824e6e 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -752,6 +752,7 @@ extern void locks_copy_lock(struct file_
 extern void locks_remove_posix(struct file *, fl_owner_t);
 extern void locks_remove_flock(struct file *);
 extern int posix_test_lock(struct file *, struct file_lock *, struct file_lock *);
+extern int posix_lock_file_conf(struct file *, struct file_lock *, struct file_lock *);
 extern int posix_lock_file(struct file *, struct file_lock *);
 extern int posix_lock_file_wait(struct file *, struct file_lock *);
 extern int posix_unblock_lock(struct file *, struct file_lock *);




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: VFS,fs/locks.c,NFSD4: add race_free posix_lock_file_conf() interface
  2006-03-21 17:16 VFS,fs/locks.c,NFSD4: add race_free posix_lock_file_conf() interface Trond Myklebust
@ 2006-03-21 17:29 ` Trond Myklebust
  2006-03-21 17:30   ` Trond Myklebust
  0 siblings, 1 reply; 3+ messages in thread
From: Trond Myklebust @ 2006-03-21 17:29 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: linux-kernel, Linux Filesystem Development, Neil Brown

On Tue, 2006-03-21 at 12:16 -0500, Trond Myklebust wrote:
> Author: Andy Adamson <andros@citi.umich.edu>
> 

Oops... That should have been "From: Andy Adamson", shouldn't it.

Sorry. I'll resend that one...

Cheers,
  Trond


^ permalink raw reply	[flat|nested] 3+ messages in thread

* VFS,fs/locks.c,NFSD4: add race_free posix_lock_file_conf() interface
  2006-03-21 17:29 ` Trond Myklebust
@ 2006-03-21 17:30   ` Trond Myklebust
  0 siblings, 0 replies; 3+ messages in thread
From: Trond Myklebust @ 2006-03-21 17:30 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: linux-kernel, Linux Filesystem Development, Neil Brown

From: Andy Adamson <andros@citi.umich.edu>

Lockd and the NFSv4 server both exercise a race condition where
posix_test_lock() is called either before or after posix_lock_file()
to deal with a denied lock request due to a conflicting lock. 

Remove the race condition for the NFSv4 server by adding a new conflicting lock 
parameter to __posix_lock_file() , changing the name to 
__posix_lock_file_conf().

Keep posix_lock_file() interface, add posix_lock_conf() interface, both 
call __posix_lock_file_conf().

Signed-off-by: Andy Adamson <andros@citi.umich.edu>
Signed-off-by: J. Bruce Fields <bfields@citi.umich.edu>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
---

 fs/locks.c         |   28 ++++++++++++++++++++++------
 include/linux/fs.h |    1 +
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/fs/locks.c b/fs/locks.c
index 56f996e..1b4b899 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -798,8 +798,9 @@ out:
 }
 
 EXPORT_SYMBOL(posix_lock_file);
+EXPORT_SYMBOL(posix_lock_file_conf);
 
-static int __posix_lock_file(struct inode *inode, struct file_lock *request)
+static int __posix_lock_file_conf(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
 {
 	struct file_lock *fl;
 	struct file_lock *new_fl, *new_fl2;
@@ -823,6 +824,8 @@ static int __posix_lock_file(struct inod
 				continue;
 			if (!posix_locks_conflict(request, fl))
 				continue;
+			if (conflock)
+				locks_copy_lock(conflock, fl);
 			error = -EAGAIN;
 			if (!(request->fl_flags & FL_SLEEP))
 				goto out;
@@ -992,7 +995,20 @@ static int __posix_lock_file(struct inod
  */
 int posix_lock_file(struct file *filp, struct file_lock *fl)
 {
-	return __posix_lock_file(filp->f_dentry->d_inode, fl);
+	return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, NULL);
+}
+
+/**
+ * posix_lock_file_conf - Apply a POSIX-style lock to a file
+ * @filp: The file to apply the lock to
+ * @fl: The lock to be applied
+ * @conflock: Place to return a copy of the conflicting lock, if found.
+ *
+ * Except for the conflock parameter, acts just like posix_lock_file.
+ */
+int posix_lock_file_conf(struct file *filp, struct file_lock *fl, struct file_lock *conflock)
+{
+	return __posix_lock_file_conf(filp->f_dentry->d_inode, fl, conflock);
 }
 
 /**
@@ -1009,7 +1025,7 @@ int posix_lock_file_wait(struct file *fi
 	int error;
 	might_sleep ();
 	for (;;) {
-		error = __posix_lock_file(filp->f_dentry->d_inode, fl);
+		error = posix_lock_file(filp, fl);
 		if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
 			break;
 		error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
@@ -1081,7 +1097,7 @@ int locks_mandatory_area(int read_write,
 	fl.fl_end = offset + count - 1;
 
 	for (;;) {
-		error = __posix_lock_file(inode, &fl);
+		error = __posix_lock_file_conf(inode, &fl, NULL);
 		if (error != -EAGAIN)
 			break;
 		if (!(fl.fl_flags & FL_SLEEP))
@@ -1694,7 +1710,7 @@ again:
 		error = filp->f_op->lock(filp, cmd, file_lock);
 	else {
 		for (;;) {
-			error = __posix_lock_file(inode, file_lock);
+			error = posix_lock_file(filp, file_lock);
 			if ((error != -EAGAIN) || (cmd == F_SETLK))
 				break;
 			error = wait_event_interruptible(file_lock->fl_wait,
@@ -1837,7 +1853,7 @@ again:
 		error = filp->f_op->lock(filp, cmd, file_lock);
 	else {
 		for (;;) {
-			error = __posix_lock_file(inode, file_lock);
+			error = posix_lock_file(filp, file_lock);
 			if ((error != -EAGAIN) || (cmd == F_SETLK64))
 				break;
 			error = wait_event_interruptible(file_lock->fl_wait,
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5dc0fa2..0824e6e 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -752,6 +752,7 @@ extern void locks_copy_lock(struct file_
 extern void locks_remove_posix(struct file *, fl_owner_t);
 extern void locks_remove_flock(struct file *);
 extern int posix_test_lock(struct file *, struct file_lock *, struct file_lock *);
+extern int posix_lock_file_conf(struct file *, struct file_lock *, struct file_lock *);
 extern int posix_lock_file(struct file *, struct file_lock *);
 extern int posix_lock_file_wait(struct file *, struct file_lock *);
 extern int posix_unblock_lock(struct file *, struct file_lock *);




^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-03-21 17:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-21 17:16 VFS,fs/locks.c,NFSD4: add race_free posix_lock_file_conf() interface Trond Myklebust
2006-03-21 17:29 ` Trond Myklebust
2006-03-21 17:30   ` Trond Myklebust

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).