Skip to content

Commit e129372

Browse files
bcodding-rhtrondmypd
authored andcommitted
NFS: Move the flock open mode check into nfs_flock()
We only need to check lock exclusive/shared types against open mode when flock() is used on NFS, so move it into the flock-specific path instead of checking it for all locks. Signed-off-by: Benjamin Coddington <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Trond Myklebust <[email protected]>
1 parent 12a16d1 commit e129372

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

fs/nfs/file.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,23 @@ int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
820820
if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
821821
is_local = 1;
822822

823-
/* We're simulating flock() locks using posix locks on the server */
824-
if (fl->fl_type == F_UNLCK)
823+
/*
824+
* VFS doesn't require the open mode to match a flock() lock's type.
825+
* NFS, however, may simulate flock() locking with posix locking which
826+
* requires the open mode to match the lock type.
827+
*/
828+
switch (fl->fl_type) {
829+
case F_UNLCK:
825830
return do_unlk(filp, cmd, fl, is_local);
831+
case F_RDLCK:
832+
if (!(filp->f_mode & FMODE_READ))
833+
return -EBADF;
834+
break;
835+
case F_WRLCK:
836+
if (!(filp->f_mode & FMODE_WRITE))
837+
return -EBADF;
838+
}
839+
826840
return do_setlk(filp, cmd, fl, is_local);
827841
}
828842
EXPORT_SYMBOL_GPL(nfs_flock);

fs/nfs/nfs4proc.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6469,20 +6469,6 @@ nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
64696469
!test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
64706470
return -ENOLCK;
64716471

6472-
/*
6473-
* Don't rely on the VFS having checked the file open mode,
6474-
* since it won't do this for flock() locks.
6475-
*/
6476-
switch (request->fl_type) {
6477-
case F_RDLCK:
6478-
if (!(filp->f_mode & FMODE_READ))
6479-
return -EBADF;
6480-
break;
6481-
case F_WRLCK:
6482-
if (!(filp->f_mode & FMODE_WRITE))
6483-
return -EBADF;
6484-
}
6485-
64866472
status = nfs4_set_lock_state(state, request);
64876473
if (status != 0)
64886474
return status;

0 commit comments

Comments
 (0)