Move this check into the posix_lock_setup_common handler 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 <jlay...@redhat.com>
---
 fs/locks.c | 46 ++++++++++++----------------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)

diff --git a/fs/locks.c b/fs/locks.c
index a5848ed..f96c455 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -354,6 +354,18 @@ flock_to_posix_lock_common(struct file_lock *fl, struct 
file *filp, short type)
        fl->fl_ops = NULL;
        fl->fl_lmops = NULL;
 
+       /* Ensure that fl->fl_filp has compatible f_mode */
+       switch (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, type);
 }
 
@@ -2060,23 +2072,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);
 
        /*
@@ -2178,23 +2173,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.4.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to