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"
35 #include <sys/param.h>
36 #include <sys/systm.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>
58 #include <ufs/ufs/extattr.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/ufsmount.h>
63 #include <machine/../linux32/linux.h>
64 #include <machine/../linux32/linux32_proto.h>
66 #include <machine/../linux/linux.h>
67 #include <machine/../linux/linux_proto.h>
69 #include <compat/linux/linux_util.h>
72 linux_creat(struct thread *td, struct linux_creat_args *args)
77 LCONVPATHEXIST(td, args->path, &path);
81 printf(ARGS(creat, "%s, %d"), path, args->mode);
83 error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
91 linux_common_open(struct thread *td, char *path, int l_flags, int mode, int openat)
93 struct proc *p = td->td_proc;
99 switch (l_flags & LINUX_O_ACCMODE) {
101 bsd_flags |= O_WRONLY;
107 bsd_flags |= O_RDONLY;
109 if (l_flags & LINUX_O_NDELAY)
110 bsd_flags |= O_NONBLOCK;
111 if (l_flags & LINUX_O_APPEND)
112 bsd_flags |= O_APPEND;
113 if (l_flags & LINUX_O_SYNC)
114 bsd_flags |= O_FSYNC;
115 if (l_flags & LINUX_O_NONBLOCK)
116 bsd_flags |= O_NONBLOCK;
117 if (l_flags & LINUX_FASYNC)
118 bsd_flags |= O_ASYNC;
119 if (l_flags & LINUX_O_CREAT)
120 bsd_flags |= O_CREAT;
121 if (l_flags & LINUX_O_TRUNC)
122 bsd_flags |= O_TRUNC;
123 if (l_flags & LINUX_O_EXCL)
125 if (l_flags & LINUX_O_NOCTTY)
126 bsd_flags |= O_NOCTTY;
127 if (l_flags & LINUX_O_DIRECT)
128 bsd_flags |= O_DIRECT;
129 if (l_flags & LINUX_O_NOFOLLOW)
130 bsd_flags |= O_NOFOLLOW;
131 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
133 error = kern_open(td, path, UIO_SYSSPACE, bsd_flags, mode);
135 fd = td->td_retval[0];
137 * XXX In between kern_open() and fget(), another process
138 * having the same filedesc could use that fd without
141 error = fget(td, fd, &fp);
143 sx_slock(&proctree_lock);
145 if (!(bsd_flags & O_NOCTTY) &&
146 SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
148 sx_unlock(&proctree_lock);
149 if (fp->f_type == DTYPE_VNODE)
150 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
154 sx_sunlock(&proctree_lock);
156 if (l_flags & LINUX_O_DIRECTORY) {
157 if (fp->f_type != DTYPE_VNODE ||
158 fp->f_vnode->v_type != VDIR) {
164 * XXX as above, fdrop()/kern_close() pair is racy.
173 printf(LMSG("open returns error %d"), error);
181 * common code for linux *at set of syscalls
184 * if filename is absolute
187 * if dirfd == AT_FDCWD
188 * return CWD/filename
190 * return DIRFD/filename
193 linux_at(struct thread *td, int dirfd, char *filename, char **newpath, char **freebuf)
196 int error = 0, vfslocked;
198 struct filedesc *fdp = td->td_proc->p_fd;
199 char *fullpath = "unknown";
200 char *freepath = NULL;
202 /* don't do anything if the pathname is absolute */
203 if (*filename == '/') {
208 /* check for AT_FDWCD */
209 if (dirfd == LINUX_AT_FDCWD) {
213 FILEDESC_SUNLOCK(fdp);
215 error = fget(td, dirfd, &fp);
219 /* only a dir can be dfd */
220 if (dvp->v_type != VDIR) {
229 * XXXRW: This is bogus, as vn_fullpath() returns only an advisory
230 * file path, and may fail in several common situations, including
231 * for file systmes that don't use the name cache, and if the entry
232 * for the file falls out of the name cache. We should implement
233 * openat() in the FreeBSD native system call layer properly (using a
234 * requested starting directory), and have Linux and other ABIs wrap
235 * the native implementation.
237 error = vn_fullpath(td, dvp, &fullpath, &freepath);
239 *newpath = malloc(strlen(fullpath) + strlen(filename) + 2, M_TEMP, M_WAITOK | M_ZERO);
241 sprintf(*newpath, "%s/%s", fullpath, filename);
245 vfslocked = VFS_LOCK_GIANT(dvp->v_mount);
247 VFS_UNLOCK_GIANT(vfslocked);
252 linux_openat(struct thread *td, struct linux_openat_args *args)
254 char *newpath, *oldpath, *freebuf, *path;
257 oldpath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
258 error = copyinstr(args->filename, oldpath, MAXPATHLEN, NULL);
260 free(oldpath, M_TEMP);
265 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd,
266 oldpath, args->flags, args->mode);
268 newpath = freebuf = NULL;
269 error = linux_at(td, args->dfd, oldpath, &newpath, &freebuf);
273 printf(LMSG("newpath: %s"), newpath);
275 if (args->flags & LINUX_O_CREAT)
276 LCONVPATH_SEG(td, newpath, &path, 1, UIO_SYSSPACE);
278 LCONVPATH_SEG(td, newpath, &path, 0, UIO_SYSSPACE);
281 free(freebuf, M_TEMP);
283 free(newpath, M_TEMP);
285 error = linux_common_open(td, path, args->flags,
289 free(oldpath, M_TEMP);
294 linux_open(struct thread *td, struct linux_open_args *args)
298 if (args->flags & LINUX_O_CREAT)
299 LCONVPATHCREAT(td, args->path, &path);
301 LCONVPATHEXIST(td, args->path, &path);
305 printf(ARGS(open, "%s, 0x%x, 0x%x"),
306 path, args->flags, args->mode);
309 return linux_common_open(td, path, args->flags, args->mode, 0);
313 linux_lseek(struct thread *td, struct linux_lseek_args *args)
316 struct lseek_args /* {
326 printf(ARGS(lseek, "%d, %ld, %d"),
327 args->fdes, (long)args->off, args->whence);
329 tmp_args.fd = args->fdes;
330 tmp_args.offset = (off_t)args->off;
331 tmp_args.whence = args->whence;
332 error = lseek(td, &tmp_args);
337 linux_llseek(struct thread *td, struct linux_llseek_args *args)
339 struct lseek_args bsd_args;
345 printf(ARGS(llseek, "%d, %d:%d, %d"),
346 args->fd, args->ohigh, args->olow, args->whence);
348 off = (args->olow) | (((off_t) args->ohigh) << 32);
350 bsd_args.fd = args->fd;
351 bsd_args.offset = off;
352 bsd_args.whence = args->whence;
354 if ((error = lseek(td, &bsd_args)))
357 if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
360 td->td_retval[0] = 0;
365 linux_readdir(struct thread *td, struct linux_readdir_args *args)
367 struct linux_getdents_args lda;
370 lda.dent = args->dent;
372 return linux_getdents(td, &lda);
376 * Note that linux_getdents(2) and linux_getdents64(2) have the same
377 * arguments. They only differ in the definition of struct dirent they
378 * operate on. We use this to common the code, with the exception of
379 * accessing struct dirent. Note that linux_readdir(2) is implemented
380 * by means of linux_getdents(2). In this case we never operate on
381 * struct dirent64 and thus don't need to handle it...
388 char d_name[LINUX_NAME_MAX + 1];
396 char d_name[LINUX_NAME_MAX + 1];
400 * Linux uses the last byte in the dirent buffer to store d_type,
401 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
403 #define LINUX_RECLEN(namlen) \
404 roundup((offsetof(struct l_dirent, d_name) + (namlen) + 2), \
407 #define LINUX_RECLEN64(namlen) \
408 roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1), \
411 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \
412 LINUX_RECLEN64(LINUX_NAME_MAX))
413 #define LINUX_DIRBLKSIZ 512
416 getdents_common(struct thread *td, struct linux_getdents64_args *args,
421 caddr_t inp, buf; /* BSD-format */
422 int len, reclen; /* BSD-format */
423 caddr_t outp; /* Linux-format */
424 int resid, linuxreclen=0; /* Linux-format */
425 caddr_t lbuf; /* Linux-format */
430 struct l_dirent *linux_dirent;
431 struct l_dirent64 *linux_dirent64;
432 int buflen, error, eofflag, nbytes, justone;
433 u_long *cookies = NULL, *cookiep;
434 int ncookies, vfslocked;
436 nbytes = args->count;
438 /* readdir(2) case. Always struct dirent. */
441 nbytes = sizeof(*linux_dirent);
446 if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
449 if ((fp->f_flag & FREAD) == 0) {
455 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
456 if (vp->v_type != VDIR) {
457 VFS_UNLOCK_GIANT(vfslocked);
464 buflen = max(LINUX_DIRBLKSIZ, nbytes);
465 buflen = min(buflen, MAXBSIZE);
466 buf = malloc(buflen, M_TEMP, M_WAITOK);
467 lbuf = malloc(LINUX_MAXRECLEN, M_TEMP, M_WAITOK | M_ZERO);
468 vn_lock(vp, LK_SHARED | LK_RETRY, td);
472 aiov.iov_len = buflen;
473 auio.uio_iov = &aiov;
475 auio.uio_rw = UIO_READ;
476 auio.uio_segflg = UIO_SYSSPACE;
478 auio.uio_resid = buflen;
479 auio.uio_offset = off;
482 free(cookies, M_TEMP);
488 * Do directory search MAC check using non-cached credentials.
490 if ((error = mac_check_vnode_readdir(td->td_ucred, vp)))
493 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
498 outp = (caddr_t)args->dirent;
500 if ((len = buflen - auio.uio_resid) <= 0)
507 * When using cookies, the vfs has the option of reading from
508 * a different offset than that supplied (UFS truncates the
509 * offset to a block boundary to make sure that it never reads
510 * partway through a directory entry, even if the directory
511 * has been compacted).
513 while (len > 0 && ncookies > 0 && *cookiep <= off) {
514 bdp = (struct dirent *) inp;
515 len -= bdp->d_reclen;
516 inp += bdp->d_reclen;
523 if (cookiep && ncookies == 0)
525 bdp = (struct dirent *) inp;
526 reclen = bdp->d_reclen;
532 if (bdp->d_fileno == 0) {
544 linuxreclen = (is64bit)
545 ? LINUX_RECLEN64(bdp->d_namlen)
546 : LINUX_RECLEN(bdp->d_namlen);
548 if (reclen > len || resid < linuxreclen) {
554 /* readdir(2) case. */
555 linux_dirent = (struct l_dirent*)lbuf;
556 linux_dirent->d_ino = bdp->d_fileno;
557 linux_dirent->d_off = (l_off_t)linuxreclen;
558 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen;
559 strlcpy(linux_dirent->d_name, bdp->d_name,
560 linuxreclen - offsetof(struct l_dirent, d_name));
561 error = copyout(linux_dirent, outp, linuxreclen);
564 linux_dirent64 = (struct l_dirent64*)lbuf;
565 linux_dirent64->d_ino = bdp->d_fileno;
566 linux_dirent64->d_off = (cookiep)
568 : (l_off_t)(off + reclen);
569 linux_dirent64->d_reclen = (l_ushort)linuxreclen;
570 linux_dirent64->d_type = bdp->d_type;
571 strlcpy(linux_dirent64->d_name, bdp->d_name,
572 linuxreclen - offsetof(struct l_dirent64, d_name));
573 error = copyout(linux_dirent64, outp, linuxreclen);
574 } else if (!justone) {
575 linux_dirent = (struct l_dirent*)lbuf;
576 linux_dirent->d_ino = bdp->d_fileno;
577 linux_dirent->d_off = (cookiep)
579 : (l_off_t)(off + reclen);
580 linux_dirent->d_reclen = (l_ushort)linuxreclen;
582 * Copy d_type to last byte of l_dirent buffer
584 lbuf[linuxreclen-1] = bdp->d_type;
585 strlcpy(linux_dirent->d_name, bdp->d_name,
586 linuxreclen - offsetof(struct l_dirent, d_name)-1);
587 error = copyout(linux_dirent, outp, linuxreclen);
601 resid -= linuxreclen;
607 if (outp == (caddr_t)args->dirent)
612 nbytes = resid + linuxreclen;
615 td->td_retval[0] = nbytes - resid;
619 free(cookies, M_TEMP);
621 VOP_UNLOCK(vp, 0, td);
622 VFS_UNLOCK_GIANT(vfslocked);
630 linux_getdents(struct thread *td, struct linux_getdents_args *args)
634 if (ldebug(getdents))
635 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
638 return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
642 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
646 if (ldebug(getdents64))
647 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
650 return (getdents_common(td, args, 1));
654 * These exist mainly for hooks for doing /compat/linux translation.
658 linux_access(struct thread *td, struct linux_access_args *args)
663 /* linux convention */
664 if (args->flags & ~(F_OK | X_OK | W_OK | R_OK))
667 LCONVPATHEXIST(td, args->path, &path);
671 printf(ARGS(access, "%s, %d"), path, args->flags);
673 error = kern_access(td, path, UIO_SYSSPACE, args->flags);
680 linux_unlink(struct thread *td, struct linux_unlink_args *args)
686 LCONVPATHEXIST(td, args->path, &path);
690 printf(ARGS(unlink, "%s"), path);
693 error = kern_unlink(td, path, UIO_SYSSPACE);
695 /* Introduce POSIX noncompliant behaviour of Linux */
696 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0)
697 if (S_ISDIR(st.st_mode))
704 linux_chdir(struct thread *td, struct linux_chdir_args *args)
709 LCONVPATHEXIST(td, args->path, &path);
713 printf(ARGS(chdir, "%s"), path);
715 error = kern_chdir(td, path, UIO_SYSSPACE);
721 linux_chmod(struct thread *td, struct linux_chmod_args *args)
726 LCONVPATHEXIST(td, args->path, &path);
730 printf(ARGS(chmod, "%s, %d"), path, args->mode);
732 error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
738 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
743 LCONVPATHCREAT(td, args->path, &path);
747 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
749 error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
755 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
760 LCONVPATHEXIST(td, args->path, &path);
764 printf(ARGS(rmdir, "%s"), path);
766 error = kern_rmdir(td, path, UIO_SYSSPACE);
772 linux_rename(struct thread *td, struct linux_rename_args *args)
777 LCONVPATHEXIST(td, args->from, &from);
778 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
779 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
787 printf(ARGS(rename, "%s, %s"), from, to);
789 error = kern_rename(td, from, to, UIO_SYSSPACE);
796 linux_symlink(struct thread *td, struct linux_symlink_args *args)
801 LCONVPATHEXIST(td, args->path, &path);
802 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
803 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
811 printf(ARGS(symlink, "%s, %s"), path, to);
813 error = kern_symlink(td, path, to, UIO_SYSSPACE);
820 linux_readlink(struct thread *td, struct linux_readlink_args *args)
825 LCONVPATHEXIST(td, args->name, &name);
828 if (ldebug(readlink))
829 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
832 error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
839 linux_truncate(struct thread *td, struct linux_truncate_args *args)
844 LCONVPATHEXIST(td, args->path, &path);
847 if (ldebug(truncate))
848 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
851 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
857 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
859 struct ftruncate_args /* {
866 nuap.length = args->length;
867 return (ftruncate(td, &nuap));
871 linux_link(struct thread *td, struct linux_link_args *args)
876 LCONVPATHEXIST(td, args->path, &path);
877 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
878 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
886 printf(ARGS(link, "%s, %s"), path, to);
888 error = kern_link(td, path, to, UIO_SYSSPACE);
895 linux_fdatasync(td, uap)
897 struct linux_fdatasync_args *uap;
899 struct fsync_args bsd;
902 return fsync(td, &bsd);
908 struct linux_pread_args *uap;
910 struct pread_args bsd;
916 bsd.nbyte = uap->nbyte;
917 bsd.offset = uap->offset;
919 error = pread(td, &bsd);
922 /* This seems to violate POSIX but linux does it */
923 if ((error = fgetvp(td, uap->fd, &vp)) != 0)
925 if (vp->v_type == VDIR) {
936 linux_pwrite(td, uap)
938 struct linux_pwrite_args *uap;
940 struct pwrite_args bsd;
944 bsd.nbyte = uap->nbyte;
945 bsd.offset = uap->offset;
946 return pwrite(td, &bsd);
950 linux_mount(struct thread *td, struct linux_mount_args *args)
953 char fstypename[MFSNAMELEN];
954 char mntonname[MNAMELEN], mntfromname[MNAMELEN];
959 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
963 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
966 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
972 printf(ARGS(mount, "%s, %s, %s"),
973 fstypename, mntfromname, mntonname);
976 if (strcmp(fstypename, "ext2") == 0) {
977 strcpy(fstypename, "ext2fs");
979 ufs.fspec = mntfromname;
980 #define DEFAULT_ROOTID -2
981 ufs.export.ex_root = DEFAULT_ROOTID;
982 ufs.export.ex_flags =
983 args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
984 } else if (strcmp(fstypename, "proc") == 0) {
985 strcpy(fstypename, "linprocfs");
993 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
995 * Linux SYNC flag is not included; the closest equivalent
996 * FreeBSD has is !ASYNC, which is our default.
998 if (args->rwflag & LINUX_MS_RDONLY)
999 fsflags |= MNT_RDONLY;
1000 if (args->rwflag & LINUX_MS_NOSUID)
1001 fsflags |= MNT_NOSUID;
1002 if (args->rwflag & LINUX_MS_NOEXEC)
1003 fsflags |= MNT_NOEXEC;
1004 if (args->rwflag & LINUX_MS_REMOUNT)
1005 fsflags |= MNT_UPDATE;
1008 if (strcmp(fstypename, "linprocfs") == 0) {
1009 error = kernel_vmount(fsflags,
1010 "fstype", fstypename,
1011 "fspath", mntonname,
1019 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1021 struct linux_umount_args args2;
1023 args2.path = args->path;
1025 return (linux_umount(td, &args2));
1029 linux_umount(struct thread *td, struct linux_umount_args *args)
1031 struct unmount_args bsd;
1033 bsd.path = args->path;
1034 bsd.flags = args->flags; /* XXX correct? */
1035 return (unmount(td, &bsd));
1039 * fcntl family of syscalls
1049 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1055 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1057 switch (linux_flock->l_type) {
1059 bsd_flock->l_type = F_RDLCK;
1062 bsd_flock->l_type = F_WRLCK;
1065 bsd_flock->l_type = F_UNLCK;
1068 bsd_flock->l_type = -1;
1071 bsd_flock->l_whence = linux_flock->l_whence;
1072 bsd_flock->l_start = (off_t)linux_flock->l_start;
1073 bsd_flock->l_len = (off_t)linux_flock->l_len;
1074 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1075 bsd_flock->l_sysid = 0;
1079 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1081 switch (bsd_flock->l_type) {
1083 linux_flock->l_type = LINUX_F_RDLCK;
1086 linux_flock->l_type = LINUX_F_WRLCK;
1089 linux_flock->l_type = LINUX_F_UNLCK;
1092 linux_flock->l_whence = bsd_flock->l_whence;
1093 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1094 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1095 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1098 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1106 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1112 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1114 switch (linux_flock->l_type) {
1116 bsd_flock->l_type = F_RDLCK;
1119 bsd_flock->l_type = F_WRLCK;
1122 bsd_flock->l_type = F_UNLCK;
1125 bsd_flock->l_type = -1;
1128 bsd_flock->l_whence = linux_flock->l_whence;
1129 bsd_flock->l_start = (off_t)linux_flock->l_start;
1130 bsd_flock->l_len = (off_t)linux_flock->l_len;
1131 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1132 bsd_flock->l_sysid = 0;
1136 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1138 switch (bsd_flock->l_type) {
1140 linux_flock->l_type = LINUX_F_RDLCK;
1143 linux_flock->l_type = LINUX_F_WRLCK;
1146 linux_flock->l_type = LINUX_F_UNLCK;
1149 linux_flock->l_whence = bsd_flock->l_whence;
1150 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1151 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1152 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1154 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1157 fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
1159 struct l_flock linux_flock;
1160 struct flock bsd_flock;
1165 switch (args->cmd) {
1167 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1170 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1173 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1176 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1177 result = td->td_retval[0];
1178 td->td_retval[0] = 0;
1179 if (result & O_RDONLY)
1180 td->td_retval[0] |= LINUX_O_RDONLY;
1181 if (result & O_WRONLY)
1182 td->td_retval[0] |= LINUX_O_WRONLY;
1183 if (result & O_RDWR)
1184 td->td_retval[0] |= LINUX_O_RDWR;
1185 if (result & O_NDELAY)
1186 td->td_retval[0] |= LINUX_O_NONBLOCK;
1187 if (result & O_APPEND)
1188 td->td_retval[0] |= LINUX_O_APPEND;
1189 if (result & O_FSYNC)
1190 td->td_retval[0] |= LINUX_O_SYNC;
1191 if (result & O_ASYNC)
1192 td->td_retval[0] |= LINUX_FASYNC;
1193 #ifdef LINUX_O_NOFOLLOW
1194 if (result & O_NOFOLLOW)
1195 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1197 #ifdef LINUX_O_DIRECT
1198 if (result & O_DIRECT)
1199 td->td_retval[0] |= LINUX_O_DIRECT;
1205 if (args->arg & LINUX_O_NDELAY)
1207 if (args->arg & LINUX_O_APPEND)
1209 if (args->arg & LINUX_O_SYNC)
1211 if (args->arg & LINUX_FASYNC)
1213 #ifdef LINUX_O_NOFOLLOW
1214 if (args->arg & LINUX_O_NOFOLLOW)
1217 #ifdef LINUX_O_DIRECT
1218 if (args->arg & LINUX_O_DIRECT)
1221 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1224 error = copyin((void *)args->arg, &linux_flock,
1225 sizeof(linux_flock));
1228 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1229 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1232 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1233 return (copyout(&linux_flock, (void *)args->arg,
1234 sizeof(linux_flock)));
1237 error = copyin((void *)args->arg, &linux_flock,
1238 sizeof(linux_flock));
1241 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1242 return (kern_fcntl(td, args->fd, F_SETLK,
1243 (intptr_t)&bsd_flock));
1245 case LINUX_F_SETLKW:
1246 error = copyin((void *)args->arg, &linux_flock,
1247 sizeof(linux_flock));
1250 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1251 return (kern_fcntl(td, args->fd, F_SETLKW,
1252 (intptr_t)&bsd_flock));
1254 case LINUX_F_GETOWN:
1255 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1257 case LINUX_F_SETOWN:
1259 * XXX some Linux applications depend on F_SETOWN having no
1260 * significant effect for pipes (SIGIO is not delivered for
1261 * pipes under Linux-2.2.35 at least).
1263 error = fget(td, args->fd, &fp);
1266 if (fp->f_type == DTYPE_PIPE) {
1272 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1279 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1281 struct linux_fcntl64_args args64;
1285 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1288 args64.fd = args->fd;
1289 args64.cmd = args->cmd;
1290 args64.arg = args->arg;
1291 return (fcntl_common(td, &args64));
1294 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1296 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1298 struct l_flock64 linux_flock;
1299 struct flock bsd_flock;
1303 if (ldebug(fcntl64))
1304 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1307 switch (args->cmd) {
1308 case LINUX_F_GETLK64:
1309 error = copyin((void *)args->arg, &linux_flock,
1310 sizeof(linux_flock));
1313 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1314 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1317 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1318 return (copyout(&linux_flock, (void *)args->arg,
1319 sizeof(linux_flock)));
1321 case LINUX_F_SETLK64:
1322 error = copyin((void *)args->arg, &linux_flock,
1323 sizeof(linux_flock));
1326 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1327 return (kern_fcntl(td, args->fd, F_SETLK,
1328 (intptr_t)&bsd_flock));
1330 case LINUX_F_SETLKW64:
1331 error = copyin((void *)args->arg, &linux_flock,
1332 sizeof(linux_flock));
1335 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1336 return (kern_fcntl(td, args->fd, F_SETLKW,
1337 (intptr_t)&bsd_flock));
1340 return (fcntl_common(td, args));
1342 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1345 linux_chown(struct thread *td, struct linux_chown_args *args)
1350 LCONVPATHEXIST(td, args->path, &path);
1354 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1356 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1362 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1367 LCONVPATHEXIST(td, args->path, &path);
1371 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1373 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);