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>
37 #include <sys/dirent.h>
38 #include <sys/fcntl.h>
40 #include <sys/filedesc.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysproto.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
55 #include <security/mac/mac_framework.h>
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/ufsmount.h>
62 #include <machine/../linux32/linux.h>
63 #include <machine/../linux32/linux32_proto.h>
65 #include <machine/../linux/linux.h>
66 #include <machine/../linux/linux_proto.h>
68 #include <compat/linux/linux_util.h>
69 #include <compat/linux/linux_file.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, int dirfd, char *path, int l_flags, int mode)
93 struct proc *p = td->td_proc;
99 switch (l_flags & LINUX_O_ACCMODE) {
101 bsd_flags |= O_WRONLY;
107 bsd_flags |= O_RDONLY;
109 if (l_flags & LINUX_O_NDELAY)
110 bsd_flags |= O_NONBLOCK;
111 if (l_flags & LINUX_O_APPEND)
112 bsd_flags |= O_APPEND;
113 if (l_flags & LINUX_O_SYNC)
114 bsd_flags |= O_FSYNC;
115 if (l_flags & LINUX_O_NONBLOCK)
116 bsd_flags |= O_NONBLOCK;
117 if (l_flags & LINUX_FASYNC)
118 bsd_flags |= O_ASYNC;
119 if (l_flags & LINUX_O_CREAT)
120 bsd_flags |= O_CREAT;
121 if (l_flags & LINUX_O_TRUNC)
122 bsd_flags |= O_TRUNC;
123 if (l_flags & LINUX_O_EXCL)
125 if (l_flags & LINUX_O_NOCTTY)
126 bsd_flags |= O_NOCTTY;
127 if (l_flags & LINUX_O_DIRECT)
128 bsd_flags |= O_DIRECT;
129 if (l_flags & LINUX_O_NOFOLLOW)
130 bsd_flags |= O_NOFOLLOW;
131 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
133 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
136 fd = td->td_retval[0];
138 * XXX In between kern_open() and fget(), another process
139 * having the same filedesc could use that fd without
142 error = fget(td, fd, &fp);
144 sx_slock(&proctree_lock);
146 if (!(bsd_flags & O_NOCTTY) &&
147 SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
149 sx_unlock(&proctree_lock);
150 if (fp->f_type == DTYPE_VNODE)
151 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
155 sx_sunlock(&proctree_lock);
157 if (l_flags & LINUX_O_DIRECTORY) {
158 if (fp->f_type != DTYPE_VNODE ||
159 fp->f_vnode->v_type != VDIR) {
165 * XXX as above, fdrop()/kern_close() pair is racy.
174 printf(LMSG("open returns error %d"), error);
181 linux_openat(struct thread *td, struct linux_openat_args *args)
186 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
187 if (args->flags & LINUX_O_CREAT)
188 LCONVPATH_AT(td, args->filename, &path, 1, dfd);
190 LCONVPATH_AT(td, args->filename, &path, 0, dfd);
193 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd,
194 path, args->flags, args->mode);
196 return (linux_common_open(td, dfd, path, args->flags, args->mode));
200 linux_open(struct thread *td, struct linux_open_args *args)
204 if (args->flags & LINUX_O_CREAT)
205 LCONVPATHCREAT(td, args->path, &path);
207 LCONVPATHEXIST(td, args->path, &path);
211 printf(ARGS(open, "%s, 0x%x, 0x%x"),
212 path, args->flags, args->mode);
215 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
219 linux_lseek(struct thread *td, struct linux_lseek_args *args)
222 struct lseek_args /* {
232 printf(ARGS(lseek, "%d, %ld, %d"),
233 args->fdes, (long)args->off, args->whence);
235 tmp_args.fd = args->fdes;
236 tmp_args.offset = (off_t)args->off;
237 tmp_args.whence = args->whence;
238 error = lseek(td, &tmp_args);
243 linux_llseek(struct thread *td, struct linux_llseek_args *args)
245 struct lseek_args bsd_args;
251 printf(ARGS(llseek, "%d, %d:%d, %d"),
252 args->fd, args->ohigh, args->olow, args->whence);
254 off = (args->olow) | (((off_t) args->ohigh) << 32);
256 bsd_args.fd = args->fd;
257 bsd_args.offset = off;
258 bsd_args.whence = args->whence;
260 if ((error = lseek(td, &bsd_args)))
263 if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
266 td->td_retval[0] = 0;
271 linux_readdir(struct thread *td, struct linux_readdir_args *args)
273 struct linux_getdents_args lda;
276 lda.dent = args->dent;
278 return linux_getdents(td, &lda);
282 * Note that linux_getdents(2) and linux_getdents64(2) have the same
283 * arguments. They only differ in the definition of struct dirent they
284 * operate on. We use this to common the code, with the exception of
285 * accessing struct dirent. Note that linux_readdir(2) is implemented
286 * by means of linux_getdents(2). In this case we never operate on
287 * struct dirent64 and thus don't need to handle it...
294 char d_name[LINUX_NAME_MAX + 1];
302 char d_name[LINUX_NAME_MAX + 1];
306 * Linux uses the last byte in the dirent buffer to store d_type,
307 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
309 #define LINUX_RECLEN(namlen) \
310 roundup((offsetof(struct l_dirent, d_name) + (namlen) + 2), \
313 #define LINUX_RECLEN64(namlen) \
314 roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1), \
317 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \
318 LINUX_RECLEN64(LINUX_NAME_MAX))
319 #define LINUX_DIRBLKSIZ 512
322 getdents_common(struct thread *td, struct linux_getdents64_args *args,
327 caddr_t inp, buf; /* BSD-format */
328 int len, reclen; /* BSD-format */
329 caddr_t outp; /* Linux-format */
330 int resid, linuxreclen=0; /* Linux-format */
331 caddr_t lbuf; /* Linux-format */
336 struct l_dirent *linux_dirent;
337 struct l_dirent64 *linux_dirent64;
338 int buflen, error, eofflag, nbytes, justone;
339 u_long *cookies = NULL, *cookiep;
340 int ncookies, vfslocked;
342 nbytes = args->count;
344 /* readdir(2) case. Always struct dirent. */
347 nbytes = sizeof(*linux_dirent);
352 if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
355 if ((fp->f_flag & FREAD) == 0) {
361 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
362 if (vp->v_type != VDIR) {
363 VFS_UNLOCK_GIANT(vfslocked);
370 buflen = max(LINUX_DIRBLKSIZ, nbytes);
371 buflen = min(buflen, MAXBSIZE);
372 buf = malloc(buflen, M_TEMP, M_WAITOK);
373 lbuf = malloc(LINUX_MAXRECLEN, M_TEMP, M_WAITOK | M_ZERO);
374 vn_lock(vp, LK_SHARED | LK_RETRY);
378 aiov.iov_len = buflen;
379 auio.uio_iov = &aiov;
381 auio.uio_rw = UIO_READ;
382 auio.uio_segflg = UIO_SYSSPACE;
384 auio.uio_resid = buflen;
385 auio.uio_offset = off;
388 free(cookies, M_TEMP);
394 * Do directory search MAC check using non-cached credentials.
396 if ((error = mac_vnode_check_readdir(td->td_ucred, vp)))
399 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
404 outp = (caddr_t)args->dirent;
406 if ((len = buflen - auio.uio_resid) <= 0)
413 * When using cookies, the vfs has the option of reading from
414 * a different offset than that supplied (UFS truncates the
415 * offset to a block boundary to make sure that it never reads
416 * partway through a directory entry, even if the directory
417 * has been compacted).
419 while (len > 0 && ncookies > 0 && *cookiep <= off) {
420 bdp = (struct dirent *) inp;
421 len -= bdp->d_reclen;
422 inp += bdp->d_reclen;
429 if (cookiep && ncookies == 0)
431 bdp = (struct dirent *) inp;
432 reclen = bdp->d_reclen;
438 if (bdp->d_fileno == 0) {
450 linuxreclen = (is64bit)
451 ? LINUX_RECLEN64(bdp->d_namlen)
452 : LINUX_RECLEN(bdp->d_namlen);
454 if (reclen > len || resid < linuxreclen) {
460 /* readdir(2) case. */
461 linux_dirent = (struct l_dirent*)lbuf;
462 linux_dirent->d_ino = bdp->d_fileno;
463 linux_dirent->d_off = (l_off_t)linuxreclen;
464 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen;
465 strlcpy(linux_dirent->d_name, bdp->d_name,
466 linuxreclen - offsetof(struct l_dirent, d_name));
467 error = copyout(linux_dirent, outp, linuxreclen);
470 linux_dirent64 = (struct l_dirent64*)lbuf;
471 linux_dirent64->d_ino = bdp->d_fileno;
472 linux_dirent64->d_off = (cookiep)
474 : (l_off_t)(off + reclen);
475 linux_dirent64->d_reclen = (l_ushort)linuxreclen;
476 linux_dirent64->d_type = bdp->d_type;
477 strlcpy(linux_dirent64->d_name, bdp->d_name,
478 linuxreclen - offsetof(struct l_dirent64, d_name));
479 error = copyout(linux_dirent64, outp, linuxreclen);
480 } else if (!justone) {
481 linux_dirent = (struct l_dirent*)lbuf;
482 linux_dirent->d_ino = bdp->d_fileno;
483 linux_dirent->d_off = (cookiep)
485 : (l_off_t)(off + reclen);
486 linux_dirent->d_reclen = (l_ushort)linuxreclen;
488 * Copy d_type to last byte of l_dirent buffer
490 lbuf[linuxreclen-1] = bdp->d_type;
491 strlcpy(linux_dirent->d_name, bdp->d_name,
492 linuxreclen - offsetof(struct l_dirent, d_name)-1);
493 error = copyout(linux_dirent, outp, linuxreclen);
507 resid -= linuxreclen;
513 if (outp == (caddr_t)args->dirent)
518 nbytes = resid + linuxreclen;
521 td->td_retval[0] = nbytes - resid;
525 free(cookies, M_TEMP);
528 VFS_UNLOCK_GIANT(vfslocked);
536 linux_getdents(struct thread *td, struct linux_getdents_args *args)
540 if (ldebug(getdents))
541 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
544 return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
548 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
552 if (ldebug(getdents64))
553 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
556 return (getdents_common(td, args, 1));
560 * These exist mainly for hooks for doing /compat/linux translation.
564 linux_access(struct thread *td, struct linux_access_args *args)
569 /* linux convention */
570 if (args->flags & ~(F_OK | X_OK | W_OK | R_OK))
573 LCONVPATHEXIST(td, args->path, &path);
577 printf(ARGS(access, "%s, %d"), path, args->flags);
579 error = kern_access(td, path, UIO_SYSSPACE, args->flags);
586 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
591 /* linux convention */
592 if (args->mode & ~(F_OK | X_OK | W_OK | R_OK))
595 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
596 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
600 printf(ARGS(access, "%s, %d"), path, args->mode);
603 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0 /* XXX */,
611 linux_unlink(struct thread *td, struct linux_unlink_args *args)
617 LCONVPATHEXIST(td, args->path, &path);
621 printf(ARGS(unlink, "%s"), path);
624 error = kern_unlink(td, path, UIO_SYSSPACE);
626 /* Introduce POSIX noncompliant behaviour of Linux */
627 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0)
628 if (S_ISDIR(st.st_mode))
635 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
641 if (args->flag & ~LINUX_AT_REMOVEDIR)
644 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
645 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
648 if (ldebug(unlinkat))
649 printf(ARGS(unlinkat, "%s"), path);
652 if (args->flag & LINUX_AT_REMOVEDIR)
653 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
655 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE);
656 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
657 /* Introduce POSIX noncompliant behaviour of Linux */
658 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
659 UIO_SYSSPACE, &st) == 0 && S_ISDIR(st.st_mode))
666 linux_chdir(struct thread *td, struct linux_chdir_args *args)
671 LCONVPATHEXIST(td, args->path, &path);
675 printf(ARGS(chdir, "%s"), path);
677 error = kern_chdir(td, path, UIO_SYSSPACE);
683 linux_chmod(struct thread *td, struct linux_chmod_args *args)
688 LCONVPATHEXIST(td, args->path, &path);
692 printf(ARGS(chmod, "%s, %d"), path, args->mode);
694 error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
700 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
705 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
706 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
709 if (ldebug(fchmodat))
710 printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
713 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
719 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
724 LCONVPATHCREAT(td, args->path, &path);
728 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
730 error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
736 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
741 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
742 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
746 printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
748 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
754 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
759 LCONVPATHEXIST(td, args->path, &path);
763 printf(ARGS(rmdir, "%s"), path);
765 error = kern_rmdir(td, path, UIO_SYSSPACE);
771 linux_rename(struct thread *td, struct linux_rename_args *args)
776 LCONVPATHEXIST(td, args->from, &from);
777 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
778 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
786 printf(ARGS(rename, "%s, %s"), from, to);
788 error = kern_rename(td, from, to, UIO_SYSSPACE);
795 linux_renameat(struct thread *td, struct linux_renameat_args *args)
798 int error, olddfd, newdfd;
800 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
801 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
802 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
803 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
804 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
811 if (ldebug(renameat))
812 printf(ARGS(renameat, "%s, %s"), from, to);
814 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
821 linux_symlink(struct thread *td, struct linux_symlink_args *args)
826 LCONVPATHEXIST(td, args->path, &path);
827 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
828 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
836 printf(ARGS(symlink, "%s, %s"), path, to);
838 error = kern_symlink(td, path, to, UIO_SYSSPACE);
845 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
850 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
851 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd);
852 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
853 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
860 if (ldebug(symlinkat))
861 printf(ARGS(symlinkat, "%s, %s"), path, to);
864 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
871 linux_readlink(struct thread *td, struct linux_readlink_args *args)
876 LCONVPATHEXIST(td, args->name, &name);
879 if (ldebug(readlink))
880 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
883 error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
890 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
895 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
896 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
899 if (ldebug(readlinkat))
900 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
904 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
905 UIO_USERSPACE, args->bufsiz);
911 linux_truncate(struct thread *td, struct linux_truncate_args *args)
916 LCONVPATHEXIST(td, args->path, &path);
919 if (ldebug(truncate))
920 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
923 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
929 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
934 LCONVPATHEXIST(td, args->path, &path);
937 if (ldebug(truncate64))
938 printf(ARGS(truncate64, "%s, %jd"), path, args->length);
941 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
946 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
948 struct ftruncate_args /* {
955 nuap.length = args->length;
956 return (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_link(td, path, to, UIO_SYSSPACE);
984 linux_linkat(struct thread *td, struct linux_linkat_args *args)
987 int error, olddfd, newdfd;
990 * They really introduced flags argument which is forbidden to
993 if (args->flags != 0)
996 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
997 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
998 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
999 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
1000 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
1008 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
1009 args->newdfd, to, args->flags);
1012 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, FOLLOW);
1019 linux_fdatasync(td, uap)
1021 struct linux_fdatasync_args *uap;
1023 struct fsync_args bsd;
1026 return fsync(td, &bsd);
1030 linux_pread(td, uap)
1032 struct linux_pread_args *uap;
1034 struct pread_args bsd;
1040 bsd.nbyte = uap->nbyte;
1041 bsd.offset = uap->offset;
1043 error = pread(td, &bsd);
1046 /* This seems to violate POSIX but linux does it */
1047 if ((error = fgetvp(td, uap->fd, &vp)) != 0)
1049 if (vp->v_type == VDIR) {
1060 linux_pwrite(td, uap)
1062 struct linux_pwrite_args *uap;
1064 struct pwrite_args bsd;
1068 bsd.nbyte = uap->nbyte;
1069 bsd.offset = uap->offset;
1070 return pwrite(td, &bsd);
1074 linux_mount(struct thread *td, struct linux_mount_args *args)
1076 struct ufs_args ufs;
1077 char fstypename[MFSNAMELEN];
1078 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");
1103 ufs.fspec = mntfromname;
1104 #define DEFAULT_ROOTID -2
1105 ufs.export.ex_root = DEFAULT_ROOTID;
1106 ufs.export.ex_flags =
1107 args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
1108 } else if (strcmp(fstypename, "proc") == 0) {
1109 strcpy(fstypename, "linprocfs");
1111 } else if (strcmp(fstypename, "vfat") == 0) {
1112 strcpy(fstypename, "msdosfs");
1120 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1122 * Linux SYNC flag is not included; the closest equivalent
1123 * FreeBSD has is !ASYNC, which is our default.
1125 if (args->rwflag & LINUX_MS_RDONLY)
1126 fsflags |= MNT_RDONLY;
1127 if (args->rwflag & LINUX_MS_NOSUID)
1128 fsflags |= MNT_NOSUID;
1129 if (args->rwflag & LINUX_MS_NOEXEC)
1130 fsflags |= MNT_NOEXEC;
1131 if (args->rwflag & LINUX_MS_REMOUNT)
1132 fsflags |= MNT_UPDATE;
1135 if (strcmp(fstypename, "linprocfs") == 0) {
1136 error = kernel_vmount(fsflags,
1137 "fstype", fstypename,
1138 "fspath", mntonname,
1140 } else if (strcmp(fstypename, "msdosfs") == 0) {
1141 error = kernel_vmount(fsflags,
1142 "fstype", fstypename,
1143 "fspath", mntonname,
1144 "from", mntfromname,
1152 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1154 struct linux_umount_args args2;
1156 args2.path = args->path;
1158 return (linux_umount(td, &args2));
1162 linux_umount(struct thread *td, struct linux_umount_args *args)
1164 struct unmount_args bsd;
1166 bsd.path = args->path;
1167 bsd.flags = args->flags; /* XXX correct? */
1168 return (unmount(td, &bsd));
1172 * fcntl family of syscalls
1182 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1188 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1190 switch (linux_flock->l_type) {
1192 bsd_flock->l_type = F_RDLCK;
1195 bsd_flock->l_type = F_WRLCK;
1198 bsd_flock->l_type = F_UNLCK;
1201 bsd_flock->l_type = -1;
1204 bsd_flock->l_whence = linux_flock->l_whence;
1205 bsd_flock->l_start = (off_t)linux_flock->l_start;
1206 bsd_flock->l_len = (off_t)linux_flock->l_len;
1207 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1208 bsd_flock->l_sysid = 0;
1212 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1214 switch (bsd_flock->l_type) {
1216 linux_flock->l_type = LINUX_F_RDLCK;
1219 linux_flock->l_type = LINUX_F_WRLCK;
1222 linux_flock->l_type = LINUX_F_UNLCK;
1225 linux_flock->l_whence = bsd_flock->l_whence;
1226 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1227 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1228 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1231 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1239 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1245 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1247 switch (linux_flock->l_type) {
1249 bsd_flock->l_type = F_RDLCK;
1252 bsd_flock->l_type = F_WRLCK;
1255 bsd_flock->l_type = F_UNLCK;
1258 bsd_flock->l_type = -1;
1261 bsd_flock->l_whence = linux_flock->l_whence;
1262 bsd_flock->l_start = (off_t)linux_flock->l_start;
1263 bsd_flock->l_len = (off_t)linux_flock->l_len;
1264 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1265 bsd_flock->l_sysid = 0;
1269 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1271 switch (bsd_flock->l_type) {
1273 linux_flock->l_type = LINUX_F_RDLCK;
1276 linux_flock->l_type = LINUX_F_WRLCK;
1279 linux_flock->l_type = LINUX_F_UNLCK;
1282 linux_flock->l_whence = bsd_flock->l_whence;
1283 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1284 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1285 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1287 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1290 fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
1292 struct l_flock linux_flock;
1293 struct flock bsd_flock;
1298 switch (args->cmd) {
1300 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1303 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1306 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1309 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1310 result = td->td_retval[0];
1311 td->td_retval[0] = 0;
1312 if (result & O_RDONLY)
1313 td->td_retval[0] |= LINUX_O_RDONLY;
1314 if (result & O_WRONLY)
1315 td->td_retval[0] |= LINUX_O_WRONLY;
1316 if (result & O_RDWR)
1317 td->td_retval[0] |= LINUX_O_RDWR;
1318 if (result & O_NDELAY)
1319 td->td_retval[0] |= LINUX_O_NONBLOCK;
1320 if (result & O_APPEND)
1321 td->td_retval[0] |= LINUX_O_APPEND;
1322 if (result & O_FSYNC)
1323 td->td_retval[0] |= LINUX_O_SYNC;
1324 if (result & O_ASYNC)
1325 td->td_retval[0] |= LINUX_FASYNC;
1326 #ifdef LINUX_O_NOFOLLOW
1327 if (result & O_NOFOLLOW)
1328 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1330 #ifdef LINUX_O_DIRECT
1331 if (result & O_DIRECT)
1332 td->td_retval[0] |= LINUX_O_DIRECT;
1338 if (args->arg & LINUX_O_NDELAY)
1340 if (args->arg & LINUX_O_APPEND)
1342 if (args->arg & LINUX_O_SYNC)
1344 if (args->arg & LINUX_FASYNC)
1346 #ifdef LINUX_O_NOFOLLOW
1347 if (args->arg & LINUX_O_NOFOLLOW)
1350 #ifdef LINUX_O_DIRECT
1351 if (args->arg & LINUX_O_DIRECT)
1354 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1357 error = copyin((void *)args->arg, &linux_flock,
1358 sizeof(linux_flock));
1361 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1362 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1365 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1366 return (copyout(&linux_flock, (void *)args->arg,
1367 sizeof(linux_flock)));
1370 error = copyin((void *)args->arg, &linux_flock,
1371 sizeof(linux_flock));
1374 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1375 return (kern_fcntl(td, args->fd, F_SETLK,
1376 (intptr_t)&bsd_flock));
1378 case LINUX_F_SETLKW:
1379 error = copyin((void *)args->arg, &linux_flock,
1380 sizeof(linux_flock));
1383 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1384 return (kern_fcntl(td, args->fd, F_SETLKW,
1385 (intptr_t)&bsd_flock));
1387 case LINUX_F_GETOWN:
1388 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1390 case LINUX_F_SETOWN:
1392 * XXX some Linux applications depend on F_SETOWN having no
1393 * significant effect for pipes (SIGIO is not delivered for
1394 * pipes under Linux-2.2.35 at least).
1396 error = fget(td, args->fd, &fp);
1399 if (fp->f_type == DTYPE_PIPE) {
1405 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1412 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1414 struct linux_fcntl64_args args64;
1418 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1421 args64.fd = args->fd;
1422 args64.cmd = args->cmd;
1423 args64.arg = args->arg;
1424 return (fcntl_common(td, &args64));
1427 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1429 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1431 struct l_flock64 linux_flock;
1432 struct flock bsd_flock;
1436 if (ldebug(fcntl64))
1437 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1440 switch (args->cmd) {
1441 case LINUX_F_GETLK64:
1442 error = copyin((void *)args->arg, &linux_flock,
1443 sizeof(linux_flock));
1446 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1447 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1450 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1451 return (copyout(&linux_flock, (void *)args->arg,
1452 sizeof(linux_flock)));
1454 case LINUX_F_SETLK64:
1455 error = copyin((void *)args->arg, &linux_flock,
1456 sizeof(linux_flock));
1459 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1460 return (kern_fcntl(td, args->fd, F_SETLK,
1461 (intptr_t)&bsd_flock));
1463 case LINUX_F_SETLKW64:
1464 error = copyin((void *)args->arg, &linux_flock,
1465 sizeof(linux_flock));
1468 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1469 return (kern_fcntl(td, args->fd, F_SETLKW,
1470 (intptr_t)&bsd_flock));
1473 return (fcntl_common(td, args));
1475 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1478 linux_chown(struct thread *td, struct linux_chown_args *args)
1483 LCONVPATHEXIST(td, args->path, &path);
1487 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1489 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1495 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1498 int error, dfd, follow;
1500 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1503 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1504 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1507 if (ldebug(fchownat))
1508 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1511 follow = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1512 AT_SYMLINK_NOFOLLOW;
1513 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1520 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1525 LCONVPATHEXIST(td, args->path, &path);
1529 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1531 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);