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);
72 linux_creat(struct thread *td, struct linux_creat_args *args)
77 LCONVPATHEXIST(td, args->path, &path);
80 printf(ARGS(creat, "%s, %d"), path, args->mode);
82 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
83 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
90 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode)
92 struct proc *p = td->td_proc;
98 switch (l_flags & LINUX_O_ACCMODE) {
100 bsd_flags |= O_WRONLY;
106 bsd_flags |= O_RDONLY;
108 if (l_flags & LINUX_O_NDELAY)
109 bsd_flags |= O_NONBLOCK;
110 if (l_flags & LINUX_O_APPEND)
111 bsd_flags |= O_APPEND;
112 if (l_flags & LINUX_O_SYNC)
113 bsd_flags |= O_FSYNC;
114 if (l_flags & LINUX_O_NONBLOCK)
115 bsd_flags |= O_NONBLOCK;
116 if (l_flags & LINUX_FASYNC)
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);
137 if (bsd_flags & O_NOCTTY)
141 * XXX In between kern_openat() and fget(), another process
142 * having the same filedesc could use that fd without
145 fd = td->td_retval[0];
146 if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
147 if (fp->f_type != DTYPE_VNODE) {
151 sx_slock(&proctree_lock);
153 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
155 sx_sunlock(&proctree_lock);
156 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
157 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
161 sx_sunlock(&proctree_lock);
169 printf(LMSG("open returns error %d"), error);
176 linux_openat(struct thread *td, struct linux_openat_args *args)
181 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
182 if (args->flags & LINUX_O_CREAT)
183 LCONVPATH_AT(td, args->filename, &path, 1, dfd);
185 LCONVPATH_AT(td, args->filename, &path, 0, dfd);
188 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd,
189 path, args->flags, args->mode);
191 return (linux_common_open(td, dfd, path, args->flags, args->mode));
195 linux_open(struct thread *td, struct linux_open_args *args)
199 if (args->flags & LINUX_O_CREAT)
200 LCONVPATHCREAT(td, args->path, &path);
202 LCONVPATHEXIST(td, args->path, &path);
205 printf(ARGS(open, "%s, 0x%x, 0x%x"),
206 path, args->flags, args->mode);
208 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
212 linux_lseek(struct thread *td, struct linux_lseek_args *args)
217 printf(ARGS(lseek, "%d, %ld, %d"),
218 args->fdes, (long)args->off, args->whence);
220 return (kern_lseek(td, args->fdes, args->off, args->whence));
223 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
225 linux_llseek(struct thread *td, struct linux_llseek_args *args)
232 printf(ARGS(llseek, "%d, %d:%d, %d"),
233 args->fd, args->ohigh, args->olow, args->whence);
235 off = (args->olow) | (((off_t) args->ohigh) << 32);
237 error = kern_lseek(td, args->fd, off, args->whence);
241 error = copyout(td->td_retval, args->res, sizeof(off_t));
245 td->td_retval[0] = 0;
248 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
251 * Note that linux_getdents(2) and linux_getdents64(2) have the same
252 * arguments. They only differ in the definition of struct dirent they
254 * Note that linux_readdir(2) is a special case of linux_getdents(2)
255 * where count is always equals 1, meaning that the buffer is one
256 * dirent-structure in size and that the code can't handle more anyway.
257 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
258 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
263 linux_getdents_error(struct thread *td, int fd, int err)
269 /* Linux return ENOTDIR in case when fd is not a directory. */
270 error = getvnode(td, fd, &cap_read_rights, &fp);
274 if (vp->v_type != VDIR) {
286 char d_name[LINUX_NAME_MAX + 1];
294 char d_name[LINUX_NAME_MAX + 1];
298 * Linux uses the last byte in the dirent buffer to store d_type,
299 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
301 #define LINUX_RECLEN(namlen) \
302 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
304 #define LINUX_RECLEN64(namlen) \
305 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
309 linux_getdents(struct thread *td, struct linux_getdents_args *args)
312 caddr_t inp, buf; /* BSD-format */
313 int len, reclen; /* BSD-format */
314 caddr_t outp; /* Linux-format */
315 int resid, linuxreclen; /* Linux-format */
316 caddr_t lbuf; /* Linux-format */
318 struct l_dirent *linux_dirent;
323 if (ldebug(getdents))
324 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
326 buflen = min(args->count, MAXBSIZE);
327 buf = malloc(buflen, M_TEMP, M_WAITOK);
329 error = kern_getdirentries(td, args->fd, buf, buflen,
330 &base, NULL, UIO_SYSSPACE);
332 error = linux_getdents_error(td, args->fd, error);
336 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
338 len = td->td_retval[0];
340 outp = (caddr_t)args->dent;
345 bdp = (struct dirent *) inp;
346 reclen = bdp->d_reclen;
347 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
349 * No more space in the user supplied dirent buffer.
352 if (resid < linuxreclen) {
357 linux_dirent = (struct l_dirent*)lbuf;
358 linux_dirent->d_ino = bdp->d_fileno;
359 linux_dirent->d_off = base + reclen;
360 linux_dirent->d_reclen = linuxreclen;
362 * Copy d_type to last byte of l_dirent buffer
364 lbuf[linuxreclen - 1] = bdp->d_type;
365 strlcpy(linux_dirent->d_name, bdp->d_name,
366 linuxreclen - offsetof(struct l_dirent, d_name)-1);
367 error = copyout(linux_dirent, outp, linuxreclen);
375 retval += linuxreclen;
377 resid -= linuxreclen;
379 td->td_retval[0] = retval;
389 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
392 caddr_t inp, buf; /* BSD-format */
393 int len, reclen; /* BSD-format */
394 caddr_t outp; /* Linux-format */
395 int resid, linuxreclen; /* Linux-format */
396 caddr_t lbuf; /* Linux-format */
398 struct l_dirent64 *linux_dirent64;
403 if (ldebug(getdents64))
404 uprintf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
406 buflen = min(args->count, MAXBSIZE);
407 buf = malloc(buflen, M_TEMP, M_WAITOK);
409 error = kern_getdirentries(td, args->fd, buf, buflen,
410 &base, NULL, UIO_SYSSPACE);
412 error = linux_getdents_error(td, args->fd, error);
416 lbuf = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
418 len = td->td_retval[0];
420 outp = (caddr_t)args->dirent;
425 bdp = (struct dirent *) inp;
426 reclen = bdp->d_reclen;
427 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
429 * No more space in the user supplied dirent buffer.
432 if (resid < linuxreclen) {
437 linux_dirent64 = (struct l_dirent64*)lbuf;
438 linux_dirent64->d_ino = bdp->d_fileno;
439 linux_dirent64->d_off = base + reclen;
440 linux_dirent64->d_reclen = linuxreclen;
441 linux_dirent64->d_type = bdp->d_type;
442 strlcpy(linux_dirent64->d_name, bdp->d_name,
443 linuxreclen - offsetof(struct l_dirent64, d_name));
444 error = copyout(linux_dirent64, outp, linuxreclen);
452 retval += linuxreclen;
454 resid -= linuxreclen;
456 td->td_retval[0] = retval;
465 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
467 linux_readdir(struct thread *td, struct linux_readdir_args *args)
470 caddr_t buf; /* BSD-format */
471 int linuxreclen; /* Linux-format */
472 caddr_t lbuf; /* Linux-format */
474 struct l_dirent *linux_dirent;
479 printf(ARGS(readdir, "%d, *"), args->fd);
481 buflen = LINUX_RECLEN(LINUX_NAME_MAX);
482 buf = malloc(buflen, M_TEMP, M_WAITOK);
484 error = kern_getdirentries(td, args->fd, buf, buflen,
485 &base, NULL, UIO_SYSSPACE);
487 error = linux_getdents_error(td, args->fd, error);
490 if (td->td_retval[0] == 0)
493 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
495 bdp = (struct dirent *) buf;
496 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
498 linux_dirent = (struct l_dirent*)lbuf;
499 linux_dirent->d_ino = bdp->d_fileno;
500 linux_dirent->d_off = linuxreclen;
501 linux_dirent->d_reclen = bdp->d_namlen;
502 strlcpy(linux_dirent->d_name, bdp->d_name,
503 linuxreclen - offsetof(struct l_dirent, d_name));
504 error = copyout(linux_dirent, args->dent, linuxreclen);
506 td->td_retval[0] = linuxreclen;
513 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
517 * These exist mainly for hooks for doing /compat/linux translation.
521 linux_access(struct thread *td, struct linux_access_args *args)
526 /* Linux convention. */
527 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
530 LCONVPATHEXIST(td, args->path, &path);
534 printf(ARGS(access, "%s, %d"), path, args->amode);
536 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
544 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
549 /* Linux convention. */
550 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
553 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
554 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
558 printf(ARGS(access, "%s, %d"), path, args->amode);
561 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode);
568 linux_unlink(struct thread *td, struct linux_unlink_args *args)
574 LCONVPATHEXIST(td, args->path, &path);
578 printf(ARGS(unlink, "%s"), path);
581 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0);
582 if (error == EPERM) {
583 /* Introduce POSIX noncompliant behaviour of Linux */
584 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
586 if (S_ISDIR(st.st_mode))
595 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
601 if (args->flag & ~LINUX_AT_REMOVEDIR)
604 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
605 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
608 if (ldebug(unlinkat))
609 printf(ARGS(unlinkat, "%s"), path);
612 if (args->flag & LINUX_AT_REMOVEDIR)
613 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
615 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
616 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
617 /* Introduce POSIX noncompliant behaviour of Linux */
618 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
619 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
626 linux_chdir(struct thread *td, struct linux_chdir_args *args)
631 LCONVPATHEXIST(td, args->path, &path);
635 printf(ARGS(chdir, "%s"), path);
637 error = kern_chdir(td, path, UIO_SYSSPACE);
643 linux_chmod(struct thread *td, struct linux_chmod_args *args)
648 LCONVPATHEXIST(td, args->path, &path);
652 printf(ARGS(chmod, "%s, %d"), path, args->mode);
654 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE,
661 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
666 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
667 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
670 if (ldebug(fchmodat))
671 printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
674 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
680 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
685 LCONVPATHCREAT(td, args->path, &path);
689 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
691 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
697 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
702 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
703 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
707 printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
709 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
715 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
720 LCONVPATHEXIST(td, args->path, &path);
724 printf(ARGS(rmdir, "%s"), path);
726 error = kern_rmdirat(td, AT_FDCWD, path, UIO_SYSSPACE);
732 linux_rename(struct thread *td, struct linux_rename_args *args)
737 LCONVPATHEXIST(td, args->from, &from);
738 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
739 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
747 printf(ARGS(rename, "%s, %s"), from, to);
749 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
756 linux_renameat(struct thread *td, struct linux_renameat_args *args)
759 int error, olddfd, newdfd;
761 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
762 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
763 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
764 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
765 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
772 if (ldebug(renameat))
773 printf(ARGS(renameat, "%s, %s"), from, to);
775 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
782 linux_symlink(struct thread *td, struct linux_symlink_args *args)
787 LCONVPATHEXIST(td, args->path, &path);
788 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
789 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
797 printf(ARGS(symlink, "%s, %s"), path, to);
799 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
806 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
811 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
812 LCONVPATHEXIST(td, args->oldname, &path);
813 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
814 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
821 if (ldebug(symlinkat))
822 printf(ARGS(symlinkat, "%s, %s"), path, to);
825 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
832 linux_readlink(struct thread *td, struct linux_readlink_args *args)
837 LCONVPATHEXIST(td, args->name, &name);
840 if (ldebug(readlink))
841 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
844 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
845 args->buf, UIO_USERSPACE, args->count);
851 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
856 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
857 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
860 if (ldebug(readlinkat))
861 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
865 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
866 UIO_USERSPACE, args->bufsiz);
872 linux_truncate(struct thread *td, struct linux_truncate_args *args)
877 LCONVPATHEXIST(td, args->path, &path);
880 if (ldebug(truncate))
881 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
884 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
889 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
891 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
896 LCONVPATHEXIST(td, args->path, &path);
899 if (ldebug(truncate64))
900 printf(ARGS(truncate64, "%s, %jd"), path, args->length);
903 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
907 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
910 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
913 return (kern_ftruncate(td, args->fd, args->length));
917 linux_link(struct thread *td, struct linux_link_args *args)
922 LCONVPATHEXIST(td, args->path, &path);
923 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
924 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
932 printf(ARGS(link, "%s, %s"), path, to);
934 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
942 linux_linkat(struct thread *td, struct linux_linkat_args *args)
945 int error, olddfd, newdfd, follow;
947 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
950 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
951 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
952 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
953 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
954 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
962 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
963 args->newdfd, to, args->flag);
966 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
968 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
975 linux_fdatasync(td, uap)
977 struct linux_fdatasync_args *uap;
980 return (kern_fsync(td, uap->fd, false));
984 linux_pread(struct thread *td, struct linux_pread_args *uap)
989 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset);
991 /* This seems to violate POSIX but Linux does it. */
992 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
995 if (vp->v_type == VDIR) {
1005 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
1008 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
1012 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1019 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1020 * pos_l and pos_h, respectively, contain the
1021 * low order and high order 32 bits of offset.
1023 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1024 (sizeof(offset) * 4)) | uap->pos_l;
1027 #ifdef COMPAT_LINUX32
1028 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1030 error = copyinuio(uap->vec, uap->vlen, &auio);
1034 error = kern_preadv(td, uap->fd, auio, offset);
1040 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1047 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1048 * pos_l and pos_h, respectively, contain the
1049 * low order and high order 32 bits of offset.
1051 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1052 (sizeof(offset) * 4)) | uap->pos_l;
1055 #ifdef COMPAT_LINUX32
1056 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1058 error = copyinuio(uap->vec, uap->vlen, &auio);
1062 error = kern_pwritev(td, uap->fd, auio, offset);
1068 linux_mount(struct thread *td, struct linux_mount_args *args)
1070 char fstypename[MFSNAMELEN];
1071 char *mntonname, *mntfromname;
1074 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1075 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1076 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1080 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1083 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1089 printf(ARGS(mount, "%s, %s, %s"),
1090 fstypename, mntfromname, mntonname);
1093 if (strcmp(fstypename, "ext2") == 0) {
1094 strcpy(fstypename, "ext2fs");
1095 } else if (strcmp(fstypename, "proc") == 0) {
1096 strcpy(fstypename, "linprocfs");
1097 } else if (strcmp(fstypename, "vfat") == 0) {
1098 strcpy(fstypename, "msdosfs");
1103 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1105 * Linux SYNC flag is not included; the closest equivalent
1106 * FreeBSD has is !ASYNC, which is our default.
1108 if (args->rwflag & LINUX_MS_RDONLY)
1109 fsflags |= MNT_RDONLY;
1110 if (args->rwflag & LINUX_MS_NOSUID)
1111 fsflags |= MNT_NOSUID;
1112 if (args->rwflag & LINUX_MS_NOEXEC)
1113 fsflags |= MNT_NOEXEC;
1114 if (args->rwflag & LINUX_MS_REMOUNT)
1115 fsflags |= MNT_UPDATE;
1118 error = kernel_vmount(fsflags,
1119 "fstype", fstypename,
1120 "fspath", mntonname,
1121 "from", mntfromname,
1124 free(mntonname, M_TEMP);
1125 free(mntfromname, M_TEMP);
1129 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1131 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1133 struct linux_umount_args args2;
1135 args2.path = args->path;
1137 return (linux_umount(td, &args2));
1139 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1142 linux_umount(struct thread *td, struct linux_umount_args *args)
1144 struct unmount_args bsd;
1146 bsd.path = args->path;
1147 bsd.flags = args->flags; /* XXX correct? */
1148 return (sys_unmount(td, &bsd));
1152 * fcntl family of syscalls
1162 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1168 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1170 switch (linux_flock->l_type) {
1172 bsd_flock->l_type = F_RDLCK;
1175 bsd_flock->l_type = F_WRLCK;
1178 bsd_flock->l_type = F_UNLCK;
1181 bsd_flock->l_type = -1;
1184 bsd_flock->l_whence = linux_flock->l_whence;
1185 bsd_flock->l_start = (off_t)linux_flock->l_start;
1186 bsd_flock->l_len = (off_t)linux_flock->l_len;
1187 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1188 bsd_flock->l_sysid = 0;
1192 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1194 switch (bsd_flock->l_type) {
1196 linux_flock->l_type = LINUX_F_RDLCK;
1199 linux_flock->l_type = LINUX_F_WRLCK;
1202 linux_flock->l_type = LINUX_F_UNLCK;
1205 linux_flock->l_whence = bsd_flock->l_whence;
1206 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1207 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1208 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1211 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1219 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1225 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1227 switch (linux_flock->l_type) {
1229 bsd_flock->l_type = F_RDLCK;
1232 bsd_flock->l_type = F_WRLCK;
1235 bsd_flock->l_type = F_UNLCK;
1238 bsd_flock->l_type = -1;
1241 bsd_flock->l_whence = linux_flock->l_whence;
1242 bsd_flock->l_start = (off_t)linux_flock->l_start;
1243 bsd_flock->l_len = (off_t)linux_flock->l_len;
1244 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1245 bsd_flock->l_sysid = 0;
1249 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1251 switch (bsd_flock->l_type) {
1253 linux_flock->l_type = LINUX_F_RDLCK;
1256 linux_flock->l_type = LINUX_F_WRLCK;
1259 linux_flock->l_type = LINUX_F_UNLCK;
1262 linux_flock->l_whence = bsd_flock->l_whence;
1263 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1264 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1265 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1267 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1270 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1272 struct l_flock linux_flock;
1273 struct flock bsd_flock;
1278 switch (args->cmd) {
1280 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1283 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1286 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1289 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1290 result = td->td_retval[0];
1291 td->td_retval[0] = 0;
1292 if (result & O_RDONLY)
1293 td->td_retval[0] |= LINUX_O_RDONLY;
1294 if (result & O_WRONLY)
1295 td->td_retval[0] |= LINUX_O_WRONLY;
1296 if (result & O_RDWR)
1297 td->td_retval[0] |= LINUX_O_RDWR;
1298 if (result & O_NDELAY)
1299 td->td_retval[0] |= LINUX_O_NONBLOCK;
1300 if (result & O_APPEND)
1301 td->td_retval[0] |= LINUX_O_APPEND;
1302 if (result & O_FSYNC)
1303 td->td_retval[0] |= LINUX_O_SYNC;
1304 if (result & O_ASYNC)
1305 td->td_retval[0] |= LINUX_FASYNC;
1306 #ifdef LINUX_O_NOFOLLOW
1307 if (result & O_NOFOLLOW)
1308 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1310 #ifdef LINUX_O_DIRECT
1311 if (result & O_DIRECT)
1312 td->td_retval[0] |= LINUX_O_DIRECT;
1318 if (args->arg & LINUX_O_NDELAY)
1320 if (args->arg & LINUX_O_APPEND)
1322 if (args->arg & LINUX_O_SYNC)
1324 if (args->arg & LINUX_FASYNC)
1326 #ifdef LINUX_O_NOFOLLOW
1327 if (args->arg & LINUX_O_NOFOLLOW)
1330 #ifdef LINUX_O_DIRECT
1331 if (args->arg & LINUX_O_DIRECT)
1334 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1337 error = copyin((void *)args->arg, &linux_flock,
1338 sizeof(linux_flock));
1341 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1342 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1345 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1346 return (copyout(&linux_flock, (void *)args->arg,
1347 sizeof(linux_flock)));
1350 error = copyin((void *)args->arg, &linux_flock,
1351 sizeof(linux_flock));
1354 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1355 return (kern_fcntl(td, args->fd, F_SETLK,
1356 (intptr_t)&bsd_flock));
1358 case LINUX_F_SETLKW:
1359 error = copyin((void *)args->arg, &linux_flock,
1360 sizeof(linux_flock));
1363 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1364 return (kern_fcntl(td, args->fd, F_SETLKW,
1365 (intptr_t)&bsd_flock));
1367 case LINUX_F_GETOWN:
1368 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1370 case LINUX_F_SETOWN:
1372 * XXX some Linux applications depend on F_SETOWN having no
1373 * significant effect for pipes (SIGIO is not delivered for
1374 * pipes under Linux-2.2.35 at least).
1376 error = fget(td, args->fd,
1377 &cap_fcntl_rights, &fp);
1380 if (fp->f_type == DTYPE_PIPE) {
1386 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1388 case LINUX_F_DUPFD_CLOEXEC:
1389 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1396 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1401 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1404 return (fcntl_common(td, args));
1407 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1409 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1411 struct l_flock64 linux_flock;
1412 struct flock bsd_flock;
1413 struct linux_fcntl_args fcntl_args;
1417 if (ldebug(fcntl64))
1418 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1421 switch (args->cmd) {
1422 case LINUX_F_GETLK64:
1423 error = copyin((void *)args->arg, &linux_flock,
1424 sizeof(linux_flock));
1427 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1428 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1431 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1432 return (copyout(&linux_flock, (void *)args->arg,
1433 sizeof(linux_flock)));
1435 case LINUX_F_SETLK64:
1436 error = copyin((void *)args->arg, &linux_flock,
1437 sizeof(linux_flock));
1440 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1441 return (kern_fcntl(td, args->fd, F_SETLK,
1442 (intptr_t)&bsd_flock));
1444 case LINUX_F_SETLKW64:
1445 error = copyin((void *)args->arg, &linux_flock,
1446 sizeof(linux_flock));
1449 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1450 return (kern_fcntl(td, args->fd, F_SETLKW,
1451 (intptr_t)&bsd_flock));
1454 fcntl_args.fd = args->fd;
1455 fcntl_args.cmd = args->cmd;
1456 fcntl_args.arg = args->arg;
1457 return (fcntl_common(td, &fcntl_args));
1459 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1462 linux_chown(struct thread *td, struct linux_chown_args *args)
1467 LCONVPATHEXIST(td, args->path, &path);
1471 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1473 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1480 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1483 int error, dfd, flag;
1485 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1488 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1489 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1492 if (ldebug(fchownat))
1493 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1496 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1497 AT_SYMLINK_NOFOLLOW;
1498 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1505 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1510 LCONVPATHEXIST(td, args->path, &path);
1514 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1516 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1517 args->gid, AT_SYMLINK_NOFOLLOW);
1523 convert_fadvice(int advice)
1526 case LINUX_POSIX_FADV_NORMAL:
1527 return (POSIX_FADV_NORMAL);
1528 case LINUX_POSIX_FADV_RANDOM:
1529 return (POSIX_FADV_RANDOM);
1530 case LINUX_POSIX_FADV_SEQUENTIAL:
1531 return (POSIX_FADV_SEQUENTIAL);
1532 case LINUX_POSIX_FADV_WILLNEED:
1533 return (POSIX_FADV_WILLNEED);
1534 case LINUX_POSIX_FADV_DONTNEED:
1535 return (POSIX_FADV_DONTNEED);
1536 case LINUX_POSIX_FADV_NOREUSE:
1537 return (POSIX_FADV_NOREUSE);
1544 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1548 advice = convert_fadvice(args->advice);
1551 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1555 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1557 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1561 advice = convert_fadvice(args->advice);
1564 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1567 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1570 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1577 printf(ARGS(pipe, "*"));
1580 error = kern_pipe(td, fildes, 0, NULL, NULL);
1584 error = copyout(fildes, args->pipefds, sizeof(fildes));
1586 (void)kern_close(td, fildes[0]);
1587 (void)kern_close(td, fildes[1]);
1594 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1601 printf(ARGS(pipe2, "*, %d"), args->flags);
1604 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1608 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1609 flags |= O_NONBLOCK;
1610 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1612 error = kern_pipe(td, fildes, flags, NULL, NULL);
1616 error = copyout(fildes, args->pipefds, sizeof(fildes));
1618 (void)kern_close(td, fildes[0]);
1619 (void)kern_close(td, fildes[1]);
1626 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1631 if (args->oldfd == args->newfd)
1633 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1635 if (args->flags & LINUX_O_CLOEXEC)
1636 cmd = F_DUP2FD_CLOEXEC;
1640 newfd = args->newfd;
1641 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1645 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1649 * We emulate only posix_fallocate system call for which
1652 if (args->mode != 0)
1655 return (kern_posix_fallocate(td, args->fd, args->offset,