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->td_proc->p_fd, args->fd,
346 cap_rights_init(&rights, CAP_READ), &fp);
350 if ((fp->f_flag & FREAD) == 0) {
355 off = foffset_lock(fp, 0);
357 if (vp->v_type != VDIR) {
358 foffset_unlock(fp, off, 0);
364 buflen = max(LINUX_DIRBLKSIZ, nbytes);
365 buflen = min(buflen, MAXBSIZE);
366 buf = malloc(buflen, M_LINUX, M_WAITOK);
367 lbuf = malloc(LINUX_MAXRECLEN, M_LINUX, M_WAITOK | M_ZERO);
368 vn_lock(vp, LK_SHARED | LK_RETRY);
371 aiov.iov_len = buflen;
372 auio.uio_iov = &aiov;
374 auio.uio_rw = UIO_READ;
375 auio.uio_segflg = UIO_SYSSPACE;
377 auio.uio_resid = buflen;
378 auio.uio_offset = off;
382 * Do directory search MAC check using non-cached credentials.
384 if ((error = mac_vnode_check_readdir(td->td_ucred, vp)))
387 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
392 outp = (caddr_t)args->dirent;
394 if ((len = buflen - auio.uio_resid) <= 0)
401 * When using cookies, the vfs has the option of reading from
402 * a different offset than that supplied (UFS truncates the
403 * offset to a block boundary to make sure that it never reads
404 * partway through a directory entry, even if the directory
405 * has been compacted).
407 while (len > 0 && ncookies > 0 && *cookiep <= off) {
408 bdp = (struct dirent *) inp;
409 len -= bdp->d_reclen;
410 inp += bdp->d_reclen;
417 if (cookiep && ncookies == 0)
419 bdp = (struct dirent *) inp;
420 reclen = bdp->d_reclen;
426 if (bdp->d_fileno == 0) {
438 linuxreclen = (is64bit)
439 ? LINUX_RECLEN64(bdp->d_namlen)
440 : LINUX_RECLEN(bdp->d_namlen);
442 if (reclen > len || resid < linuxreclen) {
448 /* readdir(2) case. */
449 linux_dirent = (struct l_dirent*)lbuf;
450 linux_dirent->d_ino = bdp->d_fileno;
451 linux_dirent->d_off = (l_off_t)linuxreclen;
452 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen;
453 strlcpy(linux_dirent->d_name, bdp->d_name,
454 linuxreclen - offsetof(struct l_dirent, d_name));
455 error = copyout(linux_dirent, outp, linuxreclen);
458 linux_dirent64 = (struct l_dirent64*)lbuf;
459 linux_dirent64->d_ino = bdp->d_fileno;
460 linux_dirent64->d_off = (cookiep)
462 : (l_off_t)(off + reclen);
463 linux_dirent64->d_reclen = (l_ushort)linuxreclen;
464 linux_dirent64->d_type = bdp->d_type;
465 strlcpy(linux_dirent64->d_name, bdp->d_name,
466 linuxreclen - offsetof(struct l_dirent64, d_name));
467 error = copyout(linux_dirent64, outp, linuxreclen);
468 } else if (!justone) {
469 linux_dirent = (struct l_dirent*)lbuf;
470 linux_dirent->d_ino = bdp->d_fileno;
471 linux_dirent->d_off = (cookiep)
473 : (l_off_t)(off + reclen);
474 linux_dirent->d_reclen = (l_ushort)linuxreclen;
476 * Copy d_type to last byte of l_dirent buffer
478 lbuf[linuxreclen-1] = bdp->d_type;
479 strlcpy(linux_dirent->d_name, bdp->d_name,
480 linuxreclen - offsetof(struct l_dirent, d_name)-1);
481 error = copyout(linux_dirent, outp, linuxreclen);
495 resid -= linuxreclen;
501 if (outp == (caddr_t)args->dirent) {
507 nbytes = resid + linuxreclen;
510 td->td_retval[0] = nbytes - resid;
513 free(cookies, M_TEMP);
516 foffset_unlock(fp, off, 0);
524 linux_getdents(struct thread *td, struct linux_getdents_args *args)
528 if (ldebug(getdents))
529 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
532 return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
536 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
540 if (ldebug(getdents64))
541 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
544 return (getdents_common(td, args, 1));
548 * These exist mainly for hooks for doing /compat/linux translation.
552 linux_access(struct thread *td, struct linux_access_args *args)
557 /* linux convention */
558 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
561 LCONVPATHEXIST(td, args->path, &path);
565 printf(ARGS(access, "%s, %d"), path, args->amode);
567 error = kern_access(td, path, UIO_SYSSPACE, args->amode);
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_unlink(td, path, UIO_SYSSPACE);
613 /* Introduce POSIX noncompliant behaviour of Linux */
614 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0)
615 if (S_ISDIR(st.st_mode))
622 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
628 if (args->flag & ~LINUX_AT_REMOVEDIR)
631 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
632 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
635 if (ldebug(unlinkat))
636 printf(ARGS(unlinkat, "%s"), path);
639 if (args->flag & LINUX_AT_REMOVEDIR)
640 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
642 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
643 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
644 /* Introduce POSIX noncompliant behaviour of Linux */
645 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
646 UIO_SYSSPACE, &st) == 0 && S_ISDIR(st.st_mode))
653 linux_chdir(struct thread *td, struct linux_chdir_args *args)
658 LCONVPATHEXIST(td, args->path, &path);
662 printf(ARGS(chdir, "%s"), path);
664 error = kern_chdir(td, path, UIO_SYSSPACE);
670 linux_chmod(struct thread *td, struct linux_chmod_args *args)
675 LCONVPATHEXIST(td, args->path, &path);
679 printf(ARGS(chmod, "%s, %d"), path, args->mode);
681 error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
687 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
692 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
693 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
696 if (ldebug(fchmodat))
697 printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
700 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
706 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
711 LCONVPATHCREAT(td, args->path, &path);
715 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
717 error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
723 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
728 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
729 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
733 printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
735 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
741 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
746 LCONVPATHEXIST(td, args->path, &path);
750 printf(ARGS(rmdir, "%s"), path);
752 error = kern_rmdir(td, path, UIO_SYSSPACE);
758 linux_rename(struct thread *td, struct linux_rename_args *args)
763 LCONVPATHEXIST(td, args->from, &from);
764 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
765 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
773 printf(ARGS(rename, "%s, %s"), from, to);
775 error = kern_rename(td, from, to, UIO_SYSSPACE);
782 linux_renameat(struct thread *td, struct linux_renameat_args *args)
785 int error, olddfd, newdfd;
787 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
788 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
789 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
790 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
791 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
798 if (ldebug(renameat))
799 printf(ARGS(renameat, "%s, %s"), from, to);
801 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
808 linux_symlink(struct thread *td, struct linux_symlink_args *args)
813 LCONVPATHEXIST(td, args->path, &path);
814 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
815 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
823 printf(ARGS(symlink, "%s, %s"), path, to);
825 error = kern_symlink(td, path, to, UIO_SYSSPACE);
832 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
837 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
838 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd);
839 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
840 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
847 if (ldebug(symlinkat))
848 printf(ARGS(symlinkat, "%s, %s"), path, to);
851 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
858 linux_readlink(struct thread *td, struct linux_readlink_args *args)
863 LCONVPATHEXIST(td, args->name, &name);
866 if (ldebug(readlink))
867 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
870 error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
877 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
882 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
883 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
886 if (ldebug(readlinkat))
887 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
891 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
892 UIO_USERSPACE, args->bufsiz);
898 linux_truncate(struct thread *td, struct linux_truncate_args *args)
903 LCONVPATHEXIST(td, args->path, &path);
906 if (ldebug(truncate))
907 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
910 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
915 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
917 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
922 LCONVPATHEXIST(td, args->path, &path);
925 if (ldebug(truncate64))
926 printf(ARGS(truncate64, "%s, %jd"), path, args->length);
929 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
933 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
936 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
938 struct ftruncate_args /* {
945 nuap.length = args->length;
946 return (sys_ftruncate(td, &nuap));
950 linux_link(struct thread *td, struct linux_link_args *args)
955 LCONVPATHEXIST(td, args->path, &path);
956 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
957 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
965 printf(ARGS(link, "%s, %s"), path, to);
967 error = kern_link(td, path, to, UIO_SYSSPACE);
974 linux_linkat(struct thread *td, struct linux_linkat_args *args)
977 int error, olddfd, newdfd, follow;
979 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
982 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
983 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
984 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
985 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
986 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
994 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
995 args->newdfd, to, args->flag);
998 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
1000 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
1007 linux_fdatasync(td, uap)
1009 struct linux_fdatasync_args *uap;
1011 struct fsync_args bsd;
1014 return (sys_fsync(td, &bsd));
1018 linux_pread(td, uap)
1020 struct linux_pread_args *uap;
1022 struct pread_args bsd;
1023 cap_rights_t rights;
1029 bsd.nbyte = uap->nbyte;
1030 bsd.offset = uap->offset;
1031 error = sys_pread(td, &bsd);
1033 /* This seems to violate POSIX but linux does it */
1034 error = fgetvp(td, uap->fd,
1035 cap_rights_init(&rights, CAP_PREAD), &vp);
1038 if (vp->v_type == VDIR) {
1048 linux_pwrite(td, uap)
1050 struct linux_pwrite_args *uap;
1052 struct pwrite_args bsd;
1056 bsd.nbyte = uap->nbyte;
1057 bsd.offset = uap->offset;
1058 return (sys_pwrite(td, &bsd));
1062 linux_mount(struct thread *td, struct linux_mount_args *args)
1064 char fstypename[MFSNAMELEN];
1065 char mntonname[MNAMELEN], mntfromname[MNAMELEN];
1069 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1073 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1076 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1082 printf(ARGS(mount, "%s, %s, %s"),
1083 fstypename, mntfromname, mntonname);
1086 if (strcmp(fstypename, "ext2") == 0) {
1087 strcpy(fstypename, "ext2fs");
1088 } else if (strcmp(fstypename, "proc") == 0) {
1089 strcpy(fstypename, "linprocfs");
1090 } else if (strcmp(fstypename, "vfat") == 0) {
1091 strcpy(fstypename, "msdosfs");
1096 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1098 * Linux SYNC flag is not included; the closest equivalent
1099 * FreeBSD has is !ASYNC, which is our default.
1101 if (args->rwflag & LINUX_MS_RDONLY)
1102 fsflags |= MNT_RDONLY;
1103 if (args->rwflag & LINUX_MS_NOSUID)
1104 fsflags |= MNT_NOSUID;
1105 if (args->rwflag & LINUX_MS_NOEXEC)
1106 fsflags |= MNT_NOEXEC;
1107 if (args->rwflag & LINUX_MS_REMOUNT)
1108 fsflags |= MNT_UPDATE;
1111 error = kernel_vmount(fsflags,
1112 "fstype", fstypename,
1113 "fspath", mntonname,
1114 "from", mntfromname,
1119 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1121 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1123 struct linux_umount_args args2;
1125 args2.path = args->path;
1127 return (linux_umount(td, &args2));
1129 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1132 linux_umount(struct thread *td, struct linux_umount_args *args)
1134 struct unmount_args bsd;
1136 bsd.path = args->path;
1137 bsd.flags = args->flags; /* XXX correct? */
1138 return (sys_unmount(td, &bsd));
1142 * fcntl family of syscalls
1152 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1158 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1160 switch (linux_flock->l_type) {
1162 bsd_flock->l_type = F_RDLCK;
1165 bsd_flock->l_type = F_WRLCK;
1168 bsd_flock->l_type = F_UNLCK;
1171 bsd_flock->l_type = -1;
1174 bsd_flock->l_whence = linux_flock->l_whence;
1175 bsd_flock->l_start = (off_t)linux_flock->l_start;
1176 bsd_flock->l_len = (off_t)linux_flock->l_len;
1177 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1178 bsd_flock->l_sysid = 0;
1182 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1184 switch (bsd_flock->l_type) {
1186 linux_flock->l_type = LINUX_F_RDLCK;
1189 linux_flock->l_type = LINUX_F_WRLCK;
1192 linux_flock->l_type = LINUX_F_UNLCK;
1195 linux_flock->l_whence = bsd_flock->l_whence;
1196 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1197 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1198 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1201 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1209 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1215 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1217 switch (linux_flock->l_type) {
1219 bsd_flock->l_type = F_RDLCK;
1222 bsd_flock->l_type = F_WRLCK;
1225 bsd_flock->l_type = F_UNLCK;
1228 bsd_flock->l_type = -1;
1231 bsd_flock->l_whence = linux_flock->l_whence;
1232 bsd_flock->l_start = (off_t)linux_flock->l_start;
1233 bsd_flock->l_len = (off_t)linux_flock->l_len;
1234 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1235 bsd_flock->l_sysid = 0;
1239 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1241 switch (bsd_flock->l_type) {
1243 linux_flock->l_type = LINUX_F_RDLCK;
1246 linux_flock->l_type = LINUX_F_WRLCK;
1249 linux_flock->l_type = LINUX_F_UNLCK;
1252 linux_flock->l_whence = bsd_flock->l_whence;
1253 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1254 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1255 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1257 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1260 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1262 struct l_flock linux_flock;
1263 struct flock bsd_flock;
1264 cap_rights_t rights;
1269 switch (args->cmd) {
1271 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1274 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1277 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1280 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1281 result = td->td_retval[0];
1282 td->td_retval[0] = 0;
1283 if (result & O_RDONLY)
1284 td->td_retval[0] |= LINUX_O_RDONLY;
1285 if (result & O_WRONLY)
1286 td->td_retval[0] |= LINUX_O_WRONLY;
1287 if (result & O_RDWR)
1288 td->td_retval[0] |= LINUX_O_RDWR;
1289 if (result & O_NDELAY)
1290 td->td_retval[0] |= LINUX_O_NONBLOCK;
1291 if (result & O_APPEND)
1292 td->td_retval[0] |= LINUX_O_APPEND;
1293 if (result & O_FSYNC)
1294 td->td_retval[0] |= LINUX_O_SYNC;
1295 if (result & O_ASYNC)
1296 td->td_retval[0] |= LINUX_FASYNC;
1297 #ifdef LINUX_O_NOFOLLOW
1298 if (result & O_NOFOLLOW)
1299 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1301 #ifdef LINUX_O_DIRECT
1302 if (result & O_DIRECT)
1303 td->td_retval[0] |= LINUX_O_DIRECT;
1309 if (args->arg & LINUX_O_NDELAY)
1311 if (args->arg & LINUX_O_APPEND)
1313 if (args->arg & LINUX_O_SYNC)
1315 if (args->arg & LINUX_FASYNC)
1317 #ifdef LINUX_O_NOFOLLOW
1318 if (args->arg & LINUX_O_NOFOLLOW)
1321 #ifdef LINUX_O_DIRECT
1322 if (args->arg & LINUX_O_DIRECT)
1325 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1328 error = copyin((void *)args->arg, &linux_flock,
1329 sizeof(linux_flock));
1332 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1333 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1336 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1337 return (copyout(&linux_flock, (void *)args->arg,
1338 sizeof(linux_flock)));
1341 error = copyin((void *)args->arg, &linux_flock,
1342 sizeof(linux_flock));
1345 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1346 return (kern_fcntl(td, args->fd, F_SETLK,
1347 (intptr_t)&bsd_flock));
1349 case LINUX_F_SETLKW:
1350 error = copyin((void *)args->arg, &linux_flock,
1351 sizeof(linux_flock));
1354 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1355 return (kern_fcntl(td, args->fd, F_SETLKW,
1356 (intptr_t)&bsd_flock));
1358 case LINUX_F_GETOWN:
1359 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1361 case LINUX_F_SETOWN:
1363 * XXX some Linux applications depend on F_SETOWN having no
1364 * significant effect for pipes (SIGIO is not delivered for
1365 * pipes under Linux-2.2.35 at least).
1367 error = fget(td, args->fd,
1368 cap_rights_init(&rights, CAP_FCNTL), &fp);
1371 if (fp->f_type == DTYPE_PIPE) {
1377 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1379 case LINUX_F_DUPFD_CLOEXEC:
1380 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1387 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1392 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1395 return (fcntl_common(td, args));
1398 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1400 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1402 struct l_flock64 linux_flock;
1403 struct flock bsd_flock;
1404 struct linux_fcntl_args fcntl_args;
1408 if (ldebug(fcntl64))
1409 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1412 switch (args->cmd) {
1413 case LINUX_F_GETLK64:
1414 error = copyin((void *)args->arg, &linux_flock,
1415 sizeof(linux_flock));
1418 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1419 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1422 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1423 return (copyout(&linux_flock, (void *)args->arg,
1424 sizeof(linux_flock)));
1426 case LINUX_F_SETLK64:
1427 error = copyin((void *)args->arg, &linux_flock,
1428 sizeof(linux_flock));
1431 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1432 return (kern_fcntl(td, args->fd, F_SETLK,
1433 (intptr_t)&bsd_flock));
1435 case LINUX_F_SETLKW64:
1436 error = copyin((void *)args->arg, &linux_flock,
1437 sizeof(linux_flock));
1440 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1441 return (kern_fcntl(td, args->fd, F_SETLKW,
1442 (intptr_t)&bsd_flock));
1445 fcntl_args.fd = args->fd;
1446 fcntl_args.cmd = args->cmd;
1447 fcntl_args.arg = args->arg;
1448 return (fcntl_common(td, &fcntl_args));
1450 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1453 linux_chown(struct thread *td, struct linux_chown_args *args)
1458 LCONVPATHEXIST(td, args->path, &path);
1462 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1464 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1470 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1473 int error, dfd, flag;
1475 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1478 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1479 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1482 if (ldebug(fchownat))
1483 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1486 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1487 AT_SYMLINK_NOFOLLOW;
1488 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1495 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1500 LCONVPATHEXIST(td, args->path, &path);
1504 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1506 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1512 convert_fadvice(int advice)
1515 case LINUX_POSIX_FADV_NORMAL:
1516 return (POSIX_FADV_NORMAL);
1517 case LINUX_POSIX_FADV_RANDOM:
1518 return (POSIX_FADV_RANDOM);
1519 case LINUX_POSIX_FADV_SEQUENTIAL:
1520 return (POSIX_FADV_SEQUENTIAL);
1521 case LINUX_POSIX_FADV_WILLNEED:
1522 return (POSIX_FADV_WILLNEED);
1523 case LINUX_POSIX_FADV_DONTNEED:
1524 return (POSIX_FADV_DONTNEED);
1525 case LINUX_POSIX_FADV_NOREUSE:
1526 return (POSIX_FADV_NOREUSE);
1533 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1537 advice = convert_fadvice(args->advice);
1540 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1544 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1546 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1550 advice = convert_fadvice(args->advice);
1553 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1556 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1559 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1566 printf(ARGS(pipe, "*"));
1569 error = kern_pipe2(td, fildes, 0);
1573 /* XXX: Close descriptors on error. */
1574 return (copyout(fildes, args->pipefds, sizeof(fildes)));
1578 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1585 printf(ARGS(pipe2, "*, %d"), args->flags);
1588 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1592 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1593 flags |= O_NONBLOCK;
1594 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1596 error = kern_pipe2(td, fildes, flags);
1600 /* XXX: Close descriptors on error. */
1601 return (copyout(fildes, args->pipefds, sizeof(fildes)));
1605 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1610 if (args->oldfd == args->newfd)
1612 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1614 if (args->flags & LINUX_O_CLOEXEC)
1615 cmd = F_DUP2FD_CLOEXEC;
1619 newfd = args->newfd;
1620 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1624 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1628 * We emulate only posix_fallocate system call for which
1631 if (args->mode != 0)
1634 return (kern_posix_fallocate(td, args->fd, args->offset,