Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.101.2.1
      1  1.101.2.1    jruoho /*	$NetBSD: linux_file.c,v 1.101.2.1 2011/06/06 09:07:27 jruoho Exp $	*/
      2       1.23       erh 
      3       1.23       erh /*-
      4       1.93        ad  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
      5       1.23       erh  * All rights reserved.
      6       1.23       erh  *
      7       1.23       erh  * This code is derived from software contributed to The NetBSD Foundation
      8       1.25      fvdl  * by Frank van der Linden and Eric Haszlakiewicz.
      9       1.23       erh  *
     10       1.23       erh  * Redistribution and use in source and binary forms, with or without
     11       1.23       erh  * modification, are permitted provided that the following conditions
     12       1.23       erh  * are met:
     13       1.23       erh  * 1. Redistributions of source code must retain the above copyright
     14       1.23       erh  *    notice, this list of conditions and the following disclaimer.
     15       1.23       erh  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.23       erh  *    notice, this list of conditions and the following disclaimer in the
     17       1.23       erh  *    documentation and/or other materials provided with the distribution.
     18       1.23       erh  *
     19       1.23       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.23       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.23       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.23       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.23       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.23       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.23       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.23       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.23       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.23       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.23       erh  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1      fvdl  */
     31        1.1      fvdl 
     32       1.23       erh /*
     33       1.23       erh  * Functions in multiarch:
     34       1.23       erh  *	linux_sys_llseek	: linux_llseek.c
     35       1.23       erh  */
     36       1.43     lukem 
     37       1.43     lukem #include <sys/cdefs.h>
     38  1.101.2.1    jruoho __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.101.2.1 2011/06/06 09:07:27 jruoho Exp $");
     39       1.23       erh 
     40        1.1      fvdl #include <sys/param.h>
     41        1.1      fvdl #include <sys/systm.h>
     42        1.1      fvdl #include <sys/namei.h>
     43        1.1      fvdl #include <sys/proc.h>
     44        1.1      fvdl #include <sys/file.h>
     45  1.101.2.1    jruoho #include <sys/fcntl.h>
     46        1.1      fvdl #include <sys/stat.h>
     47        1.1      fvdl #include <sys/filedesc.h>
     48        1.1      fvdl #include <sys/ioctl.h>
     49        1.1      fvdl #include <sys/kernel.h>
     50        1.1      fvdl #include <sys/mount.h>
     51        1.1      fvdl #include <sys/malloc.h>
     52       1.81       dsl #include <sys/namei.h>
     53       1.13      fvdl #include <sys/vnode.h>
     54       1.13      fvdl #include <sys/tty.h>
     55       1.39      manu #include <sys/socketvar.h>
     56       1.13      fvdl #include <sys/conf.h>
     57       1.41  jdolecek #include <sys/pipe.h>
     58        1.1      fvdl 
     59        1.1      fvdl #include <sys/syscallargs.h>
     60       1.81       dsl #include <sys/vfs_syscalls.h>
     61        1.1      fvdl 
     62       1.24  christos #include <compat/linux/common/linux_types.h>
     63       1.24  christos #include <compat/linux/common/linux_signal.h>
     64       1.24  christos #include <compat/linux/common/linux_fcntl.h>
     65       1.24  christos #include <compat/linux/common/linux_util.h>
     66       1.24  christos #include <compat/linux/common/linux_machdep.h>
     67       1.84     njoly #include <compat/linux/common/linux_ipc.h>
     68       1.84     njoly #include <compat/linux/common/linux_sem.h>
     69       1.24  christos 
     70        1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     71       1.14  christos 
     72       1.87       dsl static int linux_to_bsd_ioflags(int);
     73       1.87       dsl static int bsd_to_linux_ioflags(int);
     74       1.67      manu #ifndef __amd64__
     75       1.87       dsl static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
     76       1.67      manu #endif
     77       1.14  christos 
     78       1.92       dsl conv_linux_flock(linux, flock)
     79       1.92       dsl 
     80        1.1      fvdl /*
     81        1.1      fvdl  * Some file-related calls are handled here. The usual flag conversion
     82        1.1      fvdl  * an structure conversion is done, and alternate emul path searching.
     83        1.1      fvdl  */
     84        1.1      fvdl 
     85        1.1      fvdl /*
     86        1.1      fvdl  * The next two functions convert between the Linux and NetBSD values
     87        1.1      fvdl  * of the flags used in open(2) and fcntl(2).
     88        1.1      fvdl  */
     89        1.1      fvdl static int
     90       1.88       dsl linux_to_bsd_ioflags(int lflags)
     91        1.1      fvdl {
     92        1.1      fvdl 	int res = 0;
     93        1.1      fvdl 
     94        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
     95        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
     96        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
     97        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
     98        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
     99        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
    100        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
    101        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
    102        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
    103        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
    104        1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
    105      1.100       chs 	res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECTORY, O_DIRECTORY);
    106  1.101.2.1    jruoho 	res |= cvtto_bsd_mask(lflags, LINUX_O_CLOEXEC, O_CLOEXEC);
    107        1.1      fvdl 
    108        1.1      fvdl 	return res;
    109        1.1      fvdl }
    110        1.1      fvdl 
    111        1.1      fvdl static int
    112       1.88       dsl bsd_to_linux_ioflags(int bflags)
    113        1.1      fvdl {
    114        1.1      fvdl 	int res = 0;
    115        1.1      fvdl 
    116        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    117        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    118        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    119        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    120        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    121        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    122        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    123        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    124        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    125        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    126        1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    127      1.100       chs 	res |= cvtto_linux_mask(bflags, O_DIRECTORY, LINUX_O_DIRECTORY);
    128  1.101.2.1    jruoho 	res |= cvtto_linux_mask(bflags, O_CLOEXEC, LINUX_O_CLOEXEC);
    129        1.1      fvdl 
    130        1.1      fvdl 	return res;
    131        1.1      fvdl }
    132        1.1      fvdl 
    133        1.1      fvdl /*
    134        1.1      fvdl  * creat(2) is an obsolete function, but it's present as a Linux
    135        1.1      fvdl  * system call, so let's deal with it.
    136        1.1      fvdl  *
    137       1.23       erh  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    138       1.23       erh  * in syscalls.master anyway so this doesn't have to be special cased.
    139       1.23       erh  *
    140        1.1      fvdl  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    141        1.1      fvdl  */
    142        1.1      fvdl int
    143       1.90       dsl linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap, register_t *retval)
    144       1.11   thorpej {
    145       1.90       dsl 	/* {
    146       1.27  christos 		syscallarg(const char *) path;
    147        1.1      fvdl 		syscallarg(int) mode;
    148       1.90       dsl 	} */
    149       1.12   mycroft 	struct sys_open_args oa;
    150        1.1      fvdl 
    151        1.1      fvdl 	SCARG(&oa, path) = SCARG(uap, path);
    152        1.1      fvdl 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    153        1.1      fvdl 	SCARG(&oa, mode) = SCARG(uap, mode);
    154       1.12   mycroft 
    155       1.56   thorpej 	return sys_open(l, &oa, retval);
    156        1.1      fvdl }
    157        1.1      fvdl 
    158        1.1      fvdl /*
    159        1.1      fvdl  * open(2). Take care of the different flag values, and let the
    160        1.1      fvdl  * NetBSD syscall do the real work. See if this operation
    161        1.1      fvdl  * gives the current process a controlling terminal.
    162        1.1      fvdl  * (XXX is this necessary?)
    163        1.1      fvdl  */
    164        1.1      fvdl int
    165       1.90       dsl linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap, register_t *retval)
    166       1.11   thorpej {
    167       1.90       dsl 	/* {
    168       1.27  christos 		syscallarg(const char *) path;
    169        1.1      fvdl 		syscallarg(int) flags;
    170        1.1      fvdl 		syscallarg(int) mode;
    171       1.90       dsl 	} */
    172       1.56   thorpej 	struct proc *p = l->l_proc;
    173        1.1      fvdl 	int error, fl;
    174       1.12   mycroft 	struct sys_open_args boa;
    175        1.1      fvdl 
    176        1.2      fvdl 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    177        1.1      fvdl 
    178        1.1      fvdl 	SCARG(&boa, path) = SCARG(uap, path);
    179        1.1      fvdl 	SCARG(&boa, flags) = fl;
    180        1.1      fvdl 	SCARG(&boa, mode) = SCARG(uap, mode);
    181        1.2      fvdl 
    182       1.56   thorpej 	if ((error = sys_open(l, &boa, retval)))
    183        1.1      fvdl 		return error;
    184        1.1      fvdl 
    185        1.1      fvdl 	/*
    186        1.1      fvdl 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    187        1.1      fvdl 	 * If we are a session leader, and we don't have a controlling
    188        1.1      fvdl 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    189        1.1      fvdl 	 * this the controlling terminal.
    190       1.65     perry 	 */
    191       1.77        ad         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    192       1.93        ad                 file_t *fp;
    193       1.38   thorpej 
    194       1.93        ad 		fp = fd_getfile(*retval);
    195        1.1      fvdl 
    196        1.1      fvdl                 /* ignore any error, just give it a try */
    197       1.57      yamt                 if (fp != NULL) {
    198       1.57      yamt 			if (fp->f_type == DTYPE_VNODE) {
    199       1.93        ad 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
    200       1.57      yamt 			}
    201       1.93        ad 			fd_putfile(*retval);
    202       1.57      yamt 		}
    203        1.1      fvdl         }
    204        1.1      fvdl 	return 0;
    205        1.1      fvdl }
    206        1.1      fvdl 
    207        1.1      fvdl /*
    208        1.1      fvdl  * Most actions in the fcntl() call are straightforward; simply
    209        1.1      fvdl  * pass control to the NetBSD system call. A few commands need
    210        1.1      fvdl  * conversions after the actual system call has done its work,
    211        1.1      fvdl  * because the flag values and lock structure are different.
    212        1.1      fvdl  */
    213        1.1      fvdl int
    214       1.90       dsl linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap, register_t *retval)
    215       1.11   thorpej {
    216       1.90       dsl 	/* {
    217        1.1      fvdl 		syscallarg(int) fd;
    218        1.1      fvdl 		syscallarg(int) cmd;
    219        1.1      fvdl 		syscallarg(void *) arg;
    220       1.90       dsl 	} */
    221       1.56   thorpej 	struct proc *p = l->l_proc;
    222       1.23       erh 	int fd, cmd, error;
    223       1.23       erh 	u_long val;
    224       1.83       dsl 	void *arg;
    225       1.12   mycroft 	struct sys_fcntl_args fca;
    226       1.93        ad 	file_t *fp;
    227       1.13      fvdl 	struct vnode *vp;
    228       1.13      fvdl 	struct vattr va;
    229       1.13      fvdl 	long pgid;
    230       1.13      fvdl 	struct pgrp *pgrp;
    231       1.94        ad 	struct tty *tp;
    232        1.1      fvdl 
    233        1.1      fvdl 	fd = SCARG(uap, fd);
    234        1.1      fvdl 	cmd = SCARG(uap, cmd);
    235       1.92       dsl 	arg = SCARG(uap, arg);
    236        1.1      fvdl 
    237        1.1      fvdl 	switch (cmd) {
    238       1.92       dsl 
    239        1.1      fvdl 	case LINUX_F_DUPFD:
    240        1.1      fvdl 		cmd = F_DUPFD;
    241        1.1      fvdl 		break;
    242       1.92       dsl 
    243        1.1      fvdl 	case LINUX_F_GETFD:
    244        1.1      fvdl 		cmd = F_GETFD;
    245        1.1      fvdl 		break;
    246       1.92       dsl 
    247        1.1      fvdl 	case LINUX_F_SETFD:
    248        1.1      fvdl 		cmd = F_SETFD;
    249        1.1      fvdl 		break;
    250       1.92       dsl 
    251        1.1      fvdl 	case LINUX_F_GETFL:
    252        1.1      fvdl 		SCARG(&fca, fd) = fd;
    253        1.1      fvdl 		SCARG(&fca, cmd) = F_GETFL;
    254        1.1      fvdl 		SCARG(&fca, arg) = arg;
    255       1.56   thorpej 		if ((error = sys_fcntl(l, &fca, retval)))
    256        1.1      fvdl 			return error;
    257        1.1      fvdl 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    258        1.1      fvdl 		return 0;
    259       1.92       dsl 
    260       1.41  jdolecek 	case LINUX_F_SETFL: {
    261       1.93        ad 		file_t	*fp1 = NULL;
    262       1.41  jdolecek 
    263       1.23       erh 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    264       1.39      manu 		/*
    265       1.41  jdolecek 		 * Linux seems to have same semantics for sending SIGIO to the
    266       1.64       abs 		 * read side of socket, but slightly different semantics
    267       1.41  jdolecek 		 * for SIGIO to the write side.  Rather than sending the SIGIO
    268       1.41  jdolecek 		 * every time it's possible to write (directly) more data, it
    269       1.41  jdolecek 		 * only sends SIGIO if last write(2) failed due to insufficient
    270       1.41  jdolecek 		 * memory to hold the data. This is compatible enough
    271       1.41  jdolecek 		 * with NetBSD semantics to not do anything about the
    272       1.41  jdolecek 		 * difference.
    273       1.65     perry 		 *
    274       1.41  jdolecek 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
    275       1.41  jdolecek 		 * ones and DTYPE_PIPE ones. For these, we don't set
    276       1.41  jdolecek 		 * the underlying flags (we don't pass O_ASYNC flag down
    277       1.41  jdolecek 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
    278       1.41  jdolecek 		 * so that F_GETFL would report the ASYNC i/o is on.
    279       1.39      manu 		 */
    280       1.41  jdolecek 		if (val & O_ASYNC) {
    281       1.93        ad 			if (((fp1 = fd_getfile(fd)) == NULL))
    282       1.39      manu 			    return (EBADF);
    283       1.69  christos 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    284       1.69  christos 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    285       1.69  christos 			    || (fp1->f_type == DTYPE_PIPE))
    286       1.41  jdolecek 				val &= ~O_ASYNC;
    287       1.41  jdolecek 			else {
    288       1.41  jdolecek 				/* not a pipe, do not modify anything */
    289       1.93        ad 				fd_putfile(fd);
    290       1.69  christos 				fp1 = NULL;
    291       1.39      manu 			}
    292       1.41  jdolecek 		}
    293       1.41  jdolecek 
    294       1.41  jdolecek 		SCARG(&fca, fd) = fd;
    295       1.41  jdolecek 		SCARG(&fca, cmd) = F_SETFL;
    296       1.79  christos 		SCARG(&fca, arg) = (void *) val;
    297       1.39      manu 
    298       1.56   thorpej 		error = sys_fcntl(l, &fca, retval);
    299       1.41  jdolecek 
    300       1.41  jdolecek 		/* Now set the FASYNC flag for pipes */
    301       1.69  christos 		if (fp1) {
    302       1.93        ad 			if (!error) {
    303       1.93        ad 				mutex_enter(&fp1->f_lock);
    304       1.69  christos 				fp1->f_flag |= FASYNC;
    305       1.93        ad 				mutex_exit(&fp1->f_lock);
    306       1.93        ad 			}
    307       1.93        ad 			fd_putfile(fd);
    308       1.39      manu 		}
    309       1.41  jdolecek 
    310       1.41  jdolecek 		return (error);
    311       1.41  jdolecek 	    }
    312       1.92       dsl 
    313        1.1      fvdl 	case LINUX_F_GETLK:
    314       1.92       dsl 		do_linux_getlk(fd, cmd, arg, linux, flock);
    315       1.47  christos 
    316        1.1      fvdl 	case LINUX_F_SETLK:
    317        1.1      fvdl 	case LINUX_F_SETLKW:
    318       1.92       dsl 		do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
    319       1.47  christos 
    320        1.1      fvdl 	case LINUX_F_SETOWN:
    321       1.65     perry 	case LINUX_F_GETOWN:
    322       1.13      fvdl 		/*
    323       1.49  jdolecek 		 * We need to route fcntl() for tty descriptors around normal
    324       1.49  jdolecek 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    325       1.49  jdolecek 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    326       1.49  jdolecek 		 * this is not a problem.
    327       1.13      fvdl 		 */
    328       1.93        ad 		if ((fp = fd_getfile(fd)) == NULL)
    329       1.13      fvdl 			return EBADF;
    330       1.60  jdolecek 
    331       1.60  jdolecek 		/* Check it's a character device vnode */
    332       1.60  jdolecek 		if (fp->f_type != DTYPE_VNODE
    333       1.60  jdolecek 		    || (vp = (struct vnode *)fp->f_data) == NULL
    334       1.60  jdolecek 		    || vp->v_type != VCHR) {
    335       1.93        ad 			fd_putfile(fd);
    336       1.60  jdolecek 
    337       1.53   gehenna 	    not_tty:
    338       1.49  jdolecek 			/* Not a tty, proceed with common fcntl() */
    339       1.13      fvdl 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    340       1.49  jdolecek 			break;
    341       1.49  jdolecek 		}
    342       1.47  christos 
    343       1.86     pooka 		error = VOP_GETATTR(vp, &va, l->l_cred);
    344       1.60  jdolecek 
    345       1.93        ad 		fd_putfile(fd);
    346       1.60  jdolecek 
    347       1.60  jdolecek 		if (error)
    348       1.49  jdolecek 			return error;
    349       1.60  jdolecek 
    350       1.94        ad 		if ((tp = cdev_tty(va.va_rdev)) == NULL)
    351       1.53   gehenna 			goto not_tty;
    352       1.49  jdolecek 
    353       1.49  jdolecek 		/* set tty pg_id appropriately */
    354       1.95        ad 		mutex_enter(proc_lock);
    355       1.49  jdolecek 		if (cmd == LINUX_F_GETOWN) {
    356       1.58       dsl 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    357       1.95        ad 			mutex_exit(proc_lock);
    358       1.49  jdolecek 			return 0;
    359       1.49  jdolecek 		}
    360       1.49  jdolecek 		if ((long)arg <= 0) {
    361       1.49  jdolecek 			pgid = -(long)arg;
    362       1.49  jdolecek 		} else {
    363       1.99     rmind 			struct proc *p1 = proc_find((long)arg);
    364       1.99     rmind 			if (p1 == NULL) {
    365       1.99     rmind 				mutex_exit(proc_lock);
    366       1.49  jdolecek 				return (ESRCH);
    367       1.99     rmind 			}
    368       1.49  jdolecek 			pgid = (long)p1->p_pgrp->pg_id;
    369       1.13      fvdl 		}
    370       1.99     rmind 		pgrp = pgrp_find(pgid);
    371       1.78       dsl 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    372       1.95        ad 			mutex_exit(proc_lock);
    373       1.49  jdolecek 			return EPERM;
    374       1.78       dsl 		}
    375       1.49  jdolecek 		tp->t_pgrp = pgrp;
    376       1.95        ad 		mutex_exit(proc_lock);
    377       1.49  jdolecek 		return 0;
    378       1.47  christos 
    379        1.1      fvdl 	default:
    380        1.1      fvdl 		return EOPNOTSUPP;
    381        1.1      fvdl 	}
    382        1.1      fvdl 
    383        1.1      fvdl 	SCARG(&fca, fd) = fd;
    384        1.1      fvdl 	SCARG(&fca, cmd) = cmd;
    385        1.1      fvdl 	SCARG(&fca, arg) = arg;
    386       1.12   mycroft 
    387       1.56   thorpej 	return sys_fcntl(l, &fca, retval);
    388        1.1      fvdl }
    389        1.1      fvdl 
    390       1.67      manu #if !defined(__amd64__)
    391        1.1      fvdl /*
    392        1.1      fvdl  * Convert a NetBSD stat structure to a Linux stat structure.
    393        1.1      fvdl  * Only the order of the fields and the padding in the structure
    394        1.9      fvdl  * is different. linux_fakedev is a machine-dependent function
    395        1.9      fvdl  * which optionally converts device driver major/minor numbers
    396        1.9      fvdl  * (XXX horrible, but what can you do against code that compares
    397        1.9      fvdl  * things against constant major device numbers? sigh)
    398        1.1      fvdl  */
    399        1.1      fvdl static void
    400       1.88       dsl bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
    401        1.1      fvdl {
    402       1.12   mycroft 
    403       1.45  christos 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    404        1.1      fvdl 	lsp->lst_ino     = bsp->st_ino;
    405       1.19  christos 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    406       1.19  christos 	if (bsp->st_nlink >= (1 << 15))
    407       1.19  christos 		lsp->lst_nlink = (1 << 15) - 1;
    408       1.19  christos 	else
    409       1.19  christos 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    410        1.1      fvdl 	lsp->lst_uid     = bsp->st_uid;
    411        1.1      fvdl 	lsp->lst_gid     = bsp->st_gid;
    412       1.45  christos 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    413        1.1      fvdl 	lsp->lst_size    = bsp->st_size;
    414        1.1      fvdl 	lsp->lst_blksize = bsp->st_blksize;
    415        1.1      fvdl 	lsp->lst_blocks  = bsp->st_blocks;
    416        1.1      fvdl 	lsp->lst_atime   = bsp->st_atime;
    417        1.1      fvdl 	lsp->lst_mtime   = bsp->st_mtime;
    418        1.1      fvdl 	lsp->lst_ctime   = bsp->st_ctime;
    419       1.66  christos #ifdef LINUX_STAT_HAS_NSEC
    420       1.66  christos 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    421       1.66  christos 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    422       1.66  christos 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    423       1.66  christos #endif
    424        1.1      fvdl }
    425        1.1      fvdl 
    426        1.1      fvdl /*
    427        1.1      fvdl  * The stat functions below are plain sailing. stat and lstat are handled
    428        1.1      fvdl  * by one function to avoid code duplication.
    429        1.1      fvdl  */
    430        1.1      fvdl int
    431       1.90       dsl linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap, register_t *retval)
    432       1.11   thorpej {
    433       1.90       dsl 	/* {
    434        1.1      fvdl 		syscallarg(int) fd;
    435        1.1      fvdl 		syscallarg(linux_stat *) sp;
    436       1.90       dsl 	} */
    437        1.1      fvdl 	struct linux_stat tmplst;
    438       1.81       dsl 	struct stat tmpst;
    439        1.1      fvdl 	int error;
    440        1.1      fvdl 
    441       1.93        ad 	error = do_sys_fstat(SCARG(uap, fd), &tmpst);
    442       1.81       dsl 	if (error != 0)
    443        1.1      fvdl 		return error;
    444        1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    445        1.1      fvdl 
    446       1.81       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    447        1.1      fvdl }
    448        1.1      fvdl 
    449        1.1      fvdl static int
    450       1.93        ad linux_stat1(const struct linux_sys_stat_args *uap, register_t *retval, int flags)
    451        1.1      fvdl {
    452        1.1      fvdl 	struct linux_stat tmplst;
    453       1.81       dsl 	struct stat tmpst;
    454        1.1      fvdl 	int error;
    455        1.1      fvdl 
    456       1.93        ad 	error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
    457       1.81       dsl 	if (error != 0)
    458        1.1      fvdl 		return error;
    459        1.1      fvdl 
    460        1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    461        1.1      fvdl 
    462       1.81       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    463        1.1      fvdl }
    464        1.1      fvdl 
    465        1.1      fvdl int
    466       1.90       dsl linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval)
    467       1.11   thorpej {
    468       1.90       dsl 	/* {
    469       1.27  christos 		syscallarg(const char *) path;
    470        1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    471       1.90       dsl 	} */
    472       1.11   thorpej 
    473       1.93        ad 	return linux_stat1(uap, retval, FOLLOW);
    474        1.1      fvdl }
    475        1.1      fvdl 
    476       1.23       erh /* Note: this is "newlstat" in the Linux sources */
    477       1.23       erh /*	(we don't bother with the old lstat currently) */
    478        1.1      fvdl int
    479       1.90       dsl linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap, register_t *retval)
    480       1.11   thorpej {
    481       1.90       dsl 	/* {
    482       1.27  christos 		syscallarg(const char *) path;
    483        1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    484       1.90       dsl 	} */
    485       1.11   thorpej 
    486       1.93        ad 	return linux_stat1((const void *)uap, retval, NOFOLLOW);
    487        1.1      fvdl }
    488       1.67      manu #endif /* !__amd64__ */
    489        1.1      fvdl 
    490        1.1      fvdl /*
    491       1.10      fvdl  * The following syscalls are mostly here because of the alternate path check.
    492        1.1      fvdl  */
    493        1.1      fvdl int
    494       1.90       dsl linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap, register_t *retval)
    495        1.2      fvdl {
    496       1.90       dsl 	/* {
    497       1.27  christos 		syscallarg(const char *) path;
    498       1.90       dsl 	} */
    499      1.101  dholland 	int error, error2;
    500      1.101  dholland 	struct pathbuf *pb;
    501       1.63  jdolecek 	struct nameidata nd;
    502        1.2      fvdl 
    503       1.90       dsl 	error = sys_unlink(l, (const void *)uap, retval);
    504       1.63  jdolecek 	if (error != EPERM)
    505       1.63  jdolecek 		return (error);
    506       1.63  jdolecek 
    507       1.63  jdolecek 	/*
    508       1.63  jdolecek 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    509       1.63  jdolecek 	 * We return EPERM in such cases. To emulate correct behaviour,
    510       1.63  jdolecek 	 * check if the path points to directory and return EISDIR if this
    511       1.63  jdolecek 	 * is the case.
    512      1.101  dholland 	 *
    513      1.101  dholland 	 * XXX this should really not copy in the path buffer twice...
    514       1.63  jdolecek 	 */
    515      1.101  dholland 	error2 = pathbuf_copyin(SCARG(uap, path), &pb);
    516      1.101  dholland 	if (error2) {
    517      1.101  dholland 		return error2;
    518      1.101  dholland 	}
    519      1.101  dholland 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    520       1.63  jdolecek 	if (namei(&nd) == 0) {
    521       1.63  jdolecek 		struct stat sb;
    522       1.63  jdolecek 
    523       1.93        ad 		if (vn_stat(nd.ni_vp, &sb) == 0
    524       1.63  jdolecek 		    && S_ISDIR(sb.st_mode))
    525       1.63  jdolecek 			error = EISDIR;
    526       1.63  jdolecek 
    527       1.63  jdolecek 		vput(nd.ni_vp);
    528       1.63  jdolecek 	}
    529      1.101  dholland 	pathbuf_destroy(pb);
    530       1.63  jdolecek 
    531       1.63  jdolecek 	return (error);
    532        1.2      fvdl }
    533        1.2      fvdl 
    534       1.10      fvdl int
    535       1.90       dsl linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap, register_t *retval)
    536       1.11   thorpej {
    537       1.90       dsl 	/* {
    538       1.27  christos 		syscallarg(const char *) path;
    539        1.2      fvdl 		syscallarg(int) mode;
    540        1.2      fvdl 		syscallarg(int) dev;
    541       1.90       dsl 	} */
    542        1.2      fvdl 
    543       1.10      fvdl 	/*
    544       1.40       wiz 	 * BSD handles FIFOs separately
    545       1.10      fvdl 	 */
    546       1.74     pavel 	if (S_ISFIFO(SCARG(uap, mode))) {
    547       1.54  jdolecek 		struct sys_mkfifo_args bma;
    548       1.54  jdolecek 
    549       1.21   thorpej 		SCARG(&bma, path) = SCARG(uap, path);
    550       1.21   thorpej 		SCARG(&bma, mode) = SCARG(uap, mode);
    551       1.56   thorpej 		return sys_mkfifo(l, &bma, retval);
    552       1.54  jdolecek 	} else {
    553       1.54  jdolecek 
    554       1.54  jdolecek 		/*
    555       1.54  jdolecek 		 * Linux device numbers uses 8 bits for minor and 8 bits
    556       1.54  jdolecek 		 * for major. Due to how we map our major and minor,
    557       1.82       dsl 		 * this just fits into our dev_t. Just mask off the
    558       1.54  jdolecek 		 * upper 16bit to remove any random junk.
    559       1.54  jdolecek 		 */
    560       1.97  christos 		return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode),
    561       1.98      haad 		    SCARG(uap, dev) & 0xffff, retval, UIO_USERSPACE);
    562       1.54  jdolecek 	}
    563        1.2      fvdl }
    564        1.2      fvdl 
    565       1.15      fvdl /*
    566       1.15      fvdl  * This is just fsync() for now (just as it is in the Linux kernel)
    567       1.23       erh  * Note: this is not implemented under Linux on Alpha and Arm
    568       1.23       erh  *	but should still be defined in our syscalls.master.
    569       1.23       erh  *	(syscall #148 on the arm)
    570       1.15      fvdl  */
    571       1.15      fvdl int
    572       1.90       dsl linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap, register_t *retval)
    573       1.15      fvdl {
    574       1.90       dsl 	/* {
    575       1.15      fvdl 		syscallarg(int) fd;
    576       1.90       dsl 	} */
    577       1.90       dsl 
    578       1.90       dsl 	return sys_fsync(l, (const void *)uap, retval);
    579       1.28      tron }
    580       1.28      tron 
    581       1.28      tron /*
    582       1.28      tron  * pread(2).
    583       1.28      tron  */
    584       1.28      tron int
    585       1.90       dsl linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap, register_t *retval)
    586       1.28      tron {
    587       1.90       dsl 	/* {
    588       1.28      tron 		syscallarg(int) fd;
    589       1.28      tron 		syscallarg(void *) buf;
    590       1.28      tron 		syscallarg(size_t) nbyte;
    591       1.28      tron 		syscallarg(linux_off_t) offset;
    592       1.90       dsl 	} */
    593       1.28      tron 	struct sys_pread_args pra;
    594       1.28      tron 
    595       1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
    596       1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
    597       1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    598       1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
    599       1.28      tron 
    600       1.62  jdolecek 	return sys_pread(l, &pra, retval);
    601       1.28      tron }
    602       1.28      tron 
    603       1.28      tron /*
    604       1.28      tron  * pwrite(2).
    605       1.28      tron  */
    606       1.28      tron int
    607       1.90       dsl linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap, register_t *retval)
    608       1.28      tron {
    609       1.90       dsl 	/* {
    610       1.28      tron 		syscallarg(int) fd;
    611       1.28      tron 		syscallarg(void *) buf;
    612       1.28      tron 		syscallarg(size_t) nbyte;
    613       1.28      tron 		syscallarg(linux_off_t) offset;
    614       1.90       dsl 	} */
    615       1.28      tron 	struct sys_pwrite_args pra;
    616       1.28      tron 
    617       1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
    618       1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
    619       1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    620       1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
    621       1.28      tron 
    622       1.62  jdolecek 	return sys_pwrite(l, &pra, retval);
    623        1.1      fvdl }
    624       1.68      fvdl 
    625  1.101.2.1    jruoho int
    626  1.101.2.1    jruoho linux_sys_dup3(struct lwp *l, const struct linux_sys_dup3_args *uap,
    627  1.101.2.1    jruoho     register_t *retval)
    628  1.101.2.1    jruoho {
    629  1.101.2.1    jruoho 	/* {
    630  1.101.2.1    jruoho 		syscallarg(int) from;
    631  1.101.2.1    jruoho 		syscallarg(int) to;
    632  1.101.2.1    jruoho 		syscallarg(int) flags;
    633  1.101.2.1    jruoho 	} */
    634  1.101.2.1    jruoho 	int error;
    635  1.101.2.1    jruoho 	if ((error = sys_dup2(l, (const struct sys_dup2_args *)uap, retval)))
    636  1.101.2.1    jruoho 		return error;
    637  1.101.2.1    jruoho 
    638  1.101.2.1    jruoho 	if (SCARG(uap, flags) & LINUX_O_CLOEXEC)
    639  1.101.2.1    jruoho 		fd_set_exclose(l, SCARG(uap, to), true);
    640  1.101.2.1    jruoho 
    641  1.101.2.1    jruoho 	return 0;
    642  1.101.2.1    jruoho }
    643  1.101.2.1    jruoho 
    644       1.75  christos #define LINUX_NOT_SUPPORTED(fun) \
    645       1.75  christos int \
    646       1.90       dsl fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
    647       1.75  christos { \
    648       1.75  christos 	return EOPNOTSUPP; \
    649       1.75  christos }
    650       1.75  christos 
    651       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_setxattr)
    652       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
    653       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
    654       1.75  christos 
    655       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_getxattr)
    656       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
    657       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
    658       1.75  christos 
    659       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_listxattr)
    660       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
    661       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
    662       1.75  christos 
    663       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_removexattr)
    664       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
    665       1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
    666