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