xref: /freebsd/sys/compat/linux/linux_file.c (revision 681ce946f33e75c590e97c53076e86dff1fe8f4a)
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 = base + reclen;
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 = base + reclen;
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 = linuxreclen;
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 (!LUSECONVPATH(td)) {
1046 		return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE,
1047 		    args->buf, UIO_USERSPACE, args->count));
1048 	}
1049 	LCONVPATHEXIST(args->name, &name);
1050 	error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
1051 	    args->buf, UIO_USERSPACE, args->count);
1052 	LFREEPATH(name);
1053 	return (error);
1054 }
1055 #endif
1056 
1057 int
1058 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
1059 {
1060 	char *name;
1061 	int error, dfd;
1062 
1063 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1064 	if (!LUSECONVPATH(td)) {
1065 		return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE,
1066 		    args->buf, UIO_USERSPACE, args->bufsiz));
1067 	}
1068 	LCONVPATHEXIST_AT(args->path, &name, dfd);
1069 	error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
1070 	    UIO_USERSPACE, args->bufsiz);
1071 	LFREEPATH(name);
1072 	return (error);
1073 }
1074 
1075 int
1076 linux_truncate(struct thread *td, struct linux_truncate_args *args)
1077 {
1078 	char *path;
1079 	int error;
1080 
1081 	if (!LUSECONVPATH(td)) {
1082 		return (kern_truncate(td, args->path, UIO_USERSPACE, args->length));
1083 	}
1084 	LCONVPATHEXIST(args->path, &path);
1085 	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
1086 	LFREEPATH(path);
1087 	return (error);
1088 }
1089 
1090 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1091 int
1092 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
1093 {
1094 	char *path;
1095 	off_t length;
1096 	int error;
1097 
1098 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1099 	length = PAIR32TO64(off_t, args->length);
1100 #else
1101 	length = args->length;
1102 #endif
1103 
1104 	if (!LUSECONVPATH(td)) {
1105 		return (kern_truncate(td, args->path, UIO_USERSPACE, length));
1106 	}
1107 	LCONVPATHEXIST(args->path, &path);
1108 	error = kern_truncate(td, path, UIO_SYSSPACE, length);
1109 	LFREEPATH(path);
1110 	return (error);
1111 }
1112 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1113 
1114 int
1115 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
1116 {
1117 
1118 	return (kern_ftruncate(td, args->fd, args->length));
1119 }
1120 
1121 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1122 int
1123 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1124 {
1125 	off_t length;
1126 
1127 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1128 	length = PAIR32TO64(off_t, args->length);
1129 #else
1130 	length = args->length;
1131 #endif
1132 
1133 	return (kern_ftruncate(td, args->fd, length));
1134 }
1135 #endif
1136 
1137 #ifdef LINUX_LEGACY_SYSCALLS
1138 int
1139 linux_link(struct thread *td, struct linux_link_args *args)
1140 {
1141 	char *path, *to;
1142 	int error;
1143 
1144 	if (!LUSECONVPATH(td)) {
1145 		return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to,
1146 		    UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1147 	}
1148 	LCONVPATHEXIST(args->path, &path);
1149 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
1150 	error = linux_emul_convpath(args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
1151 	if (to == NULL) {
1152 		LFREEPATH(path);
1153 		return (error);
1154 	}
1155 	error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
1156 	    AT_SYMLINK_FOLLOW);
1157 	LFREEPATH(path);
1158 	LFREEPATH(to);
1159 	return (error);
1160 }
1161 #endif
1162 
1163 int
1164 linux_linkat(struct thread *td, struct linux_linkat_args *args)
1165 {
1166 	char *path, *to;
1167 	int error, olddfd, newdfd, flag;
1168 
1169 	if (args->flag & ~(LINUX_AT_SYMLINK_FOLLOW | LINUX_AT_EMPTY_PATH))
1170 		return (EINVAL);
1171 
1172 	flag = (args->flag & LINUX_AT_SYMLINK_FOLLOW) != 0 ? AT_SYMLINK_FOLLOW :
1173 	    0;
1174 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) != 0 ? AT_EMPTY_PATH : 0;
1175 
1176 	olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
1177 	newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
1178 	if (!LUSECONVPATH(td)) {
1179 		return (kern_linkat(td, olddfd, newdfd, args->oldname,
1180 		    args->newname, UIO_USERSPACE, flag));
1181 	}
1182 	LCONVPATHEXIST_AT(args->oldname, &path, olddfd);
1183 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
1184 	error = linux_emul_convpath(args->newname, UIO_USERSPACE, &to, 1, newdfd);
1185 	if (to == NULL) {
1186 		LFREEPATH(path);
1187 		return (error);
1188 	}
1189 	error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, flag);
1190 	LFREEPATH(path);
1191 	LFREEPATH(to);
1192 	return (error);
1193 }
1194 
1195 int
1196 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
1197 {
1198 
1199 	return (kern_fsync(td, uap->fd, false));
1200 }
1201 
1202 int
1203 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
1204 {
1205 	off_t nbytes, offset;
1206 
1207 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1208 	nbytes = PAIR32TO64(off_t, uap->nbytes);
1209 	offset = PAIR32TO64(off_t, uap->offset);
1210 #else
1211 	nbytes = uap->nbytes;
1212 	offset = uap->offset;
1213 #endif
1214 
1215 	if (offset < 0 || nbytes < 0 ||
1216 	    (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
1217 	    LINUX_SYNC_FILE_RANGE_WRITE |
1218 	    LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
1219 		return (EINVAL);
1220 	}
1221 
1222 	return (kern_fsync(td, uap->fd, false));
1223 }
1224 
1225 int
1226 linux_pread(struct thread *td, struct linux_pread_args *uap)
1227 {
1228 	struct vnode *vp;
1229 	off_t offset;
1230 	int error;
1231 
1232 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1233 	offset = PAIR32TO64(off_t, uap->offset);
1234 #else
1235 	offset = uap->offset;
1236 #endif
1237 
1238 	error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
1239 	if (error == 0) {
1240 		/* This seems to violate POSIX but Linux does it. */
1241 		error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
1242 		if (error != 0)
1243 			return (error);
1244 		if (vp->v_type == VDIR)
1245 			error = EISDIR;
1246 		vrele(vp);
1247 	}
1248 	return (error);
1249 }
1250 
1251 int
1252 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
1253 {
1254 	off_t offset;
1255 
1256 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1257 	offset = PAIR32TO64(off_t, uap->offset);
1258 #else
1259 	offset = uap->offset;
1260 #endif
1261 
1262 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset));
1263 }
1264 
1265 int
1266 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1267 {
1268 	struct uio *auio;
1269 	int error;
1270 	off_t offset;
1271 
1272 	/*
1273 	 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1274 	 * pos_l and pos_h, respectively, contain the
1275 	 * low order and high order 32 bits of offset.
1276 	 */
1277 	offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1278 	    (sizeof(offset) * 4)) | uap->pos_l;
1279 	if (offset < 0)
1280 		return (EINVAL);
1281 #ifdef COMPAT_LINUX32
1282 	error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1283 #else
1284 	error = copyinuio(uap->vec, uap->vlen, &auio);
1285 #endif
1286 	if (error != 0)
1287 		return (error);
1288 	error = kern_preadv(td, uap->fd, auio, offset);
1289 	free(auio, M_IOV);
1290 	return (error);
1291 }
1292 
1293 int
1294 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1295 {
1296 	struct uio *auio;
1297 	int error;
1298 	off_t offset;
1299 
1300 	/*
1301 	 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1302 	 * pos_l and pos_h, respectively, contain the
1303 	 * low order and high order 32 bits of offset.
1304 	 */
1305 	offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1306 	    (sizeof(offset) * 4)) | uap->pos_l;
1307 	if (offset < 0)
1308 		return (EINVAL);
1309 #ifdef COMPAT_LINUX32
1310 	error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1311 #else
1312 	error = copyinuio(uap->vec, uap->vlen, &auio);
1313 #endif
1314 	if (error != 0)
1315 		return (error);
1316 	error = kern_pwritev(td, uap->fd, auio, offset);
1317 	free(auio, M_IOV);
1318 	return (error);
1319 }
1320 
1321 int
1322 linux_mount(struct thread *td, struct linux_mount_args *args)
1323 {
1324 	struct mntarg *ma = NULL;
1325 	char *fstypename, *mntonname, *mntfromname, *data;
1326 	int error, fsflags;
1327 
1328 	fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1329 	mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1330 	mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1331 	data = NULL;
1332 	error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1,
1333 	    NULL);
1334 	if (error != 0)
1335 		goto out;
1336 	if (args->specialfile != NULL) {
1337 		error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1338 		if (error != 0)
1339 			goto out;
1340 	} else {
1341 		mntfromname[0] = '\0';
1342 	}
1343 	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1344 	if (error != 0)
1345 		goto out;
1346 
1347 	if (strcmp(fstypename, "ext2") == 0) {
1348 		strcpy(fstypename, "ext2fs");
1349 	} else if (strcmp(fstypename, "proc") == 0) {
1350 		strcpy(fstypename, "linprocfs");
1351 	} else if (strcmp(fstypename, "vfat") == 0) {
1352 		strcpy(fstypename, "msdosfs");
1353 	} else if (strcmp(fstypename, "fuse") == 0 ||
1354 	    strncmp(fstypename, "fuse.", 5) == 0) {
1355 		char *fuse_options, *fuse_option, *fuse_name;
1356 
1357 		strcpy(mntfromname, "/dev/fuse");
1358 		strcpy(fstypename, "fusefs");
1359 		data = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1360 		error = copyinstr(args->data, data, MNAMELEN - 1, NULL);
1361 		if (error != 0)
1362 			goto out;
1363 
1364 		fuse_options = data;
1365 		while ((fuse_option = strsep(&fuse_options, ",")) != NULL) {
1366 			fuse_name = strsep(&fuse_option, "=");
1367 			if (fuse_name == NULL || fuse_option == NULL)
1368 				goto out;
1369 			ma = mount_arg(ma, fuse_name, fuse_option, -1);
1370 		}
1371 
1372 		/*
1373 		 * The FUSE server uses Linux errno values instead of FreeBSD
1374 		 * ones; add a flag to tell fuse(4) to do errno translation.
1375 		 */
1376 		ma = mount_arg(ma, "linux_errnos", "1", -1);
1377 	}
1378 
1379 	fsflags = 0;
1380 
1381 	/*
1382 	 * Linux SYNC flag is not included; the closest equivalent
1383 	 * FreeBSD has is !ASYNC, which is our default.
1384 	 */
1385 	if (args->rwflag & LINUX_MS_RDONLY)
1386 		fsflags |= MNT_RDONLY;
1387 	if (args->rwflag & LINUX_MS_NOSUID)
1388 		fsflags |= MNT_NOSUID;
1389 	if (args->rwflag & LINUX_MS_NOEXEC)
1390 		fsflags |= MNT_NOEXEC;
1391 	if (args->rwflag & LINUX_MS_REMOUNT)
1392 		fsflags |= MNT_UPDATE;
1393 
1394 	ma = mount_arg(ma, "fstype", fstypename, -1);
1395 	ma = mount_arg(ma, "fspath", mntonname, -1);
1396 	ma = mount_arg(ma, "from", mntfromname, -1);
1397 	error = kernel_mount(ma, fsflags);
1398 out:
1399 	free(fstypename, M_TEMP);
1400 	free(mntonname, M_TEMP);
1401 	free(mntfromname, M_TEMP);
1402 	free(data, M_TEMP);
1403 	return (error);
1404 }
1405 
1406 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1407 int
1408 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1409 {
1410 
1411 	return (kern_unmount(td, args->path, 0));
1412 }
1413 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1414 
1415 #ifdef LINUX_LEGACY_SYSCALLS
1416 int
1417 linux_umount(struct thread *td, struct linux_umount_args *args)
1418 {
1419 	int flags;
1420 
1421 	flags = 0;
1422 	if ((args->flags & LINUX_MNT_FORCE) != 0) {
1423 		args->flags &= ~LINUX_MNT_FORCE;
1424 		flags |= MNT_FORCE;
1425 	}
1426 	if (args->flags != 0) {
1427 		linux_msg(td, "unsupported umount2 flags %#x", args->flags);
1428 		return (EINVAL);
1429 	}
1430 
1431 	return (kern_unmount(td, args->path, flags));
1432 }
1433 #endif
1434 
1435 /*
1436  * fcntl family of syscalls
1437  */
1438 
1439 struct l_flock {
1440 	l_short		l_type;
1441 	l_short		l_whence;
1442 	l_off_t		l_start;
1443 	l_off_t		l_len;
1444 	l_pid_t		l_pid;
1445 }
1446 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1447 __packed
1448 #endif
1449 ;
1450 
1451 static void
1452 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1453 {
1454 	switch (linux_flock->l_type) {
1455 	case LINUX_F_RDLCK:
1456 		bsd_flock->l_type = F_RDLCK;
1457 		break;
1458 	case LINUX_F_WRLCK:
1459 		bsd_flock->l_type = F_WRLCK;
1460 		break;
1461 	case LINUX_F_UNLCK:
1462 		bsd_flock->l_type = F_UNLCK;
1463 		break;
1464 	default:
1465 		bsd_flock->l_type = -1;
1466 		break;
1467 	}
1468 	bsd_flock->l_whence = linux_flock->l_whence;
1469 	bsd_flock->l_start = (off_t)linux_flock->l_start;
1470 	bsd_flock->l_len = (off_t)linux_flock->l_len;
1471 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1472 	bsd_flock->l_sysid = 0;
1473 }
1474 
1475 static void
1476 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1477 {
1478 	switch (bsd_flock->l_type) {
1479 	case F_RDLCK:
1480 		linux_flock->l_type = LINUX_F_RDLCK;
1481 		break;
1482 	case F_WRLCK:
1483 		linux_flock->l_type = LINUX_F_WRLCK;
1484 		break;
1485 	case F_UNLCK:
1486 		linux_flock->l_type = LINUX_F_UNLCK;
1487 		break;
1488 	}
1489 	linux_flock->l_whence = bsd_flock->l_whence;
1490 	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1491 	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1492 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1493 }
1494 
1495 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1496 struct l_flock64 {
1497 	l_short		l_type;
1498 	l_short		l_whence;
1499 	l_loff_t	l_start;
1500 	l_loff_t	l_len;
1501 	l_pid_t		l_pid;
1502 }
1503 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1504 __packed
1505 #endif
1506 ;
1507 
1508 static void
1509 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1510 {
1511 	switch (linux_flock->l_type) {
1512 	case LINUX_F_RDLCK:
1513 		bsd_flock->l_type = F_RDLCK;
1514 		break;
1515 	case LINUX_F_WRLCK:
1516 		bsd_flock->l_type = F_WRLCK;
1517 		break;
1518 	case LINUX_F_UNLCK:
1519 		bsd_flock->l_type = F_UNLCK;
1520 		break;
1521 	default:
1522 		bsd_flock->l_type = -1;
1523 		break;
1524 	}
1525 	bsd_flock->l_whence = linux_flock->l_whence;
1526 	bsd_flock->l_start = (off_t)linux_flock->l_start;
1527 	bsd_flock->l_len = (off_t)linux_flock->l_len;
1528 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1529 	bsd_flock->l_sysid = 0;
1530 }
1531 
1532 static void
1533 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1534 {
1535 	switch (bsd_flock->l_type) {
1536 	case F_RDLCK:
1537 		linux_flock->l_type = LINUX_F_RDLCK;
1538 		break;
1539 	case F_WRLCK:
1540 		linux_flock->l_type = LINUX_F_WRLCK;
1541 		break;
1542 	case F_UNLCK:
1543 		linux_flock->l_type = LINUX_F_UNLCK;
1544 		break;
1545 	}
1546 	linux_flock->l_whence = bsd_flock->l_whence;
1547 	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1548 	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1549 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1550 }
1551 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1552 
1553 static int
1554 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1555 {
1556 	struct l_flock linux_flock;
1557 	struct flock bsd_flock;
1558 	struct pipe *fpipe;
1559 	struct file *fp;
1560 	long arg;
1561 	int error, result;
1562 
1563 	switch (args->cmd) {
1564 	case LINUX_F_DUPFD:
1565 		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1566 
1567 	case LINUX_F_GETFD:
1568 		return (kern_fcntl(td, args->fd, F_GETFD, 0));
1569 
1570 	case LINUX_F_SETFD:
1571 		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1572 
1573 	case LINUX_F_GETFL:
1574 		error = kern_fcntl(td, args->fd, F_GETFL, 0);
1575 		result = td->td_retval[0];
1576 		td->td_retval[0] = 0;
1577 		if (result & O_RDONLY)
1578 			td->td_retval[0] |= LINUX_O_RDONLY;
1579 		if (result & O_WRONLY)
1580 			td->td_retval[0] |= LINUX_O_WRONLY;
1581 		if (result & O_RDWR)
1582 			td->td_retval[0] |= LINUX_O_RDWR;
1583 		if (result & O_NDELAY)
1584 			td->td_retval[0] |= LINUX_O_NONBLOCK;
1585 		if (result & O_APPEND)
1586 			td->td_retval[0] |= LINUX_O_APPEND;
1587 		if (result & O_FSYNC)
1588 			td->td_retval[0] |= LINUX_O_SYNC;
1589 		if (result & O_ASYNC)
1590 			td->td_retval[0] |= LINUX_O_ASYNC;
1591 #ifdef LINUX_O_NOFOLLOW
1592 		if (result & O_NOFOLLOW)
1593 			td->td_retval[0] |= LINUX_O_NOFOLLOW;
1594 #endif
1595 #ifdef LINUX_O_DIRECT
1596 		if (result & O_DIRECT)
1597 			td->td_retval[0] |= LINUX_O_DIRECT;
1598 #endif
1599 		return (error);
1600 
1601 	case LINUX_F_SETFL:
1602 		arg = 0;
1603 		if (args->arg & LINUX_O_NDELAY)
1604 			arg |= O_NONBLOCK;
1605 		if (args->arg & LINUX_O_APPEND)
1606 			arg |= O_APPEND;
1607 		if (args->arg & LINUX_O_SYNC)
1608 			arg |= O_FSYNC;
1609 		if (args->arg & LINUX_O_ASYNC)
1610 			arg |= O_ASYNC;
1611 #ifdef LINUX_O_NOFOLLOW
1612 		if (args->arg & LINUX_O_NOFOLLOW)
1613 			arg |= O_NOFOLLOW;
1614 #endif
1615 #ifdef LINUX_O_DIRECT
1616 		if (args->arg & LINUX_O_DIRECT)
1617 			arg |= O_DIRECT;
1618 #endif
1619 		return (kern_fcntl(td, args->fd, F_SETFL, arg));
1620 
1621 	case LINUX_F_GETLK:
1622 		error = copyin((void *)args->arg, &linux_flock,
1623 		    sizeof(linux_flock));
1624 		if (error)
1625 			return (error);
1626 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1627 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1628 		if (error)
1629 			return (error);
1630 		bsd_to_linux_flock(&bsd_flock, &linux_flock);
1631 		return (copyout(&linux_flock, (void *)args->arg,
1632 		    sizeof(linux_flock)));
1633 
1634 	case LINUX_F_SETLK:
1635 		error = copyin((void *)args->arg, &linux_flock,
1636 		    sizeof(linux_flock));
1637 		if (error)
1638 			return (error);
1639 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1640 		return (kern_fcntl(td, args->fd, F_SETLK,
1641 		    (intptr_t)&bsd_flock));
1642 
1643 	case LINUX_F_SETLKW:
1644 		error = copyin((void *)args->arg, &linux_flock,
1645 		    sizeof(linux_flock));
1646 		if (error)
1647 			return (error);
1648 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1649 		return (kern_fcntl(td, args->fd, F_SETLKW,
1650 		     (intptr_t)&bsd_flock));
1651 
1652 	case LINUX_F_GETOWN:
1653 		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1654 
1655 	case LINUX_F_SETOWN:
1656 		/*
1657 		 * XXX some Linux applications depend on F_SETOWN having no
1658 		 * significant effect for pipes (SIGIO is not delivered for
1659 		 * pipes under Linux-2.2.35 at least).
1660 		 */
1661 		error = fget(td, args->fd,
1662 		    &cap_fcntl_rights, &fp);
1663 		if (error)
1664 			return (error);
1665 		if (fp->f_type == DTYPE_PIPE) {
1666 			fdrop(fp, td);
1667 			return (EINVAL);
1668 		}
1669 		fdrop(fp, td);
1670 
1671 		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1672 
1673 	case LINUX_F_DUPFD_CLOEXEC:
1674 		return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1675 	/*
1676 	 * Our F_SEAL_* values match Linux one for maximum compatibility.  So we
1677 	 * only needed to account for different values for fcntl(2) commands.
1678 	 */
1679 	case LINUX_F_GET_SEALS:
1680 		error = kern_fcntl(td, args->fd, F_GET_SEALS, 0);
1681 		if (error != 0)
1682 			return (error);
1683 		td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0],
1684 		    seal_bitmap, 0);
1685 		return (0);
1686 
1687 	case LINUX_F_ADD_SEALS:
1688 		return (kern_fcntl(td, args->fd, F_ADD_SEALS,
1689 		    linux_to_bsd_bits(args->arg, seal_bitmap, 0)));
1690 
1691 	case LINUX_F_GETPIPE_SZ:
1692 		error = fget(td, args->fd,
1693 		    &cap_fcntl_rights, &fp);
1694 		if (error != 0)
1695 			return (error);
1696 		if (fp->f_type != DTYPE_PIPE) {
1697 			fdrop(fp, td);
1698 			return (EINVAL);
1699 		}
1700 		fpipe = fp->f_data;
1701 		td->td_retval[0] = fpipe->pipe_buffer.size;
1702 		fdrop(fp, td);
1703 		return (0);
1704 
1705 	default:
1706 		linux_msg(td, "unsupported fcntl cmd %d", args->cmd);
1707 		return (EINVAL);
1708 	}
1709 }
1710 
1711 int
1712 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1713 {
1714 
1715 	return (fcntl_common(td, args));
1716 }
1717 
1718 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1719 int
1720 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1721 {
1722 	struct l_flock64 linux_flock;
1723 	struct flock bsd_flock;
1724 	struct linux_fcntl_args fcntl_args;
1725 	int error;
1726 
1727 	switch (args->cmd) {
1728 	case LINUX_F_GETLK64:
1729 		error = copyin((void *)args->arg, &linux_flock,
1730 		    sizeof(linux_flock));
1731 		if (error)
1732 			return (error);
1733 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1734 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1735 		if (error)
1736 			return (error);
1737 		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1738 		return (copyout(&linux_flock, (void *)args->arg,
1739 			    sizeof(linux_flock)));
1740 
1741 	case LINUX_F_SETLK64:
1742 		error = copyin((void *)args->arg, &linux_flock,
1743 		    sizeof(linux_flock));
1744 		if (error)
1745 			return (error);
1746 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1747 		return (kern_fcntl(td, args->fd, F_SETLK,
1748 		    (intptr_t)&bsd_flock));
1749 
1750 	case LINUX_F_SETLKW64:
1751 		error = copyin((void *)args->arg, &linux_flock,
1752 		    sizeof(linux_flock));
1753 		if (error)
1754 			return (error);
1755 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1756 		return (kern_fcntl(td, args->fd, F_SETLKW,
1757 		    (intptr_t)&bsd_flock));
1758 	}
1759 
1760 	fcntl_args.fd = args->fd;
1761 	fcntl_args.cmd = args->cmd;
1762 	fcntl_args.arg = args->arg;
1763 	return (fcntl_common(td, &fcntl_args));
1764 }
1765 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1766 
1767 #ifdef LINUX_LEGACY_SYSCALLS
1768 int
1769 linux_chown(struct thread *td, struct linux_chown_args *args)
1770 {
1771 	char *path;
1772 	int error;
1773 
1774 	if (!LUSECONVPATH(td)) {
1775 		return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
1776 		    args->uid, args->gid, 0));
1777 	}
1778 	LCONVPATHEXIST(args->path, &path);
1779 	error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1780 	    args->gid, 0);
1781 	LFREEPATH(path);
1782 	return (error);
1783 }
1784 #endif
1785 
1786 int
1787 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1788 {
1789 	char *path;
1790 	int error, dfd, flag, unsupported;
1791 
1792 	unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
1793 	if (unsupported != 0) {
1794 		linux_msg(td, "fchownat unsupported flag 0x%x", unsupported);
1795 		return (EINVAL);
1796 	}
1797 
1798 	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1799 	    AT_SYMLINK_NOFOLLOW;
1800 	flag |= (args->flag & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
1801 	    AT_EMPTY_PATH;
1802 
1803 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD :  args->dfd;
1804 	if (!LUSECONVPATH(td)) {
1805 		return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE,
1806 		    args->uid, args->gid, flag));
1807 	}
1808 	LCONVPATHEXIST_AT(args->filename, &path, dfd);
1809 	error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1810 	    flag);
1811 	LFREEPATH(path);
1812 	return (error);
1813 }
1814 
1815 #ifdef LINUX_LEGACY_SYSCALLS
1816 int
1817 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1818 {
1819 	char *path;
1820 	int error;
1821 
1822 	if (!LUSECONVPATH(td)) {
1823 		return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid,
1824 		    args->gid, AT_SYMLINK_NOFOLLOW));
1825 	}
1826 	LCONVPATHEXIST(args->path, &path);
1827 	error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid, args->gid,
1828 	    AT_SYMLINK_NOFOLLOW);
1829 	LFREEPATH(path);
1830 	return (error);
1831 }
1832 #endif
1833 
1834 static int
1835 convert_fadvice(int advice)
1836 {
1837 	switch (advice) {
1838 	case LINUX_POSIX_FADV_NORMAL:
1839 		return (POSIX_FADV_NORMAL);
1840 	case LINUX_POSIX_FADV_RANDOM:
1841 		return (POSIX_FADV_RANDOM);
1842 	case LINUX_POSIX_FADV_SEQUENTIAL:
1843 		return (POSIX_FADV_SEQUENTIAL);
1844 	case LINUX_POSIX_FADV_WILLNEED:
1845 		return (POSIX_FADV_WILLNEED);
1846 	case LINUX_POSIX_FADV_DONTNEED:
1847 		return (POSIX_FADV_DONTNEED);
1848 	case LINUX_POSIX_FADV_NOREUSE:
1849 		return (POSIX_FADV_NOREUSE);
1850 	default:
1851 		return (-1);
1852 	}
1853 }
1854 
1855 int
1856 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1857 {
1858 	off_t offset;
1859 	int advice;
1860 
1861 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1862 	offset = PAIR32TO64(off_t, args->offset);
1863 #else
1864 	offset = args->offset;
1865 #endif
1866 
1867 	advice = convert_fadvice(args->advice);
1868 	if (advice == -1)
1869 		return (EINVAL);
1870 	return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
1871 }
1872 
1873 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1874 int
1875 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1876 {
1877 	off_t len, offset;
1878 	int advice;
1879 
1880 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1881 	len = PAIR32TO64(off_t, args->len);
1882 	offset = PAIR32TO64(off_t, args->offset);
1883 #else
1884 	len = args->len;
1885 	offset = args->offset;
1886 #endif
1887 
1888 	advice = convert_fadvice(args->advice);
1889 	if (advice == -1)
1890 		return (EINVAL);
1891 	return (kern_posix_fadvise(td, args->fd, offset, len, advice));
1892 }
1893 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1894 
1895 #ifdef LINUX_LEGACY_SYSCALLS
1896 int
1897 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1898 {
1899 	int fildes[2];
1900 	int error;
1901 
1902 	error = kern_pipe(td, fildes, 0, NULL, NULL);
1903 	if (error != 0)
1904 		return (error);
1905 
1906 	error = copyout(fildes, args->pipefds, sizeof(fildes));
1907 	if (error != 0) {
1908 		(void)kern_close(td, fildes[0]);
1909 		(void)kern_close(td, fildes[1]);
1910 	}
1911 
1912 	return (error);
1913 }
1914 #endif
1915 
1916 int
1917 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1918 {
1919 	int fildes[2];
1920 	int error, flags;
1921 
1922 	if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1923 		return (EINVAL);
1924 
1925 	flags = 0;
1926 	if ((args->flags & LINUX_O_NONBLOCK) != 0)
1927 		flags |= O_NONBLOCK;
1928 	if ((args->flags & LINUX_O_CLOEXEC) != 0)
1929 		flags |= O_CLOEXEC;
1930 	error = kern_pipe(td, fildes, flags, NULL, NULL);
1931 	if (error != 0)
1932 		return (error);
1933 
1934 	error = copyout(fildes, args->pipefds, sizeof(fildes));
1935 	if (error != 0) {
1936 		(void)kern_close(td, fildes[0]);
1937 		(void)kern_close(td, fildes[1]);
1938 	}
1939 
1940 	return (error);
1941 }
1942 
1943 int
1944 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1945 {
1946 	int cmd;
1947 	intptr_t newfd;
1948 
1949 	if (args->oldfd == args->newfd)
1950 		return (EINVAL);
1951 	if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1952 		return (EINVAL);
1953 	if (args->flags & LINUX_O_CLOEXEC)
1954 		cmd = F_DUP2FD_CLOEXEC;
1955 	else
1956 		cmd = F_DUP2FD;
1957 
1958 	newfd = args->newfd;
1959 	return (kern_fcntl(td, args->oldfd, cmd, newfd));
1960 }
1961 
1962 int
1963 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1964 {
1965 	off_t len, offset;
1966 
1967 	/*
1968 	 * We emulate only posix_fallocate system call for which
1969 	 * mode should be 0.
1970 	 */
1971 	if (args->mode != 0)
1972 		return (EOPNOTSUPP);
1973 
1974 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1975 	len = PAIR32TO64(off_t, args->len);
1976 	offset = PAIR32TO64(off_t, args->offset);
1977 #else
1978 	len = args->len;
1979 	offset = args->offset;
1980 #endif
1981 
1982 	return (kern_posix_fallocate(td, args->fd, offset, len));
1983 }
1984 
1985 int
1986 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
1987     *args)
1988 {
1989 	l_loff_t inoff, outoff, *inoffp, *outoffp;
1990 	int error, flags;
1991 
1992 	/*
1993 	 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
1994 	 * the native implementation.  Enforce it.
1995 	 */
1996 	if (args->flags != 0) {
1997 		linux_msg(td, "copy_file_range unsupported flags 0x%x",
1998 		    args->flags);
1999 		return (EINVAL);
2000 	}
2001 	flags = 0;
2002 	inoffp = outoffp = NULL;
2003 	if (args->off_in != NULL) {
2004 		error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
2005 		if (error != 0)
2006 			return (error);
2007 		inoffp = &inoff;
2008 	}
2009 	if (args->off_out != NULL) {
2010 		error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
2011 		if (error != 0)
2012 			return (error);
2013 		outoffp = &outoff;
2014 	}
2015 
2016 	error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
2017 	    outoffp, args->len, flags);
2018 	if (error == 0 && args->off_in != NULL)
2019 		error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
2020 	if (error == 0 && args->off_out != NULL)
2021 		error = copyout(outoffp, args->off_out, sizeof(l_loff_t));
2022 	return (error);
2023 }
2024 
2025 #define	LINUX_MEMFD_PREFIX	"memfd:"
2026 
2027 int
2028 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
2029 {
2030 	char memfd_name[LINUX_NAME_MAX + 1];
2031 	int error, flags, shmflags, oflags;
2032 
2033 	/*
2034 	 * This is our clever trick to avoid the heap allocation to copy in the
2035 	 * uname.  We don't really need to go this far out of our way, but it
2036 	 * does keep the rest of this function fairly clean as they don't have
2037 	 * to worry about cleanup on the way out.
2038 	 */
2039 	error = copyinstr(args->uname_ptr,
2040 	    memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1,
2041 	    LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL);
2042 	if (error != 0) {
2043 		if (error == ENAMETOOLONG)
2044 			error = EINVAL;
2045 		return (error);
2046 	}
2047 
2048 	memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1);
2049 	flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0);
2050 	if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB |
2051 	    MFD_HUGE_MASK)) != 0)
2052 		return (EINVAL);
2053 	/* Size specified but no HUGETLB. */
2054 	if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0)
2055 		return (EINVAL);
2056 	/* We don't actually support HUGETLB. */
2057 	if ((flags & MFD_HUGETLB) != 0)
2058 		return (ENOSYS);
2059 	oflags = O_RDWR;
2060 	shmflags = SHM_GROW_ON_WRITE;
2061 	if ((flags & MFD_CLOEXEC) != 0)
2062 		oflags |= O_CLOEXEC;
2063 	if ((flags & MFD_ALLOW_SEALING) != 0)
2064 		shmflags |= SHM_ALLOW_SEALING;
2065 	return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL,
2066 	    memfd_name));
2067 }
2068 
2069 int
2070 linux_splice(struct thread *td, struct linux_splice_args *args)
2071 {
2072 
2073 	linux_msg(td, "syscall splice not really implemented");
2074 
2075 	/*
2076 	 * splice(2) is documented to return EINVAL in various circumstances;
2077 	 * returning it instead of ENOSYS should hint the caller to use fallback
2078 	 * instead.
2079 	 */
2080 	return (EINVAL);
2081 }
2082