2022-03-29 16:03:04

by ChenXiaoSong

[permalink] [raw]
Subject: Re: [PATCH -next 2/2] NFSv4: fix open failure with O_ACCMODE flag

在 2022/3/29 21:05, Trond Myklebust 写道:
> No. This will not fit the definition of open(2) in the manpage.
>
> Linux reserves the special, nonstandard access mode 3 (binary 11) in
> flags to mean: check for read and write permission on the file and re‐
> turn a file descriptor that can't be used for reading or writing. This
> nonstandard access mode is used by some Linux drivers to return a file
> descriptor that is to be used only for device-specific ioctl(2) opera‐
> tions.
> Your patch will now cause FMODE_READ and FMODE_WRITE to be set on the
> file, allowing the file descriptor to be usable for I/O.

Reproducer:
```
1. mount -t nfs -o vers=4.2 $server_ip:/ /mnt/
2. fd = open("/mnt/file", O_ACCMODE|O_DIRECT|O_CREAT) = 3
3. close(fd)
4. fd = open("/mnt/file", O_ACCMODE|O_DIRECT) = -1
```

When firstly open with O_ACCMODE|O_DIRECT flags:
```c
path_openat
open_last_lookups
lookup_open
atomic_open
nfs_atomic_open
create_nfs_open_context
f_mode = flags_to_mode
alloc_nfs_open_context(..., f_mode, ...)
ctx->mode = f_mode // FMODE_READ|FMODE_WRITE
```

When secondly open with O_ACCMODE|O_DIRECT flags:
```c
path_openat
do_open
vfs_open
do_dentry_open
nfs4_file_open
f_mode = filp->f_mode | flags_to_mode(openflags)
alloc_nfs_open_context(..., f_mode, ...)
ctx->mode = f_mode // FMODE_READ|FMODE_WRITE
```

Before merging this patch, when firstly open, we does not set FMODE_READ
and FMODE_WRITE to file mode of client, FMODE_READ and FMODE_WRITE just
be set to context mode.

After merging this patch, when secondly open, I just do the same thing,
file mode of client will not have FMODE_READ and FMODE_WRITE bits, file
descriptor can't be used for reading or writing.


2022-03-30 08:44:58

by Trond Myklebust

[permalink] [raw]
Subject: Re: [PATCH -next 2/2] NFSv4: fix open failure with O_ACCMODE flag

On Tue, 2022-03-29 at 21:44 +0800, chenxiaosong (A) wrote:
> 在 2022/3/29 21:05, Trond Myklebust 写道:
> > No. This will not fit the definition of open(2) in the manpage.
> >
> >         Linux reserves the special, nonstandard access mode 3 
> > (binary  11)  in
> >         flags  to mean: check for read and write permission on the
> > file and re‐
> >         turn a file descriptor that can't be used for reading or
> > writing.  This
> >         nonstandard  access mode is used by some Linux drivers to
> > return a file
> >         descriptor that is to be used only for device-specific
> > ioctl(2)  opera‐
> >         tions.
>  > Your patch will now cause FMODE_READ and FMODE_WRITE to be set on
> the
>  > file, allowing the file descriptor to be usable for I/O.
>
> Reproducer:
> ```
>    1. mount -t nfs -o vers=4.2 $server_ip:/ /mnt/
>    2. fd = open("/mnt/file", O_ACCMODE|O_DIRECT|O_CREAT) = 3
>    3. close(fd)
>    4. fd = open("/mnt/file", O_ACCMODE|O_DIRECT) = -1
> ```
>
> When firstly open with O_ACCMODE|O_DIRECT flags:
> ```c
>    path_openat
>      open_last_lookups
>        lookup_open
>          atomic_open
>            nfs_atomic_open
>              create_nfs_open_context
>                f_mode = flags_to_mode
>                alloc_nfs_open_context(..., f_mode, ...)
>                  ctx->mode = f_mode // FMODE_READ|FMODE_WRITE
> ```
>
> When secondly open with O_ACCMODE|O_DIRECT flags:
> ```c
>    path_openat
>      do_open
>        vfs_open
>          do_dentry_open
>            nfs4_file_open
>              f_mode = filp->f_mode | flags_to_mode(openflags)
>              alloc_nfs_open_context(..., f_mode, ...)
>                ctx->mode = f_mode // FMODE_READ|FMODE_WRITE
> ```
>
> Before merging this patch, when firstly open, we does not set
> FMODE_READ
> and FMODE_WRITE to file mode of client, FMODE_READ and FMODE_WRITE
> just
> be set to context mode.
>
> After merging this patch, when secondly open, I just do the same
> thing,
> file mode of client will not have FMODE_READ and FMODE_WRITE bits,
> file
> descriptor can't be used for reading or writing.

I see. OK, I'll probably not apply this for the merge window (since I'm
pretty much queued up to send the pull request at this point), but it
might go in as a bug fix in rc1.

--
Trond Myklebust
Linux NFS client maintainer, Hammerspace
[email protected]