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