2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1994-1995 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/dirent.h>
33 #include <sys/fcntl.h>
35 #include <sys/filedesc.h>
38 #include <sys/selinfo.h>
43 #include <sys/syscallsubr.h>
45 #include <sys/unistd.h>
46 #include <sys/vnode.h>
49 #include <compat/freebsd32/freebsd32_misc.h>
50 #include <machine/../linux32/linux.h>
51 #include <machine/../linux32/linux32_proto.h>
53 #include <machine/../linux/linux.h>
54 #include <machine/../linux/linux_proto.h>
56 #include <compat/linux/linux_misc.h>
57 #include <compat/linux/linux_util.h>
58 #include <compat/linux/linux_file.h>
60 static int linux_common_open(struct thread *, int, const char *, int, int,
62 static int linux_do_accessat(struct thread *t, int, const char *, int, int);
63 static int linux_getdents_error(struct thread *, int, int);
65 static struct bsd_to_linux_bitmap seal_bitmap[] = {
66 BITMAP_1t1_LINUX(F_SEAL_SEAL),
67 BITMAP_1t1_LINUX(F_SEAL_SHRINK),
68 BITMAP_1t1_LINUX(F_SEAL_GROW),
69 BITMAP_1t1_LINUX(F_SEAL_WRITE),
72 #define MFD_HUGETLB_ENTRY(_size) \
74 .bsd_value = MFD_HUGE_##_size, \
75 .linux_value = LINUX_HUGETLB_FLAG_ENCODE_##_size \
77 static struct bsd_to_linux_bitmap mfd_bitmap[] = {
78 BITMAP_1t1_LINUX(MFD_CLOEXEC),
79 BITMAP_1t1_LINUX(MFD_ALLOW_SEALING),
80 BITMAP_1t1_LINUX(MFD_HUGETLB),
81 MFD_HUGETLB_ENTRY(64KB),
82 MFD_HUGETLB_ENTRY(512KB),
83 MFD_HUGETLB_ENTRY(1MB),
84 MFD_HUGETLB_ENTRY(2MB),
85 MFD_HUGETLB_ENTRY(8MB),
86 MFD_HUGETLB_ENTRY(16MB),
87 MFD_HUGETLB_ENTRY(32MB),
88 MFD_HUGETLB_ENTRY(256MB),
89 MFD_HUGETLB_ENTRY(512MB),
90 MFD_HUGETLB_ENTRY(1GB),
91 MFD_HUGETLB_ENTRY(2GB),
92 MFD_HUGETLB_ENTRY(16GB),
94 #undef MFD_HUGETLB_ENTRY
96 #ifdef LINUX_LEGACY_SYSCALLS
98 linux_creat(struct thread *td, struct linux_creat_args *args)
101 return (kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE,
102 O_WRONLY | O_CREAT | O_TRUNC, args->mode));
107 linux_common_openflags(int l_flags)
112 switch (l_flags & LINUX_O_ACCMODE) {
114 bsd_flags |= O_WRONLY;
120 bsd_flags |= O_RDONLY;
122 if (l_flags & LINUX_O_NDELAY)
123 bsd_flags |= O_NONBLOCK;
124 if (l_flags & LINUX_O_APPEND)
125 bsd_flags |= O_APPEND;
126 if (l_flags & LINUX_O_SYNC)
127 bsd_flags |= O_FSYNC;
128 if (l_flags & LINUX_O_CLOEXEC)
129 bsd_flags |= O_CLOEXEC;
130 if (l_flags & LINUX_O_NONBLOCK)
131 bsd_flags |= O_NONBLOCK;
132 if (l_flags & LINUX_O_ASYNC)
133 bsd_flags |= O_ASYNC;
134 if (l_flags & LINUX_O_CREAT)
135 bsd_flags |= O_CREAT;
136 if (l_flags & LINUX_O_TRUNC)
137 bsd_flags |= O_TRUNC;
138 if (l_flags & LINUX_O_EXCL)
140 if (l_flags & LINUX_O_NOCTTY)
141 bsd_flags |= O_NOCTTY;
142 if (l_flags & LINUX_O_DIRECT)
143 bsd_flags |= O_DIRECT;
144 if (l_flags & LINUX_O_NOFOLLOW)
145 bsd_flags |= O_NOFOLLOW;
146 if (l_flags & LINUX_O_DIRECTORY)
147 bsd_flags |= O_DIRECTORY;
148 if (l_flags & LINUX_O_PATH)
150 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
155 linux_common_open(struct thread *td, int dirfd, const char *path, int l_flags,
156 int mode, enum uio_seg seg)
158 struct proc *p = td->td_proc;
161 int bsd_flags, error;
163 bsd_flags = linux_common_openflags(l_flags);
164 error = kern_openat(td, dirfd, path, seg, bsd_flags, mode);
170 if (p->p_flag & P_CONTROLT)
172 if (bsd_flags & O_NOCTTY)
176 * XXX In between kern_openat() and fget(), another process
177 * having the same filedesc could use that fd without
180 fd = td->td_retval[0];
181 if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
182 if (fp->f_type != DTYPE_VNODE) {
186 sx_slock(&proctree_lock);
188 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
190 sx_sunlock(&proctree_lock);
191 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
192 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
196 sx_sunlock(&proctree_lock);
206 linux_openat(struct thread *td, struct linux_openat_args *args)
210 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
211 return (linux_common_open(td, dfd, args->filename, args->flags,
212 args->mode, UIO_USERSPACE));
215 #ifdef LINUX_LEGACY_SYSCALLS
217 linux_open(struct thread *td, struct linux_open_args *args)
220 return (linux_common_open(td, AT_FDCWD, args->path, args->flags,
221 args->mode, UIO_USERSPACE));
226 linux_name_to_handle_at(struct thread *td,
227 struct linux_name_to_handle_at_args *args)
229 static const l_int valid_flags = (LINUX_AT_SYMLINK_FOLLOW |
230 LINUX_AT_EMPTY_PATH);
231 static const l_uint fh_size = sizeof(fhandle_t);
236 int error, fd, bsd_flags;
238 if (args->flags & ~valid_flags)
242 if (fd == LINUX_AT_FDCWD)
246 if (!(args->flags & LINUX_AT_SYMLINK_FOLLOW))
247 bsd_flags |= AT_SYMLINK_NOFOLLOW;
248 if ((args->flags & LINUX_AT_EMPTY_PATH) != 0)
249 bsd_flags |= AT_EMPTY_PATH;
251 error = kern_getfhat(td, bsd_flags, fd, args->name,
252 UIO_USERSPACE, &fh, UIO_SYSSPACE);
256 /* Emit mount_id -- required before EOVERFLOW case. */
257 mount_id = (fh.fh_fsid.val[0] ^ fh.fh_fsid.val[1]);
258 error = copyout(&mount_id, args->mnt_id, sizeof(mount_id));
262 /* Check if there is room for handle. */
263 error = copyin(&args->handle->handle_bytes, &fh_bytes,
268 if (fh_bytes < fh_size) {
269 error = copyout(&fh_size, &args->handle->handle_bytes,
279 * We don't use handle_type for anything yet, but initialize a known
282 error = copyout(&mount_id, &args->handle->handle_type,
287 error = copyout(&fh, &args->handle->f_handle,
293 linux_open_by_handle_at(struct thread *td,
294 struct linux_open_by_handle_at_args *args)
297 int bsd_flags, error;
299 error = copyin(&args->handle->handle_bytes, &fh_bytes,
304 if (fh_bytes < sizeof(fhandle_t))
307 bsd_flags = linux_common_openflags(args->flags);
308 return (kern_fhopen(td, (void *)&args->handle->f_handle, bsd_flags));
312 linux_lseek(struct thread *td, struct linux_lseek_args *args)
315 return (kern_lseek(td, args->fdes, args->off, args->whence));
318 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
320 linux_llseek(struct thread *td, struct linux_llseek_args *args)
325 off = (args->olow) | (((off_t) args->ohigh) << 32);
327 error = kern_lseek(td, args->fd, off, args->whence);
331 error = copyout(td->td_retval, args->res, sizeof(off_t));
335 td->td_retval[0] = 0;
338 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
341 * Note that linux_getdents(2) and linux_getdents64(2) have the same
342 * arguments. They only differ in the definition of struct dirent they
344 * Note that linux_readdir(2) is a special case of linux_getdents(2)
345 * where count is always equals 1, meaning that the buffer is one
346 * dirent-structure in size and that the code can't handle more anyway.
347 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
348 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
353 linux_getdents_error(struct thread *td, int fd, int err)
359 /* Linux return ENOTDIR in case when fd is not a directory. */
360 error = getvnode(td, fd, &cap_read_rights, &fp);
364 if (vp->v_type != VDIR) {
376 char d_name[LINUX_NAME_MAX + 1];
384 char d_name[LINUX_NAME_MAX + 1];
388 * Linux uses the last byte in the dirent buffer to store d_type,
389 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
391 #define LINUX_RECLEN(namlen) \
392 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
394 #define LINUX_RECLEN64(namlen) \
395 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
398 #ifdef LINUX_LEGACY_SYSCALLS
400 linux_getdents(struct thread *td, struct linux_getdents_args *args)
403 caddr_t inp, buf; /* BSD-format */
404 int len, reclen; /* BSD-format */
405 caddr_t outp; /* Linux-format */
406 int resid, linuxreclen; /* Linux-format */
407 caddr_t lbuf; /* Linux-format */
409 struct l_dirent *linux_dirent;
413 buflen = min(args->count, MAXBSIZE);
414 buf = malloc(buflen, M_LINUX, M_WAITOK);
416 error = kern_getdirentries(td, args->fd, buf, buflen,
417 &base, NULL, UIO_SYSSPACE);
419 error = linux_getdents_error(td, args->fd, error);
423 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX, M_WAITOK | M_ZERO);
425 len = td->td_retval[0];
427 outp = (caddr_t)args->dent;
432 bdp = (struct dirent *) inp;
433 reclen = bdp->d_reclen;
434 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
436 * No more space in the user supplied dirent buffer.
439 if (resid < linuxreclen) {
444 linux_dirent = (struct l_dirent*)lbuf;
445 linux_dirent->d_ino = bdp->d_fileno;
446 linux_dirent->d_off = bdp->d_off;
447 linux_dirent->d_reclen = linuxreclen;
449 * Copy d_type to last byte of l_dirent buffer
451 lbuf[linuxreclen - 1] = bdp->d_type;
452 strlcpy(linux_dirent->d_name, bdp->d_name,
453 linuxreclen - offsetof(struct l_dirent, d_name)-1);
454 error = copyout(linux_dirent, outp, linuxreclen);
462 retval += linuxreclen;
464 resid -= linuxreclen;
466 td->td_retval[0] = retval;
477 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
480 caddr_t inp, buf; /* BSD-format */
481 int len, reclen; /* BSD-format */
482 caddr_t outp; /* Linux-format */
483 int resid, linuxreclen; /* Linux-format */
485 struct l_dirent64 *linux_dirent64;
489 buflen = min(args->count, MAXBSIZE);
490 buf = malloc(buflen, M_LINUX, M_WAITOK);
492 error = kern_getdirentries(td, args->fd, buf, buflen,
493 &base, NULL, UIO_SYSSPACE);
495 error = linux_getdents_error(td, args->fd, error);
499 linux_dirent64 = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_LINUX,
502 len = td->td_retval[0];
504 outp = (caddr_t)args->dirent;
509 bdp = (struct dirent *) inp;
510 reclen = bdp->d_reclen;
511 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
513 * No more space in the user supplied dirent buffer.
516 if (resid < linuxreclen) {
521 linux_dirent64->d_ino = bdp->d_fileno;
522 linux_dirent64->d_off = bdp->d_off;
523 linux_dirent64->d_reclen = linuxreclen;
524 linux_dirent64->d_type = bdp->d_type;
525 strlcpy(linux_dirent64->d_name, bdp->d_name,
526 linuxreclen - offsetof(struct l_dirent64, d_name));
527 error = copyout(linux_dirent64, outp, linuxreclen);
535 retval += linuxreclen;
537 resid -= linuxreclen;
539 td->td_retval[0] = retval;
542 free(linux_dirent64, M_LINUX);
548 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
550 linux_readdir(struct thread *td, struct linux_readdir_args *args)
553 caddr_t buf; /* BSD-format */
554 int linuxreclen; /* Linux-format */
556 struct l_dirent *linux_dirent; /* Linux-format */
559 buflen = sizeof(*bdp);
560 buf = malloc(buflen, M_LINUX, M_WAITOK);
562 error = kern_getdirentries(td, args->fd, buf, buflen,
563 &base, NULL, UIO_SYSSPACE);
565 error = linux_getdents_error(td, args->fd, error);
568 if (td->td_retval[0] == 0)
571 linux_dirent = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX,
574 bdp = (struct dirent *) buf;
575 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
577 linux_dirent->d_ino = bdp->d_fileno;
578 linux_dirent->d_off = bdp->d_off;
579 linux_dirent->d_reclen = bdp->d_namlen;
580 strlcpy(linux_dirent->d_name, bdp->d_name,
581 linuxreclen - offsetof(struct l_dirent, d_name));
582 error = copyout(linux_dirent, args->dent, linuxreclen);
584 td->td_retval[0] = linuxreclen;
586 free(linux_dirent, M_LINUX);
591 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
594 * These exist mainly for hooks for doing /compat/linux translation.
597 #ifdef LINUX_LEGACY_SYSCALLS
599 linux_access(struct thread *td, struct linux_access_args *args)
602 /* Linux convention. */
603 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
606 return (kern_accessat(td, AT_FDCWD, args->path, UIO_USERSPACE, 0,
612 linux_do_accessat(struct thread *td, int ldfd, const char *filename,
613 int amode, int flags)
617 /* Linux convention. */
618 if (amode & ~(F_OK | X_OK | W_OK | R_OK))
621 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
622 return (kern_accessat(td, dfd, filename, UIO_USERSPACE, flags, amode));
626 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
629 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
634 linux_faccessat2(struct thread *td, struct linux_faccessat2_args *args)
636 int flags, unsupported;
638 /* XXX. AT_SYMLINK_NOFOLLOW is not supported by kern_accessat */
639 unsupported = args->flags & ~(LINUX_AT_EACCESS | LINUX_AT_EMPTY_PATH);
640 if (unsupported != 0) {
641 linux_msg(td, "faccessat2 unsupported flag 0x%x", unsupported);
645 flags = (args->flags & LINUX_AT_EACCESS) == 0 ? 0 :
647 flags |= (args->flags & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
649 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
654 #ifdef LINUX_LEGACY_SYSCALLS
656 linux_unlink(struct thread *td, struct linux_unlink_args *args)
661 error = kern_funlinkat(td, AT_FDCWD, args->path, FD_NONE,
662 UIO_USERSPACE, 0, 0);
663 if (error == EPERM) {
664 /* Introduce POSIX noncompliant behaviour of Linux */
665 if (kern_statat(td, 0, AT_FDCWD, args->path,
666 UIO_USERSPACE, &st) == 0) {
667 if (S_ISDIR(st.st_mode))
677 linux_unlinkat_impl(struct thread *td, enum uio_seg pathseg, const char *path,
678 int dfd, struct linux_unlinkat_args *args)
683 if (args->flag & LINUX_AT_REMOVEDIR)
684 error = kern_frmdirat(td, dfd, path, FD_NONE, pathseg, 0);
686 error = kern_funlinkat(td, dfd, path, FD_NONE, pathseg, 0, 0);
687 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
688 /* Introduce POSIX noncompliant behaviour of Linux */
689 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
690 pathseg, &st) == 0 && S_ISDIR(st.st_mode))
697 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
701 if (args->flag & ~LINUX_AT_REMOVEDIR)
703 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
704 return (linux_unlinkat_impl(td, UIO_USERSPACE, args->pathname,
709 linux_chdir(struct thread *td, struct linux_chdir_args *args)
712 return (kern_chdir(td, args->path, UIO_USERSPACE));
715 #ifdef LINUX_LEGACY_SYSCALLS
717 linux_chmod(struct thread *td, struct linux_chmod_args *args)
720 return (kern_fchmodat(td, AT_FDCWD, args->path, UIO_USERSPACE,
726 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
730 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
731 return (kern_fchmodat(td, dfd, args->filename, UIO_USERSPACE,
735 #ifdef LINUX_LEGACY_SYSCALLS
737 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
740 return (kern_mkdirat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->mode));
745 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
749 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
750 return (kern_mkdirat(td, dfd, args->pathname, UIO_USERSPACE, args->mode));
753 #ifdef LINUX_LEGACY_SYSCALLS
755 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
758 return (kern_frmdirat(td, AT_FDCWD, args->path, FD_NONE,
763 linux_rename(struct thread *td, struct linux_rename_args *args)
766 return (kern_renameat(td, AT_FDCWD, args->from, AT_FDCWD,
767 args->to, UIO_USERSPACE));
772 linux_renameat(struct thread *td, struct linux_renameat_args *args)
774 struct linux_renameat2_args renameat2_args = {
775 .olddfd = args->olddfd,
776 .oldname = args->oldname,
777 .newdfd = args->newdfd,
778 .newname = args->newname,
782 return (linux_renameat2(td, &renameat2_args));
786 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
790 if (args->flags != 0) {
791 if (args->flags & ~(LINUX_RENAME_EXCHANGE |
792 LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
794 if (args->flags & LINUX_RENAME_EXCHANGE &&
795 args->flags & (LINUX_RENAME_NOREPLACE |
796 LINUX_RENAME_WHITEOUT))
800 * This spams the console on Ubuntu Focal.
802 * What's needed here is a general mechanism to let users know
803 * about missing features without hogging the system.
805 linux_msg(td, "renameat2 unsupported flags 0x%x",
811 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
812 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
813 return (kern_renameat(td, olddfd, args->oldname, newdfd,
814 args->newname, UIO_USERSPACE));
817 #ifdef LINUX_LEGACY_SYSCALLS
819 linux_symlink(struct thread *td, struct linux_symlink_args *args)
822 return (kern_symlinkat(td, args->path, AT_FDCWD, args->to,
828 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
832 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
833 return (kern_symlinkat(td, args->oldname, dfd, args->newname,
837 #ifdef LINUX_LEGACY_SYSCALLS
839 linux_readlink(struct thread *td, struct linux_readlink_args *args)
842 if (args->count <= 0)
845 return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE,
846 args->buf, UIO_USERSPACE, args->count));
851 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
855 if (args->bufsiz <= 0)
858 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
859 return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE,
860 args->buf, UIO_USERSPACE, args->bufsiz));
864 linux_truncate(struct thread *td, struct linux_truncate_args *args)
867 return (kern_truncate(td, args->path, UIO_USERSPACE, args->length));
870 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
872 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
876 #if defined(__amd64__) && defined(COMPAT_LINUX32)
877 length = PAIR32TO64(off_t, args->length);
879 length = args->length;
882 return (kern_truncate(td, args->path, UIO_USERSPACE, length));
884 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
887 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
890 return (kern_ftruncate(td, args->fd, args->length));
893 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
895 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
899 #if defined(__amd64__) && defined(COMPAT_LINUX32)
900 length = PAIR32TO64(off_t, args->length);
902 length = args->length;
905 return (kern_ftruncate(td, args->fd, length));
909 #ifdef LINUX_LEGACY_SYSCALLS
911 linux_link(struct thread *td, struct linux_link_args *args)
914 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to,
915 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
920 linux_linkat(struct thread *td, struct linux_linkat_args *args)
922 int olddfd, newdfd, flag;
924 if (args->flag & ~(LINUX_AT_SYMLINK_FOLLOW | LINUX_AT_EMPTY_PATH))
927 flag = (args->flag & LINUX_AT_SYMLINK_FOLLOW) != 0 ? AT_SYMLINK_FOLLOW :
929 flag |= (args->flag & LINUX_AT_EMPTY_PATH) != 0 ? AT_EMPTY_PATH : 0;
931 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
932 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
933 return (kern_linkat(td, olddfd, newdfd, args->oldname,
934 args->newname, UIO_USERSPACE, flag));
938 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
941 return (kern_fsync(td, uap->fd, false));
945 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
947 off_t nbytes, offset;
949 #if defined(__amd64__) && defined(COMPAT_LINUX32)
950 nbytes = PAIR32TO64(off_t, uap->nbytes);
951 offset = PAIR32TO64(off_t, uap->offset);
953 nbytes = uap->nbytes;
954 offset = uap->offset;
957 if (offset < 0 || nbytes < 0 ||
958 (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
959 LINUX_SYNC_FILE_RANGE_WRITE |
960 LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
964 return (kern_fsync(td, uap->fd, false));
968 linux_pread(struct thread *td, struct linux_pread_args *uap)
974 #if defined(__amd64__) && defined(COMPAT_LINUX32)
975 offset = PAIR32TO64(off_t, uap->offset);
977 offset = uap->offset;
980 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
982 /* This seems to violate POSIX but Linux does it. */
983 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
986 if (vp->v_type == VDIR)
994 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
998 #if defined(__amd64__) && defined(COMPAT_LINUX32)
999 offset = PAIR32TO64(off_t, uap->offset);
1001 offset = uap->offset;
1004 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset));
1007 #define HALF_LONG_BITS ((sizeof(l_long) * NBBY / 2))
1010 pos_from_hilo(unsigned long high, unsigned long low)
1013 return (((off_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1017 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1024 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1025 * pos_l and pos_h, respectively, contain the
1026 * low order and high order 32 bits of offset.
1028 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1031 #ifdef COMPAT_LINUX32
1032 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1034 error = copyinuio(uap->vec, uap->vlen, &auio);
1038 error = kern_preadv(td, uap->fd, auio, offset);
1044 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1051 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1052 * pos_l and pos_h, respectively, contain the
1053 * low order and high order 32 bits of offset.
1055 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1058 #ifdef COMPAT_LINUX32
1059 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1061 error = copyinuio(uap->vec, uap->vlen, &auio);
1065 error = kern_pwritev(td, uap->fd, auio, offset);
1071 linux_mount(struct thread *td, struct linux_mount_args *args)
1073 struct mntarg *ma = NULL;
1074 char *fstypename, *mntonname, *mntfromname, *data;
1077 fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1078 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1079 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1081 error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1,
1085 if (args->specialfile != NULL) {
1086 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1090 mntfromname[0] = '\0';
1092 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1096 if (strcmp(fstypename, "ext2") == 0) {
1097 strcpy(fstypename, "ext2fs");
1098 } else if (strcmp(fstypename, "proc") == 0) {
1099 strcpy(fstypename, "linprocfs");
1100 } else if (strcmp(fstypename, "vfat") == 0) {
1101 strcpy(fstypename, "msdosfs");
1102 } else if (strcmp(fstypename, "fuse") == 0 ||
1103 strncmp(fstypename, "fuse.", 5) == 0) {
1104 char *fuse_options, *fuse_option, *fuse_name;
1106 strcpy(mntfromname, "/dev/fuse");
1107 strcpy(fstypename, "fusefs");
1108 data = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1109 error = copyinstr(args->data, data, MNAMELEN - 1, NULL);
1113 fuse_options = data;
1114 while ((fuse_option = strsep(&fuse_options, ",")) != NULL) {
1115 fuse_name = strsep(&fuse_option, "=");
1116 if (fuse_name == NULL || fuse_option == NULL)
1118 ma = mount_arg(ma, fuse_name, fuse_option, -1);
1122 * The FUSE server uses Linux errno values instead of FreeBSD
1123 * ones; add a flag to tell fuse(4) to do errno translation.
1125 ma = mount_arg(ma, "linux_errnos", "1", -1);
1131 * Linux SYNC flag is not included; the closest equivalent
1132 * FreeBSD has is !ASYNC, which is our default.
1134 if (args->rwflag & LINUX_MS_RDONLY)
1135 fsflags |= MNT_RDONLY;
1136 if (args->rwflag & LINUX_MS_NOSUID)
1137 fsflags |= MNT_NOSUID;
1138 if (args->rwflag & LINUX_MS_NOEXEC)
1139 fsflags |= MNT_NOEXEC;
1140 if (args->rwflag & LINUX_MS_REMOUNT)
1141 fsflags |= MNT_UPDATE;
1143 ma = mount_arg(ma, "fstype", fstypename, -1);
1144 ma = mount_arg(ma, "fspath", mntonname, -1);
1145 ma = mount_arg(ma, "from", mntfromname, -1);
1146 error = kernel_mount(ma, fsflags);
1148 free(fstypename, M_TEMP);
1149 free(mntonname, M_TEMP);
1150 free(mntfromname, M_TEMP);
1155 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1157 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1160 return (kern_unmount(td, args->path, 0));
1162 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1164 #ifdef LINUX_LEGACY_SYSCALLS
1166 linux_umount(struct thread *td, struct linux_umount_args *args)
1171 if ((args->flags & LINUX_MNT_FORCE) != 0) {
1172 args->flags &= ~LINUX_MNT_FORCE;
1175 if (args->flags != 0) {
1176 linux_msg(td, "unsupported umount2 flags %#x", args->flags);
1180 return (kern_unmount(td, args->path, flags));
1185 * fcntl family of syscalls
1195 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1201 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1203 switch (linux_flock->l_type) {
1205 bsd_flock->l_type = F_RDLCK;
1208 bsd_flock->l_type = F_WRLCK;
1211 bsd_flock->l_type = F_UNLCK;
1214 bsd_flock->l_type = -1;
1217 bsd_flock->l_whence = linux_flock->l_whence;
1218 bsd_flock->l_start = (off_t)linux_flock->l_start;
1219 bsd_flock->l_len = (off_t)linux_flock->l_len;
1220 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1221 bsd_flock->l_sysid = 0;
1225 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1227 switch (bsd_flock->l_type) {
1229 linux_flock->l_type = LINUX_F_RDLCK;
1232 linux_flock->l_type = LINUX_F_WRLCK;
1235 linux_flock->l_type = LINUX_F_UNLCK;
1238 linux_flock->l_whence = bsd_flock->l_whence;
1239 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1240 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1241 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1244 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1252 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1258 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1260 switch (linux_flock->l_type) {
1262 bsd_flock->l_type = F_RDLCK;
1265 bsd_flock->l_type = F_WRLCK;
1268 bsd_flock->l_type = F_UNLCK;
1271 bsd_flock->l_type = -1;
1274 bsd_flock->l_whence = linux_flock->l_whence;
1275 bsd_flock->l_start = (off_t)linux_flock->l_start;
1276 bsd_flock->l_len = (off_t)linux_flock->l_len;
1277 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1278 bsd_flock->l_sysid = 0;
1282 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1284 switch (bsd_flock->l_type) {
1286 linux_flock->l_type = LINUX_F_RDLCK;
1289 linux_flock->l_type = LINUX_F_WRLCK;
1292 linux_flock->l_type = LINUX_F_UNLCK;
1295 linux_flock->l_whence = bsd_flock->l_whence;
1296 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1297 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1298 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1300 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1303 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1305 struct l_flock linux_flock;
1306 struct flock bsd_flock;
1312 switch (args->cmd) {
1314 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1317 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1320 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1323 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1324 result = td->td_retval[0];
1325 td->td_retval[0] = 0;
1326 if (result & O_RDONLY)
1327 td->td_retval[0] |= LINUX_O_RDONLY;
1328 if (result & O_WRONLY)
1329 td->td_retval[0] |= LINUX_O_WRONLY;
1330 if (result & O_RDWR)
1331 td->td_retval[0] |= LINUX_O_RDWR;
1332 if (result & O_NDELAY)
1333 td->td_retval[0] |= LINUX_O_NONBLOCK;
1334 if (result & O_APPEND)
1335 td->td_retval[0] |= LINUX_O_APPEND;
1336 if (result & O_FSYNC)
1337 td->td_retval[0] |= LINUX_O_SYNC;
1338 if (result & O_ASYNC)
1339 td->td_retval[0] |= LINUX_O_ASYNC;
1340 #ifdef LINUX_O_NOFOLLOW
1341 if (result & O_NOFOLLOW)
1342 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1344 #ifdef LINUX_O_DIRECT
1345 if (result & O_DIRECT)
1346 td->td_retval[0] |= LINUX_O_DIRECT;
1352 if (args->arg & LINUX_O_NDELAY)
1354 if (args->arg & LINUX_O_APPEND)
1356 if (args->arg & LINUX_O_SYNC)
1358 if (args->arg & LINUX_O_ASYNC)
1360 #ifdef LINUX_O_NOFOLLOW
1361 if (args->arg & LINUX_O_NOFOLLOW)
1364 #ifdef LINUX_O_DIRECT
1365 if (args->arg & LINUX_O_DIRECT)
1368 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1371 error = copyin((void *)args->arg, &linux_flock,
1372 sizeof(linux_flock));
1375 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1376 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1379 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1380 return (copyout(&linux_flock, (void *)args->arg,
1381 sizeof(linux_flock)));
1384 error = copyin((void *)args->arg, &linux_flock,
1385 sizeof(linux_flock));
1388 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1389 return (kern_fcntl(td, args->fd, F_SETLK,
1390 (intptr_t)&bsd_flock));
1392 case LINUX_F_SETLKW:
1393 error = copyin((void *)args->arg, &linux_flock,
1394 sizeof(linux_flock));
1397 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1398 return (kern_fcntl(td, args->fd, F_SETLKW,
1399 (intptr_t)&bsd_flock));
1401 case LINUX_F_GETOWN:
1402 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1404 case LINUX_F_SETOWN:
1406 * XXX some Linux applications depend on F_SETOWN having no
1407 * significant effect for pipes (SIGIO is not delivered for
1408 * pipes under Linux-2.2.35 at least).
1410 error = fget(td, args->fd,
1411 &cap_fcntl_rights, &fp);
1414 if (fp->f_type == DTYPE_PIPE) {
1420 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1422 case LINUX_F_DUPFD_CLOEXEC:
1423 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1425 * Our F_SEAL_* values match Linux one for maximum compatibility. So we
1426 * only needed to account for different values for fcntl(2) commands.
1428 case LINUX_F_GET_SEALS:
1429 error = kern_fcntl(td, args->fd, F_GET_SEALS, 0);
1432 td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0],
1436 case LINUX_F_ADD_SEALS:
1437 return (kern_fcntl(td, args->fd, F_ADD_SEALS,
1438 linux_to_bsd_bits(args->arg, seal_bitmap, 0)));
1440 case LINUX_F_GETPIPE_SZ:
1441 error = fget(td, args->fd,
1442 &cap_fcntl_rights, &fp);
1445 if (fp->f_type != DTYPE_PIPE) {
1450 td->td_retval[0] = fpipe->pipe_buffer.size;
1455 linux_msg(td, "unsupported fcntl cmd %d", args->cmd);
1461 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1464 return (fcntl_common(td, args));
1467 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1469 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1471 struct l_flock64 linux_flock;
1472 struct flock bsd_flock;
1473 struct linux_fcntl_args fcntl_args;
1476 switch (args->cmd) {
1477 case LINUX_F_GETLK64:
1478 error = copyin((void *)args->arg, &linux_flock,
1479 sizeof(linux_flock));
1482 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1483 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1486 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1487 return (copyout(&linux_flock, (void *)args->arg,
1488 sizeof(linux_flock)));
1490 case LINUX_F_SETLK64:
1491 error = copyin((void *)args->arg, &linux_flock,
1492 sizeof(linux_flock));
1495 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1496 return (kern_fcntl(td, args->fd, F_SETLK,
1497 (intptr_t)&bsd_flock));
1499 case LINUX_F_SETLKW64:
1500 error = copyin((void *)args->arg, &linux_flock,
1501 sizeof(linux_flock));
1504 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1505 return (kern_fcntl(td, args->fd, F_SETLKW,
1506 (intptr_t)&bsd_flock));
1509 fcntl_args.fd = args->fd;
1510 fcntl_args.cmd = args->cmd;
1511 fcntl_args.arg = args->arg;
1512 return (fcntl_common(td, &fcntl_args));
1514 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1516 #ifdef LINUX_LEGACY_SYSCALLS
1518 linux_chown(struct thread *td, struct linux_chown_args *args)
1521 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
1522 args->uid, args->gid, 0));
1527 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1529 int dfd, flag, unsupported;
1531 unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
1532 if (unsupported != 0) {
1533 linux_msg(td, "fchownat unsupported flag 0x%x", unsupported);
1537 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1538 AT_SYMLINK_NOFOLLOW;
1539 flag |= (args->flag & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
1542 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1543 return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE,
1544 args->uid, args->gid, flag));
1547 #ifdef LINUX_LEGACY_SYSCALLS
1549 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1552 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid,
1553 args->gid, AT_SYMLINK_NOFOLLOW));
1558 convert_fadvice(int advice)
1561 case LINUX_POSIX_FADV_NORMAL:
1562 return (POSIX_FADV_NORMAL);
1563 case LINUX_POSIX_FADV_RANDOM:
1564 return (POSIX_FADV_RANDOM);
1565 case LINUX_POSIX_FADV_SEQUENTIAL:
1566 return (POSIX_FADV_SEQUENTIAL);
1567 case LINUX_POSIX_FADV_WILLNEED:
1568 return (POSIX_FADV_WILLNEED);
1569 case LINUX_POSIX_FADV_DONTNEED:
1570 return (POSIX_FADV_DONTNEED);
1571 case LINUX_POSIX_FADV_NOREUSE:
1572 return (POSIX_FADV_NOREUSE);
1579 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1584 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1585 offset = PAIR32TO64(off_t, args->offset);
1587 offset = args->offset;
1590 advice = convert_fadvice(args->advice);
1593 return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
1596 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1598 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1603 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1604 len = PAIR32TO64(off_t, args->len);
1605 offset = PAIR32TO64(off_t, args->offset);
1608 offset = args->offset;
1611 advice = convert_fadvice(args->advice);
1614 return (kern_posix_fadvise(td, args->fd, offset, len, advice));
1616 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1618 #ifdef LINUX_LEGACY_SYSCALLS
1620 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1625 error = kern_pipe(td, fildes, 0, NULL, NULL);
1629 error = copyout(fildes, args->pipefds, sizeof(fildes));
1631 (void)kern_close(td, fildes[0]);
1632 (void)kern_close(td, fildes[1]);
1640 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1645 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1649 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1650 flags |= O_NONBLOCK;
1651 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1653 error = kern_pipe(td, fildes, flags, NULL, NULL);
1657 error = copyout(fildes, args->pipefds, sizeof(fildes));
1659 (void)kern_close(td, fildes[0]);
1660 (void)kern_close(td, fildes[1]);
1667 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1672 if (args->oldfd == args->newfd)
1674 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1676 if (args->flags & LINUX_O_CLOEXEC)
1677 cmd = F_DUP2FD_CLOEXEC;
1681 newfd = args->newfd;
1682 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1686 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1691 * We emulate only posix_fallocate system call for which
1694 if (args->mode != 0)
1695 return (EOPNOTSUPP);
1697 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1698 len = PAIR32TO64(off_t, args->len);
1699 offset = PAIR32TO64(off_t, args->offset);
1702 offset = args->offset;
1705 return (kern_posix_fallocate(td, args->fd, offset, len));
1709 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
1712 l_loff_t inoff, outoff, *inoffp, *outoffp;
1716 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
1717 * the native implementation. Enforce it.
1719 if (args->flags != 0) {
1720 linux_msg(td, "copy_file_range unsupported flags 0x%x",
1725 inoffp = outoffp = NULL;
1726 if (args->off_in != NULL) {
1727 error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
1732 if (args->off_out != NULL) {
1733 error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
1739 error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
1740 outoffp, args->len, flags);
1741 if (error == 0 && args->off_in != NULL)
1742 error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
1743 if (error == 0 && args->off_out != NULL)
1744 error = copyout(outoffp, args->off_out, sizeof(l_loff_t));
1748 #define LINUX_MEMFD_PREFIX "memfd:"
1751 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
1753 char memfd_name[LINUX_NAME_MAX + 1];
1754 int error, flags, shmflags, oflags;
1757 * This is our clever trick to avoid the heap allocation to copy in the
1758 * uname. We don't really need to go this far out of our way, but it
1759 * does keep the rest of this function fairly clean as they don't have
1760 * to worry about cleanup on the way out.
1762 error = copyinstr(args->uname_ptr,
1763 memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1,
1764 LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL);
1766 if (error == ENAMETOOLONG)
1771 memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1);
1772 flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0);
1773 if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB |
1774 MFD_HUGE_MASK)) != 0)
1776 /* Size specified but no HUGETLB. */
1777 if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0)
1779 /* We don't actually support HUGETLB. */
1780 if ((flags & MFD_HUGETLB) != 0)
1783 shmflags = SHM_GROW_ON_WRITE;
1784 if ((flags & MFD_CLOEXEC) != 0)
1785 oflags |= O_CLOEXEC;
1786 if ((flags & MFD_ALLOW_SEALING) != 0)
1787 shmflags |= SHM_ALLOW_SEALING;
1788 return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL,
1793 linux_splice(struct thread *td, struct linux_splice_args *args)
1796 linux_msg(td, "syscall splice not really implemented");
1799 * splice(2) is documented to return EINVAL in various circumstances;
1800 * returning it instead of ENOSYS should hint the caller to use fallback
1807 linux_close_range(struct thread *td, struct linux_close_range_args *args)
1812 * Implementing close_range(CLOSE_RANGE_UNSHARE) allows Linux to
1813 * unshare filedesc table of the calling thread from others threads
1814 * in a thread group (i.e., process in the FreeBSD) or others processes,
1815 * which shares the same table, before closing the files. FreeBSD does
1816 * not have compatible unsharing mechanism due to the fact that sharing
1817 * process resources, including filedesc table, is at thread level in the
1818 * Linux, while in the FreeBSD it is at the process level.
1819 * Return EINVAL for now if the CLOSE_RANGE_UNSHARE flag is specified
1820 * until this new Linux API stabilizes.
1823 if ((args->flags & ~(LINUX_CLOSE_RANGE_CLOEXEC)) != 0)
1825 if (args->first > args->last)
1827 if ((args->flags & LINUX_CLOSE_RANGE_CLOEXEC) != 0)
1828 flags |= CLOSE_RANGE_CLOEXEC;
1829 return (kern_close_range(td, flags, args->first, args->last));