Skip to content

Commit b243874

Browse files
ChenXiaoSongTrond Myklebust
authored andcommitted
NFSv4: fix open failure with O_ACCMODE flag
open() with O_ACCMODE|O_DIRECT flags secondly will fail. Reproducer: 1. mount -t nfs -o vers=4.2 $server_ip:/ /mnt/ 2. fd = open("/mnt/file", O_ACCMODE|O_DIRECT|O_CREAT) 3. close(fd) 4. fd = open("/mnt/file", O_ACCMODE|O_DIRECT) Server nfsd4_decode_share_access() will fail with error nfserr_bad_xdr when client use incorrect share access mode of 0. Fix this by using NFS4_SHARE_ACCESS_BOTH share access mode in client, just like firstly opening. Fixes: ce4ef7c ("NFS: Split out NFS v4 file operations") Signed-off-by: ChenXiaoSong <[email protected]> Signed-off-by: Trond Myklebust <[email protected]>
1 parent ab0fc21 commit b243874

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

fs/nfs/dir.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,16 +1991,6 @@ const struct dentry_operations nfs4_dentry_operations = {
19911991
};
19921992
EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
19931993

1994-
static fmode_t flags_to_mode(int flags)
1995-
{
1996-
fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
1997-
if ((flags & O_ACCMODE) != O_WRONLY)
1998-
res |= FMODE_READ;
1999-
if ((flags & O_ACCMODE) != O_RDONLY)
2000-
res |= FMODE_WRITE;
2001-
return res;
2002-
}
2003-
20041994
static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
20051995
{
20061996
return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);

fs/nfs/internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ static inline bool nfs_lookup_is_soft_revalidate(const struct dentry *dentry)
4242
return true;
4343
}
4444

45+
static inline fmode_t flags_to_mode(int flags)
46+
{
47+
fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
48+
if ((flags & O_ACCMODE) != O_WRONLY)
49+
res |= FMODE_READ;
50+
if ((flags & O_ACCMODE) != O_RDONLY)
51+
res |= FMODE_WRITE;
52+
return res;
53+
}
54+
4555
/*
4656
* Note: RFC 1813 doesn't limit the number of auth flavors that
4757
* a server can return, so make something up.

fs/nfs/nfs4file.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ nfs4_file_open(struct inode *inode, struct file *filp)
3232
struct dentry *parent = NULL;
3333
struct inode *dir;
3434
unsigned openflags = filp->f_flags;
35+
fmode_t f_mode;
3536
struct iattr attr;
3637
int err;
3738

@@ -50,16 +51,17 @@ nfs4_file_open(struct inode *inode, struct file *filp)
5051
if (err)
5152
return err;
5253

54+
f_mode = filp->f_mode;
5355
if ((openflags & O_ACCMODE) == 3)
54-
openflags--;
56+
f_mode |= flags_to_mode(openflags);
5557

5658
/* We can't create new files here */
5759
openflags &= ~(O_CREAT|O_EXCL);
5860

5961
parent = dget_parent(dentry);
6062
dir = d_inode(parent);
6163

62-
ctx = alloc_nfs_open_context(file_dentry(filp), filp->f_mode, filp);
64+
ctx = alloc_nfs_open_context(file_dentry(filp), f_mode, filp);
6365
err = PTR_ERR(ctx);
6466
if (IS_ERR(ctx))
6567
goto out;

0 commit comments

Comments
 (0)