2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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)
93 struct proc *p = td->td_proc;
99 switch (l_flags & LINUX_O_ACCMODE) {
101 bsd_flags |= O_WRONLY;
107 bsd_flags |= O_RDONLY;
109 if (l_flags & LINUX_O_NDELAY)
110 bsd_flags |= O_NONBLOCK;
111 if (l_flags & LINUX_O_APPEND)
112 bsd_flags |= O_APPEND;
113 if (l_flags & LINUX_O_SYNC)
114 bsd_flags |= O_FSYNC;
115 if (l_flags & LINUX_O_NONBLOCK)
116 bsd_flags |= O_NONBLOCK;
117 if (l_flags & LINUX_FASYNC)
118 bsd_flags |= O_ASYNC;
119 if (l_flags & LINUX_O_CREAT)
120 bsd_flags |= O_CREAT;
121 if (l_flags & LINUX_O_TRUNC)
122 bsd_flags |= O_TRUNC;
123 if (l_flags & LINUX_O_EXCL)
125 if (l_flags & LINUX_O_NOCTTY)
126 bsd_flags |= O_NOCTTY;
127 if (l_flags & LINUX_O_DIRECT)
128 bsd_flags |= O_DIRECT;
129 if (l_flags & LINUX_O_NOFOLLOW)
130 bsd_flags |= O_NOFOLLOW;
131 if (l_flags & LINUX_O_DIRECTORY)
132 bsd_flags |= O_DIRECTORY;
133 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
135 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
138 if (bsd_flags & O_NOCTTY)
142 * XXX In between kern_open() and fget(), another process
143 * having the same filedesc could use that fd without
146 fd = td->td_retval[0];
147 if (fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp) == 0) {
148 if (fp->f_type != DTYPE_VNODE) {
152 sx_slock(&proctree_lock);
154 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
156 sx_sunlock(&proctree_lock);
157 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
158 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
162 sx_sunlock(&proctree_lock);
170 printf(LMSG("open returns error %d"), error);
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);
189 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd,
190 path, args->flags, args->mode);
192 return (linux_common_open(td, dfd, path, args->flags, args->mode));
196 linux_open(struct thread *td, struct linux_open_args *args)
200 if (args->flags & LINUX_O_CREAT)
201 LCONVPATHCREAT(td, args->path, &path);
203 LCONVPATHEXIST(td, args->path, &path);
206 printf(ARGS(open, "%s, 0x%x, 0x%x"),
207 path, args->flags, args->mode);
209 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
213 linux_lseek(struct thread *td, struct linux_lseek_args *args)
218 printf(ARGS(lseek, "%d, %ld, %d"),
219 args->fdes, (long)args->off, args->whence);
221 return (kern_lseek(td, args->fdes, args->off, args->whence));
224 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
226 linux_llseek(struct thread *td, struct linux_llseek_args *args)
233 printf(ARGS(llseek, "%d, %d:%d, %d"),
234 args->fd, args->ohigh, args->olow, args->whence);
236 off = (args->olow) | (((off_t) args->ohigh) << 32);
238 error = kern_lseek(td, args->fd, off, args->whence);
242 error = copyout(td->td_retval, args->res, sizeof(off_t));
246 td->td_retval[0] = 0;
249 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
252 * Note that linux_getdents(2) and linux_getdents64(2) have the same
253 * arguments. They only differ in the definition of struct dirent they
255 * Note that linux_readdir(2) is a special case of linux_getdents(2)
256 * where count is always equals 1, meaning that the buffer is one
257 * dirent-structure in size and that the code can't handle more anyway.
258 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
259 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
264 linux_getdents_error(struct thread *td, int fd, int err)
271 /* Linux return ENOTDIR in case when fd is not a directory. */
272 error = getvnode(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
276 if (vp->v_type != VDIR) {
288 char d_name[LINUX_NAME_MAX + 1];
296 char d_name[LINUX_NAME_MAX + 1];
300 * Linux uses the last byte in the dirent buffer to store d_type,
301 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
303 #define LINUX_RECLEN(namlen) \
304 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
306 #define LINUX_RECLEN64(namlen) \
307 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
310 #define LINUX_DIRBLKSIZ 512
313 * Linux l_dirent is bigger than FreeBSD dirent, thus the buffer size
314 * passed to kern_getdirentries() must be smaller than the one passed
315 * to linux_getdents() by certain factor.
317 #define LINUX_RECLEN_RATIO(X) X * offsetof(struct dirent, d_name) / \
318 offsetof(struct l_dirent, d_name);
319 #define LINUX_RECLEN64_RATIO(X) X * offsetof(struct dirent, d_name) / \
320 offsetof(struct l_dirent64, d_name);
323 linux_getdents(struct thread *td, struct linux_getdents_args *args)
326 caddr_t inp, buf; /* BSD-format */
327 int len, reclen; /* BSD-format */
328 caddr_t outp; /* Linux-format */
329 int resid, linuxreclen; /* Linux-format */
330 caddr_t lbuf; /* Linux-format */
332 struct l_dirent *linux_dirent;
337 if (ldebug(getdents))
338 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
340 buflen = LINUX_RECLEN_RATIO(args->count);
341 buflen = min(buflen, MAXBSIZE);
342 buf = malloc(buflen, M_TEMP, M_WAITOK);
344 error = kern_getdirentries(td, args->fd, buf, buflen,
345 &base, NULL, UIO_SYSSPACE);
347 error = linux_getdents_error(td, args->fd, error);
351 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
353 len = td->td_retval[0];
355 outp = (caddr_t)args->dent;
360 bdp = (struct dirent *) inp;
361 reclen = bdp->d_reclen;
362 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
364 * No more space in the user supplied dirent buffer.
367 if (resid < linuxreclen) {
372 linux_dirent = (struct l_dirent*)lbuf;
373 linux_dirent->d_ino = bdp->d_fileno;
374 linux_dirent->d_off = base + reclen;
375 linux_dirent->d_reclen = linuxreclen;
377 * Copy d_type to last byte of l_dirent buffer
379 lbuf[linuxreclen - 1] = bdp->d_type;
380 strlcpy(linux_dirent->d_name, bdp->d_name,
381 linuxreclen - offsetof(struct l_dirent, d_name)-1);
382 error = copyout(linux_dirent, outp, linuxreclen);
390 retval += linuxreclen;
392 resid -= linuxreclen;
394 td->td_retval[0] = retval;
404 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
407 caddr_t inp, buf; /* BSD-format */
408 int len, reclen; /* BSD-format */
409 caddr_t outp; /* Linux-format */
410 int resid, linuxreclen; /* Linux-format */
411 caddr_t lbuf; /* Linux-format */
413 struct l_dirent64 *linux_dirent64;
418 if (ldebug(getdents64))
419 uprintf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
421 buflen = LINUX_RECLEN64_RATIO(args->count);
422 buflen = min(buflen, MAXBSIZE);
423 buf = malloc(buflen, M_TEMP, M_WAITOK);
425 error = kern_getdirentries(td, args->fd, buf, buflen,
426 &base, NULL, UIO_SYSSPACE);
428 error = linux_getdents_error(td, args->fd, error);
432 lbuf = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
434 len = td->td_retval[0];
436 outp = (caddr_t)args->dirent;
441 bdp = (struct dirent *) inp;
442 reclen = bdp->d_reclen;
443 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
445 * No more space in the user supplied dirent buffer.
448 if (resid < linuxreclen) {
453 linux_dirent64 = (struct l_dirent64*)lbuf;
454 linux_dirent64->d_ino = bdp->d_fileno;
455 linux_dirent64->d_off = base + reclen;
456 linux_dirent64->d_reclen = linuxreclen;
457 linux_dirent64->d_type = bdp->d_type;
458 strlcpy(linux_dirent64->d_name, bdp->d_name,
459 linuxreclen - offsetof(struct l_dirent64, d_name));
460 error = copyout(linux_dirent64, outp, linuxreclen);
468 retval += linuxreclen;
470 resid -= linuxreclen;
472 td->td_retval[0] = retval;
481 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
483 linux_readdir(struct thread *td, struct linux_readdir_args *args)
486 caddr_t buf; /* BSD-format */
487 int linuxreclen; /* Linux-format */
488 caddr_t lbuf; /* Linux-format */
490 struct l_dirent *linux_dirent;
495 printf(ARGS(readdir, "%d, *"), args->fd);
497 buflen = LINUX_RECLEN(LINUX_NAME_MAX);
498 buflen = LINUX_RECLEN_RATIO(buflen);
499 buf = malloc(buflen, M_TEMP, M_WAITOK);
501 error = kern_getdirentries(td, args->fd, buf, buflen,
502 &base, NULL, UIO_SYSSPACE);
504 error = linux_getdents_error(td, args->fd, error);
507 if (td->td_retval[0] == 0)
510 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_TEMP, M_WAITOK | M_ZERO);
512 bdp = (struct dirent *) buf;
513 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
515 linux_dirent = (struct l_dirent*)lbuf;
516 linux_dirent->d_ino = bdp->d_fileno;
517 linux_dirent->d_off = linuxreclen;
518 linux_dirent->d_reclen = bdp->d_namlen;
519 strlcpy(linux_dirent->d_name, bdp->d_name,
520 linuxreclen - offsetof(struct l_dirent, d_name));
521 error = copyout(linux_dirent, args->dent, linuxreclen);
523 td->td_retval[0] = linuxreclen;
530 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
534 * These exist mainly for hooks for doing /compat/linux translation.
538 linux_access(struct thread *td, struct linux_access_args *args)
543 /* linux convention */
544 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
547 LCONVPATHEXIST(td, args->path, &path);
551 printf(ARGS(access, "%s, %d"), path, args->amode);
553 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
561 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
566 /* linux convention */
567 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
570 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
571 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
575 printf(ARGS(access, "%s, %d"), path, args->amode);
578 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode);
585 linux_unlink(struct thread *td, struct linux_unlink_args *args)
591 LCONVPATHEXIST(td, args->path, &path);
595 printf(ARGS(unlink, "%s"), path);
598 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0);
599 if (error == EPERM) {
600 /* Introduce POSIX noncompliant behaviour of Linux */
601 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
603 if (S_ISDIR(st.st_mode))
612 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
618 if (args->flag & ~LINUX_AT_REMOVEDIR)
621 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
622 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
625 if (ldebug(unlinkat))
626 printf(ARGS(unlinkat, "%s"), path);
629 if (args->flag & LINUX_AT_REMOVEDIR)
630 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
632 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
633 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
634 /* Introduce POSIX noncompliant behaviour of Linux */
635 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
636 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
643 linux_chdir(struct thread *td, struct linux_chdir_args *args)
648 LCONVPATHEXIST(td, args->path, &path);
652 printf(ARGS(chdir, "%s"), path);
654 error = kern_chdir(td, path, UIO_SYSSPACE);
660 linux_chmod(struct thread *td, struct linux_chmod_args *args)
665 LCONVPATHEXIST(td, args->path, &path);
669 printf(ARGS(chmod, "%s, %d"), path, args->mode);
671 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE,
678 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
683 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
684 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
687 if (ldebug(fchmodat))
688 printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
691 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
697 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
702 LCONVPATHCREAT(td, args->path, &path);
706 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
708 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
714 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
719 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
720 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
724 printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
726 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
732 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
737 LCONVPATHEXIST(td, args->path, &path);
741 printf(ARGS(rmdir, "%s"), path);
743 error = kern_rmdirat(td, AT_FDCWD, path, UIO_SYSSPACE);
749 linux_rename(struct thread *td, struct linux_rename_args *args)
754 LCONVPATHEXIST(td, args->from, &from);
755 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
756 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
764 printf(ARGS(rename, "%s, %s"), from, to);
766 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
773 linux_renameat(struct thread *td, struct linux_renameat_args *args)
776 int error, olddfd, newdfd;
778 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
779 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
780 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
781 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
782 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
789 if (ldebug(renameat))
790 printf(ARGS(renameat, "%s, %s"), from, to);
792 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
799 linux_symlink(struct thread *td, struct linux_symlink_args *args)
804 LCONVPATHEXIST(td, args->path, &path);
805 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
806 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
814 printf(ARGS(symlink, "%s, %s"), path, to);
816 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
823 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
828 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
829 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd);
830 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
831 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
838 if (ldebug(symlinkat))
839 printf(ARGS(symlinkat, "%s, %s"), path, to);
842 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
849 linux_readlink(struct thread *td, struct linux_readlink_args *args)
854 LCONVPATHEXIST(td, args->name, &name);
857 if (ldebug(readlink))
858 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
861 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
862 args->buf, UIO_USERSPACE, args->count);
868 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
873 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
874 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
877 if (ldebug(readlinkat))
878 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
882 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
883 UIO_USERSPACE, args->bufsiz);
889 linux_truncate(struct thread *td, struct linux_truncate_args *args)
894 LCONVPATHEXIST(td, args->path, &path);
897 if (ldebug(truncate))
898 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
901 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
906 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
908 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
913 LCONVPATHEXIST(td, args->path, &path);
916 if (ldebug(truncate64))
917 printf(ARGS(truncate64, "%s, %jd"), path, args->length);
920 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
924 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
927 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
930 return (kern_ftruncate(td, args->fd, args->length));
934 linux_link(struct thread *td, struct linux_link_args *args)
939 LCONVPATHEXIST(td, args->path, &path);
940 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
941 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
949 printf(ARGS(link, "%s, %s"), path, to);
951 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
959 linux_linkat(struct thread *td, struct linux_linkat_args *args)
962 int error, olddfd, newdfd, follow;
964 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
967 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
968 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
969 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
970 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
971 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
979 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
980 args->newdfd, to, args->flag);
983 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
985 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
992 linux_fdatasync(td, uap)
994 struct linux_fdatasync_args *uap;
997 return (kern_fsync(td, uap->fd, false));
1001 linux_pread(struct thread *td, struct linux_pread_args *uap)
1003 cap_rights_t rights;
1007 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset);
1009 /* This seems to violate POSIX but linux does it */
1010 error = fgetvp(td, uap->fd,
1011 cap_rights_init(&rights, CAP_PREAD), &vp);
1014 if (vp->v_type == VDIR) {
1024 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
1027 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
1031 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1038 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1039 * pos_l and pos_h, respectively, contain the
1040 * low order and high order 32 bits of offset.
1042 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1043 (sizeof(offset) * 4)) | uap->pos_l;
1046 #ifdef COMPAT_LINUX32
1047 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1049 error = copyinuio(uap->vec, uap->vlen, &auio);
1053 error = kern_preadv(td, uap->fd, auio, offset);
1059 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1066 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1067 * pos_l and pos_h, respectively, contain the
1068 * low order and high order 32 bits of offset.
1070 offset = (((off_t)uap->pos_h << (sizeof(offset) * 4)) <<
1071 (sizeof(offset) * 4)) | uap->pos_l;
1074 #ifdef COMPAT_LINUX32
1075 error = linux32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1077 error = copyinuio(uap->vec, uap->vlen, &auio);
1081 error = kern_pwritev(td, uap->fd, auio, offset);
1087 linux_mount(struct thread *td, struct linux_mount_args *args)
1089 char fstypename[MFSNAMELEN];
1090 char mntonname[MNAMELEN], mntfromname[MNAMELEN];
1094 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1098 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1101 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1107 printf(ARGS(mount, "%s, %s, %s"),
1108 fstypename, mntfromname, mntonname);
1111 if (strcmp(fstypename, "ext2") == 0) {
1112 strcpy(fstypename, "ext2fs");
1113 } else if (strcmp(fstypename, "proc") == 0) {
1114 strcpy(fstypename, "linprocfs");
1115 } else if (strcmp(fstypename, "vfat") == 0) {
1116 strcpy(fstypename, "msdosfs");
1121 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1123 * Linux SYNC flag is not included; the closest equivalent
1124 * FreeBSD has is !ASYNC, which is our default.
1126 if (args->rwflag & LINUX_MS_RDONLY)
1127 fsflags |= MNT_RDONLY;
1128 if (args->rwflag & LINUX_MS_NOSUID)
1129 fsflags |= MNT_NOSUID;
1130 if (args->rwflag & LINUX_MS_NOEXEC)
1131 fsflags |= MNT_NOEXEC;
1132 if (args->rwflag & LINUX_MS_REMOUNT)
1133 fsflags |= MNT_UPDATE;
1136 error = kernel_vmount(fsflags,
1137 "fstype", fstypename,
1138 "fspath", mntonname,
1139 "from", mntfromname,
1144 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1146 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1148 struct linux_umount_args args2;
1150 args2.path = args->path;
1152 return (linux_umount(td, &args2));
1154 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1157 linux_umount(struct thread *td, struct linux_umount_args *args)
1159 struct unmount_args bsd;
1161 bsd.path = args->path;
1162 bsd.flags = args->flags; /* XXX correct? */
1163 return (sys_unmount(td, &bsd));
1167 * fcntl family of syscalls
1177 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1183 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1185 switch (linux_flock->l_type) {
1187 bsd_flock->l_type = F_RDLCK;
1190 bsd_flock->l_type = F_WRLCK;
1193 bsd_flock->l_type = F_UNLCK;
1196 bsd_flock->l_type = -1;
1199 bsd_flock->l_whence = linux_flock->l_whence;
1200 bsd_flock->l_start = (off_t)linux_flock->l_start;
1201 bsd_flock->l_len = (off_t)linux_flock->l_len;
1202 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1203 bsd_flock->l_sysid = 0;
1207 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1209 switch (bsd_flock->l_type) {
1211 linux_flock->l_type = LINUX_F_RDLCK;
1214 linux_flock->l_type = LINUX_F_WRLCK;
1217 linux_flock->l_type = LINUX_F_UNLCK;
1220 linux_flock->l_whence = bsd_flock->l_whence;
1221 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1222 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1223 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1226 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1234 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1240 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1242 switch (linux_flock->l_type) {
1244 bsd_flock->l_type = F_RDLCK;
1247 bsd_flock->l_type = F_WRLCK;
1250 bsd_flock->l_type = F_UNLCK;
1253 bsd_flock->l_type = -1;
1256 bsd_flock->l_whence = linux_flock->l_whence;
1257 bsd_flock->l_start = (off_t)linux_flock->l_start;
1258 bsd_flock->l_len = (off_t)linux_flock->l_len;
1259 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1260 bsd_flock->l_sysid = 0;
1264 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1266 switch (bsd_flock->l_type) {
1268 linux_flock->l_type = LINUX_F_RDLCK;
1271 linux_flock->l_type = LINUX_F_WRLCK;
1274 linux_flock->l_type = LINUX_F_UNLCK;
1277 linux_flock->l_whence = bsd_flock->l_whence;
1278 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1279 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1280 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1282 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1285 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1287 struct l_flock linux_flock;
1288 struct flock bsd_flock;
1289 cap_rights_t rights;
1294 switch (args->cmd) {
1296 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1299 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1302 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1305 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1306 result = td->td_retval[0];
1307 td->td_retval[0] = 0;
1308 if (result & O_RDONLY)
1309 td->td_retval[0] |= LINUX_O_RDONLY;
1310 if (result & O_WRONLY)
1311 td->td_retval[0] |= LINUX_O_WRONLY;
1312 if (result & O_RDWR)
1313 td->td_retval[0] |= LINUX_O_RDWR;
1314 if (result & O_NDELAY)
1315 td->td_retval[0] |= LINUX_O_NONBLOCK;
1316 if (result & O_APPEND)
1317 td->td_retval[0] |= LINUX_O_APPEND;
1318 if (result & O_FSYNC)
1319 td->td_retval[0] |= LINUX_O_SYNC;
1320 if (result & O_ASYNC)
1321 td->td_retval[0] |= LINUX_FASYNC;
1322 #ifdef LINUX_O_NOFOLLOW
1323 if (result & O_NOFOLLOW)
1324 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1326 #ifdef LINUX_O_DIRECT
1327 if (result & O_DIRECT)
1328 td->td_retval[0] |= LINUX_O_DIRECT;
1334 if (args->arg & LINUX_O_NDELAY)
1336 if (args->arg & LINUX_O_APPEND)
1338 if (args->arg & LINUX_O_SYNC)
1340 if (args->arg & LINUX_FASYNC)
1342 #ifdef LINUX_O_NOFOLLOW
1343 if (args->arg & LINUX_O_NOFOLLOW)
1346 #ifdef LINUX_O_DIRECT
1347 if (args->arg & LINUX_O_DIRECT)
1350 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1353 error = copyin((void *)args->arg, &linux_flock,
1354 sizeof(linux_flock));
1357 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1358 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1361 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1362 return (copyout(&linux_flock, (void *)args->arg,
1363 sizeof(linux_flock)));
1366 error = copyin((void *)args->arg, &linux_flock,
1367 sizeof(linux_flock));
1370 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1371 return (kern_fcntl(td, args->fd, F_SETLK,
1372 (intptr_t)&bsd_flock));
1374 case LINUX_F_SETLKW:
1375 error = copyin((void *)args->arg, &linux_flock,
1376 sizeof(linux_flock));
1379 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1380 return (kern_fcntl(td, args->fd, F_SETLKW,
1381 (intptr_t)&bsd_flock));
1383 case LINUX_F_GETOWN:
1384 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1386 case LINUX_F_SETOWN:
1388 * XXX some Linux applications depend on F_SETOWN having no
1389 * significant effect for pipes (SIGIO is not delivered for
1390 * pipes under Linux-2.2.35 at least).
1392 error = fget(td, args->fd,
1393 cap_rights_init(&rights, CAP_FCNTL), &fp);
1396 if (fp->f_type == DTYPE_PIPE) {
1402 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1404 case LINUX_F_DUPFD_CLOEXEC:
1405 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1412 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1417 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1420 return (fcntl_common(td, args));
1423 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1425 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1427 struct l_flock64 linux_flock;
1428 struct flock bsd_flock;
1429 struct linux_fcntl_args fcntl_args;
1433 if (ldebug(fcntl64))
1434 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1437 switch (args->cmd) {
1438 case LINUX_F_GETLK64:
1439 error = copyin((void *)args->arg, &linux_flock,
1440 sizeof(linux_flock));
1443 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1444 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1447 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1448 return (copyout(&linux_flock, (void *)args->arg,
1449 sizeof(linux_flock)));
1451 case LINUX_F_SETLK64:
1452 error = copyin((void *)args->arg, &linux_flock,
1453 sizeof(linux_flock));
1456 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1457 return (kern_fcntl(td, args->fd, F_SETLK,
1458 (intptr_t)&bsd_flock));
1460 case LINUX_F_SETLKW64:
1461 error = copyin((void *)args->arg, &linux_flock,
1462 sizeof(linux_flock));
1465 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1466 return (kern_fcntl(td, args->fd, F_SETLKW,
1467 (intptr_t)&bsd_flock));
1470 fcntl_args.fd = args->fd;
1471 fcntl_args.cmd = args->cmd;
1472 fcntl_args.arg = args->arg;
1473 return (fcntl_common(td, &fcntl_args));
1475 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1478 linux_chown(struct thread *td, struct linux_chown_args *args)
1483 LCONVPATHEXIST(td, args->path, &path);
1487 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1489 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1496 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1499 int error, dfd, flag;
1501 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1504 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1505 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1508 if (ldebug(fchownat))
1509 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1512 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1513 AT_SYMLINK_NOFOLLOW;
1514 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1521 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1526 LCONVPATHEXIST(td, args->path, &path);
1530 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1532 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1533 args->gid, AT_SYMLINK_NOFOLLOW);
1539 convert_fadvice(int advice)
1542 case LINUX_POSIX_FADV_NORMAL:
1543 return (POSIX_FADV_NORMAL);
1544 case LINUX_POSIX_FADV_RANDOM:
1545 return (POSIX_FADV_RANDOM);
1546 case LINUX_POSIX_FADV_SEQUENTIAL:
1547 return (POSIX_FADV_SEQUENTIAL);
1548 case LINUX_POSIX_FADV_WILLNEED:
1549 return (POSIX_FADV_WILLNEED);
1550 case LINUX_POSIX_FADV_DONTNEED:
1551 return (POSIX_FADV_DONTNEED);
1552 case LINUX_POSIX_FADV_NOREUSE:
1553 return (POSIX_FADV_NOREUSE);
1560 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1564 advice = convert_fadvice(args->advice);
1567 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1571 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1573 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1577 advice = convert_fadvice(args->advice);
1580 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1583 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1586 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1593 printf(ARGS(pipe, "*"));
1596 error = kern_pipe(td, fildes, 0, NULL, NULL);
1600 error = copyout(fildes, args->pipefds, sizeof(fildes));
1602 (void)kern_close(td, fildes[0]);
1603 (void)kern_close(td, fildes[1]);
1610 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1617 printf(ARGS(pipe2, "*, %d"), args->flags);
1620 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1624 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1625 flags |= O_NONBLOCK;
1626 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1628 error = kern_pipe(td, fildes, flags, NULL, NULL);
1632 error = copyout(fildes, args->pipefds, sizeof(fildes));
1634 (void)kern_close(td, fildes[0]);
1635 (void)kern_close(td, fildes[1]);
1642 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1647 if (args->oldfd == args->newfd)
1649 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1651 if (args->flags & LINUX_O_CLOEXEC)
1652 cmd = F_DUP2FD_CLOEXEC;
1656 newfd = args->newfd;
1657 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1661 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1665 * We emulate only posix_fallocate system call for which
1668 if (args->mode != 0)
1671 return (kern_posix_fallocate(td, args->fd, args->offset,