diff --git a/share/man/man5/fdescfs.5 b/share/man/man5/fdescfs.5 --- a/share/man/man5/fdescfs.5 +++ b/share/man/man5/fdescfs.5 @@ -34,7 +34,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 1, 2017 +.Dd May 17, 2021 .Dt FDESCFS 5 .Os .Sh NAME @@ -62,7 +62,40 @@ .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 +.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 +107,6 @@ .Ed .Pp are equivalent. -.Pp Flags to the .Xr open 2 call other than @@ -84,6 +116,38 @@ .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 +.Dv O_PATH +flag, then either +.Dv O_EMPTY_PATH +or +.Fn open +over +.Nm +mount with +.Cm nodup +option allows to convert it to regular opened file, assuming the current +permissions allow the requested +.Fa mode . +.Pp .Em "Note:" .Pa /dev/fd/0 , .Pa /dev/fd/1 @@ -92,14 +156,6 @@ 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/# diff --git a/sys/fs/fdescfs/fdesc.h b/sys/fs/fdescfs/fdesc.h --- a/sys/fs/fdescfs/fdesc.h +++ b/sys/fs/fdescfs/fdesc.h @@ -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 */ diff --git a/sys/fs/fdescfs/fdesc_vfsops.c b/sys/fs/fdescfs/fdesc_vfsops.c --- a/sys/fs/fdescfs/fdesc_vfsops.c +++ b/sys/fs/fdescfs/fdesc_vfsops.c @@ -101,6 +101,8 @@ 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); diff --git a/sys/fs/fdescfs/fdesc_vnops.c b/sys/fs/fdescfs/fdesc_vnops.c --- a/sys/fs/fdescfs/fdesc_vnops.c +++ b/sys/fs/fdescfs/fdesc_vnops.c @@ -267,10 +267,20 @@ 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); }