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