Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.80
      1 /*	$NetBSD: linux_file.c,v 1.80 2007/03/09 14:11:28 ad 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.80 2007/03/09 14:11:28 ad 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 #ifndef __amd64__
     79 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
     80 static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
     81 #endif
     82 
     83 /*
     84  * Some file-related calls are handled here. The usual flag conversion
     85  * an structure conversion is done, and alternate emul path searching.
     86  */
     87 
     88 /*
     89  * The next two functions convert between the Linux and NetBSD values
     90  * of the flags used in open(2) and fcntl(2).
     91  */
     92 static int
     93 linux_to_bsd_ioflags(lflags)
     94 	int lflags;
     95 {
     96 	int res = 0;
     97 
     98 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
     99 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
    100 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
    101 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
    102 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
    103 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
    104 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
    105 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
    106 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
    107 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
    108 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
    109 
    110 	return res;
    111 }
    112 
    113 static int
    114 bsd_to_linux_ioflags(bflags)
    115 	int bflags;
    116 {
    117 	int res = 0;
    118 
    119 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    120 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    121 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    122 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    123 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    124 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    125 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    126 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    127 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    128 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    129 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    130 
    131 	return res;
    132 }
    133 
    134 /*
    135  * creat(2) is an obsolete function, but it's present as a Linux
    136  * system call, so let's deal with it.
    137  *
    138  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    139  * in syscalls.master anyway so this doesn't have to be special cased.
    140  *
    141  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    142  */
    143 int
    144 linux_sys_creat(l, v, retval)
    145 	struct lwp *l;
    146 	void *v;
    147 	register_t *retval;
    148 {
    149 	struct linux_sys_creat_args /* {
    150 		syscallarg(const char *) path;
    151 		syscallarg(int) mode;
    152 	} */ *uap = v;
    153 	struct proc *p = l->l_proc;
    154 	struct sys_open_args oa;
    155 	void *sg;
    156 
    157 	sg = stackgap_init(p, 0);
    158 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    159 
    160 	SCARG(&oa, path) = SCARG(uap, path);
    161 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    162 	SCARG(&oa, mode) = SCARG(uap, mode);
    163 
    164 	return sys_open(l, &oa, retval);
    165 }
    166 
    167 /*
    168  * open(2). Take care of the different flag values, and let the
    169  * NetBSD syscall do the real work. See if this operation
    170  * gives the current process a controlling terminal.
    171  * (XXX is this necessary?)
    172  */
    173 int
    174 linux_sys_open(l, v, retval)
    175 	struct lwp *l;
    176 	void *v;
    177 	register_t *retval;
    178 {
    179 	struct linux_sys_open_args /* {
    180 		syscallarg(const char *) path;
    181 		syscallarg(int) flags;
    182 		syscallarg(int) mode;
    183 	} */ *uap = v;
    184 	struct proc *p = l->l_proc;
    185 	int error, fl;
    186 	struct sys_open_args boa;
    187 	void *sg;
    188 
    189 	sg = stackgap_init(p, 0);
    190 
    191 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    192 
    193 	if (fl & O_CREAT)
    194 		CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    195 	else
    196 		CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    197 
    198 	SCARG(&boa, path) = SCARG(uap, path);
    199 	SCARG(&boa, flags) = fl;
    200 	SCARG(&boa, mode) = SCARG(uap, mode);
    201 
    202 	if ((error = sys_open(l, &boa, retval)))
    203 		return error;
    204 
    205 	/*
    206 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    207 	 * If we are a session leader, and we don't have a controlling
    208 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    209 	 * this the controlling terminal.
    210 	 */
    211         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    212                 struct filedesc *fdp = p->p_fd;
    213                 struct file     *fp;
    214 
    215 		fp = fd_getfile(fdp, *retval);
    216 
    217                 /* ignore any error, just give it a try */
    218                 if (fp != NULL) {
    219 			FILE_USE(fp);
    220 			if (fp->f_type == DTYPE_VNODE) {
    221 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
    222 				    (void *) 0, l);
    223 			}
    224 			FILE_UNUSE(fp, l);
    225 		}
    226         }
    227 	return 0;
    228 }
    229 
    230 /*
    231  * The next two functions take care of converting the flock
    232  * structure back and forth between Linux and NetBSD format.
    233  * The only difference in the structures is the order of
    234  * the fields, and the 'whence' value.
    235  */
    236 static void
    237 bsd_to_linux_flock(bfp, lfp)
    238 	struct flock *bfp;
    239 	struct linux_flock *lfp;
    240 {
    241 
    242 	lfp->l_start = bfp->l_start;
    243 	lfp->l_len = bfp->l_len;
    244 	lfp->l_pid = bfp->l_pid;
    245 	lfp->l_whence = bfp->l_whence;
    246 	switch (bfp->l_type) {
    247 	case F_RDLCK:
    248 		lfp->l_type = LINUX_F_RDLCK;
    249 		break;
    250 	case F_UNLCK:
    251 		lfp->l_type = LINUX_F_UNLCK;
    252 		break;
    253 	case F_WRLCK:
    254 		lfp->l_type = LINUX_F_WRLCK;
    255 		break;
    256 	}
    257 }
    258 
    259 static void
    260 linux_to_bsd_flock(lfp, bfp)
    261 	struct linux_flock *lfp;
    262 	struct flock *bfp;
    263 {
    264 
    265 	bfp->l_start = lfp->l_start;
    266 	bfp->l_len = lfp->l_len;
    267 	bfp->l_pid = lfp->l_pid;
    268 	bfp->l_whence = lfp->l_whence;
    269 	switch (lfp->l_type) {
    270 	case LINUX_F_RDLCK:
    271 		bfp->l_type = F_RDLCK;
    272 		break;
    273 	case LINUX_F_UNLCK:
    274 		bfp->l_type = F_UNLCK;
    275 		break;
    276 	case LINUX_F_WRLCK:
    277 		bfp->l_type = F_WRLCK;
    278 		break;
    279 	}
    280 }
    281 
    282 /*
    283  * Most actions in the fcntl() call are straightforward; simply
    284  * pass control to the NetBSD system call. A few commands need
    285  * conversions after the actual system call has done its work,
    286  * because the flag values and lock structure are different.
    287  */
    288 int
    289 linux_sys_fcntl(l, v, retval)
    290 	struct lwp *l;
    291 	void *v;
    292 	register_t *retval;
    293 {
    294 	struct linux_sys_fcntl_args /* {
    295 		syscallarg(int) fd;
    296 		syscallarg(int) cmd;
    297 		syscallarg(void *) arg;
    298 	} */ *uap = v;
    299 	struct proc *p = l->l_proc;
    300 	int fd, cmd, error;
    301 	u_long val;
    302 	void *arg, *sg;
    303 	struct linux_flock lfl;
    304 	struct flock *bfp, bfl;
    305 	struct sys_fcntl_args fca;
    306 	struct filedesc *fdp;
    307 	struct file *fp;
    308 	struct vnode *vp;
    309 	struct vattr va;
    310 	const struct cdevsw *cdev;
    311 	long pgid;
    312 	struct pgrp *pgrp;
    313 	struct tty *tp, *(*d_tty) __P((dev_t));
    314 
    315 	fd = SCARG(uap, fd);
    316 	cmd = SCARG(uap, cmd);
    317 	arg = (void *) SCARG(uap, arg);
    318 
    319 	switch (cmd) {
    320 	case LINUX_F_DUPFD:
    321 		cmd = F_DUPFD;
    322 		break;
    323 	case LINUX_F_GETFD:
    324 		cmd = F_GETFD;
    325 		break;
    326 	case LINUX_F_SETFD:
    327 		cmd = F_SETFD;
    328 		break;
    329 	case LINUX_F_GETFL:
    330 		SCARG(&fca, fd) = fd;
    331 		SCARG(&fca, cmd) = F_GETFL;
    332 		SCARG(&fca, arg) = arg;
    333 		if ((error = sys_fcntl(l, &fca, retval)))
    334 			return error;
    335 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    336 		return 0;
    337 	case LINUX_F_SETFL: {
    338 		struct file	*fp1 = NULL;
    339 
    340 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    341 		/*
    342 		 * Linux seems to have same semantics for sending SIGIO to the
    343 		 * read side of socket, but slightly different semantics
    344 		 * for SIGIO to the write side.  Rather than sending the SIGIO
    345 		 * every time it's possible to write (directly) more data, it
    346 		 * only sends SIGIO if last write(2) failed due to insufficient
    347 		 * memory to hold the data. This is compatible enough
    348 		 * with NetBSD semantics to not do anything about the
    349 		 * difference.
    350 		 *
    351 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
    352 		 * ones and DTYPE_PIPE ones. For these, we don't set
    353 		 * the underlying flags (we don't pass O_ASYNC flag down
    354 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
    355 		 * so that F_GETFL would report the ASYNC i/o is on.
    356 		 */
    357 		if (val & O_ASYNC) {
    358 			if (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
    359 			    return (EBADF);
    360 
    361 			FILE_USE(fp1);
    362 
    363 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    364 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    365 			    || (fp1->f_type == DTYPE_PIPE))
    366 				val &= ~O_ASYNC;
    367 			else {
    368 				/* not a pipe, do not modify anything */
    369 				FILE_UNUSE(fp1, l);
    370 				fp1 = NULL;
    371 			}
    372 		}
    373 
    374 		SCARG(&fca, fd) = fd;
    375 		SCARG(&fca, cmd) = F_SETFL;
    376 		SCARG(&fca, arg) = (void *) val;
    377 
    378 		error = sys_fcntl(l, &fca, retval);
    379 
    380 		/* Now set the FASYNC flag for pipes */
    381 		if (fp1) {
    382 			if (!error)
    383 				fp1->f_flag |= FASYNC;
    384 			FILE_UNUSE(fp1, l);
    385 		}
    386 
    387 		return (error);
    388 	    }
    389 	case LINUX_F_GETLK:
    390 		sg = stackgap_init(p, 0);
    391 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    392 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    393 			return error;
    394 		linux_to_bsd_flock(&lfl, &bfl);
    395 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    396 			return error;
    397 		SCARG(&fca, fd) = fd;
    398 		SCARG(&fca, cmd) = F_GETLK;
    399 		SCARG(&fca, arg) = bfp;
    400 		if ((error = sys_fcntl(l, &fca, retval)))
    401 			return error;
    402 		if ((error = copyin(bfp, &bfl, sizeof bfl)))
    403 			return error;
    404 		bsd_to_linux_flock(&bfl, &lfl);
    405 		return copyout(&lfl, arg, sizeof lfl);
    406 
    407 	case LINUX_F_SETLK:
    408 	case LINUX_F_SETLKW:
    409 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
    410 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    411 			return error;
    412 		linux_to_bsd_flock(&lfl, &bfl);
    413 		sg = stackgap_init(p, 0);
    414 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    415 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    416 			return error;
    417 		arg = (void *)bfp;
    418 		break;
    419 
    420 	case LINUX_F_SETOWN:
    421 	case LINUX_F_GETOWN:
    422 		/*
    423 		 * We need to route fcntl() for tty descriptors around normal
    424 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    425 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    426 		 * this is not a problem.
    427 		 */
    428 		fdp = p->p_fd;
    429 		if ((fp = fd_getfile(fdp, fd)) == NULL)
    430 			return EBADF;
    431 		FILE_USE(fp);
    432 
    433 		/* Check it's a character device vnode */
    434 		if (fp->f_type != DTYPE_VNODE
    435 		    || (vp = (struct vnode *)fp->f_data) == NULL
    436 		    || vp->v_type != VCHR) {
    437 			FILE_UNUSE(fp, l);
    438 
    439 	    not_tty:
    440 			/* Not a tty, proceed with common fcntl() */
    441 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    442 			break;
    443 		}
    444 
    445 		error = VOP_GETATTR(vp, &va, l->l_cred, l);
    446 
    447 		FILE_UNUSE(fp, l);
    448 
    449 		if (error)
    450 			return error;
    451 
    452 		cdev = cdevsw_lookup(va.va_rdev);
    453 		if (cdev == NULL)
    454 			return (ENXIO);
    455 		d_tty = cdev->d_tty;
    456 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
    457 			goto not_tty;
    458 
    459 		/* set tty pg_id appropriately */
    460 		if (cmd == LINUX_F_GETOWN) {
    461 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    462 			return 0;
    463 		}
    464 		mutex_enter(&proclist_lock);
    465 		if ((long)arg <= 0) {
    466 			pgid = -(long)arg;
    467 		} else {
    468 			struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
    469 			if (p1 == NULL)
    470 				return (ESRCH);
    471 			pgid = (long)p1->p_pgrp->pg_id;
    472 		}
    473 		pgrp = pg_find(pgid, PFIND_LOCKED);
    474 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    475 			mutex_exit(&proclist_lock);
    476 			return EPERM;
    477 		}
    478 		tp->t_pgrp = pgrp;
    479 		mutex_exit(&proclist_lock);
    480 		return 0;
    481 
    482 	default:
    483 		return EOPNOTSUPP;
    484 	}
    485 
    486 	SCARG(&fca, fd) = fd;
    487 	SCARG(&fca, cmd) = cmd;
    488 	SCARG(&fca, arg) = arg;
    489 
    490 	return sys_fcntl(l, &fca, retval);
    491 }
    492 
    493 #if !defined(__amd64__)
    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 #ifdef LINUX_STAT_HAS_NSEC
    525 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    526 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    527 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    528 #endif
    529 }
    530 
    531 /*
    532  * The stat functions below are plain sailing. stat and lstat are handled
    533  * by one function to avoid code duplication.
    534  */
    535 int
    536 linux_sys_fstat(l, v, retval)
    537 	struct lwp *l;
    538 	void *v;
    539 	register_t *retval;
    540 {
    541 	struct linux_sys_fstat_args /* {
    542 		syscallarg(int) fd;
    543 		syscallarg(linux_stat *) sp;
    544 	} */ *uap = v;
    545 	struct proc *p = l->l_proc;
    546 	struct sys___fstat30_args fsa;
    547 	struct linux_stat tmplst;
    548 	struct stat *st,tmpst;
    549 	void *sg;
    550 	int error;
    551 
    552 	sg = stackgap_init(p, 0);
    553 
    554 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    555 
    556 	SCARG(&fsa, fd) = SCARG(uap, fd);
    557 	SCARG(&fsa, sb) = st;
    558 
    559 	if ((error = sys___fstat30(l, &fsa, retval)))
    560 		return error;
    561 
    562 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    563 		return error;
    564 
    565 	bsd_to_linux_stat(&tmpst, &tmplst);
    566 
    567 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    568 		return error;
    569 
    570 	return 0;
    571 }
    572 
    573 static int
    574 linux_stat1(l, v, retval, dolstat)
    575 	struct lwp *l;
    576 	void *v;
    577 	register_t *retval;
    578 	int dolstat;
    579 {
    580 	struct sys___stat30_args sa;
    581 	struct linux_stat tmplst;
    582 	struct stat *st, tmpst;
    583 	struct proc *p = l->l_proc;
    584 	void *sg;
    585 	int error;
    586 	struct linux_sys_stat_args *uap = v;
    587 
    588 	sg = stackgap_init(p, 0);
    589 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
    590 	if (dolstat)
    591 		CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
    592 	else
    593 		CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    594 
    595 	SCARG(&sa, ub) = st;
    596 	SCARG(&sa, path) = SCARG(uap, path);
    597 
    598 	if ((error = (dolstat ? sys___lstat30(l, &sa, retval) :
    599 				sys___stat30(l, &sa, retval))))
    600 		return error;
    601 
    602 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    603 		return error;
    604 
    605 	bsd_to_linux_stat(&tmpst, &tmplst);
    606 
    607 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    608 		return error;
    609 
    610 	return 0;
    611 }
    612 
    613 int
    614 linux_sys_stat(l, v, retval)
    615 	struct lwp *l;
    616 	void *v;
    617 	register_t *retval;
    618 {
    619 	struct linux_sys_stat_args /* {
    620 		syscallarg(const char *) path;
    621 		syscallarg(struct linux_stat *) sp;
    622 	} */ *uap = v;
    623 
    624 	return linux_stat1(l, uap, retval, 0);
    625 }
    626 
    627 /* Note: this is "newlstat" in the Linux sources */
    628 /*	(we don't bother with the old lstat currently) */
    629 int
    630 linux_sys_lstat(l, v, retval)
    631 	struct lwp *l;
    632 	void *v;
    633 	register_t *retval;
    634 {
    635 	struct linux_sys_lstat_args /* {
    636 		syscallarg(const char *) path;
    637 		syscallarg(struct linux_stat *) sp;
    638 	} */ *uap = v;
    639 
    640 	return linux_stat1(l, uap, retval, 1);
    641 }
    642 #endif /* !__amd64__ */
    643 
    644 /*
    645  * The following syscalls are mostly here because of the alternate path check.
    646  */
    647 int
    648 linux_sys_access(l, v, retval)
    649 	struct lwp *l;
    650 	void *v;
    651 	register_t *retval;
    652 {
    653 	struct linux_sys_access_args /* {
    654 		syscallarg(const char *) path;
    655 		syscallarg(int) flags;
    656 	} */ *uap = v;
    657 	struct proc *p = l->l_proc;
    658 	void *sg = stackgap_init(p, 0);
    659 
    660 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    661 
    662 	return sys_access(l, uap, retval);
    663 }
    664 
    665 int
    666 linux_sys_unlink(l, v, retval)
    667 	struct lwp *l;
    668 	void *v;
    669 	register_t *retval;
    670 
    671 {
    672 	struct linux_sys_unlink_args /* {
    673 		syscallarg(const char *) path;
    674 	} */ *uap = v;
    675 	struct proc *p = l->l_proc;
    676 	void *sg = stackgap_init(p, 0);
    677 	int error;
    678 	struct nameidata nd;
    679 
    680 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    681 
    682 	error = sys_unlink(l, uap, retval);
    683 	if (error != EPERM)
    684 		return (error);
    685 
    686 	/*
    687 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    688 	 * We return EPERM in such cases. To emulate correct behaviour,
    689 	 * check if the path points to directory and return EISDIR if this
    690 	 * is the case.
    691 	 */
    692 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    693 	    SCARG(uap, path), l);
    694 	if (namei(&nd) == 0) {
    695 		struct stat sb;
    696 
    697 		if (vn_stat(nd.ni_vp, &sb, l) == 0
    698 		    && S_ISDIR(sb.st_mode))
    699 			error = EISDIR;
    700 
    701 		vput(nd.ni_vp);
    702 	}
    703 
    704 	return (error);
    705 }
    706 
    707 int
    708 linux_sys_chdir(l, v, retval)
    709 	struct lwp *l;
    710 	void *v;
    711 	register_t *retval;
    712 {
    713 	struct linux_sys_chdir_args /* {
    714 		syscallarg(const char *) path;
    715 	} */ *uap = v;
    716 	struct proc *p = l->l_proc;
    717 	void *sg = stackgap_init(p, 0);
    718 
    719 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    720 
    721 	return sys_chdir(l, uap, retval);
    722 }
    723 
    724 int
    725 linux_sys_mknod(l, v, retval)
    726 	struct lwp *l;
    727 	void *v;
    728 	register_t *retval;
    729 {
    730 	struct linux_sys_mknod_args /* {
    731 		syscallarg(const char *) path;
    732 		syscallarg(int) mode;
    733 		syscallarg(int) dev;
    734 	} */ *uap = v;
    735 	struct proc *p = l->l_proc;
    736 	void *sg = stackgap_init(p, 0);
    737 
    738 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    739 
    740 	/*
    741 	 * BSD handles FIFOs separately
    742 	 */
    743 	if (S_ISFIFO(SCARG(uap, mode))) {
    744 		struct sys_mkfifo_args bma;
    745 
    746 		SCARG(&bma, path) = SCARG(uap, path);
    747 		SCARG(&bma, mode) = SCARG(uap, mode);
    748 		return sys_mkfifo(l, &bma, retval);
    749 	} else {
    750 		struct sys_mknod_args bma;
    751 
    752 		SCARG(&bma, path) = SCARG(uap, path);
    753 		SCARG(&bma, mode) = SCARG(uap, mode);
    754 		/*
    755 		 * Linux device numbers uses 8 bits for minor and 8 bits
    756 		 * for major. Due to how we map our major and minor,
    757 		 * this just fints into our dev_t. Just mask off the
    758 		 * upper 16bit to remove any random junk.
    759 		 */
    760 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
    761 		return sys_mknod(l, &bma, retval);
    762 	}
    763 }
    764 
    765 int
    766 linux_sys_chmod(l, v, retval)
    767 	struct lwp *l;
    768 	void *v;
    769 	register_t *retval;
    770 {
    771 	struct linux_sys_chmod_args /* {
    772 		syscallarg(const char *) path;
    773 		syscallarg(int) mode;
    774 	} */ *uap = v;
    775 	struct proc *p = l->l_proc;
    776 	void *sg = stackgap_init(p, 0);
    777 
    778 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    779 
    780 	return sys_chmod(l, uap, retval);
    781 }
    782 
    783 #if defined(__i386__) || defined(__m68k__) || \
    784     defined(__arm__)
    785 int
    786 linux_sys_chown16(l, v, retval)
    787 	struct lwp *l;
    788 	void *v;
    789 	register_t *retval;
    790 {
    791 	struct linux_sys_chown16_args /* {
    792 		syscallarg(const char *) path;
    793 		syscallarg(int) uid;
    794 		syscallarg(int) gid;
    795 	} */ *uap = v;
    796 	struct proc *p = l->l_proc;
    797 	struct sys___posix_chown_args bca;
    798 	void *sg = stackgap_init(p, 0);
    799 
    800 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    801 
    802 	SCARG(&bca, path) = SCARG(uap, path);
    803 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    804 		(uid_t)-1 : SCARG(uap, uid);
    805 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    806 		(gid_t)-1 : SCARG(uap, gid);
    807 
    808 	return sys___posix_chown(l, &bca, retval);
    809 }
    810 
    811 int
    812 linux_sys_fchown16(l, v, retval)
    813 	struct lwp *l;
    814 	void *v;
    815 	register_t *retval;
    816 {
    817 	struct linux_sys_fchown16_args /* {
    818 		syscallarg(int) fd;
    819 		syscallarg(int) uid;
    820 		syscallarg(int) gid;
    821 	} */ *uap = v;
    822 	struct sys___posix_fchown_args bfa;
    823 
    824 	SCARG(&bfa, fd) = SCARG(uap, fd);
    825 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    826 		(uid_t)-1 : SCARG(uap, uid);
    827 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    828 		(gid_t)-1 : SCARG(uap, gid);
    829 
    830 	return sys___posix_fchown(l, &bfa, retval);
    831 }
    832 
    833 int
    834 linux_sys_lchown16(l, v, retval)
    835 	struct lwp *l;
    836 	void *v;
    837 	register_t *retval;
    838 {
    839 	struct linux_sys_lchown16_args /* {
    840 		syscallarg(char *) path;
    841 		syscallarg(int) uid;
    842 		syscallarg(int) gid;
    843 	} */ *uap = v;
    844 	struct proc *p = l->l_proc;
    845 	struct sys___posix_lchown_args bla;
    846 	void *sg = stackgap_init(p, 0);
    847 
    848 	CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
    849 
    850 	SCARG(&bla, path) = SCARG(uap, path);
    851 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    852 		(uid_t)-1 : SCARG(uap, uid);
    853 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    854 		(gid_t)-1 : SCARG(uap, gid);
    855 
    856 	return sys___posix_lchown(l, &bla, retval);
    857 }
    858 #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
    859 #if defined (__i386__) || defined (__m68k__) || defined(__amd64__) || \
    860     defined (__powerpc__) || defined (__mips__) || defined (__arm__)
    861 int
    862 linux_sys_chown(l, v, retval)
    863 	struct lwp *l;
    864 	void *v;
    865 	register_t *retval;
    866 {
    867 	struct linux_sys_chown_args /* {
    868 		syscallarg(char *) path;
    869 		syscallarg(int) uid;
    870 		syscallarg(int) gid;
    871 	} */ *uap = v;
    872 	struct proc *p = l->l_proc;
    873 	void *sg = stackgap_init(p, 0);
    874 
    875 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    876 
    877 	return sys___posix_chown(l, uap, retval);
    878 }
    879 
    880 int
    881 linux_sys_lchown(l, v, retval)
    882 	struct lwp *l;
    883 	void *v;
    884 	register_t *retval;
    885 {
    886 	struct linux_sys_lchown_args /* {
    887 		syscallarg(char *) path;
    888 		syscallarg(int) uid;
    889 		syscallarg(int) gid;
    890 	} */ *uap = v;
    891 	struct proc *p = l->l_proc;
    892 	void *sg = stackgap_init(p, 0);
    893 
    894 	CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, path));
    895 
    896 	return sys___posix_lchown(l, uap, retval);
    897 }
    898 #endif /* __i386__||__m68k__||__powerpc__||__mips__||__arm__ ||__amd64__ */
    899 
    900 int
    901 linux_sys_rename(l, v, retval)
    902 	struct lwp *l;
    903 	void *v;
    904 	register_t *retval;
    905 {
    906 	struct linux_sys_rename_args /* {
    907 		syscallarg(const char *) from;
    908 		syscallarg(const char *) to;
    909 	} */ *uap = v;
    910 	struct proc *p = l->l_proc;
    911 	void *sg = stackgap_init(p, 0);
    912 
    913 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, from));
    914 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
    915 
    916 	return sys___posix_rename(l, uap, retval);
    917 }
    918 
    919 int
    920 linux_sys_mkdir(l, v, retval)
    921 	struct lwp *l;
    922 	void *v;
    923 	register_t *retval;
    924 {
    925 	struct linux_sys_mkdir_args /* {
    926 		syscallarg(const char *) path;
    927 		syscallarg(int) mode;
    928 	} */ *uap = v;
    929 	struct proc *p = l->l_proc;
    930 	void *sg = stackgap_init(p, 0);
    931 
    932 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
    933 
    934 	return sys_mkdir(l, uap, retval);
    935 }
    936 
    937 int
    938 linux_sys_rmdir(l, v, retval)
    939 	struct lwp *l;
    940 	void *v;
    941 	register_t *retval;
    942 {
    943 	struct linux_sys_rmdir_args /* {
    944 		syscallarg(const char *) path;
    945 	} */ *uap = v;
    946 	struct proc *p = l->l_proc;
    947 	void *sg = stackgap_init(p, 0);
    948 
    949 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    950 
    951 	return sys_rmdir(l, uap, retval);
    952 }
    953 
    954 int
    955 linux_sys_symlink(l, v, retval)
    956 	struct lwp *l;
    957 	void *v;
    958 	register_t *retval;
    959 {
    960 	struct linux_sys_symlink_args /* {
    961 		syscallarg(const char *) path;
    962 		syscallarg(const char *) to;
    963 	} */ *uap = v;
    964 	struct proc *p = l->l_proc;
    965 	void *sg = stackgap_init(p, 0);
    966 
    967 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    968 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, to));
    969 
    970 	return sys_symlink(l, uap, retval);
    971 }
    972 
    973 int
    974 linux_sys_link(l, v, retval)
    975 	struct lwp *l;
    976 	void *v;
    977 	register_t *retval;
    978 {
    979 	struct linux_sys_link_args /* {
    980 		syscallarg(const char *) path;
    981 		syscallarg(const char *) link;
    982 	} */ *uap = v;
    983 	struct proc *p = l->l_proc;
    984 	void *sg = stackgap_init(p, 0);
    985 
    986 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    987 	CHECK_ALT_CREAT(l, &sg, SCARG(uap, link));
    988 
    989 	return sys_link(l, uap, retval);
    990 }
    991 
    992 int
    993 linux_sys_readlink(l, v, retval)
    994 	struct lwp *l;
    995 	void *v;
    996 	register_t *retval;
    997 {
    998 	struct linux_sys_readlink_args /* {
    999 		syscallarg(const char *) name;
   1000 		syscallarg(char *) buf;
   1001 		syscallarg(int) count;
   1002 	} */ *uap = v;
   1003 	struct proc *p = l->l_proc;
   1004 	void *sg = stackgap_init(p, 0);
   1005 
   1006 	CHECK_ALT_SYMLINK(l, &sg, SCARG(uap, name));
   1007 
   1008 	return sys_readlink(l, uap, retval);
   1009 }
   1010 
   1011 #if !defined(__amd64__)
   1012 int
   1013 linux_sys_truncate(l, v, retval)
   1014 	struct lwp *l;
   1015 	void *v;
   1016 	register_t *retval;
   1017 {
   1018 	struct linux_sys_truncate_args /* {
   1019 		syscallarg(const char *) path;
   1020 		syscallarg(long) length;
   1021 	} */ *uap = v;
   1022 	struct proc *p = l->l_proc;
   1023 	void *sg = stackgap_init(p, 0);
   1024 
   1025 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
   1026 
   1027 	return compat_43_sys_truncate(l, uap, retval);
   1028 }
   1029 #endif /* !__amd64__ */
   1030 
   1031 /*
   1032  * This is just fsync() for now (just as it is in the Linux kernel)
   1033  * Note: this is not implemented under Linux on Alpha and Arm
   1034  *	but should still be defined in our syscalls.master.
   1035  *	(syscall #148 on the arm)
   1036  */
   1037 int
   1038 linux_sys_fdatasync(l, v, retval)
   1039 	struct lwp *l;
   1040 	void *v;
   1041 	register_t *retval;
   1042 {
   1043 #ifdef notdef
   1044 	struct linux_sys_fdatasync_args /* {
   1045 		syscallarg(int) fd;
   1046 	} */ *uap = v;
   1047 #endif
   1048 	return sys_fsync(l, v, retval);
   1049 }
   1050 
   1051 /*
   1052  * pread(2).
   1053  */
   1054 int
   1055 linux_sys_pread(l, v, retval)
   1056 	struct lwp *l;
   1057 	void *v;
   1058 	register_t *retval;
   1059 {
   1060 	struct linux_sys_pread_args /* {
   1061 		syscallarg(int) fd;
   1062 		syscallarg(void *) buf;
   1063 		syscallarg(size_t) nbyte;
   1064 		syscallarg(linux_off_t) offset;
   1065 	} */ *uap = v;
   1066 	struct sys_pread_args pra;
   1067 
   1068 	SCARG(&pra, fd) = SCARG(uap, fd);
   1069 	SCARG(&pra, buf) = SCARG(uap, buf);
   1070 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
   1071 	SCARG(&pra, offset) = SCARG(uap, offset);
   1072 
   1073 	return sys_pread(l, &pra, retval);
   1074 }
   1075 
   1076 /*
   1077  * pwrite(2).
   1078  */
   1079 int
   1080 linux_sys_pwrite(l, v, retval)
   1081 	struct lwp *l;
   1082 	void *v;
   1083 	register_t *retval;
   1084 {
   1085 	struct linux_sys_pwrite_args /* {
   1086 		syscallarg(int) fd;
   1087 		syscallarg(void *) buf;
   1088 		syscallarg(size_t) nbyte;
   1089 		syscallarg(linux_off_t) offset;
   1090 	} */ *uap = v;
   1091 	struct sys_pwrite_args pra;
   1092 
   1093 	SCARG(&pra, fd) = SCARG(uap, fd);
   1094 	SCARG(&pra, buf) = SCARG(uap, buf);
   1095 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
   1096 	SCARG(&pra, offset) = SCARG(uap, offset);
   1097 
   1098 	return sys_pwrite(l, &pra, retval);
   1099 }
   1100 
   1101 #define LINUX_NOT_SUPPORTED(fun) \
   1102 int \
   1103 fun(struct lwp *l, void *v, register_t *retval) \
   1104 { \
   1105 	return EOPNOTSUPP; \
   1106 }
   1107 
   1108 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
   1109 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
   1110 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
   1111 
   1112 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
   1113 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
   1114 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
   1115 
   1116 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
   1117 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
   1118 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
   1119 
   1120 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
   1121 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
   1122 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
   1123