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