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 <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
63 #include <compat/linux/linux_misc.h>
64 #include <compat/linux/linux_util.h>
65 #include <compat/linux/linux_file.h>
67 static int linux_common_open(struct thread *, int, char *, int, int);
68 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_NONBLOCK)
113 bsd_flags |= O_NONBLOCK;
114 if (l_flags & LINUX_FASYNC)
115 bsd_flags |= O_ASYNC;
116 if (l_flags & LINUX_O_CREAT)
117 bsd_flags |= O_CREAT;
118 if (l_flags & LINUX_O_TRUNC)
119 bsd_flags |= O_TRUNC;
120 if (l_flags & LINUX_O_EXCL)
122 if (l_flags & LINUX_O_NOCTTY)
123 bsd_flags |= O_NOCTTY;
124 if (l_flags & LINUX_O_DIRECT)
125 bsd_flags |= O_DIRECT;
126 if (l_flags & LINUX_O_NOFOLLOW)
127 bsd_flags |= O_NOFOLLOW;
128 if (l_flags & LINUX_O_DIRECTORY)
129 bsd_flags |= O_DIRECTORY;
130 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
132 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
135 if (bsd_flags & O_NOCTTY)
139 * XXX In between kern_openat() and fget(), another process
140 * having the same filedesc could use that fd without
143 fd = td->td_retval[0];
144 if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
145 if (fp->f_type != DTYPE_VNODE) {
149 sx_slock(&proctree_lock);
151 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
153 sx_sunlock(&proctree_lock);
154 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
155 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
159 sx_sunlock(&proctree_lock);
170 linux_openat(struct thread *td, struct linux_openat_args *args)
175 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
176 if (args->flags & LINUX_O_CREAT)
177 LCONVPATH_AT(td, args->filename, &path, 1, dfd);
179 LCONVPATH_AT(td, args->filename, &path, 0, dfd);
181 return (linux_common_open(td, dfd, path, args->flags, args->mode));
184 #ifdef LINUX_LEGACY_SYSCALLS
186 linux_open(struct thread *td, struct linux_open_args *args)
190 if (args->flags & LINUX_O_CREAT)
191 LCONVPATHCREAT(td, args->path, &path);
193 LCONVPATHEXIST(td, args->path, &path);
195 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
200 linux_lseek(struct thread *td, struct linux_lseek_args *args)
203 return (kern_lseek(td, args->fdes, args->off, args->whence));
206 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
208 linux_llseek(struct thread *td, struct linux_llseek_args *args)
213 off = (args->olow) | (((off_t) args->ohigh) << 32);
215 error = kern_lseek(td, args->fd, off, args->whence);
219 error = copyout(td->td_retval, args->res, sizeof(off_t));
223 td->td_retval[0] = 0;
226 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
229 * Note that linux_getdents(2) and linux_getdents64(2) have the same
230 * arguments. They only differ in the definition of struct dirent they
232 * Note that linux_readdir(2) is a special case of linux_getdents(2)
233 * where count is always equals 1, meaning that the buffer is one
234 * dirent-structure in size and that the code can't handle more anyway.
235 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
236 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
241 linux_getdents_error(struct thread *td, int fd, int err)
247 /* Linux return ENOTDIR in case when fd is not a directory. */
248 error = getvnode(td, fd, &cap_read_rights, &fp);
252 if (vp->v_type != VDIR) {
264 char d_name[LINUX_NAME_MAX + 1];
272 char d_name[LINUX_NAME_MAX + 1];
276 * Linux uses the last byte in the dirent buffer to store d_type,
277 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
279 #define LINUX_RECLEN(namlen) \
280 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
282 #define LINUX_RECLEN64(namlen) \
283 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
286 #ifdef LINUX_LEGACY_SYSCALLS
288 linux_getdents(struct thread *td, struct linux_getdents_args *args)
291 caddr_t inp, buf; /* BSD-format */
292 int len, reclen; /* BSD-format */
293 caddr_t outp; /* Linux-format */
294 int resid, linuxreclen; /* Linux-format */
295 caddr_t lbuf; /* Linux-format */
297 struct l_dirent *linux_dirent;
301 buflen = min(args->count, MAXBSIZE);
302 buf = malloc(buflen, M_TEMP, M_WAITOK);
304 error = kern_getdirentries(td, args->fd, buf, buflen,
305 &base, NULL, UIO_SYSSPACE);
307 error = linux_getdents_error(td, args->fd, error);
311 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
313 len = td->td_retval[0];
315 outp = (caddr_t)args->dent;
320 bdp = (struct dirent *) inp;
321 reclen = bdp->d_reclen;
322 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
324 * No more space in the user supplied dirent buffer.
327 if (resid < linuxreclen) {
332 linux_dirent = (struct l_dirent*)lbuf;
333 linux_dirent->d_ino = bdp->d_fileno;
334 linux_dirent->d_off = base + reclen;
335 linux_dirent->d_reclen = linuxreclen;
337 * Copy d_type to last byte of l_dirent buffer
339 lbuf[linuxreclen - 1] = bdp->d_type;
340 strlcpy(linux_dirent->d_name, bdp->d_name,
341 linuxreclen - offsetof(struct l_dirent, d_name)-1);
342 error = copyout(linux_dirent, outp, linuxreclen);
350 retval += linuxreclen;
352 resid -= linuxreclen;
354 td->td_retval[0] = retval;
365 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
368 caddr_t inp, buf; /* BSD-format */
369 int len, reclen; /* BSD-format */
370 caddr_t outp; /* Linux-format */
371 int resid, linuxreclen; /* Linux-format */
372 caddr_t lbuf; /* Linux-format */
374 struct l_dirent64 *linux_dirent64;
378 buflen = min(args->count, MAXBSIZE);
379 buf = malloc(buflen, M_TEMP, M_WAITOK);
381 error = kern_getdirentries(td, args->fd, buf, buflen,
382 &base, NULL, UIO_SYSSPACE);
384 error = linux_getdents_error(td, args->fd, error);
388 lbuf = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
390 len = td->td_retval[0];
392 outp = (caddr_t)args->dirent;
397 bdp = (struct dirent *) inp;
398 reclen = bdp->d_reclen;
399 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
401 * No more space in the user supplied dirent buffer.
404 if (resid < linuxreclen) {
409 linux_dirent64 = (struct l_dirent64*)lbuf;
410 linux_dirent64->d_ino = bdp->d_fileno;
411 linux_dirent64->d_off = base + reclen;
412 linux_dirent64->d_reclen = linuxreclen;
413 linux_dirent64->d_type = bdp->d_type;
414 strlcpy(linux_dirent64->d_name, bdp->d_name,
415 linuxreclen - offsetof(struct l_dirent64, d_name));
416 error = copyout(linux_dirent64, outp, linuxreclen);
424 retval += linuxreclen;
426 resid -= linuxreclen;
428 td->td_retval[0] = retval;
437 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
439 linux_readdir(struct thread *td, struct linux_readdir_args *args)
442 caddr_t buf; /* BSD-format */
443 int linuxreclen; /* Linux-format */
444 caddr_t lbuf; /* Linux-format */
446 struct l_dirent *linux_dirent;
449 buflen = LINUX_RECLEN(LINUX_NAME_MAX);
450 buf = malloc(buflen, M_TEMP, M_WAITOK);
452 error = kern_getdirentries(td, args->fd, buf, buflen,
453 &base, NULL, UIO_SYSSPACE);
455 error = linux_getdents_error(td, args->fd, error);
458 if (td->td_retval[0] == 0)
461 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
463 bdp = (struct dirent *) buf;
464 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
466 linux_dirent = (struct l_dirent*)lbuf;
467 linux_dirent->d_ino = bdp->d_fileno;
468 linux_dirent->d_off = linuxreclen;
469 linux_dirent->d_reclen = bdp->d_namlen;
470 strlcpy(linux_dirent->d_name, bdp->d_name,
471 linuxreclen - offsetof(struct l_dirent, d_name));
472 error = copyout(linux_dirent, args->dent, linuxreclen);
474 td->td_retval[0] = linuxreclen;
481 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
485 * These exist mainly for hooks for doing /compat/linux translation.
488 #ifdef LINUX_LEGACY_SYSCALLS
490 linux_access(struct thread *td, struct linux_access_args *args)
495 /* Linux convention. */
496 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
499 LCONVPATHEXIST(td, args->path, &path);
501 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
510 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
515 /* Linux convention. */
516 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
519 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
520 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
522 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode);
528 #ifdef LINUX_LEGACY_SYSCALLS
530 linux_unlink(struct thread *td, struct linux_unlink_args *args)
536 LCONVPATHEXIST(td, args->path, &path);
538 error = kern_funlinkat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0, 0);
539 if (error == EPERM) {
540 /* Introduce POSIX noncompliant behaviour of Linux */
541 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
543 if (S_ISDIR(st.st_mode))
553 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
559 if (args->flag & ~LINUX_AT_REMOVEDIR)
562 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
563 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
565 if (args->flag & LINUX_AT_REMOVEDIR)
566 error = kern_frmdirat(td, dfd, path, FD_NONE, UIO_SYSSPACE, 0);
568 error = kern_funlinkat(td, dfd, path, FD_NONE, UIO_SYSSPACE, 0,
570 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
571 /* Introduce POSIX noncompliant behaviour of Linux */
572 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
573 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
580 linux_chdir(struct thread *td, struct linux_chdir_args *args)
585 LCONVPATHEXIST(td, args->path, &path);
587 error = kern_chdir(td, path, UIO_SYSSPACE);
592 #ifdef LINUX_LEGACY_SYSCALLS
594 linux_chmod(struct thread *td, struct linux_chmod_args *args)
599 LCONVPATHEXIST(td, args->path, &path);
601 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE,
609 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
614 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
615 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
617 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
622 #ifdef LINUX_LEGACY_SYSCALLS
624 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
629 LCONVPATHCREAT(td, args->path, &path);
631 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
638 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
643 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
644 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
646 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
651 #ifdef LINUX_LEGACY_SYSCALLS
653 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
658 LCONVPATHEXIST(td, args->path, &path);
660 error = kern_frmdirat(td, AT_FDCWD, path, FD_NONE, UIO_SYSSPACE, 0);
666 linux_rename(struct thread *td, struct linux_rename_args *args)
671 LCONVPATHEXIST(td, args->from, &from);
672 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
673 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
679 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
687 linux_renameat(struct thread *td, struct linux_renameat_args *args)
689 struct linux_renameat2_args renameat2_args = {
690 .olddfd = args->olddfd,
691 .oldname = args->oldname,
692 .newdfd = args->newdfd,
693 .newname = args->newname,
697 return (linux_renameat2(td, &renameat2_args));
701 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
704 int error, olddfd, newdfd;
706 if (args->flags != 0) {
707 linux_msg(td, "renameat2 unsupported flags 0x%x",
712 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
713 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
714 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
715 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
716 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
722 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
728 #ifdef LINUX_LEGACY_SYSCALLS
730 linux_symlink(struct thread *td, struct linux_symlink_args *args)
735 LCONVPATHEXIST(td, args->path, &path);
736 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
737 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
743 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
751 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
756 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
757 LCONVPATHEXIST(td, args->oldname, &path);
758 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
759 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
765 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
771 #ifdef LINUX_LEGACY_SYSCALLS
773 linux_readlink(struct thread *td, struct linux_readlink_args *args)
778 LCONVPATHEXIST(td, args->name, &name);
780 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
781 args->buf, UIO_USERSPACE, args->count);
788 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
793 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
794 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
796 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
797 UIO_USERSPACE, args->bufsiz);
803 linux_truncate(struct thread *td, struct linux_truncate_args *args)
808 LCONVPATHEXIST(td, args->path, &path);
810 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
815 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
817 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
822 LCONVPATHEXIST(td, args->path, &path);
824 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
828 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
831 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
834 return (kern_ftruncate(td, args->fd, args->length));
837 #ifdef LINUX_LEGACY_SYSCALLS
839 linux_link(struct thread *td, struct linux_link_args *args)
844 LCONVPATHEXIST(td, args->path, &path);
845 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
846 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
852 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
861 linux_linkat(struct thread *td, struct linux_linkat_args *args)
864 int error, olddfd, newdfd, follow;
866 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
869 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
870 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
871 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
872 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
873 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
879 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
881 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
888 linux_fdatasync(td, uap)
890 struct linux_fdatasync_args *uap;
893 return (kern_fsync(td, uap->fd, false));
897 linux_pread(struct thread *td, struct linux_pread_args *uap)
902 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset);
904 /* This seems to violate POSIX but Linux does it. */
905 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
908 if (vp->v_type == VDIR) {
918 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
921 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
925 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
932 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
933 * pos_l and pos_h, respectively, contain the
934 * low order and high order 32 bits of offset.
936 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
937 (sizeof(offset) * 4)) | uap->pos_l;
940 #ifdef COMPAT_LINUX32
941 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
943 error = copyinuio(uap->vec, uap->vlen, &auio);
947 error = kern_preadv(td, uap->fd, auio, offset);
953 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
960 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
961 * pos_l and pos_h, respectively, contain the
962 * low order and high order 32 bits of offset.
964 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
965 (sizeof(offset) * 4)) | uap->pos_l;
968 #ifdef COMPAT_LINUX32
969 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
971 error = copyinuio(uap->vec, uap->vlen, &auio);
975 error = kern_pwritev(td, uap->fd, auio, offset);
981 linux_mount(struct thread *td, struct linux_mount_args *args)
983 char fstypename[MFSNAMELEN];
984 char *mntonname, *mntfromname;
987 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
988 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
989 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
993 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
996 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1000 if (strcmp(fstypename, "ext2") == 0) {
1001 strcpy(fstypename, "ext2fs");
1002 } else if (strcmp(fstypename, "proc") == 0) {
1003 strcpy(fstypename, "linprocfs");
1004 } else if (strcmp(fstypename, "vfat") == 0) {
1005 strcpy(fstypename, "msdosfs");
1010 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1012 * Linux SYNC flag is not included; the closest equivalent
1013 * FreeBSD has is !ASYNC, which is our default.
1015 if (args->rwflag & LINUX_MS_RDONLY)
1016 fsflags |= MNT_RDONLY;
1017 if (args->rwflag & LINUX_MS_NOSUID)
1018 fsflags |= MNT_NOSUID;
1019 if (args->rwflag & LINUX_MS_NOEXEC)
1020 fsflags |= MNT_NOEXEC;
1021 if (args->rwflag & LINUX_MS_REMOUNT)
1022 fsflags |= MNT_UPDATE;
1025 error = kernel_vmount(fsflags,
1026 "fstype", fstypename,
1027 "fspath", mntonname,
1028 "from", mntfromname,
1031 free(mntonname, M_TEMP);
1032 free(mntfromname, M_TEMP);
1036 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1038 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1040 struct linux_umount_args args2;
1042 args2.path = args->path;
1044 return (linux_umount(td, &args2));
1046 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1048 #ifdef LINUX_LEGACY_SYSCALLS
1050 linux_umount(struct thread *td, struct linux_umount_args *args)
1052 struct unmount_args bsd;
1054 bsd.path = args->path;
1055 bsd.flags = args->flags; /* XXX correct? */
1056 return (sys_unmount(td, &bsd));
1061 * fcntl family of syscalls
1071 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1077 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1079 switch (linux_flock->l_type) {
1081 bsd_flock->l_type = F_RDLCK;
1084 bsd_flock->l_type = F_WRLCK;
1087 bsd_flock->l_type = F_UNLCK;
1090 bsd_flock->l_type = -1;
1093 bsd_flock->l_whence = linux_flock->l_whence;
1094 bsd_flock->l_start = (off_t)linux_flock->l_start;
1095 bsd_flock->l_len = (off_t)linux_flock->l_len;
1096 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1097 bsd_flock->l_sysid = 0;
1101 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1103 switch (bsd_flock->l_type) {
1105 linux_flock->l_type = LINUX_F_RDLCK;
1108 linux_flock->l_type = LINUX_F_WRLCK;
1111 linux_flock->l_type = LINUX_F_UNLCK;
1114 linux_flock->l_whence = bsd_flock->l_whence;
1115 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1116 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1117 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1120 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1128 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1134 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1136 switch (linux_flock->l_type) {
1138 bsd_flock->l_type = F_RDLCK;
1141 bsd_flock->l_type = F_WRLCK;
1144 bsd_flock->l_type = F_UNLCK;
1147 bsd_flock->l_type = -1;
1150 bsd_flock->l_whence = linux_flock->l_whence;
1151 bsd_flock->l_start = (off_t)linux_flock->l_start;
1152 bsd_flock->l_len = (off_t)linux_flock->l_len;
1153 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1154 bsd_flock->l_sysid = 0;
1158 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1160 switch (bsd_flock->l_type) {
1162 linux_flock->l_type = LINUX_F_RDLCK;
1165 linux_flock->l_type = LINUX_F_WRLCK;
1168 linux_flock->l_type = LINUX_F_UNLCK;
1171 linux_flock->l_whence = bsd_flock->l_whence;
1172 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1173 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1174 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1176 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1179 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1181 struct l_flock linux_flock;
1182 struct flock bsd_flock;
1187 switch (args->cmd) {
1189 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1192 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1195 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1198 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1199 result = td->td_retval[0];
1200 td->td_retval[0] = 0;
1201 if (result & O_RDONLY)
1202 td->td_retval[0] |= LINUX_O_RDONLY;
1203 if (result & O_WRONLY)
1204 td->td_retval[0] |= LINUX_O_WRONLY;
1205 if (result & O_RDWR)
1206 td->td_retval[0] |= LINUX_O_RDWR;
1207 if (result & O_NDELAY)
1208 td->td_retval[0] |= LINUX_O_NONBLOCK;
1209 if (result & O_APPEND)
1210 td->td_retval[0] |= LINUX_O_APPEND;
1211 if (result & O_FSYNC)
1212 td->td_retval[0] |= LINUX_O_SYNC;
1213 if (result & O_ASYNC)
1214 td->td_retval[0] |= LINUX_FASYNC;
1215 #ifdef LINUX_O_NOFOLLOW
1216 if (result & O_NOFOLLOW)
1217 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1219 #ifdef LINUX_O_DIRECT
1220 if (result & O_DIRECT)
1221 td->td_retval[0] |= LINUX_O_DIRECT;
1227 if (args->arg & LINUX_O_NDELAY)
1229 if (args->arg & LINUX_O_APPEND)
1231 if (args->arg & LINUX_O_SYNC)
1233 if (args->arg & LINUX_FASYNC)
1235 #ifdef LINUX_O_NOFOLLOW
1236 if (args->arg & LINUX_O_NOFOLLOW)
1239 #ifdef LINUX_O_DIRECT
1240 if (args->arg & LINUX_O_DIRECT)
1243 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1246 error = copyin((void *)args->arg, &linux_flock,
1247 sizeof(linux_flock));
1250 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1251 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1254 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1255 return (copyout(&linux_flock, (void *)args->arg,
1256 sizeof(linux_flock)));
1259 error = copyin((void *)args->arg, &linux_flock,
1260 sizeof(linux_flock));
1263 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1264 return (kern_fcntl(td, args->fd, F_SETLK,
1265 (intptr_t)&bsd_flock));
1267 case LINUX_F_SETLKW:
1268 error = copyin((void *)args->arg, &linux_flock,
1269 sizeof(linux_flock));
1272 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1273 return (kern_fcntl(td, args->fd, F_SETLKW,
1274 (intptr_t)&bsd_flock));
1276 case LINUX_F_GETOWN:
1277 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1279 case LINUX_F_SETOWN:
1281 * XXX some Linux applications depend on F_SETOWN having no
1282 * significant effect for pipes (SIGIO is not delivered for
1283 * pipes under Linux-2.2.35 at least).
1285 error = fget(td, args->fd,
1286 &cap_fcntl_rights, &fp);
1289 if (fp->f_type == DTYPE_PIPE) {
1295 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1297 case LINUX_F_DUPFD_CLOEXEC:
1298 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1305 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1308 return (fcntl_common(td, args));
1311 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1313 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1315 struct l_flock64 linux_flock;
1316 struct flock bsd_flock;
1317 struct linux_fcntl_args fcntl_args;
1320 switch (args->cmd) {
1321 case LINUX_F_GETLK64:
1322 error = copyin((void *)args->arg, &linux_flock,
1323 sizeof(linux_flock));
1326 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1327 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1330 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1331 return (copyout(&linux_flock, (void *)args->arg,
1332 sizeof(linux_flock)));
1334 case LINUX_F_SETLK64:
1335 error = copyin((void *)args->arg, &linux_flock,
1336 sizeof(linux_flock));
1339 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1340 return (kern_fcntl(td, args->fd, F_SETLK,
1341 (intptr_t)&bsd_flock));
1343 case LINUX_F_SETLKW64:
1344 error = copyin((void *)args->arg, &linux_flock,
1345 sizeof(linux_flock));
1348 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1349 return (kern_fcntl(td, args->fd, F_SETLKW,
1350 (intptr_t)&bsd_flock));
1353 fcntl_args.fd = args->fd;
1354 fcntl_args.cmd = args->cmd;
1355 fcntl_args.arg = args->arg;
1356 return (fcntl_common(td, &fcntl_args));
1358 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1360 #ifdef LINUX_LEGACY_SYSCALLS
1362 linux_chown(struct thread *td, struct linux_chown_args *args)
1367 LCONVPATHEXIST(td, args->path, &path);
1369 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1377 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1380 int error, dfd, flag;
1382 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1385 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1386 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1388 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1389 AT_SYMLINK_NOFOLLOW;
1390 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1396 #ifdef LINUX_LEGACY_SYSCALLS
1398 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1403 LCONVPATHEXIST(td, args->path, &path);
1405 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1406 args->gid, AT_SYMLINK_NOFOLLOW);
1413 convert_fadvice(int advice)
1416 case LINUX_POSIX_FADV_NORMAL:
1417 return (POSIX_FADV_NORMAL);
1418 case LINUX_POSIX_FADV_RANDOM:
1419 return (POSIX_FADV_RANDOM);
1420 case LINUX_POSIX_FADV_SEQUENTIAL:
1421 return (POSIX_FADV_SEQUENTIAL);
1422 case LINUX_POSIX_FADV_WILLNEED:
1423 return (POSIX_FADV_WILLNEED);
1424 case LINUX_POSIX_FADV_DONTNEED:
1425 return (POSIX_FADV_DONTNEED);
1426 case LINUX_POSIX_FADV_NOREUSE:
1427 return (POSIX_FADV_NOREUSE);
1434 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1438 advice = convert_fadvice(args->advice);
1441 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1445 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1447 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1451 advice = convert_fadvice(args->advice);
1454 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1457 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1459 #ifdef LINUX_LEGACY_SYSCALLS
1461 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1466 error = kern_pipe(td, fildes, 0, NULL, NULL);
1470 error = copyout(fildes, args->pipefds, sizeof(fildes));
1472 (void)kern_close(td, fildes[0]);
1473 (void)kern_close(td, fildes[1]);
1481 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1486 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1490 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1491 flags |= O_NONBLOCK;
1492 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1494 error = kern_pipe(td, fildes, flags, NULL, NULL);
1498 error = copyout(fildes, args->pipefds, sizeof(fildes));
1500 (void)kern_close(td, fildes[0]);
1501 (void)kern_close(td, fildes[1]);
1508 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1513 if (args->oldfd == args->newfd)
1515 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1517 if (args->flags & LINUX_O_CLOEXEC)
1518 cmd = F_DUP2FD_CLOEXEC;
1522 newfd = args->newfd;
1523 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1527 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1531 * We emulate only posix_fallocate system call for which
1534 if (args->mode != 0)
1537 return (kern_posix_fallocate(td, args->fd, args->offset,