Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.77.2.1
      1  1.77.2.1      yamt /*	$NetBSD: linux_file.c,v 1.77.2.1 2007/02/27 16:53:37 yamt Exp $	*/
      2      1.23       erh 
      3      1.23       erh /*-
      4      1.25      fvdl  * Copyright (c) 1995, 1998 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  * 3. All advertising materials mentioning features or use of this software
     19      1.23       erh  *    must display the following acknowledgement:
     20      1.23       erh  *	This product includes software developed by the NetBSD
     21      1.23       erh  *	Foundation, Inc. and its contributors.
     22      1.23       erh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.23       erh  *    contributors may be used to endorse or promote products derived
     24      1.23       erh  *    from this software without specific prior written permission.
     25      1.23       erh  *
     26      1.23       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.23       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.23       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.23       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.23       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.23       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.23       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.23       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.23       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.23       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.23       erh  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1      fvdl  */
     38       1.1      fvdl 
     39      1.23       erh /*
     40      1.23       erh  * Functions in multiarch:
     41      1.23       erh  *	linux_sys_llseek	: linux_llseek.c
     42      1.23       erh  */
     43      1.43     lukem 
     44      1.43     lukem #include <sys/cdefs.h>
     45  1.77.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.77.2.1 2007/02/27 16:53:37 yamt Exp $");
     46      1.23       erh 
     47       1.1      fvdl #include <sys/param.h>
     48       1.1      fvdl #include <sys/systm.h>
     49       1.1      fvdl #include <sys/namei.h>
     50       1.1      fvdl #include <sys/proc.h>
     51       1.1      fvdl #include <sys/file.h>
     52       1.1      fvdl #include <sys/stat.h>
     53       1.1      fvdl #include <sys/filedesc.h>
     54       1.1      fvdl #include <sys/ioctl.h>
     55       1.1      fvdl #include <sys/kernel.h>
     56       1.1      fvdl #include <sys/mount.h>
     57       1.1      fvdl #include <sys/malloc.h>
     58      1.13      fvdl #include <sys/vnode.h>
     59      1.13      fvdl #include <sys/tty.h>
     60      1.39      manu #include <sys/socketvar.h>
     61      1.13      fvdl #include <sys/conf.h>
     62      1.41  jdolecek #include <sys/pipe.h>
     63       1.1      fvdl 
     64       1.1      fvdl #include <sys/syscallargs.h>
     65       1.1      fvdl 
     66      1.24  christos #include <compat/linux/common/linux_types.h>
     67      1.24  christos #include <compat/linux/common/linux_signal.h>
     68      1.24  christos #include <compat/linux/common/linux_fcntl.h>
     69      1.24  christos #include <compat/linux/common/linux_util.h>
     70      1.24  christos #include <compat/linux/common/linux_machdep.h>
     71      1.24  christos 
     72       1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     73      1.14  christos 
     74      1.14  christos static int linux_to_bsd_ioflags __P((int));
     75      1.14  christos static int bsd_to_linux_ioflags __P((int));
     76      1.14  christos static void bsd_to_linux_flock __P((struct flock *, struct linux_flock *));
     77      1.14  christos static void linux_to_bsd_flock __P((struct linux_flock *, struct flock *));
     78      1.67      manu #ifndef __amd64__
     79      1.14  christos static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
     80      1.56   thorpej static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
     81      1.67      manu #endif
     82      1.14  christos 
     83       1.1      fvdl /*
     84       1.1      fvdl  * Some file-related calls are handled here. The usual flag conversion
     85       1.1      fvdl  * an structure conversion is done, and alternate emul path searching.
     86       1.1      fvdl  */
     87       1.1      fvdl 
     88       1.1      fvdl /*
     89       1.1      fvdl  * The next two functions convert between the Linux and NetBSD values
     90       1.1      fvdl  * of the flags used in open(2) and fcntl(2).
     91       1.1      fvdl  */
     92       1.1      fvdl static int
     93      1.14  christos linux_to_bsd_ioflags(lflags)
     94      1.14  christos 	int lflags;
     95       1.1      fvdl {
     96       1.1      fvdl 	int res = 0;
     97       1.1      fvdl 
     98       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
     99       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
    100       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
    101       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
    102       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
    103       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
    104       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
    105       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
    106       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
    107       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
    108       1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
    109       1.1      fvdl 
    110       1.1      fvdl 	return res;
    111       1.1      fvdl }
    112       1.1      fvdl 
    113       1.1      fvdl static int
    114      1.14  christos bsd_to_linux_ioflags(bflags)
    115      1.14  christos 	int bflags;
    116       1.1      fvdl {
    117       1.1      fvdl 	int res = 0;
    118       1.1      fvdl 
    119       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    120       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    121       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    122       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    123       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    124       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    125       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    126       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    127       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    128       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    129       1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    130       1.1      fvdl 
    131       1.1      fvdl 	return res;
    132       1.1      fvdl }
    133       1.1      fvdl 
    134       1.1      fvdl /*
    135       1.1      fvdl  * creat(2) is an obsolete function, but it's present as a Linux
    136       1.1      fvdl  * system call, so let's deal with it.
    137       1.1      fvdl  *
    138      1.23       erh  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    139      1.23       erh  * in syscalls.master anyway so this doesn't have to be special cased.
    140      1.23       erh  *
    141       1.1      fvdl  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    142       1.1      fvdl  */
    143       1.1      fvdl int
    144      1.56   thorpej linux_sys_creat(l, v, retval)
    145      1.56   thorpej 	struct lwp *l;
    146      1.11   thorpej 	void *v;
    147      1.11   thorpej 	register_t *retval;
    148      1.11   thorpej {
    149      1.12   mycroft 	struct linux_sys_creat_args /* {
    150      1.27  christos 		syscallarg(const char *) path;
    151       1.1      fvdl 		syscallarg(int) mode;
    152      1.11   thorpej 	} */ *uap = v;
    153      1.56   thorpej 	struct proc *p = l->l_proc;
    154      1.12   mycroft 	struct sys_open_args oa;
    155       1.1      fvdl 	caddr_t sg;
    156       1.1      fvdl 
    157      1.46  christos 	sg = stackgap_init(p, 0);
    158      1.71  christos 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    159       1.1      fvdl 
    160       1.1      fvdl 	SCARG(&oa, path) = SCARG(uap, path);
    161       1.1      fvdl 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    162       1.1      fvdl 	SCARG(&oa, mode) = SCARG(uap, mode);
    163      1.12   mycroft 
    164      1.56   thorpej 	return sys_open(l, &oa, retval);
    165       1.1      fvdl }
    166       1.1      fvdl 
    167       1.1      fvdl /*
    168       1.1      fvdl  * open(2). Take care of the different flag values, and let the
    169       1.1      fvdl  * NetBSD syscall do the real work. See if this operation
    170       1.1      fvdl  * gives the current process a controlling terminal.
    171       1.1      fvdl  * (XXX is this necessary?)
    172       1.1      fvdl  */
    173       1.1      fvdl int
    174      1.56   thorpej linux_sys_open(l, v, retval)
    175      1.56   thorpej 	struct lwp *l;
    176      1.11   thorpej 	void *v;
    177      1.11   thorpej 	register_t *retval;
    178      1.11   thorpej {
    179      1.12   mycroft 	struct linux_sys_open_args /* {
    180      1.27  christos 		syscallarg(const char *) path;
    181       1.1      fvdl 		syscallarg(int) flags;
    182       1.1      fvdl 		syscallarg(int) mode;
    183      1.11   thorpej 	} */ *uap = v;
    184      1.56   thorpej 	struct proc *p = l->l_proc;
    185       1.1      fvdl 	int error, fl;
    186      1.12   mycroft 	struct sys_open_args boa;
    187       1.1      fvdl 	caddr_t sg;
    188       1.1      fvdl 
    189      1.46  christos 	sg = stackgap_init(p, 0);
    190       1.1      fvdl 
    191       1.2      fvdl 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    192       1.1      fvdl 
    193       1.2      fvdl 	if (fl & O_CREAT)
    194      1.71  christos 		CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    195       1.2      fvdl 	else
    196      1.71  christos 		CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    197       1.1      fvdl 
    198       1.1      fvdl 	SCARG(&boa, path) = SCARG(uap, path);
    199       1.1      fvdl 	SCARG(&boa, flags) = fl;
    200       1.1      fvdl 	SCARG(&boa, mode) = SCARG(uap, mode);
    201       1.2      fvdl 
    202      1.56   thorpej 	if ((error = sys_open(l, &boa, retval)))
    203       1.1      fvdl 		return error;
    204       1.1      fvdl 
    205       1.1      fvdl 	/*
    206       1.1      fvdl 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    207       1.1      fvdl 	 * If we are a session leader, and we don't have a controlling
    208       1.1      fvdl 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    209       1.1      fvdl 	 * this the controlling terminal.
    210      1.65     perry 	 */
    211      1.77        ad         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    212       1.1      fvdl                 struct filedesc *fdp = p->p_fd;
    213      1.38   thorpej                 struct file     *fp;
    214      1.38   thorpej 
    215      1.38   thorpej 		fp = fd_getfile(fdp, *retval);
    216       1.1      fvdl 
    217       1.1      fvdl                 /* ignore any error, just give it a try */
    218      1.57      yamt                 if (fp != NULL) {
    219      1.57      yamt 			FILE_USE(fp);
    220      1.57      yamt 			if (fp->f_type == DTYPE_VNODE) {
    221      1.57      yamt 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
    222      1.71  christos 				    (caddr_t) 0, l);
    223      1.57      yamt 			}
    224      1.71  christos 			FILE_UNUSE(fp, l);
    225      1.57      yamt 		}
    226       1.1      fvdl         }
    227       1.1      fvdl 	return 0;
    228       1.1      fvdl }
    229       1.1      fvdl 
    230       1.1      fvdl /*
    231       1.1      fvdl  * The next two functions take care of converting the flock
    232       1.1      fvdl  * structure back and forth between Linux and NetBSD format.
    233       1.1      fvdl  * The only difference in the structures is the order of
    234       1.1      fvdl  * the fields, and the 'whence' value.
    235       1.1      fvdl  */
    236       1.1      fvdl static void
    237       1.1      fvdl bsd_to_linux_flock(bfp, lfp)
    238       1.1      fvdl 	struct flock *bfp;
    239       1.1      fvdl 	struct linux_flock *lfp;
    240       1.1      fvdl {
    241      1.12   mycroft 
    242       1.1      fvdl 	lfp->l_start = bfp->l_start;
    243       1.1      fvdl 	lfp->l_len = bfp->l_len;
    244       1.1      fvdl 	lfp->l_pid = bfp->l_pid;
    245       1.3   mycroft 	lfp->l_whence = bfp->l_whence;
    246       1.3   mycroft 	switch (bfp->l_type) {
    247       1.1      fvdl 	case F_RDLCK:
    248       1.3   mycroft 		lfp->l_type = LINUX_F_RDLCK;
    249       1.1      fvdl 		break;
    250       1.1      fvdl 	case F_UNLCK:
    251       1.3   mycroft 		lfp->l_type = LINUX_F_UNLCK;
    252       1.1      fvdl 		break;
    253       1.1      fvdl 	case F_WRLCK:
    254       1.3   mycroft 		lfp->l_type = LINUX_F_WRLCK;
    255       1.1      fvdl 		break;
    256       1.1      fvdl 	}
    257       1.1      fvdl }
    258       1.1      fvdl 
    259       1.1      fvdl static void
    260       1.1      fvdl linux_to_bsd_flock(lfp, bfp)
    261       1.1      fvdl 	struct linux_flock *lfp;
    262       1.1      fvdl 	struct flock *bfp;
    263       1.1      fvdl {
    264      1.12   mycroft 
    265       1.1      fvdl 	bfp->l_start = lfp->l_start;
    266       1.1      fvdl 	bfp->l_len = lfp->l_len;
    267       1.1      fvdl 	bfp->l_pid = lfp->l_pid;
    268       1.3   mycroft 	bfp->l_whence = lfp->l_whence;
    269       1.3   mycroft 	switch (lfp->l_type) {
    270       1.1      fvdl 	case LINUX_F_RDLCK:
    271       1.3   mycroft 		bfp->l_type = F_RDLCK;
    272       1.1      fvdl 		break;
    273       1.1      fvdl 	case LINUX_F_UNLCK:
    274       1.3   mycroft 		bfp->l_type = F_UNLCK;
    275       1.1      fvdl 		break;
    276       1.1      fvdl 	case LINUX_F_WRLCK:
    277       1.3   mycroft 		bfp->l_type = F_WRLCK;
    278       1.1      fvdl 		break;
    279       1.1      fvdl 	}
    280       1.1      fvdl }
    281       1.1      fvdl 
    282       1.1      fvdl /*
    283       1.1      fvdl  * Most actions in the fcntl() call are straightforward; simply
    284       1.1      fvdl  * pass control to the NetBSD system call. A few commands need
    285       1.1      fvdl  * conversions after the actual system call has done its work,
    286       1.1      fvdl  * because the flag values and lock structure are different.
    287       1.1      fvdl  */
    288       1.1      fvdl int
    289      1.56   thorpej linux_sys_fcntl(l, v, retval)
    290      1.56   thorpej 	struct lwp *l;
    291      1.11   thorpej 	void *v;
    292      1.11   thorpej 	register_t *retval;
    293      1.11   thorpej {
    294      1.12   mycroft 	struct linux_sys_fcntl_args /* {
    295       1.1      fvdl 		syscallarg(int) fd;
    296       1.1      fvdl 		syscallarg(int) cmd;
    297       1.1      fvdl 		syscallarg(void *) arg;
    298      1.11   thorpej 	} */ *uap = v;
    299      1.56   thorpej 	struct proc *p = l->l_proc;
    300      1.23       erh 	int fd, cmd, error;
    301      1.23       erh 	u_long val;
    302       1.1      fvdl 	caddr_t arg, sg;
    303       1.1      fvdl 	struct linux_flock lfl;
    304       1.1      fvdl 	struct flock *bfp, bfl;
    305      1.12   mycroft 	struct sys_fcntl_args fca;
    306      1.13      fvdl 	struct filedesc *fdp;
    307      1.13      fvdl 	struct file *fp;
    308      1.13      fvdl 	struct vnode *vp;
    309      1.13      fvdl 	struct vattr va;
    310      1.53   gehenna 	const struct cdevsw *cdev;
    311      1.13      fvdl 	long pgid;
    312      1.13      fvdl 	struct pgrp *pgrp;
    313      1.13      fvdl 	struct tty *tp, *(*d_tty) __P((dev_t));
    314       1.1      fvdl 
    315       1.1      fvdl 	fd = SCARG(uap, fd);
    316       1.1      fvdl 	cmd = SCARG(uap, cmd);
    317       1.1      fvdl 	arg = (caddr_t) SCARG(uap, arg);
    318       1.1      fvdl 
    319       1.1      fvdl 	switch (cmd) {
    320       1.1      fvdl 	case LINUX_F_DUPFD:
    321       1.1      fvdl 		cmd = F_DUPFD;
    322       1.1      fvdl 		break;
    323       1.1      fvdl 	case LINUX_F_GETFD:
    324       1.1      fvdl 		cmd = F_GETFD;
    325       1.1      fvdl 		break;
    326       1.1      fvdl 	case LINUX_F_SETFD:
    327       1.1      fvdl 		cmd = F_SETFD;
    328       1.1      fvdl 		break;
    329       1.1      fvdl 	case LINUX_F_GETFL:
    330       1.1      fvdl 		SCARG(&fca, fd) = fd;
    331       1.1      fvdl 		SCARG(&fca, cmd) = F_GETFL;
    332       1.1      fvdl 		SCARG(&fca, arg) = arg;
    333      1.56   thorpej 		if ((error = sys_fcntl(l, &fca, retval)))
    334       1.1      fvdl 			return error;
    335       1.1      fvdl 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    336       1.1      fvdl 		return 0;
    337      1.41  jdolecek 	case LINUX_F_SETFL: {
    338      1.69  christos 		struct file	*fp1 = NULL;
    339      1.41  jdolecek 
    340      1.23       erh 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    341      1.39      manu 		/*
    342      1.41  jdolecek 		 * Linux seems to have same semantics for sending SIGIO to the
    343      1.64       abs 		 * read side of socket, but slightly different semantics
    344      1.41  jdolecek 		 * for SIGIO to the write side.  Rather than sending the SIGIO
    345      1.41  jdolecek 		 * every time it's possible to write (directly) more data, it
    346      1.41  jdolecek 		 * only sends SIGIO if last write(2) failed due to insufficient
    347      1.41  jdolecek 		 * memory to hold the data. This is compatible enough
    348      1.41  jdolecek 		 * with NetBSD semantics to not do anything about the
    349      1.41  jdolecek 		 * difference.
    350      1.65     perry 		 *
    351      1.41  jdolecek 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
    352      1.41  jdolecek 		 * ones and DTYPE_PIPE ones. For these, we don't set
    353      1.41  jdolecek 		 * the underlying flags (we don't pass O_ASYNC flag down
    354      1.41  jdolecek 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
    355      1.41  jdolecek 		 * so that F_GETFL would report the ASYNC i/o is on.
    356      1.39      manu 		 */
    357      1.41  jdolecek 		if (val & O_ASYNC) {
    358      1.69  christos 			if (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
    359      1.39      manu 			    return (EBADF);
    360      1.39      manu 
    361      1.69  christos 			FILE_USE(fp1);
    362      1.39      manu 
    363      1.69  christos 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    364      1.69  christos 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    365      1.69  christos 			    || (fp1->f_type == DTYPE_PIPE))
    366      1.41  jdolecek 				val &= ~O_ASYNC;
    367      1.41  jdolecek 			else {
    368      1.41  jdolecek 				/* not a pipe, do not modify anything */
    369      1.71  christos 				FILE_UNUSE(fp1, l);
    370      1.69  christos 				fp1 = NULL;
    371      1.39      manu 			}
    372      1.41  jdolecek 		}
    373      1.41  jdolecek 
    374      1.41  jdolecek 		SCARG(&fca, fd) = fd;
    375      1.41  jdolecek 		SCARG(&fca, cmd) = F_SETFL;
    376      1.41  jdolecek 		SCARG(&fca, arg) = (caddr_t) val;
    377      1.39      manu 
    378      1.56   thorpej 		error = sys_fcntl(l, &fca, retval);
    379      1.41  jdolecek 
    380      1.41  jdolecek 		/* Now set the FASYNC flag for pipes */
    381      1.69  christos 		if (fp1) {
    382      1.41  jdolecek 			if (!error)
    383      1.69  christos 				fp1->f_flag |= FASYNC;
    384      1.71  christos 			FILE_UNUSE(fp1, l);
    385      1.39      manu 		}
    386      1.41  jdolecek 
    387      1.41  jdolecek 		return (error);
    388      1.41  jdolecek 	    }
    389       1.1      fvdl 	case LINUX_F_GETLK:
    390      1.46  christos 		sg = stackgap_init(p, 0);
    391      1.46  christos 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    392      1.18    kleink 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    393      1.18    kleink 			return error;
    394      1.18    kleink 		linux_to_bsd_flock(&lfl, &bfl);
    395      1.18    kleink 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    396      1.18    kleink 			return error;
    397       1.1      fvdl 		SCARG(&fca, fd) = fd;
    398       1.1      fvdl 		SCARG(&fca, cmd) = F_GETLK;
    399       1.1      fvdl 		SCARG(&fca, arg) = bfp;
    400      1.56   thorpej 		if ((error = sys_fcntl(l, &fca, retval)))
    401       1.1      fvdl 			return error;
    402       1.1      fvdl 		if ((error = copyin(bfp, &bfl, sizeof bfl)))
    403       1.1      fvdl 			return error;
    404       1.1      fvdl 		bsd_to_linux_flock(&bfl, &lfl);
    405       1.1      fvdl 		return copyout(&lfl, arg, sizeof lfl);
    406      1.47  christos 
    407       1.1      fvdl 	case LINUX_F_SETLK:
    408       1.1      fvdl 	case LINUX_F_SETLKW:
    409       1.1      fvdl 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
    410       1.1      fvdl 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    411       1.1      fvdl 			return error;
    412       1.1      fvdl 		linux_to_bsd_flock(&lfl, &bfl);
    413      1.46  christos 		sg = stackgap_init(p, 0);
    414      1.46  christos 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    415       1.1      fvdl 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    416       1.1      fvdl 			return error;
    417      1.51  christos 		arg = (caddr_t)bfp;
    418       1.1      fvdl 		break;
    419      1.47  christos 
    420       1.1      fvdl 	case LINUX_F_SETOWN:
    421      1.65     perry 	case LINUX_F_GETOWN:
    422      1.13      fvdl 		/*
    423      1.49  jdolecek 		 * We need to route fcntl() for tty descriptors around normal
    424      1.49  jdolecek 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    425      1.49  jdolecek 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    426      1.49  jdolecek 		 * this is not a problem.
    427      1.13      fvdl 		 */
    428      1.13      fvdl 		fdp = p->p_fd;
    429      1.38   thorpej 		if ((fp = fd_getfile(fdp, fd)) == NULL)
    430      1.13      fvdl 			return EBADF;
    431      1.60  jdolecek 		FILE_USE(fp);
    432      1.60  jdolecek 
    433      1.60  jdolecek 		/* Check it's a character device vnode */
    434      1.60  jdolecek 		if (fp->f_type != DTYPE_VNODE
    435      1.60  jdolecek 		    || (vp = (struct vnode *)fp->f_data) == NULL
    436      1.60  jdolecek 		    || vp->v_type != VCHR) {
    437      1.71  christos 			FILE_UNUSE(fp, l);
    438      1.60  jdolecek 
    439      1.53   gehenna 	    not_tty:
    440      1.49  jdolecek 			/* Not a tty, proceed with common fcntl() */
    441      1.13      fvdl 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    442      1.49  jdolecek 			break;
    443      1.49  jdolecek 		}
    444      1.47  christos 
    445      1.73        ad 		error = VOP_GETATTR(vp, &va, l->l_cred, l);
    446      1.60  jdolecek 
    447      1.71  christos 		FILE_UNUSE(fp, l);
    448      1.60  jdolecek 
    449      1.60  jdolecek 		if (error)
    450      1.49  jdolecek 			return error;
    451      1.60  jdolecek 
    452      1.53   gehenna 		cdev = cdevsw_lookup(va.va_rdev);
    453      1.53   gehenna 		if (cdev == NULL)
    454      1.53   gehenna 			return (ENXIO);
    455      1.53   gehenna 		d_tty = cdev->d_tty;
    456      1.49  jdolecek 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
    457      1.53   gehenna 			goto not_tty;
    458      1.49  jdolecek 
    459      1.49  jdolecek 		/* set tty pg_id appropriately */
    460      1.49  jdolecek 		if (cmd == LINUX_F_GETOWN) {
    461      1.58       dsl 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    462      1.49  jdolecek 			return 0;
    463      1.49  jdolecek 		}
    464  1.77.2.1      yamt 		rw_enter(&proclist_lock, RW_READER);
    465      1.49  jdolecek 		if ((long)arg <= 0) {
    466      1.49  jdolecek 			pgid = -(long)arg;
    467      1.49  jdolecek 		} else {
    468  1.77.2.1      yamt 			struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
    469      1.49  jdolecek 			if (p1 == NULL)
    470      1.49  jdolecek 				return (ESRCH);
    471      1.49  jdolecek 			pgid = (long)p1->p_pgrp->pg_id;
    472      1.13      fvdl 		}
    473  1.77.2.1      yamt 		pgrp = pg_find(pgid, PFIND_LOCKED);
    474  1.77.2.1      yamt 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    475  1.77.2.1      yamt 			rw_exit(&proclist_lock);
    476      1.49  jdolecek 			return EPERM;
    477  1.77.2.1      yamt 		}
    478      1.49  jdolecek 		tp->t_pgrp = pgrp;
    479  1.77.2.1      yamt 		rw_exit(&proclist_lock);
    480      1.49  jdolecek 		return 0;
    481      1.47  christos 
    482       1.1      fvdl 	default:
    483       1.1      fvdl 		return EOPNOTSUPP;
    484       1.1      fvdl 	}
    485       1.1      fvdl 
    486       1.1      fvdl 	SCARG(&fca, fd) = fd;
    487       1.1      fvdl 	SCARG(&fca, cmd) = cmd;
    488       1.1      fvdl 	SCARG(&fca, arg) = arg;
    489      1.12   mycroft 
    490      1.56   thorpej 	return sys_fcntl(l, &fca, retval);
    491       1.1      fvdl }
    492       1.1      fvdl 
    493      1.67      manu #if !defined(__amd64__)
    494       1.1      fvdl /*
    495       1.1      fvdl  * Convert a NetBSD stat structure to a Linux stat structure.
    496       1.1      fvdl  * Only the order of the fields and the padding in the structure
    497       1.9      fvdl  * is different. linux_fakedev is a machine-dependent function
    498       1.9      fvdl  * which optionally converts device driver major/minor numbers
    499       1.9      fvdl  * (XXX horrible, but what can you do against code that compares
    500       1.9      fvdl  * things against constant major device numbers? sigh)
    501       1.1      fvdl  */
    502       1.1      fvdl static void
    503       1.1      fvdl bsd_to_linux_stat(bsp, lsp)
    504       1.1      fvdl 	struct stat *bsp;
    505       1.1      fvdl 	struct linux_stat *lsp;
    506       1.1      fvdl {
    507      1.12   mycroft 
    508      1.45  christos 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    509       1.1      fvdl 	lsp->lst_ino     = bsp->st_ino;
    510      1.19  christos 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    511      1.19  christos 	if (bsp->st_nlink >= (1 << 15))
    512      1.19  christos 		lsp->lst_nlink = (1 << 15) - 1;
    513      1.19  christos 	else
    514      1.19  christos 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    515       1.1      fvdl 	lsp->lst_uid     = bsp->st_uid;
    516       1.1      fvdl 	lsp->lst_gid     = bsp->st_gid;
    517      1.45  christos 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    518       1.1      fvdl 	lsp->lst_size    = bsp->st_size;
    519       1.1      fvdl 	lsp->lst_blksize = bsp->st_blksize;
    520       1.1      fvdl 	lsp->lst_blocks  = bsp->st_blocks;
    521       1.1      fvdl 	lsp->lst_atime   = bsp->st_atime;
    522       1.1      fvdl 	lsp->lst_mtime   = bsp->st_mtime;
    523       1.1      fvdl 	lsp->lst_ctime   = bsp->st_ctime;
    524      1.66  christos #ifdef LINUX_STAT_HAS_NSEC
    525      1.66  christos 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    526      1.66  christos 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    527      1.66  christos 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    528      1.66  christos #endif
    529       1.1      fvdl }
    530       1.1      fvdl 
    531       1.1      fvdl /*
    532       1.1      fvdl  * The stat functions below are plain sailing. stat and lstat are handled
    533       1.1      fvdl  * by one function to avoid code duplication.
    534       1.1      fvdl  */
    535       1.1      fvdl int
    536      1.56   thorpej linux_sys_fstat(l, v, retval)
    537      1.56   thorpej 	struct lwp *l;
    538      1.11   thorpej 	void *v;
    539      1.11   thorpej 	register_t *retval;
    540      1.11   thorpej {
    541      1.12   mycroft 	struct linux_sys_fstat_args /* {
    542       1.1      fvdl 		syscallarg(int) fd;
    543       1.1      fvdl 		syscallarg(linux_stat *) sp;
    544      1.11   thorpej 	} */ *uap = v;
    545      1.56   thorpej 	struct proc *p = l->l_proc;
    546      1.70  christos 	struct sys___fstat30_args fsa;
    547       1.1      fvdl 	struct linux_stat tmplst;
    548       1.1      fvdl 	struct stat *st,tmpst;
    549       1.1      fvdl 	caddr_t sg;
    550       1.1      fvdl 	int error;
    551       1.1      fvdl 
    552      1.46  christos 	sg = stackgap_init(p, 0);
    553       1.1      fvdl 
    554      1.46  christos 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    555       1.1      fvdl 
    556       1.1      fvdl 	SCARG(&fsa, fd) = SCARG(uap, fd);
    557       1.1      fvdl 	SCARG(&fsa, sb) = st;
    558       1.1      fvdl 
    559      1.70  christos 	if ((error = sys___fstat30(l, &fsa, retval)))
    560       1.1      fvdl 		return error;
    561       1.1      fvdl 
    562       1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    563       1.1      fvdl 		return error;
    564       1.1      fvdl 
    565       1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    566       1.1      fvdl 
    567       1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    568       1.1      fvdl 		return error;
    569       1.1      fvdl 
    570       1.1      fvdl 	return 0;
    571       1.1      fvdl }
    572       1.1      fvdl 
    573       1.1      fvdl static int
    574      1.56   thorpej linux_stat1(l, v, retval, dolstat)
    575      1.56   thorpej 	struct lwp *l;
    576      1.14  christos 	void *v;
    577       1.1      fvdl 	register_t *retval;
    578       1.1      fvdl 	int dolstat;
    579       1.1      fvdl {
    580      1.70  christos 	struct sys___stat30_args sa;
    581       1.1      fvdl 	struct linux_stat tmplst;
    582       1.1      fvdl 	struct stat *st, tmpst;
    583      1.56   thorpej 	struct proc *p = l->l_proc;
    584       1.1      fvdl 	caddr_t sg;
    585       1.1      fvdl 	int error;
    586      1.14  christos 	struct linux_sys_stat_args *uap = v;
    587       1.1      fvdl 
    588      1.46  christos 	sg = stackgap_init(p, 0);
    589      1.46  christos 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    590      1.37  jdolecek 	if (dolstat)
    591      1.71  christos 		CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
    592      1.37  jdolecek 	else
    593      1.71  christos 		CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    594       1.1      fvdl 
    595       1.1      fvdl 	SCARG(&sa, ub) = st;
    596       1.1      fvdl 	SCARG(&sa, path) = SCARG(uap, path);
    597       1.1      fvdl 
    598      1.70  christos 	if ((error = (dolstat ? sys___lstat30(l, &sa, retval) :
    599      1.70  christos 				sys___stat30(l, &sa, retval))))
    600       1.1      fvdl 		return error;
    601       1.1      fvdl 
    602       1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    603       1.1      fvdl 		return error;
    604       1.1      fvdl 
    605       1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    606       1.1      fvdl 
    607       1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    608       1.1      fvdl 		return error;
    609       1.1      fvdl 
    610       1.1      fvdl 	return 0;
    611       1.1      fvdl }
    612       1.1      fvdl 
    613       1.1      fvdl int
    614      1.56   thorpej linux_sys_stat(l, v, retval)
    615      1.56   thorpej 	struct lwp *l;
    616      1.11   thorpej 	void *v;
    617      1.11   thorpej 	register_t *retval;
    618      1.11   thorpej {
    619      1.12   mycroft 	struct linux_sys_stat_args /* {
    620      1.27  christos 		syscallarg(const char *) path;
    621       1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    622      1.11   thorpej 	} */ *uap = v;
    623      1.11   thorpej 
    624      1.56   thorpej 	return linux_stat1(l, uap, retval, 0);
    625       1.1      fvdl }
    626       1.1      fvdl 
    627      1.23       erh /* Note: this is "newlstat" in the Linux sources */
    628      1.23       erh /*	(we don't bother with the old lstat currently) */
    629       1.1      fvdl int
    630      1.56   thorpej linux_sys_lstat(l, v, retval)
    631      1.56   thorpej 	struct lwp *l;
    632      1.11   thorpej 	void *v;
    633      1.11   thorpej 	register_t *retval;
    634      1.11   thorpej {
    635      1.12   mycroft 	struct linux_sys_lstat_args /* {
    636      1.27  christos 		syscallarg(const char *) path;
    637       1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    638      1.11   thorpej 	} */ *uap = v;
    639      1.11   thorpej 
    640      1.56   thorpej 	return linux_stat1(l, uap, retval, 1);
    641       1.1      fvdl }
    642      1.67      manu #endif /* !__amd64__ */
    643       1.1      fvdl 
    644       1.1      fvdl /*
    645      1.10      fvdl  * The following syscalls are mostly here because of the alternate path check.
    646       1.1      fvdl  */
    647       1.1      fvdl int
    648      1.56   thorpej linux_sys_access(l, v, retval)
    649      1.56   thorpej 	struct lwp *l;
    650      1.11   thorpej 	void *v;
    651      1.11   thorpej 	register_t *retval;
    652      1.11   thorpej {
    653      1.12   mycroft 	struct linux_sys_access_args /* {
    654      1.27  christos 		syscallarg(const char *) path;
    655       1.1      fvdl 		syscallarg(int) flags;
    656      1.11   thorpej 	} */ *uap = v;
    657      1.56   thorpej 	struct proc *p = l->l_proc;
    658      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    659       1.1      fvdl 
    660      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    661       1.1      fvdl 
    662      1.56   thorpej 	return sys_access(l, uap, retval);
    663       1.2      fvdl }
    664       1.2      fvdl 
    665       1.2      fvdl int
    666      1.56   thorpej linux_sys_unlink(l, v, retval)
    667      1.56   thorpej 	struct lwp *l;
    668      1.11   thorpej 	void *v;
    669       1.2      fvdl 	register_t *retval;
    670       1.2      fvdl 
    671       1.2      fvdl {
    672      1.12   mycroft 	struct linux_sys_unlink_args /* {
    673      1.27  christos 		syscallarg(const char *) path;
    674      1.11   thorpej 	} */ *uap = v;
    675      1.56   thorpej 	struct proc *p = l->l_proc;
    676      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    677      1.63  jdolecek 	int error;
    678      1.63  jdolecek 	struct nameidata nd;
    679       1.2      fvdl 
    680      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    681       1.2      fvdl 
    682      1.63  jdolecek 	error = sys_unlink(l, uap, retval);
    683      1.63  jdolecek 	if (error != EPERM)
    684      1.63  jdolecek 		return (error);
    685      1.63  jdolecek 
    686      1.63  jdolecek 	/*
    687      1.63  jdolecek 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    688      1.63  jdolecek 	 * We return EPERM in such cases. To emulate correct behaviour,
    689      1.63  jdolecek 	 * check if the path points to directory and return EISDIR if this
    690      1.63  jdolecek 	 * is the case.
    691      1.63  jdolecek 	 */
    692      1.63  jdolecek 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    693      1.71  christos 	    SCARG(uap, path), l);
    694      1.63  jdolecek 	if (namei(&nd) == 0) {
    695      1.63  jdolecek 		struct stat sb;
    696      1.63  jdolecek 
    697      1.71  christos 		if (vn_stat(nd.ni_vp, &sb, l) == 0
    698      1.63  jdolecek 		    && S_ISDIR(sb.st_mode))
    699      1.63  jdolecek 			error = EISDIR;
    700      1.63  jdolecek 
    701      1.63  jdolecek 		vput(nd.ni_vp);
    702      1.63  jdolecek 	}
    703      1.63  jdolecek 
    704      1.63  jdolecek 	return (error);
    705       1.2      fvdl }
    706       1.2      fvdl 
    707      1.10      fvdl int
    708      1.56   thorpej linux_sys_chdir(l, v, retval)
    709      1.56   thorpej 	struct lwp *l;
    710      1.11   thorpej 	void *v;
    711      1.11   thorpej 	register_t *retval;
    712      1.11   thorpej {
    713      1.12   mycroft 	struct linux_sys_chdir_args /* {
    714      1.27  christos 		syscallarg(const char *) path;
    715      1.11   thorpej 	} */ *uap = v;
    716      1.56   thorpej 	struct proc *p = l->l_proc;
    717      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    718       1.2      fvdl 
    719      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    720       1.2      fvdl 
    721      1.56   thorpej 	return sys_chdir(l, uap, retval);
    722       1.2      fvdl }
    723       1.2      fvdl 
    724       1.2      fvdl int
    725      1.56   thorpej linux_sys_mknod(l, v, retval)
    726      1.56   thorpej 	struct lwp *l;
    727      1.11   thorpej 	void *v;
    728      1.11   thorpej 	register_t *retval;
    729      1.11   thorpej {
    730      1.12   mycroft 	struct linux_sys_mknod_args /* {
    731      1.27  christos 		syscallarg(const char *) path;
    732       1.2      fvdl 		syscallarg(int) mode;
    733       1.2      fvdl 		syscallarg(int) dev;
    734      1.11   thorpej 	} */ *uap = v;
    735      1.56   thorpej 	struct proc *p = l->l_proc;
    736      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    737       1.2      fvdl 
    738      1.71  christos 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    739       1.2      fvdl 
    740      1.10      fvdl 	/*
    741      1.40       wiz 	 * BSD handles FIFOs separately
    742      1.10      fvdl 	 */
    743      1.74     pavel 	if (S_ISFIFO(SCARG(uap, mode))) {
    744      1.54  jdolecek 		struct sys_mkfifo_args bma;
    745      1.54  jdolecek 
    746      1.21   thorpej 		SCARG(&bma, path) = SCARG(uap, path);
    747      1.21   thorpej 		SCARG(&bma, mode) = SCARG(uap, mode);
    748      1.56   thorpej 		return sys_mkfifo(l, &bma, retval);
    749      1.54  jdolecek 	} else {
    750      1.54  jdolecek 		struct sys_mknod_args bma;
    751      1.54  jdolecek 
    752      1.54  jdolecek 		SCARG(&bma, path) = SCARG(uap, path);
    753      1.54  jdolecek 		SCARG(&bma, mode) = SCARG(uap, mode);
    754      1.54  jdolecek 		/*
    755      1.54  jdolecek 		 * Linux device numbers uses 8 bits for minor and 8 bits
    756      1.54  jdolecek 		 * for major. Due to how we map our major and minor,
    757      1.54  jdolecek 		 * this just fints into our dev_t. Just mask off the
    758      1.54  jdolecek 		 * upper 16bit to remove any random junk.
    759      1.54  jdolecek 		 */
    760      1.54  jdolecek 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
    761      1.56   thorpej 		return sys_mknod(l, &bma, retval);
    762      1.54  jdolecek 	}
    763       1.2      fvdl }
    764       1.2      fvdl 
    765       1.2      fvdl int
    766      1.56   thorpej linux_sys_chmod(l, v, retval)
    767      1.56   thorpej 	struct lwp *l;
    768      1.11   thorpej 	void *v;
    769      1.11   thorpej 	register_t *retval;
    770      1.11   thorpej {
    771      1.12   mycroft 	struct linux_sys_chmod_args /* {
    772      1.27  christos 		syscallarg(const char *) path;
    773       1.2      fvdl 		syscallarg(int) mode;
    774      1.11   thorpej 	} */ *uap = v;
    775      1.56   thorpej 	struct proc *p = l->l_proc;
    776      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    777       1.2      fvdl 
    778      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    779       1.2      fvdl 
    780      1.56   thorpej 	return sys_chmod(l, uap, retval);
    781       1.2      fvdl }
    782       1.2      fvdl 
    783      1.67      manu #if defined(__i386__) || defined(__m68k__) || \
    784      1.67      manu     defined(__arm__)
    785       1.2      fvdl int
    786      1.56   thorpej linux_sys_chown16(l, v, retval)
    787      1.56   thorpej 	struct lwp *l;
    788      1.11   thorpej 	void *v;
    789      1.11   thorpej 	register_t *retval;
    790      1.11   thorpej {
    791      1.31      fvdl 	struct linux_sys_chown16_args /* {
    792      1.27  christos 		syscallarg(const char *) path;
    793       1.2      fvdl 		syscallarg(int) uid;
    794       1.2      fvdl 		syscallarg(int) gid;
    795      1.11   thorpej 	} */ *uap = v;
    796      1.56   thorpej 	struct proc *p = l->l_proc;
    797      1.22    kleink 	struct sys___posix_chown_args bca;
    798      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    799       1.2      fvdl 
    800      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    801       1.2      fvdl 
    802      1.10      fvdl 	SCARG(&bca, path) = SCARG(uap, path);
    803      1.10      fvdl 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    804      1.10      fvdl 		(uid_t)-1 : SCARG(uap, uid);
    805      1.10      fvdl 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    806      1.10      fvdl 		(gid_t)-1 : SCARG(uap, gid);
    807      1.65     perry 
    808      1.56   thorpej 	return sys___posix_chown(l, &bca, retval);
    809      1.10      fvdl }
    810      1.10      fvdl 
    811      1.10      fvdl int
    812      1.56   thorpej linux_sys_fchown16(l, v, retval)
    813      1.56   thorpej 	struct lwp *l;
    814      1.11   thorpej 	void *v;
    815      1.11   thorpej 	register_t *retval;
    816      1.11   thorpej {
    817      1.31      fvdl 	struct linux_sys_fchown16_args /* {
    818      1.10      fvdl 		syscallarg(int) fd;
    819      1.10      fvdl 		syscallarg(int) uid;
    820      1.10      fvdl 		syscallarg(int) gid;
    821      1.11   thorpej 	} */ *uap = v;
    822      1.22    kleink 	struct sys___posix_fchown_args bfa;
    823      1.10      fvdl 
    824      1.10      fvdl 	SCARG(&bfa, fd) = SCARG(uap, fd);
    825      1.10      fvdl 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    826      1.10      fvdl 		(uid_t)-1 : SCARG(uap, uid);
    827      1.10      fvdl 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    828      1.10      fvdl 		(gid_t)-1 : SCARG(uap, gid);
    829      1.65     perry 
    830      1.56   thorpej 	return sys___posix_fchown(l, &bfa, retval);
    831       1.2      fvdl }
    832       1.2      fvdl 
    833       1.2      fvdl int
    834      1.56   thorpej linux_sys_lchown16(l, v, retval)
    835      1.56   thorpej 	struct lwp *l;
    836      1.23       erh 	void *v;
    837      1.23       erh 	register_t *retval;
    838      1.23       erh {
    839      1.31      fvdl 	struct linux_sys_lchown16_args /* {
    840      1.23       erh 		syscallarg(char *) path;
    841      1.23       erh 		syscallarg(int) uid;
    842      1.23       erh 		syscallarg(int) gid;
    843      1.23       erh 	} */ *uap = v;
    844      1.56   thorpej 	struct proc *p = l->l_proc;
    845      1.23       erh 	struct sys___posix_lchown_args bla;
    846      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    847      1.33      fvdl 
    848      1.71  christos 	CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
    849      1.23       erh 
    850      1.23       erh 	SCARG(&bla, path) = SCARG(uap, path);
    851      1.23       erh 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    852      1.23       erh 		(uid_t)-1 : SCARG(uap, uid);
    853      1.23       erh 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    854      1.23       erh 		(gid_t)-1 : SCARG(uap, gid);
    855      1.23       erh 
    856      1.56   thorpej 	return sys___posix_lchown(l, &bla, retval);
    857      1.33      fvdl }
    858      1.67      manu #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
    859      1.67      manu #if defined (__i386__) || defined (__m68k__) || defined(__amd64__) || \
    860      1.67      manu     defined (__powerpc__) || defined (__mips__) || defined (__arm__)
    861      1.33      fvdl int
    862      1.56   thorpej linux_sys_chown(l, v, retval)
    863      1.56   thorpej 	struct lwp *l;
    864      1.33      fvdl 	void *v;
    865      1.33      fvdl 	register_t *retval;
    866      1.33      fvdl {
    867      1.33      fvdl 	struct linux_sys_chown_args /* {
    868      1.33      fvdl 		syscallarg(char *) path;
    869      1.33      fvdl 		syscallarg(int) uid;
    870      1.33      fvdl 		syscallarg(int) gid;
    871      1.33      fvdl 	} */ *uap = v;
    872      1.56   thorpej 	struct proc *p = l->l_proc;
    873      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    874      1.33      fvdl 
    875      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    876      1.33      fvdl 
    877      1.56   thorpej 	return sys___posix_chown(l, uap, retval);
    878      1.33      fvdl }
    879      1.33      fvdl 
    880      1.33      fvdl int
    881      1.56   thorpej linux_sys_lchown(l, v, retval)
    882      1.56   thorpej 	struct lwp *l;
    883      1.33      fvdl 	void *v;
    884      1.33      fvdl 	register_t *retval;
    885      1.33      fvdl {
    886      1.33      fvdl 	struct linux_sys_lchown_args /* {
    887      1.33      fvdl 		syscallarg(char *) path;
    888      1.33      fvdl 		syscallarg(int) uid;
    889      1.33      fvdl 		syscallarg(int) gid;
    890      1.33      fvdl 	} */ *uap = v;
    891      1.56   thorpej 	struct proc *p = l->l_proc;
    892      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    893      1.33      fvdl 
    894      1.71  christos 	CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
    895      1.33      fvdl 
    896      1.56   thorpej 	return sys___posix_lchown(l, uap, retval);
    897      1.23       erh }
    898      1.67      manu #endif /* __i386__||__m68k__||__powerpc__||__mips__||__arm__ ||__amd64__ */
    899      1.32   thorpej 
    900      1.23       erh int
    901      1.56   thorpej linux_sys_rename(l, v, retval)
    902      1.56   thorpej 	struct lwp *l;
    903      1.11   thorpej 	void *v;
    904      1.11   thorpej 	register_t *retval;
    905      1.11   thorpej {
    906      1.12   mycroft 	struct linux_sys_rename_args /* {
    907      1.27  christos 		syscallarg(const char *) from;
    908      1.27  christos 		syscallarg(const char *) to;
    909      1.11   thorpej 	} */ *uap = v;
    910      1.56   thorpej 	struct proc *p = l->l_proc;
    911      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    912       1.2      fvdl 
    913      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, from));
    914      1.71  christos 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
    915       1.2      fvdl 
    916      1.56   thorpej 	return sys___posix_rename(l, uap, retval);
    917       1.2      fvdl }
    918       1.2      fvdl 
    919       1.2      fvdl int
    920      1.56   thorpej linux_sys_mkdir(l, v, retval)
    921      1.56   thorpej 	struct lwp *l;
    922      1.11   thorpej 	void *v;
    923      1.11   thorpej 	register_t *retval;
    924      1.11   thorpej {
    925      1.12   mycroft 	struct linux_sys_mkdir_args /* {
    926      1.27  christos 		syscallarg(const char *) path;
    927       1.7      fvdl 		syscallarg(int) mode;
    928      1.11   thorpej 	} */ *uap = v;
    929      1.56   thorpej 	struct proc *p = l->l_proc;
    930      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    931       1.2      fvdl 
    932      1.71  christos 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    933      1.12   mycroft 
    934      1.56   thorpej 	return sys_mkdir(l, uap, retval);
    935       1.2      fvdl }
    936       1.2      fvdl 
    937       1.2      fvdl int
    938      1.56   thorpej linux_sys_rmdir(l, v, retval)
    939      1.56   thorpej 	struct lwp *l;
    940      1.11   thorpej 	void *v;
    941      1.11   thorpej 	register_t *retval;
    942      1.11   thorpej {
    943      1.12   mycroft 	struct linux_sys_rmdir_args /* {
    944      1.27  christos 		syscallarg(const char *) path;
    945      1.11   thorpej 	} */ *uap = v;
    946      1.56   thorpej 	struct proc *p = l->l_proc;
    947      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    948       1.2      fvdl 
    949      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    950      1.12   mycroft 
    951      1.56   thorpej 	return sys_rmdir(l, uap, retval);
    952       1.2      fvdl }
    953       1.2      fvdl 
    954       1.2      fvdl int
    955      1.56   thorpej linux_sys_symlink(l, v, retval)
    956      1.56   thorpej 	struct lwp *l;
    957      1.11   thorpej 	void *v;
    958      1.11   thorpej 	register_t *retval;
    959      1.11   thorpej {
    960      1.12   mycroft 	struct linux_sys_symlink_args /* {
    961      1.27  christos 		syscallarg(const char *) path;
    962      1.27  christos 		syscallarg(const char *) to;
    963      1.11   thorpej 	} */ *uap = v;
    964      1.56   thorpej 	struct proc *p = l->l_proc;
    965      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    966       1.2      fvdl 
    967      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    968      1.71  christos 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
    969       1.2      fvdl 
    970      1.56   thorpej 	return sys_symlink(l, uap, retval);
    971      1.34      fvdl }
    972      1.34      fvdl 
    973      1.34      fvdl int
    974      1.56   thorpej linux_sys_link(l, v, retval)
    975      1.56   thorpej 	struct lwp *l;
    976      1.34      fvdl 	void *v;
    977      1.34      fvdl 	register_t *retval;
    978      1.34      fvdl {
    979      1.34      fvdl 	struct linux_sys_link_args /* {
    980      1.34      fvdl 		syscallarg(const char *) path;
    981      1.34      fvdl 		syscallarg(const char *) link;
    982      1.34      fvdl 	} */ *uap = v;
    983      1.56   thorpej 	struct proc *p = l->l_proc;
    984      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
    985      1.34      fvdl 
    986      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    987      1.71  christos 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, link));
    988      1.34      fvdl 
    989      1.56   thorpej 	return sys_link(l, uap, retval);
    990       1.2      fvdl }
    991       1.2      fvdl 
    992       1.2      fvdl int
    993      1.56   thorpej linux_sys_readlink(l, v, retval)
    994      1.56   thorpej 	struct lwp *l;
    995      1.11   thorpej 	void *v;
    996      1.11   thorpej 	register_t *retval;
    997      1.11   thorpej {
    998      1.12   mycroft 	struct linux_sys_readlink_args /* {
    999      1.27  christos 		syscallarg(const char *) name;
   1000       1.2      fvdl 		syscallarg(char *) buf;
   1001       1.2      fvdl 		syscallarg(int) count;
   1002      1.11   thorpej 	} */ *uap = v;
   1003      1.56   thorpej 	struct proc *p = l->l_proc;
   1004      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
   1005       1.2      fvdl 
   1006      1.71  christos 	CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, name));
   1007      1.12   mycroft 
   1008      1.56   thorpej 	return sys_readlink(l, uap, retval);
   1009       1.2      fvdl }
   1010       1.2      fvdl 
   1011      1.67      manu #if !defined(__amd64__)
   1012       1.2      fvdl int
   1013      1.56   thorpej linux_sys_truncate(l, v, retval)
   1014      1.56   thorpej 	struct lwp *l;
   1015      1.11   thorpej 	void *v;
   1016      1.11   thorpej 	register_t *retval;
   1017      1.11   thorpej {
   1018      1.12   mycroft 	struct linux_sys_truncate_args /* {
   1019      1.27  christos 		syscallarg(const char *) path;
   1020       1.2      fvdl 		syscallarg(long) length;
   1021      1.11   thorpej 	} */ *uap = v;
   1022      1.56   thorpej 	struct proc *p = l->l_proc;
   1023      1.46  christos 	caddr_t sg = stackgap_init(p, 0);
   1024       1.2      fvdl 
   1025      1.71  christos 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
   1026      1.12   mycroft 
   1027      1.56   thorpej 	return compat_43_sys_truncate(l, uap, retval);
   1028      1.15      fvdl }
   1029      1.67      manu #endif /* !__amd64__ */
   1030      1.15      fvdl 
   1031      1.15      fvdl /*
   1032      1.15      fvdl  * This is just fsync() for now (just as it is in the Linux kernel)
   1033      1.23       erh  * Note: this is not implemented under Linux on Alpha and Arm
   1034      1.23       erh  *	but should still be defined in our syscalls.master.
   1035      1.23       erh  *	(syscall #148 on the arm)
   1036      1.15      fvdl  */
   1037      1.15      fvdl int
   1038      1.56   thorpej linux_sys_fdatasync(l, v, retval)
   1039      1.56   thorpej 	struct lwp *l;
   1040      1.15      fvdl 	void *v;
   1041      1.15      fvdl 	register_t *retval;
   1042      1.15      fvdl {
   1043      1.16  christos #ifdef notdef
   1044      1.15      fvdl 	struct linux_sys_fdatasync_args /* {
   1045      1.15      fvdl 		syscallarg(int) fd;
   1046      1.15      fvdl 	} */ *uap = v;
   1047      1.16  christos #endif
   1048      1.56   thorpej 	return sys_fsync(l, v, retval);
   1049      1.28      tron }
   1050      1.28      tron 
   1051      1.28      tron /*
   1052      1.28      tron  * pread(2).
   1053      1.28      tron  */
   1054      1.28      tron int
   1055      1.56   thorpej linux_sys_pread(l, v, retval)
   1056      1.56   thorpej 	struct lwp *l;
   1057      1.28      tron 	void *v;
   1058      1.28      tron 	register_t *retval;
   1059      1.28      tron {
   1060      1.28      tron 	struct linux_sys_pread_args /* {
   1061      1.28      tron 		syscallarg(int) fd;
   1062      1.28      tron 		syscallarg(void *) buf;
   1063      1.28      tron 		syscallarg(size_t) nbyte;
   1064      1.28      tron 		syscallarg(linux_off_t) offset;
   1065      1.28      tron 	} */ *uap = v;
   1066      1.28      tron 	struct sys_pread_args pra;
   1067      1.28      tron 
   1068      1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
   1069      1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
   1070      1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
   1071      1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
   1072      1.28      tron 
   1073      1.62  jdolecek 	return sys_pread(l, &pra, retval);
   1074      1.28      tron }
   1075      1.28      tron 
   1076      1.28      tron /*
   1077      1.28      tron  * pwrite(2).
   1078      1.28      tron  */
   1079      1.28      tron int
   1080      1.56   thorpej linux_sys_pwrite(l, v, retval)
   1081      1.56   thorpej 	struct lwp *l;
   1082      1.28      tron 	void *v;
   1083      1.28      tron 	register_t *retval;
   1084      1.28      tron {
   1085      1.28      tron 	struct linux_sys_pwrite_args /* {
   1086      1.28      tron 		syscallarg(int) fd;
   1087      1.28      tron 		syscallarg(void *) buf;
   1088      1.28      tron 		syscallarg(size_t) nbyte;
   1089      1.28      tron 		syscallarg(linux_off_t) offset;
   1090      1.28      tron 	} */ *uap = v;
   1091      1.28      tron 	struct sys_pwrite_args pra;
   1092      1.28      tron 
   1093      1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
   1094      1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
   1095      1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
   1096      1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
   1097      1.28      tron 
   1098      1.62  jdolecek 	return sys_pwrite(l, &pra, retval);
   1099       1.1      fvdl }
   1100      1.68      fvdl 
   1101      1.75  christos #define LINUX_NOT_SUPPORTED(fun) \
   1102      1.75  christos int \
   1103      1.76  christos fun(struct lwp *l, void *v, register_t *retval) \
   1104      1.75  christos { \
   1105      1.75  christos 	return EOPNOTSUPP; \
   1106      1.75  christos }
   1107      1.75  christos 
   1108      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_setxattr)
   1109      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
   1110      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
   1111      1.75  christos 
   1112      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_getxattr)
   1113      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
   1114      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
   1115      1.75  christos 
   1116      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_listxattr)
   1117      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
   1118      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
   1119      1.75  christos 
   1120      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_removexattr)
   1121      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
   1122      1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
   1123