xref: /freebsd/sys/compat/linux/linux_file.c (revision 7f39937557052c004d7ebf55dd973108323756f3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1994-1995 Søren Schmidt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/capsicum.h>
37 #include <sys/conf.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mman.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/namei.h>
48 #include <sys/selinfo.h>
49 #include <sys/pipe.h>
50 #include <sys/proc.h>
51 #include <sys/stat.h>
52 #include <sys/sx.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/tty.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
58 
59 #ifdef COMPAT_LINUX32
60 #include <compat/freebsd32/freebsd32_misc.h>
61 #include <machine/../linux32/linux.h>
62 #include <machine/../linux32/linux32_proto.h>
63 #else
64 #include <machine/../linux/linux.h>
65 #include <machine/../linux/linux_proto.h>
66 #endif
67 #include <compat/linux/linux_misc.h>
68 #include <compat/linux/linux_util.h>
69 #include <compat/linux/linux_file.h>
70 
71 static int	linux_common_open(struct thread *, int, const char *, int, int,
72 		    enum uio_seg);
73 static int	linux_do_accessat(struct thread *t, int, const char *, int, int);
74 static int	linux_getdents_error(struct thread *, int, int);
75 
76 static struct bsd_to_linux_bitmap seal_bitmap[] = {
77 	BITMAP_1t1_LINUX(F_SEAL_SEAL),
78 	BITMAP_1t1_LINUX(F_SEAL_SHRINK),
79 	BITMAP_1t1_LINUX(F_SEAL_GROW),
80 	BITMAP_1t1_LINUX(F_SEAL_WRITE),
81 };
82 
83 #define	MFD_HUGETLB_ENTRY(_size)					\
84 	{								\
85 		.bsd_value = MFD_HUGE_##_size,				\
86 		.linux_value = LINUX_HUGETLB_FLAG_ENCODE_##_size	\
87 	}
88 static struct bsd_to_linux_bitmap mfd_bitmap[] = {
89 	BITMAP_1t1_LINUX(MFD_CLOEXEC),
90 	BITMAP_1t1_LINUX(MFD_ALLOW_SEALING),
91 	BITMAP_1t1_LINUX(MFD_HUGETLB),
92 	MFD_HUGETLB_ENTRY(64KB),
93 	MFD_HUGETLB_ENTRY(512KB),
94 	MFD_HUGETLB_ENTRY(1MB),
95 	MFD_HUGETLB_ENTRY(2MB),
96 	MFD_HUGETLB_ENTRY(8MB),
97 	MFD_HUGETLB_ENTRY(16MB),
98 	MFD_HUGETLB_ENTRY(32MB),
99 	MFD_HUGETLB_ENTRY(256MB),
100 	MFD_HUGETLB_ENTRY(512MB),
101 	MFD_HUGETLB_ENTRY(1GB),
102 	MFD_HUGETLB_ENTRY(2GB),
103 	MFD_HUGETLB_ENTRY(16GB),
104 };
105 #undef MFD_HUGETLB_ENTRY
106 
107 #ifdef LINUX_LEGACY_SYSCALLS
108 int
109 linux_creat(struct thread *td, struct linux_creat_args *args)
110 {
111 	char *path;
112 	int error;
113 
114 	if (!LUSECONVPATH(td)) {
115 		return (kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE,
116 		    O_WRONLY | O_CREAT | O_TRUNC, args->mode));
117 	}
118 	LCONVPATHEXIST(args->path, &path);
119 	error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
120 	    O_WRONLY | O_CREAT | O_TRUNC, args->mode);
121 	LFREEPATH(path);
122 	return (error);
123 }
124 #endif
125 
126 static int
127 linux_common_openflags(int l_flags)
128 {
129 	int bsd_flags;
130 
131 	bsd_flags = 0;
132 	switch (l_flags & LINUX_O_ACCMODE) {
133 	case LINUX_O_WRONLY:
134 		bsd_flags |= O_WRONLY;
135 		break;
136 	case LINUX_O_RDWR:
137 		bsd_flags |= O_RDWR;
138 		break;
139 	default:
140 		bsd_flags |= O_RDONLY;
141 	}
142 	if (l_flags & LINUX_O_NDELAY)
143 		bsd_flags |= O_NONBLOCK;
144 	if (l_flags & LINUX_O_APPEND)
145 		bsd_flags |= O_APPEND;
146 	if (l_flags & LINUX_O_SYNC)
147 		bsd_flags |= O_FSYNC;
148 	if (l_flags & LINUX_O_CLOEXEC)
149 		bsd_flags |= O_CLOEXEC;
150 	if (l_flags & LINUX_O_NONBLOCK)
151 		bsd_flags |= O_NONBLOCK;
152 	if (l_flags & LINUX_O_ASYNC)
153 		bsd_flags |= O_ASYNC;
154 	if (l_flags & LINUX_O_CREAT)
155 		bsd_flags |= O_CREAT;
156 	if (l_flags & LINUX_O_TRUNC)
157 		bsd_flags |= O_TRUNC;
158 	if (l_flags & LINUX_O_EXCL)
159 		bsd_flags |= O_EXCL;
160 	if (l_flags & LINUX_O_NOCTTY)
161 		bsd_flags |= O_NOCTTY;
162 	if (l_flags & LINUX_O_DIRECT)
163 		bsd_flags |= O_DIRECT;
164 	if (l_flags & LINUX_O_NOFOLLOW)
165 		bsd_flags |= O_NOFOLLOW;
166 	if (l_flags & LINUX_O_DIRECTORY)
167 		bsd_flags |= O_DIRECTORY;
168 	if (l_flags & LINUX_O_PATH)
169 		bsd_flags |= O_PATH;
170 	/* XXX LINUX_O_NOATIME: unable to be easily implemented. */
171 	return (bsd_flags);
172 }
173 
174 static int
175 linux_common_open(struct thread *td, int dirfd, const char *path, int l_flags,
176     int mode, enum uio_seg seg)
177 {
178 	struct proc *p = td->td_proc;
179 	struct file *fp;
180 	int fd;
181 	int bsd_flags, error;
182 
183 	bsd_flags = linux_common_openflags(l_flags);
184 	error = kern_openat(td, dirfd, path, seg, bsd_flags, mode);
185 	if (error != 0) {
186 		if (error == EMLINK)
187 			error = ELOOP;
188 		goto done;
189 	}
190 	if (p->p_flag & P_CONTROLT)
191 		goto done;
192 	if (bsd_flags & O_NOCTTY)
193 		goto done;
194 
195 	/*
196 	 * XXX In between kern_openat() and fget(), another process
197 	 * having the same filedesc could use that fd without
198 	 * checking below.
199 	*/
200 	fd = td->td_retval[0];
201 	if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
202 		if (fp->f_type != DTYPE_VNODE) {
203 			fdrop(fp, td);
204 			goto done;
205 		}
206 		sx_slock(&proctree_lock);
207 		PROC_LOCK(p);
208 		if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
209 			PROC_UNLOCK(p);
210 			sx_sunlock(&proctree_lock);
211 			/* XXXPJD: Verify if TIOCSCTTY is allowed. */
212 			(void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
213 			    td->td_ucred, td);
214 		} else {
215 			PROC_UNLOCK(p);
216 			sx_sunlock(&proctree_lock);
217 		}
218 		fdrop(fp, td);
219 	}
220 
221 done:
222 	return (error);
223 }
224 
225 int
226 linux_openat(struct thread *td, struct linux_openat_args *args)
227 {
228 	char *path;
229 	int dfd, error;
230 
231 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
232 	if (!LUSECONVPATH(td)) {
233 		return (linux_common_open(td, dfd, args->filename, args->flags,
234 		    args->mode, UIO_USERSPACE));
235 	}
236 	if (args->flags & LINUX_O_CREAT)
237 		LCONVPATH_AT(args->filename, &path, 1, dfd);
238 	else
239 		LCONVPATH_AT(args->filename, &path, 0, dfd);
240 
241 	error = linux_common_open(td, dfd, path, args->flags, args->mode,
242 	    UIO_SYSSPACE);
243 	LFREEPATH(path);
244 	return (error);
245 }
246 
247 #ifdef LINUX_LEGACY_SYSCALLS
248 int
249 linux_open(struct thread *td, struct linux_open_args *args)
250 {
251 	char *path;
252 	int error;
253 
254 	if (!LUSECONVPATH(td)) {
255 		return (linux_common_open(td, AT_FDCWD, args->path, args->flags,
256 		    args->mode, UIO_USERSPACE));
257 	}
258 	if (args->flags & LINUX_O_CREAT)
259 		LCONVPATHCREAT(args->path, &path);
260 	else
261 		LCONVPATHEXIST(args->path, &path);
262 
263 	error = linux_common_open(td, AT_FDCWD, path, args->flags, args->mode,
264 	    UIO_SYSSPACE);
265 	LFREEPATH(path);
266 	return (error);
267 }
268 #endif
269 
270 int
271 linux_name_to_handle_at(struct thread *td,
272     struct linux_name_to_handle_at_args *args)
273 {
274 	static const l_int valid_flags = (LINUX_AT_SYMLINK_FOLLOW |
275 	    LINUX_AT_EMPTY_PATH);
276 	static const l_uint fh_size = sizeof(fhandle_t);
277 
278 	fhandle_t fh;
279 	l_uint fh_bytes;
280 	l_int mount_id;
281 	int error, fd, bsd_flags;
282 
283 	if (args->flags & ~valid_flags)
284 		return (EINVAL);
285 
286 	fd = args->dirfd;
287 	if (fd == LINUX_AT_FDCWD)
288 		fd = AT_FDCWD;
289 
290 	bsd_flags = 0;
291 	if (!(args->flags & LINUX_AT_SYMLINK_FOLLOW))
292 		bsd_flags |= AT_SYMLINK_NOFOLLOW;
293 	if ((args->flags & LINUX_AT_EMPTY_PATH) != 0)
294 		bsd_flags |= AT_EMPTY_PATH;
295 
296 	if (!LUSECONVPATH(td)) {
297 		error = kern_getfhat(td, bsd_flags, fd, args->name,
298 		    UIO_USERSPACE, &fh, UIO_SYSSPACE);
299 	} else {
300 		char *path;
301 
302 		LCONVPATH_AT(args->name, &path, 0, fd);
303 		error = kern_getfhat(td, bsd_flags, fd, path, UIO_SYSSPACE,
304 		    &fh, UIO_SYSSPACE);
305 		LFREEPATH(path);
306 	}
307 	if (error != 0)
308 		return (error);
309 
310 	/* Emit mount_id -- required before EOVERFLOW case. */
311 	mount_id = (fh.fh_fsid.val[0] ^ fh.fh_fsid.val[1]);
312 	error = copyout(&mount_id, args->mnt_id, sizeof(mount_id));
313 	if (error != 0)
314 		return (error);
315 
316 	/* Check if there is room for handle. */
317 	error = copyin(&args->handle->handle_bytes, &fh_bytes,
318 	    sizeof(fh_bytes));
319 	if (error != 0)
320 		return (error);
321 
322 	if (fh_bytes < fh_size) {
323 		error = copyout(&fh_size, &args->handle->handle_bytes,
324 		    sizeof(fh_size));
325 		if (error == 0)
326 			error = EOVERFLOW;
327 		return (error);
328 	}
329 
330 	/* Emit handle. */
331 	mount_id = 0;
332 	/*
333 	 * We don't use handle_type for anything yet, but initialize a known
334 	 * value.
335 	 */
336 	error = copyout(&mount_id, &args->handle->handle_type,
337 	    sizeof(mount_id));
338 	if (error != 0)
339 		return (error);
340 
341 	error = copyout(&fh, &args->handle->f_handle,
342 	    sizeof(fh));
343 	return (error);
344 }
345 
346 int
347 linux_open_by_handle_at(struct thread *td,
348     struct linux_open_by_handle_at_args *args)
349 {
350 	l_uint fh_bytes;
351 	int bsd_flags, error;
352 
353 	error = copyin(&args->handle->handle_bytes, &fh_bytes,
354 	    sizeof(fh_bytes));
355 	if (error != 0)
356 		return (error);
357 
358 	if (fh_bytes < sizeof(fhandle_t))
359 		return (EINVAL);
360 
361 	bsd_flags = linux_common_openflags(args->flags);
362 	return (kern_fhopen(td, (void *)&args->handle->f_handle, bsd_flags));
363 }
364 
365 int
366 linux_lseek(struct thread *td, struct linux_lseek_args *args)
367 {
368 
369 	return (kern_lseek(td, args->fdes, args->off, args->whence));
370 }
371 
372 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
373 int
374 linux_llseek(struct thread *td, struct linux_llseek_args *args)
375 {
376 	int error;
377 	off_t off;
378 
379 	off = (args->olow) | (((off_t) args->ohigh) << 32);
380 
381 	error = kern_lseek(td, args->fd, off, args->whence);
382 	if (error != 0)
383 		return (error);
384 
385 	error = copyout(td->td_retval, args->res, sizeof(off_t));
386 	if (error != 0)
387 		return (error);
388 
389 	td->td_retval[0] = 0;
390 	return (0);
391 }
392 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
393 
394 /*
395  * Note that linux_getdents(2) and linux_getdents64(2) have the same
396  * arguments. They only differ in the definition of struct dirent they
397  * operate on.
398  * Note that linux_readdir(2) is a special case of linux_getdents(2)
399  * where count is always equals 1, meaning that the buffer is one
400  * dirent-structure in size and that the code can't handle more anyway.
401  * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
402  * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
403  * trash user stack.
404  */
405 
406 static int
407 linux_getdents_error(struct thread *td, int fd, int err)
408 {
409 	struct vnode *vp;
410 	struct file *fp;
411 	int error;
412 
413 	/* Linux return ENOTDIR in case when fd is not a directory. */
414 	error = getvnode(td, fd, &cap_read_rights, &fp);
415 	if (error != 0)
416 		return (error);
417 	vp = fp->f_vnode;
418 	if (vp->v_type != VDIR) {
419 		fdrop(fp, td);
420 		return (ENOTDIR);
421 	}
422 	fdrop(fp, td);
423 	return (err);
424 }
425 
426 struct l_dirent {
427 	l_ulong		d_ino;
428 	l_off_t		d_off;
429 	l_ushort	d_reclen;
430 	char		d_name[LINUX_NAME_MAX + 1];
431 };
432 
433 struct l_dirent64 {
434 	uint64_t	d_ino;
435 	int64_t		d_off;
436 	l_ushort	d_reclen;
437 	u_char		d_type;
438 	char		d_name[LINUX_NAME_MAX + 1];
439 };
440 
441 /*
442  * Linux uses the last byte in the dirent buffer to store d_type,
443  * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
444  */
445 #define LINUX_RECLEN(namlen)						\
446     roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
447 
448 #define LINUX_RECLEN64(namlen)						\
449     roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1,		\
450     sizeof(uint64_t))
451 
452 #ifdef LINUX_LEGACY_SYSCALLS
453 int
454 linux_getdents(struct thread *td, struct linux_getdents_args *args)
455 {
456 	struct dirent *bdp;
457 	caddr_t inp, buf;		/* BSD-format */
458 	int len, reclen;		/* BSD-format */
459 	caddr_t outp;			/* Linux-format */
460 	int resid, linuxreclen;		/* Linux-format */
461 	caddr_t lbuf;			/* Linux-format */
462 	off_t base;
463 	struct l_dirent *linux_dirent;
464 	int buflen, error;
465 	size_t retval;
466 
467 	buflen = min(args->count, MAXBSIZE);
468 	buf = malloc(buflen, M_TEMP, M_WAITOK);
469 
470 	error = kern_getdirentries(td, args->fd, buf, buflen,
471 	    &base, NULL, UIO_SYSSPACE);
472 	if (error != 0) {
473 		error = linux_getdents_error(td, args->fd, error);
474 		goto out1;
475 	}
476 
477 	lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
478 
479 	len = td->td_retval[0];
480 	inp = buf;
481 	outp = (caddr_t)args->dent;
482 	resid = args->count;
483 	retval = 0;
484 
485 	while (len > 0) {
486 		bdp = (struct dirent *) inp;
487 		reclen = bdp->d_reclen;
488 		linuxreclen = LINUX_RECLEN(bdp->d_namlen);
489 		/*
490 		 * No more space in the user supplied dirent buffer.
491 		 * Return EINVAL.
492 		 */
493 		if (resid < linuxreclen) {
494 			error = EINVAL;
495 			goto out;
496 		}
497 
498 		linux_dirent = (struct l_dirent*)lbuf;
499 		linux_dirent->d_ino = bdp->d_fileno;
500 		linux_dirent->d_off = bdp->d_off;
501 		linux_dirent->d_reclen = linuxreclen;
502 		/*
503 		 * Copy d_type to last byte of l_dirent buffer
504 		 */
505 		lbuf[linuxreclen - 1] = bdp->d_type;
506 		strlcpy(linux_dirent->d_name, bdp->d_name,
507 		    linuxreclen - offsetof(struct l_dirent, d_name)-1);
508 		error = copyout(linux_dirent, outp, linuxreclen);
509 		if (error != 0)
510 			goto out;
511 
512 		inp += reclen;
513 		base += reclen;
514 		len -= reclen;
515 
516 		retval += linuxreclen;
517 		outp += linuxreclen;
518 		resid -= linuxreclen;
519 	}
520 	td->td_retval[0] = retval;
521 
522 out:
523 	free(lbuf, M_TEMP);
524 out1:
525 	free(buf, M_TEMP);
526 	return (error);
527 }
528 #endif
529 
530 int
531 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
532 {
533 	struct dirent *bdp;
534 	caddr_t inp, buf;		/* BSD-format */
535 	int len, reclen;		/* BSD-format */
536 	caddr_t outp;			/* Linux-format */
537 	int resid, linuxreclen;		/* Linux-format */
538 	caddr_t lbuf;			/* Linux-format */
539 	off_t base;
540 	struct l_dirent64 *linux_dirent64;
541 	int buflen, error;
542 	size_t retval;
543 
544 	buflen = min(args->count, MAXBSIZE);
545 	buf = malloc(buflen, M_TEMP, M_WAITOK);
546 
547 	error = kern_getdirentries(td, args->fd, buf, buflen,
548 	    &base, NULL, UIO_SYSSPACE);
549 	if (error != 0) {
550 		error = linux_getdents_error(td, args->fd, error);
551 		goto out1;
552 	}
553 
554 	lbuf = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
555 
556 	len = td->td_retval[0];
557 	inp = buf;
558 	outp = (caddr_t)args->dirent;
559 	resid = args->count;
560 	retval = 0;
561 
562 	while (len > 0) {
563 		bdp = (struct dirent *) inp;
564 		reclen = bdp->d_reclen;
565 		linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
566 		/*
567 		 * No more space in the user supplied dirent buffer.
568 		 * Return EINVAL.
569 		 */
570 		if (resid < linuxreclen) {
571 			error = EINVAL;
572 			goto out;
573 		}
574 
575 		linux_dirent64 = (struct l_dirent64*)lbuf;
576 		linux_dirent64->d_ino = bdp->d_fileno;
577 		linux_dirent64->d_off = bdp->d_off;
578 		linux_dirent64->d_reclen = linuxreclen;
579 		linux_dirent64->d_type = bdp->d_type;
580 		strlcpy(linux_dirent64->d_name, bdp->d_name,
581 		    linuxreclen - offsetof(struct l_dirent64, d_name));
582 		error = copyout(linux_dirent64, outp, linuxreclen);
583 		if (error != 0)
584 			goto out;
585 
586 		inp += reclen;
587 		base += reclen;
588 		len -= reclen;
589 
590 		retval += linuxreclen;
591 		outp += linuxreclen;
592 		resid -= linuxreclen;
593 	}
594 	td->td_retval[0] = retval;
595 
596 out:
597 	free(lbuf, M_TEMP);
598 out1:
599 	free(buf, M_TEMP);
600 	return (error);
601 }
602 
603 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
604 int
605 linux_readdir(struct thread *td, struct linux_readdir_args *args)
606 {
607 	struct dirent *bdp;
608 	caddr_t buf;			/* BSD-format */
609 	int linuxreclen;		/* Linux-format */
610 	caddr_t lbuf;			/* Linux-format */
611 	off_t base;
612 	struct l_dirent *linux_dirent;
613 	int buflen, error;
614 
615 	buflen = LINUX_RECLEN(LINUX_NAME_MAX);
616 	buf = malloc(buflen, M_TEMP, M_WAITOK);
617 
618 	error = kern_getdirentries(td, args->fd, buf, buflen,
619 	    &base, NULL, UIO_SYSSPACE);
620 	if (error != 0) {
621 		error = linux_getdents_error(td, args->fd, error);
622 		goto out;
623 	}
624 	if (td->td_retval[0] == 0)
625 		goto out;
626 
627 	lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
628 
629 	bdp = (struct dirent *) buf;
630 	linuxreclen = LINUX_RECLEN(bdp->d_namlen);
631 
632 	linux_dirent = (struct l_dirent*)lbuf;
633 	linux_dirent->d_ino = bdp->d_fileno;
634 	linux_dirent->d_off = bdp->d_off;
635 	linux_dirent->d_reclen = bdp->d_namlen;
636 	strlcpy(linux_dirent->d_name, bdp->d_name,
637 	    linuxreclen - offsetof(struct l_dirent, d_name));
638 	error = copyout(linux_dirent, args->dent, linuxreclen);
639 	if (error == 0)
640 		td->td_retval[0] = linuxreclen;
641 
642 	free(lbuf, M_TEMP);
643 out:
644 	free(buf, M_TEMP);
645 	return (error);
646 }
647 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
648 
649 /*
650  * These exist mainly for hooks for doing /compat/linux translation.
651  */
652 
653 #ifdef LINUX_LEGACY_SYSCALLS
654 int
655 linux_access(struct thread *td, struct linux_access_args *args)
656 {
657 	char *path;
658 	int error;
659 
660 	/* Linux convention. */
661 	if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
662 		return (EINVAL);
663 
664 	if (!LUSECONVPATH(td)) {
665 		error = kern_accessat(td, AT_FDCWD, args->path, UIO_USERSPACE, 0,
666 		    args->amode);
667 	} else {
668 		LCONVPATHEXIST(args->path, &path);
669 		error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
670 		    args->amode);
671 		LFREEPATH(path);
672 	}
673 
674 	return (error);
675 }
676 #endif
677 
678 static int
679 linux_do_accessat(struct thread *td, int ldfd, const char *filename,
680     int amode, int flags)
681 {
682 	char *path;
683 	int error, dfd;
684 
685 	/* Linux convention. */
686 	if (amode & ~(F_OK | X_OK | W_OK | R_OK))
687 		return (EINVAL);
688 
689 	dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
690 	if (!LUSECONVPATH(td)) {
691 		error = kern_accessat(td, dfd, filename, UIO_USERSPACE, flags, amode);
692 	} else {
693 		LCONVPATHEXIST_AT(filename, &path, dfd);
694 		error = kern_accessat(td, dfd, path, UIO_SYSSPACE, flags, amode);
695 		LFREEPATH(path);
696 	}
697 
698 	return (error);
699 }
700 
701 int
702 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
703 {
704 
705 	return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
706 	    0));
707 }
708 
709 int
710 linux_faccessat2(struct thread *td, struct linux_faccessat2_args *args)
711 {
712 	int flags, unsupported;
713 
714 	/* XXX. AT_SYMLINK_NOFOLLOW is not supported by kern_accessat */
715 	unsupported = args->flags & ~(LINUX_AT_EACCESS | LINUX_AT_EMPTY_PATH);
716 	if (unsupported != 0) {
717 		linux_msg(td, "faccessat2 unsupported flag 0x%x", unsupported);
718 		return (EINVAL);
719 	}
720 
721 	flags = (args->flags & LINUX_AT_EACCESS) == 0 ? 0 :
722 	    AT_EACCESS;
723 	flags |= (args->flags & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
724 	    AT_EMPTY_PATH;
725 	return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
726 	    flags));
727 }
728 
729 
730 #ifdef LINUX_LEGACY_SYSCALLS
731 int
732 linux_unlink(struct thread *td, struct linux_unlink_args *args)
733 {
734 	char *path;
735 	int error;
736 	struct stat st;
737 
738 	if (!LUSECONVPATH(td)) {
739 		error = kern_funlinkat(td, AT_FDCWD, args->path, FD_NONE,
740 		    UIO_USERSPACE, 0, 0);
741 		if (error == EPERM) {
742 			/* Introduce POSIX noncompliant behaviour of Linux */
743 			if (kern_statat(td, 0, AT_FDCWD, args->path,
744 			    UIO_SYSSPACE, &st, NULL) == 0) {
745 				if (S_ISDIR(st.st_mode))
746 					error = EISDIR;
747 			}
748 		}
749 	} else {
750 		LCONVPATHEXIST(args->path, &path);
751 		error = kern_funlinkat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0, 0);
752 		if (error == EPERM) {
753 			/* Introduce POSIX noncompliant behaviour of Linux */
754 			if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
755 			    NULL) == 0) {
756 				if (S_ISDIR(st.st_mode))
757 					error = EISDIR;
758 			}
759 		}
760 		LFREEPATH(path);
761 	}
762 
763 	return (error);
764 }
765 #endif
766 
767 static int
768 linux_unlinkat_impl(struct thread *td, enum uio_seg pathseg, const char *path,
769     int dfd, struct linux_unlinkat_args *args)
770 {
771 	struct stat st;
772 	int error;
773 
774 	if (args->flag & LINUX_AT_REMOVEDIR)
775 		error = kern_frmdirat(td, dfd, path, FD_NONE, pathseg, 0);
776 	else
777 		error = kern_funlinkat(td, dfd, path, FD_NONE, pathseg, 0, 0);
778 	if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
779 		/* Introduce POSIX noncompliant behaviour of Linux */
780 		if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
781 		    UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
782 			error = EISDIR;
783 	}
784 	return (error);
785 }
786 
787 int
788 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
789 {
790 	char *path;
791 	int error, dfd;
792 
793 	if (args->flag & ~LINUX_AT_REMOVEDIR)
794 		return (EINVAL);
795 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
796 	if (!LUSECONVPATH(td)) {
797 		return (linux_unlinkat_impl(td, UIO_USERSPACE, args->pathname,
798 		    dfd, args));
799 	}
800 	LCONVPATHEXIST_AT(args->pathname, &path, dfd);
801 	error = linux_unlinkat_impl(td, UIO_SYSSPACE, path, dfd, args);
802 	LFREEPATH(path);
803 	return (error);
804 }
805 int
806 linux_chdir(struct thread *td, struct linux_chdir_args *args)
807 {
808 	char *path;
809 	int error;
810 
811 	if (!LUSECONVPATH(td)) {
812 		return (kern_chdir(td, args->path, UIO_USERSPACE));
813 	}
814 	LCONVPATHEXIST(args->path, &path);
815 	error = kern_chdir(td, path, UIO_SYSSPACE);
816 	LFREEPATH(path);
817 	return (error);
818 }
819 
820 #ifdef LINUX_LEGACY_SYSCALLS
821 int
822 linux_chmod(struct thread *td, struct linux_chmod_args *args)
823 {
824 	char *path;
825 	int error;
826 
827 	if (!LUSECONVPATH(td)) {
828 		return (kern_fchmodat(td, AT_FDCWD, args->path, UIO_USERSPACE,
829 		    args->mode, 0));
830 	}
831 	LCONVPATHEXIST(args->path, &path);
832 	error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode, 0);
833 	LFREEPATH(path);
834 	return (error);
835 }
836 #endif
837 
838 int
839 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
840 {
841 	char *path;
842 	int error, dfd;
843 
844 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
845 	if (!LUSECONVPATH(td)) {
846 		return (kern_fchmodat(td, dfd, args->filename, UIO_USERSPACE,
847 		    args->mode, 0));
848 	}
849 	LCONVPATHEXIST_AT(args->filename, &path, dfd);
850 	error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
851 	LFREEPATH(path);
852 	return (error);
853 }
854 
855 #ifdef LINUX_LEGACY_SYSCALLS
856 int
857 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
858 {
859 	char *path;
860 	int error;
861 
862 	if (!LUSECONVPATH(td)) {
863 		return (kern_mkdirat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->mode));
864 	}
865 	LCONVPATHCREAT(args->path, &path);
866 	error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
867 	LFREEPATH(path);
868 	return (error);
869 }
870 #endif
871 
872 int
873 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
874 {
875 	char *path;
876 	int error, dfd;
877 
878 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
879 	if (!LUSECONVPATH(td)) {
880 		return (kern_mkdirat(td, dfd, args->pathname, UIO_USERSPACE, args->mode));
881 	}
882 	LCONVPATHCREAT_AT(args->pathname, &path, dfd);
883 	error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
884 	LFREEPATH(path);
885 	return (error);
886 }
887 
888 #ifdef LINUX_LEGACY_SYSCALLS
889 int
890 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
891 {
892 	char *path;
893 	int error;
894 
895 	if (!LUSECONVPATH(td)) {
896 		return (kern_frmdirat(td, AT_FDCWD, args->path, FD_NONE,
897 		    UIO_USERSPACE, 0));
898 	}
899 	LCONVPATHEXIST(args->path, &path);
900 	error = kern_frmdirat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0);
901 	LFREEPATH(path);
902 	return (error);
903 }
904 
905 int
906 linux_rename(struct thread *td, struct linux_rename_args *args)
907 {
908 	char *from, *to;
909 	int error;
910 
911 	if (!LUSECONVPATH(td)) {
912 		return (kern_renameat(td, AT_FDCWD, args->from, AT_FDCWD,
913 		    args->to, UIO_USERSPACE));
914 	}
915 	LCONVPATHEXIST(args->from, &from);
916 	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
917 	error = linux_emul_convpath(args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
918 	if (to == NULL) {
919 		LFREEPATH(from);
920 		return (error);
921 	}
922 	error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
923 	LFREEPATH(from);
924 	LFREEPATH(to);
925 	return (error);
926 }
927 #endif
928 
929 int
930 linux_renameat(struct thread *td, struct linux_renameat_args *args)
931 {
932 	struct linux_renameat2_args renameat2_args = {
933 	    .olddfd = args->olddfd,
934 	    .oldname = args->oldname,
935 	    .newdfd = args->newdfd,
936 	    .newname = args->newname,
937 	    .flags = 0
938 	};
939 
940 	return (linux_renameat2(td, &renameat2_args));
941 }
942 
943 int
944 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
945 {
946 	char *from, *to;
947 	int error, olddfd, newdfd;
948 
949 	if (args->flags != 0) {
950 		if (args->flags & ~(LINUX_RENAME_EXCHANGE |
951 		    LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
952 			return (EINVAL);
953 		if (args->flags & LINUX_RENAME_EXCHANGE &&
954 		    args->flags & (LINUX_RENAME_NOREPLACE |
955 		    LINUX_RENAME_WHITEOUT))
956 			return (EINVAL);
957 #if 0
958 		/*
959 		 * This spams the console on Ubuntu Focal.
960 		 *
961 		 * What's needed here is a general mechanism to let users know
962 		 * about missing features without hogging the system.
963 		 */
964 		linux_msg(td, "renameat2 unsupported flags 0x%x",
965 		    args->flags);
966 #endif
967 		return (EINVAL);
968 	}
969 
970 	olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
971 	newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
972 	if (!LUSECONVPATH(td)) {
973 		return (kern_renameat(td, olddfd, args->oldname, newdfd,
974 		    args->newname, UIO_USERSPACE));
975 	}
976 	LCONVPATHEXIST_AT(args->oldname, &from, olddfd);
977 	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
978 	error = linux_emul_convpath(args->newname, UIO_USERSPACE, &to, 1, newdfd);
979 	if (to == NULL) {
980 		LFREEPATH(from);
981 		return (error);
982 	}
983 	error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
984 	LFREEPATH(from);
985 	LFREEPATH(to);
986 	return (error);
987 }
988 
989 #ifdef LINUX_LEGACY_SYSCALLS
990 int
991 linux_symlink(struct thread *td, struct linux_symlink_args *args)
992 {
993 	char *path, *to;
994 	int error;
995 
996 	if (!LUSECONVPATH(td)) {
997 		return (kern_symlinkat(td, args->path, AT_FDCWD, args->to,
998 		    UIO_USERSPACE));
999 	}
1000 	LCONVPATHEXIST(args->path, &path);
1001 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
1002 	error = linux_emul_convpath(args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
1003 	if (to == NULL) {
1004 		LFREEPATH(path);
1005 		return (error);
1006 	}
1007 	error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
1008 	LFREEPATH(path);
1009 	LFREEPATH(to);
1010 	return (error);
1011 }
1012 #endif
1013 
1014 int
1015 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
1016 {
1017 	char *path, *to;
1018 	int error, dfd;
1019 
1020 	dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
1021 	if (!LUSECONVPATH(td)) {
1022 		return (kern_symlinkat(td, args->oldname, dfd, args->newname,
1023 		    UIO_USERSPACE));
1024 	}
1025 	LCONVPATHEXIST(args->oldname, &path);
1026 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
1027 	error = linux_emul_convpath(args->newname, UIO_USERSPACE, &to, 1, dfd);
1028 	if (to == NULL) {
1029 		LFREEPATH(path);
1030 		return (error);
1031 	}
1032 	error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
1033 	LFREEPATH(path);
1034 	LFREEPATH(to);
1035 	return (error);
1036 }
1037 
1038 #ifdef LINUX_LEGACY_SYSCALLS
1039 int
1040 linux_readlink(struct thread *td, struct linux_readlink_args *args)
1041 {
1042 	char *name;
1043 	int error;
1044 
1045 	if (args->count <= 0)
1046 		return (EINVAL);
1047 
1048 	if (!LUSECONVPATH(td)) {
1049 		return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE,
1050 		    args->buf, UIO_USERSPACE, args->count));
1051 	}
1052 	LCONVPATHEXIST(args->name, &name);
1053 	error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
1054 	    args->buf, UIO_USERSPACE, args->count);
1055 	LFREEPATH(name);
1056 	return (error);
1057 }
1058 #endif
1059 
1060 int
1061 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
1062 {
1063 	char *name;
1064 	int error, dfd;
1065 
1066 	if (args->bufsiz <= 0)
1067 		return (EINVAL);
1068 
1069 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1070 	if (!LUSECONVPATH(td)) {
1071 		return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE,
1072 		    args->buf, UIO_USERSPACE, args->bufsiz));
1073 	}
1074 	LCONVPATHEXIST_AT(args->path, &name, dfd);
1075 	error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
1076 	    UIO_USERSPACE, args->bufsiz);
1077 	LFREEPATH(name);
1078 	return (error);
1079 }
1080 
1081 int
1082 linux_truncate(struct thread *td, struct linux_truncate_args *args)
1083 {
1084 	char *path;
1085 	int error;
1086 
1087 	if (!LUSECONVPATH(td)) {
1088 		return (kern_truncate(td, args->path, UIO_USERSPACE, args->length));
1089 	}
1090 	LCONVPATHEXIST(args->path, &path);
1091 	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
1092 	LFREEPATH(path);
1093 	return (error);
1094 }
1095 
1096 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1097 int
1098 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
1099 {
1100 	char *path;
1101 	off_t length;
1102 	int error;
1103 
1104 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1105 	length = PAIR32TO64(off_t, args->length);
1106 #else
1107 	length = args->length;
1108 #endif
1109 
1110 	if (!LUSECONVPATH(td)) {
1111 		return (kern_truncate(td, args->path, UIO_USERSPACE, length));
1112 	}
1113 	LCONVPATHEXIST(args->path, &path);
1114 	error = kern_truncate(td, path, UIO_SYSSPACE, length);
1115 	LFREEPATH(path);
1116 	return (error);
1117 }
1118 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1119 
1120 int
1121 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
1122 {
1123 
1124 	return (kern_ftruncate(td, args->fd, args->length));
1125 }
1126 
1127 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1128 int
1129 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1130 {
1131 	off_t length;
1132 
1133 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1134 	length = PAIR32TO64(off_t, args->length);
1135 #else
1136 	length = args->length;
1137 #endif
1138 
1139 	return (kern_ftruncate(td, args->fd, length));
1140 }
1141 #endif
1142 
1143 #ifdef LINUX_LEGACY_SYSCALLS
1144 int
1145 linux_link(struct thread *td, struct linux_link_args *args)
1146 {
1147 	char *path, *to;
1148 	int error;
1149 
1150 	if (!LUSECONVPATH(td)) {
1151 		return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to,
1152 		    UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1153 	}
1154 	LCONVPATHEXIST(args->path, &path);
1155 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
1156 	error = linux_emul_convpath(args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
1157 	if (to == NULL) {
1158 		LFREEPATH(path);
1159 		return (error);
1160 	}
1161 	error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
1162 	    AT_SYMLINK_FOLLOW);
1163 	LFREEPATH(path);
1164 	LFREEPATH(to);
1165 	return (error);
1166 }
1167 #endif
1168 
1169 int
1170 linux_linkat(struct thread *td, struct linux_linkat_args *args)
1171 {
1172 	char *path, *to;
1173 	int error, olddfd, newdfd, flag;
1174 
1175 	if (args->flag & ~(LINUX_AT_SYMLINK_FOLLOW | LINUX_AT_EMPTY_PATH))
1176 		return (EINVAL);
1177 
1178 	flag = (args->flag & LINUX_AT_SYMLINK_FOLLOW) != 0 ? AT_SYMLINK_FOLLOW :
1179 	    0;
1180 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) != 0 ? AT_EMPTY_PATH : 0;
1181 
1182 	olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
1183 	newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
1184 	if (!LUSECONVPATH(td)) {
1185 		return (kern_linkat(td, olddfd, newdfd, args->oldname,
1186 		    args->newname, UIO_USERSPACE, flag));
1187 	}
1188 	LCONVPATHEXIST_AT(args->oldname, &path, olddfd);
1189 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
1190 	error = linux_emul_convpath(args->newname, UIO_USERSPACE, &to, 1, newdfd);
1191 	if (to == NULL) {
1192 		LFREEPATH(path);
1193 		return (error);
1194 	}
1195 	error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, flag);
1196 	LFREEPATH(path);
1197 	LFREEPATH(to);
1198 	return (error);
1199 }
1200 
1201 int
1202 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
1203 {
1204 
1205 	return (kern_fsync(td, uap->fd, false));
1206 }
1207 
1208 int
1209 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
1210 {
1211 	off_t nbytes, offset;
1212 
1213 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1214 	nbytes = PAIR32TO64(off_t, uap->nbytes);
1215 	offset = PAIR32TO64(off_t, uap->offset);
1216 #else
1217 	nbytes = uap->nbytes;
1218 	offset = uap->offset;
1219 #endif
1220 
1221 	if (offset < 0 || nbytes < 0 ||
1222 	    (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
1223 	    LINUX_SYNC_FILE_RANGE_WRITE |
1224 	    LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
1225 		return (EINVAL);
1226 	}
1227 
1228 	return (kern_fsync(td, uap->fd, false));
1229 }
1230 
1231 int
1232 linux_pread(struct thread *td, struct linux_pread_args *uap)
1233 {
1234 	struct vnode *vp;
1235 	off_t offset;
1236 	int error;
1237 
1238 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1239 	offset = PAIR32TO64(off_t, uap->offset);
1240 #else
1241 	offset = uap->offset;
1242 #endif
1243 
1244 	error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
1245 	if (error == 0) {
1246 		/* This seems to violate POSIX but Linux does it. */
1247 		error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
1248 		if (error != 0)
1249 			return (error);
1250 		if (vp->v_type == VDIR)
1251 			error = EISDIR;
1252 		vrele(vp);
1253 	}
1254 	return (error);
1255 }
1256 
1257 int
1258 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
1259 {
1260 	off_t offset;
1261 
1262 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1263 	offset = PAIR32TO64(off_t, uap->offset);
1264 #else
1265 	offset = uap->offset;
1266 #endif
1267 
1268 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset));
1269 }
1270 
1271 int
1272 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1273 {
1274 	struct uio *auio;
1275 	int error;
1276 	off_t offset;
1277 
1278 	/*
1279 	 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1280 	 * pos_l and pos_h, respectively, contain the
1281 	 * low order and high order 32 bits of offset.
1282 	 */
1283 	offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1284 	    (sizeof(offset) * 4)) | uap->pos_l;
1285 	if (offset < 0)
1286 		return (EINVAL);
1287 #ifdef COMPAT_LINUX32
1288 	error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1289 #else
1290 	error = copyinuio(uap->vec, uap->vlen, &auio);
1291 #endif
1292 	if (error != 0)
1293 		return (error);
1294 	error = kern_preadv(td, uap->fd, auio, offset);
1295 	free(auio, M_IOV);
1296 	return (error);
1297 }
1298 
1299 int
1300 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1301 {
1302 	struct uio *auio;
1303 	int error;
1304 	off_t offset;
1305 
1306 	/*
1307 	 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1308 	 * pos_l and pos_h, respectively, contain the
1309 	 * low order and high order 32 bits of offset.
1310 	 */
1311 	offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1312 	    (sizeof(offset) * 4)) | uap->pos_l;
1313 	if (offset < 0)
1314 		return (EINVAL);
1315 #ifdef COMPAT_LINUX32
1316 	error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1317 #else
1318 	error = copyinuio(uap->vec, uap->vlen, &auio);
1319 #endif
1320 	if (error != 0)
1321 		return (error);
1322 	error = kern_pwritev(td, uap->fd, auio, offset);
1323 	free(auio, M_IOV);
1324 	return (error);
1325 }
1326 
1327 int
1328 linux_mount(struct thread *td, struct linux_mount_args *args)
1329 {
1330 	struct mntarg *ma = NULL;
1331 	char *fstypename, *mntonname, *mntfromname, *data;
1332 	int error, fsflags;
1333 
1334 	fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1335 	mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1336 	mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1337 	data = NULL;
1338 	error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1,
1339 	    NULL);
1340 	if (error != 0)
1341 		goto out;
1342 	if (args->specialfile != NULL) {
1343 		error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1344 		if (error != 0)
1345 			goto out;
1346 	} else {
1347 		mntfromname[0] = '\0';
1348 	}
1349 	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1350 	if (error != 0)
1351 		goto out;
1352 
1353 	if (strcmp(fstypename, "ext2") == 0) {
1354 		strcpy(fstypename, "ext2fs");
1355 	} else if (strcmp(fstypename, "proc") == 0) {
1356 		strcpy(fstypename, "linprocfs");
1357 	} else if (strcmp(fstypename, "vfat") == 0) {
1358 		strcpy(fstypename, "msdosfs");
1359 	} else if (strcmp(fstypename, "fuse") == 0 ||
1360 	    strncmp(fstypename, "fuse.", 5) == 0) {
1361 		char *fuse_options, *fuse_option, *fuse_name;
1362 
1363 		strcpy(mntfromname, "/dev/fuse");
1364 		strcpy(fstypename, "fusefs");
1365 		data = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1366 		error = copyinstr(args->data, data, MNAMELEN - 1, NULL);
1367 		if (error != 0)
1368 			goto out;
1369 
1370 		fuse_options = data;
1371 		while ((fuse_option = strsep(&fuse_options, ",")) != NULL) {
1372 			fuse_name = strsep(&fuse_option, "=");
1373 			if (fuse_name == NULL || fuse_option == NULL)
1374 				goto out;
1375 			ma = mount_arg(ma, fuse_name, fuse_option, -1);
1376 		}
1377 
1378 		/*
1379 		 * The FUSE server uses Linux errno values instead of FreeBSD
1380 		 * ones; add a flag to tell fuse(4) to do errno translation.
1381 		 */
1382 		ma = mount_arg(ma, "linux_errnos", "1", -1);
1383 	}
1384 
1385 	fsflags = 0;
1386 
1387 	/*
1388 	 * Linux SYNC flag is not included; the closest equivalent
1389 	 * FreeBSD has is !ASYNC, which is our default.
1390 	 */
1391 	if (args->rwflag & LINUX_MS_RDONLY)
1392 		fsflags |= MNT_RDONLY;
1393 	if (args->rwflag & LINUX_MS_NOSUID)
1394 		fsflags |= MNT_NOSUID;
1395 	if (args->rwflag & LINUX_MS_NOEXEC)
1396 		fsflags |= MNT_NOEXEC;
1397 	if (args->rwflag & LINUX_MS_REMOUNT)
1398 		fsflags |= MNT_UPDATE;
1399 
1400 	ma = mount_arg(ma, "fstype", fstypename, -1);
1401 	ma = mount_arg(ma, "fspath", mntonname, -1);
1402 	ma = mount_arg(ma, "from", mntfromname, -1);
1403 	error = kernel_mount(ma, fsflags);
1404 out:
1405 	free(fstypename, M_TEMP);
1406 	free(mntonname, M_TEMP);
1407 	free(mntfromname, M_TEMP);
1408 	free(data, M_TEMP);
1409 	return (error);
1410 }
1411 
1412 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1413 int
1414 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1415 {
1416 
1417 	return (kern_unmount(td, args->path, 0));
1418 }
1419 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1420 
1421 #ifdef LINUX_LEGACY_SYSCALLS
1422 int
1423 linux_umount(struct thread *td, struct linux_umount_args *args)
1424 {
1425 	int flags;
1426 
1427 	flags = 0;
1428 	if ((args->flags & LINUX_MNT_FORCE) != 0) {
1429 		args->flags &= ~LINUX_MNT_FORCE;
1430 		flags |= MNT_FORCE;
1431 	}
1432 	if (args->flags != 0) {
1433 		linux_msg(td, "unsupported umount2 flags %#x", args->flags);
1434 		return (EINVAL);
1435 	}
1436 
1437 	return (kern_unmount(td, args->path, flags));
1438 }
1439 #endif
1440 
1441 /*
1442  * fcntl family of syscalls
1443  */
1444 
1445 struct l_flock {
1446 	l_short		l_type;
1447 	l_short		l_whence;
1448 	l_off_t		l_start;
1449 	l_off_t		l_len;
1450 	l_pid_t		l_pid;
1451 }
1452 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1453 __packed
1454 #endif
1455 ;
1456 
1457 static void
1458 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1459 {
1460 	switch (linux_flock->l_type) {
1461 	case LINUX_F_RDLCK:
1462 		bsd_flock->l_type = F_RDLCK;
1463 		break;
1464 	case LINUX_F_WRLCK:
1465 		bsd_flock->l_type = F_WRLCK;
1466 		break;
1467 	case LINUX_F_UNLCK:
1468 		bsd_flock->l_type = F_UNLCK;
1469 		break;
1470 	default:
1471 		bsd_flock->l_type = -1;
1472 		break;
1473 	}
1474 	bsd_flock->l_whence = linux_flock->l_whence;
1475 	bsd_flock->l_start = (off_t)linux_flock->l_start;
1476 	bsd_flock->l_len = (off_t)linux_flock->l_len;
1477 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1478 	bsd_flock->l_sysid = 0;
1479 }
1480 
1481 static void
1482 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1483 {
1484 	switch (bsd_flock->l_type) {
1485 	case F_RDLCK:
1486 		linux_flock->l_type = LINUX_F_RDLCK;
1487 		break;
1488 	case F_WRLCK:
1489 		linux_flock->l_type = LINUX_F_WRLCK;
1490 		break;
1491 	case F_UNLCK:
1492 		linux_flock->l_type = LINUX_F_UNLCK;
1493 		break;
1494 	}
1495 	linux_flock->l_whence = bsd_flock->l_whence;
1496 	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1497 	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1498 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1499 }
1500 
1501 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1502 struct l_flock64 {
1503 	l_short		l_type;
1504 	l_short		l_whence;
1505 	l_loff_t	l_start;
1506 	l_loff_t	l_len;
1507 	l_pid_t		l_pid;
1508 }
1509 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1510 __packed
1511 #endif
1512 ;
1513 
1514 static void
1515 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1516 {
1517 	switch (linux_flock->l_type) {
1518 	case LINUX_F_RDLCK:
1519 		bsd_flock->l_type = F_RDLCK;
1520 		break;
1521 	case LINUX_F_WRLCK:
1522 		bsd_flock->l_type = F_WRLCK;
1523 		break;
1524 	case LINUX_F_UNLCK:
1525 		bsd_flock->l_type = F_UNLCK;
1526 		break;
1527 	default:
1528 		bsd_flock->l_type = -1;
1529 		break;
1530 	}
1531 	bsd_flock->l_whence = linux_flock->l_whence;
1532 	bsd_flock->l_start = (off_t)linux_flock->l_start;
1533 	bsd_flock->l_len = (off_t)linux_flock->l_len;
1534 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1535 	bsd_flock->l_sysid = 0;
1536 }
1537 
1538 static void
1539 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1540 {
1541 	switch (bsd_flock->l_type) {
1542 	case F_RDLCK:
1543 		linux_flock->l_type = LINUX_F_RDLCK;
1544 		break;
1545 	case F_WRLCK:
1546 		linux_flock->l_type = LINUX_F_WRLCK;
1547 		break;
1548 	case F_UNLCK:
1549 		linux_flock->l_type = LINUX_F_UNLCK;
1550 		break;
1551 	}
1552 	linux_flock->l_whence = bsd_flock->l_whence;
1553 	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1554 	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1555 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1556 }
1557 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1558 
1559 static int
1560 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1561 {
1562 	struct l_flock linux_flock;
1563 	struct flock bsd_flock;
1564 	struct pipe *fpipe;
1565 	struct file *fp;
1566 	long arg;
1567 	int error, result;
1568 
1569 	switch (args->cmd) {
1570 	case LINUX_F_DUPFD:
1571 		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1572 
1573 	case LINUX_F_GETFD:
1574 		return (kern_fcntl(td, args->fd, F_GETFD, 0));
1575 
1576 	case LINUX_F_SETFD:
1577 		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1578 
1579 	case LINUX_F_GETFL:
1580 		error = kern_fcntl(td, args->fd, F_GETFL, 0);
1581 		result = td->td_retval[0];
1582 		td->td_retval[0] = 0;
1583 		if (result & O_RDONLY)
1584 			td->td_retval[0] |= LINUX_O_RDONLY;
1585 		if (result & O_WRONLY)
1586 			td->td_retval[0] |= LINUX_O_WRONLY;
1587 		if (result & O_RDWR)
1588 			td->td_retval[0] |= LINUX_O_RDWR;
1589 		if (result & O_NDELAY)
1590 			td->td_retval[0] |= LINUX_O_NONBLOCK;
1591 		if (result & O_APPEND)
1592 			td->td_retval[0] |= LINUX_O_APPEND;
1593 		if (result & O_FSYNC)
1594 			td->td_retval[0] |= LINUX_O_SYNC;
1595 		if (result & O_ASYNC)
1596 			td->td_retval[0] |= LINUX_O_ASYNC;
1597 #ifdef LINUX_O_NOFOLLOW
1598 		if (result & O_NOFOLLOW)
1599 			td->td_retval[0] |= LINUX_O_NOFOLLOW;
1600 #endif
1601 #ifdef LINUX_O_DIRECT
1602 		if (result & O_DIRECT)
1603 			td->td_retval[0] |= LINUX_O_DIRECT;
1604 #endif
1605 		return (error);
1606 
1607 	case LINUX_F_SETFL:
1608 		arg = 0;
1609 		if (args->arg & LINUX_O_NDELAY)
1610 			arg |= O_NONBLOCK;
1611 		if (args->arg & LINUX_O_APPEND)
1612 			arg |= O_APPEND;
1613 		if (args->arg & LINUX_O_SYNC)
1614 			arg |= O_FSYNC;
1615 		if (args->arg & LINUX_O_ASYNC)
1616 			arg |= O_ASYNC;
1617 #ifdef LINUX_O_NOFOLLOW
1618 		if (args->arg & LINUX_O_NOFOLLOW)
1619 			arg |= O_NOFOLLOW;
1620 #endif
1621 #ifdef LINUX_O_DIRECT
1622 		if (args->arg & LINUX_O_DIRECT)
1623 			arg |= O_DIRECT;
1624 #endif
1625 		return (kern_fcntl(td, args->fd, F_SETFL, arg));
1626 
1627 	case LINUX_F_GETLK:
1628 		error = copyin((void *)args->arg, &linux_flock,
1629 		    sizeof(linux_flock));
1630 		if (error)
1631 			return (error);
1632 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1633 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1634 		if (error)
1635 			return (error);
1636 		bsd_to_linux_flock(&bsd_flock, &linux_flock);
1637 		return (copyout(&linux_flock, (void *)args->arg,
1638 		    sizeof(linux_flock)));
1639 
1640 	case LINUX_F_SETLK:
1641 		error = copyin((void *)args->arg, &linux_flock,
1642 		    sizeof(linux_flock));
1643 		if (error)
1644 			return (error);
1645 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1646 		return (kern_fcntl(td, args->fd, F_SETLK,
1647 		    (intptr_t)&bsd_flock));
1648 
1649 	case LINUX_F_SETLKW:
1650 		error = copyin((void *)args->arg, &linux_flock,
1651 		    sizeof(linux_flock));
1652 		if (error)
1653 			return (error);
1654 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1655 		return (kern_fcntl(td, args->fd, F_SETLKW,
1656 		     (intptr_t)&bsd_flock));
1657 
1658 	case LINUX_F_GETOWN:
1659 		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1660 
1661 	case LINUX_F_SETOWN:
1662 		/*
1663 		 * XXX some Linux applications depend on F_SETOWN having no
1664 		 * significant effect for pipes (SIGIO is not delivered for
1665 		 * pipes under Linux-2.2.35 at least).
1666 		 */
1667 		error = fget(td, args->fd,
1668 		    &cap_fcntl_rights, &fp);
1669 		if (error)
1670 			return (error);
1671 		if (fp->f_type == DTYPE_PIPE) {
1672 			fdrop(fp, td);
1673 			return (EINVAL);
1674 		}
1675 		fdrop(fp, td);
1676 
1677 		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1678 
1679 	case LINUX_F_DUPFD_CLOEXEC:
1680 		return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1681 	/*
1682 	 * Our F_SEAL_* values match Linux one for maximum compatibility.  So we
1683 	 * only needed to account for different values for fcntl(2) commands.
1684 	 */
1685 	case LINUX_F_GET_SEALS:
1686 		error = kern_fcntl(td, args->fd, F_GET_SEALS, 0);
1687 		if (error != 0)
1688 			return (error);
1689 		td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0],
1690 		    seal_bitmap, 0);
1691 		return (0);
1692 
1693 	case LINUX_F_ADD_SEALS:
1694 		return (kern_fcntl(td, args->fd, F_ADD_SEALS,
1695 		    linux_to_bsd_bits(args->arg, seal_bitmap, 0)));
1696 
1697 	case LINUX_F_GETPIPE_SZ:
1698 		error = fget(td, args->fd,
1699 		    &cap_fcntl_rights, &fp);
1700 		if (error != 0)
1701 			return (error);
1702 		if (fp->f_type != DTYPE_PIPE) {
1703 			fdrop(fp, td);
1704 			return (EINVAL);
1705 		}
1706 		fpipe = fp->f_data;
1707 		td->td_retval[0] = fpipe->pipe_buffer.size;
1708 		fdrop(fp, td);
1709 		return (0);
1710 
1711 	default:
1712 		linux_msg(td, "unsupported fcntl cmd %d", args->cmd);
1713 		return (EINVAL);
1714 	}
1715 }
1716 
1717 int
1718 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1719 {
1720 
1721 	return (fcntl_common(td, args));
1722 }
1723 
1724 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1725 int
1726 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1727 {
1728 	struct l_flock64 linux_flock;
1729 	struct flock bsd_flock;
1730 	struct linux_fcntl_args fcntl_args;
1731 	int error;
1732 
1733 	switch (args->cmd) {
1734 	case LINUX_F_GETLK64:
1735 		error = copyin((void *)args->arg, &linux_flock,
1736 		    sizeof(linux_flock));
1737 		if (error)
1738 			return (error);
1739 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1740 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1741 		if (error)
1742 			return (error);
1743 		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1744 		return (copyout(&linux_flock, (void *)args->arg,
1745 			    sizeof(linux_flock)));
1746 
1747 	case LINUX_F_SETLK64:
1748 		error = copyin((void *)args->arg, &linux_flock,
1749 		    sizeof(linux_flock));
1750 		if (error)
1751 			return (error);
1752 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1753 		return (kern_fcntl(td, args->fd, F_SETLK,
1754 		    (intptr_t)&bsd_flock));
1755 
1756 	case LINUX_F_SETLKW64:
1757 		error = copyin((void *)args->arg, &linux_flock,
1758 		    sizeof(linux_flock));
1759 		if (error)
1760 			return (error);
1761 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1762 		return (kern_fcntl(td, args->fd, F_SETLKW,
1763 		    (intptr_t)&bsd_flock));
1764 	}
1765 
1766 	fcntl_args.fd = args->fd;
1767 	fcntl_args.cmd = args->cmd;
1768 	fcntl_args.arg = args->arg;
1769 	return (fcntl_common(td, &fcntl_args));
1770 }
1771 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1772 
1773 #ifdef LINUX_LEGACY_SYSCALLS
1774 int
1775 linux_chown(struct thread *td, struct linux_chown_args *args)
1776 {
1777 	char *path;
1778 	int error;
1779 
1780 	if (!LUSECONVPATH(td)) {
1781 		return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
1782 		    args->uid, args->gid, 0));
1783 	}
1784 	LCONVPATHEXIST(args->path, &path);
1785 	error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1786 	    args->gid, 0);
1787 	LFREEPATH(path);
1788 	return (error);
1789 }
1790 #endif
1791 
1792 int
1793 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1794 {
1795 	char *path;
1796 	int error, dfd, flag, unsupported;
1797 
1798 	unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
1799 	if (unsupported != 0) {
1800 		linux_msg(td, "fchownat unsupported flag 0x%x", unsupported);
1801 		return (EINVAL);
1802 	}
1803 
1804 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1805 	    AT_SYMLINK_NOFOLLOW;
1806 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
1807 	    AT_EMPTY_PATH;
1808 
1809 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD :  args->dfd;
1810 	if (!LUSECONVPATH(td)) {
1811 		return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE,
1812 		    args->uid, args->gid, flag));
1813 	}
1814 	LCONVPATHEXIST_AT(args->filename, &path, dfd);
1815 	error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1816 	    flag);
1817 	LFREEPATH(path);
1818 	return (error);
1819 }
1820 
1821 #ifdef LINUX_LEGACY_SYSCALLS
1822 int
1823 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1824 {
1825 	char *path;
1826 	int error;
1827 
1828 	if (!LUSECONVPATH(td)) {
1829 		return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid,
1830 		    args->gid, AT_SYMLINK_NOFOLLOW));
1831 	}
1832 	LCONVPATHEXIST(args->path, &path);
1833 	error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid, args->gid,
1834 	    AT_SYMLINK_NOFOLLOW);
1835 	LFREEPATH(path);
1836 	return (error);
1837 }
1838 #endif
1839 
1840 static int
1841 convert_fadvice(int advice)
1842 {
1843 	switch (advice) {
1844 	case LINUX_POSIX_FADV_NORMAL:
1845 		return (POSIX_FADV_NORMAL);
1846 	case LINUX_POSIX_FADV_RANDOM:
1847 		return (POSIX_FADV_RANDOM);
1848 	case LINUX_POSIX_FADV_SEQUENTIAL:
1849 		return (POSIX_FADV_SEQUENTIAL);
1850 	case LINUX_POSIX_FADV_WILLNEED:
1851 		return (POSIX_FADV_WILLNEED);
1852 	case LINUX_POSIX_FADV_DONTNEED:
1853 		return (POSIX_FADV_DONTNEED);
1854 	case LINUX_POSIX_FADV_NOREUSE:
1855 		return (POSIX_FADV_NOREUSE);
1856 	default:
1857 		return (-1);
1858 	}
1859 }
1860 
1861 int
1862 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1863 {
1864 	off_t offset;
1865 	int advice;
1866 
1867 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1868 	offset = PAIR32TO64(off_t, args->offset);
1869 #else
1870 	offset = args->offset;
1871 #endif
1872 
1873 	advice = convert_fadvice(args->advice);
1874 	if (advice == -1)
1875 		return (EINVAL);
1876 	return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
1877 }
1878 
1879 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1880 int
1881 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1882 {
1883 	off_t len, offset;
1884 	int advice;
1885 
1886 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1887 	len = PAIR32TO64(off_t, args->len);
1888 	offset = PAIR32TO64(off_t, args->offset);
1889 #else
1890 	len = args->len;
1891 	offset = args->offset;
1892 #endif
1893 
1894 	advice = convert_fadvice(args->advice);
1895 	if (advice == -1)
1896 		return (EINVAL);
1897 	return (kern_posix_fadvise(td, args->fd, offset, len, advice));
1898 }
1899 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1900 
1901 #ifdef LINUX_LEGACY_SYSCALLS
1902 int
1903 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1904 {
1905 	int fildes[2];
1906 	int error;
1907 
1908 	error = kern_pipe(td, fildes, 0, NULL, NULL);
1909 	if (error != 0)
1910 		return (error);
1911 
1912 	error = copyout(fildes, args->pipefds, sizeof(fildes));
1913 	if (error != 0) {
1914 		(void)kern_close(td, fildes[0]);
1915 		(void)kern_close(td, fildes[1]);
1916 	}
1917 
1918 	return (error);
1919 }
1920 #endif
1921 
1922 int
1923 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1924 {
1925 	int fildes[2];
1926 	int error, flags;
1927 
1928 	if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1929 		return (EINVAL);
1930 
1931 	flags = 0;
1932 	if ((args->flags & LINUX_O_NONBLOCK) != 0)
1933 		flags |= O_NONBLOCK;
1934 	if ((args->flags & LINUX_O_CLOEXEC) != 0)
1935 		flags |= O_CLOEXEC;
1936 	error = kern_pipe(td, fildes, flags, NULL, NULL);
1937 	if (error != 0)
1938 		return (error);
1939 
1940 	error = copyout(fildes, args->pipefds, sizeof(fildes));
1941 	if (error != 0) {
1942 		(void)kern_close(td, fildes[0]);
1943 		(void)kern_close(td, fildes[1]);
1944 	}
1945 
1946 	return (error);
1947 }
1948 
1949 int
1950 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1951 {
1952 	int cmd;
1953 	intptr_t newfd;
1954 
1955 	if (args->oldfd == args->newfd)
1956 		return (EINVAL);
1957 	if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1958 		return (EINVAL);
1959 	if (args->flags & LINUX_O_CLOEXEC)
1960 		cmd = F_DUP2FD_CLOEXEC;
1961 	else
1962 		cmd = F_DUP2FD;
1963 
1964 	newfd = args->newfd;
1965 	return (kern_fcntl(td, args->oldfd, cmd, newfd));
1966 }
1967 
1968 int
1969 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1970 {
1971 	off_t len, offset;
1972 
1973 	/*
1974 	 * We emulate only posix_fallocate system call for which
1975 	 * mode should be 0.
1976 	 */
1977 	if (args->mode != 0)
1978 		return (EOPNOTSUPP);
1979 
1980 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1981 	len = PAIR32TO64(off_t, args->len);
1982 	offset = PAIR32TO64(off_t, args->offset);
1983 #else
1984 	len = args->len;
1985 	offset = args->offset;
1986 #endif
1987 
1988 	return (kern_posix_fallocate(td, args->fd, offset, len));
1989 }
1990 
1991 int
1992 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
1993     *args)
1994 {
1995 	l_loff_t inoff, outoff, *inoffp, *outoffp;
1996 	int error, flags;
1997 
1998 	/*
1999 	 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
2000 	 * the native implementation.  Enforce it.
2001 	 */
2002 	if (args->flags != 0) {
2003 		linux_msg(td, "copy_file_range unsupported flags 0x%x",
2004 		    args->flags);
2005 		return (EINVAL);
2006 	}
2007 	flags = 0;
2008 	inoffp = outoffp = NULL;
2009 	if (args->off_in != NULL) {
2010 		error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
2011 		if (error != 0)
2012 			return (error);
2013 		inoffp = &inoff;
2014 	}
2015 	if (args->off_out != NULL) {
2016 		error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
2017 		if (error != 0)
2018 			return (error);
2019 		outoffp = &outoff;
2020 	}
2021 
2022 	error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
2023 	    outoffp, args->len, flags);
2024 	if (error == 0 && args->off_in != NULL)
2025 		error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
2026 	if (error == 0 && args->off_out != NULL)
2027 		error = copyout(outoffp, args->off_out, sizeof(l_loff_t));
2028 	return (error);
2029 }
2030 
2031 #define	LINUX_MEMFD_PREFIX	"memfd:"
2032 
2033 int
2034 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
2035 {
2036 	char memfd_name[LINUX_NAME_MAX + 1];
2037 	int error, flags, shmflags, oflags;
2038 
2039 	/*
2040 	 * This is our clever trick to avoid the heap allocation to copy in the
2041 	 * uname.  We don't really need to go this far out of our way, but it
2042 	 * does keep the rest of this function fairly clean as they don't have
2043 	 * to worry about cleanup on the way out.
2044 	 */
2045 	error = copyinstr(args->uname_ptr,
2046 	    memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1,
2047 	    LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL);
2048 	if (error != 0) {
2049 		if (error == ENAMETOOLONG)
2050 			error = EINVAL;
2051 		return (error);
2052 	}
2053 
2054 	memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1);
2055 	flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0);
2056 	if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB |
2057 	    MFD_HUGE_MASK)) != 0)
2058 		return (EINVAL);
2059 	/* Size specified but no HUGETLB. */
2060 	if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0)
2061 		return (EINVAL);
2062 	/* We don't actually support HUGETLB. */
2063 	if ((flags & MFD_HUGETLB) != 0)
2064 		return (ENOSYS);
2065 	oflags = O_RDWR;
2066 	shmflags = SHM_GROW_ON_WRITE;
2067 	if ((flags & MFD_CLOEXEC) != 0)
2068 		oflags |= O_CLOEXEC;
2069 	if ((flags & MFD_ALLOW_SEALING) != 0)
2070 		shmflags |= SHM_ALLOW_SEALING;
2071 	return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL,
2072 	    memfd_name));
2073 }
2074 
2075 int
2076 linux_splice(struct thread *td, struct linux_splice_args *args)
2077 {
2078 
2079 	linux_msg(td, "syscall splice not really implemented");
2080 
2081 	/*
2082 	 * splice(2) is documented to return EINVAL in various circumstances;
2083 	 * returning it instead of ENOSYS should hint the caller to use fallback
2084 	 * instead.
2085 	 */
2086 	return (EINVAL);
2087 }
2088