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