locks: consolidate checks for compatible filp->f_mode values in setlk handlers
authorJeff Layton <jlayton@redhat.com>
Mon, 3 Feb 2014 17:13:08 +0000 (12:13 -0500)
committerJeff Layton <jlayton@redhat.com>
Mon, 31 Mar 2014 12:24:42 +0000 (08:24 -0400)
Move this check into flock64_to_posix_lock instead of duplicating it 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.

Acked-by: J. Bruce Fields <bfields@fieldses.org>
Signed-off-by: Jeff Layton <jlayton@redhat.com>
fs/locks.c

index b49e853a9c7b7c2d40105d83a392d114473de4e0..4cd25781b0a45d102dff5cb6393eb7aaefbed04d 100644 (file)
@@ -388,6 +388,18 @@ static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
        fl->fl_ops = NULL;
        fl->fl_lmops = NULL;
 
+       /* Ensure that fl->fl_filp has compatible f_mode */
+       switch (l->l_type) {
+       case F_RDLCK:
+               if (!(filp->f_mode & FMODE_READ))
+                       return -EBADF;
+               break;
+       case F_WRLCK:
+               if (!(filp->f_mode & FMODE_WRITE))
+                       return -EBADF;
+               break;
+       }
+
        return assign_type(fl, l->l_type);
 }
 
@@ -2025,23 +2037,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);
 
        /*
@@ -2143,23 +2138,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);
 
        /*