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