Index: head/sys/compat/linux/linux_file.c
===================================================================
--- head/sys/compat/linux/linux_file.c	(revision 367516)
+++ head/sys/compat/linux/linux_file.c	(revision 367517)
@@ -1,1892 +1,1920 @@
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  *
  * Copyright (c) 1994-1995 Søren Schmidt
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
 #include "opt_compat.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/capsicum.h>
 #include <sys/conf.h>
 #include <sys/dirent.h>
 #include <sys/fcntl.h>
 #include <sys/file.h>
 #include <sys/filedesc.h>
 #include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/mman.h>
 #include <sys/mount.h>
 #include <sys/mutex.h>
 #include <sys/namei.h>
 #include <sys/proc.h>
 #include <sys/stat.h>
 #include <sys/sx.h>
 #include <sys/syscallsubr.h>
 #include <sys/sysproto.h>
 #include <sys/tty.h>
 #include <sys/unistd.h>
 #include <sys/vnode.h>
 
 #ifdef COMPAT_LINUX32
 #include <compat/freebsd32/freebsd32_misc.h>
 #include <machine/../linux32/linux.h>
 #include <machine/../linux32/linux32_proto.h>
 #else
 #include <machine/../linux/linux.h>
 #include <machine/../linux/linux_proto.h>
 #endif
 #include <compat/linux/linux_misc.h>
 #include <compat/linux/linux_util.h>
 #include <compat/linux/linux_file.h>
 
 static int	linux_common_open(struct thread *, int, const char *, int, int,
 		    enum uio_seg);
 static int	linux_getdents_error(struct thread *, int, int);
 
 static struct bsd_to_linux_bitmap seal_bitmap[] = {
 	BITMAP_1t1_LINUX(F_SEAL_SEAL),
 	BITMAP_1t1_LINUX(F_SEAL_SHRINK),
 	BITMAP_1t1_LINUX(F_SEAL_GROW),
 	BITMAP_1t1_LINUX(F_SEAL_WRITE),
 };
 
 #define	MFD_HUGETLB_ENTRY(_size)					\
 	{								\
 		.bsd_value = MFD_HUGE_##_size,				\
 		.linux_value = LINUX_HUGETLB_FLAG_ENCODE_##_size	\
 	}
 static struct bsd_to_linux_bitmap mfd_bitmap[] = {
 	BITMAP_1t1_LINUX(MFD_CLOEXEC),
 	BITMAP_1t1_LINUX(MFD_ALLOW_SEALING),
 	BITMAP_1t1_LINUX(MFD_HUGETLB),
 	MFD_HUGETLB_ENTRY(64KB),
 	MFD_HUGETLB_ENTRY(512KB),
 	MFD_HUGETLB_ENTRY(1MB),
 	MFD_HUGETLB_ENTRY(2MB),
 	MFD_HUGETLB_ENTRY(8MB),
 	MFD_HUGETLB_ENTRY(16MB),
 	MFD_HUGETLB_ENTRY(32MB),
 	MFD_HUGETLB_ENTRY(256MB),
 	MFD_HUGETLB_ENTRY(512MB),
 	MFD_HUGETLB_ENTRY(1GB),
 	MFD_HUGETLB_ENTRY(2GB),
 	MFD_HUGETLB_ENTRY(16GB),
 };
 #undef MFD_HUGETLB_ENTRY
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_creat(struct thread *td, struct linux_creat_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE,
 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
 	    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
 	LFREEPATH(path);
 	return (error);
 }
 #endif
 
 static int
 linux_common_open(struct thread *td, int dirfd, const char *path, int l_flags,
     int mode, enum uio_seg seg)
 {
 	struct proc *p = td->td_proc;
 	struct file *fp;
 	int fd;
 	int bsd_flags, error;
 
 	bsd_flags = 0;
 	switch (l_flags & LINUX_O_ACCMODE) {
 	case LINUX_O_WRONLY:
 		bsd_flags |= O_WRONLY;
 		break;
 	case LINUX_O_RDWR:
 		bsd_flags |= O_RDWR;
 		break;
 	default:
 		bsd_flags |= O_RDONLY;
 	}
 	if (l_flags & LINUX_O_NDELAY)
 		bsd_flags |= O_NONBLOCK;
 	if (l_flags & LINUX_O_APPEND)
 		bsd_flags |= O_APPEND;
 	if (l_flags & LINUX_O_SYNC)
 		bsd_flags |= O_FSYNC;
 	if (l_flags & LINUX_O_CLOEXEC)
 		bsd_flags |= O_CLOEXEC;
 	if (l_flags & LINUX_O_NONBLOCK)
 		bsd_flags |= O_NONBLOCK;
 	if (l_flags & LINUX_O_ASYNC)
 		bsd_flags |= O_ASYNC;
 	if (l_flags & LINUX_O_CREAT)
 		bsd_flags |= O_CREAT;
 	if (l_flags & LINUX_O_TRUNC)
 		bsd_flags |= O_TRUNC;
 	if (l_flags & LINUX_O_EXCL)
 		bsd_flags |= O_EXCL;
 	if (l_flags & LINUX_O_NOCTTY)
 		bsd_flags |= O_NOCTTY;
 	if (l_flags & LINUX_O_DIRECT)
 		bsd_flags |= O_DIRECT;
 	if (l_flags & LINUX_O_NOFOLLOW)
 		bsd_flags |= O_NOFOLLOW;
 	if (l_flags & LINUX_O_DIRECTORY)
 		bsd_flags |= O_DIRECTORY;
 	/* XXX LINUX_O_NOATIME: unable to be easily implemented. */
 
 	error = kern_openat(td, dirfd, path, seg, bsd_flags, mode);
 	if (error != 0) {
 		if (error == EMLINK)
 			error = ELOOP;
 		goto done;
 	}
 	if (p->p_flag & P_CONTROLT)
 		goto done;
 	if (bsd_flags & O_NOCTTY)
 		goto done;
 
 	/*
 	 * XXX In between kern_openat() and fget(), another process
 	 * having the same filedesc could use that fd without
 	 * checking below.
 	*/
 	fd = td->td_retval[0];
 	if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
 		if (fp->f_type != DTYPE_VNODE) {
 			fdrop(fp, td);
 			goto done;
 		}
 		sx_slock(&proctree_lock);
 		PROC_LOCK(p);
 		if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
 			PROC_UNLOCK(p);
 			sx_sunlock(&proctree_lock);
 			/* XXXPJD: Verify if TIOCSCTTY is allowed. */
 			(void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
 			    td->td_ucred, td);
 		} else {
 			PROC_UNLOCK(p);
 			sx_sunlock(&proctree_lock);
 		}
 		fdrop(fp, td);
 	}
 
 done:
 	return (error);
 }
 
 int
 linux_openat(struct thread *td, struct linux_openat_args *args)
 {
 	char *path;
 	int dfd, error;
 
 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
 	if (!LUSECONVPATH(td)) {
 		return (linux_common_open(td, dfd, args->filename, args->flags,
 		    args->mode, UIO_USERSPACE));
 	}
 	if (args->flags & LINUX_O_CREAT)
 		LCONVPATH_AT(td, args->filename, &path, 1, dfd);
 	else
 		LCONVPATH_AT(td, args->filename, &path, 0, dfd);
 
 	error = linux_common_open(td, dfd, path, args->flags, args->mode,
 	    UIO_SYSSPACE);
 	LFREEPATH(path);
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_open(struct thread *td, struct linux_open_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (linux_common_open(td, AT_FDCWD, args->path, args->flags,
 		    args->mode, UIO_USERSPACE));
 	}
 	if (args->flags & LINUX_O_CREAT)
 		LCONVPATHCREAT(td, args->path, &path);
 	else
 		LCONVPATHEXIST(td, args->path, &path);
 
 	error = linux_common_open(td, AT_FDCWD, path, args->flags, args->mode,
 	    UIO_SYSSPACE);
 	LFREEPATH(path);
 	return (error);
 }
 #endif
 
 int
 linux_lseek(struct thread *td, struct linux_lseek_args *args)
 {
 
 	return (kern_lseek(td, args->fdes, args->off, args->whence));
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 int
 linux_llseek(struct thread *td, struct linux_llseek_args *args)
 {
 	int error;
 	off_t off;
 
 	off = (args->olow) | (((off_t) args->ohigh) << 32);
 
 	error = kern_lseek(td, args->fd, off, args->whence);
 	if (error != 0)
 		return (error);
 
 	error = copyout(td->td_retval, args->res, sizeof(off_t));
 	if (error != 0)
 		return (error);
 
 	td->td_retval[0] = 0;
 	return (0);
 }
 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 
 /*
  * Note that linux_getdents(2) and linux_getdents64(2) have the same
  * arguments. They only differ in the definition of struct dirent they
  * operate on.
  * Note that linux_readdir(2) is a special case of linux_getdents(2)
  * where count is always equals 1, meaning that the buffer is one
  * dirent-structure in size and that the code can't handle more anyway.
  * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
  * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
  * trash user stack.
  */
 
 static int
 linux_getdents_error(struct thread *td, int fd, int err)
 {
 	struct vnode *vp;
 	struct file *fp;
 	int error;
 
 	/* Linux return ENOTDIR in case when fd is not a directory. */
 	error = getvnode(td, fd, &cap_read_rights, &fp);
 	if (error != 0)
 		return (error);
 	vp = fp->f_vnode;
 	if (vp->v_type != VDIR) {
 		fdrop(fp, td);
 		return (ENOTDIR);
 	}
 	fdrop(fp, td);
 	return (err);
 }
 
 struct l_dirent {
 	l_ulong		d_ino;
 	l_off_t		d_off;
 	l_ushort	d_reclen;
 	char		d_name[LINUX_NAME_MAX + 1];
 };
 
 struct l_dirent64 {
 	uint64_t	d_ino;
 	int64_t		d_off;
 	l_ushort	d_reclen;
 	u_char		d_type;
 	char		d_name[LINUX_NAME_MAX + 1];
 };
 
 /*
  * Linux uses the last byte in the dirent buffer to store d_type,
  * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
  */
 #define LINUX_RECLEN(namlen)						\
     roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
 
 #define LINUX_RECLEN64(namlen)						\
     roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1,		\
     sizeof(uint64_t))
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_getdents(struct thread *td, struct linux_getdents_args *args)
 {
 	struct dirent *bdp;
 	caddr_t inp, buf;		/* BSD-format */
 	int len, reclen;		/* BSD-format */
 	caddr_t outp;			/* Linux-format */
 	int resid, linuxreclen;		/* Linux-format */
 	caddr_t lbuf;			/* Linux-format */
 	off_t base;
 	struct l_dirent *linux_dirent;
 	int buflen, error;
 	size_t retval;
 
 	buflen = min(args->count, MAXBSIZE);
 	buf = malloc(buflen, M_TEMP, M_WAITOK);
 
 	error = kern_getdirentries(td, args->fd, buf, buflen,
 	    &base, NULL, UIO_SYSSPACE);
 	if (error != 0) {
 		error = linux_getdents_error(td, args->fd, error);
 		goto out1;
 	}
 
 	lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
 
 	len = td->td_retval[0];
 	inp = buf;
 	outp = (caddr_t)args->dent;
 	resid = args->count;
 	retval = 0;
 
 	while (len > 0) {
 		bdp = (struct dirent *) inp;
 		reclen = bdp->d_reclen;
 		linuxreclen = LINUX_RECLEN(bdp->d_namlen);
 		/*
 		 * No more space in the user supplied dirent buffer.
 		 * Return EINVAL.
 		 */
 		if (resid < linuxreclen) {
 			error = EINVAL;
 			goto out;
 		}
 
 		linux_dirent = (struct l_dirent*)lbuf;
 		linux_dirent->d_ino = bdp->d_fileno;
 		linux_dirent->d_off = base + reclen;
 		linux_dirent->d_reclen = linuxreclen;
 		/*
 		 * Copy d_type to last byte of l_dirent buffer
 		 */
 		lbuf[linuxreclen - 1] = bdp->d_type;
 		strlcpy(linux_dirent->d_name, bdp->d_name,
 		    linuxreclen - offsetof(struct l_dirent, d_name)-1);
 		error = copyout(linux_dirent, outp, linuxreclen);
 		if (error != 0)
 			goto out;
 
 		inp += reclen;
 		base += reclen;
 		len -= reclen;
 
 		retval += linuxreclen;
 		outp += linuxreclen;
 		resid -= linuxreclen;
 	}
 	td->td_retval[0] = retval;
 
 out:
 	free(lbuf, M_TEMP);
 out1:
 	free(buf, M_TEMP);
 	return (error);
 }
 #endif
 
 int
 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
 {
 	struct dirent *bdp;
 	caddr_t inp, buf;		/* BSD-format */
 	int len, reclen;		/* BSD-format */
 	caddr_t outp;			/* Linux-format */
 	int resid, linuxreclen;		/* Linux-format */
 	caddr_t lbuf;			/* Linux-format */
 	off_t base;
 	struct l_dirent64 *linux_dirent64;
 	int buflen, error;
 	size_t retval;
 
 	buflen = min(args->count, MAXBSIZE);
 	buf = malloc(buflen, M_TEMP, M_WAITOK);
 
 	error = kern_getdirentries(td, args->fd, buf, buflen,
 	    &base, NULL, UIO_SYSSPACE);
 	if (error != 0) {
 		error = linux_getdents_error(td, args->fd, error);
 		goto out1;
 	}
 
 	lbuf = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
 
 	len = td->td_retval[0];
 	inp = buf;
 	outp = (caddr_t)args->dirent;
 	resid = args->count;
 	retval = 0;
 
 	while (len > 0) {
 		bdp = (struct dirent *) inp;
 		reclen = bdp->d_reclen;
 		linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
 		/*
 		 * No more space in the user supplied dirent buffer.
 		 * Return EINVAL.
 		 */
 		if (resid < linuxreclen) {
 			error = EINVAL;
 			goto out;
 		}
 
 		linux_dirent64 = (struct l_dirent64*)lbuf;
 		linux_dirent64->d_ino = bdp->d_fileno;
 		linux_dirent64->d_off = base + reclen;
 		linux_dirent64->d_reclen = linuxreclen;
 		linux_dirent64->d_type = bdp->d_type;
 		strlcpy(linux_dirent64->d_name, bdp->d_name,
 		    linuxreclen - offsetof(struct l_dirent64, d_name));
 		error = copyout(linux_dirent64, outp, linuxreclen);
 		if (error != 0)
 			goto out;
 
 		inp += reclen;
 		base += reclen;
 		len -= reclen;
 
 		retval += linuxreclen;
 		outp += linuxreclen;
 		resid -= linuxreclen;
 	}
 	td->td_retval[0] = retval;
 
 out:
 	free(lbuf, M_TEMP);
 out1:
 	free(buf, M_TEMP);
 	return (error);
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 int
 linux_readdir(struct thread *td, struct linux_readdir_args *args)
 {
 	struct dirent *bdp;
 	caddr_t buf;			/* BSD-format */
 	int linuxreclen;		/* Linux-format */
 	caddr_t lbuf;			/* Linux-format */
 	off_t base;
 	struct l_dirent *linux_dirent;
 	int buflen, error;
 
 	buflen = LINUX_RECLEN(LINUX_NAME_MAX);
 	buf = malloc(buflen, M_TEMP, M_WAITOK);
 
 	error = kern_getdirentries(td, args->fd, buf, buflen,
 	    &base, NULL, UIO_SYSSPACE);
 	if (error != 0) {
 		error = linux_getdents_error(td, args->fd, error);
 		goto out;
 	}
 	if (td->td_retval[0] == 0)
 		goto out;
 
 	lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
 
 	bdp = (struct dirent *) buf;
 	linuxreclen = LINUX_RECLEN(bdp->d_namlen);
 
 	linux_dirent = (struct l_dirent*)lbuf;
 	linux_dirent->d_ino = bdp->d_fileno;
 	linux_dirent->d_off = linuxreclen;
 	linux_dirent->d_reclen = bdp->d_namlen;
 	strlcpy(linux_dirent->d_name, bdp->d_name,
 	    linuxreclen - offsetof(struct l_dirent, d_name));
 	error = copyout(linux_dirent, args->dent, linuxreclen);
 	if (error == 0)
 		td->td_retval[0] = linuxreclen;
 
 	free(lbuf, M_TEMP);
 out:
 	free(buf, M_TEMP);
 	return (error);
 }
 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 
 /*
  * These exist mainly for hooks for doing /compat/linux translation.
  */
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_access(struct thread *td, struct linux_access_args *args)
 {
 	char *path;
 	int error;
 
 	/* Linux convention. */
 	if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
 		return (EINVAL);
 
 	if (!LUSECONVPATH(td)) {
 		error = kern_accessat(td, AT_FDCWD, args->path, UIO_USERSPACE, 0,
 		    args->amode);
 	} else {
 		LCONVPATHEXIST(td, args->path, &path);
 		error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
 		    args->amode);
 		LFREEPATH(path);
 	}
 
 	return (error);
 }
 #endif
 
 int
 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
 {
 	char *path;
 	int error, dfd;
 
 	/* Linux convention. */
 	if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
 		return (EINVAL);
 
 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
 	if (!LUSECONVPATH(td)) {
 		error = kern_accessat(td, dfd, args->filename, UIO_USERSPACE, 0, args->amode);
 	} else {
 		LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
 		error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode);
 		LFREEPATH(path);
 	}
 
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_unlink(struct thread *td, struct linux_unlink_args *args)
 {
 	char *path;
 	int error;
 	struct stat st;
 
 	if (!LUSECONVPATH(td)) {
 		error = kern_funlinkat(td, AT_FDCWD, args->path, FD_NONE,
 		    UIO_USERSPACE, 0, 0);
 		if (error == EPERM) {
 			/* Introduce POSIX noncompliant behaviour of Linux */
 			if (kern_statat(td, 0, AT_FDCWD, args->path,
 			    UIO_SYSSPACE, &st, NULL) == 0) {
 				if (S_ISDIR(st.st_mode))
 					error = EISDIR;
 			}
 		}
 	} else {
 		LCONVPATHEXIST(td, args->path, &path);
 		error = kern_funlinkat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0, 0);
 		if (error == EPERM) {
 			/* Introduce POSIX noncompliant behaviour of Linux */
 			if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
 			    NULL) == 0) {
 				if (S_ISDIR(st.st_mode))
 					error = EISDIR;
 			}
 		}
 		LFREEPATH(path);
 	}
 
 	return (error);
 }
 #endif
 
 static int
 linux_unlinkat_impl(struct thread *td, enum uio_seg pathseg, const char *path,
     int dfd, struct linux_unlinkat_args *args)
 {
 	struct stat st;
 	int error;
 
 	if (args->flag & LINUX_AT_REMOVEDIR)
 		error = kern_frmdirat(td, dfd, path, FD_NONE, pathseg, 0);
 	else
 		error = kern_funlinkat(td, dfd, path, FD_NONE, pathseg, 0, 0);
 	if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
 		/* Introduce POSIX noncompliant behaviour of Linux */
 		if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
 		    UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
 			error = EISDIR;
 	}
 	return (error);
 }
 
 int
 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
 {
 	char *path;
 	int error, dfd;
 
 	if (args->flag & ~LINUX_AT_REMOVEDIR)
 		return (EINVAL);
 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
 	if (!LUSECONVPATH(td)) {
 		return (linux_unlinkat_impl(td, UIO_USERSPACE, args->pathname,
 		    dfd, args));
 	}
 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
 	error = linux_unlinkat_impl(td, UIO_SYSSPACE, path, dfd, args);
 	LFREEPATH(path);
 	return (error);
 }
 int
 linux_chdir(struct thread *td, struct linux_chdir_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_chdir(td, args->path, UIO_USERSPACE));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_chdir(td, path, UIO_SYSSPACE);
 	LFREEPATH(path);
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_chmod(struct thread *td, struct linux_chmod_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_fchmodat(td, AT_FDCWD, args->path, UIO_USERSPACE,
 		    args->mode, 0));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode, 0);
 	LFREEPATH(path);
 	return (error);
 }
 #endif
 
 int
 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
 {
 	char *path;
 	int error, dfd;
 
 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
 	if (!LUSECONVPATH(td)) {
 		return (kern_fchmodat(td, dfd, args->filename, UIO_USERSPACE,
 		    args->mode, 0));
 	}
 	LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
 	error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
 	LFREEPATH(path);
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_mkdirat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->mode));
 	}
 	LCONVPATHCREAT(td, args->path, &path);
 	error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
 	LFREEPATH(path);
 	return (error);
 }
 #endif
 
 int
 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
 {
 	char *path;
 	int error, dfd;
 
 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
 	if (!LUSECONVPATH(td)) {
 		return (kern_mkdirat(td, dfd, args->pathname, UIO_USERSPACE, args->mode));
 	}
 	LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
 	error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
 	LFREEPATH(path);
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_frmdirat(td, AT_FDCWD, args->path, FD_NONE,
 		    UIO_USERSPACE, 0));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_frmdirat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0);
 	LFREEPATH(path);
 	return (error);
 }
 
 int
 linux_rename(struct thread *td, struct linux_rename_args *args)
 {
 	char *from, *to;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_renameat(td, AT_FDCWD, args->from, AT_FDCWD,
 		    args->to, UIO_USERSPACE));
 	}
 	LCONVPATHEXIST(td, args->from, &from);
 	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
 	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
 	if (to == NULL) {
 		LFREEPATH(from);
 		return (error);
 	}
 	error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
 	LFREEPATH(from);
 	LFREEPATH(to);
 	return (error);
 }
 #endif
 
 int
 linux_renameat(struct thread *td, struct linux_renameat_args *args)
 {
 	struct linux_renameat2_args renameat2_args = {
 	    .olddfd = args->olddfd,
 	    .oldname = args->oldname,
 	    .newdfd = args->newdfd,
 	    .newname = args->newname,
 	    .flags = 0
 	};
 
 	return (linux_renameat2(td, &renameat2_args));
 }
 
 int
 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
 {
 	char *from, *to;
 	int error, olddfd, newdfd;
 
 	if (args->flags != 0) {
 		if (args->flags & ~(LINUX_RENAME_EXCHANGE |
 		    LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
 			return (EINVAL);
 		if (args->flags & LINUX_RENAME_EXCHANGE &&
 		    args->flags & (LINUX_RENAME_NOREPLACE |
 		    LINUX_RENAME_WHITEOUT))
 			return (EINVAL);
 #if 0
 		/*
 		 * This spams the console on Ubuntu Focal.
 		 *
 		 * What's needed here is a general mechanism to let users know
 		 * about missing features without hogging the system.
 		 */
 		linux_msg(td, "renameat2 unsupported flags 0x%x",
 		    args->flags);
 #endif
 		return (EINVAL);
 	}
 
 	olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
 	newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
 	if (!LUSECONVPATH(td)) {
 		return (kern_renameat(td, olddfd, args->oldname, newdfd,
 		    args->newname, UIO_USERSPACE));
 	}
 	LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
 	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
 	error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
 	if (to == NULL) {
 		LFREEPATH(from);
 		return (error);
 	}
 	error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
 	LFREEPATH(from);
 	LFREEPATH(to);
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_symlink(struct thread *td, struct linux_symlink_args *args)
 {
 	char *path, *to;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_symlinkat(td, args->path, AT_FDCWD, args->to,
 		    UIO_USERSPACE));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
 	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
 	if (to == NULL) {
 		LFREEPATH(path);
 		return (error);
 	}
 	error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
 	LFREEPATH(path);
 	LFREEPATH(to);
 	return (error);
 }
 #endif
 
 int
 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
 {
 	char *path, *to;
 	int error, dfd;
 
 	dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
 	if (!LUSECONVPATH(td)) {
 		return (kern_symlinkat(td, args->oldname, dfd, args->newname,
 		    UIO_USERSPACE));
 	}
 	LCONVPATHEXIST(td, args->oldname, &path);
 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
 	error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
 	if (to == NULL) {
 		LFREEPATH(path);
 		return (error);
 	}
 	error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
 	LFREEPATH(path);
 	LFREEPATH(to);
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_readlink(struct thread *td, struct linux_readlink_args *args)
 {
 	char *name;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE,
 		    args->buf, UIO_USERSPACE, args->count));
 	}
 	LCONVPATHEXIST(td, args->name, &name);
 	error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
 	    args->buf, UIO_USERSPACE, args->count);
 	LFREEPATH(name);
 	return (error);
 }
 #endif
 
 int
 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
 {
 	char *name;
 	int error, dfd;
 
 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
 	if (!LUSECONVPATH(td)) {
 		return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE,
 		    args->buf, UIO_USERSPACE, args->bufsiz));
 	}
 	LCONVPATHEXIST_AT(td, args->path, &name, dfd);
 	error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
 	    UIO_USERSPACE, args->bufsiz);
 	LFREEPATH(name);
 	return (error);
 }
 
 int
 linux_truncate(struct thread *td, struct linux_truncate_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_truncate(td, args->path, UIO_USERSPACE, args->length));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
 	LFREEPATH(path);
 	return (error);
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 int
 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
 {
 	char *path;
 	off_t length;
 	int error;
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	length = PAIR32TO64(off_t, args->length);
 #else
 	length = args->length;
 #endif
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_truncate(td, args->path, UIO_USERSPACE, length));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_truncate(td, path, UIO_SYSSPACE, length);
 	LFREEPATH(path);
 	return (error);
 }
 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 
 int
 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
 {
 
 	return (kern_ftruncate(td, args->fd, args->length));
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 int
 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
 {
 	off_t length;
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	length = PAIR32TO64(off_t, args->length);
 #else
 	length = args->length;
 #endif
 
 	return (kern_ftruncate(td, args->fd, length));
 }
 #endif
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_link(struct thread *td, struct linux_link_args *args)
 {
 	char *path, *to;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to,
 		    UIO_USERSPACE, FOLLOW));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
 	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
 	if (to == NULL) {
 		LFREEPATH(path);
 		return (error);
 	}
 	error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
 	    FOLLOW);
 	LFREEPATH(path);
 	LFREEPATH(to);
 	return (error);
 }
 #endif
 
 int
 linux_linkat(struct thread *td, struct linux_linkat_args *args)
 {
 	char *path, *to;
 	int error, olddfd, newdfd, follow;
 
 	if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
 		return (EINVAL);
 
 	olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
 	newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
 	follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
 	    FOLLOW;
 	if (!LUSECONVPATH(td)) {
 		return (kern_linkat(td, olddfd, newdfd, args->oldname,
 		    args->newname, UIO_USERSPACE, follow));
 	}
 	LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
 	error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
 	if (to == NULL) {
 		LFREEPATH(path);
 		return (error);
 	}
 	error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
 	LFREEPATH(path);
 	LFREEPATH(to);
 	return (error);
 }
 
 int
 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
 {
 
 	return (kern_fsync(td, uap->fd, false));
 }
 
 int
 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
 {
 	off_t nbytes, offset;
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	nbytes = PAIR32TO64(off_t, uap->nbytes);
 	offset = PAIR32TO64(off_t, uap->offset);
 #else
 	nbytes = uap->nbytes;
 	offset = uap->offset;
 #endif
 
 	if (offset < 0 || nbytes < 0 ||
 	    (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
 	    LINUX_SYNC_FILE_RANGE_WRITE |
 	    LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
 		return (EINVAL);
 	}
 
 	return (kern_fsync(td, uap->fd, false));
 }
 
 int
 linux_pread(struct thread *td, struct linux_pread_args *uap)
 {
 	struct vnode *vp;
 	off_t offset;
 	int error;
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	offset = PAIR32TO64(off_t, uap->offset);
 #else
 	offset = uap->offset;
 #endif
 
 	error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
 	if (error == 0) {
 		/* This seems to violate POSIX but Linux does it. */
 		error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
 		if (error != 0)
 			return (error);
 		if (vp->v_type == VDIR)
 			error = EISDIR;
 		vrele(vp);
 	}
 	return (error);
 }
 
 int
 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
 {
 	off_t offset;
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	offset = PAIR32TO64(off_t, uap->offset);
 #else
 	offset = uap->offset;
 #endif
 
 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset));
 }
 
 int
 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
 {
 	struct uio *auio;
 	int error;
 	off_t offset;
 
 	/*
 	 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
 	 * pos_l and pos_h, respectively, contain the
 	 * low order and high order 32 bits of offset.
 	 */
 	offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
 	    (sizeof(offset) * 4)) | uap->pos_l;
 	if (offset < 0)
 		return (EINVAL);
 #ifdef COMPAT_LINUX32
 	error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
 #else
 	error = copyinuio(uap->vec, uap->vlen, &auio);
 #endif
 	if (error != 0)
 		return (error);
 	error = kern_preadv(td, uap->fd, auio, offset);
 	free(auio, M_IOV);
 	return (error);
 }
 
 int
 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
 {
 	struct uio *auio;
 	int error;
 	off_t offset;
 
 	/*
 	 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
 	 * pos_l and pos_h, respectively, contain the
 	 * low order and high order 32 bits of offset.
 	 */
 	offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
 	    (sizeof(offset) * 4)) | uap->pos_l;
 	if (offset < 0)
 		return (EINVAL);
 #ifdef COMPAT_LINUX32
 	error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
 #else
 	error = copyinuio(uap->vec, uap->vlen, &auio);
 #endif
 	if (error != 0)
 		return (error);
 	error = kern_pwritev(td, uap->fd, auio, offset);
 	free(auio, M_IOV);
 	return (error);
 }
 
 int
 linux_mount(struct thread *td, struct linux_mount_args *args)
 {
-	char fstypename[MFSNAMELEN];
-	char *mntonname, *mntfromname;
+	struct mntarg *ma = NULL;
+	char *fstypename, *mntonname, *mntfromname, *data;
 	int error, fsflags;
 
+	fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK);
 	mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
 	mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
-	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
+	data = NULL;
+	error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1,
 	    NULL);
 	if (error != 0)
 		goto out;
 	if (args->specialfile != NULL) {
 		error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
 		if (error != 0)
 			goto out;
 	} else {
 		mntfromname[0] = '\0';
 	}
 	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
 	if (error != 0)
 		goto out;
 
 	if (strcmp(fstypename, "ext2") == 0) {
 		strcpy(fstypename, "ext2fs");
 	} else if (strcmp(fstypename, "proc") == 0) {
 		strcpy(fstypename, "linprocfs");
 	} else if (strcmp(fstypename, "vfat") == 0) {
 		strcpy(fstypename, "msdosfs");
+	} else if (strcmp(fstypename, "fuse") == 0) {
+		char *fuse_options, *fuse_option, *fuse_name;
+
+		if (strcmp(mntfromname, "fuse") == 0)
+			strcpy(mntfromname, "/dev/fuse");
+
+		strcpy(fstypename, "fusefs");
+		data = malloc(MNAMELEN, M_TEMP, M_WAITOK);
+		error = copyinstr(args->data, data, MNAMELEN - 1, NULL);
+		if (error != 0)
+			goto out;
+
+		fuse_options = data;
+		while ((fuse_option = strsep(&fuse_options, ",")) != NULL) {
+			fuse_name = strsep(&fuse_option, "=");
+			if (fuse_name == NULL || fuse_option == NULL)
+				goto out;
+			ma = mount_arg(ma, fuse_name, fuse_option, -1);
+		}
+
+		/*
+		 * The FUSE server uses Linux errno values instead of FreeBSD
+		 * ones; add a flag to tell fuse(4) to do errno translation.
+		 */
+		ma = mount_arg(ma, "linux_errnos", "1", -1);
 	}
 
 	fsflags = 0;
 
 	/*
 	 * Linux SYNC flag is not included; the closest equivalent
 	 * FreeBSD has is !ASYNC, which is our default.
 	 */
 	if (args->rwflag & LINUX_MS_RDONLY)
 		fsflags |= MNT_RDONLY;
 	if (args->rwflag & LINUX_MS_NOSUID)
 		fsflags |= MNT_NOSUID;
 	if (args->rwflag & LINUX_MS_NOEXEC)
 		fsflags |= MNT_NOEXEC;
 	if (args->rwflag & LINUX_MS_REMOUNT)
 		fsflags |= MNT_UPDATE;
 
-	error = kernel_vmount(fsflags,
-	    "fstype", fstypename,
-	    "fspath", mntonname,
-	    "from", mntfromname,
-	    NULL);
+	ma = mount_arg(ma, "fstype", fstypename, -1);
+	ma = mount_arg(ma, "fspath", mntonname, -1);
+	ma = mount_arg(ma, "from", mntfromname, -1);
+	error = kernel_mount(ma, fsflags);
 out:
+	free(fstypename, M_TEMP);
 	free(mntonname, M_TEMP);
 	free(mntfromname, M_TEMP);
+	free(data, M_TEMP);
 	return (error);
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 int
 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
 {
 
 	return (kern_unmount(td, args->path, 0));
 }
 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_umount(struct thread *td, struct linux_umount_args *args)
 {
 	int flags;
 
 	flags = 0;
 	if ((args->flags & LINUX_MNT_FORCE) != 0) {
 		args->flags &= ~LINUX_MNT_FORCE;
 		flags |= MNT_FORCE;
 	}
 	if (args->flags != 0) {
 		linux_msg(td, "unsupported umount2 flags %#x", args->flags);
 		return (EINVAL);
 	}
 
 	return (kern_unmount(td, args->path, flags));
 }
 #endif
 
 /*
  * fcntl family of syscalls
  */
 
 struct l_flock {
 	l_short		l_type;
 	l_short		l_whence;
 	l_off_t		l_start;
 	l_off_t		l_len;
 	l_pid_t		l_pid;
 }
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 __packed
 #endif
 ;
 
 static void
 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
 {
 	switch (linux_flock->l_type) {
 	case LINUX_F_RDLCK:
 		bsd_flock->l_type = F_RDLCK;
 		break;
 	case LINUX_F_WRLCK:
 		bsd_flock->l_type = F_WRLCK;
 		break;
 	case LINUX_F_UNLCK:
 		bsd_flock->l_type = F_UNLCK;
 		break;
 	default:
 		bsd_flock->l_type = -1;
 		break;
 	}
 	bsd_flock->l_whence = linux_flock->l_whence;
 	bsd_flock->l_start = (off_t)linux_flock->l_start;
 	bsd_flock->l_len = (off_t)linux_flock->l_len;
 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
 	bsd_flock->l_sysid = 0;
 }
 
 static void
 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
 {
 	switch (bsd_flock->l_type) {
 	case F_RDLCK:
 		linux_flock->l_type = LINUX_F_RDLCK;
 		break;
 	case F_WRLCK:
 		linux_flock->l_type = LINUX_F_WRLCK;
 		break;
 	case F_UNLCK:
 		linux_flock->l_type = LINUX_F_UNLCK;
 		break;
 	}
 	linux_flock->l_whence = bsd_flock->l_whence;
 	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
 	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 struct l_flock64 {
 	l_short		l_type;
 	l_short		l_whence;
 	l_loff_t	l_start;
 	l_loff_t	l_len;
 	l_pid_t		l_pid;
 }
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 __packed
 #endif
 ;
 
 static void
 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
 {
 	switch (linux_flock->l_type) {
 	case LINUX_F_RDLCK:
 		bsd_flock->l_type = F_RDLCK;
 		break;
 	case LINUX_F_WRLCK:
 		bsd_flock->l_type = F_WRLCK;
 		break;
 	case LINUX_F_UNLCK:
 		bsd_flock->l_type = F_UNLCK;
 		break;
 	default:
 		bsd_flock->l_type = -1;
 		break;
 	}
 	bsd_flock->l_whence = linux_flock->l_whence;
 	bsd_flock->l_start = (off_t)linux_flock->l_start;
 	bsd_flock->l_len = (off_t)linux_flock->l_len;
 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
 	bsd_flock->l_sysid = 0;
 }
 
 static void
 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
 {
 	switch (bsd_flock->l_type) {
 	case F_RDLCK:
 		linux_flock->l_type = LINUX_F_RDLCK;
 		break;
 	case F_WRLCK:
 		linux_flock->l_type = LINUX_F_WRLCK;
 		break;
 	case F_UNLCK:
 		linux_flock->l_type = LINUX_F_UNLCK;
 		break;
 	}
 	linux_flock->l_whence = bsd_flock->l_whence;
 	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
 	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
 }
 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 
 static int
 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
 {
 	struct l_flock linux_flock;
 	struct flock bsd_flock;
 	struct file *fp;
 	long arg;
 	int error, result;
 
 	switch (args->cmd) {
 	case LINUX_F_DUPFD:
 		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
 
 	case LINUX_F_GETFD:
 		return (kern_fcntl(td, args->fd, F_GETFD, 0));
 
 	case LINUX_F_SETFD:
 		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
 
 	case LINUX_F_GETFL:
 		error = kern_fcntl(td, args->fd, F_GETFL, 0);
 		result = td->td_retval[0];
 		td->td_retval[0] = 0;
 		if (result & O_RDONLY)
 			td->td_retval[0] |= LINUX_O_RDONLY;
 		if (result & O_WRONLY)
 			td->td_retval[0] |= LINUX_O_WRONLY;
 		if (result & O_RDWR)
 			td->td_retval[0] |= LINUX_O_RDWR;
 		if (result & O_NDELAY)
 			td->td_retval[0] |= LINUX_O_NONBLOCK;
 		if (result & O_APPEND)
 			td->td_retval[0] |= LINUX_O_APPEND;
 		if (result & O_FSYNC)
 			td->td_retval[0] |= LINUX_O_SYNC;
 		if (result & O_ASYNC)
 			td->td_retval[0] |= LINUX_O_ASYNC;
 #ifdef LINUX_O_NOFOLLOW
 		if (result & O_NOFOLLOW)
 			td->td_retval[0] |= LINUX_O_NOFOLLOW;
 #endif
 #ifdef LINUX_O_DIRECT
 		if (result & O_DIRECT)
 			td->td_retval[0] |= LINUX_O_DIRECT;
 #endif
 		return (error);
 
 	case LINUX_F_SETFL:
 		arg = 0;
 		if (args->arg & LINUX_O_NDELAY)
 			arg |= O_NONBLOCK;
 		if (args->arg & LINUX_O_APPEND)
 			arg |= O_APPEND;
 		if (args->arg & LINUX_O_SYNC)
 			arg |= O_FSYNC;
 		if (args->arg & LINUX_O_ASYNC)
 			arg |= O_ASYNC;
 #ifdef LINUX_O_NOFOLLOW
 		if (args->arg & LINUX_O_NOFOLLOW)
 			arg |= O_NOFOLLOW;
 #endif
 #ifdef LINUX_O_DIRECT
 		if (args->arg & LINUX_O_DIRECT)
 			arg |= O_DIRECT;
 #endif
 		return (kern_fcntl(td, args->fd, F_SETFL, arg));
 
 	case LINUX_F_GETLK:
 		error = copyin((void *)args->arg, &linux_flock,
 		    sizeof(linux_flock));
 		if (error)
 			return (error);
 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
 		if (error)
 			return (error);
 		bsd_to_linux_flock(&bsd_flock, &linux_flock);
 		return (copyout(&linux_flock, (void *)args->arg,
 		    sizeof(linux_flock)));
 
 	case LINUX_F_SETLK:
 		error = copyin((void *)args->arg, &linux_flock,
 		    sizeof(linux_flock));
 		if (error)
 			return (error);
 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
 		return (kern_fcntl(td, args->fd, F_SETLK,
 		    (intptr_t)&bsd_flock));
 
 	case LINUX_F_SETLKW:
 		error = copyin((void *)args->arg, &linux_flock,
 		    sizeof(linux_flock));
 		if (error)
 			return (error);
 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
 		return (kern_fcntl(td, args->fd, F_SETLKW,
 		     (intptr_t)&bsd_flock));
 
 	case LINUX_F_GETOWN:
 		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
 
 	case LINUX_F_SETOWN:
 		/*
 		 * XXX some Linux applications depend on F_SETOWN having no
 		 * significant effect for pipes (SIGIO is not delivered for
 		 * pipes under Linux-2.2.35 at least).
 		 */
 		error = fget(td, args->fd,
 		    &cap_fcntl_rights, &fp);
 		if (error)
 			return (error);
 		if (fp->f_type == DTYPE_PIPE) {
 			fdrop(fp, td);
 			return (EINVAL);
 		}
 		fdrop(fp, td);
 
 		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
 
 	case LINUX_F_DUPFD_CLOEXEC:
 		return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
 	/*
 	 * Our F_SEAL_* values match Linux one for maximum compatibility.  So we
 	 * only needed to account for different values for fcntl(2) commands.
 	 */
 	case LINUX_F_GET_SEALS:
 		error = kern_fcntl(td, args->fd, F_GET_SEALS, 0);
 		if (error != 0)
 			return (error);
 		td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0],
 		    seal_bitmap, 0);
 		return (0);
 
 	case LINUX_F_ADD_SEALS:
 		return (kern_fcntl(td, args->fd, F_ADD_SEALS,
 		    linux_to_bsd_bits(args->arg, seal_bitmap, 0)));
 	default:
 		linux_msg(td, "unsupported fcntl cmd %d\n", args->cmd);
 		return (EINVAL);
 	}
 }
 
 int
 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
 {
 
 	return (fcntl_common(td, args));
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 int
 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
 {
 	struct l_flock64 linux_flock;
 	struct flock bsd_flock;
 	struct linux_fcntl_args fcntl_args;
 	int error;
 
 	switch (args->cmd) {
 	case LINUX_F_GETLK64:
 		error = copyin((void *)args->arg, &linux_flock,
 		    sizeof(linux_flock));
 		if (error)
 			return (error);
 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
 		if (error)
 			return (error);
 		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
 		return (copyout(&linux_flock, (void *)args->arg,
 			    sizeof(linux_flock)));
 
 	case LINUX_F_SETLK64:
 		error = copyin((void *)args->arg, &linux_flock,
 		    sizeof(linux_flock));
 		if (error)
 			return (error);
 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
 		return (kern_fcntl(td, args->fd, F_SETLK,
 		    (intptr_t)&bsd_flock));
 
 	case LINUX_F_SETLKW64:
 		error = copyin((void *)args->arg, &linux_flock,
 		    sizeof(linux_flock));
 		if (error)
 			return (error);
 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
 		return (kern_fcntl(td, args->fd, F_SETLKW,
 		    (intptr_t)&bsd_flock));
 	}
 
 	fcntl_args.fd = args->fd;
 	fcntl_args.cmd = args->cmd;
 	fcntl_args.arg = args->arg;
 	return (fcntl_common(td, &fcntl_args));
 }
 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_chown(struct thread *td, struct linux_chown_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
 		    args->uid, args->gid, 0));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
 	    args->gid, 0);
 	LFREEPATH(path);
 	return (error);
 }
 #endif
 
 int
 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
 {
 	char *path;
 	int error, dfd, flag;
 
 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
 		return (EINVAL);
 
 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD :  args->dfd;
 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
 	    AT_SYMLINK_NOFOLLOW;
 	if (!LUSECONVPATH(td)) {
 		return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE,
 		    args->uid, args->gid, flag));
 	}
 	LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
 	error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
 	    flag);
 	LFREEPATH(path);
 	return (error);
 }
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_lchown(struct thread *td, struct linux_lchown_args *args)
 {
 	char *path;
 	int error;
 
 	if (!LUSECONVPATH(td)) {
 		return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid,
 		    args->gid, AT_SYMLINK_NOFOLLOW));
 	}
 	LCONVPATHEXIST(td, args->path, &path);
 	error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid, args->gid,
 	    AT_SYMLINK_NOFOLLOW);
 	LFREEPATH(path);
 	return (error);
 }
 #endif
 
 static int
 convert_fadvice(int advice)
 {
 	switch (advice) {
 	case LINUX_POSIX_FADV_NORMAL:
 		return (POSIX_FADV_NORMAL);
 	case LINUX_POSIX_FADV_RANDOM:
 		return (POSIX_FADV_RANDOM);
 	case LINUX_POSIX_FADV_SEQUENTIAL:
 		return (POSIX_FADV_SEQUENTIAL);
 	case LINUX_POSIX_FADV_WILLNEED:
 		return (POSIX_FADV_WILLNEED);
 	case LINUX_POSIX_FADV_DONTNEED:
 		return (POSIX_FADV_DONTNEED);
 	case LINUX_POSIX_FADV_NOREUSE:
 		return (POSIX_FADV_NOREUSE);
 	default:
 		return (-1);
 	}
 }
 
 int
 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
 {
 	off_t offset;
 	int advice;
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	offset = PAIR32TO64(off_t, args->offset);
 #else
 	offset = args->offset;
 #endif
 
 	advice = convert_fadvice(args->advice);
 	if (advice == -1)
 		return (EINVAL);
 	return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
 }
 
 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 int
 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
 {
 	off_t len, offset;
 	int advice;
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	len = PAIR32TO64(off_t, args->len);
 	offset = PAIR32TO64(off_t, args->offset);
 #else
 	len = args->len;
 	offset = args->offset;
 #endif
 
 	advice = convert_fadvice(args->advice);
 	if (advice == -1)
 		return (EINVAL);
 	return (kern_posix_fadvise(td, args->fd, offset, len, advice));
 }
 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 
 #ifdef LINUX_LEGACY_SYSCALLS
 int
 linux_pipe(struct thread *td, struct linux_pipe_args *args)
 {
 	int fildes[2];
 	int error;
 
 	error = kern_pipe(td, fildes, 0, NULL, NULL);
 	if (error != 0)
 		return (error);
 
 	error = copyout(fildes, args->pipefds, sizeof(fildes));
 	if (error != 0) {
 		(void)kern_close(td, fildes[0]);
 		(void)kern_close(td, fildes[1]);
 	}
 
 	return (error);
 }
 #endif
 
 int
 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
 {
 	int fildes[2];
 	int error, flags;
 
 	if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
 		return (EINVAL);
 
 	flags = 0;
 	if ((args->flags & LINUX_O_NONBLOCK) != 0)
 		flags |= O_NONBLOCK;
 	if ((args->flags & LINUX_O_CLOEXEC) != 0)
 		flags |= O_CLOEXEC;
 	error = kern_pipe(td, fildes, flags, NULL, NULL);
 	if (error != 0)
 		return (error);
 
 	error = copyout(fildes, args->pipefds, sizeof(fildes));
 	if (error != 0) {
 		(void)kern_close(td, fildes[0]);
 		(void)kern_close(td, fildes[1]);
 	}
 
 	return (error);
 }
 
 int
 linux_dup3(struct thread *td, struct linux_dup3_args *args)
 {
 	int cmd;
 	intptr_t newfd;
 
 	if (args->oldfd == args->newfd)
 		return (EINVAL);
 	if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
 		return (EINVAL);
 	if (args->flags & LINUX_O_CLOEXEC)
 		cmd = F_DUP2FD_CLOEXEC;
 	else
 		cmd = F_DUP2FD;
 
 	newfd = args->newfd;
 	return (kern_fcntl(td, args->oldfd, cmd, newfd));
 }
 
 int
 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
 {
 	off_t len, offset;
 
 	/*
 	 * We emulate only posix_fallocate system call for which
 	 * mode should be 0.
 	 */
 	if (args->mode != 0)
 		return (EOPNOTSUPP);
 
 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 	len = PAIR32TO64(off_t, args->len);
 	offset = PAIR32TO64(off_t, args->offset);
 #else
 	len = args->len;
 	offset = args->offset;
 #endif
 
 	return (kern_posix_fallocate(td, args->fd, offset, len));
 }
 
 int
 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
     *args)
 {
 	l_loff_t inoff, outoff, *inoffp, *outoffp;
 	int error, flags;
 
 	/*
 	 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
 	 * the native implementation.  Enforce it.
 	 */
 	if (args->flags != 0) {
 		linux_msg(td, "copy_file_range unsupported flags 0x%x",
 		    args->flags);
 		return (EINVAL);
 	}
 	flags = 0;
 	inoffp = outoffp = NULL;
 	if (args->off_in != NULL) {
 		error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
 		if (error != 0)
 			return (error);
 		inoffp = &inoff;
 	}
 	if (args->off_out != NULL) {
 		error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
 		if (error != 0)
 			return (error);
 		outoffp = &outoff;
 	}
 
 	error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
 	    outoffp, args->len, flags);
 	if (error == 0 && args->off_in != NULL)
 		error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
 	if (error == 0 && args->off_out != NULL)
 		error = copyout(outoffp, args->off_out, sizeof(l_loff_t));
 	return (error);
 }
 
 #define	LINUX_MEMFD_PREFIX	"memfd:"
 
 int
 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
 {
 	char memfd_name[LINUX_NAME_MAX + 1];
 	int error, flags, shmflags, oflags;
 
 	/*
 	 * This is our clever trick to avoid the heap allocation to copy in the
 	 * uname.  We don't really need to go this far out of our way, but it
 	 * does keep the rest of this function fairly clean as they don't have
 	 * to worry about cleanup on the way out.
 	 */
 	error = copyinstr(args->uname_ptr,
 	    memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1,
 	    LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL);
 	if (error != 0) {
 		if (error == ENAMETOOLONG)
 			error = EINVAL;
 		return (error);
 	}
 
 	memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1);
 	flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0);
 	if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB |
 	    MFD_HUGE_MASK)) != 0)
 		return (EINVAL);
 	/* Size specified but no HUGETLB. */
 	if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0)
 		return (EINVAL);
 	/* We don't actually support HUGETLB. */
 	if ((flags & MFD_HUGETLB) != 0)
 		return (ENOSYS);
 	oflags = O_RDWR;
 	shmflags = SHM_GROW_ON_WRITE;
 	if ((flags & MFD_CLOEXEC) != 0)
 		oflags |= O_CLOEXEC;
 	if ((flags & MFD_ALLOW_SEALING) != 0)
 		shmflags |= SHM_ALLOW_SEALING;
 	return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL,
 	    memfd_name));
 }
 
 int
 linux_splice(struct thread *td, struct linux_splice_args *args)
 {
 
 	linux_msg(td, "syscall splice not really implemented");
 
 	/*
 	 * splice(2) is documented to return EINVAL in various circumstances;
 	 * returning it instead of ENOSYS should hint the caller to use fallback
 	 * instead.
 	 */
 	return (EINVAL);
 }
Index: head/sys/fs/fuse/fuse_device.c
===================================================================
--- head/sys/fs/fuse/fuse_device.c	(revision 367516)
+++ head/sys/fs/fuse/fuse_device.c	(revision 367517)
@@ -1,591 +1,603 @@
 /*-
  * SPDX-License-Identifier: BSD-3-Clause
  *
  * Copyright (c) 2007-2009 Google Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
  *
  * * Redistributions of source code must retain the above copyright
  *   notice, this list of conditions and the following disclaimer.
  * * Redistributions in binary form must reproduce the above
  *   copyright notice, this list of conditions and the following disclaimer
  *   in the documentation and/or other materials provided with the
  *   distribution.
  * * Neither the name of Google Inc. nor the names of its
  *   contributors may be used to endorse or promote products derived from
  *   this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Copyright (C) 2005 Csaba Henk.
  * All rights reserved.
  *
  * Copyright (c) 2019 The FreeBSD Foundation
  *
  * Portions of this software were developed by BFF Storage Systems, LLC 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:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
 #include <sys/types.h>
 #include <sys/module.h>
 #include <sys/systm.h>
 #include <sys/errno.h>
 #include <sys/param.h>
 #include <sys/kernel.h>
 #include <sys/conf.h>
 #include <sys/uio.h>
 #include <sys/malloc.h>
 #include <sys/queue.h>
 #include <sys/lock.h>
 #include <sys/sx.h>
 #include <sys/mutex.h>
 #include <sys/proc.h>
 #include <sys/mount.h>
 #include <sys/sdt.h>
 #include <sys/stat.h>
 #include <sys/fcntl.h>
 #include <sys/sysctl.h>
 #include <sys/poll.h>
 #include <sys/selinfo.h>
 
 #include "fuse.h"
 #include "fuse_internal.h"
 #include "fuse_ipc.h"
 
+#include <compat/linux/linux_errno.h>
+#include <compat/linux/linux_errno.inc>
+
 SDT_PROVIDER_DECLARE(fusefs);
 /* 
  * Fuse trace probe:
  * arg0: verbosity.  Higher numbers give more verbose messages
  * arg1: Textual message
  */
 SDT_PROBE_DEFINE2(fusefs, , device, trace, "int", "char*");
 
 static struct cdev *fuse_dev;
 
 static d_kqfilter_t fuse_device_filter;
 static d_open_t fuse_device_open;
 static d_poll_t fuse_device_poll;
 static d_read_t fuse_device_read;
 static d_write_t fuse_device_write;
 
 static struct cdevsw fuse_device_cdevsw = {
 	.d_kqfilter = fuse_device_filter,
 	.d_open = fuse_device_open,
 	.d_name = "fuse",
 	.d_poll = fuse_device_poll,
 	.d_read = fuse_device_read,
 	.d_write = fuse_device_write,
 	.d_version = D_VERSION,
 };
 
 static int fuse_device_filt_read(struct knote *kn, long hint);
 static void fuse_device_filt_detach(struct knote *kn);
 
 struct filterops fuse_device_rfiltops = {
 	.f_isfd = 1,
 	.f_detach = fuse_device_filt_detach,
 	.f_event = fuse_device_filt_read,
 };
 
 /****************************
  *
  * >>> Fuse device op defs
  *
  ****************************/
 
 static void
 fdata_dtor(void *arg)
 {
 	struct fuse_data *fdata;
 	struct fuse_ticket *tick;
 
 	fdata = arg;
 	if (fdata == NULL)
 		return;
 
 	fdata_set_dead(fdata);
 
 	FUSE_LOCK();
 	fuse_lck_mtx_lock(fdata->aw_mtx);
 	/* wakup poll()ers */
 	selwakeuppri(&fdata->ks_rsel, PZERO + 1);
 	/* Don't let syscall handlers wait in vain */
 	while ((tick = fuse_aw_pop(fdata))) {
 		fuse_lck_mtx_lock(tick->tk_aw_mtx);
 		fticket_set_answered(tick);
 		tick->tk_aw_errno = ENOTCONN;
 		wakeup(tick);
 		fuse_lck_mtx_unlock(tick->tk_aw_mtx);
 		FUSE_ASSERT_AW_DONE(tick);
 		fuse_ticket_drop(tick);
 	}
 	fuse_lck_mtx_unlock(fdata->aw_mtx);
 
 	/* Cleanup unsent operations */
 	fuse_lck_mtx_lock(fdata->ms_mtx);
 	while ((tick = fuse_ms_pop(fdata))) {
 		fuse_ticket_drop(tick);
 	}
 	fuse_lck_mtx_unlock(fdata->ms_mtx);
 	FUSE_UNLOCK();
 
 	fdata_trydestroy(fdata);
 }
 
 static int
 fuse_device_filter(struct cdev *dev, struct knote *kn)
 {
 	struct fuse_data *data;
 	int error;
 
 	error = devfs_get_cdevpriv((void **)&data);
 
 	/* EVFILT_WRITE is not supported; the device is always ready to write */
 	if (error == 0 && kn->kn_filter == EVFILT_READ) {
 		kn->kn_fop = &fuse_device_rfiltops;
 		kn->kn_hook = data;
 		knlist_add(&data->ks_rsel.si_note, kn, 0);
 		error = 0;
 	} else if (error == 0) {
 		error = EINVAL;
 		kn->kn_data = error;
 	}
 
 	return (error);
 }
 
 static void
 fuse_device_filt_detach(struct knote *kn)
 {
 	struct fuse_data *data;
 
 	data = (struct fuse_data*)kn->kn_hook;
 	MPASS(data != NULL);
 	knlist_remove(&data->ks_rsel.si_note, kn, 0);
 	kn->kn_hook = NULL;
 }
 
 static int
 fuse_device_filt_read(struct knote *kn, long hint)
 {
 	struct fuse_data *data;
 	int ready;
 
 	data = (struct fuse_data*)kn->kn_hook;
 	MPASS(data != NULL);
 
 	mtx_assert(&data->ms_mtx, MA_OWNED);
 	if (fdata_get_dead(data)) {
 		kn->kn_flags |= EV_EOF;
 		kn->kn_fflags = ENODEV;
 		kn->kn_data = 1;
 		ready = 1;
 	} else if (STAILQ_FIRST(&data->ms_head)) {
 		MPASS(data->ms_count >= 1);
 		kn->kn_data = data->ms_count;
 		ready = 1;
 	} else {
 		ready = 0;
 	}
 
 	return (ready);
 }
 
 /*
  * Resources are set up on a per-open basis
  */
 static int
 fuse_device_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
 {
 	struct fuse_data *fdata;
 	int error;
 
 	SDT_PROBE2(fusefs, , device, trace, 1, "device open");
 
 	fdata = fdata_alloc(dev, td->td_ucred);
 	error = devfs_set_cdevpriv(fdata, fdata_dtor);
 	if (error != 0)
 		fdata_trydestroy(fdata);
 	else
 		SDT_PROBE2(fusefs, , device, trace, 1, "device open success");
 	return (error);
 }
 
 int
 fuse_device_poll(struct cdev *dev, int events, struct thread *td)
 {
 	struct fuse_data *data;
 	int error, revents = 0;
 
 	error = devfs_get_cdevpriv((void **)&data);
 	if (error != 0)
 		return (events &
 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
 
 	if (events & (POLLIN | POLLRDNORM)) {
 		fuse_lck_mtx_lock(data->ms_mtx);
 		if (fdata_get_dead(data) || STAILQ_FIRST(&data->ms_head))
 			revents |= events & (POLLIN | POLLRDNORM);
 		else
 			selrecord(td, &data->ks_rsel);
 		fuse_lck_mtx_unlock(data->ms_mtx);
 	}
 	if (events & (POLLOUT | POLLWRNORM)) {
 		revents |= events & (POLLOUT | POLLWRNORM);
 	}
 	return (revents);
 }
 
 /*
  * fuse_device_read hangs on the queue of VFS messages.
  * When it's notified that there is a new one, it picks that and
  * passes up to the daemon
  */
 int
 fuse_device_read(struct cdev *dev, struct uio *uio, int ioflag)
 {
 	int err;
 	struct fuse_data *data;
 	struct fuse_ticket *tick;
 	void *buf[] = {NULL, NULL, NULL};
 	int buflen[3];
 	int i;
 
 	SDT_PROBE2(fusefs, , device, trace, 1, "fuse device read");
 
 	err = devfs_get_cdevpriv((void **)&data);
 	if (err != 0)
 		return (err);
 
 	fuse_lck_mtx_lock(data->ms_mtx);
 again:
 	if (fdata_get_dead(data)) {
 		SDT_PROBE2(fusefs, , device, trace, 2,
 			"we know early on that reader should be kicked so we "
 			"don't wait for news");
 		fuse_lck_mtx_unlock(data->ms_mtx);
 		return (ENODEV);
 	}
 	if (!(tick = fuse_ms_pop(data))) {
 		/* check if we may block */
 		if (ioflag & O_NONBLOCK) {
 			/* get outa here soon */
 			fuse_lck_mtx_unlock(data->ms_mtx);
 			return (EAGAIN);
 		} else {
 			err = msleep(data, &data->ms_mtx, PCATCH, "fu_msg", 0);
 			if (err != 0) {
 				fuse_lck_mtx_unlock(data->ms_mtx);
 				return (fdata_get_dead(data) ? ENODEV : err);
 			}
 			tick = fuse_ms_pop(data);
 		}
 	}
 	if (!tick) {
 		/*
 		 * We can get here if fuse daemon suddenly terminates,
 		 * eg, by being hit by a SIGKILL
 		 * -- and some other cases, too, tho not totally clear, when
 		 * (cv_signal/wakeup_one signals the whole process ?)
 		 */
 		SDT_PROBE2(fusefs, , device, trace, 1, "no message on thread");
 		goto again;
 	}
 	fuse_lck_mtx_unlock(data->ms_mtx);
 
 	if (fdata_get_dead(data)) {
 		/*
 		 * somebody somewhere -- eg., umount routine --
 		 * wants this liaison finished off
 		 */
 		SDT_PROBE2(fusefs, , device, trace, 2,
 			"reader is to be sacked");
 		if (tick) {
 			SDT_PROBE2(fusefs, , device, trace, 2, "weird -- "
 				"\"kick\" is set tho there is message");
 			FUSE_ASSERT_MS_DONE(tick);
 			fuse_ticket_drop(tick);
 		}
 		return (ENODEV);	/* This should make the daemon get off
 					 * of us */
 	}
 	SDT_PROBE2(fusefs, , device, trace, 1,
 		"fuse device read message successfully");
 
 	KASSERT(tick->tk_ms_bufdata || tick->tk_ms_bufsize == 0,
 	    ("non-null buf pointer with positive size"));
 
 	switch (tick->tk_ms_type) {
 	case FT_M_FIOV:
 		buf[0] = tick->tk_ms_fiov.base;
 		buflen[0] = tick->tk_ms_fiov.len;
 		break;
 	case FT_M_BUF:
 		buf[0] = tick->tk_ms_fiov.base;
 		buflen[0] = tick->tk_ms_fiov.len;
 		buf[1] = tick->tk_ms_bufdata;
 		buflen[1] = tick->tk_ms_bufsize;
 		break;
 	default:
 		panic("unknown message type for fuse_ticket %p", tick);
 	}
 
 	for (i = 0; buf[i]; i++) {
 		/*
 		 * Why not ban mercilessly stupid daemons who can't keep up
 		 * with us? (There is no much use of a partial read here...)
 		 */
 		/*
 		 * XXX note that in such cases Linux FUSE throws EIO at the
 		 * syscall invoker and stands back to the message queue. The
 		 * rationale should be made clear (and possibly adopt that
 		 * behaviour). Keeping the current scheme at least makes
 		 * fallacy as loud as possible...
 		 */
 		if (uio->uio_resid < buflen[i]) {
 			fdata_set_dead(data);
 			SDT_PROBE2(fusefs, , device, trace, 2,
 			    "daemon is stupid, kick it off...");
 			err = ENODEV;
 			break;
 		}
 		err = uiomove(buf[i], buflen[i], uio);
 		if (err)
 			break;
 	}
 
 	FUSE_ASSERT_MS_DONE(tick);
 	fuse_ticket_drop(tick);
 
 	return (err);
 }
 
 static inline int
 fuse_ohead_audit(struct fuse_out_header *ohead, struct uio *uio)
 {
 	if (uio->uio_resid + sizeof(struct fuse_out_header) != ohead->len) {
 		SDT_PROBE2(fusefs, , device, trace, 1,
 			"Format error: body size "
 			"differs from size claimed by header");
 		return (EINVAL);
 	}
 	if (uio->uio_resid && ohead->unique != 0 && ohead->error) {
 		SDT_PROBE2(fusefs, , device, trace, 1, 
 			"Format error: non zero error but message had a body");
 		return (EINVAL);
 	}
 
 	return (0);
 }
 
 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_notify,
 	"struct fuse_out_header*");
 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_missing_ticket,
 	"uint64_t");
 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_found,
 	"struct fuse_ticket*");
 /*
  * fuse_device_write first reads the header sent by the daemon.
  * If that's OK, looks up ticket/callback node by the unique id seen in header.
  * If the callback node contains a handler function, the uio is passed over
  * that.
  */
 static int
 fuse_device_write(struct cdev *dev, struct uio *uio, int ioflag)
 {
 	struct fuse_out_header ohead;
 	int err = 0;
 	struct fuse_data *data;
 	struct mount *mp;
 	struct fuse_ticket *tick, *itick, *x_tick;
 	int found = 0;
 
 	err = devfs_get_cdevpriv((void **)&data);
 	if (err != 0)
 		return (err);
 	mp = data->mp;
 
 	if (uio->uio_resid < sizeof(struct fuse_out_header)) {
 		SDT_PROBE2(fusefs, , device, trace, 1,
 			"fuse_device_write got less than a header!");
 		fdata_set_dead(data);
 		return (EINVAL);
 	}
 	if ((err = uiomove(&ohead, sizeof(struct fuse_out_header), uio)) != 0)
 		return (err);
+
+	if (data->linux_errnos != 0 && ohead.error != 0) {
+		err = -ohead.error;
+		if (err < 0 || err >= nitems(linux_to_bsd_errtbl))
+			return (EINVAL);
+
+		/* '-', because it will get flipped again below */
+		ohead.error = -linux_to_bsd_errtbl[err];
+	}
 
 	/*
 	 * We check header information (which is redundant) and compare it
 	 * with what we see. If we see some inconsistency we discard the
 	 * whole answer and proceed on as if it had never existed. In
 	 * particular, no pretender will be woken up, regardless the
 	 * "unique" value in the header.
 	 */
 	if ((err = fuse_ohead_audit(&ohead, uio))) {
 		fdata_set_dead(data);
 		return (err);
 	}
 	/* Pass stuff over to callback if there is one installed */
 
 	/* Looking for ticket with the unique id of header */
 	fuse_lck_mtx_lock(data->aw_mtx);
 	TAILQ_FOREACH_SAFE(tick, &data->aw_head, tk_aw_link,
 	    x_tick) {
 		if (tick->tk_unique == ohead.unique) {
 			SDT_PROBE1(fusefs, , device, fuse_device_write_found,
 				tick);
 			found = 1;
 			fuse_aw_remove(tick);
 			break;
 		}
 	}
 	if (found && tick->irq_unique > 0) {
 		/* 
 		 * Discard the FUSE_INTERRUPT ticket that tried to interrupt
 		 * this operation
 		 */
 		TAILQ_FOREACH_SAFE(itick, &data->aw_head, tk_aw_link,
 		    x_tick) {
 			if (itick->tk_unique == tick->irq_unique) {
 				fuse_aw_remove(itick);
 				fuse_ticket_drop(itick);
 				break;
 			}
 		}
 		tick->irq_unique = 0;
 	}
 	fuse_lck_mtx_unlock(data->aw_mtx);
 
 	if (found) {
 		if (tick->tk_aw_handler) {
 			/*
 			 * We found a callback with proper handler. In this
 			 * case the out header will be 0wnd by the callback,
 			 * so the fun of freeing that is left for her.
 			 * (Then, by all chance, she'll just get that's done
 			 * via ticket_drop(), so no manual mucking
 			 * around...)
 			 */
 			SDT_PROBE2(fusefs, , device, trace, 1,
 				"pass ticket to a callback");
 			/* Sanitize the linuxism of negative errnos */
 			ohead.error *= -1;
 			memcpy(&tick->tk_aw_ohead, &ohead, sizeof(ohead));
 			err = tick->tk_aw_handler(tick, uio);
 		} else {
 			/* pretender doesn't wanna do anything with answer */
 			SDT_PROBE2(fusefs, , device, trace, 1,
 				"stuff devalidated, so we drop it");
 		}
 
 		/*
 		 * As aw_mtx was not held during the callback execution the
 		 * ticket may have been inserted again.  However, this is safe
 		 * because fuse_ticket_drop() will deal with refcount anyway.
 		 */
 		fuse_ticket_drop(tick);
 	} else if (ohead.unique == 0){
 		/* unique == 0 means asynchronous notification */
 		SDT_PROBE1(fusefs, , device, fuse_device_write_notify, &ohead);
 		switch (ohead.error) {
 		case FUSE_NOTIFY_INVAL_ENTRY:
 			err = fuse_internal_invalidate_entry(mp, uio);
 			break;
 		case FUSE_NOTIFY_INVAL_INODE:
 			err = fuse_internal_invalidate_inode(mp, uio);
 			break;
 		case FUSE_NOTIFY_RETRIEVE:
 		case FUSE_NOTIFY_STORE:
 			/*
 			 * Unimplemented.  I don't know of any file systems
 			 * that use them, and the protocol isn't sound anyway,
 			 * since the notification messages don't include the
 			 * inode's generation number.  Without that, it's
 			 * possible to manipulate the cache of the wrong vnode.
 			 * Finally, it's not defined what this message should
 			 * do for a file with dirty cache.
 			 */
 		case FUSE_NOTIFY_POLL:
 			/* Unimplemented.  See comments in fuse_vnops */
 		default:
 			/* Not implemented */
 			err = ENOSYS;
 		}
 	} else {
 		/* no callback at all! */
 		SDT_PROBE1(fusefs, , device, fuse_device_write_missing_ticket, 
 			ohead.unique);
 		if (ohead.error == -EAGAIN) {
 			/* 
 			 * This was probably a response to a FUSE_INTERRUPT
 			 * operation whose original operation is already
 			 * complete.  We can't store FUSE_INTERRUPT tickets
 			 * indefinitely because their responses are optional.
 			 * So we delete them when the original operation
 			 * completes.  And sadly the fuse_header_out doesn't
 			 * identify the opcode, so we have to guess.
 			 */
 			err = 0;
 		} else {
 			err = EINVAL;
 		}
 	}
 
 	return (err);
 }
 
 int
 fuse_device_init(void)
 {
 
 	fuse_dev = make_dev(&fuse_device_cdevsw, 0, UID_ROOT, GID_OPERATOR,
 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, "fuse");
 	if (fuse_dev == NULL)
 		return (ENOMEM);
 	return (0);
 }
 
 void
 fuse_device_destroy(void)
 {
 
 	MPASS(fuse_dev != NULL);
 	destroy_dev(fuse_dev);
 }
Index: head/sys/fs/fuse/fuse_ipc.h
===================================================================
--- head/sys/fs/fuse/fuse_ipc.h	(revision 367516)
+++ head/sys/fs/fuse/fuse_ipc.h	(revision 367517)
@@ -1,428 +1,429 @@
 /*-
  * SPDX-License-Identifier: BSD-3-Clause
  *
  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
  * All rights reserved.
  * 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
  * 
  * * Redistributions of source code must retain the above copyright
  *   notice, this list of conditions and the following disclaimer.
  * * Redistributions in binary form must reproduce the above
  *   copyright notice, this list of conditions and the following disclaimer
  *   in the documentation and/or other materials provided with the
  *   distribution.
  * * Neither the name of Google Inc. nor the names of its
  *   contributors may be used to endorse or promote products derived from
  *   this software without specific prior written permission.
  * 
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  * 
  * Copyright (C) 2005 Csaba Henk.
  * All rights reserved.
  *
  * Copyright (c) 2019 The FreeBSD Foundation
  *
  * Portions of this software were developed by BFF Storage Systems, LLC 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:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 
  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * $FreeBSD$
  */
 
 #ifndef _FUSE_IPC_H_
 #define _FUSE_IPC_H_
 
 #include <sys/param.h>
 #include <sys/refcount.h>
 
 enum fuse_data_cache_mode {
 	FUSE_CACHE_UC,
 	FUSE_CACHE_WT,
 	FUSE_CACHE_WB,
 };
 
 struct fuse_iov {
 	void   *base;
 	size_t  len;
 	size_t  allocated_size;
 	int     credit;
 };
 
 void fiov_init(struct fuse_iov *fiov, size_t size);
 void fiov_teardown(struct fuse_iov *fiov);
 void fiov_refresh(struct fuse_iov *fiov);
 void fiov_adjust(struct fuse_iov *fiov, size_t size);
 
 #define FUSE_DIMALLOC(fiov, spc1, spc2, amnt) do {		\
 	fiov_adjust(fiov, (sizeof(*(spc1)) + (amnt)));		\
 	(spc1) = (fiov)->base;					\
 	(spc2) = (char *)(fiov)->base + (sizeof(*(spc1)));	\
 } while (0)
 
 #define FU_AT_LEAST(siz) max((siz), 160)
 
 #define FUSE_ASSERT_AW_DONE(ftick)					\
 	KASSERT((ftick)->tk_aw_link.tqe_next == NULL &&			\
 	    (ftick)->tk_aw_link.tqe_prev == NULL,			\
 	    ("FUSE: ticket still on answer delivery list %p", (ftick)))
 
 #define FUSE_ASSERT_MS_DONE(ftick)				\
 	KASSERT((ftick)->tk_ms_link.stqe_next == NULL,		\
 	    ("FUSE: ticket still on message list %p", (ftick)))
 
 struct fuse_ticket;
 struct fuse_data;
 
 typedef int fuse_handler_t(struct fuse_ticket *ftick, struct uio *uio);
 
 struct fuse_ticket {
 	/* fields giving the identity of the ticket */
 	uint64_t			tk_unique;
 	struct fuse_data		*tk_data;
 	int				tk_flag;
 	u_int				tk_refcount;
 	/* 
 	 * If this ticket's operation has been interrupted, this will hold the
 	 * unique value of the FUSE_INTERRUPT operation.  Otherwise, it will be
 	 * 0.
 	 */
 	uint64_t			irq_unique;
 
 	/* fields for initiating an upgoing message */
 	struct fuse_iov			tk_ms_fiov;
 	void				*tk_ms_bufdata;
 	size_t				tk_ms_bufsize;
 	enum { FT_M_FIOV, FT_M_BUF }	tk_ms_type;
 	STAILQ_ENTRY(fuse_ticket)	tk_ms_link;
 
 	/* fields for handling answers coming from userspace */
 	struct fuse_iov			tk_aw_fiov;
 	void				*tk_aw_bufdata;
 	size_t				tk_aw_bufsize;
 	enum { FT_A_FIOV, FT_A_BUF }	tk_aw_type;
 
 	struct fuse_out_header		tk_aw_ohead;
 	int				tk_aw_errno;
 	struct mtx			tk_aw_mtx;
 	fuse_handler_t			*tk_aw_handler;
 	TAILQ_ENTRY(fuse_ticket)	tk_aw_link;
 };
 
 #define FT_ANSW  0x01  /* request of ticket has already been answered */
 #define FT_DIRTY 0x04  /* ticket has been used */
 
 static inline struct fuse_iov *
 fticket_resp(struct fuse_ticket *ftick)
 {
 	return (&ftick->tk_aw_fiov);
 }
 
 static inline bool
 fticket_answered(struct fuse_ticket *ftick)
 {
 	mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
 	return (ftick->tk_flag & FT_ANSW);
 }
 
 static inline void
 fticket_set_answered(struct fuse_ticket *ftick)
 {
 	mtx_assert(&ftick->tk_aw_mtx, MA_OWNED);
 	ftick->tk_flag |= FT_ANSW;
 }
 
 static inline struct fuse_in_header*
 fticket_in_header(struct fuse_ticket *ftick)
 {
 	return (struct fuse_in_header *)(ftick->tk_ms_fiov.base);
 }
 
 static inline enum fuse_opcode
 fticket_opcode(struct fuse_ticket *ftick)
 {
 	return fticket_in_header(ftick)->opcode;
 }
 
 int fticket_pull(struct fuse_ticket *ftick, struct uio *uio);
 
 /*
  * The data representing a FUSE session.
  */
 struct fuse_data {
 	struct cdev			*fdev;
 	struct mount			*mp;
 	struct vnode			*vroot;
 	struct ucred			*daemoncred;
 	int				dataflags;
 	int				ref;
 
 	struct mtx			ms_mtx;
 	STAILQ_HEAD(, fuse_ticket)	ms_head;
 	int				ms_count;
 
 	struct mtx			aw_mtx;
 	TAILQ_HEAD(, fuse_ticket)	aw_head;
 
 	/* 
 	 * Holds the next value of the FUSE operation unique value.
 	 * Also, serves as a wakeup channel to prevent any operations from
 	 * being created before INIT completes.
 	 */
 	u_long				ticketer;
 
 	struct sx			rename_lock;
 
 	uint32_t			fuse_libabi_major;
 	uint32_t			fuse_libabi_minor;
 
 	uint32_t			max_readahead_blocks;
 	uint32_t			max_write;
 	uint32_t			max_read;
 	uint32_t			subtype;
 	char				volname[MAXPATHLEN];
 
 	struct selinfo			ks_rsel;
 
 	int				daemon_timeout;
+	int				linux_errnos;
 	unsigned			time_gran;
 	uint64_t			notimpl;
 	uint64_t			mnt_flag;
 	enum fuse_data_cache_mode	cache_mode;
 };
 
 #define FSESS_DEAD                0x0001 /* session is to be closed */
 #define FSESS_INITED              0x0004 /* session has been inited */
 #define FSESS_DAEMON_CAN_SPY      0x0010 /* let non-owners access this fs */
                                          /* (and being observed by the daemon) */
 #define FSESS_PUSH_SYMLINKS_IN    0x0020 /* prefix absolute symlinks with mp */
 #define FSESS_DEFAULT_PERMISSIONS 0x0040 /* kernel does permission checking */
 #define FSESS_ASYNC_READ          0x1000 /* allow multiple reads of some file */
 #define FSESS_POSIX_LOCKS         0x2000 /* daemon supports POSIX locks */
 #define FSESS_EXPORT_SUPPORT      0x10000 /* daemon supports NFS-style lookups */
 #define FSESS_INTR                0x20000 /* interruptible mounts */
 #define FSESS_MNTOPTS_MASK	( \
 	FSESS_DAEMON_CAN_SPY | FSESS_PUSH_SYMLINKS_IN | \
 	FSESS_DEFAULT_PERMISSIONS | FSESS_INTR)
 
 extern int fuse_data_cache_mode;
 
 static inline struct fuse_data *
 fuse_get_mpdata(struct mount *mp)
 {
 	return mp->mnt_data;
 }
 
 static inline bool
 fsess_isimpl(struct mount *mp, int opcode)
 {
 	struct fuse_data *data = fuse_get_mpdata(mp);
 
 	return ((data->notimpl & (1ULL << opcode)) == 0);
 
 }
 static inline void
 fsess_set_notimpl(struct mount *mp, int opcode)
 {
 	struct fuse_data *data = fuse_get_mpdata(mp);
 
 	data->notimpl |= (1ULL << opcode);
 }
 
 static inline bool
 fsess_opt_datacache(struct mount *mp)
 {
 	struct fuse_data *data = fuse_get_mpdata(mp);
 
 	return (data->cache_mode != FUSE_CACHE_UC);
 }
 
 static inline bool
 fsess_opt_mmap(struct mount *mp)
 {
 	return (fsess_opt_datacache(mp));
 }
 
 static inline bool
 fsess_opt_writeback(struct mount *mp)
 {
 	struct fuse_data *data = fuse_get_mpdata(mp);
 
 	return (data->cache_mode == FUSE_CACHE_WB);
 }
 
 /* Insert a new upgoing message */
 static inline void
 fuse_ms_push(struct fuse_ticket *ftick)
 {
 	mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
 	refcount_acquire(&ftick->tk_refcount);
 	STAILQ_INSERT_TAIL(&ftick->tk_data->ms_head, ftick, tk_ms_link);
 	ftick->tk_data->ms_count++;
 }
 
 /* Insert a new upgoing message to the front of the queue */
 static inline void
 fuse_ms_push_head(struct fuse_ticket *ftick)
 {
 	mtx_assert(&ftick->tk_data->ms_mtx, MA_OWNED);
 	refcount_acquire(&ftick->tk_refcount);
 	STAILQ_INSERT_HEAD(&ftick->tk_data->ms_head, ftick, tk_ms_link);
 	ftick->tk_data->ms_count++;
 }
 
 static inline struct fuse_ticket *
 fuse_ms_pop(struct fuse_data *data)
 {
 	struct fuse_ticket *ftick = NULL;
 
 	mtx_assert(&data->ms_mtx, MA_OWNED);
 
 	if ((ftick = STAILQ_FIRST(&data->ms_head))) {
 		STAILQ_REMOVE_HEAD(&data->ms_head, tk_ms_link);
 		data->ms_count--;
 #ifdef INVARIANTS
 		MPASS(data->ms_count >= 0);
 		ftick->tk_ms_link.stqe_next = NULL;
 #endif
 	}
 
 	return (ftick);
 }
 
 static inline void
 fuse_aw_push(struct fuse_ticket *ftick)
 {
 	mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
 	refcount_acquire(&ftick->tk_refcount);
 	TAILQ_INSERT_TAIL(&ftick->tk_data->aw_head, ftick, tk_aw_link);
 }
 
 static inline void
 fuse_aw_remove(struct fuse_ticket *ftick)
 {
 	mtx_assert(&ftick->tk_data->aw_mtx, MA_OWNED);
 	TAILQ_REMOVE(&ftick->tk_data->aw_head, ftick, tk_aw_link);
 #ifdef INVARIANTS
 	ftick->tk_aw_link.tqe_next = NULL;
 	ftick->tk_aw_link.tqe_prev = NULL;
 #endif
 }
 
 static inline struct fuse_ticket *
 fuse_aw_pop(struct fuse_data *data)
 {
 	struct fuse_ticket *ftick;
 
 	mtx_assert(&data->aw_mtx, MA_OWNED);
 
 	if ((ftick = TAILQ_FIRST(&data->aw_head)) != NULL)
 		fuse_aw_remove(ftick);
 
 	return (ftick);
 }
 
 struct fuse_ticket *fuse_ticket_fetch(struct fuse_data *data);
 int fuse_ticket_drop(struct fuse_ticket *ftick);
 void fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t *handler);
 void fuse_insert_message(struct fuse_ticket *ftick, bool irq);
 
 static inline bool
 fuse_libabi_geq(struct fuse_data *data, uint32_t abi_maj, uint32_t abi_min)
 {
 	return (data->fuse_libabi_major > abi_maj ||
 	    (data->fuse_libabi_major == abi_maj &&
 	     data->fuse_libabi_minor >= abi_min));
 }
 
 struct fuse_data *fdata_alloc(struct cdev *dev, struct ucred *cred);
 void fdata_trydestroy(struct fuse_data *data);
 void fdata_set_dead(struct fuse_data *data);
 
 static inline bool
 fdata_get_dead(struct fuse_data *data)
 {
 	return (data->dataflags & FSESS_DEAD);
 }
 
 struct fuse_dispatcher {
 	struct fuse_ticket    *tick;
 	struct fuse_in_header *finh;
 
 	void    *indata;
 	size_t   iosize;
 	uint64_t nodeid;
 	int      answ_stat;
 	void    *answ;
 };
 
 static inline void
 fdisp_init(struct fuse_dispatcher *fdisp, size_t iosize)
 {
 	fdisp->iosize = iosize;
 	fdisp->tick = NULL;
 }
 
 static inline void
 fdisp_destroy(struct fuse_dispatcher *fdisp)
 {
 	fuse_ticket_drop(fdisp->tick);
 #ifdef INVARIANTS
 	fdisp->tick = NULL;
 #endif
 }
 
 void fdisp_refresh(struct fuse_dispatcher *fdip);
 
 void fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op,
     struct mount *mp, uint64_t nid, struct thread *td, struct ucred *cred);
 
 void fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
     struct vnode *vp, struct thread *td, struct ucred *cred);
 
 void fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
     struct vnode *vp, struct thread *td, struct ucred *cred);
 
 int fdisp_wait_answ(struct fuse_dispatcher *fdip);
 
 static inline int
 fdisp_simple_putget_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op,
     struct vnode *vp, struct thread *td, struct ucred *cred)
 {
 	fdisp_make_vp(fdip, op, vp, td, cred);
 	return (fdisp_wait_answ(fdip));
 }
 
 #endif /* _FUSE_IPC_H_ */
Index: head/sys/fs/fuse/fuse_vfsops.c
===================================================================
--- head/sys/fs/fuse/fuse_vfsops.c	(revision 367516)
+++ head/sys/fs/fuse/fuse_vfsops.c	(revision 367517)
@@ -1,695 +1,699 @@
 /*-
  * SPDX-License-Identifier: BSD-3-Clause
  *
  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
  *
  * * Redistributions of source code must retain the above copyright
  *   notice, this list of conditions and the following disclaimer.
  * * Redistributions in binary form must reproduce the above
  *   copyright notice, this list of conditions and the following disclaimer
  *   in the documentation and/or other materials provided with the
  *   distribution.
  * * Neither the name of Google Inc. nor the names of its
  *   contributors may be used to endorse or promote products derived from
  *   this software without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Copyright (C) 2005 Csaba Henk.
  * All rights reserved.
  *
  * Copyright (c) 2019 The FreeBSD Foundation
  *
  * Portions of this software were developed by BFF Storage Systems, LLC 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:
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
 
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/buf.h>
 #include <sys/module.h>
 #include <sys/systm.h>
 #include <sys/errno.h>
 #include <sys/kernel.h>
 #include <sys/capsicum.h>
 #include <sys/conf.h>
 #include <sys/filedesc.h>
 #include <sys/uio.h>
 #include <sys/malloc.h>
 #include <sys/queue.h>
 #include <sys/lock.h>
 #include <sys/sx.h>
 #include <sys/mutex.h>
 #include <sys/proc.h>
 #include <sys/vnode.h>
 #include <sys/namei.h>
 #include <sys/mount.h>
 #include <sys/sysctl.h>
 #include <sys/fcntl.h>
 
 #include "fuse.h"
 #include "fuse_node.h"
 #include "fuse_ipc.h"
 #include "fuse_internal.h"
 
 #include <sys/priv.h>
 #include <security/mac/mac_framework.h>
 
 SDT_PROVIDER_DECLARE(fusefs);
 /* 
  * Fuse trace probe:
  * arg0: verbosity.  Higher numbers give more verbose messages
  * arg1: Textual message
  */
 SDT_PROBE_DEFINE2(fusefs, , vfsops, trace, "int", "char*");
 
 /* This will do for privilege types for now */
 #ifndef PRIV_VFS_FUSE_ALLOWOTHER
 #define PRIV_VFS_FUSE_ALLOWOTHER PRIV_VFS_MOUNT_NONUSER
 #endif
 #ifndef PRIV_VFS_FUSE_MOUNT_NONUSER
 #define PRIV_VFS_FUSE_MOUNT_NONUSER PRIV_VFS_MOUNT_NONUSER
 #endif
 #ifndef PRIV_VFS_FUSE_SYNC_UNMOUNT
 #define PRIV_VFS_FUSE_SYNC_UNMOUNT PRIV_VFS_MOUNT_NONUSER
 #endif
 
 static vfs_fhtovp_t fuse_vfsop_fhtovp;
 static vfs_mount_t fuse_vfsop_mount;
 static vfs_unmount_t fuse_vfsop_unmount;
 static vfs_root_t fuse_vfsop_root;
 static vfs_statfs_t fuse_vfsop_statfs;
 static vfs_vget_t fuse_vfsop_vget;
 
 struct vfsops fuse_vfsops = {
 	.vfs_fhtovp = fuse_vfsop_fhtovp,
 	.vfs_mount = fuse_vfsop_mount,
 	.vfs_unmount = fuse_vfsop_unmount,
 	.vfs_root = fuse_vfsop_root,
 	.vfs_statfs = fuse_vfsop_statfs,
 	.vfs_vget = fuse_vfsop_vget,
 };
 
 static int fuse_enforce_dev_perms = 0;
 
 SYSCTL_INT(_vfs_fusefs, OID_AUTO, enforce_dev_perms, CTLFLAG_RW,
     &fuse_enforce_dev_perms, 0,
     "enforce fuse device permissions for secondary mounts");
 
 MALLOC_DEFINE(M_FUSEVFS, "fuse_filesystem", "buffer for fuse vfs layer");
 
 static int
 fuse_getdevice(const char *fspec, struct thread *td, struct cdev **fdevp)
 {
 	struct nameidata nd, *ndp = &nd;
 	struct vnode *devvp;
 	struct cdev *fdev;
 	int err;
 
 	/*
 	 * Not an update, or updating the name: look up the name
 	 * and verify that it refers to a sensible disk device.
 	 */
 
 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
 	if ((err = namei(ndp)) != 0)
 		return err;
 	NDFREE(ndp, NDF_ONLY_PNBUF);
 	devvp = ndp->ni_vp;
 
 	if (devvp->v_type != VCHR) {
 		vrele(devvp);
 		return ENXIO;
 	}
 	fdev = devvp->v_rdev;
 	dev_ref(fdev);
 
 	if (fuse_enforce_dev_perms) {
 		/*
 	         * Check if mounter can open the fuse device.
 	         *
 	         * This has significance only if we are doing a secondary mount
 	         * which doesn't involve actually opening fuse devices, but we
 	         * still want to enforce the permissions of the device (in
 	         * order to keep control over the circle of fuse users).
 	         *
 	         * (In case of primary mounts, we are either the superuser so
 	         * we can do anything anyway, or we can mount only if the
 	         * device is already opened by us, ie. we are permitted to open
 	         * the device.)
 	         */
 #if 0
 #ifdef MAC
 		err = mac_check_vnode_open(td->td_ucred, devvp, VREAD | VWRITE);
 		if (!err)
 #endif
 #endif /* 0 */
 			err = VOP_ACCESS(devvp, VREAD | VWRITE, td->td_ucred, td);
 		if (err) {
 			vrele(devvp);
 			dev_rel(fdev);
 			return err;
 		}
 	}
 	/*
 	 * according to coda code, no extra lock is needed --
 	 * although in sys/vnode.h this field is marked "v"
 	 */
 	vrele(devvp);
 
 	if (!fdev->si_devsw ||
 	    strcmp("fuse", fdev->si_devsw->d_name)) {
 		dev_rel(fdev);
 		return ENXIO;
 	}
 	*fdevp = fdev;
 
 	return 0;
 }
 
 #define FUSE_FLAGOPT(fnam, fval) do {				\
 	vfs_flagopt(opts, #fnam, &mntopts, fval);		\
 	vfs_flagopt(opts, "__" #fnam, &__mntopts, fval);	\
 } while (0)
 
 SDT_PROBE_DEFINE1(fusefs, , vfsops, mntopts, "uint64_t");
 SDT_PROBE_DEFINE4(fusefs, , vfsops, mount_err, "char*", "struct fuse_data*",
 	"struct mount*", "int");
 
 static int
 fuse_vfs_remount(struct mount *mp, struct thread *td, uint64_t mntopts,
 	uint32_t max_read, int daemon_timeout)
 {
 	int err = 0;
 	struct fuse_data *data = fuse_get_mpdata(mp);
 	/* Don't allow these options to be changed */
 	const static unsigned long long cant_update_opts = 
 		MNT_USER;	/* Mount owner must be the user running the daemon */
 
 	FUSE_LOCK();
 
 	if ((mp->mnt_flag ^ data->mnt_flag) & cant_update_opts) {
 		err = EOPNOTSUPP;
 		SDT_PROBE4(fusefs, , vfsops, mount_err,
 			"Can't change these mount options during remount",
 			data, mp, err);
 		goto out;
 	}
 	if (((data->dataflags ^ mntopts) & FSESS_MNTOPTS_MASK) ||
 	     (data->max_read != max_read) ||
 	     (data->daemon_timeout != daemon_timeout)) {
 		// TODO: allow changing options where it makes sense
 		err = EOPNOTSUPP;
 		SDT_PROBE4(fusefs, , vfsops, mount_err,
 			"Can't change fuse mount options during remount",
 			data, mp, err);
 		goto out;
 	}
 
 	if (fdata_get_dead(data)) {
 		err = ENOTCONN;
 		SDT_PROBE4(fusefs, , vfsops, mount_err,
 			"device is dead during mount", data, mp, err);
 		goto out;
 	}
 
 	/* Sanity + permission checks */
 	if (!data->daemoncred)
 		panic("fuse daemon found, but identity unknown");
 	if (mntopts & FSESS_DAEMON_CAN_SPY)
 		err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
 	if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
 		/* are we allowed to do the first mount? */
 		err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
 
 out:
 	FUSE_UNLOCK();
 	return err;
 }
 
 static int
 fuse_vfsop_fhtovp(struct mount *mp, struct fid *fhp, int flags,
 	struct vnode **vpp)
 {
 	struct fuse_fid *ffhp = (struct fuse_fid *)fhp;
 	struct fuse_vnode_data *fvdat;
 	struct vnode *nvp;
 	int error;
 
 	if (!(fuse_get_mpdata(mp)->dataflags & FSESS_EXPORT_SUPPORT))
 		return EOPNOTSUPP;
 
 	error = VFS_VGET(mp, ffhp->nid, LK_EXCLUSIVE, &nvp);
 	if (error) {
 		*vpp = NULLVP;
 		return (error);
 	}
 	fvdat = VTOFUD(nvp);
 	if (fvdat->generation != ffhp->gen ) {
 		vput(nvp);
 		*vpp = NULLVP;
 		return (ESTALE);
 	}
 	*vpp = nvp;
 	vnode_create_vobject(*vpp, 0, curthread);
 	return (0);
 }
 
 static int
 fuse_vfsop_mount(struct mount *mp)
 {
 	int err;
 
 	uint64_t mntopts, __mntopts;
 	uint32_t max_read;
+	int linux_errnos;
 	int daemon_timeout;
 	int fd;
 
 	struct cdev *fdev;
 	struct fuse_data *data = NULL;
 	struct thread *td;
 	struct file *fp, *fptmp;
 	char *fspec, *subtype;
 	struct vfsoptlist *opts;
 
 	subtype = NULL;
 	max_read = ~0;
+	linux_errnos = 0;
 	err = 0;
 	mntopts = 0;
 	__mntopts = 0;
 	td = curthread;
 
 	/* Get the new options passed to mount */
 	opts = mp->mnt_optnew;
 
 	if (!opts)
 		return EINVAL;
 
 	/* `fspath' contains the mount point (eg. /mnt/fuse/sshfs); REQUIRED */
 	if (!vfs_getopts(opts, "fspath", &err))
 		return err;
 
 	/*
 	 * With the help of underscored options the mount program
 	 * can inform us from the flags it sets by default
 	 */
 	FUSE_FLAGOPT(allow_other, FSESS_DAEMON_CAN_SPY);
 	FUSE_FLAGOPT(push_symlinks_in, FSESS_PUSH_SYMLINKS_IN);
 	FUSE_FLAGOPT(default_permissions, FSESS_DEFAULT_PERMISSIONS);
 	FUSE_FLAGOPT(intr, FSESS_INTR);
 
 	(void)vfs_scanopt(opts, "max_read=", "%u", &max_read);
+	(void)vfs_scanopt(opts, "linux_errnos", "%d", &linux_errnos);
 	if (vfs_scanopt(opts, "timeout=", "%u", &daemon_timeout) == 1) {
 		if (daemon_timeout < FUSE_MIN_DAEMON_TIMEOUT)
 			daemon_timeout = FUSE_MIN_DAEMON_TIMEOUT;
 		else if (daemon_timeout > FUSE_MAX_DAEMON_TIMEOUT)
 			daemon_timeout = FUSE_MAX_DAEMON_TIMEOUT;
 	} else {
 		daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT;
 	}
 	subtype = vfs_getopts(opts, "subtype=", &err);
 
 	SDT_PROBE1(fusefs, , vfsops, mntopts, mntopts);
 
 	if (mp->mnt_flag & MNT_UPDATE) {
 		return fuse_vfs_remount(mp, td, mntopts, max_read,
 			daemon_timeout);
 	}
 
 	/* `from' contains the device name (eg. /dev/fuse0); REQUIRED */
 	fspec = vfs_getopts(opts, "from", &err);
 	if (!fspec)
 		return err;
 
 	/* `fd' contains the filedescriptor for this session; REQUIRED */
 	if (vfs_scanopt(opts, "fd", "%d", &fd) != 1)
 		return EINVAL;
 
 	err = fuse_getdevice(fspec, td, &fdev);
 	if (err != 0)
 		return err;
 
 	err = fget(td, fd, &cap_read_rights, &fp);
 	if (err != 0) {
 		SDT_PROBE2(fusefs, , vfsops, trace, 1,
 			"invalid or not opened device");
 		goto out;
 	}
 	fptmp = td->td_fpop;
 	td->td_fpop = fp;
 	err = devfs_get_cdevpriv((void **)&data);
 	td->td_fpop = fptmp;
 	fdrop(fp, td);
 	FUSE_LOCK();
 
 	if (err != 0 || data == NULL) {
 		err = ENXIO;
 		SDT_PROBE4(fusefs, , vfsops, mount_err,
 			"invalid or not opened device", data, mp, err);
 		FUSE_UNLOCK();
 		goto out;
 	}
 	if (fdata_get_dead(data)) {
 		err = ENOTCONN;
 		SDT_PROBE4(fusefs, , vfsops, mount_err,
 			"device is dead during mount", data, mp, err);
 		FUSE_UNLOCK();
 		goto out;
 	}
 	/* Sanity + permission checks */
 	if (!data->daemoncred)
 		panic("fuse daemon found, but identity unknown");
 	if (mntopts & FSESS_DAEMON_CAN_SPY)
 		err = priv_check(td, PRIV_VFS_FUSE_ALLOWOTHER);
 	if (err == 0 && td->td_ucred->cr_uid != data->daemoncred->cr_uid)
 		/* are we allowed to do the first mount? */
 		err = priv_check(td, PRIV_VFS_FUSE_MOUNT_NONUSER);
 	if (err) {
 		FUSE_UNLOCK();
 		goto out;
 	}
 	data->ref++;
 	data->mp = mp;
 	data->dataflags |= mntopts;
 	data->max_read = max_read;
 	data->daemon_timeout = daemon_timeout;
+	data->linux_errnos = linux_errnos;
 	data->mnt_flag = mp->mnt_flag & MNT_UPDATEMASK;
 	FUSE_UNLOCK();
 
 	vfs_getnewfsid(mp);
 	MNT_ILOCK(mp);
 	mp->mnt_data = data;
 	/* 
 	 * FUSE file systems can be either local or remote, but the kernel
 	 * can't tell the difference.
 	 */
 	mp->mnt_flag &= ~MNT_LOCAL;
 	mp->mnt_kern_flag |= MNTK_USES_BCACHE;
 	/* 
 	 * Disable nullfs cacheing because it can consume too many resources in
 	 * the FUSE server.
 	 */
 	mp->mnt_kern_flag |= MNTK_NULL_NOCACHE;
 	MNT_IUNLOCK(mp);
 	/* We need this here as this slot is used by getnewvnode() */
 	mp->mnt_stat.f_iosize = maxbcachebuf;
 	if (subtype) {
 		strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
 		strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
 	}
 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
 	strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN);
 	mp->mnt_iosize_max = MAXPHYS;
 
 	/* Now handshaking with daemon */
 	fuse_internal_send_init(data, td);
 
 out:
 	if (err) {
 		FUSE_LOCK();
 		if (data != NULL && data->mp == mp) {
 			/*
 			 * Destroy device only if we acquired reference to
 			 * it
 			 */
 			SDT_PROBE4(fusefs, , vfsops, mount_err,
 				"mount failed, destroy device", data, mp, err);
 			data->mp = NULL;
 			mp->mnt_data = NULL;
 			fdata_trydestroy(data);
 		}
 		FUSE_UNLOCK();
 		dev_rel(fdev);
 	}
 	return err;
 }
 
 static int
 fuse_vfsop_unmount(struct mount *mp, int mntflags)
 {
 	int err = 0;
 	int flags = 0;
 
 	struct cdev *fdev;
 	struct fuse_data *data;
 	struct fuse_dispatcher fdi;
 	struct thread *td = curthread;
 
 	if (mntflags & MNT_FORCE) {
 		flags |= FORCECLOSE;
 	}
 	data = fuse_get_mpdata(mp);
 	if (!data) {
 		panic("no private data for mount point?");
 	}
 	/* There is 1 extra root vnode reference (mp->mnt_data). */
 	FUSE_LOCK();
 	if (data->vroot != NULL) {
 		struct vnode *vroot = data->vroot;
 
 		data->vroot = NULL;
 		FUSE_UNLOCK();
 		vrele(vroot);
 	} else
 		FUSE_UNLOCK();
 	err = vflush(mp, 0, flags, td);
 	if (err) {
 		return err;
 	}
 	if (fdata_get_dead(data)) {
 		goto alreadydead;
 	}
 	if (fsess_isimpl(mp, FUSE_DESTROY)) {
 		fdisp_init(&fdi, 0);
 		fdisp_make(&fdi, FUSE_DESTROY, mp, 0, td, NULL);
 
 		(void)fdisp_wait_answ(&fdi);
 		fdisp_destroy(&fdi);
 	}
 
 	fdata_set_dead(data);
 
 alreadydead:
 	FUSE_LOCK();
 	data->mp = NULL;
 	fdev = data->fdev;
 	fdata_trydestroy(data);
 	FUSE_UNLOCK();
 
 	MNT_ILOCK(mp);
 	mp->mnt_data = NULL;
 	MNT_IUNLOCK(mp);
 
 	dev_rel(fdev);
 
 	return 0;
 }
 
 SDT_PROBE_DEFINE1(fusefs, , vfsops, invalidate_without_export,
 	"struct mount*");
 static int
 fuse_vfsop_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
 {
 	struct fuse_data *data = fuse_get_mpdata(mp);
 	uint64_t nodeid = ino;
 	struct thread *td = curthread;
 	struct fuse_dispatcher fdi;
 	struct fuse_entry_out *feo;
 	struct fuse_vnode_data *fvdat;
 	const char dot[] = ".";
 	off_t filesize;
 	enum vtype vtyp;
 	int error;
 
 	if (!(data->dataflags & FSESS_EXPORT_SUPPORT)) {
 		/*
 		 * Unreachable unless you do something stupid, like export a
 		 * nullfs mount of a fusefs file system.
 		 */
 		SDT_PROBE1(fusefs, , vfsops, invalidate_without_export, mp);
 		return (EOPNOTSUPP);
 	}
 
 	error = fuse_internal_get_cached_vnode(mp, ino, flags, vpp);
 	if (error || *vpp != NULL)
 		return error;
 
 	/* Do a LOOKUP, using nodeid as the parent and "." as filename */
 	fdisp_init(&fdi, sizeof(dot));
 	fdisp_make(&fdi, FUSE_LOOKUP, mp, nodeid, td, td->td_ucred);
 	memcpy(fdi.indata, dot, sizeof(dot));
 	error = fdisp_wait_answ(&fdi);
 
 	if (error)
 		return error;
 
 	feo = (struct fuse_entry_out *)fdi.answ;
 	if (feo->nodeid == 0) {
 		/* zero nodeid means ENOENT and cache it */
 		error = ENOENT;
 		goto out;
 	}
 
 	vtyp = IFTOVT(feo->attr.mode);
 	error = fuse_vnode_get(mp, feo, nodeid, NULL, vpp, NULL, vtyp);
 	if (error)
 		goto out;
 	filesize = feo->attr.size;
 
 	/*
 	 * In the case where we are looking up a FUSE node represented by an
 	 * existing cached vnode, and the true size reported by FUSE_LOOKUP
 	 * doesn't match the vnode's cached size, then any cached writes beyond
 	 * the file's current size are lost.
 	 *
 	 * We can get here:
 	 * * following attribute cache expiration, or
 	 * * due a bug in the daemon, or
 	 */
 	fvdat = VTOFUD(*vpp);
 	if (vnode_isreg(*vpp) &&
 	    filesize != fvdat->cached_attrs.va_size &&
 	    fvdat->flag & FN_SIZECHANGE) {
 		printf("%s: WB cache incoherent on %s!\n", __func__,
 		    vnode_mount(*vpp)->mnt_stat.f_mntonname);
 
 		fvdat->flag &= ~FN_SIZECHANGE;
 	}
 
 	fuse_internal_cache_attrs(*vpp, &feo->attr, feo->attr_valid,
 		feo->attr_valid_nsec, NULL);
 	fuse_validity_2_bintime(feo->entry_valid, feo->entry_valid_nsec,
 		&fvdat->entry_cache_timeout);
 out:
 	fdisp_destroy(&fdi);
 	return error;
 }
 
 static int
 fuse_vfsop_root(struct mount *mp, int lkflags, struct vnode **vpp)
 {
 	struct fuse_data *data = fuse_get_mpdata(mp);
 	int err = 0;
 
 	if (data->vroot != NULL) {
 		err = vget(data->vroot, lkflags);
 		if (err == 0)
 			*vpp = data->vroot;
 	} else {
 		err = fuse_vnode_get(mp, NULL, FUSE_ROOT_ID, NULL, vpp, NULL,
 		    VDIR);
 		if (err == 0) {
 			FUSE_LOCK();
 			MPASS(data->vroot == NULL || data->vroot == *vpp);
 			if (data->vroot == NULL) {
 				SDT_PROBE2(fusefs, , vfsops, trace, 1,
 					"new root vnode");
 				data->vroot = *vpp;
 				FUSE_UNLOCK();
 				vref(*vpp);
 			} else if (data->vroot != *vpp) {
 				SDT_PROBE2(fusefs, , vfsops, trace, 1,
 					"root vnode race");
 				FUSE_UNLOCK();
 				VOP_UNLOCK(*vpp);
 				vrele(*vpp);
 				vrecycle(*vpp);
 				*vpp = data->vroot;
 			} else
 				FUSE_UNLOCK();
 		}
 	}
 	return err;
 }
 
 static int
 fuse_vfsop_statfs(struct mount *mp, struct statfs *sbp)
 {
 	struct fuse_dispatcher fdi;
 	int err = 0;
 
 	struct fuse_statfs_out *fsfo;
 	struct fuse_data *data;
 
 	data = fuse_get_mpdata(mp);
 
 	if (!(data->dataflags & FSESS_INITED))
 		goto fake;
 
 	fdisp_init(&fdi, 0);
 	fdisp_make(&fdi, FUSE_STATFS, mp, FUSE_ROOT_ID, NULL, NULL);
 	err = fdisp_wait_answ(&fdi);
 	if (err) {
 		fdisp_destroy(&fdi);
 		if (err == ENOTCONN) {
 			/*
 	                 * We want to seem a legitimate fs even if the daemon
 	                 * is stiff dead... (so that, eg., we can still do path
 	                 * based unmounting after the daemon dies).
 	                 */
 			goto fake;
 		}
 		return err;
 	}
 	fsfo = fdi.answ;
 
 	sbp->f_blocks = fsfo->st.blocks;
 	sbp->f_bfree = fsfo->st.bfree;
 	sbp->f_bavail = fsfo->st.bavail;
 	sbp->f_files = fsfo->st.files;
 	sbp->f_ffree = fsfo->st.ffree;	/* cast from uint64_t to int64_t */
 	sbp->f_namemax = fsfo->st.namelen;
 	sbp->f_bsize = fsfo->st.frsize;	/* cast from uint32_t to uint64_t */
 
 	fdisp_destroy(&fdi);
 	return 0;
 
 fake:
 	sbp->f_blocks = 0;
 	sbp->f_bfree = 0;
 	sbp->f_bavail = 0;
 	sbp->f_files = 0;
 	sbp->f_ffree = 0;
 	sbp->f_namemax = 0;
 	sbp->f_bsize = S_BLKSIZE;
 
 	return 0;
 }