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