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