2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/capsicum.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
41 #include <sys/filedesc.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mutex.h>
46 #include <sys/namei.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/sysproto.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
57 #include <compat/freebsd32/freebsd32_misc.h>
58 #include <machine/../linux32/linux.h>
59 #include <machine/../linux32/linux32_proto.h>
61 #include <machine/../linux/linux.h>
62 #include <machine/../linux/linux_proto.h>
64 #include <compat/linux/linux_misc.h>
65 #include <compat/linux/linux_util.h>
66 #include <compat/linux/linux_file.h>
68 static int linux_common_open(struct thread *, int, char *, int, int);
69 static int linux_getdents_error(struct thread *, int, int);
71 #ifdef LINUX_LEGACY_SYSCALLS
73 linux_creat(struct thread *td, struct linux_creat_args *args)
78 LCONVPATHEXIST(td, args->path, &path);
80 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
81 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
88 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode)
90 struct proc *p = td->td_proc;
96 switch (l_flags & LINUX_O_ACCMODE) {
98 bsd_flags |= O_WRONLY;
104 bsd_flags |= O_RDONLY;
106 if (l_flags & LINUX_O_NDELAY)
107 bsd_flags |= O_NONBLOCK;
108 if (l_flags & LINUX_O_APPEND)
109 bsd_flags |= O_APPEND;
110 if (l_flags & LINUX_O_SYNC)
111 bsd_flags |= O_FSYNC;
112 if (l_flags & LINUX_O_CLOEXEC)
113 bsd_flags |= O_CLOEXEC;
114 if (l_flags & LINUX_O_NONBLOCK)
115 bsd_flags |= O_NONBLOCK;
116 if (l_flags & LINUX_O_ASYNC)
117 bsd_flags |= O_ASYNC;
118 if (l_flags & LINUX_O_CREAT)
119 bsd_flags |= O_CREAT;
120 if (l_flags & LINUX_O_TRUNC)
121 bsd_flags |= O_TRUNC;
122 if (l_flags & LINUX_O_EXCL)
124 if (l_flags & LINUX_O_NOCTTY)
125 bsd_flags |= O_NOCTTY;
126 if (l_flags & LINUX_O_DIRECT)
127 bsd_flags |= O_DIRECT;
128 if (l_flags & LINUX_O_NOFOLLOW)
129 bsd_flags |= O_NOFOLLOW;
130 if (l_flags & LINUX_O_DIRECTORY)
131 bsd_flags |= O_DIRECTORY;
132 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
134 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
140 if (p->p_flag & P_CONTROLT)
142 if (bsd_flags & O_NOCTTY)
146 * XXX In between kern_openat() and fget(), another process
147 * having the same filedesc could use that fd without
150 fd = td->td_retval[0];
151 if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
152 if (fp->f_type != DTYPE_VNODE) {
156 sx_slock(&proctree_lock);
158 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
160 sx_sunlock(&proctree_lock);
161 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
162 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
166 sx_sunlock(&proctree_lock);
177 linux_openat(struct thread *td, struct linux_openat_args *args)
182 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
183 if (args->flags & LINUX_O_CREAT)
184 LCONVPATH_AT(td, args->filename, &path, 1, dfd);
186 LCONVPATH_AT(td, args->filename, &path, 0, dfd);
188 return (linux_common_open(td, dfd, path, args->flags, args->mode));
191 #ifdef LINUX_LEGACY_SYSCALLS
193 linux_open(struct thread *td, struct linux_open_args *args)
197 if (args->flags & LINUX_O_CREAT)
198 LCONVPATHCREAT(td, args->path, &path);
200 LCONVPATHEXIST(td, args->path, &path);
202 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
207 linux_lseek(struct thread *td, struct linux_lseek_args *args)
210 return (kern_lseek(td, args->fdes, args->off, args->whence));
213 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
215 linux_llseek(struct thread *td, struct linux_llseek_args *args)
220 off = (args->olow) | (((off_t) args->ohigh) << 32);
222 error = kern_lseek(td, args->fd, off, args->whence);
226 error = copyout(td->td_retval, args->res, sizeof(off_t));
230 td->td_retval[0] = 0;
233 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
236 * Note that linux_getdents(2) and linux_getdents64(2) have the same
237 * arguments. They only differ in the definition of struct dirent they
239 * Note that linux_readdir(2) is a special case of linux_getdents(2)
240 * where count is always equals 1, meaning that the buffer is one
241 * dirent-structure in size and that the code can't handle more anyway.
242 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
243 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
248 linux_getdents_error(struct thread *td, int fd, int err)
254 /* Linux return ENOTDIR in case when fd is not a directory. */
255 error = getvnode(td, fd, &cap_read_rights, &fp);
259 if (vp->v_type != VDIR) {
271 char d_name[LINUX_NAME_MAX + 1];
279 char d_name[LINUX_NAME_MAX + 1];
283 * Linux uses the last byte in the dirent buffer to store d_type,
284 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
286 #define LINUX_RECLEN(namlen) \
287 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
289 #define LINUX_RECLEN64(namlen) \
290 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
293 #ifdef LINUX_LEGACY_SYSCALLS
295 linux_getdents(struct thread *td, struct linux_getdents_args *args)
298 caddr_t inp, buf; /* BSD-format */
299 int len, reclen; /* BSD-format */
300 caddr_t outp; /* Linux-format */
301 int resid, linuxreclen; /* Linux-format */
302 caddr_t lbuf; /* Linux-format */
304 struct l_dirent *linux_dirent;
308 buflen = min(args->count, MAXBSIZE);
309 buf = malloc(buflen, M_TEMP, M_WAITOK);
311 error = kern_getdirentries(td, args->fd, buf, buflen,
312 &base, NULL, UIO_SYSSPACE);
314 error = linux_getdents_error(td, args->fd, error);
318 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
320 len = td->td_retval[0];
322 outp = (caddr_t)args->dent;
327 bdp = (struct dirent *) inp;
328 reclen = bdp->d_reclen;
329 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
331 * No more space in the user supplied dirent buffer.
334 if (resid < linuxreclen) {
339 linux_dirent = (struct l_dirent*)lbuf;
340 linux_dirent->d_ino = bdp->d_fileno;
341 linux_dirent->d_off = base + reclen;
342 linux_dirent->d_reclen = linuxreclen;
344 * Copy d_type to last byte of l_dirent buffer
346 lbuf[linuxreclen - 1] = bdp->d_type;
347 strlcpy(linux_dirent->d_name, bdp->d_name,
348 linuxreclen - offsetof(struct l_dirent, d_name)-1);
349 error = copyout(linux_dirent, outp, linuxreclen);
357 retval += linuxreclen;
359 resid -= linuxreclen;
361 td->td_retval[0] = retval;
372 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
375 caddr_t inp, buf; /* BSD-format */
376 int len, reclen; /* BSD-format */
377 caddr_t outp; /* Linux-format */
378 int resid, linuxreclen; /* Linux-format */
379 caddr_t lbuf; /* Linux-format */
381 struct l_dirent64 *linux_dirent64;
385 buflen = min(args->count, MAXBSIZE);
386 buf = malloc(buflen, M_TEMP, M_WAITOK);
388 error = kern_getdirentries(td, args->fd, buf, buflen,
389 &base, NULL, UIO_SYSSPACE);
391 error = linux_getdents_error(td, args->fd, error);
395 lbuf = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
397 len = td->td_retval[0];
399 outp = (caddr_t)args->dirent;
404 bdp = (struct dirent *) inp;
405 reclen = bdp->d_reclen;
406 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
408 * No more space in the user supplied dirent buffer.
411 if (resid < linuxreclen) {
416 linux_dirent64 = (struct l_dirent64*)lbuf;
417 linux_dirent64->d_ino = bdp->d_fileno;
418 linux_dirent64->d_off = base + reclen;
419 linux_dirent64->d_reclen = linuxreclen;
420 linux_dirent64->d_type = bdp->d_type;
421 strlcpy(linux_dirent64->d_name, bdp->d_name,
422 linuxreclen - offsetof(struct l_dirent64, d_name));
423 error = copyout(linux_dirent64, outp, linuxreclen);
431 retval += linuxreclen;
433 resid -= linuxreclen;
435 td->td_retval[0] = retval;
444 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
446 linux_readdir(struct thread *td, struct linux_readdir_args *args)
449 caddr_t buf; /* BSD-format */
450 int linuxreclen; /* Linux-format */
451 caddr_t lbuf; /* Linux-format */
453 struct l_dirent *linux_dirent;
456 buflen = LINUX_RECLEN(LINUX_NAME_MAX);
457 buf = malloc(buflen, M_TEMP, M_WAITOK);
459 error = kern_getdirentries(td, args->fd, buf, buflen,
460 &base, NULL, UIO_SYSSPACE);
462 error = linux_getdents_error(td, args->fd, error);
465 if (td->td_retval[0] == 0)
468 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
470 bdp = (struct dirent *) buf;
471 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
473 linux_dirent = (struct l_dirent*)lbuf;
474 linux_dirent->d_ino = bdp->d_fileno;
475 linux_dirent->d_off = linuxreclen;
476 linux_dirent->d_reclen = bdp->d_namlen;
477 strlcpy(linux_dirent->d_name, bdp->d_name,
478 linuxreclen - offsetof(struct l_dirent, d_name));
479 error = copyout(linux_dirent, args->dent, linuxreclen);
481 td->td_retval[0] = linuxreclen;
488 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
492 * These exist mainly for hooks for doing /compat/linux translation.
495 #ifdef LINUX_LEGACY_SYSCALLS
497 linux_access(struct thread *td, struct linux_access_args *args)
502 /* Linux convention. */
503 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
506 LCONVPATHEXIST(td, args->path, &path);
508 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
517 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
522 /* Linux convention. */
523 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
526 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
527 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
529 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode);
535 #ifdef LINUX_LEGACY_SYSCALLS
537 linux_unlink(struct thread *td, struct linux_unlink_args *args)
543 LCONVPATHEXIST(td, args->path, &path);
545 error = kern_funlinkat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0, 0);
546 if (error == EPERM) {
547 /* Introduce POSIX noncompliant behaviour of Linux */
548 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
550 if (S_ISDIR(st.st_mode))
560 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
566 if (args->flag & ~LINUX_AT_REMOVEDIR)
569 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
570 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
572 if (args->flag & LINUX_AT_REMOVEDIR)
573 error = kern_frmdirat(td, dfd, path, FD_NONE, UIO_SYSSPACE, 0);
575 error = kern_funlinkat(td, dfd, path, FD_NONE, UIO_SYSSPACE, 0,
577 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
578 /* Introduce POSIX noncompliant behaviour of Linux */
579 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
580 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
587 linux_chdir(struct thread *td, struct linux_chdir_args *args)
592 LCONVPATHEXIST(td, args->path, &path);
594 error = kern_chdir(td, path, UIO_SYSSPACE);
599 #ifdef LINUX_LEGACY_SYSCALLS
601 linux_chmod(struct thread *td, struct linux_chmod_args *args)
606 LCONVPATHEXIST(td, args->path, &path);
608 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE,
616 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
621 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
622 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
624 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
629 #ifdef LINUX_LEGACY_SYSCALLS
631 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
636 LCONVPATHCREAT(td, args->path, &path);
638 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
645 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
650 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
651 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
653 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
658 #ifdef LINUX_LEGACY_SYSCALLS
660 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
665 LCONVPATHEXIST(td, args->path, &path);
667 error = kern_frmdirat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0);
673 linux_rename(struct thread *td, struct linux_rename_args *args)
678 LCONVPATHEXIST(td, args->from, &from);
679 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
680 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
686 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
694 linux_renameat(struct thread *td, struct linux_renameat_args *args)
696 struct linux_renameat2_args renameat2_args = {
697 .olddfd = args->olddfd,
698 .oldname = args->oldname,
699 .newdfd = args->newdfd,
700 .newname = args->newname,
704 return (linux_renameat2(td, &renameat2_args));
708 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
711 int error, olddfd, newdfd;
713 if (args->flags != 0) {
714 if (args->flags & ~(LINUX_RENAME_EXCHANGE |
715 LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
717 if (args->flags & LINUX_RENAME_EXCHANGE &&
718 args->flags & (LINUX_RENAME_NOREPLACE |
719 LINUX_RENAME_WHITEOUT))
721 linux_msg(td, "renameat2 unsupported flags 0x%x",
726 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
727 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
728 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
729 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
730 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
736 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
742 #ifdef LINUX_LEGACY_SYSCALLS
744 linux_symlink(struct thread *td, struct linux_symlink_args *args)
749 LCONVPATHEXIST(td, args->path, &path);
750 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
751 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
757 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
765 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
770 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
771 LCONVPATHEXIST(td, args->oldname, &path);
772 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
773 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
779 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
785 #ifdef LINUX_LEGACY_SYSCALLS
787 linux_readlink(struct thread *td, struct linux_readlink_args *args)
792 LCONVPATHEXIST(td, args->name, &name);
794 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
795 args->buf, UIO_USERSPACE, args->count);
802 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
807 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
808 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
810 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
811 UIO_USERSPACE, args->bufsiz);
817 linux_truncate(struct thread *td, struct linux_truncate_args *args)
822 LCONVPATHEXIST(td, args->path, &path);
823 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
828 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
830 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
836 #if defined(__amd64__) && defined(COMPAT_LINUX32)
837 length = PAIR32TO64(off_t, args->length);
839 length = args->length;
842 LCONVPATHEXIST(td, args->path, &path);
843 error = kern_truncate(td, path, UIO_SYSSPACE, length);
847 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
850 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
853 return (kern_ftruncate(td, args->fd, args->length));
856 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
858 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
862 #if defined(__amd64__) && defined(COMPAT_LINUX32)
863 length = PAIR32TO64(off_t, args->length);
865 length = args->length;
868 return (kern_ftruncate(td, args->fd, length));
872 #ifdef LINUX_LEGACY_SYSCALLS
874 linux_link(struct thread *td, struct linux_link_args *args)
879 LCONVPATHEXIST(td, args->path, &path);
880 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
881 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
887 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
896 linux_linkat(struct thread *td, struct linux_linkat_args *args)
899 int error, olddfd, newdfd, follow;
901 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
904 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
905 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
906 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
907 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
908 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
914 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
916 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
923 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
926 return (kern_fsync(td, uap->fd, false));
930 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
932 off_t nbytes, offset;
934 #if defined(__amd64__) && defined(COMPAT_LINUX32)
935 nbytes = PAIR32TO64(off_t, uap->nbytes);
936 offset = PAIR32TO64(off_t, uap->offset);
938 nbytes = uap->nbytes;
939 offset = uap->offset;
942 if (offset < 0 || nbytes < 0 ||
943 (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
944 LINUX_SYNC_FILE_RANGE_WRITE |
945 LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
949 return (kern_fsync(td, uap->fd, false));
953 linux_pread(struct thread *td, struct linux_pread_args *uap)
959 #if defined(__amd64__) && defined(COMPAT_LINUX32)
960 offset = PAIR32TO64(off_t, uap->offset);
962 offset = uap->offset;
965 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
967 /* This seems to violate POSIX but Linux does it. */
968 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
971 if (vp->v_type == VDIR)
979 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
983 #if defined(__amd64__) && defined(COMPAT_LINUX32)
984 offset = PAIR32TO64(off_t, uap->offset);
986 offset = uap->offset;
989 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset));
993 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1000 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1001 * pos_l and pos_h, respectively, contain the
1002 * low order and high order 32 bits of offset.
1004 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1005 (sizeof(offset) * 4)) | uap->pos_l;
1008 #ifdef COMPAT_LINUX32
1009 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1011 error = copyinuio(uap->vec, uap->vlen, &auio);
1015 error = kern_preadv(td, uap->fd, auio, offset);
1021 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1028 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1029 * pos_l and pos_h, respectively, contain the
1030 * low order and high order 32 bits of offset.
1032 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1033 (sizeof(offset) * 4)) | uap->pos_l;
1036 #ifdef COMPAT_LINUX32
1037 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1039 error = copyinuio(uap->vec, uap->vlen, &auio);
1043 error = kern_pwritev(td, uap->fd, auio, offset);
1049 linux_mount(struct thread *td, struct linux_mount_args *args)
1051 char fstypename[MFSNAMELEN];
1052 char *mntonname, *mntfromname;
1055 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1056 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1057 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1061 if (args->specialfile != NULL) {
1062 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1066 mntfromname[0] = '\0';
1068 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1072 if (strcmp(fstypename, "ext2") == 0) {
1073 strcpy(fstypename, "ext2fs");
1074 } else if (strcmp(fstypename, "proc") == 0) {
1075 strcpy(fstypename, "linprocfs");
1076 } else if (strcmp(fstypename, "vfat") == 0) {
1077 strcpy(fstypename, "msdosfs");
1083 * Linux SYNC flag is not included; the closest equivalent
1084 * FreeBSD has is !ASYNC, which is our default.
1086 if (args->rwflag & LINUX_MS_RDONLY)
1087 fsflags |= MNT_RDONLY;
1088 if (args->rwflag & LINUX_MS_NOSUID)
1089 fsflags |= MNT_NOSUID;
1090 if (args->rwflag & LINUX_MS_NOEXEC)
1091 fsflags |= MNT_NOEXEC;
1092 if (args->rwflag & LINUX_MS_REMOUNT)
1093 fsflags |= MNT_UPDATE;
1095 error = kernel_vmount(fsflags,
1096 "fstype", fstypename,
1097 "fspath", mntonname,
1098 "from", mntfromname,
1101 free(mntonname, M_TEMP);
1102 free(mntfromname, M_TEMP);
1106 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1108 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1111 return (kern_unmount(td, args->path, 0));
1113 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1115 #ifdef LINUX_LEGACY_SYSCALLS
1117 linux_umount(struct thread *td, struct linux_umount_args *args)
1122 if ((args->flags & LINUX_MNT_FORCE) != 0) {
1123 args->flags &= ~LINUX_MNT_FORCE;
1126 if (args->flags != 0) {
1127 linux_msg(td, "unsupported umount2 flags %#x", args->flags);
1131 return (kern_unmount(td, args->path, flags));
1136 * fcntl family of syscalls
1146 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1152 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1154 switch (linux_flock->l_type) {
1156 bsd_flock->l_type = F_RDLCK;
1159 bsd_flock->l_type = F_WRLCK;
1162 bsd_flock->l_type = F_UNLCK;
1165 bsd_flock->l_type = -1;
1168 bsd_flock->l_whence = linux_flock->l_whence;
1169 bsd_flock->l_start = (off_t)linux_flock->l_start;
1170 bsd_flock->l_len = (off_t)linux_flock->l_len;
1171 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1172 bsd_flock->l_sysid = 0;
1176 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1178 switch (bsd_flock->l_type) {
1180 linux_flock->l_type = LINUX_F_RDLCK;
1183 linux_flock->l_type = LINUX_F_WRLCK;
1186 linux_flock->l_type = LINUX_F_UNLCK;
1189 linux_flock->l_whence = bsd_flock->l_whence;
1190 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1191 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1192 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1195 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1203 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1209 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1211 switch (linux_flock->l_type) {
1213 bsd_flock->l_type = F_RDLCK;
1216 bsd_flock->l_type = F_WRLCK;
1219 bsd_flock->l_type = F_UNLCK;
1222 bsd_flock->l_type = -1;
1225 bsd_flock->l_whence = linux_flock->l_whence;
1226 bsd_flock->l_start = (off_t)linux_flock->l_start;
1227 bsd_flock->l_len = (off_t)linux_flock->l_len;
1228 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1229 bsd_flock->l_sysid = 0;
1233 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1235 switch (bsd_flock->l_type) {
1237 linux_flock->l_type = LINUX_F_RDLCK;
1240 linux_flock->l_type = LINUX_F_WRLCK;
1243 linux_flock->l_type = LINUX_F_UNLCK;
1246 linux_flock->l_whence = bsd_flock->l_whence;
1247 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1248 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1249 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1251 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1254 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1256 struct l_flock linux_flock;
1257 struct flock bsd_flock;
1262 switch (args->cmd) {
1264 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1267 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1270 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1273 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1274 result = td->td_retval[0];
1275 td->td_retval[0] = 0;
1276 if (result & O_RDONLY)
1277 td->td_retval[0] |= LINUX_O_RDONLY;
1278 if (result & O_WRONLY)
1279 td->td_retval[0] |= LINUX_O_WRONLY;
1280 if (result & O_RDWR)
1281 td->td_retval[0] |= LINUX_O_RDWR;
1282 if (result & O_NDELAY)
1283 td->td_retval[0] |= LINUX_O_NONBLOCK;
1284 if (result & O_APPEND)
1285 td->td_retval[0] |= LINUX_O_APPEND;
1286 if (result & O_FSYNC)
1287 td->td_retval[0] |= LINUX_O_SYNC;
1288 if (result & O_ASYNC)
1289 td->td_retval[0] |= LINUX_O_ASYNC;
1290 #ifdef LINUX_O_NOFOLLOW
1291 if (result & O_NOFOLLOW)
1292 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1294 #ifdef LINUX_O_DIRECT
1295 if (result & O_DIRECT)
1296 td->td_retval[0] |= LINUX_O_DIRECT;
1302 if (args->arg & LINUX_O_NDELAY)
1304 if (args->arg & LINUX_O_APPEND)
1306 if (args->arg & LINUX_O_SYNC)
1308 if (args->arg & LINUX_O_ASYNC)
1310 #ifdef LINUX_O_NOFOLLOW
1311 if (args->arg & LINUX_O_NOFOLLOW)
1314 #ifdef LINUX_O_DIRECT
1315 if (args->arg & LINUX_O_DIRECT)
1318 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1321 error = copyin((void *)args->arg, &linux_flock,
1322 sizeof(linux_flock));
1325 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1326 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1329 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1330 return (copyout(&linux_flock, (void *)args->arg,
1331 sizeof(linux_flock)));
1334 error = copyin((void *)args->arg, &linux_flock,
1335 sizeof(linux_flock));
1338 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1339 return (kern_fcntl(td, args->fd, F_SETLK,
1340 (intptr_t)&bsd_flock));
1342 case LINUX_F_SETLKW:
1343 error = copyin((void *)args->arg, &linux_flock,
1344 sizeof(linux_flock));
1347 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1348 return (kern_fcntl(td, args->fd, F_SETLKW,
1349 (intptr_t)&bsd_flock));
1351 case LINUX_F_GETOWN:
1352 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1354 case LINUX_F_SETOWN:
1356 * XXX some Linux applications depend on F_SETOWN having no
1357 * significant effect for pipes (SIGIO is not delivered for
1358 * pipes under Linux-2.2.35 at least).
1360 error = fget(td, args->fd,
1361 &cap_fcntl_rights, &fp);
1364 if (fp->f_type == DTYPE_PIPE) {
1370 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1372 case LINUX_F_DUPFD_CLOEXEC:
1373 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1375 linux_msg(td, "unsupported fcntl cmd %d\n", args->cmd);
1381 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1384 return (fcntl_common(td, args));
1387 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1389 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1391 struct l_flock64 linux_flock;
1392 struct flock bsd_flock;
1393 struct linux_fcntl_args fcntl_args;
1396 switch (args->cmd) {
1397 case LINUX_F_GETLK64:
1398 error = copyin((void *)args->arg, &linux_flock,
1399 sizeof(linux_flock));
1402 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1403 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1406 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1407 return (copyout(&linux_flock, (void *)args->arg,
1408 sizeof(linux_flock)));
1410 case LINUX_F_SETLK64:
1411 error = copyin((void *)args->arg, &linux_flock,
1412 sizeof(linux_flock));
1415 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1416 return (kern_fcntl(td, args->fd, F_SETLK,
1417 (intptr_t)&bsd_flock));
1419 case LINUX_F_SETLKW64:
1420 error = copyin((void *)args->arg, &linux_flock,
1421 sizeof(linux_flock));
1424 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1425 return (kern_fcntl(td, args->fd, F_SETLKW,
1426 (intptr_t)&bsd_flock));
1429 fcntl_args.fd = args->fd;
1430 fcntl_args.cmd = args->cmd;
1431 fcntl_args.arg = args->arg;
1432 return (fcntl_common(td, &fcntl_args));
1434 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1436 #ifdef LINUX_LEGACY_SYSCALLS
1438 linux_chown(struct thread *td, struct linux_chown_args *args)
1443 LCONVPATHEXIST(td, args->path, &path);
1445 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1453 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1456 int error, dfd, flag;
1458 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1461 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1462 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1464 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1465 AT_SYMLINK_NOFOLLOW;
1466 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1472 #ifdef LINUX_LEGACY_SYSCALLS
1474 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1479 LCONVPATHEXIST(td, args->path, &path);
1481 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1482 args->gid, AT_SYMLINK_NOFOLLOW);
1489 convert_fadvice(int advice)
1492 case LINUX_POSIX_FADV_NORMAL:
1493 return (POSIX_FADV_NORMAL);
1494 case LINUX_POSIX_FADV_RANDOM:
1495 return (POSIX_FADV_RANDOM);
1496 case LINUX_POSIX_FADV_SEQUENTIAL:
1497 return (POSIX_FADV_SEQUENTIAL);
1498 case LINUX_POSIX_FADV_WILLNEED:
1499 return (POSIX_FADV_WILLNEED);
1500 case LINUX_POSIX_FADV_DONTNEED:
1501 return (POSIX_FADV_DONTNEED);
1502 case LINUX_POSIX_FADV_NOREUSE:
1503 return (POSIX_FADV_NOREUSE);
1510 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1515 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1516 offset = PAIR32TO64(off_t, args->offset);
1518 offset = args->offset;
1521 advice = convert_fadvice(args->advice);
1524 return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
1527 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1529 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1534 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1535 len = PAIR32TO64(off_t, args->len);
1536 offset = PAIR32TO64(off_t, args->offset);
1539 offset = args->offset;
1542 advice = convert_fadvice(args->advice);
1545 return (kern_posix_fadvise(td, args->fd, offset, len, advice));
1547 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1549 #ifdef LINUX_LEGACY_SYSCALLS
1551 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1556 error = kern_pipe(td, fildes, 0, NULL, NULL);
1560 error = copyout(fildes, args->pipefds, sizeof(fildes));
1562 (void)kern_close(td, fildes[0]);
1563 (void)kern_close(td, fildes[1]);
1571 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1576 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1580 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1581 flags |= O_NONBLOCK;
1582 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1584 error = kern_pipe(td, fildes, flags, NULL, NULL);
1588 error = copyout(fildes, args->pipefds, sizeof(fildes));
1590 (void)kern_close(td, fildes[0]);
1591 (void)kern_close(td, fildes[1]);
1598 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1603 if (args->oldfd == args->newfd)
1605 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1607 if (args->flags & LINUX_O_CLOEXEC)
1608 cmd = F_DUP2FD_CLOEXEC;
1612 newfd = args->newfd;
1613 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1617 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1622 * We emulate only posix_fallocate system call for which
1625 if (args->mode != 0)
1628 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1629 len = PAIR32TO64(off_t, args->len);
1630 offset = PAIR32TO64(off_t, args->offset);
1633 offset = args->offset;
1636 return (kern_posix_fallocate(td, args->fd, offset, len));
1640 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
1643 l_loff_t inoff, outoff, *inoffp, *outoffp;
1647 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
1648 * the native implementation. Enforce it.
1650 if (args->flags != 0) {
1651 linux_msg(td, "copy_file_range unsupported flags 0x%x",
1656 inoffp = outoffp = NULL;
1657 if (args->off_in != NULL) {
1658 error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
1663 if (args->off_out != NULL) {
1664 error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
1670 error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
1671 outoffp, args->len, flags);
1672 if (error == 0 && args->off_in != NULL)
1673 error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
1674 if (error == 0 && args->off_out != NULL)
1675 error = copyout(outoffp, args->off_out, sizeof(l_loff_t));