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