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