/*-
* Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Frank van der Linden and Eric Haszlakiewicz.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Functions in multiarch:
* linux_sys_llseek : linux_llseek.c
*/
static int bsd_to_linux_ioflags(int);
#if !defined(__aarch64__) && !defined(__amd64__)
static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
#endif
conv_linux_flock(linux, flock)
/*
* Some file-related calls are handled here. The usual flag conversion
* an structure conversion is done, and alternate emul path searching.
*/
/*
* The next two functions convert between the Linux and NetBSD values
* of the flags used in open(2) and fcntl(2).
*/
int
linux_to_bsd_ioflags(int lflags)
{
int res = 0;
res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
res |= cvtto_bsd_mask(lflags, LINUX_O_NONBLOCK, O_NONBLOCK);
res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECT, O_DIRECT);
res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECTORY, O_DIRECTORY);
res |= cvtto_bsd_mask(lflags, LINUX_O_NOFOLLOW, O_NOFOLLOW);
res |= cvtto_bsd_mask(lflags, LINUX_O_CLOEXEC, O_CLOEXEC);
return res;
}
static int
bsd_to_linux_ioflags(int bflags)
{
int res = 0;
res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
res |= cvtto_linux_mask(bflags, O_NONBLOCK, LINUX_O_NONBLOCK);
res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
res |= cvtto_linux_mask(bflags, O_DIRECT, LINUX_O_DIRECT);
res |= cvtto_linux_mask(bflags, O_DIRECTORY, LINUX_O_DIRECTORY);
res |= cvtto_linux_mask(bflags, O_NOFOLLOW, LINUX_O_NOFOLLOW);
res |= cvtto_linux_mask(bflags, O_CLOEXEC, LINUX_O_CLOEXEC);
return res;
}
static inline off_t
linux_hilo_to_off_t(unsigned long hi, unsigned long lo)
{
#ifdef _LP64
/*
* Linux discards the "hi" portion on LP64 platforms; even though
* glibc puts of the upper 32-bits of the offset into the "hi"
* argument regardless, the "lo" argument has all the bits in
* this case.
*/
(void) hi;
return (off_t)lo;
#else
return (((off_t)hi) << 32) | lo;
#endif /* _LP64 */
}
#if !defined(__aarch64__)
/*
* creat(2) is an obsolete function, but it's present as a Linux
* system call, so let's deal with it.
*
* Note: On the Alpha this doesn't really exist in Linux, but it's defined
* in syscalls.master anyway so this doesn't have to be special cased.
*
* Just call open(2) with the TRUNC, CREAT and WRONLY flags.
*/
int
linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap,
register_t *retval)
{
/* {
syscallarg(const char *) path;
syscallarg(linux_umode_t) mode;
} */
struct sys_open_args oa;
static void
linux_open_ctty(struct lwp *l, int flags, int fd)
{
struct proc *p = l->l_proc;
/*
* this bit from sunos_misc.c (and svr4_fcntl.c).
* If we are a session leader, and we don't have a controlling
* terminal yet, and the O_NOCTTY flag is not set, try to make
* this the controlling terminal.
*/
if (!(flags & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
file_t *fp;
fp = fd_getfile(fd);
/* ignore any error, just give it a try */
if (fp != NULL) {
if (fp->f_type == DTYPE_VNODE) {
(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
}
fd_putfile(fd);
}
}
}
/*
* open(2). Take care of the different flag values, and let the
* NetBSD syscall do the real work. See if this operation
* gives the current process a controlling terminal.
* (XXX is this necessary?)
*/
int
linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap,
register_t *retval)
{
/* {
syscallarg(const char *) path;
syscallarg(int) flags;
syscallarg(linux_umode_t) mode;
} */
int error, fl;
struct sys_open_args boa;
/*
* Most actions in the fcntl() call are straightforward; simply
* pass control to the NetBSD system call. A few commands need
* conversions after the actual system call has done its work,
* because the flag values and lock structure are different.
*/
int
linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap,
register_t *retval)
{
/* {
syscallarg(int) fd;
syscallarg(int) cmd;
syscallarg(void *) arg;
} */
struct proc *p = l->l_proc;
int fd, cmd, error;
u_long val;
void *arg;
struct sys_fcntl_args fca;
file_t *fp;
struct vnode *vp;
struct vattr va;
long pgid;
struct pgrp *pgrp;
struct tty *tp;
val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
/*
* Linux seems to have same semantics for sending SIGIO to the
* read side of socket, but slightly different semantics
* for SIGIO to the write side. Rather than sending the SIGIO
* every time it's possible to write (directly) more data, it
* only sends SIGIO if last write(2) failed due to insufficient
* memory to hold the data. This is compatible enough
* with NetBSD semantics to not do anything about the
* difference.
*
* Linux does NOT send SIGIO for pipes. Deal with socketpair
* ones and DTYPE_PIPE ones. For these, we don't set
* the underlying flags (we don't pass O_ASYNC flag down
* to sys_fcntl()), but set the FASYNC flag for file descriptor,
* so that F_GETFL would report the ASYNC i/o is on.
*/
if (val & O_ASYNC) {
if (((fp1 = fd_getfile(fd)) == NULL))
return (EBADF);
if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
&& ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
|| (fp1->f_type == DTYPE_PIPE))
val &= ~O_ASYNC;
else {
/* not a pipe, do not modify anything */
fd_putfile(fd);
fp1 = NULL;
}
}
/* Now set the FASYNC flag for pipes */
if (fp1) {
if (!error) {
mutex_enter(&fp1->f_lock);
fp1->f_flag |= FASYNC;
mutex_exit(&fp1->f_lock);
}
fd_putfile(fd);
}
return (error);
}
case LINUX_F_GETLK:
do_linux_getlk(fd, cmd, arg, linux, flock);
case LINUX_F_SETLK:
case LINUX_F_SETLKW:
do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
case LINUX_F_SETOWN:
case LINUX_F_GETOWN:
/*
* We need to route fcntl() for tty descriptors around normal
* fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
* restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
* this is not a problem.
*/
if ((fp = fd_getfile(fd)) == NULL)
return EBADF;
/* Check it's a character device vnode */
if (fp->f_type != DTYPE_VNODE
|| (vp = (struct vnode *)fp->f_data) == NULL
|| vp->v_type != VCHR) {
fd_putfile(fd);
not_tty:
/* Not a tty, proceed with common fcntl() */
cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
break;
}
#if !defined(__aarch64__) && !defined(__amd64__)
/*
* Convert a NetBSD stat structure to a Linux stat structure.
* Only the order of the fields and the padding in the structure
* is different. linux_fakedev is a machine-dependent function
* which optionally converts device driver major/minor numbers
* (XXX horrible, but what can you do against code that compares
* things against constant major device numbers? sigh)
*/
static void
bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
{
/*
* The stat functions below are plain sailing. stat and lstat are handled
* by one function to avoid code duplication.
*/
int
linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap,
register_t *retval)
{
/* {
syscallarg(int) fd;
syscallarg(linux_stat *) sp;
} */
struct linux_stat tmplst;
struct stat tmpst;
int error;
/* Note: this is "newlstat" in the Linux sources */
/* (we don't bother with the old lstat currently) */
int
linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap,
register_t *retval)
{
/* {
syscallarg(const char *) path;
syscallarg(struct linux_stat *) sp;
} */
static int
linux_unlink_dircheck(const char *path)
{
struct nameidata nd;
struct pathbuf *pb;
int error;
/*
* Linux returns EISDIR if unlink(2) is called on a directory.
* We return EPERM in such cases. To emulate correct behaviour,
* check if the path points to directory and return EISDIR if this
* is the case.
*
* XXX this should really not copy in the path buffer twice...
*/
error = pathbuf_copyin(path, &pb);
if (error) {
return error;
}
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
if (namei(&nd) == 0) {
struct stat sb;
if (vn_stat(nd.ni_vp, &sb) == 0
&& S_ISDIR(sb.st_mode))
error = EISDIR;
/*
* Linux device numbers uses 8 bits for minor and 8 bits
* for major. Due to how we map our major and minor,
* this just fits into our dev_t. Just mask off the
* upper 16bit to remove any random junk.
*/
/*
* This is just fsync() for now (just as it is in the Linux kernel)
* Note: this is not implemented under Linux on Alpha and Arm
* but should still be defined in our syscalls.master.
* (syscall #148 on the arm)
*/
int
linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap,
register_t *retval)
{
/* {
syscallarg(int) fd;
} */
unsigned int flags = SCARG(uap, flags);
int error;
if (flags != 0) {
if (flags & ~LINUX_RENAME_ALL)
return EINVAL;
if ((flags & LINUX_RENAME_EXCHANGE) != 0 &&
(flags & (LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
!= 0)
return EINVAL;
/*
* Suppoting renameat2 flags without support from file systems
* becomes a messy affair cause of locks and how VOP_RENAME
* protocol is implemented. So, return EOPNOTSUPP for now.
*/
return EOPNOTSUPP;
}
if (have_off_in) {
/* Adjust user space offset */
error = copyout(&off_in, SCARG(uap, off_in), sizeof(off_t));
if (error) {
DPRINTF("%s: Error adjusting user space offset\n",
__func__);
}
goto out;
}
if (have_off_out) {
/* Adjust user space offset */
error = copyout(&off_out, SCARG(uap, off_out), sizeof(off_t));
if (error) {
DPRINTF("%s: Error adjusting user space offset\n",
__func__);
}
}
*retval = total_copied;
out:
if (buffer) {
kmem_free(buffer, LINUX_COPY_FILE_RANGE_MAX_CHUNK);
}
if (fp_out) {
fd_putfile(fd_out);
}
if (fp_in) {
fd_putfile(fd_in);
}
return error;
}
/*
* For now just return EOPNOTSUPP, this makes glibc posix_fallocate()
* to fallback to emulation.
* XXX Right now no filesystem actually implements fallocate support,
* so no need for mapping.
*/
LINUX_NOT_SUPPORTED(linux_sys_fallocate)