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);
78 printf(ARGS(creat, "%s, %d"), path, args->mode);
80 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
81 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
88 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode)
91 struct proc *p = td->td_proc;
97 switch (l_flags & LINUX_O_ACCMODE) {
99 bsd_flags |= O_WRONLY;
105 bsd_flags |= O_RDONLY;
107 if (l_flags & LINUX_O_NDELAY)
108 bsd_flags |= O_NONBLOCK;
109 if (l_flags & LINUX_O_APPEND)
110 bsd_flags |= O_APPEND;
111 if (l_flags & LINUX_O_SYNC)
112 bsd_flags |= O_FSYNC;
113 if (l_flags & LINUX_O_NONBLOCK)
114 bsd_flags |= O_NONBLOCK;
115 if (l_flags & LINUX_FASYNC)
116 bsd_flags |= O_ASYNC;
117 if (l_flags & LINUX_O_CREAT)
118 bsd_flags |= O_CREAT;
119 if (l_flags & LINUX_O_TRUNC)
120 bsd_flags |= O_TRUNC;
121 if (l_flags & LINUX_O_EXCL)
123 if (l_flags & LINUX_O_NOCTTY)
124 bsd_flags |= O_NOCTTY;
125 if (l_flags & LINUX_O_DIRECT)
126 bsd_flags |= O_DIRECT;
127 if (l_flags & LINUX_O_NOFOLLOW)
128 bsd_flags |= O_NOFOLLOW;
129 if (l_flags & LINUX_O_DIRECTORY)
130 bsd_flags |= O_DIRECTORY;
131 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
133 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
136 if (bsd_flags & O_NOCTTY)
140 * XXX In between kern_open() and fget(), another process
141 * having the same filedesc could use that fd without
144 fd = td->td_retval[0];
145 if (fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp) == 0) {
146 if (fp->f_type != DTYPE_VNODE) {
150 sx_slock(&proctree_lock);
152 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
154 sx_sunlock(&proctree_lock);
155 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
156 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
160 sx_sunlock(&proctree_lock);
168 printf(LMSG("open returns error %d"), error);
175 linux_openat(struct thread *td, struct linux_openat_args *args)
180 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
181 if (args->flags & LINUX_O_CREAT)
182 LCONVPATH_AT(td, args->filename, &path, 1, dfd);
184 LCONVPATH_AT(td, args->filename, &path, 0, dfd);
187 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd,
188 path, args->flags, args->mode);
190 return (linux_common_open(td, dfd, path, args->flags, args->mode));
194 linux_open(struct thread *td, struct linux_open_args *args)
198 if (args->flags & LINUX_O_CREAT)
199 LCONVPATHCREAT(td, args->path, &path);
201 LCONVPATHEXIST(td, args->path, &path);
204 printf(ARGS(open, "%s, 0x%x, 0x%x"),
205 path, args->flags, args->mode);
207 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
211 linux_lseek(struct thread *td, struct linux_lseek_args *args)
213 struct lseek_args /* {
223 printf(ARGS(lseek, "%d, %ld, %d"),
224 args->fdes, (long)args->off, args->whence);
226 tmp_args.fd = args->fdes;
227 tmp_args.offset = (off_t)args->off;
228 tmp_args.whence = args->whence;
229 error = sys_lseek(td, &tmp_args);
233 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
235 linux_llseek(struct thread *td, struct linux_llseek_args *args)
237 struct lseek_args bsd_args;
243 printf(ARGS(llseek, "%d, %d:%d, %d"),
244 args->fd, args->ohigh, args->olow, args->whence);
246 off = (args->olow) | (((off_t) args->ohigh) << 32);
248 bsd_args.fd = args->fd;
249 bsd_args.offset = off;
250 bsd_args.whence = args->whence;
252 if ((error = sys_lseek(td, &bsd_args)))
255 if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
258 td->td_retval[0] = 0;
263 linux_readdir(struct thread *td, struct linux_readdir_args *args)
265 struct linux_getdents_args lda;
268 lda.dent = args->dent;
270 return (linux_getdents(td, &lda));
272 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
275 * Note that linux_getdents(2) and linux_getdents64(2) have the same
276 * arguments. They only differ in the definition of struct dirent they
277 * operate on. We use this to common the code, with the exception of
278 * accessing struct dirent. Note that linux_readdir(2) is implemented
279 * by means of linux_getdents(2). In this case we never operate on
280 * struct dirent64 and thus don't need to handle it...
287 char d_name[LINUX_NAME_MAX + 1];
295 char d_name[LINUX_NAME_MAX + 1];
299 * Linux uses the last byte in the dirent buffer to store d_type,
300 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
302 #define LINUX_RECLEN(namlen) \
303 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
305 #define LINUX_RECLEN64(namlen) \
306 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
309 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \
310 LINUX_RECLEN64(LINUX_NAME_MAX))
311 #define LINUX_DIRBLKSIZ 512
314 getdents_common(struct thread *td, struct linux_getdents64_args *args,
319 caddr_t inp, buf; /* BSD-format */
320 int len, reclen; /* BSD-format */
321 caddr_t outp; /* Linux-format */
322 int resid, linuxreclen=0; /* Linux-format */
323 caddr_t lbuf; /* Linux-format */
329 struct l_dirent *linux_dirent;
330 struct l_dirent64 *linux_dirent64;
331 int buflen, error, eofflag, nbytes, justone;
332 u_long *cookies = NULL, *cookiep;
335 nbytes = args->count;
337 /* readdir(2) case. Always struct dirent. */
340 nbytes = sizeof(*linux_dirent);
345 error = getvnode(td, args->fd, cap_rights_init(&rights, CAP_READ), &fp);
349 if ((fp->f_flag & FREAD) == 0) {
354 off = foffset_lock(fp, 0);
356 if (vp->v_type != VDIR) {
357 foffset_unlock(fp, off, 0);
363 buflen = max(LINUX_DIRBLKSIZ, nbytes);
364 buflen = min(buflen, MAXBSIZE);
365 buf = malloc(buflen, M_LINUX, M_WAITOK);
366 lbuf = malloc(LINUX_MAXRECLEN, M_LINUX, M_WAITOK | M_ZERO);
367 vn_lock(vp, LK_SHARED | LK_RETRY);
370 aiov.iov_len = buflen;
371 auio.uio_iov = &aiov;
373 auio.uio_rw = UIO_READ;
374 auio.uio_segflg = UIO_SYSSPACE;
376 auio.uio_resid = buflen;
377 auio.uio_offset = off;
381 * Do directory search MAC check using non-cached credentials.
383 if ((error = mac_vnode_check_readdir(td->td_ucred, vp)))
386 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
391 outp = (caddr_t)args->dirent;
393 if ((len = buflen - auio.uio_resid) <= 0)
400 * When using cookies, the vfs has the option of reading from
401 * a different offset than that supplied (UFS truncates the
402 * offset to a block boundary to make sure that it never reads
403 * partway through a directory entry, even if the directory
404 * has been compacted).
406 while (len > 0 && ncookies > 0 && *cookiep <= off) {
407 bdp = (struct dirent *) inp;
408 len -= bdp->d_reclen;
409 inp += bdp->d_reclen;
416 if (cookiep && ncookies == 0)
418 bdp = (struct dirent *) inp;
419 reclen = bdp->d_reclen;
425 if (bdp->d_fileno == 0) {
437 linuxreclen = (is64bit)
438 ? LINUX_RECLEN64(bdp->d_namlen)
439 : LINUX_RECLEN(bdp->d_namlen);
441 if (reclen > len || resid < linuxreclen) {
447 /* readdir(2) case. */
448 linux_dirent = (struct l_dirent*)lbuf;
449 linux_dirent->d_ino = bdp->d_fileno;
450 linux_dirent->d_off = (l_off_t)linuxreclen;
451 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen;
452 strlcpy(linux_dirent->d_name, bdp->d_name,
453 linuxreclen - offsetof(struct l_dirent, d_name));
454 error = copyout(linux_dirent, outp, linuxreclen);
457 linux_dirent64 = (struct l_dirent64*)lbuf;
458 linux_dirent64->d_ino = bdp->d_fileno;
459 linux_dirent64->d_off = (cookiep)
461 : (l_off_t)(off + reclen);
462 linux_dirent64->d_reclen = (l_ushort)linuxreclen;
463 linux_dirent64->d_type = bdp->d_type;
464 strlcpy(linux_dirent64->d_name, bdp->d_name,
465 linuxreclen - offsetof(struct l_dirent64, d_name));
466 error = copyout(linux_dirent64, outp, linuxreclen);
467 } else if (!justone) {
468 linux_dirent = (struct l_dirent*)lbuf;
469 linux_dirent->d_ino = bdp->d_fileno;
470 linux_dirent->d_off = (cookiep)
472 : (l_off_t)(off + reclen);
473 linux_dirent->d_reclen = (l_ushort)linuxreclen;
475 * Copy d_type to last byte of l_dirent buffer
477 lbuf[linuxreclen-1] = bdp->d_type;
478 strlcpy(linux_dirent->d_name, bdp->d_name,
479 linuxreclen - offsetof(struct l_dirent, d_name)-1);
480 error = copyout(linux_dirent, outp, linuxreclen);
494 resid -= linuxreclen;
500 if (outp == (caddr_t)args->dirent) {
506 nbytes = resid + linuxreclen;
509 td->td_retval[0] = nbytes - resid;
512 free(cookies, M_TEMP);
515 foffset_unlock(fp, off, 0);
523 linux_getdents(struct thread *td, struct linux_getdents_args *args)
527 if (ldebug(getdents))
528 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
531 return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
535 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
539 if (ldebug(getdents64))
540 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
543 return (getdents_common(td, args, 1));
547 * These exist mainly for hooks for doing /compat/linux translation.
551 linux_access(struct thread *td, struct linux_access_args *args)
556 /* linux convention */
557 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
560 LCONVPATHEXIST(td, args->path, &path);
564 printf(ARGS(access, "%s, %d"), path, args->amode);
566 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
574 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
579 /* linux convention */
580 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
583 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
584 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
588 printf(ARGS(access, "%s, %d"), path, args->amode);
591 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode);
598 linux_unlink(struct thread *td, struct linux_unlink_args *args)
604 LCONVPATHEXIST(td, args->path, &path);
608 printf(ARGS(unlink, "%s"), path);
611 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0);
612 if (error == EPERM) {
613 /* Introduce POSIX noncompliant behaviour of Linux */
614 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
616 if (S_ISDIR(st.st_mode))
625 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
631 if (args->flag & ~LINUX_AT_REMOVEDIR)
634 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
635 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
638 if (ldebug(unlinkat))
639 printf(ARGS(unlinkat, "%s"), path);
642 if (args->flag & LINUX_AT_REMOVEDIR)
643 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
645 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
646 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
647 /* Introduce POSIX noncompliant behaviour of Linux */
648 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
649 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
656 linux_chdir(struct thread *td, struct linux_chdir_args *args)
661 LCONVPATHEXIST(td, args->path, &path);
665 printf(ARGS(chdir, "%s"), path);
667 error = kern_chdir(td, path, UIO_SYSSPACE);
673 linux_chmod(struct thread *td, struct linux_chmod_args *args)
678 LCONVPATHEXIST(td, args->path, &path);
682 printf(ARGS(chmod, "%s, %d"), path, args->mode);
684 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE,
691 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
696 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
697 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
700 if (ldebug(fchmodat))
701 printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
704 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
710 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
715 LCONVPATHCREAT(td, args->path, &path);
719 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
721 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
727 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
732 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
733 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
737 printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
739 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
745 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
750 LCONVPATHEXIST(td, args->path, &path);
754 printf(ARGS(rmdir, "%s"), path);
756 error = kern_rmdirat(td, AT_FDCWD, path, UIO_SYSSPACE);
762 linux_rename(struct thread *td, struct linux_rename_args *args)
767 LCONVPATHEXIST(td, args->from, &from);
768 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
769 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
777 printf(ARGS(rename, "%s, %s"), from, to);
779 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
786 linux_renameat(struct thread *td, struct linux_renameat_args *args)
789 int error, olddfd, newdfd;
791 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
792 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
793 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
794 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
795 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
802 if (ldebug(renameat))
803 printf(ARGS(renameat, "%s, %s"), from, to);
805 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
812 linux_symlink(struct thread *td, struct linux_symlink_args *args)
817 LCONVPATHEXIST(td, args->path, &path);
818 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
819 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
827 printf(ARGS(symlink, "%s, %s"), path, to);
829 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
836 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
841 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
842 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd);
843 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
844 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
851 if (ldebug(symlinkat))
852 printf(ARGS(symlinkat, "%s, %s"), path, to);
855 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
862 linux_readlink(struct thread *td, struct linux_readlink_args *args)
867 LCONVPATHEXIST(td, args->name, &name);
870 if (ldebug(readlink))
871 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
874 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
875 args->buf, UIO_USERSPACE, args->count);
881 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
886 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
887 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
890 if (ldebug(readlinkat))
891 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
895 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
896 UIO_USERSPACE, args->bufsiz);
902 linux_truncate(struct thread *td, struct linux_truncate_args *args)
907 LCONVPATHEXIST(td, args->path, &path);
910 if (ldebug(truncate))
911 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
914 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
919 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
921 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
926 LCONVPATHEXIST(td, args->path, &path);
929 if (ldebug(truncate64))
930 printf(ARGS(truncate64, "%s, %jd"), path, args->length);
933 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
937 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
940 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
942 struct ftruncate_args /* {
949 nuap.length = args->length;
950 return (sys_ftruncate(td, &nuap));
954 linux_link(struct thread *td, struct linux_link_args *args)
959 LCONVPATHEXIST(td, args->path, &path);
960 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
961 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
969 printf(ARGS(link, "%s, %s"), path, to);
971 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
979 linux_linkat(struct thread *td, struct linux_linkat_args *args)
982 int error, olddfd, newdfd, follow;
984 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
987 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
988 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
989 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
990 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
991 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
999 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
1000 args->newdfd, to, args->flag);
1003 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
1005 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
1012 linux_fdatasync(td, uap)
1014 struct linux_fdatasync_args *uap;
1017 return (kern_fsync(td, uap->fd, false));
1021 linux_pread(td, uap)
1023 struct linux_pread_args *uap;
1025 struct pread_args bsd;
1026 cap_rights_t rights;
1032 bsd.nbyte = uap->nbyte;
1033 bsd.offset = uap->offset;
1034 error = sys_pread(td, &bsd);
1036 /* This seems to violate POSIX but linux does it */
1037 error = fgetvp(td, uap->fd,
1038 cap_rights_init(&rights, CAP_PREAD), &vp);
1041 if (vp->v_type == VDIR) {
1051 linux_pwrite(td, uap)
1053 struct linux_pwrite_args *uap;
1055 struct pwrite_args bsd;
1059 bsd.nbyte = uap->nbyte;
1060 bsd.offset = uap->offset;
1061 return (sys_pwrite(td, &bsd));
1065 linux_mount(struct thread *td, struct linux_mount_args *args)
1067 char fstypename[MFSNAMELEN];
1068 char mntonname[MNAMELEN], mntfromname[MNAMELEN];
1072 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1076 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1079 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1085 printf(ARGS(mount, "%s, %s, %s"),
1086 fstypename, mntfromname, mntonname);
1089 if (strcmp(fstypename, "ext2") == 0) {
1090 strcpy(fstypename, "ext2fs");
1091 } else if (strcmp(fstypename, "proc") == 0) {
1092 strcpy(fstypename, "linprocfs");
1093 } else if (strcmp(fstypename, "vfat") == 0) {
1094 strcpy(fstypename, "msdosfs");
1099 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1101 * Linux SYNC flag is not included; the closest equivalent
1102 * FreeBSD has is !ASYNC, which is our default.
1104 if (args->rwflag & LINUX_MS_RDONLY)
1105 fsflags |= MNT_RDONLY;
1106 if (args->rwflag & LINUX_MS_NOSUID)
1107 fsflags |= MNT_NOSUID;
1108 if (args->rwflag & LINUX_MS_NOEXEC)
1109 fsflags |= MNT_NOEXEC;
1110 if (args->rwflag & LINUX_MS_REMOUNT)
1111 fsflags |= MNT_UPDATE;
1114 error = kernel_vmount(fsflags,
1115 "fstype", fstypename,
1116 "fspath", mntonname,
1117 "from", mntfromname,
1122 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1124 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1126 struct linux_umount_args args2;
1128 args2.path = args->path;
1130 return (linux_umount(td, &args2));
1132 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1135 linux_umount(struct thread *td, struct linux_umount_args *args)
1137 struct unmount_args bsd;
1139 bsd.path = args->path;
1140 bsd.flags = args->flags; /* XXX correct? */
1141 return (sys_unmount(td, &bsd));
1145 * fcntl family of syscalls
1155 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1161 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1163 switch (linux_flock->l_type) {
1165 bsd_flock->l_type = F_RDLCK;
1168 bsd_flock->l_type = F_WRLCK;
1171 bsd_flock->l_type = F_UNLCK;
1174 bsd_flock->l_type = -1;
1177 bsd_flock->l_whence = linux_flock->l_whence;
1178 bsd_flock->l_start = (off_t)linux_flock->l_start;
1179 bsd_flock->l_len = (off_t)linux_flock->l_len;
1180 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1181 bsd_flock->l_sysid = 0;
1185 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1187 switch (bsd_flock->l_type) {
1189 linux_flock->l_type = LINUX_F_RDLCK;
1192 linux_flock->l_type = LINUX_F_WRLCK;
1195 linux_flock->l_type = LINUX_F_UNLCK;
1198 linux_flock->l_whence = bsd_flock->l_whence;
1199 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1200 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1201 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1204 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1212 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1218 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1220 switch (linux_flock->l_type) {
1222 bsd_flock->l_type = F_RDLCK;
1225 bsd_flock->l_type = F_WRLCK;
1228 bsd_flock->l_type = F_UNLCK;
1231 bsd_flock->l_type = -1;
1234 bsd_flock->l_whence = linux_flock->l_whence;
1235 bsd_flock->l_start = (off_t)linux_flock->l_start;
1236 bsd_flock->l_len = (off_t)linux_flock->l_len;
1237 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1238 bsd_flock->l_sysid = 0;
1242 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1244 switch (bsd_flock->l_type) {
1246 linux_flock->l_type = LINUX_F_RDLCK;
1249 linux_flock->l_type = LINUX_F_WRLCK;
1252 linux_flock->l_type = LINUX_F_UNLCK;
1255 linux_flock->l_whence = bsd_flock->l_whence;
1256 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1257 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1258 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1260 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1263 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1265 struct l_flock linux_flock;
1266 struct flock bsd_flock;
1267 cap_rights_t rights;
1272 switch (args->cmd) {
1274 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1277 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1280 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1283 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1284 result = td->td_retval[0];
1285 td->td_retval[0] = 0;
1286 if (result & O_RDONLY)
1287 td->td_retval[0] |= LINUX_O_RDONLY;
1288 if (result & O_WRONLY)
1289 td->td_retval[0] |= LINUX_O_WRONLY;
1290 if (result & O_RDWR)
1291 td->td_retval[0] |= LINUX_O_RDWR;
1292 if (result & O_NDELAY)
1293 td->td_retval[0] |= LINUX_O_NONBLOCK;
1294 if (result & O_APPEND)
1295 td->td_retval[0] |= LINUX_O_APPEND;
1296 if (result & O_FSYNC)
1297 td->td_retval[0] |= LINUX_O_SYNC;
1298 if (result & O_ASYNC)
1299 td->td_retval[0] |= LINUX_FASYNC;
1300 #ifdef LINUX_O_NOFOLLOW
1301 if (result & O_NOFOLLOW)
1302 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1304 #ifdef LINUX_O_DIRECT
1305 if (result & O_DIRECT)
1306 td->td_retval[0] |= LINUX_O_DIRECT;
1312 if (args->arg & LINUX_O_NDELAY)
1314 if (args->arg & LINUX_O_APPEND)
1316 if (args->arg & LINUX_O_SYNC)
1318 if (args->arg & LINUX_FASYNC)
1320 #ifdef LINUX_O_NOFOLLOW
1321 if (args->arg & LINUX_O_NOFOLLOW)
1324 #ifdef LINUX_O_DIRECT
1325 if (args->arg & LINUX_O_DIRECT)
1328 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1331 error = copyin((void *)args->arg, &linux_flock,
1332 sizeof(linux_flock));
1335 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1336 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1339 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1340 return (copyout(&linux_flock, (void *)args->arg,
1341 sizeof(linux_flock)));
1344 error = copyin((void *)args->arg, &linux_flock,
1345 sizeof(linux_flock));
1348 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1349 return (kern_fcntl(td, args->fd, F_SETLK,
1350 (intptr_t)&bsd_flock));
1352 case LINUX_F_SETLKW:
1353 error = copyin((void *)args->arg, &linux_flock,
1354 sizeof(linux_flock));
1357 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1358 return (kern_fcntl(td, args->fd, F_SETLKW,
1359 (intptr_t)&bsd_flock));
1361 case LINUX_F_GETOWN:
1362 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1364 case LINUX_F_SETOWN:
1366 * XXX some Linux applications depend on F_SETOWN having no
1367 * significant effect for pipes (SIGIO is not delivered for
1368 * pipes under Linux-2.2.35 at least).
1370 error = fget(td, args->fd,
1371 cap_rights_init(&rights, CAP_FCNTL), &fp);
1374 if (fp->f_type == DTYPE_PIPE) {
1380 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1382 case LINUX_F_DUPFD_CLOEXEC:
1383 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1390 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1395 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1398 return (fcntl_common(td, args));
1401 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1403 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1405 struct l_flock64 linux_flock;
1406 struct flock bsd_flock;
1407 struct linux_fcntl_args fcntl_args;
1411 if (ldebug(fcntl64))
1412 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1415 switch (args->cmd) {
1416 case LINUX_F_GETLK64:
1417 error = copyin((void *)args->arg, &linux_flock,
1418 sizeof(linux_flock));
1421 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1422 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1425 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1426 return (copyout(&linux_flock, (void *)args->arg,
1427 sizeof(linux_flock)));
1429 case LINUX_F_SETLK64:
1430 error = copyin((void *)args->arg, &linux_flock,
1431 sizeof(linux_flock));
1434 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1435 return (kern_fcntl(td, args->fd, F_SETLK,
1436 (intptr_t)&bsd_flock));
1438 case LINUX_F_SETLKW64:
1439 error = copyin((void *)args->arg, &linux_flock,
1440 sizeof(linux_flock));
1443 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1444 return (kern_fcntl(td, args->fd, F_SETLKW,
1445 (intptr_t)&bsd_flock));
1448 fcntl_args.fd = args->fd;
1449 fcntl_args.cmd = args->cmd;
1450 fcntl_args.arg = args->arg;
1451 return (fcntl_common(td, &fcntl_args));
1453 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1456 linux_chown(struct thread *td, struct linux_chown_args *args)
1461 LCONVPATHEXIST(td, args->path, &path);
1465 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1467 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1474 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1477 int error, dfd, flag;
1479 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1482 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1483 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1486 if (ldebug(fchownat))
1487 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1490 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1491 AT_SYMLINK_NOFOLLOW;
1492 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1499 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1504 LCONVPATHEXIST(td, args->path, &path);
1508 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1510 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1511 args->gid, AT_SYMLINK_NOFOLLOW);
1517 convert_fadvice(int advice)
1520 case LINUX_POSIX_FADV_NORMAL:
1521 return (POSIX_FADV_NORMAL);
1522 case LINUX_POSIX_FADV_RANDOM:
1523 return (POSIX_FADV_RANDOM);
1524 case LINUX_POSIX_FADV_SEQUENTIAL:
1525 return (POSIX_FADV_SEQUENTIAL);
1526 case LINUX_POSIX_FADV_WILLNEED:
1527 return (POSIX_FADV_WILLNEED);
1528 case LINUX_POSIX_FADV_DONTNEED:
1529 return (POSIX_FADV_DONTNEED);
1530 case LINUX_POSIX_FADV_NOREUSE:
1531 return (POSIX_FADV_NOREUSE);
1538 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1542 advice = convert_fadvice(args->advice);
1545 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1549 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1551 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1555 advice = convert_fadvice(args->advice);
1558 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1561 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1564 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1571 printf(ARGS(pipe, "*"));
1574 error = kern_pipe(td, fildes, 0, NULL, NULL);
1578 /* XXX: Close descriptors on error. */
1579 return (copyout(fildes, args->pipefds, sizeof(fildes)));
1583 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1590 printf(ARGS(pipe2, "*, %d"), args->flags);
1593 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1597 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1598 flags |= O_NONBLOCK;
1599 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1601 error = kern_pipe(td, fildes, flags, NULL, NULL);
1605 /* XXX: Close descriptors on error. */
1606 return (copyout(fildes, args->pipefds, sizeof(fildes)));
1610 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1615 if (args->oldfd == args->newfd)
1617 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1619 if (args->flags & LINUX_O_CLOEXEC)
1620 cmd = F_DUP2FD_CLOEXEC;
1624 newfd = args->newfd;
1625 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1629 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1633 * We emulate only posix_fallocate system call for which
1636 if (args->mode != 0)
1639 return (kern_posix_fallocate(td, args->fd, args->offset,