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