Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.77.2.3
      1 /*	$NetBSD: linux_file.c,v 1.77.2.3 2007/05/07 10:55:12 yamt 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.77.2.3 2007/05/07 10:55:12 yamt 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/namei.h>
     59 #include <sys/vnode.h>
     60 #include <sys/tty.h>
     61 #include <sys/socketvar.h>
     62 #include <sys/conf.h>
     63 #include <sys/pipe.h>
     64 
     65 #include <sys/syscallargs.h>
     66 #include <sys/vfs_syscalls.h>
     67 
     68 #include <compat/linux/common/linux_types.h>
     69 #include <compat/linux/common/linux_signal.h>
     70 #include <compat/linux/common/linux_fcntl.h>
     71 #include <compat/linux/common/linux_util.h>
     72 #include <compat/linux/common/linux_machdep.h>
     73 
     74 #include <compat/linux/linux_syscallargs.h>
     75 
     76 static int linux_to_bsd_ioflags __P((int));
     77 static int bsd_to_linux_ioflags __P((int));
     78 static void bsd_to_linux_flock __P((struct flock *, struct linux_flock *));
     79 static void linux_to_bsd_flock __P((struct linux_flock *, struct flock *));
     80 #ifndef __amd64__
     81 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
     82 static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
     83 #endif
     84 
     85 /*
     86  * Some file-related calls are handled here. The usual flag conversion
     87  * an structure conversion is done, and alternate emul path searching.
     88  */
     89 
     90 /*
     91  * The next two functions convert between the Linux and NetBSD values
     92  * of the flags used in open(2) and fcntl(2).
     93  */
     94 static int
     95 linux_to_bsd_ioflags(lflags)
     96 	int lflags;
     97 {
     98 	int res = 0;
     99 
    100 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
    101 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
    102 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
    103 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
    104 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
    105 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
    106 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
    107 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
    108 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
    109 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
    110 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
    111 
    112 	return res;
    113 }
    114 
    115 static int
    116 bsd_to_linux_ioflags(bflags)
    117 	int bflags;
    118 {
    119 	int res = 0;
    120 
    121 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    122 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    123 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    124 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    125 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    126 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    127 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    128 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    129 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    130 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    131 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    132 
    133 	return res;
    134 }
    135 
    136 /*
    137  * creat(2) is an obsolete function, but it's present as a Linux
    138  * system call, so let's deal with it.
    139  *
    140  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    141  * in syscalls.master anyway so this doesn't have to be special cased.
    142  *
    143  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    144  */
    145 int
    146 linux_sys_creat(l, v, retval)
    147 	struct lwp *l;
    148 	void *v;
    149 	register_t *retval;
    150 {
    151 	struct linux_sys_creat_args /* {
    152 		syscallarg(const char *) path;
    153 		syscallarg(int) mode;
    154 	} */ *uap = v;
    155 	struct sys_open_args oa;
    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(l, &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(l, v, retval)
    172 	struct lwp *l;
    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 	struct proc *p = l->l_proc;
    182 	int error, fl;
    183 	struct sys_open_args boa;
    184 
    185 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    186 
    187 	SCARG(&boa, path) = SCARG(uap, path);
    188 	SCARG(&boa, flags) = fl;
    189 	SCARG(&boa, mode) = SCARG(uap, mode);
    190 
    191 	if ((error = sys_open(l, &boa, retval)))
    192 		return error;
    193 
    194 	/*
    195 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    196 	 * If we are a session leader, and we don't have a controlling
    197 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    198 	 * this the controlling terminal.
    199 	 */
    200         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    201                 struct filedesc *fdp = p->p_fd;
    202                 struct file     *fp;
    203 
    204 		fp = fd_getfile(fdp, *retval);
    205 
    206                 /* ignore any error, just give it a try */
    207                 if (fp != NULL) {
    208 			FILE_USE(fp);
    209 			if (fp->f_type == DTYPE_VNODE) {
    210 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
    211 				    (void *) 0, l);
    212 			}
    213 			FILE_UNUSE(fp, l);
    214 		}
    215         }
    216 	return 0;
    217 }
    218 
    219 /*
    220  * The next two functions take care of converting the flock
    221  * structure back and forth between Linux and NetBSD format.
    222  * The only difference in the structures is the order of
    223  * the fields, and the 'whence' value.
    224  */
    225 static void
    226 bsd_to_linux_flock(bfp, lfp)
    227 	struct flock *bfp;
    228 	struct linux_flock *lfp;
    229 {
    230 
    231 	lfp->l_start = bfp->l_start;
    232 	lfp->l_len = bfp->l_len;
    233 	lfp->l_pid = bfp->l_pid;
    234 	lfp->l_whence = bfp->l_whence;
    235 	switch (bfp->l_type) {
    236 	case F_RDLCK:
    237 		lfp->l_type = LINUX_F_RDLCK;
    238 		break;
    239 	case F_UNLCK:
    240 		lfp->l_type = LINUX_F_UNLCK;
    241 		break;
    242 	case F_WRLCK:
    243 		lfp->l_type = LINUX_F_WRLCK;
    244 		break;
    245 	}
    246 }
    247 
    248 static void
    249 linux_to_bsd_flock(lfp, bfp)
    250 	struct linux_flock *lfp;
    251 	struct flock *bfp;
    252 {
    253 
    254 	bfp->l_start = lfp->l_start;
    255 	bfp->l_len = lfp->l_len;
    256 	bfp->l_pid = lfp->l_pid;
    257 	bfp->l_whence = lfp->l_whence;
    258 	switch (lfp->l_type) {
    259 	case LINUX_F_RDLCK:
    260 		bfp->l_type = F_RDLCK;
    261 		break;
    262 	case LINUX_F_UNLCK:
    263 		bfp->l_type = F_UNLCK;
    264 		break;
    265 	case LINUX_F_WRLCK:
    266 		bfp->l_type = F_WRLCK;
    267 		break;
    268 	}
    269 }
    270 
    271 /*
    272  * Most actions in the fcntl() call are straightforward; simply
    273  * pass control to the NetBSD system call. A few commands need
    274  * conversions after the actual system call has done its work,
    275  * because the flag values and lock structure are different.
    276  */
    277 int
    278 linux_sys_fcntl(l, v, retval)
    279 	struct lwp *l;
    280 	void *v;
    281 	register_t *retval;
    282 {
    283 	struct linux_sys_fcntl_args /* {
    284 		syscallarg(int) fd;
    285 		syscallarg(int) cmd;
    286 		syscallarg(void *) arg;
    287 	} */ *uap = v;
    288 	struct proc *p = l->l_proc;
    289 	int fd, cmd, error;
    290 	u_long val;
    291 	void *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 	const struct cdevsw *cdev;
    300 	long pgid;
    301 	struct pgrp *pgrp;
    302 	struct tty *tp, *(*d_tty) __P((dev_t));
    303 
    304 	fd = SCARG(uap, fd);
    305 	cmd = SCARG(uap, cmd);
    306 	arg = (void *) SCARG(uap, arg);
    307 
    308 	switch (cmd) {
    309 	case LINUX_F_DUPFD:
    310 		cmd = F_DUPFD;
    311 		break;
    312 	case LINUX_F_GETFD:
    313 		cmd = F_GETFD;
    314 		break;
    315 	case LINUX_F_SETFD:
    316 		cmd = F_SETFD;
    317 		break;
    318 	case LINUX_F_GETFL:
    319 		SCARG(&fca, fd) = fd;
    320 		SCARG(&fca, cmd) = F_GETFL;
    321 		SCARG(&fca, arg) = arg;
    322 		if ((error = sys_fcntl(l, &fca, retval)))
    323 			return error;
    324 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    325 		return 0;
    326 	case LINUX_F_SETFL: {
    327 		struct file	*fp1 = NULL;
    328 
    329 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    330 		/*
    331 		 * Linux seems to have same semantics for sending SIGIO to the
    332 		 * read side of socket, but slightly 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 (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
    348 			    return (EBADF);
    349 
    350 			FILE_USE(fp1);
    351 
    352 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    353 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    354 			    || (fp1->f_type == DTYPE_PIPE))
    355 				val &= ~O_ASYNC;
    356 			else {
    357 				/* not a pipe, do not modify anything */
    358 				FILE_UNUSE(fp1, l);
    359 				fp1 = NULL;
    360 			}
    361 		}
    362 
    363 		SCARG(&fca, fd) = fd;
    364 		SCARG(&fca, cmd) = F_SETFL;
    365 		SCARG(&fca, arg) = (void *) val;
    366 
    367 		error = sys_fcntl(l, &fca, retval);
    368 
    369 		/* Now set the FASYNC flag for pipes */
    370 		if (fp1) {
    371 			if (!error)
    372 				fp1->f_flag |= FASYNC;
    373 			FILE_UNUSE(fp1, l);
    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(l, &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 
    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, 0);
    403 		bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
    404 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    405 			return error;
    406 		arg = (void *)bfp;
    407 		break;
    408 
    409 	case LINUX_F_SETOWN:
    410 	case LINUX_F_GETOWN:
    411 		/*
    412 		 * We need to route fcntl() for tty descriptors around normal
    413 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    414 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    415 		 * this is not a problem.
    416 		 */
    417 		fdp = p->p_fd;
    418 		if ((fp = fd_getfile(fdp, fd)) == NULL)
    419 			return EBADF;
    420 		FILE_USE(fp);
    421 
    422 		/* Check it's a character device vnode */
    423 		if (fp->f_type != DTYPE_VNODE
    424 		    || (vp = (struct vnode *)fp->f_data) == NULL
    425 		    || vp->v_type != VCHR) {
    426 			FILE_UNUSE(fp, l);
    427 
    428 	    not_tty:
    429 			/* Not a tty, proceed with common fcntl() */
    430 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    431 			break;
    432 		}
    433 
    434 		error = VOP_GETATTR(vp, &va, l->l_cred, l);
    435 
    436 		FILE_UNUSE(fp, l);
    437 
    438 		if (error)
    439 			return error;
    440 
    441 		cdev = cdevsw_lookup(va.va_rdev);
    442 		if (cdev == NULL)
    443 			return (ENXIO);
    444 		d_tty = cdev->d_tty;
    445 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
    446 			goto not_tty;
    447 
    448 		/* set tty pg_id appropriately */
    449 		if (cmd == LINUX_F_GETOWN) {
    450 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    451 			return 0;
    452 		}
    453 		mutex_enter(&proclist_lock);
    454 		if ((long)arg <= 0) {
    455 			pgid = -(long)arg;
    456 		} else {
    457 			struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
    458 			if (p1 == NULL)
    459 				return (ESRCH);
    460 			pgid = (long)p1->p_pgrp->pg_id;
    461 		}
    462 		pgrp = pg_find(pgid, PFIND_LOCKED);
    463 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    464 			mutex_exit(&proclist_lock);
    465 			return EPERM;
    466 		}
    467 		tp->t_pgrp = pgrp;
    468 		mutex_exit(&proclist_lock);
    469 		return 0;
    470 
    471 	default:
    472 		return EOPNOTSUPP;
    473 	}
    474 
    475 	SCARG(&fca, fd) = fd;
    476 	SCARG(&fca, cmd) = cmd;
    477 	SCARG(&fca, arg) = arg;
    478 
    479 	return sys_fcntl(l, &fca, retval);
    480 }
    481 
    482 #if !defined(__amd64__)
    483 /*
    484  * Convert a NetBSD stat structure to a Linux stat structure.
    485  * Only the order of the fields and the padding in the structure
    486  * is different. linux_fakedev is a machine-dependent function
    487  * which optionally converts device driver major/minor numbers
    488  * (XXX horrible, but what can you do against code that compares
    489  * things against constant major device numbers? sigh)
    490  */
    491 static void
    492 bsd_to_linux_stat(bsp, lsp)
    493 	struct stat *bsp;
    494 	struct linux_stat *lsp;
    495 {
    496 
    497 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    498 	lsp->lst_ino     = bsp->st_ino;
    499 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    500 	if (bsp->st_nlink >= (1 << 15))
    501 		lsp->lst_nlink = (1 << 15) - 1;
    502 	else
    503 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    504 	lsp->lst_uid     = bsp->st_uid;
    505 	lsp->lst_gid     = bsp->st_gid;
    506 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    507 	lsp->lst_size    = bsp->st_size;
    508 	lsp->lst_blksize = bsp->st_blksize;
    509 	lsp->lst_blocks  = bsp->st_blocks;
    510 	lsp->lst_atime   = bsp->st_atime;
    511 	lsp->lst_mtime   = bsp->st_mtime;
    512 	lsp->lst_ctime   = bsp->st_ctime;
    513 #ifdef LINUX_STAT_HAS_NSEC
    514 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    515 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    516 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    517 #endif
    518 }
    519 
    520 /*
    521  * The stat functions below are plain sailing. stat and lstat are handled
    522  * by one function to avoid code duplication.
    523  */
    524 int
    525 linux_sys_fstat(l, v, retval)
    526 	struct lwp *l;
    527 	void *v;
    528 	register_t *retval;
    529 {
    530 	struct linux_sys_fstat_args /* {
    531 		syscallarg(int) fd;
    532 		syscallarg(linux_stat *) sp;
    533 	} */ *uap = v;
    534 	struct linux_stat tmplst;
    535 	struct stat tmpst;
    536 	int error;
    537 
    538 	error = do_sys_fstat(l, SCARG(uap, fd), &tmpst);
    539 	if (error != 0)
    540 		return error;
    541 	bsd_to_linux_stat(&tmpst, &tmplst);
    542 
    543 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    544 }
    545 
    546 static int
    547 linux_stat1(l, v, retval, flags)
    548 	struct lwp *l;
    549 	void *v;
    550 	register_t *retval;
    551 	int flags;
    552 {
    553 	struct linux_stat tmplst;
    554 	struct stat tmpst;
    555 	int error;
    556 	struct linux_sys_stat_args *uap = v;
    557 
    558 	error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
    559 	if (error != 0)
    560 		return error;
    561 
    562 	bsd_to_linux_stat(&tmpst, &tmplst);
    563 
    564 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    565 }
    566 
    567 int
    568 linux_sys_stat(l, v, retval)
    569 	struct lwp *l;
    570 	void *v;
    571 	register_t *retval;
    572 {
    573 	struct linux_sys_stat_args /* {
    574 		syscallarg(const char *) path;
    575 		syscallarg(struct linux_stat *) sp;
    576 	} */ *uap = v;
    577 
    578 	return linux_stat1(l, uap, retval, FOLLOW);
    579 }
    580 
    581 /* Note: this is "newlstat" in the Linux sources */
    582 /*	(we don't bother with the old lstat currently) */
    583 int
    584 linux_sys_lstat(l, v, retval)
    585 	struct lwp *l;
    586 	void *v;
    587 	register_t *retval;
    588 {
    589 	struct linux_sys_lstat_args /* {
    590 		syscallarg(const char *) path;
    591 		syscallarg(struct linux_stat *) sp;
    592 	} */ *uap = v;
    593 
    594 	return linux_stat1(l, uap, retval, NOFOLLOW);
    595 }
    596 #endif /* !__amd64__ */
    597 
    598 /*
    599  * The following syscalls are mostly here because of the alternate path check.
    600  */
    601 int
    602 linux_sys_access(l, v, retval)
    603 	struct lwp *l;
    604 	void *v;
    605 	register_t *retval;
    606 {
    607 	struct linux_sys_access_args /* {
    608 		syscallarg(const char *) path;
    609 		syscallarg(int) flags;
    610 	} */ *uap = v;
    611 
    612 	return sys_access(l, uap, retval);
    613 }
    614 
    615 int
    616 linux_sys_unlink(l, v, retval)
    617 	struct lwp *l;
    618 	void *v;
    619 	register_t *retval;
    620 
    621 {
    622 	struct linux_sys_unlink_args /* {
    623 		syscallarg(const char *) path;
    624 	} */ *uap = v;
    625 	int error;
    626 	struct nameidata nd;
    627 
    628 	error = sys_unlink(l, uap, retval);
    629 	if (error != EPERM)
    630 		return (error);
    631 
    632 	/*
    633 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    634 	 * We return EPERM in such cases. To emulate correct behaviour,
    635 	 * check if the path points to directory and return EISDIR if this
    636 	 * is the case.
    637 	 */
    638 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
    639 	    SCARG(uap, path), l);
    640 	if (namei(&nd) == 0) {
    641 		struct stat sb;
    642 
    643 		if (vn_stat(nd.ni_vp, &sb, l) == 0
    644 		    && S_ISDIR(sb.st_mode))
    645 			error = EISDIR;
    646 
    647 		vput(nd.ni_vp);
    648 	}
    649 
    650 	return (error);
    651 }
    652 
    653 int
    654 linux_sys_chdir(l, v, retval)
    655 	struct lwp *l;
    656 	void *v;
    657 	register_t *retval;
    658 {
    659 	struct linux_sys_chdir_args /* {
    660 		syscallarg(const char *) path;
    661 	} */ *uap = v;
    662 
    663 	return sys_chdir(l, uap, retval);
    664 }
    665 
    666 int
    667 linux_sys_mknod(l, v, retval)
    668 	struct lwp *l;
    669 	void *v;
    670 	register_t *retval;
    671 {
    672 	struct linux_sys_mknod_args /* {
    673 		syscallarg(const char *) path;
    674 		syscallarg(int) mode;
    675 		syscallarg(int) dev;
    676 	} */ *uap = v;
    677 
    678 	/*
    679 	 * BSD handles FIFOs separately
    680 	 */
    681 	if (S_ISFIFO(SCARG(uap, mode))) {
    682 		struct sys_mkfifo_args bma;
    683 
    684 		SCARG(&bma, path) = SCARG(uap, path);
    685 		SCARG(&bma, mode) = SCARG(uap, mode);
    686 		return sys_mkfifo(l, &bma, retval);
    687 	} else {
    688 		struct sys_mknod_args bma;
    689 
    690 		SCARG(&bma, path) = SCARG(uap, path);
    691 		SCARG(&bma, mode) = SCARG(uap, mode);
    692 		/*
    693 		 * Linux device numbers uses 8 bits for minor and 8 bits
    694 		 * for major. Due to how we map our major and minor,
    695 		 * this just fits into our dev_t. Just mask off the
    696 		 * upper 16bit to remove any random junk.
    697 		 */
    698 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
    699 		return sys_mknod(l, &bma, retval);
    700 	}
    701 }
    702 
    703 int
    704 linux_sys_chmod(l, v, retval)
    705 	struct lwp *l;
    706 	void *v;
    707 	register_t *retval;
    708 {
    709 	struct linux_sys_chmod_args /* {
    710 		syscallarg(const char *) path;
    711 		syscallarg(int) mode;
    712 	} */ *uap = v;
    713 
    714 	return sys_chmod(l, uap, retval);
    715 }
    716 
    717 #if defined(__i386__) || defined(__m68k__) || \
    718     defined(__arm__)
    719 int
    720 linux_sys_chown16(l, v, retval)
    721 	struct lwp *l;
    722 	void *v;
    723 	register_t *retval;
    724 {
    725 	struct linux_sys_chown16_args /* {
    726 		syscallarg(const char *) path;
    727 		syscallarg(int) uid;
    728 		syscallarg(int) gid;
    729 	} */ *uap = v;
    730 	struct sys___posix_chown_args bca;
    731 
    732 	SCARG(&bca, path) = SCARG(uap, path);
    733 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    734 		(uid_t)-1 : SCARG(uap, uid);
    735 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    736 		(gid_t)-1 : SCARG(uap, gid);
    737 
    738 	return sys___posix_chown(l, &bca, retval);
    739 }
    740 
    741 int
    742 linux_sys_fchown16(l, v, retval)
    743 	struct lwp *l;
    744 	void *v;
    745 	register_t *retval;
    746 {
    747 	struct linux_sys_fchown16_args /* {
    748 		syscallarg(int) fd;
    749 		syscallarg(int) uid;
    750 		syscallarg(int) gid;
    751 	} */ *uap = v;
    752 	struct sys___posix_fchown_args bfa;
    753 
    754 	SCARG(&bfa, fd) = SCARG(uap, fd);
    755 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    756 		(uid_t)-1 : SCARG(uap, uid);
    757 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    758 		(gid_t)-1 : SCARG(uap, gid);
    759 
    760 	return sys___posix_fchown(l, &bfa, retval);
    761 }
    762 
    763 int
    764 linux_sys_lchown16(l, v, retval)
    765 	struct lwp *l;
    766 	void *v;
    767 	register_t *retval;
    768 {
    769 	struct linux_sys_lchown16_args /* {
    770 		syscallarg(char *) path;
    771 		syscallarg(int) uid;
    772 		syscallarg(int) gid;
    773 	} */ *uap = v;
    774 	struct sys___posix_lchown_args bla;
    775 
    776 	SCARG(&bla, path) = SCARG(uap, path);
    777 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    778 		(uid_t)-1 : SCARG(uap, uid);
    779 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    780 		(gid_t)-1 : SCARG(uap, gid);
    781 
    782 	return sys___posix_lchown(l, &bla, retval);
    783 }
    784 #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
    785 #if defined (__i386__) || defined (__m68k__) || defined(__amd64__) || \
    786     defined (__powerpc__) || defined (__mips__) || defined (__arm__)
    787 int
    788 linux_sys_chown(l, v, retval)
    789 	struct lwp *l;
    790 	void *v;
    791 	register_t *retval;
    792 {
    793 	struct linux_sys_chown_args /* {
    794 		syscallarg(char *) path;
    795 		syscallarg(int) uid;
    796 		syscallarg(int) gid;
    797 	} */ *uap = v;
    798 
    799 	return sys___posix_chown(l, uap, retval);
    800 }
    801 
    802 int
    803 linux_sys_lchown(l, v, retval)
    804 	struct lwp *l;
    805 	void *v;
    806 	register_t *retval;
    807 {
    808 	struct linux_sys_lchown_args /* {
    809 		syscallarg(char *) path;
    810 		syscallarg(int) uid;
    811 		syscallarg(int) gid;
    812 	} */ *uap = v;
    813 
    814 	return sys___posix_lchown(l, uap, retval);
    815 }
    816 #endif /* __i386__||__m68k__||__powerpc__||__mips__||__arm__ ||__amd64__ */
    817 
    818 int
    819 linux_sys_rename(l, v, retval)
    820 	struct lwp *l;
    821 	void *v;
    822 	register_t *retval;
    823 {
    824 	struct linux_sys_rename_args /* {
    825 		syscallarg(const char *) from;
    826 		syscallarg(const char *) to;
    827 	} */ *uap = v;
    828 
    829 	return sys___posix_rename(l, uap, retval);
    830 }
    831 
    832 int
    833 linux_sys_mkdir(l, v, retval)
    834 	struct lwp *l;
    835 	void *v;
    836 	register_t *retval;
    837 {
    838 	struct linux_sys_mkdir_args /* {
    839 		syscallarg(const char *) path;
    840 		syscallarg(int) mode;
    841 	} */ *uap = v;
    842 
    843 	return sys_mkdir(l, uap, retval);
    844 }
    845 
    846 int
    847 linux_sys_rmdir(l, v, retval)
    848 	struct lwp *l;
    849 	void *v;
    850 	register_t *retval;
    851 {
    852 	struct linux_sys_rmdir_args /* {
    853 		syscallarg(const char *) path;
    854 	} */ *uap = v;
    855 
    856 	return sys_rmdir(l, uap, retval);
    857 }
    858 
    859 int
    860 linux_sys_symlink(l, v, retval)
    861 	struct lwp *l;
    862 	void *v;
    863 	register_t *retval;
    864 {
    865 	struct linux_sys_symlink_args /* {
    866 		syscallarg(const char *) path;
    867 		syscallarg(const char *) to;
    868 	} */ *uap = v;
    869 
    870 	return sys_symlink(l, uap, retval);
    871 }
    872 
    873 int
    874 linux_sys_link(l, v, retval)
    875 	struct lwp *l;
    876 	void *v;
    877 	register_t *retval;
    878 {
    879 	struct linux_sys_link_args /* {
    880 		syscallarg(const char *) path;
    881 		syscallarg(const char *) link;
    882 	} */ *uap = v;
    883 
    884 	return sys_link(l, uap, retval);
    885 }
    886 
    887 int
    888 linux_sys_readlink(l, v, retval)
    889 	struct lwp *l;
    890 	void *v;
    891 	register_t *retval;
    892 {
    893 	struct linux_sys_readlink_args /* {
    894 		syscallarg(const char *) name;
    895 		syscallarg(char *) buf;
    896 		syscallarg(int) count;
    897 	} */ *uap = v;
    898 
    899 	return sys_readlink(l, uap, retval);
    900 }
    901 
    902 #if !defined(__amd64__)
    903 int
    904 linux_sys_truncate(l, v, retval)
    905 	struct lwp *l;
    906 	void *v;
    907 	register_t *retval;
    908 {
    909 	struct linux_sys_truncate_args /* {
    910 		syscallarg(const char *) path;
    911 		syscallarg(long) length;
    912 	} */ *uap = v;
    913 
    914 	return compat_43_sys_truncate(l, uap, retval);
    915 }
    916 #endif /* !__amd64__ */
    917 
    918 /*
    919  * This is just fsync() for now (just as it is in the Linux kernel)
    920  * Note: this is not implemented under Linux on Alpha and Arm
    921  *	but should still be defined in our syscalls.master.
    922  *	(syscall #148 on the arm)
    923  */
    924 int
    925 linux_sys_fdatasync(l, v, retval)
    926 	struct lwp *l;
    927 	void *v;
    928 	register_t *retval;
    929 {
    930 #ifdef notdef
    931 	struct linux_sys_fdatasync_args /* {
    932 		syscallarg(int) fd;
    933 	} */ *uap = v;
    934 #endif
    935 	return sys_fsync(l, v, retval);
    936 }
    937 
    938 /*
    939  * pread(2).
    940  */
    941 int
    942 linux_sys_pread(l, v, retval)
    943 	struct lwp *l;
    944 	void *v;
    945 	register_t *retval;
    946 {
    947 	struct linux_sys_pread_args /* {
    948 		syscallarg(int) fd;
    949 		syscallarg(void *) buf;
    950 		syscallarg(size_t) nbyte;
    951 		syscallarg(linux_off_t) offset;
    952 	} */ *uap = v;
    953 	struct sys_pread_args pra;
    954 
    955 	SCARG(&pra, fd) = SCARG(uap, fd);
    956 	SCARG(&pra, buf) = SCARG(uap, buf);
    957 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    958 	SCARG(&pra, offset) = SCARG(uap, offset);
    959 
    960 	return sys_pread(l, &pra, retval);
    961 }
    962 
    963 /*
    964  * pwrite(2).
    965  */
    966 int
    967 linux_sys_pwrite(l, v, retval)
    968 	struct lwp *l;
    969 	void *v;
    970 	register_t *retval;
    971 {
    972 	struct linux_sys_pwrite_args /* {
    973 		syscallarg(int) fd;
    974 		syscallarg(void *) buf;
    975 		syscallarg(size_t) nbyte;
    976 		syscallarg(linux_off_t) offset;
    977 	} */ *uap = v;
    978 	struct sys_pwrite_args pra;
    979 
    980 	SCARG(&pra, fd) = SCARG(uap, fd);
    981 	SCARG(&pra, buf) = SCARG(uap, buf);
    982 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    983 	SCARG(&pra, offset) = SCARG(uap, offset);
    984 
    985 	return sys_pwrite(l, &pra, retval);
    986 }
    987 
    988 #define LINUX_NOT_SUPPORTED(fun) \
    989 int \
    990 fun(struct lwp *l, void *v, register_t *retval) \
    991 { \
    992 	return EOPNOTSUPP; \
    993 }
    994 
    995 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
    996 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
    997 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
    998 
    999 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
   1000 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
   1001 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
   1002 
   1003 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
   1004 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
   1005 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
   1006 
   1007 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
   1008 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
   1009 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
   1010