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