fdescfs: add an option to return underlying file vnode on lookup

The 'nodup' option forces fdescfs to return real vnode behind file
descriptor instead of the fdescfs fd vnode, on lookup. The end result
is that e.g. stat("/dev/fd/3") returns the stat data for the underlying
vnode, if any.  Similarly, fchdir(2) works in the expected way.

For open(2), if applied over file descriptor opened with O_PATH, it
effectively re-open that vnode into normal file descriptor which has the
specified access mode, assuming the current vnode permissions allow it.

If the file descriptor does not reference vnode, the behavior is unchanged.

This is done by a mount option, because permission check on open(2) breaks
established fdescfs open semantic of dup(2)-ing the descriptor.  So it
is not suitable for /dev/fd mount.

Tested by:	Andrew Walker <awalker@ixsystems.com>
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D30140
This commit is contained in:
Konstantin Belousov 2021-05-06 01:53:20 +03:00
parent eae2ef5a01
commit f9b1e711f0
4 changed files with 95 additions and 14 deletions

View File

@ -1,3 +1,5 @@
.\" Copyright (c) 2021 The FreeBSD Foundation, Inc.
.\"
.\" Copyright (c) 1996
.\" Mike Pritchard <mpp@FreeBSD.org>. All rights reserved.
.\"
@ -8,6 +10,10 @@
.\" This code is derived from software donated to Berkeley by
.\" Jan-Simon Pendry.
.\"
.\" Parts of this documentation was written by
.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship
.\" from the FreeBSD Foundation.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
@ -34,7 +40,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd August 1, 2017
.Dd May 17, 2021
.Dt FDESCFS 5
.Os
.Sh NAME
@ -62,7 +68,40 @@ through
.Pa /dev/fd/#
refer to file descriptors which can be accessed through the file
system.
If the file descriptor is open and the mode the file is being opened
.Pp
The following mount options can be used when mounting
.Nm
filesystem:
.Bl -tag -width linrdlnk
.It Cm nodup
For file descriptors referencing vnodes, instead of the
.Xr dup 2
semantic described above, implement re-opening of the referenced vnode.
See below for more details.
.It Cm linrdlnk
Report the type of the
.Nm
vnode as
.Dv VLNK
instead of
.Fx
traditional
.Dv VCHR .
For
.Xr linux 4
ABI compatibility mount
.Nm
volume with the
.Cm linrdlnk
option.
.El
.Pp
For
.Nm
mounted without the
.Cm nodup
mount option,
if the file descriptor is open and the mode the file is being opened
with is a subset of the mode of the existing descriptor, the call:
.Bd -literal -offset indent
fd = open("/dev/fd/0", mode);
@ -74,7 +113,6 @@ fd = fcntl(0, F_DUPFD, 0);
.Ed
.Pp
are equivalent.
.Pp
Flags to the
.Xr open 2
call other than
@ -84,6 +122,38 @@ and
.Dv O_RDWR
are ignored.
.Pp
For
.Nm
mounted with the
.Cm nodup
option, and file descriptor referencing a vnode, the call:
.Bd -literal -offset indent
fd = open("/dev/fd/0", mode);
.Ed
.Pp
reopens the referenced vnode with the specified
.Fa mode .
In other words, the
.Fn open
call above is equivalent to
.Bd -literal -offset indent
fd = openat(0, "", O_EMPTY_PATH, mode);
.Ed
.Pp
In particular, if the file descriptor was opened with the
.Dv O_PATH
flag, then either
.Dv O_EMPTY_PATH
or
.Fn open
over
.Nm
mount with
.Cm nodup
option allows one to convert it to a regularly opened file,
assuming that the current permissions allow the requested
.Fa mode .
.Pp
.Em "Note:"
.Pa /dev/fd/0 ,
.Pa /dev/fd/1
@ -92,14 +162,6 @@ and
files are created by default when devfs alone is mounted.
.Nm
creates entries for all file descriptors opened by the process.
.Pp
For
.Xr linux 4
ABI compatibility mount
.Nm
volume with
.Cm linrdlnk
option.
.Sh FILES
.Bl -tag -width /dev/stderr -compact
.It Pa /dev/fd/#
@ -110,13 +172,19 @@ To mount a
volume located on
.Pa /dev/fd :
.Pp
.Dl "mount -t fdescfs null /dev/fd"
.Dl "mount -t fdescfs none /dev/fd"
.Pp
For
.Xr linux 4
ABI compatibility:
.Pp
.Dl "mount -t fdescfs -o linrdlnk null /compat/linux/dev/fd"
.Dl "mount -t fdescfs -o linrdlnk none /compat/linux/dev/fd"
.Pp
For substitute of
.Dv O_EMPTY_PATH
flag use:
.Pp
.Dl "mount -t fdescfs -o nodup none /dev/fdpath"
.Sh SEE ALSO
.Xr devfs 5 ,
.Xr mount 8

View File

@ -42,6 +42,7 @@
/* Private mount flags for fdescfs. */
#define FMNT_UNMOUNTF 0x01
#define FMNT_LINRDLNKF 0x02
#define FMNT_NODUP 0x04
struct fdescmount {
struct vnode *f_root; /* Root node */

View File

@ -101,6 +101,8 @@ fdesc_mount(struct mount *mp)
fmp->flags = 0;
if (vfs_getopt(mp->mnt_optnew, "linrdlnk", NULL, NULL) == 0)
fmp->flags |= FMNT_LINRDLNKF;
if (vfs_getopt(mp->mnt_optnew, "nodup", NULL, NULL) == 0)
fmp->flags |= FMNT_NODUP;
error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp);
if (error) {
free(fmp, M_FDESCMNT);

View File

@ -264,10 +264,20 @@ fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
struct vnode **rvp)
{
struct fdesc_get_ino_args *a;
struct fdescmount *fdm;
struct vnode *vp;
int error;
a = arg;
error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
fdm = VFSTOFDESC(mp);
if ((fdm->flags & FMNT_NODUP) != 0 && a->fp->f_type == DTYPE_VNODE) {
vp = a->fp->f_vnode;
vget(vp, lkflags | LK_RETRY);
*rvp = vp;
error = 0;
} else {
error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
}
fdrop(a->fp, a->td);
return (error);
}