Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.37.2.6
      1 /*	$NetBSD: linux_file.c,v 1.37.2.6 2001/11/14 19:13:10 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.6 2001/11/14 19:13:10 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->p_emul);
    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->p_emul);
    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->p_emul);
    382 		bfp = (struct flock *) stackgap_alloc(&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 	case LINUX_F_SETLK:
    399 	case LINUX_F_SETLKW:
    400 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
    401 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    402 			return error;
    403 		linux_to_bsd_flock(&lfl, &bfl);
    404 		sg = stackgap_init(p->p_emul);
    405 		bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
    406 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    407 			return error;
    408 		SCARG(&fca, fd) = fd;
    409 		SCARG(&fca, cmd) = cmd;
    410 		SCARG(&fca, arg) = bfp;
    411 		return sys_fcntl(l, &fca, retval);
    412 		break;
    413 	case LINUX_F_SETOWN:
    414 	case LINUX_F_GETOWN:
    415 		/*
    416 		 * We need to route around the normal fcntl() for these calls,
    417 		 * since it uses TIOC{G,S}PGRP, which is too restrictive for
    418 		 * Linux F_{G,S}ETOWN semantics. For sockets, this problem
    419 		 * does not exist.
    420 		 */
    421 		fdp = p->p_fd;
    422 		if ((fp = fd_getfile(fdp, fd)) == NULL)
    423 			return EBADF;
    424 		if (fp->f_type == DTYPE_SOCKET) {
    425 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    426 			break;
    427 		}
    428 		vp = (struct vnode *)fp->f_data;
    429 		if (vp->v_type != VCHR)
    430 			return EINVAL;
    431 		if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    432 			return error;
    433 		d_tty = cdevsw[major(va.va_rdev)].d_tty;
    434 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
    435 			return EINVAL;
    436 		if (cmd == LINUX_F_GETOWN) {
    437 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
    438 			return 0;
    439 		}
    440 		if ((long)arg <= 0) {
    441 			pgid = -(long)arg;
    442 		} else {
    443 			struct proc *p1 = pfind((long)arg);
    444 			if (p1 == 0)
    445 				return (ESRCH);
    446 			pgid = (long)p1->p_pgrp->pg_id;
    447 		}
    448 		pgrp = pgfind(pgid);
    449 		if (pgrp == NULL || pgrp->pg_session != p->p_session)
    450 			return EPERM;
    451 		tp->t_pgrp = pgrp;
    452 		return 0;
    453 	default:
    454 		return EOPNOTSUPP;
    455 	}
    456 
    457 	SCARG(&fca, fd) = fd;
    458 	SCARG(&fca, cmd) = cmd;
    459 	SCARG(&fca, arg) = arg;
    460 
    461 	return sys_fcntl(l, &fca, retval);
    462 }
    463 
    464 /*
    465  * Convert a NetBSD stat structure to a Linux stat structure.
    466  * Only the order of the fields and the padding in the structure
    467  * is different. linux_fakedev is a machine-dependent function
    468  * which optionally converts device driver major/minor numbers
    469  * (XXX horrible, but what can you do against code that compares
    470  * things against constant major device numbers? sigh)
    471  */
    472 static void
    473 bsd_to_linux_stat(bsp, lsp)
    474 	struct stat *bsp;
    475 	struct linux_stat *lsp;
    476 {
    477 
    478 	lsp->lst_dev     = bsp->st_dev;
    479 	lsp->lst_ino     = bsp->st_ino;
    480 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    481 	if (bsp->st_nlink >= (1 << 15))
    482 		lsp->lst_nlink = (1 << 15) - 1;
    483 	else
    484 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    485 	lsp->lst_uid     = bsp->st_uid;
    486 	lsp->lst_gid     = bsp->st_gid;
    487 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev);
    488 	lsp->lst_size    = bsp->st_size;
    489 	lsp->lst_blksize = bsp->st_blksize;
    490 	lsp->lst_blocks  = bsp->st_blocks;
    491 	lsp->lst_atime   = bsp->st_atime;
    492 	lsp->lst_mtime   = bsp->st_mtime;
    493 	lsp->lst_ctime   = bsp->st_ctime;
    494 }
    495 
    496 /*
    497  * The stat functions below are plain sailing. stat and lstat are handled
    498  * by one function to avoid code duplication.
    499  */
    500 int
    501 linux_sys_fstat(l, v, retval)
    502 	struct lwp *l;
    503 	void *v;
    504 	register_t *retval;
    505 {
    506 	struct linux_sys_fstat_args /* {
    507 		syscallarg(int) fd;
    508 		syscallarg(linux_stat *) sp;
    509 	} */ *uap = v;
    510 	struct proc *p = l->l_proc;
    511 	struct sys___fstat13_args fsa;
    512 	struct linux_stat tmplst;
    513 	struct stat *st,tmpst;
    514 	caddr_t sg;
    515 	int error;
    516 
    517 	sg = stackgap_init(p->p_emul);
    518 
    519 	st = stackgap_alloc(&sg, sizeof (struct stat));
    520 
    521 	SCARG(&fsa, fd) = SCARG(uap, fd);
    522 	SCARG(&fsa, sb) = st;
    523 
    524 	if ((error = sys___fstat13(l, &fsa, retval)))
    525 		return error;
    526 
    527 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    528 		return error;
    529 
    530 	bsd_to_linux_stat(&tmpst, &tmplst);
    531 
    532 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    533 		return error;
    534 
    535 	return 0;
    536 }
    537 
    538 static int
    539 linux_stat1(l, v, retval, dolstat)
    540 	struct lwp *l;
    541 	void *v;
    542 	register_t *retval;
    543 	int dolstat;
    544 {
    545 	struct sys___stat13_args sa;
    546 	struct linux_stat tmplst;
    547 	struct stat *st, tmpst;
    548 	struct proc *p = l->l_proc;
    549 	caddr_t sg;
    550 	int error;
    551 	struct linux_sys_stat_args *uap = v;
    552 
    553 	sg = stackgap_init(p->p_emul);
    554 	st = stackgap_alloc(&sg, sizeof (struct stat));
    555 	if (dolstat)
    556 		CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, path));
    557 	else
    558 		CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    559 
    560 	SCARG(&sa, ub) = st;
    561 	SCARG(&sa, path) = SCARG(uap, path);
    562 
    563 	if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
    564 				sys___stat13(l, &sa, retval))))
    565 		return error;
    566 
    567 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    568 		return error;
    569 
    570 	bsd_to_linux_stat(&tmpst, &tmplst);
    571 
    572 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    573 		return error;
    574 
    575 	return 0;
    576 }
    577 
    578 int
    579 linux_sys_stat(l, v, retval)
    580 	struct lwp *l;
    581 	void *v;
    582 	register_t *retval;
    583 {
    584 	struct linux_sys_stat_args /* {
    585 		syscallarg(const char *) path;
    586 		syscallarg(struct linux_stat *) sp;
    587 	} */ *uap = v;
    588 
    589 	return linux_stat1(l, uap, retval, 0);
    590 }
    591 
    592 /* Note: this is "newlstat" in the Linux sources */
    593 /*	(we don't bother with the old lstat currently) */
    594 int
    595 linux_sys_lstat(l, v, retval)
    596 	struct lwp *l;
    597 	void *v;
    598 	register_t *retval;
    599 {
    600 	struct linux_sys_lstat_args /* {
    601 		syscallarg(const char *) path;
    602 		syscallarg(struct linux_stat *) sp;
    603 	} */ *uap = v;
    604 
    605 	return linux_stat1(l, uap, retval, 1);
    606 }
    607 
    608 /*
    609  * The following syscalls are mostly here because of the alternate path check.
    610  */
    611 int
    612 linux_sys_access(l, v, retval)
    613 	struct lwp *l;
    614 	void *v;
    615 	register_t *retval;
    616 {
    617 	struct linux_sys_access_args /* {
    618 		syscallarg(const char *) path;
    619 		syscallarg(int) flags;
    620 	} */ *uap = v;
    621 	struct proc *p = l->l_proc;
    622 	caddr_t sg = stackgap_init(p->p_emul);
    623 
    624 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    625 
    626 	return sys_access(l, uap, retval);
    627 }
    628 
    629 int
    630 linux_sys_unlink(l, v, retval)
    631 	struct lwp *l;
    632 	void *v;
    633 	register_t *retval;
    634 
    635 {
    636 	struct linux_sys_unlink_args /* {
    637 		syscallarg(const char *) path;
    638 	} */ *uap = v;
    639 	struct proc *p = l->l_proc;
    640 	caddr_t sg = stackgap_init(p->p_emul);
    641 
    642 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    643 
    644 	return sys_unlink(l, uap, retval);
    645 }
    646 
    647 int
    648 linux_sys_chdir(l, v, retval)
    649 	struct lwp *l;
    650 	void *v;
    651 	register_t *retval;
    652 {
    653 	struct linux_sys_chdir_args /* {
    654 		syscallarg(const char *) path;
    655 	} */ *uap = v;
    656 	struct proc *p = l->l_proc;
    657 	caddr_t sg = stackgap_init(p->p_emul);
    658 
    659 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    660 
    661 	return sys_chdir(l, uap, retval);
    662 }
    663 
    664 int
    665 linux_sys_mknod(l, v, retval)
    666 	struct lwp *l;
    667 	void *v;
    668 	register_t *retval;
    669 {
    670 	struct linux_sys_mknod_args /* {
    671 		syscallarg(const char *) path;
    672 		syscallarg(int) mode;
    673 		syscallarg(int) dev;
    674 	} */ *uap = v;
    675 	struct proc *p = l->l_proc;
    676 	caddr_t sg = stackgap_init(p->p_emul);
    677 	struct sys_mkfifo_args bma;
    678 
    679 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    680 
    681 	/*
    682 	 * BSD handles FIFOs separately
    683 	 */
    684 	if (SCARG(uap, mode) & S_IFIFO) {
    685 		SCARG(&bma, path) = SCARG(uap, path);
    686 		SCARG(&bma, mode) = SCARG(uap, mode);
    687 		return sys_mkfifo(l, uap, retval);
    688 	} else
    689 		return sys_mknod(l, uap, retval);
    690 }
    691 
    692 int
    693 linux_sys_chmod(l, v, retval)
    694 	struct lwp *l;
    695 	void *v;
    696 	register_t *retval;
    697 {
    698 	struct linux_sys_chmod_args /* {
    699 		syscallarg(const char *) path;
    700 		syscallarg(int) mode;
    701 	} */ *uap = v;
    702 	struct proc *p = l->l_proc;
    703 	caddr_t sg = stackgap_init(p->p_emul);
    704 
    705 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    706 
    707 	return sys_chmod(l, uap, retval);
    708 }
    709 
    710 #if defined(__i386__) || defined(__m68k__)
    711 int
    712 linux_sys_chown16(l, v, retval)
    713 	struct lwp *l;
    714 	void *v;
    715 	register_t *retval;
    716 {
    717 	struct linux_sys_chown16_args /* {
    718 		syscallarg(const char *) path;
    719 		syscallarg(int) uid;
    720 		syscallarg(int) gid;
    721 	} */ *uap = v;
    722 	struct proc *p = l->l_proc;
    723 	struct sys___posix_chown_args bca;
    724 	caddr_t sg = stackgap_init(p->p_emul);
    725 
    726 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    727 
    728 	SCARG(&bca, path) = SCARG(uap, path);
    729 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    730 		(uid_t)-1 : SCARG(uap, uid);
    731 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    732 		(gid_t)-1 : SCARG(uap, gid);
    733 
    734 	return sys___posix_chown(l, &bca, retval);
    735 }
    736 
    737 int
    738 linux_sys_fchown16(l, v, retval)
    739 	struct lwp *l;
    740 	void *v;
    741 	register_t *retval;
    742 {
    743 	struct linux_sys_fchown16_args /* {
    744 		syscallarg(int) fd;
    745 		syscallarg(int) uid;
    746 		syscallarg(int) gid;
    747 	} */ *uap = v;
    748 	struct sys___posix_fchown_args bfa;
    749 
    750 	SCARG(&bfa, fd) = SCARG(uap, fd);
    751 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    752 		(uid_t)-1 : SCARG(uap, uid);
    753 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    754 		(gid_t)-1 : SCARG(uap, gid);
    755 
    756 	return sys___posix_fchown(l, &bfa, retval);
    757 }
    758 
    759 int
    760 linux_sys_lchown16(l, v, retval)
    761 	struct lwp *l;
    762 	void *v;
    763 	register_t *retval;
    764 {
    765 	struct linux_sys_lchown16_args /* {
    766 		syscallarg(char *) path;
    767 		syscallarg(int) uid;
    768 		syscallarg(int) gid;
    769 	} */ *uap = v;
    770 	struct proc *p = l->l_proc;
    771 	struct sys___posix_lchown_args bla;
    772 	caddr_t sg = stackgap_init(p->p_emul);
    773 
    774 	CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, path));
    775 
    776 	SCARG(&bla, path) = SCARG(uap, path);
    777 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    778 		(uid_t)-1 : SCARG(uap, uid);
    779 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    780 		(gid_t)-1 : SCARG(uap, gid);
    781 
    782 	return sys___posix_lchown(l, &bla, retval);
    783 }
    784 #endif /* __i386__ || __m68k__ */
    785 #if defined (__i386__) || defined (__m68k__) || \
    786     defined (__powerpc__) || defined (__mips__)
    787 int
    788 linux_sys_chown(l, v, retval)
    789 	struct lwp *l;
    790 	void *v;
    791 	register_t *retval;
    792 {
    793 	struct linux_sys_chown_args /* {
    794 		syscallarg(char *) path;
    795 		syscallarg(int) uid;
    796 		syscallarg(int) gid;
    797 	} */ *uap = v;
    798 	struct proc *p = l->l_proc;
    799 	caddr_t sg = stackgap_init(p->p_emul);
    800 
    801 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    802 
    803 	return sys___posix_chown(l, uap, retval);
    804 }
    805 
    806 int
    807 linux_sys_lchown(l, v, retval)
    808 	struct lwp *l;
    809 	void *v;
    810 	register_t *retval;
    811 {
    812 	struct linux_sys_lchown_args /* {
    813 		syscallarg(char *) path;
    814 		syscallarg(int) uid;
    815 		syscallarg(int) gid;
    816 	} */ *uap = v;
    817 	struct proc *p = l->l_proc;
    818 	caddr_t sg = stackgap_init(p->p_emul);
    819 
    820 	CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, path));
    821 
    822 	return sys___posix_lchown(l, uap, retval);
    823 }
    824 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ */
    825 
    826 int
    827 linux_sys_rename(l, v, retval)
    828 	struct lwp *l;
    829 	void *v;
    830 	register_t *retval;
    831 {
    832 	struct linux_sys_rename_args /* {
    833 		syscallarg(const char *) from;
    834 		syscallarg(const char *) to;
    835 	} */ *uap = v;
    836 	struct proc *p = l->l_proc;
    837 	caddr_t sg = stackgap_init(p->p_emul);
    838 
    839 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
    840 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
    841 
    842 	return sys___posix_rename(l, uap, retval);
    843 }
    844 
    845 int
    846 linux_sys_mkdir(l, v, retval)
    847 	struct lwp *l;
    848 	void *v;
    849 	register_t *retval;
    850 {
    851 	struct linux_sys_mkdir_args /* {
    852 		syscallarg(const char *) path;
    853 		syscallarg(int) mode;
    854 	} */ *uap = v;
    855 	struct proc *p = l->l_proc;
    856 	caddr_t sg = stackgap_init(p->p_emul);
    857 
    858 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    859 
    860 	return sys_mkdir(l, uap, retval);
    861 }
    862 
    863 int
    864 linux_sys_rmdir(l, v, retval)
    865 	struct lwp *l;
    866 	void *v;
    867 	register_t *retval;
    868 {
    869 	struct linux_sys_rmdir_args /* {
    870 		syscallarg(const char *) path;
    871 	} */ *uap = v;
    872 	struct proc *p = l->l_proc;
    873 	caddr_t sg = stackgap_init(p->p_emul);
    874 
    875 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    876 
    877 	return sys_rmdir(l, uap, retval);
    878 }
    879 
    880 int
    881 linux_sys_symlink(l, v, retval)
    882 	struct lwp *l;
    883 	void *v;
    884 	register_t *retval;
    885 {
    886 	struct linux_sys_symlink_args /* {
    887 		syscallarg(const char *) path;
    888 		syscallarg(const char *) to;
    889 	} */ *uap = v;
    890 	struct proc *p = l->l_proc;
    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, to));
    895 
    896 	return sys_symlink(l, uap, retval);
    897 }
    898 
    899 int
    900 linux_sys_link(l, v, retval)
    901 	struct lwp *l;
    902 	void *v;
    903 	register_t *retval;
    904 {
    905 	struct linux_sys_link_args /* {
    906 		syscallarg(const char *) path;
    907 		syscallarg(const char *) link;
    908 	} */ *uap = v;
    909 	struct proc *p = l->l_proc;
    910 	caddr_t sg = stackgap_init(p->p_emul);
    911 
    912 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    913 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, link));
    914 
    915 	return sys_link(l, uap, retval);
    916 }
    917 
    918 int
    919 linux_sys_readlink(l, v, retval)
    920 	struct lwp *l;
    921 	void *v;
    922 	register_t *retval;
    923 {
    924 	struct linux_sys_readlink_args /* {
    925 		syscallarg(const char *) name;
    926 		syscallarg(char *) buf;
    927 		syscallarg(int) count;
    928 	} */ *uap = v;
    929 	struct proc *p = l->l_proc;
    930 	caddr_t sg = stackgap_init(p->p_emul);
    931 
    932 	CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, name));
    933 
    934 	return sys_readlink(l, uap, retval);
    935 }
    936 
    937 int
    938 linux_sys_truncate(l, v, retval)
    939 	struct lwp *l;
    940 	void *v;
    941 	register_t *retval;
    942 {
    943 	struct linux_sys_truncate_args /* {
    944 		syscallarg(const char *) path;
    945 		syscallarg(long) length;
    946 	} */ *uap = v;
    947 	struct proc *p = l->l_proc;
    948 	caddr_t sg = stackgap_init(p->p_emul);
    949 
    950 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    951 
    952 	return compat_43_sys_truncate(l, uap, retval);
    953 }
    954 
    955 /*
    956  * This is just fsync() for now (just as it is in the Linux kernel)
    957  * Note: this is not implemented under Linux on Alpha and Arm
    958  *	but should still be defined in our syscalls.master.
    959  *	(syscall #148 on the arm)
    960  */
    961 int
    962 linux_sys_fdatasync(l, v, retval)
    963 	struct lwp *l;
    964 	void *v;
    965 	register_t *retval;
    966 {
    967 #ifdef notdef
    968 	struct linux_sys_fdatasync_args /* {
    969 		syscallarg(int) fd;
    970 	} */ *uap = v;
    971 #endif
    972 	return sys_fsync(l, v, retval);
    973 }
    974 
    975 /*
    976  * pread(2).
    977  */
    978 int
    979 linux_sys_pread(l, v, retval)
    980 	struct lwp *l;
    981 	void *v;
    982 	register_t *retval;
    983 {
    984 	struct linux_sys_pread_args /* {
    985 		syscallarg(int) fd;
    986 		syscallarg(void *) buf;
    987 		syscallarg(size_t) nbyte;
    988 		syscallarg(linux_off_t) offset;
    989 	} */ *uap = v;
    990 	struct sys_pread_args pra;
    991 
    992 	SCARG(&pra, fd) = SCARG(uap, fd);
    993 	SCARG(&pra, buf) = SCARG(uap, buf);
    994 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    995 	SCARG(&pra, offset) = SCARG(uap, offset);
    996 
    997 	return sys_read(l, &pra, retval);
    998 }
    999 
   1000 /*
   1001  * pwrite(2).
   1002  */
   1003 int
   1004 linux_sys_pwrite(l, v, retval)
   1005 	struct lwp *l;
   1006 	void *v;
   1007 	register_t *retval;
   1008 {
   1009 	struct linux_sys_pwrite_args /* {
   1010 		syscallarg(int) fd;
   1011 		syscallarg(void *) buf;
   1012 		syscallarg(size_t) nbyte;
   1013 		syscallarg(linux_off_t) offset;
   1014 	} */ *uap = v;
   1015 	struct sys_pwrite_args pra;
   1016 
   1017 	SCARG(&pra, fd) = SCARG(uap, fd);
   1018 	SCARG(&pra, buf) = SCARG(uap, buf);
   1019 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
   1020 	SCARG(&pra, offset) = SCARG(uap, offset);
   1021 
   1022 	return sys_write(l, &pra, retval);
   1023 }
   1024