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