[RFC PATCH v2 1/5] locks: consolidate checks for compatible filp->f_mode values in setlk handlers

Jeff Layton jlayton at redhat.com
Wed Nov 20 09:45:02 MST 2013


Add a wrapper around assign_type that does this instead of duplicating
this check in two places. This also fixes a minor wart in the code where
we continue referring to the struct flock after converting it to struct
file_lock.

Signed-off-by: Jeff Layton <jlayton at redhat.com>
---
 fs/locks.c | 61 +++++++++++++++++++++++++------------------------------------
 1 file changed, 25 insertions(+), 36 deletions(-)

diff --git a/fs/locks.c b/fs/locks.c
index 92a0f0a..a387ef7 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -344,6 +344,29 @@ static int assign_type(struct file_lock *fl, long type)
 	return 0;
 }
 
+static int posix_assign_type(struct file_lock *fl, long type)
+{
+	int err;
+
+	err = assign_type(fl, type);
+	if (err)
+		return err;
+
+	/* Ensure that fl->fl_filp has compatible f_mode */
+	switch (fl->fl_type) {
+	case F_RDLCK:
+		if (!(fl->fl_file->f_mode & FMODE_READ))
+			return -EBADF;
+		break;
+	case F_WRLCK:
+		if (!(fl->fl_file->f_mode & FMODE_WRITE))
+			return -EBADF;
+		break;
+	}
+
+	return 0;
+}
+
 /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
  * style lock.
  */
@@ -393,7 +416,7 @@ static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
 	fl->fl_ops = NULL;
 	fl->fl_lmops = NULL;
 
-	return assign_type(fl, l->l_type);
+	return posix_assign_type(fl, l->l_type);
 }
 
 #if BITS_PER_LONG == 32
@@ -439,7 +462,7 @@ static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
 	fl->fl_ops = NULL;
 	fl->fl_lmops = NULL;
 
-	return assign_type(fl, l->l_type);
+	return posix_assign_type(fl, l->l_type);
 }
 #endif
 
@@ -2016,23 +2039,6 @@ again:
 		file_lock->fl_flags |= FL_SLEEP;
 	}
 	
-	error = -EBADF;
-	switch (flock.l_type) {
-	case F_RDLCK:
-		if (!(filp->f_mode & FMODE_READ))
-			goto out;
-		break;
-	case F_WRLCK:
-		if (!(filp->f_mode & FMODE_WRITE))
-			goto out;
-		break;
-	case F_UNLCK:
-		break;
-	default:
-		error = -EINVAL;
-		goto out;
-	}
-
 	error = do_lock_file_wait(filp, cmd, file_lock);
 
 	/*
@@ -2134,23 +2140,6 @@ again:
 		file_lock->fl_flags |= FL_SLEEP;
 	}
 	
-	error = -EBADF;
-	switch (flock.l_type) {
-	case F_RDLCK:
-		if (!(filp->f_mode & FMODE_READ))
-			goto out;
-		break;
-	case F_WRLCK:
-		if (!(filp->f_mode & FMODE_WRITE))
-			goto out;
-		break;
-	case F_UNLCK:
-		break;
-	default:
-		error = -EINVAL;
-		goto out;
-	}
-
 	error = do_lock_file_wait(filp, cmd, file_lock);
 
 	/*
-- 
1.8.3.1



More information about the samba-technical mailing list