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>
56 #include <security/mac/mac_framework.h>
59 #include <machine/../linux32/linux.h>
60 #include <machine/../linux32/linux32_proto.h>
62 #include <machine/../linux/linux.h>
63 #include <machine/../linux/linux_proto.h>
65 #include <compat/linux/linux_misc.h>
66 #include <compat/linux/linux_util.h>
67 #include <compat/linux/linux_file.h>
70 linux_creat(struct thread *td, struct linux_creat_args *args)
75 LCONVPATHEXIST(td, args->path, &path);
79 printf(ARGS(creat, "%s, %d"), path, args->mode);
81 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
82 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
89 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);
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);
207 printf(ARGS(open, "%s, 0x%x, 0x%x"),
208 path, args->flags, args->mode);
211 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
215 linux_lseek(struct thread *td, struct linux_lseek_args *args)
218 struct lseek_args /* {
228 printf(ARGS(lseek, "%d, %ld, %d"),
229 args->fdes, (long)args->off, args->whence);
231 tmp_args.fd = args->fdes;
232 tmp_args.offset = (off_t)args->off;
233 tmp_args.whence = args->whence;
234 error = sys_lseek(td, &tmp_args);
238 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
240 linux_llseek(struct thread *td, struct linux_llseek_args *args)
242 struct lseek_args bsd_args;
248 printf(ARGS(llseek, "%d, %d:%d, %d"),
249 args->fd, args->ohigh, args->olow, args->whence);
251 off = (args->olow) | (((off_t) args->ohigh) << 32);
253 bsd_args.fd = args->fd;
254 bsd_args.offset = off;
255 bsd_args.whence = args->whence;
257 if ((error = sys_lseek(td, &bsd_args)))
260 if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
263 td->td_retval[0] = 0;
268 linux_readdir(struct thread *td, struct linux_readdir_args *args)
270 struct linux_getdents_args lda;
273 lda.dent = args->dent;
275 return linux_getdents(td, &lda);
277 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
280 * Note that linux_getdents(2) and linux_getdents64(2) have the same
281 * arguments. They only differ in the definition of struct dirent they
282 * operate on. We use this to common the code, with the exception of
283 * accessing struct dirent. Note that linux_readdir(2) is implemented
284 * by means of linux_getdents(2). In this case we never operate on
285 * struct dirent64 and thus don't need to handle it...
292 char d_name[LINUX_NAME_MAX + 1];
300 char d_name[LINUX_NAME_MAX + 1];
304 * Linux uses the last byte in the dirent buffer to store d_type,
305 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
307 #define LINUX_RECLEN(namlen) \
308 roundup((offsetof(struct l_dirent, d_name) + (namlen) + 2), \
311 #define LINUX_RECLEN64(namlen) \
312 roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1), \
315 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \
316 LINUX_RECLEN64(LINUX_NAME_MAX))
317 #define LINUX_DIRBLKSIZ 512
320 getdents_common(struct thread *td, struct linux_getdents64_args *args,
325 caddr_t inp, buf; /* BSD-format */
326 int len, reclen; /* BSD-format */
327 caddr_t outp; /* Linux-format */
328 int resid, linuxreclen=0; /* Linux-format */
329 caddr_t lbuf; /* Linux-format */
335 struct l_dirent *linux_dirent;
336 struct l_dirent64 *linux_dirent64;
337 int buflen, error, eofflag, nbytes, justone;
338 u_long *cookies = NULL, *cookiep;
341 nbytes = args->count;
343 /* readdir(2) case. Always struct dirent. */
346 nbytes = sizeof(*linux_dirent);
351 error = getvnode(td, args->fd, cap_rights_init(&rights, CAP_READ), &fp);
355 if ((fp->f_flag & FREAD) == 0) {
360 off = foffset_lock(fp, 0);
362 if (vp->v_type != VDIR) {
363 foffset_unlock(fp, off, 0);
369 buflen = max(LINUX_DIRBLKSIZ, nbytes);
370 buflen = min(buflen, MAXBSIZE);
371 buf = malloc(buflen, M_LINUX, M_WAITOK);
372 lbuf = malloc(LINUX_MAXRECLEN, M_LINUX, M_WAITOK | M_ZERO);
373 vn_lock(vp, LK_SHARED | LK_RETRY);
376 aiov.iov_len = buflen;
377 auio.uio_iov = &aiov;
379 auio.uio_rw = UIO_READ;
380 auio.uio_segflg = UIO_SYSSPACE;
382 auio.uio_resid = buflen;
383 auio.uio_offset = off;
387 * Do directory search MAC check using non-cached credentials.
389 if ((error = mac_vnode_check_readdir(td->td_ucred, vp)))
392 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
397 outp = (caddr_t)args->dirent;
399 if ((len = buflen - auio.uio_resid) <= 0)
406 * When using cookies, the vfs has the option of reading from
407 * a different offset than that supplied (UFS truncates the
408 * offset to a block boundary to make sure that it never reads
409 * partway through a directory entry, even if the directory
410 * has been compacted).
412 while (len > 0 && ncookies > 0 && *cookiep <= off) {
413 bdp = (struct dirent *) inp;
414 len -= bdp->d_reclen;
415 inp += bdp->d_reclen;
422 if (cookiep && ncookies == 0)
424 bdp = (struct dirent *) inp;
425 reclen = bdp->d_reclen;
431 if (bdp->d_fileno == 0) {
443 linuxreclen = (is64bit)
444 ? LINUX_RECLEN64(bdp->d_namlen)
445 : LINUX_RECLEN(bdp->d_namlen);
447 if (reclen > len || resid < linuxreclen) {
453 /* readdir(2) case. */
454 linux_dirent = (struct l_dirent*)lbuf;
455 linux_dirent->d_ino = bdp->d_fileno;
456 linux_dirent->d_off = (l_off_t)linuxreclen;
457 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen;
458 strlcpy(linux_dirent->d_name, bdp->d_name,
459 linuxreclen - offsetof(struct l_dirent, d_name));
460 error = copyout(linux_dirent, outp, linuxreclen);
463 linux_dirent64 = (struct l_dirent64*)lbuf;
464 linux_dirent64->d_ino = bdp->d_fileno;
465 linux_dirent64->d_off = (cookiep)
467 : (l_off_t)(off + reclen);
468 linux_dirent64->d_reclen = (l_ushort)linuxreclen;
469 linux_dirent64->d_type = bdp->d_type;
470 strlcpy(linux_dirent64->d_name, bdp->d_name,
471 linuxreclen - offsetof(struct l_dirent64, d_name));
472 error = copyout(linux_dirent64, outp, linuxreclen);
473 } else if (!justone) {
474 linux_dirent = (struct l_dirent*)lbuf;
475 linux_dirent->d_ino = bdp->d_fileno;
476 linux_dirent->d_off = (cookiep)
478 : (l_off_t)(off + reclen);
479 linux_dirent->d_reclen = (l_ushort)linuxreclen;
481 * Copy d_type to last byte of l_dirent buffer
483 lbuf[linuxreclen-1] = bdp->d_type;
484 strlcpy(linux_dirent->d_name, bdp->d_name,
485 linuxreclen - offsetof(struct l_dirent, d_name)-1);
486 error = copyout(linux_dirent, outp, linuxreclen);
500 resid -= linuxreclen;
506 if (outp == (caddr_t)args->dirent) {
512 nbytes = resid + linuxreclen;
515 td->td_retval[0] = nbytes - resid;
518 free(cookies, M_TEMP);
521 foffset_unlock(fp, off, 0);
529 linux_getdents(struct thread *td, struct linux_getdents_args *args)
533 if (ldebug(getdents))
534 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
537 return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
541 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
545 if (ldebug(getdents64))
546 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
549 return (getdents_common(td, args, 1));
553 * These exist mainly for hooks for doing /compat/linux translation.
557 linux_access(struct thread *td, struct linux_access_args *args)
562 /* linux convention */
563 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
566 LCONVPATHEXIST(td, args->path, &path);
570 printf(ARGS(access, "%s, %d"), path, args->amode);
572 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
580 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
585 /* linux convention */
586 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
589 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
590 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
594 printf(ARGS(access, "%s, %d"), path, args->amode);
597 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode);
604 linux_unlink(struct thread *td, struct linux_unlink_args *args)
610 LCONVPATHEXIST(td, args->path, &path);
614 printf(ARGS(unlink, "%s"), path);
617 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0);
618 if (error == EPERM) {
619 /* Introduce POSIX noncompliant behaviour of Linux */
620 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
622 if (S_ISDIR(st.st_mode))
631 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
637 if (args->flag & ~LINUX_AT_REMOVEDIR)
640 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
641 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
644 if (ldebug(unlinkat))
645 printf(ARGS(unlinkat, "%s"), path);
648 if (args->flag & LINUX_AT_REMOVEDIR)
649 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
651 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
652 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
653 /* Introduce POSIX noncompliant behaviour of Linux */
654 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
655 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
662 linux_chdir(struct thread *td, struct linux_chdir_args *args)
667 LCONVPATHEXIST(td, args->path, &path);
671 printf(ARGS(chdir, "%s"), path);
673 error = kern_chdir(td, path, UIO_SYSSPACE);
679 linux_chmod(struct thread *td, struct linux_chmod_args *args)
684 LCONVPATHEXIST(td, args->path, &path);
688 printf(ARGS(chmod, "%s, %d"), path, args->mode);
690 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE,
697 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
702 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
703 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
706 if (ldebug(fchmodat))
707 printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
710 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
716 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
721 LCONVPATHCREAT(td, args->path, &path);
725 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
727 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
733 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
738 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
739 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
743 printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
745 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
751 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
756 LCONVPATHEXIST(td, args->path, &path);
760 printf(ARGS(rmdir, "%s"), path);
762 error = kern_rmdirat(td, AT_FDCWD, path, UIO_SYSSPACE);
768 linux_rename(struct thread *td, struct linux_rename_args *args)
773 LCONVPATHEXIST(td, args->from, &from);
774 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
775 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
783 printf(ARGS(rename, "%s, %s"), from, to);
785 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
792 linux_renameat(struct thread *td, struct linux_renameat_args *args)
795 int error, olddfd, newdfd;
797 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
798 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
799 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
800 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
801 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
808 if (ldebug(renameat))
809 printf(ARGS(renameat, "%s, %s"), from, to);
811 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
818 linux_symlink(struct thread *td, struct linux_symlink_args *args)
823 LCONVPATHEXIST(td, args->path, &path);
824 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
825 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
833 printf(ARGS(symlink, "%s, %s"), path, to);
835 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
842 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
847 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
848 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd);
849 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
850 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
857 if (ldebug(symlinkat))
858 printf(ARGS(symlinkat, "%s, %s"), path, to);
861 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
868 linux_readlink(struct thread *td, struct linux_readlink_args *args)
873 LCONVPATHEXIST(td, args->name, &name);
876 if (ldebug(readlink))
877 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
880 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
881 args->buf, UIO_USERSPACE, args->count);
887 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
892 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
893 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
896 if (ldebug(readlinkat))
897 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
901 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
902 UIO_USERSPACE, args->bufsiz);
908 linux_truncate(struct thread *td, struct linux_truncate_args *args)
913 LCONVPATHEXIST(td, args->path, &path);
916 if (ldebug(truncate))
917 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
920 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
925 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
927 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
932 LCONVPATHEXIST(td, args->path, &path);
935 if (ldebug(truncate64))
936 printf(ARGS(truncate64, "%s, %jd"), path, args->length);
939 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
943 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
946 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
948 struct ftruncate_args /* {
955 nuap.length = args->length;
956 return (sys_ftruncate(td, &nuap));
960 linux_link(struct thread *td, struct linux_link_args *args)
965 LCONVPATHEXIST(td, args->path, &path);
966 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
967 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
975 printf(ARGS(link, "%s, %s"), path, to);
977 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
985 linux_linkat(struct thread *td, struct linux_linkat_args *args)
988 int error, olddfd, newdfd, follow;
990 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
993 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
994 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
995 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
996 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
997 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
1005 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
1006 args->newdfd, to, args->flag);
1009 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
1011 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
1018 linux_fdatasync(td, uap)
1020 struct linux_fdatasync_args *uap;
1022 struct fsync_args bsd;
1025 return sys_fsync(td, &bsd);
1029 linux_pread(td, uap)
1031 struct linux_pread_args *uap;
1033 struct pread_args bsd;
1034 cap_rights_t rights;
1040 bsd.nbyte = uap->nbyte;
1041 bsd.offset = uap->offset;
1043 error = sys_pread(td, &bsd);
1046 /* This seems to violate POSIX but linux does it */
1047 error = fgetvp(td, uap->fd,
1048 cap_rights_init(&rights, CAP_PREAD), &vp);
1051 if (vp->v_type == VDIR) {
1062 linux_pwrite(td, uap)
1064 struct linux_pwrite_args *uap;
1066 struct pwrite_args bsd;
1070 bsd.nbyte = uap->nbyte;
1071 bsd.offset = uap->offset;
1072 return sys_pwrite(td, &bsd);
1076 linux_mount(struct thread *td, struct linux_mount_args *args)
1078 char fstypename[MFSNAMELEN];
1079 char mntonname[MNAMELEN], mntfromname[MNAMELEN];
1083 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1087 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1090 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1096 printf(ARGS(mount, "%s, %s, %s"),
1097 fstypename, mntfromname, mntonname);
1100 if (strcmp(fstypename, "ext2") == 0) {
1101 strcpy(fstypename, "ext2fs");
1102 } else if (strcmp(fstypename, "proc") == 0) {
1103 strcpy(fstypename, "linprocfs");
1104 } else if (strcmp(fstypename, "vfat") == 0) {
1105 strcpy(fstypename, "msdosfs");
1110 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1112 * Linux SYNC flag is not included; the closest equivalent
1113 * FreeBSD has is !ASYNC, which is our default.
1115 if (args->rwflag & LINUX_MS_RDONLY)
1116 fsflags |= MNT_RDONLY;
1117 if (args->rwflag & LINUX_MS_NOSUID)
1118 fsflags |= MNT_NOSUID;
1119 if (args->rwflag & LINUX_MS_NOEXEC)
1120 fsflags |= MNT_NOEXEC;
1121 if (args->rwflag & LINUX_MS_REMOUNT)
1122 fsflags |= MNT_UPDATE;
1125 error = kernel_vmount(fsflags,
1126 "fstype", fstypename,
1127 "fspath", mntonname,
1128 "from", mntfromname,
1133 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1135 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1137 struct linux_umount_args args2;
1139 args2.path = args->path;
1141 return (linux_umount(td, &args2));
1143 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1146 linux_umount(struct thread *td, struct linux_umount_args *args)
1148 struct unmount_args bsd;
1150 bsd.path = args->path;
1151 bsd.flags = args->flags; /* XXX correct? */
1152 return (sys_unmount(td, &bsd));
1156 * fcntl family of syscalls
1166 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1172 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1174 switch (linux_flock->l_type) {
1176 bsd_flock->l_type = F_RDLCK;
1179 bsd_flock->l_type = F_WRLCK;
1182 bsd_flock->l_type = F_UNLCK;
1185 bsd_flock->l_type = -1;
1188 bsd_flock->l_whence = linux_flock->l_whence;
1189 bsd_flock->l_start = (off_t)linux_flock->l_start;
1190 bsd_flock->l_len = (off_t)linux_flock->l_len;
1191 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1192 bsd_flock->l_sysid = 0;
1196 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1198 switch (bsd_flock->l_type) {
1200 linux_flock->l_type = LINUX_F_RDLCK;
1203 linux_flock->l_type = LINUX_F_WRLCK;
1206 linux_flock->l_type = LINUX_F_UNLCK;
1209 linux_flock->l_whence = bsd_flock->l_whence;
1210 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1211 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1212 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1215 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1223 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1229 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1231 switch (linux_flock->l_type) {
1233 bsd_flock->l_type = F_RDLCK;
1236 bsd_flock->l_type = F_WRLCK;
1239 bsd_flock->l_type = F_UNLCK;
1242 bsd_flock->l_type = -1;
1245 bsd_flock->l_whence = linux_flock->l_whence;
1246 bsd_flock->l_start = (off_t)linux_flock->l_start;
1247 bsd_flock->l_len = (off_t)linux_flock->l_len;
1248 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1249 bsd_flock->l_sysid = 0;
1253 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1255 switch (bsd_flock->l_type) {
1257 linux_flock->l_type = LINUX_F_RDLCK;
1260 linux_flock->l_type = LINUX_F_WRLCK;
1263 linux_flock->l_type = LINUX_F_UNLCK;
1266 linux_flock->l_whence = bsd_flock->l_whence;
1267 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1268 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1269 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1271 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1274 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1276 struct l_flock linux_flock;
1277 struct flock bsd_flock;
1278 cap_rights_t rights;
1283 switch (args->cmd) {
1285 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1288 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1291 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1294 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1295 result = td->td_retval[0];
1296 td->td_retval[0] = 0;
1297 if (result & O_RDONLY)
1298 td->td_retval[0] |= LINUX_O_RDONLY;
1299 if (result & O_WRONLY)
1300 td->td_retval[0] |= LINUX_O_WRONLY;
1301 if (result & O_RDWR)
1302 td->td_retval[0] |= LINUX_O_RDWR;
1303 if (result & O_NDELAY)
1304 td->td_retval[0] |= LINUX_O_NONBLOCK;
1305 if (result & O_APPEND)
1306 td->td_retval[0] |= LINUX_O_APPEND;
1307 if (result & O_FSYNC)
1308 td->td_retval[0] |= LINUX_O_SYNC;
1309 if (result & O_ASYNC)
1310 td->td_retval[0] |= LINUX_FASYNC;
1311 #ifdef LINUX_O_NOFOLLOW
1312 if (result & O_NOFOLLOW)
1313 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1315 #ifdef LINUX_O_DIRECT
1316 if (result & O_DIRECT)
1317 td->td_retval[0] |= LINUX_O_DIRECT;
1323 if (args->arg & LINUX_O_NDELAY)
1325 if (args->arg & LINUX_O_APPEND)
1327 if (args->arg & LINUX_O_SYNC)
1329 if (args->arg & LINUX_FASYNC)
1331 #ifdef LINUX_O_NOFOLLOW
1332 if (args->arg & LINUX_O_NOFOLLOW)
1335 #ifdef LINUX_O_DIRECT
1336 if (args->arg & LINUX_O_DIRECT)
1339 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1342 error = copyin((void *)args->arg, &linux_flock,
1343 sizeof(linux_flock));
1346 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1347 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1350 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1351 return (copyout(&linux_flock, (void *)args->arg,
1352 sizeof(linux_flock)));
1355 error = copyin((void *)args->arg, &linux_flock,
1356 sizeof(linux_flock));
1359 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1360 return (kern_fcntl(td, args->fd, F_SETLK,
1361 (intptr_t)&bsd_flock));
1363 case LINUX_F_SETLKW:
1364 error = copyin((void *)args->arg, &linux_flock,
1365 sizeof(linux_flock));
1368 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1369 return (kern_fcntl(td, args->fd, F_SETLKW,
1370 (intptr_t)&bsd_flock));
1372 case LINUX_F_GETOWN:
1373 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1375 case LINUX_F_SETOWN:
1377 * XXX some Linux applications depend on F_SETOWN having no
1378 * significant effect for pipes (SIGIO is not delivered for
1379 * pipes under Linux-2.2.35 at least).
1381 error = fget(td, args->fd,
1382 cap_rights_init(&rights, CAP_FCNTL), &fp);
1385 if (fp->f_type == DTYPE_PIPE) {
1391 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1393 case LINUX_F_DUPFD_CLOEXEC:
1394 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1401 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1406 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1409 return (fcntl_common(td, args));
1412 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1414 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1416 struct l_flock64 linux_flock;
1417 struct flock bsd_flock;
1418 struct linux_fcntl_args fcntl_args;
1422 if (ldebug(fcntl64))
1423 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1426 switch (args->cmd) {
1427 case LINUX_F_GETLK64:
1428 error = copyin((void *)args->arg, &linux_flock,
1429 sizeof(linux_flock));
1432 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1433 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1436 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1437 return (copyout(&linux_flock, (void *)args->arg,
1438 sizeof(linux_flock)));
1440 case LINUX_F_SETLK64:
1441 error = copyin((void *)args->arg, &linux_flock,
1442 sizeof(linux_flock));
1445 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1446 return (kern_fcntl(td, args->fd, F_SETLK,
1447 (intptr_t)&bsd_flock));
1449 case LINUX_F_SETLKW64:
1450 error = copyin((void *)args->arg, &linux_flock,
1451 sizeof(linux_flock));
1454 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1455 return (kern_fcntl(td, args->fd, F_SETLKW,
1456 (intptr_t)&bsd_flock));
1459 fcntl_args.fd = args->fd;
1460 fcntl_args.cmd = args->cmd;
1461 fcntl_args.arg = args->arg;
1462 return (fcntl_common(td, &fcntl_args));
1464 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1467 linux_chown(struct thread *td, struct linux_chown_args *args)
1472 LCONVPATHEXIST(td, args->path, &path);
1476 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1478 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1485 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1488 int error, dfd, flag;
1490 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1493 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1494 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1497 if (ldebug(fchownat))
1498 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1501 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1502 AT_SYMLINK_NOFOLLOW;
1503 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1510 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1515 LCONVPATHEXIST(td, args->path, &path);
1519 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1521 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1522 args->gid, AT_SYMLINK_NOFOLLOW);
1528 convert_fadvice(int advice)
1531 case LINUX_POSIX_FADV_NORMAL:
1532 return (POSIX_FADV_NORMAL);
1533 case LINUX_POSIX_FADV_RANDOM:
1534 return (POSIX_FADV_RANDOM);
1535 case LINUX_POSIX_FADV_SEQUENTIAL:
1536 return (POSIX_FADV_SEQUENTIAL);
1537 case LINUX_POSIX_FADV_WILLNEED:
1538 return (POSIX_FADV_WILLNEED);
1539 case LINUX_POSIX_FADV_DONTNEED:
1540 return (POSIX_FADV_DONTNEED);
1541 case LINUX_POSIX_FADV_NOREUSE:
1542 return (POSIX_FADV_NOREUSE);
1549 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1553 advice = convert_fadvice(args->advice);
1556 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1560 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1562 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1566 advice = convert_fadvice(args->advice);
1569 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1572 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1575 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1582 printf(ARGS(pipe, "*"));
1585 error = kern_pipe(td, fildes, 0, NULL, NULL);
1589 /* XXX: Close descriptors on error. */
1590 return (copyout(fildes, args->pipefds, sizeof(fildes)));
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 /* XXX: Close descriptors on error. */
1617 return (copyout(fildes, args->pipefds, sizeof(fildes)));
1621 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1626 if (args->oldfd == args->newfd)
1628 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1630 if (args->flags & LINUX_O_CLOEXEC)
1631 cmd = F_DUP2FD_CLOEXEC;
1635 newfd = args->newfd;
1636 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1640 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1644 * We emulate only posix_fallocate system call for which
1647 if (args->mode != 0)
1650 return (kern_posix_fallocate(td, args->fd, args->offset,