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@xxxxxxxxxx> --- fs/locks.c | 61 +++++++++++++++++++++++++------------------------------------ 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/fs/locks.c b/fs/locks.c index bd88c04..389382c 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 @@ -2025,23 +2048,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 +2149,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 -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html